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
;
41 /* already dismissed */
44 smc
= container_of(cdcpend
->conn
, struct smc_sock
, conn
);
45 bh_lock_sock(&smc
->sk
);
47 diff
= smc_curs_diff(cdcpend
->conn
->sndbuf_size
,
48 &cdcpend
->conn
->tx_curs_fin
,
50 /* sndbuf_space is decreased in smc_sendmsg */
51 smp_mb__before_atomic();
52 atomic_add(diff
, &cdcpend
->conn
->sndbuf_space
);
53 /* guarantee 0 <= sndbuf_space <= sndbuf_size */
54 smp_mb__after_atomic();
55 smc_curs_write(&cdcpend
->conn
->tx_curs_fin
,
56 smc_curs_read(&cdcpend
->cursor
, cdcpend
->conn
),
59 smc_tx_sndbuf_nonfull(smc
);
60 if (smc
->sk
.sk_state
!= SMC_ACTIVE
)
61 /* wake up smc_close_wait_tx_pends() */
62 smc
->sk
.sk_state_change(&smc
->sk
);
63 bh_unlock_sock(&smc
->sk
);
66 int smc_cdc_get_free_slot(struct smc_connection
*conn
,
67 struct smc_wr_buf
**wr_buf
,
68 struct smc_cdc_tx_pend
**pend
)
70 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
72 return smc_wr_tx_get_free_slot(link
, smc_cdc_tx_handler
, wr_buf
,
73 (struct smc_wr_tx_pend_priv
**)pend
);
76 static inline void smc_cdc_add_pending_send(struct smc_connection
*conn
,
77 struct smc_cdc_tx_pend
*pend
)
80 sizeof(struct smc_cdc_msg
) > SMC_WR_BUF_SIZE
,
81 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
83 offsetof(struct smc_cdc_msg
, reserved
) > SMC_WR_TX_SIZE
,
84 "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()");
86 sizeof(struct smc_cdc_tx_pend
) > SMC_WR_TX_PEND_PRIV_SIZE
,
87 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
89 pend
->cursor
= conn
->tx_curs_sent
;
90 pend
->p_cursor
= conn
->local_tx_ctrl
.prod
;
91 pend
->ctrl_seq
= conn
->tx_cdc_seq
;
94 int smc_cdc_msg_send(struct smc_connection
*conn
,
95 struct smc_wr_buf
*wr_buf
,
96 struct smc_cdc_tx_pend
*pend
)
98 struct smc_link
*link
;
101 link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
103 smc_cdc_add_pending_send(conn
, pend
);
106 conn
->local_tx_ctrl
.seqno
= conn
->tx_cdc_seq
;
107 smc_host_msg_to_cdc((struct smc_cdc_msg
*)wr_buf
,
108 &conn
->local_tx_ctrl
, conn
);
109 rc
= smc_wr_tx_send(link
, (struct smc_wr_tx_pend_priv
*)pend
);
111 smc_curs_write(&conn
->rx_curs_confirmed
,
112 smc_curs_read(&conn
->local_tx_ctrl
.cons
, conn
),
118 int smc_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 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv
*tx_pend
,
134 struct smc_connection
*conn
= (struct smc_connection
*)data
;
135 struct smc_cdc_tx_pend
*cdc_pend
=
136 (struct smc_cdc_tx_pend
*)tx_pend
;
138 return cdc_pend
->conn
== conn
;
141 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv
*tx_pend
)
143 struct smc_cdc_tx_pend
*cdc_pend
=
144 (struct smc_cdc_tx_pend
*)tx_pend
;
146 cdc_pend
->conn
= NULL
;
149 void smc_cdc_tx_dismiss_slots(struct smc_connection
*conn
)
151 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
153 smc_wr_tx_dismiss_slots(link
, SMC_CDC_MSG_TYPE
,
154 smc_cdc_tx_filter
, smc_cdc_tx_dismisser
,
155 (unsigned long)conn
);
158 bool smc_cdc_tx_has_pending(struct smc_connection
*conn
)
160 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
162 return smc_wr_tx_has_pending(link
, SMC_CDC_MSG_TYPE
,
163 smc_cdc_tx_filter
, (unsigned long)conn
);
166 /********************************* receive ***********************************/
168 static inline bool smc_cdc_before(u16 seq1
, u16 seq2
)
170 return (s16
)(seq1
- seq2
) < 0;
173 static void smc_cdc_msg_recv_action(struct smc_sock
*smc
,
174 struct smc_link
*link
,
175 struct smc_cdc_msg
*cdc
)
177 union smc_host_cursor cons_old
, prod_old
;
178 struct smc_connection
*conn
= &smc
->conn
;
179 int diff_cons
, diff_prod
;
181 if (!cdc
->prod_flags
.failover_validation
) {
182 if (smc_cdc_before(ntohs(cdc
->seqno
),
183 conn
->local_rx_ctrl
.seqno
))
184 /* received seqno is old */
187 smc_curs_write(&prod_old
,
188 smc_curs_read(&conn
->local_rx_ctrl
.prod
, conn
),
190 smc_curs_write(&cons_old
,
191 smc_curs_read(&conn
->local_rx_ctrl
.cons
, conn
),
193 smc_cdc_msg_to_host(&conn
->local_rx_ctrl
, cdc
, conn
);
195 diff_cons
= smc_curs_diff(conn
->peer_rmbe_size
, &cons_old
,
196 &conn
->local_rx_ctrl
.cons
);
198 /* peer_rmbe_space is decreased during data transfer with RDMA
201 smp_mb__before_atomic();
202 atomic_add(diff_cons
, &conn
->peer_rmbe_space
);
203 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
204 smp_mb__after_atomic();
207 diff_prod
= smc_curs_diff(conn
->rmbe_size
, &prod_old
,
208 &conn
->local_rx_ctrl
.prod
);
210 /* bytes_to_rcv is decreased in smc_recvmsg */
211 smp_mb__before_atomic();
212 atomic_add(diff_prod
, &conn
->bytes_to_rcv
);
213 /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
214 smp_mb__after_atomic();
215 smc
->sk
.sk_data_ready(&smc
->sk
);
218 if (conn
->local_rx_ctrl
.conn_state_flags
.peer_conn_abort
) {
219 smc
->sk
.sk_err
= ECONNRESET
;
220 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
222 if (smc_cdc_rxed_any_close_or_senddone(conn
)) {
223 smc
->sk
.sk_shutdown
|= RCV_SHUTDOWN
;
224 if (smc
->clcsock
&& smc
->clcsock
->sk
)
225 smc
->clcsock
->sk
->sk_shutdown
|= RCV_SHUTDOWN
;
226 sock_set_flag(&smc
->sk
, SOCK_DONE
);
227 schedule_work(&conn
->close_work
);
230 /* piggy backed tx info */
231 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
232 if (diff_cons
&& smc_tx_prepared_sends(conn
)) {
233 smc_tx_sndbuf_nonempty(conn
);
234 /* trigger socket release if connection closed */
235 smc_close_wake_tx_prepared(smc
);
238 /* socket connected but not accepted */
239 if (!smc
->sk
.sk_socket
)
243 if ((conn
->local_rx_ctrl
.prod_flags
.write_blocked
) ||
244 (conn
->local_rx_ctrl
.prod_flags
.cons_curs_upd_req
))
245 smc_tx_consumer_update(conn
);
248 /* called under tasklet context */
249 static inline void smc_cdc_msg_recv(struct smc_cdc_msg
*cdc
,
250 struct smc_link
*link
, u64 wr_id
)
252 struct smc_link_group
*lgr
= container_of(link
, struct smc_link_group
,
253 lnk
[SMC_SINGLE_LINK
]);
254 struct smc_connection
*connection
;
255 struct smc_sock
*smc
;
257 /* lookup connection */
258 read_lock_bh(&lgr
->conns_lock
);
259 connection
= smc_lgr_find_conn(ntohl(cdc
->token
), lgr
);
261 read_unlock_bh(&lgr
->conns_lock
);
264 smc
= container_of(connection
, struct smc_sock
, conn
);
266 read_unlock_bh(&lgr
->conns_lock
);
267 bh_lock_sock(&smc
->sk
);
268 smc_cdc_msg_recv_action(smc
, link
, cdc
);
269 bh_unlock_sock(&smc
->sk
);
270 sock_put(&smc
->sk
); /* no free sk in softirq-context */
273 /***************************** init, exit, misc ******************************/
275 static void smc_cdc_rx_handler(struct ib_wc
*wc
, void *buf
)
277 struct smc_link
*link
= (struct smc_link
*)wc
->qp
->qp_context
;
278 struct smc_cdc_msg
*cdc
= buf
;
280 if (wc
->byte_len
< offsetof(struct smc_cdc_msg
, reserved
))
281 return; /* short message */
282 if (cdc
->len
!= sizeof(*cdc
))
283 return; /* invalid message */
284 smc_cdc_msg_recv(cdc
, link
, wc
->wr_id
);
287 static struct smc_wr_rx_handler smc_cdc_rx_handlers
[] = {
289 .handler
= smc_cdc_rx_handler
,
290 .type
= SMC_CDC_MSG_TYPE
297 int __init
smc_cdc_init(void)
299 struct smc_wr_rx_handler
*handler
;
302 for (handler
= smc_cdc_rx_handlers
; handler
->handler
; handler
++) {
303 INIT_HLIST_NODE(&handler
->list
);
304 rc
= smc_wr_rx_register_handler(handler
);