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_debug.h>
65 #include <cam/ctl/ctl_ha.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_private.h>
69 #include <dev/iscsi/icl.h>
70 #include <dev/iscsi/icl_wrappers.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_ioctl(struct cdev *dev,
150 u_long cmd, caddr_t addr, int flag, struct thread *td);
151 static void cfiscsi_datamove(union ctl_io *io);
152 static void cfiscsi_datamove_in(union ctl_io *io);
153 static void cfiscsi_datamove_out(union ctl_io *io);
154 static void cfiscsi_done(union ctl_io *io);
155 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
156 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
157 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
158 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
159 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
160 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
161 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
162 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
163 struct cfiscsi_session *cs, union ctl_io *io,
164 uint32_t initiator_task_tag,
165 uint32_t *target_transfer_tagp);
166 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
167 struct cfiscsi_data_wait *cdw);
168 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
169 *softc, const char *name, uint16_t tag);
170 static struct cfiscsi_target *cfiscsi_target_find_or_create(
171 struct cfiscsi_softc *softc, const char *name, const char *alias,
173 static void cfiscsi_target_release(struct cfiscsi_target *ct);
174 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
176 static struct cfiscsi_softc cfiscsi_softc;
177 extern struct ctl_softc *control_softc;
179 static struct ctl_frontend cfiscsi_frontend =
182 .init = cfiscsi_init,
183 .ioctl = cfiscsi_ioctl,
185 CTL_FRONTEND_DECLARE(ctlcfiscsi, cfiscsi_frontend);
186 MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
188 static struct icl_pdu *
189 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
192 return (icl_pdu_new(request->ip_conn, flags));
196 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
198 const struct iscsi_bhs_scsi_command *bhssc;
199 struct cfiscsi_session *cs;
200 uint32_t cmdsn, expstatsn;
202 cs = PDU_SESSION(request);
205 * Every incoming PDU - not just NOP-Out - resets the ping timer.
206 * The purpose of the timeout is to reset the connection when it stalls;
207 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
215 * Data-Out PDUs don't contain CmdSN.
217 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
218 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
222 * We're only using fields common for all the request
223 * (initiator -> target) PDUs.
225 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
226 cmdsn = ntohl(bhssc->bhssc_cmdsn);
227 expstatsn = ntohl(bhssc->bhssc_expstatsn);
229 CFISCSI_SESSION_LOCK(cs);
231 if (expstatsn != cs->cs_statsn) {
232 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
233 "while current StatSN is %d", expstatsn,
238 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
240 * The target MUST silently ignore any non-immediate command
241 * outside of this range.
243 if (ISCSI_SNLT(cmdsn, cs->cs_cmdsn) ||
244 ISCSI_SNGT(cmdsn, cs->cs_cmdsn + maxcmdsn_delta)) {
245 CFISCSI_SESSION_UNLOCK(cs);
246 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
247 "while expected %u", cmdsn, cs->cs_cmdsn);
252 * We don't support multiple connections now, so any
253 * discontinuity in CmdSN means lost PDUs. Since we don't
254 * support PDU retransmission -- terminate the connection.
256 if (cmdsn != cs->cs_cmdsn) {
257 CFISCSI_SESSION_UNLOCK(cs);
258 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
259 "while expected %u; dropping connection",
260 cmdsn, cs->cs_cmdsn);
261 cfiscsi_session_terminate(cs);
267 CFISCSI_SESSION_UNLOCK(cs);
273 cfiscsi_pdu_handle(struct icl_pdu *request)
275 struct cfiscsi_session *cs;
278 cs = PDU_SESSION(request);
280 ignore = cfiscsi_pdu_update_cmdsn(request);
282 icl_pdu_free(request);
287 * Handle the PDU; this includes e.g. receiving the remaining
288 * part of PDU and submitting the SCSI command to CTL
289 * or queueing a reply. The handling routine is responsible
290 * for freeing the PDU when it's no longer needed.
292 switch (request->ip_bhs->bhs_opcode &
293 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
294 case ISCSI_BHS_OPCODE_NOP_OUT:
295 cfiscsi_pdu_handle_nop_out(request);
297 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
298 cfiscsi_pdu_handle_scsi_command(request);
300 case ISCSI_BHS_OPCODE_TASK_REQUEST:
301 cfiscsi_pdu_handle_task_request(request);
303 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
304 cfiscsi_pdu_handle_data_out(request);
306 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
307 cfiscsi_pdu_handle_logout_request(request);
310 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
311 "opcode 0x%x; dropping connection",
312 request->ip_bhs->bhs_opcode);
313 icl_pdu_free(request);
314 cfiscsi_session_terminate(cs);
320 cfiscsi_receive_callback(struct icl_pdu *request)
322 struct cfiscsi_session *cs;
324 cs = PDU_SESSION(request);
326 #ifdef ICL_KERNEL_PROXY
327 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
328 if (cs->cs_login_pdu == NULL)
329 cs->cs_login_pdu = request;
331 icl_pdu_free(request);
332 cv_signal(&cs->cs_login_cv);
337 cfiscsi_pdu_handle(request);
341 cfiscsi_error_callback(struct icl_conn *ic)
343 struct cfiscsi_session *cs;
345 cs = CONN_SESSION(ic);
347 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
348 cfiscsi_session_terminate(cs);
352 cfiscsi_pdu_prepare(struct icl_pdu *response)
354 struct cfiscsi_session *cs;
355 struct iscsi_bhs_scsi_response *bhssr;
356 bool advance_statsn = true;
358 cs = PDU_SESSION(response);
360 CFISCSI_SESSION_LOCK_ASSERT(cs);
363 * We're only using fields common for all the response
364 * (target -> initiator) PDUs.
366 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
369 * 10.8.3: "The StatSN for this connection is not advanced
370 * after this PDU is sent."
372 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
373 advance_statsn = false;
376 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
377 * StatSN for the connection is not advanced after this PDU is sent."
379 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
380 bhssr->bhssr_initiator_task_tag == 0xffffffff)
381 advance_statsn = false;
384 * See the comment below - StatSN is not meaningful and must
387 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
388 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
389 advance_statsn = false;
392 * 10.7.3: "The fields StatSN, Status, and Residual Count
393 * only have meaningful content if the S bit is set to 1."
395 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
396 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
397 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
398 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
399 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
408 cfiscsi_pdu_queue(struct icl_pdu *response)
410 struct cfiscsi_session *cs;
412 cs = PDU_SESSION(response);
414 CFISCSI_SESSION_LOCK(cs);
415 cfiscsi_pdu_prepare(response);
416 icl_pdu_queue(response);
417 CFISCSI_SESSION_UNLOCK(cs);
421 cfiscsi_decode_lun(uint64_t encoded)
427 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
428 * but is in fact an evil, multidimensional structure defined
429 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
431 memcpy(lun, &encoded, sizeof(lun));
432 switch (lun[0] & 0xC0) {
434 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
435 lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
436 CFISCSI_WARN("malformed LUN "
437 "(peripheral device addressing method): 0x%jx",
445 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
446 lun[6] != 0 || lun[7] != 0) {
447 CFISCSI_WARN("malformed LUN "
448 "(flat address space addressing method): 0x%jx",
453 result = ((lun[0] & 0x3f) << 8) + lun[1];
456 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
457 lun[6] != 0 || lun[7] != 0) {
458 CFISCSI_WARN("malformed LUN (extended flat "
459 "address space addressing method): 0x%jx",
464 result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
466 CFISCSI_WARN("unsupported LUN format 0x%jx",
476 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
478 struct cfiscsi_session *cs;
479 struct iscsi_bhs_nop_out *bhsno;
480 struct iscsi_bhs_nop_in *bhsni;
481 struct icl_pdu *response;
486 cs = PDU_SESSION(request);
487 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
489 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
491 * Nothing to do, iscsi_pdu_update_statsn() already
492 * zeroed the timeout.
494 icl_pdu_free(request);
498 datasize = icl_pdu_data_segment_length(request);
500 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
502 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
503 "dropping connection");
504 icl_pdu_free(request);
505 cfiscsi_session_terminate(cs);
508 icl_pdu_get_data(request, 0, data, datasize);
511 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
512 if (response == NULL) {
513 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
514 "droppping connection");
515 free(data, M_CFISCSI);
516 icl_pdu_free(request);
517 cfiscsi_session_terminate(cs);
520 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
521 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
522 bhsni->bhsni_flags = 0x80;
523 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
524 bhsni->bhsni_target_transfer_tag = 0xffffffff;
526 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
528 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
529 "dropping connection");
530 free(data, M_CFISCSI);
531 icl_pdu_free(request);
532 icl_pdu_free(response);
533 cfiscsi_session_terminate(cs);
536 free(data, M_CFISCSI);
539 icl_pdu_free(request);
540 cfiscsi_pdu_queue(response);
544 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
546 struct iscsi_bhs_scsi_command *bhssc;
547 struct cfiscsi_session *cs;
551 cs = PDU_SESSION(request);
552 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
553 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
554 // bhssc->bhssc_initiator_task_tag);
556 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
557 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
558 "ImmediateData=No; dropping connection");
559 icl_pdu_free(request);
560 cfiscsi_session_terminate(cs);
563 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
565 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
566 io->io_hdr.io_type = CTL_IO_SCSI;
567 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
568 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
569 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
570 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
571 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
572 case BHSSC_FLAGS_ATTR_UNTAGGED:
573 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
575 case BHSSC_FLAGS_ATTR_SIMPLE:
576 io->scsiio.tag_type = CTL_TAG_SIMPLE;
578 case BHSSC_FLAGS_ATTR_ORDERED:
579 io->scsiio.tag_type = CTL_TAG_ORDERED;
581 case BHSSC_FLAGS_ATTR_HOQ:
582 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
584 case BHSSC_FLAGS_ATTR_ACA:
585 io->scsiio.tag_type = CTL_TAG_ACA;
588 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
589 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
590 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
593 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
594 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
595 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
596 error = ctl_queue(io);
597 if (error != CTL_RETVAL_COMPLETE) {
598 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
599 "dropping connection", error);
601 refcount_release(&cs->cs_outstanding_ctl_pdus);
602 icl_pdu_free(request);
603 cfiscsi_session_terminate(cs);
608 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
610 struct iscsi_bhs_task_management_request *bhstmr;
611 struct iscsi_bhs_task_management_response *bhstmr2;
612 struct icl_pdu *response;
613 struct cfiscsi_session *cs;
617 cs = PDU_SESSION(request);
618 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
619 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
621 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
622 io->io_hdr.io_type = CTL_IO_TASK;
623 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
624 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
625 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
626 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
628 switch (bhstmr->bhstmr_function & ~0x80) {
629 case BHSTMR_FUNCTION_ABORT_TASK:
631 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
633 io->taskio.task_action = CTL_TASK_ABORT_TASK;
634 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
636 case BHSTMR_FUNCTION_ABORT_TASK_SET:
638 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
640 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
642 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
644 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
646 io->taskio.task_action = CTL_TASK_LUN_RESET;
648 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
650 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
652 io->taskio.task_action = CTL_TASK_TARGET_RESET;
655 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
656 bhstmr->bhstmr_function & ~0x80);
659 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
660 if (response == NULL) {
661 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
662 "dropping connection");
663 icl_pdu_free(request);
664 cfiscsi_session_terminate(cs);
667 bhstmr2 = (struct iscsi_bhs_task_management_response *)
669 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
670 bhstmr2->bhstmr_flags = 0x80;
671 bhstmr2->bhstmr_response =
672 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
673 bhstmr2->bhstmr_initiator_task_tag =
674 bhstmr->bhstmr_initiator_task_tag;
675 icl_pdu_free(request);
676 cfiscsi_pdu_queue(response);
680 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
681 error = ctl_queue(io);
682 if (error != CTL_RETVAL_COMPLETE) {
683 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
684 "dropping connection", error);
686 refcount_release(&cs->cs_outstanding_ctl_pdus);
687 icl_pdu_free(request);
688 cfiscsi_session_terminate(cs);
693 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
695 struct iscsi_bhs_data_out *bhsdo;
696 struct cfiscsi_session *cs;
697 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
698 size_t copy_len, len, off, buffer_offset;
702 cs = PDU_SESSION(request);
704 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
705 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
706 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
707 ISCSI_BHS_OPCODE_SCSI_COMMAND,
708 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
711 * We're only using fields common for Data-Out and SCSI Command PDUs.
713 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
715 io = cdw->cdw_ctl_io;
716 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
717 ("CTL_FLAG_DATA_IN"));
720 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
721 request->ip_data_len, io->scsiio.kern_total_len);
724 if (io->scsiio.kern_sg_entries > 0) {
725 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
726 ctl_sg_count = io->scsiio.kern_sg_entries;
728 ctl_sglist = &ctl_sg_entry;
729 ctl_sglist->addr = io->scsiio.kern_data_ptr;
730 ctl_sglist->len = io->scsiio.kern_data_len;
734 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
735 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
736 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
739 len = icl_pdu_data_segment_length(request);
742 * Make sure the offset, as sent by the initiator, matches the offset
743 * we're supposed to be at in the scatter-gather list.
746 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
747 buffer_offset + len <=
748 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
749 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
750 "expected %zd; dropping connection", buffer_offset,
751 (size_t)io->scsiio.kern_rel_offset +
752 (size_t)io->scsiio.ext_data_filled);
753 ctl_set_data_phase_error(&io->scsiio);
754 cfiscsi_session_terminate(cs);
759 * This is the offset within the PDU data segment, as opposed
760 * to buffer_offset, which is the offset within the task (SCSI
763 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
767 * Iterate over the scatter/gather segments, filling them with data
768 * from the PDU data segment. Note that this can get called multiple
769 * times for one SCSI command; the cdw structure holds state for the
770 * scatter/gather list.
773 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
774 ("cdw->cdw_sg_index >= ctl_sg_count"));
775 if (cdw->cdw_sg_len == 0) {
776 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
777 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
779 KASSERT(off <= len, ("len > off"));
780 copy_len = len - off;
781 if (copy_len > cdw->cdw_sg_len)
782 copy_len = cdw->cdw_sg_len;
784 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
785 cdw->cdw_sg_addr += copy_len;
786 cdw->cdw_sg_len -= copy_len;
788 io->scsiio.ext_data_filled += copy_len;
790 if (cdw->cdw_sg_len == 0) {
792 * End of current segment.
794 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
796 * Last segment in scatter/gather list.
805 * End of PDU payload.
813 * In case of unsolicited data, it's possible that the buffer
814 * provided by CTL is smaller than negotiated FirstBurstLength.
815 * Just ignore the superfluous data; will ask for them with R2T
816 * on next call to cfiscsi_datamove().
818 * This obviously can only happen with SCSI Command PDU.
820 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
821 ISCSI_BHS_OPCODE_SCSI_COMMAND)
824 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
825 "expected %zd; dropping connection",
826 icl_pdu_data_segment_length(request), off);
827 ctl_set_data_phase_error(&io->scsiio);
828 cfiscsi_session_terminate(cs);
832 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
833 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
834 CFISCSI_SESSION_WARN(cs, "got the final packet without "
835 "the F flag; flags = 0x%x; dropping connection",
837 ctl_set_data_phase_error(&io->scsiio);
838 cfiscsi_session_terminate(cs);
842 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
843 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
844 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
845 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
846 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
847 "transmitted size was %zd bytes instead of %d; "
848 "dropping connection",
849 (size_t)io->scsiio.ext_data_filled,
851 ctl_set_data_phase_error(&io->scsiio);
852 cfiscsi_session_terminate(cs);
856 * For SCSI Command PDU, this just means we need to
857 * solicit more data by sending R2T.
863 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
865 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
866 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
876 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
878 struct iscsi_bhs_data_out *bhsdo;
879 struct cfiscsi_session *cs;
880 struct cfiscsi_data_wait *cdw = NULL;
884 cs = PDU_SESSION(request);
885 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
887 CFISCSI_SESSION_LOCK(cs);
888 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
890 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
891 "ttt 0x%x, itt 0x%x",
892 bhsdo->bhsdo_target_transfer_tag,
893 bhsdo->bhsdo_initiator_task_tag,
894 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
896 if (bhsdo->bhsdo_target_transfer_tag ==
897 cdw->cdw_target_transfer_tag)
900 CFISCSI_SESSION_UNLOCK(cs);
902 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
903 "0x%x, not found; dropping connection",
904 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
905 icl_pdu_free(request);
906 cfiscsi_session_terminate(cs);
910 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
911 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
912 "DataSN %u, while expected %u; dropping connection",
913 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
914 icl_pdu_free(request);
915 cfiscsi_session_terminate(cs);
920 io = cdw->cdw_ctl_io;
921 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
922 ("CTL_FLAG_DATA_IN"));
924 done = cfiscsi_handle_data_segment(request, cdw);
926 CFISCSI_SESSION_LOCK(cs);
927 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
928 CFISCSI_SESSION_UNLOCK(cs);
929 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
930 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
931 cfiscsi_data_wait_free(cs, cdw);
933 io->scsiio.be_move_done(io);
935 cfiscsi_datamove_out(io);
938 icl_pdu_free(request);
942 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
944 struct iscsi_bhs_logout_request *bhslr;
945 struct iscsi_bhs_logout_response *bhslr2;
946 struct icl_pdu *response;
947 struct cfiscsi_session *cs;
949 cs = PDU_SESSION(request);
950 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
951 switch (bhslr->bhslr_reason & 0x7f) {
952 case BHSLR_REASON_CLOSE_SESSION:
953 case BHSLR_REASON_CLOSE_CONNECTION:
954 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
955 if (response == NULL) {
956 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
957 icl_pdu_free(request);
958 cfiscsi_session_terminate(cs);
961 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
962 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
963 bhslr2->bhslr_flags = 0x80;
964 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
965 bhslr2->bhslr_initiator_task_tag =
966 bhslr->bhslr_initiator_task_tag;
967 icl_pdu_free(request);
968 cfiscsi_pdu_queue(response);
969 cfiscsi_session_terminate(cs);
971 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
972 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
973 if (response == NULL) {
974 CFISCSI_SESSION_WARN(cs,
975 "failed to allocate memory; dropping connection");
976 icl_pdu_free(request);
977 cfiscsi_session_terminate(cs);
980 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
981 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
982 bhslr2->bhslr_flags = 0x80;
983 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
984 bhslr2->bhslr_initiator_task_tag =
985 bhslr->bhslr_initiator_task_tag;
986 icl_pdu_free(request);
987 cfiscsi_pdu_queue(response);
990 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
991 bhslr->bhslr_reason);
992 icl_pdu_free(request);
993 cfiscsi_session_terminate(cs);
999 cfiscsi_callout(void *context)
1002 struct iscsi_bhs_nop_in *bhsni;
1003 struct cfiscsi_session *cs;
1007 if (cs->cs_terminating)
1010 callout_schedule(&cs->cs_callout, 1 * hz);
1012 atomic_add_int(&cs->cs_timeout, 1);
1014 #ifdef ICL_KERNEL_PROXY
1015 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1016 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1017 CFISCSI_SESSION_WARN(cs, "login timed out after "
1018 "%d seconds; dropping connection", cs->cs_timeout);
1019 cfiscsi_session_terminate(cs);
1025 if (ping_timeout <= 0) {
1027 * Pings are disabled. Don't send NOP-In in this case;
1028 * user might have disabled pings to work around problems
1029 * with certain initiators that can't properly handle
1030 * NOP-In, such as iPXE. Reset the timeout, to avoid
1031 * triggering reconnection, should the user decide to
1038 if (cs->cs_timeout >= ping_timeout) {
1039 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1040 "dropping connection", ping_timeout);
1041 cfiscsi_session_terminate(cs);
1046 * If the ping was reset less than one second ago - which means
1047 * that we've received some PDU during the last second - assume
1048 * the traffic flows correctly and don't bother sending a NOP-Out.
1050 * (It's 2 - one for one second, and one for incrementing is_timeout
1051 * earlier in this routine.)
1053 if (cs->cs_timeout < 2)
1056 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1058 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1061 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1062 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1063 bhsni->bhsni_flags = 0x80;
1064 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1066 cfiscsi_pdu_queue(cp);
1069 static struct cfiscsi_data_wait *
1070 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1071 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1073 struct cfiscsi_data_wait *cdw;
1076 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1078 CFISCSI_SESSION_WARN(cs,
1079 "failed to allocate %zd bytes", sizeof(*cdw));
1083 error = icl_conn_transfer_setup(cs->cs_conn, io, target_transfer_tagp,
1086 CFISCSI_SESSION_WARN(cs,
1087 "icl_conn_transfer_setup() failed with error %d", error);
1088 uma_zfree(cfiscsi_data_wait_zone, cdw);
1092 cdw->cdw_ctl_io = io;
1093 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1094 cdw->cdw_initiator_task_tag = initiator_task_tag;
1100 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1101 struct cfiscsi_data_wait *cdw)
1104 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1105 uma_zfree(cfiscsi_data_wait_zone, cdw);
1109 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1111 struct cfiscsi_data_wait *cdw;
1113 int error, last, wait;
1115 if (cs->cs_target == NULL)
1116 return; /* No target yet, so nothing to do. */
1117 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1119 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1120 io->io_hdr.io_type = CTL_IO_TASK;
1121 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1122 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1123 io->io_hdr.nexus.targ_lun = 0;
1124 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1125 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1126 wait = cs->cs_outstanding_ctl_pdus;
1127 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1128 error = ctl_queue(io);
1129 if (error != CTL_RETVAL_COMPLETE) {
1130 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1131 refcount_release(&cs->cs_outstanding_ctl_pdus);
1135 CFISCSI_SESSION_LOCK(cs);
1136 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1137 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1138 CFISCSI_SESSION_UNLOCK(cs);
1140 * Set nonzero port status; this prevents backends from
1141 * assuming that the data transfer actually succeeded
1142 * and writing uninitialized data to disk.
1144 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1145 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1146 cfiscsi_data_wait_free(cs, cdw);
1147 CFISCSI_SESSION_LOCK(cs);
1149 CFISCSI_SESSION_UNLOCK(cs);
1152 * Wait for CTL to terminate all the tasks.
1155 CFISCSI_SESSION_WARN(cs,
1156 "waiting for CTL to terminate %d tasks", wait);
1158 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1159 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1162 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1163 0, "cfiscsi_terminate", hz / 100);
1166 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1170 cfiscsi_maintenance_thread(void *arg)
1172 struct cfiscsi_session *cs;
1177 CFISCSI_SESSION_LOCK(cs);
1178 if (cs->cs_terminating == false)
1179 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1180 CFISCSI_SESSION_UNLOCK(cs);
1182 if (cs->cs_terminating) {
1185 * We used to wait up to 30 seconds to deliver queued
1186 * PDUs to the initiator. We also tried hard to deliver
1187 * SCSI Responses for the aborted PDUs. We don't do
1188 * that anymore. We might need to revisit that.
1190 callout_drain(&cs->cs_callout);
1191 icl_conn_close(cs->cs_conn);
1194 * At this point ICL receive thread is no longer
1195 * running; no new tasks can be queued.
1197 cfiscsi_session_terminate_tasks(cs);
1198 cfiscsi_session_delete(cs);
1202 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1207 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1210 if (cs->cs_terminating)
1212 cs->cs_terminating = true;
1213 cv_signal(&cs->cs_maintenance_cv);
1214 #ifdef ICL_KERNEL_PROXY
1215 cv_signal(&cs->cs_login_cv);
1220 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1222 struct cfiscsi_target *ct;
1226 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1229 name = strdup(cs->cs_initiator_id, M_CTL);
1230 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1232 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1234 cs->cs_ctl_initid = -1;
1237 cs->cs_ctl_initid = i;
1239 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1246 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1250 if (cs->cs_ctl_initid == -1)
1253 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1255 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1258 cs->cs_ctl_initid = -1;
1261 static struct cfiscsi_session *
1262 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1264 struct cfiscsi_session *cs;
1267 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1269 CFISCSI_WARN("malloc failed");
1272 cs->cs_ctl_initid = -1;
1274 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1275 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1276 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1277 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1278 #ifdef ICL_KERNEL_PROXY
1279 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1282 cs->cs_conn = icl_new_conn(offload, "cfiscsi", &cs->cs_lock);
1283 if (cs->cs_conn == NULL) {
1284 free(cs, M_CFISCSI);
1287 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1288 cs->cs_conn->ic_error = cfiscsi_error_callback;
1289 cs->cs_conn->ic_prv0 = cs;
1291 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1293 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1294 free(cs, M_CFISCSI);
1298 mtx_lock(&softc->lock);
1299 cs->cs_id = ++softc->last_session_id;
1300 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1301 mtx_unlock(&softc->lock);
1304 * Start pinging the initiator.
1306 callout_init(&cs->cs_callout, 1);
1307 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1313 cfiscsi_session_delete(struct cfiscsi_session *cs)
1315 struct cfiscsi_softc *softc;
1317 softc = &cfiscsi_softc;
1319 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1320 ("destroying session with outstanding CTL pdus"));
1321 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1322 ("destroying session with non-empty queue"));
1324 cfiscsi_session_unregister_initiator(cs);
1325 if (cs->cs_target != NULL)
1326 cfiscsi_target_release(cs->cs_target);
1327 icl_conn_close(cs->cs_conn);
1328 icl_conn_free(cs->cs_conn);
1330 mtx_lock(&softc->lock);
1331 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1332 cv_signal(&softc->sessions_cv);
1333 mtx_unlock(&softc->lock);
1335 free(cs, M_CFISCSI);
1341 struct cfiscsi_softc *softc;
1344 softc = &cfiscsi_softc;
1346 bzero(softc, sizeof(*softc));
1347 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1349 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1350 #ifdef ICL_KERNEL_PROXY
1351 cv_init(&softc->accept_cv, "cfiscsi_accept");
1353 TAILQ_INIT(&softc->sessions);
1354 TAILQ_INIT(&softc->targets);
1356 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1357 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1363 #ifdef ICL_KERNEL_PROXY
1365 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1367 struct cfiscsi_session *cs;
1369 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1371 CFISCSI_WARN("failed to create session");
1375 icl_conn_handoff_sock(cs->cs_conn, so);
1376 cs->cs_initiator_sa = sa;
1377 cs->cs_portal_id = portal_id;
1378 cs->cs_waiting_for_ctld = true;
1379 cv_signal(&cfiscsi_softc.accept_cv);
1384 cfiscsi_online(void *arg)
1386 struct cfiscsi_softc *softc;
1387 struct cfiscsi_target *ct;
1390 ct = (struct cfiscsi_target *)arg;
1391 softc = ct->ct_softc;
1393 mtx_lock(&softc->lock);
1394 if (ct->ct_online) {
1395 mtx_unlock(&softc->lock);
1399 online = softc->online++;
1400 mtx_unlock(&softc->lock);
1404 #ifdef ICL_KERNEL_PROXY
1405 if (softc->listener != NULL)
1406 icl_listen_free(softc->listener);
1407 softc->listener = icl_listen_new(cfiscsi_accept);
1412 cfiscsi_offline(void *arg)
1414 struct cfiscsi_softc *softc;
1415 struct cfiscsi_target *ct;
1416 struct cfiscsi_session *cs;
1419 ct = (struct cfiscsi_target *)arg;
1420 softc = ct->ct_softc;
1422 mtx_lock(&softc->lock);
1423 if (!ct->ct_online) {
1424 mtx_unlock(&softc->lock);
1428 online = --softc->online;
1430 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1431 if (cs->cs_target == ct)
1432 cfiscsi_session_terminate(cs);
1435 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1436 if (cs->cs_target == ct)
1440 cv_wait(&softc->sessions_cv, &softc->lock);
1441 } while (cs != NULL && ct->ct_online == 0);
1442 mtx_unlock(&softc->lock);
1446 #ifdef ICL_KERNEL_PROXY
1447 icl_listen_free(softc->listener);
1448 softc->listener = NULL;
1453 cfiscsi_info(void *arg, struct sbuf *sb)
1455 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1458 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1464 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1466 struct cfiscsi_softc *softc;
1467 struct cfiscsi_session *cs, *cs2;
1468 struct cfiscsi_target *ct;
1469 struct ctl_iscsi_handoff_params *cihp;
1472 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1473 softc = &cfiscsi_softc;
1475 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1476 cihp->initiator_name, cihp->initiator_addr,
1479 ct = cfiscsi_target_find(softc, cihp->target_name,
1480 cihp->portal_group_tag);
1482 ci->status = CTL_ISCSI_ERROR;
1483 snprintf(ci->error_str, sizeof(ci->error_str),
1484 "%s: target not found", __func__);
1488 #ifdef ICL_KERNEL_PROXY
1489 if (cihp->socket > 0 && cihp->connection_id > 0) {
1490 snprintf(ci->error_str, sizeof(ci->error_str),
1491 "both socket and connection_id set");
1492 ci->status = CTL_ISCSI_ERROR;
1493 cfiscsi_target_release(ct);
1496 if (cihp->socket == 0) {
1497 mtx_lock(&cfiscsi_softc.lock);
1498 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1499 if (cs->cs_id == cihp->connection_id)
1503 mtx_unlock(&cfiscsi_softc.lock);
1504 snprintf(ci->error_str, sizeof(ci->error_str),
1505 "connection not found");
1506 ci->status = CTL_ISCSI_ERROR;
1507 cfiscsi_target_release(ct);
1510 mtx_unlock(&cfiscsi_softc.lock);
1513 cs = cfiscsi_session_new(softc, cihp->offload);
1515 ci->status = CTL_ISCSI_ERROR;
1516 snprintf(ci->error_str, sizeof(ci->error_str),
1517 "%s: cfiscsi_session_new failed", __func__);
1518 cfiscsi_target_release(ct);
1521 #ifdef ICL_KERNEL_PROXY
1526 * First PDU of Full Feature phase has the same CmdSN as the last
1527 * PDU from the Login Phase received from the initiator. Thus,
1530 cs->cs_cmdsn = cihp->cmdsn;
1531 cs->cs_statsn = cihp->statsn;
1532 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1533 cs->cs_max_burst_length = cihp->max_burst_length;
1534 cs->cs_immediate_data = !!cihp->immediate_data;
1535 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1536 cs->cs_conn->ic_header_crc32c = true;
1537 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1538 cs->cs_conn->ic_data_crc32c = true;
1540 strlcpy(cs->cs_initiator_name,
1541 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1542 strlcpy(cs->cs_initiator_addr,
1543 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1544 strlcpy(cs->cs_initiator_alias,
1545 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1546 memcpy(cs->cs_initiator_isid,
1547 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1548 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1549 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1550 cihp->initiator_isid[0], cihp->initiator_isid[1],
1551 cihp->initiator_isid[2], cihp->initiator_isid[3],
1552 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1554 mtx_lock(&softc->lock);
1555 if (ct->ct_online == 0) {
1556 mtx_unlock(&softc->lock);
1557 cfiscsi_session_terminate(cs);
1558 cfiscsi_target_release(ct);
1559 ci->status = CTL_ISCSI_ERROR;
1560 snprintf(ci->error_str, sizeof(ci->error_str),
1561 "%s: port offline", __func__);
1565 mtx_unlock(&softc->lock);
1567 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1569 if (!cs->cs_terminating) {
1570 mtx_lock(&softc->lock);
1571 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1572 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1573 cs->cs_target == cs2->cs_target &&
1574 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1575 cfiscsi_session_terminate(cs2);
1576 mtx_unlock(&softc->lock);
1577 pause("cfiscsi_reinstate", 1);
1581 mtx_unlock(&softc->lock);
1585 * Register initiator with CTL.
1587 cfiscsi_session_register_initiator(cs);
1589 #ifdef ICL_KERNEL_PROXY
1590 if (cihp->socket > 0) {
1592 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1594 cfiscsi_session_terminate(cs);
1595 refcount_release(&cs->cs_outstanding_ctl_pdus);
1596 ci->status = CTL_ISCSI_ERROR;
1597 snprintf(ci->error_str, sizeof(ci->error_str),
1598 "%s: icl_conn_handoff failed with error %d",
1602 #ifdef ICL_KERNEL_PROXY
1606 #ifdef ICL_KERNEL_PROXY
1607 cs->cs_login_phase = false;
1610 * First PDU of the Full Feature phase has likely already arrived.
1611 * We have to pick it up and execute properly.
1613 if (cs->cs_login_pdu != NULL) {
1614 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1615 cfiscsi_pdu_handle(cs->cs_login_pdu);
1616 cs->cs_login_pdu = NULL;
1620 refcount_release(&cs->cs_outstanding_ctl_pdus);
1621 ci->status = CTL_ISCSI_OK;
1625 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1627 struct ctl_iscsi_list_params *cilp;
1628 struct cfiscsi_session *cs;
1629 struct cfiscsi_softc *softc;
1633 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1634 softc = &cfiscsi_softc;
1636 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1638 ci->status = CTL_ISCSI_ERROR;
1639 snprintf(ci->error_str, sizeof(ci->error_str),
1640 "Unable to allocate %d bytes for iSCSI session list",
1645 sbuf_printf(sb, "<ctlislist>\n");
1646 mtx_lock(&softc->lock);
1647 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1648 #ifdef ICL_KERNEL_PROXY
1649 if (cs->cs_target == NULL)
1652 error = sbuf_printf(sb, "<connection id=\"%d\">"
1653 "<initiator>%s</initiator>"
1654 "<initiator_addr>%s</initiator_addr>"
1655 "<initiator_alias>%s</initiator_alias>"
1656 "<target>%s</target>"
1657 "<target_alias>%s</target_alias>"
1658 "<target_portal_group_tag>%u</target_portal_group_tag>"
1659 "<header_digest>%s</header_digest>"
1660 "<data_digest>%s</data_digest>"
1661 "<max_data_segment_length>%zd</max_data_segment_length>"
1662 "<immediate_data>%d</immediate_data>"
1664 "<offload>%s</offload>"
1667 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1668 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1669 cs->cs_target->ct_tag,
1670 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1671 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1672 cs->cs_max_data_segment_length,
1673 cs->cs_immediate_data,
1674 cs->cs_conn->ic_iser,
1675 cs->cs_conn->ic_offload);
1679 mtx_unlock(&softc->lock);
1680 error = sbuf_printf(sb, "</ctlislist>\n");
1683 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1684 snprintf(ci->error_str, sizeof(ci->error_str),
1685 "Out of space, %d bytes is too small", cilp->alloc_len);
1690 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1691 cilp->fill_len = sbuf_len(sb) + 1;
1692 ci->status = CTL_ISCSI_OK;
1697 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1699 struct icl_pdu *response;
1700 struct iscsi_bhs_asynchronous_message *bhsam;
1701 struct ctl_iscsi_logout_params *cilp;
1702 struct cfiscsi_session *cs;
1703 struct cfiscsi_softc *softc;
1706 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1707 softc = &cfiscsi_softc;
1709 mtx_lock(&softc->lock);
1710 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1711 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1712 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1713 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1716 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1717 if (response == NULL) {
1718 ci->status = CTL_ISCSI_ERROR;
1719 snprintf(ci->error_str, sizeof(ci->error_str),
1720 "Unable to allocate memory");
1721 mtx_unlock(&softc->lock);
1725 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1726 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1727 bhsam->bhsam_flags = 0x80;
1728 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1729 bhsam->bhsam_parameter3 = htons(10);
1730 cfiscsi_pdu_queue(response);
1733 mtx_unlock(&softc->lock);
1736 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1737 snprintf(ci->error_str, sizeof(ci->error_str),
1738 "No matching connections found");
1742 ci->status = CTL_ISCSI_OK;
1746 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1748 struct icl_pdu *response;
1749 struct iscsi_bhs_asynchronous_message *bhsam;
1750 struct ctl_iscsi_terminate_params *citp;
1751 struct cfiscsi_session *cs;
1752 struct cfiscsi_softc *softc;
1755 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1756 softc = &cfiscsi_softc;
1758 mtx_lock(&softc->lock);
1759 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1760 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1761 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1762 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1765 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1766 if (response == NULL) {
1768 * Oh well. Just terminate the connection.
1771 bhsam = (struct iscsi_bhs_asynchronous_message *)
1773 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1774 bhsam->bhsam_flags = 0x80;
1775 bhsam->bhsam_0xffffffff = 0xffffffff;
1776 bhsam->bhsam_async_event =
1777 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1778 cfiscsi_pdu_queue(response);
1780 cfiscsi_session_terminate(cs);
1783 mtx_unlock(&softc->lock);
1786 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1787 snprintf(ci->error_str, sizeof(ci->error_str),
1788 "No matching connections found");
1792 ci->status = CTL_ISCSI_OK;
1796 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1798 struct ctl_iscsi_limits_params *cilp;
1801 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1803 error = icl_limits(cilp->offload, &cilp->data_segment_limit);
1805 ci->status = CTL_ISCSI_ERROR;
1806 snprintf(ci->error_str, sizeof(ci->error_str),
1807 "%s: icl_limits failed with error %d",
1812 ci->status = CTL_ISCSI_OK;
1815 #ifdef ICL_KERNEL_PROXY
1817 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1819 struct ctl_iscsi_listen_params *cilp;
1820 struct sockaddr *sa;
1823 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1825 if (cfiscsi_softc.listener == NULL) {
1826 CFISCSI_DEBUG("no listener");
1827 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1828 ci->status = CTL_ISCSI_ERROR;
1832 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1834 CFISCSI_DEBUG("getsockaddr, error %d", error);
1835 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1836 ci->status = CTL_ISCSI_ERROR;
1840 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1841 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1844 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1845 snprintf(ci->error_str, sizeof(ci->error_str),
1846 "icl_listen_add failed, error %d", error);
1847 ci->status = CTL_ISCSI_ERROR;
1851 ci->status = CTL_ISCSI_OK;
1855 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1857 struct ctl_iscsi_accept_params *ciap;
1858 struct cfiscsi_session *cs;
1861 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1863 mtx_lock(&cfiscsi_softc.lock);
1865 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1866 if (cs->cs_waiting_for_ctld)
1871 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1873 mtx_unlock(&cfiscsi_softc.lock);
1874 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1875 ci->status = CTL_ISCSI_ERROR;
1879 mtx_unlock(&cfiscsi_softc.lock);
1881 cs->cs_waiting_for_ctld = false;
1882 cs->cs_login_phase = true;
1884 ciap->connection_id = cs->cs_id;
1885 ciap->portal_id = cs->cs_portal_id;
1886 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1887 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1888 cs->cs_initiator_sa->sa_len);
1890 snprintf(ci->error_str, sizeof(ci->error_str),
1891 "copyout failed with error %d", error);
1892 ci->status = CTL_ISCSI_ERROR;
1896 ci->status = CTL_ISCSI_OK;
1900 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1902 struct ctl_iscsi_send_params *cisp;
1903 struct cfiscsi_session *cs;
1909 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1911 mtx_lock(&cfiscsi_softc.lock);
1912 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1913 if (cs->cs_id == cisp->connection_id)
1917 mtx_unlock(&cfiscsi_softc.lock);
1918 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1919 ci->status = CTL_ISCSI_ERROR;
1922 mtx_unlock(&cfiscsi_softc.lock);
1925 if (cs->cs_login_phase == false)
1929 if (cs->cs_terminating) {
1930 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1931 ci->status = CTL_ISCSI_ERROR;
1935 datalen = cisp->data_segment_len;
1939 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1940 if (datalen > 65535) {
1941 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1942 ci->status = CTL_ISCSI_ERROR;
1946 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1947 error = copyin(cisp->data_segment, data, datalen);
1949 free(data, M_CFISCSI);
1950 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1951 ci->status = CTL_ISCSI_ERROR;
1956 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1957 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1959 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1960 free(data, M_CFISCSI);
1962 CFISCSI_SESSION_LOCK(cs);
1964 CFISCSI_SESSION_UNLOCK(cs);
1965 ci->status = CTL_ISCSI_OK;
1969 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1971 struct ctl_iscsi_receive_params *cirp;
1972 struct cfiscsi_session *cs;
1977 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1979 mtx_lock(&cfiscsi_softc.lock);
1980 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1981 if (cs->cs_id == cirp->connection_id)
1985 mtx_unlock(&cfiscsi_softc.lock);
1986 snprintf(ci->error_str, sizeof(ci->error_str),
1987 "connection not found");
1988 ci->status = CTL_ISCSI_ERROR;
1991 mtx_unlock(&cfiscsi_softc.lock);
1994 if (is->is_login_phase == false)
1998 CFISCSI_SESSION_LOCK(cs);
1999 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2000 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2002 CFISCSI_SESSION_UNLOCK(cs);
2003 snprintf(ci->error_str, sizeof(ci->error_str),
2004 "interrupted by signal");
2005 ci->status = CTL_ISCSI_ERROR;
2010 if (cs->cs_terminating) {
2011 CFISCSI_SESSION_UNLOCK(cs);
2012 snprintf(ci->error_str, sizeof(ci->error_str),
2013 "connection terminating");
2014 ci->status = CTL_ISCSI_ERROR;
2017 ip = cs->cs_login_pdu;
2018 cs->cs_login_pdu = NULL;
2019 CFISCSI_SESSION_UNLOCK(cs);
2021 if (ip->ip_data_len > cirp->data_segment_len) {
2023 snprintf(ci->error_str, sizeof(ci->error_str),
2024 "data segment too big");
2025 ci->status = CTL_ISCSI_ERROR;
2029 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2030 if (ip->ip_data_len > 0) {
2031 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2032 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2033 copyout(data, cirp->data_segment, ip->ip_data_len);
2034 free(data, M_CFISCSI);
2038 ci->status = CTL_ISCSI_OK;
2041 #endif /* !ICL_KERNEL_PROXY */
2044 cfiscsi_ioctl_port_create(struct ctl_req *req)
2046 struct cfiscsi_target *ct;
2047 struct ctl_port *port;
2048 const char *target, *alias, *tags;
2049 struct scsi_vpd_id_descriptor *desc;
2051 int retval, len, idlen;
2054 ctl_init_opts(&opts, req->num_args, req->kern_args);
2055 target = ctl_get_opt(&opts, "cfiscsi_target");
2056 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
2057 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2058 if (target == NULL || tags == NULL) {
2059 req->status = CTL_LUN_ERROR;
2060 snprintf(req->error_str, sizeof(req->error_str),
2061 "Missing required argument");
2062 ctl_free_opts(&opts);
2065 tag = strtol(tags, (char **)NULL, 10);
2066 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2068 req->status = CTL_LUN_ERROR;
2069 snprintf(req->error_str, sizeof(req->error_str),
2070 "failed to create target \"%s\"", target);
2071 ctl_free_opts(&opts);
2074 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2075 req->status = CTL_LUN_ERROR;
2076 snprintf(req->error_str, sizeof(req->error_str),
2077 "target \"%s\" already exists", target);
2078 cfiscsi_target_release(ct);
2079 ctl_free_opts(&opts);
2082 port = &ct->ct_port;
2084 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2087 port->frontend = &cfiscsi_frontend;
2088 port->port_type = CTL_PORT_ISCSI;
2089 /* XXX KDM what should the real number be here? */
2090 port->num_requested_ctl_io = 4096;
2091 port->port_name = "iscsi";
2092 port->physical_port = tag;
2093 port->virtual_port = ct->ct_target_id;
2094 port->port_online = cfiscsi_online;
2095 port->port_offline = cfiscsi_offline;
2096 port->port_info = cfiscsi_info;
2097 port->onoff_arg = ct;
2098 port->fe_datamove = cfiscsi_datamove;
2099 port->fe_done = cfiscsi_done;
2101 /* XXX KDM what should we report here? */
2102 /* XXX These should probably be fetched from CTL. */
2103 port->max_targets = 1;
2104 port->max_target_id = 15;
2105 port->targ_port = -1;
2107 port->options = opts;
2110 /* Generate Port ID. */
2111 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2112 idlen = roundup2(idlen, 4);
2113 len = sizeof(struct scsi_vpd_device_id) + idlen;
2114 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2115 M_CTL, M_WAITOK | M_ZERO);
2116 port->port_devid->len = len;
2117 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2118 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2119 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2120 SVPD_ID_TYPE_SCSI_NAME;
2121 desc->length = idlen;
2122 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2124 /* Generate Target ID. */
2125 idlen = strlen(target) + 1;
2126 idlen = roundup2(idlen, 4);
2127 len = sizeof(struct scsi_vpd_device_id) + idlen;
2128 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2129 M_CTL, M_WAITOK | M_ZERO);
2130 port->target_devid->len = len;
2131 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2132 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2133 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2134 SVPD_ID_TYPE_SCSI_NAME;
2135 desc->length = idlen;
2136 strlcpy(desc->identifier, target, idlen);
2138 retval = ctl_port_register(port);
2140 ctl_free_opts(&port->options);
2141 cfiscsi_target_release(ct);
2142 free(port->port_devid, M_CFISCSI);
2143 free(port->target_devid, M_CFISCSI);
2144 req->status = CTL_LUN_ERROR;
2145 snprintf(req->error_str, sizeof(req->error_str),
2146 "ctl_port_register() failed with error %d", retval);
2150 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2151 req->status = CTL_LUN_OK;
2152 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2153 sizeof(port->targ_port)); //XXX
2157 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2159 struct cfiscsi_target *ct;
2160 const char *target, *tags;
2164 ctl_init_opts(&opts, req->num_args, req->kern_args);
2165 target = ctl_get_opt(&opts, "cfiscsi_target");
2166 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2167 if (target == NULL || tags == NULL) {
2168 ctl_free_opts(&opts);
2169 req->status = CTL_LUN_ERROR;
2170 snprintf(req->error_str, sizeof(req->error_str),
2171 "Missing required argument");
2174 tag = strtol(tags, (char **)NULL, 10);
2175 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2177 ctl_free_opts(&opts);
2178 req->status = CTL_LUN_ERROR;
2179 snprintf(req->error_str, sizeof(req->error_str),
2180 "can't find target \"%s\"", target);
2183 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2184 ctl_free_opts(&opts);
2185 req->status = CTL_LUN_ERROR;
2186 snprintf(req->error_str, sizeof(req->error_str),
2187 "target \"%s\" is already dying", target);
2190 ctl_free_opts(&opts);
2192 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2193 ctl_port_offline(&ct->ct_port);
2194 cfiscsi_target_release(ct);
2195 cfiscsi_target_release(ct);
2196 req->status = CTL_LUN_OK;
2200 cfiscsi_ioctl(struct cdev *dev,
2201 u_long cmd, caddr_t addr, int flag, struct thread *td)
2203 struct ctl_iscsi *ci;
2204 struct ctl_req *req;
2206 if (cmd == CTL_PORT_REQ) {
2207 req = (struct ctl_req *)addr;
2208 switch (req->reqtype) {
2209 case CTL_REQ_CREATE:
2210 cfiscsi_ioctl_port_create(req);
2212 case CTL_REQ_REMOVE:
2213 cfiscsi_ioctl_port_remove(req);
2216 req->status = CTL_LUN_ERROR;
2217 snprintf(req->error_str, sizeof(req->error_str),
2218 "Unsupported request type %d", req->reqtype);
2223 if (cmd != CTL_ISCSI)
2226 ci = (struct ctl_iscsi *)addr;
2228 case CTL_ISCSI_HANDOFF:
2229 cfiscsi_ioctl_handoff(ci);
2231 case CTL_ISCSI_LIST:
2232 cfiscsi_ioctl_list(ci);
2234 case CTL_ISCSI_LOGOUT:
2235 cfiscsi_ioctl_logout(ci);
2237 case CTL_ISCSI_TERMINATE:
2238 cfiscsi_ioctl_terminate(ci);
2240 case CTL_ISCSI_LIMITS:
2241 cfiscsi_ioctl_limits(ci);
2243 #ifdef ICL_KERNEL_PROXY
2244 case CTL_ISCSI_LISTEN:
2245 cfiscsi_ioctl_listen(ci);
2247 case CTL_ISCSI_ACCEPT:
2248 cfiscsi_ioctl_accept(ci);
2250 case CTL_ISCSI_SEND:
2251 cfiscsi_ioctl_send(ci);
2253 case CTL_ISCSI_RECEIVE:
2254 cfiscsi_ioctl_receive(ci);
2257 case CTL_ISCSI_LISTEN:
2258 case CTL_ISCSI_ACCEPT:
2259 case CTL_ISCSI_SEND:
2260 case CTL_ISCSI_RECEIVE:
2261 ci->status = CTL_ISCSI_ERROR;
2262 snprintf(ci->error_str, sizeof(ci->error_str),
2263 "%s: CTL compiled without ICL_KERNEL_PROXY",
2266 #endif /* !ICL_KERNEL_PROXY */
2268 ci->status = CTL_ISCSI_ERROR;
2269 snprintf(ci->error_str, sizeof(ci->error_str),
2270 "%s: invalid iSCSI request type %d", __func__, ci->type);
2278 cfiscsi_target_hold(struct cfiscsi_target *ct)
2281 refcount_acquire(&ct->ct_refcount);
2285 cfiscsi_target_release(struct cfiscsi_target *ct)
2287 struct cfiscsi_softc *softc;
2289 softc = ct->ct_softc;
2290 mtx_lock(&softc->lock);
2291 if (refcount_release(&ct->ct_refcount)) {
2292 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2293 mtx_unlock(&softc->lock);
2294 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2295 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2296 if (ctl_port_deregister(&ct->ct_port) != 0)
2297 printf("%s: ctl_port_deregister() failed\n",
2300 free(ct, M_CFISCSI);
2304 mtx_unlock(&softc->lock);
2307 static struct cfiscsi_target *
2308 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2310 struct cfiscsi_target *ct;
2312 mtx_lock(&softc->lock);
2313 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2314 if (ct->ct_tag != tag ||
2315 strcmp(name, ct->ct_name) != 0 ||
2316 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2318 cfiscsi_target_hold(ct);
2319 mtx_unlock(&softc->lock);
2322 mtx_unlock(&softc->lock);
2327 static struct cfiscsi_target *
2328 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2329 const char *alias, uint16_t tag)
2331 struct cfiscsi_target *ct, *newct;
2333 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2336 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2338 mtx_lock(&softc->lock);
2339 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2340 if (ct->ct_tag != tag ||
2341 strcmp(name, ct->ct_name) != 0 ||
2342 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2344 cfiscsi_target_hold(ct);
2345 mtx_unlock(&softc->lock);
2346 free(newct, M_CFISCSI);
2350 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2352 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2353 newct->ct_tag = tag;
2354 refcount_init(&newct->ct_refcount, 1);
2355 newct->ct_softc = softc;
2356 if (TAILQ_EMPTY(&softc->targets))
2357 softc->last_target_id = 0;
2358 newct->ct_target_id = ++softc->last_target_id;
2359 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2360 mtx_unlock(&softc->lock);
2366 cfiscsi_datamove_in(union ctl_io *io)
2368 struct cfiscsi_session *cs;
2369 struct icl_pdu *request, *response;
2370 const struct iscsi_bhs_scsi_command *bhssc;
2371 struct iscsi_bhs_data_in *bhsdi;
2372 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2373 size_t len, expected_len, sg_len, buffer_offset;
2374 const char *sg_addr;
2375 int ctl_sg_count, error, i;
2377 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2378 cs = PDU_SESSION(request);
2380 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2381 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2382 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2383 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2385 if (io->scsiio.kern_sg_entries > 0) {
2386 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2387 ctl_sg_count = io->scsiio.kern_sg_entries;
2389 ctl_sglist = &ctl_sg_entry;
2390 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2391 ctl_sglist->len = io->scsiio.kern_data_len;
2396 * This is the total amount of data to be transferred within the current
2397 * SCSI command. We need to record it so that we can properly report
2398 * underflow/underflow.
2400 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2403 * This is the offset within the current SCSI command; for the first
2404 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2405 * it will be the sum of lengths of previous ones.
2407 buffer_offset = io->scsiio.kern_rel_offset;
2410 * This is the transfer length expected by the initiator. In theory,
2411 * it could be different from the correct amount of data from the SCSI
2412 * point of view, even if that doesn't make any sense.
2414 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2416 if (expected_len != io->scsiio.kern_total_len) {
2417 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2418 "actual length %zd", expected_len,
2419 (size_t)io->scsiio.kern_total_len);
2423 if (buffer_offset >= expected_len) {
2425 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2426 "already sent the expected len", buffer_offset);
2428 io->scsiio.be_move_done(io);
2438 if (response == NULL) {
2439 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2440 if (response == NULL) {
2441 CFISCSI_SESSION_WARN(cs, "failed to "
2442 "allocate memory; dropping connection");
2443 ctl_set_busy(&io->scsiio);
2444 io->scsiio.be_move_done(io);
2445 cfiscsi_session_terminate(cs);
2448 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2449 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2450 bhsdi->bhsdi_initiator_task_tag =
2451 bhssc->bhssc_initiator_task_tag;
2452 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2453 PDU_EXPDATASN(request)++;
2454 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2457 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2459 sg_addr = ctl_sglist[i].addr;
2460 sg_len = ctl_sglist[i].len;
2461 KASSERT(sg_len > 0, ("sg_len <= 0"));
2467 * Truncate to maximum data segment length.
2469 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2470 ("ip_data_len %zd >= max_data_segment_length %zd",
2471 response->ip_data_len, cs->cs_max_data_segment_length));
2472 if (response->ip_data_len + len >
2473 cs->cs_max_data_segment_length) {
2474 len = cs->cs_max_data_segment_length -
2475 response->ip_data_len;
2476 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2481 * Truncate to expected data transfer length.
2483 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2484 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2485 buffer_offset, response->ip_data_len, expected_len));
2486 if (buffer_offset + response->ip_data_len + len > expected_len) {
2487 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2488 "to expected data transfer length %zd",
2489 buffer_offset + response->ip_data_len + len, expected_len);
2490 len = expected_len - (buffer_offset + response->ip_data_len);
2491 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2495 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2497 CFISCSI_SESSION_WARN(cs, "failed to "
2498 "allocate memory; dropping connection");
2499 icl_pdu_free(response);
2500 ctl_set_busy(&io->scsiio);
2501 io->scsiio.be_move_done(io);
2502 cfiscsi_session_terminate(cs);
2508 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2509 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2510 buffer_offset, response->ip_data_len, expected_len));
2511 if (buffer_offset + response->ip_data_len == expected_len) {
2513 * Already have the amount of data the initiator wanted.
2520 * End of scatter-gather segment;
2521 * proceed to the next one...
2523 if (i == ctl_sg_count - 1) {
2525 * ... unless this was the last one.
2532 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2534 * Can't stuff more data into the current PDU;
2535 * queue it. Note that's not enough to check
2536 * for kern_data_resid == 0 instead; there
2537 * may be several Data-In PDUs for the final
2538 * call to cfiscsi_datamove(), and we want
2539 * to set the F flag only on the last of them.
2541 buffer_offset += response->ip_data_len;
2542 if (buffer_offset == io->scsiio.kern_total_len ||
2543 buffer_offset == expected_len) {
2544 buffer_offset -= response->ip_data_len;
2547 cfiscsi_pdu_queue(response);
2552 if (response != NULL) {
2553 buffer_offset += response->ip_data_len;
2554 if (buffer_offset == io->scsiio.kern_total_len ||
2555 buffer_offset == expected_len) {
2556 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2557 if (io->io_hdr.status == CTL_SUCCESS) {
2558 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2559 if (PDU_TOTAL_TRANSFER_LEN(request) <
2560 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2561 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2562 bhsdi->bhsdi_residual_count =
2563 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2564 PDU_TOTAL_TRANSFER_LEN(request));
2565 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2566 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2567 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2568 bhsdi->bhsdi_residual_count =
2569 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2570 ntohl(bhssc->bhssc_expected_data_transfer_length));
2572 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2573 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2576 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2577 cfiscsi_pdu_queue(response);
2580 io->scsiio.be_move_done(io);
2584 cfiscsi_datamove_out(union ctl_io *io)
2586 struct cfiscsi_session *cs;
2587 struct icl_pdu *request, *response;
2588 const struct iscsi_bhs_scsi_command *bhssc;
2589 struct iscsi_bhs_r2t *bhsr2t;
2590 struct cfiscsi_data_wait *cdw;
2591 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2592 uint32_t expected_len, r2t_off, r2t_len;
2593 uint32_t target_transfer_tag;
2596 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2597 cs = PDU_SESSION(request);
2599 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2600 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2601 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2602 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2605 * We need to record it so that we can properly report
2606 * underflow/underflow.
2608 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2611 * Report write underflow as error since CTL and backends don't
2612 * really support it, and SCSI does not tell how to do it right.
2614 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2615 if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len >
2617 io->scsiio.io_hdr.port_status = 43;
2618 io->scsiio.be_move_done(io);
2622 target_transfer_tag =
2623 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2624 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2625 &target_transfer_tag);
2627 CFISCSI_SESSION_WARN(cs, "failed to "
2628 "allocate memory; dropping connection");
2629 ctl_set_busy(&io->scsiio);
2630 io->scsiio.be_move_done(io);
2631 cfiscsi_session_terminate(cs);
2635 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2636 "task tag 0x%x, target transfer tag 0x%x",
2637 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2640 cdw->cdw_ctl_io = io;
2641 cdw->cdw_target_transfer_tag = target_transfer_tag;
2642 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2643 cdw->cdw_r2t_end = io->scsiio.kern_data_len;
2644 cdw->cdw_datasn = 0;
2646 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2647 if (io->scsiio.kern_sg_entries > 0) {
2648 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2650 ctl_sglist = &ctl_sg_entry;
2651 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2652 ctl_sglist->len = io->scsiio.kern_data_len;
2654 cdw->cdw_sg_index = 0;
2655 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2656 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2657 r2t_off = io->scsiio.ext_data_filled;
2658 while (r2t_off > 0) {
2659 if (r2t_off >= cdw->cdw_sg_len) {
2660 r2t_off -= cdw->cdw_sg_len;
2661 cdw->cdw_sg_index++;
2662 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2663 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2666 cdw->cdw_sg_addr += r2t_off;
2667 cdw->cdw_sg_len -= r2t_off;
2671 if (cs->cs_immediate_data &&
2672 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2673 icl_pdu_data_segment_length(request)) {
2674 done = cfiscsi_handle_data_segment(request, cdw);
2676 cfiscsi_data_wait_free(cs, cdw);
2677 io->scsiio.be_move_done(io);
2682 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2683 r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled,
2684 cs->cs_max_burst_length);
2685 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2687 CFISCSI_SESSION_LOCK(cs);
2688 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2689 CFISCSI_SESSION_UNLOCK(cs);
2692 * XXX: We should limit the number of outstanding R2T PDUs
2693 * per task to MaxOutstandingR2T.
2695 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2696 if (response == NULL) {
2697 CFISCSI_SESSION_WARN(cs, "failed to "
2698 "allocate memory; dropping connection");
2699 ctl_set_busy(&io->scsiio);
2700 io->scsiio.be_move_done(io);
2701 cfiscsi_session_terminate(cs);
2704 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2705 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2706 bhsr2t->bhsr2t_flags = 0x80;
2707 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2708 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2709 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2711 * XXX: Here we assume that cfiscsi_datamove() won't ever
2712 * be running concurrently on several CPUs for a given
2715 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2716 PDU_R2TSN(request)++;
2718 * This is the offset within the current SCSI command;
2719 * i.e. for the first call of datamove(), it will be 0,
2720 * and for subsequent ones it will be the sum of lengths
2723 * The ext_data_filled is to account for unsolicited
2724 * (immediate) data that might have already arrived.
2726 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2728 * This is the total length (sum of S/G lengths) this call
2729 * to cfiscsi_datamove() is supposed to handle, limited by
2732 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2733 cfiscsi_pdu_queue(response);
2737 cfiscsi_datamove(union ctl_io *io)
2740 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2741 cfiscsi_datamove_in(io);
2743 /* We hadn't received anything during this datamove yet. */
2744 io->scsiio.ext_data_filled = 0;
2745 cfiscsi_datamove_out(io);
2750 cfiscsi_scsi_command_done(union ctl_io *io)
2752 struct icl_pdu *request, *response;
2753 struct iscsi_bhs_scsi_command *bhssc;
2754 struct iscsi_bhs_scsi_response *bhssr;
2756 struct cfiscsi_data_wait *cdw;
2758 struct cfiscsi_session *cs;
2759 uint16_t sense_length;
2761 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2762 cs = PDU_SESSION(request);
2763 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2764 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2765 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2766 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2768 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2769 // bhssc->bhssc_initiator_task_tag);
2772 CFISCSI_SESSION_LOCK(cs);
2773 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2774 KASSERT(bhssc->bhssc_initiator_task_tag !=
2775 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2776 CFISCSI_SESSION_UNLOCK(cs);
2780 * Do not return status for aborted commands.
2781 * There are exceptions, but none supported by CTL yet.
2783 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2784 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2785 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2787 icl_pdu_free(request);
2791 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2792 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2793 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2794 bhssr->bhssr_flags = 0x80;
2796 * XXX: We don't deal with bidirectional under/overflows;
2797 * does anything actually support those?
2799 if (PDU_TOTAL_TRANSFER_LEN(request) <
2800 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2801 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2802 bhssr->bhssr_residual_count =
2803 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2804 PDU_TOTAL_TRANSFER_LEN(request));
2805 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2806 // ntohl(bhssr->bhssr_residual_count));
2807 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2808 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2809 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2810 bhssr->bhssr_residual_count =
2811 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2812 ntohl(bhssc->bhssc_expected_data_transfer_length));
2813 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2814 // ntohl(bhssr->bhssr_residual_count));
2816 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2817 bhssr->bhssr_status = io->scsiio.scsi_status;
2818 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2819 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2821 if (io->scsiio.sense_len > 0) {
2823 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2824 io->scsiio.sense_len);
2826 sense_length = htons(io->scsiio.sense_len);
2827 icl_pdu_append_data(response,
2828 &sense_length, sizeof(sense_length), M_WAITOK);
2829 icl_pdu_append_data(response,
2830 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2834 icl_pdu_free(request);
2835 cfiscsi_pdu_queue(response);
2839 cfiscsi_task_management_done(union ctl_io *io)
2841 struct icl_pdu *request, *response;
2842 struct iscsi_bhs_task_management_request *bhstmr;
2843 struct iscsi_bhs_task_management_response *bhstmr2;
2844 struct cfiscsi_data_wait *cdw, *tmpcdw;
2845 struct cfiscsi_session *cs;
2847 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2848 cs = PDU_SESSION(request);
2849 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2850 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2851 ISCSI_BHS_OPCODE_TASK_REQUEST,
2852 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2855 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2856 bhstmr->bhstmr_initiator_task_tag,
2857 bhstmr->bhstmr_referenced_task_tag);
2860 if ((bhstmr->bhstmr_function & ~0x80) ==
2861 BHSTMR_FUNCTION_ABORT_TASK) {
2863 * Make sure we no longer wait for Data-Out for this command.
2865 CFISCSI_SESSION_LOCK(cs);
2866 TAILQ_FOREACH_SAFE(cdw,
2867 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2868 if (bhstmr->bhstmr_referenced_task_tag !=
2869 cdw->cdw_initiator_task_tag)
2873 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2874 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2876 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2878 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2879 cfiscsi_data_wait_free(cs, cdw);
2881 CFISCSI_SESSION_UNLOCK(cs);
2884 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2885 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2887 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2888 bhstmr2->bhstmr_flags = 0x80;
2889 if (io->io_hdr.status == CTL_SUCCESS) {
2890 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2893 * XXX: How to figure out what exactly went wrong? iSCSI spec
2894 * expects us to provide detailed error, e.g. "Task does
2895 * not exist" or "LUN does not exist".
2897 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2898 bhstmr2->bhstmr_response =
2899 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2901 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2904 icl_pdu_free(request);
2905 cfiscsi_pdu_queue(response);
2909 cfiscsi_done(union ctl_io *io)
2911 struct icl_pdu *request;
2912 struct cfiscsi_session *cs;
2914 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2915 ("invalid CTL status %#x", io->io_hdr.status));
2917 if (io->io_hdr.io_type == CTL_IO_TASK &&
2918 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2920 * Implicit task termination has just completed; nothing to do.
2922 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2923 cs->cs_tasks_aborted = true;
2924 refcount_release(&cs->cs_outstanding_ctl_pdus);
2925 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2930 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2931 cs = PDU_SESSION(request);
2932 refcount_release(&cs->cs_outstanding_ctl_pdus);
2934 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2935 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2936 cfiscsi_scsi_command_done(io);
2938 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2939 cfiscsi_task_management_done(io);
2942 panic("cfiscsi_done called with wrong opcode 0x%x",
2943 request->ip_bhs->bhs_opcode);