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 "ctl_frontend_iscsi.h"
74 #ifdef ICL_KERNEL_PROXY
75 #include <sys/socketvar.h>
78 #ifdef ICL_KERNEL_PROXY
79 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
82 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
83 static uma_zone_t cfiscsi_data_wait_zone;
85 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
86 "CAM Target Layer iSCSI Frontend");
88 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
89 &debug, 1, "Enable debug messages");
90 static int ping_timeout = 5;
91 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
92 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
93 static int login_timeout = 60;
94 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
95 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
96 static int maxcmdsn_delta = 256;
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN,
98 &maxcmdsn_delta, 256, "Number of commands the initiator can send "
99 "without confirmation");
101 #define CFISCSI_DEBUG(X, ...) \
104 printf("%s: " X "\n", \
105 __func__, ## __VA_ARGS__); \
109 #define CFISCSI_WARN(X, ...) \
112 printf("WARNING: %s: " X "\n", \
113 __func__, ## __VA_ARGS__); \
117 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
120 printf("%s: %s (%s): " X "\n", \
121 __func__, S->cs_initiator_addr, \
122 S->cs_initiator_name, ## __VA_ARGS__); \
126 #define CFISCSI_SESSION_WARN(S, X, ...) \
129 printf("WARNING: %s (%s): " X "\n", \
130 S->cs_initiator_addr, \
131 S->cs_initiator_name, ## __VA_ARGS__); \
135 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
136 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
137 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
139 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
140 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
141 #define PDU_EXPDATASN(X) (X)->ip_prv0
142 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
143 #define PDU_R2TSN(X) (X)->ip_prv2
145 int cfiscsi_init(void);
146 static void cfiscsi_online(void *arg);
147 static void cfiscsi_offline(void *arg);
148 static int cfiscsi_info(void *arg, struct sbuf *sb);
149 static int cfiscsi_lun_enable(void *arg,
150 struct ctl_id target_id, int lun_id);
151 static int cfiscsi_lun_disable(void *arg,
152 struct ctl_id target_id, int lun_id);
153 static uint32_t cfiscsi_lun_map(void *arg, uint32_t lun);
154 static int cfiscsi_ioctl(struct cdev *dev,
155 u_long cmd, caddr_t addr, int flag, struct thread *td);
156 static void cfiscsi_datamove(union ctl_io *io);
157 static void cfiscsi_done(union ctl_io *io);
158 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
159 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
160 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
161 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
162 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
163 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
164 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
165 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
166 *softc, const char *name);
167 static struct cfiscsi_target *cfiscsi_target_find_or_create(
168 struct cfiscsi_softc *softc, const char *name, const char *alias);
169 static void cfiscsi_target_release(struct cfiscsi_target *ct);
170 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
172 static struct cfiscsi_softc cfiscsi_softc;
173 extern struct ctl_softc *control_softc;
175 static struct ctl_frontend cfiscsi_frontend =
178 .init = cfiscsi_init,
179 .ioctl = cfiscsi_ioctl,
181 CTL_FRONTEND_DECLARE(ctlcfiscsi, cfiscsi_frontend);
183 static struct icl_pdu *
184 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
187 return (icl_pdu_new_bhs(request->ip_conn, flags));
191 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
193 const struct iscsi_bhs_scsi_command *bhssc;
194 struct cfiscsi_session *cs;
195 uint32_t cmdsn, expstatsn;
197 cs = PDU_SESSION(request);
200 * Every incoming PDU - not just NOP-Out - resets the ping timer.
201 * The purpose of the timeout is to reset the connection when it stalls;
202 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
210 * Data-Out PDUs don't contain CmdSN.
212 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
213 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
217 * We're only using fields common for all the request
218 * (initiator -> target) PDUs.
220 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
221 cmdsn = ntohl(bhssc->bhssc_cmdsn);
222 expstatsn = ntohl(bhssc->bhssc_expstatsn);
224 CFISCSI_SESSION_LOCK(cs);
226 if (expstatsn != cs->cs_statsn) {
227 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
228 "while current StatSN is %d", expstatsn,
234 * The target MUST silently ignore any non-immediate command outside
237 if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
238 CFISCSI_SESSION_UNLOCK(cs);
239 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
240 "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
244 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
247 CFISCSI_SESSION_UNLOCK(cs);
253 cfiscsi_pdu_handle(struct icl_pdu *request)
255 struct cfiscsi_session *cs;
258 cs = PDU_SESSION(request);
260 ignore = cfiscsi_pdu_update_cmdsn(request);
262 icl_pdu_free(request);
267 * Handle the PDU; this includes e.g. receiving the remaining
268 * part of PDU and submitting the SCSI command to CTL
269 * or queueing a reply. The handling routine is responsible
270 * for freeing the PDU when it's no longer needed.
272 switch (request->ip_bhs->bhs_opcode &
273 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
274 case ISCSI_BHS_OPCODE_NOP_OUT:
275 cfiscsi_pdu_handle_nop_out(request);
277 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
278 cfiscsi_pdu_handle_scsi_command(request);
280 case ISCSI_BHS_OPCODE_TASK_REQUEST:
281 cfiscsi_pdu_handle_task_request(request);
283 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
284 cfiscsi_pdu_handle_data_out(request);
286 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
287 cfiscsi_pdu_handle_logout_request(request);
290 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
291 "opcode 0x%x; dropping connection",
292 request->ip_bhs->bhs_opcode);
293 icl_pdu_free(request);
294 cfiscsi_session_terminate(cs);
300 cfiscsi_receive_callback(struct icl_pdu *request)
302 struct cfiscsi_session *cs;
304 cs = PDU_SESSION(request);
306 #ifdef ICL_KERNEL_PROXY
307 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
308 if (cs->cs_login_pdu == NULL)
309 cs->cs_login_pdu = request;
311 icl_pdu_free(request);
312 cv_signal(&cs->cs_login_cv);
317 cfiscsi_pdu_handle(request);
321 cfiscsi_error_callback(struct icl_conn *ic)
323 struct cfiscsi_session *cs;
325 cs = CONN_SESSION(ic);
327 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
328 cfiscsi_session_terminate(cs);
332 cfiscsi_pdu_prepare(struct icl_pdu *response)
334 struct cfiscsi_session *cs;
335 struct iscsi_bhs_scsi_response *bhssr;
336 bool advance_statsn = true;
338 cs = PDU_SESSION(response);
340 CFISCSI_SESSION_LOCK_ASSERT(cs);
343 * We're only using fields common for all the response
344 * (target -> initiator) PDUs.
346 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
349 * 10.8.3: "The StatSN for this connection is not advanced
350 * after this PDU is sent."
352 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
353 advance_statsn = false;
356 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
357 * StatSN for the connection is not advanced after this PDU is sent."
359 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
360 bhssr->bhssr_initiator_task_tag == 0xffffffff)
361 advance_statsn = false;
364 * See the comment below - StatSN is not meaningful and must
367 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN)
368 advance_statsn = false;
371 * 10.7.3: "The fields StatSN, Status, and Residual Count
372 * only have meaningful content if the S bit is set to 1."
374 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN)
375 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
376 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
377 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
386 cfiscsi_pdu_queue(struct icl_pdu *response)
388 struct cfiscsi_session *cs;
390 cs = PDU_SESSION(response);
392 CFISCSI_SESSION_LOCK(cs);
393 cfiscsi_pdu_prepare(response);
394 icl_pdu_queue(response);
395 CFISCSI_SESSION_UNLOCK(cs);
399 cfiscsi_decode_lun(uint64_t encoded)
405 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
406 * but is in fact an evil, multidimensional structure defined
407 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
409 memcpy(lun, &encoded, sizeof(lun));
410 switch (lun[0] & 0xC0) {
412 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
413 lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
414 CFISCSI_WARN("malformed LUN "
415 "(peripheral device addressing method): 0x%jx",
423 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
424 lun[6] != 0 || lun[7] != 0) {
425 CFISCSI_WARN("malformed LUN "
426 "(flat address space addressing method): 0x%jx",
431 result = ((lun[0] & 0x3f) << 8) + lun[1];
434 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
435 lun[6] != 0 || lun[7] != 0) {
436 CFISCSI_WARN("malformed LUN (extended flat "
437 "address space addressing method): 0x%jx",
442 result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
444 CFISCSI_WARN("unsupported LUN format 0x%jx",
454 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
456 struct cfiscsi_session *cs;
457 struct iscsi_bhs_nop_out *bhsno;
458 struct iscsi_bhs_nop_in *bhsni;
459 struct icl_pdu *response;
464 cs = PDU_SESSION(request);
465 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
467 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
469 * Nothing to do, iscsi_pdu_update_statsn() already
470 * zeroed the timeout.
472 icl_pdu_free(request);
476 datasize = icl_pdu_data_segment_length(request);
478 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
480 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
481 "dropping connection");
482 icl_pdu_free(request);
483 cfiscsi_session_terminate(cs);
486 icl_pdu_get_data(request, 0, data, datasize);
489 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
490 if (response == NULL) {
491 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
492 "droppping connection");
493 free(data, M_CFISCSI);
494 icl_pdu_free(request);
495 cfiscsi_session_terminate(cs);
498 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
499 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
500 bhsni->bhsni_flags = 0x80;
501 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
502 bhsni->bhsni_target_transfer_tag = 0xffffffff;
504 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
506 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
507 "dropping connection");
508 free(data, M_CFISCSI);
509 icl_pdu_free(request);
510 icl_pdu_free(response);
511 cfiscsi_session_terminate(cs);
514 free(data, M_CFISCSI);
517 icl_pdu_free(request);
518 cfiscsi_pdu_queue(response);
522 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
524 struct iscsi_bhs_scsi_command *bhssc;
525 struct cfiscsi_session *cs;
529 cs = PDU_SESSION(request);
530 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
531 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
532 // bhssc->bhssc_initiator_task_tag);
534 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
535 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
536 "ImmediateData=No; dropping connection");
537 icl_pdu_free(request);
538 cfiscsi_session_terminate(cs);
541 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
543 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; "
544 "dropping connection");
545 icl_pdu_free(request);
546 cfiscsi_session_terminate(cs);
550 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
551 io->io_hdr.io_type = CTL_IO_SCSI;
552 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
553 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
554 io->io_hdr.nexus.targ_target.id = 0;
555 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
556 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
557 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
558 case BHSSC_FLAGS_ATTR_UNTAGGED:
559 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
561 case BHSSC_FLAGS_ATTR_SIMPLE:
562 io->scsiio.tag_type = CTL_TAG_SIMPLE;
564 case BHSSC_FLAGS_ATTR_ORDERED:
565 io->scsiio.tag_type = CTL_TAG_ORDERED;
567 case BHSSC_FLAGS_ATTR_HOQ:
568 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
570 case BHSSC_FLAGS_ATTR_ACA:
571 io->scsiio.tag_type = CTL_TAG_ACA;
574 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
575 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
576 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
579 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
580 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
581 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
582 error = ctl_queue(io);
583 if (error != CTL_RETVAL_COMPLETE) {
584 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
585 "dropping connection", error);
587 refcount_release(&cs->cs_outstanding_ctl_pdus);
588 icl_pdu_free(request);
589 cfiscsi_session_terminate(cs);
594 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
596 struct iscsi_bhs_task_management_request *bhstmr;
597 struct iscsi_bhs_task_management_response *bhstmr2;
598 struct icl_pdu *response;
599 struct cfiscsi_session *cs;
603 cs = PDU_SESSION(request);
604 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
605 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
607 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;"
608 "dropping connection");
609 icl_pdu_free(request);
610 cfiscsi_session_terminate(cs);
614 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
615 io->io_hdr.io_type = CTL_IO_TASK;
616 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
617 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
618 io->io_hdr.nexus.targ_target.id = 0;
619 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
620 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
622 switch (bhstmr->bhstmr_function & ~0x80) {
623 case BHSTMR_FUNCTION_ABORT_TASK:
625 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
627 io->taskio.task_action = CTL_TASK_ABORT_TASK;
628 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
630 case BHSTMR_FUNCTION_ABORT_TASK_SET:
632 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
634 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
636 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
638 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
640 io->taskio.task_action = CTL_TASK_LUN_RESET;
642 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
644 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
646 io->taskio.task_action = CTL_TASK_TARGET_RESET;
649 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
650 bhstmr->bhstmr_function & ~0x80);
653 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
654 if (response == NULL) {
655 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
656 "dropping connection");
657 icl_pdu_free(request);
658 cfiscsi_session_terminate(cs);
661 bhstmr2 = (struct iscsi_bhs_task_management_response *)
663 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
664 bhstmr2->bhstmr_flags = 0x80;
665 bhstmr2->bhstmr_response =
666 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
667 bhstmr2->bhstmr_initiator_task_tag =
668 bhstmr->bhstmr_initiator_task_tag;
669 icl_pdu_free(request);
670 cfiscsi_pdu_queue(response);
674 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
675 error = ctl_queue(io);
676 if (error != CTL_RETVAL_COMPLETE) {
677 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
678 "dropping connection", error);
680 refcount_release(&cs->cs_outstanding_ctl_pdus);
681 icl_pdu_free(request);
682 cfiscsi_session_terminate(cs);
687 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
689 struct iscsi_bhs_data_out *bhsdo;
690 struct cfiscsi_session *cs;
691 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
692 size_t copy_len, len, off, buffer_offset;
696 cs = PDU_SESSION(request);
698 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
699 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
700 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
701 ISCSI_BHS_OPCODE_SCSI_COMMAND,
702 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
705 * We're only using fields common for Data-Out and SCSI Command PDUs.
707 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
709 io = cdw->cdw_ctl_io;
710 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
711 ("CTL_FLAG_DATA_IN"));
714 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
715 request->ip_data_len, io->scsiio.kern_total_len);
718 if (io->scsiio.kern_sg_entries > 0) {
719 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
720 ctl_sg_count = io->scsiio.kern_sg_entries;
722 ctl_sglist = &ctl_sg_entry;
723 ctl_sglist->addr = io->scsiio.kern_data_ptr;
724 ctl_sglist->len = io->scsiio.kern_data_len;
728 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
729 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
730 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
733 len = icl_pdu_data_segment_length(request);
736 * Make sure the offset, as sent by the initiator, matches the offset
737 * we're supposed to be at in the scatter-gather list.
740 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
741 buffer_offset + len <=
742 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
743 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
744 "expected %zd; dropping connection", buffer_offset,
745 (size_t)io->scsiio.kern_rel_offset +
746 (size_t)io->scsiio.ext_data_filled);
747 ctl_set_data_phase_error(&io->scsiio);
748 cfiscsi_session_terminate(cs);
753 * This is the offset within the PDU data segment, as opposed
754 * to buffer_offset, which is the offset within the task (SCSI
757 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
761 * Iterate over the scatter/gather segments, filling them with data
762 * from the PDU data segment. Note that this can get called multiple
763 * times for one SCSI command; the cdw structure holds state for the
764 * scatter/gather list.
767 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
768 ("cdw->cdw_sg_index >= ctl_sg_count"));
769 if (cdw->cdw_sg_len == 0) {
770 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
771 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
773 KASSERT(off <= len, ("len > off"));
774 copy_len = len - off;
775 if (copy_len > cdw->cdw_sg_len)
776 copy_len = cdw->cdw_sg_len;
778 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
779 cdw->cdw_sg_addr += copy_len;
780 cdw->cdw_sg_len -= copy_len;
782 io->scsiio.ext_data_filled += copy_len;
784 if (cdw->cdw_sg_len == 0) {
786 * End of current segment.
788 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
790 * Last segment in scatter/gather list.
799 * End of PDU payload.
807 * In case of unsolicited data, it's possible that the buffer
808 * provided by CTL is smaller than negotiated FirstBurstLength.
809 * Just ignore the superfluous data; will ask for them with R2T
810 * on next call to cfiscsi_datamove().
812 * This obviously can only happen with SCSI Command PDU.
814 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
815 ISCSI_BHS_OPCODE_SCSI_COMMAND)
818 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
819 "expected %zd; dropping connection",
820 icl_pdu_data_segment_length(request), off);
821 ctl_set_data_phase_error(&io->scsiio);
822 cfiscsi_session_terminate(cs);
826 if (io->scsiio.ext_data_filled == io->scsiio.kern_data_len &&
827 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
828 CFISCSI_SESSION_WARN(cs, "got the final packet without "
829 "the F flag; flags = 0x%x; dropping connection",
831 ctl_set_data_phase_error(&io->scsiio);
832 cfiscsi_session_terminate(cs);
836 if (io->scsiio.ext_data_filled != io->scsiio.kern_data_len &&
837 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
838 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
839 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
840 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
841 "transmitted size was %zd bytes instead of %d; "
842 "dropping connection",
843 (size_t)io->scsiio.ext_data_filled,
844 io->scsiio.kern_data_len);
845 ctl_set_data_phase_error(&io->scsiio);
846 cfiscsi_session_terminate(cs);
850 * For SCSI Command PDU, this just means we need to
851 * solicit more data by sending R2T.
857 if (io->scsiio.ext_data_filled == io->scsiio.kern_data_len) {
859 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
860 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
870 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
872 struct iscsi_bhs_data_out *bhsdo;
873 struct cfiscsi_session *cs;
874 struct cfiscsi_data_wait *cdw = NULL;
878 cs = PDU_SESSION(request);
879 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
881 CFISCSI_SESSION_LOCK(cs);
882 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
884 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
885 "ttt 0x%x, itt 0x%x",
886 bhsdo->bhsdo_target_transfer_tag,
887 bhsdo->bhsdo_initiator_task_tag,
888 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
890 if (bhsdo->bhsdo_target_transfer_tag ==
891 cdw->cdw_target_transfer_tag)
894 CFISCSI_SESSION_UNLOCK(cs);
896 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
897 "0x%x, not found; dropping connection",
898 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
899 icl_pdu_free(request);
900 cfiscsi_session_terminate(cs);
904 io = cdw->cdw_ctl_io;
905 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
906 ("CTL_FLAG_DATA_IN"));
908 done = cfiscsi_handle_data_segment(request, cdw);
910 CFISCSI_SESSION_LOCK(cs);
911 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
912 CFISCSI_SESSION_UNLOCK(cs);
913 uma_zfree(cfiscsi_data_wait_zone, cdw);
914 io->scsiio.be_move_done(io);
917 icl_pdu_free(request);
921 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
923 struct iscsi_bhs_logout_request *bhslr;
924 struct iscsi_bhs_logout_response *bhslr2;
925 struct icl_pdu *response;
926 struct cfiscsi_session *cs;
928 cs = PDU_SESSION(request);
929 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
930 switch (bhslr->bhslr_reason & 0x7f) {
931 case BHSLR_REASON_CLOSE_SESSION:
932 case BHSLR_REASON_CLOSE_CONNECTION:
933 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
934 if (response == NULL) {
935 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
936 icl_pdu_free(request);
937 cfiscsi_session_terminate(cs);
940 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
941 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
942 bhslr2->bhslr_flags = 0x80;
943 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
944 bhslr2->bhslr_initiator_task_tag =
945 bhslr->bhslr_initiator_task_tag;
946 icl_pdu_free(request);
947 cfiscsi_pdu_queue(response);
948 cfiscsi_session_terminate(cs);
950 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
951 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
952 if (response == NULL) {
953 CFISCSI_SESSION_WARN(cs,
954 "failed to allocate memory; dropping connection");
955 icl_pdu_free(request);
956 cfiscsi_session_terminate(cs);
959 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
960 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
961 bhslr2->bhslr_flags = 0x80;
962 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
963 bhslr2->bhslr_initiator_task_tag =
964 bhslr->bhslr_initiator_task_tag;
965 icl_pdu_free(request);
966 cfiscsi_pdu_queue(response);
969 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
970 bhslr->bhslr_reason);
971 icl_pdu_free(request);
972 cfiscsi_session_terminate(cs);
978 cfiscsi_callout(void *context)
981 struct iscsi_bhs_nop_in *bhsni;
982 struct cfiscsi_session *cs;
986 if (cs->cs_terminating)
989 callout_schedule(&cs->cs_callout, 1 * hz);
991 atomic_add_int(&cs->cs_timeout, 1);
993 #ifdef ICL_KERNEL_PROXY
994 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
995 if (cs->cs_timeout > login_timeout) {
996 CFISCSI_SESSION_WARN(cs, "login timed out after "
997 "%d seconds; dropping connection", cs->cs_timeout);
998 cfiscsi_session_terminate(cs);
1004 if (cs->cs_timeout >= ping_timeout) {
1005 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1006 "dropping connection", ping_timeout);
1007 cfiscsi_session_terminate(cs);
1012 * If the ping was reset less than one second ago - which means
1013 * that we've received some PDU during the last second - assume
1014 * the traffic flows correctly and don't bother sending a NOP-Out.
1016 * (It's 2 - one for one second, and one for incrementing is_timeout
1017 * earlier in this routine.)
1019 if (cs->cs_timeout < 2)
1022 cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1024 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1027 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1028 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1029 bhsni->bhsni_flags = 0x80;
1030 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1032 cfiscsi_pdu_queue(cp);
1036 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1038 struct cfiscsi_data_wait *cdw;
1042 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1044 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1048 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1049 io->io_hdr.io_type = CTL_IO_TASK;
1050 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1051 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1052 io->io_hdr.nexus.targ_target.id = 0;
1053 io->io_hdr.nexus.targ_lun = 0;
1054 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1055 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1056 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1057 error = ctl_queue(io);
1058 if (error != CTL_RETVAL_COMPLETE) {
1059 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1060 refcount_release(&cs->cs_outstanding_ctl_pdus);
1064 CFISCSI_SESSION_LOCK(cs);
1065 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1066 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1067 CFISCSI_SESSION_UNLOCK(cs);
1069 * Set nonzero port status; this prevents backends from
1070 * assuming that the data transfer actually succeeded
1071 * and writing uninitialized data to disk.
1073 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1074 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1075 uma_zfree(cfiscsi_data_wait_zone, cdw);
1076 CFISCSI_SESSION_LOCK(cs);
1078 CFISCSI_SESSION_UNLOCK(cs);
1081 * Wait for CTL to terminate all the tasks.
1084 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1085 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1088 CFISCSI_SESSION_WARN(cs, "waiting for CTL to terminate tasks, "
1089 "%d remaining", cs->cs_outstanding_ctl_pdus);
1090 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1091 0, "cfiscsi_terminate", hz / 100);
1096 cfiscsi_maintenance_thread(void *arg)
1098 struct cfiscsi_session *cs;
1103 CFISCSI_SESSION_LOCK(cs);
1104 if (cs->cs_terminating == false)
1105 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1106 CFISCSI_SESSION_UNLOCK(cs);
1108 if (cs->cs_terminating) {
1111 * We used to wait up to 30 seconds to deliver queued
1112 * PDUs to the initiator. We also tried hard to deliver
1113 * SCSI Responses for the aborted PDUs. We don't do
1114 * that anymore. We might need to revisit that.
1116 callout_drain(&cs->cs_callout);
1117 icl_conn_shutdown(cs->cs_conn);
1118 icl_conn_close(cs->cs_conn);
1121 * At this point ICL receive thread is no longer
1122 * running; no new tasks can be queued.
1124 cfiscsi_session_terminate_tasks(cs);
1125 cfiscsi_session_delete(cs);
1129 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1134 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1137 if (cs->cs_terminating)
1139 cs->cs_terminating = true;
1140 cv_signal(&cs->cs_maintenance_cv);
1141 #ifdef ICL_KERNEL_PROXY
1142 cv_signal(&cs->cs_login_cv);
1147 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1149 struct cfiscsi_target *ct;
1153 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1156 name = strdup(cs->cs_initiator_id, M_CTL);
1157 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1159 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1161 cs->cs_ctl_initid = -1;
1164 cs->cs_ctl_initid = i;
1166 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1173 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1177 if (cs->cs_ctl_initid == -1)
1180 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1182 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1185 cs->cs_ctl_initid = -1;
1188 static struct cfiscsi_session *
1189 cfiscsi_session_new(struct cfiscsi_softc *softc)
1191 struct cfiscsi_session *cs;
1194 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1196 CFISCSI_WARN("malloc failed");
1199 cs->cs_ctl_initid = -1;
1201 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1202 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1203 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1204 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1205 #ifdef ICL_KERNEL_PROXY
1206 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1209 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1210 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1211 cs->cs_conn->ic_error = cfiscsi_error_callback;
1212 cs->cs_conn->ic_prv0 = cs;
1214 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1216 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1217 free(cs, M_CFISCSI);
1221 mtx_lock(&softc->lock);
1222 cs->cs_id = softc->last_session_id + 1;
1223 softc->last_session_id++;
1224 mtx_unlock(&softc->lock);
1226 mtx_lock(&softc->lock);
1227 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1228 mtx_unlock(&softc->lock);
1231 * Start pinging the initiator.
1233 callout_init(&cs->cs_callout, 1);
1234 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1240 cfiscsi_session_delete(struct cfiscsi_session *cs)
1242 struct cfiscsi_softc *softc;
1244 softc = &cfiscsi_softc;
1246 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1247 ("destroying session with outstanding CTL pdus"));
1248 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1249 ("destroying session with non-empty queue"));
1251 cfiscsi_session_unregister_initiator(cs);
1252 if (cs->cs_target != NULL)
1253 cfiscsi_target_release(cs->cs_target);
1254 icl_conn_close(cs->cs_conn);
1255 icl_conn_free(cs->cs_conn);
1257 mtx_lock(&softc->lock);
1258 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1259 mtx_unlock(&softc->lock);
1261 free(cs, M_CFISCSI);
1267 struct cfiscsi_softc *softc;
1270 softc = &cfiscsi_softc;
1272 bzero(softc, sizeof(*softc));
1273 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1275 #ifdef ICL_KERNEL_PROXY
1276 cv_init(&softc->accept_cv, "cfiscsi_accept");
1278 TAILQ_INIT(&softc->sessions);
1279 TAILQ_INIT(&softc->targets);
1281 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1282 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1288 #ifdef ICL_KERNEL_PROXY
1290 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1292 struct cfiscsi_session *cs;
1294 cs = cfiscsi_session_new(&cfiscsi_softc);
1296 CFISCSI_WARN("failed to create session");
1300 icl_conn_handoff_sock(cs->cs_conn, so);
1301 cs->cs_initiator_sa = sa;
1302 cs->cs_portal_id = portal_id;
1303 cs->cs_waiting_for_ctld = true;
1304 cv_signal(&cfiscsi_softc.accept_cv);
1309 cfiscsi_online(void *arg)
1311 struct cfiscsi_softc *softc;
1312 struct cfiscsi_target *ct;
1315 ct = (struct cfiscsi_target *)arg;
1316 softc = ct->ct_softc;
1318 mtx_lock(&softc->lock);
1319 if (ct->ct_online) {
1320 mtx_unlock(&softc->lock);
1324 online = softc->online++;
1325 mtx_unlock(&softc->lock);
1329 #ifdef ICL_KERNEL_PROXY
1330 if (softc->listener != NULL)
1331 icl_listen_free(softc->listener);
1332 softc->listener = icl_listen_new(cfiscsi_accept);
1337 cfiscsi_offline(void *arg)
1339 struct cfiscsi_softc *softc;
1340 struct cfiscsi_target *ct;
1341 struct cfiscsi_session *cs;
1344 ct = (struct cfiscsi_target *)arg;
1345 softc = ct->ct_softc;
1347 mtx_lock(&softc->lock);
1348 if (!ct->ct_online) {
1349 mtx_unlock(&softc->lock);
1353 online = --softc->online;
1355 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1356 if (cs->cs_target == ct)
1357 cfiscsi_session_terminate(cs);
1359 mtx_unlock(&softc->lock);
1363 #ifdef ICL_KERNEL_PROXY
1364 icl_listen_free(softc->listener);
1365 softc->listener = NULL;
1370 cfiscsi_info(void *arg, struct sbuf *sb)
1372 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1375 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1381 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1383 struct cfiscsi_softc *softc;
1384 struct cfiscsi_session *cs, *cs2;
1385 struct cfiscsi_target *ct;
1386 struct ctl_iscsi_handoff_params *cihp;
1389 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1390 softc = &cfiscsi_softc;
1392 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1393 cihp->initiator_name, cihp->initiator_addr,
1396 ct = cfiscsi_target_find(softc, cihp->target_name);
1398 ci->status = CTL_ISCSI_ERROR;
1399 snprintf(ci->error_str, sizeof(ci->error_str),
1400 "%s: target not found", __func__);
1404 if (ct->ct_online == 0) {
1405 ci->status = CTL_ISCSI_ERROR;
1406 snprintf(ci->error_str, sizeof(ci->error_str),
1407 "%s: port offline", __func__);
1408 cfiscsi_target_release(ct);
1412 #ifdef ICL_KERNEL_PROXY
1413 if (cihp->socket > 0 && cihp->connection_id > 0) {
1414 snprintf(ci->error_str, sizeof(ci->error_str),
1415 "both socket and connection_id set");
1416 ci->status = CTL_ISCSI_ERROR;
1417 cfiscsi_target_release(ct);
1420 if (cihp->socket == 0) {
1421 mtx_lock(&cfiscsi_softc.lock);
1422 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1423 if (cs->cs_id == cihp->socket)
1427 mtx_unlock(&cfiscsi_softc.lock);
1428 snprintf(ci->error_str, sizeof(ci->error_str),
1429 "connection not found");
1430 ci->status = CTL_ISCSI_ERROR;
1431 cfiscsi_target_release(ct);
1434 mtx_unlock(&cfiscsi_softc.lock);
1437 cs = cfiscsi_session_new(softc);
1439 ci->status = CTL_ISCSI_ERROR;
1440 snprintf(ci->error_str, sizeof(ci->error_str),
1441 "%s: cfiscsi_session_new failed", __func__);
1442 cfiscsi_target_release(ct);
1445 #ifdef ICL_KERNEL_PROXY
1451 * First PDU of Full Feature phase has the same CmdSN as the last
1452 * PDU from the Login Phase received from the initiator. Thus,
1455 cs->cs_portal_group_tag = cihp->portal_group_tag;
1456 cs->cs_cmdsn = cihp->cmdsn;
1457 cs->cs_statsn = cihp->statsn;
1458 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1459 cs->cs_max_burst_length = cihp->max_burst_length;
1460 cs->cs_immediate_data = !!cihp->immediate_data;
1461 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1462 cs->cs_conn->ic_header_crc32c = true;
1463 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1464 cs->cs_conn->ic_data_crc32c = true;
1466 strlcpy(cs->cs_initiator_name,
1467 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1468 strlcpy(cs->cs_initiator_addr,
1469 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1470 strlcpy(cs->cs_initiator_alias,
1471 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1472 memcpy(cs->cs_initiator_isid,
1473 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1474 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1475 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1476 cihp->initiator_isid[0], cihp->initiator_isid[1],
1477 cihp->initiator_isid[2], cihp->initiator_isid[3],
1478 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1480 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1482 if (!cs->cs_terminating) {
1483 mtx_lock(&softc->lock);
1484 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1485 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1486 cs->cs_target == cs2->cs_target &&
1487 cs->cs_portal_group_tag == cs2->cs_portal_group_tag &&
1488 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1489 cfiscsi_session_terminate(cs2);
1490 mtx_unlock(&softc->lock);
1491 pause("cfiscsi_reinstate", 1);
1495 mtx_unlock(&softc->lock);
1499 * Register initiator with CTL.
1501 cfiscsi_session_register_initiator(cs);
1503 #ifdef ICL_KERNEL_PROXY
1504 if (cihp->socket > 0) {
1506 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1508 cfiscsi_session_terminate(cs);
1509 refcount_release(&cs->cs_outstanding_ctl_pdus);
1510 ci->status = CTL_ISCSI_ERROR;
1511 snprintf(ci->error_str, sizeof(ci->error_str),
1512 "%s: icl_conn_handoff failed with error %d",
1516 #ifdef ICL_KERNEL_PROXY
1520 #ifdef ICL_KERNEL_PROXY
1521 cs->cs_login_phase = false;
1524 * First PDU of the Full Feature phase has likely already arrived.
1525 * We have to pick it up and execute properly.
1527 if (cs->cs_login_pdu != NULL) {
1528 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1529 cfiscsi_pdu_handle(cs->cs_login_pdu);
1530 cs->cs_login_pdu = NULL;
1534 refcount_release(&cs->cs_outstanding_ctl_pdus);
1535 ci->status = CTL_ISCSI_OK;
1539 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1541 struct ctl_iscsi_list_params *cilp;
1542 struct cfiscsi_session *cs;
1543 struct cfiscsi_softc *softc;
1547 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1548 softc = &cfiscsi_softc;
1550 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1552 ci->status = CTL_ISCSI_ERROR;
1553 snprintf(ci->error_str, sizeof(ci->error_str),
1554 "Unable to allocate %d bytes for iSCSI session list",
1559 sbuf_printf(sb, "<ctlislist>\n");
1560 mtx_lock(&softc->lock);
1561 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1562 #ifdef ICL_KERNEL_PROXY
1563 if (cs->cs_target == NULL)
1566 error = sbuf_printf(sb, "<connection id=\"%d\">"
1567 "<initiator>%s</initiator>"
1568 "<initiator_addr>%s</initiator_addr>"
1569 "<initiator_alias>%s</initiator_alias>"
1570 "<target>%s</target>"
1571 "<target_alias>%s</target_alias>"
1572 "<header_digest>%s</header_digest>"
1573 "<data_digest>%s</data_digest>"
1574 "<max_data_segment_length>%zd</max_data_segment_length>"
1575 "<immediate_data>%d</immediate_data>"
1579 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1580 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1581 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1582 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1583 cs->cs_max_data_segment_length,
1584 cs->cs_immediate_data,
1585 cs->cs_conn->ic_iser);
1589 mtx_unlock(&softc->lock);
1590 error = sbuf_printf(sb, "</ctlislist>\n");
1593 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1594 snprintf(ci->error_str, sizeof(ci->error_str),
1595 "Out of space, %d bytes is too small", cilp->alloc_len);
1600 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1601 cilp->fill_len = sbuf_len(sb) + 1;
1602 ci->status = CTL_ISCSI_OK;
1607 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1609 struct icl_pdu *response;
1610 struct iscsi_bhs_asynchronous_message *bhsam;
1611 struct ctl_iscsi_terminate_params *citp;
1612 struct cfiscsi_session *cs;
1613 struct cfiscsi_softc *softc;
1616 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1617 softc = &cfiscsi_softc;
1619 mtx_lock(&softc->lock);
1620 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1621 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1622 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1623 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1626 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1627 if (response == NULL) {
1629 * Oh well. Just terminate the connection.
1632 bhsam = (struct iscsi_bhs_asynchronous_message *)
1634 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1635 bhsam->bhsam_flags = 0x80;
1636 bhsam->bhsam_0xffffffff = 0xffffffff;
1637 bhsam->bhsam_async_event =
1638 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1639 cfiscsi_pdu_queue(response);
1641 cfiscsi_session_terminate(cs);
1644 mtx_unlock(&softc->lock);
1647 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1648 snprintf(ci->error_str, sizeof(ci->error_str),
1649 "No matching connections found");
1653 ci->status = CTL_ISCSI_OK;
1657 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1659 struct icl_pdu *response;
1660 struct iscsi_bhs_asynchronous_message *bhsam;
1661 struct ctl_iscsi_logout_params *cilp;
1662 struct cfiscsi_session *cs;
1663 struct cfiscsi_softc *softc;
1666 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1667 softc = &cfiscsi_softc;
1669 mtx_lock(&softc->lock);
1670 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1671 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1672 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1673 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1676 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1677 if (response == NULL) {
1678 ci->status = CTL_ISCSI_ERROR;
1679 snprintf(ci->error_str, sizeof(ci->error_str),
1680 "Unable to allocate memory");
1681 mtx_unlock(&softc->lock);
1685 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1686 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1687 bhsam->bhsam_flags = 0x80;
1688 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1689 bhsam->bhsam_parameter3 = htons(10);
1690 cfiscsi_pdu_queue(response);
1693 mtx_unlock(&softc->lock);
1696 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1697 snprintf(ci->error_str, sizeof(ci->error_str),
1698 "No matching connections found");
1702 ci->status = CTL_ISCSI_OK;
1705 #ifdef ICL_KERNEL_PROXY
1707 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1709 struct ctl_iscsi_listen_params *cilp;
1710 struct sockaddr *sa;
1713 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1715 if (cfiscsi_softc.listener == NULL) {
1716 CFISCSI_DEBUG("no listener");
1717 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1718 ci->status = CTL_ISCSI_ERROR;
1722 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1724 CFISCSI_DEBUG("getsockaddr, error %d", error);
1725 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1726 ci->status = CTL_ISCSI_ERROR;
1730 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1731 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1734 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1735 snprintf(ci->error_str, sizeof(ci->error_str),
1736 "icl_listen_add failed, error %d", error);
1737 ci->status = CTL_ISCSI_ERROR;
1741 ci->status = CTL_ISCSI_OK;
1745 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1747 struct ctl_iscsi_accept_params *ciap;
1748 struct cfiscsi_session *cs;
1751 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1753 mtx_lock(&cfiscsi_softc.lock);
1755 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1756 if (cs->cs_waiting_for_ctld)
1761 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1763 mtx_unlock(&cfiscsi_softc.lock);
1764 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1765 ci->status = CTL_ISCSI_ERROR;
1769 mtx_unlock(&cfiscsi_softc.lock);
1771 cs->cs_waiting_for_ctld = false;
1772 cs->cs_login_phase = true;
1774 ciap->connection_id = cs->cs_id;
1775 ciap->portal_id = cs->cs_portal_id;
1776 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1777 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1778 cs->cs_initiator_sa->sa_len);
1780 snprintf(ci->error_str, sizeof(ci->error_str),
1781 "copyout failed with error %d", error);
1782 ci->status = CTL_ISCSI_ERROR;
1786 ci->status = CTL_ISCSI_OK;
1790 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1792 struct ctl_iscsi_send_params *cisp;
1793 struct cfiscsi_session *cs;
1799 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1801 mtx_lock(&cfiscsi_softc.lock);
1802 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1803 if (cs->cs_id == cisp->connection_id)
1807 mtx_unlock(&cfiscsi_softc.lock);
1808 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1809 ci->status = CTL_ISCSI_ERROR;
1812 mtx_unlock(&cfiscsi_softc.lock);
1815 if (cs->cs_login_phase == false)
1819 if (cs->cs_terminating) {
1820 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1821 ci->status = CTL_ISCSI_ERROR;
1825 datalen = cisp->data_segment_len;
1829 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1830 if (datalen > 65535) {
1831 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1832 ci->status = CTL_ISCSI_ERROR;
1836 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1837 error = copyin(cisp->data_segment, data, datalen);
1839 free(data, M_CFISCSI);
1840 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1841 ci->status = CTL_ISCSI_ERROR;
1846 ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1847 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1849 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1850 free(data, M_CFISCSI);
1852 CFISCSI_SESSION_LOCK(cs);
1854 CFISCSI_SESSION_UNLOCK(cs);
1855 ci->status = CTL_ISCSI_OK;
1859 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1861 struct ctl_iscsi_receive_params *cirp;
1862 struct cfiscsi_session *cs;
1867 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1869 mtx_lock(&cfiscsi_softc.lock);
1870 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1871 if (cs->cs_id == cirp->connection_id)
1875 mtx_unlock(&cfiscsi_softc.lock);
1876 snprintf(ci->error_str, sizeof(ci->error_str),
1877 "connection not found");
1878 ci->status = CTL_ISCSI_ERROR;
1881 mtx_unlock(&cfiscsi_softc.lock);
1884 if (is->is_login_phase == false)
1888 CFISCSI_SESSION_LOCK(cs);
1889 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
1890 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
1892 CFISCSI_SESSION_UNLOCK(cs);
1893 snprintf(ci->error_str, sizeof(ci->error_str),
1894 "interrupted by signal");
1895 ci->status = CTL_ISCSI_ERROR;
1900 if (cs->cs_terminating) {
1901 CFISCSI_SESSION_UNLOCK(cs);
1902 snprintf(ci->error_str, sizeof(ci->error_str),
1903 "connection terminating");
1904 ci->status = CTL_ISCSI_ERROR;
1907 ip = cs->cs_login_pdu;
1908 cs->cs_login_pdu = NULL;
1909 CFISCSI_SESSION_UNLOCK(cs);
1911 if (ip->ip_data_len > cirp->data_segment_len) {
1913 snprintf(ci->error_str, sizeof(ci->error_str),
1914 "data segment too big");
1915 ci->status = CTL_ISCSI_ERROR;
1919 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1920 if (ip->ip_data_len > 0) {
1921 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1922 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1923 copyout(data, cirp->data_segment, ip->ip_data_len);
1924 free(data, M_CFISCSI);
1928 ci->status = CTL_ISCSI_OK;
1931 #endif /* !ICL_KERNEL_PROXY */
1934 cfiscsi_ioctl_port_create(struct ctl_req *req)
1936 struct cfiscsi_target *ct;
1937 struct ctl_port *port;
1938 const char *target, *alias, *tag;
1939 struct scsi_vpd_id_descriptor *desc;
1941 int retval, len, idlen;
1943 ctl_init_opts(&opts, req->num_args, req->kern_args);
1944 target = ctl_get_opt(&opts, "cfiscsi_target");
1945 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
1946 tag = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
1947 if (target == NULL || tag == NULL) {
1948 req->status = CTL_LUN_ERROR;
1949 snprintf(req->error_str, sizeof(req->error_str),
1950 "Missing required argument");
1951 ctl_free_opts(&opts);
1954 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias);
1956 req->status = CTL_LUN_ERROR;
1957 snprintf(req->error_str, sizeof(req->error_str),
1958 "failed to create target \"%s\"", target);
1959 ctl_free_opts(&opts);
1962 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
1963 req->status = CTL_LUN_ERROR;
1964 snprintf(req->error_str, sizeof(req->error_str),
1965 "target \"%s\" already exist", target);
1966 cfiscsi_target_release(ct);
1967 ctl_free_opts(&opts);
1970 port = &ct->ct_port;
1971 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
1974 port->frontend = &cfiscsi_frontend;
1975 port->port_type = CTL_PORT_ISCSI;
1976 /* XXX KDM what should the real number be here? */
1977 port->num_requested_ctl_io = 4096;
1978 port->port_name = "iscsi";
1979 port->virtual_port = strtoul(tag, NULL, 0);
1980 port->port_online = cfiscsi_online;
1981 port->port_offline = cfiscsi_offline;
1982 port->port_info = cfiscsi_info;
1983 port->onoff_arg = ct;
1984 port->lun_enable = cfiscsi_lun_enable;
1985 port->lun_disable = cfiscsi_lun_disable;
1986 port->lun_map = cfiscsi_lun_map;
1987 port->targ_lun_arg = ct;
1988 port->fe_datamove = cfiscsi_datamove;
1989 port->fe_done = cfiscsi_done;
1991 /* XXX KDM what should we report here? */
1992 /* XXX These should probably be fetched from CTL. */
1993 port->max_targets = 1;
1994 port->max_target_id = 15;
1996 port->options = opts;
1999 /* Generate Port ID. */
2000 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2001 idlen = roundup2(idlen, 4);
2002 len = sizeof(struct scsi_vpd_device_id) + idlen;
2003 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2004 M_CTL, M_WAITOK | M_ZERO);
2005 port->port_devid->len = len;
2006 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2007 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2008 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2009 SVPD_ID_TYPE_SCSI_NAME;
2010 desc->length = idlen;
2011 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x",
2012 target, port->virtual_port);
2014 /* Generate Target ID. */
2015 idlen = strlen(target) + 1;
2016 idlen = roundup2(idlen, 4);
2017 len = sizeof(struct scsi_vpd_device_id) + idlen;
2018 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2019 M_CTL, M_WAITOK | M_ZERO);
2020 port->target_devid->len = len;
2021 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2022 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2023 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2024 SVPD_ID_TYPE_SCSI_NAME;
2025 desc->length = idlen;
2026 strlcpy(desc->identifier, target, idlen);
2028 retval = ctl_port_register(port, /*master_SC*/ 1);
2030 ctl_free_opts(&port->options);
2031 cfiscsi_target_release(ct);
2032 free(port->port_devid, M_CFISCSI);
2033 free(port->target_devid, M_CFISCSI);
2034 req->status = CTL_LUN_ERROR;
2035 snprintf(req->error_str, sizeof(req->error_str),
2036 "ctl_frontend_register() failed with error %d", retval);
2040 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2041 req->status = CTL_LUN_OK;
2042 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2043 sizeof(port->targ_port)); //XXX
2047 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2049 struct cfiscsi_target *ct;
2053 ctl_init_opts(&opts, req->num_args, req->kern_args);
2054 target = ctl_get_opt(&opts, "cfiscsi_target");
2055 if (target == NULL) {
2056 ctl_free_opts(&opts);
2057 req->status = CTL_LUN_ERROR;
2058 snprintf(req->error_str, sizeof(req->error_str),
2059 "Missing required argument");
2062 ct = cfiscsi_target_find(&cfiscsi_softc, target);
2064 ctl_free_opts(&opts);
2065 req->status = CTL_LUN_ERROR;
2066 snprintf(req->error_str, sizeof(req->error_str),
2067 "can't find target \"%s\"", target);
2070 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2071 ctl_free_opts(&opts);
2072 req->status = CTL_LUN_ERROR;
2073 snprintf(req->error_str, sizeof(req->error_str),
2074 "target \"%s\" is already dying", target);
2077 ctl_free_opts(&opts);
2079 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2080 ctl_port_offline(&ct->ct_port);
2081 cfiscsi_target_release(ct);
2082 cfiscsi_target_release(ct);
2086 cfiscsi_ioctl(struct cdev *dev,
2087 u_long cmd, caddr_t addr, int flag, struct thread *td)
2089 struct ctl_iscsi *ci;
2090 struct ctl_req *req;
2092 if (cmd == CTL_PORT_REQ) {
2093 req = (struct ctl_req *)addr;
2094 switch (req->reqtype) {
2095 case CTL_REQ_CREATE:
2096 cfiscsi_ioctl_port_create(req);
2098 case CTL_REQ_REMOVE:
2099 cfiscsi_ioctl_port_remove(req);
2102 req->status = CTL_LUN_ERROR;
2103 snprintf(req->error_str, sizeof(req->error_str),
2104 "Unsupported request type %d", req->reqtype);
2109 if (cmd != CTL_ISCSI)
2112 ci = (struct ctl_iscsi *)addr;
2114 case CTL_ISCSI_HANDOFF:
2115 cfiscsi_ioctl_handoff(ci);
2117 case CTL_ISCSI_LIST:
2118 cfiscsi_ioctl_list(ci);
2120 case CTL_ISCSI_TERMINATE:
2121 cfiscsi_ioctl_terminate(ci);
2123 case CTL_ISCSI_LOGOUT:
2124 cfiscsi_ioctl_logout(ci);
2126 #ifdef ICL_KERNEL_PROXY
2127 case CTL_ISCSI_LISTEN:
2128 cfiscsi_ioctl_listen(ci);
2130 case CTL_ISCSI_ACCEPT:
2131 cfiscsi_ioctl_accept(ci);
2133 case CTL_ISCSI_SEND:
2134 cfiscsi_ioctl_send(ci);
2136 case CTL_ISCSI_RECEIVE:
2137 cfiscsi_ioctl_receive(ci);
2140 case CTL_ISCSI_LISTEN:
2141 case CTL_ISCSI_ACCEPT:
2142 case CTL_ISCSI_SEND:
2143 case CTL_ISCSI_RECEIVE:
2144 ci->status = CTL_ISCSI_ERROR;
2145 snprintf(ci->error_str, sizeof(ci->error_str),
2146 "%s: CTL compiled without ICL_KERNEL_PROXY",
2149 #endif /* !ICL_KERNEL_PROXY */
2151 ci->status = CTL_ISCSI_ERROR;
2152 snprintf(ci->error_str, sizeof(ci->error_str),
2153 "%s: invalid iSCSI request type %d", __func__, ci->type);
2161 cfiscsi_target_hold(struct cfiscsi_target *ct)
2164 refcount_acquire(&ct->ct_refcount);
2168 cfiscsi_target_release(struct cfiscsi_target *ct)
2170 struct cfiscsi_softc *softc;
2172 softc = ct->ct_softc;
2173 mtx_lock(&softc->lock);
2174 if (refcount_release(&ct->ct_refcount)) {
2175 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2176 mtx_unlock(&softc->lock);
2177 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2178 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2179 if (ctl_port_deregister(&ct->ct_port) != 0)
2180 printf("%s: ctl_port_deregister() failed\n",
2183 free(ct, M_CFISCSI);
2187 mtx_unlock(&softc->lock);
2190 static struct cfiscsi_target *
2191 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2193 struct cfiscsi_target *ct;
2195 mtx_lock(&softc->lock);
2196 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2197 if (strcmp(name, ct->ct_name) != 0 ||
2198 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2200 cfiscsi_target_hold(ct);
2201 mtx_unlock(&softc->lock);
2204 mtx_unlock(&softc->lock);
2209 static struct cfiscsi_target *
2210 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2213 struct cfiscsi_target *ct, *newct;
2216 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2219 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2221 mtx_lock(&softc->lock);
2222 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2223 if (strcmp(name, ct->ct_name) != 0 ||
2224 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2226 cfiscsi_target_hold(ct);
2227 mtx_unlock(&softc->lock);
2228 free(newct, M_CFISCSI);
2232 for (i = 0; i < CTL_MAX_LUNS; i++)
2233 newct->ct_luns[i] = UINT32_MAX;
2235 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2237 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2238 refcount_init(&newct->ct_refcount, 1);
2239 newct->ct_softc = softc;
2240 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2241 mtx_unlock(&softc->lock);
2247 * Takes LUN from the target space and returns LUN from the CTL space.
2250 cfiscsi_lun_map(void *arg, uint32_t lun)
2252 struct cfiscsi_target *ct = arg;
2254 if (lun >= CTL_MAX_LUNS) {
2255 CFISCSI_DEBUG("requested lun number %d is higher "
2256 "than maximum %d", lun, CTL_MAX_LUNS - 1);
2257 return (UINT32_MAX);
2259 return (ct->ct_luns[lun]);
2263 cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2264 unsigned long lun_id, unsigned long ctl_lun_id)
2267 if (lun_id >= CTL_MAX_LUNS) {
2268 CFISCSI_WARN("requested lun number %ld is higher "
2269 "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2273 if (ct->ct_luns[lun_id] < CTL_MAX_LUNS) {
2275 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2276 * when the LUN is created, and a second time just before
2277 * the port is brought online; don't emit warnings
2280 if (ct->ct_luns[lun_id] == ctl_lun_id)
2282 CFISCSI_WARN("lun %ld already allocated", lun_id);
2287 CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2288 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2291 ct->ct_luns[lun_id] = ctl_lun_id;
2297 cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2299 struct cfiscsi_softc *softc;
2300 struct cfiscsi_target *ct;
2301 const char *target = NULL;
2302 const char *lun = NULL;
2305 ct = (struct cfiscsi_target *)arg;
2306 softc = ct->ct_softc;
2308 target = ctl_get_opt(&control_softc->ctl_luns[lun_id]->be_lun->options,
2310 lun = ctl_get_opt(&control_softc->ctl_luns[lun_id]->be_lun->options,
2313 if (target == NULL && lun == NULL)
2316 if (target == NULL || lun == NULL) {
2317 CFISCSI_WARN("lun added with cfiscsi_target, but without "
2318 "cfiscsi_lun, or the other way around; ignoring");
2322 if (strcmp(target, ct->ct_name) != 0)
2325 tmp = strtoul(lun, NULL, 10);
2326 cfiscsi_target_set_lun(ct, tmp, lun_id);
2331 cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2333 struct cfiscsi_softc *softc;
2334 struct cfiscsi_target *ct;
2337 ct = (struct cfiscsi_target *)arg;
2338 softc = ct->ct_softc;
2340 mtx_lock(&softc->lock);
2341 for (i = 0; i < CTL_MAX_LUNS; i++) {
2342 if (ct->ct_luns[i] != lun_id)
2344 ct->ct_luns[i] = UINT32_MAX;
2347 mtx_unlock(&softc->lock);
2352 cfiscsi_datamove_in(union ctl_io *io)
2354 struct cfiscsi_session *cs;
2355 struct icl_pdu *request, *response;
2356 const struct iscsi_bhs_scsi_command *bhssc;
2357 struct iscsi_bhs_data_in *bhsdi;
2358 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2359 size_t len, expected_len, sg_len, buffer_offset;
2360 const char *sg_addr;
2361 int ctl_sg_count, error, i;
2363 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2364 cs = PDU_SESSION(request);
2366 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2367 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2368 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2369 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2371 if (io->scsiio.kern_sg_entries > 0) {
2372 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2373 ctl_sg_count = io->scsiio.kern_sg_entries;
2375 ctl_sglist = &ctl_sg_entry;
2376 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2377 ctl_sglist->len = io->scsiio.kern_data_len;
2382 * This is the total amount of data to be transferred within the current
2383 * SCSI command. We need to record it so that we can properly report
2384 * underflow/underflow.
2386 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2389 * This is the offset within the current SCSI command; for the first
2390 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2391 * it will be the sum of lengths of previous ones.
2393 buffer_offset = io->scsiio.kern_rel_offset;
2396 * This is the transfer length expected by the initiator. In theory,
2397 * it could be different from the correct amount of data from the SCSI
2398 * point of view, even if that doesn't make any sense.
2400 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2402 if (expected_len != io->scsiio.kern_total_len) {
2403 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2404 "actual length %zd", expected_len,
2405 (size_t)io->scsiio.kern_total_len);
2409 if (buffer_offset >= expected_len) {
2411 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2412 "already sent the expected len", buffer_offset);
2414 io->scsiio.be_move_done(io);
2424 if (response == NULL) {
2425 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2426 if (response == NULL) {
2427 CFISCSI_SESSION_WARN(cs, "failed to "
2428 "allocate memory; dropping connection");
2429 ctl_set_busy(&io->scsiio);
2430 io->scsiio.be_move_done(io);
2431 cfiscsi_session_terminate(cs);
2434 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2435 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2436 bhsdi->bhsdi_initiator_task_tag =
2437 bhssc->bhssc_initiator_task_tag;
2438 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2439 PDU_EXPDATASN(request)++;
2440 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2443 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2445 sg_addr = ctl_sglist[i].addr;
2446 sg_len = ctl_sglist[i].len;
2447 KASSERT(sg_len > 0, ("sg_len <= 0"));
2453 * Truncate to maximum data segment length.
2455 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2456 ("ip_data_len %zd >= max_data_segment_length %zd",
2457 response->ip_data_len, cs->cs_max_data_segment_length));
2458 if (response->ip_data_len + len >
2459 cs->cs_max_data_segment_length) {
2460 len = cs->cs_max_data_segment_length -
2461 response->ip_data_len;
2462 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2467 * Truncate to expected data transfer length.
2469 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2470 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2471 buffer_offset, response->ip_data_len, expected_len));
2472 if (buffer_offset + response->ip_data_len + len > expected_len) {
2473 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2474 "to expected data transfer length %zd",
2475 buffer_offset + response->ip_data_len + len, expected_len);
2476 len = expected_len - (buffer_offset + response->ip_data_len);
2477 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2481 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2483 CFISCSI_SESSION_WARN(cs, "failed to "
2484 "allocate memory; dropping connection");
2485 icl_pdu_free(response);
2486 ctl_set_busy(&io->scsiio);
2487 io->scsiio.be_move_done(io);
2488 cfiscsi_session_terminate(cs);
2494 KASSERT(buffer_offset + request->ip_data_len <= expected_len,
2495 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2496 buffer_offset, request->ip_data_len, expected_len));
2497 if (buffer_offset + request->ip_data_len == expected_len) {
2499 * Already have the amount of data the initiator wanted.
2506 * End of scatter-gather segment;
2507 * proceed to the next one...
2509 if (i == ctl_sg_count - 1) {
2511 * ... unless this was the last one.
2518 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2520 * Can't stuff more data into the current PDU;
2521 * queue it. Note that's not enough to check
2522 * for kern_data_resid == 0 instead; there
2523 * may be several Data-In PDUs for the final
2524 * call to cfiscsi_datamove(), and we want
2525 * to set the F flag only on the last of them.
2527 buffer_offset += response->ip_data_len;
2528 if (buffer_offset == io->scsiio.kern_total_len ||
2529 buffer_offset == expected_len)
2530 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2531 cfiscsi_pdu_queue(response);
2536 if (response != NULL) {
2537 buffer_offset += response->ip_data_len;
2538 if (buffer_offset == io->scsiio.kern_total_len ||
2539 buffer_offset == expected_len)
2540 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2541 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2542 cfiscsi_pdu_queue(response);
2545 io->scsiio.be_move_done(io);
2549 cfiscsi_datamove_out(union ctl_io *io)
2551 struct cfiscsi_session *cs;
2552 struct icl_pdu *request, *response;
2553 const struct iscsi_bhs_scsi_command *bhssc;
2554 struct iscsi_bhs_r2t *bhsr2t;
2555 struct cfiscsi_data_wait *cdw;
2556 uint32_t target_transfer_tag;
2559 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2560 cs = PDU_SESSION(request);
2562 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2563 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2564 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2565 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2568 * We need to record it so that we can properly report
2569 * underflow/underflow.
2571 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2574 * We hadn't received anything during this datamove yet.
2576 io->scsiio.ext_data_filled = 0;
2578 target_transfer_tag =
2579 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2582 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2583 "task tag 0x%x, target transfer tag 0x%x",
2584 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2586 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2588 CFISCSI_SESSION_WARN(cs, "failed to "
2589 "allocate memory; dropping connection");
2590 ctl_set_busy(&io->scsiio);
2591 io->scsiio.be_move_done(io);
2592 cfiscsi_session_terminate(cs);
2595 cdw->cdw_ctl_io = io;
2596 cdw->cdw_target_transfer_tag = target_transfer_tag;
2597 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2599 if (cs->cs_immediate_data && io->scsiio.kern_rel_offset <
2600 icl_pdu_data_segment_length(request)) {
2601 done = cfiscsi_handle_data_segment(request, cdw);
2603 uma_zfree(cfiscsi_data_wait_zone, cdw);
2604 io->scsiio.be_move_done(io);
2609 CFISCSI_SESSION_LOCK(cs);
2610 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2611 CFISCSI_SESSION_UNLOCK(cs);
2614 * XXX: We should limit the number of outstanding R2T PDUs
2615 * per task to MaxOutstandingR2T.
2617 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2618 if (response == NULL) {
2619 CFISCSI_SESSION_WARN(cs, "failed to "
2620 "allocate memory; dropping connection");
2621 ctl_set_busy(&io->scsiio);
2622 io->scsiio.be_move_done(io);
2623 cfiscsi_session_terminate(cs);
2626 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2627 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2628 bhsr2t->bhsr2t_flags = 0x80;
2629 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2630 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2631 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2633 * XXX: Here we assume that cfiscsi_datamove() won't ever
2634 * be running concurrently on several CPUs for a given
2637 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2638 PDU_R2TSN(request)++;
2640 * This is the offset within the current SCSI command;
2641 * i.e. for the first call of datamove(), it will be 0,
2642 * and for subsequent ones it will be the sum of lengths
2645 * The ext_data_filled is to account for unsolicited
2646 * (immediate) data that might have already arrived.
2648 bhsr2t->bhsr2t_buffer_offset =
2649 htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2651 * This is the total length (sum of S/G lengths) this call
2652 * to cfiscsi_datamove() is supposed to handle.
2654 * XXX: Limit it to MaxBurstLength.
2656 bhsr2t->bhsr2t_desired_data_transfer_length =
2657 htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2658 cfiscsi_pdu_queue(response);
2662 cfiscsi_datamove(union ctl_io *io)
2665 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2666 cfiscsi_datamove_in(io);
2668 cfiscsi_datamove_out(io);
2672 cfiscsi_scsi_command_done(union ctl_io *io)
2674 struct icl_pdu *request, *response;
2675 struct iscsi_bhs_scsi_command *bhssc;
2676 struct iscsi_bhs_scsi_response *bhssr;
2678 struct cfiscsi_data_wait *cdw;
2680 struct cfiscsi_session *cs;
2681 uint16_t sense_length;
2683 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2684 cs = PDU_SESSION(request);
2685 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2686 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2687 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2688 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2690 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2691 // bhssc->bhssc_initiator_task_tag);
2694 CFISCSI_SESSION_LOCK(cs);
2695 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2696 KASSERT(bhssc->bhssc_initiator_task_tag !=
2697 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2698 CFISCSI_SESSION_UNLOCK(cs);
2702 * Do not return status for aborted commands.
2703 * There are exceptions, but none supported by CTL yet.
2705 if (io->io_hdr.status == CTL_CMD_ABORTED) {
2707 icl_pdu_free(request);
2711 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2712 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2713 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2714 bhssr->bhssr_flags = 0x80;
2716 * XXX: We don't deal with bidirectional under/overflows;
2717 * does anything actually support those?
2719 if (PDU_TOTAL_TRANSFER_LEN(request) <
2720 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2721 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2722 bhssr->bhssr_residual_count =
2723 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2724 PDU_TOTAL_TRANSFER_LEN(request));
2725 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2726 // ntohl(bhssr->bhssr_residual_count));
2727 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2728 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2729 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2730 bhssr->bhssr_residual_count =
2731 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2732 ntohl(bhssc->bhssc_expected_data_transfer_length));
2733 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2734 // ntohl(bhssr->bhssr_residual_count));
2736 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2737 bhssr->bhssr_status = io->scsiio.scsi_status;
2738 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2739 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2741 if (io->scsiio.sense_len > 0) {
2743 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2744 io->scsiio.sense_len);
2746 sense_length = htons(io->scsiio.sense_len);
2747 icl_pdu_append_data(response,
2748 &sense_length, sizeof(sense_length), M_WAITOK);
2749 icl_pdu_append_data(response,
2750 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2754 icl_pdu_free(request);
2755 cfiscsi_pdu_queue(response);
2759 cfiscsi_task_management_done(union ctl_io *io)
2761 struct icl_pdu *request, *response;
2762 struct iscsi_bhs_task_management_request *bhstmr;
2763 struct iscsi_bhs_task_management_response *bhstmr2;
2764 struct cfiscsi_data_wait *cdw, *tmpcdw;
2765 struct cfiscsi_session *cs;
2767 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2768 cs = PDU_SESSION(request);
2769 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2770 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2771 ISCSI_BHS_OPCODE_TASK_REQUEST,
2772 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2775 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2776 bhstmr->bhstmr_initiator_task_tag,
2777 bhstmr->bhstmr_referenced_task_tag);
2780 if ((bhstmr->bhstmr_function & ~0x80) ==
2781 BHSTMR_FUNCTION_ABORT_TASK) {
2783 * Make sure we no longer wait for Data-Out for this command.
2785 CFISCSI_SESSION_LOCK(cs);
2786 TAILQ_FOREACH_SAFE(cdw,
2787 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2788 if (bhstmr->bhstmr_referenced_task_tag !=
2789 cdw->cdw_initiator_task_tag)
2793 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2794 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2796 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2798 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2799 uma_zfree(cfiscsi_data_wait_zone, cdw);
2801 CFISCSI_SESSION_UNLOCK(cs);
2804 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2805 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2807 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2808 bhstmr2->bhstmr_flags = 0x80;
2809 if (io->io_hdr.status == CTL_SUCCESS) {
2810 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2813 * XXX: How to figure out what exactly went wrong? iSCSI spec
2814 * expects us to provide detailed error, e.g. "Task does
2815 * not exist" or "LUN does not exist".
2817 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2818 bhstmr2->bhstmr_response =
2819 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2821 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2824 icl_pdu_free(request);
2825 cfiscsi_pdu_queue(response);
2829 cfiscsi_done(union ctl_io *io)
2831 struct icl_pdu *request;
2832 struct cfiscsi_session *cs;
2834 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2835 ("invalid CTL status %#x", io->io_hdr.status));
2837 if (io->io_hdr.io_type == CTL_IO_TASK &&
2838 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2840 * Implicit task termination has just completed; nothing to do.
2842 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2843 cs->cs_tasks_aborted = true;
2844 refcount_release(&cs->cs_outstanding_ctl_pdus);
2845 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2850 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2851 cs = PDU_SESSION(request);
2852 refcount_release(&cs->cs_outstanding_ctl_pdus);
2854 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2855 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2856 cfiscsi_scsi_command_done(io);
2858 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2859 cfiscsi_task_management_done(io);
2862 panic("cfiscsi_done called with wrong opcode 0x%x",
2863 request->ip_bhs->bhs_opcode);