18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved. 38c2ecf20Sopenharmony_ci * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 68c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 98c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 128c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 138c2ecf20Sopenharmony_ci * conditions are met: 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 168c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 178c2ecf20Sopenharmony_ci * disclaimer. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 208c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 218c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 228c2ecf20Sopenharmony_ci * provided with the distribution. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 258c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 268c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 278c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 288c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 298c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 308c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 318c2ecf20Sopenharmony_ci * SOFTWARE. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci */ 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#include <linux/module.h> 368c2ecf20Sopenharmony_ci#include <linux/init.h> 378c2ecf20Sopenharmony_ci#include <linux/slab.h> 388c2ecf20Sopenharmony_ci#include <linux/err.h> 398c2ecf20Sopenharmony_ci#include <linux/ctype.h> 408c2ecf20Sopenharmony_ci#include <linux/kthread.h> 418c2ecf20Sopenharmony_ci#include <linux/string.h> 428c2ecf20Sopenharmony_ci#include <linux/delay.h> 438c2ecf20Sopenharmony_ci#include <linux/atomic.h> 448c2ecf20Sopenharmony_ci#include <linux/inet.h> 458c2ecf20Sopenharmony_ci#include <rdma/ib_cache.h> 468c2ecf20Sopenharmony_ci#include <scsi/scsi_proto.h> 478c2ecf20Sopenharmony_ci#include <scsi/scsi_tcq.h> 488c2ecf20Sopenharmony_ci#include <target/target_core_base.h> 498c2ecf20Sopenharmony_ci#include <target/target_core_fabric.h> 508c2ecf20Sopenharmony_ci#include "ib_srpt.h" 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* Name of this kernel module. */ 538c2ecf20Sopenharmony_ci#define DRV_NAME "ib_srpt" 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define SRPT_ID_STRING "Linux SRP target" 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci#undef pr_fmt 588c2ecf20Sopenharmony_ci#define pr_fmt(fmt) DRV_NAME " " fmt 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vu Pham and Bart Van Assche"); 618c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SCSI RDMA Protocol target driver"); 628c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL"); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/* 658c2ecf20Sopenharmony_ci * Global Variables 668c2ecf20Sopenharmony_ci */ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic u64 srpt_service_guid; 698c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */ 708c2ecf20Sopenharmony_cistatic LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */ 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE; 738c2ecf20Sopenharmony_cimodule_param(srp_max_req_size, int, 0444); 748c2ecf20Sopenharmony_ciMODULE_PARM_DESC(srp_max_req_size, 758c2ecf20Sopenharmony_ci "Maximum size of SRP request messages in bytes."); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE; 788c2ecf20Sopenharmony_cimodule_param(srpt_srq_size, int, 0444); 798c2ecf20Sopenharmony_ciMODULE_PARM_DESC(srpt_srq_size, 808c2ecf20Sopenharmony_ci "Shared receive queue (SRQ) size."); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic int srpt_get_u64_x(char *buffer, const struct kernel_param *kp) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci return sprintf(buffer, "0x%016llx\n", *(u64 *)kp->arg); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_cimodule_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid, 878c2ecf20Sopenharmony_ci 0444); 888c2ecf20Sopenharmony_ciMODULE_PARM_DESC(srpt_service_guid, 898c2ecf20Sopenharmony_ci "Using this value for ioc_guid, id_ext, and cm_listen_id instead of using the node_guid of the first HCA."); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic struct ib_client srpt_client; 928c2ecf20Sopenharmony_ci/* Protects both rdma_cm_port and rdma_cm_id. */ 938c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(rdma_cm_mutex); 948c2ecf20Sopenharmony_ci/* Port number RDMA/CM will bind to. */ 958c2ecf20Sopenharmony_cistatic u16 rdma_cm_port; 968c2ecf20Sopenharmony_cistatic struct rdma_cm_id *rdma_cm_id; 978c2ecf20Sopenharmony_cistatic void srpt_release_cmd(struct se_cmd *se_cmd); 988c2ecf20Sopenharmony_cistatic void srpt_free_ch(struct kref *kref); 998c2ecf20Sopenharmony_cistatic int srpt_queue_status(struct se_cmd *cmd); 1008c2ecf20Sopenharmony_cistatic void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc); 1018c2ecf20Sopenharmony_cistatic void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc); 1028c2ecf20Sopenharmony_cistatic void srpt_process_wait_list(struct srpt_rdma_ch *ch); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/* 1058c2ecf20Sopenharmony_ci * The only allowed channel state changes are those that change the channel 1068c2ecf20Sopenharmony_ci * state into a state with a higher numerical value. Hence the new > prev test. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_cistatic bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci unsigned long flags; 1118c2ecf20Sopenharmony_ci enum rdma_ch_state prev; 1128c2ecf20Sopenharmony_ci bool changed = false; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci spin_lock_irqsave(&ch->spinlock, flags); 1158c2ecf20Sopenharmony_ci prev = ch->state; 1168c2ecf20Sopenharmony_ci if (new > prev) { 1178c2ecf20Sopenharmony_ci ch->state = new; 1188c2ecf20Sopenharmony_ci changed = true; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ch->spinlock, flags); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return changed; 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/** 1268c2ecf20Sopenharmony_ci * srpt_event_handler - asynchronous IB event callback function 1278c2ecf20Sopenharmony_ci * @handler: IB event handler registered by ib_register_event_handler(). 1288c2ecf20Sopenharmony_ci * @event: Description of the event that occurred. 1298c2ecf20Sopenharmony_ci * 1308c2ecf20Sopenharmony_ci * Callback function called by the InfiniBand core when an asynchronous IB 1318c2ecf20Sopenharmony_ci * event occurs. This callback may occur in interrupt context. See also 1328c2ecf20Sopenharmony_ci * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand 1338c2ecf20Sopenharmony_ci * Architecture Specification. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_cistatic void srpt_event_handler(struct ib_event_handler *handler, 1368c2ecf20Sopenharmony_ci struct ib_event *event) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci struct srpt_device *sdev = 1398c2ecf20Sopenharmony_ci container_of(handler, struct srpt_device, event_handler); 1408c2ecf20Sopenharmony_ci struct srpt_port *sport; 1418c2ecf20Sopenharmony_ci u8 port_num; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci pr_debug("ASYNC event= %d on device= %s\n", event->event, 1448c2ecf20Sopenharmony_ci dev_name(&sdev->device->dev)); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci switch (event->event) { 1478c2ecf20Sopenharmony_ci case IB_EVENT_PORT_ERR: 1488c2ecf20Sopenharmony_ci port_num = event->element.port_num - 1; 1498c2ecf20Sopenharmony_ci if (port_num < sdev->device->phys_port_cnt) { 1508c2ecf20Sopenharmony_ci sport = &sdev->port[port_num]; 1518c2ecf20Sopenharmony_ci sport->lid = 0; 1528c2ecf20Sopenharmony_ci sport->sm_lid = 0; 1538c2ecf20Sopenharmony_ci } else { 1548c2ecf20Sopenharmony_ci WARN(true, "event %d: port_num %d out of range 1..%d\n", 1558c2ecf20Sopenharmony_ci event->event, port_num + 1, 1568c2ecf20Sopenharmony_ci sdev->device->phys_port_cnt); 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci break; 1598c2ecf20Sopenharmony_ci case IB_EVENT_PORT_ACTIVE: 1608c2ecf20Sopenharmony_ci case IB_EVENT_LID_CHANGE: 1618c2ecf20Sopenharmony_ci case IB_EVENT_PKEY_CHANGE: 1628c2ecf20Sopenharmony_ci case IB_EVENT_SM_CHANGE: 1638c2ecf20Sopenharmony_ci case IB_EVENT_CLIENT_REREGISTER: 1648c2ecf20Sopenharmony_ci case IB_EVENT_GID_CHANGE: 1658c2ecf20Sopenharmony_ci /* Refresh port data asynchronously. */ 1668c2ecf20Sopenharmony_ci port_num = event->element.port_num - 1; 1678c2ecf20Sopenharmony_ci if (port_num < sdev->device->phys_port_cnt) { 1688c2ecf20Sopenharmony_ci sport = &sdev->port[port_num]; 1698c2ecf20Sopenharmony_ci if (!sport->lid && !sport->sm_lid) 1708c2ecf20Sopenharmony_ci schedule_work(&sport->work); 1718c2ecf20Sopenharmony_ci } else { 1728c2ecf20Sopenharmony_ci WARN(true, "event %d: port_num %d out of range 1..%d\n", 1738c2ecf20Sopenharmony_ci event->event, port_num + 1, 1748c2ecf20Sopenharmony_ci sdev->device->phys_port_cnt); 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci break; 1778c2ecf20Sopenharmony_ci default: 1788c2ecf20Sopenharmony_ci pr_err("received unrecognized IB event %d\n", event->event); 1798c2ecf20Sopenharmony_ci break; 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/** 1848c2ecf20Sopenharmony_ci * srpt_srq_event - SRQ event callback function 1858c2ecf20Sopenharmony_ci * @event: Description of the event that occurred. 1868c2ecf20Sopenharmony_ci * @ctx: Context pointer specified at SRQ creation time. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_cistatic void srpt_srq_event(struct ib_event *event, void *ctx) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci pr_debug("SRQ event %d\n", event->event); 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic const char *get_ch_state_name(enum rdma_ch_state s) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci switch (s) { 1968c2ecf20Sopenharmony_ci case CH_CONNECTING: 1978c2ecf20Sopenharmony_ci return "connecting"; 1988c2ecf20Sopenharmony_ci case CH_LIVE: 1998c2ecf20Sopenharmony_ci return "live"; 2008c2ecf20Sopenharmony_ci case CH_DISCONNECTING: 2018c2ecf20Sopenharmony_ci return "disconnecting"; 2028c2ecf20Sopenharmony_ci case CH_DRAINING: 2038c2ecf20Sopenharmony_ci return "draining"; 2048c2ecf20Sopenharmony_ci case CH_DISCONNECTED: 2058c2ecf20Sopenharmony_ci return "disconnected"; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci return "???"; 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci/** 2118c2ecf20Sopenharmony_ci * srpt_qp_event - QP event callback function 2128c2ecf20Sopenharmony_ci * @event: Description of the event that occurred. 2138c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 2148c2ecf20Sopenharmony_ci */ 2158c2ecf20Sopenharmony_cistatic void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci pr_debug("QP event %d on ch=%p sess_name=%s-%d state=%s\n", 2188c2ecf20Sopenharmony_ci event->event, ch, ch->sess_name, ch->qp->qp_num, 2198c2ecf20Sopenharmony_ci get_ch_state_name(ch->state)); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci switch (event->event) { 2228c2ecf20Sopenharmony_ci case IB_EVENT_COMM_EST: 2238c2ecf20Sopenharmony_ci if (ch->using_rdma_cm) 2248c2ecf20Sopenharmony_ci rdma_notify(ch->rdma_cm.cm_id, event->event); 2258c2ecf20Sopenharmony_ci else 2268c2ecf20Sopenharmony_ci ib_cm_notify(ch->ib_cm.cm_id, event->event); 2278c2ecf20Sopenharmony_ci break; 2288c2ecf20Sopenharmony_ci case IB_EVENT_QP_LAST_WQE_REACHED: 2298c2ecf20Sopenharmony_ci pr_debug("%s-%d, state %s: received Last WQE event.\n", 2308c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, 2318c2ecf20Sopenharmony_ci get_ch_state_name(ch->state)); 2328c2ecf20Sopenharmony_ci break; 2338c2ecf20Sopenharmony_ci default: 2348c2ecf20Sopenharmony_ci pr_err("received unrecognized IB QP event %d\n", event->event); 2358c2ecf20Sopenharmony_ci break; 2368c2ecf20Sopenharmony_ci } 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci/** 2408c2ecf20Sopenharmony_ci * srpt_set_ioc - initialize a IOUnitInfo structure 2418c2ecf20Sopenharmony_ci * @c_list: controller list. 2428c2ecf20Sopenharmony_ci * @slot: one-based slot number. 2438c2ecf20Sopenharmony_ci * @value: four-bit value. 2448c2ecf20Sopenharmony_ci * 2458c2ecf20Sopenharmony_ci * Copies the lowest four bits of value in element slot of the array of four 2468c2ecf20Sopenharmony_ci * bit elements called c_list (controller list). The index slot is one-based. 2478c2ecf20Sopenharmony_ci */ 2488c2ecf20Sopenharmony_cistatic void srpt_set_ioc(u8 *c_list, u32 slot, u8 value) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci u16 id; 2518c2ecf20Sopenharmony_ci u8 tmp; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci id = (slot - 1) / 2; 2548c2ecf20Sopenharmony_ci if (slot & 0x1) { 2558c2ecf20Sopenharmony_ci tmp = c_list[id] & 0xf; 2568c2ecf20Sopenharmony_ci c_list[id] = (value << 4) | tmp; 2578c2ecf20Sopenharmony_ci } else { 2588c2ecf20Sopenharmony_ci tmp = c_list[id] & 0xf0; 2598c2ecf20Sopenharmony_ci c_list[id] = (value & 0xf) | tmp; 2608c2ecf20Sopenharmony_ci } 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci/** 2648c2ecf20Sopenharmony_ci * srpt_get_class_port_info - copy ClassPortInfo to a management datagram 2658c2ecf20Sopenharmony_ci * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO. 2668c2ecf20Sopenharmony_ci * 2678c2ecf20Sopenharmony_ci * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture 2688c2ecf20Sopenharmony_ci * Specification. 2698c2ecf20Sopenharmony_ci */ 2708c2ecf20Sopenharmony_cistatic void srpt_get_class_port_info(struct ib_dm_mad *mad) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct ib_class_port_info *cif; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci cif = (struct ib_class_port_info *)mad->data; 2758c2ecf20Sopenharmony_ci memset(cif, 0, sizeof(*cif)); 2768c2ecf20Sopenharmony_ci cif->base_version = 1; 2778c2ecf20Sopenharmony_ci cif->class_version = 1; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci ib_set_cpi_resp_time(cif, 20); 2808c2ecf20Sopenharmony_ci mad->mad_hdr.status = 0; 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci/** 2848c2ecf20Sopenharmony_ci * srpt_get_iou - write IOUnitInfo to a management datagram 2858c2ecf20Sopenharmony_ci * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO. 2868c2ecf20Sopenharmony_ci * 2878c2ecf20Sopenharmony_ci * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture 2888c2ecf20Sopenharmony_ci * Specification. See also section B.7, table B.6 in the SRP r16a document. 2898c2ecf20Sopenharmony_ci */ 2908c2ecf20Sopenharmony_cistatic void srpt_get_iou(struct ib_dm_mad *mad) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci struct ib_dm_iou_info *ioui; 2938c2ecf20Sopenharmony_ci u8 slot; 2948c2ecf20Sopenharmony_ci int i; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci ioui = (struct ib_dm_iou_info *)mad->data; 2978c2ecf20Sopenharmony_ci ioui->change_id = cpu_to_be16(1); 2988c2ecf20Sopenharmony_ci ioui->max_controllers = 16; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci /* set present for slot 1 and empty for the rest */ 3018c2ecf20Sopenharmony_ci srpt_set_ioc(ioui->controller_list, 1, 1); 3028c2ecf20Sopenharmony_ci for (i = 1, slot = 2; i < 16; i++, slot++) 3038c2ecf20Sopenharmony_ci srpt_set_ioc(ioui->controller_list, slot, 0); 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci mad->mad_hdr.status = 0; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci/** 3098c2ecf20Sopenharmony_ci * srpt_get_ioc - write IOControllerprofile to a management datagram 3108c2ecf20Sopenharmony_ci * @sport: HCA port through which the MAD has been received. 3118c2ecf20Sopenharmony_ci * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query. 3128c2ecf20Sopenharmony_ci * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE. 3138c2ecf20Sopenharmony_ci * 3148c2ecf20Sopenharmony_ci * See also section 16.3.3.4 IOControllerProfile in the InfiniBand 3158c2ecf20Sopenharmony_ci * Architecture Specification. See also section B.7, table B.7 in the SRP 3168c2ecf20Sopenharmony_ci * r16a document. 3178c2ecf20Sopenharmony_ci */ 3188c2ecf20Sopenharmony_cistatic void srpt_get_ioc(struct srpt_port *sport, u32 slot, 3198c2ecf20Sopenharmony_ci struct ib_dm_mad *mad) 3208c2ecf20Sopenharmony_ci{ 3218c2ecf20Sopenharmony_ci struct srpt_device *sdev = sport->sdev; 3228c2ecf20Sopenharmony_ci struct ib_dm_ioc_profile *iocp; 3238c2ecf20Sopenharmony_ci int send_queue_depth; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci iocp = (struct ib_dm_ioc_profile *)mad->data; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci if (!slot || slot > 16) { 3288c2ecf20Sopenharmony_ci mad->mad_hdr.status 3298c2ecf20Sopenharmony_ci = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); 3308c2ecf20Sopenharmony_ci return; 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci if (slot > 2) { 3348c2ecf20Sopenharmony_ci mad->mad_hdr.status 3358c2ecf20Sopenharmony_ci = cpu_to_be16(DM_MAD_STATUS_NO_IOC); 3368c2ecf20Sopenharmony_ci return; 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci if (sdev->use_srq) 3408c2ecf20Sopenharmony_ci send_queue_depth = sdev->srq_size; 3418c2ecf20Sopenharmony_ci else 3428c2ecf20Sopenharmony_ci send_queue_depth = min(MAX_SRPT_RQ_SIZE, 3438c2ecf20Sopenharmony_ci sdev->device->attrs.max_qp_wr); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci memset(iocp, 0, sizeof(*iocp)); 3468c2ecf20Sopenharmony_ci strcpy(iocp->id_string, SRPT_ID_STRING); 3478c2ecf20Sopenharmony_ci iocp->guid = cpu_to_be64(srpt_service_guid); 3488c2ecf20Sopenharmony_ci iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); 3498c2ecf20Sopenharmony_ci iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id); 3508c2ecf20Sopenharmony_ci iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver); 3518c2ecf20Sopenharmony_ci iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id); 3528c2ecf20Sopenharmony_ci iocp->subsys_device_id = 0x0; 3538c2ecf20Sopenharmony_ci iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS); 3548c2ecf20Sopenharmony_ci iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS); 3558c2ecf20Sopenharmony_ci iocp->protocol = cpu_to_be16(SRP_PROTOCOL); 3568c2ecf20Sopenharmony_ci iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION); 3578c2ecf20Sopenharmony_ci iocp->send_queue_depth = cpu_to_be16(send_queue_depth); 3588c2ecf20Sopenharmony_ci iocp->rdma_read_depth = 4; 3598c2ecf20Sopenharmony_ci iocp->send_size = cpu_to_be32(srp_max_req_size); 3608c2ecf20Sopenharmony_ci iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size, 3618c2ecf20Sopenharmony_ci 1U << 24)); 3628c2ecf20Sopenharmony_ci iocp->num_svc_entries = 1; 3638c2ecf20Sopenharmony_ci iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC | 3648c2ecf20Sopenharmony_ci SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC; 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci mad->mad_hdr.status = 0; 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci/** 3708c2ecf20Sopenharmony_ci * srpt_get_svc_entries - write ServiceEntries to a management datagram 3718c2ecf20Sopenharmony_ci * @ioc_guid: I/O controller GUID to use in reply. 3728c2ecf20Sopenharmony_ci * @slot: I/O controller number. 3738c2ecf20Sopenharmony_ci * @hi: End of the range of service entries to be specified in the reply. 3748c2ecf20Sopenharmony_ci * @lo: Start of the range of service entries to be specified in the reply.. 3758c2ecf20Sopenharmony_ci * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES. 3768c2ecf20Sopenharmony_ci * 3778c2ecf20Sopenharmony_ci * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture 3788c2ecf20Sopenharmony_ci * Specification. See also section B.7, table B.8 in the SRP r16a document. 3798c2ecf20Sopenharmony_ci */ 3808c2ecf20Sopenharmony_cistatic void srpt_get_svc_entries(u64 ioc_guid, 3818c2ecf20Sopenharmony_ci u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad) 3828c2ecf20Sopenharmony_ci{ 3838c2ecf20Sopenharmony_ci struct ib_dm_svc_entries *svc_entries; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci WARN_ON(!ioc_guid); 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci if (!slot || slot > 16) { 3888c2ecf20Sopenharmony_ci mad->mad_hdr.status 3898c2ecf20Sopenharmony_ci = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD); 3908c2ecf20Sopenharmony_ci return; 3918c2ecf20Sopenharmony_ci } 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci if (slot > 2 || lo > hi || hi > 1) { 3948c2ecf20Sopenharmony_ci mad->mad_hdr.status 3958c2ecf20Sopenharmony_ci = cpu_to_be16(DM_MAD_STATUS_NO_IOC); 3968c2ecf20Sopenharmony_ci return; 3978c2ecf20Sopenharmony_ci } 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci svc_entries = (struct ib_dm_svc_entries *)mad->data; 4008c2ecf20Sopenharmony_ci memset(svc_entries, 0, sizeof(*svc_entries)); 4018c2ecf20Sopenharmony_ci svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid); 4028c2ecf20Sopenharmony_ci snprintf(svc_entries->service_entries[0].name, 4038c2ecf20Sopenharmony_ci sizeof(svc_entries->service_entries[0].name), 4048c2ecf20Sopenharmony_ci "%s%016llx", 4058c2ecf20Sopenharmony_ci SRP_SERVICE_NAME_PREFIX, 4068c2ecf20Sopenharmony_ci ioc_guid); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci mad->mad_hdr.status = 0; 4098c2ecf20Sopenharmony_ci} 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci/** 4128c2ecf20Sopenharmony_ci * srpt_mgmt_method_get - process a received management datagram 4138c2ecf20Sopenharmony_ci * @sp: HCA port through which the MAD has been received. 4148c2ecf20Sopenharmony_ci * @rq_mad: received MAD. 4158c2ecf20Sopenharmony_ci * @rsp_mad: response MAD. 4168c2ecf20Sopenharmony_ci */ 4178c2ecf20Sopenharmony_cistatic void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad, 4188c2ecf20Sopenharmony_ci struct ib_dm_mad *rsp_mad) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci u16 attr_id; 4218c2ecf20Sopenharmony_ci u32 slot; 4228c2ecf20Sopenharmony_ci u8 hi, lo; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id); 4258c2ecf20Sopenharmony_ci switch (attr_id) { 4268c2ecf20Sopenharmony_ci case DM_ATTR_CLASS_PORT_INFO: 4278c2ecf20Sopenharmony_ci srpt_get_class_port_info(rsp_mad); 4288c2ecf20Sopenharmony_ci break; 4298c2ecf20Sopenharmony_ci case DM_ATTR_IOU_INFO: 4308c2ecf20Sopenharmony_ci srpt_get_iou(rsp_mad); 4318c2ecf20Sopenharmony_ci break; 4328c2ecf20Sopenharmony_ci case DM_ATTR_IOC_PROFILE: 4338c2ecf20Sopenharmony_ci slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); 4348c2ecf20Sopenharmony_ci srpt_get_ioc(sp, slot, rsp_mad); 4358c2ecf20Sopenharmony_ci break; 4368c2ecf20Sopenharmony_ci case DM_ATTR_SVC_ENTRIES: 4378c2ecf20Sopenharmony_ci slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod); 4388c2ecf20Sopenharmony_ci hi = (u8) ((slot >> 8) & 0xff); 4398c2ecf20Sopenharmony_ci lo = (u8) (slot & 0xff); 4408c2ecf20Sopenharmony_ci slot = (u16) ((slot >> 16) & 0xffff); 4418c2ecf20Sopenharmony_ci srpt_get_svc_entries(srpt_service_guid, 4428c2ecf20Sopenharmony_ci slot, hi, lo, rsp_mad); 4438c2ecf20Sopenharmony_ci break; 4448c2ecf20Sopenharmony_ci default: 4458c2ecf20Sopenharmony_ci rsp_mad->mad_hdr.status = 4468c2ecf20Sopenharmony_ci cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); 4478c2ecf20Sopenharmony_ci break; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci} 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci/** 4528c2ecf20Sopenharmony_ci * srpt_mad_send_handler - MAD send completion callback 4538c2ecf20Sopenharmony_ci * @mad_agent: Return value of ib_register_mad_agent(). 4548c2ecf20Sopenharmony_ci * @mad_wc: Work completion reporting that the MAD has been sent. 4558c2ecf20Sopenharmony_ci */ 4568c2ecf20Sopenharmony_cistatic void srpt_mad_send_handler(struct ib_mad_agent *mad_agent, 4578c2ecf20Sopenharmony_ci struct ib_mad_send_wc *mad_wc) 4588c2ecf20Sopenharmony_ci{ 4598c2ecf20Sopenharmony_ci rdma_destroy_ah(mad_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE); 4608c2ecf20Sopenharmony_ci ib_free_send_mad(mad_wc->send_buf); 4618c2ecf20Sopenharmony_ci} 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci/** 4648c2ecf20Sopenharmony_ci * srpt_mad_recv_handler - MAD reception callback function 4658c2ecf20Sopenharmony_ci * @mad_agent: Return value of ib_register_mad_agent(). 4668c2ecf20Sopenharmony_ci * @send_buf: Not used. 4678c2ecf20Sopenharmony_ci * @mad_wc: Work completion reporting that a MAD has been received. 4688c2ecf20Sopenharmony_ci */ 4698c2ecf20Sopenharmony_cistatic void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent, 4708c2ecf20Sopenharmony_ci struct ib_mad_send_buf *send_buf, 4718c2ecf20Sopenharmony_ci struct ib_mad_recv_wc *mad_wc) 4728c2ecf20Sopenharmony_ci{ 4738c2ecf20Sopenharmony_ci struct srpt_port *sport = (struct srpt_port *)mad_agent->context; 4748c2ecf20Sopenharmony_ci struct ib_ah *ah; 4758c2ecf20Sopenharmony_ci struct ib_mad_send_buf *rsp; 4768c2ecf20Sopenharmony_ci struct ib_dm_mad *dm_mad; 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci if (!mad_wc || !mad_wc->recv_buf.mad) 4798c2ecf20Sopenharmony_ci return; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc, 4828c2ecf20Sopenharmony_ci mad_wc->recv_buf.grh, mad_agent->port_num); 4838c2ecf20Sopenharmony_ci if (IS_ERR(ah)) 4848c2ecf20Sopenharmony_ci goto err; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR); 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp, 4898c2ecf20Sopenharmony_ci mad_wc->wc->pkey_index, 0, 4908c2ecf20Sopenharmony_ci IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA, 4918c2ecf20Sopenharmony_ci GFP_KERNEL, 4928c2ecf20Sopenharmony_ci IB_MGMT_BASE_VERSION); 4938c2ecf20Sopenharmony_ci if (IS_ERR(rsp)) 4948c2ecf20Sopenharmony_ci goto err_rsp; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci rsp->ah = ah; 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci dm_mad = rsp->mad; 4998c2ecf20Sopenharmony_ci memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad)); 5008c2ecf20Sopenharmony_ci dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP; 5018c2ecf20Sopenharmony_ci dm_mad->mad_hdr.status = 0; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci switch (mad_wc->recv_buf.mad->mad_hdr.method) { 5048c2ecf20Sopenharmony_ci case IB_MGMT_METHOD_GET: 5058c2ecf20Sopenharmony_ci srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad); 5068c2ecf20Sopenharmony_ci break; 5078c2ecf20Sopenharmony_ci case IB_MGMT_METHOD_SET: 5088c2ecf20Sopenharmony_ci dm_mad->mad_hdr.status = 5098c2ecf20Sopenharmony_ci cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR); 5108c2ecf20Sopenharmony_ci break; 5118c2ecf20Sopenharmony_ci default: 5128c2ecf20Sopenharmony_ci dm_mad->mad_hdr.status = 5138c2ecf20Sopenharmony_ci cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD); 5148c2ecf20Sopenharmony_ci break; 5158c2ecf20Sopenharmony_ci } 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci if (!ib_post_send_mad(rsp, NULL)) { 5188c2ecf20Sopenharmony_ci ib_free_recv_mad(mad_wc); 5198c2ecf20Sopenharmony_ci /* will destroy_ah & free_send_mad in send completion */ 5208c2ecf20Sopenharmony_ci return; 5218c2ecf20Sopenharmony_ci } 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci ib_free_send_mad(rsp); 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cierr_rsp: 5268c2ecf20Sopenharmony_ci rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE); 5278c2ecf20Sopenharmony_cierr: 5288c2ecf20Sopenharmony_ci ib_free_recv_mad(mad_wc); 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_cistatic int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid) 5328c2ecf20Sopenharmony_ci{ 5338c2ecf20Sopenharmony_ci const __be16 *g = (const __be16 *)guid; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci return snprintf(buf, size, "%04x:%04x:%04x:%04x", 5368c2ecf20Sopenharmony_ci be16_to_cpu(g[0]), be16_to_cpu(g[1]), 5378c2ecf20Sopenharmony_ci be16_to_cpu(g[2]), be16_to_cpu(g[3])); 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci/** 5418c2ecf20Sopenharmony_ci * srpt_refresh_port - configure a HCA port 5428c2ecf20Sopenharmony_ci * @sport: SRPT HCA port. 5438c2ecf20Sopenharmony_ci * 5448c2ecf20Sopenharmony_ci * Enable InfiniBand management datagram processing, update the cached sm_lid, 5458c2ecf20Sopenharmony_ci * lid and gid values, and register a callback function for processing MADs 5468c2ecf20Sopenharmony_ci * on the specified port. 5478c2ecf20Sopenharmony_ci * 5488c2ecf20Sopenharmony_ci * Note: It is safe to call this function more than once for the same port. 5498c2ecf20Sopenharmony_ci */ 5508c2ecf20Sopenharmony_cistatic int srpt_refresh_port(struct srpt_port *sport) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci struct ib_mad_agent *mad_agent; 5538c2ecf20Sopenharmony_ci struct ib_mad_reg_req reg_req; 5548c2ecf20Sopenharmony_ci struct ib_port_modify port_modify; 5558c2ecf20Sopenharmony_ci struct ib_port_attr port_attr; 5568c2ecf20Sopenharmony_ci int ret; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci ret = ib_query_port(sport->sdev->device, sport->port, &port_attr); 5598c2ecf20Sopenharmony_ci if (ret) 5608c2ecf20Sopenharmony_ci return ret; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci sport->sm_lid = port_attr.sm_lid; 5638c2ecf20Sopenharmony_ci sport->lid = port_attr.lid; 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci ret = rdma_query_gid(sport->sdev->device, sport->port, 0, &sport->gid); 5668c2ecf20Sopenharmony_ci if (ret) 5678c2ecf20Sopenharmony_ci return ret; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci srpt_format_guid(sport->guid_name, ARRAY_SIZE(sport->guid_name), 5708c2ecf20Sopenharmony_ci &sport->gid.global.interface_id); 5718c2ecf20Sopenharmony_ci snprintf(sport->gid_name, ARRAY_SIZE(sport->gid_name), 5728c2ecf20Sopenharmony_ci "0x%016llx%016llx", 5738c2ecf20Sopenharmony_ci be64_to_cpu(sport->gid.global.subnet_prefix), 5748c2ecf20Sopenharmony_ci be64_to_cpu(sport->gid.global.interface_id)); 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci if (rdma_protocol_iwarp(sport->sdev->device, sport->port)) 5778c2ecf20Sopenharmony_ci return 0; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci memset(&port_modify, 0, sizeof(port_modify)); 5808c2ecf20Sopenharmony_ci port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; 5818c2ecf20Sopenharmony_ci port_modify.clr_port_cap_mask = 0; 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify); 5848c2ecf20Sopenharmony_ci if (ret) { 5858c2ecf20Sopenharmony_ci pr_warn("%s-%d: enabling device management failed (%d). Note: this is expected if SR-IOV is enabled.\n", 5868c2ecf20Sopenharmony_ci dev_name(&sport->sdev->device->dev), sport->port, ret); 5878c2ecf20Sopenharmony_ci return 0; 5888c2ecf20Sopenharmony_ci } 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci if (!sport->mad_agent) { 5918c2ecf20Sopenharmony_ci memset(®_req, 0, sizeof(reg_req)); 5928c2ecf20Sopenharmony_ci reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT; 5938c2ecf20Sopenharmony_ci reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION; 5948c2ecf20Sopenharmony_ci set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask); 5958c2ecf20Sopenharmony_ci set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask); 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci mad_agent = ib_register_mad_agent(sport->sdev->device, 5988c2ecf20Sopenharmony_ci sport->port, 5998c2ecf20Sopenharmony_ci IB_QPT_GSI, 6008c2ecf20Sopenharmony_ci ®_req, 0, 6018c2ecf20Sopenharmony_ci srpt_mad_send_handler, 6028c2ecf20Sopenharmony_ci srpt_mad_recv_handler, 6038c2ecf20Sopenharmony_ci sport, 0); 6048c2ecf20Sopenharmony_ci if (IS_ERR(mad_agent)) { 6058c2ecf20Sopenharmony_ci pr_err("%s-%d: MAD agent registration failed (%ld). Note: this is expected if SR-IOV is enabled.\n", 6068c2ecf20Sopenharmony_ci dev_name(&sport->sdev->device->dev), sport->port, 6078c2ecf20Sopenharmony_ci PTR_ERR(mad_agent)); 6088c2ecf20Sopenharmony_ci sport->mad_agent = NULL; 6098c2ecf20Sopenharmony_ci memset(&port_modify, 0, sizeof(port_modify)); 6108c2ecf20Sopenharmony_ci port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP; 6118c2ecf20Sopenharmony_ci ib_modify_port(sport->sdev->device, sport->port, 0, 6128c2ecf20Sopenharmony_ci &port_modify); 6138c2ecf20Sopenharmony_ci return 0; 6148c2ecf20Sopenharmony_ci } 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci sport->mad_agent = mad_agent; 6178c2ecf20Sopenharmony_ci } 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci return 0; 6208c2ecf20Sopenharmony_ci} 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci/** 6238c2ecf20Sopenharmony_ci * srpt_unregister_mad_agent - unregister MAD callback functions 6248c2ecf20Sopenharmony_ci * @sdev: SRPT HCA pointer. 6258c2ecf20Sopenharmony_ci * @port_cnt: number of ports with registered MAD 6268c2ecf20Sopenharmony_ci * 6278c2ecf20Sopenharmony_ci * Note: It is safe to call this function more than once for the same device. 6288c2ecf20Sopenharmony_ci */ 6298c2ecf20Sopenharmony_cistatic void srpt_unregister_mad_agent(struct srpt_device *sdev, int port_cnt) 6308c2ecf20Sopenharmony_ci{ 6318c2ecf20Sopenharmony_ci struct ib_port_modify port_modify = { 6328c2ecf20Sopenharmony_ci .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP, 6338c2ecf20Sopenharmony_ci }; 6348c2ecf20Sopenharmony_ci struct srpt_port *sport; 6358c2ecf20Sopenharmony_ci int i; 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci for (i = 1; i <= port_cnt; i++) { 6388c2ecf20Sopenharmony_ci sport = &sdev->port[i - 1]; 6398c2ecf20Sopenharmony_ci WARN_ON(sport->port != i); 6408c2ecf20Sopenharmony_ci if (sport->mad_agent) { 6418c2ecf20Sopenharmony_ci ib_modify_port(sdev->device, i, 0, &port_modify); 6428c2ecf20Sopenharmony_ci ib_unregister_mad_agent(sport->mad_agent); 6438c2ecf20Sopenharmony_ci sport->mad_agent = NULL; 6448c2ecf20Sopenharmony_ci } 6458c2ecf20Sopenharmony_ci } 6468c2ecf20Sopenharmony_ci} 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci/** 6498c2ecf20Sopenharmony_ci * srpt_alloc_ioctx - allocate a SRPT I/O context structure 6508c2ecf20Sopenharmony_ci * @sdev: SRPT HCA pointer. 6518c2ecf20Sopenharmony_ci * @ioctx_size: I/O context size. 6528c2ecf20Sopenharmony_ci * @buf_cache: I/O buffer cache. 6538c2ecf20Sopenharmony_ci * @dir: DMA data direction. 6548c2ecf20Sopenharmony_ci */ 6558c2ecf20Sopenharmony_cistatic struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev, 6568c2ecf20Sopenharmony_ci int ioctx_size, 6578c2ecf20Sopenharmony_ci struct kmem_cache *buf_cache, 6588c2ecf20Sopenharmony_ci enum dma_data_direction dir) 6598c2ecf20Sopenharmony_ci{ 6608c2ecf20Sopenharmony_ci struct srpt_ioctx *ioctx; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci ioctx = kzalloc(ioctx_size, GFP_KERNEL); 6638c2ecf20Sopenharmony_ci if (!ioctx) 6648c2ecf20Sopenharmony_ci goto err; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci ioctx->buf = kmem_cache_alloc(buf_cache, GFP_KERNEL); 6678c2ecf20Sopenharmony_ci if (!ioctx->buf) 6688c2ecf20Sopenharmony_ci goto err_free_ioctx; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, 6718c2ecf20Sopenharmony_ci kmem_cache_size(buf_cache), dir); 6728c2ecf20Sopenharmony_ci if (ib_dma_mapping_error(sdev->device, ioctx->dma)) 6738c2ecf20Sopenharmony_ci goto err_free_buf; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci return ioctx; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_cierr_free_buf: 6788c2ecf20Sopenharmony_ci kmem_cache_free(buf_cache, ioctx->buf); 6798c2ecf20Sopenharmony_cierr_free_ioctx: 6808c2ecf20Sopenharmony_ci kfree(ioctx); 6818c2ecf20Sopenharmony_cierr: 6828c2ecf20Sopenharmony_ci return NULL; 6838c2ecf20Sopenharmony_ci} 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci/** 6868c2ecf20Sopenharmony_ci * srpt_free_ioctx - free a SRPT I/O context structure 6878c2ecf20Sopenharmony_ci * @sdev: SRPT HCA pointer. 6888c2ecf20Sopenharmony_ci * @ioctx: I/O context pointer. 6898c2ecf20Sopenharmony_ci * @buf_cache: I/O buffer cache. 6908c2ecf20Sopenharmony_ci * @dir: DMA data direction. 6918c2ecf20Sopenharmony_ci */ 6928c2ecf20Sopenharmony_cistatic void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx, 6938c2ecf20Sopenharmony_ci struct kmem_cache *buf_cache, 6948c2ecf20Sopenharmony_ci enum dma_data_direction dir) 6958c2ecf20Sopenharmony_ci{ 6968c2ecf20Sopenharmony_ci if (!ioctx) 6978c2ecf20Sopenharmony_ci return; 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci ib_dma_unmap_single(sdev->device, ioctx->dma, 7008c2ecf20Sopenharmony_ci kmem_cache_size(buf_cache), dir); 7018c2ecf20Sopenharmony_ci kmem_cache_free(buf_cache, ioctx->buf); 7028c2ecf20Sopenharmony_ci kfree(ioctx); 7038c2ecf20Sopenharmony_ci} 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci/** 7068c2ecf20Sopenharmony_ci * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures 7078c2ecf20Sopenharmony_ci * @sdev: Device to allocate the I/O context ring for. 7088c2ecf20Sopenharmony_ci * @ring_size: Number of elements in the I/O context ring. 7098c2ecf20Sopenharmony_ci * @ioctx_size: I/O context size. 7108c2ecf20Sopenharmony_ci * @buf_cache: I/O buffer cache. 7118c2ecf20Sopenharmony_ci * @alignment_offset: Offset in each ring buffer at which the SRP information 7128c2ecf20Sopenharmony_ci * unit starts. 7138c2ecf20Sopenharmony_ci * @dir: DMA data direction. 7148c2ecf20Sopenharmony_ci */ 7158c2ecf20Sopenharmony_cistatic struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev, 7168c2ecf20Sopenharmony_ci int ring_size, int ioctx_size, 7178c2ecf20Sopenharmony_ci struct kmem_cache *buf_cache, 7188c2ecf20Sopenharmony_ci int alignment_offset, 7198c2ecf20Sopenharmony_ci enum dma_data_direction dir) 7208c2ecf20Sopenharmony_ci{ 7218c2ecf20Sopenharmony_ci struct srpt_ioctx **ring; 7228c2ecf20Sopenharmony_ci int i; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx) && 7258c2ecf20Sopenharmony_ci ioctx_size != sizeof(struct srpt_send_ioctx)); 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci ring = kvmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL); 7288c2ecf20Sopenharmony_ci if (!ring) 7298c2ecf20Sopenharmony_ci goto out; 7308c2ecf20Sopenharmony_ci for (i = 0; i < ring_size; ++i) { 7318c2ecf20Sopenharmony_ci ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, buf_cache, dir); 7328c2ecf20Sopenharmony_ci if (!ring[i]) 7338c2ecf20Sopenharmony_ci goto err; 7348c2ecf20Sopenharmony_ci ring[i]->index = i; 7358c2ecf20Sopenharmony_ci ring[i]->offset = alignment_offset; 7368c2ecf20Sopenharmony_ci } 7378c2ecf20Sopenharmony_ci goto out; 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_cierr: 7408c2ecf20Sopenharmony_ci while (--i >= 0) 7418c2ecf20Sopenharmony_ci srpt_free_ioctx(sdev, ring[i], buf_cache, dir); 7428c2ecf20Sopenharmony_ci kvfree(ring); 7438c2ecf20Sopenharmony_ci ring = NULL; 7448c2ecf20Sopenharmony_ciout: 7458c2ecf20Sopenharmony_ci return ring; 7468c2ecf20Sopenharmony_ci} 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci/** 7498c2ecf20Sopenharmony_ci * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures 7508c2ecf20Sopenharmony_ci * @ioctx_ring: I/O context ring to be freed. 7518c2ecf20Sopenharmony_ci * @sdev: SRPT HCA pointer. 7528c2ecf20Sopenharmony_ci * @ring_size: Number of ring elements. 7538c2ecf20Sopenharmony_ci * @buf_cache: I/O buffer cache. 7548c2ecf20Sopenharmony_ci * @dir: DMA data direction. 7558c2ecf20Sopenharmony_ci */ 7568c2ecf20Sopenharmony_cistatic void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring, 7578c2ecf20Sopenharmony_ci struct srpt_device *sdev, int ring_size, 7588c2ecf20Sopenharmony_ci struct kmem_cache *buf_cache, 7598c2ecf20Sopenharmony_ci enum dma_data_direction dir) 7608c2ecf20Sopenharmony_ci{ 7618c2ecf20Sopenharmony_ci int i; 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci if (!ioctx_ring) 7648c2ecf20Sopenharmony_ci return; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci for (i = 0; i < ring_size; ++i) 7678c2ecf20Sopenharmony_ci srpt_free_ioctx(sdev, ioctx_ring[i], buf_cache, dir); 7688c2ecf20Sopenharmony_ci kvfree(ioctx_ring); 7698c2ecf20Sopenharmony_ci} 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci/** 7728c2ecf20Sopenharmony_ci * srpt_set_cmd_state - set the state of a SCSI command 7738c2ecf20Sopenharmony_ci * @ioctx: Send I/O context. 7748c2ecf20Sopenharmony_ci * @new: New I/O context state. 7758c2ecf20Sopenharmony_ci * 7768c2ecf20Sopenharmony_ci * Does not modify the state of aborted commands. Returns the previous command 7778c2ecf20Sopenharmony_ci * state. 7788c2ecf20Sopenharmony_ci */ 7798c2ecf20Sopenharmony_cistatic enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx, 7808c2ecf20Sopenharmony_ci enum srpt_command_state new) 7818c2ecf20Sopenharmony_ci{ 7828c2ecf20Sopenharmony_ci enum srpt_command_state previous; 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci previous = ioctx->state; 7858c2ecf20Sopenharmony_ci if (previous != SRPT_STATE_DONE) 7868c2ecf20Sopenharmony_ci ioctx->state = new; 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci return previous; 7898c2ecf20Sopenharmony_ci} 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci/** 7928c2ecf20Sopenharmony_ci * srpt_test_and_set_cmd_state - test and set the state of a command 7938c2ecf20Sopenharmony_ci * @ioctx: Send I/O context. 7948c2ecf20Sopenharmony_ci * @old: Current I/O context state. 7958c2ecf20Sopenharmony_ci * @new: New I/O context state. 7968c2ecf20Sopenharmony_ci * 7978c2ecf20Sopenharmony_ci * Returns true if and only if the previous command state was equal to 'old'. 7988c2ecf20Sopenharmony_ci */ 7998c2ecf20Sopenharmony_cistatic bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx, 8008c2ecf20Sopenharmony_ci enum srpt_command_state old, 8018c2ecf20Sopenharmony_ci enum srpt_command_state new) 8028c2ecf20Sopenharmony_ci{ 8038c2ecf20Sopenharmony_ci enum srpt_command_state previous; 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci WARN_ON(!ioctx); 8068c2ecf20Sopenharmony_ci WARN_ON(old == SRPT_STATE_DONE); 8078c2ecf20Sopenharmony_ci WARN_ON(new == SRPT_STATE_NEW); 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci previous = ioctx->state; 8108c2ecf20Sopenharmony_ci if (previous == old) 8118c2ecf20Sopenharmony_ci ioctx->state = new; 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci return previous == old; 8148c2ecf20Sopenharmony_ci} 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci/** 8178c2ecf20Sopenharmony_ci * srpt_post_recv - post an IB receive request 8188c2ecf20Sopenharmony_ci * @sdev: SRPT HCA pointer. 8198c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 8208c2ecf20Sopenharmony_ci * @ioctx: Receive I/O context pointer. 8218c2ecf20Sopenharmony_ci */ 8228c2ecf20Sopenharmony_cistatic int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch, 8238c2ecf20Sopenharmony_ci struct srpt_recv_ioctx *ioctx) 8248c2ecf20Sopenharmony_ci{ 8258c2ecf20Sopenharmony_ci struct ib_sge list; 8268c2ecf20Sopenharmony_ci struct ib_recv_wr wr; 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci BUG_ON(!sdev); 8298c2ecf20Sopenharmony_ci list.addr = ioctx->ioctx.dma + ioctx->ioctx.offset; 8308c2ecf20Sopenharmony_ci list.length = srp_max_req_size; 8318c2ecf20Sopenharmony_ci list.lkey = sdev->lkey; 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ci ioctx->ioctx.cqe.done = srpt_recv_done; 8348c2ecf20Sopenharmony_ci wr.wr_cqe = &ioctx->ioctx.cqe; 8358c2ecf20Sopenharmony_ci wr.next = NULL; 8368c2ecf20Sopenharmony_ci wr.sg_list = &list; 8378c2ecf20Sopenharmony_ci wr.num_sge = 1; 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci if (sdev->use_srq) 8408c2ecf20Sopenharmony_ci return ib_post_srq_recv(sdev->srq, &wr, NULL); 8418c2ecf20Sopenharmony_ci else 8428c2ecf20Sopenharmony_ci return ib_post_recv(ch->qp, &wr, NULL); 8438c2ecf20Sopenharmony_ci} 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci/** 8468c2ecf20Sopenharmony_ci * srpt_zerolength_write - perform a zero-length RDMA write 8478c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 8488c2ecf20Sopenharmony_ci * 8498c2ecf20Sopenharmony_ci * A quote from the InfiniBand specification: C9-88: For an HCA responder 8508c2ecf20Sopenharmony_ci * using Reliable Connection service, for each zero-length RDMA READ or WRITE 8518c2ecf20Sopenharmony_ci * request, the R_Key shall not be validated, even if the request includes 8528c2ecf20Sopenharmony_ci * Immediate data. 8538c2ecf20Sopenharmony_ci */ 8548c2ecf20Sopenharmony_cistatic int srpt_zerolength_write(struct srpt_rdma_ch *ch) 8558c2ecf20Sopenharmony_ci{ 8568c2ecf20Sopenharmony_ci struct ib_rdma_wr wr = { 8578c2ecf20Sopenharmony_ci .wr = { 8588c2ecf20Sopenharmony_ci .next = NULL, 8598c2ecf20Sopenharmony_ci { .wr_cqe = &ch->zw_cqe, }, 8608c2ecf20Sopenharmony_ci .opcode = IB_WR_RDMA_WRITE, 8618c2ecf20Sopenharmony_ci .send_flags = IB_SEND_SIGNALED, 8628c2ecf20Sopenharmony_ci } 8638c2ecf20Sopenharmony_ci }; 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci pr_debug("%s-%d: queued zerolength write\n", ch->sess_name, 8668c2ecf20Sopenharmony_ci ch->qp->qp_num); 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci return ib_post_send(ch->qp, &wr.wr, NULL); 8698c2ecf20Sopenharmony_ci} 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_cistatic void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc) 8728c2ecf20Sopenharmony_ci{ 8738c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = wc->qp->qp_context; 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num, 8768c2ecf20Sopenharmony_ci wc->status); 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci if (wc->status == IB_WC_SUCCESS) { 8798c2ecf20Sopenharmony_ci srpt_process_wait_list(ch); 8808c2ecf20Sopenharmony_ci } else { 8818c2ecf20Sopenharmony_ci if (srpt_set_ch_state(ch, CH_DISCONNECTED)) 8828c2ecf20Sopenharmony_ci schedule_work(&ch->release_work); 8838c2ecf20Sopenharmony_ci else 8848c2ecf20Sopenharmony_ci pr_debug("%s-%d: already disconnected.\n", 8858c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num); 8868c2ecf20Sopenharmony_ci } 8878c2ecf20Sopenharmony_ci} 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_cistatic int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx, 8908c2ecf20Sopenharmony_ci struct srp_direct_buf *db, int nbufs, struct scatterlist **sg, 8918c2ecf20Sopenharmony_ci unsigned *sg_cnt) 8928c2ecf20Sopenharmony_ci{ 8938c2ecf20Sopenharmony_ci enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd); 8948c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = ioctx->ch; 8958c2ecf20Sopenharmony_ci struct scatterlist *prev = NULL; 8968c2ecf20Sopenharmony_ci unsigned prev_nents; 8978c2ecf20Sopenharmony_ci int ret, i; 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci if (nbufs == 1) { 9008c2ecf20Sopenharmony_ci ioctx->rw_ctxs = &ioctx->s_rw_ctx; 9018c2ecf20Sopenharmony_ci } else { 9028c2ecf20Sopenharmony_ci ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs), 9038c2ecf20Sopenharmony_ci GFP_KERNEL); 9048c2ecf20Sopenharmony_ci if (!ioctx->rw_ctxs) 9058c2ecf20Sopenharmony_ci return -ENOMEM; 9068c2ecf20Sopenharmony_ci } 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) { 9098c2ecf20Sopenharmony_ci struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 9108c2ecf20Sopenharmony_ci u64 remote_addr = be64_to_cpu(db->va); 9118c2ecf20Sopenharmony_ci u32 size = be32_to_cpu(db->len); 9128c2ecf20Sopenharmony_ci u32 rkey = be32_to_cpu(db->key); 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false, 9158c2ecf20Sopenharmony_ci i < nbufs - 1); 9168c2ecf20Sopenharmony_ci if (ret) 9178c2ecf20Sopenharmony_ci goto unwind; 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port, 9208c2ecf20Sopenharmony_ci ctx->sg, ctx->nents, 0, remote_addr, rkey, dir); 9218c2ecf20Sopenharmony_ci if (ret < 0) { 9228c2ecf20Sopenharmony_ci target_free_sgl(ctx->sg, ctx->nents); 9238c2ecf20Sopenharmony_ci goto unwind; 9248c2ecf20Sopenharmony_ci } 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci ioctx->n_rdma += ret; 9278c2ecf20Sopenharmony_ci ioctx->n_rw_ctx++; 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci if (prev) { 9308c2ecf20Sopenharmony_ci sg_unmark_end(&prev[prev_nents - 1]); 9318c2ecf20Sopenharmony_ci sg_chain(prev, prev_nents + 1, ctx->sg); 9328c2ecf20Sopenharmony_ci } else { 9338c2ecf20Sopenharmony_ci *sg = ctx->sg; 9348c2ecf20Sopenharmony_ci } 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci prev = ctx->sg; 9378c2ecf20Sopenharmony_ci prev_nents = ctx->nents; 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci *sg_cnt += ctx->nents; 9408c2ecf20Sopenharmony_ci } 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_ci return 0; 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ciunwind: 9458c2ecf20Sopenharmony_ci while (--i >= 0) { 9468c2ecf20Sopenharmony_ci struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port, 9498c2ecf20Sopenharmony_ci ctx->sg, ctx->nents, dir); 9508c2ecf20Sopenharmony_ci target_free_sgl(ctx->sg, ctx->nents); 9518c2ecf20Sopenharmony_ci } 9528c2ecf20Sopenharmony_ci if (ioctx->rw_ctxs != &ioctx->s_rw_ctx) 9538c2ecf20Sopenharmony_ci kfree(ioctx->rw_ctxs); 9548c2ecf20Sopenharmony_ci return ret; 9558c2ecf20Sopenharmony_ci} 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_cistatic void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch, 9588c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx) 9598c2ecf20Sopenharmony_ci{ 9608c2ecf20Sopenharmony_ci enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd); 9618c2ecf20Sopenharmony_ci int i; 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci for (i = 0; i < ioctx->n_rw_ctx; i++) { 9648c2ecf20Sopenharmony_ci struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port, 9678c2ecf20Sopenharmony_ci ctx->sg, ctx->nents, dir); 9688c2ecf20Sopenharmony_ci target_free_sgl(ctx->sg, ctx->nents); 9698c2ecf20Sopenharmony_ci } 9708c2ecf20Sopenharmony_ci 9718c2ecf20Sopenharmony_ci if (ioctx->rw_ctxs != &ioctx->s_rw_ctx) 9728c2ecf20Sopenharmony_ci kfree(ioctx->rw_ctxs); 9738c2ecf20Sopenharmony_ci} 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_cistatic inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd) 9768c2ecf20Sopenharmony_ci{ 9778c2ecf20Sopenharmony_ci /* 9788c2ecf20Sopenharmony_ci * The pointer computations below will only be compiled correctly 9798c2ecf20Sopenharmony_ci * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check 9808c2ecf20Sopenharmony_ci * whether srp_cmd::add_data has been declared as a byte pointer. 9818c2ecf20Sopenharmony_ci */ 9828c2ecf20Sopenharmony_ci BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) && 9838c2ecf20Sopenharmony_ci !__same_type(srp_cmd->add_data[0], (u8)0)); 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci /* 9868c2ecf20Sopenharmony_ci * According to the SRP spec, the lower two bits of the 'ADDITIONAL 9878c2ecf20Sopenharmony_ci * CDB LENGTH' field are reserved and the size in bytes of this field 9888c2ecf20Sopenharmony_ci * is four times the value specified in bits 3..7. Hence the "& ~3". 9898c2ecf20Sopenharmony_ci */ 9908c2ecf20Sopenharmony_ci return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3); 9918c2ecf20Sopenharmony_ci} 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci/** 9948c2ecf20Sopenharmony_ci * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request 9958c2ecf20Sopenharmony_ci * @recv_ioctx: I/O context associated with the received command @srp_cmd. 9968c2ecf20Sopenharmony_ci * @ioctx: I/O context that will be used for responding to the initiator. 9978c2ecf20Sopenharmony_ci * @srp_cmd: Pointer to the SRP_CMD request data. 9988c2ecf20Sopenharmony_ci * @dir: Pointer to the variable to which the transfer direction will be 9998c2ecf20Sopenharmony_ci * written. 10008c2ecf20Sopenharmony_ci * @sg: [out] scatterlist for the parsed SRP_CMD. 10018c2ecf20Sopenharmony_ci * @sg_cnt: [out] length of @sg. 10028c2ecf20Sopenharmony_ci * @data_len: Pointer to the variable to which the total data length of all 10038c2ecf20Sopenharmony_ci * descriptors in the SRP_CMD request will be written. 10048c2ecf20Sopenharmony_ci * @imm_data_offset: [in] Offset in SRP_CMD requests at which immediate data 10058c2ecf20Sopenharmony_ci * starts. 10068c2ecf20Sopenharmony_ci * 10078c2ecf20Sopenharmony_ci * This function initializes ioctx->nrbuf and ioctx->r_bufs. 10088c2ecf20Sopenharmony_ci * 10098c2ecf20Sopenharmony_ci * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors; 10108c2ecf20Sopenharmony_ci * -ENOMEM when memory allocation fails and zero upon success. 10118c2ecf20Sopenharmony_ci */ 10128c2ecf20Sopenharmony_cistatic int srpt_get_desc_tbl(struct srpt_recv_ioctx *recv_ioctx, 10138c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx, 10148c2ecf20Sopenharmony_ci struct srp_cmd *srp_cmd, enum dma_data_direction *dir, 10158c2ecf20Sopenharmony_ci struct scatterlist **sg, unsigned int *sg_cnt, u64 *data_len, 10168c2ecf20Sopenharmony_ci u16 imm_data_offset) 10178c2ecf20Sopenharmony_ci{ 10188c2ecf20Sopenharmony_ci BUG_ON(!dir); 10198c2ecf20Sopenharmony_ci BUG_ON(!data_len); 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci /* 10228c2ecf20Sopenharmony_ci * The lower four bits of the buffer format field contain the DATA-IN 10238c2ecf20Sopenharmony_ci * buffer descriptor format, and the highest four bits contain the 10248c2ecf20Sopenharmony_ci * DATA-OUT buffer descriptor format. 10258c2ecf20Sopenharmony_ci */ 10268c2ecf20Sopenharmony_ci if (srp_cmd->buf_fmt & 0xf) 10278c2ecf20Sopenharmony_ci /* DATA-IN: transfer data from target to initiator (read). */ 10288c2ecf20Sopenharmony_ci *dir = DMA_FROM_DEVICE; 10298c2ecf20Sopenharmony_ci else if (srp_cmd->buf_fmt >> 4) 10308c2ecf20Sopenharmony_ci /* DATA-OUT: transfer data from initiator to target (write). */ 10318c2ecf20Sopenharmony_ci *dir = DMA_TO_DEVICE; 10328c2ecf20Sopenharmony_ci else 10338c2ecf20Sopenharmony_ci *dir = DMA_NONE; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */ 10368c2ecf20Sopenharmony_ci ioctx->cmd.data_direction = *dir; 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) || 10398c2ecf20Sopenharmony_ci ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) { 10408c2ecf20Sopenharmony_ci struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd); 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci *data_len = be32_to_cpu(db->len); 10438c2ecf20Sopenharmony_ci return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt); 10448c2ecf20Sopenharmony_ci } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) || 10458c2ecf20Sopenharmony_ci ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) { 10468c2ecf20Sopenharmony_ci struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd); 10478c2ecf20Sopenharmony_ci int nbufs = be32_to_cpu(idb->table_desc.len) / 10488c2ecf20Sopenharmony_ci sizeof(struct srp_direct_buf); 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci if (nbufs > 10518c2ecf20Sopenharmony_ci (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) { 10528c2ecf20Sopenharmony_ci pr_err("received unsupported SRP_CMD request type (%u out + %u in != %u / %zu)\n", 10538c2ecf20Sopenharmony_ci srp_cmd->data_out_desc_cnt, 10548c2ecf20Sopenharmony_ci srp_cmd->data_in_desc_cnt, 10558c2ecf20Sopenharmony_ci be32_to_cpu(idb->table_desc.len), 10568c2ecf20Sopenharmony_ci sizeof(struct srp_direct_buf)); 10578c2ecf20Sopenharmony_ci return -EINVAL; 10588c2ecf20Sopenharmony_ci } 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ci *data_len = be32_to_cpu(idb->len); 10618c2ecf20Sopenharmony_ci return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs, 10628c2ecf20Sopenharmony_ci sg, sg_cnt); 10638c2ecf20Sopenharmony_ci } else if ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_IMM) { 10648c2ecf20Sopenharmony_ci struct srp_imm_buf *imm_buf = srpt_get_desc_buf(srp_cmd); 10658c2ecf20Sopenharmony_ci void *data = (void *)srp_cmd + imm_data_offset; 10668c2ecf20Sopenharmony_ci uint32_t len = be32_to_cpu(imm_buf->len); 10678c2ecf20Sopenharmony_ci uint32_t req_size = imm_data_offset + len; 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ci if (req_size > srp_max_req_size) { 10708c2ecf20Sopenharmony_ci pr_err("Immediate data (length %d + %d) exceeds request size %d\n", 10718c2ecf20Sopenharmony_ci imm_data_offset, len, srp_max_req_size); 10728c2ecf20Sopenharmony_ci return -EINVAL; 10738c2ecf20Sopenharmony_ci } 10748c2ecf20Sopenharmony_ci if (recv_ioctx->byte_len < req_size) { 10758c2ecf20Sopenharmony_ci pr_err("Received too few data - %d < %d\n", 10768c2ecf20Sopenharmony_ci recv_ioctx->byte_len, req_size); 10778c2ecf20Sopenharmony_ci return -EIO; 10788c2ecf20Sopenharmony_ci } 10798c2ecf20Sopenharmony_ci /* 10808c2ecf20Sopenharmony_ci * The immediate data buffer descriptor must occur before the 10818c2ecf20Sopenharmony_ci * immediate data itself. 10828c2ecf20Sopenharmony_ci */ 10838c2ecf20Sopenharmony_ci if ((void *)(imm_buf + 1) > (void *)data) { 10848c2ecf20Sopenharmony_ci pr_err("Received invalid write request\n"); 10858c2ecf20Sopenharmony_ci return -EINVAL; 10868c2ecf20Sopenharmony_ci } 10878c2ecf20Sopenharmony_ci *data_len = len; 10888c2ecf20Sopenharmony_ci ioctx->recv_ioctx = recv_ioctx; 10898c2ecf20Sopenharmony_ci if ((uintptr_t)data & 511) { 10908c2ecf20Sopenharmony_ci pr_warn_once("Internal error - the receive buffers are not aligned properly.\n"); 10918c2ecf20Sopenharmony_ci return -EINVAL; 10928c2ecf20Sopenharmony_ci } 10938c2ecf20Sopenharmony_ci sg_init_one(&ioctx->imm_sg, data, len); 10948c2ecf20Sopenharmony_ci *sg = &ioctx->imm_sg; 10958c2ecf20Sopenharmony_ci *sg_cnt = 1; 10968c2ecf20Sopenharmony_ci return 0; 10978c2ecf20Sopenharmony_ci } else { 10988c2ecf20Sopenharmony_ci *data_len = 0; 10998c2ecf20Sopenharmony_ci return 0; 11008c2ecf20Sopenharmony_ci } 11018c2ecf20Sopenharmony_ci} 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci/** 11048c2ecf20Sopenharmony_ci * srpt_init_ch_qp - initialize queue pair attributes 11058c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 11068c2ecf20Sopenharmony_ci * @qp: Queue pair pointer. 11078c2ecf20Sopenharmony_ci * 11088c2ecf20Sopenharmony_ci * Initialized the attributes of queue pair 'qp' by allowing local write, 11098c2ecf20Sopenharmony_ci * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT. 11108c2ecf20Sopenharmony_ci */ 11118c2ecf20Sopenharmony_cistatic int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp) 11128c2ecf20Sopenharmony_ci{ 11138c2ecf20Sopenharmony_ci struct ib_qp_attr *attr; 11148c2ecf20Sopenharmony_ci int ret; 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ci WARN_ON_ONCE(ch->using_rdma_cm); 11178c2ecf20Sopenharmony_ci 11188c2ecf20Sopenharmony_ci attr = kzalloc(sizeof(*attr), GFP_KERNEL); 11198c2ecf20Sopenharmony_ci if (!attr) 11208c2ecf20Sopenharmony_ci return -ENOMEM; 11218c2ecf20Sopenharmony_ci 11228c2ecf20Sopenharmony_ci attr->qp_state = IB_QPS_INIT; 11238c2ecf20Sopenharmony_ci attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE; 11248c2ecf20Sopenharmony_ci attr->port_num = ch->sport->port; 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port, 11278c2ecf20Sopenharmony_ci ch->pkey, &attr->pkey_index); 11288c2ecf20Sopenharmony_ci if (ret < 0) 11298c2ecf20Sopenharmony_ci pr_err("Translating pkey %#x failed (%d) - using index 0\n", 11308c2ecf20Sopenharmony_ci ch->pkey, ret); 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci ret = ib_modify_qp(qp, attr, 11338c2ecf20Sopenharmony_ci IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT | 11348c2ecf20Sopenharmony_ci IB_QP_PKEY_INDEX); 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_ci kfree(attr); 11378c2ecf20Sopenharmony_ci return ret; 11388c2ecf20Sopenharmony_ci} 11398c2ecf20Sopenharmony_ci 11408c2ecf20Sopenharmony_ci/** 11418c2ecf20Sopenharmony_ci * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR) 11428c2ecf20Sopenharmony_ci * @ch: channel of the queue pair. 11438c2ecf20Sopenharmony_ci * @qp: queue pair to change the state of. 11448c2ecf20Sopenharmony_ci * 11458c2ecf20Sopenharmony_ci * Returns zero upon success and a negative value upon failure. 11468c2ecf20Sopenharmony_ci * 11478c2ecf20Sopenharmony_ci * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. 11488c2ecf20Sopenharmony_ci * If this structure ever becomes larger, it might be necessary to allocate 11498c2ecf20Sopenharmony_ci * it dynamically instead of on the stack. 11508c2ecf20Sopenharmony_ci */ 11518c2ecf20Sopenharmony_cistatic int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp) 11528c2ecf20Sopenharmony_ci{ 11538c2ecf20Sopenharmony_ci struct ib_qp_attr qp_attr; 11548c2ecf20Sopenharmony_ci int attr_mask; 11558c2ecf20Sopenharmony_ci int ret; 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ci WARN_ON_ONCE(ch->using_rdma_cm); 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci qp_attr.qp_state = IB_QPS_RTR; 11608c2ecf20Sopenharmony_ci ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask); 11618c2ecf20Sopenharmony_ci if (ret) 11628c2ecf20Sopenharmony_ci goto out; 11638c2ecf20Sopenharmony_ci 11648c2ecf20Sopenharmony_ci qp_attr.max_dest_rd_atomic = 4; 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci ret = ib_modify_qp(qp, &qp_attr, attr_mask); 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ciout: 11698c2ecf20Sopenharmony_ci return ret; 11708c2ecf20Sopenharmony_ci} 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci/** 11738c2ecf20Sopenharmony_ci * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS) 11748c2ecf20Sopenharmony_ci * @ch: channel of the queue pair. 11758c2ecf20Sopenharmony_ci * @qp: queue pair to change the state of. 11768c2ecf20Sopenharmony_ci * 11778c2ecf20Sopenharmony_ci * Returns zero upon success and a negative value upon failure. 11788c2ecf20Sopenharmony_ci * 11798c2ecf20Sopenharmony_ci * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system. 11808c2ecf20Sopenharmony_ci * If this structure ever becomes larger, it might be necessary to allocate 11818c2ecf20Sopenharmony_ci * it dynamically instead of on the stack. 11828c2ecf20Sopenharmony_ci */ 11838c2ecf20Sopenharmony_cistatic int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp) 11848c2ecf20Sopenharmony_ci{ 11858c2ecf20Sopenharmony_ci struct ib_qp_attr qp_attr; 11868c2ecf20Sopenharmony_ci int attr_mask; 11878c2ecf20Sopenharmony_ci int ret; 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci qp_attr.qp_state = IB_QPS_RTS; 11908c2ecf20Sopenharmony_ci ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask); 11918c2ecf20Sopenharmony_ci if (ret) 11928c2ecf20Sopenharmony_ci goto out; 11938c2ecf20Sopenharmony_ci 11948c2ecf20Sopenharmony_ci qp_attr.max_rd_atomic = 4; 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_ci ret = ib_modify_qp(qp, &qp_attr, attr_mask); 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ciout: 11998c2ecf20Sopenharmony_ci return ret; 12008c2ecf20Sopenharmony_ci} 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_ci/** 12038c2ecf20Sopenharmony_ci * srpt_ch_qp_err - set the channel queue pair state to 'error' 12048c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 12058c2ecf20Sopenharmony_ci */ 12068c2ecf20Sopenharmony_cistatic int srpt_ch_qp_err(struct srpt_rdma_ch *ch) 12078c2ecf20Sopenharmony_ci{ 12088c2ecf20Sopenharmony_ci struct ib_qp_attr qp_attr; 12098c2ecf20Sopenharmony_ci 12108c2ecf20Sopenharmony_ci qp_attr.qp_state = IB_QPS_ERR; 12118c2ecf20Sopenharmony_ci return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE); 12128c2ecf20Sopenharmony_ci} 12138c2ecf20Sopenharmony_ci 12148c2ecf20Sopenharmony_ci/** 12158c2ecf20Sopenharmony_ci * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator 12168c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 12178c2ecf20Sopenharmony_ci */ 12188c2ecf20Sopenharmony_cistatic struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch) 12198c2ecf20Sopenharmony_ci{ 12208c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx; 12218c2ecf20Sopenharmony_ci int tag, cpu; 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_ci BUG_ON(!ch); 12248c2ecf20Sopenharmony_ci 12258c2ecf20Sopenharmony_ci tag = sbitmap_queue_get(&ch->sess->sess_tag_pool, &cpu); 12268c2ecf20Sopenharmony_ci if (tag < 0) 12278c2ecf20Sopenharmony_ci return NULL; 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_ci ioctx = ch->ioctx_ring[tag]; 12308c2ecf20Sopenharmony_ci BUG_ON(ioctx->ch != ch); 12318c2ecf20Sopenharmony_ci ioctx->state = SRPT_STATE_NEW; 12328c2ecf20Sopenharmony_ci WARN_ON_ONCE(ioctx->recv_ioctx); 12338c2ecf20Sopenharmony_ci ioctx->n_rdma = 0; 12348c2ecf20Sopenharmony_ci ioctx->n_rw_ctx = 0; 12358c2ecf20Sopenharmony_ci ioctx->queue_status_only = false; 12368c2ecf20Sopenharmony_ci /* 12378c2ecf20Sopenharmony_ci * transport_init_se_cmd() does not initialize all fields, so do it 12388c2ecf20Sopenharmony_ci * here. 12398c2ecf20Sopenharmony_ci */ 12408c2ecf20Sopenharmony_ci memset(&ioctx->cmd, 0, sizeof(ioctx->cmd)); 12418c2ecf20Sopenharmony_ci memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data)); 12428c2ecf20Sopenharmony_ci ioctx->cmd.map_tag = tag; 12438c2ecf20Sopenharmony_ci ioctx->cmd.map_cpu = cpu; 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci return ioctx; 12468c2ecf20Sopenharmony_ci} 12478c2ecf20Sopenharmony_ci 12488c2ecf20Sopenharmony_ci/** 12498c2ecf20Sopenharmony_ci * srpt_abort_cmd - abort a SCSI command 12508c2ecf20Sopenharmony_ci * @ioctx: I/O context associated with the SCSI command. 12518c2ecf20Sopenharmony_ci */ 12528c2ecf20Sopenharmony_cistatic int srpt_abort_cmd(struct srpt_send_ioctx *ioctx) 12538c2ecf20Sopenharmony_ci{ 12548c2ecf20Sopenharmony_ci enum srpt_command_state state; 12558c2ecf20Sopenharmony_ci 12568c2ecf20Sopenharmony_ci BUG_ON(!ioctx); 12578c2ecf20Sopenharmony_ci 12588c2ecf20Sopenharmony_ci /* 12598c2ecf20Sopenharmony_ci * If the command is in a state where the target core is waiting for 12608c2ecf20Sopenharmony_ci * the ib_srpt driver, change the state to the next state. 12618c2ecf20Sopenharmony_ci */ 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_ci state = ioctx->state; 12648c2ecf20Sopenharmony_ci switch (state) { 12658c2ecf20Sopenharmony_ci case SRPT_STATE_NEED_DATA: 12668c2ecf20Sopenharmony_ci ioctx->state = SRPT_STATE_DATA_IN; 12678c2ecf20Sopenharmony_ci break; 12688c2ecf20Sopenharmony_ci case SRPT_STATE_CMD_RSP_SENT: 12698c2ecf20Sopenharmony_ci case SRPT_STATE_MGMT_RSP_SENT: 12708c2ecf20Sopenharmony_ci ioctx->state = SRPT_STATE_DONE; 12718c2ecf20Sopenharmony_ci break; 12728c2ecf20Sopenharmony_ci default: 12738c2ecf20Sopenharmony_ci WARN_ONCE(true, "%s: unexpected I/O context state %d\n", 12748c2ecf20Sopenharmony_ci __func__, state); 12758c2ecf20Sopenharmony_ci break; 12768c2ecf20Sopenharmony_ci } 12778c2ecf20Sopenharmony_ci 12788c2ecf20Sopenharmony_ci pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state, 12798c2ecf20Sopenharmony_ci ioctx->state, ioctx->cmd.tag); 12808c2ecf20Sopenharmony_ci 12818c2ecf20Sopenharmony_ci switch (state) { 12828c2ecf20Sopenharmony_ci case SRPT_STATE_NEW: 12838c2ecf20Sopenharmony_ci case SRPT_STATE_DATA_IN: 12848c2ecf20Sopenharmony_ci case SRPT_STATE_MGMT: 12858c2ecf20Sopenharmony_ci case SRPT_STATE_DONE: 12868c2ecf20Sopenharmony_ci /* 12878c2ecf20Sopenharmony_ci * Do nothing - defer abort processing until 12888c2ecf20Sopenharmony_ci * srpt_queue_response() is invoked. 12898c2ecf20Sopenharmony_ci */ 12908c2ecf20Sopenharmony_ci break; 12918c2ecf20Sopenharmony_ci case SRPT_STATE_NEED_DATA: 12928c2ecf20Sopenharmony_ci pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag); 12938c2ecf20Sopenharmony_ci transport_generic_request_failure(&ioctx->cmd, 12948c2ecf20Sopenharmony_ci TCM_CHECK_CONDITION_ABORT_CMD); 12958c2ecf20Sopenharmony_ci break; 12968c2ecf20Sopenharmony_ci case SRPT_STATE_CMD_RSP_SENT: 12978c2ecf20Sopenharmony_ci /* 12988c2ecf20Sopenharmony_ci * SRP_RSP sending failed or the SRP_RSP send completion has 12998c2ecf20Sopenharmony_ci * not been received in time. 13008c2ecf20Sopenharmony_ci */ 13018c2ecf20Sopenharmony_ci transport_generic_free_cmd(&ioctx->cmd, 0); 13028c2ecf20Sopenharmony_ci break; 13038c2ecf20Sopenharmony_ci case SRPT_STATE_MGMT_RSP_SENT: 13048c2ecf20Sopenharmony_ci transport_generic_free_cmd(&ioctx->cmd, 0); 13058c2ecf20Sopenharmony_ci break; 13068c2ecf20Sopenharmony_ci default: 13078c2ecf20Sopenharmony_ci WARN(1, "Unexpected command state (%d)", state); 13088c2ecf20Sopenharmony_ci break; 13098c2ecf20Sopenharmony_ci } 13108c2ecf20Sopenharmony_ci 13118c2ecf20Sopenharmony_ci return state; 13128c2ecf20Sopenharmony_ci} 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci/** 13158c2ecf20Sopenharmony_ci * srpt_rdma_read_done - RDMA read completion callback 13168c2ecf20Sopenharmony_ci * @cq: Completion queue. 13178c2ecf20Sopenharmony_ci * @wc: Work completion. 13188c2ecf20Sopenharmony_ci * 13198c2ecf20Sopenharmony_ci * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping 13208c2ecf20Sopenharmony_ci * the data that has been transferred via IB RDMA had to be postponed until the 13218c2ecf20Sopenharmony_ci * check_stop_free() callback. None of this is necessary anymore and needs to 13228c2ecf20Sopenharmony_ci * be cleaned up. 13238c2ecf20Sopenharmony_ci */ 13248c2ecf20Sopenharmony_cistatic void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc) 13258c2ecf20Sopenharmony_ci{ 13268c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = wc->qp->qp_context; 13278c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = 13288c2ecf20Sopenharmony_ci container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe); 13298c2ecf20Sopenharmony_ci 13308c2ecf20Sopenharmony_ci WARN_ON(ioctx->n_rdma <= 0); 13318c2ecf20Sopenharmony_ci atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); 13328c2ecf20Sopenharmony_ci ioctx->n_rdma = 0; 13338c2ecf20Sopenharmony_ci 13348c2ecf20Sopenharmony_ci if (unlikely(wc->status != IB_WC_SUCCESS)) { 13358c2ecf20Sopenharmony_ci pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n", 13368c2ecf20Sopenharmony_ci ioctx, wc->status); 13378c2ecf20Sopenharmony_ci srpt_abort_cmd(ioctx); 13388c2ecf20Sopenharmony_ci return; 13398c2ecf20Sopenharmony_ci } 13408c2ecf20Sopenharmony_ci 13418c2ecf20Sopenharmony_ci if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA, 13428c2ecf20Sopenharmony_ci SRPT_STATE_DATA_IN)) 13438c2ecf20Sopenharmony_ci target_execute_cmd(&ioctx->cmd); 13448c2ecf20Sopenharmony_ci else 13458c2ecf20Sopenharmony_ci pr_err("%s[%d]: wrong state = %d\n", __func__, 13468c2ecf20Sopenharmony_ci __LINE__, ioctx->state); 13478c2ecf20Sopenharmony_ci} 13488c2ecf20Sopenharmony_ci 13498c2ecf20Sopenharmony_ci/** 13508c2ecf20Sopenharmony_ci * srpt_build_cmd_rsp - build a SRP_RSP response 13518c2ecf20Sopenharmony_ci * @ch: RDMA channel through which the request has been received. 13528c2ecf20Sopenharmony_ci * @ioctx: I/O context associated with the SRP_CMD request. The response will 13538c2ecf20Sopenharmony_ci * be built in the buffer ioctx->buf points at and hence this function will 13548c2ecf20Sopenharmony_ci * overwrite the request data. 13558c2ecf20Sopenharmony_ci * @tag: tag of the request for which this response is being generated. 13568c2ecf20Sopenharmony_ci * @status: value for the STATUS field of the SRP_RSP information unit. 13578c2ecf20Sopenharmony_ci * 13588c2ecf20Sopenharmony_ci * Returns the size in bytes of the SRP_RSP response. 13598c2ecf20Sopenharmony_ci * 13608c2ecf20Sopenharmony_ci * An SRP_RSP response contains a SCSI status or service response. See also 13618c2ecf20Sopenharmony_ci * section 6.9 in the SRP r16a document for the format of an SRP_RSP 13628c2ecf20Sopenharmony_ci * response. See also SPC-2 for more information about sense data. 13638c2ecf20Sopenharmony_ci */ 13648c2ecf20Sopenharmony_cistatic int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch, 13658c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx, u64 tag, 13668c2ecf20Sopenharmony_ci int status) 13678c2ecf20Sopenharmony_ci{ 13688c2ecf20Sopenharmony_ci struct se_cmd *cmd = &ioctx->cmd; 13698c2ecf20Sopenharmony_ci struct srp_rsp *srp_rsp; 13708c2ecf20Sopenharmony_ci const u8 *sense_data; 13718c2ecf20Sopenharmony_ci int sense_data_len, max_sense_len; 13728c2ecf20Sopenharmony_ci u32 resid = cmd->residual_count; 13738c2ecf20Sopenharmony_ci 13748c2ecf20Sopenharmony_ci /* 13758c2ecf20Sopenharmony_ci * The lowest bit of all SAM-3 status codes is zero (see also 13768c2ecf20Sopenharmony_ci * paragraph 5.3 in SAM-3). 13778c2ecf20Sopenharmony_ci */ 13788c2ecf20Sopenharmony_ci WARN_ON(status & 1); 13798c2ecf20Sopenharmony_ci 13808c2ecf20Sopenharmony_ci srp_rsp = ioctx->ioctx.buf; 13818c2ecf20Sopenharmony_ci BUG_ON(!srp_rsp); 13828c2ecf20Sopenharmony_ci 13838c2ecf20Sopenharmony_ci sense_data = ioctx->sense_data; 13848c2ecf20Sopenharmony_ci sense_data_len = ioctx->cmd.scsi_sense_length; 13858c2ecf20Sopenharmony_ci WARN_ON(sense_data_len > sizeof(ioctx->sense_data)); 13868c2ecf20Sopenharmony_ci 13878c2ecf20Sopenharmony_ci memset(srp_rsp, 0, sizeof(*srp_rsp)); 13888c2ecf20Sopenharmony_ci srp_rsp->opcode = SRP_RSP; 13898c2ecf20Sopenharmony_ci srp_rsp->req_lim_delta = 13908c2ecf20Sopenharmony_ci cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); 13918c2ecf20Sopenharmony_ci srp_rsp->tag = tag; 13928c2ecf20Sopenharmony_ci srp_rsp->status = status; 13938c2ecf20Sopenharmony_ci 13948c2ecf20Sopenharmony_ci if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { 13958c2ecf20Sopenharmony_ci if (cmd->data_direction == DMA_TO_DEVICE) { 13968c2ecf20Sopenharmony_ci /* residual data from an underflow write */ 13978c2ecf20Sopenharmony_ci srp_rsp->flags = SRP_RSP_FLAG_DOUNDER; 13988c2ecf20Sopenharmony_ci srp_rsp->data_out_res_cnt = cpu_to_be32(resid); 13998c2ecf20Sopenharmony_ci } else if (cmd->data_direction == DMA_FROM_DEVICE) { 14008c2ecf20Sopenharmony_ci /* residual data from an underflow read */ 14018c2ecf20Sopenharmony_ci srp_rsp->flags = SRP_RSP_FLAG_DIUNDER; 14028c2ecf20Sopenharmony_ci srp_rsp->data_in_res_cnt = cpu_to_be32(resid); 14038c2ecf20Sopenharmony_ci } 14048c2ecf20Sopenharmony_ci } else if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { 14058c2ecf20Sopenharmony_ci if (cmd->data_direction == DMA_TO_DEVICE) { 14068c2ecf20Sopenharmony_ci /* residual data from an overflow write */ 14078c2ecf20Sopenharmony_ci srp_rsp->flags = SRP_RSP_FLAG_DOOVER; 14088c2ecf20Sopenharmony_ci srp_rsp->data_out_res_cnt = cpu_to_be32(resid); 14098c2ecf20Sopenharmony_ci } else if (cmd->data_direction == DMA_FROM_DEVICE) { 14108c2ecf20Sopenharmony_ci /* residual data from an overflow read */ 14118c2ecf20Sopenharmony_ci srp_rsp->flags = SRP_RSP_FLAG_DIOVER; 14128c2ecf20Sopenharmony_ci srp_rsp->data_in_res_cnt = cpu_to_be32(resid); 14138c2ecf20Sopenharmony_ci } 14148c2ecf20Sopenharmony_ci } 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_ci if (sense_data_len) { 14178c2ecf20Sopenharmony_ci BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp)); 14188c2ecf20Sopenharmony_ci max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp); 14198c2ecf20Sopenharmony_ci if (sense_data_len > max_sense_len) { 14208c2ecf20Sopenharmony_ci pr_warn("truncated sense data from %d to %d bytes\n", 14218c2ecf20Sopenharmony_ci sense_data_len, max_sense_len); 14228c2ecf20Sopenharmony_ci sense_data_len = max_sense_len; 14238c2ecf20Sopenharmony_ci } 14248c2ecf20Sopenharmony_ci 14258c2ecf20Sopenharmony_ci srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID; 14268c2ecf20Sopenharmony_ci srp_rsp->sense_data_len = cpu_to_be32(sense_data_len); 14278c2ecf20Sopenharmony_ci memcpy(srp_rsp + 1, sense_data, sense_data_len); 14288c2ecf20Sopenharmony_ci } 14298c2ecf20Sopenharmony_ci 14308c2ecf20Sopenharmony_ci return sizeof(*srp_rsp) + sense_data_len; 14318c2ecf20Sopenharmony_ci} 14328c2ecf20Sopenharmony_ci 14338c2ecf20Sopenharmony_ci/** 14348c2ecf20Sopenharmony_ci * srpt_build_tskmgmt_rsp - build a task management response 14358c2ecf20Sopenharmony_ci * @ch: RDMA channel through which the request has been received. 14368c2ecf20Sopenharmony_ci * @ioctx: I/O context in which the SRP_RSP response will be built. 14378c2ecf20Sopenharmony_ci * @rsp_code: RSP_CODE that will be stored in the response. 14388c2ecf20Sopenharmony_ci * @tag: Tag of the request for which this response is being generated. 14398c2ecf20Sopenharmony_ci * 14408c2ecf20Sopenharmony_ci * Returns the size in bytes of the SRP_RSP response. 14418c2ecf20Sopenharmony_ci * 14428c2ecf20Sopenharmony_ci * An SRP_RSP response contains a SCSI status or service response. See also 14438c2ecf20Sopenharmony_ci * section 6.9 in the SRP r16a document for the format of an SRP_RSP 14448c2ecf20Sopenharmony_ci * response. 14458c2ecf20Sopenharmony_ci */ 14468c2ecf20Sopenharmony_cistatic int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch, 14478c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx, 14488c2ecf20Sopenharmony_ci u8 rsp_code, u64 tag) 14498c2ecf20Sopenharmony_ci{ 14508c2ecf20Sopenharmony_ci struct srp_rsp *srp_rsp; 14518c2ecf20Sopenharmony_ci int resp_data_len; 14528c2ecf20Sopenharmony_ci int resp_len; 14538c2ecf20Sopenharmony_ci 14548c2ecf20Sopenharmony_ci resp_data_len = 4; 14558c2ecf20Sopenharmony_ci resp_len = sizeof(*srp_rsp) + resp_data_len; 14568c2ecf20Sopenharmony_ci 14578c2ecf20Sopenharmony_ci srp_rsp = ioctx->ioctx.buf; 14588c2ecf20Sopenharmony_ci BUG_ON(!srp_rsp); 14598c2ecf20Sopenharmony_ci memset(srp_rsp, 0, sizeof(*srp_rsp)); 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_ci srp_rsp->opcode = SRP_RSP; 14628c2ecf20Sopenharmony_ci srp_rsp->req_lim_delta = 14638c2ecf20Sopenharmony_ci cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0)); 14648c2ecf20Sopenharmony_ci srp_rsp->tag = tag; 14658c2ecf20Sopenharmony_ci 14668c2ecf20Sopenharmony_ci srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID; 14678c2ecf20Sopenharmony_ci srp_rsp->resp_data_len = cpu_to_be32(resp_data_len); 14688c2ecf20Sopenharmony_ci srp_rsp->data[3] = rsp_code; 14698c2ecf20Sopenharmony_ci 14708c2ecf20Sopenharmony_ci return resp_len; 14718c2ecf20Sopenharmony_ci} 14728c2ecf20Sopenharmony_ci 14738c2ecf20Sopenharmony_cistatic int srpt_check_stop_free(struct se_cmd *cmd) 14748c2ecf20Sopenharmony_ci{ 14758c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = container_of(cmd, 14768c2ecf20Sopenharmony_ci struct srpt_send_ioctx, cmd); 14778c2ecf20Sopenharmony_ci 14788c2ecf20Sopenharmony_ci return target_put_sess_cmd(&ioctx->cmd); 14798c2ecf20Sopenharmony_ci} 14808c2ecf20Sopenharmony_ci 14818c2ecf20Sopenharmony_ci/** 14828c2ecf20Sopenharmony_ci * srpt_handle_cmd - process a SRP_CMD information unit 14838c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 14848c2ecf20Sopenharmony_ci * @recv_ioctx: Receive I/O context. 14858c2ecf20Sopenharmony_ci * @send_ioctx: Send I/O context. 14868c2ecf20Sopenharmony_ci */ 14878c2ecf20Sopenharmony_cistatic void srpt_handle_cmd(struct srpt_rdma_ch *ch, 14888c2ecf20Sopenharmony_ci struct srpt_recv_ioctx *recv_ioctx, 14898c2ecf20Sopenharmony_ci struct srpt_send_ioctx *send_ioctx) 14908c2ecf20Sopenharmony_ci{ 14918c2ecf20Sopenharmony_ci struct se_cmd *cmd; 14928c2ecf20Sopenharmony_ci struct srp_cmd *srp_cmd; 14938c2ecf20Sopenharmony_ci struct scatterlist *sg = NULL; 14948c2ecf20Sopenharmony_ci unsigned sg_cnt = 0; 14958c2ecf20Sopenharmony_ci u64 data_len; 14968c2ecf20Sopenharmony_ci enum dma_data_direction dir; 14978c2ecf20Sopenharmony_ci int rc; 14988c2ecf20Sopenharmony_ci 14998c2ecf20Sopenharmony_ci BUG_ON(!send_ioctx); 15008c2ecf20Sopenharmony_ci 15018c2ecf20Sopenharmony_ci srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; 15028c2ecf20Sopenharmony_ci cmd = &send_ioctx->cmd; 15038c2ecf20Sopenharmony_ci cmd->tag = srp_cmd->tag; 15048c2ecf20Sopenharmony_ci 15058c2ecf20Sopenharmony_ci switch (srp_cmd->task_attr) { 15068c2ecf20Sopenharmony_ci case SRP_CMD_SIMPLE_Q: 15078c2ecf20Sopenharmony_ci cmd->sam_task_attr = TCM_SIMPLE_TAG; 15088c2ecf20Sopenharmony_ci break; 15098c2ecf20Sopenharmony_ci case SRP_CMD_ORDERED_Q: 15108c2ecf20Sopenharmony_ci default: 15118c2ecf20Sopenharmony_ci cmd->sam_task_attr = TCM_ORDERED_TAG; 15128c2ecf20Sopenharmony_ci break; 15138c2ecf20Sopenharmony_ci case SRP_CMD_HEAD_OF_Q: 15148c2ecf20Sopenharmony_ci cmd->sam_task_attr = TCM_HEAD_TAG; 15158c2ecf20Sopenharmony_ci break; 15168c2ecf20Sopenharmony_ci case SRP_CMD_ACA: 15178c2ecf20Sopenharmony_ci cmd->sam_task_attr = TCM_ACA_TAG; 15188c2ecf20Sopenharmony_ci break; 15198c2ecf20Sopenharmony_ci } 15208c2ecf20Sopenharmony_ci 15218c2ecf20Sopenharmony_ci rc = srpt_get_desc_tbl(recv_ioctx, send_ioctx, srp_cmd, &dir, 15228c2ecf20Sopenharmony_ci &sg, &sg_cnt, &data_len, ch->imm_data_offset); 15238c2ecf20Sopenharmony_ci if (rc) { 15248c2ecf20Sopenharmony_ci if (rc != -EAGAIN) { 15258c2ecf20Sopenharmony_ci pr_err("0x%llx: parsing SRP descriptor table failed.\n", 15268c2ecf20Sopenharmony_ci srp_cmd->tag); 15278c2ecf20Sopenharmony_ci } 15288c2ecf20Sopenharmony_ci goto busy; 15298c2ecf20Sopenharmony_ci } 15308c2ecf20Sopenharmony_ci 15318c2ecf20Sopenharmony_ci rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb, 15328c2ecf20Sopenharmony_ci &send_ioctx->sense_data[0], 15338c2ecf20Sopenharmony_ci scsilun_to_int(&srp_cmd->lun), data_len, 15348c2ecf20Sopenharmony_ci TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF, 15358c2ecf20Sopenharmony_ci sg, sg_cnt, NULL, 0, NULL, 0); 15368c2ecf20Sopenharmony_ci if (rc != 0) { 15378c2ecf20Sopenharmony_ci pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc, 15388c2ecf20Sopenharmony_ci srp_cmd->tag); 15398c2ecf20Sopenharmony_ci goto busy; 15408c2ecf20Sopenharmony_ci } 15418c2ecf20Sopenharmony_ci return; 15428c2ecf20Sopenharmony_ci 15438c2ecf20Sopenharmony_cibusy: 15448c2ecf20Sopenharmony_ci target_send_busy(cmd); 15458c2ecf20Sopenharmony_ci} 15468c2ecf20Sopenharmony_ci 15478c2ecf20Sopenharmony_cistatic int srp_tmr_to_tcm(int fn) 15488c2ecf20Sopenharmony_ci{ 15498c2ecf20Sopenharmony_ci switch (fn) { 15508c2ecf20Sopenharmony_ci case SRP_TSK_ABORT_TASK: 15518c2ecf20Sopenharmony_ci return TMR_ABORT_TASK; 15528c2ecf20Sopenharmony_ci case SRP_TSK_ABORT_TASK_SET: 15538c2ecf20Sopenharmony_ci return TMR_ABORT_TASK_SET; 15548c2ecf20Sopenharmony_ci case SRP_TSK_CLEAR_TASK_SET: 15558c2ecf20Sopenharmony_ci return TMR_CLEAR_TASK_SET; 15568c2ecf20Sopenharmony_ci case SRP_TSK_LUN_RESET: 15578c2ecf20Sopenharmony_ci return TMR_LUN_RESET; 15588c2ecf20Sopenharmony_ci case SRP_TSK_CLEAR_ACA: 15598c2ecf20Sopenharmony_ci return TMR_CLEAR_ACA; 15608c2ecf20Sopenharmony_ci default: 15618c2ecf20Sopenharmony_ci return -1; 15628c2ecf20Sopenharmony_ci } 15638c2ecf20Sopenharmony_ci} 15648c2ecf20Sopenharmony_ci 15658c2ecf20Sopenharmony_ci/** 15668c2ecf20Sopenharmony_ci * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit 15678c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 15688c2ecf20Sopenharmony_ci * @recv_ioctx: Receive I/O context. 15698c2ecf20Sopenharmony_ci * @send_ioctx: Send I/O context. 15708c2ecf20Sopenharmony_ci * 15718c2ecf20Sopenharmony_ci * Returns 0 if and only if the request will be processed by the target core. 15728c2ecf20Sopenharmony_ci * 15738c2ecf20Sopenharmony_ci * For more information about SRP_TSK_MGMT information units, see also section 15748c2ecf20Sopenharmony_ci * 6.7 in the SRP r16a document. 15758c2ecf20Sopenharmony_ci */ 15768c2ecf20Sopenharmony_cistatic void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch, 15778c2ecf20Sopenharmony_ci struct srpt_recv_ioctx *recv_ioctx, 15788c2ecf20Sopenharmony_ci struct srpt_send_ioctx *send_ioctx) 15798c2ecf20Sopenharmony_ci{ 15808c2ecf20Sopenharmony_ci struct srp_tsk_mgmt *srp_tsk; 15818c2ecf20Sopenharmony_ci struct se_cmd *cmd; 15828c2ecf20Sopenharmony_ci struct se_session *sess = ch->sess; 15838c2ecf20Sopenharmony_ci int tcm_tmr; 15848c2ecf20Sopenharmony_ci int rc; 15858c2ecf20Sopenharmony_ci 15868c2ecf20Sopenharmony_ci BUG_ON(!send_ioctx); 15878c2ecf20Sopenharmony_ci 15888c2ecf20Sopenharmony_ci srp_tsk = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; 15898c2ecf20Sopenharmony_ci cmd = &send_ioctx->cmd; 15908c2ecf20Sopenharmony_ci 15918c2ecf20Sopenharmony_ci pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n", 15928c2ecf20Sopenharmony_ci srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch, 15938c2ecf20Sopenharmony_ci ch->sess); 15948c2ecf20Sopenharmony_ci 15958c2ecf20Sopenharmony_ci srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT); 15968c2ecf20Sopenharmony_ci send_ioctx->cmd.tag = srp_tsk->tag; 15978c2ecf20Sopenharmony_ci tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func); 15988c2ecf20Sopenharmony_ci rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, 15998c2ecf20Sopenharmony_ci scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr, 16008c2ecf20Sopenharmony_ci GFP_KERNEL, srp_tsk->task_tag, 16018c2ecf20Sopenharmony_ci TARGET_SCF_ACK_KREF); 16028c2ecf20Sopenharmony_ci if (rc != 0) { 16038c2ecf20Sopenharmony_ci send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED; 16048c2ecf20Sopenharmony_ci cmd->se_tfo->queue_tm_rsp(cmd); 16058c2ecf20Sopenharmony_ci } 16068c2ecf20Sopenharmony_ci return; 16078c2ecf20Sopenharmony_ci} 16088c2ecf20Sopenharmony_ci 16098c2ecf20Sopenharmony_ci/** 16108c2ecf20Sopenharmony_ci * srpt_handle_new_iu - process a newly received information unit 16118c2ecf20Sopenharmony_ci * @ch: RDMA channel through which the information unit has been received. 16128c2ecf20Sopenharmony_ci * @recv_ioctx: Receive I/O context associated with the information unit. 16138c2ecf20Sopenharmony_ci */ 16148c2ecf20Sopenharmony_cistatic bool 16158c2ecf20Sopenharmony_cisrpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx) 16168c2ecf20Sopenharmony_ci{ 16178c2ecf20Sopenharmony_ci struct srpt_send_ioctx *send_ioctx = NULL; 16188c2ecf20Sopenharmony_ci struct srp_cmd *srp_cmd; 16198c2ecf20Sopenharmony_ci bool res = false; 16208c2ecf20Sopenharmony_ci u8 opcode; 16218c2ecf20Sopenharmony_ci 16228c2ecf20Sopenharmony_ci BUG_ON(!ch); 16238c2ecf20Sopenharmony_ci BUG_ON(!recv_ioctx); 16248c2ecf20Sopenharmony_ci 16258c2ecf20Sopenharmony_ci if (unlikely(ch->state == CH_CONNECTING)) 16268c2ecf20Sopenharmony_ci goto push; 16278c2ecf20Sopenharmony_ci 16288c2ecf20Sopenharmony_ci ib_dma_sync_single_for_cpu(ch->sport->sdev->device, 16298c2ecf20Sopenharmony_ci recv_ioctx->ioctx.dma, 16308c2ecf20Sopenharmony_ci recv_ioctx->ioctx.offset + srp_max_req_size, 16318c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 16328c2ecf20Sopenharmony_ci 16338c2ecf20Sopenharmony_ci srp_cmd = recv_ioctx->ioctx.buf + recv_ioctx->ioctx.offset; 16348c2ecf20Sopenharmony_ci opcode = srp_cmd->opcode; 16358c2ecf20Sopenharmony_ci if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) { 16368c2ecf20Sopenharmony_ci send_ioctx = srpt_get_send_ioctx(ch); 16378c2ecf20Sopenharmony_ci if (unlikely(!send_ioctx)) 16388c2ecf20Sopenharmony_ci goto push; 16398c2ecf20Sopenharmony_ci } 16408c2ecf20Sopenharmony_ci 16418c2ecf20Sopenharmony_ci if (!list_empty(&recv_ioctx->wait_list)) { 16428c2ecf20Sopenharmony_ci WARN_ON_ONCE(!ch->processing_wait_list); 16438c2ecf20Sopenharmony_ci list_del_init(&recv_ioctx->wait_list); 16448c2ecf20Sopenharmony_ci } 16458c2ecf20Sopenharmony_ci 16468c2ecf20Sopenharmony_ci switch (opcode) { 16478c2ecf20Sopenharmony_ci case SRP_CMD: 16488c2ecf20Sopenharmony_ci srpt_handle_cmd(ch, recv_ioctx, send_ioctx); 16498c2ecf20Sopenharmony_ci break; 16508c2ecf20Sopenharmony_ci case SRP_TSK_MGMT: 16518c2ecf20Sopenharmony_ci srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx); 16528c2ecf20Sopenharmony_ci break; 16538c2ecf20Sopenharmony_ci case SRP_I_LOGOUT: 16548c2ecf20Sopenharmony_ci pr_err("Not yet implemented: SRP_I_LOGOUT\n"); 16558c2ecf20Sopenharmony_ci break; 16568c2ecf20Sopenharmony_ci case SRP_CRED_RSP: 16578c2ecf20Sopenharmony_ci pr_debug("received SRP_CRED_RSP\n"); 16588c2ecf20Sopenharmony_ci break; 16598c2ecf20Sopenharmony_ci case SRP_AER_RSP: 16608c2ecf20Sopenharmony_ci pr_debug("received SRP_AER_RSP\n"); 16618c2ecf20Sopenharmony_ci break; 16628c2ecf20Sopenharmony_ci case SRP_RSP: 16638c2ecf20Sopenharmony_ci pr_err("Received SRP_RSP\n"); 16648c2ecf20Sopenharmony_ci break; 16658c2ecf20Sopenharmony_ci default: 16668c2ecf20Sopenharmony_ci pr_err("received IU with unknown opcode 0x%x\n", opcode); 16678c2ecf20Sopenharmony_ci break; 16688c2ecf20Sopenharmony_ci } 16698c2ecf20Sopenharmony_ci 16708c2ecf20Sopenharmony_ci if (!send_ioctx || !send_ioctx->recv_ioctx) 16718c2ecf20Sopenharmony_ci srpt_post_recv(ch->sport->sdev, ch, recv_ioctx); 16728c2ecf20Sopenharmony_ci res = true; 16738c2ecf20Sopenharmony_ci 16748c2ecf20Sopenharmony_ciout: 16758c2ecf20Sopenharmony_ci return res; 16768c2ecf20Sopenharmony_ci 16778c2ecf20Sopenharmony_cipush: 16788c2ecf20Sopenharmony_ci if (list_empty(&recv_ioctx->wait_list)) { 16798c2ecf20Sopenharmony_ci WARN_ON_ONCE(ch->processing_wait_list); 16808c2ecf20Sopenharmony_ci list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list); 16818c2ecf20Sopenharmony_ci } 16828c2ecf20Sopenharmony_ci goto out; 16838c2ecf20Sopenharmony_ci} 16848c2ecf20Sopenharmony_ci 16858c2ecf20Sopenharmony_cistatic void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc) 16868c2ecf20Sopenharmony_ci{ 16878c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = wc->qp->qp_context; 16888c2ecf20Sopenharmony_ci struct srpt_recv_ioctx *ioctx = 16898c2ecf20Sopenharmony_ci container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe); 16908c2ecf20Sopenharmony_ci 16918c2ecf20Sopenharmony_ci if (wc->status == IB_WC_SUCCESS) { 16928c2ecf20Sopenharmony_ci int req_lim; 16938c2ecf20Sopenharmony_ci 16948c2ecf20Sopenharmony_ci req_lim = atomic_dec_return(&ch->req_lim); 16958c2ecf20Sopenharmony_ci if (unlikely(req_lim < 0)) 16968c2ecf20Sopenharmony_ci pr_err("req_lim = %d < 0\n", req_lim); 16978c2ecf20Sopenharmony_ci ioctx->byte_len = wc->byte_len; 16988c2ecf20Sopenharmony_ci srpt_handle_new_iu(ch, ioctx); 16998c2ecf20Sopenharmony_ci } else { 17008c2ecf20Sopenharmony_ci pr_info_ratelimited("receiving failed for ioctx %p with status %d\n", 17018c2ecf20Sopenharmony_ci ioctx, wc->status); 17028c2ecf20Sopenharmony_ci } 17038c2ecf20Sopenharmony_ci} 17048c2ecf20Sopenharmony_ci 17058c2ecf20Sopenharmony_ci/* 17068c2ecf20Sopenharmony_ci * This function must be called from the context in which RDMA completions are 17078c2ecf20Sopenharmony_ci * processed because it accesses the wait list without protection against 17088c2ecf20Sopenharmony_ci * access from other threads. 17098c2ecf20Sopenharmony_ci */ 17108c2ecf20Sopenharmony_cistatic void srpt_process_wait_list(struct srpt_rdma_ch *ch) 17118c2ecf20Sopenharmony_ci{ 17128c2ecf20Sopenharmony_ci struct srpt_recv_ioctx *recv_ioctx, *tmp; 17138c2ecf20Sopenharmony_ci 17148c2ecf20Sopenharmony_ci WARN_ON_ONCE(ch->state == CH_CONNECTING); 17158c2ecf20Sopenharmony_ci 17168c2ecf20Sopenharmony_ci if (list_empty(&ch->cmd_wait_list)) 17178c2ecf20Sopenharmony_ci return; 17188c2ecf20Sopenharmony_ci 17198c2ecf20Sopenharmony_ci WARN_ON_ONCE(ch->processing_wait_list); 17208c2ecf20Sopenharmony_ci ch->processing_wait_list = true; 17218c2ecf20Sopenharmony_ci list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list, 17228c2ecf20Sopenharmony_ci wait_list) { 17238c2ecf20Sopenharmony_ci if (!srpt_handle_new_iu(ch, recv_ioctx)) 17248c2ecf20Sopenharmony_ci break; 17258c2ecf20Sopenharmony_ci } 17268c2ecf20Sopenharmony_ci ch->processing_wait_list = false; 17278c2ecf20Sopenharmony_ci} 17288c2ecf20Sopenharmony_ci 17298c2ecf20Sopenharmony_ci/** 17308c2ecf20Sopenharmony_ci * srpt_send_done - send completion callback 17318c2ecf20Sopenharmony_ci * @cq: Completion queue. 17328c2ecf20Sopenharmony_ci * @wc: Work completion. 17338c2ecf20Sopenharmony_ci * 17348c2ecf20Sopenharmony_ci * Note: Although this has not yet been observed during tests, at least in 17358c2ecf20Sopenharmony_ci * theory it is possible that the srpt_get_send_ioctx() call invoked by 17368c2ecf20Sopenharmony_ci * srpt_handle_new_iu() fails. This is possible because the req_lim_delta 17378c2ecf20Sopenharmony_ci * value in each response is set to one, and it is possible that this response 17388c2ecf20Sopenharmony_ci * makes the initiator send a new request before the send completion for that 17398c2ecf20Sopenharmony_ci * response has been processed. This could e.g. happen if the call to 17408c2ecf20Sopenharmony_ci * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or 17418c2ecf20Sopenharmony_ci * if IB retransmission causes generation of the send completion to be 17428c2ecf20Sopenharmony_ci * delayed. Incoming information units for which srpt_get_send_ioctx() fails 17438c2ecf20Sopenharmony_ci * are queued on cmd_wait_list. The code below processes these delayed 17448c2ecf20Sopenharmony_ci * requests one at a time. 17458c2ecf20Sopenharmony_ci */ 17468c2ecf20Sopenharmony_cistatic void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc) 17478c2ecf20Sopenharmony_ci{ 17488c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = wc->qp->qp_context; 17498c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = 17508c2ecf20Sopenharmony_ci container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe); 17518c2ecf20Sopenharmony_ci enum srpt_command_state state; 17528c2ecf20Sopenharmony_ci 17538c2ecf20Sopenharmony_ci state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); 17548c2ecf20Sopenharmony_ci 17558c2ecf20Sopenharmony_ci WARN_ON(state != SRPT_STATE_CMD_RSP_SENT && 17568c2ecf20Sopenharmony_ci state != SRPT_STATE_MGMT_RSP_SENT); 17578c2ecf20Sopenharmony_ci 17588c2ecf20Sopenharmony_ci atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail); 17598c2ecf20Sopenharmony_ci 17608c2ecf20Sopenharmony_ci if (wc->status != IB_WC_SUCCESS) 17618c2ecf20Sopenharmony_ci pr_info("sending response for ioctx 0x%p failed with status %d\n", 17628c2ecf20Sopenharmony_ci ioctx, wc->status); 17638c2ecf20Sopenharmony_ci 17648c2ecf20Sopenharmony_ci if (state != SRPT_STATE_DONE) { 17658c2ecf20Sopenharmony_ci transport_generic_free_cmd(&ioctx->cmd, 0); 17668c2ecf20Sopenharmony_ci } else { 17678c2ecf20Sopenharmony_ci pr_err("IB completion has been received too late for wr_id = %u.\n", 17688c2ecf20Sopenharmony_ci ioctx->ioctx.index); 17698c2ecf20Sopenharmony_ci } 17708c2ecf20Sopenharmony_ci 17718c2ecf20Sopenharmony_ci srpt_process_wait_list(ch); 17728c2ecf20Sopenharmony_ci} 17738c2ecf20Sopenharmony_ci 17748c2ecf20Sopenharmony_ci/** 17758c2ecf20Sopenharmony_ci * srpt_create_ch_ib - create receive and send completion queues 17768c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 17778c2ecf20Sopenharmony_ci */ 17788c2ecf20Sopenharmony_cistatic int srpt_create_ch_ib(struct srpt_rdma_ch *ch) 17798c2ecf20Sopenharmony_ci{ 17808c2ecf20Sopenharmony_ci struct ib_qp_init_attr *qp_init; 17818c2ecf20Sopenharmony_ci struct srpt_port *sport = ch->sport; 17828c2ecf20Sopenharmony_ci struct srpt_device *sdev = sport->sdev; 17838c2ecf20Sopenharmony_ci const struct ib_device_attr *attrs = &sdev->device->attrs; 17848c2ecf20Sopenharmony_ci int sq_size = sport->port_attrib.srp_sq_size; 17858c2ecf20Sopenharmony_ci int i, ret; 17868c2ecf20Sopenharmony_ci 17878c2ecf20Sopenharmony_ci WARN_ON(ch->rq_size < 1); 17888c2ecf20Sopenharmony_ci 17898c2ecf20Sopenharmony_ci ret = -ENOMEM; 17908c2ecf20Sopenharmony_ci qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL); 17918c2ecf20Sopenharmony_ci if (!qp_init) 17928c2ecf20Sopenharmony_ci goto out; 17938c2ecf20Sopenharmony_ci 17948c2ecf20Sopenharmony_ciretry: 17958c2ecf20Sopenharmony_ci ch->cq = ib_cq_pool_get(sdev->device, ch->rq_size + sq_size, -1, 17968c2ecf20Sopenharmony_ci IB_POLL_WORKQUEUE); 17978c2ecf20Sopenharmony_ci if (IS_ERR(ch->cq)) { 17988c2ecf20Sopenharmony_ci ret = PTR_ERR(ch->cq); 17998c2ecf20Sopenharmony_ci pr_err("failed to create CQ cqe= %d ret= %d\n", 18008c2ecf20Sopenharmony_ci ch->rq_size + sq_size, ret); 18018c2ecf20Sopenharmony_ci goto out; 18028c2ecf20Sopenharmony_ci } 18038c2ecf20Sopenharmony_ci ch->cq_size = ch->rq_size + sq_size; 18048c2ecf20Sopenharmony_ci 18058c2ecf20Sopenharmony_ci qp_init->qp_context = (void *)ch; 18068c2ecf20Sopenharmony_ci qp_init->event_handler 18078c2ecf20Sopenharmony_ci = (void(*)(struct ib_event *, void*))srpt_qp_event; 18088c2ecf20Sopenharmony_ci qp_init->send_cq = ch->cq; 18098c2ecf20Sopenharmony_ci qp_init->recv_cq = ch->cq; 18108c2ecf20Sopenharmony_ci qp_init->sq_sig_type = IB_SIGNAL_REQ_WR; 18118c2ecf20Sopenharmony_ci qp_init->qp_type = IB_QPT_RC; 18128c2ecf20Sopenharmony_ci /* 18138c2ecf20Sopenharmony_ci * We divide up our send queue size into half SEND WRs to send the 18148c2ecf20Sopenharmony_ci * completions, and half R/W contexts to actually do the RDMA 18158c2ecf20Sopenharmony_ci * READ/WRITE transfers. Note that we need to allocate CQ slots for 18168c2ecf20Sopenharmony_ci * both both, as RDMA contexts will also post completions for the 18178c2ecf20Sopenharmony_ci * RDMA READ case. 18188c2ecf20Sopenharmony_ci */ 18198c2ecf20Sopenharmony_ci qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr); 18208c2ecf20Sopenharmony_ci qp_init->cap.max_rdma_ctxs = sq_size / 2; 18218c2ecf20Sopenharmony_ci qp_init->cap.max_send_sge = attrs->max_send_sge; 18228c2ecf20Sopenharmony_ci qp_init->cap.max_recv_sge = 1; 18238c2ecf20Sopenharmony_ci qp_init->port_num = ch->sport->port; 18248c2ecf20Sopenharmony_ci if (sdev->use_srq) 18258c2ecf20Sopenharmony_ci qp_init->srq = sdev->srq; 18268c2ecf20Sopenharmony_ci else 18278c2ecf20Sopenharmony_ci qp_init->cap.max_recv_wr = ch->rq_size; 18288c2ecf20Sopenharmony_ci 18298c2ecf20Sopenharmony_ci if (ch->using_rdma_cm) { 18308c2ecf20Sopenharmony_ci ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init); 18318c2ecf20Sopenharmony_ci ch->qp = ch->rdma_cm.cm_id->qp; 18328c2ecf20Sopenharmony_ci } else { 18338c2ecf20Sopenharmony_ci ch->qp = ib_create_qp(sdev->pd, qp_init); 18348c2ecf20Sopenharmony_ci if (!IS_ERR(ch->qp)) { 18358c2ecf20Sopenharmony_ci ret = srpt_init_ch_qp(ch, ch->qp); 18368c2ecf20Sopenharmony_ci if (ret) 18378c2ecf20Sopenharmony_ci ib_destroy_qp(ch->qp); 18388c2ecf20Sopenharmony_ci } else { 18398c2ecf20Sopenharmony_ci ret = PTR_ERR(ch->qp); 18408c2ecf20Sopenharmony_ci } 18418c2ecf20Sopenharmony_ci } 18428c2ecf20Sopenharmony_ci if (ret) { 18438c2ecf20Sopenharmony_ci bool retry = sq_size > MIN_SRPT_SQ_SIZE; 18448c2ecf20Sopenharmony_ci 18458c2ecf20Sopenharmony_ci if (retry) { 18468c2ecf20Sopenharmony_ci pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n", 18478c2ecf20Sopenharmony_ci sq_size, ret); 18488c2ecf20Sopenharmony_ci ib_cq_pool_put(ch->cq, ch->cq_size); 18498c2ecf20Sopenharmony_ci sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE); 18508c2ecf20Sopenharmony_ci goto retry; 18518c2ecf20Sopenharmony_ci } else { 18528c2ecf20Sopenharmony_ci pr_err("failed to create queue pair with sq_size = %d (%d)\n", 18538c2ecf20Sopenharmony_ci sq_size, ret); 18548c2ecf20Sopenharmony_ci goto err_destroy_cq; 18558c2ecf20Sopenharmony_ci } 18568c2ecf20Sopenharmony_ci } 18578c2ecf20Sopenharmony_ci 18588c2ecf20Sopenharmony_ci atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr); 18598c2ecf20Sopenharmony_ci 18608c2ecf20Sopenharmony_ci pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n", 18618c2ecf20Sopenharmony_ci __func__, ch->cq->cqe, qp_init->cap.max_send_sge, 18628c2ecf20Sopenharmony_ci qp_init->cap.max_send_wr, ch); 18638c2ecf20Sopenharmony_ci 18648c2ecf20Sopenharmony_ci if (!sdev->use_srq) 18658c2ecf20Sopenharmony_ci for (i = 0; i < ch->rq_size; i++) 18668c2ecf20Sopenharmony_ci srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]); 18678c2ecf20Sopenharmony_ci 18688c2ecf20Sopenharmony_ciout: 18698c2ecf20Sopenharmony_ci kfree(qp_init); 18708c2ecf20Sopenharmony_ci return ret; 18718c2ecf20Sopenharmony_ci 18728c2ecf20Sopenharmony_cierr_destroy_cq: 18738c2ecf20Sopenharmony_ci ch->qp = NULL; 18748c2ecf20Sopenharmony_ci ib_cq_pool_put(ch->cq, ch->cq_size); 18758c2ecf20Sopenharmony_ci goto out; 18768c2ecf20Sopenharmony_ci} 18778c2ecf20Sopenharmony_ci 18788c2ecf20Sopenharmony_cistatic void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch) 18798c2ecf20Sopenharmony_ci{ 18808c2ecf20Sopenharmony_ci ib_destroy_qp(ch->qp); 18818c2ecf20Sopenharmony_ci ib_cq_pool_put(ch->cq, ch->cq_size); 18828c2ecf20Sopenharmony_ci} 18838c2ecf20Sopenharmony_ci 18848c2ecf20Sopenharmony_ci/** 18858c2ecf20Sopenharmony_ci * srpt_close_ch - close a RDMA channel 18868c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 18878c2ecf20Sopenharmony_ci * 18888c2ecf20Sopenharmony_ci * Make sure all resources associated with the channel will be deallocated at 18898c2ecf20Sopenharmony_ci * an appropriate time. 18908c2ecf20Sopenharmony_ci * 18918c2ecf20Sopenharmony_ci * Returns true if and only if the channel state has been modified into 18928c2ecf20Sopenharmony_ci * CH_DRAINING. 18938c2ecf20Sopenharmony_ci */ 18948c2ecf20Sopenharmony_cistatic bool srpt_close_ch(struct srpt_rdma_ch *ch) 18958c2ecf20Sopenharmony_ci{ 18968c2ecf20Sopenharmony_ci int ret; 18978c2ecf20Sopenharmony_ci 18988c2ecf20Sopenharmony_ci if (!srpt_set_ch_state(ch, CH_DRAINING)) { 18998c2ecf20Sopenharmony_ci pr_debug("%s: already closed\n", ch->sess_name); 19008c2ecf20Sopenharmony_ci return false; 19018c2ecf20Sopenharmony_ci } 19028c2ecf20Sopenharmony_ci 19038c2ecf20Sopenharmony_ci kref_get(&ch->kref); 19048c2ecf20Sopenharmony_ci 19058c2ecf20Sopenharmony_ci ret = srpt_ch_qp_err(ch); 19068c2ecf20Sopenharmony_ci if (ret < 0) 19078c2ecf20Sopenharmony_ci pr_err("%s-%d: changing queue pair into error state failed: %d\n", 19088c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, ret); 19098c2ecf20Sopenharmony_ci 19108c2ecf20Sopenharmony_ci ret = srpt_zerolength_write(ch); 19118c2ecf20Sopenharmony_ci if (ret < 0) { 19128c2ecf20Sopenharmony_ci pr_err("%s-%d: queuing zero-length write failed: %d\n", 19138c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, ret); 19148c2ecf20Sopenharmony_ci if (srpt_set_ch_state(ch, CH_DISCONNECTED)) 19158c2ecf20Sopenharmony_ci schedule_work(&ch->release_work); 19168c2ecf20Sopenharmony_ci else 19178c2ecf20Sopenharmony_ci WARN_ON_ONCE(true); 19188c2ecf20Sopenharmony_ci } 19198c2ecf20Sopenharmony_ci 19208c2ecf20Sopenharmony_ci kref_put(&ch->kref, srpt_free_ch); 19218c2ecf20Sopenharmony_ci 19228c2ecf20Sopenharmony_ci return true; 19238c2ecf20Sopenharmony_ci} 19248c2ecf20Sopenharmony_ci 19258c2ecf20Sopenharmony_ci/* 19268c2ecf20Sopenharmony_ci * Change the channel state into CH_DISCONNECTING. If a channel has not yet 19278c2ecf20Sopenharmony_ci * reached the connected state, close it. If a channel is in the connected 19288c2ecf20Sopenharmony_ci * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is 19298c2ecf20Sopenharmony_ci * the responsibility of the caller to ensure that this function is not 19308c2ecf20Sopenharmony_ci * invoked concurrently with the code that accepts a connection. This means 19318c2ecf20Sopenharmony_ci * that this function must either be invoked from inside a CM callback 19328c2ecf20Sopenharmony_ci * function or that it must be invoked with the srpt_port.mutex held. 19338c2ecf20Sopenharmony_ci */ 19348c2ecf20Sopenharmony_cistatic int srpt_disconnect_ch(struct srpt_rdma_ch *ch) 19358c2ecf20Sopenharmony_ci{ 19368c2ecf20Sopenharmony_ci int ret; 19378c2ecf20Sopenharmony_ci 19388c2ecf20Sopenharmony_ci if (!srpt_set_ch_state(ch, CH_DISCONNECTING)) 19398c2ecf20Sopenharmony_ci return -ENOTCONN; 19408c2ecf20Sopenharmony_ci 19418c2ecf20Sopenharmony_ci if (ch->using_rdma_cm) { 19428c2ecf20Sopenharmony_ci ret = rdma_disconnect(ch->rdma_cm.cm_id); 19438c2ecf20Sopenharmony_ci } else { 19448c2ecf20Sopenharmony_ci ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0); 19458c2ecf20Sopenharmony_ci if (ret < 0) 19468c2ecf20Sopenharmony_ci ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0); 19478c2ecf20Sopenharmony_ci } 19488c2ecf20Sopenharmony_ci 19498c2ecf20Sopenharmony_ci if (ret < 0 && srpt_close_ch(ch)) 19508c2ecf20Sopenharmony_ci ret = 0; 19518c2ecf20Sopenharmony_ci 19528c2ecf20Sopenharmony_ci return ret; 19538c2ecf20Sopenharmony_ci} 19548c2ecf20Sopenharmony_ci 19558c2ecf20Sopenharmony_ci/* Send DREQ and wait for DREP. */ 19568c2ecf20Sopenharmony_cistatic void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch) 19578c2ecf20Sopenharmony_ci{ 19588c2ecf20Sopenharmony_ci DECLARE_COMPLETION_ONSTACK(closed); 19598c2ecf20Sopenharmony_ci struct srpt_port *sport = ch->sport; 19608c2ecf20Sopenharmony_ci 19618c2ecf20Sopenharmony_ci pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num, 19628c2ecf20Sopenharmony_ci ch->state); 19638c2ecf20Sopenharmony_ci 19648c2ecf20Sopenharmony_ci ch->closed = &closed; 19658c2ecf20Sopenharmony_ci 19668c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 19678c2ecf20Sopenharmony_ci srpt_disconnect_ch(ch); 19688c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 19698c2ecf20Sopenharmony_ci 19708c2ecf20Sopenharmony_ci while (wait_for_completion_timeout(&closed, 5 * HZ) == 0) 19718c2ecf20Sopenharmony_ci pr_info("%s(%s-%d state %d): still waiting ...\n", __func__, 19728c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, ch->state); 19738c2ecf20Sopenharmony_ci 19748c2ecf20Sopenharmony_ci} 19758c2ecf20Sopenharmony_ci 19768c2ecf20Sopenharmony_cistatic void __srpt_close_all_ch(struct srpt_port *sport) 19778c2ecf20Sopenharmony_ci{ 19788c2ecf20Sopenharmony_ci struct srpt_nexus *nexus; 19798c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch; 19808c2ecf20Sopenharmony_ci 19818c2ecf20Sopenharmony_ci lockdep_assert_held(&sport->mutex); 19828c2ecf20Sopenharmony_ci 19838c2ecf20Sopenharmony_ci list_for_each_entry(nexus, &sport->nexus_list, entry) { 19848c2ecf20Sopenharmony_ci list_for_each_entry(ch, &nexus->ch_list, list) { 19858c2ecf20Sopenharmony_ci if (srpt_disconnect_ch(ch) >= 0) 19868c2ecf20Sopenharmony_ci pr_info("Closing channel %s-%d because target %s_%d has been disabled\n", 19878c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, 19888c2ecf20Sopenharmony_ci dev_name(&sport->sdev->device->dev), 19898c2ecf20Sopenharmony_ci sport->port); 19908c2ecf20Sopenharmony_ci srpt_close_ch(ch); 19918c2ecf20Sopenharmony_ci } 19928c2ecf20Sopenharmony_ci } 19938c2ecf20Sopenharmony_ci} 19948c2ecf20Sopenharmony_ci 19958c2ecf20Sopenharmony_ci/* 19968c2ecf20Sopenharmony_ci * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if 19978c2ecf20Sopenharmony_ci * it does not yet exist. 19988c2ecf20Sopenharmony_ci */ 19998c2ecf20Sopenharmony_cistatic struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport, 20008c2ecf20Sopenharmony_ci const u8 i_port_id[16], 20018c2ecf20Sopenharmony_ci const u8 t_port_id[16]) 20028c2ecf20Sopenharmony_ci{ 20038c2ecf20Sopenharmony_ci struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n; 20048c2ecf20Sopenharmony_ci 20058c2ecf20Sopenharmony_ci for (;;) { 20068c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 20078c2ecf20Sopenharmony_ci list_for_each_entry(n, &sport->nexus_list, entry) { 20088c2ecf20Sopenharmony_ci if (memcmp(n->i_port_id, i_port_id, 16) == 0 && 20098c2ecf20Sopenharmony_ci memcmp(n->t_port_id, t_port_id, 16) == 0) { 20108c2ecf20Sopenharmony_ci nexus = n; 20118c2ecf20Sopenharmony_ci break; 20128c2ecf20Sopenharmony_ci } 20138c2ecf20Sopenharmony_ci } 20148c2ecf20Sopenharmony_ci if (!nexus && tmp_nexus) { 20158c2ecf20Sopenharmony_ci list_add_tail_rcu(&tmp_nexus->entry, 20168c2ecf20Sopenharmony_ci &sport->nexus_list); 20178c2ecf20Sopenharmony_ci swap(nexus, tmp_nexus); 20188c2ecf20Sopenharmony_ci } 20198c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 20208c2ecf20Sopenharmony_ci 20218c2ecf20Sopenharmony_ci if (nexus) 20228c2ecf20Sopenharmony_ci break; 20238c2ecf20Sopenharmony_ci tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL); 20248c2ecf20Sopenharmony_ci if (!tmp_nexus) { 20258c2ecf20Sopenharmony_ci nexus = ERR_PTR(-ENOMEM); 20268c2ecf20Sopenharmony_ci break; 20278c2ecf20Sopenharmony_ci } 20288c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&tmp_nexus->ch_list); 20298c2ecf20Sopenharmony_ci memcpy(tmp_nexus->i_port_id, i_port_id, 16); 20308c2ecf20Sopenharmony_ci memcpy(tmp_nexus->t_port_id, t_port_id, 16); 20318c2ecf20Sopenharmony_ci } 20328c2ecf20Sopenharmony_ci 20338c2ecf20Sopenharmony_ci kfree(tmp_nexus); 20348c2ecf20Sopenharmony_ci 20358c2ecf20Sopenharmony_ci return nexus; 20368c2ecf20Sopenharmony_ci} 20378c2ecf20Sopenharmony_ci 20388c2ecf20Sopenharmony_cistatic void srpt_set_enabled(struct srpt_port *sport, bool enabled) 20398c2ecf20Sopenharmony_ci __must_hold(&sport->mutex) 20408c2ecf20Sopenharmony_ci{ 20418c2ecf20Sopenharmony_ci lockdep_assert_held(&sport->mutex); 20428c2ecf20Sopenharmony_ci 20438c2ecf20Sopenharmony_ci if (sport->enabled == enabled) 20448c2ecf20Sopenharmony_ci return; 20458c2ecf20Sopenharmony_ci sport->enabled = enabled; 20468c2ecf20Sopenharmony_ci if (!enabled) 20478c2ecf20Sopenharmony_ci __srpt_close_all_ch(sport); 20488c2ecf20Sopenharmony_ci} 20498c2ecf20Sopenharmony_ci 20508c2ecf20Sopenharmony_cistatic void srpt_drop_sport_ref(struct srpt_port *sport) 20518c2ecf20Sopenharmony_ci{ 20528c2ecf20Sopenharmony_ci if (atomic_dec_return(&sport->refcount) == 0 && sport->freed_channels) 20538c2ecf20Sopenharmony_ci complete(sport->freed_channels); 20548c2ecf20Sopenharmony_ci} 20558c2ecf20Sopenharmony_ci 20568c2ecf20Sopenharmony_cistatic void srpt_free_ch(struct kref *kref) 20578c2ecf20Sopenharmony_ci{ 20588c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref); 20598c2ecf20Sopenharmony_ci 20608c2ecf20Sopenharmony_ci srpt_drop_sport_ref(ch->sport); 20618c2ecf20Sopenharmony_ci kfree_rcu(ch, rcu); 20628c2ecf20Sopenharmony_ci} 20638c2ecf20Sopenharmony_ci 20648c2ecf20Sopenharmony_ci/* 20658c2ecf20Sopenharmony_ci * Shut down the SCSI target session, tell the connection manager to 20668c2ecf20Sopenharmony_ci * disconnect the associated RDMA channel, transition the QP to the error 20678c2ecf20Sopenharmony_ci * state and remove the channel from the channel list. This function is 20688c2ecf20Sopenharmony_ci * typically called from inside srpt_zerolength_write_done(). Concurrent 20698c2ecf20Sopenharmony_ci * srpt_zerolength_write() calls from inside srpt_close_ch() are possible 20708c2ecf20Sopenharmony_ci * as long as the channel is on sport->nexus_list. 20718c2ecf20Sopenharmony_ci */ 20728c2ecf20Sopenharmony_cistatic void srpt_release_channel_work(struct work_struct *w) 20738c2ecf20Sopenharmony_ci{ 20748c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch; 20758c2ecf20Sopenharmony_ci struct srpt_device *sdev; 20768c2ecf20Sopenharmony_ci struct srpt_port *sport; 20778c2ecf20Sopenharmony_ci struct se_session *se_sess; 20788c2ecf20Sopenharmony_ci 20798c2ecf20Sopenharmony_ci ch = container_of(w, struct srpt_rdma_ch, release_work); 20808c2ecf20Sopenharmony_ci pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num); 20818c2ecf20Sopenharmony_ci 20828c2ecf20Sopenharmony_ci sdev = ch->sport->sdev; 20838c2ecf20Sopenharmony_ci BUG_ON(!sdev); 20848c2ecf20Sopenharmony_ci 20858c2ecf20Sopenharmony_ci se_sess = ch->sess; 20868c2ecf20Sopenharmony_ci BUG_ON(!se_sess); 20878c2ecf20Sopenharmony_ci 20888c2ecf20Sopenharmony_ci target_sess_cmd_list_set_waiting(se_sess); 20898c2ecf20Sopenharmony_ci target_wait_for_sess_cmds(se_sess); 20908c2ecf20Sopenharmony_ci 20918c2ecf20Sopenharmony_ci target_remove_session(se_sess); 20928c2ecf20Sopenharmony_ci ch->sess = NULL; 20938c2ecf20Sopenharmony_ci 20948c2ecf20Sopenharmony_ci if (ch->using_rdma_cm) 20958c2ecf20Sopenharmony_ci rdma_destroy_id(ch->rdma_cm.cm_id); 20968c2ecf20Sopenharmony_ci else 20978c2ecf20Sopenharmony_ci ib_destroy_cm_id(ch->ib_cm.cm_id); 20988c2ecf20Sopenharmony_ci 20998c2ecf20Sopenharmony_ci sport = ch->sport; 21008c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 21018c2ecf20Sopenharmony_ci list_del_rcu(&ch->list); 21028c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 21038c2ecf20Sopenharmony_ci 21048c2ecf20Sopenharmony_ci if (ch->closed) 21058c2ecf20Sopenharmony_ci complete(ch->closed); 21068c2ecf20Sopenharmony_ci 21078c2ecf20Sopenharmony_ci srpt_destroy_ch_ib(ch); 21088c2ecf20Sopenharmony_ci 21098c2ecf20Sopenharmony_ci srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, 21108c2ecf20Sopenharmony_ci ch->sport->sdev, ch->rq_size, 21118c2ecf20Sopenharmony_ci ch->rsp_buf_cache, DMA_TO_DEVICE); 21128c2ecf20Sopenharmony_ci 21138c2ecf20Sopenharmony_ci kmem_cache_destroy(ch->rsp_buf_cache); 21148c2ecf20Sopenharmony_ci 21158c2ecf20Sopenharmony_ci srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring, 21168c2ecf20Sopenharmony_ci sdev, ch->rq_size, 21178c2ecf20Sopenharmony_ci ch->req_buf_cache, DMA_FROM_DEVICE); 21188c2ecf20Sopenharmony_ci 21198c2ecf20Sopenharmony_ci kmem_cache_destroy(ch->req_buf_cache); 21208c2ecf20Sopenharmony_ci 21218c2ecf20Sopenharmony_ci kref_put(&ch->kref, srpt_free_ch); 21228c2ecf20Sopenharmony_ci} 21238c2ecf20Sopenharmony_ci 21248c2ecf20Sopenharmony_ci/** 21258c2ecf20Sopenharmony_ci * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED 21268c2ecf20Sopenharmony_ci * @sdev: HCA through which the login request was received. 21278c2ecf20Sopenharmony_ci * @ib_cm_id: IB/CM connection identifier in case of IB/CM. 21288c2ecf20Sopenharmony_ci * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM. 21298c2ecf20Sopenharmony_ci * @port_num: Port through which the REQ message was received. 21308c2ecf20Sopenharmony_ci * @pkey: P_Key of the incoming connection. 21318c2ecf20Sopenharmony_ci * @req: SRP login request. 21328c2ecf20Sopenharmony_ci * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted 21338c2ecf20Sopenharmony_ci * the login request. 21348c2ecf20Sopenharmony_ci * 21358c2ecf20Sopenharmony_ci * Ownership of the cm_id is transferred to the target session if this 21368c2ecf20Sopenharmony_ci * function returns zero. Otherwise the caller remains the owner of cm_id. 21378c2ecf20Sopenharmony_ci */ 21388c2ecf20Sopenharmony_cistatic int srpt_cm_req_recv(struct srpt_device *const sdev, 21398c2ecf20Sopenharmony_ci struct ib_cm_id *ib_cm_id, 21408c2ecf20Sopenharmony_ci struct rdma_cm_id *rdma_cm_id, 21418c2ecf20Sopenharmony_ci u8 port_num, __be16 pkey, 21428c2ecf20Sopenharmony_ci const struct srp_login_req *req, 21438c2ecf20Sopenharmony_ci const char *src_addr) 21448c2ecf20Sopenharmony_ci{ 21458c2ecf20Sopenharmony_ci struct srpt_port *sport = &sdev->port[port_num - 1]; 21468c2ecf20Sopenharmony_ci struct srpt_nexus *nexus; 21478c2ecf20Sopenharmony_ci struct srp_login_rsp *rsp = NULL; 21488c2ecf20Sopenharmony_ci struct srp_login_rej *rej = NULL; 21498c2ecf20Sopenharmony_ci union { 21508c2ecf20Sopenharmony_ci struct rdma_conn_param rdma_cm; 21518c2ecf20Sopenharmony_ci struct ib_cm_rep_param ib_cm; 21528c2ecf20Sopenharmony_ci } *rep_param = NULL; 21538c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = NULL; 21548c2ecf20Sopenharmony_ci char i_port_id[36]; 21558c2ecf20Sopenharmony_ci u32 it_iu_len; 21568c2ecf20Sopenharmony_ci int i, tag_num, tag_size, ret; 21578c2ecf20Sopenharmony_ci struct srpt_tpg *stpg; 21588c2ecf20Sopenharmony_ci 21598c2ecf20Sopenharmony_ci WARN_ON_ONCE(irqs_disabled()); 21608c2ecf20Sopenharmony_ci 21618c2ecf20Sopenharmony_ci it_iu_len = be32_to_cpu(req->req_it_iu_len); 21628c2ecf20Sopenharmony_ci 21638c2ecf20Sopenharmony_ci 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", 21648c2ecf20Sopenharmony_ci req->initiator_port_id, req->target_port_id, it_iu_len, 21658c2ecf20Sopenharmony_ci port_num, &sport->gid, be16_to_cpu(pkey)); 21668c2ecf20Sopenharmony_ci 21678c2ecf20Sopenharmony_ci nexus = srpt_get_nexus(sport, req->initiator_port_id, 21688c2ecf20Sopenharmony_ci req->target_port_id); 21698c2ecf20Sopenharmony_ci if (IS_ERR(nexus)) { 21708c2ecf20Sopenharmony_ci ret = PTR_ERR(nexus); 21718c2ecf20Sopenharmony_ci goto out; 21728c2ecf20Sopenharmony_ci } 21738c2ecf20Sopenharmony_ci 21748c2ecf20Sopenharmony_ci ret = -ENOMEM; 21758c2ecf20Sopenharmony_ci rsp = kzalloc(sizeof(*rsp), GFP_KERNEL); 21768c2ecf20Sopenharmony_ci rej = kzalloc(sizeof(*rej), GFP_KERNEL); 21778c2ecf20Sopenharmony_ci rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL); 21788c2ecf20Sopenharmony_ci if (!rsp || !rej || !rep_param) 21798c2ecf20Sopenharmony_ci goto out; 21808c2ecf20Sopenharmony_ci 21818c2ecf20Sopenharmony_ci ret = -EINVAL; 21828c2ecf20Sopenharmony_ci if (it_iu_len > srp_max_req_size || it_iu_len < 64) { 21838c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32( 21848c2ecf20Sopenharmony_ci SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE); 21858c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n", 21868c2ecf20Sopenharmony_ci it_iu_len, 64, srp_max_req_size); 21878c2ecf20Sopenharmony_ci goto reject; 21888c2ecf20Sopenharmony_ci } 21898c2ecf20Sopenharmony_ci 21908c2ecf20Sopenharmony_ci if (!sport->enabled) { 21918c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 21928c2ecf20Sopenharmony_ci pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n", 21938c2ecf20Sopenharmony_ci dev_name(&sport->sdev->device->dev), port_num); 21948c2ecf20Sopenharmony_ci goto reject; 21958c2ecf20Sopenharmony_ci } 21968c2ecf20Sopenharmony_ci 21978c2ecf20Sopenharmony_ci if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid) 21988c2ecf20Sopenharmony_ci || *(__be64 *)(req->target_port_id + 8) != 21998c2ecf20Sopenharmony_ci cpu_to_be64(srpt_service_guid)) { 22008c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32( 22018c2ecf20Sopenharmony_ci SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL); 22028c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n"); 22038c2ecf20Sopenharmony_ci goto reject; 22048c2ecf20Sopenharmony_ci } 22058c2ecf20Sopenharmony_ci 22068c2ecf20Sopenharmony_ci ret = -ENOMEM; 22078c2ecf20Sopenharmony_ci ch = kzalloc(sizeof(*ch), GFP_KERNEL); 22088c2ecf20Sopenharmony_ci if (!ch) { 22098c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 22108c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because out of memory.\n"); 22118c2ecf20Sopenharmony_ci goto reject; 22128c2ecf20Sopenharmony_ci } 22138c2ecf20Sopenharmony_ci 22148c2ecf20Sopenharmony_ci kref_init(&ch->kref); 22158c2ecf20Sopenharmony_ci ch->pkey = be16_to_cpu(pkey); 22168c2ecf20Sopenharmony_ci ch->nexus = nexus; 22178c2ecf20Sopenharmony_ci ch->zw_cqe.done = srpt_zerolength_write_done; 22188c2ecf20Sopenharmony_ci INIT_WORK(&ch->release_work, srpt_release_channel_work); 22198c2ecf20Sopenharmony_ci ch->sport = sport; 22208c2ecf20Sopenharmony_ci if (ib_cm_id) { 22218c2ecf20Sopenharmony_ci ch->ib_cm.cm_id = ib_cm_id; 22228c2ecf20Sopenharmony_ci ib_cm_id->context = ch; 22238c2ecf20Sopenharmony_ci } else { 22248c2ecf20Sopenharmony_ci ch->using_rdma_cm = true; 22258c2ecf20Sopenharmony_ci ch->rdma_cm.cm_id = rdma_cm_id; 22268c2ecf20Sopenharmony_ci rdma_cm_id->context = ch; 22278c2ecf20Sopenharmony_ci } 22288c2ecf20Sopenharmony_ci /* 22298c2ecf20Sopenharmony_ci * ch->rq_size should be at least as large as the initiator queue 22308c2ecf20Sopenharmony_ci * depth to avoid that the initiator driver has to report QUEUE_FULL 22318c2ecf20Sopenharmony_ci * to the SCSI mid-layer. 22328c2ecf20Sopenharmony_ci */ 22338c2ecf20Sopenharmony_ci ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr); 22348c2ecf20Sopenharmony_ci spin_lock_init(&ch->spinlock); 22358c2ecf20Sopenharmony_ci ch->state = CH_CONNECTING; 22368c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ch->cmd_wait_list); 22378c2ecf20Sopenharmony_ci ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size; 22388c2ecf20Sopenharmony_ci 22398c2ecf20Sopenharmony_ci ch->rsp_buf_cache = kmem_cache_create("srpt-rsp-buf", ch->max_rsp_size, 22408c2ecf20Sopenharmony_ci 512, 0, NULL); 22418c2ecf20Sopenharmony_ci if (!ch->rsp_buf_cache) 22428c2ecf20Sopenharmony_ci goto free_ch; 22438c2ecf20Sopenharmony_ci 22448c2ecf20Sopenharmony_ci ch->ioctx_ring = (struct srpt_send_ioctx **) 22458c2ecf20Sopenharmony_ci srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, 22468c2ecf20Sopenharmony_ci sizeof(*ch->ioctx_ring[0]), 22478c2ecf20Sopenharmony_ci ch->rsp_buf_cache, 0, DMA_TO_DEVICE); 22488c2ecf20Sopenharmony_ci if (!ch->ioctx_ring) { 22498c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n"); 22508c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 22518c2ecf20Sopenharmony_ci goto free_rsp_cache; 22528c2ecf20Sopenharmony_ci } 22538c2ecf20Sopenharmony_ci 22548c2ecf20Sopenharmony_ci for (i = 0; i < ch->rq_size; i++) 22558c2ecf20Sopenharmony_ci ch->ioctx_ring[i]->ch = ch; 22568c2ecf20Sopenharmony_ci if (!sdev->use_srq) { 22578c2ecf20Sopenharmony_ci u16 imm_data_offset = req->req_flags & SRP_IMMED_REQUESTED ? 22588c2ecf20Sopenharmony_ci be16_to_cpu(req->imm_data_offset) : 0; 22598c2ecf20Sopenharmony_ci u16 alignment_offset; 22608c2ecf20Sopenharmony_ci u32 req_sz; 22618c2ecf20Sopenharmony_ci 22628c2ecf20Sopenharmony_ci if (req->req_flags & SRP_IMMED_REQUESTED) 22638c2ecf20Sopenharmony_ci pr_debug("imm_data_offset = %d\n", 22648c2ecf20Sopenharmony_ci be16_to_cpu(req->imm_data_offset)); 22658c2ecf20Sopenharmony_ci if (imm_data_offset >= sizeof(struct srp_cmd)) { 22668c2ecf20Sopenharmony_ci ch->imm_data_offset = imm_data_offset; 22678c2ecf20Sopenharmony_ci rsp->rsp_flags |= SRP_LOGIN_RSP_IMMED_SUPP; 22688c2ecf20Sopenharmony_ci } else { 22698c2ecf20Sopenharmony_ci ch->imm_data_offset = 0; 22708c2ecf20Sopenharmony_ci } 22718c2ecf20Sopenharmony_ci alignment_offset = round_up(imm_data_offset, 512) - 22728c2ecf20Sopenharmony_ci imm_data_offset; 22738c2ecf20Sopenharmony_ci req_sz = alignment_offset + imm_data_offset + srp_max_req_size; 22748c2ecf20Sopenharmony_ci ch->req_buf_cache = kmem_cache_create("srpt-req-buf", req_sz, 22758c2ecf20Sopenharmony_ci 512, 0, NULL); 22768c2ecf20Sopenharmony_ci if (!ch->req_buf_cache) 22778c2ecf20Sopenharmony_ci goto free_rsp_ring; 22788c2ecf20Sopenharmony_ci 22798c2ecf20Sopenharmony_ci ch->ioctx_recv_ring = (struct srpt_recv_ioctx **) 22808c2ecf20Sopenharmony_ci srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size, 22818c2ecf20Sopenharmony_ci sizeof(*ch->ioctx_recv_ring[0]), 22828c2ecf20Sopenharmony_ci ch->req_buf_cache, 22838c2ecf20Sopenharmony_ci alignment_offset, 22848c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 22858c2ecf20Sopenharmony_ci if (!ch->ioctx_recv_ring) { 22868c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n"); 22878c2ecf20Sopenharmony_ci rej->reason = 22888c2ecf20Sopenharmony_ci cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 22898c2ecf20Sopenharmony_ci goto free_recv_cache; 22908c2ecf20Sopenharmony_ci } 22918c2ecf20Sopenharmony_ci for (i = 0; i < ch->rq_size; i++) 22928c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list); 22938c2ecf20Sopenharmony_ci } 22948c2ecf20Sopenharmony_ci 22958c2ecf20Sopenharmony_ci ret = srpt_create_ch_ib(ch); 22968c2ecf20Sopenharmony_ci if (ret) { 22978c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 22988c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n"); 22998c2ecf20Sopenharmony_ci goto free_recv_ring; 23008c2ecf20Sopenharmony_ci } 23018c2ecf20Sopenharmony_ci 23028c2ecf20Sopenharmony_ci strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name)); 23038c2ecf20Sopenharmony_ci snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx", 23048c2ecf20Sopenharmony_ci be64_to_cpu(*(__be64 *)nexus->i_port_id), 23058c2ecf20Sopenharmony_ci be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8))); 23068c2ecf20Sopenharmony_ci 23078c2ecf20Sopenharmony_ci pr_debug("registering src addr %s or i_port_id %s\n", ch->sess_name, 23088c2ecf20Sopenharmony_ci i_port_id); 23098c2ecf20Sopenharmony_ci 23108c2ecf20Sopenharmony_ci tag_num = ch->rq_size; 23118c2ecf20Sopenharmony_ci tag_size = 1; /* ib_srpt does not use se_sess->sess_cmd_map */ 23128c2ecf20Sopenharmony_ci 23138c2ecf20Sopenharmony_ci if (sport->guid_id) { 23148c2ecf20Sopenharmony_ci mutex_lock(&sport->guid_id->mutex); 23158c2ecf20Sopenharmony_ci list_for_each_entry(stpg, &sport->guid_id->tpg_list, entry) { 23168c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(ch->sess)) 23178c2ecf20Sopenharmony_ci break; 23188c2ecf20Sopenharmony_ci ch->sess = target_setup_session(&stpg->tpg, tag_num, 23198c2ecf20Sopenharmony_ci tag_size, TARGET_PROT_NORMAL, 23208c2ecf20Sopenharmony_ci ch->sess_name, ch, NULL); 23218c2ecf20Sopenharmony_ci } 23228c2ecf20Sopenharmony_ci mutex_unlock(&sport->guid_id->mutex); 23238c2ecf20Sopenharmony_ci } 23248c2ecf20Sopenharmony_ci 23258c2ecf20Sopenharmony_ci if (sport->gid_id) { 23268c2ecf20Sopenharmony_ci mutex_lock(&sport->gid_id->mutex); 23278c2ecf20Sopenharmony_ci list_for_each_entry(stpg, &sport->gid_id->tpg_list, entry) { 23288c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(ch->sess)) 23298c2ecf20Sopenharmony_ci break; 23308c2ecf20Sopenharmony_ci ch->sess = target_setup_session(&stpg->tpg, tag_num, 23318c2ecf20Sopenharmony_ci tag_size, TARGET_PROT_NORMAL, i_port_id, 23328c2ecf20Sopenharmony_ci ch, NULL); 23338c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(ch->sess)) 23348c2ecf20Sopenharmony_ci break; 23358c2ecf20Sopenharmony_ci /* Retry without leading "0x" */ 23368c2ecf20Sopenharmony_ci ch->sess = target_setup_session(&stpg->tpg, tag_num, 23378c2ecf20Sopenharmony_ci tag_size, TARGET_PROT_NORMAL, 23388c2ecf20Sopenharmony_ci i_port_id + 2, ch, NULL); 23398c2ecf20Sopenharmony_ci } 23408c2ecf20Sopenharmony_ci mutex_unlock(&sport->gid_id->mutex); 23418c2ecf20Sopenharmony_ci } 23428c2ecf20Sopenharmony_ci 23438c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(ch->sess)) { 23448c2ecf20Sopenharmony_ci WARN_ON_ONCE(ch->sess == NULL); 23458c2ecf20Sopenharmony_ci ret = PTR_ERR(ch->sess); 23468c2ecf20Sopenharmony_ci ch->sess = NULL; 23478c2ecf20Sopenharmony_ci pr_info("Rejected login for initiator %s: ret = %d.\n", 23488c2ecf20Sopenharmony_ci ch->sess_name, ret); 23498c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(ret == -ENOMEM ? 23508c2ecf20Sopenharmony_ci SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES : 23518c2ecf20Sopenharmony_ci SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED); 23528c2ecf20Sopenharmony_ci goto destroy_ib; 23538c2ecf20Sopenharmony_ci } 23548c2ecf20Sopenharmony_ci 23558c2ecf20Sopenharmony_ci /* 23568c2ecf20Sopenharmony_ci * Once a session has been created destruction of srpt_rdma_ch objects 23578c2ecf20Sopenharmony_ci * will decrement sport->refcount. Hence increment sport->refcount now. 23588c2ecf20Sopenharmony_ci */ 23598c2ecf20Sopenharmony_ci atomic_inc(&sport->refcount); 23608c2ecf20Sopenharmony_ci 23618c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 23628c2ecf20Sopenharmony_ci 23638c2ecf20Sopenharmony_ci if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) { 23648c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch2; 23658c2ecf20Sopenharmony_ci 23668c2ecf20Sopenharmony_ci list_for_each_entry(ch2, &nexus->ch_list, list) { 23678c2ecf20Sopenharmony_ci if (srpt_disconnect_ch(ch2) < 0) 23688c2ecf20Sopenharmony_ci continue; 23698c2ecf20Sopenharmony_ci pr_info("Relogin - closed existing channel %s\n", 23708c2ecf20Sopenharmony_ci ch2->sess_name); 23718c2ecf20Sopenharmony_ci rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_TERMINATED; 23728c2ecf20Sopenharmony_ci } 23738c2ecf20Sopenharmony_ci } else { 23748c2ecf20Sopenharmony_ci rsp->rsp_flags |= SRP_LOGIN_RSP_MULTICHAN_MAINTAINED; 23758c2ecf20Sopenharmony_ci } 23768c2ecf20Sopenharmony_ci 23778c2ecf20Sopenharmony_ci list_add_tail_rcu(&ch->list, &nexus->ch_list); 23788c2ecf20Sopenharmony_ci 23798c2ecf20Sopenharmony_ci if (!sport->enabled) { 23808c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32( 23818c2ecf20Sopenharmony_ci SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 23828c2ecf20Sopenharmony_ci pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n", 23838c2ecf20Sopenharmony_ci dev_name(&sdev->device->dev), port_num); 23848c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 23858c2ecf20Sopenharmony_ci ret = -EINVAL; 23868c2ecf20Sopenharmony_ci goto reject; 23878c2ecf20Sopenharmony_ci } 23888c2ecf20Sopenharmony_ci 23898c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 23908c2ecf20Sopenharmony_ci 23918c2ecf20Sopenharmony_ci ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp); 23928c2ecf20Sopenharmony_ci if (ret) { 23938c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 23948c2ecf20Sopenharmony_ci pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n", 23958c2ecf20Sopenharmony_ci ret); 23968c2ecf20Sopenharmony_ci goto reject; 23978c2ecf20Sopenharmony_ci } 23988c2ecf20Sopenharmony_ci 23998c2ecf20Sopenharmony_ci pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess, 24008c2ecf20Sopenharmony_ci ch->sess_name, ch); 24018c2ecf20Sopenharmony_ci 24028c2ecf20Sopenharmony_ci /* create srp_login_response */ 24038c2ecf20Sopenharmony_ci rsp->opcode = SRP_LOGIN_RSP; 24048c2ecf20Sopenharmony_ci rsp->tag = req->tag; 24058c2ecf20Sopenharmony_ci rsp->max_it_iu_len = cpu_to_be32(srp_max_req_size); 24068c2ecf20Sopenharmony_ci rsp->max_ti_iu_len = req->req_it_iu_len; 24078c2ecf20Sopenharmony_ci ch->max_ti_iu_len = it_iu_len; 24088c2ecf20Sopenharmony_ci rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 24098c2ecf20Sopenharmony_ci SRP_BUF_FORMAT_INDIRECT); 24108c2ecf20Sopenharmony_ci rsp->req_lim_delta = cpu_to_be32(ch->rq_size); 24118c2ecf20Sopenharmony_ci atomic_set(&ch->req_lim, ch->rq_size); 24128c2ecf20Sopenharmony_ci atomic_set(&ch->req_lim_delta, 0); 24138c2ecf20Sopenharmony_ci 24148c2ecf20Sopenharmony_ci /* create cm reply */ 24158c2ecf20Sopenharmony_ci if (ch->using_rdma_cm) { 24168c2ecf20Sopenharmony_ci rep_param->rdma_cm.private_data = (void *)rsp; 24178c2ecf20Sopenharmony_ci rep_param->rdma_cm.private_data_len = sizeof(*rsp); 24188c2ecf20Sopenharmony_ci rep_param->rdma_cm.rnr_retry_count = 7; 24198c2ecf20Sopenharmony_ci rep_param->rdma_cm.flow_control = 1; 24208c2ecf20Sopenharmony_ci rep_param->rdma_cm.responder_resources = 4; 24218c2ecf20Sopenharmony_ci rep_param->rdma_cm.initiator_depth = 4; 24228c2ecf20Sopenharmony_ci } else { 24238c2ecf20Sopenharmony_ci rep_param->ib_cm.qp_num = ch->qp->qp_num; 24248c2ecf20Sopenharmony_ci rep_param->ib_cm.private_data = (void *)rsp; 24258c2ecf20Sopenharmony_ci rep_param->ib_cm.private_data_len = sizeof(*rsp); 24268c2ecf20Sopenharmony_ci rep_param->ib_cm.rnr_retry_count = 7; 24278c2ecf20Sopenharmony_ci rep_param->ib_cm.flow_control = 1; 24288c2ecf20Sopenharmony_ci rep_param->ib_cm.failover_accepted = 0; 24298c2ecf20Sopenharmony_ci rep_param->ib_cm.srq = 1; 24308c2ecf20Sopenharmony_ci rep_param->ib_cm.responder_resources = 4; 24318c2ecf20Sopenharmony_ci rep_param->ib_cm.initiator_depth = 4; 24328c2ecf20Sopenharmony_ci } 24338c2ecf20Sopenharmony_ci 24348c2ecf20Sopenharmony_ci /* 24358c2ecf20Sopenharmony_ci * Hold the sport mutex while accepting a connection to avoid that 24368c2ecf20Sopenharmony_ci * srpt_disconnect_ch() is invoked concurrently with this code. 24378c2ecf20Sopenharmony_ci */ 24388c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 24398c2ecf20Sopenharmony_ci if (sport->enabled && ch->state == CH_CONNECTING) { 24408c2ecf20Sopenharmony_ci if (ch->using_rdma_cm) 24418c2ecf20Sopenharmony_ci ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm); 24428c2ecf20Sopenharmony_ci else 24438c2ecf20Sopenharmony_ci ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm); 24448c2ecf20Sopenharmony_ci } else { 24458c2ecf20Sopenharmony_ci ret = -EINVAL; 24468c2ecf20Sopenharmony_ci } 24478c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 24488c2ecf20Sopenharmony_ci 24498c2ecf20Sopenharmony_ci switch (ret) { 24508c2ecf20Sopenharmony_ci case 0: 24518c2ecf20Sopenharmony_ci break; 24528c2ecf20Sopenharmony_ci case -EINVAL: 24538c2ecf20Sopenharmony_ci goto reject; 24548c2ecf20Sopenharmony_ci default: 24558c2ecf20Sopenharmony_ci rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES); 24568c2ecf20Sopenharmony_ci pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n", 24578c2ecf20Sopenharmony_ci ret); 24588c2ecf20Sopenharmony_ci goto reject; 24598c2ecf20Sopenharmony_ci } 24608c2ecf20Sopenharmony_ci 24618c2ecf20Sopenharmony_ci goto out; 24628c2ecf20Sopenharmony_ci 24638c2ecf20Sopenharmony_cidestroy_ib: 24648c2ecf20Sopenharmony_ci srpt_destroy_ch_ib(ch); 24658c2ecf20Sopenharmony_ci 24668c2ecf20Sopenharmony_cifree_recv_ring: 24678c2ecf20Sopenharmony_ci srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring, 24688c2ecf20Sopenharmony_ci ch->sport->sdev, ch->rq_size, 24698c2ecf20Sopenharmony_ci ch->req_buf_cache, DMA_FROM_DEVICE); 24708c2ecf20Sopenharmony_ci 24718c2ecf20Sopenharmony_cifree_recv_cache: 24728c2ecf20Sopenharmony_ci kmem_cache_destroy(ch->req_buf_cache); 24738c2ecf20Sopenharmony_ci 24748c2ecf20Sopenharmony_cifree_rsp_ring: 24758c2ecf20Sopenharmony_ci srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring, 24768c2ecf20Sopenharmony_ci ch->sport->sdev, ch->rq_size, 24778c2ecf20Sopenharmony_ci ch->rsp_buf_cache, DMA_TO_DEVICE); 24788c2ecf20Sopenharmony_ci 24798c2ecf20Sopenharmony_cifree_rsp_cache: 24808c2ecf20Sopenharmony_ci kmem_cache_destroy(ch->rsp_buf_cache); 24818c2ecf20Sopenharmony_ci 24828c2ecf20Sopenharmony_cifree_ch: 24838c2ecf20Sopenharmony_ci if (rdma_cm_id) 24848c2ecf20Sopenharmony_ci rdma_cm_id->context = NULL; 24858c2ecf20Sopenharmony_ci else 24868c2ecf20Sopenharmony_ci ib_cm_id->context = NULL; 24878c2ecf20Sopenharmony_ci kfree(ch); 24888c2ecf20Sopenharmony_ci ch = NULL; 24898c2ecf20Sopenharmony_ci 24908c2ecf20Sopenharmony_ci WARN_ON_ONCE(ret == 0); 24918c2ecf20Sopenharmony_ci 24928c2ecf20Sopenharmony_cireject: 24938c2ecf20Sopenharmony_ci pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason)); 24948c2ecf20Sopenharmony_ci rej->opcode = SRP_LOGIN_REJ; 24958c2ecf20Sopenharmony_ci rej->tag = req->tag; 24968c2ecf20Sopenharmony_ci rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | 24978c2ecf20Sopenharmony_ci SRP_BUF_FORMAT_INDIRECT); 24988c2ecf20Sopenharmony_ci 24998c2ecf20Sopenharmony_ci if (rdma_cm_id) 25008c2ecf20Sopenharmony_ci rdma_reject(rdma_cm_id, rej, sizeof(*rej), 25018c2ecf20Sopenharmony_ci IB_CM_REJ_CONSUMER_DEFINED); 25028c2ecf20Sopenharmony_ci else 25038c2ecf20Sopenharmony_ci ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, 25048c2ecf20Sopenharmony_ci rej, sizeof(*rej)); 25058c2ecf20Sopenharmony_ci 25068c2ecf20Sopenharmony_ci if (ch && ch->sess) { 25078c2ecf20Sopenharmony_ci srpt_close_ch(ch); 25088c2ecf20Sopenharmony_ci /* 25098c2ecf20Sopenharmony_ci * Tell the caller not to free cm_id since 25108c2ecf20Sopenharmony_ci * srpt_release_channel_work() will do that. 25118c2ecf20Sopenharmony_ci */ 25128c2ecf20Sopenharmony_ci ret = 0; 25138c2ecf20Sopenharmony_ci } 25148c2ecf20Sopenharmony_ci 25158c2ecf20Sopenharmony_ciout: 25168c2ecf20Sopenharmony_ci kfree(rep_param); 25178c2ecf20Sopenharmony_ci kfree(rsp); 25188c2ecf20Sopenharmony_ci kfree(rej); 25198c2ecf20Sopenharmony_ci 25208c2ecf20Sopenharmony_ci return ret; 25218c2ecf20Sopenharmony_ci} 25228c2ecf20Sopenharmony_ci 25238c2ecf20Sopenharmony_cistatic int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id, 25248c2ecf20Sopenharmony_ci const struct ib_cm_req_event_param *param, 25258c2ecf20Sopenharmony_ci void *private_data) 25268c2ecf20Sopenharmony_ci{ 25278c2ecf20Sopenharmony_ci char sguid[40]; 25288c2ecf20Sopenharmony_ci 25298c2ecf20Sopenharmony_ci srpt_format_guid(sguid, sizeof(sguid), 25308c2ecf20Sopenharmony_ci ¶m->primary_path->dgid.global.interface_id); 25318c2ecf20Sopenharmony_ci 25328c2ecf20Sopenharmony_ci return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port, 25338c2ecf20Sopenharmony_ci param->primary_path->pkey, 25348c2ecf20Sopenharmony_ci private_data, sguid); 25358c2ecf20Sopenharmony_ci} 25368c2ecf20Sopenharmony_ci 25378c2ecf20Sopenharmony_cistatic int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id, 25388c2ecf20Sopenharmony_ci struct rdma_cm_event *event) 25398c2ecf20Sopenharmony_ci{ 25408c2ecf20Sopenharmony_ci struct srpt_device *sdev; 25418c2ecf20Sopenharmony_ci struct srp_login_req req; 25428c2ecf20Sopenharmony_ci const struct srp_login_req_rdma *req_rdma; 25438c2ecf20Sopenharmony_ci struct sa_path_rec *path_rec = cm_id->route.path_rec; 25448c2ecf20Sopenharmony_ci char src_addr[40]; 25458c2ecf20Sopenharmony_ci 25468c2ecf20Sopenharmony_ci sdev = ib_get_client_data(cm_id->device, &srpt_client); 25478c2ecf20Sopenharmony_ci if (!sdev) 25488c2ecf20Sopenharmony_ci return -ECONNREFUSED; 25498c2ecf20Sopenharmony_ci 25508c2ecf20Sopenharmony_ci if (event->param.conn.private_data_len < sizeof(*req_rdma)) 25518c2ecf20Sopenharmony_ci return -EINVAL; 25528c2ecf20Sopenharmony_ci 25538c2ecf20Sopenharmony_ci /* Transform srp_login_req_rdma into srp_login_req. */ 25548c2ecf20Sopenharmony_ci req_rdma = event->param.conn.private_data; 25558c2ecf20Sopenharmony_ci memset(&req, 0, sizeof(req)); 25568c2ecf20Sopenharmony_ci req.opcode = req_rdma->opcode; 25578c2ecf20Sopenharmony_ci req.tag = req_rdma->tag; 25588c2ecf20Sopenharmony_ci req.req_it_iu_len = req_rdma->req_it_iu_len; 25598c2ecf20Sopenharmony_ci req.req_buf_fmt = req_rdma->req_buf_fmt; 25608c2ecf20Sopenharmony_ci req.req_flags = req_rdma->req_flags; 25618c2ecf20Sopenharmony_ci memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16); 25628c2ecf20Sopenharmony_ci memcpy(req.target_port_id, req_rdma->target_port_id, 16); 25638c2ecf20Sopenharmony_ci req.imm_data_offset = req_rdma->imm_data_offset; 25648c2ecf20Sopenharmony_ci 25658c2ecf20Sopenharmony_ci snprintf(src_addr, sizeof(src_addr), "%pIS", 25668c2ecf20Sopenharmony_ci &cm_id->route.addr.src_addr); 25678c2ecf20Sopenharmony_ci 25688c2ecf20Sopenharmony_ci return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num, 25698c2ecf20Sopenharmony_ci path_rec ? path_rec->pkey : 0, &req, src_addr); 25708c2ecf20Sopenharmony_ci} 25718c2ecf20Sopenharmony_ci 25728c2ecf20Sopenharmony_cistatic void srpt_cm_rej_recv(struct srpt_rdma_ch *ch, 25738c2ecf20Sopenharmony_ci enum ib_cm_rej_reason reason, 25748c2ecf20Sopenharmony_ci const u8 *private_data, 25758c2ecf20Sopenharmony_ci u8 private_data_len) 25768c2ecf20Sopenharmony_ci{ 25778c2ecf20Sopenharmony_ci char *priv = NULL; 25788c2ecf20Sopenharmony_ci int i; 25798c2ecf20Sopenharmony_ci 25808c2ecf20Sopenharmony_ci if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1, 25818c2ecf20Sopenharmony_ci GFP_KERNEL))) { 25828c2ecf20Sopenharmony_ci for (i = 0; i < private_data_len; i++) 25838c2ecf20Sopenharmony_ci sprintf(priv + 3 * i, " %02x", private_data[i]); 25848c2ecf20Sopenharmony_ci } 25858c2ecf20Sopenharmony_ci pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n", 25868c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, reason, private_data_len ? 25878c2ecf20Sopenharmony_ci "; private data" : "", priv ? priv : " (?)"); 25888c2ecf20Sopenharmony_ci kfree(priv); 25898c2ecf20Sopenharmony_ci} 25908c2ecf20Sopenharmony_ci 25918c2ecf20Sopenharmony_ci/** 25928c2ecf20Sopenharmony_ci * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event 25938c2ecf20Sopenharmony_ci * @ch: SRPT RDMA channel. 25948c2ecf20Sopenharmony_ci * 25958c2ecf20Sopenharmony_ci * An RTU (ready to use) message indicates that the connection has been 25968c2ecf20Sopenharmony_ci * established and that the recipient may begin transmitting. 25978c2ecf20Sopenharmony_ci */ 25988c2ecf20Sopenharmony_cistatic void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch) 25998c2ecf20Sopenharmony_ci{ 26008c2ecf20Sopenharmony_ci int ret; 26018c2ecf20Sopenharmony_ci 26028c2ecf20Sopenharmony_ci ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp); 26038c2ecf20Sopenharmony_ci if (ret < 0) { 26048c2ecf20Sopenharmony_ci pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name, 26058c2ecf20Sopenharmony_ci ch->qp->qp_num); 26068c2ecf20Sopenharmony_ci srpt_close_ch(ch); 26078c2ecf20Sopenharmony_ci return; 26088c2ecf20Sopenharmony_ci } 26098c2ecf20Sopenharmony_ci 26108c2ecf20Sopenharmony_ci /* 26118c2ecf20Sopenharmony_ci * Note: calling srpt_close_ch() if the transition to the LIVE state 26128c2ecf20Sopenharmony_ci * fails is not necessary since that means that that function has 26138c2ecf20Sopenharmony_ci * already been invoked from another thread. 26148c2ecf20Sopenharmony_ci */ 26158c2ecf20Sopenharmony_ci if (!srpt_set_ch_state(ch, CH_LIVE)) { 26168c2ecf20Sopenharmony_ci pr_err("%s-%d: channel transition to LIVE state failed\n", 26178c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num); 26188c2ecf20Sopenharmony_ci return; 26198c2ecf20Sopenharmony_ci } 26208c2ecf20Sopenharmony_ci 26218c2ecf20Sopenharmony_ci /* Trigger wait list processing. */ 26228c2ecf20Sopenharmony_ci ret = srpt_zerolength_write(ch); 26238c2ecf20Sopenharmony_ci WARN_ONCE(ret < 0, "%d\n", ret); 26248c2ecf20Sopenharmony_ci} 26258c2ecf20Sopenharmony_ci 26268c2ecf20Sopenharmony_ci/** 26278c2ecf20Sopenharmony_ci * srpt_cm_handler - IB connection manager callback function 26288c2ecf20Sopenharmony_ci * @cm_id: IB/CM connection identifier. 26298c2ecf20Sopenharmony_ci * @event: IB/CM event. 26308c2ecf20Sopenharmony_ci * 26318c2ecf20Sopenharmony_ci * A non-zero return value will cause the caller destroy the CM ID. 26328c2ecf20Sopenharmony_ci * 26338c2ecf20Sopenharmony_ci * Note: srpt_cm_handler() must only return a non-zero value when transferring 26348c2ecf20Sopenharmony_ci * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning 26358c2ecf20Sopenharmony_ci * a non-zero value in any other case will trigger a race with the 26368c2ecf20Sopenharmony_ci * ib_destroy_cm_id() call in srpt_release_channel(). 26378c2ecf20Sopenharmony_ci */ 26388c2ecf20Sopenharmony_cistatic int srpt_cm_handler(struct ib_cm_id *cm_id, 26398c2ecf20Sopenharmony_ci const struct ib_cm_event *event) 26408c2ecf20Sopenharmony_ci{ 26418c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = cm_id->context; 26428c2ecf20Sopenharmony_ci int ret; 26438c2ecf20Sopenharmony_ci 26448c2ecf20Sopenharmony_ci ret = 0; 26458c2ecf20Sopenharmony_ci switch (event->event) { 26468c2ecf20Sopenharmony_ci case IB_CM_REQ_RECEIVED: 26478c2ecf20Sopenharmony_ci ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd, 26488c2ecf20Sopenharmony_ci event->private_data); 26498c2ecf20Sopenharmony_ci break; 26508c2ecf20Sopenharmony_ci case IB_CM_REJ_RECEIVED: 26518c2ecf20Sopenharmony_ci srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason, 26528c2ecf20Sopenharmony_ci event->private_data, 26538c2ecf20Sopenharmony_ci IB_CM_REJ_PRIVATE_DATA_SIZE); 26548c2ecf20Sopenharmony_ci break; 26558c2ecf20Sopenharmony_ci case IB_CM_RTU_RECEIVED: 26568c2ecf20Sopenharmony_ci case IB_CM_USER_ESTABLISHED: 26578c2ecf20Sopenharmony_ci srpt_cm_rtu_recv(ch); 26588c2ecf20Sopenharmony_ci break; 26598c2ecf20Sopenharmony_ci case IB_CM_DREQ_RECEIVED: 26608c2ecf20Sopenharmony_ci srpt_disconnect_ch(ch); 26618c2ecf20Sopenharmony_ci break; 26628c2ecf20Sopenharmony_ci case IB_CM_DREP_RECEIVED: 26638c2ecf20Sopenharmony_ci pr_info("Received CM DREP message for ch %s-%d.\n", 26648c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num); 26658c2ecf20Sopenharmony_ci srpt_close_ch(ch); 26668c2ecf20Sopenharmony_ci break; 26678c2ecf20Sopenharmony_ci case IB_CM_TIMEWAIT_EXIT: 26688c2ecf20Sopenharmony_ci pr_info("Received CM TimeWait exit for ch %s-%d.\n", 26698c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num); 26708c2ecf20Sopenharmony_ci srpt_close_ch(ch); 26718c2ecf20Sopenharmony_ci break; 26728c2ecf20Sopenharmony_ci case IB_CM_REP_ERROR: 26738c2ecf20Sopenharmony_ci pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, 26748c2ecf20Sopenharmony_ci ch->qp->qp_num); 26758c2ecf20Sopenharmony_ci break; 26768c2ecf20Sopenharmony_ci case IB_CM_DREQ_ERROR: 26778c2ecf20Sopenharmony_ci pr_info("Received CM DREQ ERROR event.\n"); 26788c2ecf20Sopenharmony_ci break; 26798c2ecf20Sopenharmony_ci case IB_CM_MRA_RECEIVED: 26808c2ecf20Sopenharmony_ci pr_info("Received CM MRA event\n"); 26818c2ecf20Sopenharmony_ci break; 26828c2ecf20Sopenharmony_ci default: 26838c2ecf20Sopenharmony_ci pr_err("received unrecognized CM event %d\n", event->event); 26848c2ecf20Sopenharmony_ci break; 26858c2ecf20Sopenharmony_ci } 26868c2ecf20Sopenharmony_ci 26878c2ecf20Sopenharmony_ci return ret; 26888c2ecf20Sopenharmony_ci} 26898c2ecf20Sopenharmony_ci 26908c2ecf20Sopenharmony_cistatic int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id, 26918c2ecf20Sopenharmony_ci struct rdma_cm_event *event) 26928c2ecf20Sopenharmony_ci{ 26938c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = cm_id->context; 26948c2ecf20Sopenharmony_ci int ret = 0; 26958c2ecf20Sopenharmony_ci 26968c2ecf20Sopenharmony_ci switch (event->event) { 26978c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_CONNECT_REQUEST: 26988c2ecf20Sopenharmony_ci ret = srpt_rdma_cm_req_recv(cm_id, event); 26998c2ecf20Sopenharmony_ci break; 27008c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_REJECTED: 27018c2ecf20Sopenharmony_ci srpt_cm_rej_recv(ch, event->status, 27028c2ecf20Sopenharmony_ci event->param.conn.private_data, 27038c2ecf20Sopenharmony_ci event->param.conn.private_data_len); 27048c2ecf20Sopenharmony_ci break; 27058c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_ESTABLISHED: 27068c2ecf20Sopenharmony_ci srpt_cm_rtu_recv(ch); 27078c2ecf20Sopenharmony_ci break; 27088c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_DISCONNECTED: 27098c2ecf20Sopenharmony_ci if (ch->state < CH_DISCONNECTING) 27108c2ecf20Sopenharmony_ci srpt_disconnect_ch(ch); 27118c2ecf20Sopenharmony_ci else 27128c2ecf20Sopenharmony_ci srpt_close_ch(ch); 27138c2ecf20Sopenharmony_ci break; 27148c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_TIMEWAIT_EXIT: 27158c2ecf20Sopenharmony_ci srpt_close_ch(ch); 27168c2ecf20Sopenharmony_ci break; 27178c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_UNREACHABLE: 27188c2ecf20Sopenharmony_ci pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name, 27198c2ecf20Sopenharmony_ci ch->qp->qp_num); 27208c2ecf20Sopenharmony_ci break; 27218c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_DEVICE_REMOVAL: 27228c2ecf20Sopenharmony_ci case RDMA_CM_EVENT_ADDR_CHANGE: 27238c2ecf20Sopenharmony_ci break; 27248c2ecf20Sopenharmony_ci default: 27258c2ecf20Sopenharmony_ci pr_err("received unrecognized RDMA CM event %d\n", 27268c2ecf20Sopenharmony_ci event->event); 27278c2ecf20Sopenharmony_ci break; 27288c2ecf20Sopenharmony_ci } 27298c2ecf20Sopenharmony_ci 27308c2ecf20Sopenharmony_ci return ret; 27318c2ecf20Sopenharmony_ci} 27328c2ecf20Sopenharmony_ci 27338c2ecf20Sopenharmony_ci/* 27348c2ecf20Sopenharmony_ci * srpt_write_pending - Start data transfer from initiator to target (write). 27358c2ecf20Sopenharmony_ci */ 27368c2ecf20Sopenharmony_cistatic int srpt_write_pending(struct se_cmd *se_cmd) 27378c2ecf20Sopenharmony_ci{ 27388c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = 27398c2ecf20Sopenharmony_ci container_of(se_cmd, struct srpt_send_ioctx, cmd); 27408c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = ioctx->ch; 27418c2ecf20Sopenharmony_ci struct ib_send_wr *first_wr = NULL; 27428c2ecf20Sopenharmony_ci struct ib_cqe *cqe = &ioctx->rdma_cqe; 27438c2ecf20Sopenharmony_ci enum srpt_command_state new_state; 27448c2ecf20Sopenharmony_ci int ret, i; 27458c2ecf20Sopenharmony_ci 27468c2ecf20Sopenharmony_ci if (ioctx->recv_ioctx) { 27478c2ecf20Sopenharmony_ci srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN); 27488c2ecf20Sopenharmony_ci target_execute_cmd(&ioctx->cmd); 27498c2ecf20Sopenharmony_ci return 0; 27508c2ecf20Sopenharmony_ci } 27518c2ecf20Sopenharmony_ci 27528c2ecf20Sopenharmony_ci new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA); 27538c2ecf20Sopenharmony_ci WARN_ON(new_state == SRPT_STATE_DONE); 27548c2ecf20Sopenharmony_ci 27558c2ecf20Sopenharmony_ci if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) { 27568c2ecf20Sopenharmony_ci pr_warn("%s: IB send queue full (needed %d)\n", 27578c2ecf20Sopenharmony_ci __func__, ioctx->n_rdma); 27588c2ecf20Sopenharmony_ci ret = -ENOMEM; 27598c2ecf20Sopenharmony_ci goto out_undo; 27608c2ecf20Sopenharmony_ci } 27618c2ecf20Sopenharmony_ci 27628c2ecf20Sopenharmony_ci cqe->done = srpt_rdma_read_done; 27638c2ecf20Sopenharmony_ci for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) { 27648c2ecf20Sopenharmony_ci struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 27658c2ecf20Sopenharmony_ci 27668c2ecf20Sopenharmony_ci first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port, 27678c2ecf20Sopenharmony_ci cqe, first_wr); 27688c2ecf20Sopenharmony_ci cqe = NULL; 27698c2ecf20Sopenharmony_ci } 27708c2ecf20Sopenharmony_ci 27718c2ecf20Sopenharmony_ci ret = ib_post_send(ch->qp, first_wr, NULL); 27728c2ecf20Sopenharmony_ci if (ret) { 27738c2ecf20Sopenharmony_ci pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n", 27748c2ecf20Sopenharmony_ci __func__, ret, ioctx->n_rdma, 27758c2ecf20Sopenharmony_ci atomic_read(&ch->sq_wr_avail)); 27768c2ecf20Sopenharmony_ci goto out_undo; 27778c2ecf20Sopenharmony_ci } 27788c2ecf20Sopenharmony_ci 27798c2ecf20Sopenharmony_ci return 0; 27808c2ecf20Sopenharmony_ciout_undo: 27818c2ecf20Sopenharmony_ci atomic_add(ioctx->n_rdma, &ch->sq_wr_avail); 27828c2ecf20Sopenharmony_ci return ret; 27838c2ecf20Sopenharmony_ci} 27848c2ecf20Sopenharmony_ci 27858c2ecf20Sopenharmony_cistatic u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status) 27868c2ecf20Sopenharmony_ci{ 27878c2ecf20Sopenharmony_ci switch (tcm_mgmt_status) { 27888c2ecf20Sopenharmony_ci case TMR_FUNCTION_COMPLETE: 27898c2ecf20Sopenharmony_ci return SRP_TSK_MGMT_SUCCESS; 27908c2ecf20Sopenharmony_ci case TMR_FUNCTION_REJECTED: 27918c2ecf20Sopenharmony_ci return SRP_TSK_MGMT_FUNC_NOT_SUPP; 27928c2ecf20Sopenharmony_ci } 27938c2ecf20Sopenharmony_ci return SRP_TSK_MGMT_FAILED; 27948c2ecf20Sopenharmony_ci} 27958c2ecf20Sopenharmony_ci 27968c2ecf20Sopenharmony_ci/** 27978c2ecf20Sopenharmony_ci * srpt_queue_response - transmit the response to a SCSI command 27988c2ecf20Sopenharmony_ci * @cmd: SCSI target command. 27998c2ecf20Sopenharmony_ci * 28008c2ecf20Sopenharmony_ci * Callback function called by the TCM core. Must not block since it can be 28018c2ecf20Sopenharmony_ci * invoked on the context of the IB completion handler. 28028c2ecf20Sopenharmony_ci */ 28038c2ecf20Sopenharmony_cistatic void srpt_queue_response(struct se_cmd *cmd) 28048c2ecf20Sopenharmony_ci{ 28058c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = 28068c2ecf20Sopenharmony_ci container_of(cmd, struct srpt_send_ioctx, cmd); 28078c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = ioctx->ch; 28088c2ecf20Sopenharmony_ci struct srpt_device *sdev = ch->sport->sdev; 28098c2ecf20Sopenharmony_ci struct ib_send_wr send_wr, *first_wr = &send_wr; 28108c2ecf20Sopenharmony_ci struct ib_sge sge; 28118c2ecf20Sopenharmony_ci enum srpt_command_state state; 28128c2ecf20Sopenharmony_ci int resp_len, ret, i; 28138c2ecf20Sopenharmony_ci u8 srp_tm_status; 28148c2ecf20Sopenharmony_ci 28158c2ecf20Sopenharmony_ci state = ioctx->state; 28168c2ecf20Sopenharmony_ci switch (state) { 28178c2ecf20Sopenharmony_ci case SRPT_STATE_NEW: 28188c2ecf20Sopenharmony_ci case SRPT_STATE_DATA_IN: 28198c2ecf20Sopenharmony_ci ioctx->state = SRPT_STATE_CMD_RSP_SENT; 28208c2ecf20Sopenharmony_ci break; 28218c2ecf20Sopenharmony_ci case SRPT_STATE_MGMT: 28228c2ecf20Sopenharmony_ci ioctx->state = SRPT_STATE_MGMT_RSP_SENT; 28238c2ecf20Sopenharmony_ci break; 28248c2ecf20Sopenharmony_ci default: 28258c2ecf20Sopenharmony_ci WARN(true, "ch %p; cmd %d: unexpected command state %d\n", 28268c2ecf20Sopenharmony_ci ch, ioctx->ioctx.index, ioctx->state); 28278c2ecf20Sopenharmony_ci break; 28288c2ecf20Sopenharmony_ci } 28298c2ecf20Sopenharmony_ci 28308c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)) 28318c2ecf20Sopenharmony_ci return; 28328c2ecf20Sopenharmony_ci 28338c2ecf20Sopenharmony_ci /* For read commands, transfer the data to the initiator. */ 28348c2ecf20Sopenharmony_ci if (ioctx->cmd.data_direction == DMA_FROM_DEVICE && 28358c2ecf20Sopenharmony_ci ioctx->cmd.data_length && 28368c2ecf20Sopenharmony_ci !ioctx->queue_status_only) { 28378c2ecf20Sopenharmony_ci for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) { 28388c2ecf20Sopenharmony_ci struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i]; 28398c2ecf20Sopenharmony_ci 28408c2ecf20Sopenharmony_ci first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, 28418c2ecf20Sopenharmony_ci ch->sport->port, NULL, first_wr); 28428c2ecf20Sopenharmony_ci } 28438c2ecf20Sopenharmony_ci } 28448c2ecf20Sopenharmony_ci 28458c2ecf20Sopenharmony_ci if (state != SRPT_STATE_MGMT) 28468c2ecf20Sopenharmony_ci resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag, 28478c2ecf20Sopenharmony_ci cmd->scsi_status); 28488c2ecf20Sopenharmony_ci else { 28498c2ecf20Sopenharmony_ci srp_tm_status 28508c2ecf20Sopenharmony_ci = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response); 28518c2ecf20Sopenharmony_ci resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status, 28528c2ecf20Sopenharmony_ci ioctx->cmd.tag); 28538c2ecf20Sopenharmony_ci } 28548c2ecf20Sopenharmony_ci 28558c2ecf20Sopenharmony_ci atomic_inc(&ch->req_lim); 28568c2ecf20Sopenharmony_ci 28578c2ecf20Sopenharmony_ci if (unlikely(atomic_sub_return(1 + ioctx->n_rdma, 28588c2ecf20Sopenharmony_ci &ch->sq_wr_avail) < 0)) { 28598c2ecf20Sopenharmony_ci pr_warn("%s: IB send queue full (needed %d)\n", 28608c2ecf20Sopenharmony_ci __func__, ioctx->n_rdma); 28618c2ecf20Sopenharmony_ci ret = -ENOMEM; 28628c2ecf20Sopenharmony_ci goto out; 28638c2ecf20Sopenharmony_ci } 28648c2ecf20Sopenharmony_ci 28658c2ecf20Sopenharmony_ci ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len, 28668c2ecf20Sopenharmony_ci DMA_TO_DEVICE); 28678c2ecf20Sopenharmony_ci 28688c2ecf20Sopenharmony_ci sge.addr = ioctx->ioctx.dma; 28698c2ecf20Sopenharmony_ci sge.length = resp_len; 28708c2ecf20Sopenharmony_ci sge.lkey = sdev->lkey; 28718c2ecf20Sopenharmony_ci 28728c2ecf20Sopenharmony_ci ioctx->ioctx.cqe.done = srpt_send_done; 28738c2ecf20Sopenharmony_ci send_wr.next = NULL; 28748c2ecf20Sopenharmony_ci send_wr.wr_cqe = &ioctx->ioctx.cqe; 28758c2ecf20Sopenharmony_ci send_wr.sg_list = &sge; 28768c2ecf20Sopenharmony_ci send_wr.num_sge = 1; 28778c2ecf20Sopenharmony_ci send_wr.opcode = IB_WR_SEND; 28788c2ecf20Sopenharmony_ci send_wr.send_flags = IB_SEND_SIGNALED; 28798c2ecf20Sopenharmony_ci 28808c2ecf20Sopenharmony_ci ret = ib_post_send(ch->qp, first_wr, NULL); 28818c2ecf20Sopenharmony_ci if (ret < 0) { 28828c2ecf20Sopenharmony_ci pr_err("%s: sending cmd response failed for tag %llu (%d)\n", 28838c2ecf20Sopenharmony_ci __func__, ioctx->cmd.tag, ret); 28848c2ecf20Sopenharmony_ci goto out; 28858c2ecf20Sopenharmony_ci } 28868c2ecf20Sopenharmony_ci 28878c2ecf20Sopenharmony_ci return; 28888c2ecf20Sopenharmony_ci 28898c2ecf20Sopenharmony_ciout: 28908c2ecf20Sopenharmony_ci atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail); 28918c2ecf20Sopenharmony_ci atomic_dec(&ch->req_lim); 28928c2ecf20Sopenharmony_ci srpt_set_cmd_state(ioctx, SRPT_STATE_DONE); 28938c2ecf20Sopenharmony_ci target_put_sess_cmd(&ioctx->cmd); 28948c2ecf20Sopenharmony_ci} 28958c2ecf20Sopenharmony_ci 28968c2ecf20Sopenharmony_cistatic int srpt_queue_data_in(struct se_cmd *cmd) 28978c2ecf20Sopenharmony_ci{ 28988c2ecf20Sopenharmony_ci srpt_queue_response(cmd); 28998c2ecf20Sopenharmony_ci return 0; 29008c2ecf20Sopenharmony_ci} 29018c2ecf20Sopenharmony_ci 29028c2ecf20Sopenharmony_cistatic void srpt_queue_tm_rsp(struct se_cmd *cmd) 29038c2ecf20Sopenharmony_ci{ 29048c2ecf20Sopenharmony_ci srpt_queue_response(cmd); 29058c2ecf20Sopenharmony_ci} 29068c2ecf20Sopenharmony_ci 29078c2ecf20Sopenharmony_ci/* 29088c2ecf20Sopenharmony_ci * This function is called for aborted commands if no response is sent to the 29098c2ecf20Sopenharmony_ci * initiator. Make sure that the credits freed by aborting a command are 29108c2ecf20Sopenharmony_ci * returned to the initiator the next time a response is sent by incrementing 29118c2ecf20Sopenharmony_ci * ch->req_lim_delta. 29128c2ecf20Sopenharmony_ci */ 29138c2ecf20Sopenharmony_cistatic void srpt_aborted_task(struct se_cmd *cmd) 29148c2ecf20Sopenharmony_ci{ 29158c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = container_of(cmd, 29168c2ecf20Sopenharmony_ci struct srpt_send_ioctx, cmd); 29178c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = ioctx->ch; 29188c2ecf20Sopenharmony_ci 29198c2ecf20Sopenharmony_ci atomic_inc(&ch->req_lim_delta); 29208c2ecf20Sopenharmony_ci} 29218c2ecf20Sopenharmony_ci 29228c2ecf20Sopenharmony_cistatic int srpt_queue_status(struct se_cmd *cmd) 29238c2ecf20Sopenharmony_ci{ 29248c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx; 29258c2ecf20Sopenharmony_ci 29268c2ecf20Sopenharmony_ci ioctx = container_of(cmd, struct srpt_send_ioctx, cmd); 29278c2ecf20Sopenharmony_ci BUG_ON(ioctx->sense_data != cmd->sense_buffer); 29288c2ecf20Sopenharmony_ci if (cmd->se_cmd_flags & 29298c2ecf20Sopenharmony_ci (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE)) 29308c2ecf20Sopenharmony_ci WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION); 29318c2ecf20Sopenharmony_ci ioctx->queue_status_only = true; 29328c2ecf20Sopenharmony_ci srpt_queue_response(cmd); 29338c2ecf20Sopenharmony_ci return 0; 29348c2ecf20Sopenharmony_ci} 29358c2ecf20Sopenharmony_ci 29368c2ecf20Sopenharmony_cistatic void srpt_refresh_port_work(struct work_struct *work) 29378c2ecf20Sopenharmony_ci{ 29388c2ecf20Sopenharmony_ci struct srpt_port *sport = container_of(work, struct srpt_port, work); 29398c2ecf20Sopenharmony_ci 29408c2ecf20Sopenharmony_ci srpt_refresh_port(sport); 29418c2ecf20Sopenharmony_ci} 29428c2ecf20Sopenharmony_ci 29438c2ecf20Sopenharmony_ci/** 29448c2ecf20Sopenharmony_ci * srpt_release_sport - disable login and wait for associated channels 29458c2ecf20Sopenharmony_ci * @sport: SRPT HCA port. 29468c2ecf20Sopenharmony_ci */ 29478c2ecf20Sopenharmony_cistatic int srpt_release_sport(struct srpt_port *sport) 29488c2ecf20Sopenharmony_ci{ 29498c2ecf20Sopenharmony_ci DECLARE_COMPLETION_ONSTACK(c); 29508c2ecf20Sopenharmony_ci struct srpt_nexus *nexus, *next_n; 29518c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch; 29528c2ecf20Sopenharmony_ci 29538c2ecf20Sopenharmony_ci WARN_ON_ONCE(irqs_disabled()); 29548c2ecf20Sopenharmony_ci 29558c2ecf20Sopenharmony_ci sport->freed_channels = &c; 29568c2ecf20Sopenharmony_ci 29578c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 29588c2ecf20Sopenharmony_ci srpt_set_enabled(sport, false); 29598c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 29608c2ecf20Sopenharmony_ci 29618c2ecf20Sopenharmony_ci while (atomic_read(&sport->refcount) > 0 && 29628c2ecf20Sopenharmony_ci wait_for_completion_timeout(&c, 5 * HZ) <= 0) { 29638c2ecf20Sopenharmony_ci pr_info("%s_%d: waiting for unregistration of %d sessions ...\n", 29648c2ecf20Sopenharmony_ci dev_name(&sport->sdev->device->dev), sport->port, 29658c2ecf20Sopenharmony_ci atomic_read(&sport->refcount)); 29668c2ecf20Sopenharmony_ci rcu_read_lock(); 29678c2ecf20Sopenharmony_ci list_for_each_entry(nexus, &sport->nexus_list, entry) { 29688c2ecf20Sopenharmony_ci list_for_each_entry(ch, &nexus->ch_list, list) { 29698c2ecf20Sopenharmony_ci pr_info("%s-%d: state %s\n", 29708c2ecf20Sopenharmony_ci ch->sess_name, ch->qp->qp_num, 29718c2ecf20Sopenharmony_ci get_ch_state_name(ch->state)); 29728c2ecf20Sopenharmony_ci } 29738c2ecf20Sopenharmony_ci } 29748c2ecf20Sopenharmony_ci rcu_read_unlock(); 29758c2ecf20Sopenharmony_ci } 29768c2ecf20Sopenharmony_ci 29778c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 29788c2ecf20Sopenharmony_ci list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) { 29798c2ecf20Sopenharmony_ci list_del(&nexus->entry); 29808c2ecf20Sopenharmony_ci kfree_rcu(nexus, rcu); 29818c2ecf20Sopenharmony_ci } 29828c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 29838c2ecf20Sopenharmony_ci 29848c2ecf20Sopenharmony_ci return 0; 29858c2ecf20Sopenharmony_ci} 29868c2ecf20Sopenharmony_ci 29878c2ecf20Sopenharmony_cistruct port_and_port_id { 29888c2ecf20Sopenharmony_ci struct srpt_port *sport; 29898c2ecf20Sopenharmony_ci struct srpt_port_id **port_id; 29908c2ecf20Sopenharmony_ci}; 29918c2ecf20Sopenharmony_ci 29928c2ecf20Sopenharmony_cistatic struct port_and_port_id __srpt_lookup_port(const char *name) 29938c2ecf20Sopenharmony_ci{ 29948c2ecf20Sopenharmony_ci struct ib_device *dev; 29958c2ecf20Sopenharmony_ci struct srpt_device *sdev; 29968c2ecf20Sopenharmony_ci struct srpt_port *sport; 29978c2ecf20Sopenharmony_ci int i; 29988c2ecf20Sopenharmony_ci 29998c2ecf20Sopenharmony_ci list_for_each_entry(sdev, &srpt_dev_list, list) { 30008c2ecf20Sopenharmony_ci dev = sdev->device; 30018c2ecf20Sopenharmony_ci if (!dev) 30028c2ecf20Sopenharmony_ci continue; 30038c2ecf20Sopenharmony_ci 30048c2ecf20Sopenharmony_ci for (i = 0; i < dev->phys_port_cnt; i++) { 30058c2ecf20Sopenharmony_ci sport = &sdev->port[i]; 30068c2ecf20Sopenharmony_ci 30078c2ecf20Sopenharmony_ci if (strcmp(sport->guid_name, name) == 0) { 30088c2ecf20Sopenharmony_ci kref_get(&sdev->refcnt); 30098c2ecf20Sopenharmony_ci return (struct port_and_port_id){ 30108c2ecf20Sopenharmony_ci sport, &sport->guid_id}; 30118c2ecf20Sopenharmony_ci } 30128c2ecf20Sopenharmony_ci if (strcmp(sport->gid_name, name) == 0) { 30138c2ecf20Sopenharmony_ci kref_get(&sdev->refcnt); 30148c2ecf20Sopenharmony_ci return (struct port_and_port_id){ 30158c2ecf20Sopenharmony_ci sport, &sport->gid_id}; 30168c2ecf20Sopenharmony_ci } 30178c2ecf20Sopenharmony_ci } 30188c2ecf20Sopenharmony_ci } 30198c2ecf20Sopenharmony_ci 30208c2ecf20Sopenharmony_ci return (struct port_and_port_id){}; 30218c2ecf20Sopenharmony_ci} 30228c2ecf20Sopenharmony_ci 30238c2ecf20Sopenharmony_ci/** 30248c2ecf20Sopenharmony_ci * srpt_lookup_port() - Look up an RDMA port by name 30258c2ecf20Sopenharmony_ci * @name: ASCII port name 30268c2ecf20Sopenharmony_ci * 30278c2ecf20Sopenharmony_ci * Increments the RDMA port reference count if an RDMA port pointer is returned. 30288c2ecf20Sopenharmony_ci * The caller must drop that reference count by calling srpt_port_put_ref(). 30298c2ecf20Sopenharmony_ci */ 30308c2ecf20Sopenharmony_cistatic struct port_and_port_id srpt_lookup_port(const char *name) 30318c2ecf20Sopenharmony_ci{ 30328c2ecf20Sopenharmony_ci struct port_and_port_id papi; 30338c2ecf20Sopenharmony_ci 30348c2ecf20Sopenharmony_ci spin_lock(&srpt_dev_lock); 30358c2ecf20Sopenharmony_ci papi = __srpt_lookup_port(name); 30368c2ecf20Sopenharmony_ci spin_unlock(&srpt_dev_lock); 30378c2ecf20Sopenharmony_ci 30388c2ecf20Sopenharmony_ci return papi; 30398c2ecf20Sopenharmony_ci} 30408c2ecf20Sopenharmony_ci 30418c2ecf20Sopenharmony_cistatic void srpt_free_srq(struct srpt_device *sdev) 30428c2ecf20Sopenharmony_ci{ 30438c2ecf20Sopenharmony_ci if (!sdev->srq) 30448c2ecf20Sopenharmony_ci return; 30458c2ecf20Sopenharmony_ci 30468c2ecf20Sopenharmony_ci ib_destroy_srq(sdev->srq); 30478c2ecf20Sopenharmony_ci srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev, 30488c2ecf20Sopenharmony_ci sdev->srq_size, sdev->req_buf_cache, 30498c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 30508c2ecf20Sopenharmony_ci kmem_cache_destroy(sdev->req_buf_cache); 30518c2ecf20Sopenharmony_ci sdev->srq = NULL; 30528c2ecf20Sopenharmony_ci} 30538c2ecf20Sopenharmony_ci 30548c2ecf20Sopenharmony_cistatic int srpt_alloc_srq(struct srpt_device *sdev) 30558c2ecf20Sopenharmony_ci{ 30568c2ecf20Sopenharmony_ci struct ib_srq_init_attr srq_attr = { 30578c2ecf20Sopenharmony_ci .event_handler = srpt_srq_event, 30588c2ecf20Sopenharmony_ci .srq_context = (void *)sdev, 30598c2ecf20Sopenharmony_ci .attr.max_wr = sdev->srq_size, 30608c2ecf20Sopenharmony_ci .attr.max_sge = 1, 30618c2ecf20Sopenharmony_ci .srq_type = IB_SRQT_BASIC, 30628c2ecf20Sopenharmony_ci }; 30638c2ecf20Sopenharmony_ci struct ib_device *device = sdev->device; 30648c2ecf20Sopenharmony_ci struct ib_srq *srq; 30658c2ecf20Sopenharmony_ci int i; 30668c2ecf20Sopenharmony_ci 30678c2ecf20Sopenharmony_ci WARN_ON_ONCE(sdev->srq); 30688c2ecf20Sopenharmony_ci srq = ib_create_srq(sdev->pd, &srq_attr); 30698c2ecf20Sopenharmony_ci if (IS_ERR(srq)) { 30708c2ecf20Sopenharmony_ci pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq)); 30718c2ecf20Sopenharmony_ci return PTR_ERR(srq); 30728c2ecf20Sopenharmony_ci } 30738c2ecf20Sopenharmony_ci 30748c2ecf20Sopenharmony_ci pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size, 30758c2ecf20Sopenharmony_ci sdev->device->attrs.max_srq_wr, dev_name(&device->dev)); 30768c2ecf20Sopenharmony_ci 30778c2ecf20Sopenharmony_ci sdev->req_buf_cache = kmem_cache_create("srpt-srq-req-buf", 30788c2ecf20Sopenharmony_ci srp_max_req_size, 0, 0, NULL); 30798c2ecf20Sopenharmony_ci if (!sdev->req_buf_cache) 30808c2ecf20Sopenharmony_ci goto free_srq; 30818c2ecf20Sopenharmony_ci 30828c2ecf20Sopenharmony_ci sdev->ioctx_ring = (struct srpt_recv_ioctx **) 30838c2ecf20Sopenharmony_ci srpt_alloc_ioctx_ring(sdev, sdev->srq_size, 30848c2ecf20Sopenharmony_ci sizeof(*sdev->ioctx_ring[0]), 30858c2ecf20Sopenharmony_ci sdev->req_buf_cache, 0, DMA_FROM_DEVICE); 30868c2ecf20Sopenharmony_ci if (!sdev->ioctx_ring) 30878c2ecf20Sopenharmony_ci goto free_cache; 30888c2ecf20Sopenharmony_ci 30898c2ecf20Sopenharmony_ci sdev->use_srq = true; 30908c2ecf20Sopenharmony_ci sdev->srq = srq; 30918c2ecf20Sopenharmony_ci 30928c2ecf20Sopenharmony_ci for (i = 0; i < sdev->srq_size; ++i) { 30938c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list); 30948c2ecf20Sopenharmony_ci srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]); 30958c2ecf20Sopenharmony_ci } 30968c2ecf20Sopenharmony_ci 30978c2ecf20Sopenharmony_ci return 0; 30988c2ecf20Sopenharmony_ci 30998c2ecf20Sopenharmony_cifree_cache: 31008c2ecf20Sopenharmony_ci kmem_cache_destroy(sdev->req_buf_cache); 31018c2ecf20Sopenharmony_ci 31028c2ecf20Sopenharmony_cifree_srq: 31038c2ecf20Sopenharmony_ci ib_destroy_srq(srq); 31048c2ecf20Sopenharmony_ci return -ENOMEM; 31058c2ecf20Sopenharmony_ci} 31068c2ecf20Sopenharmony_ci 31078c2ecf20Sopenharmony_cistatic int srpt_use_srq(struct srpt_device *sdev, bool use_srq) 31088c2ecf20Sopenharmony_ci{ 31098c2ecf20Sopenharmony_ci struct ib_device *device = sdev->device; 31108c2ecf20Sopenharmony_ci int ret = 0; 31118c2ecf20Sopenharmony_ci 31128c2ecf20Sopenharmony_ci if (!use_srq) { 31138c2ecf20Sopenharmony_ci srpt_free_srq(sdev); 31148c2ecf20Sopenharmony_ci sdev->use_srq = false; 31158c2ecf20Sopenharmony_ci } else if (use_srq && !sdev->srq) { 31168c2ecf20Sopenharmony_ci ret = srpt_alloc_srq(sdev); 31178c2ecf20Sopenharmony_ci } 31188c2ecf20Sopenharmony_ci pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, 31198c2ecf20Sopenharmony_ci dev_name(&device->dev), sdev->use_srq, ret); 31208c2ecf20Sopenharmony_ci return ret; 31218c2ecf20Sopenharmony_ci} 31228c2ecf20Sopenharmony_ci 31238c2ecf20Sopenharmony_cistatic void srpt_free_sdev(struct kref *refcnt) 31248c2ecf20Sopenharmony_ci{ 31258c2ecf20Sopenharmony_ci struct srpt_device *sdev = container_of(refcnt, typeof(*sdev), refcnt); 31268c2ecf20Sopenharmony_ci 31278c2ecf20Sopenharmony_ci kfree(sdev); 31288c2ecf20Sopenharmony_ci} 31298c2ecf20Sopenharmony_ci 31308c2ecf20Sopenharmony_cistatic void srpt_sdev_put(struct srpt_device *sdev) 31318c2ecf20Sopenharmony_ci{ 31328c2ecf20Sopenharmony_ci kref_put(&sdev->refcnt, srpt_free_sdev); 31338c2ecf20Sopenharmony_ci} 31348c2ecf20Sopenharmony_ci 31358c2ecf20Sopenharmony_ci/** 31368c2ecf20Sopenharmony_ci * srpt_add_one - InfiniBand device addition callback function 31378c2ecf20Sopenharmony_ci * @device: Describes a HCA. 31388c2ecf20Sopenharmony_ci */ 31398c2ecf20Sopenharmony_cistatic int srpt_add_one(struct ib_device *device) 31408c2ecf20Sopenharmony_ci{ 31418c2ecf20Sopenharmony_ci struct srpt_device *sdev; 31428c2ecf20Sopenharmony_ci struct srpt_port *sport; 31438c2ecf20Sopenharmony_ci int i, ret; 31448c2ecf20Sopenharmony_ci 31458c2ecf20Sopenharmony_ci pr_debug("device = %p\n", device); 31468c2ecf20Sopenharmony_ci 31478c2ecf20Sopenharmony_ci sdev = kzalloc(struct_size(sdev, port, device->phys_port_cnt), 31488c2ecf20Sopenharmony_ci GFP_KERNEL); 31498c2ecf20Sopenharmony_ci if (!sdev) 31508c2ecf20Sopenharmony_ci return -ENOMEM; 31518c2ecf20Sopenharmony_ci 31528c2ecf20Sopenharmony_ci kref_init(&sdev->refcnt); 31538c2ecf20Sopenharmony_ci sdev->device = device; 31548c2ecf20Sopenharmony_ci mutex_init(&sdev->sdev_mutex); 31558c2ecf20Sopenharmony_ci 31568c2ecf20Sopenharmony_ci sdev->pd = ib_alloc_pd(device, 0); 31578c2ecf20Sopenharmony_ci if (IS_ERR(sdev->pd)) { 31588c2ecf20Sopenharmony_ci ret = PTR_ERR(sdev->pd); 31598c2ecf20Sopenharmony_ci goto free_dev; 31608c2ecf20Sopenharmony_ci } 31618c2ecf20Sopenharmony_ci 31628c2ecf20Sopenharmony_ci sdev->lkey = sdev->pd->local_dma_lkey; 31638c2ecf20Sopenharmony_ci 31648c2ecf20Sopenharmony_ci sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr); 31658c2ecf20Sopenharmony_ci 31668c2ecf20Sopenharmony_ci srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq); 31678c2ecf20Sopenharmony_ci 31688c2ecf20Sopenharmony_ci if (!srpt_service_guid) 31698c2ecf20Sopenharmony_ci srpt_service_guid = be64_to_cpu(device->node_guid); 31708c2ecf20Sopenharmony_ci 31718c2ecf20Sopenharmony_ci if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND) 31728c2ecf20Sopenharmony_ci sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev); 31738c2ecf20Sopenharmony_ci if (IS_ERR(sdev->cm_id)) { 31748c2ecf20Sopenharmony_ci pr_info("ib_create_cm_id() failed: %ld\n", 31758c2ecf20Sopenharmony_ci PTR_ERR(sdev->cm_id)); 31768c2ecf20Sopenharmony_ci ret = PTR_ERR(sdev->cm_id); 31778c2ecf20Sopenharmony_ci sdev->cm_id = NULL; 31788c2ecf20Sopenharmony_ci if (!rdma_cm_id) 31798c2ecf20Sopenharmony_ci goto err_ring; 31808c2ecf20Sopenharmony_ci } 31818c2ecf20Sopenharmony_ci 31828c2ecf20Sopenharmony_ci /* print out target login information */ 31838c2ecf20Sopenharmony_ci pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,pkey=ffff,service_id=%016llx\n", 31848c2ecf20Sopenharmony_ci srpt_service_guid, srpt_service_guid, srpt_service_guid); 31858c2ecf20Sopenharmony_ci 31868c2ecf20Sopenharmony_ci /* 31878c2ecf20Sopenharmony_ci * We do not have a consistent service_id (ie. also id_ext of target_id) 31888c2ecf20Sopenharmony_ci * to identify this target. We currently use the guid of the first HCA 31898c2ecf20Sopenharmony_ci * in the system as service_id; therefore, the target_id will change 31908c2ecf20Sopenharmony_ci * if this HCA is gone bad and replaced by different HCA 31918c2ecf20Sopenharmony_ci */ 31928c2ecf20Sopenharmony_ci ret = sdev->cm_id ? 31938c2ecf20Sopenharmony_ci ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) : 31948c2ecf20Sopenharmony_ci 0; 31958c2ecf20Sopenharmony_ci if (ret < 0) { 31968c2ecf20Sopenharmony_ci pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret, 31978c2ecf20Sopenharmony_ci sdev->cm_id->state); 31988c2ecf20Sopenharmony_ci goto err_cm; 31998c2ecf20Sopenharmony_ci } 32008c2ecf20Sopenharmony_ci 32018c2ecf20Sopenharmony_ci INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device, 32028c2ecf20Sopenharmony_ci srpt_event_handler); 32038c2ecf20Sopenharmony_ci 32048c2ecf20Sopenharmony_ci for (i = 1; i <= sdev->device->phys_port_cnt; i++) { 32058c2ecf20Sopenharmony_ci sport = &sdev->port[i - 1]; 32068c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&sport->nexus_list); 32078c2ecf20Sopenharmony_ci mutex_init(&sport->mutex); 32088c2ecf20Sopenharmony_ci sport->sdev = sdev; 32098c2ecf20Sopenharmony_ci sport->port = i; 32108c2ecf20Sopenharmony_ci sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE; 32118c2ecf20Sopenharmony_ci sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE; 32128c2ecf20Sopenharmony_ci sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE; 32138c2ecf20Sopenharmony_ci sport->port_attrib.use_srq = false; 32148c2ecf20Sopenharmony_ci INIT_WORK(&sport->work, srpt_refresh_port_work); 32158c2ecf20Sopenharmony_ci 32168c2ecf20Sopenharmony_ci ret = srpt_refresh_port(sport); 32178c2ecf20Sopenharmony_ci if (ret) { 32188c2ecf20Sopenharmony_ci pr_err("MAD registration failed for %s-%d.\n", 32198c2ecf20Sopenharmony_ci dev_name(&sdev->device->dev), i); 32208c2ecf20Sopenharmony_ci i--; 32218c2ecf20Sopenharmony_ci goto err_port; 32228c2ecf20Sopenharmony_ci } 32238c2ecf20Sopenharmony_ci } 32248c2ecf20Sopenharmony_ci 32258c2ecf20Sopenharmony_ci ib_register_event_handler(&sdev->event_handler); 32268c2ecf20Sopenharmony_ci spin_lock(&srpt_dev_lock); 32278c2ecf20Sopenharmony_ci list_add_tail(&sdev->list, &srpt_dev_list); 32288c2ecf20Sopenharmony_ci spin_unlock(&srpt_dev_lock); 32298c2ecf20Sopenharmony_ci 32308c2ecf20Sopenharmony_ci ib_set_client_data(device, &srpt_client, sdev); 32318c2ecf20Sopenharmony_ci pr_debug("added %s.\n", dev_name(&device->dev)); 32328c2ecf20Sopenharmony_ci return 0; 32338c2ecf20Sopenharmony_ci 32348c2ecf20Sopenharmony_cierr_port: 32358c2ecf20Sopenharmony_ci srpt_unregister_mad_agent(sdev, i); 32368c2ecf20Sopenharmony_cierr_cm: 32378c2ecf20Sopenharmony_ci if (sdev->cm_id) 32388c2ecf20Sopenharmony_ci ib_destroy_cm_id(sdev->cm_id); 32398c2ecf20Sopenharmony_cierr_ring: 32408c2ecf20Sopenharmony_ci srpt_free_srq(sdev); 32418c2ecf20Sopenharmony_ci ib_dealloc_pd(sdev->pd); 32428c2ecf20Sopenharmony_cifree_dev: 32438c2ecf20Sopenharmony_ci srpt_sdev_put(sdev); 32448c2ecf20Sopenharmony_ci pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev)); 32458c2ecf20Sopenharmony_ci return ret; 32468c2ecf20Sopenharmony_ci} 32478c2ecf20Sopenharmony_ci 32488c2ecf20Sopenharmony_ci/** 32498c2ecf20Sopenharmony_ci * srpt_remove_one - InfiniBand device removal callback function 32508c2ecf20Sopenharmony_ci * @device: Describes a HCA. 32518c2ecf20Sopenharmony_ci * @client_data: The value passed as the third argument to ib_set_client_data(). 32528c2ecf20Sopenharmony_ci */ 32538c2ecf20Sopenharmony_cistatic void srpt_remove_one(struct ib_device *device, void *client_data) 32548c2ecf20Sopenharmony_ci{ 32558c2ecf20Sopenharmony_ci struct srpt_device *sdev = client_data; 32568c2ecf20Sopenharmony_ci int i; 32578c2ecf20Sopenharmony_ci 32588c2ecf20Sopenharmony_ci srpt_unregister_mad_agent(sdev, sdev->device->phys_port_cnt); 32598c2ecf20Sopenharmony_ci 32608c2ecf20Sopenharmony_ci ib_unregister_event_handler(&sdev->event_handler); 32618c2ecf20Sopenharmony_ci 32628c2ecf20Sopenharmony_ci /* Cancel any work queued by the just unregistered IB event handler. */ 32638c2ecf20Sopenharmony_ci for (i = 0; i < sdev->device->phys_port_cnt; i++) 32648c2ecf20Sopenharmony_ci cancel_work_sync(&sdev->port[i].work); 32658c2ecf20Sopenharmony_ci 32668c2ecf20Sopenharmony_ci if (sdev->cm_id) 32678c2ecf20Sopenharmony_ci ib_destroy_cm_id(sdev->cm_id); 32688c2ecf20Sopenharmony_ci 32698c2ecf20Sopenharmony_ci ib_set_client_data(device, &srpt_client, NULL); 32708c2ecf20Sopenharmony_ci 32718c2ecf20Sopenharmony_ci /* 32728c2ecf20Sopenharmony_ci * Unregistering a target must happen after destroying sdev->cm_id 32738c2ecf20Sopenharmony_ci * such that no new SRP_LOGIN_REQ information units can arrive while 32748c2ecf20Sopenharmony_ci * destroying the target. 32758c2ecf20Sopenharmony_ci */ 32768c2ecf20Sopenharmony_ci spin_lock(&srpt_dev_lock); 32778c2ecf20Sopenharmony_ci list_del(&sdev->list); 32788c2ecf20Sopenharmony_ci spin_unlock(&srpt_dev_lock); 32798c2ecf20Sopenharmony_ci 32808c2ecf20Sopenharmony_ci for (i = 0; i < sdev->device->phys_port_cnt; i++) 32818c2ecf20Sopenharmony_ci srpt_release_sport(&sdev->port[i]); 32828c2ecf20Sopenharmony_ci 32838c2ecf20Sopenharmony_ci srpt_free_srq(sdev); 32848c2ecf20Sopenharmony_ci 32858c2ecf20Sopenharmony_ci ib_dealloc_pd(sdev->pd); 32868c2ecf20Sopenharmony_ci 32878c2ecf20Sopenharmony_ci srpt_sdev_put(sdev); 32888c2ecf20Sopenharmony_ci} 32898c2ecf20Sopenharmony_ci 32908c2ecf20Sopenharmony_cistatic struct ib_client srpt_client = { 32918c2ecf20Sopenharmony_ci .name = DRV_NAME, 32928c2ecf20Sopenharmony_ci .add = srpt_add_one, 32938c2ecf20Sopenharmony_ci .remove = srpt_remove_one 32948c2ecf20Sopenharmony_ci}; 32958c2ecf20Sopenharmony_ci 32968c2ecf20Sopenharmony_cistatic int srpt_check_true(struct se_portal_group *se_tpg) 32978c2ecf20Sopenharmony_ci{ 32988c2ecf20Sopenharmony_ci return 1; 32998c2ecf20Sopenharmony_ci} 33008c2ecf20Sopenharmony_ci 33018c2ecf20Sopenharmony_cistatic int srpt_check_false(struct se_portal_group *se_tpg) 33028c2ecf20Sopenharmony_ci{ 33038c2ecf20Sopenharmony_ci return 0; 33048c2ecf20Sopenharmony_ci} 33058c2ecf20Sopenharmony_ci 33068c2ecf20Sopenharmony_cistatic struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg) 33078c2ecf20Sopenharmony_ci{ 33088c2ecf20Sopenharmony_ci return tpg->se_tpg_wwn->priv; 33098c2ecf20Sopenharmony_ci} 33108c2ecf20Sopenharmony_ci 33118c2ecf20Sopenharmony_cistatic struct srpt_port_id *srpt_wwn_to_sport_id(struct se_wwn *wwn) 33128c2ecf20Sopenharmony_ci{ 33138c2ecf20Sopenharmony_ci struct srpt_port *sport = wwn->priv; 33148c2ecf20Sopenharmony_ci 33158c2ecf20Sopenharmony_ci if (sport->guid_id && &sport->guid_id->wwn == wwn) 33168c2ecf20Sopenharmony_ci return sport->guid_id; 33178c2ecf20Sopenharmony_ci if (sport->gid_id && &sport->gid_id->wwn == wwn) 33188c2ecf20Sopenharmony_ci return sport->gid_id; 33198c2ecf20Sopenharmony_ci WARN_ON_ONCE(true); 33208c2ecf20Sopenharmony_ci return NULL; 33218c2ecf20Sopenharmony_ci} 33228c2ecf20Sopenharmony_ci 33238c2ecf20Sopenharmony_cistatic char *srpt_get_fabric_wwn(struct se_portal_group *tpg) 33248c2ecf20Sopenharmony_ci{ 33258c2ecf20Sopenharmony_ci struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg); 33268c2ecf20Sopenharmony_ci 33278c2ecf20Sopenharmony_ci return stpg->sport_id->name; 33288c2ecf20Sopenharmony_ci} 33298c2ecf20Sopenharmony_ci 33308c2ecf20Sopenharmony_cistatic u16 srpt_get_tag(struct se_portal_group *tpg) 33318c2ecf20Sopenharmony_ci{ 33328c2ecf20Sopenharmony_ci return 1; 33338c2ecf20Sopenharmony_ci} 33348c2ecf20Sopenharmony_ci 33358c2ecf20Sopenharmony_cistatic u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg) 33368c2ecf20Sopenharmony_ci{ 33378c2ecf20Sopenharmony_ci return 1; 33388c2ecf20Sopenharmony_ci} 33398c2ecf20Sopenharmony_ci 33408c2ecf20Sopenharmony_cistatic void srpt_release_cmd(struct se_cmd *se_cmd) 33418c2ecf20Sopenharmony_ci{ 33428c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx = container_of(se_cmd, 33438c2ecf20Sopenharmony_ci struct srpt_send_ioctx, cmd); 33448c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = ioctx->ch; 33458c2ecf20Sopenharmony_ci struct srpt_recv_ioctx *recv_ioctx = ioctx->recv_ioctx; 33468c2ecf20Sopenharmony_ci 33478c2ecf20Sopenharmony_ci WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE && 33488c2ecf20Sopenharmony_ci !(ioctx->cmd.transport_state & CMD_T_ABORTED)); 33498c2ecf20Sopenharmony_ci 33508c2ecf20Sopenharmony_ci if (recv_ioctx) { 33518c2ecf20Sopenharmony_ci WARN_ON_ONCE(!list_empty(&recv_ioctx->wait_list)); 33528c2ecf20Sopenharmony_ci ioctx->recv_ioctx = NULL; 33538c2ecf20Sopenharmony_ci srpt_post_recv(ch->sport->sdev, ch, recv_ioctx); 33548c2ecf20Sopenharmony_ci } 33558c2ecf20Sopenharmony_ci 33568c2ecf20Sopenharmony_ci if (ioctx->n_rw_ctx) { 33578c2ecf20Sopenharmony_ci srpt_free_rw_ctxs(ch, ioctx); 33588c2ecf20Sopenharmony_ci ioctx->n_rw_ctx = 0; 33598c2ecf20Sopenharmony_ci } 33608c2ecf20Sopenharmony_ci 33618c2ecf20Sopenharmony_ci target_free_tag(se_cmd->se_sess, se_cmd); 33628c2ecf20Sopenharmony_ci} 33638c2ecf20Sopenharmony_ci 33648c2ecf20Sopenharmony_ci/** 33658c2ecf20Sopenharmony_ci * srpt_close_session - forcibly close a session 33668c2ecf20Sopenharmony_ci * @se_sess: SCSI target session. 33678c2ecf20Sopenharmony_ci * 33688c2ecf20Sopenharmony_ci * Callback function invoked by the TCM core to clean up sessions associated 33698c2ecf20Sopenharmony_ci * with a node ACL when the user invokes 33708c2ecf20Sopenharmony_ci * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id 33718c2ecf20Sopenharmony_ci */ 33728c2ecf20Sopenharmony_cistatic void srpt_close_session(struct se_session *se_sess) 33738c2ecf20Sopenharmony_ci{ 33748c2ecf20Sopenharmony_ci struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr; 33758c2ecf20Sopenharmony_ci 33768c2ecf20Sopenharmony_ci srpt_disconnect_ch_sync(ch); 33778c2ecf20Sopenharmony_ci} 33788c2ecf20Sopenharmony_ci 33798c2ecf20Sopenharmony_ci/** 33808c2ecf20Sopenharmony_ci * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB) 33818c2ecf20Sopenharmony_ci * @se_sess: SCSI target session. 33828c2ecf20Sopenharmony_ci * 33838c2ecf20Sopenharmony_ci * A quote from RFC 4455 (SCSI-MIB) about this MIB object: 33848c2ecf20Sopenharmony_ci * This object represents an arbitrary integer used to uniquely identify a 33858c2ecf20Sopenharmony_ci * particular attached remote initiator port to a particular SCSI target port 33868c2ecf20Sopenharmony_ci * within a particular SCSI target device within a particular SCSI instance. 33878c2ecf20Sopenharmony_ci */ 33888c2ecf20Sopenharmony_cistatic u32 srpt_sess_get_index(struct se_session *se_sess) 33898c2ecf20Sopenharmony_ci{ 33908c2ecf20Sopenharmony_ci return 0; 33918c2ecf20Sopenharmony_ci} 33928c2ecf20Sopenharmony_ci 33938c2ecf20Sopenharmony_cistatic void srpt_set_default_node_attrs(struct se_node_acl *nacl) 33948c2ecf20Sopenharmony_ci{ 33958c2ecf20Sopenharmony_ci} 33968c2ecf20Sopenharmony_ci 33978c2ecf20Sopenharmony_ci/* Note: only used from inside debug printk's by the TCM core. */ 33988c2ecf20Sopenharmony_cistatic int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd) 33998c2ecf20Sopenharmony_ci{ 34008c2ecf20Sopenharmony_ci struct srpt_send_ioctx *ioctx; 34018c2ecf20Sopenharmony_ci 34028c2ecf20Sopenharmony_ci ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd); 34038c2ecf20Sopenharmony_ci return ioctx->state; 34048c2ecf20Sopenharmony_ci} 34058c2ecf20Sopenharmony_ci 34068c2ecf20Sopenharmony_cistatic int srpt_parse_guid(u64 *guid, const char *name) 34078c2ecf20Sopenharmony_ci{ 34088c2ecf20Sopenharmony_ci u16 w[4]; 34098c2ecf20Sopenharmony_ci int ret = -EINVAL; 34108c2ecf20Sopenharmony_ci 34118c2ecf20Sopenharmony_ci if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4) 34128c2ecf20Sopenharmony_ci goto out; 34138c2ecf20Sopenharmony_ci *guid = get_unaligned_be64(w); 34148c2ecf20Sopenharmony_ci ret = 0; 34158c2ecf20Sopenharmony_ciout: 34168c2ecf20Sopenharmony_ci return ret; 34178c2ecf20Sopenharmony_ci} 34188c2ecf20Sopenharmony_ci 34198c2ecf20Sopenharmony_ci/** 34208c2ecf20Sopenharmony_ci * srpt_parse_i_port_id - parse an initiator port ID 34218c2ecf20Sopenharmony_ci * @name: ASCII representation of a 128-bit initiator port ID. 34228c2ecf20Sopenharmony_ci * @i_port_id: Binary 128-bit port ID. 34238c2ecf20Sopenharmony_ci */ 34248c2ecf20Sopenharmony_cistatic int srpt_parse_i_port_id(u8 i_port_id[16], const char *name) 34258c2ecf20Sopenharmony_ci{ 34268c2ecf20Sopenharmony_ci const char *p; 34278c2ecf20Sopenharmony_ci unsigned len, count, leading_zero_bytes; 34288c2ecf20Sopenharmony_ci int ret; 34298c2ecf20Sopenharmony_ci 34308c2ecf20Sopenharmony_ci p = name; 34318c2ecf20Sopenharmony_ci if (strncasecmp(p, "0x", 2) == 0) 34328c2ecf20Sopenharmony_ci p += 2; 34338c2ecf20Sopenharmony_ci ret = -EINVAL; 34348c2ecf20Sopenharmony_ci len = strlen(p); 34358c2ecf20Sopenharmony_ci if (len % 2) 34368c2ecf20Sopenharmony_ci goto out; 34378c2ecf20Sopenharmony_ci count = min(len / 2, 16U); 34388c2ecf20Sopenharmony_ci leading_zero_bytes = 16 - count; 34398c2ecf20Sopenharmony_ci memset(i_port_id, 0, leading_zero_bytes); 34408c2ecf20Sopenharmony_ci ret = hex2bin(i_port_id + leading_zero_bytes, p, count); 34418c2ecf20Sopenharmony_ci 34428c2ecf20Sopenharmony_ciout: 34438c2ecf20Sopenharmony_ci return ret; 34448c2ecf20Sopenharmony_ci} 34458c2ecf20Sopenharmony_ci 34468c2ecf20Sopenharmony_ci/* 34478c2ecf20Sopenharmony_ci * configfs callback function invoked for mkdir 34488c2ecf20Sopenharmony_ci * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id 34498c2ecf20Sopenharmony_ci * 34508c2ecf20Sopenharmony_ci * i_port_id must be an initiator port GUID, GID or IP address. See also the 34518c2ecf20Sopenharmony_ci * target_alloc_session() calls in this driver. Examples of valid initiator 34528c2ecf20Sopenharmony_ci * port IDs: 34538c2ecf20Sopenharmony_ci * 0x0000000000000000505400fffe4a0b7b 34548c2ecf20Sopenharmony_ci * 0000000000000000505400fffe4a0b7b 34558c2ecf20Sopenharmony_ci * 5054:00ff:fe4a:0b7b 34568c2ecf20Sopenharmony_ci * 192.168.122.76 34578c2ecf20Sopenharmony_ci */ 34588c2ecf20Sopenharmony_cistatic int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name) 34598c2ecf20Sopenharmony_ci{ 34608c2ecf20Sopenharmony_ci struct sockaddr_storage sa; 34618c2ecf20Sopenharmony_ci u64 guid; 34628c2ecf20Sopenharmony_ci u8 i_port_id[16]; 34638c2ecf20Sopenharmony_ci int ret; 34648c2ecf20Sopenharmony_ci 34658c2ecf20Sopenharmony_ci ret = srpt_parse_guid(&guid, name); 34668c2ecf20Sopenharmony_ci if (ret < 0) 34678c2ecf20Sopenharmony_ci ret = srpt_parse_i_port_id(i_port_id, name); 34688c2ecf20Sopenharmony_ci if (ret < 0) 34698c2ecf20Sopenharmony_ci ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL, 34708c2ecf20Sopenharmony_ci &sa); 34718c2ecf20Sopenharmony_ci if (ret < 0) 34728c2ecf20Sopenharmony_ci pr_err("invalid initiator port ID %s\n", name); 34738c2ecf20Sopenharmony_ci return ret; 34748c2ecf20Sopenharmony_ci} 34758c2ecf20Sopenharmony_ci 34768c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item, 34778c2ecf20Sopenharmony_ci char *page) 34788c2ecf20Sopenharmony_ci{ 34798c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 34808c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 34818c2ecf20Sopenharmony_ci 34828c2ecf20Sopenharmony_ci return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size); 34838c2ecf20Sopenharmony_ci} 34848c2ecf20Sopenharmony_ci 34858c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item, 34868c2ecf20Sopenharmony_ci const char *page, size_t count) 34878c2ecf20Sopenharmony_ci{ 34888c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 34898c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 34908c2ecf20Sopenharmony_ci unsigned long val; 34918c2ecf20Sopenharmony_ci int ret; 34928c2ecf20Sopenharmony_ci 34938c2ecf20Sopenharmony_ci ret = kstrtoul(page, 0, &val); 34948c2ecf20Sopenharmony_ci if (ret < 0) { 34958c2ecf20Sopenharmony_ci pr_err("kstrtoul() failed with ret: %d\n", ret); 34968c2ecf20Sopenharmony_ci return -EINVAL; 34978c2ecf20Sopenharmony_ci } 34988c2ecf20Sopenharmony_ci if (val > MAX_SRPT_RDMA_SIZE) { 34998c2ecf20Sopenharmony_ci pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val, 35008c2ecf20Sopenharmony_ci MAX_SRPT_RDMA_SIZE); 35018c2ecf20Sopenharmony_ci return -EINVAL; 35028c2ecf20Sopenharmony_ci } 35038c2ecf20Sopenharmony_ci if (val < DEFAULT_MAX_RDMA_SIZE) { 35048c2ecf20Sopenharmony_ci pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n", 35058c2ecf20Sopenharmony_ci val, DEFAULT_MAX_RDMA_SIZE); 35068c2ecf20Sopenharmony_ci return -EINVAL; 35078c2ecf20Sopenharmony_ci } 35088c2ecf20Sopenharmony_ci sport->port_attrib.srp_max_rdma_size = val; 35098c2ecf20Sopenharmony_ci 35108c2ecf20Sopenharmony_ci return count; 35118c2ecf20Sopenharmony_ci} 35128c2ecf20Sopenharmony_ci 35138c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item, 35148c2ecf20Sopenharmony_ci char *page) 35158c2ecf20Sopenharmony_ci{ 35168c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 35178c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 35188c2ecf20Sopenharmony_ci 35198c2ecf20Sopenharmony_ci return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size); 35208c2ecf20Sopenharmony_ci} 35218c2ecf20Sopenharmony_ci 35228c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item, 35238c2ecf20Sopenharmony_ci const char *page, size_t count) 35248c2ecf20Sopenharmony_ci{ 35258c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 35268c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 35278c2ecf20Sopenharmony_ci unsigned long val; 35288c2ecf20Sopenharmony_ci int ret; 35298c2ecf20Sopenharmony_ci 35308c2ecf20Sopenharmony_ci ret = kstrtoul(page, 0, &val); 35318c2ecf20Sopenharmony_ci if (ret < 0) { 35328c2ecf20Sopenharmony_ci pr_err("kstrtoul() failed with ret: %d\n", ret); 35338c2ecf20Sopenharmony_ci return -EINVAL; 35348c2ecf20Sopenharmony_ci } 35358c2ecf20Sopenharmony_ci if (val > MAX_SRPT_RSP_SIZE) { 35368c2ecf20Sopenharmony_ci pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val, 35378c2ecf20Sopenharmony_ci MAX_SRPT_RSP_SIZE); 35388c2ecf20Sopenharmony_ci return -EINVAL; 35398c2ecf20Sopenharmony_ci } 35408c2ecf20Sopenharmony_ci if (val < MIN_MAX_RSP_SIZE) { 35418c2ecf20Sopenharmony_ci pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val, 35428c2ecf20Sopenharmony_ci MIN_MAX_RSP_SIZE); 35438c2ecf20Sopenharmony_ci return -EINVAL; 35448c2ecf20Sopenharmony_ci } 35458c2ecf20Sopenharmony_ci sport->port_attrib.srp_max_rsp_size = val; 35468c2ecf20Sopenharmony_ci 35478c2ecf20Sopenharmony_ci return count; 35488c2ecf20Sopenharmony_ci} 35498c2ecf20Sopenharmony_ci 35508c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item, 35518c2ecf20Sopenharmony_ci char *page) 35528c2ecf20Sopenharmony_ci{ 35538c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 35548c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 35558c2ecf20Sopenharmony_ci 35568c2ecf20Sopenharmony_ci return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size); 35578c2ecf20Sopenharmony_ci} 35588c2ecf20Sopenharmony_ci 35598c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item, 35608c2ecf20Sopenharmony_ci const char *page, size_t count) 35618c2ecf20Sopenharmony_ci{ 35628c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 35638c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 35648c2ecf20Sopenharmony_ci unsigned long val; 35658c2ecf20Sopenharmony_ci int ret; 35668c2ecf20Sopenharmony_ci 35678c2ecf20Sopenharmony_ci ret = kstrtoul(page, 0, &val); 35688c2ecf20Sopenharmony_ci if (ret < 0) { 35698c2ecf20Sopenharmony_ci pr_err("kstrtoul() failed with ret: %d\n", ret); 35708c2ecf20Sopenharmony_ci return -EINVAL; 35718c2ecf20Sopenharmony_ci } 35728c2ecf20Sopenharmony_ci if (val > MAX_SRPT_SRQ_SIZE) { 35738c2ecf20Sopenharmony_ci pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val, 35748c2ecf20Sopenharmony_ci MAX_SRPT_SRQ_SIZE); 35758c2ecf20Sopenharmony_ci return -EINVAL; 35768c2ecf20Sopenharmony_ci } 35778c2ecf20Sopenharmony_ci if (val < MIN_SRPT_SRQ_SIZE) { 35788c2ecf20Sopenharmony_ci pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val, 35798c2ecf20Sopenharmony_ci MIN_SRPT_SRQ_SIZE); 35808c2ecf20Sopenharmony_ci return -EINVAL; 35818c2ecf20Sopenharmony_ci } 35828c2ecf20Sopenharmony_ci sport->port_attrib.srp_sq_size = val; 35838c2ecf20Sopenharmony_ci 35848c2ecf20Sopenharmony_ci return count; 35858c2ecf20Sopenharmony_ci} 35868c2ecf20Sopenharmony_ci 35878c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item, 35888c2ecf20Sopenharmony_ci char *page) 35898c2ecf20Sopenharmony_ci{ 35908c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 35918c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 35928c2ecf20Sopenharmony_ci 35938c2ecf20Sopenharmony_ci return sprintf(page, "%d\n", sport->port_attrib.use_srq); 35948c2ecf20Sopenharmony_ci} 35958c2ecf20Sopenharmony_ci 35968c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item, 35978c2ecf20Sopenharmony_ci const char *page, size_t count) 35988c2ecf20Sopenharmony_ci{ 35998c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = attrib_to_tpg(item); 36008c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 36018c2ecf20Sopenharmony_ci struct srpt_device *sdev = sport->sdev; 36028c2ecf20Sopenharmony_ci unsigned long val; 36038c2ecf20Sopenharmony_ci bool enabled; 36048c2ecf20Sopenharmony_ci int ret; 36058c2ecf20Sopenharmony_ci 36068c2ecf20Sopenharmony_ci ret = kstrtoul(page, 0, &val); 36078c2ecf20Sopenharmony_ci if (ret < 0) 36088c2ecf20Sopenharmony_ci return ret; 36098c2ecf20Sopenharmony_ci if (val != !!val) 36108c2ecf20Sopenharmony_ci return -EINVAL; 36118c2ecf20Sopenharmony_ci 36128c2ecf20Sopenharmony_ci ret = mutex_lock_interruptible(&sdev->sdev_mutex); 36138c2ecf20Sopenharmony_ci if (ret < 0) 36148c2ecf20Sopenharmony_ci return ret; 36158c2ecf20Sopenharmony_ci ret = mutex_lock_interruptible(&sport->mutex); 36168c2ecf20Sopenharmony_ci if (ret < 0) 36178c2ecf20Sopenharmony_ci goto unlock_sdev; 36188c2ecf20Sopenharmony_ci enabled = sport->enabled; 36198c2ecf20Sopenharmony_ci /* Log out all initiator systems before changing 'use_srq'. */ 36208c2ecf20Sopenharmony_ci srpt_set_enabled(sport, false); 36218c2ecf20Sopenharmony_ci sport->port_attrib.use_srq = val; 36228c2ecf20Sopenharmony_ci srpt_use_srq(sdev, sport->port_attrib.use_srq); 36238c2ecf20Sopenharmony_ci srpt_set_enabled(sport, enabled); 36248c2ecf20Sopenharmony_ci ret = count; 36258c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 36268c2ecf20Sopenharmony_ciunlock_sdev: 36278c2ecf20Sopenharmony_ci mutex_unlock(&sdev->sdev_mutex); 36288c2ecf20Sopenharmony_ci 36298c2ecf20Sopenharmony_ci return ret; 36308c2ecf20Sopenharmony_ci} 36318c2ecf20Sopenharmony_ci 36328c2ecf20Sopenharmony_ciCONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size); 36338c2ecf20Sopenharmony_ciCONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size); 36348c2ecf20Sopenharmony_ciCONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size); 36358c2ecf20Sopenharmony_ciCONFIGFS_ATTR(srpt_tpg_attrib_, use_srq); 36368c2ecf20Sopenharmony_ci 36378c2ecf20Sopenharmony_cistatic struct configfs_attribute *srpt_tpg_attrib_attrs[] = { 36388c2ecf20Sopenharmony_ci &srpt_tpg_attrib_attr_srp_max_rdma_size, 36398c2ecf20Sopenharmony_ci &srpt_tpg_attrib_attr_srp_max_rsp_size, 36408c2ecf20Sopenharmony_ci &srpt_tpg_attrib_attr_srp_sq_size, 36418c2ecf20Sopenharmony_ci &srpt_tpg_attrib_attr_use_srq, 36428c2ecf20Sopenharmony_ci NULL, 36438c2ecf20Sopenharmony_ci}; 36448c2ecf20Sopenharmony_ci 36458c2ecf20Sopenharmony_cistatic struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr) 36468c2ecf20Sopenharmony_ci{ 36478c2ecf20Sopenharmony_ci struct rdma_cm_id *rdma_cm_id; 36488c2ecf20Sopenharmony_ci int ret; 36498c2ecf20Sopenharmony_ci 36508c2ecf20Sopenharmony_ci rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler, 36518c2ecf20Sopenharmony_ci NULL, RDMA_PS_TCP, IB_QPT_RC); 36528c2ecf20Sopenharmony_ci if (IS_ERR(rdma_cm_id)) { 36538c2ecf20Sopenharmony_ci pr_err("RDMA/CM ID creation failed: %ld\n", 36548c2ecf20Sopenharmony_ci PTR_ERR(rdma_cm_id)); 36558c2ecf20Sopenharmony_ci goto out; 36568c2ecf20Sopenharmony_ci } 36578c2ecf20Sopenharmony_ci 36588c2ecf20Sopenharmony_ci ret = rdma_bind_addr(rdma_cm_id, listen_addr); 36598c2ecf20Sopenharmony_ci if (ret) { 36608c2ecf20Sopenharmony_ci char addr_str[64]; 36618c2ecf20Sopenharmony_ci 36628c2ecf20Sopenharmony_ci snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr); 36638c2ecf20Sopenharmony_ci pr_err("Binding RDMA/CM ID to address %s failed: %d\n", 36648c2ecf20Sopenharmony_ci addr_str, ret); 36658c2ecf20Sopenharmony_ci rdma_destroy_id(rdma_cm_id); 36668c2ecf20Sopenharmony_ci rdma_cm_id = ERR_PTR(ret); 36678c2ecf20Sopenharmony_ci goto out; 36688c2ecf20Sopenharmony_ci } 36698c2ecf20Sopenharmony_ci 36708c2ecf20Sopenharmony_ci ret = rdma_listen(rdma_cm_id, 128); 36718c2ecf20Sopenharmony_ci if (ret) { 36728c2ecf20Sopenharmony_ci pr_err("rdma_listen() failed: %d\n", ret); 36738c2ecf20Sopenharmony_ci rdma_destroy_id(rdma_cm_id); 36748c2ecf20Sopenharmony_ci rdma_cm_id = ERR_PTR(ret); 36758c2ecf20Sopenharmony_ci } 36768c2ecf20Sopenharmony_ci 36778c2ecf20Sopenharmony_ciout: 36788c2ecf20Sopenharmony_ci return rdma_cm_id; 36798c2ecf20Sopenharmony_ci} 36808c2ecf20Sopenharmony_ci 36818c2ecf20Sopenharmony_cistatic ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page) 36828c2ecf20Sopenharmony_ci{ 36838c2ecf20Sopenharmony_ci return sprintf(page, "%d\n", rdma_cm_port); 36848c2ecf20Sopenharmony_ci} 36858c2ecf20Sopenharmony_ci 36868c2ecf20Sopenharmony_cistatic ssize_t srpt_rdma_cm_port_store(struct config_item *item, 36878c2ecf20Sopenharmony_ci const char *page, size_t count) 36888c2ecf20Sopenharmony_ci{ 36898c2ecf20Sopenharmony_ci struct sockaddr_in addr4 = { .sin_family = AF_INET }; 36908c2ecf20Sopenharmony_ci struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 }; 36918c2ecf20Sopenharmony_ci struct rdma_cm_id *new_id = NULL; 36928c2ecf20Sopenharmony_ci u16 val; 36938c2ecf20Sopenharmony_ci int ret; 36948c2ecf20Sopenharmony_ci 36958c2ecf20Sopenharmony_ci ret = kstrtou16(page, 0, &val); 36968c2ecf20Sopenharmony_ci if (ret < 0) 36978c2ecf20Sopenharmony_ci return ret; 36988c2ecf20Sopenharmony_ci ret = count; 36998c2ecf20Sopenharmony_ci if (rdma_cm_port == val) 37008c2ecf20Sopenharmony_ci goto out; 37018c2ecf20Sopenharmony_ci 37028c2ecf20Sopenharmony_ci if (val) { 37038c2ecf20Sopenharmony_ci addr6.sin6_port = cpu_to_be16(val); 37048c2ecf20Sopenharmony_ci new_id = srpt_create_rdma_id((struct sockaddr *)&addr6); 37058c2ecf20Sopenharmony_ci if (IS_ERR(new_id)) { 37068c2ecf20Sopenharmony_ci addr4.sin_port = cpu_to_be16(val); 37078c2ecf20Sopenharmony_ci new_id = srpt_create_rdma_id((struct sockaddr *)&addr4); 37088c2ecf20Sopenharmony_ci if (IS_ERR(new_id)) { 37098c2ecf20Sopenharmony_ci ret = PTR_ERR(new_id); 37108c2ecf20Sopenharmony_ci goto out; 37118c2ecf20Sopenharmony_ci } 37128c2ecf20Sopenharmony_ci } 37138c2ecf20Sopenharmony_ci } 37148c2ecf20Sopenharmony_ci 37158c2ecf20Sopenharmony_ci mutex_lock(&rdma_cm_mutex); 37168c2ecf20Sopenharmony_ci rdma_cm_port = val; 37178c2ecf20Sopenharmony_ci swap(rdma_cm_id, new_id); 37188c2ecf20Sopenharmony_ci mutex_unlock(&rdma_cm_mutex); 37198c2ecf20Sopenharmony_ci 37208c2ecf20Sopenharmony_ci if (new_id) 37218c2ecf20Sopenharmony_ci rdma_destroy_id(new_id); 37228c2ecf20Sopenharmony_ci ret = count; 37238c2ecf20Sopenharmony_ciout: 37248c2ecf20Sopenharmony_ci return ret; 37258c2ecf20Sopenharmony_ci} 37268c2ecf20Sopenharmony_ci 37278c2ecf20Sopenharmony_ciCONFIGFS_ATTR(srpt_, rdma_cm_port); 37288c2ecf20Sopenharmony_ci 37298c2ecf20Sopenharmony_cistatic struct configfs_attribute *srpt_da_attrs[] = { 37308c2ecf20Sopenharmony_ci &srpt_attr_rdma_cm_port, 37318c2ecf20Sopenharmony_ci NULL, 37328c2ecf20Sopenharmony_ci}; 37338c2ecf20Sopenharmony_ci 37348c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_enable_show(struct config_item *item, char *page) 37358c2ecf20Sopenharmony_ci{ 37368c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = to_tpg(item); 37378c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 37388c2ecf20Sopenharmony_ci 37398c2ecf20Sopenharmony_ci return snprintf(page, PAGE_SIZE, "%d\n", sport->enabled); 37408c2ecf20Sopenharmony_ci} 37418c2ecf20Sopenharmony_ci 37428c2ecf20Sopenharmony_cistatic ssize_t srpt_tpg_enable_store(struct config_item *item, 37438c2ecf20Sopenharmony_ci const char *page, size_t count) 37448c2ecf20Sopenharmony_ci{ 37458c2ecf20Sopenharmony_ci struct se_portal_group *se_tpg = to_tpg(item); 37468c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(se_tpg); 37478c2ecf20Sopenharmony_ci unsigned long tmp; 37488c2ecf20Sopenharmony_ci int ret; 37498c2ecf20Sopenharmony_ci 37508c2ecf20Sopenharmony_ci ret = kstrtoul(page, 0, &tmp); 37518c2ecf20Sopenharmony_ci if (ret < 0) { 37528c2ecf20Sopenharmony_ci pr_err("Unable to extract srpt_tpg_store_enable\n"); 37538c2ecf20Sopenharmony_ci return -EINVAL; 37548c2ecf20Sopenharmony_ci } 37558c2ecf20Sopenharmony_ci 37568c2ecf20Sopenharmony_ci if ((tmp != 0) && (tmp != 1)) { 37578c2ecf20Sopenharmony_ci pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp); 37588c2ecf20Sopenharmony_ci return -EINVAL; 37598c2ecf20Sopenharmony_ci } 37608c2ecf20Sopenharmony_ci 37618c2ecf20Sopenharmony_ci mutex_lock(&sport->mutex); 37628c2ecf20Sopenharmony_ci srpt_set_enabled(sport, tmp); 37638c2ecf20Sopenharmony_ci mutex_unlock(&sport->mutex); 37648c2ecf20Sopenharmony_ci 37658c2ecf20Sopenharmony_ci return count; 37668c2ecf20Sopenharmony_ci} 37678c2ecf20Sopenharmony_ci 37688c2ecf20Sopenharmony_ciCONFIGFS_ATTR(srpt_tpg_, enable); 37698c2ecf20Sopenharmony_ci 37708c2ecf20Sopenharmony_cistatic struct configfs_attribute *srpt_tpg_attrs[] = { 37718c2ecf20Sopenharmony_ci &srpt_tpg_attr_enable, 37728c2ecf20Sopenharmony_ci NULL, 37738c2ecf20Sopenharmony_ci}; 37748c2ecf20Sopenharmony_ci 37758c2ecf20Sopenharmony_ci/** 37768c2ecf20Sopenharmony_ci * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg 37778c2ecf20Sopenharmony_ci * @wwn: Corresponds to $driver/$port. 37788c2ecf20Sopenharmony_ci * @name: $tpg. 37798c2ecf20Sopenharmony_ci */ 37808c2ecf20Sopenharmony_cistatic struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn, 37818c2ecf20Sopenharmony_ci const char *name) 37828c2ecf20Sopenharmony_ci{ 37838c2ecf20Sopenharmony_ci struct srpt_port_id *sport_id = srpt_wwn_to_sport_id(wwn); 37848c2ecf20Sopenharmony_ci struct srpt_tpg *stpg; 37858c2ecf20Sopenharmony_ci int res = -ENOMEM; 37868c2ecf20Sopenharmony_ci 37878c2ecf20Sopenharmony_ci stpg = kzalloc(sizeof(*stpg), GFP_KERNEL); 37888c2ecf20Sopenharmony_ci if (!stpg) 37898c2ecf20Sopenharmony_ci return ERR_PTR(res); 37908c2ecf20Sopenharmony_ci stpg->sport_id = sport_id; 37918c2ecf20Sopenharmony_ci res = core_tpg_register(wwn, &stpg->tpg, SCSI_PROTOCOL_SRP); 37928c2ecf20Sopenharmony_ci if (res) { 37938c2ecf20Sopenharmony_ci kfree(stpg); 37948c2ecf20Sopenharmony_ci return ERR_PTR(res); 37958c2ecf20Sopenharmony_ci } 37968c2ecf20Sopenharmony_ci 37978c2ecf20Sopenharmony_ci mutex_lock(&sport_id->mutex); 37988c2ecf20Sopenharmony_ci list_add_tail(&stpg->entry, &sport_id->tpg_list); 37998c2ecf20Sopenharmony_ci mutex_unlock(&sport_id->mutex); 38008c2ecf20Sopenharmony_ci 38018c2ecf20Sopenharmony_ci return &stpg->tpg; 38028c2ecf20Sopenharmony_ci} 38038c2ecf20Sopenharmony_ci 38048c2ecf20Sopenharmony_ci/** 38058c2ecf20Sopenharmony_ci * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg 38068c2ecf20Sopenharmony_ci * @tpg: Target portal group to deregister. 38078c2ecf20Sopenharmony_ci */ 38088c2ecf20Sopenharmony_cistatic void srpt_drop_tpg(struct se_portal_group *tpg) 38098c2ecf20Sopenharmony_ci{ 38108c2ecf20Sopenharmony_ci struct srpt_tpg *stpg = container_of(tpg, typeof(*stpg), tpg); 38118c2ecf20Sopenharmony_ci struct srpt_port_id *sport_id = stpg->sport_id; 38128c2ecf20Sopenharmony_ci struct srpt_port *sport = srpt_tpg_to_sport(tpg); 38138c2ecf20Sopenharmony_ci 38148c2ecf20Sopenharmony_ci mutex_lock(&sport_id->mutex); 38158c2ecf20Sopenharmony_ci list_del(&stpg->entry); 38168c2ecf20Sopenharmony_ci mutex_unlock(&sport_id->mutex); 38178c2ecf20Sopenharmony_ci 38188c2ecf20Sopenharmony_ci sport->enabled = false; 38198c2ecf20Sopenharmony_ci core_tpg_deregister(tpg); 38208c2ecf20Sopenharmony_ci kfree(stpg); 38218c2ecf20Sopenharmony_ci} 38228c2ecf20Sopenharmony_ci 38238c2ecf20Sopenharmony_ci/** 38248c2ecf20Sopenharmony_ci * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port 38258c2ecf20Sopenharmony_ci * @tf: Not used. 38268c2ecf20Sopenharmony_ci * @group: Not used. 38278c2ecf20Sopenharmony_ci * @name: $port. 38288c2ecf20Sopenharmony_ci */ 38298c2ecf20Sopenharmony_cistatic struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf, 38308c2ecf20Sopenharmony_ci struct config_group *group, 38318c2ecf20Sopenharmony_ci const char *name) 38328c2ecf20Sopenharmony_ci{ 38338c2ecf20Sopenharmony_ci struct port_and_port_id papi = srpt_lookup_port(name); 38348c2ecf20Sopenharmony_ci struct srpt_port *sport = papi.sport; 38358c2ecf20Sopenharmony_ci struct srpt_port_id *port_id; 38368c2ecf20Sopenharmony_ci 38378c2ecf20Sopenharmony_ci if (!papi.port_id) 38388c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 38398c2ecf20Sopenharmony_ci if (*papi.port_id) { 38408c2ecf20Sopenharmony_ci /* Attempt to create a directory that already exists. */ 38418c2ecf20Sopenharmony_ci WARN_ON_ONCE(true); 38428c2ecf20Sopenharmony_ci return &(*papi.port_id)->wwn; 38438c2ecf20Sopenharmony_ci } 38448c2ecf20Sopenharmony_ci port_id = kzalloc(sizeof(*port_id), GFP_KERNEL); 38458c2ecf20Sopenharmony_ci if (!port_id) { 38468c2ecf20Sopenharmony_ci srpt_sdev_put(sport->sdev); 38478c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 38488c2ecf20Sopenharmony_ci } 38498c2ecf20Sopenharmony_ci mutex_init(&port_id->mutex); 38508c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&port_id->tpg_list); 38518c2ecf20Sopenharmony_ci port_id->wwn.priv = sport; 38528c2ecf20Sopenharmony_ci memcpy(port_id->name, port_id == sport->guid_id ? sport->guid_name : 38538c2ecf20Sopenharmony_ci sport->gid_name, ARRAY_SIZE(port_id->name)); 38548c2ecf20Sopenharmony_ci 38558c2ecf20Sopenharmony_ci *papi.port_id = port_id; 38568c2ecf20Sopenharmony_ci 38578c2ecf20Sopenharmony_ci return &port_id->wwn; 38588c2ecf20Sopenharmony_ci} 38598c2ecf20Sopenharmony_ci 38608c2ecf20Sopenharmony_ci/** 38618c2ecf20Sopenharmony_ci * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port 38628c2ecf20Sopenharmony_ci * @wwn: $port. 38638c2ecf20Sopenharmony_ci */ 38648c2ecf20Sopenharmony_cistatic void srpt_drop_tport(struct se_wwn *wwn) 38658c2ecf20Sopenharmony_ci{ 38668c2ecf20Sopenharmony_ci struct srpt_port_id *port_id = container_of(wwn, typeof(*port_id), wwn); 38678c2ecf20Sopenharmony_ci struct srpt_port *sport = wwn->priv; 38688c2ecf20Sopenharmony_ci 38698c2ecf20Sopenharmony_ci if (sport->guid_id == port_id) 38708c2ecf20Sopenharmony_ci sport->guid_id = NULL; 38718c2ecf20Sopenharmony_ci else if (sport->gid_id == port_id) 38728c2ecf20Sopenharmony_ci sport->gid_id = NULL; 38738c2ecf20Sopenharmony_ci else 38748c2ecf20Sopenharmony_ci WARN_ON_ONCE(true); 38758c2ecf20Sopenharmony_ci 38768c2ecf20Sopenharmony_ci srpt_sdev_put(sport->sdev); 38778c2ecf20Sopenharmony_ci kfree(port_id); 38788c2ecf20Sopenharmony_ci} 38798c2ecf20Sopenharmony_ci 38808c2ecf20Sopenharmony_cistatic ssize_t srpt_wwn_version_show(struct config_item *item, char *buf) 38818c2ecf20Sopenharmony_ci{ 38828c2ecf20Sopenharmony_ci return scnprintf(buf, PAGE_SIZE, "\n"); 38838c2ecf20Sopenharmony_ci} 38848c2ecf20Sopenharmony_ci 38858c2ecf20Sopenharmony_ciCONFIGFS_ATTR_RO(srpt_wwn_, version); 38868c2ecf20Sopenharmony_ci 38878c2ecf20Sopenharmony_cistatic struct configfs_attribute *srpt_wwn_attrs[] = { 38888c2ecf20Sopenharmony_ci &srpt_wwn_attr_version, 38898c2ecf20Sopenharmony_ci NULL, 38908c2ecf20Sopenharmony_ci}; 38918c2ecf20Sopenharmony_ci 38928c2ecf20Sopenharmony_cistatic const struct target_core_fabric_ops srpt_template = { 38938c2ecf20Sopenharmony_ci .module = THIS_MODULE, 38948c2ecf20Sopenharmony_ci .fabric_name = "srpt", 38958c2ecf20Sopenharmony_ci .tpg_get_wwn = srpt_get_fabric_wwn, 38968c2ecf20Sopenharmony_ci .tpg_get_tag = srpt_get_tag, 38978c2ecf20Sopenharmony_ci .tpg_check_demo_mode = srpt_check_false, 38988c2ecf20Sopenharmony_ci .tpg_check_demo_mode_cache = srpt_check_true, 38998c2ecf20Sopenharmony_ci .tpg_check_demo_mode_write_protect = srpt_check_true, 39008c2ecf20Sopenharmony_ci .tpg_check_prod_mode_write_protect = srpt_check_false, 39018c2ecf20Sopenharmony_ci .tpg_get_inst_index = srpt_tpg_get_inst_index, 39028c2ecf20Sopenharmony_ci .release_cmd = srpt_release_cmd, 39038c2ecf20Sopenharmony_ci .check_stop_free = srpt_check_stop_free, 39048c2ecf20Sopenharmony_ci .close_session = srpt_close_session, 39058c2ecf20Sopenharmony_ci .sess_get_index = srpt_sess_get_index, 39068c2ecf20Sopenharmony_ci .sess_get_initiator_sid = NULL, 39078c2ecf20Sopenharmony_ci .write_pending = srpt_write_pending, 39088c2ecf20Sopenharmony_ci .set_default_node_attributes = srpt_set_default_node_attrs, 39098c2ecf20Sopenharmony_ci .get_cmd_state = srpt_get_tcm_cmd_state, 39108c2ecf20Sopenharmony_ci .queue_data_in = srpt_queue_data_in, 39118c2ecf20Sopenharmony_ci .queue_status = srpt_queue_status, 39128c2ecf20Sopenharmony_ci .queue_tm_rsp = srpt_queue_tm_rsp, 39138c2ecf20Sopenharmony_ci .aborted_task = srpt_aborted_task, 39148c2ecf20Sopenharmony_ci /* 39158c2ecf20Sopenharmony_ci * Setup function pointers for generic logic in 39168c2ecf20Sopenharmony_ci * target_core_fabric_configfs.c 39178c2ecf20Sopenharmony_ci */ 39188c2ecf20Sopenharmony_ci .fabric_make_wwn = srpt_make_tport, 39198c2ecf20Sopenharmony_ci .fabric_drop_wwn = srpt_drop_tport, 39208c2ecf20Sopenharmony_ci .fabric_make_tpg = srpt_make_tpg, 39218c2ecf20Sopenharmony_ci .fabric_drop_tpg = srpt_drop_tpg, 39228c2ecf20Sopenharmony_ci .fabric_init_nodeacl = srpt_init_nodeacl, 39238c2ecf20Sopenharmony_ci 39248c2ecf20Sopenharmony_ci .tfc_discovery_attrs = srpt_da_attrs, 39258c2ecf20Sopenharmony_ci .tfc_wwn_attrs = srpt_wwn_attrs, 39268c2ecf20Sopenharmony_ci .tfc_tpg_base_attrs = srpt_tpg_attrs, 39278c2ecf20Sopenharmony_ci .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs, 39288c2ecf20Sopenharmony_ci}; 39298c2ecf20Sopenharmony_ci 39308c2ecf20Sopenharmony_ci/** 39318c2ecf20Sopenharmony_ci * srpt_init_module - kernel module initialization 39328c2ecf20Sopenharmony_ci * 39338c2ecf20Sopenharmony_ci * Note: Since ib_register_client() registers callback functions, and since at 39348c2ecf20Sopenharmony_ci * least one of these callback functions (srpt_add_one()) calls target core 39358c2ecf20Sopenharmony_ci * functions, this driver must be registered with the target core before 39368c2ecf20Sopenharmony_ci * ib_register_client() is called. 39378c2ecf20Sopenharmony_ci */ 39388c2ecf20Sopenharmony_cistatic int __init srpt_init_module(void) 39398c2ecf20Sopenharmony_ci{ 39408c2ecf20Sopenharmony_ci int ret; 39418c2ecf20Sopenharmony_ci 39428c2ecf20Sopenharmony_ci ret = -EINVAL; 39438c2ecf20Sopenharmony_ci if (srp_max_req_size < MIN_MAX_REQ_SIZE) { 39448c2ecf20Sopenharmony_ci pr_err("invalid value %d for kernel module parameter srp_max_req_size -- must be at least %d.\n", 39458c2ecf20Sopenharmony_ci srp_max_req_size, MIN_MAX_REQ_SIZE); 39468c2ecf20Sopenharmony_ci goto out; 39478c2ecf20Sopenharmony_ci } 39488c2ecf20Sopenharmony_ci 39498c2ecf20Sopenharmony_ci if (srpt_srq_size < MIN_SRPT_SRQ_SIZE 39508c2ecf20Sopenharmony_ci || srpt_srq_size > MAX_SRPT_SRQ_SIZE) { 39518c2ecf20Sopenharmony_ci pr_err("invalid value %d for kernel module parameter srpt_srq_size -- must be in the range [%d..%d].\n", 39528c2ecf20Sopenharmony_ci srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE); 39538c2ecf20Sopenharmony_ci goto out; 39548c2ecf20Sopenharmony_ci } 39558c2ecf20Sopenharmony_ci 39568c2ecf20Sopenharmony_ci ret = target_register_template(&srpt_template); 39578c2ecf20Sopenharmony_ci if (ret) 39588c2ecf20Sopenharmony_ci goto out; 39598c2ecf20Sopenharmony_ci 39608c2ecf20Sopenharmony_ci ret = ib_register_client(&srpt_client); 39618c2ecf20Sopenharmony_ci if (ret) { 39628c2ecf20Sopenharmony_ci pr_err("couldn't register IB client\n"); 39638c2ecf20Sopenharmony_ci goto out_unregister_target; 39648c2ecf20Sopenharmony_ci } 39658c2ecf20Sopenharmony_ci 39668c2ecf20Sopenharmony_ci return 0; 39678c2ecf20Sopenharmony_ci 39688c2ecf20Sopenharmony_ciout_unregister_target: 39698c2ecf20Sopenharmony_ci target_unregister_template(&srpt_template); 39708c2ecf20Sopenharmony_ciout: 39718c2ecf20Sopenharmony_ci return ret; 39728c2ecf20Sopenharmony_ci} 39738c2ecf20Sopenharmony_ci 39748c2ecf20Sopenharmony_cistatic void __exit srpt_cleanup_module(void) 39758c2ecf20Sopenharmony_ci{ 39768c2ecf20Sopenharmony_ci if (rdma_cm_id) 39778c2ecf20Sopenharmony_ci rdma_destroy_id(rdma_cm_id); 39788c2ecf20Sopenharmony_ci ib_unregister_client(&srpt_client); 39798c2ecf20Sopenharmony_ci target_unregister_template(&srpt_template); 39808c2ecf20Sopenharmony_ci} 39818c2ecf20Sopenharmony_ci 39828c2ecf20Sopenharmony_cimodule_init(srpt_init_module); 39838c2ecf20Sopenharmony_cimodule_exit(srpt_cleanup_module); 3984