2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * CTL frontend for the iSCSI protocol.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/condvar.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/queue.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
54 #include <sys/unistd.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_da.h>
59 #include <cam/ctl/ctl_io.h>
60 #include <cam/ctl/ctl.h>
61 #include <cam/ctl/ctl_backend.h>
62 #include <cam/ctl/ctl_error.h>
63 #include <cam/ctl/ctl_frontend.h>
64 #include <cam/ctl/ctl_frontend_internal.h>
65 #include <cam/ctl/ctl_debug.h>
66 #include <cam/ctl/ctl_ha.h>
67 #include <cam/ctl/ctl_ioctl.h>
68 #include <cam/ctl/ctl_private.h>
70 #include <dev/iscsi/icl.h>
71 #include <dev/iscsi/iscsi_proto.h>
72 #include <cam/ctl/ctl_frontend_iscsi.h>
74 #ifdef ICL_KERNEL_PROXY
75 #include <sys/socketvar.h>
78 #ifdef ICL_KERNEL_PROXY
79 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
82 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
83 static uma_zone_t cfiscsi_data_wait_zone;
85 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
86 "CAM Target Layer iSCSI Frontend");
88 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
89 &debug, 1, "Enable debug messages");
90 static int ping_timeout = 5;
91 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
92 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
93 static int login_timeout = 60;
94 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
95 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
96 static int maxcmdsn_delta = 256;
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN,
98 &maxcmdsn_delta, 256, "Number of commands the initiator can send "
99 "without confirmation");
101 #define CFISCSI_DEBUG(X, ...) \
104 printf("%s: " X "\n", \
105 __func__, ## __VA_ARGS__); \
109 #define CFISCSI_WARN(X, ...) \
112 printf("WARNING: %s: " X "\n", \
113 __func__, ## __VA_ARGS__); \
117 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
120 printf("%s: %s (%s): " X "\n", \
121 __func__, S->cs_initiator_addr, \
122 S->cs_initiator_name, ## __VA_ARGS__); \
126 #define CFISCSI_SESSION_WARN(S, X, ...) \
129 printf("WARNING: %s (%s): " X "\n", \
130 S->cs_initiator_addr, \
131 S->cs_initiator_name, ## __VA_ARGS__); \
135 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
136 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
137 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
139 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
140 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
141 #define PDU_EXPDATASN(X) (X)->ip_prv0
142 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
143 #define PDU_R2TSN(X) (X)->ip_prv2
145 int cfiscsi_init(void);
146 static void cfiscsi_online(void *arg);
147 static void cfiscsi_offline(void *arg);
148 static int cfiscsi_info(void *arg, struct sbuf *sb);
149 static int cfiscsi_lun_enable(void *arg,
150 struct ctl_id target_id, int lun_id);
151 static int cfiscsi_lun_disable(void *arg,
152 struct ctl_id target_id, int lun_id);
153 static uint32_t cfiscsi_lun_map(void *arg, uint32_t lun);
154 static int cfiscsi_ioctl(struct cdev *dev,
155 u_long cmd, caddr_t addr, int flag, struct thread *td);
156 static void cfiscsi_datamove(union ctl_io *io);
157 static void cfiscsi_datamove_in(union ctl_io *io);
158 static void cfiscsi_datamove_out(union ctl_io *io);
159 static void cfiscsi_done(union ctl_io *io);
160 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
161 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
162 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
163 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
164 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
165 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
166 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
167 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
168 *softc, const char *name);
169 static struct cfiscsi_target *cfiscsi_target_find_or_create(
170 struct cfiscsi_softc *softc, const char *name, const char *alias);
171 static void cfiscsi_target_release(struct cfiscsi_target *ct);
172 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
174 static struct cfiscsi_softc cfiscsi_softc;
175 extern struct ctl_softc *control_softc;
177 static struct ctl_frontend cfiscsi_frontend =
180 .init = cfiscsi_init,
181 .ioctl = cfiscsi_ioctl,
183 CTL_FRONTEND_DECLARE(ctlcfiscsi, cfiscsi_frontend);
184 MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
186 static struct icl_pdu *
187 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
190 return (icl_pdu_new(request->ip_conn, flags));
194 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
196 const struct iscsi_bhs_scsi_command *bhssc;
197 struct cfiscsi_session *cs;
198 uint32_t cmdsn, expstatsn;
200 cs = PDU_SESSION(request);
203 * Every incoming PDU - not just NOP-Out - resets the ping timer.
204 * The purpose of the timeout is to reset the connection when it stalls;
205 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
213 * Data-Out PDUs don't contain CmdSN.
215 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
216 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
220 * We're only using fields common for all the request
221 * (initiator -> target) PDUs.
223 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
224 cmdsn = ntohl(bhssc->bhssc_cmdsn);
225 expstatsn = ntohl(bhssc->bhssc_expstatsn);
227 CFISCSI_SESSION_LOCK(cs);
229 if (expstatsn != cs->cs_statsn) {
230 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
231 "while current StatSN is %d", expstatsn,
237 * The target MUST silently ignore any non-immediate command outside
240 if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
241 CFISCSI_SESSION_UNLOCK(cs);
242 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
243 "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
247 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
250 CFISCSI_SESSION_UNLOCK(cs);
256 cfiscsi_pdu_handle(struct icl_pdu *request)
258 struct cfiscsi_session *cs;
261 cs = PDU_SESSION(request);
263 ignore = cfiscsi_pdu_update_cmdsn(request);
265 icl_pdu_free(request);
270 * Handle the PDU; this includes e.g. receiving the remaining
271 * part of PDU and submitting the SCSI command to CTL
272 * or queueing a reply. The handling routine is responsible
273 * for freeing the PDU when it's no longer needed.
275 switch (request->ip_bhs->bhs_opcode &
276 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
277 case ISCSI_BHS_OPCODE_NOP_OUT:
278 cfiscsi_pdu_handle_nop_out(request);
280 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
281 cfiscsi_pdu_handle_scsi_command(request);
283 case ISCSI_BHS_OPCODE_TASK_REQUEST:
284 cfiscsi_pdu_handle_task_request(request);
286 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
287 cfiscsi_pdu_handle_data_out(request);
289 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
290 cfiscsi_pdu_handle_logout_request(request);
293 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
294 "opcode 0x%x; dropping connection",
295 request->ip_bhs->bhs_opcode);
296 icl_pdu_free(request);
297 cfiscsi_session_terminate(cs);
303 cfiscsi_receive_callback(struct icl_pdu *request)
305 struct cfiscsi_session *cs;
307 cs = PDU_SESSION(request);
309 #ifdef ICL_KERNEL_PROXY
310 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
311 if (cs->cs_login_pdu == NULL)
312 cs->cs_login_pdu = request;
314 icl_pdu_free(request);
315 cv_signal(&cs->cs_login_cv);
320 cfiscsi_pdu_handle(request);
324 cfiscsi_error_callback(struct icl_conn *ic)
326 struct cfiscsi_session *cs;
328 cs = CONN_SESSION(ic);
330 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
331 cfiscsi_session_terminate(cs);
335 cfiscsi_pdu_prepare(struct icl_pdu *response)
337 struct cfiscsi_session *cs;
338 struct iscsi_bhs_scsi_response *bhssr;
339 bool advance_statsn = true;
341 cs = PDU_SESSION(response);
343 CFISCSI_SESSION_LOCK_ASSERT(cs);
346 * We're only using fields common for all the response
347 * (target -> initiator) PDUs.
349 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
352 * 10.8.3: "The StatSN for this connection is not advanced
353 * after this PDU is sent."
355 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
356 advance_statsn = false;
359 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
360 * StatSN for the connection is not advanced after this PDU is sent."
362 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
363 bhssr->bhssr_initiator_task_tag == 0xffffffff)
364 advance_statsn = false;
367 * See the comment below - StatSN is not meaningful and must
370 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
371 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
372 advance_statsn = false;
375 * 10.7.3: "The fields StatSN, Status, and Residual Count
376 * only have meaningful content if the S bit is set to 1."
378 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
379 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
380 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
381 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
382 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
391 cfiscsi_pdu_queue(struct icl_pdu *response)
393 struct cfiscsi_session *cs;
395 cs = PDU_SESSION(response);
397 CFISCSI_SESSION_LOCK(cs);
398 cfiscsi_pdu_prepare(response);
399 icl_pdu_queue(response);
400 CFISCSI_SESSION_UNLOCK(cs);
404 cfiscsi_decode_lun(uint64_t encoded)
410 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
411 * but is in fact an evil, multidimensional structure defined
412 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
414 memcpy(lun, &encoded, sizeof(lun));
415 switch (lun[0] & 0xC0) {
417 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
418 lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
419 CFISCSI_WARN("malformed LUN "
420 "(peripheral device addressing method): 0x%jx",
428 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
429 lun[6] != 0 || lun[7] != 0) {
430 CFISCSI_WARN("malformed LUN "
431 "(flat address space addressing method): 0x%jx",
436 result = ((lun[0] & 0x3f) << 8) + lun[1];
439 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
440 lun[6] != 0 || lun[7] != 0) {
441 CFISCSI_WARN("malformed LUN (extended flat "
442 "address space addressing method): 0x%jx",
447 result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
449 CFISCSI_WARN("unsupported LUN format 0x%jx",
459 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
461 struct cfiscsi_session *cs;
462 struct iscsi_bhs_nop_out *bhsno;
463 struct iscsi_bhs_nop_in *bhsni;
464 struct icl_pdu *response;
469 cs = PDU_SESSION(request);
470 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
472 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
474 * Nothing to do, iscsi_pdu_update_statsn() already
475 * zeroed the timeout.
477 icl_pdu_free(request);
481 datasize = icl_pdu_data_segment_length(request);
483 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
485 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
486 "dropping connection");
487 icl_pdu_free(request);
488 cfiscsi_session_terminate(cs);
491 icl_pdu_get_data(request, 0, data, datasize);
494 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
495 if (response == NULL) {
496 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
497 "droppping connection");
498 free(data, M_CFISCSI);
499 icl_pdu_free(request);
500 cfiscsi_session_terminate(cs);
503 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
504 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
505 bhsni->bhsni_flags = 0x80;
506 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
507 bhsni->bhsni_target_transfer_tag = 0xffffffff;
509 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
511 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
512 "dropping connection");
513 free(data, M_CFISCSI);
514 icl_pdu_free(request);
515 icl_pdu_free(response);
516 cfiscsi_session_terminate(cs);
519 free(data, M_CFISCSI);
522 icl_pdu_free(request);
523 cfiscsi_pdu_queue(response);
527 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
529 struct iscsi_bhs_scsi_command *bhssc;
530 struct cfiscsi_session *cs;
534 cs = PDU_SESSION(request);
535 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
536 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
537 // bhssc->bhssc_initiator_task_tag);
539 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
540 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
541 "ImmediateData=No; dropping connection");
542 icl_pdu_free(request);
543 cfiscsi_session_terminate(cs);
546 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
548 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
549 io->io_hdr.io_type = CTL_IO_SCSI;
550 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
551 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
552 io->io_hdr.nexus.targ_target.id = 0;
553 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
554 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
555 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
556 case BHSSC_FLAGS_ATTR_UNTAGGED:
557 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
559 case BHSSC_FLAGS_ATTR_SIMPLE:
560 io->scsiio.tag_type = CTL_TAG_SIMPLE;
562 case BHSSC_FLAGS_ATTR_ORDERED:
563 io->scsiio.tag_type = CTL_TAG_ORDERED;
565 case BHSSC_FLAGS_ATTR_HOQ:
566 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
568 case BHSSC_FLAGS_ATTR_ACA:
569 io->scsiio.tag_type = CTL_TAG_ACA;
572 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
573 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
574 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
577 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
578 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
579 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
580 error = ctl_queue(io);
581 if (error != CTL_RETVAL_COMPLETE) {
582 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
583 "dropping connection", error);
585 refcount_release(&cs->cs_outstanding_ctl_pdus);
586 icl_pdu_free(request);
587 cfiscsi_session_terminate(cs);
592 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
594 struct iscsi_bhs_task_management_request *bhstmr;
595 struct iscsi_bhs_task_management_response *bhstmr2;
596 struct icl_pdu *response;
597 struct cfiscsi_session *cs;
601 cs = PDU_SESSION(request);
602 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
603 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
605 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
606 io->io_hdr.io_type = CTL_IO_TASK;
607 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
608 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
609 io->io_hdr.nexus.targ_target.id = 0;
610 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
611 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
613 switch (bhstmr->bhstmr_function & ~0x80) {
614 case BHSTMR_FUNCTION_ABORT_TASK:
616 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
618 io->taskio.task_action = CTL_TASK_ABORT_TASK;
619 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
621 case BHSTMR_FUNCTION_ABORT_TASK_SET:
623 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
625 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
627 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
629 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
631 io->taskio.task_action = CTL_TASK_LUN_RESET;
633 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
635 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
637 io->taskio.task_action = CTL_TASK_TARGET_RESET;
640 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
641 bhstmr->bhstmr_function & ~0x80);
644 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
645 if (response == NULL) {
646 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
647 "dropping connection");
648 icl_pdu_free(request);
649 cfiscsi_session_terminate(cs);
652 bhstmr2 = (struct iscsi_bhs_task_management_response *)
654 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
655 bhstmr2->bhstmr_flags = 0x80;
656 bhstmr2->bhstmr_response =
657 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
658 bhstmr2->bhstmr_initiator_task_tag =
659 bhstmr->bhstmr_initiator_task_tag;
660 icl_pdu_free(request);
661 cfiscsi_pdu_queue(response);
665 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
666 error = ctl_queue(io);
667 if (error != CTL_RETVAL_COMPLETE) {
668 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
669 "dropping connection", error);
671 refcount_release(&cs->cs_outstanding_ctl_pdus);
672 icl_pdu_free(request);
673 cfiscsi_session_terminate(cs);
678 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
680 struct iscsi_bhs_data_out *bhsdo;
681 struct cfiscsi_session *cs;
682 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
683 size_t copy_len, len, off, buffer_offset;
687 cs = PDU_SESSION(request);
689 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
690 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
691 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
692 ISCSI_BHS_OPCODE_SCSI_COMMAND,
693 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
696 * We're only using fields common for Data-Out and SCSI Command PDUs.
698 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
700 io = cdw->cdw_ctl_io;
701 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
702 ("CTL_FLAG_DATA_IN"));
705 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
706 request->ip_data_len, io->scsiio.kern_total_len);
709 if (io->scsiio.kern_sg_entries > 0) {
710 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
711 ctl_sg_count = io->scsiio.kern_sg_entries;
713 ctl_sglist = &ctl_sg_entry;
714 ctl_sglist->addr = io->scsiio.kern_data_ptr;
715 ctl_sglist->len = io->scsiio.kern_data_len;
719 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
720 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
721 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
724 len = icl_pdu_data_segment_length(request);
727 * Make sure the offset, as sent by the initiator, matches the offset
728 * we're supposed to be at in the scatter-gather list.
731 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
732 buffer_offset + len <=
733 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
734 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
735 "expected %zd; dropping connection", buffer_offset,
736 (size_t)io->scsiio.kern_rel_offset +
737 (size_t)io->scsiio.ext_data_filled);
738 ctl_set_data_phase_error(&io->scsiio);
739 cfiscsi_session_terminate(cs);
744 * This is the offset within the PDU data segment, as opposed
745 * to buffer_offset, which is the offset within the task (SCSI
748 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
752 * Iterate over the scatter/gather segments, filling them with data
753 * from the PDU data segment. Note that this can get called multiple
754 * times for one SCSI command; the cdw structure holds state for the
755 * scatter/gather list.
758 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
759 ("cdw->cdw_sg_index >= ctl_sg_count"));
760 if (cdw->cdw_sg_len == 0) {
761 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
762 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
764 KASSERT(off <= len, ("len > off"));
765 copy_len = len - off;
766 if (copy_len > cdw->cdw_sg_len)
767 copy_len = cdw->cdw_sg_len;
769 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
770 cdw->cdw_sg_addr += copy_len;
771 cdw->cdw_sg_len -= copy_len;
773 io->scsiio.ext_data_filled += copy_len;
775 if (cdw->cdw_sg_len == 0) {
777 * End of current segment.
779 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
781 * Last segment in scatter/gather list.
790 * End of PDU payload.
798 * In case of unsolicited data, it's possible that the buffer
799 * provided by CTL is smaller than negotiated FirstBurstLength.
800 * Just ignore the superfluous data; will ask for them with R2T
801 * on next call to cfiscsi_datamove().
803 * This obviously can only happen with SCSI Command PDU.
805 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
806 ISCSI_BHS_OPCODE_SCSI_COMMAND)
809 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
810 "expected %zd; dropping connection",
811 icl_pdu_data_segment_length(request), off);
812 ctl_set_data_phase_error(&io->scsiio);
813 cfiscsi_session_terminate(cs);
817 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
818 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
819 CFISCSI_SESSION_WARN(cs, "got the final packet without "
820 "the F flag; flags = 0x%x; dropping connection",
822 ctl_set_data_phase_error(&io->scsiio);
823 cfiscsi_session_terminate(cs);
827 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
828 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
829 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
830 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
831 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
832 "transmitted size was %zd bytes instead of %d; "
833 "dropping connection",
834 (size_t)io->scsiio.ext_data_filled,
836 ctl_set_data_phase_error(&io->scsiio);
837 cfiscsi_session_terminate(cs);
841 * For SCSI Command PDU, this just means we need to
842 * solicit more data by sending R2T.
848 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
850 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
851 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
861 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
863 struct iscsi_bhs_data_out *bhsdo;
864 struct cfiscsi_session *cs;
865 struct cfiscsi_data_wait *cdw = NULL;
869 cs = PDU_SESSION(request);
870 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
872 CFISCSI_SESSION_LOCK(cs);
873 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
875 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
876 "ttt 0x%x, itt 0x%x",
877 bhsdo->bhsdo_target_transfer_tag,
878 bhsdo->bhsdo_initiator_task_tag,
879 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
881 if (bhsdo->bhsdo_target_transfer_tag ==
882 cdw->cdw_target_transfer_tag)
885 CFISCSI_SESSION_UNLOCK(cs);
887 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
888 "0x%x, not found; dropping connection",
889 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
890 icl_pdu_free(request);
891 cfiscsi_session_terminate(cs);
895 io = cdw->cdw_ctl_io;
896 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
897 ("CTL_FLAG_DATA_IN"));
899 done = cfiscsi_handle_data_segment(request, cdw);
901 CFISCSI_SESSION_LOCK(cs);
902 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
903 CFISCSI_SESSION_UNLOCK(cs);
904 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
905 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
906 uma_zfree(cfiscsi_data_wait_zone, cdw);
908 io->scsiio.be_move_done(io);
910 cfiscsi_datamove_out(io);
913 icl_pdu_free(request);
917 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
919 struct iscsi_bhs_logout_request *bhslr;
920 struct iscsi_bhs_logout_response *bhslr2;
921 struct icl_pdu *response;
922 struct cfiscsi_session *cs;
924 cs = PDU_SESSION(request);
925 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
926 switch (bhslr->bhslr_reason & 0x7f) {
927 case BHSLR_REASON_CLOSE_SESSION:
928 case BHSLR_REASON_CLOSE_CONNECTION:
929 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
930 if (response == NULL) {
931 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
932 icl_pdu_free(request);
933 cfiscsi_session_terminate(cs);
936 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
937 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
938 bhslr2->bhslr_flags = 0x80;
939 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
940 bhslr2->bhslr_initiator_task_tag =
941 bhslr->bhslr_initiator_task_tag;
942 icl_pdu_free(request);
943 cfiscsi_pdu_queue(response);
944 cfiscsi_session_terminate(cs);
946 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
947 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
948 if (response == NULL) {
949 CFISCSI_SESSION_WARN(cs,
950 "failed to allocate memory; dropping connection");
951 icl_pdu_free(request);
952 cfiscsi_session_terminate(cs);
955 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
956 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
957 bhslr2->bhslr_flags = 0x80;
958 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
959 bhslr2->bhslr_initiator_task_tag =
960 bhslr->bhslr_initiator_task_tag;
961 icl_pdu_free(request);
962 cfiscsi_pdu_queue(response);
965 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
966 bhslr->bhslr_reason);
967 icl_pdu_free(request);
968 cfiscsi_session_terminate(cs);
974 cfiscsi_callout(void *context)
977 struct iscsi_bhs_nop_in *bhsni;
978 struct cfiscsi_session *cs;
982 if (cs->cs_terminating)
985 callout_schedule(&cs->cs_callout, 1 * hz);
987 atomic_add_int(&cs->cs_timeout, 1);
989 #ifdef ICL_KERNEL_PROXY
990 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
991 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
992 CFISCSI_SESSION_WARN(cs, "login timed out after "
993 "%d seconds; dropping connection", cs->cs_timeout);
994 cfiscsi_session_terminate(cs);
1000 if (ping_timeout <= 0) {
1002 * Pings are disabled. Don't send NOP-In in this case;
1003 * user might have disabled pings to work around problems
1004 * with certain initiators that can't properly handle
1005 * NOP-In, such as iPXE. Reset the timeout, to avoid
1006 * triggering reconnection, should the user decide to
1013 if (cs->cs_timeout >= ping_timeout) {
1014 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1015 "dropping connection", ping_timeout);
1016 cfiscsi_session_terminate(cs);
1021 * If the ping was reset less than one second ago - which means
1022 * that we've received some PDU during the last second - assume
1023 * the traffic flows correctly and don't bother sending a NOP-Out.
1025 * (It's 2 - one for one second, and one for incrementing is_timeout
1026 * earlier in this routine.)
1028 if (cs->cs_timeout < 2)
1031 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1033 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1036 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1037 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1038 bhsni->bhsni_flags = 0x80;
1039 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1041 cfiscsi_pdu_queue(cp);
1045 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1047 struct cfiscsi_data_wait *cdw;
1049 int error, last, wait;
1051 if (cs->cs_target == NULL)
1052 return; /* No target yet, so nothing to do. */
1053 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1055 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1056 io->io_hdr.io_type = CTL_IO_TASK;
1057 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1058 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1059 io->io_hdr.nexus.targ_target.id = 0;
1060 io->io_hdr.nexus.targ_lun = 0;
1061 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1062 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1063 wait = cs->cs_outstanding_ctl_pdus;
1064 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1065 error = ctl_queue(io);
1066 if (error != CTL_RETVAL_COMPLETE) {
1067 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1068 refcount_release(&cs->cs_outstanding_ctl_pdus);
1072 CFISCSI_SESSION_LOCK(cs);
1073 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1074 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1075 CFISCSI_SESSION_UNLOCK(cs);
1077 * Set nonzero port status; this prevents backends from
1078 * assuming that the data transfer actually succeeded
1079 * and writing uninitialized data to disk.
1081 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1082 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1083 uma_zfree(cfiscsi_data_wait_zone, cdw);
1084 CFISCSI_SESSION_LOCK(cs);
1086 CFISCSI_SESSION_UNLOCK(cs);
1089 * Wait for CTL to terminate all the tasks.
1092 CFISCSI_SESSION_WARN(cs,
1093 "waiting for CTL to terminate %d tasks", wait);
1095 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1096 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1099 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1100 0, "cfiscsi_terminate", hz / 100);
1103 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1107 cfiscsi_maintenance_thread(void *arg)
1109 struct cfiscsi_session *cs;
1114 CFISCSI_SESSION_LOCK(cs);
1115 if (cs->cs_terminating == false)
1116 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1117 CFISCSI_SESSION_UNLOCK(cs);
1119 if (cs->cs_terminating) {
1122 * We used to wait up to 30 seconds to deliver queued
1123 * PDUs to the initiator. We also tried hard to deliver
1124 * SCSI Responses for the aborted PDUs. We don't do
1125 * that anymore. We might need to revisit that.
1127 callout_drain(&cs->cs_callout);
1128 icl_conn_close(cs->cs_conn);
1131 * At this point ICL receive thread is no longer
1132 * running; no new tasks can be queued.
1134 cfiscsi_session_terminate_tasks(cs);
1135 cfiscsi_session_delete(cs);
1139 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1144 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1147 if (cs->cs_terminating)
1149 cs->cs_terminating = true;
1150 cv_signal(&cs->cs_maintenance_cv);
1151 #ifdef ICL_KERNEL_PROXY
1152 cv_signal(&cs->cs_login_cv);
1157 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1159 struct cfiscsi_target *ct;
1163 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1166 name = strdup(cs->cs_initiator_id, M_CTL);
1167 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1169 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1171 cs->cs_ctl_initid = -1;
1174 cs->cs_ctl_initid = i;
1176 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1183 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1187 if (cs->cs_ctl_initid == -1)
1190 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1192 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1195 cs->cs_ctl_initid = -1;
1198 static struct cfiscsi_session *
1199 cfiscsi_session_new(struct cfiscsi_softc *softc)
1201 struct cfiscsi_session *cs;
1204 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1206 CFISCSI_WARN("malloc failed");
1209 cs->cs_ctl_initid = -1;
1211 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1212 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1213 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1214 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1215 #ifdef ICL_KERNEL_PROXY
1216 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1219 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1220 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1221 cs->cs_conn->ic_error = cfiscsi_error_callback;
1222 cs->cs_conn->ic_prv0 = cs;
1224 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1226 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1227 free(cs, M_CFISCSI);
1231 mtx_lock(&softc->lock);
1232 cs->cs_id = ++softc->last_session_id;
1233 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1234 mtx_unlock(&softc->lock);
1237 * Start pinging the initiator.
1239 callout_init(&cs->cs_callout, 1);
1240 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1246 cfiscsi_session_delete(struct cfiscsi_session *cs)
1248 struct cfiscsi_softc *softc;
1250 softc = &cfiscsi_softc;
1252 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1253 ("destroying session with outstanding CTL pdus"));
1254 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1255 ("destroying session with non-empty queue"));
1257 cfiscsi_session_unregister_initiator(cs);
1258 if (cs->cs_target != NULL)
1259 cfiscsi_target_release(cs->cs_target);
1260 icl_conn_close(cs->cs_conn);
1261 icl_conn_free(cs->cs_conn);
1263 mtx_lock(&softc->lock);
1264 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1265 cv_signal(&softc->sessions_cv);
1266 mtx_unlock(&softc->lock);
1268 free(cs, M_CFISCSI);
1274 struct cfiscsi_softc *softc;
1277 softc = &cfiscsi_softc;
1279 bzero(softc, sizeof(*softc));
1280 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1282 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1283 #ifdef ICL_KERNEL_PROXY
1284 cv_init(&softc->accept_cv, "cfiscsi_accept");
1286 TAILQ_INIT(&softc->sessions);
1287 TAILQ_INIT(&softc->targets);
1289 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1290 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1296 #ifdef ICL_KERNEL_PROXY
1298 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1300 struct cfiscsi_session *cs;
1302 cs = cfiscsi_session_new(&cfiscsi_softc);
1304 CFISCSI_WARN("failed to create session");
1308 icl_conn_handoff_sock(cs->cs_conn, so);
1309 cs->cs_initiator_sa = sa;
1310 cs->cs_portal_id = portal_id;
1311 cs->cs_waiting_for_ctld = true;
1312 cv_signal(&cfiscsi_softc.accept_cv);
1317 cfiscsi_online(void *arg)
1319 struct cfiscsi_softc *softc;
1320 struct cfiscsi_target *ct;
1323 ct = (struct cfiscsi_target *)arg;
1324 softc = ct->ct_softc;
1326 mtx_lock(&softc->lock);
1327 if (ct->ct_online) {
1328 mtx_unlock(&softc->lock);
1332 online = softc->online++;
1333 mtx_unlock(&softc->lock);
1337 #ifdef ICL_KERNEL_PROXY
1338 if (softc->listener != NULL)
1339 icl_listen_free(softc->listener);
1340 softc->listener = icl_listen_new(cfiscsi_accept);
1345 cfiscsi_offline(void *arg)
1347 struct cfiscsi_softc *softc;
1348 struct cfiscsi_target *ct;
1349 struct cfiscsi_session *cs;
1352 ct = (struct cfiscsi_target *)arg;
1353 softc = ct->ct_softc;
1355 mtx_lock(&softc->lock);
1356 if (!ct->ct_online) {
1357 mtx_unlock(&softc->lock);
1361 online = --softc->online;
1363 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1364 if (cs->cs_target == ct)
1365 cfiscsi_session_terminate(cs);
1368 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1369 if (cs->cs_target == ct)
1373 cv_wait(&softc->sessions_cv, &softc->lock);
1374 } while (cs != NULL && ct->ct_online == 0);
1375 mtx_unlock(&softc->lock);
1379 #ifdef ICL_KERNEL_PROXY
1380 icl_listen_free(softc->listener);
1381 softc->listener = NULL;
1386 cfiscsi_info(void *arg, struct sbuf *sb)
1388 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1391 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1397 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1399 struct cfiscsi_softc *softc;
1400 struct cfiscsi_session *cs, *cs2;
1401 struct cfiscsi_target *ct;
1402 struct ctl_iscsi_handoff_params *cihp;
1405 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1406 softc = &cfiscsi_softc;
1408 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1409 cihp->initiator_name, cihp->initiator_addr,
1412 ct = cfiscsi_target_find(softc, cihp->target_name);
1414 ci->status = CTL_ISCSI_ERROR;
1415 snprintf(ci->error_str, sizeof(ci->error_str),
1416 "%s: target not found", __func__);
1420 #ifdef ICL_KERNEL_PROXY
1421 if (cihp->socket > 0 && cihp->connection_id > 0) {
1422 snprintf(ci->error_str, sizeof(ci->error_str),
1423 "both socket and connection_id set");
1424 ci->status = CTL_ISCSI_ERROR;
1425 cfiscsi_target_release(ct);
1428 if (cihp->socket == 0) {
1429 mtx_lock(&cfiscsi_softc.lock);
1430 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1431 if (cs->cs_id == cihp->connection_id)
1435 mtx_unlock(&cfiscsi_softc.lock);
1436 snprintf(ci->error_str, sizeof(ci->error_str),
1437 "connection not found");
1438 ci->status = CTL_ISCSI_ERROR;
1439 cfiscsi_target_release(ct);
1442 mtx_unlock(&cfiscsi_softc.lock);
1445 cs = cfiscsi_session_new(softc);
1447 ci->status = CTL_ISCSI_ERROR;
1448 snprintf(ci->error_str, sizeof(ci->error_str),
1449 "%s: cfiscsi_session_new failed", __func__);
1450 cfiscsi_target_release(ct);
1453 #ifdef ICL_KERNEL_PROXY
1458 * First PDU of Full Feature phase has the same CmdSN as the last
1459 * PDU from the Login Phase received from the initiator. Thus,
1462 cs->cs_portal_group_tag = cihp->portal_group_tag;
1463 cs->cs_cmdsn = cihp->cmdsn;
1464 cs->cs_statsn = cihp->statsn;
1465 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1466 cs->cs_max_burst_length = cihp->max_burst_length;
1467 cs->cs_immediate_data = !!cihp->immediate_data;
1468 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1469 cs->cs_conn->ic_header_crc32c = true;
1470 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1471 cs->cs_conn->ic_data_crc32c = true;
1473 strlcpy(cs->cs_initiator_name,
1474 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1475 strlcpy(cs->cs_initiator_addr,
1476 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1477 strlcpy(cs->cs_initiator_alias,
1478 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1479 memcpy(cs->cs_initiator_isid,
1480 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1481 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1482 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1483 cihp->initiator_isid[0], cihp->initiator_isid[1],
1484 cihp->initiator_isid[2], cihp->initiator_isid[3],
1485 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1487 mtx_lock(&softc->lock);
1488 if (ct->ct_online == 0) {
1489 mtx_unlock(&softc->lock);
1490 cfiscsi_session_terminate(cs);
1491 cfiscsi_target_release(ct);
1492 ci->status = CTL_ISCSI_ERROR;
1493 snprintf(ci->error_str, sizeof(ci->error_str),
1494 "%s: port offline", __func__);
1498 mtx_unlock(&softc->lock);
1500 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1502 if (!cs->cs_terminating) {
1503 mtx_lock(&softc->lock);
1504 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1505 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1506 cs->cs_target == cs2->cs_target &&
1507 cs->cs_portal_group_tag == cs2->cs_portal_group_tag &&
1508 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1509 cfiscsi_session_terminate(cs2);
1510 mtx_unlock(&softc->lock);
1511 pause("cfiscsi_reinstate", 1);
1515 mtx_unlock(&softc->lock);
1519 * Register initiator with CTL.
1521 cfiscsi_session_register_initiator(cs);
1523 #ifdef ICL_KERNEL_PROXY
1524 if (cihp->socket > 0) {
1526 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1528 cfiscsi_session_terminate(cs);
1529 refcount_release(&cs->cs_outstanding_ctl_pdus);
1530 ci->status = CTL_ISCSI_ERROR;
1531 snprintf(ci->error_str, sizeof(ci->error_str),
1532 "%s: icl_conn_handoff failed with error %d",
1536 #ifdef ICL_KERNEL_PROXY
1540 #ifdef ICL_KERNEL_PROXY
1541 cs->cs_login_phase = false;
1544 * First PDU of the Full Feature phase has likely already arrived.
1545 * We have to pick it up and execute properly.
1547 if (cs->cs_login_pdu != NULL) {
1548 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1549 cfiscsi_pdu_handle(cs->cs_login_pdu);
1550 cs->cs_login_pdu = NULL;
1554 refcount_release(&cs->cs_outstanding_ctl_pdus);
1555 ci->status = CTL_ISCSI_OK;
1559 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1561 struct ctl_iscsi_list_params *cilp;
1562 struct cfiscsi_session *cs;
1563 struct cfiscsi_softc *softc;
1567 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1568 softc = &cfiscsi_softc;
1570 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1572 ci->status = CTL_ISCSI_ERROR;
1573 snprintf(ci->error_str, sizeof(ci->error_str),
1574 "Unable to allocate %d bytes for iSCSI session list",
1579 sbuf_printf(sb, "<ctlislist>\n");
1580 mtx_lock(&softc->lock);
1581 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1582 #ifdef ICL_KERNEL_PROXY
1583 if (cs->cs_target == NULL)
1586 error = sbuf_printf(sb, "<connection id=\"%d\">"
1587 "<initiator>%s</initiator>"
1588 "<initiator_addr>%s</initiator_addr>"
1589 "<initiator_alias>%s</initiator_alias>"
1590 "<target>%s</target>"
1591 "<target_alias>%s</target_alias>"
1592 "<header_digest>%s</header_digest>"
1593 "<data_digest>%s</data_digest>"
1594 "<max_data_segment_length>%zd</max_data_segment_length>"
1595 "<immediate_data>%d</immediate_data>"
1599 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1600 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1601 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1602 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1603 cs->cs_max_data_segment_length,
1604 cs->cs_immediate_data,
1605 cs->cs_conn->ic_iser);
1609 mtx_unlock(&softc->lock);
1610 error = sbuf_printf(sb, "</ctlislist>\n");
1613 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1614 snprintf(ci->error_str, sizeof(ci->error_str),
1615 "Out of space, %d bytes is too small", cilp->alloc_len);
1620 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1621 cilp->fill_len = sbuf_len(sb) + 1;
1622 ci->status = CTL_ISCSI_OK;
1627 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1629 struct icl_pdu *response;
1630 struct iscsi_bhs_asynchronous_message *bhsam;
1631 struct ctl_iscsi_terminate_params *citp;
1632 struct cfiscsi_session *cs;
1633 struct cfiscsi_softc *softc;
1636 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1637 softc = &cfiscsi_softc;
1639 mtx_lock(&softc->lock);
1640 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1641 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1642 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1643 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1646 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1647 if (response == NULL) {
1649 * Oh well. Just terminate the connection.
1652 bhsam = (struct iscsi_bhs_asynchronous_message *)
1654 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1655 bhsam->bhsam_flags = 0x80;
1656 bhsam->bhsam_0xffffffff = 0xffffffff;
1657 bhsam->bhsam_async_event =
1658 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1659 cfiscsi_pdu_queue(response);
1661 cfiscsi_session_terminate(cs);
1664 mtx_unlock(&softc->lock);
1667 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1668 snprintf(ci->error_str, sizeof(ci->error_str),
1669 "No matching connections found");
1673 ci->status = CTL_ISCSI_OK;
1677 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1679 struct icl_pdu *response;
1680 struct iscsi_bhs_asynchronous_message *bhsam;
1681 struct ctl_iscsi_logout_params *cilp;
1682 struct cfiscsi_session *cs;
1683 struct cfiscsi_softc *softc;
1686 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1687 softc = &cfiscsi_softc;
1689 mtx_lock(&softc->lock);
1690 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1691 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1692 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1693 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1696 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1697 if (response == NULL) {
1698 ci->status = CTL_ISCSI_ERROR;
1699 snprintf(ci->error_str, sizeof(ci->error_str),
1700 "Unable to allocate memory");
1701 mtx_unlock(&softc->lock);
1705 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1706 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1707 bhsam->bhsam_flags = 0x80;
1708 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1709 bhsam->bhsam_parameter3 = htons(10);
1710 cfiscsi_pdu_queue(response);
1713 mtx_unlock(&softc->lock);
1716 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1717 snprintf(ci->error_str, sizeof(ci->error_str),
1718 "No matching connections found");
1722 ci->status = CTL_ISCSI_OK;
1725 #ifdef ICL_KERNEL_PROXY
1727 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1729 struct ctl_iscsi_listen_params *cilp;
1730 struct sockaddr *sa;
1733 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1735 if (cfiscsi_softc.listener == NULL) {
1736 CFISCSI_DEBUG("no listener");
1737 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1738 ci->status = CTL_ISCSI_ERROR;
1742 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1744 CFISCSI_DEBUG("getsockaddr, error %d", error);
1745 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1746 ci->status = CTL_ISCSI_ERROR;
1750 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1751 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1754 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1755 snprintf(ci->error_str, sizeof(ci->error_str),
1756 "icl_listen_add failed, error %d", error);
1757 ci->status = CTL_ISCSI_ERROR;
1761 ci->status = CTL_ISCSI_OK;
1765 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1767 struct ctl_iscsi_accept_params *ciap;
1768 struct cfiscsi_session *cs;
1771 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1773 mtx_lock(&cfiscsi_softc.lock);
1775 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1776 if (cs->cs_waiting_for_ctld)
1781 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1783 mtx_unlock(&cfiscsi_softc.lock);
1784 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1785 ci->status = CTL_ISCSI_ERROR;
1789 mtx_unlock(&cfiscsi_softc.lock);
1791 cs->cs_waiting_for_ctld = false;
1792 cs->cs_login_phase = true;
1794 ciap->connection_id = cs->cs_id;
1795 ciap->portal_id = cs->cs_portal_id;
1796 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1797 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1798 cs->cs_initiator_sa->sa_len);
1800 snprintf(ci->error_str, sizeof(ci->error_str),
1801 "copyout failed with error %d", error);
1802 ci->status = CTL_ISCSI_ERROR;
1806 ci->status = CTL_ISCSI_OK;
1810 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1812 struct ctl_iscsi_send_params *cisp;
1813 struct cfiscsi_session *cs;
1819 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1821 mtx_lock(&cfiscsi_softc.lock);
1822 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1823 if (cs->cs_id == cisp->connection_id)
1827 mtx_unlock(&cfiscsi_softc.lock);
1828 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1829 ci->status = CTL_ISCSI_ERROR;
1832 mtx_unlock(&cfiscsi_softc.lock);
1835 if (cs->cs_login_phase == false)
1839 if (cs->cs_terminating) {
1840 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1841 ci->status = CTL_ISCSI_ERROR;
1845 datalen = cisp->data_segment_len;
1849 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1850 if (datalen > 65535) {
1851 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1852 ci->status = CTL_ISCSI_ERROR;
1856 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1857 error = copyin(cisp->data_segment, data, datalen);
1859 free(data, M_CFISCSI);
1860 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1861 ci->status = CTL_ISCSI_ERROR;
1866 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1867 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1869 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1870 free(data, M_CFISCSI);
1872 CFISCSI_SESSION_LOCK(cs);
1874 CFISCSI_SESSION_UNLOCK(cs);
1875 ci->status = CTL_ISCSI_OK;
1879 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1881 struct ctl_iscsi_receive_params *cirp;
1882 struct cfiscsi_session *cs;
1887 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1889 mtx_lock(&cfiscsi_softc.lock);
1890 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1891 if (cs->cs_id == cirp->connection_id)
1895 mtx_unlock(&cfiscsi_softc.lock);
1896 snprintf(ci->error_str, sizeof(ci->error_str),
1897 "connection not found");
1898 ci->status = CTL_ISCSI_ERROR;
1901 mtx_unlock(&cfiscsi_softc.lock);
1904 if (is->is_login_phase == false)
1908 CFISCSI_SESSION_LOCK(cs);
1909 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
1910 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
1912 CFISCSI_SESSION_UNLOCK(cs);
1913 snprintf(ci->error_str, sizeof(ci->error_str),
1914 "interrupted by signal");
1915 ci->status = CTL_ISCSI_ERROR;
1920 if (cs->cs_terminating) {
1921 CFISCSI_SESSION_UNLOCK(cs);
1922 snprintf(ci->error_str, sizeof(ci->error_str),
1923 "connection terminating");
1924 ci->status = CTL_ISCSI_ERROR;
1927 ip = cs->cs_login_pdu;
1928 cs->cs_login_pdu = NULL;
1929 CFISCSI_SESSION_UNLOCK(cs);
1931 if (ip->ip_data_len > cirp->data_segment_len) {
1933 snprintf(ci->error_str, sizeof(ci->error_str),
1934 "data segment too big");
1935 ci->status = CTL_ISCSI_ERROR;
1939 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1940 if (ip->ip_data_len > 0) {
1941 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1942 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1943 copyout(data, cirp->data_segment, ip->ip_data_len);
1944 free(data, M_CFISCSI);
1948 ci->status = CTL_ISCSI_OK;
1951 #endif /* !ICL_KERNEL_PROXY */
1954 cfiscsi_ioctl_port_create(struct ctl_req *req)
1956 struct cfiscsi_target *ct;
1957 struct ctl_port *port;
1958 const char *target, *alias, *tag;
1959 struct scsi_vpd_id_descriptor *desc;
1961 int retval, len, idlen;
1963 ctl_init_opts(&opts, req->num_args, req->kern_args);
1964 target = ctl_get_opt(&opts, "cfiscsi_target");
1965 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
1966 tag = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
1967 if (target == NULL || tag == NULL) {
1968 req->status = CTL_LUN_ERROR;
1969 snprintf(req->error_str, sizeof(req->error_str),
1970 "Missing required argument");
1971 ctl_free_opts(&opts);
1974 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias);
1976 req->status = CTL_LUN_ERROR;
1977 snprintf(req->error_str, sizeof(req->error_str),
1978 "failed to create target \"%s\"", target);
1979 ctl_free_opts(&opts);
1982 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
1983 req->status = CTL_LUN_ERROR;
1984 snprintf(req->error_str, sizeof(req->error_str),
1985 "target \"%s\" already exists", target);
1986 cfiscsi_target_release(ct);
1987 ctl_free_opts(&opts);
1990 port = &ct->ct_port;
1991 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
1994 port->frontend = &cfiscsi_frontend;
1995 port->port_type = CTL_PORT_ISCSI;
1996 /* XXX KDM what should the real number be here? */
1997 port->num_requested_ctl_io = 4096;
1998 port->port_name = "iscsi";
1999 port->physical_port = strtoul(tag, NULL, 0);
2000 port->virtual_port = ct->ct_target_id;
2001 port->port_online = cfiscsi_online;
2002 port->port_offline = cfiscsi_offline;
2003 port->port_info = cfiscsi_info;
2004 port->onoff_arg = ct;
2005 port->lun_enable = cfiscsi_lun_enable;
2006 port->lun_disable = cfiscsi_lun_disable;
2007 port->lun_map = cfiscsi_lun_map;
2008 port->targ_lun_arg = ct;
2009 port->fe_datamove = cfiscsi_datamove;
2010 port->fe_done = cfiscsi_done;
2012 /* XXX KDM what should we report here? */
2013 /* XXX These should probably be fetched from CTL. */
2014 port->max_targets = 1;
2015 port->max_target_id = 15;
2017 port->options = opts;
2020 /* Generate Port ID. */
2021 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2022 idlen = roundup2(idlen, 4);
2023 len = sizeof(struct scsi_vpd_device_id) + idlen;
2024 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2025 M_CTL, M_WAITOK | M_ZERO);
2026 port->port_devid->len = len;
2027 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2028 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2029 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2030 SVPD_ID_TYPE_SCSI_NAME;
2031 desc->length = idlen;
2032 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x",
2033 target, port->physical_port);
2035 /* Generate Target ID. */
2036 idlen = strlen(target) + 1;
2037 idlen = roundup2(idlen, 4);
2038 len = sizeof(struct scsi_vpd_device_id) + idlen;
2039 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2040 M_CTL, M_WAITOK | M_ZERO);
2041 port->target_devid->len = len;
2042 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2043 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2044 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2045 SVPD_ID_TYPE_SCSI_NAME;
2046 desc->length = idlen;
2047 strlcpy(desc->identifier, target, idlen);
2049 retval = ctl_port_register(port);
2051 ctl_free_opts(&port->options);
2052 cfiscsi_target_release(ct);
2053 free(port->port_devid, M_CFISCSI);
2054 free(port->target_devid, M_CFISCSI);
2055 req->status = CTL_LUN_ERROR;
2056 snprintf(req->error_str, sizeof(req->error_str),
2057 "ctl_frontend_register() failed with error %d", retval);
2061 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2062 req->status = CTL_LUN_OK;
2063 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2064 sizeof(port->targ_port)); //XXX
2068 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2070 struct cfiscsi_target *ct;
2074 ctl_init_opts(&opts, req->num_args, req->kern_args);
2075 target = ctl_get_opt(&opts, "cfiscsi_target");
2076 if (target == NULL) {
2077 ctl_free_opts(&opts);
2078 req->status = CTL_LUN_ERROR;
2079 snprintf(req->error_str, sizeof(req->error_str),
2080 "Missing required argument");
2083 ct = cfiscsi_target_find(&cfiscsi_softc, target);
2085 ctl_free_opts(&opts);
2086 req->status = CTL_LUN_ERROR;
2087 snprintf(req->error_str, sizeof(req->error_str),
2088 "can't find target \"%s\"", target);
2091 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2092 ctl_free_opts(&opts);
2093 req->status = CTL_LUN_ERROR;
2094 snprintf(req->error_str, sizeof(req->error_str),
2095 "target \"%s\" is already dying", target);
2098 ctl_free_opts(&opts);
2100 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2101 ctl_port_offline(&ct->ct_port);
2102 cfiscsi_target_release(ct);
2103 cfiscsi_target_release(ct);
2107 cfiscsi_ioctl(struct cdev *dev,
2108 u_long cmd, caddr_t addr, int flag, struct thread *td)
2110 struct ctl_iscsi *ci;
2111 struct ctl_req *req;
2113 if (cmd == CTL_PORT_REQ) {
2114 req = (struct ctl_req *)addr;
2115 switch (req->reqtype) {
2116 case CTL_REQ_CREATE:
2117 cfiscsi_ioctl_port_create(req);
2119 case CTL_REQ_REMOVE:
2120 cfiscsi_ioctl_port_remove(req);
2123 req->status = CTL_LUN_ERROR;
2124 snprintf(req->error_str, sizeof(req->error_str),
2125 "Unsupported request type %d", req->reqtype);
2130 if (cmd != CTL_ISCSI)
2133 ci = (struct ctl_iscsi *)addr;
2135 case CTL_ISCSI_HANDOFF:
2136 cfiscsi_ioctl_handoff(ci);
2138 case CTL_ISCSI_LIST:
2139 cfiscsi_ioctl_list(ci);
2141 case CTL_ISCSI_TERMINATE:
2142 cfiscsi_ioctl_terminate(ci);
2144 case CTL_ISCSI_LOGOUT:
2145 cfiscsi_ioctl_logout(ci);
2147 #ifdef ICL_KERNEL_PROXY
2148 case CTL_ISCSI_LISTEN:
2149 cfiscsi_ioctl_listen(ci);
2151 case CTL_ISCSI_ACCEPT:
2152 cfiscsi_ioctl_accept(ci);
2154 case CTL_ISCSI_SEND:
2155 cfiscsi_ioctl_send(ci);
2157 case CTL_ISCSI_RECEIVE:
2158 cfiscsi_ioctl_receive(ci);
2161 case CTL_ISCSI_LISTEN:
2162 case CTL_ISCSI_ACCEPT:
2163 case CTL_ISCSI_SEND:
2164 case CTL_ISCSI_RECEIVE:
2165 ci->status = CTL_ISCSI_ERROR;
2166 snprintf(ci->error_str, sizeof(ci->error_str),
2167 "%s: CTL compiled without ICL_KERNEL_PROXY",
2170 #endif /* !ICL_KERNEL_PROXY */
2172 ci->status = CTL_ISCSI_ERROR;
2173 snprintf(ci->error_str, sizeof(ci->error_str),
2174 "%s: invalid iSCSI request type %d", __func__, ci->type);
2182 cfiscsi_target_hold(struct cfiscsi_target *ct)
2185 refcount_acquire(&ct->ct_refcount);
2189 cfiscsi_target_release(struct cfiscsi_target *ct)
2191 struct cfiscsi_softc *softc;
2193 softc = ct->ct_softc;
2194 mtx_lock(&softc->lock);
2195 if (refcount_release(&ct->ct_refcount)) {
2196 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2197 mtx_unlock(&softc->lock);
2198 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2199 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2200 if (ctl_port_deregister(&ct->ct_port) != 0)
2201 printf("%s: ctl_port_deregister() failed\n",
2204 free(ct, M_CFISCSI);
2208 mtx_unlock(&softc->lock);
2211 static struct cfiscsi_target *
2212 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2214 struct cfiscsi_target *ct;
2216 mtx_lock(&softc->lock);
2217 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2218 if (strcmp(name, ct->ct_name) != 0 ||
2219 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2221 cfiscsi_target_hold(ct);
2222 mtx_unlock(&softc->lock);
2225 mtx_unlock(&softc->lock);
2230 static struct cfiscsi_target *
2231 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2234 struct cfiscsi_target *ct, *newct;
2237 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2240 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2242 mtx_lock(&softc->lock);
2243 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2244 if (strcmp(name, ct->ct_name) != 0 ||
2245 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2247 cfiscsi_target_hold(ct);
2248 mtx_unlock(&softc->lock);
2249 free(newct, M_CFISCSI);
2253 for (i = 0; i < CTL_MAX_LUNS; i++)
2254 newct->ct_luns[i] = UINT32_MAX;
2256 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2258 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2259 refcount_init(&newct->ct_refcount, 1);
2260 newct->ct_softc = softc;
2261 if (TAILQ_EMPTY(&softc->targets))
2262 softc->last_target_id = 0;
2263 newct->ct_target_id = ++softc->last_target_id;
2264 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2265 mtx_unlock(&softc->lock);
2271 * Takes LUN from the target space and returns LUN from the CTL space.
2274 cfiscsi_lun_map(void *arg, uint32_t lun)
2276 struct cfiscsi_target *ct = arg;
2278 if (lun >= CTL_MAX_LUNS) {
2279 CFISCSI_DEBUG("requested lun number %d is higher "
2280 "than maximum %d", lun, CTL_MAX_LUNS - 1);
2281 return (UINT32_MAX);
2283 return (ct->ct_luns[lun]);
2287 cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2288 unsigned long lun_id, unsigned long ctl_lun_id)
2291 if (lun_id >= CTL_MAX_LUNS) {
2292 CFISCSI_WARN("requested lun number %ld is higher "
2293 "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2297 if (ct->ct_luns[lun_id] < CTL_MAX_LUNS) {
2299 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2300 * when the LUN is created, and a second time just before
2301 * the port is brought online; don't emit warnings
2304 if (ct->ct_luns[lun_id] == ctl_lun_id)
2306 CFISCSI_WARN("lun %ld already allocated", lun_id);
2311 CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2312 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2315 ct->ct_luns[lun_id] = ctl_lun_id;
2321 cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2323 struct cfiscsi_softc *softc;
2324 struct cfiscsi_target *ct;
2325 const char *target = NULL;
2326 const char *lun = NULL;
2329 ct = (struct cfiscsi_target *)arg;
2330 softc = ct->ct_softc;
2332 target = ctl_get_opt(&control_softc->ctl_luns[lun_id]->be_lun->options,
2334 lun = ctl_get_opt(&control_softc->ctl_luns[lun_id]->be_lun->options,
2337 if (target == NULL && lun == NULL)
2340 if (target == NULL || lun == NULL) {
2341 CFISCSI_WARN("lun added with cfiscsi_target, but without "
2342 "cfiscsi_lun, or the other way around; ignoring");
2346 if (strcmp(target, ct->ct_name) != 0)
2349 tmp = strtoul(lun, NULL, 10);
2350 cfiscsi_target_set_lun(ct, tmp, lun_id);
2355 cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2357 struct cfiscsi_softc *softc;
2358 struct cfiscsi_target *ct;
2361 ct = (struct cfiscsi_target *)arg;
2362 softc = ct->ct_softc;
2364 mtx_lock(&softc->lock);
2365 for (i = 0; i < CTL_MAX_LUNS; i++) {
2366 if (ct->ct_luns[i] != lun_id)
2368 ct->ct_luns[i] = UINT32_MAX;
2371 mtx_unlock(&softc->lock);
2376 cfiscsi_datamove_in(union ctl_io *io)
2378 struct cfiscsi_session *cs;
2379 struct icl_pdu *request, *response;
2380 const struct iscsi_bhs_scsi_command *bhssc;
2381 struct iscsi_bhs_data_in *bhsdi;
2382 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2383 size_t len, expected_len, sg_len, buffer_offset;
2384 const char *sg_addr;
2385 int ctl_sg_count, error, i;
2387 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2388 cs = PDU_SESSION(request);
2390 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2391 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2392 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2393 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2395 if (io->scsiio.kern_sg_entries > 0) {
2396 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2397 ctl_sg_count = io->scsiio.kern_sg_entries;
2399 ctl_sglist = &ctl_sg_entry;
2400 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2401 ctl_sglist->len = io->scsiio.kern_data_len;
2406 * This is the total amount of data to be transferred within the current
2407 * SCSI command. We need to record it so that we can properly report
2408 * underflow/underflow.
2410 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2413 * This is the offset within the current SCSI command; for the first
2414 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2415 * it will be the sum of lengths of previous ones.
2417 buffer_offset = io->scsiio.kern_rel_offset;
2420 * This is the transfer length expected by the initiator. In theory,
2421 * it could be different from the correct amount of data from the SCSI
2422 * point of view, even if that doesn't make any sense.
2424 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2426 if (expected_len != io->scsiio.kern_total_len) {
2427 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2428 "actual length %zd", expected_len,
2429 (size_t)io->scsiio.kern_total_len);
2433 if (buffer_offset >= expected_len) {
2435 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2436 "already sent the expected len", buffer_offset);
2438 io->scsiio.be_move_done(io);
2448 if (response == NULL) {
2449 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2450 if (response == NULL) {
2451 CFISCSI_SESSION_WARN(cs, "failed to "
2452 "allocate memory; dropping connection");
2453 ctl_set_busy(&io->scsiio);
2454 io->scsiio.be_move_done(io);
2455 cfiscsi_session_terminate(cs);
2458 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2459 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2460 bhsdi->bhsdi_initiator_task_tag =
2461 bhssc->bhssc_initiator_task_tag;
2462 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2463 PDU_EXPDATASN(request)++;
2464 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2467 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2469 sg_addr = ctl_sglist[i].addr;
2470 sg_len = ctl_sglist[i].len;
2471 KASSERT(sg_len > 0, ("sg_len <= 0"));
2477 * Truncate to maximum data segment length.
2479 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2480 ("ip_data_len %zd >= max_data_segment_length %zd",
2481 response->ip_data_len, cs->cs_max_data_segment_length));
2482 if (response->ip_data_len + len >
2483 cs->cs_max_data_segment_length) {
2484 len = cs->cs_max_data_segment_length -
2485 response->ip_data_len;
2486 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2491 * Truncate to expected data transfer length.
2493 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2494 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2495 buffer_offset, response->ip_data_len, expected_len));
2496 if (buffer_offset + response->ip_data_len + len > expected_len) {
2497 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2498 "to expected data transfer length %zd",
2499 buffer_offset + response->ip_data_len + len, expected_len);
2500 len = expected_len - (buffer_offset + response->ip_data_len);
2501 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2505 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2507 CFISCSI_SESSION_WARN(cs, "failed to "
2508 "allocate memory; dropping connection");
2509 icl_pdu_free(response);
2510 ctl_set_busy(&io->scsiio);
2511 io->scsiio.be_move_done(io);
2512 cfiscsi_session_terminate(cs);
2518 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2519 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2520 buffer_offset, response->ip_data_len, expected_len));
2521 if (buffer_offset + response->ip_data_len == expected_len) {
2523 * Already have the amount of data the initiator wanted.
2530 * End of scatter-gather segment;
2531 * proceed to the next one...
2533 if (i == ctl_sg_count - 1) {
2535 * ... unless this was the last one.
2542 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2544 * Can't stuff more data into the current PDU;
2545 * queue it. Note that's not enough to check
2546 * for kern_data_resid == 0 instead; there
2547 * may be several Data-In PDUs for the final
2548 * call to cfiscsi_datamove(), and we want
2549 * to set the F flag only on the last of them.
2551 buffer_offset += response->ip_data_len;
2552 if (buffer_offset == io->scsiio.kern_total_len ||
2553 buffer_offset == expected_len) {
2554 buffer_offset -= response->ip_data_len;
2557 cfiscsi_pdu_queue(response);
2562 if (response != NULL) {
2563 buffer_offset += response->ip_data_len;
2564 if (buffer_offset == io->scsiio.kern_total_len ||
2565 buffer_offset == expected_len) {
2566 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2567 if (io->io_hdr.status == CTL_SUCCESS) {
2568 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2569 if (PDU_TOTAL_TRANSFER_LEN(request) <
2570 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2571 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2572 bhsdi->bhsdi_residual_count =
2573 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2574 PDU_TOTAL_TRANSFER_LEN(request));
2575 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2576 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2577 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2578 bhsdi->bhsdi_residual_count =
2579 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2580 ntohl(bhssc->bhssc_expected_data_transfer_length));
2582 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2583 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2586 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2587 cfiscsi_pdu_queue(response);
2590 io->scsiio.be_move_done(io);
2594 cfiscsi_datamove_out(union ctl_io *io)
2596 struct cfiscsi_session *cs;
2597 struct icl_pdu *request, *response;
2598 const struct iscsi_bhs_scsi_command *bhssc;
2599 struct iscsi_bhs_r2t *bhsr2t;
2600 struct cfiscsi_data_wait *cdw;
2601 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2602 uint32_t expected_len, r2t_off, r2t_len;
2603 uint32_t target_transfer_tag;
2606 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2607 cs = PDU_SESSION(request);
2609 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2610 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2611 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2612 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2615 * We need to record it so that we can properly report
2616 * underflow/underflow.
2618 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2621 * Report write underflow as error since CTL and backends don't
2622 * really support it, and SCSI does not tell how to do it right.
2624 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2625 if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len >
2627 io->scsiio.io_hdr.port_status = 43;
2628 io->scsiio.be_move_done(io);
2632 target_transfer_tag =
2633 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2636 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2637 "task tag 0x%x, target transfer tag 0x%x",
2638 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2640 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2642 CFISCSI_SESSION_WARN(cs, "failed to "
2643 "allocate memory; dropping connection");
2644 ctl_set_busy(&io->scsiio);
2645 io->scsiio.be_move_done(io);
2646 cfiscsi_session_terminate(cs);
2649 cdw->cdw_ctl_io = io;
2650 cdw->cdw_target_transfer_tag = target_transfer_tag;
2651 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2652 cdw->cdw_r2t_end = io->scsiio.kern_data_len;
2654 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2655 if (io->scsiio.kern_sg_entries > 0) {
2656 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2658 ctl_sglist = &ctl_sg_entry;
2659 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2660 ctl_sglist->len = io->scsiio.kern_data_len;
2662 cdw->cdw_sg_index = 0;
2663 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2664 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2665 r2t_off = io->scsiio.ext_data_filled;
2666 while (r2t_off > 0) {
2667 if (r2t_off >= cdw->cdw_sg_len) {
2668 r2t_off -= cdw->cdw_sg_len;
2669 cdw->cdw_sg_index++;
2670 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2671 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2674 cdw->cdw_sg_addr += r2t_off;
2675 cdw->cdw_sg_len -= r2t_off;
2679 if (cs->cs_immediate_data &&
2680 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2681 icl_pdu_data_segment_length(request)) {
2682 done = cfiscsi_handle_data_segment(request, cdw);
2684 uma_zfree(cfiscsi_data_wait_zone, cdw);
2685 io->scsiio.be_move_done(io);
2690 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2691 r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled,
2692 cs->cs_max_burst_length);
2693 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2695 CFISCSI_SESSION_LOCK(cs);
2696 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2697 CFISCSI_SESSION_UNLOCK(cs);
2700 * XXX: We should limit the number of outstanding R2T PDUs
2701 * per task to MaxOutstandingR2T.
2703 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2704 if (response == NULL) {
2705 CFISCSI_SESSION_WARN(cs, "failed to "
2706 "allocate memory; dropping connection");
2707 ctl_set_busy(&io->scsiio);
2708 io->scsiio.be_move_done(io);
2709 cfiscsi_session_terminate(cs);
2712 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2713 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2714 bhsr2t->bhsr2t_flags = 0x80;
2715 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2716 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2717 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2719 * XXX: Here we assume that cfiscsi_datamove() won't ever
2720 * be running concurrently on several CPUs for a given
2723 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2724 PDU_R2TSN(request)++;
2726 * This is the offset within the current SCSI command;
2727 * i.e. for the first call of datamove(), it will be 0,
2728 * and for subsequent ones it will be the sum of lengths
2731 * The ext_data_filled is to account for unsolicited
2732 * (immediate) data that might have already arrived.
2734 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2736 * This is the total length (sum of S/G lengths) this call
2737 * to cfiscsi_datamove() is supposed to handle, limited by
2740 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2741 cfiscsi_pdu_queue(response);
2745 cfiscsi_datamove(union ctl_io *io)
2748 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2749 cfiscsi_datamove_in(io);
2751 /* We hadn't received anything during this datamove yet. */
2752 io->scsiio.ext_data_filled = 0;
2753 cfiscsi_datamove_out(io);
2758 cfiscsi_scsi_command_done(union ctl_io *io)
2760 struct icl_pdu *request, *response;
2761 struct iscsi_bhs_scsi_command *bhssc;
2762 struct iscsi_bhs_scsi_response *bhssr;
2764 struct cfiscsi_data_wait *cdw;
2766 struct cfiscsi_session *cs;
2767 uint16_t sense_length;
2769 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2770 cs = PDU_SESSION(request);
2771 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2772 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2773 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2774 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2776 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2777 // bhssc->bhssc_initiator_task_tag);
2780 CFISCSI_SESSION_LOCK(cs);
2781 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2782 KASSERT(bhssc->bhssc_initiator_task_tag !=
2783 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2784 CFISCSI_SESSION_UNLOCK(cs);
2788 * Do not return status for aborted commands.
2789 * There are exceptions, but none supported by CTL yet.
2791 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2792 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2793 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2795 icl_pdu_free(request);
2799 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2800 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2801 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2802 bhssr->bhssr_flags = 0x80;
2804 * XXX: We don't deal with bidirectional under/overflows;
2805 * does anything actually support those?
2807 if (PDU_TOTAL_TRANSFER_LEN(request) <
2808 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2809 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2810 bhssr->bhssr_residual_count =
2811 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2812 PDU_TOTAL_TRANSFER_LEN(request));
2813 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2814 // ntohl(bhssr->bhssr_residual_count));
2815 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2816 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2817 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2818 bhssr->bhssr_residual_count =
2819 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2820 ntohl(bhssc->bhssc_expected_data_transfer_length));
2821 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2822 // ntohl(bhssr->bhssr_residual_count));
2824 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2825 bhssr->bhssr_status = io->scsiio.scsi_status;
2826 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2827 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2829 if (io->scsiio.sense_len > 0) {
2831 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2832 io->scsiio.sense_len);
2834 sense_length = htons(io->scsiio.sense_len);
2835 icl_pdu_append_data(response,
2836 &sense_length, sizeof(sense_length), M_WAITOK);
2837 icl_pdu_append_data(response,
2838 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2842 icl_pdu_free(request);
2843 cfiscsi_pdu_queue(response);
2847 cfiscsi_task_management_done(union ctl_io *io)
2849 struct icl_pdu *request, *response;
2850 struct iscsi_bhs_task_management_request *bhstmr;
2851 struct iscsi_bhs_task_management_response *bhstmr2;
2852 struct cfiscsi_data_wait *cdw, *tmpcdw;
2853 struct cfiscsi_session *cs;
2855 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2856 cs = PDU_SESSION(request);
2857 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2858 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2859 ISCSI_BHS_OPCODE_TASK_REQUEST,
2860 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2863 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2864 bhstmr->bhstmr_initiator_task_tag,
2865 bhstmr->bhstmr_referenced_task_tag);
2868 if ((bhstmr->bhstmr_function & ~0x80) ==
2869 BHSTMR_FUNCTION_ABORT_TASK) {
2871 * Make sure we no longer wait for Data-Out for this command.
2873 CFISCSI_SESSION_LOCK(cs);
2874 TAILQ_FOREACH_SAFE(cdw,
2875 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2876 if (bhstmr->bhstmr_referenced_task_tag !=
2877 cdw->cdw_initiator_task_tag)
2881 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2882 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2884 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2886 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2887 uma_zfree(cfiscsi_data_wait_zone, cdw);
2889 CFISCSI_SESSION_UNLOCK(cs);
2892 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2893 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2895 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2896 bhstmr2->bhstmr_flags = 0x80;
2897 if (io->io_hdr.status == CTL_SUCCESS) {
2898 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2901 * XXX: How to figure out what exactly went wrong? iSCSI spec
2902 * expects us to provide detailed error, e.g. "Task does
2903 * not exist" or "LUN does not exist".
2905 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2906 bhstmr2->bhstmr_response =
2907 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2909 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2912 icl_pdu_free(request);
2913 cfiscsi_pdu_queue(response);
2917 cfiscsi_done(union ctl_io *io)
2919 struct icl_pdu *request;
2920 struct cfiscsi_session *cs;
2922 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2923 ("invalid CTL status %#x", io->io_hdr.status));
2925 if (io->io_hdr.io_type == CTL_IO_TASK &&
2926 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2928 * Implicit task termination has just completed; nothing to do.
2930 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2931 cs->cs_tasks_aborted = true;
2932 refcount_release(&cs->cs_outstanding_ctl_pdus);
2933 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2938 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2939 cs = PDU_SESSION(request);
2940 refcount_release(&cs->cs_outstanding_ctl_pdus);
2942 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2943 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2944 cfiscsi_scsi_command_done(io);
2946 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2947 cfiscsi_task_management_done(io);
2950 panic("cfiscsi_done called with wrong opcode 0x%x",
2951 request->ip_bhs->bhs_opcode);