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