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_link
*link
,
67 struct smc_wr_buf
**wr_buf
,
68 struct smc_cdc_tx_pend
**pend
)
70 return smc_wr_tx_get_free_slot(link
, smc_cdc_tx_handler
, wr_buf
,
71 (struct smc_wr_tx_pend_priv
**)pend
);
74 static inline void smc_cdc_add_pending_send(struct smc_connection
*conn
,
75 struct smc_cdc_tx_pend
*pend
)
78 sizeof(struct smc_cdc_msg
) > SMC_WR_BUF_SIZE
,
79 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
81 offsetof(struct smc_cdc_msg
, reserved
) > SMC_WR_TX_SIZE
,
82 "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()");
84 sizeof(struct smc_cdc_tx_pend
) > SMC_WR_TX_PEND_PRIV_SIZE
,
85 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
87 pend
->cursor
= conn
->tx_curs_sent
;
88 pend
->p_cursor
= conn
->local_tx_ctrl
.prod
;
89 pend
->ctrl_seq
= conn
->tx_cdc_seq
;
92 int smc_cdc_msg_send(struct smc_connection
*conn
,
93 struct smc_wr_buf
*wr_buf
,
94 struct smc_cdc_tx_pend
*pend
)
96 struct smc_link
*link
;
99 link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
101 smc_cdc_add_pending_send(conn
, pend
);
104 conn
->local_tx_ctrl
.seqno
= conn
->tx_cdc_seq
;
105 smc_host_msg_to_cdc((struct smc_cdc_msg
*)wr_buf
,
106 &conn
->local_tx_ctrl
, conn
);
107 rc
= smc_wr_tx_send(link
, (struct smc_wr_tx_pend_priv
*)pend
);
109 smc_curs_write(&conn
->rx_curs_confirmed
,
110 smc_curs_read(&conn
->local_tx_ctrl
.cons
, conn
),
116 int smc_cdc_get_slot_and_msg_send(struct smc_connection
*conn
)
118 struct smc_cdc_tx_pend
*pend
;
119 struct smc_wr_buf
*wr_buf
;
122 rc
= smc_cdc_get_free_slot(&conn
->lgr
->lnk
[SMC_SINGLE_LINK
], &wr_buf
,
127 return smc_cdc_msg_send(conn
, wr_buf
, pend
);
130 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv
*tx_pend
,
133 struct smc_connection
*conn
= (struct smc_connection
*)data
;
134 struct smc_cdc_tx_pend
*cdc_pend
=
135 (struct smc_cdc_tx_pend
*)tx_pend
;
137 return cdc_pend
->conn
== conn
;
140 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv
*tx_pend
)
142 struct smc_cdc_tx_pend
*cdc_pend
=
143 (struct smc_cdc_tx_pend
*)tx_pend
;
145 cdc_pend
->conn
= NULL
;
148 void smc_cdc_tx_dismiss_slots(struct smc_connection
*conn
)
150 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
152 smc_wr_tx_dismiss_slots(link
, SMC_CDC_MSG_TYPE
,
153 smc_cdc_tx_filter
, smc_cdc_tx_dismisser
,
154 (unsigned long)conn
);
157 bool smc_cdc_tx_has_pending(struct smc_connection
*conn
)
159 struct smc_link
*link
= &conn
->lgr
->lnk
[SMC_SINGLE_LINK
];
161 return smc_wr_tx_has_pending(link
, SMC_CDC_MSG_TYPE
,
162 smc_cdc_tx_filter
, (unsigned long)conn
);
165 /********************************* receive ***********************************/
167 static inline bool smc_cdc_before(u16 seq1
, u16 seq2
)
169 return (s16
)(seq1
- seq2
) < 0;
172 static void smc_cdc_msg_recv_action(struct smc_sock
*smc
,
173 struct smc_link
*link
,
174 struct smc_cdc_msg
*cdc
)
176 union smc_host_cursor cons_old
, prod_old
;
177 struct smc_connection
*conn
= &smc
->conn
;
178 int diff_cons
, diff_prod
;
180 if (!cdc
->prod_flags
.failover_validation
) {
181 if (smc_cdc_before(ntohs(cdc
->seqno
),
182 conn
->local_rx_ctrl
.seqno
))
183 /* received seqno is old */
186 smc_curs_write(&prod_old
,
187 smc_curs_read(&conn
->local_rx_ctrl
.prod
, conn
),
189 smc_curs_write(&cons_old
,
190 smc_curs_read(&conn
->local_rx_ctrl
.cons
, conn
),
192 smc_cdc_msg_to_host(&conn
->local_rx_ctrl
, cdc
, conn
);
194 diff_cons
= smc_curs_diff(conn
->peer_rmbe_size
, &cons_old
,
195 &conn
->local_rx_ctrl
.cons
);
197 /* peer_rmbe_space is decreased during data transfer with RDMA
200 smp_mb__before_atomic();
201 atomic_add(diff_cons
, &conn
->peer_rmbe_space
);
202 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
203 smp_mb__after_atomic();
206 diff_prod
= smc_curs_diff(conn
->rmbe_size
, &prod_old
,
207 &conn
->local_rx_ctrl
.prod
);
209 /* bytes_to_rcv is decreased in smc_recvmsg */
210 smp_mb__before_atomic();
211 atomic_add(diff_prod
, &conn
->bytes_to_rcv
);
212 /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
213 smp_mb__after_atomic();
214 smc
->sk
.sk_data_ready(&smc
->sk
);
217 if (conn
->local_rx_ctrl
.conn_state_flags
.peer_conn_abort
) {
218 smc
->sk
.sk_err
= ECONNRESET
;
219 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
221 if (smc_cdc_rxed_any_close_or_senddone(conn
)) {
222 smc
->sk
.sk_shutdown
|= RCV_SHUTDOWN
;
223 if (smc
->clcsock
&& smc
->clcsock
->sk
)
224 smc
->clcsock
->sk
->sk_shutdown
|= RCV_SHUTDOWN
;
225 sock_set_flag(&smc
->sk
, SOCK_DONE
);
226 schedule_work(&conn
->close_work
);
229 /* piggy backed tx info */
230 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
231 if (diff_cons
&& smc_tx_prepared_sends(conn
)) {
232 smc_tx_sndbuf_nonempty(conn
);
233 /* trigger socket release if connection closed */
234 smc_close_wake_tx_prepared(smc
);
237 /* socket connected but not accepted */
238 if (!smc
->sk
.sk_socket
)
242 if ((conn
->local_rx_ctrl
.prod_flags
.write_blocked
) ||
243 (conn
->local_rx_ctrl
.prod_flags
.cons_curs_upd_req
))
244 smc_tx_consumer_update(conn
);
247 /* called under tasklet context */
248 static inline void smc_cdc_msg_recv(struct smc_cdc_msg
*cdc
,
249 struct smc_link
*link
, u64 wr_id
)
251 struct smc_link_group
*lgr
= container_of(link
, struct smc_link_group
,
252 lnk
[SMC_SINGLE_LINK
]);
253 struct smc_connection
*connection
;
254 struct smc_sock
*smc
;
256 /* lookup connection */
257 read_lock_bh(&lgr
->conns_lock
);
258 connection
= smc_lgr_find_conn(ntohl(cdc
->token
), lgr
);
260 read_unlock_bh(&lgr
->conns_lock
);
263 smc
= container_of(connection
, struct smc_sock
, conn
);
265 read_unlock_bh(&lgr
->conns_lock
);
266 bh_lock_sock(&smc
->sk
);
267 smc_cdc_msg_recv_action(smc
, link
, cdc
);
268 bh_unlock_sock(&smc
->sk
);
269 sock_put(&smc
->sk
); /* no free sk in softirq-context */
272 /***************************** init, exit, misc ******************************/
274 static void smc_cdc_rx_handler(struct ib_wc
*wc
, void *buf
)
276 struct smc_link
*link
= (struct smc_link
*)wc
->qp
->qp_context
;
277 struct smc_cdc_msg
*cdc
= buf
;
279 if (wc
->byte_len
< offsetof(struct smc_cdc_msg
, reserved
))
280 return; /* short message */
281 if (cdc
->len
!= sizeof(*cdc
))
282 return; /* invalid message */
283 smc_cdc_msg_recv(cdc
, link
, wc
->wr_id
);
286 static struct smc_wr_rx_handler smc_cdc_rx_handlers
[] = {
288 .handler
= smc_cdc_rx_handler
,
289 .type
= SMC_CDC_MSG_TYPE
296 int __init
smc_cdc_init(void)
298 struct smc_wr_rx_handler
*handler
;
301 for (handler
= smc_cdc_rx_handlers
; handler
->handler
; handler
++) {
302 INIT_HLIST_NODE(&handler
->list
);
303 rc
= smc_wr_rx_register_handler(handler
);