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/sysctl.h>
47 #include <sys/systm.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/cam_debug.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_periph.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
62 #include <dev/iscsi/icl.h>
63 #include <dev/iscsi/icl_wrappers.h>
64 #include <dev/iscsi/iscsi_ioctl.h>
65 #include <dev/iscsi/iscsi_proto.h>
66 #include <dev/iscsi/iscsi.h>
68 #ifdef ICL_KERNEL_PROXY
69 #include <sys/socketvar.h>
72 #ifdef ICL_KERNEL_PROXY
73 FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY");
77 * XXX: This is global so the iscsi_unload() can access it.
78 * Think about how to do this properly.
80 static struct iscsi_softc *sc;
82 SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD, 0, "iSCSI initiator");
84 SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
85 &debug, 0, "Enable debug messages");
86 static int ping_timeout = 5;
87 SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout,
88 0, "Timeout for ping (NOP-Out) requests, in seconds");
89 static int iscsid_timeout = 60;
90 SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout,
91 0, "Time to wait for iscsid(8) to handle reconnection, in seconds");
92 static int login_timeout = 60;
93 SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout,
94 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds");
95 static int maxtags = 255;
96 SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags,
97 0, "Max number of IO requests queued");
98 static int fail_on_disconnection = 0;
99 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN,
100 &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure");
101 static int fail_on_shutdown = 1;
102 SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_shutdown, CTLFLAG_RWTUN,
103 &fail_on_shutdown, 0, "Fail disconnected sessions on shutdown");
105 static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator");
106 static uma_zone_t iscsi_outstanding_zone;
108 #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0)
109 #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn))
111 #define ISCSI_DEBUG(X, ...) \
114 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
117 #define ISCSI_WARN(X, ...) \
120 printf("WARNING: %s: " X "\n", \
121 __func__, ## __VA_ARGS__); \
125 #define ISCSI_SESSION_DEBUG(S, X, ...) \
128 printf("%s: %s (%s): " X "\n", \
129 __func__, S->is_conf.isc_target_addr, \
130 S->is_conf.isc_target, ## __VA_ARGS__); \
134 #define ISCSI_SESSION_WARN(S, X, ...) \
137 printf("WARNING: %s (%s): " X "\n", \
138 S->is_conf.isc_target_addr, \
139 S->is_conf.isc_target, ## __VA_ARGS__); \
143 #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock)
144 #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock)
145 #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED)
146 #define ISCSI_SESSION_LOCK_ASSERT_NOT(X) mtx_assert(&X->is_lock, MA_NOTOWNED)
148 static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
149 int mode, struct thread *td);
151 static struct cdevsw iscsi_cdevsw = {
152 .d_version = D_VERSION,
153 .d_ioctl = iscsi_ioctl,
157 static void iscsi_pdu_queue_locked(struct icl_pdu *request);
158 static void iscsi_pdu_queue(struct icl_pdu *request);
159 static void iscsi_pdu_update_statsn(const struct icl_pdu *response);
160 static void iscsi_pdu_handle_nop_in(struct icl_pdu *response);
161 static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response);
162 static void iscsi_pdu_handle_task_response(struct icl_pdu *response);
163 static void iscsi_pdu_handle_data_in(struct icl_pdu *response);
164 static void iscsi_pdu_handle_logout_response(struct icl_pdu *response);
165 static void iscsi_pdu_handle_r2t(struct icl_pdu *response);
166 static void iscsi_pdu_handle_async_message(struct icl_pdu *response);
167 static void iscsi_pdu_handle_reject(struct icl_pdu *response);
168 static void iscsi_session_reconnect(struct iscsi_session *is);
169 static void iscsi_session_terminate(struct iscsi_session *is);
170 static void iscsi_action(struct cam_sim *sim, union ccb *ccb);
171 static void iscsi_poll(struct cam_sim *sim);
172 static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is,
173 uint32_t initiator_task_tag);
174 static struct iscsi_outstanding *iscsi_outstanding_add(struct iscsi_session *is,
175 union ccb *ccb, uint32_t *initiator_task_tagp);
176 static void iscsi_outstanding_remove(struct iscsi_session *is,
177 struct iscsi_outstanding *io);
180 iscsi_pdu_prepare(struct icl_pdu *request)
182 struct iscsi_session *is;
183 struct iscsi_bhs_scsi_command *bhssc;
185 is = PDU_SESSION(request);
187 ISCSI_SESSION_LOCK_ASSERT(is);
190 * We're only using fields common for all the request
191 * (initiator -> target) PDUs.
193 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
196 * Data-Out PDU does not contain CmdSN.
198 if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
199 if (ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn) &&
200 (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
202 * Current MaxCmdSN prevents us from sending any more
203 * SCSI Command PDUs to the target; postpone the PDU.
204 * It will get resent by either iscsi_pdu_queue(),
205 * or by maintenance thread.
208 ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %u, "
209 "ExpCmdSN %u, MaxCmdSN %u, opcode 0x%x",
210 is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn,
211 bhssc->bhssc_opcode);
215 bhssc->bhssc_cmdsn = htonl(is->is_cmdsn);
216 if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
219 bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1);
225 iscsi_session_send_postponed(struct iscsi_session *is)
227 struct icl_pdu *request;
230 ISCSI_SESSION_LOCK_ASSERT(is);
232 while (!STAILQ_EMPTY(&is->is_postponed)) {
233 request = STAILQ_FIRST(&is->is_postponed);
234 postpone = iscsi_pdu_prepare(request);
237 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
238 icl_pdu_queue(request);
243 iscsi_pdu_queue_locked(struct icl_pdu *request)
245 struct iscsi_session *is;
248 is = PDU_SESSION(request);
249 ISCSI_SESSION_LOCK_ASSERT(is);
250 iscsi_session_send_postponed(is);
251 postpone = iscsi_pdu_prepare(request);
253 STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next);
256 icl_pdu_queue(request);
260 iscsi_pdu_queue(struct icl_pdu *request)
262 struct iscsi_session *is;
264 is = PDU_SESSION(request);
265 ISCSI_SESSION_LOCK(is);
266 iscsi_pdu_queue_locked(request);
267 ISCSI_SESSION_UNLOCK(is);
271 iscsi_session_logout(struct iscsi_session *is)
273 struct icl_pdu *request;
274 struct iscsi_bhs_logout_request *bhslr;
276 request = icl_pdu_new(is->is_conn, M_NOWAIT);
280 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
281 bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST;
282 bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
283 iscsi_pdu_queue_locked(request);
287 iscsi_session_terminate_task(struct iscsi_session *is,
288 struct iscsi_outstanding *io, bool requeue)
291 if (io->io_ccb != NULL) {
292 io->io_ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
294 io->io_ccb->ccb_h.status |= CAM_REQUEUE_REQ;
296 io->io_ccb->ccb_h.status |= CAM_REQ_ABORTED;
297 if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
298 io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN;
299 xpt_freeze_devq(io->io_ccb->ccb_h.path, 1);
300 ISCSI_SESSION_DEBUG(is, "freezing devq");
302 xpt_done(io->io_ccb);
304 iscsi_outstanding_remove(is, io);
308 iscsi_session_terminate_tasks(struct iscsi_session *is, bool requeue)
310 struct iscsi_outstanding *io, *tmp;
312 ISCSI_SESSION_LOCK_ASSERT(is);
314 TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) {
315 iscsi_session_terminate_task(is, io, requeue);
320 iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim)
324 ISCSI_SESSION_LOCK_ASSERT(is);
327 * Don't queue any new PDUs.
329 if (is->is_sim != NULL && is->is_simq_frozen == false) {
330 ISCSI_SESSION_DEBUG(is, "freezing");
331 xpt_freeze_simq(is->is_sim, 1);
332 is->is_simq_frozen = true;
336 * Remove postponed PDUs.
338 while (!STAILQ_EMPTY(&is->is_postponed)) {
339 pdu = STAILQ_FIRST(&is->is_postponed);
340 STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
344 if (destroy_sim == false) {
346 * Terminate SCSI tasks, asking CAM to requeue them.
348 iscsi_session_terminate_tasks(is, true);
352 iscsi_session_terminate_tasks(is, false);
354 if (is->is_sim == NULL)
357 ISCSI_SESSION_DEBUG(is, "deregistering SIM");
358 xpt_async(AC_LOST_DEVICE, is->is_path, NULL);
360 if (is->is_simq_frozen) {
361 xpt_release_simq(is->is_sim, 1);
362 is->is_simq_frozen = false;
365 xpt_free_path(is->is_path);
367 xpt_bus_deregister(cam_sim_path(is->is_sim));
368 cam_sim_free(is->is_sim, TRUE /*free_devq*/);
374 iscsi_maintenance_thread_reconnect(struct iscsi_session *is)
377 icl_conn_close(is->is_conn);
379 ISCSI_SESSION_LOCK(is);
381 is->is_connected = false;
382 is->is_reconnecting = false;
383 is->is_login_phase = false;
385 #ifdef ICL_KERNEL_PROXY
386 if (is->is_login_pdu != NULL) {
387 icl_pdu_free(is->is_login_pdu);
388 is->is_login_pdu = NULL;
390 cv_signal(&is->is_login_cv);
393 if (fail_on_disconnection) {
394 ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices");
395 iscsi_session_cleanup(is, true);
397 iscsi_session_cleanup(is, false);
400 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
401 ("destroying session with active tasks"));
402 KASSERT(STAILQ_EMPTY(&is->is_postponed),
403 ("destroying session with postponed PDUs"));
406 * Request immediate reconnection from iscsid(8).
408 //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)");
409 is->is_waiting_for_iscsid = true;
410 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
412 ISCSI_SESSION_UNLOCK(is);
413 cv_signal(&is->is_softc->sc_cv);
417 iscsi_maintenance_thread_terminate(struct iscsi_session *is)
419 struct iscsi_softc *sc;
422 sx_xlock(&sc->sc_lock);
424 icl_conn_close(is->is_conn);
425 callout_drain(&is->is_callout);
427 ISCSI_SESSION_LOCK(is);
429 KASSERT(is->is_terminating, ("is_terminating == false"));
431 #ifdef ICL_KERNEL_PROXY
432 if (is->is_login_pdu != NULL) {
433 icl_pdu_free(is->is_login_pdu);
434 is->is_login_pdu = NULL;
436 cv_signal(&is->is_login_cv);
439 iscsi_session_cleanup(is, true);
441 KASSERT(TAILQ_EMPTY(&is->is_outstanding),
442 ("destroying session with active tasks"));
443 KASSERT(STAILQ_EMPTY(&is->is_postponed),
444 ("destroying session with postponed PDUs"));
446 ISCSI_SESSION_UNLOCK(is);
448 icl_conn_free(is->is_conn);
449 mtx_destroy(&is->is_lock);
450 cv_destroy(&is->is_maintenance_cv);
451 #ifdef ICL_KERNEL_PROXY
452 cv_destroy(&is->is_login_cv);
454 TAILQ_REMOVE(&sc->sc_sessions, is, is_next);
455 sx_xunlock(&sc->sc_lock);
457 ISCSI_SESSION_DEBUG(is, "terminated");
461 * The iscsi_unload() routine might be waiting.
463 cv_signal(&sc->sc_cv);
467 iscsi_maintenance_thread(void *arg)
469 struct iscsi_session *is;
474 ISCSI_SESSION_LOCK(is);
475 if (is->is_reconnecting == false &&
476 is->is_terminating == false &&
477 STAILQ_EMPTY(&is->is_postponed))
478 cv_wait(&is->is_maintenance_cv, &is->is_lock);
480 /* Terminate supersedes reconnect. */
481 if (is->is_terminating) {
482 ISCSI_SESSION_UNLOCK(is);
483 iscsi_maintenance_thread_terminate(is);
488 if (is->is_reconnecting) {
489 ISCSI_SESSION_UNLOCK(is);
490 iscsi_maintenance_thread_reconnect(is);
494 iscsi_session_send_postponed(is);
495 ISCSI_SESSION_UNLOCK(is);
500 iscsi_session_reconnect(struct iscsi_session *is)
504 * XXX: We can't use locking here, because
505 * it's being called from various contexts.
506 * Hope it doesn't break anything.
508 if (is->is_reconnecting)
511 is->is_reconnecting = true;
512 cv_signal(&is->is_maintenance_cv);
516 iscsi_session_terminate(struct iscsi_session *is)
519 if (is->is_terminating)
522 is->is_terminating = true;
525 iscsi_session_logout(is);
527 cv_signal(&is->is_maintenance_cv);
531 iscsi_callout(void *context)
533 struct icl_pdu *request;
534 struct iscsi_bhs_nop_out *bhsno;
535 struct iscsi_session *is;
536 bool reconnect_needed = false;
540 ISCSI_SESSION_LOCK(is);
541 if (is->is_terminating) {
542 ISCSI_SESSION_UNLOCK(is);
546 callout_schedule(&is->is_callout, 1 * hz);
550 if (is->is_waiting_for_iscsid) {
551 if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) {
552 ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) "
553 "for %d seconds; reconnecting",
555 reconnect_needed = true;
560 if (is->is_login_phase) {
561 if (login_timeout > 0 && is->is_timeout > login_timeout) {
562 ISCSI_SESSION_WARN(is, "login timed out after %d seconds; "
563 "reconnecting", is->is_timeout);
564 reconnect_needed = true;
569 if (ping_timeout <= 0) {
571 * Pings are disabled. Don't send NOP-Out in this case.
572 * Reset the timeout, to avoid triggering reconnection,
573 * should the user decide to reenable them.
579 if (is->is_timeout >= ping_timeout) {
580 ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; "
581 "reconnecting", ping_timeout);
582 reconnect_needed = true;
586 ISCSI_SESSION_UNLOCK(is);
589 * If the ping was reset less than one second ago - which means
590 * that we've received some PDU during the last second - assume
591 * the traffic flows correctly and don't bother sending a NOP-Out.
593 * (It's 2 - one for one second, and one for incrementing is_timeout
594 * earlier in this routine.)
596 if (is->is_timeout < 2)
599 request = icl_pdu_new(is->is_conn, M_NOWAIT);
600 if (request == NULL) {
601 ISCSI_SESSION_WARN(is, "failed to allocate PDU");
604 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
605 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
606 ISCSI_BHS_OPCODE_IMMEDIATE;
607 bhsno->bhsno_flags = 0x80;
608 bhsno->bhsno_target_transfer_tag = 0xffffffff;
609 iscsi_pdu_queue(request);
613 if (is->is_terminating) {
614 ISCSI_SESSION_UNLOCK(is);
618 ISCSI_SESSION_UNLOCK(is);
620 if (reconnect_needed)
621 iscsi_session_reconnect(is);
625 iscsi_pdu_update_statsn(const struct icl_pdu *response)
627 const struct iscsi_bhs_data_in *bhsdi;
628 struct iscsi_session *is;
629 uint32_t expcmdsn, maxcmdsn, statsn;
631 is = PDU_SESSION(response);
633 ISCSI_SESSION_LOCK_ASSERT(is);
636 * We're only using fields common for all the response
637 * (target -> initiator) PDUs.
639 bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs;
641 * Ok, I lied. In case of Data-In, "The fields StatSN, Status,
642 * and Residual Count only have meaningful content if the S bit
643 * is set to 1", so we also need to check the bit specific for
646 if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
647 (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) {
648 statsn = ntohl(bhsdi->bhsdi_statsn);
649 if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) {
650 /* XXX: This is normal situation for MCS */
651 ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != "
652 "session ExpStatSN %u (or + 1); reconnecting",
653 bhsdi->bhsdi_opcode, statsn, is->is_statsn);
654 iscsi_session_reconnect(is);
656 if (ISCSI_SNGT(statsn, is->is_statsn))
657 is->is_statsn = statsn;
660 expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn);
661 maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn);
663 if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) {
664 ISCSI_SESSION_DEBUG(is,
665 "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring",
668 if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) {
669 is->is_maxcmdsn = maxcmdsn;
672 * Command window increased; kick the maintanance thread
673 * to send out postponed commands.
675 if (!STAILQ_EMPTY(&is->is_postponed))
676 cv_signal(&is->is_maintenance_cv);
677 } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) {
678 /* XXX: This is normal situation for MCS */
679 ISCSI_SESSION_DEBUG(is,
680 "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring",
681 maxcmdsn, is->is_maxcmdsn);
684 if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) {
685 is->is_expcmdsn = expcmdsn;
686 } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) {
687 /* XXX: This is normal situation for MCS */
688 ISCSI_SESSION_DEBUG(is,
689 "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring",
690 expcmdsn, is->is_expcmdsn);
695 * Every incoming PDU - not just NOP-In - resets the ping timer.
696 * The purpose of the timeout is to reset the connection when it stalls;
697 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
704 iscsi_receive_callback(struct icl_pdu *response)
706 struct iscsi_session *is;
708 is = PDU_SESSION(response);
710 ISCSI_SESSION_LOCK(is);
712 #ifdef ICL_KERNEL_PROXY
713 if (is->is_login_phase) {
714 if (is->is_login_pdu == NULL)
715 is->is_login_pdu = response;
717 icl_pdu_free(response);
718 ISCSI_SESSION_UNLOCK(is);
719 cv_signal(&is->is_login_cv);
724 iscsi_pdu_update_statsn(response);
727 * The handling routine is responsible for freeing the PDU
728 * when it's no longer needed.
730 switch (response->ip_bhs->bhs_opcode) {
731 case ISCSI_BHS_OPCODE_NOP_IN:
732 iscsi_pdu_handle_nop_in(response);
733 ISCSI_SESSION_UNLOCK(is);
735 case ISCSI_BHS_OPCODE_SCSI_RESPONSE:
736 iscsi_pdu_handle_scsi_response(response);
737 /* Session lock dropped inside. */
738 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
740 case ISCSI_BHS_OPCODE_TASK_RESPONSE:
741 iscsi_pdu_handle_task_response(response);
742 ISCSI_SESSION_UNLOCK(is);
744 case ISCSI_BHS_OPCODE_SCSI_DATA_IN:
745 iscsi_pdu_handle_data_in(response);
746 /* Session lock dropped inside. */
747 ISCSI_SESSION_LOCK_ASSERT_NOT(is);
749 case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE:
750 iscsi_pdu_handle_logout_response(response);
751 ISCSI_SESSION_UNLOCK(is);
753 case ISCSI_BHS_OPCODE_R2T:
754 iscsi_pdu_handle_r2t(response);
755 ISCSI_SESSION_UNLOCK(is);
757 case ISCSI_BHS_OPCODE_ASYNC_MESSAGE:
758 iscsi_pdu_handle_async_message(response);
759 ISCSI_SESSION_UNLOCK(is);
761 case ISCSI_BHS_OPCODE_REJECT:
762 iscsi_pdu_handle_reject(response);
763 ISCSI_SESSION_UNLOCK(is);
766 ISCSI_SESSION_WARN(is, "received PDU with unsupported "
767 "opcode 0x%x; reconnecting",
768 response->ip_bhs->bhs_opcode);
769 iscsi_session_reconnect(is);
770 ISCSI_SESSION_UNLOCK(is);
771 icl_pdu_free(response);
776 iscsi_error_callback(struct icl_conn *ic)
778 struct iscsi_session *is;
780 is = CONN_SESSION(ic);
782 ISCSI_SESSION_WARN(is, "connection error; reconnecting");
783 iscsi_session_reconnect(is);
787 iscsi_pdu_handle_nop_in(struct icl_pdu *response)
789 struct iscsi_session *is;
790 struct iscsi_bhs_nop_out *bhsno;
791 struct iscsi_bhs_nop_in *bhsni;
792 struct icl_pdu *request;
797 is = PDU_SESSION(response);
798 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
800 if (bhsni->bhsni_target_transfer_tag == 0xffffffff) {
802 * Nothing to do; iscsi_pdu_update_statsn() already
803 * zeroed the timeout.
805 icl_pdu_free(response);
809 datasize = icl_pdu_data_segment_length(response);
811 data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO);
813 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
815 icl_pdu_free(response);
816 iscsi_session_reconnect(is);
819 icl_pdu_get_data(response, 0, data, datasize);
822 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
823 if (request == NULL) {
824 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
827 icl_pdu_free(response);
828 iscsi_session_reconnect(is);
831 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
832 bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
833 ISCSI_BHS_OPCODE_IMMEDIATE;
834 bhsno->bhsno_flags = 0x80;
835 bhsno->bhsno_initiator_task_tag = 0xffffffff;
836 bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag;
838 error = icl_pdu_append_data(request, data, datasize, M_NOWAIT);
840 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
843 icl_pdu_free(request);
844 icl_pdu_free(response);
845 iscsi_session_reconnect(is);
851 icl_pdu_free(response);
852 iscsi_pdu_queue_locked(request);
856 iscsi_pdu_handle_scsi_response(struct icl_pdu *response)
858 struct iscsi_bhs_scsi_response *bhssr;
859 struct iscsi_outstanding *io;
860 struct iscsi_session *is;
862 struct ccb_scsiio *csio;
863 size_t data_segment_len, received;
866 is = PDU_SESSION(response);
868 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
869 io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag);
870 if (io == NULL || io->io_ccb == NULL) {
871 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag);
872 icl_pdu_free(response);
873 iscsi_session_reconnect(is);
874 ISCSI_SESSION_UNLOCK(is);
879 received = io->io_received;
880 iscsi_outstanding_remove(is, io);
881 ISCSI_SESSION_UNLOCK(is);
883 if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) {
884 ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response);
885 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
886 xpt_freeze_devq(ccb->ccb_h.path, 1);
887 ISCSI_SESSION_DEBUG(is, "freezing devq");
889 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
890 } else if (bhssr->bhssr_status == 0) {
891 ccb->ccb_h.status = CAM_REQ_CMP;
893 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
894 xpt_freeze_devq(ccb->ccb_h.path, 1);
895 ISCSI_SESSION_DEBUG(is, "freezing devq");
897 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
898 ccb->csio.scsi_status = bhssr->bhssr_status;
902 data_segment_len = icl_pdu_data_segment_length(response);
903 if (data_segment_len > 0) {
904 if (data_segment_len < sizeof(sense_len)) {
905 ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)",
907 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
908 xpt_freeze_devq(ccb->ccb_h.path, 1);
909 ISCSI_SESSION_DEBUG(is, "freezing devq");
911 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
914 icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len));
915 sense_len = ntohs(sense_len);
917 ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd",
918 sense_len, data_segment_len);
920 if (sizeof(sense_len) + sense_len > data_segment_len) {
921 ISCSI_SESSION_WARN(is, "truncated data segment "
922 "(%zd bytes, should be %zd)",
923 data_segment_len, sizeof(sense_len) + sense_len);
924 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
925 xpt_freeze_devq(ccb->ccb_h.path, 1);
926 ISCSI_SESSION_DEBUG(is, "freezing devq");
928 ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
930 } else if (sizeof(sense_len) + sense_len < data_segment_len)
931 ISCSI_SESSION_WARN(is, "oversize data segment "
932 "(%zd bytes, should be %zd)",
933 data_segment_len, sizeof(sense_len) + sense_len);
934 if (sense_len > csio->sense_len) {
935 ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d",
936 sense_len, csio->sense_len);
937 sense_len = csio->sense_len;
939 icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len);
940 csio->sense_resid = csio->sense_len - sense_len;
941 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
945 if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW)
946 csio->resid = ntohl(bhssr->bhssr_residual_count);
948 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
949 KASSERT(received <= csio->dxfer_len,
950 ("received > csio->dxfer_len"));
951 if (received < csio->dxfer_len) {
952 if (csio->resid != csio->dxfer_len - received) {
953 ISCSI_SESSION_WARN(is, "underflow mismatch: "
954 "target indicates %d, we calculated %zd",
955 csio->resid, csio->dxfer_len - received);
957 csio->resid = csio->dxfer_len - received;
962 icl_pdu_free(response);
966 iscsi_pdu_handle_task_response(struct icl_pdu *response)
968 struct iscsi_bhs_task_management_response *bhstmr;
969 struct iscsi_outstanding *io, *aio;
970 struct iscsi_session *is;
972 is = PDU_SESSION(response);
974 bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs;
975 io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag);
976 if (io == NULL || io->io_ccb != NULL) {
977 ISCSI_SESSION_WARN(is, "bad itt 0x%x",
978 bhstmr->bhstmr_initiator_task_tag);
979 icl_pdu_free(response);
980 iscsi_session_reconnect(is);
984 if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) {
985 ISCSI_SESSION_WARN(is, "task response 0x%x",
986 bhstmr->bhstmr_response);
988 aio = iscsi_outstanding_find(is, io->io_datasn);
989 if (aio != NULL && aio->io_ccb != NULL)
990 iscsi_session_terminate_task(is, aio, false);
993 iscsi_outstanding_remove(is, io);
994 icl_pdu_free(response);
998 iscsi_pdu_handle_data_in(struct icl_pdu *response)
1000 struct iscsi_bhs_data_in *bhsdi;
1001 struct iscsi_outstanding *io;
1002 struct iscsi_session *is;
1004 struct ccb_scsiio *csio;
1005 size_t data_segment_len, received, oreceived;
1007 is = PDU_SESSION(response);
1008 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
1009 io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag);
1010 if (io == NULL || io->io_ccb == NULL) {
1011 ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag);
1012 icl_pdu_free(response);
1013 iscsi_session_reconnect(is);
1014 ISCSI_SESSION_UNLOCK(is);
1018 data_segment_len = icl_pdu_data_segment_length(response);
1019 if (data_segment_len == 0) {
1021 * "The sending of 0 length data segments should be avoided,
1022 * but initiators and targets MUST be able to properly receive
1023 * 0 length data segments."
1025 ISCSI_SESSION_UNLOCK(is);
1026 icl_pdu_free(response);
1031 * We need to track this for security reasons - without it, malicious target
1032 * could respond to SCSI READ without sending Data-In PDUs, which would result
1033 * in read operation on the initiator side returning random kernel data.
1035 if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) {
1036 ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd",
1037 io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset));
1038 icl_pdu_free(response);
1039 iscsi_session_reconnect(is);
1040 ISCSI_SESSION_UNLOCK(is);
1047 if (io->io_received + data_segment_len > csio->dxfer_len) {
1048 ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes "
1049 "at offset %zd, buffer is %d)",
1050 data_segment_len, io->io_received, csio->dxfer_len);
1051 icl_pdu_free(response);
1052 iscsi_session_reconnect(is);
1053 ISCSI_SESSION_UNLOCK(is);
1057 oreceived = io->io_received;
1058 io->io_received += data_segment_len;
1059 received = io->io_received;
1060 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0)
1061 iscsi_outstanding_remove(is, io);
1062 ISCSI_SESSION_UNLOCK(is);
1064 icl_pdu_get_data(response, 0, csio->data_ptr + oreceived, data_segment_len);
1067 * XXX: Check DataSN.
1070 if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) {
1072 * Nothing more to do.
1074 icl_pdu_free(response);
1078 //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status);
1079 if (bhsdi->bhsdi_status == 0) {
1080 ccb->ccb_h.status = CAM_REQ_CMP;
1082 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
1083 xpt_freeze_devq(ccb->ccb_h.path, 1);
1084 ISCSI_SESSION_DEBUG(is, "freezing devq");
1086 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
1087 csio->scsi_status = bhsdi->bhsdi_status;
1090 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1091 KASSERT(received <= csio->dxfer_len,
1092 ("received > csio->dxfer_len"));
1093 if (received < csio->dxfer_len) {
1094 csio->resid = ntohl(bhsdi->bhsdi_residual_count);
1095 if (csio->resid != csio->dxfer_len - received) {
1096 ISCSI_SESSION_WARN(is, "underflow mismatch: "
1097 "target indicates %d, we calculated %zd",
1098 csio->resid, csio->dxfer_len - received);
1100 csio->resid = csio->dxfer_len - received;
1105 icl_pdu_free(response);
1109 iscsi_pdu_handle_logout_response(struct icl_pdu *response)
1112 ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response");
1113 icl_pdu_free(response);
1117 iscsi_pdu_handle_r2t(struct icl_pdu *response)
1119 struct icl_pdu *request;
1120 struct iscsi_session *is;
1121 struct iscsi_bhs_r2t *bhsr2t;
1122 struct iscsi_bhs_data_out *bhsdo;
1123 struct iscsi_outstanding *io;
1124 struct ccb_scsiio *csio;
1125 size_t off, len, total_len;
1128 is = PDU_SESSION(response);
1130 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
1131 io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag);
1132 if (io == NULL || io->io_ccb == NULL) {
1133 ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting",
1134 bhsr2t->bhsr2t_initiator_task_tag);
1135 icl_pdu_free(response);
1136 iscsi_session_reconnect(is);
1140 csio = &io->io_ccb->csio;
1142 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) {
1143 ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting");
1144 icl_pdu_free(response);
1145 iscsi_session_reconnect(is);
1150 * XXX: Verify R2TSN.
1155 off = ntohl(bhsr2t->bhsr2t_buffer_offset);
1156 if (off > csio->dxfer_len) {
1157 ISCSI_SESSION_WARN(is, "target requested invalid offset "
1158 "%zd, buffer is is %d; reconnecting", off, csio->dxfer_len);
1159 icl_pdu_free(response);
1160 iscsi_session_reconnect(is);
1164 total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length);
1165 if (total_len == 0 || total_len > csio->dxfer_len) {
1166 ISCSI_SESSION_WARN(is, "target requested invalid length "
1167 "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len);
1168 icl_pdu_free(response);
1169 iscsi_session_reconnect(is);
1173 //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len);
1178 if (len > is->is_max_data_segment_length)
1179 len = is->is_max_data_segment_length;
1181 if (off + len > csio->dxfer_len) {
1182 ISCSI_SESSION_WARN(is, "target requested invalid "
1183 "length/offset %zd, buffer is %d; reconnecting",
1184 off + len, csio->dxfer_len);
1185 icl_pdu_free(response);
1186 iscsi_session_reconnect(is);
1190 request = icl_pdu_new(response->ip_conn, M_NOWAIT);
1191 if (request == NULL) {
1192 icl_pdu_free(response);
1193 iscsi_session_reconnect(is);
1197 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
1198 bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT;
1199 bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun;
1200 bhsdo->bhsdo_initiator_task_tag =
1201 bhsr2t->bhsr2t_initiator_task_tag;
1202 bhsdo->bhsdo_target_transfer_tag =
1203 bhsr2t->bhsr2t_target_transfer_tag;
1204 bhsdo->bhsdo_datasn = htonl(io->io_datasn++);
1205 bhsdo->bhsdo_buffer_offset = htonl(off);
1206 error = icl_pdu_append_data(request, csio->data_ptr + off, len,
1209 ISCSI_SESSION_WARN(is, "failed to allocate memory; "
1211 icl_pdu_free(request);
1212 icl_pdu_free(response);
1213 iscsi_session_reconnect(is);
1220 if (total_len == 0) {
1221 bhsdo->bhsdo_flags |= BHSDO_FLAGS_F;
1222 //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off);
1224 //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off);
1227 iscsi_pdu_queue_locked(request);
1233 icl_pdu_free(response);
1237 iscsi_pdu_handle_async_message(struct icl_pdu *response)
1239 struct iscsi_bhs_asynchronous_message *bhsam;
1240 struct iscsi_session *is;
1242 is = PDU_SESSION(response);
1243 bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1244 switch (bhsam->bhsam_async_event) {
1245 case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT:
1246 ISCSI_SESSION_WARN(is, "target requests logout; removing session");
1247 iscsi_session_logout(is);
1248 iscsi_session_terminate(is);
1250 case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION:
1251 ISCSI_SESSION_WARN(is, "target indicates it will drop drop the connection");
1253 case BHSAM_EVENT_TARGET_TERMINATES_SESSION:
1254 ISCSI_SESSION_WARN(is, "target indicates it will drop drop the session");
1258 * XXX: Technically, we're obligated to also handle
1259 * parameter renegotiation.
1261 ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event);
1265 icl_pdu_free(response);
1269 iscsi_pdu_handle_reject(struct icl_pdu *response)
1271 struct iscsi_bhs_reject *bhsr;
1272 struct iscsi_session *is;
1274 is = PDU_SESSION(response);
1275 bhsr = (struct iscsi_bhs_reject *)response->ip_bhs;
1276 ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?",
1279 icl_pdu_free(response);
1283 iscsi_ioctl_daemon_wait(struct iscsi_softc *sc,
1284 struct iscsi_daemon_request *request)
1286 struct iscsi_session *is;
1289 sx_slock(&sc->sc_lock);
1291 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1292 ISCSI_SESSION_LOCK(is);
1293 if (is->is_waiting_for_iscsid)
1295 ISCSI_SESSION_UNLOCK(is);
1300 * No session requires attention from iscsid(8); wait.
1302 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
1304 sx_sunlock(&sc->sc_lock);
1310 is->is_waiting_for_iscsid = false;
1311 is->is_login_phase = true;
1312 is->is_reason[0] = '\0';
1313 ISCSI_SESSION_UNLOCK(is);
1315 request->idr_session_id = is->is_id;
1316 memcpy(&request->idr_isid, &is->is_isid,
1317 sizeof(request->idr_isid));
1318 request->idr_tsih = 0; /* New or reinstated session. */
1319 memcpy(&request->idr_conf, &is->is_conf,
1320 sizeof(request->idr_conf));
1322 error = icl_limits(is->is_conf.isc_offload,
1323 &request->idr_limits.isl_max_data_segment_length);
1325 ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" "
1326 "failed with error %d", is->is_conf.isc_offload,
1328 sx_sunlock(&sc->sc_lock);
1332 sx_sunlock(&sc->sc_lock);
1338 iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc,
1339 struct iscsi_daemon_handoff *handoff)
1341 struct iscsi_session *is;
1342 struct icl_conn *ic;
1345 sx_slock(&sc->sc_lock);
1348 * Find the session to hand off socket to.
1350 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1351 if (is->is_id == handoff->idh_session_id)
1355 sx_sunlock(&sc->sc_lock);
1358 ISCSI_SESSION_LOCK(is);
1360 if (is->is_conf.isc_discovery || is->is_terminating) {
1361 ISCSI_SESSION_UNLOCK(is);
1362 sx_sunlock(&sc->sc_lock);
1365 if (is->is_connected) {
1367 * This might have happened because another iscsid(8)
1368 * instance handed off the connection in the meantime.
1371 ISCSI_SESSION_WARN(is, "handoff on already connected "
1373 ISCSI_SESSION_UNLOCK(is);
1374 sx_sunlock(&sc->sc_lock);
1378 strlcpy(is->is_target_alias, handoff->idh_target_alias,
1379 sizeof(is->is_target_alias));
1380 is->is_tsih = handoff->idh_tsih;
1381 is->is_statsn = handoff->idh_statsn;
1382 is->is_initial_r2t = handoff->idh_initial_r2t;
1383 is->is_immediate_data = handoff->idh_immediate_data;
1386 * Cap MaxRecvDataSegmentLength obtained from the target to the maximum
1387 * size supported by our ICL module.
1389 is->is_max_data_segment_length = min(ic->ic_max_data_segment_length,
1390 handoff->idh_max_data_segment_length);
1391 is->is_max_burst_length = handoff->idh_max_burst_length;
1392 is->is_first_burst_length = handoff->idh_first_burst_length;
1394 if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C)
1395 ic->ic_header_crc32c = true;
1397 ic->ic_header_crc32c = false;
1398 if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C)
1399 ic->ic_data_crc32c = true;
1401 ic->ic_data_crc32c = false;
1404 is->is_expcmdsn = 0;
1405 is->is_maxcmdsn = 0;
1406 is->is_waiting_for_iscsid = false;
1407 is->is_login_phase = false;
1409 is->is_connected = true;
1410 is->is_reason[0] = '\0';
1412 ISCSI_SESSION_UNLOCK(is);
1414 #ifdef ICL_KERNEL_PROXY
1415 if (handoff->idh_socket != 0) {
1418 * Handoff without using ICL proxy.
1420 error = icl_conn_handoff(ic, handoff->idh_socket);
1422 sx_sunlock(&sc->sc_lock);
1423 iscsi_session_terminate(is);
1426 #ifdef ICL_KERNEL_PROXY
1430 sx_sunlock(&sc->sc_lock);
1432 if (is->is_sim != NULL) {
1434 * When reconnecting, there already is SIM allocated for the session.
1436 KASSERT(is->is_simq_frozen, ("reconnect without frozen simq"));
1437 ISCSI_SESSION_LOCK(is);
1438 ISCSI_SESSION_DEBUG(is, "releasing");
1439 xpt_release_simq(is->is_sim, 1);
1440 is->is_simq_frozen = false;
1441 ISCSI_SESSION_UNLOCK(is);
1444 ISCSI_SESSION_LOCK(is);
1445 is->is_devq = cam_simq_alloc(maxtags);
1446 if (is->is_devq == NULL) {
1447 ISCSI_SESSION_WARN(is, "failed to allocate simq");
1448 iscsi_session_terminate(is);
1452 is->is_sim = cam_sim_alloc(iscsi_action, iscsi_poll, "iscsi",
1453 is, is->is_id /* unit */, &is->is_lock,
1454 1, maxtags, is->is_devq);
1455 if (is->is_sim == NULL) {
1456 ISCSI_SESSION_UNLOCK(is);
1457 ISCSI_SESSION_WARN(is, "failed to allocate SIM");
1458 cam_simq_free(is->is_devq);
1459 iscsi_session_terminate(is);
1463 error = xpt_bus_register(is->is_sim, NULL, 0);
1465 ISCSI_SESSION_UNLOCK(is);
1466 ISCSI_SESSION_WARN(is, "failed to register bus");
1467 iscsi_session_terminate(is);
1471 error = xpt_create_path(&is->is_path, /*periph*/NULL,
1472 cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD,
1474 if (error != CAM_REQ_CMP) {
1475 ISCSI_SESSION_UNLOCK(is);
1476 ISCSI_SESSION_WARN(is, "failed to create path");
1477 iscsi_session_terminate(is);
1480 ISCSI_SESSION_UNLOCK(is);
1487 iscsi_ioctl_daemon_fail(struct iscsi_softc *sc,
1488 struct iscsi_daemon_fail *fail)
1490 struct iscsi_session *is;
1492 sx_slock(&sc->sc_lock);
1494 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1495 if (is->is_id == fail->idf_session_id)
1499 sx_sunlock(&sc->sc_lock);
1502 ISCSI_SESSION_LOCK(is);
1503 ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s",
1505 strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason));
1506 //is->is_waiting_for_iscsid = false;
1507 //is->is_login_phase = true;
1508 //iscsi_session_reconnect(is);
1509 ISCSI_SESSION_UNLOCK(is);
1510 sx_sunlock(&sc->sc_lock);
1515 #ifdef ICL_KERNEL_PROXY
1517 iscsi_ioctl_daemon_connect(struct iscsi_softc *sc,
1518 struct iscsi_daemon_connect *idc)
1520 struct iscsi_session *is;
1521 struct sockaddr *from_sa, *to_sa;
1524 sx_slock(&sc->sc_lock);
1525 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1526 if (is->is_id == idc->idc_session_id)
1530 sx_sunlock(&sc->sc_lock);
1533 sx_sunlock(&sc->sc_lock);
1535 if (idc->idc_from_addrlen > 0) {
1536 error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen);
1538 ISCSI_SESSION_WARN(is,
1539 "getsockaddr failed with error %d", error);
1545 error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen);
1547 ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d",
1549 free(from_sa, M_SONAME);
1553 ISCSI_SESSION_LOCK(is);
1554 is->is_waiting_for_iscsid = false;
1555 is->is_login_phase = true;
1557 ISCSI_SESSION_UNLOCK(is);
1559 error = icl_conn_connect(is->is_conn, idc->idc_iser, idc->idc_domain,
1560 idc->idc_socktype, idc->idc_protocol, from_sa, to_sa);
1561 free(from_sa, M_SONAME);
1562 free(to_sa, M_SONAME);
1565 * Digests are always disabled during login phase.
1567 is->is_conn->ic_header_crc32c = false;
1568 is->is_conn->ic_data_crc32c = false;
1574 iscsi_ioctl_daemon_send(struct iscsi_softc *sc,
1575 struct iscsi_daemon_send *ids)
1577 struct iscsi_session *is;
1583 sx_slock(&sc->sc_lock);
1584 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1585 if (is->is_id == ids->ids_session_id)
1589 sx_sunlock(&sc->sc_lock);
1592 sx_sunlock(&sc->sc_lock);
1594 if (is->is_login_phase == false)
1597 if (is->is_terminating || is->is_reconnecting)
1600 datalen = ids->ids_data_segment_len;
1601 if (datalen > ISCSI_MAX_DATA_SEGMENT_LENGTH)
1604 data = malloc(datalen, M_ISCSI, M_WAITOK);
1605 error = copyin(ids->ids_data_segment, data, datalen);
1607 free(data, M_ISCSI);
1612 ip = icl_pdu_new(is->is_conn, M_WAITOK);
1613 memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs));
1615 error = icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1616 KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed"));
1617 free(data, M_ISCSI);
1625 iscsi_ioctl_daemon_receive(struct iscsi_softc *sc,
1626 struct iscsi_daemon_receive *idr)
1628 struct iscsi_session *is;
1632 sx_slock(&sc->sc_lock);
1633 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1634 if (is->is_id == idr->idr_session_id)
1638 sx_sunlock(&sc->sc_lock);
1641 sx_sunlock(&sc->sc_lock);
1643 if (is->is_login_phase == false)
1646 ISCSI_SESSION_LOCK(is);
1647 while (is->is_login_pdu == NULL &&
1648 is->is_terminating == false &&
1649 is->is_reconnecting == false)
1650 cv_wait(&is->is_login_cv, &is->is_lock);
1651 if (is->is_terminating || is->is_reconnecting) {
1652 ISCSI_SESSION_UNLOCK(is);
1655 ip = is->is_login_pdu;
1656 is->is_login_pdu = NULL;
1657 ISCSI_SESSION_UNLOCK(is);
1659 if (ip->ip_data_len > idr->idr_data_segment_len) {
1664 copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs));
1665 if (ip->ip_data_len > 0) {
1666 data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK);
1667 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1668 copyout(data, idr->idr_data_segment, ip->ip_data_len);
1669 free(data, M_ISCSI);
1676 #endif /* ICL_KERNEL_PROXY */
1679 iscsi_sanitize_session_conf(struct iscsi_session_conf *isc)
1682 * Just make sure all the fields are null-terminated.
1684 * XXX: This is not particularly secure. We should
1685 * create our own conf and then copy in relevant
1688 isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0';
1689 isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0';
1690 isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0';
1691 isc->isc_target[ISCSI_NAME_LEN - 1] = '\0';
1692 isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0';
1693 isc->isc_user[ISCSI_NAME_LEN - 1] = '\0';
1694 isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0';
1695 isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0';
1696 isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0';
1700 iscsi_valid_session_conf(const struct iscsi_session_conf *isc)
1703 if (isc->isc_initiator[0] == '\0') {
1704 ISCSI_DEBUG("empty isc_initiator");
1708 if (isc->isc_target_addr[0] == '\0') {
1709 ISCSI_DEBUG("empty isc_target_addr");
1713 if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) {
1714 ISCSI_DEBUG("non-empty isc_target for discovery session");
1718 if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) {
1719 ISCSI_DEBUG("empty isc_target for non-discovery session");
1727 iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa)
1729 struct iscsi_session *is;
1730 const struct iscsi_session *is2;
1733 iscsi_sanitize_session_conf(&isa->isa_conf);
1734 if (iscsi_valid_session_conf(&isa->isa_conf) == false)
1737 is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK);
1738 memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf));
1740 sx_xlock(&sc->sc_lock);
1743 * Prevent duplicates.
1745 TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
1746 if (!!is->is_conf.isc_discovery !=
1747 !!is2->is_conf.isc_discovery)
1750 if (strcmp(is->is_conf.isc_target_addr,
1751 is2->is_conf.isc_target_addr) != 0)
1754 if (is->is_conf.isc_discovery == 0 &&
1755 strcmp(is->is_conf.isc_target,
1756 is2->is_conf.isc_target) != 0)
1759 sx_xunlock(&sc->sc_lock);
1764 is->is_conn = icl_new_conn(is->is_conf.isc_offload,
1765 "iscsi", &is->is_lock);
1766 if (is->is_conn == NULL) {
1767 sx_xunlock(&sc->sc_lock);
1771 is->is_conn->ic_receive = iscsi_receive_callback;
1772 is->is_conn->ic_error = iscsi_error_callback;
1773 is->is_conn->ic_prv0 = is;
1774 TAILQ_INIT(&is->is_outstanding);
1775 STAILQ_INIT(&is->is_postponed);
1776 mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF);
1777 cv_init(&is->is_maintenance_cv, "iscsi_mt");
1778 #ifdef ICL_KERNEL_PROXY
1779 cv_init(&is->is_login_cv, "iscsi_login");
1783 sc->sc_last_session_id++;
1784 is->is_id = sc->sc_last_session_id;
1785 is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */
1786 arc4rand(&is->is_isid[1], 5, 0);
1788 callout_init(&is->is_callout, 1);
1790 error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt");
1792 ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error);
1793 sx_xunlock(&sc->sc_lock);
1797 callout_reset(&is->is_callout, 1 * hz, iscsi_callout, is);
1798 TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next);
1801 * Trigger immediate reconnection.
1803 ISCSI_SESSION_LOCK(is);
1804 is->is_waiting_for_iscsid = true;
1805 strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
1806 ISCSI_SESSION_UNLOCK(is);
1807 cv_signal(&sc->sc_cv);
1809 sx_xunlock(&sc->sc_lock);
1815 iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1,
1816 unsigned int id2, const struct iscsi_session_conf *c2)
1819 if (id2 != 0 && id2 != id1)
1821 if (c2->isc_target[0] != '\0' &&
1822 strcmp(c1->isc_target, c2->isc_target) != 0)
1824 if (c2->isc_target_addr[0] != '\0' &&
1825 strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0)
1831 iscsi_ioctl_session_remove(struct iscsi_softc *sc,
1832 struct iscsi_session_remove *isr)
1834 struct iscsi_session *is, *tmp;
1837 iscsi_sanitize_session_conf(&isr->isr_conf);
1839 sx_xlock(&sc->sc_lock);
1840 TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) {
1841 ISCSI_SESSION_LOCK(is);
1842 if (iscsi_session_conf_matches(is->is_id, &is->is_conf,
1843 isr->isr_session_id, &isr->isr_conf)) {
1845 iscsi_session_logout(is);
1846 iscsi_session_terminate(is);
1848 ISCSI_SESSION_UNLOCK(is);
1850 sx_xunlock(&sc->sc_lock);
1859 iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl)
1863 struct iscsi_session *is;
1864 struct iscsi_session_state iss;
1866 sx_slock(&sc->sc_lock);
1867 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1868 if (i >= isl->isl_nentries) {
1869 sx_sunlock(&sc->sc_lock);
1872 memset(&iss, 0, sizeof(iss));
1873 memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf));
1874 iss.iss_id = is->is_id;
1875 strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias));
1876 strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason));
1877 strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload));
1879 if (is->is_conn->ic_header_crc32c)
1880 iss.iss_header_digest = ISCSI_DIGEST_CRC32C;
1882 iss.iss_header_digest = ISCSI_DIGEST_NONE;
1884 if (is->is_conn->ic_data_crc32c)
1885 iss.iss_data_digest = ISCSI_DIGEST_CRC32C;
1887 iss.iss_data_digest = ISCSI_DIGEST_NONE;
1889 iss.iss_max_data_segment_length = is->is_max_data_segment_length;
1890 iss.iss_immediate_data = is->is_immediate_data;
1891 iss.iss_connected = is->is_connected;
1893 error = copyout(&iss, isl->isl_pstates + i, sizeof(iss));
1895 sx_sunlock(&sc->sc_lock);
1900 sx_sunlock(&sc->sc_lock);
1902 isl->isl_nentries = i;
1908 iscsi_ioctl_session_modify(struct iscsi_softc *sc,
1909 struct iscsi_session_modify *ism)
1911 struct iscsi_session *is;
1913 iscsi_sanitize_session_conf(&ism->ism_conf);
1914 if (iscsi_valid_session_conf(&ism->ism_conf) == false)
1917 sx_xlock(&sc->sc_lock);
1918 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1919 ISCSI_SESSION_LOCK(is);
1920 if (is->is_id == ism->ism_session_id)
1922 ISCSI_SESSION_UNLOCK(is);
1925 sx_xunlock(&sc->sc_lock);
1928 sx_xunlock(&sc->sc_lock);
1930 memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf));
1931 ISCSI_SESSION_UNLOCK(is);
1933 iscsi_session_reconnect(is);
1939 iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
1942 struct iscsi_softc *sc;
1948 return (iscsi_ioctl_daemon_wait(sc,
1949 (struct iscsi_daemon_request *)arg));
1951 return (iscsi_ioctl_daemon_handoff(sc,
1952 (struct iscsi_daemon_handoff *)arg));
1954 return (iscsi_ioctl_daemon_fail(sc,
1955 (struct iscsi_daemon_fail *)arg));
1956 #ifdef ICL_KERNEL_PROXY
1958 return (iscsi_ioctl_daemon_connect(sc,
1959 (struct iscsi_daemon_connect *)arg));
1961 return (iscsi_ioctl_daemon_send(sc,
1962 (struct iscsi_daemon_send *)arg));
1964 return (iscsi_ioctl_daemon_receive(sc,
1965 (struct iscsi_daemon_receive *)arg));
1966 #endif /* ICL_KERNEL_PROXY */
1968 return (iscsi_ioctl_session_add(sc,
1969 (struct iscsi_session_add *)arg));
1971 return (iscsi_ioctl_session_remove(sc,
1972 (struct iscsi_session_remove *)arg));
1974 return (iscsi_ioctl_session_list(sc,
1975 (struct iscsi_session_list *)arg));
1977 return (iscsi_ioctl_session_modify(sc,
1978 (struct iscsi_session_modify *)arg));
1984 static struct iscsi_outstanding *
1985 iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag)
1987 struct iscsi_outstanding *io;
1989 ISCSI_SESSION_LOCK_ASSERT(is);
1991 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
1992 if (io->io_initiator_task_tag == initiator_task_tag)
1998 static struct iscsi_outstanding *
1999 iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb)
2001 struct iscsi_outstanding *io;
2003 ISCSI_SESSION_LOCK_ASSERT(is);
2005 TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2006 if (io->io_ccb == ccb)
2012 static struct iscsi_outstanding *
2013 iscsi_outstanding_add(struct iscsi_session *is,
2014 union ccb *ccb, uint32_t *initiator_task_tagp)
2016 struct iscsi_outstanding *io;
2019 ISCSI_SESSION_LOCK_ASSERT(is);
2021 io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO);
2023 ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes",
2028 error = icl_conn_task_setup(is->is_conn, &ccb->csio,
2029 initiator_task_tagp, &io->io_icl_prv);
2031 ISCSI_SESSION_WARN(is,
2032 "icl_conn_task_setup() failed with error %d", error);
2033 uma_zfree(iscsi_outstanding_zone, io);
2037 KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL,
2038 ("initiator_task_tag 0x%x already added", *initiator_task_tagp));
2040 io->io_initiator_task_tag = *initiator_task_tagp;
2042 TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next);
2047 iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io)
2050 ISCSI_SESSION_LOCK_ASSERT(is);
2052 icl_conn_task_done(is->is_conn, io->io_icl_prv);
2053 TAILQ_REMOVE(&is->is_outstanding, io, io_next);
2054 uma_zfree(iscsi_outstanding_zone, io);
2058 iscsi_action_abort(struct iscsi_session *is, union ccb *ccb)
2060 struct icl_pdu *request;
2061 struct iscsi_bhs_task_management_request *bhstmr;
2062 struct ccb_abort *cab = &ccb->cab;
2063 struct iscsi_outstanding *io, *aio;
2064 uint32_t initiator_task_tag;
2066 ISCSI_SESSION_LOCK_ASSERT(is);
2069 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2071 if (is->is_login_phase) {
2072 ccb->ccb_h.status = CAM_REQ_ABORTED;
2078 aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb);
2080 ccb->ccb_h.status = CAM_REQ_CMP;
2085 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2086 if (request == NULL) {
2087 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2092 initiator_task_tag = is->is_initiator_task_tag++;
2094 io = iscsi_outstanding_add(is, NULL, &initiator_task_tag);
2096 icl_pdu_free(request);
2097 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2101 io->io_datasn = aio->io_initiator_task_tag;
2103 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2104 bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST;
2105 bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK;
2106 bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2107 bhstmr->bhstmr_initiator_task_tag = initiator_task_tag;
2108 bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag;
2110 iscsi_pdu_queue_locked(request);
2114 iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb)
2116 struct icl_pdu *request;
2117 struct iscsi_bhs_scsi_command *bhssc;
2118 struct ccb_scsiio *csio;
2119 struct iscsi_outstanding *io;
2121 uint32_t initiator_task_tag;
2124 ISCSI_SESSION_LOCK_ASSERT(is);
2127 KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2129 if (is->is_login_phase) {
2130 ISCSI_SESSION_DEBUG(is, "called during login phase");
2131 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2132 xpt_freeze_devq(ccb->ccb_h.path, 1);
2133 ISCSI_SESSION_DEBUG(is, "freezing devq");
2135 ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN;
2141 request = icl_pdu_new(is->is_conn, M_NOWAIT);
2142 if (request == NULL) {
2143 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2144 xpt_freeze_devq(ccb->ccb_h.path, 1);
2145 ISCSI_SESSION_DEBUG(is, "freezing devq");
2147 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2152 initiator_task_tag = is->is_initiator_task_tag++;
2153 io = iscsi_outstanding_add(is, ccb, &initiator_task_tag);
2155 icl_pdu_free(request);
2156 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2157 xpt_freeze_devq(ccb->ccb_h.path, 1);
2158 ISCSI_SESSION_DEBUG(is, "freezing devq");
2160 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2166 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2167 bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND;
2168 bhssc->bhssc_flags |= BHSSC_FLAGS_F;
2169 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
2171 bhssc->bhssc_flags |= BHSSC_FLAGS_R;
2174 bhssc->bhssc_flags |= BHSSC_FLAGS_W;
2178 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2179 switch (csio->tag_action) {
2180 case MSG_HEAD_OF_Q_TAG:
2181 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ;
2183 case MSG_ORDERED_Q_TAG:
2184 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED;
2187 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA;
2189 case MSG_SIMPLE_Q_TAG:
2191 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE;
2195 bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED;
2197 bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2198 bhssc->bhssc_initiator_task_tag = initiator_task_tag;
2199 bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len);
2200 KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb),
2201 ("unsupported CDB size %zd", (size_t)csio->cdb_len));
2203 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2204 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len);
2206 memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len);
2208 if (is->is_immediate_data &&
2209 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2210 len = csio->dxfer_len;
2211 //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len);
2212 if (len > is->is_first_burst_length) {
2213 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_first_burst_length);
2214 len = is->is_first_burst_length;
2216 if (len > is->is_max_data_segment_length) {
2217 ISCSI_SESSION_DEBUG(is, "len %zd -> %zd", len, is->is_max_data_segment_length);
2218 len = is->is_max_data_segment_length;
2221 error = icl_pdu_append_data(request, csio->data_ptr, len, M_NOWAIT);
2223 icl_pdu_free(request);
2224 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2225 xpt_freeze_devq(ccb->ccb_h.path, 1);
2226 ISCSI_SESSION_DEBUG(is, "freezing devq");
2228 ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2233 iscsi_pdu_queue_locked(request);
2237 iscsi_action(struct cam_sim *sim, union ccb *ccb)
2239 struct iscsi_session *is;
2241 is = cam_sim_softc(sim);
2243 ISCSI_SESSION_LOCK_ASSERT(is);
2245 if (is->is_terminating ||
2246 (is->is_connected == false && fail_on_disconnection)) {
2247 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2252 switch (ccb->ccb_h.func_code) {
2255 struct ccb_pathinq *cpi = &ccb->cpi;
2257 cpi->version_num = 1;
2258 cpi->hba_inquiry = PI_TAG_ABLE;
2259 cpi->target_sprt = 0;
2260 cpi->hba_misc = PIM_EXTLUNS;
2261 cpi->hba_eng_cnt = 0;
2262 cpi->max_target = 0;
2264 * Note that the variable below is only relevant for targets
2265 * that don't claim compliance with anything above SPC2, which
2266 * means they don't support REPORT_LUNS.
2269 cpi->initiator_id = ~0;
2270 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2271 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
2272 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2273 cpi->unit_number = cam_sim_unit(sim);
2274 cpi->bus_id = cam_sim_bus(sim);
2275 cpi->base_transfer_speed = 150000; /* XXX */
2276 cpi->transport = XPORT_ISCSI;
2277 cpi->transport_version = 0;
2278 cpi->protocol = PROTO_SCSI;
2279 cpi->protocol_version = SCSI_REV_SPC3;
2280 cpi->maxio = MAXPHYS;
2281 cpi->ccb_h.status = CAM_REQ_CMP;
2284 case XPT_GET_TRAN_SETTINGS:
2286 struct ccb_trans_settings *cts;
2287 struct ccb_trans_settings_scsi *scsi;
2290 scsi = &cts->proto_specific.scsi;
2292 cts->protocol = PROTO_SCSI;
2293 cts->protocol_version = SCSI_REV_SPC3;
2294 cts->transport = XPORT_ISCSI;
2295 cts->transport_version = 0;
2296 scsi->valid = CTS_SCSI_VALID_TQ;
2297 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2298 cts->ccb_h.status = CAM_REQ_CMP;
2301 case XPT_CALC_GEOMETRY:
2302 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2303 ccb->ccb_h.status = CAM_REQ_CMP;
2307 * XXX: What's the point?
2311 ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io");
2312 ccb->ccb_h.status = CAM_REQ_CMP;
2316 iscsi_action_abort(is, ccb);
2319 iscsi_action_scsiio(is, ccb);
2323 ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code);
2325 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2332 iscsi_poll(struct cam_sim *sim)
2335 KASSERT(0, ("%s: you're not supposed to be here", __func__));
2339 iscsi_terminate_sessions(struct iscsi_softc *sc)
2341 struct iscsi_session *is;
2343 sx_slock(&sc->sc_lock);
2344 TAILQ_FOREACH(is, &sc->sc_sessions, is_next)
2345 iscsi_session_terminate(is);
2346 while(!TAILQ_EMPTY(&sc->sc_sessions)) {
2347 ISCSI_DEBUG("waiting for sessions to terminate");
2348 cv_wait(&sc->sc_cv, &sc->sc_lock);
2350 ISCSI_DEBUG("all sessions terminated");
2351 sx_sunlock(&sc->sc_lock);
2355 iscsi_shutdown_pre(struct iscsi_softc *sc)
2357 struct iscsi_session *is;
2359 if (!fail_on_shutdown)
2363 * If we have any sessions waiting for reconnection, request
2364 * maintenance thread to fail them immediately instead of waiting
2365 * for reconnect timeout.
2367 * This prevents LUNs with mounted filesystems that are supported
2368 * by disconnected iSCSI sessions from hanging, however it will
2369 * fail all queued BIOs.
2371 ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown");
2373 fail_on_disconnection = 1;
2375 sx_slock(&sc->sc_lock);
2376 TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2377 ISCSI_SESSION_LOCK(is);
2378 if (!is->is_connected) {
2379 ISCSI_SESSION_DEBUG(is, "force failing disconnected session early");
2380 iscsi_session_reconnect(is);
2382 ISCSI_SESSION_UNLOCK(is);
2384 sx_sunlock(&sc->sc_lock);
2388 iscsi_shutdown_post(struct iscsi_softc *sc)
2391 ISCSI_DEBUG("removing all sessions due to shutdown");
2392 iscsi_terminate_sessions(sc);
2400 sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK);
2401 sx_init(&sc->sc_lock, "iscsi");
2402 TAILQ_INIT(&sc->sc_sessions);
2403 cv_init(&sc->sc_cv, "iscsi_cv");
2405 iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding",
2406 sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL,
2409 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw,
2410 NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
2412 ISCSI_WARN("failed to create device node, error %d", error);
2415 sc->sc_cdev->si_drv1 = sc;
2417 sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2418 iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST);
2420 * shutdown_post_sync needs to run after filesystem shutdown and before
2421 * CAM shutdown - otherwise when rebooting with an iSCSI session that is
2422 * disconnected but has outstanding requests, dashutdown() will hang on
2423 * cam_periph_runccb().
2425 sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
2426 iscsi_shutdown_post, sc, SHUTDOWN_PRI_DEFAULT - 1);
2435 if (sc->sc_cdev != NULL) {
2436 ISCSI_DEBUG("removing device node");
2437 destroy_dev(sc->sc_cdev);
2438 ISCSI_DEBUG("device node removed");
2441 if (sc->sc_shutdown_pre_eh != NULL)
2442 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh);
2443 if (sc->sc_shutdown_post_eh != NULL)
2444 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh);
2446 iscsi_terminate_sessions(sc);
2448 uma_zdestroy(iscsi_outstanding_zone);
2449 sx_destroy(&sc->sc_lock);
2450 cv_destroy(&sc->sc_cv);
2458 sx_slock(&sc->sc_lock);
2459 if (!TAILQ_EMPTY(&sc->sc_sessions)) {
2460 sx_sunlock(&sc->sc_lock);
2463 sx_sunlock(&sc->sc_lock);
2468 iscsi_modevent(module_t mod, int what, void *arg)
2474 error = iscsi_load();
2477 error = iscsi_unload();
2480 error = iscsi_quiesce();
2489 moduledata_t iscsi_data = {
2495 DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2496 MODULE_DEPEND(iscsi, cam, 1, 1, 1);
2497 MODULE_DEPEND(iscsi, icl, 1, 1, 1);