xref: /freebsd/sys/dev/cxgbe/tom/t4_ddp.c (revision 18849b5da0c5eaa88500b457be05b038813b51b1)
1 /*-
2  * Copyright (c) 2012 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_inet.h"
32 
33 #include <sys/param.h>
34 #include <sys/aio.h>
35 #include <sys/file.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/ktr.h>
39 #include <sys/module.h>
40 #include <sys/protosw.h>
41 #include <sys/proc.h>
42 #include <sys/domain.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/taskqueue.h>
46 #include <sys/uio.h>
47 #include <netinet/in.h>
48 #include <netinet/in_pcb.h>
49 #include <netinet/ip.h>
50 #include <netinet/tcp_var.h>
51 #define TCPSTATES
52 #include <netinet/tcp_fsm.h>
53 #include <netinet/toecore.h>
54 
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_param.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_object.h>
62 
63 #ifdef TCP_OFFLOAD
64 #include "common/common.h"
65 #include "common/t4_msg.h"
66 #include "common/t4_regs.h"
67 #include "common/t4_tcb.h"
68 #include "tom/t4_tom.h"
69 
70 VNET_DECLARE(int, tcp_do_autorcvbuf);
71 #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf)
72 VNET_DECLARE(int, tcp_autorcvbuf_inc);
73 #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc)
74 VNET_DECLARE(int, tcp_autorcvbuf_max);
75 #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max)
76 
77 static void aio_ddp_requeue_task(void *context, int pending);
78 static void ddp_complete_all(struct toepcb *toep, int error);
79 static void t4_aio_cancel_active(struct kaiocb *job);
80 static void t4_aio_cancel_queued(struct kaiocb *job);
81 
82 #define PPOD_SZ(n)	((n) * sizeof(struct pagepod))
83 #define PPOD_SIZE	(PPOD_SZ(1))
84 
85 /* XXX: must match A_ULP_RX_TDDP_PSZ */
86 static int t4_ddp_pgsz[] = {4096, 4096 << 2, 4096 << 4, 4096 << 6};
87 
88 static TAILQ_HEAD(, pageset) ddp_orphan_pagesets;
89 static struct mtx ddp_orphan_pagesets_lock;
90 static struct task ddp_orphan_task;
91 
92 #define MAX_DDP_BUFFER_SIZE		(M_TCB_RX_DDP_BUF0_LEN)
93 static int
94 alloc_ppods(struct tom_data *td, int n, u_int *ppod_addr)
95 {
96 	vmem_addr_t v;
97 	int rc;
98 
99 	MPASS(n > 0);
100 
101 	rc = vmem_alloc(td->ppod_arena, PPOD_SZ(n), M_NOWAIT | M_FIRSTFIT, &v);
102 	*ppod_addr = (u_int)v;
103 
104 	return (rc);
105 }
106 
107 static void
108 free_ppods(struct tom_data *td, u_int ppod_addr, int n)
109 {
110 
111 	MPASS(n > 0);
112 
113 	vmem_free(td->ppod_arena, (vmem_addr_t)ppod_addr, PPOD_SZ(n));
114 }
115 
116 static inline int
117 pages_to_nppods(int npages, int ddp_pgsz)
118 {
119 	int nsegs = npages * PAGE_SIZE / ddp_pgsz;
120 
121 	return (howmany(nsegs, PPOD_PAGES));
122 }
123 
124 /*
125  * A page set holds information about a buffer used for DDP.  The page
126  * set holds resources such as the VM pages backing the buffer (either
127  * held or wired) and the page pods associated with the buffer.
128  * Recently used page sets are cached to allow for efficient reuse of
129  * buffers (avoiding the need to re-fault in pages, hold them, etc.).
130  * Note that cached page sets keep the backing pages wired.  The
131  * number of wired pages is capped by only allowing for two wired
132  * pagesets per connection.  This is not a perfect cap, but is a
133  * trade-off for performance.
134  *
135  * If an application ping-pongs two buffers for a connection via
136  * aio_read(2) then those buffers should remain wired and expensive VM
137  * fault lookups should be avoided after each buffer has been used
138  * once.  If an application uses more than two buffers then this will
139  * fall back to doing expensive VM fault lookups for each operation.
140  */
141 static void
142 free_pageset(struct tom_data *td, struct pageset *ps)
143 {
144 	vm_page_t p;
145 	int i;
146 
147 	if (ps->nppods > 0)
148 		free_ppods(td, ps->ppod_addr, ps->nppods);
149 
150 	if (ps->flags & PS_WIRED) {
151 		for (i = 0; i < ps->npages; i++) {
152 			p = ps->pages[i];
153 			vm_page_lock(p);
154 			vm_page_unwire(p, PQ_INACTIVE);
155 			vm_page_unlock(p);
156 		}
157 	} else
158 		vm_page_unhold_pages(ps->pages, ps->npages);
159 	mtx_lock(&ddp_orphan_pagesets_lock);
160 	TAILQ_INSERT_TAIL(&ddp_orphan_pagesets, ps, link);
161 	taskqueue_enqueue(taskqueue_thread, &ddp_orphan_task);
162 	mtx_unlock(&ddp_orphan_pagesets_lock);
163 }
164 
165 static void
166 ddp_free_orphan_pagesets(void *context, int pending)
167 {
168 	struct pageset *ps;
169 
170 	mtx_lock(&ddp_orphan_pagesets_lock);
171 	while (!TAILQ_EMPTY(&ddp_orphan_pagesets)) {
172 		ps = TAILQ_FIRST(&ddp_orphan_pagesets);
173 		TAILQ_REMOVE(&ddp_orphan_pagesets, ps, link);
174 		mtx_unlock(&ddp_orphan_pagesets_lock);
175 		if (ps->vm)
176 			vmspace_free(ps->vm);
177 		free(ps, M_CXGBE);
178 		mtx_lock(&ddp_orphan_pagesets_lock);
179 	}
180 	mtx_unlock(&ddp_orphan_pagesets_lock);
181 }
182 
183 static void
184 recycle_pageset(struct toepcb *toep, struct pageset *ps)
185 {
186 
187 	DDP_ASSERT_LOCKED(toep);
188 	if (!(toep->ddp_flags & DDP_DEAD) && ps->flags & PS_WIRED) {
189 		KASSERT(toep->ddp_cached_count + toep->ddp_active_count <
190 		    nitems(toep->db), ("too many wired pagesets"));
191 		TAILQ_INSERT_HEAD(&toep->ddp_cached_pagesets, ps, link);
192 		toep->ddp_cached_count++;
193 	} else
194 		free_pageset(toep->td, ps);
195 }
196 
197 static void
198 ddp_complete_one(struct kaiocb *job, int error)
199 {
200 	long copied;
201 
202 	/*
203 	 * If this job had copied data out of the socket buffer before
204 	 * it was cancelled, report it as a short read rather than an
205 	 * error.
206 	 */
207 	copied = job->uaiocb._aiocb_private.status;
208 	if (copied != 0 || error == 0)
209 		aio_complete(job, copied, 0);
210 	else
211 		aio_complete(job, -1, error);
212 }
213 
214 static void
215 free_ddp_buffer(struct tom_data *td, struct ddp_buffer *db)
216 {
217 
218 	if (db->job) {
219 		/*
220 		 * XXX: If we are un-offloading the socket then we
221 		 * should requeue these on the socket somehow.  If we
222 		 * got a FIN from the remote end, then this completes
223 		 * any remaining requests with an EOF read.
224 		 */
225 		if (!aio_clear_cancel_function(db->job))
226 			ddp_complete_one(db->job, 0);
227 	}
228 
229 	if (db->ps)
230 		free_pageset(td, db->ps);
231 }
232 
233 void
234 ddp_init_toep(struct toepcb *toep)
235 {
236 
237 	TAILQ_INIT(&toep->ddp_aiojobq);
238 	TASK_INIT(&toep->ddp_requeue_task, 0, aio_ddp_requeue_task, toep);
239 	toep->ddp_active_id = -1;
240 	mtx_init(&toep->ddp_lock, "t4 ddp", NULL, MTX_DEF);
241 }
242 
243 void
244 ddp_uninit_toep(struct toepcb *toep)
245 {
246 
247 	mtx_destroy(&toep->ddp_lock);
248 }
249 
250 void
251 release_ddp_resources(struct toepcb *toep)
252 {
253 	struct pageset *ps;
254 	int i;
255 
256 	DDP_LOCK(toep);
257 	toep->flags |= DDP_DEAD;
258 	for (i = 0; i < nitems(toep->db); i++) {
259 		free_ddp_buffer(toep->td, &toep->db[i]);
260 	}
261 	while ((ps = TAILQ_FIRST(&toep->ddp_cached_pagesets)) != NULL) {
262 		TAILQ_REMOVE(&toep->ddp_cached_pagesets, ps, link);
263 		free_pageset(toep->td, ps);
264 	}
265 	ddp_complete_all(toep, 0);
266 	DDP_UNLOCK(toep);
267 }
268 
269 #ifdef INVARIANTS
270 void
271 ddp_assert_empty(struct toepcb *toep)
272 {
273 	int i;
274 
275 	MPASS(!(toep->ddp_flags & DDP_TASK_ACTIVE));
276 	for (i = 0; i < nitems(toep->db); i++) {
277 		MPASS(toep->db[i].job == NULL);
278 		MPASS(toep->db[i].ps == NULL);
279 	}
280 	MPASS(TAILQ_EMPTY(&toep->ddp_cached_pagesets));
281 	MPASS(TAILQ_EMPTY(&toep->ddp_aiojobq));
282 }
283 #endif
284 
285 static void
286 complete_ddp_buffer(struct toepcb *toep, struct ddp_buffer *db,
287     unsigned int db_idx)
288 {
289 	unsigned int db_flag;
290 
291 	toep->ddp_active_count--;
292 	if (toep->ddp_active_id == db_idx) {
293 		if (toep->ddp_active_count == 0) {
294 			KASSERT(toep->db[db_idx ^ 1].job == NULL,
295 			    ("%s: active_count mismatch", __func__));
296 			toep->ddp_active_id = -1;
297 		} else
298 			toep->ddp_active_id ^= 1;
299 		CTR2(KTR_CXGBE, "%s: ddp_active_id = %d", __func__,
300 		    toep->ddp_active_id);
301 	} else {
302 		KASSERT(toep->ddp_active_count != 0 &&
303 		    toep->ddp_active_id != -1,
304 		    ("%s: active count mismatch", __func__));
305 	}
306 
307 	db->cancel_pending = 0;
308 	db->job = NULL;
309 	recycle_pageset(toep, db->ps);
310 	db->ps = NULL;
311 
312 	db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
313 	KASSERT(toep->ddp_flags & db_flag,
314 	    ("%s: DDP buffer not active. toep %p, ddp_flags 0x%x",
315 	    __func__, toep, toep->ddp_flags));
316 	toep->ddp_flags &= ~db_flag;
317 }
318 
319 /* XXX: handle_ddp_data code duplication */
320 void
321 insert_ddp_data(struct toepcb *toep, uint32_t n)
322 {
323 	struct inpcb *inp = toep->inp;
324 	struct tcpcb *tp = intotcpcb(inp);
325 	struct ddp_buffer *db;
326 	struct kaiocb *job;
327 	size_t placed;
328 	long copied;
329 	unsigned int db_flag, db_idx;
330 
331 	INP_WLOCK_ASSERT(inp);
332 	DDP_ASSERT_LOCKED(toep);
333 
334 	tp->rcv_nxt += n;
335 #ifndef USE_DDP_RX_FLOW_CONTROL
336 	KASSERT(tp->rcv_wnd >= n, ("%s: negative window size", __func__));
337 	tp->rcv_wnd -= n;
338 #endif
339 #ifndef USE_DDP_RX_FLOW_CONTROL
340 	toep->rx_credits += n;
341 #endif
342 	CTR2(KTR_CXGBE, "%s: placed %u bytes before falling out of DDP",
343 	    __func__, n);
344 	while (toep->ddp_active_count > 0) {
345 		MPASS(toep->ddp_active_id != -1);
346 		db_idx = toep->ddp_active_id;
347 		db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
348 		MPASS((toep->ddp_flags & db_flag) != 0);
349 		db = &toep->db[db_idx];
350 		job = db->job;
351 		copied = job->uaiocb._aiocb_private.status;
352 		placed = n;
353 		if (placed > job->uaiocb.aio_nbytes - copied)
354 			placed = job->uaiocb.aio_nbytes - copied;
355 		if (!aio_clear_cancel_function(job)) {
356 			/*
357 			 * Update the copied length for when
358 			 * t4_aio_cancel_active() completes this
359 			 * request.
360 			 */
361 			job->uaiocb._aiocb_private.status += placed;
362 		} else if (copied + placed != 0) {
363 			CTR4(KTR_CXGBE,
364 			    "%s: completing %p (copied %ld, placed %lu)",
365 			    __func__, job, copied, placed);
366 			/* XXX: This always completes if there is some data. */
367 			aio_complete(job, copied + placed, 0);
368 		} else if (aio_set_cancel_function(job, t4_aio_cancel_queued)) {
369 			TAILQ_INSERT_HEAD(&toep->ddp_aiojobq, job, list);
370 			toep->ddp_waiting_count++;
371 		} else
372 			aio_cancel(job);
373 		n -= placed;
374 		complete_ddp_buffer(toep, db, db_idx);
375 	}
376 
377 	MPASS(n == 0);
378 }
379 
380 /* SET_TCB_FIELD sent as a ULP command looks like this */
381 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \
382     sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core))
383 
384 /* RX_DATA_ACK sent as a ULP command looks like this */
385 #define LEN__RX_DATA_ACK_ULP (sizeof(struct ulp_txpkt) + \
386     sizeof(struct ulptx_idata) + sizeof(struct cpl_rx_data_ack_core))
387 
388 static inline void *
389 mk_set_tcb_field_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep,
390     uint64_t word, uint64_t mask, uint64_t val)
391 {
392 	struct ulptx_idata *ulpsc;
393 	struct cpl_set_tcb_field_core *req;
394 
395 	ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
396 	ulpmc->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16));
397 
398 	ulpsc = (struct ulptx_idata *)(ulpmc + 1);
399 	ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
400 	ulpsc->len = htobe32(sizeof(*req));
401 
402 	req = (struct cpl_set_tcb_field_core *)(ulpsc + 1);
403 	OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, toep->tid));
404 	req->reply_ctrl = htobe16(V_NO_REPLY(1) |
405 	    V_QUEUENO(toep->ofld_rxq->iq.abs_id));
406 	req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
407         req->mask = htobe64(mask);
408         req->val = htobe64(val);
409 
410 	ulpsc = (struct ulptx_idata *)(req + 1);
411 	if (LEN__SET_TCB_FIELD_ULP % 16) {
412 		ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
413 		ulpsc->len = htobe32(0);
414 		return (ulpsc + 1);
415 	}
416 	return (ulpsc);
417 }
418 
419 static inline void *
420 mk_rx_data_ack_ulp(struct ulp_txpkt *ulpmc, struct toepcb *toep)
421 {
422 	struct ulptx_idata *ulpsc;
423 	struct cpl_rx_data_ack_core *req;
424 
425 	ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
426 	ulpmc->len = htobe32(howmany(LEN__RX_DATA_ACK_ULP, 16));
427 
428 	ulpsc = (struct ulptx_idata *)(ulpmc + 1);
429 	ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
430 	ulpsc->len = htobe32(sizeof(*req));
431 
432 	req = (struct cpl_rx_data_ack_core *)(ulpsc + 1);
433 	OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_RX_DATA_ACK, toep->tid));
434 	req->credit_dack = htobe32(F_RX_MODULATE_RX);
435 
436 	ulpsc = (struct ulptx_idata *)(req + 1);
437 	if (LEN__RX_DATA_ACK_ULP % 16) {
438 		ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
439 		ulpsc->len = htobe32(0);
440 		return (ulpsc + 1);
441 	}
442 	return (ulpsc);
443 }
444 
445 static struct wrqe *
446 mk_update_tcb_for_ddp(struct adapter *sc, struct toepcb *toep, int db_idx,
447     struct pageset *ps, int offset, uint64_t ddp_flags, uint64_t ddp_flags_mask)
448 {
449 	struct wrqe *wr;
450 	struct work_request_hdr *wrh;
451 	struct ulp_txpkt *ulpmc;
452 	int len;
453 
454 	KASSERT(db_idx == 0 || db_idx == 1,
455 	    ("%s: bad DDP buffer index %d", __func__, db_idx));
456 
457 	/*
458 	 * We'll send a compound work request that has 3 SET_TCB_FIELDs and an
459 	 * RX_DATA_ACK (with RX_MODULATE to speed up delivery).
460 	 *
461 	 * The work request header is 16B and always ends at a 16B boundary.
462 	 * The ULPTX master commands that follow must all end at 16B boundaries
463 	 * too so we round up the size to 16.
464 	 */
465 	len = sizeof(*wrh) + 3 * roundup2(LEN__SET_TCB_FIELD_ULP, 16) +
466 	    roundup2(LEN__RX_DATA_ACK_ULP, 16);
467 
468 	wr = alloc_wrqe(len, toep->ctrlq);
469 	if (wr == NULL)
470 		return (NULL);
471 	wrh = wrtod(wr);
472 	INIT_ULPTX_WRH(wrh, len, 1, 0);	/* atomic */
473 	ulpmc = (struct ulp_txpkt *)(wrh + 1);
474 
475 	/* Write the buffer's tag */
476 	ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
477 	    W_TCB_RX_DDP_BUF0_TAG + db_idx,
478 	    V_TCB_RX_DDP_BUF0_TAG(M_TCB_RX_DDP_BUF0_TAG),
479 	    V_TCB_RX_DDP_BUF0_TAG(ps->tag));
480 
481 	/* Update the current offset in the DDP buffer and its total length */
482 	if (db_idx == 0)
483 		ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
484 		    W_TCB_RX_DDP_BUF0_OFFSET,
485 		    V_TCB_RX_DDP_BUF0_OFFSET(M_TCB_RX_DDP_BUF0_OFFSET) |
486 		    V_TCB_RX_DDP_BUF0_LEN(M_TCB_RX_DDP_BUF0_LEN),
487 		    V_TCB_RX_DDP_BUF0_OFFSET(offset) |
488 		    V_TCB_RX_DDP_BUF0_LEN(ps->len));
489 	else
490 		ulpmc = mk_set_tcb_field_ulp(ulpmc, toep,
491 		    W_TCB_RX_DDP_BUF1_OFFSET,
492 		    V_TCB_RX_DDP_BUF1_OFFSET(M_TCB_RX_DDP_BUF1_OFFSET) |
493 		    V_TCB_RX_DDP_BUF1_LEN((u64)M_TCB_RX_DDP_BUF1_LEN << 32),
494 		    V_TCB_RX_DDP_BUF1_OFFSET(offset) |
495 		    V_TCB_RX_DDP_BUF1_LEN((u64)ps->len << 32));
496 
497 	/* Update DDP flags */
498 	ulpmc = mk_set_tcb_field_ulp(ulpmc, toep, W_TCB_RX_DDP_FLAGS,
499 	    ddp_flags_mask, ddp_flags);
500 
501 	/* Gratuitous RX_DATA_ACK with RX_MODULATE set to speed up delivery. */
502 	ulpmc = mk_rx_data_ack_ulp(ulpmc, toep);
503 
504 	return (wr);
505 }
506 
507 static int
508 handle_ddp_data(struct toepcb *toep, __be32 ddp_report, __be32 rcv_nxt, int len)
509 {
510 	uint32_t report = be32toh(ddp_report);
511 	unsigned int db_idx;
512 	struct inpcb *inp = toep->inp;
513 	struct ddp_buffer *db;
514 	struct tcpcb *tp;
515 	struct socket *so;
516 	struct sockbuf *sb;
517 	struct kaiocb *job;
518 	long copied;
519 
520 	db_idx = report & F_DDP_BUF_IDX ? 1 : 0;
521 
522 	if (__predict_false(!(report & F_DDP_INV)))
523 		CXGBE_UNIMPLEMENTED("DDP buffer still valid");
524 
525 	INP_WLOCK(inp);
526 	so = inp_inpcbtosocket(inp);
527 	sb = &so->so_rcv;
528 	DDP_LOCK(toep);
529 
530 	KASSERT(toep->ddp_active_id == db_idx,
531 	    ("completed DDP buffer (%d) != active_id (%d) for tid %d", db_idx,
532 	    toep->ddp_active_id, toep->tid));
533 	db = &toep->db[db_idx];
534 	job = db->job;
535 
536 	if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
537 		/*
538 		 * This can happen due to an administrative tcpdrop(8).
539 		 * Just fail the request with ECONNRESET.
540 		 */
541 		CTR5(KTR_CXGBE, "%s: tid %u, seq 0x%x, len %d, inp_flags 0x%x",
542 		    __func__, toep->tid, be32toh(rcv_nxt), len, inp->inp_flags);
543 		if (aio_clear_cancel_function(job))
544 			ddp_complete_one(job, ECONNRESET);
545 		goto completed;
546 	}
547 
548 	tp = intotcpcb(inp);
549 
550 	/*
551 	 * For RX_DDP_COMPLETE, len will be zero and rcv_nxt is the
552 	 * sequence number of the next byte to receive.  The length of
553 	 * the data received for this message must be computed by
554 	 * comparing the new and old values of rcv_nxt.
555 	 *
556 	 * For RX_DATA_DDP, len might be non-zero, but it is only the
557 	 * length of the most recent DMA.  It does not include the
558 	 * total length of the data received since the previous update
559 	 * for this DDP buffer.  rcv_nxt is the sequence number of the
560 	 * first received byte from the most recent DMA.
561 	 */
562 	len += be32toh(rcv_nxt) - tp->rcv_nxt;
563 	tp->rcv_nxt += len;
564 	tp->t_rcvtime = ticks;
565 #ifndef USE_DDP_RX_FLOW_CONTROL
566 	KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
567 	tp->rcv_wnd -= len;
568 #endif
569 #ifdef VERBOSE_TRACES
570 	CTR4(KTR_CXGBE, "%s: DDP[%d] placed %d bytes (%#x)", __func__, db_idx,
571 	    len, report);
572 #endif
573 
574 	/* receive buffer autosize */
575 	CURVNET_SET(so->so_vnet);
576 	SOCKBUF_LOCK(sb);
577 	if (sb->sb_flags & SB_AUTOSIZE &&
578 	    V_tcp_do_autorcvbuf &&
579 	    sb->sb_hiwat < V_tcp_autorcvbuf_max &&
580 	    len > (sbspace(sb) / 8 * 7)) {
581 		unsigned int hiwat = sb->sb_hiwat;
582 		unsigned int newsize = min(hiwat + V_tcp_autorcvbuf_inc,
583 		    V_tcp_autorcvbuf_max);
584 
585 		if (!sbreserve_locked(sb, newsize, so, NULL))
586 			sb->sb_flags &= ~SB_AUTOSIZE;
587 		else
588 			toep->rx_credits += newsize - hiwat;
589 	}
590 	SOCKBUF_UNLOCK(sb);
591 	CURVNET_RESTORE();
592 
593 #ifndef USE_DDP_RX_FLOW_CONTROL
594 	toep->rx_credits += len;
595 #endif
596 
597 	if (db->cancel_pending) {
598 		/*
599 		 * Update the job's length but defer completion to the
600 		 * TCB_RPL callback.
601 		 */
602 		job->uaiocb._aiocb_private.status += len;
603 		goto out;
604 	} else if (!aio_clear_cancel_function(job)) {
605 		/*
606 		 * Update the copied length for when
607 		 * t4_aio_cancel_active() completes this request.
608 		 */
609 		job->uaiocb._aiocb_private.status += len;
610 	} else {
611 		copied = job->uaiocb._aiocb_private.status;
612 #ifdef VERBOSE_TRACES
613 		CTR4(KTR_CXGBE, "%s: completing %p (copied %ld, placed %d)",
614 		    __func__, job, copied, len);
615 #endif
616 		aio_complete(job, copied + len, 0);
617 		t4_rcvd(&toep->td->tod, tp);
618 	}
619 
620 completed:
621 	complete_ddp_buffer(toep, db, db_idx);
622 	if (toep->ddp_waiting_count > 0)
623 		ddp_queue_toep(toep);
624 out:
625 	DDP_UNLOCK(toep);
626 	INP_WUNLOCK(inp);
627 
628 	return (0);
629 }
630 
631 void
632 handle_ddp_indicate(struct toepcb *toep)
633 {
634 
635 	DDP_ASSERT_LOCKED(toep);
636 	MPASS(toep->ddp_active_count == 0);
637 	MPASS((toep->ddp_flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0);
638 	if (toep->ddp_waiting_count == 0) {
639 		/*
640 		 * The pending requests that triggered the request for an
641 		 * an indicate were cancelled.  Those cancels should have
642 		 * already disabled DDP.  Just ignore this as the data is
643 		 * going into the socket buffer anyway.
644 		 */
645 		return;
646 	}
647 	CTR3(KTR_CXGBE, "%s: tid %d indicated (%d waiting)", __func__,
648 	    toep->tid, toep->ddp_waiting_count);
649 	ddp_queue_toep(toep);
650 }
651 
652 enum {
653 	DDP_BUF0_INVALIDATED = 0x2,
654 	DDP_BUF1_INVALIDATED
655 };
656 
657 void
658 handle_ddp_tcb_rpl(struct toepcb *toep, const struct cpl_set_tcb_rpl *cpl)
659 {
660 	unsigned int db_idx;
661 	struct inpcb *inp = toep->inp;
662 	struct ddp_buffer *db;
663 	struct kaiocb *job;
664 	long copied;
665 
666 	if (cpl->status != CPL_ERR_NONE)
667 		panic("XXX: tcp_rpl failed: %d", cpl->status);
668 
669 	switch (cpl->cookie) {
670 	case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF0_INVALIDATED):
671 	case V_WORD(W_TCB_RX_DDP_FLAGS) | V_COOKIE(DDP_BUF1_INVALIDATED):
672 		/*
673 		 * XXX: This duplicates a lot of code with handle_ddp_data().
674 		 */
675 		db_idx = G_COOKIE(cpl->cookie) - DDP_BUF0_INVALIDATED;
676 		INP_WLOCK(inp);
677 		DDP_LOCK(toep);
678 		db = &toep->db[db_idx];
679 
680 		/*
681 		 * handle_ddp_data() should leave the job around until
682 		 * this callback runs once a cancel is pending.
683 		 */
684 		MPASS(db != NULL);
685 		MPASS(db->job != NULL);
686 		MPASS(db->cancel_pending);
687 
688 		/*
689 		 * XXX: It's not clear what happens if there is data
690 		 * placed when the buffer is invalidated.  I suspect we
691 		 * need to read the TCB to see how much data was placed.
692 		 *
693 		 * For now this just pretends like nothing was placed.
694 		 *
695 		 * XXX: Note that if we did check the PCB we would need to
696 		 * also take care of updating the tp, etc.
697 		 */
698 		job = db->job;
699 		copied = job->uaiocb._aiocb_private.status;
700 		if (copied == 0) {
701 			CTR2(KTR_CXGBE, "%s: cancelling %p", __func__, job);
702 			aio_cancel(job);
703 		} else {
704 			CTR3(KTR_CXGBE, "%s: completing %p (copied %ld)",
705 			    __func__, job, copied);
706 			aio_complete(job, copied, 0);
707 			t4_rcvd(&toep->td->tod, intotcpcb(inp));
708 		}
709 
710 		complete_ddp_buffer(toep, db, db_idx);
711 		if (toep->ddp_waiting_count > 0)
712 			ddp_queue_toep(toep);
713 		DDP_UNLOCK(toep);
714 		INP_WUNLOCK(inp);
715 		break;
716 	default:
717 		panic("XXX: unknown tcb_rpl offset %#x, cookie %#x",
718 		    G_WORD(cpl->cookie), G_COOKIE(cpl->cookie));
719 	}
720 }
721 
722 void
723 handle_ddp_close(struct toepcb *toep, struct tcpcb *tp, __be32 rcv_nxt)
724 {
725 	struct ddp_buffer *db;
726 	struct kaiocb *job;
727 	long copied;
728 	unsigned int db_flag, db_idx;
729 	int len, placed;
730 
731 	INP_WLOCK_ASSERT(toep->inp);
732 	DDP_ASSERT_LOCKED(toep);
733 	len = be32toh(rcv_nxt) - tp->rcv_nxt;
734 
735 	tp->rcv_nxt += len;
736 #ifndef USE_DDP_RX_FLOW_CONTROL
737 	toep->rx_credits += len;
738 #endif
739 
740 	while (toep->ddp_active_count > 0) {
741 		MPASS(toep->ddp_active_id != -1);
742 		db_idx = toep->ddp_active_id;
743 		db_flag = db_idx == 1 ? DDP_BUF1_ACTIVE : DDP_BUF0_ACTIVE;
744 		MPASS((toep->ddp_flags & db_flag) != 0);
745 		db = &toep->db[db_idx];
746 		job = db->job;
747 		copied = job->uaiocb._aiocb_private.status;
748 		placed = len;
749 		if (placed > job->uaiocb.aio_nbytes - copied)
750 			placed = job->uaiocb.aio_nbytes - copied;
751 		if (!aio_clear_cancel_function(job)) {
752 			/*
753 			 * Update the copied length for when
754 			 * t4_aio_cancel_active() completes this
755 			 * request.
756 			 */
757 			job->uaiocb._aiocb_private.status += placed;
758 		} else {
759 			CTR4(KTR_CXGBE, "%s: tid %d completed buf %d len %d",
760 			    __func__, toep->tid, db_idx, placed);
761 			aio_complete(job, copied + placed, 0);
762 		}
763 		len -= placed;
764 		complete_ddp_buffer(toep, db, db_idx);
765 	}
766 
767 	MPASS(len == 0);
768 	ddp_complete_all(toep, 0);
769 }
770 
771 #define DDP_ERR (F_DDP_PPOD_MISMATCH | F_DDP_LLIMIT_ERR | F_DDP_ULIMIT_ERR |\
772 	 F_DDP_PPOD_PARITY_ERR | F_DDP_PADDING_ERR | F_DDP_OFFSET_ERR |\
773 	 F_DDP_INVALID_TAG | F_DDP_COLOR_ERR | F_DDP_TID_MISMATCH |\
774 	 F_DDP_INVALID_PPOD | F_DDP_HDRCRC_ERR | F_DDP_DATACRC_ERR)
775 
776 static int
777 do_rx_data_ddp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
778 {
779 	struct adapter *sc = iq->adapter;
780 	const struct cpl_rx_data_ddp *cpl = (const void *)(rss + 1);
781 	unsigned int tid = GET_TID(cpl);
782 	uint32_t vld;
783 	struct toepcb *toep = lookup_tid(sc, tid);
784 
785 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
786 	KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
787 	KASSERT(!(toep->flags & TPF_SYNQE),
788 	    ("%s: toep %p claims to be a synq entry", __func__, toep));
789 
790 	vld = be32toh(cpl->ddpvld);
791 	if (__predict_false(vld & DDP_ERR)) {
792 		panic("%s: DDP error 0x%x (tid %d, toep %p)",
793 		    __func__, vld, tid, toep);
794 	}
795 
796 	if (toep->ulp_mode == ULP_MODE_ISCSI) {
797 		sc->cpl_handler[CPL_RX_ISCSI_DDP](iq, rss, m);
798 		return (0);
799 	}
800 
801 	handle_ddp_data(toep, cpl->u.ddp_report, cpl->seq, be16toh(cpl->len));
802 
803 	return (0);
804 }
805 
806 static int
807 do_rx_ddp_complete(struct sge_iq *iq, const struct rss_header *rss,
808     struct mbuf *m)
809 {
810 	struct adapter *sc = iq->adapter;
811 	const struct cpl_rx_ddp_complete *cpl = (const void *)(rss + 1);
812 	unsigned int tid = GET_TID(cpl);
813 	struct toepcb *toep = lookup_tid(sc, tid);
814 
815 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
816 	KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
817 	KASSERT(!(toep->flags & TPF_SYNQE),
818 	    ("%s: toep %p claims to be a synq entry", __func__, toep));
819 
820 	handle_ddp_data(toep, cpl->ddp_report, cpl->rcv_nxt, 0);
821 
822 	return (0);
823 }
824 
825 static void
826 enable_ddp(struct adapter *sc, struct toepcb *toep)
827 {
828 
829 	KASSERT((toep->ddp_flags & (DDP_ON | DDP_OK | DDP_SC_REQ)) == DDP_OK,
830 	    ("%s: toep %p has bad ddp_flags 0x%x",
831 	    __func__, toep, toep->ddp_flags));
832 
833 	CTR3(KTR_CXGBE, "%s: tid %u (time %u)",
834 	    __func__, toep->tid, time_uptime);
835 
836 	DDP_ASSERT_LOCKED(toep);
837 	toep->ddp_flags |= DDP_SC_REQ;
838 	t4_set_tcb_field(sc, toep, 1, W_TCB_RX_DDP_FLAGS,
839 	    V_TF_DDP_OFF(1) | V_TF_DDP_INDICATE_OUT(1) |
840 	    V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1) |
841 	    V_TF_DDP_BUF0_VALID(1) | V_TF_DDP_BUF1_VALID(1),
842 	    V_TF_DDP_BUF0_INDICATE(1) | V_TF_DDP_BUF1_INDICATE(1));
843 	t4_set_tcb_field(sc, toep, 1, W_TCB_T_FLAGS,
844 	    V_TF_RCV_COALESCE_ENABLE(1), 0);
845 }
846 
847 static int
848 calculate_hcf(int n1, int n2)
849 {
850 	int a, b, t;
851 
852 	if (n1 <= n2) {
853 		a = n1;
854 		b = n2;
855 	} else {
856 		a = n2;
857 		b = n1;
858 	}
859 
860 	while (a != 0) {
861 		t = a;
862 		a = b % a;
863 		b = t;
864 	}
865 
866 	return (b);
867 }
868 
869 static int
870 alloc_page_pods(struct tom_data *td, struct pageset *ps)
871 {
872 	int i, hcf, seglen, idx, ppod, nppods;
873 	u_int ppod_addr;
874 
875 	KASSERT(ps->nppods == 0, ("%s: page pods already allocated", __func__));
876 
877 	/*
878 	 * The DDP page size is unrelated to the VM page size.  We combine
879 	 * contiguous physical pages into larger segments to get the best DDP
880 	 * page size possible.  This is the largest of the four sizes in
881 	 * A_ULP_RX_TDDP_PSZ that evenly divides the HCF of the segment sizes in
882 	 * the page list.
883 	 */
884 	hcf = 0;
885 	for (i = 0; i < ps->npages; i++) {
886 		seglen = PAGE_SIZE;
887 		while (i < ps->npages - 1 &&
888 		    ps->pages[i]->phys_addr + PAGE_SIZE ==
889 		    ps->pages[i + 1]->phys_addr) {
890 			seglen += PAGE_SIZE;
891 			i++;
892 		}
893 
894 		hcf = calculate_hcf(hcf, seglen);
895 		if (hcf < t4_ddp_pgsz[1]) {
896 			idx = 0;
897 			goto have_pgsz;	/* give up, short circuit */
898 		}
899 	}
900 
901 	if (hcf % t4_ddp_pgsz[0] != 0) {
902 		/* hmmm.  This could only happen when PAGE_SIZE < 4K */
903 		KASSERT(PAGE_SIZE < 4096,
904 		    ("%s: PAGE_SIZE %d, hcf %d", __func__, PAGE_SIZE, hcf));
905 		CTR3(KTR_CXGBE, "%s: PAGE_SIZE %d, hcf %d",
906 		    __func__, PAGE_SIZE, hcf);
907 		return (0);
908 	}
909 
910 	for (idx = nitems(t4_ddp_pgsz) - 1; idx > 0; idx--) {
911 		if (hcf % t4_ddp_pgsz[idx] == 0)
912 			break;
913 	}
914 have_pgsz:
915 	MPASS(idx <= M_PPOD_PGSZ);
916 
917 	nppods = pages_to_nppods(ps->npages, t4_ddp_pgsz[idx]);
918 	if (alloc_ppods(td, nppods, &ppod_addr) != 0) {
919 		CTR4(KTR_CXGBE, "%s: no pods, nppods %d, npages %d, pgsz %d",
920 		    __func__, nppods, ps->npages, t4_ddp_pgsz[idx]);
921 		return (0);
922 	}
923 
924 	ppod = (ppod_addr - td->ppod_start) / PPOD_SIZE;
925 	ps->tag = V_PPOD_PGSZ(idx) | V_PPOD_TAG(ppod);
926 	ps->ppod_addr = ppod_addr;
927 	ps->nppods = nppods;
928 
929 	CTR5(KTR_CXGBE, "New page pods.  "
930 	    "ps %p, ddp_pgsz %d, ppod 0x%x, npages %d, nppods %d",
931 	    ps, t4_ddp_pgsz[idx], ppod, ps->npages, ps->nppods);
932 
933 	return (1);
934 }
935 
936 #define NUM_ULP_TX_SC_IMM_PPODS (256 / PPOD_SIZE)
937 
938 static int
939 write_page_pods(struct adapter *sc, struct toepcb *toep, struct pageset *ps)
940 {
941 	struct wrqe *wr;
942 	struct ulp_mem_io *ulpmc;
943 	struct ulptx_idata *ulpsc;
944 	struct pagepod *ppod;
945 	int i, j, k, n, chunk, len, ddp_pgsz, idx;
946 	u_int ppod_addr;
947 	uint32_t cmd;
948 
949 	KASSERT(!(ps->flags & PS_PPODS_WRITTEN),
950 	    ("%s: page pods already written", __func__));
951 
952 	cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE));
953 	if (is_t4(sc))
954 		cmd |= htobe32(F_ULP_MEMIO_ORDER);
955 	else
956 		cmd |= htobe32(F_T5_ULP_MEMIO_IMM);
957 	ddp_pgsz = t4_ddp_pgsz[G_PPOD_PGSZ(ps->tag)];
958 	ppod_addr = ps->ppod_addr;
959 	for (i = 0; i < ps->nppods; ppod_addr += chunk) {
960 
961 		/* How many page pods are we writing in this cycle */
962 		n = min(ps->nppods - i, NUM_ULP_TX_SC_IMM_PPODS);
963 		chunk = PPOD_SZ(n);
964 		len = roundup2(sizeof(*ulpmc) + sizeof(*ulpsc) + chunk, 16);
965 
966 		wr = alloc_wrqe(len, toep->ctrlq);
967 		if (wr == NULL)
968 			return (ENOMEM);	/* ok to just bail out */
969 		ulpmc = wrtod(wr);
970 
971 		INIT_ULPTX_WR(ulpmc, len, 0, 0);
972 		ulpmc->cmd = cmd;
973 		ulpmc->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(chunk / 32));
974 		ulpmc->len16 = htobe32(howmany(len - sizeof(ulpmc->wr), 16));
975 		ulpmc->lock_addr = htobe32(V_ULP_MEMIO_ADDR(ppod_addr >> 5));
976 
977 		ulpsc = (struct ulptx_idata *)(ulpmc + 1);
978 		ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
979 		ulpsc->len = htobe32(chunk);
980 
981 		ppod = (struct pagepod *)(ulpsc + 1);
982 		for (j = 0; j < n; i++, j++, ppod++) {
983 			ppod->vld_tid_pgsz_tag_color = htobe64(F_PPOD_VALID |
984 			    V_PPOD_TID(toep->tid) | ps->tag);
985 			ppod->len_offset = htobe64(V_PPOD_LEN(ps->len) |
986 			    V_PPOD_OFST(ps->offset));
987 			ppod->rsvd = 0;
988 			idx = i * PPOD_PAGES * (ddp_pgsz / PAGE_SIZE);
989 			for (k = 0; k < nitems(ppod->addr); k++) {
990 				if (idx < ps->npages) {
991 					ppod->addr[k] =
992 					    htobe64(ps->pages[idx]->phys_addr);
993 					idx += ddp_pgsz / PAGE_SIZE;
994 				} else
995 					ppod->addr[k] = 0;
996 #if 0
997 				CTR5(KTR_CXGBE,
998 				    "%s: tid %d ppod[%d]->addr[%d] = %p",
999 				    __func__, toep->tid, i, k,
1000 				    htobe64(ppod->addr[k]));
1001 #endif
1002 			}
1003 
1004 		}
1005 
1006 		t4_wrq_tx(sc, wr);
1007 	}
1008 	ps->flags |= PS_PPODS_WRITTEN;
1009 
1010 	return (0);
1011 }
1012 
1013 static void
1014 wire_pageset(struct pageset *ps)
1015 {
1016 	vm_page_t p;
1017 	int i;
1018 
1019 	KASSERT(!(ps->flags & PS_WIRED), ("pageset already wired"));
1020 
1021 	for (i = 0; i < ps->npages; i++) {
1022 		p = ps->pages[i];
1023 		vm_page_lock(p);
1024 		vm_page_wire(p);
1025 		vm_page_unhold(p);
1026 		vm_page_unlock(p);
1027 	}
1028 	ps->flags |= PS_WIRED;
1029 }
1030 
1031 /*
1032  * Prepare a pageset for DDP.  This wires the pageset and sets up page
1033  * pods.
1034  */
1035 static int
1036 prep_pageset(struct adapter *sc, struct toepcb *toep, struct pageset *ps)
1037 {
1038 	struct tom_data *td = sc->tom_softc;
1039 
1040 	if (!(ps->flags & PS_WIRED))
1041 		wire_pageset(ps);
1042 	if (ps->nppods == 0 && !alloc_page_pods(td, ps)) {
1043 		return (0);
1044 	}
1045 	if (!(ps->flags & PS_PPODS_WRITTEN) &&
1046 	    write_page_pods(sc, toep, ps) != 0) {
1047 		return (0);
1048 	}
1049 
1050 	return (1);
1051 }
1052 
1053 void
1054 t4_init_ddp(struct adapter *sc, struct tom_data *td)
1055 {
1056 
1057 	td->ppod_start = sc->vres.ddp.start;
1058 	td->ppod_arena = vmem_create("DDP page pods", sc->vres.ddp.start,
1059 	    sc->vres.ddp.size, 1, 32, M_FIRSTFIT | M_NOWAIT);
1060 
1061 	t4_register_cpl_handler(sc, CPL_RX_DATA_DDP, do_rx_data_ddp);
1062 	t4_register_cpl_handler(sc, CPL_RX_DDP_COMPLETE, do_rx_ddp_complete);
1063 }
1064 
1065 void
1066 t4_uninit_ddp(struct adapter *sc __unused, struct tom_data *td)
1067 {
1068 
1069 	if (td->ppod_arena != NULL) {
1070 		vmem_destroy(td->ppod_arena);
1071 		td->ppod_arena = NULL;
1072 	}
1073 }
1074 
1075 static int
1076 pscmp(struct pageset *ps, struct vmspace *vm, vm_offset_t start, int npages,
1077     int pgoff, int len)
1078 {
1079 
1080 	if (ps->npages != npages || ps->offset != pgoff || ps->len != len)
1081 		return (1);
1082 
1083 	return (ps->vm != vm || ps->vm_timestamp != vm->vm_map.timestamp);
1084 }
1085 
1086 static int
1087 hold_aio(struct toepcb *toep, struct kaiocb *job, struct pageset **pps)
1088 {
1089 	struct vmspace *vm;
1090 	vm_map_t map;
1091 	vm_offset_t start, end, pgoff;
1092 	struct pageset *ps;
1093 	int n;
1094 
1095 	DDP_ASSERT_LOCKED(toep);
1096 
1097 	/*
1098 	 * The AIO subsystem will cancel and drain all requests before
1099 	 * permitting a process to exit or exec, so p_vmspace should
1100 	 * be stable here.
1101 	 */
1102 	vm = job->userproc->p_vmspace;
1103 	map = &vm->vm_map;
1104 	start = (uintptr_t)job->uaiocb.aio_buf;
1105 	pgoff = start & PAGE_MASK;
1106 	end = round_page(start + job->uaiocb.aio_nbytes);
1107 	start = trunc_page(start);
1108 
1109 	if (end - start > MAX_DDP_BUFFER_SIZE) {
1110 		/*
1111 		 * Truncate the request to a short read.
1112 		 * Alternatively, we could DDP in chunks to the larger
1113 		 * buffer, but that would be quite a bit more work.
1114 		 *
1115 		 * When truncating, round the request down to avoid
1116 		 * crossing a cache line on the final transaction.
1117 		 */
1118 		end = rounddown2(start + MAX_DDP_BUFFER_SIZE, CACHE_LINE_SIZE);
1119 #ifdef VERBOSE_TRACES
1120 		CTR4(KTR_CXGBE, "%s: tid %d, truncating size from %lu to %lu",
1121 		    __func__, toep->tid, (unsigned long)job->uaiocb.aio_nbytes,
1122 		    (unsigned long)(end - (start + pgoff)));
1123 		job->uaiocb.aio_nbytes = end - (start + pgoff);
1124 #endif
1125 		end = round_page(end);
1126 	}
1127 
1128 	n = atop(end - start);
1129 
1130 	/*
1131 	 * Try to reuse a cached pageset.
1132 	 */
1133 	TAILQ_FOREACH(ps, &toep->ddp_cached_pagesets, link) {
1134 		if (pscmp(ps, vm, start, n, pgoff,
1135 		    job->uaiocb.aio_nbytes) == 0) {
1136 			TAILQ_REMOVE(&toep->ddp_cached_pagesets, ps, link);
1137 			toep->ddp_cached_count--;
1138 			*pps = ps;
1139 			return (0);
1140 		}
1141 	}
1142 
1143 	/*
1144 	 * If there are too many cached pagesets to create a new one,
1145 	 * free a pageset before creating a new one.
1146 	 */
1147 	KASSERT(toep->ddp_active_count + toep->ddp_cached_count <=
1148 	    nitems(toep->db), ("%s: too many wired pagesets", __func__));
1149 	if (toep->ddp_active_count + toep->ddp_cached_count ==
1150 	    nitems(toep->db)) {
1151 		KASSERT(toep->ddp_cached_count > 0,
1152 		    ("no cached pageset to free"));
1153 		ps = TAILQ_LAST(&toep->ddp_cached_pagesets, pagesetq);
1154 		TAILQ_REMOVE(&toep->ddp_cached_pagesets, ps, link);
1155 		toep->ddp_cached_count--;
1156 		free_pageset(toep->td, ps);
1157 	}
1158 	DDP_UNLOCK(toep);
1159 
1160 	/* Create a new pageset. */
1161 	ps = malloc(sizeof(*ps) + n * sizeof(vm_page_t), M_CXGBE, M_WAITOK |
1162 	    M_ZERO);
1163 	ps->pages = (vm_page_t *)(ps + 1);
1164 	ps->vm_timestamp = map->timestamp;
1165 	ps->npages = vm_fault_quick_hold_pages(map, start, end - start,
1166 	    VM_PROT_WRITE, ps->pages, n);
1167 
1168 	DDP_LOCK(toep);
1169 	if (ps->npages < 0) {
1170 		free(ps, M_CXGBE);
1171 		return (EFAULT);
1172 	}
1173 
1174 	KASSERT(ps->npages == n, ("hold_aio: page count mismatch: %d vs %d",
1175 	    ps->npages, n));
1176 
1177 	ps->offset = pgoff;
1178 	ps->len = job->uaiocb.aio_nbytes;
1179 	atomic_add_int(&vm->vm_refcnt, 1);
1180 	ps->vm = vm;
1181 
1182 	CTR5(KTR_CXGBE, "%s: tid %d, new pageset %p for job %p, npages %d",
1183 	    __func__, toep->tid, ps, job, ps->npages);
1184 	*pps = ps;
1185 	return (0);
1186 }
1187 
1188 static void
1189 ddp_complete_all(struct toepcb *toep, int error)
1190 {
1191 	struct kaiocb *job;
1192 
1193 	DDP_ASSERT_LOCKED(toep);
1194 	while (!TAILQ_EMPTY(&toep->ddp_aiojobq)) {
1195 		job = TAILQ_FIRST(&toep->ddp_aiojobq);
1196 		TAILQ_REMOVE(&toep->ddp_aiojobq, job, list);
1197 		toep->ddp_waiting_count--;
1198 		if (aio_clear_cancel_function(job))
1199 			ddp_complete_one(job, error);
1200 	}
1201 }
1202 
1203 static void
1204 aio_ddp_cancel_one(struct kaiocb *job)
1205 {
1206 	long copied;
1207 
1208 	/*
1209 	 * If this job had copied data out of the socket buffer before
1210 	 * it was cancelled, report it as a short read rather than an
1211 	 * error.
1212 	 */
1213 	copied = job->uaiocb._aiocb_private.status;
1214 	if (copied != 0)
1215 		aio_complete(job, copied, 0);
1216 	else
1217 		aio_cancel(job);
1218 }
1219 
1220 /*
1221  * Called when the main loop wants to requeue a job to retry it later.
1222  * Deals with the race of the job being cancelled while it was being
1223  * examined.
1224  */
1225 static void
1226 aio_ddp_requeue_one(struct toepcb *toep, struct kaiocb *job)
1227 {
1228 
1229 	DDP_ASSERT_LOCKED(toep);
1230 	if (!(toep->ddp_flags & DDP_DEAD) &&
1231 	    aio_set_cancel_function(job, t4_aio_cancel_queued)) {
1232 		TAILQ_INSERT_HEAD(&toep->ddp_aiojobq, job, list);
1233 		toep->ddp_waiting_count++;
1234 	} else
1235 		aio_ddp_cancel_one(job);
1236 }
1237 
1238 static void
1239 aio_ddp_requeue(struct toepcb *toep)
1240 {
1241 	struct adapter *sc = td_adapter(toep->td);
1242 	struct socket *so;
1243 	struct sockbuf *sb;
1244 	struct inpcb *inp;
1245 	struct kaiocb *job;
1246 	struct ddp_buffer *db;
1247 	size_t copied, offset, resid;
1248 	struct pageset *ps;
1249 	struct mbuf *m;
1250 	uint64_t ddp_flags, ddp_flags_mask;
1251 	struct wrqe *wr;
1252 	int buf_flag, db_idx, error;
1253 
1254 	DDP_ASSERT_LOCKED(toep);
1255 
1256 restart:
1257 	if (toep->ddp_flags & DDP_DEAD) {
1258 		MPASS(toep->ddp_waiting_count == 0);
1259 		MPASS(toep->ddp_active_count == 0);
1260 		return;
1261 	}
1262 
1263 	if (toep->ddp_waiting_count == 0 ||
1264 	    toep->ddp_active_count == nitems(toep->db)) {
1265 		return;
1266 	}
1267 
1268 	job = TAILQ_FIRST(&toep->ddp_aiojobq);
1269 	so = job->fd_file->f_data;
1270 	sb = &so->so_rcv;
1271 	SOCKBUF_LOCK(sb);
1272 
1273 	/* We will never get anything unless we are or were connected. */
1274 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1275 		SOCKBUF_UNLOCK(sb);
1276 		ddp_complete_all(toep, ENOTCONN);
1277 		return;
1278 	}
1279 
1280 	KASSERT(toep->ddp_active_count == 0 || sbavail(sb) == 0,
1281 	    ("%s: pending sockbuf data and DDP is active", __func__));
1282 
1283 	/* Abort if socket has reported problems. */
1284 	/* XXX: Wait for any queued DDP's to finish and/or flush them? */
1285 	if (so->so_error && sbavail(sb) == 0) {
1286 		toep->ddp_waiting_count--;
1287 		TAILQ_REMOVE(&toep->ddp_aiojobq, job, list);
1288 		if (!aio_clear_cancel_function(job)) {
1289 			SOCKBUF_UNLOCK(sb);
1290 			goto restart;
1291 		}
1292 
1293 		/*
1294 		 * If this job has previously copied some data, report
1295 		 * a short read and leave the error to be reported by
1296 		 * a future request.
1297 		 */
1298 		copied = job->uaiocb._aiocb_private.status;
1299 		if (copied != 0) {
1300 			SOCKBUF_UNLOCK(sb);
1301 			aio_complete(job, copied, 0);
1302 			goto restart;
1303 		}
1304 		error = so->so_error;
1305 		so->so_error = 0;
1306 		SOCKBUF_UNLOCK(sb);
1307 		aio_complete(job, -1, error);
1308 		goto restart;
1309 	}
1310 
1311 	/*
1312 	 * Door is closed.  If there is pending data in the socket buffer,
1313 	 * deliver it.  If there are pending DDP requests, wait for those
1314 	 * to complete.  Once they have completed, return EOF reads.
1315 	 */
1316 	if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1317 		SOCKBUF_UNLOCK(sb);
1318 		if (toep->ddp_active_count != 0)
1319 			return;
1320 		ddp_complete_all(toep, 0);
1321 		return;
1322 	}
1323 
1324 	/*
1325 	 * If DDP is not enabled and there is no pending socket buffer
1326 	 * data, try to enable DDP.
1327 	 */
1328 	if (sbavail(sb) == 0 && (toep->ddp_flags & DDP_ON) == 0) {
1329 		SOCKBUF_UNLOCK(sb);
1330 
1331 		/*
1332 		 * Wait for the card to ACK that DDP is enabled before
1333 		 * queueing any buffers.  Currently this waits for an
1334 		 * indicate to arrive.  This could use a TCB_SET_FIELD_RPL
1335 		 * message to know that DDP was enabled instead of waiting
1336 		 * for the indicate which would avoid copying the indicate
1337 		 * if no data is pending.
1338 		 *
1339 		 * XXX: Might want to limit the indicate size to the size
1340 		 * of the first queued request.
1341 		 */
1342 		if ((toep->ddp_flags & DDP_SC_REQ) == 0)
1343 			enable_ddp(sc, toep);
1344 		return;
1345 	}
1346 	SOCKBUF_UNLOCK(sb);
1347 
1348 	/*
1349 	 * If another thread is queueing a buffer for DDP, let it
1350 	 * drain any work and return.
1351 	 */
1352 	if (toep->ddp_queueing != NULL)
1353 		return;
1354 
1355 	/* Take the next job to prep it for DDP. */
1356 	toep->ddp_waiting_count--;
1357 	TAILQ_REMOVE(&toep->ddp_aiojobq, job, list);
1358 	if (!aio_clear_cancel_function(job))
1359 		goto restart;
1360 	toep->ddp_queueing = job;
1361 
1362 	/* NB: This drops DDP_LOCK while it holds the backing VM pages. */
1363 	error = hold_aio(toep, job, &ps);
1364 	if (error != 0) {
1365 		ddp_complete_one(job, error);
1366 		toep->ddp_queueing = NULL;
1367 		goto restart;
1368 	}
1369 
1370 	SOCKBUF_LOCK(sb);
1371 	if (so->so_error && sbavail(sb) == 0) {
1372 		copied = job->uaiocb._aiocb_private.status;
1373 		if (copied != 0) {
1374 			SOCKBUF_UNLOCK(sb);
1375 			recycle_pageset(toep, ps);
1376 			aio_complete(job, copied, 0);
1377 			toep->ddp_queueing = NULL;
1378 			goto restart;
1379 		}
1380 
1381 		error = so->so_error;
1382 		so->so_error = 0;
1383 		SOCKBUF_UNLOCK(sb);
1384 		recycle_pageset(toep, ps);
1385 		aio_complete(job, -1, error);
1386 		toep->ddp_queueing = NULL;
1387 		goto restart;
1388 	}
1389 
1390 	if (sb->sb_state & SBS_CANTRCVMORE && sbavail(sb) == 0) {
1391 		SOCKBUF_UNLOCK(sb);
1392 		recycle_pageset(toep, ps);
1393 		if (toep->ddp_active_count != 0) {
1394 			/*
1395 			 * The door is closed, but there are still pending
1396 			 * DDP buffers.  Requeue.  These jobs will all be
1397 			 * completed once those buffers drain.
1398 			 */
1399 			aio_ddp_requeue_one(toep, job);
1400 			toep->ddp_queueing = NULL;
1401 			return;
1402 		}
1403 		ddp_complete_one(job, 0);
1404 		ddp_complete_all(toep, 0);
1405 		toep->ddp_queueing = NULL;
1406 		return;
1407 	}
1408 
1409 sbcopy:
1410 	/*
1411 	 * If the toep is dead, there shouldn't be any data in the socket
1412 	 * buffer, so the above case should have handled this.
1413 	 */
1414 	MPASS(!(toep->ddp_flags & DDP_DEAD));
1415 
1416 	/*
1417 	 * If there is pending data in the socket buffer (either
1418 	 * from before the requests were queued or a DDP indicate),
1419 	 * copy those mbufs out directly.
1420 	 */
1421 	copied = 0;
1422 	offset = ps->offset + job->uaiocb._aiocb_private.status;
1423 	MPASS(job->uaiocb._aiocb_private.status <= job->uaiocb.aio_nbytes);
1424 	resid = job->uaiocb.aio_nbytes - job->uaiocb._aiocb_private.status;
1425 	m = sb->sb_mb;
1426 	KASSERT(m == NULL || toep->ddp_active_count == 0,
1427 	    ("%s: sockbuf data with active DDP", __func__));
1428 	while (m != NULL && resid > 0) {
1429 		struct iovec iov[1];
1430 		struct uio uio;
1431 		int error;
1432 
1433 		iov[0].iov_base = mtod(m, void *);
1434 		iov[0].iov_len = m->m_len;
1435 		if (iov[0].iov_len > resid)
1436 			iov[0].iov_len = resid;
1437 		uio.uio_iov = iov;
1438 		uio.uio_iovcnt = 1;
1439 		uio.uio_offset = 0;
1440 		uio.uio_resid = iov[0].iov_len;
1441 		uio.uio_segflg = UIO_SYSSPACE;
1442 		uio.uio_rw = UIO_WRITE;
1443 		error = uiomove_fromphys(ps->pages, offset + copied,
1444 		    uio.uio_resid, &uio);
1445 		MPASS(error == 0 && uio.uio_resid == 0);
1446 		copied += uio.uio_offset;
1447 		resid -= uio.uio_offset;
1448 		m = m->m_next;
1449 	}
1450 	if (copied != 0) {
1451 		sbdrop_locked(sb, copied);
1452 		job->uaiocb._aiocb_private.status += copied;
1453 		copied = job->uaiocb._aiocb_private.status;
1454 		inp = sotoinpcb(so);
1455 		if (!INP_TRY_WLOCK(inp)) {
1456 			/*
1457 			 * The reference on the socket file descriptor in
1458 			 * the AIO job should keep 'sb' and 'inp' stable.
1459 			 * Our caller has a reference on the 'toep' that
1460 			 * keeps it stable.
1461 			 */
1462 			SOCKBUF_UNLOCK(sb);
1463 			DDP_UNLOCK(toep);
1464 			INP_WLOCK(inp);
1465 			DDP_LOCK(toep);
1466 			SOCKBUF_LOCK(sb);
1467 
1468 			/*
1469 			 * If the socket has been closed, we should detect
1470 			 * that and complete this request if needed on
1471 			 * the next trip around the loop.
1472 			 */
1473 		}
1474 		t4_rcvd_locked(&toep->td->tod, intotcpcb(inp));
1475 		INP_WUNLOCK(inp);
1476 		if (resid == 0 || toep->ddp_flags & DDP_DEAD) {
1477 			/*
1478 			 * We filled the entire buffer with socket
1479 			 * data, DDP is not being used, or the socket
1480 			 * is being shut down, so complete the
1481 			 * request.
1482 			 */
1483 			SOCKBUF_UNLOCK(sb);
1484 			recycle_pageset(toep, ps);
1485 			aio_complete(job, copied, 0);
1486 			toep->ddp_queueing = NULL;
1487 			goto restart;
1488 		}
1489 
1490 		/*
1491 		 * If DDP is not enabled, requeue this request and restart.
1492 		 * This will either enable DDP or wait for more data to
1493 		 * arrive on the socket buffer.
1494 		 */
1495 		if ((toep->ddp_flags & (DDP_ON | DDP_SC_REQ)) != DDP_ON) {
1496 			SOCKBUF_UNLOCK(sb);
1497 			recycle_pageset(toep, ps);
1498 			aio_ddp_requeue_one(toep, job);
1499 			toep->ddp_queueing = NULL;
1500 			goto restart;
1501 		}
1502 
1503 		/*
1504 		 * An indicate might have arrived and been added to
1505 		 * the socket buffer while it was unlocked after the
1506 		 * copy to lock the INP.  If so, restart the copy.
1507 		 */
1508 		if (sbavail(sb) != 0)
1509 			goto sbcopy;
1510 	}
1511 	SOCKBUF_UNLOCK(sb);
1512 
1513 	if (prep_pageset(sc, toep, ps) == 0) {
1514 		recycle_pageset(toep, ps);
1515 		aio_ddp_requeue_one(toep, job);
1516 		toep->ddp_queueing = NULL;
1517 
1518 		/*
1519 		 * XXX: Need to retry this later.  Mostly need a trigger
1520 		 * when page pods are freed up.
1521 		 */
1522 		printf("%s: prep_pageset failed\n", __func__);
1523 		return;
1524 	}
1525 
1526 	/* Determine which DDP buffer to use. */
1527 	if (toep->db[0].job == NULL) {
1528 		db_idx = 0;
1529 	} else {
1530 		MPASS(toep->db[1].job == NULL);
1531 		db_idx = 1;
1532 	}
1533 
1534 	ddp_flags = 0;
1535 	ddp_flags_mask = 0;
1536 	if (db_idx == 0) {
1537 		ddp_flags |= V_TF_DDP_BUF0_VALID(1);
1538 		if (so->so_state & SS_NBIO)
1539 			ddp_flags |= V_TF_DDP_BUF0_FLUSH(1);
1540 		ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE0(1) |
1541 		    V_TF_DDP_PUSH_DISABLE_0(1) | V_TF_DDP_PSHF_ENABLE_0(1) |
1542 		    V_TF_DDP_BUF0_FLUSH(1) | V_TF_DDP_BUF0_VALID(1);
1543 		buf_flag = DDP_BUF0_ACTIVE;
1544 	} else {
1545 		ddp_flags |= V_TF_DDP_BUF1_VALID(1);
1546 		if (so->so_state & SS_NBIO)
1547 			ddp_flags |= V_TF_DDP_BUF1_FLUSH(1);
1548 		ddp_flags_mask |= V_TF_DDP_PSH_NO_INVALIDATE1(1) |
1549 		    V_TF_DDP_PUSH_DISABLE_1(1) | V_TF_DDP_PSHF_ENABLE_1(1) |
1550 		    V_TF_DDP_BUF1_FLUSH(1) | V_TF_DDP_BUF1_VALID(1);
1551 		buf_flag = DDP_BUF1_ACTIVE;
1552 	}
1553 	MPASS((toep->ddp_flags & buf_flag) == 0);
1554 	if ((toep->ddp_flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)) == 0) {
1555 		MPASS(db_idx == 0);
1556 		MPASS(toep->ddp_active_id == -1);
1557 		MPASS(toep->ddp_active_count == 0);
1558 		ddp_flags_mask |= V_TF_DDP_ACTIVE_BUF(1);
1559 	}
1560 
1561 	/*
1562 	 * The TID for this connection should still be valid.  If DDP_DEAD
1563 	 * is set, SBS_CANTRCVMORE should be set, so we shouldn't be
1564 	 * this far anyway.  Even if the socket is closing on the other
1565 	 * end, the AIO job holds a reference on this end of the socket
1566 	 * which will keep it open and keep the TCP PCB attached until
1567 	 * after the job is completed.
1568 	 */
1569 	wr = mk_update_tcb_for_ddp(sc, toep, db_idx, ps,
1570 	    job->uaiocb._aiocb_private.status, ddp_flags, ddp_flags_mask);
1571 	if (wr == NULL) {
1572 		recycle_pageset(toep, ps);
1573 		aio_ddp_requeue_one(toep, job);
1574 		toep->ddp_queueing = NULL;
1575 
1576 		/*
1577 		 * XXX: Need a way to kick a retry here.
1578 		 *
1579 		 * XXX: We know the fixed size needed and could
1580 		 * preallocate this using a blocking request at the
1581 		 * start of the task to avoid having to handle this
1582 		 * edge case.
1583 		 */
1584 		printf("%s: mk_update_tcb_for_ddp failed\n", __func__);
1585 		return;
1586 	}
1587 
1588 	if (!aio_set_cancel_function(job, t4_aio_cancel_active)) {
1589 		free_wrqe(wr);
1590 		recycle_pageset(toep, ps);
1591 		aio_ddp_cancel_one(job);
1592 		toep->ddp_queueing = NULL;
1593 		goto restart;
1594 	}
1595 
1596 #ifdef VERBOSE_TRACES
1597 	CTR5(KTR_CXGBE, "%s: scheduling %p for DDP[%d] (flags %#lx/%#lx)",
1598 	    __func__, job, db_idx, ddp_flags, ddp_flags_mask);
1599 #endif
1600 	/* Give the chip the go-ahead. */
1601 	t4_wrq_tx(sc, wr);
1602 	db = &toep->db[db_idx];
1603 	db->cancel_pending = 0;
1604 	db->job = job;
1605 	db->ps = ps;
1606 	toep->ddp_queueing = NULL;
1607 	toep->ddp_flags |= buf_flag;
1608 	toep->ddp_active_count++;
1609 	if (toep->ddp_active_count == 1) {
1610 		MPASS(toep->ddp_active_id == -1);
1611 		toep->ddp_active_id = db_idx;
1612 		CTR2(KTR_CXGBE, "%s: ddp_active_id = %d", __func__,
1613 		    toep->ddp_active_id);
1614 	}
1615 	goto restart;
1616 }
1617 
1618 void
1619 ddp_queue_toep(struct toepcb *toep)
1620 {
1621 
1622 	DDP_ASSERT_LOCKED(toep);
1623 	if (toep->ddp_flags & DDP_TASK_ACTIVE)
1624 		return;
1625 	toep->ddp_flags |= DDP_TASK_ACTIVE;
1626 	hold_toepcb(toep);
1627 	soaio_enqueue(&toep->ddp_requeue_task);
1628 }
1629 
1630 static void
1631 aio_ddp_requeue_task(void *context, int pending)
1632 {
1633 	struct toepcb *toep = context;
1634 
1635 	DDP_LOCK(toep);
1636 	aio_ddp_requeue(toep);
1637 	toep->ddp_flags &= ~DDP_TASK_ACTIVE;
1638 	DDP_UNLOCK(toep);
1639 
1640 	free_toepcb(toep);
1641 }
1642 
1643 static void
1644 t4_aio_cancel_active(struct kaiocb *job)
1645 {
1646 	struct socket *so = job->fd_file->f_data;
1647 	struct tcpcb *tp = so_sototcpcb(so);
1648 	struct toepcb *toep = tp->t_toe;
1649 	struct adapter *sc = td_adapter(toep->td);
1650 	uint64_t valid_flag;
1651 	int i;
1652 
1653 	DDP_LOCK(toep);
1654 	if (aio_cancel_cleared(job)) {
1655 		DDP_UNLOCK(toep);
1656 		aio_ddp_cancel_one(job);
1657 		return;
1658 	}
1659 
1660 	for (i = 0; i < nitems(toep->db); i++) {
1661 		if (toep->db[i].job == job) {
1662 			/* Should only ever get one cancel request for a job. */
1663 			MPASS(toep->db[i].cancel_pending == 0);
1664 
1665 			/*
1666 			 * Invalidate this buffer.  It will be
1667 			 * cancelled or partially completed once the
1668 			 * card ACKs the invalidate.
1669 			 */
1670 			valid_flag = i == 0 ? V_TF_DDP_BUF0_VALID(1) :
1671 			    V_TF_DDP_BUF1_VALID(1);
1672 			t4_set_tcb_field_rpl(sc, toep, 1, W_TCB_RX_DDP_FLAGS,
1673 			    valid_flag, 0, i + DDP_BUF0_INVALIDATED);
1674 			toep->db[i].cancel_pending = 1;
1675 			CTR2(KTR_CXGBE, "%s: request %p marked pending",
1676 			    __func__, job);
1677 			break;
1678 		}
1679 	}
1680 	DDP_UNLOCK(toep);
1681 }
1682 
1683 static void
1684 t4_aio_cancel_queued(struct kaiocb *job)
1685 {
1686 	struct socket *so = job->fd_file->f_data;
1687 	struct tcpcb *tp = so_sototcpcb(so);
1688 	struct toepcb *toep = tp->t_toe;
1689 
1690 	DDP_LOCK(toep);
1691 	if (!aio_cancel_cleared(job)) {
1692 		TAILQ_REMOVE(&toep->ddp_aiojobq, job, list);
1693 		toep->ddp_waiting_count--;
1694 		if (toep->ddp_waiting_count == 0)
1695 			ddp_queue_toep(toep);
1696 	}
1697 	CTR2(KTR_CXGBE, "%s: request %p cancelled", __func__, job);
1698 	DDP_UNLOCK(toep);
1699 
1700 	aio_ddp_cancel_one(job);
1701 }
1702 
1703 int
1704 t4_aio_queue_ddp(struct socket *so, struct kaiocb *job)
1705 {
1706 	struct tcpcb *tp = so_sototcpcb(so);
1707 	struct toepcb *toep = tp->t_toe;
1708 
1709 
1710 	/* Ignore writes. */
1711 	if (job->uaiocb.aio_lio_opcode != LIO_READ)
1712 		return (EOPNOTSUPP);
1713 
1714 	DDP_LOCK(toep);
1715 
1716 	/*
1717 	 * XXX: Think about possibly returning errors for ENOTCONN,
1718 	 * etc.  Perhaps the caller would only queue the request
1719 	 * if it failed with EOPNOTSUPP?
1720 	 */
1721 
1722 #ifdef VERBOSE_TRACES
1723 	CTR2(KTR_CXGBE, "%s: queueing %p", __func__, job);
1724 #endif
1725 	if (!aio_set_cancel_function(job, t4_aio_cancel_queued))
1726 		panic("new job was cancelled");
1727 	TAILQ_INSERT_TAIL(&toep->ddp_aiojobq, job, list);
1728 	job->uaiocb._aiocb_private.status = 0;
1729 	toep->ddp_waiting_count++;
1730 	toep->ddp_flags |= DDP_OK;
1731 
1732 	/*
1733 	 * Try to handle this request synchronously.  If this has
1734 	 * to block because the task is running, it will just bail
1735 	 * and let the task handle it instead.
1736 	 */
1737 	aio_ddp_requeue(toep);
1738 	DDP_UNLOCK(toep);
1739 	return (0);
1740 }
1741 
1742 int
1743 t4_ddp_mod_load(void)
1744 {
1745 
1746 	TAILQ_INIT(&ddp_orphan_pagesets);
1747 	mtx_init(&ddp_orphan_pagesets_lock, "ddp orphans", NULL, MTX_DEF);
1748 	TASK_INIT(&ddp_orphan_task, 0, ddp_free_orphan_pagesets, NULL);
1749 	return (0);
1750 }
1751 
1752 void
1753 t4_ddp_mod_unload(void)
1754 {
1755 
1756 	taskqueue_drain(taskqueue_thread, &ddp_orphan_task);
1757 	MPASS(TAILQ_EMPTY(&ddp_orphan_pagesets));
1758 	mtx_destroy(&ddp_orphan_pagesets_lock);
1759 }
1760 #endif
1761