2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * CTL frontend for the iSCSI protocol.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/condvar.h>
42 #include <sys/endian.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/queue.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
55 #include <sys/unistd.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_da.h>
60 #include <cam/ctl/ctl_io.h>
61 #include <cam/ctl/ctl.h>
62 #include <cam/ctl/ctl_backend.h>
63 #include <cam/ctl/ctl_error.h>
64 #include <cam/ctl/ctl_frontend.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/icl_wrappers.h>
72 #include <dev/iscsi/iscsi_proto.h>
73 #include <cam/ctl/ctl_frontend_iscsi.h>
75 #ifdef ICL_KERNEL_PROXY
76 #include <sys/socketvar.h>
79 #ifdef ICL_KERNEL_PROXY
80 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
83 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
84 static uma_zone_t cfiscsi_data_wait_zone;
86 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
87 "CAM Target Layer iSCSI Frontend");
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 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
93 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
94 static int login_timeout = 60;
95 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
96 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
97 static int maxcmdsn_delta = 256;
98 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN,
99 &maxcmdsn_delta, 256, "Number of commands the initiator can send "
100 "without confirmation");
102 #define CFISCSI_DEBUG(X, ...) \
105 printf("%s: " X "\n", \
106 __func__, ## __VA_ARGS__); \
110 #define CFISCSI_WARN(X, ...) \
113 printf("WARNING: %s: " X "\n", \
114 __func__, ## __VA_ARGS__); \
118 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
121 printf("%s: %s (%s): " X "\n", \
122 __func__, S->cs_initiator_addr, \
123 S->cs_initiator_name, ## __VA_ARGS__); \
127 #define CFISCSI_SESSION_WARN(S, X, ...) \
130 printf("WARNING: %s (%s): " X "\n", \
131 S->cs_initiator_addr, \
132 S->cs_initiator_name, ## __VA_ARGS__); \
136 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
137 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
138 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
140 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
141 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
142 #define PDU_EXPDATASN(X) (X)->ip_prv0
143 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
144 #define PDU_R2TSN(X) (X)->ip_prv2
146 int cfiscsi_init(void);
147 static void cfiscsi_online(void *arg);
148 static void cfiscsi_offline(void *arg);
149 static int cfiscsi_info(void *arg, struct sbuf *sb);
150 static int cfiscsi_ioctl(struct cdev *dev,
151 u_long cmd, caddr_t addr, int flag, struct thread *td);
152 static void cfiscsi_datamove(union ctl_io *io);
153 static void cfiscsi_datamove_in(union ctl_io *io);
154 static void cfiscsi_datamove_out(union ctl_io *io);
155 static void cfiscsi_done(union ctl_io *io);
156 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
157 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
158 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
159 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
160 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
161 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
162 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
163 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
164 struct cfiscsi_session *cs, union ctl_io *io,
165 uint32_t initiator_task_tag,
166 uint32_t *target_transfer_tagp);
167 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
168 struct cfiscsi_data_wait *cdw);
169 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
170 *softc, const char *name, uint16_t tag);
171 static struct cfiscsi_target *cfiscsi_target_find_or_create(
172 struct cfiscsi_softc *softc, const char *name, const char *alias,
174 static void cfiscsi_target_release(struct cfiscsi_target *ct);
175 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
177 static struct cfiscsi_softc cfiscsi_softc;
179 static struct ctl_frontend cfiscsi_frontend =
182 .init = cfiscsi_init,
183 .ioctl = cfiscsi_ioctl,
185 CTL_FRONTEND_DECLARE(ctlcfiscsi, cfiscsi_frontend);
186 MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
188 static struct icl_pdu *
189 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
192 return (icl_pdu_new(request->ip_conn, flags));
196 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
198 const struct iscsi_bhs_scsi_command *bhssc;
199 struct cfiscsi_session *cs;
200 uint32_t cmdsn, expstatsn;
202 cs = PDU_SESSION(request);
205 * Every incoming PDU - not just NOP-Out - resets the ping timer.
206 * The purpose of the timeout is to reset the connection when it stalls;
207 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
215 * Data-Out PDUs don't contain CmdSN.
217 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
218 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
222 * We're only using fields common for all the request
223 * (initiator -> target) PDUs.
225 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
226 cmdsn = ntohl(bhssc->bhssc_cmdsn);
227 expstatsn = ntohl(bhssc->bhssc_expstatsn);
229 CFISCSI_SESSION_LOCK(cs);
231 if (expstatsn != cs->cs_statsn) {
232 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
233 "while current StatSN is %d", expstatsn,
238 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
240 * The target MUST silently ignore any non-immediate command
241 * outside of this range.
243 if (ISCSI_SNLT(cmdsn, cs->cs_cmdsn) ||
244 ISCSI_SNGT(cmdsn, cs->cs_cmdsn + maxcmdsn_delta)) {
245 CFISCSI_SESSION_UNLOCK(cs);
246 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
247 "while expected %u", cmdsn, cs->cs_cmdsn);
252 * We don't support multiple connections now, so any
253 * discontinuity in CmdSN means lost PDUs. Since we don't
254 * support PDU retransmission -- terminate the connection.
256 if (cmdsn != cs->cs_cmdsn) {
257 CFISCSI_SESSION_UNLOCK(cs);
258 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
259 "while expected %u; dropping connection",
260 cmdsn, cs->cs_cmdsn);
261 cfiscsi_session_terminate(cs);
267 CFISCSI_SESSION_UNLOCK(cs);
273 cfiscsi_pdu_handle(struct icl_pdu *request)
275 struct cfiscsi_session *cs;
278 cs = PDU_SESSION(request);
280 ignore = cfiscsi_pdu_update_cmdsn(request);
282 icl_pdu_free(request);
287 * Handle the PDU; this includes e.g. receiving the remaining
288 * part of PDU and submitting the SCSI command to CTL
289 * or queueing a reply. The handling routine is responsible
290 * for freeing the PDU when it's no longer needed.
292 switch (request->ip_bhs->bhs_opcode &
293 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
294 case ISCSI_BHS_OPCODE_NOP_OUT:
295 cfiscsi_pdu_handle_nop_out(request);
297 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
298 cfiscsi_pdu_handle_scsi_command(request);
300 case ISCSI_BHS_OPCODE_TASK_REQUEST:
301 cfiscsi_pdu_handle_task_request(request);
303 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
304 cfiscsi_pdu_handle_data_out(request);
306 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
307 cfiscsi_pdu_handle_logout_request(request);
310 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
311 "opcode 0x%x; dropping connection",
312 request->ip_bhs->bhs_opcode);
313 icl_pdu_free(request);
314 cfiscsi_session_terminate(cs);
320 cfiscsi_receive_callback(struct icl_pdu *request)
322 struct cfiscsi_session *cs;
324 cs = PDU_SESSION(request);
326 #ifdef ICL_KERNEL_PROXY
327 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
328 if (cs->cs_login_pdu == NULL)
329 cs->cs_login_pdu = request;
331 icl_pdu_free(request);
332 cv_signal(&cs->cs_login_cv);
337 cfiscsi_pdu_handle(request);
341 cfiscsi_error_callback(struct icl_conn *ic)
343 struct cfiscsi_session *cs;
345 cs = CONN_SESSION(ic);
347 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
348 cfiscsi_session_terminate(cs);
352 cfiscsi_pdu_prepare(struct icl_pdu *response)
354 struct cfiscsi_session *cs;
355 struct iscsi_bhs_scsi_response *bhssr;
356 bool advance_statsn = true;
358 cs = PDU_SESSION(response);
360 CFISCSI_SESSION_LOCK_ASSERT(cs);
363 * We're only using fields common for all the response
364 * (target -> initiator) PDUs.
366 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
369 * 10.8.3: "The StatSN for this connection is not advanced
370 * after this PDU is sent."
372 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
373 advance_statsn = false;
376 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
377 * StatSN for the connection is not advanced after this PDU is sent."
379 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
380 bhssr->bhssr_initiator_task_tag == 0xffffffff)
381 advance_statsn = false;
384 * See the comment below - StatSN is not meaningful and must
387 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
388 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
389 advance_statsn = false;
392 * 10.7.3: "The fields StatSN, Status, and Residual Count
393 * only have meaningful content if the S bit is set to 1."
395 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
396 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
397 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
398 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
399 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
408 cfiscsi_pdu_queue(struct icl_pdu *response)
410 struct cfiscsi_session *cs;
412 cs = PDU_SESSION(response);
414 CFISCSI_SESSION_LOCK(cs);
415 cfiscsi_pdu_prepare(response);
416 icl_pdu_queue(response);
417 CFISCSI_SESSION_UNLOCK(cs);
421 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
423 struct cfiscsi_session *cs;
424 struct iscsi_bhs_nop_out *bhsno;
425 struct iscsi_bhs_nop_in *bhsni;
426 struct icl_pdu *response;
431 cs = PDU_SESSION(request);
432 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
434 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
436 * Nothing to do, iscsi_pdu_update_statsn() already
437 * zeroed the timeout.
439 icl_pdu_free(request);
443 datasize = icl_pdu_data_segment_length(request);
445 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
447 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
448 "dropping connection");
449 icl_pdu_free(request);
450 cfiscsi_session_terminate(cs);
453 icl_pdu_get_data(request, 0, data, datasize);
456 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
457 if (response == NULL) {
458 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
459 "droppping connection");
460 free(data, M_CFISCSI);
461 icl_pdu_free(request);
462 cfiscsi_session_terminate(cs);
465 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
466 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
467 bhsni->bhsni_flags = 0x80;
468 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
469 bhsni->bhsni_target_transfer_tag = 0xffffffff;
471 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
473 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
474 "dropping connection");
475 free(data, M_CFISCSI);
476 icl_pdu_free(request);
477 icl_pdu_free(response);
478 cfiscsi_session_terminate(cs);
481 free(data, M_CFISCSI);
484 icl_pdu_free(request);
485 cfiscsi_pdu_queue(response);
489 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
491 struct iscsi_bhs_scsi_command *bhssc;
492 struct cfiscsi_session *cs;
496 cs = PDU_SESSION(request);
497 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
498 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
499 // bhssc->bhssc_initiator_task_tag);
501 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
502 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
503 "ImmediateData=No; dropping connection");
504 icl_pdu_free(request);
505 cfiscsi_session_terminate(cs);
508 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
510 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
511 io->io_hdr.io_type = CTL_IO_SCSI;
512 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
513 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
514 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhssc->bhssc_lun));
515 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
516 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
517 case BHSSC_FLAGS_ATTR_UNTAGGED:
518 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
520 case BHSSC_FLAGS_ATTR_SIMPLE:
521 io->scsiio.tag_type = CTL_TAG_SIMPLE;
523 case BHSSC_FLAGS_ATTR_ORDERED:
524 io->scsiio.tag_type = CTL_TAG_ORDERED;
526 case BHSSC_FLAGS_ATTR_HOQ:
527 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
529 case BHSSC_FLAGS_ATTR_ACA:
530 io->scsiio.tag_type = CTL_TAG_ACA;
533 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
534 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
535 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
538 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
539 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
540 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
541 error = ctl_queue(io);
542 if (error != CTL_RETVAL_COMPLETE) {
543 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
544 "dropping connection", error);
546 refcount_release(&cs->cs_outstanding_ctl_pdus);
547 icl_pdu_free(request);
548 cfiscsi_session_terminate(cs);
553 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
555 struct iscsi_bhs_task_management_request *bhstmr;
556 struct iscsi_bhs_task_management_response *bhstmr2;
557 struct icl_pdu *response;
558 struct cfiscsi_session *cs;
562 cs = PDU_SESSION(request);
563 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
564 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
566 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
567 io->io_hdr.io_type = CTL_IO_TASK;
568 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
569 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
570 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhstmr->bhstmr_lun));
571 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
573 switch (bhstmr->bhstmr_function & ~0x80) {
574 case BHSTMR_FUNCTION_ABORT_TASK:
576 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
578 io->taskio.task_action = CTL_TASK_ABORT_TASK;
579 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
581 case BHSTMR_FUNCTION_ABORT_TASK_SET:
583 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
585 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
587 case BHSTMR_FUNCTION_CLEAR_TASK_SET:
589 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_CLEAR_TASK_SET");
591 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
593 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
595 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
597 io->taskio.task_action = CTL_TASK_LUN_RESET;
599 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
601 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
603 io->taskio.task_action = CTL_TASK_TARGET_RESET;
605 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
607 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
609 io->taskio.task_action = CTL_TASK_TARGET_RESET;
611 case BHSTMR_FUNCTION_QUERY_TASK:
613 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK");
615 io->taskio.task_action = CTL_TASK_QUERY_TASK;
616 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
618 case BHSTMR_FUNCTION_QUERY_TASK_SET:
620 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK_SET");
622 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
624 case BHSTMR_FUNCTION_I_T_NEXUS_RESET:
626 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_I_T_NEXUS_RESET");
628 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
630 case BHSTMR_FUNCTION_QUERY_ASYNC_EVENT:
632 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_ASYNC_EVENT");
634 io->taskio.task_action = CTL_TASK_QUERY_ASYNC_EVENT;
637 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
638 bhstmr->bhstmr_function & ~0x80);
641 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
642 if (response == NULL) {
643 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
644 "dropping connection");
645 icl_pdu_free(request);
646 cfiscsi_session_terminate(cs);
649 bhstmr2 = (struct iscsi_bhs_task_management_response *)
651 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
652 bhstmr2->bhstmr_flags = 0x80;
653 bhstmr2->bhstmr_response =
654 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
655 bhstmr2->bhstmr_initiator_task_tag =
656 bhstmr->bhstmr_initiator_task_tag;
657 icl_pdu_free(request);
658 cfiscsi_pdu_queue(response);
662 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
663 error = ctl_queue(io);
664 if (error != CTL_RETVAL_COMPLETE) {
665 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
666 "dropping connection", error);
668 refcount_release(&cs->cs_outstanding_ctl_pdus);
669 icl_pdu_free(request);
670 cfiscsi_session_terminate(cs);
675 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
677 struct iscsi_bhs_data_out *bhsdo;
678 struct cfiscsi_session *cs;
679 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
680 size_t copy_len, len, off, buffer_offset;
684 cs = PDU_SESSION(request);
686 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
687 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
688 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
689 ISCSI_BHS_OPCODE_SCSI_COMMAND,
690 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
693 * We're only using fields common for Data-Out and SCSI Command PDUs.
695 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
697 io = cdw->cdw_ctl_io;
698 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
699 ("CTL_FLAG_DATA_IN"));
702 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
703 request->ip_data_len, io->scsiio.kern_total_len);
706 if (io->scsiio.kern_sg_entries > 0) {
707 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
708 ctl_sg_count = io->scsiio.kern_sg_entries;
710 ctl_sglist = &ctl_sg_entry;
711 ctl_sglist->addr = io->scsiio.kern_data_ptr;
712 ctl_sglist->len = io->scsiio.kern_data_len;
716 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
717 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
718 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
721 len = icl_pdu_data_segment_length(request);
724 * Make sure the offset, as sent by the initiator, matches the offset
725 * we're supposed to be at in the scatter-gather list.
728 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
729 buffer_offset + len <=
730 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
731 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
732 "expected %zd; dropping connection", buffer_offset,
733 (size_t)io->scsiio.kern_rel_offset +
734 (size_t)io->scsiio.ext_data_filled);
735 ctl_set_data_phase_error(&io->scsiio);
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
745 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
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.
795 * In case of unsolicited data, it's possible that the buffer
796 * provided by CTL is smaller than negotiated FirstBurstLength.
797 * Just ignore the superfluous data; will ask for them with R2T
798 * on next call to cfiscsi_datamove().
800 * This obviously can only happen with SCSI Command PDU.
802 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
803 ISCSI_BHS_OPCODE_SCSI_COMMAND)
806 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
807 "expected %zd; dropping connection",
808 icl_pdu_data_segment_length(request), off);
809 ctl_set_data_phase_error(&io->scsiio);
810 cfiscsi_session_terminate(cs);
814 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
815 (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 ctl_set_data_phase_error(&io->scsiio);
820 cfiscsi_session_terminate(cs);
824 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
825 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
826 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
827 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
828 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
829 "transmitted size was %zd bytes instead of %d; "
830 "dropping connection",
831 (size_t)io->scsiio.ext_data_filled,
833 ctl_set_data_phase_error(&io->scsiio);
834 cfiscsi_session_terminate(cs);
838 * For SCSI Command PDU, this just means we need to
839 * solicit more data by sending R2T.
845 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
847 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
848 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
858 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
860 struct iscsi_bhs_data_out *bhsdo;
861 struct cfiscsi_session *cs;
862 struct cfiscsi_data_wait *cdw = NULL;
866 cs = PDU_SESSION(request);
867 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
869 CFISCSI_SESSION_LOCK(cs);
870 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
872 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
873 "ttt 0x%x, itt 0x%x",
874 bhsdo->bhsdo_target_transfer_tag,
875 bhsdo->bhsdo_initiator_task_tag,
876 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
878 if (bhsdo->bhsdo_target_transfer_tag ==
879 cdw->cdw_target_transfer_tag)
882 CFISCSI_SESSION_UNLOCK(cs);
884 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
885 "0x%x, not found; dropping connection",
886 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
887 icl_pdu_free(request);
888 cfiscsi_session_terminate(cs);
892 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
893 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
894 "DataSN %u, while expected %u; dropping connection",
895 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
896 icl_pdu_free(request);
897 cfiscsi_session_terminate(cs);
902 io = cdw->cdw_ctl_io;
903 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
904 ("CTL_FLAG_DATA_IN"));
906 done = cfiscsi_handle_data_segment(request, cdw);
908 CFISCSI_SESSION_LOCK(cs);
909 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
910 CFISCSI_SESSION_UNLOCK(cs);
911 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
912 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
913 cfiscsi_data_wait_free(cs, cdw);
914 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
916 io->scsiio.be_move_done(io);
918 cfiscsi_datamove_out(io);
921 icl_pdu_free(request);
925 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
927 struct iscsi_bhs_logout_request *bhslr;
928 struct iscsi_bhs_logout_response *bhslr2;
929 struct icl_pdu *response;
930 struct cfiscsi_session *cs;
932 cs = PDU_SESSION(request);
933 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
934 switch (bhslr->bhslr_reason & 0x7f) {
935 case BHSLR_REASON_CLOSE_SESSION:
936 case BHSLR_REASON_CLOSE_CONNECTION:
937 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
938 if (response == NULL) {
939 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
940 icl_pdu_free(request);
941 cfiscsi_session_terminate(cs);
944 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
945 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
946 bhslr2->bhslr_flags = 0x80;
947 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
948 bhslr2->bhslr_initiator_task_tag =
949 bhslr->bhslr_initiator_task_tag;
950 icl_pdu_free(request);
951 cfiscsi_pdu_queue(response);
952 cfiscsi_session_terminate(cs);
954 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
955 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
956 if (response == NULL) {
957 CFISCSI_SESSION_WARN(cs,
958 "failed to allocate memory; dropping connection");
959 icl_pdu_free(request);
960 cfiscsi_session_terminate(cs);
963 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
964 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
965 bhslr2->bhslr_flags = 0x80;
966 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
967 bhslr2->bhslr_initiator_task_tag =
968 bhslr->bhslr_initiator_task_tag;
969 icl_pdu_free(request);
970 cfiscsi_pdu_queue(response);
973 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
974 bhslr->bhslr_reason);
975 icl_pdu_free(request);
976 cfiscsi_session_terminate(cs);
982 cfiscsi_callout(void *context)
985 struct iscsi_bhs_nop_in *bhsni;
986 struct cfiscsi_session *cs;
990 if (cs->cs_terminating)
993 callout_schedule(&cs->cs_callout, 1 * hz);
995 atomic_add_int(&cs->cs_timeout, 1);
997 #ifdef ICL_KERNEL_PROXY
998 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
999 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1000 CFISCSI_SESSION_WARN(cs, "login timed out after "
1001 "%d seconds; dropping connection", cs->cs_timeout);
1002 cfiscsi_session_terminate(cs);
1008 if (ping_timeout <= 0) {
1010 * Pings are disabled. Don't send NOP-In in this case;
1011 * user might have disabled pings to work around problems
1012 * with certain initiators that can't properly handle
1013 * NOP-In, such as iPXE. Reset the timeout, to avoid
1014 * triggering reconnection, should the user decide to
1021 if (cs->cs_timeout >= ping_timeout) {
1022 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1023 "dropping connection", ping_timeout);
1024 cfiscsi_session_terminate(cs);
1029 * If the ping was reset less than one second ago - which means
1030 * that we've received some PDU during the last second - assume
1031 * the traffic flows correctly and don't bother sending a NOP-Out.
1033 * (It's 2 - one for one second, and one for incrementing is_timeout
1034 * earlier in this routine.)
1036 if (cs->cs_timeout < 2)
1039 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1041 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1044 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1045 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1046 bhsni->bhsni_flags = 0x80;
1047 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1049 cfiscsi_pdu_queue(cp);
1052 static struct cfiscsi_data_wait *
1053 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1054 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1056 struct cfiscsi_data_wait *cdw;
1059 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1061 CFISCSI_SESSION_WARN(cs,
1062 "failed to allocate %zd bytes", sizeof(*cdw));
1066 error = icl_conn_transfer_setup(cs->cs_conn, io, target_transfer_tagp,
1069 CFISCSI_SESSION_WARN(cs,
1070 "icl_conn_transfer_setup() failed with error %d", error);
1071 uma_zfree(cfiscsi_data_wait_zone, cdw);
1075 cdw->cdw_ctl_io = io;
1076 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1077 cdw->cdw_initiator_task_tag = initiator_task_tag;
1083 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1084 struct cfiscsi_data_wait *cdw)
1087 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1088 uma_zfree(cfiscsi_data_wait_zone, cdw);
1092 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1094 struct cfiscsi_data_wait *cdw;
1096 int error, last, wait;
1098 if (cs->cs_target == NULL)
1099 return; /* No target yet, so nothing to do. */
1100 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1102 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1103 io->io_hdr.io_type = CTL_IO_TASK;
1104 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1105 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1106 io->io_hdr.nexus.targ_lun = 0;
1107 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1108 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1109 wait = cs->cs_outstanding_ctl_pdus;
1110 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1111 error = ctl_queue(io);
1112 if (error != CTL_RETVAL_COMPLETE) {
1113 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1114 refcount_release(&cs->cs_outstanding_ctl_pdus);
1118 CFISCSI_SESSION_LOCK(cs);
1119 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1120 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1121 CFISCSI_SESSION_UNLOCK(cs);
1123 * Set nonzero port status; this prevents backends from
1124 * assuming that the data transfer actually succeeded
1125 * and writing uninitialized data to disk.
1127 cdw->cdw_ctl_io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1128 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1129 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1130 cfiscsi_data_wait_free(cs, cdw);
1131 CFISCSI_SESSION_LOCK(cs);
1133 CFISCSI_SESSION_UNLOCK(cs);
1136 * Wait for CTL to terminate all the tasks.
1139 CFISCSI_SESSION_WARN(cs,
1140 "waiting for CTL to terminate %d tasks", wait);
1142 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1143 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1146 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1147 0, "cfiscsi_terminate", hz / 100);
1150 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1154 cfiscsi_maintenance_thread(void *arg)
1156 struct cfiscsi_session *cs;
1161 CFISCSI_SESSION_LOCK(cs);
1162 if (cs->cs_terminating == false)
1163 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1164 CFISCSI_SESSION_UNLOCK(cs);
1166 if (cs->cs_terminating) {
1169 * We used to wait up to 30 seconds to deliver queued
1170 * PDUs to the initiator. We also tried hard to deliver
1171 * SCSI Responses for the aborted PDUs. We don't do
1172 * that anymore. We might need to revisit that.
1174 callout_drain(&cs->cs_callout);
1175 icl_conn_close(cs->cs_conn);
1178 * At this point ICL receive thread is no longer
1179 * running; no new tasks can be queued.
1181 cfiscsi_session_terminate_tasks(cs);
1182 cfiscsi_session_delete(cs);
1186 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1191 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1194 if (cs->cs_terminating)
1196 cs->cs_terminating = true;
1197 cv_signal(&cs->cs_maintenance_cv);
1198 #ifdef ICL_KERNEL_PROXY
1199 cv_signal(&cs->cs_login_cv);
1204 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1206 struct cfiscsi_target *ct;
1210 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1213 name = strdup(cs->cs_initiator_id, M_CTL);
1214 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1216 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1218 cs->cs_ctl_initid = -1;
1221 cs->cs_ctl_initid = i;
1223 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1230 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1234 if (cs->cs_ctl_initid == -1)
1237 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1239 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1242 cs->cs_ctl_initid = -1;
1245 static struct cfiscsi_session *
1246 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1248 struct cfiscsi_session *cs;
1251 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1253 CFISCSI_WARN("malloc failed");
1256 cs->cs_ctl_initid = -1;
1258 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1259 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1260 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1261 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1262 #ifdef ICL_KERNEL_PROXY
1263 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1266 cs->cs_conn = icl_new_conn(offload, "cfiscsi", &cs->cs_lock);
1267 if (cs->cs_conn == NULL) {
1268 free(cs, M_CFISCSI);
1271 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1272 cs->cs_conn->ic_error = cfiscsi_error_callback;
1273 cs->cs_conn->ic_prv0 = cs;
1275 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1277 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1278 free(cs, M_CFISCSI);
1282 mtx_lock(&softc->lock);
1283 cs->cs_id = ++softc->last_session_id;
1284 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1285 mtx_unlock(&softc->lock);
1288 * Start pinging the initiator.
1290 callout_init(&cs->cs_callout, 1);
1291 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1297 cfiscsi_session_delete(struct cfiscsi_session *cs)
1299 struct cfiscsi_softc *softc;
1301 softc = &cfiscsi_softc;
1303 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1304 ("destroying session with outstanding CTL pdus"));
1305 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1306 ("destroying session with non-empty queue"));
1308 cfiscsi_session_unregister_initiator(cs);
1309 if (cs->cs_target != NULL)
1310 cfiscsi_target_release(cs->cs_target);
1311 icl_conn_close(cs->cs_conn);
1312 icl_conn_free(cs->cs_conn);
1314 mtx_lock(&softc->lock);
1315 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1316 cv_signal(&softc->sessions_cv);
1317 mtx_unlock(&softc->lock);
1319 free(cs, M_CFISCSI);
1325 struct cfiscsi_softc *softc;
1327 softc = &cfiscsi_softc;
1328 bzero(softc, sizeof(*softc));
1329 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1331 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1332 #ifdef ICL_KERNEL_PROXY
1333 cv_init(&softc->accept_cv, "cfiscsi_accept");
1335 TAILQ_INIT(&softc->sessions);
1336 TAILQ_INIT(&softc->targets);
1338 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1339 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1345 #ifdef ICL_KERNEL_PROXY
1347 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1349 struct cfiscsi_session *cs;
1351 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1353 CFISCSI_WARN("failed to create session");
1357 icl_conn_handoff_sock(cs->cs_conn, so);
1358 cs->cs_initiator_sa = sa;
1359 cs->cs_portal_id = portal_id;
1360 cs->cs_waiting_for_ctld = true;
1361 cv_signal(&cfiscsi_softc.accept_cv);
1366 cfiscsi_online(void *arg)
1368 struct cfiscsi_softc *softc;
1369 struct cfiscsi_target *ct;
1372 ct = (struct cfiscsi_target *)arg;
1373 softc = ct->ct_softc;
1375 mtx_lock(&softc->lock);
1376 if (ct->ct_online) {
1377 mtx_unlock(&softc->lock);
1381 online = softc->online++;
1382 mtx_unlock(&softc->lock);
1386 #ifdef ICL_KERNEL_PROXY
1387 if (softc->listener != NULL)
1388 icl_listen_free(softc->listener);
1389 softc->listener = icl_listen_new(cfiscsi_accept);
1394 cfiscsi_offline(void *arg)
1396 struct cfiscsi_softc *softc;
1397 struct cfiscsi_target *ct;
1398 struct cfiscsi_session *cs;
1401 ct = (struct cfiscsi_target *)arg;
1402 softc = ct->ct_softc;
1404 mtx_lock(&softc->lock);
1405 if (!ct->ct_online) {
1406 mtx_unlock(&softc->lock);
1410 online = --softc->online;
1412 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1413 if (cs->cs_target == ct)
1414 cfiscsi_session_terminate(cs);
1417 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1418 if (cs->cs_target == ct)
1422 cv_wait(&softc->sessions_cv, &softc->lock);
1423 } while (cs != NULL && ct->ct_online == 0);
1424 mtx_unlock(&softc->lock);
1428 #ifdef ICL_KERNEL_PROXY
1429 icl_listen_free(softc->listener);
1430 softc->listener = NULL;
1435 cfiscsi_info(void *arg, struct sbuf *sb)
1437 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1440 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1446 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1448 struct cfiscsi_softc *softc;
1449 struct cfiscsi_session *cs, *cs2;
1450 struct cfiscsi_target *ct;
1451 struct ctl_iscsi_handoff_params *cihp;
1454 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1455 softc = &cfiscsi_softc;
1457 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1458 cihp->initiator_name, cihp->initiator_addr,
1461 ct = cfiscsi_target_find(softc, cihp->target_name,
1462 cihp->portal_group_tag);
1464 ci->status = CTL_ISCSI_ERROR;
1465 snprintf(ci->error_str, sizeof(ci->error_str),
1466 "%s: target not found", __func__);
1470 #ifdef ICL_KERNEL_PROXY
1471 if (cihp->socket > 0 && cihp->connection_id > 0) {
1472 snprintf(ci->error_str, sizeof(ci->error_str),
1473 "both socket and connection_id set");
1474 ci->status = CTL_ISCSI_ERROR;
1475 cfiscsi_target_release(ct);
1478 if (cihp->socket == 0) {
1479 mtx_lock(&cfiscsi_softc.lock);
1480 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1481 if (cs->cs_id == cihp->connection_id)
1485 mtx_unlock(&cfiscsi_softc.lock);
1486 snprintf(ci->error_str, sizeof(ci->error_str),
1487 "connection not found");
1488 ci->status = CTL_ISCSI_ERROR;
1489 cfiscsi_target_release(ct);
1492 mtx_unlock(&cfiscsi_softc.lock);
1495 cs = cfiscsi_session_new(softc, cihp->offload);
1497 ci->status = CTL_ISCSI_ERROR;
1498 snprintf(ci->error_str, sizeof(ci->error_str),
1499 "%s: cfiscsi_session_new failed", __func__);
1500 cfiscsi_target_release(ct);
1503 #ifdef ICL_KERNEL_PROXY
1508 * First PDU of Full Feature phase has the same CmdSN as the last
1509 * PDU from the Login Phase received from the initiator. Thus,
1512 cs->cs_cmdsn = cihp->cmdsn;
1513 cs->cs_statsn = cihp->statsn;
1514 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1515 cs->cs_max_burst_length = cihp->max_burst_length;
1516 cs->cs_immediate_data = !!cihp->immediate_data;
1517 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1518 cs->cs_conn->ic_header_crc32c = true;
1519 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1520 cs->cs_conn->ic_data_crc32c = true;
1522 strlcpy(cs->cs_initiator_name,
1523 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1524 strlcpy(cs->cs_initiator_addr,
1525 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1526 strlcpy(cs->cs_initiator_alias,
1527 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1528 memcpy(cs->cs_initiator_isid,
1529 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1530 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1531 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1532 cihp->initiator_isid[0], cihp->initiator_isid[1],
1533 cihp->initiator_isid[2], cihp->initiator_isid[3],
1534 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1536 mtx_lock(&softc->lock);
1537 if (ct->ct_online == 0) {
1538 mtx_unlock(&softc->lock);
1539 cfiscsi_session_terminate(cs);
1540 cfiscsi_target_release(ct);
1541 ci->status = CTL_ISCSI_ERROR;
1542 snprintf(ci->error_str, sizeof(ci->error_str),
1543 "%s: port offline", __func__);
1547 mtx_unlock(&softc->lock);
1549 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1551 if (!cs->cs_terminating) {
1552 mtx_lock(&softc->lock);
1553 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1554 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1555 cs->cs_target == cs2->cs_target &&
1556 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1557 if (strcmp(cs->cs_initiator_addr,
1558 cs2->cs_initiator_addr) != 0) {
1559 CFISCSI_SESSION_WARN(cs2,
1560 "session reinstatement from "
1561 "different address %s",
1562 cs->cs_initiator_addr);
1564 CFISCSI_SESSION_DEBUG(cs2,
1565 "session reinstatement");
1567 cfiscsi_session_terminate(cs2);
1568 mtx_unlock(&softc->lock);
1569 pause("cfiscsi_reinstate", 1);
1573 mtx_unlock(&softc->lock);
1577 * Register initiator with CTL.
1579 cfiscsi_session_register_initiator(cs);
1581 #ifdef ICL_KERNEL_PROXY
1582 if (cihp->socket > 0) {
1584 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1586 cfiscsi_session_terminate(cs);
1587 refcount_release(&cs->cs_outstanding_ctl_pdus);
1588 ci->status = CTL_ISCSI_ERROR;
1589 snprintf(ci->error_str, sizeof(ci->error_str),
1590 "%s: icl_conn_handoff failed with error %d",
1594 #ifdef ICL_KERNEL_PROXY
1598 #ifdef ICL_KERNEL_PROXY
1599 cs->cs_login_phase = false;
1602 * First PDU of the Full Feature phase has likely already arrived.
1603 * We have to pick it up and execute properly.
1605 if (cs->cs_login_pdu != NULL) {
1606 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1607 cfiscsi_pdu_handle(cs->cs_login_pdu);
1608 cs->cs_login_pdu = NULL;
1612 refcount_release(&cs->cs_outstanding_ctl_pdus);
1613 ci->status = CTL_ISCSI_OK;
1617 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1619 struct ctl_iscsi_list_params *cilp;
1620 struct cfiscsi_session *cs;
1621 struct cfiscsi_softc *softc;
1625 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1626 softc = &cfiscsi_softc;
1628 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1630 ci->status = CTL_ISCSI_ERROR;
1631 snprintf(ci->error_str, sizeof(ci->error_str),
1632 "Unable to allocate %d bytes for iSCSI session list",
1637 sbuf_printf(sb, "<ctlislist>\n");
1638 mtx_lock(&softc->lock);
1639 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1640 #ifdef ICL_KERNEL_PROXY
1641 if (cs->cs_target == NULL)
1644 error = sbuf_printf(sb, "<connection id=\"%d\">"
1645 "<initiator>%s</initiator>"
1646 "<initiator_addr>%s</initiator_addr>"
1647 "<initiator_alias>%s</initiator_alias>"
1648 "<target>%s</target>"
1649 "<target_alias>%s</target_alias>"
1650 "<target_portal_group_tag>%u</target_portal_group_tag>"
1651 "<header_digest>%s</header_digest>"
1652 "<data_digest>%s</data_digest>"
1653 "<max_data_segment_length>%zd</max_data_segment_length>"
1654 "<immediate_data>%d</immediate_data>"
1656 "<offload>%s</offload>"
1659 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1660 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1661 cs->cs_target->ct_tag,
1662 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1663 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1664 cs->cs_max_data_segment_length,
1665 cs->cs_immediate_data,
1666 cs->cs_conn->ic_iser,
1667 cs->cs_conn->ic_offload);
1671 mtx_unlock(&softc->lock);
1672 error = sbuf_printf(sb, "</ctlislist>\n");
1675 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1676 snprintf(ci->error_str, sizeof(ci->error_str),
1677 "Out of space, %d bytes is too small", cilp->alloc_len);
1682 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1683 cilp->fill_len = sbuf_len(sb) + 1;
1684 ci->status = CTL_ISCSI_OK;
1689 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1691 struct icl_pdu *response;
1692 struct iscsi_bhs_asynchronous_message *bhsam;
1693 struct ctl_iscsi_logout_params *cilp;
1694 struct cfiscsi_session *cs;
1695 struct cfiscsi_softc *softc;
1698 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1699 softc = &cfiscsi_softc;
1701 mtx_lock(&softc->lock);
1702 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1703 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1704 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1705 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1708 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1709 if (response == NULL) {
1710 ci->status = CTL_ISCSI_ERROR;
1711 snprintf(ci->error_str, sizeof(ci->error_str),
1712 "Unable to allocate memory");
1713 mtx_unlock(&softc->lock);
1717 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1718 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1719 bhsam->bhsam_flags = 0x80;
1720 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1721 bhsam->bhsam_parameter3 = htons(10);
1722 cfiscsi_pdu_queue(response);
1725 mtx_unlock(&softc->lock);
1728 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1729 snprintf(ci->error_str, sizeof(ci->error_str),
1730 "No matching connections found");
1734 ci->status = CTL_ISCSI_OK;
1738 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1740 struct icl_pdu *response;
1741 struct iscsi_bhs_asynchronous_message *bhsam;
1742 struct ctl_iscsi_terminate_params *citp;
1743 struct cfiscsi_session *cs;
1744 struct cfiscsi_softc *softc;
1747 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1748 softc = &cfiscsi_softc;
1750 mtx_lock(&softc->lock);
1751 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1752 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1753 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1754 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1757 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1758 if (response == NULL) {
1760 * Oh well. Just terminate the connection.
1763 bhsam = (struct iscsi_bhs_asynchronous_message *)
1765 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1766 bhsam->bhsam_flags = 0x80;
1767 bhsam->bhsam_0xffffffff = 0xffffffff;
1768 bhsam->bhsam_async_event =
1769 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1770 cfiscsi_pdu_queue(response);
1772 cfiscsi_session_terminate(cs);
1775 mtx_unlock(&softc->lock);
1778 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1779 snprintf(ci->error_str, sizeof(ci->error_str),
1780 "No matching connections found");
1784 ci->status = CTL_ISCSI_OK;
1788 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1790 struct ctl_iscsi_limits_params *cilp;
1793 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1795 error = icl_limits(cilp->offload, &cilp->data_segment_limit);
1797 ci->status = CTL_ISCSI_ERROR;
1798 snprintf(ci->error_str, sizeof(ci->error_str),
1799 "%s: icl_limits failed with error %d",
1804 ci->status = CTL_ISCSI_OK;
1807 #ifdef ICL_KERNEL_PROXY
1809 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1811 struct ctl_iscsi_listen_params *cilp;
1812 struct sockaddr *sa;
1815 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1817 if (cfiscsi_softc.listener == NULL) {
1818 CFISCSI_DEBUG("no listener");
1819 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1820 ci->status = CTL_ISCSI_ERROR;
1824 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1826 CFISCSI_DEBUG("getsockaddr, error %d", error);
1827 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1828 ci->status = CTL_ISCSI_ERROR;
1832 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1833 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1836 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1837 snprintf(ci->error_str, sizeof(ci->error_str),
1838 "icl_listen_add failed, error %d", error);
1839 ci->status = CTL_ISCSI_ERROR;
1843 ci->status = CTL_ISCSI_OK;
1847 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1849 struct ctl_iscsi_accept_params *ciap;
1850 struct cfiscsi_session *cs;
1853 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1855 mtx_lock(&cfiscsi_softc.lock);
1857 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1858 if (cs->cs_waiting_for_ctld)
1863 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1865 mtx_unlock(&cfiscsi_softc.lock);
1866 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1867 ci->status = CTL_ISCSI_ERROR;
1871 mtx_unlock(&cfiscsi_softc.lock);
1873 cs->cs_waiting_for_ctld = false;
1874 cs->cs_login_phase = true;
1876 ciap->connection_id = cs->cs_id;
1877 ciap->portal_id = cs->cs_portal_id;
1878 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1879 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1880 cs->cs_initiator_sa->sa_len);
1882 snprintf(ci->error_str, sizeof(ci->error_str),
1883 "copyout failed with error %d", error);
1884 ci->status = CTL_ISCSI_ERROR;
1888 ci->status = CTL_ISCSI_OK;
1892 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1894 struct ctl_iscsi_send_params *cisp;
1895 struct cfiscsi_session *cs;
1901 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1903 mtx_lock(&cfiscsi_softc.lock);
1904 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1905 if (cs->cs_id == cisp->connection_id)
1909 mtx_unlock(&cfiscsi_softc.lock);
1910 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1911 ci->status = CTL_ISCSI_ERROR;
1914 mtx_unlock(&cfiscsi_softc.lock);
1917 if (cs->cs_login_phase == false)
1921 if (cs->cs_terminating) {
1922 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1923 ci->status = CTL_ISCSI_ERROR;
1927 datalen = cisp->data_segment_len;
1931 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1932 if (datalen > 65535) {
1933 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1934 ci->status = CTL_ISCSI_ERROR;
1938 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1939 error = copyin(cisp->data_segment, data, datalen);
1941 free(data, M_CFISCSI);
1942 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1943 ci->status = CTL_ISCSI_ERROR;
1948 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1949 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1951 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1952 free(data, M_CFISCSI);
1954 CFISCSI_SESSION_LOCK(cs);
1956 CFISCSI_SESSION_UNLOCK(cs);
1957 ci->status = CTL_ISCSI_OK;
1961 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1963 struct ctl_iscsi_receive_params *cirp;
1964 struct cfiscsi_session *cs;
1969 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1971 mtx_lock(&cfiscsi_softc.lock);
1972 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1973 if (cs->cs_id == cirp->connection_id)
1977 mtx_unlock(&cfiscsi_softc.lock);
1978 snprintf(ci->error_str, sizeof(ci->error_str),
1979 "connection not found");
1980 ci->status = CTL_ISCSI_ERROR;
1983 mtx_unlock(&cfiscsi_softc.lock);
1986 if (is->is_login_phase == false)
1990 CFISCSI_SESSION_LOCK(cs);
1991 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
1992 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
1994 CFISCSI_SESSION_UNLOCK(cs);
1995 snprintf(ci->error_str, sizeof(ci->error_str),
1996 "interrupted by signal");
1997 ci->status = CTL_ISCSI_ERROR;
2002 if (cs->cs_terminating) {
2003 CFISCSI_SESSION_UNLOCK(cs);
2004 snprintf(ci->error_str, sizeof(ci->error_str),
2005 "connection terminating");
2006 ci->status = CTL_ISCSI_ERROR;
2009 ip = cs->cs_login_pdu;
2010 cs->cs_login_pdu = NULL;
2011 CFISCSI_SESSION_UNLOCK(cs);
2013 if (ip->ip_data_len > cirp->data_segment_len) {
2015 snprintf(ci->error_str, sizeof(ci->error_str),
2016 "data segment too big");
2017 ci->status = CTL_ISCSI_ERROR;
2021 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2022 if (ip->ip_data_len > 0) {
2023 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2024 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2025 copyout(data, cirp->data_segment, ip->ip_data_len);
2026 free(data, M_CFISCSI);
2030 ci->status = CTL_ISCSI_OK;
2033 #endif /* !ICL_KERNEL_PROXY */
2036 cfiscsi_ioctl_port_create(struct ctl_req *req)
2038 struct cfiscsi_target *ct;
2039 struct ctl_port *port;
2040 const char *target, *alias, *tags;
2041 struct scsi_vpd_id_descriptor *desc;
2043 int retval, len, idlen;
2046 ctl_init_opts(&opts, req->num_args, req->kern_args);
2047 target = ctl_get_opt(&opts, "cfiscsi_target");
2048 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
2049 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2050 if (target == NULL || tags == NULL) {
2051 req->status = CTL_LUN_ERROR;
2052 snprintf(req->error_str, sizeof(req->error_str),
2053 "Missing required argument");
2054 ctl_free_opts(&opts);
2057 tag = strtol(tags, (char **)NULL, 10);
2058 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2060 req->status = CTL_LUN_ERROR;
2061 snprintf(req->error_str, sizeof(req->error_str),
2062 "failed to create target \"%s\"", target);
2063 ctl_free_opts(&opts);
2066 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2067 req->status = CTL_LUN_ERROR;
2068 snprintf(req->error_str, sizeof(req->error_str),
2069 "target \"%s\" already exists", target);
2070 cfiscsi_target_release(ct);
2071 ctl_free_opts(&opts);
2074 port = &ct->ct_port;
2076 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2079 port->frontend = &cfiscsi_frontend;
2080 port->port_type = CTL_PORT_ISCSI;
2081 /* XXX KDM what should the real number be here? */
2082 port->num_requested_ctl_io = 4096;
2083 port->port_name = "iscsi";
2084 port->physical_port = tag;
2085 port->virtual_port = ct->ct_target_id;
2086 port->port_online = cfiscsi_online;
2087 port->port_offline = cfiscsi_offline;
2088 port->port_info = cfiscsi_info;
2089 port->onoff_arg = ct;
2090 port->fe_datamove = cfiscsi_datamove;
2091 port->fe_done = cfiscsi_done;
2093 /* XXX KDM what should we report here? */
2094 /* XXX These should probably be fetched from CTL. */
2095 port->max_targets = 1;
2096 port->max_target_id = 15;
2097 port->targ_port = -1;
2099 port->options = opts;
2102 /* Generate Port ID. */
2103 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2104 idlen = roundup2(idlen, 4);
2105 len = sizeof(struct scsi_vpd_device_id) + idlen;
2106 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2107 M_CTL, M_WAITOK | M_ZERO);
2108 port->port_devid->len = len;
2109 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2110 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2111 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2112 SVPD_ID_TYPE_SCSI_NAME;
2113 desc->length = idlen;
2114 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2116 /* Generate Target ID. */
2117 idlen = strlen(target) + 1;
2118 idlen = roundup2(idlen, 4);
2119 len = sizeof(struct scsi_vpd_device_id) + idlen;
2120 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2121 M_CTL, M_WAITOK | M_ZERO);
2122 port->target_devid->len = len;
2123 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2124 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2125 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2126 SVPD_ID_TYPE_SCSI_NAME;
2127 desc->length = idlen;
2128 strlcpy(desc->identifier, target, idlen);
2130 retval = ctl_port_register(port);
2132 ctl_free_opts(&port->options);
2133 cfiscsi_target_release(ct);
2134 free(port->port_devid, M_CFISCSI);
2135 free(port->target_devid, M_CFISCSI);
2136 req->status = CTL_LUN_ERROR;
2137 snprintf(req->error_str, sizeof(req->error_str),
2138 "ctl_port_register() failed with error %d", retval);
2142 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2143 req->status = CTL_LUN_OK;
2144 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2145 sizeof(port->targ_port)); //XXX
2149 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2151 struct cfiscsi_target *ct;
2152 const char *target, *tags;
2156 ctl_init_opts(&opts, req->num_args, req->kern_args);
2157 target = ctl_get_opt(&opts, "cfiscsi_target");
2158 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2159 if (target == NULL || tags == NULL) {
2160 ctl_free_opts(&opts);
2161 req->status = CTL_LUN_ERROR;
2162 snprintf(req->error_str, sizeof(req->error_str),
2163 "Missing required argument");
2166 tag = strtol(tags, (char **)NULL, 10);
2167 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2169 ctl_free_opts(&opts);
2170 req->status = CTL_LUN_ERROR;
2171 snprintf(req->error_str, sizeof(req->error_str),
2172 "can't find target \"%s\"", target);
2175 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2176 ctl_free_opts(&opts);
2177 req->status = CTL_LUN_ERROR;
2178 snprintf(req->error_str, sizeof(req->error_str),
2179 "target \"%s\" is already dying", target);
2182 ctl_free_opts(&opts);
2184 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2185 ctl_port_offline(&ct->ct_port);
2186 cfiscsi_target_release(ct);
2187 cfiscsi_target_release(ct);
2188 req->status = CTL_LUN_OK;
2192 cfiscsi_ioctl(struct cdev *dev,
2193 u_long cmd, caddr_t addr, int flag, struct thread *td)
2195 struct ctl_iscsi *ci;
2196 struct ctl_req *req;
2198 if (cmd == CTL_PORT_REQ) {
2199 req = (struct ctl_req *)addr;
2200 switch (req->reqtype) {
2201 case CTL_REQ_CREATE:
2202 cfiscsi_ioctl_port_create(req);
2204 case CTL_REQ_REMOVE:
2205 cfiscsi_ioctl_port_remove(req);
2208 req->status = CTL_LUN_ERROR;
2209 snprintf(req->error_str, sizeof(req->error_str),
2210 "Unsupported request type %d", req->reqtype);
2215 if (cmd != CTL_ISCSI)
2218 ci = (struct ctl_iscsi *)addr;
2220 case CTL_ISCSI_HANDOFF:
2221 cfiscsi_ioctl_handoff(ci);
2223 case CTL_ISCSI_LIST:
2224 cfiscsi_ioctl_list(ci);
2226 case CTL_ISCSI_LOGOUT:
2227 cfiscsi_ioctl_logout(ci);
2229 case CTL_ISCSI_TERMINATE:
2230 cfiscsi_ioctl_terminate(ci);
2232 case CTL_ISCSI_LIMITS:
2233 cfiscsi_ioctl_limits(ci);
2235 #ifdef ICL_KERNEL_PROXY
2236 case CTL_ISCSI_LISTEN:
2237 cfiscsi_ioctl_listen(ci);
2239 case CTL_ISCSI_ACCEPT:
2240 cfiscsi_ioctl_accept(ci);
2242 case CTL_ISCSI_SEND:
2243 cfiscsi_ioctl_send(ci);
2245 case CTL_ISCSI_RECEIVE:
2246 cfiscsi_ioctl_receive(ci);
2249 case CTL_ISCSI_LISTEN:
2250 case CTL_ISCSI_ACCEPT:
2251 case CTL_ISCSI_SEND:
2252 case CTL_ISCSI_RECEIVE:
2253 ci->status = CTL_ISCSI_ERROR;
2254 snprintf(ci->error_str, sizeof(ci->error_str),
2255 "%s: CTL compiled without ICL_KERNEL_PROXY",
2258 #endif /* !ICL_KERNEL_PROXY */
2260 ci->status = CTL_ISCSI_ERROR;
2261 snprintf(ci->error_str, sizeof(ci->error_str),
2262 "%s: invalid iSCSI request type %d", __func__, ci->type);
2270 cfiscsi_target_hold(struct cfiscsi_target *ct)
2273 refcount_acquire(&ct->ct_refcount);
2277 cfiscsi_target_release(struct cfiscsi_target *ct)
2279 struct cfiscsi_softc *softc;
2281 softc = ct->ct_softc;
2282 mtx_lock(&softc->lock);
2283 if (refcount_release(&ct->ct_refcount)) {
2284 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2285 mtx_unlock(&softc->lock);
2286 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2287 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2288 if (ctl_port_deregister(&ct->ct_port) != 0)
2289 printf("%s: ctl_port_deregister() failed\n",
2292 free(ct, M_CFISCSI);
2296 mtx_unlock(&softc->lock);
2299 static struct cfiscsi_target *
2300 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2302 struct cfiscsi_target *ct;
2304 mtx_lock(&softc->lock);
2305 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2306 if (ct->ct_tag != tag ||
2307 strcmp(name, ct->ct_name) != 0 ||
2308 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2310 cfiscsi_target_hold(ct);
2311 mtx_unlock(&softc->lock);
2314 mtx_unlock(&softc->lock);
2319 static struct cfiscsi_target *
2320 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2321 const char *alias, uint16_t tag)
2323 struct cfiscsi_target *ct, *newct;
2325 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2328 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2330 mtx_lock(&softc->lock);
2331 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2332 if (ct->ct_tag != tag ||
2333 strcmp(name, ct->ct_name) != 0 ||
2334 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2336 cfiscsi_target_hold(ct);
2337 mtx_unlock(&softc->lock);
2338 free(newct, M_CFISCSI);
2342 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2344 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2345 newct->ct_tag = tag;
2346 refcount_init(&newct->ct_refcount, 1);
2347 newct->ct_softc = softc;
2348 if (TAILQ_EMPTY(&softc->targets))
2349 softc->last_target_id = 0;
2350 newct->ct_target_id = ++softc->last_target_id;
2351 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2352 mtx_unlock(&softc->lock);
2358 cfiscsi_datamove_in(union ctl_io *io)
2360 struct cfiscsi_session *cs;
2361 struct icl_pdu *request, *response;
2362 const struct iscsi_bhs_scsi_command *bhssc;
2363 struct iscsi_bhs_data_in *bhsdi;
2364 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2365 size_t len, expected_len, sg_len, buffer_offset;
2366 const char *sg_addr;
2367 int ctl_sg_count, error, i;
2369 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2370 cs = PDU_SESSION(request);
2372 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2373 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2374 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2375 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2377 if (io->scsiio.kern_sg_entries > 0) {
2378 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2379 ctl_sg_count = io->scsiio.kern_sg_entries;
2381 ctl_sglist = &ctl_sg_entry;
2382 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2383 ctl_sglist->len = io->scsiio.kern_data_len;
2388 * This is the total amount of data to be transferred within the current
2389 * SCSI command. We need to record it so that we can properly report
2390 * underflow/underflow.
2392 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2395 * This is the offset within the current SCSI command; for the first
2396 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2397 * it will be the sum of lengths of previous ones.
2399 buffer_offset = io->scsiio.kern_rel_offset;
2402 * This is the transfer length expected by the initiator. In theory,
2403 * it could be different from the correct amount of data from the SCSI
2404 * point of view, even if that doesn't make any sense.
2406 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2408 if (expected_len != io->scsiio.kern_total_len) {
2409 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2410 "actual length %zd", expected_len,
2411 (size_t)io->scsiio.kern_total_len);
2415 if (buffer_offset >= expected_len) {
2417 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2418 "already sent the expected len", buffer_offset);
2420 io->scsiio.be_move_done(io);
2430 if (response == NULL) {
2431 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2432 if (response == NULL) {
2433 CFISCSI_SESSION_WARN(cs, "failed to "
2434 "allocate memory; dropping connection");
2435 ctl_set_busy(&io->scsiio);
2436 io->scsiio.be_move_done(io);
2437 cfiscsi_session_terminate(cs);
2440 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2441 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2442 bhsdi->bhsdi_initiator_task_tag =
2443 bhssc->bhssc_initiator_task_tag;
2444 bhsdi->bhsdi_target_transfer_tag = 0xffffffff;
2445 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2446 PDU_EXPDATASN(request)++;
2447 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2450 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2452 sg_addr = ctl_sglist[i].addr;
2453 sg_len = ctl_sglist[i].len;
2454 KASSERT(sg_len > 0, ("sg_len <= 0"));
2460 * Truncate to maximum data segment length.
2462 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2463 ("ip_data_len %zd >= max_data_segment_length %zd",
2464 response->ip_data_len, cs->cs_max_data_segment_length));
2465 if (response->ip_data_len + len >
2466 cs->cs_max_data_segment_length) {
2467 len = cs->cs_max_data_segment_length -
2468 response->ip_data_len;
2469 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2474 * Truncate to expected data transfer length.
2476 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2477 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2478 buffer_offset, response->ip_data_len, expected_len));
2479 if (buffer_offset + response->ip_data_len + len > expected_len) {
2480 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2481 "to expected data transfer length %zd",
2482 buffer_offset + response->ip_data_len + len, expected_len);
2483 len = expected_len - (buffer_offset + response->ip_data_len);
2484 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2488 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2490 CFISCSI_SESSION_WARN(cs, "failed to "
2491 "allocate memory; dropping connection");
2492 icl_pdu_free(response);
2493 ctl_set_busy(&io->scsiio);
2494 io->scsiio.be_move_done(io);
2495 cfiscsi_session_terminate(cs);
2501 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2502 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2503 buffer_offset, response->ip_data_len, expected_len));
2504 if (buffer_offset + response->ip_data_len == expected_len) {
2506 * Already have the amount of data the initiator wanted.
2513 * End of scatter-gather segment;
2514 * proceed to the next one...
2516 if (i == ctl_sg_count - 1) {
2518 * ... unless this was the last one.
2525 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2527 * Can't stuff more data into the current PDU;
2528 * queue it. Note that's not enough to check
2529 * for kern_data_resid == 0 instead; there
2530 * may be several Data-In PDUs for the final
2531 * call to cfiscsi_datamove(), and we want
2532 * to set the F flag only on the last of them.
2534 buffer_offset += response->ip_data_len;
2535 if (buffer_offset == io->scsiio.kern_total_len ||
2536 buffer_offset == expected_len) {
2537 buffer_offset -= response->ip_data_len;
2540 cfiscsi_pdu_queue(response);
2545 if (response != NULL) {
2546 buffer_offset += response->ip_data_len;
2547 if (buffer_offset == io->scsiio.kern_total_len ||
2548 buffer_offset == expected_len) {
2549 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2550 if (io->io_hdr.status == CTL_SUCCESS) {
2551 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2552 if (PDU_TOTAL_TRANSFER_LEN(request) <
2553 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2554 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2555 bhsdi->bhsdi_residual_count =
2556 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2557 PDU_TOTAL_TRANSFER_LEN(request));
2558 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2559 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2560 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2561 bhsdi->bhsdi_residual_count =
2562 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2563 ntohl(bhssc->bhssc_expected_data_transfer_length));
2565 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2566 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2569 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2570 cfiscsi_pdu_queue(response);
2573 io->scsiio.be_move_done(io);
2577 cfiscsi_datamove_out(union ctl_io *io)
2579 struct cfiscsi_session *cs;
2580 struct icl_pdu *request, *response;
2581 const struct iscsi_bhs_scsi_command *bhssc;
2582 struct iscsi_bhs_r2t *bhsr2t;
2583 struct cfiscsi_data_wait *cdw;
2584 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2585 uint32_t expected_len, r2t_off, r2t_len;
2586 uint32_t target_transfer_tag;
2589 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2590 cs = PDU_SESSION(request);
2592 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2593 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2594 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2595 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2598 * We need to record it so that we can properly report
2599 * underflow/underflow.
2601 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2604 * Report write underflow as error since CTL and backends don't
2605 * really support it, and SCSI does not tell how to do it right.
2607 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2608 if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len >
2610 io->scsiio.io_hdr.port_status = 43;
2611 io->scsiio.be_move_done(io);
2615 target_transfer_tag =
2616 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2617 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2618 &target_transfer_tag);
2620 CFISCSI_SESSION_WARN(cs, "failed to "
2621 "allocate memory; dropping connection");
2622 ctl_set_busy(&io->scsiio);
2623 io->scsiio.be_move_done(io);
2624 cfiscsi_session_terminate(cs);
2628 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2629 "task tag 0x%x, target transfer tag 0x%x",
2630 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2633 cdw->cdw_ctl_io = io;
2634 cdw->cdw_target_transfer_tag = target_transfer_tag;
2635 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2636 cdw->cdw_r2t_end = io->scsiio.kern_data_len;
2637 cdw->cdw_datasn = 0;
2639 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2640 if (io->scsiio.kern_sg_entries > 0) {
2641 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2643 ctl_sglist = &ctl_sg_entry;
2644 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2645 ctl_sglist->len = io->scsiio.kern_data_len;
2647 cdw->cdw_sg_index = 0;
2648 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2649 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2650 r2t_off = io->scsiio.ext_data_filled;
2651 while (r2t_off > 0) {
2652 if (r2t_off >= cdw->cdw_sg_len) {
2653 r2t_off -= cdw->cdw_sg_len;
2654 cdw->cdw_sg_index++;
2655 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2656 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2659 cdw->cdw_sg_addr += r2t_off;
2660 cdw->cdw_sg_len -= r2t_off;
2664 if (cs->cs_immediate_data &&
2665 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2666 icl_pdu_data_segment_length(request)) {
2667 done = cfiscsi_handle_data_segment(request, cdw);
2669 cfiscsi_data_wait_free(cs, cdw);
2670 io->scsiio.be_move_done(io);
2675 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2676 r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled,
2677 cs->cs_max_burst_length);
2678 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2680 CFISCSI_SESSION_LOCK(cs);
2681 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2682 CFISCSI_SESSION_UNLOCK(cs);
2685 * XXX: We should limit the number of outstanding R2T PDUs
2686 * per task to MaxOutstandingR2T.
2688 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2689 if (response == NULL) {
2690 CFISCSI_SESSION_WARN(cs, "failed to "
2691 "allocate memory; dropping connection");
2692 ctl_set_busy(&io->scsiio);
2693 io->scsiio.be_move_done(io);
2694 cfiscsi_session_terminate(cs);
2697 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
2698 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2699 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2700 bhsr2t->bhsr2t_flags = 0x80;
2701 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2702 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2703 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2705 * XXX: Here we assume that cfiscsi_datamove() won't ever
2706 * be running concurrently on several CPUs for a given
2709 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2710 PDU_R2TSN(request)++;
2712 * This is the offset within the current SCSI command;
2713 * i.e. for the first call of datamove(), it will be 0,
2714 * and for subsequent ones it will be the sum of lengths
2717 * The ext_data_filled is to account for unsolicited
2718 * (immediate) data that might have already arrived.
2720 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2722 * This is the total length (sum of S/G lengths) this call
2723 * to cfiscsi_datamove() is supposed to handle, limited by
2726 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2727 cfiscsi_pdu_queue(response);
2731 cfiscsi_datamove(union ctl_io *io)
2734 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2735 cfiscsi_datamove_in(io);
2737 /* We hadn't received anything during this datamove yet. */
2738 io->scsiio.ext_data_filled = 0;
2739 cfiscsi_datamove_out(io);
2744 cfiscsi_scsi_command_done(union ctl_io *io)
2746 struct icl_pdu *request, *response;
2747 struct iscsi_bhs_scsi_command *bhssc;
2748 struct iscsi_bhs_scsi_response *bhssr;
2750 struct cfiscsi_data_wait *cdw;
2752 struct cfiscsi_session *cs;
2753 uint16_t sense_length;
2755 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2756 cs = PDU_SESSION(request);
2757 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2758 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2759 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2760 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2762 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2763 // bhssc->bhssc_initiator_task_tag);
2766 CFISCSI_SESSION_LOCK(cs);
2767 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2768 KASSERT(bhssc->bhssc_initiator_task_tag !=
2769 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2770 CFISCSI_SESSION_UNLOCK(cs);
2774 * Do not return status for aborted commands.
2775 * There are exceptions, but none supported by CTL yet.
2777 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2778 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2779 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2781 icl_pdu_free(request);
2785 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2786 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2787 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2788 bhssr->bhssr_flags = 0x80;
2790 * XXX: We don't deal with bidirectional under/overflows;
2791 * does anything actually support those?
2793 if (PDU_TOTAL_TRANSFER_LEN(request) <
2794 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2795 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2796 bhssr->bhssr_residual_count =
2797 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2798 PDU_TOTAL_TRANSFER_LEN(request));
2799 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2800 // ntohl(bhssr->bhssr_residual_count));
2801 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2802 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2803 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2804 bhssr->bhssr_residual_count =
2805 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2806 ntohl(bhssc->bhssc_expected_data_transfer_length));
2807 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2808 // ntohl(bhssr->bhssr_residual_count));
2810 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2811 bhssr->bhssr_status = io->scsiio.scsi_status;
2812 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2813 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2815 if (io->scsiio.sense_len > 0) {
2817 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2818 io->scsiio.sense_len);
2820 sense_length = htons(io->scsiio.sense_len);
2821 icl_pdu_append_data(response,
2822 &sense_length, sizeof(sense_length), M_WAITOK);
2823 icl_pdu_append_data(response,
2824 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2828 icl_pdu_free(request);
2829 cfiscsi_pdu_queue(response);
2833 cfiscsi_task_management_done(union ctl_io *io)
2835 struct icl_pdu *request, *response;
2836 struct iscsi_bhs_task_management_request *bhstmr;
2837 struct iscsi_bhs_task_management_response *bhstmr2;
2838 struct cfiscsi_data_wait *cdw, *tmpcdw;
2839 struct cfiscsi_session *cs, *tcs;
2840 struct cfiscsi_softc *softc;
2843 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2844 cs = PDU_SESSION(request);
2845 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2846 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2847 ISCSI_BHS_OPCODE_TASK_REQUEST,
2848 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2851 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2852 bhstmr->bhstmr_initiator_task_tag,
2853 bhstmr->bhstmr_referenced_task_tag);
2856 if ((bhstmr->bhstmr_function & ~0x80) ==
2857 BHSTMR_FUNCTION_ABORT_TASK) {
2859 * Make sure we no longer wait for Data-Out for this command.
2861 CFISCSI_SESSION_LOCK(cs);
2862 TAILQ_FOREACH_SAFE(cdw,
2863 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2864 if (bhstmr->bhstmr_referenced_task_tag !=
2865 cdw->cdw_initiator_task_tag)
2869 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2870 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2872 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2874 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
2875 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 43;
2876 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2877 cfiscsi_data_wait_free(cs, cdw);
2879 CFISCSI_SESSION_UNLOCK(cs);
2881 if ((bhstmr->bhstmr_function & ~0x80) ==
2882 BHSTMR_FUNCTION_TARGET_COLD_RESET &&
2883 io->io_hdr.status == CTL_SUCCESS)
2886 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2887 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2889 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2890 bhstmr2->bhstmr_flags = 0x80;
2891 switch (io->taskio.task_status) {
2892 case CTL_TASK_FUNCTION_COMPLETE:
2893 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2895 case CTL_TASK_FUNCTION_SUCCEEDED:
2896 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED;
2898 case CTL_TASK_LUN_DOES_NOT_EXIST:
2899 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST;
2901 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
2903 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2906 memcpy(bhstmr2->bhstmr_additional_reponse_information,
2907 io->taskio.task_resp, sizeof(io->taskio.task_resp));
2908 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2911 icl_pdu_free(request);
2912 cfiscsi_pdu_queue(response);
2915 softc = cs->cs_target->ct_softc;
2916 mtx_lock(&softc->lock);
2917 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) {
2918 if (tcs->cs_target == cs->cs_target)
2919 cfiscsi_session_terminate(tcs);
2921 mtx_unlock(&softc->lock);
2926 cfiscsi_done(union ctl_io *io)
2928 struct icl_pdu *request;
2929 struct cfiscsi_session *cs;
2931 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2932 ("invalid CTL status %#x", io->io_hdr.status));
2934 if (io->io_hdr.io_type == CTL_IO_TASK &&
2935 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2937 * Implicit task termination has just completed; nothing to do.
2939 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2940 cs->cs_tasks_aborted = true;
2941 refcount_release(&cs->cs_outstanding_ctl_pdus);
2942 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2947 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2948 cs = PDU_SESSION(request);
2949 refcount_release(&cs->cs_outstanding_ctl_pdus);
2951 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2952 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2953 cfiscsi_scsi_command_done(io);
2955 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2956 cfiscsi_task_management_done(io);
2959 panic("cfiscsi_done called with wrong opcode 0x%x",
2960 request->ip_bhs->bhs_opcode);