1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Connection Data Control (CDC)
7 * Copyright IBM Corp. 2016
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
15 #include <linux/kernel.h> /* max_t */
16 #include <linux/atomic.h>
18 #include <linux/compiler.h>
24 #define SMC_CDC_MSG_TYPE 0xFE
26 /* in network byte order */
27 union smc_cdc_cursor { /* SMC cursor */
33 #ifdef KERNEL_HAS_ATOMIC64
34 atomic64_t acurs; /* for atomic processing */
36 u64 acurs; /* for atomic processing */
40 /* in network byte order */
42 struct smc_wr_rx_hdr common; /* .type = 0xFE */
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;
53 /* SMC-D cursor format */
54 union smcd_cdc_cursor {
58 struct smc_cdc_producer_flags prod_flags;
59 struct smc_cdc_conn_state_flags conn_state_flags;
61 #ifdef KERNEL_HAS_ATOMIC64
62 atomic64_t acurs; /* for atomic processing */
64 u64 acurs; /* for atomic processing */
68 /* CDC message for SMC-D */
70 struct smc_wr_rx_hdr common; /* Type = 0xFE */
72 union smcd_cdc_cursor prod;
73 union smcd_cdc_cursor cons;
77 static inline bool smc_cdc_rxed_any_close(struct smc_connection *conn)
79 return conn->local_rx_ctrl.conn_state_flags.peer_conn_abort ||
80 conn->local_rx_ctrl.conn_state_flags.peer_conn_closed;
83 static inline bool smc_cdc_rxed_any_close_or_senddone(
84 struct smc_connection *conn)
86 return smc_cdc_rxed_any_close(conn) ||
87 conn->local_rx_ctrl.conn_state_flags.peer_done_writing;
90 static inline void smc_curs_add(int size, union smc_host_cursor *curs,
94 if (curs->count >= size) {
100 /* Copy cursor src into tgt */
101 static inline void smc_curs_copy(union smc_host_cursor *tgt,
102 union smc_host_cursor *src,
103 struct smc_connection *conn)
105 #ifndef KERNEL_HAS_ATOMIC64
108 spin_lock_irqsave(&conn->acurs_lock, flags);
109 tgt->acurs = src->acurs;
110 spin_unlock_irqrestore(&conn->acurs_lock, flags);
112 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
116 static inline void smc_curs_copy_net(union smc_cdc_cursor *tgt,
117 union smc_cdc_cursor *src,
118 struct smc_connection *conn)
120 #ifndef KERNEL_HAS_ATOMIC64
123 spin_lock_irqsave(&conn->acurs_lock, flags);
124 tgt->acurs = src->acurs;
125 spin_unlock_irqrestore(&conn->acurs_lock, flags);
127 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
131 static inline void smcd_curs_copy(union smcd_cdc_cursor *tgt,
132 union smcd_cdc_cursor *src,
133 struct smc_connection *conn)
135 #ifndef KERNEL_HAS_ATOMIC64
138 spin_lock_irqsave(&conn->acurs_lock, flags);
139 tgt->acurs = src->acurs;
140 spin_unlock_irqrestore(&conn->acurs_lock, flags);
142 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
146 /* calculate cursor difference between old and new, where old <= new and
147 * difference cannot exceed size
149 static inline int smc_curs_diff(unsigned int size,
150 union smc_host_cursor *old,
151 union smc_host_cursor *new)
153 if (old->wrap != new->wrap)
155 ((size - old->count) + new->count));
157 return max_t(int, 0, (new->count - old->count));
160 /* calculate cursor difference between old and new - returns negative
161 * value in case old > new
163 static inline int smc_curs_comp(unsigned int size,
164 union smc_host_cursor *old,
165 union smc_host_cursor *new)
167 if (old->wrap > new->wrap ||
168 (old->wrap == new->wrap && old->count > new->count))
169 return -smc_curs_diff(size, new, old);
170 return smc_curs_diff(size, old, new);
173 /* calculate cursor difference between old and new, where old <= new and
174 * difference may exceed size
176 static inline int smc_curs_diff_large(unsigned int size,
177 union smc_host_cursor *old,
178 union smc_host_cursor *new)
180 if (old->wrap < new->wrap)
182 (size - old->count) + new->count +
183 (new->wrap - old->wrap - 1) * size,
186 if (old->wrap > new->wrap) /* wrap has switched from 0xffff to 0x0000 */
188 (size - old->count) + new->count +
189 (new->wrap + 0xffff - old->wrap) * size,
192 return max_t(int, 0, (new->count - old->count));
195 static inline void smc_host_cursor_to_cdc(union smc_cdc_cursor *peer,
196 union smc_host_cursor *local,
197 union smc_host_cursor *save,
198 struct smc_connection *conn)
200 smc_curs_copy(save, local, conn);
201 peer->count = htonl(save->count);
202 peer->wrap = htons(save->wrap);
203 /* peer->reserved = htons(0); must be ensured by caller */
206 static inline void smc_host_msg_to_cdc(struct smc_cdc_msg *peer,
207 struct smc_connection *conn,
208 union smc_host_cursor *save)
210 struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
212 peer->common.type = local->common.type;
213 peer->len = local->len;
214 peer->seqno = htons(local->seqno);
215 peer->token = htonl(local->token);
216 smc_host_cursor_to_cdc(&peer->prod, &local->prod, save, conn);
217 smc_host_cursor_to_cdc(&peer->cons, &local->cons, save, conn);
218 peer->prod_flags = local->prod_flags;
219 peer->conn_state_flags = local->conn_state_flags;
222 static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local,
223 union smc_cdc_cursor *peer,
224 struct smc_connection *conn)
226 union smc_host_cursor temp, old;
227 union smc_cdc_cursor net;
229 smc_curs_copy(&old, local, conn);
230 smc_curs_copy_net(&net, peer, conn);
231 temp.count = ntohl(net.count);
232 temp.wrap = ntohs(net.wrap);
233 if ((old.wrap > temp.wrap) && temp.wrap)
235 if ((old.wrap == temp.wrap) &&
236 (old.count > temp.count))
238 smc_curs_copy(local, &temp, conn);
241 static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local,
242 struct smc_cdc_msg *peer,
243 struct smc_connection *conn)
245 local->common.type = peer->common.type;
246 local->len = peer->len;
247 local->seqno = ntohs(peer->seqno);
248 local->token = ntohl(peer->token);
249 smc_cdc_cursor_to_host(&local->prod, &peer->prod, conn);
250 smc_cdc_cursor_to_host(&local->cons, &peer->cons, conn);
251 local->prod_flags = peer->prod_flags;
252 local->conn_state_flags = peer->conn_state_flags;
255 static inline void smcd_cdc_msg_to_host(struct smc_host_cdc_msg *local,
256 struct smcd_cdc_msg *peer,
257 struct smc_connection *conn)
259 union smc_host_cursor temp;
261 temp.wrap = peer->prod.wrap;
262 temp.count = peer->prod.count;
263 smc_curs_copy(&local->prod, &temp, conn);
265 temp.wrap = peer->cons.wrap;
266 temp.count = peer->cons.count;
267 smc_curs_copy(&local->cons, &temp, conn);
268 local->prod_flags = peer->cons.prod_flags;
269 local->conn_state_flags = peer->cons.conn_state_flags;
272 static inline void smc_cdc_msg_to_host(struct smc_host_cdc_msg *local,
273 struct smc_cdc_msg *peer,
274 struct smc_connection *conn)
276 if (conn->lgr->is_smcd)
277 smcd_cdc_msg_to_host(local, (struct smcd_cdc_msg *)peer, conn);
279 smcr_cdc_msg_to_host(local, peer, conn);
282 struct smc_cdc_tx_pend {
283 struct smc_connection *conn; /* socket connection */
284 union smc_host_cursor cursor; /* tx sndbuf cursor sent */
285 union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
286 u16 ctrl_seq; /* conn. tx sequence # */
289 int smc_cdc_get_free_slot(struct smc_connection *conn,
290 struct smc_link *link,
291 struct smc_wr_buf **wr_buf,
292 struct smc_rdma_wr **wr_rdma_buf,
293 struct smc_cdc_tx_pend **pend);
294 void smc_cdc_wait_pend_tx_wr(struct smc_connection *conn);
295 int smc_cdc_msg_send(struct smc_connection *conn, struct smc_wr_buf *wr_buf,
296 struct smc_cdc_tx_pend *pend);
297 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn);
298 int smcd_cdc_msg_send(struct smc_connection *conn);
299 int smcr_cdc_msg_send_validation(struct smc_connection *conn,
300 struct smc_cdc_tx_pend *pend,
301 struct smc_wr_buf *wr_buf);
302 int smc_cdc_init(void) __init;
303 void smcd_cdc_rx_init(struct smc_connection *conn);
305 #endif /* SMC_CDC_H */