1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * Copyright (c) 2015 Chelsio Communications, Inc. 4 * All rights reserved. 5 * 6 * This software was developed by Edward Tomasz Napierala under sponsorship 7 * from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * cxgbei implementation of iSCSI Common Layer kobj(9) interface. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_inet.h" 40 #include "opt_inet6.h" 41 42 #ifdef TCP_OFFLOAD 43 #include <sys/param.h> 44 #include <sys/capsicum.h> 45 #include <sys/condvar.h> 46 #include <sys/conf.h> 47 #include <sys/file.h> 48 #include <sys/kernel.h> 49 #include <sys/kthread.h> 50 #include <sys/ktr.h> 51 #include <sys/lock.h> 52 #include <sys/mbuf.h> 53 #include <sys/mutex.h> 54 #include <sys/module.h> 55 #include <sys/protosw.h> 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 #include <sys/sysctl.h> 59 #include <sys/systm.h> 60 #include <sys/sx.h> 61 #include <sys/uio.h> 62 #include <machine/bus.h> 63 #include <vm/vm.h> 64 #include <vm/pmap.h> 65 #include <netinet/in.h> 66 #include <netinet/in_pcb.h> 67 #include <netinet/tcp.h> 68 #include <netinet/tcp_var.h> 69 #include <netinet/toecore.h> 70 71 #include <dev/iscsi/icl.h> 72 #include <dev/iscsi/iscsi_proto.h> 73 #include <icl_conn_if.h> 74 75 #include <cam/scsi/scsi_all.h> 76 #include <cam/scsi/scsi_da.h> 77 #include <cam/ctl/ctl_io.h> 78 #include <cam/ctl/ctl.h> 79 #include <cam/ctl/ctl_backend.h> 80 #include <cam/ctl/ctl_error.h> 81 #include <cam/ctl/ctl_frontend.h> 82 #include <cam/ctl/ctl_debug.h> 83 #include <cam/ctl/ctl_ha.h> 84 #include <cam/ctl/ctl_ioctl.h> 85 86 #include <cam/cam.h> 87 #include <cam/cam_ccb.h> 88 #include <cam/cam_xpt.h> 89 #include <cam/cam_debug.h> 90 #include <cam/cam_sim.h> 91 #include <cam/cam_xpt_sim.h> 92 #include <cam/cam_xpt_periph.h> 93 #include <cam/cam_periph.h> 94 #include <cam/cam_compat.h> 95 #include <cam/scsi/scsi_message.h> 96 97 #include "common/common.h" 98 #include "common/t4_regs.h" 99 #include "common/t4_tcb.h" 100 #include "tom/t4_tom.h" 101 #include "cxgbei.h" 102 103 /* 104 * Use the page pod tag for the TT hash. 105 */ 106 #define TT_HASH(icc, tt) (G_PPOD_TAG(tt) & (icc)->cmp_hash_mask) 107 108 struct cxgbei_ddp_state { 109 struct ppod_reservation prsv; 110 struct cxgbei_cmp cmp; 111 }; 112 113 static MALLOC_DEFINE(M_CXGBEI, "cxgbei", "cxgbei(4)"); 114 115 SYSCTL_NODE(_kern_icl, OID_AUTO, cxgbei, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 116 "Chelsio iSCSI offload"); 117 static int first_burst_length = 8192; 118 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, first_burst_length, CTLFLAG_RWTUN, 119 &first_burst_length, 0, "First burst length"); 120 static int max_burst_length = 2 * 1024 * 1024; 121 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, max_burst_length, CTLFLAG_RWTUN, 122 &max_burst_length, 0, "Maximum burst length"); 123 static int sendspace = 1048576; 124 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, sendspace, CTLFLAG_RWTUN, 125 &sendspace, 0, "Default send socket buffer size"); 126 static int recvspace = 1048576; 127 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, recvspace, CTLFLAG_RWTUN, 128 &recvspace, 0, "Default receive socket buffer size"); 129 130 static volatile u_int icl_cxgbei_ncons; 131 132 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock) 133 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock) 134 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED) 135 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED) 136 137 static icl_conn_new_pdu_t icl_cxgbei_conn_new_pdu; 138 static icl_conn_pdu_data_segment_length_t 139 icl_cxgbei_conn_pdu_data_segment_length; 140 static icl_conn_pdu_append_data_t icl_cxgbei_conn_pdu_append_data; 141 static icl_conn_pdu_get_data_t icl_cxgbei_conn_pdu_get_data; 142 static icl_conn_pdu_queue_t icl_cxgbei_conn_pdu_queue; 143 static icl_conn_pdu_queue_cb_t icl_cxgbei_conn_pdu_queue_cb; 144 static icl_conn_handoff_t icl_cxgbei_conn_handoff; 145 static icl_conn_free_t icl_cxgbei_conn_free; 146 static icl_conn_close_t icl_cxgbei_conn_close; 147 static icl_conn_task_setup_t icl_cxgbei_conn_task_setup; 148 static icl_conn_task_done_t icl_cxgbei_conn_task_done; 149 static icl_conn_transfer_setup_t icl_cxgbei_conn_transfer_setup; 150 static icl_conn_transfer_done_t icl_cxgbei_conn_transfer_done; 151 152 static kobj_method_t icl_cxgbei_methods[] = { 153 KOBJMETHOD(icl_conn_new_pdu, icl_cxgbei_conn_new_pdu), 154 KOBJMETHOD(icl_conn_pdu_free, icl_cxgbei_conn_pdu_free), 155 KOBJMETHOD(icl_conn_pdu_data_segment_length, 156 icl_cxgbei_conn_pdu_data_segment_length), 157 KOBJMETHOD(icl_conn_pdu_append_data, icl_cxgbei_conn_pdu_append_data), 158 KOBJMETHOD(icl_conn_pdu_get_data, icl_cxgbei_conn_pdu_get_data), 159 KOBJMETHOD(icl_conn_pdu_queue, icl_cxgbei_conn_pdu_queue), 160 KOBJMETHOD(icl_conn_pdu_queue_cb, icl_cxgbei_conn_pdu_queue_cb), 161 KOBJMETHOD(icl_conn_handoff, icl_cxgbei_conn_handoff), 162 KOBJMETHOD(icl_conn_free, icl_cxgbei_conn_free), 163 KOBJMETHOD(icl_conn_close, icl_cxgbei_conn_close), 164 KOBJMETHOD(icl_conn_task_setup, icl_cxgbei_conn_task_setup), 165 KOBJMETHOD(icl_conn_task_done, icl_cxgbei_conn_task_done), 166 KOBJMETHOD(icl_conn_transfer_setup, icl_cxgbei_conn_transfer_setup), 167 KOBJMETHOD(icl_conn_transfer_done, icl_cxgbei_conn_transfer_done), 168 { 0, 0 } 169 }; 170 171 DEFINE_CLASS(icl_cxgbei, icl_cxgbei_methods, sizeof(struct icl_cxgbei_conn)); 172 173 void 174 icl_cxgbei_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip) 175 { 176 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 177 178 KASSERT(icp->ref_cnt != 0, ("freeing deleted PDU")); 179 MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE); 180 MPASS(ic == ip->ip_conn); 181 182 m_freem(ip->ip_ahs_mbuf); 183 m_freem(ip->ip_data_mbuf); 184 m_freem(ip->ip_bhs_mbuf); 185 186 KASSERT(ic != NULL || icp->ref_cnt == 1, 187 ("orphaned PDU has oustanding references")); 188 189 if (atomic_fetchadd_int(&icp->ref_cnt, -1) != 1) 190 return; 191 192 free(icp, M_CXGBEI); 193 #ifdef DIAGNOSTIC 194 if (__predict_true(ic != NULL)) 195 refcount_release(&ic->ic_outstanding_pdus); 196 #endif 197 } 198 199 static void 200 icl_cxgbei_pdu_call_cb(struct icl_pdu *ip) 201 { 202 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 203 204 MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE); 205 206 if (icp->cb != NULL) 207 icp->cb(ip, icp->error); 208 #ifdef DIAGNOSTIC 209 if (__predict_true(ip->ip_conn != NULL)) 210 refcount_release(&ip->ip_conn->ic_outstanding_pdus); 211 #endif 212 free(icp, M_CXGBEI); 213 } 214 215 static void 216 icl_cxgbei_pdu_done(struct icl_pdu *ip, int error) 217 { 218 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 219 220 if (error != 0) 221 icp->error = error; 222 223 m_freem(ip->ip_ahs_mbuf); 224 ip->ip_ahs_mbuf = NULL; 225 m_freem(ip->ip_data_mbuf); 226 ip->ip_data_mbuf = NULL; 227 m_freem(ip->ip_bhs_mbuf); 228 ip->ip_bhs_mbuf = NULL; 229 230 /* 231 * All other references to this PDU should have been dropped 232 * by the m_freem() of ip_data_mbuf. 233 */ 234 if (atomic_fetchadd_int(&icp->ref_cnt, -1) == 1) 235 icl_cxgbei_pdu_call_cb(ip); 236 else 237 __assert_unreachable(); 238 } 239 240 static void 241 icl_cxgbei_mbuf_done(struct mbuf *mb) 242 { 243 244 struct icl_cxgbei_pdu *icp = (struct icl_cxgbei_pdu *)mb->m_ext.ext_arg1; 245 246 /* 247 * NB: mb_free_mext() might leave ref_cnt as 1 without 248 * decrementing it if it hits the fast path in the ref_cnt 249 * check. 250 */ 251 icl_cxgbei_pdu_call_cb(&icp->ip); 252 } 253 254 struct icl_pdu * 255 icl_cxgbei_new_pdu(int flags) 256 { 257 struct icl_cxgbei_pdu *icp; 258 struct icl_pdu *ip; 259 struct mbuf *m; 260 261 icp = malloc(sizeof(*icp), M_CXGBEI, flags | M_ZERO); 262 if (__predict_false(icp == NULL)) 263 return (NULL); 264 265 icp->icp_signature = CXGBEI_PDU_SIGNATURE; 266 icp->ref_cnt = 1; 267 ip = &icp->ip; 268 269 m = m_gethdr(flags, MT_DATA); 270 if (__predict_false(m == NULL)) { 271 free(icp, M_CXGBEI); 272 return (NULL); 273 } 274 275 ip->ip_bhs_mbuf = m; 276 ip->ip_bhs = mtod(m, struct iscsi_bhs *); 277 memset(ip->ip_bhs, 0, sizeof(*ip->ip_bhs)); 278 m->m_len = sizeof(struct iscsi_bhs); 279 m->m_pkthdr.len = m->m_len; 280 281 return (ip); 282 } 283 284 void 285 icl_cxgbei_new_pdu_set_conn(struct icl_pdu *ip, struct icl_conn *ic) 286 { 287 288 ip->ip_conn = ic; 289 #ifdef DIAGNOSTIC 290 refcount_acquire(&ic->ic_outstanding_pdus); 291 #endif 292 } 293 294 /* 295 * Allocate icl_pdu with empty BHS to fill up by the caller. 296 */ 297 static struct icl_pdu * 298 icl_cxgbei_conn_new_pdu(struct icl_conn *ic, int flags) 299 { 300 struct icl_pdu *ip; 301 302 ip = icl_cxgbei_new_pdu(flags); 303 if (__predict_false(ip == NULL)) 304 return (NULL); 305 icl_cxgbei_new_pdu_set_conn(ip, ic); 306 307 return (ip); 308 } 309 310 static size_t 311 icl_pdu_data_segment_length(const struct icl_pdu *request) 312 { 313 uint32_t len = 0; 314 315 len += request->ip_bhs->bhs_data_segment_len[0]; 316 len <<= 8; 317 len += request->ip_bhs->bhs_data_segment_len[1]; 318 len <<= 8; 319 len += request->ip_bhs->bhs_data_segment_len[2]; 320 321 return (len); 322 } 323 324 size_t 325 icl_cxgbei_conn_pdu_data_segment_length(struct icl_conn *ic, 326 const struct icl_pdu *request) 327 { 328 329 return (icl_pdu_data_segment_length(request)); 330 } 331 332 static struct mbuf * 333 finalize_pdu(struct icl_cxgbei_conn *icc, struct icl_cxgbei_pdu *icp) 334 { 335 struct icl_pdu *ip = &icp->ip; 336 uint8_t ulp_submode, padding; 337 struct mbuf *m, *last; 338 struct iscsi_bhs *bhs; 339 int data_len; 340 341 /* 342 * Fix up the data segment mbuf first. 343 */ 344 m = ip->ip_data_mbuf; 345 ulp_submode = icc->ulp_submode; 346 if (m != NULL) { 347 last = m_last(m); 348 349 /* 350 * Round up the data segment to a 4B boundary. Pad with 0 if 351 * necessary. There will definitely be room in the mbuf. 352 */ 353 padding = roundup2(ip->ip_data_len, 4) - ip->ip_data_len; 354 if (padding != 0) { 355 MPASS(padding <= M_TRAILINGSPACE(last)); 356 bzero(mtod(last, uint8_t *) + last->m_len, padding); 357 last->m_len += padding; 358 } 359 } else { 360 MPASS(ip->ip_data_len == 0); 361 ulp_submode &= ~ULP_CRC_DATA; 362 padding = 0; 363 } 364 365 /* 366 * Now the header mbuf that has the BHS. 367 */ 368 m = ip->ip_bhs_mbuf; 369 MPASS(m->m_pkthdr.len == sizeof(struct iscsi_bhs)); 370 MPASS(m->m_len == sizeof(struct iscsi_bhs)); 371 372 bhs = ip->ip_bhs; 373 data_len = ip->ip_data_len; 374 if (data_len > icc->ic.ic_max_send_data_segment_length) { 375 struct iscsi_bhs_data_in *bhsdi; 376 int flags; 377 378 KASSERT(padding == 0, ("%s: ISO with padding %d for icp %p", 379 __func__, padding, icp)); 380 switch (bhs->bhs_opcode) { 381 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT: 382 flags = 1; 383 break; 384 case ISCSI_BHS_OPCODE_SCSI_DATA_IN: 385 flags = 2; 386 break; 387 default: 388 panic("invalid opcode %#x for ISO", bhs->bhs_opcode); 389 } 390 data_len = icc->ic.ic_max_send_data_segment_length; 391 bhsdi = (struct iscsi_bhs_data_in *)bhs; 392 if (bhsdi->bhsdi_flags & BHSDI_FLAGS_F) { 393 /* 394 * Firmware will set F on the final PDU in the 395 * burst. 396 */ 397 flags |= CXGBE_ISO_F; 398 bhsdi->bhsdi_flags &= ~BHSDI_FLAGS_F; 399 } 400 set_mbuf_iscsi_iso(m, true); 401 set_mbuf_iscsi_iso_flags(m, flags); 402 set_mbuf_iscsi_iso_mss(m, data_len); 403 } 404 405 bhs->bhs_data_segment_len[2] = data_len; 406 bhs->bhs_data_segment_len[1] = data_len >> 8; 407 bhs->bhs_data_segment_len[0] = data_len >> 16; 408 409 /* 410 * Extract mbuf chain from PDU. 411 */ 412 m->m_pkthdr.len += ip->ip_data_len + padding; 413 m->m_next = ip->ip_data_mbuf; 414 set_mbuf_ulp_submode(m, ulp_submode); 415 ip->ip_bhs_mbuf = NULL; 416 ip->ip_data_mbuf = NULL; 417 ip->ip_bhs = NULL; 418 419 /* 420 * Drop PDU reference on icp. Additional references might 421 * still be held by zero-copy PDU buffers (ICL_NOCOPY). 422 */ 423 if (atomic_fetchadd_int(&icp->ref_cnt, -1) == 1) 424 icl_cxgbei_pdu_call_cb(ip); 425 426 return (m); 427 } 428 429 int 430 icl_cxgbei_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *ip, 431 const void *addr, size_t len, int flags) 432 { 433 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 434 struct mbuf *m, *m_tail; 435 const char *src; 436 437 MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE); 438 MPASS(ic == ip->ip_conn); 439 KASSERT(len > 0, ("%s: len is %jd", __func__, (intmax_t)len)); 440 441 m_tail = ip->ip_data_mbuf; 442 if (m_tail != NULL) 443 for (; m_tail->m_next != NULL; m_tail = m_tail->m_next) 444 ; 445 446 if (flags & ICL_NOCOPY) { 447 m = m_get(flags & ~ICL_NOCOPY, MT_DATA); 448 if (m == NULL) { 449 ICL_WARN("failed to allocate mbuf"); 450 return (ENOMEM); 451 } 452 453 m->m_flags |= M_RDONLY; 454 m_extaddref(m, __DECONST(char *, addr), len, &icp->ref_cnt, 455 icl_cxgbei_mbuf_done, icp, NULL); 456 m->m_len = len; 457 if (ip->ip_data_mbuf == NULL) { 458 ip->ip_data_mbuf = m; 459 ip->ip_data_len = len; 460 } else { 461 m_tail->m_next = m; 462 m_tail = m_tail->m_next; 463 ip->ip_data_len += len; 464 } 465 466 return (0); 467 } 468 469 src = (const char *)addr; 470 471 /* Allocate as jumbo mbufs of size MJUM16BYTES. */ 472 while (len >= MJUM16BYTES) { 473 m = m_getjcl(M_NOWAIT, MT_DATA, 0, MJUM16BYTES); 474 if (__predict_false(m == NULL)) { 475 if ((flags & M_WAITOK) != 0) { 476 /* Fall back to non-jumbo mbufs. */ 477 break; 478 } 479 return (ENOMEM); 480 } 481 memcpy(mtod(m, void *), src, MJUM16BYTES); 482 m->m_len = MJUM16BYTES; 483 if (ip->ip_data_mbuf == NULL) { 484 ip->ip_data_mbuf = m_tail = m; 485 ip->ip_data_len = MJUM16BYTES; 486 } else { 487 m_tail->m_next = m; 488 m_tail = m_tail->m_next; 489 ip->ip_data_len += MJUM16BYTES; 490 } 491 src += MJUM16BYTES; 492 len -= MJUM16BYTES; 493 } 494 495 /* Allocate mbuf chain for the remaining data. */ 496 if (len != 0) { 497 m = m_getm2(NULL, len, flags, MT_DATA, 0); 498 if (__predict_false(m == NULL)) 499 return (ENOMEM); 500 if (ip->ip_data_mbuf == NULL) { 501 ip->ip_data_mbuf = m; 502 ip->ip_data_len = len; 503 } else { 504 m_tail->m_next = m; 505 ip->ip_data_len += len; 506 } 507 for (; m != NULL; m = m->m_next) { 508 m->m_len = min(len, M_SIZE(m)); 509 memcpy(mtod(m, void *), src, m->m_len); 510 src += m->m_len; 511 len -= m->m_len; 512 } 513 MPASS(len == 0); 514 } 515 MPASS(ip->ip_data_len <= max(ic->ic_max_send_data_segment_length, 516 ic->ic_hw_isomax)); 517 518 return (0); 519 } 520 521 void 522 icl_cxgbei_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip, 523 size_t off, void *addr, size_t len) 524 { 525 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 526 527 if (icp->icp_flags & ICPF_RX_DDP) 528 return; /* data is DDP'ed, no need to copy */ 529 m_copydata(ip->ip_data_mbuf, off, len, addr); 530 } 531 532 void 533 icl_cxgbei_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip) 534 { 535 icl_cxgbei_conn_pdu_queue_cb(ic, ip, NULL); 536 } 537 538 void 539 icl_cxgbei_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip, 540 icl_pdu_cb cb) 541 { 542 struct epoch_tracker et; 543 struct icl_cxgbei_conn *icc = ic_to_icc(ic); 544 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 545 struct socket *so = ic->ic_socket; 546 struct toepcb *toep = icc->toep; 547 struct inpcb *inp; 548 struct mbuf *m; 549 550 MPASS(ic == ip->ip_conn); 551 MPASS(ip->ip_bhs_mbuf != NULL); 552 /* The kernel doesn't generate PDUs with AHS. */ 553 MPASS(ip->ip_ahs_mbuf == NULL && ip->ip_ahs_len == 0); 554 555 ICL_CONN_LOCK_ASSERT(ic); 556 557 icp->cb = cb; 558 559 /* NOTE: sowriteable without so_snd lock is a mostly harmless race. */ 560 if (ic->ic_disconnecting || so == NULL || !sowriteable(so)) { 561 icl_cxgbei_pdu_done(ip, ENOTCONN); 562 return; 563 } 564 565 m = finalize_pdu(icc, icp); 566 M_ASSERTPKTHDR(m); 567 MPASS((m->m_pkthdr.len & 3) == 0); 568 569 /* 570 * Do not get inp from toep->inp as the toepcb might have detached 571 * already. 572 */ 573 inp = sotoinpcb(so); 574 CURVNET_SET(toep->vnet); 575 NET_EPOCH_ENTER(et); 576 INP_WLOCK(inp); 577 if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) || 578 __predict_false((toep->flags & TPF_ATTACHED) == 0)) 579 m_freem(m); 580 else { 581 mbufq_enqueue(&toep->ulp_pduq, m); 582 t4_push_pdus(icc->sc, toep, 0); 583 } 584 INP_WUNLOCK(inp); 585 NET_EPOCH_EXIT(et); 586 CURVNET_RESTORE(); 587 } 588 589 static struct icl_conn * 590 icl_cxgbei_new_conn(const char *name, struct mtx *lock) 591 { 592 struct icl_cxgbei_conn *icc; 593 struct icl_conn *ic; 594 595 refcount_acquire(&icl_cxgbei_ncons); 596 597 icc = (struct icl_cxgbei_conn *)kobj_create(&icl_cxgbei_class, M_CXGBE, 598 M_WAITOK | M_ZERO); 599 icc->icc_signature = CXGBEI_CONN_SIGNATURE; 600 STAILQ_INIT(&icc->rcvd_pdus); 601 602 icc->cmp_table = hashinit(64, M_CXGBEI, &icc->cmp_hash_mask); 603 mtx_init(&icc->cmp_lock, "cxgbei_cmp", NULL, MTX_DEF); 604 605 ic = &icc->ic; 606 ic->ic_lock = lock; 607 608 #ifdef DIAGNOSTIC 609 refcount_init(&ic->ic_outstanding_pdus, 0); 610 #endif 611 ic->ic_name = name; 612 ic->ic_offload = "cxgbei"; 613 ic->ic_unmapped = false; 614 615 CTR2(KTR_CXGBE, "%s: icc %p", __func__, icc); 616 617 return (ic); 618 } 619 620 void 621 icl_cxgbei_conn_free(struct icl_conn *ic) 622 { 623 struct icl_cxgbei_conn *icc = ic_to_icc(ic); 624 625 MPASS(icc->icc_signature == CXGBEI_CONN_SIGNATURE); 626 627 CTR2(KTR_CXGBE, "%s: icc %p", __func__, icc); 628 629 mtx_destroy(&icc->cmp_lock); 630 hashdestroy(icc->cmp_table, M_CXGBEI, icc->cmp_hash_mask); 631 kobj_delete((struct kobj *)icc, M_CXGBE); 632 refcount_release(&icl_cxgbei_ncons); 633 } 634 635 static int 636 icl_cxgbei_setsockopt(struct icl_conn *ic, struct socket *so, int sspace, 637 int rspace) 638 { 639 struct sockopt opt; 640 int error, one = 1, ss, rs; 641 642 ss = max(sendspace, sspace); 643 rs = max(recvspace, rspace); 644 645 error = soreserve(so, ss, rs); 646 if (error != 0) { 647 icl_cxgbei_conn_close(ic); 648 return (error); 649 } 650 SOCKBUF_LOCK(&so->so_snd); 651 so->so_snd.sb_flags |= SB_AUTOSIZE; 652 SOCKBUF_UNLOCK(&so->so_snd); 653 SOCKBUF_LOCK(&so->so_rcv); 654 so->so_rcv.sb_flags |= SB_AUTOSIZE; 655 SOCKBUF_UNLOCK(&so->so_rcv); 656 657 /* 658 * Disable Nagle. 659 */ 660 bzero(&opt, sizeof(opt)); 661 opt.sopt_dir = SOPT_SET; 662 opt.sopt_level = IPPROTO_TCP; 663 opt.sopt_name = TCP_NODELAY; 664 opt.sopt_val = &one; 665 opt.sopt_valsize = sizeof(one); 666 error = sosetopt(so, &opt); 667 if (error != 0) { 668 icl_cxgbei_conn_close(ic); 669 return (error); 670 } 671 672 return (0); 673 } 674 675 /* 676 * Request/response structure used to find out the adapter offloading a socket. 677 */ 678 struct find_ofld_adapter_rr { 679 struct socket *so; 680 struct adapter *sc; /* result */ 681 }; 682 683 static void 684 find_offload_adapter(struct adapter *sc, void *arg) 685 { 686 struct find_ofld_adapter_rr *fa = arg; 687 struct socket *so = fa->so; 688 struct tom_data *td = sc->tom_softc; 689 struct tcpcb *tp; 690 struct inpcb *inp; 691 692 /* Non-TCP were filtered out earlier. */ 693 MPASS(so->so_proto->pr_protocol == IPPROTO_TCP); 694 695 if (fa->sc != NULL) 696 return; /* Found already. */ 697 698 if (td == NULL) 699 return; /* TOE not enabled on this adapter. */ 700 701 inp = sotoinpcb(so); 702 INP_WLOCK(inp); 703 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) { 704 tp = intotcpcb(inp); 705 if (tp->t_flags & TF_TOE && tp->tod == &td->tod) 706 fa->sc = sc; /* Found. */ 707 } 708 INP_WUNLOCK(inp); 709 } 710 711 static bool 712 is_memfree(struct adapter *sc) 713 { 714 uint32_t em; 715 716 em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE); 717 if ((em & F_EXT_MEM_ENABLE) != 0) 718 return (false); 719 if (is_t5(sc) && (em & F_EXT_MEM1_ENABLE) != 0) 720 return (false); 721 return (true); 722 } 723 724 /* XXXNP: move this to t4_tom. */ 725 static void 726 send_iscsi_flowc_wr(struct adapter *sc, struct toepcb *toep, int maxlen) 727 { 728 struct wrqe *wr; 729 struct fw_flowc_wr *flowc; 730 const u_int nparams = 1; 731 u_int flowclen; 732 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx]; 733 734 flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval); 735 736 wr = alloc_wrqe(roundup2(flowclen, 16), &toep->ofld_txq->wrq); 737 if (wr == NULL) { 738 /* XXX */ 739 panic("%s: allocation failure.", __func__); 740 } 741 flowc = wrtod(wr); 742 memset(flowc, 0, wr->wr_len); 743 744 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) | 745 V_FW_FLOWC_WR_NPARAMS(nparams)); 746 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) | 747 V_FW_WR_FLOWID(toep->tid)); 748 749 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX; 750 flowc->mnemval[0].val = htobe32(maxlen); 751 752 txsd->tx_credits = howmany(flowclen, 16); 753 txsd->plen = 0; 754 KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0, 755 ("%s: not enough credits (%d)", __func__, toep->tx_credits)); 756 toep->tx_credits -= txsd->tx_credits; 757 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) 758 toep->txsd_pidx = 0; 759 toep->txsd_avail--; 760 761 t4_wrq_tx(sc, wr); 762 } 763 764 static void 765 set_ulp_mode_iscsi(struct adapter *sc, struct toepcb *toep, u_int ulp_submode) 766 { 767 uint64_t val; 768 769 CTR3(KTR_CXGBE, "%s: tid %u, ULP_MODE_ISCSI, submode=%#x", 770 __func__, toep->tid, ulp_submode); 771 772 val = V_TCB_ULP_TYPE(ULP_MODE_ISCSI) | V_TCB_ULP_RAW(ulp_submode); 773 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_ULP_TYPE, 774 V_TCB_ULP_TYPE(M_TCB_ULP_TYPE) | V_TCB_ULP_RAW(M_TCB_ULP_RAW), val, 775 0, 0); 776 777 val = V_TF_RX_FLOW_CONTROL_DISABLE(1ULL); 778 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS, val, val, 0, 0); 779 } 780 781 /* 782 * XXXNP: Who is responsible for cleaning up the socket if this returns with an 783 * error? Review all error paths. 784 * 785 * XXXNP: What happens to the socket's fd reference if the operation is 786 * successful, and how does that affect the socket's life cycle? 787 */ 788 int 789 icl_cxgbei_conn_handoff(struct icl_conn *ic, int fd) 790 { 791 struct icl_cxgbei_conn *icc = ic_to_icc(ic); 792 struct cxgbei_data *ci; 793 struct find_ofld_adapter_rr fa; 794 struct file *fp; 795 struct socket *so; 796 struct inpcb *inp; 797 struct tcpcb *tp; 798 struct toepcb *toep; 799 cap_rights_t rights; 800 u_int max_rx_pdu_len, max_tx_pdu_len; 801 int error, max_iso_pdus; 802 803 MPASS(icc->icc_signature == CXGBEI_CONN_SIGNATURE); 804 ICL_CONN_LOCK_ASSERT_NOT(ic); 805 806 /* 807 * Steal the socket from userland. 808 */ 809 error = fget(curthread, fd, 810 cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp); 811 if (error != 0) 812 return (error); 813 if (fp->f_type != DTYPE_SOCKET) { 814 fdrop(fp, curthread); 815 return (EINVAL); 816 } 817 so = fp->f_data; 818 if (so->so_type != SOCK_STREAM || 819 so->so_proto->pr_protocol != IPPROTO_TCP) { 820 fdrop(fp, curthread); 821 return (EINVAL); 822 } 823 824 ICL_CONN_LOCK(ic); 825 if (ic->ic_socket != NULL) { 826 ICL_CONN_UNLOCK(ic); 827 fdrop(fp, curthread); 828 return (EBUSY); 829 } 830 ic->ic_disconnecting = false; 831 ic->ic_socket = so; 832 fp->f_ops = &badfileops; 833 fp->f_data = NULL; 834 fdrop(fp, curthread); 835 ICL_CONN_UNLOCK(ic); 836 837 /* Find the adapter offloading this socket. */ 838 fa.sc = NULL; 839 fa.so = so; 840 t4_iterate(find_offload_adapter, &fa); 841 if (fa.sc == NULL) 842 return (EINVAL); 843 icc->sc = fa.sc; 844 ci = icc->sc->iscsi_ulp_softc; 845 846 max_rx_pdu_len = ISCSI_BHS_SIZE + ic->ic_max_recv_data_segment_length; 847 max_tx_pdu_len = ISCSI_BHS_SIZE + ic->ic_max_send_data_segment_length; 848 if (ic->ic_header_crc32c) { 849 max_rx_pdu_len += ISCSI_HEADER_DIGEST_SIZE; 850 max_tx_pdu_len += ISCSI_HEADER_DIGEST_SIZE; 851 } 852 if (ic->ic_data_crc32c) { 853 max_rx_pdu_len += ISCSI_DATA_DIGEST_SIZE; 854 max_tx_pdu_len += ISCSI_DATA_DIGEST_SIZE; 855 } 856 857 inp = sotoinpcb(so); 858 INP_WLOCK(inp); 859 tp = intotcpcb(inp); 860 if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) 861 error = EBUSY; 862 else { 863 /* 864 * socket could not have been "unoffloaded" if here. 865 */ 866 MPASS(tp->t_flags & TF_TOE); 867 MPASS(tp->tod != NULL); 868 MPASS(tp->t_toe != NULL); 869 toep = tp->t_toe; 870 MPASS(toep->vi->adapter == icc->sc); 871 icc->toep = toep; 872 icc->cwt = cxgbei_select_worker_thread(icc); 873 874 icc->ulp_submode = 0; 875 if (ic->ic_header_crc32c) 876 icc->ulp_submode |= ULP_CRC_HEADER; 877 if (ic->ic_data_crc32c) 878 icc->ulp_submode |= ULP_CRC_DATA; 879 880 if (icc->sc->tt.iso && chip_id(icc->sc) >= CHELSIO_T5 && 881 !is_memfree(icc->sc)) { 882 max_iso_pdus = CXGBEI_MAX_ISO_PAYLOAD / 883 max_tx_pdu_len; 884 ic->ic_hw_isomax = max_iso_pdus * 885 ic->ic_max_send_data_segment_length; 886 } else 887 max_iso_pdus = 1; 888 889 so->so_options |= SO_NO_DDP; 890 toep->params.ulp_mode = ULP_MODE_ISCSI; 891 toep->ulpcb = icc; 892 893 send_iscsi_flowc_wr(icc->sc, toep, 894 roundup(max_iso_pdus * max_tx_pdu_len, tp->t_maxseg)); 895 set_ulp_mode_iscsi(icc->sc, toep, icc->ulp_submode); 896 error = 0; 897 } 898 INP_WUNLOCK(inp); 899 900 if (error == 0) { 901 error = icl_cxgbei_setsockopt(ic, so, max_tx_pdu_len, 902 max_rx_pdu_len); 903 } 904 905 return (error); 906 } 907 908 void 909 icl_cxgbei_conn_close(struct icl_conn *ic) 910 { 911 struct icl_cxgbei_conn *icc = ic_to_icc(ic); 912 struct icl_pdu *ip; 913 struct socket *so; 914 struct sockbuf *sb; 915 struct inpcb *inp; 916 struct toepcb *toep = icc->toep; 917 918 MPASS(icc->icc_signature == CXGBEI_CONN_SIGNATURE); 919 ICL_CONN_LOCK_ASSERT_NOT(ic); 920 921 ICL_CONN_LOCK(ic); 922 so = ic->ic_socket; 923 if (ic->ic_disconnecting || so == NULL) { 924 CTR4(KTR_CXGBE, "%s: icc %p (disconnecting = %d), so %p", 925 __func__, icc, ic->ic_disconnecting, so); 926 ICL_CONN_UNLOCK(ic); 927 return; 928 } 929 ic->ic_disconnecting = true; 930 931 #ifdef DIAGNOSTIC 932 KASSERT(ic->ic_outstanding_pdus == 0, 933 ("destroying session with %d outstanding PDUs", 934 ic->ic_outstanding_pdus)); 935 #endif 936 ICL_CONN_UNLOCK(ic); 937 938 CTR3(KTR_CXGBE, "%s: tid %d, icc %p", __func__, toep ? toep->tid : -1, 939 icc); 940 inp = sotoinpcb(so); 941 sb = &so->so_rcv; 942 INP_WLOCK(inp); 943 if (toep != NULL) { /* NULL if connection was never offloaded. */ 944 toep->ulpcb = NULL; 945 946 /* Discard PDUs queued for TX. */ 947 mbufq_drain(&toep->ulp_pduq); 948 949 /* 950 * Wait for the cwt threads to stop processing this 951 * connection. 952 */ 953 SOCKBUF_LOCK(sb); 954 if (icc->rx_flags & RXF_ACTIVE) { 955 volatile u_int *p = &icc->rx_flags; 956 957 SOCKBUF_UNLOCK(sb); 958 INP_WUNLOCK(inp); 959 960 while (*p & RXF_ACTIVE) 961 pause("conclo", 1); 962 963 INP_WLOCK(inp); 964 SOCKBUF_LOCK(sb); 965 } 966 967 /* 968 * Discard received PDUs not passed to the iSCSI 969 * layer. 970 */ 971 while (!STAILQ_EMPTY(&icc->rcvd_pdus)) { 972 ip = STAILQ_FIRST(&icc->rcvd_pdus); 973 STAILQ_REMOVE_HEAD(&icc->rcvd_pdus, ip_next); 974 icl_cxgbei_pdu_done(ip, ENOTCONN); 975 } 976 SOCKBUF_UNLOCK(sb); 977 978 /* 979 * Grab a reference to use when waiting for the final 980 * CPL to be received. If toep->inp is NULL, then 981 * final_cpl_received() has already been called (e.g. 982 * due to the peer sending a RST). 983 */ 984 if (toep->inp != NULL) { 985 toep = hold_toepcb(toep); 986 toep->flags |= TPF_WAITING_FOR_FINAL; 987 } else 988 toep = NULL; 989 } 990 INP_WUNLOCK(inp); 991 992 ICL_CONN_LOCK(ic); 993 ic->ic_socket = NULL; 994 ICL_CONN_UNLOCK(ic); 995 996 /* 997 * XXXNP: we should send RST instead of FIN when PDUs held in various 998 * queues were purged instead of delivered reliably but soabort isn't 999 * really general purpose and wouldn't do the right thing here. 1000 */ 1001 soclose(so); 1002 1003 /* 1004 * Wait for the socket to fully close. This ensures any 1005 * pending received data has been received (and in particular, 1006 * any data that would be received by DDP has been handled). 1007 * Callers assume that it is safe to free buffers for tasks 1008 * and transfers after this function returns. 1009 */ 1010 if (toep != NULL) { 1011 struct mtx *lock = mtx_pool_find(mtxpool_sleep, toep); 1012 1013 mtx_lock(lock); 1014 while ((toep->flags & TPF_WAITING_FOR_FINAL) != 0) 1015 mtx_sleep(toep, lock, PSOCK, "conclo2", 0); 1016 mtx_unlock(lock); 1017 free_toepcb(toep); 1018 } 1019 } 1020 1021 static void 1022 cxgbei_insert_cmp(struct icl_cxgbei_conn *icc, struct cxgbei_cmp *cmp, 1023 uint32_t tt) 1024 { 1025 #ifdef INVARIANTS 1026 struct cxgbei_cmp *cmp2; 1027 #endif 1028 1029 cmp->tt = tt; 1030 1031 mtx_lock(&icc->cmp_lock); 1032 #ifdef INVARIANTS 1033 LIST_FOREACH(cmp2, &icc->cmp_table[TT_HASH(icc, tt)], link) { 1034 KASSERT(cmp2->tt != tt, ("%s: duplicate cmp", __func__)); 1035 } 1036 #endif 1037 LIST_INSERT_HEAD(&icc->cmp_table[TT_HASH(icc, tt)], cmp, link); 1038 mtx_unlock(&icc->cmp_lock); 1039 } 1040 1041 struct cxgbei_cmp * 1042 cxgbei_find_cmp(struct icl_cxgbei_conn *icc, uint32_t tt) 1043 { 1044 struct cxgbei_cmp *cmp; 1045 1046 mtx_lock(&icc->cmp_lock); 1047 LIST_FOREACH(cmp, &icc->cmp_table[TT_HASH(icc, tt)], link) { 1048 if (cmp->tt == tt) 1049 break; 1050 } 1051 mtx_unlock(&icc->cmp_lock); 1052 return (cmp); 1053 } 1054 1055 static void 1056 cxgbei_rm_cmp(struct icl_cxgbei_conn *icc, struct cxgbei_cmp *cmp) 1057 { 1058 #ifdef INVARIANTS 1059 struct cxgbei_cmp *cmp2; 1060 #endif 1061 1062 mtx_lock(&icc->cmp_lock); 1063 1064 #ifdef INVARIANTS 1065 LIST_FOREACH(cmp2, &icc->cmp_table[TT_HASH(icc, cmp->tt)], link) { 1066 if (cmp2 == cmp) 1067 goto found; 1068 } 1069 panic("%s: could not find cmp", __func__); 1070 found: 1071 #endif 1072 LIST_REMOVE(cmp, link); 1073 mtx_unlock(&icc->cmp_lock); 1074 } 1075 1076 int 1077 icl_cxgbei_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip, 1078 struct ccb_scsiio *csio, uint32_t *ittp, void **arg) 1079 { 1080 struct icl_cxgbei_conn *icc = ic_to_icc(ic); 1081 struct toepcb *toep = icc->toep; 1082 struct adapter *sc = icc->sc; 1083 struct cxgbei_data *ci = sc->iscsi_ulp_softc; 1084 struct ppod_region *pr = &ci->pr; 1085 struct cxgbei_ddp_state *ddp; 1086 struct ppod_reservation *prsv; 1087 struct inpcb *inp; 1088 struct mbufq mq; 1089 uint32_t itt; 1090 int rc = 0; 1091 1092 ICL_CONN_LOCK_ASSERT(ic); 1093 1094 /* This is for the offload driver's state. Must not be set already. */ 1095 MPASS(arg != NULL); 1096 MPASS(*arg == NULL); 1097 1098 if (ic->ic_disconnecting || ic->ic_socket == NULL) 1099 return (ECONNRESET); 1100 1101 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_IN || 1102 csio->dxfer_len < ci->ddp_threshold) { 1103 no_ddp: 1104 /* 1105 * No DDP for this I/O. Allocate an ITT (based on the one 1106 * passed in) that cannot be a valid hardware DDP tag in the 1107 * iSCSI region. 1108 */ 1109 itt = *ittp & M_PPOD_TAG; 1110 itt = V_PPOD_TAG(itt) | pr->pr_invalid_bit; 1111 *ittp = htobe32(itt); 1112 MPASS(*arg == NULL); /* State is maintained for DDP only. */ 1113 if (rc != 0) 1114 counter_u64_add( 1115 toep->ofld_rxq->rx_iscsi_ddp_setup_error, 1); 1116 return (0); 1117 } 1118 1119 /* 1120 * Reserve resources for DDP, update the itt that should be used in the 1121 * PDU, and save DDP specific state for this I/O in *arg. 1122 */ 1123 ddp = malloc(sizeof(*ddp), M_CXGBEI, M_NOWAIT | M_ZERO); 1124 if (ddp == NULL) { 1125 rc = ENOMEM; 1126 goto no_ddp; 1127 } 1128 prsv = &ddp->prsv; 1129 1130 /* XXX add support for all CAM_DATA_ types */ 1131 MPASS((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR); 1132 rc = t4_alloc_page_pods_for_buf(pr, (vm_offset_t)csio->data_ptr, 1133 csio->dxfer_len, prsv); 1134 if (rc != 0) { 1135 free(ddp, M_CXGBEI); 1136 goto no_ddp; 1137 } 1138 1139 mbufq_init(&mq, INT_MAX); 1140 rc = t4_write_page_pods_for_buf(sc, toep, prsv, 1141 (vm_offset_t)csio->data_ptr, csio->dxfer_len, &mq); 1142 if (__predict_false(rc != 0)) { 1143 mbufq_drain(&mq); 1144 t4_free_page_pods(prsv); 1145 free(ddp, M_CXGBEI); 1146 goto no_ddp; 1147 } 1148 1149 /* 1150 * Do not get inp from toep->inp as the toepcb might have 1151 * detached already. 1152 */ 1153 inp = sotoinpcb(ic->ic_socket); 1154 INP_WLOCK(inp); 1155 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) != 0) { 1156 INP_WUNLOCK(inp); 1157 mbufq_drain(&mq); 1158 t4_free_page_pods(prsv); 1159 free(ddp, M_CXGBEI); 1160 return (ECONNRESET); 1161 } 1162 mbufq_concat(&toep->ulp_pduq, &mq); 1163 INP_WUNLOCK(inp); 1164 1165 ddp->cmp.last_datasn = -1; 1166 cxgbei_insert_cmp(icc, &ddp->cmp, prsv->prsv_tag); 1167 *ittp = htobe32(prsv->prsv_tag); 1168 *arg = prsv; 1169 counter_u64_add(toep->ofld_rxq->rx_iscsi_ddp_setup_ok, 1); 1170 return (0); 1171 } 1172 1173 void 1174 icl_cxgbei_conn_task_done(struct icl_conn *ic, void *arg) 1175 { 1176 1177 if (arg != NULL) { 1178 struct cxgbei_ddp_state *ddp = arg; 1179 1180 cxgbei_rm_cmp(ic_to_icc(ic), &ddp->cmp); 1181 t4_free_page_pods(&ddp->prsv); 1182 free(ddp, M_CXGBEI); 1183 } 1184 } 1185 1186 static inline bool 1187 ddp_sgl_check(struct ctl_sg_entry *sg, int entries, int xferlen) 1188 { 1189 int total_len = 0; 1190 1191 MPASS(entries > 0); 1192 if (((vm_offset_t)sg[--entries].addr & 3U) != 0) 1193 return (false); 1194 1195 total_len += sg[entries].len; 1196 1197 while (--entries >= 0) { 1198 if (((vm_offset_t)sg[entries].addr & PAGE_MASK) != 0 || 1199 (sg[entries].len % PAGE_SIZE) != 0) 1200 return (false); 1201 total_len += sg[entries].len; 1202 } 1203 1204 MPASS(total_len == xferlen); 1205 return (true); 1206 } 1207 1208 /* XXXNP: PDU should be passed in as parameter, like on the initiator. */ 1209 #define io_to_request_pdu(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr) 1210 #define io_to_ddp_state(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr) 1211 1212 int 1213 icl_cxgbei_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io, 1214 uint32_t *tttp, void **arg) 1215 { 1216 struct icl_cxgbei_conn *icc = ic_to_icc(ic); 1217 struct toepcb *toep = icc->toep; 1218 struct ctl_scsiio *ctsio = &io->scsiio; 1219 struct adapter *sc = icc->sc; 1220 struct cxgbei_data *ci = sc->iscsi_ulp_softc; 1221 struct ppod_region *pr = &ci->pr; 1222 struct cxgbei_ddp_state *ddp; 1223 struct ppod_reservation *prsv; 1224 struct ctl_sg_entry *sgl, sg_entry; 1225 struct inpcb *inp; 1226 struct mbufq mq; 1227 int sg_entries = ctsio->kern_sg_entries; 1228 uint32_t ttt; 1229 int xferlen, rc = 0, alias; 1230 1231 /* This is for the offload driver's state. Must not be set already. */ 1232 MPASS(arg != NULL); 1233 MPASS(*arg == NULL); 1234 1235 if (ctsio->ext_data_filled == 0) { 1236 int first_burst; 1237 struct icl_pdu *ip = io_to_request_pdu(io); 1238 #ifdef INVARIANTS 1239 struct icl_cxgbei_pdu *icp = ip_to_icp(ip); 1240 1241 MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE); 1242 MPASS(ic == ip->ip_conn); 1243 MPASS(ip->ip_bhs_mbuf != NULL); 1244 #endif 1245 first_burst = icl_pdu_data_segment_length(ip); 1246 1247 /* 1248 * Note that ICL calls conn_transfer_setup even if the first 1249 * burst had everything and there's nothing left to transfer. 1250 * 1251 * NB: The CTL frontend might have provided a buffer 1252 * whose length (kern_data_len) is smaller than the 1253 * FirstBurstLength of unsolicited data. Treat those 1254 * as an empty transfer. 1255 */ 1256 xferlen = ctsio->kern_data_len; 1257 if (xferlen < first_burst || 1258 xferlen - first_burst < ci->ddp_threshold) { 1259 no_ddp: 1260 /* 1261 * No DDP for this transfer. Allocate a TTT (based on 1262 * the one passed in) that cannot be a valid hardware 1263 * DDP tag in the iSCSI region. 1264 */ 1265 ttt = *tttp & M_PPOD_TAG; 1266 ttt = V_PPOD_TAG(ttt) | pr->pr_invalid_bit; 1267 *tttp = htobe32(ttt); 1268 MPASS(io_to_ddp_state(io) == NULL); 1269 if (rc != 0) 1270 counter_u64_add( 1271 toep->ofld_rxq->rx_iscsi_ddp_setup_error, 1); 1272 return (0); 1273 } 1274 1275 if (sg_entries == 0) { 1276 sgl = &sg_entry; 1277 sgl->len = xferlen; 1278 sgl->addr = (void *)ctsio->kern_data_ptr; 1279 sg_entries = 1; 1280 } else 1281 sgl = (void *)ctsio->kern_data_ptr; 1282 1283 if (!ddp_sgl_check(sgl, sg_entries, xferlen)) 1284 goto no_ddp; 1285 1286 /* 1287 * Reserve resources for DDP, update the ttt that should be used 1288 * in the PDU, and save DDP specific state for this I/O. 1289 */ 1290 MPASS(io_to_ddp_state(io) == NULL); 1291 ddp = malloc(sizeof(*ddp), M_CXGBEI, M_NOWAIT | M_ZERO); 1292 if (ddp == NULL) { 1293 rc = ENOMEM; 1294 goto no_ddp; 1295 } 1296 prsv = &ddp->prsv; 1297 1298 rc = t4_alloc_page_pods_for_sgl(pr, sgl, sg_entries, prsv); 1299 if (rc != 0) { 1300 free(ddp, M_CXGBEI); 1301 goto no_ddp; 1302 } 1303 1304 mbufq_init(&mq, INT_MAX); 1305 rc = t4_write_page_pods_for_sgl(sc, toep, prsv, sgl, sg_entries, 1306 xferlen, &mq); 1307 if (__predict_false(rc != 0)) { 1308 mbufq_drain(&mq); 1309 t4_free_page_pods(prsv); 1310 free(ddp, M_CXGBEI); 1311 goto no_ddp; 1312 } 1313 1314 /* 1315 * Do not get inp from toep->inp as the toepcb might 1316 * have detached already. 1317 */ 1318 ICL_CONN_LOCK(ic); 1319 if (ic->ic_disconnecting || ic->ic_socket == NULL) { 1320 ICL_CONN_UNLOCK(ic); 1321 mbufq_drain(&mq); 1322 t4_free_page_pods(prsv); 1323 free(ddp, M_CXGBEI); 1324 return (ECONNRESET); 1325 } 1326 inp = sotoinpcb(ic->ic_socket); 1327 INP_WLOCK(inp); 1328 ICL_CONN_UNLOCK(ic); 1329 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) != 0) { 1330 INP_WUNLOCK(inp); 1331 mbufq_drain(&mq); 1332 t4_free_page_pods(prsv); 1333 free(ddp, M_CXGBEI); 1334 return (ECONNRESET); 1335 } 1336 mbufq_concat(&toep->ulp_pduq, &mq); 1337 INP_WUNLOCK(inp); 1338 1339 ddp->cmp.next_buffer_offset = ctsio->kern_rel_offset + 1340 first_burst; 1341 ddp->cmp.last_datasn = -1; 1342 cxgbei_insert_cmp(icc, &ddp->cmp, prsv->prsv_tag); 1343 *tttp = htobe32(prsv->prsv_tag); 1344 io_to_ddp_state(io) = ddp; 1345 *arg = ctsio; 1346 counter_u64_add(toep->ofld_rxq->rx_iscsi_ddp_setup_ok, 1); 1347 return (0); 1348 } 1349 1350 /* 1351 * In the middle of an I/O. A non-NULL page pod reservation indicates 1352 * that a DDP buffer is being used for the I/O. 1353 */ 1354 ddp = io_to_ddp_state(ctsio); 1355 if (ddp == NULL) 1356 goto no_ddp; 1357 prsv = &ddp->prsv; 1358 1359 alias = (prsv->prsv_tag & pr->pr_alias_mask) >> pr->pr_alias_shift; 1360 alias++; 1361 prsv->prsv_tag &= ~pr->pr_alias_mask; 1362 prsv->prsv_tag |= alias << pr->pr_alias_shift & pr->pr_alias_mask; 1363 1364 ddp->cmp.last_datasn = -1; 1365 cxgbei_insert_cmp(icc, &ddp->cmp, prsv->prsv_tag); 1366 *tttp = htobe32(prsv->prsv_tag); 1367 *arg = ctsio; 1368 1369 return (0); 1370 } 1371 1372 void 1373 icl_cxgbei_conn_transfer_done(struct icl_conn *ic, void *arg) 1374 { 1375 struct ctl_scsiio *ctsio = arg; 1376 1377 if (ctsio != NULL) { 1378 struct cxgbei_ddp_state *ddp; 1379 1380 ddp = io_to_ddp_state(ctsio); 1381 MPASS(ddp != NULL); 1382 1383 cxgbei_rm_cmp(ic_to_icc(ic), &ddp->cmp); 1384 if (ctsio->kern_data_len == ctsio->ext_data_filled || 1385 ic->ic_disconnecting) { 1386 t4_free_page_pods(&ddp->prsv); 1387 free(ddp, M_CXGBEI); 1388 io_to_ddp_state(ctsio) = NULL; 1389 } 1390 } 1391 } 1392 1393 static void 1394 cxgbei_limits(struct adapter *sc, void *arg) 1395 { 1396 struct icl_drv_limits *idl = arg; 1397 struct cxgbei_data *ci; 1398 int max_dsl; 1399 1400 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4lims") != 0) 1401 return; 1402 1403 if (uld_active(sc, ULD_ISCSI)) { 1404 ci = sc->iscsi_ulp_softc; 1405 MPASS(ci != NULL); 1406 1407 1408 max_dsl = ci->max_rx_data_len; 1409 if (idl->idl_max_recv_data_segment_length > max_dsl) 1410 idl->idl_max_recv_data_segment_length = max_dsl; 1411 1412 max_dsl = ci->max_tx_data_len; 1413 if (idl->idl_max_send_data_segment_length > max_dsl) 1414 idl->idl_max_send_data_segment_length = max_dsl; 1415 } 1416 1417 end_synchronized_op(sc, LOCK_HELD); 1418 } 1419 1420 static int 1421 icl_cxgbei_limits(struct icl_drv_limits *idl) 1422 { 1423 1424 /* Maximum allowed by the RFC. cxgbei_limits will clip them. */ 1425 idl->idl_max_recv_data_segment_length = (1 << 24) - 1; 1426 idl->idl_max_send_data_segment_length = (1 << 24) - 1; 1427 1428 /* These are somewhat arbitrary. */ 1429 idl->idl_max_burst_length = max_burst_length; 1430 idl->idl_first_burst_length = first_burst_length; 1431 1432 t4_iterate(cxgbei_limits, idl); 1433 1434 return (0); 1435 } 1436 1437 int 1438 icl_cxgbei_mod_load(void) 1439 { 1440 int rc; 1441 1442 refcount_init(&icl_cxgbei_ncons, 0); 1443 1444 rc = icl_register("cxgbei", false, -100, icl_cxgbei_limits, 1445 icl_cxgbei_new_conn); 1446 1447 return (rc); 1448 } 1449 1450 int 1451 icl_cxgbei_mod_unload(void) 1452 { 1453 1454 if (icl_cxgbei_ncons != 0) 1455 return (EBUSY); 1456 1457 icl_unregister("cxgbei", false); 1458 1459 return (0); 1460 } 1461 #endif 1462