GNU Linux-libre 4.14.290-gnu1
[releases.git] / net / smc / smc_cdc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  * Connection Data Control (CDC)
6  * handles flow control
7  *
8  * Copyright IBM Corp. 2016
9  *
10  * Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
11  */
12
13 #include <linux/spinlock.h>
14
15 #include "smc.h"
16 #include "smc_wr.h"
17 #include "smc_cdc.h"
18 #include "smc_tx.h"
19 #include "smc_rx.h"
20 #include "smc_close.h"
21
22 /********************************** send *************************************/
23
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 # */
29 };
30
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)
35 {
36         struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
37         struct smc_sock *smc;
38         int diff;
39
40         if (!cdcpend->conn)
41                 /* already dismissed */
42                 return;
43
44         smc = container_of(cdcpend->conn, struct smc_sock, conn);
45         bh_lock_sock(&smc->sk);
46         if (!wc_status) {
47                 diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
48                                      &cdcpend->conn->tx_curs_fin,
49                                      &cdcpend->cursor);
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),
57                                cdcpend->conn);
58         }
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);
64 }
65
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)
69 {
70         return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
71                                        (struct smc_wr_tx_pend_priv **)pend);
72 }
73
74 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
75                                             struct smc_cdc_tx_pend *pend)
76 {
77         BUILD_BUG_ON_MSG(
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)");
80         BUILD_BUG_ON_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()");
83         BUILD_BUG_ON_MSG(
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)");
86         pend->conn = conn;
87         pend->cursor = conn->tx_curs_sent;
88         pend->p_cursor = conn->local_tx_ctrl.prod;
89         pend->ctrl_seq = conn->tx_cdc_seq;
90 }
91
92 int smc_cdc_msg_send(struct smc_connection *conn,
93                      struct smc_wr_buf *wr_buf,
94                      struct smc_cdc_tx_pend *pend)
95 {
96         struct smc_link *link;
97         int rc;
98
99         link = &conn->lgr->lnk[SMC_SINGLE_LINK];
100
101         smc_cdc_add_pending_send(conn, pend);
102
103         conn->tx_cdc_seq++;
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);
108         if (!rc)
109                 smc_curs_write(&conn->rx_curs_confirmed,
110                                smc_curs_read(&conn->local_tx_ctrl.cons, conn),
111                                conn);
112
113         return rc;
114 }
115
116 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
117 {
118         struct smc_cdc_tx_pend *pend;
119         struct smc_wr_buf *wr_buf;
120         int rc;
121
122         rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
123                                    &pend);
124         if (rc)
125                 return rc;
126
127         return smc_cdc_msg_send(conn, wr_buf, pend);
128 }
129
130 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
131                               unsigned long data)
132 {
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;
136
137         return cdc_pend->conn == conn;
138 }
139
140 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
141 {
142         struct smc_cdc_tx_pend *cdc_pend =
143                 (struct smc_cdc_tx_pend *)tx_pend;
144
145         cdc_pend->conn = NULL;
146 }
147
148 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
149 {
150         struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
151
152         smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
153                                 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
154                                 (unsigned long)conn);
155 }
156
157 bool smc_cdc_tx_has_pending(struct smc_connection *conn)
158 {
159         struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
160
161         return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
162                                      smc_cdc_tx_filter, (unsigned long)conn);
163 }
164
165 /********************************* receive ***********************************/
166
167 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
168 {
169         return (s16)(seq1 - seq2) < 0;
170 }
171
172 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
173                                     struct smc_link *link,
174                                     struct smc_cdc_msg *cdc)
175 {
176         union smc_host_cursor cons_old, prod_old;
177         struct smc_connection *conn = &smc->conn;
178         int diff_cons, diff_prod;
179
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 */
184                         return;
185         }
186         smc_curs_write(&prod_old,
187                        smc_curs_read(&conn->local_rx_ctrl.prod, conn),
188                        conn);
189         smc_curs_write(&cons_old,
190                        smc_curs_read(&conn->local_rx_ctrl.cons, conn),
191                        conn);
192         smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
193
194         diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
195                                   &conn->local_rx_ctrl.cons);
196         if (diff_cons) {
197                 /* peer_rmbe_space is decreased during data transfer with RDMA
198                  * write
199                  */
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();
204         }
205
206         diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
207                                   &conn->local_rx_ctrl.prod);
208         if (diff_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);
215         }
216
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;
220         }
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);
227         }
228
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);
235         }
236
237         /* socket connected but not accepted */
238         if (!smc->sk.sk_socket)
239                 return;
240
241         /* data available */
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);
245 }
246
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)
250 {
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;
255
256         /* lookup connection */
257         read_lock_bh(&lgr->conns_lock);
258         connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
259         if (!connection) {
260                 read_unlock_bh(&lgr->conns_lock);
261                 return;
262         }
263         smc = container_of(connection, struct smc_sock, conn);
264         sock_hold(&smc->sk);
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 */
270 }
271
272 /***************************** init, exit, misc ******************************/
273
274 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
275 {
276         struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
277         struct smc_cdc_msg *cdc = buf;
278
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);
284 }
285
286 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
287         {
288                 .handler        = smc_cdc_rx_handler,
289                 .type           = SMC_CDC_MSG_TYPE
290         },
291         {
292                 .handler        = NULL,
293         }
294 };
295
296 int __init smc_cdc_init(void)
297 {
298         struct smc_wr_rx_handler *handler;
299         int rc = 0;
300
301         for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
302                 INIT_HLIST_NODE(&handler->list);
303                 rc = smc_wr_rx_register_handler(handler);
304                 if (rc)
305                         break;
306         }
307         return rc;
308 }