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