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;
552 ISCSI_SESSION_LOCK(is);
553 if (is->is_terminating) {
554 ISCSI_SESSION_UNLOCK(is);
558 callout_schedule(&is->is_callout, 1 * hz);
560 if (is->is_conf.isc_enable == 0)
565 if (is->is_waiting_for_iscsid) {
566 if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) {
567 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) "
568 "for %d seconds; reconnecting",
570 reconnect_needed = true;
575 if (is->is_login_phase) {
576 if (login_timeout > 0 && is->is_timeout > login_timeout) {
577 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; "
578 "reconnecting", is->is_timeout);
579 reconnect_needed = true;
584 if (ping_timeout <= 0) {
586 * Pings are disabled. Don't send NOP-Out in this case.
587 * Reset the timeout, to avoid triggering reconnection,
588 * should the user decide to reenable them.
594 if (is->is_timeout >= ping_timeout) {
595 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; "
596 "reconnecting", ping_timeout);
597 reconnect_needed = true;
601 ISCSI_SESSION_UNLOCK(is);
604 * If the ping was reset less than one second ago - which means
605 * that we've received some PDU during the last second - assume
606 * the traffic flows correctly and don't bother sending a NOP-Out.
608 * (It's 2 - one for one second, and one for incrementing is_timeout
609 * earlier in this routine.)
611 if (is->is_timeout < 2)
614 request = icl_pdu_new(is->is_conn, M_NOWAIT);
615 if (request == NULL) {
616 ISCSI_SESSION_WARN(is, "failed to allocate PDU");
619 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
620 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
621 ISCSI_BHS_OPCODE_IMMEDIATE;
622 bhsno->bhsno_flags = 0x80;
623 bhsno->bhsno_target_transfer_tag = 0xffffffff;
624 iscsi_pdu_queue(request);
628 if (is->is_terminating) {
629 ISCSI_SESSION_UNLOCK(is);
633 ISCSI_SESSION_UNLOCK(is);
635 if (reconnect_needed)
636 iscsi_session_reconnect(is);
640 iscsi_pdu_update_statsn(const struct icl_pdu *response)
642 const struct iscsi_bhs_data_in *bhsdi;
643 struct iscsi_session *is;
644 uint32_t expcmdsn, maxcmdsn, statsn;
646 is = PDU_SESSION(response);
648 ISCSI_SESSION_LOCK_ASSERT(is);
651 * We're only using fields common for all the response
652 * (target -> initiator) PDUs.
654 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs;
656 * Ok, I lied. In case of Data-In, "The fields StatSN, Status,
657 * and Residual Count only have meaningful content if the S bit
658 * is set to 1", so we also need to check the bit specific for
661 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
662 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) {
663 statsn = ntohl(bhsdi->bhsdi_statsn);
664 if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) {
665 /* XXX: This is normal situation for MCS */
666 ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != "
667 "session ExpStatSN %u (or + 1); reconnecting",
668 bhsdi->bhsdi_opcode, statsn, is->is_statsn);
669 iscsi_session_reconnect(is);
671 if (ISCSI_SNGT(statsn, is->is_statsn))
672 is->is_statsn = statsn;
675 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn);
676 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn);
678 if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) {
679 ISCSI_SESSION_DEBUG(is,
680 "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring",
683 if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) {
684 is->is_maxcmdsn = maxcmdsn;
687 * Command window increased; kick the maintanance thread
688 * to send out postponed commands.
690 if (!STAILQ_EMPTY(&is->is_postponed))
691 cv_signal(&is->is_maintenance_cv);
692 } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) {
693 /* XXX: This is normal situation for MCS */
694 ISCSI_SESSION_DEBUG(is,
695 "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring",
696 maxcmdsn, is->is_maxcmdsn);
699 if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) {
700 is->is_expcmdsn = expcmdsn;
701 } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) {
702 /* XXX: This is normal situation for MCS */
703 ISCSI_SESSION_DEBUG(is,
704 "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring",
705 expcmdsn, is->is_expcmdsn);
710 * Every incoming PDU - not just NOP-In - resets the ping timer.
711 * The purpose of the timeout is to reset the connection when it stalls;
712 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
719 iscsi_receive_callback(struct icl_pdu *response)
721 struct iscsi_session *is;
723 is = PDU_SESSION(response);
725 ISCSI_SESSION_LOCK(is);
727 iscsi_pdu_update_statsn(response);
729 #ifdef ICL_KERNEL_PROXY
730 if (is->is_login_phase) {
731 if (is->is_login_pdu == NULL)
732 is->is_login_pdu = response;
734 icl_pdu_free(response);
735 ISCSI_SESSION_UNLOCK(is);
736 cv_signal(&is->is_login_cv);
742 * The handling routine is responsible for freeing the PDU
743 * when it's no longer needed.
745 switch (response->ip_bhs->bhs_opcode) {
746 case ISCSI_BHS_OPCODE_NOP_IN:
747 iscsi_pdu_handle_nop_in(response);
748 ISCSI_SESSION_UNLOCK(is);
750 case ISCSI_BHS_OPCODE_SCSI_RESPONSE:
751 iscsi_pdu_handle_scsi_response(response);
752 /* Session lock dropped inside. */
753 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
755 case ISCSI_BHS_OPCODE_TASK_RESPONSE:
756 iscsi_pdu_handle_task_response(response);
757 ISCSI_SESSION_UNLOCK(is);
759 case ISCSI_BHS_OPCODE_SCSI_DATA_IN:
760 iscsi_pdu_handle_data_in(response);
761 /* Session lock dropped inside. */
762 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
764 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE:
765 iscsi_pdu_handle_logout_response(response);
766 ISCSI_SESSION_UNLOCK(is);
768 case ISCSI_BHS_OPCODE_R2T:
769 iscsi_pdu_handle_r2t(response);
770 ISCSI_SESSION_UNLOCK(is);
772 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE:
773 iscsi_pdu_handle_async_message(response);
774 ISCSI_SESSION_UNLOCK(is);
776 case ISCSI_BHS_OPCODE_REJECT:
777 iscsi_pdu_handle_reject(response);
778 ISCSI_SESSION_UNLOCK(is);
781 ISCSI_SESSION_WARN(is, "received PDU with unsupported "
782 "opcode 0x%x; reconnecting",
783 response->ip_bhs->bhs_opcode);
784 iscsi_session_reconnect(is);
785 ISCSI_SESSION_UNLOCK(is);
786 icl_pdu_free(response);
791 iscsi_error_callback(struct icl_conn *ic)
793 struct iscsi_session *is;
795 is = CONN_SESSION(ic);
797 ISCSI_SESSION_WARN(is, "connection error; reconnecting");
798 iscsi_session_reconnect(is);
802 iscsi_pdu_handle_nop_in(struct icl_pdu *response)
804 struct iscsi_session *is;
805 struct iscsi_bhs_nop_out *bhsno;
806 struct iscsi_bhs_nop_in *bhsni;
807 struct icl_pdu *request;
812 is = PDU_SESSION(response);
813 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
815 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) {
817 * Nothing to do; iscsi_pdu_update_statsn() already
818 * zeroed the timeout.
820 icl_pdu_free(response);
824 datasize = icl_pdu_data_segment_length(response);
826 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO);
828 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
830 icl_pdu_free(response);
831 iscsi_session_reconnect(is);
834 icl_pdu_get_data(response, 0, data, datasize);
837 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
838 if (request == NULL) {
839 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
842 icl_pdu_free(response);
843 iscsi_session_reconnect(is);
846 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
847 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
848 ISCSI_BHS_OPCODE_IMMEDIATE;
849 bhsno->bhsno_flags = 0x80;
850 bhsno->bhsno_initiator_task_tag = 0xffffffff;
851 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag;
853 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT);
855 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
858 icl_pdu_free(request);
859 icl_pdu_free(response);
860 iscsi_session_reconnect(is);
866 icl_pdu_free(response);
867 iscsi_pdu_queue_locked(request);
871 iscsi_pdu_handle_scsi_response(struct icl_pdu *response)
873 struct iscsi_bhs_scsi_response *bhssr;
874 struct iscsi_outstanding *io;
875 struct iscsi_session *is;
877 struct ccb_scsiio *csio;
878 size_t data_segment_len, received;
882 is = PDU_SESSION(response);
884 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
885 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag);
886 if (io == NULL || io->io_ccb == NULL) {
887 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag);
888 icl_pdu_free(response);
889 iscsi_session_reconnect(is);
890 ISCSI_SESSION_UNLOCK(is);
897 * With iSER, after getting good response we can be sure
898 * that all the data has been successfully transferred.
900 if (is->is_conn->ic_iser) {
901 resid = ntohl(bhssr->bhssr_residual_count);
902 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) {
903 io->io_received = ccb->csio.dxfer_len - resid;
904 } else if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) {
905 ISCSI_SESSION_WARN(is, "overflow: target indicates %d", resid);
907 io->io_received = ccb->csio.dxfer_len;
911 received = io->io_received;
912 iscsi_outstanding_remove(is, io);
913 ISCSI_SESSION_UNLOCK(is);
915 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) {
916 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response);
917 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
918 xpt_freeze_devq(ccb->ccb_h.path, 1);
919 ISCSI_SESSION_DEBUG(is, "freezing devq");
921 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
922 } else if (bhssr->bhssr_status == 0) {
923 ccb->ccb_h.status = CAM_REQ_CMP;
925 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
926 xpt_freeze_devq(ccb->ccb_h.path, 1);
927 ISCSI_SESSION_DEBUG(is, "freezing devq");
929 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
930 ccb->csio.scsi_status = bhssr->bhssr_status;
934 data_segment_len = icl_pdu_data_segment_length(response);
935 if (data_segment_len > 0) {
936 if (data_segment_len < sizeof(sense_len)) {
937 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)",
939 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
940 xpt_freeze_devq(ccb->ccb_h.path, 1);
941 ISCSI_SESSION_DEBUG(is, "freezing devq");
943 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
946 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len));
947 sense_len = ntohs(sense_len);
949 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd",
950 sense_len, data_segment_len);
952 if (sizeof(sense_len) + sense_len > data_segment_len) {
953 ISCSI_SESSION_WARN(is, "truncated data segment "
954 "(%zd bytes, should be %zd)",
955 data_segment_len, sizeof(sense_len) + sense_len);
956 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
957 xpt_freeze_devq(ccb->ccb_h.path, 1);
958 ISCSI_SESSION_DEBUG(is, "freezing devq");
960 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
962 } else if (sizeof(sense_len) + sense_len < data_segment_len)
963 ISCSI_SESSION_WARN(is, "oversize data segment "
964 "(%zd bytes, should be %zd)",
965 data_segment_len, sizeof(sense_len) + sense_len);
966 if (sense_len > csio->sense_len) {
967 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d",
968 sense_len, csio->sense_len);
969 sense_len = csio->sense_len;
971 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len);
972 csio->sense_resid = csio->sense_len - sense_len;
973 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
977 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW)
978 csio->resid = ntohl(bhssr->bhssr_residual_count);
980 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
981 KASSERT(received <= csio->dxfer_len,
982 ("received > csio->dxfer_len"));
983 if (received < csio->dxfer_len) {
984 if (csio->resid != csio->dxfer_len - received) {
985 ISCSI_SESSION_WARN(is, "underflow mismatch: "
986 "target indicates %d, we calculated %zd",
987 csio->resid, csio->dxfer_len - received);
989 csio->resid = csio->dxfer_len - received;
994 icl_pdu_free(response);
998 iscsi_pdu_handle_task_response(struct icl_pdu *response)
1000 struct iscsi_bhs_task_management_response *bhstmr;
1001 struct iscsi_outstanding *io, *aio;
1002 struct iscsi_session *is;
1004 is = PDU_SESSION(response);
1006 bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs;
1007 io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag);
1008 if (io == NULL || io->io_ccb != NULL) {
1009 ISCSI_SESSION_WARN(is, "bad itt 0x%x",
1010 bhstmr->bhstmr_initiator_task_tag);
1011 icl_pdu_free(response);
1012 iscsi_session_reconnect(is);
1016 if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) {
1017 ISCSI_SESSION_WARN(is, "task response 0x%x",
1018 bhstmr->bhstmr_response);
1020 aio = iscsi_outstanding_find(is, io->io_referenced_task_tag);
1021 if (aio != NULL && aio->io_ccb != NULL)
1022 iscsi_session_terminate_task(is, aio, CAM_REQ_ABORTED);
1025 iscsi_outstanding_remove(is, io);
1026 icl_pdu_free(response);
1030 iscsi_pdu_handle_data_in(struct icl_pdu *response)
1032 struct iscsi_bhs_data_in *bhsdi;
1033 struct iscsi_outstanding *io;
1034 struct iscsi_session *is;
1036 struct ccb_scsiio *csio;
1037 size_t data_segment_len, received, oreceived;
1039 is = PDU_SESSION(response);
1040 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
1041 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag);
1042 if (io == NULL || io->io_ccb == NULL) {
1043 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag);
1044 icl_pdu_free(response);
1045 iscsi_session_reconnect(is);
1046 ISCSI_SESSION_UNLOCK(is);
1050 data_segment_len = icl_pdu_data_segment_length(response);
1051 if (data_segment_len == 0) {
1053 * "The sending of 0 length data segments should be avoided,
1054 * but initiators and targets MUST be able to properly receive
1055 * 0 length data segments."
1057 ISCSI_SESSION_UNLOCK(is);
1058 icl_pdu_free(response);
1063 * We need to track this for security reasons - without it, malicious target
1064 * could respond to SCSI READ without sending Data-In PDUs, which would result
1065 * in read operation on the initiator side returning random kernel data.
1067 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) {
1068 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd",
1069 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset));
1070 icl_pdu_free(response);
1071 iscsi_session_reconnect(is);
1072 ISCSI_SESSION_UNLOCK(is);
1079 if (io->io_received + data_segment_len > csio->dxfer_len) {
1080 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes "
1081 "at offset %zd, buffer is %d)",
1082 data_segment_len, io->io_received, csio->dxfer_len);
1083 icl_pdu_free(response);
1084 iscsi_session_reconnect(is);
1085 ISCSI_SESSION_UNLOCK(is);
1089 oreceived = io->io_received;
1090 io->io_received += data_segment_len;
1091 received = io->io_received;
1092 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0)
1093 iscsi_outstanding_remove(is, io);
1094 ISCSI_SESSION_UNLOCK(is);
1096 icl_pdu_get_data(response, 0, csio->data_ptr + oreceived, data_segment_len);
1099 * XXX: Check DataSN.
1102 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) {
1104 * Nothing more to do.
1106 icl_pdu_free(response);
1110 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status);
1111 if (bhsdi->bhsdi_status == 0) {
1112 ccb->ccb_h.status = CAM_REQ_CMP;
1114 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1115 xpt_freeze_devq(ccb->ccb_h.path, 1);
1116 ISCSI_SESSION_DEBUG(is, "freezing devq");
1118 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
1119 csio->scsi_status = bhsdi->bhsdi_status;
1122 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1123 KASSERT(received <= csio->dxfer_len,
1124 ("received > csio->dxfer_len"));
1125 if (received < csio->dxfer_len) {
1126 csio->resid = ntohl(bhsdi->bhsdi_residual_count);
1127 if (csio->resid != csio->dxfer_len - received) {
1128 ISCSI_SESSION_WARN(is, "underflow mismatch: "
1129 "target indicates %d, we calculated %zd",
1130 csio->resid, csio->dxfer_len - received);
1132 csio->resid = csio->dxfer_len - received;
1137 icl_pdu_free(response);
1141 iscsi_pdu_handle_logout_response(struct icl_pdu *response)
1144 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response");
1145 icl_pdu_free(response);
1149 iscsi_pdu_handle_r2t(struct icl_pdu *response)
1151 struct icl_pdu *request;
1152 struct iscsi_session *is;
1153 struct iscsi_bhs_r2t *bhsr2t;
1154 struct iscsi_bhs_data_out *bhsdo;
1155 struct iscsi_outstanding *io;
1156 struct ccb_scsiio *csio;
1157 size_t off, len, total_len;
1159 uint32_t datasn = 0;
1161 is = PDU_SESSION(response);
1163 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
1164 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag);
1165 if (io == NULL || io->io_ccb == NULL) {
1166 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting",
1167 bhsr2t->bhsr2t_initiator_task_tag);
1168 icl_pdu_free(response);
1169 iscsi_session_reconnect(is);
1173 csio = &io->io_ccb->csio;
1175 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) {
1176 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting");
1177 icl_pdu_free(response);
1178 iscsi_session_reconnect(is);
1183 * XXX: Verify R2TSN.
1186 off = ntohl(bhsr2t->bhsr2t_buffer_offset);
1187 if (off > csio->dxfer_len) {
1188 ISCSI_SESSION_WARN(is, "target requested invalid offset "
1189 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len);
1190 icl_pdu_free(response);
1191 iscsi_session_reconnect(is);
1195 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length);
1196 if (total_len == 0 || total_len > csio->dxfer_len) {
1197 ISCSI_SESSION_WARN(is, "target requested invalid length "
1198 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len);
1199 icl_pdu_free(response);
1200 iscsi_session_reconnect(is);
1204 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len);
1209 if (len > is->is_conn->ic_max_send_data_segment_length)
1210 len = is->is_conn->ic_max_send_data_segment_length;
1212 if (off + len > csio->dxfer_len) {
1213 ISCSI_SESSION_WARN(is, "target requested invalid "
1214 "length/offset %zd, buffer is %d; reconnecting",
1215 off + len, csio->dxfer_len);
1216 icl_pdu_free(response);
1217 iscsi_session_reconnect(is);
1221 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
1222 if (request == NULL) {
1223 icl_pdu_free(response);
1224 iscsi_session_reconnect(is);
1228 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
1229 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT;
1230 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun;
1231 bhsdo->bhsdo_initiator_task_tag =
1232 bhsr2t->bhsr2t_initiator_task_tag;
1233 bhsdo->bhsdo_target_transfer_tag =
1234 bhsr2t->bhsr2t_target_transfer_tag;
1235 bhsdo->bhsdo_datasn = htonl(datasn++);
1236 bhsdo->bhsdo_buffer_offset = htonl(off);
1237 error = icl_pdu_append_data(request, csio->data_ptr + off, len,
1240 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
1242 icl_pdu_free(request);
1243 icl_pdu_free(response);
1244 iscsi_session_reconnect(is);
1251 if (total_len == 0) {
1252 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F;
1253 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off);
1255 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off);
1258 iscsi_pdu_queue_locked(request);
1264 icl_pdu_free(response);
1268 iscsi_pdu_handle_async_message(struct icl_pdu *response)
1270 struct iscsi_bhs_asynchronous_message *bhsam;
1271 struct iscsi_session *is;
1273 is = PDU_SESSION(response);
1274 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1275 switch (bhsam->bhsam_async_event) {
1276 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT:
1277 ISCSI_SESSION_WARN(is, "target requests logout; removing session");
1278 iscsi_session_logout(is);
1279 iscsi_session_terminate(is);
1281 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION:
1282 ISCSI_SESSION_WARN(is, "target indicates it will drop the connection");
1284 case BHSAM_EVENT_TARGET_TERMINATES_SESSION:
1285 ISCSI_SESSION_WARN(is, "target indicates it will drop the session");
1289 * XXX: Technically, we're obligated to also handle
1290 * parameter renegotiation.
1292 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event);
1296 icl_pdu_free(response);
1300 iscsi_pdu_handle_reject(struct icl_pdu *response)
1302 struct iscsi_bhs_reject *bhsr;
1303 struct iscsi_session *is;
1305 is = PDU_SESSION(response);
1306 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs;
1307 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?",
1310 icl_pdu_free(response);
1314 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc,
1315 struct iscsi_daemon_request *request)
1317 struct iscsi_session *is;
1318 struct icl_drv_limits idl;
1321 sx_slock(&sc->sc_lock);
1323 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1324 ISCSI_SESSION_LOCK(is);
1325 if (is->is_conf.isc_enable == 0 &&
1326 is->is_conf.isc_discovery == 0) {
1327 ISCSI_SESSION_UNLOCK(is);
1330 if (is->is_waiting_for_iscsid)
1332 ISCSI_SESSION_UNLOCK(is);
1336 if (sc->sc_unloading) {
1337 sx_sunlock(&sc->sc_lock);
1342 * No session requires attention from iscsid(8); wait.
1344 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
1346 sx_sunlock(&sc->sc_lock);
1352 is->is_waiting_for_iscsid = false;
1353 is->is_login_phase = true;
1354 is->is_reason[0] = '\0';
1355 ISCSI_SESSION_UNLOCK(is);
1357 request->idr_session_id = is->is_id;
1358 memcpy(&request->idr_isid, &is->is_isid,
1359 sizeof(request->idr_isid));
1360 request->idr_tsih = 0; /* New or reinstated session. */
1361 memcpy(&request->idr_conf, &is->is_conf,
1362 sizeof(request->idr_conf));
1364 error = icl_limits(is->is_conf.isc_offload,
1365 is->is_conf.isc_iser, &idl);
1367 ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" "
1368 "failed with error %d", is->is_conf.isc_offload,
1370 sx_sunlock(&sc->sc_lock);
1373 request->idr_limits.isl_max_recv_data_segment_length =
1374 idl.idl_max_recv_data_segment_length;
1375 request->idr_limits.isl_max_send_data_segment_length =
1376 idl.idl_max_send_data_segment_length;
1377 request->idr_limits.isl_max_burst_length =
1378 idl.idl_max_burst_length;
1379 request->idr_limits.isl_first_burst_length =
1380 idl.idl_first_burst_length;
1382 sx_sunlock(&sc->sc_lock);
1388 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc,
1389 struct iscsi_daemon_handoff *handoff)
1391 struct iscsi_session *is;
1392 struct icl_conn *ic;
1395 sx_slock(&sc->sc_lock);
1398 * Find the session to hand off socket to.
1400 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1401 if (is->is_id == handoff->idh_session_id)
1405 sx_sunlock(&sc->sc_lock);
1408 ISCSI_SESSION_LOCK(is);
1410 if (is->is_conf.isc_discovery || is->is_terminating) {
1411 ISCSI_SESSION_UNLOCK(is);
1412 sx_sunlock(&sc->sc_lock);
1415 if (is->is_connected) {
1417 * This might have happened because another iscsid(8)
1418 * instance handed off the connection in the meantime.
1421 ISCSI_SESSION_WARN(is, "handoff on already connected "
1423 ISCSI_SESSION_UNLOCK(is);
1424 sx_sunlock(&sc->sc_lock);
1428 strlcpy(is->is_target_alias, handoff->idh_target_alias,
1429 sizeof(is->is_target_alias));
1430 is->is_tsih = handoff->idh_tsih;
1431 is->is_statsn = handoff->idh_statsn;
1432 is->is_protocol_level = handoff->idh_protocol_level;
1433 is->is_initial_r2t = handoff->idh_initial_r2t;
1434 is->is_immediate_data = handoff->idh_immediate_data;
1436 ic->ic_max_recv_data_segment_length =
1437 handoff->idh_max_recv_data_segment_length;
1438 ic->ic_max_send_data_segment_length =
1439 handoff->idh_max_send_data_segment_length;
1440 is->is_max_burst_length = handoff->idh_max_burst_length;
1441 is->is_first_burst_length = handoff->idh_first_burst_length;
1443 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C)
1444 ic->ic_header_crc32c = true;
1446 ic->ic_header_crc32c = false;
1447 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C)
1448 ic->ic_data_crc32c = true;
1450 ic->ic_data_crc32c = false;
1451 ic->ic_maxtags = maxtags;
1454 is->is_expcmdsn = 0;
1455 is->is_maxcmdsn = 0;
1456 is->is_waiting_for_iscsid = false;
1457 is->is_login_phase = false;
1459 is->is_connected = true;
1460 is->is_reason[0] = '\0';
1462 ISCSI_SESSION_UNLOCK(is);
1465 * If we're going through the proxy, the idh_socket will be 0,
1466 * and the ICL module can simply ignore this call. It can also
1467 * use it to determine it's no longer in the Login phase.
1469 error = icl_conn_handoff(ic, handoff->idh_socket);
1471 sx_sunlock(&sc->sc_lock);
1472 iscsi_session_terminate(is);
1476 sx_sunlock(&sc->sc_lock);
1478 if (is->is_sim != NULL) {
1480 * When reconnecting, there already is SIM allocated for the session.
1482 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq"));
1483 ISCSI_SESSION_LOCK(is);
1484 ISCSI_SESSION_DEBUG(is, "releasing");
1485 is->is_simq_frozen = false;
1486 xpt_release_simq(is->is_sim, 1);
1487 ISCSI_SESSION_UNLOCK(is);
1490 ISCSI_SESSION_LOCK(is);
1491 is->is_devq = cam_simq_alloc(ic->ic_maxtags);
1492 if (is->is_devq == NULL) {
1493 ISCSI_SESSION_WARN(is, "failed to allocate simq");
1494 iscsi_session_terminate(is);
1498 is->is_sim = cam_sim_alloc(iscsi_action, NULL, "iscsi",
1499 is, is->is_id /* unit */, &is->is_lock,
1500 1, ic->ic_maxtags, is->is_devq);
1501 if (is->is_sim == NULL) {
1502 ISCSI_SESSION_UNLOCK(is);
1503 ISCSI_SESSION_WARN(is, "failed to allocate SIM");
1504 cam_simq_free(is->is_devq);
1505 iscsi_session_terminate(is);
1509 error = xpt_bus_register(is->is_sim, NULL, 0);
1511 ISCSI_SESSION_UNLOCK(is);
1512 ISCSI_SESSION_WARN(is, "failed to register bus");
1513 iscsi_session_terminate(is);
1517 error = xpt_create_path(&is->is_path, /*periph*/NULL,
1518 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD,
1520 if (error != CAM_REQ_CMP) {
1521 ISCSI_SESSION_UNLOCK(is);
1522 ISCSI_SESSION_WARN(is, "failed to create path");
1523 iscsi_session_terminate(is);
1526 ISCSI_SESSION_UNLOCK(is);
1533 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc,
1534 struct iscsi_daemon_fail *fail)
1536 struct iscsi_session *is;
1538 sx_slock(&sc->sc_lock);
1540 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1541 if (is->is_id == fail->idf_session_id)
1545 sx_sunlock(&sc->sc_lock);
1548 ISCSI_SESSION_LOCK(is);
1549 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s",
1551 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason));
1552 //is->is_waiting_for_iscsid = false;
1553 //is->is_login_phase = true;
1554 //iscsi_session_reconnect(is);
1555 ISCSI_SESSION_UNLOCK(is);
1556 sx_sunlock(&sc->sc_lock);
1561 #ifdef ICL_KERNEL_PROXY
1563 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc,
1564 struct iscsi_daemon_connect *idc)
1566 struct iscsi_session *is;
1567 struct sockaddr *from_sa, *to_sa;
1570 sx_slock(&sc->sc_lock);
1571 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1572 if (is->is_id == idc->idc_session_id)
1576 sx_sunlock(&sc->sc_lock);
1579 sx_sunlock(&sc->sc_lock);
1581 if (idc->idc_from_addrlen > 0) {
1582 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen);
1584 ISCSI_SESSION_WARN(is,
1585 "getsockaddr failed with error %d", error);
1591 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen);
1593 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d",
1595 free(from_sa, M_SONAME);
1599 ISCSI_SESSION_LOCK(is);
1602 is->is_expcmdsn = 0;
1603 is->is_maxcmdsn = 0;
1604 is->is_waiting_for_iscsid = false;
1605 is->is_login_phase = true;
1607 ISCSI_SESSION_UNLOCK(is);
1609 error = icl_conn_connect(is->is_conn, idc->idc_domain,
1610 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa);
1611 free(from_sa, M_SONAME);
1612 free(to_sa, M_SONAME);
1615 * Digests are always disabled during login phase.
1617 is->is_conn->ic_header_crc32c = false;
1618 is->is_conn->ic_data_crc32c = false;
1624 iscsi_ioctl_daemon_send(struct iscsi_softc *sc,
1625 struct iscsi_daemon_send *ids)
1627 struct iscsi_session *is;
1633 sx_slock(&sc->sc_lock);
1634 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1635 if (is->is_id == ids->ids_session_id)
1639 sx_sunlock(&sc->sc_lock);
1642 sx_sunlock(&sc->sc_lock);
1644 if (is->is_login_phase == false)
1647 if (is->is_terminating || is->is_reconnecting)
1650 datalen = ids->ids_data_segment_len;
1651 if (datalen > is->is_conn->ic_max_send_data_segment_length)
1654 data = malloc(datalen, M_ISCSI, M_WAITOK);
1655 error = copyin(ids->ids_data_segment, data, datalen);
1657 free(data, M_ISCSI);
1662 ip = icl_pdu_new(is->is_conn, M_WAITOK);
1663 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs));
1665 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1666 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed"));
1667 free(data, M_ISCSI);
1669 iscsi_pdu_queue(ip);
1675 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc,
1676 struct iscsi_daemon_receive *idr)
1678 struct iscsi_session *is;
1683 sx_slock(&sc->sc_lock);
1684 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1685 if (is->is_id == idr->idr_session_id)
1689 sx_sunlock(&sc->sc_lock);
1692 sx_sunlock(&sc->sc_lock);
1694 if (is->is_login_phase == false)
1697 ISCSI_SESSION_LOCK(is);
1698 while (is->is_login_pdu == NULL &&
1699 is->is_terminating == false &&
1700 is->is_reconnecting == false) {
1701 error = cv_wait_sig(&is->is_login_cv, &is->is_lock);
1703 ISCSI_SESSION_UNLOCK(is);
1707 if (is->is_terminating || is->is_reconnecting) {
1708 ISCSI_SESSION_UNLOCK(is);
1711 ip = is->is_login_pdu;
1712 is->is_login_pdu = NULL;
1713 ISCSI_SESSION_UNLOCK(is);
1715 if (ip->ip_data_len > idr->idr_data_segment_len) {
1720 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs));
1721 if (ip->ip_data_len > 0) {
1722 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK);
1723 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1724 copyout(data, idr->idr_data_segment, ip->ip_data_len);
1725 free(data, M_ISCSI);
1732 #endif /* ICL_KERNEL_PROXY */
1735 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc)
1738 * Just make sure all the fields are null-terminated.
1740 * XXX: This is not particularly secure. We should
1741 * create our own conf and then copy in relevant
1744 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0';
1745 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0';
1746 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0';
1747 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0';
1748 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0';
1749 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0';
1750 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0';
1751 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0';
1752 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0';
1756 iscsi_valid_session_conf(const struct iscsi_session_conf *isc)
1759 if (isc->isc_initiator[0] == '\0') {
1760 ISCSI_DEBUG("empty isc_initiator");
1764 if (isc->isc_target_addr[0] == '\0') {
1765 ISCSI_DEBUG("empty isc_target_addr");
1769 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) {
1770 ISCSI_DEBUG("non-empty isc_target for discovery session");
1774 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) {
1775 ISCSI_DEBUG("empty isc_target for non-discovery session");
1783 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa)
1785 struct iscsi_session *is;
1786 const struct iscsi_session *is2;
1789 iscsi_sanitize_session_conf(&isa->isa_conf);
1790 if (iscsi_valid_session_conf(&isa->isa_conf) == false)
1793 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK);
1794 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf));
1796 sx_xlock(&sc->sc_lock);
1799 * Prevent duplicates.
1801 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
1802 if (!!is->is_conf.isc_discovery !=
1803 !!is2->is_conf.isc_discovery)
1806 if (strcmp(is->is_conf.isc_target_addr,
1807 is2->is_conf.isc_target_addr) != 0)
1810 if (is->is_conf.isc_discovery == 0 &&
1811 strcmp(is->is_conf.isc_target,
1812 is2->is_conf.isc_target) != 0)
1815 sx_xunlock(&sc->sc_lock);
1820 is->is_conn = icl_new_conn(is->is_conf.isc_offload,
1821 is->is_conf.isc_iser, "iscsi", &is->is_lock);
1822 if (is->is_conn == NULL) {
1823 sx_xunlock(&sc->sc_lock);
1827 is->is_conn->ic_receive = iscsi_receive_callback;
1828 is->is_conn->ic_error = iscsi_error_callback;
1829 is->is_conn->ic_prv0 = is;
1830 TAILQ_INIT(&is->is_outstanding);
1831 STAILQ_INIT(&is->is_postponed);
1832 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF);
1833 cv_init(&is->is_maintenance_cv, "iscsi_mt");
1834 #ifdef ICL_KERNEL_PROXY
1835 cv_init(&is->is_login_cv, "iscsi_login");
1839 * Set some default values, from RFC 3720, section 12.
1841 * These values are updated by the handoff IOCTL, but are
1842 * needed prior to the handoff to support sending the ISER
1845 is->is_conn->ic_max_recv_data_segment_length = 8192;
1846 is->is_conn->ic_max_send_data_segment_length = 8192;
1847 is->is_max_burst_length = 262144;
1848 is->is_first_burst_length = 65536;
1851 sc->sc_last_session_id++;
1852 is->is_id = sc->sc_last_session_id;
1853 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */
1854 arc4rand(&is->is_isid[1], 5, 0);
1856 callout_init(&is->is_callout, 1);
1858 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt");
1860 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error);
1861 sx_xunlock(&sc->sc_lock);
1865 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is);
1866 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next);
1868 ISCSI_SESSION_LOCK(is);
1870 * Don't notify iscsid(8) if the session is disabled and it's not
1871 * a discovery session,
1873 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
1874 ISCSI_SESSION_UNLOCK(is);
1875 sx_xunlock(&sc->sc_lock);
1879 is->is_waiting_for_iscsid = true;
1880 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
1881 ISCSI_SESSION_UNLOCK(is);
1882 cv_signal(&sc->sc_cv);
1883 sx_xunlock(&sc->sc_lock);
1888 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1,
1889 unsigned int id2, const struct iscsi_session_conf *c2)
1892 if (id2 != 0 && id2 != id1)
1894 if (c2->isc_target[0] != '\0' &&
1895 strcmp(c1->isc_target, c2->isc_target) != 0)
1897 if (c2->isc_target_addr[0] != '\0' &&
1898 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0)
1904 iscsi_ioctl_session_remove(struct iscsi_softc *sc,
1905 struct iscsi_session_remove *isr)
1907 struct iscsi_session *is, *tmp;
1910 iscsi_sanitize_session_conf(&isr->isr_conf);
1912 sx_xlock(&sc->sc_lock);
1913 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) {
1914 ISCSI_SESSION_LOCK(is);
1915 if (iscsi_session_conf_matches(is->is_id, &is->is_conf,
1916 isr->isr_session_id, &isr->isr_conf)) {
1918 iscsi_session_logout(is);
1919 iscsi_session_terminate(is);
1921 ISCSI_SESSION_UNLOCK(is);
1923 sx_xunlock(&sc->sc_lock);
1932 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl)
1936 struct iscsi_session *is;
1937 struct iscsi_session_state iss;
1939 sx_slock(&sc->sc_lock);
1940 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1941 if (i >= isl->isl_nentries) {
1942 sx_sunlock(&sc->sc_lock);
1945 memset(&iss, 0, sizeof(iss));
1946 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf));
1947 iss.iss_id = is->is_id;
1948 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias));
1949 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason));
1950 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload));
1952 if (is->is_conn->ic_header_crc32c)
1953 iss.iss_header_digest = ISCSI_DIGEST_CRC32C;
1955 iss.iss_header_digest = ISCSI_DIGEST_NONE;
1957 if (is->is_conn->ic_data_crc32c)
1958 iss.iss_data_digest = ISCSI_DIGEST_CRC32C;
1960 iss.iss_data_digest = ISCSI_DIGEST_NONE;
1962 iss.iss_max_send_data_segment_length =
1963 is->is_conn->ic_max_send_data_segment_length;
1964 iss.iss_max_recv_data_segment_length =
1965 is->is_conn->ic_max_recv_data_segment_length;
1966 iss.iss_max_burst_length = is->is_max_burst_length;
1967 iss.iss_first_burst_length = is->is_first_burst_length;
1968 iss.iss_immediate_data = is->is_immediate_data;
1969 iss.iss_connected = is->is_connected;
1971 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss));
1973 sx_sunlock(&sc->sc_lock);
1978 sx_sunlock(&sc->sc_lock);
1980 isl->isl_nentries = i;
1986 iscsi_ioctl_session_modify(struct iscsi_softc *sc,
1987 struct iscsi_session_modify *ism)
1989 struct iscsi_session *is;
1990 const struct iscsi_session *is2;
1992 iscsi_sanitize_session_conf(&ism->ism_conf);
1993 if (iscsi_valid_session_conf(&ism->ism_conf) == false)
1996 sx_xlock(&sc->sc_lock);
1997 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1998 ISCSI_SESSION_LOCK(is);
1999 if (is->is_id == ism->ism_session_id) {
2000 /* Note that the session remains locked. */
2003 ISCSI_SESSION_UNLOCK(is);
2006 sx_xunlock(&sc->sc_lock);
2011 * Prevent duplicates.
2013 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
2017 if (!!ism->ism_conf.isc_discovery !=
2018 !!is2->is_conf.isc_discovery)
2021 if (strcmp(ism->ism_conf.isc_target_addr,
2022 is2->is_conf.isc_target_addr) != 0)
2025 if (ism->ism_conf.isc_discovery == 0 &&
2026 strcmp(ism->ism_conf.isc_target,
2027 is2->is_conf.isc_target) != 0)
2030 ISCSI_SESSION_UNLOCK(is);
2031 sx_xunlock(&sc->sc_lock);
2035 sx_xunlock(&sc->sc_lock);
2037 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf));
2038 ISCSI_SESSION_UNLOCK(is);
2040 iscsi_session_reconnect(is);
2046 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
2049 struct iscsi_softc *sc;
2055 return (iscsi_ioctl_daemon_wait(sc,
2056 (struct iscsi_daemon_request *)arg));
2058 return (iscsi_ioctl_daemon_handoff(sc,
2059 (struct iscsi_daemon_handoff *)arg));
2061 return (iscsi_ioctl_daemon_fail(sc,
2062 (struct iscsi_daemon_fail *)arg));
2063 #ifdef ICL_KERNEL_PROXY
2065 return (iscsi_ioctl_daemon_connect(sc,
2066 (struct iscsi_daemon_connect *)arg));
2068 return (iscsi_ioctl_daemon_send(sc,
2069 (struct iscsi_daemon_send *)arg));
2071 return (iscsi_ioctl_daemon_receive(sc,
2072 (struct iscsi_daemon_receive *)arg));
2073 #endif /* ICL_KERNEL_PROXY */
2075 return (iscsi_ioctl_session_add(sc,
2076 (struct iscsi_session_add *)arg));
2078 return (iscsi_ioctl_session_remove(sc,
2079 (struct iscsi_session_remove *)arg));
2081 return (iscsi_ioctl_session_list(sc,
2082 (struct iscsi_session_list *)arg));
2084 return (iscsi_ioctl_session_modify(sc,
2085 (struct iscsi_session_modify *)arg));
2091 static struct iscsi_outstanding *
2092 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag)
2094 struct iscsi_outstanding *io;
2096 ISCSI_SESSION_LOCK_ASSERT(is);
2098 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2099 if (io->io_initiator_task_tag == initiator_task_tag)
2105 static struct iscsi_outstanding *
2106 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb)
2108 struct iscsi_outstanding *io;
2110 ISCSI_SESSION_LOCK_ASSERT(is);
2112 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2113 if (io->io_ccb == ccb)
2119 static struct iscsi_outstanding *
2120 iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request,
2121 union ccb *ccb, uint32_t *initiator_task_tagp)
2123 struct iscsi_outstanding *io;
2126 ISCSI_SESSION_LOCK_ASSERT(is);
2128 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO);
2130 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes",
2135 error = icl_conn_task_setup(is->is_conn, request, &ccb->csio,
2136 initiator_task_tagp, &io->io_icl_prv);
2138 ISCSI_SESSION_WARN(is,
2139 "icl_conn_task_setup() failed with error %d", error);
2140 uma_zfree(iscsi_outstanding_zone, io);
2144 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL,
2145 ("initiator_task_tag 0x%x already added", *initiator_task_tagp));
2147 io->io_initiator_task_tag = *initiator_task_tagp;
2149 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next);
2154 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io)
2157 ISCSI_SESSION_LOCK_ASSERT(is);
2159 icl_conn_task_done(is->is_conn, io->io_icl_prv);
2160 TAILQ_REMOVE(&is->is_outstanding, io, io_next);
2161 uma_zfree(iscsi_outstanding_zone, io);
2165 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb)
2167 struct icl_pdu *request;
2168 struct iscsi_bhs_task_management_request *bhstmr;
2169 struct ccb_abort *cab = &ccb->cab;
2170 struct iscsi_outstanding *io, *aio;
2171 uint32_t initiator_task_tag;
2173 ISCSI_SESSION_LOCK_ASSERT(is);
2176 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2178 if (is->is_login_phase) {
2179 ccb->ccb_h.status = CAM_REQ_ABORTED;
2185 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb);
2187 ccb->ccb_h.status = CAM_REQ_CMP;
2192 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2193 if (request == NULL) {
2194 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2199 initiator_task_tag = is->is_initiator_task_tag++;
2200 if (initiator_task_tag == 0xffffffff)
2201 initiator_task_tag = is->is_initiator_task_tag++;
2203 io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag);
2205 icl_pdu_free(request);
2206 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2210 io->io_referenced_task_tag = aio->io_initiator_task_tag;
2212 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2213 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST;
2214 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK;
2215 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2216 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag;
2217 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag;
2219 iscsi_pdu_queue_locked(request);
2223 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb)
2225 struct icl_pdu *request;
2226 struct iscsi_bhs_scsi_command *bhssc;
2227 struct ccb_scsiio *csio;
2228 struct iscsi_outstanding *io;
2230 uint32_t initiator_task_tag;
2233 ISCSI_SESSION_LOCK_ASSERT(is);
2236 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2238 if (is->is_login_phase) {
2239 ISCSI_SESSION_DEBUG(is, "called during login phase");
2240 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2241 xpt_freeze_devq(ccb->ccb_h.path, 1);
2242 ISCSI_SESSION_DEBUG(is, "freezing devq");
2244 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN;
2250 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2251 if (request == NULL) {
2252 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2253 xpt_freeze_devq(ccb->ccb_h.path, 1);
2254 ISCSI_SESSION_DEBUG(is, "freezing devq");
2256 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2261 initiator_task_tag = is->is_initiator_task_tag++;
2262 if (initiator_task_tag == 0xffffffff)
2263 initiator_task_tag = is->is_initiator_task_tag++;
2265 io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag);
2267 icl_pdu_free(request);
2268 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2269 xpt_freeze_devq(ccb->ccb_h.path, 1);
2270 ISCSI_SESSION_DEBUG(is, "freezing devq");
2272 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2278 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2279 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND;
2280 bhssc->bhssc_flags |= BHSSC_FLAGS_F;
2281 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
2283 bhssc->bhssc_flags |= BHSSC_FLAGS_R;
2286 bhssc->bhssc_flags |= BHSSC_FLAGS_W;
2290 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2291 switch (csio->tag_action) {
2292 case MSG_HEAD_OF_Q_TAG:
2293 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ;
2295 case MSG_ORDERED_Q_TAG:
2296 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED;
2299 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA;
2301 case MSG_SIMPLE_Q_TAG:
2303 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE;
2307 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED;
2309 if (is->is_protocol_level >= 2) {
2310 bhssc->bhssc_pri = (csio->priority << BHSSC_PRI_SHIFT) &
2314 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2315 bhssc->bhssc_initiator_task_tag = initiator_task_tag;
2316 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len);
2317 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb),
2318 ("unsupported CDB size %zd", (size_t)csio->cdb_len));
2320 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2321 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len);
2323 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len);
2325 if (is->is_immediate_data &&
2326 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2327 len = csio->dxfer_len;
2328 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len);
2329 if (len > is->is_first_burst_length) {
2330 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len, is->is_first_burst_length);
2331 len = is->is_first_burst_length;
2333 if (len > is->is_conn->ic_max_send_data_segment_length) {
2334 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len,
2335 is->is_conn->ic_max_send_data_segment_length);
2336 len = is->is_conn->ic_max_send_data_segment_length;
2339 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT);
2341 iscsi_outstanding_remove(is, io);
2342 icl_pdu_free(request);
2343 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2344 xpt_freeze_devq(ccb->ccb_h.path, 1);
2345 ISCSI_SESSION_DEBUG(is, "freezing devq");
2347 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2352 iscsi_pdu_queue_locked(request);
2356 iscsi_action(struct cam_sim *sim, union ccb *ccb)
2358 struct iscsi_session *is;
2360 is = cam_sim_softc(sim);
2362 ISCSI_SESSION_LOCK_ASSERT(is);
2364 if (is->is_terminating ||
2365 (is->is_connected == false && fail_on_disconnection)) {
2366 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2372 * Make sure CAM doesn't sneak in a CCB just after freezing the queue.
2374 if (is->is_simq_frozen == true) {
2375 ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
2376 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
2377 /* Don't freeze the devq - the SIM queue is already frozen. */
2382 switch (ccb->ccb_h.func_code) {
2385 struct ccb_pathinq *cpi = &ccb->cpi;
2387 cpi->version_num = 1;
2388 cpi->hba_inquiry = PI_TAG_ABLE;
2389 cpi->target_sprt = 0;
2390 cpi->hba_misc = PIM_EXTLUNS;
2392 * XXX: It shouldn't ever be NULL; this could be turned
2393 * into a KASSERT eventually.
2395 if (is->is_conn == NULL)
2396 ISCSI_WARN("NULL conn");
2397 else if (is->is_conn->ic_unmapped)
2398 cpi->hba_misc |= PIM_UNMAPPED;
2399 cpi->hba_eng_cnt = 0;
2400 cpi->max_target = 0;
2402 * Note that the variable below is only relevant for targets
2403 * that don't claim compliance with anything above SPC2, which
2404 * means they don't support REPORT_LUNS.
2407 cpi->initiator_id = ~0;
2408 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2409 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
2410 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2411 cpi->unit_number = cam_sim_unit(sim);
2412 cpi->bus_id = cam_sim_bus(sim);
2413 cpi->base_transfer_speed = 150000; /* XXX */
2414 cpi->transport = XPORT_ISCSI;
2415 cpi->transport_version = 0;
2416 cpi->protocol = PROTO_SCSI;
2417 cpi->protocol_version = SCSI_REV_SPC3;
2418 cpi->maxio = maxphys;
2419 cpi->ccb_h.status = CAM_REQ_CMP;
2422 case XPT_GET_TRAN_SETTINGS:
2424 struct ccb_trans_settings *cts;
2425 struct ccb_trans_settings_scsi *scsi;
2428 scsi = &cts->proto_specific.scsi;
2430 cts->protocol = PROTO_SCSI;
2431 cts->protocol_version = SCSI_REV_SPC3;
2432 cts->transport = XPORT_ISCSI;
2433 cts->transport_version = 0;
2434 scsi->valid = CTS_SCSI_VALID_TQ;
2435 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2436 cts->ccb_h.status = CAM_REQ_CMP;
2439 case XPT_CALC_GEOMETRY:
2440 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2441 ccb->ccb_h.status = CAM_REQ_CMP;
2445 * XXX: What's the point?
2449 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io");
2450 ccb->ccb_h.status = CAM_REQ_CMP;
2454 iscsi_action_abort(is, ccb);
2457 iscsi_action_scsiio(is, ccb);
2461 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code);
2463 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2470 iscsi_terminate_sessions(struct iscsi_softc *sc)
2472 struct iscsi_session *is;
2474 sx_slock(&sc->sc_lock);
2475 TAILQ_FOREACH(is, &sc->sc_sessions, is_next)
2476 iscsi_session_terminate(is);
2477 while(!TAILQ_EMPTY(&sc->sc_sessions)) {
2478 ISCSI_DEBUG("waiting for sessions to terminate");
2479 cv_wait(&sc->sc_cv, &sc->sc_lock);
2481 ISCSI_DEBUG("all sessions terminated");
2482 sx_sunlock(&sc->sc_lock);
2486 iscsi_shutdown_pre(struct iscsi_softc *sc)
2488 struct iscsi_session *is;
2490 if (!fail_on_shutdown)
2494 * If we have any sessions waiting for reconnection, request
2495 * maintenance thread to fail them immediately instead of waiting
2496 * for reconnect timeout.
2498 * This prevents LUNs with mounted filesystems that are supported
2499 * by disconnected iSCSI sessions from hanging, however it will
2500 * fail all queued BIOs.
2502 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown");
2504 fail_on_disconnection = 1;
2506 sx_slock(&sc->sc_lock);
2507 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2508 ISCSI_SESSION_LOCK(is);
2509 if (!is->is_connected) {
2510 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early");
2511 iscsi_session_reconnect(is);
2513 ISCSI_SESSION_UNLOCK(is);
2515 sx_sunlock(&sc->sc_lock);
2519 iscsi_shutdown_post(struct iscsi_softc *sc)
2522 if (!KERNEL_PANICKED()) {
2523 ISCSI_DEBUG("removing all sessions due to shutdown");
2524 iscsi_terminate_sessions(sc);
2533 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK);
2534 sx_init(&sc->sc_lock, "iscsi");
2535 TAILQ_INIT(&sc->sc_sessions);
2536 cv_init(&sc->sc_cv, "iscsi_cv");
2538 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding",
2539 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL,
2542 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw,
2543 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
2545 ISCSI_WARN("failed to create device node, error %d", error);
2548 sc->sc_cdev->si_drv1 = sc;
2550 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2551 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST);
2553 * shutdown_post_sync needs to run after filesystem shutdown and before
2554 * CAM shutdown - otherwise when rebooting with an iSCSI session that is
2555 * disconnected but has outstanding requests, dashutdown() will hang on
2556 * cam_periph_runccb().
2558 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
2559 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1);
2568 /* Awaken any threads asleep in iscsi_ioctl(). */
2569 sx_xlock(&sc->sc_lock);
2570 sc->sc_unloading = true;
2571 cv_signal(&sc->sc_cv);
2572 sx_xunlock(&sc->sc_lock);
2574 if (sc->sc_cdev != NULL) {
2575 ISCSI_DEBUG("removing device node");
2576 destroy_dev(sc->sc_cdev);
2577 ISCSI_DEBUG("device node removed");
2580 if (sc->sc_shutdown_pre_eh != NULL)
2581 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh);
2582 if (sc->sc_shutdown_post_eh != NULL)
2583 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh);
2585 iscsi_terminate_sessions(sc);
2587 uma_zdestroy(iscsi_outstanding_zone);
2588 sx_destroy(&sc->sc_lock);
2589 cv_destroy(&sc->sc_cv);
2597 sx_slock(&sc->sc_lock);
2598 if (!TAILQ_EMPTY(&sc->sc_sessions)) {
2599 sx_sunlock(&sc->sc_lock);
2602 sx_sunlock(&sc->sc_lock);
2607 iscsi_modevent(module_t mod, int what, void *arg)
2613 error = iscsi_load();
2616 error = iscsi_unload();
2619 error = iscsi_quiesce();
2628 moduledata_t iscsi_data = {
2634 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2635 MODULE_DEPEND(iscsi, cam, 1, 1, 1);
2636 MODULE_DEPEND(iscsi, icl, 1, 1, 1);