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 if (ps->flags & PS_WIRED) {
116 for (i = 0; i < ps->npages; i++) {
119 vm_page_unwire(p, PQ_INACTIVE);
123 vm_page_unhold_pages(ps->pages, ps->npages);
124 mtx_lock(&ddp_orphan_pagesets_lock);
125 TAILQ_INSERT_TAIL(&ddp_orphan_pagesets, ps, link);
126 taskqueue_enqueue(taskqueue_thread, &ddp_orphan_task);
127 mtx_unlock(&ddp_orphan_pagesets_lock);
131 ddp_free_orphan_pagesets(void *context, int pending)
135 mtx_lock(&ddp_orphan_pagesets_lock);
136 while (!TAILQ_EMPTY(&ddp_orphan_pagesets)) {
137 ps = TAILQ_FIRST(&ddp_orphan_pagesets);
138 TAILQ_REMOVE(&ddp_orphan_pagesets, ps, link);
139 mtx_unlock(&ddp_orphan_pagesets_lock);
141 vmspace_free(ps->vm);
143 mtx_lock(&ddp_orphan_pagesets_lock);
145 mtx_unlock(&ddp_orphan_pagesets_lock);
149 recycle_pageset(struct toepcb *toep, struct pageset *ps)
152 DDP_ASSERT_LOCKED(toep);
153 if (!(toep->ddp.flags & DDP_DEAD) && ps->flags & PS_WIRED) {
154 KASSERT(toep->ddp.cached_count + toep->ddp.active_count <
155 nitems(toep->ddp.db), ("too many wired pagesets"));
156 TAILQ_INSERT_HEAD(&toep->ddp.cached_pagesets, ps, link);
157 toep->ddp.cached_count++;
159 free_pageset(toep->td, ps);
163 ddp_complete_one(struct kaiocb *job, int error)
168 * If this job had copied data out of the socket buffer before
169 * it was cancelled, report it as a short read rather than an
172 copied = job->aio_received;
173 if (copied != 0 || error == 0)
174 aio_complete(job, copied, 0);
176 aio_complete(job, -1, error);
180 free_ddp_buffer(struct tom_data *td, struct ddp_buffer *db)
185 * XXX: If we are un-offloading the socket then we
186 * should requeue these on the socket somehow. If we
187 * got a FIN from the remote end, then this completes
188 * any remaining requests with an EOF read.
190 if (!aio_clear_cancel_function(db->job))
191 ddp_complete_one(db->job, 0);
195 free_pageset(td, db->ps);
199 ddp_init_toep(struct toepcb *toep)
202 TAILQ_INIT(&toep->ddp.aiojobq);
203 TASK_INIT(&toep->ddp.requeue_task, 0, aio_ddp_requeue_task, toep);
204 toep->ddp.flags = DDP_OK;
205 toep->ddp.active_id = -1;
206 mtx_init(&toep->ddp.lock, "t4 ddp", NULL, MTX_DEF);
210 ddp_uninit_toep(struct toepcb *toep)
213 mtx_destroy(&toep->ddp.lock);
217 release_ddp_resources(struct toepcb *toep)
223 toep->ddp.flags |= DDP_DEAD;
224 for (i = 0; i < nitems(toep->ddp.db); i++) {
225 free_ddp_buffer(toep->td, &toep->ddp.db[i]);
227 while ((ps = TAILQ_FIRST(&toep->ddp.cached_pagesets)) != NULL) {
228 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
229 free_pageset(toep->td, ps);
231 ddp_complete_all(toep, 0);
237 ddp_assert_empty(struct toepcb *toep)
241 MPASS(!(toep->ddp.flags & DDP_TASK_ACTIVE));
242 for (i = 0; i < nitems(toep->ddp.db); i++) {
243 MPASS(toep->ddp.db[i].job == NULL);
244 MPASS(toep->ddp.db[i].ps == NULL);
246 MPASS(TAILQ_EMPTY(&toep->ddp.cached_pagesets));
247 MPASS(TAILQ_EMPTY(&toep->ddp.aiojobq));
252 complete_ddp_buffer(struct toepcb *toep, struct ddp_buffer *db,
255 unsigned int db_flag;
257 toep->ddp.active_count--;
258 if (toep->ddp.active_id == db_idx) {
259 if (toep->ddp.active_count == 0) {
260 KASSERT(toep->ddp.db[db_idx ^ 1].job == NULL,
261 ("%s: active_count mismatch", __func__));
262 toep->ddp.active_id = -1;
264 toep->ddp.active_id ^= 1;
265 #ifdef VERBOSE_TRACES
266 CTR2(KTR_CXGBE, "%s: ddp_active_id = %d", __func__,
267 toep->ddp.active_id);
270 KASSERT(toep->ddp.active_count != 0 &&
271 toep->ddp.active_id != -1,
272 ("%s: active count mismatch", __func__));
275 db->cancel_pending = 0;
277 recycle_pageset(toep, db->ps);
280 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
281 KASSERT(toep->ddp.flags & db_flag,
282 ("%s: DDP buffer not active. toep %p, ddp_flags 0x%x",
283 __func__, toep, toep->ddp.flags));
284 toep->ddp.flags &= ~db_flag;
287 /* XXX: handle_ddp_data code duplication */
289 insert_ddp_data(struct toepcb *toep, uint32_t n)
291 struct inpcb *inp = toep->inp;
292 struct tcpcb *tp = intotcpcb(inp);
293 struct ddp_buffer *db;
297 unsigned int db_flag, db_idx;
299 INP_WLOCK_ASSERT(inp);
300 DDP_ASSERT_LOCKED(toep);
303 #ifndef USE_DDP_RX_FLOW_CONTROL
304 KASSERT(tp->rcv_wnd >= n, ("%s: negative window size", __func__));
307 CTR2(KTR_CXGBE, "%s: placed %u bytes before falling out of DDP",
309 while (toep->ddp.active_count > 0) {
310 MPASS(toep->ddp.active_id != -1);
311 db_idx = toep->ddp.active_id;
312 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
313 MPASS((toep->ddp.flags & db_flag) != 0);
314 db = &toep->ddp.db[db_idx];
316 copied = job->aio_received;
318 if (placed > job->uaiocb.aio_nbytes - copied)
319 placed = job->uaiocb.aio_nbytes - copied;
322 if (!aio_clear_cancel_function(job)) {
324 * Update the copied length for when
325 * t4_aio_cancel_active() completes this
328 job->aio_received += placed;
329 } else if (copied + placed != 0) {
331 "%s: completing %p (copied %ld, placed %lu)",
332 __func__, job, copied, placed);
333 /* XXX: This always completes if there is some data. */
334 aio_complete(job, copied + placed, 0);
335 } else if (aio_set_cancel_function(job, t4_aio_cancel_queued)) {
336 TAILQ_INSERT_HEAD(&toep->ddp.aiojobq, job, list);
337 toep->ddp.waiting_count++;
341 complete_ddp_buffer(toep, db, db_idx);
347 /* SET_TCB_FIELD sent as a ULP command looks like this */
348 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \
349 sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core))
351 /* RX_DATA_ACK sent as a ULP command looks like this */
352 #define LEN__RX_DATA_ACK_ULP (sizeof(struct ulp_txpkt) + \
353 sizeof(struct ulptx_idata) + sizeof(struct cpl_rx_data_ack_core))
356 mk_set_tcb_field_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep,
357 uint64_t word, uint64_t mask, uint64_t val)
359 struct ulptx_idata *ulpsc;
360 struct cpl_set_tcb_field_core *req;
362 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
363 ulpmc->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16));
365 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
366 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
367 ulpsc->len = htobe32(sizeof(*req));
369 req = (struct cpl_set_tcb_field_core *)(ulpsc + 1);
370 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, toep->tid));
371 req->reply_ctrl = htobe16(V_NO_REPLY(1) |
372 V_QUEUENO(toep->ofld_rxq->iq.abs_id));
373 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
374 req->mask = htobe64(mask);
375 req->val = htobe64(val);
377 ulpsc = (struct ulptx_idata *)(req + 1);
378 if (LEN__SET_TCB_FIELD_ULP % 16) {
379 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
380 ulpsc->len = htobe32(0);
387 mk_rx_data_ack_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep)
389 struct ulptx_idata *ulpsc;
390 struct cpl_rx_data_ack_core *req;
392 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
393 ulpmc->len = htobe32(howmany(LEN__RX_DATA_ACK_ULP, 16));
395 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
396 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
397 ulpsc->len = htobe32(sizeof(*req));
399 req = (struct cpl_rx_data_ack_core *)(ulpsc + 1);
400 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_RX_DATA_ACK, toep->tid));
401 req->credit_dack = htobe32(F_RX_MODULATE_RX);
403 ulpsc = (struct ulptx_idata *)(req + 1);
404 if (LEN__RX_DATA_ACK_ULP % 16) {
405 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
406 ulpsc->len = htobe32(0);
413 mk_update_tcb_for_ddp(struct adapter *sc, struct toepcb *toep, int db_idx,
414 struct pageset *ps, int offset, uint64_t ddp_flags, uint64_t ddp_flags_mask)
417 struct work_request_hdr *wrh;
418 struct ulp_txpkt *ulpmc;
421 KASSERT(db_idx == 0 || db_idx == 1,
422 ("%s: bad DDP buffer index %d", __func__, db_idx));
425 * We'll send a compound work request that has 3 SET_TCB_FIELDs and an
426 * RX_DATA_ACK (with RX_MODULATE to speed up delivery).
428 * The work request header is 16B and always ends at a 16B boundary.
429 * The ULPTX master commands that follow must all end at 16B boundaries
430 * too so we round up the size to 16.
432 len = sizeof(*wrh) + 3 * roundup2(LEN__SET_TCB_FIELD_ULP, 16) +
433 roundup2(LEN__RX_DATA_ACK_ULP, 16);
435 wr = alloc_wrqe(len, toep->ctrlq);
439 INIT_ULPTX_WRH(wrh, len, 1, 0); /* atomic */
440 ulpmc = (struct ulp_txpkt *)(wrh + 1);
442 /* Write the buffer's tag */
443 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
444 W_TCB_RX_DDP_BUF0_TAG + db_idx,
445 V_TCB_RX_DDP_BUF0_TAG(M_TCB_RX_DDP_BUF0_TAG),
446 V_TCB_RX_DDP_BUF0_TAG(ps->prsv.prsv_tag));
448 /* Update the current offset in the DDP buffer and its total length */
450 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
451 W_TCB_RX_DDP_BUF0_OFFSET,
452 V_TCB_RX_DDP_BUF0_OFFSET(M_TCB_RX_DDP_BUF0_OFFSET) |
453 V_TCB_RX_DDP_BUF0_LEN(M_TCB_RX_DDP_BUF0_LEN),
454 V_TCB_RX_DDP_BUF0_OFFSET(offset) |
455 V_TCB_RX_DDP_BUF0_LEN(ps->len));
457 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
458 W_TCB_RX_DDP_BUF1_OFFSET,
459 V_TCB_RX_DDP_BUF1_OFFSET(M_TCB_RX_DDP_BUF1_OFFSET) |
460 V_TCB_RX_DDP_BUF1_LEN((u64)M_TCB_RX_DDP_BUF1_LEN << 32),
461 V_TCB_RX_DDP_BUF1_OFFSET(offset) |
462 V_TCB_RX_DDP_BUF1_LEN((u64)ps->len << 32));
464 /* Update DDP flags */
465 ulpmc = mk_set_tcb_field_ulp(ulpmc, toep, W_TCB_RX_DDP_FLAGS,
466 ddp_flags_mask, ddp_flags);
468 /* Gratuitous RX_DATA_ACK with RX_MODULATE set to speed up delivery. */
469 ulpmc = mk_rx_data_ack_ulp(ulpmc, toep);
475 handle_ddp_data(struct toepcb *toep, __be32 ddp_report, __be32 rcv_nxt, int len)
477 uint32_t report = be32toh(ddp_report);
479 struct inpcb *inp = toep->inp;
480 struct ddp_buffer *db;
487 db_idx = report & F_DDP_BUF_IDX ? 1 : 0;
489 if (__predict_false(!(report & F_DDP_INV)))
490 CXGBE_UNIMPLEMENTED("DDP buffer still valid");
493 so = inp_inpcbtosocket(inp);
497 KASSERT(toep->ddp.active_id == db_idx,
498 ("completed DDP buffer (%d) != active_id (%d) for tid %d", db_idx,
499 toep->ddp.active_id, toep->tid));
500 db = &toep->ddp.db[db_idx];
503 if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
505 * This can happen due to an administrative tcpdrop(8).
506 * Just fail the request with ECONNRESET.
508 CTR5(KTR_CXGBE, "%s: tid %u, seq 0x%x, len %d, inp_flags 0x%x",
509 __func__, toep->tid, be32toh(rcv_nxt), len, inp->inp_flags);
510 if (aio_clear_cancel_function(job))
511 ddp_complete_one(job, ECONNRESET);
518 * For RX_DDP_COMPLETE, len will be zero and rcv_nxt is the
519 * sequence number of the next byte to receive. The length of
520 * the data received for this message must be computed by
521 * comparing the new and old values of rcv_nxt.
523 * For RX_DATA_DDP, len might be non-zero, but it is only the
524 * length of the most recent DMA. It does not include the
525 * total length of the data received since the previous update
526 * for this DDP buffer. rcv_nxt is the sequence number of the
527 * first received byte from the most recent DMA.
529 len += be32toh(rcv_nxt) - tp->rcv_nxt;
531 tp->t_rcvtime = ticks;
532 #ifndef USE_DDP_RX_FLOW_CONTROL
533 KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
536 #ifdef VERBOSE_TRACES
537 CTR4(KTR_CXGBE, "%s: DDP[%d] placed %d bytes (%#x)", __func__, db_idx,
541 /* receive buffer autosize */
542 MPASS(toep->vnet == so->so_vnet);
543 CURVNET_SET(toep->vnet);
545 if (sb->sb_flags & SB_AUTOSIZE &&
546 V_tcp_do_autorcvbuf &&
547 sb->sb_hiwat < V_tcp_autorcvbuf_max &&
548 len > (sbspace(sb) / 8 * 7)) {
549 struct adapter *sc = td_adapter(toep->td);
550 unsigned int hiwat = sb->sb_hiwat;
551 unsigned int newsize = min(hiwat + sc->tt.autorcvbuf_inc,
552 V_tcp_autorcvbuf_max);
554 if (!sbreserve_locked(sb, newsize, so, NULL))
555 sb->sb_flags &= ~SB_AUTOSIZE;
561 if (db->cancel_pending) {
563 * Update the job's length but defer completion to the
566 job->aio_received += len;
568 } else if (!aio_clear_cancel_function(job)) {
570 * Update the copied length for when
571 * t4_aio_cancel_active() completes this request.
573 job->aio_received += len;
575 copied = job->aio_received;
576 #ifdef VERBOSE_TRACES
577 CTR4(KTR_CXGBE, "%s: completing %p (copied %ld, placed %d)",
578 __func__, job, copied, len);
580 aio_complete(job, copied + len, 0);
581 t4_rcvd(&toep->td->tod, tp);
585 complete_ddp_buffer(toep, db, db_idx);
586 if (toep->ddp.waiting_count > 0)
587 ddp_queue_toep(toep);
596 handle_ddp_indicate(struct toepcb *toep)
599 DDP_ASSERT_LOCKED(toep);
600 MPASS(toep->ddp.active_count == 0);
601 MPASS((toep->ddp.flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0);
602 if (toep->ddp.waiting_count == 0) {
604 * The pending requests that triggered the request for an
605 * an indicate were cancelled. Those cancels should have
606 * already disabled DDP. Just ignore this as the data is
607 * going into the socket buffer anyway.
611 CTR3(KTR_CXGBE, "%s: tid %d indicated (%d waiting)", __func__,
612 toep->tid, toep->ddp.waiting_count);
613 ddp_queue_toep(toep);
617 DDP_BUF0_INVALIDATED = 0x2,
621 CTASSERT(DDP_BUF0_INVALIDATED == CPL_COOKIE_DDP0);
624 do_ddp_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
626 struct adapter *sc = iq->adapter;
627 const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
628 unsigned int tid = GET_TID(cpl);
632 struct ddp_buffer *db;
636 if (cpl->status != CPL_ERR_NONE)
637 panic("XXX: tcp_rpl failed: %d", cpl->status);
639 toep = lookup_tid(sc, tid);
641 switch (cpl->cookie) {
642 case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF0_INVALIDATED):
643 case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF1_INVALIDATED):
645 * XXX: This duplicates a lot of code with handle_ddp_data().
647 db_idx = G_COOKIE(cpl->cookie) - DDP_BUF0_INVALIDATED;
648 MPASS(db_idx < nitems(toep->ddp.db));
651 db = &toep->ddp.db[db_idx];
654 * handle_ddp_data() should leave the job around until
655 * this callback runs once a cancel is pending.
658 MPASS(db->job != NULL);
659 MPASS(db->cancel_pending);
662 * XXX: It's not clear what happens if there is data
663 * placed when the buffer is invalidated. I suspect we
664 * need to read the TCB to see how much data was placed.
666 * For now this just pretends like nothing was placed.
668 * XXX: Note that if we did check the PCB we would need to
669 * also take care of updating the tp, etc.
672 copied = job->aio_received;
674 CTR2(KTR_CXGBE, "%s: cancelling %p", __func__, job);
677 CTR3(KTR_CXGBE, "%s: completing %p (copied %ld)",
678 __func__, job, copied);
679 aio_complete(job, copied, 0);
680 t4_rcvd(&toep->td->tod, intotcpcb(inp));
683 complete_ddp_buffer(toep, db, db_idx);
684 if (toep->ddp.waiting_count > 0)
685 ddp_queue_toep(toep);
690 panic("XXX: unknown tcb_rpl offset %#x, cookie %#x",
691 G_WORD(cpl->cookie), G_COOKIE(cpl->cookie));
698 handle_ddp_close(struct toepcb *toep, struct tcpcb *tp, __be32 rcv_nxt)
700 struct ddp_buffer *db;
703 unsigned int db_flag, db_idx;
706 INP_WLOCK_ASSERT(toep->inp);
707 DDP_ASSERT_LOCKED(toep);
709 len = be32toh(rcv_nxt) - tp->rcv_nxt;
712 while (toep->ddp.active_count > 0) {
713 MPASS(toep->ddp.active_id != -1);
714 db_idx = toep->ddp.active_id;
715 db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
716 MPASS((toep->ddp.flags & db_flag) != 0);
717 db = &toep->ddp.db[db_idx];
719 copied = job->aio_received;
721 if (placed > job->uaiocb.aio_nbytes - copied)
722 placed = job->uaiocb.aio_nbytes - copied;
725 if (!aio_clear_cancel_function(job)) {
727 * Update the copied length for when
728 * t4_aio_cancel_active() completes this
731 job->aio_received += placed;
733 CTR4(KTR_CXGBE, "%s: tid %d completed buf %d len %d",
734 __func__, toep->tid, db_idx, placed);
735 aio_complete(job, copied + placed, 0);
738 complete_ddp_buffer(toep, db, db_idx);
742 ddp_complete_all(toep, 0);
745 #define DDP_ERR (F_DDP_PPOD_MISMATCH | F_DDP_LLIMIT_ERR | F_DDP_ULIMIT_ERR |\
746 F_DDP_PPOD_PARITY_ERR | F_DDP_PADDING_ERR | F_DDP_OFFSET_ERR |\
747 F_DDP_INVALID_TAG | F_DDP_COLOR_ERR | F_DDP_TID_MISMATCH |\
748 F_DDP_INVALID_PPOD | F_DDP_HDRCRC_ERR | F_DDP_DATACRC_ERR)
750 extern cpl_handler_t t4_cpl_handler[];
753 do_rx_data_ddp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
755 struct adapter *sc = iq->adapter;
756 const struct cpl_rx_data_ddp *cpl = (const void *)(rss + 1);
757 unsigned int tid = GET_TID(cpl);
759 struct toepcb *toep = lookup_tid(sc, tid);
761 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
762 KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
763 KASSERT(!(toep->flags & TPF_SYNQE),
764 ("%s: toep %p claims to be a synq entry", __func__, toep));
766 vld = be32toh(cpl->ddpvld);
767 if (__predict_false(vld & DDP_ERR)) {
768 panic("%s: DDP error 0x%x (tid %d, toep %p)",
769 __func__, vld, tid, toep);
772 if (toep->ulp_mode == ULP_MODE_ISCSI) {
773 t4_cpl_handler[CPL_RX_ISCSI_DDP](iq, rss, m);
777 handle_ddp_data(toep, cpl->u.ddp_report, cpl->seq, be16toh(cpl->len));
783 do_rx_ddp_complete(struct sge_iq *iq, const struct rss_header *rss,
786 struct adapter *sc = iq->adapter;
787 const struct cpl_rx_ddp_complete *cpl = (const void *)(rss + 1);
788 unsigned int tid = GET_TID(cpl);
789 struct toepcb *toep = lookup_tid(sc, tid);
791 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
792 KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
793 KASSERT(!(toep->flags & TPF_SYNQE),
794 ("%s: toep %p claims to be a synq entry", __func__, toep));
796 handle_ddp_data(toep, cpl->ddp_report, cpl->rcv_nxt, 0);
802 enable_ddp(struct adapter *sc, struct toepcb *toep)
805 KASSERT((toep->ddp.flags & (DDP_ON | DDP_OK | DDP_SC_REQ)) == DDP_OK,
806 ("%s: toep %p has bad ddp_flags 0x%x",
807 __func__, toep, toep->ddp.flags));
809 CTR3(KTR_CXGBE, "%s: tid %u (time %u)",
810 __func__, toep->tid, time_uptime);
812 DDP_ASSERT_LOCKED(toep);
813 toep->ddp.flags |= DDP_SC_REQ;
814 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_RX_DDP_FLAGS,
815 V_TF_DDP_OFF(1) | V_TF_DDP_INDICATE_OUT(1) |
816 V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1) |
817 V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_BUF1_VALID(1),
818 V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1), 0, 0);
819 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
820 V_TF_RCV_COALESCE_ENABLE(1), 0, 0, 0);
824 calculate_hcf(int n1, int n2)
846 pages_to_nppods(int npages, int ddp_page_shift)
849 MPASS(ddp_page_shift >= PAGE_SHIFT);
851 return (howmany(npages >> (ddp_page_shift - PAGE_SHIFT), PPOD_PAGES));
855 alloc_page_pods(struct ppod_region *pr, u_int nppods, u_int pgsz_idx,
856 struct ppod_reservation *prsv)
858 vmem_addr_t addr; /* relative to start of region */
860 if (vmem_alloc(pr->pr_arena, PPOD_SZ(nppods), M_NOWAIT | M_FIRSTFIT,
864 CTR5(KTR_CXGBE, "%-17s arena %p, addr 0x%08x, nppods %d, pgsz %d",
865 __func__, pr->pr_arena, (uint32_t)addr & pr->pr_tag_mask,
866 nppods, 1 << pr->pr_page_shift[pgsz_idx]);
869 * The hardware tagmask includes an extra invalid bit but the arena was
870 * seeded with valid values only. An allocation out of this arena will
871 * fit inside the tagmask but won't have the invalid bit set.
873 MPASS((addr & pr->pr_tag_mask) == addr);
874 MPASS((addr & pr->pr_invalid_bit) == 0);
877 prsv->prsv_tag = V_PPOD_PGSZ(pgsz_idx) | addr;
878 prsv->prsv_nppods = nppods;
884 t4_alloc_page_pods_for_ps(struct ppod_region *pr, struct pageset *ps)
886 int i, hcf, seglen, idx, nppods;
887 struct ppod_reservation *prsv = &ps->prsv;
889 KASSERT(prsv->prsv_nppods == 0,
890 ("%s: page pods already allocated", __func__));
893 * The DDP page size is unrelated to the VM page size. We combine
894 * contiguous physical pages into larger segments to get the best DDP
895 * page size possible. This is the largest of the four sizes in
896 * A_ULP_RX_TDDP_PSZ that evenly divides the HCF of the segment sizes in
900 for (i = 0; i < ps->npages; i++) {
902 while (i < ps->npages - 1 &&
903 ps->pages[i]->phys_addr + PAGE_SIZE ==
904 ps->pages[i + 1]->phys_addr) {
909 hcf = calculate_hcf(hcf, seglen);
910 if (hcf < (1 << pr->pr_page_shift[1])) {
912 goto have_pgsz; /* give up, short circuit */
916 #define PR_PAGE_MASK(x) ((1 << pr->pr_page_shift[(x)]) - 1)
917 MPASS((hcf & PR_PAGE_MASK(0)) == 0); /* PAGE_SIZE is >= 4K everywhere */
918 for (idx = nitems(pr->pr_page_shift) - 1; idx > 0; idx--) {
919 if ((hcf & PR_PAGE_MASK(idx)) == 0)
925 MPASS(idx <= M_PPOD_PGSZ);
927 nppods = pages_to_nppods(ps->npages, pr->pr_page_shift[idx]);
928 if (alloc_page_pods(pr, nppods, idx, prsv) != 0)
930 MPASS(prsv->prsv_nppods > 0);
936 t4_alloc_page_pods_for_buf(struct ppod_region *pr, vm_offset_t buf, int len,
937 struct ppod_reservation *prsv)
939 int hcf, seglen, idx, npages, nppods;
940 uintptr_t start_pva, end_pva, pva, p1;
946 * The DDP page size is unrelated to the VM page size. We combine
947 * contiguous physical pages into larger segments to get the best DDP
948 * page size possible. This is the largest of the four sizes in
949 * A_ULP_RX_ISCSI_PSZ that evenly divides the HCF of the segment sizes
953 start_pva = trunc_page(buf);
954 end_pva = trunc_page(buf + len - 1);
956 while (pva <= end_pva) {
958 p1 = pmap_kextract(pva);
960 while (pva <= end_pva && p1 + seglen == pmap_kextract(pva)) {
965 hcf = calculate_hcf(hcf, seglen);
966 if (hcf < (1 << pr->pr_page_shift[1])) {
968 goto have_pgsz; /* give up, short circuit */
972 #define PR_PAGE_MASK(x) ((1 << pr->pr_page_shift[(x)]) - 1)
973 MPASS((hcf & PR_PAGE_MASK(0)) == 0); /* PAGE_SIZE is >= 4K everywhere */
974 for (idx = nitems(pr->pr_page_shift) - 1; idx > 0; idx--) {
975 if ((hcf & PR_PAGE_MASK(idx)) == 0)
981 MPASS(idx <= M_PPOD_PGSZ);
984 npages += (end_pva - start_pva) >> pr->pr_page_shift[idx];
985 nppods = howmany(npages, PPOD_PAGES);
986 if (alloc_page_pods(pr, nppods, idx, prsv) != 0)
988 MPASS(prsv->prsv_nppods > 0);
994 t4_free_page_pods(struct ppod_reservation *prsv)
996 struct ppod_region *pr = prsv->prsv_pr;
1000 MPASS(prsv->prsv_nppods != 0);
1002 addr = prsv->prsv_tag & pr->pr_tag_mask;
1003 MPASS((addr & pr->pr_invalid_bit) == 0);
1005 CTR4(KTR_CXGBE, "%-17s arena %p, addr 0x%08x, nppods %d", __func__,
1006 pr->pr_arena, addr, prsv->prsv_nppods);
1008 vmem_free(pr->pr_arena, addr, PPOD_SZ(prsv->prsv_nppods));
1009 prsv->prsv_nppods = 0;
1012 #define NUM_ULP_TX_SC_IMM_PPODS (256 / PPOD_SIZE)
1015 t4_write_page_pods_for_ps(struct adapter *sc, struct sge_wrq *wrq, int tid,
1019 struct ulp_mem_io *ulpmc;
1020 struct ulptx_idata *ulpsc;
1021 struct pagepod *ppod;
1022 int i, j, k, n, chunk, len, ddp_pgsz, idx;
1025 struct ppod_reservation *prsv = &ps->prsv;
1026 struct ppod_region *pr = prsv->prsv_pr;
1028 KASSERT(!(ps->flags & PS_PPODS_WRITTEN),
1029 ("%s: page pods already written", __func__));
1030 MPASS(prsv->prsv_nppods > 0);
1032 cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
1034 cmd |= htobe32(F_ULP_MEMIO_ORDER);
1036 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
1037 ddp_pgsz = 1 << pr->pr_page_shift[G_PPOD_PGSZ(prsv->prsv_tag)];
1038 ppod_addr = pr->pr_start + (prsv->prsv_tag & pr->pr_tag_mask);
1039 for (i = 0; i < prsv->prsv_nppods; ppod_addr += chunk) {
1041 /* How many page pods are we writing in this cycle */
1042 n = min(prsv->prsv_nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
1044 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
1046 wr = alloc_wrqe(len, wrq);
1048 return (ENOMEM); /* ok to just bail out */
1051 INIT_ULPTX_WR(ulpmc, len, 0, 0);
1053 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
1054 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
1055 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
1057 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
1058 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
1059 ulpsc->len = htobe32(chunk);
1061 ppod = (struct pagepod *)(ulpsc + 1);
1062 for (j = 0; j < n; i++, j++, ppod++) {
1063 ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
1064 V_PPOD_TID(tid) | prsv->prsv_tag);
1065 ppod->len_offset = htobe64(V_PPOD_LEN(ps->len) |
1066 V_PPOD_OFST(ps->offset));
1068 idx = i * PPOD_PAGES * (ddp_pgsz / PAGE_SIZE);
1069 for (k = 0; k < nitems(ppod->addr); k++) {
1070 if (idx < ps->npages) {
1072 htobe64(ps->pages[idx]->phys_addr);
1073 idx += ddp_pgsz / PAGE_SIZE;
1078 "%s: tid %d ppod[%d]->addr[%d] = %p",
1079 __func__, toep->tid, i, k,
1080 htobe64(ppod->addr[k]));
1088 ps->flags |= PS_PPODS_WRITTEN;
1094 t4_write_page_pods_for_buf(struct adapter *sc, struct sge_wrq *wrq, int tid,
1095 struct ppod_reservation *prsv, vm_offset_t buf, int buflen)
1098 struct ulp_mem_io *ulpmc;
1099 struct ulptx_idata *ulpsc;
1100 struct pagepod *ppod;
1101 int i, j, k, n, chunk, len, ddp_pgsz;
1102 u_int ppod_addr, offset;
1104 struct ppod_region *pr = prsv->prsv_pr;
1105 uintptr_t end_pva, pva, pa;
1107 cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
1109 cmd |= htobe32(F_ULP_MEMIO_ORDER);
1111 cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
1112 ddp_pgsz = 1 << pr->pr_page_shift[G_PPOD_PGSZ(prsv->prsv_tag)];
1113 offset = buf & PAGE_MASK;
1114 ppod_addr = pr->pr_start + (prsv->prsv_tag & pr->pr_tag_mask);
1115 pva = trunc_page(buf);
1116 end_pva = trunc_page(buf + buflen - 1);
1117 for (i = 0; i < prsv->prsv_nppods; ppod_addr += chunk) {
1119 /* How many page pods are we writing in this cycle */
1120 n = min(prsv->prsv_nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
1123 len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
1125 wr = alloc_wrqe(len, wrq);
1127 return (ENOMEM); /* ok to just bail out */
1130 INIT_ULPTX_WR(ulpmc, len, 0, 0);
1132 ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
1133 ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
1134 ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
1136 ulpsc = (struct ulptx_idata *)(ulpmc + 1);
1137 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
1138 ulpsc->len = htobe32(chunk);
1140 ppod = (struct pagepod *)(ulpsc + 1);
1141 for (j = 0; j < n; i++, j++, ppod++) {
1142 ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
1144 (prsv->prsv_tag & ~V_PPOD_PGSZ(M_PPOD_PGSZ)));
1145 ppod->len_offset = htobe64(V_PPOD_LEN(buflen) |
1146 V_PPOD_OFST(offset));
1149 for (k = 0; k < nitems(ppod->addr); k++) {
1153 pa = pmap_kextract(pva);
1154 ppod->addr[k] = htobe64(pa);
1159 "%s: tid %d ppod[%d]->addr[%d] = %p",
1160 __func__, tid, i, k,
1161 htobe64(ppod->addr[k]));
1166 * Walk back 1 segment so that the first address in the
1167 * next pod is the same as the last one in the current
1176 MPASS(pva <= end_pva);
1182 wire_pageset(struct pageset *ps)
1187 KASSERT(!(ps->flags & PS_WIRED), ("pageset already wired"));
1189 for (i = 0; i < ps->npages; i++) {
1196 ps->flags |= PS_WIRED;
1200 * Prepare a pageset for DDP. This wires the pageset and sets up page
1204 prep_pageset(struct adapter *sc, struct toepcb *toep, struct pageset *ps)
1206 struct tom_data *td = sc->tom_softc;
1208 if (!(ps->flags & PS_WIRED))
1210 if (ps->prsv.prsv_nppods == 0 &&
1211 !t4_alloc_page_pods_for_ps(&td->pr, ps)) {
1214 if (!(ps->flags & PS_PPODS_WRITTEN) &&
1215 t4_write_page_pods_for_ps(sc, toep->ctrlq, toep->tid, ps) != 0) {
1223 t4_init_ppod_region(struct ppod_region *pr, struct t4_range *r, u_int psz,
1231 pr->pr_start = r->start;
1232 pr->pr_len = r->size;
1233 pr->pr_page_shift[0] = 12 + G_HPZ0(psz);
1234 pr->pr_page_shift[1] = 12 + G_HPZ1(psz);
1235 pr->pr_page_shift[2] = 12 + G_HPZ2(psz);
1236 pr->pr_page_shift[3] = 12 + G_HPZ3(psz);
1238 /* The SGL -> page pod algorithm requires the sizes to be in order. */
1239 for (i = 1; i < nitems(pr->pr_page_shift); i++) {
1240 if (pr->pr_page_shift[i] <= pr->pr_page_shift[i - 1])
1244 pr->pr_tag_mask = ((1 << fls(r->size)) - 1) & V_PPOD_TAG(M_PPOD_TAG);
1245 pr->pr_alias_mask = V_PPOD_TAG(M_PPOD_TAG) & ~pr->pr_tag_mask;
1246 if (pr->pr_tag_mask == 0 || pr->pr_alias_mask == 0)
1248 pr->pr_alias_shift = fls(pr->pr_tag_mask);
1249 pr->pr_invalid_bit = 1 << (pr->pr_alias_shift - 1);
1251 pr->pr_arena = vmem_create(name, 0, pr->pr_len, PPOD_SIZE, 0,
1252 M_FIRSTFIT | M_NOWAIT);
1253 if (pr->pr_arena == NULL)
1260 t4_free_ppod_region(struct ppod_region *pr)
1266 vmem_destroy(pr->pr_arena);
1267 bzero(pr, sizeof(*pr));
1271 pscmp(struct pageset *ps, struct vmspace *vm, vm_offset_t start, int npages,
1275 if (ps->start != start || ps->npages != npages ||
1276 ps->offset != pgoff || ps->len != len)
1279 return (ps->vm != vm || ps->vm_timestamp != vm->vm_map.timestamp);
1283 hold_aio(struct toepcb *toep, struct kaiocb *job, struct pageset **pps)
1287 vm_offset_t start, end, pgoff;
1291 DDP_ASSERT_LOCKED(toep);
1294 * The AIO subsystem will cancel and drain all requests before
1295 * permitting a process to exit or exec, so p_vmspace should
1298 vm = job->userproc->p_vmspace;
1300 start = (uintptr_t)job->uaiocb.aio_buf;
1301 pgoff = start & PAGE_MASK;
1302 end = round_page(start + job->uaiocb.aio_nbytes);
1303 start = trunc_page(start);
1305 if (end - start > MAX_DDP_BUFFER_SIZE) {
1307 * Truncate the request to a short read.
1308 * Alternatively, we could DDP in chunks to the larger
1309 * buffer, but that would be quite a bit more work.
1311 * When truncating, round the request down to avoid
1312 * crossing a cache line on the final transaction.
1314 end = rounddown2(start + MAX_DDP_BUFFER_SIZE, CACHE_LINE_SIZE);
1315 #ifdef VERBOSE_TRACES
1316 CTR4(KTR_CXGBE, "%s: tid %d, truncating size from %lu to %lu",
1317 __func__, toep->tid, (unsigned long)job->uaiocb.aio_nbytes,
1318 (unsigned long)(end - (start + pgoff)));
1319 job->uaiocb.aio_nbytes = end - (start + pgoff);
1321 end = round_page(end);
1324 n = atop(end - start);
1327 * Try to reuse a cached pageset.
1329 TAILQ_FOREACH(ps, &toep->ddp.cached_pagesets, link) {
1330 if (pscmp(ps, vm, start, n, pgoff,
1331 job->uaiocb.aio_nbytes) == 0) {
1332 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
1333 toep->ddp.cached_count--;
1340 * If there are too many cached pagesets to create a new one,
1341 * free a pageset before creating a new one.
1343 KASSERT(toep->ddp.active_count + toep->ddp.cached_count <=
1344 nitems(toep->ddp.db), ("%s: too many wired pagesets", __func__));
1345 if (toep->ddp.active_count + toep->ddp.cached_count ==
1346 nitems(toep->ddp.db)) {
1347 KASSERT(toep->ddp.cached_count > 0,
1348 ("no cached pageset to free"));
1349 ps = TAILQ_LAST(&toep->ddp.cached_pagesets, pagesetq);
1350 TAILQ_REMOVE(&toep->ddp.cached_pagesets, ps, link);
1351 toep->ddp.cached_count--;
1352 free_pageset(toep->td, ps);
1356 /* Create a new pageset. */
1357 ps = malloc(sizeof(*ps) + n * sizeof(vm_page_t), M_CXGBE, M_WAITOK |
1359 ps->pages = (vm_page_t *)(ps + 1);
1360 ps->vm_timestamp = map->timestamp;
1361 ps->npages = vm_fault_quick_hold_pages(map, start, end - start,
1362 VM_PROT_WRITE, ps->pages, n);
1365 if (ps->npages < 0) {
1370 KASSERT(ps->npages == n, ("hold_aio: page count mismatch: %d vs %d",
1374 ps->len = job->uaiocb.aio_nbytes;
1375 atomic_add_int(&vm->vm_refcnt, 1);
1379 CTR5(KTR_CXGBE, "%s: tid %d, new pageset %p for job %p, npages %d",
1380 __func__, toep->tid, ps, job, ps->npages);
1386 ddp_complete_all(struct toepcb *toep, int error)
1390 DDP_ASSERT_LOCKED(toep);
1391 while (!TAILQ_EMPTY(&toep->ddp.aiojobq)) {
1392 job = TAILQ_FIRST(&toep->ddp.aiojobq);
1393 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1394 toep->ddp.waiting_count--;
1395 if (aio_clear_cancel_function(job))
1396 ddp_complete_one(job, error);
1401 aio_ddp_cancel_one(struct kaiocb *job)
1406 * If this job had copied data out of the socket buffer before
1407 * it was cancelled, report it as a short read rather than an
1410 copied = job->aio_received;
1412 aio_complete(job, copied, 0);
1418 * Called when the main loop wants to requeue a job to retry it later.
1419 * Deals with the race of the job being cancelled while it was being
1423 aio_ddp_requeue_one(struct toepcb *toep, struct kaiocb *job)
1426 DDP_ASSERT_LOCKED(toep);
1427 if (!(toep->ddp.flags & DDP_DEAD) &&
1428 aio_set_cancel_function(job, t4_aio_cancel_queued)) {
1429 TAILQ_INSERT_HEAD(&toep->ddp.aiojobq, job, list);
1430 toep->ddp.waiting_count++;
1432 aio_ddp_cancel_one(job);
1436 aio_ddp_requeue(struct toepcb *toep)
1438 struct adapter *sc = td_adapter(toep->td);
1443 struct ddp_buffer *db;
1444 size_t copied, offset, resid;
1447 uint64_t ddp_flags, ddp_flags_mask;
1449 int buf_flag, db_idx, error;
1451 DDP_ASSERT_LOCKED(toep);
1454 if (toep->ddp.flags & DDP_DEAD) {
1455 MPASS(toep->ddp.waiting_count == 0);
1456 MPASS(toep->ddp.active_count == 0);
1460 if (toep->ddp.waiting_count == 0 ||
1461 toep->ddp.active_count == nitems(toep->ddp.db)) {
1465 job = TAILQ_FIRST(&toep->ddp.aiojobq);
1466 so = job->fd_file->f_data;
1470 /* We will never get anything unless we are or were connected. */
1471 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1473 ddp_complete_all(toep, ENOTCONN);
1477 KASSERT(toep->ddp.active_count == 0 || sbavail(sb) == 0,
1478 ("%s: pending sockbuf data and DDP is active", __func__));
1480 /* Abort if socket has reported problems. */
1481 /* XXX: Wait for any queued DDP's to finish and/or flush them? */
1482 if (so->so_error && sbavail(sb) == 0) {
1483 toep->ddp.waiting_count--;
1484 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1485 if (!aio_clear_cancel_function(job)) {
1491 * If this job has previously copied some data, report
1492 * a short read and leave the error to be reported by
1495 copied = job->aio_received;
1498 aio_complete(job, copied, 0);
1501 error = so->so_error;
1504 aio_complete(job, -1, error);
1509 * Door is closed. If there is pending data in the socket buffer,
1510 * deliver it. If there are pending DDP requests, wait for those
1511 * to complete. Once they have completed, return EOF reads.
1513 if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1515 if (toep->ddp.active_count != 0)
1517 ddp_complete_all(toep, 0);
1522 * If DDP is not enabled and there is no pending socket buffer
1523 * data, try to enable DDP.
1525 if (sbavail(sb) == 0 && (toep->ddp.flags & DDP_ON) == 0) {
1529 * Wait for the card to ACK that DDP is enabled before
1530 * queueing any buffers. Currently this waits for an
1531 * indicate to arrive. This could use a TCB_SET_FIELD_RPL
1532 * message to know that DDP was enabled instead of waiting
1533 * for the indicate which would avoid copying the indicate
1534 * if no data is pending.
1536 * XXX: Might want to limit the indicate size to the size
1537 * of the first queued request.
1539 if ((toep->ddp.flags & DDP_SC_REQ) == 0)
1540 enable_ddp(sc, toep);
1546 * If another thread is queueing a buffer for DDP, let it
1547 * drain any work and return.
1549 if (toep->ddp.queueing != NULL)
1552 /* Take the next job to prep it for DDP. */
1553 toep->ddp.waiting_count--;
1554 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1555 if (!aio_clear_cancel_function(job))
1557 toep->ddp.queueing = job;
1559 /* NB: This drops DDP_LOCK while it holds the backing VM pages. */
1560 error = hold_aio(toep, job, &ps);
1562 ddp_complete_one(job, error);
1563 toep->ddp.queueing = NULL;
1568 if (so->so_error && sbavail(sb) == 0) {
1569 copied = job->aio_received;
1572 recycle_pageset(toep, ps);
1573 aio_complete(job, copied, 0);
1574 toep->ddp.queueing = NULL;
1578 error = so->so_error;
1581 recycle_pageset(toep, ps);
1582 aio_complete(job, -1, error);
1583 toep->ddp.queueing = NULL;
1587 if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1589 recycle_pageset(toep, ps);
1590 if (toep->ddp.active_count != 0) {
1592 * The door is closed, but there are still pending
1593 * DDP buffers. Requeue. These jobs will all be
1594 * completed once those buffers drain.
1596 aio_ddp_requeue_one(toep, job);
1597 toep->ddp.queueing = NULL;
1600 ddp_complete_one(job, 0);
1601 ddp_complete_all(toep, 0);
1602 toep->ddp.queueing = NULL;
1608 * If the toep is dead, there shouldn't be any data in the socket
1609 * buffer, so the above case should have handled this.
1611 MPASS(!(toep->ddp.flags & DDP_DEAD));
1614 * If there is pending data in the socket buffer (either
1615 * from before the requests were queued or a DDP indicate),
1616 * copy those mbufs out directly.
1619 offset = ps->offset + job->aio_received;
1620 MPASS(job->aio_received <= job->uaiocb.aio_nbytes);
1621 resid = job->uaiocb.aio_nbytes - job->aio_received;
1623 KASSERT(m == NULL || toep->ddp.active_count == 0,
1624 ("%s: sockbuf data with active DDP", __func__));
1625 while (m != NULL && resid > 0) {
1626 struct iovec iov[1];
1630 iov[0].iov_base = mtod(m, void *);
1631 iov[0].iov_len = m->m_len;
1632 if (iov[0].iov_len > resid)
1633 iov[0].iov_len = resid;
1637 uio.uio_resid = iov[0].iov_len;
1638 uio.uio_segflg = UIO_SYSSPACE;
1639 uio.uio_rw = UIO_WRITE;
1640 error = uiomove_fromphys(ps->pages, offset + copied,
1641 uio.uio_resid, &uio);
1642 MPASS(error == 0 && uio.uio_resid == 0);
1643 copied += uio.uio_offset;
1644 resid -= uio.uio_offset;
1648 sbdrop_locked(sb, copied);
1649 job->aio_received += copied;
1651 copied = job->aio_received;
1652 inp = sotoinpcb(so);
1653 if (!INP_TRY_WLOCK(inp)) {
1655 * The reference on the socket file descriptor in
1656 * the AIO job should keep 'sb' and 'inp' stable.
1657 * Our caller has a reference on the 'toep' that
1667 * If the socket has been closed, we should detect
1668 * that and complete this request if needed on
1669 * the next trip around the loop.
1672 t4_rcvd_locked(&toep->td->tod, intotcpcb(inp));
1674 if (resid == 0 || toep->ddp.flags & DDP_DEAD) {
1676 * We filled the entire buffer with socket
1677 * data, DDP is not being used, or the socket
1678 * is being shut down, so complete the
1682 recycle_pageset(toep, ps);
1683 aio_complete(job, copied, 0);
1684 toep->ddp.queueing = NULL;
1689 * If DDP is not enabled, requeue this request and restart.
1690 * This will either enable DDP or wait for more data to
1691 * arrive on the socket buffer.
1693 if ((toep->ddp.flags & (DDP_ON | DDP_SC_REQ)) != DDP_ON) {
1695 recycle_pageset(toep, ps);
1696 aio_ddp_requeue_one(toep, job);
1697 toep->ddp.queueing = NULL;
1702 * An indicate might have arrived and been added to
1703 * the socket buffer while it was unlocked after the
1704 * copy to lock the INP. If so, restart the copy.
1706 if (sbavail(sb) != 0)
1711 if (prep_pageset(sc, toep, ps) == 0) {
1712 recycle_pageset(toep, ps);
1713 aio_ddp_requeue_one(toep, job);
1714 toep->ddp.queueing = NULL;
1717 * XXX: Need to retry this later. Mostly need a trigger
1718 * when page pods are freed up.
1720 printf("%s: prep_pageset failed\n", __func__);
1724 /* Determine which DDP buffer to use. */
1725 if (toep->ddp.db[0].job == NULL) {
1728 MPASS(toep->ddp.db[1].job == NULL);
1735 ddp_flags |= V_TF_DDP_BUF0_VALID(1);
1736 if (so->so_state & SS_NBIO)
1737 ddp_flags |= V_TF_DDP_BUF0_FLUSH(1);
1738 ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE0(1) |
1739 V_TF_DDP_PUSH_DISABLE_0(1) | V_TF_DDP_PSHF_ENABLE_0(1) |
1740 V_TF_DDP_BUF0_FLUSH(1) | V_TF_DDP_BUF0_VALID(1);
1741 buf_flag = DDP_BUF0_ACTIVE;
1743 ddp_flags |= V_TF_DDP_BUF1_VALID(1);
1744 if (so->so_state & SS_NBIO)
1745 ddp_flags |= V_TF_DDP_BUF1_FLUSH(1);
1746 ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE1(1) |
1747 V_TF_DDP_PUSH_DISABLE_1(1) | V_TF_DDP_PSHF_ENABLE_1(1) |
1748 V_TF_DDP_BUF1_FLUSH(1) | V_TF_DDP_BUF1_VALID(1);
1749 buf_flag = DDP_BUF1_ACTIVE;
1751 MPASS((toep->ddp.flags & buf_flag) == 0);
1752 if ((toep->ddp.flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0) {
1754 MPASS(toep->ddp.active_id == -1);
1755 MPASS(toep->ddp.active_count == 0);
1756 ddp_flags_mask |= V_TF_DDP_ACTIVE_BUF(1);
1760 * The TID for this connection should still be valid. If DDP_DEAD
1761 * is set, SBS_CANTRCVMORE should be set, so we shouldn't be
1762 * this far anyway. Even if the socket is closing on the other
1763 * end, the AIO job holds a reference on this end of the socket
1764 * which will keep it open and keep the TCP PCB attached until
1765 * after the job is completed.
1767 wr = mk_update_tcb_for_ddp(sc, toep, db_idx, ps, job->aio_received,
1768 ddp_flags, ddp_flags_mask);
1770 recycle_pageset(toep, ps);
1771 aio_ddp_requeue_one(toep, job);
1772 toep->ddp.queueing = NULL;
1775 * XXX: Need a way to kick a retry here.
1777 * XXX: We know the fixed size needed and could
1778 * preallocate this using a blocking request at the
1779 * start of the task to avoid having to handle this
1782 printf("%s: mk_update_tcb_for_ddp failed\n", __func__);
1786 if (!aio_set_cancel_function(job, t4_aio_cancel_active)) {
1788 recycle_pageset(toep, ps);
1789 aio_ddp_cancel_one(job);
1790 toep->ddp.queueing = NULL;
1794 #ifdef VERBOSE_TRACES
1795 CTR5(KTR_CXGBE, "%s: scheduling %p for DDP[%d] (flags %#lx/%#lx)",
1796 __func__, job, db_idx, ddp_flags, ddp_flags_mask);
1798 /* Give the chip the go-ahead. */
1800 db = &toep->ddp.db[db_idx];
1801 db->cancel_pending = 0;
1804 toep->ddp.queueing = NULL;
1805 toep->ddp.flags |= buf_flag;
1806 toep->ddp.active_count++;
1807 if (toep->ddp.active_count == 1) {
1808 MPASS(toep->ddp.active_id == -1);
1809 toep->ddp.active_id = db_idx;
1810 CTR2(KTR_CXGBE, "%s: ddp_active_id = %d", __func__,
1811 toep->ddp.active_id);
1817 ddp_queue_toep(struct toepcb *toep)
1820 DDP_ASSERT_LOCKED(toep);
1821 if (toep->ddp.flags & DDP_TASK_ACTIVE)
1823 toep->ddp.flags |= DDP_TASK_ACTIVE;
1825 soaio_enqueue(&toep->ddp.requeue_task);
1829 aio_ddp_requeue_task(void *context, int pending)
1831 struct toepcb *toep = context;
1834 aio_ddp_requeue(toep);
1835 toep->ddp.flags &= ~DDP_TASK_ACTIVE;
1842 t4_aio_cancel_active(struct kaiocb *job)
1844 struct socket *so = job->fd_file->f_data;
1845 struct tcpcb *tp = so_sototcpcb(so);
1846 struct toepcb *toep = tp->t_toe;
1847 struct adapter *sc = td_adapter(toep->td);
1848 uint64_t valid_flag;
1852 if (aio_cancel_cleared(job)) {
1854 aio_ddp_cancel_one(job);
1858 for (i = 0; i < nitems(toep->ddp.db); i++) {
1859 if (toep->ddp.db[i].job == job) {
1860 /* Should only ever get one cancel request for a job. */
1861 MPASS(toep->ddp.db[i].cancel_pending == 0);
1864 * Invalidate this buffer. It will be
1865 * cancelled or partially completed once the
1866 * card ACKs the invalidate.
1868 valid_flag = i == 0 ? V_TF_DDP_BUF0_VALID(1) :
1869 V_TF_DDP_BUF1_VALID(1);
1870 t4_set_tcb_field(sc, toep->ctrlq, toep,
1871 W_TCB_RX_DDP_FLAGS, valid_flag, 0, 1,
1872 i + DDP_BUF0_INVALIDATED);
1873 toep->ddp.db[i].cancel_pending = 1;
1874 CTR2(KTR_CXGBE, "%s: request %p marked pending",
1883 t4_aio_cancel_queued(struct kaiocb *job)
1885 struct socket *so = job->fd_file->f_data;
1886 struct tcpcb *tp = so_sototcpcb(so);
1887 struct toepcb *toep = tp->t_toe;
1890 if (!aio_cancel_cleared(job)) {
1891 TAILQ_REMOVE(&toep->ddp.aiojobq, job, list);
1892 toep->ddp.waiting_count--;
1893 if (toep->ddp.waiting_count == 0)
1894 ddp_queue_toep(toep);
1896 CTR2(KTR_CXGBE, "%s: request %p cancelled", __func__, job);
1899 aio_ddp_cancel_one(job);
1903 t4_aio_queue_ddp(struct socket *so, struct kaiocb *job)
1905 struct tcpcb *tp = so_sototcpcb(so);
1906 struct toepcb *toep = tp->t_toe;
1909 /* Ignore writes. */
1910 if (job->uaiocb.aio_lio_opcode != LIO_READ)
1911 return (EOPNOTSUPP);
1916 * XXX: Think about possibly returning errors for ENOTCONN,
1917 * etc. Perhaps the caller would only queue the request
1918 * if it failed with EOPNOTSUPP?
1921 #ifdef VERBOSE_TRACES
1922 CTR2(KTR_CXGBE, "%s: queueing %p", __func__, job);
1924 if (!aio_set_cancel_function(job, t4_aio_cancel_queued))
1925 panic("new job was cancelled");
1926 TAILQ_INSERT_TAIL(&toep->ddp.aiojobq, job, list);
1927 toep->ddp.waiting_count++;
1928 toep->ddp.flags |= DDP_OK;
1931 * Try to handle this request synchronously. If this has
1932 * to block because the task is running, it will just bail
1933 * and let the task handle it instead.
1935 aio_ddp_requeue(toep);
1941 t4_ddp_mod_load(void)
1944 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, do_ddp_tcb_rpl,
1946 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, do_ddp_tcb_rpl,
1948 t4_register_cpl_handler(CPL_RX_DATA_DDP, do_rx_data_ddp);
1949 t4_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_rx_ddp_complete);
1950 TAILQ_INIT(&ddp_orphan_pagesets);
1951 mtx_init(&ddp_orphan_pagesets_lock, "ddp orphans", NULL, MTX_DEF);
1952 TASK_INIT(&ddp_orphan_task, 0, ddp_free_orphan_pagesets, NULL);
1956 t4_ddp_mod_unload(void)
1959 taskqueue_drain(taskqueue_thread, &ddp_orphan_task);
1960 MPASS(TAILQ_EMPTY(&ddp_orphan_pagesets));
1961 mtx_destroy(&ddp_orphan_pagesets_lock);
1962 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, NULL, CPL_COOKIE_DDP0);
1963 t4_register_shared_cpl_handler(CPL_SET_TCB_RPL, NULL, CPL_COOKIE_DDP1);
1964 t4_register_cpl_handler(CPL_RX_DATA_DDP, NULL);
1965 t4_register_cpl_handler(CPL_RX_DDP_COMPLETE, NULL);