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