2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/condvar.h>
39 #include <sys/endian.h>
40 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/module.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_xpt.h>
57 #include <cam/cam_debug.h>
58 #include <cam/cam_sim.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_periph.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
65 #include <dev/iscsi/icl.h>
66 #include <dev/iscsi/icl_wrappers.h>
67 #include <dev/iscsi/iscsi_ioctl.h>
68 #include <dev/iscsi/iscsi_proto.h>
69 #include <dev/iscsi/iscsi.h>
71 #ifdef ICL_KERNEL_PROXY
72 #include <sys/socketvar.h>
75 #ifdef ICL_KERNEL_PROXY
76 FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY");
80 * XXX: This is global so the iscsi_unload() can access it.
81 * Think about how to do this properly.
83 static struct iscsi_softc *sc;
85 SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
88 SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
89 &debug, 0, "Enable debug messages");
90 static int ping_timeout = 5;
91 SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout,
92 0, "Timeout for ping (NOP-Out) requests, in seconds");
93 static int iscsid_timeout = 60;
94 SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout,
95 0, "Time to wait for iscsid(8) to handle reconnection, in seconds");
96 static int login_timeout = 60;
97 SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout,
98 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds");
99 static int maxtags = 255;
100 SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags,
101 0, "Max number of IO requests queued");
102 static int fail_on_disconnection = 0;
103 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN,
104 &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure");
105 static int fail_on_shutdown = 1;
106 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_shutdown, CTLFLAG_RWTUN,
107 &fail_on_shutdown, 0, "Fail disconnected sessions on shutdown");
109 static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator");
110 static uma_zone_t iscsi_outstanding_zone;
112 #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0)
113 #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn))
115 #define ISCSI_DEBUG(X, ...) \
118 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
121 #define ISCSI_WARN(X, ...) \
124 printf("WARNING: %s: " X "\n", \
125 __func__, ## __VA_ARGS__); \
129 #define ISCSI_SESSION_DEBUG(S, X, ...) \
132 printf("%s: %s (%s): " X "\n", \
133 __func__, S->is_conf.isc_target_addr, \
134 S->is_conf.isc_target, ## __VA_ARGS__); \
138 #define ISCSI_SESSION_WARN(S, X, ...) \
141 printf("WARNING: %s (%s): " X "\n", \
142 S->is_conf.isc_target_addr, \
143 S->is_conf.isc_target, ## __VA_ARGS__); \
147 #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock)
148 #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock)
149 #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED)
150 #define ISCSI_SESSION_LOCK_ASSERT_NOT(X) mtx_assert(&X->is_lock, MA_NOTOWNED)
152 static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
153 int mode, struct thread *td);
155 static struct cdevsw iscsi_cdevsw = {
156 .d_version = D_VERSION,
157 .d_ioctl = iscsi_ioctl,
161 static void iscsi_pdu_queue_locked(struct icl_pdu *request);
162 static void iscsi_pdu_queue(struct icl_pdu *request);
163 static void iscsi_pdu_update_statsn(const struct icl_pdu *response);
164 static void iscsi_pdu_handle_nop_in(struct icl_pdu *response);
165 static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response);
166 static void iscsi_pdu_handle_task_response(struct icl_pdu *response);
167 static void iscsi_pdu_handle_data_in(struct icl_pdu *response);
168 static void iscsi_pdu_handle_logout_response(struct icl_pdu *response);
169 static void iscsi_pdu_handle_r2t(struct icl_pdu *response);
170 static void iscsi_pdu_handle_async_message(struct icl_pdu *response);
171 static void iscsi_pdu_handle_reject(struct icl_pdu *response);
172 static void iscsi_session_reconnect(struct iscsi_session *is);
173 static void iscsi_session_terminate(struct iscsi_session *is);
174 static void iscsi_action(struct cam_sim *sim, union ccb *ccb);
175 static void iscsi_poll(struct cam_sim *sim);
176 static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is,
177 uint32_t initiator_task_tag);
178 static struct iscsi_outstanding *iscsi_outstanding_add(struct iscsi_session *is,
179 struct icl_pdu *request, union ccb *ccb,
180 uint32_t *initiator_task_tagp);
181 static void iscsi_outstanding_remove(struct iscsi_session *is,
182 struct iscsi_outstanding *io);
185 iscsi_pdu_prepare(struct icl_pdu *request)
187 struct iscsi_session *is;
188 struct iscsi_bhs_scsi_command *bhssc;
190 is = PDU_SESSION(request);
192 ISCSI_SESSION_LOCK_ASSERT(is);
195 * We're only using fields common for all the request
196 * (initiator -> target) PDUs.
198 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
201 * Data-Out PDU does not contain CmdSN.
203 if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
204 if (ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn) &&
205 (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
207 * Current MaxCmdSN prevents us from sending any more
208 * SCSI Command PDUs to the target; postpone the PDU.
209 * It will get resent by either iscsi_pdu_queue(),
210 * or by maintenance thread.
213 ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %u, "
214 "ExpCmdSN %u, MaxCmdSN %u, opcode 0x%x",
215 is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn,
216 bhssc->bhssc_opcode);
220 bhssc->bhssc_cmdsn = htonl(is->is_cmdsn);
221 if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
224 bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1);
230 iscsi_session_send_postponed(struct iscsi_session *is)
232 struct icl_pdu *request;
235 ISCSI_SESSION_LOCK_ASSERT(is);
237 if (STAILQ_EMPTY(&is->is_postponed))
239 while ((request = STAILQ_FIRST(&is->is_postponed)) != NULL) {
240 postpone = iscsi_pdu_prepare(request);
243 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
244 icl_pdu_queue(request);
246 xpt_release_simq(is->is_sim, 1);
250 iscsi_pdu_queue_locked(struct icl_pdu *request)
252 struct iscsi_session *is;
255 is = PDU_SESSION(request);
256 ISCSI_SESSION_LOCK_ASSERT(is);
257 iscsi_session_send_postponed(is);
258 postpone = iscsi_pdu_prepare(request);
260 if (STAILQ_EMPTY(&is->is_postponed))
261 xpt_freeze_simq(is->is_sim, 1);
262 STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next);
265 icl_pdu_queue(request);
269 iscsi_pdu_queue(struct icl_pdu *request)
271 struct iscsi_session *is;
273 is = PDU_SESSION(request);
274 ISCSI_SESSION_LOCK(is);
275 iscsi_pdu_queue_locked(request);
276 ISCSI_SESSION_UNLOCK(is);
280 iscsi_session_logout(struct iscsi_session *is)
282 struct icl_pdu *request;
283 struct iscsi_bhs_logout_request *bhslr;
285 request = icl_pdu_new(is->is_conn, M_NOWAIT);
289 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
290 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST;
291 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
292 iscsi_pdu_queue_locked(request);
296 iscsi_session_terminate_task(struct iscsi_session *is,
297 struct iscsi_outstanding *io, cam_status status)
300 ISCSI_SESSION_LOCK_ASSERT(is);
302 if (io->io_ccb != NULL) {
303 io->io_ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
304 io->io_ccb->ccb_h.status |= status;
305 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
306 io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN;
307 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1);
308 ISCSI_SESSION_DEBUG(is, "freezing devq");
310 xpt_done(io->io_ccb);
312 iscsi_outstanding_remove(is, io);
316 iscsi_session_terminate_tasks(struct iscsi_session *is, cam_status status)
318 struct iscsi_outstanding *io, *tmp;
320 ISCSI_SESSION_LOCK_ASSERT(is);
322 TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) {
323 iscsi_session_terminate_task(is, io, status);
328 iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim)
332 ISCSI_SESSION_LOCK_ASSERT(is);
335 * Don't queue any new PDUs.
337 if (is->is_sim != NULL && is->is_simq_frozen == false) {
338 ISCSI_SESSION_DEBUG(is, "freezing");
339 xpt_freeze_simq(is->is_sim, 1);
340 is->is_simq_frozen = true;
344 * Remove postponed PDUs.
346 if (!STAILQ_EMPTY(&is->is_postponed))
347 xpt_release_simq(is->is_sim, 1);
348 while ((pdu = STAILQ_FIRST(&is->is_postponed)) != NULL) {
349 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
353 if (destroy_sim == false) {
355 * Terminate SCSI tasks, asking CAM to requeue them.
357 iscsi_session_terminate_tasks(is, CAM_REQUEUE_REQ);
361 iscsi_session_terminate_tasks(is, CAM_DEV_NOT_THERE);
363 if (is->is_sim == NULL)
366 ISCSI_SESSION_DEBUG(is, "deregistering SIM");
367 xpt_async(AC_LOST_DEVICE, is->is_path, NULL);
369 if (is->is_simq_frozen) {
370 is->is_simq_frozen = false;
371 xpt_release_simq(is->is_sim, 1);
374 xpt_free_path(is->is_path);
376 xpt_bus_deregister(cam_sim_path(is->is_sim));
377 cam_sim_free(is->is_sim, TRUE /*free_devq*/);
383 iscsi_maintenance_thread_reconnect(struct iscsi_session *is)
386 icl_conn_close(is->is_conn);
388 ISCSI_SESSION_LOCK(is);
390 is->is_connected = false;
391 is->is_reconnecting = false;
392 is->is_login_phase = false;
394 #ifdef ICL_KERNEL_PROXY
395 if (is->is_login_pdu != NULL) {
396 icl_pdu_free(is->is_login_pdu);
397 is->is_login_pdu = NULL;
399 cv_signal(&is->is_login_cv);
402 if (fail_on_disconnection) {
403 ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices");
404 iscsi_session_cleanup(is, true);
406 iscsi_session_cleanup(is, false);
409 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
410 ("destroying session with active tasks"));
411 KASSERT(STAILQ_EMPTY(&is->is_postponed),
412 ("destroying session with postponed PDUs"));
414 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
415 ISCSI_SESSION_UNLOCK(is);
420 * Request immediate reconnection from iscsid(8).
422 //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)");
423 is->is_waiting_for_iscsid = true;
424 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
426 ISCSI_SESSION_UNLOCK(is);
427 cv_signal(&is->is_softc->sc_cv);
431 iscsi_maintenance_thread_terminate(struct iscsi_session *is)
433 struct iscsi_softc *sc;
436 sx_xlock(&sc->sc_lock);
437 TAILQ_REMOVE(&sc->sc_sessions, is, is_next);
438 sx_xunlock(&sc->sc_lock);
440 icl_conn_close(is->is_conn);
441 callout_drain(&is->is_callout);
443 ISCSI_SESSION_LOCK(is);
445 KASSERT(is->is_terminating, ("is_terminating == false"));
447 #ifdef ICL_KERNEL_PROXY
448 if (is->is_login_pdu != NULL) {
449 icl_pdu_free(is->is_login_pdu);
450 is->is_login_pdu = NULL;
452 cv_signal(&is->is_login_cv);
455 iscsi_session_cleanup(is, true);
457 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
458 ("destroying session with active tasks"));
459 KASSERT(STAILQ_EMPTY(&is->is_postponed),
460 ("destroying session with postponed PDUs"));
462 ISCSI_SESSION_UNLOCK(is);
464 icl_conn_free(is->is_conn);
465 mtx_destroy(&is->is_lock);
466 cv_destroy(&is->is_maintenance_cv);
467 #ifdef ICL_KERNEL_PROXY
468 cv_destroy(&is->is_login_cv);
471 ISCSI_SESSION_DEBUG(is, "terminated");
475 * The iscsi_unload() routine might be waiting.
477 cv_signal(&sc->sc_cv);
481 iscsi_maintenance_thread(void *arg)
483 struct iscsi_session *is = arg;
485 ISCSI_SESSION_LOCK(is);
487 if (is->is_reconnecting == false &&
488 is->is_terminating == false &&
489 (STAILQ_EMPTY(&is->is_postponed) ||
490 ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn)))
491 cv_wait(&is->is_maintenance_cv, &is->is_lock);
493 /* Terminate supersedes reconnect. */
494 if (is->is_terminating) {
495 ISCSI_SESSION_UNLOCK(is);
496 iscsi_maintenance_thread_terminate(is);
501 if (is->is_reconnecting) {
502 ISCSI_SESSION_UNLOCK(is);
503 iscsi_maintenance_thread_reconnect(is);
504 ISCSI_SESSION_LOCK(is);
508 iscsi_session_send_postponed(is);
510 ISCSI_SESSION_UNLOCK(is);
514 iscsi_session_reconnect(struct iscsi_session *is)
518 * XXX: We can't use locking here, because
519 * it's being called from various contexts.
520 * Hope it doesn't break anything.
522 if (is->is_reconnecting)
525 is->is_reconnecting = true;
526 cv_signal(&is->is_maintenance_cv);
530 iscsi_session_terminate(struct iscsi_session *is)
533 if (is->is_terminating)
536 is->is_terminating = true;
539 iscsi_session_logout(is);
541 cv_signal(&is->is_maintenance_cv);
545 iscsi_callout(void *context)
547 struct icl_pdu *request;
548 struct iscsi_bhs_nop_out *bhsno;
549 struct iscsi_session *is;
550 bool reconnect_needed = false;
554 ISCSI_SESSION_LOCK(is);
555 if (is->is_terminating) {
556 ISCSI_SESSION_UNLOCK(is);
560 callout_schedule(&is->is_callout, 1 * hz);
562 if (is->is_conf.isc_enable == 0)
567 if (is->is_waiting_for_iscsid) {
568 if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) {
569 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) "
570 "for %d seconds; reconnecting",
572 reconnect_needed = true;
577 if (is->is_login_phase) {
578 if (login_timeout > 0 && is->is_timeout > login_timeout) {
579 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; "
580 "reconnecting", is->is_timeout);
581 reconnect_needed = true;
586 if (ping_timeout <= 0) {
588 * Pings are disabled. Don't send NOP-Out in this case.
589 * Reset the timeout, to avoid triggering reconnection,
590 * should the user decide to reenable them.
596 if (is->is_timeout >= ping_timeout) {
597 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; "
598 "reconnecting", ping_timeout);
599 reconnect_needed = true;
603 ISCSI_SESSION_UNLOCK(is);
606 * If the ping was reset less than one second ago - which means
607 * that we've received some PDU during the last second - assume
608 * the traffic flows correctly and don't bother sending a NOP-Out.
610 * (It's 2 - one for one second, and one for incrementing is_timeout
611 * earlier in this routine.)
613 if (is->is_timeout < 2)
616 request = icl_pdu_new(is->is_conn, M_NOWAIT);
617 if (request == NULL) {
618 ISCSI_SESSION_WARN(is, "failed to allocate PDU");
621 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
622 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
623 ISCSI_BHS_OPCODE_IMMEDIATE;
624 bhsno->bhsno_flags = 0x80;
625 bhsno->bhsno_target_transfer_tag = 0xffffffff;
626 iscsi_pdu_queue(request);
630 if (is->is_terminating) {
631 ISCSI_SESSION_UNLOCK(is);
635 ISCSI_SESSION_UNLOCK(is);
637 if (reconnect_needed)
638 iscsi_session_reconnect(is);
642 iscsi_pdu_update_statsn(const struct icl_pdu *response)
644 const struct iscsi_bhs_data_in *bhsdi;
645 struct iscsi_session *is;
646 uint32_t expcmdsn, maxcmdsn, statsn;
648 is = PDU_SESSION(response);
650 ISCSI_SESSION_LOCK_ASSERT(is);
653 * We're only using fields common for all the response
654 * (target -> initiator) PDUs.
656 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs;
658 * Ok, I lied. In case of Data-In, "The fields StatSN, Status,
659 * and Residual Count only have meaningful content if the S bit
660 * is set to 1", so we also need to check the bit specific for
663 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
664 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) {
665 statsn = ntohl(bhsdi->bhsdi_statsn);
666 if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) {
667 /* XXX: This is normal situation for MCS */
668 ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != "
669 "session ExpStatSN %u (or + 1); reconnecting",
670 bhsdi->bhsdi_opcode, statsn, is->is_statsn);
671 iscsi_session_reconnect(is);
673 if (ISCSI_SNGT(statsn, is->is_statsn))
674 is->is_statsn = statsn;
677 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn);
678 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn);
680 if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) {
681 ISCSI_SESSION_DEBUG(is,
682 "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring",
685 if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) {
686 is->is_maxcmdsn = maxcmdsn;
689 * Command window increased; kick the maintanance thread
690 * to send out postponed commands.
692 if (!STAILQ_EMPTY(&is->is_postponed))
693 cv_signal(&is->is_maintenance_cv);
694 } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) {
695 /* XXX: This is normal situation for MCS */
696 ISCSI_SESSION_DEBUG(is,
697 "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring",
698 maxcmdsn, is->is_maxcmdsn);
701 if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) {
702 is->is_expcmdsn = expcmdsn;
703 } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) {
704 /* XXX: This is normal situation for MCS */
705 ISCSI_SESSION_DEBUG(is,
706 "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring",
707 expcmdsn, is->is_expcmdsn);
712 * Every incoming PDU - not just NOP-In - resets the ping timer.
713 * The purpose of the timeout is to reset the connection when it stalls;
714 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
721 iscsi_receive_callback(struct icl_pdu *response)
723 struct iscsi_session *is;
725 is = PDU_SESSION(response);
727 ISCSI_SESSION_LOCK(is);
729 iscsi_pdu_update_statsn(response);
731 #ifdef ICL_KERNEL_PROXY
732 if (is->is_login_phase) {
733 if (is->is_login_pdu == NULL)
734 is->is_login_pdu = response;
736 icl_pdu_free(response);
737 ISCSI_SESSION_UNLOCK(is);
738 cv_signal(&is->is_login_cv);
744 * The handling routine is responsible for freeing the PDU
745 * when it's no longer needed.
747 switch (response->ip_bhs->bhs_opcode) {
748 case ISCSI_BHS_OPCODE_NOP_IN:
749 iscsi_pdu_handle_nop_in(response);
750 ISCSI_SESSION_UNLOCK(is);
752 case ISCSI_BHS_OPCODE_SCSI_RESPONSE:
753 iscsi_pdu_handle_scsi_response(response);
754 /* Session lock dropped inside. */
755 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
757 case ISCSI_BHS_OPCODE_TASK_RESPONSE:
758 iscsi_pdu_handle_task_response(response);
759 ISCSI_SESSION_UNLOCK(is);
761 case ISCSI_BHS_OPCODE_SCSI_DATA_IN:
762 iscsi_pdu_handle_data_in(response);
763 /* Session lock dropped inside. */
764 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
766 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE:
767 iscsi_pdu_handle_logout_response(response);
768 ISCSI_SESSION_UNLOCK(is);
770 case ISCSI_BHS_OPCODE_R2T:
771 iscsi_pdu_handle_r2t(response);
772 ISCSI_SESSION_UNLOCK(is);
774 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE:
775 iscsi_pdu_handle_async_message(response);
776 ISCSI_SESSION_UNLOCK(is);
778 case ISCSI_BHS_OPCODE_REJECT:
779 iscsi_pdu_handle_reject(response);
780 ISCSI_SESSION_UNLOCK(is);
783 ISCSI_SESSION_WARN(is, "received PDU with unsupported "
784 "opcode 0x%x; reconnecting",
785 response->ip_bhs->bhs_opcode);
786 iscsi_session_reconnect(is);
787 ISCSI_SESSION_UNLOCK(is);
788 icl_pdu_free(response);
793 iscsi_error_callback(struct icl_conn *ic)
795 struct iscsi_session *is;
797 is = CONN_SESSION(ic);
799 ISCSI_SESSION_WARN(is, "connection error; reconnecting");
800 iscsi_session_reconnect(is);
804 iscsi_pdu_handle_nop_in(struct icl_pdu *response)
806 struct iscsi_session *is;
807 struct iscsi_bhs_nop_out *bhsno;
808 struct iscsi_bhs_nop_in *bhsni;
809 struct icl_pdu *request;
814 is = PDU_SESSION(response);
815 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
817 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) {
819 * Nothing to do; iscsi_pdu_update_statsn() already
820 * zeroed the timeout.
822 icl_pdu_free(response);
826 datasize = icl_pdu_data_segment_length(response);
828 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO);
830 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
832 icl_pdu_free(response);
833 iscsi_session_reconnect(is);
836 icl_pdu_get_data(response, 0, data, datasize);
839 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
840 if (request == NULL) {
841 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
844 icl_pdu_free(response);
845 iscsi_session_reconnect(is);
848 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
849 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
850 ISCSI_BHS_OPCODE_IMMEDIATE;
851 bhsno->bhsno_flags = 0x80;
852 bhsno->bhsno_initiator_task_tag = 0xffffffff;
853 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag;
855 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT);
857 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
860 icl_pdu_free(request);
861 icl_pdu_free(response);
862 iscsi_session_reconnect(is);
868 icl_pdu_free(response);
869 iscsi_pdu_queue_locked(request);
873 iscsi_pdu_handle_scsi_response(struct icl_pdu *response)
875 struct iscsi_bhs_scsi_response *bhssr;
876 struct iscsi_outstanding *io;
877 struct iscsi_session *is;
879 struct ccb_scsiio *csio;
880 size_t data_segment_len, received;
884 is = PDU_SESSION(response);
886 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
887 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag);
888 if (io == NULL || io->io_ccb == NULL) {
889 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag);
890 icl_pdu_free(response);
891 iscsi_session_reconnect(is);
892 ISCSI_SESSION_UNLOCK(is);
899 * With iSER, after getting good response we can be sure
900 * that all the data has been successfully transferred.
902 if (is->is_conn->ic_iser) {
903 resid = ntohl(bhssr->bhssr_residual_count);
904 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) {
905 io->io_received = ccb->csio.dxfer_len - resid;
906 } else if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) {
907 ISCSI_SESSION_WARN(is, "overflow: target indicates %d", resid);
909 io->io_received = ccb->csio.dxfer_len;
913 received = io->io_received;
914 iscsi_outstanding_remove(is, io);
915 ISCSI_SESSION_UNLOCK(is);
917 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) {
918 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response);
919 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
920 xpt_freeze_devq(ccb->ccb_h.path, 1);
921 ISCSI_SESSION_DEBUG(is, "freezing devq");
923 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
924 } else if (bhssr->bhssr_status == 0) {
925 ccb->ccb_h.status = CAM_REQ_CMP;
927 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
928 xpt_freeze_devq(ccb->ccb_h.path, 1);
929 ISCSI_SESSION_DEBUG(is, "freezing devq");
931 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
932 ccb->csio.scsi_status = bhssr->bhssr_status;
936 data_segment_len = icl_pdu_data_segment_length(response);
937 if (data_segment_len > 0) {
938 if (data_segment_len < sizeof(sense_len)) {
939 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)",
941 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
942 xpt_freeze_devq(ccb->ccb_h.path, 1);
943 ISCSI_SESSION_DEBUG(is, "freezing devq");
945 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
948 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len));
949 sense_len = ntohs(sense_len);
951 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd",
952 sense_len, data_segment_len);
954 if (sizeof(sense_len) + sense_len > data_segment_len) {
955 ISCSI_SESSION_WARN(is, "truncated data segment "
956 "(%zd bytes, should be %zd)",
957 data_segment_len, sizeof(sense_len) + sense_len);
958 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
959 xpt_freeze_devq(ccb->ccb_h.path, 1);
960 ISCSI_SESSION_DEBUG(is, "freezing devq");
962 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
964 } else if (sizeof(sense_len) + sense_len < data_segment_len)
965 ISCSI_SESSION_WARN(is, "oversize data segment "
966 "(%zd bytes, should be %zd)",
967 data_segment_len, sizeof(sense_len) + sense_len);
968 if (sense_len > csio->sense_len) {
969 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d",
970 sense_len, csio->sense_len);
971 sense_len = csio->sense_len;
973 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len);
974 csio->sense_resid = csio->sense_len - sense_len;
975 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
979 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW)
980 csio->resid = ntohl(bhssr->bhssr_residual_count);
982 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
983 KASSERT(received <= csio->dxfer_len,
984 ("received > csio->dxfer_len"));
985 if (received < csio->dxfer_len) {
986 if (csio->resid != csio->dxfer_len - received) {
987 ISCSI_SESSION_WARN(is, "underflow mismatch: "
988 "target indicates %d, we calculated %zd",
989 csio->resid, csio->dxfer_len - received);
991 csio->resid = csio->dxfer_len - received;
996 icl_pdu_free(response);
1000 iscsi_pdu_handle_task_response(struct icl_pdu *response)
1002 struct iscsi_bhs_task_management_response *bhstmr;
1003 struct iscsi_outstanding *io, *aio;
1004 struct iscsi_session *is;
1006 is = PDU_SESSION(response);
1008 bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs;
1009 io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag);
1010 if (io == NULL || io->io_ccb != NULL) {
1011 ISCSI_SESSION_WARN(is, "bad itt 0x%x",
1012 bhstmr->bhstmr_initiator_task_tag);
1013 icl_pdu_free(response);
1014 iscsi_session_reconnect(is);
1018 if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) {
1019 ISCSI_SESSION_WARN(is, "task response 0x%x",
1020 bhstmr->bhstmr_response);
1022 aio = iscsi_outstanding_find(is, io->io_datasn);
1023 if (aio != NULL && aio->io_ccb != NULL)
1024 iscsi_session_terminate_task(is, aio, CAM_REQ_ABORTED);
1027 iscsi_outstanding_remove(is, io);
1028 icl_pdu_free(response);
1032 iscsi_pdu_handle_data_in(struct icl_pdu *response)
1034 struct iscsi_bhs_data_in *bhsdi;
1035 struct iscsi_outstanding *io;
1036 struct iscsi_session *is;
1038 struct ccb_scsiio *csio;
1039 size_t data_segment_len, received, oreceived;
1041 is = PDU_SESSION(response);
1042 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
1043 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag);
1044 if (io == NULL || io->io_ccb == NULL) {
1045 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag);
1046 icl_pdu_free(response);
1047 iscsi_session_reconnect(is);
1048 ISCSI_SESSION_UNLOCK(is);
1052 data_segment_len = icl_pdu_data_segment_length(response);
1053 if (data_segment_len == 0) {
1055 * "The sending of 0 length data segments should be avoided,
1056 * but initiators and targets MUST be able to properly receive
1057 * 0 length data segments."
1059 ISCSI_SESSION_UNLOCK(is);
1060 icl_pdu_free(response);
1065 * We need to track this for security reasons - without it, malicious target
1066 * could respond to SCSI READ without sending Data-In PDUs, which would result
1067 * in read operation on the initiator side returning random kernel data.
1069 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) {
1070 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd",
1071 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset));
1072 icl_pdu_free(response);
1073 iscsi_session_reconnect(is);
1074 ISCSI_SESSION_UNLOCK(is);
1081 if (io->io_received + data_segment_len > csio->dxfer_len) {
1082 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes "
1083 "at offset %zd, buffer is %d)",
1084 data_segment_len, io->io_received, csio->dxfer_len);
1085 icl_pdu_free(response);
1086 iscsi_session_reconnect(is);
1087 ISCSI_SESSION_UNLOCK(is);
1091 oreceived = io->io_received;
1092 io->io_received += data_segment_len;
1093 received = io->io_received;
1094 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0)
1095 iscsi_outstanding_remove(is, io);
1096 ISCSI_SESSION_UNLOCK(is);
1098 icl_pdu_get_data(response, 0, csio->data_ptr + oreceived, data_segment_len);
1101 * XXX: Check DataSN.
1104 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) {
1106 * Nothing more to do.
1108 icl_pdu_free(response);
1112 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status);
1113 if (bhsdi->bhsdi_status == 0) {
1114 ccb->ccb_h.status = CAM_REQ_CMP;
1116 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1117 xpt_freeze_devq(ccb->ccb_h.path, 1);
1118 ISCSI_SESSION_DEBUG(is, "freezing devq");
1120 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
1121 csio->scsi_status = bhsdi->bhsdi_status;
1124 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1125 KASSERT(received <= csio->dxfer_len,
1126 ("received > csio->dxfer_len"));
1127 if (received < csio->dxfer_len) {
1128 csio->resid = ntohl(bhsdi->bhsdi_residual_count);
1129 if (csio->resid != csio->dxfer_len - received) {
1130 ISCSI_SESSION_WARN(is, "underflow mismatch: "
1131 "target indicates %d, we calculated %zd",
1132 csio->resid, csio->dxfer_len - received);
1134 csio->resid = csio->dxfer_len - received;
1139 icl_pdu_free(response);
1143 iscsi_pdu_handle_logout_response(struct icl_pdu *response)
1146 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response");
1147 icl_pdu_free(response);
1151 iscsi_pdu_handle_r2t(struct icl_pdu *response)
1153 struct icl_pdu *request;
1154 struct iscsi_session *is;
1155 struct iscsi_bhs_r2t *bhsr2t;
1156 struct iscsi_bhs_data_out *bhsdo;
1157 struct iscsi_outstanding *io;
1158 struct ccb_scsiio *csio;
1159 size_t off, len, total_len;
1162 is = PDU_SESSION(response);
1164 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
1165 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag);
1166 if (io == NULL || io->io_ccb == NULL) {
1167 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting",
1168 bhsr2t->bhsr2t_initiator_task_tag);
1169 icl_pdu_free(response);
1170 iscsi_session_reconnect(is);
1174 csio = &io->io_ccb->csio;
1176 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) {
1177 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting");
1178 icl_pdu_free(response);
1179 iscsi_session_reconnect(is);
1184 * XXX: Verify R2TSN.
1189 off = ntohl(bhsr2t->bhsr2t_buffer_offset);
1190 if (off > csio->dxfer_len) {
1191 ISCSI_SESSION_WARN(is, "target requested invalid offset "
1192 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len);
1193 icl_pdu_free(response);
1194 iscsi_session_reconnect(is);
1198 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length);
1199 if (total_len == 0 || total_len > csio->dxfer_len) {
1200 ISCSI_SESSION_WARN(is, "target requested invalid length "
1201 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len);
1202 icl_pdu_free(response);
1203 iscsi_session_reconnect(is);
1207 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len);
1212 if (len > is->is_max_send_data_segment_length)
1213 len = is->is_max_send_data_segment_length;
1215 if (off + len > csio->dxfer_len) {
1216 ISCSI_SESSION_WARN(is, "target requested invalid "
1217 "length/offset %zd, buffer is %d; reconnecting",
1218 off + len, csio->dxfer_len);
1219 icl_pdu_free(response);
1220 iscsi_session_reconnect(is);
1224 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
1225 if (request == NULL) {
1226 icl_pdu_free(response);
1227 iscsi_session_reconnect(is);
1231 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
1232 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT;
1233 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun;
1234 bhsdo->bhsdo_initiator_task_tag =
1235 bhsr2t->bhsr2t_initiator_task_tag;
1236 bhsdo->bhsdo_target_transfer_tag =
1237 bhsr2t->bhsr2t_target_transfer_tag;
1238 bhsdo->bhsdo_datasn = htonl(io->io_datasn++);
1239 bhsdo->bhsdo_buffer_offset = htonl(off);
1240 error = icl_pdu_append_data(request, csio->data_ptr + off, len,
1243 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
1245 icl_pdu_free(request);
1246 icl_pdu_free(response);
1247 iscsi_session_reconnect(is);
1254 if (total_len == 0) {
1255 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F;
1256 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off);
1258 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off);
1261 iscsi_pdu_queue_locked(request);
1267 icl_pdu_free(response);
1271 iscsi_pdu_handle_async_message(struct icl_pdu *response)
1273 struct iscsi_bhs_asynchronous_message *bhsam;
1274 struct iscsi_session *is;
1276 is = PDU_SESSION(response);
1277 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1278 switch (bhsam->bhsam_async_event) {
1279 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT:
1280 ISCSI_SESSION_WARN(is, "target requests logout; removing session");
1281 iscsi_session_logout(is);
1282 iscsi_session_terminate(is);
1284 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION:
1285 ISCSI_SESSION_WARN(is, "target indicates it will drop the connection");
1287 case BHSAM_EVENT_TARGET_TERMINATES_SESSION:
1288 ISCSI_SESSION_WARN(is, "target indicates it will drop the session");
1292 * XXX: Technically, we're obligated to also handle
1293 * parameter renegotiation.
1295 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event);
1299 icl_pdu_free(response);
1303 iscsi_pdu_handle_reject(struct icl_pdu *response)
1305 struct iscsi_bhs_reject *bhsr;
1306 struct iscsi_session *is;
1308 is = PDU_SESSION(response);
1309 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs;
1310 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?",
1313 icl_pdu_free(response);
1317 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc,
1318 struct iscsi_daemon_request *request)
1320 struct iscsi_session *is;
1321 struct icl_drv_limits idl;
1324 sx_slock(&sc->sc_lock);
1326 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1327 ISCSI_SESSION_LOCK(is);
1328 if (is->is_conf.isc_enable == 0 &&
1329 is->is_conf.isc_discovery == 0) {
1330 ISCSI_SESSION_UNLOCK(is);
1333 if (is->is_waiting_for_iscsid)
1335 ISCSI_SESSION_UNLOCK(is);
1340 * No session requires attention from iscsid(8); wait.
1342 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
1344 sx_sunlock(&sc->sc_lock);
1350 is->is_waiting_for_iscsid = false;
1351 is->is_login_phase = true;
1352 is->is_reason[0] = '\0';
1353 ISCSI_SESSION_UNLOCK(is);
1355 request->idr_session_id = is->is_id;
1356 memcpy(&request->idr_isid, &is->is_isid,
1357 sizeof(request->idr_isid));
1358 request->idr_tsih = 0; /* New or reinstated session. */
1359 memcpy(&request->idr_conf, &is->is_conf,
1360 sizeof(request->idr_conf));
1362 error = icl_limits(is->is_conf.isc_offload,
1363 is->is_conf.isc_iser, &idl);
1365 ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" "
1366 "failed with error %d", is->is_conf.isc_offload,
1368 sx_sunlock(&sc->sc_lock);
1371 request->idr_limits.isl_max_recv_data_segment_length =
1372 idl.idl_max_recv_data_segment_length;
1373 request->idr_limits.isl_max_send_data_segment_length =
1374 idl.idl_max_send_data_segment_length;
1375 request->idr_limits.isl_max_burst_length =
1376 idl.idl_max_burst_length;
1377 request->idr_limits.isl_first_burst_length =
1378 idl.idl_first_burst_length;
1380 sx_sunlock(&sc->sc_lock);
1386 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc,
1387 struct iscsi_daemon_handoff *handoff)
1389 struct iscsi_session *is;
1390 struct icl_conn *ic;
1393 sx_slock(&sc->sc_lock);
1396 * Find the session to hand off socket to.
1398 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1399 if (is->is_id == handoff->idh_session_id)
1403 sx_sunlock(&sc->sc_lock);
1406 ISCSI_SESSION_LOCK(is);
1408 if (is->is_conf.isc_discovery || is->is_terminating) {
1409 ISCSI_SESSION_UNLOCK(is);
1410 sx_sunlock(&sc->sc_lock);
1413 if (is->is_connected) {
1415 * This might have happened because another iscsid(8)
1416 * instance handed off the connection in the meantime.
1419 ISCSI_SESSION_WARN(is, "handoff on already connected "
1421 ISCSI_SESSION_UNLOCK(is);
1422 sx_sunlock(&sc->sc_lock);
1426 strlcpy(is->is_target_alias, handoff->idh_target_alias,
1427 sizeof(is->is_target_alias));
1428 is->is_tsih = handoff->idh_tsih;
1429 is->is_statsn = handoff->idh_statsn;
1430 is->is_protocol_level = handoff->idh_protocol_level;
1431 is->is_initial_r2t = handoff->idh_initial_r2t;
1432 is->is_immediate_data = handoff->idh_immediate_data;
1434 is->is_max_recv_data_segment_length =
1435 handoff->idh_max_recv_data_segment_length;
1436 is->is_max_send_data_segment_length =
1437 handoff->idh_max_send_data_segment_length;
1438 is->is_max_burst_length = handoff->idh_max_burst_length;
1439 is->is_first_burst_length = handoff->idh_first_burst_length;
1441 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C)
1442 ic->ic_header_crc32c = true;
1444 ic->ic_header_crc32c = false;
1445 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C)
1446 ic->ic_data_crc32c = true;
1448 ic->ic_data_crc32c = false;
1449 ic->ic_maxtags = maxtags;
1452 is->is_expcmdsn = 0;
1453 is->is_maxcmdsn = 0;
1454 is->is_waiting_for_iscsid = false;
1455 is->is_login_phase = false;
1457 is->is_connected = true;
1458 is->is_reason[0] = '\0';
1460 ISCSI_SESSION_UNLOCK(is);
1463 * If we're going through the proxy, the idh_socket will be 0,
1464 * and the ICL module can simply ignore this call. It can also
1465 * use it to determine it's no longer in the Login phase.
1467 error = icl_conn_handoff(ic, handoff->idh_socket);
1469 sx_sunlock(&sc->sc_lock);
1470 iscsi_session_terminate(is);
1474 sx_sunlock(&sc->sc_lock);
1476 if (is->is_sim != NULL) {
1478 * When reconnecting, there already is SIM allocated for the session.
1480 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq"));
1481 ISCSI_SESSION_LOCK(is);
1482 ISCSI_SESSION_DEBUG(is, "releasing");
1483 is->is_simq_frozen = false;
1484 xpt_release_simq(is->is_sim, 1);
1485 ISCSI_SESSION_UNLOCK(is);
1488 ISCSI_SESSION_LOCK(is);
1489 is->is_devq = cam_simq_alloc(ic->ic_maxtags);
1490 if (is->is_devq == NULL) {
1491 ISCSI_SESSION_WARN(is, "failed to allocate simq");
1492 iscsi_session_terminate(is);
1496 is->is_sim = cam_sim_alloc(iscsi_action, iscsi_poll, "iscsi",
1497 is, is->is_id /* unit */, &is->is_lock,
1498 1, ic->ic_maxtags, is->is_devq);
1499 if (is->is_sim == NULL) {
1500 ISCSI_SESSION_UNLOCK(is);
1501 ISCSI_SESSION_WARN(is, "failed to allocate SIM");
1502 cam_simq_free(is->is_devq);
1503 iscsi_session_terminate(is);
1507 error = xpt_bus_register(is->is_sim, NULL, 0);
1509 ISCSI_SESSION_UNLOCK(is);
1510 ISCSI_SESSION_WARN(is, "failed to register bus");
1511 iscsi_session_terminate(is);
1515 error = xpt_create_path(&is->is_path, /*periph*/NULL,
1516 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD,
1518 if (error != CAM_REQ_CMP) {
1519 ISCSI_SESSION_UNLOCK(is);
1520 ISCSI_SESSION_WARN(is, "failed to create path");
1521 iscsi_session_terminate(is);
1524 ISCSI_SESSION_UNLOCK(is);
1531 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc,
1532 struct iscsi_daemon_fail *fail)
1534 struct iscsi_session *is;
1536 sx_slock(&sc->sc_lock);
1538 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1539 if (is->is_id == fail->idf_session_id)
1543 sx_sunlock(&sc->sc_lock);
1546 ISCSI_SESSION_LOCK(is);
1547 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s",
1549 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason));
1550 //is->is_waiting_for_iscsid = false;
1551 //is->is_login_phase = true;
1552 //iscsi_session_reconnect(is);
1553 ISCSI_SESSION_UNLOCK(is);
1554 sx_sunlock(&sc->sc_lock);
1559 #ifdef ICL_KERNEL_PROXY
1561 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc,
1562 struct iscsi_daemon_connect *idc)
1564 struct iscsi_session *is;
1565 struct sockaddr *from_sa, *to_sa;
1568 sx_slock(&sc->sc_lock);
1569 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1570 if (is->is_id == idc->idc_session_id)
1574 sx_sunlock(&sc->sc_lock);
1577 sx_sunlock(&sc->sc_lock);
1579 if (idc->idc_from_addrlen > 0) {
1580 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen);
1582 ISCSI_SESSION_WARN(is,
1583 "getsockaddr failed with error %d", error);
1589 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen);
1591 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d",
1593 free(from_sa, M_SONAME);
1597 ISCSI_SESSION_LOCK(is);
1600 is->is_expcmdsn = 0;
1601 is->is_maxcmdsn = 0;
1602 is->is_waiting_for_iscsid = false;
1603 is->is_login_phase = true;
1605 ISCSI_SESSION_UNLOCK(is);
1607 error = icl_conn_connect(is->is_conn, idc->idc_domain,
1608 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa);
1609 free(from_sa, M_SONAME);
1610 free(to_sa, M_SONAME);
1613 * Digests are always disabled during login phase.
1615 is->is_conn->ic_header_crc32c = false;
1616 is->is_conn->ic_data_crc32c = false;
1622 iscsi_ioctl_daemon_send(struct iscsi_softc *sc,
1623 struct iscsi_daemon_send *ids)
1625 struct iscsi_session *is;
1631 sx_slock(&sc->sc_lock);
1632 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1633 if (is->is_id == ids->ids_session_id)
1637 sx_sunlock(&sc->sc_lock);
1640 sx_sunlock(&sc->sc_lock);
1642 if (is->is_login_phase == false)
1645 if (is->is_terminating || is->is_reconnecting)
1648 datalen = ids->ids_data_segment_len;
1649 if (datalen > is->is_max_send_data_segment_length)
1652 data = malloc(datalen, M_ISCSI, M_WAITOK);
1653 error = copyin(ids->ids_data_segment, data, datalen);
1655 free(data, M_ISCSI);
1660 ip = icl_pdu_new(is->is_conn, M_WAITOK);
1661 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs));
1663 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1664 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed"));
1665 free(data, M_ISCSI);
1667 iscsi_pdu_queue(ip);
1673 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc,
1674 struct iscsi_daemon_receive *idr)
1676 struct iscsi_session *is;
1681 sx_slock(&sc->sc_lock);
1682 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1683 if (is->is_id == idr->idr_session_id)
1687 sx_sunlock(&sc->sc_lock);
1690 sx_sunlock(&sc->sc_lock);
1692 if (is->is_login_phase == false)
1695 ISCSI_SESSION_LOCK(is);
1696 while (is->is_login_pdu == NULL &&
1697 is->is_terminating == false &&
1698 is->is_reconnecting == false) {
1699 error = cv_wait_sig(&is->is_login_cv, &is->is_lock);
1701 ISCSI_SESSION_UNLOCK(is);
1705 if (is->is_terminating || is->is_reconnecting) {
1706 ISCSI_SESSION_UNLOCK(is);
1709 ip = is->is_login_pdu;
1710 is->is_login_pdu = NULL;
1711 ISCSI_SESSION_UNLOCK(is);
1713 if (ip->ip_data_len > idr->idr_data_segment_len) {
1718 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs));
1719 if (ip->ip_data_len > 0) {
1720 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK);
1721 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1722 copyout(data, idr->idr_data_segment, ip->ip_data_len);
1723 free(data, M_ISCSI);
1730 #endif /* ICL_KERNEL_PROXY */
1733 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc)
1736 * Just make sure all the fields are null-terminated.
1738 * XXX: This is not particularly secure. We should
1739 * create our own conf and then copy in relevant
1742 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0';
1743 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0';
1744 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0';
1745 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0';
1746 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0';
1747 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0';
1748 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0';
1749 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0';
1750 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0';
1754 iscsi_valid_session_conf(const struct iscsi_session_conf *isc)
1757 if (isc->isc_initiator[0] == '\0') {
1758 ISCSI_DEBUG("empty isc_initiator");
1762 if (isc->isc_target_addr[0] == '\0') {
1763 ISCSI_DEBUG("empty isc_target_addr");
1767 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) {
1768 ISCSI_DEBUG("non-empty isc_target for discovery session");
1772 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) {
1773 ISCSI_DEBUG("empty isc_target for non-discovery session");
1781 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa)
1783 struct iscsi_session *is;
1784 const struct iscsi_session *is2;
1787 iscsi_sanitize_session_conf(&isa->isa_conf);
1788 if (iscsi_valid_session_conf(&isa->isa_conf) == false)
1791 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK);
1792 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf));
1795 * Set some default values, from RFC 3720, section 12.
1797 * These values are updated by the handoff IOCTL, but are
1798 * needed prior to the handoff to support sending the ISER
1801 is->is_max_recv_data_segment_length = 8192;
1802 is->is_max_send_data_segment_length = 8192;
1803 is->is_max_burst_length = 262144;
1804 is->is_first_burst_length = 65536;
1806 sx_xlock(&sc->sc_lock);
1809 * Prevent duplicates.
1811 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
1812 if (!!is->is_conf.isc_discovery !=
1813 !!is2->is_conf.isc_discovery)
1816 if (strcmp(is->is_conf.isc_target_addr,
1817 is2->is_conf.isc_target_addr) != 0)
1820 if (is->is_conf.isc_discovery == 0 &&
1821 strcmp(is->is_conf.isc_target,
1822 is2->is_conf.isc_target) != 0)
1825 sx_xunlock(&sc->sc_lock);
1830 is->is_conn = icl_new_conn(is->is_conf.isc_offload,
1831 is->is_conf.isc_iser, "iscsi", &is->is_lock);
1832 if (is->is_conn == NULL) {
1833 sx_xunlock(&sc->sc_lock);
1837 is->is_conn->ic_receive = iscsi_receive_callback;
1838 is->is_conn->ic_error = iscsi_error_callback;
1839 is->is_conn->ic_prv0 = is;
1840 TAILQ_INIT(&is->is_outstanding);
1841 STAILQ_INIT(&is->is_postponed);
1842 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF);
1843 cv_init(&is->is_maintenance_cv, "iscsi_mt");
1844 #ifdef ICL_KERNEL_PROXY
1845 cv_init(&is->is_login_cv, "iscsi_login");
1849 sc->sc_last_session_id++;
1850 is->is_id = sc->sc_last_session_id;
1851 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */
1852 arc4rand(&is->is_isid[1], 5, 0);
1854 callout_init(&is->is_callout, 1);
1856 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt");
1858 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error);
1859 sx_xunlock(&sc->sc_lock);
1863 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is);
1864 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next);
1866 ISCSI_SESSION_LOCK(is);
1868 * Don't notify iscsid(8) if the session is disabled and it's not
1869 * a discovery session,
1871 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
1872 ISCSI_SESSION_UNLOCK(is);
1873 sx_xunlock(&sc->sc_lock);
1877 is->is_waiting_for_iscsid = true;
1878 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
1879 ISCSI_SESSION_UNLOCK(is);
1880 cv_signal(&sc->sc_cv);
1881 sx_xunlock(&sc->sc_lock);
1886 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1,
1887 unsigned int id2, const struct iscsi_session_conf *c2)
1890 if (id2 != 0 && id2 != id1)
1892 if (c2->isc_target[0] != '\0' &&
1893 strcmp(c1->isc_target, c2->isc_target) != 0)
1895 if (c2->isc_target_addr[0] != '\0' &&
1896 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0)
1902 iscsi_ioctl_session_remove(struct iscsi_softc *sc,
1903 struct iscsi_session_remove *isr)
1905 struct iscsi_session *is, *tmp;
1908 iscsi_sanitize_session_conf(&isr->isr_conf);
1910 sx_xlock(&sc->sc_lock);
1911 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) {
1912 ISCSI_SESSION_LOCK(is);
1913 if (iscsi_session_conf_matches(is->is_id, &is->is_conf,
1914 isr->isr_session_id, &isr->isr_conf)) {
1916 iscsi_session_logout(is);
1917 iscsi_session_terminate(is);
1919 ISCSI_SESSION_UNLOCK(is);
1921 sx_xunlock(&sc->sc_lock);
1930 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl)
1934 struct iscsi_session *is;
1935 struct iscsi_session_state iss;
1937 sx_slock(&sc->sc_lock);
1938 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1939 if (i >= isl->isl_nentries) {
1940 sx_sunlock(&sc->sc_lock);
1943 memset(&iss, 0, sizeof(iss));
1944 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf));
1945 iss.iss_id = is->is_id;
1946 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias));
1947 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason));
1948 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload));
1950 if (is->is_conn->ic_header_crc32c)
1951 iss.iss_header_digest = ISCSI_DIGEST_CRC32C;
1953 iss.iss_header_digest = ISCSI_DIGEST_NONE;
1955 if (is->is_conn->ic_data_crc32c)
1956 iss.iss_data_digest = ISCSI_DIGEST_CRC32C;
1958 iss.iss_data_digest = ISCSI_DIGEST_NONE;
1960 iss.iss_max_send_data_segment_length =
1961 is->is_max_send_data_segment_length;
1962 iss.iss_max_recv_data_segment_length =
1963 is->is_max_recv_data_segment_length;
1964 iss.iss_max_burst_length = is->is_max_burst_length;
1965 iss.iss_first_burst_length = is->is_first_burst_length;
1966 iss.iss_immediate_data = is->is_immediate_data;
1967 iss.iss_connected = is->is_connected;
1969 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss));
1971 sx_sunlock(&sc->sc_lock);
1976 sx_sunlock(&sc->sc_lock);
1978 isl->isl_nentries = i;
1984 iscsi_ioctl_session_modify(struct iscsi_softc *sc,
1985 struct iscsi_session_modify *ism)
1987 struct iscsi_session *is;
1988 const struct iscsi_session *is2;
1990 iscsi_sanitize_session_conf(&ism->ism_conf);
1991 if (iscsi_valid_session_conf(&ism->ism_conf) == false)
1994 sx_xlock(&sc->sc_lock);
1995 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1996 ISCSI_SESSION_LOCK(is);
1997 if (is->is_id == ism->ism_session_id) {
1998 /* Note that the session remains locked. */
2001 ISCSI_SESSION_UNLOCK(is);
2004 sx_xunlock(&sc->sc_lock);
2009 * Prevent duplicates.
2011 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
2015 if (!!ism->ism_conf.isc_discovery !=
2016 !!is2->is_conf.isc_discovery)
2019 if (strcmp(ism->ism_conf.isc_target_addr,
2020 is2->is_conf.isc_target_addr) != 0)
2023 if (ism->ism_conf.isc_discovery == 0 &&
2024 strcmp(ism->ism_conf.isc_target,
2025 is2->is_conf.isc_target) != 0)
2028 ISCSI_SESSION_UNLOCK(is);
2029 sx_xunlock(&sc->sc_lock);
2033 sx_xunlock(&sc->sc_lock);
2035 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf));
2036 ISCSI_SESSION_UNLOCK(is);
2038 iscsi_session_reconnect(is);
2044 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
2047 struct iscsi_softc *sc;
2053 return (iscsi_ioctl_daemon_wait(sc,
2054 (struct iscsi_daemon_request *)arg));
2056 return (iscsi_ioctl_daemon_handoff(sc,
2057 (struct iscsi_daemon_handoff *)arg));
2059 return (iscsi_ioctl_daemon_fail(sc,
2060 (struct iscsi_daemon_fail *)arg));
2061 #ifdef ICL_KERNEL_PROXY
2063 return (iscsi_ioctl_daemon_connect(sc,
2064 (struct iscsi_daemon_connect *)arg));
2066 return (iscsi_ioctl_daemon_send(sc,
2067 (struct iscsi_daemon_send *)arg));
2069 return (iscsi_ioctl_daemon_receive(sc,
2070 (struct iscsi_daemon_receive *)arg));
2071 #endif /* ICL_KERNEL_PROXY */
2073 return (iscsi_ioctl_session_add(sc,
2074 (struct iscsi_session_add *)arg));
2076 return (iscsi_ioctl_session_remove(sc,
2077 (struct iscsi_session_remove *)arg));
2079 return (iscsi_ioctl_session_list(sc,
2080 (struct iscsi_session_list *)arg));
2082 return (iscsi_ioctl_session_modify(sc,
2083 (struct iscsi_session_modify *)arg));
2089 static struct iscsi_outstanding *
2090 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag)
2092 struct iscsi_outstanding *io;
2094 ISCSI_SESSION_LOCK_ASSERT(is);
2096 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2097 if (io->io_initiator_task_tag == initiator_task_tag)
2103 static struct iscsi_outstanding *
2104 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb)
2106 struct iscsi_outstanding *io;
2108 ISCSI_SESSION_LOCK_ASSERT(is);
2110 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2111 if (io->io_ccb == ccb)
2117 static struct iscsi_outstanding *
2118 iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request,
2119 union ccb *ccb, uint32_t *initiator_task_tagp)
2121 struct iscsi_outstanding *io;
2124 ISCSI_SESSION_LOCK_ASSERT(is);
2126 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO);
2128 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes",
2133 error = icl_conn_task_setup(is->is_conn, request, &ccb->csio,
2134 initiator_task_tagp, &io->io_icl_prv);
2136 ISCSI_SESSION_WARN(is,
2137 "icl_conn_task_setup() failed with error %d", error);
2138 uma_zfree(iscsi_outstanding_zone, io);
2142 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL,
2143 ("initiator_task_tag 0x%x already added", *initiator_task_tagp));
2145 io->io_initiator_task_tag = *initiator_task_tagp;
2147 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next);
2152 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io)
2155 ISCSI_SESSION_LOCK_ASSERT(is);
2157 icl_conn_task_done(is->is_conn, io->io_icl_prv);
2158 TAILQ_REMOVE(&is->is_outstanding, io, io_next);
2159 uma_zfree(iscsi_outstanding_zone, io);
2163 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb)
2165 struct icl_pdu *request;
2166 struct iscsi_bhs_task_management_request *bhstmr;
2167 struct ccb_abort *cab = &ccb->cab;
2168 struct iscsi_outstanding *io, *aio;
2169 uint32_t initiator_task_tag;
2171 ISCSI_SESSION_LOCK_ASSERT(is);
2174 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2176 if (is->is_login_phase) {
2177 ccb->ccb_h.status = CAM_REQ_ABORTED;
2183 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb);
2185 ccb->ccb_h.status = CAM_REQ_CMP;
2190 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2191 if (request == NULL) {
2192 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2197 initiator_task_tag = is->is_initiator_task_tag++;
2199 io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag);
2201 icl_pdu_free(request);
2202 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2206 io->io_datasn = aio->io_initiator_task_tag;
2208 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2209 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST;
2210 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK;
2211 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2212 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag;
2213 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag;
2215 iscsi_pdu_queue_locked(request);
2219 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb)
2221 struct icl_pdu *request;
2222 struct iscsi_bhs_scsi_command *bhssc;
2223 struct ccb_scsiio *csio;
2224 struct iscsi_outstanding *io;
2226 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 ISCSI_SESSION_DEBUG(is, "called during login phase");
2236 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2237 xpt_freeze_devq(ccb->ccb_h.path, 1);
2238 ISCSI_SESSION_DEBUG(is, "freezing devq");
2240 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN;
2246 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2247 if (request == NULL) {
2248 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2249 xpt_freeze_devq(ccb->ccb_h.path, 1);
2250 ISCSI_SESSION_DEBUG(is, "freezing devq");
2252 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2257 initiator_task_tag = is->is_initiator_task_tag++;
2258 io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag);
2260 icl_pdu_free(request);
2261 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2262 xpt_freeze_devq(ccb->ccb_h.path, 1);
2263 ISCSI_SESSION_DEBUG(is, "freezing devq");
2265 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2271 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2272 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND;
2273 bhssc->bhssc_flags |= BHSSC_FLAGS_F;
2274 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
2276 bhssc->bhssc_flags |= BHSSC_FLAGS_R;
2279 bhssc->bhssc_flags |= BHSSC_FLAGS_W;
2283 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2284 switch (csio->tag_action) {
2285 case MSG_HEAD_OF_Q_TAG:
2286 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ;
2288 case MSG_ORDERED_Q_TAG:
2289 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED;
2292 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA;
2294 case MSG_SIMPLE_Q_TAG:
2296 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE;
2300 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED;
2302 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2303 bhssc->bhssc_initiator_task_tag = initiator_task_tag;
2304 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len);
2305 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb),
2306 ("unsupported CDB size %zd", (size_t)csio->cdb_len));
2308 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2309 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len);
2311 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len);
2313 if (is->is_immediate_data &&
2314 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2315 len = csio->dxfer_len;
2316 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len);
2317 if (len > is->is_first_burst_length) {
2318 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len, is->is_first_burst_length);
2319 len = is->is_first_burst_length;
2321 if (len > is->is_max_send_data_segment_length) {
2322 ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len,
2323 is->is_max_send_data_segment_length);
2324 len = is->is_max_send_data_segment_length;
2327 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT);
2329 iscsi_outstanding_remove(is, io);
2330 icl_pdu_free(request);
2331 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2332 xpt_freeze_devq(ccb->ccb_h.path, 1);
2333 ISCSI_SESSION_DEBUG(is, "freezing devq");
2335 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2340 iscsi_pdu_queue_locked(request);
2344 iscsi_action(struct cam_sim *sim, union ccb *ccb)
2346 struct iscsi_session *is;
2348 is = cam_sim_softc(sim);
2350 ISCSI_SESSION_LOCK_ASSERT(is);
2352 if (is->is_terminating ||
2353 (is->is_connected == false && fail_on_disconnection)) {
2354 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2360 * Make sure CAM doesn't sneak in a CCB just after freezing the queue.
2362 if (is->is_simq_frozen == true) {
2363 ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
2364 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
2365 /* Don't freeze the devq - the SIM queue is already frozen. */
2370 switch (ccb->ccb_h.func_code) {
2373 struct ccb_pathinq *cpi = &ccb->cpi;
2375 cpi->version_num = 1;
2376 cpi->hba_inquiry = PI_TAG_ABLE;
2377 cpi->target_sprt = 0;
2378 cpi->hba_misc = PIM_EXTLUNS;
2380 * XXX: It shouldn't ever be NULL; this could be turned
2381 * into a KASSERT eventually.
2383 if (is->is_conn == NULL)
2384 ISCSI_WARN("NULL conn");
2385 else if (is->is_conn->ic_unmapped)
2386 cpi->hba_misc |= PIM_UNMAPPED;
2387 cpi->hba_eng_cnt = 0;
2388 cpi->max_target = 0;
2390 * Note that the variable below is only relevant for targets
2391 * that don't claim compliance with anything above SPC2, which
2392 * means they don't support REPORT_LUNS.
2395 cpi->initiator_id = ~0;
2396 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2397 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
2398 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2399 cpi->unit_number = cam_sim_unit(sim);
2400 cpi->bus_id = cam_sim_bus(sim);
2401 cpi->base_transfer_speed = 150000; /* XXX */
2402 cpi->transport = XPORT_ISCSI;
2403 cpi->transport_version = 0;
2404 cpi->protocol = PROTO_SCSI;
2405 cpi->protocol_version = SCSI_REV_SPC3;
2406 cpi->maxio = MAXPHYS;
2407 cpi->ccb_h.status = CAM_REQ_CMP;
2410 case XPT_GET_TRAN_SETTINGS:
2412 struct ccb_trans_settings *cts;
2413 struct ccb_trans_settings_scsi *scsi;
2416 scsi = &cts->proto_specific.scsi;
2418 cts->protocol = PROTO_SCSI;
2419 cts->protocol_version = SCSI_REV_SPC3;
2420 cts->transport = XPORT_ISCSI;
2421 cts->transport_version = 0;
2422 scsi->valid = CTS_SCSI_VALID_TQ;
2423 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2424 cts->ccb_h.status = CAM_REQ_CMP;
2427 case XPT_CALC_GEOMETRY:
2428 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2429 ccb->ccb_h.status = CAM_REQ_CMP;
2433 * XXX: What's the point?
2437 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io");
2438 ccb->ccb_h.status = CAM_REQ_CMP;
2442 iscsi_action_abort(is, ccb);
2445 iscsi_action_scsiio(is, ccb);
2449 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code);
2451 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2458 iscsi_poll(struct cam_sim *sim)
2461 KASSERT(0, ("%s: you're not supposed to be here", __func__));
2465 iscsi_terminate_sessions(struct iscsi_softc *sc)
2467 struct iscsi_session *is;
2469 sx_slock(&sc->sc_lock);
2470 TAILQ_FOREACH(is, &sc->sc_sessions, is_next)
2471 iscsi_session_terminate(is);
2472 while(!TAILQ_EMPTY(&sc->sc_sessions)) {
2473 ISCSI_DEBUG("waiting for sessions to terminate");
2474 cv_wait(&sc->sc_cv, &sc->sc_lock);
2476 ISCSI_DEBUG("all sessions terminated");
2477 sx_sunlock(&sc->sc_lock);
2481 iscsi_shutdown_pre(struct iscsi_softc *sc)
2483 struct iscsi_session *is;
2485 if (!fail_on_shutdown)
2489 * If we have any sessions waiting for reconnection, request
2490 * maintenance thread to fail them immediately instead of waiting
2491 * for reconnect timeout.
2493 * This prevents LUNs with mounted filesystems that are supported
2494 * by disconnected iSCSI sessions from hanging, however it will
2495 * fail all queued BIOs.
2497 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown");
2499 fail_on_disconnection = 1;
2501 sx_slock(&sc->sc_lock);
2502 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2503 ISCSI_SESSION_LOCK(is);
2504 if (!is->is_connected) {
2505 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early");
2506 iscsi_session_reconnect(is);
2508 ISCSI_SESSION_UNLOCK(is);
2510 sx_sunlock(&sc->sc_lock);
2514 iscsi_shutdown_post(struct iscsi_softc *sc)
2517 if (!KERNEL_PANICKED()) {
2518 ISCSI_DEBUG("removing all sessions due to shutdown");
2519 iscsi_terminate_sessions(sc);
2528 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK);
2529 sx_init(&sc->sc_lock, "iscsi");
2530 TAILQ_INIT(&sc->sc_sessions);
2531 cv_init(&sc->sc_cv, "iscsi_cv");
2533 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding",
2534 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL,
2537 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw,
2538 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
2540 ISCSI_WARN("failed to create device node, error %d", error);
2543 sc->sc_cdev->si_drv1 = sc;
2545 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2546 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST);
2548 * shutdown_post_sync needs to run after filesystem shutdown and before
2549 * CAM shutdown - otherwise when rebooting with an iSCSI session that is
2550 * disconnected but has outstanding requests, dashutdown() will hang on
2551 * cam_periph_runccb().
2553 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
2554 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1);
2563 if (sc->sc_cdev != NULL) {
2564 ISCSI_DEBUG("removing device node");
2565 destroy_dev(sc->sc_cdev);
2566 ISCSI_DEBUG("device node removed");
2569 if (sc->sc_shutdown_pre_eh != NULL)
2570 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh);
2571 if (sc->sc_shutdown_post_eh != NULL)
2572 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh);
2574 iscsi_terminate_sessions(sc);
2576 uma_zdestroy(iscsi_outstanding_zone);
2577 sx_destroy(&sc->sc_lock);
2578 cv_destroy(&sc->sc_cv);
2586 sx_slock(&sc->sc_lock);
2587 if (!TAILQ_EMPTY(&sc->sc_sessions)) {
2588 sx_sunlock(&sc->sc_lock);
2591 sx_sunlock(&sc->sc_lock);
2596 iscsi_modevent(module_t mod, int what, void *arg)
2602 error = iscsi_load();
2605 error = iscsi_unload();
2608 error = iscsi_quiesce();
2617 moduledata_t iscsi_data = {
2623 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2624 MODULE_DEPEND(iscsi, cam, 1, 1, 1);
2625 MODULE_DEPEND(iscsi, icl, 1, 1, 1);