net/smc: preallocated memory for rdma work requests
[linux-2.6-block.git] / net / smc / smc_cdc.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
5f08318f
UB
2/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Connection Data Control (CDC)
6 *
7 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
10 */
11
12#ifndef SMC_CDC_H
13#define SMC_CDC_H
14
15#include <linux/kernel.h> /* max_t */
16#include <linux/atomic.h>
17#include <linux/in.h>
18#include <linux/compiler.h>
19
20#include "smc.h"
21#include "smc_core.h"
22#include "smc_wr.h"
23
24#define SMC_CDC_MSG_TYPE 0xFE
25
26/* in network byte order */
27union smc_cdc_cursor { /* SMC cursor */
28 struct {
29 __be16 reserved;
30 __be16 wrap;
31 __be32 count;
32 };
33#ifdef KERNEL_HAS_ATOMIC64
34 atomic64_t acurs; /* for atomic processing */
35#else
36 u64 acurs; /* for atomic processing */
37#endif
38} __aligned(8);
39
40/* in network byte order */
41struct smc_cdc_msg {
42 struct smc_wr_rx_hdr common; /* .type = 0xFE */
43 u8 len; /* 44 */
44 __be16 seqno;
45 __be32 token;
46 union smc_cdc_cursor prod;
47 union smc_cdc_cursor cons; /* piggy backed "ack" */
48 struct smc_cdc_producer_flags prod_flags;
49 struct smc_cdc_conn_state_flags conn_state_flags;
50 u8 reserved[18];
b9a22dd9
UB
51};
52
53/* SMC-D cursor format */
54union smcd_cdc_cursor {
55 struct {
56 u16 wrap;
57 u32 count;
58 struct smc_cdc_producer_flags prod_flags;
59 struct smc_cdc_conn_state_flags conn_state_flags;
60 } __packed;
61#ifdef KERNEL_HAS_ATOMIC64
62 atomic64_t acurs; /* for atomic processing */
63#else
64 u64 acurs; /* for atomic processing */
65#endif
66} __aligned(8);
5f08318f 67
be244f28
HW
68/* CDC message for SMC-D */
69struct smcd_cdc_msg {
70 struct smc_wr_rx_hdr common; /* Type = 0xFE */
71 u8 res1[7];
b9a22dd9
UB
72 union smcd_cdc_cursor prod;
73 union smcd_cdc_cursor cons;
be244f28 74 u8 res3[8];
b9a22dd9 75} __aligned(8);
be244f28 76
5f08318f
UB
77static inline bool smc_cdc_rxed_any_close(struct smc_connection *conn)
78{
79 return conn->local_rx_ctrl.conn_state_flags.peer_conn_abort ||
80 conn->local_rx_ctrl.conn_state_flags.peer_conn_closed;
81}
82
83static inline bool smc_cdc_rxed_any_close_or_senddone(
84 struct smc_connection *conn)
85{
86 return smc_cdc_rxed_any_close(conn) ||
87 conn->local_rx_ctrl.conn_state_flags.peer_done_writing;
88}
89
90static inline void smc_curs_add(int size, union smc_host_cursor *curs,
91 int value)
92{
93 curs->count += value;
94 if (curs->count >= size) {
95 curs->wrap++;
96 curs->count -= size;
97 }
98}
99
100/* SMC cursors are 8 bytes long and require atomic reading and writing */
101static inline u64 smc_curs_read(union smc_host_cursor *curs,
102 struct smc_connection *conn)
103{
104#ifndef KERNEL_HAS_ATOMIC64
105 unsigned long flags;
106 u64 ret;
107
108 spin_lock_irqsave(&conn->acurs_lock, flags);
109 ret = curs->acurs;
110 spin_unlock_irqrestore(&conn->acurs_lock, flags);
111 return ret;
112#else
113 return atomic64_read(&curs->acurs);
114#endif
115}
116
bac6de7b
SR
117/* Copy cursor src into tgt */
118static inline void smc_curs_copy(union smc_host_cursor *tgt,
119 union smc_host_cursor *src,
120 struct smc_connection *conn)
5f08318f
UB
121{
122#ifndef KERNEL_HAS_ATOMIC64
123 unsigned long flags;
124
125 spin_lock_irqsave(&conn->acurs_lock, flags);
bac6de7b 126 tgt->acurs = src->acurs;
5f08318f
UB
127 spin_unlock_irqrestore(&conn->acurs_lock, flags);
128#else
bac6de7b 129 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
5f08318f
UB
130#endif
131}
132
bac6de7b
SR
133static inline void smc_curs_copy_net(union smc_cdc_cursor *tgt,
134 union smc_cdc_cursor *src,
135 struct smc_connection *conn)
5f08318f
UB
136{
137#ifndef KERNEL_HAS_ATOMIC64
138 unsigned long flags;
139
140 spin_lock_irqsave(&conn->acurs_lock, flags);
bac6de7b 141 tgt->acurs = src->acurs;
5f08318f
UB
142 spin_unlock_irqrestore(&conn->acurs_lock, flags);
143#else
bac6de7b 144 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
5f08318f
UB
145#endif
146}
147
b9a22dd9
UB
148static inline void smcd_curs_copy(union smcd_cdc_cursor *tgt,
149 union smcd_cdc_cursor *src,
150 struct smc_connection *conn)
151{
152#ifndef KERNEL_HAS_ATOMIC64
153 unsigned long flags;
154
155 spin_lock_irqsave(&conn->acurs_lock, flags);
156 tgt->acurs = src->acurs;
157 spin_unlock_irqrestore(&conn->acurs_lock, flags);
158#else
159 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
160#endif
161}
162
5f08318f
UB
163/* calculate cursor difference between old and new, where old <= new */
164static inline int smc_curs_diff(unsigned int size,
165 union smc_host_cursor *old,
166 union smc_host_cursor *new)
167{
168 if (old->wrap != new->wrap)
169 return max_t(int, 0,
170 ((size - old->count) + new->count));
171
172 return max_t(int, 0, (new->count - old->count));
173}
174
de8474eb
SR
175/* calculate cursor difference between old and new - returns negative
176 * value in case old > new
177 */
178static inline int smc_curs_comp(unsigned int size,
179 union smc_host_cursor *old,
180 union smc_host_cursor *new)
181{
182 if (old->wrap > new->wrap ||
183 (old->wrap == new->wrap && old->count > new->count))
184 return -smc_curs_diff(size, new, old);
185 return smc_curs_diff(size, old, new);
186}
187
5f08318f
UB
188static inline void smc_host_cursor_to_cdc(union smc_cdc_cursor *peer,
189 union smc_host_cursor *local,
190 struct smc_connection *conn)
191{
192 union smc_host_cursor temp;
193
bac6de7b 194 smc_curs_copy(&temp, local, conn);
5f08318f
UB
195 peer->count = htonl(temp.count);
196 peer->wrap = htons(temp.wrap);
197 /* peer->reserved = htons(0); must be ensured by caller */
198}
199
200static inline void smc_host_msg_to_cdc(struct smc_cdc_msg *peer,
201 struct smc_host_cdc_msg *local,
202 struct smc_connection *conn)
203{
204 peer->common.type = local->common.type;
205 peer->len = local->len;
206 peer->seqno = htons(local->seqno);
207 peer->token = htonl(local->token);
208 smc_host_cursor_to_cdc(&peer->prod, &local->prod, conn);
209 smc_host_cursor_to_cdc(&peer->cons, &local->cons, conn);
210 peer->prod_flags = local->prod_flags;
211 peer->conn_state_flags = local->conn_state_flags;
212}
213
214static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local,
215 union smc_cdc_cursor *peer,
216 struct smc_connection *conn)
217{
218 union smc_host_cursor temp, old;
219 union smc_cdc_cursor net;
220
bac6de7b
SR
221 smc_curs_copy(&old, local, conn);
222 smc_curs_copy_net(&net, peer, conn);
5f08318f
UB
223 temp.count = ntohl(net.count);
224 temp.wrap = ntohs(net.wrap);
225 if ((old.wrap > temp.wrap) && temp.wrap)
226 return;
227 if ((old.wrap == temp.wrap) &&
228 (old.count > temp.count))
229 return;
bac6de7b 230 smc_curs_copy(local, &temp, conn);
5f08318f
UB
231}
232
be244f28
HW
233static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local,
234 struct smc_cdc_msg *peer,
235 struct smc_connection *conn)
5f08318f
UB
236{
237 local->common.type = peer->common.type;
238 local->len = peer->len;
239 local->seqno = ntohs(peer->seqno);
240 local->token = ntohl(peer->token);
241 smc_cdc_cursor_to_host(&local->prod, &peer->prod, conn);
242 smc_cdc_cursor_to_host(&local->cons, &peer->cons, conn);
243 local->prod_flags = peer->prod_flags;
244 local->conn_state_flags = peer->conn_state_flags;
245}
246
be244f28
HW
247static inline void smcd_cdc_msg_to_host(struct smc_host_cdc_msg *local,
248 struct smcd_cdc_msg *peer)
249{
b9a22dd9
UB
250 union smc_host_cursor temp;
251
252 temp.wrap = peer->prod.wrap;
253 temp.count = peer->prod.count;
254 atomic64_set(&local->prod.acurs, atomic64_read(&temp.acurs));
255
256 temp.wrap = peer->cons.wrap;
257 temp.count = peer->cons.count;
258 atomic64_set(&local->cons.acurs, atomic64_read(&temp.acurs));
259 local->prod_flags = peer->cons.prod_flags;
260 local->conn_state_flags = peer->cons.conn_state_flags;
be244f28
HW
261}
262
263static inline void smc_cdc_msg_to_host(struct smc_host_cdc_msg *local,
264 struct smc_cdc_msg *peer,
265 struct smc_connection *conn)
266{
267 if (conn->lgr->is_smcd)
268 smcd_cdc_msg_to_host(local, (struct smcd_cdc_msg *)peer);
269 else
270 smcr_cdc_msg_to_host(local, peer, conn);
271}
272
ad6f317f
UB
273struct smc_cdc_tx_pend {
274 struct smc_connection *conn; /* socket connection */
275 union smc_host_cursor cursor; /* tx sndbuf cursor sent */
276 union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
277 u16 ctrl_seq; /* conn. tx sequence # */
278};
5f08318f 279
51957bc5
UB
280int smc_cdc_get_free_slot(struct smc_connection *conn,
281 struct smc_wr_buf **wr_buf,
ad6f317f 282 struct smc_rdma_wr **wr_rdma_buf,
5f08318f
UB
283 struct smc_cdc_tx_pend **pend);
284void smc_cdc_tx_dismiss_slots(struct smc_connection *conn);
285int smc_cdc_msg_send(struct smc_connection *conn, struct smc_wr_buf *wr_buf,
286 struct smc_cdc_tx_pend *pend);
287int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn);
be244f28 288int smcd_cdc_msg_send(struct smc_connection *conn);
5f08318f 289int smc_cdc_init(void) __init;
be244f28 290void smcd_cdc_rx_init(struct smc_connection *conn);
5f08318f
UB
291
292#endif /* SMC_CDC_H */