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