Input: wm97xx: add new AC97 bus support
[sfrench/cifs-2.6.git] / net / smc / smc_tx.c
1 /*
2  * Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  * Manage send buffer.
5  * Producer:
6  * Copy user space data into send buffer, if send buffer space available.
7  * Consumer:
8  * Trigger RDMA write into RMBE of peer and send CDC, if RMBE space available.
9  *
10  * Copyright IBM Corp. 2016
11  *
12  * Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
13  */
14
15 #include <linux/net.h>
16 #include <linux/rcupdate.h>
17 #include <linux/workqueue.h>
18 #include <linux/sched/signal.h>
19
20 #include <net/sock.h>
21
22 #include "smc.h"
23 #include "smc_wr.h"
24 #include "smc_cdc.h"
25 #include "smc_tx.h"
26
27 /***************************** sndbuf producer *******************************/
28
29 /* callback implementation for sk.sk_write_space()
30  * to wakeup sndbuf producers that blocked with smc_tx_wait_memory().
31  * called under sk_socket lock.
32  */
33 static void smc_tx_write_space(struct sock *sk)
34 {
35         struct socket *sock = sk->sk_socket;
36         struct smc_sock *smc = smc_sk(sk);
37         struct socket_wq *wq;
38
39         /* similar to sk_stream_write_space */
40         if (atomic_read(&smc->conn.sndbuf_space) && sock) {
41                 clear_bit(SOCK_NOSPACE, &sock->flags);
42                 rcu_read_lock();
43                 wq = rcu_dereference(sk->sk_wq);
44                 if (skwq_has_sleeper(wq))
45                         wake_up_interruptible_poll(&wq->wait,
46                                                    POLLOUT | POLLWRNORM |
47                                                    POLLWRBAND);
48                 if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
49                         sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
50                 rcu_read_unlock();
51         }
52 }
53
54 /* Wakeup sndbuf producers that blocked with smc_tx_wait_memory().
55  * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
56  */
57 void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
58 {
59         if (smc->sk.sk_socket &&
60             test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
61                 smc->sk.sk_write_space(&smc->sk);
62 }
63
64 /* blocks sndbuf producer until at least one byte of free space available */
65 static int smc_tx_wait_memory(struct smc_sock *smc, int flags)
66 {
67         DEFINE_WAIT_FUNC(wait, woken_wake_function);
68         struct smc_connection *conn = &smc->conn;
69         struct sock *sk = &smc->sk;
70         bool noblock;
71         long timeo;
72         int rc = 0;
73
74         /* similar to sk_stream_wait_memory */
75         timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
76         noblock = timeo ? false : true;
77         add_wait_queue(sk_sleep(sk), &wait);
78         while (1) {
79                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
80                 if (sk->sk_err ||
81                     (sk->sk_shutdown & SEND_SHUTDOWN) ||
82                     conn->local_tx_ctrl.conn_state_flags.peer_done_writing) {
83                         rc = -EPIPE;
84                         break;
85                 }
86                 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
87                         rc = -ECONNRESET;
88                         break;
89                 }
90                 if (!timeo) {
91                         if (noblock)
92                                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
93                         rc = -EAGAIN;
94                         break;
95                 }
96                 if (signal_pending(current)) {
97                         rc = sock_intr_errno(timeo);
98                         break;
99                 }
100                 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
101                 if (atomic_read(&conn->sndbuf_space))
102                         break; /* at least 1 byte of free space available */
103                 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
104                 sk->sk_write_pending++;
105                 sk_wait_event(sk, &timeo,
106                               sk->sk_err ||
107                               (sk->sk_shutdown & SEND_SHUTDOWN) ||
108                               smc_cdc_rxed_any_close_or_senddone(conn) ||
109                               atomic_read(&conn->sndbuf_space),
110                               &wait);
111                 sk->sk_write_pending--;
112         }
113         remove_wait_queue(sk_sleep(sk), &wait);
114         return rc;
115 }
116
117 /* sndbuf producer: main API called by socket layer.
118  * called under sock lock.
119  */
120 int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
121 {
122         size_t copylen, send_done = 0, send_remaining = len;
123         size_t chunk_len, chunk_off, chunk_len_sum;
124         struct smc_connection *conn = &smc->conn;
125         union smc_host_cursor prep;
126         struct sock *sk = &smc->sk;
127         char *sndbuf_base;
128         int tx_cnt_prep;
129         int writespace;
130         int rc, chunk;
131
132         /* This should be in poll */
133         sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
134
135         if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
136                 rc = -EPIPE;
137                 goto out_err;
138         }
139
140         while (msg_data_left(msg)) {
141                 if (sk->sk_state == SMC_INIT)
142                         return -ENOTCONN;
143                 if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
144                     (smc->sk.sk_err == ECONNABORTED) ||
145                     conn->local_tx_ctrl.conn_state_flags.peer_conn_abort)
146                         return -EPIPE;
147                 if (smc_cdc_rxed_any_close(conn))
148                         return send_done ?: -ECONNRESET;
149
150                 if (!atomic_read(&conn->sndbuf_space)) {
151                         rc = smc_tx_wait_memory(smc, msg->msg_flags);
152                         if (rc) {
153                                 if (send_done)
154                                         return send_done;
155                                 goto out_err;
156                         }
157                         continue;
158                 }
159
160                 /* initialize variables for 1st iteration of subsequent loop */
161                 /* could be just 1 byte, even after smc_tx_wait_memory above */
162                 writespace = atomic_read(&conn->sndbuf_space);
163                 /* not more than what user space asked for */
164                 copylen = min_t(size_t, send_remaining, writespace);
165                 /* determine start of sndbuf */
166                 sndbuf_base = conn->sndbuf_desc->cpu_addr;
167                 smc_curs_write(&prep,
168                                smc_curs_read(&conn->tx_curs_prep, conn),
169                                conn);
170                 tx_cnt_prep = prep.count;
171                 /* determine chunks where to write into sndbuf */
172                 /* either unwrapped case, or 1st chunk of wrapped case */
173                 chunk_len = min_t(size_t,
174                                   copylen, conn->sndbuf_size - tx_cnt_prep);
175                 chunk_len_sum = chunk_len;
176                 chunk_off = tx_cnt_prep;
177                 smc_sndbuf_sync_sg_for_cpu(conn);
178                 for (chunk = 0; chunk < 2; chunk++) {
179                         rc = memcpy_from_msg(sndbuf_base + chunk_off,
180                                              msg, chunk_len);
181                         if (rc) {
182                                 smc_sndbuf_sync_sg_for_device(conn);
183                                 if (send_done)
184                                         return send_done;
185                                 goto out_err;
186                         }
187                         send_done += chunk_len;
188                         send_remaining -= chunk_len;
189
190                         if (chunk_len_sum == copylen)
191                                 break; /* either on 1st or 2nd iteration */
192                         /* prepare next (== 2nd) iteration */
193                         chunk_len = copylen - chunk_len; /* remainder */
194                         chunk_len_sum += chunk_len;
195                         chunk_off = 0; /* modulo offset in send ring buffer */
196                 }
197                 smc_sndbuf_sync_sg_for_device(conn);
198                 /* update cursors */
199                 smc_curs_add(conn->sndbuf_size, &prep, copylen);
200                 smc_curs_write(&conn->tx_curs_prep,
201                                smc_curs_read(&prep, conn),
202                                conn);
203                 /* increased in send tasklet smc_cdc_tx_handler() */
204                 smp_mb__before_atomic();
205                 atomic_sub(copylen, &conn->sndbuf_space);
206                 /* guarantee 0 <= sndbuf_space <= sndbuf_size */
207                 smp_mb__after_atomic();
208                 /* since we just produced more new data into sndbuf,
209                  * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
210                  */
211                 smc_tx_sndbuf_nonempty(conn);
212         } /* while (msg_data_left(msg)) */
213
214         return send_done;
215
216 out_err:
217         rc = sk_stream_error(sk, msg->msg_flags, rc);
218         /* make sure we wake any epoll edge trigger waiter */
219         if (unlikely(rc == -EAGAIN))
220                 sk->sk_write_space(sk);
221         return rc;
222 }
223
224 /***************************** sndbuf consumer *******************************/
225
226 /* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
227 static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
228                              int num_sges, struct ib_sge sges[])
229 {
230         struct smc_link_group *lgr = conn->lgr;
231         struct ib_send_wr *failed_wr = NULL;
232         struct ib_rdma_wr rdma_wr;
233         struct smc_link *link;
234         int rc;
235
236         memset(&rdma_wr, 0, sizeof(rdma_wr));
237         link = &lgr->lnk[SMC_SINGLE_LINK];
238         rdma_wr.wr.wr_id = smc_wr_tx_get_next_wr_id(link);
239         rdma_wr.wr.sg_list = sges;
240         rdma_wr.wr.num_sge = num_sges;
241         rdma_wr.wr.opcode = IB_WR_RDMA_WRITE;
242         rdma_wr.remote_addr =
243                 lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr +
244                 /* RMBE within RMB */
245                 ((conn->peer_conn_idx - 1) * conn->peer_rmbe_size) +
246                 /* offset within RMBE */
247                 peer_rmbe_offset;
248         rdma_wr.rkey = lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey;
249         rc = ib_post_send(link->roce_qp, &rdma_wr.wr, &failed_wr);
250         if (rc)
251                 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
252         return rc;
253 }
254
255 /* sndbuf consumer */
256 static inline void smc_tx_advance_cursors(struct smc_connection *conn,
257                                           union smc_host_cursor *prod,
258                                           union smc_host_cursor *sent,
259                                           size_t len)
260 {
261         smc_curs_add(conn->peer_rmbe_size, prod, len);
262         /* increased in recv tasklet smc_cdc_msg_rcv() */
263         smp_mb__before_atomic();
264         /* data in flight reduces usable snd_wnd */
265         atomic_sub(len, &conn->peer_rmbe_space);
266         /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
267         smp_mb__after_atomic();
268         smc_curs_add(conn->sndbuf_size, sent, len);
269 }
270
271 /* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
272  * usable snd_wnd as max transmit
273  */
274 static int smc_tx_rdma_writes(struct smc_connection *conn)
275 {
276         size_t src_off, src_len, dst_off, dst_len; /* current chunk values */
277         size_t len, dst_len_sum, src_len_sum, dstchunk, srcchunk;
278         union smc_host_cursor sent, prep, prod, cons;
279         struct ib_sge sges[SMC_IB_MAX_SEND_SGE];
280         struct smc_link_group *lgr = conn->lgr;
281         int to_send, rmbespace;
282         struct smc_link *link;
283         dma_addr_t dma_addr;
284         int num_sges;
285         int rc;
286
287         /* source: sndbuf */
288         smc_curs_write(&sent, smc_curs_read(&conn->tx_curs_sent, conn), conn);
289         smc_curs_write(&prep, smc_curs_read(&conn->tx_curs_prep, conn), conn);
290         /* cf. wmem_alloc - (snd_max - snd_una) */
291         to_send = smc_curs_diff(conn->sndbuf_size, &sent, &prep);
292         if (to_send <= 0)
293                 return 0;
294
295         /* destination: RMBE */
296         /* cf. snd_wnd */
297         rmbespace = atomic_read(&conn->peer_rmbe_space);
298         if (rmbespace <= 0)
299                 return 0;
300         smc_curs_write(&prod,
301                        smc_curs_read(&conn->local_tx_ctrl.prod, conn),
302                        conn);
303         smc_curs_write(&cons,
304                        smc_curs_read(&conn->local_rx_ctrl.cons, conn),
305                        conn);
306
307         /* if usable snd_wnd closes ask peer to advertise once it opens again */
308         conn->local_tx_ctrl.prod_flags.write_blocked = (to_send >= rmbespace);
309         /* cf. usable snd_wnd */
310         len = min(to_send, rmbespace);
311
312         /* initialize variables for first iteration of subsequent nested loop */
313         link = &lgr->lnk[SMC_SINGLE_LINK];
314         dst_off = prod.count;
315         if (prod.wrap == cons.wrap) {
316                 /* the filled destination area is unwrapped,
317                  * hence the available free destination space is wrapped
318                  * and we need 2 destination chunks of sum len; start with 1st
319                  * which is limited by what's available in sndbuf
320                  */
321                 dst_len = min_t(size_t,
322                                 conn->peer_rmbe_size - prod.count, len);
323         } else {
324                 /* the filled destination area is wrapped,
325                  * hence the available free destination space is unwrapped
326                  * and we need a single destination chunk of entire len
327                  */
328                 dst_len = len;
329         }
330         dst_len_sum = dst_len;
331         src_off = sent.count;
332         /* dst_len determines the maximum src_len */
333         if (sent.count + dst_len <= conn->sndbuf_size) {
334                 /* unwrapped src case: single chunk of entire dst_len */
335                 src_len = dst_len;
336         } else {
337                 /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
338                 src_len = conn->sndbuf_size - sent.count;
339         }
340         src_len_sum = src_len;
341         dma_addr = sg_dma_address(conn->sndbuf_desc->sgt[SMC_SINGLE_LINK].sgl);
342         for (dstchunk = 0; dstchunk < 2; dstchunk++) {
343                 num_sges = 0;
344                 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
345                         sges[srcchunk].addr = dma_addr + src_off;
346                         sges[srcchunk].length = src_len;
347                         sges[srcchunk].lkey = link->roce_pd->local_dma_lkey;
348                         num_sges++;
349                         src_off += src_len;
350                         if (src_off >= conn->sndbuf_size)
351                                 src_off -= conn->sndbuf_size;
352                                                 /* modulo in send ring */
353                         if (src_len_sum == dst_len)
354                                 break; /* either on 1st or 2nd iteration */
355                         /* prepare next (== 2nd) iteration */
356                         src_len = dst_len - src_len; /* remainder */
357                         src_len_sum += src_len;
358                 }
359                 rc = smc_tx_rdma_write(conn, dst_off, num_sges, sges);
360                 if (rc)
361                         return rc;
362                 if (dst_len_sum == len)
363                         break; /* either on 1st or 2nd iteration */
364                 /* prepare next (== 2nd) iteration */
365                 dst_off = 0; /* modulo offset in RMBE ring buffer */
366                 dst_len = len - dst_len; /* remainder */
367                 dst_len_sum += dst_len;
368                 src_len = min_t(int,
369                                 dst_len, conn->sndbuf_size - sent.count);
370                 src_len_sum = src_len;
371         }
372
373         smc_tx_advance_cursors(conn, &prod, &sent, len);
374         /* update connection's cursors with advanced local cursors */
375         smc_curs_write(&conn->local_tx_ctrl.prod,
376                        smc_curs_read(&prod, conn),
377                        conn);
378                                                         /* dst: peer RMBE */
379         smc_curs_write(&conn->tx_curs_sent,
380                        smc_curs_read(&sent, conn),
381                        conn);
382                                                         /* src: local sndbuf */
383
384         return 0;
385 }
386
387 /* Wakeup sndbuf consumers from any context (IRQ or process)
388  * since there is more data to transmit; usable snd_wnd as max transmit
389  */
390 int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
391 {
392         struct smc_cdc_tx_pend *pend;
393         struct smc_wr_buf *wr_buf;
394         int rc;
395
396         spin_lock_bh(&conn->send_lock);
397         rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
398                                    &pend);
399         if (rc < 0) {
400                 if (rc == -EBUSY) {
401                         struct smc_sock *smc =
402                                 container_of(conn, struct smc_sock, conn);
403
404                         if (smc->sk.sk_err == ECONNABORTED) {
405                                 rc = sock_error(&smc->sk);
406                                 goto out_unlock;
407                         }
408                         rc = 0;
409                         schedule_work(&conn->tx_work);
410                 }
411                 goto out_unlock;
412         }
413
414         rc = smc_tx_rdma_writes(conn);
415         if (rc) {
416                 smc_wr_tx_put_slot(&conn->lgr->lnk[SMC_SINGLE_LINK],
417                                    (struct smc_wr_tx_pend_priv *)pend);
418                 goto out_unlock;
419         }
420
421         rc = smc_cdc_msg_send(conn, wr_buf, pend);
422
423 out_unlock:
424         spin_unlock_bh(&conn->send_lock);
425         return rc;
426 }
427
428 /* Wakeup sndbuf consumers from process context
429  * since there is more data to transmit
430  */
431 static void smc_tx_work(struct work_struct *work)
432 {
433         struct smc_connection *conn = container_of(work,
434                                                    struct smc_connection,
435                                                    tx_work);
436         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
437         int rc;
438
439         lock_sock(&smc->sk);
440         rc = smc_tx_sndbuf_nonempty(conn);
441         if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
442             !atomic_read(&conn->bytes_to_rcv))
443                 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
444         release_sock(&smc->sk);
445 }
446
447 void smc_tx_consumer_update(struct smc_connection *conn)
448 {
449         union smc_host_cursor cfed, cons;
450         struct smc_cdc_tx_pend *pend;
451         struct smc_wr_buf *wr_buf;
452         int to_confirm, rc;
453
454         smc_curs_write(&cons,
455                        smc_curs_read(&conn->local_tx_ctrl.cons, conn),
456                        conn);
457         smc_curs_write(&cfed,
458                        smc_curs_read(&conn->rx_curs_confirmed, conn),
459                        conn);
460         to_confirm = smc_curs_diff(conn->rmbe_size, &cfed, &cons);
461
462         if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
463             ((to_confirm > conn->rmbe_update_limit) &&
464              ((to_confirm > (conn->rmbe_size / 2)) ||
465               conn->local_rx_ctrl.prod_flags.write_blocked))) {
466                 rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK],
467                                            &wr_buf, &pend);
468                 if (!rc)
469                         rc = smc_cdc_msg_send(conn, wr_buf, pend);
470                 if (rc < 0) {
471                         schedule_work(&conn->tx_work);
472                         return;
473                 }
474                 smc_curs_write(&conn->rx_curs_confirmed,
475                                smc_curs_read(&conn->local_tx_ctrl.cons, conn),
476                                conn);
477                 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
478         }
479         if (conn->local_rx_ctrl.prod_flags.write_blocked &&
480             !atomic_read(&conn->bytes_to_rcv))
481                 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
482 }
483
484 /***************************** send initialize *******************************/
485
486 /* Initialize send properties on connection establishment. NB: not __init! */
487 void smc_tx_init(struct smc_sock *smc)
488 {
489         smc->sk.sk_write_space = smc_tx_write_space;
490         INIT_WORK(&smc->conn.tx_work, smc_tx_work);
491         spin_lock_init(&smc->conn.send_lock);
492 }