Bluetooth: Properly check L2CAP config option output buffer length
[linux/fpc-iii.git] / net / smc / smc_tx.c
blob3c656beb8820cb16a28f379ee32f2f39a8ce6631
1 /*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
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.
10 * Copyright IBM Corp. 2016
12 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
15 #include <linux/net.h>
16 #include <linux/rcupdate.h>
17 #include <linux/workqueue.h>
18 #include <linux/sched/signal.h>
20 #include <net/sock.h>
22 #include "smc.h"
23 #include "smc_wr.h"
24 #include "smc_cdc.h"
25 #include "smc_tx.h"
27 /***************************** sndbuf producer *******************************/
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.
33 static void smc_tx_write_space(struct sock *sk)
35 struct socket *sock = sk->sk_socket;
36 struct smc_sock *smc = smc_sk(sk);
37 struct socket_wq *wq;
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();
54 /* Wakeup sndbuf producers that blocked with smc_tx_wait_memory().
55 * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
57 void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
59 if (smc->sk.sk_socket &&
60 test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
61 smc->sk.sk_write_space(&smc->sk);
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)
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;
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;
86 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
87 rc = -ECONNRESET;
88 break;
90 if (!timeo) {
91 if (noblock)
92 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
93 rc = -EAGAIN;
94 break;
96 if (signal_pending(current)) {
97 rc = sock_intr_errno(timeo);
98 break;
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--;
113 remove_wait_queue(sk_sleep(sk), &wait);
114 return rc;
117 /* sndbuf producer: main API called by socket layer.
118 * called under sock lock.
120 int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
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;
132 /* This should be in poll */
133 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
135 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
136 rc = -EPIPE;
137 goto out_err;
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;
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;
157 continue;
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;
187 send_done += chunk_len;
188 send_remaining -= chunk_len;
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 */
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
211 smc_tx_sndbuf_nonempty(conn);
212 } /* while (msg_data_left(msg)) */
214 return send_done;
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;
224 /***************************** sndbuf consumer *******************************/
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[])
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;
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;
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)
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);
271 /* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
272 * usable snd_wnd as max transmit
274 static int smc_tx_rdma_writes(struct smc_connection *conn)
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;
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;
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);
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);
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
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
328 dst_len = len;
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;
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;
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;
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 */
384 return 0;
387 /* Wakeup sndbuf consumers from any context (IRQ or process)
388 * since there is more data to transmit; usable snd_wnd as max transmit
390 int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
392 struct smc_cdc_tx_pend *pend;
393 struct smc_wr_buf *wr_buf;
394 int rc;
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);
404 if (smc->sk.sk_err == ECONNABORTED) {
405 rc = sock_error(&smc->sk);
406 goto out_unlock;
408 rc = 0;
409 schedule_work(&conn->tx_work);
411 goto out_unlock;
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;
421 rc = smc_cdc_msg_send(conn, wr_buf, pend);
423 out_unlock:
424 spin_unlock_bh(&conn->send_lock);
425 return rc;
428 /* Wakeup sndbuf consumers from process context
429 * since there is more data to transmit
431 static void smc_tx_work(struct work_struct *work)
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;
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);
447 void smc_tx_consumer_update(struct smc_connection *conn)
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;
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);
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;
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;
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;
484 /***************************** send initialize *******************************/
486 /* Initialize send properties on connection establishment. NB: not __init! */
487 void smc_tx_init(struct smc_sock *smc)
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);