1219820Sjeff/* 2219820Sjeff * Copyright (c) 2005 Voltaire Inc. All rights reserved. 3219820Sjeff * Copyright (c) 2005 Intel Corporation. All rights reserved. 4219820Sjeff * 5219820Sjeff * This software is available to you under a choice of one of two 6219820Sjeff * licenses. You may choose to be licensed under the terms of the GNU 7219820Sjeff * General Public License (GPL) Version 2, available from the file 8219820Sjeff * COPYING in the main directory of this source tree, or the 9219820Sjeff * OpenIB.org BSD license below: 10219820Sjeff * 11219820Sjeff * Redistribution and use in source and binary forms, with or 12219820Sjeff * without modification, are permitted provided that the following 13219820Sjeff * conditions are met: 14219820Sjeff * 15219820Sjeff * - Redistributions of source code must retain the above 16219820Sjeff * copyright notice, this list of conditions and the following 17219820Sjeff * disclaimer. 18219820Sjeff * 19219820Sjeff * - Redistributions in binary form must reproduce the above 20219820Sjeff * copyright notice, this list of conditions and the following 21219820Sjeff * disclaimer in the documentation and/or other materials 22219820Sjeff * provided with the distribution. 23219820Sjeff * 24219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31219820Sjeff * SOFTWARE. 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#if !defined(RDMA_CM_H) 35219820Sjeff#define RDMA_CM_H 36219820Sjeff 37219820Sjeff#include <linux/socket.h> 38219820Sjeff#include <linux/in6.h> 39219820Sjeff#include <rdma/ib_addr.h> 40219820Sjeff#include <rdma/ib_sa.h> 41219820Sjeff 42219820Sjeff/* 43219820Sjeff * Upon receiving a device removal event, users must destroy the associated 44219820Sjeff * RDMA identifier and release all resources allocated with the device. 45219820Sjeff */ 46219820Sjeffenum rdma_cm_event_type { 47219820Sjeff RDMA_CM_EVENT_ADDR_RESOLVED, 48219820Sjeff RDMA_CM_EVENT_ADDR_ERROR, 49219820Sjeff RDMA_CM_EVENT_ROUTE_RESOLVED, 50219820Sjeff RDMA_CM_EVENT_ROUTE_ERROR, 51219820Sjeff RDMA_CM_EVENT_CONNECT_REQUEST, 52219820Sjeff RDMA_CM_EVENT_CONNECT_RESPONSE, 53219820Sjeff RDMA_CM_EVENT_CONNECT_ERROR, 54219820Sjeff RDMA_CM_EVENT_UNREACHABLE, 55219820Sjeff RDMA_CM_EVENT_REJECTED, 56219820Sjeff RDMA_CM_EVENT_ESTABLISHED, 57219820Sjeff RDMA_CM_EVENT_DISCONNECTED, 58219820Sjeff RDMA_CM_EVENT_DEVICE_REMOVAL, 59219820Sjeff RDMA_CM_EVENT_MULTICAST_JOIN, 60219820Sjeff RDMA_CM_EVENT_MULTICAST_ERROR, 61219820Sjeff RDMA_CM_EVENT_ADDR_CHANGE, 62219820Sjeff RDMA_CM_EVENT_TIMEWAIT_EXIT 63219820Sjeff}; 64219820Sjeff 65219820Sjeffenum rdma_port_space { 66219820Sjeff RDMA_PS_SDP = 0x0001, 67219820Sjeff RDMA_PS_IPOIB = 0x0002, 68219820Sjeff RDMA_PS_TCP = 0x0106, 69219820Sjeff RDMA_PS_UDP = 0x0111, 70219820Sjeff RDMA_PS_SCTP = 0x0183 71219820Sjeff}; 72219820Sjeff 73219820Sjeffstruct rdma_addr { 74219820Sjeff struct sockaddr_storage src_addr; 75219820Sjeff struct sockaddr_storage dst_addr; 76219820Sjeff struct rdma_dev_addr dev_addr; 77219820Sjeff}; 78219820Sjeff 79219820Sjeffstruct rdma_route { 80219820Sjeff struct rdma_addr addr; 81219820Sjeff struct ib_sa_path_rec *path_rec; 82219820Sjeff int num_paths; 83219820Sjeff}; 84219820Sjeff 85219820Sjeffstruct rdma_conn_param { 86219820Sjeff const void *private_data; 87219820Sjeff u8 private_data_len; 88219820Sjeff u8 responder_resources; 89219820Sjeff u8 initiator_depth; 90219820Sjeff u8 flow_control; 91219820Sjeff u8 retry_count; /* ignored when accepting */ 92219820Sjeff u8 rnr_retry_count; 93219820Sjeff /* Fields below ignored if a QP is created on the rdma_cm_id. */ 94219820Sjeff u8 srq; 95219820Sjeff u32 qp_num; 96219820Sjeff}; 97219820Sjeff 98219820Sjeffstruct rdma_ud_param { 99219820Sjeff const void *private_data; 100219820Sjeff u8 private_data_len; 101219820Sjeff struct ib_ah_attr ah_attr; 102219820Sjeff u32 qp_num; 103219820Sjeff u32 qkey; 104219820Sjeff}; 105219820Sjeff 106219820Sjeffstruct rdma_cm_event { 107219820Sjeff enum rdma_cm_event_type event; 108219820Sjeff int status; 109219820Sjeff union { 110219820Sjeff struct rdma_conn_param conn; 111219820Sjeff struct rdma_ud_param ud; 112219820Sjeff } param; 113219820Sjeff}; 114219820Sjeff 115219820Sjeffstruct rdma_cm_id; 116219820Sjeff 117219820Sjeff/** 118219820Sjeff * rdma_cm_event_handler - Callback used to report user events. 119219820Sjeff * 120219820Sjeff * Notes: Users may not call rdma_destroy_id from this callback to destroy 121219820Sjeff * the passed in id, or a corresponding listen id. Returning a 122219820Sjeff * non-zero value from the callback will destroy the passed in id. 123219820Sjeff */ 124219820Sjefftypedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id, 125219820Sjeff struct rdma_cm_event *event); 126219820Sjeff 127219820Sjeffstruct rdma_cm_id { 128219820Sjeff struct ib_device *device; 129219820Sjeff void *context; 130219820Sjeff struct ib_qp *qp; 131219820Sjeff rdma_cm_event_handler event_handler; 132219820Sjeff struct rdma_route route; 133219820Sjeff enum rdma_port_space ps; 134219820Sjeff u8 port_num; 135219820Sjeff}; 136219820Sjeff 137219820Sjeff/** 138219820Sjeff * rdma_create_id - Create an RDMA identifier. 139219820Sjeff * 140219820Sjeff * @event_handler: User callback invoked to report events associated with the 141219820Sjeff * returned rdma_id. 142219820Sjeff * @context: User specified context associated with the id. 143219820Sjeff * @ps: RDMA port space. 144219820Sjeff */ 145219820Sjeffstruct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler, 146219820Sjeff void *context, enum rdma_port_space ps); 147219820Sjeff 148219820Sjeff/** 149219820Sjeff * rdma_destroy_id - Destroys an RDMA identifier. 150219820Sjeff * 151219820Sjeff * @id: RDMA identifier. 152219820Sjeff * 153219820Sjeff * Note: calling this function has the effect of canceling in-flight 154219820Sjeff * asynchronous operations associated with the id. 155219820Sjeff */ 156219820Sjeffvoid rdma_destroy_id(struct rdma_cm_id *id); 157219820Sjeff 158219820Sjeff/** 159219820Sjeff * rdma_bind_addr - Bind an RDMA identifier to a source address and 160219820Sjeff * associated RDMA device, if needed. 161219820Sjeff * 162219820Sjeff * @id: RDMA identifier. 163219820Sjeff * @addr: Local address information. Wildcard values are permitted. 164219820Sjeff * 165219820Sjeff * This associates a source address with the RDMA identifier before calling 166219820Sjeff * rdma_listen. If a specific local address is given, the RDMA identifier will 167219820Sjeff * be bound to a local RDMA device. 168219820Sjeff */ 169219820Sjeffint rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr); 170219820Sjeff 171219820Sjeff/** 172219820Sjeff * rdma_resolve_addr - Resolve destination and optional source addresses 173219820Sjeff * from IP addresses to an RDMA address. If successful, the specified 174219820Sjeff * rdma_cm_id will be bound to a local device. 175219820Sjeff * 176219820Sjeff * @id: RDMA identifier. 177219820Sjeff * @src_addr: Source address information. This parameter may be NULL. 178219820Sjeff * @dst_addr: Destination address information. 179219820Sjeff * @timeout_ms: Time to wait for resolution to complete. 180219820Sjeff */ 181219820Sjeffint rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 182219820Sjeff struct sockaddr *dst_addr, int timeout_ms); 183219820Sjeff 184219820Sjeff/** 185219820Sjeff * rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier 186219820Sjeff * into route information needed to establish a connection. 187219820Sjeff * 188219820Sjeff * This is called on the client side of a connection. 189219820Sjeff * Users must have first called rdma_resolve_addr to resolve a dst_addr 190219820Sjeff * into an RDMA address before calling this routine. 191219820Sjeff */ 192219820Sjeffint rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms); 193219820Sjeff 194219820Sjeff/** 195219820Sjeff * rdma_create_qp - Allocate a QP and associate it with the specified RDMA 196219820Sjeff * identifier. 197219820Sjeff * 198219820Sjeff * QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA 199219820Sjeff * through their states. 200219820Sjeff */ 201219820Sjeffint rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, 202219820Sjeff struct ib_qp_init_attr *qp_init_attr); 203219820Sjeff 204219820Sjeff/** 205219820Sjeff * rdma_destroy_qp - Deallocate the QP associated with the specified RDMA 206219820Sjeff * identifier. 207219820Sjeff * 208219820Sjeff * Users must destroy any QP associated with an RDMA identifier before 209219820Sjeff * destroying the RDMA ID. 210219820Sjeff */ 211219820Sjeffvoid rdma_destroy_qp(struct rdma_cm_id *id); 212219820Sjeff 213219820Sjeff/** 214219820Sjeff * rdma_init_qp_attr - Initializes the QP attributes for use in transitioning 215219820Sjeff * to a specified QP state. 216219820Sjeff * @id: Communication identifier associated with the QP attributes to 217219820Sjeff * initialize. 218219820Sjeff * @qp_attr: On input, specifies the desired QP state. On output, the 219219820Sjeff * mandatory and desired optional attributes will be set in order to 220219820Sjeff * modify the QP to the specified state. 221219820Sjeff * @qp_attr_mask: The QP attribute mask that may be used to transition the 222219820Sjeff * QP to the specified state. 223219820Sjeff * 224219820Sjeff * Users must set the @qp_attr->qp_state to the desired QP state. This call 225219820Sjeff * will set all required attributes for the given transition, along with 226219820Sjeff * known optional attributes. Users may override the attributes returned from 227219820Sjeff * this call before calling ib_modify_qp. 228219820Sjeff * 229219820Sjeff * Users that wish to have their QP automatically transitioned through its 230219820Sjeff * states can associate a QP with the rdma_cm_id by calling rdma_create_qp(). 231219820Sjeff */ 232219820Sjeffint rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, 233219820Sjeff int *qp_attr_mask); 234219820Sjeff 235219820Sjeff/** 236219820Sjeff * rdma_connect - Initiate an active connection request. 237219820Sjeff * @id: Connection identifier to connect. 238219820Sjeff * @conn_param: Connection information used for connected QPs. 239219820Sjeff * 240219820Sjeff * Users must have resolved a route for the rdma_cm_id to connect with 241219820Sjeff * by having called rdma_resolve_route before calling this routine. 242219820Sjeff * 243219820Sjeff * This call will either connect to a remote QP or obtain remote QP 244219820Sjeff * information for unconnected rdma_cm_id's. The actual operation is 245219820Sjeff * based on the rdma_cm_id's port space. 246219820Sjeff */ 247219820Sjeffint rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param); 248219820Sjeff 249219820Sjeff/** 250219820Sjeff * rdma_listen - This function is called by the passive side to 251219820Sjeff * listen for incoming connection requests. 252219820Sjeff * 253219820Sjeff * Users must have bound the rdma_cm_id to a local address by calling 254219820Sjeff * rdma_bind_addr before calling this routine. 255219820Sjeff */ 256219820Sjeffint rdma_listen(struct rdma_cm_id *id, int backlog); 257219820Sjeff 258219820Sjeff/** 259219820Sjeff * rdma_accept - Called to accept a connection request or response. 260219820Sjeff * @id: Connection identifier associated with the request. 261219820Sjeff * @conn_param: Information needed to establish the connection. This must be 262219820Sjeff * provided if accepting a connection request. If accepting a connection 263219820Sjeff * response, this parameter must be NULL. 264219820Sjeff * 265219820Sjeff * Typically, this routine is only called by the listener to accept a connection 266219820Sjeff * request. It must also be called on the active side of a connection if the 267219820Sjeff * user is performing their own QP transitions. 268219820Sjeff * 269219820Sjeff * In the case of error, a reject message is sent to the remote side and the 270219820Sjeff * state of the qp associated with the id is modified to error, such that any 271219820Sjeff * previously posted receive buffers would be flushed. 272219820Sjeff */ 273219820Sjeffint rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param); 274219820Sjeff 275219820Sjeff/** 276219820Sjeff * rdma_notify - Notifies the RDMA CM of an asynchronous event that has 277219820Sjeff * occurred on the connection. 278219820Sjeff * @id: Connection identifier to transition to established. 279219820Sjeff * @event: Asynchronous event. 280219820Sjeff * 281219820Sjeff * This routine should be invoked by users to notify the CM of relevant 282219820Sjeff * communication events. Events that should be reported to the CM and 283219820Sjeff * when to report them are: 284219820Sjeff * 285219820Sjeff * IB_EVENT_COMM_EST - Used when a message is received on a connected 286219820Sjeff * QP before an RTU has been received. 287219820Sjeff */ 288219820Sjeffint rdma_notify(struct rdma_cm_id *id, enum ib_event_type event); 289219820Sjeff 290219820Sjeff/** 291219820Sjeff * rdma_reject - Called to reject a connection request or response. 292219820Sjeff */ 293219820Sjeffint rdma_reject(struct rdma_cm_id *id, const void *private_data, 294219820Sjeff u8 private_data_len); 295219820Sjeff 296219820Sjeff/** 297219820Sjeff * rdma_disconnect - This function disconnects the associated QP and 298219820Sjeff * transitions it into the error state. 299219820Sjeff */ 300219820Sjeffint rdma_disconnect(struct rdma_cm_id *id); 301219820Sjeff 302219820Sjeff/** 303219820Sjeff * rdma_join_multicast - Join the multicast group specified by the given 304219820Sjeff * address. 305219820Sjeff * @id: Communication identifier associated with the request. 306219820Sjeff * @addr: Multicast address identifying the group to join. 307219820Sjeff * @context: User-defined context associated with the join request, returned 308219820Sjeff * to the user through the private_data pointer in multicast events. 309219820Sjeff */ 310219820Sjeffint rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, 311219820Sjeff void *context); 312219820Sjeff 313219820Sjeff/** 314219820Sjeff * rdma_leave_multicast - Leave the multicast group specified by the given 315219820Sjeff * address. 316219820Sjeff */ 317219820Sjeffvoid rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr); 318219820Sjeff 319219820Sjeff/** 320219820Sjeff * rdma_set_service_type - Set the type of service associated with a 321219820Sjeff * connection identifier. 322219820Sjeff * @id: Communication identifier to associated with service type. 323219820Sjeff * @tos: Type of service. 324219820Sjeff * 325219820Sjeff * The type of service is interpretted as a differentiated service 326219820Sjeff * field (RFC 2474). The service type should be specified before 327219820Sjeff * performing route resolution, as existing communication on the 328219820Sjeff * connection identifier may be unaffected. The type of service 329219820Sjeff * requested may not be supported by the network to all destinations. 330219820Sjeff */ 331219820Sjeffvoid rdma_set_service_type(struct rdma_cm_id *id, int tos); 332219820Sjeff 333219820Sjeff#endif /* RDMA_CM_H */ 334