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 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_targ_enable(void *arg, struct ctl_id targ_id);
153 static int cfiscsi_targ_disable(void *arg, struct ctl_id targ_id);
154 static int cfiscsi_lun_enable(void *arg,
155 struct ctl_id target_id, int lun_id);
156 static int cfiscsi_lun_disable(void *arg,
157 struct ctl_id target_id, int lun_id);
158 static int cfiscsi_ioctl(struct cdev *dev,
159 u_long cmd, caddr_t addr, int flag, struct thread *td);
160 static int cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len);
161 static void cfiscsi_datamove(union ctl_io *io);
162 static void cfiscsi_done(union ctl_io *io);
163 static uint32_t cfiscsi_map_lun(void *arg, uint32_t lun);
164 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
165 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
166 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
167 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
168 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
169 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
170 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
171 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
172 *softc, const char *name);
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 int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *);
181 static moduledata_t cfiscsi_moduledata = {
183 cfiscsi_module_event_handler,
187 DECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
188 MODULE_VERSION(ctlcfiscsi, 1);
189 MODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1);
190 MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
192 static struct icl_pdu *
193 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
196 return (icl_pdu_new_bhs(request->ip_conn, flags));
200 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
202 const struct iscsi_bhs_scsi_command *bhssc;
203 struct cfiscsi_session *cs;
204 uint32_t cmdsn, expstatsn;
206 cs = PDU_SESSION(request);
209 * Every incoming PDU - not just NOP-Out - resets the ping timer.
210 * The purpose of the timeout is to reset the connection when it stalls;
211 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
219 * Data-Out PDUs don't contain CmdSN.
221 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
222 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
226 * We're only using fields common for all the request
227 * (initiator -> target) PDUs.
229 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
230 cmdsn = ntohl(bhssc->bhssc_cmdsn);
231 expstatsn = ntohl(bhssc->bhssc_expstatsn);
233 CFISCSI_SESSION_LOCK(cs);
235 if (expstatsn != cs->cs_statsn) {
236 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
237 "while current StatSN is %d", expstatsn,
243 * The target MUST silently ignore any non-immediate command outside
246 if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
247 CFISCSI_SESSION_UNLOCK(cs);
248 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
249 "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
253 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
256 CFISCSI_SESSION_UNLOCK(cs);
262 cfiscsi_pdu_handle(struct icl_pdu *request)
264 struct cfiscsi_session *cs;
267 cs = PDU_SESSION(request);
269 ignore = cfiscsi_pdu_update_cmdsn(request);
271 icl_pdu_free(request);
276 * Handle the PDU; this includes e.g. receiving the remaining
277 * part of PDU and submitting the SCSI command to CTL
278 * or queueing a reply. The handling routine is responsible
279 * for freeing the PDU when it's no longer needed.
281 switch (request->ip_bhs->bhs_opcode &
282 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
283 case ISCSI_BHS_OPCODE_NOP_OUT:
284 cfiscsi_pdu_handle_nop_out(request);
286 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
287 cfiscsi_pdu_handle_scsi_command(request);
289 case ISCSI_BHS_OPCODE_TASK_REQUEST:
290 cfiscsi_pdu_handle_task_request(request);
292 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
293 cfiscsi_pdu_handle_data_out(request);
295 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
296 cfiscsi_pdu_handle_logout_request(request);
299 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
300 "opcode 0x%x; dropping connection",
301 request->ip_bhs->bhs_opcode);
302 icl_pdu_free(request);
303 cfiscsi_session_terminate(cs);
309 cfiscsi_receive_callback(struct icl_pdu *request)
311 struct cfiscsi_session *cs;
313 cs = PDU_SESSION(request);
315 #ifdef ICL_KERNEL_PROXY
316 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
317 if (cs->cs_login_pdu == NULL)
318 cs->cs_login_pdu = request;
320 icl_pdu_free(request);
321 cv_signal(&cs->cs_login_cv);
326 cfiscsi_pdu_handle(request);
330 cfiscsi_error_callback(struct icl_conn *ic)
332 struct cfiscsi_session *cs;
334 cs = CONN_SESSION(ic);
336 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
337 cfiscsi_session_terminate(cs);
341 cfiscsi_pdu_prepare(struct icl_pdu *response)
343 struct cfiscsi_session *cs;
344 struct iscsi_bhs_scsi_response *bhssr;
345 bool advance_statsn = true;
347 cs = PDU_SESSION(response);
349 CFISCSI_SESSION_LOCK_ASSERT(cs);
352 * We're only using fields common for all the response
353 * (target -> initiator) PDUs.
355 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
358 * 10.8.3: "The StatSN for this connection is not advanced
359 * after this PDU is sent."
361 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
362 advance_statsn = false;
365 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
366 * StatSN for the connection is not advanced after this PDU is sent."
368 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
369 bhssr->bhssr_initiator_task_tag == 0xffffffff)
370 advance_statsn = false;
373 * See the comment below - StatSN is not meaningful and must
376 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN)
377 advance_statsn = false;
380 * 10.7.3: "The fields StatSN, Status, and Residual Count
381 * only have meaningful content if the S bit is set to 1."
383 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN)
384 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
385 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
386 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
395 cfiscsi_pdu_queue(struct icl_pdu *response)
397 struct cfiscsi_session *cs;
399 cs = PDU_SESSION(response);
401 CFISCSI_SESSION_LOCK(cs);
402 cfiscsi_pdu_prepare(response);
403 icl_pdu_queue(response);
404 CFISCSI_SESSION_UNLOCK(cs);
408 cfiscsi_decode_lun(uint64_t encoded)
414 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
415 * but is in fact an evil, multidimensional structure defined
416 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
418 memcpy(lun, &encoded, sizeof(lun));
419 switch (lun[0] & 0xC0) {
421 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
422 lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
423 CFISCSI_WARN("malformed LUN "
424 "(peripheral device addressing method): 0x%jx",
432 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
433 lun[6] != 0 || lun[7] != 0) {
434 CFISCSI_WARN("malformed LUN "
435 "(flat address space addressing method): 0x%jx",
440 result = ((lun[0] & 0x3f) << 8) + lun[1];
443 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
444 lun[6] != 0 || lun[7] != 0) {
445 CFISCSI_WARN("malformed LUN (extended flat "
446 "address space addressing method): 0x%jx",
451 result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
453 CFISCSI_WARN("unsupported LUN format 0x%jx",
463 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
465 struct cfiscsi_session *cs;
466 struct iscsi_bhs_nop_out *bhsno;
467 struct iscsi_bhs_nop_in *bhsni;
468 struct icl_pdu *response;
473 cs = PDU_SESSION(request);
474 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
476 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
478 * Nothing to do, iscsi_pdu_update_statsn() already
479 * zeroed the timeout.
481 icl_pdu_free(request);
485 datasize = icl_pdu_data_segment_length(request);
487 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
489 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
490 "dropping connection");
491 icl_pdu_free(request);
492 cfiscsi_session_terminate(cs);
495 icl_pdu_get_data(request, 0, data, datasize);
498 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
499 if (response == NULL) {
500 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
501 "droppping connection");
502 free(data, M_CFISCSI);
503 icl_pdu_free(request);
504 cfiscsi_session_terminate(cs);
507 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
508 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
509 bhsni->bhsni_flags = 0x80;
510 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
511 bhsni->bhsni_target_transfer_tag = 0xffffffff;
513 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
515 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
516 "dropping connection");
517 free(data, M_CFISCSI);
518 icl_pdu_free(request);
519 icl_pdu_free(response);
520 cfiscsi_session_terminate(cs);
523 free(data, M_CFISCSI);
526 icl_pdu_free(request);
527 cfiscsi_pdu_queue(response);
531 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
533 struct iscsi_bhs_scsi_command *bhssc;
534 struct cfiscsi_session *cs;
538 cs = PDU_SESSION(request);
539 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
540 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
541 // bhssc->bhssc_initiator_task_tag);
543 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
544 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
545 "ImmediateData=No; dropping connection");
546 icl_pdu_free(request);
547 cfiscsi_session_terminate(cs);
550 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
552 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; "
553 "dropping connection");
554 icl_pdu_free(request);
555 cfiscsi_session_terminate(cs);
559 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
560 io->io_hdr.io_type = CTL_IO_SCSI;
561 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
562 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
563 io->io_hdr.nexus.targ_target.id = 0;
564 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
565 io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
566 io->io_hdr.nexus.lun_map_arg = cs;
567 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
568 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
569 case BHSSC_FLAGS_ATTR_UNTAGGED:
570 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
572 case BHSSC_FLAGS_ATTR_SIMPLE:
573 io->scsiio.tag_type = CTL_TAG_SIMPLE;
575 case BHSSC_FLAGS_ATTR_ORDERED:
576 io->scsiio.tag_type = CTL_TAG_ORDERED;
578 case BHSSC_FLAGS_ATTR_HOQ:
579 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
581 case BHSSC_FLAGS_ATTR_ACA:
582 io->scsiio.tag_type = CTL_TAG_ACA;
585 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
586 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
587 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
590 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
591 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
592 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
593 error = ctl_queue(io);
594 if (error != CTL_RETVAL_COMPLETE) {
595 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
596 "dropping connection", error);
598 refcount_release(&cs->cs_outstanding_ctl_pdus);
599 icl_pdu_free(request);
600 cfiscsi_session_terminate(cs);
605 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
607 struct iscsi_bhs_task_management_request *bhstmr;
608 struct iscsi_bhs_task_management_response *bhstmr2;
609 struct icl_pdu *response;
610 struct cfiscsi_session *cs;
614 cs = PDU_SESSION(request);
615 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
616 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
618 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;"
619 "dropping connection");
620 icl_pdu_free(request);
621 cfiscsi_session_terminate(cs);
625 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
626 io->io_hdr.io_type = CTL_IO_TASK;
627 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
628 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
629 io->io_hdr.nexus.targ_target.id = 0;
630 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
631 io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
632 io->io_hdr.nexus.lun_map_arg = cs;
633 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
635 switch (bhstmr->bhstmr_function & ~0x80) {
636 case BHSTMR_FUNCTION_ABORT_TASK:
638 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
640 io->taskio.task_action = CTL_TASK_ABORT_TASK;
641 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
643 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
645 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
647 io->taskio.task_action = CTL_TASK_LUN_RESET;
649 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
651 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
653 io->taskio.task_action = CTL_TASK_TARGET_RESET;
656 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
657 bhstmr->bhstmr_function & ~0x80);
660 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
661 if (response == NULL) {
662 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
663 "dropping connection");
664 icl_pdu_free(request);
665 cfiscsi_session_terminate(cs);
668 bhstmr2 = (struct iscsi_bhs_task_management_response *)
670 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
671 bhstmr2->bhstmr_flags = 0x80;
672 bhstmr2->bhstmr_response =
673 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
674 bhstmr2->bhstmr_initiator_task_tag =
675 bhstmr->bhstmr_initiator_task_tag;
676 icl_pdu_free(request);
677 cfiscsi_pdu_queue(response);
681 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
682 error = ctl_queue(io);
683 if (error != CTL_RETVAL_COMPLETE) {
684 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
685 "dropping connection", error);
687 refcount_release(&cs->cs_outstanding_ctl_pdus);
688 icl_pdu_free(request);
689 cfiscsi_session_terminate(cs);
694 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
696 struct iscsi_bhs_data_out *bhsdo;
697 struct cfiscsi_session *cs;
698 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
699 size_t copy_len, len, off, buffer_offset;
703 cs = PDU_SESSION(request);
705 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
706 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
707 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
708 ISCSI_BHS_OPCODE_SCSI_COMMAND,
709 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
712 * We're only using fields common for Data-Out and SCSI Command PDUs.
714 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
716 io = cdw->cdw_ctl_io;
717 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
718 ("CTL_FLAG_DATA_IN"));
721 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
722 request->ip_data_len, io->scsiio.kern_total_len);
725 if (io->scsiio.kern_sg_entries > 0) {
726 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
727 ctl_sg_count = io->scsiio.kern_sg_entries;
729 ctl_sglist = &ctl_sg_entry;
730 ctl_sglist->addr = io->scsiio.kern_data_ptr;
731 ctl_sglist->len = io->scsiio.kern_data_len;
735 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
736 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
737 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
742 * Make sure the offset, as sent by the initiator, matches the offset
743 * we're supposed to be at in the scatter-gather list.
746 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
747 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
748 "expected %zd; dropping connection", buffer_offset,
749 (size_t)io->scsiio.kern_rel_offset +
750 (size_t)io->scsiio.ext_data_filled);
751 ctl_set_data_phase_error(&io->scsiio);
752 cfiscsi_session_terminate(cs);
757 * This is the offset within the PDU data segment, as opposed
758 * to buffer_offset, which is the offset within the task (SCSI
762 len = icl_pdu_data_segment_length(request);
765 * Iterate over the scatter/gather segments, filling them with data
766 * from the PDU data segment. Note that this can get called multiple
767 * times for one SCSI command; the cdw structure holds state for the
768 * scatter/gather list.
771 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
772 ("cdw->cdw_sg_index >= ctl_sg_count"));
773 if (cdw->cdw_sg_len == 0) {
774 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
775 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
777 KASSERT(off <= len, ("len > off"));
778 copy_len = len - off;
779 if (copy_len > cdw->cdw_sg_len)
780 copy_len = cdw->cdw_sg_len;
782 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
783 cdw->cdw_sg_addr += copy_len;
784 cdw->cdw_sg_len -= copy_len;
786 io->scsiio.ext_data_filled += copy_len;
788 if (cdw->cdw_sg_len == 0) {
790 * End of current segment.
792 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
794 * Last segment in scatter/gather list.
803 * End of PDU payload.
811 * In case of unsolicited data, it's possible that the buffer
812 * provided by CTL is smaller than negotiated FirstBurstLength.
813 * Just ignore the superfluous data; will ask for them with R2T
814 * on next call to cfiscsi_datamove().
816 * This obviously can only happen with SCSI Command PDU.
818 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
819 ISCSI_BHS_OPCODE_SCSI_COMMAND) {
820 CFISCSI_SESSION_DEBUG(cs, "received too much immediate "
821 "data: got %zd bytes, expected %zd",
822 icl_pdu_data_segment_length(request), off);
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 == io->scsiio.kern_data_len &&
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 != io->scsiio.kern_data_len &&
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,
852 io->scsiio.kern_data_len);
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 == io->scsiio.kern_data_len) {
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 io = cdw->cdw_ctl_io;
913 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
914 ("CTL_FLAG_DATA_IN"));
916 done = cfiscsi_handle_data_segment(request, cdw);
918 CFISCSI_SESSION_LOCK(cs);
919 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
920 CFISCSI_SESSION_UNLOCK(cs);
921 uma_zfree(cfiscsi_data_wait_zone, cdw);
922 io->scsiio.be_move_done(io);
925 icl_pdu_free(request);
929 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
931 struct iscsi_bhs_logout_request *bhslr;
932 struct iscsi_bhs_logout_response *bhslr2;
933 struct icl_pdu *response;
934 struct cfiscsi_session *cs;
936 cs = PDU_SESSION(request);
937 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
938 switch (bhslr->bhslr_reason & 0x7f) {
939 case BHSLR_REASON_CLOSE_SESSION:
940 case BHSLR_REASON_CLOSE_CONNECTION:
941 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
942 if (response == NULL) {
943 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
944 icl_pdu_free(request);
945 cfiscsi_session_terminate(cs);
948 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
949 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
950 bhslr2->bhslr_flags = 0x80;
951 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
952 bhslr2->bhslr_initiator_task_tag =
953 bhslr->bhslr_initiator_task_tag;
954 icl_pdu_free(request);
955 cfiscsi_pdu_queue(response);
956 cfiscsi_session_terminate(cs);
958 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
959 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
960 if (response == NULL) {
961 CFISCSI_SESSION_WARN(cs,
962 "failed to allocate memory; dropping connection");
963 icl_pdu_free(request);
964 cfiscsi_session_terminate(cs);
967 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
968 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
969 bhslr2->bhslr_flags = 0x80;
970 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
971 bhslr2->bhslr_initiator_task_tag =
972 bhslr->bhslr_initiator_task_tag;
973 icl_pdu_free(request);
974 cfiscsi_pdu_queue(response);
977 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
978 bhslr->bhslr_reason);
979 icl_pdu_free(request);
980 cfiscsi_session_terminate(cs);
986 cfiscsi_callout(void *context)
989 struct iscsi_bhs_nop_in *bhsni;
990 struct cfiscsi_session *cs;
994 if (cs->cs_terminating)
997 callout_schedule(&cs->cs_callout, 1 * hz);
999 atomic_add_int(&cs->cs_timeout, 1);
1001 #ifdef ICL_KERNEL_PROXY
1002 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1003 if (cs->cs_timeout > login_timeout) {
1004 CFISCSI_SESSION_WARN(cs, "login timed out after "
1005 "%d seconds; dropping connection", cs->cs_timeout);
1006 cfiscsi_session_terminate(cs);
1012 if (cs->cs_timeout >= ping_timeout) {
1013 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1014 "dropping connection", ping_timeout);
1015 cfiscsi_session_terminate(cs);
1020 * If the ping was reset less than one second ago - which means
1021 * that we've received some PDU during the last second - assume
1022 * the traffic flows correctly and don't bother sending a NOP-Out.
1024 * (It's 2 - one for one second, and one for incrementing is_timeout
1025 * earlier in this routine.)
1027 if (cs->cs_timeout < 2)
1030 cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1032 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1035 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1036 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1037 bhsni->bhsni_flags = 0x80;
1038 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1040 cfiscsi_pdu_queue(cp);
1044 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1046 struct cfiscsi_data_wait *cdw, *tmpcdw;
1051 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1053 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1057 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1058 io->io_hdr.io_type = CTL_IO_TASK;
1059 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1060 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1061 io->io_hdr.nexus.targ_target.id = 0;
1062 io->io_hdr.nexus.targ_lun = lun;
1063 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1064 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1065 error = ctl_queue(io);
1066 if (error != CTL_RETVAL_COMPLETE) {
1067 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1072 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1073 * just iterate over tasks that are waiting for something - data - and
1076 CFISCSI_SESSION_LOCK(cs);
1077 TAILQ_FOREACH_SAFE(cdw,
1078 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1079 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1081 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1085 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1086 io->io_hdr.io_type = CTL_IO_TASK;
1087 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1088 io->io_hdr.nexus.targ_port =
1089 cs->cs_target->ct_softc->fe.targ_port;
1090 io->io_hdr.nexus.targ_target.id = 0;
1091 //io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1092 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1093 io->taskio.task_action = CTL_TASK_ABORT_TASK;
1094 io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1095 error = ctl_queue(io);
1096 if (error != CTL_RETVAL_COMPLETE) {
1097 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1102 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1103 "0x%x", cdw->cdw_initiator_task_tag);
1105 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1106 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1107 uma_zfree(cfiscsi_data_wait_zone, cdw);
1109 CFISCSI_SESSION_UNLOCK(cs);
1114 cfiscsi_maintenance_thread(void *arg)
1116 struct cfiscsi_session *cs;
1121 CFISCSI_SESSION_LOCK(cs);
1122 if (cs->cs_terminating == false)
1123 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1124 CFISCSI_SESSION_UNLOCK(cs);
1126 if (cs->cs_terminating) {
1127 cfiscsi_session_terminate_tasks(cs);
1128 callout_drain(&cs->cs_callout);
1130 icl_conn_shutdown(cs->cs_conn);
1131 icl_conn_close(cs->cs_conn);
1133 cs->cs_terminating++;
1136 * XXX: We used to wait up to 30 seconds to deliver queued PDUs
1137 * to the initiator. We also tried hard to deliver SCSI Responses
1138 * for the aborted PDUs. We don't do that anymore. We might need
1142 cfiscsi_session_delete(cs);
1146 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1151 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1154 if (cs->cs_terminating != 0)
1156 cs->cs_terminating = 1;
1157 cv_signal(&cs->cs_maintenance_cv);
1158 #ifdef ICL_KERNEL_PROXY
1159 cv_signal(&cs->cs_login_cv);
1164 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1167 struct cfiscsi_softc *softc;
1169 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1171 softc = &cfiscsi_softc;
1173 mtx_lock(&softc->lock);
1174 for (i = 0; i < softc->max_initiators; i++) {
1175 if (softc->ctl_initids[i] == 0)
1178 if (i == softc->max_initiators) {
1179 CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1180 softc->max_initiators);
1181 mtx_unlock(&softc->lock);
1184 softc->ctl_initids[i] = 1;
1185 mtx_unlock(&softc->lock);
1188 CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1189 i, softc->max_initiators);
1191 cs->cs_ctl_initid = i;
1192 error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1194 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1195 mtx_lock(&softc->lock);
1196 softc->ctl_initids[cs->cs_ctl_initid] = 0;
1197 mtx_unlock(&softc->lock);
1198 cs->cs_ctl_initid = -1;
1206 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1209 struct cfiscsi_softc *softc;
1211 if (cs->cs_ctl_initid == -1)
1214 softc = &cfiscsi_softc;
1216 error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1218 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1221 mtx_lock(&softc->lock);
1222 softc->ctl_initids[cs->cs_ctl_initid] = 0;
1223 mtx_unlock(&softc->lock);
1224 cs->cs_ctl_initid = -1;
1227 static struct cfiscsi_session *
1228 cfiscsi_session_new(struct cfiscsi_softc *softc)
1230 struct cfiscsi_session *cs;
1233 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1235 CFISCSI_WARN("malloc failed");
1238 cs->cs_ctl_initid = -1;
1240 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1241 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1242 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1243 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1244 #ifdef ICL_KERNEL_PROXY
1245 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1248 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1249 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1250 cs->cs_conn->ic_error = cfiscsi_error_callback;
1251 cs->cs_conn->ic_prv0 = cs;
1253 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1255 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1256 free(cs, M_CFISCSI);
1260 mtx_lock(&softc->lock);
1261 cs->cs_id = softc->last_session_id + 1;
1262 softc->last_session_id++;
1263 mtx_unlock(&softc->lock);
1265 mtx_lock(&softc->lock);
1266 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1267 mtx_unlock(&softc->lock);
1270 * Start pinging the initiator.
1272 callout_init(&cs->cs_callout, 1);
1273 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1279 cfiscsi_session_delete(struct cfiscsi_session *cs)
1281 struct cfiscsi_softc *softc;
1283 softc = &cfiscsi_softc;
1285 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1286 ("destroying session with outstanding CTL pdus"));
1287 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1288 ("destroying session with non-empty queue"));
1290 cfiscsi_session_unregister_initiator(cs);
1291 if (cs->cs_target != NULL)
1292 cfiscsi_target_release(cs->cs_target);
1293 icl_conn_close(cs->cs_conn);
1294 icl_conn_free(cs->cs_conn);
1296 mtx_lock(&softc->lock);
1297 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1298 mtx_unlock(&softc->lock);
1300 free(cs, M_CFISCSI);
1306 struct cfiscsi_softc *softc;
1307 struct ctl_frontend *fe;
1310 softc = &cfiscsi_softc;
1312 bzero(softc, sizeof(*softc));
1313 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1315 #ifdef ICL_KERNEL_PROXY
1316 cv_init(&softc->accept_cv, "cfiscsi_accept");
1318 TAILQ_INIT(&softc->sessions);
1319 TAILQ_INIT(&softc->targets);
1322 fe->port_type = CTL_PORT_ISCSI;
1323 /* XXX KDM what should the real number be here? */
1324 fe->num_requested_ctl_io = 4096;
1325 snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1326 fe->port_name = softc->port_name;
1327 fe->port_online = cfiscsi_online;
1328 fe->port_offline = cfiscsi_offline;
1329 fe->onoff_arg = softc;
1330 fe->targ_enable = cfiscsi_targ_enable;
1331 fe->targ_disable = cfiscsi_targ_disable;
1332 fe->lun_enable = cfiscsi_lun_enable;
1333 fe->lun_disable = cfiscsi_lun_disable;
1334 fe->targ_lun_arg = softc;
1335 fe->ioctl = cfiscsi_ioctl;
1336 fe->devid = cfiscsi_devid;
1337 fe->fe_datamove = cfiscsi_datamove;
1338 fe->fe_done = cfiscsi_done;
1340 /* XXX KDM what should we report here? */
1341 /* XXX These should probably be fetched from CTL. */
1342 fe->max_targets = 1;
1343 fe->max_target_id = 15;
1345 retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1347 CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1353 softc->max_initiators = fe->max_initiators;
1355 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1356 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1366 cfiscsi_module_event_handler(module_t mod, int what, void *arg)
1371 return (cfiscsi_init());
1375 return (EOPNOTSUPP);
1379 #ifdef ICL_KERNEL_PROXY
1381 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1383 struct cfiscsi_session *cs;
1385 cs = cfiscsi_session_new(&cfiscsi_softc);
1387 CFISCSI_WARN("failed to create session");
1391 icl_conn_handoff_sock(cs->cs_conn, so);
1392 cs->cs_initiator_sa = sa;
1393 cs->cs_portal_id = portal_id;
1394 cs->cs_waiting_for_ctld = true;
1395 cv_signal(&cfiscsi_softc.accept_cv);
1400 cfiscsi_online(void *arg)
1402 struct cfiscsi_softc *softc;
1404 softc = (struct cfiscsi_softc *)arg;
1407 #ifdef ICL_KERNEL_PROXY
1408 if (softc->listener != NULL)
1409 icl_listen_free(softc->listener);
1410 softc->listener = icl_listen_new(cfiscsi_accept);
1415 cfiscsi_offline(void *arg)
1417 struct cfiscsi_softc *softc;
1418 struct cfiscsi_session *cs;
1420 softc = (struct cfiscsi_softc *)arg;
1424 mtx_lock(&softc->lock);
1425 TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1426 cfiscsi_session_terminate(cs);
1427 mtx_unlock(&softc->lock);
1429 #ifdef ICL_KERNEL_PROXY
1430 icl_listen_free(softc->listener);
1431 softc->listener = NULL;
1436 cfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1443 cfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1450 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1452 struct cfiscsi_softc *softc;
1453 struct cfiscsi_session *cs;
1454 struct cfiscsi_target *ct;
1455 struct ctl_iscsi_handoff_params *cihp;
1458 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1459 softc = &cfiscsi_softc;
1461 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1462 cihp->initiator_name, cihp->initiator_addr,
1465 if (softc->online == 0) {
1466 ci->status = CTL_ISCSI_ERROR;
1467 snprintf(ci->error_str, sizeof(ci->error_str),
1468 "%s: port offline", __func__);
1472 ct = cfiscsi_target_find(softc, cihp->target_name);
1474 ci->status = CTL_ISCSI_ERROR;
1475 snprintf(ci->error_str, sizeof(ci->error_str),
1476 "%s: target not found", __func__);
1480 #ifdef ICL_KERNEL_PROXY
1481 if (cihp->socket > 0 && cihp->connection_id > 0) {
1482 snprintf(ci->error_str, sizeof(ci->error_str),
1483 "both socket and connection_id set");
1484 ci->status = CTL_ISCSI_ERROR;
1485 cfiscsi_target_release(ct);
1488 if (cihp->socket == 0) {
1489 mtx_lock(&cfiscsi_softc.lock);
1490 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1491 if (cs->cs_id == cihp->socket)
1495 mtx_unlock(&cfiscsi_softc.lock);
1496 snprintf(ci->error_str, sizeof(ci->error_str),
1497 "connection not found");
1498 ci->status = CTL_ISCSI_ERROR;
1499 cfiscsi_target_release(ct);
1502 mtx_unlock(&cfiscsi_softc.lock);
1505 cs = cfiscsi_session_new(softc);
1507 ci->status = CTL_ISCSI_ERROR;
1508 snprintf(ci->error_str, sizeof(ci->error_str),
1509 "%s: cfiscsi_session_new failed", __func__);
1510 cfiscsi_target_release(ct);
1513 #ifdef ICL_KERNEL_PROXY
1519 * First PDU of Full Feature phase has the same CmdSN as the last
1520 * PDU from the Login Phase received from the initiator. Thus,
1523 cs->cs_portal_group_tag = cihp->portal_group_tag;
1524 cs->cs_cmdsn = cihp->cmdsn;
1525 cs->cs_statsn = cihp->statsn;
1526 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1527 cs->cs_max_burst_length = cihp->max_burst_length;
1528 cs->cs_immediate_data = !!cihp->immediate_data;
1529 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1530 cs->cs_conn->ic_header_crc32c = true;
1531 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1532 cs->cs_conn->ic_data_crc32c = true;
1534 strlcpy(cs->cs_initiator_name,
1535 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1536 strlcpy(cs->cs_initiator_addr,
1537 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1538 strlcpy(cs->cs_initiator_alias,
1539 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1541 #ifdef ICL_KERNEL_PROXY
1542 if (cihp->socket > 0) {
1544 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1546 cfiscsi_session_delete(cs);
1547 ci->status = CTL_ISCSI_ERROR;
1548 snprintf(ci->error_str, sizeof(ci->error_str),
1549 "%s: icl_conn_handoff failed with error %d",
1553 #ifdef ICL_KERNEL_PROXY
1558 * Register initiator with CTL.
1560 cfiscsi_session_register_initiator(cs);
1562 #ifdef ICL_KERNEL_PROXY
1563 cs->cs_login_phase = false;
1566 * First PDU of the Full Feature phase has likely already arrived.
1567 * We have to pick it up and execute properly.
1569 if (cs->cs_login_pdu != NULL) {
1570 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1571 cfiscsi_pdu_handle(cs->cs_login_pdu);
1572 cs->cs_login_pdu = NULL;
1576 ci->status = CTL_ISCSI_OK;
1580 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1582 struct ctl_iscsi_list_params *cilp;
1583 struct cfiscsi_session *cs;
1584 struct cfiscsi_softc *softc;
1588 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1589 softc = &cfiscsi_softc;
1591 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1593 ci->status = CTL_ISCSI_ERROR;
1594 snprintf(ci->error_str, sizeof(ci->error_str),
1595 "Unable to allocate %d bytes for iSCSI session list",
1600 sbuf_printf(sb, "<ctlislist>\n");
1601 mtx_lock(&softc->lock);
1602 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1603 #ifdef ICL_KERNEL_PROXY
1604 if (cs->cs_target == NULL)
1607 error = sbuf_printf(sb, "<connection id=\"%d\">"
1608 "<initiator>%s</initiator>"
1609 "<initiator_addr>%s</initiator_addr>"
1610 "<initiator_alias>%s</initiator_alias>"
1611 "<target>%s</target>"
1612 "<target_alias>%s</target_alias>"
1613 "<header_digest>%s</header_digest>"
1614 "<data_digest>%s</data_digest>"
1615 "<max_data_segment_length>%zd</max_data_segment_length>"
1616 "<immediate_data>%d</immediate_data>"
1620 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1621 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1622 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1623 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1624 cs->cs_max_data_segment_length,
1625 cs->cs_immediate_data,
1626 cs->cs_conn->ic_iser);
1630 mtx_unlock(&softc->lock);
1631 error = sbuf_printf(sb, "</ctlislist>\n");
1634 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1635 snprintf(ci->error_str, sizeof(ci->error_str),
1636 "Out of space, %d bytes is too small", cilp->alloc_len);
1641 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1642 cilp->fill_len = sbuf_len(sb) + 1;
1643 ci->status = CTL_ISCSI_OK;
1648 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1650 struct icl_pdu *response;
1651 struct iscsi_bhs_asynchronous_message *bhsam;
1652 struct ctl_iscsi_terminate_params *citp;
1653 struct cfiscsi_session *cs;
1654 struct cfiscsi_softc *softc;
1657 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1658 softc = &cfiscsi_softc;
1660 mtx_lock(&softc->lock);
1661 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1662 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1663 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1664 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1667 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1668 if (response == NULL) {
1670 * Oh well. Just terminate the connection.
1673 bhsam = (struct iscsi_bhs_asynchronous_message *)
1675 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1676 bhsam->bhsam_flags = 0x80;
1677 bhsam->bhsam_0xffffffff = 0xffffffff;
1678 bhsam->bhsam_async_event =
1679 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1680 cfiscsi_pdu_queue(response);
1682 cfiscsi_session_terminate(cs);
1685 mtx_unlock(&softc->lock);
1688 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1689 snprintf(ci->error_str, sizeof(ci->error_str),
1690 "No matching connections found");
1694 ci->status = CTL_ISCSI_OK;
1698 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1700 struct icl_pdu *response;
1701 struct iscsi_bhs_asynchronous_message *bhsam;
1702 struct ctl_iscsi_logout_params *cilp;
1703 struct cfiscsi_session *cs;
1704 struct cfiscsi_softc *softc;
1707 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1708 softc = &cfiscsi_softc;
1710 mtx_lock(&softc->lock);
1711 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1712 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1713 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1714 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1717 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1718 if (response == NULL) {
1719 ci->status = CTL_ISCSI_ERROR;
1720 snprintf(ci->error_str, sizeof(ci->error_str),
1721 "Unable to allocate memory");
1722 mtx_unlock(&softc->lock);
1726 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1727 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1728 bhsam->bhsam_flags = 0x80;
1729 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1730 bhsam->bhsam_parameter3 = htons(10);
1731 cfiscsi_pdu_queue(response);
1734 mtx_unlock(&softc->lock);
1737 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1738 snprintf(ci->error_str, sizeof(ci->error_str),
1739 "No matching connections found");
1743 ci->status = CTL_ISCSI_OK;
1746 #ifdef ICL_KERNEL_PROXY
1748 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1750 struct ctl_iscsi_listen_params *cilp;
1751 struct sockaddr *sa;
1754 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1756 if (cfiscsi_softc.listener == NULL) {
1757 CFISCSI_DEBUG("no listener");
1758 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1759 ci->status = CTL_ISCSI_ERROR;
1763 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1765 CFISCSI_DEBUG("getsockaddr, error %d", error);
1766 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1767 ci->status = CTL_ISCSI_ERROR;
1771 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1772 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1775 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1776 snprintf(ci->error_str, sizeof(ci->error_str),
1777 "icl_listen_add failed, error %d", error);
1778 ci->status = CTL_ISCSI_ERROR;
1782 ci->status = CTL_ISCSI_OK;
1786 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1788 struct ctl_iscsi_accept_params *ciap;
1789 struct cfiscsi_session *cs;
1792 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1794 mtx_lock(&cfiscsi_softc.lock);
1796 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1797 if (cs->cs_waiting_for_ctld)
1802 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1804 mtx_unlock(&cfiscsi_softc.lock);
1805 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1806 ci->status = CTL_ISCSI_ERROR;
1810 mtx_unlock(&cfiscsi_softc.lock);
1812 cs->cs_waiting_for_ctld = false;
1813 cs->cs_login_phase = true;
1815 ciap->connection_id = cs->cs_id;
1816 ciap->portal_id = cs->cs_portal_id;
1817 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1818 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1819 cs->cs_initiator_sa->sa_len);
1821 snprintf(ci->error_str, sizeof(ci->error_str),
1822 "copyout failed with error %d", error);
1823 ci->status = CTL_ISCSI_ERROR;
1827 ci->status = CTL_ISCSI_OK;
1831 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1833 struct ctl_iscsi_send_params *cisp;
1834 struct cfiscsi_session *cs;
1840 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1842 mtx_lock(&cfiscsi_softc.lock);
1843 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1844 if (cs->cs_id == cisp->connection_id)
1848 mtx_unlock(&cfiscsi_softc.lock);
1849 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1850 ci->status = CTL_ISCSI_ERROR;
1853 mtx_unlock(&cfiscsi_softc.lock);
1856 if (cs->cs_login_phase == false)
1860 if (cs->cs_terminating) {
1861 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1862 ci->status = CTL_ISCSI_ERROR;
1866 datalen = cisp->data_segment_len;
1870 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1871 if (datalen > 65535) {
1872 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1873 ci->status = CTL_ISCSI_ERROR;
1877 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1878 error = copyin(cisp->data_segment, data, datalen);
1880 free(data, M_CFISCSI);
1881 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1882 ci->status = CTL_ISCSI_ERROR;
1887 ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1888 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1890 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1891 free(data, M_CFISCSI);
1893 CFISCSI_SESSION_LOCK(cs);
1895 CFISCSI_SESSION_UNLOCK(cs);
1896 ci->status = CTL_ISCSI_OK;
1900 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1902 struct ctl_iscsi_receive_params *cirp;
1903 struct cfiscsi_session *cs;
1908 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1910 mtx_lock(&cfiscsi_softc.lock);
1911 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1912 if (cs->cs_id == cirp->connection_id)
1916 mtx_unlock(&cfiscsi_softc.lock);
1917 snprintf(ci->error_str, sizeof(ci->error_str),
1918 "connection not found");
1919 ci->status = CTL_ISCSI_ERROR;
1922 mtx_unlock(&cfiscsi_softc.lock);
1925 if (is->is_login_phase == false)
1929 CFISCSI_SESSION_LOCK(cs);
1930 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
1931 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
1933 CFISCSI_SESSION_UNLOCK(cs);
1934 snprintf(ci->error_str, sizeof(ci->error_str),
1935 "interrupted by signal");
1936 ci->status = CTL_ISCSI_ERROR;
1941 if (cs->cs_terminating) {
1942 CFISCSI_SESSION_UNLOCK(cs);
1943 snprintf(ci->error_str, sizeof(ci->error_str),
1944 "connection terminating");
1945 ci->status = CTL_ISCSI_ERROR;
1948 ip = cs->cs_login_pdu;
1949 cs->cs_login_pdu = NULL;
1950 CFISCSI_SESSION_UNLOCK(cs);
1952 if (ip->ip_data_len > cirp->data_segment_len) {
1954 snprintf(ci->error_str, sizeof(ci->error_str),
1955 "data segment too big");
1956 ci->status = CTL_ISCSI_ERROR;
1960 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1961 if (ip->ip_data_len > 0) {
1962 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1963 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1964 copyout(data, cirp->data_segment, ip->ip_data_len);
1965 free(data, M_CFISCSI);
1969 ci->status = CTL_ISCSI_OK;
1972 #endif /* !ICL_KERNEL_PROXY */
1975 cfiscsi_ioctl(struct cdev *dev,
1976 u_long cmd, caddr_t addr, int flag, struct thread *td)
1978 struct ctl_iscsi *ci;
1980 if (cmd != CTL_ISCSI)
1983 ci = (struct ctl_iscsi *)addr;
1985 case CTL_ISCSI_HANDOFF:
1986 cfiscsi_ioctl_handoff(ci);
1988 case CTL_ISCSI_LIST:
1989 cfiscsi_ioctl_list(ci);
1991 case CTL_ISCSI_TERMINATE:
1992 cfiscsi_ioctl_terminate(ci);
1994 case CTL_ISCSI_LOGOUT:
1995 cfiscsi_ioctl_logout(ci);
1997 #ifdef ICL_KERNEL_PROXY
1998 case CTL_ISCSI_LISTEN:
1999 cfiscsi_ioctl_listen(ci);
2001 case CTL_ISCSI_ACCEPT:
2002 cfiscsi_ioctl_accept(ci);
2004 case CTL_ISCSI_SEND:
2005 cfiscsi_ioctl_send(ci);
2007 case CTL_ISCSI_RECEIVE:
2008 cfiscsi_ioctl_receive(ci);
2011 case CTL_ISCSI_LISTEN:
2012 case CTL_ISCSI_ACCEPT:
2013 case CTL_ISCSI_SEND:
2014 case CTL_ISCSI_RECEIVE:
2015 ci->status = CTL_ISCSI_ERROR;
2016 snprintf(ci->error_str, sizeof(ci->error_str),
2017 "%s: CTL compiled without ICL_KERNEL_PROXY",
2020 #endif /* !ICL_KERNEL_PROXY */
2022 ci->status = CTL_ISCSI_ERROR;
2023 snprintf(ci->error_str, sizeof(ci->error_str),
2024 "%s: invalid iSCSI request type %d", __func__, ci->type);
2032 cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
2034 struct cfiscsi_session *cs;
2035 struct scsi_vpd_device_id *devid_ptr;
2036 struct scsi_vpd_id_descriptor *desc, *desc1;
2037 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
2038 struct scsi_vpd_id_t10 *t10id;
2039 struct ctl_lun *lun;
2040 const struct icl_pdu *request;
2041 size_t devid_len, wwpn_len;
2043 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2044 request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2045 cs = PDU_SESSION(request);
2047 wwpn_len = strlen(cs->cs_target->ct_name);
2048 wwpn_len += strlen(",t,0x01");
2049 wwpn_len += 1; /* '\0' */
2050 if ((wwpn_len % 4) != 0)
2051 wwpn_len += (4 - (wwpn_len % 4));
2053 devid_len = sizeof(struct scsi_vpd_device_id) +
2054 sizeof(struct scsi_vpd_id_descriptor) +
2055 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
2056 sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
2057 sizeof(struct scsi_vpd_id_descriptor) +
2058 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
2059 sizeof(struct scsi_vpd_id_descriptor) +
2060 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
2062 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
2063 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
2064 ctsio->kern_sg_entries = 0;
2066 if (devid_len < alloc_len) {
2067 ctsio->residual = alloc_len - devid_len;
2068 ctsio->kern_data_len = devid_len;
2069 ctsio->kern_total_len = devid_len;
2071 ctsio->residual = 0;
2072 ctsio->kern_data_len = alloc_len;
2073 ctsio->kern_total_len = alloc_len;
2075 ctsio->kern_data_resid = 0;
2076 ctsio->kern_rel_offset = 0;
2077 ctsio->kern_sg_entries = 0;
2079 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2080 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2081 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2082 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2083 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2085 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2086 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2089 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2090 lun->be_lun->lun_type;
2092 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2094 devid_ptr->page_code = SVPD_DEVICE_ID;
2096 scsi_ulto2b(devid_len - 4, devid_ptr->length);
2099 * We're using a LUN association here. i.e., this device ID is a
2100 * per-LUN identifier.
2102 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2103 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2104 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2105 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2108 * If we've actually got a backend, copy the device id from the
2109 * per-LUN data. Otherwise, set it to all spaces.
2113 * Copy the backend's LUN ID.
2115 strncpy((char *)t10id->vendor_spec_id,
2116 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2119 * No backend, set this to spaces.
2121 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2125 * desc1 is for the WWPN which is a port asscociation.
2127 desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2128 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2129 SVPD_ID_TYPE_SCSI_NAME;
2130 desc1->length = wwpn_len;
2131 snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2132 cs->cs_target->ct_name, cs->cs_portal_group_tag);
2135 * desc2 is for the Relative Target Port(type 4h) identifier
2137 desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2138 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2139 SVPD_ID_TYPE_RELTARG;
2141 desc2->identifier[3] = 1;
2144 * desc3 is for the Target Port Group(type 5h) identifier
2146 desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2147 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2148 SVPD_ID_TYPE_TPORTGRP;
2150 desc3->identifier[3] = 1;
2152 ctsio->scsi_status = SCSI_STATUS_OK;
2154 ctsio->be_move_done = ctl_config_move_done;
2155 ctl_datamove((union ctl_io *)ctsio);
2157 return (CTL_RETVAL_COMPLETE);
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 free(ct, M_CFISCSI);
2181 mtx_unlock(&softc->lock);
2184 static struct cfiscsi_target *
2185 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2187 struct cfiscsi_target *ct;
2189 mtx_lock(&softc->lock);
2190 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2191 if (strcmp(name, ct->ct_name) != 0)
2193 cfiscsi_target_hold(ct);
2194 mtx_unlock(&softc->lock);
2197 mtx_unlock(&softc->lock);
2202 static struct cfiscsi_target *
2203 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2206 struct cfiscsi_target *ct, *newct;
2209 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2212 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2214 mtx_lock(&softc->lock);
2215 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2216 if (strcmp(name, ct->ct_name) != 0)
2218 cfiscsi_target_hold(ct);
2219 mtx_unlock(&softc->lock);
2220 free(newct, M_CFISCSI);
2224 for (i = 0; i < CTL_MAX_LUNS; i++)
2225 newct->ct_luns[i] = -1;
2227 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2229 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2230 refcount_init(&newct->ct_refcount, 1);
2231 newct->ct_softc = softc;
2232 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2233 mtx_unlock(&softc->lock);
2239 * Takes LUN from the target space and returns LUN from the CTL space.
2242 cfiscsi_map_lun(void *arg, uint32_t lun)
2244 struct cfiscsi_session *cs;
2248 if (lun >= CTL_MAX_LUNS) {
2249 CFISCSI_DEBUG("requested lun number %d is higher "
2250 "than maximum %d", lun, CTL_MAX_LUNS - 1);
2251 return (0xffffffff);
2254 if (cs->cs_target->ct_luns[lun] < 0)
2255 return (0xffffffff);
2257 return (cs->cs_target->ct_luns[lun]);
2261 cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2262 unsigned long lun_id, unsigned long ctl_lun_id)
2265 if (lun_id >= CTL_MAX_LUNS) {
2266 CFISCSI_WARN("requested lun number %ld is higher "
2267 "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2271 if (ct->ct_luns[lun_id] >= 0) {
2273 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2274 * when the LUN is created, and a second time just before
2275 * the port is brought online; don't emit warnings
2278 if (ct->ct_luns[lun_id] == ctl_lun_id)
2280 CFISCSI_WARN("lun %ld already allocated", lun_id);
2285 CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2286 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2289 ct->ct_luns[lun_id] = ctl_lun_id;
2290 cfiscsi_target_hold(ct);
2296 cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2299 if (ct->ct_luns[lun_id] < 0) {
2300 CFISCSI_WARN("lun %ld not allocated", lun_id);
2304 ct->ct_luns[lun_id] = -1;
2305 cfiscsi_target_release(ct);
2311 cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2313 struct cfiscsi_softc *softc;
2314 struct cfiscsi_target *ct;
2315 struct ctl_be_lun_option *opt;
2316 const char *target = NULL, *target_alias = NULL;
2317 const char *lun = NULL;
2320 softc = (struct cfiscsi_softc *)arg;
2323 &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2324 if (strcmp(opt->name, "cfiscsi_target") == 0)
2325 target = opt->value;
2326 else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2327 target_alias = opt->value;
2328 else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2332 if (target == NULL && lun == NULL)
2335 if (target == NULL || lun == NULL) {
2336 CFISCSI_WARN("lun added with cfiscsi_target, but without "
2337 "cfiscsi_lun, or the other way around; ignoring");
2341 ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2343 CFISCSI_WARN("failed to create target \"%s\"", target);
2347 tmp = strtoul(lun, NULL, 10);
2348 cfiscsi_target_set_lun(ct, tmp, lun_id);
2349 cfiscsi_target_release(ct);
2354 cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2356 struct cfiscsi_softc *softc;
2357 struct cfiscsi_target *ct;
2360 softc = (struct cfiscsi_softc *)arg;
2362 mtx_lock(&softc->lock);
2363 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2364 for (i = 0; i < CTL_MAX_LUNS; i++) {
2365 if (ct->ct_luns[i] < 0)
2367 if (ct->ct_luns[i] != lun_id)
2369 mtx_unlock(&softc->lock);
2370 cfiscsi_target_unset_lun(ct, i);
2374 mtx_unlock(&softc->lock);
2379 cfiscsi_datamove_in(union ctl_io *io)
2381 struct cfiscsi_session *cs;
2382 struct icl_pdu *request, *response;
2383 const struct iscsi_bhs_scsi_command *bhssc;
2384 struct iscsi_bhs_data_in *bhsdi;
2385 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2386 size_t len, expected_len, sg_len, buffer_offset;
2387 const char *sg_addr;
2388 int ctl_sg_count, error, i;
2390 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2391 cs = PDU_SESSION(request);
2393 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2394 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2395 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2396 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2398 if (io->scsiio.kern_sg_entries > 0) {
2399 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2400 ctl_sg_count = io->scsiio.kern_sg_entries;
2402 ctl_sglist = &ctl_sg_entry;
2403 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2404 ctl_sglist->len = io->scsiio.kern_data_len;
2409 * This is the total amount of data to be transferred within the current
2410 * SCSI command. We need to record it so that we can properly report
2411 * underflow/underflow.
2413 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2416 * This is the offset within the current SCSI command; for the first
2417 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2418 * it will be the sum of lengths of previous ones.
2420 buffer_offset = io->scsiio.kern_rel_offset;
2423 * This is the transfer length expected by the initiator. In theory,
2424 * it could be different from the correct amount of data from the SCSI
2425 * point of view, even if that doesn't make any sense.
2427 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2429 if (expected_len != io->scsiio.kern_total_len) {
2430 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2431 "actual length %zd", expected_len,
2432 (size_t)io->scsiio.kern_total_len);
2436 if (buffer_offset >= expected_len) {
2438 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2439 "already sent the expected len", buffer_offset);
2441 io->scsiio.be_move_done(io);
2451 if (response == NULL) {
2452 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2453 if (response == NULL) {
2454 CFISCSI_SESSION_WARN(cs, "failed to "
2455 "allocate memory; dropping connection");
2456 ctl_set_busy(&io->scsiio);
2457 io->scsiio.be_move_done(io);
2458 cfiscsi_session_terminate(cs);
2461 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2462 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2463 bhsdi->bhsdi_initiator_task_tag =
2464 bhssc->bhssc_initiator_task_tag;
2465 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2466 PDU_EXPDATASN(request)++;
2467 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2470 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2472 sg_addr = ctl_sglist[i].addr;
2473 sg_len = ctl_sglist[i].len;
2474 KASSERT(sg_len > 0, ("sg_len <= 0"));
2480 * Truncate to maximum data segment length.
2482 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2483 ("ip_data_len %zd >= max_data_segment_length %zd",
2484 response->ip_data_len, cs->cs_max_data_segment_length));
2485 if (response->ip_data_len + len >
2486 cs->cs_max_data_segment_length) {
2487 len = cs->cs_max_data_segment_length -
2488 response->ip_data_len;
2489 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2494 * Truncate to expected data transfer length.
2496 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2497 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2498 buffer_offset, response->ip_data_len, expected_len));
2499 if (buffer_offset + response->ip_data_len + len > expected_len) {
2500 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2501 "to expected data transfer length %zd",
2502 buffer_offset + response->ip_data_len + len, expected_len);
2503 len = expected_len - (buffer_offset + response->ip_data_len);
2504 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2508 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2510 CFISCSI_SESSION_WARN(cs, "failed to "
2511 "allocate memory; dropping connection");
2512 icl_pdu_free(response);
2513 ctl_set_busy(&io->scsiio);
2514 io->scsiio.be_move_done(io);
2515 cfiscsi_session_terminate(cs);
2521 KASSERT(buffer_offset + request->ip_data_len <= expected_len,
2522 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2523 buffer_offset, request->ip_data_len, expected_len));
2524 if (buffer_offset + request->ip_data_len == expected_len) {
2526 * Already have the amount of data the initiator wanted.
2533 * End of scatter-gather segment;
2534 * proceed to the next one...
2536 if (i == ctl_sg_count - 1) {
2538 * ... unless this was the last one.
2545 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2547 * Can't stuff more data into the current PDU;
2548 * queue it. Note that's not enough to check
2549 * for kern_data_resid == 0 instead; there
2550 * may be several Data-In PDUs for the final
2551 * call to cfiscsi_datamove(), and we want
2552 * to set the F flag only on the last of them.
2554 buffer_offset += response->ip_data_len;
2555 if (buffer_offset == io->scsiio.kern_total_len ||
2556 buffer_offset == expected_len)
2557 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2558 cfiscsi_pdu_queue(response);
2563 if (response != NULL) {
2564 buffer_offset += response->ip_data_len;
2565 if (buffer_offset == io->scsiio.kern_total_len ||
2566 buffer_offset == expected_len)
2567 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2568 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2569 cfiscsi_pdu_queue(response);
2572 io->scsiio.be_move_done(io);
2576 cfiscsi_datamove_out(union ctl_io *io)
2578 struct cfiscsi_session *cs;
2579 struct icl_pdu *request, *response;
2580 const struct iscsi_bhs_scsi_command *bhssc;
2581 struct iscsi_bhs_r2t *bhsr2t;
2582 struct cfiscsi_data_wait *cdw;
2583 uint32_t target_transfer_tag;
2586 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2587 cs = PDU_SESSION(request);
2589 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2590 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2591 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2592 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2595 * We need to record it so that we can properly report
2596 * underflow/underflow.
2598 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2601 * We hadn't received anything during this datamove yet.
2603 io->scsiio.ext_data_filled = 0;
2605 target_transfer_tag =
2606 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2609 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2610 "task tag 0x%x, target transfer tag 0x%x",
2611 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2613 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2615 CFISCSI_SESSION_WARN(cs, "failed to "
2616 "allocate memory; dropping connection");
2617 ctl_set_busy(&io->scsiio);
2618 io->scsiio.be_move_done(io);
2619 cfiscsi_session_terminate(cs);
2622 cdw->cdw_ctl_io = io;
2623 cdw->cdw_target_transfer_tag = target_transfer_tag;
2624 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2626 if (cs->cs_immediate_data && io->scsiio.kern_rel_offset == 0 &&
2627 icl_pdu_data_segment_length(request) > 0) {
2628 done = cfiscsi_handle_data_segment(request, cdw);
2630 uma_zfree(cfiscsi_data_wait_zone, cdw);
2631 io->scsiio.be_move_done(io);
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 =
2676 htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2678 * This is the total length (sum of S/G lengths) this call
2679 * to cfiscsi_datamove() is supposed to handle.
2681 * XXX: Limit it to MaxBurstLength.
2683 bhsr2t->bhsr2t_desired_data_transfer_length =
2684 htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2685 cfiscsi_pdu_queue(response);
2689 cfiscsi_datamove(union ctl_io *io)
2692 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2693 cfiscsi_datamove_in(io);
2695 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);
2728 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2729 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2730 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2731 bhssr->bhssr_flags = 0x80;
2733 * XXX: We don't deal with bidirectional under/overflows;
2734 * does anything actually support those?
2736 if (PDU_TOTAL_TRANSFER_LEN(request) <
2737 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2738 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2739 bhssr->bhssr_residual_count =
2740 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2741 PDU_TOTAL_TRANSFER_LEN(request));
2742 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2743 // ntohl(bhssr->bhssr_residual_count));
2744 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2745 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2746 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2747 bhssr->bhssr_residual_count =
2748 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2749 ntohl(bhssc->bhssc_expected_data_transfer_length));
2750 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2751 // ntohl(bhssr->bhssr_residual_count));
2753 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2754 bhssr->bhssr_status = io->scsiio.scsi_status;
2755 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2756 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2758 if (io->scsiio.sense_len > 0) {
2760 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2761 io->scsiio.sense_len);
2763 sense_length = htons(io->scsiio.sense_len);
2764 icl_pdu_append_data(response,
2765 &sense_length, sizeof(sense_length), M_WAITOK);
2766 icl_pdu_append_data(response,
2767 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2771 icl_pdu_free(request);
2772 cfiscsi_pdu_queue(response);
2776 cfiscsi_task_management_done(union ctl_io *io)
2778 struct icl_pdu *request, *response;
2779 struct iscsi_bhs_task_management_request *bhstmr;
2780 struct iscsi_bhs_task_management_response *bhstmr2;
2781 struct cfiscsi_data_wait *cdw, *tmpcdw;
2782 struct cfiscsi_session *cs;
2784 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2785 cs = PDU_SESSION(request);
2786 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2787 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2788 ISCSI_BHS_OPCODE_TASK_REQUEST,
2789 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2792 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2793 bhstmr->bhstmr_initiator_task_tag,
2794 bhstmr->bhstmr_referenced_task_tag);
2797 if ((bhstmr->bhstmr_function & ~0x80) ==
2798 BHSTMR_FUNCTION_ABORT_TASK) {
2800 * Make sure we no longer wait for Data-Out for this command.
2802 CFISCSI_SESSION_LOCK(cs);
2803 TAILQ_FOREACH_SAFE(cdw,
2804 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2805 if (bhstmr->bhstmr_referenced_task_tag !=
2806 cdw->cdw_initiator_task_tag)
2810 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2811 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2813 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2815 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2816 uma_zfree(cfiscsi_data_wait_zone, cdw);
2818 CFISCSI_SESSION_UNLOCK(cs);
2821 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2822 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2824 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2825 bhstmr2->bhstmr_flags = 0x80;
2826 if (io->io_hdr.status == CTL_SUCCESS) {
2827 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2830 * XXX: How to figure out what exactly went wrong? iSCSI spec
2831 * expects us to provide detailed error, e.g. "Task does
2832 * not exist" or "LUN does not exist".
2834 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2835 bhstmr2->bhstmr_response =
2836 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2838 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2841 icl_pdu_free(request);
2842 cfiscsi_pdu_queue(response);
2846 cfiscsi_done(union ctl_io *io)
2848 struct icl_pdu *request;
2849 struct cfiscsi_session *cs;
2851 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2852 ("invalid CTL status %#x", io->io_hdr.status));
2854 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2855 if (request == NULL) {
2857 * Implicit task termination has just completed; nothing to do.
2862 cs = PDU_SESSION(request);
2863 refcount_release(&cs->cs_outstanding_ctl_pdus);
2865 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2866 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2867 cfiscsi_scsi_command_done(io);
2869 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2870 cfiscsi_task_management_done(io);
2873 panic("cfiscsi_done called with wrong opcode 0x%x",
2874 request->ip_bhs->bhs_opcode);