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 * $FreeBSD$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/condvar.h> 34 #include <sys/conf.h> 35 #include <sys/endian.h> 36 #include <sys/eventhandler.h> 37 #include <sys/file.h> 38 #include <sys/kernel.h> 39 #include <sys/kthread.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/mutex.h> 43 #include <sys/module.h> 44 #include <sys/sysctl.h> 45 #include <sys/systm.h> 46 #include <sys/sx.h> 47 #include <vm/uma.h> 48 49 #include <cam/cam.h> 50 #include <cam/cam_ccb.h> 51 #include <cam/cam_xpt.h> 52 #include <cam/cam_debug.h> 53 #include <cam/cam_sim.h> 54 #include <cam/cam_xpt_sim.h> 55 #include <cam/cam_xpt_periph.h> 56 #include <cam/cam_periph.h> 57 #include <cam/scsi/scsi_all.h> 58 #include <cam/scsi/scsi_message.h> 59 60 #include "iscsi_ioctl.h" 61 #include "iscsi.h" 62 #include "icl.h" 63 #include "iscsi_proto.h" 64 65 #ifdef ICL_KERNEL_PROXY 66 #include <sys/socketvar.h> 67 #endif 68 69 #ifdef ICL_KERNEL_PROXY 70 FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY"); 71 #endif 72 73 /* 74 * XXX: This is global so the iscsi_unload() can access it. 75 * Think about how to do this properly. 76 */ 77 static struct iscsi_softc *sc; 78 79 SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD, 0, "iSCSI initiator"); 80 static int debug = 1; 81 SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN, 82 &debug, 0, "Enable debug messages"); 83 static int ping_timeout = 5; 84 SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout, 85 0, "Timeout for ping (NOP-Out) requests, in seconds"); 86 static int iscsid_timeout = 60; 87 SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout, 88 0, "Time to wait for iscsid(8) to handle reconnection, in seconds"); 89 static int login_timeout = 60; 90 SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout, 91 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds"); 92 static int maxtags = 255; 93 SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags, 94 0, "Max number of IO requests queued"); 95 static int fail_on_disconnection = 0; 96 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN, 97 &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure"); 98 99 static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator"); 100 static uma_zone_t iscsi_outstanding_zone; 101 102 #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0) 103 #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn)) 104 105 #define ISCSI_DEBUG(X, ...) \ 106 do { \ 107 if (debug > 1) \ 108 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\ 109 } while (0) 110 111 #define ISCSI_WARN(X, ...) \ 112 do { \ 113 if (debug > 0) { \ 114 printf("WARNING: %s: " X "\n", \ 115 __func__, ## __VA_ARGS__); \ 116 } \ 117 } while (0) 118 119 #define ISCSI_SESSION_DEBUG(S, X, ...) \ 120 do { \ 121 if (debug > 1) { \ 122 printf("%s: %s (%s): " X "\n", \ 123 __func__, S->is_conf.isc_target_addr, \ 124 S->is_conf.isc_target, ## __VA_ARGS__); \ 125 } \ 126 } while (0) 127 128 #define ISCSI_SESSION_WARN(S, X, ...) \ 129 do { \ 130 if (debug > 0) { \ 131 printf("WARNING: %s (%s): " X "\n", \ 132 S->is_conf.isc_target_addr, \ 133 S->is_conf.isc_target, ## __VA_ARGS__); \ 134 } \ 135 } while (0) 136 137 #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock) 138 #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock) 139 #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED) 140 141 static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, 142 int mode, struct thread *td); 143 144 static struct cdevsw iscsi_cdevsw = { 145 .d_version = D_VERSION, 146 .d_ioctl = iscsi_ioctl, 147 .d_name = "iscsi", 148 }; 149 150 static void iscsi_pdu_queue_locked(struct icl_pdu *request); 151 static void iscsi_pdu_queue(struct icl_pdu *request); 152 static void iscsi_pdu_update_statsn(const struct icl_pdu *response); 153 static void iscsi_pdu_handle_nop_in(struct icl_pdu *response); 154 static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response); 155 static void iscsi_pdu_handle_data_in(struct icl_pdu *response); 156 static void iscsi_pdu_handle_logout_response(struct icl_pdu *response); 157 static void iscsi_pdu_handle_r2t(struct icl_pdu *response); 158 static void iscsi_pdu_handle_async_message(struct icl_pdu *response); 159 static void iscsi_pdu_handle_reject(struct icl_pdu *response); 160 static void iscsi_session_reconnect(struct iscsi_session *is); 161 static void iscsi_session_terminate(struct iscsi_session *is); 162 static void iscsi_action(struct cam_sim *sim, union ccb *ccb); 163 static void iscsi_poll(struct cam_sim *sim); 164 static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is, 165 uint32_t initiator_task_tag); 166 static int iscsi_outstanding_add(struct iscsi_session *is, 167 uint32_t initiator_task_tag, union ccb *ccb); 168 static void iscsi_outstanding_remove(struct iscsi_session *is, 169 struct iscsi_outstanding *io); 170 171 static bool 172 iscsi_pdu_prepare(struct icl_pdu *request) 173 { 174 struct iscsi_session *is; 175 struct iscsi_bhs_scsi_command *bhssc; 176 177 is = PDU_SESSION(request); 178 179 ISCSI_SESSION_LOCK_ASSERT(is); 180 181 /* 182 * We're only using fields common for all the request 183 * (initiator -> target) PDUs. 184 */ 185 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 186 187 /* 188 * Data-Out PDU does not contain CmdSN. 189 */ 190 if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) { 191 if (is->is_cmdsn > is->is_maxcmdsn && 192 (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) { 193 /* 194 * Current MaxCmdSN prevents us from sending any more 195 * SCSI Command PDUs to the target; postpone the PDU. 196 * It will get resent by either iscsi_pdu_queue(), 197 * or by maintenance thread. 198 */ 199 #if 0 200 ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %d, ExpCmdSN %d, MaxCmdSN %d, opcode 0x%x", 201 is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn, bhssc->bhssc_opcode); 202 #endif 203 return (true); 204 } 205 bhssc->bhssc_cmdsn = htonl(is->is_cmdsn); 206 if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) 207 is->is_cmdsn++; 208 } 209 bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1); 210 211 return (false); 212 } 213 214 static void 215 iscsi_session_send_postponed(struct iscsi_session *is) 216 { 217 struct icl_pdu *request; 218 bool postpone; 219 220 ISCSI_SESSION_LOCK_ASSERT(is); 221 222 while (!STAILQ_EMPTY(&is->is_postponed)) { 223 request = STAILQ_FIRST(&is->is_postponed); 224 postpone = iscsi_pdu_prepare(request); 225 if (postpone) 226 break; 227 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next); 228 icl_pdu_queue(request); 229 } 230 } 231 232 static void 233 iscsi_pdu_queue_locked(struct icl_pdu *request) 234 { 235 struct iscsi_session *is; 236 bool postpone; 237 238 is = PDU_SESSION(request); 239 ISCSI_SESSION_LOCK_ASSERT(is); 240 iscsi_session_send_postponed(is); 241 postpone = iscsi_pdu_prepare(request); 242 if (postpone) { 243 STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next); 244 return; 245 } 246 icl_pdu_queue(request); 247 } 248 249 static void 250 iscsi_pdu_queue(struct icl_pdu *request) 251 { 252 struct iscsi_session *is; 253 254 is = PDU_SESSION(request); 255 ISCSI_SESSION_LOCK(is); 256 iscsi_pdu_queue_locked(request); 257 ISCSI_SESSION_UNLOCK(is); 258 } 259 260 static void 261 iscsi_session_logout(struct iscsi_session *is) 262 { 263 struct icl_pdu *request; 264 struct iscsi_bhs_logout_request *bhslr; 265 266 request = icl_pdu_new_bhs(is->is_conn, M_NOWAIT); 267 if (request == NULL) 268 return; 269 270 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs; 271 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST; 272 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION; 273 iscsi_pdu_queue_locked(request); 274 } 275 276 static void 277 iscsi_session_terminate_tasks(struct iscsi_session *is, bool requeue) 278 { 279 struct iscsi_outstanding *io, *tmp; 280 281 ISCSI_SESSION_LOCK_ASSERT(is); 282 283 TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) { 284 if (requeue) { 285 io->io_ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 286 io->io_ccb->ccb_h.status |= CAM_REQUEUE_REQ; 287 } else { 288 io->io_ccb->ccb_h.status = CAM_REQ_ABORTED; 289 } 290 291 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 292 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 293 ISCSI_SESSION_DEBUG(is, "freezing devq"); 294 } 295 io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN; 296 xpt_done(io->io_ccb); 297 iscsi_outstanding_remove(is, io); 298 } 299 } 300 301 static void 302 iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim) 303 { 304 struct icl_pdu *pdu; 305 306 ISCSI_SESSION_LOCK_ASSERT(is); 307 308 /* 309 * Don't queue any new PDUs. 310 */ 311 if (is->is_sim != NULL && is->is_simq_frozen == false) { 312 ISCSI_SESSION_DEBUG(is, "freezing"); 313 xpt_freeze_simq(is->is_sim, 1); 314 is->is_simq_frozen = true; 315 } 316 317 /* 318 * Remove postponed PDUs. 319 */ 320 while (!STAILQ_EMPTY(&is->is_postponed)) { 321 pdu = STAILQ_FIRST(&is->is_postponed); 322 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next); 323 icl_pdu_free(pdu); 324 } 325 326 if (destroy_sim == false) { 327 /* 328 * Terminate SCSI tasks, asking CAM to requeue them. 329 */ 330 iscsi_session_terminate_tasks(is, true); 331 return; 332 } 333 334 iscsi_session_terminate_tasks(is, false); 335 336 if (is->is_sim == NULL) 337 return; 338 339 ISCSI_SESSION_DEBUG(is, "deregistering SIM"); 340 xpt_async(AC_LOST_DEVICE, is->is_path, NULL); 341 342 if (is->is_simq_frozen) { 343 xpt_release_simq(is->is_sim, 1); 344 is->is_simq_frozen = false; 345 } 346 347 xpt_free_path(is->is_path); 348 is->is_path = NULL; 349 xpt_bus_deregister(cam_sim_path(is->is_sim)); 350 cam_sim_free(is->is_sim, TRUE /*free_devq*/); 351 is->is_sim = NULL; 352 is->is_devq = NULL; 353 } 354 355 static void 356 iscsi_maintenance_thread_reconnect(struct iscsi_session *is) 357 { 358 359 icl_conn_shutdown(is->is_conn); 360 icl_conn_close(is->is_conn); 361 362 ISCSI_SESSION_LOCK(is); 363 364 is->is_connected = false; 365 is->is_reconnecting = false; 366 is->is_login_phase = false; 367 368 #ifdef ICL_KERNEL_PROXY 369 if (is->is_login_pdu != NULL) { 370 icl_pdu_free(is->is_login_pdu); 371 is->is_login_pdu = NULL; 372 } 373 cv_signal(&is->is_login_cv); 374 #endif 375 376 if (fail_on_disconnection) { 377 ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices"); 378 iscsi_session_cleanup(is, true); 379 } else { 380 iscsi_session_cleanup(is, false); 381 } 382 383 KASSERT(TAILQ_EMPTY(&is->is_outstanding), 384 ("destroying session with active tasks")); 385 KASSERT(STAILQ_EMPTY(&is->is_postponed), 386 ("destroying session with postponed PDUs")); 387 388 /* 389 * Request immediate reconnection from iscsid(8). 390 */ 391 //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)"); 392 is->is_waiting_for_iscsid = true; 393 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason)); 394 is->is_timeout = 0; 395 ISCSI_SESSION_UNLOCK(is); 396 cv_signal(&is->is_softc->sc_cv); 397 } 398 399 static void 400 iscsi_maintenance_thread_terminate(struct iscsi_session *is) 401 { 402 struct iscsi_softc *sc; 403 404 sc = is->is_softc; 405 sx_xlock(&sc->sc_lock); 406 TAILQ_REMOVE(&sc->sc_sessions, is, is_next); 407 sx_xunlock(&sc->sc_lock); 408 409 icl_conn_close(is->is_conn); 410 411 ISCSI_SESSION_LOCK(is); 412 413 KASSERT(is->is_terminating, ("is_terminating == false")); 414 415 #ifdef ICL_KERNEL_PROXY 416 if (is->is_login_pdu != NULL) { 417 icl_pdu_free(is->is_login_pdu); 418 is->is_login_pdu = NULL; 419 } 420 cv_signal(&is->is_login_cv); 421 #endif 422 423 callout_drain(&is->is_callout); 424 425 iscsi_session_cleanup(is, true); 426 427 KASSERT(TAILQ_EMPTY(&is->is_outstanding), 428 ("destroying session with active tasks")); 429 KASSERT(STAILQ_EMPTY(&is->is_postponed), 430 ("destroying session with postponed PDUs")); 431 432 ISCSI_SESSION_UNLOCK(is); 433 434 icl_conn_free(is->is_conn); 435 mtx_destroy(&is->is_lock); 436 cv_destroy(&is->is_maintenance_cv); 437 #ifdef ICL_KERNEL_PROXY 438 cv_destroy(&is->is_login_cv); 439 #endif 440 ISCSI_SESSION_DEBUG(is, "terminated"); 441 free(is, M_ISCSI); 442 443 /* 444 * The iscsi_unload() routine might be waiting. 445 */ 446 cv_signal(&sc->sc_cv); 447 } 448 449 static void 450 iscsi_maintenance_thread(void *arg) 451 { 452 struct iscsi_session *is; 453 454 is = arg; 455 456 for (;;) { 457 ISCSI_SESSION_LOCK(is); 458 if (is->is_reconnecting == false && 459 is->is_terminating == false && 460 STAILQ_EMPTY(&is->is_postponed)) 461 cv_wait(&is->is_maintenance_cv, &is->is_lock); 462 463 if (is->is_reconnecting) { 464 ISCSI_SESSION_UNLOCK(is); 465 iscsi_maintenance_thread_reconnect(is); 466 continue; 467 } 468 469 if (is->is_terminating) { 470 ISCSI_SESSION_UNLOCK(is); 471 iscsi_maintenance_thread_terminate(is); 472 kthread_exit(); 473 return; 474 } 475 476 iscsi_session_send_postponed(is); 477 ISCSI_SESSION_UNLOCK(is); 478 } 479 } 480 481 static void 482 iscsi_session_reconnect(struct iscsi_session *is) 483 { 484 485 /* 486 * XXX: We can't use locking here, because 487 * it's being called from various contexts. 488 * Hope it doesn't break anything. 489 */ 490 if (is->is_reconnecting) 491 return; 492 493 is->is_reconnecting = true; 494 cv_signal(&is->is_maintenance_cv); 495 } 496 497 static void 498 iscsi_session_terminate(struct iscsi_session *is) 499 { 500 if (is->is_terminating) 501 return; 502 503 is->is_terminating = true; 504 505 #if 0 506 iscsi_session_logout(is); 507 #endif 508 cv_signal(&is->is_maintenance_cv); 509 } 510 511 static void 512 iscsi_callout(void *context) 513 { 514 struct icl_pdu *request; 515 struct iscsi_bhs_nop_out *bhsno; 516 struct iscsi_session *is; 517 bool reconnect_needed = false; 518 519 is = context; 520 521 if (is->is_terminating) 522 return; 523 524 callout_schedule(&is->is_callout, 1 * hz); 525 526 ISCSI_SESSION_LOCK(is); 527 is->is_timeout++; 528 529 if (is->is_waiting_for_iscsid) { 530 if (is->is_timeout > iscsid_timeout) { 531 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) " 532 "for %d seconds; reconnecting", 533 is->is_timeout); 534 reconnect_needed = true; 535 } 536 goto out; 537 } 538 539 if (is->is_login_phase) { 540 if (is->is_timeout > login_timeout) { 541 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; " 542 "reconnecting", is->is_timeout); 543 reconnect_needed = true; 544 } 545 goto out; 546 } 547 548 if (is->is_timeout >= ping_timeout) { 549 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; " 550 "reconnecting", ping_timeout); 551 reconnect_needed = true; 552 goto out; 553 } 554 555 ISCSI_SESSION_UNLOCK(is); 556 557 /* 558 * If the ping was reset less than one second ago - which means 559 * that we've received some PDU during the last second - assume 560 * the traffic flows correctly and don't bother sending a NOP-Out. 561 * 562 * (It's 2 - one for one second, and one for incrementing is_timeout 563 * earlier in this routine.) 564 */ 565 if (is->is_timeout < 2) 566 return; 567 568 request = icl_pdu_new_bhs(is->is_conn, M_NOWAIT); 569 if (request == NULL) { 570 ISCSI_SESSION_WARN(is, "failed to allocate PDU"); 571 return; 572 } 573 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs; 574 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT | 575 ISCSI_BHS_OPCODE_IMMEDIATE; 576 bhsno->bhsno_flags = 0x80; 577 bhsno->bhsno_target_transfer_tag = 0xffffffff; 578 iscsi_pdu_queue(request); 579 return; 580 581 out: 582 ISCSI_SESSION_UNLOCK(is); 583 584 if (reconnect_needed) 585 iscsi_session_reconnect(is); 586 } 587 588 static void 589 iscsi_pdu_update_statsn(const struct icl_pdu *response) 590 { 591 const struct iscsi_bhs_data_in *bhsdi; 592 struct iscsi_session *is; 593 uint32_t expcmdsn, maxcmdsn; 594 595 is = PDU_SESSION(response); 596 597 ISCSI_SESSION_LOCK_ASSERT(is); 598 599 /* 600 * We're only using fields common for all the response 601 * (target -> initiator) PDUs. 602 */ 603 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs; 604 /* 605 * Ok, I lied. In case of Data-In, "The fields StatSN, Status, 606 * and Residual Count only have meaningful content if the S bit 607 * is set to 1", so we also need to check the bit specific for 608 * Data-In PDU. 609 */ 610 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN || 611 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) { 612 if (ntohl(bhsdi->bhsdi_statsn) < is->is_statsn) { 613 ISCSI_SESSION_WARN(is, 614 "PDU StatSN %d >= session StatSN %d, opcode 0x%x", 615 is->is_statsn, ntohl(bhsdi->bhsdi_statsn), 616 bhsdi->bhsdi_opcode); 617 } 618 is->is_statsn = ntohl(bhsdi->bhsdi_statsn); 619 } 620 621 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn); 622 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn); 623 624 /* 625 * XXX: Compare using Serial Arithmetic Sense. 626 */ 627 if (maxcmdsn + 1 < expcmdsn) { 628 ISCSI_SESSION_DEBUG(is, "PDU MaxCmdSN %d + 1 < PDU ExpCmdSN %d; ignoring", 629 maxcmdsn, expcmdsn); 630 } else { 631 if (maxcmdsn > is->is_maxcmdsn) { 632 is->is_maxcmdsn = maxcmdsn; 633 634 /* 635 * Command window increased; kick the maintanance thread 636 * to send out postponed commands. 637 */ 638 if (!STAILQ_EMPTY(&is->is_postponed)) 639 cv_signal(&is->is_maintenance_cv); 640 } else if (maxcmdsn < is->is_maxcmdsn) { 641 ISCSI_SESSION_DEBUG(is, "PDU MaxCmdSN %d < session MaxCmdSN %d; ignoring", 642 maxcmdsn, is->is_maxcmdsn); 643 } 644 645 if (expcmdsn > is->is_expcmdsn) { 646 is->is_expcmdsn = expcmdsn; 647 } else if (expcmdsn < is->is_expcmdsn) { 648 ISCSI_SESSION_DEBUG(is, "PDU ExpCmdSN %d < session ExpCmdSN %d; ignoring", 649 expcmdsn, is->is_expcmdsn); 650 } 651 } 652 653 /* 654 * Every incoming PDU - not just NOP-In - resets the ping timer. 655 * The purpose of the timeout is to reset the connection when it stalls; 656 * we don't want this to happen when NOP-In or NOP-Out ends up delayed 657 * in some queue. 658 */ 659 is->is_timeout = 0; 660 } 661 662 static void 663 iscsi_receive_callback(struct icl_pdu *response) 664 { 665 struct iscsi_session *is; 666 667 is = PDU_SESSION(response); 668 669 ISCSI_SESSION_LOCK(is); 670 671 #ifdef ICL_KERNEL_PROXY 672 if (is->is_login_phase) { 673 if (is->is_login_pdu == NULL) 674 is->is_login_pdu = response; 675 else 676 icl_pdu_free(response); 677 ISCSI_SESSION_UNLOCK(is); 678 cv_signal(&is->is_login_cv); 679 return; 680 } 681 #endif 682 683 iscsi_pdu_update_statsn(response); 684 685 /* 686 * The handling routine is responsible for freeing the PDU 687 * when it's no longer needed. 688 */ 689 switch (response->ip_bhs->bhs_opcode) { 690 case ISCSI_BHS_OPCODE_NOP_IN: 691 iscsi_pdu_handle_nop_in(response); 692 break; 693 case ISCSI_BHS_OPCODE_SCSI_RESPONSE: 694 iscsi_pdu_handle_scsi_response(response); 695 break; 696 case ISCSI_BHS_OPCODE_SCSI_DATA_IN: 697 iscsi_pdu_handle_data_in(response); 698 break; 699 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE: 700 iscsi_pdu_handle_logout_response(response); 701 break; 702 case ISCSI_BHS_OPCODE_R2T: 703 iscsi_pdu_handle_r2t(response); 704 break; 705 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE: 706 iscsi_pdu_handle_async_message(response); 707 break; 708 case ISCSI_BHS_OPCODE_REJECT: 709 iscsi_pdu_handle_reject(response); 710 break; 711 default: 712 ISCSI_SESSION_WARN(is, "received PDU with unsupported " 713 "opcode 0x%x; reconnecting", 714 response->ip_bhs->bhs_opcode); 715 iscsi_session_reconnect(is); 716 icl_pdu_free(response); 717 } 718 719 ISCSI_SESSION_UNLOCK(is); 720 } 721 722 static void 723 iscsi_error_callback(struct icl_conn *ic) 724 { 725 struct iscsi_session *is; 726 727 is = CONN_SESSION(ic); 728 729 ISCSI_SESSION_WARN(is, "connection error; reconnecting"); 730 iscsi_session_reconnect(is); 731 } 732 733 static void 734 iscsi_pdu_handle_nop_in(struct icl_pdu *response) 735 { 736 struct iscsi_session *is; 737 struct iscsi_bhs_nop_out *bhsno; 738 struct iscsi_bhs_nop_in *bhsni; 739 struct icl_pdu *request; 740 void *data = NULL; 741 size_t datasize; 742 int error; 743 744 is = PDU_SESSION(response); 745 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs; 746 747 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) { 748 /* 749 * Nothing to do; iscsi_pdu_update_statsn() already 750 * zeroed the timeout. 751 */ 752 icl_pdu_free(response); 753 return; 754 } 755 756 datasize = icl_pdu_data_segment_length(response); 757 if (datasize > 0) { 758 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO); 759 if (data == NULL) { 760 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 761 "reconnecting"); 762 icl_pdu_free(response); 763 iscsi_session_reconnect(is); 764 return; 765 } 766 icl_pdu_get_data(response, 0, data, datasize); 767 } 768 769 request = icl_pdu_new_bhs(response->ip_conn, M_NOWAIT); 770 if (request == NULL) { 771 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 772 "reconnecting"); 773 free(data, M_ISCSI); 774 icl_pdu_free(response); 775 iscsi_session_reconnect(is); 776 return; 777 } 778 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs; 779 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT | 780 ISCSI_BHS_OPCODE_IMMEDIATE; 781 bhsno->bhsno_flags = 0x80; 782 bhsno->bhsno_initiator_task_tag = 0xffffffff; 783 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag; 784 if (datasize > 0) { 785 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT); 786 if (error != 0) { 787 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 788 "reconnecting"); 789 free(data, M_ISCSI); 790 icl_pdu_free(request); 791 icl_pdu_free(response); 792 iscsi_session_reconnect(is); 793 return; 794 } 795 free(data, M_ISCSI); 796 } 797 798 icl_pdu_free(response); 799 iscsi_pdu_queue_locked(request); 800 } 801 802 static void 803 iscsi_pdu_handle_scsi_response(struct icl_pdu *response) 804 { 805 struct iscsi_bhs_scsi_response *bhssr; 806 struct iscsi_outstanding *io; 807 struct iscsi_session *is; 808 struct ccb_scsiio *csio; 809 size_t data_segment_len; 810 uint16_t sense_len; 811 812 is = PDU_SESSION(response); 813 814 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 815 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag); 816 if (io == NULL) { 817 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag); 818 icl_pdu_free(response); 819 iscsi_session_reconnect(is); 820 return; 821 } 822 823 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) { 824 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response); 825 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 826 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 827 ISCSI_SESSION_DEBUG(is, "freezing devq"); 828 } 829 io->io_ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN; 830 } else if (bhssr->bhssr_status == 0) { 831 io->io_ccb->ccb_h.status = CAM_REQ_CMP; 832 } else { 833 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 834 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 835 ISCSI_SESSION_DEBUG(is, "freezing devq"); 836 } 837 io->io_ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN; 838 io->io_ccb->csio.scsi_status = bhssr->bhssr_status; 839 } 840 841 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) { 842 ISCSI_SESSION_WARN(is, "target indicated residual overflow"); 843 icl_pdu_free(response); 844 iscsi_session_reconnect(is); 845 return; 846 } 847 848 csio = &io->io_ccb->csio; 849 850 data_segment_len = icl_pdu_data_segment_length(response); 851 if (data_segment_len > 0) { 852 if (data_segment_len < sizeof(sense_len)) { 853 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)", 854 data_segment_len); 855 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 856 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 857 ISCSI_SESSION_DEBUG(is, "freezing devq"); 858 } 859 io->io_ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN; 860 goto out; 861 } 862 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len)); 863 sense_len = ntohs(sense_len); 864 #if 0 865 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd", 866 sense_len, data_segment_len); 867 #endif 868 if (sizeof(sense_len) + sense_len > data_segment_len) { 869 ISCSI_SESSION_WARN(is, "truncated data segment " 870 "(%zd bytes, should be %zd)", 871 data_segment_len, sizeof(sense_len) + sense_len); 872 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 873 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 874 ISCSI_SESSION_DEBUG(is, "freezing devq"); 875 } 876 io->io_ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN; 877 goto out; 878 } else if (sizeof(sense_len) + sense_len < data_segment_len) 879 ISCSI_SESSION_WARN(is, "oversize data segment " 880 "(%zd bytes, should be %zd)", 881 data_segment_len, sizeof(sense_len) + sense_len); 882 if (sense_len > csio->sense_len) { 883 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d", 884 sense_len, csio->sense_len); 885 sense_len = csio->sense_len; 886 } 887 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len); 888 csio->sense_resid = csio->sense_len - sense_len; 889 io->io_ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 890 } 891 892 out: 893 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) 894 csio->resid = ntohl(bhssr->bhssr_residual_count); 895 896 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 897 KASSERT(io->io_received <= csio->dxfer_len, 898 ("io->io_received > csio->dxfer_len")); 899 if (io->io_received < csio->dxfer_len) { 900 if (csio->resid != csio->dxfer_len - io->io_received) { 901 ISCSI_SESSION_WARN(is, "underflow mismatch: " 902 "target indicates %d, we calculated %zd", 903 csio->resid, 904 csio->dxfer_len - io->io_received); 905 } 906 csio->resid = csio->dxfer_len - io->io_received; 907 } 908 } 909 910 xpt_done(io->io_ccb); 911 iscsi_outstanding_remove(is, io); 912 icl_pdu_free(response); 913 } 914 915 static void 916 iscsi_pdu_handle_data_in(struct icl_pdu *response) 917 { 918 struct iscsi_bhs_data_in *bhsdi; 919 struct iscsi_outstanding *io; 920 struct iscsi_session *is; 921 struct ccb_scsiio *csio; 922 size_t data_segment_len; 923 924 is = PDU_SESSION(response); 925 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs; 926 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag); 927 if (io == NULL) { 928 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag); 929 icl_pdu_free(response); 930 iscsi_session_reconnect(is); 931 return; 932 } 933 934 data_segment_len = icl_pdu_data_segment_length(response); 935 if (data_segment_len == 0) { 936 /* 937 * "The sending of 0 length data segments should be avoided, 938 * but initiators and targets MUST be able to properly receive 939 * 0 length data segments." 940 */ 941 icl_pdu_free(response); 942 return; 943 } 944 945 /* 946 * We need to track this for security reasons - without it, malicious target 947 * could respond to SCSI READ without sending Data-In PDUs, which would result 948 * in read operation on the initiator side returning random kernel data. 949 */ 950 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) { 951 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd", 952 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset)); 953 icl_pdu_free(response); 954 iscsi_session_reconnect(is); 955 return; 956 } 957 958 csio = &io->io_ccb->csio; 959 960 if (io->io_received + data_segment_len > csio->dxfer_len) { 961 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes " 962 "at offset %zd, buffer is %d)", 963 data_segment_len, io->io_received, csio->dxfer_len); 964 icl_pdu_free(response); 965 iscsi_session_reconnect(is); 966 return; 967 } 968 969 icl_pdu_get_data(response, 0, csio->data_ptr + io->io_received, data_segment_len); 970 io->io_received += data_segment_len; 971 972 /* 973 * XXX: Check DataSN. 974 * XXX: Check F. 975 */ 976 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) { 977 /* 978 * Nothing more to do. 979 */ 980 icl_pdu_free(response); 981 return; 982 } 983 984 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status); 985 if (bhsdi->bhsdi_status == 0) { 986 io->io_ccb->ccb_h.status = CAM_REQ_CMP; 987 } else { 988 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 989 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1); 990 ISCSI_SESSION_DEBUG(is, "freezing devq"); 991 } 992 io->io_ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN; 993 csio->scsi_status = bhsdi->bhsdi_status; 994 } 995 996 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 997 KASSERT(io->io_received <= csio->dxfer_len, 998 ("io->io_received > csio->dxfer_len")); 999 if (io->io_received < csio->dxfer_len) { 1000 csio->resid = ntohl(bhsdi->bhsdi_residual_count); 1001 if (csio->resid != csio->dxfer_len - io->io_received) { 1002 ISCSI_SESSION_WARN(is, "underflow mismatch: " 1003 "target indicates %d, we calculated %zd", 1004 csio->resid, 1005 csio->dxfer_len - io->io_received); 1006 } 1007 csio->resid = csio->dxfer_len - io->io_received; 1008 } 1009 } 1010 1011 xpt_done(io->io_ccb); 1012 iscsi_outstanding_remove(is, io); 1013 icl_pdu_free(response); 1014 } 1015 1016 static void 1017 iscsi_pdu_handle_logout_response(struct icl_pdu *response) 1018 { 1019 1020 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response"); 1021 icl_pdu_free(response); 1022 } 1023 1024 static void 1025 iscsi_pdu_handle_r2t(struct icl_pdu *response) 1026 { 1027 struct icl_pdu *request; 1028 struct iscsi_session *is; 1029 struct iscsi_bhs_r2t *bhsr2t; 1030 struct iscsi_bhs_data_out *bhsdo; 1031 struct iscsi_outstanding *io; 1032 struct ccb_scsiio *csio; 1033 size_t off, len, total_len; 1034 int error; 1035 1036 is = PDU_SESSION(response); 1037 1038 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs; 1039 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag); 1040 if (io == NULL) { 1041 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting", 1042 bhsr2t->bhsr2t_initiator_task_tag); 1043 icl_pdu_free(response); 1044 iscsi_session_reconnect(is); 1045 return; 1046 } 1047 1048 csio = &io->io_ccb->csio; 1049 1050 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) { 1051 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting"); 1052 icl_pdu_free(response); 1053 iscsi_session_reconnect(is); 1054 return; 1055 } 1056 1057 /* 1058 * XXX: Verify R2TSN. 1059 */ 1060 1061 io->io_datasn = 0; 1062 1063 off = ntohl(bhsr2t->bhsr2t_buffer_offset); 1064 if (off > csio->dxfer_len) { 1065 ISCSI_SESSION_WARN(is, "target requested invalid offset " 1066 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len); 1067 icl_pdu_free(response); 1068 iscsi_session_reconnect(is); 1069 return; 1070 } 1071 1072 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length); 1073 if (total_len == 0 || total_len > csio->dxfer_len) { 1074 ISCSI_SESSION_WARN(is, "target requested invalid length " 1075 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len); 1076 icl_pdu_free(response); 1077 iscsi_session_reconnect(is); 1078 return; 1079 } 1080 1081 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len); 1082 1083 for (;;) { 1084 len = total_len; 1085 1086 if (len > is->is_max_data_segment_length) 1087 len = is->is_max_data_segment_length; 1088 1089 if (off + len > csio->dxfer_len) { 1090 ISCSI_SESSION_WARN(is, "target requested invalid " 1091 "length/offset %zd, buffer is %d; reconnecting", 1092 off + len, csio->dxfer_len); 1093 icl_pdu_free(response); 1094 iscsi_session_reconnect(is); 1095 return; 1096 } 1097 1098 request = icl_pdu_new_bhs(response->ip_conn, M_NOWAIT); 1099 if (request == NULL) { 1100 icl_pdu_free(response); 1101 iscsi_session_reconnect(is); 1102 return; 1103 } 1104 1105 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs; 1106 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT; 1107 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun; 1108 bhsdo->bhsdo_initiator_task_tag = 1109 bhsr2t->bhsr2t_initiator_task_tag; 1110 bhsdo->bhsdo_target_transfer_tag = 1111 bhsr2t->bhsr2t_target_transfer_tag; 1112 bhsdo->bhsdo_datasn = htonl(io->io_datasn++); 1113 bhsdo->bhsdo_buffer_offset = htonl(off); 1114 error = icl_pdu_append_data(request, csio->data_ptr + off, len, 1115 M_NOWAIT); 1116 if (error != 0) { 1117 ISCSI_SESSION_WARN(is, "failed to allocate memory; " 1118 "reconnecting"); 1119 icl_pdu_free(request); 1120 icl_pdu_free(response); 1121 iscsi_session_reconnect(is); 1122 return; 1123 } 1124 1125 off += len; 1126 total_len -= len; 1127 1128 if (total_len == 0) { 1129 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F; 1130 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off); 1131 } else { 1132 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off); 1133 } 1134 1135 iscsi_pdu_queue_locked(request); 1136 1137 if (total_len == 0) 1138 break; 1139 } 1140 1141 icl_pdu_free(response); 1142 } 1143 1144 static void 1145 iscsi_pdu_handle_async_message(struct icl_pdu *response) 1146 { 1147 struct iscsi_bhs_asynchronous_message *bhsam; 1148 struct iscsi_session *is; 1149 1150 is = PDU_SESSION(response); 1151 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs; 1152 switch (bhsam->bhsam_async_event) { 1153 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT: 1154 ISCSI_SESSION_WARN(is, "target requests logout; removing session"); 1155 iscsi_session_logout(is); 1156 iscsi_session_terminate(is); 1157 break; 1158 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION: 1159 ISCSI_SESSION_WARN(is, "target indicates it will drop drop the connection"); 1160 break; 1161 case BHSAM_EVENT_TARGET_TERMINATES_SESSION: 1162 ISCSI_SESSION_WARN(is, "target indicates it will drop drop the session"); 1163 break; 1164 default: 1165 /* 1166 * XXX: Technically, we're obligated to also handle 1167 * parameter renegotiation. 1168 */ 1169 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event); 1170 break; 1171 } 1172 1173 icl_pdu_free(response); 1174 } 1175 1176 static void 1177 iscsi_pdu_handle_reject(struct icl_pdu *response) 1178 { 1179 struct iscsi_bhs_reject *bhsr; 1180 struct iscsi_session *is; 1181 1182 is = PDU_SESSION(response); 1183 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs; 1184 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?", 1185 bhsr->bhsr_reason); 1186 1187 icl_pdu_free(response); 1188 } 1189 1190 static int 1191 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc, 1192 struct iscsi_daemon_request *request) 1193 { 1194 struct iscsi_session *is; 1195 int error; 1196 1197 sx_slock(&sc->sc_lock); 1198 for (;;) { 1199 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1200 ISCSI_SESSION_LOCK(is); 1201 if (is->is_waiting_for_iscsid) 1202 break; 1203 ISCSI_SESSION_UNLOCK(is); 1204 } 1205 1206 if (is == NULL) { 1207 /* 1208 * No session requires attention from iscsid(8); wait. 1209 */ 1210 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock); 1211 if (error != 0) { 1212 sx_sunlock(&sc->sc_lock); 1213 return (error); 1214 } 1215 continue; 1216 } 1217 1218 is->is_waiting_for_iscsid = false; 1219 is->is_login_phase = true; 1220 is->is_reason[0] = '\0'; 1221 ISCSI_SESSION_UNLOCK(is); 1222 1223 request->idr_session_id = is->is_id; 1224 memcpy(&request->idr_isid, &is->is_isid, 1225 sizeof(request->idr_isid)); 1226 request->idr_tsih = 0; /* New or reinstated session. */ 1227 memcpy(&request->idr_conf, &is->is_conf, 1228 sizeof(request->idr_conf)); 1229 1230 sx_sunlock(&sc->sc_lock); 1231 return (0); 1232 } 1233 } 1234 1235 static int 1236 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc, 1237 struct iscsi_daemon_handoff *handoff) 1238 { 1239 struct iscsi_session *is; 1240 int error; 1241 1242 sx_slock(&sc->sc_lock); 1243 1244 /* 1245 * Find the session to hand off socket to. 1246 */ 1247 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1248 if (is->is_id == handoff->idh_session_id) 1249 break; 1250 } 1251 if (is == NULL) { 1252 sx_sunlock(&sc->sc_lock); 1253 return (ESRCH); 1254 } 1255 ISCSI_SESSION_LOCK(is); 1256 if (is->is_conf.isc_discovery || is->is_terminating) { 1257 ISCSI_SESSION_UNLOCK(is); 1258 sx_sunlock(&sc->sc_lock); 1259 return (EINVAL); 1260 } 1261 if (is->is_connected) { 1262 /* 1263 * This might have happened because another iscsid(8) 1264 * instance handed off the connection in the meantime. 1265 * Just return. 1266 */ 1267 ISCSI_SESSION_WARN(is, "handoff on already connected " 1268 "session"); 1269 ISCSI_SESSION_UNLOCK(is); 1270 sx_sunlock(&sc->sc_lock); 1271 return (EBUSY); 1272 } 1273 1274 strlcpy(is->is_target_alias, handoff->idh_target_alias, 1275 sizeof(is->is_target_alias)); 1276 is->is_tsih = handoff->idh_tsih; 1277 is->is_statsn = handoff->idh_statsn; 1278 is->is_initial_r2t = handoff->idh_initial_r2t; 1279 is->is_immediate_data = handoff->idh_immediate_data; 1280 is->is_max_data_segment_length = handoff->idh_max_data_segment_length; 1281 is->is_max_burst_length = handoff->idh_max_burst_length; 1282 is->is_first_burst_length = handoff->idh_first_burst_length; 1283 1284 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C) 1285 is->is_conn->ic_header_crc32c = true; 1286 else 1287 is->is_conn->ic_header_crc32c = false; 1288 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C) 1289 is->is_conn->ic_data_crc32c = true; 1290 else 1291 is->is_conn->ic_data_crc32c = false; 1292 1293 is->is_cmdsn = 0; 1294 is->is_expcmdsn = 0; 1295 is->is_maxcmdsn = 0; 1296 is->is_waiting_for_iscsid = false; 1297 is->is_login_phase = false; 1298 is->is_timeout = 0; 1299 is->is_connected = true; 1300 is->is_reason[0] = '\0'; 1301 1302 ISCSI_SESSION_UNLOCK(is); 1303 1304 #ifdef ICL_KERNEL_PROXY 1305 if (handoff->idh_socket != 0) { 1306 #endif 1307 /* 1308 * Handoff without using ICL proxy. 1309 */ 1310 error = icl_conn_handoff(is->is_conn, handoff->idh_socket); 1311 if (error != 0) { 1312 sx_sunlock(&sc->sc_lock); 1313 iscsi_session_terminate(is); 1314 return (error); 1315 } 1316 #ifdef ICL_KERNEL_PROXY 1317 } 1318 #endif 1319 1320 sx_sunlock(&sc->sc_lock); 1321 1322 if (is->is_sim != NULL) { 1323 /* 1324 * When reconnecting, there already is SIM allocated for the session. 1325 */ 1326 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq")); 1327 ISCSI_SESSION_LOCK(is); 1328 ISCSI_SESSION_DEBUG(is, "releasing"); 1329 xpt_release_simq(is->is_sim, 1); 1330 is->is_simq_frozen = false; 1331 ISCSI_SESSION_UNLOCK(is); 1332 1333 } else { 1334 ISCSI_SESSION_LOCK(is); 1335 is->is_devq = cam_simq_alloc(maxtags); 1336 if (is->is_devq == NULL) { 1337 ISCSI_SESSION_WARN(is, "failed to allocate simq"); 1338 iscsi_session_terminate(is); 1339 return (ENOMEM); 1340 } 1341 1342 is->is_sim = cam_sim_alloc(iscsi_action, iscsi_poll, "iscsi", 1343 is, is->is_id /* unit */, &is->is_lock, 1344 1, maxtags, is->is_devq); 1345 if (is->is_sim == NULL) { 1346 ISCSI_SESSION_UNLOCK(is); 1347 ISCSI_SESSION_WARN(is, "failed to allocate SIM"); 1348 cam_simq_free(is->is_devq); 1349 iscsi_session_terminate(is); 1350 return (ENOMEM); 1351 } 1352 1353 error = xpt_bus_register(is->is_sim, NULL, 0); 1354 if (error != 0) { 1355 ISCSI_SESSION_UNLOCK(is); 1356 ISCSI_SESSION_WARN(is, "failed to register bus"); 1357 iscsi_session_terminate(is); 1358 return (ENOMEM); 1359 } 1360 1361 error = xpt_create_path(&is->is_path, /*periph*/NULL, 1362 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD, 1363 CAM_LUN_WILDCARD); 1364 if (error != CAM_REQ_CMP) { 1365 ISCSI_SESSION_UNLOCK(is); 1366 ISCSI_SESSION_WARN(is, "failed to create path"); 1367 iscsi_session_terminate(is); 1368 return (ENOMEM); 1369 } 1370 ISCSI_SESSION_UNLOCK(is); 1371 } 1372 1373 return (0); 1374 } 1375 1376 static int 1377 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc, 1378 struct iscsi_daemon_fail *fail) 1379 { 1380 struct iscsi_session *is; 1381 1382 sx_slock(&sc->sc_lock); 1383 1384 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1385 if (is->is_id == fail->idf_session_id) 1386 break; 1387 } 1388 if (is == NULL) { 1389 sx_sunlock(&sc->sc_lock); 1390 return (ESRCH); 1391 } 1392 ISCSI_SESSION_LOCK(is); 1393 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s", 1394 fail->idf_reason); 1395 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason)); 1396 //is->is_waiting_for_iscsid = false; 1397 //is->is_login_phase = true; 1398 //iscsi_session_reconnect(is); 1399 ISCSI_SESSION_UNLOCK(is); 1400 sx_sunlock(&sc->sc_lock); 1401 1402 return (0); 1403 } 1404 1405 #ifdef ICL_KERNEL_PROXY 1406 static int 1407 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc, 1408 struct iscsi_daemon_connect *idc) 1409 { 1410 struct iscsi_session *is; 1411 struct sockaddr *from_sa, *to_sa; 1412 int error; 1413 1414 sx_slock(&sc->sc_lock); 1415 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1416 if (is->is_id == idc->idc_session_id) 1417 break; 1418 } 1419 if (is == NULL) { 1420 sx_sunlock(&sc->sc_lock); 1421 return (ESRCH); 1422 } 1423 sx_sunlock(&sc->sc_lock); 1424 1425 if (idc->idc_from_addrlen > 0) { 1426 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen); 1427 if (error != 0) { 1428 ISCSI_SESSION_WARN(is, 1429 "getsockaddr failed with error %d", error); 1430 return (error); 1431 } 1432 } else { 1433 from_sa = NULL; 1434 } 1435 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen); 1436 if (error != 0) { 1437 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d", 1438 error); 1439 free(from_sa, M_SONAME); 1440 return (error); 1441 } 1442 1443 ISCSI_SESSION_LOCK(is); 1444 is->is_waiting_for_iscsid = false; 1445 is->is_login_phase = true; 1446 is->is_timeout = 0; 1447 ISCSI_SESSION_UNLOCK(is); 1448 1449 error = icl_conn_connect(is->is_conn, idc->idc_iser, idc->idc_domain, 1450 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa); 1451 free(from_sa, M_SONAME); 1452 free(to_sa, M_SONAME); 1453 1454 /* 1455 * Digests are always disabled during login phase. 1456 */ 1457 is->is_conn->ic_header_crc32c = false; 1458 is->is_conn->ic_data_crc32c = false; 1459 1460 return (error); 1461 } 1462 1463 static int 1464 iscsi_ioctl_daemon_send(struct iscsi_softc *sc, 1465 struct iscsi_daemon_send *ids) 1466 { 1467 struct iscsi_session *is; 1468 struct icl_pdu *ip; 1469 size_t datalen; 1470 void *data; 1471 int error; 1472 1473 sx_slock(&sc->sc_lock); 1474 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1475 if (is->is_id == ids->ids_session_id) 1476 break; 1477 } 1478 if (is == NULL) { 1479 sx_sunlock(&sc->sc_lock); 1480 return (ESRCH); 1481 } 1482 sx_sunlock(&sc->sc_lock); 1483 1484 if (is->is_login_phase == false) 1485 return (EBUSY); 1486 1487 if (is->is_terminating || is->is_reconnecting) 1488 return (EIO); 1489 1490 datalen = ids->ids_data_segment_len; 1491 if (datalen > ISCSI_MAX_DATA_SEGMENT_LENGTH) 1492 return (EINVAL); 1493 if (datalen > 0) { 1494 data = malloc(datalen, M_ISCSI, M_WAITOK); 1495 error = copyin(ids->ids_data_segment, data, datalen); 1496 if (error != 0) { 1497 free(data, M_ISCSI); 1498 return (error); 1499 } 1500 } 1501 1502 ip = icl_pdu_new_bhs(is->is_conn, M_WAITOK); 1503 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs)); 1504 if (datalen > 0) { 1505 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK); 1506 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed")); 1507 free(data, M_ISCSI); 1508 } 1509 icl_pdu_queue(ip); 1510 1511 return (0); 1512 } 1513 1514 static int 1515 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc, 1516 struct iscsi_daemon_receive *idr) 1517 { 1518 struct iscsi_session *is; 1519 struct icl_pdu *ip; 1520 void *data; 1521 1522 sx_slock(&sc->sc_lock); 1523 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1524 if (is->is_id == idr->idr_session_id) 1525 break; 1526 } 1527 if (is == NULL) { 1528 sx_sunlock(&sc->sc_lock); 1529 return (ESRCH); 1530 } 1531 sx_sunlock(&sc->sc_lock); 1532 1533 if (is->is_login_phase == false) 1534 return (EBUSY); 1535 1536 ISCSI_SESSION_LOCK(is); 1537 while (is->is_login_pdu == NULL && 1538 is->is_terminating == false && 1539 is->is_reconnecting == false) 1540 cv_wait(&is->is_login_cv, &is->is_lock); 1541 if (is->is_terminating || is->is_reconnecting) { 1542 ISCSI_SESSION_UNLOCK(is); 1543 return (EIO); 1544 } 1545 ip = is->is_login_pdu; 1546 is->is_login_pdu = NULL; 1547 ISCSI_SESSION_UNLOCK(is); 1548 1549 if (ip->ip_data_len > idr->idr_data_segment_len) { 1550 icl_pdu_free(ip); 1551 return (EMSGSIZE); 1552 } 1553 1554 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs)); 1555 if (ip->ip_data_len > 0) { 1556 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK); 1557 icl_pdu_get_data(ip, 0, data, ip->ip_data_len); 1558 copyout(data, idr->idr_data_segment, ip->ip_data_len); 1559 free(data, M_ISCSI); 1560 } 1561 1562 icl_pdu_free(ip); 1563 1564 return (0); 1565 } 1566 #endif /* ICL_KERNEL_PROXY */ 1567 1568 static void 1569 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc) 1570 { 1571 /* 1572 * Just make sure all the fields are null-terminated. 1573 * 1574 * XXX: This is not particularly secure. We should 1575 * create our own conf and then copy in relevant 1576 * fields. 1577 */ 1578 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0'; 1579 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0'; 1580 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0'; 1581 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0'; 1582 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0'; 1583 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0'; 1584 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0'; 1585 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0'; 1586 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0'; 1587 } 1588 1589 static bool 1590 iscsi_valid_session_conf(const struct iscsi_session_conf *isc) 1591 { 1592 1593 if (isc->isc_initiator[0] == '\0') { 1594 ISCSI_DEBUG("empty isc_initiator"); 1595 return (false); 1596 } 1597 1598 if (isc->isc_target_addr[0] == '\0') { 1599 ISCSI_DEBUG("empty isc_target_addr"); 1600 return (false); 1601 } 1602 1603 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) { 1604 ISCSI_DEBUG("non-empty isc_target for discovery session"); 1605 return (false); 1606 } 1607 1608 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) { 1609 ISCSI_DEBUG("empty isc_target for non-discovery session"); 1610 return (false); 1611 } 1612 1613 return (true); 1614 } 1615 1616 static int 1617 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa) 1618 { 1619 struct iscsi_session *is; 1620 const struct iscsi_session *is2; 1621 int error; 1622 1623 iscsi_sanitize_session_conf(&isa->isa_conf); 1624 if (iscsi_valid_session_conf(&isa->isa_conf) == false) 1625 return (EINVAL); 1626 1627 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK); 1628 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf)); 1629 1630 sx_xlock(&sc->sc_lock); 1631 1632 /* 1633 * Prevent duplicates. 1634 */ 1635 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) { 1636 if (!!is->is_conf.isc_discovery != 1637 !!is2->is_conf.isc_discovery) 1638 continue; 1639 1640 if (strcmp(is->is_conf.isc_target_addr, 1641 is2->is_conf.isc_target_addr) != 0) 1642 continue; 1643 1644 if (is->is_conf.isc_discovery == 0 && 1645 strcmp(is->is_conf.isc_target, 1646 is2->is_conf.isc_target) != 0) 1647 continue; 1648 1649 sx_xunlock(&sc->sc_lock); 1650 free(is, M_ISCSI); 1651 return (EBUSY); 1652 } 1653 1654 is->is_conn = icl_conn_new("iscsi", &is->is_lock); 1655 is->is_conn->ic_receive = iscsi_receive_callback; 1656 is->is_conn->ic_error = iscsi_error_callback; 1657 is->is_conn->ic_prv0 = is; 1658 TAILQ_INIT(&is->is_outstanding); 1659 STAILQ_INIT(&is->is_postponed); 1660 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF); 1661 cv_init(&is->is_maintenance_cv, "iscsi_mt"); 1662 #ifdef ICL_KERNEL_PROXY 1663 cv_init(&is->is_login_cv, "iscsi_login"); 1664 #endif 1665 1666 is->is_softc = sc; 1667 sc->sc_last_session_id++; 1668 is->is_id = sc->sc_last_session_id; 1669 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */ 1670 arc4rand(&is->is_isid[1], 5, 0); 1671 is->is_tsih = 0; 1672 callout_init(&is->is_callout, 1); 1673 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is); 1674 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next); 1675 1676 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt"); 1677 if (error != 0) { 1678 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error); 1679 return (error); 1680 } 1681 1682 /* 1683 * Trigger immediate reconnection. 1684 */ 1685 ISCSI_SESSION_LOCK(is); 1686 is->is_waiting_for_iscsid = true; 1687 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason)); 1688 ISCSI_SESSION_UNLOCK(is); 1689 cv_signal(&sc->sc_cv); 1690 1691 sx_xunlock(&sc->sc_lock); 1692 1693 return (0); 1694 } 1695 1696 static bool 1697 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1, 1698 unsigned int id2, const struct iscsi_session_conf *c2) 1699 { 1700 if (id2 == 0 && c2->isc_target[0] == '\0' && 1701 c2->isc_target_addr[0] == '\0') 1702 return (true); 1703 if (id2 != 0 && id2 == id1) 1704 return (true); 1705 if (c2->isc_target[0] != '\0' && 1706 strcmp(c1->isc_target, c2->isc_target) == 0) 1707 return (true); 1708 if (c2->isc_target_addr[0] != '\0' && 1709 strcmp(c1->isc_target_addr, c2->isc_target_addr) == 0) 1710 return (true); 1711 return (false); 1712 } 1713 1714 static int 1715 iscsi_ioctl_session_remove(struct iscsi_softc *sc, 1716 struct iscsi_session_remove *isr) 1717 { 1718 struct iscsi_session *is, *tmp; 1719 bool found = false; 1720 1721 iscsi_sanitize_session_conf(&isr->isr_conf); 1722 1723 sx_xlock(&sc->sc_lock); 1724 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) { 1725 ISCSI_SESSION_LOCK(is); 1726 if (iscsi_session_conf_matches(is->is_id, &is->is_conf, 1727 isr->isr_session_id, &isr->isr_conf)) { 1728 found = true; 1729 iscsi_session_logout(is); 1730 iscsi_session_terminate(is); 1731 } 1732 ISCSI_SESSION_UNLOCK(is); 1733 } 1734 sx_xunlock(&sc->sc_lock); 1735 1736 if (!found) 1737 return (ESRCH); 1738 1739 return (0); 1740 } 1741 1742 static int 1743 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl) 1744 { 1745 int error; 1746 unsigned int i = 0; 1747 struct iscsi_session *is; 1748 struct iscsi_session_state iss; 1749 1750 sx_slock(&sc->sc_lock); 1751 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1752 if (i >= isl->isl_nentries) { 1753 sx_sunlock(&sc->sc_lock); 1754 return (EMSGSIZE); 1755 } 1756 memset(&iss, 0, sizeof(iss)); 1757 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf)); 1758 iss.iss_id = is->is_id; 1759 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias)); 1760 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason)); 1761 1762 if (is->is_conn->ic_header_crc32c) 1763 iss.iss_header_digest = ISCSI_DIGEST_CRC32C; 1764 else 1765 iss.iss_header_digest = ISCSI_DIGEST_NONE; 1766 1767 if (is->is_conn->ic_data_crc32c) 1768 iss.iss_data_digest = ISCSI_DIGEST_CRC32C; 1769 else 1770 iss.iss_data_digest = ISCSI_DIGEST_NONE; 1771 1772 iss.iss_max_data_segment_length = is->is_max_data_segment_length; 1773 iss.iss_immediate_data = is->is_immediate_data; 1774 iss.iss_connected = is->is_connected; 1775 1776 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss)); 1777 if (error != 0) { 1778 sx_sunlock(&sc->sc_lock); 1779 return (error); 1780 } 1781 i++; 1782 } 1783 sx_sunlock(&sc->sc_lock); 1784 1785 isl->isl_nentries = i; 1786 1787 return (0); 1788 } 1789 1790 static int 1791 iscsi_ioctl_session_modify(struct iscsi_softc *sc, 1792 struct iscsi_session_modify *ism) 1793 { 1794 struct iscsi_session *is; 1795 1796 iscsi_sanitize_session_conf(&ism->ism_conf); 1797 if (iscsi_valid_session_conf(&ism->ism_conf) == false) 1798 return (EINVAL); 1799 1800 sx_xlock(&sc->sc_lock); 1801 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) { 1802 ISCSI_SESSION_LOCK(is); 1803 if (is->is_id == ism->ism_session_id) 1804 break; 1805 ISCSI_SESSION_UNLOCK(is); 1806 } 1807 if (is == NULL) { 1808 sx_xunlock(&sc->sc_lock); 1809 return (ESRCH); 1810 } 1811 sx_xunlock(&sc->sc_lock); 1812 1813 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf)); 1814 ISCSI_SESSION_UNLOCK(is); 1815 1816 iscsi_session_reconnect(is); 1817 1818 return (0); 1819 } 1820 1821 static int 1822 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, 1823 struct thread *td) 1824 { 1825 struct iscsi_softc *sc; 1826 1827 sc = dev->si_drv1; 1828 1829 switch (cmd) { 1830 case ISCSIDWAIT: 1831 return (iscsi_ioctl_daemon_wait(sc, 1832 (struct iscsi_daemon_request *)arg)); 1833 case ISCSIDHANDOFF: 1834 return (iscsi_ioctl_daemon_handoff(sc, 1835 (struct iscsi_daemon_handoff *)arg)); 1836 case ISCSIDFAIL: 1837 return (iscsi_ioctl_daemon_fail(sc, 1838 (struct iscsi_daemon_fail *)arg)); 1839 #ifdef ICL_KERNEL_PROXY 1840 case ISCSIDCONNECT: 1841 return (iscsi_ioctl_daemon_connect(sc, 1842 (struct iscsi_daemon_connect *)arg)); 1843 case ISCSIDSEND: 1844 return (iscsi_ioctl_daemon_send(sc, 1845 (struct iscsi_daemon_send *)arg)); 1846 case ISCSIDRECEIVE: 1847 return (iscsi_ioctl_daemon_receive(sc, 1848 (struct iscsi_daemon_receive *)arg)); 1849 #endif /* ICL_KERNEL_PROXY */ 1850 case ISCSISADD: 1851 return (iscsi_ioctl_session_add(sc, 1852 (struct iscsi_session_add *)arg)); 1853 case ISCSISREMOVE: 1854 return (iscsi_ioctl_session_remove(sc, 1855 (struct iscsi_session_remove *)arg)); 1856 case ISCSISLIST: 1857 return (iscsi_ioctl_session_list(sc, 1858 (struct iscsi_session_list *)arg)); 1859 case ISCSISMODIFY: 1860 return (iscsi_ioctl_session_modify(sc, 1861 (struct iscsi_session_modify *)arg)); 1862 default: 1863 return (EINVAL); 1864 } 1865 } 1866 1867 static struct iscsi_outstanding * 1868 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag) 1869 { 1870 struct iscsi_outstanding *io; 1871 1872 ISCSI_SESSION_LOCK_ASSERT(is); 1873 1874 TAILQ_FOREACH(io, &is->is_outstanding, io_next) { 1875 if (io->io_initiator_task_tag == initiator_task_tag) 1876 return (io); 1877 } 1878 return (NULL); 1879 } 1880 1881 static int 1882 iscsi_outstanding_add(struct iscsi_session *is, 1883 uint32_t initiator_task_tag, union ccb *ccb) 1884 { 1885 struct iscsi_outstanding *io; 1886 1887 ISCSI_SESSION_LOCK_ASSERT(is); 1888 1889 KASSERT(iscsi_outstanding_find(is, initiator_task_tag) == NULL, 1890 ("initiator_task_tag 0x%x already added", initiator_task_tag)); 1891 1892 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO); 1893 if (io == NULL) { 1894 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes", sizeof(*io)); 1895 return (ENOMEM); 1896 } 1897 io->io_initiator_task_tag = initiator_task_tag; 1898 io->io_ccb = ccb; 1899 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next); 1900 return (0); 1901 } 1902 1903 static void 1904 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io) 1905 { 1906 1907 ISCSI_SESSION_LOCK_ASSERT(is); 1908 1909 TAILQ_REMOVE(&is->is_outstanding, io, io_next); 1910 uma_zfree(iscsi_outstanding_zone, io); 1911 } 1912 1913 static void 1914 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb) 1915 { 1916 struct icl_pdu *request; 1917 struct iscsi_bhs_scsi_command *bhssc; 1918 struct ccb_scsiio *csio; 1919 size_t len; 1920 int error; 1921 1922 ISCSI_SESSION_LOCK_ASSERT(is); 1923 1924 #if 0 1925 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__)); 1926 #else 1927 if (is->is_login_phase) { 1928 ISCSI_SESSION_DEBUG(is, "called during login phase"); 1929 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 1930 xpt_freeze_devq(ccb->ccb_h.path, 1); 1931 ISCSI_SESSION_DEBUG(is, "freezing devq"); 1932 } 1933 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN; 1934 xpt_done(ccb); 1935 return; 1936 } 1937 #endif 1938 1939 request = icl_pdu_new_bhs(is->is_conn, M_NOWAIT); 1940 if (request == NULL) { 1941 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 1942 xpt_freeze_devq(ccb->ccb_h.path, 1); 1943 ISCSI_SESSION_DEBUG(is, "freezing devq"); 1944 } 1945 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 1946 xpt_done(ccb); 1947 return; 1948 } 1949 1950 csio = &ccb->csio; 1951 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 1952 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND; 1953 bhssc->bhssc_flags |= BHSSC_FLAGS_F; 1954 switch (csio->ccb_h.flags & CAM_DIR_MASK) { 1955 case CAM_DIR_IN: 1956 bhssc->bhssc_flags |= BHSSC_FLAGS_R; 1957 break; 1958 case CAM_DIR_OUT: 1959 bhssc->bhssc_flags |= BHSSC_FLAGS_W; 1960 break; 1961 } 1962 1963 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) { 1964 switch (csio->tag_action) { 1965 case MSG_HEAD_OF_Q_TAG: 1966 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ; 1967 break; 1968 case MSG_ORDERED_Q_TAG: 1969 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED; 1970 break; 1971 case MSG_ACA_TASK: 1972 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA; 1973 break; 1974 case MSG_SIMPLE_Q_TAG: 1975 default: 1976 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE; 1977 break; 1978 } 1979 } else 1980 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED; 1981 1982 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun)); 1983 bhssc->bhssc_initiator_task_tag = is->is_initiator_task_tag; 1984 is->is_initiator_task_tag++; 1985 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len); 1986 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb), 1987 ("unsupported CDB size %zd", (size_t)csio->cdb_len)); 1988 1989 if (csio->ccb_h.flags & CAM_CDB_POINTER) 1990 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len); 1991 else 1992 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len); 1993 1994 error = iscsi_outstanding_add(is, bhssc->bhssc_initiator_task_tag, ccb); 1995 if (error != 0) { 1996 icl_pdu_free(request); 1997 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 1998 xpt_freeze_devq(ccb->ccb_h.path, 1); 1999 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2000 } 2001 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2002 xpt_done(ccb); 2003 return; 2004 } 2005 2006 if (is->is_immediate_data && 2007 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) { 2008 len = csio->dxfer_len; 2009 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len); 2010 if (len > is->is_first_burst_length) { 2011 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_first_burst_length); 2012 len = is->is_first_burst_length; 2013 } 2014 2015 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT); 2016 if (error != 0) { 2017 icl_pdu_free(request); 2018 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 2019 xpt_freeze_devq(ccb->ccb_h.path, 1); 2020 ISCSI_SESSION_DEBUG(is, "freezing devq"); 2021 } 2022 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN; 2023 xpt_done(ccb); 2024 return; 2025 } 2026 } 2027 iscsi_pdu_queue_locked(request); 2028 } 2029 2030 static void 2031 iscsi_action(struct cam_sim *sim, union ccb *ccb) 2032 { 2033 struct iscsi_session *is; 2034 2035 is = cam_sim_softc(sim); 2036 2037 ISCSI_SESSION_LOCK_ASSERT(is); 2038 2039 if (is->is_terminating || 2040 (is->is_connected == false && fail_on_disconnection)) { 2041 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2042 xpt_done(ccb); 2043 return; 2044 } 2045 2046 switch (ccb->ccb_h.func_code) { 2047 case XPT_PATH_INQ: 2048 { 2049 struct ccb_pathinq *cpi = &ccb->cpi; 2050 2051 cpi->version_num = 1; 2052 cpi->hba_inquiry = PI_TAG_ABLE; 2053 cpi->target_sprt = 0; 2054 cpi->hba_misc = PIM_EXTLUNS; 2055 cpi->hba_eng_cnt = 0; 2056 cpi->max_target = 0; 2057 cpi->max_lun = 0; 2058 cpi->initiator_id = ~0; 2059 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2060 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN); 2061 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2062 cpi->unit_number = cam_sim_unit(sim); 2063 cpi->bus_id = cam_sim_bus(sim); 2064 cpi->base_transfer_speed = 150000; /* XXX */ 2065 cpi->transport = XPORT_ISCSI; 2066 cpi->transport_version = 0; 2067 cpi->protocol = PROTO_SCSI; 2068 cpi->protocol_version = SCSI_REV_SPC3; 2069 cpi->maxio = MAXPHYS; 2070 cpi->ccb_h.status = CAM_REQ_CMP; 2071 break; 2072 } 2073 case XPT_GET_TRAN_SETTINGS: 2074 { 2075 struct ccb_trans_settings *cts; 2076 struct ccb_trans_settings_scsi *scsi; 2077 2078 cts = &ccb->cts; 2079 scsi = &cts->proto_specific.scsi; 2080 2081 cts->protocol = PROTO_SCSI; 2082 cts->protocol_version = SCSI_REV_SPC3; 2083 cts->transport = XPORT_ISCSI; 2084 cts->transport_version = 0; 2085 scsi->valid = CTS_SCSI_VALID_TQ; 2086 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 2087 cts->ccb_h.status = CAM_REQ_CMP; 2088 break; 2089 } 2090 case XPT_CALC_GEOMETRY: 2091 cam_calc_geometry(&ccb->ccg, /*extended*/1); 2092 ccb->ccb_h.status = CAM_REQ_CMP; 2093 break; 2094 #if 0 2095 /* 2096 * XXX: What's the point? 2097 */ 2098 case XPT_RESET_BUS: 2099 case XPT_ABORT: 2100 case XPT_TERM_IO: 2101 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io"); 2102 ccb->ccb_h.status = CAM_REQ_CMP; 2103 break; 2104 #endif 2105 case XPT_SCSI_IO: 2106 iscsi_action_scsiio(is, ccb); 2107 return; 2108 default: 2109 #if 0 2110 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code); 2111 #endif 2112 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2113 break; 2114 } 2115 xpt_done(ccb); 2116 } 2117 2118 static void 2119 iscsi_poll(struct cam_sim *sim) 2120 { 2121 2122 KASSERT(0, ("%s: you're not supposed to be here", __func__)); 2123 } 2124 2125 static void 2126 iscsi_shutdown(struct iscsi_softc *sc) 2127 { 2128 struct iscsi_session *is; 2129 2130 ISCSI_DEBUG("removing all sessions due to shutdown"); 2131 2132 sx_slock(&sc->sc_lock); 2133 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) 2134 iscsi_session_terminate(is); 2135 sx_sunlock(&sc->sc_lock); 2136 } 2137 2138 static int 2139 iscsi_load(void) 2140 { 2141 int error; 2142 2143 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK); 2144 sx_init(&sc->sc_lock, "iscsi"); 2145 TAILQ_INIT(&sc->sc_sessions); 2146 cv_init(&sc->sc_cv, "iscsi_cv"); 2147 2148 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding", 2149 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL, 2150 UMA_ALIGN_PTR, 0); 2151 2152 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw, 2153 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi"); 2154 if (error != 0) { 2155 ISCSI_WARN("failed to create device node, error %d", error); 2156 return (error); 2157 } 2158 sc->sc_cdev->si_drv1 = sc; 2159 2160 /* 2161 * Note that this needs to get run before dashutdown(). Otherwise, 2162 * when rebooting with iSCSI session with outstanding requests, 2163 * but disconnected, dashutdown() will hang on cam_periph_runccb(). 2164 */ 2165 sc->sc_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_post_sync, 2166 iscsi_shutdown, sc, SHUTDOWN_PRI_FIRST); 2167 2168 return (0); 2169 } 2170 2171 static int 2172 iscsi_unload(void) 2173 { 2174 struct iscsi_session *is, *tmp; 2175 2176 if (sc->sc_cdev != NULL) { 2177 ISCSI_DEBUG("removing device node"); 2178 destroy_dev(sc->sc_cdev); 2179 ISCSI_DEBUG("device node removed"); 2180 } 2181 2182 if (sc->sc_shutdown_eh != NULL) 2183 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_eh); 2184 2185 sx_slock(&sc->sc_lock); 2186 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) 2187 iscsi_session_terminate(is); 2188 while(!TAILQ_EMPTY(&sc->sc_sessions)) { 2189 ISCSI_DEBUG("waiting for sessions to terminate"); 2190 cv_wait(&sc->sc_cv, &sc->sc_lock); 2191 } 2192 ISCSI_DEBUG("all sessions terminated"); 2193 sx_sunlock(&sc->sc_lock); 2194 2195 uma_zdestroy(iscsi_outstanding_zone); 2196 sx_destroy(&sc->sc_lock); 2197 cv_destroy(&sc->sc_cv); 2198 free(sc, M_ISCSI); 2199 return (0); 2200 } 2201 2202 static int 2203 iscsi_quiesce(void) 2204 { 2205 sx_slock(&sc->sc_lock); 2206 if (!TAILQ_EMPTY(&sc->sc_sessions)) { 2207 sx_sunlock(&sc->sc_lock); 2208 return (EBUSY); 2209 } 2210 sx_sunlock(&sc->sc_lock); 2211 return (0); 2212 } 2213 2214 static int 2215 iscsi_modevent(module_t mod, int what, void *arg) 2216 { 2217 int error; 2218 2219 switch (what) { 2220 case MOD_LOAD: 2221 error = iscsi_load(); 2222 break; 2223 case MOD_UNLOAD: 2224 error = iscsi_unload(); 2225 break; 2226 case MOD_QUIESCE: 2227 error = iscsi_quiesce(); 2228 break; 2229 default: 2230 error = EINVAL; 2231 break; 2232 } 2233 return (error); 2234 } 2235 2236 moduledata_t iscsi_data = { 2237 "iscsi", 2238 iscsi_modevent, 2239 0 2240 }; 2241 2242 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 2243 MODULE_DEPEND(iscsi, cam, 1, 1, 1); 2244 MODULE_DEPEND(iscsi, icl, 1, 1, 1); 2245