blob: 4c9c2f6cd021c13de24c671df41915cea8297893 [file] [log] [blame]
Ursula Braun5f083182017-01-09 16:55:22 +01001/*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * Connection Data Control (CDC)
5 * handles flow control
6 *
7 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
10 */
11
12#include <linux/spinlock.h>
13
14#include "smc.h"
15#include "smc_wr.h"
16#include "smc_cdc.h"
Ursula Braune6727f32017-01-09 16:55:23 +010017#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010018#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010019#include "smc_close.h"
Ursula Braun5f083182017-01-09 16:55:22 +010020
21/********************************** send *************************************/
22
23struct smc_cdc_tx_pend {
24 struct smc_connection *conn; /* socket connection */
25 union smc_host_cursor cursor; /* tx sndbuf cursor sent */
26 union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
27 u16 ctrl_seq; /* conn. tx sequence # */
28};
29
30/* handler for send/transmission completion of a CDC msg */
31static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
32 struct smc_link *link,
33 enum ib_wc_status wc_status)
34{
35 struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
36 struct smc_sock *smc;
37 int diff;
38
39 if (!cdcpend->conn)
40 /* already dismissed */
41 return;
42
43 smc = container_of(cdcpend->conn, struct smc_sock, conn);
44 bh_lock_sock(&smc->sk);
45 if (!wc_status) {
46 diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
47 &cdcpend->conn->tx_curs_fin,
48 &cdcpend->cursor);
49 /* sndbuf_space is decreased in smc_sendmsg */
50 smp_mb__before_atomic();
51 atomic_add(diff, &cdcpend->conn->sndbuf_space);
52 /* guarantee 0 <= sndbuf_space <= sndbuf_size */
53 smp_mb__after_atomic();
54 smc_curs_write(&cdcpend->conn->tx_curs_fin,
55 smc_curs_read(&cdcpend->cursor, cdcpend->conn),
56 cdcpend->conn);
57 }
Ursula Braune6727f32017-01-09 16:55:23 +010058 smc_tx_sndbuf_nonfull(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +010059 if (smc->sk.sk_state != SMC_ACTIVE)
60 /* wake up smc_close_wait_tx_pends() */
61 smc->sk.sk_state_change(&smc->sk);
Ursula Braun5f083182017-01-09 16:55:22 +010062 bh_unlock_sock(&smc->sk);
63}
64
65int smc_cdc_get_free_slot(struct smc_link *link,
66 struct smc_wr_buf **wr_buf,
67 struct smc_cdc_tx_pend **pend)
68{
69 return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
70 (struct smc_wr_tx_pend_priv **)pend);
71}
72
73static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
74 struct smc_cdc_tx_pend *pend)
75{
76 BUILD_BUG_ON_MSG(
77 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
78 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
79 BUILD_BUG_ON_MSG(
80 offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
81 "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()");
82 BUILD_BUG_ON_MSG(
83 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
84 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
85 pend->conn = conn;
86 pend->cursor = conn->tx_curs_sent;
87 pend->p_cursor = conn->local_tx_ctrl.prod;
88 pend->ctrl_seq = conn->tx_cdc_seq;
89}
90
91int smc_cdc_msg_send(struct smc_connection *conn,
92 struct smc_wr_buf *wr_buf,
93 struct smc_cdc_tx_pend *pend)
94{
95 struct smc_link *link;
96 int rc;
97
98 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
99
100 smc_cdc_add_pending_send(conn, pend);
101
102 conn->tx_cdc_seq++;
103 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
104 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
105 &conn->local_tx_ctrl, conn);
106 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
107 if (!rc)
108 smc_curs_write(&conn->rx_curs_confirmed,
109 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
110 conn);
111
112 return rc;
113}
114
115int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
116{
117 struct smc_cdc_tx_pend *pend;
118 struct smc_wr_buf *wr_buf;
119 int rc;
120
121 rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
122 &pend);
123 if (rc)
124 return rc;
125
126 return smc_cdc_msg_send(conn, wr_buf, pend);
127}
128
129static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
130 unsigned long data)
131{
132 struct smc_connection *conn = (struct smc_connection *)data;
133 struct smc_cdc_tx_pend *cdc_pend =
134 (struct smc_cdc_tx_pend *)tx_pend;
135
136 return cdc_pend->conn == conn;
137}
138
139static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
140{
141 struct smc_cdc_tx_pend *cdc_pend =
142 (struct smc_cdc_tx_pend *)tx_pend;
143
144 cdc_pend->conn = NULL;
145}
146
147void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
148{
149 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
150
151 smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
152 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
153 (unsigned long)conn);
154}
155
Ursula Braunb38d7322017-01-09 16:55:25 +0100156bool smc_cdc_tx_has_pending(struct smc_connection *conn)
157{
158 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
159
160 return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
161 smc_cdc_tx_filter, (unsigned long)conn);
162}
163
Ursula Braun5f083182017-01-09 16:55:22 +0100164/********************************* receive ***********************************/
165
166static inline bool smc_cdc_before(u16 seq1, u16 seq2)
167{
168 return (s16)(seq1 - seq2) < 0;
169}
170
171static void smc_cdc_msg_recv_action(struct smc_sock *smc,
172 struct smc_link *link,
173 struct smc_cdc_msg *cdc)
174{
175 union smc_host_cursor cons_old, prod_old;
176 struct smc_connection *conn = &smc->conn;
177 int diff_cons, diff_prod;
178
179 if (!cdc->prod_flags.failover_validation) {
180 if (smc_cdc_before(ntohs(cdc->seqno),
181 conn->local_rx_ctrl.seqno))
182 /* received seqno is old */
183 return;
184 }
185 smc_curs_write(&prod_old,
186 smc_curs_read(&conn->local_rx_ctrl.prod, conn),
187 conn);
188 smc_curs_write(&cons_old,
189 smc_curs_read(&conn->local_rx_ctrl.cons, conn),
190 conn);
191 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
192
193 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
194 &conn->local_rx_ctrl.cons);
195 if (diff_cons) {
196 /* peer_rmbe_space is decreased during data transfer with RDMA
197 * write
198 */
199 smp_mb__before_atomic();
200 atomic_add(diff_cons, &conn->peer_rmbe_space);
201 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
202 smp_mb__after_atomic();
203 }
204
205 diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
206 &conn->local_rx_ctrl.prod);
207 if (diff_prod) {
208 /* bytes_to_rcv is decreased in smc_recvmsg */
209 smp_mb__before_atomic();
210 atomic_add(diff_prod, &conn->bytes_to_rcv);
211 /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
212 smp_mb__after_atomic();
Ursula Braun952310c2017-01-09 16:55:24 +0100213 smc->sk.sk_data_ready(&smc->sk);
Ursula Braun5f083182017-01-09 16:55:22 +0100214 }
215
Ursula Braunb38d7322017-01-09 16:55:25 +0100216 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
Ursula Braun5f083182017-01-09 16:55:22 +0100217 smc->sk.sk_err = ECONNRESET;
Ursula Braunb38d7322017-01-09 16:55:25 +0100218 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
219 }
Ursula Braun5f083182017-01-09 16:55:22 +0100220 if (smc_cdc_rxed_any_close_or_senddone(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100221 smc_close_passive_received(smc);
Ursula Braun5f083182017-01-09 16:55:22 +0100222
223 /* piggy backed tx info */
Ursula Braune6727f32017-01-09 16:55:23 +0100224 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
Ursula Braunb38d7322017-01-09 16:55:25 +0100225 if (diff_cons && smc_tx_prepared_sends(conn)) {
Ursula Braune6727f32017-01-09 16:55:23 +0100226 smc_tx_sndbuf_nonempty(conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100227 /* trigger socket release if connection closed */
228 smc_close_wake_tx_prepared(smc);
229 }
Ursula Braun5f083182017-01-09 16:55:22 +0100230
Ursula Braun5f083182017-01-09 16:55:22 +0100231 /* socket connected but not accepted */
232 if (!smc->sk.sk_socket)
233 return;
234
235 /* data available */
Ursula Braun952310c2017-01-09 16:55:24 +0100236 if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
237 (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
238 smc_tx_consumer_update(conn);
Ursula Braun5f083182017-01-09 16:55:22 +0100239}
240
241/* called under tasklet context */
242static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
243 struct smc_link *link, u64 wr_id)
244{
245 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
246 lnk[SMC_SINGLE_LINK]);
247 struct smc_connection *connection;
248 struct smc_sock *smc;
249
250 /* lookup connection */
251 read_lock_bh(&lgr->conns_lock);
252 connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
253 if (!connection) {
254 read_unlock_bh(&lgr->conns_lock);
255 return;
256 }
257 smc = container_of(connection, struct smc_sock, conn);
258 sock_hold(&smc->sk);
259 read_unlock_bh(&lgr->conns_lock);
260 bh_lock_sock(&smc->sk);
261 smc_cdc_msg_recv_action(smc, link, cdc);
262 bh_unlock_sock(&smc->sk);
263 sock_put(&smc->sk); /* no free sk in softirq-context */
264}
265
266/***************************** init, exit, misc ******************************/
267
268static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
269{
270 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
271 struct smc_cdc_msg *cdc = buf;
272
273 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
274 return; /* short message */
275 if (cdc->len != sizeof(*cdc))
276 return; /* invalid message */
277 smc_cdc_msg_recv(cdc, link, wc->wr_id);
278}
279
280static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
281 {
282 .handler = smc_cdc_rx_handler,
283 .type = SMC_CDC_MSG_TYPE
284 },
285 {
286 .handler = NULL,
287 }
288};
289
290int __init smc_cdc_init(void)
291{
292 struct smc_wr_rx_handler *handler;
293 int rc = 0;
294
295 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
296 INIT_HLIST_NODE(&handler->list);
297 rc = smc_wr_rx_register_handler(handler);
298 if (rc)
299 break;
300 }
301 return rc;
302}