2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/condvar.h>
38 #include <sys/endian.h>
39 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/module.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_debug.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_periph.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
64 #include <dev/iscsi/icl.h>
65 #include <dev/iscsi/icl_wrappers.h>
66 #include <dev/iscsi/iscsi_ioctl.h>
67 #include <dev/iscsi/iscsi_proto.h>
68 #include <dev/iscsi/iscsi.h>
70 #ifdef ICL_KERNEL_PROXY
71 #include <sys/socketvar.h>
74 #ifdef ICL_KERNEL_PROXY
75 FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY");
79 * XXX: This is global so the iscsi_unload() can access it.
80 * Think about how to do this properly.
82 static struct iscsi_softc *sc;
84 SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
87 SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
88 &debug, 0, "Enable debug messages");
89 static int ping_timeout = 5;
90 SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout,
91 0, "Timeout for ping (NOP-Out) requests, in seconds");
92 static int iscsid_timeout = 60;
93 SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout,
94 0, "Time to wait for iscsid(8) to handle reconnection, in seconds");
95 static int login_timeout = 60;
96 SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout,
97 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds");
98 static int maxtags = 255;
99 SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags,
100 0, "Max number of IO requests queued");
101 static int fail_on_disconnection = 0;
102 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN,
103 &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure");
104 static int fail_on_shutdown = 1;
105 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_shutdown, CTLFLAG_RWTUN,
106 &fail_on_shutdown, 0, "Fail disconnected sessions on shutdown");
108 static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator");
109 static uma_zone_t iscsi_outstanding_zone;
111 #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0)
112 #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn))
114 #define ISCSI_DEBUG(X, ...) \
117 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
120 #define ISCSI_WARN(X, ...) \
123 printf("WARNING: %s: " X "\n", \
124 __func__, ## __VA_ARGS__); \
128 #define ISCSI_SESSION_DEBUG(S, X, ...) \
131 printf("%s: %s (%s): " X "\n", \
132 __func__, S->is_conf.isc_target_addr, \
133 S->is_conf.isc_target, ## __VA_ARGS__); \
137 #define ISCSI_SESSION_WARN(S, X, ...) \
140 printf("WARNING: %s (%s): " X "\n", \
141 S->is_conf.isc_target_addr, \
142 S->is_conf.isc_target, ## __VA_ARGS__); \
146 #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock)
147 #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock)
148 #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED)
149 #define ISCSI_SESSION_LOCK_ASSERT_NOT(X) mtx_assert(&X->is_lock, MA_NOTOWNED)
151 static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
152 int mode, struct thread *td);
154 static struct cdevsw iscsi_cdevsw = {
155 .d_version = D_VERSION,
156 .d_ioctl = iscsi_ioctl,
160 static void iscsi_pdu_queue_locked(struct icl_pdu *request);
161 static void iscsi_pdu_queue(struct icl_pdu *request);
162 static void iscsi_pdu_update_statsn(const struct icl_pdu *response);
163 static void iscsi_pdu_handle_nop_in(struct icl_pdu *response);
164 static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response);
165 static void iscsi_pdu_handle_task_response(struct icl_pdu *response);
166 static void iscsi_pdu_handle_data_in(struct icl_pdu *response);
167 static void iscsi_pdu_handle_logout_response(struct icl_pdu *response);
168 static void iscsi_pdu_handle_r2t(struct icl_pdu *response);
169 static void iscsi_pdu_handle_async_message(struct icl_pdu *response);
170 static void iscsi_pdu_handle_reject(struct icl_pdu *response);
171 static void iscsi_session_reconnect(struct iscsi_session *is);
172 static void iscsi_session_terminate(struct iscsi_session *is);
173 static void iscsi_action(struct cam_sim *sim, union ccb *ccb);
174 static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is,
175 uint32_t initiator_task_tag);
176 static struct iscsi_outstanding *iscsi_outstanding_add(struct iscsi_session *is,
177 struct icl_pdu *request, union ccb *ccb,
178 uint32_t *initiator_task_tagp);
179 static void iscsi_outstanding_remove(struct iscsi_session *is,
180 struct iscsi_outstanding *io);
183 iscsi_pdu_prepare(struct icl_pdu *request)
185 struct iscsi_session *is;
186 struct iscsi_bhs_scsi_command *bhssc;
188 is = PDU_SESSION(request);
190 ISCSI_SESSION_LOCK_ASSERT(is);
193 * We're only using fields common for all the request
194 * (initiator -> target) PDUs.
196 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
199 * Data-Out PDU does not contain CmdSN.
201 if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
202 if (ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn) &&
203 (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
205 * Current MaxCmdSN prevents us from sending any more
206 * SCSI Command PDUs to the target; postpone the PDU.
207 * It will get resent by either iscsi_pdu_queue(),
208 * or by maintenance thread.
211 ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %u, "
212 "ExpCmdSN %u, MaxCmdSN %u, opcode 0x%x",
213 is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn,
214 bhssc->bhssc_opcode);
218 bhssc->bhssc_cmdsn = htonl(is->is_cmdsn);
219 if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
222 bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1);
228 iscsi_session_send_postponed(struct iscsi_session *is)
230 struct icl_pdu *request;
233 ISCSI_SESSION_LOCK_ASSERT(is);
235 if (STAILQ_EMPTY(&is->is_postponed))
237 while ((request = STAILQ_FIRST(&is->is_postponed)) != NULL) {
238 postpone = iscsi_pdu_prepare(request);
241 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
242 icl_pdu_queue(request);
244 xpt_release_simq(is->is_sim, 1);
248 iscsi_pdu_queue_locked(struct icl_pdu *request)
250 struct iscsi_session *is;
253 is = PDU_SESSION(request);
254 ISCSI_SESSION_LOCK_ASSERT(is);
255 iscsi_session_send_postponed(is);
256 postpone = iscsi_pdu_prepare(request);
258 if (STAILQ_EMPTY(&is->is_postponed))
259 xpt_freeze_simq(is->is_sim, 1);
260 STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next);
263 icl_pdu_queue(request);
267 iscsi_pdu_queue(struct icl_pdu *request)
269 struct iscsi_session *is;
271 is = PDU_SESSION(request);
272 ISCSI_SESSION_LOCK(is);
273 iscsi_pdu_queue_locked(request);
274 ISCSI_SESSION_UNLOCK(is);
278 iscsi_session_logout(struct iscsi_session *is)
280 struct icl_pdu *request;
281 struct iscsi_bhs_logout_request *bhslr;
283 request = icl_pdu_new(is->is_conn, M_NOWAIT);
287 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
288 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST;
289 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
290 iscsi_pdu_queue_locked(request);
294 iscsi_session_terminate_task(struct iscsi_session *is,
295 struct iscsi_outstanding *io, cam_status status)
298 ISCSI_SESSION_LOCK_ASSERT(is);
300 if (io->io_ccb != NULL) {
301 io->io_ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
302 io->io_ccb->ccb_h.status |= status;
303 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
304 io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN;
305 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1);
306 ISCSI_SESSION_DEBUG(is, "freezing devq");
308 xpt_done(io->io_ccb);
310 iscsi_outstanding_remove(is, io);
314 iscsi_session_terminate_tasks(struct iscsi_session *is, cam_status status)
316 struct iscsi_outstanding *io, *tmp;
318 ISCSI_SESSION_LOCK_ASSERT(is);
320 TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) {
321 iscsi_session_terminate_task(is, io, status);
326 iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim)
330 ISCSI_SESSION_LOCK_ASSERT(is);
333 * Don't queue any new PDUs.
335 if (is->is_sim != NULL && is->is_simq_frozen == false) {
336 ISCSI_SESSION_DEBUG(is, "freezing");
337 xpt_freeze_simq(is->is_sim, 1);
338 is->is_simq_frozen = true;
342 * Remove postponed PDUs.
344 if (!STAILQ_EMPTY(&is->is_postponed))
345 xpt_release_simq(is->is_sim, 1);
346 while ((pdu = STAILQ_FIRST(&is->is_postponed)) != NULL) {
347 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
351 if (destroy_sim == false) {
353 * Terminate SCSI tasks, asking CAM to requeue them.
355 iscsi_session_terminate_tasks(is, CAM_REQUEUE_REQ);
359 iscsi_session_terminate_tasks(is, CAM_DEV_NOT_THERE);
361 if (is->is_sim == NULL)
364 ISCSI_SESSION_DEBUG(is, "deregistering SIM");
365 xpt_async(AC_LOST_DEVICE, is->is_path, NULL);
367 if (is->is_simq_frozen) {
368 is->is_simq_frozen = false;
369 xpt_release_simq(is->is_sim, 1);
372 xpt_free_path(is->is_path);
374 xpt_bus_deregister(cam_sim_path(is->is_sim));
375 cam_sim_free(is->is_sim, TRUE /*free_devq*/);
381 iscsi_maintenance_thread_reconnect(struct iscsi_session *is)
384 icl_conn_close(is->is_conn);
386 ISCSI_SESSION_LOCK(is);
388 is->is_connected = false;
389 is->is_reconnecting = false;
390 is->is_login_phase = false;
392 #ifdef ICL_KERNEL_PROXY
393 if (is->is_login_pdu != NULL) {
394 icl_pdu_free(is->is_login_pdu);
395 is->is_login_pdu = NULL;
397 cv_signal(&is->is_login_cv);
400 if (fail_on_disconnection) {
401 ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices");
402 iscsi_session_cleanup(is, true);
404 iscsi_session_cleanup(is, false);
407 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
408 ("destroying session with active tasks"));
409 KASSERT(STAILQ_EMPTY(&is->is_postponed),
410 ("destroying session with postponed PDUs"));
412 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
413 ISCSI_SESSION_UNLOCK(is);
418 * Request immediate reconnection from iscsid(8).
420 //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)");
421 is->is_waiting_for_iscsid = true;
422 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
424 ISCSI_SESSION_UNLOCK(is);
425 cv_signal(&is->is_softc->sc_cv);
429 iscsi_maintenance_thread_terminate(struct iscsi_session *is)
431 struct iscsi_softc *sc;
434 sx_xlock(&sc->sc_lock);
435 TAILQ_REMOVE(&sc->sc_sessions, is, is_next);
436 sx_xunlock(&sc->sc_lock);
438 icl_conn_close(is->is_conn);
439 callout_drain(&is->is_callout);
441 ISCSI_SESSION_LOCK(is);
443 KASSERT(is->is_terminating, ("is_terminating == false"));
445 #ifdef ICL_KERNEL_PROXY
446 if (is->is_login_pdu != NULL) {
447 icl_pdu_free(is->is_login_pdu);
448 is->is_login_pdu = NULL;
450 cv_signal(&is->is_login_cv);
453 iscsi_session_cleanup(is, true);
455 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
456 ("destroying session with active tasks"));
457 KASSERT(STAILQ_EMPTY(&is->is_postponed),
458 ("destroying session with postponed PDUs"));
460 ISCSI_SESSION_UNLOCK(is);
462 icl_conn_free(is->is_conn);
463 mtx_destroy(&is->is_lock);
464 cv_destroy(&is->is_maintenance_cv);
465 #ifdef ICL_KERNEL_PROXY
466 cv_destroy(&is->is_login_cv);
469 ISCSI_SESSION_DEBUG(is, "terminated");
473 * The iscsi_unload() routine might be waiting.
475 cv_signal(&sc->sc_cv);
479 iscsi_maintenance_thread(void *arg)
481 struct iscsi_session *is = arg;
483 ISCSI_SESSION_LOCK(is);
485 if (is->is_reconnecting == false &&
486 is->is_terminating == false &&
487 (STAILQ_EMPTY(&is->is_postponed) ||
488 ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn)))
489 cv_wait(&is->is_maintenance_cv, &is->is_lock);
491 /* Terminate supersedes reconnect. */
492 if (is->is_terminating) {
493 ISCSI_SESSION_UNLOCK(is);
494 iscsi_maintenance_thread_terminate(is);
499 if (is->is_reconnecting) {
500 ISCSI_SESSION_UNLOCK(is);
501 iscsi_maintenance_thread_reconnect(is);
502 ISCSI_SESSION_LOCK(is);
506 iscsi_session_send_postponed(is);
508 ISCSI_SESSION_UNLOCK(is);
512 iscsi_session_reconnect(struct iscsi_session *is)
516 * XXX: We can't use locking here, because
517 * it's being called from various contexts.
518 * Hope it doesn't break anything.
520 if (is->is_reconnecting)
523 is->is_reconnecting = true;
524 cv_signal(&is->is_maintenance_cv);
528 iscsi_session_terminate(struct iscsi_session *is)
531 if (is->is_terminating)
534 is->is_terminating = true;
537 iscsi_session_logout(is);
539 cv_signal(&is->is_maintenance_cv);
543 iscsi_callout(void *context)
545 struct icl_pdu *request;
546 struct iscsi_bhs_nop_out *bhsno;
547 struct iscsi_session *is;
548 bool reconnect_needed = false;
553 ISCSI_SESSION_LOCK(is);
554 if (is->is_terminating) {
555 ISCSI_SESSION_UNLOCK(is);
561 callout_schedule_sbt(&is->is_callout, sbt, pr, 0);
563 if (is->is_conf.isc_enable == 0)
568 if (is->is_waiting_for_iscsid) {
569 if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) {
570 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) "
571 "for %d seconds; reconnecting",
573 reconnect_needed = true;
578 if (is->is_login_phase) {
579 if (login_timeout > 0 && is->is_timeout > login_timeout) {
580 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; "
581 "reconnecting", is->is_timeout);
582 reconnect_needed = true;
587 if (ping_timeout <= 0) {
589 * Pings are disabled. Don't send NOP-Out in this case.
590 * Reset the timeout, to avoid triggering reconnection,
591 * should the user decide to reenable them.
597 if (is->is_timeout >= ping_timeout) {
598 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; "
599 "reconnecting", ping_timeout);
600 reconnect_needed = true;
604 ISCSI_SESSION_UNLOCK(is);
607 * If the ping was reset less than one second ago - which means
608 * that we've received some PDU during the last second - assume
609 * the traffic flows correctly and don't bother sending a NOP-Out.
611 * (It's 2 - one for one second, and one for incrementing is_timeout
612 * earlier in this routine.)
614 if (is->is_timeout < 2)
617 request = icl_pdu_new(is->is_conn, M_NOWAIT);
618 if (request == NULL) {
619 ISCSI_SESSION_WARN(is, "failed to allocate PDU");
622 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
623 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
624 ISCSI_BHS_OPCODE_IMMEDIATE;
625 bhsno->bhsno_flags = 0x80;
626 bhsno->bhsno_target_transfer_tag = 0xffffffff;
627 iscsi_pdu_queue(request);
631 if (is->is_terminating) {
632 ISCSI_SESSION_UNLOCK(is);
636 ISCSI_SESSION_UNLOCK(is);
638 if (reconnect_needed)
639 iscsi_session_reconnect(is);
643 iscsi_pdu_update_statsn(const struct icl_pdu *response)
645 const struct iscsi_bhs_data_in *bhsdi;
646 struct iscsi_session *is;
647 uint32_t expcmdsn, maxcmdsn, statsn;
649 is = PDU_SESSION(response);
651 ISCSI_SESSION_LOCK_ASSERT(is);
654 * We're only using fields common for all the response
655 * (target -> initiator) PDUs.
657 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs;
659 * Ok, I lied. In case of Data-In, "The fields StatSN, Status,
660 * and Residual Count only have meaningful content if the S bit
661 * is set to 1", so we also need to check the bit specific for
664 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
665 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) {
666 statsn = ntohl(bhsdi->bhsdi_statsn);
667 if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) {
668 /* XXX: This is normal situation for MCS */
669 ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != "
670 "session ExpStatSN %u (or + 1); reconnecting",
671 bhsdi->bhsdi_opcode, statsn, is->is_statsn);
672 iscsi_session_reconnect(is);
674 if (ISCSI_SNGT(statsn, is->is_statsn))
675 is->is_statsn = statsn;
678 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn);
679 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn);
681 if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) {
682 ISCSI_SESSION_DEBUG(is,
683 "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring",
686 if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) {
687 is->is_maxcmdsn = maxcmdsn;
690 * Command window increased; kick the maintanance thread
691 * to send out postponed commands.
693 if (!STAILQ_EMPTY(&is->is_postponed))
694 cv_signal(&is->is_maintenance_cv);
695 } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) {
696 /* XXX: This is normal situation for MCS */
697 ISCSI_SESSION_DEBUG(is,
698 "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring",
699 maxcmdsn, is->is_maxcmdsn);
702 if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) {
703 is->is_expcmdsn = expcmdsn;
704 } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) {
705 /* XXX: This is normal situation for MCS */
706 ISCSI_SESSION_DEBUG(is,
707 "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring",
708 expcmdsn, is->is_expcmdsn);
713 * Every incoming PDU - not just NOP-In - resets the ping timer.
714 * The purpose of the timeout is to reset the connection when it stalls;
715 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
722 iscsi_receive_callback(struct icl_pdu *response)
724 struct iscsi_session *is;
726 is = PDU_SESSION(response);
728 ISCSI_SESSION_LOCK(is);
730 iscsi_pdu_update_statsn(response);
732 #ifdef ICL_KERNEL_PROXY
733 if (is->is_login_phase) {
734 if (is->is_login_pdu == NULL)
735 is->is_login_pdu = response;
737 icl_pdu_free(response);
738 ISCSI_SESSION_UNLOCK(is);
739 cv_signal(&is->is_login_cv);
745 * The handling routine is responsible for freeing the PDU
746 * when it's no longer needed.
748 switch (response->ip_bhs->bhs_opcode) {
749 case ISCSI_BHS_OPCODE_NOP_IN:
750 iscsi_pdu_handle_nop_in(response);
751 ISCSI_SESSION_UNLOCK(is);
753 case ISCSI_BHS_OPCODE_SCSI_RESPONSE:
754 iscsi_pdu_handle_scsi_response(response);
755 /* Session lock dropped inside. */
756 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
758 case ISCSI_BHS_OPCODE_TASK_RESPONSE:
759 iscsi_pdu_handle_task_response(response);
760 ISCSI_SESSION_UNLOCK(is);
762 case ISCSI_BHS_OPCODE_SCSI_DATA_IN:
763 iscsi_pdu_handle_data_in(response);
764 /* Session lock dropped inside. */
765 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
767 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE:
768 iscsi_pdu_handle_logout_response(response);
769 ISCSI_SESSION_UNLOCK(is);
771 case ISCSI_BHS_OPCODE_R2T:
772 iscsi_pdu_handle_r2t(response);
773 ISCSI_SESSION_UNLOCK(is);
775 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE:
776 iscsi_pdu_handle_async_message(response);
777 ISCSI_SESSION_UNLOCK(is);
779 case ISCSI_BHS_OPCODE_REJECT:
780 iscsi_pdu_handle_reject(response);
781 ISCSI_SESSION_UNLOCK(is);
784 ISCSI_SESSION_WARN(is, "received PDU with unsupported "
785 "opcode 0x%x; reconnecting",
786 response->ip_bhs->bhs_opcode);
787 iscsi_session_reconnect(is);
788 ISCSI_SESSION_UNLOCK(is);
789 icl_pdu_free(response);
794 iscsi_error_callback(struct icl_conn *ic)
796 struct iscsi_session *is;
798 is = CONN_SESSION(ic);
800 ISCSI_SESSION_WARN(is, "connection error; reconnecting");
801 iscsi_session_reconnect(is);
805 iscsi_pdu_handle_nop_in(struct icl_pdu *response)
807 struct iscsi_session *is;
808 struct iscsi_bhs_nop_out *bhsno;
809 struct iscsi_bhs_nop_in *bhsni;
810 struct icl_pdu *request;
815 is = PDU_SESSION(response);
816 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
818 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) {
820 * Nothing to do; iscsi_pdu_update_statsn() already
821 * zeroed the timeout.
823 icl_pdu_free(response);
827 datasize = icl_pdu_data_segment_length(response);
829 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO);
831 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
833 icl_pdu_free(response);
834 iscsi_session_reconnect(is);
837 icl_pdu_get_data(response, 0, data, datasize);
840 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
841 if (request == NULL) {
842 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
845 icl_pdu_free(response);
846 iscsi_session_reconnect(is);
849 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
850 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
851 ISCSI_BHS_OPCODE_IMMEDIATE;
852 bhsno->bhsno_flags = 0x80;
853 bhsno->bhsno_initiator_task_tag = 0xffffffff;
854 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag;
856 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT);
858 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
861 icl_pdu_free(request);
862 icl_pdu_free(response);
863 iscsi_session_reconnect(is);
869 icl_pdu_free(response);
870 iscsi_pdu_queue_locked(request);
874 iscsi_pdu_handle_scsi_response(struct icl_pdu *response)
876 struct iscsi_bhs_scsi_response *bhssr;
877 struct iscsi_outstanding *io;
878 struct iscsi_session *is;
880 struct ccb_scsiio *csio;
881 size_t data_segment_len, received;
885 is = PDU_SESSION(response);
887 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
888 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag);
889 if (io == NULL || io->io_ccb == NULL) {
890 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag);
891 icl_pdu_free(response);
892 iscsi_session_reconnect(is);
893 ISCSI_SESSION_UNLOCK(is);
898 if (bhssr->bhssr_response == BHSSR_RESPONSE_COMMAND_COMPLETED) {
899 if (ntohl(bhssr->bhssr_expdatasn) != io->io_datasn) {
900 ISCSI_SESSION_WARN(is,
901 "ExpDataSN mismatch in SCSI Response (%u vs %u)",
902 ntohl(bhssr->bhssr_expdatasn), io->io_datasn);
905 * XXX: Permit an ExpDataSN of zero for errors.
907 * This doesn't conform to RFC 7143, but some
908 * targets seem to do this.
910 if (bhssr->bhssr_status != 0 &&
911 bhssr->bhssr_expdatasn == htonl(0))
914 icl_pdu_free(response);
915 iscsi_session_reconnect(is);
916 ISCSI_SESSION_UNLOCK(is);
920 if (bhssr->bhssr_expdatasn != htonl(0)) {
921 ISCSI_SESSION_WARN(is,
922 "ExpDataSN mismatch in SCSI Response (%u vs 0)",
923 ntohl(bhssr->bhssr_expdatasn));
924 icl_pdu_free(response);
925 iscsi_session_reconnect(is);
926 ISCSI_SESSION_UNLOCK(is);
933 * With iSER, after getting good response we can be sure
934 * that all the data has been successfully transferred.
936 if (is->is_conn->ic_iser) {
937 resid = ntohl(bhssr->bhssr_residual_count);
938 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) {
939 io->io_received = ccb->csio.dxfer_len - resid;
940 } else if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) {
941 ISCSI_SESSION_WARN(is, "overflow: target indicates %d", resid);
943 io->io_received = ccb->csio.dxfer_len;
947 received = io->io_received;
948 iscsi_outstanding_remove(is, io);
949 ISCSI_SESSION_UNLOCK(is);
951 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) {
952 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response);
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");
957 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
958 } else if (bhssr->bhssr_status == 0) {
959 ccb->ccb_h.status = CAM_REQ_CMP;
961 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
962 xpt_freeze_devq(ccb->ccb_h.path, 1);
963 ISCSI_SESSION_DEBUG(is, "freezing devq");
965 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
966 ccb->csio.scsi_status = bhssr->bhssr_status;
970 data_segment_len = icl_pdu_data_segment_length(response);
971 if (data_segment_len > 0) {
972 if (data_segment_len < sizeof(sense_len)) {
973 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)",
975 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
976 xpt_freeze_devq(ccb->ccb_h.path, 1);
977 ISCSI_SESSION_DEBUG(is, "freezing devq");
979 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
982 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len));
983 sense_len = ntohs(sense_len);
985 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd",
986 sense_len, data_segment_len);
988 if (sizeof(sense_len) + sense_len > data_segment_len) {
989 ISCSI_SESSION_WARN(is, "truncated data segment "
990 "(%zd bytes, should be %zd)",
991 data_segment_len, sizeof(sense_len) + sense_len);
992 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
993 xpt_freeze_devq(ccb->ccb_h.path, 1);
994 ISCSI_SESSION_DEBUG(is, "freezing devq");
996 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
998 } else if (sizeof(sense_len) + sense_len < data_segment_len)
999 ISCSI_SESSION_WARN(is, "oversize data segment "
1000 "(%zd bytes, should be %zd)",
1001 data_segment_len, sizeof(sense_len) + sense_len);
1002 if (sense_len > csio->sense_len) {
1003 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d",
1004 sense_len, csio->sense_len);
1005 sense_len = csio->sense_len;
1007 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len);
1008 csio->sense_resid = csio->sense_len - sense_len;
1009 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1013 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW)
1014 csio->resid = ntohl(bhssr->bhssr_residual_count);
1016 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1017 KASSERT(received <= csio->dxfer_len,
1018 ("received > csio->dxfer_len"));
1019 if (received < csio->dxfer_len) {
1020 if (csio->resid != csio->dxfer_len - received) {
1021 ISCSI_SESSION_WARN(is, "underflow mismatch: "
1022 "target indicates %d, we calculated %zd",
1023 csio->resid, csio->dxfer_len - received);
1025 csio->resid = csio->dxfer_len - received;
1030 icl_pdu_free(response);
1034 iscsi_pdu_handle_task_response(struct icl_pdu *response)
1036 struct iscsi_bhs_task_management_response *bhstmr;
1037 struct iscsi_outstanding *io, *aio;
1038 struct iscsi_session *is;
1040 is = PDU_SESSION(response);
1042 bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs;
1043 io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag);
1044 if (io == NULL || io->io_ccb != NULL) {
1045 ISCSI_SESSION_WARN(is, "bad itt 0x%x",
1046 bhstmr->bhstmr_initiator_task_tag);
1047 icl_pdu_free(response);
1048 iscsi_session_reconnect(is);
1052 if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) {
1053 ISCSI_SESSION_WARN(is, "task response 0x%x",
1054 bhstmr->bhstmr_response);
1056 aio = iscsi_outstanding_find(is, io->io_referenced_task_tag);
1057 if (aio != NULL && aio->io_ccb != NULL)
1058 iscsi_session_terminate_task(is, aio, CAM_REQ_ABORTED);
1061 iscsi_outstanding_remove(is, io);
1062 icl_pdu_free(response);
1066 iscsi_pdu_handle_data_in(struct icl_pdu *response)
1068 struct iscsi_bhs_data_in *bhsdi;
1069 struct iscsi_outstanding *io;
1070 struct iscsi_session *is;
1072 struct ccb_scsiio *csio;
1073 size_t data_segment_len, received, oreceived;
1075 is = PDU_SESSION(response);
1076 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
1077 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag);
1078 if (io == NULL || io->io_ccb == NULL) {
1079 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag);
1080 icl_pdu_free(response);
1081 iscsi_session_reconnect(is);
1082 ISCSI_SESSION_UNLOCK(is);
1086 if (io->io_datasn != ntohl(bhsdi->bhsdi_datasn)) {
1087 ISCSI_SESSION_WARN(is, "received Data-In PDU with "
1088 "DataSN %u, while expected %u; dropping connection",
1089 ntohl(bhsdi->bhsdi_datasn), io->io_datasn);
1090 icl_pdu_free(response);
1091 iscsi_session_reconnect(is);
1092 ISCSI_SESSION_UNLOCK(is);
1095 io->io_datasn += response->ip_additional_pdus + 1;
1097 data_segment_len = icl_pdu_data_segment_length(response);
1098 if (data_segment_len == 0) {
1100 * "The sending of 0 length data segments should be avoided,
1101 * but initiators and targets MUST be able to properly receive
1102 * 0 length data segments."
1104 ISCSI_SESSION_UNLOCK(is);
1105 icl_pdu_free(response);
1110 * We need to track this for security reasons - without it, malicious target
1111 * could respond to SCSI READ without sending Data-In PDUs, which would result
1112 * in read operation on the initiator side returning random kernel data.
1114 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) {
1115 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd",
1116 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset));
1117 icl_pdu_free(response);
1118 iscsi_session_reconnect(is);
1119 ISCSI_SESSION_UNLOCK(is);
1126 if (io->io_received + data_segment_len > csio->dxfer_len) {
1127 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes "
1128 "at offset %zd, buffer is %d)",
1129 data_segment_len, io->io_received, csio->dxfer_len);
1130 icl_pdu_free(response);
1131 iscsi_session_reconnect(is);
1132 ISCSI_SESSION_UNLOCK(is);
1136 oreceived = io->io_received;
1137 io->io_received += data_segment_len;
1138 received = io->io_received;
1139 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0)
1140 iscsi_outstanding_remove(is, io);
1141 ISCSI_SESSION_UNLOCK(is);
1143 icl_pdu_get_data(response, 0, csio->data_ptr + oreceived, data_segment_len);
1148 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) {
1150 * Nothing more to do.
1152 icl_pdu_free(response);
1156 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status);
1157 if (bhsdi->bhsdi_status == 0) {
1158 ccb->ccb_h.status = CAM_REQ_CMP;
1160 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1161 xpt_freeze_devq(ccb->ccb_h.path, 1);
1162 ISCSI_SESSION_DEBUG(is, "freezing devq");
1164 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
1165 csio->scsi_status = bhsdi->bhsdi_status;
1168 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1169 KASSERT(received <= csio->dxfer_len,
1170 ("received > csio->dxfer_len"));
1171 if (received < csio->dxfer_len) {
1172 csio->resid = ntohl(bhsdi->bhsdi_residual_count);
1173 if (csio->resid != csio->dxfer_len - received) {
1174 ISCSI_SESSION_WARN(is, "underflow mismatch: "
1175 "target indicates %d, we calculated %zd",
1176 csio->resid, csio->dxfer_len - received);
1178 csio->resid = csio->dxfer_len - received;
1183 icl_pdu_free(response);
1187 iscsi_pdu_handle_logout_response(struct icl_pdu *response)
1190 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response");
1191 icl_pdu_free(response);
1195 iscsi_pdu_handle_r2t(struct icl_pdu *response)
1197 struct icl_pdu *request;
1198 struct iscsi_session *is;
1199 struct iscsi_bhs_r2t *bhsr2t;
1200 struct iscsi_bhs_data_out *bhsdo;
1201 struct iscsi_outstanding *io;
1202 struct ccb_scsiio *csio;
1203 size_t off, len, max_send_data_segment_length, total_len;
1205 uint32_t datasn = 0;
1207 is = PDU_SESSION(response);
1209 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
1210 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag);
1211 if (io == NULL || io->io_ccb == NULL) {
1212 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting",
1213 bhsr2t->bhsr2t_initiator_task_tag);
1214 icl_pdu_free(response);
1215 iscsi_session_reconnect(is);
1219 csio = &io->io_ccb->csio;
1221 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) {
1222 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting");
1223 icl_pdu_free(response);
1224 iscsi_session_reconnect(is);
1229 * XXX: Verify R2TSN.
1232 off = ntohl(bhsr2t->bhsr2t_buffer_offset);
1233 if (off > csio->dxfer_len) {
1234 ISCSI_SESSION_WARN(is, "target requested invalid offset "
1235 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len);
1236 icl_pdu_free(response);
1237 iscsi_session_reconnect(is);
1241 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length);
1242 if (total_len == 0 || total_len > csio->dxfer_len) {
1243 ISCSI_SESSION_WARN(is, "target requested invalid length "
1244 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len);
1245 icl_pdu_free(response);
1246 iscsi_session_reconnect(is);
1250 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len);
1252 if (is->is_conn->ic_hw_isomax != 0)
1253 max_send_data_segment_length = is->is_conn->ic_hw_isomax;
1255 max_send_data_segment_length =
1256 is->is_conn->ic_max_send_data_segment_length;
1260 if (len > max_send_data_segment_length)
1261 len = max_send_data_segment_length;
1263 if (off + len > csio->dxfer_len) {
1264 ISCSI_SESSION_WARN(is, "target requested invalid "
1265 "length/offset %zd, buffer is %d; reconnecting",
1266 off + len, csio->dxfer_len);
1267 icl_pdu_free(response);
1268 iscsi_session_reconnect(is);
1272 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
1273 if (request == NULL) {
1274 icl_pdu_free(response);
1275 iscsi_session_reconnect(is);
1279 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
1280 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT;
1281 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun;
1282 bhsdo->bhsdo_initiator_task_tag =
1283 bhsr2t->bhsr2t_initiator_task_tag;
1284 bhsdo->bhsdo_target_transfer_tag =
1285 bhsr2t->bhsr2t_target_transfer_tag;
1286 bhsdo->bhsdo_datasn = htonl(datasn);
1287 bhsdo->bhsdo_buffer_offset = htonl(off);
1288 error = icl_pdu_append_data(request, csio->data_ptr + off, len,
1291 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
1293 icl_pdu_free(request);
1294 icl_pdu_free(response);
1295 iscsi_session_reconnect(is);
1299 datasn += howmany(len,
1300 is->is_conn->ic_max_send_data_segment_length);
1304 if (total_len == 0) {
1305 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F;
1306 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off);
1308 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off);
1311 iscsi_pdu_queue_locked(request);
1317 icl_pdu_free(response);
1321 iscsi_pdu_handle_async_message(struct icl_pdu *response)
1323 struct iscsi_bhs_asynchronous_message *bhsam;
1324 struct iscsi_session *is;
1326 is = PDU_SESSION(response);
1327 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1328 switch (bhsam->bhsam_async_event) {
1329 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT:
1330 ISCSI_SESSION_WARN(is, "target requests logout; removing session");
1331 iscsi_session_logout(is);
1332 iscsi_session_terminate(is);
1334 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION:
1335 ISCSI_SESSION_WARN(is, "target indicates it will drop the connection");
1337 case BHSAM_EVENT_TARGET_TERMINATES_SESSION:
1338 ISCSI_SESSION_WARN(is, "target indicates it will drop the session");
1342 * XXX: Technically, we're obligated to also handle
1343 * parameter renegotiation.
1345 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event);
1349 icl_pdu_free(response);
1353 iscsi_pdu_handle_reject(struct icl_pdu *response)
1355 struct iscsi_bhs_reject *bhsr;
1356 struct iscsi_session *is;
1358 is = PDU_SESSION(response);
1359 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs;
1360 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?",
1363 icl_pdu_free(response);
1367 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc,
1368 struct iscsi_daemon_request *request)
1370 struct iscsi_session *is;
1371 struct icl_drv_limits idl;
1374 sx_slock(&sc->sc_lock);
1376 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1377 ISCSI_SESSION_LOCK(is);
1378 if (is->is_conf.isc_enable == 0 &&
1379 is->is_conf.isc_discovery == 0) {
1380 ISCSI_SESSION_UNLOCK(is);
1383 if (is->is_waiting_for_iscsid)
1385 ISCSI_SESSION_UNLOCK(is);
1389 if (sc->sc_unloading) {
1390 sx_sunlock(&sc->sc_lock);
1395 * No session requires attention from iscsid(8); wait.
1397 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
1399 sx_sunlock(&sc->sc_lock);
1405 is->is_waiting_for_iscsid = false;
1406 is->is_login_phase = true;
1407 is->is_reason[0] = '\0';
1408 ISCSI_SESSION_UNLOCK(is);
1410 request->idr_session_id = is->is_id;
1411 memcpy(&request->idr_isid, &is->is_isid,
1412 sizeof(request->idr_isid));
1413 request->idr_tsih = 0; /* New or reinstated session. */
1414 memcpy(&request->idr_conf, &is->is_conf,
1415 sizeof(request->idr_conf));
1417 error = icl_limits(is->is_conf.isc_offload,
1418 is->is_conf.isc_iser, &idl);
1420 ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" "
1421 "failed with error %d", is->is_conf.isc_offload,
1423 sx_sunlock(&sc->sc_lock);
1426 request->idr_limits.isl_max_recv_data_segment_length =
1427 idl.idl_max_recv_data_segment_length;
1428 request->idr_limits.isl_max_send_data_segment_length =
1429 idl.idl_max_send_data_segment_length;
1430 request->idr_limits.isl_max_burst_length =
1431 idl.idl_max_burst_length;
1432 request->idr_limits.isl_first_burst_length =
1433 idl.idl_first_burst_length;
1435 sx_sunlock(&sc->sc_lock);
1441 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc,
1442 struct iscsi_daemon_handoff *handoff)
1444 struct iscsi_session *is;
1445 struct icl_conn *ic;
1448 sx_slock(&sc->sc_lock);
1451 * Find the session to hand off socket to.
1453 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1454 if (is->is_id == handoff->idh_session_id)
1458 sx_sunlock(&sc->sc_lock);
1461 ISCSI_SESSION_LOCK(is);
1463 if (is->is_conf.isc_discovery || is->is_terminating) {
1464 ISCSI_SESSION_UNLOCK(is);
1465 sx_sunlock(&sc->sc_lock);
1468 if (is->is_connected) {
1470 * This might have happened because another iscsid(8)
1471 * instance handed off the connection in the meantime.
1474 ISCSI_SESSION_WARN(is, "handoff on already connected "
1476 ISCSI_SESSION_UNLOCK(is);
1477 sx_sunlock(&sc->sc_lock);
1481 strlcpy(is->is_target_alias, handoff->idh_target_alias,
1482 sizeof(is->is_target_alias));
1483 is->is_tsih = handoff->idh_tsih;
1484 is->is_statsn = handoff->idh_statsn;
1485 is->is_protocol_level = handoff->idh_protocol_level;
1486 is->is_initial_r2t = handoff->idh_initial_r2t;
1487 is->is_immediate_data = handoff->idh_immediate_data;
1489 ic->ic_max_recv_data_segment_length =
1490 handoff->idh_max_recv_data_segment_length;
1491 ic->ic_max_send_data_segment_length =
1492 handoff->idh_max_send_data_segment_length;
1493 is->is_max_burst_length = handoff->idh_max_burst_length;
1494 is->is_first_burst_length = handoff->idh_first_burst_length;
1496 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C)
1497 ic->ic_header_crc32c = true;
1499 ic->ic_header_crc32c = false;
1500 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C)
1501 ic->ic_data_crc32c = true;
1503 ic->ic_data_crc32c = false;
1504 ic->ic_maxtags = maxtags;
1507 is->is_expcmdsn = 0;
1508 is->is_maxcmdsn = 0;
1509 is->is_waiting_for_iscsid = false;
1510 is->is_login_phase = false;
1512 is->is_connected = true;
1513 is->is_reason[0] = '\0';
1515 ISCSI_SESSION_UNLOCK(is);
1518 * If we're going through the proxy, the idh_socket will be 0,
1519 * and the ICL module can simply ignore this call. It can also
1520 * use it to determine it's no longer in the Login phase.
1522 error = icl_conn_handoff(ic, handoff->idh_socket);
1524 sx_sunlock(&sc->sc_lock);
1525 iscsi_session_terminate(is);
1529 sx_sunlock(&sc->sc_lock);
1531 if (is->is_sim != NULL) {
1533 * When reconnecting, there already is SIM allocated for the session.
1535 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq"));
1536 ISCSI_SESSION_LOCK(is);
1537 ISCSI_SESSION_DEBUG(is, "releasing");
1538 is->is_simq_frozen = false;
1539 xpt_release_simq(is->is_sim, 1);
1540 ISCSI_SESSION_UNLOCK(is);
1543 ISCSI_SESSION_LOCK(is);
1544 is->is_devq = cam_simq_alloc(ic->ic_maxtags);
1545 if (is->is_devq == NULL) {
1546 ISCSI_SESSION_UNLOCK(is);
1547 ISCSI_SESSION_WARN(is, "failed to allocate simq");
1548 iscsi_session_terminate(is);
1552 is->is_sim = cam_sim_alloc(iscsi_action, NULL, "iscsi",
1553 is, is->is_id /* unit */, &is->is_lock,
1554 1, ic->ic_maxtags, is->is_devq);
1555 if (is->is_sim == NULL) {
1556 ISCSI_SESSION_UNLOCK(is);
1557 ISCSI_SESSION_WARN(is, "failed to allocate SIM");
1558 cam_simq_free(is->is_devq);
1559 iscsi_session_terminate(is);
1563 if (xpt_bus_register(is->is_sim, NULL, 0) != 0) {
1564 ISCSI_SESSION_UNLOCK(is);
1565 ISCSI_SESSION_WARN(is, "failed to register bus");
1566 iscsi_session_terminate(is);
1570 error = xpt_create_path(&is->is_path, /*periph*/NULL,
1571 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD,
1573 if (error != CAM_REQ_CMP) {
1574 ISCSI_SESSION_UNLOCK(is);
1575 ISCSI_SESSION_WARN(is, "failed to create path");
1576 iscsi_session_terminate(is);
1579 ISCSI_SESSION_UNLOCK(is);
1586 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc,
1587 struct iscsi_daemon_fail *fail)
1589 struct iscsi_session *is;
1591 sx_slock(&sc->sc_lock);
1593 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1594 if (is->is_id == fail->idf_session_id)
1598 sx_sunlock(&sc->sc_lock);
1601 ISCSI_SESSION_LOCK(is);
1602 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s",
1604 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason));
1605 //is->is_waiting_for_iscsid = false;
1606 //is->is_login_phase = true;
1607 //iscsi_session_reconnect(is);
1608 ISCSI_SESSION_UNLOCK(is);
1609 sx_sunlock(&sc->sc_lock);
1614 #ifdef ICL_KERNEL_PROXY
1616 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc,
1617 struct iscsi_daemon_connect *idc)
1619 struct iscsi_session *is;
1620 struct sockaddr *from_sa, *to_sa;
1623 sx_slock(&sc->sc_lock);
1624 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1625 if (is->is_id == idc->idc_session_id)
1629 sx_sunlock(&sc->sc_lock);
1632 sx_sunlock(&sc->sc_lock);
1634 if (idc->idc_from_addrlen > 0) {
1635 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen);
1637 ISCSI_SESSION_WARN(is,
1638 "getsockaddr failed with error %d", error);
1644 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen);
1646 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d",
1648 free(from_sa, M_SONAME);
1652 ISCSI_SESSION_LOCK(is);
1655 is->is_expcmdsn = 0;
1656 is->is_maxcmdsn = 0;
1657 is->is_waiting_for_iscsid = false;
1658 is->is_login_phase = true;
1660 ISCSI_SESSION_UNLOCK(is);
1662 error = icl_conn_connect(is->is_conn, idc->idc_domain,
1663 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa);
1664 free(from_sa, M_SONAME);
1665 free(to_sa, M_SONAME);
1668 * Digests are always disabled during login phase.
1670 is->is_conn->ic_header_crc32c = false;
1671 is->is_conn->ic_data_crc32c = false;
1677 iscsi_ioctl_daemon_send(struct iscsi_softc *sc,
1678 struct iscsi_daemon_send *ids)
1680 struct iscsi_session *is;
1686 sx_slock(&sc->sc_lock);
1687 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1688 if (is->is_id == ids->ids_session_id)
1692 sx_sunlock(&sc->sc_lock);
1695 sx_sunlock(&sc->sc_lock);
1697 if (is->is_login_phase == false)
1700 if (is->is_terminating || is->is_reconnecting)
1703 datalen = ids->ids_data_segment_len;
1704 if (datalen > is->is_conn->ic_max_send_data_segment_length)
1707 data = malloc(datalen, M_ISCSI, M_WAITOK);
1708 error = copyin(ids->ids_data_segment, data, datalen);
1710 free(data, M_ISCSI);
1715 ip = icl_pdu_new(is->is_conn, M_WAITOK);
1716 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs));
1718 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1719 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed"));
1720 free(data, M_ISCSI);
1722 iscsi_pdu_queue(ip);
1728 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc,
1729 struct iscsi_daemon_receive *idr)
1731 struct iscsi_session *is;
1736 sx_slock(&sc->sc_lock);
1737 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1738 if (is->is_id == idr->idr_session_id)
1742 sx_sunlock(&sc->sc_lock);
1745 sx_sunlock(&sc->sc_lock);
1747 if (is->is_login_phase == false)
1750 ISCSI_SESSION_LOCK(is);
1751 while (is->is_login_pdu == NULL &&
1752 is->is_terminating == false &&
1753 is->is_reconnecting == false) {
1754 error = cv_wait_sig(&is->is_login_cv, &is->is_lock);
1756 ISCSI_SESSION_UNLOCK(is);
1760 if (is->is_terminating || is->is_reconnecting) {
1761 ISCSI_SESSION_UNLOCK(is);
1764 ip = is->is_login_pdu;
1765 is->is_login_pdu = NULL;
1766 ISCSI_SESSION_UNLOCK(is);
1768 if (ip->ip_data_len > idr->idr_data_segment_len) {
1773 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs));
1774 if (ip->ip_data_len > 0) {
1775 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK);
1776 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1777 copyout(data, idr->idr_data_segment, ip->ip_data_len);
1778 free(data, M_ISCSI);
1785 #endif /* ICL_KERNEL_PROXY */
1788 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc)
1791 * Just make sure all the fields are null-terminated.
1793 * XXX: This is not particularly secure. We should
1794 * create our own conf and then copy in relevant
1797 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0';
1798 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0';
1799 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0';
1800 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0';
1801 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0';
1802 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0';
1803 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0';
1804 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0';
1805 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0';
1809 iscsi_valid_session_conf(const struct iscsi_session_conf *isc)
1812 if (isc->isc_initiator[0] == '\0') {
1813 ISCSI_DEBUG("empty isc_initiator");
1817 if (isc->isc_target_addr[0] == '\0') {
1818 ISCSI_DEBUG("empty isc_target_addr");
1822 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) {
1823 ISCSI_DEBUG("non-empty isc_target for discovery session");
1827 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) {
1828 ISCSI_DEBUG("empty isc_target for non-discovery session");
1836 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa)
1838 struct iscsi_session *is;
1839 const struct iscsi_session *is2;
1843 iscsi_sanitize_session_conf(&isa->isa_conf);
1844 if (iscsi_valid_session_conf(&isa->isa_conf) == false)
1847 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK);
1848 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf));
1850 sx_xlock(&sc->sc_lock);
1853 * Prevent duplicates.
1855 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
1856 if (!!is->is_conf.isc_discovery !=
1857 !!is2->is_conf.isc_discovery)
1860 if (strcmp(is->is_conf.isc_target_addr,
1861 is2->is_conf.isc_target_addr) != 0)
1864 if (is->is_conf.isc_discovery == 0 &&
1865 strcmp(is->is_conf.isc_target,
1866 is2->is_conf.isc_target) != 0)
1869 sx_xunlock(&sc->sc_lock);
1874 is->is_conn = icl_new_conn(is->is_conf.isc_offload,
1875 is->is_conf.isc_iser, "iscsi", &is->is_lock);
1876 if (is->is_conn == NULL) {
1877 sx_xunlock(&sc->sc_lock);
1881 is->is_conn->ic_receive = iscsi_receive_callback;
1882 is->is_conn->ic_error = iscsi_error_callback;
1883 is->is_conn->ic_prv0 = is;
1884 TAILQ_INIT(&is->is_outstanding);
1885 STAILQ_INIT(&is->is_postponed);
1886 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF);
1887 cv_init(&is->is_maintenance_cv, "iscsi_mt");
1888 #ifdef ICL_KERNEL_PROXY
1889 cv_init(&is->is_login_cv, "iscsi_login");
1893 * Set some default values, from RFC 3720, section 12.
1895 * These values are updated by the handoff IOCTL, but are
1896 * needed prior to the handoff to support sending the ISER
1899 is->is_conn->ic_max_recv_data_segment_length = 8192;
1900 is->is_conn->ic_max_send_data_segment_length = 8192;
1901 is->is_max_burst_length = 262144;
1902 is->is_first_burst_length = 65536;
1905 sc->sc_last_session_id++;
1906 is->is_id = sc->sc_last_session_id;
1907 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */
1908 arc4rand(&is->is_isid[1], 5, 0);
1910 callout_init(&is->is_callout, 1);
1912 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt");
1914 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error);
1915 sx_xunlock(&sc->sc_lock);
1921 callout_reset_sbt(&is->is_callout, sbt, pr, iscsi_callout, is, 0);
1922 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next);
1924 ISCSI_SESSION_LOCK(is);
1926 * Don't notify iscsid(8) if the session is disabled and it's not
1927 * a discovery session,
1929 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
1930 ISCSI_SESSION_UNLOCK(is);
1931 sx_xunlock(&sc->sc_lock);
1935 is->is_waiting_for_iscsid = true;
1936 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
1937 ISCSI_SESSION_UNLOCK(is);
1938 cv_signal(&sc->sc_cv);
1939 sx_xunlock(&sc->sc_lock);
1944 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1,
1945 unsigned int id2, const struct iscsi_session_conf *c2)
1948 if (id2 != 0 && id2 != id1)
1950 if (c2->isc_target[0] != '\0' &&
1951 strcmp(c1->isc_target, c2->isc_target) != 0)
1953 if (c2->isc_target_addr[0] != '\0' &&
1954 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0)
1960 iscsi_ioctl_session_remove(struct iscsi_softc *sc,
1961 struct iscsi_session_remove *isr)
1963 struct iscsi_session *is, *tmp;
1966 iscsi_sanitize_session_conf(&isr->isr_conf);
1968 sx_xlock(&sc->sc_lock);
1969 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) {
1970 ISCSI_SESSION_LOCK(is);
1971 if (iscsi_session_conf_matches(is->is_id, &is->is_conf,
1972 isr->isr_session_id, &isr->isr_conf)) {
1974 iscsi_session_logout(is);
1975 iscsi_session_terminate(is);
1977 ISCSI_SESSION_UNLOCK(is);
1979 sx_xunlock(&sc->sc_lock);
1988 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl)
1992 struct iscsi_session *is;
1993 struct iscsi_session_state iss;
1995 sx_slock(&sc->sc_lock);
1996 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1997 if (i >= isl->isl_nentries) {
1998 sx_sunlock(&sc->sc_lock);
2001 memset(&iss, 0, sizeof(iss));
2002 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf));
2003 iss.iss_id = is->is_id;
2004 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias));
2005 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason));
2006 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload));
2008 if (is->is_conn->ic_header_crc32c)
2009 iss.iss_header_digest = ISCSI_DIGEST_CRC32C;
2011 iss.iss_header_digest = ISCSI_DIGEST_NONE;
2013 if (is->is_conn->ic_data_crc32c)
2014 iss.iss_data_digest = ISCSI_DIGEST_CRC32C;
2016 iss.iss_data_digest = ISCSI_DIGEST_NONE;
2018 iss.iss_max_send_data_segment_length =
2019 is->is_conn->ic_max_send_data_segment_length;
2020 iss.iss_max_recv_data_segment_length =
2021 is->is_conn->ic_max_recv_data_segment_length;
2022 iss.iss_max_burst_length = is->is_max_burst_length;
2023 iss.iss_first_burst_length = is->is_first_burst_length;
2024 iss.iss_immediate_data = is->is_immediate_data;
2025 iss.iss_connected = is->is_connected;
2027 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss));
2029 sx_sunlock(&sc->sc_lock);
2034 sx_sunlock(&sc->sc_lock);
2036 isl->isl_nentries = i;
2042 iscsi_ioctl_session_modify(struct iscsi_softc *sc,
2043 struct iscsi_session_modify *ism)
2045 struct iscsi_session *is;
2046 const struct iscsi_session *is2;
2048 iscsi_sanitize_session_conf(&ism->ism_conf);
2049 if (iscsi_valid_session_conf(&ism->ism_conf) == false)
2052 sx_xlock(&sc->sc_lock);
2053 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2054 ISCSI_SESSION_LOCK(is);
2055 if (is->is_id == ism->ism_session_id) {
2056 /* Note that the session remains locked. */
2059 ISCSI_SESSION_UNLOCK(is);
2062 sx_xunlock(&sc->sc_lock);
2067 * Prevent duplicates.
2069 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
2073 if (!!ism->ism_conf.isc_discovery !=
2074 !!is2->is_conf.isc_discovery)
2077 if (strcmp(ism->ism_conf.isc_target_addr,
2078 is2->is_conf.isc_target_addr) != 0)
2081 if (ism->ism_conf.isc_discovery == 0 &&
2082 strcmp(ism->ism_conf.isc_target,
2083 is2->is_conf.isc_target) != 0)
2086 ISCSI_SESSION_UNLOCK(is);
2087 sx_xunlock(&sc->sc_lock);
2091 sx_xunlock(&sc->sc_lock);
2093 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf));
2094 ISCSI_SESSION_UNLOCK(is);
2096 iscsi_session_reconnect(is);
2102 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
2105 struct iscsi_softc *sc;
2111 return (iscsi_ioctl_daemon_wait(sc,
2112 (struct iscsi_daemon_request *)arg));
2114 return (iscsi_ioctl_daemon_handoff(sc,
2115 (struct iscsi_daemon_handoff *)arg));
2117 return (iscsi_ioctl_daemon_fail(sc,
2118 (struct iscsi_daemon_fail *)arg));
2119 #ifdef ICL_KERNEL_PROXY
2121 return (iscsi_ioctl_daemon_connect(sc,
2122 (struct iscsi_daemon_connect *)arg));
2124 return (iscsi_ioctl_daemon_send(sc,
2125 (struct iscsi_daemon_send *)arg));
2127 return (iscsi_ioctl_daemon_receive(sc,
2128 (struct iscsi_daemon_receive *)arg));
2129 #endif /* ICL_KERNEL_PROXY */
2131 return (iscsi_ioctl_session_add(sc,
2132 (struct iscsi_session_add *)arg));
2134 return (iscsi_ioctl_session_remove(sc,
2135 (struct iscsi_session_remove *)arg));
2137 return (iscsi_ioctl_session_list(sc,
2138 (struct iscsi_session_list *)arg));
2140 return (iscsi_ioctl_session_modify(sc,
2141 (struct iscsi_session_modify *)arg));
2147 static struct iscsi_outstanding *
2148 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag)
2150 struct iscsi_outstanding *io;
2152 ISCSI_SESSION_LOCK_ASSERT(is);
2154 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2155 if (io->io_initiator_task_tag == initiator_task_tag)
2161 static struct iscsi_outstanding *
2162 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb)
2164 struct iscsi_outstanding *io;
2166 ISCSI_SESSION_LOCK_ASSERT(is);
2168 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2169 if (io->io_ccb == ccb)
2175 static struct iscsi_outstanding *
2176 iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request,
2177 union ccb *ccb, uint32_t *initiator_task_tagp)
2179 struct iscsi_outstanding *io;
2182 ISCSI_SESSION_LOCK_ASSERT(is);
2184 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO);
2186 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes",
2191 error = icl_conn_task_setup(is->is_conn, request, &ccb->csio,
2192 initiator_task_tagp, &io->io_icl_prv);
2194 ISCSI_SESSION_WARN(is,
2195 "icl_conn_task_setup() failed with error %d", error);
2196 uma_zfree(iscsi_outstanding_zone, io);
2200 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL,
2201 ("initiator_task_tag 0x%x already added", *initiator_task_tagp));
2203 io->io_initiator_task_tag = *initiator_task_tagp;
2205 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next);
2210 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io)
2213 ISCSI_SESSION_LOCK_ASSERT(is);
2215 icl_conn_task_done(is->is_conn, io->io_icl_prv);
2216 TAILQ_REMOVE(&is->is_outstanding, io, io_next);
2217 uma_zfree(iscsi_outstanding_zone, io);
2221 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb)
2223 struct icl_pdu *request;
2224 struct iscsi_bhs_task_management_request *bhstmr;
2225 struct ccb_abort *cab = &ccb->cab;
2226 struct iscsi_outstanding *io, *aio;
2227 uint32_t initiator_task_tag;
2229 ISCSI_SESSION_LOCK_ASSERT(is);
2232 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2234 if (is->is_login_phase) {
2235 ccb->ccb_h.status = CAM_REQ_ABORTED;
2241 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb);
2243 ccb->ccb_h.status = CAM_REQ_CMP;
2248 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2249 if (request == NULL) {
2250 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2255 initiator_task_tag = is->is_initiator_task_tag++;
2256 if (initiator_task_tag == 0xffffffff)
2257 initiator_task_tag = is->is_initiator_task_tag++;
2259 io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag);
2261 icl_pdu_free(request);
2262 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2266 io->io_referenced_task_tag = aio->io_initiator_task_tag;
2268 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2269 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST;
2270 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK;
2271 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2272 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag;
2273 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag;
2275 iscsi_pdu_queue_locked(request);
2279 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb)
2281 struct icl_pdu *request;
2282 struct iscsi_bhs_scsi_command *bhssc;
2283 struct ccb_scsiio *csio;
2284 struct iscsi_outstanding *io;
2286 uint32_t initiator_task_tag;
2289 ISCSI_SESSION_LOCK_ASSERT(is);
2292 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2294 if (is->is_login_phase) {
2295 ISCSI_SESSION_DEBUG(is, "called during login phase");
2296 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2297 xpt_freeze_devq(ccb->ccb_h.path, 1);
2298 ISCSI_SESSION_DEBUG(is, "freezing devq");
2300 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN;
2306 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2307 if (request == NULL) {
2308 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2309 xpt_freeze_devq(ccb->ccb_h.path, 1);
2310 ISCSI_SESSION_DEBUG(is, "freezing devq");
2312 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2317 initiator_task_tag = is->is_initiator_task_tag++;
2318 if (initiator_task_tag == 0xffffffff)
2319 initiator_task_tag = is->is_initiator_task_tag++;
2321 io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag);
2323 icl_pdu_free(request);
2324 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2325 xpt_freeze_devq(ccb->ccb_h.path, 1);
2326 ISCSI_SESSION_DEBUG(is, "freezing devq");
2328 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2334 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2335 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND;
2336 bhssc->bhssc_flags |= BHSSC_FLAGS_F;
2337 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
2339 bhssc->bhssc_flags |= BHSSC_FLAGS_R;
2342 bhssc->bhssc_flags |= BHSSC_FLAGS_W;
2346 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2347 switch (csio->tag_action) {
2348 case MSG_HEAD_OF_Q_TAG:
2349 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ;
2351 case MSG_ORDERED_Q_TAG:
2352 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED;
2355 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA;
2357 case MSG_SIMPLE_Q_TAG:
2359 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE;
2363 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED;
2365 if (is->is_protocol_level >= 2) {
2366 bhssc->bhssc_pri = (csio->priority << BHSSC_PRI_SHIFT) &
2370 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2371 bhssc->bhssc_initiator_task_tag = initiator_task_tag;
2372 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len);
2373 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb),
2374 ("unsupported CDB size %zd", (size_t)csio->cdb_len));
2376 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2377 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len);
2379 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len);
2381 if (is->is_immediate_data &&
2382 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2383 len = csio->dxfer_len;
2384 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len);
2385 if (len > is->is_first_burst_length) {
2386 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len, is->is_first_burst_length);
2387 len = is->is_first_burst_length;
2389 if (len > is->is_conn->ic_max_send_data_segment_length) {
2390 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len,
2391 is->is_conn->ic_max_send_data_segment_length);
2392 len = is->is_conn->ic_max_send_data_segment_length;
2395 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT);
2397 iscsi_outstanding_remove(is, io);
2398 icl_pdu_free(request);
2399 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2400 xpt_freeze_devq(ccb->ccb_h.path, 1);
2401 ISCSI_SESSION_DEBUG(is, "freezing devq");
2403 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2408 iscsi_pdu_queue_locked(request);
2412 iscsi_action(struct cam_sim *sim, union ccb *ccb)
2414 struct iscsi_session *is;
2416 is = cam_sim_softc(sim);
2418 ISCSI_SESSION_LOCK_ASSERT(is);
2420 if (is->is_terminating ||
2421 (is->is_connected == false && fail_on_disconnection)) {
2422 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2428 * Make sure CAM doesn't sneak in a CCB just after freezing the queue.
2430 if (is->is_simq_frozen == true) {
2431 ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
2432 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
2433 /* Don't freeze the devq - the SIM queue is already frozen. */
2438 switch (ccb->ccb_h.func_code) {
2441 struct ccb_pathinq *cpi = &ccb->cpi;
2443 cpi->version_num = 1;
2444 cpi->hba_inquiry = PI_TAG_ABLE;
2445 cpi->target_sprt = 0;
2446 cpi->hba_misc = PIM_EXTLUNS;
2448 * XXX: It shouldn't ever be NULL; this could be turned
2449 * into a KASSERT eventually.
2451 if (is->is_conn == NULL)
2452 ISCSI_WARN("NULL conn");
2453 else if (is->is_conn->ic_unmapped)
2454 cpi->hba_misc |= PIM_UNMAPPED;
2455 cpi->hba_eng_cnt = 0;
2456 cpi->max_target = 0;
2458 * Note that the variable below is only relevant for targets
2459 * that don't claim compliance with anything above SPC2, which
2460 * means they don't support REPORT_LUNS.
2463 cpi->initiator_id = ~0;
2464 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2465 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
2466 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2467 cpi->unit_number = cam_sim_unit(sim);
2468 cpi->bus_id = cam_sim_bus(sim);
2469 cpi->base_transfer_speed = 150000; /* XXX */
2470 cpi->transport = XPORT_ISCSI;
2471 cpi->transport_version = 0;
2472 cpi->protocol = PROTO_SCSI;
2473 cpi->protocol_version = SCSI_REV_SPC3;
2474 cpi->maxio = maxphys;
2475 cpi->ccb_h.status = CAM_REQ_CMP;
2478 case XPT_GET_TRAN_SETTINGS:
2480 struct ccb_trans_settings *cts;
2481 struct ccb_trans_settings_scsi *scsi;
2484 scsi = &cts->proto_specific.scsi;
2486 cts->protocol = PROTO_SCSI;
2487 cts->protocol_version = SCSI_REV_SPC3;
2488 cts->transport = XPORT_ISCSI;
2489 cts->transport_version = 0;
2490 scsi->valid = CTS_SCSI_VALID_TQ;
2491 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2492 cts->ccb_h.status = CAM_REQ_CMP;
2495 case XPT_CALC_GEOMETRY:
2496 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2497 ccb->ccb_h.status = CAM_REQ_CMP;
2501 * XXX: What's the point?
2505 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io");
2506 ccb->ccb_h.status = CAM_REQ_CMP;
2510 iscsi_action_abort(is, ccb);
2513 iscsi_action_scsiio(is, ccb);
2517 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code);
2519 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2526 iscsi_terminate_sessions(struct iscsi_softc *sc)
2528 struct iscsi_session *is;
2530 sx_slock(&sc->sc_lock);
2531 TAILQ_FOREACH(is, &sc->sc_sessions, is_next)
2532 iscsi_session_terminate(is);
2533 while(!TAILQ_EMPTY(&sc->sc_sessions)) {
2534 ISCSI_DEBUG("waiting for sessions to terminate");
2535 cv_wait(&sc->sc_cv, &sc->sc_lock);
2537 ISCSI_DEBUG("all sessions terminated");
2538 sx_sunlock(&sc->sc_lock);
2542 iscsi_shutdown_pre(struct iscsi_softc *sc)
2544 struct iscsi_session *is;
2546 if (!fail_on_shutdown)
2550 * If we have any sessions waiting for reconnection, request
2551 * maintenance thread to fail them immediately instead of waiting
2552 * for reconnect timeout.
2554 * This prevents LUNs with mounted filesystems that are supported
2555 * by disconnected iSCSI sessions from hanging, however it will
2556 * fail all queued BIOs.
2558 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown");
2560 fail_on_disconnection = 1;
2562 sx_slock(&sc->sc_lock);
2563 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2564 ISCSI_SESSION_LOCK(is);
2565 if (!is->is_connected) {
2566 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early");
2567 iscsi_session_reconnect(is);
2569 ISCSI_SESSION_UNLOCK(is);
2571 sx_sunlock(&sc->sc_lock);
2575 iscsi_shutdown_post(struct iscsi_softc *sc)
2578 if (!KERNEL_PANICKED()) {
2579 ISCSI_DEBUG("removing all sessions due to shutdown");
2580 iscsi_terminate_sessions(sc);
2589 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK);
2590 sx_init(&sc->sc_lock, "iscsi");
2591 TAILQ_INIT(&sc->sc_sessions);
2592 cv_init(&sc->sc_cv, "iscsi_cv");
2594 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding",
2595 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL,
2598 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw,
2599 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
2601 ISCSI_WARN("failed to create device node, error %d", error);
2604 sc->sc_cdev->si_drv1 = sc;
2606 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2607 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST);
2609 * shutdown_post_sync needs to run after filesystem shutdown and before
2610 * CAM shutdown - otherwise when rebooting with an iSCSI session that is
2611 * disconnected but has outstanding requests, dashutdown() will hang on
2612 * cam_periph_runccb().
2614 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
2615 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1);
2624 /* Awaken any threads asleep in iscsi_ioctl(). */
2625 sx_xlock(&sc->sc_lock);
2626 sc->sc_unloading = true;
2627 cv_signal(&sc->sc_cv);
2628 sx_xunlock(&sc->sc_lock);
2630 if (sc->sc_cdev != NULL) {
2631 ISCSI_DEBUG("removing device node");
2632 destroy_dev(sc->sc_cdev);
2633 ISCSI_DEBUG("device node removed");
2636 if (sc->sc_shutdown_pre_eh != NULL)
2637 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh);
2638 if (sc->sc_shutdown_post_eh != NULL)
2639 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh);
2641 iscsi_terminate_sessions(sc);
2643 uma_zdestroy(iscsi_outstanding_zone);
2644 sx_destroy(&sc->sc_lock);
2645 cv_destroy(&sc->sc_cv);
2653 sx_slock(&sc->sc_lock);
2654 if (!TAILQ_EMPTY(&sc->sc_sessions)) {
2655 sx_sunlock(&sc->sc_lock);
2658 sx_sunlock(&sc->sc_lock);
2663 iscsi_modevent(module_t mod, int what, void *arg)
2669 error = iscsi_load();
2672 error = iscsi_unload();
2675 error = iscsi_quiesce();
2684 moduledata_t iscsi_data = {
2690 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2691 MODULE_DEPEND(iscsi, cam, 1, 1, 1);
2692 MODULE_DEPEND(iscsi, icl, 1, 1, 1);