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;
51 } __packed; /* format defined in RFC7609 */
53 /* CDC message for SMC-D */
55 struct smc_wr_rx_hdr common; /* Type = 0xFE */
62 struct smc_cdc_producer_flags prod_flags;
63 struct smc_cdc_conn_state_flags conn_state_flags;
67 static inline bool smc_cdc_rxed_any_close(struct smc_connection *conn)
69 return conn->local_rx_ctrl.conn_state_flags.peer_conn_abort ||
70 conn->local_rx_ctrl.conn_state_flags.peer_conn_closed;
73 static inline bool smc_cdc_rxed_any_close_or_senddone(
74 struct smc_connection *conn)
76 return smc_cdc_rxed_any_close(conn) ||
77 conn->local_rx_ctrl.conn_state_flags.peer_done_writing;
80 static inline void smc_curs_add(int size, union smc_host_cursor *curs,
84 if (curs->count >= size) {
90 /* SMC cursors are 8 bytes long and require atomic reading and writing */
91 static inline u64 smc_curs_read(union smc_host_cursor *curs,
92 struct smc_connection *conn)
94 #ifndef KERNEL_HAS_ATOMIC64
98 spin_lock_irqsave(&conn->acurs_lock, flags);
100 spin_unlock_irqrestore(&conn->acurs_lock, flags);
103 return atomic64_read(&curs->acurs);
107 /* Copy cursor src into tgt */
108 static inline void smc_curs_copy(union smc_host_cursor *tgt,
109 union smc_host_cursor *src,
110 struct smc_connection *conn)
112 #ifndef KERNEL_HAS_ATOMIC64
115 spin_lock_irqsave(&conn->acurs_lock, flags);
116 tgt->acurs = src->acurs;
117 spin_unlock_irqrestore(&conn->acurs_lock, flags);
119 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
123 static inline void smc_curs_copy_net(union smc_cdc_cursor *tgt,
124 union smc_cdc_cursor *src,
125 struct smc_connection *conn)
127 #ifndef KERNEL_HAS_ATOMIC64
130 spin_lock_irqsave(&conn->acurs_lock, flags);
131 tgt->acurs = src->acurs;
132 spin_unlock_irqrestore(&conn->acurs_lock, flags);
134 atomic64_set(&tgt->acurs, atomic64_read(&src->acurs));
138 /* calculate cursor difference between old and new, where old <= new and
139 * difference cannot exceed size
141 static inline int smc_curs_diff(unsigned int size,
142 union smc_host_cursor *old,
143 union smc_host_cursor *new)
145 if (old->wrap != new->wrap)
147 ((size - old->count) + new->count));
149 return max_t(int, 0, (new->count - old->count));
152 /* calculate cursor difference between old and new - returns negative
153 * value in case old > new
155 static inline int smc_curs_comp(unsigned int size,
156 union smc_host_cursor *old,
157 union smc_host_cursor *new)
159 if (old->wrap > new->wrap ||
160 (old->wrap == new->wrap && old->count > new->count))
161 return -smc_curs_diff(size, new, old);
162 return smc_curs_diff(size, old, new);
165 /* calculate cursor difference between old and new, where old <= new and
166 * difference may exceed size
168 static inline int smc_curs_diff_large(unsigned int size,
169 union smc_host_cursor *old,
170 union smc_host_cursor *new)
172 if (old->wrap < new->wrap)
174 (size - old->count) + new->count +
175 (new->wrap - old->wrap - 1) * size,
178 if (old->wrap > new->wrap) /* wrap has switched from 0xffff to 0x0000 */
180 (size - old->count) + new->count +
181 (new->wrap + 0xffff - old->wrap) * size,
184 return max_t(int, 0, (new->count - old->count));
187 static inline void smc_host_cursor_to_cdc(union smc_cdc_cursor *peer,
188 union smc_host_cursor *local,
189 union smc_host_cursor *save,
190 struct smc_connection *conn)
192 smc_curs_copy(save, local, conn);
193 peer->count = htonl(save->count);
194 peer->wrap = htons(save->wrap);
195 /* peer->reserved = htons(0); must be ensured by caller */
198 static inline void smc_host_msg_to_cdc(struct smc_cdc_msg *peer,
199 struct smc_connection *conn,
200 union smc_host_cursor *save)
202 struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
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, save, conn);
209 smc_host_cursor_to_cdc(&peer->cons, &local->cons, save, conn);
210 peer->prod_flags = local->prod_flags;
211 peer->conn_state_flags = local->conn_state_flags;
214 static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local,
215 union smc_cdc_cursor *peer,
216 struct smc_connection *conn)
218 union smc_host_cursor temp, old;
219 union smc_cdc_cursor net;
221 smc_curs_copy(&old, local, conn);
222 smc_curs_copy_net(&net, peer, conn);
223 temp.count = ntohl(net.count);
224 temp.wrap = ntohs(net.wrap);
225 if ((old.wrap > temp.wrap) && temp.wrap)
227 if ((old.wrap == temp.wrap) &&
228 (old.count > temp.count))
230 smc_curs_copy(local, &temp, conn);
233 static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local,
234 struct smc_cdc_msg *peer,
235 struct smc_connection *conn)
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;
247 static inline void smcd_cdc_msg_to_host(struct smc_host_cdc_msg *local,
248 struct smcd_cdc_msg *peer)
250 local->prod.wrap = peer->prod_wrap;
251 local->prod.count = peer->prod_count;
252 local->cons.wrap = peer->cons_wrap;
253 local->cons.count = peer->cons_count;
254 local->prod_flags = peer->prod_flags;
255 local->conn_state_flags = peer->conn_state_flags;
258 static inline void smc_cdc_msg_to_host(struct smc_host_cdc_msg *local,
259 struct smc_cdc_msg *peer,
260 struct smc_connection *conn)
262 if (conn->lgr->is_smcd)
263 smcd_cdc_msg_to_host(local, (struct smcd_cdc_msg *)peer);
265 smcr_cdc_msg_to_host(local, peer, conn);
268 struct smc_cdc_tx_pend;
270 int smc_cdc_get_free_slot(struct smc_connection *conn,
271 struct smc_wr_buf **wr_buf,
272 struct smc_cdc_tx_pend **pend);
273 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn);
274 int smc_cdc_msg_send(struct smc_connection *conn, struct smc_wr_buf *wr_buf,
275 struct smc_cdc_tx_pend *pend);
276 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn);
277 int smcd_cdc_msg_send(struct smc_connection *conn);
278 int smc_cdc_init(void) __init;
279 void smcd_cdc_rx_init(struct smc_connection *conn);
281 #endif /* SMC_CDC_H */