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 static inline bool smc_cdc_rxed_any_close(struct smc_connection
*conn
)
55 return conn
->local_rx_ctrl
.conn_state_flags
.peer_conn_abort
||
56 conn
->local_rx_ctrl
.conn_state_flags
.peer_conn_closed
;
59 static inline bool smc_cdc_rxed_any_close_or_senddone(
60 struct smc_connection
*conn
)
62 return smc_cdc_rxed_any_close(conn
) ||
63 conn
->local_rx_ctrl
.conn_state_flags
.peer_done_writing
;
66 static inline void smc_curs_add(int size
, union smc_host_cursor
*curs
,
70 if (curs
->count
>= size
) {
76 /* SMC cursors are 8 bytes long and require atomic reading and writing */
77 static inline u64
smc_curs_read(union smc_host_cursor
*curs
,
78 struct smc_connection
*conn
)
80 #ifndef KERNEL_HAS_ATOMIC64
84 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
86 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
89 return atomic64_read(&curs
->acurs
);
93 static inline u64
smc_curs_read_net(union smc_cdc_cursor
*curs
,
94 struct smc_connection
*conn
)
96 #ifndef KERNEL_HAS_ATOMIC64
100 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
102 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
105 return atomic64_read(&curs
->acurs
);
109 static inline void smc_curs_write(union smc_host_cursor
*curs
, u64 val
,
110 struct smc_connection
*conn
)
112 #ifndef KERNEL_HAS_ATOMIC64
115 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
117 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
119 atomic64_set(&curs
->acurs
, val
);
123 static inline void smc_curs_write_net(union smc_cdc_cursor
*curs
, u64 val
,
124 struct smc_connection
*conn
)
126 #ifndef KERNEL_HAS_ATOMIC64
129 spin_lock_irqsave(&conn
->acurs_lock
, flags
);
131 spin_unlock_irqrestore(&conn
->acurs_lock
, flags
);
133 atomic64_set(&curs
->acurs
, val
);
137 /* calculate cursor difference between old and new, where old <= new */
138 static inline int smc_curs_diff(unsigned int size
,
139 union smc_host_cursor
*old
,
140 union smc_host_cursor
*new)
142 if (old
->wrap
!= new->wrap
)
144 ((size
- old
->count
) + new->count
));
146 return max_t(int, 0, (new->count
- old
->count
));
149 static inline void smc_host_cursor_to_cdc(union smc_cdc_cursor
*peer
,
150 union smc_host_cursor
*local
,
151 struct smc_connection
*conn
)
153 union smc_host_cursor temp
;
155 smc_curs_write(&temp
, smc_curs_read(local
, conn
), conn
);
156 peer
->count
= htonl(temp
.count
);
157 peer
->wrap
= htons(temp
.wrap
);
158 /* peer->reserved = htons(0); must be ensured by caller */
161 static inline void smc_host_msg_to_cdc(struct smc_cdc_msg
*peer
,
162 struct smc_host_cdc_msg
*local
,
163 struct smc_connection
*conn
)
165 peer
->common
.type
= local
->common
.type
;
166 peer
->len
= local
->len
;
167 peer
->seqno
= htons(local
->seqno
);
168 peer
->token
= htonl(local
->token
);
169 smc_host_cursor_to_cdc(&peer
->prod
, &local
->prod
, conn
);
170 smc_host_cursor_to_cdc(&peer
->cons
, &local
->cons
, conn
);
171 peer
->prod_flags
= local
->prod_flags
;
172 peer
->conn_state_flags
= local
->conn_state_flags
;
175 static inline void smc_cdc_cursor_to_host(union smc_host_cursor
*local
,
176 union smc_cdc_cursor
*peer
,
177 struct smc_connection
*conn
)
179 union smc_host_cursor temp
, old
;
180 union smc_cdc_cursor net
;
182 smc_curs_write(&old
, smc_curs_read(local
, conn
), conn
);
183 smc_curs_write_net(&net
, smc_curs_read_net(peer
, conn
), conn
);
184 temp
.count
= ntohl(net
.count
);
185 temp
.wrap
= ntohs(net
.wrap
);
186 if ((old
.wrap
> temp
.wrap
) && temp
.wrap
)
188 if ((old
.wrap
== temp
.wrap
) &&
189 (old
.count
> temp
.count
))
191 smc_curs_write(local
, smc_curs_read(&temp
, conn
), conn
);
194 static inline void smc_cdc_msg_to_host(struct smc_host_cdc_msg
*local
,
195 struct smc_cdc_msg
*peer
,
196 struct smc_connection
*conn
)
198 local
->common
.type
= peer
->common
.type
;
199 local
->len
= peer
->len
;
200 local
->seqno
= ntohs(peer
->seqno
);
201 local
->token
= ntohl(peer
->token
);
202 smc_cdc_cursor_to_host(&local
->prod
, &peer
->prod
, conn
);
203 smc_cdc_cursor_to_host(&local
->cons
, &peer
->cons
, conn
);
204 local
->prod_flags
= peer
->prod_flags
;
205 local
->conn_state_flags
= peer
->conn_state_flags
;
208 struct smc_cdc_tx_pend
;
210 int smc_cdc_get_free_slot(struct smc_connection
*conn
,
211 struct smc_wr_buf
**wr_buf
,
212 struct smc_cdc_tx_pend
**pend
);
213 void smc_cdc_tx_dismiss_slots(struct smc_connection
*conn
);
214 int smc_cdc_msg_send(struct smc_connection
*conn
, struct smc_wr_buf
*wr_buf
,
215 struct smc_cdc_tx_pend
*pend
);
216 int smc_cdc_get_slot_and_msg_send(struct smc_connection
*conn
);
217 int smc_cdc_init(void) __init
;
219 #endif /* SMC_CDC_H */