1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Connection Data Control (CDC)
8 * Copyright IBM Corp. 2016
10 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
13 #include <linux/spinlock.h>
20 #include "smc_close.h"
22 /********************************** send *************************************/
24 struct smc_cdc_tx_pend
{
25 struct smc_connection
*conn
; /* socket connection */
26 union smc_host_cursor cursor
; /* tx sndbuf cursor sent */
27 union smc_host_cursor p_cursor
; /* rx RMBE cursor produced */
28 u16 ctrl_seq
; /* conn. tx sequence # */
31 /* handler for send/transmission completion of a CDC msg */
32 static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv
*pnd_snd
,
33 struct smc_link
*link
,
34 enum ib_wc_status wc_status
)
36 struct smc_cdc_tx_pend
*cdcpend
= (struct smc_cdc_tx_pend
*)pnd_snd
;
37 struct smc_connection
*conn
= cdcpend
->conn
;
42 /* already dismissed */
45 smc
= container_of(conn
, struct smc_sock
, conn
);
46 bh_lock_sock(&smc
->sk
);
48 diff
= smc_curs_diff(cdcpend
->conn
->sndbuf_desc
->len
,
49 &cdcpend
->conn
->tx_curs_fin
,
51 /* sndbuf_space is decreased in smc_sendmsg */
52 smp_mb__before_atomic();
53 atomic_add(diff
, &cdcpend
->conn
->sndbuf_space
);
54 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
55 smp_mb__after_atomic();
56 smc_curs_copy(&conn
->tx_curs_fin
, &cdcpend
->cursor
, conn
);
58 smc_tx_sndbuf_nonfull(smc
);
59 bh_unlock_sock(&smc
->sk
);
62 int smc_cdc_get_free_slot(struct smc_connection
*conn
,
63 struct smc_wr_buf
**wr_buf
,
64 struct smc_cdc_tx_pend
**pend
)
66 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
69 rc
= smc_wr_tx_get_free_slot(link
, smc_cdc_tx_handler
, wr_buf
,
70 (struct smc_wr_tx_pend_priv
**)pend
);
71 if (!conn
->alert_token_local
)
72 /* abnormal termination */
77 static inline void smc_cdc_add_pending_send(struct smc_connection
*conn
,
78 struct smc_cdc_tx_pend
*pend
)
81 sizeof(struct smc_cdc_msg
) > SMC_WR_BUF_SIZE
,
82 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
84 sizeof(struct smc_cdc_msg
) != SMC_WR_TX_SIZE
,
85 "must adapt SMC_WR_TX_SIZE to sizeof(struct smc_cdc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()");
87 sizeof(struct smc_cdc_tx_pend
) > SMC_WR_TX_PEND_PRIV_SIZE
,
88 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
90 pend
->cursor
= conn
->tx_curs_sent
;
91 pend
->p_cursor
= conn
->local_tx_ctrl
.prod
;
92 pend
->ctrl_seq
= conn
->tx_cdc_seq
;
95 int smc_cdc_msg_send(struct smc_connection
*conn
,
96 struct smc_wr_buf
*wr_buf
,
97 struct smc_cdc_tx_pend
*pend
)
99 struct smc_link
*link
;
102 link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
104 smc_cdc_add_pending_send(conn
, pend
);
107 conn
->local_tx_ctrl
.seqno
= conn
->tx_cdc_seq
;
108 smc_host_msg_to_cdc((struct smc_cdc_msg
*)wr_buf
,
109 &conn
->local_tx_ctrl
, conn
);
110 rc
= smc_wr_tx_send(link
, (struct smc_wr_tx_pend_priv
*)pend
);
112 smc_curs_copy(&conn
->rx_curs_confirmed
,
113 &conn
->local_tx_ctrl
.cons
, conn
);
118 static int smcr_cdc_get_slot_and_msg_send(struct smc_connection
*conn
)
120 struct smc_cdc_tx_pend
*pend
;
121 struct smc_wr_buf
*wr_buf
;
124 rc
= smc_cdc_get_free_slot(conn
, &wr_buf
, &pend
);
128 return smc_cdc_msg_send(conn
, wr_buf
, pend
);
131 int smc_cdc_get_slot_and_msg_send(struct smc_connection
*conn
)
135 if (conn
->lgr
->is_smcd
) {
136 spin_lock_bh(&conn
->send_lock
);
137 rc
= smcd_cdc_msg_send(conn
);
138 spin_unlock_bh(&conn
->send_lock
);
140 rc
= smcr_cdc_get_slot_and_msg_send(conn
);
146 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv
*tx_pend
,
149 struct smc_connection
*conn
= (struct smc_connection
*)data
;
150 struct smc_cdc_tx_pend
*cdc_pend
=
151 (struct smc_cdc_tx_pend
*)tx_pend
;
153 return cdc_pend
->conn
== conn
;
156 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv
*tx_pend
)
158 struct smc_cdc_tx_pend
*cdc_pend
=
159 (struct smc_cdc_tx_pend
*)tx_pend
;
161 cdc_pend
->conn
= NULL
;
164 void smc_cdc_tx_dismiss_slots(struct smc_connection
*conn
)
166 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
168 smc_wr_tx_dismiss_slots(link
, SMC_CDC_MSG_TYPE
,
169 smc_cdc_tx_filter
, smc_cdc_tx_dismisser
,
170 (unsigned long)conn
);
173 /* Send a SMC-D CDC header.
174 * This increments the free space available in our send buffer.
175 * Also update the confirmed receive buffer with what was sent to the peer.
177 int smcd_cdc_msg_send(struct smc_connection
*conn
)
179 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
180 struct smcd_cdc_msg cdc
;
183 memset(&cdc
, 0, sizeof(cdc
));
184 cdc
.common
.type
= SMC_CDC_MSG_TYPE
;
185 cdc
.prod_wrap
= conn
->local_tx_ctrl
.prod
.wrap
;
186 cdc
.prod_count
= conn
->local_tx_ctrl
.prod
.count
;
188 cdc
.cons_wrap
= conn
->local_tx_ctrl
.cons
.wrap
;
189 cdc
.cons_count
= conn
->local_tx_ctrl
.cons
.count
;
190 cdc
.prod_flags
= conn
->local_tx_ctrl
.prod_flags
;
191 cdc
.conn_state_flags
= conn
->local_tx_ctrl
.conn_state_flags
;
192 rc
= smcd_tx_ism_write(conn
, &cdc
, sizeof(cdc
), 0, 1);
195 smc_curs_copy(&conn
->rx_curs_confirmed
, &conn
->local_tx_ctrl
.cons
,
197 /* Calculate transmitted data and increment free send buffer space */
198 diff
= smc_curs_diff(conn
->sndbuf_desc
->len
, &conn
->tx_curs_fin
,
199 &conn
->tx_curs_sent
);
200 /* increased by confirmed number of bytes */
201 smp_mb__before_atomic();
202 atomic_add(diff
, &conn
->sndbuf_space
);
203 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
204 smp_mb__after_atomic();
205 smc_curs_copy(&conn
->tx_curs_fin
, &conn
->tx_curs_sent
, conn
);
207 smc_tx_sndbuf_nonfull(smc
);
211 /********************************* receive ***********************************/
213 static inline bool smc_cdc_before(u16 seq1
, u16 seq2
)
215 return (s16
)(seq1
- seq2
) < 0;
218 static void smc_cdc_handle_urg_data_arrival(struct smc_sock
*smc
,
221 struct smc_connection
*conn
= &smc
->conn
;
224 /* new data included urgent business */
225 smc_curs_copy(&conn
->urg_curs
, &conn
->local_rx_ctrl
.prod
, conn
);
226 conn
->urg_state
= SMC_URG_VALID
;
227 if (!sock_flag(&smc
->sk
, SOCK_URGINLINE
))
228 /* we'll skip the urgent byte, so don't account for it */
230 base
= (char *)conn
->rmb_desc
->cpu_addr
+ conn
->rx_off
;
231 if (conn
->urg_curs
.count
)
232 conn
->urg_rx_byte
= *(base
+ conn
->urg_curs
.count
- 1);
234 conn
->urg_rx_byte
= *(base
+ conn
->rmb_desc
->len
- 1);
235 sk_send_sigurg(&smc
->sk
);
238 static void smc_cdc_msg_recv_action(struct smc_sock
*smc
,
239 struct smc_cdc_msg
*cdc
)
241 union smc_host_cursor cons_old
, prod_old
;
242 struct smc_connection
*conn
= &smc
->conn
;
243 int diff_cons
, diff_prod
;
245 smc_curs_copy(&prod_old
, &conn
->local_rx_ctrl
.prod
, conn
);
246 smc_curs_copy(&cons_old
, &conn
->local_rx_ctrl
.cons
, conn
);
247 smc_cdc_msg_to_host(&conn
->local_rx_ctrl
, cdc
, conn
);
249 diff_cons
= smc_curs_diff(conn
->peer_rmbe_size
, &cons_old
,
250 &conn
->local_rx_ctrl
.cons
);
252 /* peer_rmbe_space is decreased during data transfer with RDMA
255 smp_mb__before_atomic();
256 atomic_add(diff_cons
, &conn
->peer_rmbe_space
);
257 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
258 smp_mb__after_atomic();
261 diff_prod
= smc_curs_diff(conn
->rmb_desc
->len
, &prod_old
,
262 &conn
->local_rx_ctrl
.prod
);
264 if (conn
->local_rx_ctrl
.prod_flags
.urg_data_present
)
265 smc_cdc_handle_urg_data_arrival(smc
, &diff_prod
);
266 /* bytes_to_rcv is decreased in smc_recvmsg */
267 smp_mb__before_atomic();
268 atomic_add(diff_prod
, &conn
->bytes_to_rcv
);
269 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
270 smp_mb__after_atomic();
271 smc
->sk
.sk_data_ready(&smc
->sk
);
273 if (conn
->local_rx_ctrl
.prod_flags
.write_blocked
||
274 conn
->local_rx_ctrl
.prod_flags
.cons_curs_upd_req
||
275 conn
->local_rx_ctrl
.prod_flags
.urg_data_pending
) {
276 if (conn
->local_rx_ctrl
.prod_flags
.urg_data_pending
)
277 conn
->urg_state
= SMC_URG_NOTYET
;
278 /* force immediate tx of current consumer cursor, but
279 * under send_lock to guarantee arrival in seqno-order
281 if (smc
->sk
.sk_state
!= SMC_INIT
)
282 smc_tx_sndbuf_nonempty(conn
);
286 /* piggy backed tx info */
287 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
288 if (diff_cons
&& smc_tx_prepared_sends(conn
)) {
289 smc_tx_sndbuf_nonempty(conn
);
290 /* trigger socket release if connection closed */
291 smc_close_wake_tx_prepared(smc
);
293 if (diff_cons
&& conn
->urg_tx_pend
&&
294 atomic_read(&conn
->peer_rmbe_space
) == conn
->peer_rmbe_size
) {
295 /* urg data confirmed by peer, indicate we're ready for more */
296 conn
->urg_tx_pend
= false;
297 smc
->sk
.sk_write_space(&smc
->sk
);
300 if (conn
->local_rx_ctrl
.conn_state_flags
.peer_conn_abort
) {
301 smc
->sk
.sk_err
= ECONNRESET
;
302 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
304 if (smc_cdc_rxed_any_close_or_senddone(conn
)) {
305 smc
->sk
.sk_shutdown
|= RCV_SHUTDOWN
;
306 if (smc
->clcsock
&& smc
->clcsock
->sk
)
307 smc
->clcsock
->sk
->sk_shutdown
|= RCV_SHUTDOWN
;
308 sock_set_flag(&smc
->sk
, SOCK_DONE
);
309 sock_hold(&smc
->sk
); /* sock_put in close_work */
310 if (!schedule_work(&conn
->close_work
))
315 /* called under tasklet context */
316 static void smc_cdc_msg_recv(struct smc_sock
*smc
, struct smc_cdc_msg
*cdc
)
319 bh_lock_sock(&smc
->sk
);
320 smc_cdc_msg_recv_action(smc
, cdc
);
321 bh_unlock_sock(&smc
->sk
);
322 sock_put(&smc
->sk
); /* no free sk in softirq-context */
325 /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
326 * handler to indicate update in the DMBE.
331 static void smcd_cdc_rx_tsklet(unsigned long data
)
333 struct smc_connection
*conn
= (struct smc_connection
*)data
;
334 struct smcd_cdc_msg cdc
;
335 struct smc_sock
*smc
;
340 memcpy(&cdc
, conn
->rmb_desc
->cpu_addr
, sizeof(cdc
));
341 smc
= container_of(conn
, struct smc_sock
, conn
);
342 smc_cdc_msg_recv(smc
, (struct smc_cdc_msg
*)&cdc
);
345 /* Initialize receive tasklet. Called from ISM device IRQ handler to start
348 void smcd_cdc_rx_init(struct smc_connection
*conn
)
350 tasklet_init(&conn
->rx_tsklet
, smcd_cdc_rx_tsklet
, (unsigned long)conn
);
353 /***************************** init, exit, misc ******************************/
355 static void smc_cdc_rx_handler(struct ib_wc
*wc
, void *buf
)
357 struct smc_link
*link
= (struct smc_link
*)wc
->qp
->qp_context
;
358 struct smc_cdc_msg
*cdc
= buf
;
359 struct smc_connection
*conn
;
360 struct smc_link_group
*lgr
;
361 struct smc_sock
*smc
;
363 if (wc
->byte_len
< offsetof(struct smc_cdc_msg
, reserved
))
364 return; /* short message */
365 if (cdc
->len
!= SMC_WR_TX_SIZE
)
366 return; /* invalid message */
368 /* lookup connection */
369 lgr
= smc_get_lgr(link
);
370 read_lock_bh(&lgr
->conns_lock
);
371 conn
= smc_lgr_find_conn(ntohl(cdc
->token
), lgr
);
372 read_unlock_bh(&lgr
->conns_lock
);
375 smc
= container_of(conn
, struct smc_sock
, conn
);
377 if (!cdc
->prod_flags
.failover_validation
) {
378 if (smc_cdc_before(ntohs(cdc
->seqno
),
379 conn
->local_rx_ctrl
.seqno
))
380 /* received seqno is old */
383 smc_cdc_msg_recv(smc
, cdc
);
386 static struct smc_wr_rx_handler smc_cdc_rx_handlers
[] = {
388 .handler
= smc_cdc_rx_handler
,
389 .type
= SMC_CDC_MSG_TYPE
396 int __init
smc_cdc_init(void)
398 struct smc_wr_rx_handler
*handler
;
401 for (handler
= smc_cdc_rx_handlers
; handler
->handler
; handler
++) {
402 INIT_HLIST_NODE(&handler
->list
);
403 rc
= smc_wr_rx_register_handler(handler
);