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