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