1009ea47eSEdward Tomasz Napierala /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4009ea47eSEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation
5009ea47eSEdward Tomasz Napierala *
6009ea47eSEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship
7009ea47eSEdward Tomasz Napierala * from the FreeBSD Foundation.
8009ea47eSEdward Tomasz Napierala *
9009ea47eSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without
10009ea47eSEdward Tomasz Napierala * modification, are permitted provided that the following conditions
11009ea47eSEdward Tomasz Napierala * are met:
12009ea47eSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright
13009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer.
14009ea47eSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright
15009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the
16009ea47eSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution.
17009ea47eSEdward Tomasz Napierala *
18009ea47eSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19009ea47eSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20009ea47eSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21009ea47eSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22009ea47eSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23009ea47eSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24009ea47eSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25009ea47eSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26009ea47eSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27009ea47eSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28009ea47eSEdward Tomasz Napierala * SUCH DAMAGE.
29009ea47eSEdward Tomasz Napierala *
30009ea47eSEdward Tomasz Napierala */
31009ea47eSEdward Tomasz Napierala
32009ea47eSEdward Tomasz Napierala #include <sys/param.h>
337aab9c14SJohn Baldwin #include <sys/bio.h>
34009ea47eSEdward Tomasz Napierala #include <sys/condvar.h>
35009ea47eSEdward Tomasz Napierala #include <sys/conf.h>
36a9f2f291SEdward Tomasz Napierala #include <sys/endian.h>
37009ea47eSEdward Tomasz Napierala #include <sys/eventhandler.h>
38009ea47eSEdward Tomasz Napierala #include <sys/file.h>
39009ea47eSEdward Tomasz Napierala #include <sys/kernel.h>
40009ea47eSEdward Tomasz Napierala #include <sys/kthread.h>
41009ea47eSEdward Tomasz Napierala #include <sys/lock.h>
42009ea47eSEdward Tomasz Napierala #include <sys/malloc.h>
43bd6bb493SRichard Scheffenegger #include <sys/mbuf.h>
44009ea47eSEdward Tomasz Napierala #include <sys/mutex.h>
45009ea47eSEdward Tomasz Napierala #include <sys/module.h>
462ce1c45bSMitchell Horne #include <sys/proc.h>
474eb861d3SMitchell Horne #include <sys/reboot.h>
48f41492b0SEdward Tomasz Napierala #include <sys/socket.h>
49bd6bb493SRichard Scheffenegger #include <sys/sockopt.h>
50009ea47eSEdward Tomasz Napierala #include <sys/sysctl.h>
51009ea47eSEdward Tomasz Napierala #include <sys/systm.h>
52009ea47eSEdward Tomasz Napierala #include <sys/sx.h>
534eb861d3SMitchell Horne
54009ea47eSEdward Tomasz Napierala #include <vm/uma.h>
55009ea47eSEdward Tomasz Napierala
56009ea47eSEdward Tomasz Napierala #include <cam/cam.h>
57009ea47eSEdward Tomasz Napierala #include <cam/cam_ccb.h>
58009ea47eSEdward Tomasz Napierala #include <cam/cam_xpt.h>
59009ea47eSEdward Tomasz Napierala #include <cam/cam_debug.h>
60009ea47eSEdward Tomasz Napierala #include <cam/cam_sim.h>
61009ea47eSEdward Tomasz Napierala #include <cam/cam_xpt_sim.h>
62009ea47eSEdward Tomasz Napierala #include <cam/cam_xpt_periph.h>
63009ea47eSEdward Tomasz Napierala #include <cam/cam_periph.h>
64009ea47eSEdward Tomasz Napierala #include <cam/scsi/scsi_all.h>
65009ea47eSEdward Tomasz Napierala #include <cam/scsi/scsi_message.h>
66009ea47eSEdward Tomasz Napierala
67ddc5fd90SEdward Tomasz Napierala #include <dev/iscsi/icl.h>
68321b17ecSEdward Tomasz Napierala #include <dev/iscsi/icl_wrappers.h>
69ddc5fd90SEdward Tomasz Napierala #include <dev/iscsi/iscsi_ioctl.h>
70ddc5fd90SEdward Tomasz Napierala #include <dev/iscsi/iscsi_proto.h>
71ddc5fd90SEdward Tomasz Napierala #include <dev/iscsi/iscsi.h>
72009ea47eSEdward Tomasz Napierala
73009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
74009ea47eSEdward Tomasz Napierala #include <sys/socketvar.h>
75009ea47eSEdward Tomasz Napierala #endif
76009ea47eSEdward Tomasz Napierala
772ebde326SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
782ebde326SEdward Tomasz Napierala FEATURE(iscsi_kernel_proxy, "iSCSI initiator built with ICL_KERNEL_PROXY");
792ebde326SEdward Tomasz Napierala #endif
802ebde326SEdward Tomasz Napierala
817b02c1e8SJohn Baldwin #ifdef COMPAT_FREEBSD13
827b02c1e8SJohn Baldwin struct iscsi_daemon_request13 {
837b02c1e8SJohn Baldwin unsigned int idr_session_id;
847b02c1e8SJohn Baldwin struct iscsi_session_conf idr_conf;
857b02c1e8SJohn Baldwin uint8_t idr_isid[6];
867b02c1e8SJohn Baldwin uint16_t idr_tsih;
877b02c1e8SJohn Baldwin uint16_t idr_spare_cid;
887b02c1e8SJohn Baldwin struct iscsi_session_limits idr_limits;
897b02c1e8SJohn Baldwin int idr_spare[4];
907b02c1e8SJohn Baldwin };
917b02c1e8SJohn Baldwin
927b02c1e8SJohn Baldwin #define ISCSIDWAIT13 _IOR('I', 0x01, struct iscsi_daemon_request13)
937b02c1e8SJohn Baldwin #endif
947b02c1e8SJohn Baldwin
95009ea47eSEdward Tomasz Napierala /*
96009ea47eSEdward Tomasz Napierala * XXX: This is global so the iscsi_unload() can access it.
97009ea47eSEdward Tomasz Napierala * Think about how to do this properly.
98009ea47eSEdward Tomasz Napierala */
99009ea47eSEdward Tomasz Napierala static struct iscsi_softc *sc;
100009ea47eSEdward Tomasz Napierala
101e0d69c5aSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, iscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
102e0d69c5aSPawel Biernacki "iSCSI initiator");
103009ea47eSEdward Tomasz Napierala static int debug = 1;
104605a34b0SEdward Tomasz Napierala SYSCTL_INT(_kern_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
1051f6b2af2SAlexander Motin &debug, 0, "Enable debug messages");
106bd6bb493SRichard Scheffenegger
107009ea47eSEdward Tomasz Napierala static int ping_timeout = 5;
108605a34b0SEdward Tomasz Napierala SYSCTL_INT(_kern_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, &ping_timeout,
1091f6b2af2SAlexander Motin 0, "Timeout for ping (NOP-Out) requests, in seconds");
110009ea47eSEdward Tomasz Napierala static int iscsid_timeout = 60;
111605a34b0SEdward Tomasz Napierala SYSCTL_INT(_kern_iscsi, OID_AUTO, iscsid_timeout, CTLFLAG_RWTUN, &iscsid_timeout,
1121f6b2af2SAlexander Motin 0, "Time to wait for iscsid(8) to handle reconnection, in seconds");
113009ea47eSEdward Tomasz Napierala static int login_timeout = 60;
114605a34b0SEdward Tomasz Napierala SYSCTL_INT(_kern_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, &login_timeout,
1151f6b2af2SAlexander Motin 0, "Time to wait for iscsid(8) to finish Login Phase, in seconds");
116009ea47eSEdward Tomasz Napierala static int maxtags = 255;
117605a34b0SEdward Tomasz Napierala SYSCTL_INT(_kern_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN, &maxtags,
1181f6b2af2SAlexander Motin 0, "Max number of IO requests queued");
119f1f12e40SEdward Tomasz Napierala static int fail_on_disconnection = 0;
120f1f12e40SEdward Tomasz Napierala SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_disconnection, CTLFLAG_RWTUN,
121f1f12e40SEdward Tomasz Napierala &fail_on_disconnection, 0, "Destroy CAM SIM on connection failure");
122481b36c6SSteven Hartland static int fail_on_shutdown = 1;
123481b36c6SSteven Hartland SYSCTL_INT(_kern_iscsi, OID_AUTO, fail_on_shutdown, CTLFLAG_RWTUN,
124481b36c6SSteven Hartland &fail_on_shutdown, 0, "Fail disconnected sessions on shutdown");
125009ea47eSEdward Tomasz Napierala
126009ea47eSEdward Tomasz Napierala static MALLOC_DEFINE(M_ISCSI, "iSCSI", "iSCSI initiator");
127009ea47eSEdward Tomasz Napierala static uma_zone_t iscsi_outstanding_zone;
128009ea47eSEdward Tomasz Napierala
129009ea47eSEdward Tomasz Napierala #define CONN_SESSION(X) ((struct iscsi_session *)X->ic_prv0)
130009ea47eSEdward Tomasz Napierala #define PDU_SESSION(X) (CONN_SESSION(X->ip_conn))
131009ea47eSEdward Tomasz Napierala
132009ea47eSEdward Tomasz Napierala #define ISCSI_DEBUG(X, ...) \
133b1277ad1SEdward Tomasz Napierala do { \
134b1277ad1SEdward Tomasz Napierala if (debug > 1) \
135009ea47eSEdward Tomasz Napierala printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
136009ea47eSEdward Tomasz Napierala } while (0)
137009ea47eSEdward Tomasz Napierala
138009ea47eSEdward Tomasz Napierala #define ISCSI_WARN(X, ...) \
139b1277ad1SEdward Tomasz Napierala do { \
140009ea47eSEdward Tomasz Napierala if (debug > 0) { \
141009ea47eSEdward Tomasz Napierala printf("WARNING: %s: " X "\n", \
142009ea47eSEdward Tomasz Napierala __func__, ## __VA_ARGS__); \
143b1277ad1SEdward Tomasz Napierala } \
144009ea47eSEdward Tomasz Napierala } while (0)
145009ea47eSEdward Tomasz Napierala
146009ea47eSEdward Tomasz Napierala #define ISCSI_SESSION_DEBUG(S, X, ...) \
147b1277ad1SEdward Tomasz Napierala do { \
148009ea47eSEdward Tomasz Napierala if (debug > 1) { \
149009ea47eSEdward Tomasz Napierala printf("%s: %s (%s): " X "\n", \
150009ea47eSEdward Tomasz Napierala __func__, S->is_conf.isc_target_addr, \
151009ea47eSEdward Tomasz Napierala S->is_conf.isc_target, ## __VA_ARGS__); \
152b1277ad1SEdward Tomasz Napierala } \
153009ea47eSEdward Tomasz Napierala } while (0)
154009ea47eSEdward Tomasz Napierala
155009ea47eSEdward Tomasz Napierala #define ISCSI_SESSION_WARN(S, X, ...) \
156b1277ad1SEdward Tomasz Napierala do { \
157009ea47eSEdward Tomasz Napierala if (debug > 0) { \
158009ea47eSEdward Tomasz Napierala printf("WARNING: %s (%s): " X "\n", \
159009ea47eSEdward Tomasz Napierala S->is_conf.isc_target_addr, \
160009ea47eSEdward Tomasz Napierala S->is_conf.isc_target, ## __VA_ARGS__); \
161b1277ad1SEdward Tomasz Napierala } \
162009ea47eSEdward Tomasz Napierala } while (0)
163009ea47eSEdward Tomasz Napierala
164009ea47eSEdward Tomasz Napierala #define ISCSI_SESSION_LOCK(X) mtx_lock(&X->is_lock)
165009ea47eSEdward Tomasz Napierala #define ISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->is_lock)
166009ea47eSEdward Tomasz Napierala #define ISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->is_lock, MA_OWNED)
167c215ad3aSAlexander Motin #define ISCSI_SESSION_LOCK_ASSERT_NOT(X) mtx_assert(&X->is_lock, MA_NOTOWNED)
168009ea47eSEdward Tomasz Napierala
169009ea47eSEdward Tomasz Napierala static int iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
170009ea47eSEdward Tomasz Napierala int mode, struct thread *td);
171009ea47eSEdward Tomasz Napierala
172009ea47eSEdward Tomasz Napierala static struct cdevsw iscsi_cdevsw = {
173009ea47eSEdward Tomasz Napierala .d_version = D_VERSION,
174009ea47eSEdward Tomasz Napierala .d_ioctl = iscsi_ioctl,
175009ea47eSEdward Tomasz Napierala .d_name = "iscsi",
176009ea47eSEdward Tomasz Napierala };
177009ea47eSEdward Tomasz Napierala
178009ea47eSEdward Tomasz Napierala static void iscsi_pdu_queue_locked(struct icl_pdu *request);
179009ea47eSEdward Tomasz Napierala static void iscsi_pdu_queue(struct icl_pdu *request);
180009ea47eSEdward Tomasz Napierala static void iscsi_pdu_update_statsn(const struct icl_pdu *response);
181009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_nop_in(struct icl_pdu *response);
182009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_scsi_response(struct icl_pdu *response);
183aa75114cSAlexander Motin static void iscsi_pdu_handle_task_response(struct icl_pdu *response);
184009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_data_in(struct icl_pdu *response);
185009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_logout_response(struct icl_pdu *response);
186009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_r2t(struct icl_pdu *response);
187009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_async_message(struct icl_pdu *response);
188009ea47eSEdward Tomasz Napierala static void iscsi_pdu_handle_reject(struct icl_pdu *response);
189009ea47eSEdward Tomasz Napierala static void iscsi_session_reconnect(struct iscsi_session *is);
190009ea47eSEdward Tomasz Napierala static void iscsi_session_terminate(struct iscsi_session *is);
191009ea47eSEdward Tomasz Napierala static void iscsi_action(struct cam_sim *sim, union ccb *ccb);
192009ea47eSEdward Tomasz Napierala static struct iscsi_outstanding *iscsi_outstanding_find(struct iscsi_session *is,
193009ea47eSEdward Tomasz Napierala uint32_t initiator_task_tag);
194aa75114cSAlexander Motin static struct iscsi_outstanding *iscsi_outstanding_add(struct iscsi_session *is,
195604c023fSEdward Tomasz Napierala struct icl_pdu *request, union ccb *ccb,
196604c023fSEdward Tomasz Napierala uint32_t *initiator_task_tagp);
197009ea47eSEdward Tomasz Napierala static void iscsi_outstanding_remove(struct iscsi_session *is,
198009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io);
199009ea47eSEdward Tomasz Napierala
200009ea47eSEdward Tomasz Napierala static bool
iscsi_pdu_prepare(struct icl_pdu * request)201009ea47eSEdward Tomasz Napierala iscsi_pdu_prepare(struct icl_pdu *request)
202009ea47eSEdward Tomasz Napierala {
203009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
204009ea47eSEdward Tomasz Napierala struct iscsi_bhs_scsi_command *bhssc;
205009ea47eSEdward Tomasz Napierala
206009ea47eSEdward Tomasz Napierala is = PDU_SESSION(request);
207009ea47eSEdward Tomasz Napierala
208009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
209009ea47eSEdward Tomasz Napierala
210009ea47eSEdward Tomasz Napierala /*
211009ea47eSEdward Tomasz Napierala * We're only using fields common for all the request
212009ea47eSEdward Tomasz Napierala * (initiator -> target) PDUs.
213009ea47eSEdward Tomasz Napierala */
214009ea47eSEdward Tomasz Napierala bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
215009ea47eSEdward Tomasz Napierala
216009ea47eSEdward Tomasz Napierala /*
217009ea47eSEdward Tomasz Napierala * Data-Out PDU does not contain CmdSN.
218009ea47eSEdward Tomasz Napierala */
219009ea47eSEdward Tomasz Napierala if (bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
2202124e3b0SAlexander Motin if (ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn) &&
221009ea47eSEdward Tomasz Napierala (bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
222009ea47eSEdward Tomasz Napierala /*
223009ea47eSEdward Tomasz Napierala * Current MaxCmdSN prevents us from sending any more
224009ea47eSEdward Tomasz Napierala * SCSI Command PDUs to the target; postpone the PDU.
225009ea47eSEdward Tomasz Napierala * It will get resent by either iscsi_pdu_queue(),
226009ea47eSEdward Tomasz Napierala * or by maintenance thread.
227009ea47eSEdward Tomasz Napierala */
228009ea47eSEdward Tomasz Napierala #if 0
2292124e3b0SAlexander Motin ISCSI_SESSION_DEBUG(is, "postponing send, CmdSN %u, "
2302124e3b0SAlexander Motin "ExpCmdSN %u, MaxCmdSN %u, opcode 0x%x",
2312124e3b0SAlexander Motin is->is_cmdsn, is->is_expcmdsn, is->is_maxcmdsn,
2322124e3b0SAlexander Motin bhssc->bhssc_opcode);
233009ea47eSEdward Tomasz Napierala #endif
234009ea47eSEdward Tomasz Napierala return (true);
235009ea47eSEdward Tomasz Napierala }
236009ea47eSEdward Tomasz Napierala bhssc->bhssc_cmdsn = htonl(is->is_cmdsn);
237009ea47eSEdward Tomasz Napierala if ((bhssc->bhssc_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
238009ea47eSEdward Tomasz Napierala is->is_cmdsn++;
239009ea47eSEdward Tomasz Napierala }
240009ea47eSEdward Tomasz Napierala bhssc->bhssc_expstatsn = htonl(is->is_statsn + 1);
241009ea47eSEdward Tomasz Napierala
242009ea47eSEdward Tomasz Napierala return (false);
243009ea47eSEdward Tomasz Napierala }
244009ea47eSEdward Tomasz Napierala
245009ea47eSEdward Tomasz Napierala static void
iscsi_session_send_postponed(struct iscsi_session * is)246009ea47eSEdward Tomasz Napierala iscsi_session_send_postponed(struct iscsi_session *is)
247009ea47eSEdward Tomasz Napierala {
248009ea47eSEdward Tomasz Napierala struct icl_pdu *request;
249009ea47eSEdward Tomasz Napierala bool postpone;
250009ea47eSEdward Tomasz Napierala
251009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
252009ea47eSEdward Tomasz Napierala
2534c9ea0ceSAlexander Motin if (STAILQ_EMPTY(&is->is_postponed))
2544c9ea0ceSAlexander Motin return;
2554c9ea0ceSAlexander Motin while ((request = STAILQ_FIRST(&is->is_postponed)) != NULL) {
256009ea47eSEdward Tomasz Napierala postpone = iscsi_pdu_prepare(request);
257009ea47eSEdward Tomasz Napierala if (postpone)
2584c9ea0ceSAlexander Motin return;
25916c158a5SEdward Tomasz Napierala STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
260009ea47eSEdward Tomasz Napierala icl_pdu_queue(request);
261009ea47eSEdward Tomasz Napierala }
2624c9ea0ceSAlexander Motin xpt_release_simq(is->is_sim, 1);
263009ea47eSEdward Tomasz Napierala }
264009ea47eSEdward Tomasz Napierala
265009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_queue_locked(struct icl_pdu * request)266009ea47eSEdward Tomasz Napierala iscsi_pdu_queue_locked(struct icl_pdu *request)
267009ea47eSEdward Tomasz Napierala {
268009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
269009ea47eSEdward Tomasz Napierala bool postpone;
270009ea47eSEdward Tomasz Napierala
271009ea47eSEdward Tomasz Napierala is = PDU_SESSION(request);
272009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
273009ea47eSEdward Tomasz Napierala iscsi_session_send_postponed(is);
274009ea47eSEdward Tomasz Napierala postpone = iscsi_pdu_prepare(request);
275009ea47eSEdward Tomasz Napierala if (postpone) {
2764c9ea0ceSAlexander Motin if (STAILQ_EMPTY(&is->is_postponed))
2774c9ea0ceSAlexander Motin xpt_freeze_simq(is->is_sim, 1);
27816c158a5SEdward Tomasz Napierala STAILQ_INSERT_TAIL(&is->is_postponed, request, ip_next);
279009ea47eSEdward Tomasz Napierala return;
280009ea47eSEdward Tomasz Napierala }
281009ea47eSEdward Tomasz Napierala icl_pdu_queue(request);
282009ea47eSEdward Tomasz Napierala }
283009ea47eSEdward Tomasz Napierala
284009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_queue(struct icl_pdu * request)285009ea47eSEdward Tomasz Napierala iscsi_pdu_queue(struct icl_pdu *request)
286009ea47eSEdward Tomasz Napierala {
287009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
288009ea47eSEdward Tomasz Napierala
289009ea47eSEdward Tomasz Napierala is = PDU_SESSION(request);
290009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
291009ea47eSEdward Tomasz Napierala iscsi_pdu_queue_locked(request);
292009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
293009ea47eSEdward Tomasz Napierala }
294009ea47eSEdward Tomasz Napierala
295009ea47eSEdward Tomasz Napierala static void
iscsi_session_logout(struct iscsi_session * is)296009ea47eSEdward Tomasz Napierala iscsi_session_logout(struct iscsi_session *is)
297009ea47eSEdward Tomasz Napierala {
298009ea47eSEdward Tomasz Napierala struct icl_pdu *request;
299009ea47eSEdward Tomasz Napierala struct iscsi_bhs_logout_request *bhslr;
300009ea47eSEdward Tomasz Napierala
301d2d52b00SEdward Tomasz Napierala request = icl_pdu_new(is->is_conn, M_NOWAIT);
302009ea47eSEdward Tomasz Napierala if (request == NULL)
303009ea47eSEdward Tomasz Napierala return;
304009ea47eSEdward Tomasz Napierala
305009ea47eSEdward Tomasz Napierala bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
306009ea47eSEdward Tomasz Napierala bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST;
307009ea47eSEdward Tomasz Napierala bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
308009ea47eSEdward Tomasz Napierala iscsi_pdu_queue_locked(request);
309009ea47eSEdward Tomasz Napierala }
310009ea47eSEdward Tomasz Napierala
311009ea47eSEdward Tomasz Napierala static void
iscsi_session_terminate_task(struct iscsi_session * is,struct iscsi_outstanding * io,cam_status status)312aa75114cSAlexander Motin iscsi_session_terminate_task(struct iscsi_session *is,
313ad10496cSAndriy Gapon struct iscsi_outstanding *io, cam_status status)
314aa75114cSAlexander Motin {
315aa75114cSAlexander Motin
316e204e2cdSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
317e204e2cdSEdward Tomasz Napierala
318aa75114cSAlexander Motin if (io->io_ccb != NULL) {
319aa75114cSAlexander Motin io->io_ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
320ad10496cSAndriy Gapon io->io_ccb->ccb_h.status |= status;
321aa75114cSAlexander Motin if ((io->io_ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
322aa75114cSAlexander Motin io->io_ccb->ccb_h.status |= CAM_DEV_QFRZN;
323aa75114cSAlexander Motin xpt_freeze_devq(io->io_ccb->ccb_h.path, 1);
324aa75114cSAlexander Motin ISCSI_SESSION_DEBUG(is, "freezing devq");
325aa75114cSAlexander Motin }
326aa75114cSAlexander Motin xpt_done(io->io_ccb);
327aa75114cSAlexander Motin }
328aa75114cSAlexander Motin iscsi_outstanding_remove(is, io);
329aa75114cSAlexander Motin }
330aa75114cSAlexander Motin
331aa75114cSAlexander Motin static void
iscsi_session_terminate_tasks(struct iscsi_session * is,cam_status status)332ad10496cSAndriy Gapon iscsi_session_terminate_tasks(struct iscsi_session *is, cam_status status)
333009ea47eSEdward Tomasz Napierala {
334009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io, *tmp;
335009ea47eSEdward Tomasz Napierala
336009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
337009ea47eSEdward Tomasz Napierala
338009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(io, &is->is_outstanding, io_next, tmp) {
339ad10496cSAndriy Gapon iscsi_session_terminate_task(is, io, status);
340009ea47eSEdward Tomasz Napierala }
341009ea47eSEdward Tomasz Napierala }
342009ea47eSEdward Tomasz Napierala
343009ea47eSEdward Tomasz Napierala static void
iscsi_session_cleanup(struct iscsi_session * is,bool destroy_sim)344f1f12e40SEdward Tomasz Napierala iscsi_session_cleanup(struct iscsi_session *is, bool destroy_sim)
345009ea47eSEdward Tomasz Napierala {
346009ea47eSEdward Tomasz Napierala struct icl_pdu *pdu;
347009ea47eSEdward Tomasz Napierala
348f1f12e40SEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
349009ea47eSEdward Tomasz Napierala
350009ea47eSEdward Tomasz Napierala /*
351009ea47eSEdward Tomasz Napierala * Don't queue any new PDUs.
352009ea47eSEdward Tomasz Napierala */
353009ea47eSEdward Tomasz Napierala if (is->is_sim != NULL && is->is_simq_frozen == false) {
354009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing");
355009ea47eSEdward Tomasz Napierala xpt_freeze_simq(is->is_sim, 1);
356009ea47eSEdward Tomasz Napierala is->is_simq_frozen = true;
357009ea47eSEdward Tomasz Napierala }
358009ea47eSEdward Tomasz Napierala
359009ea47eSEdward Tomasz Napierala /*
360009ea47eSEdward Tomasz Napierala * Remove postponed PDUs.
361009ea47eSEdward Tomasz Napierala */
3624c9ea0ceSAlexander Motin if (!STAILQ_EMPTY(&is->is_postponed))
3634c9ea0ceSAlexander Motin xpt_release_simq(is->is_sim, 1);
3644c9ea0ceSAlexander Motin while ((pdu = STAILQ_FIRST(&is->is_postponed)) != NULL) {
36516c158a5SEdward Tomasz Napierala STAILQ_REMOVE_HEAD(&is->is_postponed, ip_next);
366009ea47eSEdward Tomasz Napierala icl_pdu_free(pdu);
367009ea47eSEdward Tomasz Napierala }
368009ea47eSEdward Tomasz Napierala
369f1f12e40SEdward Tomasz Napierala if (destroy_sim == false) {
370009ea47eSEdward Tomasz Napierala /*
371009ea47eSEdward Tomasz Napierala * Terminate SCSI tasks, asking CAM to requeue them.
372009ea47eSEdward Tomasz Napierala */
373ad10496cSAndriy Gapon iscsi_session_terminate_tasks(is, CAM_REQUEUE_REQ);
374f1f12e40SEdward Tomasz Napierala return;
375f1f12e40SEdward Tomasz Napierala }
376f1f12e40SEdward Tomasz Napierala
377ad10496cSAndriy Gapon iscsi_session_terminate_tasks(is, CAM_DEV_NOT_THERE);
378f1f12e40SEdward Tomasz Napierala
379f1f12e40SEdward Tomasz Napierala if (is->is_sim == NULL)
380f1f12e40SEdward Tomasz Napierala return;
381f1f12e40SEdward Tomasz Napierala
382f1f12e40SEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "deregistering SIM");
383f1f12e40SEdward Tomasz Napierala xpt_async(AC_LOST_DEVICE, is->is_path, NULL);
384f1f12e40SEdward Tomasz Napierala
385f1f12e40SEdward Tomasz Napierala if (is->is_simq_frozen) {
386f1f12e40SEdward Tomasz Napierala is->is_simq_frozen = false;
387186bcdaaSEdward Tomasz Napierala xpt_release_simq(is->is_sim, 1);
388f1f12e40SEdward Tomasz Napierala }
389f1f12e40SEdward Tomasz Napierala
390f1f12e40SEdward Tomasz Napierala xpt_free_path(is->is_path);
391f1f12e40SEdward Tomasz Napierala is->is_path = NULL;
392f1f12e40SEdward Tomasz Napierala xpt_bus_deregister(cam_sim_path(is->is_sim));
393f1f12e40SEdward Tomasz Napierala cam_sim_free(is->is_sim, TRUE /*free_devq*/);
394f1f12e40SEdward Tomasz Napierala is->is_sim = NULL;
395f1f12e40SEdward Tomasz Napierala is->is_devq = NULL;
396f1f12e40SEdward Tomasz Napierala }
397f1f12e40SEdward Tomasz Napierala
398f1f12e40SEdward Tomasz Napierala static void
iscsi_maintenance_thread_reconnect(struct iscsi_session * is)399f1f12e40SEdward Tomasz Napierala iscsi_maintenance_thread_reconnect(struct iscsi_session *is)
400f1f12e40SEdward Tomasz Napierala {
401bd6bb493SRichard Scheffenegger /*
402bd6bb493SRichard Scheffenegger * As we will be reconnecting shortly,
403bd6bb493SRichard Scheffenegger * discard outstanding data immediately on
404bd6bb493SRichard Scheffenegger * close(), also notify peer via RST if
405bd6bb493SRichard Scheffenegger * any packets come in.
406bd6bb493SRichard Scheffenegger */
407bd6bb493SRichard Scheffenegger struct socket *so;
408bd6bb493SRichard Scheffenegger so = is->is_conn->ic_socket;
409bd6bb493SRichard Scheffenegger if (so != NULL) {
410bd6bb493SRichard Scheffenegger struct sockopt sopt;
411bd6bb493SRichard Scheffenegger struct linger sl;
412bd6bb493SRichard Scheffenegger sopt.sopt_dir = SOPT_SET;
413bd6bb493SRichard Scheffenegger sopt.sopt_level = SOL_SOCKET;
414bd6bb493SRichard Scheffenegger sopt.sopt_name = SO_LINGER;
415bd6bb493SRichard Scheffenegger sopt.sopt_val = &sl;
416bd6bb493SRichard Scheffenegger sopt.sopt_valsize = sizeof(sl);
417bd6bb493SRichard Scheffenegger sl.l_onoff = 1; /* non-zero value enables linger option in kernel */
418bd6bb493SRichard Scheffenegger sl.l_linger = 0; /* timeout interval in seconds */
419bd6bb493SRichard Scheffenegger sosetopt(is->is_conn->ic_socket, &sopt);
420bd6bb493SRichard Scheffenegger }
421f1f12e40SEdward Tomasz Napierala
422f1f12e40SEdward Tomasz Napierala icl_conn_close(is->is_conn);
423f1f12e40SEdward Tomasz Napierala
424f1f12e40SEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
425f1f12e40SEdward Tomasz Napierala
426f1f12e40SEdward Tomasz Napierala is->is_connected = false;
427f1f12e40SEdward Tomasz Napierala is->is_reconnecting = false;
428f1f12e40SEdward Tomasz Napierala is->is_login_phase = false;
429f1f12e40SEdward Tomasz Napierala
430f1f12e40SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
431f1f12e40SEdward Tomasz Napierala if (is->is_login_pdu != NULL) {
432f1f12e40SEdward Tomasz Napierala icl_pdu_free(is->is_login_pdu);
433f1f12e40SEdward Tomasz Napierala is->is_login_pdu = NULL;
434f1f12e40SEdward Tomasz Napierala }
435f1f12e40SEdward Tomasz Napierala cv_signal(&is->is_login_cv);
436f1f12e40SEdward Tomasz Napierala #endif
437f1f12e40SEdward Tomasz Napierala
438f1f12e40SEdward Tomasz Napierala if (fail_on_disconnection) {
439f1f12e40SEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "connection failed, destroying devices");
440f1f12e40SEdward Tomasz Napierala iscsi_session_cleanup(is, true);
441f1f12e40SEdward Tomasz Napierala } else {
442f1f12e40SEdward Tomasz Napierala iscsi_session_cleanup(is, false);
443f1f12e40SEdward Tomasz Napierala }
444009ea47eSEdward Tomasz Napierala
445009ea47eSEdward Tomasz Napierala KASSERT(TAILQ_EMPTY(&is->is_outstanding),
446009ea47eSEdward Tomasz Napierala ("destroying session with active tasks"));
44716c158a5SEdward Tomasz Napierala KASSERT(STAILQ_EMPTY(&is->is_postponed),
448009ea47eSEdward Tomasz Napierala ("destroying session with postponed PDUs"));
449009ea47eSEdward Tomasz Napierala
450ba165a31SEdward Tomasz Napierala if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
451ba165a31SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
452ba165a31SEdward Tomasz Napierala return;
453ba165a31SEdward Tomasz Napierala }
454ba165a31SEdward Tomasz Napierala
455009ea47eSEdward Tomasz Napierala /*
456009ea47eSEdward Tomasz Napierala * Request immediate reconnection from iscsid(8).
457009ea47eSEdward Tomasz Napierala */
458009ea47eSEdward Tomasz Napierala //ISCSI_SESSION_DEBUG(is, "waking up iscsid(8)");
459009ea47eSEdward Tomasz Napierala is->is_waiting_for_iscsid = true;
460009ea47eSEdward Tomasz Napierala strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
461009ea47eSEdward Tomasz Napierala is->is_timeout = 0;
462009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
463009ea47eSEdward Tomasz Napierala cv_signal(&is->is_softc->sc_cv);
464009ea47eSEdward Tomasz Napierala }
465009ea47eSEdward Tomasz Napierala
466009ea47eSEdward Tomasz Napierala static void
iscsi_maintenance_thread_terminate(struct iscsi_session * is)467009ea47eSEdward Tomasz Napierala iscsi_maintenance_thread_terminate(struct iscsi_session *is)
468009ea47eSEdward Tomasz Napierala {
469009ea47eSEdward Tomasz Napierala struct iscsi_softc *sc;
470009ea47eSEdward Tomasz Napierala
471009ea47eSEdward Tomasz Napierala sc = is->is_softc;
472009ea47eSEdward Tomasz Napierala sx_xlock(&sc->sc_lock);
47320e9cab5SAndriy Gapon TAILQ_REMOVE(&sc->sc_sessions, is, is_next);
47420e9cab5SAndriy Gapon sx_xunlock(&sc->sc_lock);
475009ea47eSEdward Tomasz Napierala
476009ea47eSEdward Tomasz Napierala icl_conn_close(is->is_conn);
477674074daSEdward Tomasz Napierala callout_drain(&is->is_callout);
478009ea47eSEdward Tomasz Napierala
479009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
480009ea47eSEdward Tomasz Napierala
481009ea47eSEdward Tomasz Napierala KASSERT(is->is_terminating, ("is_terminating == false"));
482009ea47eSEdward Tomasz Napierala
483009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
484009ea47eSEdward Tomasz Napierala if (is->is_login_pdu != NULL) {
485009ea47eSEdward Tomasz Napierala icl_pdu_free(is->is_login_pdu);
486009ea47eSEdward Tomasz Napierala is->is_login_pdu = NULL;
487009ea47eSEdward Tomasz Napierala }
488009ea47eSEdward Tomasz Napierala cv_signal(&is->is_login_cv);
489009ea47eSEdward Tomasz Napierala #endif
490009ea47eSEdward Tomasz Napierala
491f1f12e40SEdward Tomasz Napierala iscsi_session_cleanup(is, true);
492009ea47eSEdward Tomasz Napierala
493009ea47eSEdward Tomasz Napierala KASSERT(TAILQ_EMPTY(&is->is_outstanding),
494009ea47eSEdward Tomasz Napierala ("destroying session with active tasks"));
49516c158a5SEdward Tomasz Napierala KASSERT(STAILQ_EMPTY(&is->is_postponed),
496009ea47eSEdward Tomasz Napierala ("destroying session with postponed PDUs"));
497009ea47eSEdward Tomasz Napierala
498009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
499009ea47eSEdward Tomasz Napierala
500009ea47eSEdward Tomasz Napierala icl_conn_free(is->is_conn);
501009ea47eSEdward Tomasz Napierala mtx_destroy(&is->is_lock);
502009ea47eSEdward Tomasz Napierala cv_destroy(&is->is_maintenance_cv);
503009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
504009ea47eSEdward Tomasz Napierala cv_destroy(&is->is_login_cv);
505009ea47eSEdward Tomasz Napierala #endif
506481b36c6SSteven Hartland
507009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "terminated");
508009ea47eSEdward Tomasz Napierala free(is, M_ISCSI);
509009ea47eSEdward Tomasz Napierala
510009ea47eSEdward Tomasz Napierala /*
511009ea47eSEdward Tomasz Napierala * The iscsi_unload() routine might be waiting.
512009ea47eSEdward Tomasz Napierala */
513009ea47eSEdward Tomasz Napierala cv_signal(&sc->sc_cv);
514009ea47eSEdward Tomasz Napierala }
515009ea47eSEdward Tomasz Napierala
516009ea47eSEdward Tomasz Napierala static void
iscsi_maintenance_thread(void * arg)517009ea47eSEdward Tomasz Napierala iscsi_maintenance_thread(void *arg)
518009ea47eSEdward Tomasz Napierala {
5195b338bc0SAlexander Motin struct iscsi_session *is = arg;
520009ea47eSEdward Tomasz Napierala
521009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
5225b338bc0SAlexander Motin for (;;) {
523009ea47eSEdward Tomasz Napierala if (is->is_reconnecting == false &&
524009ea47eSEdward Tomasz Napierala is->is_terminating == false &&
5255b338bc0SAlexander Motin (STAILQ_EMPTY(&is->is_postponed) ||
5265b338bc0SAlexander Motin ISCSI_SNGT(is->is_cmdsn, is->is_maxcmdsn)))
527009ea47eSEdward Tomasz Napierala cv_wait(&is->is_maintenance_cv, &is->is_lock);
528009ea47eSEdward Tomasz Napierala
529481b36c6SSteven Hartland /* Terminate supersedes reconnect. */
530009ea47eSEdward Tomasz Napierala if (is->is_terminating) {
531009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
532009ea47eSEdward Tomasz Napierala iscsi_maintenance_thread_terminate(is);
533009ea47eSEdward Tomasz Napierala kthread_exit();
534009ea47eSEdward Tomasz Napierala return;
535009ea47eSEdward Tomasz Napierala }
536009ea47eSEdward Tomasz Napierala
537481b36c6SSteven Hartland if (is->is_reconnecting) {
538481b36c6SSteven Hartland ISCSI_SESSION_UNLOCK(is);
539481b36c6SSteven Hartland iscsi_maintenance_thread_reconnect(is);
5405b338bc0SAlexander Motin ISCSI_SESSION_LOCK(is);
541481b36c6SSteven Hartland continue;
542481b36c6SSteven Hartland }
543481b36c6SSteven Hartland
544009ea47eSEdward Tomasz Napierala iscsi_session_send_postponed(is);
545009ea47eSEdward Tomasz Napierala }
5465b338bc0SAlexander Motin ISCSI_SESSION_UNLOCK(is);
547009ea47eSEdward Tomasz Napierala }
548009ea47eSEdward Tomasz Napierala
549009ea47eSEdward Tomasz Napierala static void
iscsi_session_reconnect(struct iscsi_session * is)550009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(struct iscsi_session *is)
551009ea47eSEdward Tomasz Napierala {
552009ea47eSEdward Tomasz Napierala
553009ea47eSEdward Tomasz Napierala /*
554009ea47eSEdward Tomasz Napierala * XXX: We can't use locking here, because
555009ea47eSEdward Tomasz Napierala * it's being called from various contexts.
556009ea47eSEdward Tomasz Napierala * Hope it doesn't break anything.
557009ea47eSEdward Tomasz Napierala */
558009ea47eSEdward Tomasz Napierala if (is->is_reconnecting)
559009ea47eSEdward Tomasz Napierala return;
560009ea47eSEdward Tomasz Napierala
561009ea47eSEdward Tomasz Napierala is->is_reconnecting = true;
562009ea47eSEdward Tomasz Napierala cv_signal(&is->is_maintenance_cv);
563009ea47eSEdward Tomasz Napierala }
564009ea47eSEdward Tomasz Napierala
565009ea47eSEdward Tomasz Napierala static void
iscsi_session_terminate(struct iscsi_session * is)566009ea47eSEdward Tomasz Napierala iscsi_session_terminate(struct iscsi_session *is)
567009ea47eSEdward Tomasz Napierala {
568674074daSEdward Tomasz Napierala
569009ea47eSEdward Tomasz Napierala if (is->is_terminating)
570009ea47eSEdward Tomasz Napierala return;
571009ea47eSEdward Tomasz Napierala
572009ea47eSEdward Tomasz Napierala is->is_terminating = true;
573009ea47eSEdward Tomasz Napierala
574009ea47eSEdward Tomasz Napierala #if 0
575009ea47eSEdward Tomasz Napierala iscsi_session_logout(is);
576009ea47eSEdward Tomasz Napierala #endif
577009ea47eSEdward Tomasz Napierala cv_signal(&is->is_maintenance_cv);
578009ea47eSEdward Tomasz Napierala }
579009ea47eSEdward Tomasz Napierala
580009ea47eSEdward Tomasz Napierala static void
iscsi_callout(void * context)581009ea47eSEdward Tomasz Napierala iscsi_callout(void *context)
582009ea47eSEdward Tomasz Napierala {
583009ea47eSEdward Tomasz Napierala struct icl_pdu *request;
584009ea47eSEdward Tomasz Napierala struct iscsi_bhs_nop_out *bhsno;
585009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
586009ea47eSEdward Tomasz Napierala bool reconnect_needed = false;
587972a7d95SRichard Scheffenegger sbintime_t sbt, pr;
588009ea47eSEdward Tomasz Napierala
589009ea47eSEdward Tomasz Napierala is = context;
590009ea47eSEdward Tomasz Napierala
591674074daSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
592674074daSEdward Tomasz Napierala if (is->is_terminating) {
593674074daSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
594009ea47eSEdward Tomasz Napierala return;
595674074daSEdward Tomasz Napierala }
596009ea47eSEdward Tomasz Napierala
597972a7d95SRichard Scheffenegger sbt = mstosbt(995);
598972a7d95SRichard Scheffenegger pr = mstosbt(10);
599972a7d95SRichard Scheffenegger callout_schedule_sbt(&is->is_callout, sbt, pr, 0);
600009ea47eSEdward Tomasz Napierala
601ba165a31SEdward Tomasz Napierala if (is->is_conf.isc_enable == 0)
602ba165a31SEdward Tomasz Napierala goto out;
603ba165a31SEdward Tomasz Napierala
604009ea47eSEdward Tomasz Napierala is->is_timeout++;
605009ea47eSEdward Tomasz Napierala
606009ea47eSEdward Tomasz Napierala if (is->is_waiting_for_iscsid) {
6072c5b89e5SEdward Tomasz Napierala if (iscsid_timeout > 0 && is->is_timeout > iscsid_timeout) {
608009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "timed out waiting for iscsid(8) "
609009ea47eSEdward Tomasz Napierala "for %d seconds; reconnecting",
610009ea47eSEdward Tomasz Napierala is->is_timeout);
611009ea47eSEdward Tomasz Napierala reconnect_needed = true;
612009ea47eSEdward Tomasz Napierala }
613009ea47eSEdward Tomasz Napierala goto out;
614009ea47eSEdward Tomasz Napierala }
615009ea47eSEdward Tomasz Napierala
616009ea47eSEdward Tomasz Napierala if (is->is_login_phase) {
617bd6bb493SRichard Scheffenegger if (is->is_login_timeout > 0 && is->is_timeout > is->is_login_timeout) {
618009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "login timed out after %d seconds; "
619009ea47eSEdward Tomasz Napierala "reconnecting", is->is_timeout);
620009ea47eSEdward Tomasz Napierala reconnect_needed = true;
621009ea47eSEdward Tomasz Napierala }
622009ea47eSEdward Tomasz Napierala goto out;
623009ea47eSEdward Tomasz Napierala }
624009ea47eSEdward Tomasz Napierala
625bd6bb493SRichard Scheffenegger if (is->is_ping_timeout <= 0) {
6262c5b89e5SEdward Tomasz Napierala /*
6272c5b89e5SEdward Tomasz Napierala * Pings are disabled. Don't send NOP-Out in this case.
6282c5b89e5SEdward Tomasz Napierala * Reset the timeout, to avoid triggering reconnection,
6292c5b89e5SEdward Tomasz Napierala * should the user decide to reenable them.
6302c5b89e5SEdward Tomasz Napierala */
6312c5b89e5SEdward Tomasz Napierala is->is_timeout = 0;
6322c5b89e5SEdward Tomasz Napierala goto out;
6332c5b89e5SEdward Tomasz Napierala }
6342c5b89e5SEdward Tomasz Napierala
635bd6bb493SRichard Scheffenegger if (is->is_timeout >= is->is_ping_timeout) {
636009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "no ping reply (NOP-In) after %d seconds; "
637bd6bb493SRichard Scheffenegger "reconnecting", is->is_ping_timeout);
638009ea47eSEdward Tomasz Napierala reconnect_needed = true;
639009ea47eSEdward Tomasz Napierala goto out;
640009ea47eSEdward Tomasz Napierala }
641009ea47eSEdward Tomasz Napierala
642009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
643009ea47eSEdward Tomasz Napierala
644009ea47eSEdward Tomasz Napierala /*
645009ea47eSEdward Tomasz Napierala * If the ping was reset less than one second ago - which means
646009ea47eSEdward Tomasz Napierala * that we've received some PDU during the last second - assume
647009ea47eSEdward Tomasz Napierala * the traffic flows correctly and don't bother sending a NOP-Out.
648009ea47eSEdward Tomasz Napierala *
649009ea47eSEdward Tomasz Napierala * (It's 2 - one for one second, and one for incrementing is_timeout
650009ea47eSEdward Tomasz Napierala * earlier in this routine.)
651009ea47eSEdward Tomasz Napierala */
652009ea47eSEdward Tomasz Napierala if (is->is_timeout < 2)
653009ea47eSEdward Tomasz Napierala return;
654009ea47eSEdward Tomasz Napierala
655d2d52b00SEdward Tomasz Napierala request = icl_pdu_new(is->is_conn, M_NOWAIT);
65646aaea89SEdward Tomasz Napierala if (request == NULL) {
65746aaea89SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate PDU");
65846aaea89SEdward Tomasz Napierala return;
65946aaea89SEdward Tomasz Napierala }
660009ea47eSEdward Tomasz Napierala bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
661009ea47eSEdward Tomasz Napierala bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
662009ea47eSEdward Tomasz Napierala ISCSI_BHS_OPCODE_IMMEDIATE;
663009ea47eSEdward Tomasz Napierala bhsno->bhsno_flags = 0x80;
664009ea47eSEdward Tomasz Napierala bhsno->bhsno_target_transfer_tag = 0xffffffff;
665009ea47eSEdward Tomasz Napierala iscsi_pdu_queue(request);
666009ea47eSEdward Tomasz Napierala return;
667009ea47eSEdward Tomasz Napierala
668009ea47eSEdward Tomasz Napierala out:
669481b36c6SSteven Hartland if (is->is_terminating) {
670481b36c6SSteven Hartland ISCSI_SESSION_UNLOCK(is);
671481b36c6SSteven Hartland return;
672481b36c6SSteven Hartland }
673481b36c6SSteven Hartland
674009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
675009ea47eSEdward Tomasz Napierala
676009ea47eSEdward Tomasz Napierala if (reconnect_needed)
677009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
678009ea47eSEdward Tomasz Napierala }
679009ea47eSEdward Tomasz Napierala
680009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_update_statsn(const struct icl_pdu * response)681009ea47eSEdward Tomasz Napierala iscsi_pdu_update_statsn(const struct icl_pdu *response)
682009ea47eSEdward Tomasz Napierala {
683009ea47eSEdward Tomasz Napierala const struct iscsi_bhs_data_in *bhsdi;
684009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
6852124e3b0SAlexander Motin uint32_t expcmdsn, maxcmdsn, statsn;
686009ea47eSEdward Tomasz Napierala
687009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
688009ea47eSEdward Tomasz Napierala
689009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
690009ea47eSEdward Tomasz Napierala
691009ea47eSEdward Tomasz Napierala /*
692009ea47eSEdward Tomasz Napierala * We're only using fields common for all the response
693009ea47eSEdward Tomasz Napierala * (target -> initiator) PDUs.
694009ea47eSEdward Tomasz Napierala */
695009ea47eSEdward Tomasz Napierala bhsdi = (const struct iscsi_bhs_data_in *)response->ip_bhs;
696009ea47eSEdward Tomasz Napierala /*
697009ea47eSEdward Tomasz Napierala * Ok, I lied. In case of Data-In, "The fields StatSN, Status,
698009ea47eSEdward Tomasz Napierala * and Residual Count only have meaningful content if the S bit
699009ea47eSEdward Tomasz Napierala * is set to 1", so we also need to check the bit specific for
700009ea47eSEdward Tomasz Napierala * Data-In PDU.
701009ea47eSEdward Tomasz Napierala */
702009ea47eSEdward Tomasz Napierala if (bhsdi->bhsdi_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
703009ea47eSEdward Tomasz Napierala (bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0) {
7042124e3b0SAlexander Motin statsn = ntohl(bhsdi->bhsdi_statsn);
7052124e3b0SAlexander Motin if (statsn != is->is_statsn && statsn != (is->is_statsn + 1)) {
7062124e3b0SAlexander Motin /* XXX: This is normal situation for MCS */
7072124e3b0SAlexander Motin ISCSI_SESSION_WARN(is, "PDU 0x%x StatSN %u != "
7082124e3b0SAlexander Motin "session ExpStatSN %u (or + 1); reconnecting",
7092124e3b0SAlexander Motin bhsdi->bhsdi_opcode, statsn, is->is_statsn);
7102124e3b0SAlexander Motin iscsi_session_reconnect(is);
711009ea47eSEdward Tomasz Napierala }
7122124e3b0SAlexander Motin if (ISCSI_SNGT(statsn, is->is_statsn))
7132124e3b0SAlexander Motin is->is_statsn = statsn;
714009ea47eSEdward Tomasz Napierala }
715009ea47eSEdward Tomasz Napierala
716009ea47eSEdward Tomasz Napierala expcmdsn = ntohl(bhsdi->bhsdi_expcmdsn);
717009ea47eSEdward Tomasz Napierala maxcmdsn = ntohl(bhsdi->bhsdi_maxcmdsn);
718009ea47eSEdward Tomasz Napierala
7192124e3b0SAlexander Motin if (ISCSI_SNLT(maxcmdsn + 1, expcmdsn)) {
7202124e3b0SAlexander Motin ISCSI_SESSION_DEBUG(is,
7212124e3b0SAlexander Motin "PDU MaxCmdSN %u + 1 < PDU ExpCmdSN %u; ignoring",
722009ea47eSEdward Tomasz Napierala maxcmdsn, expcmdsn);
723009ea47eSEdward Tomasz Napierala } else {
7242124e3b0SAlexander Motin if (ISCSI_SNGT(maxcmdsn, is->is_maxcmdsn)) {
725009ea47eSEdward Tomasz Napierala is->is_maxcmdsn = maxcmdsn;
726009ea47eSEdward Tomasz Napierala
727009ea47eSEdward Tomasz Napierala /*
728009ea47eSEdward Tomasz Napierala * Command window increased; kick the maintanance thread
729009ea47eSEdward Tomasz Napierala * to send out postponed commands.
730009ea47eSEdward Tomasz Napierala */
73116c158a5SEdward Tomasz Napierala if (!STAILQ_EMPTY(&is->is_postponed))
732009ea47eSEdward Tomasz Napierala cv_signal(&is->is_maintenance_cv);
7332124e3b0SAlexander Motin } else if (ISCSI_SNLT(maxcmdsn, is->is_maxcmdsn)) {
7342124e3b0SAlexander Motin /* XXX: This is normal situation for MCS */
7352124e3b0SAlexander Motin ISCSI_SESSION_DEBUG(is,
7362124e3b0SAlexander Motin "PDU MaxCmdSN %u < session MaxCmdSN %u; ignoring",
737009ea47eSEdward Tomasz Napierala maxcmdsn, is->is_maxcmdsn);
738009ea47eSEdward Tomasz Napierala }
739009ea47eSEdward Tomasz Napierala
7402124e3b0SAlexander Motin if (ISCSI_SNGT(expcmdsn, is->is_expcmdsn)) {
741009ea47eSEdward Tomasz Napierala is->is_expcmdsn = expcmdsn;
7422124e3b0SAlexander Motin } else if (ISCSI_SNLT(expcmdsn, is->is_expcmdsn)) {
7432124e3b0SAlexander Motin /* XXX: This is normal situation for MCS */
7442124e3b0SAlexander Motin ISCSI_SESSION_DEBUG(is,
7452124e3b0SAlexander Motin "PDU ExpCmdSN %u < session ExpCmdSN %u; ignoring",
746009ea47eSEdward Tomasz Napierala expcmdsn, is->is_expcmdsn);
747009ea47eSEdward Tomasz Napierala }
748009ea47eSEdward Tomasz Napierala }
749009ea47eSEdward Tomasz Napierala
750009ea47eSEdward Tomasz Napierala /*
751009ea47eSEdward Tomasz Napierala * Every incoming PDU - not just NOP-In - resets the ping timer.
752009ea47eSEdward Tomasz Napierala * The purpose of the timeout is to reset the connection when it stalls;
753009ea47eSEdward Tomasz Napierala * we don't want this to happen when NOP-In or NOP-Out ends up delayed
754009ea47eSEdward Tomasz Napierala * in some queue.
755009ea47eSEdward Tomasz Napierala */
756009ea47eSEdward Tomasz Napierala is->is_timeout = 0;
757009ea47eSEdward Tomasz Napierala }
758009ea47eSEdward Tomasz Napierala
759009ea47eSEdward Tomasz Napierala static void
iscsi_receive_callback(struct icl_pdu * response)760009ea47eSEdward Tomasz Napierala iscsi_receive_callback(struct icl_pdu *response)
761009ea47eSEdward Tomasz Napierala {
762009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
763009ea47eSEdward Tomasz Napierala
764009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
765009ea47eSEdward Tomasz Napierala
766009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
767009ea47eSEdward Tomasz Napierala
768d66a906bSEdward Tomasz Napierala iscsi_pdu_update_statsn(response);
769d66a906bSEdward Tomasz Napierala
770009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
771009ea47eSEdward Tomasz Napierala if (is->is_login_phase) {
772009ea47eSEdward Tomasz Napierala if (is->is_login_pdu == NULL)
773009ea47eSEdward Tomasz Napierala is->is_login_pdu = response;
774009ea47eSEdward Tomasz Napierala else
775009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
776009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
777009ea47eSEdward Tomasz Napierala cv_signal(&is->is_login_cv);
778009ea47eSEdward Tomasz Napierala return;
779009ea47eSEdward Tomasz Napierala }
780009ea47eSEdward Tomasz Napierala #endif
781009ea47eSEdward Tomasz Napierala
782009ea47eSEdward Tomasz Napierala /*
783009ea47eSEdward Tomasz Napierala * The handling routine is responsible for freeing the PDU
784009ea47eSEdward Tomasz Napierala * when it's no longer needed.
785009ea47eSEdward Tomasz Napierala */
786009ea47eSEdward Tomasz Napierala switch (response->ip_bhs->bhs_opcode) {
787009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_NOP_IN:
788009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_nop_in(response);
789c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
790009ea47eSEdward Tomasz Napierala break;
791009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_SCSI_RESPONSE:
792009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_scsi_response(response);
793c215ad3aSAlexander Motin /* Session lock dropped inside. */
794c215ad3aSAlexander Motin ISCSI_SESSION_LOCK_ASSERT_NOT(is);
795009ea47eSEdward Tomasz Napierala break;
796aa75114cSAlexander Motin case ISCSI_BHS_OPCODE_TASK_RESPONSE:
797aa75114cSAlexander Motin iscsi_pdu_handle_task_response(response);
798c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
799aa75114cSAlexander Motin break;
800009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_SCSI_DATA_IN:
801009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_data_in(response);
802c215ad3aSAlexander Motin /* Session lock dropped inside. */
803c215ad3aSAlexander Motin ISCSI_SESSION_LOCK_ASSERT_NOT(is);
804009ea47eSEdward Tomasz Napierala break;
805009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_LOGOUT_RESPONSE:
806009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_logout_response(response);
807c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
808009ea47eSEdward Tomasz Napierala break;
809009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_R2T:
810009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_r2t(response);
811c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
812009ea47eSEdward Tomasz Napierala break;
813009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_ASYNC_MESSAGE:
814009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_async_message(response);
815c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
816009ea47eSEdward Tomasz Napierala break;
817009ea47eSEdward Tomasz Napierala case ISCSI_BHS_OPCODE_REJECT:
818009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_reject(response);
819c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
820009ea47eSEdward Tomasz Napierala break;
821009ea47eSEdward Tomasz Napierala default:
822009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "received PDU with unsupported "
823009ea47eSEdward Tomasz Napierala "opcode 0x%x; reconnecting",
824009ea47eSEdward Tomasz Napierala response->ip_bhs->bhs_opcode);
825009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
826c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
827009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
828009ea47eSEdward Tomasz Napierala }
829009ea47eSEdward Tomasz Napierala }
830009ea47eSEdward Tomasz Napierala
831009ea47eSEdward Tomasz Napierala static void
iscsi_error_callback(struct icl_conn * ic)832009ea47eSEdward Tomasz Napierala iscsi_error_callback(struct icl_conn *ic)
833009ea47eSEdward Tomasz Napierala {
834009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
835009ea47eSEdward Tomasz Napierala
836009ea47eSEdward Tomasz Napierala is = CONN_SESSION(ic);
837009ea47eSEdward Tomasz Napierala
838009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "connection error; reconnecting");
839009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
840009ea47eSEdward Tomasz Napierala }
841009ea47eSEdward Tomasz Napierala
842009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_nop_in(struct icl_pdu * response)843009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_nop_in(struct icl_pdu *response)
844009ea47eSEdward Tomasz Napierala {
8451008ac5eSEdward Tomasz Napierala struct iscsi_session *is;
846009ea47eSEdward Tomasz Napierala struct iscsi_bhs_nop_out *bhsno;
847009ea47eSEdward Tomasz Napierala struct iscsi_bhs_nop_in *bhsni;
848009ea47eSEdward Tomasz Napierala struct icl_pdu *request;
8491008ac5eSEdward Tomasz Napierala void *data = NULL;
8501008ac5eSEdward Tomasz Napierala size_t datasize;
8511008ac5eSEdward Tomasz Napierala int error;
852009ea47eSEdward Tomasz Napierala
8531008ac5eSEdward Tomasz Napierala is = PDU_SESSION(response);
854009ea47eSEdward Tomasz Napierala bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
855009ea47eSEdward Tomasz Napierala
856009ea47eSEdward Tomasz Napierala if (bhsni->bhsni_target_transfer_tag == 0xffffffff) {
857009ea47eSEdward Tomasz Napierala /*
858009ea47eSEdward Tomasz Napierala * Nothing to do; iscsi_pdu_update_statsn() already
859009ea47eSEdward Tomasz Napierala * zeroed the timeout.
860009ea47eSEdward Tomasz Napierala */
861009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
862009ea47eSEdward Tomasz Napierala return;
863009ea47eSEdward Tomasz Napierala }
864009ea47eSEdward Tomasz Napierala
8651008ac5eSEdward Tomasz Napierala datasize = icl_pdu_data_segment_length(response);
8661008ac5eSEdward Tomasz Napierala if (datasize > 0) {
8671008ac5eSEdward Tomasz Napierala data = malloc(datasize, M_ISCSI, M_NOWAIT | M_ZERO);
8681008ac5eSEdward Tomasz Napierala if (data == NULL) {
8691008ac5eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate memory; "
8701008ac5eSEdward Tomasz Napierala "reconnecting");
8711008ac5eSEdward Tomasz Napierala icl_pdu_free(response);
8721008ac5eSEdward Tomasz Napierala iscsi_session_reconnect(is);
8731008ac5eSEdward Tomasz Napierala return;
8741008ac5eSEdward Tomasz Napierala }
8751008ac5eSEdward Tomasz Napierala icl_pdu_get_data(response, 0, data, datasize);
8761008ac5eSEdward Tomasz Napierala }
8771008ac5eSEdward Tomasz Napierala
878d2d52b00SEdward Tomasz Napierala request = icl_pdu_new(response->ip_conn, M_NOWAIT);
879009ea47eSEdward Tomasz Napierala if (request == NULL) {
8801008ac5eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate memory; "
8811008ac5eSEdward Tomasz Napierala "reconnecting");
8821008ac5eSEdward Tomasz Napierala free(data, M_ISCSI);
883009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
8841008ac5eSEdward Tomasz Napierala iscsi_session_reconnect(is);
885009ea47eSEdward Tomasz Napierala return;
886009ea47eSEdward Tomasz Napierala }
887009ea47eSEdward Tomasz Napierala bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
888009ea47eSEdward Tomasz Napierala bhsno->bhsno_opcode = ISCSI_BHS_OPCODE_NOP_OUT |
889009ea47eSEdward Tomasz Napierala ISCSI_BHS_OPCODE_IMMEDIATE;
890009ea47eSEdward Tomasz Napierala bhsno->bhsno_flags = 0x80;
8911008ac5eSEdward Tomasz Napierala bhsno->bhsno_initiator_task_tag = 0xffffffff;
892009ea47eSEdward Tomasz Napierala bhsno->bhsno_target_transfer_tag = bhsni->bhsni_target_transfer_tag;
8931008ac5eSEdward Tomasz Napierala if (datasize > 0) {
8941008ac5eSEdward Tomasz Napierala error = icl_pdu_append_data(request, data, datasize, M_NOWAIT);
8951008ac5eSEdward Tomasz Napierala if (error != 0) {
8961008ac5eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate memory; "
8971008ac5eSEdward Tomasz Napierala "reconnecting");
8981008ac5eSEdward Tomasz Napierala free(data, M_ISCSI);
8991008ac5eSEdward Tomasz Napierala icl_pdu_free(request);
9001008ac5eSEdward Tomasz Napierala icl_pdu_free(response);
9011008ac5eSEdward Tomasz Napierala iscsi_session_reconnect(is);
9021008ac5eSEdward Tomasz Napierala return;
9031008ac5eSEdward Tomasz Napierala }
9041008ac5eSEdward Tomasz Napierala free(data, M_ISCSI);
9051008ac5eSEdward Tomasz Napierala }
906009ea47eSEdward Tomasz Napierala
907009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
908009ea47eSEdward Tomasz Napierala iscsi_pdu_queue_locked(request);
909009ea47eSEdward Tomasz Napierala }
910009ea47eSEdward Tomasz Napierala
911009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_scsi_response(struct icl_pdu * response)912009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_scsi_response(struct icl_pdu *response)
913009ea47eSEdward Tomasz Napierala {
914009ea47eSEdward Tomasz Napierala struct iscsi_bhs_scsi_response *bhssr;
915009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io;
916009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
91773e2e95dSAlexander Motin union ccb *ccb;
918009ea47eSEdward Tomasz Napierala struct ccb_scsiio *csio;
91973e2e95dSAlexander Motin size_t data_segment_len, received;
920009ea47eSEdward Tomasz Napierala uint16_t sense_len;
921bcec64bcSEdward Tomasz Napierala uint32_t resid;
922009ea47eSEdward Tomasz Napierala
923009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
924009ea47eSEdward Tomasz Napierala
925009ea47eSEdward Tomasz Napierala bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
926009ea47eSEdward Tomasz Napierala io = iscsi_outstanding_find(is, bhssr->bhssr_initiator_task_tag);
927aa75114cSAlexander Motin if (io == NULL || io->io_ccb == NULL) {
928009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhssr->bhssr_initiator_task_tag);
929009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
930009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
931c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
932009ea47eSEdward Tomasz Napierala return;
933009ea47eSEdward Tomasz Napierala }
934009ea47eSEdward Tomasz Napierala
93573e2e95dSAlexander Motin ccb = io->io_ccb;
936cdbc4a07SJohn Baldwin if (bhssr->bhssr_response == BHSSR_RESPONSE_COMMAND_COMPLETED) {
9374f0f5bf9SJohn Baldwin if (ntohl(bhssr->bhssr_expdatasn) != io->io_datasn) {
9384f0f5bf9SJohn Baldwin ISCSI_SESSION_WARN(is,
9394f0f5bf9SJohn Baldwin "ExpDataSN mismatch in SCSI Response (%u vs %u)",
9404f0f5bf9SJohn Baldwin ntohl(bhssr->bhssr_expdatasn), io->io_datasn);
941cdbc4a07SJohn Baldwin
942cdbc4a07SJohn Baldwin /*
943cdbc4a07SJohn Baldwin * XXX: Permit an ExpDataSN of zero for errors.
944cdbc4a07SJohn Baldwin *
945cdbc4a07SJohn Baldwin * This doesn't conform to RFC 7143, but some
946cdbc4a07SJohn Baldwin * targets seem to do this.
947cdbc4a07SJohn Baldwin */
948cdbc4a07SJohn Baldwin if (bhssr->bhssr_status != 0 &&
949cdbc4a07SJohn Baldwin bhssr->bhssr_expdatasn == htonl(0))
950cdbc4a07SJohn Baldwin goto skip_expdatasn;
951cdbc4a07SJohn Baldwin
9524f0f5bf9SJohn Baldwin icl_pdu_free(response);
9534f0f5bf9SJohn Baldwin iscsi_session_reconnect(is);
9544f0f5bf9SJohn Baldwin ISCSI_SESSION_UNLOCK(is);
9554f0f5bf9SJohn Baldwin return;
9564f0f5bf9SJohn Baldwin }
957cdbc4a07SJohn Baldwin } else {
958cdbc4a07SJohn Baldwin if (bhssr->bhssr_expdatasn != htonl(0)) {
959cdbc4a07SJohn Baldwin ISCSI_SESSION_WARN(is,
960cdbc4a07SJohn Baldwin "ExpDataSN mismatch in SCSI Response (%u vs 0)",
961cdbc4a07SJohn Baldwin ntohl(bhssr->bhssr_expdatasn));
962cdbc4a07SJohn Baldwin icl_pdu_free(response);
963cdbc4a07SJohn Baldwin iscsi_session_reconnect(is);
964cdbc4a07SJohn Baldwin ISCSI_SESSION_UNLOCK(is);
965cdbc4a07SJohn Baldwin return;
966cdbc4a07SJohn Baldwin }
967cdbc4a07SJohn Baldwin }
968cdbc4a07SJohn Baldwin skip_expdatasn:
969bcec64bcSEdward Tomasz Napierala
970bcec64bcSEdward Tomasz Napierala /*
971bcec64bcSEdward Tomasz Napierala * With iSER, after getting good response we can be sure
972bcec64bcSEdward Tomasz Napierala * that all the data has been successfully transferred.
973bcec64bcSEdward Tomasz Napierala */
974bcec64bcSEdward Tomasz Napierala if (is->is_conn->ic_iser) {
975bcec64bcSEdward Tomasz Napierala resid = ntohl(bhssr->bhssr_residual_count);
976bcec64bcSEdward Tomasz Napierala if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW) {
977bcec64bcSEdward Tomasz Napierala io->io_received = ccb->csio.dxfer_len - resid;
978bcec64bcSEdward Tomasz Napierala } else if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_OVERFLOW) {
979bcec64bcSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "overflow: target indicates %d", resid);
980bcec64bcSEdward Tomasz Napierala } else {
981bcec64bcSEdward Tomasz Napierala io->io_received = ccb->csio.dxfer_len;
982bcec64bcSEdward Tomasz Napierala }
983bcec64bcSEdward Tomasz Napierala }
984bcec64bcSEdward Tomasz Napierala
98573e2e95dSAlexander Motin received = io->io_received;
986c215ad3aSAlexander Motin iscsi_outstanding_remove(is, io);
987c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
988c215ad3aSAlexander Motin
989009ea47eSEdward Tomasz Napierala if (bhssr->bhssr_response != BHSSR_RESPONSE_COMMAND_COMPLETED) {
990009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "service response 0x%x", bhssr->bhssr_response);
99173e2e95dSAlexander Motin if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
99273e2e95dSAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1);
993009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
994009ea47eSEdward Tomasz Napierala }
99573e2e95dSAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
996009ea47eSEdward Tomasz Napierala } else if (bhssr->bhssr_status == 0) {
99773e2e95dSAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP;
998009ea47eSEdward Tomasz Napierala } else {
99973e2e95dSAlexander Motin if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
100073e2e95dSAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1);
1001009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
1002009ea47eSEdward Tomasz Napierala }
100373e2e95dSAlexander Motin ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
100473e2e95dSAlexander Motin ccb->csio.scsi_status = bhssr->bhssr_status;
1005009ea47eSEdward Tomasz Napierala }
1006009ea47eSEdward Tomasz Napierala
100773e2e95dSAlexander Motin csio = &ccb->csio;
1008009ea47eSEdward Tomasz Napierala data_segment_len = icl_pdu_data_segment_length(response);
1009009ea47eSEdward Tomasz Napierala if (data_segment_len > 0) {
1010009ea47eSEdward Tomasz Napierala if (data_segment_len < sizeof(sense_len)) {
1011009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "truncated data segment (%zd bytes)",
1012009ea47eSEdward Tomasz Napierala data_segment_len);
101373e2e95dSAlexander Motin if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
101473e2e95dSAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1);
1015009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
1016009ea47eSEdward Tomasz Napierala }
101773e2e95dSAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
1018009ea47eSEdward Tomasz Napierala goto out;
1019009ea47eSEdward Tomasz Napierala }
1020009ea47eSEdward Tomasz Napierala icl_pdu_get_data(response, 0, &sense_len, sizeof(sense_len));
1021009ea47eSEdward Tomasz Napierala sense_len = ntohs(sense_len);
1022009ea47eSEdward Tomasz Napierala #if 0
1023009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "sense_len %d, data len %zd",
1024009ea47eSEdward Tomasz Napierala sense_len, data_segment_len);
1025009ea47eSEdward Tomasz Napierala #endif
1026009ea47eSEdward Tomasz Napierala if (sizeof(sense_len) + sense_len > data_segment_len) {
1027009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "truncated data segment "
1028009ea47eSEdward Tomasz Napierala "(%zd bytes, should be %zd)",
1029009ea47eSEdward Tomasz Napierala data_segment_len, sizeof(sense_len) + sense_len);
103073e2e95dSAlexander Motin if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
103173e2e95dSAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1);
1032009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
1033009ea47eSEdward Tomasz Napierala }
103473e2e95dSAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP_ERR | CAM_DEV_QFRZN;
1035009ea47eSEdward Tomasz Napierala goto out;
1036009ea47eSEdward Tomasz Napierala } else if (sizeof(sense_len) + sense_len < data_segment_len)
1037009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "oversize data segment "
1038009ea47eSEdward Tomasz Napierala "(%zd bytes, should be %zd)",
1039009ea47eSEdward Tomasz Napierala data_segment_len, sizeof(sense_len) + sense_len);
1040009ea47eSEdward Tomasz Napierala if (sense_len > csio->sense_len) {
1041009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "truncating sense from %d to %d",
1042009ea47eSEdward Tomasz Napierala sense_len, csio->sense_len);
1043009ea47eSEdward Tomasz Napierala sense_len = csio->sense_len;
1044009ea47eSEdward Tomasz Napierala }
1045009ea47eSEdward Tomasz Napierala icl_pdu_get_data(response, sizeof(sense_len), &csio->sense_data, sense_len);
1046009ea47eSEdward Tomasz Napierala csio->sense_resid = csio->sense_len - sense_len;
104773e2e95dSAlexander Motin ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1048009ea47eSEdward Tomasz Napierala }
1049009ea47eSEdward Tomasz Napierala
1050009ea47eSEdward Tomasz Napierala out:
1051009ea47eSEdward Tomasz Napierala if (bhssr->bhssr_flags & BHSSR_FLAGS_RESIDUAL_UNDERFLOW)
1052009ea47eSEdward Tomasz Napierala csio->resid = ntohl(bhssr->bhssr_residual_count);
1053009ea47eSEdward Tomasz Napierala
1054009ea47eSEdward Tomasz Napierala if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
105573e2e95dSAlexander Motin KASSERT(received <= csio->dxfer_len,
105673e2e95dSAlexander Motin ("received > csio->dxfer_len"));
105773e2e95dSAlexander Motin if (received < csio->dxfer_len) {
105873e2e95dSAlexander Motin if (csio->resid != csio->dxfer_len - received) {
1059009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "underflow mismatch: "
1060009ea47eSEdward Tomasz Napierala "target indicates %d, we calculated %zd",
106173e2e95dSAlexander Motin csio->resid, csio->dxfer_len - received);
1062009ea47eSEdward Tomasz Napierala }
106373e2e95dSAlexander Motin csio->resid = csio->dxfer_len - received;
1064009ea47eSEdward Tomasz Napierala }
1065009ea47eSEdward Tomasz Napierala }
1066009ea47eSEdward Tomasz Napierala
106773e2e95dSAlexander Motin xpt_done(ccb);
1068009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1069009ea47eSEdward Tomasz Napierala }
1070009ea47eSEdward Tomasz Napierala
1071009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_task_response(struct icl_pdu * response)1072aa75114cSAlexander Motin iscsi_pdu_handle_task_response(struct icl_pdu *response)
1073aa75114cSAlexander Motin {
1074aa75114cSAlexander Motin struct iscsi_bhs_task_management_response *bhstmr;
1075aa75114cSAlexander Motin struct iscsi_outstanding *io, *aio;
1076aa75114cSAlexander Motin struct iscsi_session *is;
1077aa75114cSAlexander Motin
1078aa75114cSAlexander Motin is = PDU_SESSION(response);
1079aa75114cSAlexander Motin
1080aa75114cSAlexander Motin bhstmr = (struct iscsi_bhs_task_management_response *)response->ip_bhs;
1081aa75114cSAlexander Motin io = iscsi_outstanding_find(is, bhstmr->bhstmr_initiator_task_tag);
1082aa75114cSAlexander Motin if (io == NULL || io->io_ccb != NULL) {
1083aa75114cSAlexander Motin ISCSI_SESSION_WARN(is, "bad itt 0x%x",
1084aa75114cSAlexander Motin bhstmr->bhstmr_initiator_task_tag);
1085aa75114cSAlexander Motin icl_pdu_free(response);
1086aa75114cSAlexander Motin iscsi_session_reconnect(is);
1087aa75114cSAlexander Motin return;
1088aa75114cSAlexander Motin }
1089aa75114cSAlexander Motin
1090aa75114cSAlexander Motin if (bhstmr->bhstmr_response != BHSTMR_RESPONSE_FUNCTION_COMPLETE) {
1091aa75114cSAlexander Motin ISCSI_SESSION_WARN(is, "task response 0x%x",
1092aa75114cSAlexander Motin bhstmr->bhstmr_response);
1093aa75114cSAlexander Motin } else {
10943dd2a7a5SAlexander Motin aio = iscsi_outstanding_find(is, io->io_referenced_task_tag);
1095aa75114cSAlexander Motin if (aio != NULL && aio->io_ccb != NULL)
1096ad10496cSAndriy Gapon iscsi_session_terminate_task(is, aio, CAM_REQ_ABORTED);
1097aa75114cSAlexander Motin }
1098aa75114cSAlexander Motin
1099aa75114cSAlexander Motin iscsi_outstanding_remove(is, io);
1100aa75114cSAlexander Motin icl_pdu_free(response);
1101aa75114cSAlexander Motin }
1102aa75114cSAlexander Motin
1103aa75114cSAlexander Motin static void
iscsi_pdu_get_data_csio(struct icl_pdu * response,size_t pdu_offset,struct ccb_scsiio * csio,size_t oreceived,size_t data_segment_len)11047aab9c14SJohn Baldwin iscsi_pdu_get_data_csio(struct icl_pdu *response, size_t pdu_offset,
11057aab9c14SJohn Baldwin struct ccb_scsiio *csio, size_t oreceived, size_t data_segment_len)
11067aab9c14SJohn Baldwin {
11077aab9c14SJohn Baldwin switch (csio->ccb_h.flags & CAM_DATA_MASK) {
11087aab9c14SJohn Baldwin case CAM_DATA_BIO:
11097aab9c14SJohn Baldwin icl_pdu_get_bio(response, pdu_offset,
11107aab9c14SJohn Baldwin (struct bio *)csio->data_ptr, oreceived, data_segment_len);
11117aab9c14SJohn Baldwin break;
11127aab9c14SJohn Baldwin case CAM_DATA_VADDR:
11137aab9c14SJohn Baldwin icl_pdu_get_data(response, pdu_offset,
11147aab9c14SJohn Baldwin csio->data_ptr + oreceived, data_segment_len);
11157aab9c14SJohn Baldwin break;
11167aab9c14SJohn Baldwin default:
11177aab9c14SJohn Baldwin __assert_unreachable();
11187aab9c14SJohn Baldwin }
11197aab9c14SJohn Baldwin }
11207aab9c14SJohn Baldwin
11217aab9c14SJohn Baldwin static void
iscsi_pdu_handle_data_in(struct icl_pdu * response)1122009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_data_in(struct icl_pdu *response)
1123009ea47eSEdward Tomasz Napierala {
1124009ea47eSEdward Tomasz Napierala struct iscsi_bhs_data_in *bhsdi;
1125009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io;
1126009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
112773e2e95dSAlexander Motin union ccb *ccb;
1128009ea47eSEdward Tomasz Napierala struct ccb_scsiio *csio;
112973e2e95dSAlexander Motin size_t data_segment_len, received, oreceived;
1130009ea47eSEdward Tomasz Napierala
1131009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
1132009ea47eSEdward Tomasz Napierala bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
1133009ea47eSEdward Tomasz Napierala io = iscsi_outstanding_find(is, bhsdi->bhsdi_initiator_task_tag);
1134aa75114cSAlexander Motin if (io == NULL || io->io_ccb == NULL) {
1135009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "bad itt 0x%x", bhsdi->bhsdi_initiator_task_tag);
1136009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1137009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1138c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
1139009ea47eSEdward Tomasz Napierala return;
1140009ea47eSEdward Tomasz Napierala }
1141009ea47eSEdward Tomasz Napierala
11424f0f5bf9SJohn Baldwin if (io->io_datasn != ntohl(bhsdi->bhsdi_datasn)) {
11434f0f5bf9SJohn Baldwin ISCSI_SESSION_WARN(is, "received Data-In PDU with "
11444f0f5bf9SJohn Baldwin "DataSN %u, while expected %u; dropping connection",
11454f0f5bf9SJohn Baldwin ntohl(bhsdi->bhsdi_datasn), io->io_datasn);
11464f0f5bf9SJohn Baldwin icl_pdu_free(response);
11474f0f5bf9SJohn Baldwin iscsi_session_reconnect(is);
11484f0f5bf9SJohn Baldwin ISCSI_SESSION_UNLOCK(is);
11494f0f5bf9SJohn Baldwin return;
11504f0f5bf9SJohn Baldwin }
11514f0f5bf9SJohn Baldwin io->io_datasn += response->ip_additional_pdus + 1;
11524f0f5bf9SJohn Baldwin
1153009ea47eSEdward Tomasz Napierala data_segment_len = icl_pdu_data_segment_length(response);
1154009ea47eSEdward Tomasz Napierala if (data_segment_len == 0) {
1155009ea47eSEdward Tomasz Napierala /*
1156009ea47eSEdward Tomasz Napierala * "The sending of 0 length data segments should be avoided,
1157009ea47eSEdward Tomasz Napierala * but initiators and targets MUST be able to properly receive
1158009ea47eSEdward Tomasz Napierala * 0 length data segments."
1159009ea47eSEdward Tomasz Napierala */
1160c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
1161009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1162009ea47eSEdward Tomasz Napierala return;
1163009ea47eSEdward Tomasz Napierala }
1164009ea47eSEdward Tomasz Napierala
1165009ea47eSEdward Tomasz Napierala /*
1166009ea47eSEdward Tomasz Napierala * We need to track this for security reasons - without it, malicious target
1167009ea47eSEdward Tomasz Napierala * could respond to SCSI READ without sending Data-In PDUs, which would result
1168009ea47eSEdward Tomasz Napierala * in read operation on the initiator side returning random kernel data.
1169009ea47eSEdward Tomasz Napierala */
1170009ea47eSEdward Tomasz Napierala if (ntohl(bhsdi->bhsdi_buffer_offset) != io->io_received) {
1171009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "data out of order; expected offset %zd, got %zd",
1172009ea47eSEdward Tomasz Napierala io->io_received, (size_t)ntohl(bhsdi->bhsdi_buffer_offset));
1173009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1174009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1175c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
1176009ea47eSEdward Tomasz Napierala return;
1177009ea47eSEdward Tomasz Napierala }
1178009ea47eSEdward Tomasz Napierala
117973e2e95dSAlexander Motin ccb = io->io_ccb;
118073e2e95dSAlexander Motin csio = &ccb->csio;
1181009ea47eSEdward Tomasz Napierala
1182d43c9ec1SEdward Tomasz Napierala if (io->io_received + data_segment_len > csio->dxfer_len) {
1183009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "oversize data segment (%zd bytes "
1184d43c9ec1SEdward Tomasz Napierala "at offset %zd, buffer is %d)",
1185d43c9ec1SEdward Tomasz Napierala data_segment_len, io->io_received, csio->dxfer_len);
1186009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1187009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1188c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
1189009ea47eSEdward Tomasz Napierala return;
1190009ea47eSEdward Tomasz Napierala }
1191009ea47eSEdward Tomasz Napierala
119273e2e95dSAlexander Motin oreceived = io->io_received;
1193009ea47eSEdward Tomasz Napierala io->io_received += data_segment_len;
119473e2e95dSAlexander Motin received = io->io_received;
1195c215ad3aSAlexander Motin if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) != 0)
1196c215ad3aSAlexander Motin iscsi_outstanding_remove(is, io);
1197c215ad3aSAlexander Motin ISCSI_SESSION_UNLOCK(is);
1198c215ad3aSAlexander Motin
11997aab9c14SJohn Baldwin iscsi_pdu_get_data_csio(response, 0, csio, oreceived, data_segment_len);
1200009ea47eSEdward Tomasz Napierala
1201009ea47eSEdward Tomasz Napierala /*
1202009ea47eSEdward Tomasz Napierala * XXX: Check F.
1203009ea47eSEdward Tomasz Napierala */
120469647b7bSEdward Tomasz Napierala if ((bhsdi->bhsdi_flags & BHSDI_FLAGS_S) == 0) {
120569647b7bSEdward Tomasz Napierala /*
120669647b7bSEdward Tomasz Napierala * Nothing more to do.
120769647b7bSEdward Tomasz Napierala */
120869647b7bSEdward Tomasz Napierala icl_pdu_free(response);
120969647b7bSEdward Tomasz Napierala return;
121069647b7bSEdward Tomasz Napierala }
121169647b7bSEdward Tomasz Napierala
1212009ea47eSEdward Tomasz Napierala //ISCSI_SESSION_DEBUG(is, "got S flag; status 0x%x", bhsdi->bhsdi_status);
1213009ea47eSEdward Tomasz Napierala if (bhsdi->bhsdi_status == 0) {
121473e2e95dSAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP;
1215009ea47eSEdward Tomasz Napierala } else {
121673e2e95dSAlexander Motin if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
121773e2e95dSAlexander Motin xpt_freeze_devq(ccb->ccb_h.path, 1);
1218009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
1219009ea47eSEdward Tomasz Napierala }
122073e2e95dSAlexander Motin ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_DEV_QFRZN;
1221009ea47eSEdward Tomasz Napierala csio->scsi_status = bhsdi->bhsdi_status;
1222009ea47eSEdward Tomasz Napierala }
122369647b7bSEdward Tomasz Napierala
122469647b7bSEdward Tomasz Napierala if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
122573e2e95dSAlexander Motin KASSERT(received <= csio->dxfer_len,
122673e2e95dSAlexander Motin ("received > csio->dxfer_len"));
122773e2e95dSAlexander Motin if (received < csio->dxfer_len) {
122869647b7bSEdward Tomasz Napierala csio->resid = ntohl(bhsdi->bhsdi_residual_count);
122973e2e95dSAlexander Motin if (csio->resid != csio->dxfer_len - received) {
123069647b7bSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "underflow mismatch: "
123169647b7bSEdward Tomasz Napierala "target indicates %d, we calculated %zd",
123273e2e95dSAlexander Motin csio->resid, csio->dxfer_len - received);
123369647b7bSEdward Tomasz Napierala }
123473e2e95dSAlexander Motin csio->resid = csio->dxfer_len - received;
123569647b7bSEdward Tomasz Napierala }
1236009ea47eSEdward Tomasz Napierala }
1237009ea47eSEdward Tomasz Napierala
123873e2e95dSAlexander Motin xpt_done(ccb);
1239009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1240009ea47eSEdward Tomasz Napierala }
1241009ea47eSEdward Tomasz Napierala
1242009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_logout_response(struct icl_pdu * response)1243009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_logout_response(struct icl_pdu *response)
1244009ea47eSEdward Tomasz Napierala {
1245009ea47eSEdward Tomasz Napierala
1246009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(PDU_SESSION(response), "logout response");
1247009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1248009ea47eSEdward Tomasz Napierala }
1249009ea47eSEdward Tomasz Napierala
12507aab9c14SJohn Baldwin static int
iscsi_pdu_append_data_csio(struct icl_pdu * request,struct ccb_scsiio * csio,size_t off,size_t len,int how)12517aab9c14SJohn Baldwin iscsi_pdu_append_data_csio(struct icl_pdu *request, struct ccb_scsiio *csio,
12527aab9c14SJohn Baldwin size_t off, size_t len, int how)
12537aab9c14SJohn Baldwin {
12547aab9c14SJohn Baldwin switch (csio->ccb_h.flags & CAM_DATA_MASK) {
12557aab9c14SJohn Baldwin case CAM_DATA_BIO:
12567aab9c14SJohn Baldwin return (icl_pdu_append_bio(request,
12577aab9c14SJohn Baldwin (struct bio *)csio->data_ptr, off, len, how));
12587aab9c14SJohn Baldwin case CAM_DATA_VADDR:
12597aab9c14SJohn Baldwin return (icl_pdu_append_data(request, csio->data_ptr + off, len,
12607aab9c14SJohn Baldwin how));
12617aab9c14SJohn Baldwin default:
12627aab9c14SJohn Baldwin __assert_unreachable();
12637aab9c14SJohn Baldwin }
12647aab9c14SJohn Baldwin }
12657aab9c14SJohn Baldwin
1266009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_r2t(struct icl_pdu * response)1267009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_r2t(struct icl_pdu *response)
1268009ea47eSEdward Tomasz Napierala {
1269009ea47eSEdward Tomasz Napierala struct icl_pdu *request;
1270009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1271009ea47eSEdward Tomasz Napierala struct iscsi_bhs_r2t *bhsr2t;
1272009ea47eSEdward Tomasz Napierala struct iscsi_bhs_data_out *bhsdo;
1273009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io;
1274009ea47eSEdward Tomasz Napierala struct ccb_scsiio *csio;
1275f0594f52SJohn Baldwin size_t off, len, max_send_data_segment_length, total_len;
1276009ea47eSEdward Tomasz Napierala int error;
12773dd2a7a5SAlexander Motin uint32_t datasn = 0;
1278009ea47eSEdward Tomasz Napierala
1279009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
1280009ea47eSEdward Tomasz Napierala
1281009ea47eSEdward Tomasz Napierala bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
1282009ea47eSEdward Tomasz Napierala io = iscsi_outstanding_find(is, bhsr2t->bhsr2t_initiator_task_tag);
1283aa75114cSAlexander Motin if (io == NULL || io->io_ccb == NULL) {
1284009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "bad itt 0x%x; reconnecting",
1285009ea47eSEdward Tomasz Napierala bhsr2t->bhsr2t_initiator_task_tag);
1286009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1287009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1288009ea47eSEdward Tomasz Napierala return;
1289009ea47eSEdward Tomasz Napierala }
1290009ea47eSEdward Tomasz Napierala
1291009ea47eSEdward Tomasz Napierala csio = &io->io_ccb->csio;
1292009ea47eSEdward Tomasz Napierala
1293009ea47eSEdward Tomasz Napierala if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT) {
1294009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "received R2T for read command; reconnecting");
1295009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1296009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1297009ea47eSEdward Tomasz Napierala return;
1298009ea47eSEdward Tomasz Napierala }
1299009ea47eSEdward Tomasz Napierala
1300009ea47eSEdward Tomasz Napierala /*
1301009ea47eSEdward Tomasz Napierala * XXX: Verify R2TSN.
1302009ea47eSEdward Tomasz Napierala */
1303009ea47eSEdward Tomasz Napierala
1304009ea47eSEdward Tomasz Napierala off = ntohl(bhsr2t->bhsr2t_buffer_offset);
1305d43c9ec1SEdward Tomasz Napierala if (off > csio->dxfer_len) {
1306d43c9ec1SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "target requested invalid offset "
1307756220b5SGordon Bergling "%zd, buffer is %d; reconnecting", off, csio->dxfer_len);
1308d43c9ec1SEdward Tomasz Napierala icl_pdu_free(response);
1309d43c9ec1SEdward Tomasz Napierala iscsi_session_reconnect(is);
1310d43c9ec1SEdward Tomasz Napierala return;
1311d43c9ec1SEdward Tomasz Napierala }
1312d43c9ec1SEdward Tomasz Napierala
1313009ea47eSEdward Tomasz Napierala total_len = ntohl(bhsr2t->bhsr2t_desired_data_transfer_length);
1314d43c9ec1SEdward Tomasz Napierala if (total_len == 0 || total_len > csio->dxfer_len) {
1315d43c9ec1SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "target requested invalid length "
1316d43c9ec1SEdward Tomasz Napierala "%zd, buffer is %d; reconnecting", total_len, csio->dxfer_len);
1317d43c9ec1SEdward Tomasz Napierala icl_pdu_free(response);
1318d43c9ec1SEdward Tomasz Napierala iscsi_session_reconnect(is);
1319d43c9ec1SEdward Tomasz Napierala return;
1320d43c9ec1SEdward Tomasz Napierala }
1321009ea47eSEdward Tomasz Napierala
1322009ea47eSEdward Tomasz Napierala //ISCSI_SESSION_DEBUG(is, "r2t; off %zd, len %zd", off, total_len);
1323009ea47eSEdward Tomasz Napierala
1324f0594f52SJohn Baldwin if (is->is_conn->ic_hw_isomax != 0)
1325f0594f52SJohn Baldwin max_send_data_segment_length = is->is_conn->ic_hw_isomax;
1326f0594f52SJohn Baldwin else
1327f0594f52SJohn Baldwin max_send_data_segment_length =
1328f0594f52SJohn Baldwin is->is_conn->ic_max_send_data_segment_length;
1329009ea47eSEdward Tomasz Napierala for (;;) {
1330009ea47eSEdward Tomasz Napierala len = total_len;
1331009ea47eSEdward Tomasz Napierala
1332f0594f52SJohn Baldwin if (len > max_send_data_segment_length)
1333f0594f52SJohn Baldwin len = max_send_data_segment_length;
1334009ea47eSEdward Tomasz Napierala
1335009ea47eSEdward Tomasz Napierala if (off + len > csio->dxfer_len) {
1336d43c9ec1SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "target requested invalid "
1337d43c9ec1SEdward Tomasz Napierala "length/offset %zd, buffer is %d; reconnecting",
1338009ea47eSEdward Tomasz Napierala off + len, csio->dxfer_len);
1339009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1340009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1341009ea47eSEdward Tomasz Napierala return;
1342009ea47eSEdward Tomasz Napierala }
1343009ea47eSEdward Tomasz Napierala
1344d2d52b00SEdward Tomasz Napierala request = icl_pdu_new(response->ip_conn, M_NOWAIT);
1345009ea47eSEdward Tomasz Napierala if (request == NULL) {
1346009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1347009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1348009ea47eSEdward Tomasz Napierala return;
1349009ea47eSEdward Tomasz Napierala }
1350009ea47eSEdward Tomasz Napierala
1351009ea47eSEdward Tomasz Napierala bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
1352009ea47eSEdward Tomasz Napierala bhsdo->bhsdo_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_OUT;
1353009ea47eSEdward Tomasz Napierala bhsdo->bhsdo_lun = bhsr2t->bhsr2t_lun;
1354009ea47eSEdward Tomasz Napierala bhsdo->bhsdo_initiator_task_tag =
1355009ea47eSEdward Tomasz Napierala bhsr2t->bhsr2t_initiator_task_tag;
1356009ea47eSEdward Tomasz Napierala bhsdo->bhsdo_target_transfer_tag =
1357009ea47eSEdward Tomasz Napierala bhsr2t->bhsr2t_target_transfer_tag;
1358f0594f52SJohn Baldwin bhsdo->bhsdo_datasn = htonl(datasn);
1359009ea47eSEdward Tomasz Napierala bhsdo->bhsdo_buffer_offset = htonl(off);
13607aab9c14SJohn Baldwin error = iscsi_pdu_append_data_csio(request, csio, off, len,
13619c7a4875SJohn Baldwin M_NOWAIT | ICL_NOCOPY);
1362009ea47eSEdward Tomasz Napierala if (error != 0) {
1363d43c9ec1SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate memory; "
1364d43c9ec1SEdward Tomasz Napierala "reconnecting");
1365009ea47eSEdward Tomasz Napierala icl_pdu_free(request);
1366009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1367009ea47eSEdward Tomasz Napierala iscsi_session_reconnect(is);
1368009ea47eSEdward Tomasz Napierala return;
1369009ea47eSEdward Tomasz Napierala }
1370009ea47eSEdward Tomasz Napierala
1371f0594f52SJohn Baldwin datasn += howmany(len,
1372f0594f52SJohn Baldwin is->is_conn->ic_max_send_data_segment_length);
1373009ea47eSEdward Tomasz Napierala off += len;
1374009ea47eSEdward Tomasz Napierala total_len -= len;
1375009ea47eSEdward Tomasz Napierala
1376009ea47eSEdward Tomasz Napierala if (total_len == 0) {
1377009ea47eSEdward Tomasz Napierala bhsdo->bhsdo_flags |= BHSDO_FLAGS_F;
1378009ea47eSEdward Tomasz Napierala //ISCSI_SESSION_DEBUG(is, "setting F, off %zd", off);
1379009ea47eSEdward Tomasz Napierala } else {
1380009ea47eSEdward Tomasz Napierala //ISCSI_SESSION_DEBUG(is, "not finished, off %zd", off);
1381009ea47eSEdward Tomasz Napierala }
1382009ea47eSEdward Tomasz Napierala
1383009ea47eSEdward Tomasz Napierala iscsi_pdu_queue_locked(request);
1384009ea47eSEdward Tomasz Napierala
1385009ea47eSEdward Tomasz Napierala if (total_len == 0)
1386009ea47eSEdward Tomasz Napierala break;
1387009ea47eSEdward Tomasz Napierala }
1388009ea47eSEdward Tomasz Napierala
1389009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1390009ea47eSEdward Tomasz Napierala }
1391009ea47eSEdward Tomasz Napierala
1392009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_async_message(struct icl_pdu * response)1393009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_async_message(struct icl_pdu *response)
1394009ea47eSEdward Tomasz Napierala {
1395009ea47eSEdward Tomasz Napierala struct iscsi_bhs_asynchronous_message *bhsam;
1396009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1397009ea47eSEdward Tomasz Napierala
1398009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
1399009ea47eSEdward Tomasz Napierala bhsam = (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1400009ea47eSEdward Tomasz Napierala switch (bhsam->bhsam_async_event) {
1401009ea47eSEdward Tomasz Napierala case BHSAM_EVENT_TARGET_REQUESTS_LOGOUT:
1402009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "target requests logout; removing session");
1403009ea47eSEdward Tomasz Napierala iscsi_session_logout(is);
1404009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
1405009ea47eSEdward Tomasz Napierala break;
1406009ea47eSEdward Tomasz Napierala case BHSAM_EVENT_TARGET_TERMINATES_CONNECTION:
14076960c4e1SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "target indicates it will drop the connection");
1408009ea47eSEdward Tomasz Napierala break;
1409009ea47eSEdward Tomasz Napierala case BHSAM_EVENT_TARGET_TERMINATES_SESSION:
14106960c4e1SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "target indicates it will drop the session");
1411009ea47eSEdward Tomasz Napierala break;
1412009ea47eSEdward Tomasz Napierala default:
1413009ea47eSEdward Tomasz Napierala /*
1414009ea47eSEdward Tomasz Napierala * XXX: Technically, we're obligated to also handle
1415009ea47eSEdward Tomasz Napierala * parameter renegotiation.
1416009ea47eSEdward Tomasz Napierala */
1417009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "ignoring AsyncEvent %d", bhsam->bhsam_async_event);
1418009ea47eSEdward Tomasz Napierala break;
1419009ea47eSEdward Tomasz Napierala }
1420009ea47eSEdward Tomasz Napierala
1421009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1422009ea47eSEdward Tomasz Napierala }
1423009ea47eSEdward Tomasz Napierala
1424009ea47eSEdward Tomasz Napierala static void
iscsi_pdu_handle_reject(struct icl_pdu * response)1425009ea47eSEdward Tomasz Napierala iscsi_pdu_handle_reject(struct icl_pdu *response)
1426009ea47eSEdward Tomasz Napierala {
1427009ea47eSEdward Tomasz Napierala struct iscsi_bhs_reject *bhsr;
1428009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1429009ea47eSEdward Tomasz Napierala
1430009ea47eSEdward Tomasz Napierala is = PDU_SESSION(response);
1431009ea47eSEdward Tomasz Napierala bhsr = (struct iscsi_bhs_reject *)response->ip_bhs;
1432009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "received Reject PDU, reason 0x%x; protocol error?",
1433009ea47eSEdward Tomasz Napierala bhsr->bhsr_reason);
1434009ea47eSEdward Tomasz Napierala
1435009ea47eSEdward Tomasz Napierala icl_pdu_free(response);
1436009ea47eSEdward Tomasz Napierala }
1437009ea47eSEdward Tomasz Napierala
1438009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_daemon_wait(struct iscsi_softc * sc,struct iscsi_daemon_request * request,bool freebsd13)1439009ea47eSEdward Tomasz Napierala iscsi_ioctl_daemon_wait(struct iscsi_softc *sc,
14407b02c1e8SJohn Baldwin struct iscsi_daemon_request *request, bool freebsd13)
1441009ea47eSEdward Tomasz Napierala {
1442009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1443009ea47eSEdward Tomasz Napierala int error;
1444009ea47eSEdward Tomasz Napierala
1445009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
1446009ea47eSEdward Tomasz Napierala for (;;) {
1447009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
14482be596adSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1449ba165a31SEdward Tomasz Napierala if (is->is_conf.isc_enable == 0 &&
1450ba165a31SEdward Tomasz Napierala is->is_conf.isc_discovery == 0) {
1451ba165a31SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1452ba165a31SEdward Tomasz Napierala continue;
1453ba165a31SEdward Tomasz Napierala }
1454009ea47eSEdward Tomasz Napierala if (is->is_waiting_for_iscsid)
1455009ea47eSEdward Tomasz Napierala break;
14562be596adSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1457009ea47eSEdward Tomasz Napierala }
1458009ea47eSEdward Tomasz Napierala
1459009ea47eSEdward Tomasz Napierala if (is == NULL) {
146089df4847SJohn Baldwin if (sc->sc_unloading) {
146189df4847SJohn Baldwin sx_sunlock(&sc->sc_lock);
146289df4847SJohn Baldwin return (ENXIO);
146389df4847SJohn Baldwin }
146489df4847SJohn Baldwin
1465009ea47eSEdward Tomasz Napierala /*
1466009ea47eSEdward Tomasz Napierala * No session requires attention from iscsid(8); wait.
1467009ea47eSEdward Tomasz Napierala */
1468009ea47eSEdward Tomasz Napierala error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
1469009ea47eSEdward Tomasz Napierala if (error != 0) {
1470009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1471009ea47eSEdward Tomasz Napierala return (error);
1472009ea47eSEdward Tomasz Napierala }
1473009ea47eSEdward Tomasz Napierala continue;
1474009ea47eSEdward Tomasz Napierala }
1475009ea47eSEdward Tomasz Napierala
1476009ea47eSEdward Tomasz Napierala is->is_waiting_for_iscsid = false;
1477009ea47eSEdward Tomasz Napierala is->is_login_phase = true;
1478009ea47eSEdward Tomasz Napierala is->is_reason[0] = '\0';
1479009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1480009ea47eSEdward Tomasz Napierala
1481009ea47eSEdward Tomasz Napierala request->idr_session_id = is->is_id;
1482ffe82e05SAlexander Motin memcpy(&request->idr_isid, &is->is_isid,
1483ffe82e05SAlexander Motin sizeof(request->idr_isid));
1484ffe82e05SAlexander Motin request->idr_tsih = 0; /* New or reinstated session. */
1485009ea47eSEdward Tomasz Napierala memcpy(&request->idr_conf, &is->is_conf,
1486009ea47eSEdward Tomasz Napierala sizeof(request->idr_conf));
1487009ea47eSEdward Tomasz Napierala
14887b02c1e8SJohn Baldwin #ifdef COMPAT_FREEBSD13
14897b02c1e8SJohn Baldwin if (freebsd13) {
14907b02c1e8SJohn Baldwin struct icl_drv_limits idl;
14917b02c1e8SJohn Baldwin struct iscsi_daemon_request13 *request13;
14927b02c1e8SJohn Baldwin
149382babffbSEdward Tomasz Napierala error = icl_limits(is->is_conf.isc_offload,
14947b02c1e8SJohn Baldwin is->is_conf.isc_iser, 0, &idl);
149582babffbSEdward Tomasz Napierala if (error != 0) {
14967b02c1e8SJohn Baldwin ISCSI_SESSION_WARN(is, "icl_limits for "
14977b02c1e8SJohn Baldwin "offload \"%s\" failed with error %d",
14987b02c1e8SJohn Baldwin is->is_conf.isc_offload, error);
149982babffbSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
150082babffbSEdward Tomasz Napierala return (error);
150182babffbSEdward Tomasz Napierala }
15027b02c1e8SJohn Baldwin request13 = (struct iscsi_daemon_request13 *)request;
15037b02c1e8SJohn Baldwin request13->idr_limits.isl_max_recv_data_segment_length =
150497b84d34SNavdeep Parhar idl.idl_max_recv_data_segment_length;
15057b02c1e8SJohn Baldwin request13->idr_limits.isl_max_send_data_segment_length =
150648214203SNavdeep Parhar idl.idl_max_send_data_segment_length;
15077b02c1e8SJohn Baldwin request13->idr_limits.isl_max_burst_length =
150897b84d34SNavdeep Parhar idl.idl_max_burst_length;
15097b02c1e8SJohn Baldwin request13->idr_limits.isl_first_burst_length =
151097b84d34SNavdeep Parhar idl.idl_first_burst_length;
15117b02c1e8SJohn Baldwin }
15127b02c1e8SJohn Baldwin #endif
1513009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1514009ea47eSEdward Tomasz Napierala return (0);
1515009ea47eSEdward Tomasz Napierala }
1516009ea47eSEdward Tomasz Napierala }
1517009ea47eSEdward Tomasz Napierala
1518009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_daemon_limits(struct iscsi_softc * sc,struct iscsi_daemon_limits * limits)15197b02c1e8SJohn Baldwin iscsi_ioctl_daemon_limits(struct iscsi_softc *sc,
15207b02c1e8SJohn Baldwin struct iscsi_daemon_limits *limits)
15217b02c1e8SJohn Baldwin {
15227b02c1e8SJohn Baldwin struct icl_drv_limits idl;
15237b02c1e8SJohn Baldwin struct iscsi_session *is;
15247b02c1e8SJohn Baldwin int error;
15257b02c1e8SJohn Baldwin
15267b02c1e8SJohn Baldwin sx_slock(&sc->sc_lock);
15277b02c1e8SJohn Baldwin
15287b02c1e8SJohn Baldwin /*
15297b02c1e8SJohn Baldwin * Find the session to fetch limits for.
15307b02c1e8SJohn Baldwin */
15317b02c1e8SJohn Baldwin TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
15327b02c1e8SJohn Baldwin if (is->is_id == limits->idl_session_id)
15337b02c1e8SJohn Baldwin break;
15347b02c1e8SJohn Baldwin }
15357b02c1e8SJohn Baldwin if (is == NULL) {
15367b02c1e8SJohn Baldwin sx_sunlock(&sc->sc_lock);
15377b02c1e8SJohn Baldwin return (ESRCH);
15387b02c1e8SJohn Baldwin }
15397b02c1e8SJohn Baldwin
15407b02c1e8SJohn Baldwin error = icl_limits(is->is_conf.isc_offload, is->is_conf.isc_iser,
15417b02c1e8SJohn Baldwin limits->idl_socket, &idl);
15427b02c1e8SJohn Baldwin sx_sunlock(&sc->sc_lock);
15437b02c1e8SJohn Baldwin if (error != 0) {
15447b02c1e8SJohn Baldwin ISCSI_SESSION_WARN(is, "icl_limits for offload \"%s\" "
15457b02c1e8SJohn Baldwin "failed with error %d", is->is_conf.isc_offload, error);
15467b02c1e8SJohn Baldwin return (error);
15477b02c1e8SJohn Baldwin }
15487b02c1e8SJohn Baldwin limits->idl_limits.isl_max_recv_data_segment_length =
15497b02c1e8SJohn Baldwin idl.idl_max_recv_data_segment_length;
15507b02c1e8SJohn Baldwin limits->idl_limits.isl_max_send_data_segment_length =
15517b02c1e8SJohn Baldwin idl.idl_max_send_data_segment_length;
15527b02c1e8SJohn Baldwin limits->idl_limits.isl_max_burst_length =
15537b02c1e8SJohn Baldwin idl.idl_max_burst_length;
15547b02c1e8SJohn Baldwin limits->idl_limits.isl_first_burst_length =
15557b02c1e8SJohn Baldwin idl.idl_first_burst_length;
15567b02c1e8SJohn Baldwin
15577b02c1e8SJohn Baldwin return (0);
15587b02c1e8SJohn Baldwin }
15597b02c1e8SJohn Baldwin
15607b02c1e8SJohn Baldwin static int
iscsi_ioctl_daemon_handoff(struct iscsi_softc * sc,struct iscsi_daemon_handoff * handoff)1561009ea47eSEdward Tomasz Napierala iscsi_ioctl_daemon_handoff(struct iscsi_softc *sc,
1562009ea47eSEdward Tomasz Napierala struct iscsi_daemon_handoff *handoff)
1563009ea47eSEdward Tomasz Napierala {
1564009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1565f5cadbc4SNavdeep Parhar struct icl_conn *ic;
1566009ea47eSEdward Tomasz Napierala int error;
1567009ea47eSEdward Tomasz Napierala
1568009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
1569009ea47eSEdward Tomasz Napierala
1570009ea47eSEdward Tomasz Napierala /*
1571009ea47eSEdward Tomasz Napierala * Find the session to hand off socket to.
1572009ea47eSEdward Tomasz Napierala */
1573009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1574009ea47eSEdward Tomasz Napierala if (is->is_id == handoff->idh_session_id)
1575009ea47eSEdward Tomasz Napierala break;
1576009ea47eSEdward Tomasz Napierala }
1577009ea47eSEdward Tomasz Napierala if (is == NULL) {
1578009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1579009ea47eSEdward Tomasz Napierala return (ESRCH);
1580009ea47eSEdward Tomasz Napierala }
1581009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1582f5cadbc4SNavdeep Parhar ic = is->is_conn;
1583009ea47eSEdward Tomasz Napierala if (is->is_conf.isc_discovery || is->is_terminating) {
1584009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1585009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1586009ea47eSEdward Tomasz Napierala return (EINVAL);
1587009ea47eSEdward Tomasz Napierala }
15880260023fSEdward Tomasz Napierala if (is->is_connected) {
15890260023fSEdward Tomasz Napierala /*
15900260023fSEdward Tomasz Napierala * This might have happened because another iscsid(8)
15910260023fSEdward Tomasz Napierala * instance handed off the connection in the meantime.
15920260023fSEdward Tomasz Napierala * Just return.
15930260023fSEdward Tomasz Napierala */
15940260023fSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "handoff on already connected "
15950260023fSEdward Tomasz Napierala "session");
15960260023fSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
15970260023fSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
15980260023fSEdward Tomasz Napierala return (EBUSY);
15990260023fSEdward Tomasz Napierala }
1600009ea47eSEdward Tomasz Napierala
1601009ea47eSEdward Tomasz Napierala strlcpy(is->is_target_alias, handoff->idh_target_alias,
1602009ea47eSEdward Tomasz Napierala sizeof(is->is_target_alias));
1603ffe82e05SAlexander Motin is->is_tsih = handoff->idh_tsih;
1604009ea47eSEdward Tomasz Napierala is->is_statsn = handoff->idh_statsn;
16057dbbd1aeSAlexander Motin is->is_protocol_level = handoff->idh_protocol_level;
1606009ea47eSEdward Tomasz Napierala is->is_initial_r2t = handoff->idh_initial_r2t;
1607009ea47eSEdward Tomasz Napierala is->is_immediate_data = handoff->idh_immediate_data;
1608c8b73c2cSNavdeep Parhar
16090cc7d64aSJohn Baldwin ic->ic_max_recv_data_segment_length =
161097b84d34SNavdeep Parhar handoff->idh_max_recv_data_segment_length;
16110cc7d64aSJohn Baldwin ic->ic_max_send_data_segment_length =
161297b84d34SNavdeep Parhar handoff->idh_max_send_data_segment_length;
1613009ea47eSEdward Tomasz Napierala is->is_max_burst_length = handoff->idh_max_burst_length;
1614009ea47eSEdward Tomasz Napierala is->is_first_burst_length = handoff->idh_first_burst_length;
1615009ea47eSEdward Tomasz Napierala
1616009ea47eSEdward Tomasz Napierala if (handoff->idh_header_digest == ISCSI_DIGEST_CRC32C)
1617f5cadbc4SNavdeep Parhar ic->ic_header_crc32c = true;
1618009ea47eSEdward Tomasz Napierala else
1619f5cadbc4SNavdeep Parhar ic->ic_header_crc32c = false;
1620009ea47eSEdward Tomasz Napierala if (handoff->idh_data_digest == ISCSI_DIGEST_CRC32C)
1621f5cadbc4SNavdeep Parhar ic->ic_data_crc32c = true;
1622009ea47eSEdward Tomasz Napierala else
1623f5cadbc4SNavdeep Parhar ic->ic_data_crc32c = false;
1624b218ca6fSEdward Tomasz Napierala ic->ic_maxtags = maxtags;
1625009ea47eSEdward Tomasz Napierala
1626009ea47eSEdward Tomasz Napierala is->is_cmdsn = 0;
1627cf23c509SEdward Tomasz Napierala is->is_expcmdsn = 0;
1628cf23c509SEdward Tomasz Napierala is->is_maxcmdsn = 0;
1629009ea47eSEdward Tomasz Napierala is->is_waiting_for_iscsid = false;
1630009ea47eSEdward Tomasz Napierala is->is_login_phase = false;
1631009ea47eSEdward Tomasz Napierala is->is_timeout = 0;
1632bd6bb493SRichard Scheffenegger is->is_ping_timeout = is->is_conf.isc_ping_timeout;
1633bd6bb493SRichard Scheffenegger if (is->is_ping_timeout < 0)
1634bd6bb493SRichard Scheffenegger is->is_ping_timeout = ping_timeout;
1635bd6bb493SRichard Scheffenegger is->is_login_timeout = is->is_conf.isc_login_timeout;
1636bd6bb493SRichard Scheffenegger if (is->is_login_timeout < 0)
1637bd6bb493SRichard Scheffenegger is->is_login_timeout = login_timeout;
1638009ea47eSEdward Tomasz Napierala is->is_connected = true;
1639009ea47eSEdward Tomasz Napierala is->is_reason[0] = '\0';
1640009ea47eSEdward Tomasz Napierala
1641009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1642009ea47eSEdward Tomasz Napierala
164357a4f20bSEdward Tomasz Napierala /*
1644906a424bSEdward Tomasz Napierala * If we're going through the proxy, the idh_socket will be 0,
1645906a424bSEdward Tomasz Napierala * and the ICL module can simply ignore this call. It can also
1646906a424bSEdward Tomasz Napierala * use it to determine it's no longer in the Login phase.
164757a4f20bSEdward Tomasz Napierala */
1648f5cadbc4SNavdeep Parhar error = icl_conn_handoff(ic, handoff->idh_socket);
1649009ea47eSEdward Tomasz Napierala if (error != 0) {
1650009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1651009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
1652009ea47eSEdward Tomasz Napierala return (error);
1653009ea47eSEdward Tomasz Napierala }
1654009ea47eSEdward Tomasz Napierala
1655009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1656009ea47eSEdward Tomasz Napierala
1657009ea47eSEdward Tomasz Napierala if (is->is_sim != NULL) {
1658009ea47eSEdward Tomasz Napierala /*
1659009ea47eSEdward Tomasz Napierala * When reconnecting, there already is SIM allocated for the session.
1660009ea47eSEdward Tomasz Napierala */
1661009ea47eSEdward Tomasz Napierala KASSERT(is->is_simq_frozen, ("reconnect without frozen simq"));
1662009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1663009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "releasing");
1664009ea47eSEdward Tomasz Napierala is->is_simq_frozen = false;
1665186bcdaaSEdward Tomasz Napierala xpt_release_simq(is->is_sim, 1);
1666009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1667009ea47eSEdward Tomasz Napierala
1668009ea47eSEdward Tomasz Napierala } else {
1669009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1670b218ca6fSEdward Tomasz Napierala is->is_devq = cam_simq_alloc(ic->ic_maxtags);
1671009ea47eSEdward Tomasz Napierala if (is->is_devq == NULL) {
1672fa669505SKa Ho Ng ISCSI_SESSION_UNLOCK(is);
1673009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate simq");
1674009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
1675009ea47eSEdward Tomasz Napierala return (ENOMEM);
1676009ea47eSEdward Tomasz Napierala }
1677009ea47eSEdward Tomasz Napierala
167847769bc5SJohn Baldwin is->is_sim = cam_sim_alloc(iscsi_action, NULL, "iscsi",
1679009ea47eSEdward Tomasz Napierala is, is->is_id /* unit */, &is->is_lock,
1680b218ca6fSEdward Tomasz Napierala 1, ic->ic_maxtags, is->is_devq);
1681009ea47eSEdward Tomasz Napierala if (is->is_sim == NULL) {
1682009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1683009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate SIM");
1684009ea47eSEdward Tomasz Napierala cam_simq_free(is->is_devq);
1685009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
1686009ea47eSEdward Tomasz Napierala return (ENOMEM);
1687009ea47eSEdward Tomasz Napierala }
1688009ea47eSEdward Tomasz Napierala
168930f8afd0SWarner Losh if (xpt_bus_register(is->is_sim, NULL, 0) != 0) {
1690009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1691009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to register bus");
1692009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
1693009ea47eSEdward Tomasz Napierala return (ENOMEM);
1694009ea47eSEdward Tomasz Napierala }
1695009ea47eSEdward Tomasz Napierala
1696009ea47eSEdward Tomasz Napierala error = xpt_create_path(&is->is_path, /*periph*/NULL,
1697009ea47eSEdward Tomasz Napierala cam_sim_path(is->is_sim), CAM_TARGET_WILDCARD,
1698009ea47eSEdward Tomasz Napierala CAM_LUN_WILDCARD);
1699009ea47eSEdward Tomasz Napierala if (error != CAM_REQ_CMP) {
1700009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1701009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to create path");
1702009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
1703009ea47eSEdward Tomasz Napierala return (ENOMEM);
1704009ea47eSEdward Tomasz Napierala }
1705009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1706009ea47eSEdward Tomasz Napierala }
1707009ea47eSEdward Tomasz Napierala
1708009ea47eSEdward Tomasz Napierala return (0);
1709009ea47eSEdward Tomasz Napierala }
1710009ea47eSEdward Tomasz Napierala
1711009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_daemon_fail(struct iscsi_softc * sc,struct iscsi_daemon_fail * fail)1712009ea47eSEdward Tomasz Napierala iscsi_ioctl_daemon_fail(struct iscsi_softc *sc,
1713009ea47eSEdward Tomasz Napierala struct iscsi_daemon_fail *fail)
1714009ea47eSEdward Tomasz Napierala {
1715009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1716009ea47eSEdward Tomasz Napierala
1717009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
1718009ea47eSEdward Tomasz Napierala
1719009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1720009ea47eSEdward Tomasz Napierala if (is->is_id == fail->idf_session_id)
1721009ea47eSEdward Tomasz Napierala break;
1722009ea47eSEdward Tomasz Napierala }
1723009ea47eSEdward Tomasz Napierala if (is == NULL) {
1724009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1725009ea47eSEdward Tomasz Napierala return (ESRCH);
1726009ea47eSEdward Tomasz Napierala }
1727009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1728009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "iscsid(8) failed: %s",
1729009ea47eSEdward Tomasz Napierala fail->idf_reason);
1730009ea47eSEdward Tomasz Napierala strlcpy(is->is_reason, fail->idf_reason, sizeof(is->is_reason));
1731009ea47eSEdward Tomasz Napierala //is->is_waiting_for_iscsid = false;
1732009ea47eSEdward Tomasz Napierala //is->is_login_phase = true;
1733009ea47eSEdward Tomasz Napierala //iscsi_session_reconnect(is);
1734009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1735009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1736009ea47eSEdward Tomasz Napierala
1737009ea47eSEdward Tomasz Napierala return (0);
1738009ea47eSEdward Tomasz Napierala }
1739009ea47eSEdward Tomasz Napierala
1740009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1741009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_daemon_connect(struct iscsi_softc * sc,struct iscsi_daemon_connect * idc)1742009ea47eSEdward Tomasz Napierala iscsi_ioctl_daemon_connect(struct iscsi_softc *sc,
1743009ea47eSEdward Tomasz Napierala struct iscsi_daemon_connect *idc)
1744009ea47eSEdward Tomasz Napierala {
1745009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1746009ea47eSEdward Tomasz Napierala struct sockaddr *from_sa, *to_sa;
1747009ea47eSEdward Tomasz Napierala int error;
1748009ea47eSEdward Tomasz Napierala
1749009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
1750009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1751009ea47eSEdward Tomasz Napierala if (is->is_id == idc->idc_session_id)
1752009ea47eSEdward Tomasz Napierala break;
1753009ea47eSEdward Tomasz Napierala }
1754009ea47eSEdward Tomasz Napierala if (is == NULL) {
1755009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1756009ea47eSEdward Tomasz Napierala return (ESRCH);
1757009ea47eSEdward Tomasz Napierala }
1758009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1759009ea47eSEdward Tomasz Napierala
1760009ea47eSEdward Tomasz Napierala if (idc->idc_from_addrlen > 0) {
1761009ea47eSEdward Tomasz Napierala error = getsockaddr(&from_sa, (void *)idc->idc_from_addr, idc->idc_from_addrlen);
176257a4f20bSEdward Tomasz Napierala if (error != 0) {
176357a4f20bSEdward Tomasz Napierala ISCSI_SESSION_WARN(is,
176457a4f20bSEdward Tomasz Napierala "getsockaddr failed with error %d", error);
1765009ea47eSEdward Tomasz Napierala return (error);
176657a4f20bSEdward Tomasz Napierala }
1767009ea47eSEdward Tomasz Napierala } else {
1768009ea47eSEdward Tomasz Napierala from_sa = NULL;
1769009ea47eSEdward Tomasz Napierala }
1770009ea47eSEdward Tomasz Napierala error = getsockaddr(&to_sa, (void *)idc->idc_to_addr, idc->idc_to_addrlen);
1771009ea47eSEdward Tomasz Napierala if (error != 0) {
177257a4f20bSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "getsockaddr failed with error %d",
177357a4f20bSEdward Tomasz Napierala error);
1774009ea47eSEdward Tomasz Napierala free(from_sa, M_SONAME);
1775009ea47eSEdward Tomasz Napierala return (error);
1776009ea47eSEdward Tomasz Napierala }
1777009ea47eSEdward Tomasz Napierala
1778009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1779a3fd63f2SEdward Tomasz Napierala is->is_statsn = 0;
1780a3fd63f2SEdward Tomasz Napierala is->is_cmdsn = 0;
1781a3fd63f2SEdward Tomasz Napierala is->is_expcmdsn = 0;
1782a3fd63f2SEdward Tomasz Napierala is->is_maxcmdsn = 0;
1783009ea47eSEdward Tomasz Napierala is->is_waiting_for_iscsid = false;
1784009ea47eSEdward Tomasz Napierala is->is_login_phase = true;
1785009ea47eSEdward Tomasz Napierala is->is_timeout = 0;
1786009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1787009ea47eSEdward Tomasz Napierala
1788f41492b0SEdward Tomasz Napierala error = icl_conn_connect(is->is_conn, idc->idc_domain,
1789009ea47eSEdward Tomasz Napierala idc->idc_socktype, idc->idc_protocol, from_sa, to_sa);
1790009ea47eSEdward Tomasz Napierala free(from_sa, M_SONAME);
1791009ea47eSEdward Tomasz Napierala free(to_sa, M_SONAME);
1792009ea47eSEdward Tomasz Napierala
1793009ea47eSEdward Tomasz Napierala /*
1794009ea47eSEdward Tomasz Napierala * Digests are always disabled during login phase.
1795009ea47eSEdward Tomasz Napierala */
1796009ea47eSEdward Tomasz Napierala is->is_conn->ic_header_crc32c = false;
1797009ea47eSEdward Tomasz Napierala is->is_conn->ic_data_crc32c = false;
1798009ea47eSEdward Tomasz Napierala
1799009ea47eSEdward Tomasz Napierala return (error);
1800009ea47eSEdward Tomasz Napierala }
1801009ea47eSEdward Tomasz Napierala
1802009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_daemon_send(struct iscsi_softc * sc,struct iscsi_daemon_send * ids)1803009ea47eSEdward Tomasz Napierala iscsi_ioctl_daemon_send(struct iscsi_softc *sc,
1804009ea47eSEdward Tomasz Napierala struct iscsi_daemon_send *ids)
1805009ea47eSEdward Tomasz Napierala {
1806009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1807009ea47eSEdward Tomasz Napierala struct icl_pdu *ip;
1808009ea47eSEdward Tomasz Napierala size_t datalen;
1809009ea47eSEdward Tomasz Napierala void *data;
1810009ea47eSEdward Tomasz Napierala int error;
1811009ea47eSEdward Tomasz Napierala
1812009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
1813009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1814009ea47eSEdward Tomasz Napierala if (is->is_id == ids->ids_session_id)
1815009ea47eSEdward Tomasz Napierala break;
1816009ea47eSEdward Tomasz Napierala }
1817009ea47eSEdward Tomasz Napierala if (is == NULL) {
1818009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1819009ea47eSEdward Tomasz Napierala return (ESRCH);
1820009ea47eSEdward Tomasz Napierala }
1821009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1822009ea47eSEdward Tomasz Napierala
1823009ea47eSEdward Tomasz Napierala if (is->is_login_phase == false)
1824009ea47eSEdward Tomasz Napierala return (EBUSY);
1825009ea47eSEdward Tomasz Napierala
1826009ea47eSEdward Tomasz Napierala if (is->is_terminating || is->is_reconnecting)
1827009ea47eSEdward Tomasz Napierala return (EIO);
1828009ea47eSEdward Tomasz Napierala
1829009ea47eSEdward Tomasz Napierala datalen = ids->ids_data_segment_len;
18300cc7d64aSJohn Baldwin if (datalen > is->is_conn->ic_max_send_data_segment_length)
1831009ea47eSEdward Tomasz Napierala return (EINVAL);
1832009ea47eSEdward Tomasz Napierala if (datalen > 0) {
1833009ea47eSEdward Tomasz Napierala data = malloc(datalen, M_ISCSI, M_WAITOK);
1834009ea47eSEdward Tomasz Napierala error = copyin(ids->ids_data_segment, data, datalen);
1835009ea47eSEdward Tomasz Napierala if (error != 0) {
1836009ea47eSEdward Tomasz Napierala free(data, M_ISCSI);
1837009ea47eSEdward Tomasz Napierala return (error);
1838009ea47eSEdward Tomasz Napierala }
1839009ea47eSEdward Tomasz Napierala }
1840009ea47eSEdward Tomasz Napierala
1841d2d52b00SEdward Tomasz Napierala ip = icl_pdu_new(is->is_conn, M_WAITOK);
1842009ea47eSEdward Tomasz Napierala memcpy(ip->ip_bhs, ids->ids_bhs, sizeof(*ip->ip_bhs));
1843009ea47eSEdward Tomasz Napierala if (datalen > 0) {
1844009ea47eSEdward Tomasz Napierala error = icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1845009ea47eSEdward Tomasz Napierala KASSERT(error == 0, ("icl_pdu_append_data(..., M_WAITOK) failed"));
1846009ea47eSEdward Tomasz Napierala free(data, M_ISCSI);
1847009ea47eSEdward Tomasz Napierala }
1848d66a906bSEdward Tomasz Napierala iscsi_pdu_queue(ip);
1849009ea47eSEdward Tomasz Napierala
1850009ea47eSEdward Tomasz Napierala return (0);
1851009ea47eSEdward Tomasz Napierala }
1852009ea47eSEdward Tomasz Napierala
1853009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_daemon_receive(struct iscsi_softc * sc,struct iscsi_daemon_receive * idr)1854009ea47eSEdward Tomasz Napierala iscsi_ioctl_daemon_receive(struct iscsi_softc *sc,
1855009ea47eSEdward Tomasz Napierala struct iscsi_daemon_receive *idr)
1856009ea47eSEdward Tomasz Napierala {
1857009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1858009ea47eSEdward Tomasz Napierala struct icl_pdu *ip;
1859009ea47eSEdward Tomasz Napierala void *data;
18602f0586b2SEdward Tomasz Napierala int error;
1861009ea47eSEdward Tomasz Napierala
1862009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
1863009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
1864009ea47eSEdward Tomasz Napierala if (is->is_id == idr->idr_session_id)
1865009ea47eSEdward Tomasz Napierala break;
1866009ea47eSEdward Tomasz Napierala }
1867009ea47eSEdward Tomasz Napierala if (is == NULL) {
1868009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1869009ea47eSEdward Tomasz Napierala return (ESRCH);
1870009ea47eSEdward Tomasz Napierala }
1871009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
1872009ea47eSEdward Tomasz Napierala
1873009ea47eSEdward Tomasz Napierala if (is->is_login_phase == false)
1874009ea47eSEdward Tomasz Napierala return (EBUSY);
1875009ea47eSEdward Tomasz Napierala
1876009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
1877009ea47eSEdward Tomasz Napierala while (is->is_login_pdu == NULL &&
1878009ea47eSEdward Tomasz Napierala is->is_terminating == false &&
18792f0586b2SEdward Tomasz Napierala is->is_reconnecting == false) {
18802f0586b2SEdward Tomasz Napierala error = cv_wait_sig(&is->is_login_cv, &is->is_lock);
18812f0586b2SEdward Tomasz Napierala if (error != 0) {
18822f0586b2SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
18832f0586b2SEdward Tomasz Napierala return (error);
18842f0586b2SEdward Tomasz Napierala }
18852f0586b2SEdward Tomasz Napierala }
1886009ea47eSEdward Tomasz Napierala if (is->is_terminating || is->is_reconnecting) {
1887009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1888009ea47eSEdward Tomasz Napierala return (EIO);
1889009ea47eSEdward Tomasz Napierala }
1890009ea47eSEdward Tomasz Napierala ip = is->is_login_pdu;
1891009ea47eSEdward Tomasz Napierala is->is_login_pdu = NULL;
1892009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
1893009ea47eSEdward Tomasz Napierala
1894009ea47eSEdward Tomasz Napierala if (ip->ip_data_len > idr->idr_data_segment_len) {
1895009ea47eSEdward Tomasz Napierala icl_pdu_free(ip);
1896009ea47eSEdward Tomasz Napierala return (EMSGSIZE);
1897009ea47eSEdward Tomasz Napierala }
1898009ea47eSEdward Tomasz Napierala
1899*74e71380SMark Johnston error = copyout(ip->ip_bhs, idr->idr_bhs, sizeof(*ip->ip_bhs));
1900*74e71380SMark Johnston if (error == 0 && ip->ip_data_len > 0) {
1901009ea47eSEdward Tomasz Napierala data = malloc(ip->ip_data_len, M_ISCSI, M_WAITOK);
1902009ea47eSEdward Tomasz Napierala icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1903*74e71380SMark Johnston error = copyout(data, idr->idr_data_segment, ip->ip_data_len);
1904009ea47eSEdward Tomasz Napierala free(data, M_ISCSI);
1905009ea47eSEdward Tomasz Napierala }
1906009ea47eSEdward Tomasz Napierala
1907009ea47eSEdward Tomasz Napierala icl_pdu_free(ip);
1908009ea47eSEdward Tomasz Napierala
1909*74e71380SMark Johnston return (error);
1910009ea47eSEdward Tomasz Napierala }
1911009ea47eSEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */
1912009ea47eSEdward Tomasz Napierala
1913009ea47eSEdward Tomasz Napierala static void
iscsi_sanitize_session_conf(struct iscsi_session_conf * isc)1914009ea47eSEdward Tomasz Napierala iscsi_sanitize_session_conf(struct iscsi_session_conf *isc)
1915009ea47eSEdward Tomasz Napierala {
1916009ea47eSEdward Tomasz Napierala /*
1917009ea47eSEdward Tomasz Napierala * Just make sure all the fields are null-terminated.
1918009ea47eSEdward Tomasz Napierala *
1919009ea47eSEdward Tomasz Napierala * XXX: This is not particularly secure. We should
1920009ea47eSEdward Tomasz Napierala * create our own conf and then copy in relevant
1921009ea47eSEdward Tomasz Napierala * fields.
1922009ea47eSEdward Tomasz Napierala */
1923009ea47eSEdward Tomasz Napierala isc->isc_initiator[ISCSI_NAME_LEN - 1] = '\0';
1924009ea47eSEdward Tomasz Napierala isc->isc_initiator_addr[ISCSI_ADDR_LEN - 1] = '\0';
1925009ea47eSEdward Tomasz Napierala isc->isc_initiator_alias[ISCSI_ALIAS_LEN - 1] = '\0';
1926009ea47eSEdward Tomasz Napierala isc->isc_target[ISCSI_NAME_LEN - 1] = '\0';
1927009ea47eSEdward Tomasz Napierala isc->isc_target_addr[ISCSI_ADDR_LEN - 1] = '\0';
1928009ea47eSEdward Tomasz Napierala isc->isc_user[ISCSI_NAME_LEN - 1] = '\0';
1929009ea47eSEdward Tomasz Napierala isc->isc_secret[ISCSI_SECRET_LEN - 1] = '\0';
1930009ea47eSEdward Tomasz Napierala isc->isc_mutual_user[ISCSI_NAME_LEN - 1] = '\0';
1931009ea47eSEdward Tomasz Napierala isc->isc_mutual_secret[ISCSI_SECRET_LEN - 1] = '\0';
1932009ea47eSEdward Tomasz Napierala }
1933009ea47eSEdward Tomasz Napierala
193451be90b5SEdward Tomasz Napierala static bool
iscsi_valid_session_conf(const struct iscsi_session_conf * isc)193551be90b5SEdward Tomasz Napierala iscsi_valid_session_conf(const struct iscsi_session_conf *isc)
193651be90b5SEdward Tomasz Napierala {
193751be90b5SEdward Tomasz Napierala
193851be90b5SEdward Tomasz Napierala if (isc->isc_initiator[0] == '\0') {
193951be90b5SEdward Tomasz Napierala ISCSI_DEBUG("empty isc_initiator");
194051be90b5SEdward Tomasz Napierala return (false);
194151be90b5SEdward Tomasz Napierala }
194251be90b5SEdward Tomasz Napierala
194351be90b5SEdward Tomasz Napierala if (isc->isc_target_addr[0] == '\0') {
194451be90b5SEdward Tomasz Napierala ISCSI_DEBUG("empty isc_target_addr");
194551be90b5SEdward Tomasz Napierala return (false);
194651be90b5SEdward Tomasz Napierala }
194751be90b5SEdward Tomasz Napierala
194851be90b5SEdward Tomasz Napierala if (isc->isc_discovery != 0 && isc->isc_target[0] != 0) {
194951be90b5SEdward Tomasz Napierala ISCSI_DEBUG("non-empty isc_target for discovery session");
195051be90b5SEdward Tomasz Napierala return (false);
195151be90b5SEdward Tomasz Napierala }
195251be90b5SEdward Tomasz Napierala
195351be90b5SEdward Tomasz Napierala if (isc->isc_discovery == 0 && isc->isc_target[0] == 0) {
195451be90b5SEdward Tomasz Napierala ISCSI_DEBUG("empty isc_target for non-discovery session");
195551be90b5SEdward Tomasz Napierala return (false);
195651be90b5SEdward Tomasz Napierala }
195751be90b5SEdward Tomasz Napierala
195851be90b5SEdward Tomasz Napierala return (true);
195951be90b5SEdward Tomasz Napierala }
196051be90b5SEdward Tomasz Napierala
1961009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_session_add(struct iscsi_softc * sc,struct iscsi_session_add * isa)1962009ea47eSEdward Tomasz Napierala iscsi_ioctl_session_add(struct iscsi_softc *sc, struct iscsi_session_add *isa)
1963009ea47eSEdward Tomasz Napierala {
1964009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
1965009ea47eSEdward Tomasz Napierala const struct iscsi_session *is2;
1966009ea47eSEdward Tomasz Napierala int error;
1967972a7d95SRichard Scheffenegger sbintime_t sbt, pr;
1968009ea47eSEdward Tomasz Napierala
1969009ea47eSEdward Tomasz Napierala iscsi_sanitize_session_conf(&isa->isa_conf);
197051be90b5SEdward Tomasz Napierala if (iscsi_valid_session_conf(&isa->isa_conf) == false)
197151be90b5SEdward Tomasz Napierala return (EINVAL);
1972009ea47eSEdward Tomasz Napierala
1973009ea47eSEdward Tomasz Napierala is = malloc(sizeof(*is), M_ISCSI, M_ZERO | M_WAITOK);
1974009ea47eSEdward Tomasz Napierala memcpy(&is->is_conf, &isa->isa_conf, sizeof(is->is_conf));
1975009ea47eSEdward Tomasz Napierala
1976009ea47eSEdward Tomasz Napierala sx_xlock(&sc->sc_lock);
1977009ea47eSEdward Tomasz Napierala
1978009ea47eSEdward Tomasz Napierala /*
1979009ea47eSEdward Tomasz Napierala * Prevent duplicates.
1980009ea47eSEdward Tomasz Napierala */
1981009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
19827843bd03SEdward Tomasz Napierala if (!!is->is_conf.isc_discovery !=
19837843bd03SEdward Tomasz Napierala !!is2->is_conf.isc_discovery)
19847843bd03SEdward Tomasz Napierala continue;
19857843bd03SEdward Tomasz Napierala
19867843bd03SEdward Tomasz Napierala if (strcmp(is->is_conf.isc_target_addr,
19877843bd03SEdward Tomasz Napierala is2->is_conf.isc_target_addr) != 0)
19887843bd03SEdward Tomasz Napierala continue;
19897843bd03SEdward Tomasz Napierala
19907843bd03SEdward Tomasz Napierala if (is->is_conf.isc_discovery == 0 &&
19917843bd03SEdward Tomasz Napierala strcmp(is->is_conf.isc_target,
19927843bd03SEdward Tomasz Napierala is2->is_conf.isc_target) != 0)
19937843bd03SEdward Tomasz Napierala continue;
19947843bd03SEdward Tomasz Napierala
1995009ea47eSEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
19967843bd03SEdward Tomasz Napierala free(is, M_ISCSI);
1997009ea47eSEdward Tomasz Napierala return (EBUSY);
1998009ea47eSEdward Tomasz Napierala }
1999009ea47eSEdward Tomasz Napierala
200082babffbSEdward Tomasz Napierala is->is_conn = icl_new_conn(is->is_conf.isc_offload,
2001b8911594SEdward Tomasz Napierala is->is_conf.isc_iser, "iscsi", &is->is_lock);
200282babffbSEdward Tomasz Napierala if (is->is_conn == NULL) {
200382babffbSEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
200482babffbSEdward Tomasz Napierala free(is, M_ISCSI);
200582babffbSEdward Tomasz Napierala return (EINVAL);
200682babffbSEdward Tomasz Napierala }
2007009ea47eSEdward Tomasz Napierala is->is_conn->ic_receive = iscsi_receive_callback;
2008009ea47eSEdward Tomasz Napierala is->is_conn->ic_error = iscsi_error_callback;
2009009ea47eSEdward Tomasz Napierala is->is_conn->ic_prv0 = is;
2010009ea47eSEdward Tomasz Napierala TAILQ_INIT(&is->is_outstanding);
201116c158a5SEdward Tomasz Napierala STAILQ_INIT(&is->is_postponed);
2012009ea47eSEdward Tomasz Napierala mtx_init(&is->is_lock, "iscsi_lock", NULL, MTX_DEF);
2013009ea47eSEdward Tomasz Napierala cv_init(&is->is_maintenance_cv, "iscsi_mt");
2014009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2015009ea47eSEdward Tomasz Napierala cv_init(&is->is_login_cv, "iscsi_login");
2016009ea47eSEdward Tomasz Napierala #endif
2017009ea47eSEdward Tomasz Napierala
20180cc7d64aSJohn Baldwin /*
20190cc7d64aSJohn Baldwin * Set some default values, from RFC 3720, section 12.
20200cc7d64aSJohn Baldwin *
20210cc7d64aSJohn Baldwin * These values are updated by the handoff IOCTL, but are
20220cc7d64aSJohn Baldwin * needed prior to the handoff to support sending the ISER
20230cc7d64aSJohn Baldwin * login PDU.
20240cc7d64aSJohn Baldwin */
20250cc7d64aSJohn Baldwin is->is_conn->ic_max_recv_data_segment_length = 8192;
20260cc7d64aSJohn Baldwin is->is_conn->ic_max_send_data_segment_length = 8192;
20270cc7d64aSJohn Baldwin is->is_max_burst_length = 262144;
20280cc7d64aSJohn Baldwin is->is_first_burst_length = 65536;
20290cc7d64aSJohn Baldwin
2030009ea47eSEdward Tomasz Napierala is->is_softc = sc;
2031009ea47eSEdward Tomasz Napierala sc->sc_last_session_id++;
2032009ea47eSEdward Tomasz Napierala is->is_id = sc->sc_last_session_id;
2033ffe82e05SAlexander Motin is->is_isid[0] = 0x80; /* RFC 3720, 10.12.5: 10b, "Random" ISID. */
2034ffe82e05SAlexander Motin arc4rand(&is->is_isid[1], 5, 0);
2035ffe82e05SAlexander Motin is->is_tsih = 0;
2036009ea47eSEdward Tomasz Napierala callout_init(&is->is_callout, 1);
2037009ea47eSEdward Tomasz Napierala
2038009ea47eSEdward Tomasz Napierala error = kthread_add(iscsi_maintenance_thread, is, NULL, NULL, 0, 0, "iscsimt");
2039009ea47eSEdward Tomasz Napierala if (error != 0) {
2040009ea47eSEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "kthread_add(9) failed with error %d", error);
204109f2b94bSEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
2042009ea47eSEdward Tomasz Napierala return (error);
2043009ea47eSEdward Tomasz Napierala }
2044bd6bb493SRichard Scheffenegger is->is_ping_timeout = is->is_conf.isc_ping_timeout;
2045bd6bb493SRichard Scheffenegger if (is->is_ping_timeout < 0)
2046bd6bb493SRichard Scheffenegger is->is_ping_timeout = ping_timeout;
2047bd6bb493SRichard Scheffenegger is->is_login_timeout = is->is_conf.isc_login_timeout;
2048bd6bb493SRichard Scheffenegger if (is->is_login_timeout < 0)
2049bd6bb493SRichard Scheffenegger is->is_login_timeout = login_timeout;
2050009ea47eSEdward Tomasz Napierala
2051972a7d95SRichard Scheffenegger sbt = mstosbt(995);
2052972a7d95SRichard Scheffenegger pr = mstosbt(10);
2053972a7d95SRichard Scheffenegger callout_reset_sbt(&is->is_callout, sbt, pr, iscsi_callout, is, 0);
205409f2b94bSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&sc->sc_sessions, is, is_next);
205509f2b94bSEdward Tomasz Napierala
20562be596adSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
2057ba165a31SEdward Tomasz Napierala /*
2058ba165a31SEdward Tomasz Napierala * Don't notify iscsid(8) if the session is disabled and it's not
2059ba165a31SEdward Tomasz Napierala * a discovery session,
2060ba165a31SEdward Tomasz Napierala */
2061ba165a31SEdward Tomasz Napierala if (is->is_conf.isc_enable == 0 && is->is_conf.isc_discovery == 0) {
2062ba165a31SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
2063ba165a31SEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
2064ba165a31SEdward Tomasz Napierala return (0);
2065ba165a31SEdward Tomasz Napierala }
2066ba165a31SEdward Tomasz Napierala
2067009ea47eSEdward Tomasz Napierala is->is_waiting_for_iscsid = true;
2068009ea47eSEdward Tomasz Napierala strlcpy(is->is_reason, "Waiting for iscsid(8)", sizeof(is->is_reason));
20692be596adSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
2070009ea47eSEdward Tomasz Napierala cv_signal(&sc->sc_cv);
2071009ea47eSEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
2072009ea47eSEdward Tomasz Napierala return (0);
2073009ea47eSEdward Tomasz Napierala }
2074009ea47eSEdward Tomasz Napierala
2075009ea47eSEdward Tomasz Napierala static bool
iscsi_session_conf_matches(unsigned int id1,const struct iscsi_session_conf * c1,unsigned int id2,const struct iscsi_session_conf * c2)2076009ea47eSEdward Tomasz Napierala iscsi_session_conf_matches(unsigned int id1, const struct iscsi_session_conf *c1,
2077009ea47eSEdward Tomasz Napierala unsigned int id2, const struct iscsi_session_conf *c2)
2078009ea47eSEdward Tomasz Napierala {
20796cc1e267SEdward Tomasz Napierala
20806cc1e267SEdward Tomasz Napierala if (id2 != 0 && id2 != id1)
2081009ea47eSEdward Tomasz Napierala return (false);
20826cc1e267SEdward Tomasz Napierala if (c2->isc_target[0] != '\0' &&
20836cc1e267SEdward Tomasz Napierala strcmp(c1->isc_target, c2->isc_target) != 0)
20846cc1e267SEdward Tomasz Napierala return (false);
20856cc1e267SEdward Tomasz Napierala if (c2->isc_target_addr[0] != '\0' &&
20866cc1e267SEdward Tomasz Napierala strcmp(c1->isc_target_addr, c2->isc_target_addr) != 0)
20876cc1e267SEdward Tomasz Napierala return (false);
20886cc1e267SEdward Tomasz Napierala return (true);
2089009ea47eSEdward Tomasz Napierala }
2090009ea47eSEdward Tomasz Napierala
2091009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_session_remove(struct iscsi_softc * sc,struct iscsi_session_remove * isr)2092009ea47eSEdward Tomasz Napierala iscsi_ioctl_session_remove(struct iscsi_softc *sc,
2093009ea47eSEdward Tomasz Napierala struct iscsi_session_remove *isr)
2094009ea47eSEdward Tomasz Napierala {
2095009ea47eSEdward Tomasz Napierala struct iscsi_session *is, *tmp;
2096009ea47eSEdward Tomasz Napierala bool found = false;
2097009ea47eSEdward Tomasz Napierala
2098009ea47eSEdward Tomasz Napierala iscsi_sanitize_session_conf(&isr->isr_conf);
2099009ea47eSEdward Tomasz Napierala
2100009ea47eSEdward Tomasz Napierala sx_xlock(&sc->sc_lock);
2101009ea47eSEdward Tomasz Napierala TAILQ_FOREACH_SAFE(is, &sc->sc_sessions, is_next, tmp) {
2102009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
2103009ea47eSEdward Tomasz Napierala if (iscsi_session_conf_matches(is->is_id, &is->is_conf,
2104009ea47eSEdward Tomasz Napierala isr->isr_session_id, &isr->isr_conf)) {
2105009ea47eSEdward Tomasz Napierala found = true;
2106009ea47eSEdward Tomasz Napierala iscsi_session_logout(is);
2107009ea47eSEdward Tomasz Napierala iscsi_session_terminate(is);
2108009ea47eSEdward Tomasz Napierala }
2109009ea47eSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
2110009ea47eSEdward Tomasz Napierala }
2111009ea47eSEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
2112009ea47eSEdward Tomasz Napierala
2113009ea47eSEdward Tomasz Napierala if (!found)
2114009ea47eSEdward Tomasz Napierala return (ESRCH);
2115009ea47eSEdward Tomasz Napierala
2116009ea47eSEdward Tomasz Napierala return (0);
2117009ea47eSEdward Tomasz Napierala }
2118009ea47eSEdward Tomasz Napierala
2119009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_session_list(struct iscsi_softc * sc,struct iscsi_session_list * isl)2120009ea47eSEdward Tomasz Napierala iscsi_ioctl_session_list(struct iscsi_softc *sc, struct iscsi_session_list *isl)
2121009ea47eSEdward Tomasz Napierala {
2122009ea47eSEdward Tomasz Napierala int error;
2123009ea47eSEdward Tomasz Napierala unsigned int i = 0;
2124009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
2125009ea47eSEdward Tomasz Napierala struct iscsi_session_state iss;
2126009ea47eSEdward Tomasz Napierala
2127009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
2128009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2129009ea47eSEdward Tomasz Napierala if (i >= isl->isl_nentries) {
2130009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
2131009ea47eSEdward Tomasz Napierala return (EMSGSIZE);
2132009ea47eSEdward Tomasz Napierala }
2133009ea47eSEdward Tomasz Napierala memset(&iss, 0, sizeof(iss));
2134009ea47eSEdward Tomasz Napierala memcpy(&iss.iss_conf, &is->is_conf, sizeof(iss.iss_conf));
2135009ea47eSEdward Tomasz Napierala iss.iss_id = is->is_id;
2136009ea47eSEdward Tomasz Napierala strlcpy(iss.iss_target_alias, is->is_target_alias, sizeof(iss.iss_target_alias));
2137009ea47eSEdward Tomasz Napierala strlcpy(iss.iss_reason, is->is_reason, sizeof(iss.iss_reason));
213882babffbSEdward Tomasz Napierala strlcpy(iss.iss_offload, is->is_conn->ic_offload, sizeof(iss.iss_offload));
2139009ea47eSEdward Tomasz Napierala
2140009ea47eSEdward Tomasz Napierala if (is->is_conn->ic_header_crc32c)
2141009ea47eSEdward Tomasz Napierala iss.iss_header_digest = ISCSI_DIGEST_CRC32C;
2142009ea47eSEdward Tomasz Napierala else
2143009ea47eSEdward Tomasz Napierala iss.iss_header_digest = ISCSI_DIGEST_NONE;
2144009ea47eSEdward Tomasz Napierala
2145009ea47eSEdward Tomasz Napierala if (is->is_conn->ic_data_crc32c)
2146009ea47eSEdward Tomasz Napierala iss.iss_data_digest = ISCSI_DIGEST_CRC32C;
2147009ea47eSEdward Tomasz Napierala else
2148009ea47eSEdward Tomasz Napierala iss.iss_data_digest = ISCSI_DIGEST_NONE;
2149009ea47eSEdward Tomasz Napierala
215097b84d34SNavdeep Parhar iss.iss_max_send_data_segment_length =
21510cc7d64aSJohn Baldwin is->is_conn->ic_max_send_data_segment_length;
215297b84d34SNavdeep Parhar iss.iss_max_recv_data_segment_length =
21530cc7d64aSJohn Baldwin is->is_conn->ic_max_recv_data_segment_length;
21544e5408f1SEdward Tomasz Napierala iss.iss_max_burst_length = is->is_max_burst_length;
21554e5408f1SEdward Tomasz Napierala iss.iss_first_burst_length = is->is_first_burst_length;
2156009ea47eSEdward Tomasz Napierala iss.iss_immediate_data = is->is_immediate_data;
2157009ea47eSEdward Tomasz Napierala iss.iss_connected = is->is_connected;
2158009ea47eSEdward Tomasz Napierala
2159009ea47eSEdward Tomasz Napierala error = copyout(&iss, isl->isl_pstates + i, sizeof(iss));
2160009ea47eSEdward Tomasz Napierala if (error != 0) {
2161009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
2162009ea47eSEdward Tomasz Napierala return (error);
2163009ea47eSEdward Tomasz Napierala }
2164009ea47eSEdward Tomasz Napierala i++;
2165009ea47eSEdward Tomasz Napierala }
2166009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
2167009ea47eSEdward Tomasz Napierala
2168009ea47eSEdward Tomasz Napierala isl->isl_nentries = i;
2169009ea47eSEdward Tomasz Napierala
2170009ea47eSEdward Tomasz Napierala return (0);
2171009ea47eSEdward Tomasz Napierala }
2172009ea47eSEdward Tomasz Napierala
2173009ea47eSEdward Tomasz Napierala static int
iscsi_ioctl_session_modify(struct iscsi_softc * sc,struct iscsi_session_modify * ism)217451be90b5SEdward Tomasz Napierala iscsi_ioctl_session_modify(struct iscsi_softc *sc,
217551be90b5SEdward Tomasz Napierala struct iscsi_session_modify *ism)
217651be90b5SEdward Tomasz Napierala {
217751be90b5SEdward Tomasz Napierala struct iscsi_session *is;
21788ff2372aSEdward Tomasz Napierala const struct iscsi_session *is2;
217951be90b5SEdward Tomasz Napierala
218051be90b5SEdward Tomasz Napierala iscsi_sanitize_session_conf(&ism->ism_conf);
218151be90b5SEdward Tomasz Napierala if (iscsi_valid_session_conf(&ism->ism_conf) == false)
218251be90b5SEdward Tomasz Napierala return (EINVAL);
218351be90b5SEdward Tomasz Napierala
218451be90b5SEdward Tomasz Napierala sx_xlock(&sc->sc_lock);
218551be90b5SEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
218651be90b5SEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
21878ff2372aSEdward Tomasz Napierala if (is->is_id == ism->ism_session_id) {
21888ff2372aSEdward Tomasz Napierala /* Note that the session remains locked. */
218951be90b5SEdward Tomasz Napierala break;
21908ff2372aSEdward Tomasz Napierala }
219151be90b5SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
219251be90b5SEdward Tomasz Napierala }
219351be90b5SEdward Tomasz Napierala if (is == NULL) {
219451be90b5SEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
219551be90b5SEdward Tomasz Napierala return (ESRCH);
219651be90b5SEdward Tomasz Napierala }
21978ff2372aSEdward Tomasz Napierala
21988ff2372aSEdward Tomasz Napierala /*
21998ff2372aSEdward Tomasz Napierala * Prevent duplicates.
22008ff2372aSEdward Tomasz Napierala */
22018ff2372aSEdward Tomasz Napierala TAILQ_FOREACH(is2, &sc->sc_sessions, is_next) {
22028ff2372aSEdward Tomasz Napierala if (is == is2)
22038ff2372aSEdward Tomasz Napierala continue;
22048ff2372aSEdward Tomasz Napierala
22058ff2372aSEdward Tomasz Napierala if (!!ism->ism_conf.isc_discovery !=
22068ff2372aSEdward Tomasz Napierala !!is2->is_conf.isc_discovery)
22078ff2372aSEdward Tomasz Napierala continue;
22088ff2372aSEdward Tomasz Napierala
22098ff2372aSEdward Tomasz Napierala if (strcmp(ism->ism_conf.isc_target_addr,
22108ff2372aSEdward Tomasz Napierala is2->is_conf.isc_target_addr) != 0)
22118ff2372aSEdward Tomasz Napierala continue;
22128ff2372aSEdward Tomasz Napierala
22138ff2372aSEdward Tomasz Napierala if (ism->ism_conf.isc_discovery == 0 &&
22148ff2372aSEdward Tomasz Napierala strcmp(ism->ism_conf.isc_target,
22158ff2372aSEdward Tomasz Napierala is2->is_conf.isc_target) != 0)
22168ff2372aSEdward Tomasz Napierala continue;
22178ff2372aSEdward Tomasz Napierala
22188ff2372aSEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
22198ff2372aSEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
22208ff2372aSEdward Tomasz Napierala return (EBUSY);
22218ff2372aSEdward Tomasz Napierala }
22228ff2372aSEdward Tomasz Napierala
222351be90b5SEdward Tomasz Napierala sx_xunlock(&sc->sc_lock);
222451be90b5SEdward Tomasz Napierala
222551be90b5SEdward Tomasz Napierala memcpy(&is->is_conf, &ism->ism_conf, sizeof(is->is_conf));
222651be90b5SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
222751be90b5SEdward Tomasz Napierala
222851be90b5SEdward Tomasz Napierala iscsi_session_reconnect(is);
222951be90b5SEdward Tomasz Napierala
223051be90b5SEdward Tomasz Napierala return (0);
223151be90b5SEdward Tomasz Napierala }
223251be90b5SEdward Tomasz Napierala
223351be90b5SEdward Tomasz Napierala static int
iscsi_ioctl(struct cdev * dev,u_long cmd,caddr_t arg,int mode,struct thread * td)2234009ea47eSEdward Tomasz Napierala iscsi_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
2235009ea47eSEdward Tomasz Napierala struct thread *td)
2236009ea47eSEdward Tomasz Napierala {
2237009ea47eSEdward Tomasz Napierala struct iscsi_softc *sc;
2238009ea47eSEdward Tomasz Napierala
2239009ea47eSEdward Tomasz Napierala sc = dev->si_drv1;
2240009ea47eSEdward Tomasz Napierala
2241009ea47eSEdward Tomasz Napierala switch (cmd) {
2242009ea47eSEdward Tomasz Napierala case ISCSIDWAIT:
2243009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_daemon_wait(sc,
22447b02c1e8SJohn Baldwin (struct iscsi_daemon_request *)arg, false));
22457b02c1e8SJohn Baldwin #ifdef COMPAT_FREEBSD13
22467b02c1e8SJohn Baldwin case ISCSIDWAIT13:
22477b02c1e8SJohn Baldwin return (iscsi_ioctl_daemon_wait(sc,
22487b02c1e8SJohn Baldwin (struct iscsi_daemon_request *)arg, true));
22497b02c1e8SJohn Baldwin #endif
22507b02c1e8SJohn Baldwin case ISCSIDLIMITS:
22517b02c1e8SJohn Baldwin return (iscsi_ioctl_daemon_limits(sc,
22527b02c1e8SJohn Baldwin (struct iscsi_daemon_limits *)arg));
2253009ea47eSEdward Tomasz Napierala case ISCSIDHANDOFF:
2254009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_daemon_handoff(sc,
2255009ea47eSEdward Tomasz Napierala (struct iscsi_daemon_handoff *)arg));
2256009ea47eSEdward Tomasz Napierala case ISCSIDFAIL:
2257009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_daemon_fail(sc,
2258009ea47eSEdward Tomasz Napierala (struct iscsi_daemon_fail *)arg));
2259009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2260009ea47eSEdward Tomasz Napierala case ISCSIDCONNECT:
2261009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_daemon_connect(sc,
2262009ea47eSEdward Tomasz Napierala (struct iscsi_daemon_connect *)arg));
2263009ea47eSEdward Tomasz Napierala case ISCSIDSEND:
2264009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_daemon_send(sc,
2265009ea47eSEdward Tomasz Napierala (struct iscsi_daemon_send *)arg));
2266009ea47eSEdward Tomasz Napierala case ISCSIDRECEIVE:
2267009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_daemon_receive(sc,
2268009ea47eSEdward Tomasz Napierala (struct iscsi_daemon_receive *)arg));
2269009ea47eSEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */
2270009ea47eSEdward Tomasz Napierala case ISCSISADD:
2271009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_session_add(sc,
2272009ea47eSEdward Tomasz Napierala (struct iscsi_session_add *)arg));
2273009ea47eSEdward Tomasz Napierala case ISCSISREMOVE:
2274009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_session_remove(sc,
2275009ea47eSEdward Tomasz Napierala (struct iscsi_session_remove *)arg));
2276009ea47eSEdward Tomasz Napierala case ISCSISLIST:
2277009ea47eSEdward Tomasz Napierala return (iscsi_ioctl_session_list(sc,
2278009ea47eSEdward Tomasz Napierala (struct iscsi_session_list *)arg));
227951be90b5SEdward Tomasz Napierala case ISCSISMODIFY:
228051be90b5SEdward Tomasz Napierala return (iscsi_ioctl_session_modify(sc,
228151be90b5SEdward Tomasz Napierala (struct iscsi_session_modify *)arg));
2282009ea47eSEdward Tomasz Napierala default:
2283009ea47eSEdward Tomasz Napierala return (EINVAL);
2284009ea47eSEdward Tomasz Napierala }
2285009ea47eSEdward Tomasz Napierala }
2286009ea47eSEdward Tomasz Napierala
2287009ea47eSEdward Tomasz Napierala static struct iscsi_outstanding *
iscsi_outstanding_find(struct iscsi_session * is,uint32_t initiator_task_tag)2288009ea47eSEdward Tomasz Napierala iscsi_outstanding_find(struct iscsi_session *is, uint32_t initiator_task_tag)
2289009ea47eSEdward Tomasz Napierala {
2290009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io;
2291009ea47eSEdward Tomasz Napierala
2292009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
2293009ea47eSEdward Tomasz Napierala
2294009ea47eSEdward Tomasz Napierala TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2295009ea47eSEdward Tomasz Napierala if (io->io_initiator_task_tag == initiator_task_tag)
2296009ea47eSEdward Tomasz Napierala return (io);
2297009ea47eSEdward Tomasz Napierala }
2298009ea47eSEdward Tomasz Napierala return (NULL);
2299009ea47eSEdward Tomasz Napierala }
2300009ea47eSEdward Tomasz Napierala
2301aa75114cSAlexander Motin static struct iscsi_outstanding *
iscsi_outstanding_find_ccb(struct iscsi_session * is,union ccb * ccb)2302aa75114cSAlexander Motin iscsi_outstanding_find_ccb(struct iscsi_session *is, union ccb *ccb)
2303aa75114cSAlexander Motin {
2304aa75114cSAlexander Motin struct iscsi_outstanding *io;
2305aa75114cSAlexander Motin
2306aa75114cSAlexander Motin ISCSI_SESSION_LOCK_ASSERT(is);
2307aa75114cSAlexander Motin
2308aa75114cSAlexander Motin TAILQ_FOREACH(io, &is->is_outstanding, io_next) {
2309aa75114cSAlexander Motin if (io->io_ccb == ccb)
2310aa75114cSAlexander Motin return (io);
2311aa75114cSAlexander Motin }
2312aa75114cSAlexander Motin return (NULL);
2313aa75114cSAlexander Motin }
2314aa75114cSAlexander Motin
2315aa75114cSAlexander Motin static struct iscsi_outstanding *
iscsi_outstanding_add(struct iscsi_session * is,struct icl_pdu * request,union ccb * ccb,uint32_t * initiator_task_tagp)2316604c023fSEdward Tomasz Napierala iscsi_outstanding_add(struct iscsi_session *is, struct icl_pdu *request,
23177a03d007SEdward Tomasz Napierala union ccb *ccb, uint32_t *initiator_task_tagp)
2318009ea47eSEdward Tomasz Napierala {
2319009ea47eSEdward Tomasz Napierala struct iscsi_outstanding *io;
23207a03d007SEdward Tomasz Napierala int error;
2321009ea47eSEdward Tomasz Napierala
2322009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
2323009ea47eSEdward Tomasz Napierala
2324009ea47eSEdward Tomasz Napierala io = uma_zalloc(iscsi_outstanding_zone, M_NOWAIT | M_ZERO);
2325009ea47eSEdward Tomasz Napierala if (io == NULL) {
23267a03d007SEdward Tomasz Napierala ISCSI_SESSION_WARN(is, "failed to allocate %zd bytes",
23277a03d007SEdward Tomasz Napierala sizeof(*io));
2328aa75114cSAlexander Motin return (NULL);
2329009ea47eSEdward Tomasz Napierala }
23307a03d007SEdward Tomasz Napierala
2331604c023fSEdward Tomasz Napierala error = icl_conn_task_setup(is->is_conn, request, &ccb->csio,
23327a03d007SEdward Tomasz Napierala initiator_task_tagp, &io->io_icl_prv);
23337a03d007SEdward Tomasz Napierala if (error != 0) {
23347a03d007SEdward Tomasz Napierala ISCSI_SESSION_WARN(is,
23357a03d007SEdward Tomasz Napierala "icl_conn_task_setup() failed with error %d", error);
23367a03d007SEdward Tomasz Napierala uma_zfree(iscsi_outstanding_zone, io);
23377a03d007SEdward Tomasz Napierala return (NULL);
23387a03d007SEdward Tomasz Napierala }
23397a03d007SEdward Tomasz Napierala
23407a03d007SEdward Tomasz Napierala KASSERT(iscsi_outstanding_find(is, *initiator_task_tagp) == NULL,
23417a03d007SEdward Tomasz Napierala ("initiator_task_tag 0x%x already added", *initiator_task_tagp));
23427a03d007SEdward Tomasz Napierala
23437a03d007SEdward Tomasz Napierala io->io_initiator_task_tag = *initiator_task_tagp;
2344009ea47eSEdward Tomasz Napierala io->io_ccb = ccb;
2345009ea47eSEdward Tomasz Napierala TAILQ_INSERT_TAIL(&is->is_outstanding, io, io_next);
2346aa75114cSAlexander Motin return (io);
2347009ea47eSEdward Tomasz Napierala }
2348009ea47eSEdward Tomasz Napierala
2349009ea47eSEdward Tomasz Napierala static void
iscsi_outstanding_remove(struct iscsi_session * is,struct iscsi_outstanding * io)2350009ea47eSEdward Tomasz Napierala iscsi_outstanding_remove(struct iscsi_session *is, struct iscsi_outstanding *io)
2351009ea47eSEdward Tomasz Napierala {
2352009ea47eSEdward Tomasz Napierala
2353009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
2354009ea47eSEdward Tomasz Napierala
23557a03d007SEdward Tomasz Napierala icl_conn_task_done(is->is_conn, io->io_icl_prv);
2356009ea47eSEdward Tomasz Napierala TAILQ_REMOVE(&is->is_outstanding, io, io_next);
2357009ea47eSEdward Tomasz Napierala uma_zfree(iscsi_outstanding_zone, io);
2358009ea47eSEdward Tomasz Napierala }
2359009ea47eSEdward Tomasz Napierala
2360009ea47eSEdward Tomasz Napierala static void
iscsi_action_abort(struct iscsi_session * is,union ccb * ccb)2361aa75114cSAlexander Motin iscsi_action_abort(struct iscsi_session *is, union ccb *ccb)
2362aa75114cSAlexander Motin {
2363aa75114cSAlexander Motin struct icl_pdu *request;
2364aa75114cSAlexander Motin struct iscsi_bhs_task_management_request *bhstmr;
2365aa75114cSAlexander Motin struct ccb_abort *cab = &ccb->cab;
2366aa75114cSAlexander Motin struct iscsi_outstanding *io, *aio;
23677a03d007SEdward Tomasz Napierala uint32_t initiator_task_tag;
2368aa75114cSAlexander Motin
2369aa75114cSAlexander Motin ISCSI_SESSION_LOCK_ASSERT(is);
2370aa75114cSAlexander Motin
2371aa75114cSAlexander Motin #if 0
2372aa75114cSAlexander Motin KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2373aa75114cSAlexander Motin #else
2374aa75114cSAlexander Motin if (is->is_login_phase) {
2375aa75114cSAlexander Motin ccb->ccb_h.status = CAM_REQ_ABORTED;
2376aa75114cSAlexander Motin xpt_done(ccb);
2377aa75114cSAlexander Motin return;
2378aa75114cSAlexander Motin }
2379aa75114cSAlexander Motin #endif
2380aa75114cSAlexander Motin
2381aa75114cSAlexander Motin aio = iscsi_outstanding_find_ccb(is, cab->abort_ccb);
2382aa75114cSAlexander Motin if (aio == NULL) {
2383aa75114cSAlexander Motin ccb->ccb_h.status = CAM_REQ_CMP;
2384aa75114cSAlexander Motin xpt_done(ccb);
2385aa75114cSAlexander Motin return;
2386aa75114cSAlexander Motin }
2387aa75114cSAlexander Motin
2388d2d52b00SEdward Tomasz Napierala request = icl_pdu_new(is->is_conn, M_NOWAIT);
2389aa75114cSAlexander Motin if (request == NULL) {
2390aa75114cSAlexander Motin ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2391aa75114cSAlexander Motin xpt_done(ccb);
2392aa75114cSAlexander Motin return;
2393aa75114cSAlexander Motin }
2394aa75114cSAlexander Motin
23957a03d007SEdward Tomasz Napierala initiator_task_tag = is->is_initiator_task_tag++;
23969bee9a98SAlexander Motin if (initiator_task_tag == 0xffffffff)
23979bee9a98SAlexander Motin initiator_task_tag = is->is_initiator_task_tag++;
2398aa75114cSAlexander Motin
2399604c023fSEdward Tomasz Napierala io = iscsi_outstanding_add(is, request, NULL, &initiator_task_tag);
2400aa75114cSAlexander Motin if (io == NULL) {
2401aa75114cSAlexander Motin icl_pdu_free(request);
2402aa75114cSAlexander Motin ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2403aa75114cSAlexander Motin xpt_done(ccb);
2404aa75114cSAlexander Motin return;
2405aa75114cSAlexander Motin }
24063dd2a7a5SAlexander Motin io->io_referenced_task_tag = aio->io_initiator_task_tag;
24077a03d007SEdward Tomasz Napierala
24087a03d007SEdward Tomasz Napierala bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
24097a03d007SEdward Tomasz Napierala bhstmr->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_REQUEST;
24107a03d007SEdward Tomasz Napierala bhstmr->bhstmr_function = 0x80 | BHSTMR_FUNCTION_ABORT_TASK;
24117a03d007SEdward Tomasz Napierala bhstmr->bhstmr_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
24127a03d007SEdward Tomasz Napierala bhstmr->bhstmr_initiator_task_tag = initiator_task_tag;
24137a03d007SEdward Tomasz Napierala bhstmr->bhstmr_referenced_task_tag = aio->io_initiator_task_tag;
24147a03d007SEdward Tomasz Napierala
2415aa75114cSAlexander Motin iscsi_pdu_queue_locked(request);
2416aa75114cSAlexander Motin }
2417aa75114cSAlexander Motin
2418aa75114cSAlexander Motin static void
iscsi_action_scsiio(struct iscsi_session * is,union ccb * ccb)2419009ea47eSEdward Tomasz Napierala iscsi_action_scsiio(struct iscsi_session *is, union ccb *ccb)
2420009ea47eSEdward Tomasz Napierala {
2421009ea47eSEdward Tomasz Napierala struct icl_pdu *request;
2422009ea47eSEdward Tomasz Napierala struct iscsi_bhs_scsi_command *bhssc;
2423009ea47eSEdward Tomasz Napierala struct ccb_scsiio *csio;
2424aa75114cSAlexander Motin struct iscsi_outstanding *io;
2425009ea47eSEdward Tomasz Napierala size_t len;
24267a03d007SEdward Tomasz Napierala uint32_t initiator_task_tag;
2427009ea47eSEdward Tomasz Napierala int error;
2428009ea47eSEdward Tomasz Napierala
2429009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
2430009ea47eSEdward Tomasz Napierala
2431009ea47eSEdward Tomasz Napierala #if 0
2432009ea47eSEdward Tomasz Napierala KASSERT(is->is_login_phase == false, ("%s called during Login Phase", __func__));
2433009ea47eSEdward Tomasz Napierala #else
2434009ea47eSEdward Tomasz Napierala if (is->is_login_phase) {
2435009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "called during login phase");
2436009ea47eSEdward Tomasz Napierala if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2437009ea47eSEdward Tomasz Napierala xpt_freeze_devq(ccb->ccb_h.path, 1);
2438009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
2439009ea47eSEdward Tomasz Napierala }
2440009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_REQ_ABORTED | CAM_DEV_QFRZN;
2441009ea47eSEdward Tomasz Napierala xpt_done(ccb);
2442009ea47eSEdward Tomasz Napierala return;
2443009ea47eSEdward Tomasz Napierala }
2444009ea47eSEdward Tomasz Napierala #endif
2445009ea47eSEdward Tomasz Napierala
2446d2d52b00SEdward Tomasz Napierala request = icl_pdu_new(is->is_conn, M_NOWAIT);
2447009ea47eSEdward Tomasz Napierala if (request == NULL) {
2448009ea47eSEdward Tomasz Napierala if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2449009ea47eSEdward Tomasz Napierala xpt_freeze_devq(ccb->ccb_h.path, 1);
2450009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
2451009ea47eSEdward Tomasz Napierala }
2452009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2453009ea47eSEdward Tomasz Napierala xpt_done(ccb);
2454009ea47eSEdward Tomasz Napierala return;
2455009ea47eSEdward Tomasz Napierala }
2456009ea47eSEdward Tomasz Napierala
24577a03d007SEdward Tomasz Napierala initiator_task_tag = is->is_initiator_task_tag++;
24589bee9a98SAlexander Motin if (initiator_task_tag == 0xffffffff)
24599bee9a98SAlexander Motin initiator_task_tag = is->is_initiator_task_tag++;
24609bee9a98SAlexander Motin
2461604c023fSEdward Tomasz Napierala io = iscsi_outstanding_add(is, request, ccb, &initiator_task_tag);
24627a03d007SEdward Tomasz Napierala if (io == NULL) {
24637a03d007SEdward Tomasz Napierala icl_pdu_free(request);
24647a03d007SEdward Tomasz Napierala if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
24657a03d007SEdward Tomasz Napierala xpt_freeze_devq(ccb->ccb_h.path, 1);
24667a03d007SEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
24677a03d007SEdward Tomasz Napierala }
24687a03d007SEdward Tomasz Napierala ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
24697a03d007SEdward Tomasz Napierala xpt_done(ccb);
24707a03d007SEdward Tomasz Napierala return;
24717a03d007SEdward Tomasz Napierala }
24727a03d007SEdward Tomasz Napierala
2473009ea47eSEdward Tomasz Napierala csio = &ccb->csio;
2474009ea47eSEdward Tomasz Napierala bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2475009ea47eSEdward Tomasz Napierala bhssc->bhssc_opcode = ISCSI_BHS_OPCODE_SCSI_COMMAND;
2476009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_F;
2477009ea47eSEdward Tomasz Napierala switch (csio->ccb_h.flags & CAM_DIR_MASK) {
2478009ea47eSEdward Tomasz Napierala case CAM_DIR_IN:
2479009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_R;
2480009ea47eSEdward Tomasz Napierala break;
2481009ea47eSEdward Tomasz Napierala case CAM_DIR_OUT:
2482009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_W;
2483009ea47eSEdward Tomasz Napierala break;
2484009ea47eSEdward Tomasz Napierala }
2485009ea47eSEdward Tomasz Napierala
2486462cf3baSAlexander Motin if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2487009ea47eSEdward Tomasz Napierala switch (csio->tag_action) {
2488009ea47eSEdward Tomasz Napierala case MSG_HEAD_OF_Q_TAG:
2489009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_HOQ;
2490009ea47eSEdward Tomasz Napierala break;
2491009ea47eSEdward Tomasz Napierala case MSG_ORDERED_Q_TAG:
2492009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ORDERED;
2493009ea47eSEdward Tomasz Napierala break;
2494009ea47eSEdward Tomasz Napierala case MSG_ACA_TASK:
2495009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_ACA;
2496009ea47eSEdward Tomasz Napierala break;
2497009ea47eSEdward Tomasz Napierala case MSG_SIMPLE_Q_TAG:
2498009ea47eSEdward Tomasz Napierala default:
2499009ea47eSEdward Tomasz Napierala bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_SIMPLE;
2500009ea47eSEdward Tomasz Napierala break;
2501009ea47eSEdward Tomasz Napierala }
2502462cf3baSAlexander Motin } else
2503462cf3baSAlexander Motin bhssc->bhssc_flags |= BHSSC_FLAGS_ATTR_UNTAGGED;
2504009ea47eSEdward Tomasz Napierala
250588364968SAlexander Motin if (is->is_protocol_level >= 2) {
250688364968SAlexander Motin bhssc->bhssc_pri = (csio->priority << BHSSC_PRI_SHIFT) &
250788364968SAlexander Motin BHSSC_PRI_MASK;
250888364968SAlexander Motin }
250988364968SAlexander Motin
2510a9f2f291SEdward Tomasz Napierala bhssc->bhssc_lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
25117a03d007SEdward Tomasz Napierala bhssc->bhssc_initiator_task_tag = initiator_task_tag;
2512009ea47eSEdward Tomasz Napierala bhssc->bhssc_expected_data_transfer_length = htonl(csio->dxfer_len);
2513009ea47eSEdward Tomasz Napierala KASSERT(csio->cdb_len <= sizeof(bhssc->bhssc_cdb),
2514009ea47eSEdward Tomasz Napierala ("unsupported CDB size %zd", (size_t)csio->cdb_len));
2515009ea47eSEdward Tomasz Napierala
2516009ea47eSEdward Tomasz Napierala if (csio->ccb_h.flags & CAM_CDB_POINTER)
2517009ea47eSEdward Tomasz Napierala memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_ptr, csio->cdb_len);
2518009ea47eSEdward Tomasz Napierala else
2519009ea47eSEdward Tomasz Napierala memcpy(&bhssc->bhssc_cdb, csio->cdb_io.cdb_bytes, csio->cdb_len);
2520009ea47eSEdward Tomasz Napierala
2521009ea47eSEdward Tomasz Napierala if (is->is_immediate_data &&
2522009ea47eSEdward Tomasz Napierala (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2523009ea47eSEdward Tomasz Napierala len = csio->dxfer_len;
2524009ea47eSEdward Tomasz Napierala //ISCSI_SESSION_DEBUG(is, "adding %zd of immediate data", len);
2525009ea47eSEdward Tomasz Napierala if (len > is->is_first_burst_length) {
252697b84d34SNavdeep Parhar ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len, is->is_first_burst_length);
2527009ea47eSEdward Tomasz Napierala len = is->is_first_burst_length;
2528009ea47eSEdward Tomasz Napierala }
25290cc7d64aSJohn Baldwin if (len > is->is_conn->ic_max_send_data_segment_length) {
253097b84d34SNavdeep Parhar ISCSI_SESSION_DEBUG(is, "len %zd -> %d", len,
25310cc7d64aSJohn Baldwin is->is_conn->ic_max_send_data_segment_length);
25320cc7d64aSJohn Baldwin len = is->is_conn->ic_max_send_data_segment_length;
25333f9e1172SAlexander Motin }
2534009ea47eSEdward Tomasz Napierala
25357aab9c14SJohn Baldwin error = iscsi_pdu_append_data_csio(request, csio, 0, len,
25369c7a4875SJohn Baldwin M_NOWAIT | ICL_NOCOPY);
2537009ea47eSEdward Tomasz Napierala if (error != 0) {
2538938bcb04SEdward Tomasz Napierala iscsi_outstanding_remove(is, io);
2539009ea47eSEdward Tomasz Napierala icl_pdu_free(request);
2540009ea47eSEdward Tomasz Napierala if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2541009ea47eSEdward Tomasz Napierala xpt_freeze_devq(ccb->ccb_h.path, 1);
2542009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "freezing devq");
2543009ea47eSEdward Tomasz Napierala }
2544009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_RESRC_UNAVAIL | CAM_DEV_QFRZN;
2545009ea47eSEdward Tomasz Napierala xpt_done(ccb);
2546009ea47eSEdward Tomasz Napierala return;
2547009ea47eSEdward Tomasz Napierala }
2548009ea47eSEdward Tomasz Napierala }
2549009ea47eSEdward Tomasz Napierala iscsi_pdu_queue_locked(request);
2550009ea47eSEdward Tomasz Napierala }
2551009ea47eSEdward Tomasz Napierala
2552009ea47eSEdward Tomasz Napierala static void
iscsi_action(struct cam_sim * sim,union ccb * ccb)2553009ea47eSEdward Tomasz Napierala iscsi_action(struct cam_sim *sim, union ccb *ccb)
2554009ea47eSEdward Tomasz Napierala {
2555009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
2556009ea47eSEdward Tomasz Napierala
2557009ea47eSEdward Tomasz Napierala is = cam_sim_softc(sim);
2558009ea47eSEdward Tomasz Napierala
2559009ea47eSEdward Tomasz Napierala ISCSI_SESSION_LOCK_ASSERT(is);
2560009ea47eSEdward Tomasz Napierala
2561f1f12e40SEdward Tomasz Napierala if (is->is_terminating ||
2562f1f12e40SEdward Tomasz Napierala (is->is_connected == false && fail_on_disconnection)) {
2563009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2564009ea47eSEdward Tomasz Napierala xpt_done(ccb);
2565009ea47eSEdward Tomasz Napierala return;
2566009ea47eSEdward Tomasz Napierala }
2567009ea47eSEdward Tomasz Napierala
2568186bcdaaSEdward Tomasz Napierala /*
2569186bcdaaSEdward Tomasz Napierala * Make sure CAM doesn't sneak in a CCB just after freezing the queue.
2570186bcdaaSEdward Tomasz Napierala */
2571186bcdaaSEdward Tomasz Napierala if (is->is_simq_frozen == true) {
2572186bcdaaSEdward Tomasz Napierala ccb->ccb_h.status &= ~(CAM_SIM_QUEUED | CAM_STATUS_MASK);
2573186bcdaaSEdward Tomasz Napierala ccb->ccb_h.status |= CAM_REQUEUE_REQ;
2574186bcdaaSEdward Tomasz Napierala /* Don't freeze the devq - the SIM queue is already frozen. */
2575186bcdaaSEdward Tomasz Napierala xpt_done(ccb);
2576186bcdaaSEdward Tomasz Napierala return;
2577186bcdaaSEdward Tomasz Napierala }
2578186bcdaaSEdward Tomasz Napierala
2579009ea47eSEdward Tomasz Napierala switch (ccb->ccb_h.func_code) {
2580009ea47eSEdward Tomasz Napierala case XPT_PATH_INQ:
2581009ea47eSEdward Tomasz Napierala {
2582009ea47eSEdward Tomasz Napierala struct ccb_pathinq *cpi = &ccb->cpi;
2583009ea47eSEdward Tomasz Napierala
2584009ea47eSEdward Tomasz Napierala cpi->version_num = 1;
2585009ea47eSEdward Tomasz Napierala cpi->hba_inquiry = PI_TAG_ABLE;
2586009ea47eSEdward Tomasz Napierala cpi->target_sprt = 0;
2587a9f2f291SEdward Tomasz Napierala cpi->hba_misc = PIM_EXTLUNS;
25887deb68abSEdward Tomasz Napierala /*
25897deb68abSEdward Tomasz Napierala * XXX: It shouldn't ever be NULL; this could be turned
25907deb68abSEdward Tomasz Napierala * into a KASSERT eventually.
25917deb68abSEdward Tomasz Napierala */
25927deb68abSEdward Tomasz Napierala if (is->is_conn == NULL)
25937deb68abSEdward Tomasz Napierala ISCSI_WARN("NULL conn");
25947deb68abSEdward Tomasz Napierala else if (is->is_conn->ic_unmapped)
25957deb68abSEdward Tomasz Napierala cpi->hba_misc |= PIM_UNMAPPED;
2596009ea47eSEdward Tomasz Napierala cpi->hba_eng_cnt = 0;
2597009ea47eSEdward Tomasz Napierala cpi->max_target = 0;
259817cf3eb1SEdward Tomasz Napierala /*
259917cf3eb1SEdward Tomasz Napierala * Note that the variable below is only relevant for targets
260017cf3eb1SEdward Tomasz Napierala * that don't claim compliance with anything above SPC2, which
260117cf3eb1SEdward Tomasz Napierala * means they don't support REPORT_LUNS.
260217cf3eb1SEdward Tomasz Napierala */
260317cf3eb1SEdward Tomasz Napierala cpi->max_lun = 255;
2604a9f2f291SEdward Tomasz Napierala cpi->initiator_id = ~0;
26057843bd03SEdward Tomasz Napierala strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
26067843bd03SEdward Tomasz Napierala strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
26077843bd03SEdward Tomasz Napierala strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2608009ea47eSEdward Tomasz Napierala cpi->unit_number = cam_sim_unit(sim);
2609009ea47eSEdward Tomasz Napierala cpi->bus_id = cam_sim_bus(sim);
2610009ea47eSEdward Tomasz Napierala cpi->base_transfer_speed = 150000; /* XXX */
2611009ea47eSEdward Tomasz Napierala cpi->transport = XPORT_ISCSI;
2612009ea47eSEdward Tomasz Napierala cpi->transport_version = 0;
2613009ea47eSEdward Tomasz Napierala cpi->protocol = PROTO_SCSI;
2614009ea47eSEdward Tomasz Napierala cpi->protocol_version = SCSI_REV_SPC3;
2615cd853791SKonstantin Belousov cpi->maxio = maxphys;
2616009ea47eSEdward Tomasz Napierala cpi->ccb_h.status = CAM_REQ_CMP;
2617009ea47eSEdward Tomasz Napierala break;
2618009ea47eSEdward Tomasz Napierala }
2619462cf3baSAlexander Motin case XPT_GET_TRAN_SETTINGS:
2620462cf3baSAlexander Motin {
2621462cf3baSAlexander Motin struct ccb_trans_settings *cts;
2622462cf3baSAlexander Motin struct ccb_trans_settings_scsi *scsi;
2623462cf3baSAlexander Motin
2624462cf3baSAlexander Motin cts = &ccb->cts;
2625462cf3baSAlexander Motin scsi = &cts->proto_specific.scsi;
2626462cf3baSAlexander Motin
2627462cf3baSAlexander Motin cts->protocol = PROTO_SCSI;
2628462cf3baSAlexander Motin cts->protocol_version = SCSI_REV_SPC3;
2629462cf3baSAlexander Motin cts->transport = XPORT_ISCSI;
2630462cf3baSAlexander Motin cts->transport_version = 0;
2631462cf3baSAlexander Motin scsi->valid = CTS_SCSI_VALID_TQ;
2632462cf3baSAlexander Motin scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2633462cf3baSAlexander Motin cts->ccb_h.status = CAM_REQ_CMP;
2634462cf3baSAlexander Motin break;
2635462cf3baSAlexander Motin }
2636009ea47eSEdward Tomasz Napierala case XPT_CALC_GEOMETRY:
2637009ea47eSEdward Tomasz Napierala cam_calc_geometry(&ccb->ccg, /*extended*/1);
2638009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_REQ_CMP;
2639009ea47eSEdward Tomasz Napierala break;
2640009ea47eSEdward Tomasz Napierala #if 0
2641009ea47eSEdward Tomasz Napierala /*
2642009ea47eSEdward Tomasz Napierala * XXX: What's the point?
2643009ea47eSEdward Tomasz Napierala */
2644009ea47eSEdward Tomasz Napierala case XPT_RESET_BUS:
2645009ea47eSEdward Tomasz Napierala case XPT_TERM_IO:
2646009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "faking success for reset, abort, or term_io");
2647009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_REQ_CMP;
2648009ea47eSEdward Tomasz Napierala break;
2649009ea47eSEdward Tomasz Napierala #endif
2650aa75114cSAlexander Motin case XPT_ABORT:
2651aa75114cSAlexander Motin iscsi_action_abort(is, ccb);
2652aa75114cSAlexander Motin return;
2653009ea47eSEdward Tomasz Napierala case XPT_SCSI_IO:
2654009ea47eSEdward Tomasz Napierala iscsi_action_scsiio(is, ccb);
2655009ea47eSEdward Tomasz Napierala return;
2656009ea47eSEdward Tomasz Napierala default:
2657009ea47eSEdward Tomasz Napierala #if 0
2658009ea47eSEdward Tomasz Napierala ISCSI_SESSION_DEBUG(is, "got unsupported code 0x%x", ccb->ccb_h.func_code);
2659009ea47eSEdward Tomasz Napierala #endif
2660009ea47eSEdward Tomasz Napierala ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2661009ea47eSEdward Tomasz Napierala break;
2662009ea47eSEdward Tomasz Napierala }
2663009ea47eSEdward Tomasz Napierala xpt_done(ccb);
2664009ea47eSEdward Tomasz Napierala }
2665009ea47eSEdward Tomasz Napierala
2666009ea47eSEdward Tomasz Napierala static void
iscsi_terminate_sessions(struct iscsi_softc * sc)2667481b36c6SSteven Hartland iscsi_terminate_sessions(struct iscsi_softc *sc)
2668009ea47eSEdward Tomasz Napierala {
2669009ea47eSEdward Tomasz Napierala struct iscsi_session *is;
2670009ea47eSEdward Tomasz Napierala
2671481b36c6SSteven Hartland sx_slock(&sc->sc_lock);
2672481b36c6SSteven Hartland TAILQ_FOREACH(is, &sc->sc_sessions, is_next)
2673481b36c6SSteven Hartland iscsi_session_terminate(is);
2674481b36c6SSteven Hartland while(!TAILQ_EMPTY(&sc->sc_sessions)) {
2675481b36c6SSteven Hartland ISCSI_DEBUG("waiting for sessions to terminate");
2676481b36c6SSteven Hartland cv_wait(&sc->sc_cv, &sc->sc_lock);
2677481b36c6SSteven Hartland }
2678481b36c6SSteven Hartland ISCSI_DEBUG("all sessions terminated");
2679481b36c6SSteven Hartland sx_sunlock(&sc->sc_lock);
2680481b36c6SSteven Hartland }
2681481b36c6SSteven Hartland
2682481b36c6SSteven Hartland static void
iscsi_shutdown_pre(struct iscsi_softc * sc,int howto)26832ce1c45bSMitchell Horne iscsi_shutdown_pre(struct iscsi_softc *sc, int howto)
2684481b36c6SSteven Hartland {
2685481b36c6SSteven Hartland struct iscsi_session *is;
2686481b36c6SSteven Hartland
26872ce1c45bSMitchell Horne if (!fail_on_shutdown || (howto & RB_NOSYNC) != 0 ||
26882ce1c45bSMitchell Horne SCHEDULER_STOPPED())
2689481b36c6SSteven Hartland return;
2690009ea47eSEdward Tomasz Napierala
2691e553ca49SEdward Tomasz Napierala /*
2692e553ca49SEdward Tomasz Napierala * If we have any sessions waiting for reconnection, request
2693e553ca49SEdward Tomasz Napierala * maintenance thread to fail them immediately instead of waiting
2694e553ca49SEdward Tomasz Napierala * for reconnect timeout.
2695481b36c6SSteven Hartland *
2696481b36c6SSteven Hartland * This prevents LUNs with mounted filesystems that are supported
2697481b36c6SSteven Hartland * by disconnected iSCSI sessions from hanging, however it will
2698481b36c6SSteven Hartland * fail all queued BIOs.
2699e553ca49SEdward Tomasz Napierala */
2700481b36c6SSteven Hartland ISCSI_DEBUG("forcing failing all disconnected sessions due to shutdown");
2701481b36c6SSteven Hartland
2702481b36c6SSteven Hartland fail_on_disconnection = 1;
2703481b36c6SSteven Hartland
2704009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
2705e553ca49SEdward Tomasz Napierala TAILQ_FOREACH(is, &sc->sc_sessions, is_next) {
2706e553ca49SEdward Tomasz Napierala ISCSI_SESSION_LOCK(is);
2707481b36c6SSteven Hartland if (!is->is_connected) {
2708481b36c6SSteven Hartland ISCSI_SESSION_DEBUG(is, "force failing disconnected session early");
2709e553ca49SEdward Tomasz Napierala iscsi_session_reconnect(is);
2710481b36c6SSteven Hartland }
2711e553ca49SEdward Tomasz Napierala ISCSI_SESSION_UNLOCK(is);
2712e553ca49SEdward Tomasz Napierala }
2713009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
2714009ea47eSEdward Tomasz Napierala }
2715009ea47eSEdward Tomasz Napierala
2716481b36c6SSteven Hartland static void
iscsi_shutdown_post_sync(struct iscsi_softc * sc,int howto)27174eb861d3SMitchell Horne iscsi_shutdown_post_sync(struct iscsi_softc *sc, int howto)
2718481b36c6SSteven Hartland {
2719481b36c6SSteven Hartland
27204eb861d3SMitchell Horne if ((howto & RB_NOSYNC) == 0) {
2721481b36c6SSteven Hartland ISCSI_DEBUG("removing all sessions due to shutdown");
2722481b36c6SSteven Hartland iscsi_terminate_sessions(sc);
2723481b36c6SSteven Hartland }
2724e54fb4ffSAndriy Gapon }
2725481b36c6SSteven Hartland
2726009ea47eSEdward Tomasz Napierala static int
iscsi_load(void)2727009ea47eSEdward Tomasz Napierala iscsi_load(void)
2728009ea47eSEdward Tomasz Napierala {
2729009ea47eSEdward Tomasz Napierala int error;
2730009ea47eSEdward Tomasz Napierala
2731009ea47eSEdward Tomasz Napierala sc = malloc(sizeof(*sc), M_ISCSI, M_ZERO | M_WAITOK);
2732009ea47eSEdward Tomasz Napierala sx_init(&sc->sc_lock, "iscsi");
2733009ea47eSEdward Tomasz Napierala TAILQ_INIT(&sc->sc_sessions);
2734009ea47eSEdward Tomasz Napierala cv_init(&sc->sc_cv, "iscsi_cv");
2735009ea47eSEdward Tomasz Napierala
2736009ea47eSEdward Tomasz Napierala iscsi_outstanding_zone = uma_zcreate("iscsi_outstanding",
2737009ea47eSEdward Tomasz Napierala sizeof(struct iscsi_outstanding), NULL, NULL, NULL, NULL,
2738c28c09c1SEdward Tomasz Napierala UMA_ALIGN_PTR, 0);
2739009ea47eSEdward Tomasz Napierala
2740009ea47eSEdward Tomasz Napierala error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &iscsi_cdevsw,
2741009ea47eSEdward Tomasz Napierala NULL, UID_ROOT, GID_WHEEL, 0600, "iscsi");
2742009ea47eSEdward Tomasz Napierala if (error != 0) {
2743009ea47eSEdward Tomasz Napierala ISCSI_WARN("failed to create device node, error %d", error);
2744009ea47eSEdward Tomasz Napierala return (error);
2745009ea47eSEdward Tomasz Napierala }
2746009ea47eSEdward Tomasz Napierala sc->sc_cdev->si_drv1 = sc;
2747009ea47eSEdward Tomasz Napierala
2748481b36c6SSteven Hartland sc->sc_shutdown_pre_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2749481b36c6SSteven Hartland iscsi_shutdown_pre, sc, SHUTDOWN_PRI_FIRST);
2750481b36c6SSteven Hartland /*
2751481b36c6SSteven Hartland * shutdown_post_sync needs to run after filesystem shutdown and before
2752481b36c6SSteven Hartland * CAM shutdown - otherwise when rebooting with an iSCSI session that is
2753481b36c6SSteven Hartland * disconnected but has outstanding requests, dashutdown() will hang on
2754481b36c6SSteven Hartland * cam_periph_runccb().
2755481b36c6SSteven Hartland */
2756481b36c6SSteven Hartland sc->sc_shutdown_post_eh = EVENTHANDLER_REGISTER(shutdown_post_sync,
27574eb861d3SMitchell Horne iscsi_shutdown_post_sync, sc, SHUTDOWN_PRI_DEFAULT - 1);
2758009ea47eSEdward Tomasz Napierala
2759009ea47eSEdward Tomasz Napierala return (0);
2760009ea47eSEdward Tomasz Napierala }
2761009ea47eSEdward Tomasz Napierala
2762009ea47eSEdward Tomasz Napierala static int
iscsi_unload(void)2763009ea47eSEdward Tomasz Napierala iscsi_unload(void)
2764009ea47eSEdward Tomasz Napierala {
2765009ea47eSEdward Tomasz Napierala
276689df4847SJohn Baldwin /* Awaken any threads asleep in iscsi_ioctl(). */
276789df4847SJohn Baldwin sx_xlock(&sc->sc_lock);
276889df4847SJohn Baldwin sc->sc_unloading = true;
276989df4847SJohn Baldwin cv_signal(&sc->sc_cv);
277089df4847SJohn Baldwin sx_xunlock(&sc->sc_lock);
277189df4847SJohn Baldwin
2772a667cf73SEdward Tomasz Napierala if (sc->sc_cdev != NULL) {
2773009ea47eSEdward Tomasz Napierala ISCSI_DEBUG("removing device node");
2774009ea47eSEdward Tomasz Napierala destroy_dev(sc->sc_cdev);
2775009ea47eSEdward Tomasz Napierala ISCSI_DEBUG("device node removed");
2776a667cf73SEdward Tomasz Napierala }
2777009ea47eSEdward Tomasz Napierala
2778481b36c6SSteven Hartland if (sc->sc_shutdown_pre_eh != NULL)
2779481b36c6SSteven Hartland EVENTHANDLER_DEREGISTER(shutdown_pre_sync, sc->sc_shutdown_pre_eh);
2780481b36c6SSteven Hartland if (sc->sc_shutdown_post_eh != NULL)
2781481b36c6SSteven Hartland EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->sc_shutdown_post_eh);
2782009ea47eSEdward Tomasz Napierala
2783481b36c6SSteven Hartland iscsi_terminate_sessions(sc);
2784009ea47eSEdward Tomasz Napierala
2785009ea47eSEdward Tomasz Napierala uma_zdestroy(iscsi_outstanding_zone);
2786009ea47eSEdward Tomasz Napierala sx_destroy(&sc->sc_lock);
2787009ea47eSEdward Tomasz Napierala cv_destroy(&sc->sc_cv);
2788009ea47eSEdward Tomasz Napierala free(sc, M_ISCSI);
2789009ea47eSEdward Tomasz Napierala return (0);
2790009ea47eSEdward Tomasz Napierala }
2791009ea47eSEdward Tomasz Napierala
2792009ea47eSEdward Tomasz Napierala static int
iscsi_quiesce(void)2793009ea47eSEdward Tomasz Napierala iscsi_quiesce(void)
2794009ea47eSEdward Tomasz Napierala {
2795009ea47eSEdward Tomasz Napierala sx_slock(&sc->sc_lock);
2796009ea47eSEdward Tomasz Napierala if (!TAILQ_EMPTY(&sc->sc_sessions)) {
2797009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
2798009ea47eSEdward Tomasz Napierala return (EBUSY);
2799009ea47eSEdward Tomasz Napierala }
2800009ea47eSEdward Tomasz Napierala sx_sunlock(&sc->sc_lock);
2801009ea47eSEdward Tomasz Napierala return (0);
2802009ea47eSEdward Tomasz Napierala }
2803009ea47eSEdward Tomasz Napierala
2804009ea47eSEdward Tomasz Napierala static int
iscsi_modevent(module_t mod,int what,void * arg)2805009ea47eSEdward Tomasz Napierala iscsi_modevent(module_t mod, int what, void *arg)
2806009ea47eSEdward Tomasz Napierala {
2807009ea47eSEdward Tomasz Napierala int error;
2808009ea47eSEdward Tomasz Napierala
2809009ea47eSEdward Tomasz Napierala switch (what) {
2810009ea47eSEdward Tomasz Napierala case MOD_LOAD:
2811009ea47eSEdward Tomasz Napierala error = iscsi_load();
2812009ea47eSEdward Tomasz Napierala break;
2813009ea47eSEdward Tomasz Napierala case MOD_UNLOAD:
2814009ea47eSEdward Tomasz Napierala error = iscsi_unload();
2815009ea47eSEdward Tomasz Napierala break;
2816009ea47eSEdward Tomasz Napierala case MOD_QUIESCE:
2817009ea47eSEdward Tomasz Napierala error = iscsi_quiesce();
2818009ea47eSEdward Tomasz Napierala break;
2819009ea47eSEdward Tomasz Napierala default:
2820009ea47eSEdward Tomasz Napierala error = EINVAL;
2821009ea47eSEdward Tomasz Napierala break;
2822009ea47eSEdward Tomasz Napierala }
2823009ea47eSEdward Tomasz Napierala return (error);
2824009ea47eSEdward Tomasz Napierala }
2825009ea47eSEdward Tomasz Napierala
2826009ea47eSEdward Tomasz Napierala moduledata_t iscsi_data = {
2827009ea47eSEdward Tomasz Napierala "iscsi",
2828009ea47eSEdward Tomasz Napierala iscsi_modevent,
2829009ea47eSEdward Tomasz Napierala 0
2830009ea47eSEdward Tomasz Napierala };
2831009ea47eSEdward Tomasz Napierala
2832009ea47eSEdward Tomasz Napierala DECLARE_MODULE(iscsi, iscsi_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
2833009ea47eSEdward Tomasz Napierala MODULE_DEPEND(iscsi, cam, 1, 1, 1);
2834009ea47eSEdward Tomasz Napierala MODULE_DEPEND(iscsi, icl, 1, 1, 1);
2835