]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/tom/t4_ddp.c
cxgbe/tom: use vmem(9) as the DDP page pod allocator.
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / tom / t4_ddp.c
1 /*-
2  * Copyright (c) 2012 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_inet.h"
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/ktr.h>
38 #include <sys/module.h>
39 #include <sys/protosw.h>
40 #include <sys/proc.h>
41 #include <sys/domain.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/uio.h>
45 #include <netinet/in.h>
46 #include <netinet/in_pcb.h>
47 #include <netinet/ip.h>
48 #include <netinet/tcp_var.h>
49 #define TCPSTATES
50 #include <netinet/tcp_fsm.h>
51 #include <netinet/toecore.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_param.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_object.h>
60
61 #ifdef TCP_OFFLOAD
62 #include "common/common.h"
63 #include "common/t4_msg.h"
64 #include "common/t4_regs.h"
65 #include "common/t4_tcb.h"
66 #include "tom/t4_tom.h"
67
68 #define PPOD_SZ(n)      ((n) * sizeof(struct pagepod))
69 #define PPOD_SIZE       (PPOD_SZ(1))
70
71 /* XXX: must match A_ULP_RX_TDDP_PSZ */
72 static int t4_ddp_pgsz[] = {4096, 4096 << 2, 4096 << 4, 4096 << 6};
73
74 #if 0
75 static void
76 t4_dump_tcb(struct adapter *sc, int tid)
77 {
78         uint32_t tcb_base, off, i, j;
79
80         /* Dump TCB for the tid */
81         tcb_base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
82         t4_write_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2),
83             tcb_base + tid * TCB_SIZE);
84         t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2));
85         off = 0;
86         printf("\n");
87         for (i = 0; i < 4; i++) {
88                 uint32_t buf[8];
89                 for (j = 0; j < 8; j++, off += 4)
90                         buf[j] = htonl(t4_read_reg(sc, MEMWIN2_BASE + off));
91
92                 printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
93                     buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
94                     buf[7]);
95         }
96 }
97 #endif
98
99 #define MAX_DDP_BUFFER_SIZE             (M_TCB_RX_DDP_BUF0_LEN)
100 static int
101 alloc_ppods(struct tom_data *td, int n)
102 {
103         vmem_addr_t ppod;
104
105         MPASS(n > 0);
106
107         if (vmem_alloc(td->ppod_arena, n, M_NOWAIT | M_FIRSTFIT, &ppod) != 0)
108                 return (-1);
109         return ((int)ppod);
110 }
111
112 static void
113 free_ppods(struct tom_data *td, int ppod, int n)
114 {
115
116         MPASS(n > 0);
117
118         vmem_free(td->ppod_arena, ppod, n);
119 }
120
121 static inline int
122 pages_to_nppods(int npages, int ddp_pgsz)
123 {
124         int nsegs = npages * PAGE_SIZE / ddp_pgsz;
125
126         return (howmany(nsegs, PPOD_PAGES));
127 }
128
129 static void
130 free_ddp_buffer(struct tom_data *td, struct ddp_buffer *db)
131 {
132
133         if (db == NULL)
134                 return;
135
136         if (db->pages)
137                 free(db->pages, M_CXGBE);
138
139         if (db->nppods > 0)
140                 free_ppods(td, G_PPOD_TAG(db->tag), db->nppods);
141
142         free(db, M_CXGBE);
143 }
144
145 void
146 release_ddp_resources(struct toepcb *toep)
147 {
148         int i;
149
150         for (i = 0; i < nitems(toep->db); i++) {
151                 if (toep->db[i] != NULL) {
152                         free_ddp_buffer(toep->td, toep->db[i]);
153                         toep->db[i] = NULL;
154                 }
155         }
156 }
157
158 /* XXX: handle_ddp_data code duplication */
159 void
160 insert_ddp_data(struct toepcb *toep, uint32_t n)
161 {
162         struct inpcb *inp = toep->inp;
163         struct tcpcb *tp = intotcpcb(inp);
164         struct sockbuf *sb = &inp->inp_socket->so_rcv;
165         struct mbuf *m;
166
167         INP_WLOCK_ASSERT(inp);
168         SOCKBUF_LOCK_ASSERT(sb);
169
170         m = get_ddp_mbuf(n);
171         tp->rcv_nxt += n;
172 #ifndef USE_DDP_RX_FLOW_CONTROL
173         KASSERT(tp->rcv_wnd >= n, ("%s: negative window size", __func__));
174         tp->rcv_wnd -= n;
175 #endif
176
177         KASSERT(toep->sb_cc >= sbused(sb),
178             ("%s: sb %p has more data (%d) than last time (%d).",
179             __func__, sb, sbused(sb), toep->sb_cc));
180         toep->rx_credits += toep->sb_cc - sbused(sb);
181 #ifdef USE_DDP_RX_FLOW_CONTROL
182         toep->rx_credits -= n;  /* adjust for F_RX_FC_DDP */
183 #endif
184         sbappendstream_locked(sb, m, 0);
185         toep->sb_cc = sbused(sb);
186 }
187
188 /* SET_TCB_FIELD sent as a ULP command looks like this */
189 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \
190     sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core))
191
192 /* RX_DATA_ACK sent as a ULP command looks like this */
193 #define LEN__RX_DATA_ACK_ULP (sizeof(struct ulp_txpkt) + \
194     sizeof(struct ulptx_idata) + sizeof(struct cpl_rx_data_ack_core))
195
196 static inline void *
197 mk_set_tcb_field_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep,
198     uint64_t word, uint64_t mask, uint64_t val)
199 {
200         struct ulptx_idata *ulpsc;
201         struct cpl_set_tcb_field_core *req;
202
203         ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
204         ulpmc->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16));
205
206         ulpsc = (struct ulptx_idata *)(ulpmc + 1);
207         ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
208         ulpsc->len = htobe32(sizeof(*req));
209
210         req = (struct cpl_set_tcb_field_core *)(ulpsc + 1);
211         OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, toep->tid));
212         req->reply_ctrl = htobe16(V_NO_REPLY(1) |
213             V_QUEUENO(toep->ofld_rxq->iq.abs_id));
214         req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
215         req->mask = htobe64(mask);
216         req->val = htobe64(val);
217
218         ulpsc = (struct ulptx_idata *)(req + 1);
219         if (LEN__SET_TCB_FIELD_ULP % 16) {
220                 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
221                 ulpsc->len = htobe32(0);
222                 return (ulpsc + 1);
223         }
224         return (ulpsc);
225 }
226
227 static inline void *
228 mk_rx_data_ack_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep)
229 {
230         struct ulptx_idata *ulpsc;
231         struct cpl_rx_data_ack_core *req;
232
233         ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
234         ulpmc->len = htobe32(howmany(LEN__RX_DATA_ACK_ULP, 16));
235
236         ulpsc = (struct ulptx_idata *)(ulpmc + 1);
237         ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
238         ulpsc->len = htobe32(sizeof(*req));
239
240         req = (struct cpl_rx_data_ack_core *)(ulpsc + 1);
241         OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_RX_DATA_ACK, toep->tid));
242         req->credit_dack = htobe32(F_RX_MODULATE_RX);
243
244         ulpsc = (struct ulptx_idata *)(req + 1);
245         if (LEN__RX_DATA_ACK_ULP % 16) {
246                 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
247                 ulpsc->len = htobe32(0);
248                 return (ulpsc + 1);
249         }
250         return (ulpsc);
251 }
252
253 static inline uint64_t
254 select_ddp_flags(struct socket *so, int flags, int db_idx)
255 {
256         uint64_t ddp_flags = V_TF_DDP_INDICATE_OUT(0);
257         int waitall = flags & MSG_WAITALL;
258         int nb = so->so_state & SS_NBIO || flags & (MSG_DONTWAIT | MSG_NBIO);
259
260         KASSERT(db_idx == 0 || db_idx == 1,
261             ("%s: bad DDP buffer index %d", __func__, db_idx));
262
263         if (db_idx == 0) {
264                 ddp_flags |= V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_ACTIVE_BUF(0);
265                 if (waitall)
266                         ddp_flags |= V_TF_DDP_PUSH_DISABLE_0(1);
267                 else if (nb)
268                         ddp_flags |= V_TF_DDP_BUF0_FLUSH(1);
269                 else
270                         ddp_flags |= V_TF_DDP_BUF0_FLUSH(0);
271         } else {
272                 ddp_flags |= V_TF_DDP_BUF1_VALID(1) | V_TF_DDP_ACTIVE_BUF(1);
273                 if (waitall)
274                         ddp_flags |= V_TF_DDP_PUSH_DISABLE_1(1);
275                 else if (nb)
276                         ddp_flags |= V_TF_DDP_BUF1_FLUSH(1);
277                 else
278                         ddp_flags |= V_TF_DDP_BUF1_FLUSH(0);
279         }
280
281         return (ddp_flags);
282 }
283
284 static struct wrqe *
285 mk_update_tcb_for_ddp(struct adapter *sc, struct toepcb *toep, int db_idx,
286     int offset, uint64_t ddp_flags)
287 {
288         struct ddp_buffer *db = toep->db[db_idx];
289         struct wrqe *wr;
290         struct work_request_hdr *wrh;
291         struct ulp_txpkt *ulpmc;
292         int len;
293
294         KASSERT(db_idx == 0 || db_idx == 1,
295             ("%s: bad DDP buffer index %d", __func__, db_idx));
296
297         /*
298          * We'll send a compound work request that has 3 SET_TCB_FIELDs and an
299          * RX_DATA_ACK (with RX_MODULATE to speed up delivery).
300          *
301          * The work request header is 16B and always ends at a 16B boundary.
302          * The ULPTX master commands that follow must all end at 16B boundaries
303          * too so we round up the size to 16.
304          */
305         len = sizeof(*wrh) + 3 * roundup2(LEN__SET_TCB_FIELD_ULP, 16) +
306             roundup2(LEN__RX_DATA_ACK_ULP, 16);
307
308         wr = alloc_wrqe(len, toep->ctrlq);
309         if (wr == NULL)
310                 return (NULL);
311         wrh = wrtod(wr);
312         INIT_ULPTX_WRH(wrh, len, 1, 0); /* atomic */
313         ulpmc = (struct ulp_txpkt *)(wrh + 1);
314
315         /* Write the buffer's tag */
316         ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
317             W_TCB_RX_DDP_BUF0_TAG + db_idx,
318             V_TCB_RX_DDP_BUF0_TAG(M_TCB_RX_DDP_BUF0_TAG),
319             V_TCB_RX_DDP_BUF0_TAG(db->tag));
320
321         /* Update the current offset in the DDP buffer and its total length */
322         if (db_idx == 0)
323                 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
324                     W_TCB_RX_DDP_BUF0_OFFSET,
325                     V_TCB_RX_DDP_BUF0_OFFSET(M_TCB_RX_DDP_BUF0_OFFSET) |
326                     V_TCB_RX_DDP_BUF0_LEN(M_TCB_RX_DDP_BUF0_LEN),
327                     V_TCB_RX_DDP_BUF0_OFFSET(offset) |
328                     V_TCB_RX_DDP_BUF0_LEN(db->len));
329         else
330                 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
331                     W_TCB_RX_DDP_BUF1_OFFSET,
332                     V_TCB_RX_DDP_BUF1_OFFSET(M_TCB_RX_DDP_BUF1_OFFSET) |
333                     V_TCB_RX_DDP_BUF1_LEN((u64)M_TCB_RX_DDP_BUF1_LEN << 32),
334                     V_TCB_RX_DDP_BUF1_OFFSET(offset) |
335                     V_TCB_RX_DDP_BUF1_LEN((u64)db->len << 32));
336
337         /* Update DDP flags */
338         ulpmc = mk_set_tcb_field_ulp(ulpmc, toep, W_TCB_RX_DDP_FLAGS,
339             V_TF_DDP_BUF0_FLUSH(1) | V_TF_DDP_BUF1_FLUSH(1) |
340             V_TF_DDP_PUSH_DISABLE_0(1) | V_TF_DDP_PUSH_DISABLE_1(1) |
341             V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_BUF1_VALID(1) |
342             V_TF_DDP_ACTIVE_BUF(1) | V_TF_DDP_INDICATE_OUT(1), ddp_flags);
343
344         /* Gratuitous RX_DATA_ACK with RX_MODULATE set to speed up delivery. */
345         ulpmc = mk_rx_data_ack_ulp(ulpmc, toep);
346
347         return (wr);
348 }
349
350 static void
351 discourage_ddp(struct toepcb *toep)
352 {
353
354         if (toep->ddp_score && --toep->ddp_score == 0) {
355                 toep->ddp_flags &= ~DDP_OK;
356                 toep->ddp_disabled = time_uptime;
357                 CTR3(KTR_CXGBE, "%s: tid %u !DDP_OK @ %u",
358                     __func__, toep->tid, time_uptime);
359         }
360 }
361
362 static int
363 handle_ddp_data(struct toepcb *toep, __be32 ddp_report, __be32 rcv_nxt, int len)
364 {
365         uint32_t report = be32toh(ddp_report);
366         unsigned int db_flag;
367         struct inpcb *inp = toep->inp;
368         struct tcpcb *tp;
369         struct socket *so;
370         struct sockbuf *sb;
371         struct mbuf *m;
372
373         db_flag = report & F_DDP_BUF_IDX ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
374
375         if (__predict_false(!(report & F_DDP_INV)))
376                 CXGBE_UNIMPLEMENTED("DDP buffer still valid");
377
378         INP_WLOCK(inp);
379         so = inp_inpcbtosocket(inp);
380         sb = &so->so_rcv;
381         if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
382
383                 /*
384                  * XXX: think a bit more.
385                  * tcpcb probably gone, but socket should still be around
386                  * because we always wait for DDP completion in soreceive no
387                  * matter what.  Just wake it up and let it clean up.
388                  */
389
390                 CTR5(KTR_CXGBE, "%s: tid %u, seq 0x%x, len %d, inp_flags 0x%x",
391                     __func__, toep->tid, be32toh(rcv_nxt), len, inp->inp_flags);
392                 SOCKBUF_LOCK(sb);
393                 goto wakeup;
394         }
395
396         tp = intotcpcb(inp);
397         len += be32toh(rcv_nxt) - tp->rcv_nxt;
398         tp->rcv_nxt += len;
399         tp->t_rcvtime = ticks;
400 #ifndef USE_DDP_RX_FLOW_CONTROL
401         KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
402         tp->rcv_wnd -= len;
403 #endif
404         m = get_ddp_mbuf(len);
405
406         SOCKBUF_LOCK(sb);
407         if (report & F_DDP_BUF_COMPLETE)
408                 toep->ddp_score = DDP_HIGH_SCORE;
409         else
410                 discourage_ddp(toep);
411
412         KASSERT(toep->sb_cc >= sbused(sb),
413             ("%s: sb %p has more data (%d) than last time (%d).",
414             __func__, sb, sbused(sb), toep->sb_cc));
415         toep->rx_credits += toep->sb_cc - sbused(sb);
416 #ifdef USE_DDP_RX_FLOW_CONTROL
417         toep->rx_credits -= len;        /* adjust for F_RX_FC_DDP */
418 #endif
419         sbappendstream_locked(sb, m, 0);
420         toep->sb_cc = sbused(sb);
421 wakeup:
422         KASSERT(toep->ddp_flags & db_flag,
423             ("%s: DDP buffer not active. toep %p, ddp_flags 0x%x, report 0x%x",
424             __func__, toep, toep->ddp_flags, report));
425         toep->ddp_flags &= ~db_flag;
426         sorwakeup_locked(so);
427         SOCKBUF_UNLOCK_ASSERT(sb);
428
429         INP_WUNLOCK(inp);
430         return (0);
431 }
432
433 #define DDP_ERR (F_DDP_PPOD_MISMATCH | F_DDP_LLIMIT_ERR | F_DDP_ULIMIT_ERR |\
434          F_DDP_PPOD_PARITY_ERR | F_DDP_PADDING_ERR | F_DDP_OFFSET_ERR |\
435          F_DDP_INVALID_TAG | F_DDP_COLOR_ERR | F_DDP_TID_MISMATCH |\
436          F_DDP_INVALID_PPOD | F_DDP_HDRCRC_ERR | F_DDP_DATACRC_ERR)
437
438 static int
439 do_rx_data_ddp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
440 {
441         struct adapter *sc = iq->adapter;
442         const struct cpl_rx_data_ddp *cpl = (const void *)(rss + 1);
443         unsigned int tid = GET_TID(cpl);
444         uint32_t vld;
445         struct toepcb *toep = lookup_tid(sc, tid);
446         struct tom_data *td = toep->td;
447
448         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
449         KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
450         KASSERT(!(toep->flags & TPF_SYNQE),
451             ("%s: toep %p claims to be a synq entry", __func__, toep));
452
453         vld = be32toh(cpl->ddpvld);
454         if (__predict_false(vld & DDP_ERR)) {
455                 panic("%s: DDP error 0x%x (tid %d, toep %p)",
456                     __func__, vld, tid, toep);
457         }
458         if (toep->ulp_mode == ULP_MODE_ISCSI) {
459                 m = m_get(M_NOWAIT, MT_DATA);
460                 if (m == NULL)
461                         CXGBE_UNIMPLEMENTED("mbuf alloc failure");
462                 memcpy(mtod(m, unsigned char *), cpl,
463                     sizeof(struct cpl_rx_data_ddp));
464                 if (!t4_cpl_iscsi_callback(td, toep, m, CPL_RX_DATA_DDP))
465                         return (0);
466                 m_freem(m);
467         }
468
469         handle_ddp_data(toep, cpl->u.ddp_report, cpl->seq, be16toh(cpl->len));
470
471         return (0);
472 }
473
474 static int
475 do_rx_ddp_complete(struct sge_iq *iq, const struct rss_header *rss,
476     struct mbuf *m)
477 {
478         struct adapter *sc = iq->adapter;
479         const struct cpl_rx_ddp_complete *cpl = (const void *)(rss + 1);
480         unsigned int tid = GET_TID(cpl);
481         struct toepcb *toep = lookup_tid(sc, tid);
482
483         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
484         KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
485         KASSERT(!(toep->flags & TPF_SYNQE),
486             ("%s: toep %p claims to be a synq entry", __func__, toep));
487
488         handle_ddp_data(toep, cpl->ddp_report, cpl->rcv_nxt, 0);
489
490         return (0);
491 }
492
493 void
494 enable_ddp(struct adapter *sc, struct toepcb *toep)
495 {
496
497         KASSERT((toep->ddp_flags & (DDP_ON | DDP_OK | DDP_SC_REQ)) == DDP_OK,
498             ("%s: toep %p has bad ddp_flags 0x%x",
499             __func__, toep, toep->ddp_flags));
500
501         CTR3(KTR_CXGBE, "%s: tid %u (time %u)",
502             __func__, toep->tid, time_uptime);
503
504         toep->ddp_flags |= DDP_SC_REQ;
505         t4_set_tcb_field(sc, toep, 1, W_TCB_RX_DDP_FLAGS,
506             V_TF_DDP_OFF(1) | V_TF_DDP_INDICATE_OUT(1) |
507             V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1) |
508             V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_BUF1_VALID(1),
509             V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1));
510         t4_set_tcb_field(sc, toep, 1, W_TCB_T_FLAGS,
511             V_TF_RCV_COALESCE_ENABLE(1), 0);
512 }
513
514 static inline void
515 disable_ddp(struct adapter *sc, struct toepcb *toep)
516 {
517
518         KASSERT((toep->ddp_flags & (DDP_ON | DDP_SC_REQ)) == DDP_ON,
519             ("%s: toep %p has bad ddp_flags 0x%x",
520             __func__, toep, toep->ddp_flags));
521
522         CTR3(KTR_CXGBE, "%s: tid %u (time %u)",
523             __func__, toep->tid, time_uptime);
524
525         toep->ddp_flags |= DDP_SC_REQ;
526         t4_set_tcb_field(sc, toep, 1, W_TCB_T_FLAGS,
527             V_TF_RCV_COALESCE_ENABLE(1), V_TF_RCV_COALESCE_ENABLE(1));
528         t4_set_tcb_field(sc, toep, 1, W_TCB_RX_DDP_FLAGS, V_TF_DDP_OFF(1),
529             V_TF_DDP_OFF(1));
530 }
531
532 static int
533 hold_uio(struct uio *uio, vm_page_t **ppages, int *pnpages)
534 {
535         struct vm_map *map;
536         struct iovec *iov;
537         vm_offset_t start, end;
538         vm_page_t *pp;
539         int n;
540
541         KASSERT(uio->uio_iovcnt == 1,
542             ("%s: uio_iovcnt %d", __func__, uio->uio_iovcnt));
543         KASSERT(uio->uio_td->td_proc == curproc,
544             ("%s: uio proc (%p) is not curproc (%p)",
545             __func__, uio->uio_td->td_proc, curproc));
546
547         map = &curproc->p_vmspace->vm_map;
548         iov = &uio->uio_iov[0];
549         start = trunc_page((uintptr_t)iov->iov_base);
550         end = round_page((vm_offset_t)iov->iov_base + iov->iov_len);
551         n = howmany(end - start, PAGE_SIZE);
552
553         if (end - start > MAX_DDP_BUFFER_SIZE)
554                 return (E2BIG);
555
556         pp = malloc(n * sizeof(vm_page_t), M_CXGBE, M_NOWAIT);
557         if (pp == NULL)
558                 return (ENOMEM);
559
560         if (vm_fault_quick_hold_pages(map, (vm_offset_t)iov->iov_base,
561             iov->iov_len, VM_PROT_WRITE, pp, n) < 0) {
562                 free(pp, M_CXGBE);
563                 return (EFAULT);
564         }
565
566         *ppages = pp;
567         *pnpages = n;
568
569         return (0);
570 }
571
572 static int
573 bufcmp(struct ddp_buffer *db, vm_page_t *pages, int npages, int offset, int len)
574 {
575         int i;
576
577         if (db == NULL || db->npages != npages || db->offset != offset ||
578             db->len != len)
579                 return (1);
580
581         for (i = 0; i < npages; i++) {
582                 if (pages[i]->phys_addr != db->pages[i]->phys_addr)
583                         return (1);
584         }
585
586         return (0);
587 }
588
589 static int
590 calculate_hcf(int n1, int n2)
591 {
592         int a, b, t;
593
594         if (n1 <= n2) {
595                 a = n1;
596                 b = n2;
597         } else {
598                 a = n2;
599                 b = n1;
600         }
601
602         while (a != 0) {
603                 t = a;
604                 a = b % a;
605                 b = t;
606         }
607
608         return (b);
609 }
610
611 static struct ddp_buffer *
612 alloc_ddp_buffer(struct tom_data *td, vm_page_t *pages, int npages, int offset,
613     int len)
614 {
615         int i, hcf, seglen, idx, ppod, nppods;
616         struct ddp_buffer *db;
617
618         /*
619          * The DDP page size is unrelated to the VM page size.  We combine
620          * contiguous physical pages into larger segments to get the best DDP
621          * page size possible.  This is the largest of the four sizes in
622          * A_ULP_RX_TDDP_PSZ that evenly divides the HCF of the segment sizes in
623          * the page list.
624          */
625         hcf = 0;
626         for (i = 0; i < npages; i++) {
627                 seglen = PAGE_SIZE;
628                 while (i < npages - 1 &&
629                     pages[i]->phys_addr + PAGE_SIZE == pages[i + 1]->phys_addr) {
630                         seglen += PAGE_SIZE;
631                         i++;
632                 }
633
634                 hcf = calculate_hcf(hcf, seglen);
635                 if (hcf < t4_ddp_pgsz[1]) {
636                         idx = 0;
637                         goto have_pgsz; /* give up, short circuit */
638                 }
639         }
640
641         if (hcf % t4_ddp_pgsz[0] != 0) {
642                 /* hmmm.  This could only happen when PAGE_SIZE < 4K */
643                 KASSERT(PAGE_SIZE < 4096,
644                     ("%s: PAGE_SIZE %d, hcf %d", __func__, PAGE_SIZE, hcf));
645                 CTR3(KTR_CXGBE, "%s: PAGE_SIZE %d, hcf %d",
646                     __func__, PAGE_SIZE, hcf);
647                 return (NULL);
648         }
649
650         for (idx = nitems(t4_ddp_pgsz) - 1; idx > 0; idx--) {
651                 if (hcf % t4_ddp_pgsz[idx] == 0)
652                         break;
653         }
654 have_pgsz:
655
656         db = malloc(sizeof(*db), M_CXGBE, M_NOWAIT);
657         if (db == NULL) {
658                 CTR1(KTR_CXGBE, "%s: malloc failed.", __func__);
659                 return (NULL);
660         }
661
662         nppods = pages_to_nppods(npages, t4_ddp_pgsz[idx]);
663         ppod = alloc_ppods(td, nppods);
664         if (ppod < 0) {
665                 free(db, M_CXGBE);
666                 CTR4(KTR_CXGBE, "%s: no pods, nppods %d, resid %d, pgsz %d",
667                     __func__, nppods, len, t4_ddp_pgsz[idx]);
668                 return (NULL);
669         }
670
671         KASSERT(idx <= M_PPOD_PGSZ && ppod <= M_PPOD_TAG,
672             ("%s: DDP pgsz_idx = %d, ppod = %d", __func__, idx, ppod));
673
674         db->tag = V_PPOD_PGSZ(idx) | V_PPOD_TAG(ppod);
675         db->nppods = nppods;
676         db->npages = npages;
677         db->pages = pages;
678         db->offset = offset;
679         db->len = len;
680
681         CTR6(KTR_CXGBE, "New DDP buffer.  "
682             "ddp_pgsz %d, ppod 0x%x, npages %d, nppods %d, offset %d, len %d",
683             t4_ddp_pgsz[idx], ppod, db->npages, db->nppods, db->offset,
684             db->len);
685
686         return (db);
687 }
688
689 #define NUM_ULP_TX_SC_IMM_PPODS (256 / PPOD_SIZE)
690
691 static int
692 write_page_pods(struct adapter *sc, struct toepcb *toep, struct ddp_buffer *db)
693 {
694         struct wrqe *wr;
695         struct ulp_mem_io *ulpmc;
696         struct ulptx_idata *ulpsc;
697         struct pagepod *ppod;
698         int i, j, k, n, chunk, len, ddp_pgsz, idx, ppod_addr;
699         uint32_t cmd;
700
701         cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
702         if (is_t4(sc))
703                 cmd |= htobe32(F_ULP_MEMIO_ORDER);
704         else
705                 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
706         ddp_pgsz = t4_ddp_pgsz[G_PPOD_PGSZ(db->tag)];
707         ppod_addr = sc->vres.ddp.start + G_PPOD_TAG(db->tag) * PPOD_SIZE;
708         for (i = 0; i < db->nppods; ppod_addr += chunk) {
709
710                 /* How many page pods are we writing in this cycle */
711                 n = min(db->nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
712                 chunk = PPOD_SZ(n);
713                 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
714
715                 wr = alloc_wrqe(len, toep->ctrlq);
716                 if (wr == NULL)
717                         return (ENOMEM);        /* ok to just bail out */
718                 ulpmc = wrtod(wr);
719
720                 INIT_ULPTX_WR(ulpmc, len, 0, 0);
721                 ulpmc->cmd = cmd;
722                 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
723                 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
724                 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
725
726                 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
727                 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
728                 ulpsc->len = htobe32(chunk);
729
730                 ppod = (struct pagepod *)(ulpsc + 1);
731                 for (j = 0; j < n; i++, j++, ppod++) {
732                         ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
733                             V_PPOD_TID(toep->tid) | db->tag);
734                         ppod->len_offset = htobe64(V_PPOD_LEN(db->len) |
735                             V_PPOD_OFST(db->offset));
736                         ppod->rsvd = 0;
737                         idx = i * PPOD_PAGES * (ddp_pgsz / PAGE_SIZE);
738                         for (k = 0; k < nitems(ppod->addr); k++) {
739                                 if (idx < db->npages) {
740                                         ppod->addr[k] =
741                                             htobe64(db->pages[idx]->phys_addr);
742                                         idx += ddp_pgsz / PAGE_SIZE;
743                                 } else
744                                         ppod->addr[k] = 0;
745 #if 0
746                                 CTR5(KTR_CXGBE,
747                                     "%s: tid %d ppod[%d]->addr[%d] = %p",
748                                     __func__, toep->tid, i, k,
749                                     htobe64(ppod->addr[k]));
750 #endif
751                         }
752
753                 }
754
755                 t4_wrq_tx(sc, wr);
756         }
757
758         return (0);
759 }
760
761 /*
762  * Reuse, or allocate (and program the page pods for) a new DDP buffer.  The
763  * "pages" array is handed over to this function and should not be used in any
764  * way by the caller after that.
765  */
766 static int
767 select_ddp_buffer(struct adapter *sc, struct toepcb *toep, vm_page_t *pages,
768     int npages, int db_off, int db_len)
769 {
770         struct ddp_buffer *db;
771         struct tom_data *td = sc->tom_softc;
772         int i, empty_slot = -1;
773
774         /* Try to reuse */
775         for (i = 0; i < nitems(toep->db); i++) {
776                 if (bufcmp(toep->db[i], pages, npages, db_off, db_len) == 0) {
777                         free(pages, M_CXGBE);
778                         return (i);     /* pages still held */
779                 } else if (toep->db[i] == NULL && empty_slot < 0)
780                         empty_slot = i;
781         }
782
783         /* Allocate new buffer, write its page pods. */
784         db = alloc_ddp_buffer(td, pages, npages, db_off, db_len);
785         if (db == NULL) {
786                 vm_page_unhold_pages(pages, npages);
787                 free(pages, M_CXGBE);
788                 return (-1);
789         }
790         if (write_page_pods(sc, toep, db) != 0) {
791                 vm_page_unhold_pages(pages, npages);
792                 free_ddp_buffer(td, db);
793                 return (-1);
794         }
795
796         i = empty_slot;
797         if (i < 0) {
798                 i = arc4random() % nitems(toep->db);
799                 free_ddp_buffer(td, toep->db[i]);
800         }
801         toep->db[i] = db;
802
803         CTR5(KTR_CXGBE, "%s: tid %d, DDP buffer[%d] = %p (tag 0x%x)",
804             __func__, toep->tid, i, db, db->tag);
805
806         return (i);
807 }
808
809 static void
810 wire_ddp_buffer(struct ddp_buffer *db)
811 {
812         int i;
813         vm_page_t p;
814
815         for (i = 0; i < db->npages; i++) {
816                 p = db->pages[i];
817                 vm_page_lock(p);
818                 vm_page_wire(p);
819                 vm_page_unhold(p);
820                 vm_page_unlock(p);
821         }
822 }
823
824 static void
825 unwire_ddp_buffer(struct ddp_buffer *db)
826 {
827         int i;
828         vm_page_t p;
829
830         for (i = 0; i < db->npages; i++) {
831                 p = db->pages[i];
832                 vm_page_lock(p);
833                 vm_page_unwire(p, PQ_INACTIVE);
834                 vm_page_unlock(p);
835         }
836 }
837
838 static int
839 handle_ddp(struct socket *so, struct uio *uio, int flags, int error)
840 {
841         struct sockbuf *sb = &so->so_rcv;
842         struct tcpcb *tp = so_sototcpcb(so);
843         struct toepcb *toep = tp->t_toe;
844         struct adapter *sc = td_adapter(toep->td);
845         vm_page_t *pages;
846         int npages, db_idx, rc, buf_flag;
847         struct ddp_buffer *db;
848         struct wrqe *wr;
849         uint64_t ddp_flags;
850
851         SOCKBUF_LOCK_ASSERT(sb);
852
853 #if 0
854         if (sb->sb_cc + sc->tt.ddp_thres > uio->uio_resid) {
855                 CTR4(KTR_CXGBE, "%s: sb_cc %d, threshold %d, resid %d",
856                     __func__, sb->sb_cc, sc->tt.ddp_thres, uio->uio_resid);
857         }
858 #endif
859
860         /* XXX: too eager to disable DDP, could handle NBIO better than this. */
861         if (sbused(sb) >= uio->uio_resid || uio->uio_resid < sc->tt.ddp_thres ||
862             uio->uio_resid > MAX_DDP_BUFFER_SIZE || uio->uio_iovcnt > 1 ||
863             so->so_state & SS_NBIO || flags & (MSG_DONTWAIT | MSG_NBIO) ||
864             error || so->so_error || sb->sb_state & SBS_CANTRCVMORE)
865                 goto no_ddp;
866
867         /*
868          * Fault in and then hold the pages of the uio buffers.  We'll wire them
869          * a bit later if everything else works out.
870          */
871         SOCKBUF_UNLOCK(sb);
872         if (hold_uio(uio, &pages, &npages) != 0) {
873                 SOCKBUF_LOCK(sb);
874                 goto no_ddp;
875         }
876         SOCKBUF_LOCK(sb);
877         if (__predict_false(so->so_error || sb->sb_state & SBS_CANTRCVMORE)) {
878                 vm_page_unhold_pages(pages, npages);
879                 free(pages, M_CXGBE);
880                 goto no_ddp;
881         }
882
883         /*
884          * Figure out which one of the two DDP buffers to use this time.
885          */
886         db_idx = select_ddp_buffer(sc, toep, pages, npages,
887             (uintptr_t)uio->uio_iov->iov_base & PAGE_MASK, uio->uio_resid);
888         pages = NULL;   /* handed off to select_ddp_buffer */
889         if (db_idx < 0)
890                 goto no_ddp;
891         db = toep->db[db_idx];
892         buf_flag = db_idx == 0 ? DDP_BUF0_ACTIVE : DDP_BUF1_ACTIVE;
893
894         /*
895          * Build the compound work request that tells the chip where to DMA the
896          * payload.
897          */
898         ddp_flags = select_ddp_flags(so, flags, db_idx);
899         wr = mk_update_tcb_for_ddp(sc, toep, db_idx, sbused(sb), ddp_flags);
900         if (wr == NULL) {
901                 /*
902                  * Just unhold the pages.  The DDP buffer's software state is
903                  * left as-is in the toep.  The page pods were written
904                  * successfully and we may have an opportunity to use it in the
905                  * future.
906                  */
907                 vm_page_unhold_pages(db->pages, db->npages);
908                 goto no_ddp;
909         }
910
911         /* Wire (and then unhold) the pages, and give the chip the go-ahead. */
912         wire_ddp_buffer(db);
913         t4_wrq_tx(sc, wr);
914         sb->sb_flags &= ~SB_DDP_INDICATE;
915         toep->ddp_flags |= buf_flag;
916
917         /*
918          * Wait for the DDP operation to complete and then unwire the pages.
919          * The return code from the sbwait will be the final return code of this
920          * function.  But we do need to wait for DDP no matter what.
921          */
922         rc = sbwait(sb);
923         while (toep->ddp_flags & buf_flag) {
924                 /* XXXGL: shouldn't here be sbwait() call? */
925                 sb->sb_flags |= SB_WAIT;
926                 msleep(&sb->sb_acc, &sb->sb_mtx, PSOCK , "sbwait", 0);
927         }
928         unwire_ddp_buffer(db);
929         return (rc);
930 no_ddp:
931         disable_ddp(sc, toep);
932         discourage_ddp(toep);
933         sb->sb_flags &= ~SB_DDP_INDICATE;
934         return (0);
935 }
936
937 void
938 t4_init_ddp(struct adapter *sc, struct tom_data *td)
939 {
940         int nppods = sc->vres.ddp.size / PPOD_SIZE;
941
942         td->ppod_arena = vmem_create("DDP page pods", 0, nppods, 1, 32,
943             M_FIRSTFIT | M_NOWAIT);
944
945         t4_register_cpl_handler(sc, CPL_RX_DATA_DDP, do_rx_data_ddp);
946         t4_register_cpl_handler(sc, CPL_RX_DDP_COMPLETE, do_rx_ddp_complete);
947 }
948
949 void
950 t4_uninit_ddp(struct adapter *sc __unused, struct tom_data *td)
951 {
952
953         if (td->ppod_arena != NULL) {
954                 vmem_destroy(td->ppod_arena);
955                 td->ppod_arena = NULL;
956         }
957 }
958
959 #define VNET_SO_ASSERT(so)                                              \
960         VNET_ASSERT(curvnet != NULL,                                    \
961             ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
962 #define SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
963 static int
964 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
965 {
966
967         CXGBE_UNIMPLEMENTED(__func__);
968 }
969
970 static char ddp_magic_str[] = "nothing to see here";
971
972 struct mbuf *
973 get_ddp_mbuf(int len)
974 {
975         struct mbuf *m;
976
977         m = m_get(M_NOWAIT, MT_DATA);
978         if (m == NULL)
979                 CXGBE_UNIMPLEMENTED("mbuf alloc failure");
980         m->m_len = len;
981         m->m_data = &ddp_magic_str[0];
982
983         return (m);
984 }
985
986 static inline int
987 is_ddp_mbuf(struct mbuf *m)
988 {
989
990         return (m->m_data == &ddp_magic_str[0]);
991 }
992
993 /*
994  * Copy an mbuf chain into a uio limited by len if set.
995  */
996 static int
997 m_mbuftouio_ddp(struct uio *uio, struct mbuf *m, int len)
998 {
999         int error, length, total;
1000         int progress = 0;
1001
1002         if (len > 0)
1003                 total = min(uio->uio_resid, len);
1004         else
1005                 total = uio->uio_resid;
1006
1007         /* Fill the uio with data from the mbufs. */
1008         for (; m != NULL; m = m->m_next) {
1009                 length = min(m->m_len, total - progress);
1010
1011                 if (is_ddp_mbuf(m)) {
1012                         enum uio_seg segflag = uio->uio_segflg;
1013
1014                         uio->uio_segflg = UIO_NOCOPY;
1015                         error = uiomove(mtod(m, void *), length, uio);
1016                         uio->uio_segflg = segflag;
1017                 } else
1018                         error = uiomove(mtod(m, void *), length, uio);
1019                 if (error)
1020                         return (error);
1021
1022                 progress += length;
1023         }
1024
1025         return (0);
1026 }
1027
1028 /*
1029  * Based on soreceive_stream() in uipc_socket.c
1030  */
1031 int
1032 t4_soreceive_ddp(struct socket *so, struct sockaddr **psa, struct uio *uio,
1033     struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1034 {
1035         int len = 0, error = 0, flags, oresid, ddp_handled = 0;
1036         struct sockbuf *sb;
1037         struct mbuf *m, *n = NULL;
1038
1039         /* We only do stream sockets. */
1040         if (so->so_type != SOCK_STREAM)
1041                 return (EINVAL);
1042         if (psa != NULL)
1043                 *psa = NULL;
1044         if (controlp != NULL)
1045                 return (EINVAL);
1046         if (flagsp != NULL)
1047                 flags = *flagsp &~ MSG_EOR;
1048         else
1049                 flags = 0;
1050         if (flags & MSG_OOB)
1051                 return (soreceive_rcvoob(so, uio, flags));
1052         if (mp0 != NULL)
1053                 *mp0 = NULL;
1054
1055         sb = &so->so_rcv;
1056
1057         /* Prevent other readers from entering the socket. */
1058         error = sblock(sb, SBLOCKWAIT(flags));
1059         if (error)
1060                 goto out;
1061         SOCKBUF_LOCK(sb);
1062
1063         /* Easy one, no space to copyout anything. */
1064         if (uio->uio_resid == 0) {
1065                 error = EINVAL;
1066                 goto out;
1067         }
1068         oresid = uio->uio_resid;
1069
1070         /* We will never ever get anything unless we are or were connected. */
1071         if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1072                 error = ENOTCONN;
1073                 goto out;
1074         }
1075
1076 restart:
1077         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1078
1079         if (sb->sb_flags & SB_DDP_INDICATE && !ddp_handled) {
1080
1081                 /* uio should be just as it was at entry */
1082                 KASSERT(oresid == uio->uio_resid,
1083                     ("%s: oresid = %d, uio_resid = %zd, sbused = %d",
1084                     __func__, oresid, uio->uio_resid, sbused(sb)));
1085
1086                 error = handle_ddp(so, uio, flags, 0);
1087                 ddp_handled = 1;
1088                 if (error)
1089                         goto out;
1090         }
1091
1092         /* Abort if socket has reported problems. */
1093         if (so->so_error) {
1094                 if (sbused(sb))
1095                         goto deliver;
1096                 if (oresid > uio->uio_resid)
1097                         goto out;
1098                 error = so->so_error;
1099                 if (!(flags & MSG_PEEK))
1100                         so->so_error = 0;
1101                 goto out;
1102         }
1103
1104         /* Door is closed.  Deliver what is left, if any. */
1105         if (sb->sb_state & SBS_CANTRCVMORE) {
1106                 if (sbused(sb))
1107                         goto deliver;
1108                 else
1109                         goto out;
1110         }
1111
1112         /* Socket buffer is empty and we shall not block. */
1113         if (sbused(sb) == 0 &&
1114             ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
1115                 error = EAGAIN;
1116                 goto out;
1117         }
1118
1119         /* Socket buffer got some data that we shall deliver now. */
1120         if (sbused(sb) && !(flags & MSG_WAITALL) &&
1121             ((so->so_state & SS_NBIO) ||
1122              (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
1123              sbused(sb) >= sb->sb_lowat ||
1124              sbused(sb) >= uio->uio_resid ||
1125              sbused(sb) >= sb->sb_hiwat) ) {
1126                 goto deliver;
1127         }
1128
1129         /* On MSG_WAITALL we must wait until all data or error arrives. */
1130         if ((flags & MSG_WAITALL) &&
1131             (sbused(sb) >= uio->uio_resid || sbused(sb) >= sb->sb_lowat))
1132                 goto deliver;
1133
1134         /*
1135          * Wait and block until (more) data comes in.
1136          * NB: Drops the sockbuf lock during wait.
1137          */
1138         error = sbwait(sb);
1139         if (error) {
1140                 if (sb->sb_flags & SB_DDP_INDICATE && !ddp_handled) {
1141                         (void) handle_ddp(so, uio, flags, 1);
1142                         ddp_handled = 1;
1143                 }
1144                 goto out;
1145         }
1146         goto restart;
1147
1148 deliver:
1149         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1150         KASSERT(sbused(sb) > 0, ("%s: sockbuf empty", __func__));
1151         KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
1152
1153         if (sb->sb_flags & SB_DDP_INDICATE && !ddp_handled)
1154                 goto restart;
1155
1156         /* Statistics. */
1157         if (uio->uio_td)
1158                 uio->uio_td->td_ru.ru_msgrcv++;
1159
1160         /* Fill uio until full or current end of socket buffer is reached. */
1161         len = min(uio->uio_resid, sbused(sb));
1162         if (mp0 != NULL) {
1163                 /* Dequeue as many mbufs as possible. */
1164                 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
1165                         for (*mp0 = m = sb->sb_mb;
1166                              m != NULL && m->m_len <= len;
1167                              m = m->m_next) {
1168                                 len -= m->m_len;
1169                                 uio->uio_resid -= m->m_len;
1170                                 sbfree(sb, m);
1171                                 n = m;
1172                         }
1173                         sb->sb_mb = m;
1174                         if (sb->sb_mb == NULL)
1175                                 SB_EMPTY_FIXUP(sb);
1176                         n->m_next = NULL;
1177                 }
1178                 /* Copy the remainder. */
1179                 if (len > 0) {
1180                         KASSERT(sb->sb_mb != NULL,
1181                             ("%s: len > 0 && sb->sb_mb empty", __func__));
1182
1183                         m = m_copym(sb->sb_mb, 0, len, M_NOWAIT);
1184                         if (m == NULL)
1185                                 len = 0;        /* Don't flush data from sockbuf. */
1186                         else
1187                                 uio->uio_resid -= m->m_len;
1188                         if (*mp0 != NULL)
1189                                 n->m_next = m;
1190                         else
1191                                 *mp0 = m;
1192                         if (*mp0 == NULL) {
1193                                 error = ENOBUFS;
1194                                 goto out;
1195                         }
1196                 }
1197         } else {
1198                 /* NB: Must unlock socket buffer as uiomove may sleep. */
1199                 SOCKBUF_UNLOCK(sb);
1200                 error = m_mbuftouio_ddp(uio, sb->sb_mb, len);
1201                 SOCKBUF_LOCK(sb);
1202                 if (error)
1203                         goto out;
1204         }
1205         SBLASTRECORDCHK(sb);
1206         SBLASTMBUFCHK(sb);
1207
1208         /*
1209          * Remove the delivered data from the socket buffer unless we
1210          * were only peeking.
1211          */
1212         if (!(flags & MSG_PEEK)) {
1213                 if (len > 0)
1214                         sbdrop_locked(sb, len);
1215
1216                 /* Notify protocol that we drained some data. */
1217                 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
1218                     (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
1219                      !(flags & MSG_SOCALLBCK))) {
1220                         SOCKBUF_UNLOCK(sb);
1221                         VNET_SO_ASSERT(so);
1222                         (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
1223                         SOCKBUF_LOCK(sb);
1224                 }
1225         }
1226
1227         /*
1228          * For MSG_WAITALL we may have to loop again and wait for
1229          * more data to come in.
1230          */
1231         if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
1232                 goto restart;
1233 out:
1234         SOCKBUF_LOCK_ASSERT(sb);
1235         SBLASTRECORDCHK(sb);
1236         SBLASTMBUFCHK(sb);
1237         SOCKBUF_UNLOCK(sb);
1238         sbunlock(sb);
1239         return (error);
1240 }
1241
1242 #endif