1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 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 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/condvar.h> 37 #include <sys/conf.h> 38 #include <sys/endian.h> 39 #include <sys/eventhandler.h> 40 #include <sys/file.h> 41 #include <sys/kernel.h> 42 #include <sys/kthread.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/mutex.h> 46 #include <sys/module.h> 47 #include <sys/socket.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 #include <sys/sx.h> 51 #include <vm/uma.h> 52 53 #include <cam/cam.h> 54 #include <cam/cam_ccb.h> 55 #include <cam/cam_xpt.h> 56 #include <cam/cam_debug.h> 57 #include <cam/cam_sim.h> 58 #include <cam/cam_xpt_sim.h> 59 #include <cam/cam_xpt_periph.h> 60 #include <cam/cam_periph.h> 61 #include <cam/scsi/scsi_all.h> 62 #include <cam/scsi/scsi_message.h> 63 64 #include <dev/iscsi/icl.h> 65 #include <dev/iscsi/icl_wrappers.h> 66 #include <dev/iscsi/iscsi_ioctl.h> 67 #include <dev/iscsi/iscsi_proto.h> 68 #include <dev/iscsi/iscsi.h> 69 70 #ifdef ICL_KERNEL_PROXY 71 #include <sys/socketvar.h> 72 #endif 73 74 #ifdef ICL_KERNEL_PROXY 75 FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY"); 76 #endif 77 78 /* 79 * XXX: This is global so the iscsi_unload() can access it. 80 * Think about how to do this properly. 81 */ 82 static struct iscsi_softc *sc; 83 84 SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 85 "iSCSI initiator"); 86 static int debug = 1; 87 SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN, 88 &debug, 0, "Enable debug messages"); 89 static int ping_timeout = 5; 90 SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout, 91 0, "Timeout for ping (NOP-Out) requests, in seconds"); 92 static int iscsid_timeout = 60; 93 SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout, 94 0, "Time to wait for iscsid(8) to handle reconnection, in seconds"); 95 static int login_timeout = 60; 96 SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout, 97 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds"); 98 static int maxtags = 255; 99 SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags, 100 0, "Max number of IO requests queued"); 101 static int fail_on_disconnection = 0; 102 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN, 103 &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure"); 104 static int fail_on_shutdown = 1; 105 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_shutdown, CTLFLAG_RWTUN, 106 &fail_on_shutdown, 0, "Fail disconnected sessions on shutdown"); 107 108 static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator"); 109 static uma_zone_t iscsi_outstanding_zone; 110 111 #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0) 112 #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn)) 113 114 #define ISCSI_DEBUG(X, ...) \ 115 do { \ 116 if (debug > 1) \ 117 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\ 118 } while (0) 119 120 #define ISCSI_WARN(X, ...) \ 121 do { \ 122 if (debug > 0) { \ 123 printf("WARNING: %s: " X "\n", \ 124 __func__, ## __VA_ARGS__); \ 125 } \ 126 } while (0) 127 128 #define ISCSI_SESSION_DEBUG(S, X, ...) \ 129 do { \ 130 if (debug > 1) { \ 131 printf("%s: %s (%s): " X "\n", \ 132 __func__, S->is_conf.isc_target_addr, \ 133 S->is_conf.isc_target, ## __VA_ARGS__); \ 134 } \ 135 } while (0) 136 137 #define ISCSI_SESSION_WARN(S, X, ...) \ 138 do { \ 139 if (debug > 0) { \ 140 printf("WARNING: %s (%s): " X "\n", \ 141 S->is_conf.isc_target_addr, \ 142 S->is_conf.isc_target, ## __VA_ARGS__); \ 143 } \ 144 } while (0) 145 146 #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock) 147 #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock) 148 #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED) 149 #define ISCSI_SESSION_LOCK_ASSERT_NOT(X) mtx_assert(&X->is_lock, MA_NOTOWNED) 150 151 static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, 152 int mode, struct thread *td); 153 154 static struct cdevsw iscsi_cdevsw = { 155 .d_version = D_VERSION, 156 .d_ioctl = iscsi_ioctl, 157 .d_name = "iscsi", 158 }; 159 160 static void iscsi_pdu_queue_locked(struct icl_pdu *request); 161 static void iscsi_pdu_queue(struct icl_pdu *request); 162 static void iscsi_pdu_update_statsn(const struct icl_pdu *response); 163 static void iscsi_pdu_handle_nop_in(struct icl_pdu *response); 164 static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response); 165 static void iscsi_pdu_handle_task_response(struct icl_pdu *response); 166 static void iscsi_pdu_handle_data_in(struct icl_pdu *response); 167 static void iscsi_pdu_handle_logout_response(struct icl_pdu *response); 168 static void iscsi_pdu_handle_r2t(struct icl_pdu *response); 169 static void iscsi_pdu_handle_async_message(struct icl_pdu *response); 170 static void iscsi_pdu_handle_reject(struct icl_pdu *response); 171 static void iscsi_session_reconnect(struct iscsi_session *is); 172 static void iscsi_session_terminate(struct iscsi_session *is); 173 static void iscsi_action(struct cam_sim *sim, union ccb *ccb); 174 static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is, 175 uint32_t initiator_task_tag); 176 static struct iscsi_outstanding *iscsi_outstanding_add(struct iscsi_session *is, 177 struct icl_pdu *request, union ccb *ccb, 178 uint32_t *initiator_task_tagp); 179 static void iscsi_outstanding_remove(struct iscsi_session *is, 180 struct iscsi_outstanding *io); 181 182 static bool 183 iscsi_pdu_prepare(struct icl_pdu *request) 184 { 185 struct iscsi_session *is; 186 struct iscsi_bhs_scsi_command *bhssc; 187 188 is = PDU_SESSION(request); 189 190 ISCSI_SESSION_LOCK_ASSERT(is); 191 192 /* 193 * We're only using fields common for all the request 194 * (initiator -> target) PDUs. 195 */ 196 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 197 198 /* 199 * Data-Out PDU does not contain CmdSN. 200 */ 201 if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) { 202 if (ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn) && 203 (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) { 204 /* 205 * Current MaxCmdSN prevents us from sending any more 206 * SCSI Command PDUs to the target; postpone the PDU. 207 * It will get resent by either iscsi_pdu_queue(), 208 * or by maintenance thread. 209 */ 210 #if 0 211 ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %u, " 212 "ExpCmdSN %u, MaxCmdSN %u, opcode 0x%x", 213 is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn, 214 bhssc->bhssc_opcode); 215 #endif 216 return (true); 217 } 218 bhssc->bhssc_cmdsn = htonl(is->is_cmdsn); 219 if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) 220 is->is_cmdsn++; 221 } 222 bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1); 223 224 return (false); 225 } 226 227 static void 228 iscsi_session_send_postponed(struct iscsi_session *is) 229 { 230 struct icl_pdu *request; 231 bool postpone; 232 233 ISCSI_SESSION_LOCK_ASSERT(is); 234 235 if (STAILQ_EMPTY(&is->is_postponed)) 236 return; 237 while ((request = STAILQ_FIRST(&is->is_postponed)) != NULL) { 238 postpone = iscsi_pdu_prepare(request); 239 if (postpone) 240 return; 241 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next); 242 icl_pdu_queue(request); 243 } 244 xpt_release_simq(is->is_sim, 1); 245 } 246 247 static void 248 iscsi_pdu_queue_locked(struct icl_pdu *request) 249 { 250 struct iscsi_session *is; 251 bool postpone; 252 253 is = PDU_SESSION(request); 254 ISCSI_SESSION_LOCK_ASSERT(is); 255 iscsi_session_send_postponed(is); 256 postpone = iscsi_pdu_prepare(request); 257 if (postpone) { 258 if (STAILQ_EMPTY(&is->is_postponed)) 259 xpt_freeze_simq(is->is_sim, 1); 260 STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next); 261 return; 262 } 263 icl_pdu_queue(request); 264 } 265 266 static void 267 iscsi_pdu_queue(struct icl_pdu *request) 268 { 269 struct iscsi_session *is; 270 271 is = PDU_SESSION(request); 272 ISCSI_SESSION_LOCK(is); 273 iscsi_pdu_queue_locked(request); 274 ISCSI_SESSION_UNLOCK(is); 275 } 276 277 static void 278 iscsi_session_logout(struct iscsi_session *is) 279 { 280 struct icl_pdu *request; 281 struct iscsi_bhs_logout_request *bhslr; 282 283 request = icl_pdu_new(is->is_conn, M_NOWAIT); 284 if (request == NULL) 285 return; 286 287 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs; 288 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST; 289 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION; 290 iscsi_pdu_queue_locked(request); 291 } 292 293 static void 294 iscsi_session_terminate_task(struct iscsi_session *is, 295 struct iscsi_outstanding *io, cam_status status) 296 { 297 298 ISCSI_SESSION_LOCK_ASSERT(is); 299 300 if (io->io_ccb != NULL) { 301 io->io_ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK); 302 io->io_ccb->ccb_h.status |= status; 303 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 304 io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN; 305 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 306 ISCSI_SESSION_DEBUG(is, "freezing devq"); 307 } 308 xpt_done(io->io_ccb); 309 } 310 iscsi_outstanding_remove(is, io); 311 } 312 313 static void 314 iscsi_session_terminate_tasks(struct iscsi_session *is, cam_status status) 315 { 316 struct iscsi_outstanding *io, *tmp; 317 318 ISCSI_SESSION_LOCK_ASSERT(is); 319 320 TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) { 321 iscsi_session_terminate_task(is, io, status); 322 } 323 } 324 325 static void 326 iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim) 327 { 328 struct icl_pdu *pdu; 329 330 ISCSI_SESSION_LOCK_ASSERT(is); 331 332 /* 333 * Don't queue any new PDUs. 334 */ 335 if (is->is_sim != NULL && is->is_simq_frozen == false) { 336 ISCSI_SESSION_DEBUG(is, "freezing"); 337 xpt_freeze_simq(is->is_sim, 1); 338 is->is_simq_frozen = true; 339 } 340 341 /* 342 * Remove postponed PDUs. 343 */ 344 if (!STAILQ_EMPTY(&is->is_postponed)) 345 xpt_release_simq(is->is_sim, 1); 346 while ((pdu = STAILQ_FIRST(&is->is_postponed)) != NULL) { 347 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next); 348 icl_pdu_free(pdu); 349 } 350 351 if (destroy_sim == false) { 352 /* 353 * Terminate SCSI tasks, asking CAM to requeue them. 354 */ 355 iscsi_session_terminate_tasks(is, CAM_REQUEUE_REQ); 356 return; 357 } 358 359 iscsi_session_terminate_tasks(is, CAM_DEV_NOT_THERE); 360 361 if (is->is_sim == NULL) 362 return; 363 364 ISCSI_SESSION_DEBUG(is, "deregistering SIM"); 365 xpt_async(AC_LOST_DEVICE, is->is_path, NULL); 366 367 if (is->is_simq_frozen) { 368 is->is_simq_frozen = false; 369 xpt_release_simq(is->is_sim, 1); 370 } 371 372 xpt_free_path(is->is_path); 373 is->is_path = NULL; 374 xpt_bus_deregister(cam_sim_path(is->is_sim)); 375 cam_sim_free(is->is_sim, TRUE /*free_devq*/); 376 is->is_sim = NULL; 377 is->is_devq = NULL; 378 } 379 380 static void 381 iscsi_maintenance_thread_reconnect(struct iscsi_session *is) 382 { 383 384 icl_conn_close(is->is_conn); 385 386 ISCSI_SESSION_LOCK(is); 387 388 is->is_connected = false; 389 is->is_reconnecting = false; 390 is->is_login_phase = false; 391 392 #ifdef ICL_KERNEL_PROXY 393 if (is->is_login_pdu != NULL) { 394 icl_pdu_free(is->is_login_pdu); 395 is->is_login_pdu = NULL; 396 } 397 cv_signal(&is->is_login_cv); 398 #endif 399 400 if (fail_on_disconnection) { 401 ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices"); 402 iscsi_session_cleanup(is, true); 403 } else { 404 iscsi_session_cleanup(is, false); 405 } 406 407 KASSERT(TAILQ_EMPTY(&is->is_outstanding), 408 ("destroying session with active tasks")); 409 KASSERT(STAILQ_EMPTY(&is->is_postponed), 410 ("destroying session with postponed PDUs")); 411 412 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) { 413 ISCSI_SESSION_UNLOCK(is); 414 return; 415 } 416 417 /* 418 * Request immediate reconnection from iscsid(8). 419 */ 420 //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)"); 421 is->is_waiting_for_iscsid = true; 422 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason)); 423 is->is_timeout = 0; 424 ISCSI_SESSION_UNLOCK(is); 425 cv_signal(&is->is_softc->sc_cv); 426 } 427 428 static void 429 iscsi_maintenance_thread_terminate(struct iscsi_session *is) 430 { 431 struct iscsi_softc *sc; 432 433 sc = is->is_softc; 434 sx_xlock(&sc->sc_lock); 435 TAILQ_REMOVE(&sc->sc_sessions, is, is_next); 436 sx_xunlock(&sc->sc_lock); 437 438 icl_conn_close(is->is_conn); 439 callout_drain(&is->is_callout); 440 441 ISCSI_SESSION_LOCK(is); 442 443 KASSERT(is->is_terminating, ("is_terminating == false")); 444 445 #ifdef ICL_KERNEL_PROXY 446 if (is->is_login_pdu != NULL) { 447 icl_pdu_free(is->is_login_pdu); 448 is->is_login_pdu = NULL; 449 } 450 cv_signal(&is->is_login_cv); 451 #endif 452 453 iscsi_session_cleanup(is, true); 454 455 KASSERT(TAILQ_EMPTY(&is->is_outstanding), 456 ("destroying session with active tasks")); 457 KASSERT(STAILQ_EMPTY(&is->is_postponed), 458 ("destroying session with postponed PDUs")); 459 460 ISCSI_SESSION_UNLOCK(is); 461 462 icl_conn_free(is->is_conn); 463 mtx_destroy(&is->is_lock); 464 cv_destroy(&is->is_maintenance_cv); 465 #ifdef ICL_KERNEL_PROXY 466 cv_destroy(&is->is_login_cv); 467 #endif 468 469 ISCSI_SESSION_DEBUG(is, "terminated"); 470 free(is, M_ISCSI); 471 472 /* 473 * The iscsi_unload() routine might be waiting. 474 */ 475 cv_signal(&sc->sc_cv); 476 } 477 478 static void 479 iscsi_maintenance_thread(void *arg) 480 { 481 struct iscsi_session *is = arg; 482 483 ISCSI_SESSION_LOCK(is); 484 for (;;) { 485 if (is->is_reconnecting == false && 486 is->is_terminating == false && 487 (STAILQ_EMPTY(&is->is_postponed) || 488 ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn))) 489 cv_wait(&is->is_maintenance_cv, &is->is_lock); 490 491 /* Terminate supersedes reconnect. */ 492 if (is->is_terminating) { 493 ISCSI_SESSION_UNLOCK(is); 494 iscsi_maintenance_thread_terminate(is); 495 kthread_exit(); 496 return; 497 } 498 499 if (is->is_reconnecting) { 500 ISCSI_SESSION_UNLOCK(is); 501 iscsi_maintenance_thread_reconnect(is); 502 ISCSI_SESSION_LOCK(is); 503 continue; 504 } 505 506 iscsi_session_send_postponed(is); 507 } 508 ISCSI_SESSION_UNLOCK(is); 509 } 510 511 static void 512 iscsi_session_reconnect(struct iscsi_session *is) 513 { 514 515 /* 516 * XXX: We can't use locking here, because 517 * it's being called from various contexts. 518 * Hope it doesn't break anything. 519 */ 520 if (is->is_reconnecting) 521 return; 522 523 is->is_reconnecting = true; 524 cv_signal(&is->is_maintenance_cv); 525 } 526 527 static void 528 iscsi_session_terminate(struct iscsi_session *is) 529 { 530 531 if (is->is_terminating) 532 return; 533 534 is->is_terminating = true; 535 536 #if 0 537 iscsi_session_logout(is); 538 #endif 539 cv_signal(&is->is_maintenance_cv); 540 } 541 542 static void 543 iscsi_callout(void *context) 544 { 545 struct icl_pdu *request; 546 struct iscsi_bhs_nop_out *bhsno; 547 struct iscsi_session *is; 548 bool reconnect_needed = false; 549 550 is = context; 551 552 ISCSI_SESSION_LOCK(is); 553 if (is->is_terminating) { 554 ISCSI_SESSION_UNLOCK(is); 555 return; 556 } 557 558 callout_schedule(&is->is_callout, 1 * hz); 559 560 if (is->is_conf.isc_enable == 0) 561 goto out; 562 563 is->is_timeout++; 564 565 if (is->is_waiting_for_iscsid) { 566 if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) { 567 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) " 568 "for %d seconds; reconnecting", 569 is->is_timeout); 570 reconnect_needed = true; 571 } 572 goto out; 573 } 574 575 if (is->is_login_phase) { 576 if (login_timeout > 0 && is->is_timeout > login_timeout) { 577 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; " 578 "reconnecting", is->is_timeout); 579 reconnect_needed = true; 580 } 581 goto out; 582 } 583 584 if (ping_timeout <= 0) { 585 /* 586 * Pings are disabled. Don't send NOP-Out in this case. 587 * Reset the timeout, to avoid triggering reconnection, 588 * should the user decide to reenable them. 589 */ 590 is->is_timeout = 0; 591 goto out; 592 } 593 594 if (is->is_timeout >= ping_timeout) { 595 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; " 596 "reconnecting", ping_timeout); 597 reconnect_needed = true; 598 goto out; 599 } 600 601 ISCSI_SESSION_UNLOCK(is); 602 603 /* 604 * If the ping was reset less than one second ago - which means 605 * that we've received some PDU during the last second - assume 606 * the traffic flows correctly and don't bother sending a NOP-Out. 607 * 608 * (It's 2 - one for one second, and one for incrementing is_timeout 609 * earlier in this routine.) 610 */ 611 if (is->is_timeout < 2) 612 return; 613 614 request = icl_pdu_new(is->is_conn, M_NOWAIT); 615 if (request == NULL) { 616 ISCSI_SESSION_WARN(is, "failed to allocate PDU"); 617 return; 618 } 619 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs; 620 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT | 621 ISCSI_BHS_OPCODE_IMMEDIATE; 622 bhsno->bhsno_flags = 0x80; 623 bhsno->bhsno_target_transfer_tag = 0xffffffff; 624 iscsi_pdu_queue(request); 625 return; 626 627 out: 628 if (is->is_terminating) { 629 ISCSI_SESSION_UNLOCK(is); 630 return; 631 } 632 633 ISCSI_SESSION_UNLOCK(is); 634 635 if (reconnect_needed) 636 iscsi_session_reconnect(is); 637 } 638 639 static void 640 iscsi_pdu_update_statsn(const struct icl_pdu *response) 641 { 642 const struct iscsi_bhs_data_in *bhsdi; 643 struct iscsi_session *is; 644 uint32_t expcmdsn, maxcmdsn, statsn; 645 646 is = PDU_SESSION(response); 647 648 ISCSI_SESSION_LOCK_ASSERT(is); 649 650 /* 651 * We're only using fields common for all the response 652 * (target -> initiator) PDUs. 653 */ 654 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs; 655 /* 656 * Ok, I lied. In case of Data-In, "The fields StatSN, Status, 657 * and Residual Count only have meaningful content if the S bit 658 * is set to 1", so we also need to check the bit specific for 659 * Data-In PDU. 660 */ 661 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN || 662 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) { 663 statsn = ntohl(bhsdi->bhsdi_statsn); 664 if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) { 665 /* XXX: This is normal situation for MCS */ 666 ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != " 667 "session ExpStatSN %u (or + 1); reconnecting", 668 bhsdi->bhsdi_opcode, statsn, is->is_statsn); 669 iscsi_session_reconnect(is); 670 } 671 if (ISCSI_SNGT(statsn, is->is_statsn)) 672 is->is_statsn = statsn; 673 } 674 675 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn); 676 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn); 677 678 if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) { 679 ISCSI_SESSION_DEBUG(is, 680 "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring", 681 maxcmdsn, expcmdsn); 682 } else { 683 if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) { 684 is->is_maxcmdsn = maxcmdsn; 685 686 /* 687 * Command window increased; kick the maintanance thread 688 * to send out postponed commands. 689 */ 690 if (!STAILQ_EMPTY(&is->is_postponed)) 691 cv_signal(&is->is_maintenance_cv); 692 } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) { 693 /* XXX: This is normal situation for MCS */ 694 ISCSI_SESSION_DEBUG(is, 695 "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring", 696 maxcmdsn, is->is_maxcmdsn); 697 } 698 699 if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) { 700 is->is_expcmdsn = expcmdsn; 701 } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) { 702 /* XXX: This is normal situation for MCS */ 703 ISCSI_SESSION_DEBUG(is, 704 "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring", 705 expcmdsn, is->is_expcmdsn); 706 } 707 } 708 709 /* 710 * Every incoming PDU - not just NOP-In - resets the ping timer. 711 * The purpose of the timeout is to reset the connection when it stalls; 712 * we don't want this to happen when NOP-In or NOP-Out ends up delayed 713 * in some queue. 714 */ 715 is->is_timeout = 0; 716 } 717 718 static void 719 iscsi_receive_callback(struct icl_pdu *response) 720 { 721 struct iscsi_session *is; 722 723 is = PDU_SESSION(response); 724 725 ISCSI_SESSION_LOCK(is); 726 727 iscsi_pdu_update_statsn(response); 728 729 #ifdef ICL_KERNEL_PROXY 730 if (is->is_login_phase) { 731 if (is->is_login_pdu == NULL) 732 is->is_login_pdu = response; 733 else 734 icl_pdu_free(response); 735 ISCSI_SESSION_UNLOCK(is); 736 cv_signal(&is->is_login_cv); 737 return; 738 } 739 #endif 740 741 /* 742 * The handling routine is responsible for freeing the PDU 743 * when it's no longer needed. 744 */ 745 switch (response->ip_bhs->bhs_opcode) { 746 case ISCSI_BHS_OPCODE_NOP_IN: 747 iscsi_pdu_handle_nop_in(response); 748 ISCSI_SESSION_UNLOCK(is); 749 break; 750 case ISCSI_BHS_OPCODE_SCSI_RESPONSE: 751 iscsi_pdu_handle_scsi_response(response); 752 /* Session lock dropped inside. */ 753 ISCSI_SESSION_LOCK_ASSERT_NOT(is); 754 break; 755 case ISCSI_BHS_OPCODE_TASK_RESPONSE: 756 iscsi_pdu_handle_task_response(response); 757 ISCSI_SESSION_UNLOCK(is); 758 break; 759 case ISCSI_BHS_OPCODE_SCSI_DATA_IN: 760 iscsi_pdu_handle_data_in(response); 761 /* Session lock dropped inside. */ 762 ISCSI_SESSION_LOCK_ASSERT_NOT(is); 763 break; 764 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE: 765 iscsi_pdu_handle_logout_response(response); 766 ISCSI_SESSION_UNLOCK(is); 767 break; 768 case ISCSI_BHS_OPCODE_R2T: 769 iscsi_pdu_handle_r2t(response); 770 ISCSI_SESSION_UNLOCK(is); 771 break; 772 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE: 773 iscsi_pdu_handle_async_message(response); 774 ISCSI_SESSION_UNLOCK(is); 775 break; 776 case ISCSI_BHS_OPCODE_REJECT: 777 iscsi_pdu_handle_reject(response); 778 ISCSI_SESSION_UNLOCK(is); 779 break; 780 default: 781 ISCSI_SESSION_WARN(is, "received PDU with unsupported " 782 "opcode 0x%x; reconnecting", 783 response->ip_bhs->bhs_opcode); 784 iscsi_session_reconnect(is); 785 ISCSI_SESSION_UNLOCK(is); 786 icl_pdu_free(response); 787 } 788 } 789 790 static void 791 iscsi_error_callback(struct icl_conn *ic) 792 { 793 struct iscsi_session *is; 794 795 is = CONN_SESSION(ic); 796 797 ISCSI_SESSION_WARN(is, "connection error; reconnecting"); 798 iscsi_session_reconnect(is); 799 } 800 801 static void 802 iscsi_pdu_handle_nop_in(struct icl_pdu *response) 803 { 804 struct iscsi_session *is; 805 struct iscsi_bhs_nop_out *bhsno; 806 struct iscsi_bhs_nop_in *bhsni; 807 struct icl_pdu *request; 808 void *data = NULL; 809 size_t datasize; 810 int error; 811 812 is = PDU_SESSION(response); 813 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs; 814 815 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) { 816 /* 817 * Nothing to do; iscsi_pdu_update_statsn() already 818 * zeroed the timeout. 819 */ 820 icl_pdu_free(response); 821 return; 822 } 823 824 datasize = icl_pdu_data_segment_length(response); 825 if (datasize > 0) { 826 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO); 827 if (data == NULL) { 828 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 829 "reconnecting"); 830 icl_pdu_free(response); 831 iscsi_session_reconnect(is); 832 return; 833 } 834 icl_pdu_get_data(response, 0, data, datasize); 835 } 836 837 request = icl_pdu_new(response->ip_conn, M_NOWAIT); 838 if (request == NULL) { 839 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 840 "reconnecting"); 841 free(data, M_ISCSI); 842 icl_pdu_free(response); 843 iscsi_session_reconnect(is); 844 return; 845 } 846 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs; 847 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT | 848 ISCSI_BHS_OPCODE_IMMEDIATE; 849 bhsno->bhsno_flags = 0x80; 850 bhsno->bhsno_initiator_task_tag = 0xffffffff; 851 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag; 852 if (datasize > 0) { 853 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT); 854 if (error != 0) { 855 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 856 "reconnecting"); 857 free(data, M_ISCSI); 858 icl_pdu_free(request); 859 icl_pdu_free(response); 860 iscsi_session_reconnect(is); 861 return; 862 } 863 free(data, M_ISCSI); 864 } 865 866 icl_pdu_free(response); 867 iscsi_pdu_queue_locked(request); 868 } 869 870 static void 871 iscsi_pdu_handle_scsi_response(struct icl_pdu *response) 872 { 873 struct iscsi_bhs_scsi_response *bhssr; 874 struct iscsi_outstanding *io; 875 struct iscsi_session *is; 876 union ccb *ccb; 877 struct ccb_scsiio *csio; 878 size_t data_segment_len, received; 879 uint16_t sense_len; 880 uint32_t resid; 881 882 is = PDU_SESSION(response); 883 884 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 885 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag); 886 if (io == NULL || io->io_ccb == NULL) { 887 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag); 888 icl_pdu_free(response); 889 iscsi_session_reconnect(is); 890 ISCSI_SESSION_UNLOCK(is); 891 return; 892 } 893 894 ccb = io->io_ccb; 895 896 /* 897 * With iSER, after getting good response we can be sure 898 * that all the data has been successfully transferred. 899 */ 900 if (is->is_conn->ic_iser) { 901 resid = ntohl(bhssr->bhssr_residual_count); 902 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) { 903 io->io_received = ccb->csio.dxfer_len - resid; 904 } else if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) { 905 ISCSI_SESSION_WARN(is, "overflow: target indicates %d", resid); 906 } else { 907 io->io_received = ccb->csio.dxfer_len; 908 } 909 } 910 911 received = io->io_received; 912 iscsi_outstanding_remove(is, io); 913 ISCSI_SESSION_UNLOCK(is); 914 915 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) { 916 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response); 917 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 918 xpt_freeze_devq(ccb->ccb_h.path, 1); 919 ISCSI_SESSION_DEBUG(is, "freezing devq"); 920 } 921 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN; 922 } else if (bhssr->bhssr_status == 0) { 923 ccb->ccb_h.status = CAM_REQ_CMP; 924 } else { 925 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 926 xpt_freeze_devq(ccb->ccb_h.path, 1); 927 ISCSI_SESSION_DEBUG(is, "freezing devq"); 928 } 929 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN; 930 ccb->csio.scsi_status = bhssr->bhssr_status; 931 } 932 933 csio = &ccb->csio; 934 data_segment_len = icl_pdu_data_segment_length(response); 935 if (data_segment_len > 0) { 936 if (data_segment_len < sizeof(sense_len)) { 937 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)", 938 data_segment_len); 939 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 940 xpt_freeze_devq(ccb->ccb_h.path, 1); 941 ISCSI_SESSION_DEBUG(is, "freezing devq"); 942 } 943 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN; 944 goto out; 945 } 946 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len)); 947 sense_len = ntohs(sense_len); 948 #if 0 949 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd", 950 sense_len, data_segment_len); 951 #endif 952 if (sizeof(sense_len) + sense_len > data_segment_len) { 953 ISCSI_SESSION_WARN(is, "truncated data segment " 954 "(%zd bytes, should be %zd)", 955 data_segment_len, sizeof(sense_len) + sense_len); 956 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 957 xpt_freeze_devq(ccb->ccb_h.path, 1); 958 ISCSI_SESSION_DEBUG(is, "freezing devq"); 959 } 960 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN; 961 goto out; 962 } else if (sizeof(sense_len) + sense_len < data_segment_len) 963 ISCSI_SESSION_WARN(is, "oversize data segment " 964 "(%zd bytes, should be %zd)", 965 data_segment_len, sizeof(sense_len) + sense_len); 966 if (sense_len > csio->sense_len) { 967 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d", 968 sense_len, csio->sense_len); 969 sense_len = csio->sense_len; 970 } 971 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len); 972 csio->sense_resid = csio->sense_len - sense_len; 973 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 974 } 975 976 out: 977 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) 978 csio->resid = ntohl(bhssr->bhssr_residual_count); 979 980 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 981 KASSERT(received <= csio->dxfer_len, 982 ("received > csio->dxfer_len")); 983 if (received < csio->dxfer_len) { 984 if (csio->resid != csio->dxfer_len - received) { 985 ISCSI_SESSION_WARN(is, "underflow mismatch: " 986 "target indicates %d, we calculated %zd", 987 csio->resid, csio->dxfer_len - received); 988 } 989 csio->resid = csio->dxfer_len - received; 990 } 991 } 992 993 xpt_done(ccb); 994 icl_pdu_free(response); 995 } 996 997 static void 998 iscsi_pdu_handle_task_response(struct icl_pdu *response) 999 { 1000 struct iscsi_bhs_task_management_response *bhstmr; 1001 struct iscsi_outstanding *io, *aio; 1002 struct iscsi_session *is; 1003 1004 is = PDU_SESSION(response); 1005 1006 bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs; 1007 io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag); 1008 if (io == NULL || io->io_ccb != NULL) { 1009 ISCSI_SESSION_WARN(is, "bad itt 0x%x", 1010 bhstmr->bhstmr_initiator_task_tag); 1011 icl_pdu_free(response); 1012 iscsi_session_reconnect(is); 1013 return; 1014 } 1015 1016 if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) { 1017 ISCSI_SESSION_WARN(is, "task response 0x%x", 1018 bhstmr->bhstmr_response); 1019 } else { 1020 aio = iscsi_outstanding_find(is, io->io_referenced_task_tag); 1021 if (aio != NULL && aio->io_ccb != NULL) 1022 iscsi_session_terminate_task(is, aio, CAM_REQ_ABORTED); 1023 } 1024 1025 iscsi_outstanding_remove(is, io); 1026 icl_pdu_free(response); 1027 } 1028 1029 static void 1030 iscsi_pdu_handle_data_in(struct icl_pdu *response) 1031 { 1032 struct iscsi_bhs_data_in *bhsdi; 1033 struct iscsi_outstanding *io; 1034 struct iscsi_session *is; 1035 union ccb *ccb; 1036 struct ccb_scsiio *csio; 1037 size_t data_segment_len, received, oreceived; 1038 1039 is = PDU_SESSION(response); 1040 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs; 1041 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag); 1042 if (io == NULL || io->io_ccb == NULL) { 1043 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag); 1044 icl_pdu_free(response); 1045 iscsi_session_reconnect(is); 1046 ISCSI_SESSION_UNLOCK(is); 1047 return; 1048 } 1049 1050 data_segment_len = icl_pdu_data_segment_length(response); 1051 if (data_segment_len == 0) { 1052 /* 1053 * "The sending of 0 length data segments should be avoided, 1054 * but initiators and targets MUST be able to properly receive 1055 * 0 length data segments." 1056 */ 1057 ISCSI_SESSION_UNLOCK(is); 1058 icl_pdu_free(response); 1059 return; 1060 } 1061 1062 /* 1063 * We need to track this for security reasons - without it, malicious target 1064 * could respond to SCSI READ without sending Data-In PDUs, which would result 1065 * in read operation on the initiator side returning random kernel data. 1066 */ 1067 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) { 1068 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd", 1069 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset)); 1070 icl_pdu_free(response); 1071 iscsi_session_reconnect(is); 1072 ISCSI_SESSION_UNLOCK(is); 1073 return; 1074 } 1075 1076 ccb = io->io_ccb; 1077 csio = &ccb->csio; 1078 1079 if (io->io_received + data_segment_len > csio->dxfer_len) { 1080 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes " 1081 "at offset %zd, buffer is %d)", 1082 data_segment_len, io->io_received, csio->dxfer_len); 1083 icl_pdu_free(response); 1084 iscsi_session_reconnect(is); 1085 ISCSI_SESSION_UNLOCK(is); 1086 return; 1087 } 1088 1089 oreceived = io->io_received; 1090 io->io_received += data_segment_len; 1091 received = io->io_received; 1092 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) 1093 iscsi_outstanding_remove(is, io); 1094 ISCSI_SESSION_UNLOCK(is); 1095 1096 icl_pdu_get_data(response, 0, csio->data_ptr + oreceived, data_segment_len); 1097 1098 /* 1099 * XXX: Check DataSN. 1100 * XXX: Check F. 1101 */ 1102 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) { 1103 /* 1104 * Nothing more to do. 1105 */ 1106 icl_pdu_free(response); 1107 return; 1108 } 1109 1110 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status); 1111 if (bhsdi->bhsdi_status == 0) { 1112 ccb->ccb_h.status = CAM_REQ_CMP; 1113 } else { 1114 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 1115 xpt_freeze_devq(ccb->ccb_h.path, 1); 1116 ISCSI_SESSION_DEBUG(is, "freezing devq"); 1117 } 1118 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN; 1119 csio->scsi_status = bhsdi->bhsdi_status; 1120 } 1121 1122 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1123 KASSERT(received <= csio->dxfer_len, 1124 ("received > csio->dxfer_len")); 1125 if (received < csio->dxfer_len) { 1126 csio->resid = ntohl(bhsdi->bhsdi_residual_count); 1127 if (csio->resid != csio->dxfer_len - received) { 1128 ISCSI_SESSION_WARN(is, "underflow mismatch: " 1129 "target indicates %d, we calculated %zd", 1130 csio->resid, csio->dxfer_len - received); 1131 } 1132 csio->resid = csio->dxfer_len - received; 1133 } 1134 } 1135 1136 xpt_done(ccb); 1137 icl_pdu_free(response); 1138 } 1139 1140 static void 1141 iscsi_pdu_handle_logout_response(struct icl_pdu *response) 1142 { 1143 1144 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response"); 1145 icl_pdu_free(response); 1146 } 1147 1148 static void 1149 iscsi_pdu_handle_r2t(struct icl_pdu *response) 1150 { 1151 struct icl_pdu *request; 1152 struct iscsi_session *is; 1153 struct iscsi_bhs_r2t *bhsr2t; 1154 struct iscsi_bhs_data_out *bhsdo; 1155 struct iscsi_outstanding *io; 1156 struct ccb_scsiio *csio; 1157 size_t off, len, max_send_data_segment_length, total_len; 1158 int error; 1159 uint32_t datasn = 0; 1160 1161 is = PDU_SESSION(response); 1162 1163 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs; 1164 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag); 1165 if (io == NULL || io->io_ccb == NULL) { 1166 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting", 1167 bhsr2t->bhsr2t_initiator_task_tag); 1168 icl_pdu_free(response); 1169 iscsi_session_reconnect(is); 1170 return; 1171 } 1172 1173 csio = &io->io_ccb->csio; 1174 1175 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) { 1176 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting"); 1177 icl_pdu_free(response); 1178 iscsi_session_reconnect(is); 1179 return; 1180 } 1181 1182 /* 1183 * XXX: Verify R2TSN. 1184 */ 1185 1186 off = ntohl(bhsr2t->bhsr2t_buffer_offset); 1187 if (off > csio->dxfer_len) { 1188 ISCSI_SESSION_WARN(is, "target requested invalid offset " 1189 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len); 1190 icl_pdu_free(response); 1191 iscsi_session_reconnect(is); 1192 return; 1193 } 1194 1195 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length); 1196 if (total_len == 0 || total_len > csio->dxfer_len) { 1197 ISCSI_SESSION_WARN(is, "target requested invalid length " 1198 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len); 1199 icl_pdu_free(response); 1200 iscsi_session_reconnect(is); 1201 return; 1202 } 1203 1204 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len); 1205 1206 if (is->is_conn->ic_hw_isomax != 0) 1207 max_send_data_segment_length = is->is_conn->ic_hw_isomax; 1208 else 1209 max_send_data_segment_length = 1210 is->is_conn->ic_max_send_data_segment_length; 1211 for (;;) { 1212 len = total_len; 1213 1214 if (len > max_send_data_segment_length) 1215 len = max_send_data_segment_length; 1216 1217 if (off + len > csio->dxfer_len) { 1218 ISCSI_SESSION_WARN(is, "target requested invalid " 1219 "length/offset %zd, buffer is %d; reconnecting", 1220 off + len, csio->dxfer_len); 1221 icl_pdu_free(response); 1222 iscsi_session_reconnect(is); 1223 return; 1224 } 1225 1226 request = icl_pdu_new(response->ip_conn, M_NOWAIT); 1227 if (request == NULL) { 1228 icl_pdu_free(response); 1229 iscsi_session_reconnect(is); 1230 return; 1231 } 1232 1233 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs; 1234 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT; 1235 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun; 1236 bhsdo->bhsdo_initiator_task_tag = 1237 bhsr2t->bhsr2t_initiator_task_tag; 1238 bhsdo->bhsdo_target_transfer_tag = 1239 bhsr2t->bhsr2t_target_transfer_tag; 1240 bhsdo->bhsdo_datasn = htonl(datasn); 1241 bhsdo->bhsdo_buffer_offset = htonl(off); 1242 error = icl_pdu_append_data(request, csio->data_ptr + off, len, 1243 M_NOWAIT); 1244 if (error != 0) { 1245 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 1246 "reconnecting"); 1247 icl_pdu_free(request); 1248 icl_pdu_free(response); 1249 iscsi_session_reconnect(is); 1250 return; 1251 } 1252 1253 datasn += howmany(len, 1254 is->is_conn->ic_max_send_data_segment_length); 1255 off += len; 1256 total_len -= len; 1257 1258 if (total_len == 0) { 1259 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F; 1260 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off); 1261 } else { 1262 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off); 1263 } 1264 1265 iscsi_pdu_queue_locked(request); 1266 1267 if (total_len == 0) 1268 break; 1269 } 1270 1271 icl_pdu_free(response); 1272 } 1273 1274 static void 1275 iscsi_pdu_handle_async_message(struct icl_pdu *response) 1276 { 1277 struct iscsi_bhs_asynchronous_message *bhsam; 1278 struct iscsi_session *is; 1279 1280 is = PDU_SESSION(response); 1281 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs; 1282 switch (bhsam->bhsam_async_event) { 1283 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT: 1284 ISCSI_SESSION_WARN(is, "target requests logout; removing session"); 1285 iscsi_session_logout(is); 1286 iscsi_session_terminate(is); 1287 break; 1288 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION: 1289 ISCSI_SESSION_WARN(is, "target indicates it will drop the connection"); 1290 break; 1291 case BHSAM_EVENT_TARGET_TERMINATES_SESSION: 1292 ISCSI_SESSION_WARN(is, "target indicates it will drop the session"); 1293 break; 1294 default: 1295 /* 1296 * XXX: Technically, we're obligated to also handle 1297 * parameter renegotiation. 1298 */ 1299 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event); 1300 break; 1301 } 1302 1303 icl_pdu_free(response); 1304 } 1305 1306 static void 1307 iscsi_pdu_handle_reject(struct icl_pdu *response) 1308 { 1309 struct iscsi_bhs_reject *bhsr; 1310 struct iscsi_session *is; 1311 1312 is = PDU_SESSION(response); 1313 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs; 1314 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?", 1315 bhsr->bhsr_reason); 1316 1317 icl_pdu_free(response); 1318 } 1319 1320 static int 1321 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc, 1322 struct iscsi_daemon_request *request) 1323 { 1324 struct iscsi_session *is; 1325 struct icl_drv_limits idl; 1326 int error; 1327 1328 sx_slock(&sc->sc_lock); 1329 for (;;) { 1330 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1331 ISCSI_SESSION_LOCK(is); 1332 if (is->is_conf.isc_enable == 0 && 1333 is->is_conf.isc_discovery == 0) { 1334 ISCSI_SESSION_UNLOCK(is); 1335 continue; 1336 } 1337 if (is->is_waiting_for_iscsid) 1338 break; 1339 ISCSI_SESSION_UNLOCK(is); 1340 } 1341 1342 if (is == NULL) { 1343 if (sc->sc_unloading) { 1344 sx_sunlock(&sc->sc_lock); 1345 return (ENXIO); 1346 } 1347 1348 /* 1349 * No session requires attention from iscsid(8); wait. 1350 */ 1351 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock); 1352 if (error != 0) { 1353 sx_sunlock(&sc->sc_lock); 1354 return (error); 1355 } 1356 continue; 1357 } 1358 1359 is->is_waiting_for_iscsid = false; 1360 is->is_login_phase = true; 1361 is->is_reason[0] = '\0'; 1362 ISCSI_SESSION_UNLOCK(is); 1363 1364 request->idr_session_id = is->is_id; 1365 memcpy(&request->idr_isid, &is->is_isid, 1366 sizeof(request->idr_isid)); 1367 request->idr_tsih = 0; /* New or reinstated session. */ 1368 memcpy(&request->idr_conf, &is->is_conf, 1369 sizeof(request->idr_conf)); 1370 1371 error = icl_limits(is->is_conf.isc_offload, 1372 is->is_conf.isc_iser, &idl); 1373 if (error != 0) { 1374 ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" " 1375 "failed with error %d", is->is_conf.isc_offload, 1376 error); 1377 sx_sunlock(&sc->sc_lock); 1378 return (error); 1379 } 1380 request->idr_limits.isl_max_recv_data_segment_length = 1381 idl.idl_max_recv_data_segment_length; 1382 request->idr_limits.isl_max_send_data_segment_length = 1383 idl.idl_max_send_data_segment_length; 1384 request->idr_limits.isl_max_burst_length = 1385 idl.idl_max_burst_length; 1386 request->idr_limits.isl_first_burst_length = 1387 idl.idl_first_burst_length; 1388 1389 sx_sunlock(&sc->sc_lock); 1390 return (0); 1391 } 1392 } 1393 1394 static int 1395 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc, 1396 struct iscsi_daemon_handoff *handoff) 1397 { 1398 struct iscsi_session *is; 1399 struct icl_conn *ic; 1400 int error; 1401 1402 sx_slock(&sc->sc_lock); 1403 1404 /* 1405 * Find the session to hand off socket to. 1406 */ 1407 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1408 if (is->is_id == handoff->idh_session_id) 1409 break; 1410 } 1411 if (is == NULL) { 1412 sx_sunlock(&sc->sc_lock); 1413 return (ESRCH); 1414 } 1415 ISCSI_SESSION_LOCK(is); 1416 ic = is->is_conn; 1417 if (is->is_conf.isc_discovery || is->is_terminating) { 1418 ISCSI_SESSION_UNLOCK(is); 1419 sx_sunlock(&sc->sc_lock); 1420 return (EINVAL); 1421 } 1422 if (is->is_connected) { 1423 /* 1424 * This might have happened because another iscsid(8) 1425 * instance handed off the connection in the meantime. 1426 * Just return. 1427 */ 1428 ISCSI_SESSION_WARN(is, "handoff on already connected " 1429 "session"); 1430 ISCSI_SESSION_UNLOCK(is); 1431 sx_sunlock(&sc->sc_lock); 1432 return (EBUSY); 1433 } 1434 1435 strlcpy(is->is_target_alias, handoff->idh_target_alias, 1436 sizeof(is->is_target_alias)); 1437 is->is_tsih = handoff->idh_tsih; 1438 is->is_statsn = handoff->idh_statsn; 1439 is->is_protocol_level = handoff->idh_protocol_level; 1440 is->is_initial_r2t = handoff->idh_initial_r2t; 1441 is->is_immediate_data = handoff->idh_immediate_data; 1442 1443 ic->ic_max_recv_data_segment_length = 1444 handoff->idh_max_recv_data_segment_length; 1445 ic->ic_max_send_data_segment_length = 1446 handoff->idh_max_send_data_segment_length; 1447 is->is_max_burst_length = handoff->idh_max_burst_length; 1448 is->is_first_burst_length = handoff->idh_first_burst_length; 1449 1450 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C) 1451 ic->ic_header_crc32c = true; 1452 else 1453 ic->ic_header_crc32c = false; 1454 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C) 1455 ic->ic_data_crc32c = true; 1456 else 1457 ic->ic_data_crc32c = false; 1458 ic->ic_maxtags = maxtags; 1459 1460 is->is_cmdsn = 0; 1461 is->is_expcmdsn = 0; 1462 is->is_maxcmdsn = 0; 1463 is->is_waiting_for_iscsid = false; 1464 is->is_login_phase = false; 1465 is->is_timeout = 0; 1466 is->is_connected = true; 1467 is->is_reason[0] = '\0'; 1468 1469 ISCSI_SESSION_UNLOCK(is); 1470 1471 /* 1472 * If we're going through the proxy, the idh_socket will be 0, 1473 * and the ICL module can simply ignore this call. It can also 1474 * use it to determine it's no longer in the Login phase. 1475 */ 1476 error = icl_conn_handoff(ic, handoff->idh_socket); 1477 if (error != 0) { 1478 sx_sunlock(&sc->sc_lock); 1479 iscsi_session_terminate(is); 1480 return (error); 1481 } 1482 1483 sx_sunlock(&sc->sc_lock); 1484 1485 if (is->is_sim != NULL) { 1486 /* 1487 * When reconnecting, there already is SIM allocated for the session. 1488 */ 1489 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq")); 1490 ISCSI_SESSION_LOCK(is); 1491 ISCSI_SESSION_DEBUG(is, "releasing"); 1492 is->is_simq_frozen = false; 1493 xpt_release_simq(is->is_sim, 1); 1494 ISCSI_SESSION_UNLOCK(is); 1495 1496 } else { 1497 ISCSI_SESSION_LOCK(is); 1498 is->is_devq = cam_simq_alloc(ic->ic_maxtags); 1499 if (is->is_devq == NULL) { 1500 ISCSI_SESSION_WARN(is, "failed to allocate simq"); 1501 iscsi_session_terminate(is); 1502 return (ENOMEM); 1503 } 1504 1505 is->is_sim = cam_sim_alloc(iscsi_action, NULL, "iscsi", 1506 is, is->is_id /* unit */, &is->is_lock, 1507 1, ic->ic_maxtags, is->is_devq); 1508 if (is->is_sim == NULL) { 1509 ISCSI_SESSION_UNLOCK(is); 1510 ISCSI_SESSION_WARN(is, "failed to allocate SIM"); 1511 cam_simq_free(is->is_devq); 1512 iscsi_session_terminate(is); 1513 return (ENOMEM); 1514 } 1515 1516 if (xpt_bus_register(is->is_sim, NULL, 0) != 0) { 1517 ISCSI_SESSION_UNLOCK(is); 1518 ISCSI_SESSION_WARN(is, "failed to register bus"); 1519 iscsi_session_terminate(is); 1520 return (ENOMEM); 1521 } 1522 1523 error = xpt_create_path(&is->is_path, /*periph*/NULL, 1524 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD, 1525 CAM_LUN_WILDCARD); 1526 if (error != CAM_REQ_CMP) { 1527 ISCSI_SESSION_UNLOCK(is); 1528 ISCSI_SESSION_WARN(is, "failed to create path"); 1529 iscsi_session_terminate(is); 1530 return (ENOMEM); 1531 } 1532 ISCSI_SESSION_UNLOCK(is); 1533 } 1534 1535 return (0); 1536 } 1537 1538 static int 1539 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc, 1540 struct iscsi_daemon_fail *fail) 1541 { 1542 struct iscsi_session *is; 1543 1544 sx_slock(&sc->sc_lock); 1545 1546 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1547 if (is->is_id == fail->idf_session_id) 1548 break; 1549 } 1550 if (is == NULL) { 1551 sx_sunlock(&sc->sc_lock); 1552 return (ESRCH); 1553 } 1554 ISCSI_SESSION_LOCK(is); 1555 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s", 1556 fail->idf_reason); 1557 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason)); 1558 //is->is_waiting_for_iscsid = false; 1559 //is->is_login_phase = true; 1560 //iscsi_session_reconnect(is); 1561 ISCSI_SESSION_UNLOCK(is); 1562 sx_sunlock(&sc->sc_lock); 1563 1564 return (0); 1565 } 1566 1567 #ifdef ICL_KERNEL_PROXY 1568 static int 1569 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc, 1570 struct iscsi_daemon_connect *idc) 1571 { 1572 struct iscsi_session *is; 1573 struct sockaddr *from_sa, *to_sa; 1574 int error; 1575 1576 sx_slock(&sc->sc_lock); 1577 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1578 if (is->is_id == idc->idc_session_id) 1579 break; 1580 } 1581 if (is == NULL) { 1582 sx_sunlock(&sc->sc_lock); 1583 return (ESRCH); 1584 } 1585 sx_sunlock(&sc->sc_lock); 1586 1587 if (idc->idc_from_addrlen > 0) { 1588 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen); 1589 if (error != 0) { 1590 ISCSI_SESSION_WARN(is, 1591 "getsockaddr failed with error %d", error); 1592 return (error); 1593 } 1594 } else { 1595 from_sa = NULL; 1596 } 1597 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen); 1598 if (error != 0) { 1599 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d", 1600 error); 1601 free(from_sa, M_SONAME); 1602 return (error); 1603 } 1604 1605 ISCSI_SESSION_LOCK(is); 1606 is->is_statsn = 0; 1607 is->is_cmdsn = 0; 1608 is->is_expcmdsn = 0; 1609 is->is_maxcmdsn = 0; 1610 is->is_waiting_for_iscsid = false; 1611 is->is_login_phase = true; 1612 is->is_timeout = 0; 1613 ISCSI_SESSION_UNLOCK(is); 1614 1615 error = icl_conn_connect(is->is_conn, idc->idc_domain, 1616 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa); 1617 free(from_sa, M_SONAME); 1618 free(to_sa, M_SONAME); 1619 1620 /* 1621 * Digests are always disabled during login phase. 1622 */ 1623 is->is_conn->ic_header_crc32c = false; 1624 is->is_conn->ic_data_crc32c = false; 1625 1626 return (error); 1627 } 1628 1629 static int 1630 iscsi_ioctl_daemon_send(struct iscsi_softc *sc, 1631 struct iscsi_daemon_send *ids) 1632 { 1633 struct iscsi_session *is; 1634 struct icl_pdu *ip; 1635 size_t datalen; 1636 void *data; 1637 int error; 1638 1639 sx_slock(&sc->sc_lock); 1640 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1641 if (is->is_id == ids->ids_session_id) 1642 break; 1643 } 1644 if (is == NULL) { 1645 sx_sunlock(&sc->sc_lock); 1646 return (ESRCH); 1647 } 1648 sx_sunlock(&sc->sc_lock); 1649 1650 if (is->is_login_phase == false) 1651 return (EBUSY); 1652 1653 if (is->is_terminating || is->is_reconnecting) 1654 return (EIO); 1655 1656 datalen = ids->ids_data_segment_len; 1657 if (datalen > is->is_conn->ic_max_send_data_segment_length) 1658 return (EINVAL); 1659 if (datalen > 0) { 1660 data = malloc(datalen, M_ISCSI, M_WAITOK); 1661 error = copyin(ids->ids_data_segment, data, datalen); 1662 if (error != 0) { 1663 free(data, M_ISCSI); 1664 return (error); 1665 } 1666 } 1667 1668 ip = icl_pdu_new(is->is_conn, M_WAITOK); 1669 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs)); 1670 if (datalen > 0) { 1671 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK); 1672 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed")); 1673 free(data, M_ISCSI); 1674 } 1675 iscsi_pdu_queue(ip); 1676 1677 return (0); 1678 } 1679 1680 static int 1681 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc, 1682 struct iscsi_daemon_receive *idr) 1683 { 1684 struct iscsi_session *is; 1685 struct icl_pdu *ip; 1686 void *data; 1687 int error; 1688 1689 sx_slock(&sc->sc_lock); 1690 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1691 if (is->is_id == idr->idr_session_id) 1692 break; 1693 } 1694 if (is == NULL) { 1695 sx_sunlock(&sc->sc_lock); 1696 return (ESRCH); 1697 } 1698 sx_sunlock(&sc->sc_lock); 1699 1700 if (is->is_login_phase == false) 1701 return (EBUSY); 1702 1703 ISCSI_SESSION_LOCK(is); 1704 while (is->is_login_pdu == NULL && 1705 is->is_terminating == false && 1706 is->is_reconnecting == false) { 1707 error = cv_wait_sig(&is->is_login_cv, &is->is_lock); 1708 if (error != 0) { 1709 ISCSI_SESSION_UNLOCK(is); 1710 return (error); 1711 } 1712 } 1713 if (is->is_terminating || is->is_reconnecting) { 1714 ISCSI_SESSION_UNLOCK(is); 1715 return (EIO); 1716 } 1717 ip = is->is_login_pdu; 1718 is->is_login_pdu = NULL; 1719 ISCSI_SESSION_UNLOCK(is); 1720 1721 if (ip->ip_data_len > idr->idr_data_segment_len) { 1722 icl_pdu_free(ip); 1723 return (EMSGSIZE); 1724 } 1725 1726 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs)); 1727 if (ip->ip_data_len > 0) { 1728 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK); 1729 icl_pdu_get_data(ip, 0, data, ip->ip_data_len); 1730 copyout(data, idr->idr_data_segment, ip->ip_data_len); 1731 free(data, M_ISCSI); 1732 } 1733 1734 icl_pdu_free(ip); 1735 1736 return (0); 1737 } 1738 #endif /* ICL_KERNEL_PROXY */ 1739 1740 static void 1741 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc) 1742 { 1743 /* 1744 * Just make sure all the fields are null-terminated. 1745 * 1746 * XXX: This is not particularly secure. We should 1747 * create our own conf and then copy in relevant 1748 * fields. 1749 */ 1750 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0'; 1751 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0'; 1752 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0'; 1753 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0'; 1754 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0'; 1755 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0'; 1756 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0'; 1757 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0'; 1758 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0'; 1759 } 1760 1761 static bool 1762 iscsi_valid_session_conf(const struct iscsi_session_conf *isc) 1763 { 1764 1765 if (isc->isc_initiator[0] == '\0') { 1766 ISCSI_DEBUG("empty isc_initiator"); 1767 return (false); 1768 } 1769 1770 if (isc->isc_target_addr[0] == '\0') { 1771 ISCSI_DEBUG("empty isc_target_addr"); 1772 return (false); 1773 } 1774 1775 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) { 1776 ISCSI_DEBUG("non-empty isc_target for discovery session"); 1777 return (false); 1778 } 1779 1780 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) { 1781 ISCSI_DEBUG("empty isc_target for non-discovery session"); 1782 return (false); 1783 } 1784 1785 return (true); 1786 } 1787 1788 static int 1789 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa) 1790 { 1791 struct iscsi_session *is; 1792 const struct iscsi_session *is2; 1793 int error; 1794 1795 iscsi_sanitize_session_conf(&isa->isa_conf); 1796 if (iscsi_valid_session_conf(&isa->isa_conf) == false) 1797 return (EINVAL); 1798 1799 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK); 1800 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf)); 1801 1802 sx_xlock(&sc->sc_lock); 1803 1804 /* 1805 * Prevent duplicates. 1806 */ 1807 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) { 1808 if (!!is->is_conf.isc_discovery != 1809 !!is2->is_conf.isc_discovery) 1810 continue; 1811 1812 if (strcmp(is->is_conf.isc_target_addr, 1813 is2->is_conf.isc_target_addr) != 0) 1814 continue; 1815 1816 if (is->is_conf.isc_discovery == 0 && 1817 strcmp(is->is_conf.isc_target, 1818 is2->is_conf.isc_target) != 0) 1819 continue; 1820 1821 sx_xunlock(&sc->sc_lock); 1822 free(is, M_ISCSI); 1823 return (EBUSY); 1824 } 1825 1826 is->is_conn = icl_new_conn(is->is_conf.isc_offload, 1827 is->is_conf.isc_iser, "iscsi", &is->is_lock); 1828 if (is->is_conn == NULL) { 1829 sx_xunlock(&sc->sc_lock); 1830 free(is, M_ISCSI); 1831 return (EINVAL); 1832 } 1833 is->is_conn->ic_receive = iscsi_receive_callback; 1834 is->is_conn->ic_error = iscsi_error_callback; 1835 is->is_conn->ic_prv0 = is; 1836 TAILQ_INIT(&is->is_outstanding); 1837 STAILQ_INIT(&is->is_postponed); 1838 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF); 1839 cv_init(&is->is_maintenance_cv, "iscsi_mt"); 1840 #ifdef ICL_KERNEL_PROXY 1841 cv_init(&is->is_login_cv, "iscsi_login"); 1842 #endif 1843 1844 /* 1845 * Set some default values, from RFC 3720, section 12. 1846 * 1847 * These values are updated by the handoff IOCTL, but are 1848 * needed prior to the handoff to support sending the ISER 1849 * login PDU. 1850 */ 1851 is->is_conn->ic_max_recv_data_segment_length = 8192; 1852 is->is_conn->ic_max_send_data_segment_length = 8192; 1853 is->is_max_burst_length = 262144; 1854 is->is_first_burst_length = 65536; 1855 1856 is->is_softc = sc; 1857 sc->sc_last_session_id++; 1858 is->is_id = sc->sc_last_session_id; 1859 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */ 1860 arc4rand(&is->is_isid[1], 5, 0); 1861 is->is_tsih = 0; 1862 callout_init(&is->is_callout, 1); 1863 1864 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt"); 1865 if (error != 0) { 1866 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error); 1867 sx_xunlock(&sc->sc_lock); 1868 return (error); 1869 } 1870 1871 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is); 1872 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next); 1873 1874 ISCSI_SESSION_LOCK(is); 1875 /* 1876 * Don't notify iscsid(8) if the session is disabled and it's not 1877 * a discovery session, 1878 */ 1879 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) { 1880 ISCSI_SESSION_UNLOCK(is); 1881 sx_xunlock(&sc->sc_lock); 1882 return (0); 1883 } 1884 1885 is->is_waiting_for_iscsid = true; 1886 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason)); 1887 ISCSI_SESSION_UNLOCK(is); 1888 cv_signal(&sc->sc_cv); 1889 sx_xunlock(&sc->sc_lock); 1890 return (0); 1891 } 1892 1893 static bool 1894 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1, 1895 unsigned int id2, const struct iscsi_session_conf *c2) 1896 { 1897 1898 if (id2 != 0 && id2 != id1) 1899 return (false); 1900 if (c2->isc_target[0] != '\0' && 1901 strcmp(c1->isc_target, c2->isc_target) != 0) 1902 return (false); 1903 if (c2->isc_target_addr[0] != '\0' && 1904 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0) 1905 return (false); 1906 return (true); 1907 } 1908 1909 static int 1910 iscsi_ioctl_session_remove(struct iscsi_softc *sc, 1911 struct iscsi_session_remove *isr) 1912 { 1913 struct iscsi_session *is, *tmp; 1914 bool found = false; 1915 1916 iscsi_sanitize_session_conf(&isr->isr_conf); 1917 1918 sx_xlock(&sc->sc_lock); 1919 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) { 1920 ISCSI_SESSION_LOCK(is); 1921 if (iscsi_session_conf_matches(is->is_id, &is->is_conf, 1922 isr->isr_session_id, &isr->isr_conf)) { 1923 found = true; 1924 iscsi_session_logout(is); 1925 iscsi_session_terminate(is); 1926 } 1927 ISCSI_SESSION_UNLOCK(is); 1928 } 1929 sx_xunlock(&sc->sc_lock); 1930 1931 if (!found) 1932 return (ESRCH); 1933 1934 return (0); 1935 } 1936 1937 static int 1938 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl) 1939 { 1940 int error; 1941 unsigned int i = 0; 1942 struct iscsi_session *is; 1943 struct iscsi_session_state iss; 1944 1945 sx_slock(&sc->sc_lock); 1946 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1947 if (i >= isl->isl_nentries) { 1948 sx_sunlock(&sc->sc_lock); 1949 return (EMSGSIZE); 1950 } 1951 memset(&iss, 0, sizeof(iss)); 1952 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf)); 1953 iss.iss_id = is->is_id; 1954 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias)); 1955 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason)); 1956 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload)); 1957 1958 if (is->is_conn->ic_header_crc32c) 1959 iss.iss_header_digest = ISCSI_DIGEST_CRC32C; 1960 else 1961 iss.iss_header_digest = ISCSI_DIGEST_NONE; 1962 1963 if (is->is_conn->ic_data_crc32c) 1964 iss.iss_data_digest = ISCSI_DIGEST_CRC32C; 1965 else 1966 iss.iss_data_digest = ISCSI_DIGEST_NONE; 1967 1968 iss.iss_max_send_data_segment_length = 1969 is->is_conn->ic_max_send_data_segment_length; 1970 iss.iss_max_recv_data_segment_length = 1971 is->is_conn->ic_max_recv_data_segment_length; 1972 iss.iss_max_burst_length = is->is_max_burst_length; 1973 iss.iss_first_burst_length = is->is_first_burst_length; 1974 iss.iss_immediate_data = is->is_immediate_data; 1975 iss.iss_connected = is->is_connected; 1976 1977 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss)); 1978 if (error != 0) { 1979 sx_sunlock(&sc->sc_lock); 1980 return (error); 1981 } 1982 i++; 1983 } 1984 sx_sunlock(&sc->sc_lock); 1985 1986 isl->isl_nentries = i; 1987 1988 return (0); 1989 } 1990 1991 static int 1992 iscsi_ioctl_session_modify(struct iscsi_softc *sc, 1993 struct iscsi_session_modify *ism) 1994 { 1995 struct iscsi_session *is; 1996 const struct iscsi_session *is2; 1997 1998 iscsi_sanitize_session_conf(&ism->ism_conf); 1999 if (iscsi_valid_session_conf(&ism->ism_conf) == false) 2000 return (EINVAL); 2001 2002 sx_xlock(&sc->sc_lock); 2003 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 2004 ISCSI_SESSION_LOCK(is); 2005 if (is->is_id == ism->ism_session_id) { 2006 /* Note that the session remains locked. */ 2007 break; 2008 } 2009 ISCSI_SESSION_UNLOCK(is); 2010 } 2011 if (is == NULL) { 2012 sx_xunlock(&sc->sc_lock); 2013 return (ESRCH); 2014 } 2015 2016 /* 2017 * Prevent duplicates. 2018 */ 2019 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) { 2020 if (is == is2) 2021 continue; 2022 2023 if (!!ism->ism_conf.isc_discovery != 2024 !!is2->is_conf.isc_discovery) 2025 continue; 2026 2027 if (strcmp(ism->ism_conf.isc_target_addr, 2028 is2->is_conf.isc_target_addr) != 0) 2029 continue; 2030 2031 if (ism->ism_conf.isc_discovery == 0 && 2032 strcmp(ism->ism_conf.isc_target, 2033 is2->is_conf.isc_target) != 0) 2034 continue; 2035 2036 ISCSI_SESSION_UNLOCK(is); 2037 sx_xunlock(&sc->sc_lock); 2038 return (EBUSY); 2039 } 2040 2041 sx_xunlock(&sc->sc_lock); 2042 2043 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf)); 2044 ISCSI_SESSION_UNLOCK(is); 2045 2046 iscsi_session_reconnect(is); 2047 2048 return (0); 2049 } 2050 2051 static int 2052 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, 2053 struct thread *td) 2054 { 2055 struct iscsi_softc *sc; 2056 2057 sc = dev->si_drv1; 2058 2059 switch (cmd) { 2060 case ISCSIDWAIT: 2061 return (iscsi_ioctl_daemon_wait(sc, 2062 (struct iscsi_daemon_request *)arg)); 2063 case ISCSIDHANDOFF: 2064 return (iscsi_ioctl_daemon_handoff(sc, 2065 (struct iscsi_daemon_handoff *)arg)); 2066 case ISCSIDFAIL: 2067 return (iscsi_ioctl_daemon_fail(sc, 2068 (struct iscsi_daemon_fail *)arg)); 2069 #ifdef ICL_KERNEL_PROXY 2070 case ISCSIDCONNECT: 2071 return (iscsi_ioctl_daemon_connect(sc, 2072 (struct iscsi_daemon_connect *)arg)); 2073 case ISCSIDSEND: 2074 return (iscsi_ioctl_daemon_send(sc, 2075 (struct iscsi_daemon_send *)arg)); 2076 case ISCSIDRECEIVE: 2077 return (iscsi_ioctl_daemon_receive(sc, 2078 (struct iscsi_daemon_receive *)arg)); 2079 #endif /* ICL_KERNEL_PROXY */ 2080 case ISCSISADD: 2081 return (iscsi_ioctl_session_add(sc, 2082 (struct iscsi_session_add *)arg)); 2083 case ISCSISREMOVE: 2084 return (iscsi_ioctl_session_remove(sc, 2085 (struct iscsi_session_remove *)arg)); 2086 case ISCSISLIST: 2087 return (iscsi_ioctl_session_list(sc, 2088 (struct iscsi_session_list *)arg)); 2089 case ISCSISMODIFY: 2090 return (iscsi_ioctl_session_modify(sc, 2091 (struct iscsi_session_modify *)arg)); 2092 default: 2093 return (EINVAL); 2094 } 2095 } 2096 2097 static struct iscsi_outstanding * 2098 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag) 2099 { 2100 struct iscsi_outstanding *io; 2101 2102 ISCSI_SESSION_LOCK_ASSERT(is); 2103 2104 TAILQ_FOREACH(io, &is->is_outstanding, io_next) { 2105 if (io->io_initiator_task_tag == initiator_task_tag) 2106 return (io); 2107 } 2108 return (NULL); 2109 } 2110 2111 static struct iscsi_outstanding * 2112 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb) 2113 { 2114 struct iscsi_outstanding *io; 2115 2116 ISCSI_SESSION_LOCK_ASSERT(is); 2117 2118 TAILQ_FOREACH(io, &is->is_outstanding, io_next) { 2119 if (io->io_ccb == ccb) 2120 return (io); 2121 } 2122 return (NULL); 2123 } 2124 2125 static struct iscsi_outstanding * 2126 iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request, 2127 union ccb *ccb, uint32_t *initiator_task_tagp) 2128 { 2129 struct iscsi_outstanding *io; 2130 int error; 2131 2132 ISCSI_SESSION_LOCK_ASSERT(is); 2133 2134 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO); 2135 if (io == NULL) { 2136 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes", 2137 sizeof(*io)); 2138 return (NULL); 2139 } 2140 2141 error = icl_conn_task_setup(is->is_conn, request, &ccb->csio, 2142 initiator_task_tagp, &io->io_icl_prv); 2143 if (error != 0) { 2144 ISCSI_SESSION_WARN(is, 2145 "icl_conn_task_setup() failed with error %d", error); 2146 uma_zfree(iscsi_outstanding_zone, io); 2147 return (NULL); 2148 } 2149 2150 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL, 2151 ("initiator_task_tag 0x%x already added", *initiator_task_tagp)); 2152 2153 io->io_initiator_task_tag = *initiator_task_tagp; 2154 io->io_ccb = ccb; 2155 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next); 2156 return (io); 2157 } 2158 2159 static void 2160 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io) 2161 { 2162 2163 ISCSI_SESSION_LOCK_ASSERT(is); 2164 2165 icl_conn_task_done(is->is_conn, io->io_icl_prv); 2166 TAILQ_REMOVE(&is->is_outstanding, io, io_next); 2167 uma_zfree(iscsi_outstanding_zone, io); 2168 } 2169 2170 static void 2171 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb) 2172 { 2173 struct icl_pdu *request; 2174 struct iscsi_bhs_task_management_request *bhstmr; 2175 struct ccb_abort *cab = &ccb->cab; 2176 struct iscsi_outstanding *io, *aio; 2177 uint32_t initiator_task_tag; 2178 2179 ISCSI_SESSION_LOCK_ASSERT(is); 2180 2181 #if 0 2182 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__)); 2183 #else 2184 if (is->is_login_phase) { 2185 ccb->ccb_h.status = CAM_REQ_ABORTED; 2186 xpt_done(ccb); 2187 return; 2188 } 2189 #endif 2190 2191 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb); 2192 if (aio == NULL) { 2193 ccb->ccb_h.status = CAM_REQ_CMP; 2194 xpt_done(ccb); 2195 return; 2196 } 2197 2198 request = icl_pdu_new(is->is_conn, M_NOWAIT); 2199 if (request == NULL) { 2200 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2201 xpt_done(ccb); 2202 return; 2203 } 2204 2205 initiator_task_tag = is->is_initiator_task_tag++; 2206 if (initiator_task_tag == 0xffffffff) 2207 initiator_task_tag = is->is_initiator_task_tag++; 2208 2209 io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag); 2210 if (io == NULL) { 2211 icl_pdu_free(request); 2212 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2213 xpt_done(ccb); 2214 return; 2215 } 2216 io->io_referenced_task_tag = aio->io_initiator_task_tag; 2217 2218 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 2219 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST; 2220 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK; 2221 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun)); 2222 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag; 2223 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag; 2224 2225 iscsi_pdu_queue_locked(request); 2226 } 2227 2228 static void 2229 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb) 2230 { 2231 struct icl_pdu *request; 2232 struct iscsi_bhs_scsi_command *bhssc; 2233 struct ccb_scsiio *csio; 2234 struct iscsi_outstanding *io; 2235 size_t len; 2236 uint32_t initiator_task_tag; 2237 int error; 2238 2239 ISCSI_SESSION_LOCK_ASSERT(is); 2240 2241 #if 0 2242 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__)); 2243 #else 2244 if (is->is_login_phase) { 2245 ISCSI_SESSION_DEBUG(is, "called during login phase"); 2246 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2247 xpt_freeze_devq(ccb->ccb_h.path, 1); 2248 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2249 } 2250 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN; 2251 xpt_done(ccb); 2252 return; 2253 } 2254 #endif 2255 2256 request = icl_pdu_new(is->is_conn, M_NOWAIT); 2257 if (request == NULL) { 2258 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2259 xpt_freeze_devq(ccb->ccb_h.path, 1); 2260 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2261 } 2262 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2263 xpt_done(ccb); 2264 return; 2265 } 2266 2267 initiator_task_tag = is->is_initiator_task_tag++; 2268 if (initiator_task_tag == 0xffffffff) 2269 initiator_task_tag = is->is_initiator_task_tag++; 2270 2271 io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag); 2272 if (io == NULL) { 2273 icl_pdu_free(request); 2274 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2275 xpt_freeze_devq(ccb->ccb_h.path, 1); 2276 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2277 } 2278 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2279 xpt_done(ccb); 2280 return; 2281 } 2282 2283 csio = &ccb->csio; 2284 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 2285 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND; 2286 bhssc->bhssc_flags |= BHSSC_FLAGS_F; 2287 switch (csio->ccb_h.flags & CAM_DIR_MASK) { 2288 case CAM_DIR_IN: 2289 bhssc->bhssc_flags |= BHSSC_FLAGS_R; 2290 break; 2291 case CAM_DIR_OUT: 2292 bhssc->bhssc_flags |= BHSSC_FLAGS_W; 2293 break; 2294 } 2295 2296 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) { 2297 switch (csio->tag_action) { 2298 case MSG_HEAD_OF_Q_TAG: 2299 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ; 2300 break; 2301 case MSG_ORDERED_Q_TAG: 2302 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED; 2303 break; 2304 case MSG_ACA_TASK: 2305 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA; 2306 break; 2307 case MSG_SIMPLE_Q_TAG: 2308 default: 2309 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE; 2310 break; 2311 } 2312 } else 2313 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED; 2314 2315 if (is->is_protocol_level >= 2) { 2316 bhssc->bhssc_pri = (csio->priority << BHSSC_PRI_SHIFT) & 2317 BHSSC_PRI_MASK; 2318 } 2319 2320 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun)); 2321 bhssc->bhssc_initiator_task_tag = initiator_task_tag; 2322 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len); 2323 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb), 2324 ("unsupported CDB size %zd", (size_t)csio->cdb_len)); 2325 2326 if (csio->ccb_h.flags & CAM_CDB_POINTER) 2327 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len); 2328 else 2329 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len); 2330 2331 if (is->is_immediate_data && 2332 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 2333 len = csio->dxfer_len; 2334 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len); 2335 if (len > is->is_first_burst_length) { 2336 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len, is->is_first_burst_length); 2337 len = is->is_first_burst_length; 2338 } 2339 if (len > is->is_conn->ic_max_send_data_segment_length) { 2340 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len, 2341 is->is_conn->ic_max_send_data_segment_length); 2342 len = is->is_conn->ic_max_send_data_segment_length; 2343 } 2344 2345 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT); 2346 if (error != 0) { 2347 iscsi_outstanding_remove(is, io); 2348 icl_pdu_free(request); 2349 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2350 xpt_freeze_devq(ccb->ccb_h.path, 1); 2351 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2352 } 2353 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2354 xpt_done(ccb); 2355 return; 2356 } 2357 } 2358 iscsi_pdu_queue_locked(request); 2359 } 2360 2361 static void 2362 iscsi_action(struct cam_sim *sim, union ccb *ccb) 2363 { 2364 struct iscsi_session *is; 2365 2366 is = cam_sim_softc(sim); 2367 2368 ISCSI_SESSION_LOCK_ASSERT(is); 2369 2370 if (is->is_terminating || 2371 (is->is_connected == false && fail_on_disconnection)) { 2372 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2373 xpt_done(ccb); 2374 return; 2375 } 2376 2377 /* 2378 * Make sure CAM doesn't sneak in a CCB just after freezing the queue. 2379 */ 2380 if (is->is_simq_frozen == true) { 2381 ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK); 2382 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 2383 /* Don't freeze the devq - the SIM queue is already frozen. */ 2384 xpt_done(ccb); 2385 return; 2386 } 2387 2388 switch (ccb->ccb_h.func_code) { 2389 case XPT_PATH_INQ: 2390 { 2391 struct ccb_pathinq *cpi = &ccb->cpi; 2392 2393 cpi->version_num = 1; 2394 cpi->hba_inquiry = PI_TAG_ABLE; 2395 cpi->target_sprt = 0; 2396 cpi->hba_misc = PIM_EXTLUNS; 2397 /* 2398 * XXX: It shouldn't ever be NULL; this could be turned 2399 * into a KASSERT eventually. 2400 */ 2401 if (is->is_conn == NULL) 2402 ISCSI_WARN("NULL conn"); 2403 else if (is->is_conn->ic_unmapped) 2404 cpi->hba_misc |= PIM_UNMAPPED; 2405 cpi->hba_eng_cnt = 0; 2406 cpi->max_target = 0; 2407 /* 2408 * Note that the variable below is only relevant for targets 2409 * that don't claim compliance with anything above SPC2, which 2410 * means they don't support REPORT_LUNS. 2411 */ 2412 cpi->max_lun = 255; 2413 cpi->initiator_id = ~0; 2414 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2415 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN); 2416 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2417 cpi->unit_number = cam_sim_unit(sim); 2418 cpi->bus_id = cam_sim_bus(sim); 2419 cpi->base_transfer_speed = 150000; /* XXX */ 2420 cpi->transport = XPORT_ISCSI; 2421 cpi->transport_version = 0; 2422 cpi->protocol = PROTO_SCSI; 2423 cpi->protocol_version = SCSI_REV_SPC3; 2424 cpi->maxio = maxphys; 2425 cpi->ccb_h.status = CAM_REQ_CMP; 2426 break; 2427 } 2428 case XPT_GET_TRAN_SETTINGS: 2429 { 2430 struct ccb_trans_settings *cts; 2431 struct ccb_trans_settings_scsi *scsi; 2432 2433 cts = &ccb->cts; 2434 scsi = &cts->proto_specific.scsi; 2435 2436 cts->protocol = PROTO_SCSI; 2437 cts->protocol_version = SCSI_REV_SPC3; 2438 cts->transport = XPORT_ISCSI; 2439 cts->transport_version = 0; 2440 scsi->valid = CTS_SCSI_VALID_TQ; 2441 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 2442 cts->ccb_h.status = CAM_REQ_CMP; 2443 break; 2444 } 2445 case XPT_CALC_GEOMETRY: 2446 cam_calc_geometry(&ccb->ccg, /*extended*/1); 2447 ccb->ccb_h.status = CAM_REQ_CMP; 2448 break; 2449 #if 0 2450 /* 2451 * XXX: What's the point? 2452 */ 2453 case XPT_RESET_BUS: 2454 case XPT_TERM_IO: 2455 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io"); 2456 ccb->ccb_h.status = CAM_REQ_CMP; 2457 break; 2458 #endif 2459 case XPT_ABORT: 2460 iscsi_action_abort(is, ccb); 2461 return; 2462 case XPT_SCSI_IO: 2463 iscsi_action_scsiio(is, ccb); 2464 return; 2465 default: 2466 #if 0 2467 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code); 2468 #endif 2469 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2470 break; 2471 } 2472 xpt_done(ccb); 2473 } 2474 2475 static void 2476 iscsi_terminate_sessions(struct iscsi_softc *sc) 2477 { 2478 struct iscsi_session *is; 2479 2480 sx_slock(&sc->sc_lock); 2481 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) 2482 iscsi_session_terminate(is); 2483 while(!TAILQ_EMPTY(&sc->sc_sessions)) { 2484 ISCSI_DEBUG("waiting for sessions to terminate"); 2485 cv_wait(&sc->sc_cv, &sc->sc_lock); 2486 } 2487 ISCSI_DEBUG("all sessions terminated"); 2488 sx_sunlock(&sc->sc_lock); 2489 } 2490 2491 static void 2492 iscsi_shutdown_pre(struct iscsi_softc *sc) 2493 { 2494 struct iscsi_session *is; 2495 2496 if (!fail_on_shutdown) 2497 return; 2498 2499 /* 2500 * If we have any sessions waiting for reconnection, request 2501 * maintenance thread to fail them immediately instead of waiting 2502 * for reconnect timeout. 2503 * 2504 * This prevents LUNs with mounted filesystems that are supported 2505 * by disconnected iSCSI sessions from hanging, however it will 2506 * fail all queued BIOs. 2507 */ 2508 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown"); 2509 2510 fail_on_disconnection = 1; 2511 2512 sx_slock(&sc->sc_lock); 2513 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 2514 ISCSI_SESSION_LOCK(is); 2515 if (!is->is_connected) { 2516 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early"); 2517 iscsi_session_reconnect(is); 2518 } 2519 ISCSI_SESSION_UNLOCK(is); 2520 } 2521 sx_sunlock(&sc->sc_lock); 2522 } 2523 2524 static void 2525 iscsi_shutdown_post(struct iscsi_softc *sc) 2526 { 2527 2528 if (!KERNEL_PANICKED()) { 2529 ISCSI_DEBUG("removing all sessions due to shutdown"); 2530 iscsi_terminate_sessions(sc); 2531 } 2532 } 2533 2534 static int 2535 iscsi_load(void) 2536 { 2537 int error; 2538 2539 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK); 2540 sx_init(&sc->sc_lock, "iscsi"); 2541 TAILQ_INIT(&sc->sc_sessions); 2542 cv_init(&sc->sc_cv, "iscsi_cv"); 2543 2544 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding", 2545 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL, 2546 UMA_ALIGN_PTR, 0); 2547 2548 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw, 2549 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi"); 2550 if (error != 0) { 2551 ISCSI_WARN("failed to create device node, error %d", error); 2552 return (error); 2553 } 2554 sc->sc_cdev->si_drv1 = sc; 2555 2556 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, 2557 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST); 2558 /* 2559 * shutdown_post_sync needs to run after filesystem shutdown and before 2560 * CAM shutdown - otherwise when rebooting with an iSCSI session that is 2561 * disconnected but has outstanding requests, dashutdown() will hang on 2562 * cam_periph_runccb(). 2563 */ 2564 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync, 2565 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1); 2566 2567 return (0); 2568 } 2569 2570 static int 2571 iscsi_unload(void) 2572 { 2573 2574 /* Awaken any threads asleep in iscsi_ioctl(). */ 2575 sx_xlock(&sc->sc_lock); 2576 sc->sc_unloading = true; 2577 cv_signal(&sc->sc_cv); 2578 sx_xunlock(&sc->sc_lock); 2579 2580 if (sc->sc_cdev != NULL) { 2581 ISCSI_DEBUG("removing device node"); 2582 destroy_dev(sc->sc_cdev); 2583 ISCSI_DEBUG("device node removed"); 2584 } 2585 2586 if (sc->sc_shutdown_pre_eh != NULL) 2587 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh); 2588 if (sc->sc_shutdown_post_eh != NULL) 2589 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh); 2590 2591 iscsi_terminate_sessions(sc); 2592 2593 uma_zdestroy(iscsi_outstanding_zone); 2594 sx_destroy(&sc->sc_lock); 2595 cv_destroy(&sc->sc_cv); 2596 free(sc, M_ISCSI); 2597 return (0); 2598 } 2599 2600 static int 2601 iscsi_quiesce(void) 2602 { 2603 sx_slock(&sc->sc_lock); 2604 if (!TAILQ_EMPTY(&sc->sc_sessions)) { 2605 sx_sunlock(&sc->sc_lock); 2606 return (EBUSY); 2607 } 2608 sx_sunlock(&sc->sc_lock); 2609 return (0); 2610 } 2611 2612 static int 2613 iscsi_modevent(module_t mod, int what, void *arg) 2614 { 2615 int error; 2616 2617 switch (what) { 2618 case MOD_LOAD: 2619 error = iscsi_load(); 2620 break; 2621 case MOD_UNLOAD: 2622 error = iscsi_unload(); 2623 break; 2624 case MOD_QUIESCE: 2625 error = iscsi_quiesce(); 2626 break; 2627 default: 2628 error = EINVAL; 2629 break; 2630 } 2631 return (error); 2632 } 2633 2634 moduledata_t iscsi_data = { 2635 "iscsi", 2636 iscsi_modevent, 2637 0 2638 }; 2639 2640 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 2641 MODULE_DEPEND(iscsi, cam, 1, 1, 1); 2642 MODULE_DEPEND(iscsi, icl, 1, 1, 1); 2643