2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Chelsio Communications, Inc.
6 * Written by: Navdeep Parhar <np@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/protosw.h>
44 #include <sys/domain.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/taskqueue.h>
49 #include <netinet/in.h>
50 #include <netinet/in_pcb.h>
51 #include <netinet/ip.h>
52 #include <netinet/tcp_var.h>
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/toecore.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_param.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_object.h>
66 #include "common/common.h"
67 #include "common/t4_msg.h"
68 #include "common/t4_regs.h"
69 #include "common/t4_tcb.h"
70 #include "tom/t4_tom.h"
73 * Use the 'backend3' field in AIO jobs to store the amount of data
74 * received by the AIO job so far.
76 #define aio_received backend3
78 static void aio_ddp_requeue_task(void *context, int pending);
79 static void ddp_complete_all(struct toepcb *toep, int error);
80 static void t4_aio_cancel_active(struct kaiocb *job);
81 static void t4_aio_cancel_queued(struct kaiocb *job);
83 static TAILQ_HEAD(, pageset) ddp_orphan_pagesets;
84 static struct mtx ddp_orphan_pagesets_lock;
85 static struct task ddp_orphan_task;
87 #define MAX_DDP_BUFFER_SIZE (M_TCB_RX_DDP_BUF0_LEN)
90 * A page set holds information about a buffer used for DDP. The page
91 * set holds resources such as the VM pages backing the buffer (either
92 * held or wired) and the page pods associated with the buffer.
93 * Recently used page sets are cached to allow for efficient reuse of
94 * buffers (avoiding the need to re-fault in pages, hold them, etc.).
95 * Note that cached page sets keep the backing pages wired. The
96 * number of wired pages is capped by only allowing for two wired
97 * pagesets per connection. This is not a perfect cap, but is a
98 * trade-off for performance.
100 * If an application ping-pongs two buffers for a connection via
101 * aio_read(2) then those buffers should remain wired and expensive VM
102 * fault lookups should be avoided after each buffer has been used
103 * once. If an application uses more than two buffers then this will
104 * fall back to doing expensive VM fault lookups for each operation.
107 free_pageset(struct tom_data *td, struct pageset *ps)
112 if (ps->prsv.prsv_nppods > 0)
113 t4_free_page_pods(&ps->prsv);
115 for (i = 0; i < ps->npages; i++) {
117 vm_page_unwire(p, PQ_INACTIVE);
119 mtx_lock(&ddp_orphan_pagesets_lock);
120 TAILQ_INSERT_TAIL(&ddp_orphan_pagesets, ps, link);
121 taskqueue_enqueue(taskqueue_thread, &ddp_orphan_task);
122 mtx_unlock(&ddp_orphan_pagesets_lock);
126 ddp_free_orphan_pagesets(void *context, int pending)
130 mtx_lock(&ddp_orphan_pagesets_lock);
131 while (!TAILQ_EMPTY(&ddp_orphan_pagesets)) {
132 ps = TAILQ_FIRST(&ddp_orphan_pagesets);
133 TAILQ_REMOVE(&ddp_orphan_pagesets, ps, link);
134 mtx_unlock(&ddp_orphan_pagesets_lock);
136 vmspace_free(ps->vm);
138 mtx_lock(&ddp_orphan_pagesets_lock);
140 mtx_unlock(&ddp_orphan_pagesets_lock);
144 recycle_pageset(struct toepcb *toep, struct pageset *ps)
147 DDP_ASSERT_LOCKED(toep);
148 if (!(toep->ddp.flags & DDP_DEAD)) {
149 KASSERT(toep->ddp.cached_count + toep->ddp.active_count <
150 nitems(toep->ddp.db), ("too many wired pagesets"));
151 TAILQ_INSERT_HEAD(&toep->ddp.cached_pagesets, ps, link);
152 toep->ddp.cached_count++;
154 free_pageset(toep->td, ps);
158 ddp_complete_one(struct kaiocb *job, int error)
163 * If this job had copied data out of the socket buffer before
164 * it was cancelled, report it as a short read rather than an
167 copied = job->aio_received;
168 if (copied != 0 || error == 0)
169 aio_complete(job, copied, 0);
171 aio_complete(job, -1, error);
175 free_ddp_buffer(struct tom_data *td, struct ddp_buffer *db)
180 * XXX: If we are un-offloading the socket then we
181 * should requeue these on the socket somehow. If we
182 * got a FIN from the remote end, then this completes
183 * any remaining requests with an EOF read.
185 if (!aio_clear_cancel_function(db->job))
186 ddp_complete_one(db->job, 0);
190 free_pageset(td, db->ps);
194 ddp_init_toep(struct toepcb *toep)
197 TAILQ_INIT(&toep->ddp.aiojobq);
198 TASK_INIT(&toep->ddp.requeue_task, 0, aio_ddp_requeue_task, toep);
199 toep->ddp.flags = DDP_OK;
200 toep->ddp.active_id = -1;
201 mtx_init(&toep->ddp.lock, "t4 ddp", NULL, MTX_DEF);
205 ddp_uninit_toep(struct toepcb *toep)
208 mtx_destroy(&toep->ddp.lock);
212 release_ddp_resources(struct toepcb *toep)
218 toep->ddp.flags |= DDP_DEAD;
219 for (i = 0; i < nitems(toep->ddp.db); i++) {
220 free_ddp_buffer(toep->td, &toep->ddp.db[i]);
222 while ((ps = TAILQ_FIRST(&toep->ddp.cached_pagesets)) != NULL) {
223 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
224 free_pageset(toep->td, ps);
226 ddp_complete_all(toep, 0);
232 ddp_assert_empty(struct toepcb *toep)
236 MPASS(!(toep->ddp.flags & DDP_TASK_ACTIVE));
237 for (i = 0; i < nitems(toep->ddp.db); i++) {
238 MPASS(toep->ddp.db[i].job == NULL);
239 MPASS(toep->ddp.db[i].ps == NULL);
241 MPASS(TAILQ_EMPTY(&toep->ddp.cached_pagesets));
242 MPASS(TAILQ_EMPTY(&toep->ddp.aiojobq));
247 complete_ddp_buffer(struct toepcb *toep, struct ddp_buffer *db,
250 unsigned int db_flag;
252 toep->ddp.active_count--;
253 if (toep->ddp.active_id == db_idx) {
254 if (toep->ddp.active_count == 0) {
255 KASSERT(toep->ddp.db[db_idx ^ 1].job == NULL,
256 ("%s: active_count mismatch", __func__));
257 toep->ddp.active_id = -1;
259 toep->ddp.active_id ^= 1;
260 #ifdef VERBOSE_TRACES
261 CTR3(KTR_CXGBE, "%s: tid %u, ddp_active_id = %d", __func__,
262 toep->tid, toep->ddp.active_id);
265 KASSERT(toep->ddp.active_count != 0 &&
266 toep->ddp.active_id != -1,
267 ("%s: active count mismatch", __func__));
270 db->cancel_pending = 0;
272 recycle_pageset(toep, db->ps);
275 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
276 KASSERT(toep->ddp.flags & db_flag,
277 ("%s: DDP buffer not active. toep %p, ddp_flags 0x%x",
278 __func__, toep, toep->ddp.flags));
279 toep->ddp.flags &= ~db_flag;
282 /* XXX: handle_ddp_data code duplication */
284 insert_ddp_data(struct toepcb *toep, uint32_t n)
286 struct inpcb *inp = toep->inp;
287 struct tcpcb *tp = intotcpcb(inp);
288 struct ddp_buffer *db;
292 unsigned int db_flag, db_idx;
294 INP_WLOCK_ASSERT(inp);
295 DDP_ASSERT_LOCKED(toep);
298 #ifndef USE_DDP_RX_FLOW_CONTROL
299 KASSERT(tp->rcv_wnd >= n, ("%s: negative window size", __func__));
302 CTR2(KTR_CXGBE, "%s: placed %u bytes before falling out of DDP",
304 while (toep->ddp.active_count > 0) {
305 MPASS(toep->ddp.active_id != -1);
306 db_idx = toep->ddp.active_id;
307 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
308 MPASS((toep->ddp.flags & db_flag) != 0);
309 db = &toep->ddp.db[db_idx];
311 copied = job->aio_received;
313 if (placed > job->uaiocb.aio_nbytes - copied)
314 placed = job->uaiocb.aio_nbytes - copied;
317 if (!aio_clear_cancel_function(job)) {
319 * Update the copied length for when
320 * t4_aio_cancel_active() completes this
323 job->aio_received += placed;
324 } else if (copied + placed != 0) {
326 "%s: completing %p (copied %ld, placed %lu)",
327 __func__, job, copied, placed);
328 /* XXX: This always completes if there is some data. */
329 aio_complete(job, copied + placed, 0);
330 } else if (aio_set_cancel_function(job, t4_aio_cancel_queued)) {
331 TAILQ_INSERT_HEAD(&toep->ddp.aiojobq, job, list);
332 toep->ddp.waiting_count++;
336 complete_ddp_buffer(toep, db, db_idx);
342 /* SET_TCB_FIELD sent as a ULP command looks like this */
343 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \
344 sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core))
346 /* RX_DATA_ACK sent as a ULP command looks like this */
347 #define LEN__RX_DATA_ACK_ULP (sizeof(struct ulp_txpkt) + \
348 sizeof(struct ulptx_idata) + sizeof(struct cpl_rx_data_ack_core))
351 mk_set_tcb_field_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep,
352 uint64_t word, uint64_t mask, uint64_t val)
354 struct ulptx_idata *ulpsc;
355 struct cpl_set_tcb_field_core *req;
357 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
358 ulpmc->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16));
360 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
361 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
362 ulpsc->len = htobe32(sizeof(*req));
364 req = (struct cpl_set_tcb_field_core *)(ulpsc + 1);
365 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, toep->tid));
366 req->reply_ctrl = htobe16(V_NO_REPLY(1) |
367 V_QUEUENO(toep->ofld_rxq->iq.abs_id));
368 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
369 req->mask = htobe64(mask);
370 req->val = htobe64(val);
372 ulpsc = (struct ulptx_idata *)(req + 1);
373 if (LEN__SET_TCB_FIELD_ULP % 16) {
374 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
375 ulpsc->len = htobe32(0);
382 mk_rx_data_ack_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep)
384 struct ulptx_idata *ulpsc;
385 struct cpl_rx_data_ack_core *req;
387 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
388 ulpmc->len = htobe32(howmany(LEN__RX_DATA_ACK_ULP, 16));
390 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
391 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
392 ulpsc->len = htobe32(sizeof(*req));
394 req = (struct cpl_rx_data_ack_core *)(ulpsc + 1);
395 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_RX_DATA_ACK, toep->tid));
396 req->credit_dack = htobe32(F_RX_MODULATE_RX);
398 ulpsc = (struct ulptx_idata *)(req + 1);
399 if (LEN__RX_DATA_ACK_ULP % 16) {
400 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
401 ulpsc->len = htobe32(0);
408 mk_update_tcb_for_ddp(struct adapter *sc, struct toepcb *toep, int db_idx,
409 struct pageset *ps, int offset, uint64_t ddp_flags, uint64_t ddp_flags_mask)
412 struct work_request_hdr *wrh;
413 struct ulp_txpkt *ulpmc;
416 KASSERT(db_idx == 0 || db_idx == 1,
417 ("%s: bad DDP buffer index %d", __func__, db_idx));
420 * We'll send a compound work request that has 3 SET_TCB_FIELDs and an
421 * RX_DATA_ACK (with RX_MODULATE to speed up delivery).
423 * The work request header is 16B and always ends at a 16B boundary.
424 * The ULPTX master commands that follow must all end at 16B boundaries
425 * too so we round up the size to 16.
427 len = sizeof(*wrh) + 3 * roundup2(LEN__SET_TCB_FIELD_ULP, 16) +
428 roundup2(LEN__RX_DATA_ACK_ULP, 16);
430 wr = alloc_wrqe(len, toep->ctrlq);
434 INIT_ULPTX_WRH(wrh, len, 1, 0); /* atomic */
435 ulpmc = (struct ulp_txpkt *)(wrh + 1);
437 /* Write the buffer's tag */
438 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
439 W_TCB_RX_DDP_BUF0_TAG + db_idx,
440 V_TCB_RX_DDP_BUF0_TAG(M_TCB_RX_DDP_BUF0_TAG),
441 V_TCB_RX_DDP_BUF0_TAG(ps->prsv.prsv_tag));
443 /* Update the current offset in the DDP buffer and its total length */
445 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
446 W_TCB_RX_DDP_BUF0_OFFSET,
447 V_TCB_RX_DDP_BUF0_OFFSET(M_TCB_RX_DDP_BUF0_OFFSET) |
448 V_TCB_RX_DDP_BUF0_LEN(M_TCB_RX_DDP_BUF0_LEN),
449 V_TCB_RX_DDP_BUF0_OFFSET(offset) |
450 V_TCB_RX_DDP_BUF0_LEN(ps->len));
452 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
453 W_TCB_RX_DDP_BUF1_OFFSET,
454 V_TCB_RX_DDP_BUF1_OFFSET(M_TCB_RX_DDP_BUF1_OFFSET) |
455 V_TCB_RX_DDP_BUF1_LEN((u64)M_TCB_RX_DDP_BUF1_LEN << 32),
456 V_TCB_RX_DDP_BUF1_OFFSET(offset) |
457 V_TCB_RX_DDP_BUF1_LEN((u64)ps->len << 32));
459 /* Update DDP flags */
460 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep, W_TCB_RX_DDP_FLAGS,
461 ddp_flags_mask, ddp_flags);
463 /* Gratuitous RX_DATA_ACK with RX_MODULATE set to speed up delivery. */
464 ulpmc = mk_rx_data_ack_ulp(ulpmc, toep);
470 handle_ddp_data(struct toepcb *toep, __be32 ddp_report, __be32 rcv_nxt, int len)
472 uint32_t report = be32toh(ddp_report);
474 struct inpcb *inp = toep->inp;
475 struct ddp_buffer *db;
482 db_idx = report & F_DDP_BUF_IDX ? 1 : 0;
484 if (__predict_false(!(report & F_DDP_INV)))
485 CXGBE_UNIMPLEMENTED("DDP buffer still valid");
488 so = inp_inpcbtosocket(inp);
492 KASSERT(toep->ddp.active_id == db_idx,
493 ("completed DDP buffer (%d) != active_id (%d) for tid %d", db_idx,
494 toep->ddp.active_id, toep->tid));
495 db = &toep->ddp.db[db_idx];
498 if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
500 * This can happen due to an administrative tcpdrop(8).
501 * Just fail the request with ECONNRESET.
503 CTR5(KTR_CXGBE, "%s: tid %u, seq 0x%x, len %d, inp_flags 0x%x",
504 __func__, toep->tid, be32toh(rcv_nxt), len, inp->inp_flags);
505 if (aio_clear_cancel_function(job))
506 ddp_complete_one(job, ECONNRESET);
513 * For RX_DDP_COMPLETE, len will be zero and rcv_nxt is the
514 * sequence number of the next byte to receive. The length of
515 * the data received for this message must be computed by
516 * comparing the new and old values of rcv_nxt.
518 * For RX_DATA_DDP, len might be non-zero, but it is only the
519 * length of the most recent DMA. It does not include the
520 * total length of the data received since the previous update
521 * for this DDP buffer. rcv_nxt is the sequence number of the
522 * first received byte from the most recent DMA.
524 len += be32toh(rcv_nxt) - tp->rcv_nxt;
526 tp->t_rcvtime = ticks;
527 #ifndef USE_DDP_RX_FLOW_CONTROL
528 KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
531 #ifdef VERBOSE_TRACES
532 CTR5(KTR_CXGBE, "%s: tid %u, DDP[%d] placed %d bytes (%#x)", __func__,
533 toep->tid, db_idx, len, report);
536 /* receive buffer autosize */
537 MPASS(toep->vnet == so->so_vnet);
538 CURVNET_SET(toep->vnet);
540 if (sb->sb_flags & SB_AUTOSIZE &&
541 V_tcp_do_autorcvbuf &&
542 sb->sb_hiwat < V_tcp_autorcvbuf_max &&
543 len > (sbspace(sb) / 8 * 7)) {
544 struct adapter *sc = td_adapter(toep->td);
545 unsigned int hiwat = sb->sb_hiwat;
546 unsigned int newsize = min(hiwat + sc->tt.autorcvbuf_inc,
547 V_tcp_autorcvbuf_max);
549 if (!sbreserve_locked(sb, newsize, so, NULL))
550 sb->sb_flags &= ~SB_AUTOSIZE;
556 if (db->cancel_pending) {
558 * Update the job's length but defer completion to the
561 job->aio_received += len;
563 } else if (!aio_clear_cancel_function(job)) {
565 * Update the copied length for when
566 * t4_aio_cancel_active() completes this request.
568 job->aio_received += len;
570 copied = job->aio_received;
571 #ifdef VERBOSE_TRACES
573 "%s: tid %u, completing %p (copied %ld, placed %d)",
574 __func__, toep->tid, job, copied, len);
576 aio_complete(job, copied + len, 0);
577 t4_rcvd(&toep->td->tod, tp);
581 complete_ddp_buffer(toep, db, db_idx);
582 if (toep->ddp.waiting_count > 0)
583 ddp_queue_toep(toep);
592 handle_ddp_indicate(struct toepcb *toep)
595 DDP_ASSERT_LOCKED(toep);
596 MPASS(toep->ddp.active_count == 0);
597 MPASS((toep->ddp.flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0);
598 if (toep->ddp.waiting_count == 0) {
600 * The pending requests that triggered the request for an
601 * an indicate were cancelled. Those cancels should have
602 * already disabled DDP. Just ignore this as the data is
603 * going into the socket buffer anyway.
607 CTR3(KTR_CXGBE, "%s: tid %d indicated (%d waiting)", __func__,
608 toep->tid, toep->ddp.waiting_count);
609 ddp_queue_toep(toep);
613 DDP_BUF0_INVALIDATED = 0x2,
617 CTASSERT(DDP_BUF0_INVALIDATED == CPL_COOKIE_DDP0);
620 do_ddp_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
622 struct adapter *sc = iq->adapter;
623 const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
624 unsigned int tid = GET_TID(cpl);
628 struct ddp_buffer *db;
632 if (cpl->status != CPL_ERR_NONE)
633 panic("XXX: tcp_rpl failed: %d", cpl->status);
635 toep = lookup_tid(sc, tid);
637 switch (cpl->cookie) {
638 case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF0_INVALIDATED):
639 case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF1_INVALIDATED):
641 * XXX: This duplicates a lot of code with handle_ddp_data().
643 db_idx = G_COOKIE(cpl->cookie) - DDP_BUF0_INVALIDATED;
644 MPASS(db_idx < nitems(toep->ddp.db));
647 db = &toep->ddp.db[db_idx];
650 * handle_ddp_data() should leave the job around until
651 * this callback runs once a cancel is pending.
654 MPASS(db->job != NULL);
655 MPASS(db->cancel_pending);
658 * XXX: It's not clear what happens if there is data
659 * placed when the buffer is invalidated. I suspect we
660 * need to read the TCB to see how much data was placed.
662 * For now this just pretends like nothing was placed.
664 * XXX: Note that if we did check the PCB we would need to
665 * also take care of updating the tp, etc.
668 copied = job->aio_received;
670 CTR2(KTR_CXGBE, "%s: cancelling %p", __func__, job);
673 CTR3(KTR_CXGBE, "%s: completing %p (copied %ld)",
674 __func__, job, copied);
675 aio_complete(job, copied, 0);
676 t4_rcvd(&toep->td->tod, intotcpcb(inp));
679 complete_ddp_buffer(toep, db, db_idx);
680 if (toep->ddp.waiting_count > 0)
681 ddp_queue_toep(toep);
686 panic("XXX: unknown tcb_rpl offset %#x, cookie %#x",
687 G_WORD(cpl->cookie), G_COOKIE(cpl->cookie));
694 handle_ddp_close(struct toepcb *toep, struct tcpcb *tp, __be32 rcv_nxt)
696 struct ddp_buffer *db;
699 unsigned int db_flag, db_idx;
702 INP_WLOCK_ASSERT(toep->inp);
703 DDP_ASSERT_LOCKED(toep);
705 len = be32toh(rcv_nxt) - tp->rcv_nxt;
708 while (toep->ddp.active_count > 0) {
709 MPASS(toep->ddp.active_id != -1);
710 db_idx = toep->ddp.active_id;
711 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
712 MPASS((toep->ddp.flags & db_flag) != 0);
713 db = &toep->ddp.db[db_idx];
715 copied = job->aio_received;
717 if (placed > job->uaiocb.aio_nbytes - copied)
718 placed = job->uaiocb.aio_nbytes - copied;
721 if (!aio_clear_cancel_function(job)) {
723 * Update the copied length for when
724 * t4_aio_cancel_active() completes this
727 job->aio_received += placed;
729 CTR4(KTR_CXGBE, "%s: tid %d completed buf %d len %d",
730 __func__, toep->tid, db_idx, placed);
731 aio_complete(job, copied + placed, 0);
734 complete_ddp_buffer(toep, db, db_idx);
738 ddp_complete_all(toep, 0);
741 #define DDP_ERR (F_DDP_PPOD_MISMATCH | F_DDP_LLIMIT_ERR | F_DDP_ULIMIT_ERR |\
742 F_DDP_PPOD_PARITY_ERR | F_DDP_PADDING_ERR | F_DDP_OFFSET_ERR |\
743 F_DDP_INVALID_TAG | F_DDP_COLOR_ERR | F_DDP_TID_MISMATCH |\
744 F_DDP_INVALID_PPOD | F_DDP_HDRCRC_ERR | F_DDP_DATACRC_ERR)
746 extern cpl_handler_t t4_cpl_handler[];
749 do_rx_data_ddp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
751 struct adapter *sc = iq->adapter;
752 const struct cpl_rx_data_ddp *cpl = (const void *)(rss + 1);
753 unsigned int tid = GET_TID(cpl);
755 struct toepcb *toep = lookup_tid(sc, tid);
757 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
758 KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
759 KASSERT(!(toep->flags & TPF_SYNQE),
760 ("%s: toep %p claims to be a synq entry", __func__, toep));
762 vld = be32toh(cpl->ddpvld);
763 if (__predict_false(vld & DDP_ERR)) {
764 panic("%s: DDP error 0x%x (tid %d, toep %p)",
765 __func__, vld, tid, toep);
768 if (ulp_mode(toep) == ULP_MODE_ISCSI) {
769 t4_cpl_handler[CPL_RX_ISCSI_DDP](iq, rss, m);
773 handle_ddp_data(toep, cpl->u.ddp_report, cpl->seq, be16toh(cpl->len));
779 do_rx_ddp_complete(struct sge_iq *iq, const struct rss_header *rss,
782 struct adapter *sc = iq->adapter;
783 const struct cpl_rx_ddp_complete *cpl = (const void *)(rss + 1);
784 unsigned int tid = GET_TID(cpl);
785 struct toepcb *toep = lookup_tid(sc, tid);
787 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
788 KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
789 KASSERT(!(toep->flags & TPF_SYNQE),
790 ("%s: toep %p claims to be a synq entry", __func__, toep));
792 handle_ddp_data(toep, cpl->ddp_report, cpl->rcv_nxt, 0);
798 enable_ddp(struct adapter *sc, struct toepcb *toep)
801 KASSERT((toep->ddp.flags & (DDP_ON | DDP_OK | DDP_SC_REQ)) == DDP_OK,
802 ("%s: toep %p has bad ddp_flags 0x%x",
803 __func__, toep, toep->ddp.flags));
805 CTR3(KTR_CXGBE, "%s: tid %u (time %u)",
806 __func__, toep->tid, time_uptime);
808 DDP_ASSERT_LOCKED(toep);
809 toep->ddp.flags |= DDP_SC_REQ;
810 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_RX_DDP_FLAGS,
811 V_TF_DDP_OFF(1) | V_TF_DDP_INDICATE_OUT(1) |
812 V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1) |
813 V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_BUF1_VALID(1),
814 V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1), 0, 0);
815 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
816 V_TF_RCV_COALESCE_ENABLE(1), 0, 0, 0);
820 calculate_hcf(int n1, int n2)
842 pages_to_nppods(int npages, int ddp_page_shift)
845 MPASS(ddp_page_shift >= PAGE_SHIFT);
847 return (howmany(npages >> (ddp_page_shift - PAGE_SHIFT), PPOD_PAGES));
851 alloc_page_pods(struct ppod_region *pr, u_int nppods, u_int pgsz_idx,
852 struct ppod_reservation *prsv)
854 vmem_addr_t addr; /* relative to start of region */
856 if (vmem_alloc(pr->pr_arena, PPOD_SZ(nppods), M_NOWAIT | M_FIRSTFIT,
860 CTR5(KTR_CXGBE, "%-17s arena %p, addr 0x%08x, nppods %d, pgsz %d",
861 __func__, pr->pr_arena, (uint32_t)addr & pr->pr_tag_mask,
862 nppods, 1 << pr->pr_page_shift[pgsz_idx]);
865 * The hardware tagmask includes an extra invalid bit but the arena was
866 * seeded with valid values only. An allocation out of this arena will
867 * fit inside the tagmask but won't have the invalid bit set.
869 MPASS((addr & pr->pr_tag_mask) == addr);
870 MPASS((addr & pr->pr_invalid_bit) == 0);
873 prsv->prsv_tag = V_PPOD_PGSZ(pgsz_idx) | addr;
874 prsv->prsv_nppods = nppods;
880 t4_alloc_page_pods_for_ps(struct ppod_region *pr, struct pageset *ps)
882 int i, hcf, seglen, idx, nppods;
883 struct ppod_reservation *prsv = &ps->prsv;
885 KASSERT(prsv->prsv_nppods == 0,
886 ("%s: page pods already allocated", __func__));
889 * The DDP page size is unrelated to the VM page size. We combine
890 * contiguous physical pages into larger segments to get the best DDP
891 * page size possible. This is the largest of the four sizes in
892 * A_ULP_RX_TDDP_PSZ that evenly divides the HCF of the segment sizes in
896 for (i = 0; i < ps->npages; i++) {
898 while (i < ps->npages - 1 &&
899 ps->pages[i]->phys_addr + PAGE_SIZE ==
900 ps->pages[i + 1]->phys_addr) {
905 hcf = calculate_hcf(hcf, seglen);
906 if (hcf < (1 << pr->pr_page_shift[1])) {
908 goto have_pgsz; /* give up, short circuit */
912 #define PR_PAGE_MASK(x) ((1 << pr->pr_page_shift[(x)]) - 1)
913 MPASS((hcf & PR_PAGE_MASK(0)) == 0); /* PAGE_SIZE is >= 4K everywhere */
914 for (idx = nitems(pr->pr_page_shift) - 1; idx > 0; idx--) {
915 if ((hcf & PR_PAGE_MASK(idx)) == 0)
921 MPASS(idx <= M_PPOD_PGSZ);
923 nppods = pages_to_nppods(ps->npages, pr->pr_page_shift[idx]);
924 if (alloc_page_pods(pr, nppods, idx, prsv) != 0)
926 MPASS(prsv->prsv_nppods > 0);
932 t4_alloc_page_pods_for_buf(struct ppod_region *pr, vm_offset_t buf, int len,
933 struct ppod_reservation *prsv)
935 int hcf, seglen, idx, npages, nppods;
936 uintptr_t start_pva, end_pva, pva, p1;
942 * The DDP page size is unrelated to the VM page size. We combine
943 * contiguous physical pages into larger segments to get the best DDP
944 * page size possible. This is the largest of the four sizes in
945 * A_ULP_RX_ISCSI_PSZ that evenly divides the HCF of the segment sizes
949 start_pva = trunc_page(buf);
950 end_pva = trunc_page(buf + len - 1);
952 while (pva <= end_pva) {
954 p1 = pmap_kextract(pva);
956 while (pva <= end_pva && p1 + seglen == pmap_kextract(pva)) {
961 hcf = calculate_hcf(hcf, seglen);
962 if (hcf < (1 << pr->pr_page_shift[1])) {
964 goto have_pgsz; /* give up, short circuit */
968 #define PR_PAGE_MASK(x) ((1 << pr->pr_page_shift[(x)]) - 1)
969 MPASS((hcf & PR_PAGE_MASK(0)) == 0); /* PAGE_SIZE is >= 4K everywhere */
970 for (idx = nitems(pr->pr_page_shift) - 1; idx > 0; idx--) {
971 if ((hcf & PR_PAGE_MASK(idx)) == 0)
977 MPASS(idx <= M_PPOD_PGSZ);
980 npages += (end_pva - start_pva) >> pr->pr_page_shift[idx];
981 nppods = howmany(npages, PPOD_PAGES);
982 if (alloc_page_pods(pr, nppods, idx, prsv) != 0)
984 MPASS(prsv->prsv_nppods > 0);
990 t4_free_page_pods(struct ppod_reservation *prsv)
992 struct ppod_region *pr = prsv->prsv_pr;
996 MPASS(prsv->prsv_nppods != 0);
998 addr = prsv->prsv_tag & pr->pr_tag_mask;
999 MPASS((addr & pr->pr_invalid_bit) == 0);
1001 CTR4(KTR_CXGBE, "%-17s arena %p, addr 0x%08x, nppods %d", __func__,
1002 pr->pr_arena, addr, prsv->prsv_nppods);
1004 vmem_free(pr->pr_arena, addr, PPOD_SZ(prsv->prsv_nppods));
1005 prsv->prsv_nppods = 0;
1008 #define NUM_ULP_TX_SC_IMM_PPODS (256 / PPOD_SIZE)
1011 t4_write_page_pods_for_ps(struct adapter *sc, struct sge_wrq *wrq, int tid,
1015 struct ulp_mem_io *ulpmc;
1016 struct ulptx_idata *ulpsc;
1017 struct pagepod *ppod;
1018 int i, j, k, n, chunk, len, ddp_pgsz, idx;
1021 struct ppod_reservation *prsv = &ps->prsv;
1022 struct ppod_region *pr = prsv->prsv_pr;
1024 KASSERT(!(ps->flags & PS_PPODS_WRITTEN),
1025 ("%s: page pods already written", __func__));
1026 MPASS(prsv->prsv_nppods > 0);
1028 cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
1030 cmd |= htobe32(F_ULP_MEMIO_ORDER);
1032 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
1033 ddp_pgsz = 1 << pr->pr_page_shift[G_PPOD_PGSZ(prsv->prsv_tag)];
1034 ppod_addr = pr->pr_start + (prsv->prsv_tag & pr->pr_tag_mask);
1035 for (i = 0; i < prsv->prsv_nppods; ppod_addr += chunk) {
1037 /* How many page pods are we writing in this cycle */
1038 n = min(prsv->prsv_nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
1040 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
1042 wr = alloc_wrqe(len, wrq);
1044 return (ENOMEM); /* ok to just bail out */
1047 INIT_ULPTX_WR(ulpmc, len, 0, 0);
1049 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
1050 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
1051 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
1053 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
1054 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
1055 ulpsc->len = htobe32(chunk);
1057 ppod = (struct pagepod *)(ulpsc + 1);
1058 for (j = 0; j < n; i++, j++, ppod++) {
1059 ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
1060 V_PPOD_TID(tid) | prsv->prsv_tag);
1061 ppod->len_offset = htobe64(V_PPOD_LEN(ps->len) |
1062 V_PPOD_OFST(ps->offset));
1064 idx = i * PPOD_PAGES * (ddp_pgsz / PAGE_SIZE);
1065 for (k = 0; k < nitems(ppod->addr); k++) {
1066 if (idx < ps->npages) {
1068 htobe64(ps->pages[idx]->phys_addr);
1069 idx += ddp_pgsz / PAGE_SIZE;
1074 "%s: tid %d ppod[%d]->addr[%d] = %p",
1075 __func__, toep->tid, i, k,
1076 htobe64(ppod->addr[k]));
1084 ps->flags |= PS_PPODS_WRITTEN;
1090 t4_write_page_pods_for_buf(struct adapter *sc, struct sge_wrq *wrq, int tid,
1091 struct ppod_reservation *prsv, vm_offset_t buf, int buflen)
1094 struct ulp_mem_io *ulpmc;
1095 struct ulptx_idata *ulpsc;
1096 struct pagepod *ppod;
1097 int i, j, k, n, chunk, len, ddp_pgsz;
1098 u_int ppod_addr, offset;
1100 struct ppod_region *pr = prsv->prsv_pr;
1101 uintptr_t end_pva, pva, pa;
1103 cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
1105 cmd |= htobe32(F_ULP_MEMIO_ORDER);
1107 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
1108 ddp_pgsz = 1 << pr->pr_page_shift[G_PPOD_PGSZ(prsv->prsv_tag)];
1109 offset = buf & PAGE_MASK;
1110 ppod_addr = pr->pr_start + (prsv->prsv_tag & pr->pr_tag_mask);
1111 pva = trunc_page(buf);
1112 end_pva = trunc_page(buf + buflen - 1);
1113 for (i = 0; i < prsv->prsv_nppods; ppod_addr += chunk) {
1115 /* How many page pods are we writing in this cycle */
1116 n = min(prsv->prsv_nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
1119 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
1121 wr = alloc_wrqe(len, wrq);
1123 return (ENOMEM); /* ok to just bail out */
1126 INIT_ULPTX_WR(ulpmc, len, 0, 0);
1128 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
1129 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
1130 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
1132 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
1133 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
1134 ulpsc->len = htobe32(chunk);
1136 ppod = (struct pagepod *)(ulpsc + 1);
1137 for (j = 0; j < n; i++, j++, ppod++) {
1138 ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
1140 (prsv->prsv_tag & ~V_PPOD_PGSZ(M_PPOD_PGSZ)));
1141 ppod->len_offset = htobe64(V_PPOD_LEN(buflen) |
1142 V_PPOD_OFST(offset));
1145 for (k = 0; k < nitems(ppod->addr); k++) {
1149 pa = pmap_kextract(pva);
1150 ppod->addr[k] = htobe64(pa);
1155 "%s: tid %d ppod[%d]->addr[%d] = %p",
1156 __func__, tid, i, k,
1157 htobe64(ppod->addr[k]));
1162 * Walk back 1 segment so that the first address in the
1163 * next pod is the same as the last one in the current
1172 MPASS(pva <= end_pva);
1178 * Prepare a pageset for DDP. This sets up page pods.
1181 prep_pageset(struct adapter *sc, struct toepcb *toep, struct pageset *ps)
1183 struct tom_data *td = sc->tom_softc;
1185 if (ps->prsv.prsv_nppods == 0 &&
1186 !t4_alloc_page_pods_for_ps(&td->pr, ps)) {
1189 if (!(ps->flags & PS_PPODS_WRITTEN) &&
1190 t4_write_page_pods_for_ps(sc, toep->ctrlq, toep->tid, ps) != 0) {
1198 t4_init_ppod_region(struct ppod_region *pr, struct t4_range *r, u_int psz,
1206 pr->pr_start = r->start;
1207 pr->pr_len = r->size;
1208 pr->pr_page_shift[0] = 12 + G_HPZ0(psz);
1209 pr->pr_page_shift[1] = 12 + G_HPZ1(psz);
1210 pr->pr_page_shift[2] = 12 + G_HPZ2(psz);
1211 pr->pr_page_shift[3] = 12 + G_HPZ3(psz);
1213 /* The SGL -> page pod algorithm requires the sizes to be in order. */
1214 for (i = 1; i < nitems(pr->pr_page_shift); i++) {
1215 if (pr->pr_page_shift[i] <= pr->pr_page_shift[i - 1])
1219 pr->pr_tag_mask = ((1 << fls(r->size)) - 1) & V_PPOD_TAG(M_PPOD_TAG);
1220 pr->pr_alias_mask = V_PPOD_TAG(M_PPOD_TAG) & ~pr->pr_tag_mask;
1221 if (pr->pr_tag_mask == 0 || pr->pr_alias_mask == 0)
1223 pr->pr_alias_shift = fls(pr->pr_tag_mask);
1224 pr->pr_invalid_bit = 1 << (pr->pr_alias_shift - 1);
1226 pr->pr_arena = vmem_create(name, 0, pr->pr_len, PPOD_SIZE, 0,
1227 M_FIRSTFIT | M_NOWAIT);
1228 if (pr->pr_arena == NULL)
1235 t4_free_ppod_region(struct ppod_region *pr)
1241 vmem_destroy(pr->pr_arena);
1242 bzero(pr, sizeof(*pr));
1246 pscmp(struct pageset *ps, struct vmspace *vm, vm_offset_t start, int npages,
1250 if (ps->start != start || ps->npages != npages ||
1251 ps->offset != pgoff || ps->len != len)
1254 return (ps->vm != vm || ps->vm_timestamp != vm->vm_map.timestamp);
1258 hold_aio(struct toepcb *toep, struct kaiocb *job, struct pageset **pps)
1262 vm_offset_t start, end, pgoff;
1266 DDP_ASSERT_LOCKED(toep);
1269 * The AIO subsystem will cancel and drain all requests before
1270 * permitting a process to exit or exec, so p_vmspace should
1273 vm = job->userproc->p_vmspace;
1275 start = (uintptr_t)job->uaiocb.aio_buf;
1276 pgoff = start & PAGE_MASK;
1277 end = round_page(start + job->uaiocb.aio_nbytes);
1278 start = trunc_page(start);
1280 if (end - start > MAX_DDP_BUFFER_SIZE) {
1282 * Truncate the request to a short read.
1283 * Alternatively, we could DDP in chunks to the larger
1284 * buffer, but that would be quite a bit more work.
1286 * When truncating, round the request down to avoid
1287 * crossing a cache line on the final transaction.
1289 end = rounddown2(start + MAX_DDP_BUFFER_SIZE, CACHE_LINE_SIZE);
1290 #ifdef VERBOSE_TRACES
1291 CTR4(KTR_CXGBE, "%s: tid %d, truncating size from %lu to %lu",
1292 __func__, toep->tid, (unsigned long)job->uaiocb.aio_nbytes,
1293 (unsigned long)(end - (start + pgoff)));
1294 job->uaiocb.aio_nbytes = end - (start + pgoff);
1296 end = round_page(end);
1299 n = atop(end - start);
1302 * Try to reuse a cached pageset.
1304 TAILQ_FOREACH(ps, &toep->ddp.cached_pagesets, link) {
1305 if (pscmp(ps, vm, start, n, pgoff,
1306 job->uaiocb.aio_nbytes) == 0) {
1307 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
1308 toep->ddp.cached_count--;
1315 * If there are too many cached pagesets to create a new one,
1316 * free a pageset before creating a new one.
1318 KASSERT(toep->ddp.active_count + toep->ddp.cached_count <=
1319 nitems(toep->ddp.db), ("%s: too many wired pagesets", __func__));
1320 if (toep->ddp.active_count + toep->ddp.cached_count ==
1321 nitems(toep->ddp.db)) {
1322 KASSERT(toep->ddp.cached_count > 0,
1323 ("no cached pageset to free"));
1324 ps = TAILQ_LAST(&toep->ddp.cached_pagesets, pagesetq);
1325 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
1326 toep->ddp.cached_count--;
1327 free_pageset(toep->td, ps);
1331 /* Create a new pageset. */
1332 ps = malloc(sizeof(*ps) + n * sizeof(vm_page_t), M_CXGBE, M_WAITOK |
1334 ps->pages = (vm_page_t *)(ps + 1);
1335 ps->vm_timestamp = map->timestamp;
1336 ps->npages = vm_fault_quick_hold_pages(map, start, end - start,
1337 VM_PROT_WRITE, ps->pages, n);
1340 if (ps->npages < 0) {
1345 KASSERT(ps->npages == n, ("hold_aio: page count mismatch: %d vs %d",
1349 ps->len = job->uaiocb.aio_nbytes;
1350 atomic_add_int(&vm->vm_refcnt, 1);
1354 CTR5(KTR_CXGBE, "%s: tid %d, new pageset %p for job %p, npages %d",
1355 __func__, toep->tid, ps, job, ps->npages);
1361 ddp_complete_all(struct toepcb *toep, int error)
1365 DDP_ASSERT_LOCKED(toep);
1366 while (!TAILQ_EMPTY(&toep->ddp.aiojobq)) {
1367 job = TAILQ_FIRST(&toep->ddp.aiojobq);
1368 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1369 toep->ddp.waiting_count--;
1370 if (aio_clear_cancel_function(job))
1371 ddp_complete_one(job, error);
1376 aio_ddp_cancel_one(struct kaiocb *job)
1381 * If this job had copied data out of the socket buffer before
1382 * it was cancelled, report it as a short read rather than an
1385 copied = job->aio_received;
1387 aio_complete(job, copied, 0);
1393 * Called when the main loop wants to requeue a job to retry it later.
1394 * Deals with the race of the job being cancelled while it was being
1398 aio_ddp_requeue_one(struct toepcb *toep, struct kaiocb *job)
1401 DDP_ASSERT_LOCKED(toep);
1402 if (!(toep->ddp.flags & DDP_DEAD) &&
1403 aio_set_cancel_function(job, t4_aio_cancel_queued)) {
1404 TAILQ_INSERT_HEAD(&toep->ddp.aiojobq, job, list);
1405 toep->ddp.waiting_count++;
1407 aio_ddp_cancel_one(job);
1411 aio_ddp_requeue(struct toepcb *toep)
1413 struct adapter *sc = td_adapter(toep->td);
1418 struct ddp_buffer *db;
1419 size_t copied, offset, resid;
1422 uint64_t ddp_flags, ddp_flags_mask;
1424 int buf_flag, db_idx, error;
1426 DDP_ASSERT_LOCKED(toep);
1429 if (toep->ddp.flags & DDP_DEAD) {
1430 MPASS(toep->ddp.waiting_count == 0);
1431 MPASS(toep->ddp.active_count == 0);
1435 if (toep->ddp.waiting_count == 0 ||
1436 toep->ddp.active_count == nitems(toep->ddp.db)) {
1440 job = TAILQ_FIRST(&toep->ddp.aiojobq);
1441 so = job->fd_file->f_data;
1445 /* We will never get anything unless we are or were connected. */
1446 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1448 ddp_complete_all(toep, ENOTCONN);
1452 KASSERT(toep->ddp.active_count == 0 || sbavail(sb) == 0,
1453 ("%s: pending sockbuf data and DDP is active", __func__));
1455 /* Abort if socket has reported problems. */
1456 /* XXX: Wait for any queued DDP's to finish and/or flush them? */
1457 if (so->so_error && sbavail(sb) == 0) {
1458 toep->ddp.waiting_count--;
1459 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1460 if (!aio_clear_cancel_function(job)) {
1466 * If this job has previously copied some data, report
1467 * a short read and leave the error to be reported by
1470 copied = job->aio_received;
1473 aio_complete(job, copied, 0);
1476 error = so->so_error;
1479 aio_complete(job, -1, error);
1484 * Door is closed. If there is pending data in the socket buffer,
1485 * deliver it. If there are pending DDP requests, wait for those
1486 * to complete. Once they have completed, return EOF reads.
1488 if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1490 if (toep->ddp.active_count != 0)
1492 ddp_complete_all(toep, 0);
1497 * If DDP is not enabled and there is no pending socket buffer
1498 * data, try to enable DDP.
1500 if (sbavail(sb) == 0 && (toep->ddp.flags & DDP_ON) == 0) {
1504 * Wait for the card to ACK that DDP is enabled before
1505 * queueing any buffers. Currently this waits for an
1506 * indicate to arrive. This could use a TCB_SET_FIELD_RPL
1507 * message to know that DDP was enabled instead of waiting
1508 * for the indicate which would avoid copying the indicate
1509 * if no data is pending.
1511 * XXX: Might want to limit the indicate size to the size
1512 * of the first queued request.
1514 if ((toep->ddp.flags & DDP_SC_REQ) == 0)
1515 enable_ddp(sc, toep);
1521 * If another thread is queueing a buffer for DDP, let it
1522 * drain any work and return.
1524 if (toep->ddp.queueing != NULL)
1527 /* Take the next job to prep it for DDP. */
1528 toep->ddp.waiting_count--;
1529 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1530 if (!aio_clear_cancel_function(job))
1532 toep->ddp.queueing = job;
1534 /* NB: This drops DDP_LOCK while it holds the backing VM pages. */
1535 error = hold_aio(toep, job, &ps);
1537 ddp_complete_one(job, error);
1538 toep->ddp.queueing = NULL;
1543 if (so->so_error && sbavail(sb) == 0) {
1544 copied = job->aio_received;
1547 recycle_pageset(toep, ps);
1548 aio_complete(job, copied, 0);
1549 toep->ddp.queueing = NULL;
1553 error = so->so_error;
1556 recycle_pageset(toep, ps);
1557 aio_complete(job, -1, error);
1558 toep->ddp.queueing = NULL;
1562 if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1564 recycle_pageset(toep, ps);
1565 if (toep->ddp.active_count != 0) {
1567 * The door is closed, but there are still pending
1568 * DDP buffers. Requeue. These jobs will all be
1569 * completed once those buffers drain.
1571 aio_ddp_requeue_one(toep, job);
1572 toep->ddp.queueing = NULL;
1575 ddp_complete_one(job, 0);
1576 ddp_complete_all(toep, 0);
1577 toep->ddp.queueing = NULL;
1583 * If the toep is dead, there shouldn't be any data in the socket
1584 * buffer, so the above case should have handled this.
1586 MPASS(!(toep->ddp.flags & DDP_DEAD));
1589 * If there is pending data in the socket buffer (either
1590 * from before the requests were queued or a DDP indicate),
1591 * copy those mbufs out directly.
1594 offset = ps->offset + job->aio_received;
1595 MPASS(job->aio_received <= job->uaiocb.aio_nbytes);
1596 resid = job->uaiocb.aio_nbytes - job->aio_received;
1598 KASSERT(m == NULL || toep->ddp.active_count == 0,
1599 ("%s: sockbuf data with active DDP", __func__));
1600 while (m != NULL && resid > 0) {
1601 struct iovec iov[1];
1605 iov[0].iov_base = mtod(m, void *);
1606 iov[0].iov_len = m->m_len;
1607 if (iov[0].iov_len > resid)
1608 iov[0].iov_len = resid;
1612 uio.uio_resid = iov[0].iov_len;
1613 uio.uio_segflg = UIO_SYSSPACE;
1614 uio.uio_rw = UIO_WRITE;
1615 error = uiomove_fromphys(ps->pages, offset + copied,
1616 uio.uio_resid, &uio);
1617 MPASS(error == 0 && uio.uio_resid == 0);
1618 copied += uio.uio_offset;
1619 resid -= uio.uio_offset;
1623 sbdrop_locked(sb, copied);
1624 job->aio_received += copied;
1626 copied = job->aio_received;
1627 inp = sotoinpcb(so);
1628 if (!INP_TRY_WLOCK(inp)) {
1630 * The reference on the socket file descriptor in
1631 * the AIO job should keep 'sb' and 'inp' stable.
1632 * Our caller has a reference on the 'toep' that
1642 * If the socket has been closed, we should detect
1643 * that and complete this request if needed on
1644 * the next trip around the loop.
1647 t4_rcvd_locked(&toep->td->tod, intotcpcb(inp));
1649 if (resid == 0 || toep->ddp.flags & DDP_DEAD) {
1651 * We filled the entire buffer with socket
1652 * data, DDP is not being used, or the socket
1653 * is being shut down, so complete the
1657 recycle_pageset(toep, ps);
1658 aio_complete(job, copied, 0);
1659 toep->ddp.queueing = NULL;
1664 * If DDP is not enabled, requeue this request and restart.
1665 * This will either enable DDP or wait for more data to
1666 * arrive on the socket buffer.
1668 if ((toep->ddp.flags & (DDP_ON | DDP_SC_REQ)) != DDP_ON) {
1670 recycle_pageset(toep, ps);
1671 aio_ddp_requeue_one(toep, job);
1672 toep->ddp.queueing = NULL;
1677 * An indicate might have arrived and been added to
1678 * the socket buffer while it was unlocked after the
1679 * copy to lock the INP. If so, restart the copy.
1681 if (sbavail(sb) != 0)
1686 if (prep_pageset(sc, toep, ps) == 0) {
1687 recycle_pageset(toep, ps);
1688 aio_ddp_requeue_one(toep, job);
1689 toep->ddp.queueing = NULL;
1692 * XXX: Need to retry this later. Mostly need a trigger
1693 * when page pods are freed up.
1695 printf("%s: prep_pageset failed\n", __func__);
1699 /* Determine which DDP buffer to use. */
1700 if (toep->ddp.db[0].job == NULL) {
1703 MPASS(toep->ddp.db[1].job == NULL);
1710 ddp_flags |= V_TF_DDP_BUF0_VALID(1);
1711 if (so->so_state & SS_NBIO)
1712 ddp_flags |= V_TF_DDP_BUF0_FLUSH(1);
1713 ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE0(1) |
1714 V_TF_DDP_PUSH_DISABLE_0(1) | V_TF_DDP_PSHF_ENABLE_0(1) |
1715 V_TF_DDP_BUF0_FLUSH(1) | V_TF_DDP_BUF0_VALID(1);
1716 buf_flag = DDP_BUF0_ACTIVE;
1718 ddp_flags |= V_TF_DDP_BUF1_VALID(1);
1719 if (so->so_state & SS_NBIO)
1720 ddp_flags |= V_TF_DDP_BUF1_FLUSH(1);
1721 ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE1(1) |
1722 V_TF_DDP_PUSH_DISABLE_1(1) | V_TF_DDP_PSHF_ENABLE_1(1) |
1723 V_TF_DDP_BUF1_FLUSH(1) | V_TF_DDP_BUF1_VALID(1);
1724 buf_flag = DDP_BUF1_ACTIVE;
1726 MPASS((toep->ddp.flags & buf_flag) == 0);
1727 if ((toep->ddp.flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0) {
1729 MPASS(toep->ddp.active_id == -1);
1730 MPASS(toep->ddp.active_count == 0);
1731 ddp_flags_mask |= V_TF_DDP_ACTIVE_BUF(1);
1735 * The TID for this connection should still be valid. If DDP_DEAD
1736 * is set, SBS_CANTRCVMORE should be set, so we shouldn't be
1737 * this far anyway. Even if the socket is closing on the other
1738 * end, the AIO job holds a reference on this end of the socket
1739 * which will keep it open and keep the TCP PCB attached until
1740 * after the job is completed.
1742 wr = mk_update_tcb_for_ddp(sc, toep, db_idx, ps, job->aio_received,
1743 ddp_flags, ddp_flags_mask);
1745 recycle_pageset(toep, ps);
1746 aio_ddp_requeue_one(toep, job);
1747 toep->ddp.queueing = NULL;
1750 * XXX: Need a way to kick a retry here.
1752 * XXX: We know the fixed size needed and could
1753 * preallocate this using a blocking request at the
1754 * start of the task to avoid having to handle this
1757 printf("%s: mk_update_tcb_for_ddp failed\n", __func__);
1761 if (!aio_set_cancel_function(job, t4_aio_cancel_active)) {
1763 recycle_pageset(toep, ps);
1764 aio_ddp_cancel_one(job);
1765 toep->ddp.queueing = NULL;
1769 #ifdef VERBOSE_TRACES
1771 "%s: tid %u, scheduling %p for DDP[%d] (flags %#lx/%#lx)", __func__,
1772 toep->tid, job, db_idx, ddp_flags, ddp_flags_mask);
1774 /* Give the chip the go-ahead. */
1776 db = &toep->ddp.db[db_idx];
1777 db->cancel_pending = 0;
1780 toep->ddp.queueing = NULL;
1781 toep->ddp.flags |= buf_flag;
1782 toep->ddp.active_count++;
1783 if (toep->ddp.active_count == 1) {
1784 MPASS(toep->ddp.active_id == -1);
1785 toep->ddp.active_id = db_idx;
1786 CTR2(KTR_CXGBE, "%s: ddp_active_id = %d", __func__,
1787 toep->ddp.active_id);
1793 ddp_queue_toep(struct toepcb *toep)
1796 DDP_ASSERT_LOCKED(toep);
1797 if (toep->ddp.flags & DDP_TASK_ACTIVE)
1799 toep->ddp.flags |= DDP_TASK_ACTIVE;
1801 soaio_enqueue(&toep->ddp.requeue_task);
1805 aio_ddp_requeue_task(void *context, int pending)
1807 struct toepcb *toep = context;
1810 aio_ddp_requeue(toep);
1811 toep->ddp.flags &= ~DDP_TASK_ACTIVE;
1818 t4_aio_cancel_active(struct kaiocb *job)
1820 struct socket *so = job->fd_file->f_data;
1821 struct tcpcb *tp = so_sototcpcb(so);
1822 struct toepcb *toep = tp->t_toe;
1823 struct adapter *sc = td_adapter(toep->td);
1824 uint64_t valid_flag;
1828 if (aio_cancel_cleared(job)) {
1830 aio_ddp_cancel_one(job);
1834 for (i = 0; i < nitems(toep->ddp.db); i++) {
1835 if (toep->ddp.db[i].job == job) {
1836 /* Should only ever get one cancel request for a job. */
1837 MPASS(toep->ddp.db[i].cancel_pending == 0);
1840 * Invalidate this buffer. It will be
1841 * cancelled or partially completed once the
1842 * card ACKs the invalidate.
1844 valid_flag = i == 0 ? V_TF_DDP_BUF0_VALID(1) :
1845 V_TF_DDP_BUF1_VALID(1);
1846 t4_set_tcb_field(sc, toep->ctrlq, toep,
1847 W_TCB_RX_DDP_FLAGS, valid_flag, 0, 1,
1848 i + DDP_BUF0_INVALIDATED);
1849 toep->ddp.db[i].cancel_pending = 1;
1850 CTR2(KTR_CXGBE, "%s: request %p marked pending",
1859 t4_aio_cancel_queued(struct kaiocb *job)
1861 struct socket *so = job->fd_file->f_data;
1862 struct tcpcb *tp = so_sototcpcb(so);
1863 struct toepcb *toep = tp->t_toe;
1866 if (!aio_cancel_cleared(job)) {
1867 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1868 toep->ddp.waiting_count--;
1869 if (toep->ddp.waiting_count == 0)
1870 ddp_queue_toep(toep);
1872 CTR2(KTR_CXGBE, "%s: request %p cancelled", __func__, job);
1875 aio_ddp_cancel_one(job);
1879 t4_aio_queue_ddp(struct socket *so, struct kaiocb *job)
1881 struct tcpcb *tp = so_sototcpcb(so);
1882 struct toepcb *toep = tp->t_toe;
1885 /* Ignore writes. */
1886 if (job->uaiocb.aio_lio_opcode != LIO_READ)
1887 return (EOPNOTSUPP);
1892 * XXX: Think about possibly returning errors for ENOTCONN,
1893 * etc. Perhaps the caller would only queue the request
1894 * if it failed with EOPNOTSUPP?
1897 #ifdef VERBOSE_TRACES
1898 CTR3(KTR_CXGBE, "%s: queueing %p for tid %u", __func__, job, toep->tid);
1900 if (!aio_set_cancel_function(job, t4_aio_cancel_queued))
1901 panic("new job was cancelled");
1902 TAILQ_INSERT_TAIL(&toep->ddp.aiojobq, job, list);
1903 toep->ddp.waiting_count++;
1904 toep->ddp.flags |= DDP_OK;
1907 * Try to handle this request synchronously. If this has
1908 * to block because the task is running, it will just bail
1909 * and let the task handle it instead.
1911 aio_ddp_requeue(toep);
1917 t4_ddp_mod_load(void)
1920 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, do_ddp_tcb_rpl,
1922 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, do_ddp_tcb_rpl,
1924 t4_register_cpl_handler(CPL_RX_DATA_DDP, do_rx_data_ddp);
1925 t4_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_rx_ddp_complete);
1926 TAILQ_INIT(&ddp_orphan_pagesets);
1927 mtx_init(&ddp_orphan_pagesets_lock, "ddp orphans", NULL, MTX_DEF);
1928 TASK_INIT(&ddp_orphan_task, 0, ddp_free_orphan_pagesets, NULL);
1932 t4_ddp_mod_unload(void)
1935 taskqueue_drain(taskqueue_thread, &ddp_orphan_task);
1936 MPASS(TAILQ_EMPTY(&ddp_orphan_pagesets));
1937 mtx_destroy(&ddp_orphan_pagesets_lock);
1938 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, NULL, CPL_COOKIE_DDP0);
1939 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, NULL, CPL_COOKIE_DDP1);
1940 t4_register_cpl_handler(CPL_RX_DATA_DDP, NULL);
1941 t4_register_cpl_handler(CPL_RX_DDP_COMPLETE, NULL);