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/hex.h> 37 #include <linux/init.h> 38 #include <linux/slab.h> 39 #include <linux/err.h> 40 #include <linux/ctype.h> 41 #include <linux/kthread.h> 42 #include <linux/string.h> 43 #include <linux/delay.h> 44 #include <linux/atomic.h> 45 #include <linux/inet.h> 46 #include <rdma/ib_cache.h> 47 #include <scsi/scsi_proto.h> 48 #include <scsi/scsi_tcq.h> 49 #include <target/target_core_base.h> 50 #include <target/target_core_fabric.h> 51 #include "ib_srpt.h" 52 53 /* Name of this kernel module. */ 54 #define DRV_NAME "ib_srpt" 55 56 #define SRPT_ID_STRING "Linux SRP target" 57 58 #undef pr_fmt 59 #define pr_fmt(fmt) DRV_NAME " " fmt 60 61 MODULE_AUTHOR("Vu Pham and Bart Van Assche"); 62 MODULE_DESCRIPTION("SCSI RDMA Protocol target driver"); 63 MODULE_LICENSE("Dual BSD/GPL"); 64 65 /* 66 * Global Variables 67 */ 68 69 static u64 srpt_service_guid; 70 static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */ 71 static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */ 72 static DEFINE_MUTEX(srpt_mc_mutex); /* Protects srpt_memory_caches. */ 73 static DEFINE_XARRAY(srpt_memory_caches); /* See also srpt_memory_cache_entry */ 74 75 static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE; 76 module_param(srp_max_req_size, int, 0444); 77 MODULE_PARM_DESC(srp_max_req_size, 78 "Maximum size of SRP request messages in bytes."); 79 80 static unsigned int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE; 81 module_param(srpt_srq_size, uint, 0444); 82 MODULE_PARM_DESC(srpt_srq_size, 83 "Shared receive queue (SRQ) size."); 84 85 static int srpt_set_u64_x(const char *buffer, const struct kernel_param *kp) 86 { 87 return kstrtou64(buffer, 16, (u64 *)kp->arg); 88 } 89 static int srpt_get_u64_x(char *buffer, const struct kernel_param *kp) 90 { 91 return sprintf(buffer, "0x%016llx\n", *(u64 *)kp->arg); 92 } 93 module_param_call(srpt_service_guid, srpt_set_u64_x, srpt_get_u64_x, 94 &srpt_service_guid, 0444); 95 MODULE_PARM_DESC(srpt_service_guid, 96 "Using this value for ioc_guid, id_ext, and cm_listen_id instead of using the node_guid of the first HCA."); 97 98 static struct ib_client srpt_client; 99 /* Protects both rdma_cm_port and rdma_cm_id. */ 100 static DEFINE_MUTEX(rdma_cm_mutex); 101 /* Port number RDMA/CM will bind to. */ 102 static u16 rdma_cm_port; 103 static struct rdma_cm_id *rdma_cm_id; 104 static void srpt_release_cmd(struct se_cmd *se_cmd); 105 static void srpt_free_ch(struct kref *kref); 106 static int srpt_queue_status(struct se_cmd *cmd); 107 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc); 108 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc); 109 static void srpt_process_wait_list(struct srpt_rdma_ch *ch); 110 111 /* Type of the entries in srpt_memory_caches. */ 112 struct srpt_memory_cache_entry { 113 refcount_t ref; 114 struct kmem_cache *c; 115 }; 116 117 static struct kmem_cache *srpt_cache_get(unsigned int object_size) 118 { 119 struct srpt_memory_cache_entry *e; 120 char name[32]; 121 void *res; 122 123 guard(mutex)(&srpt_mc_mutex); 124 e = xa_load(&srpt_memory_caches, object_size); 125 if (e) { 126 refcount_inc(&e->ref); 127 return e->c; 128 } 129 snprintf(name, sizeof(name), "srpt-%u", object_size); 130 e = kmalloc_obj(*e); 131 if (!e) 132 return NULL; 133 refcount_set(&e->ref, 1); 134 e->c = kmem_cache_create(name, object_size, /*align=*/512, 0, NULL); 135 if (!e->c) 136 goto free_entry; 137 res = xa_store(&srpt_memory_caches, object_size, e, GFP_KERNEL); 138 if (xa_is_err(res)) 139 goto destroy_cache; 140 return e->c; 141 142 destroy_cache: 143 kmem_cache_destroy(e->c); 144 145 free_entry: 146 kfree(e); 147 return NULL; 148 } 149 150 static void srpt_cache_put(struct kmem_cache *c) 151 { 152 struct srpt_memory_cache_entry *e = NULL; 153 unsigned long object_size; 154 155 guard(mutex)(&srpt_mc_mutex); 156 xa_for_each(&srpt_memory_caches, object_size, e) 157 if (e->c == c) 158 break; 159 if (WARN_ON_ONCE(!e)) 160 return; 161 if (!refcount_dec_and_test(&e->ref)) 162 return; 163 WARN_ON_ONCE(xa_erase(&srpt_memory_caches, object_size) != e); 164 kmem_cache_destroy(e->c); 165 kfree(e); 166 } 167 168 /* 169 * The only allowed channel state changes are those that change the channel 170 * state into a state with a higher numerical value. Hence the new > prev test. 171 */ 172 static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new) 173 { 174 unsigned long flags; 175 enum rdma_ch_state prev; 176 bool changed = false; 177 178 spin_lock_irqsave(&ch->spinlock, flags); 179 prev = ch->state; 180 if (new > prev) { 181 ch->state = new; 182 changed = true; 183 } 184 spin_unlock_irqrestore(&ch->spinlock, flags); 185 186 return changed; 187 } 188 189 /** 190 * srpt_event_handler - asynchronous IB event callback function 191 * @handler: IB event handler registered by ib_register_event_handler(). 192 * @event: Description of the event that occurred. 193 * 194 * Callback function called by the InfiniBand core when an asynchronous IB 195 * event occurs. This callback may occur in interrupt context. See also 196 * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand 197 * Architecture Specification. 198 */ 199 static void srpt_event_handler(struct ib_event_handler *handler, 200 struct ib_event *event) 201 { 202 struct srpt_device *sdev = 203 container_of(handler, struct srpt_device, event_handler); 204 struct srpt_port *sport; 205 u8 port_num; 206 207 pr_debug("ASYNC event= %d on device= %s\n", event->event, 208 dev_name(&sdev->device->dev)); 209 210 switch (event->event) { 211 case IB_EVENT_PORT_ERR: 212 port_num = event->element.port_num - 1; 213 if (port_num < sdev->device->phys_port_cnt) { 214 sport = &sdev->port[port_num]; 215 sport->lid = 0; 216 sport->sm_lid = 0; 217 } else { 218 WARN(true, "event %d: port_num %d out of range 1..%d\n", 219 event->event, port_num + 1, 220 sdev->device->phys_port_cnt); 221 } 222 break; 223 case IB_EVENT_PORT_ACTIVE: 224 case IB_EVENT_LID_CHANGE: 225 case IB_EVENT_PKEY_CHANGE: 226 case IB_EVENT_SM_CHANGE: 227 case IB_EVENT_CLIENT_REREGISTER: 228 case IB_EVENT_GID_CHANGE: 229 /* Refresh port data asynchronously. */ 230 port_num = event->element.port_num - 1; 231 if (port_num < sdev->device->phys_port_cnt) { 232 sport = &sdev->port[port_num]; 233 if (!sport->lid && !sport->sm_lid) 234 schedule_work(&sport->work); 235 } else { 236 WARN(true, "event %d: port_num %d out of range 1..%d\n", 237 event->event, port_num + 1, 238 sdev->device->phys_port_cnt); 239 } 240 break; 241 default: 242 pr_err("received unrecognized IB event %d\n", event->event); 243 break; 244 } 245 } 246 247 /** 248 * srpt_srq_event - SRQ event callback function 249 * @event: Description of the event that occurred. 250 * @ctx: Context pointer specified at SRQ creation time. 251 */ 252 static void srpt_srq_event(struct ib_event *event, void *ctx) 253 { 254 pr_debug("SRQ event %d\n", event->event); 255 } 256 257 static const char *get_ch_state_name(enum rdma_ch_state s) 258 { 259 switch (s) { 260 case CH_CONNECTING: 261 return "connecting"; 262 case CH_LIVE: 263 return "live"; 264 case CH_DISCONNECTING: 265 return "disconnecting"; 266 case CH_DRAINING: 267 return "draining"; 268 case CH_DISCONNECTED: 269 return "disconnected"; 270 } 271 return "???"; 272 } 273 274 /** 275 * srpt_qp_event - QP event callback function 276 * @event: Description of the event that occurred. 277 * @ptr: SRPT RDMA channel. 278 */ 279 static void srpt_qp_event(struct ib_event *event, void *ptr) 280 { 281 struct srpt_rdma_ch *ch = ptr; 282 283 pr_debug("QP event %d on ch=%p sess_name=%s-%d state=%s\n", 284 event->event, ch, ch->sess_name, ch->qp->qp_num, 285 get_ch_state_name(ch->state)); 286 287 switch (event->event) { 288 case IB_EVENT_COMM_EST: 289 if (ch->using_rdma_cm) 290 rdma_notify(ch->rdma_cm.cm_id, event->event); 291 else 292 ib_cm_notify(ch->ib_cm.cm_id, event->event); 293 break; 294 case IB_EVENT_QP_LAST_WQE_REACHED: 295 pr_debug("%s-%d, state %s: received Last WQE event.\n", 296 ch->sess_name, ch->qp->qp_num, 297 get_ch_state_name(ch->state)); 298 break; 299 default: 300 pr_err("received unrecognized IB QP event %d\n", event->event); 301 break; 302 } 303 } 304 305 /** 306 * srpt_set_ioc - initialize a IOUnitInfo structure 307 * @c_list: controller list. 308 * @slot: one-based slot number. 309 * @value: four-bit value. 310 * 311 * Copies the lowest four bits of value in element slot of the array of four 312 * bit elements called c_list (controller list). The index slot is one-based. 313 */ 314 static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value) 315 { 316 u16 id; 317 u8 tmp; 318 319 id = (slot - 1) / 2; 320 if (slot & 0x1) { 321 tmp = c_list[id] & 0xf; 322 c_list[id] = (value << 4) | tmp; 323 } else { 324 tmp = c_list[id] & 0xf0; 325 c_list[id] = (value & 0xf) | tmp; 326 } 327 } 328 329 /** 330 * srpt_get_class_port_info - copy ClassPortInfo to a management datagram 331 * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO. 332 * 333 * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture 334 * Specification. 335 */ 336 static void srpt_get_class_port_info(struct ib_dm_mad *mad) 337 { 338 struct ib_class_port_info *cif; 339 340 cif = (struct ib_class_port_info *)mad->data; 341 memset(cif, 0, sizeof(*cif)); 342 cif->base_version = 1; 343 cif->class_version = 1; 344 345 ib_set_cpi_resp_time(cif, 20); 346 mad->mad_hdr.status = 0; 347 } 348 349 /** 350 * srpt_get_iou - write IOUnitInfo to a management datagram 351 * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO. 352 * 353 * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture 354 * Specification. See also section B.7, table B.6 in the SRP r16a document. 355 */ 356 static void srpt_get_iou(struct ib_dm_mad *mad) 357 { 358 struct ib_dm_iou_info *ioui; 359 u8 slot; 360 int i; 361 362 ioui = (struct ib_dm_iou_info *)mad->data; 363 ioui->change_id = cpu_to_be16(1); 364 ioui->max_controllers = 16; 365 366 /* set present for slot 1 and empty for the rest */ 367 srpt_set_ioc(ioui->controller_list, 1, 1); 368 for (i = 1, slot = 2; i < 16; i++, slot++) 369 srpt_set_ioc(ioui->controller_list, slot, 0); 370 371 mad->mad_hdr.status = 0; 372 } 373 374 /** 375 * srpt_get_ioc - write IOControllerprofile to a management datagram 376 * @sport: HCA port through which the MAD has been received. 377 * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query. 378 * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE. 379 * 380 * See also section 16.3.3.4 IOControllerProfile in the InfiniBand 381 * Architecture Specification. See also section B.7, table B.7 in the SRP 382 * r16a document. 383 */ 384 static void srpt_get_ioc(struct srpt_port *sport, u32 slot, 385 struct ib_dm_mad *mad) 386 { 387 struct srpt_device *sdev = sport->sdev; 388 struct ib_dm_ioc_profile *iocp; 389 int send_queue_depth; 390 391 iocp = (struct ib_dm_ioc_profile *)mad->data; 392 393 if (!slot || slot > 16) { 394 mad->mad_hdr.status 395 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); 396 return; 397 } 398 399 if (slot > 2) { 400 mad->mad_hdr.status 401 = cpu_to_be16(DM_MAD_STATUS_NO_IOC); 402 return; 403 } 404 405 if (sdev->use_srq) 406 send_queue_depth = sdev->srq_size; 407 else 408 send_queue_depth = min(sdev->device->attrs.max_qp_wr, MAX_SRPT_RQ_SIZE); 409 410 memset(iocp, 0, sizeof(*iocp)); 411 strcpy(iocp->id_string, SRPT_ID_STRING); 412 iocp->guid = cpu_to_be64(srpt_service_guid); 413 iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); 414 iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id); 415 iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver); 416 iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); 417 iocp->subsys_device_id = 0x0; 418 iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS); 419 iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS); 420 iocp->protocol = cpu_to_be16(SRP_PROTOCOL); 421 iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION); 422 iocp->send_queue_depth = cpu_to_be16(send_queue_depth); 423 iocp->rdma_read_depth = 4; 424 iocp->send_size = cpu_to_be32(srp_max_req_size); 425 iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size, 426 1U << 24)); 427 iocp->num_svc_entries = 1; 428 iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC | 429 SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC; 430 431 mad->mad_hdr.status = 0; 432 } 433 434 /** 435 * srpt_get_svc_entries - write ServiceEntries to a management datagram 436 * @ioc_guid: I/O controller GUID to use in reply. 437 * @slot: I/O controller number. 438 * @hi: End of the range of service entries to be specified in the reply. 439 * @lo: Start of the range of service entries to be specified in the reply.. 440 * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES. 441 * 442 * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture 443 * Specification. See also section B.7, table B.8 in the SRP r16a document. 444 */ 445 static void srpt_get_svc_entries(u64 ioc_guid, 446 u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad) 447 { 448 struct ib_dm_svc_entries *svc_entries; 449 450 WARN_ON(!ioc_guid); 451 452 if (!slot || slot > 16) { 453 mad->mad_hdr.status 454 = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); 455 return; 456 } 457 458 if (slot > 2 || lo > hi || hi > 1) { 459 mad->mad_hdr.status 460 = cpu_to_be16(DM_MAD_STATUS_NO_IOC); 461 return; 462 } 463 464 svc_entries = (struct ib_dm_svc_entries *)mad->data; 465 memset(svc_entries, 0, sizeof(*svc_entries)); 466 svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid); 467 snprintf(svc_entries->service_entries[0].name, 468 sizeof(svc_entries->service_entries[0].name), 469 "%s%016llx", 470 SRP_SERVICE_NAME_PREFIX, 471 ioc_guid); 472 473 mad->mad_hdr.status = 0; 474 } 475 476 /** 477 * srpt_mgmt_method_get - process a received management datagram 478 * @sp: HCA port through which the MAD has been received. 479 * @rq_mad: received MAD. 480 * @rsp_mad: response MAD. 481 */ 482 static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad, 483 struct ib_dm_mad *rsp_mad) 484 { 485 u16 attr_id; 486 u32 slot; 487 u8 hi, lo; 488 489 attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id); 490 switch (attr_id) { 491 case DM_ATTR_CLASS_PORT_INFO: 492 srpt_get_class_port_info(rsp_mad); 493 break; 494 case DM_ATTR_IOU_INFO: 495 srpt_get_iou(rsp_mad); 496 break; 497 case DM_ATTR_IOC_PROFILE: 498 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); 499 srpt_get_ioc(sp, slot, rsp_mad); 500 break; 501 case DM_ATTR_SVC_ENTRIES: 502 slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); 503 hi = (u8) ((slot >> 8) & 0xff); 504 lo = (u8) (slot & 0xff); 505 slot = (u16) ((slot >> 16) & 0xffff); 506 srpt_get_svc_entries(srpt_service_guid, 507 slot, hi, lo, rsp_mad); 508 break; 509 default: 510 rsp_mad->mad_hdr.status = 511 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); 512 break; 513 } 514 } 515 516 /** 517 * srpt_mad_send_handler - MAD send completion callback 518 * @mad_agent: Return value of ib_register_mad_agent(). 519 * @mad_wc: Work completion reporting that the MAD has been sent. 520 */ 521 static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent, 522 struct ib_mad_send_wc *mad_wc) 523 { 524 rdma_destroy_ah(mad_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE); 525 ib_free_send_mad(mad_wc->send_buf); 526 } 527 528 /** 529 * srpt_mad_recv_handler - MAD reception callback function 530 * @mad_agent: Return value of ib_register_mad_agent(). 531 * @send_buf: Not used. 532 * @mad_wc: Work completion reporting that a MAD has been received. 533 */ 534 static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent, 535 struct ib_mad_send_buf *send_buf, 536 struct ib_mad_recv_wc *mad_wc) 537 { 538 struct srpt_port *sport = (struct srpt_port *)mad_agent->context; 539 struct ib_ah *ah; 540 struct ib_mad_send_buf *rsp; 541 struct ib_dm_mad *dm_mad; 542 543 if (!mad_wc || !mad_wc->recv_buf.mad) 544 return; 545 546 ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc, 547 mad_wc->recv_buf.grh, mad_agent->port_num); 548 if (IS_ERR(ah)) 549 goto err; 550 551 BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR); 552 553 rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp, 554 mad_wc->wc->pkey_index, 0, 555 IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA, 556 GFP_KERNEL, 557 IB_MGMT_BASE_VERSION); 558 if (IS_ERR(rsp)) 559 goto err_rsp; 560 561 rsp->ah = ah; 562 563 dm_mad = rsp->mad; 564 memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad)); 565 dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP; 566 dm_mad->mad_hdr.status = 0; 567 568 switch (mad_wc->recv_buf.mad->mad_hdr.method) { 569 case IB_MGMT_METHOD_GET: 570 srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad); 571 break; 572 case IB_MGMT_METHOD_SET: 573 dm_mad->mad_hdr.status = 574 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); 575 break; 576 default: 577 dm_mad->mad_hdr.status = 578 cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD); 579 break; 580 } 581 582 if (!ib_post_send_mad(rsp, NULL)) { 583 ib_free_recv_mad(mad_wc); 584 /* will destroy_ah & free_send_mad in send completion */ 585 return; 586 } 587 588 ib_free_send_mad(rsp); 589 590 err_rsp: 591 rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE); 592 err: 593 ib_free_recv_mad(mad_wc); 594 } 595 596 static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid) 597 { 598 const __be16 *g = (const __be16 *)guid; 599 600 return snprintf(buf, size, "%04x:%04x:%04x:%04x", 601 be16_to_cpu(g[0]), be16_to_cpu(g[1]), 602 be16_to_cpu(g[2]), be16_to_cpu(g[3])); 603 } 604 605 /** 606 * srpt_refresh_port - configure a HCA port 607 * @sport: SRPT HCA port. 608 * 609 * Enable InfiniBand management datagram processing, update the cached sm_lid, 610 * lid and gid values, and register a callback function for processing MADs 611 * on the specified port. 612 * 613 * Note: It is safe to call this function more than once for the same port. 614 */ 615 static int srpt_refresh_port(struct srpt_port *sport) 616 { 617 struct ib_mad_agent *mad_agent; 618 struct ib_mad_reg_req reg_req; 619 struct ib_port_modify port_modify; 620 struct ib_port_attr port_attr; 621 int ret; 622 623 ret = ib_query_port(sport->sdev->device, sport->port, &port_attr); 624 if (ret) 625 return ret; 626 627 sport->sm_lid = port_attr.sm_lid; 628 sport->lid = port_attr.lid; 629 630 ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid); 631 if (ret) 632 return ret; 633 634 srpt_format_guid(sport->guid_name, ARRAY_SIZE(sport->guid_name), 635 &sport->gid.global.interface_id); 636 snprintf(sport->gid_name, ARRAY_SIZE(sport->gid_name), 637 "0x%016llx%016llx", 638 be64_to_cpu(sport->gid.global.subnet_prefix), 639 be64_to_cpu(sport->gid.global.interface_id)); 640 641 if (rdma_protocol_iwarp(sport->sdev->device, sport->port)) 642 return 0; 643 644 memset(&port_modify, 0, sizeof(port_modify)); 645 port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; 646 port_modify.clr_port_cap_mask = 0; 647 648 ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify); 649 if (ret) { 650 pr_warn("%s-%d: enabling device management failed (%d). Note: this is expected if SR-IOV is enabled.\n", 651 dev_name(&sport->sdev->device->dev), sport->port, ret); 652 return 0; 653 } 654 655 if (!sport->mad_agent) { 656 memset(®_req, 0, sizeof(reg_req)); 657 reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT; 658 reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION; 659 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask); 660 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask); 661 662 mad_agent = ib_register_mad_agent(sport->sdev->device, 663 sport->port, 664 IB_QPT_GSI, 665 ®_req, 0, 666 srpt_mad_send_handler, 667 srpt_mad_recv_handler, 668 sport, 0); 669 if (IS_ERR(mad_agent)) { 670 pr_err("%s-%d: MAD agent registration failed (%pe). Note: this is expected if SR-IOV is enabled.\n", 671 dev_name(&sport->sdev->device->dev), sport->port, 672 mad_agent); 673 sport->mad_agent = NULL; 674 memset(&port_modify, 0, sizeof(port_modify)); 675 port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; 676 ib_modify_port(sport->sdev->device, sport->port, 0, 677 &port_modify); 678 return 0; 679 } 680 681 sport->mad_agent = mad_agent; 682 } 683 684 return 0; 685 } 686 687 /** 688 * srpt_unregister_mad_agent - unregister MAD callback functions 689 * @sdev: SRPT HCA pointer. 690 * @port_cnt: number of ports with registered MAD 691 * 692 * Note: It is safe to call this function more than once for the same device. 693 */ 694 static void srpt_unregister_mad_agent(struct srpt_device *sdev, int port_cnt) 695 { 696 struct ib_port_modify port_modify = { 697 .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP, 698 }; 699 struct srpt_port *sport; 700 int i; 701 702 for (i = 1; i <= port_cnt; i++) { 703 sport = &sdev->port[i - 1]; 704 WARN_ON(sport->port != i); 705 if (sport->mad_agent) { 706 ib_modify_port(sdev->device, i, 0, &port_modify); 707 ib_unregister_mad_agent(sport->mad_agent); 708 sport->mad_agent = NULL; 709 } 710 } 711 } 712 713 /** 714 * srpt_alloc_ioctx - allocate a SRPT I/O context structure 715 * @sdev: SRPT HCA pointer. 716 * @ioctx_size: I/O context size. 717 * @buf_cache: I/O buffer cache. 718 * @dir: DMA data direction. 719 */ 720 static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev, 721 int ioctx_size, 722 struct kmem_cache *buf_cache, 723 enum dma_data_direction dir) 724 { 725 struct srpt_ioctx *ioctx; 726 727 ioctx = kzalloc(ioctx_size, GFP_KERNEL); 728 if (!ioctx) 729 goto err; 730 731 ioctx->buf = kmem_cache_alloc(buf_cache, GFP_KERNEL); 732 if (!ioctx->buf) 733 goto err_free_ioctx; 734 735 ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, 736 kmem_cache_size(buf_cache), dir); 737 if (ib_dma_mapping_error(sdev->device, ioctx->dma)) 738 goto err_free_buf; 739 740 return ioctx; 741 742 err_free_buf: 743 kmem_cache_free(buf_cache, ioctx->buf); 744 err_free_ioctx: 745 kfree(ioctx); 746 err: 747 return NULL; 748 } 749 750 /** 751 * srpt_free_ioctx - free a SRPT I/O context structure 752 * @sdev: SRPT HCA pointer. 753 * @ioctx: I/O context pointer. 754 * @buf_cache: I/O buffer cache. 755 * @dir: DMA data direction. 756 */ 757 static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx, 758 struct kmem_cache *buf_cache, 759 enum dma_data_direction dir) 760 { 761 if (!ioctx) 762 return; 763 764 ib_dma_unmap_single(sdev->device, ioctx->dma, 765 kmem_cache_size(buf_cache), dir); 766 kmem_cache_free(buf_cache, ioctx->buf); 767 kfree(ioctx); 768 } 769 770 /** 771 * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures 772 * @sdev: Device to allocate the I/O context ring for. 773 * @ring_size: Number of elements in the I/O context ring. 774 * @ioctx_size: I/O context size. 775 * @buf_cache: I/O buffer cache. 776 * @alignment_offset: Offset in each ring buffer at which the SRP information 777 * unit starts. 778 * @dir: DMA data direction. 779 */ 780 static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev, 781 int ring_size, int ioctx_size, 782 struct kmem_cache *buf_cache, 783 int alignment_offset, 784 enum dma_data_direction dir) 785 { 786 struct srpt_ioctx **ring; 787 int i; 788 789 WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) && 790 ioctx_size != sizeof(struct srpt_send_ioctx)); 791 792 ring = kvmalloc_objs(ring[0], ring_size); 793 if (!ring) 794 goto out; 795 for (i = 0; i < ring_size; ++i) { 796 ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, buf_cache, dir); 797 if (!ring[i]) 798 goto err; 799 ring[i]->index = i; 800 ring[i]->offset = alignment_offset; 801 } 802 goto out; 803 804 err: 805 while (--i >= 0) 806 srpt_free_ioctx(sdev, ring[i], buf_cache, dir); 807 kvfree(ring); 808 ring = NULL; 809 out: 810 return ring; 811 } 812 813 /** 814 * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures 815 * @ioctx_ring: I/O context ring to be freed. 816 * @sdev: SRPT HCA pointer. 817 * @ring_size: Number of ring elements. 818 * @buf_cache: I/O buffer cache. 819 * @dir: DMA data direction. 820 */ 821 static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring, 822 struct srpt_device *sdev, int ring_size, 823 struct kmem_cache *buf_cache, 824 enum dma_data_direction dir) 825 { 826 int i; 827 828 if (!ioctx_ring) 829 return; 830 831 for (i = 0; i < ring_size; ++i) 832 srpt_free_ioctx(sdev, ioctx_ring[i], buf_cache, dir); 833 kvfree(ioctx_ring); 834 } 835 836 /** 837 * srpt_set_cmd_state - set the state of a SCSI command 838 * @ioctx: Send I/O context. 839 * @new: New I/O context state. 840 * 841 * Does not modify the state of aborted commands. Returns the previous command 842 * state. 843 */ 844 static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx, 845 enum srpt_command_state new) 846 { 847 enum srpt_command_state previous; 848 849 previous = ioctx->state; 850 if (previous != SRPT_STATE_DONE) 851 ioctx->state = new; 852 853 return previous; 854 } 855 856 /** 857 * srpt_test_and_set_cmd_state - test and set the state of a command 858 * @ioctx: Send I/O context. 859 * @old: Current I/O context state. 860 * @new: New I/O context state. 861 * 862 * Returns true if and only if the previous command state was equal to 'old'. 863 */ 864 static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx, 865 enum srpt_command_state old, 866 enum srpt_command_state new) 867 { 868 enum srpt_command_state previous; 869 870 WARN_ON(!ioctx); 871 WARN_ON(old == SRPT_STATE_DONE); 872 WARN_ON(new == SRPT_STATE_NEW); 873 874 previous = ioctx->state; 875 if (previous == old) 876 ioctx->state = new; 877 878 return previous == old; 879 } 880 881 /** 882 * srpt_post_recv - post an IB receive request 883 * @sdev: SRPT HCA pointer. 884 * @ch: SRPT RDMA channel. 885 * @ioctx: Receive I/O context pointer. 886 */ 887 static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch, 888 struct srpt_recv_ioctx *ioctx) 889 { 890 struct ib_sge list; 891 struct ib_recv_wr wr; 892 893 BUG_ON(!sdev); 894 list.addr = ioctx->ioctx.dma + ioctx->ioctx.offset; 895 list.length = srp_max_req_size; 896 list.lkey = sdev->lkey; 897 898 ioctx->ioctx.cqe.done = srpt_recv_done; 899 wr.wr_cqe = &ioctx->ioctx.cqe; 900 wr.next = NULL; 901 wr.sg_list = &list; 902 wr.num_sge = 1; 903 904 if (sdev->use_srq) 905 return ib_post_srq_recv(sdev->srq, &wr, NULL); 906 else 907 return ib_post_recv(ch->qp, &wr, NULL); 908 } 909 910 /** 911 * srpt_zerolength_write - perform a zero-length RDMA write 912 * @ch: SRPT RDMA channel. 913 * 914 * A quote from the InfiniBand specification: C9-88: For an HCA responder 915 * using Reliable Connection service, for each zero-length RDMA READ or WRITE 916 * request, the R_Key shall not be validated, even if the request includes 917 * Immediate data. 918 */ 919 static int srpt_zerolength_write(struct srpt_rdma_ch *ch) 920 { 921 struct ib_rdma_wr wr = { 922 .wr = { 923 .next = NULL, 924 { .wr_cqe = &ch->zw_cqe, }, 925 .opcode = IB_WR_RDMA_WRITE, 926 .send_flags = IB_SEND_SIGNALED, 927 } 928 }; 929 930 pr_debug("%s-%d: queued zerolength write\n", ch->sess_name, 931 ch->qp->qp_num); 932 933 return ib_post_send(ch->qp, &wr.wr, NULL); 934 } 935 936 static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc) 937 { 938 struct srpt_rdma_ch *ch = wc->qp->qp_context; 939 940 pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num, 941 wc->status); 942 943 if (wc->status == IB_WC_SUCCESS) { 944 srpt_process_wait_list(ch); 945 } else { 946 if (srpt_set_ch_state(ch, CH_DISCONNECTED)) 947 schedule_work(&ch->release_work); 948 else 949 pr_debug("%s-%d: already disconnected.\n", 950 ch->sess_name, ch->qp->qp_num); 951 } 952 } 953 954 static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx, 955 struct srp_direct_buf *db, int nbufs, struct scatterlist **sg, 956 unsigned *sg_cnt) 957 { 958 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd); 959 struct srpt_rdma_ch *ch = ioctx->ch; 960 struct scatterlist *prev = NULL; 961 unsigned prev_nents; 962 u8 n_rdma, n_rw_ctx; 963 int ret, i; 964 965 if (nbufs == 1) { 966 ioctx->rw_ctxs = &ioctx->s_rw_ctx; 967 } else { 968 ioctx->rw_ctxs = kmalloc_objs(*ioctx->rw_ctxs, nbufs); 969 if (!ioctx->rw_ctxs) 970 return -ENOMEM; 971 } 972 973 n_rw_ctx = ioctx->n_rw_ctx; 974 n_rdma = ioctx->n_rdma; 975 976 for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) { 977 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 978 u64 remote_addr = be64_to_cpu(db->va); 979 u32 size = be32_to_cpu(db->len); 980 u32 rkey = be32_to_cpu(db->key); 981 982 ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false, 983 i < nbufs - 1); 984 if (ret) 985 goto unwind; 986 987 ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port, 988 ctx->sg, ctx->nents, 0, remote_addr, rkey, dir); 989 if (ret < 0) { 990 target_free_sgl(ctx->sg, ctx->nents); 991 goto unwind; 992 } 993 994 ioctx->n_rdma += ret; 995 ioctx->n_rw_ctx++; 996 997 if (prev) { 998 sg_unmark_end(&prev[prev_nents - 1]); 999 sg_chain(prev, prev_nents + 1, ctx->sg); 1000 } else { 1001 *sg = ctx->sg; 1002 } 1003 1004 prev = ctx->sg; 1005 prev_nents = ctx->nents; 1006 1007 *sg_cnt += ctx->nents; 1008 } 1009 1010 return 0; 1011 1012 unwind: 1013 while (--i >= 0) { 1014 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 1015 1016 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port, 1017 ctx->sg, ctx->nents, dir); 1018 target_free_sgl(ctx->sg, ctx->nents); 1019 } 1020 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx) 1021 kfree(ioctx->rw_ctxs); 1022 ioctx->rw_ctxs = NULL; 1023 ioctx->n_rw_ctx = n_rw_ctx; 1024 ioctx->n_rdma = n_rdma; 1025 return ret; 1026 } 1027 1028 static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch, 1029 struct srpt_send_ioctx *ioctx) 1030 { 1031 enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd); 1032 int i; 1033 1034 for (i = 0; i < ioctx->n_rw_ctx; i++) { 1035 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 1036 1037 rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port, 1038 ctx->sg, ctx->nents, dir); 1039 target_free_sgl(ctx->sg, ctx->nents); 1040 } 1041 1042 if (ioctx->rw_ctxs != &ioctx->s_rw_ctx) 1043 kfree(ioctx->rw_ctxs); 1044 } 1045 1046 static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd) 1047 { 1048 /* 1049 * The pointer computations below will only be compiled correctly 1050 * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check 1051 * whether srp_cmd::add_data has been declared as a byte pointer. 1052 */ 1053 BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) && 1054 !__same_type(srp_cmd->add_data[0], (u8)0)); 1055 1056 /* 1057 * According to the SRP spec, the lower two bits of the 'ADDITIONAL 1058 * CDB LENGTH' field are reserved and the size in bytes of this field 1059 * is four times the value specified in bits 3..7. Hence the "& ~3". 1060 */ 1061 return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3); 1062 } 1063 1064 /** 1065 * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request 1066 * @recv_ioctx: I/O context associated with the received command @srp_cmd. 1067 * @ioctx: I/O context that will be used for responding to the initiator. 1068 * @srp_cmd: Pointer to the SRP_CMD request data. 1069 * @dir: Pointer to the variable to which the transfer direction will be 1070 * written. 1071 * @sg: [out] scatterlist for the parsed SRP_CMD. 1072 * @sg_cnt: [out] length of @sg. 1073 * @data_len: Pointer to the variable to which the total data length of all 1074 * descriptors in the SRP_CMD request will be written. 1075 * @imm_data_offset: [in] Offset in SRP_CMD requests at which immediate data 1076 * starts. 1077 * 1078 * This function initializes ioctx->nrbuf and ioctx->r_bufs. 1079 * 1080 * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors; 1081 * -ENOMEM when memory allocation fails and zero upon success. 1082 */ 1083 static int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx, 1084 struct srpt_send_ioctx *ioctx, 1085 struct srp_cmd *srp_cmd, enum dma_data_direction *dir, 1086 struct scatterlist **sg, unsigned int *sg_cnt, u64 *data_len, 1087 u16 imm_data_offset) 1088 { 1089 BUG_ON(!dir); 1090 BUG_ON(!data_len); 1091 1092 /* 1093 * The lower four bits of the buffer format field contain the DATA-IN 1094 * buffer descriptor format, and the highest four bits contain the 1095 * DATA-OUT buffer descriptor format. 1096 */ 1097 if (srp_cmd->buf_fmt & 0xf) 1098 /* DATA-IN: transfer data from target to initiator (read). */ 1099 *dir = DMA_FROM_DEVICE; 1100 else if (srp_cmd->buf_fmt >> 4) 1101 /* DATA-OUT: transfer data from initiator to target (write). */ 1102 *dir = DMA_TO_DEVICE; 1103 else 1104 *dir = DMA_NONE; 1105 1106 /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */ 1107 ioctx->cmd.data_direction = *dir; 1108 1109 if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) || 1110 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) { 1111 struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd); 1112 1113 *data_len = be32_to_cpu(db->len); 1114 return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt); 1115 } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) || 1116 ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) { 1117 struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd); 1118 int nbufs = be32_to_cpu(idb->table_desc.len) / 1119 sizeof(struct srp_direct_buf); 1120 1121 if (nbufs > 1122 (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) { 1123 pr_err("received unsupported SRP_CMD request type (%u out + %u in != %u / %zu)\n", 1124 srp_cmd->data_out_desc_cnt, 1125 srp_cmd->data_in_desc_cnt, 1126 be32_to_cpu(idb->table_desc.len), 1127 sizeof(struct srp_direct_buf)); 1128 return -EINVAL; 1129 } 1130 1131 *data_len = be32_to_cpu(idb->len); 1132 return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs, 1133 sg, sg_cnt); 1134 } else if ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_IMM) { 1135 struct srp_imm_buf *imm_buf = srpt_get_desc_buf(srp_cmd); 1136 void *data = (void *)srp_cmd + imm_data_offset; 1137 uint32_t len = be32_to_cpu(imm_buf->len); 1138 uint32_t req_size; 1139 1140 if (check_add_overflow((uint32_t)imm_data_offset, len, &req_size) || 1141 req_size > srp_max_req_size) { 1142 pr_err("Immediate data (length %d + %d) exceeds request size %d\n", 1143 imm_data_offset, len, srp_max_req_size); 1144 return -EINVAL; 1145 } 1146 if (recv_ioctx->byte_len < req_size) { 1147 pr_err("Received too few data - %d < %d\n", 1148 recv_ioctx->byte_len, req_size); 1149 return -EIO; 1150 } 1151 /* 1152 * The immediate data buffer descriptor must occur before the 1153 * immediate data itself. 1154 */ 1155 if ((void *)(imm_buf + 1) > (void *)data) { 1156 pr_err("Received invalid write request\n"); 1157 return -EINVAL; 1158 } 1159 *data_len = len; 1160 ioctx->recv_ioctx = recv_ioctx; 1161 if ((uintptr_t)data & 511) { 1162 pr_warn_once("Internal error - the receive buffers are not aligned properly.\n"); 1163 return -EINVAL; 1164 } 1165 sg_init_one(&ioctx->imm_sg, data, len); 1166 *sg = &ioctx->imm_sg; 1167 *sg_cnt = 1; 1168 return 0; 1169 } else { 1170 *data_len = 0; 1171 return 0; 1172 } 1173 } 1174 1175 /** 1176 * srpt_init_ch_qp - initialize queue pair attributes 1177 * @ch: SRPT RDMA channel. 1178 * @qp: Queue pair pointer. 1179 * 1180 * Initialized the attributes of queue pair 'qp' by allowing local write, 1181 * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT. 1182 */ 1183 static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp) 1184 { 1185 struct ib_qp_attr *attr; 1186 int ret; 1187 1188 WARN_ON_ONCE(ch->using_rdma_cm); 1189 1190 attr = kzalloc_obj(*attr); 1191 if (!attr) 1192 return -ENOMEM; 1193 1194 attr->qp_state = IB_QPS_INIT; 1195 attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE; 1196 attr->port_num = ch->sport->port; 1197 1198 ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port, 1199 ch->pkey, &attr->pkey_index); 1200 if (ret < 0) 1201 pr_err("Translating pkey %#x failed (%d) - using index 0\n", 1202 ch->pkey, ret); 1203 1204 ret = ib_modify_qp(qp, attr, 1205 IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT | 1206 IB_QP_PKEY_INDEX); 1207 1208 kfree(attr); 1209 return ret; 1210 } 1211 1212 /** 1213 * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR) 1214 * @ch: channel of the queue pair. 1215 * @qp: queue pair to change the state of. 1216 * 1217 * Returns zero upon success and a negative value upon failure. 1218 * 1219 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. 1220 * If this structure ever becomes larger, it might be necessary to allocate 1221 * it dynamically instead of on the stack. 1222 */ 1223 static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp) 1224 { 1225 struct ib_qp_attr qp_attr; 1226 int attr_mask; 1227 int ret; 1228 1229 WARN_ON_ONCE(ch->using_rdma_cm); 1230 1231 qp_attr.qp_state = IB_QPS_RTR; 1232 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask); 1233 if (ret) 1234 goto out; 1235 1236 qp_attr.max_dest_rd_atomic = 4; 1237 1238 ret = ib_modify_qp(qp, &qp_attr, attr_mask); 1239 1240 out: 1241 return ret; 1242 } 1243 1244 /** 1245 * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS) 1246 * @ch: channel of the queue pair. 1247 * @qp: queue pair to change the state of. 1248 * 1249 * Returns zero upon success and a negative value upon failure. 1250 * 1251 * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. 1252 * If this structure ever becomes larger, it might be necessary to allocate 1253 * it dynamically instead of on the stack. 1254 */ 1255 static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp) 1256 { 1257 struct ib_qp_attr qp_attr; 1258 int attr_mask; 1259 int ret; 1260 1261 qp_attr.qp_state = IB_QPS_RTS; 1262 ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask); 1263 if (ret) 1264 goto out; 1265 1266 qp_attr.max_rd_atomic = 4; 1267 1268 ret = ib_modify_qp(qp, &qp_attr, attr_mask); 1269 1270 out: 1271 return ret; 1272 } 1273 1274 /** 1275 * srpt_ch_qp_err - set the channel queue pair state to 'error' 1276 * @ch: SRPT RDMA channel. 1277 */ 1278 static int srpt_ch_qp_err(struct srpt_rdma_ch *ch) 1279 { 1280 struct ib_qp_attr qp_attr; 1281 1282 qp_attr.qp_state = IB_QPS_ERR; 1283 return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE); 1284 } 1285 1286 /** 1287 * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator 1288 * @ch: SRPT RDMA channel. 1289 */ 1290 static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch) 1291 { 1292 struct srpt_send_ioctx *ioctx; 1293 int tag, cpu; 1294 1295 BUG_ON(!ch); 1296 1297 tag = sbitmap_queue_get(&ch->sess->sess_tag_pool, &cpu); 1298 if (tag < 0) 1299 return NULL; 1300 1301 ioctx = ch->ioctx_ring[tag]; 1302 BUG_ON(ioctx->ch != ch); 1303 ioctx->state = SRPT_STATE_NEW; 1304 WARN_ON_ONCE(ioctx->recv_ioctx); 1305 ioctx->n_rdma = 0; 1306 ioctx->n_rw_ctx = 0; 1307 ioctx->queue_status_only = false; 1308 /* 1309 * transport_init_se_cmd() does not initialize all fields, so do it 1310 * here. 1311 */ 1312 memset(&ioctx->cmd, 0, sizeof(ioctx->cmd)); 1313 memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data)); 1314 ioctx->cmd.map_tag = tag; 1315 ioctx->cmd.map_cpu = cpu; 1316 1317 return ioctx; 1318 } 1319 1320 /** 1321 * srpt_abort_cmd - abort a SCSI command 1322 * @ioctx: I/O context associated with the SCSI command. 1323 */ 1324 static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx) 1325 { 1326 enum srpt_command_state state; 1327 1328 BUG_ON(!ioctx); 1329 1330 /* 1331 * If the command is in a state where the target core is waiting for 1332 * the ib_srpt driver, change the state to the next state. 1333 */ 1334 1335 state = ioctx->state; 1336 switch (state) { 1337 case SRPT_STATE_NEED_DATA: 1338 ioctx->state = SRPT_STATE_DATA_IN; 1339 break; 1340 case SRPT_STATE_CMD_RSP_SENT: 1341 case SRPT_STATE_MGMT_RSP_SENT: 1342 ioctx->state = SRPT_STATE_DONE; 1343 break; 1344 default: 1345 WARN_ONCE(true, "%s: unexpected I/O context state %d\n", 1346 __func__, state); 1347 break; 1348 } 1349 1350 pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state, 1351 ioctx->state, ioctx->cmd.tag); 1352 1353 switch (state) { 1354 case SRPT_STATE_NEW: 1355 case SRPT_STATE_DATA_IN: 1356 case SRPT_STATE_MGMT: 1357 case SRPT_STATE_DONE: 1358 /* 1359 * Do nothing - defer abort processing until 1360 * srpt_queue_response() is invoked. 1361 */ 1362 break; 1363 case SRPT_STATE_NEED_DATA: 1364 pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag); 1365 transport_generic_request_failure(&ioctx->cmd, 1366 TCM_CHECK_CONDITION_ABORT_CMD); 1367 break; 1368 case SRPT_STATE_CMD_RSP_SENT: 1369 /* 1370 * SRP_RSP sending failed or the SRP_RSP send completion has 1371 * not been received in time. 1372 */ 1373 transport_generic_free_cmd(&ioctx->cmd, 0); 1374 break; 1375 case SRPT_STATE_MGMT_RSP_SENT: 1376 transport_generic_free_cmd(&ioctx->cmd, 0); 1377 break; 1378 default: 1379 WARN(1, "Unexpected command state (%d)", state); 1380 break; 1381 } 1382 1383 return state; 1384 } 1385 1386 /** 1387 * srpt_rdma_read_done - RDMA read completion callback 1388 * @cq: Completion queue. 1389 * @wc: Work completion. 1390 * 1391 * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping 1392 * the data that has been transferred via IB RDMA had to be postponed until the 1393 * check_stop_free() callback. None of this is necessary anymore and needs to 1394 * be cleaned up. 1395 */ 1396 static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc) 1397 { 1398 struct srpt_rdma_ch *ch = wc->qp->qp_context; 1399 struct srpt_send_ioctx *ioctx = 1400 container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); 1401 1402 WARN_ON(ioctx->n_rdma <= 0); 1403 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); 1404 ioctx->n_rdma = 0; 1405 1406 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1407 pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n", 1408 ioctx, wc->status); 1409 srpt_abort_cmd(ioctx); 1410 return; 1411 } 1412 1413 if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA, 1414 SRPT_STATE_DATA_IN)) 1415 target_execute_cmd(&ioctx->cmd); 1416 else 1417 pr_err("%s[%d]: wrong state = %d\n", __func__, 1418 __LINE__, ioctx->state); 1419 } 1420 1421 /** 1422 * srpt_build_cmd_rsp - build a 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 se_cmd *cmd = &ioctx->cmd; 1441 struct srp_rsp *srp_rsp; 1442 const u8 *sense_data; 1443 int sense_data_len, max_sense_len; 1444 u32 resid = cmd->residual_count; 1445 1446 /* 1447 * The lowest bit of all SAM-3 status codes is zero (see also 1448 * paragraph 5.3 in SAM-3). 1449 */ 1450 WARN_ON(status & 1); 1451 1452 srp_rsp = ioctx->ioctx.buf; 1453 BUG_ON(!srp_rsp); 1454 1455 sense_data = ioctx->sense_data; 1456 sense_data_len = ioctx->cmd.scsi_sense_length; 1457 WARN_ON(sense_data_len > sizeof(ioctx->sense_data)); 1458 1459 memset(srp_rsp, 0, sizeof(*srp_rsp)); 1460 srp_rsp->opcode = SRP_RSP; 1461 srp_rsp->req_lim_delta = 1462 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); 1463 srp_rsp->tag = tag; 1464 srp_rsp->status = status; 1465 1466 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { 1467 if (cmd->data_direction == DMA_TO_DEVICE) { 1468 /* residual data from an underflow write */ 1469 srp_rsp->flags = SRP_RSP_FLAG_DOUNDER; 1470 srp_rsp->data_out_res_cnt = cpu_to_be32(resid); 1471 } else if (cmd->data_direction == DMA_FROM_DEVICE) { 1472 /* residual data from an underflow read */ 1473 srp_rsp->flags = SRP_RSP_FLAG_DIUNDER; 1474 srp_rsp->data_in_res_cnt = cpu_to_be32(resid); 1475 } 1476 } else if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { 1477 if (cmd->data_direction == DMA_TO_DEVICE) { 1478 /* residual data from an overflow write */ 1479 srp_rsp->flags = SRP_RSP_FLAG_DOOVER; 1480 srp_rsp->data_out_res_cnt = cpu_to_be32(resid); 1481 } else if (cmd->data_direction == DMA_FROM_DEVICE) { 1482 /* residual data from an overflow read */ 1483 srp_rsp->flags = SRP_RSP_FLAG_DIOVER; 1484 srp_rsp->data_in_res_cnt = cpu_to_be32(resid); 1485 } 1486 } 1487 1488 if (sense_data_len) { 1489 BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp)); 1490 max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp); 1491 if (sense_data_len > max_sense_len) { 1492 pr_warn("truncated sense data from %d to %d bytes\n", 1493 sense_data_len, max_sense_len); 1494 sense_data_len = max_sense_len; 1495 } 1496 1497 srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID; 1498 srp_rsp->sense_data_len = cpu_to_be32(sense_data_len); 1499 memcpy(srp_rsp->data, sense_data, sense_data_len); 1500 } 1501 1502 return sizeof(*srp_rsp) + sense_data_len; 1503 } 1504 1505 /** 1506 * srpt_build_tskmgmt_rsp - build a task management response 1507 * @ch: RDMA channel through which the request has been received. 1508 * @ioctx: I/O context in which the SRP_RSP response will be built. 1509 * @rsp_code: RSP_CODE that will be stored in the response. 1510 * @tag: Tag of the request for which this response is being generated. 1511 * 1512 * Returns the size in bytes of the SRP_RSP response. 1513 * 1514 * An SRP_RSP response contains a SCSI status or service response. See also 1515 * section 6.9 in the SRP r16a document for the format of an SRP_RSP 1516 * response. 1517 */ 1518 static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch, 1519 struct srpt_send_ioctx *ioctx, 1520 u8 rsp_code, u64 tag) 1521 { 1522 struct srp_rsp *srp_rsp; 1523 int resp_data_len; 1524 int resp_len; 1525 1526 resp_data_len = 4; 1527 resp_len = sizeof(*srp_rsp) + resp_data_len; 1528 1529 srp_rsp = ioctx->ioctx.buf; 1530 BUG_ON(!srp_rsp); 1531 memset(srp_rsp, 0, sizeof(*srp_rsp)); 1532 1533 srp_rsp->opcode = SRP_RSP; 1534 srp_rsp->req_lim_delta = 1535 cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); 1536 srp_rsp->tag = tag; 1537 1538 srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID; 1539 srp_rsp->resp_data_len = cpu_to_be32(resp_data_len); 1540 srp_rsp->data[3] = rsp_code; 1541 1542 return resp_len; 1543 } 1544 1545 static int srpt_check_stop_free(struct se_cmd *cmd) 1546 { 1547 struct srpt_send_ioctx *ioctx = container_of(cmd, 1548 struct srpt_send_ioctx, cmd); 1549 1550 return target_put_sess_cmd(&ioctx->cmd); 1551 } 1552 1553 /** 1554 * srpt_handle_cmd - process a SRP_CMD information unit 1555 * @ch: SRPT RDMA channel. 1556 * @recv_ioctx: Receive I/O context. 1557 * @send_ioctx: Send I/O context. 1558 */ 1559 static void srpt_handle_cmd(struct srpt_rdma_ch *ch, 1560 struct srpt_recv_ioctx *recv_ioctx, 1561 struct srpt_send_ioctx *send_ioctx) 1562 { 1563 struct se_cmd *cmd; 1564 struct srp_cmd *srp_cmd; 1565 struct scatterlist *sg = NULL; 1566 unsigned sg_cnt = 0; 1567 u64 data_len; 1568 enum dma_data_direction dir; 1569 int rc; 1570 1571 BUG_ON(!send_ioctx); 1572 1573 srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; 1574 cmd = &send_ioctx->cmd; 1575 cmd->tag = srp_cmd->tag; 1576 1577 switch (srp_cmd->task_attr) { 1578 case SRP_CMD_SIMPLE_Q: 1579 cmd->sam_task_attr = TCM_SIMPLE_TAG; 1580 break; 1581 case SRP_CMD_ORDERED_Q: 1582 default: 1583 cmd->sam_task_attr = TCM_ORDERED_TAG; 1584 break; 1585 case SRP_CMD_HEAD_OF_Q: 1586 cmd->sam_task_attr = TCM_HEAD_TAG; 1587 break; 1588 case SRP_CMD_ACA: 1589 cmd->sam_task_attr = TCM_ACA_TAG; 1590 break; 1591 } 1592 1593 rc = srpt_get_desc_tbl(recv_ioctx, send_ioctx, srp_cmd, &dir, 1594 &sg, &sg_cnt, &data_len, ch->imm_data_offset); 1595 if (rc) { 1596 if (rc != -EAGAIN) { 1597 pr_err("0x%llx: parsing SRP descriptor table failed.\n", 1598 srp_cmd->tag); 1599 } 1600 goto busy; 1601 } 1602 1603 rc = target_init_cmd(cmd, ch->sess, &send_ioctx->sense_data[0], 1604 scsilun_to_int(&srp_cmd->lun), data_len, 1605 cmd->sam_task_attr, dir, TARGET_SCF_ACK_KREF); 1606 if (rc != 0) { 1607 pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc, 1608 srp_cmd->tag); 1609 goto busy; 1610 } 1611 1612 if (target_submit_prep(cmd, srp_cmd->cdb, sg, sg_cnt, NULL, 0, NULL, 0, 1613 GFP_KERNEL)) 1614 return; 1615 1616 target_submit(cmd); 1617 return; 1618 1619 busy: 1620 target_send_busy(cmd); 1621 } 1622 1623 static int srp_tmr_to_tcm(int fn) 1624 { 1625 switch (fn) { 1626 case SRP_TSK_ABORT_TASK: 1627 return TMR_ABORT_TASK; 1628 case SRP_TSK_ABORT_TASK_SET: 1629 return TMR_ABORT_TASK_SET; 1630 case SRP_TSK_CLEAR_TASK_SET: 1631 return TMR_CLEAR_TASK_SET; 1632 case SRP_TSK_LUN_RESET: 1633 return TMR_LUN_RESET; 1634 case SRP_TSK_CLEAR_ACA: 1635 return TMR_CLEAR_ACA; 1636 default: 1637 return -1; 1638 } 1639 } 1640 1641 /** 1642 * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit 1643 * @ch: SRPT RDMA channel. 1644 * @recv_ioctx: Receive I/O context. 1645 * @send_ioctx: Send I/O context. 1646 * 1647 * Returns 0 if and only if the request will be processed by the target core. 1648 * 1649 * For more information about SRP_TSK_MGMT information units, see also section 1650 * 6.7 in the SRP r16a document. 1651 */ 1652 static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch, 1653 struct srpt_recv_ioctx *recv_ioctx, 1654 struct srpt_send_ioctx *send_ioctx) 1655 { 1656 struct srp_tsk_mgmt *srp_tsk; 1657 struct se_cmd *cmd; 1658 struct se_session *sess = ch->sess; 1659 int tcm_tmr; 1660 int rc; 1661 1662 BUG_ON(!send_ioctx); 1663 1664 srp_tsk = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; 1665 cmd = &send_ioctx->cmd; 1666 1667 pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n", 1668 srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch, 1669 ch->sess); 1670 1671 srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT); 1672 send_ioctx->cmd.tag = srp_tsk->tag; 1673 tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func); 1674 rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, 1675 scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr, 1676 GFP_KERNEL, srp_tsk->task_tag, 1677 TARGET_SCF_ACK_KREF); 1678 if (rc != 0) { 1679 send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED; 1680 cmd->se_tfo->queue_tm_rsp(cmd); 1681 } 1682 return; 1683 } 1684 1685 /** 1686 * srpt_handle_new_iu - process a newly received information unit 1687 * @ch: RDMA channel through which the information unit has been received. 1688 * @recv_ioctx: Receive I/O context associated with the information unit. 1689 */ 1690 static bool 1691 srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx) 1692 { 1693 struct srpt_send_ioctx *send_ioctx = NULL; 1694 struct srp_cmd *srp_cmd; 1695 bool res = false; 1696 u8 opcode; 1697 1698 BUG_ON(!ch); 1699 BUG_ON(!recv_ioctx); 1700 1701 if (unlikely(ch->state == CH_CONNECTING)) 1702 goto push; 1703 1704 ib_dma_sync_single_for_cpu(ch->sport->sdev->device, 1705 recv_ioctx->ioctx.dma, 1706 recv_ioctx->ioctx.offset + srp_max_req_size, 1707 DMA_FROM_DEVICE); 1708 1709 srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; 1710 opcode = srp_cmd->opcode; 1711 if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) { 1712 send_ioctx = srpt_get_send_ioctx(ch); 1713 if (unlikely(!send_ioctx)) 1714 goto push; 1715 } 1716 1717 if (!list_empty(&recv_ioctx->wait_list)) { 1718 WARN_ON_ONCE(!ch->processing_wait_list); 1719 list_del_init(&recv_ioctx->wait_list); 1720 } 1721 1722 switch (opcode) { 1723 case SRP_CMD: 1724 srpt_handle_cmd(ch, recv_ioctx, send_ioctx); 1725 break; 1726 case SRP_TSK_MGMT: 1727 srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx); 1728 break; 1729 case SRP_I_LOGOUT: 1730 pr_err("Not yet implemented: SRP_I_LOGOUT\n"); 1731 break; 1732 case SRP_CRED_RSP: 1733 pr_debug("received SRP_CRED_RSP\n"); 1734 break; 1735 case SRP_AER_RSP: 1736 pr_debug("received SRP_AER_RSP\n"); 1737 break; 1738 case SRP_RSP: 1739 pr_err("Received SRP_RSP\n"); 1740 break; 1741 default: 1742 pr_err("received IU with unknown opcode 0x%x\n", opcode); 1743 break; 1744 } 1745 1746 if (!send_ioctx || !send_ioctx->recv_ioctx) 1747 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx); 1748 res = true; 1749 1750 out: 1751 return res; 1752 1753 push: 1754 if (list_empty(&recv_ioctx->wait_list)) { 1755 WARN_ON_ONCE(ch->processing_wait_list); 1756 list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list); 1757 } 1758 goto out; 1759 } 1760 1761 static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1762 { 1763 struct srpt_rdma_ch *ch = wc->qp->qp_context; 1764 struct srpt_recv_ioctx *ioctx = 1765 container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe); 1766 1767 if (wc->status == IB_WC_SUCCESS) { 1768 int req_lim; 1769 1770 req_lim = atomic_dec_return(&ch->req_lim); 1771 if (unlikely(req_lim < 0)) 1772 pr_err("req_lim = %d < 0\n", req_lim); 1773 ioctx->byte_len = wc->byte_len; 1774 srpt_handle_new_iu(ch, ioctx); 1775 } else { 1776 pr_info_ratelimited("receiving failed for ioctx %p with status %d\n", 1777 ioctx, wc->status); 1778 } 1779 } 1780 1781 /* 1782 * This function must be called from the context in which RDMA completions are 1783 * processed because it accesses the wait list without protection against 1784 * access from other threads. 1785 */ 1786 static void srpt_process_wait_list(struct srpt_rdma_ch *ch) 1787 { 1788 struct srpt_recv_ioctx *recv_ioctx, *tmp; 1789 1790 WARN_ON_ONCE(ch->state == CH_CONNECTING); 1791 1792 if (list_empty(&ch->cmd_wait_list)) 1793 return; 1794 1795 WARN_ON_ONCE(ch->processing_wait_list); 1796 ch->processing_wait_list = true; 1797 list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list, 1798 wait_list) { 1799 if (!srpt_handle_new_iu(ch, recv_ioctx)) 1800 break; 1801 } 1802 ch->processing_wait_list = false; 1803 } 1804 1805 /** 1806 * srpt_send_done - send completion callback 1807 * @cq: Completion queue. 1808 * @wc: Work completion. 1809 * 1810 * Note: Although this has not yet been observed during tests, at least in 1811 * theory it is possible that the srpt_get_send_ioctx() call invoked by 1812 * srpt_handle_new_iu() fails. This is possible because the req_lim_delta 1813 * value in each response is set to one, and it is possible that this response 1814 * makes the initiator send a new request before the send completion for that 1815 * response has been processed. This could e.g. happen if the call to 1816 * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or 1817 * if IB retransmission causes generation of the send completion to be 1818 * delayed. Incoming information units for which srpt_get_send_ioctx() fails 1819 * are queued on cmd_wait_list. The code below processes these delayed 1820 * requests one at a time. 1821 */ 1822 static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc) 1823 { 1824 struct srpt_rdma_ch *ch = wc->qp->qp_context; 1825 struct srpt_send_ioctx *ioctx = 1826 container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe); 1827 enum srpt_command_state state; 1828 1829 state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); 1830 1831 WARN_ON(state != SRPT_STATE_CMD_RSP_SENT && 1832 state != SRPT_STATE_MGMT_RSP_SENT); 1833 1834 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail); 1835 1836 if (wc->status != IB_WC_SUCCESS) 1837 pr_info("sending response for ioctx 0x%p failed with status %d\n", 1838 ioctx, wc->status); 1839 1840 if (state != SRPT_STATE_DONE) { 1841 transport_generic_free_cmd(&ioctx->cmd, 0); 1842 } else { 1843 pr_err("IB completion has been received too late for wr_id = %u.\n", 1844 ioctx->ioctx.index); 1845 } 1846 1847 srpt_process_wait_list(ch); 1848 } 1849 1850 /** 1851 * srpt_create_ch_ib - create receive and send completion queues 1852 * @ch: SRPT RDMA channel. 1853 */ 1854 static int srpt_create_ch_ib(struct srpt_rdma_ch *ch) 1855 { 1856 struct ib_qp_init_attr *qp_init; 1857 struct srpt_port *sport = ch->sport; 1858 struct srpt_device *sdev = sport->sdev; 1859 const struct ib_device_attr *attrs = &sdev->device->attrs; 1860 u32 sq_size = sport->port_attrib.srp_sq_size; 1861 int i, ret; 1862 1863 WARN_ON(ch->rq_size < 1); 1864 1865 ret = -ENOMEM; 1866 qp_init = kzalloc_obj(*qp_init); 1867 if (!qp_init) 1868 goto out; 1869 1870 retry: 1871 ch->cq = ib_cq_pool_get(sdev->device, ch->rq_size + sq_size, -1, 1872 IB_POLL_WORKQUEUE); 1873 if (IS_ERR(ch->cq)) { 1874 ret = PTR_ERR(ch->cq); 1875 pr_err("failed to create CQ cqe= %d ret= %pe\n", 1876 ch->rq_size + sq_size, ch->cq); 1877 goto out; 1878 } 1879 ch->cq_size = ch->rq_size + sq_size; 1880 1881 qp_init->qp_context = (void *)ch; 1882 qp_init->event_handler = srpt_qp_event; 1883 qp_init->send_cq = ch->cq; 1884 qp_init->recv_cq = ch->cq; 1885 qp_init->sq_sig_type = IB_SIGNAL_REQ_WR; 1886 qp_init->qp_type = IB_QPT_RC; 1887 /* 1888 * We divide up our send queue size into half SEND WRs to send the 1889 * completions, and half R/W contexts to actually do the RDMA 1890 * READ/WRITE transfers. Note that we need to allocate CQ slots for 1891 * both both, as RDMA contexts will also post completions for the 1892 * RDMA READ case. 1893 */ 1894 qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr); 1895 qp_init->cap.max_rdma_ctxs = sq_size / 2; 1896 qp_init->cap.max_send_sge = attrs->max_send_sge; 1897 qp_init->cap.max_recv_sge = 1; 1898 qp_init->port_num = ch->sport->port; 1899 if (sdev->use_srq) 1900 qp_init->srq = sdev->srq; 1901 else 1902 qp_init->cap.max_recv_wr = ch->rq_size; 1903 1904 if (ch->using_rdma_cm) { 1905 ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init); 1906 ch->qp = ch->rdma_cm.cm_id->qp; 1907 } else { 1908 ch->qp = ib_create_qp(sdev->pd, qp_init); 1909 if (!IS_ERR(ch->qp)) { 1910 ret = srpt_init_ch_qp(ch, ch->qp); 1911 if (ret) 1912 ib_destroy_qp(ch->qp); 1913 } else { 1914 ret = PTR_ERR(ch->qp); 1915 } 1916 } 1917 if (ret) { 1918 bool retry = sq_size > MIN_SRPT_SQ_SIZE; 1919 1920 if (retry) { 1921 pr_debug("failed to create queue pair with sq_size = %u (%d) - retrying\n", 1922 sq_size, ret); 1923 ib_cq_pool_put(ch->cq, ch->cq_size); 1924 sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE); 1925 goto retry; 1926 } else { 1927 pr_err("failed to create queue pair with sq_size = %u (%d)\n", 1928 sq_size, ret); 1929 goto err_destroy_cq; 1930 } 1931 } 1932 1933 atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr); 1934 1935 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %u ch= %p\n", 1936 __func__, ch->cq->cqe, qp_init->cap.max_send_sge, 1937 qp_init->cap.max_send_wr, ch); 1938 1939 if (!sdev->use_srq) 1940 for (i = 0; i < ch->rq_size; i++) 1941 srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]); 1942 1943 out: 1944 kfree(qp_init); 1945 return ret; 1946 1947 err_destroy_cq: 1948 ch->qp = NULL; 1949 ib_cq_pool_put(ch->cq, ch->cq_size); 1950 goto out; 1951 } 1952 1953 static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch) 1954 { 1955 ib_destroy_qp(ch->qp); 1956 ib_cq_pool_put(ch->cq, ch->cq_size); 1957 } 1958 1959 /** 1960 * srpt_close_ch - close a RDMA channel 1961 * @ch: SRPT RDMA channel. 1962 * 1963 * Make sure all resources associated with the channel will be deallocated at 1964 * an appropriate time. 1965 * 1966 * Returns true if and only if the channel state has been modified into 1967 * CH_DRAINING. 1968 */ 1969 static bool srpt_close_ch(struct srpt_rdma_ch *ch) 1970 { 1971 int ret; 1972 1973 if (!srpt_set_ch_state(ch, CH_DRAINING)) { 1974 pr_debug("%s: already closed\n", ch->sess_name); 1975 return false; 1976 } 1977 1978 kref_get(&ch->kref); 1979 1980 ret = srpt_ch_qp_err(ch); 1981 if (ret < 0) 1982 pr_err("%s-%d: changing queue pair into error state failed: %d\n", 1983 ch->sess_name, ch->qp->qp_num, ret); 1984 1985 ret = srpt_zerolength_write(ch); 1986 if (ret < 0) { 1987 pr_err("%s-%d: queuing zero-length write failed: %d\n", 1988 ch->sess_name, ch->qp->qp_num, ret); 1989 if (srpt_set_ch_state(ch, CH_DISCONNECTED)) 1990 schedule_work(&ch->release_work); 1991 else 1992 WARN_ON_ONCE(true); 1993 } 1994 1995 kref_put(&ch->kref, srpt_free_ch); 1996 1997 return true; 1998 } 1999 2000 /* 2001 * Change the channel state into CH_DISCONNECTING. If a channel has not yet 2002 * reached the connected state, close it. If a channel is in the connected 2003 * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is 2004 * the responsibility of the caller to ensure that this function is not 2005 * invoked concurrently with the code that accepts a connection. This means 2006 * that this function must either be invoked from inside a CM callback 2007 * function or that it must be invoked with the srpt_port.mutex held. 2008 */ 2009 static int srpt_disconnect_ch(struct srpt_rdma_ch *ch) 2010 { 2011 int ret; 2012 2013 if (!srpt_set_ch_state(ch, CH_DISCONNECTING)) 2014 return -ENOTCONN; 2015 2016 if (ch->using_rdma_cm) { 2017 ret = rdma_disconnect(ch->rdma_cm.cm_id); 2018 } else { 2019 ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0); 2020 if (ret < 0) 2021 ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0); 2022 } 2023 2024 if (ret < 0 && srpt_close_ch(ch)) 2025 ret = 0; 2026 2027 return ret; 2028 } 2029 2030 /* Send DREQ and wait for DREP. */ 2031 static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch) 2032 { 2033 DECLARE_COMPLETION_ONSTACK(closed); 2034 struct srpt_port *sport = ch->sport; 2035 2036 pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num, 2037 ch->state); 2038 2039 ch->closed = &closed; 2040 2041 mutex_lock(&sport->mutex); 2042 srpt_disconnect_ch(ch); 2043 mutex_unlock(&sport->mutex); 2044 2045 while (wait_for_completion_timeout(&closed, 5 * HZ) == 0) 2046 pr_info("%s(%s-%d state %d): still waiting ...\n", __func__, 2047 ch->sess_name, ch->qp->qp_num, ch->state); 2048 2049 } 2050 2051 static void __srpt_close_all_ch(struct srpt_port *sport) 2052 { 2053 struct srpt_nexus *nexus; 2054 struct srpt_rdma_ch *ch; 2055 2056 lockdep_assert_held(&sport->mutex); 2057 2058 list_for_each_entry(nexus, &sport->nexus_list, entry) { 2059 list_for_each_entry(ch, &nexus->ch_list, list) { 2060 if (srpt_disconnect_ch(ch) >= 0) 2061 pr_info("Closing channel %s-%d because target %s_%d has been disabled\n", 2062 ch->sess_name, ch->qp->qp_num, 2063 dev_name(&sport->sdev->device->dev), 2064 sport->port); 2065 srpt_close_ch(ch); 2066 } 2067 } 2068 } 2069 2070 /* 2071 * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if 2072 * it does not yet exist. 2073 */ 2074 static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport, 2075 const u8 i_port_id[16], 2076 const u8 t_port_id[16]) 2077 { 2078 struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n; 2079 2080 for (;;) { 2081 mutex_lock(&sport->mutex); 2082 list_for_each_entry(n, &sport->nexus_list, entry) { 2083 if (memcmp(n->i_port_id, i_port_id, 16) == 0 && 2084 memcmp(n->t_port_id, t_port_id, 16) == 0) { 2085 nexus = n; 2086 break; 2087 } 2088 } 2089 if (!nexus && tmp_nexus) { 2090 list_add_tail_rcu(&tmp_nexus->entry, 2091 &sport->nexus_list); 2092 swap(nexus, tmp_nexus); 2093 } 2094 mutex_unlock(&sport->mutex); 2095 2096 if (nexus) 2097 break; 2098 tmp_nexus = kzalloc_obj(*nexus); 2099 if (!tmp_nexus) { 2100 nexus = ERR_PTR(-ENOMEM); 2101 break; 2102 } 2103 INIT_LIST_HEAD(&tmp_nexus->ch_list); 2104 memcpy(tmp_nexus->i_port_id, i_port_id, 16); 2105 memcpy(tmp_nexus->t_port_id, t_port_id, 16); 2106 } 2107 2108 kfree(tmp_nexus); 2109 2110 return nexus; 2111 } 2112 2113 static void srpt_set_enabled(struct srpt_port *sport, bool enabled) 2114 __must_hold(&sport->mutex) 2115 { 2116 lockdep_assert_held(&sport->mutex); 2117 2118 if (sport->enabled == enabled) 2119 return; 2120 sport->enabled = enabled; 2121 if (!enabled) 2122 __srpt_close_all_ch(sport); 2123 } 2124 2125 static void srpt_drop_sport_ref(struct srpt_port *sport) 2126 { 2127 if (atomic_dec_return(&sport->refcount) == 0 && sport->freed_channels) 2128 complete(sport->freed_channels); 2129 } 2130 2131 static void srpt_free_ch(struct kref *kref) 2132 { 2133 struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref); 2134 2135 srpt_drop_sport_ref(ch->sport); 2136 kfree_rcu(ch, rcu); 2137 } 2138 2139 /* 2140 * Shut down the SCSI target session, tell the connection manager to 2141 * disconnect the associated RDMA channel, transition the QP to the error 2142 * state and remove the channel from the channel list. This function is 2143 * typically called from inside srpt_zerolength_write_done(). Concurrent 2144 * srpt_zerolength_write() calls from inside srpt_close_ch() are possible 2145 * as long as the channel is on sport->nexus_list. 2146 */ 2147 static void srpt_release_channel_work(struct work_struct *w) 2148 { 2149 struct srpt_rdma_ch *ch; 2150 struct srpt_device *sdev; 2151 struct srpt_port *sport; 2152 struct se_session *se_sess; 2153 2154 ch = container_of(w, struct srpt_rdma_ch, release_work); 2155 pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num); 2156 2157 sdev = ch->sport->sdev; 2158 BUG_ON(!sdev); 2159 2160 se_sess = ch->sess; 2161 BUG_ON(!se_sess); 2162 2163 target_stop_session(se_sess); 2164 target_wait_for_sess_cmds(se_sess); 2165 2166 target_remove_session(se_sess); 2167 ch->sess = NULL; 2168 2169 if (ch->using_rdma_cm) 2170 rdma_destroy_id(ch->rdma_cm.cm_id); 2171 else 2172 ib_destroy_cm_id(ch->ib_cm.cm_id); 2173 2174 sport = ch->sport; 2175 mutex_lock(&sport->mutex); 2176 list_del_rcu(&ch->list); 2177 mutex_unlock(&sport->mutex); 2178 2179 if (ch->closed) 2180 complete(ch->closed); 2181 2182 srpt_destroy_ch_ib(ch); 2183 2184 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, 2185 ch->sport->sdev, ch->rq_size, 2186 ch->rsp_buf_cache, DMA_TO_DEVICE); 2187 2188 srpt_cache_put(ch->rsp_buf_cache); 2189 2190 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring, 2191 sdev, ch->rq_size, 2192 ch->req_buf_cache, DMA_FROM_DEVICE); 2193 2194 srpt_cache_put(ch->req_buf_cache); 2195 2196 kref_put(&ch->kref, srpt_free_ch); 2197 } 2198 2199 /** 2200 * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED 2201 * @sdev: HCA through which the login request was received. 2202 * @ib_cm_id: IB/CM connection identifier in case of IB/CM. 2203 * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM. 2204 * @port_num: Port through which the REQ message was received. 2205 * @pkey: P_Key of the incoming connection. 2206 * @req: SRP login request. 2207 * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted 2208 * the login request. 2209 * 2210 * Ownership of the cm_id is transferred to the target session if this 2211 * function returns zero. Otherwise the caller remains the owner of cm_id. 2212 */ 2213 static int srpt_cm_req_recv(struct srpt_device *const sdev, 2214 struct ib_cm_id *ib_cm_id, 2215 struct rdma_cm_id *rdma_cm_id, 2216 u8 port_num, __be16 pkey, 2217 const struct srp_login_req *req, 2218 const char *src_addr) 2219 { 2220 struct srpt_port *sport = &sdev->port[port_num - 1]; 2221 struct srpt_nexus *nexus; 2222 struct srp_login_rsp *rsp = NULL; 2223 struct srp_login_rej *rej = NULL; 2224 union { 2225 struct rdma_conn_param rdma_cm; 2226 struct ib_cm_rep_param ib_cm; 2227 } *rep_param = NULL; 2228 struct srpt_rdma_ch *ch = NULL; 2229 char i_port_id[36]; 2230 u32 it_iu_len; 2231 int i, tag_num, tag_size, ret; 2232 struct srpt_tpg *stpg; 2233 2234 WARN_ON_ONCE(irqs_disabled()); 2235 2236 it_iu_len = be32_to_cpu(req->req_it_iu_len); 2237 2238 pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n", 2239 req->initiator_port_id, req->target_port_id, it_iu_len, 2240 port_num, &sport->gid, be16_to_cpu(pkey)); 2241 2242 nexus = srpt_get_nexus(sport, req->initiator_port_id, 2243 req->target_port_id); 2244 if (IS_ERR(nexus)) { 2245 ret = PTR_ERR(nexus); 2246 goto out; 2247 } 2248 2249 ret = -ENOMEM; 2250 rsp = kzalloc_obj(*rsp); 2251 rej = kzalloc_obj(*rej); 2252 rep_param = kzalloc_obj(*rep_param); 2253 if (!rsp || !rej || !rep_param) 2254 goto out; 2255 2256 ret = -EINVAL; 2257 if (it_iu_len > srp_max_req_size || it_iu_len < 64) { 2258 rej->reason = cpu_to_be32( 2259 SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE); 2260 pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n", 2261 it_iu_len, 64, srp_max_req_size); 2262 goto reject; 2263 } 2264 2265 if (!sport->enabled) { 2266 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2267 pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n", 2268 dev_name(&sport->sdev->device->dev), port_num); 2269 goto reject; 2270 } 2271 2272 if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid) 2273 || *(__be64 *)(req->target_port_id + 8) != 2274 cpu_to_be64(srpt_service_guid)) { 2275 rej->reason = cpu_to_be32( 2276 SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL); 2277 pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n"); 2278 goto reject; 2279 } 2280 2281 ret = -ENOMEM; 2282 ch = kzalloc_obj(*ch); 2283 if (!ch) { 2284 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2285 pr_err("rejected SRP_LOGIN_REQ because out of memory.\n"); 2286 goto reject; 2287 } 2288 2289 kref_init(&ch->kref); 2290 ch->pkey = be16_to_cpu(pkey); 2291 ch->nexus = nexus; 2292 ch->zw_cqe.done = srpt_zerolength_write_done; 2293 INIT_WORK(&ch->release_work, srpt_release_channel_work); 2294 ch->sport = sport; 2295 if (rdma_cm_id) { 2296 ch->using_rdma_cm = true; 2297 ch->rdma_cm.cm_id = rdma_cm_id; 2298 rdma_cm_id->context = ch; 2299 } else { 2300 ch->ib_cm.cm_id = ib_cm_id; 2301 ib_cm_id->context = ch; 2302 } 2303 /* 2304 * ch->rq_size should be at least as large as the initiator queue 2305 * depth to avoid that the initiator driver has to report QUEUE_FULL 2306 * to the SCSI mid-layer. 2307 */ 2308 ch->rq_size = min(sdev->device->attrs.max_qp_wr, MAX_SRPT_RQ_SIZE); 2309 spin_lock_init(&ch->spinlock); 2310 ch->state = CH_CONNECTING; 2311 INIT_LIST_HEAD(&ch->cmd_wait_list); 2312 ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size; 2313 2314 ch->rsp_buf_cache = srpt_cache_get(ch->max_rsp_size); 2315 if (!ch->rsp_buf_cache) 2316 goto free_ch; 2317 2318 ch->ioctx_ring = (struct srpt_send_ioctx **) 2319 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, 2320 sizeof(*ch->ioctx_ring[0]), 2321 ch->rsp_buf_cache, 0, DMA_TO_DEVICE); 2322 if (!ch->ioctx_ring) { 2323 pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n"); 2324 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2325 goto free_rsp_cache; 2326 } 2327 2328 for (i = 0; i < ch->rq_size; i++) 2329 ch->ioctx_ring[i]->ch = ch; 2330 if (!sdev->use_srq) { 2331 u16 imm_data_offset = req->req_flags & SRP_IMMED_REQUESTED ? 2332 be16_to_cpu(req->imm_data_offset) : 0; 2333 u16 alignment_offset; 2334 u32 req_sz; 2335 2336 if (req->req_flags & SRP_IMMED_REQUESTED) 2337 pr_debug("imm_data_offset = %d\n", 2338 be16_to_cpu(req->imm_data_offset)); 2339 if (imm_data_offset >= sizeof(struct srp_cmd)) { 2340 ch->imm_data_offset = imm_data_offset; 2341 rsp->rsp_flags |= SRP_LOGIN_RSP_IMMED_SUPP; 2342 } else { 2343 ch->imm_data_offset = 0; 2344 } 2345 alignment_offset = round_up(imm_data_offset, 512) - 2346 imm_data_offset; 2347 req_sz = alignment_offset + imm_data_offset + srp_max_req_size; 2348 ch->req_buf_cache = srpt_cache_get(req_sz); 2349 if (!ch->req_buf_cache) 2350 goto free_rsp_ring; 2351 2352 ch->ioctx_recv_ring = (struct srpt_recv_ioctx **) 2353 srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, 2354 sizeof(*ch->ioctx_recv_ring[0]), 2355 ch->req_buf_cache, 2356 alignment_offset, 2357 DMA_FROM_DEVICE); 2358 if (!ch->ioctx_recv_ring) { 2359 pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n"); 2360 rej->reason = 2361 cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2362 goto free_recv_cache; 2363 } 2364 for (i = 0; i < ch->rq_size; i++) 2365 INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list); 2366 } 2367 2368 ret = srpt_create_ch_ib(ch); 2369 if (ret) { 2370 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2371 pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n"); 2372 goto free_recv_ring; 2373 } 2374 2375 strscpy(ch->sess_name, src_addr, sizeof(ch->sess_name)); 2376 snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx", 2377 be64_to_cpu(*(__be64 *)nexus->i_port_id), 2378 be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8))); 2379 2380 pr_debug("registering src addr %s or i_port_id %s\n", ch->sess_name, 2381 i_port_id); 2382 2383 tag_num = ch->rq_size; 2384 tag_size = 1; /* ib_srpt does not use se_sess->sess_cmd_map */ 2385 2386 if (sport->guid_id) { 2387 mutex_lock(&sport->guid_id->mutex); 2388 list_for_each_entry(stpg, &sport->guid_id->tpg_list, entry) { 2389 if (!IS_ERR_OR_NULL(ch->sess)) 2390 break; 2391 ch->sess = target_setup_session(&stpg->tpg, tag_num, 2392 tag_size, TARGET_PROT_NORMAL, 2393 ch->sess_name, ch, NULL); 2394 } 2395 mutex_unlock(&sport->guid_id->mutex); 2396 } 2397 2398 if (sport->gid_id) { 2399 mutex_lock(&sport->gid_id->mutex); 2400 list_for_each_entry(stpg, &sport->gid_id->tpg_list, entry) { 2401 if (!IS_ERR_OR_NULL(ch->sess)) 2402 break; 2403 ch->sess = target_setup_session(&stpg->tpg, tag_num, 2404 tag_size, TARGET_PROT_NORMAL, i_port_id, 2405 ch, NULL); 2406 if (!IS_ERR_OR_NULL(ch->sess)) 2407 break; 2408 /* Retry without leading "0x" */ 2409 ch->sess = target_setup_session(&stpg->tpg, tag_num, 2410 tag_size, TARGET_PROT_NORMAL, 2411 i_port_id + 2, ch, NULL); 2412 } 2413 mutex_unlock(&sport->gid_id->mutex); 2414 } 2415 2416 if (IS_ERR_OR_NULL(ch->sess)) { 2417 WARN_ON_ONCE(ch->sess == NULL); 2418 ret = PTR_ERR(ch->sess); 2419 ch->sess = NULL; 2420 pr_info("Rejected login for initiator %s: ret = %d.\n", 2421 ch->sess_name, ret); 2422 rej->reason = cpu_to_be32(ret == -ENOMEM ? 2423 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES : 2424 SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED); 2425 goto destroy_ib; 2426 } 2427 2428 /* 2429 * Once a session has been created destruction of srpt_rdma_ch objects 2430 * will decrement sport->refcount. Hence increment sport->refcount now. 2431 */ 2432 atomic_inc(&sport->refcount); 2433 2434 mutex_lock(&sport->mutex); 2435 2436 if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) { 2437 struct srpt_rdma_ch *ch2; 2438 2439 list_for_each_entry(ch2, &nexus->ch_list, list) { 2440 if (srpt_disconnect_ch(ch2) < 0) 2441 continue; 2442 pr_info("Relogin - closed existing channel %s\n", 2443 ch2->sess_name); 2444 rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_TERMINATED; 2445 } 2446 } else { 2447 rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_MAINTAINED; 2448 } 2449 2450 list_add_tail_rcu(&ch->list, &nexus->ch_list); 2451 2452 if (!sport->enabled) { 2453 rej->reason = cpu_to_be32( 2454 SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2455 pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n", 2456 dev_name(&sdev->device->dev), port_num); 2457 mutex_unlock(&sport->mutex); 2458 ret = -EINVAL; 2459 goto reject; 2460 } 2461 2462 mutex_unlock(&sport->mutex); 2463 2464 ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp); 2465 if (ret) { 2466 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2467 pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n", 2468 ret); 2469 goto reject; 2470 } 2471 2472 pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess, 2473 ch->sess_name, ch); 2474 2475 /* create srp_login_response */ 2476 rsp->opcode = SRP_LOGIN_RSP; 2477 rsp->tag = req->tag; 2478 rsp->max_it_iu_len = cpu_to_be32(srp_max_req_size); 2479 rsp->max_ti_iu_len = req->req_it_iu_len; 2480 ch->max_ti_iu_len = it_iu_len; 2481 rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 2482 SRP_BUF_FORMAT_INDIRECT); 2483 rsp->req_lim_delta = cpu_to_be32(ch->rq_size); 2484 atomic_set(&ch->req_lim, ch->rq_size); 2485 atomic_set(&ch->req_lim_delta, 0); 2486 2487 /* create cm reply */ 2488 if (ch->using_rdma_cm) { 2489 rep_param->rdma_cm.private_data = (void *)rsp; 2490 rep_param->rdma_cm.private_data_len = sizeof(*rsp); 2491 rep_param->rdma_cm.rnr_retry_count = 7; 2492 rep_param->rdma_cm.flow_control = 1; 2493 rep_param->rdma_cm.responder_resources = 4; 2494 rep_param->rdma_cm.initiator_depth = 4; 2495 } else { 2496 rep_param->ib_cm.qp_num = ch->qp->qp_num; 2497 rep_param->ib_cm.private_data = (void *)rsp; 2498 rep_param->ib_cm.private_data_len = sizeof(*rsp); 2499 rep_param->ib_cm.rnr_retry_count = 7; 2500 rep_param->ib_cm.flow_control = 1; 2501 rep_param->ib_cm.failover_accepted = 0; 2502 rep_param->ib_cm.srq = 1; 2503 rep_param->ib_cm.responder_resources = 4; 2504 rep_param->ib_cm.initiator_depth = 4; 2505 } 2506 2507 /* 2508 * Hold the sport mutex while accepting a connection to avoid that 2509 * srpt_disconnect_ch() is invoked concurrently with this code. 2510 */ 2511 mutex_lock(&sport->mutex); 2512 if (sport->enabled && ch->state == CH_CONNECTING) { 2513 if (ch->using_rdma_cm) 2514 ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm); 2515 else 2516 ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm); 2517 } else { 2518 ret = -EINVAL; 2519 } 2520 mutex_unlock(&sport->mutex); 2521 2522 switch (ret) { 2523 case 0: 2524 break; 2525 case -EINVAL: 2526 goto reject; 2527 default: 2528 rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 2529 pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n", 2530 ret); 2531 goto reject; 2532 } 2533 2534 goto out; 2535 2536 destroy_ib: 2537 srpt_destroy_ch_ib(ch); 2538 2539 free_recv_ring: 2540 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring, 2541 ch->sport->sdev, ch->rq_size, 2542 ch->req_buf_cache, DMA_FROM_DEVICE); 2543 2544 free_recv_cache: 2545 srpt_cache_put(ch->req_buf_cache); 2546 2547 free_rsp_ring: 2548 srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, 2549 ch->sport->sdev, ch->rq_size, 2550 ch->rsp_buf_cache, DMA_TO_DEVICE); 2551 2552 free_rsp_cache: 2553 srpt_cache_put(ch->rsp_buf_cache); 2554 2555 free_ch: 2556 if (rdma_cm_id) 2557 rdma_cm_id->context = NULL; 2558 else 2559 ib_cm_id->context = NULL; 2560 kfree(ch); 2561 ch = NULL; 2562 2563 WARN_ON_ONCE(ret == 0); 2564 2565 reject: 2566 pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason)); 2567 rej->opcode = SRP_LOGIN_REJ; 2568 rej->tag = req->tag; 2569 rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 2570 SRP_BUF_FORMAT_INDIRECT); 2571 2572 if (rdma_cm_id) 2573 rdma_reject(rdma_cm_id, rej, sizeof(*rej), 2574 IB_CM_REJ_CONSUMER_DEFINED); 2575 else 2576 ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, 2577 rej, sizeof(*rej)); 2578 2579 if (ch && ch->sess) { 2580 srpt_close_ch(ch); 2581 /* 2582 * Tell the caller not to free cm_id since 2583 * srpt_release_channel_work() will do that. 2584 */ 2585 ret = 0; 2586 } 2587 2588 out: 2589 kfree(rep_param); 2590 kfree(rsp); 2591 kfree(rej); 2592 2593 return ret; 2594 } 2595 2596 static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id, 2597 const struct ib_cm_req_event_param *param, 2598 void *private_data) 2599 { 2600 char sguid[40]; 2601 2602 srpt_format_guid(sguid, sizeof(sguid), 2603 ¶m->primary_path->dgid.global.interface_id); 2604 2605 return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port, 2606 param->primary_path->pkey, 2607 private_data, sguid); 2608 } 2609 2610 static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id, 2611 struct rdma_cm_event *event) 2612 { 2613 struct srpt_device *sdev; 2614 struct srp_login_req req; 2615 const struct srp_login_req_rdma *req_rdma; 2616 struct sa_path_rec *path_rec = cm_id->route.path_rec; 2617 char src_addr[40]; 2618 2619 sdev = ib_get_client_data(cm_id->device, &srpt_client); 2620 if (!sdev) 2621 return -ECONNREFUSED; 2622 2623 if (event->param.conn.private_data_len < sizeof(*req_rdma)) 2624 return -EINVAL; 2625 2626 /* Transform srp_login_req_rdma into srp_login_req. */ 2627 req_rdma = event->param.conn.private_data; 2628 memset(&req, 0, sizeof(req)); 2629 req.opcode = req_rdma->opcode; 2630 req.tag = req_rdma->tag; 2631 req.req_it_iu_len = req_rdma->req_it_iu_len; 2632 req.req_buf_fmt = req_rdma->req_buf_fmt; 2633 req.req_flags = req_rdma->req_flags; 2634 memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16); 2635 memcpy(req.target_port_id, req_rdma->target_port_id, 16); 2636 req.imm_data_offset = req_rdma->imm_data_offset; 2637 2638 snprintf(src_addr, sizeof(src_addr), "%pIS", 2639 &cm_id->route.addr.src_addr); 2640 2641 return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num, 2642 path_rec ? path_rec->pkey : 0, &req, src_addr); 2643 } 2644 2645 static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch, 2646 enum ib_cm_rej_reason reason, 2647 const u8 *private_data, 2648 u8 private_data_len) 2649 { 2650 char *priv = NULL; 2651 int i; 2652 2653 if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1, 2654 GFP_KERNEL))) { 2655 for (i = 0; i < private_data_len; i++) 2656 sprintf(priv + 3 * i, " %02x", private_data[i]); 2657 } 2658 pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n", 2659 ch->sess_name, ch->qp->qp_num, reason, private_data_len ? 2660 "; private data" : "", priv ? priv : " (?)"); 2661 kfree(priv); 2662 } 2663 2664 /** 2665 * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event 2666 * @ch: SRPT RDMA channel. 2667 * 2668 * An RTU (ready to use) message indicates that the connection has been 2669 * established and that the recipient may begin transmitting. 2670 */ 2671 static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch) 2672 { 2673 int ret; 2674 2675 ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp); 2676 if (ret < 0) { 2677 pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name, 2678 ch->qp->qp_num); 2679 srpt_close_ch(ch); 2680 return; 2681 } 2682 2683 /* 2684 * Note: calling srpt_close_ch() if the transition to the LIVE state 2685 * fails is not necessary since that means that that function has 2686 * already been invoked from another thread. 2687 */ 2688 if (!srpt_set_ch_state(ch, CH_LIVE)) { 2689 pr_err("%s-%d: channel transition to LIVE state failed\n", 2690 ch->sess_name, ch->qp->qp_num); 2691 return; 2692 } 2693 2694 /* Trigger wait list processing. */ 2695 ret = srpt_zerolength_write(ch); 2696 WARN_ONCE(ret < 0, "%d\n", ret); 2697 } 2698 2699 /** 2700 * srpt_cm_handler - IB connection manager callback function 2701 * @cm_id: IB/CM connection identifier. 2702 * @event: IB/CM event. 2703 * 2704 * A non-zero return value will cause the caller destroy the CM ID. 2705 * 2706 * Note: srpt_cm_handler() must only return a non-zero value when transferring 2707 * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning 2708 * a non-zero value in any other case will trigger a race with the 2709 * ib_destroy_cm_id() call in srpt_release_channel(). 2710 */ 2711 static int srpt_cm_handler(struct ib_cm_id *cm_id, 2712 const struct ib_cm_event *event) 2713 { 2714 struct srpt_rdma_ch *ch = cm_id->context; 2715 int ret; 2716 2717 ret = 0; 2718 switch (event->event) { 2719 case IB_CM_REQ_RECEIVED: 2720 ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd, 2721 event->private_data); 2722 break; 2723 case IB_CM_REJ_RECEIVED: 2724 srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason, 2725 event->private_data, 2726 IB_CM_REJ_PRIVATE_DATA_SIZE); 2727 break; 2728 case IB_CM_RTU_RECEIVED: 2729 case IB_CM_USER_ESTABLISHED: 2730 srpt_cm_rtu_recv(ch); 2731 break; 2732 case IB_CM_DREQ_RECEIVED: 2733 srpt_disconnect_ch(ch); 2734 break; 2735 case IB_CM_DREP_RECEIVED: 2736 pr_info("Received CM DREP message for ch %s-%d.\n", 2737 ch->sess_name, ch->qp->qp_num); 2738 srpt_close_ch(ch); 2739 break; 2740 case IB_CM_TIMEWAIT_EXIT: 2741 pr_info("Received CM TimeWait exit for ch %s-%d.\n", 2742 ch->sess_name, ch->qp->qp_num); 2743 srpt_close_ch(ch); 2744 break; 2745 case IB_CM_REP_ERROR: 2746 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, 2747 ch->qp->qp_num); 2748 break; 2749 case IB_CM_DREQ_ERROR: 2750 pr_info("Received CM DREQ ERROR event.\n"); 2751 break; 2752 case IB_CM_MRA_RECEIVED: 2753 pr_info("Received CM MRA event\n"); 2754 break; 2755 default: 2756 pr_err("received unrecognized CM event %d\n", event->event); 2757 break; 2758 } 2759 2760 return ret; 2761 } 2762 2763 static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id, 2764 struct rdma_cm_event *event) 2765 { 2766 struct srpt_rdma_ch *ch = cm_id->context; 2767 int ret = 0; 2768 2769 switch (event->event) { 2770 case RDMA_CM_EVENT_CONNECT_REQUEST: 2771 ret = srpt_rdma_cm_req_recv(cm_id, event); 2772 break; 2773 case RDMA_CM_EVENT_REJECTED: 2774 srpt_cm_rej_recv(ch, event->status, 2775 event->param.conn.private_data, 2776 event->param.conn.private_data_len); 2777 break; 2778 case RDMA_CM_EVENT_ESTABLISHED: 2779 srpt_cm_rtu_recv(ch); 2780 break; 2781 case RDMA_CM_EVENT_DISCONNECTED: 2782 if (ch->state < CH_DISCONNECTING) 2783 srpt_disconnect_ch(ch); 2784 else 2785 srpt_close_ch(ch); 2786 break; 2787 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 2788 srpt_close_ch(ch); 2789 break; 2790 case RDMA_CM_EVENT_UNREACHABLE: 2791 pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, 2792 ch->qp->qp_num); 2793 break; 2794 case RDMA_CM_EVENT_DEVICE_REMOVAL: 2795 case RDMA_CM_EVENT_ADDR_CHANGE: 2796 break; 2797 default: 2798 pr_err("received unrecognized RDMA CM event %d\n", 2799 event->event); 2800 break; 2801 } 2802 2803 return ret; 2804 } 2805 2806 /* 2807 * srpt_write_pending - Start data transfer from initiator to target (write). 2808 */ 2809 static int srpt_write_pending(struct se_cmd *se_cmd) 2810 { 2811 struct srpt_send_ioctx *ioctx = 2812 container_of(se_cmd, struct srpt_send_ioctx, cmd); 2813 struct srpt_rdma_ch *ch = ioctx->ch; 2814 struct ib_send_wr *first_wr = NULL; 2815 struct ib_cqe *cqe = &ioctx->rdma_cqe; 2816 enum srpt_command_state new_state; 2817 int ret, i; 2818 2819 if (ioctx->recv_ioctx) { 2820 srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN); 2821 target_execute_cmd(&ioctx->cmd); 2822 return 0; 2823 } 2824 2825 new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA); 2826 WARN_ON(new_state == SRPT_STATE_DONE); 2827 2828 if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) { 2829 pr_warn("%s: IB send queue full (needed %d)\n", 2830 __func__, ioctx->n_rdma); 2831 ret = -ENOMEM; 2832 goto out_undo; 2833 } 2834 2835 cqe->done = srpt_rdma_read_done; 2836 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) { 2837 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 2838 2839 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port, 2840 cqe, first_wr); 2841 cqe = NULL; 2842 } 2843 2844 ret = ib_post_send(ch->qp, first_wr, NULL); 2845 if (ret) { 2846 pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n", 2847 __func__, ret, ioctx->n_rdma, 2848 atomic_read(&ch->sq_wr_avail)); 2849 goto out_undo; 2850 } 2851 2852 return 0; 2853 out_undo: 2854 atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); 2855 return ret; 2856 } 2857 2858 static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status) 2859 { 2860 switch (tcm_mgmt_status) { 2861 case TMR_FUNCTION_COMPLETE: 2862 return SRP_TSK_MGMT_SUCCESS; 2863 case TMR_FUNCTION_REJECTED: 2864 return SRP_TSK_MGMT_FUNC_NOT_SUPP; 2865 } 2866 return SRP_TSK_MGMT_FAILED; 2867 } 2868 2869 /** 2870 * srpt_queue_response - transmit the response to a SCSI command 2871 * @cmd: SCSI target command. 2872 * 2873 * Callback function called by the TCM core. Must not block since it can be 2874 * invoked on the context of the IB completion handler. 2875 */ 2876 static void srpt_queue_response(struct se_cmd *cmd) 2877 { 2878 struct srpt_send_ioctx *ioctx = 2879 container_of(cmd, struct srpt_send_ioctx, cmd); 2880 struct srpt_rdma_ch *ch = ioctx->ch; 2881 struct srpt_device *sdev = ch->sport->sdev; 2882 struct ib_send_wr send_wr, *first_wr = &send_wr; 2883 struct ib_sge sge; 2884 enum srpt_command_state state; 2885 int resp_len, ret, i; 2886 u8 srp_tm_status; 2887 2888 state = ioctx->state; 2889 switch (state) { 2890 case SRPT_STATE_NEW: 2891 case SRPT_STATE_DATA_IN: 2892 ioctx->state = SRPT_STATE_CMD_RSP_SENT; 2893 break; 2894 case SRPT_STATE_MGMT: 2895 ioctx->state = SRPT_STATE_MGMT_RSP_SENT; 2896 break; 2897 default: 2898 WARN(true, "ch %p; cmd %d: unexpected command state %d\n", 2899 ch, ioctx->ioctx.index, ioctx->state); 2900 break; 2901 } 2902 2903 if (WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)) 2904 return; 2905 2906 /* For read commands, transfer the data to the initiator. */ 2907 if (ioctx->cmd.data_direction == DMA_FROM_DEVICE && 2908 ioctx->cmd.data_length && 2909 !ioctx->queue_status_only) { 2910 for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) { 2911 struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 2912 2913 first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, 2914 ch->sport->port, NULL, first_wr); 2915 } 2916 } 2917 2918 if (state != SRPT_STATE_MGMT) 2919 resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag, 2920 cmd->scsi_status); 2921 else { 2922 srp_tm_status 2923 = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response); 2924 resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status, 2925 ioctx->cmd.tag); 2926 } 2927 2928 atomic_inc(&ch->req_lim); 2929 2930 if (unlikely(atomic_sub_return(1 + ioctx->n_rdma, 2931 &ch->sq_wr_avail) < 0)) { 2932 pr_warn("%s: IB send queue full (needed %d)\n", 2933 __func__, ioctx->n_rdma); 2934 goto out; 2935 } 2936 2937 ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len, 2938 DMA_TO_DEVICE); 2939 2940 sge.addr = ioctx->ioctx.dma; 2941 sge.length = resp_len; 2942 sge.lkey = sdev->lkey; 2943 2944 ioctx->ioctx.cqe.done = srpt_send_done; 2945 send_wr.next = NULL; 2946 send_wr.wr_cqe = &ioctx->ioctx.cqe; 2947 send_wr.sg_list = &sge; 2948 send_wr.num_sge = 1; 2949 send_wr.opcode = IB_WR_SEND; 2950 send_wr.send_flags = IB_SEND_SIGNALED; 2951 2952 ret = ib_post_send(ch->qp, first_wr, NULL); 2953 if (ret < 0) { 2954 pr_err("%s: sending cmd response failed for tag %llu (%d)\n", 2955 __func__, ioctx->cmd.tag, ret); 2956 goto out; 2957 } 2958 2959 return; 2960 2961 out: 2962 atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail); 2963 atomic_dec(&ch->req_lim); 2964 srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); 2965 target_put_sess_cmd(&ioctx->cmd); 2966 } 2967 2968 static int srpt_queue_data_in(struct se_cmd *cmd) 2969 { 2970 srpt_queue_response(cmd); 2971 return 0; 2972 } 2973 2974 static void srpt_queue_tm_rsp(struct se_cmd *cmd) 2975 { 2976 srpt_queue_response(cmd); 2977 } 2978 2979 /* 2980 * This function is called for aborted commands if no response is sent to the 2981 * initiator. Make sure that the credits freed by aborting a command are 2982 * returned to the initiator the next time a response is sent by incrementing 2983 * ch->req_lim_delta. 2984 */ 2985 static void srpt_aborted_task(struct se_cmd *cmd) 2986 { 2987 struct srpt_send_ioctx *ioctx = container_of(cmd, 2988 struct srpt_send_ioctx, cmd); 2989 struct srpt_rdma_ch *ch = ioctx->ch; 2990 2991 atomic_inc(&ch->req_lim_delta); 2992 } 2993 2994 static int srpt_queue_status(struct se_cmd *cmd) 2995 { 2996 struct srpt_send_ioctx *ioctx; 2997 2998 ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); 2999 BUG_ON(ioctx->sense_data != cmd->sense_buffer); 3000 if (cmd->se_cmd_flags & 3001 (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE)) 3002 WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION); 3003 ioctx->queue_status_only = true; 3004 srpt_queue_response(cmd); 3005 return 0; 3006 } 3007 3008 static void srpt_refresh_port_work(struct work_struct *work) 3009 { 3010 struct srpt_port *sport = container_of(work, struct srpt_port, work); 3011 3012 srpt_refresh_port(sport); 3013 } 3014 3015 /** 3016 * srpt_release_sport - disable login and wait for associated channels 3017 * @sport: SRPT HCA port. 3018 */ 3019 static int srpt_release_sport(struct srpt_port *sport) 3020 { 3021 DECLARE_COMPLETION_ONSTACK(c); 3022 struct srpt_nexus *nexus, *next_n; 3023 struct srpt_rdma_ch *ch; 3024 3025 WARN_ON_ONCE(irqs_disabled()); 3026 3027 sport->freed_channels = &c; 3028 3029 mutex_lock(&sport->mutex); 3030 srpt_set_enabled(sport, false); 3031 mutex_unlock(&sport->mutex); 3032 3033 while (atomic_read(&sport->refcount) > 0 && 3034 wait_for_completion_timeout(&c, 5 * HZ) <= 0) { 3035 pr_info("%s_%d: waiting for unregistration of %d sessions ...\n", 3036 dev_name(&sport->sdev->device->dev), sport->port, 3037 atomic_read(&sport->refcount)); 3038 rcu_read_lock(); 3039 list_for_each_entry(nexus, &sport->nexus_list, entry) { 3040 list_for_each_entry(ch, &nexus->ch_list, list) { 3041 pr_info("%s-%d: state %s\n", 3042 ch->sess_name, ch->qp->qp_num, 3043 get_ch_state_name(ch->state)); 3044 } 3045 } 3046 rcu_read_unlock(); 3047 } 3048 3049 mutex_lock(&sport->mutex); 3050 list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) { 3051 list_del(&nexus->entry); 3052 kfree_rcu(nexus, rcu); 3053 } 3054 mutex_unlock(&sport->mutex); 3055 3056 return 0; 3057 } 3058 3059 struct port_and_port_id { 3060 struct srpt_port *sport; 3061 struct srpt_port_id **port_id; 3062 }; 3063 3064 static struct port_and_port_id __srpt_lookup_port(const char *name) 3065 { 3066 struct ib_device *dev; 3067 struct srpt_device *sdev; 3068 struct srpt_port *sport; 3069 int i; 3070 3071 list_for_each_entry(sdev, &srpt_dev_list, list) { 3072 dev = sdev->device; 3073 if (!dev) 3074 continue; 3075 3076 for (i = 0; i < dev->phys_port_cnt; i++) { 3077 sport = &sdev->port[i]; 3078 3079 if (strcmp(sport->guid_name, name) == 0) { 3080 kref_get(&sdev->refcnt); 3081 return (struct port_and_port_id){ 3082 sport, &sport->guid_id}; 3083 } 3084 if (strcmp(sport->gid_name, name) == 0) { 3085 kref_get(&sdev->refcnt); 3086 return (struct port_and_port_id){ 3087 sport, &sport->gid_id}; 3088 } 3089 } 3090 } 3091 3092 return (struct port_and_port_id){}; 3093 } 3094 3095 /** 3096 * srpt_lookup_port() - Look up an RDMA port by name 3097 * @name: ASCII port name 3098 * 3099 * Increments the RDMA port reference count if an RDMA port pointer is returned. 3100 * The caller must drop that reference count by calling srpt_port_put_ref(). 3101 */ 3102 static struct port_and_port_id srpt_lookup_port(const char *name) 3103 { 3104 struct port_and_port_id papi; 3105 3106 spin_lock(&srpt_dev_lock); 3107 papi = __srpt_lookup_port(name); 3108 spin_unlock(&srpt_dev_lock); 3109 3110 return papi; 3111 } 3112 3113 static void srpt_free_srq(struct srpt_device *sdev) 3114 { 3115 if (!sdev->srq) 3116 return; 3117 3118 ib_destroy_srq(sdev->srq); 3119 srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, 3120 sdev->srq_size, sdev->req_buf_cache, 3121 DMA_FROM_DEVICE); 3122 srpt_cache_put(sdev->req_buf_cache); 3123 sdev->srq = NULL; 3124 } 3125 3126 static int srpt_alloc_srq(struct srpt_device *sdev) 3127 { 3128 struct ib_srq_init_attr srq_attr = { 3129 .event_handler = srpt_srq_event, 3130 .srq_context = (void *)sdev, 3131 .attr.max_wr = sdev->srq_size, 3132 .attr.max_sge = 1, 3133 .srq_type = IB_SRQT_BASIC, 3134 }; 3135 struct ib_device *device = sdev->device; 3136 struct ib_srq *srq; 3137 int i; 3138 3139 WARN_ON_ONCE(sdev->srq); 3140 srq = ib_create_srq(sdev->pd, &srq_attr); 3141 if (IS_ERR(srq)) { 3142 pr_debug("ib_create_srq() failed: %pe\n", srq); 3143 return PTR_ERR(srq); 3144 } 3145 3146 pr_debug("create SRQ #wr= %d max_allow=%u dev= %s\n", sdev->srq_size, 3147 sdev->device->attrs.max_srq_wr, dev_name(&device->dev)); 3148 3149 sdev->req_buf_cache = srpt_cache_get(srp_max_req_size); 3150 if (!sdev->req_buf_cache) 3151 goto free_srq; 3152 3153 sdev->ioctx_ring = (struct srpt_recv_ioctx **) 3154 srpt_alloc_ioctx_ring(sdev, sdev->srq_size, 3155 sizeof(*sdev->ioctx_ring[0]), 3156 sdev->req_buf_cache, 0, DMA_FROM_DEVICE); 3157 if (!sdev->ioctx_ring) 3158 goto free_cache; 3159 3160 sdev->use_srq = true; 3161 sdev->srq = srq; 3162 3163 for (i = 0; i < sdev->srq_size; ++i) { 3164 INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list); 3165 srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]); 3166 } 3167 3168 return 0; 3169 3170 free_cache: 3171 srpt_cache_put(sdev->req_buf_cache); 3172 3173 free_srq: 3174 ib_destroy_srq(srq); 3175 return -ENOMEM; 3176 } 3177 3178 static int srpt_use_srq(struct srpt_device *sdev, bool use_srq) 3179 { 3180 struct ib_device *device = sdev->device; 3181 int ret = 0; 3182 3183 if (!use_srq) { 3184 srpt_free_srq(sdev); 3185 sdev->use_srq = false; 3186 } else if (use_srq && !sdev->srq) { 3187 ret = srpt_alloc_srq(sdev); 3188 } 3189 pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, 3190 dev_name(&device->dev), sdev->use_srq, ret); 3191 return ret; 3192 } 3193 3194 static void srpt_free_sdev(struct kref *refcnt) 3195 { 3196 struct srpt_device *sdev = container_of(refcnt, typeof(*sdev), refcnt); 3197 3198 kfree(sdev); 3199 } 3200 3201 static void srpt_sdev_put(struct srpt_device *sdev) 3202 { 3203 kref_put(&sdev->refcnt, srpt_free_sdev); 3204 } 3205 3206 /** 3207 * srpt_add_one - InfiniBand device addition callback function 3208 * @device: Describes a HCA. 3209 */ 3210 static int srpt_add_one(struct ib_device *device) 3211 { 3212 struct srpt_device *sdev; 3213 struct srpt_port *sport; 3214 int ret; 3215 u32 i; 3216 3217 pr_debug("device = %p\n", device); 3218 3219 sdev = kzalloc_flex(*sdev, port, device->phys_port_cnt); 3220 if (!sdev) 3221 return -ENOMEM; 3222 3223 kref_init(&sdev->refcnt); 3224 sdev->device = device; 3225 mutex_init(&sdev->sdev_mutex); 3226 3227 sdev->pd = ib_alloc_pd(device, 0); 3228 if (IS_ERR(sdev->pd)) { 3229 ret = PTR_ERR(sdev->pd); 3230 goto free_dev; 3231 } 3232 3233 sdev->lkey = sdev->pd->local_dma_lkey; 3234 3235 sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr); 3236 3237 srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq); 3238 3239 if (!srpt_service_guid) 3240 srpt_service_guid = be64_to_cpu(device->node_guid); 3241 3242 if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND) 3243 sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev); 3244 if (IS_ERR(sdev->cm_id)) { 3245 pr_info("ib_create_cm_id() failed: %pe\n", sdev->cm_id); 3246 ret = PTR_ERR(sdev->cm_id); 3247 sdev->cm_id = NULL; 3248 if (!rdma_cm_id) 3249 goto err_ring; 3250 } 3251 3252 /* print out target login information */ 3253 pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n", 3254 srpt_service_guid, srpt_service_guid, srpt_service_guid); 3255 3256 /* 3257 * We do not have a consistent service_id (ie. also id_ext of target_id) 3258 * to identify this target. We currently use the guid of the first HCA 3259 * in the system as service_id; therefore, the target_id will change 3260 * if this HCA is gone bad and replaced by different HCA 3261 */ 3262 ret = sdev->cm_id ? 3263 ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid)) : 3264 0; 3265 if (ret < 0) { 3266 pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret, 3267 sdev->cm_id->state); 3268 goto err_cm; 3269 } 3270 3271 INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device, 3272 srpt_event_handler); 3273 3274 for (i = 1; i <= sdev->device->phys_port_cnt; i++) { 3275 sport = &sdev->port[i - 1]; 3276 INIT_LIST_HEAD(&sport->nexus_list); 3277 mutex_init(&sport->mutex); 3278 sport->sdev = sdev; 3279 sport->port = i; 3280 sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE; 3281 sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE; 3282 sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE; 3283 sport->port_attrib.use_srq = false; 3284 INIT_WORK(&sport->work, srpt_refresh_port_work); 3285 3286 ret = srpt_refresh_port(sport); 3287 if (ret) { 3288 pr_err("MAD registration failed for %s-%d.\n", 3289 dev_name(&sdev->device->dev), i); 3290 i--; 3291 goto err_port; 3292 } 3293 } 3294 3295 ib_register_event_handler(&sdev->event_handler); 3296 spin_lock(&srpt_dev_lock); 3297 list_add_tail(&sdev->list, &srpt_dev_list); 3298 spin_unlock(&srpt_dev_lock); 3299 3300 ib_set_client_data(device, &srpt_client, sdev); 3301 pr_debug("added %s.\n", dev_name(&device->dev)); 3302 return 0; 3303 3304 err_port: 3305 srpt_unregister_mad_agent(sdev, i); 3306 err_cm: 3307 if (sdev->cm_id) 3308 ib_destroy_cm_id(sdev->cm_id); 3309 err_ring: 3310 srpt_free_srq(sdev); 3311 ib_dealloc_pd(sdev->pd); 3312 free_dev: 3313 srpt_sdev_put(sdev); 3314 pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev)); 3315 return ret; 3316 } 3317 3318 /** 3319 * srpt_remove_one - InfiniBand device removal callback function 3320 * @device: Describes a HCA. 3321 * @client_data: The value passed as the third argument to ib_set_client_data(). 3322 */ 3323 static void srpt_remove_one(struct ib_device *device, void *client_data) 3324 { 3325 struct srpt_device *sdev = client_data; 3326 int i; 3327 3328 srpt_unregister_mad_agent(sdev, sdev->device->phys_port_cnt); 3329 3330 ib_unregister_event_handler(&sdev->event_handler); 3331 3332 /* Cancel any work queued by the just unregistered IB event handler. */ 3333 for (i = 0; i < sdev->device->phys_port_cnt; i++) 3334 cancel_work_sync(&sdev->port[i].work); 3335 3336 if (sdev->cm_id) 3337 ib_destroy_cm_id(sdev->cm_id); 3338 3339 ib_set_client_data(device, &srpt_client, NULL); 3340 3341 /* 3342 * Unregistering a target must happen after destroying sdev->cm_id 3343 * such that no new SRP_LOGIN_REQ information units can arrive while 3344 * destroying the target. 3345 */ 3346 spin_lock(&srpt_dev_lock); 3347 list_del(&sdev->list); 3348 spin_unlock(&srpt_dev_lock); 3349 3350 for (i = 0; i < sdev->device->phys_port_cnt; i++) 3351 srpt_release_sport(&sdev->port[i]); 3352 3353 srpt_free_srq(sdev); 3354 3355 ib_dealloc_pd(sdev->pd); 3356 3357 srpt_sdev_put(sdev); 3358 } 3359 3360 static struct ib_client srpt_client = { 3361 .name = DRV_NAME, 3362 .add = srpt_add_one, 3363 .remove = srpt_remove_one 3364 }; 3365 3366 static int srpt_check_true(struct se_portal_group *se_tpg) 3367 { 3368 return 1; 3369 } 3370 3371 static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg) 3372 { 3373 return tpg->se_tpg_wwn->priv; 3374 } 3375 3376 static struct srpt_port_id *srpt_wwn_to_sport_id(struct se_wwn *wwn) 3377 { 3378 struct srpt_port *sport = wwn->priv; 3379 3380 if (sport->guid_id && &sport->guid_id->wwn == wwn) 3381 return sport->guid_id; 3382 if (sport->gid_id && &sport->gid_id->wwn == wwn) 3383 return sport->gid_id; 3384 WARN_ON_ONCE(true); 3385 return NULL; 3386 } 3387 3388 static char *srpt_get_fabric_wwn(struct se_portal_group *tpg) 3389 { 3390 struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg); 3391 3392 return stpg->sport_id->name; 3393 } 3394 3395 static u16 srpt_get_tag(struct se_portal_group *tpg) 3396 { 3397 return 1; 3398 } 3399 3400 static void srpt_release_cmd(struct se_cmd *se_cmd) 3401 { 3402 struct srpt_send_ioctx *ioctx = container_of(se_cmd, 3403 struct srpt_send_ioctx, cmd); 3404 struct srpt_rdma_ch *ch = ioctx->ch; 3405 struct srpt_recv_ioctx *recv_ioctx = ioctx->recv_ioctx; 3406 3407 WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE && 3408 !(ioctx->cmd.transport_state & CMD_T_ABORTED)); 3409 3410 if (recv_ioctx) { 3411 WARN_ON_ONCE(!list_empty(&recv_ioctx->wait_list)); 3412 ioctx->recv_ioctx = NULL; 3413 srpt_post_recv(ch->sport->sdev, ch, recv_ioctx); 3414 } 3415 3416 if (ioctx->n_rw_ctx) { 3417 srpt_free_rw_ctxs(ch, ioctx); 3418 ioctx->n_rw_ctx = 0; 3419 } 3420 3421 target_free_tag(se_cmd->se_sess, se_cmd); 3422 } 3423 3424 /** 3425 * srpt_close_session - forcibly close a session 3426 * @se_sess: SCSI target session. 3427 * 3428 * Callback function invoked by the TCM core to clean up sessions associated 3429 * with a node ACL when the user invokes 3430 * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id 3431 */ 3432 static void srpt_close_session(struct se_session *se_sess) 3433 { 3434 struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr; 3435 3436 srpt_disconnect_ch_sync(ch); 3437 } 3438 3439 /* Note: only used from inside debug printk's by the TCM core. */ 3440 static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd) 3441 { 3442 struct srpt_send_ioctx *ioctx; 3443 3444 ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); 3445 return ioctx->state; 3446 } 3447 3448 static int srpt_parse_guid(u64 *guid, const char *name) 3449 { 3450 u16 w[4]; 3451 int ret = -EINVAL; 3452 3453 if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4) 3454 goto out; 3455 *guid = get_unaligned_be64(w); 3456 ret = 0; 3457 out: 3458 return ret; 3459 } 3460 3461 /** 3462 * srpt_parse_i_port_id - parse an initiator port ID 3463 * @name: ASCII representation of a 128-bit initiator port ID. 3464 * @i_port_id: Binary 128-bit port ID. 3465 */ 3466 static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name) 3467 { 3468 const char *p; 3469 unsigned len, count, leading_zero_bytes; 3470 int ret; 3471 3472 p = name; 3473 if (strncasecmp(p, "0x", 2) == 0) 3474 p += 2; 3475 ret = -EINVAL; 3476 len = strlen(p); 3477 if (len % 2) 3478 goto out; 3479 count = min(len / 2, 16U); 3480 leading_zero_bytes = 16 - count; 3481 memset(i_port_id, 0, leading_zero_bytes); 3482 ret = hex2bin(i_port_id + leading_zero_bytes, p, count); 3483 3484 out: 3485 return ret; 3486 } 3487 3488 /* 3489 * configfs callback function invoked for mkdir 3490 * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id 3491 * 3492 * i_port_id must be an initiator port GUID, GID or IP address. See also the 3493 * target_alloc_session() calls in this driver. Examples of valid initiator 3494 * port IDs: 3495 * 0x0000000000000000505400fffe4a0b7b 3496 * 0000000000000000505400fffe4a0b7b 3497 * 5054:00ff:fe4a:0b7b 3498 * 192.168.122.76 3499 */ 3500 static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name) 3501 { 3502 struct sockaddr_storage sa; 3503 u64 guid; 3504 u8 i_port_id[16]; 3505 int ret; 3506 3507 ret = srpt_parse_guid(&guid, name); 3508 if (ret < 0) 3509 ret = srpt_parse_i_port_id(i_port_id, name); 3510 if (ret < 0) 3511 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL, 3512 &sa); 3513 if (ret < 0) 3514 pr_err("invalid initiator port ID %s\n", name); 3515 return ret; 3516 } 3517 3518 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item, 3519 char *page) 3520 { 3521 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3522 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3523 3524 return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rdma_size); 3525 } 3526 3527 static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item, 3528 const char *page, size_t count) 3529 { 3530 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3531 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3532 unsigned long val; 3533 int ret; 3534 3535 ret = kstrtoul(page, 0, &val); 3536 if (ret < 0) { 3537 pr_err("kstrtoul() failed with ret: %d\n", ret); 3538 return -EINVAL; 3539 } 3540 if (val > MAX_SRPT_RDMA_SIZE) { 3541 pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val, 3542 MAX_SRPT_RDMA_SIZE); 3543 return -EINVAL; 3544 } 3545 if (val < DEFAULT_MAX_RDMA_SIZE) { 3546 pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n", 3547 val, DEFAULT_MAX_RDMA_SIZE); 3548 return -EINVAL; 3549 } 3550 sport->port_attrib.srp_max_rdma_size = val; 3551 3552 return count; 3553 } 3554 3555 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item, 3556 char *page) 3557 { 3558 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3559 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3560 3561 return sysfs_emit(page, "%u\n", sport->port_attrib.srp_max_rsp_size); 3562 } 3563 3564 static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item, 3565 const char *page, size_t count) 3566 { 3567 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3568 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3569 unsigned long val; 3570 int ret; 3571 3572 ret = kstrtoul(page, 0, &val); 3573 if (ret < 0) { 3574 pr_err("kstrtoul() failed with ret: %d\n", ret); 3575 return -EINVAL; 3576 } 3577 if (val > MAX_SRPT_RSP_SIZE) { 3578 pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val, 3579 MAX_SRPT_RSP_SIZE); 3580 return -EINVAL; 3581 } 3582 if (val < MIN_MAX_RSP_SIZE) { 3583 pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val, 3584 MIN_MAX_RSP_SIZE); 3585 return -EINVAL; 3586 } 3587 sport->port_attrib.srp_max_rsp_size = val; 3588 3589 return count; 3590 } 3591 3592 static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item, 3593 char *page) 3594 { 3595 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3596 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3597 3598 return sysfs_emit(page, "%u\n", sport->port_attrib.srp_sq_size); 3599 } 3600 3601 static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item, 3602 const char *page, size_t count) 3603 { 3604 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3605 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3606 unsigned long val; 3607 int ret; 3608 3609 ret = kstrtoul(page, 0, &val); 3610 if (ret < 0) { 3611 pr_err("kstrtoul() failed with ret: %d\n", ret); 3612 return -EINVAL; 3613 } 3614 if (val > MAX_SRPT_SRQ_SIZE) { 3615 pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val, 3616 MAX_SRPT_SRQ_SIZE); 3617 return -EINVAL; 3618 } 3619 if (val < MIN_SRPT_SRQ_SIZE) { 3620 pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val, 3621 MIN_SRPT_SRQ_SIZE); 3622 return -EINVAL; 3623 } 3624 sport->port_attrib.srp_sq_size = val; 3625 3626 return count; 3627 } 3628 3629 static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item, 3630 char *page) 3631 { 3632 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3633 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3634 3635 return sysfs_emit(page, "%d\n", sport->port_attrib.use_srq); 3636 } 3637 3638 static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item, 3639 const char *page, size_t count) 3640 { 3641 struct se_portal_group *se_tpg = attrib_to_tpg(item); 3642 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3643 struct srpt_device *sdev = sport->sdev; 3644 unsigned long val; 3645 bool enabled; 3646 int ret; 3647 3648 ret = kstrtoul(page, 0, &val); 3649 if (ret < 0) 3650 return ret; 3651 if (val != !!val) 3652 return -EINVAL; 3653 3654 ret = mutex_lock_interruptible(&sdev->sdev_mutex); 3655 if (ret < 0) 3656 return ret; 3657 ret = mutex_lock_interruptible(&sport->mutex); 3658 if (ret < 0) 3659 goto unlock_sdev; 3660 enabled = sport->enabled; 3661 /* Log out all initiator systems before changing 'use_srq'. */ 3662 srpt_set_enabled(sport, false); 3663 sport->port_attrib.use_srq = val; 3664 srpt_use_srq(sdev, sport->port_attrib.use_srq); 3665 srpt_set_enabled(sport, enabled); 3666 ret = count; 3667 mutex_unlock(&sport->mutex); 3668 unlock_sdev: 3669 mutex_unlock(&sdev->sdev_mutex); 3670 3671 return ret; 3672 } 3673 3674 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size); 3675 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size); 3676 CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size); 3677 CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq); 3678 3679 static struct configfs_attribute *srpt_tpg_attrib_attrs[] = { 3680 &srpt_tpg_attrib_attr_srp_max_rdma_size, 3681 &srpt_tpg_attrib_attr_srp_max_rsp_size, 3682 &srpt_tpg_attrib_attr_srp_sq_size, 3683 &srpt_tpg_attrib_attr_use_srq, 3684 NULL, 3685 }; 3686 3687 static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr) 3688 { 3689 struct rdma_cm_id *rdma_cm_id; 3690 int ret; 3691 3692 rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler, 3693 NULL, RDMA_PS_TCP, IB_QPT_RC); 3694 if (IS_ERR(rdma_cm_id)) { 3695 pr_err("RDMA/CM ID creation failed: %pe\n", rdma_cm_id); 3696 goto out; 3697 } 3698 3699 ret = rdma_bind_addr(rdma_cm_id, listen_addr); 3700 if (ret) { 3701 char addr_str[64]; 3702 3703 snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr); 3704 pr_err("Binding RDMA/CM ID to address %s failed: %d\n", 3705 addr_str, ret); 3706 rdma_destroy_id(rdma_cm_id); 3707 rdma_cm_id = ERR_PTR(ret); 3708 goto out; 3709 } 3710 3711 ret = rdma_listen(rdma_cm_id, 128); 3712 if (ret) { 3713 pr_err("rdma_listen() failed: %d\n", ret); 3714 rdma_destroy_id(rdma_cm_id); 3715 rdma_cm_id = ERR_PTR(ret); 3716 } 3717 3718 out: 3719 return rdma_cm_id; 3720 } 3721 3722 static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page) 3723 { 3724 return sysfs_emit(page, "%d\n", rdma_cm_port); 3725 } 3726 3727 static ssize_t srpt_rdma_cm_port_store(struct config_item *item, 3728 const char *page, size_t count) 3729 { 3730 struct sockaddr_in addr4 = { .sin_family = AF_INET }; 3731 struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 }; 3732 struct rdma_cm_id *new_id = NULL; 3733 u16 val; 3734 int ret; 3735 3736 ret = kstrtou16(page, 0, &val); 3737 if (ret < 0) 3738 return ret; 3739 ret = count; 3740 if (rdma_cm_port == val) 3741 goto out; 3742 3743 if (val) { 3744 addr6.sin6_port = cpu_to_be16(val); 3745 new_id = srpt_create_rdma_id((struct sockaddr *)&addr6); 3746 if (IS_ERR(new_id)) { 3747 addr4.sin_port = cpu_to_be16(val); 3748 new_id = srpt_create_rdma_id((struct sockaddr *)&addr4); 3749 if (IS_ERR(new_id)) { 3750 ret = PTR_ERR(new_id); 3751 goto out; 3752 } 3753 } 3754 } 3755 3756 mutex_lock(&rdma_cm_mutex); 3757 rdma_cm_port = val; 3758 swap(rdma_cm_id, new_id); 3759 mutex_unlock(&rdma_cm_mutex); 3760 3761 if (new_id) 3762 rdma_destroy_id(new_id); 3763 ret = count; 3764 out: 3765 return ret; 3766 } 3767 3768 CONFIGFS_ATTR(srpt_, rdma_cm_port); 3769 3770 static struct configfs_attribute *srpt_da_attrs[] = { 3771 &srpt_attr_rdma_cm_port, 3772 NULL, 3773 }; 3774 3775 static int srpt_enable_tpg(struct se_portal_group *se_tpg, bool enable) 3776 { 3777 struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 3778 3779 mutex_lock(&sport->mutex); 3780 srpt_set_enabled(sport, enable); 3781 mutex_unlock(&sport->mutex); 3782 3783 return 0; 3784 } 3785 3786 /** 3787 * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg 3788 * @wwn: Corresponds to $driver/$port. 3789 * @name: $tpg. 3790 */ 3791 static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn, 3792 const char *name) 3793 { 3794 struct srpt_port_id *sport_id = srpt_wwn_to_sport_id(wwn); 3795 struct srpt_tpg *stpg; 3796 int res = -ENOMEM; 3797 3798 stpg = kzalloc_obj(*stpg); 3799 if (!stpg) 3800 return ERR_PTR(res); 3801 stpg->sport_id = sport_id; 3802 res = core_tpg_register(wwn, &stpg->tpg, SCSI_PROTOCOL_SRP); 3803 if (res) { 3804 kfree(stpg); 3805 return ERR_PTR(res); 3806 } 3807 3808 mutex_lock(&sport_id->mutex); 3809 list_add_tail(&stpg->entry, &sport_id->tpg_list); 3810 mutex_unlock(&sport_id->mutex); 3811 3812 return &stpg->tpg; 3813 } 3814 3815 /** 3816 * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg 3817 * @tpg: Target portal group to deregister. 3818 */ 3819 static void srpt_drop_tpg(struct se_portal_group *tpg) 3820 { 3821 struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg); 3822 struct srpt_port_id *sport_id = stpg->sport_id; 3823 struct srpt_port *sport = srpt_tpg_to_sport(tpg); 3824 3825 mutex_lock(&sport_id->mutex); 3826 list_del(&stpg->entry); 3827 mutex_unlock(&sport_id->mutex); 3828 3829 sport->enabled = false; 3830 core_tpg_deregister(tpg); 3831 kfree(stpg); 3832 } 3833 3834 /** 3835 * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port 3836 * @tf: Not used. 3837 * @group: Not used. 3838 * @name: $port. 3839 */ 3840 static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf, 3841 struct config_group *group, 3842 const char *name) 3843 { 3844 struct port_and_port_id papi = srpt_lookup_port(name); 3845 struct srpt_port *sport = papi.sport; 3846 struct srpt_port_id *port_id; 3847 3848 if (!papi.port_id) 3849 return ERR_PTR(-EINVAL); 3850 if (*papi.port_id) { 3851 /* Attempt to create a directory that already exists. */ 3852 WARN_ON_ONCE(true); 3853 return &(*papi.port_id)->wwn; 3854 } 3855 port_id = kzalloc_obj(*port_id); 3856 if (!port_id) { 3857 srpt_sdev_put(sport->sdev); 3858 return ERR_PTR(-ENOMEM); 3859 } 3860 mutex_init(&port_id->mutex); 3861 INIT_LIST_HEAD(&port_id->tpg_list); 3862 port_id->wwn.priv = sport; 3863 memcpy(port_id->name, port_id == sport->guid_id ? sport->guid_name : 3864 sport->gid_name, ARRAY_SIZE(port_id->name)); 3865 3866 *papi.port_id = port_id; 3867 3868 return &port_id->wwn; 3869 } 3870 3871 /** 3872 * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port 3873 * @wwn: $port. 3874 */ 3875 static void srpt_drop_tport(struct se_wwn *wwn) 3876 { 3877 struct srpt_port_id *port_id = container_of(wwn, typeof(*port_id), wwn); 3878 struct srpt_port *sport = wwn->priv; 3879 3880 if (sport->guid_id == port_id) 3881 sport->guid_id = NULL; 3882 else if (sport->gid_id == port_id) 3883 sport->gid_id = NULL; 3884 else 3885 WARN_ON_ONCE(true); 3886 3887 srpt_sdev_put(sport->sdev); 3888 kfree(port_id); 3889 } 3890 3891 static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf) 3892 { 3893 return sysfs_emit(buf, "\n"); 3894 } 3895 3896 CONFIGFS_ATTR_RO(srpt_wwn_, version); 3897 3898 static struct configfs_attribute *srpt_wwn_attrs[] = { 3899 &srpt_wwn_attr_version, 3900 NULL, 3901 }; 3902 3903 static const struct target_core_fabric_ops srpt_template = { 3904 .module = THIS_MODULE, 3905 .fabric_name = "srpt", 3906 .tpg_get_wwn = srpt_get_fabric_wwn, 3907 .tpg_get_tag = srpt_get_tag, 3908 .tpg_check_demo_mode_cache = srpt_check_true, 3909 .tpg_check_demo_mode_write_protect = srpt_check_true, 3910 .release_cmd = srpt_release_cmd, 3911 .check_stop_free = srpt_check_stop_free, 3912 .close_session = srpt_close_session, 3913 .sess_get_initiator_sid = NULL, 3914 .write_pending = srpt_write_pending, 3915 .get_cmd_state = srpt_get_tcm_cmd_state, 3916 .queue_data_in = srpt_queue_data_in, 3917 .queue_status = srpt_queue_status, 3918 .queue_tm_rsp = srpt_queue_tm_rsp, 3919 .aborted_task = srpt_aborted_task, 3920 /* 3921 * Setup function pointers for generic logic in 3922 * target_core_fabric_configfs.c 3923 */ 3924 .fabric_make_wwn = srpt_make_tport, 3925 .fabric_drop_wwn = srpt_drop_tport, 3926 .fabric_make_tpg = srpt_make_tpg, 3927 .fabric_enable_tpg = srpt_enable_tpg, 3928 .fabric_drop_tpg = srpt_drop_tpg, 3929 .fabric_init_nodeacl = srpt_init_nodeacl, 3930 3931 .tfc_discovery_attrs = srpt_da_attrs, 3932 .tfc_wwn_attrs = srpt_wwn_attrs, 3933 .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs, 3934 3935 .default_compl_type = TARGET_QUEUE_COMPL, 3936 .default_submit_type = TARGET_DIRECT_SUBMIT, 3937 .direct_submit_supp = 1, 3938 }; 3939 3940 /** 3941 * srpt_init_module - kernel module initialization 3942 * 3943 * Note: Since ib_register_client() registers callback functions, and since at 3944 * least one of these callback functions (srpt_add_one()) calls target core 3945 * functions, this driver must be registered with the target core before 3946 * ib_register_client() is called. 3947 */ 3948 static int __init srpt_init_module(void) 3949 { 3950 int ret; 3951 3952 ret = -EINVAL; 3953 if (srp_max_req_size < MIN_MAX_REQ_SIZE) { 3954 pr_err("invalid value %d for kernel module parameter srp_max_req_size -- must be at least %d.\n", 3955 srp_max_req_size, MIN_MAX_REQ_SIZE); 3956 goto out; 3957 } 3958 3959 if (srpt_srq_size < MIN_SRPT_SRQ_SIZE 3960 || srpt_srq_size > MAX_SRPT_SRQ_SIZE) { 3961 pr_err("invalid value %u for kernel module parameter srpt_srq_size -- must be in the range [%d..%d].\n", 3962 srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE); 3963 goto out; 3964 } 3965 3966 ret = target_register_template(&srpt_template); 3967 if (ret) 3968 goto out; 3969 3970 ret = ib_register_client(&srpt_client); 3971 if (ret) { 3972 pr_err("couldn't register IB client\n"); 3973 goto out_unregister_target; 3974 } 3975 3976 return 0; 3977 3978 out_unregister_target: 3979 target_unregister_template(&srpt_template); 3980 out: 3981 return ret; 3982 } 3983 3984 static void __exit srpt_cleanup_module(void) 3985 { 3986 if (rdma_cm_id) 3987 rdma_destroy_id(rdma_cm_id); 3988 ib_unregister_client(&srpt_client); 3989 target_unregister_template(&srpt_template); 3990 } 3991 3992 module_init(srpt_init_module); 3993 module_exit(srpt_cleanup_module); 3994