/* 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_EXIT36};3738const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event);3940#define RDMA_IB_IP_PS_MASK 0xFFFFFFFFFFFF0000ULL41#define RDMA_IB_IP_PS_TCP 0x0000000001060000ULL42#define RDMA_IB_IP_PS_UDP 0x0000000001110000ULL43#define RDMA_IB_IP_PS_IB 0x00000000013F0000ULL4445struct rdma_addr {46struct sockaddr_storage src_addr;47struct sockaddr_storage dst_addr;48struct rdma_dev_addr dev_addr;49};5051struct rdma_route {52struct rdma_addr addr;53struct sa_path_rec *path_rec;5455/* Optional path records of primary path */56struct sa_path_rec *path_rec_inbound;57struct sa_path_rec *path_rec_outbound;5859/*60* 0 - No primary nor alternate path is available61* 1 - Only primary path is available62* 2 - Both primary and alternate path are available63*/64int num_pri_alt_paths;65};6667struct rdma_conn_param {68const void *private_data;69u8 private_data_len;70u8 responder_resources;71u8 initiator_depth;72u8 flow_control;73u8 retry_count; /* ignored when accepting */74u8 rnr_retry_count;75/* Fields below ignored if a QP is created on the rdma_cm_id. */76u8 srq;77u32 qp_num;78u32 qkey;79};8081struct rdma_ud_param {82const void *private_data;83u8 private_data_len;84struct rdma_ah_attr ah_attr;85u32 qp_num;86u32 qkey;87};8889struct rdma_cm_event {90enum rdma_cm_event_type event;91int status;92union {93struct rdma_conn_param conn;94struct rdma_ud_param ud;95} param;96struct rdma_ucm_ece ece;97};9899struct rdma_cm_id;100101/**102* rdma_cm_event_handler - Callback used to report user events.103*104* Notes: Users may not call rdma_destroy_id from this callback to destroy105* the passed in id, or a corresponding listen id. Returning a106* non-zero value from the callback will destroy the passed in id.107*/108typedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id,109struct rdma_cm_event *event);110111struct rdma_cm_id {112struct ib_device *device;113void *context;114struct ib_qp *qp;115rdma_cm_event_handler event_handler;116struct rdma_route route;117enum rdma_ucm_port_space ps;118enum ib_qp_type qp_type;119u32 port_num;120struct work_struct net_work;121};122123struct rdma_cm_id *124__rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,125void *context, enum rdma_ucm_port_space ps,126enum ib_qp_type qp_type, const char *caller);127struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,128void *context,129enum rdma_ucm_port_space ps,130enum ib_qp_type qp_type);131132/**133* rdma_create_id - Create an RDMA identifier.134*135* @net: The network namespace in which to create the new id.136* @event_handler: User callback invoked to report events associated with the137* returned rdma_id.138* @context: User specified context associated with the id.139* @ps: RDMA port space.140* @qp_type: type of queue pair associated with the id.141*142* Returns a new rdma_cm_id. The id holds a reference on the network143* namespace until it is destroyed.144*145* The event handler callback serializes on the id's mutex and is146* allowed to sleep.147*/148#define rdma_create_id(net, event_handler, context, ps, qp_type) \149__rdma_create_kernel_id(net, event_handler, context, ps, qp_type, \150KBUILD_MODNAME)151152/**153* rdma_destroy_id - Destroys an RDMA identifier.154*155* @id: RDMA identifier.156*157* Note: calling this function has the effect of canceling in-flight158* asynchronous operations associated with the id.159*/160void rdma_destroy_id(struct rdma_cm_id *id);161162/**163* rdma_bind_addr - Bind an RDMA identifier to a source address and164* associated RDMA device, if needed.165*166* @id: RDMA identifier.167* @addr: Local address information. Wildcard values are permitted.168*169* This associates a source address with the RDMA identifier before calling170* rdma_listen. If a specific local address is given, the RDMA identifier will171* be bound to a local RDMA device.172*/173int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr);174175/**176* rdma_resolve_addr - Resolve destination and optional source addresses177* from IP addresses to an RDMA address. If successful, the specified178* rdma_cm_id will be bound to a local device.179*180* @id: RDMA identifier.181* @src_addr: Source address information. This parameter may be NULL.182* @dst_addr: Destination address information.183* @timeout_ms: Time to wait for resolution to complete.184*/185int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,186const struct sockaddr *dst_addr,187unsigned long timeout_ms);188189/**190* rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier191* into route information needed to establish a connection.192*193* This is called on the client side of a connection.194* Users must have first called rdma_resolve_addr to resolve a dst_addr195* into an RDMA address before calling this routine.196*/197int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms);198199/**200* rdma_create_qp - Allocate a QP and associate it with the specified RDMA201* identifier.202*203* QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA204* through their states.205*/206int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,207struct ib_qp_init_attr *qp_init_attr);208209/**210* rdma_destroy_qp - Deallocate the QP associated with the specified RDMA211* identifier.212*213* Users must destroy any QP associated with an RDMA identifier before214* destroying the RDMA ID.215*/216void rdma_destroy_qp(struct rdma_cm_id *id);217218/**219* rdma_init_qp_attr - Initializes the QP attributes for use in transitioning220* to a specified QP state.221* @id: Communication identifier associated with the QP attributes to222* initialize.223* @qp_attr: On input, specifies the desired QP state. On output, the224* mandatory and desired optional attributes will be set in order to225* modify the QP to the specified state.226* @qp_attr_mask: The QP attribute mask that may be used to transition the227* QP to the specified state.228*229* Users must set the @qp_attr->qp_state to the desired QP state. This call230* will set all required attributes for the given transition, along with231* known optional attributes. Users may override the attributes returned from232* this call before calling ib_modify_qp.233*234* Users that wish to have their QP automatically transitioned through its235* states can associate a QP with the rdma_cm_id by calling rdma_create_qp().236*/237int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,238int *qp_attr_mask);239240int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);241int rdma_connect_locked(struct rdma_cm_id *id,242struct rdma_conn_param *conn_param);243244int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,245struct rdma_ucm_ece *ece);246247/**248* rdma_listen - This function is called by the passive side to249* listen for incoming connection requests.250*251* Users must have bound the rdma_cm_id to a local address by calling252* rdma_bind_addr before calling this routine.253*/254int rdma_listen(struct rdma_cm_id *id, int backlog);255256int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);257258void rdma_lock_handler(struct rdma_cm_id *id);259void rdma_unlock_handler(struct rdma_cm_id *id);260int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,261struct rdma_ucm_ece *ece);262263/**264* rdma_notify - Notifies the RDMA CM of an asynchronous event that has265* occurred on the connection.266* @id: Connection identifier to transition to established.267* @event: Asynchronous event.268*269* This routine should be invoked by users to notify the CM of relevant270* communication events. Events that should be reported to the CM and271* when to report them are:272*273* IB_EVENT_COMM_EST - Used when a message is received on a connected274* QP before an RTU has been received.275*/276int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event);277278/**279* rdma_reject - Called to reject a connection request or response.280*/281int rdma_reject(struct rdma_cm_id *id, const void *private_data,282u8 private_data_len, u8 reason);283284/**285* rdma_disconnect - This function disconnects the associated QP and286* transitions it into the error state.287*/288int rdma_disconnect(struct rdma_cm_id *id);289290/**291* rdma_join_multicast - Join the multicast group specified by the given292* address.293* @id: Communication identifier associated with the request.294* @addr: Multicast address identifying the group to join.295* @join_state: Multicast JoinState bitmap requested by port.296* Bitmap is based on IB_SA_MCMEMBER_REC_JOIN_STATE bits.297* @context: User-defined context associated with the join request, returned298* to the user through the private_data pointer in multicast events.299*/300int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,301u8 join_state, void *context);302303/**304* rdma_leave_multicast - Leave the multicast group specified by the given305* address.306*/307void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);308309/**310* rdma_set_service_type - Set the type of service associated with a311* connection identifier.312* @id: Communication identifier to associated with service type.313* @tos: Type of service.314*315* The type of service is interpretted as a differentiated service316* field (RFC 2474). The service type should be specified before317* performing route resolution, as existing communication on the318* connection identifier may be unaffected. The type of service319* requested may not be supported by the network to all destinations.320*/321void rdma_set_service_type(struct rdma_cm_id *id, int tos);322323/**324* rdma_set_reuseaddr - Allow the reuse of local addresses when binding325* the rdma_cm_id.326* @id: Communication identifier to configure.327* @reuse: Value indicating if the bound address is reusable.328*329* Reuse must be set before an address is bound to the id.330*/331int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse);332333/**334* rdma_set_afonly - Specify that listens are restricted to the335* bound address family only.336* @id: Communication identifer to configure.337* @afonly: Value indicating if listens are restricted.338*339* Must be set before identifier is in the listening state.340*/341int rdma_set_afonly(struct rdma_cm_id *id, int afonly);342343int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout);344345int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer);346/**347* rdma_get_service_id - Return the IB service ID for a specified address.348* @id: Communication identifier associated with the address.349* @addr: Address for the service ID.350*/351__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr);352353/**354* rdma_reject_msg - return a pointer to a reject message string.355* @id: Communication identifier that received the REJECT event.356* @reason: Value returned in the REJECT event status field.357*/358const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,359int reason);360/**361* rdma_consumer_reject_data - return the consumer reject private data and362* length, if any.363* @id: Communication identifier that received the REJECT event.364* @ev: RDMA CM reject event.365* @data_len: Pointer to the resulting length of the consumer data.366*/367const void *rdma_consumer_reject_data(struct rdma_cm_id *id,368struct rdma_cm_event *ev, u8 *data_len);369370/**371* rdma_read_gids - Return the SGID and DGID used for establishing372* connection. This can be used after rdma_resolve_addr()373* on client side. This can be use on new connection374* on server side. This is applicable to IB, RoCE, iWarp.375* If cm_id is not bound yet to the RDMA device, it doesn't376* copy and SGID or DGID to the given pointers.377* @id: Communication identifier whose GIDs are queried.378* @sgid: Pointer to SGID where SGID will be returned. It is optional.379* @dgid: Pointer to DGID where DGID will be returned. It is optional.380* Note: This API should not be used by any new ULPs or new code.381* Instead, users interested in querying GIDs should refer to path record382* of the rdma_cm_id to query the GIDs.383* This API is provided for compatibility for existing users.384*/385386void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,387union ib_gid *dgid);388389struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *cm_id);390391#endif /* RDMA_CM_H */392393394