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++) {
118 vm_page_unwire(p, PQ_INACTIVE);
121 mtx_lock(&ddp_orphan_pagesets_lock);
122 TAILQ_INSERT_TAIL(&ddp_orphan_pagesets, ps, link);
123 taskqueue_enqueue(taskqueue_thread, &ddp_orphan_task);
124 mtx_unlock(&ddp_orphan_pagesets_lock);
128 ddp_free_orphan_pagesets(void *context, int pending)
132 mtx_lock(&ddp_orphan_pagesets_lock);
133 while (!TAILQ_EMPTY(&ddp_orphan_pagesets)) {
134 ps = TAILQ_FIRST(&ddp_orphan_pagesets);
135 TAILQ_REMOVE(&ddp_orphan_pagesets, ps, link);
136 mtx_unlock(&ddp_orphan_pagesets_lock);
138 vmspace_free(ps->vm);
140 mtx_lock(&ddp_orphan_pagesets_lock);
142 mtx_unlock(&ddp_orphan_pagesets_lock);
146 recycle_pageset(struct toepcb *toep, struct pageset *ps)
149 DDP_ASSERT_LOCKED(toep);
150 if (!(toep->ddp.flags & DDP_DEAD)) {
151 KASSERT(toep->ddp.cached_count + toep->ddp.active_count <
152 nitems(toep->ddp.db), ("too many wired pagesets"));
153 TAILQ_INSERT_HEAD(&toep->ddp.cached_pagesets, ps, link);
154 toep->ddp.cached_count++;
156 free_pageset(toep->td, ps);
160 ddp_complete_one(struct kaiocb *job, int error)
165 * If this job had copied data out of the socket buffer before
166 * it was cancelled, report it as a short read rather than an
169 copied = job->aio_received;
170 if (copied != 0 || error == 0)
171 aio_complete(job, copied, 0);
173 aio_complete(job, -1, error);
177 free_ddp_buffer(struct tom_data *td, struct ddp_buffer *db)
182 * XXX: If we are un-offloading the socket then we
183 * should requeue these on the socket somehow. If we
184 * got a FIN from the remote end, then this completes
185 * any remaining requests with an EOF read.
187 if (!aio_clear_cancel_function(db->job))
188 ddp_complete_one(db->job, 0);
192 free_pageset(td, db->ps);
196 ddp_init_toep(struct toepcb *toep)
199 TAILQ_INIT(&toep->ddp.aiojobq);
200 TASK_INIT(&toep->ddp.requeue_task, 0, aio_ddp_requeue_task, toep);
201 toep->ddp.flags = DDP_OK;
202 toep->ddp.active_id = -1;
203 mtx_init(&toep->ddp.lock, "t4 ddp", NULL, MTX_DEF);
207 ddp_uninit_toep(struct toepcb *toep)
210 mtx_destroy(&toep->ddp.lock);
214 release_ddp_resources(struct toepcb *toep)
220 toep->ddp.flags |= DDP_DEAD;
221 for (i = 0; i < nitems(toep->ddp.db); i++) {
222 free_ddp_buffer(toep->td, &toep->ddp.db[i]);
224 while ((ps = TAILQ_FIRST(&toep->ddp.cached_pagesets)) != NULL) {
225 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
226 free_pageset(toep->td, ps);
228 ddp_complete_all(toep, 0);
234 ddp_assert_empty(struct toepcb *toep)
238 MPASS(!(toep->ddp.flags & DDP_TASK_ACTIVE));
239 for (i = 0; i < nitems(toep->ddp.db); i++) {
240 MPASS(toep->ddp.db[i].job == NULL);
241 MPASS(toep->ddp.db[i].ps == NULL);
243 MPASS(TAILQ_EMPTY(&toep->ddp.cached_pagesets));
244 MPASS(TAILQ_EMPTY(&toep->ddp.aiojobq));
249 complete_ddp_buffer(struct toepcb *toep, struct ddp_buffer *db,
252 unsigned int db_flag;
254 toep->ddp.active_count--;
255 if (toep->ddp.active_id == db_idx) {
256 if (toep->ddp.active_count == 0) {
257 KASSERT(toep->ddp.db[db_idx ^ 1].job == NULL,
258 ("%s: active_count mismatch", __func__));
259 toep->ddp.active_id = -1;
261 toep->ddp.active_id ^= 1;
262 #ifdef VERBOSE_TRACES
263 CTR3(KTR_CXGBE, "%s: tid %u, ddp_active_id = %d", __func__,
264 toep->tid, toep->ddp.active_id);
267 KASSERT(toep->ddp.active_count != 0 &&
268 toep->ddp.active_id != -1,
269 ("%s: active count mismatch", __func__));
272 db->cancel_pending = 0;
274 recycle_pageset(toep, db->ps);
277 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
278 KASSERT(toep->ddp.flags & db_flag,
279 ("%s: DDP buffer not active. toep %p, ddp_flags 0x%x",
280 __func__, toep, toep->ddp.flags));
281 toep->ddp.flags &= ~db_flag;
284 /* XXX: handle_ddp_data code duplication */
286 insert_ddp_data(struct toepcb *toep, uint32_t n)
288 struct inpcb *inp = toep->inp;
289 struct tcpcb *tp = intotcpcb(inp);
290 struct ddp_buffer *db;
294 unsigned int db_flag, db_idx;
296 INP_WLOCK_ASSERT(inp);
297 DDP_ASSERT_LOCKED(toep);
300 #ifndef USE_DDP_RX_FLOW_CONTROL
301 KASSERT(tp->rcv_wnd >= n, ("%s: negative window size", __func__));
304 CTR2(KTR_CXGBE, "%s: placed %u bytes before falling out of DDP",
306 while (toep->ddp.active_count > 0) {
307 MPASS(toep->ddp.active_id != -1);
308 db_idx = toep->ddp.active_id;
309 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
310 MPASS((toep->ddp.flags & db_flag) != 0);
311 db = &toep->ddp.db[db_idx];
313 copied = job->aio_received;
315 if (placed > job->uaiocb.aio_nbytes - copied)
316 placed = job->uaiocb.aio_nbytes - copied;
319 if (!aio_clear_cancel_function(job)) {
321 * Update the copied length for when
322 * t4_aio_cancel_active() completes this
325 job->aio_received += placed;
326 } else if (copied + placed != 0) {
328 "%s: completing %p (copied %ld, placed %lu)",
329 __func__, job, copied, placed);
330 /* XXX: This always completes if there is some data. */
331 aio_complete(job, copied + placed, 0);
332 } else if (aio_set_cancel_function(job, t4_aio_cancel_queued)) {
333 TAILQ_INSERT_HEAD(&toep->ddp.aiojobq, job, list);
334 toep->ddp.waiting_count++;
338 complete_ddp_buffer(toep, db, db_idx);
344 /* SET_TCB_FIELD sent as a ULP command looks like this */
345 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \
346 sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core))
348 /* RX_DATA_ACK sent as a ULP command looks like this */
349 #define LEN__RX_DATA_ACK_ULP (sizeof(struct ulp_txpkt) + \
350 sizeof(struct ulptx_idata) + sizeof(struct cpl_rx_data_ack_core))
353 mk_set_tcb_field_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep,
354 uint64_t word, uint64_t mask, uint64_t val)
356 struct ulptx_idata *ulpsc;
357 struct cpl_set_tcb_field_core *req;
359 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
360 ulpmc->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16));
362 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
363 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
364 ulpsc->len = htobe32(sizeof(*req));
366 req = (struct cpl_set_tcb_field_core *)(ulpsc + 1);
367 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, toep->tid));
368 req->reply_ctrl = htobe16(V_NO_REPLY(1) |
369 V_QUEUENO(toep->ofld_rxq->iq.abs_id));
370 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
371 req->mask = htobe64(mask);
372 req->val = htobe64(val);
374 ulpsc = (struct ulptx_idata *)(req + 1);
375 if (LEN__SET_TCB_FIELD_ULP % 16) {
376 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
377 ulpsc->len = htobe32(0);
384 mk_rx_data_ack_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep)
386 struct ulptx_idata *ulpsc;
387 struct cpl_rx_data_ack_core *req;
389 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
390 ulpmc->len = htobe32(howmany(LEN__RX_DATA_ACK_ULP, 16));
392 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
393 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
394 ulpsc->len = htobe32(sizeof(*req));
396 req = (struct cpl_rx_data_ack_core *)(ulpsc + 1);
397 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_RX_DATA_ACK, toep->tid));
398 req->credit_dack = htobe32(F_RX_MODULATE_RX);
400 ulpsc = (struct ulptx_idata *)(req + 1);
401 if (LEN__RX_DATA_ACK_ULP % 16) {
402 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
403 ulpsc->len = htobe32(0);
410 mk_update_tcb_for_ddp(struct adapter *sc, struct toepcb *toep, int db_idx,
411 struct pageset *ps, int offset, uint64_t ddp_flags, uint64_t ddp_flags_mask)
414 struct work_request_hdr *wrh;
415 struct ulp_txpkt *ulpmc;
418 KASSERT(db_idx == 0 || db_idx == 1,
419 ("%s: bad DDP buffer index %d", __func__, db_idx));
422 * We'll send a compound work request that has 3 SET_TCB_FIELDs and an
423 * RX_DATA_ACK (with RX_MODULATE to speed up delivery).
425 * The work request header is 16B and always ends at a 16B boundary.
426 * The ULPTX master commands that follow must all end at 16B boundaries
427 * too so we round up the size to 16.
429 len = sizeof(*wrh) + 3 * roundup2(LEN__SET_TCB_FIELD_ULP, 16) +
430 roundup2(LEN__RX_DATA_ACK_ULP, 16);
432 wr = alloc_wrqe(len, toep->ctrlq);
436 INIT_ULPTX_WRH(wrh, len, 1, 0); /* atomic */
437 ulpmc = (struct ulp_txpkt *)(wrh + 1);
439 /* Write the buffer's tag */
440 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
441 W_TCB_RX_DDP_BUF0_TAG + db_idx,
442 V_TCB_RX_DDP_BUF0_TAG(M_TCB_RX_DDP_BUF0_TAG),
443 V_TCB_RX_DDP_BUF0_TAG(ps->prsv.prsv_tag));
445 /* Update the current offset in the DDP buffer and its total length */
447 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
448 W_TCB_RX_DDP_BUF0_OFFSET,
449 V_TCB_RX_DDP_BUF0_OFFSET(M_TCB_RX_DDP_BUF0_OFFSET) |
450 V_TCB_RX_DDP_BUF0_LEN(M_TCB_RX_DDP_BUF0_LEN),
451 V_TCB_RX_DDP_BUF0_OFFSET(offset) |
452 V_TCB_RX_DDP_BUF0_LEN(ps->len));
454 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
455 W_TCB_RX_DDP_BUF1_OFFSET,
456 V_TCB_RX_DDP_BUF1_OFFSET(M_TCB_RX_DDP_BUF1_OFFSET) |
457 V_TCB_RX_DDP_BUF1_LEN((u64)M_TCB_RX_DDP_BUF1_LEN << 32),
458 V_TCB_RX_DDP_BUF1_OFFSET(offset) |
459 V_TCB_RX_DDP_BUF1_LEN((u64)ps->len << 32));
461 /* Update DDP flags */
462 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep, W_TCB_RX_DDP_FLAGS,
463 ddp_flags_mask, ddp_flags);
465 /* Gratuitous RX_DATA_ACK with RX_MODULATE set to speed up delivery. */
466 ulpmc = mk_rx_data_ack_ulp(ulpmc, toep);
472 handle_ddp_data(struct toepcb *toep, __be32 ddp_report, __be32 rcv_nxt, int len)
474 uint32_t report = be32toh(ddp_report);
476 struct inpcb *inp = toep->inp;
477 struct ddp_buffer *db;
484 db_idx = report & F_DDP_BUF_IDX ? 1 : 0;
486 if (__predict_false(!(report & F_DDP_INV)))
487 CXGBE_UNIMPLEMENTED("DDP buffer still valid");
490 so = inp_inpcbtosocket(inp);
494 KASSERT(toep->ddp.active_id == db_idx,
495 ("completed DDP buffer (%d) != active_id (%d) for tid %d", db_idx,
496 toep->ddp.active_id, toep->tid));
497 db = &toep->ddp.db[db_idx];
500 if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
502 * This can happen due to an administrative tcpdrop(8).
503 * Just fail the request with ECONNRESET.
505 CTR5(KTR_CXGBE, "%s: tid %u, seq 0x%x, len %d, inp_flags 0x%x",
506 __func__, toep->tid, be32toh(rcv_nxt), len, inp->inp_flags);
507 if (aio_clear_cancel_function(job))
508 ddp_complete_one(job, ECONNRESET);
515 * For RX_DDP_COMPLETE, len will be zero and rcv_nxt is the
516 * sequence number of the next byte to receive. The length of
517 * the data received for this message must be computed by
518 * comparing the new and old values of rcv_nxt.
520 * For RX_DATA_DDP, len might be non-zero, but it is only the
521 * length of the most recent DMA. It does not include the
522 * total length of the data received since the previous update
523 * for this DDP buffer. rcv_nxt is the sequence number of the
524 * first received byte from the most recent DMA.
526 len += be32toh(rcv_nxt) - tp->rcv_nxt;
528 tp->t_rcvtime = ticks;
529 #ifndef USE_DDP_RX_FLOW_CONTROL
530 KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
533 #ifdef VERBOSE_TRACES
534 CTR5(KTR_CXGBE, "%s: tid %u, DDP[%d] placed %d bytes (%#x)", __func__,
535 toep->tid, db_idx, len, report);
538 /* receive buffer autosize */
539 MPASS(toep->vnet == so->so_vnet);
540 CURVNET_SET(toep->vnet);
542 if (sb->sb_flags & SB_AUTOSIZE &&
543 V_tcp_do_autorcvbuf &&
544 sb->sb_hiwat < V_tcp_autorcvbuf_max &&
545 len > (sbspace(sb) / 8 * 7)) {
546 struct adapter *sc = td_adapter(toep->td);
547 unsigned int hiwat = sb->sb_hiwat;
548 unsigned int newsize = min(hiwat + sc->tt.autorcvbuf_inc,
549 V_tcp_autorcvbuf_max);
551 if (!sbreserve_locked(sb, newsize, so, NULL))
552 sb->sb_flags &= ~SB_AUTOSIZE;
558 if (db->cancel_pending) {
560 * Update the job's length but defer completion to the
563 job->aio_received += len;
565 } else if (!aio_clear_cancel_function(job)) {
567 * Update the copied length for when
568 * t4_aio_cancel_active() completes this request.
570 job->aio_received += len;
572 copied = job->aio_received;
573 #ifdef VERBOSE_TRACES
575 "%s: tid %u, completing %p (copied %ld, placed %d)",
576 __func__, toep->tid, job, copied, len);
578 aio_complete(job, copied + len, 0);
579 t4_rcvd(&toep->td->tod, tp);
583 complete_ddp_buffer(toep, db, db_idx);
584 if (toep->ddp.waiting_count > 0)
585 ddp_queue_toep(toep);
594 handle_ddp_indicate(struct toepcb *toep)
597 DDP_ASSERT_LOCKED(toep);
598 MPASS(toep->ddp.active_count == 0);
599 MPASS((toep->ddp.flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0);
600 if (toep->ddp.waiting_count == 0) {
602 * The pending requests that triggered the request for an
603 * an indicate were cancelled. Those cancels should have
604 * already disabled DDP. Just ignore this as the data is
605 * going into the socket buffer anyway.
609 CTR3(KTR_CXGBE, "%s: tid %d indicated (%d waiting)", __func__,
610 toep->tid, toep->ddp.waiting_count);
611 ddp_queue_toep(toep);
615 DDP_BUF0_INVALIDATED = 0x2,
619 CTASSERT(DDP_BUF0_INVALIDATED == CPL_COOKIE_DDP0);
622 do_ddp_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
624 struct adapter *sc = iq->adapter;
625 const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
626 unsigned int tid = GET_TID(cpl);
630 struct ddp_buffer *db;
634 if (cpl->status != CPL_ERR_NONE)
635 panic("XXX: tcp_rpl failed: %d", cpl->status);
637 toep = lookup_tid(sc, tid);
639 switch (cpl->cookie) {
640 case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF0_INVALIDATED):
641 case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF1_INVALIDATED):
643 * XXX: This duplicates a lot of code with handle_ddp_data().
645 db_idx = G_COOKIE(cpl->cookie) - DDP_BUF0_INVALIDATED;
646 MPASS(db_idx < nitems(toep->ddp.db));
649 db = &toep->ddp.db[db_idx];
652 * handle_ddp_data() should leave the job around until
653 * this callback runs once a cancel is pending.
656 MPASS(db->job != NULL);
657 MPASS(db->cancel_pending);
660 * XXX: It's not clear what happens if there is data
661 * placed when the buffer is invalidated. I suspect we
662 * need to read the TCB to see how much data was placed.
664 * For now this just pretends like nothing was placed.
666 * XXX: Note that if we did check the PCB we would need to
667 * also take care of updating the tp, etc.
670 copied = job->aio_received;
672 CTR2(KTR_CXGBE, "%s: cancelling %p", __func__, job);
675 CTR3(KTR_CXGBE, "%s: completing %p (copied %ld)",
676 __func__, job, copied);
677 aio_complete(job, copied, 0);
678 t4_rcvd(&toep->td->tod, intotcpcb(inp));
681 complete_ddp_buffer(toep, db, db_idx);
682 if (toep->ddp.waiting_count > 0)
683 ddp_queue_toep(toep);
688 panic("XXX: unknown tcb_rpl offset %#x, cookie %#x",
689 G_WORD(cpl->cookie), G_COOKIE(cpl->cookie));
696 handle_ddp_close(struct toepcb *toep, struct tcpcb *tp, __be32 rcv_nxt)
698 struct ddp_buffer *db;
701 unsigned int db_flag, db_idx;
704 INP_WLOCK_ASSERT(toep->inp);
705 DDP_ASSERT_LOCKED(toep);
707 len = be32toh(rcv_nxt) - tp->rcv_nxt;
710 while (toep->ddp.active_count > 0) {
711 MPASS(toep->ddp.active_id != -1);
712 db_idx = toep->ddp.active_id;
713 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
714 MPASS((toep->ddp.flags & db_flag) != 0);
715 db = &toep->ddp.db[db_idx];
717 copied = job->aio_received;
719 if (placed > job->uaiocb.aio_nbytes - copied)
720 placed = job->uaiocb.aio_nbytes - copied;
723 if (!aio_clear_cancel_function(job)) {
725 * Update the copied length for when
726 * t4_aio_cancel_active() completes this
729 job->aio_received += placed;
731 CTR4(KTR_CXGBE, "%s: tid %d completed buf %d len %d",
732 __func__, toep->tid, db_idx, placed);
733 aio_complete(job, copied + placed, 0);
736 complete_ddp_buffer(toep, db, db_idx);
740 ddp_complete_all(toep, 0);
743 #define DDP_ERR (F_DDP_PPOD_MISMATCH | F_DDP_LLIMIT_ERR | F_DDP_ULIMIT_ERR |\
744 F_DDP_PPOD_PARITY_ERR | F_DDP_PADDING_ERR | F_DDP_OFFSET_ERR |\
745 F_DDP_INVALID_TAG | F_DDP_COLOR_ERR | F_DDP_TID_MISMATCH |\
746 F_DDP_INVALID_PPOD | F_DDP_HDRCRC_ERR | F_DDP_DATACRC_ERR)
748 extern cpl_handler_t t4_cpl_handler[];
751 do_rx_data_ddp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
753 struct adapter *sc = iq->adapter;
754 const struct cpl_rx_data_ddp *cpl = (const void *)(rss + 1);
755 unsigned int tid = GET_TID(cpl);
757 struct toepcb *toep = lookup_tid(sc, tid);
759 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
760 KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
761 KASSERT(!(toep->flags & TPF_SYNQE),
762 ("%s: toep %p claims to be a synq entry", __func__, toep));
764 vld = be32toh(cpl->ddpvld);
765 if (__predict_false(vld & DDP_ERR)) {
766 panic("%s: DDP error 0x%x (tid %d, toep %p)",
767 __func__, vld, tid, toep);
770 if (toep->ulp_mode == ULP_MODE_ISCSI) {
771 t4_cpl_handler[CPL_RX_ISCSI_DDP](iq, rss, m);
775 handle_ddp_data(toep, cpl->u.ddp_report, cpl->seq, be16toh(cpl->len));
781 do_rx_ddp_complete(struct sge_iq *iq, const struct rss_header *rss,
784 struct adapter *sc = iq->adapter;
785 const struct cpl_rx_ddp_complete *cpl = (const void *)(rss + 1);
786 unsigned int tid = GET_TID(cpl);
787 struct toepcb *toep = lookup_tid(sc, tid);
789 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
790 KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
791 KASSERT(!(toep->flags & TPF_SYNQE),
792 ("%s: toep %p claims to be a synq entry", __func__, toep));
794 handle_ddp_data(toep, cpl->ddp_report, cpl->rcv_nxt, 0);
800 enable_ddp(struct adapter *sc, struct toepcb *toep)
803 KASSERT((toep->ddp.flags & (DDP_ON | DDP_OK | DDP_SC_REQ)) == DDP_OK,
804 ("%s: toep %p has bad ddp_flags 0x%x",
805 __func__, toep, toep->ddp.flags));
807 CTR3(KTR_CXGBE, "%s: tid %u (time %u)",
808 __func__, toep->tid, time_uptime);
810 DDP_ASSERT_LOCKED(toep);
811 toep->ddp.flags |= DDP_SC_REQ;
812 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_RX_DDP_FLAGS,
813 V_TF_DDP_OFF(1) | V_TF_DDP_INDICATE_OUT(1) |
814 V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1) |
815 V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_BUF1_VALID(1),
816 V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1), 0, 0);
817 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
818 V_TF_RCV_COALESCE_ENABLE(1), 0, 0, 0);
822 calculate_hcf(int n1, int n2)
844 pages_to_nppods(int npages, int ddp_page_shift)
847 MPASS(ddp_page_shift >= PAGE_SHIFT);
849 return (howmany(npages >> (ddp_page_shift - PAGE_SHIFT), PPOD_PAGES));
853 alloc_page_pods(struct ppod_region *pr, u_int nppods, u_int pgsz_idx,
854 struct ppod_reservation *prsv)
856 vmem_addr_t addr; /* relative to start of region */
858 if (vmem_alloc(pr->pr_arena, PPOD_SZ(nppods), M_NOWAIT | M_FIRSTFIT,
862 CTR5(KTR_CXGBE, "%-17s arena %p, addr 0x%08x, nppods %d, pgsz %d",
863 __func__, pr->pr_arena, (uint32_t)addr & pr->pr_tag_mask,
864 nppods, 1 << pr->pr_page_shift[pgsz_idx]);
867 * The hardware tagmask includes an extra invalid bit but the arena was
868 * seeded with valid values only. An allocation out of this arena will
869 * fit inside the tagmask but won't have the invalid bit set.
871 MPASS((addr & pr->pr_tag_mask) == addr);
872 MPASS((addr & pr->pr_invalid_bit) == 0);
875 prsv->prsv_tag = V_PPOD_PGSZ(pgsz_idx) | addr;
876 prsv->prsv_nppods = nppods;
882 t4_alloc_page_pods_for_ps(struct ppod_region *pr, struct pageset *ps)
884 int i, hcf, seglen, idx, nppods;
885 struct ppod_reservation *prsv = &ps->prsv;
887 KASSERT(prsv->prsv_nppods == 0,
888 ("%s: page pods already allocated", __func__));
891 * The DDP page size is unrelated to the VM page size. We combine
892 * contiguous physical pages into larger segments to get the best DDP
893 * page size possible. This is the largest of the four sizes in
894 * A_ULP_RX_TDDP_PSZ that evenly divides the HCF of the segment sizes in
898 for (i = 0; i < ps->npages; i++) {
900 while (i < ps->npages - 1 &&
901 ps->pages[i]->phys_addr + PAGE_SIZE ==
902 ps->pages[i + 1]->phys_addr) {
907 hcf = calculate_hcf(hcf, seglen);
908 if (hcf < (1 << pr->pr_page_shift[1])) {
910 goto have_pgsz; /* give up, short circuit */
914 #define PR_PAGE_MASK(x) ((1 << pr->pr_page_shift[(x)]) - 1)
915 MPASS((hcf & PR_PAGE_MASK(0)) == 0); /* PAGE_SIZE is >= 4K everywhere */
916 for (idx = nitems(pr->pr_page_shift) - 1; idx > 0; idx--) {
917 if ((hcf & PR_PAGE_MASK(idx)) == 0)
923 MPASS(idx <= M_PPOD_PGSZ);
925 nppods = pages_to_nppods(ps->npages, pr->pr_page_shift[idx]);
926 if (alloc_page_pods(pr, nppods, idx, prsv) != 0)
928 MPASS(prsv->prsv_nppods > 0);
934 t4_alloc_page_pods_for_buf(struct ppod_region *pr, vm_offset_t buf, int len,
935 struct ppod_reservation *prsv)
937 int hcf, seglen, idx, npages, nppods;
938 uintptr_t start_pva, end_pva, pva, p1;
944 * The DDP page size is unrelated to the VM page size. We combine
945 * contiguous physical pages into larger segments to get the best DDP
946 * page size possible. This is the largest of the four sizes in
947 * A_ULP_RX_ISCSI_PSZ that evenly divides the HCF of the segment sizes
951 start_pva = trunc_page(buf);
952 end_pva = trunc_page(buf + len - 1);
954 while (pva <= end_pva) {
956 p1 = pmap_kextract(pva);
958 while (pva <= end_pva && p1 + seglen == pmap_kextract(pva)) {
963 hcf = calculate_hcf(hcf, seglen);
964 if (hcf < (1 << pr->pr_page_shift[1])) {
966 goto have_pgsz; /* give up, short circuit */
970 #define PR_PAGE_MASK(x) ((1 << pr->pr_page_shift[(x)]) - 1)
971 MPASS((hcf & PR_PAGE_MASK(0)) == 0); /* PAGE_SIZE is >= 4K everywhere */
972 for (idx = nitems(pr->pr_page_shift) - 1; idx > 0; idx--) {
973 if ((hcf & PR_PAGE_MASK(idx)) == 0)
979 MPASS(idx <= M_PPOD_PGSZ);
982 npages += (end_pva - start_pva) >> pr->pr_page_shift[idx];
983 nppods = howmany(npages, PPOD_PAGES);
984 if (alloc_page_pods(pr, nppods, idx, prsv) != 0)
986 MPASS(prsv->prsv_nppods > 0);
992 t4_free_page_pods(struct ppod_reservation *prsv)
994 struct ppod_region *pr = prsv->prsv_pr;
998 MPASS(prsv->prsv_nppods != 0);
1000 addr = prsv->prsv_tag & pr->pr_tag_mask;
1001 MPASS((addr & pr->pr_invalid_bit) == 0);
1003 CTR4(KTR_CXGBE, "%-17s arena %p, addr 0x%08x, nppods %d", __func__,
1004 pr->pr_arena, addr, prsv->prsv_nppods);
1006 vmem_free(pr->pr_arena, addr, PPOD_SZ(prsv->prsv_nppods));
1007 prsv->prsv_nppods = 0;
1010 #define NUM_ULP_TX_SC_IMM_PPODS (256 / PPOD_SIZE)
1013 t4_write_page_pods_for_ps(struct adapter *sc, struct sge_wrq *wrq, int tid,
1017 struct ulp_mem_io *ulpmc;
1018 struct ulptx_idata *ulpsc;
1019 struct pagepod *ppod;
1020 int i, j, k, n, chunk, len, ddp_pgsz, idx;
1023 struct ppod_reservation *prsv = &ps->prsv;
1024 struct ppod_region *pr = prsv->prsv_pr;
1026 KASSERT(!(ps->flags & PS_PPODS_WRITTEN),
1027 ("%s: page pods already written", __func__));
1028 MPASS(prsv->prsv_nppods > 0);
1030 cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
1032 cmd |= htobe32(F_ULP_MEMIO_ORDER);
1034 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
1035 ddp_pgsz = 1 << pr->pr_page_shift[G_PPOD_PGSZ(prsv->prsv_tag)];
1036 ppod_addr = pr->pr_start + (prsv->prsv_tag & pr->pr_tag_mask);
1037 for (i = 0; i < prsv->prsv_nppods; ppod_addr += chunk) {
1039 /* How many page pods are we writing in this cycle */
1040 n = min(prsv->prsv_nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
1042 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
1044 wr = alloc_wrqe(len, wrq);
1046 return (ENOMEM); /* ok to just bail out */
1049 INIT_ULPTX_WR(ulpmc, len, 0, 0);
1051 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
1052 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
1053 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
1055 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
1056 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
1057 ulpsc->len = htobe32(chunk);
1059 ppod = (struct pagepod *)(ulpsc + 1);
1060 for (j = 0; j < n; i++, j++, ppod++) {
1061 ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
1062 V_PPOD_TID(tid) | prsv->prsv_tag);
1063 ppod->len_offset = htobe64(V_PPOD_LEN(ps->len) |
1064 V_PPOD_OFST(ps->offset));
1066 idx = i * PPOD_PAGES * (ddp_pgsz / PAGE_SIZE);
1067 for (k = 0; k < nitems(ppod->addr); k++) {
1068 if (idx < ps->npages) {
1070 htobe64(ps->pages[idx]->phys_addr);
1071 idx += ddp_pgsz / PAGE_SIZE;
1076 "%s: tid %d ppod[%d]->addr[%d] = %p",
1077 __func__, toep->tid, i, k,
1078 htobe64(ppod->addr[k]));
1086 ps->flags |= PS_PPODS_WRITTEN;
1092 t4_write_page_pods_for_buf(struct adapter *sc, struct sge_wrq *wrq, int tid,
1093 struct ppod_reservation *prsv, vm_offset_t buf, int buflen)
1096 struct ulp_mem_io *ulpmc;
1097 struct ulptx_idata *ulpsc;
1098 struct pagepod *ppod;
1099 int i, j, k, n, chunk, len, ddp_pgsz;
1100 u_int ppod_addr, offset;
1102 struct ppod_region *pr = prsv->prsv_pr;
1103 uintptr_t end_pva, pva, pa;
1105 cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
1107 cmd |= htobe32(F_ULP_MEMIO_ORDER);
1109 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
1110 ddp_pgsz = 1 << pr->pr_page_shift[G_PPOD_PGSZ(prsv->prsv_tag)];
1111 offset = buf & PAGE_MASK;
1112 ppod_addr = pr->pr_start + (prsv->prsv_tag & pr->pr_tag_mask);
1113 pva = trunc_page(buf);
1114 end_pva = trunc_page(buf + buflen - 1);
1115 for (i = 0; i < prsv->prsv_nppods; ppod_addr += chunk) {
1117 /* How many page pods are we writing in this cycle */
1118 n = min(prsv->prsv_nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
1121 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
1123 wr = alloc_wrqe(len, wrq);
1125 return (ENOMEM); /* ok to just bail out */
1128 INIT_ULPTX_WR(ulpmc, len, 0, 0);
1130 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
1131 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
1132 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
1134 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
1135 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
1136 ulpsc->len = htobe32(chunk);
1138 ppod = (struct pagepod *)(ulpsc + 1);
1139 for (j = 0; j < n; i++, j++, ppod++) {
1140 ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
1142 (prsv->prsv_tag & ~V_PPOD_PGSZ(M_PPOD_PGSZ)));
1143 ppod->len_offset = htobe64(V_PPOD_LEN(buflen) |
1144 V_PPOD_OFST(offset));
1147 for (k = 0; k < nitems(ppod->addr); k++) {
1151 pa = pmap_kextract(pva);
1152 ppod->addr[k] = htobe64(pa);
1157 "%s: tid %d ppod[%d]->addr[%d] = %p",
1158 __func__, tid, i, k,
1159 htobe64(ppod->addr[k]));
1164 * Walk back 1 segment so that the first address in the
1165 * next pod is the same as the last one in the current
1174 MPASS(pva <= end_pva);
1180 * Prepare a pageset for DDP. This sets up page pods.
1183 prep_pageset(struct adapter *sc, struct toepcb *toep, struct pageset *ps)
1185 struct tom_data *td = sc->tom_softc;
1187 if (ps->prsv.prsv_nppods == 0 &&
1188 !t4_alloc_page_pods_for_ps(&td->pr, ps)) {
1191 if (!(ps->flags & PS_PPODS_WRITTEN) &&
1192 t4_write_page_pods_for_ps(sc, toep->ctrlq, toep->tid, ps) != 0) {
1200 t4_init_ppod_region(struct ppod_region *pr, struct t4_range *r, u_int psz,
1208 pr->pr_start = r->start;
1209 pr->pr_len = r->size;
1210 pr->pr_page_shift[0] = 12 + G_HPZ0(psz);
1211 pr->pr_page_shift[1] = 12 + G_HPZ1(psz);
1212 pr->pr_page_shift[2] = 12 + G_HPZ2(psz);
1213 pr->pr_page_shift[3] = 12 + G_HPZ3(psz);
1215 /* The SGL -> page pod algorithm requires the sizes to be in order. */
1216 for (i = 1; i < nitems(pr->pr_page_shift); i++) {
1217 if (pr->pr_page_shift[i] <= pr->pr_page_shift[i - 1])
1221 pr->pr_tag_mask = ((1 << fls(r->size)) - 1) & V_PPOD_TAG(M_PPOD_TAG);
1222 pr->pr_alias_mask = V_PPOD_TAG(M_PPOD_TAG) & ~pr->pr_tag_mask;
1223 if (pr->pr_tag_mask == 0 || pr->pr_alias_mask == 0)
1225 pr->pr_alias_shift = fls(pr->pr_tag_mask);
1226 pr->pr_invalid_bit = 1 << (pr->pr_alias_shift - 1);
1228 pr->pr_arena = vmem_create(name, 0, pr->pr_len, PPOD_SIZE, 0,
1229 M_FIRSTFIT | M_NOWAIT);
1230 if (pr->pr_arena == NULL)
1237 t4_free_ppod_region(struct ppod_region *pr)
1243 vmem_destroy(pr->pr_arena);
1244 bzero(pr, sizeof(*pr));
1248 pscmp(struct pageset *ps, struct vmspace *vm, vm_offset_t start, int npages,
1252 if (ps->start != start || ps->npages != npages ||
1253 ps->offset != pgoff || ps->len != len)
1256 return (ps->vm != vm || ps->vm_timestamp != vm->vm_map.timestamp);
1260 hold_aio(struct toepcb *toep, struct kaiocb *job, struct pageset **pps)
1264 vm_offset_t start, end, pgoff;
1268 DDP_ASSERT_LOCKED(toep);
1271 * The AIO subsystem will cancel and drain all requests before
1272 * permitting a process to exit or exec, so p_vmspace should
1275 vm = job->userproc->p_vmspace;
1277 start = (uintptr_t)job->uaiocb.aio_buf;
1278 pgoff = start & PAGE_MASK;
1279 end = round_page(start + job->uaiocb.aio_nbytes);
1280 start = trunc_page(start);
1282 if (end - start > MAX_DDP_BUFFER_SIZE) {
1284 * Truncate the request to a short read.
1285 * Alternatively, we could DDP in chunks to the larger
1286 * buffer, but that would be quite a bit more work.
1288 * When truncating, round the request down to avoid
1289 * crossing a cache line on the final transaction.
1291 end = rounddown2(start + MAX_DDP_BUFFER_SIZE, CACHE_LINE_SIZE);
1292 #ifdef VERBOSE_TRACES
1293 CTR4(KTR_CXGBE, "%s: tid %d, truncating size from %lu to %lu",
1294 __func__, toep->tid, (unsigned long)job->uaiocb.aio_nbytes,
1295 (unsigned long)(end - (start + pgoff)));
1296 job->uaiocb.aio_nbytes = end - (start + pgoff);
1298 end = round_page(end);
1301 n = atop(end - start);
1304 * Try to reuse a cached pageset.
1306 TAILQ_FOREACH(ps, &toep->ddp.cached_pagesets, link) {
1307 if (pscmp(ps, vm, start, n, pgoff,
1308 job->uaiocb.aio_nbytes) == 0) {
1309 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
1310 toep->ddp.cached_count--;
1317 * If there are too many cached pagesets to create a new one,
1318 * free a pageset before creating a new one.
1320 KASSERT(toep->ddp.active_count + toep->ddp.cached_count <=
1321 nitems(toep->ddp.db), ("%s: too many wired pagesets", __func__));
1322 if (toep->ddp.active_count + toep->ddp.cached_count ==
1323 nitems(toep->ddp.db)) {
1324 KASSERT(toep->ddp.cached_count > 0,
1325 ("no cached pageset to free"));
1326 ps = TAILQ_LAST(&toep->ddp.cached_pagesets, pagesetq);
1327 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
1328 toep->ddp.cached_count--;
1329 free_pageset(toep->td, ps);
1333 /* Create a new pageset. */
1334 ps = malloc(sizeof(*ps) + n * sizeof(vm_page_t), M_CXGBE, M_WAITOK |
1336 ps->pages = (vm_page_t *)(ps + 1);
1337 ps->vm_timestamp = map->timestamp;
1338 ps->npages = vm_fault_quick_hold_pages(map, start, end - start,
1339 VM_PROT_WRITE, ps->pages, n);
1342 if (ps->npages < 0) {
1347 KASSERT(ps->npages == n, ("hold_aio: page count mismatch: %d vs %d",
1351 ps->len = job->uaiocb.aio_nbytes;
1352 atomic_add_int(&vm->vm_refcnt, 1);
1356 CTR5(KTR_CXGBE, "%s: tid %d, new pageset %p for job %p, npages %d",
1357 __func__, toep->tid, ps, job, ps->npages);
1363 ddp_complete_all(struct toepcb *toep, int error)
1367 DDP_ASSERT_LOCKED(toep);
1368 while (!TAILQ_EMPTY(&toep->ddp.aiojobq)) {
1369 job = TAILQ_FIRST(&toep->ddp.aiojobq);
1370 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1371 toep->ddp.waiting_count--;
1372 if (aio_clear_cancel_function(job))
1373 ddp_complete_one(job, error);
1378 aio_ddp_cancel_one(struct kaiocb *job)
1383 * If this job had copied data out of the socket buffer before
1384 * it was cancelled, report it as a short read rather than an
1387 copied = job->aio_received;
1389 aio_complete(job, copied, 0);
1395 * Called when the main loop wants to requeue a job to retry it later.
1396 * Deals with the race of the job being cancelled while it was being
1400 aio_ddp_requeue_one(struct toepcb *toep, struct kaiocb *job)
1403 DDP_ASSERT_LOCKED(toep);
1404 if (!(toep->ddp.flags & DDP_DEAD) &&
1405 aio_set_cancel_function(job, t4_aio_cancel_queued)) {
1406 TAILQ_INSERT_HEAD(&toep->ddp.aiojobq, job, list);
1407 toep->ddp.waiting_count++;
1409 aio_ddp_cancel_one(job);
1413 aio_ddp_requeue(struct toepcb *toep)
1415 struct adapter *sc = td_adapter(toep->td);
1420 struct ddp_buffer *db;
1421 size_t copied, offset, resid;
1424 uint64_t ddp_flags, ddp_flags_mask;
1426 int buf_flag, db_idx, error;
1428 DDP_ASSERT_LOCKED(toep);
1431 if (toep->ddp.flags & DDP_DEAD) {
1432 MPASS(toep->ddp.waiting_count == 0);
1433 MPASS(toep->ddp.active_count == 0);
1437 if (toep->ddp.waiting_count == 0 ||
1438 toep->ddp.active_count == nitems(toep->ddp.db)) {
1442 job = TAILQ_FIRST(&toep->ddp.aiojobq);
1443 so = job->fd_file->f_data;
1447 /* We will never get anything unless we are or were connected. */
1448 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1450 ddp_complete_all(toep, ENOTCONN);
1454 KASSERT(toep->ddp.active_count == 0 || sbavail(sb) == 0,
1455 ("%s: pending sockbuf data and DDP is active", __func__));
1457 /* Abort if socket has reported problems. */
1458 /* XXX: Wait for any queued DDP's to finish and/or flush them? */
1459 if (so->so_error && sbavail(sb) == 0) {
1460 toep->ddp.waiting_count--;
1461 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1462 if (!aio_clear_cancel_function(job)) {
1468 * If this job has previously copied some data, report
1469 * a short read and leave the error to be reported by
1472 copied = job->aio_received;
1475 aio_complete(job, copied, 0);
1478 error = so->so_error;
1481 aio_complete(job, -1, error);
1486 * Door is closed. If there is pending data in the socket buffer,
1487 * deliver it. If there are pending DDP requests, wait for those
1488 * to complete. Once they have completed, return EOF reads.
1490 if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1492 if (toep->ddp.active_count != 0)
1494 ddp_complete_all(toep, 0);
1499 * If DDP is not enabled and there is no pending socket buffer
1500 * data, try to enable DDP.
1502 if (sbavail(sb) == 0 && (toep->ddp.flags & DDP_ON) == 0) {
1506 * Wait for the card to ACK that DDP is enabled before
1507 * queueing any buffers. Currently this waits for an
1508 * indicate to arrive. This could use a TCB_SET_FIELD_RPL
1509 * message to know that DDP was enabled instead of waiting
1510 * for the indicate which would avoid copying the indicate
1511 * if no data is pending.
1513 * XXX: Might want to limit the indicate size to the size
1514 * of the first queued request.
1516 if ((toep->ddp.flags & DDP_SC_REQ) == 0)
1517 enable_ddp(sc, toep);
1523 * If another thread is queueing a buffer for DDP, let it
1524 * drain any work and return.
1526 if (toep->ddp.queueing != NULL)
1529 /* Take the next job to prep it for DDP. */
1530 toep->ddp.waiting_count--;
1531 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1532 if (!aio_clear_cancel_function(job))
1534 toep->ddp.queueing = job;
1536 /* NB: This drops DDP_LOCK while it holds the backing VM pages. */
1537 error = hold_aio(toep, job, &ps);
1539 ddp_complete_one(job, error);
1540 toep->ddp.queueing = NULL;
1545 if (so->so_error && sbavail(sb) == 0) {
1546 copied = job->aio_received;
1549 recycle_pageset(toep, ps);
1550 aio_complete(job, copied, 0);
1551 toep->ddp.queueing = NULL;
1555 error = so->so_error;
1558 recycle_pageset(toep, ps);
1559 aio_complete(job, -1, error);
1560 toep->ddp.queueing = NULL;
1564 if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1566 recycle_pageset(toep, ps);
1567 if (toep->ddp.active_count != 0) {
1569 * The door is closed, but there are still pending
1570 * DDP buffers. Requeue. These jobs will all be
1571 * completed once those buffers drain.
1573 aio_ddp_requeue_one(toep, job);
1574 toep->ddp.queueing = NULL;
1577 ddp_complete_one(job, 0);
1578 ddp_complete_all(toep, 0);
1579 toep->ddp.queueing = NULL;
1585 * If the toep is dead, there shouldn't be any data in the socket
1586 * buffer, so the above case should have handled this.
1588 MPASS(!(toep->ddp.flags & DDP_DEAD));
1591 * If there is pending data in the socket buffer (either
1592 * from before the requests were queued or a DDP indicate),
1593 * copy those mbufs out directly.
1596 offset = ps->offset + job->aio_received;
1597 MPASS(job->aio_received <= job->uaiocb.aio_nbytes);
1598 resid = job->uaiocb.aio_nbytes - job->aio_received;
1600 KASSERT(m == NULL || toep->ddp.active_count == 0,
1601 ("%s: sockbuf data with active DDP", __func__));
1602 while (m != NULL && resid > 0) {
1603 struct iovec iov[1];
1607 iov[0].iov_base = mtod(m, void *);
1608 iov[0].iov_len = m->m_len;
1609 if (iov[0].iov_len > resid)
1610 iov[0].iov_len = resid;
1614 uio.uio_resid = iov[0].iov_len;
1615 uio.uio_segflg = UIO_SYSSPACE;
1616 uio.uio_rw = UIO_WRITE;
1617 error = uiomove_fromphys(ps->pages, offset + copied,
1618 uio.uio_resid, &uio);
1619 MPASS(error == 0 && uio.uio_resid == 0);
1620 copied += uio.uio_offset;
1621 resid -= uio.uio_offset;
1625 sbdrop_locked(sb, copied);
1626 job->aio_received += copied;
1628 copied = job->aio_received;
1629 inp = sotoinpcb(so);
1630 if (!INP_TRY_WLOCK(inp)) {
1632 * The reference on the socket file descriptor in
1633 * the AIO job should keep 'sb' and 'inp' stable.
1634 * Our caller has a reference on the 'toep' that
1644 * If the socket has been closed, we should detect
1645 * that and complete this request if needed on
1646 * the next trip around the loop.
1649 t4_rcvd_locked(&toep->td->tod, intotcpcb(inp));
1651 if (resid == 0 || toep->ddp.flags & DDP_DEAD) {
1653 * We filled the entire buffer with socket
1654 * data, DDP is not being used, or the socket
1655 * is being shut down, so complete the
1659 recycle_pageset(toep, ps);
1660 aio_complete(job, copied, 0);
1661 toep->ddp.queueing = NULL;
1666 * If DDP is not enabled, requeue this request and restart.
1667 * This will either enable DDP or wait for more data to
1668 * arrive on the socket buffer.
1670 if ((toep->ddp.flags & (DDP_ON | DDP_SC_REQ)) != DDP_ON) {
1672 recycle_pageset(toep, ps);
1673 aio_ddp_requeue_one(toep, job);
1674 toep->ddp.queueing = NULL;
1679 * An indicate might have arrived and been added to
1680 * the socket buffer while it was unlocked after the
1681 * copy to lock the INP. If so, restart the copy.
1683 if (sbavail(sb) != 0)
1688 if (prep_pageset(sc, toep, ps) == 0) {
1689 recycle_pageset(toep, ps);
1690 aio_ddp_requeue_one(toep, job);
1691 toep->ddp.queueing = NULL;
1694 * XXX: Need to retry this later. Mostly need a trigger
1695 * when page pods are freed up.
1697 printf("%s: prep_pageset failed\n", __func__);
1701 /* Determine which DDP buffer to use. */
1702 if (toep->ddp.db[0].job == NULL) {
1705 MPASS(toep->ddp.db[1].job == NULL);
1712 ddp_flags |= V_TF_DDP_BUF0_VALID(1);
1713 if (so->so_state & SS_NBIO)
1714 ddp_flags |= V_TF_DDP_BUF0_FLUSH(1);
1715 ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE0(1) |
1716 V_TF_DDP_PUSH_DISABLE_0(1) | V_TF_DDP_PSHF_ENABLE_0(1) |
1717 V_TF_DDP_BUF0_FLUSH(1) | V_TF_DDP_BUF0_VALID(1);
1718 buf_flag = DDP_BUF0_ACTIVE;
1720 ddp_flags |= V_TF_DDP_BUF1_VALID(1);
1721 if (so->so_state & SS_NBIO)
1722 ddp_flags |= V_TF_DDP_BUF1_FLUSH(1);
1723 ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE1(1) |
1724 V_TF_DDP_PUSH_DISABLE_1(1) | V_TF_DDP_PSHF_ENABLE_1(1) |
1725 V_TF_DDP_BUF1_FLUSH(1) | V_TF_DDP_BUF1_VALID(1);
1726 buf_flag = DDP_BUF1_ACTIVE;
1728 MPASS((toep->ddp.flags & buf_flag) == 0);
1729 if ((toep->ddp.flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0) {
1731 MPASS(toep->ddp.active_id == -1);
1732 MPASS(toep->ddp.active_count == 0);
1733 ddp_flags_mask |= V_TF_DDP_ACTIVE_BUF(1);
1737 * The TID for this connection should still be valid. If DDP_DEAD
1738 * is set, SBS_CANTRCVMORE should be set, so we shouldn't be
1739 * this far anyway. Even if the socket is closing on the other
1740 * end, the AIO job holds a reference on this end of the socket
1741 * which will keep it open and keep the TCP PCB attached until
1742 * after the job is completed.
1744 wr = mk_update_tcb_for_ddp(sc, toep, db_idx, ps, job->aio_received,
1745 ddp_flags, ddp_flags_mask);
1747 recycle_pageset(toep, ps);
1748 aio_ddp_requeue_one(toep, job);
1749 toep->ddp.queueing = NULL;
1752 * XXX: Need a way to kick a retry here.
1754 * XXX: We know the fixed size needed and could
1755 * preallocate this using a blocking request at the
1756 * start of the task to avoid having to handle this
1759 printf("%s: mk_update_tcb_for_ddp failed\n", __func__);
1763 if (!aio_set_cancel_function(job, t4_aio_cancel_active)) {
1765 recycle_pageset(toep, ps);
1766 aio_ddp_cancel_one(job);
1767 toep->ddp.queueing = NULL;
1771 #ifdef VERBOSE_TRACES
1773 "%s: tid %u, scheduling %p for DDP[%d] (flags %#lx/%#lx)", __func__,
1774 toep->tid, job, db_idx, ddp_flags, ddp_flags_mask);
1776 /* Give the chip the go-ahead. */
1778 db = &toep->ddp.db[db_idx];
1779 db->cancel_pending = 0;
1782 toep->ddp.queueing = NULL;
1783 toep->ddp.flags |= buf_flag;
1784 toep->ddp.active_count++;
1785 if (toep->ddp.active_count == 1) {
1786 MPASS(toep->ddp.active_id == -1);
1787 toep->ddp.active_id = db_idx;
1788 CTR2(KTR_CXGBE, "%s: ddp_active_id = %d", __func__,
1789 toep->ddp.active_id);
1795 ddp_queue_toep(struct toepcb *toep)
1798 DDP_ASSERT_LOCKED(toep);
1799 if (toep->ddp.flags & DDP_TASK_ACTIVE)
1801 toep->ddp.flags |= DDP_TASK_ACTIVE;
1803 soaio_enqueue(&toep->ddp.requeue_task);
1807 aio_ddp_requeue_task(void *context, int pending)
1809 struct toepcb *toep = context;
1812 aio_ddp_requeue(toep);
1813 toep->ddp.flags &= ~DDP_TASK_ACTIVE;
1820 t4_aio_cancel_active(struct kaiocb *job)
1822 struct socket *so = job->fd_file->f_data;
1823 struct tcpcb *tp = so_sototcpcb(so);
1824 struct toepcb *toep = tp->t_toe;
1825 struct adapter *sc = td_adapter(toep->td);
1826 uint64_t valid_flag;
1830 if (aio_cancel_cleared(job)) {
1832 aio_ddp_cancel_one(job);
1836 for (i = 0; i < nitems(toep->ddp.db); i++) {
1837 if (toep->ddp.db[i].job == job) {
1838 /* Should only ever get one cancel request for a job. */
1839 MPASS(toep->ddp.db[i].cancel_pending == 0);
1842 * Invalidate this buffer. It will be
1843 * cancelled or partially completed once the
1844 * card ACKs the invalidate.
1846 valid_flag = i == 0 ? V_TF_DDP_BUF0_VALID(1) :
1847 V_TF_DDP_BUF1_VALID(1);
1848 t4_set_tcb_field(sc, toep->ctrlq, toep,
1849 W_TCB_RX_DDP_FLAGS, valid_flag, 0, 1,
1850 i + DDP_BUF0_INVALIDATED);
1851 toep->ddp.db[i].cancel_pending = 1;
1852 CTR2(KTR_CXGBE, "%s: request %p marked pending",
1861 t4_aio_cancel_queued(struct kaiocb *job)
1863 struct socket *so = job->fd_file->f_data;
1864 struct tcpcb *tp = so_sototcpcb(so);
1865 struct toepcb *toep = tp->t_toe;
1868 if (!aio_cancel_cleared(job)) {
1869 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1870 toep->ddp.waiting_count--;
1871 if (toep->ddp.waiting_count == 0)
1872 ddp_queue_toep(toep);
1874 CTR2(KTR_CXGBE, "%s: request %p cancelled", __func__, job);
1877 aio_ddp_cancel_one(job);
1881 t4_aio_queue_ddp(struct socket *so, struct kaiocb *job)
1883 struct tcpcb *tp = so_sototcpcb(so);
1884 struct toepcb *toep = tp->t_toe;
1887 /* Ignore writes. */
1888 if (job->uaiocb.aio_lio_opcode != LIO_READ)
1889 return (EOPNOTSUPP);
1894 * XXX: Think about possibly returning errors for ENOTCONN,
1895 * etc. Perhaps the caller would only queue the request
1896 * if it failed with EOPNOTSUPP?
1899 #ifdef VERBOSE_TRACES
1900 CTR3(KTR_CXGBE, "%s: queueing %p for tid %u", __func__, job, toep->tid);
1902 if (!aio_set_cancel_function(job, t4_aio_cancel_queued))
1903 panic("new job was cancelled");
1904 TAILQ_INSERT_TAIL(&toep->ddp.aiojobq, job, list);
1905 toep->ddp.waiting_count++;
1906 toep->ddp.flags |= DDP_OK;
1909 * Try to handle this request synchronously. If this has
1910 * to block because the task is running, it will just bail
1911 * and let the task handle it instead.
1913 aio_ddp_requeue(toep);
1919 t4_ddp_mod_load(void)
1922 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, do_ddp_tcb_rpl,
1924 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, do_ddp_tcb_rpl,
1926 t4_register_cpl_handler(CPL_RX_DATA_DDP, do_rx_data_ddp);
1927 t4_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_rx_ddp_complete);
1928 TAILQ_INIT(&ddp_orphan_pagesets);
1929 mtx_init(&ddp_orphan_pagesets_lock, "ddp orphans", NULL, MTX_DEF);
1930 TASK_INIT(&ddp_orphan_task, 0, ddp_free_orphan_pagesets, NULL);
1934 t4_ddp_mod_unload(void)
1937 taskqueue_drain(taskqueue_thread, &ddp_orphan_task);
1938 MPASS(TAILQ_EMPTY(&ddp_orphan_pagesets));
1939 mtx_destroy(&ddp_orphan_pagesets_lock);
1940 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, NULL, CPL_COOKIE_DDP0);
1941 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, NULL, CPL_COOKIE_DDP1);
1942 t4_register_cpl_handler(CPL_RX_DATA_DDP, NULL);
1943 t4_register_cpl_handler(CPL_RX_DDP_COMPLETE, NULL);