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 TUNABLE_INT("kern.cam.ctl.iscsi.debug", &debug);
89 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
90 &debug, 1, "Enable debug messages");
91 static int ping_timeout = 5;
92 TUNABLE_INT("kern.cam.ctl.iscsi.ping_timeout", &ping_timeout);
93 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
94 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
95 static int login_timeout = 60;
96 TUNABLE_INT("kern.cam.ctl.iscsi.login_timeout", &login_timeout);
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
98 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
99 static int maxcmdsn_delta = 256;
100 TUNABLE_INT("kern.cam.ctl.iscsi.maxcmdsn_delta", &maxcmdsn_delta);
101 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN,
102 &maxcmdsn_delta, 256, "Number of commands the initiator can send "
103 "without confirmation");
105 #define CFISCSI_DEBUG(X, ...) \
108 printf("%s: " X "\n", \
109 __func__, ## __VA_ARGS__); \
113 #define CFISCSI_WARN(X, ...) \
116 printf("WARNING: %s: " X "\n", \
117 __func__, ## __VA_ARGS__); \
121 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
124 printf("%s: %s (%s): " X "\n", \
125 __func__, S->cs_initiator_addr, \
126 S->cs_initiator_name, ## __VA_ARGS__); \
130 #define CFISCSI_SESSION_WARN(S, X, ...) \
133 printf("WARNING: %s (%s): " X "\n", \
134 S->cs_initiator_addr, \
135 S->cs_initiator_name, ## __VA_ARGS__); \
139 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
140 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
141 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
143 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
144 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
145 #define PDU_EXPDATASN(X) (X)->ip_prv0
146 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
147 #define PDU_R2TSN(X) (X)->ip_prv2
149 int cfiscsi_init(void);
150 static void cfiscsi_online(void *arg);
151 static void cfiscsi_offline(void *arg);
152 static int cfiscsi_info(void *arg, struct sbuf *sb);
153 static int cfiscsi_lun_enable(void *arg, int lun_id);
154 static int cfiscsi_lun_disable(void *arg, int lun_id);
155 static int cfiscsi_ioctl(struct cdev *dev,
156 u_long cmd, caddr_t addr, int flag, struct thread *td);
157 static void cfiscsi_datamove(union ctl_io *io);
158 static void cfiscsi_datamove_in(union ctl_io *io);
159 static void cfiscsi_datamove_out(union ctl_io *io);
160 static void cfiscsi_done(union ctl_io *io);
161 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
162 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
163 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
164 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
165 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
166 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
167 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
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.id = cs->cs_ctl_initid;
568 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
569 io->io_hdr.nexus.targ_target.id = 0;
570 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
571 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
572 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
573 case BHSSC_FLAGS_ATTR_UNTAGGED:
574 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
576 case BHSSC_FLAGS_ATTR_SIMPLE:
577 io->scsiio.tag_type = CTL_TAG_SIMPLE;
579 case BHSSC_FLAGS_ATTR_ORDERED:
580 io->scsiio.tag_type = CTL_TAG_ORDERED;
582 case BHSSC_FLAGS_ATTR_HOQ:
583 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
585 case BHSSC_FLAGS_ATTR_ACA:
586 io->scsiio.tag_type = CTL_TAG_ACA;
589 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
590 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
591 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
594 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
595 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
596 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
597 error = ctl_queue(io);
598 if (error != CTL_RETVAL_COMPLETE) {
599 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
600 "dropping connection", error);
602 refcount_release(&cs->cs_outstanding_ctl_pdus);
603 icl_pdu_free(request);
604 cfiscsi_session_terminate(cs);
609 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
611 struct iscsi_bhs_task_management_request *bhstmr;
612 struct iscsi_bhs_task_management_response *bhstmr2;
613 struct icl_pdu *response;
614 struct cfiscsi_session *cs;
618 cs = PDU_SESSION(request);
619 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
620 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
622 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
623 io->io_hdr.io_type = CTL_IO_TASK;
624 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
625 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
626 io->io_hdr.nexus.targ_target.id = 0;
627 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
628 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
630 switch (bhstmr->bhstmr_function & ~0x80) {
631 case BHSTMR_FUNCTION_ABORT_TASK:
633 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
635 io->taskio.task_action = CTL_TASK_ABORT_TASK;
636 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
638 case BHSTMR_FUNCTION_ABORT_TASK_SET:
640 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
642 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
644 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
646 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
648 io->taskio.task_action = CTL_TASK_LUN_RESET;
650 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
652 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
654 io->taskio.task_action = CTL_TASK_TARGET_RESET;
657 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
658 bhstmr->bhstmr_function & ~0x80);
661 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
662 if (response == NULL) {
663 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
664 "dropping connection");
665 icl_pdu_free(request);
666 cfiscsi_session_terminate(cs);
669 bhstmr2 = (struct iscsi_bhs_task_management_response *)
671 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
672 bhstmr2->bhstmr_flags = 0x80;
673 bhstmr2->bhstmr_response =
674 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
675 bhstmr2->bhstmr_initiator_task_tag =
676 bhstmr->bhstmr_initiator_task_tag;
677 icl_pdu_free(request);
678 cfiscsi_pdu_queue(response);
682 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
683 error = ctl_queue(io);
684 if (error != CTL_RETVAL_COMPLETE) {
685 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
686 "dropping connection", error);
688 refcount_release(&cs->cs_outstanding_ctl_pdus);
689 icl_pdu_free(request);
690 cfiscsi_session_terminate(cs);
695 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
697 struct iscsi_bhs_data_out *bhsdo;
698 struct cfiscsi_session *cs;
699 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
700 size_t copy_len, len, off, buffer_offset;
704 cs = PDU_SESSION(request);
706 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
707 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
708 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
709 ISCSI_BHS_OPCODE_SCSI_COMMAND,
710 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
713 * We're only using fields common for Data-Out and SCSI Command PDUs.
715 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
717 io = cdw->cdw_ctl_io;
718 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
719 ("CTL_FLAG_DATA_IN"));
722 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
723 request->ip_data_len, io->scsiio.kern_total_len);
726 if (io->scsiio.kern_sg_entries > 0) {
727 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
728 ctl_sg_count = io->scsiio.kern_sg_entries;
730 ctl_sglist = &ctl_sg_entry;
731 ctl_sglist->addr = io->scsiio.kern_data_ptr;
732 ctl_sglist->len = io->scsiio.kern_data_len;
736 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
737 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
738 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
741 len = icl_pdu_data_segment_length(request);
744 * Make sure the offset, as sent by the initiator, matches the offset
745 * we're supposed to be at in the scatter-gather list.
748 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
749 buffer_offset + len <=
750 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
751 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
752 "expected %zd; dropping connection", buffer_offset,
753 (size_t)io->scsiio.kern_rel_offset +
754 (size_t)io->scsiio.ext_data_filled);
755 ctl_set_data_phase_error(&io->scsiio);
756 cfiscsi_session_terminate(cs);
761 * This is the offset within the PDU data segment, as opposed
762 * to buffer_offset, which is the offset within the task (SCSI
765 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
769 * Iterate over the scatter/gather segments, filling them with data
770 * from the PDU data segment. Note that this can get called multiple
771 * times for one SCSI command; the cdw structure holds state for the
772 * scatter/gather list.
775 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
776 ("cdw->cdw_sg_index >= ctl_sg_count"));
777 if (cdw->cdw_sg_len == 0) {
778 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
779 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
781 KASSERT(off <= len, ("len > off"));
782 copy_len = len - off;
783 if (copy_len > cdw->cdw_sg_len)
784 copy_len = cdw->cdw_sg_len;
786 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
787 cdw->cdw_sg_addr += copy_len;
788 cdw->cdw_sg_len -= copy_len;
790 io->scsiio.ext_data_filled += copy_len;
792 if (cdw->cdw_sg_len == 0) {
794 * End of current segment.
796 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
798 * Last segment in scatter/gather list.
807 * End of PDU payload.
815 * In case of unsolicited data, it's possible that the buffer
816 * provided by CTL is smaller than negotiated FirstBurstLength.
817 * Just ignore the superfluous data; will ask for them with R2T
818 * on next call to cfiscsi_datamove().
820 * This obviously can only happen with SCSI Command PDU.
822 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
823 ISCSI_BHS_OPCODE_SCSI_COMMAND)
826 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
827 "expected %zd; dropping connection",
828 icl_pdu_data_segment_length(request), off);
829 ctl_set_data_phase_error(&io->scsiio);
830 cfiscsi_session_terminate(cs);
834 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
835 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
836 CFISCSI_SESSION_WARN(cs, "got the final packet without "
837 "the F flag; flags = 0x%x; dropping connection",
839 ctl_set_data_phase_error(&io->scsiio);
840 cfiscsi_session_terminate(cs);
844 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
845 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
846 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
847 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
848 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
849 "transmitted size was %zd bytes instead of %d; "
850 "dropping connection",
851 (size_t)io->scsiio.ext_data_filled,
853 ctl_set_data_phase_error(&io->scsiio);
854 cfiscsi_session_terminate(cs);
858 * For SCSI Command PDU, this just means we need to
859 * solicit more data by sending R2T.
865 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
867 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
868 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
878 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
880 struct iscsi_bhs_data_out *bhsdo;
881 struct cfiscsi_session *cs;
882 struct cfiscsi_data_wait *cdw = NULL;
886 cs = PDU_SESSION(request);
887 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
889 CFISCSI_SESSION_LOCK(cs);
890 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
892 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
893 "ttt 0x%x, itt 0x%x",
894 bhsdo->bhsdo_target_transfer_tag,
895 bhsdo->bhsdo_initiator_task_tag,
896 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
898 if (bhsdo->bhsdo_target_transfer_tag ==
899 cdw->cdw_target_transfer_tag)
902 CFISCSI_SESSION_UNLOCK(cs);
904 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
905 "0x%x, not found; dropping connection",
906 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
907 icl_pdu_free(request);
908 cfiscsi_session_terminate(cs);
912 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
913 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
914 "DataSN %u, while expected %u; dropping connection",
915 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
916 icl_pdu_free(request);
917 cfiscsi_session_terminate(cs);
922 io = cdw->cdw_ctl_io;
923 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
924 ("CTL_FLAG_DATA_IN"));
926 done = cfiscsi_handle_data_segment(request, cdw);
928 CFISCSI_SESSION_LOCK(cs);
929 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
930 CFISCSI_SESSION_UNLOCK(cs);
931 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
932 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
933 uma_zfree(cfiscsi_data_wait_zone, cdw);
935 io->scsiio.be_move_done(io);
937 cfiscsi_datamove_out(io);
940 icl_pdu_free(request);
944 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
946 struct iscsi_bhs_logout_request *bhslr;
947 struct iscsi_bhs_logout_response *bhslr2;
948 struct icl_pdu *response;
949 struct cfiscsi_session *cs;
951 cs = PDU_SESSION(request);
952 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
953 switch (bhslr->bhslr_reason & 0x7f) {
954 case BHSLR_REASON_CLOSE_SESSION:
955 case BHSLR_REASON_CLOSE_CONNECTION:
956 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
957 if (response == NULL) {
958 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
959 icl_pdu_free(request);
960 cfiscsi_session_terminate(cs);
963 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
964 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
965 bhslr2->bhslr_flags = 0x80;
966 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
967 bhslr2->bhslr_initiator_task_tag =
968 bhslr->bhslr_initiator_task_tag;
969 icl_pdu_free(request);
970 cfiscsi_pdu_queue(response);
971 cfiscsi_session_terminate(cs);
973 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
974 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
975 if (response == NULL) {
976 CFISCSI_SESSION_WARN(cs,
977 "failed to allocate memory; dropping connection");
978 icl_pdu_free(request);
979 cfiscsi_session_terminate(cs);
982 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
983 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
984 bhslr2->bhslr_flags = 0x80;
985 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
986 bhslr2->bhslr_initiator_task_tag =
987 bhslr->bhslr_initiator_task_tag;
988 icl_pdu_free(request);
989 cfiscsi_pdu_queue(response);
992 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
993 bhslr->bhslr_reason);
994 icl_pdu_free(request);
995 cfiscsi_session_terminate(cs);
1001 cfiscsi_callout(void *context)
1004 struct iscsi_bhs_nop_in *bhsni;
1005 struct cfiscsi_session *cs;
1009 if (cs->cs_terminating)
1012 callout_schedule(&cs->cs_callout, 1 * hz);
1014 atomic_add_int(&cs->cs_timeout, 1);
1016 #ifdef ICL_KERNEL_PROXY
1017 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1018 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1019 CFISCSI_SESSION_WARN(cs, "login timed out after "
1020 "%d seconds; dropping connection", cs->cs_timeout);
1021 cfiscsi_session_terminate(cs);
1027 if (ping_timeout <= 0) {
1029 * Pings are disabled. Don't send NOP-In in this case;
1030 * user might have disabled pings to work around problems
1031 * with certain initiators that can't properly handle
1032 * NOP-In, such as iPXE. Reset the timeout, to avoid
1033 * triggering reconnection, should the user decide to
1040 if (cs->cs_timeout >= ping_timeout) {
1041 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1042 "dropping connection", ping_timeout);
1043 cfiscsi_session_terminate(cs);
1048 * If the ping was reset less than one second ago - which means
1049 * that we've received some PDU during the last second - assume
1050 * the traffic flows correctly and don't bother sending a NOP-Out.
1052 * (It's 2 - one for one second, and one for incrementing is_timeout
1053 * earlier in this routine.)
1055 if (cs->cs_timeout < 2)
1058 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1060 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1063 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1064 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1065 bhsni->bhsni_flags = 0x80;
1066 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1068 cfiscsi_pdu_queue(cp);
1072 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1074 struct cfiscsi_data_wait *cdw;
1076 int error, last, wait;
1078 if (cs->cs_target == NULL)
1079 return; /* No target yet, so nothing to do. */
1080 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1082 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1083 io->io_hdr.io_type = CTL_IO_TASK;
1084 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1085 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1086 io->io_hdr.nexus.targ_target.id = 0;
1087 io->io_hdr.nexus.targ_lun = 0;
1088 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1089 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1090 wait = cs->cs_outstanding_ctl_pdus;
1091 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1092 error = ctl_queue(io);
1093 if (error != CTL_RETVAL_COMPLETE) {
1094 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1095 refcount_release(&cs->cs_outstanding_ctl_pdus);
1099 CFISCSI_SESSION_LOCK(cs);
1100 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1101 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1102 CFISCSI_SESSION_UNLOCK(cs);
1104 * Set nonzero port status; this prevents backends from
1105 * assuming that the data transfer actually succeeded
1106 * and writing uninitialized data to disk.
1108 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1109 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1110 uma_zfree(cfiscsi_data_wait_zone, cdw);
1111 CFISCSI_SESSION_LOCK(cs);
1113 CFISCSI_SESSION_UNLOCK(cs);
1116 * Wait for CTL to terminate all the tasks.
1119 CFISCSI_SESSION_WARN(cs,
1120 "waiting for CTL to terminate %d tasks", wait);
1122 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1123 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1126 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1127 0, "cfiscsi_terminate", hz / 100);
1130 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1134 cfiscsi_maintenance_thread(void *arg)
1136 struct cfiscsi_session *cs;
1141 CFISCSI_SESSION_LOCK(cs);
1142 if (cs->cs_terminating == false)
1143 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1144 CFISCSI_SESSION_UNLOCK(cs);
1146 if (cs->cs_terminating) {
1149 * We used to wait up to 30 seconds to deliver queued
1150 * PDUs to the initiator. We also tried hard to deliver
1151 * SCSI Responses for the aborted PDUs. We don't do
1152 * that anymore. We might need to revisit that.
1154 callout_drain(&cs->cs_callout);
1155 icl_conn_close(cs->cs_conn);
1158 * At this point ICL receive thread is no longer
1159 * running; no new tasks can be queued.
1161 cfiscsi_session_terminate_tasks(cs);
1162 cfiscsi_session_delete(cs);
1166 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1171 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1174 if (cs->cs_terminating)
1176 cs->cs_terminating = true;
1177 cv_signal(&cs->cs_maintenance_cv);
1178 #ifdef ICL_KERNEL_PROXY
1179 cv_signal(&cs->cs_login_cv);
1184 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1186 struct cfiscsi_target *ct;
1190 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1193 name = strdup(cs->cs_initiator_id, M_CTL);
1194 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1196 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1198 cs->cs_ctl_initid = -1;
1201 cs->cs_ctl_initid = i;
1203 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1210 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1214 if (cs->cs_ctl_initid == -1)
1217 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1219 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1222 cs->cs_ctl_initid = -1;
1225 static struct cfiscsi_session *
1226 cfiscsi_session_new(struct cfiscsi_softc *softc)
1228 struct cfiscsi_session *cs;
1231 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1233 CFISCSI_WARN("malloc failed");
1236 cs->cs_ctl_initid = -1;
1238 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1239 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1240 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1241 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1242 #ifdef ICL_KERNEL_PROXY
1243 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1246 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1247 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1248 cs->cs_conn->ic_error = cfiscsi_error_callback;
1249 cs->cs_conn->ic_prv0 = cs;
1251 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1253 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1254 free(cs, M_CFISCSI);
1258 mtx_lock(&softc->lock);
1259 cs->cs_id = ++softc->last_session_id;
1260 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1261 mtx_unlock(&softc->lock);
1264 * Start pinging the initiator.
1266 callout_init(&cs->cs_callout, 1);
1267 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1273 cfiscsi_session_delete(struct cfiscsi_session *cs)
1275 struct cfiscsi_softc *softc;
1277 softc = &cfiscsi_softc;
1279 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1280 ("destroying session with outstanding CTL pdus"));
1281 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1282 ("destroying session with non-empty queue"));
1284 cfiscsi_session_unregister_initiator(cs);
1285 if (cs->cs_target != NULL)
1286 cfiscsi_target_release(cs->cs_target);
1287 icl_conn_close(cs->cs_conn);
1288 icl_conn_free(cs->cs_conn);
1290 mtx_lock(&softc->lock);
1291 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1292 cv_signal(&softc->sessions_cv);
1293 mtx_unlock(&softc->lock);
1295 free(cs, M_CFISCSI);
1301 struct cfiscsi_softc *softc;
1304 softc = &cfiscsi_softc;
1306 bzero(softc, sizeof(*softc));
1307 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1309 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1310 #ifdef ICL_KERNEL_PROXY
1311 cv_init(&softc->accept_cv, "cfiscsi_accept");
1313 TAILQ_INIT(&softc->sessions);
1314 TAILQ_INIT(&softc->targets);
1316 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1317 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1323 #ifdef ICL_KERNEL_PROXY
1325 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1327 struct cfiscsi_session *cs;
1329 cs = cfiscsi_session_new(&cfiscsi_softc);
1331 CFISCSI_WARN("failed to create session");
1335 icl_conn_handoff_sock(cs->cs_conn, so);
1336 cs->cs_initiator_sa = sa;
1337 cs->cs_portal_id = portal_id;
1338 cs->cs_waiting_for_ctld = true;
1339 cv_signal(&cfiscsi_softc.accept_cv);
1344 cfiscsi_online(void *arg)
1346 struct cfiscsi_softc *softc;
1347 struct cfiscsi_target *ct;
1350 ct = (struct cfiscsi_target *)arg;
1351 softc = ct->ct_softc;
1353 mtx_lock(&softc->lock);
1354 if (ct->ct_online) {
1355 mtx_unlock(&softc->lock);
1359 online = softc->online++;
1360 mtx_unlock(&softc->lock);
1364 #ifdef ICL_KERNEL_PROXY
1365 if (softc->listener != NULL)
1366 icl_listen_free(softc->listener);
1367 softc->listener = icl_listen_new(cfiscsi_accept);
1372 cfiscsi_offline(void *arg)
1374 struct cfiscsi_softc *softc;
1375 struct cfiscsi_target *ct;
1376 struct cfiscsi_session *cs;
1379 ct = (struct cfiscsi_target *)arg;
1380 softc = ct->ct_softc;
1382 mtx_lock(&softc->lock);
1383 if (!ct->ct_online) {
1384 mtx_unlock(&softc->lock);
1388 online = --softc->online;
1390 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1391 if (cs->cs_target == ct)
1392 cfiscsi_session_terminate(cs);
1395 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1396 if (cs->cs_target == ct)
1400 cv_wait(&softc->sessions_cv, &softc->lock);
1401 } while (cs != NULL && ct->ct_online == 0);
1402 mtx_unlock(&softc->lock);
1406 #ifdef ICL_KERNEL_PROXY
1407 icl_listen_free(softc->listener);
1408 softc->listener = NULL;
1413 cfiscsi_info(void *arg, struct sbuf *sb)
1415 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1418 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1424 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1426 struct cfiscsi_softc *softc;
1427 struct cfiscsi_session *cs, *cs2;
1428 struct cfiscsi_target *ct;
1429 struct ctl_iscsi_handoff_params *cihp;
1432 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1433 softc = &cfiscsi_softc;
1435 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1436 cihp->initiator_name, cihp->initiator_addr,
1439 ct = cfiscsi_target_find(softc, cihp->target_name,
1440 cihp->portal_group_tag);
1442 ci->status = CTL_ISCSI_ERROR;
1443 snprintf(ci->error_str, sizeof(ci->error_str),
1444 "%s: target not found", __func__);
1448 #ifdef ICL_KERNEL_PROXY
1449 if (cihp->socket > 0 && cihp->connection_id > 0) {
1450 snprintf(ci->error_str, sizeof(ci->error_str),
1451 "both socket and connection_id set");
1452 ci->status = CTL_ISCSI_ERROR;
1453 cfiscsi_target_release(ct);
1456 if (cihp->socket == 0) {
1457 mtx_lock(&cfiscsi_softc.lock);
1458 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1459 if (cs->cs_id == cihp->connection_id)
1463 mtx_unlock(&cfiscsi_softc.lock);
1464 snprintf(ci->error_str, sizeof(ci->error_str),
1465 "connection not found");
1466 ci->status = CTL_ISCSI_ERROR;
1467 cfiscsi_target_release(ct);
1470 mtx_unlock(&cfiscsi_softc.lock);
1473 cs = cfiscsi_session_new(softc);
1475 ci->status = CTL_ISCSI_ERROR;
1476 snprintf(ci->error_str, sizeof(ci->error_str),
1477 "%s: cfiscsi_session_new failed", __func__);
1478 cfiscsi_target_release(ct);
1481 #ifdef ICL_KERNEL_PROXY
1486 * First PDU of Full Feature phase has the same CmdSN as the last
1487 * PDU from the Login Phase received from the initiator. Thus,
1490 cs->cs_cmdsn = cihp->cmdsn;
1491 cs->cs_statsn = cihp->statsn;
1492 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1493 cs->cs_max_burst_length = cihp->max_burst_length;
1494 cs->cs_immediate_data = !!cihp->immediate_data;
1495 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1496 cs->cs_conn->ic_header_crc32c = true;
1497 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1498 cs->cs_conn->ic_data_crc32c = true;
1500 strlcpy(cs->cs_initiator_name,
1501 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1502 strlcpy(cs->cs_initiator_addr,
1503 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1504 strlcpy(cs->cs_initiator_alias,
1505 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1506 memcpy(cs->cs_initiator_isid,
1507 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1508 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1509 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1510 cihp->initiator_isid[0], cihp->initiator_isid[1],
1511 cihp->initiator_isid[2], cihp->initiator_isid[3],
1512 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1514 mtx_lock(&softc->lock);
1515 if (ct->ct_online == 0) {
1516 mtx_unlock(&softc->lock);
1517 cfiscsi_session_terminate(cs);
1518 cfiscsi_target_release(ct);
1519 ci->status = CTL_ISCSI_ERROR;
1520 snprintf(ci->error_str, sizeof(ci->error_str),
1521 "%s: port offline", __func__);
1525 mtx_unlock(&softc->lock);
1527 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1529 if (!cs->cs_terminating) {
1530 mtx_lock(&softc->lock);
1531 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1532 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1533 cs->cs_target == cs2->cs_target &&
1534 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1535 cfiscsi_session_terminate(cs2);
1536 mtx_unlock(&softc->lock);
1537 pause("cfiscsi_reinstate", 1);
1541 mtx_unlock(&softc->lock);
1545 * Register initiator with CTL.
1547 cfiscsi_session_register_initiator(cs);
1549 #ifdef ICL_KERNEL_PROXY
1550 if (cihp->socket > 0) {
1552 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1554 cfiscsi_session_terminate(cs);
1555 refcount_release(&cs->cs_outstanding_ctl_pdus);
1556 ci->status = CTL_ISCSI_ERROR;
1557 snprintf(ci->error_str, sizeof(ci->error_str),
1558 "%s: icl_conn_handoff failed with error %d",
1562 #ifdef ICL_KERNEL_PROXY
1566 #ifdef ICL_KERNEL_PROXY
1567 cs->cs_login_phase = false;
1570 * First PDU of the Full Feature phase has likely already arrived.
1571 * We have to pick it up and execute properly.
1573 if (cs->cs_login_pdu != NULL) {
1574 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1575 cfiscsi_pdu_handle(cs->cs_login_pdu);
1576 cs->cs_login_pdu = NULL;
1580 refcount_release(&cs->cs_outstanding_ctl_pdus);
1581 ci->status = CTL_ISCSI_OK;
1585 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1587 struct ctl_iscsi_list_params *cilp;
1588 struct cfiscsi_session *cs;
1589 struct cfiscsi_softc *softc;
1593 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1594 softc = &cfiscsi_softc;
1596 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1598 ci->status = CTL_ISCSI_ERROR;
1599 snprintf(ci->error_str, sizeof(ci->error_str),
1600 "Unable to allocate %d bytes for iSCSI session list",
1605 sbuf_printf(sb, "<ctlislist>\n");
1606 mtx_lock(&softc->lock);
1607 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1608 #ifdef ICL_KERNEL_PROXY
1609 if (cs->cs_target == NULL)
1612 error = sbuf_printf(sb, "<connection id=\"%d\">"
1613 "<initiator>%s</initiator>"
1614 "<initiator_addr>%s</initiator_addr>"
1615 "<initiator_alias>%s</initiator_alias>"
1616 "<target>%s</target>"
1617 "<target_alias>%s</target_alias>"
1618 "<target_portal_group_tag>%u</target_portal_group_tag>"
1619 "<header_digest>%s</header_digest>"
1620 "<data_digest>%s</data_digest>"
1621 "<max_data_segment_length>%zd</max_data_segment_length>"
1622 "<immediate_data>%d</immediate_data>"
1626 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1627 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1628 cs->cs_target->ct_tag,
1629 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1630 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1631 cs->cs_max_data_segment_length,
1632 cs->cs_immediate_data,
1633 cs->cs_conn->ic_iser);
1637 mtx_unlock(&softc->lock);
1638 error = sbuf_printf(sb, "</ctlislist>\n");
1641 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1642 snprintf(ci->error_str, sizeof(ci->error_str),
1643 "Out of space, %d bytes is too small", cilp->alloc_len);
1648 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1649 cilp->fill_len = sbuf_len(sb) + 1;
1650 ci->status = CTL_ISCSI_OK;
1655 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1657 struct icl_pdu *response;
1658 struct iscsi_bhs_asynchronous_message *bhsam;
1659 struct ctl_iscsi_terminate_params *citp;
1660 struct cfiscsi_session *cs;
1661 struct cfiscsi_softc *softc;
1664 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1665 softc = &cfiscsi_softc;
1667 mtx_lock(&softc->lock);
1668 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1669 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1670 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1671 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1674 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1675 if (response == NULL) {
1677 * Oh well. Just terminate the connection.
1680 bhsam = (struct iscsi_bhs_asynchronous_message *)
1682 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1683 bhsam->bhsam_flags = 0x80;
1684 bhsam->bhsam_0xffffffff = 0xffffffff;
1685 bhsam->bhsam_async_event =
1686 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1687 cfiscsi_pdu_queue(response);
1689 cfiscsi_session_terminate(cs);
1692 mtx_unlock(&softc->lock);
1695 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1696 snprintf(ci->error_str, sizeof(ci->error_str),
1697 "No matching connections found");
1701 ci->status = CTL_ISCSI_OK;
1705 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1707 struct icl_pdu *response;
1708 struct iscsi_bhs_asynchronous_message *bhsam;
1709 struct ctl_iscsi_logout_params *cilp;
1710 struct cfiscsi_session *cs;
1711 struct cfiscsi_softc *softc;
1714 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1715 softc = &cfiscsi_softc;
1717 mtx_lock(&softc->lock);
1718 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1719 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1720 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1721 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1724 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1725 if (response == NULL) {
1726 ci->status = CTL_ISCSI_ERROR;
1727 snprintf(ci->error_str, sizeof(ci->error_str),
1728 "Unable to allocate memory");
1729 mtx_unlock(&softc->lock);
1733 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1734 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1735 bhsam->bhsam_flags = 0x80;
1736 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1737 bhsam->bhsam_parameter3 = htons(10);
1738 cfiscsi_pdu_queue(response);
1741 mtx_unlock(&softc->lock);
1744 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1745 snprintf(ci->error_str, sizeof(ci->error_str),
1746 "No matching connections found");
1750 ci->status = CTL_ISCSI_OK;
1753 #ifdef ICL_KERNEL_PROXY
1755 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1757 struct ctl_iscsi_listen_params *cilp;
1758 struct sockaddr *sa;
1761 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1763 if (cfiscsi_softc.listener == NULL) {
1764 CFISCSI_DEBUG("no listener");
1765 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1766 ci->status = CTL_ISCSI_ERROR;
1770 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1772 CFISCSI_DEBUG("getsockaddr, error %d", error);
1773 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1774 ci->status = CTL_ISCSI_ERROR;
1778 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1779 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1782 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1783 snprintf(ci->error_str, sizeof(ci->error_str),
1784 "icl_listen_add failed, error %d", error);
1785 ci->status = CTL_ISCSI_ERROR;
1789 ci->status = CTL_ISCSI_OK;
1793 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1795 struct ctl_iscsi_accept_params *ciap;
1796 struct cfiscsi_session *cs;
1799 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1801 mtx_lock(&cfiscsi_softc.lock);
1803 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1804 if (cs->cs_waiting_for_ctld)
1809 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1811 mtx_unlock(&cfiscsi_softc.lock);
1812 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1813 ci->status = CTL_ISCSI_ERROR;
1817 mtx_unlock(&cfiscsi_softc.lock);
1819 cs->cs_waiting_for_ctld = false;
1820 cs->cs_login_phase = true;
1822 ciap->connection_id = cs->cs_id;
1823 ciap->portal_id = cs->cs_portal_id;
1824 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1825 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1826 cs->cs_initiator_sa->sa_len);
1828 snprintf(ci->error_str, sizeof(ci->error_str),
1829 "copyout failed with error %d", error);
1830 ci->status = CTL_ISCSI_ERROR;
1834 ci->status = CTL_ISCSI_OK;
1838 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1840 struct ctl_iscsi_send_params *cisp;
1841 struct cfiscsi_session *cs;
1847 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1849 mtx_lock(&cfiscsi_softc.lock);
1850 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1851 if (cs->cs_id == cisp->connection_id)
1855 mtx_unlock(&cfiscsi_softc.lock);
1856 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1857 ci->status = CTL_ISCSI_ERROR;
1860 mtx_unlock(&cfiscsi_softc.lock);
1863 if (cs->cs_login_phase == false)
1867 if (cs->cs_terminating) {
1868 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1869 ci->status = CTL_ISCSI_ERROR;
1873 datalen = cisp->data_segment_len;
1877 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1878 if (datalen > 65535) {
1879 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1880 ci->status = CTL_ISCSI_ERROR;
1884 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1885 error = copyin(cisp->data_segment, data, datalen);
1887 free(data, M_CFISCSI);
1888 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1889 ci->status = CTL_ISCSI_ERROR;
1894 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1895 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1897 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1898 free(data, M_CFISCSI);
1900 CFISCSI_SESSION_LOCK(cs);
1902 CFISCSI_SESSION_UNLOCK(cs);
1903 ci->status = CTL_ISCSI_OK;
1907 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1909 struct ctl_iscsi_receive_params *cirp;
1910 struct cfiscsi_session *cs;
1915 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1917 mtx_lock(&cfiscsi_softc.lock);
1918 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1919 if (cs->cs_id == cirp->connection_id)
1923 mtx_unlock(&cfiscsi_softc.lock);
1924 snprintf(ci->error_str, sizeof(ci->error_str),
1925 "connection not found");
1926 ci->status = CTL_ISCSI_ERROR;
1929 mtx_unlock(&cfiscsi_softc.lock);
1932 if (is->is_login_phase == false)
1936 CFISCSI_SESSION_LOCK(cs);
1937 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
1938 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
1940 CFISCSI_SESSION_UNLOCK(cs);
1941 snprintf(ci->error_str, sizeof(ci->error_str),
1942 "interrupted by signal");
1943 ci->status = CTL_ISCSI_ERROR;
1948 if (cs->cs_terminating) {
1949 CFISCSI_SESSION_UNLOCK(cs);
1950 snprintf(ci->error_str, sizeof(ci->error_str),
1951 "connection terminating");
1952 ci->status = CTL_ISCSI_ERROR;
1955 ip = cs->cs_login_pdu;
1956 cs->cs_login_pdu = NULL;
1957 CFISCSI_SESSION_UNLOCK(cs);
1959 if (ip->ip_data_len > cirp->data_segment_len) {
1961 snprintf(ci->error_str, sizeof(ci->error_str),
1962 "data segment too big");
1963 ci->status = CTL_ISCSI_ERROR;
1967 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1968 if (ip->ip_data_len > 0) {
1969 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1970 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1971 copyout(data, cirp->data_segment, ip->ip_data_len);
1972 free(data, M_CFISCSI);
1976 ci->status = CTL_ISCSI_OK;
1979 #endif /* !ICL_KERNEL_PROXY */
1982 cfiscsi_ioctl_port_create(struct ctl_req *req)
1984 struct cfiscsi_target *ct;
1985 struct ctl_port *port;
1986 const char *target, *alias, *tags;
1987 struct scsi_vpd_id_descriptor *desc;
1989 int retval, len, idlen;
1992 ctl_init_opts(&opts, req->num_args, req->kern_args);
1993 target = ctl_get_opt(&opts, "cfiscsi_target");
1994 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
1995 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
1996 if (target == NULL || tags == NULL) {
1997 req->status = CTL_LUN_ERROR;
1998 snprintf(req->error_str, sizeof(req->error_str),
1999 "Missing required argument");
2000 ctl_free_opts(&opts);
2003 tag = strtol(tags, (char **)NULL, 10);
2004 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2006 req->status = CTL_LUN_ERROR;
2007 snprintf(req->error_str, sizeof(req->error_str),
2008 "failed to create target \"%s\"", target);
2009 ctl_free_opts(&opts);
2012 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2013 req->status = CTL_LUN_ERROR;
2014 snprintf(req->error_str, sizeof(req->error_str),
2015 "target \"%s\" already exists", target);
2016 cfiscsi_target_release(ct);
2017 ctl_free_opts(&opts);
2020 port = &ct->ct_port;
2021 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2024 port->frontend = &cfiscsi_frontend;
2025 port->port_type = CTL_PORT_ISCSI;
2026 /* XXX KDM what should the real number be here? */
2027 port->num_requested_ctl_io = 4096;
2028 port->port_name = "iscsi";
2029 port->physical_port = tag;
2030 port->virtual_port = ct->ct_target_id;
2031 port->port_online = cfiscsi_online;
2032 port->port_offline = cfiscsi_offline;
2033 port->port_info = cfiscsi_info;
2034 port->onoff_arg = ct;
2035 port->lun_enable = cfiscsi_lun_enable;
2036 port->lun_disable = cfiscsi_lun_disable;
2037 port->targ_lun_arg = ct;
2038 port->fe_datamove = cfiscsi_datamove;
2039 port->fe_done = cfiscsi_done;
2041 /* XXX KDM what should we report here? */
2042 /* XXX These should probably be fetched from CTL. */
2043 port->max_targets = 1;
2044 port->max_target_id = 15;
2046 port->options = opts;
2049 /* Generate Port ID. */
2050 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2051 idlen = roundup2(idlen, 4);
2052 len = sizeof(struct scsi_vpd_device_id) + idlen;
2053 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2054 M_CTL, M_WAITOK | M_ZERO);
2055 port->port_devid->len = len;
2056 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2057 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2058 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2059 SVPD_ID_TYPE_SCSI_NAME;
2060 desc->length = idlen;
2061 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2063 /* Generate Target ID. */
2064 idlen = strlen(target) + 1;
2065 idlen = roundup2(idlen, 4);
2066 len = sizeof(struct scsi_vpd_device_id) + idlen;
2067 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2068 M_CTL, M_WAITOK | M_ZERO);
2069 port->target_devid->len = len;
2070 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2071 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2072 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2073 SVPD_ID_TYPE_SCSI_NAME;
2074 desc->length = idlen;
2075 strlcpy(desc->identifier, target, idlen);
2077 retval = ctl_port_register(port);
2079 ctl_free_opts(&port->options);
2080 cfiscsi_target_release(ct);
2081 free(port->port_devid, M_CFISCSI);
2082 free(port->target_devid, M_CFISCSI);
2083 req->status = CTL_LUN_ERROR;
2084 snprintf(req->error_str, sizeof(req->error_str),
2085 "ctl_port_register() failed with error %d", retval);
2089 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2090 req->status = CTL_LUN_OK;
2091 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2092 sizeof(port->targ_port)); //XXX
2096 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2098 struct cfiscsi_target *ct;
2099 const char *target, *tags;
2103 ctl_init_opts(&opts, req->num_args, req->kern_args);
2104 target = ctl_get_opt(&opts, "cfiscsi_target");
2105 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2106 if (target == NULL || tags == NULL) {
2107 ctl_free_opts(&opts);
2108 req->status = CTL_LUN_ERROR;
2109 snprintf(req->error_str, sizeof(req->error_str),
2110 "Missing required argument");
2113 tag = strtol(tags, (char **)NULL, 10);
2114 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2116 ctl_free_opts(&opts);
2117 req->status = CTL_LUN_ERROR;
2118 snprintf(req->error_str, sizeof(req->error_str),
2119 "can't find target \"%s\"", target);
2122 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2123 ctl_free_opts(&opts);
2124 req->status = CTL_LUN_ERROR;
2125 snprintf(req->error_str, sizeof(req->error_str),
2126 "target \"%s\" is already dying", target);
2129 ctl_free_opts(&opts);
2131 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2132 ctl_port_offline(&ct->ct_port);
2133 cfiscsi_target_release(ct);
2134 cfiscsi_target_release(ct);
2135 req->status = CTL_LUN_OK;
2139 cfiscsi_ioctl(struct cdev *dev,
2140 u_long cmd, caddr_t addr, int flag, struct thread *td)
2142 struct ctl_iscsi *ci;
2143 struct ctl_req *req;
2145 if (cmd == CTL_PORT_REQ) {
2146 req = (struct ctl_req *)addr;
2147 switch (req->reqtype) {
2148 case CTL_REQ_CREATE:
2149 cfiscsi_ioctl_port_create(req);
2151 case CTL_REQ_REMOVE:
2152 cfiscsi_ioctl_port_remove(req);
2155 req->status = CTL_LUN_ERROR;
2156 snprintf(req->error_str, sizeof(req->error_str),
2157 "Unsupported request type %d", req->reqtype);
2162 if (cmd != CTL_ISCSI)
2165 ci = (struct ctl_iscsi *)addr;
2167 case CTL_ISCSI_HANDOFF:
2168 cfiscsi_ioctl_handoff(ci);
2170 case CTL_ISCSI_LIST:
2171 cfiscsi_ioctl_list(ci);
2173 case CTL_ISCSI_TERMINATE:
2174 cfiscsi_ioctl_terminate(ci);
2176 case CTL_ISCSI_LOGOUT:
2177 cfiscsi_ioctl_logout(ci);
2179 #ifdef ICL_KERNEL_PROXY
2180 case CTL_ISCSI_LISTEN:
2181 cfiscsi_ioctl_listen(ci);
2183 case CTL_ISCSI_ACCEPT:
2184 cfiscsi_ioctl_accept(ci);
2186 case CTL_ISCSI_SEND:
2187 cfiscsi_ioctl_send(ci);
2189 case CTL_ISCSI_RECEIVE:
2190 cfiscsi_ioctl_receive(ci);
2193 case CTL_ISCSI_LISTEN:
2194 case CTL_ISCSI_ACCEPT:
2195 case CTL_ISCSI_SEND:
2196 case CTL_ISCSI_RECEIVE:
2197 ci->status = CTL_ISCSI_ERROR;
2198 snprintf(ci->error_str, sizeof(ci->error_str),
2199 "%s: CTL compiled without ICL_KERNEL_PROXY",
2202 #endif /* !ICL_KERNEL_PROXY */
2204 ci->status = CTL_ISCSI_ERROR;
2205 snprintf(ci->error_str, sizeof(ci->error_str),
2206 "%s: invalid iSCSI request type %d", __func__, ci->type);
2214 cfiscsi_target_hold(struct cfiscsi_target *ct)
2217 refcount_acquire(&ct->ct_refcount);
2221 cfiscsi_target_release(struct cfiscsi_target *ct)
2223 struct cfiscsi_softc *softc;
2225 softc = ct->ct_softc;
2226 mtx_lock(&softc->lock);
2227 if (refcount_release(&ct->ct_refcount)) {
2228 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2229 mtx_unlock(&softc->lock);
2230 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2231 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2232 if (ctl_port_deregister(&ct->ct_port) != 0)
2233 printf("%s: ctl_port_deregister() failed\n",
2236 free(ct, M_CFISCSI);
2240 mtx_unlock(&softc->lock);
2243 static struct cfiscsi_target *
2244 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2246 struct cfiscsi_target *ct;
2248 mtx_lock(&softc->lock);
2249 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2250 if (ct->ct_tag != tag ||
2251 strcmp(name, ct->ct_name) != 0 ||
2252 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2254 cfiscsi_target_hold(ct);
2255 mtx_unlock(&softc->lock);
2258 mtx_unlock(&softc->lock);
2263 static struct cfiscsi_target *
2264 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2265 const char *alias, uint16_t tag)
2267 struct cfiscsi_target *ct, *newct;
2269 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2272 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2274 mtx_lock(&softc->lock);
2275 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2276 if (ct->ct_tag != tag ||
2277 strcmp(name, ct->ct_name) != 0 ||
2278 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2280 cfiscsi_target_hold(ct);
2281 mtx_unlock(&softc->lock);
2282 free(newct, M_CFISCSI);
2286 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2288 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2289 newct->ct_tag = tag;
2290 refcount_init(&newct->ct_refcount, 1);
2291 newct->ct_softc = softc;
2292 if (TAILQ_EMPTY(&softc->targets))
2293 softc->last_target_id = 0;
2294 newct->ct_target_id = ++softc->last_target_id;
2295 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2296 mtx_unlock(&softc->lock);
2302 cfiscsi_lun_enable(void *arg, int lun_id)
2309 cfiscsi_lun_disable(void *arg, int lun_id)
2316 cfiscsi_datamove_in(union ctl_io *io)
2318 struct cfiscsi_session *cs;
2319 struct icl_pdu *request, *response;
2320 const struct iscsi_bhs_scsi_command *bhssc;
2321 struct iscsi_bhs_data_in *bhsdi;
2322 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2323 size_t len, expected_len, sg_len, buffer_offset;
2324 const char *sg_addr;
2325 int ctl_sg_count, error, i;
2327 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2328 cs = PDU_SESSION(request);
2330 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2331 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2332 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2333 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2335 if (io->scsiio.kern_sg_entries > 0) {
2336 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2337 ctl_sg_count = io->scsiio.kern_sg_entries;
2339 ctl_sglist = &ctl_sg_entry;
2340 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2341 ctl_sglist->len = io->scsiio.kern_data_len;
2346 * This is the total amount of data to be transferred within the current
2347 * SCSI command. We need to record it so that we can properly report
2348 * underflow/underflow.
2350 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2353 * This is the offset within the current SCSI command; for the first
2354 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2355 * it will be the sum of lengths of previous ones.
2357 buffer_offset = io->scsiio.kern_rel_offset;
2360 * This is the transfer length expected by the initiator. In theory,
2361 * it could be different from the correct amount of data from the SCSI
2362 * point of view, even if that doesn't make any sense.
2364 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2366 if (expected_len != io->scsiio.kern_total_len) {
2367 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2368 "actual length %zd", expected_len,
2369 (size_t)io->scsiio.kern_total_len);
2373 if (buffer_offset >= expected_len) {
2375 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2376 "already sent the expected len", buffer_offset);
2378 io->scsiio.be_move_done(io);
2388 if (response == NULL) {
2389 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2390 if (response == NULL) {
2391 CFISCSI_SESSION_WARN(cs, "failed to "
2392 "allocate memory; dropping connection");
2393 ctl_set_busy(&io->scsiio);
2394 io->scsiio.be_move_done(io);
2395 cfiscsi_session_terminate(cs);
2398 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2399 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2400 bhsdi->bhsdi_initiator_task_tag =
2401 bhssc->bhssc_initiator_task_tag;
2402 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2403 PDU_EXPDATASN(request)++;
2404 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2407 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2409 sg_addr = ctl_sglist[i].addr;
2410 sg_len = ctl_sglist[i].len;
2411 KASSERT(sg_len > 0, ("sg_len <= 0"));
2417 * Truncate to maximum data segment length.
2419 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2420 ("ip_data_len %zd >= max_data_segment_length %zd",
2421 response->ip_data_len, cs->cs_max_data_segment_length));
2422 if (response->ip_data_len + len >
2423 cs->cs_max_data_segment_length) {
2424 len = cs->cs_max_data_segment_length -
2425 response->ip_data_len;
2426 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2431 * Truncate to expected data transfer length.
2433 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2434 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2435 buffer_offset, response->ip_data_len, expected_len));
2436 if (buffer_offset + response->ip_data_len + len > expected_len) {
2437 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2438 "to expected data transfer length %zd",
2439 buffer_offset + response->ip_data_len + len, expected_len);
2440 len = expected_len - (buffer_offset + response->ip_data_len);
2441 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2445 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2447 CFISCSI_SESSION_WARN(cs, "failed to "
2448 "allocate memory; dropping connection");
2449 icl_pdu_free(response);
2450 ctl_set_busy(&io->scsiio);
2451 io->scsiio.be_move_done(io);
2452 cfiscsi_session_terminate(cs);
2458 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2459 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2460 buffer_offset, response->ip_data_len, expected_len));
2461 if (buffer_offset + response->ip_data_len == expected_len) {
2463 * Already have the amount of data the initiator wanted.
2470 * End of scatter-gather segment;
2471 * proceed to the next one...
2473 if (i == ctl_sg_count - 1) {
2475 * ... unless this was the last one.
2482 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2484 * Can't stuff more data into the current PDU;
2485 * queue it. Note that's not enough to check
2486 * for kern_data_resid == 0 instead; there
2487 * may be several Data-In PDUs for the final
2488 * call to cfiscsi_datamove(), and we want
2489 * to set the F flag only on the last of them.
2491 buffer_offset += response->ip_data_len;
2492 if (buffer_offset == io->scsiio.kern_total_len ||
2493 buffer_offset == expected_len) {
2494 buffer_offset -= response->ip_data_len;
2497 cfiscsi_pdu_queue(response);
2502 if (response != NULL) {
2503 buffer_offset += response->ip_data_len;
2504 if (buffer_offset == io->scsiio.kern_total_len ||
2505 buffer_offset == expected_len) {
2506 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2507 if (io->io_hdr.status == CTL_SUCCESS) {
2508 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2509 if (PDU_TOTAL_TRANSFER_LEN(request) <
2510 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2511 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2512 bhsdi->bhsdi_residual_count =
2513 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2514 PDU_TOTAL_TRANSFER_LEN(request));
2515 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2516 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2517 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2518 bhsdi->bhsdi_residual_count =
2519 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2520 ntohl(bhssc->bhssc_expected_data_transfer_length));
2522 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2523 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2526 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2527 cfiscsi_pdu_queue(response);
2530 io->scsiio.be_move_done(io);
2534 cfiscsi_datamove_out(union ctl_io *io)
2536 struct cfiscsi_session *cs;
2537 struct icl_pdu *request, *response;
2538 const struct iscsi_bhs_scsi_command *bhssc;
2539 struct iscsi_bhs_r2t *bhsr2t;
2540 struct cfiscsi_data_wait *cdw;
2541 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2542 uint32_t expected_len, r2t_off, r2t_len;
2543 uint32_t target_transfer_tag;
2546 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2547 cs = PDU_SESSION(request);
2549 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2550 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2551 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2552 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2555 * We need to record it so that we can properly report
2556 * underflow/underflow.
2558 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2561 * Report write underflow as error since CTL and backends don't
2562 * really support it, and SCSI does not tell how to do it right.
2564 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2565 if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len >
2567 io->scsiio.io_hdr.port_status = 43;
2568 io->scsiio.be_move_done(io);
2572 target_transfer_tag =
2573 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2576 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2577 "task tag 0x%x, target transfer tag 0x%x",
2578 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2580 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2582 CFISCSI_SESSION_WARN(cs, "failed to "
2583 "allocate memory; dropping connection");
2584 ctl_set_busy(&io->scsiio);
2585 io->scsiio.be_move_done(io);
2586 cfiscsi_session_terminate(cs);
2589 cdw->cdw_ctl_io = io;
2590 cdw->cdw_target_transfer_tag = target_transfer_tag;
2591 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2592 cdw->cdw_r2t_end = io->scsiio.kern_data_len;
2593 cdw->cdw_datasn = 0;
2595 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2596 if (io->scsiio.kern_sg_entries > 0) {
2597 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2599 ctl_sglist = &ctl_sg_entry;
2600 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2601 ctl_sglist->len = io->scsiio.kern_data_len;
2603 cdw->cdw_sg_index = 0;
2604 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2605 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2606 r2t_off = io->scsiio.ext_data_filled;
2607 while (r2t_off > 0) {
2608 if (r2t_off >= cdw->cdw_sg_len) {
2609 r2t_off -= cdw->cdw_sg_len;
2610 cdw->cdw_sg_index++;
2611 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2612 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2615 cdw->cdw_sg_addr += r2t_off;
2616 cdw->cdw_sg_len -= r2t_off;
2620 if (cs->cs_immediate_data &&
2621 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2622 icl_pdu_data_segment_length(request)) {
2623 done = cfiscsi_handle_data_segment(request, cdw);
2625 uma_zfree(cfiscsi_data_wait_zone, cdw);
2626 io->scsiio.be_move_done(io);
2631 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2632 r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled,
2633 cs->cs_max_burst_length);
2634 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2636 CFISCSI_SESSION_LOCK(cs);
2637 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2638 CFISCSI_SESSION_UNLOCK(cs);
2641 * XXX: We should limit the number of outstanding R2T PDUs
2642 * per task to MaxOutstandingR2T.
2644 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2645 if (response == NULL) {
2646 CFISCSI_SESSION_WARN(cs, "failed to "
2647 "allocate memory; dropping connection");
2648 ctl_set_busy(&io->scsiio);
2649 io->scsiio.be_move_done(io);
2650 cfiscsi_session_terminate(cs);
2653 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2654 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2655 bhsr2t->bhsr2t_flags = 0x80;
2656 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2657 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2658 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2660 * XXX: Here we assume that cfiscsi_datamove() won't ever
2661 * be running concurrently on several CPUs for a given
2664 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2665 PDU_R2TSN(request)++;
2667 * This is the offset within the current SCSI command;
2668 * i.e. for the first call of datamove(), it will be 0,
2669 * and for subsequent ones it will be the sum of lengths
2672 * The ext_data_filled is to account for unsolicited
2673 * (immediate) data that might have already arrived.
2675 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2677 * This is the total length (sum of S/G lengths) this call
2678 * to cfiscsi_datamove() is supposed to handle, limited by
2681 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2682 cfiscsi_pdu_queue(response);
2686 cfiscsi_datamove(union ctl_io *io)
2689 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2690 cfiscsi_datamove_in(io);
2692 /* We hadn't received anything during this datamove yet. */
2693 io->scsiio.ext_data_filled = 0;
2694 cfiscsi_datamove_out(io);
2699 cfiscsi_scsi_command_done(union ctl_io *io)
2701 struct icl_pdu *request, *response;
2702 struct iscsi_bhs_scsi_command *bhssc;
2703 struct iscsi_bhs_scsi_response *bhssr;
2705 struct cfiscsi_data_wait *cdw;
2707 struct cfiscsi_session *cs;
2708 uint16_t sense_length;
2710 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2711 cs = PDU_SESSION(request);
2712 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2713 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2714 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2715 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2717 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2718 // bhssc->bhssc_initiator_task_tag);
2721 CFISCSI_SESSION_LOCK(cs);
2722 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2723 KASSERT(bhssc->bhssc_initiator_task_tag !=
2724 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2725 CFISCSI_SESSION_UNLOCK(cs);
2729 * Do not return status for aborted commands.
2730 * There are exceptions, but none supported by CTL yet.
2732 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2733 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2734 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2736 icl_pdu_free(request);
2740 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2741 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2742 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2743 bhssr->bhssr_flags = 0x80;
2745 * XXX: We don't deal with bidirectional under/overflows;
2746 * does anything actually support those?
2748 if (PDU_TOTAL_TRANSFER_LEN(request) <
2749 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2750 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2751 bhssr->bhssr_residual_count =
2752 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2753 PDU_TOTAL_TRANSFER_LEN(request));
2754 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2755 // ntohl(bhssr->bhssr_residual_count));
2756 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2757 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2758 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2759 bhssr->bhssr_residual_count =
2760 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2761 ntohl(bhssc->bhssc_expected_data_transfer_length));
2762 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2763 // ntohl(bhssr->bhssr_residual_count));
2765 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2766 bhssr->bhssr_status = io->scsiio.scsi_status;
2767 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2768 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2770 if (io->scsiio.sense_len > 0) {
2772 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2773 io->scsiio.sense_len);
2775 sense_length = htons(io->scsiio.sense_len);
2776 icl_pdu_append_data(response,
2777 &sense_length, sizeof(sense_length), M_WAITOK);
2778 icl_pdu_append_data(response,
2779 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2783 icl_pdu_free(request);
2784 cfiscsi_pdu_queue(response);
2788 cfiscsi_task_management_done(union ctl_io *io)
2790 struct icl_pdu *request, *response;
2791 struct iscsi_bhs_task_management_request *bhstmr;
2792 struct iscsi_bhs_task_management_response *bhstmr2;
2793 struct cfiscsi_data_wait *cdw, *tmpcdw;
2794 struct cfiscsi_session *cs;
2796 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2797 cs = PDU_SESSION(request);
2798 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2799 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2800 ISCSI_BHS_OPCODE_TASK_REQUEST,
2801 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2804 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2805 bhstmr->bhstmr_initiator_task_tag,
2806 bhstmr->bhstmr_referenced_task_tag);
2809 if ((bhstmr->bhstmr_function & ~0x80) ==
2810 BHSTMR_FUNCTION_ABORT_TASK) {
2812 * Make sure we no longer wait for Data-Out for this command.
2814 CFISCSI_SESSION_LOCK(cs);
2815 TAILQ_FOREACH_SAFE(cdw,
2816 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2817 if (bhstmr->bhstmr_referenced_task_tag !=
2818 cdw->cdw_initiator_task_tag)
2822 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2823 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2825 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2827 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2828 uma_zfree(cfiscsi_data_wait_zone, cdw);
2830 CFISCSI_SESSION_UNLOCK(cs);
2833 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2834 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2836 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2837 bhstmr2->bhstmr_flags = 0x80;
2838 if (io->io_hdr.status == CTL_SUCCESS) {
2839 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2842 * XXX: How to figure out what exactly went wrong? iSCSI spec
2843 * expects us to provide detailed error, e.g. "Task does
2844 * not exist" or "LUN does not exist".
2846 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2847 bhstmr2->bhstmr_response =
2848 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2850 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2853 icl_pdu_free(request);
2854 cfiscsi_pdu_queue(response);
2858 cfiscsi_done(union ctl_io *io)
2860 struct icl_pdu *request;
2861 struct cfiscsi_session *cs;
2863 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2864 ("invalid CTL status %#x", io->io_hdr.status));
2866 if (io->io_hdr.io_type == CTL_IO_TASK &&
2867 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2869 * Implicit task termination has just completed; nothing to do.
2871 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2872 cs->cs_tasks_aborted = true;
2873 refcount_release(&cs->cs_outstanding_ctl_pdus);
2874 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2879 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2880 cs = PDU_SESSION(request);
2881 refcount_release(&cs->cs_outstanding_ctl_pdus);
2883 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2884 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2885 cfiscsi_scsi_command_done(io);
2887 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2888 cfiscsi_task_management_done(io);
2891 panic("cfiscsi_done called with wrong opcode 0x%x",
2892 request->ip_bhs->bhs_opcode);