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