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