xref: /openbmc/linux/net/smc/smc_core.h (revision d8bcaabe)
1 /*
2  * Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  *  Definitions for SMC Connections, Link Groups and Links
5  *
6  *  Copyright IBM Corp. 2016
7  *
8  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
9  */
10 
11 #ifndef _SMC_CORE_H
12 #define _SMC_CORE_H
13 
14 #include <linux/atomic.h>
15 #include <rdma/ib_verbs.h>
16 
17 #include "smc.h"
18 #include "smc_ib.h"
19 
20 #define SMC_RMBS_PER_LGR_MAX	255	/* max. # of RMBs per link group */
21 
22 struct smc_lgr_list {			/* list of link group definition */
23 	struct list_head	list;
24 	spinlock_t		lock;	/* protects list of link groups */
25 };
26 
27 extern struct smc_lgr_list	smc_lgr_list; /* list of link groups */
28 
29 enum smc_lgr_role {		/* possible roles of a link group */
30 	SMC_CLNT,	/* client */
31 	SMC_SERV	/* server */
32 };
33 
34 #define SMC_WR_BUF_SIZE		48	/* size of work request buffer */
35 
36 struct smc_wr_buf {
37 	u8	raw[SMC_WR_BUF_SIZE];
38 };
39 
40 #define SMC_WR_REG_MR_WAIT_TIME	(5 * HZ)/* wait time for ib_wr_reg_mr result */
41 
42 enum smc_wr_reg_state {
43 	POSTED,		/* ib_wr_reg_mr request posted */
44 	CONFIRMED,	/* ib_wr_reg_mr response: successful */
45 	FAILED		/* ib_wr_reg_mr response: failure */
46 };
47 
48 struct smc_link {
49 	struct smc_ib_device	*smcibdev;	/* ib-device */
50 	u8			ibport;		/* port - values 1 | 2 */
51 	struct ib_pd		*roce_pd;	/* IB protection domain,
52 						 * unique for every RoCE QP
53 						 */
54 	struct ib_qp		*roce_qp;	/* IB queue pair */
55 	struct ib_qp_attr	qp_attr;	/* IB queue pair attributes */
56 
57 	struct smc_wr_buf	*wr_tx_bufs;	/* WR send payload buffers */
58 	struct ib_send_wr	*wr_tx_ibs;	/* WR send meta data */
59 	struct ib_sge		*wr_tx_sges;	/* WR send gather meta data */
60 	struct smc_wr_tx_pend	*wr_tx_pends;	/* WR send waiting for CQE */
61 	/* above four vectors have wr_tx_cnt elements and use the same index */
62 	dma_addr_t		wr_tx_dma_addr;	/* DMA address of wr_tx_bufs */
63 	atomic_long_t		wr_tx_id;	/* seq # of last sent WR */
64 	unsigned long		*wr_tx_mask;	/* bit mask of used indexes */
65 	u32			wr_tx_cnt;	/* number of WR send buffers */
66 	wait_queue_head_t	wr_tx_wait;	/* wait for free WR send buf */
67 
68 	struct smc_wr_buf	*wr_rx_bufs;	/* WR recv payload buffers */
69 	struct ib_recv_wr	*wr_rx_ibs;	/* WR recv meta data */
70 	struct ib_sge		*wr_rx_sges;	/* WR recv scatter meta data */
71 	/* above three vectors have wr_rx_cnt elements and use the same index */
72 	dma_addr_t		wr_rx_dma_addr;	/* DMA address of wr_rx_bufs */
73 	u64			wr_rx_id;	/* seq # of last recv WR */
74 	u32			wr_rx_cnt;	/* number of WR recv buffers */
75 
76 	struct ib_reg_wr	wr_reg;		/* WR register memory region */
77 	wait_queue_head_t	wr_reg_wait;	/* wait for wr_reg result */
78 	enum smc_wr_reg_state	wr_reg_state;	/* state of wr_reg request */
79 
80 	union ib_gid		gid;		/* gid matching used vlan id */
81 	u32			peer_qpn;	/* QP number of peer */
82 	enum ib_mtu		path_mtu;	/* used mtu */
83 	enum ib_mtu		peer_mtu;	/* mtu size of peer */
84 	u32			psn_initial;	/* QP tx initial packet seqno */
85 	u32			peer_psn;	/* QP rx initial packet seqno */
86 	u8			peer_mac[ETH_ALEN];	/* = gid[8:10||13:15] */
87 	u8			peer_gid[sizeof(union ib_gid)];	/* gid of peer*/
88 	u8			link_id;	/* unique # within link group */
89 	struct completion	llc_confirm;	/* wait for rx of conf link */
90 	struct completion	llc_confirm_resp; /* wait 4 rx of cnf lnk rsp */
91 };
92 
93 /* For now we just allow one parallel link per link group. The SMC protocol
94  * allows more (up to 8).
95  */
96 #define SMC_LINKS_PER_LGR_MAX	1
97 #define SMC_SINGLE_LINK		0
98 
99 #define SMC_FIRST_CONTACT	1		/* first contact to a peer */
100 #define SMC_REUSE_CONTACT	0		/* follow-on contact to a peer*/
101 
102 /* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
103 struct smc_buf_desc {
104 	struct list_head	list;
105 	void			*cpu_addr;	/* virtual address of buffer */
106 	struct sg_table		sgt[SMC_LINKS_PER_LGR_MAX];/* virtual buffer */
107 	struct ib_mr		*mr_rx[SMC_LINKS_PER_LGR_MAX];
108 						/* for rmb only: memory region
109 						 * incl. rkey provided to peer
110 						 */
111 	u32			order;		/* allocation order */
112 	u32			used;		/* currently used / unused */
113 	bool			reused;		/* new created / reused */
114 };
115 
116 struct smc_rtoken {				/* address/key of remote RMB */
117 	u64			dma_addr;
118 	u32			rkey;
119 };
120 
121 #define SMC_LGR_ID_SIZE		4
122 
123 struct smc_link_group {
124 	struct list_head	list;
125 	enum smc_lgr_role	role;		/* client or server */
126 	__be32			daddr;		/* destination ip address */
127 	struct smc_link		lnk[SMC_LINKS_PER_LGR_MAX];	/* smc link */
128 	char			peer_systemid[SMC_SYSTEMID_LEN];
129 						/* unique system_id of peer */
130 	struct rb_root		conns_all;	/* connection tree */
131 	rwlock_t		conns_lock;	/* protects conns_all */
132 	unsigned int		conns_num;	/* current # of connections */
133 	unsigned short		vlan_id;	/* vlan id of link group */
134 
135 	struct list_head	sndbufs[SMC_RMBE_SIZES];/* tx buffers */
136 	rwlock_t		sndbufs_lock;	/* protects tx buffers */
137 	struct list_head	rmbs[SMC_RMBE_SIZES];	/* rx buffers */
138 	rwlock_t		rmbs_lock;	/* protects rx buffers */
139 	struct smc_rtoken	rtokens[SMC_RMBS_PER_LGR_MAX]
140 				       [SMC_LINKS_PER_LGR_MAX];
141 						/* remote addr/key pairs */
142 	unsigned long		rtokens_used_mask[BITS_TO_LONGS(
143 							SMC_RMBS_PER_LGR_MAX)];
144 						/* used rtoken elements */
145 
146 	u8			id[SMC_LGR_ID_SIZE];	/* unique lgr id */
147 	struct delayed_work	free_work;	/* delayed freeing of an lgr */
148 	bool			sync_err;	/* lgr no longer fits to peer */
149 };
150 
151 /* Find the connection associated with the given alert token in the link group.
152  * To use rbtrees we have to implement our own search core.
153  * Requires @conns_lock
154  * @token	alert token to search for
155  * @lgr		 link group to search in
156  * Returns connection associated with token if found, NULL otherwise.
157  */
158 static inline struct smc_connection *smc_lgr_find_conn(
159 	u32 token, struct smc_link_group *lgr)
160 {
161 	struct smc_connection *res = NULL;
162 	struct rb_node *node;
163 
164 	node = lgr->conns_all.rb_node;
165 	while (node) {
166 		struct smc_connection *cur = rb_entry(node,
167 					struct smc_connection, alert_node);
168 
169 		if (cur->alert_token_local > token) {
170 			node = node->rb_left;
171 		} else {
172 			if (cur->alert_token_local < token) {
173 				node = node->rb_right;
174 			} else {
175 				res = cur;
176 				break;
177 			}
178 		}
179 	}
180 
181 	return res;
182 }
183 
184 struct smc_sock;
185 struct smc_clc_msg_accept_confirm;
186 
187 void smc_lgr_free(struct smc_link_group *lgr);
188 void smc_lgr_terminate(struct smc_link_group *lgr);
189 int smc_buf_create(struct smc_sock *smc);
190 int smc_rmb_rtoken_handling(struct smc_connection *conn,
191 			    struct smc_clc_msg_accept_confirm *clc);
192 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
193 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
194 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
195 void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
196 #endif
197