1/*
2 * Copyright (c) 2005-2014 Intel Corporation.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#if !defined(CMA_H)
35#define CMA_H
36
37#include <config.h>
38
39#include <stdlib.h>
40#include <errno.h>
41#include <infiniband/endian.h>
42#include <semaphore.h>
43#include <stdatomic.h>
44
45#include <rdma/rdma_cma.h>
46#include <infiniband/ib.h>
47
48#define PFX "librdmacm: "
49
50/*
51 * Fast synchronization for low contention locking.
52 */
53typedef struct {
54	sem_t sem;
55	_Atomic(int) cnt;
56} fastlock_t;
57static inline void fastlock_init(fastlock_t *lock)
58{
59	sem_init(&lock->sem, 0, 0);
60	atomic_store(&lock->cnt, 0);
61}
62static inline void fastlock_destroy(fastlock_t *lock)
63{
64	sem_destroy(&lock->sem);
65}
66static inline void fastlock_acquire(fastlock_t *lock)
67{
68	if (atomic_fetch_add(&lock->cnt, 1) > 0)
69		sem_wait(&lock->sem);
70}
71static inline void fastlock_release(fastlock_t *lock)
72{
73	if (atomic_fetch_sub(&lock->cnt, 1) > 1)
74		sem_post(&lock->sem);
75}
76
77__be16 ucma_get_port(struct sockaddr *addr);
78int ucma_addrlen(struct sockaddr *addr);
79void ucma_set_sid(enum rdma_port_space ps, struct sockaddr *addr,
80		  struct sockaddr_ib *sib);
81int ucma_max_qpsize(struct rdma_cm_id *id);
82int ucma_complete(struct rdma_cm_id *id);
83int ucma_shutdown(struct rdma_cm_id *id);
84
85static inline int ERR(int err)
86{
87	errno = err;
88	return -1;
89}
90
91int ucma_init(void);
92extern int af_ib_support;
93
94#define RAI_ROUTEONLY		0x01000000
95
96void ucma_ib_init(void);
97void ucma_ib_cleanup(void);
98void ucma_ib_resolve(struct rdma_addrinfo **rai,
99		     const struct rdma_addrinfo *hints);
100
101struct ib_connect_hdr {
102	uint8_t  cma_version;
103	uint8_t  ip_version; /* IP version: 7:4 */
104	uint16_t port;
105	uint32_t src_addr[4];
106	uint32_t dst_addr[4];
107#define cma_src_ip4 src_addr[3]
108#define cma_src_ip6 src_addr[0]
109#define cma_dst_ip4 dst_addr[3]
110#define cma_dst_ip6 dst_addr[0]
111};
112
113#endif /* CMA_H */
114