/*-1* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.02*3* Copyright (c) 2005 Voltaire Inc. All rights reserved.4* Copyright (c) 2005 Intel Corporation. All rights reserved.5*6* This software is available to you under a choice of one of two7* licenses. You may choose to be licensed under the terms of the GNU8* General Public License (GPL) Version 2, available from the file9* COPYING in the main directory of this source tree, or the10* OpenIB.org BSD license below:11*12* Redistribution and use in source and binary forms, with or13* without modification, are permitted provided that the following14* conditions are met:15*16* - Redistributions of source code must retain the above17* copyright notice, this list of conditions and the following18* disclaimer.19*20* - Redistributions in binary form must reproduce the above21* copyright notice, this list of conditions and the following22* disclaimer in the documentation and/or other materials23* provided with the distribution.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,26* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF27* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND28* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS29* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN30* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN31* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE32* SOFTWARE.33*/3435#if !defined(RDMA_CM_H)36#define RDMA_CM_H3738#include <linux/socket.h>39#include <linux/in6.h>40#include <rdma/ib_addr.h>41#include <rdma/ib_sa.h>4243/*44* Upon receiving a device removal event, users must destroy the associated45* RDMA identifier and release all resources allocated with the device.46*/47enum rdma_cm_event_type {48RDMA_CM_EVENT_ADDR_RESOLVED,49RDMA_CM_EVENT_ADDR_ERROR,50RDMA_CM_EVENT_ROUTE_RESOLVED,51RDMA_CM_EVENT_ROUTE_ERROR,52RDMA_CM_EVENT_CONNECT_REQUEST,53RDMA_CM_EVENT_CONNECT_RESPONSE,54RDMA_CM_EVENT_CONNECT_ERROR,55RDMA_CM_EVENT_UNREACHABLE,56RDMA_CM_EVENT_REJECTED,57RDMA_CM_EVENT_ESTABLISHED,58RDMA_CM_EVENT_DISCONNECTED,59RDMA_CM_EVENT_DEVICE_REMOVAL,60RDMA_CM_EVENT_MULTICAST_JOIN,61RDMA_CM_EVENT_MULTICAST_ERROR,62RDMA_CM_EVENT_ADDR_CHANGE,63RDMA_CM_EVENT_TIMEWAIT_EXIT64};6566const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event);6768enum rdma_port_space {69RDMA_PS_SDP = 0x0001,70RDMA_PS_IPOIB = 0x0002,71RDMA_PS_IB = 0x013F,72RDMA_PS_TCP = 0x0106,73RDMA_PS_UDP = 0x0111,74};7576#define RDMA_IB_IP_PS_MASK 0xFFFFFFFFFFFF0000ULL77#define RDMA_IB_IP_PS_TCP 0x0000000001060000ULL78#define RDMA_IB_IP_PS_UDP 0x0000000001110000ULL79#define RDMA_IB_IP_PS_IB 0x00000000013F0000ULL8081struct rdma_addr {82struct sockaddr_storage src_addr;83struct sockaddr_storage dst_addr;84struct rdma_dev_addr dev_addr;85};8687struct rdma_route {88struct rdma_addr addr;89struct ib_sa_path_rec *path_rec;90int num_paths;91};9293struct rdma_conn_param {94const void *private_data;95u8 private_data_len;96u8 responder_resources;97u8 initiator_depth;98u8 flow_control;99u8 retry_count; /* ignored when accepting */100u8 rnr_retry_count;101/* Fields below ignored if a QP is created on the rdma_cm_id. */102u8 srq;103u32 qp_num;104u32 qkey;105};106107struct rdma_ud_param {108const void *private_data;109u8 private_data_len;110struct ib_ah_attr ah_attr;111u32 qp_num;112u32 qkey;113};114115struct rdma_cm_event {116enum rdma_cm_event_type event;117int status;118union {119struct rdma_conn_param conn;120struct rdma_ud_param ud;121} param;122};123124enum rdma_cm_state {125RDMA_CM_IDLE,126RDMA_CM_ADDR_QUERY,127RDMA_CM_ADDR_RESOLVED,128RDMA_CM_ROUTE_QUERY,129RDMA_CM_ROUTE_RESOLVED,130RDMA_CM_CONNECT,131RDMA_CM_DISCONNECT,132RDMA_CM_ADDR_BOUND,133RDMA_CM_LISTEN,134RDMA_CM_DEVICE_REMOVAL,135RDMA_CM_DESTROYING136};137138struct rdma_cm_id;139140/**141* rdma_cm_event_handler - Callback used to report user events.142*143* Notes: Users may not call rdma_destroy_id from this callback to destroy144* the passed in id, or a corresponding listen id. Returning a145* non-zero value from the callback will destroy the passed in id.146*/147typedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id,148struct rdma_cm_event *event);149150struct rdma_cm_id {151struct ib_device *device;152void *context;153struct ib_qp *qp;154rdma_cm_event_handler event_handler;155struct rdma_route route;156enum rdma_port_space ps;157enum ib_qp_type qp_type;158u8 port_num;159};160161/**162* rdma_create_id - Create an RDMA identifier.163*164* @net: The network namespace in which to create the new id.165* @event_handler: User callback invoked to report events associated with the166* returned rdma_id.167* @context: User specified context associated with the id.168* @ps: RDMA port space.169* @qp_type: type of queue pair associated with the id.170*171* The id holds a reference on the network namespace until it is destroyed.172*/173struct rdma_cm_id *rdma_create_id(struct vnet *net,174rdma_cm_event_handler event_handler,175void *context, enum rdma_port_space ps,176enum ib_qp_type qp_type);177178/**179* rdma_destroy_id - Destroys an RDMA identifier.180*181* @id: RDMA identifier.182*183* Note: calling this function has the effect of canceling in-flight184* asynchronous operations associated with the id.185*/186void rdma_destroy_id(struct rdma_cm_id *id);187188/**189* rdma_bind_addr - Bind an RDMA identifier to a source address and190* associated RDMA device, if needed.191*192* @id: RDMA identifier.193* @addr: Local address information. Wildcard values are permitted.194*195* This associates a source address with the RDMA identifier before calling196* rdma_listen. If a specific local address is given, the RDMA identifier will197* be bound to a local RDMA device.198*/199int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr);200201/**202* rdma_resolve_addr - Resolve destination and optional source addresses203* from IP addresses to an RDMA address. If successful, the specified204* rdma_cm_id will be bound to a local device.205*206* @id: RDMA identifier.207* @src_addr: Source address information. This parameter may be NULL.208* @dst_addr: Destination address information.209* @timeout_ms: Time to wait for resolution to complete.210*/211int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,212struct sockaddr *dst_addr, int timeout_ms);213214/**215* rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier216* into route information needed to establish a connection.217*218* This is called on the client side of a connection.219* Users must have first called rdma_resolve_addr to resolve a dst_addr220* into an RDMA address before calling this routine.221*/222int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms);223224/**225* rdma_create_qp - Allocate a QP and associate it with the specified RDMA226* identifier.227*228* QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA229* through their states.230*/231int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,232struct ib_qp_init_attr *qp_init_attr);233234/**235* rdma_destroy_qp - Deallocate the QP associated with the specified RDMA236* identifier.237*238* Users must destroy any QP associated with an RDMA identifier before239* destroying the RDMA ID.240*/241void rdma_destroy_qp(struct rdma_cm_id *id);242243/**244* rdma_init_qp_attr - Initializes the QP attributes for use in transitioning245* to a specified QP state.246* @id: Communication identifier associated with the QP attributes to247* initialize.248* @qp_attr: On input, specifies the desired QP state. On output, the249* mandatory and desired optional attributes will be set in order to250* modify the QP to the specified state.251* @qp_attr_mask: The QP attribute mask that may be used to transition the252* QP to the specified state.253*254* Users must set the @qp_attr->qp_state to the desired QP state. This call255* will set all required attributes for the given transition, along with256* known optional attributes. Users may override the attributes returned from257* this call before calling ib_modify_qp.258*259* Users that wish to have their QP automatically transitioned through its260* states can associate a QP with the rdma_cm_id by calling rdma_create_qp().261*/262int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,263int *qp_attr_mask);264265/**266* rdma_connect - Initiate an active connection request.267* @id: Connection identifier to connect.268* @conn_param: Connection information used for connected QPs.269*270* Users must have resolved a route for the rdma_cm_id to connect with271* by having called rdma_resolve_route before calling this routine.272*273* This call will either connect to a remote QP or obtain remote QP274* information for unconnected rdma_cm_id's. The actual operation is275* based on the rdma_cm_id's port space.276*/277int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);278279/**280* rdma_listen - This function is called by the passive side to281* listen for incoming connection requests.282*283* Users must have bound the rdma_cm_id to a local address by calling284* rdma_bind_addr before calling this routine.285*/286int rdma_listen(struct rdma_cm_id *id, int backlog);287288/**289* rdma_accept - Called to accept a connection request or response.290* @id: Connection identifier associated with the request.291* @conn_param: Information needed to establish the connection. This must be292* provided if accepting a connection request. If accepting a connection293* response, this parameter must be NULL.294*295* Typically, this routine is only called by the listener to accept a connection296* request. It must also be called on the active side of a connection if the297* user is performing their own QP transitions.298*299* In the case of error, a reject message is sent to the remote side and the300* state of the qp associated with the id is modified to error, such that any301* previously posted receive buffers would be flushed.302*/303int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);304305/**306* rdma_notify - Notifies the RDMA CM of an asynchronous event that has307* occurred on the connection.308* @id: Connection identifier to transition to established.309* @event: Asynchronous event.310*311* This routine should be invoked by users to notify the CM of relevant312* communication events. Events that should be reported to the CM and313* when to report them are:314*315* IB_EVENT_COMM_EST - Used when a message is received on a connected316* QP before an RTU has been received.317*/318int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event);319320/**321* rdma_reject - Called to reject a connection request or response.322*/323int rdma_reject(struct rdma_cm_id *id, const void *private_data,324u8 private_data_len);325326/**327* rdma_disconnect - This function disconnects the associated QP and328* transitions it into the error state.329*/330int rdma_disconnect(struct rdma_cm_id *id);331332/**333* rdma_join_multicast - Join the multicast group specified by the given334* address.335* @id: Communication identifier associated with the request.336* @addr: Multicast address identifying the group to join.337* @join_state: Multicast JoinState bitmap requested by port.338* Bitmap is based on IB_SA_MCMEMBER_REC_JOIN_STATE bits.339* @context: User-defined context associated with the join request, returned340* to the user through the private_data pointer in multicast events.341*/342int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,343u8 join_state, void *context);344345/**346* rdma_leave_multicast - Leave the multicast group specified by the given347* address.348*/349void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);350351/**352* rdma_set_service_type - Set the type of service associated with a353* connection identifier.354* @id: Communication identifier to associated with service type.355* @tos: Type of service.356*357* The type of service is interpretted as a differentiated service358* field (RFC 2474). The service type should be specified before359* performing route resolution, as existing communication on the360* connection identifier may be unaffected. The type of service361* requested may not be supported by the network to all destinations.362*/363void rdma_set_service_type(struct rdma_cm_id *id, int tos);364365/**366* rdma_set_reuseaddr - Allow the reuse of local addresses when binding367* the rdma_cm_id.368* @id: Communication identifier to configure.369* @reuse: Value indicating if the bound address is reusable.370*371* Reuse must be set before an address is bound to the id.372*/373int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse);374375/**376* rdma_set_afonly - Specify that listens are restricted to the377* bound address family only.378* @id: Communication identifer to configure.379* @afonly: Value indicating if listens are restricted.380*381* Must be set before identifier is in the listening state.382*/383int rdma_set_afonly(struct rdma_cm_id *id, int afonly);384385int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout);386/**387* rdma_get_service_id - Return the IB service ID for a specified address.388* @id: Communication identifier associated with the address.389* @addr: Address for the service ID.390*/391__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr);392393/**394* rdma_reject_msg - return a pointer to a reject message string.395* @id: Communication identifier that received the REJECT event.396* @reason: Value returned in the REJECT event status field.397*/398const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,399int reason);400401#endif /* RDMA_CM_H */402403404