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/capability.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.
745 if (buffer_offset != io->scsiio.ext_data_filled) {
746 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
747 "expected %zd", buffer_offset,
748 (size_t)io->scsiio.ext_data_filled);
749 cfiscsi_session_terminate(cs);
754 * This is the offset within the PDU data segment, as opposed
755 * to buffer_offset, which is the offset within the task (SCSI
759 len = icl_pdu_data_segment_length(request);
762 * Iterate over the scatter/gather segments, filling them with data
763 * from the PDU data segment. Note that this can get called multiple
764 * times for one SCSI command; the cdw structure holds state for the
765 * scatter/gather list.
768 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
769 ("cdw->cdw_sg_index >= ctl_sg_count"));
770 if (cdw->cdw_sg_len == 0) {
771 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
772 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
774 KASSERT(off <= len, ("len > off"));
775 copy_len = len - off;
776 if (copy_len > cdw->cdw_sg_len)
777 copy_len = cdw->cdw_sg_len;
779 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
780 cdw->cdw_sg_addr += copy_len;
781 cdw->cdw_sg_len -= copy_len;
783 io->scsiio.ext_data_filled += copy_len;
785 if (cdw->cdw_sg_len == 0) {
787 * End of current segment.
789 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
791 * Last segment in scatter/gather list.
800 * End of PDU payload.
807 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
808 "expected %zd", icl_pdu_data_segment_length(request), off);
809 cfiscsi_session_terminate(cs);
813 if (bhsdo->bhsdo_flags & BHSDO_FLAGS_F ||
814 io->scsiio.ext_data_filled == io->scsiio.kern_total_len) {
815 if ((bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
816 CFISCSI_SESSION_WARN(cs, "got the final packet without "
817 "the F flag; flags = 0x%x; dropping connection",
819 cfiscsi_session_terminate(cs);
823 if (io->scsiio.ext_data_filled != io->scsiio.kern_total_len) {
824 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
825 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
826 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
827 "transmitted size was %zd bytes instead of %d; "
828 "dropping connection",
829 (size_t)io->scsiio.ext_data_filled,
830 io->scsiio.kern_total_len);
831 cfiscsi_session_terminate(cs);
835 * For SCSI Command PDU, this just means we need to
836 * solicit more data by sending R2T.
842 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
843 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
853 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
855 struct iscsi_bhs_data_out *bhsdo;
856 struct cfiscsi_session *cs;
857 struct cfiscsi_data_wait *cdw = NULL;
861 cs = PDU_SESSION(request);
862 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
864 CFISCSI_SESSION_LOCK(cs);
865 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
867 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
868 "ttt 0x%x, itt 0x%x",
869 bhsdo->bhsdo_target_transfer_tag,
870 bhsdo->bhsdo_initiator_task_tag,
871 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
873 if (bhsdo->bhsdo_target_transfer_tag ==
874 cdw->cdw_target_transfer_tag)
877 CFISCSI_SESSION_UNLOCK(cs);
879 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
880 "0x%x, not found; dropping connection",
881 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
882 icl_pdu_free(request);
883 cfiscsi_session_terminate(cs);
887 io = cdw->cdw_ctl_io;
888 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
889 ("CTL_FLAG_DATA_IN"));
891 done = cfiscsi_handle_data_segment(request, cdw);
893 CFISCSI_SESSION_LOCK(cs);
894 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
895 CFISCSI_SESSION_UNLOCK(cs);
896 uma_zfree(cfiscsi_data_wait_zone, cdw);
897 io->scsiio.be_move_done(io);
900 icl_pdu_free(request);
904 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
906 struct iscsi_bhs_logout_request *bhslr;
907 struct iscsi_bhs_logout_response *bhslr2;
908 struct icl_pdu *response;
909 struct cfiscsi_session *cs;
911 cs = PDU_SESSION(request);
912 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
913 switch (bhslr->bhslr_reason & 0x7f) {
914 case BHSLR_REASON_CLOSE_SESSION:
915 case BHSLR_REASON_CLOSE_CONNECTION:
916 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
917 if (response == NULL) {
918 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
919 icl_pdu_free(request);
920 cfiscsi_session_terminate(cs);
923 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
924 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
925 bhslr2->bhslr_flags = 0x80;
926 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
927 bhslr2->bhslr_initiator_task_tag =
928 bhslr->bhslr_initiator_task_tag;
929 icl_pdu_free(request);
930 cfiscsi_pdu_queue(response);
931 cfiscsi_session_terminate(cs);
933 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
934 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
935 if (response == NULL) {
936 CFISCSI_SESSION_WARN(cs,
937 "failed to allocate memory; dropping connection");
938 icl_pdu_free(request);
939 cfiscsi_session_terminate(cs);
942 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
943 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
944 bhslr2->bhslr_flags = 0x80;
945 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
946 bhslr2->bhslr_initiator_task_tag =
947 bhslr->bhslr_initiator_task_tag;
948 icl_pdu_free(request);
949 cfiscsi_pdu_queue(response);
952 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
953 bhslr->bhslr_reason);
954 icl_pdu_free(request);
955 cfiscsi_session_terminate(cs);
961 cfiscsi_callout(void *context)
964 struct iscsi_bhs_nop_in *bhsni;
965 struct cfiscsi_session *cs;
969 if (cs->cs_terminating)
972 callout_schedule(&cs->cs_callout, 1 * hz);
974 atomic_add_int(&cs->cs_timeout, 1);
976 #ifdef ICL_KERNEL_PROXY
977 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
978 if (cs->cs_timeout > login_timeout) {
979 CFISCSI_SESSION_WARN(cs, "login timed out after "
980 "%d seconds; dropping connection", cs->cs_timeout);
981 cfiscsi_session_terminate(cs);
987 if (cs->cs_timeout >= ping_timeout) {
988 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
989 "dropping connection", ping_timeout);
990 cfiscsi_session_terminate(cs);
995 * If the ping was reset less than one second ago - which means
996 * that we've received some PDU during the last second - assume
997 * the traffic flows correctly and don't bother sending a NOP-Out.
999 * (It's 2 - one for one second, and one for incrementing is_timeout
1000 * earlier in this routine.)
1002 if (cs->cs_timeout < 2)
1005 cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1007 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1010 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1011 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1012 bhsni->bhsni_flags = 0x80;
1013 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1015 cfiscsi_pdu_queue(cp);
1019 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1021 struct cfiscsi_data_wait *cdw, *tmpcdw;
1026 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1028 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1032 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1033 io->io_hdr.io_type = CTL_IO_TASK;
1034 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1035 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1036 io->io_hdr.nexus.targ_target.id = 0;
1037 io->io_hdr.nexus.targ_lun = lun;
1038 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1039 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1040 error = ctl_queue(io);
1041 if (error != CTL_RETVAL_COMPLETE) {
1042 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1047 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1048 * just iterate over tasks that are waiting for something - data - and
1051 CFISCSI_SESSION_LOCK(cs);
1052 TAILQ_FOREACH_SAFE(cdw,
1053 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1054 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1056 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1060 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1061 io->io_hdr.io_type = CTL_IO_TASK;
1062 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1063 io->io_hdr.nexus.targ_port =
1064 cs->cs_target->ct_softc->fe.targ_port;
1065 io->io_hdr.nexus.targ_target.id = 0;
1066 //io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1067 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1068 io->taskio.task_action = CTL_TASK_ABORT_TASK;
1069 io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1070 error = ctl_queue(io);
1071 if (error != CTL_RETVAL_COMPLETE) {
1072 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1077 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1078 "0x%x", cdw->cdw_initiator_task_tag);
1080 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1081 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1082 uma_zfree(cfiscsi_data_wait_zone, cdw);
1084 CFISCSI_SESSION_UNLOCK(cs);
1089 cfiscsi_maintenance_thread(void *arg)
1091 struct cfiscsi_session *cs;
1096 CFISCSI_SESSION_LOCK(cs);
1097 if (cs->cs_terminating == false)
1098 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1099 CFISCSI_SESSION_UNLOCK(cs);
1101 if (cs->cs_terminating) {
1102 cfiscsi_session_terminate_tasks(cs);
1103 callout_drain(&cs->cs_callout);
1105 icl_conn_shutdown(cs->cs_conn);
1106 icl_conn_close(cs->cs_conn);
1108 cs->cs_terminating++;
1111 * XXX: We used to wait up to 30 seconds to deliver queued PDUs
1112 * to the initiator. We also tried hard to deliver SCSI Responses
1113 * for the aborted PDUs. We don't do that anymore. We might need
1117 cfiscsi_session_delete(cs);
1121 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1126 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1129 if (cs->cs_terminating != 0)
1131 cs->cs_terminating = 1;
1132 cv_signal(&cs->cs_maintenance_cv);
1136 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1139 struct cfiscsi_softc *softc;
1141 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1143 softc = &cfiscsi_softc;
1145 mtx_lock(&softc->lock);
1146 for (i = 0; i < softc->max_initiators; i++) {
1147 if (softc->ctl_initids[i] == 0)
1150 if (i == softc->max_initiators) {
1151 CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1152 softc->max_initiators);
1153 mtx_unlock(&softc->lock);
1156 softc->ctl_initids[i] = 1;
1157 mtx_unlock(&softc->lock);
1160 CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1161 i, softc->max_initiators);
1163 cs->cs_ctl_initid = i;
1164 error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1166 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1167 mtx_lock(&softc->lock);
1168 softc->ctl_initids[cs->cs_ctl_initid] = 0;
1169 mtx_unlock(&softc->lock);
1170 cs->cs_ctl_initid = -1;
1178 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1181 struct cfiscsi_softc *softc;
1183 if (cs->cs_ctl_initid == -1)
1186 softc = &cfiscsi_softc;
1188 error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1190 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1193 mtx_lock(&softc->lock);
1194 softc->ctl_initids[cs->cs_ctl_initid] = 0;
1195 mtx_unlock(&softc->lock);
1196 cs->cs_ctl_initid = -1;
1199 static struct cfiscsi_session *
1200 cfiscsi_session_new(struct cfiscsi_softc *softc)
1202 struct cfiscsi_session *cs;
1205 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1207 CFISCSI_WARN("malloc failed");
1210 cs->cs_ctl_initid = -1;
1212 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1213 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1214 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1215 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1216 #ifdef ICL_KERNEL_PROXY
1217 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1220 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1221 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1222 cs->cs_conn->ic_error = cfiscsi_error_callback;
1223 cs->cs_conn->ic_prv0 = cs;
1225 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1227 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1228 free(cs, M_CFISCSI);
1232 mtx_lock(&softc->lock);
1233 cs->cs_id = softc->last_session_id + 1;
1234 softc->last_session_id++;
1235 mtx_unlock(&softc->lock);
1237 mtx_lock(&softc->lock);
1238 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1239 mtx_unlock(&softc->lock);
1242 * Start pinging the initiator.
1244 callout_init(&cs->cs_callout, 1);
1245 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1251 cfiscsi_session_delete(struct cfiscsi_session *cs)
1253 struct cfiscsi_softc *softc;
1255 softc = &cfiscsi_softc;
1257 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1258 ("destroying session with outstanding CTL pdus"));
1259 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1260 ("destroying session with non-empty queue"));
1262 cfiscsi_session_unregister_initiator(cs);
1263 if (cs->cs_target != NULL)
1264 cfiscsi_target_release(cs->cs_target);
1265 icl_conn_close(cs->cs_conn);
1266 icl_conn_free(cs->cs_conn);
1268 mtx_lock(&softc->lock);
1269 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1270 mtx_unlock(&softc->lock);
1272 free(cs, M_CFISCSI);
1278 struct cfiscsi_softc *softc;
1279 struct ctl_frontend *fe;
1282 softc = &cfiscsi_softc;
1284 bzero(softc, sizeof(*softc));
1285 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1287 #ifdef ICL_KERNEL_PROXY
1288 cv_init(&softc->accept_cv, "cfiscsi_accept");
1290 TAILQ_INIT(&softc->sessions);
1291 TAILQ_INIT(&softc->targets);
1294 fe->port_type = CTL_PORT_ISCSI;
1295 /* XXX KDM what should the real number be here? */
1296 fe->num_requested_ctl_io = 4096;
1297 snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1298 fe->port_name = softc->port_name;
1299 fe->port_online = cfiscsi_online;
1300 fe->port_offline = cfiscsi_offline;
1301 fe->onoff_arg = softc;
1302 fe->targ_enable = cfiscsi_targ_enable;
1303 fe->targ_disable = cfiscsi_targ_disable;
1304 fe->lun_enable = cfiscsi_lun_enable;
1305 fe->lun_disable = cfiscsi_lun_disable;
1306 fe->targ_lun_arg = softc;
1307 fe->ioctl = cfiscsi_ioctl;
1308 fe->devid = cfiscsi_devid;
1309 fe->fe_datamove = cfiscsi_datamove;
1310 fe->fe_done = cfiscsi_done;
1312 /* XXX KDM what should we report here? */
1313 /* XXX These should probably be fetched from CTL. */
1314 fe->max_targets = 1;
1315 fe->max_target_id = 15;
1317 retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1319 CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1325 softc->max_initiators = fe->max_initiators;
1327 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1328 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1338 cfiscsi_module_event_handler(module_t mod, int what, void *arg)
1343 return (cfiscsi_init());
1347 return (EOPNOTSUPP);
1351 #ifdef ICL_KERNEL_PROXY
1353 cfiscsi_accept(struct socket *so, int portal_id)
1355 struct cfiscsi_session *cs;
1357 cs = cfiscsi_session_new(&cfiscsi_softc);
1359 CFISCSI_WARN("failed to create session");
1363 icl_conn_handoff_sock(cs->cs_conn, so);
1364 cs->cs_portal_id = portal_id;
1365 cs->cs_waiting_for_ctld = true;
1366 cv_signal(&cfiscsi_softc.accept_cv);
1371 cfiscsi_online(void *arg)
1373 struct cfiscsi_softc *softc;
1375 softc = (struct cfiscsi_softc *)arg;
1378 #ifdef ICL_KERNEL_PROXY
1379 if (softc->listener != NULL)
1380 icl_listen_free(softc->listener);
1381 softc->listener = icl_listen_new(cfiscsi_accept);
1386 cfiscsi_offline(void *arg)
1388 struct cfiscsi_softc *softc;
1389 struct cfiscsi_session *cs;
1391 softc = (struct cfiscsi_softc *)arg;
1395 mtx_lock(&softc->lock);
1396 TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1397 cfiscsi_session_terminate(cs);
1398 mtx_unlock(&softc->lock);
1400 #ifdef ICL_KERNEL_PROXY
1401 icl_listen_free(softc->listener);
1402 softc->listener = NULL;
1407 cfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1414 cfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1421 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1423 struct cfiscsi_softc *softc;
1424 struct cfiscsi_session *cs;
1425 struct cfiscsi_target *ct;
1426 struct ctl_iscsi_handoff_params *cihp;
1429 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1430 softc = &cfiscsi_softc;
1432 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1433 cihp->initiator_name, cihp->initiator_addr,
1436 if (softc->online == 0) {
1437 ci->status = CTL_ISCSI_ERROR;
1438 snprintf(ci->error_str, sizeof(ci->error_str),
1439 "%s: port offline", __func__);
1443 ct = cfiscsi_target_find(softc, cihp->target_name);
1445 ci->status = CTL_ISCSI_ERROR;
1446 snprintf(ci->error_str, sizeof(ci->error_str),
1447 "%s: target not found", __func__);
1451 #ifdef ICL_KERNEL_PROXY
1452 if (cihp->socket > 0 && cihp->connection_id > 0) {
1453 snprintf(ci->error_str, sizeof(ci->error_str),
1454 "both socket and connection_id set");
1455 ci->status = CTL_ISCSI_ERROR;
1456 cfiscsi_target_release(ct);
1459 if (cihp->socket == 0) {
1460 mtx_lock(&cfiscsi_softc.lock);
1461 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1462 if (cs->cs_id == cihp->socket)
1466 mtx_unlock(&cfiscsi_softc.lock);
1467 snprintf(ci->error_str, sizeof(ci->error_str),
1468 "connection not found");
1469 ci->status = CTL_ISCSI_ERROR;
1470 cfiscsi_target_release(ct);
1473 mtx_unlock(&cfiscsi_softc.lock);
1476 cs = cfiscsi_session_new(softc);
1478 ci->status = CTL_ISCSI_ERROR;
1479 snprintf(ci->error_str, sizeof(ci->error_str),
1480 "%s: cfiscsi_session_new failed", __func__);
1481 cfiscsi_target_release(ct);
1484 #ifdef ICL_KERNEL_PROXY
1490 * First PDU of Full Feature phase has the same CmdSN as the last
1491 * PDU from the Login Phase received from the initiator. Thus,
1494 cs->cs_portal_group_tag = cihp->portal_group_tag;
1495 cs->cs_cmdsn = cihp->cmdsn;
1496 cs->cs_statsn = cihp->statsn;
1497 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1498 cs->cs_max_burst_length = cihp->max_burst_length;
1499 cs->cs_immediate_data = !!cihp->immediate_data;
1500 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1501 cs->cs_conn->ic_header_crc32c = true;
1502 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1503 cs->cs_conn->ic_data_crc32c = true;
1505 strlcpy(cs->cs_initiator_name,
1506 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1507 strlcpy(cs->cs_initiator_addr,
1508 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1509 strlcpy(cs->cs_initiator_alias,
1510 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1512 #ifdef ICL_KERNEL_PROXY
1513 if (cihp->socket > 0) {
1515 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1517 cfiscsi_session_delete(cs);
1518 ci->status = CTL_ISCSI_ERROR;
1519 snprintf(ci->error_str, sizeof(ci->error_str),
1520 "%s: icl_conn_handoff failed with error %d",
1524 #ifdef ICL_KERNEL_PROXY
1529 * Register initiator with CTL.
1531 cfiscsi_session_register_initiator(cs);
1533 #ifdef ICL_KERNEL_PROXY
1534 cs->cs_login_phase = false;
1537 * First PDU of the Full Feature phase has likely already arrived.
1538 * We have to pick it up and execute properly.
1540 if (cs->cs_login_pdu != NULL) {
1541 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1542 cfiscsi_pdu_handle(cs->cs_login_pdu);
1543 cs->cs_login_pdu = NULL;
1547 ci->status = CTL_ISCSI_OK;
1551 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1553 struct ctl_iscsi_list_params *cilp;
1554 struct cfiscsi_session *cs;
1555 struct cfiscsi_softc *softc;
1559 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1560 softc = &cfiscsi_softc;
1562 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1564 ci->status = CTL_ISCSI_ERROR;
1565 snprintf(ci->error_str, sizeof(ci->error_str),
1566 "Unable to allocate %d bytes for iSCSI session list",
1571 sbuf_printf(sb, "<ctlislist>\n");
1572 mtx_lock(&softc->lock);
1573 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1574 #ifdef ICL_KERNEL_PROXY
1575 if (cs->cs_target == NULL)
1578 error = sbuf_printf(sb, "<connection id=\"%d\">"
1579 "<initiator>%s</initiator>"
1580 "<initiator_addr>%s</initiator_addr>"
1581 "<initiator_alias>%s</initiator_alias>"
1582 "<target>%s</target>"
1583 "<target_alias>%s</target_alias>"
1584 "<header_digest>%s</header_digest>"
1585 "<data_digest>%s</data_digest>"
1586 "<max_data_segment_length>%zd</max_data_segment_length>"
1587 "<immediate_data>%d</immediate_data>"
1591 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1592 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1593 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1594 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1595 cs->cs_max_data_segment_length,
1596 cs->cs_immediate_data,
1597 cs->cs_conn->ic_iser);
1601 mtx_unlock(&softc->lock);
1602 error = sbuf_printf(sb, "</ctlislist>\n");
1605 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1606 snprintf(ci->error_str, sizeof(ci->error_str),
1607 "Out of space, %d bytes is too small", cilp->alloc_len);
1612 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1613 cilp->fill_len = sbuf_len(sb) + 1;
1614 ci->status = CTL_ISCSI_OK;
1619 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1621 struct icl_pdu *response;
1622 struct iscsi_bhs_asynchronous_message *bhsam;
1623 struct ctl_iscsi_terminate_params *citp;
1624 struct cfiscsi_session *cs;
1625 struct cfiscsi_softc *softc;
1628 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1629 softc = &cfiscsi_softc;
1631 mtx_lock(&softc->lock);
1632 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1633 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1634 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1635 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1638 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1639 if (response == NULL) {
1641 * Oh well. Just terminate the connection.
1644 bhsam = (struct iscsi_bhs_asynchronous_message *)
1646 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1647 bhsam->bhsam_flags = 0x80;
1648 bhsam->bhsam_0xffffffff = 0xffffffff;
1649 bhsam->bhsam_async_event =
1650 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1651 cfiscsi_pdu_queue(response);
1653 cfiscsi_session_terminate(cs);
1656 mtx_unlock(&softc->lock);
1659 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1660 snprintf(ci->error_str, sizeof(ci->error_str),
1661 "No matching connections found");
1665 ci->status = CTL_ISCSI_OK;
1669 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1671 struct icl_pdu *response;
1672 struct iscsi_bhs_asynchronous_message *bhsam;
1673 struct ctl_iscsi_logout_params *cilp;
1674 struct cfiscsi_session *cs;
1675 struct cfiscsi_softc *softc;
1678 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1679 softc = &cfiscsi_softc;
1681 mtx_lock(&softc->lock);
1682 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1683 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1684 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1685 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1688 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1689 if (response == NULL) {
1690 ci->status = CTL_ISCSI_ERROR;
1691 snprintf(ci->error_str, sizeof(ci->error_str),
1692 "Unable to allocate memory");
1693 mtx_unlock(&softc->lock);
1697 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1698 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1699 bhsam->bhsam_flags = 0x80;
1700 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1701 bhsam->bhsam_parameter3 = htons(10);
1702 cfiscsi_pdu_queue(response);
1705 mtx_unlock(&softc->lock);
1708 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1709 snprintf(ci->error_str, sizeof(ci->error_str),
1710 "No matching connections found");
1714 ci->status = CTL_ISCSI_OK;
1717 #ifdef ICL_KERNEL_PROXY
1719 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1721 struct ctl_iscsi_listen_params *cilp;
1722 struct sockaddr *sa;
1725 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1727 if (cfiscsi_softc.listener == NULL) {
1728 CFISCSI_DEBUG("no listener");
1729 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1730 ci->status = CTL_ISCSI_ERROR;
1734 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1736 CFISCSI_DEBUG("getsockaddr, error %d", error);
1737 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1738 ci->status = CTL_ISCSI_ERROR;
1742 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1743 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1746 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1747 snprintf(ci->error_str, sizeof(ci->error_str),
1748 "icl_listen_add failed, error %d", error);
1749 ci->status = CTL_ISCSI_ERROR;
1753 ci->status = CTL_ISCSI_OK;
1757 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1759 struct ctl_iscsi_accept_params *ciap;
1760 struct cfiscsi_session *cs;
1763 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1765 mtx_lock(&cfiscsi_softc.lock);
1767 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1768 if (cs->cs_waiting_for_ctld)
1773 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1775 mtx_unlock(&cfiscsi_softc.lock);
1776 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1777 ci->status = CTL_ISCSI_ERROR;
1781 mtx_unlock(&cfiscsi_softc.lock);
1783 cs->cs_waiting_for_ctld = false;
1784 cs->cs_login_phase = true;
1786 ciap->connection_id = cs->cs_id;
1787 ciap->portal_id = cs->cs_portal_id;
1788 ci->status = CTL_ISCSI_OK;
1792 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1794 struct ctl_iscsi_send_params *cisp;
1795 struct cfiscsi_session *cs;
1801 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1803 mtx_lock(&cfiscsi_softc.lock);
1804 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1805 if (cs->cs_id == cisp->connection_id)
1809 mtx_unlock(&cfiscsi_softc.lock);
1810 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1811 ci->status = CTL_ISCSI_ERROR;
1814 mtx_unlock(&cfiscsi_softc.lock);
1817 if (cs->cs_login_phase == false)
1821 if (cs->cs_terminating) {
1822 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1823 ci->status = CTL_ISCSI_ERROR;
1827 datalen = cisp->data_segment_len;
1831 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1832 if (datalen > 65535) {
1833 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1834 ci->status = CTL_ISCSI_ERROR;
1838 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1839 error = copyin(cisp->data_segment, data, datalen);
1841 free(data, M_CFISCSI);
1842 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1843 ci->status = CTL_ISCSI_ERROR;
1848 ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1849 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1851 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1852 free(data, M_CFISCSI);
1854 CFISCSI_SESSION_LOCK(cs);
1856 CFISCSI_SESSION_UNLOCK(cs);
1857 ci->status = CTL_ISCSI_OK;
1861 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1863 struct ctl_iscsi_receive_params *cirp;
1864 struct cfiscsi_session *cs;
1868 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1870 mtx_lock(&cfiscsi_softc.lock);
1871 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1872 if (cs->cs_id == cirp->connection_id)
1876 mtx_unlock(&cfiscsi_softc.lock);
1877 snprintf(ci->error_str, sizeof(ci->error_str), "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 &&
1890 cs->cs_terminating == false)
1891 cv_wait(&cs->cs_login_cv, &cs->cs_lock);
1892 if (cs->cs_terminating) {
1893 CFISCSI_SESSION_UNLOCK(cs);
1894 snprintf(ci->error_str, sizeof(ci->error_str), "connection terminating");
1895 ci->status = CTL_ISCSI_ERROR;
1898 ip = cs->cs_login_pdu;
1899 cs->cs_login_pdu = NULL;
1900 CFISCSI_SESSION_UNLOCK(cs);
1902 if (ip->ip_data_len > cirp->data_segment_len) {
1904 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1905 ci->status = CTL_ISCSI_ERROR;
1909 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1910 if (ip->ip_data_len > 0) {
1911 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1912 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1913 copyout(data, cirp->data_segment, ip->ip_data_len);
1914 free(data, M_CFISCSI);
1918 ci->status = CTL_ISCSI_OK;
1921 #endif /* !ICL_KERNEL_PROXY */
1924 cfiscsi_ioctl(struct cdev *dev,
1925 u_long cmd, caddr_t addr, int flag, struct thread *td)
1927 struct ctl_iscsi *ci;
1929 if (cmd != CTL_ISCSI)
1932 ci = (struct ctl_iscsi *)addr;
1934 case CTL_ISCSI_HANDOFF:
1935 cfiscsi_ioctl_handoff(ci);
1937 case CTL_ISCSI_LIST:
1938 cfiscsi_ioctl_list(ci);
1940 case CTL_ISCSI_TERMINATE:
1941 cfiscsi_ioctl_terminate(ci);
1943 case CTL_ISCSI_LOGOUT:
1944 cfiscsi_ioctl_logout(ci);
1946 #ifdef ICL_KERNEL_PROXY
1947 case CTL_ISCSI_LISTEN:
1948 cfiscsi_ioctl_listen(ci);
1950 case CTL_ISCSI_ACCEPT:
1951 cfiscsi_ioctl_accept(ci);
1953 case CTL_ISCSI_SEND:
1954 cfiscsi_ioctl_send(ci);
1956 case CTL_ISCSI_RECEIVE:
1957 cfiscsi_ioctl_receive(ci);
1960 case CTL_ISCSI_LISTEN:
1961 case CTL_ISCSI_ACCEPT:
1962 case CTL_ISCSI_SEND:
1963 case CTL_ISCSI_RECEIVE:
1964 ci->status = CTL_ISCSI_ERROR;
1965 snprintf(ci->error_str, sizeof(ci->error_str),
1966 "%s: CTL compiled without ICL_KERNEL_PROXY",
1969 #endif /* !ICL_KERNEL_PROXY */
1971 ci->status = CTL_ISCSI_ERROR;
1972 snprintf(ci->error_str, sizeof(ci->error_str),
1973 "%s: invalid iSCSI request type %d", __func__, ci->type);
1981 cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
1983 struct cfiscsi_session *cs;
1984 struct scsi_vpd_device_id *devid_ptr;
1985 struct scsi_vpd_id_descriptor *desc, *desc1;
1986 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
1987 struct scsi_vpd_id_t10 *t10id;
1988 struct ctl_lun *lun;
1989 const struct icl_pdu *request;
1990 size_t devid_len, wwpn_len;
1992 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1993 request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1994 cs = PDU_SESSION(request);
1996 wwpn_len = strlen(cs->cs_target->ct_name);
1997 wwpn_len += strlen(",t,0x01");
1998 wwpn_len += 1; /* '\0' */
1999 if ((wwpn_len % 4) != 0)
2000 wwpn_len += (4 - (wwpn_len % 4));
2002 devid_len = sizeof(struct scsi_vpd_device_id) +
2003 sizeof(struct scsi_vpd_id_descriptor) +
2004 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
2005 sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
2006 sizeof(struct scsi_vpd_id_descriptor) +
2007 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
2008 sizeof(struct scsi_vpd_id_descriptor) +
2009 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
2011 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
2012 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
2013 ctsio->kern_sg_entries = 0;
2015 if (devid_len < alloc_len) {
2016 ctsio->residual = alloc_len - devid_len;
2017 ctsio->kern_data_len = devid_len;
2018 ctsio->kern_total_len = devid_len;
2020 ctsio->residual = 0;
2021 ctsio->kern_data_len = alloc_len;
2022 ctsio->kern_total_len = alloc_len;
2024 ctsio->kern_data_resid = 0;
2025 ctsio->kern_rel_offset = 0;
2026 ctsio->kern_sg_entries = 0;
2028 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2029 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2030 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2031 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2032 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2034 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2035 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2038 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2039 lun->be_lun->lun_type;
2041 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2043 devid_ptr->page_code = SVPD_DEVICE_ID;
2045 scsi_ulto2b(devid_len - 4, devid_ptr->length);
2048 * We're using a LUN association here. i.e., this device ID is a
2049 * per-LUN identifier.
2051 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2052 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2053 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2054 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2057 * If we've actually got a backend, copy the device id from the
2058 * per-LUN data. Otherwise, set it to all spaces.
2062 * Copy the backend's LUN ID.
2064 strncpy((char *)t10id->vendor_spec_id,
2065 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2068 * No backend, set this to spaces.
2070 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2074 * desc1 is for the WWPN which is a port asscociation.
2076 desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2077 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2078 SVPD_ID_TYPE_SCSI_NAME;
2079 desc1->length = wwpn_len;
2080 snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2081 cs->cs_target->ct_name, cs->cs_portal_group_tag);
2084 * desc2 is for the Relative Target Port(type 4h) identifier
2086 desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2087 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2088 SVPD_ID_TYPE_RELTARG;
2090 desc2->identifier[3] = 1;
2093 * desc3 is for the Target Port Group(type 5h) identifier
2095 desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2096 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2097 SVPD_ID_TYPE_TPORTGRP;
2099 desc3->identifier[3] = 1;
2101 ctsio->scsi_status = SCSI_STATUS_OK;
2103 ctsio->be_move_done = ctl_config_move_done;
2104 ctl_datamove((union ctl_io *)ctsio);
2106 return (CTL_RETVAL_COMPLETE);
2110 cfiscsi_target_hold(struct cfiscsi_target *ct)
2113 refcount_acquire(&ct->ct_refcount);
2117 cfiscsi_target_release(struct cfiscsi_target *ct)
2119 struct cfiscsi_softc *softc;
2121 softc = ct->ct_softc;
2122 mtx_lock(&softc->lock);
2123 if (refcount_release(&ct->ct_refcount)) {
2124 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2125 mtx_unlock(&softc->lock);
2126 free(ct, M_CFISCSI);
2130 mtx_unlock(&softc->lock);
2133 static struct cfiscsi_target *
2134 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2136 struct cfiscsi_target *ct;
2138 mtx_lock(&softc->lock);
2139 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2140 if (strcmp(name, ct->ct_name) != 0)
2142 cfiscsi_target_hold(ct);
2143 mtx_unlock(&softc->lock);
2146 mtx_unlock(&softc->lock);
2151 static struct cfiscsi_target *
2152 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2155 struct cfiscsi_target *ct, *newct;
2158 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2161 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2163 mtx_lock(&softc->lock);
2164 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2165 if (strcmp(name, ct->ct_name) != 0)
2167 cfiscsi_target_hold(ct);
2168 mtx_unlock(&softc->lock);
2169 free(newct, M_CFISCSI);
2173 for (i = 0; i < CTL_MAX_LUNS; i++)
2174 newct->ct_luns[i] = -1;
2176 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2178 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2179 refcount_init(&newct->ct_refcount, 1);
2180 newct->ct_softc = softc;
2181 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2182 mtx_unlock(&softc->lock);
2188 * Takes LUN from the target space and returns LUN from the CTL space.
2191 cfiscsi_map_lun(void *arg, uint32_t lun)
2193 struct cfiscsi_session *cs;
2197 if (lun >= CTL_MAX_LUNS) {
2198 CFISCSI_DEBUG("requested lun number %d is higher "
2199 "than maximum %d", lun, CTL_MAX_LUNS - 1);
2200 return (0xffffffff);
2203 if (cs->cs_target->ct_luns[lun] < 0)
2204 return (0xffffffff);
2206 return (cs->cs_target->ct_luns[lun]);
2210 cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2211 unsigned long lun_id, unsigned long ctl_lun_id)
2214 if (lun_id >= CTL_MAX_LUNS) {
2215 CFISCSI_WARN("requested lun number %ld is higher "
2216 "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2220 if (ct->ct_luns[lun_id] >= 0) {
2222 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2223 * when the LUN is created, and a second time just before
2224 * the port is brought online; don't emit warnings
2227 if (ct->ct_luns[lun_id] == ctl_lun_id)
2229 CFISCSI_WARN("lun %ld already allocated", lun_id);
2234 CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2235 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2238 ct->ct_luns[lun_id] = ctl_lun_id;
2239 cfiscsi_target_hold(ct);
2245 cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2248 if (ct->ct_luns[lun_id] < 0) {
2249 CFISCSI_WARN("lun %ld not allocated", lun_id);
2253 ct->ct_luns[lun_id] = -1;
2254 cfiscsi_target_release(ct);
2260 cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2262 struct cfiscsi_softc *softc;
2263 struct cfiscsi_target *ct;
2264 struct ctl_be_lun_option *opt;
2265 const char *target = NULL, *target_alias = NULL;
2266 const char *lun = NULL;
2269 softc = (struct cfiscsi_softc *)arg;
2272 &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2273 if (strcmp(opt->name, "cfiscsi_target") == 0)
2274 target = opt->value;
2275 else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2276 target_alias = opt->value;
2277 else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2281 if (target == NULL && lun == NULL)
2284 if (target == NULL || lun == NULL) {
2285 CFISCSI_WARN("lun added with cfiscsi_target, but without "
2286 "cfiscsi_lun, or the other way around; ignoring");
2290 ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2292 CFISCSI_WARN("failed to create target \"%s\"", target);
2296 tmp = strtoul(lun, NULL, 10);
2297 cfiscsi_target_set_lun(ct, tmp, lun_id);
2298 cfiscsi_target_release(ct);
2303 cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2305 struct cfiscsi_softc *softc;
2306 struct cfiscsi_target *ct;
2309 softc = (struct cfiscsi_softc *)arg;
2311 mtx_lock(&softc->lock);
2312 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2313 for (i = 0; i < CTL_MAX_LUNS; i++) {
2314 if (ct->ct_luns[i] < 0)
2316 if (ct->ct_luns[i] != lun_id)
2318 mtx_unlock(&softc->lock);
2319 cfiscsi_target_unset_lun(ct, i);
2323 mtx_unlock(&softc->lock);
2328 cfiscsi_datamove_in(union ctl_io *io)
2330 struct cfiscsi_session *cs;
2331 struct icl_pdu *request, *response;
2332 const struct iscsi_bhs_scsi_command *bhssc;
2333 struct iscsi_bhs_data_in *bhsdi;
2334 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2335 size_t len, expected_len, sg_len, buffer_offset;
2336 const char *sg_addr;
2337 int ctl_sg_count, error, i;
2339 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2340 cs = PDU_SESSION(request);
2342 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2343 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2344 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2345 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2347 if (io->scsiio.kern_sg_entries > 0) {
2348 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2349 ctl_sg_count = io->scsiio.kern_sg_entries;
2351 ctl_sglist = &ctl_sg_entry;
2352 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2353 ctl_sglist->len = io->scsiio.kern_data_len;
2358 * This is the total amount of data to be transferred within the current
2359 * SCSI command. We need to record it so that we can properly report
2360 * underflow/underflow.
2362 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2365 * This is the offset within the current SCSI command; for the first
2366 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2367 * it will be the sum of lengths of previous ones. It's being
2368 * incremented as we append data to the data segment.
2370 buffer_offset = io->scsiio.kern_rel_offset;
2373 * This is the transfer length expected by the initiator. In theory,
2374 * it could be different from the correct amount of data from the SCSI
2375 * point of view, even if that doesn't make any sense.
2377 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2379 if (expected_len != io->scsiio.kern_total_len)
2380 CFISCSI_SESSION_DEBUG(cs, "expected transfer length = %zd, "
2381 "actual length = %zd", expected_len,
2382 io->scsiio.kern_total_len);
2385 if (buffer_offset >= expected_len) {
2387 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2388 "already sent the expected len", buffer_offset);
2390 io->scsiio.ext_data_filled = io->scsiio.kern_total_len;
2391 io->scsiio.be_move_done(io);
2401 if (response == NULL) {
2402 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2403 if (response == NULL) {
2404 CFISCSI_SESSION_WARN(cs, "failed to "
2405 "allocate memory; dropping connection");
2406 ctl_set_busy(&io->scsiio);
2407 io->scsiio.be_move_done(io);
2408 cfiscsi_session_terminate(cs);
2411 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2412 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2413 bhsdi->bhsdi_initiator_task_tag =
2414 bhssc->bhssc_initiator_task_tag;
2415 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2416 PDU_EXPDATASN(request)++;
2417 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2420 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2422 sg_addr = ctl_sglist[i].addr;
2423 sg_len = ctl_sglist[i].len;
2424 KASSERT(sg_len > 0, ("sg_len <= 0"));
2430 * Truncate to maximum data segment length.
2432 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2433 ("max_data_segment_length %zd >= ip_data_len %zd",
2434 response->ip_data_len, cs->cs_max_data_segment_length));
2435 if (response->ip_data_len + len >
2436 cs->cs_max_data_segment_length)
2437 len = cs->cs_max_data_segment_length -
2438 response->ip_data_len;
2441 * Truncate to expected data transfer length.
2443 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2444 ("%zd >= %zd", buffer_offset + response->ip_data_len, expected_len));
2445 if (buffer_offset + response->ip_data_len + len > expected_len) {
2446 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2447 "to expected data transfer length %zd",
2448 buffer_offset + response->ip_data_len + len, expected_len);
2449 len = expected_len - (buffer_offset + response->ip_data_len);
2452 KASSERT(len <= sg_len, ("len > sg_len"));
2453 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2455 CFISCSI_SESSION_WARN(cs, "failed to "
2456 "allocate memory; dropping connection");
2457 icl_pdu_free(response);
2458 ctl_set_busy(&io->scsiio);
2459 io->scsiio.be_move_done(io);
2460 cfiscsi_session_terminate(cs);
2465 buffer_offset += len;
2466 io->scsiio.ext_data_filled += len;
2468 if (buffer_offset == expected_len) {
2470 * Already have the amount of data the initiator wanted.
2477 * End of scatter-gather segment;
2478 * proceed to the next one...
2480 if (i == ctl_sg_count - 1) {
2482 * ... unless this was the last one.
2489 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2491 * Can't stuff more data into the current PDU;
2492 * queue it. Note that's not enough to check
2493 * for kern_data_resid == 0 instead; there
2494 * may be several Data-In PDUs for the final
2495 * call to cfiscsi_datamove(), and we want
2496 * to set the F flag only on the last of them.
2498 if (buffer_offset == io->scsiio.kern_total_len ||
2499 buffer_offset == expected_len)
2500 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2501 cfiscsi_pdu_queue(response);
2506 if (response != NULL) {
2507 if (buffer_offset == io->scsiio.kern_total_len ||
2508 buffer_offset == expected_len)
2509 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2510 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2511 cfiscsi_pdu_queue(response);
2514 io->scsiio.be_move_done(io);
2518 cfiscsi_datamove_out(union ctl_io *io)
2520 struct cfiscsi_session *cs;
2521 struct icl_pdu *request, *response;
2522 const struct iscsi_bhs_scsi_command *bhssc;
2523 struct iscsi_bhs_r2t *bhsr2t;
2524 struct cfiscsi_data_wait *cdw;
2525 uint32_t target_transfer_tag;
2528 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2529 cs = PDU_SESSION(request);
2531 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2532 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2533 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2534 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2537 * We need to record it so that we can properly report
2538 * underflow/underflow.
2540 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2542 target_transfer_tag =
2543 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2546 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2547 "task tag 0x%x, target transfer tag 0x%x",
2548 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2550 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2552 CFISCSI_SESSION_WARN(cs, "failed to "
2553 "allocate memory; dropping connection");
2554 ctl_set_busy(&io->scsiio);
2555 io->scsiio.be_move_done(io);
2556 cfiscsi_session_terminate(cs);
2559 cdw->cdw_ctl_io = io;
2560 cdw->cdw_target_transfer_tag = target_transfer_tag;
2561 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2563 if (cs->cs_immediate_data && icl_pdu_data_segment_length(request) > 0) {
2564 done = cfiscsi_handle_data_segment(request, cdw);
2566 uma_zfree(cfiscsi_data_wait_zone, cdw);
2567 io->scsiio.be_move_done(io);
2572 if (io->scsiio.ext_data_filled != 0)
2573 CFISCSI_SESSION_DEBUG(cs, "got %zd bytes of immediate data, need %zd",
2574 io->scsiio.ext_data_filled, io->scsiio.kern_data_len);
2578 CFISCSI_SESSION_LOCK(cs);
2579 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2580 CFISCSI_SESSION_UNLOCK(cs);
2583 * XXX: We should limit the number of outstanding R2T PDUs
2584 * per task to MaxOutstandingR2T.
2586 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2587 if (response == NULL) {
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 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2596 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2597 bhsr2t->bhsr2t_flags = 0x80;
2598 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2599 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2600 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2602 * XXX: Here we assume that cfiscsi_datamove() won't ever
2603 * be running concurrently on several CPUs for a given
2606 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2607 PDU_R2TSN(request)++;
2609 * This is the offset within the current SCSI command;
2610 * i.e. for the first call of datamove(), it will be 0,
2611 * and for subsequent ones it will be the sum of lengths
2614 * The ext_data_filled is to account for unsolicited
2615 * (immediate) data that might have already arrived.
2617 bhsr2t->bhsr2t_buffer_offset =
2618 htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2620 * This is the total length (sum of S/G lengths) this call
2621 * to cfiscsi_datamove() is supposed to handle.
2623 * XXX: Limit it to MaxBurstLength.
2625 bhsr2t->bhsr2t_desired_data_transfer_length =
2626 htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2627 cfiscsi_pdu_queue(response);
2631 cfiscsi_datamove(union ctl_io *io)
2634 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2635 cfiscsi_datamove_in(io);
2637 cfiscsi_datamove_out(io);
2641 cfiscsi_scsi_command_done(union ctl_io *io)
2643 struct icl_pdu *request, *response;
2644 struct iscsi_bhs_scsi_command *bhssc;
2645 struct iscsi_bhs_scsi_response *bhssr;
2647 struct cfiscsi_data_wait *cdw;
2649 struct cfiscsi_session *cs;
2650 uint16_t sense_length;
2652 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2653 cs = PDU_SESSION(request);
2654 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2655 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2656 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2657 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2659 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2660 // bhssc->bhssc_initiator_task_tag);
2663 CFISCSI_SESSION_LOCK(cs);
2664 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2665 KASSERT(bhssc->bhssc_initiator_task_tag !=
2666 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2667 CFISCSI_SESSION_UNLOCK(cs);
2670 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2671 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2672 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2673 bhssr->bhssr_flags = 0x80;
2675 * XXX: We don't deal with bidirectional under/overflows;
2676 * does anything actually support those?
2678 if (PDU_TOTAL_TRANSFER_LEN(request) <
2679 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2680 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2681 bhssr->bhssr_residual_count =
2682 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2683 PDU_TOTAL_TRANSFER_LEN(request));
2684 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2685 // ntohl(bhssr->bhssr_residual_count));
2686 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2687 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2688 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2689 bhssr->bhssr_residual_count =
2690 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2691 ntohl(bhssc->bhssc_expected_data_transfer_length));
2692 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2693 // ntohl(bhssr->bhssr_residual_count));
2695 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2696 bhssr->bhssr_status = io->scsiio.scsi_status;
2697 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2698 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2700 if (io->scsiio.sense_len > 0) {
2702 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2703 io->scsiio.sense_len);
2705 sense_length = htons(io->scsiio.sense_len);
2706 icl_pdu_append_data(response,
2707 &sense_length, sizeof(sense_length), M_WAITOK);
2708 icl_pdu_append_data(response,
2709 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2713 icl_pdu_free(request);
2714 cfiscsi_pdu_queue(response);
2718 cfiscsi_task_management_done(union ctl_io *io)
2720 struct icl_pdu *request, *response;
2721 struct iscsi_bhs_task_management_request *bhstmr;
2722 struct iscsi_bhs_task_management_response *bhstmr2;
2723 struct cfiscsi_data_wait *cdw, *tmpcdw;
2724 struct cfiscsi_session *cs;
2726 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2727 cs = PDU_SESSION(request);
2728 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2729 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2730 ISCSI_BHS_OPCODE_TASK_REQUEST,
2731 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2734 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2735 bhstmr->bhstmr_initiator_task_tag,
2736 bhstmr->bhstmr_referenced_task_tag);
2739 if ((bhstmr->bhstmr_function & ~0x80) ==
2740 BHSTMR_FUNCTION_ABORT_TASK) {
2742 * Make sure we no longer wait for Data-Out for this command.
2744 CFISCSI_SESSION_LOCK(cs);
2745 TAILQ_FOREACH_SAFE(cdw,
2746 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2747 if (bhstmr->bhstmr_referenced_task_tag !=
2748 cdw->cdw_initiator_task_tag)
2752 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2753 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2755 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2757 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2758 uma_zfree(cfiscsi_data_wait_zone, cdw);
2760 CFISCSI_SESSION_UNLOCK(cs);
2763 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2764 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2766 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2767 bhstmr2->bhstmr_flags = 0x80;
2768 if (io->io_hdr.status == CTL_SUCCESS) {
2769 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2772 * XXX: How to figure out what exactly went wrong? iSCSI spec
2773 * expects us to provide detailed error, e.g. "Task does
2774 * not exist" or "LUN does not exist".
2776 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2777 bhstmr2->bhstmr_response =
2778 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2780 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2783 icl_pdu_free(request);
2784 cfiscsi_pdu_queue(response);
2788 cfiscsi_done(union ctl_io *io)
2790 struct icl_pdu *request;
2791 struct cfiscsi_session *cs;
2793 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2794 ("invalid CTL status %#x", io->io_hdr.status));
2796 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2797 if (request == NULL) {
2799 * Implicit task termination has just completed; nothing to do.
2804 cs = PDU_SESSION(request);
2805 refcount_release(&cs->cs_outstanding_ctl_pdus);
2807 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2808 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2809 cfiscsi_scsi_command_done(io);
2811 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2812 cfiscsi_task_management_done(io);
2815 panic("cfiscsi_done called with wrong opcode 0x%x",
2816 request->ip_bhs->bhs_opcode);