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