/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */1/*2* Copyright (c) 2005 Voltaire Inc. All rights reserved.3* Copyright (c) 2005 Intel Corporation. All rights reserved.4*/56#ifndef RDMA_CM_H7#define RDMA_CM_H89#include <linux/socket.h>10#include <linux/in6.h>11#include <rdma/ib_addr.h>12#include <rdma/ib_sa.h>13#include <uapi/rdma/rdma_user_cm.h>1415/*16* Upon receiving a device removal event, users must destroy the associated17* RDMA identifier and release all resources allocated with the device.18*/19enum rdma_cm_event_type {20RDMA_CM_EVENT_ADDR_RESOLVED,21RDMA_CM_EVENT_ADDR_ERROR,22RDMA_CM_EVENT_ROUTE_RESOLVED,23RDMA_CM_EVENT_ROUTE_ERROR,24RDMA_CM_EVENT_CONNECT_REQUEST,25RDMA_CM_EVENT_CONNECT_RESPONSE,26RDMA_CM_EVENT_CONNECT_ERROR,27RDMA_CM_EVENT_UNREACHABLE,28RDMA_CM_EVENT_REJECTED,29RDMA_CM_EVENT_ESTABLISHED,30RDMA_CM_EVENT_DISCONNECTED,31RDMA_CM_EVENT_DEVICE_REMOVAL,32RDMA_CM_EVENT_MULTICAST_JOIN,33RDMA_CM_EVENT_MULTICAST_ERROR,34RDMA_CM_EVENT_ADDR_CHANGE,35RDMA_CM_EVENT_TIMEWAIT_EXIT,36RDMA_CM_EVENT_ADDRINFO_RESOLVED,37RDMA_CM_EVENT_ADDRINFO_ERROR,38RDMA_CM_EVENT_USER,39RDMA_CM_EVENT_INTERNAL,40};4142const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event);4344#define RDMA_IB_IP_PS_MASK 0xFFFFFFFFFFFF0000ULL45#define RDMA_IB_IP_PS_TCP 0x0000000001060000ULL46#define RDMA_IB_IP_PS_UDP 0x0000000001110000ULL47#define RDMA_IB_IP_PS_IB 0x00000000013F0000ULL4849struct rdma_addr {50struct sockaddr_storage src_addr;51struct sockaddr_storage dst_addr;52struct rdma_dev_addr dev_addr;53};5455struct rdma_route {56struct rdma_addr addr;57struct sa_path_rec *path_rec;5859/* Optional path records of primary path */60struct sa_path_rec *path_rec_inbound;61struct sa_path_rec *path_rec_outbound;6263/*64* 0 - No primary nor alternate path is available65* 1 - Only primary path is available66* 2 - Both primary and alternate path are available67*/68int num_pri_alt_paths;6970unsigned int num_service_recs;71struct sa_service_rec *service_recs;72};7374struct rdma_conn_param {75const void *private_data;76u8 private_data_len;77u8 responder_resources;78u8 initiator_depth;79u8 flow_control;80u8 retry_count; /* ignored when accepting */81u8 rnr_retry_count;82/* Fields below ignored if a QP is created on the rdma_cm_id. */83u8 srq;84u32 qp_num;85u32 qkey;86};8788struct rdma_ud_param {89const void *private_data;90u8 private_data_len;91struct rdma_ah_attr ah_attr;92u32 qp_num;93u32 qkey;94};9596struct rdma_cm_event {97enum rdma_cm_event_type event;98int status;99union {100struct rdma_conn_param conn;101struct rdma_ud_param ud;102u64 arg;103} param;104struct rdma_ucm_ece ece;105};106107struct rdma_cm_id;108109/**110* rdma_cm_event_handler - Callback used to report user events.111*112* Notes: Users may not call rdma_destroy_id from this callback to destroy113* the passed in id, or a corresponding listen id. Returning a114* non-zero value from the callback will destroy the passed in id.115*/116typedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id,117struct rdma_cm_event *event);118119struct rdma_cm_id {120struct ib_device *device;121void *context;122struct ib_qp *qp;123rdma_cm_event_handler event_handler;124struct rdma_route route;125enum rdma_ucm_port_space ps;126enum ib_qp_type qp_type;127u32 port_num;128struct work_struct net_work;129};130131struct rdma_cm_id *132__rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,133void *context, enum rdma_ucm_port_space ps,134enum ib_qp_type qp_type, const char *caller);135struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,136void *context,137enum rdma_ucm_port_space ps,138enum ib_qp_type qp_type);139140/**141* rdma_create_id - Create an RDMA identifier.142*143* @net: The network namespace in which to create the new id.144* @event_handler: User callback invoked to report events associated with the145* returned rdma_id.146* @context: User specified context associated with the id.147* @ps: RDMA port space.148* @qp_type: type of queue pair associated with the id.149*150* Returns a new rdma_cm_id. The id holds a reference on the network151* namespace until it is destroyed.152*153* The event handler callback serializes on the id's mutex and is154* allowed to sleep.155*/156#define rdma_create_id(net, event_handler, context, ps, qp_type) \157__rdma_create_kernel_id(net, event_handler, context, ps, qp_type, \158KBUILD_MODNAME)159160/**161* rdma_destroy_id - Destroys an RDMA identifier.162*163* @id: RDMA identifier.164*165* Note: calling this function has the effect of canceling in-flight166* asynchronous operations associated with the id.167*/168void rdma_destroy_id(struct rdma_cm_id *id);169170/**171* rdma_bind_addr - Bind an RDMA identifier to a source address and172* associated RDMA device, if needed.173*174* @id: RDMA identifier.175* @addr: Local address information. Wildcard values are permitted.176*177* This associates a source address with the RDMA identifier before calling178* rdma_listen. If a specific local address is given, the RDMA identifier will179* be bound to a local RDMA device.180*/181int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr);182183/**184* rdma_resolve_addr - Resolve destination and optional source addresses185* from IP addresses to an RDMA address. If successful, the specified186* rdma_cm_id will be bound to a local device.187*188* @id: RDMA identifier.189* @src_addr: Source address information. This parameter may be NULL.190* @dst_addr: Destination address information.191* @timeout_ms: Time to wait for resolution to complete.192*/193int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,194const struct sockaddr *dst_addr,195unsigned long timeout_ms);196197/**198* rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier199* into route information needed to establish a connection.200*201* This is called on the client side of a connection.202* Users must have first called rdma_resolve_addr to resolve a dst_addr203* into an RDMA address before calling this routine.204*/205int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms);206207/**208* rdma_resolve_ib_service - Resolve the IB service record of the209* service with the given service ID or name.210*211* This function is optional in the rdma cm flow. It is called on the client212* side of a connection, before calling rdma_resolve_route. The resolution213* can be done once per rdma_cm_id.214*/215int rdma_resolve_ib_service(struct rdma_cm_id *id,216struct rdma_ucm_ib_service *ibs);217218/**219* rdma_create_qp - Allocate a QP and associate it with the specified RDMA220* identifier.221*222* QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA223* through their states.224*/225int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,226struct ib_qp_init_attr *qp_init_attr);227228/**229* rdma_destroy_qp - Deallocate the QP associated with the specified RDMA230* identifier.231*232* Users must destroy any QP associated with an RDMA identifier before233* destroying the RDMA ID.234*/235void rdma_destroy_qp(struct rdma_cm_id *id);236237/**238* rdma_init_qp_attr - Initializes the QP attributes for use in transitioning239* to a specified QP state.240* @id: Communication identifier associated with the QP attributes to241* initialize.242* @qp_attr: On input, specifies the desired QP state. On output, the243* mandatory and desired optional attributes will be set in order to244* modify the QP to the specified state.245* @qp_attr_mask: The QP attribute mask that may be used to transition the246* QP to the specified state.247*248* Users must set the @qp_attr->qp_state to the desired QP state. This call249* will set all required attributes for the given transition, along with250* known optional attributes. Users may override the attributes returned from251* this call before calling ib_modify_qp.252*253* Users that wish to have their QP automatically transitioned through its254* states can associate a QP with the rdma_cm_id by calling rdma_create_qp().255*/256int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,257int *qp_attr_mask);258259int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);260int rdma_connect_locked(struct rdma_cm_id *id,261struct rdma_conn_param *conn_param);262263int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,264struct rdma_ucm_ece *ece);265266/**267* rdma_listen - This function is called by the passive side to268* listen for incoming connection requests.269*270* Users must have bound the rdma_cm_id to a local address by calling271* rdma_bind_addr before calling this routine.272*/273int rdma_listen(struct rdma_cm_id *id, int backlog);274275int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);276277void rdma_lock_handler(struct rdma_cm_id *id);278void rdma_unlock_handler(struct rdma_cm_id *id);279int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,280struct rdma_ucm_ece *ece);281282/**283* rdma_notify - Notifies the RDMA CM of an asynchronous event that has284* occurred on the connection.285* @id: Connection identifier to transition to established.286* @event: Asynchronous event.287*288* This routine should be invoked by users to notify the CM of relevant289* communication events. Events that should be reported to the CM and290* when to report them are:291*292* IB_EVENT_COMM_EST - Used when a message is received on a connected293* QP before an RTU has been received.294*/295int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event);296297/**298* rdma_reject - Called to reject a connection request or response.299*/300int rdma_reject(struct rdma_cm_id *id, const void *private_data,301u8 private_data_len, u8 reason);302303/**304* rdma_disconnect - This function disconnects the associated QP and305* transitions it into the error state.306*/307int rdma_disconnect(struct rdma_cm_id *id);308309/**310* rdma_join_multicast - Join the multicast group specified by the given311* address.312* @id: Communication identifier associated with the request.313* @addr: Multicast address identifying the group to join.314* @join_state: Multicast JoinState bitmap requested by port.315* Bitmap is based on IB_SA_MCMEMBER_REC_JOIN_STATE bits.316* @context: User-defined context associated with the join request, returned317* to the user through the private_data pointer in multicast events.318*/319int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,320u8 join_state, void *context);321322/**323* rdma_leave_multicast - Leave the multicast group specified by the given324* address.325*/326void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);327328/**329* rdma_set_service_type - Set the type of service associated with a330* connection identifier.331* @id: Communication identifier to associated with service type.332* @tos: Type of service.333*334* The type of service is interpretted as a differentiated service335* field (RFC 2474). The service type should be specified before336* performing route resolution, as existing communication on the337* connection identifier may be unaffected. The type of service338* requested may not be supported by the network to all destinations.339*/340void rdma_set_service_type(struct rdma_cm_id *id, int tos);341342/**343* rdma_set_reuseaddr - Allow the reuse of local addresses when binding344* the rdma_cm_id.345* @id: Communication identifier to configure.346* @reuse: Value indicating if the bound address is reusable.347*348* Reuse must be set before an address is bound to the id.349*/350int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse);351352/**353* rdma_set_afonly - Specify that listens are restricted to the354* bound address family only.355* @id: Communication identifer to configure.356* @afonly: Value indicating if listens are restricted.357*358* Must be set before identifier is in the listening state.359*/360int rdma_set_afonly(struct rdma_cm_id *id, int afonly);361362int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout);363364int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer);365/**366* rdma_get_service_id - Return the IB service ID for a specified address.367* @id: Communication identifier associated with the address.368* @addr: Address for the service ID.369*/370__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr);371372/**373* rdma_reject_msg - return a pointer to a reject message string.374* @id: Communication identifier that received the REJECT event.375* @reason: Value returned in the REJECT event status field.376*/377const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,378int reason);379/**380* rdma_consumer_reject_data - return the consumer reject private data and381* length, if any.382* @id: Communication identifier that received the REJECT event.383* @ev: RDMA CM reject event.384* @data_len: Pointer to the resulting length of the consumer data.385*/386const void *rdma_consumer_reject_data(struct rdma_cm_id *id,387struct rdma_cm_event *ev, u8 *data_len);388389/**390* rdma_read_gids - Return the SGID and DGID used for establishing391* connection. This can be used after rdma_resolve_addr()392* on client side. This can be use on new connection393* on server side. This is applicable to IB, RoCE, iWarp.394* If cm_id is not bound yet to the RDMA device, it doesn't395* copy and SGID or DGID to the given pointers.396* @id: Communication identifier whose GIDs are queried.397* @sgid: Pointer to SGID where SGID will be returned. It is optional.398* @dgid: Pointer to DGID where DGID will be returned. It is optional.399* Note: This API should not be used by any new ULPs or new code.400* Instead, users interested in querying GIDs should refer to path record401* of the rdma_cm_id to query the GIDs.402* This API is provided for compatibility for existing users.403*/404405void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,406union ib_gid *dgid);407408struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *cm_id);409410#endif /* RDMA_CM_H */411412413