2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/condvar.h>
37 #include <sys/endian.h>
38 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/module.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/cam_debug.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_periph.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
63 #include <dev/iscsi/icl.h>
64 #include <dev/iscsi/icl_wrappers.h>
65 #include <dev/iscsi/iscsi_ioctl.h>
66 #include <dev/iscsi/iscsi_proto.h>
67 #include <dev/iscsi/iscsi.h>
69 #ifdef ICL_KERNEL_PROXY
70 #include <sys/socketvar.h>
73 #ifdef ICL_KERNEL_PROXY
74 FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY");
78 * XXX: This is global so the iscsi_unload() can access it.
79 * Think about how to do this properly.
81 static struct iscsi_softc *sc;
83 SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD, 0, "iSCSI initiator");
85 SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
86 &debug, 0, "Enable debug messages");
87 static int ping_timeout = 5;
88 SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout,
89 0, "Timeout for ping (NOP-Out) requests, in seconds");
90 static int iscsid_timeout = 60;
91 SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout,
92 0, "Time to wait for iscsid(8) to handle reconnection, in seconds");
93 static int login_timeout = 60;
94 SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout,
95 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds");
96 static int maxtags = 255;
97 SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags,
98 0, "Max number of IO requests queued");
99 static int fail_on_disconnection = 0;
100 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN,
101 &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure");
102 static int fail_on_shutdown = 1;
103 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_shutdown, CTLFLAG_RWTUN,
104 &fail_on_shutdown, 0, "Fail disconnected sessions on shutdown");
106 static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator");
107 static uma_zone_t iscsi_outstanding_zone;
109 #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0)
110 #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn))
112 #define ISCSI_DEBUG(X, ...) \
115 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
118 #define ISCSI_WARN(X, ...) \
121 printf("WARNING: %s: " X "\n", \
122 __func__, ## __VA_ARGS__); \
126 #define ISCSI_SESSION_DEBUG(S, X, ...) \
129 printf("%s: %s (%s): " X "\n", \
130 __func__, S->is_conf.isc_target_addr, \
131 S->is_conf.isc_target, ## __VA_ARGS__); \
135 #define ISCSI_SESSION_WARN(S, X, ...) \
138 printf("WARNING: %s (%s): " X "\n", \
139 S->is_conf.isc_target_addr, \
140 S->is_conf.isc_target, ## __VA_ARGS__); \
144 #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock)
145 #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock)
146 #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED)
147 #define ISCSI_SESSION_LOCK_ASSERT_NOT(X) mtx_assert(&X->is_lock, MA_NOTOWNED)
149 static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
150 int mode, struct thread *td);
152 static struct cdevsw iscsi_cdevsw = {
153 .d_version = D_VERSION,
154 .d_ioctl = iscsi_ioctl,
158 static void iscsi_pdu_queue_locked(struct icl_pdu *request);
159 static void iscsi_pdu_queue(struct icl_pdu *request);
160 static void iscsi_pdu_update_statsn(const struct icl_pdu *response);
161 static void iscsi_pdu_handle_nop_in(struct icl_pdu *response);
162 static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response);
163 static void iscsi_pdu_handle_task_response(struct icl_pdu *response);
164 static void iscsi_pdu_handle_data_in(struct icl_pdu *response);
165 static void iscsi_pdu_handle_logout_response(struct icl_pdu *response);
166 static void iscsi_pdu_handle_r2t(struct icl_pdu *response);
167 static void iscsi_pdu_handle_async_message(struct icl_pdu *response);
168 static void iscsi_pdu_handle_reject(struct icl_pdu *response);
169 static void iscsi_session_reconnect(struct iscsi_session *is);
170 static void iscsi_session_terminate(struct iscsi_session *is);
171 static void iscsi_action(struct cam_sim *sim, union ccb *ccb);
172 static void iscsi_poll(struct cam_sim *sim);
173 static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is,
174 uint32_t initiator_task_tag);
175 static struct iscsi_outstanding *iscsi_outstanding_add(struct iscsi_session *is,
176 struct icl_pdu *request, union ccb *ccb,
177 uint32_t *initiator_task_tagp);
178 static void iscsi_outstanding_remove(struct iscsi_session *is,
179 struct iscsi_outstanding *io);
182 iscsi_pdu_prepare(struct icl_pdu *request)
184 struct iscsi_session *is;
185 struct iscsi_bhs_scsi_command *bhssc;
187 is = PDU_SESSION(request);
189 ISCSI_SESSION_LOCK_ASSERT(is);
192 * We're only using fields common for all the request
193 * (initiator -> target) PDUs.
195 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
198 * Data-Out PDU does not contain CmdSN.
200 if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
201 if (ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn) &&
202 (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
204 * Current MaxCmdSN prevents us from sending any more
205 * SCSI Command PDUs to the target; postpone the PDU.
206 * It will get resent by either iscsi_pdu_queue(),
207 * or by maintenance thread.
210 ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %u, "
211 "ExpCmdSN %u, MaxCmdSN %u, opcode 0x%x",
212 is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn,
213 bhssc->bhssc_opcode);
217 bhssc->bhssc_cmdsn = htonl(is->is_cmdsn);
218 if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
221 bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1);
227 iscsi_session_send_postponed(struct iscsi_session *is)
229 struct icl_pdu *request;
232 ISCSI_SESSION_LOCK_ASSERT(is);
234 while (!STAILQ_EMPTY(&is->is_postponed)) {
235 request = STAILQ_FIRST(&is->is_postponed);
236 postpone = iscsi_pdu_prepare(request);
239 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
240 icl_pdu_queue(request);
245 iscsi_pdu_queue_locked(struct icl_pdu *request)
247 struct iscsi_session *is;
250 is = PDU_SESSION(request);
251 ISCSI_SESSION_LOCK_ASSERT(is);
252 iscsi_session_send_postponed(is);
253 postpone = iscsi_pdu_prepare(request);
255 STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next);
258 icl_pdu_queue(request);
262 iscsi_pdu_queue(struct icl_pdu *request)
264 struct iscsi_session *is;
266 is = PDU_SESSION(request);
267 ISCSI_SESSION_LOCK(is);
268 iscsi_pdu_queue_locked(request);
269 ISCSI_SESSION_UNLOCK(is);
273 iscsi_session_logout(struct iscsi_session *is)
275 struct icl_pdu *request;
276 struct iscsi_bhs_logout_request *bhslr;
278 request = icl_pdu_new(is->is_conn, M_NOWAIT);
282 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
283 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST;
284 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
285 iscsi_pdu_queue_locked(request);
289 iscsi_session_terminate_task(struct iscsi_session *is,
290 struct iscsi_outstanding *io, bool requeue)
293 ISCSI_SESSION_LOCK_ASSERT(is);
295 if (io->io_ccb != NULL) {
296 io->io_ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
298 io->io_ccb->ccb_h.status |= CAM_REQUEUE_REQ;
300 io->io_ccb->ccb_h.status |= CAM_REQ_ABORTED;
301 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
302 io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN;
303 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1);
304 ISCSI_SESSION_DEBUG(is, "freezing devq");
306 xpt_done(io->io_ccb);
308 iscsi_outstanding_remove(is, io);
312 iscsi_session_terminate_tasks(struct iscsi_session *is, bool requeue)
314 struct iscsi_outstanding *io, *tmp;
316 ISCSI_SESSION_LOCK_ASSERT(is);
318 TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) {
319 iscsi_session_terminate_task(is, io, requeue);
324 iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim)
328 ISCSI_SESSION_LOCK_ASSERT(is);
331 * Don't queue any new PDUs.
333 if (is->is_sim != NULL && is->is_simq_frozen == false) {
334 ISCSI_SESSION_DEBUG(is, "freezing");
335 xpt_freeze_simq(is->is_sim, 1);
336 is->is_simq_frozen = true;
340 * Remove postponed PDUs.
342 while (!STAILQ_EMPTY(&is->is_postponed)) {
343 pdu = STAILQ_FIRST(&is->is_postponed);
344 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
348 if (destroy_sim == false) {
350 * Terminate SCSI tasks, asking CAM to requeue them.
352 iscsi_session_terminate_tasks(is, true);
356 iscsi_session_terminate_tasks(is, false);
358 if (is->is_sim == NULL)
361 ISCSI_SESSION_DEBUG(is, "deregistering SIM");
362 xpt_async(AC_LOST_DEVICE, is->is_path, NULL);
364 if (is->is_simq_frozen) {
365 xpt_release_simq(is->is_sim, 1);
366 is->is_simq_frozen = false;
369 xpt_free_path(is->is_path);
371 xpt_bus_deregister(cam_sim_path(is->is_sim));
372 cam_sim_free(is->is_sim, TRUE /*free_devq*/);
378 iscsi_maintenance_thread_reconnect(struct iscsi_session *is)
381 icl_conn_close(is->is_conn);
383 ISCSI_SESSION_LOCK(is);
385 is->is_connected = false;
386 is->is_reconnecting = false;
387 is->is_login_phase = false;
389 #ifdef ICL_KERNEL_PROXY
390 if (is->is_login_pdu != NULL) {
391 icl_pdu_free(is->is_login_pdu);
392 is->is_login_pdu = NULL;
394 cv_signal(&is->is_login_cv);
397 if (fail_on_disconnection) {
398 ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices");
399 iscsi_session_cleanup(is, true);
401 iscsi_session_cleanup(is, false);
404 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
405 ("destroying session with active tasks"));
406 KASSERT(STAILQ_EMPTY(&is->is_postponed),
407 ("destroying session with postponed PDUs"));
409 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
410 ISCSI_SESSION_UNLOCK(is);
415 * Request immediate reconnection from iscsid(8).
417 //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)");
418 is->is_waiting_for_iscsid = true;
419 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
421 ISCSI_SESSION_UNLOCK(is);
422 cv_signal(&is->is_softc->sc_cv);
426 iscsi_maintenance_thread_terminate(struct iscsi_session *is)
428 struct iscsi_softc *sc;
431 sx_xlock(&sc->sc_lock);
433 icl_conn_close(is->is_conn);
434 callout_drain(&is->is_callout);
436 ISCSI_SESSION_LOCK(is);
438 KASSERT(is->is_terminating, ("is_terminating == false"));
440 #ifdef ICL_KERNEL_PROXY
441 if (is->is_login_pdu != NULL) {
442 icl_pdu_free(is->is_login_pdu);
443 is->is_login_pdu = NULL;
445 cv_signal(&is->is_login_cv);
448 iscsi_session_cleanup(is, true);
450 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
451 ("destroying session with active tasks"));
452 KASSERT(STAILQ_EMPTY(&is->is_postponed),
453 ("destroying session with postponed PDUs"));
455 ISCSI_SESSION_UNLOCK(is);
457 icl_conn_free(is->is_conn);
458 mtx_destroy(&is->is_lock);
459 cv_destroy(&is->is_maintenance_cv);
460 #ifdef ICL_KERNEL_PROXY
461 cv_destroy(&is->is_login_cv);
463 TAILQ_REMOVE(&sc->sc_sessions, is, is_next);
464 sx_xunlock(&sc->sc_lock);
466 ISCSI_SESSION_DEBUG(is, "terminated");
470 * The iscsi_unload() routine might be waiting.
472 cv_signal(&sc->sc_cv);
476 iscsi_maintenance_thread(void *arg)
478 struct iscsi_session *is;
483 ISCSI_SESSION_LOCK(is);
484 if (is->is_reconnecting == false &&
485 is->is_terminating == false &&
486 STAILQ_EMPTY(&is->is_postponed))
487 cv_wait(&is->is_maintenance_cv, &is->is_lock);
489 /* Terminate supersedes reconnect. */
490 if (is->is_terminating) {
491 ISCSI_SESSION_UNLOCK(is);
492 iscsi_maintenance_thread_terminate(is);
497 if (is->is_reconnecting) {
498 ISCSI_SESSION_UNLOCK(is);
499 iscsi_maintenance_thread_reconnect(is);
503 iscsi_session_send_postponed(is);
504 ISCSI_SESSION_UNLOCK(is);
509 iscsi_session_reconnect(struct iscsi_session *is)
513 * XXX: We can't use locking here, because
514 * it's being called from various contexts.
515 * Hope it doesn't break anything.
517 if (is->is_reconnecting)
520 is->is_reconnecting = true;
521 cv_signal(&is->is_maintenance_cv);
525 iscsi_session_terminate(struct iscsi_session *is)
528 if (is->is_terminating)
531 is->is_terminating = true;
534 iscsi_session_logout(is);
536 cv_signal(&is->is_maintenance_cv);
540 iscsi_callout(void *context)
542 struct icl_pdu *request;
543 struct iscsi_bhs_nop_out *bhsno;
544 struct iscsi_session *is;
545 bool reconnect_needed = false;
549 ISCSI_SESSION_LOCK(is);
550 if (is->is_terminating) {
551 ISCSI_SESSION_UNLOCK(is);
555 callout_schedule(&is->is_callout, 1 * hz);
557 if (is->is_conf.isc_enable == 0)
562 if (is->is_waiting_for_iscsid) {
563 if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) {
564 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) "
565 "for %d seconds; reconnecting",
567 reconnect_needed = true;
572 if (is->is_login_phase) {
573 if (login_timeout > 0 && is->is_timeout > login_timeout) {
574 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; "
575 "reconnecting", is->is_timeout);
576 reconnect_needed = true;
581 if (ping_timeout <= 0) {
583 * Pings are disabled. Don't send NOP-Out in this case.
584 * Reset the timeout, to avoid triggering reconnection,
585 * should the user decide to reenable them.
591 if (is->is_timeout >= ping_timeout) {
592 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; "
593 "reconnecting", ping_timeout);
594 reconnect_needed = true;
598 ISCSI_SESSION_UNLOCK(is);
601 * If the ping was reset less than one second ago - which means
602 * that we've received some PDU during the last second - assume
603 * the traffic flows correctly and don't bother sending a NOP-Out.
605 * (It's 2 - one for one second, and one for incrementing is_timeout
606 * earlier in this routine.)
608 if (is->is_timeout < 2)
611 request = icl_pdu_new(is->is_conn, M_NOWAIT);
612 if (request == NULL) {
613 ISCSI_SESSION_WARN(is, "failed to allocate PDU");
616 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
617 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
618 ISCSI_BHS_OPCODE_IMMEDIATE;
619 bhsno->bhsno_flags = 0x80;
620 bhsno->bhsno_target_transfer_tag = 0xffffffff;
621 iscsi_pdu_queue(request);
625 if (is->is_terminating) {
626 ISCSI_SESSION_UNLOCK(is);
630 ISCSI_SESSION_UNLOCK(is);
632 if (reconnect_needed)
633 iscsi_session_reconnect(is);
637 iscsi_pdu_update_statsn(const struct icl_pdu *response)
639 const struct iscsi_bhs_data_in *bhsdi;
640 struct iscsi_session *is;
641 uint32_t expcmdsn, maxcmdsn, statsn;
643 is = PDU_SESSION(response);
645 ISCSI_SESSION_LOCK_ASSERT(is);
648 * We're only using fields common for all the response
649 * (target -> initiator) PDUs.
651 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs;
653 * Ok, I lied. In case of Data-In, "The fields StatSN, Status,
654 * and Residual Count only have meaningful content if the S bit
655 * is set to 1", so we also need to check the bit specific for
658 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
659 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) {
660 statsn = ntohl(bhsdi->bhsdi_statsn);
661 if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) {
662 /* XXX: This is normal situation for MCS */
663 ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != "
664 "session ExpStatSN %u (or + 1); reconnecting",
665 bhsdi->bhsdi_opcode, statsn, is->is_statsn);
666 iscsi_session_reconnect(is);
668 if (ISCSI_SNGT(statsn, is->is_statsn))
669 is->is_statsn = statsn;
672 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn);
673 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn);
675 if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) {
676 ISCSI_SESSION_DEBUG(is,
677 "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring",
680 if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) {
681 is->is_maxcmdsn = maxcmdsn;
684 * Command window increased; kick the maintanance thread
685 * to send out postponed commands.
687 if (!STAILQ_EMPTY(&is->is_postponed))
688 cv_signal(&is->is_maintenance_cv);
689 } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) {
690 /* XXX: This is normal situation for MCS */
691 ISCSI_SESSION_DEBUG(is,
692 "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring",
693 maxcmdsn, is->is_maxcmdsn);
696 if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) {
697 is->is_expcmdsn = expcmdsn;
698 } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) {
699 /* XXX: This is normal situation for MCS */
700 ISCSI_SESSION_DEBUG(is,
701 "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring",
702 expcmdsn, is->is_expcmdsn);
707 * Every incoming PDU - not just NOP-In - resets the ping timer.
708 * The purpose of the timeout is to reset the connection when it stalls;
709 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
716 iscsi_receive_callback(struct icl_pdu *response)
718 struct iscsi_session *is;
720 is = PDU_SESSION(response);
722 ISCSI_SESSION_LOCK(is);
724 iscsi_pdu_update_statsn(response);
726 #ifdef ICL_KERNEL_PROXY
727 if (is->is_login_phase) {
728 if (is->is_login_pdu == NULL)
729 is->is_login_pdu = response;
731 icl_pdu_free(response);
732 ISCSI_SESSION_UNLOCK(is);
733 cv_signal(&is->is_login_cv);
739 * The handling routine is responsible for freeing the PDU
740 * when it's no longer needed.
742 switch (response->ip_bhs->bhs_opcode) {
743 case ISCSI_BHS_OPCODE_NOP_IN:
744 iscsi_pdu_handle_nop_in(response);
745 ISCSI_SESSION_UNLOCK(is);
747 case ISCSI_BHS_OPCODE_SCSI_RESPONSE:
748 iscsi_pdu_handle_scsi_response(response);
749 /* Session lock dropped inside. */
750 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
752 case ISCSI_BHS_OPCODE_TASK_RESPONSE:
753 iscsi_pdu_handle_task_response(response);
754 ISCSI_SESSION_UNLOCK(is);
756 case ISCSI_BHS_OPCODE_SCSI_DATA_IN:
757 iscsi_pdu_handle_data_in(response);
758 /* Session lock dropped inside. */
759 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
761 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE:
762 iscsi_pdu_handle_logout_response(response);
763 ISCSI_SESSION_UNLOCK(is);
765 case ISCSI_BHS_OPCODE_R2T:
766 iscsi_pdu_handle_r2t(response);
767 ISCSI_SESSION_UNLOCK(is);
769 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE:
770 iscsi_pdu_handle_async_message(response);
771 ISCSI_SESSION_UNLOCK(is);
773 case ISCSI_BHS_OPCODE_REJECT:
774 iscsi_pdu_handle_reject(response);
775 ISCSI_SESSION_UNLOCK(is);
778 ISCSI_SESSION_WARN(is, "received PDU with unsupported "
779 "opcode 0x%x; reconnecting",
780 response->ip_bhs->bhs_opcode);
781 iscsi_session_reconnect(is);
782 ISCSI_SESSION_UNLOCK(is);
783 icl_pdu_free(response);
788 iscsi_error_callback(struct icl_conn *ic)
790 struct iscsi_session *is;
792 is = CONN_SESSION(ic);
794 ISCSI_SESSION_WARN(is, "connection error; reconnecting");
795 iscsi_session_reconnect(is);
799 iscsi_pdu_handle_nop_in(struct icl_pdu *response)
801 struct iscsi_session *is;
802 struct iscsi_bhs_nop_out *bhsno;
803 struct iscsi_bhs_nop_in *bhsni;
804 struct icl_pdu *request;
809 is = PDU_SESSION(response);
810 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
812 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) {
814 * Nothing to do; iscsi_pdu_update_statsn() already
815 * zeroed the timeout.
817 icl_pdu_free(response);
821 datasize = icl_pdu_data_segment_length(response);
823 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO);
825 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
827 icl_pdu_free(response);
828 iscsi_session_reconnect(is);
831 icl_pdu_get_data(response, 0, data, datasize);
834 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
835 if (request == NULL) {
836 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
839 icl_pdu_free(response);
840 iscsi_session_reconnect(is);
843 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
844 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
845 ISCSI_BHS_OPCODE_IMMEDIATE;
846 bhsno->bhsno_flags = 0x80;
847 bhsno->bhsno_initiator_task_tag = 0xffffffff;
848 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag;
850 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT);
852 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
855 icl_pdu_free(request);
856 icl_pdu_free(response);
857 iscsi_session_reconnect(is);
863 icl_pdu_free(response);
864 iscsi_pdu_queue_locked(request);
868 iscsi_pdu_handle_scsi_response(struct icl_pdu *response)
870 struct iscsi_bhs_scsi_response *bhssr;
871 struct iscsi_outstanding *io;
872 struct iscsi_session *is;
874 struct ccb_scsiio *csio;
875 size_t data_segment_len, received;
879 is = PDU_SESSION(response);
881 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
882 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag);
883 if (io == NULL || io->io_ccb == NULL) {
884 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag);
885 icl_pdu_free(response);
886 iscsi_session_reconnect(is);
887 ISCSI_SESSION_UNLOCK(is);
894 * With iSER, after getting good response we can be sure
895 * that all the data has been successfully transferred.
897 if (is->is_conn->ic_iser) {
898 resid = ntohl(bhssr->bhssr_residual_count);
899 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) {
900 io->io_received = ccb->csio.dxfer_len - resid;
901 } else if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) {
902 ISCSI_SESSION_WARN(is, "overflow: target indicates %d", resid);
904 io->io_received = ccb->csio.dxfer_len;
908 received = io->io_received;
909 iscsi_outstanding_remove(is, io);
910 ISCSI_SESSION_UNLOCK(is);
912 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) {
913 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response);
914 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
915 xpt_freeze_devq(ccb->ccb_h.path, 1);
916 ISCSI_SESSION_DEBUG(is, "freezing devq");
918 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
919 } else if (bhssr->bhssr_status == 0) {
920 ccb->ccb_h.status = CAM_REQ_CMP;
922 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
923 xpt_freeze_devq(ccb->ccb_h.path, 1);
924 ISCSI_SESSION_DEBUG(is, "freezing devq");
926 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
927 ccb->csio.scsi_status = bhssr->bhssr_status;
931 data_segment_len = icl_pdu_data_segment_length(response);
932 if (data_segment_len > 0) {
933 if (data_segment_len < sizeof(sense_len)) {
934 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)",
936 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
937 xpt_freeze_devq(ccb->ccb_h.path, 1);
938 ISCSI_SESSION_DEBUG(is, "freezing devq");
940 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
943 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len));
944 sense_len = ntohs(sense_len);
946 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd",
947 sense_len, data_segment_len);
949 if (sizeof(sense_len) + sense_len > data_segment_len) {
950 ISCSI_SESSION_WARN(is, "truncated data segment "
951 "(%zd bytes, should be %zd)",
952 data_segment_len, sizeof(sense_len) + sense_len);
953 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
954 xpt_freeze_devq(ccb->ccb_h.path, 1);
955 ISCSI_SESSION_DEBUG(is, "freezing devq");
957 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
959 } else if (sizeof(sense_len) + sense_len < data_segment_len)
960 ISCSI_SESSION_WARN(is, "oversize data segment "
961 "(%zd bytes, should be %zd)",
962 data_segment_len, sizeof(sense_len) + sense_len);
963 if (sense_len > csio->sense_len) {
964 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d",
965 sense_len, csio->sense_len);
966 sense_len = csio->sense_len;
968 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len);
969 csio->sense_resid = csio->sense_len - sense_len;
970 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
974 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW)
975 csio->resid = ntohl(bhssr->bhssr_residual_count);
977 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
978 KASSERT(received <= csio->dxfer_len,
979 ("received > csio->dxfer_len"));
980 if (received < csio->dxfer_len) {
981 if (csio->resid != csio->dxfer_len - received) {
982 ISCSI_SESSION_WARN(is, "underflow mismatch: "
983 "target indicates %d, we calculated %zd",
984 csio->resid, csio->dxfer_len - received);
986 csio->resid = csio->dxfer_len - received;
991 icl_pdu_free(response);
995 iscsi_pdu_handle_task_response(struct icl_pdu *response)
997 struct iscsi_bhs_task_management_response *bhstmr;
998 struct iscsi_outstanding *io, *aio;
999 struct iscsi_session *is;
1001 is = PDU_SESSION(response);
1003 bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs;
1004 io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag);
1005 if (io == NULL || io->io_ccb != NULL) {
1006 ISCSI_SESSION_WARN(is, "bad itt 0x%x",
1007 bhstmr->bhstmr_initiator_task_tag);
1008 icl_pdu_free(response);
1009 iscsi_session_reconnect(is);
1013 if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) {
1014 ISCSI_SESSION_WARN(is, "task response 0x%x",
1015 bhstmr->bhstmr_response);
1017 aio = iscsi_outstanding_find(is, io->io_datasn);
1018 if (aio != NULL && aio->io_ccb != NULL)
1019 iscsi_session_terminate_task(is, aio, false);
1022 iscsi_outstanding_remove(is, io);
1023 icl_pdu_free(response);
1027 iscsi_pdu_handle_data_in(struct icl_pdu *response)
1029 struct iscsi_bhs_data_in *bhsdi;
1030 struct iscsi_outstanding *io;
1031 struct iscsi_session *is;
1033 struct ccb_scsiio *csio;
1034 size_t data_segment_len, received, oreceived;
1036 is = PDU_SESSION(response);
1037 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
1038 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag);
1039 if (io == NULL || io->io_ccb == NULL) {
1040 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag);
1041 icl_pdu_free(response);
1042 iscsi_session_reconnect(is);
1043 ISCSI_SESSION_UNLOCK(is);
1047 data_segment_len = icl_pdu_data_segment_length(response);
1048 if (data_segment_len == 0) {
1050 * "The sending of 0 length data segments should be avoided,
1051 * but initiators and targets MUST be able to properly receive
1052 * 0 length data segments."
1054 ISCSI_SESSION_UNLOCK(is);
1055 icl_pdu_free(response);
1060 * We need to track this for security reasons - without it, malicious target
1061 * could respond to SCSI READ without sending Data-In PDUs, which would result
1062 * in read operation on the initiator side returning random kernel data.
1064 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) {
1065 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd",
1066 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset));
1067 icl_pdu_free(response);
1068 iscsi_session_reconnect(is);
1069 ISCSI_SESSION_UNLOCK(is);
1076 if (io->io_received + data_segment_len > csio->dxfer_len) {
1077 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes "
1078 "at offset %zd, buffer is %d)",
1079 data_segment_len, io->io_received, csio->dxfer_len);
1080 icl_pdu_free(response);
1081 iscsi_session_reconnect(is);
1082 ISCSI_SESSION_UNLOCK(is);
1086 oreceived = io->io_received;
1087 io->io_received += data_segment_len;
1088 received = io->io_received;
1089 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0)
1090 iscsi_outstanding_remove(is, io);
1091 ISCSI_SESSION_UNLOCK(is);
1093 icl_pdu_get_data(response, 0, csio->data_ptr + oreceived, data_segment_len);
1096 * XXX: Check DataSN.
1099 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) {
1101 * Nothing more to do.
1103 icl_pdu_free(response);
1107 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status);
1108 if (bhsdi->bhsdi_status == 0) {
1109 ccb->ccb_h.status = CAM_REQ_CMP;
1111 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1112 xpt_freeze_devq(ccb->ccb_h.path, 1);
1113 ISCSI_SESSION_DEBUG(is, "freezing devq");
1115 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
1116 csio->scsi_status = bhsdi->bhsdi_status;
1119 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1120 KASSERT(received <= csio->dxfer_len,
1121 ("received > csio->dxfer_len"));
1122 if (received < csio->dxfer_len) {
1123 csio->resid = ntohl(bhsdi->bhsdi_residual_count);
1124 if (csio->resid != csio->dxfer_len - received) {
1125 ISCSI_SESSION_WARN(is, "underflow mismatch: "
1126 "target indicates %d, we calculated %zd",
1127 csio->resid, csio->dxfer_len - received);
1129 csio->resid = csio->dxfer_len - received;
1134 icl_pdu_free(response);
1138 iscsi_pdu_handle_logout_response(struct icl_pdu *response)
1141 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response");
1142 icl_pdu_free(response);
1146 iscsi_pdu_handle_r2t(struct icl_pdu *response)
1148 struct icl_pdu *request;
1149 struct iscsi_session *is;
1150 struct iscsi_bhs_r2t *bhsr2t;
1151 struct iscsi_bhs_data_out *bhsdo;
1152 struct iscsi_outstanding *io;
1153 struct ccb_scsiio *csio;
1154 size_t off, len, total_len;
1157 is = PDU_SESSION(response);
1159 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
1160 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag);
1161 if (io == NULL || io->io_ccb == NULL) {
1162 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting",
1163 bhsr2t->bhsr2t_initiator_task_tag);
1164 icl_pdu_free(response);
1165 iscsi_session_reconnect(is);
1169 csio = &io->io_ccb->csio;
1171 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) {
1172 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting");
1173 icl_pdu_free(response);
1174 iscsi_session_reconnect(is);
1179 * XXX: Verify R2TSN.
1184 off = ntohl(bhsr2t->bhsr2t_buffer_offset);
1185 if (off > csio->dxfer_len) {
1186 ISCSI_SESSION_WARN(is, "target requested invalid offset "
1187 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len);
1188 icl_pdu_free(response);
1189 iscsi_session_reconnect(is);
1193 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length);
1194 if (total_len == 0 || total_len > csio->dxfer_len) {
1195 ISCSI_SESSION_WARN(is, "target requested invalid length "
1196 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len);
1197 icl_pdu_free(response);
1198 iscsi_session_reconnect(is);
1202 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len);
1207 if (len > is->is_max_data_segment_length)
1208 len = is->is_max_data_segment_length;
1210 if (off + len > csio->dxfer_len) {
1211 ISCSI_SESSION_WARN(is, "target requested invalid "
1212 "length/offset %zd, buffer is %d; reconnecting",
1213 off + len, csio->dxfer_len);
1214 icl_pdu_free(response);
1215 iscsi_session_reconnect(is);
1219 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
1220 if (request == NULL) {
1221 icl_pdu_free(response);
1222 iscsi_session_reconnect(is);
1226 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
1227 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT;
1228 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun;
1229 bhsdo->bhsdo_initiator_task_tag =
1230 bhsr2t->bhsr2t_initiator_task_tag;
1231 bhsdo->bhsdo_target_transfer_tag =
1232 bhsr2t->bhsr2t_target_transfer_tag;
1233 bhsdo->bhsdo_datasn = htonl(io->io_datasn++);
1234 bhsdo->bhsdo_buffer_offset = htonl(off);
1235 error = icl_pdu_append_data(request, csio->data_ptr + off, len,
1238 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
1240 icl_pdu_free(request);
1241 icl_pdu_free(response);
1242 iscsi_session_reconnect(is);
1249 if (total_len == 0) {
1250 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F;
1251 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off);
1253 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off);
1256 iscsi_pdu_queue_locked(request);
1262 icl_pdu_free(response);
1266 iscsi_pdu_handle_async_message(struct icl_pdu *response)
1268 struct iscsi_bhs_asynchronous_message *bhsam;
1269 struct iscsi_session *is;
1271 is = PDU_SESSION(response);
1272 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1273 switch (bhsam->bhsam_async_event) {
1274 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT:
1275 ISCSI_SESSION_WARN(is, "target requests logout; removing session");
1276 iscsi_session_logout(is);
1277 iscsi_session_terminate(is);
1279 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION:
1280 ISCSI_SESSION_WARN(is, "target indicates it will drop drop the connection");
1282 case BHSAM_EVENT_TARGET_TERMINATES_SESSION:
1283 ISCSI_SESSION_WARN(is, "target indicates it will drop drop the session");
1287 * XXX: Technically, we're obligated to also handle
1288 * parameter renegotiation.
1290 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event);
1294 icl_pdu_free(response);
1298 iscsi_pdu_handle_reject(struct icl_pdu *response)
1300 struct iscsi_bhs_reject *bhsr;
1301 struct iscsi_session *is;
1303 is = PDU_SESSION(response);
1304 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs;
1305 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?",
1308 icl_pdu_free(response);
1312 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc,
1313 struct iscsi_daemon_request *request)
1315 struct iscsi_session *is;
1318 sx_slock(&sc->sc_lock);
1320 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1321 ISCSI_SESSION_LOCK(is);
1322 if (is->is_conf.isc_enable == 0 &&
1323 is->is_conf.isc_discovery == 0) {
1324 ISCSI_SESSION_UNLOCK(is);
1327 if (is->is_waiting_for_iscsid)
1329 ISCSI_SESSION_UNLOCK(is);
1334 * No session requires attention from iscsid(8); wait.
1336 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
1338 sx_sunlock(&sc->sc_lock);
1344 is->is_waiting_for_iscsid = false;
1345 is->is_login_phase = true;
1346 is->is_reason[0] = '\0';
1347 ISCSI_SESSION_UNLOCK(is);
1349 request->idr_session_id = is->is_id;
1350 memcpy(&request->idr_isid, &is->is_isid,
1351 sizeof(request->idr_isid));
1352 request->idr_tsih = 0; /* New or reinstated session. */
1353 memcpy(&request->idr_conf, &is->is_conf,
1354 sizeof(request->idr_conf));
1356 error = icl_limits(is->is_conf.isc_offload,
1357 is->is_conf.isc_iser,
1358 &request->idr_limits.isl_max_data_segment_length);
1360 ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" "
1361 "failed with error %d", is->is_conf.isc_offload,
1363 sx_sunlock(&sc->sc_lock);
1367 sx_sunlock(&sc->sc_lock);
1373 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc,
1374 struct iscsi_daemon_handoff *handoff)
1376 struct iscsi_session *is;
1377 struct icl_conn *ic;
1380 sx_slock(&sc->sc_lock);
1383 * Find the session to hand off socket to.
1385 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1386 if (is->is_id == handoff->idh_session_id)
1390 sx_sunlock(&sc->sc_lock);
1393 ISCSI_SESSION_LOCK(is);
1395 if (is->is_conf.isc_discovery || is->is_terminating) {
1396 ISCSI_SESSION_UNLOCK(is);
1397 sx_sunlock(&sc->sc_lock);
1400 if (is->is_connected) {
1402 * This might have happened because another iscsid(8)
1403 * instance handed off the connection in the meantime.
1406 ISCSI_SESSION_WARN(is, "handoff on already connected "
1408 ISCSI_SESSION_UNLOCK(is);
1409 sx_sunlock(&sc->sc_lock);
1413 strlcpy(is->is_target_alias, handoff->idh_target_alias,
1414 sizeof(is->is_target_alias));
1415 is->is_tsih = handoff->idh_tsih;
1416 is->is_statsn = handoff->idh_statsn;
1417 is->is_initial_r2t = handoff->idh_initial_r2t;
1418 is->is_immediate_data = handoff->idh_immediate_data;
1421 * Cap MaxRecvDataSegmentLength obtained from the target to the maximum
1422 * size supported by our ICL module.
1424 is->is_max_data_segment_length = min(ic->ic_max_data_segment_length,
1425 handoff->idh_max_data_segment_length);
1426 is->is_max_burst_length = handoff->idh_max_burst_length;
1427 is->is_first_burst_length = handoff->idh_first_burst_length;
1429 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C)
1430 ic->ic_header_crc32c = true;
1432 ic->ic_header_crc32c = false;
1433 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C)
1434 ic->ic_data_crc32c = true;
1436 ic->ic_data_crc32c = false;
1437 ic->ic_maxtags = maxtags;
1440 is->is_expcmdsn = 0;
1441 is->is_maxcmdsn = 0;
1442 is->is_waiting_for_iscsid = false;
1443 is->is_login_phase = false;
1445 is->is_connected = true;
1446 is->is_reason[0] = '\0';
1448 ISCSI_SESSION_UNLOCK(is);
1451 * If we're going through the proxy, the idh_socket will be 0,
1452 * and the ICL module can simply ignore this call. It can also
1453 * use it to determine it's no longer in the Login phase.
1455 error = icl_conn_handoff(ic, handoff->idh_socket);
1457 sx_sunlock(&sc->sc_lock);
1458 iscsi_session_terminate(is);
1462 sx_sunlock(&sc->sc_lock);
1464 if (is->is_sim != NULL) {
1466 * When reconnecting, there already is SIM allocated for the session.
1468 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq"));
1469 ISCSI_SESSION_LOCK(is);
1470 ISCSI_SESSION_DEBUG(is, "releasing");
1471 xpt_release_simq(is->is_sim, 1);
1472 is->is_simq_frozen = false;
1473 ISCSI_SESSION_UNLOCK(is);
1476 ISCSI_SESSION_LOCK(is);
1477 is->is_devq = cam_simq_alloc(ic->ic_maxtags);
1478 if (is->is_devq == NULL) {
1479 ISCSI_SESSION_WARN(is, "failed to allocate simq");
1480 iscsi_session_terminate(is);
1484 is->is_sim = cam_sim_alloc(iscsi_action, iscsi_poll, "iscsi",
1485 is, is->is_id /* unit */, &is->is_lock,
1486 1, ic->ic_maxtags, is->is_devq);
1487 if (is->is_sim == NULL) {
1488 ISCSI_SESSION_UNLOCK(is);
1489 ISCSI_SESSION_WARN(is, "failed to allocate SIM");
1490 cam_simq_free(is->is_devq);
1491 iscsi_session_terminate(is);
1495 error = xpt_bus_register(is->is_sim, NULL, 0);
1497 ISCSI_SESSION_UNLOCK(is);
1498 ISCSI_SESSION_WARN(is, "failed to register bus");
1499 iscsi_session_terminate(is);
1503 error = xpt_create_path(&is->is_path, /*periph*/NULL,
1504 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD,
1506 if (error != CAM_REQ_CMP) {
1507 ISCSI_SESSION_UNLOCK(is);
1508 ISCSI_SESSION_WARN(is, "failed to create path");
1509 iscsi_session_terminate(is);
1512 ISCSI_SESSION_UNLOCK(is);
1519 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc,
1520 struct iscsi_daemon_fail *fail)
1522 struct iscsi_session *is;
1524 sx_slock(&sc->sc_lock);
1526 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1527 if (is->is_id == fail->idf_session_id)
1531 sx_sunlock(&sc->sc_lock);
1534 ISCSI_SESSION_LOCK(is);
1535 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s",
1537 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason));
1538 //is->is_waiting_for_iscsid = false;
1539 //is->is_login_phase = true;
1540 //iscsi_session_reconnect(is);
1541 ISCSI_SESSION_UNLOCK(is);
1542 sx_sunlock(&sc->sc_lock);
1547 #ifdef ICL_KERNEL_PROXY
1549 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc,
1550 struct iscsi_daemon_connect *idc)
1552 struct iscsi_session *is;
1553 struct sockaddr *from_sa, *to_sa;
1556 sx_slock(&sc->sc_lock);
1557 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1558 if (is->is_id == idc->idc_session_id)
1562 sx_sunlock(&sc->sc_lock);
1565 sx_sunlock(&sc->sc_lock);
1567 if (idc->idc_from_addrlen > 0) {
1568 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen);
1570 ISCSI_SESSION_WARN(is,
1571 "getsockaddr failed with error %d", error);
1577 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen);
1579 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d",
1581 free(from_sa, M_SONAME);
1585 ISCSI_SESSION_LOCK(is);
1588 is->is_expcmdsn = 0;
1589 is->is_maxcmdsn = 0;
1590 is->is_waiting_for_iscsid = false;
1591 is->is_login_phase = true;
1593 ISCSI_SESSION_UNLOCK(is);
1595 error = icl_conn_connect(is->is_conn, idc->idc_domain,
1596 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa);
1597 free(from_sa, M_SONAME);
1598 free(to_sa, M_SONAME);
1601 * Digests are always disabled during login phase.
1603 is->is_conn->ic_header_crc32c = false;
1604 is->is_conn->ic_data_crc32c = false;
1610 iscsi_ioctl_daemon_send(struct iscsi_softc *sc,
1611 struct iscsi_daemon_send *ids)
1613 struct iscsi_session *is;
1619 sx_slock(&sc->sc_lock);
1620 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1621 if (is->is_id == ids->ids_session_id)
1625 sx_sunlock(&sc->sc_lock);
1628 sx_sunlock(&sc->sc_lock);
1630 if (is->is_login_phase == false)
1633 if (is->is_terminating || is->is_reconnecting)
1636 datalen = ids->ids_data_segment_len;
1637 if (datalen > ISCSI_MAX_DATA_SEGMENT_LENGTH)
1640 data = malloc(datalen, M_ISCSI, M_WAITOK);
1641 error = copyin(ids->ids_data_segment, data, datalen);
1643 free(data, M_ISCSI);
1648 ip = icl_pdu_new(is->is_conn, M_WAITOK);
1649 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs));
1651 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1652 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed"));
1653 free(data, M_ISCSI);
1655 iscsi_pdu_queue(ip);
1661 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc,
1662 struct iscsi_daemon_receive *idr)
1664 struct iscsi_session *is;
1669 sx_slock(&sc->sc_lock);
1670 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1671 if (is->is_id == idr->idr_session_id)
1675 sx_sunlock(&sc->sc_lock);
1678 sx_sunlock(&sc->sc_lock);
1680 if (is->is_login_phase == false)
1683 ISCSI_SESSION_LOCK(is);
1684 while (is->is_login_pdu == NULL &&
1685 is->is_terminating == false &&
1686 is->is_reconnecting == false) {
1687 error = cv_wait_sig(&is->is_login_cv, &is->is_lock);
1689 ISCSI_SESSION_UNLOCK(is);
1693 if (is->is_terminating || is->is_reconnecting) {
1694 ISCSI_SESSION_UNLOCK(is);
1697 ip = is->is_login_pdu;
1698 is->is_login_pdu = NULL;
1699 ISCSI_SESSION_UNLOCK(is);
1701 if (ip->ip_data_len > idr->idr_data_segment_len) {
1706 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs));
1707 if (ip->ip_data_len > 0) {
1708 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK);
1709 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1710 copyout(data, idr->idr_data_segment, ip->ip_data_len);
1711 free(data, M_ISCSI);
1718 #endif /* ICL_KERNEL_PROXY */
1721 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc)
1724 * Just make sure all the fields are null-terminated.
1726 * XXX: This is not particularly secure. We should
1727 * create our own conf and then copy in relevant
1730 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0';
1731 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0';
1732 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0';
1733 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0';
1734 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0';
1735 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0';
1736 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0';
1737 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0';
1738 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0';
1742 iscsi_valid_session_conf(const struct iscsi_session_conf *isc)
1745 if (isc->isc_initiator[0] == '\0') {
1746 ISCSI_DEBUG("empty isc_initiator");
1750 if (isc->isc_target_addr[0] == '\0') {
1751 ISCSI_DEBUG("empty isc_target_addr");
1755 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) {
1756 ISCSI_DEBUG("non-empty isc_target for discovery session");
1760 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) {
1761 ISCSI_DEBUG("empty isc_target for non-discovery session");
1769 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa)
1771 struct iscsi_session *is;
1772 const struct iscsi_session *is2;
1775 iscsi_sanitize_session_conf(&isa->isa_conf);
1776 if (iscsi_valid_session_conf(&isa->isa_conf) == false)
1779 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK);
1780 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf));
1782 sx_xlock(&sc->sc_lock);
1785 * Prevent duplicates.
1787 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
1788 if (!!is->is_conf.isc_discovery !=
1789 !!is2->is_conf.isc_discovery)
1792 if (strcmp(is->is_conf.isc_target_addr,
1793 is2->is_conf.isc_target_addr) != 0)
1796 if (is->is_conf.isc_discovery == 0 &&
1797 strcmp(is->is_conf.isc_target,
1798 is2->is_conf.isc_target) != 0)
1801 sx_xunlock(&sc->sc_lock);
1806 is->is_conn = icl_new_conn(is->is_conf.isc_offload,
1807 is->is_conf.isc_iser, "iscsi", &is->is_lock);
1808 if (is->is_conn == NULL) {
1809 sx_xunlock(&sc->sc_lock);
1813 is->is_conn->ic_receive = iscsi_receive_callback;
1814 is->is_conn->ic_error = iscsi_error_callback;
1815 is->is_conn->ic_prv0 = is;
1816 TAILQ_INIT(&is->is_outstanding);
1817 STAILQ_INIT(&is->is_postponed);
1818 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF);
1819 cv_init(&is->is_maintenance_cv, "iscsi_mt");
1820 #ifdef ICL_KERNEL_PROXY
1821 cv_init(&is->is_login_cv, "iscsi_login");
1825 sc->sc_last_session_id++;
1826 is->is_id = sc->sc_last_session_id;
1827 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */
1828 arc4rand(&is->is_isid[1], 5, 0);
1830 callout_init(&is->is_callout, 1);
1832 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt");
1834 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error);
1835 sx_xunlock(&sc->sc_lock);
1839 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is);
1840 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next);
1842 ISCSI_SESSION_LOCK(is);
1844 * Don't notify iscsid(8) if the session is disabled and it's not
1845 * a discovery session,
1847 if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
1848 ISCSI_SESSION_UNLOCK(is);
1849 sx_xunlock(&sc->sc_lock);
1853 is->is_waiting_for_iscsid = true;
1854 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
1855 ISCSI_SESSION_UNLOCK(is);
1856 cv_signal(&sc->sc_cv);
1857 sx_xunlock(&sc->sc_lock);
1862 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1,
1863 unsigned int id2, const struct iscsi_session_conf *c2)
1866 if (id2 != 0 && id2 != id1)
1868 if (c2->isc_target[0] != '\0' &&
1869 strcmp(c1->isc_target, c2->isc_target) != 0)
1871 if (c2->isc_target_addr[0] != '\0' &&
1872 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0)
1878 iscsi_ioctl_session_remove(struct iscsi_softc *sc,
1879 struct iscsi_session_remove *isr)
1881 struct iscsi_session *is, *tmp;
1884 iscsi_sanitize_session_conf(&isr->isr_conf);
1886 sx_xlock(&sc->sc_lock);
1887 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) {
1888 ISCSI_SESSION_LOCK(is);
1889 if (iscsi_session_conf_matches(is->is_id, &is->is_conf,
1890 isr->isr_session_id, &isr->isr_conf)) {
1892 iscsi_session_logout(is);
1893 iscsi_session_terminate(is);
1895 ISCSI_SESSION_UNLOCK(is);
1897 sx_xunlock(&sc->sc_lock);
1906 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl)
1910 struct iscsi_session *is;
1911 struct iscsi_session_state iss;
1913 sx_slock(&sc->sc_lock);
1914 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1915 if (i >= isl->isl_nentries) {
1916 sx_sunlock(&sc->sc_lock);
1919 memset(&iss, 0, sizeof(iss));
1920 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf));
1921 iss.iss_id = is->is_id;
1922 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias));
1923 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason));
1924 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload));
1926 if (is->is_conn->ic_header_crc32c)
1927 iss.iss_header_digest = ISCSI_DIGEST_CRC32C;
1929 iss.iss_header_digest = ISCSI_DIGEST_NONE;
1931 if (is->is_conn->ic_data_crc32c)
1932 iss.iss_data_digest = ISCSI_DIGEST_CRC32C;
1934 iss.iss_data_digest = ISCSI_DIGEST_NONE;
1936 iss.iss_max_data_segment_length = is->is_max_data_segment_length;
1937 iss.iss_max_burst_length = is->is_max_burst_length;
1938 iss.iss_first_burst_length = is->is_first_burst_length;
1939 iss.iss_immediate_data = is->is_immediate_data;
1940 iss.iss_connected = is->is_connected;
1942 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss));
1944 sx_sunlock(&sc->sc_lock);
1949 sx_sunlock(&sc->sc_lock);
1951 isl->isl_nentries = i;
1957 iscsi_ioctl_session_modify(struct iscsi_softc *sc,
1958 struct iscsi_session_modify *ism)
1960 struct iscsi_session *is;
1962 iscsi_sanitize_session_conf(&ism->ism_conf);
1963 if (iscsi_valid_session_conf(&ism->ism_conf) == false)
1966 sx_xlock(&sc->sc_lock);
1967 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1968 ISCSI_SESSION_LOCK(is);
1969 if (is->is_id == ism->ism_session_id)
1971 ISCSI_SESSION_UNLOCK(is);
1974 sx_xunlock(&sc->sc_lock);
1977 sx_xunlock(&sc->sc_lock);
1979 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf));
1980 ISCSI_SESSION_UNLOCK(is);
1982 iscsi_session_reconnect(is);
1988 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
1991 struct iscsi_softc *sc;
1997 return (iscsi_ioctl_daemon_wait(sc,
1998 (struct iscsi_daemon_request *)arg));
2000 return (iscsi_ioctl_daemon_handoff(sc,
2001 (struct iscsi_daemon_handoff *)arg));
2003 return (iscsi_ioctl_daemon_fail(sc,
2004 (struct iscsi_daemon_fail *)arg));
2005 #ifdef ICL_KERNEL_PROXY
2007 return (iscsi_ioctl_daemon_connect(sc,
2008 (struct iscsi_daemon_connect *)arg));
2010 return (iscsi_ioctl_daemon_send(sc,
2011 (struct iscsi_daemon_send *)arg));
2013 return (iscsi_ioctl_daemon_receive(sc,
2014 (struct iscsi_daemon_receive *)arg));
2015 #endif /* ICL_KERNEL_PROXY */
2017 return (iscsi_ioctl_session_add(sc,
2018 (struct iscsi_session_add *)arg));
2020 return (iscsi_ioctl_session_remove(sc,
2021 (struct iscsi_session_remove *)arg));
2023 return (iscsi_ioctl_session_list(sc,
2024 (struct iscsi_session_list *)arg));
2026 return (iscsi_ioctl_session_modify(sc,
2027 (struct iscsi_session_modify *)arg));
2033 static struct iscsi_outstanding *
2034 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag)
2036 struct iscsi_outstanding *io;
2038 ISCSI_SESSION_LOCK_ASSERT(is);
2040 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2041 if (io->io_initiator_task_tag == initiator_task_tag)
2047 static struct iscsi_outstanding *
2048 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb)
2050 struct iscsi_outstanding *io;
2052 ISCSI_SESSION_LOCK_ASSERT(is);
2054 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2055 if (io->io_ccb == ccb)
2061 static struct iscsi_outstanding *
2062 iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request,
2063 union ccb *ccb, uint32_t *initiator_task_tagp)
2065 struct iscsi_outstanding *io;
2068 ISCSI_SESSION_LOCK_ASSERT(is);
2070 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO);
2072 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes",
2077 error = icl_conn_task_setup(is->is_conn, request, &ccb->csio,
2078 initiator_task_tagp, &io->io_icl_prv);
2080 ISCSI_SESSION_WARN(is,
2081 "icl_conn_task_setup() failed with error %d", error);
2082 uma_zfree(iscsi_outstanding_zone, io);
2086 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL,
2087 ("initiator_task_tag 0x%x already added", *initiator_task_tagp));
2089 io->io_initiator_task_tag = *initiator_task_tagp;
2091 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next);
2096 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io)
2099 ISCSI_SESSION_LOCK_ASSERT(is);
2101 icl_conn_task_done(is->is_conn, io->io_icl_prv);
2102 TAILQ_REMOVE(&is->is_outstanding, io, io_next);
2103 uma_zfree(iscsi_outstanding_zone, io);
2107 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb)
2109 struct icl_pdu *request;
2110 struct iscsi_bhs_task_management_request *bhstmr;
2111 struct ccb_abort *cab = &ccb->cab;
2112 struct iscsi_outstanding *io, *aio;
2113 uint32_t initiator_task_tag;
2115 ISCSI_SESSION_LOCK_ASSERT(is);
2118 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2120 if (is->is_login_phase) {
2121 ccb->ccb_h.status = CAM_REQ_ABORTED;
2127 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb);
2129 ccb->ccb_h.status = CAM_REQ_CMP;
2134 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2135 if (request == NULL) {
2136 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2141 initiator_task_tag = is->is_initiator_task_tag++;
2143 io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag);
2145 icl_pdu_free(request);
2146 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2150 io->io_datasn = aio->io_initiator_task_tag;
2152 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2153 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST;
2154 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK;
2155 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2156 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag;
2157 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag;
2159 iscsi_pdu_queue_locked(request);
2163 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb)
2165 struct icl_pdu *request;
2166 struct iscsi_bhs_scsi_command *bhssc;
2167 struct ccb_scsiio *csio;
2168 struct iscsi_outstanding *io;
2170 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 ISCSI_SESSION_DEBUG(is, "called during login phase");
2180 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2181 xpt_freeze_devq(ccb->ccb_h.path, 1);
2182 ISCSI_SESSION_DEBUG(is, "freezing devq");
2184 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN;
2190 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2191 if (request == NULL) {
2192 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2193 xpt_freeze_devq(ccb->ccb_h.path, 1);
2194 ISCSI_SESSION_DEBUG(is, "freezing devq");
2196 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2201 initiator_task_tag = is->is_initiator_task_tag++;
2202 io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag);
2204 icl_pdu_free(request);
2205 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2206 xpt_freeze_devq(ccb->ccb_h.path, 1);
2207 ISCSI_SESSION_DEBUG(is, "freezing devq");
2209 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2215 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2216 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND;
2217 bhssc->bhssc_flags |= BHSSC_FLAGS_F;
2218 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
2220 bhssc->bhssc_flags |= BHSSC_FLAGS_R;
2223 bhssc->bhssc_flags |= BHSSC_FLAGS_W;
2227 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2228 switch (csio->tag_action) {
2229 case MSG_HEAD_OF_Q_TAG:
2230 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ;
2232 case MSG_ORDERED_Q_TAG:
2233 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED;
2236 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA;
2238 case MSG_SIMPLE_Q_TAG:
2240 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE;
2244 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED;
2246 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2247 bhssc->bhssc_initiator_task_tag = initiator_task_tag;
2248 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len);
2249 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb),
2250 ("unsupported CDB size %zd", (size_t)csio->cdb_len));
2252 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2253 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len);
2255 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len);
2257 if (is->is_immediate_data &&
2258 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2259 len = csio->dxfer_len;
2260 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len);
2261 if (len > is->is_first_burst_length) {
2262 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_first_burst_length);
2263 len = is->is_first_burst_length;
2265 if (len > is->is_max_data_segment_length) {
2266 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_max_data_segment_length);
2267 len = is->is_max_data_segment_length;
2270 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT);
2272 iscsi_outstanding_remove(is, io);
2273 icl_pdu_free(request);
2274 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2275 xpt_freeze_devq(ccb->ccb_h.path, 1);
2276 ISCSI_SESSION_DEBUG(is, "freezing devq");
2278 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2283 iscsi_pdu_queue_locked(request);
2287 iscsi_action(struct cam_sim *sim, union ccb *ccb)
2289 struct iscsi_session *is;
2291 is = cam_sim_softc(sim);
2293 ISCSI_SESSION_LOCK_ASSERT(is);
2295 if (is->is_terminating ||
2296 (is->is_connected == false && fail_on_disconnection)) {
2297 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2302 switch (ccb->ccb_h.func_code) {
2305 struct ccb_pathinq *cpi = &ccb->cpi;
2307 cpi->version_num = 1;
2308 cpi->hba_inquiry = PI_TAG_ABLE;
2309 cpi->target_sprt = 0;
2310 cpi->hba_misc = PIM_EXTLUNS;
2312 * XXX: It shouldn't ever be NULL; this could be turned
2313 * into a KASSERT eventually.
2315 if (is->is_conn == NULL)
2316 ISCSI_WARN("NULL conn");
2317 else if (is->is_conn->ic_unmapped)
2318 cpi->hba_misc |= PIM_UNMAPPED;
2319 cpi->hba_eng_cnt = 0;
2320 cpi->max_target = 0;
2322 * Note that the variable below is only relevant for targets
2323 * that don't claim compliance with anything above SPC2, which
2324 * means they don't support REPORT_LUNS.
2327 cpi->initiator_id = ~0;
2328 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2329 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
2330 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2331 cpi->unit_number = cam_sim_unit(sim);
2332 cpi->bus_id = cam_sim_bus(sim);
2333 cpi->base_transfer_speed = 150000; /* XXX */
2334 cpi->transport = XPORT_ISCSI;
2335 cpi->transport_version = 0;
2336 cpi->protocol = PROTO_SCSI;
2337 cpi->protocol_version = SCSI_REV_SPC3;
2338 cpi->maxio = MAXPHYS;
2339 cpi->ccb_h.status = CAM_REQ_CMP;
2342 case XPT_GET_TRAN_SETTINGS:
2344 struct ccb_trans_settings *cts;
2345 struct ccb_trans_settings_scsi *scsi;
2348 scsi = &cts->proto_specific.scsi;
2350 cts->protocol = PROTO_SCSI;
2351 cts->protocol_version = SCSI_REV_SPC3;
2352 cts->transport = XPORT_ISCSI;
2353 cts->transport_version = 0;
2354 scsi->valid = CTS_SCSI_VALID_TQ;
2355 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2356 cts->ccb_h.status = CAM_REQ_CMP;
2359 case XPT_CALC_GEOMETRY:
2360 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2361 ccb->ccb_h.status = CAM_REQ_CMP;
2365 * XXX: What's the point?
2369 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io");
2370 ccb->ccb_h.status = CAM_REQ_CMP;
2374 iscsi_action_abort(is, ccb);
2377 iscsi_action_scsiio(is, ccb);
2381 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code);
2383 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2390 iscsi_poll(struct cam_sim *sim)
2393 KASSERT(0, ("%s: you're not supposed to be here", __func__));
2397 iscsi_terminate_sessions(struct iscsi_softc *sc)
2399 struct iscsi_session *is;
2401 sx_slock(&sc->sc_lock);
2402 TAILQ_FOREACH(is, &sc->sc_sessions, is_next)
2403 iscsi_session_terminate(is);
2404 while(!TAILQ_EMPTY(&sc->sc_sessions)) {
2405 ISCSI_DEBUG("waiting for sessions to terminate");
2406 cv_wait(&sc->sc_cv, &sc->sc_lock);
2408 ISCSI_DEBUG("all sessions terminated");
2409 sx_sunlock(&sc->sc_lock);
2413 iscsi_shutdown_pre(struct iscsi_softc *sc)
2415 struct iscsi_session *is;
2417 if (!fail_on_shutdown)
2421 * If we have any sessions waiting for reconnection, request
2422 * maintenance thread to fail them immediately instead of waiting
2423 * for reconnect timeout.
2425 * This prevents LUNs with mounted filesystems that are supported
2426 * by disconnected iSCSI sessions from hanging, however it will
2427 * fail all queued BIOs.
2429 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown");
2431 fail_on_disconnection = 1;
2433 sx_slock(&sc->sc_lock);
2434 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2435 ISCSI_SESSION_LOCK(is);
2436 if (!is->is_connected) {
2437 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early");
2438 iscsi_session_reconnect(is);
2440 ISCSI_SESSION_UNLOCK(is);
2442 sx_sunlock(&sc->sc_lock);
2446 iscsi_shutdown_post(struct iscsi_softc *sc)
2449 ISCSI_DEBUG("removing all sessions due to shutdown");
2450 iscsi_terminate_sessions(sc);
2458 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK);
2459 sx_init(&sc->sc_lock, "iscsi");
2460 TAILQ_INIT(&sc->sc_sessions);
2461 cv_init(&sc->sc_cv, "iscsi_cv");
2463 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding",
2464 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL,
2467 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw,
2468 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
2470 ISCSI_WARN("failed to create device node, error %d", error);
2473 sc->sc_cdev->si_drv1 = sc;
2475 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2476 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST);
2478 * shutdown_post_sync needs to run after filesystem shutdown and before
2479 * CAM shutdown - otherwise when rebooting with an iSCSI session that is
2480 * disconnected but has outstanding requests, dashutdown() will hang on
2481 * cam_periph_runccb().
2483 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
2484 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1);
2493 if (sc->sc_cdev != NULL) {
2494 ISCSI_DEBUG("removing device node");
2495 destroy_dev(sc->sc_cdev);
2496 ISCSI_DEBUG("device node removed");
2499 if (sc->sc_shutdown_pre_eh != NULL)
2500 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh);
2501 if (sc->sc_shutdown_post_eh != NULL)
2502 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh);
2504 iscsi_terminate_sessions(sc);
2506 uma_zdestroy(iscsi_outstanding_zone);
2507 sx_destroy(&sc->sc_lock);
2508 cv_destroy(&sc->sc_cv);
2516 sx_slock(&sc->sc_lock);
2517 if (!TAILQ_EMPTY(&sc->sc_sessions)) {
2518 sx_sunlock(&sc->sc_lock);
2521 sx_sunlock(&sc->sc_lock);
2526 iscsi_modevent(module_t mod, int what, void *arg)
2532 error = iscsi_load();
2535 error = iscsi_unload();
2538 error = iscsi_quiesce();
2547 moduledata_t iscsi_data = {
2553 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2554 MODULE_DEPEND(iscsi, cam, 1, 1, 1);
2555 MODULE_DEPEND(iscsi, icl, 1, 1, 1);