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 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
79 static uma_zone_t cfiscsi_data_wait_zone;
81 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
82 "CAM Target Layer iSCSI Frontend");
84 TUNABLE_INT("kern.cam.ctl.iscsi.debug", &debug);
85 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RW,
86 &debug, 1, "Enable debug messages");
87 static int ping_timeout = 5;
88 TUNABLE_INT("kern.cam.ctl.iscsi.ping_timeout", &ping_timeout);
89 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RW,
90 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
91 static int login_timeout = 60;
92 TUNABLE_INT("kern.cam.ctl.iscsi.login_timeout", &login_timeout);
93 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RW,
94 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
95 static int maxcmdsn_delta = 256;
96 TUNABLE_INT("kern.cam.ctl.iscsi.maxcmdsn_delta", &maxcmdsn_delta);
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RW,
98 &maxcmdsn_delta, 256, "Number of commands the initiator can send "
99 "without confirmation");
101 #define CFISCSI_DEBUG(X, ...) \
103 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
106 #define CFISCSI_WARN(X, ...) \
108 printf("WARNING: %s: " X "\n", \
109 __func__, ## __VA_ARGS__); \
112 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
114 printf("%s: %s (%s): " X "\n", \
115 __func__, S->cs_initiator_addr, \
116 S->cs_initiator_name, ## __VA_ARGS__); \
119 #define CFISCSI_SESSION_WARN(S, X, ...) \
121 printf("WARNING: %s (%s): " X "\n", \
122 S->cs_initiator_addr, \
123 S->cs_initiator_name, ## __VA_ARGS__); \
126 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
127 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
128 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
130 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
131 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
132 #define PDU_EXPDATASN(X) (X)->ip_prv0
133 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
134 #define PDU_R2TSN(X) (X)->ip_prv2
136 int cfiscsi_init(void);
137 static void cfiscsi_online(void *arg);
138 static void cfiscsi_offline(void *arg);
139 static int cfiscsi_targ_enable(void *arg, struct ctl_id targ_id);
140 static int cfiscsi_targ_disable(void *arg, struct ctl_id targ_id);
141 static int cfiscsi_lun_enable(void *arg,
142 struct ctl_id target_id, int lun_id);
143 static int cfiscsi_lun_disable(void *arg,
144 struct ctl_id target_id, int lun_id);
145 static int cfiscsi_ioctl(struct cdev *dev,
146 u_long cmd, caddr_t addr, int flag, struct thread *td);
147 static int cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len);
148 static void cfiscsi_datamove(union ctl_io *io);
149 static void cfiscsi_done(union ctl_io *io);
150 static uint32_t cfiscsi_map_lun(void *arg, uint32_t lun);
151 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
152 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
153 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
154 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
155 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
156 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
157 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
158 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
159 *softc, const char *name);
160 static void cfiscsi_target_release(struct cfiscsi_target *ct);
161 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
163 static struct cfiscsi_softc cfiscsi_softc;
164 extern struct ctl_softc *control_softc;
166 static int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *);
168 static moduledata_t cfiscsi_moduledata = {
170 cfiscsi_module_event_handler,
174 DECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
175 MODULE_VERSION(ctlcfiscsi, 1);
176 MODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1);
177 MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
179 static struct icl_pdu *
180 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
183 return (icl_pdu_new_bhs(request->ip_conn, flags));
187 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
189 const struct iscsi_bhs_scsi_command *bhssc;
190 struct cfiscsi_session *cs;
191 uint32_t cmdsn, expstatsn;
193 cs = PDU_SESSION(request);
196 * Every incoming PDU - not just NOP-Out - resets the ping timer.
197 * The purpose of the timeout is to reset the connection when it stalls;
198 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
206 * Data-Out PDUs don't contain CmdSN.
208 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
209 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
213 * We're only using fields common for all the request
214 * (initiator -> target) PDUs.
216 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
217 cmdsn = ntohl(bhssc->bhssc_cmdsn);
218 expstatsn = ntohl(bhssc->bhssc_expstatsn);
220 CFISCSI_SESSION_LOCK(cs);
222 if (expstatsn != cs->cs_statsn) {
223 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
224 "while current StatSN is %d", expstatsn,
230 * The target MUST silently ignore any non-immediate command outside
233 if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
234 CFISCSI_SESSION_UNLOCK(cs);
235 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
236 "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
240 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
243 CFISCSI_SESSION_UNLOCK(cs);
249 cfiscsi_pdu_handle(struct icl_pdu *request)
251 struct cfiscsi_session *cs;
254 cs = PDU_SESSION(request);
256 ignore = cfiscsi_pdu_update_cmdsn(request);
258 icl_pdu_free(request);
263 * Handle the PDU; this includes e.g. receiving the remaining
264 * part of PDU and submitting the SCSI command to CTL
265 * or queueing a reply. The handling routine is responsible
266 * for freeing the PDU when it's no longer needed.
268 switch (request->ip_bhs->bhs_opcode &
269 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
270 case ISCSI_BHS_OPCODE_NOP_OUT:
271 cfiscsi_pdu_handle_nop_out(request);
273 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
274 cfiscsi_pdu_handle_scsi_command(request);
276 case ISCSI_BHS_OPCODE_TASK_REQUEST:
277 cfiscsi_pdu_handle_task_request(request);
279 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
280 cfiscsi_pdu_handle_data_out(request);
282 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
283 cfiscsi_pdu_handle_logout_request(request);
286 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
287 "opcode 0x%x; dropping connection",
288 request->ip_bhs->bhs_opcode);
289 icl_pdu_free(request);
290 cfiscsi_session_terminate(cs);
296 cfiscsi_receive_callback(struct icl_pdu *request)
298 struct cfiscsi_session *cs;
300 cs = PDU_SESSION(request);
302 #ifdef ICL_KERNEL_PROXY
303 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
304 if (cs->cs_login_pdu == NULL)
305 cs->cs_login_pdu = request;
307 icl_pdu_free(request);
308 cv_signal(&cs->cs_login_cv);
313 cfiscsi_pdu_handle(request);
317 cfiscsi_error_callback(struct icl_conn *ic)
319 struct cfiscsi_session *cs;
321 cs = CONN_SESSION(ic);
323 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
324 cfiscsi_session_terminate(cs);
328 cfiscsi_pdu_prepare(struct icl_pdu *response)
330 struct cfiscsi_session *cs;
331 struct iscsi_bhs_scsi_response *bhssr;
332 bool advance_statsn = true;
334 cs = PDU_SESSION(response);
336 CFISCSI_SESSION_LOCK_ASSERT(cs);
339 * We're only using fields common for all the response
340 * (target -> initiator) PDUs.
342 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
345 * 10.8.3: "The StatSN for this connection is not advanced
346 * after this PDU is sent."
348 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
349 advance_statsn = false;
352 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
353 * StatSN for the connection is not advanced after this PDU is sent."
355 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
356 bhssr->bhssr_initiator_task_tag == 0xffffffff)
357 advance_statsn = false;
360 * See the comment below - StatSN is not meaningful and must
363 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN)
364 advance_statsn = false;
367 * 10.7.3: "The fields StatSN, Status, and Residual Count
368 * only have meaningful content if the S bit is set to 1."
370 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN)
371 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
372 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
373 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
382 cfiscsi_pdu_queue(struct icl_pdu *response)
384 struct cfiscsi_session *cs;
386 cs = PDU_SESSION(response);
388 CFISCSI_SESSION_LOCK(cs);
389 cfiscsi_pdu_prepare(response);
390 icl_pdu_queue(response);
391 CFISCSI_SESSION_UNLOCK(cs);
395 cfiscsi_decode_lun(uint64_t encoded)
401 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
402 * but is in fact an evil, multidimensional structure defined
403 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
405 memcpy(lun, &encoded, sizeof(lun));
406 switch (lun[0] & 0xC0) {
408 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
409 lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
410 CFISCSI_WARN("malformed LUN "
411 "(peripheral device addressing method): 0x%jx",
419 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
420 lun[6] != 0 || lun[7] != 0) {
421 CFISCSI_WARN("malformed LUN "
422 "(flat address space addressing method): 0x%jx",
427 result = ((lun[0] & 0x3f) << 8) + lun[1];
430 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
431 lun[6] != 0 || lun[7] != 0) {
432 CFISCSI_WARN("malformed LUN (extended flat "
433 "address space addressing method): 0x%jx",
438 result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
440 CFISCSI_WARN("unsupported LUN format 0x%jx",
450 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
452 struct cfiscsi_session *cs;
453 struct iscsi_bhs_nop_out *bhsno;
454 struct iscsi_bhs_nop_in *bhsni;
455 struct icl_pdu *response;
460 cs = PDU_SESSION(request);
461 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
463 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
465 * Nothing to do, iscsi_pdu_update_statsn() already
466 * zeroed the timeout.
468 icl_pdu_free(request);
472 datasize = icl_pdu_data_segment_length(request);
474 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
476 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
477 "dropping connection");
478 icl_pdu_free(request);
479 cfiscsi_session_terminate(cs);
482 icl_pdu_get_data(request, 0, data, datasize);
485 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
486 if (response == NULL) {
487 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
488 "droppping connection");
489 free(data, M_CFISCSI);
490 icl_pdu_free(request);
491 cfiscsi_session_terminate(cs);
494 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
495 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
496 bhsni->bhsni_flags = 0x80;
497 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
498 bhsni->bhsni_target_transfer_tag = 0xffffffff;
500 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
502 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
503 "dropping connection");
504 free(data, M_CFISCSI);
505 icl_pdu_free(request);
506 icl_pdu_free(response);
507 cfiscsi_session_terminate(cs);
510 free(data, M_CFISCSI);
513 icl_pdu_free(request);
514 cfiscsi_pdu_queue(response);
518 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
520 struct iscsi_bhs_scsi_command *bhssc;
521 struct cfiscsi_session *cs;
525 cs = PDU_SESSION(request);
526 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
527 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
528 // bhssc->bhssc_initiator_task_tag);
530 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
531 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
532 "ImmediateData=No; dropping connection");
533 icl_pdu_free(request);
534 cfiscsi_session_terminate(cs);
537 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
539 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; "
540 "dropping connection");
541 icl_pdu_free(request);
542 cfiscsi_session_terminate(cs);
546 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
547 io->io_hdr.io_type = CTL_IO_SCSI;
548 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
549 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
550 io->io_hdr.nexus.targ_target.id = 0;
551 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
552 io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
553 io->io_hdr.nexus.lun_map_arg = cs;
554 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
555 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
556 case BHSSC_FLAGS_ATTR_UNTAGGED:
557 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
559 case BHSSC_FLAGS_ATTR_SIMPLE:
560 io->scsiio.tag_type = CTL_TAG_SIMPLE;
562 case BHSSC_FLAGS_ATTR_ORDERED:
563 io->scsiio.tag_type = CTL_TAG_ORDERED;
565 case BHSSC_FLAGS_ATTR_HOQ:
566 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
568 case BHSSC_FLAGS_ATTR_ACA:
569 io->scsiio.tag_type = CTL_TAG_ACA;
572 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
573 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
574 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
577 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
578 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
579 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
580 error = ctl_queue(io);
581 if (error != CTL_RETVAL_COMPLETE) {
582 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
583 "dropping connection", error);
585 refcount_release(&cs->cs_outstanding_ctl_pdus);
586 icl_pdu_free(request);
587 cfiscsi_session_terminate(cs);
592 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
594 struct iscsi_bhs_task_management_request *bhstmr;
595 struct iscsi_bhs_task_management_response *bhstmr2;
596 struct icl_pdu *response;
597 struct cfiscsi_session *cs;
601 cs = PDU_SESSION(request);
602 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
603 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
605 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;"
606 "dropping connection");
607 icl_pdu_free(request);
608 cfiscsi_session_terminate(cs);
612 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
613 io->io_hdr.io_type = CTL_IO_TASK;
614 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
615 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
616 io->io_hdr.nexus.targ_target.id = 0;
617 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
618 io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
619 io->io_hdr.nexus.lun_map_arg = cs;
620 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
622 switch (bhstmr->bhstmr_function & ~0x80) {
623 case BHSTMR_FUNCTION_ABORT_TASK:
625 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
627 io->taskio.task_action = CTL_TASK_ABORT_TASK;
628 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
630 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
632 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
634 io->taskio.task_action = CTL_TASK_LUN_RESET;
636 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
638 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
640 io->taskio.task_action = CTL_TASK_BUS_RESET;
643 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
644 bhstmr->bhstmr_function & ~0x80);
647 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
648 if (response == NULL) {
649 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
650 "dropping connection");
651 icl_pdu_free(request);
652 cfiscsi_session_terminate(cs);
655 bhstmr2 = (struct iscsi_bhs_task_management_response *)
657 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
658 bhstmr2->bhstmr_flags = 0x80;
659 bhstmr2->bhstmr_response =
660 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
661 bhstmr2->bhstmr_initiator_task_tag =
662 bhstmr->bhstmr_initiator_task_tag;
663 icl_pdu_free(request);
664 cfiscsi_pdu_queue(response);
668 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
669 error = ctl_queue(io);
670 if (error != CTL_RETVAL_COMPLETE) {
671 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
672 "dropping connection", error);
674 refcount_release(&cs->cs_outstanding_ctl_pdus);
675 icl_pdu_free(request);
676 cfiscsi_session_terminate(cs);
681 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
683 struct iscsi_bhs_data_out *bhsdo;
684 struct cfiscsi_session *cs;
685 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
686 size_t copy_len, len, off, buffer_offset;
690 cs = PDU_SESSION(request);
692 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
693 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
694 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
695 ISCSI_BHS_OPCODE_SCSI_COMMAND,
696 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
699 * We're only using fields common for Data Out and SCSI Command PDUs.
701 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
703 io = cdw->cdw_ctl_io;
704 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
705 ("CTL_FLAG_DATA_IN"));
708 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
709 request->ip_data_len, io->scsiio.kern_total_len);
712 if (io->scsiio.kern_sg_entries > 0) {
713 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
714 ctl_sg_count = io->scsiio.kern_sg_entries;
716 ctl_sglist = &ctl_sg_entry;
717 ctl_sglist->addr = io->scsiio.kern_data_ptr;
718 ctl_sglist->len = io->scsiio.kern_data_len;
722 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
723 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
724 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
729 * Make sure the offset, as sent by the initiator, matches the offset
730 * we're supposed to be at in the scatter-gather list.
732 if (buffer_offset != io->scsiio.ext_data_filled) {
733 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
734 "expected %zd", buffer_offset,
735 (size_t)io->scsiio.ext_data_filled);
736 cfiscsi_session_terminate(cs);
741 * This is the offset within the PDU data segment, as opposed
742 * to buffer_offset, which is the offset within the task (SCSI
746 len = icl_pdu_data_segment_length(request);
749 * Iterate over the scatter/gather segments, filling them with data
750 * from the PDU data segment. Note that this can get called multiple
751 * times for one SCSI command; the cdw structure holds state for the
752 * scatter/gather list.
755 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
756 ("cdw->cdw_sg_index >= ctl_sg_count"));
757 if (cdw->cdw_sg_len == 0) {
758 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
759 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
761 KASSERT(off <= len, ("len > off"));
762 copy_len = len - off;
763 if (copy_len > cdw->cdw_sg_len)
764 copy_len = cdw->cdw_sg_len;
766 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
767 cdw->cdw_sg_addr += copy_len;
768 cdw->cdw_sg_len -= copy_len;
770 io->scsiio.ext_data_filled += copy_len;
772 if (cdw->cdw_sg_len == 0) {
774 * End of current segment.
776 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
778 * Last segment in scatter/gather list.
787 * End of PDU payload.
794 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
795 "expected %zd", icl_pdu_data_segment_length(request), off);
796 cfiscsi_session_terminate(cs);
800 if (bhsdo->bhsdo_flags & BHSDO_FLAGS_F ||
801 io->scsiio.ext_data_filled == io->scsiio.kern_total_len) {
802 if ((bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
803 CFISCSI_SESSION_WARN(cs, "got the final packet without "
804 "the F flag; flags = 0x%x; dropping connection",
806 cfiscsi_session_terminate(cs);
810 if (io->scsiio.ext_data_filled != io->scsiio.kern_total_len) {
811 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
812 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
813 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
814 "transmitted size was %zd bytes instead of %d; "
815 "dropping connection",
816 (size_t)io->scsiio.ext_data_filled,
817 io->scsiio.kern_total_len);
818 cfiscsi_session_terminate(cs);
822 * For SCSI Command PDU, this just means we need to
823 * solicit more data by sending R2T.
829 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
830 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
840 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
842 struct iscsi_bhs_data_out *bhsdo;
843 struct cfiscsi_session *cs;
844 struct cfiscsi_data_wait *cdw = NULL;
848 cs = PDU_SESSION(request);
849 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
851 CFISCSI_SESSION_LOCK(cs);
852 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
854 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
855 "ttt 0x%x, itt 0x%x",
856 bhsdo->bhsdo_target_transfer_tag,
857 bhsdo->bhsdo_initiator_task_tag,
858 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
860 if (bhsdo->bhsdo_target_transfer_tag ==
861 cdw->cdw_target_transfer_tag)
864 CFISCSI_SESSION_UNLOCK(cs);
866 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
867 "0x%x, not found; dropping connection",
868 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
869 icl_pdu_free(request);
870 cfiscsi_session_terminate(cs);
874 io = cdw->cdw_ctl_io;
875 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
876 ("CTL_FLAG_DATA_IN"));
878 done = cfiscsi_handle_data_segment(request, cdw);
880 CFISCSI_SESSION_LOCK(cs);
881 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
882 CFISCSI_SESSION_UNLOCK(cs);
883 uma_zfree(cfiscsi_data_wait_zone, cdw);
884 io->scsiio.be_move_done(io);
887 icl_pdu_free(request);
891 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
893 struct iscsi_bhs_logout_request *bhslr;
894 struct iscsi_bhs_logout_response *bhslr2;
895 struct icl_pdu *response;
896 struct cfiscsi_session *cs;
898 cs = PDU_SESSION(request);
899 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
900 switch (bhslr->bhslr_reason & 0x7f) {
901 case BHSLR_REASON_CLOSE_SESSION:
902 case BHSLR_REASON_CLOSE_CONNECTION:
903 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
904 if (response == NULL) {
905 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
906 icl_pdu_free(request);
907 cfiscsi_session_terminate(cs);
910 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
911 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
912 bhslr2->bhslr_flags = 0x80;
913 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
914 bhslr2->bhslr_initiator_task_tag =
915 bhslr->bhslr_initiator_task_tag;
916 icl_pdu_free(request);
917 cfiscsi_pdu_queue(response);
918 cfiscsi_session_terminate(cs);
920 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
921 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
922 if (response == NULL) {
923 CFISCSI_SESSION_WARN(cs,
924 "failed to allocate memory; dropping connection");
925 icl_pdu_free(request);
926 cfiscsi_session_terminate(cs);
929 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
930 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
931 bhslr2->bhslr_flags = 0x80;
932 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
933 bhslr2->bhslr_initiator_task_tag =
934 bhslr->bhslr_initiator_task_tag;
935 icl_pdu_free(request);
936 cfiscsi_pdu_queue(response);
939 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
940 bhslr->bhslr_reason);
941 icl_pdu_free(request);
942 cfiscsi_session_terminate(cs);
948 cfiscsi_callout(void *context)
951 struct iscsi_bhs_nop_in *bhsni;
952 struct cfiscsi_session *cs;
956 if (cs->cs_terminating)
959 callout_schedule(&cs->cs_callout, 1 * hz);
961 CFISCSI_SESSION_LOCK(cs);
963 CFISCSI_SESSION_UNLOCK(cs);
965 #ifdef ICL_KERNEL_PROXY
966 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
967 if (cs->cs_timeout > login_timeout) {
968 CFISCSI_SESSION_WARN(cs, "login timed out after "
969 "%d seconds; dropping connection", cs->cs_timeout);
970 cfiscsi_session_terminate(cs);
976 if (cs->cs_timeout >= ping_timeout) {
977 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
978 "dropping connection", ping_timeout);
979 cfiscsi_session_terminate(cs);
984 * If the ping was reset less than one second ago - which means
985 * that we've received some PDU during the last second - assume
986 * the traffic flows correctly and don't bother sending a NOP-Out.
988 * (It's 2 - one for one second, and one for incrementing is_timeout
989 * earlier in this routine.)
991 if (cs->cs_timeout < 2)
994 cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
996 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
999 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1000 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1001 bhsni->bhsni_flags = 0x80;
1002 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1004 cfiscsi_pdu_queue(cp);
1008 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1010 struct cfiscsi_data_wait *cdw, *tmpcdw;
1015 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1017 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1021 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1022 io->io_hdr.io_type = CTL_IO_TASK;
1023 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1024 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1025 io->io_hdr.nexus.targ_target.id = 0;
1026 io->io_hdr.nexus.targ_lun = lun;
1027 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1028 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1029 error = ctl_queue(io);
1030 if (error != CTL_RETVAL_COMPLETE) {
1031 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1036 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1037 * just iterate over tasks that are waiting for something - data - and
1040 CFISCSI_SESSION_LOCK(cs);
1041 TAILQ_FOREACH_SAFE(cdw,
1042 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1043 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1045 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1049 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1050 io->io_hdr.io_type = CTL_IO_TASK;
1051 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1052 io->io_hdr.nexus.targ_port =
1053 cs->cs_target->ct_softc->fe.targ_port;
1054 io->io_hdr.nexus.targ_target.id = 0;
1055 //io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1056 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1057 io->taskio.task_action = CTL_TASK_ABORT_TASK;
1058 io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1059 error = ctl_queue(io);
1060 if (error != CTL_RETVAL_COMPLETE) {
1061 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1066 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1067 "0x%x", cdw->cdw_initiator_task_tag);
1069 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1070 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1071 uma_zfree(cfiscsi_data_wait_zone, cdw);
1073 CFISCSI_SESSION_UNLOCK(cs);
1078 cfiscsi_maintenance_thread(void *arg)
1080 struct cfiscsi_session *cs;
1085 CFISCSI_SESSION_LOCK(cs);
1086 if (cs->cs_terminating == false)
1087 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1088 CFISCSI_SESSION_UNLOCK(cs);
1090 if (cs->cs_terminating) {
1091 cfiscsi_session_terminate_tasks(cs);
1092 callout_drain(&cs->cs_callout);
1094 icl_conn_shutdown(cs->cs_conn);
1095 icl_conn_close(cs->cs_conn);
1097 cs->cs_terminating++;
1100 * XXX: We used to wait up to 30 seconds to deliver queued PDUs
1101 * to the initiator. We also tried hard to deliver SCSI Responses
1102 * for the aborted PDUs. We don't do that anymore. We might need
1106 cfiscsi_session_delete(cs);
1110 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1115 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1118 if (cs->cs_terminating != 0)
1120 cs->cs_terminating = 1;
1121 cv_signal(&cs->cs_maintenance_cv);
1125 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1128 struct cfiscsi_softc *softc;
1130 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1132 softc = &cfiscsi_softc;
1134 mtx_lock(&softc->lock);
1135 for (i = 0; i < softc->max_initiators; i++) {
1136 if (softc->ctl_initids[i] == 0)
1139 if (i == softc->max_initiators) {
1140 CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1141 softc->max_initiators);
1142 mtx_unlock(&softc->lock);
1145 softc->ctl_initids[i] = 1;
1146 mtx_unlock(&softc->lock);
1149 CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1150 i, softc->max_initiators);
1152 cs->cs_ctl_initid = i;
1153 error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1155 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1156 mtx_lock(&softc->lock);
1157 softc->ctl_initids[cs->cs_ctl_initid] = 0;
1158 mtx_unlock(&softc->lock);
1159 cs->cs_ctl_initid = -1;
1167 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1170 struct cfiscsi_softc *softc;
1172 if (cs->cs_ctl_initid == -1)
1175 softc = &cfiscsi_softc;
1177 error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1179 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1182 mtx_lock(&softc->lock);
1183 softc->ctl_initids[cs->cs_ctl_initid] = 0;
1184 mtx_unlock(&softc->lock);
1185 cs->cs_ctl_initid = -1;
1188 static struct cfiscsi_session *
1189 cfiscsi_session_new(struct cfiscsi_softc *softc)
1191 struct cfiscsi_session *cs;
1194 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1196 CFISCSI_WARN("malloc failed");
1199 cs->cs_ctl_initid = -1;
1201 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1202 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1203 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1204 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1205 #ifdef ICL_KERNEL_PROXY
1206 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1209 cs->cs_conn = icl_conn_new();
1210 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1211 cs->cs_conn->ic_error = cfiscsi_error_callback;
1212 cs->cs_conn->ic_prv0 = cs;
1214 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1216 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1217 free(cs, M_CFISCSI);
1221 mtx_lock(&softc->lock);
1222 cs->cs_id = softc->last_session_id + 1;
1223 softc->last_session_id++;
1224 mtx_unlock(&softc->lock);
1226 mtx_lock(&softc->lock);
1227 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1228 mtx_unlock(&softc->lock);
1231 * Start pinging the initiator.
1233 callout_init(&cs->cs_callout, 1);
1234 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1240 cfiscsi_session_delete(struct cfiscsi_session *cs)
1242 struct cfiscsi_softc *softc;
1244 softc = &cfiscsi_softc;
1246 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1247 ("destroying session with outstanding CTL pdus"));
1248 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1249 ("destroying session with non-empty queue"));
1251 cfiscsi_session_unregister_initiator(cs);
1252 if (cs->cs_target != NULL)
1253 cfiscsi_target_release(cs->cs_target);
1254 icl_conn_close(cs->cs_conn);
1255 icl_conn_free(cs->cs_conn);
1257 mtx_lock(&softc->lock);
1258 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1259 mtx_unlock(&softc->lock);
1261 free(cs, M_CFISCSI);
1267 struct cfiscsi_softc *softc;
1268 struct ctl_frontend *fe;
1271 softc = &cfiscsi_softc;
1273 bzero(softc, sizeof(*softc));
1274 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1276 #ifdef ICL_KERNEL_PROXY
1277 cv_init(&softc->accept_cv, "cfiscsi_accept");
1279 TAILQ_INIT(&softc->sessions);
1280 TAILQ_INIT(&softc->targets);
1283 fe->port_type = CTL_PORT_ISCSI;
1284 /* XXX KDM what should the real number be here? */
1285 fe->num_requested_ctl_io = 4096;
1286 snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1287 fe->port_name = softc->port_name;
1288 fe->port_online = cfiscsi_online;
1289 fe->port_offline = cfiscsi_offline;
1290 fe->onoff_arg = softc;
1291 fe->targ_enable = cfiscsi_targ_enable;
1292 fe->targ_disable = cfiscsi_targ_disable;
1293 fe->lun_enable = cfiscsi_lun_enable;
1294 fe->lun_disable = cfiscsi_lun_disable;
1295 fe->targ_lun_arg = softc;
1296 fe->ioctl = cfiscsi_ioctl;
1297 fe->devid = cfiscsi_devid;
1298 fe->fe_datamove = cfiscsi_datamove;
1299 fe->fe_done = cfiscsi_done;
1301 /* XXX KDM what should we report here? */
1302 /* XXX These should probably be fetched from CTL. */
1303 fe->max_targets = 1;
1304 fe->max_target_id = 15;
1306 retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1308 CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1314 softc->max_initiators = fe->max_initiators;
1316 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1317 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1327 cfiscsi_module_event_handler(module_t mod, int what, void *arg)
1332 return (cfiscsi_init());
1336 return (EOPNOTSUPP);
1340 #ifdef ICL_KERNEL_PROXY
1342 cfiscsi_accept(struct socket *so)
1344 struct cfiscsi_session *cs;
1346 cs = cfiscsi_session_new(&cfiscsi_softc);
1348 CFISCSI_WARN("failed to create session");
1352 icl_conn_handoff_sock(cs->cs_conn, so);
1353 cs->cs_waiting_for_ctld = true;
1354 cv_signal(&cfiscsi_softc.accept_cv);
1359 cfiscsi_online(void *arg)
1361 struct cfiscsi_softc *softc;
1363 softc = (struct cfiscsi_softc *)arg;
1366 #ifdef ICL_KERNEL_PROXY
1367 if (softc->listener != NULL)
1368 icl_listen_free(softc->listener);
1369 softc->listener = icl_listen_new(cfiscsi_accept);
1374 cfiscsi_offline(void *arg)
1376 struct cfiscsi_softc *softc;
1377 struct cfiscsi_session *cs;
1379 softc = (struct cfiscsi_softc *)arg;
1383 mtx_lock(&softc->lock);
1384 TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1385 cfiscsi_session_terminate(cs);
1386 mtx_unlock(&softc->lock);
1388 #ifdef ICL_KERNEL_PROXY
1389 icl_listen_free(softc->listener);
1390 softc->listener = NULL;
1395 cfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1402 cfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1409 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1411 struct cfiscsi_softc *softc;
1412 struct cfiscsi_session *cs;
1413 struct cfiscsi_target *ct;
1414 struct ctl_iscsi_handoff_params *cihp;
1415 #ifndef ICL_KERNEL_PROXY
1419 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1420 softc = &cfiscsi_softc;
1422 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1423 cihp->initiator_name, cihp->initiator_addr,
1426 if (softc->online == 0) {
1427 ci->status = CTL_ISCSI_ERROR;
1428 snprintf(ci->error_str, sizeof(ci->error_str),
1429 "%s: port offline", __func__);
1433 ct = cfiscsi_target_find(softc, cihp->target_name);
1435 ci->status = CTL_ISCSI_ERROR;
1436 snprintf(ci->error_str, sizeof(ci->error_str),
1437 "%s: target not found", __func__);
1441 #ifdef ICL_KERNEL_PROXY
1442 mtx_lock(&cfiscsi_softc.lock);
1443 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1444 if (cs->cs_id == cihp->socket)
1448 mtx_unlock(&cfiscsi_softc.lock);
1449 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1450 ci->status = CTL_ISCSI_ERROR;
1453 mtx_unlock(&cfiscsi_softc.lock);
1455 cs = cfiscsi_session_new(softc);
1457 ci->status = CTL_ISCSI_ERROR;
1458 snprintf(ci->error_str, sizeof(ci->error_str),
1459 "%s: cfiscsi_session_new failed", __func__);
1460 cfiscsi_target_release(ct);
1467 * First PDU of Full Feature phase has the same CmdSN as the last
1468 * PDU from the Login Phase received from the initiator. Thus,
1471 cs->cs_portal_group_tag = cihp->portal_group_tag;
1472 cs->cs_cmdsn = cihp->cmdsn;
1473 cs->cs_statsn = cihp->statsn;
1474 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1475 cs->cs_max_burst_length = cihp->max_burst_length;
1476 cs->cs_immediate_data = !!cihp->immediate_data;
1477 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1478 cs->cs_conn->ic_header_crc32c = true;
1479 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1480 cs->cs_conn->ic_data_crc32c = true;
1482 strlcpy(cs->cs_initiator_name,
1483 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1484 strlcpy(cs->cs_initiator_addr,
1485 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1486 strlcpy(cs->cs_initiator_alias,
1487 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1489 #ifdef ICL_KERNEL_PROXY
1490 cs->cs_login_phase = false;
1492 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1494 cfiscsi_session_delete(cs);
1495 ci->status = CTL_ISCSI_ERROR;
1496 snprintf(ci->error_str, sizeof(ci->error_str),
1497 "%s: icl_conn_handoff failed with error %d",
1504 * Register initiator with CTL.
1506 cfiscsi_session_register_initiator(cs);
1508 #ifdef ICL_KERNEL_PROXY
1510 * First PDU of the Full Feature phase has likely already arrived.
1511 * We have to pick it up and execute properly.
1513 if (cs->cs_login_pdu != NULL) {
1514 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1515 cfiscsi_pdu_handle(cs->cs_login_pdu);
1516 cs->cs_login_pdu = NULL;
1520 ci->status = CTL_ISCSI_OK;
1524 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1526 struct ctl_iscsi_list_params *cilp;
1527 struct cfiscsi_session *cs;
1528 struct cfiscsi_softc *softc;
1532 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1533 softc = &cfiscsi_softc;
1535 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1537 ci->status = CTL_ISCSI_ERROR;
1538 snprintf(ci->error_str, sizeof(ci->error_str),
1539 "Unable to allocate %d bytes for iSCSI session list",
1544 sbuf_printf(sb, "<ctlislist>\n");
1545 mtx_lock(&softc->lock);
1546 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1547 #ifdef ICL_KERNEL_PROXY
1548 if (cs->cs_target == NULL)
1551 error = sbuf_printf(sb, "<connection id=\"%d\">"
1552 "<initiator>%s</initiator>"
1553 "<initiator_addr>%s</initiator_addr>"
1554 "<initiator_alias>%s</initiator_alias>"
1555 "<target>%s</target>"
1556 "<target_alias>%s</target_alias>"
1557 "<header_digest>%s</header_digest>"
1558 "<data_digest>%s</data_digest>"
1559 "<max_data_segment_length>%zd</max_data_segment_length>"
1560 "<immediate_data>%d</immediate_data>"
1564 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1565 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1566 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1567 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1568 cs->cs_max_data_segment_length,
1569 cs->cs_immediate_data,
1570 cs->cs_conn->ic_iser);
1574 mtx_unlock(&softc->lock);
1575 error = sbuf_printf(sb, "</ctlislist>\n");
1578 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1579 snprintf(ci->error_str, sizeof(ci->error_str),
1580 "Out of space, %d bytes is too small", cilp->alloc_len);
1585 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1586 cilp->fill_len = sbuf_len(sb) + 1;
1587 ci->status = CTL_ISCSI_OK;
1592 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1594 struct icl_pdu *response;
1595 struct iscsi_bhs_asynchronous_message *bhsam;
1596 struct ctl_iscsi_terminate_params *citp;
1597 struct cfiscsi_session *cs;
1598 struct cfiscsi_softc *softc;
1601 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1602 softc = &cfiscsi_softc;
1604 mtx_lock(&softc->lock);
1605 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1606 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1607 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1608 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1611 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1612 if (response == NULL) {
1614 * Oh well. Just terminate the connection.
1617 bhsam = (struct iscsi_bhs_asynchronous_message *)
1619 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1620 bhsam->bhsam_flags = 0x80;
1621 bhsam->bhsam_0xffffffff = 0xffffffff;
1622 bhsam->bhsam_async_event =
1623 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1624 cfiscsi_pdu_queue(response);
1626 cfiscsi_session_terminate(cs);
1629 mtx_unlock(&softc->lock);
1632 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1633 snprintf(ci->error_str, sizeof(ci->error_str),
1634 "No matching connections found");
1638 ci->status = CTL_ISCSI_OK;
1642 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1644 struct icl_pdu *response;
1645 struct iscsi_bhs_asynchronous_message *bhsam;
1646 struct ctl_iscsi_logout_params *cilp;
1647 struct cfiscsi_session *cs;
1648 struct cfiscsi_softc *softc;
1651 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1652 softc = &cfiscsi_softc;
1654 mtx_lock(&softc->lock);
1655 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1656 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1657 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1658 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1661 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1662 if (response == NULL) {
1663 ci->status = CTL_ISCSI_ERROR;
1664 snprintf(ci->error_str, sizeof(ci->error_str),
1665 "Unable to allocate memory");
1666 mtx_unlock(&softc->lock);
1670 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1671 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1672 bhsam->bhsam_flags = 0x80;
1673 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1674 bhsam->bhsam_parameter3 = htons(10);
1675 cfiscsi_pdu_queue(response);
1678 mtx_unlock(&softc->lock);
1681 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1682 snprintf(ci->error_str, sizeof(ci->error_str),
1683 "No matching connections found");
1687 ci->status = CTL_ISCSI_OK;
1690 #ifdef ICL_KERNEL_PROXY
1692 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1694 struct ctl_iscsi_listen_params *cilp;
1695 struct sockaddr *sa;
1698 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1700 if (cfiscsi_softc.listener == NULL) {
1701 CFISCSI_DEBUG("no listener");
1702 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1703 ci->status = CTL_ISCSI_ERROR;
1707 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1709 CFISCSI_DEBUG("getsockaddr, error %d", error);
1710 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1711 ci->status = CTL_ISCSI_ERROR;
1715 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1716 cilp->socktype, cilp->protocol, sa);
1719 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1720 snprintf(ci->error_str, sizeof(ci->error_str),
1721 "icl_listen_add failed, error %d", error);
1722 ci->status = CTL_ISCSI_ERROR;
1726 ci->status = CTL_ISCSI_OK;
1730 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1732 struct ctl_iscsi_accept_params *ciap;
1733 struct cfiscsi_session *cs;
1736 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1738 mtx_lock(&cfiscsi_softc.lock);
1740 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1741 if (cs->cs_waiting_for_ctld)
1746 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1748 mtx_unlock(&cfiscsi_softc.lock);
1749 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1750 ci->status = CTL_ISCSI_ERROR;
1754 mtx_unlock(&cfiscsi_softc.lock);
1756 cs->cs_waiting_for_ctld = false;
1757 cs->cs_login_phase = true;
1759 ciap->connection_id = cs->cs_id;
1760 ci->status = CTL_ISCSI_OK;
1764 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1766 struct ctl_iscsi_send_params *cisp;
1767 struct cfiscsi_session *cs;
1773 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1775 mtx_lock(&cfiscsi_softc.lock);
1776 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1777 if (cs->cs_id == cisp->connection_id)
1781 mtx_unlock(&cfiscsi_softc.lock);
1782 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1783 ci->status = CTL_ISCSI_ERROR;
1786 mtx_unlock(&cfiscsi_softc.lock);
1789 if (cs->cs_login_phase == false)
1793 if (cs->cs_terminating) {
1794 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1795 ci->status = CTL_ISCSI_ERROR;
1799 datalen = cisp->data_segment_len;
1803 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1804 if (datalen > 65535) {
1805 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1806 ci->status = CTL_ISCSI_ERROR;
1810 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1811 error = copyin(cisp->data_segment, data, datalen);
1813 free(data, M_CFISCSI);
1814 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1815 ci->status = CTL_ISCSI_ERROR;
1820 ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1821 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1823 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1824 free(data, M_CFISCSI);
1827 ci->status = CTL_ISCSI_OK;
1831 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1833 struct ctl_iscsi_receive_params *cirp;
1834 struct cfiscsi_session *cs;
1838 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1840 mtx_lock(&cfiscsi_softc.lock);
1841 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1842 if (cs->cs_id == cirp->connection_id)
1846 mtx_unlock(&cfiscsi_softc.lock);
1847 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1848 ci->status = CTL_ISCSI_ERROR;
1851 mtx_unlock(&cfiscsi_softc.lock);
1854 if (is->is_login_phase == false)
1858 CFISCSI_SESSION_LOCK(cs);
1859 while (cs->cs_login_pdu == NULL &&
1860 cs->cs_terminating == false)
1861 cv_wait(&cs->cs_login_cv, &cs->cs_lock);
1862 if (cs->cs_terminating) {
1863 CFISCSI_SESSION_UNLOCK(cs);
1864 snprintf(ci->error_str, sizeof(ci->error_str), "connection terminating");
1865 ci->status = CTL_ISCSI_ERROR;
1868 ip = cs->cs_login_pdu;
1869 cs->cs_login_pdu = NULL;
1870 CFISCSI_SESSION_UNLOCK(cs);
1872 if (ip->ip_data_len > cirp->data_segment_len) {
1874 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1875 ci->status = CTL_ISCSI_ERROR;
1879 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1880 if (ip->ip_data_len > 0) {
1881 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1882 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1883 copyout(data, cirp->data_segment, ip->ip_data_len);
1884 free(data, M_CFISCSI);
1888 ci->status = CTL_ISCSI_OK;
1892 cfiscsi_ioctl_close(struct ctl_iscsi *ci)
1898 #endif /* !ICL_KERNEL_PROXY */
1901 cfiscsi_ioctl(struct cdev *dev,
1902 u_long cmd, caddr_t addr, int flag, struct thread *td)
1904 struct ctl_iscsi *ci;
1906 if (cmd != CTL_ISCSI)
1909 ci = (struct ctl_iscsi *)addr;
1911 case CTL_ISCSI_HANDOFF:
1912 cfiscsi_ioctl_handoff(ci);
1914 case CTL_ISCSI_LIST:
1915 cfiscsi_ioctl_list(ci);
1917 case CTL_ISCSI_TERMINATE:
1918 cfiscsi_ioctl_terminate(ci);
1920 case CTL_ISCSI_LOGOUT:
1921 cfiscsi_ioctl_logout(ci);
1923 #ifdef ICL_KERNEL_PROXY
1924 case CTL_ISCSI_LISTEN:
1925 cfiscsi_ioctl_listen(ci);
1927 case CTL_ISCSI_ACCEPT:
1928 cfiscsi_ioctl_accept(ci);
1930 case CTL_ISCSI_SEND:
1931 cfiscsi_ioctl_send(ci);
1933 case CTL_ISCSI_RECEIVE:
1934 cfiscsi_ioctl_receive(ci);
1936 case CTL_ISCSI_CLOSE:
1937 cfiscsi_ioctl_close(ci);
1939 #endif /* ICL_KERNEL_PROXY */
1941 ci->status = CTL_ISCSI_ERROR;
1942 snprintf(ci->error_str, sizeof(ci->error_str),
1943 "%s: invalid iSCSI request type %d", __func__, ci->type);
1951 cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
1953 struct cfiscsi_session *cs;
1954 struct scsi_vpd_device_id *devid_ptr;
1955 struct scsi_vpd_id_descriptor *desc, *desc1;
1956 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
1957 struct scsi_vpd_id_t10 *t10id;
1958 struct ctl_lun *lun;
1959 const struct icl_pdu *request;
1960 size_t devid_len, wwpn_len;
1962 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1963 request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1964 cs = PDU_SESSION(request);
1966 wwpn_len = strlen(cs->cs_target->ct_name);
1967 wwpn_len += strlen(",t,0x01");
1968 wwpn_len += 1; /* '\0' */
1969 if ((wwpn_len % 4) != 0)
1970 wwpn_len += (4 - (wwpn_len % 4));
1972 devid_len = sizeof(struct scsi_vpd_device_id) +
1973 sizeof(struct scsi_vpd_id_descriptor) +
1974 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
1975 sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
1976 sizeof(struct scsi_vpd_id_descriptor) +
1977 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
1978 sizeof(struct scsi_vpd_id_descriptor) +
1979 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
1981 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
1982 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
1983 ctsio->kern_sg_entries = 0;
1985 if (devid_len < alloc_len) {
1986 ctsio->residual = alloc_len - devid_len;
1987 ctsio->kern_data_len = devid_len;
1988 ctsio->kern_total_len = devid_len;
1990 ctsio->residual = 0;
1991 ctsio->kern_data_len = alloc_len;
1992 ctsio->kern_total_len = alloc_len;
1994 ctsio->kern_data_resid = 0;
1995 ctsio->kern_rel_offset = 0;
1996 ctsio->kern_sg_entries = 0;
1998 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
1999 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2000 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2001 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2002 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2004 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2005 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2008 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2009 lun->be_lun->lun_type;
2011 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2013 devid_ptr->page_code = SVPD_DEVICE_ID;
2015 scsi_ulto2b(devid_len - 4, devid_ptr->length);
2018 * We're using a LUN association here. i.e., this device ID is a
2019 * per-LUN identifier.
2021 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2022 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2023 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2024 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2027 * If we've actually got a backend, copy the device id from the
2028 * per-LUN data. Otherwise, set it to all spaces.
2032 * Copy the backend's LUN ID.
2034 strncpy((char *)t10id->vendor_spec_id,
2035 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2038 * No backend, set this to spaces.
2040 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2044 * desc1 is for the WWPN which is a port asscociation.
2046 desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2047 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2048 SVPD_ID_TYPE_SCSI_NAME;
2049 desc1->length = wwpn_len;
2050 snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2051 cs->cs_target->ct_name, cs->cs_portal_group_tag);
2054 * desc2 is for the Relative Target Port(type 4h) identifier
2056 desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2057 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2058 SVPD_ID_TYPE_RELTARG;
2060 desc2->identifier[3] = 1;
2063 * desc3 is for the Target Port Group(type 5h) identifier
2065 desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2066 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2067 SVPD_ID_TYPE_TPORTGRP;
2069 desc3->identifier[3] = 1;
2071 ctsio->scsi_status = SCSI_STATUS_OK;
2073 ctsio->be_move_done = ctl_config_move_done;
2074 ctl_datamove((union ctl_io *)ctsio);
2076 return (CTL_RETVAL_COMPLETE);
2080 cfiscsi_target_hold(struct cfiscsi_target *ct)
2083 refcount_acquire(&ct->ct_refcount);
2087 cfiscsi_target_release(struct cfiscsi_target *ct)
2090 struct cfiscsi_softc *softc;
2092 softc = ct->ct_softc;
2094 old = ct->ct_refcount;
2095 if (old > 1 && atomic_cmpset_int(&ct->ct_refcount, old, old - 1))
2098 mtx_lock(&softc->lock);
2099 if (refcount_release(&ct->ct_refcount)) {
2100 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2101 mtx_unlock(&softc->lock);
2102 free(ct, M_CFISCSI);
2106 mtx_unlock(&softc->lock);
2109 static struct cfiscsi_target *
2110 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2112 struct cfiscsi_target *ct;
2114 mtx_lock(&softc->lock);
2115 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2116 if (strcmp(name, ct->ct_name) != 0)
2118 cfiscsi_target_hold(ct);
2119 mtx_unlock(&softc->lock);
2122 mtx_unlock(&softc->lock);
2127 static struct cfiscsi_target *
2128 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2131 struct cfiscsi_target *ct, *newct;
2134 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2137 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2139 mtx_lock(&softc->lock);
2140 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2141 if (strcmp(name, ct->ct_name) != 0)
2143 cfiscsi_target_hold(ct);
2144 mtx_unlock(&softc->lock);
2145 free(newct, M_CFISCSI);
2149 for (i = 0; i < CTL_MAX_LUNS; i++)
2150 newct->ct_luns[i] = -1;
2152 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2154 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2155 refcount_init(&newct->ct_refcount, 1);
2156 newct->ct_softc = softc;
2157 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2158 mtx_unlock(&softc->lock);
2164 * Takes LUN from the target space and returns LUN from the CTL space.
2167 cfiscsi_map_lun(void *arg, uint32_t lun)
2169 struct cfiscsi_session *cs;
2173 if (lun >= CTL_MAX_LUNS) {
2174 CFISCSI_DEBUG("requested lun number %d is higher "
2175 "than maximum %d", lun, CTL_MAX_LUNS - 1);
2176 return (0xffffffff);
2179 if (cs->cs_target->ct_luns[lun] < 0)
2180 return (0xffffffff);
2182 return (cs->cs_target->ct_luns[lun]);
2186 cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2187 unsigned long lun_id, unsigned long ctl_lun_id)
2190 if (lun_id >= CTL_MAX_LUNS) {
2191 CFISCSI_WARN("requested lun number %ld is higher "
2192 "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2196 if (ct->ct_luns[lun_id] >= 0) {
2198 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2199 * when the LUN is created, and a second time just before
2200 * the port is brought online; don't emit warnings
2203 if (ct->ct_luns[lun_id] == ctl_lun_id)
2205 CFISCSI_WARN("lun %ld already allocated", lun_id);
2210 CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2211 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2214 ct->ct_luns[lun_id] = ctl_lun_id;
2215 cfiscsi_target_hold(ct);
2221 cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2224 if (ct->ct_luns[lun_id] < 0) {
2225 CFISCSI_WARN("lun %ld not allocated", lun_id);
2229 ct->ct_luns[lun_id] = -1;
2230 cfiscsi_target_release(ct);
2236 cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2238 struct cfiscsi_softc *softc;
2239 struct cfiscsi_target *ct;
2240 struct ctl_be_lun_option *opt;
2241 const char *target = NULL, *target_alias = NULL;
2242 const char *lun = NULL;
2245 softc = (struct cfiscsi_softc *)arg;
2248 &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2249 if (strcmp(opt->name, "cfiscsi_target") == 0)
2250 target = opt->value;
2251 else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2252 target_alias = opt->value;
2253 else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2257 if (target == NULL && lun == NULL)
2260 if (target == NULL || lun == NULL) {
2261 CFISCSI_WARN("lun added with cfiscsi_target, but without "
2262 "cfiscsi_lun, or the other way around; ignoring");
2266 ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2268 CFISCSI_WARN("failed to create target \"%s\"", target);
2272 tmp = strtoul(lun, NULL, 10);
2273 cfiscsi_target_set_lun(ct, tmp, lun_id);
2278 cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2280 struct cfiscsi_softc *softc;
2281 struct cfiscsi_target *ct;
2284 softc = (struct cfiscsi_softc *)arg;
2286 mtx_lock(&softc->lock);
2287 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2288 for (i = 0; i < CTL_MAX_LUNS; i++) {
2289 if (ct->ct_luns[i] < 0)
2291 if (ct->ct_luns[i] != lun_id)
2293 cfiscsi_target_unset_lun(ct, i);
2297 mtx_unlock(&softc->lock);
2302 cfiscsi_datamove_in(union ctl_io *io)
2304 struct cfiscsi_session *cs;
2305 struct icl_pdu *request, *response;
2306 const struct iscsi_bhs_scsi_command *bhssc;
2307 struct iscsi_bhs_data_in *bhsdi;
2308 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2309 size_t copy_len, len, off;
2311 int ctl_sg_count, error, i;
2313 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2314 cs = PDU_SESSION(request);
2316 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2317 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2318 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2319 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2321 if (io->scsiio.kern_sg_entries > 0) {
2322 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2323 ctl_sg_count = io->scsiio.kern_sg_entries;
2325 ctl_sglist = &ctl_sg_entry;
2326 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2327 ctl_sglist->len = io->scsiio.kern_data_len;
2332 * We need to record it so that we can properly report
2333 * underflow/underflow.
2335 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2338 * This is the offset within the current SCSI command;
2339 * i.e. for the first call of datamove(), it will be 0,
2340 * and for subsequent ones it will be the sum of lengths
2343 off = htonl(io->scsiio.kern_rel_offset);
2351 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2352 if (response == NULL) {
2353 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2354 if (response == NULL) {
2355 CFISCSI_SESSION_WARN(cs, "failed to "
2356 "allocate memory; dropping connection");
2357 ctl_set_busy(&io->scsiio);
2358 io->scsiio.be_move_done(io);
2359 cfiscsi_session_terminate(cs);
2362 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2363 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2364 bhsdi->bhsdi_initiator_task_tag =
2365 bhssc->bhssc_initiator_task_tag;
2366 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2367 PDU_EXPDATASN(request)++;
2368 bhsdi->bhsdi_buffer_offset = htonl(off);
2372 addr = ctl_sglist[i].addr;
2373 len = ctl_sglist[i].len;
2374 KASSERT(len > 0, ("len <= 0"));
2378 if (response->ip_data_len + copy_len >
2379 cs->cs_max_data_segment_length)
2380 copy_len = cs->cs_max_data_segment_length -
2381 response->ip_data_len;
2382 KASSERT(copy_len <= len, ("copy_len > len"));
2383 error = icl_pdu_append_data(response, addr, copy_len, M_NOWAIT);
2385 CFISCSI_SESSION_WARN(cs, "failed to "
2386 "allocate memory; dropping connection");
2387 icl_pdu_free(response);
2388 ctl_set_busy(&io->scsiio);
2389 io->scsiio.be_move_done(io);
2390 cfiscsi_session_terminate(cs);
2396 io->scsiio.ext_data_filled += copy_len;
2400 * End of scatter-gather segment;
2401 * proceed to the next one...
2403 if (i == ctl_sg_count - 1) {
2405 * ... unless this was the last one.
2412 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2414 * Can't stuff more data into the current PDU;
2415 * queue it. Note that's not enough to check
2416 * for kern_data_resid == 0 instead; there
2417 * may be several Data-In PDUs for the final
2418 * call to cfiscsi_datamove(), and we want
2419 * to set the F flag only on the last of them.
2421 if (off == io->scsiio.kern_total_len)
2422 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2423 KASSERT(response->ip_data_len > 0,
2424 ("sending empty Data-In"));
2425 cfiscsi_pdu_queue(response);
2430 KASSERT(i == ctl_sg_count - 1, ("missed SG segment"));
2431 KASSERT(len == 0, ("missed data from SG segment"));
2432 if (response != NULL) {
2433 if (off == io->scsiio.kern_total_len) {
2434 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2437 CFISCSI_SESSION_DEBUG(cs, "not setting the F flag; "
2438 "have %zd, need %zd", off,
2439 (size_t)io->scsiio.kern_total_len);
2442 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2443 cfiscsi_pdu_queue(response);
2446 io->scsiio.be_move_done(io);
2450 cfiscsi_datamove_out(union ctl_io *io)
2452 struct cfiscsi_session *cs;
2453 struct icl_pdu *request, *response;
2454 const struct iscsi_bhs_scsi_command *bhssc;
2455 struct iscsi_bhs_r2t *bhsr2t;
2456 struct cfiscsi_data_wait *cdw;
2457 uint32_t target_transfer_tag;
2460 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2461 cs = PDU_SESSION(request);
2463 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2464 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2465 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2466 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2469 * We need to record it so that we can properly report
2470 * underflow/underflow.
2472 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2474 CFISCSI_SESSION_LOCK(cs);
2475 target_transfer_tag = cs->cs_target_transfer_tag;
2476 cs->cs_target_transfer_tag++;
2477 CFISCSI_SESSION_UNLOCK(cs);
2480 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2481 "task tag 0x%x, target transfer tag 0x%x",
2482 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2484 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2486 CFISCSI_SESSION_WARN(cs, "failed to "
2487 "allocate memory; dropping connection");
2488 ctl_set_busy(&io->scsiio);
2489 io->scsiio.be_move_done(io);
2490 cfiscsi_session_terminate(cs);
2493 cdw->cdw_ctl_io = io;
2494 cdw->cdw_target_transfer_tag = htonl(target_transfer_tag);
2495 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2497 if (cs->cs_immediate_data && icl_pdu_data_segment_length(request) > 0) {
2498 done = cfiscsi_handle_data_segment(request, cdw);
2500 uma_zfree(cfiscsi_data_wait_zone, cdw);
2501 io->scsiio.be_move_done(io);
2506 if (io->scsiio.ext_data_filled != 0)
2507 CFISCSI_SESSION_DEBUG(cs, "got %zd bytes of immediate data, need %zd",
2508 io->scsiio.ext_data_filled, io->scsiio.kern_data_len);
2512 CFISCSI_SESSION_LOCK(cs);
2513 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2514 CFISCSI_SESSION_UNLOCK(cs);
2517 * XXX: We should limit the number of outstanding R2T PDUs
2518 * per task to MaxOutstandingR2T.
2520 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2521 if (response == NULL) {
2522 CFISCSI_SESSION_WARN(cs, "failed to "
2523 "allocate memory; dropping connection");
2524 ctl_set_busy(&io->scsiio);
2525 io->scsiio.be_move_done(io);
2526 cfiscsi_session_terminate(cs);
2529 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2530 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2531 bhsr2t->bhsr2t_flags = 0x80;
2532 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2533 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2534 bhsr2t->bhsr2t_target_transfer_tag = htonl(target_transfer_tag);
2536 * XXX: Here we assume that cfiscsi_datamove() won't ever
2537 * be running concurrently on several CPUs for a given
2540 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2541 PDU_R2TSN(request)++;
2543 * This is the offset within the current SCSI command;
2544 * i.e. for the first call of datamove(), it will be 0,
2545 * and for subsequent ones it will be the sum of lengths
2548 * The ext_data_filled is to account for unsolicited
2549 * (immediate) data that might have already arrived.
2551 bhsr2t->bhsr2t_buffer_offset =
2552 htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2554 * This is the total length (sum of S/G lengths) this call
2555 * to cfiscsi_datamove() is supposed to handle.
2557 * XXX: Limit it to MaxBurstLength.
2559 bhsr2t->bhsr2t_desired_data_transfer_length =
2560 htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2561 cfiscsi_pdu_queue(response);
2565 cfiscsi_datamove(union ctl_io *io)
2568 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2569 cfiscsi_datamove_in(io);
2571 cfiscsi_datamove_out(io);
2575 cfiscsi_scsi_command_done(union ctl_io *io)
2577 struct icl_pdu *request, *response;
2578 struct iscsi_bhs_scsi_command *bhssc;
2579 struct iscsi_bhs_scsi_response *bhssr;
2581 struct cfiscsi_data_wait *cdw;
2583 struct cfiscsi_session *cs;
2584 uint16_t sense_length;
2586 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2587 cs = PDU_SESSION(request);
2588 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2589 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2590 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2591 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2593 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2594 // bhssc->bhssc_initiator_task_tag);
2597 CFISCSI_SESSION_LOCK(cs);
2598 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2599 KASSERT(bhssc->bhssc_initiator_task_tag !=
2600 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2601 CFISCSI_SESSION_UNLOCK(cs);
2604 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2605 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2606 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2607 bhssr->bhssr_flags = 0x80;
2609 * XXX: We don't deal with bidirectional under/overflows;
2610 * does anything actually support those?
2612 if (PDU_TOTAL_TRANSFER_LEN(request) <
2613 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2614 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2615 bhssr->bhssr_residual_count =
2616 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2617 PDU_TOTAL_TRANSFER_LEN(request));
2618 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2619 // ntohl(bhssr->bhssr_residual_count));
2620 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2621 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2622 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2623 bhssr->bhssr_residual_count =
2624 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2625 ntohl(bhssc->bhssc_expected_data_transfer_length));
2626 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2627 // ntohl(bhssr->bhssr_residual_count));
2629 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2630 bhssr->bhssr_status = io->scsiio.scsi_status;
2631 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2632 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2634 if (io->scsiio.sense_len > 0) {
2636 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2637 io->scsiio.sense_len);
2639 sense_length = htons(io->scsiio.sense_len);
2640 icl_pdu_append_data(response,
2641 &sense_length, sizeof(sense_length), M_WAITOK);
2642 icl_pdu_append_data(response,
2643 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2647 icl_pdu_free(request);
2648 cfiscsi_pdu_queue(response);
2652 cfiscsi_task_management_done(union ctl_io *io)
2654 struct icl_pdu *request, *response;
2655 struct iscsi_bhs_task_management_request *bhstmr;
2656 struct iscsi_bhs_task_management_response *bhstmr2;
2657 struct cfiscsi_data_wait *cdw, *tmpcdw;
2658 struct cfiscsi_session *cs;
2660 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2661 cs = PDU_SESSION(request);
2662 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2663 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2664 ISCSI_BHS_OPCODE_TASK_REQUEST,
2665 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2668 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2669 bhstmr->bhstmr_initiator_task_tag,
2670 bhstmr->bhstmr_referenced_task_tag);
2673 if ((bhstmr->bhstmr_function & ~0x80) ==
2674 BHSTMR_FUNCTION_ABORT_TASK) {
2676 * Make sure we no longer wait for Data-Out for this command.
2678 CFISCSI_SESSION_LOCK(cs);
2679 TAILQ_FOREACH_SAFE(cdw,
2680 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2681 if (bhstmr->bhstmr_referenced_task_tag !=
2682 cdw->cdw_initiator_task_tag)
2686 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2687 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2689 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2691 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2692 uma_zfree(cfiscsi_data_wait_zone, cdw);
2694 CFISCSI_SESSION_UNLOCK(cs);
2697 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2698 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2700 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2701 bhstmr2->bhstmr_flags = 0x80;
2702 if (io->io_hdr.status == CTL_SUCCESS) {
2703 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2706 * XXX: How to figure out what exactly went wrong? iSCSI spec
2707 * expects us to provide detailed error, e.g. "Task does
2708 * not exist" or "LUN does not exist".
2710 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2711 bhstmr2->bhstmr_response =
2712 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2714 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2717 icl_pdu_free(request);
2718 cfiscsi_pdu_queue(response);
2722 cfiscsi_done(union ctl_io *io)
2724 struct icl_pdu *request;
2725 struct cfiscsi_session *cs;
2727 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2728 ("invalid CTL status %#x", io->io_hdr.status));
2730 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2731 if (request == NULL) {
2733 * Implicit task termination has just completed; nothing to do.
2738 cs = PDU_SESSION(request);
2739 refcount_release(&cs->cs_outstanding_ctl_pdus);
2741 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2742 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2743 cfiscsi_scsi_command_done(io);
2745 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2746 cfiscsi_task_management_done(io);
2749 panic("cfiscsi_done called with wrong opcode 0x%x",
2750 request->ip_bhs->bhs_opcode);