2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * CTL frontend for the iSCSI protocol.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/condvar.h>
43 #include <sys/endian.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/queue.h>
53 #include <sys/socket.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
57 #include <sys/unistd.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_da.h>
64 #include <cam/ctl/ctl_io.h>
65 #include <cam/ctl/ctl.h>
66 #include <cam/ctl/ctl_backend.h>
67 #include <cam/ctl/ctl_error.h>
68 #include <cam/ctl/ctl_frontend.h>
69 #include <cam/ctl/ctl_debug.h>
70 #include <cam/ctl/ctl_ha.h>
71 #include <cam/ctl/ctl_ioctl.h>
72 #include <cam/ctl/ctl_private.h>
74 #include <dev/iscsi/icl.h>
75 #include <dev/iscsi/icl_wrappers.h>
76 #include <dev/iscsi/iscsi_proto.h>
77 #include <cam/ctl/ctl_frontend_iscsi.h>
79 #ifdef ICL_KERNEL_PROXY
80 #include <sys/socketvar.h>
83 #ifdef ICL_KERNEL_PROXY
84 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
87 /* Used for internal nexus reset task. */
88 #define ISCSI_BHS_OPCODE_INTERNAL 0x3e
90 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
91 static uma_zone_t cfiscsi_data_wait_zone;
93 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
94 "CAM Target Layer iSCSI Frontend");
96 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
97 &debug, 1, "Enable debug messages");
98 static int ping_timeout = 5;
99 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
100 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
101 static int login_timeout = 60;
102 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
103 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
104 static int maxtags = 256;
105 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN,
106 &maxtags, 0, "Max number of requests queued by initiator");
108 #define CFISCSI_DEBUG(X, ...) \
111 printf("%s: " X "\n", \
112 __func__, ## __VA_ARGS__); \
116 #define CFISCSI_WARN(X, ...) \
119 printf("WARNING: %s: " X "\n", \
120 __func__, ## __VA_ARGS__); \
124 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
127 printf("%s: %s (%s): " X "\n", \
128 __func__, S->cs_initiator_addr, \
129 S->cs_initiator_name, ## __VA_ARGS__); \
133 #define CFISCSI_SESSION_WARN(S, X, ...) \
136 printf("WARNING: %s (%s): " X "\n", \
137 S->cs_initiator_addr, \
138 S->cs_initiator_name, ## __VA_ARGS__); \
142 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
143 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
144 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
146 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
147 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
149 struct cfiscsi_priv {
155 ((struct cfiscsi_priv *)&(io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND])
156 #define PRIV_REQUEST(io) PRIV(io)->request
157 #define PRIV_EXPDATASN(io) PRIV(io)->expdatasn
158 #define PRIV_R2TSN(io) PRIV(io)->r2tsn
160 static int cfiscsi_init(void);
161 static int cfiscsi_shutdown(void);
162 static void cfiscsi_online(void *arg);
163 static void cfiscsi_offline(void *arg);
164 static int cfiscsi_info(void *arg, struct sbuf *sb);
165 static int cfiscsi_ioctl(struct cdev *dev,
166 u_long cmd, caddr_t addr, int flag, struct thread *td);
167 static void cfiscsi_datamove(union ctl_io *io);
168 static void cfiscsi_datamove_in(union ctl_io *io);
169 static void cfiscsi_datamove_out(union ctl_io *io);
170 static void cfiscsi_done(union ctl_io *io);
171 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
172 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
173 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
174 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
175 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
176 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
177 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
178 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
179 struct cfiscsi_session *cs, union ctl_io *io,
180 uint32_t initiator_task_tag,
181 uint32_t *target_transfer_tagp);
182 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
183 struct cfiscsi_data_wait *cdw);
184 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
185 *softc, const char *name, uint16_t tag);
186 static struct cfiscsi_target *cfiscsi_target_find_or_create(
187 struct cfiscsi_softc *softc, const char *name, const char *alias,
189 static void cfiscsi_target_release(struct cfiscsi_target *ct);
190 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
192 static struct cfiscsi_softc cfiscsi_softc;
194 static struct ctl_frontend cfiscsi_frontend =
197 .init = cfiscsi_init,
198 .ioctl = cfiscsi_ioctl,
199 .shutdown = cfiscsi_shutdown,
201 CTL_FRONTEND_DECLARE(cfiscsi, cfiscsi_frontend);
202 MODULE_DEPEND(cfiscsi, icl, 1, 1, 1);
204 static struct icl_pdu *
205 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
208 return (icl_pdu_new(request->ip_conn, flags));
212 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
214 const struct iscsi_bhs_scsi_command *bhssc;
215 struct cfiscsi_session *cs;
216 uint32_t cmdsn, curcmdsn;
218 cs = PDU_SESSION(request);
221 * Every incoming PDU - not just NOP-Out - resets the ping timer.
222 * The purpose of the timeout is to reset the connection when it stalls;
223 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
229 * Immediate commands carry cmdsn, but it is neither incremented nor
232 if (request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE)
236 * Data-Out PDUs don't contain CmdSN.
238 if (request->ip_bhs->bhs_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
242 * We're only using fields common for all the request
243 * (initiator -> target) PDUs.
245 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
246 curcmdsn = cmdsn = ntohl(bhssc->bhssc_cmdsn);
249 * Increment session cmdsn and exit if we received the expected value.
252 if (atomic_fcmpset_32(&cs->cs_cmdsn, &curcmdsn, cmdsn + 1))
254 } while (curcmdsn == cmdsn);
257 * The target MUST silently ignore any non-immediate command outside
260 if (ISCSI_SNLT(cmdsn, curcmdsn) ||
261 ISCSI_SNGT(cmdsn, curcmdsn - 1 + maxtags)) {
262 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
263 "while expected %u", cmdsn, curcmdsn);
268 * We don't support multiple connections now, so any discontinuity in
269 * CmdSN means lost PDUs. Since we don't support PDU retransmission --
270 * terminate the connection.
272 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
273 "while expected %u; dropping connection",
275 cfiscsi_session_terminate(cs);
280 cfiscsi_pdu_handle(struct icl_pdu *request)
282 struct cfiscsi_session *cs;
285 cs = PDU_SESSION(request);
287 ignore = cfiscsi_pdu_update_cmdsn(request);
289 icl_pdu_free(request);
294 * Handle the PDU; this includes e.g. receiving the remaining
295 * part of PDU and submitting the SCSI command to CTL
296 * or queueing a reply. The handling routine is responsible
297 * for freeing the PDU when it's no longer needed.
299 switch (request->ip_bhs->bhs_opcode &
300 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
301 case ISCSI_BHS_OPCODE_NOP_OUT:
302 cfiscsi_pdu_handle_nop_out(request);
304 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
305 cfiscsi_pdu_handle_scsi_command(request);
307 case ISCSI_BHS_OPCODE_TASK_REQUEST:
308 cfiscsi_pdu_handle_task_request(request);
310 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
311 cfiscsi_pdu_handle_data_out(request);
313 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
314 cfiscsi_pdu_handle_logout_request(request);
317 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
318 "opcode 0x%x; dropping connection",
319 request->ip_bhs->bhs_opcode);
320 icl_pdu_free(request);
321 cfiscsi_session_terminate(cs);
327 cfiscsi_receive_callback(struct icl_pdu *request)
329 #ifdef ICL_KERNEL_PROXY
330 struct cfiscsi_session *cs;
332 cs = PDU_SESSION(request);
333 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
334 if (cs->cs_login_pdu == NULL)
335 cs->cs_login_pdu = request;
337 icl_pdu_free(request);
338 cv_signal(&cs->cs_login_cv);
343 cfiscsi_pdu_handle(request);
347 cfiscsi_error_callback(struct icl_conn *ic)
349 struct cfiscsi_session *cs;
351 cs = CONN_SESSION(ic);
353 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
354 cfiscsi_session_terminate(cs);
358 cfiscsi_pdu_prepare(struct icl_pdu *response)
360 struct cfiscsi_session *cs;
361 struct iscsi_bhs_scsi_response *bhssr;
362 bool advance_statsn = true;
365 cs = PDU_SESSION(response);
367 CFISCSI_SESSION_LOCK_ASSERT(cs);
370 * We're only using fields common for all the response
371 * (target -> initiator) PDUs.
373 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
376 * 10.8.3: "The StatSN for this connection is not advanced
377 * after this PDU is sent."
379 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
380 advance_statsn = false;
383 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
384 * StatSN for the connection is not advanced after this PDU is sent."
386 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
387 bhssr->bhssr_initiator_task_tag == 0xffffffff)
388 advance_statsn = false;
391 * See the comment below - StatSN is not meaningful and must
394 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
395 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
396 advance_statsn = false;
399 * 10.7.3: "The fields StatSN, Status, and Residual Count
400 * only have meaningful content if the S bit is set to 1."
402 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
403 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
404 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
405 cmdsn = cs->cs_cmdsn;
406 bhssr->bhssr_expcmdsn = htonl(cmdsn);
407 bhssr->bhssr_maxcmdsn = htonl(cmdsn - 1 +
408 imax(0, maxtags - cs->cs_outstanding_ctl_pdus));
417 cfiscsi_pdu_queue(struct icl_pdu *response)
419 struct cfiscsi_session *cs;
421 cs = PDU_SESSION(response);
423 CFISCSI_SESSION_LOCK(cs);
424 cfiscsi_pdu_prepare(response);
425 icl_pdu_queue(response);
426 CFISCSI_SESSION_UNLOCK(cs);
430 cfiscsi_pdu_queue_cb(struct icl_pdu *response, icl_pdu_cb cb)
432 struct cfiscsi_session *cs = PDU_SESSION(response);
434 CFISCSI_SESSION_LOCK(cs);
435 cfiscsi_pdu_prepare(response);
436 icl_pdu_queue_cb(response, cb);
437 CFISCSI_SESSION_UNLOCK(cs);
441 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
443 struct cfiscsi_session *cs;
444 struct iscsi_bhs_nop_out *bhsno;
445 struct iscsi_bhs_nop_in *bhsni;
446 struct icl_pdu *response;
451 cs = PDU_SESSION(request);
452 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
454 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
456 * Nothing to do, iscsi_pdu_update_statsn() already
457 * zeroed the timeout.
459 icl_pdu_free(request);
463 datasize = icl_pdu_data_segment_length(request);
465 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
467 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
468 "dropping connection");
469 icl_pdu_free(request);
470 cfiscsi_session_terminate(cs);
473 icl_pdu_get_data(request, 0, data, datasize);
476 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
477 if (response == NULL) {
478 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
479 "droppping connection");
480 free(data, M_CFISCSI);
481 icl_pdu_free(request);
482 cfiscsi_session_terminate(cs);
485 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
486 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
487 bhsni->bhsni_flags = 0x80;
488 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
489 bhsni->bhsni_target_transfer_tag = 0xffffffff;
491 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
493 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
494 "dropping connection");
495 free(data, M_CFISCSI);
496 icl_pdu_free(request);
497 icl_pdu_free(response);
498 cfiscsi_session_terminate(cs);
501 free(data, M_CFISCSI);
504 icl_pdu_free(request);
505 cfiscsi_pdu_queue(response);
509 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
511 struct iscsi_bhs_scsi_command *bhssc;
512 struct cfiscsi_session *cs;
516 cs = PDU_SESSION(request);
517 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
518 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
519 // bhssc->bhssc_initiator_task_tag);
521 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
522 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
523 "ImmediateData=No; dropping connection");
524 icl_pdu_free(request);
525 cfiscsi_session_terminate(cs);
528 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
530 PRIV_REQUEST(io) = request;
531 io->io_hdr.io_type = CTL_IO_SCSI;
532 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
533 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
534 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhssc->bhssc_lun));
535 io->scsiio.priority = (bhssc->bhssc_pri & BHSSC_PRI_MASK) >>
537 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
538 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
539 case BHSSC_FLAGS_ATTR_UNTAGGED:
540 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
542 case BHSSC_FLAGS_ATTR_SIMPLE:
543 io->scsiio.tag_type = CTL_TAG_SIMPLE;
545 case BHSSC_FLAGS_ATTR_ORDERED:
546 io->scsiio.tag_type = CTL_TAG_ORDERED;
548 case BHSSC_FLAGS_ATTR_HOQ:
549 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
551 case BHSSC_FLAGS_ATTR_ACA:
552 io->scsiio.tag_type = CTL_TAG_ACA;
555 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
556 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
557 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
560 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
561 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
562 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
564 if (error != CTL_RETVAL_COMPLETE) {
565 CFISCSI_SESSION_WARN(cs, "ctl_run() failed; error %d; "
566 "dropping connection", error);
568 refcount_release(&cs->cs_outstanding_ctl_pdus);
569 icl_pdu_free(request);
570 cfiscsi_session_terminate(cs);
575 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
577 struct iscsi_bhs_task_management_request *bhstmr;
578 struct iscsi_bhs_task_management_response *bhstmr2;
579 struct icl_pdu *response;
580 struct cfiscsi_session *cs;
584 cs = PDU_SESSION(request);
585 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
586 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
588 PRIV_REQUEST(io) = request;
589 io->io_hdr.io_type = CTL_IO_TASK;
590 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
591 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
592 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhstmr->bhstmr_lun));
593 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
595 switch (bhstmr->bhstmr_function & ~0x80) {
596 case BHSTMR_FUNCTION_ABORT_TASK:
598 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
600 io->taskio.task_action = CTL_TASK_ABORT_TASK;
601 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
603 case BHSTMR_FUNCTION_ABORT_TASK_SET:
605 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
607 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
609 case BHSTMR_FUNCTION_CLEAR_TASK_SET:
611 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_CLEAR_TASK_SET");
613 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
615 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
617 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
619 io->taskio.task_action = CTL_TASK_LUN_RESET;
621 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
623 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
625 io->taskio.task_action = CTL_TASK_TARGET_RESET;
627 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
629 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
631 io->taskio.task_action = CTL_TASK_TARGET_RESET;
633 case BHSTMR_FUNCTION_QUERY_TASK:
635 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK");
637 io->taskio.task_action = CTL_TASK_QUERY_TASK;
638 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
640 case BHSTMR_FUNCTION_QUERY_TASK_SET:
642 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK_SET");
644 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
646 case BHSTMR_FUNCTION_I_T_NEXUS_RESET:
648 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_I_T_NEXUS_RESET");
650 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
652 case BHSTMR_FUNCTION_QUERY_ASYNC_EVENT:
654 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_ASYNC_EVENT");
656 io->taskio.task_action = CTL_TASK_QUERY_ASYNC_EVENT;
659 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
660 bhstmr->bhstmr_function & ~0x80);
663 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
664 if (response == NULL) {
665 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
666 "dropping connection");
667 icl_pdu_free(request);
668 cfiscsi_session_terminate(cs);
671 bhstmr2 = (struct iscsi_bhs_task_management_response *)
673 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
674 bhstmr2->bhstmr_flags = 0x80;
675 bhstmr2->bhstmr_response =
676 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
677 bhstmr2->bhstmr_initiator_task_tag =
678 bhstmr->bhstmr_initiator_task_tag;
679 icl_pdu_free(request);
680 cfiscsi_pdu_queue(response);
684 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
686 if (error != CTL_RETVAL_COMPLETE) {
687 CFISCSI_SESSION_WARN(cs, "ctl_run() failed; error %d; "
688 "dropping connection", error);
690 refcount_release(&cs->cs_outstanding_ctl_pdus);
691 icl_pdu_free(request);
692 cfiscsi_session_terminate(cs);
697 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
699 struct iscsi_bhs_data_out *bhsdo;
700 struct cfiscsi_session *cs;
701 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
702 size_t copy_len, len, off, buffer_offset;
706 cs = PDU_SESSION(request);
708 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
709 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
710 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
711 ISCSI_BHS_OPCODE_SCSI_COMMAND,
712 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
715 * We're only using fields common for Data-Out and SCSI Command PDUs.
717 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
719 io = cdw->cdw_ctl_io;
720 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
721 ("CTL_FLAG_DATA_IN"));
724 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
725 request->ip_data_len, io->scsiio.kern_total_len);
728 if (io->scsiio.kern_sg_entries > 0) {
729 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
730 ctl_sg_count = io->scsiio.kern_sg_entries;
732 ctl_sglist = &ctl_sg_entry;
733 ctl_sglist->addr = io->scsiio.kern_data_ptr;
734 ctl_sglist->len = io->scsiio.kern_data_len;
738 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
739 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
740 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
743 len = icl_pdu_data_segment_length(request);
746 * Make sure the offset, as sent by the initiator, matches the offset
747 * we're supposed to be at in the scatter-gather list.
750 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
751 buffer_offset + len <=
752 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
753 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
754 "expected %zd; dropping connection", buffer_offset,
755 (size_t)io->scsiio.kern_rel_offset +
756 (size_t)io->scsiio.ext_data_filled);
757 ctl_set_data_phase_error(&io->scsiio);
758 cfiscsi_session_terminate(cs);
763 * This is the offset within the PDU data segment, as opposed
764 * to buffer_offset, which is the offset within the task (SCSI
767 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
771 * Iterate over the scatter/gather segments, filling them with data
772 * from the PDU data segment. Note that this can get called multiple
773 * times for one SCSI command; the cdw structure holds state for the
774 * scatter/gather list.
777 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
778 ("cdw->cdw_sg_index >= ctl_sg_count"));
779 if (cdw->cdw_sg_len == 0) {
780 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
781 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
783 KASSERT(off <= len, ("len > off"));
784 copy_len = len - off;
785 if (copy_len > cdw->cdw_sg_len)
786 copy_len = cdw->cdw_sg_len;
788 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
789 cdw->cdw_sg_addr += copy_len;
790 cdw->cdw_sg_len -= copy_len;
792 io->scsiio.ext_data_filled += copy_len;
793 io->scsiio.kern_data_resid -= copy_len;
795 if (cdw->cdw_sg_len == 0) {
797 * End of current segment.
799 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
801 * Last segment in scatter/gather list.
810 * End of PDU payload.
818 * In case of unsolicited data, it's possible that the buffer
819 * provided by CTL is smaller than negotiated FirstBurstLength.
820 * Just ignore the superfluous data; will ask for them with R2T
821 * on next call to cfiscsi_datamove().
823 * This obviously can only happen with SCSI Command PDU.
825 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
826 ISCSI_BHS_OPCODE_SCSI_COMMAND)
829 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
830 "expected %zd; dropping connection",
831 icl_pdu_data_segment_length(request), off);
832 ctl_set_data_phase_error(&io->scsiio);
833 cfiscsi_session_terminate(cs);
837 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
838 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
839 CFISCSI_SESSION_WARN(cs, "got the final packet without "
840 "the F flag; flags = 0x%x; dropping connection",
842 ctl_set_data_phase_error(&io->scsiio);
843 cfiscsi_session_terminate(cs);
847 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
848 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
849 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
850 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
851 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
852 "transmitted size was %zd bytes instead of %d; "
853 "dropping connection",
854 (size_t)io->scsiio.ext_data_filled,
856 ctl_set_data_phase_error(&io->scsiio);
857 cfiscsi_session_terminate(cs);
861 * For SCSI Command PDU, this just means we need to
862 * solicit more data by sending R2T.
868 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
870 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
871 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
881 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
883 struct iscsi_bhs_data_out *bhsdo;
884 struct cfiscsi_session *cs;
885 struct cfiscsi_data_wait *cdw = NULL;
889 cs = PDU_SESSION(request);
890 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
892 CFISCSI_SESSION_LOCK(cs);
893 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
895 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
896 "ttt 0x%x, itt 0x%x",
897 bhsdo->bhsdo_target_transfer_tag,
898 bhsdo->bhsdo_initiator_task_tag,
899 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
901 if (bhsdo->bhsdo_target_transfer_tag ==
902 cdw->cdw_target_transfer_tag)
905 CFISCSI_SESSION_UNLOCK(cs);
907 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
908 "0x%x, not found; dropping connection",
909 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
910 icl_pdu_free(request);
911 cfiscsi_session_terminate(cs);
915 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
916 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
917 "DataSN %u, while expected %u; dropping connection",
918 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
919 icl_pdu_free(request);
920 cfiscsi_session_terminate(cs);
923 cdw->cdw_datasn += request->ip_additional_pdus + 1;
925 io = cdw->cdw_ctl_io;
926 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
927 ("CTL_FLAG_DATA_IN"));
929 done = cfiscsi_handle_data_segment(request, cdw);
931 CFISCSI_SESSION_LOCK(cs);
932 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
933 CFISCSI_SESSION_UNLOCK(cs);
934 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
935 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
936 cfiscsi_data_wait_free(cs, cdw);
937 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
939 ctl_datamove_done(io, false);
941 cfiscsi_datamove_out(io);
944 icl_pdu_free(request);
948 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
950 struct iscsi_bhs_logout_request *bhslr;
951 struct iscsi_bhs_logout_response *bhslr2;
952 struct icl_pdu *response;
953 struct cfiscsi_session *cs;
955 cs = PDU_SESSION(request);
956 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
957 switch (bhslr->bhslr_reason & 0x7f) {
958 case BHSLR_REASON_CLOSE_SESSION:
959 case BHSLR_REASON_CLOSE_CONNECTION:
960 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
961 if (response == NULL) {
962 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
963 icl_pdu_free(request);
964 cfiscsi_session_terminate(cs);
967 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
968 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
969 bhslr2->bhslr_flags = 0x80;
970 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
971 bhslr2->bhslr_initiator_task_tag =
972 bhslr->bhslr_initiator_task_tag;
973 icl_pdu_free(request);
974 cfiscsi_pdu_queue(response);
975 cfiscsi_session_terminate(cs);
977 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
978 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
979 if (response == NULL) {
980 CFISCSI_SESSION_WARN(cs,
981 "failed to allocate memory; dropping connection");
982 icl_pdu_free(request);
983 cfiscsi_session_terminate(cs);
986 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
987 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
988 bhslr2->bhslr_flags = 0x80;
989 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
990 bhslr2->bhslr_initiator_task_tag =
991 bhslr->bhslr_initiator_task_tag;
992 icl_pdu_free(request);
993 cfiscsi_pdu_queue(response);
996 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
997 bhslr->bhslr_reason);
998 icl_pdu_free(request);
999 cfiscsi_session_terminate(cs);
1005 cfiscsi_callout(void *context)
1008 struct iscsi_bhs_nop_in *bhsni;
1009 struct cfiscsi_session *cs;
1013 if (cs->cs_terminating)
1016 callout_schedule(&cs->cs_callout, 1 * hz);
1018 atomic_add_int(&cs->cs_timeout, 1);
1020 #ifdef ICL_KERNEL_PROXY
1021 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1022 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1023 CFISCSI_SESSION_WARN(cs, "login timed out after "
1024 "%d seconds; dropping connection", cs->cs_timeout);
1025 cfiscsi_session_terminate(cs);
1031 if (ping_timeout <= 0) {
1033 * Pings are disabled. Don't send NOP-In in this case;
1034 * user might have disabled pings to work around problems
1035 * with certain initiators that can't properly handle
1036 * NOP-In, such as iPXE. Reset the timeout, to avoid
1037 * triggering reconnection, should the user decide to
1044 if (cs->cs_timeout >= ping_timeout) {
1045 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1046 "dropping connection", ping_timeout);
1047 cfiscsi_session_terminate(cs);
1052 * If the ping was reset less than one second ago - which means
1053 * that we've received some PDU during the last second - assume
1054 * the traffic flows correctly and don't bother sending a NOP-Out.
1056 * (It's 2 - one for one second, and one for incrementing is_timeout
1057 * earlier in this routine.)
1059 if (cs->cs_timeout < 2)
1062 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1064 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1067 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1068 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1069 bhsni->bhsni_flags = 0x80;
1070 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1072 cfiscsi_pdu_queue(cp);
1075 static struct cfiscsi_data_wait *
1076 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1077 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1079 struct cfiscsi_data_wait *cdw;
1082 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1084 CFISCSI_SESSION_WARN(cs,
1085 "failed to allocate %zd bytes", sizeof(*cdw));
1089 error = icl_conn_transfer_setup(cs->cs_conn, PRIV_REQUEST(io), io,
1090 target_transfer_tagp, &cdw->cdw_icl_prv);
1092 CFISCSI_SESSION_WARN(cs,
1093 "icl_conn_transfer_setup() failed with error %d", error);
1094 uma_zfree(cfiscsi_data_wait_zone, cdw);
1098 cdw->cdw_ctl_io = io;
1099 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1100 cdw->cdw_initiator_task_tag = initiator_task_tag;
1106 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1107 struct cfiscsi_data_wait *cdw)
1110 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1111 uma_zfree(cfiscsi_data_wait_zone, cdw);
1115 cfiscsi_data_wait_abort(struct cfiscsi_session *cs,
1116 struct cfiscsi_data_wait *cdw, int status)
1118 union ctl_io *cdw_io;
1121 * Set nonzero port status; this prevents backends from
1122 * assuming that the data transfer actually succeeded
1123 * and writing uninitialized data to disk.
1126 cdw_io = cdw->cdw_ctl_io;
1127 cdw_io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1128 cdw_io->scsiio.io_hdr.port_status = status;
1129 cfiscsi_data_wait_free(cs, cdw);
1130 ctl_datamove_done(cdw_io, false);
1134 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1136 struct cfiscsi_data_wait *cdw;
1139 int error, last, wait;
1141 if (cs->cs_target == NULL)
1142 return; /* No target yet, so nothing to do. */
1143 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1144 ip->ip_bhs->bhs_opcode = ISCSI_BHS_OPCODE_INTERNAL;
1145 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1147 PRIV_REQUEST(io) = ip;
1148 io->io_hdr.io_type = CTL_IO_TASK;
1149 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1150 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1151 io->io_hdr.nexus.targ_lun = 0;
1152 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1153 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1154 wait = cs->cs_outstanding_ctl_pdus;
1155 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1156 error = ctl_run(io);
1157 if (error != CTL_RETVAL_COMPLETE) {
1158 CFISCSI_SESSION_WARN(cs, "ctl_run() failed; error %d", error);
1159 refcount_release(&cs->cs_outstanding_ctl_pdus);
1164 CFISCSI_SESSION_LOCK(cs);
1165 cs->cs_terminating_tasks = true;
1166 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1167 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1168 CFISCSI_SESSION_UNLOCK(cs);
1169 cfiscsi_data_wait_abort(cs, cdw, 42);
1170 CFISCSI_SESSION_LOCK(cs);
1172 CFISCSI_SESSION_UNLOCK(cs);
1175 * Wait for CTL to terminate all the tasks.
1178 CFISCSI_SESSION_WARN(cs,
1179 "waiting for CTL to terminate %d tasks", wait);
1181 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1182 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1185 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1186 0, "cfiscsi_terminate", hz / 100);
1189 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1193 cfiscsi_maintenance_thread(void *arg)
1195 struct cfiscsi_session *cs;
1200 CFISCSI_SESSION_LOCK(cs);
1201 if (cs->cs_terminating == false || cs->cs_handoff_in_progress)
1202 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1203 CFISCSI_SESSION_UNLOCK(cs);
1205 if (cs->cs_terminating && cs->cs_handoff_in_progress == false) {
1207 * We used to wait up to 30 seconds to deliver queued
1208 * PDUs to the initiator. We also tried hard to deliver
1209 * SCSI Responses for the aborted PDUs. We don't do
1210 * that anymore. We might need to revisit that.
1212 callout_drain(&cs->cs_callout);
1213 icl_conn_close(cs->cs_conn);
1216 * At this point ICL receive thread is no longer
1217 * running; no new tasks can be queued.
1219 cfiscsi_session_terminate_tasks(cs);
1220 cfiscsi_session_delete(cs);
1224 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1229 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1232 cs->cs_terminating = true;
1233 cv_signal(&cs->cs_maintenance_cv);
1234 #ifdef ICL_KERNEL_PROXY
1235 cv_signal(&cs->cs_login_cv);
1240 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1242 struct cfiscsi_target *ct;
1246 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1249 name = strdup(cs->cs_initiator_id, M_CTL);
1250 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1252 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1254 cs->cs_ctl_initid = -1;
1257 cs->cs_ctl_initid = i;
1259 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1266 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1270 if (cs->cs_ctl_initid == -1)
1273 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1275 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1278 cs->cs_ctl_initid = -1;
1281 static struct cfiscsi_session *
1282 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1284 struct cfiscsi_session *cs;
1287 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1289 CFISCSI_WARN("malloc failed");
1292 cs->cs_ctl_initid = -1;
1294 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1295 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1296 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1297 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1298 #ifdef ICL_KERNEL_PROXY
1299 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1303 * The purpose of this is to avoid racing with session shutdown.
1304 * Otherwise we could have the maintenance thread call icl_conn_close()
1305 * before we call icl_conn_handoff().
1307 cs->cs_handoff_in_progress = true;
1309 cs->cs_conn = icl_new_conn(offload, false, "cfiscsi", &cs->cs_lock);
1310 if (cs->cs_conn == NULL) {
1311 free(cs, M_CFISCSI);
1314 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1315 cs->cs_conn->ic_error = cfiscsi_error_callback;
1316 cs->cs_conn->ic_prv0 = cs;
1318 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1320 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1321 free(cs, M_CFISCSI);
1325 mtx_lock(&softc->lock);
1326 cs->cs_id = ++softc->last_session_id;
1327 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1328 mtx_unlock(&softc->lock);
1331 * Start pinging the initiator.
1333 callout_init(&cs->cs_callout, 1);
1334 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1340 cfiscsi_session_delete(struct cfiscsi_session *cs)
1342 struct cfiscsi_softc *softc;
1344 softc = &cfiscsi_softc;
1346 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1347 ("destroying session with outstanding CTL pdus"));
1348 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1349 ("destroying session with non-empty queue"));
1351 mtx_lock(&softc->lock);
1352 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1353 mtx_unlock(&softc->lock);
1355 cfiscsi_session_unregister_initiator(cs);
1356 if (cs->cs_target != NULL)
1357 cfiscsi_target_release(cs->cs_target);
1358 icl_conn_close(cs->cs_conn);
1359 icl_conn_free(cs->cs_conn);
1360 free(cs, M_CFISCSI);
1361 cv_signal(&softc->sessions_cv);
1367 struct cfiscsi_softc *softc;
1369 softc = &cfiscsi_softc;
1370 bzero(softc, sizeof(*softc));
1371 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1373 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1374 #ifdef ICL_KERNEL_PROXY
1375 cv_init(&softc->accept_cv, "cfiscsi_accept");
1377 TAILQ_INIT(&softc->sessions);
1378 TAILQ_INIT(&softc->targets);
1380 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1381 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1388 cfiscsi_shutdown(void)
1390 struct cfiscsi_softc *softc = &cfiscsi_softc;
1392 if (!TAILQ_EMPTY(&softc->sessions) || !TAILQ_EMPTY(&softc->targets))
1395 uma_zdestroy(cfiscsi_data_wait_zone);
1396 #ifdef ICL_KERNEL_PROXY
1397 cv_destroy(&softc->accept_cv);
1399 cv_destroy(&softc->sessions_cv);
1400 mtx_destroy(&softc->lock);
1404 #ifdef ICL_KERNEL_PROXY
1406 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1408 struct cfiscsi_session *cs;
1410 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1412 CFISCSI_WARN("failed to create session");
1416 icl_conn_handoff_sock(cs->cs_conn, so);
1417 cs->cs_initiator_sa = sa;
1418 cs->cs_portal_id = portal_id;
1419 cs->cs_handoff_in_progress = false;
1420 cs->cs_waiting_for_ctld = true;
1421 cv_signal(&cfiscsi_softc.accept_cv);
1423 CFISCSI_SESSION_LOCK(cs);
1425 * Wake up the maintenance thread if we got scheduled for termination
1426 * somewhere between cfiscsi_session_new() and icl_conn_handoff_sock().
1428 if (cs->cs_terminating)
1429 cfiscsi_session_terminate(cs);
1430 CFISCSI_SESSION_UNLOCK(cs);
1435 cfiscsi_online(void *arg)
1437 struct cfiscsi_softc *softc;
1438 struct cfiscsi_target *ct;
1441 ct = (struct cfiscsi_target *)arg;
1442 softc = ct->ct_softc;
1444 mtx_lock(&softc->lock);
1445 if (ct->ct_online) {
1446 mtx_unlock(&softc->lock);
1450 online = softc->online++;
1451 mtx_unlock(&softc->lock);
1455 #ifdef ICL_KERNEL_PROXY
1456 if (softc->listener != NULL)
1457 icl_listen_free(softc->listener);
1458 softc->listener = icl_listen_new(cfiscsi_accept);
1463 cfiscsi_offline(void *arg)
1465 struct cfiscsi_softc *softc;
1466 struct cfiscsi_target *ct;
1467 struct cfiscsi_session *cs;
1470 ct = (struct cfiscsi_target *)arg;
1471 softc = ct->ct_softc;
1473 mtx_lock(&softc->lock);
1474 if (!ct->ct_online) {
1475 mtx_unlock(&softc->lock);
1479 online = --softc->online;
1482 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1483 if (cs->cs_target == ct)
1484 cfiscsi_session_terminate(cs);
1486 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1487 if (cs->cs_target == ct)
1491 error = cv_wait_sig(&softc->sessions_cv, &softc->lock);
1493 CFISCSI_SESSION_DEBUG(cs,
1494 "cv_wait failed with error %d\n", error);
1498 } while (cs != NULL && ct->ct_online == 0);
1499 mtx_unlock(&softc->lock);
1503 #ifdef ICL_KERNEL_PROXY
1504 icl_listen_free(softc->listener);
1505 softc->listener = NULL;
1510 cfiscsi_info(void *arg, struct sbuf *sb)
1512 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1515 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1521 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1523 struct cfiscsi_softc *softc;
1524 struct cfiscsi_session *cs, *cs2;
1525 struct cfiscsi_target *ct;
1526 struct ctl_iscsi_handoff_params *cihp;
1529 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1530 softc = &cfiscsi_softc;
1532 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1533 cihp->initiator_name, cihp->initiator_addr,
1536 ct = cfiscsi_target_find(softc, cihp->target_name,
1537 cihp->portal_group_tag);
1539 ci->status = CTL_ISCSI_ERROR;
1540 snprintf(ci->error_str, sizeof(ci->error_str),
1541 "%s: target not found", __func__);
1545 #ifdef ICL_KERNEL_PROXY
1546 if (cihp->socket > 0 && cihp->connection_id > 0) {
1547 snprintf(ci->error_str, sizeof(ci->error_str),
1548 "both socket and connection_id set");
1549 ci->status = CTL_ISCSI_ERROR;
1550 cfiscsi_target_release(ct);
1553 if (cihp->socket == 0) {
1554 mtx_lock(&cfiscsi_softc.lock);
1555 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1556 if (cs->cs_id == cihp->connection_id)
1560 mtx_unlock(&cfiscsi_softc.lock);
1561 snprintf(ci->error_str, sizeof(ci->error_str),
1562 "connection not found");
1563 ci->status = CTL_ISCSI_ERROR;
1564 cfiscsi_target_release(ct);
1567 mtx_unlock(&cfiscsi_softc.lock);
1570 cs = cfiscsi_session_new(softc, cihp->offload);
1572 ci->status = CTL_ISCSI_ERROR;
1573 snprintf(ci->error_str, sizeof(ci->error_str),
1574 "%s: cfiscsi_session_new failed", __func__);
1575 cfiscsi_target_release(ct);
1578 #ifdef ICL_KERNEL_PROXY
1583 * First PDU of Full Feature phase has the same CmdSN as the last
1584 * PDU from the Login Phase received from the initiator. Thus,
1587 cs->cs_cmdsn = cihp->cmdsn;
1588 cs->cs_statsn = cihp->statsn;
1589 cs->cs_conn->ic_max_recv_data_segment_length =
1590 cihp->max_recv_data_segment_length;
1591 cs->cs_conn->ic_max_send_data_segment_length =
1592 cihp->max_send_data_segment_length;
1593 cs->cs_max_burst_length = cihp->max_burst_length;
1594 cs->cs_first_burst_length = cihp->first_burst_length;
1595 cs->cs_immediate_data = !!cihp->immediate_data;
1596 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1597 cs->cs_conn->ic_header_crc32c = true;
1598 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1599 cs->cs_conn->ic_data_crc32c = true;
1601 strlcpy(cs->cs_initiator_name,
1602 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1603 strlcpy(cs->cs_initiator_addr,
1604 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1605 strlcpy(cs->cs_initiator_alias,
1606 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1607 memcpy(cs->cs_initiator_isid,
1608 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1609 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1610 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1611 cihp->initiator_isid[0], cihp->initiator_isid[1],
1612 cihp->initiator_isid[2], cihp->initiator_isid[3],
1613 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1615 mtx_lock(&softc->lock);
1616 if (ct->ct_online == 0) {
1617 mtx_unlock(&softc->lock);
1618 CFISCSI_SESSION_LOCK(cs);
1619 cs->cs_handoff_in_progress = false;
1620 cfiscsi_session_terminate(cs);
1621 CFISCSI_SESSION_UNLOCK(cs);
1622 cfiscsi_target_release(ct);
1623 ci->status = CTL_ISCSI_ERROR;
1624 snprintf(ci->error_str, sizeof(ci->error_str),
1625 "%s: port offline", __func__);
1629 mtx_unlock(&softc->lock);
1632 if (!cs->cs_terminating) {
1633 mtx_lock(&softc->lock);
1634 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1635 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1636 cs->cs_target == cs2->cs_target &&
1637 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1638 if (strcmp(cs->cs_initiator_addr,
1639 cs2->cs_initiator_addr) != 0) {
1640 CFISCSI_SESSION_WARN(cs2,
1641 "session reinstatement from "
1642 "different address %s",
1643 cs->cs_initiator_addr);
1645 CFISCSI_SESSION_DEBUG(cs2,
1646 "session reinstatement");
1648 cfiscsi_session_terminate(cs2);
1649 mtx_unlock(&softc->lock);
1650 pause("cfiscsi_reinstate", 1);
1654 mtx_unlock(&softc->lock);
1658 * Register initiator with CTL.
1660 cfiscsi_session_register_initiator(cs);
1662 #ifdef ICL_KERNEL_PROXY
1663 if (cihp->socket > 0) {
1665 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1667 CFISCSI_SESSION_LOCK(cs);
1668 cs->cs_handoff_in_progress = false;
1669 cfiscsi_session_terminate(cs);
1670 CFISCSI_SESSION_UNLOCK(cs);
1671 ci->status = CTL_ISCSI_ERROR;
1672 snprintf(ci->error_str, sizeof(ci->error_str),
1673 "%s: icl_conn_handoff failed with error %d",
1677 #ifdef ICL_KERNEL_PROXY
1681 #ifdef ICL_KERNEL_PROXY
1682 cs->cs_login_phase = false;
1685 * First PDU of the Full Feature phase has likely already arrived.
1686 * We have to pick it up and execute properly.
1688 if (cs->cs_login_pdu != NULL) {
1689 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1690 cfiscsi_pdu_handle(cs->cs_login_pdu);
1691 cs->cs_login_pdu = NULL;
1695 CFISCSI_SESSION_LOCK(cs);
1696 cs->cs_handoff_in_progress = false;
1699 * Wake up the maintenance thread if we got scheduled for termination.
1701 if (cs->cs_terminating)
1702 cfiscsi_session_terminate(cs);
1703 CFISCSI_SESSION_UNLOCK(cs);
1705 ci->status = CTL_ISCSI_OK;
1709 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1711 struct ctl_iscsi_list_params *cilp;
1712 struct cfiscsi_session *cs;
1713 struct cfiscsi_softc *softc;
1717 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1718 softc = &cfiscsi_softc;
1720 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1722 ci->status = CTL_ISCSI_ERROR;
1723 snprintf(ci->error_str, sizeof(ci->error_str),
1724 "Unable to allocate %d bytes for iSCSI session list",
1729 sbuf_printf(sb, "<ctlislist>\n");
1730 mtx_lock(&softc->lock);
1731 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1732 if (cs->cs_target == NULL)
1734 error = sbuf_printf(sb, "<connection id=\"%d\">"
1735 "<initiator>%s</initiator>"
1736 "<initiator_addr>%s</initiator_addr>"
1737 "<initiator_alias>%s</initiator_alias>"
1738 "<target>%s</target>"
1739 "<target_alias>%s</target_alias>"
1740 "<target_portal_group_tag>%u</target_portal_group_tag>"
1741 "<header_digest>%s</header_digest>"
1742 "<data_digest>%s</data_digest>"
1743 "<max_recv_data_segment_length>%d</max_recv_data_segment_length>"
1744 "<max_send_data_segment_length>%d</max_send_data_segment_length>"
1745 "<max_burst_length>%d</max_burst_length>"
1746 "<first_burst_length>%d</first_burst_length>"
1747 "<immediate_data>%d</immediate_data>"
1749 "<offload>%s</offload>"
1752 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1753 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1754 cs->cs_target->ct_tag,
1755 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1756 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1757 cs->cs_conn->ic_max_recv_data_segment_length,
1758 cs->cs_conn->ic_max_send_data_segment_length,
1759 cs->cs_max_burst_length,
1760 cs->cs_first_burst_length,
1761 cs->cs_immediate_data,
1762 cs->cs_conn->ic_iser,
1763 cs->cs_conn->ic_offload);
1767 mtx_unlock(&softc->lock);
1768 error = sbuf_printf(sb, "</ctlislist>\n");
1771 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1772 snprintf(ci->error_str, sizeof(ci->error_str),
1773 "Out of space, %d bytes is too small", cilp->alloc_len);
1778 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1781 snprintf(ci->error_str, sizeof(ci->error_str),
1782 "copyout failed with error %d", error);
1783 ci->status = CTL_ISCSI_ERROR;
1786 cilp->fill_len = sbuf_len(sb) + 1;
1787 ci->status = CTL_ISCSI_OK;
1792 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1794 struct icl_pdu *response;
1795 struct iscsi_bhs_asynchronous_message *bhsam;
1796 struct ctl_iscsi_logout_params *cilp;
1797 struct cfiscsi_session *cs;
1798 struct cfiscsi_softc *softc;
1801 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1802 softc = &cfiscsi_softc;
1804 mtx_lock(&softc->lock);
1805 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1806 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1807 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1808 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1811 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1812 if (response == NULL) {
1813 ci->status = CTL_ISCSI_ERROR;
1814 snprintf(ci->error_str, sizeof(ci->error_str),
1815 "Unable to allocate memory");
1816 mtx_unlock(&softc->lock);
1820 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1821 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1822 bhsam->bhsam_flags = 0x80;
1823 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1824 bhsam->bhsam_parameter3 = htons(10);
1825 cfiscsi_pdu_queue(response);
1828 mtx_unlock(&softc->lock);
1831 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1832 snprintf(ci->error_str, sizeof(ci->error_str),
1833 "No matching connections found");
1837 ci->status = CTL_ISCSI_OK;
1841 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1843 struct icl_pdu *response;
1844 struct iscsi_bhs_asynchronous_message *bhsam;
1845 struct ctl_iscsi_terminate_params *citp;
1846 struct cfiscsi_session *cs;
1847 struct cfiscsi_softc *softc;
1850 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1851 softc = &cfiscsi_softc;
1853 mtx_lock(&softc->lock);
1854 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1855 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1856 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1857 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1860 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1861 if (response == NULL) {
1863 * Oh well. Just terminate the connection.
1866 bhsam = (struct iscsi_bhs_asynchronous_message *)
1868 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1869 bhsam->bhsam_flags = 0x80;
1870 bhsam->bhsam_0xffffffff = 0xffffffff;
1871 bhsam->bhsam_async_event =
1872 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1873 cfiscsi_pdu_queue(response);
1875 cfiscsi_session_terminate(cs);
1878 mtx_unlock(&softc->lock);
1881 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1882 snprintf(ci->error_str, sizeof(ci->error_str),
1883 "No matching connections found");
1887 ci->status = CTL_ISCSI_OK;
1891 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1893 struct ctl_iscsi_limits_params *cilp;
1894 struct icl_drv_limits idl;
1897 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1899 error = icl_limits(cilp->offload, false, cilp->socket, &idl);
1901 ci->status = CTL_ISCSI_ERROR;
1902 snprintf(ci->error_str, sizeof(ci->error_str),
1903 "%s: icl_limits failed with error %d",
1908 cilp->max_recv_data_segment_length =
1909 idl.idl_max_recv_data_segment_length;
1910 cilp->max_send_data_segment_length =
1911 idl.idl_max_send_data_segment_length;
1912 cilp->max_burst_length = idl.idl_max_burst_length;
1913 cilp->first_burst_length = idl.idl_first_burst_length;
1915 ci->status = CTL_ISCSI_OK;
1918 #ifdef ICL_KERNEL_PROXY
1920 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1922 struct ctl_iscsi_listen_params *cilp;
1923 struct sockaddr *sa;
1926 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1928 if (cfiscsi_softc.listener == NULL) {
1929 CFISCSI_DEBUG("no listener");
1930 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1931 ci->status = CTL_ISCSI_ERROR;
1935 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1937 CFISCSI_DEBUG("getsockaddr, error %d", error);
1938 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1939 ci->status = CTL_ISCSI_ERROR;
1943 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1944 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1947 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1948 snprintf(ci->error_str, sizeof(ci->error_str),
1949 "icl_listen_add failed, error %d", error);
1950 ci->status = CTL_ISCSI_ERROR;
1954 ci->status = CTL_ISCSI_OK;
1958 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1960 struct ctl_iscsi_accept_params *ciap;
1961 struct cfiscsi_session *cs;
1964 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1966 mtx_lock(&cfiscsi_softc.lock);
1968 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1969 if (cs->cs_waiting_for_ctld)
1974 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1976 mtx_unlock(&cfiscsi_softc.lock);
1977 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1978 ci->status = CTL_ISCSI_ERROR;
1982 mtx_unlock(&cfiscsi_softc.lock);
1984 cs->cs_waiting_for_ctld = false;
1985 cs->cs_login_phase = true;
1987 ciap->connection_id = cs->cs_id;
1988 ciap->portal_id = cs->cs_portal_id;
1989 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1990 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1991 cs->cs_initiator_sa->sa_len);
1993 snprintf(ci->error_str, sizeof(ci->error_str),
1994 "copyout failed with error %d", error);
1995 ci->status = CTL_ISCSI_ERROR;
1999 ci->status = CTL_ISCSI_OK;
2003 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
2005 struct ctl_iscsi_send_params *cisp;
2006 struct cfiscsi_session *cs;
2012 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
2014 mtx_lock(&cfiscsi_softc.lock);
2015 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2016 if (cs->cs_id == cisp->connection_id)
2020 mtx_unlock(&cfiscsi_softc.lock);
2021 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
2022 ci->status = CTL_ISCSI_ERROR;
2025 mtx_unlock(&cfiscsi_softc.lock);
2028 if (cs->cs_login_phase == false)
2032 if (cs->cs_terminating) {
2033 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
2034 ci->status = CTL_ISCSI_ERROR;
2038 datalen = cisp->data_segment_len;
2042 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
2043 if (datalen > 65535) {
2044 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
2045 ci->status = CTL_ISCSI_ERROR;
2049 data = malloc(datalen, M_CFISCSI, M_WAITOK);
2050 error = copyin(cisp->data_segment, data, datalen);
2052 free(data, M_CFISCSI);
2053 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
2054 ci->status = CTL_ISCSI_ERROR;
2059 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
2060 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
2062 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
2063 free(data, M_CFISCSI);
2065 CFISCSI_SESSION_LOCK(cs);
2067 CFISCSI_SESSION_UNLOCK(cs);
2068 ci->status = CTL_ISCSI_OK;
2072 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
2074 struct ctl_iscsi_receive_params *cirp;
2075 struct cfiscsi_session *cs;
2080 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
2082 mtx_lock(&cfiscsi_softc.lock);
2083 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2084 if (cs->cs_id == cirp->connection_id)
2088 mtx_unlock(&cfiscsi_softc.lock);
2089 snprintf(ci->error_str, sizeof(ci->error_str),
2090 "connection not found");
2091 ci->status = CTL_ISCSI_ERROR;
2094 mtx_unlock(&cfiscsi_softc.lock);
2097 if (is->is_login_phase == false)
2101 CFISCSI_SESSION_LOCK(cs);
2102 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2103 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2105 CFISCSI_SESSION_UNLOCK(cs);
2106 snprintf(ci->error_str, sizeof(ci->error_str),
2107 "interrupted by signal");
2108 ci->status = CTL_ISCSI_ERROR;
2113 if (cs->cs_terminating) {
2114 CFISCSI_SESSION_UNLOCK(cs);
2115 snprintf(ci->error_str, sizeof(ci->error_str),
2116 "connection terminating");
2117 ci->status = CTL_ISCSI_ERROR;
2120 ip = cs->cs_login_pdu;
2121 cs->cs_login_pdu = NULL;
2122 CFISCSI_SESSION_UNLOCK(cs);
2124 if (ip->ip_data_len > cirp->data_segment_len) {
2126 snprintf(ci->error_str, sizeof(ci->error_str),
2127 "data segment too big");
2128 ci->status = CTL_ISCSI_ERROR;
2132 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2133 if (ip->ip_data_len > 0) {
2134 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2135 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2136 copyout(data, cirp->data_segment, ip->ip_data_len);
2137 free(data, M_CFISCSI);
2141 ci->status = CTL_ISCSI_OK;
2144 #endif /* !ICL_KERNEL_PROXY */
2147 cfiscsi_ioctl_port_create(struct ctl_req *req)
2149 struct cfiscsi_target *ct;
2150 struct ctl_port *port;
2151 const char *target, *alias, *val;
2152 struct scsi_vpd_id_descriptor *desc;
2153 int retval, len, idlen;
2156 target = dnvlist_get_string(req->args_nvl, "cfiscsi_target", NULL);
2157 alias = dnvlist_get_string(req->args_nvl, "cfiscsi_target_alias", NULL);
2158 val = dnvlist_get_string(req->args_nvl, "cfiscsi_portal_group_tag",
2161 if (target == NULL || val == NULL) {
2162 req->status = CTL_LUN_ERROR;
2163 snprintf(req->error_str, sizeof(req->error_str),
2164 "Missing required argument");
2168 tag = strtoul(val, NULL, 0);
2169 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2171 req->status = CTL_LUN_ERROR;
2172 snprintf(req->error_str, sizeof(req->error_str),
2173 "failed to create target \"%s\"", target);
2176 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2177 req->status = CTL_LUN_ERROR;
2178 snprintf(req->error_str, sizeof(req->error_str),
2179 "target \"%s\" for portal group tag %u already exists",
2181 cfiscsi_target_release(ct);
2184 port = &ct->ct_port;
2186 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2189 port->frontend = &cfiscsi_frontend;
2190 port->port_type = CTL_PORT_ISCSI;
2191 /* XXX KDM what should the real number be here? */
2192 port->num_requested_ctl_io = 4096;
2193 port->port_name = "iscsi";
2194 port->physical_port = (int)tag;
2195 port->virtual_port = ct->ct_target_id;
2196 port->port_online = cfiscsi_online;
2197 port->port_offline = cfiscsi_offline;
2198 port->port_info = cfiscsi_info;
2199 port->onoff_arg = ct;
2200 port->fe_datamove = cfiscsi_datamove;
2201 port->fe_done = cfiscsi_done;
2202 port->targ_port = -1;
2203 port->options = nvlist_clone(req->args_nvl);
2205 /* Generate Port ID. */
2206 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2207 idlen = roundup2(idlen, 4);
2208 len = sizeof(struct scsi_vpd_device_id) + idlen;
2209 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2210 M_CTL, M_WAITOK | M_ZERO);
2211 port->port_devid->len = len;
2212 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2213 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2214 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2215 SVPD_ID_TYPE_SCSI_NAME;
2216 desc->length = idlen;
2217 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2219 /* Generate Target ID. */
2220 idlen = strlen(target) + 1;
2221 idlen = roundup2(idlen, 4);
2222 len = sizeof(struct scsi_vpd_device_id) + idlen;
2223 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2224 M_CTL, M_WAITOK | M_ZERO);
2225 port->target_devid->len = len;
2226 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2227 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2228 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2229 SVPD_ID_TYPE_SCSI_NAME;
2230 desc->length = idlen;
2231 strlcpy(desc->identifier, target, idlen);
2233 retval = ctl_port_register(port);
2235 free(port->port_devid, M_CFISCSI);
2236 free(port->target_devid, M_CFISCSI);
2237 cfiscsi_target_release(ct);
2238 req->status = CTL_LUN_ERROR;
2239 snprintf(req->error_str, sizeof(req->error_str),
2240 "ctl_port_register() failed with error %d", retval);
2244 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2245 req->status = CTL_LUN_OK;
2246 req->result_nvl = nvlist_create(0);
2247 nvlist_add_number(req->result_nvl, "port_id", port->targ_port);
2251 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2253 struct cfiscsi_target *ct;
2254 const char *target, *val;
2257 target = dnvlist_get_string(req->args_nvl, "cfiscsi_target", NULL);
2258 val = dnvlist_get_string(req->args_nvl, "cfiscsi_portal_group_tag",
2261 if (target == NULL || val == NULL) {
2262 req->status = CTL_LUN_ERROR;
2263 snprintf(req->error_str, sizeof(req->error_str),
2264 "Missing required argument");
2268 tag = strtoul(val, NULL, 0);
2269 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2271 req->status = CTL_LUN_ERROR;
2272 snprintf(req->error_str, sizeof(req->error_str),
2273 "can't find target \"%s\"", target);
2277 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2278 ctl_port_offline(&ct->ct_port);
2279 cfiscsi_target_release(ct);
2280 cfiscsi_target_release(ct);
2281 req->status = CTL_LUN_OK;
2285 cfiscsi_ioctl(struct cdev *dev,
2286 u_long cmd, caddr_t addr, int flag, struct thread *td)
2288 struct ctl_iscsi *ci;
2289 struct ctl_req *req;
2291 if (cmd == CTL_PORT_REQ) {
2292 req = (struct ctl_req *)addr;
2293 switch (req->reqtype) {
2294 case CTL_REQ_CREATE:
2295 cfiscsi_ioctl_port_create(req);
2297 case CTL_REQ_REMOVE:
2298 cfiscsi_ioctl_port_remove(req);
2301 req->status = CTL_LUN_ERROR;
2302 snprintf(req->error_str, sizeof(req->error_str),
2303 "Unsupported request type %d", req->reqtype);
2308 if (cmd != CTL_ISCSI)
2311 ci = (struct ctl_iscsi *)addr;
2313 case CTL_ISCSI_HANDOFF:
2314 cfiscsi_ioctl_handoff(ci);
2316 case CTL_ISCSI_LIST:
2317 cfiscsi_ioctl_list(ci);
2319 case CTL_ISCSI_LOGOUT:
2320 cfiscsi_ioctl_logout(ci);
2322 case CTL_ISCSI_TERMINATE:
2323 cfiscsi_ioctl_terminate(ci);
2325 case CTL_ISCSI_LIMITS:
2326 cfiscsi_ioctl_limits(ci);
2328 #ifdef ICL_KERNEL_PROXY
2329 case CTL_ISCSI_LISTEN:
2330 cfiscsi_ioctl_listen(ci);
2332 case CTL_ISCSI_ACCEPT:
2333 cfiscsi_ioctl_accept(ci);
2335 case CTL_ISCSI_SEND:
2336 cfiscsi_ioctl_send(ci);
2338 case CTL_ISCSI_RECEIVE:
2339 cfiscsi_ioctl_receive(ci);
2342 case CTL_ISCSI_LISTEN:
2343 case CTL_ISCSI_ACCEPT:
2344 case CTL_ISCSI_SEND:
2345 case CTL_ISCSI_RECEIVE:
2346 ci->status = CTL_ISCSI_ERROR;
2347 snprintf(ci->error_str, sizeof(ci->error_str),
2348 "%s: CTL compiled without ICL_KERNEL_PROXY",
2351 #endif /* !ICL_KERNEL_PROXY */
2353 ci->status = CTL_ISCSI_ERROR;
2354 snprintf(ci->error_str, sizeof(ci->error_str),
2355 "%s: invalid iSCSI request type %d", __func__, ci->type);
2363 cfiscsi_target_hold(struct cfiscsi_target *ct)
2366 refcount_acquire(&ct->ct_refcount);
2370 cfiscsi_target_release(struct cfiscsi_target *ct)
2372 struct cfiscsi_softc *softc;
2374 softc = ct->ct_softc;
2375 mtx_lock(&softc->lock);
2376 if (refcount_release(&ct->ct_refcount)) {
2377 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2378 mtx_unlock(&softc->lock);
2379 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2380 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2381 if (ctl_port_deregister(&ct->ct_port) != 0)
2382 printf("%s: ctl_port_deregister() failed\n",
2385 free(ct, M_CFISCSI);
2389 mtx_unlock(&softc->lock);
2392 static struct cfiscsi_target *
2393 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2395 struct cfiscsi_target *ct;
2397 mtx_lock(&softc->lock);
2398 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2399 if (ct->ct_tag != tag ||
2400 strcmp(name, ct->ct_name) != 0 ||
2401 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2403 cfiscsi_target_hold(ct);
2404 mtx_unlock(&softc->lock);
2407 mtx_unlock(&softc->lock);
2412 static struct cfiscsi_target *
2413 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2414 const char *alias, uint16_t tag)
2416 struct cfiscsi_target *ct, *newct;
2418 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2421 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2423 mtx_lock(&softc->lock);
2424 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2425 if (ct->ct_tag != tag ||
2426 strcmp(name, ct->ct_name) != 0 ||
2427 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2429 cfiscsi_target_hold(ct);
2430 mtx_unlock(&softc->lock);
2431 free(newct, M_CFISCSI);
2435 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2437 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2438 newct->ct_tag = tag;
2439 refcount_init(&newct->ct_refcount, 1);
2440 newct->ct_softc = softc;
2441 if (TAILQ_EMPTY(&softc->targets))
2442 softc->last_target_id = 0;
2443 newct->ct_target_id = ++softc->last_target_id;
2444 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2445 mtx_unlock(&softc->lock);
2451 cfiscsi_pdu_done(struct icl_pdu *ip, int error)
2455 ; // XXX: Do something on error?
2456 ((ctl_ref)ip->ip_prv0)(ip->ip_prv1, -1);
2460 cfiscsi_datamove_in(union ctl_io *io)
2462 struct cfiscsi_session *cs;
2463 struct icl_pdu *request, *response;
2464 const struct iscsi_bhs_scsi_command *bhssc;
2465 struct iscsi_bhs_data_in *bhsdi;
2466 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2467 size_t len, expected_len, sg_len, buffer_offset;
2468 size_t max_send_data_segment_length;
2469 const char *sg_addr;
2471 int ctl_sg_count, error, i;
2473 request = PRIV_REQUEST(io);
2474 cs = PDU_SESSION(request);
2476 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2477 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2478 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2479 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2481 if (io->scsiio.kern_sg_entries > 0) {
2482 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2483 ctl_sg_count = io->scsiio.kern_sg_entries;
2485 ctl_sglist = &ctl_sg_entry;
2486 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2487 ctl_sglist->len = io->scsiio.kern_data_len;
2492 * This is the offset within the current SCSI command; for the first
2493 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2494 * it will be the sum of lengths of previous ones.
2496 buffer_offset = io->scsiio.kern_rel_offset;
2499 * This is the transfer length expected by the initiator. It can be
2500 * different from the amount of data from the SCSI point of view.
2502 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2505 * If the transfer is outside of expected length -- we are done.
2507 if (buffer_offset >= expected_len) {
2509 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2510 "already sent the expected len", buffer_offset);
2512 ctl_datamove_done(io, true);
2516 if (io->scsiio.kern_data_ref != NULL)
2517 cb = cfiscsi_pdu_done;
2526 if (cs->cs_conn->ic_hw_isomax != 0)
2527 max_send_data_segment_length = cs->cs_conn->ic_hw_isomax;
2529 max_send_data_segment_length =
2530 cs->cs_conn->ic_max_send_data_segment_length;
2532 if (response == NULL) {
2533 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2534 if (response == NULL) {
2535 CFISCSI_SESSION_WARN(cs, "failed to "
2536 "allocate memory; dropping connection");
2537 ctl_set_busy(&io->scsiio);
2538 ctl_datamove_done(io, true);
2539 cfiscsi_session_terminate(cs);
2542 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2543 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2544 bhsdi->bhsdi_initiator_task_tag =
2545 bhssc->bhssc_initiator_task_tag;
2546 bhsdi->bhsdi_target_transfer_tag = 0xffffffff;
2547 bhsdi->bhsdi_datasn = htonl(PRIV_EXPDATASN(io));
2548 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2551 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2553 sg_addr = ctl_sglist[i].addr;
2554 sg_len = ctl_sglist[i].len;
2555 KASSERT(sg_len > 0, ("sg_len <= 0"));
2561 * Truncate to maximum data segment length.
2563 KASSERT(response->ip_data_len < max_send_data_segment_length,
2564 ("ip_data_len %zd >= max_send_data_segment_length %zd",
2565 response->ip_data_len, max_send_data_segment_length));
2566 if (response->ip_data_len + len > max_send_data_segment_length) {
2567 len = max_send_data_segment_length - response->ip_data_len;
2568 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2573 * Truncate to expected data transfer length.
2575 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2576 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2577 buffer_offset, response->ip_data_len, expected_len));
2578 if (buffer_offset + response->ip_data_len + len > expected_len) {
2579 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2580 "to expected data transfer length %zd",
2581 buffer_offset + response->ip_data_len + len, expected_len);
2582 len = expected_len - (buffer_offset + response->ip_data_len);
2583 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2587 error = icl_pdu_append_data(response, sg_addr, len,
2588 M_NOWAIT | (cb ? ICL_NOCOPY : 0));
2590 CFISCSI_SESSION_WARN(cs, "failed to "
2591 "allocate memory; dropping connection");
2592 icl_pdu_free(response);
2593 ctl_set_busy(&io->scsiio);
2594 ctl_datamove_done(io, true);
2595 cfiscsi_session_terminate(cs);
2600 io->scsiio.kern_data_resid -= len;
2602 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2603 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2604 buffer_offset, response->ip_data_len, expected_len));
2605 if (buffer_offset + response->ip_data_len == expected_len) {
2607 * Already have the amount of data the initiator wanted.
2614 * End of scatter-gather segment;
2615 * proceed to the next one...
2617 if (i == ctl_sg_count - 1) {
2619 * ... unless this was the last one.
2626 if (response->ip_data_len == max_send_data_segment_length) {
2628 * Can't stuff more data into the current PDU;
2629 * queue it. Note that's not enough to check
2630 * for kern_data_resid == 0 instead; there
2631 * may be several Data-In PDUs for the final
2632 * call to cfiscsi_datamove(), and we want
2633 * to set the F flag only on the last of them.
2635 buffer_offset += response->ip_data_len;
2636 if (buffer_offset == io->scsiio.kern_total_len ||
2637 buffer_offset == expected_len) {
2638 buffer_offset -= response->ip_data_len;
2641 PRIV_EXPDATASN(io) += howmany(response->ip_data_len,
2642 cs->cs_conn->ic_max_send_data_segment_length);
2644 response->ip_prv0 = io->scsiio.kern_data_ref;
2645 response->ip_prv1 = io->scsiio.kern_data_arg;
2646 io->scsiio.kern_data_ref(io->scsiio.kern_data_arg, 1);
2648 cfiscsi_pdu_queue_cb(response, cb);
2653 if (response != NULL) {
2654 buffer_offset += response->ip_data_len;
2655 if (buffer_offset == io->scsiio.kern_total_len ||
2656 buffer_offset == expected_len) {
2657 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2658 if (io->io_hdr.status == CTL_SUCCESS) {
2659 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2660 if (io->scsiio.kern_total_len <
2661 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2662 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2663 bhsdi->bhsdi_residual_count =
2664 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2665 io->scsiio.kern_total_len);
2666 } else if (io->scsiio.kern_total_len >
2667 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2668 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2669 bhsdi->bhsdi_residual_count =
2670 htonl(io->scsiio.kern_total_len -
2671 ntohl(bhssc->bhssc_expected_data_transfer_length));
2673 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2674 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2677 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2678 PRIV_EXPDATASN(io) += howmany(response->ip_data_len,
2679 cs->cs_conn->ic_max_send_data_segment_length);
2681 response->ip_prv0 = io->scsiio.kern_data_ref;
2682 response->ip_prv1 = io->scsiio.kern_data_arg;
2683 io->scsiio.kern_data_ref(io->scsiio.kern_data_arg, 1);
2685 cfiscsi_pdu_queue_cb(response, cb);
2688 ctl_datamove_done(io, true);
2692 cfiscsi_datamove_out(union ctl_io *io)
2694 struct cfiscsi_session *cs;
2695 struct icl_pdu *request, *response;
2696 const struct iscsi_bhs_scsi_command *bhssc;
2697 struct iscsi_bhs_r2t *bhsr2t;
2698 struct cfiscsi_data_wait *cdw;
2699 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2700 uint32_t expected_len, datamove_len, r2t_off, r2t_len;
2701 uint32_t target_transfer_tag;
2704 request = PRIV_REQUEST(io);
2705 cs = PDU_SESSION(request);
2707 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2708 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2709 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2710 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2713 * Complete write underflow. Not a single byte to read. Return.
2715 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2716 if (io->scsiio.kern_rel_offset >= expected_len) {
2717 ctl_datamove_done(io, true);
2721 datamove_len = MIN(io->scsiio.kern_data_len,
2722 expected_len - io->scsiio.kern_rel_offset);
2724 target_transfer_tag =
2725 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2726 if (target_transfer_tag == 0xffffffff) {
2727 target_transfer_tag =
2728 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2730 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2731 &target_transfer_tag);
2733 CFISCSI_SESSION_WARN(cs, "failed to "
2734 "allocate memory; dropping connection");
2735 ctl_set_busy(&io->scsiio);
2736 ctl_datamove_done(io, true);
2737 cfiscsi_session_terminate(cs);
2741 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2742 "task tag 0x%x, target transfer tag 0x%x",
2743 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2746 cdw->cdw_ctl_io = io;
2747 cdw->cdw_target_transfer_tag = target_transfer_tag;
2748 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2749 cdw->cdw_r2t_end = datamove_len;
2750 cdw->cdw_datasn = 0;
2752 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2753 if (io->scsiio.kern_sg_entries > 0) {
2754 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2756 ctl_sglist = &ctl_sg_entry;
2757 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2758 ctl_sglist->len = datamove_len;
2760 cdw->cdw_sg_index = 0;
2761 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2762 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2763 r2t_off = io->scsiio.ext_data_filled;
2764 while (r2t_off > 0) {
2765 if (r2t_off >= cdw->cdw_sg_len) {
2766 r2t_off -= cdw->cdw_sg_len;
2767 cdw->cdw_sg_index++;
2768 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2769 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2772 cdw->cdw_sg_addr += r2t_off;
2773 cdw->cdw_sg_len -= r2t_off;
2777 if (cs->cs_immediate_data &&
2778 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2779 icl_pdu_data_segment_length(request)) {
2780 done = cfiscsi_handle_data_segment(request, cdw);
2782 cfiscsi_data_wait_free(cs, cdw);
2783 ctl_datamove_done(io, true);
2788 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2789 r2t_len = MIN(datamove_len - io->scsiio.ext_data_filled,
2790 cs->cs_max_burst_length);
2791 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2793 CFISCSI_SESSION_LOCK(cs);
2794 if (cs->cs_terminating_tasks) {
2795 CFISCSI_SESSION_UNLOCK(cs);
2796 KASSERT((io->io_hdr.flags & CTL_FLAG_ABORT) != 0,
2797 ("%s: I/O request %p on termating session %p not aborted",
2799 CFISCSI_SESSION_WARN(cs, "aborting data_wait for aborted I/O");
2800 cfiscsi_data_wait_abort(cs, cdw, 44);
2803 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2804 CFISCSI_SESSION_UNLOCK(cs);
2807 * XXX: We should limit the number of outstanding R2T PDUs
2808 * per task to MaxOutstandingR2T.
2810 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2811 if (response == NULL) {
2812 CFISCSI_SESSION_WARN(cs, "failed to "
2813 "allocate memory; dropping connection");
2814 ctl_set_busy(&io->scsiio);
2815 ctl_datamove_done(io, true);
2816 cfiscsi_session_terminate(cs);
2819 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
2820 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2821 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2822 bhsr2t->bhsr2t_flags = 0x80;
2823 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2824 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2825 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2827 * XXX: Here we assume that cfiscsi_datamove() won't ever
2828 * be running concurrently on several CPUs for a given
2831 bhsr2t->bhsr2t_r2tsn = htonl(PRIV_R2TSN(io)++);
2833 * This is the offset within the current SCSI command;
2834 * i.e. for the first call of datamove(), it will be 0,
2835 * and for subsequent ones it will be the sum of lengths
2838 * The ext_data_filled is to account for unsolicited
2839 * (immediate) data that might have already arrived.
2841 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2843 * This is the total length (sum of S/G lengths) this call
2844 * to cfiscsi_datamove() is supposed to handle, limited by
2847 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2848 cfiscsi_pdu_queue(response);
2852 cfiscsi_datamove(union ctl_io *io)
2855 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2856 cfiscsi_datamove_in(io);
2858 /* We hadn't received anything during this datamove yet. */
2859 io->scsiio.ext_data_filled = 0;
2860 cfiscsi_datamove_out(io);
2865 cfiscsi_scsi_command_done(union ctl_io *io)
2867 struct icl_pdu *request, *response;
2868 struct iscsi_bhs_scsi_command *bhssc;
2869 struct iscsi_bhs_scsi_response *bhssr;
2871 struct cfiscsi_data_wait *cdw;
2872 struct cfiscsi_session *cs;
2874 uint16_t sense_length;
2876 request = PRIV_REQUEST(io);
2877 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2878 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2879 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2880 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2882 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2883 // bhssc->bhssc_initiator_task_tag);
2886 cs = PDU_SESSION(request);
2887 CFISCSI_SESSION_LOCK(cs);
2888 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2889 KASSERT(bhssc->bhssc_initiator_task_tag !=
2890 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2891 CFISCSI_SESSION_UNLOCK(cs);
2895 * Do not return status for aborted commands.
2896 * There are exceptions, but none supported by CTL yet.
2898 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2899 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2900 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2902 icl_pdu_free(request);
2906 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2907 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2908 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2909 bhssr->bhssr_flags = 0x80;
2911 * XXX: We don't deal with bidirectional under/overflows;
2912 * does anything actually support those?
2914 if (io->scsiio.kern_total_len <
2915 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2916 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2917 bhssr->bhssr_residual_count =
2918 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2919 io->scsiio.kern_total_len);
2920 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2921 // ntohl(bhssr->bhssr_residual_count));
2922 } else if (io->scsiio.kern_total_len >
2923 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2924 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2925 bhssr->bhssr_residual_count = htonl(io->scsiio.kern_total_len -
2926 ntohl(bhssc->bhssc_expected_data_transfer_length));
2927 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2928 // ntohl(bhssr->bhssr_residual_count));
2930 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2931 bhssr->bhssr_status = io->scsiio.scsi_status;
2932 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2933 bhssr->bhssr_expdatasn = htonl(PRIV_EXPDATASN(io));
2935 if (io->scsiio.sense_len > 0) {
2937 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2938 io->scsiio.sense_len);
2940 sense_length = htons(io->scsiio.sense_len);
2941 icl_pdu_append_data(response,
2942 &sense_length, sizeof(sense_length), M_WAITOK);
2943 icl_pdu_append_data(response,
2944 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2948 icl_pdu_free(request);
2949 cfiscsi_pdu_queue(response);
2953 cfiscsi_task_management_done(union ctl_io *io)
2955 struct icl_pdu *request, *response;
2956 struct iscsi_bhs_task_management_request *bhstmr;
2957 struct iscsi_bhs_task_management_response *bhstmr2;
2958 struct cfiscsi_data_wait *cdw, *tmpcdw;
2959 struct cfiscsi_session *cs, *tcs;
2960 struct cfiscsi_softc *softc;
2963 request = PRIV_REQUEST(io);
2964 cs = PDU_SESSION(request);
2965 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2966 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2967 ISCSI_BHS_OPCODE_TASK_REQUEST,
2968 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2971 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2972 bhstmr->bhstmr_initiator_task_tag,
2973 bhstmr->bhstmr_referenced_task_tag);
2976 if ((bhstmr->bhstmr_function & ~0x80) ==
2977 BHSTMR_FUNCTION_ABORT_TASK) {
2979 * Make sure we no longer wait for Data-Out for this command.
2981 CFISCSI_SESSION_LOCK(cs);
2982 TAILQ_FOREACH_SAFE(cdw,
2983 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2984 if (bhstmr->bhstmr_referenced_task_tag !=
2985 cdw->cdw_initiator_task_tag)
2989 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2990 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2992 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2994 cfiscsi_data_wait_abort(cs, cdw, 43);
2996 CFISCSI_SESSION_UNLOCK(cs);
2998 if ((bhstmr->bhstmr_function & ~0x80) ==
2999 BHSTMR_FUNCTION_TARGET_COLD_RESET &&
3000 io->io_hdr.status == CTL_SUCCESS)
3003 response = cfiscsi_pdu_new_response(request, M_WAITOK);
3004 bhstmr2 = (struct iscsi_bhs_task_management_response *)
3006 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
3007 bhstmr2->bhstmr_flags = 0x80;
3008 switch (io->taskio.task_status) {
3009 case CTL_TASK_FUNCTION_COMPLETE:
3010 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
3012 case CTL_TASK_FUNCTION_SUCCEEDED:
3013 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED;
3015 case CTL_TASK_LUN_DOES_NOT_EXIST:
3016 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST;
3018 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
3020 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
3023 memcpy(bhstmr2->bhstmr_additional_reponse_information,
3024 io->taskio.task_resp, sizeof(io->taskio.task_resp));
3025 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
3028 icl_pdu_free(request);
3029 cfiscsi_pdu_queue(response);
3032 softc = cs->cs_target->ct_softc;
3033 mtx_lock(&softc->lock);
3034 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) {
3035 if (tcs->cs_target == cs->cs_target)
3036 cfiscsi_session_terminate(tcs);
3038 mtx_unlock(&softc->lock);
3043 cfiscsi_done(union ctl_io *io)
3045 struct icl_pdu *request;
3046 struct cfiscsi_session *cs;
3048 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
3049 ("invalid CTL status %#x", io->io_hdr.status));
3051 request = PRIV_REQUEST(io);
3052 cs = PDU_SESSION(request);
3054 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
3055 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
3056 cfiscsi_scsi_command_done(io);
3058 case ISCSI_BHS_OPCODE_TASK_REQUEST:
3059 cfiscsi_task_management_done(io);
3061 case ISCSI_BHS_OPCODE_INTERNAL:
3063 * Implicit task termination has just completed; nothing to do.
3065 icl_pdu_free(request);
3066 cs->cs_tasks_aborted = true;
3067 refcount_release(&cs->cs_outstanding_ctl_pdus);
3068 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
3072 panic("cfiscsi_done called with wrong opcode 0x%x",
3073 request->ip_bhs->bhs_opcode);
3076 refcount_release(&cs->cs_outstanding_ctl_pdus);