Merge tag 'for-linus-4.11-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / net / smc / smc_cdc.c
1 /*
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"
17 #include "smc_tx.h"
18 #include "smc_rx.h"
19 #include "smc_close.h"
20
21 /********************************** send *************************************/
22
23 struct 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 */
31 static 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         }
58         smc_tx_sndbuf_nonfull(smc);
59         if (smc->sk.sk_state != SMC_ACTIVE)
60                 /* wake up smc_close_wait_tx_pends() */
61                 smc->sk.sk_state_change(&smc->sk);
62         bh_unlock_sock(&smc->sk);
63 }
64
65 int 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
73 static 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
91 int 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
115 int 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
129 static 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
139 static 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
147 void 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
156 bool 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
164 /********************************* receive ***********************************/
165
166 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
167 {
168         return (s16)(seq1 - seq2) < 0;
169 }
170
171 static 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();
213                 smc->sk.sk_data_ready(&smc->sk);
214         }
215
216         if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
217                 smc->sk.sk_err = ECONNRESET;
218                 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
219         }
220         if (smc_cdc_rxed_any_close_or_senddone(conn))
221                 smc_close_passive_received(smc);
222
223         /* piggy backed tx info */
224         /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
225         if (diff_cons && smc_tx_prepared_sends(conn)) {
226                 smc_tx_sndbuf_nonempty(conn);
227                 /* trigger socket release if connection closed */
228                 smc_close_wake_tx_prepared(smc);
229         }
230
231         /* subsequent patch: trigger socket release if connection closed */
232
233         /* socket connected but not accepted */
234         if (!smc->sk.sk_socket)
235                 return;
236
237         /* data available */
238         if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
239             (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
240                 smc_tx_consumer_update(conn);
241 }
242
243 /* called under tasklet context */
244 static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
245                                     struct smc_link *link, u64 wr_id)
246 {
247         struct smc_link_group *lgr = container_of(link, struct smc_link_group,
248                                                   lnk[SMC_SINGLE_LINK]);
249         struct smc_connection *connection;
250         struct smc_sock *smc;
251
252         /* lookup connection */
253         read_lock_bh(&lgr->conns_lock);
254         connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
255         if (!connection) {
256                 read_unlock_bh(&lgr->conns_lock);
257                 return;
258         }
259         smc = container_of(connection, struct smc_sock, conn);
260         sock_hold(&smc->sk);
261         read_unlock_bh(&lgr->conns_lock);
262         bh_lock_sock(&smc->sk);
263         smc_cdc_msg_recv_action(smc, link, cdc);
264         bh_unlock_sock(&smc->sk);
265         sock_put(&smc->sk); /* no free sk in softirq-context */
266 }
267
268 /***************************** init, exit, misc ******************************/
269
270 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
271 {
272         struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
273         struct smc_cdc_msg *cdc = buf;
274
275         if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
276                 return; /* short message */
277         if (cdc->len != sizeof(*cdc))
278                 return; /* invalid message */
279         smc_cdc_msg_recv(cdc, link, wc->wr_id);
280 }
281
282 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
283         {
284                 .handler        = smc_cdc_rx_handler,
285                 .type           = SMC_CDC_MSG_TYPE
286         },
287         {
288                 .handler        = NULL,
289         }
290 };
291
292 int __init smc_cdc_init(void)
293 {
294         struct smc_wr_rx_handler *handler;
295         int rc = 0;
296
297         for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
298                 INIT_HLIST_NODE(&handler->list);
299                 rc = smc_wr_rx_register_handler(handler);
300                 if (rc)
301                         break;
302         }
303         return rc;
304 }