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 /* 1419 * If we're going through the proxy, the idh_socket will be 0, 1420 * and the ICL module can simply ignore this call. It can also 1421 * use it to determine it's no longer in the Login phase. 1422 */ 1423 error = icl_conn_handoff(ic, handoff->idh_socket); 1424 if (error != 0) { 1425 sx_sunlock(&sc->sc_lock); 1426 iscsi_session_terminate(is); 1427 return (error); 1428 } 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_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 iscsi_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 int error; 1632 1633 sx_slock(&sc->sc_lock); 1634 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1635 if (is->is_id == idr->idr_session_id) 1636 break; 1637 } 1638 if (is == NULL) { 1639 sx_sunlock(&sc->sc_lock); 1640 return (ESRCH); 1641 } 1642 sx_sunlock(&sc->sc_lock); 1643 1644 if (is->is_login_phase == false) 1645 return (EBUSY); 1646 1647 ISCSI_SESSION_LOCK(is); 1648 while (is->is_login_pdu == NULL && 1649 is->is_terminating == false && 1650 is->is_reconnecting == false) { 1651 error = cv_wait_sig(&is->is_login_cv, &is->is_lock); 1652 if (error != 0) { 1653 ISCSI_SESSION_UNLOCK(is); 1654 return (error); 1655 } 1656 } 1657 if (is->is_terminating || is->is_reconnecting) { 1658 ISCSI_SESSION_UNLOCK(is); 1659 return (EIO); 1660 } 1661 ip = is->is_login_pdu; 1662 is->is_login_pdu = NULL; 1663 ISCSI_SESSION_UNLOCK(is); 1664 1665 if (ip->ip_data_len > idr->idr_data_segment_len) { 1666 icl_pdu_free(ip); 1667 return (EMSGSIZE); 1668 } 1669 1670 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs)); 1671 if (ip->ip_data_len > 0) { 1672 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK); 1673 icl_pdu_get_data(ip, 0, data, ip->ip_data_len); 1674 copyout(data, idr->idr_data_segment, ip->ip_data_len); 1675 free(data, M_ISCSI); 1676 } 1677 1678 icl_pdu_free(ip); 1679 1680 return (0); 1681 } 1682 #endif /* ICL_KERNEL_PROXY */ 1683 1684 static void 1685 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc) 1686 { 1687 /* 1688 * Just make sure all the fields are null-terminated. 1689 * 1690 * XXX: This is not particularly secure. We should 1691 * create our own conf and then copy in relevant 1692 * fields. 1693 */ 1694 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0'; 1695 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0'; 1696 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0'; 1697 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0'; 1698 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0'; 1699 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0'; 1700 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0'; 1701 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0'; 1702 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0'; 1703 } 1704 1705 static bool 1706 iscsi_valid_session_conf(const struct iscsi_session_conf *isc) 1707 { 1708 1709 if (isc->isc_initiator[0] == '\0') { 1710 ISCSI_DEBUG("empty isc_initiator"); 1711 return (false); 1712 } 1713 1714 if (isc->isc_target_addr[0] == '\0') { 1715 ISCSI_DEBUG("empty isc_target_addr"); 1716 return (false); 1717 } 1718 1719 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) { 1720 ISCSI_DEBUG("non-empty isc_target for discovery session"); 1721 return (false); 1722 } 1723 1724 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) { 1725 ISCSI_DEBUG("empty isc_target for non-discovery session"); 1726 return (false); 1727 } 1728 1729 return (true); 1730 } 1731 1732 static int 1733 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa) 1734 { 1735 struct iscsi_session *is; 1736 const struct iscsi_session *is2; 1737 int error; 1738 1739 iscsi_sanitize_session_conf(&isa->isa_conf); 1740 if (iscsi_valid_session_conf(&isa->isa_conf) == false) 1741 return (EINVAL); 1742 1743 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK); 1744 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf)); 1745 1746 sx_xlock(&sc->sc_lock); 1747 1748 /* 1749 * Prevent duplicates. 1750 */ 1751 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) { 1752 if (!!is->is_conf.isc_discovery != 1753 !!is2->is_conf.isc_discovery) 1754 continue; 1755 1756 if (strcmp(is->is_conf.isc_target_addr, 1757 is2->is_conf.isc_target_addr) != 0) 1758 continue; 1759 1760 if (is->is_conf.isc_discovery == 0 && 1761 strcmp(is->is_conf.isc_target, 1762 is2->is_conf.isc_target) != 0) 1763 continue; 1764 1765 sx_xunlock(&sc->sc_lock); 1766 free(is, M_ISCSI); 1767 return (EBUSY); 1768 } 1769 1770 is->is_conn = icl_new_conn(is->is_conf.isc_offload, 1771 "iscsi", &is->is_lock); 1772 if (is->is_conn == NULL) { 1773 sx_xunlock(&sc->sc_lock); 1774 free(is, M_ISCSI); 1775 return (EINVAL); 1776 } 1777 is->is_conn->ic_receive = iscsi_receive_callback; 1778 is->is_conn->ic_error = iscsi_error_callback; 1779 is->is_conn->ic_prv0 = is; 1780 TAILQ_INIT(&is->is_outstanding); 1781 STAILQ_INIT(&is->is_postponed); 1782 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF); 1783 cv_init(&is->is_maintenance_cv, "iscsi_mt"); 1784 #ifdef ICL_KERNEL_PROXY 1785 cv_init(&is->is_login_cv, "iscsi_login"); 1786 #endif 1787 1788 is->is_softc = sc; 1789 sc->sc_last_session_id++; 1790 is->is_id = sc->sc_last_session_id; 1791 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */ 1792 arc4rand(&is->is_isid[1], 5, 0); 1793 is->is_tsih = 0; 1794 callout_init(&is->is_callout, 1); 1795 1796 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt"); 1797 if (error != 0) { 1798 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error); 1799 sx_xunlock(&sc->sc_lock); 1800 return (error); 1801 } 1802 1803 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is); 1804 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next); 1805 1806 /* 1807 * Trigger immediate reconnection. 1808 */ 1809 ISCSI_SESSION_LOCK(is); 1810 is->is_waiting_for_iscsid = true; 1811 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason)); 1812 ISCSI_SESSION_UNLOCK(is); 1813 cv_signal(&sc->sc_cv); 1814 1815 sx_xunlock(&sc->sc_lock); 1816 1817 return (0); 1818 } 1819 1820 static bool 1821 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1, 1822 unsigned int id2, const struct iscsi_session_conf *c2) 1823 { 1824 1825 if (id2 != 0 && id2 != id1) 1826 return (false); 1827 if (c2->isc_target[0] != '\0' && 1828 strcmp(c1->isc_target, c2->isc_target) != 0) 1829 return (false); 1830 if (c2->isc_target_addr[0] != '\0' && 1831 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0) 1832 return (false); 1833 return (true); 1834 } 1835 1836 static int 1837 iscsi_ioctl_session_remove(struct iscsi_softc *sc, 1838 struct iscsi_session_remove *isr) 1839 { 1840 struct iscsi_session *is, *tmp; 1841 bool found = false; 1842 1843 iscsi_sanitize_session_conf(&isr->isr_conf); 1844 1845 sx_xlock(&sc->sc_lock); 1846 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) { 1847 ISCSI_SESSION_LOCK(is); 1848 if (iscsi_session_conf_matches(is->is_id, &is->is_conf, 1849 isr->isr_session_id, &isr->isr_conf)) { 1850 found = true; 1851 iscsi_session_logout(is); 1852 iscsi_session_terminate(is); 1853 } 1854 ISCSI_SESSION_UNLOCK(is); 1855 } 1856 sx_xunlock(&sc->sc_lock); 1857 1858 if (!found) 1859 return (ESRCH); 1860 1861 return (0); 1862 } 1863 1864 static int 1865 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl) 1866 { 1867 int error; 1868 unsigned int i = 0; 1869 struct iscsi_session *is; 1870 struct iscsi_session_state iss; 1871 1872 sx_slock(&sc->sc_lock); 1873 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1874 if (i >= isl->isl_nentries) { 1875 sx_sunlock(&sc->sc_lock); 1876 return (EMSGSIZE); 1877 } 1878 memset(&iss, 0, sizeof(iss)); 1879 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf)); 1880 iss.iss_id = is->is_id; 1881 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias)); 1882 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason)); 1883 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload)); 1884 1885 if (is->is_conn->ic_header_crc32c) 1886 iss.iss_header_digest = ISCSI_DIGEST_CRC32C; 1887 else 1888 iss.iss_header_digest = ISCSI_DIGEST_NONE; 1889 1890 if (is->is_conn->ic_data_crc32c) 1891 iss.iss_data_digest = ISCSI_DIGEST_CRC32C; 1892 else 1893 iss.iss_data_digest = ISCSI_DIGEST_NONE; 1894 1895 iss.iss_max_data_segment_length = is->is_max_data_segment_length; 1896 iss.iss_immediate_data = is->is_immediate_data; 1897 iss.iss_connected = is->is_connected; 1898 1899 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss)); 1900 if (error != 0) { 1901 sx_sunlock(&sc->sc_lock); 1902 return (error); 1903 } 1904 i++; 1905 } 1906 sx_sunlock(&sc->sc_lock); 1907 1908 isl->isl_nentries = i; 1909 1910 return (0); 1911 } 1912 1913 static int 1914 iscsi_ioctl_session_modify(struct iscsi_softc *sc, 1915 struct iscsi_session_modify *ism) 1916 { 1917 struct iscsi_session *is; 1918 1919 iscsi_sanitize_session_conf(&ism->ism_conf); 1920 if (iscsi_valid_session_conf(&ism->ism_conf) == false) 1921 return (EINVAL); 1922 1923 sx_xlock(&sc->sc_lock); 1924 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1925 ISCSI_SESSION_LOCK(is); 1926 if (is->is_id == ism->ism_session_id) 1927 break; 1928 ISCSI_SESSION_UNLOCK(is); 1929 } 1930 if (is == NULL) { 1931 sx_xunlock(&sc->sc_lock); 1932 return (ESRCH); 1933 } 1934 sx_xunlock(&sc->sc_lock); 1935 1936 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf)); 1937 ISCSI_SESSION_UNLOCK(is); 1938 1939 iscsi_session_reconnect(is); 1940 1941 return (0); 1942 } 1943 1944 static int 1945 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, 1946 struct thread *td) 1947 { 1948 struct iscsi_softc *sc; 1949 1950 sc = dev->si_drv1; 1951 1952 switch (cmd) { 1953 case ISCSIDWAIT: 1954 return (iscsi_ioctl_daemon_wait(sc, 1955 (struct iscsi_daemon_request *)arg)); 1956 case ISCSIDHANDOFF: 1957 return (iscsi_ioctl_daemon_handoff(sc, 1958 (struct iscsi_daemon_handoff *)arg)); 1959 case ISCSIDFAIL: 1960 return (iscsi_ioctl_daemon_fail(sc, 1961 (struct iscsi_daemon_fail *)arg)); 1962 #ifdef ICL_KERNEL_PROXY 1963 case ISCSIDCONNECT: 1964 return (iscsi_ioctl_daemon_connect(sc, 1965 (struct iscsi_daemon_connect *)arg)); 1966 case ISCSIDSEND: 1967 return (iscsi_ioctl_daemon_send(sc, 1968 (struct iscsi_daemon_send *)arg)); 1969 case ISCSIDRECEIVE: 1970 return (iscsi_ioctl_daemon_receive(sc, 1971 (struct iscsi_daemon_receive *)arg)); 1972 #endif /* ICL_KERNEL_PROXY */ 1973 case ISCSISADD: 1974 return (iscsi_ioctl_session_add(sc, 1975 (struct iscsi_session_add *)arg)); 1976 case ISCSISREMOVE: 1977 return (iscsi_ioctl_session_remove(sc, 1978 (struct iscsi_session_remove *)arg)); 1979 case ISCSISLIST: 1980 return (iscsi_ioctl_session_list(sc, 1981 (struct iscsi_session_list *)arg)); 1982 case ISCSISMODIFY: 1983 return (iscsi_ioctl_session_modify(sc, 1984 (struct iscsi_session_modify *)arg)); 1985 default: 1986 return (EINVAL); 1987 } 1988 } 1989 1990 static struct iscsi_outstanding * 1991 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag) 1992 { 1993 struct iscsi_outstanding *io; 1994 1995 ISCSI_SESSION_LOCK_ASSERT(is); 1996 1997 TAILQ_FOREACH(io, &is->is_outstanding, io_next) { 1998 if (io->io_initiator_task_tag == initiator_task_tag) 1999 return (io); 2000 } 2001 return (NULL); 2002 } 2003 2004 static struct iscsi_outstanding * 2005 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb) 2006 { 2007 struct iscsi_outstanding *io; 2008 2009 ISCSI_SESSION_LOCK_ASSERT(is); 2010 2011 TAILQ_FOREACH(io, &is->is_outstanding, io_next) { 2012 if (io->io_ccb == ccb) 2013 return (io); 2014 } 2015 return (NULL); 2016 } 2017 2018 static struct iscsi_outstanding * 2019 iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request, 2020 union ccb *ccb, uint32_t *initiator_task_tagp) 2021 { 2022 struct iscsi_outstanding *io; 2023 int error; 2024 2025 ISCSI_SESSION_LOCK_ASSERT(is); 2026 2027 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO); 2028 if (io == NULL) { 2029 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes", 2030 sizeof(*io)); 2031 return (NULL); 2032 } 2033 2034 error = icl_conn_task_setup(is->is_conn, request, &ccb->csio, 2035 initiator_task_tagp, &io->io_icl_prv); 2036 if (error != 0) { 2037 ISCSI_SESSION_WARN(is, 2038 "icl_conn_task_setup() failed with error %d", error); 2039 uma_zfree(iscsi_outstanding_zone, io); 2040 return (NULL); 2041 } 2042 2043 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL, 2044 ("initiator_task_tag 0x%x already added", *initiator_task_tagp)); 2045 2046 io->io_initiator_task_tag = *initiator_task_tagp; 2047 io->io_ccb = ccb; 2048 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next); 2049 return (io); 2050 } 2051 2052 static void 2053 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io) 2054 { 2055 2056 ISCSI_SESSION_LOCK_ASSERT(is); 2057 2058 icl_conn_task_done(is->is_conn, io->io_icl_prv); 2059 TAILQ_REMOVE(&is->is_outstanding, io, io_next); 2060 uma_zfree(iscsi_outstanding_zone, io); 2061 } 2062 2063 static void 2064 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb) 2065 { 2066 struct icl_pdu *request; 2067 struct iscsi_bhs_task_management_request *bhstmr; 2068 struct ccb_abort *cab = &ccb->cab; 2069 struct iscsi_outstanding *io, *aio; 2070 uint32_t initiator_task_tag; 2071 2072 ISCSI_SESSION_LOCK_ASSERT(is); 2073 2074 #if 0 2075 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__)); 2076 #else 2077 if (is->is_login_phase) { 2078 ccb->ccb_h.status = CAM_REQ_ABORTED; 2079 xpt_done(ccb); 2080 return; 2081 } 2082 #endif 2083 2084 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb); 2085 if (aio == NULL) { 2086 ccb->ccb_h.status = CAM_REQ_CMP; 2087 xpt_done(ccb); 2088 return; 2089 } 2090 2091 request = icl_pdu_new(is->is_conn, M_NOWAIT); 2092 if (request == NULL) { 2093 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2094 xpt_done(ccb); 2095 return; 2096 } 2097 2098 initiator_task_tag = is->is_initiator_task_tag++; 2099 2100 io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag); 2101 if (io == NULL) { 2102 icl_pdu_free(request); 2103 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2104 xpt_done(ccb); 2105 return; 2106 } 2107 io->io_datasn = aio->io_initiator_task_tag; 2108 2109 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 2110 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST; 2111 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK; 2112 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun)); 2113 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag; 2114 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag; 2115 2116 iscsi_pdu_queue_locked(request); 2117 } 2118 2119 static void 2120 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb) 2121 { 2122 struct icl_pdu *request; 2123 struct iscsi_bhs_scsi_command *bhssc; 2124 struct ccb_scsiio *csio; 2125 struct iscsi_outstanding *io; 2126 size_t len; 2127 uint32_t initiator_task_tag; 2128 int error; 2129 2130 ISCSI_SESSION_LOCK_ASSERT(is); 2131 2132 #if 0 2133 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__)); 2134 #else 2135 if (is->is_login_phase) { 2136 ISCSI_SESSION_DEBUG(is, "called during login phase"); 2137 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2138 xpt_freeze_devq(ccb->ccb_h.path, 1); 2139 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2140 } 2141 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN; 2142 xpt_done(ccb); 2143 return; 2144 } 2145 #endif 2146 2147 request = icl_pdu_new(is->is_conn, M_NOWAIT); 2148 if (request == NULL) { 2149 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2150 xpt_freeze_devq(ccb->ccb_h.path, 1); 2151 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2152 } 2153 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2154 xpt_done(ccb); 2155 return; 2156 } 2157 2158 initiator_task_tag = is->is_initiator_task_tag++; 2159 io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag); 2160 if (io == NULL) { 2161 icl_pdu_free(request); 2162 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2163 xpt_freeze_devq(ccb->ccb_h.path, 1); 2164 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2165 } 2166 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2167 xpt_done(ccb); 2168 return; 2169 } 2170 2171 csio = &ccb->csio; 2172 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 2173 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND; 2174 bhssc->bhssc_flags |= BHSSC_FLAGS_F; 2175 switch (csio->ccb_h.flags & CAM_DIR_MASK) { 2176 case CAM_DIR_IN: 2177 bhssc->bhssc_flags |= BHSSC_FLAGS_R; 2178 break; 2179 case CAM_DIR_OUT: 2180 bhssc->bhssc_flags |= BHSSC_FLAGS_W; 2181 break; 2182 } 2183 2184 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) { 2185 switch (csio->tag_action) { 2186 case MSG_HEAD_OF_Q_TAG: 2187 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ; 2188 break; 2189 case MSG_ORDERED_Q_TAG: 2190 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED; 2191 break; 2192 case MSG_ACA_TASK: 2193 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA; 2194 break; 2195 case MSG_SIMPLE_Q_TAG: 2196 default: 2197 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE; 2198 break; 2199 } 2200 } else 2201 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED; 2202 2203 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun)); 2204 bhssc->bhssc_initiator_task_tag = initiator_task_tag; 2205 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len); 2206 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb), 2207 ("unsupported CDB size %zd", (size_t)csio->cdb_len)); 2208 2209 if (csio->ccb_h.flags & CAM_CDB_POINTER) 2210 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len); 2211 else 2212 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len); 2213 2214 if (is->is_immediate_data && 2215 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 2216 len = csio->dxfer_len; 2217 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len); 2218 if (len > is->is_first_burst_length) { 2219 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_first_burst_length); 2220 len = is->is_first_burst_length; 2221 } 2222 if (len > is->is_max_data_segment_length) { 2223 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_max_data_segment_length); 2224 len = is->is_max_data_segment_length; 2225 } 2226 2227 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT); 2228 if (error != 0) { 2229 iscsi_outstanding_remove(is, io); 2230 icl_pdu_free(request); 2231 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2232 xpt_freeze_devq(ccb->ccb_h.path, 1); 2233 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2234 } 2235 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2236 xpt_done(ccb); 2237 return; 2238 } 2239 } 2240 iscsi_pdu_queue_locked(request); 2241 } 2242 2243 static void 2244 iscsi_action(struct cam_sim *sim, union ccb *ccb) 2245 { 2246 struct iscsi_session *is; 2247 2248 is = cam_sim_softc(sim); 2249 2250 ISCSI_SESSION_LOCK_ASSERT(is); 2251 2252 if (is->is_terminating || 2253 (is->is_connected == false && fail_on_disconnection)) { 2254 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2255 xpt_done(ccb); 2256 return; 2257 } 2258 2259 switch (ccb->ccb_h.func_code) { 2260 case XPT_PATH_INQ: 2261 { 2262 struct ccb_pathinq *cpi = &ccb->cpi; 2263 2264 cpi->version_num = 1; 2265 cpi->hba_inquiry = PI_TAG_ABLE; 2266 cpi->target_sprt = 0; 2267 cpi->hba_misc = PIM_EXTLUNS; 2268 cpi->hba_eng_cnt = 0; 2269 cpi->max_target = 0; 2270 /* 2271 * Note that the variable below is only relevant for targets 2272 * that don't claim compliance with anything above SPC2, which 2273 * means they don't support REPORT_LUNS. 2274 */ 2275 cpi->max_lun = 255; 2276 cpi->initiator_id = ~0; 2277 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2278 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN); 2279 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2280 cpi->unit_number = cam_sim_unit(sim); 2281 cpi->bus_id = cam_sim_bus(sim); 2282 cpi->base_transfer_speed = 150000; /* XXX */ 2283 cpi->transport = XPORT_ISCSI; 2284 cpi->transport_version = 0; 2285 cpi->protocol = PROTO_SCSI; 2286 cpi->protocol_version = SCSI_REV_SPC3; 2287 cpi->maxio = MAXPHYS; 2288 cpi->ccb_h.status = CAM_REQ_CMP; 2289 break; 2290 } 2291 case XPT_GET_TRAN_SETTINGS: 2292 { 2293 struct ccb_trans_settings *cts; 2294 struct ccb_trans_settings_scsi *scsi; 2295 2296 cts = &ccb->cts; 2297 scsi = &cts->proto_specific.scsi; 2298 2299 cts->protocol = PROTO_SCSI; 2300 cts->protocol_version = SCSI_REV_SPC3; 2301 cts->transport = XPORT_ISCSI; 2302 cts->transport_version = 0; 2303 scsi->valid = CTS_SCSI_VALID_TQ; 2304 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 2305 cts->ccb_h.status = CAM_REQ_CMP; 2306 break; 2307 } 2308 case XPT_CALC_GEOMETRY: 2309 cam_calc_geometry(&ccb->ccg, /*extended*/1); 2310 ccb->ccb_h.status = CAM_REQ_CMP; 2311 break; 2312 #if 0 2313 /* 2314 * XXX: What's the point? 2315 */ 2316 case XPT_RESET_BUS: 2317 case XPT_TERM_IO: 2318 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io"); 2319 ccb->ccb_h.status = CAM_REQ_CMP; 2320 break; 2321 #endif 2322 case XPT_ABORT: 2323 iscsi_action_abort(is, ccb); 2324 return; 2325 case XPT_SCSI_IO: 2326 iscsi_action_scsiio(is, ccb); 2327 return; 2328 default: 2329 #if 0 2330 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code); 2331 #endif 2332 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2333 break; 2334 } 2335 xpt_done(ccb); 2336 } 2337 2338 static void 2339 iscsi_poll(struct cam_sim *sim) 2340 { 2341 2342 KASSERT(0, ("%s: you're not supposed to be here", __func__)); 2343 } 2344 2345 static void 2346 iscsi_terminate_sessions(struct iscsi_softc *sc) 2347 { 2348 struct iscsi_session *is; 2349 2350 sx_slock(&sc->sc_lock); 2351 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) 2352 iscsi_session_terminate(is); 2353 while(!TAILQ_EMPTY(&sc->sc_sessions)) { 2354 ISCSI_DEBUG("waiting for sessions to terminate"); 2355 cv_wait(&sc->sc_cv, &sc->sc_lock); 2356 } 2357 ISCSI_DEBUG("all sessions terminated"); 2358 sx_sunlock(&sc->sc_lock); 2359 } 2360 2361 static void 2362 iscsi_shutdown_pre(struct iscsi_softc *sc) 2363 { 2364 struct iscsi_session *is; 2365 2366 if (!fail_on_shutdown) 2367 return; 2368 2369 /* 2370 * If we have any sessions waiting for reconnection, request 2371 * maintenance thread to fail them immediately instead of waiting 2372 * for reconnect timeout. 2373 * 2374 * This prevents LUNs with mounted filesystems that are supported 2375 * by disconnected iSCSI sessions from hanging, however it will 2376 * fail all queued BIOs. 2377 */ 2378 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown"); 2379 2380 fail_on_disconnection = 1; 2381 2382 sx_slock(&sc->sc_lock); 2383 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 2384 ISCSI_SESSION_LOCK(is); 2385 if (!is->is_connected) { 2386 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early"); 2387 iscsi_session_reconnect(is); 2388 } 2389 ISCSI_SESSION_UNLOCK(is); 2390 } 2391 sx_sunlock(&sc->sc_lock); 2392 } 2393 2394 static void 2395 iscsi_shutdown_post(struct iscsi_softc *sc) 2396 { 2397 2398 ISCSI_DEBUG("removing all sessions due to shutdown"); 2399 iscsi_terminate_sessions(sc); 2400 } 2401 2402 static int 2403 iscsi_load(void) 2404 { 2405 int error; 2406 2407 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK); 2408 sx_init(&sc->sc_lock, "iscsi"); 2409 TAILQ_INIT(&sc->sc_sessions); 2410 cv_init(&sc->sc_cv, "iscsi_cv"); 2411 2412 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding", 2413 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL, 2414 UMA_ALIGN_PTR, 0); 2415 2416 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw, 2417 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi"); 2418 if (error != 0) { 2419 ISCSI_WARN("failed to create device node, error %d", error); 2420 return (error); 2421 } 2422 sc->sc_cdev->si_drv1 = sc; 2423 2424 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, 2425 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST); 2426 /* 2427 * shutdown_post_sync needs to run after filesystem shutdown and before 2428 * CAM shutdown - otherwise when rebooting with an iSCSI session that is 2429 * disconnected but has outstanding requests, dashutdown() will hang on 2430 * cam_periph_runccb(). 2431 */ 2432 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync, 2433 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1); 2434 2435 return (0); 2436 } 2437 2438 static int 2439 iscsi_unload(void) 2440 { 2441 2442 if (sc->sc_cdev != NULL) { 2443 ISCSI_DEBUG("removing device node"); 2444 destroy_dev(sc->sc_cdev); 2445 ISCSI_DEBUG("device node removed"); 2446 } 2447 2448 if (sc->sc_shutdown_pre_eh != NULL) 2449 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh); 2450 if (sc->sc_shutdown_post_eh != NULL) 2451 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh); 2452 2453 iscsi_terminate_sessions(sc); 2454 2455 uma_zdestroy(iscsi_outstanding_zone); 2456 sx_destroy(&sc->sc_lock); 2457 cv_destroy(&sc->sc_cv); 2458 free(sc, M_ISCSI); 2459 return (0); 2460 } 2461 2462 static int 2463 iscsi_quiesce(void) 2464 { 2465 sx_slock(&sc->sc_lock); 2466 if (!TAILQ_EMPTY(&sc->sc_sessions)) { 2467 sx_sunlock(&sc->sc_lock); 2468 return (EBUSY); 2469 } 2470 sx_sunlock(&sc->sc_lock); 2471 return (0); 2472 } 2473 2474 static int 2475 iscsi_modevent(module_t mod, int what, void *arg) 2476 { 2477 int error; 2478 2479 switch (what) { 2480 case MOD_LOAD: 2481 error = iscsi_load(); 2482 break; 2483 case MOD_UNLOAD: 2484 error = iscsi_unload(); 2485 break; 2486 case MOD_QUIESCE: 2487 error = iscsi_quiesce(); 2488 break; 2489 default: 2490 error = EINVAL; 2491 break; 2492 } 2493 return (error); 2494 } 2495 2496 moduledata_t iscsi_data = { 2497 "iscsi", 2498 iscsi_modevent, 2499 0 2500 }; 2501 2502 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 2503 MODULE_DEPEND(iscsi, cam, 1, 1, 1); 2504 MODULE_DEPEND(iscsi, icl, 1, 1, 1); 2505