2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * CTL frontend for the iSCSI protocol.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
43 #include <sys/condvar.h>
44 #include <sys/endian.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
58 #include <sys/unistd.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_da.h>
63 #include <cam/ctl/ctl_io.h>
64 #include <cam/ctl/ctl.h>
65 #include <cam/ctl/ctl_backend.h>
66 #include <cam/ctl/ctl_error.h>
67 #include <cam/ctl/ctl_frontend.h>
68 #include <cam/ctl/ctl_debug.h>
69 #include <cam/ctl/ctl_ha.h>
70 #include <cam/ctl/ctl_ioctl.h>
71 #include <cam/ctl/ctl_private.h>
73 #include <dev/iscsi/icl.h>
74 #include <dev/iscsi/icl_wrappers.h>
75 #include <dev/iscsi/iscsi_proto.h>
76 #include <cam/ctl/ctl_frontend_iscsi.h>
78 #ifdef ICL_KERNEL_PROXY
79 #include <sys/socketvar.h>
82 #ifdef ICL_KERNEL_PROXY
83 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
86 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
87 static uma_zone_t cfiscsi_data_wait_zone;
89 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
90 "CAM Target Layer iSCSI Frontend");
92 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
93 &debug, 1, "Enable debug messages");
94 static int ping_timeout = 5;
95 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
96 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
97 static int login_timeout = 60;
98 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
99 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
100 static int maxtags = 256;
101 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN,
102 &maxtags, 0, "Max number of requests queued by initiator");
104 #define CFISCSI_DEBUG(X, ...) \
107 printf("%s: " X "\n", \
108 __func__, ## __VA_ARGS__); \
112 #define CFISCSI_WARN(X, ...) \
115 printf("WARNING: %s: " X "\n", \
116 __func__, ## __VA_ARGS__); \
120 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
123 printf("%s: %s (%s): " X "\n", \
124 __func__, S->cs_initiator_addr, \
125 S->cs_initiator_name, ## __VA_ARGS__); \
129 #define CFISCSI_SESSION_WARN(S, X, ...) \
132 printf("WARNING: %s (%s): " X "\n", \
133 S->cs_initiator_addr, \
134 S->cs_initiator_name, ## __VA_ARGS__); \
138 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
139 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
140 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
142 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
143 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
144 #define PDU_EXPDATASN(X) (X)->ip_prv0
145 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
146 #define PDU_R2TSN(X) (X)->ip_prv2
148 static int cfiscsi_init(void);
149 static int cfiscsi_shutdown(void);
150 static void cfiscsi_online(void *arg);
151 static void cfiscsi_offline(void *arg);
152 static int cfiscsi_info(void *arg, struct sbuf *sb);
153 static int cfiscsi_ioctl(struct cdev *dev,
154 u_long cmd, caddr_t addr, int flag, struct thread *td);
155 static void cfiscsi_datamove(union ctl_io *io);
156 static void cfiscsi_datamove_in(union ctl_io *io);
157 static void cfiscsi_datamove_out(union ctl_io *io);
158 static void cfiscsi_done(union ctl_io *io);
159 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
160 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
161 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
162 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
163 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
164 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
165 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
166 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
167 struct cfiscsi_session *cs, union ctl_io *io,
168 uint32_t initiator_task_tag,
169 uint32_t *target_transfer_tagp);
170 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
171 struct cfiscsi_data_wait *cdw);
172 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
173 *softc, const char *name, uint16_t tag);
174 static struct cfiscsi_target *cfiscsi_target_find_or_create(
175 struct cfiscsi_softc *softc, const char *name, const char *alias,
177 static void cfiscsi_target_release(struct cfiscsi_target *ct);
178 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
180 static struct cfiscsi_softc cfiscsi_softc;
182 static struct ctl_frontend cfiscsi_frontend =
185 .init = cfiscsi_init,
186 .ioctl = cfiscsi_ioctl,
187 .shutdown = cfiscsi_shutdown,
189 CTL_FRONTEND_DECLARE(cfiscsi, cfiscsi_frontend);
190 MODULE_DEPEND(cfiscsi, icl, 1, 1, 1);
192 static struct icl_pdu *
193 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
196 return (icl_pdu_new(request->ip_conn, flags));
200 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
202 const struct iscsi_bhs_scsi_command *bhssc;
203 struct cfiscsi_session *cs;
204 uint32_t cmdsn, expstatsn;
206 cs = PDU_SESSION(request);
209 * Every incoming PDU - not just NOP-Out - resets the ping timer.
210 * The purpose of the timeout is to reset the connection when it stalls;
211 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
219 * Data-Out PDUs don't contain CmdSN.
221 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
222 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
226 * We're only using fields common for all the request
227 * (initiator -> target) PDUs.
229 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
230 cmdsn = ntohl(bhssc->bhssc_cmdsn);
231 expstatsn = ntohl(bhssc->bhssc_expstatsn);
233 CFISCSI_SESSION_LOCK(cs);
235 if (expstatsn != cs->cs_statsn) {
236 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
237 "while current StatSN is %d", expstatsn,
242 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
244 * The target MUST silently ignore any non-immediate command
245 * outside of this range.
247 if (ISCSI_SNLT(cmdsn, cs->cs_cmdsn) ||
248 ISCSI_SNGT(cmdsn, cs->cs_cmdsn - 1 + maxtags)) {
249 CFISCSI_SESSION_UNLOCK(cs);
250 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
251 "while expected %u", cmdsn, cs->cs_cmdsn);
256 * We don't support multiple connections now, so any
257 * discontinuity in CmdSN means lost PDUs. Since we don't
258 * support PDU retransmission -- terminate the connection.
260 if (cmdsn != cs->cs_cmdsn) {
261 CFISCSI_SESSION_UNLOCK(cs);
262 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
263 "while expected %u; dropping connection",
264 cmdsn, cs->cs_cmdsn);
265 cfiscsi_session_terminate(cs);
271 CFISCSI_SESSION_UNLOCK(cs);
277 cfiscsi_pdu_handle(struct icl_pdu *request)
279 struct cfiscsi_session *cs;
282 cs = PDU_SESSION(request);
284 ignore = cfiscsi_pdu_update_cmdsn(request);
286 icl_pdu_free(request);
291 * Handle the PDU; this includes e.g. receiving the remaining
292 * part of PDU and submitting the SCSI command to CTL
293 * or queueing a reply. The handling routine is responsible
294 * for freeing the PDU when it's no longer needed.
296 switch (request->ip_bhs->bhs_opcode &
297 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
298 case ISCSI_BHS_OPCODE_NOP_OUT:
299 cfiscsi_pdu_handle_nop_out(request);
301 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
302 cfiscsi_pdu_handle_scsi_command(request);
304 case ISCSI_BHS_OPCODE_TASK_REQUEST:
305 cfiscsi_pdu_handle_task_request(request);
307 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
308 cfiscsi_pdu_handle_data_out(request);
310 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
311 cfiscsi_pdu_handle_logout_request(request);
314 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
315 "opcode 0x%x; dropping connection",
316 request->ip_bhs->bhs_opcode);
317 icl_pdu_free(request);
318 cfiscsi_session_terminate(cs);
324 cfiscsi_receive_callback(struct icl_pdu *request)
326 #ifdef ICL_KERNEL_PROXY
327 struct cfiscsi_session *cs;
329 cs = PDU_SESSION(request);
330 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
331 if (cs->cs_login_pdu == NULL)
332 cs->cs_login_pdu = request;
334 icl_pdu_free(request);
335 cv_signal(&cs->cs_login_cv);
340 cfiscsi_pdu_handle(request);
344 cfiscsi_error_callback(struct icl_conn *ic)
346 struct cfiscsi_session *cs;
348 cs = CONN_SESSION(ic);
350 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
351 cfiscsi_session_terminate(cs);
355 cfiscsi_pdu_prepare(struct icl_pdu *response)
357 struct cfiscsi_session *cs;
358 struct iscsi_bhs_scsi_response *bhssr;
359 bool advance_statsn = true;
361 cs = PDU_SESSION(response);
363 CFISCSI_SESSION_LOCK_ASSERT(cs);
366 * We're only using fields common for all the response
367 * (target -> initiator) PDUs.
369 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
372 * 10.8.3: "The StatSN for this connection is not advanced
373 * after this PDU is sent."
375 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
376 advance_statsn = false;
379 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
380 * StatSN for the connection is not advanced after this PDU is sent."
382 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
383 bhssr->bhssr_initiator_task_tag == 0xffffffff)
384 advance_statsn = false;
387 * See the comment below - StatSN is not meaningful and must
390 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
391 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
392 advance_statsn = false;
395 * 10.7.3: "The fields StatSN, Status, and Residual Count
396 * only have meaningful content if the S bit is set to 1."
398 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
399 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
400 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
401 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
402 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn - 1 +
403 imax(0, maxtags - cs->cs_outstanding_ctl_pdus));
412 cfiscsi_pdu_queue(struct icl_pdu *response)
414 struct cfiscsi_session *cs;
416 cs = PDU_SESSION(response);
418 CFISCSI_SESSION_LOCK(cs);
419 cfiscsi_pdu_prepare(response);
420 icl_pdu_queue(response);
421 CFISCSI_SESSION_UNLOCK(cs);
425 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
427 struct cfiscsi_session *cs;
428 struct iscsi_bhs_nop_out *bhsno;
429 struct iscsi_bhs_nop_in *bhsni;
430 struct icl_pdu *response;
435 cs = PDU_SESSION(request);
436 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
438 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
440 * Nothing to do, iscsi_pdu_update_statsn() already
441 * zeroed the timeout.
443 icl_pdu_free(request);
447 datasize = icl_pdu_data_segment_length(request);
449 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
451 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
452 "dropping connection");
453 icl_pdu_free(request);
454 cfiscsi_session_terminate(cs);
457 icl_pdu_get_data(request, 0, data, datasize);
460 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
461 if (response == NULL) {
462 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
463 "droppping connection");
464 free(data, M_CFISCSI);
465 icl_pdu_free(request);
466 cfiscsi_session_terminate(cs);
469 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
470 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
471 bhsni->bhsni_flags = 0x80;
472 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
473 bhsni->bhsni_target_transfer_tag = 0xffffffff;
475 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
477 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
478 "dropping connection");
479 free(data, M_CFISCSI);
480 icl_pdu_free(request);
481 icl_pdu_free(response);
482 cfiscsi_session_terminate(cs);
485 free(data, M_CFISCSI);
488 icl_pdu_free(request);
489 cfiscsi_pdu_queue(response);
493 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
495 struct iscsi_bhs_scsi_command *bhssc;
496 struct cfiscsi_session *cs;
500 cs = PDU_SESSION(request);
501 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
502 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
503 // bhssc->bhssc_initiator_task_tag);
505 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
506 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
507 "ImmediateData=No; dropping connection");
508 icl_pdu_free(request);
509 cfiscsi_session_terminate(cs);
512 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
514 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
515 io->io_hdr.io_type = CTL_IO_SCSI;
516 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
517 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
518 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhssc->bhssc_lun));
519 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
520 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
521 case BHSSC_FLAGS_ATTR_UNTAGGED:
522 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
524 case BHSSC_FLAGS_ATTR_SIMPLE:
525 io->scsiio.tag_type = CTL_TAG_SIMPLE;
527 case BHSSC_FLAGS_ATTR_ORDERED:
528 io->scsiio.tag_type = CTL_TAG_ORDERED;
530 case BHSSC_FLAGS_ATTR_HOQ:
531 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
533 case BHSSC_FLAGS_ATTR_ACA:
534 io->scsiio.tag_type = CTL_TAG_ACA;
537 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
538 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
539 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
542 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
543 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
544 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
545 error = ctl_queue(io);
546 if (error != CTL_RETVAL_COMPLETE) {
547 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
548 "dropping connection", error);
550 refcount_release(&cs->cs_outstanding_ctl_pdus);
551 icl_pdu_free(request);
552 cfiscsi_session_terminate(cs);
557 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
559 struct iscsi_bhs_task_management_request *bhstmr;
560 struct iscsi_bhs_task_management_response *bhstmr2;
561 struct icl_pdu *response;
562 struct cfiscsi_session *cs;
566 cs = PDU_SESSION(request);
567 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
568 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
570 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
571 io->io_hdr.io_type = CTL_IO_TASK;
572 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
573 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
574 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhstmr->bhstmr_lun));
575 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
577 switch (bhstmr->bhstmr_function & ~0x80) {
578 case BHSTMR_FUNCTION_ABORT_TASK:
580 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
582 io->taskio.task_action = CTL_TASK_ABORT_TASK;
583 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
585 case BHSTMR_FUNCTION_ABORT_TASK_SET:
587 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
589 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
591 case BHSTMR_FUNCTION_CLEAR_TASK_SET:
593 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_CLEAR_TASK_SET");
595 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
597 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
599 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
601 io->taskio.task_action = CTL_TASK_LUN_RESET;
603 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
605 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
607 io->taskio.task_action = CTL_TASK_TARGET_RESET;
609 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
611 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
613 io->taskio.task_action = CTL_TASK_TARGET_RESET;
615 case BHSTMR_FUNCTION_QUERY_TASK:
617 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK");
619 io->taskio.task_action = CTL_TASK_QUERY_TASK;
620 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
622 case BHSTMR_FUNCTION_QUERY_TASK_SET:
624 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK_SET");
626 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
628 case BHSTMR_FUNCTION_I_T_NEXUS_RESET:
630 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_I_T_NEXUS_RESET");
632 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
634 case BHSTMR_FUNCTION_QUERY_ASYNC_EVENT:
636 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_ASYNC_EVENT");
638 io->taskio.task_action = CTL_TASK_QUERY_ASYNC_EVENT;
641 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
642 bhstmr->bhstmr_function & ~0x80);
645 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
646 if (response == NULL) {
647 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
648 "dropping connection");
649 icl_pdu_free(request);
650 cfiscsi_session_terminate(cs);
653 bhstmr2 = (struct iscsi_bhs_task_management_response *)
655 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
656 bhstmr2->bhstmr_flags = 0x80;
657 bhstmr2->bhstmr_response =
658 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
659 bhstmr2->bhstmr_initiator_task_tag =
660 bhstmr->bhstmr_initiator_task_tag;
661 icl_pdu_free(request);
662 cfiscsi_pdu_queue(response);
666 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
667 error = ctl_queue(io);
668 if (error != CTL_RETVAL_COMPLETE) {
669 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
670 "dropping connection", error);
672 refcount_release(&cs->cs_outstanding_ctl_pdus);
673 icl_pdu_free(request);
674 cfiscsi_session_terminate(cs);
679 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
681 struct iscsi_bhs_data_out *bhsdo;
682 struct cfiscsi_session *cs;
683 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
684 size_t copy_len, len, off, buffer_offset;
688 cs = PDU_SESSION(request);
690 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
691 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
692 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
693 ISCSI_BHS_OPCODE_SCSI_COMMAND,
694 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
697 * We're only using fields common for Data-Out and SCSI Command PDUs.
699 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
701 io = cdw->cdw_ctl_io;
702 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
703 ("CTL_FLAG_DATA_IN"));
706 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
707 request->ip_data_len, io->scsiio.kern_total_len);
710 if (io->scsiio.kern_sg_entries > 0) {
711 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
712 ctl_sg_count = io->scsiio.kern_sg_entries;
714 ctl_sglist = &ctl_sg_entry;
715 ctl_sglist->addr = io->scsiio.kern_data_ptr;
716 ctl_sglist->len = io->scsiio.kern_data_len;
720 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
721 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
722 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
725 len = icl_pdu_data_segment_length(request);
728 * Make sure the offset, as sent by the initiator, matches the offset
729 * we're supposed to be at in the scatter-gather list.
732 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
733 buffer_offset + len <=
734 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
735 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
736 "expected %zd; dropping connection", buffer_offset,
737 (size_t)io->scsiio.kern_rel_offset +
738 (size_t)io->scsiio.ext_data_filled);
739 ctl_set_data_phase_error(&io->scsiio);
740 cfiscsi_session_terminate(cs);
745 * This is the offset within the PDU data segment, as opposed
746 * to buffer_offset, which is the offset within the task (SCSI
749 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
753 * Iterate over the scatter/gather segments, filling them with data
754 * from the PDU data segment. Note that this can get called multiple
755 * times for one SCSI command; the cdw structure holds state for the
756 * scatter/gather list.
759 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
760 ("cdw->cdw_sg_index >= ctl_sg_count"));
761 if (cdw->cdw_sg_len == 0) {
762 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
763 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
765 KASSERT(off <= len, ("len > off"));
766 copy_len = len - off;
767 if (copy_len > cdw->cdw_sg_len)
768 copy_len = cdw->cdw_sg_len;
770 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
771 cdw->cdw_sg_addr += copy_len;
772 cdw->cdw_sg_len -= copy_len;
774 io->scsiio.ext_data_filled += copy_len;
775 io->scsiio.kern_data_resid -= copy_len;
777 if (cdw->cdw_sg_len == 0) {
779 * End of current segment.
781 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
783 * Last segment in scatter/gather list.
792 * End of PDU payload.
800 * In case of unsolicited data, it's possible that the buffer
801 * provided by CTL is smaller than negotiated FirstBurstLength.
802 * Just ignore the superfluous data; will ask for them with R2T
803 * on next call to cfiscsi_datamove().
805 * This obviously can only happen with SCSI Command PDU.
807 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
808 ISCSI_BHS_OPCODE_SCSI_COMMAND)
811 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
812 "expected %zd; dropping connection",
813 icl_pdu_data_segment_length(request), off);
814 ctl_set_data_phase_error(&io->scsiio);
815 cfiscsi_session_terminate(cs);
819 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
820 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
821 CFISCSI_SESSION_WARN(cs, "got the final packet without "
822 "the F flag; flags = 0x%x; dropping connection",
824 ctl_set_data_phase_error(&io->scsiio);
825 cfiscsi_session_terminate(cs);
829 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
830 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
831 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
832 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
833 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
834 "transmitted size was %zd bytes instead of %d; "
835 "dropping connection",
836 (size_t)io->scsiio.ext_data_filled,
838 ctl_set_data_phase_error(&io->scsiio);
839 cfiscsi_session_terminate(cs);
843 * For SCSI Command PDU, this just means we need to
844 * solicit more data by sending R2T.
850 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
852 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
853 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
863 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
865 struct iscsi_bhs_data_out *bhsdo;
866 struct cfiscsi_session *cs;
867 struct cfiscsi_data_wait *cdw = NULL;
871 cs = PDU_SESSION(request);
872 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
874 CFISCSI_SESSION_LOCK(cs);
875 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
877 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
878 "ttt 0x%x, itt 0x%x",
879 bhsdo->bhsdo_target_transfer_tag,
880 bhsdo->bhsdo_initiator_task_tag,
881 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
883 if (bhsdo->bhsdo_target_transfer_tag ==
884 cdw->cdw_target_transfer_tag)
887 CFISCSI_SESSION_UNLOCK(cs);
889 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
890 "0x%x, not found; dropping connection",
891 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
892 icl_pdu_free(request);
893 cfiscsi_session_terminate(cs);
897 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
898 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
899 "DataSN %u, while expected %u; dropping connection",
900 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
901 icl_pdu_free(request);
902 cfiscsi_session_terminate(cs);
907 io = cdw->cdw_ctl_io;
908 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
909 ("CTL_FLAG_DATA_IN"));
911 done = cfiscsi_handle_data_segment(request, cdw);
913 CFISCSI_SESSION_LOCK(cs);
914 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
915 CFISCSI_SESSION_UNLOCK(cs);
916 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
917 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
918 cfiscsi_data_wait_free(cs, cdw);
919 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
921 io->scsiio.be_move_done(io);
923 cfiscsi_datamove_out(io);
926 icl_pdu_free(request);
930 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
932 struct iscsi_bhs_logout_request *bhslr;
933 struct iscsi_bhs_logout_response *bhslr2;
934 struct icl_pdu *response;
935 struct cfiscsi_session *cs;
937 cs = PDU_SESSION(request);
938 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
939 switch (bhslr->bhslr_reason & 0x7f) {
940 case BHSLR_REASON_CLOSE_SESSION:
941 case BHSLR_REASON_CLOSE_CONNECTION:
942 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
943 if (response == NULL) {
944 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
945 icl_pdu_free(request);
946 cfiscsi_session_terminate(cs);
949 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
950 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
951 bhslr2->bhslr_flags = 0x80;
952 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
953 bhslr2->bhslr_initiator_task_tag =
954 bhslr->bhslr_initiator_task_tag;
955 icl_pdu_free(request);
956 cfiscsi_pdu_queue(response);
957 cfiscsi_session_terminate(cs);
959 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
960 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
961 if (response == NULL) {
962 CFISCSI_SESSION_WARN(cs,
963 "failed to allocate memory; dropping connection");
964 icl_pdu_free(request);
965 cfiscsi_session_terminate(cs);
968 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
969 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
970 bhslr2->bhslr_flags = 0x80;
971 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
972 bhslr2->bhslr_initiator_task_tag =
973 bhslr->bhslr_initiator_task_tag;
974 icl_pdu_free(request);
975 cfiscsi_pdu_queue(response);
978 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
979 bhslr->bhslr_reason);
980 icl_pdu_free(request);
981 cfiscsi_session_terminate(cs);
987 cfiscsi_callout(void *context)
990 struct iscsi_bhs_nop_in *bhsni;
991 struct cfiscsi_session *cs;
995 if (cs->cs_terminating)
998 callout_schedule(&cs->cs_callout, 1 * hz);
1000 atomic_add_int(&cs->cs_timeout, 1);
1002 #ifdef ICL_KERNEL_PROXY
1003 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1004 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1005 CFISCSI_SESSION_WARN(cs, "login timed out after "
1006 "%d seconds; dropping connection", cs->cs_timeout);
1007 cfiscsi_session_terminate(cs);
1013 if (ping_timeout <= 0) {
1015 * Pings are disabled. Don't send NOP-In in this case;
1016 * user might have disabled pings to work around problems
1017 * with certain initiators that can't properly handle
1018 * NOP-In, such as iPXE. Reset the timeout, to avoid
1019 * triggering reconnection, should the user decide to
1026 if (cs->cs_timeout >= ping_timeout) {
1027 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1028 "dropping connection", ping_timeout);
1029 cfiscsi_session_terminate(cs);
1034 * If the ping was reset less than one second ago - which means
1035 * that we've received some PDU during the last second - assume
1036 * the traffic flows correctly and don't bother sending a NOP-Out.
1038 * (It's 2 - one for one second, and one for incrementing is_timeout
1039 * earlier in this routine.)
1041 if (cs->cs_timeout < 2)
1044 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1046 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1049 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1050 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1051 bhsni->bhsni_flags = 0x80;
1052 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1054 cfiscsi_pdu_queue(cp);
1057 static struct cfiscsi_data_wait *
1058 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1059 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1061 struct cfiscsi_data_wait *cdw;
1064 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1066 CFISCSI_SESSION_WARN(cs,
1067 "failed to allocate %zd bytes", sizeof(*cdw));
1071 error = icl_conn_transfer_setup(cs->cs_conn, io, target_transfer_tagp,
1074 CFISCSI_SESSION_WARN(cs,
1075 "icl_conn_transfer_setup() failed with error %d", error);
1076 uma_zfree(cfiscsi_data_wait_zone, cdw);
1080 cdw->cdw_ctl_io = io;
1081 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1082 cdw->cdw_initiator_task_tag = initiator_task_tag;
1088 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1089 struct cfiscsi_data_wait *cdw)
1092 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1093 uma_zfree(cfiscsi_data_wait_zone, cdw);
1097 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1099 struct cfiscsi_data_wait *cdw;
1101 int error, last, wait;
1103 if (cs->cs_target == NULL)
1104 return; /* No target yet, so nothing to do. */
1105 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1107 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1108 io->io_hdr.io_type = CTL_IO_TASK;
1109 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1110 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1111 io->io_hdr.nexus.targ_lun = 0;
1112 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1113 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1114 wait = cs->cs_outstanding_ctl_pdus;
1115 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1116 error = ctl_queue(io);
1117 if (error != CTL_RETVAL_COMPLETE) {
1118 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1119 refcount_release(&cs->cs_outstanding_ctl_pdus);
1123 CFISCSI_SESSION_LOCK(cs);
1124 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1125 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1126 CFISCSI_SESSION_UNLOCK(cs);
1128 * Set nonzero port status; this prevents backends from
1129 * assuming that the data transfer actually succeeded
1130 * and writing uninitialized data to disk.
1132 cdw->cdw_ctl_io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1133 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1134 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1135 cfiscsi_data_wait_free(cs, cdw);
1136 CFISCSI_SESSION_LOCK(cs);
1138 CFISCSI_SESSION_UNLOCK(cs);
1141 * Wait for CTL to terminate all the tasks.
1144 CFISCSI_SESSION_WARN(cs,
1145 "waiting for CTL to terminate %d tasks", wait);
1147 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1148 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1151 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1152 0, "cfiscsi_terminate", hz / 100);
1155 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1159 cfiscsi_maintenance_thread(void *arg)
1161 struct cfiscsi_session *cs;
1166 CFISCSI_SESSION_LOCK(cs);
1167 if (cs->cs_terminating == false)
1168 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1169 CFISCSI_SESSION_UNLOCK(cs);
1171 if (cs->cs_terminating) {
1174 * We used to wait up to 30 seconds to deliver queued
1175 * PDUs to the initiator. We also tried hard to deliver
1176 * SCSI Responses for the aborted PDUs. We don't do
1177 * that anymore. We might need to revisit that.
1179 callout_drain(&cs->cs_callout);
1180 icl_conn_close(cs->cs_conn);
1183 * At this point ICL receive thread is no longer
1184 * running; no new tasks can be queued.
1186 cfiscsi_session_terminate_tasks(cs);
1187 cfiscsi_session_delete(cs);
1191 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1196 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1199 if (cs->cs_terminating)
1201 cs->cs_terminating = true;
1202 cv_signal(&cs->cs_maintenance_cv);
1203 #ifdef ICL_KERNEL_PROXY
1204 cv_signal(&cs->cs_login_cv);
1209 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1211 struct cfiscsi_target *ct;
1215 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1218 name = strdup(cs->cs_initiator_id, M_CTL);
1219 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1221 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1223 cs->cs_ctl_initid = -1;
1226 cs->cs_ctl_initid = i;
1228 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1235 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1239 if (cs->cs_ctl_initid == -1)
1242 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1244 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1247 cs->cs_ctl_initid = -1;
1250 static struct cfiscsi_session *
1251 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1253 struct cfiscsi_session *cs;
1256 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1258 CFISCSI_WARN("malloc failed");
1261 cs->cs_ctl_initid = -1;
1263 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1264 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1265 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1266 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1267 #ifdef ICL_KERNEL_PROXY
1268 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1271 cs->cs_conn = icl_new_conn(offload, false, "cfiscsi", &cs->cs_lock);
1272 if (cs->cs_conn == NULL) {
1273 free(cs, M_CFISCSI);
1276 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1277 cs->cs_conn->ic_error = cfiscsi_error_callback;
1278 cs->cs_conn->ic_prv0 = cs;
1280 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1282 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1283 free(cs, M_CFISCSI);
1287 mtx_lock(&softc->lock);
1288 cs->cs_id = ++softc->last_session_id;
1289 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1290 mtx_unlock(&softc->lock);
1293 * Start pinging the initiator.
1295 callout_init(&cs->cs_callout, 1);
1296 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1302 cfiscsi_session_delete(struct cfiscsi_session *cs)
1304 struct cfiscsi_softc *softc;
1306 softc = &cfiscsi_softc;
1308 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1309 ("destroying session with outstanding CTL pdus"));
1310 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1311 ("destroying session with non-empty queue"));
1313 mtx_lock(&softc->lock);
1314 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1315 mtx_unlock(&softc->lock);
1317 cfiscsi_session_unregister_initiator(cs);
1318 if (cs->cs_target != NULL)
1319 cfiscsi_target_release(cs->cs_target);
1320 icl_conn_close(cs->cs_conn);
1321 icl_conn_free(cs->cs_conn);
1322 free(cs, M_CFISCSI);
1323 cv_signal(&softc->sessions_cv);
1329 struct cfiscsi_softc *softc;
1331 softc = &cfiscsi_softc;
1332 bzero(softc, sizeof(*softc));
1333 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1335 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1336 #ifdef ICL_KERNEL_PROXY
1337 cv_init(&softc->accept_cv, "cfiscsi_accept");
1339 TAILQ_INIT(&softc->sessions);
1340 TAILQ_INIT(&softc->targets);
1342 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1343 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1350 cfiscsi_shutdown(void)
1352 struct cfiscsi_softc *softc = &cfiscsi_softc;
1354 if (!TAILQ_EMPTY(&softc->sessions) || !TAILQ_EMPTY(&softc->targets))
1357 uma_zdestroy(cfiscsi_data_wait_zone);
1358 #ifdef ICL_KERNEL_PROXY
1359 cv_destroy(&softc->accept_cv);
1361 cv_destroy(&softc->sessions_cv);
1362 mtx_destroy(&softc->lock);
1366 #ifdef ICL_KERNEL_PROXY
1368 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1370 struct cfiscsi_session *cs;
1372 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1374 CFISCSI_WARN("failed to create session");
1378 icl_conn_handoff_sock(cs->cs_conn, so);
1379 cs->cs_initiator_sa = sa;
1380 cs->cs_portal_id = portal_id;
1381 cs->cs_waiting_for_ctld = true;
1382 cv_signal(&cfiscsi_softc.accept_cv);
1387 cfiscsi_online(void *arg)
1389 struct cfiscsi_softc *softc;
1390 struct cfiscsi_target *ct;
1393 ct = (struct cfiscsi_target *)arg;
1394 softc = ct->ct_softc;
1396 mtx_lock(&softc->lock);
1397 if (ct->ct_online) {
1398 mtx_unlock(&softc->lock);
1402 online = softc->online++;
1403 mtx_unlock(&softc->lock);
1407 #ifdef ICL_KERNEL_PROXY
1408 if (softc->listener != NULL)
1409 icl_listen_free(softc->listener);
1410 softc->listener = icl_listen_new(cfiscsi_accept);
1415 cfiscsi_offline(void *arg)
1417 struct cfiscsi_softc *softc;
1418 struct cfiscsi_target *ct;
1419 struct cfiscsi_session *cs;
1422 ct = (struct cfiscsi_target *)arg;
1423 softc = ct->ct_softc;
1425 mtx_lock(&softc->lock);
1426 if (!ct->ct_online) {
1427 mtx_unlock(&softc->lock);
1431 online = --softc->online;
1433 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1434 if (cs->cs_target == ct)
1435 cfiscsi_session_terminate(cs);
1438 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1439 if (cs->cs_target == ct)
1443 cv_wait(&softc->sessions_cv, &softc->lock);
1444 } while (cs != NULL && ct->ct_online == 0);
1445 mtx_unlock(&softc->lock);
1449 #ifdef ICL_KERNEL_PROXY
1450 icl_listen_free(softc->listener);
1451 softc->listener = NULL;
1456 cfiscsi_info(void *arg, struct sbuf *sb)
1458 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1461 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1467 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1469 struct cfiscsi_softc *softc;
1470 struct cfiscsi_session *cs, *cs2;
1471 struct cfiscsi_target *ct;
1472 struct ctl_iscsi_handoff_params *cihp;
1475 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1476 softc = &cfiscsi_softc;
1478 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1479 cihp->initiator_name, cihp->initiator_addr,
1482 ct = cfiscsi_target_find(softc, cihp->target_name,
1483 cihp->portal_group_tag);
1485 ci->status = CTL_ISCSI_ERROR;
1486 snprintf(ci->error_str, sizeof(ci->error_str),
1487 "%s: target not found", __func__);
1491 #ifdef ICL_KERNEL_PROXY
1492 if (cihp->socket > 0 && cihp->connection_id > 0) {
1493 snprintf(ci->error_str, sizeof(ci->error_str),
1494 "both socket and connection_id set");
1495 ci->status = CTL_ISCSI_ERROR;
1496 cfiscsi_target_release(ct);
1499 if (cihp->socket == 0) {
1500 mtx_lock(&cfiscsi_softc.lock);
1501 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1502 if (cs->cs_id == cihp->connection_id)
1506 mtx_unlock(&cfiscsi_softc.lock);
1507 snprintf(ci->error_str, sizeof(ci->error_str),
1508 "connection not found");
1509 ci->status = CTL_ISCSI_ERROR;
1510 cfiscsi_target_release(ct);
1513 mtx_unlock(&cfiscsi_softc.lock);
1516 cs = cfiscsi_session_new(softc, cihp->offload);
1518 ci->status = CTL_ISCSI_ERROR;
1519 snprintf(ci->error_str, sizeof(ci->error_str),
1520 "%s: cfiscsi_session_new failed", __func__);
1521 cfiscsi_target_release(ct);
1524 #ifdef ICL_KERNEL_PROXY
1529 * First PDU of Full Feature phase has the same CmdSN as the last
1530 * PDU from the Login Phase received from the initiator. Thus,
1533 cs->cs_cmdsn = cihp->cmdsn;
1534 cs->cs_statsn = cihp->statsn;
1535 cs->cs_max_recv_data_segment_length = cihp->max_recv_data_segment_length;
1536 cs->cs_max_send_data_segment_length = cihp->max_send_data_segment_length;
1537 cs->cs_max_burst_length = cihp->max_burst_length;
1538 cs->cs_first_burst_length = cihp->first_burst_length;
1539 cs->cs_immediate_data = !!cihp->immediate_data;
1540 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1541 cs->cs_conn->ic_header_crc32c = true;
1542 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1543 cs->cs_conn->ic_data_crc32c = true;
1545 strlcpy(cs->cs_initiator_name,
1546 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1547 strlcpy(cs->cs_initiator_addr,
1548 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1549 strlcpy(cs->cs_initiator_alias,
1550 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1551 memcpy(cs->cs_initiator_isid,
1552 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1553 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1554 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1555 cihp->initiator_isid[0], cihp->initiator_isid[1],
1556 cihp->initiator_isid[2], cihp->initiator_isid[3],
1557 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1559 mtx_lock(&softc->lock);
1560 if (ct->ct_online == 0) {
1561 mtx_unlock(&softc->lock);
1562 cfiscsi_session_terminate(cs);
1563 cfiscsi_target_release(ct);
1564 ci->status = CTL_ISCSI_ERROR;
1565 snprintf(ci->error_str, sizeof(ci->error_str),
1566 "%s: port offline", __func__);
1570 mtx_unlock(&softc->lock);
1572 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1574 if (!cs->cs_terminating) {
1575 mtx_lock(&softc->lock);
1576 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1577 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1578 cs->cs_target == cs2->cs_target &&
1579 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1580 if (strcmp(cs->cs_initiator_addr,
1581 cs2->cs_initiator_addr) != 0) {
1582 CFISCSI_SESSION_WARN(cs2,
1583 "session reinstatement from "
1584 "different address %s",
1585 cs->cs_initiator_addr);
1587 CFISCSI_SESSION_DEBUG(cs2,
1588 "session reinstatement");
1590 cfiscsi_session_terminate(cs2);
1591 mtx_unlock(&softc->lock);
1592 pause("cfiscsi_reinstate", 1);
1596 mtx_unlock(&softc->lock);
1600 * Register initiator with CTL.
1602 cfiscsi_session_register_initiator(cs);
1604 #ifdef ICL_KERNEL_PROXY
1605 if (cihp->socket > 0) {
1607 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1609 cfiscsi_session_terminate(cs);
1610 refcount_release(&cs->cs_outstanding_ctl_pdus);
1611 ci->status = CTL_ISCSI_ERROR;
1612 snprintf(ci->error_str, sizeof(ci->error_str),
1613 "%s: icl_conn_handoff failed with error %d",
1617 #ifdef ICL_KERNEL_PROXY
1621 #ifdef ICL_KERNEL_PROXY
1622 cs->cs_login_phase = false;
1625 * First PDU of the Full Feature phase has likely already arrived.
1626 * We have to pick it up and execute properly.
1628 if (cs->cs_login_pdu != NULL) {
1629 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1630 cfiscsi_pdu_handle(cs->cs_login_pdu);
1631 cs->cs_login_pdu = NULL;
1635 refcount_release(&cs->cs_outstanding_ctl_pdus);
1636 ci->status = CTL_ISCSI_OK;
1640 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1642 struct ctl_iscsi_list_params *cilp;
1643 struct cfiscsi_session *cs;
1644 struct cfiscsi_softc *softc;
1648 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1649 softc = &cfiscsi_softc;
1651 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1653 ci->status = CTL_ISCSI_ERROR;
1654 snprintf(ci->error_str, sizeof(ci->error_str),
1655 "Unable to allocate %d bytes for iSCSI session list",
1660 sbuf_printf(sb, "<ctlislist>\n");
1661 mtx_lock(&softc->lock);
1662 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1663 #ifdef ICL_KERNEL_PROXY
1664 if (cs->cs_target == NULL)
1667 error = sbuf_printf(sb, "<connection id=\"%d\">"
1668 "<initiator>%s</initiator>"
1669 "<initiator_addr>%s</initiator_addr>"
1670 "<initiator_alias>%s</initiator_alias>"
1671 "<target>%s</target>"
1672 "<target_alias>%s</target_alias>"
1673 "<target_portal_group_tag>%u</target_portal_group_tag>"
1674 "<header_digest>%s</header_digest>"
1675 "<data_digest>%s</data_digest>"
1676 "<max_recv_data_segment_length>%d</max_recv_data_segment_length>"
1677 "<max_send_data_segment_length>%d</max_send_data_segment_length>"
1678 "<max_burst_length>%d</max_burst_length>"
1679 "<first_burst_length>%d</first_burst_length>"
1680 "<immediate_data>%d</immediate_data>"
1682 "<offload>%s</offload>"
1685 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1686 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1687 cs->cs_target->ct_tag,
1688 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1689 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1690 cs->cs_max_recv_data_segment_length,
1691 cs->cs_max_send_data_segment_length,
1692 cs->cs_max_burst_length,
1693 cs->cs_first_burst_length,
1694 cs->cs_immediate_data,
1695 cs->cs_conn->ic_iser,
1696 cs->cs_conn->ic_offload);
1700 mtx_unlock(&softc->lock);
1701 error = sbuf_printf(sb, "</ctlislist>\n");
1704 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1705 snprintf(ci->error_str, sizeof(ci->error_str),
1706 "Out of space, %d bytes is too small", cilp->alloc_len);
1711 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1714 snprintf(ci->error_str, sizeof(ci->error_str),
1715 "copyout failed with error %d", error);
1716 ci->status = CTL_ISCSI_ERROR;
1719 cilp->fill_len = sbuf_len(sb) + 1;
1720 ci->status = CTL_ISCSI_OK;
1725 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1727 struct icl_pdu *response;
1728 struct iscsi_bhs_asynchronous_message *bhsam;
1729 struct ctl_iscsi_logout_params *cilp;
1730 struct cfiscsi_session *cs;
1731 struct cfiscsi_softc *softc;
1734 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1735 softc = &cfiscsi_softc;
1737 mtx_lock(&softc->lock);
1738 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1739 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1740 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1741 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1744 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1745 if (response == NULL) {
1746 ci->status = CTL_ISCSI_ERROR;
1747 snprintf(ci->error_str, sizeof(ci->error_str),
1748 "Unable to allocate memory");
1749 mtx_unlock(&softc->lock);
1753 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1754 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1755 bhsam->bhsam_flags = 0x80;
1756 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1757 bhsam->bhsam_parameter3 = htons(10);
1758 cfiscsi_pdu_queue(response);
1761 mtx_unlock(&softc->lock);
1764 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1765 snprintf(ci->error_str, sizeof(ci->error_str),
1766 "No matching connections found");
1770 ci->status = CTL_ISCSI_OK;
1774 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1776 struct icl_pdu *response;
1777 struct iscsi_bhs_asynchronous_message *bhsam;
1778 struct ctl_iscsi_terminate_params *citp;
1779 struct cfiscsi_session *cs;
1780 struct cfiscsi_softc *softc;
1783 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1784 softc = &cfiscsi_softc;
1786 mtx_lock(&softc->lock);
1787 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1788 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1789 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1790 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1793 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1794 if (response == NULL) {
1796 * Oh well. Just terminate the connection.
1799 bhsam = (struct iscsi_bhs_asynchronous_message *)
1801 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1802 bhsam->bhsam_flags = 0x80;
1803 bhsam->bhsam_0xffffffff = 0xffffffff;
1804 bhsam->bhsam_async_event =
1805 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1806 cfiscsi_pdu_queue(response);
1808 cfiscsi_session_terminate(cs);
1811 mtx_unlock(&softc->lock);
1814 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1815 snprintf(ci->error_str, sizeof(ci->error_str),
1816 "No matching connections found");
1820 ci->status = CTL_ISCSI_OK;
1824 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1826 struct ctl_iscsi_limits_params *cilp;
1827 struct icl_drv_limits idl;
1830 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1832 error = icl_limits(cilp->offload, false, &idl);
1834 ci->status = CTL_ISCSI_ERROR;
1835 snprintf(ci->error_str, sizeof(ci->error_str),
1836 "%s: icl_limits failed with error %d",
1841 cilp->max_recv_data_segment_length =
1842 idl.idl_max_recv_data_segment_length;
1843 cilp->max_send_data_segment_length =
1844 idl.idl_max_send_data_segment_length;
1845 cilp->max_burst_length = idl.idl_max_burst_length;
1846 cilp->first_burst_length = idl.idl_first_burst_length;
1848 ci->status = CTL_ISCSI_OK;
1851 #ifdef ICL_KERNEL_PROXY
1853 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1855 struct ctl_iscsi_listen_params *cilp;
1856 struct sockaddr *sa;
1859 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1861 if (cfiscsi_softc.listener == NULL) {
1862 CFISCSI_DEBUG("no listener");
1863 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1864 ci->status = CTL_ISCSI_ERROR;
1868 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1870 CFISCSI_DEBUG("getsockaddr, error %d", error);
1871 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1872 ci->status = CTL_ISCSI_ERROR;
1876 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1877 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1880 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1881 snprintf(ci->error_str, sizeof(ci->error_str),
1882 "icl_listen_add failed, error %d", error);
1883 ci->status = CTL_ISCSI_ERROR;
1887 ci->status = CTL_ISCSI_OK;
1891 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1893 struct ctl_iscsi_accept_params *ciap;
1894 struct cfiscsi_session *cs;
1897 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1899 mtx_lock(&cfiscsi_softc.lock);
1901 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1902 if (cs->cs_waiting_for_ctld)
1907 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1909 mtx_unlock(&cfiscsi_softc.lock);
1910 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1911 ci->status = CTL_ISCSI_ERROR;
1915 mtx_unlock(&cfiscsi_softc.lock);
1917 cs->cs_waiting_for_ctld = false;
1918 cs->cs_login_phase = true;
1920 ciap->connection_id = cs->cs_id;
1921 ciap->portal_id = cs->cs_portal_id;
1922 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1923 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1924 cs->cs_initiator_sa->sa_len);
1926 snprintf(ci->error_str, sizeof(ci->error_str),
1927 "copyout failed with error %d", error);
1928 ci->status = CTL_ISCSI_ERROR;
1932 ci->status = CTL_ISCSI_OK;
1936 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1938 struct ctl_iscsi_send_params *cisp;
1939 struct cfiscsi_session *cs;
1945 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1947 mtx_lock(&cfiscsi_softc.lock);
1948 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1949 if (cs->cs_id == cisp->connection_id)
1953 mtx_unlock(&cfiscsi_softc.lock);
1954 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1955 ci->status = CTL_ISCSI_ERROR;
1958 mtx_unlock(&cfiscsi_softc.lock);
1961 if (cs->cs_login_phase == false)
1965 if (cs->cs_terminating) {
1966 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1967 ci->status = CTL_ISCSI_ERROR;
1971 datalen = cisp->data_segment_len;
1975 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1976 if (datalen > 65535) {
1977 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1978 ci->status = CTL_ISCSI_ERROR;
1982 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1983 error = copyin(cisp->data_segment, data, datalen);
1985 free(data, M_CFISCSI);
1986 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1987 ci->status = CTL_ISCSI_ERROR;
1992 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1993 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1995 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1996 free(data, M_CFISCSI);
1998 CFISCSI_SESSION_LOCK(cs);
2000 CFISCSI_SESSION_UNLOCK(cs);
2001 ci->status = CTL_ISCSI_OK;
2005 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
2007 struct ctl_iscsi_receive_params *cirp;
2008 struct cfiscsi_session *cs;
2013 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
2015 mtx_lock(&cfiscsi_softc.lock);
2016 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2017 if (cs->cs_id == cirp->connection_id)
2021 mtx_unlock(&cfiscsi_softc.lock);
2022 snprintf(ci->error_str, sizeof(ci->error_str),
2023 "connection not found");
2024 ci->status = CTL_ISCSI_ERROR;
2027 mtx_unlock(&cfiscsi_softc.lock);
2030 if (is->is_login_phase == false)
2034 CFISCSI_SESSION_LOCK(cs);
2035 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2036 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2038 CFISCSI_SESSION_UNLOCK(cs);
2039 snprintf(ci->error_str, sizeof(ci->error_str),
2040 "interrupted by signal");
2041 ci->status = CTL_ISCSI_ERROR;
2046 if (cs->cs_terminating) {
2047 CFISCSI_SESSION_UNLOCK(cs);
2048 snprintf(ci->error_str, sizeof(ci->error_str),
2049 "connection terminating");
2050 ci->status = CTL_ISCSI_ERROR;
2053 ip = cs->cs_login_pdu;
2054 cs->cs_login_pdu = NULL;
2055 CFISCSI_SESSION_UNLOCK(cs);
2057 if (ip->ip_data_len > cirp->data_segment_len) {
2059 snprintf(ci->error_str, sizeof(ci->error_str),
2060 "data segment too big");
2061 ci->status = CTL_ISCSI_ERROR;
2065 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2066 if (ip->ip_data_len > 0) {
2067 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2068 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2069 copyout(data, cirp->data_segment, ip->ip_data_len);
2070 free(data, M_CFISCSI);
2074 ci->status = CTL_ISCSI_OK;
2077 #endif /* !ICL_KERNEL_PROXY */
2080 cfiscsi_ioctl_port_create(struct ctl_req *req)
2082 struct cfiscsi_target *ct;
2083 struct ctl_port *port;
2084 const char *target, *alias, *tags;
2085 struct scsi_vpd_id_descriptor *desc;
2087 int retval, len, idlen;
2090 ctl_init_opts(&opts, req->num_args, req->kern_args);
2091 target = ctl_get_opt(&opts, "cfiscsi_target");
2092 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
2093 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2094 if (target == NULL || tags == NULL) {
2095 req->status = CTL_LUN_ERROR;
2096 snprintf(req->error_str, sizeof(req->error_str),
2097 "Missing required argument");
2098 ctl_free_opts(&opts);
2101 tag = strtol(tags, (char **)NULL, 10);
2102 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2104 req->status = CTL_LUN_ERROR;
2105 snprintf(req->error_str, sizeof(req->error_str),
2106 "failed to create target \"%s\"", target);
2107 ctl_free_opts(&opts);
2110 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2111 req->status = CTL_LUN_ERROR;
2112 snprintf(req->error_str, sizeof(req->error_str),
2113 "target \"%s\" for portal group tag %u already exists",
2115 cfiscsi_target_release(ct);
2116 ctl_free_opts(&opts);
2119 port = &ct->ct_port;
2121 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2124 port->frontend = &cfiscsi_frontend;
2125 port->port_type = CTL_PORT_ISCSI;
2126 /* XXX KDM what should the real number be here? */
2127 port->num_requested_ctl_io = 4096;
2128 port->port_name = "iscsi";
2129 port->physical_port = tag;
2130 port->virtual_port = ct->ct_target_id;
2131 port->port_online = cfiscsi_online;
2132 port->port_offline = cfiscsi_offline;
2133 port->port_info = cfiscsi_info;
2134 port->onoff_arg = ct;
2135 port->fe_datamove = cfiscsi_datamove;
2136 port->fe_done = cfiscsi_done;
2137 port->targ_port = -1;
2139 port->options = opts;
2142 /* Generate Port ID. */
2143 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2144 idlen = roundup2(idlen, 4);
2145 len = sizeof(struct scsi_vpd_device_id) + idlen;
2146 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2147 M_CTL, M_WAITOK | M_ZERO);
2148 port->port_devid->len = len;
2149 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2150 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2151 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2152 SVPD_ID_TYPE_SCSI_NAME;
2153 desc->length = idlen;
2154 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2156 /* Generate Target ID. */
2157 idlen = strlen(target) + 1;
2158 idlen = roundup2(idlen, 4);
2159 len = sizeof(struct scsi_vpd_device_id) + idlen;
2160 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2161 M_CTL, M_WAITOK | M_ZERO);
2162 port->target_devid->len = len;
2163 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2164 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2165 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2166 SVPD_ID_TYPE_SCSI_NAME;
2167 desc->length = idlen;
2168 strlcpy(desc->identifier, target, idlen);
2170 retval = ctl_port_register(port);
2172 ctl_free_opts(&port->options);
2173 free(port->port_devid, M_CFISCSI);
2174 free(port->target_devid, M_CFISCSI);
2175 cfiscsi_target_release(ct);
2176 req->status = CTL_LUN_ERROR;
2177 snprintf(req->error_str, sizeof(req->error_str),
2178 "ctl_port_register() failed with error %d", retval);
2182 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2183 req->status = CTL_LUN_OK;
2184 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2185 sizeof(port->targ_port)); //XXX
2189 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2191 struct cfiscsi_target *ct;
2192 const char *target, *tags;
2196 ctl_init_opts(&opts, req->num_args, req->kern_args);
2197 target = ctl_get_opt(&opts, "cfiscsi_target");
2198 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2199 if (target == NULL || tags == NULL) {
2200 ctl_free_opts(&opts);
2201 req->status = CTL_LUN_ERROR;
2202 snprintf(req->error_str, sizeof(req->error_str),
2203 "Missing required argument");
2206 tag = strtol(tags, (char **)NULL, 10);
2207 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2209 ctl_free_opts(&opts);
2210 req->status = CTL_LUN_ERROR;
2211 snprintf(req->error_str, sizeof(req->error_str),
2212 "can't find target \"%s\"", target);
2215 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2216 ctl_free_opts(&opts);
2217 req->status = CTL_LUN_ERROR;
2218 snprintf(req->error_str, sizeof(req->error_str),
2219 "target \"%s\" is already dying", target);
2222 ctl_free_opts(&opts);
2224 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2225 ctl_port_offline(&ct->ct_port);
2226 cfiscsi_target_release(ct);
2227 cfiscsi_target_release(ct);
2228 req->status = CTL_LUN_OK;
2232 cfiscsi_ioctl(struct cdev *dev,
2233 u_long cmd, caddr_t addr, int flag, struct thread *td)
2235 struct ctl_iscsi *ci;
2236 struct ctl_req *req;
2238 if (cmd == CTL_PORT_REQ) {
2239 req = (struct ctl_req *)addr;
2240 switch (req->reqtype) {
2241 case CTL_REQ_CREATE:
2242 cfiscsi_ioctl_port_create(req);
2244 case CTL_REQ_REMOVE:
2245 cfiscsi_ioctl_port_remove(req);
2248 req->status = CTL_LUN_ERROR;
2249 snprintf(req->error_str, sizeof(req->error_str),
2250 "Unsupported request type %d", req->reqtype);
2255 if (cmd != CTL_ISCSI)
2258 ci = (struct ctl_iscsi *)addr;
2260 case CTL_ISCSI_HANDOFF:
2261 cfiscsi_ioctl_handoff(ci);
2263 case CTL_ISCSI_LIST:
2264 cfiscsi_ioctl_list(ci);
2266 case CTL_ISCSI_LOGOUT:
2267 cfiscsi_ioctl_logout(ci);
2269 case CTL_ISCSI_TERMINATE:
2270 cfiscsi_ioctl_terminate(ci);
2272 case CTL_ISCSI_LIMITS:
2273 cfiscsi_ioctl_limits(ci);
2275 #ifdef ICL_KERNEL_PROXY
2276 case CTL_ISCSI_LISTEN:
2277 cfiscsi_ioctl_listen(ci);
2279 case CTL_ISCSI_ACCEPT:
2280 cfiscsi_ioctl_accept(ci);
2282 case CTL_ISCSI_SEND:
2283 cfiscsi_ioctl_send(ci);
2285 case CTL_ISCSI_RECEIVE:
2286 cfiscsi_ioctl_receive(ci);
2289 case CTL_ISCSI_LISTEN:
2290 case CTL_ISCSI_ACCEPT:
2291 case CTL_ISCSI_SEND:
2292 case CTL_ISCSI_RECEIVE:
2293 ci->status = CTL_ISCSI_ERROR;
2294 snprintf(ci->error_str, sizeof(ci->error_str),
2295 "%s: CTL compiled without ICL_KERNEL_PROXY",
2298 #endif /* !ICL_KERNEL_PROXY */
2300 ci->status = CTL_ISCSI_ERROR;
2301 snprintf(ci->error_str, sizeof(ci->error_str),
2302 "%s: invalid iSCSI request type %d", __func__, ci->type);
2310 cfiscsi_target_hold(struct cfiscsi_target *ct)
2313 refcount_acquire(&ct->ct_refcount);
2317 cfiscsi_target_release(struct cfiscsi_target *ct)
2319 struct cfiscsi_softc *softc;
2321 softc = ct->ct_softc;
2322 mtx_lock(&softc->lock);
2323 if (refcount_release(&ct->ct_refcount)) {
2324 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2325 mtx_unlock(&softc->lock);
2326 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2327 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2328 if (ctl_port_deregister(&ct->ct_port) != 0)
2329 printf("%s: ctl_port_deregister() failed\n",
2332 free(ct, M_CFISCSI);
2336 mtx_unlock(&softc->lock);
2339 static struct cfiscsi_target *
2340 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2342 struct cfiscsi_target *ct;
2344 mtx_lock(&softc->lock);
2345 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2346 if (ct->ct_tag != tag ||
2347 strcmp(name, ct->ct_name) != 0 ||
2348 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2350 cfiscsi_target_hold(ct);
2351 mtx_unlock(&softc->lock);
2354 mtx_unlock(&softc->lock);
2359 static struct cfiscsi_target *
2360 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2361 const char *alias, uint16_t tag)
2363 struct cfiscsi_target *ct, *newct;
2365 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2368 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2370 mtx_lock(&softc->lock);
2371 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2372 if (ct->ct_tag != tag ||
2373 strcmp(name, ct->ct_name) != 0 ||
2374 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2376 cfiscsi_target_hold(ct);
2377 mtx_unlock(&softc->lock);
2378 free(newct, M_CFISCSI);
2382 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2384 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2385 newct->ct_tag = tag;
2386 refcount_init(&newct->ct_refcount, 1);
2387 newct->ct_softc = softc;
2388 if (TAILQ_EMPTY(&softc->targets))
2389 softc->last_target_id = 0;
2390 newct->ct_target_id = ++softc->last_target_id;
2391 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2392 mtx_unlock(&softc->lock);
2398 cfiscsi_datamove_in(union ctl_io *io)
2400 struct cfiscsi_session *cs;
2401 struct icl_pdu *request, *response;
2402 const struct iscsi_bhs_scsi_command *bhssc;
2403 struct iscsi_bhs_data_in *bhsdi;
2404 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2405 size_t len, expected_len, sg_len, buffer_offset;
2406 const char *sg_addr;
2407 int ctl_sg_count, error, i;
2409 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2410 cs = PDU_SESSION(request);
2412 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2413 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2414 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2415 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2417 if (io->scsiio.kern_sg_entries > 0) {
2418 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2419 ctl_sg_count = io->scsiio.kern_sg_entries;
2421 ctl_sglist = &ctl_sg_entry;
2422 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2423 ctl_sglist->len = io->scsiio.kern_data_len;
2428 * This is the total amount of data to be transferred within the current
2429 * SCSI command. We need to record it so that we can properly report
2430 * underflow/underflow.
2432 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2435 * This is the offset within the current SCSI command; for the first
2436 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2437 * it will be the sum of lengths of previous ones.
2439 buffer_offset = io->scsiio.kern_rel_offset;
2442 * This is the transfer length expected by the initiator. In theory,
2443 * it could be different from the correct amount of data from the SCSI
2444 * point of view, even if that doesn't make any sense.
2446 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2448 if (expected_len != io->scsiio.kern_total_len) {
2449 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2450 "actual length %zd", expected_len,
2451 (size_t)io->scsiio.kern_total_len);
2455 if (buffer_offset >= expected_len) {
2457 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2458 "already sent the expected len", buffer_offset);
2460 io->scsiio.be_move_done(io);
2470 if (response == NULL) {
2471 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2472 if (response == NULL) {
2473 CFISCSI_SESSION_WARN(cs, "failed to "
2474 "allocate memory; dropping connection");
2475 ctl_set_busy(&io->scsiio);
2476 io->scsiio.be_move_done(io);
2477 cfiscsi_session_terminate(cs);
2480 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2481 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2482 bhsdi->bhsdi_initiator_task_tag =
2483 bhssc->bhssc_initiator_task_tag;
2484 bhsdi->bhsdi_target_transfer_tag = 0xffffffff;
2485 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2486 PDU_EXPDATASN(request)++;
2487 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2490 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2492 sg_addr = ctl_sglist[i].addr;
2493 sg_len = ctl_sglist[i].len;
2494 KASSERT(sg_len > 0, ("sg_len <= 0"));
2500 * Truncate to maximum data segment length.
2502 KASSERT(response->ip_data_len < cs->cs_max_send_data_segment_length,
2503 ("ip_data_len %zd >= max_send_data_segment_length %d",
2504 response->ip_data_len, cs->cs_max_send_data_segment_length));
2505 if (response->ip_data_len + len >
2506 cs->cs_max_send_data_segment_length) {
2507 len = cs->cs_max_send_data_segment_length -
2508 response->ip_data_len;
2509 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2514 * Truncate to expected data transfer length.
2516 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2517 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2518 buffer_offset, response->ip_data_len, expected_len));
2519 if (buffer_offset + response->ip_data_len + len > expected_len) {
2520 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2521 "to expected data transfer length %zd",
2522 buffer_offset + response->ip_data_len + len, expected_len);
2523 len = expected_len - (buffer_offset + response->ip_data_len);
2524 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2528 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2530 CFISCSI_SESSION_WARN(cs, "failed to "
2531 "allocate memory; dropping connection");
2532 icl_pdu_free(response);
2533 ctl_set_busy(&io->scsiio);
2534 io->scsiio.be_move_done(io);
2535 cfiscsi_session_terminate(cs);
2540 io->scsiio.kern_data_resid -= len;
2542 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2543 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2544 buffer_offset, response->ip_data_len, expected_len));
2545 if (buffer_offset + response->ip_data_len == expected_len) {
2547 * Already have the amount of data the initiator wanted.
2554 * End of scatter-gather segment;
2555 * proceed to the next one...
2557 if (i == ctl_sg_count - 1) {
2559 * ... unless this was the last one.
2566 if (response->ip_data_len == cs->cs_max_send_data_segment_length) {
2568 * Can't stuff more data into the current PDU;
2569 * queue it. Note that's not enough to check
2570 * for kern_data_resid == 0 instead; there
2571 * may be several Data-In PDUs for the final
2572 * call to cfiscsi_datamove(), and we want
2573 * to set the F flag only on the last of them.
2575 buffer_offset += response->ip_data_len;
2576 if (buffer_offset == io->scsiio.kern_total_len ||
2577 buffer_offset == expected_len) {
2578 buffer_offset -= response->ip_data_len;
2581 cfiscsi_pdu_queue(response);
2586 if (response != NULL) {
2587 buffer_offset += response->ip_data_len;
2588 if (buffer_offset == io->scsiio.kern_total_len ||
2589 buffer_offset == expected_len) {
2590 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2591 if (io->io_hdr.status == CTL_SUCCESS) {
2592 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2593 if (PDU_TOTAL_TRANSFER_LEN(request) <
2594 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2595 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2596 bhsdi->bhsdi_residual_count =
2597 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2598 PDU_TOTAL_TRANSFER_LEN(request));
2599 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2600 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2601 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2602 bhsdi->bhsdi_residual_count =
2603 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2604 ntohl(bhssc->bhssc_expected_data_transfer_length));
2606 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2607 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2610 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2611 cfiscsi_pdu_queue(response);
2614 io->scsiio.be_move_done(io);
2618 cfiscsi_datamove_out(union ctl_io *io)
2620 struct cfiscsi_session *cs;
2621 struct icl_pdu *request, *response;
2622 const struct iscsi_bhs_scsi_command *bhssc;
2623 struct iscsi_bhs_r2t *bhsr2t;
2624 struct cfiscsi_data_wait *cdw;
2625 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2626 uint32_t expected_len, datamove_len, r2t_off, r2t_len;
2627 uint32_t target_transfer_tag;
2630 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2631 cs = PDU_SESSION(request);
2633 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2634 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2635 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2636 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2639 * We need to record it so that we can properly report
2640 * underflow/underflow.
2642 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2645 * Complete write underflow. Not a single byte to read. Return.
2647 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2648 if (io->scsiio.kern_rel_offset >= expected_len) {
2649 io->scsiio.be_move_done(io);
2652 datamove_len = MIN(io->scsiio.kern_data_len,
2653 expected_len - io->scsiio.kern_rel_offset);
2655 target_transfer_tag =
2656 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2657 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2658 &target_transfer_tag);
2660 CFISCSI_SESSION_WARN(cs, "failed to "
2661 "allocate memory; dropping connection");
2662 ctl_set_busy(&io->scsiio);
2663 io->scsiio.be_move_done(io);
2664 cfiscsi_session_terminate(cs);
2668 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2669 "task tag 0x%x, target transfer tag 0x%x",
2670 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2673 cdw->cdw_ctl_io = io;
2674 cdw->cdw_target_transfer_tag = target_transfer_tag;
2675 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2676 cdw->cdw_r2t_end = datamove_len;
2677 cdw->cdw_datasn = 0;
2679 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2680 if (io->scsiio.kern_sg_entries > 0) {
2681 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2683 ctl_sglist = &ctl_sg_entry;
2684 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2685 ctl_sglist->len = datamove_len;
2687 cdw->cdw_sg_index = 0;
2688 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2689 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2690 r2t_off = io->scsiio.ext_data_filled;
2691 while (r2t_off > 0) {
2692 if (r2t_off >= cdw->cdw_sg_len) {
2693 r2t_off -= cdw->cdw_sg_len;
2694 cdw->cdw_sg_index++;
2695 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2696 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2699 cdw->cdw_sg_addr += r2t_off;
2700 cdw->cdw_sg_len -= r2t_off;
2704 if (cs->cs_immediate_data &&
2705 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2706 icl_pdu_data_segment_length(request)) {
2707 done = cfiscsi_handle_data_segment(request, cdw);
2709 cfiscsi_data_wait_free(cs, cdw);
2710 io->scsiio.be_move_done(io);
2715 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2716 r2t_len = MIN(datamove_len - io->scsiio.ext_data_filled,
2717 cs->cs_max_burst_length);
2718 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2720 CFISCSI_SESSION_LOCK(cs);
2721 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2722 CFISCSI_SESSION_UNLOCK(cs);
2725 * XXX: We should limit the number of outstanding R2T PDUs
2726 * per task to MaxOutstandingR2T.
2728 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2729 if (response == NULL) {
2730 CFISCSI_SESSION_WARN(cs, "failed to "
2731 "allocate memory; dropping connection");
2732 ctl_set_busy(&io->scsiio);
2733 io->scsiio.be_move_done(io);
2734 cfiscsi_session_terminate(cs);
2737 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
2738 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2739 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2740 bhsr2t->bhsr2t_flags = 0x80;
2741 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2742 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2743 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2745 * XXX: Here we assume that cfiscsi_datamove() won't ever
2746 * be running concurrently on several CPUs for a given
2749 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2750 PDU_R2TSN(request)++;
2752 * This is the offset within the current SCSI command;
2753 * i.e. for the first call of datamove(), it will be 0,
2754 * and for subsequent ones it will be the sum of lengths
2757 * The ext_data_filled is to account for unsolicited
2758 * (immediate) data that might have already arrived.
2760 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2762 * This is the total length (sum of S/G lengths) this call
2763 * to cfiscsi_datamove() is supposed to handle, limited by
2766 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2767 cfiscsi_pdu_queue(response);
2771 cfiscsi_datamove(union ctl_io *io)
2774 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2775 cfiscsi_datamove_in(io);
2777 /* We hadn't received anything during this datamove yet. */
2778 io->scsiio.ext_data_filled = 0;
2779 cfiscsi_datamove_out(io);
2784 cfiscsi_scsi_command_done(union ctl_io *io)
2786 struct icl_pdu *request, *response;
2787 struct iscsi_bhs_scsi_command *bhssc;
2788 struct iscsi_bhs_scsi_response *bhssr;
2790 struct cfiscsi_data_wait *cdw;
2792 struct cfiscsi_session *cs;
2793 uint16_t sense_length;
2795 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2796 cs = PDU_SESSION(request);
2797 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2798 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2799 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2800 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2802 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2803 // bhssc->bhssc_initiator_task_tag);
2806 CFISCSI_SESSION_LOCK(cs);
2807 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2808 KASSERT(bhssc->bhssc_initiator_task_tag !=
2809 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2810 CFISCSI_SESSION_UNLOCK(cs);
2814 * Do not return status for aborted commands.
2815 * There are exceptions, but none supported by CTL yet.
2817 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2818 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2819 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2821 icl_pdu_free(request);
2825 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2826 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2827 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2828 bhssr->bhssr_flags = 0x80;
2830 * XXX: We don't deal with bidirectional under/overflows;
2831 * does anything actually support those?
2833 if (PDU_TOTAL_TRANSFER_LEN(request) <
2834 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2835 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2836 bhssr->bhssr_residual_count =
2837 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2838 PDU_TOTAL_TRANSFER_LEN(request));
2839 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2840 // ntohl(bhssr->bhssr_residual_count));
2841 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2842 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2843 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2844 bhssr->bhssr_residual_count =
2845 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2846 ntohl(bhssc->bhssc_expected_data_transfer_length));
2847 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2848 // ntohl(bhssr->bhssr_residual_count));
2850 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2851 bhssr->bhssr_status = io->scsiio.scsi_status;
2852 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2853 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2855 if (io->scsiio.sense_len > 0) {
2857 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2858 io->scsiio.sense_len);
2860 sense_length = htons(io->scsiio.sense_len);
2861 icl_pdu_append_data(response,
2862 &sense_length, sizeof(sense_length), M_WAITOK);
2863 icl_pdu_append_data(response,
2864 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2868 icl_pdu_free(request);
2869 cfiscsi_pdu_queue(response);
2873 cfiscsi_task_management_done(union ctl_io *io)
2875 struct icl_pdu *request, *response;
2876 struct iscsi_bhs_task_management_request *bhstmr;
2877 struct iscsi_bhs_task_management_response *bhstmr2;
2878 struct cfiscsi_data_wait *cdw, *tmpcdw;
2879 struct cfiscsi_session *cs, *tcs;
2880 struct cfiscsi_softc *softc;
2883 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2884 cs = PDU_SESSION(request);
2885 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2886 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2887 ISCSI_BHS_OPCODE_TASK_REQUEST,
2888 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2891 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2892 bhstmr->bhstmr_initiator_task_tag,
2893 bhstmr->bhstmr_referenced_task_tag);
2896 if ((bhstmr->bhstmr_function & ~0x80) ==
2897 BHSTMR_FUNCTION_ABORT_TASK) {
2899 * Make sure we no longer wait for Data-Out for this command.
2901 CFISCSI_SESSION_LOCK(cs);
2902 TAILQ_FOREACH_SAFE(cdw,
2903 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2904 if (bhstmr->bhstmr_referenced_task_tag !=
2905 cdw->cdw_initiator_task_tag)
2909 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2910 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2912 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2914 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
2915 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 43;
2916 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2917 cfiscsi_data_wait_free(cs, cdw);
2919 CFISCSI_SESSION_UNLOCK(cs);
2921 if ((bhstmr->bhstmr_function & ~0x80) ==
2922 BHSTMR_FUNCTION_TARGET_COLD_RESET &&
2923 io->io_hdr.status == CTL_SUCCESS)
2926 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2927 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2929 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2930 bhstmr2->bhstmr_flags = 0x80;
2931 switch (io->taskio.task_status) {
2932 case CTL_TASK_FUNCTION_COMPLETE:
2933 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2935 case CTL_TASK_FUNCTION_SUCCEEDED:
2936 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED;
2938 case CTL_TASK_LUN_DOES_NOT_EXIST:
2939 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST;
2941 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
2943 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2946 memcpy(bhstmr2->bhstmr_additional_reponse_information,
2947 io->taskio.task_resp, sizeof(io->taskio.task_resp));
2948 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2951 icl_pdu_free(request);
2952 cfiscsi_pdu_queue(response);
2955 softc = cs->cs_target->ct_softc;
2956 mtx_lock(&softc->lock);
2957 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) {
2958 if (tcs->cs_target == cs->cs_target)
2959 cfiscsi_session_terminate(tcs);
2961 mtx_unlock(&softc->lock);
2966 cfiscsi_done(union ctl_io *io)
2968 struct icl_pdu *request;
2969 struct cfiscsi_session *cs;
2971 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2972 ("invalid CTL status %#x", io->io_hdr.status));
2974 if (io->io_hdr.io_type == CTL_IO_TASK &&
2975 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2977 * Implicit task termination has just completed; nothing to do.
2979 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2980 cs->cs_tasks_aborted = true;
2981 refcount_release(&cs->cs_outstanding_ctl_pdus);
2982 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2987 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2988 cs = PDU_SESSION(request);
2990 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2991 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2992 cfiscsi_scsi_command_done(io);
2994 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2995 cfiscsi_task_management_done(io);
2998 panic("cfiscsi_done called with wrong opcode 0x%x",
2999 request->ip_bhs->bhs_opcode);
3002 refcount_release(&cs->cs_outstanding_ctl_pdus);