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 /* SMC cursors are 8 bytes long and require atomic reading and writing */
101 static inline u64
smc_curs_read(union smc_host_cursor
*curs
,
102 struct smc_connection
*conn
)
104 #ifndef KERNEL_HAS_ATOMIC64
108 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
110 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
113 return atomic64_read(&curs
->acurs
);
117 /* Copy cursor src into tgt */
118 static inline void smc_curs_copy(union smc_host_cursor
*tgt
,
119 union smc_host_cursor
*src
,
120 struct smc_connection
*conn
)
122 #ifndef KERNEL_HAS_ATOMIC64
125 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
126 tgt
->acurs
= src
->acurs
;
127 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
129 atomic64_set(&tgt
->acurs
, atomic64_read(&src
->acurs
));
133 static inline void smc_curs_copy_net(union smc_cdc_cursor
*tgt
,
134 union smc_cdc_cursor
*src
,
135 struct smc_connection
*conn
)
137 #ifndef KERNEL_HAS_ATOMIC64
140 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
141 tgt
->acurs
= src
->acurs
;
142 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
144 atomic64_set(&tgt
->acurs
, atomic64_read(&src
->acurs
));
148 static inline void smcd_curs_copy(union smcd_cdc_cursor
*tgt
,
149 union smcd_cdc_cursor
*src
,
150 struct smc_connection
*conn
)
152 #ifndef KERNEL_HAS_ATOMIC64
155 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
156 tgt
->acurs
= src
->acurs
;
157 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
159 atomic64_set(&tgt
->acurs
, atomic64_read(&src
->acurs
));
163 /* calculate cursor difference between old and new, where old <= new and
164 * difference cannot exceed size
166 static inline int smc_curs_diff(unsigned int size
,
167 union smc_host_cursor
*old
,
168 union smc_host_cursor
*new)
170 if (old
->wrap
!= new->wrap
)
172 ((size
- old
->count
) + new->count
));
174 return max_t(int, 0, (new->count
- old
->count
));
177 /* calculate cursor difference between old and new - returns negative
178 * value in case old > new
180 static inline int smc_curs_comp(unsigned int size
,
181 union smc_host_cursor
*old
,
182 union smc_host_cursor
*new)
184 if (old
->wrap
> new->wrap
||
185 (old
->wrap
== new->wrap
&& old
->count
> new->count
))
186 return -smc_curs_diff(size
, new, old
);
187 return smc_curs_diff(size
, old
, new);
190 /* calculate cursor difference between old and new, where old <= new and
191 * difference may exceed size
193 static inline int smc_curs_diff_large(unsigned int size
,
194 union smc_host_cursor
*old
,
195 union smc_host_cursor
*new)
197 if (old
->wrap
< new->wrap
)
199 (size
- old
->count
) + new->count
+
200 (new->wrap
- old
->wrap
- 1) * size
,
203 if (old
->wrap
> new->wrap
) /* wrap has switched from 0xffff to 0x0000 */
205 (size
- old
->count
) + new->count
+
206 (new->wrap
+ 0xffff - old
->wrap
) * size
,
209 return max_t(int, 0, (new->count
- old
->count
));
212 static inline void smc_host_cursor_to_cdc(union smc_cdc_cursor
*peer
,
213 union smc_host_cursor
*local
,
214 union smc_host_cursor
*save
,
215 struct smc_connection
*conn
)
217 smc_curs_copy(save
, local
, conn
);
218 peer
->count
= htonl(save
->count
);
219 peer
->wrap
= htons(save
->wrap
);
220 /* peer->reserved = htons(0); must be ensured by caller */
223 static inline void smc_host_msg_to_cdc(struct smc_cdc_msg
*peer
,
224 struct smc_connection
*conn
,
225 union smc_host_cursor
*save
)
227 struct smc_host_cdc_msg
*local
= &conn
->local_tx_ctrl
;
229 peer
->common
.type
= local
->common
.type
;
230 peer
->len
= local
->len
;
231 peer
->seqno
= htons(local
->seqno
);
232 peer
->token
= htonl(local
->token
);
233 smc_host_cursor_to_cdc(&peer
->prod
, &local
->prod
, save
, conn
);
234 smc_host_cursor_to_cdc(&peer
->cons
, &local
->cons
, save
, conn
);
235 peer
->prod_flags
= local
->prod_flags
;
236 peer
->conn_state_flags
= local
->conn_state_flags
;
239 static inline void smc_cdc_cursor_to_host(union smc_host_cursor
*local
,
240 union smc_cdc_cursor
*peer
,
241 struct smc_connection
*conn
)
243 union smc_host_cursor temp
, old
;
244 union smc_cdc_cursor net
;
246 smc_curs_copy(&old
, local
, conn
);
247 smc_curs_copy_net(&net
, peer
, conn
);
248 temp
.count
= ntohl(net
.count
);
249 temp
.wrap
= ntohs(net
.wrap
);
250 if ((old
.wrap
> temp
.wrap
) && temp
.wrap
)
252 if ((old
.wrap
== temp
.wrap
) &&
253 (old
.count
> temp
.count
))
255 smc_curs_copy(local
, &temp
, conn
);
258 static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg
*local
,
259 struct smc_cdc_msg
*peer
,
260 struct smc_connection
*conn
)
262 local
->common
.type
= peer
->common
.type
;
263 local
->len
= peer
->len
;
264 local
->seqno
= ntohs(peer
->seqno
);
265 local
->token
= ntohl(peer
->token
);
266 smc_cdc_cursor_to_host(&local
->prod
, &peer
->prod
, conn
);
267 smc_cdc_cursor_to_host(&local
->cons
, &peer
->cons
, conn
);
268 local
->prod_flags
= peer
->prod_flags
;
269 local
->conn_state_flags
= peer
->conn_state_flags
;
272 static inline void smcd_cdc_msg_to_host(struct smc_host_cdc_msg
*local
,
273 struct smcd_cdc_msg
*peer
,
274 struct smc_connection
*conn
)
276 union smc_host_cursor temp
;
278 temp
.wrap
= peer
->prod
.wrap
;
279 temp
.count
= peer
->prod
.count
;
280 smc_curs_copy(&local
->prod
, &temp
, conn
);
282 temp
.wrap
= peer
->cons
.wrap
;
283 temp
.count
= peer
->cons
.count
;
284 smc_curs_copy(&local
->cons
, &temp
, conn
);
285 local
->prod_flags
= peer
->cons
.prod_flags
;
286 local
->conn_state_flags
= peer
->cons
.conn_state_flags
;
289 static inline void smc_cdc_msg_to_host(struct smc_host_cdc_msg
*local
,
290 struct smc_cdc_msg
*peer
,
291 struct smc_connection
*conn
)
293 if (conn
->lgr
->is_smcd
)
294 smcd_cdc_msg_to_host(local
, (struct smcd_cdc_msg
*)peer
, conn
);
296 smcr_cdc_msg_to_host(local
, peer
, conn
);
299 struct smc_cdc_tx_pend
{
300 struct smc_connection
*conn
; /* socket connection */
301 union smc_host_cursor cursor
; /* tx sndbuf cursor sent */
302 union smc_host_cursor p_cursor
; /* rx RMBE cursor produced */
303 u16 ctrl_seq
; /* conn. tx sequence # */
306 int smc_cdc_get_free_slot(struct smc_connection
*conn
,
307 struct smc_wr_buf
**wr_buf
,
308 struct smc_rdma_wr
**wr_rdma_buf
,
309 struct smc_cdc_tx_pend
**pend
);
310 void smc_cdc_tx_dismiss_slots(struct smc_connection
*conn
);
311 int smc_cdc_msg_send(struct smc_connection
*conn
, struct smc_wr_buf
*wr_buf
,
312 struct smc_cdc_tx_pend
*pend
);
313 int smc_cdc_get_slot_and_msg_send(struct smc_connection
*conn
);
314 int smcd_cdc_msg_send(struct smc_connection
*conn
);
315 int smc_cdc_init(void) __init
;
316 void smcd_cdc_rx_init(struct smc_connection
*conn
);
318 #endif /* SMC_CDC_H */