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