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>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_da.h>
65 #include <cam/ctl/ctl_io.h>
66 #include <cam/ctl/ctl.h>
67 #include <cam/ctl/ctl_backend.h>
68 #include <cam/ctl/ctl_error.h>
69 #include <cam/ctl/ctl_frontend.h>
70 #include <cam/ctl/ctl_debug.h>
71 #include <cam/ctl/ctl_ha.h>
72 #include <cam/ctl/ctl_ioctl.h>
73 #include <cam/ctl/ctl_private.h>
75 #include <dev/iscsi/icl.h>
76 #include <dev/iscsi/icl_wrappers.h>
77 #include <dev/iscsi/iscsi_proto.h>
78 #include <cam/ctl/ctl_frontend_iscsi.h>
80 #ifdef ICL_KERNEL_PROXY
81 #include <sys/socketvar.h>
84 #ifdef ICL_KERNEL_PROXY
85 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
88 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
89 static uma_zone_t cfiscsi_data_wait_zone;
91 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
92 "CAM Target Layer iSCSI Frontend");
94 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
95 &debug, 1, "Enable debug messages");
96 static int ping_timeout = 5;
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
98 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
99 static int login_timeout = 60;
100 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
101 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
102 static int maxtags = 256;
103 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN,
104 &maxtags, 0, "Max number of requests queued by initiator");
106 #define CFISCSI_DEBUG(X, ...) \
109 printf("%s: " X "\n", \
110 __func__, ## __VA_ARGS__); \
114 #define CFISCSI_WARN(X, ...) \
117 printf("WARNING: %s: " X "\n", \
118 __func__, ## __VA_ARGS__); \
122 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
125 printf("%s: %s (%s): " X "\n", \
126 __func__, S->cs_initiator_addr, \
127 S->cs_initiator_name, ## __VA_ARGS__); \
131 #define CFISCSI_SESSION_WARN(S, X, ...) \
134 printf("WARNING: %s (%s): " X "\n", \
135 S->cs_initiator_addr, \
136 S->cs_initiator_name, ## __VA_ARGS__); \
140 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
141 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
142 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
144 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
145 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
146 #define PDU_EXPDATASN(X) (X)->ip_prv0
147 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
148 #define PDU_R2TSN(X) (X)->ip_prv2
150 static int cfiscsi_init(void);
151 static int cfiscsi_shutdown(void);
152 static void cfiscsi_online(void *arg);
153 static void cfiscsi_offline(void *arg);
154 static int cfiscsi_info(void *arg, struct sbuf *sb);
155 static int cfiscsi_ioctl(struct cdev *dev,
156 u_long cmd, caddr_t addr, int flag, struct thread *td);
157 static void cfiscsi_datamove(union ctl_io *io);
158 static void cfiscsi_datamove_in(union ctl_io *io);
159 static void cfiscsi_datamove_out(union ctl_io *io);
160 static void cfiscsi_done(union ctl_io *io);
161 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
162 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
163 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
164 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
165 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
166 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
167 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
168 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
169 struct cfiscsi_session *cs, union ctl_io *io,
170 uint32_t initiator_task_tag,
171 uint32_t *target_transfer_tagp);
172 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
173 struct cfiscsi_data_wait *cdw);
174 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
175 *softc, const char *name, uint16_t tag);
176 static struct cfiscsi_target *cfiscsi_target_find_or_create(
177 struct cfiscsi_softc *softc, const char *name, const char *alias,
179 static void cfiscsi_target_release(struct cfiscsi_target *ct);
180 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
182 static struct cfiscsi_softc cfiscsi_softc;
184 static struct ctl_frontend cfiscsi_frontend =
187 .init = cfiscsi_init,
188 .ioctl = cfiscsi_ioctl,
189 .shutdown = cfiscsi_shutdown,
191 CTL_FRONTEND_DECLARE(cfiscsi, cfiscsi_frontend);
192 MODULE_DEPEND(cfiscsi, icl, 1, 1, 1);
194 static struct icl_pdu *
195 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
198 return (icl_pdu_new(request->ip_conn, flags));
202 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
204 const struct iscsi_bhs_scsi_command *bhssc;
205 struct cfiscsi_session *cs;
206 uint32_t cmdsn, expstatsn;
208 cs = PDU_SESSION(request);
211 * Every incoming PDU - not just NOP-Out - resets the ping timer.
212 * The purpose of the timeout is to reset the connection when it stalls;
213 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
221 * Data-Out PDUs don't contain CmdSN.
223 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
224 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
228 * We're only using fields common for all the request
229 * (initiator -> target) PDUs.
231 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
232 cmdsn = ntohl(bhssc->bhssc_cmdsn);
233 expstatsn = ntohl(bhssc->bhssc_expstatsn);
235 CFISCSI_SESSION_LOCK(cs);
237 if (expstatsn != cs->cs_statsn) {
238 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
239 "while current StatSN is %d", expstatsn,
244 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
246 * The target MUST silently ignore any non-immediate command
247 * outside of this range.
249 if (ISCSI_SNLT(cmdsn, cs->cs_cmdsn) ||
250 ISCSI_SNGT(cmdsn, cs->cs_cmdsn - 1 + maxtags)) {
251 CFISCSI_SESSION_UNLOCK(cs);
252 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
253 "while expected %u", cmdsn, cs->cs_cmdsn);
258 * We don't support multiple connections now, so any
259 * discontinuity in CmdSN means lost PDUs. Since we don't
260 * support PDU retransmission -- terminate the connection.
262 if (cmdsn != cs->cs_cmdsn) {
263 CFISCSI_SESSION_UNLOCK(cs);
264 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
265 "while expected %u; dropping connection",
266 cmdsn, cs->cs_cmdsn);
267 cfiscsi_session_terminate(cs);
273 CFISCSI_SESSION_UNLOCK(cs);
279 cfiscsi_pdu_handle(struct icl_pdu *request)
281 struct cfiscsi_session *cs;
284 cs = PDU_SESSION(request);
286 ignore = cfiscsi_pdu_update_cmdsn(request);
288 icl_pdu_free(request);
293 * Handle the PDU; this includes e.g. receiving the remaining
294 * part of PDU and submitting the SCSI command to CTL
295 * or queueing a reply. The handling routine is responsible
296 * for freeing the PDU when it's no longer needed.
298 switch (request->ip_bhs->bhs_opcode &
299 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
300 case ISCSI_BHS_OPCODE_NOP_OUT:
301 cfiscsi_pdu_handle_nop_out(request);
303 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
304 cfiscsi_pdu_handle_scsi_command(request);
306 case ISCSI_BHS_OPCODE_TASK_REQUEST:
307 cfiscsi_pdu_handle_task_request(request);
309 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
310 cfiscsi_pdu_handle_data_out(request);
312 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
313 cfiscsi_pdu_handle_logout_request(request);
316 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
317 "opcode 0x%x; dropping connection",
318 request->ip_bhs->bhs_opcode);
319 icl_pdu_free(request);
320 cfiscsi_session_terminate(cs);
326 cfiscsi_receive_callback(struct icl_pdu *request)
328 #ifdef ICL_KERNEL_PROXY
329 struct cfiscsi_session *cs;
331 cs = PDU_SESSION(request);
332 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
333 if (cs->cs_login_pdu == NULL)
334 cs->cs_login_pdu = request;
336 icl_pdu_free(request);
337 cv_signal(&cs->cs_login_cv);
342 cfiscsi_pdu_handle(request);
346 cfiscsi_error_callback(struct icl_conn *ic)
348 struct cfiscsi_session *cs;
350 cs = CONN_SESSION(ic);
352 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
353 cfiscsi_session_terminate(cs);
357 cfiscsi_pdu_prepare(struct icl_pdu *response)
359 struct cfiscsi_session *cs;
360 struct iscsi_bhs_scsi_response *bhssr;
361 bool advance_statsn = true;
363 cs = PDU_SESSION(response);
365 CFISCSI_SESSION_LOCK_ASSERT(cs);
368 * We're only using fields common for all the response
369 * (target -> initiator) PDUs.
371 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
374 * 10.8.3: "The StatSN for this connection is not advanced
375 * after this PDU is sent."
377 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
378 advance_statsn = false;
381 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
382 * StatSN for the connection is not advanced after this PDU is sent."
384 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
385 bhssr->bhssr_initiator_task_tag == 0xffffffff)
386 advance_statsn = false;
389 * See the comment below - StatSN is not meaningful and must
392 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
393 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
394 advance_statsn = false;
397 * 10.7.3: "The fields StatSN, Status, and Residual Count
398 * only have meaningful content if the S bit is set to 1."
400 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
401 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
402 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
403 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
404 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn - 1 +
405 imax(0, maxtags - cs->cs_outstanding_ctl_pdus));
414 cfiscsi_pdu_queue(struct icl_pdu *response)
416 struct cfiscsi_session *cs;
418 cs = PDU_SESSION(response);
420 CFISCSI_SESSION_LOCK(cs);
421 cfiscsi_pdu_prepare(response);
422 icl_pdu_queue(response);
423 CFISCSI_SESSION_UNLOCK(cs);
427 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
429 struct cfiscsi_session *cs;
430 struct iscsi_bhs_nop_out *bhsno;
431 struct iscsi_bhs_nop_in *bhsni;
432 struct icl_pdu *response;
437 cs = PDU_SESSION(request);
438 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
440 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
442 * Nothing to do, iscsi_pdu_update_statsn() already
443 * zeroed the timeout.
445 icl_pdu_free(request);
449 datasize = icl_pdu_data_segment_length(request);
451 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
453 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
454 "dropping connection");
455 icl_pdu_free(request);
456 cfiscsi_session_terminate(cs);
459 icl_pdu_get_data(request, 0, data, datasize);
462 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
463 if (response == NULL) {
464 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
465 "droppping connection");
466 free(data, M_CFISCSI);
467 icl_pdu_free(request);
468 cfiscsi_session_terminate(cs);
471 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
472 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
473 bhsni->bhsni_flags = 0x80;
474 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
475 bhsni->bhsni_target_transfer_tag = 0xffffffff;
477 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
479 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
480 "dropping connection");
481 free(data, M_CFISCSI);
482 icl_pdu_free(request);
483 icl_pdu_free(response);
484 cfiscsi_session_terminate(cs);
487 free(data, M_CFISCSI);
490 icl_pdu_free(request);
491 cfiscsi_pdu_queue(response);
495 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
497 struct iscsi_bhs_scsi_command *bhssc;
498 struct cfiscsi_session *cs;
502 cs = PDU_SESSION(request);
503 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
504 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
505 // bhssc->bhssc_initiator_task_tag);
507 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
508 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
509 "ImmediateData=No; dropping connection");
510 icl_pdu_free(request);
511 cfiscsi_session_terminate(cs);
514 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
516 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
517 io->io_hdr.io_type = CTL_IO_SCSI;
518 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
519 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
520 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhssc->bhssc_lun));
521 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
522 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
523 case BHSSC_FLAGS_ATTR_UNTAGGED:
524 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
526 case BHSSC_FLAGS_ATTR_SIMPLE:
527 io->scsiio.tag_type = CTL_TAG_SIMPLE;
529 case BHSSC_FLAGS_ATTR_ORDERED:
530 io->scsiio.tag_type = CTL_TAG_ORDERED;
532 case BHSSC_FLAGS_ATTR_HOQ:
533 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
535 case BHSSC_FLAGS_ATTR_ACA:
536 io->scsiio.tag_type = CTL_TAG_ACA;
539 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
540 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
541 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
544 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
545 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
546 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
547 error = ctl_queue(io);
548 if (error != CTL_RETVAL_COMPLETE) {
549 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
550 "dropping connection", error);
552 refcount_release(&cs->cs_outstanding_ctl_pdus);
553 icl_pdu_free(request);
554 cfiscsi_session_terminate(cs);
559 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
561 struct iscsi_bhs_task_management_request *bhstmr;
562 struct iscsi_bhs_task_management_response *bhstmr2;
563 struct icl_pdu *response;
564 struct cfiscsi_session *cs;
568 cs = PDU_SESSION(request);
569 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
570 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
572 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
573 io->io_hdr.io_type = CTL_IO_TASK;
574 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
575 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
576 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhstmr->bhstmr_lun));
577 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
579 switch (bhstmr->bhstmr_function & ~0x80) {
580 case BHSTMR_FUNCTION_ABORT_TASK:
582 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
584 io->taskio.task_action = CTL_TASK_ABORT_TASK;
585 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
587 case BHSTMR_FUNCTION_ABORT_TASK_SET:
589 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
591 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
593 case BHSTMR_FUNCTION_CLEAR_TASK_SET:
595 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_CLEAR_TASK_SET");
597 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
599 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
601 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
603 io->taskio.task_action = CTL_TASK_LUN_RESET;
605 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
607 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
609 io->taskio.task_action = CTL_TASK_TARGET_RESET;
611 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
613 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
615 io->taskio.task_action = CTL_TASK_TARGET_RESET;
617 case BHSTMR_FUNCTION_QUERY_TASK:
619 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK");
621 io->taskio.task_action = CTL_TASK_QUERY_TASK;
622 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
624 case BHSTMR_FUNCTION_QUERY_TASK_SET:
626 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK_SET");
628 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
630 case BHSTMR_FUNCTION_I_T_NEXUS_RESET:
632 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_I_T_NEXUS_RESET");
634 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
636 case BHSTMR_FUNCTION_QUERY_ASYNC_EVENT:
638 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_ASYNC_EVENT");
640 io->taskio.task_action = CTL_TASK_QUERY_ASYNC_EVENT;
643 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
644 bhstmr->bhstmr_function & ~0x80);
647 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
648 if (response == NULL) {
649 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
650 "dropping connection");
651 icl_pdu_free(request);
652 cfiscsi_session_terminate(cs);
655 bhstmr2 = (struct iscsi_bhs_task_management_response *)
657 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
658 bhstmr2->bhstmr_flags = 0x80;
659 bhstmr2->bhstmr_response =
660 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
661 bhstmr2->bhstmr_initiator_task_tag =
662 bhstmr->bhstmr_initiator_task_tag;
663 icl_pdu_free(request);
664 cfiscsi_pdu_queue(response);
668 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
669 error = ctl_queue(io);
670 if (error != CTL_RETVAL_COMPLETE) {
671 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
672 "dropping connection", error);
674 refcount_release(&cs->cs_outstanding_ctl_pdus);
675 icl_pdu_free(request);
676 cfiscsi_session_terminate(cs);
681 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
683 struct iscsi_bhs_data_out *bhsdo;
684 struct cfiscsi_session *cs;
685 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
686 size_t copy_len, len, off, buffer_offset;
690 cs = PDU_SESSION(request);
692 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
693 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
694 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
695 ISCSI_BHS_OPCODE_SCSI_COMMAND,
696 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
699 * We're only using fields common for Data-Out and SCSI Command PDUs.
701 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
703 io = cdw->cdw_ctl_io;
704 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
705 ("CTL_FLAG_DATA_IN"));
708 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
709 request->ip_data_len, io->scsiio.kern_total_len);
712 if (io->scsiio.kern_sg_entries > 0) {
713 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
714 ctl_sg_count = io->scsiio.kern_sg_entries;
716 ctl_sglist = &ctl_sg_entry;
717 ctl_sglist->addr = io->scsiio.kern_data_ptr;
718 ctl_sglist->len = io->scsiio.kern_data_len;
722 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
723 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
724 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
727 len = icl_pdu_data_segment_length(request);
730 * Make sure the offset, as sent by the initiator, matches the offset
731 * we're supposed to be at in the scatter-gather list.
734 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
735 buffer_offset + len <=
736 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
737 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
738 "expected %zd; dropping connection", buffer_offset,
739 (size_t)io->scsiio.kern_rel_offset +
740 (size_t)io->scsiio.ext_data_filled);
741 ctl_set_data_phase_error(&io->scsiio);
742 cfiscsi_session_terminate(cs);
747 * This is the offset within the PDU data segment, as opposed
748 * to buffer_offset, which is the offset within the task (SCSI
751 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
755 * Iterate over the scatter/gather segments, filling them with data
756 * from the PDU data segment. Note that this can get called multiple
757 * times for one SCSI command; the cdw structure holds state for the
758 * scatter/gather list.
761 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
762 ("cdw->cdw_sg_index >= ctl_sg_count"));
763 if (cdw->cdw_sg_len == 0) {
764 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
765 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
767 KASSERT(off <= len, ("len > off"));
768 copy_len = len - off;
769 if (copy_len > cdw->cdw_sg_len)
770 copy_len = cdw->cdw_sg_len;
772 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
773 cdw->cdw_sg_addr += copy_len;
774 cdw->cdw_sg_len -= copy_len;
776 io->scsiio.ext_data_filled += copy_len;
777 io->scsiio.kern_data_resid -= copy_len;
779 if (cdw->cdw_sg_len == 0) {
781 * End of current segment.
783 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
785 * Last segment in scatter/gather list.
794 * End of PDU payload.
802 * In case of unsolicited data, it's possible that the buffer
803 * provided by CTL is smaller than negotiated FirstBurstLength.
804 * Just ignore the superfluous data; will ask for them with R2T
805 * on next call to cfiscsi_datamove().
807 * This obviously can only happen with SCSI Command PDU.
809 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
810 ISCSI_BHS_OPCODE_SCSI_COMMAND)
813 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
814 "expected %zd; dropping connection",
815 icl_pdu_data_segment_length(request), off);
816 ctl_set_data_phase_error(&io->scsiio);
817 cfiscsi_session_terminate(cs);
821 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
822 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
823 CFISCSI_SESSION_WARN(cs, "got the final packet without "
824 "the F flag; flags = 0x%x; dropping connection",
826 ctl_set_data_phase_error(&io->scsiio);
827 cfiscsi_session_terminate(cs);
831 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
832 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
833 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
834 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
835 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
836 "transmitted size was %zd bytes instead of %d; "
837 "dropping connection",
838 (size_t)io->scsiio.ext_data_filled,
840 ctl_set_data_phase_error(&io->scsiio);
841 cfiscsi_session_terminate(cs);
845 * For SCSI Command PDU, this just means we need to
846 * solicit more data by sending R2T.
852 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
854 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
855 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
865 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
867 struct iscsi_bhs_data_out *bhsdo;
868 struct cfiscsi_session *cs;
869 struct cfiscsi_data_wait *cdw = NULL;
873 cs = PDU_SESSION(request);
874 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
876 CFISCSI_SESSION_LOCK(cs);
877 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
879 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
880 "ttt 0x%x, itt 0x%x",
881 bhsdo->bhsdo_target_transfer_tag,
882 bhsdo->bhsdo_initiator_task_tag,
883 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
885 if (bhsdo->bhsdo_target_transfer_tag ==
886 cdw->cdw_target_transfer_tag)
889 CFISCSI_SESSION_UNLOCK(cs);
891 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
892 "0x%x, not found; dropping connection",
893 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
894 icl_pdu_free(request);
895 cfiscsi_session_terminate(cs);
899 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
900 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
901 "DataSN %u, while expected %u; dropping connection",
902 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
903 icl_pdu_free(request);
904 cfiscsi_session_terminate(cs);
909 io = cdw->cdw_ctl_io;
910 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
911 ("CTL_FLAG_DATA_IN"));
913 done = cfiscsi_handle_data_segment(request, cdw);
915 CFISCSI_SESSION_LOCK(cs);
916 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
917 CFISCSI_SESSION_UNLOCK(cs);
918 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
919 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
920 cfiscsi_data_wait_free(cs, cdw);
921 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
923 io->scsiio.be_move_done(io);
925 cfiscsi_datamove_out(io);
928 icl_pdu_free(request);
932 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
934 struct iscsi_bhs_logout_request *bhslr;
935 struct iscsi_bhs_logout_response *bhslr2;
936 struct icl_pdu *response;
937 struct cfiscsi_session *cs;
939 cs = PDU_SESSION(request);
940 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
941 switch (bhslr->bhslr_reason & 0x7f) {
942 case BHSLR_REASON_CLOSE_SESSION:
943 case BHSLR_REASON_CLOSE_CONNECTION:
944 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
945 if (response == NULL) {
946 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
947 icl_pdu_free(request);
948 cfiscsi_session_terminate(cs);
951 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
952 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
953 bhslr2->bhslr_flags = 0x80;
954 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
955 bhslr2->bhslr_initiator_task_tag =
956 bhslr->bhslr_initiator_task_tag;
957 icl_pdu_free(request);
958 cfiscsi_pdu_queue(response);
959 cfiscsi_session_terminate(cs);
961 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
962 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
963 if (response == NULL) {
964 CFISCSI_SESSION_WARN(cs,
965 "failed to allocate memory; dropping connection");
966 icl_pdu_free(request);
967 cfiscsi_session_terminate(cs);
970 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
971 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
972 bhslr2->bhslr_flags = 0x80;
973 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
974 bhslr2->bhslr_initiator_task_tag =
975 bhslr->bhslr_initiator_task_tag;
976 icl_pdu_free(request);
977 cfiscsi_pdu_queue(response);
980 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
981 bhslr->bhslr_reason);
982 icl_pdu_free(request);
983 cfiscsi_session_terminate(cs);
989 cfiscsi_callout(void *context)
992 struct iscsi_bhs_nop_in *bhsni;
993 struct cfiscsi_session *cs;
997 if (cs->cs_terminating)
1000 callout_schedule(&cs->cs_callout, 1 * hz);
1002 atomic_add_int(&cs->cs_timeout, 1);
1004 #ifdef ICL_KERNEL_PROXY
1005 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1006 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1007 CFISCSI_SESSION_WARN(cs, "login timed out after "
1008 "%d seconds; dropping connection", cs->cs_timeout);
1009 cfiscsi_session_terminate(cs);
1015 if (ping_timeout <= 0) {
1017 * Pings are disabled. Don't send NOP-In in this case;
1018 * user might have disabled pings to work around problems
1019 * with certain initiators that can't properly handle
1020 * NOP-In, such as iPXE. Reset the timeout, to avoid
1021 * triggering reconnection, should the user decide to
1028 if (cs->cs_timeout >= ping_timeout) {
1029 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1030 "dropping connection", ping_timeout);
1031 cfiscsi_session_terminate(cs);
1036 * If the ping was reset less than one second ago - which means
1037 * that we've received some PDU during the last second - assume
1038 * the traffic flows correctly and don't bother sending a NOP-Out.
1040 * (It's 2 - one for one second, and one for incrementing is_timeout
1041 * earlier in this routine.)
1043 if (cs->cs_timeout < 2)
1046 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1048 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1051 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1052 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1053 bhsni->bhsni_flags = 0x80;
1054 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1056 cfiscsi_pdu_queue(cp);
1059 static struct cfiscsi_data_wait *
1060 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1061 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1063 struct cfiscsi_data_wait *cdw;
1066 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1068 CFISCSI_SESSION_WARN(cs,
1069 "failed to allocate %zd bytes", sizeof(*cdw));
1073 error = icl_conn_transfer_setup(cs->cs_conn, io, target_transfer_tagp,
1076 CFISCSI_SESSION_WARN(cs,
1077 "icl_conn_transfer_setup() failed with error %d", error);
1078 uma_zfree(cfiscsi_data_wait_zone, cdw);
1082 cdw->cdw_ctl_io = io;
1083 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1084 cdw->cdw_initiator_task_tag = initiator_task_tag;
1090 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1091 struct cfiscsi_data_wait *cdw)
1094 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1095 uma_zfree(cfiscsi_data_wait_zone, cdw);
1099 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1101 struct cfiscsi_data_wait *cdw;
1103 int error, last, wait;
1105 if (cs->cs_target == NULL)
1106 return; /* No target yet, so nothing to do. */
1107 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1109 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1110 io->io_hdr.io_type = CTL_IO_TASK;
1111 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1112 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1113 io->io_hdr.nexus.targ_lun = 0;
1114 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1115 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1116 wait = cs->cs_outstanding_ctl_pdus;
1117 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1118 error = ctl_queue(io);
1119 if (error != CTL_RETVAL_COMPLETE) {
1120 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1121 refcount_release(&cs->cs_outstanding_ctl_pdus);
1125 CFISCSI_SESSION_LOCK(cs);
1126 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1127 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1128 CFISCSI_SESSION_UNLOCK(cs);
1130 * Set nonzero port status; this prevents backends from
1131 * assuming that the data transfer actually succeeded
1132 * and writing uninitialized data to disk.
1134 cdw->cdw_ctl_io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1135 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1136 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1137 cfiscsi_data_wait_free(cs, cdw);
1138 CFISCSI_SESSION_LOCK(cs);
1140 CFISCSI_SESSION_UNLOCK(cs);
1143 * Wait for CTL to terminate all the tasks.
1146 CFISCSI_SESSION_WARN(cs,
1147 "waiting for CTL to terminate %d tasks", wait);
1149 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1150 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1153 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1154 0, "cfiscsi_terminate", hz / 100);
1157 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1161 cfiscsi_maintenance_thread(void *arg)
1163 struct cfiscsi_session *cs;
1168 CFISCSI_SESSION_LOCK(cs);
1169 if (cs->cs_terminating == false || cs->cs_handoff_in_progress)
1170 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1171 CFISCSI_SESSION_UNLOCK(cs);
1173 if (cs->cs_terminating && cs->cs_handoff_in_progress == false) {
1176 * We used to wait up to 30 seconds to deliver queued
1177 * PDUs to the initiator. We also tried hard to deliver
1178 * SCSI Responses for the aborted PDUs. We don't do
1179 * that anymore. We might need to revisit that.
1181 callout_drain(&cs->cs_callout);
1182 icl_conn_close(cs->cs_conn);
1185 * At this point ICL receive thread is no longer
1186 * running; no new tasks can be queued.
1188 cfiscsi_session_terminate_tasks(cs);
1189 cfiscsi_session_delete(cs);
1193 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1198 cfiscsi_session_terminate(struct cfiscsi_session *cs)
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");
1272 * The purpose of this is to avoid racing with session shutdown.
1273 * Otherwise we could have the maintenance thread call icl_conn_close()
1274 * before we call icl_conn_handoff().
1276 cs->cs_handoff_in_progress = true;
1278 cs->cs_conn = icl_new_conn(offload, false, "cfiscsi", &cs->cs_lock);
1279 if (cs->cs_conn == NULL) {
1280 free(cs, M_CFISCSI);
1283 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1284 cs->cs_conn->ic_error = cfiscsi_error_callback;
1285 cs->cs_conn->ic_prv0 = cs;
1287 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1289 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1290 free(cs, M_CFISCSI);
1294 mtx_lock(&softc->lock);
1295 cs->cs_id = ++softc->last_session_id;
1296 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1297 mtx_unlock(&softc->lock);
1300 * Start pinging the initiator.
1302 callout_init(&cs->cs_callout, 1);
1303 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1309 cfiscsi_session_delete(struct cfiscsi_session *cs)
1311 struct cfiscsi_softc *softc;
1313 softc = &cfiscsi_softc;
1315 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1316 ("destroying session with outstanding CTL pdus"));
1317 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1318 ("destroying session with non-empty queue"));
1320 mtx_lock(&softc->lock);
1321 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1322 mtx_unlock(&softc->lock);
1324 cfiscsi_session_unregister_initiator(cs);
1325 if (cs->cs_target != NULL)
1326 cfiscsi_target_release(cs->cs_target);
1327 icl_conn_close(cs->cs_conn);
1328 icl_conn_free(cs->cs_conn);
1329 free(cs, M_CFISCSI);
1330 cv_signal(&softc->sessions_cv);
1336 struct cfiscsi_softc *softc;
1338 softc = &cfiscsi_softc;
1339 bzero(softc, sizeof(*softc));
1340 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1342 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1343 #ifdef ICL_KERNEL_PROXY
1344 cv_init(&softc->accept_cv, "cfiscsi_accept");
1346 TAILQ_INIT(&softc->sessions);
1347 TAILQ_INIT(&softc->targets);
1349 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1350 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1357 cfiscsi_shutdown(void)
1359 struct cfiscsi_softc *softc = &cfiscsi_softc;
1361 if (!TAILQ_EMPTY(&softc->sessions) || !TAILQ_EMPTY(&softc->targets))
1364 uma_zdestroy(cfiscsi_data_wait_zone);
1365 #ifdef ICL_KERNEL_PROXY
1366 cv_destroy(&softc->accept_cv);
1368 cv_destroy(&softc->sessions_cv);
1369 mtx_destroy(&softc->lock);
1373 #ifdef ICL_KERNEL_PROXY
1375 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1377 struct cfiscsi_session *cs;
1379 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1381 CFISCSI_WARN("failed to create session");
1385 icl_conn_handoff_sock(cs->cs_conn, so);
1386 cs->cs_initiator_sa = sa;
1387 cs->cs_portal_id = portal_id;
1388 cs->cs_handoff_in_progress = false;
1389 cs->cs_waiting_for_ctld = true;
1390 cv_signal(&cfiscsi_softc.accept_cv);
1392 CFISCSI_SESSION_LOCK(cs);
1394 * Wake up the maintenance thread if we got scheduled for termination
1395 * somewhere between cfiscsi_session_new() and icl_conn_handoff_sock().
1397 if (cs->cs_terminating)
1398 cfiscsi_session_terminate(cs);
1399 CFISCSI_SESSION_UNLOCK(cs);
1404 cfiscsi_online(void *arg)
1406 struct cfiscsi_softc *softc;
1407 struct cfiscsi_target *ct;
1410 ct = (struct cfiscsi_target *)arg;
1411 softc = ct->ct_softc;
1413 mtx_lock(&softc->lock);
1414 if (ct->ct_online) {
1415 mtx_unlock(&softc->lock);
1419 online = softc->online++;
1420 mtx_unlock(&softc->lock);
1424 #ifdef ICL_KERNEL_PROXY
1425 if (softc->listener != NULL)
1426 icl_listen_free(softc->listener);
1427 softc->listener = icl_listen_new(cfiscsi_accept);
1432 cfiscsi_offline(void *arg)
1434 struct cfiscsi_softc *softc;
1435 struct cfiscsi_target *ct;
1436 struct cfiscsi_session *cs;
1439 ct = (struct cfiscsi_target *)arg;
1440 softc = ct->ct_softc;
1442 mtx_lock(&softc->lock);
1443 if (!ct->ct_online) {
1444 mtx_unlock(&softc->lock);
1448 online = --softc->online;
1450 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1451 if (cs->cs_target == ct)
1452 cfiscsi_session_terminate(cs);
1455 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1456 if (cs->cs_target == ct)
1460 cv_wait(&softc->sessions_cv, &softc->lock);
1461 } while (cs != NULL && ct->ct_online == 0);
1462 mtx_unlock(&softc->lock);
1466 #ifdef ICL_KERNEL_PROXY
1467 icl_listen_free(softc->listener);
1468 softc->listener = NULL;
1473 cfiscsi_info(void *arg, struct sbuf *sb)
1475 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1478 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1484 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1486 struct cfiscsi_softc *softc;
1487 struct cfiscsi_session *cs, *cs2;
1488 struct cfiscsi_target *ct;
1489 struct ctl_iscsi_handoff_params *cihp;
1492 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1493 softc = &cfiscsi_softc;
1495 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1496 cihp->initiator_name, cihp->initiator_addr,
1499 ct = cfiscsi_target_find(softc, cihp->target_name,
1500 cihp->portal_group_tag);
1502 ci->status = CTL_ISCSI_ERROR;
1503 snprintf(ci->error_str, sizeof(ci->error_str),
1504 "%s: target not found", __func__);
1508 #ifdef ICL_KERNEL_PROXY
1509 if (cihp->socket > 0 && cihp->connection_id > 0) {
1510 snprintf(ci->error_str, sizeof(ci->error_str),
1511 "both socket and connection_id set");
1512 ci->status = CTL_ISCSI_ERROR;
1513 cfiscsi_target_release(ct);
1516 if (cihp->socket == 0) {
1517 mtx_lock(&cfiscsi_softc.lock);
1518 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1519 if (cs->cs_id == cihp->connection_id)
1523 mtx_unlock(&cfiscsi_softc.lock);
1524 snprintf(ci->error_str, sizeof(ci->error_str),
1525 "connection not found");
1526 ci->status = CTL_ISCSI_ERROR;
1527 cfiscsi_target_release(ct);
1530 mtx_unlock(&cfiscsi_softc.lock);
1533 cs = cfiscsi_session_new(softc, cihp->offload);
1535 ci->status = CTL_ISCSI_ERROR;
1536 snprintf(ci->error_str, sizeof(ci->error_str),
1537 "%s: cfiscsi_session_new failed", __func__);
1538 cfiscsi_target_release(ct);
1541 #ifdef ICL_KERNEL_PROXY
1546 * First PDU of Full Feature phase has the same CmdSN as the last
1547 * PDU from the Login Phase received from the initiator. Thus,
1550 cs->cs_cmdsn = cihp->cmdsn;
1551 cs->cs_statsn = cihp->statsn;
1552 cs->cs_max_recv_data_segment_length = cihp->max_recv_data_segment_length;
1553 cs->cs_max_send_data_segment_length = cihp->max_send_data_segment_length;
1554 cs->cs_max_burst_length = cihp->max_burst_length;
1555 cs->cs_first_burst_length = cihp->first_burst_length;
1556 cs->cs_immediate_data = !!cihp->immediate_data;
1557 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1558 cs->cs_conn->ic_header_crc32c = true;
1559 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1560 cs->cs_conn->ic_data_crc32c = true;
1562 strlcpy(cs->cs_initiator_name,
1563 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1564 strlcpy(cs->cs_initiator_addr,
1565 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1566 strlcpy(cs->cs_initiator_alias,
1567 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1568 memcpy(cs->cs_initiator_isid,
1569 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1570 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1571 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1572 cihp->initiator_isid[0], cihp->initiator_isid[1],
1573 cihp->initiator_isid[2], cihp->initiator_isid[3],
1574 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1576 mtx_lock(&softc->lock);
1577 if (ct->ct_online == 0) {
1578 mtx_unlock(&softc->lock);
1579 cs->cs_handoff_in_progress = false;
1580 cfiscsi_session_terminate(cs);
1581 cfiscsi_target_release(ct);
1582 ci->status = CTL_ISCSI_ERROR;
1583 snprintf(ci->error_str, sizeof(ci->error_str),
1584 "%s: port offline", __func__);
1588 mtx_unlock(&softc->lock);
1591 if (!cs->cs_terminating) {
1592 mtx_lock(&softc->lock);
1593 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1594 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1595 cs->cs_target == cs2->cs_target &&
1596 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1597 if (strcmp(cs->cs_initiator_addr,
1598 cs2->cs_initiator_addr) != 0) {
1599 CFISCSI_SESSION_WARN(cs2,
1600 "session reinstatement from "
1601 "different address %s",
1602 cs->cs_initiator_addr);
1604 CFISCSI_SESSION_DEBUG(cs2,
1605 "session reinstatement");
1607 cfiscsi_session_terminate(cs2);
1608 mtx_unlock(&softc->lock);
1609 pause("cfiscsi_reinstate", 1);
1613 mtx_unlock(&softc->lock);
1617 * Register initiator with CTL.
1619 cfiscsi_session_register_initiator(cs);
1621 #ifdef ICL_KERNEL_PROXY
1622 if (cihp->socket > 0) {
1624 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1626 cs->cs_handoff_in_progress = false;
1627 cfiscsi_session_terminate(cs);
1628 ci->status = CTL_ISCSI_ERROR;
1629 snprintf(ci->error_str, sizeof(ci->error_str),
1630 "%s: icl_conn_handoff failed with error %d",
1634 #ifdef ICL_KERNEL_PROXY
1638 #ifdef ICL_KERNEL_PROXY
1639 cs->cs_login_phase = false;
1642 * First PDU of the Full Feature phase has likely already arrived.
1643 * We have to pick it up and execute properly.
1645 if (cs->cs_login_pdu != NULL) {
1646 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1647 cfiscsi_pdu_handle(cs->cs_login_pdu);
1648 cs->cs_login_pdu = NULL;
1652 CFISCSI_SESSION_LOCK(cs);
1653 cs->cs_handoff_in_progress = false;
1656 * Wake up the maintenance thread if we got scheduled for termination.
1658 if (cs->cs_terminating)
1659 cfiscsi_session_terminate(cs);
1660 CFISCSI_SESSION_UNLOCK(cs);
1662 ci->status = CTL_ISCSI_OK;
1666 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1668 struct ctl_iscsi_list_params *cilp;
1669 struct cfiscsi_session *cs;
1670 struct cfiscsi_softc *softc;
1674 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1675 softc = &cfiscsi_softc;
1677 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1679 ci->status = CTL_ISCSI_ERROR;
1680 snprintf(ci->error_str, sizeof(ci->error_str),
1681 "Unable to allocate %d bytes for iSCSI session list",
1686 sbuf_printf(sb, "<ctlislist>\n");
1687 mtx_lock(&softc->lock);
1688 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1689 #ifdef ICL_KERNEL_PROXY
1690 if (cs->cs_target == NULL)
1693 error = sbuf_printf(sb, "<connection id=\"%d\">"
1694 "<initiator>%s</initiator>"
1695 "<initiator_addr>%s</initiator_addr>"
1696 "<initiator_alias>%s</initiator_alias>"
1697 "<target>%s</target>"
1698 "<target_alias>%s</target_alias>"
1699 "<target_portal_group_tag>%u</target_portal_group_tag>"
1700 "<header_digest>%s</header_digest>"
1701 "<data_digest>%s</data_digest>"
1702 "<max_recv_data_segment_length>%d</max_recv_data_segment_length>"
1703 "<max_send_data_segment_length>%d</max_send_data_segment_length>"
1704 "<max_burst_length>%d</max_burst_length>"
1705 "<first_burst_length>%d</first_burst_length>"
1706 "<immediate_data>%d</immediate_data>"
1708 "<offload>%s</offload>"
1711 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1712 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1713 cs->cs_target->ct_tag,
1714 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1715 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1716 cs->cs_max_recv_data_segment_length,
1717 cs->cs_max_send_data_segment_length,
1718 cs->cs_max_burst_length,
1719 cs->cs_first_burst_length,
1720 cs->cs_immediate_data,
1721 cs->cs_conn->ic_iser,
1722 cs->cs_conn->ic_offload);
1726 mtx_unlock(&softc->lock);
1727 error = sbuf_printf(sb, "</ctlislist>\n");
1730 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1731 snprintf(ci->error_str, sizeof(ci->error_str),
1732 "Out of space, %d bytes is too small", cilp->alloc_len);
1737 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1740 snprintf(ci->error_str, sizeof(ci->error_str),
1741 "copyout failed with error %d", error);
1742 ci->status = CTL_ISCSI_ERROR;
1745 cilp->fill_len = sbuf_len(sb) + 1;
1746 ci->status = CTL_ISCSI_OK;
1751 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1753 struct icl_pdu *response;
1754 struct iscsi_bhs_asynchronous_message *bhsam;
1755 struct ctl_iscsi_logout_params *cilp;
1756 struct cfiscsi_session *cs;
1757 struct cfiscsi_softc *softc;
1760 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1761 softc = &cfiscsi_softc;
1763 mtx_lock(&softc->lock);
1764 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1765 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1766 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1767 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1770 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1771 if (response == NULL) {
1772 ci->status = CTL_ISCSI_ERROR;
1773 snprintf(ci->error_str, sizeof(ci->error_str),
1774 "Unable to allocate memory");
1775 mtx_unlock(&softc->lock);
1779 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1780 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1781 bhsam->bhsam_flags = 0x80;
1782 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1783 bhsam->bhsam_parameter3 = htons(10);
1784 cfiscsi_pdu_queue(response);
1787 mtx_unlock(&softc->lock);
1790 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1791 snprintf(ci->error_str, sizeof(ci->error_str),
1792 "No matching connections found");
1796 ci->status = CTL_ISCSI_OK;
1800 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1802 struct icl_pdu *response;
1803 struct iscsi_bhs_asynchronous_message *bhsam;
1804 struct ctl_iscsi_terminate_params *citp;
1805 struct cfiscsi_session *cs;
1806 struct cfiscsi_softc *softc;
1809 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1810 softc = &cfiscsi_softc;
1812 mtx_lock(&softc->lock);
1813 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1814 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1815 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1816 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1819 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1820 if (response == NULL) {
1822 * Oh well. Just terminate the connection.
1825 bhsam = (struct iscsi_bhs_asynchronous_message *)
1827 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1828 bhsam->bhsam_flags = 0x80;
1829 bhsam->bhsam_0xffffffff = 0xffffffff;
1830 bhsam->bhsam_async_event =
1831 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1832 cfiscsi_pdu_queue(response);
1834 cfiscsi_session_terminate(cs);
1837 mtx_unlock(&softc->lock);
1840 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1841 snprintf(ci->error_str, sizeof(ci->error_str),
1842 "No matching connections found");
1846 ci->status = CTL_ISCSI_OK;
1850 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1852 struct ctl_iscsi_limits_params *cilp;
1853 struct icl_drv_limits idl;
1856 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1858 error = icl_limits(cilp->offload, false, &idl);
1860 ci->status = CTL_ISCSI_ERROR;
1861 snprintf(ci->error_str, sizeof(ci->error_str),
1862 "%s: icl_limits failed with error %d",
1867 cilp->max_recv_data_segment_length =
1868 idl.idl_max_recv_data_segment_length;
1869 cilp->max_send_data_segment_length =
1870 idl.idl_max_send_data_segment_length;
1871 cilp->max_burst_length = idl.idl_max_burst_length;
1872 cilp->first_burst_length = idl.idl_first_burst_length;
1874 ci->status = CTL_ISCSI_OK;
1877 #ifdef ICL_KERNEL_PROXY
1879 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1881 struct ctl_iscsi_listen_params *cilp;
1882 struct sockaddr *sa;
1885 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1887 if (cfiscsi_softc.listener == NULL) {
1888 CFISCSI_DEBUG("no listener");
1889 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1890 ci->status = CTL_ISCSI_ERROR;
1894 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1896 CFISCSI_DEBUG("getsockaddr, error %d", error);
1897 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1898 ci->status = CTL_ISCSI_ERROR;
1902 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1903 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1906 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1907 snprintf(ci->error_str, sizeof(ci->error_str),
1908 "icl_listen_add failed, error %d", error);
1909 ci->status = CTL_ISCSI_ERROR;
1913 ci->status = CTL_ISCSI_OK;
1917 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1919 struct ctl_iscsi_accept_params *ciap;
1920 struct cfiscsi_session *cs;
1923 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1925 mtx_lock(&cfiscsi_softc.lock);
1927 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1928 if (cs->cs_waiting_for_ctld)
1933 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1935 mtx_unlock(&cfiscsi_softc.lock);
1936 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1937 ci->status = CTL_ISCSI_ERROR;
1941 mtx_unlock(&cfiscsi_softc.lock);
1943 cs->cs_waiting_for_ctld = false;
1944 cs->cs_login_phase = true;
1946 ciap->connection_id = cs->cs_id;
1947 ciap->portal_id = cs->cs_portal_id;
1948 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1949 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1950 cs->cs_initiator_sa->sa_len);
1952 snprintf(ci->error_str, sizeof(ci->error_str),
1953 "copyout failed with error %d", error);
1954 ci->status = CTL_ISCSI_ERROR;
1958 ci->status = CTL_ISCSI_OK;
1962 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1964 struct ctl_iscsi_send_params *cisp;
1965 struct cfiscsi_session *cs;
1971 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1973 mtx_lock(&cfiscsi_softc.lock);
1974 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1975 if (cs->cs_id == cisp->connection_id)
1979 mtx_unlock(&cfiscsi_softc.lock);
1980 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1981 ci->status = CTL_ISCSI_ERROR;
1984 mtx_unlock(&cfiscsi_softc.lock);
1987 if (cs->cs_login_phase == false)
1991 if (cs->cs_terminating) {
1992 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1993 ci->status = CTL_ISCSI_ERROR;
1997 datalen = cisp->data_segment_len;
2001 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
2002 if (datalen > 65535) {
2003 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
2004 ci->status = CTL_ISCSI_ERROR;
2008 data = malloc(datalen, M_CFISCSI, M_WAITOK);
2009 error = copyin(cisp->data_segment, data, datalen);
2011 free(data, M_CFISCSI);
2012 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
2013 ci->status = CTL_ISCSI_ERROR;
2018 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
2019 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
2021 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
2022 free(data, M_CFISCSI);
2024 CFISCSI_SESSION_LOCK(cs);
2026 CFISCSI_SESSION_UNLOCK(cs);
2027 ci->status = CTL_ISCSI_OK;
2031 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
2033 struct ctl_iscsi_receive_params *cirp;
2034 struct cfiscsi_session *cs;
2039 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
2041 mtx_lock(&cfiscsi_softc.lock);
2042 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2043 if (cs->cs_id == cirp->connection_id)
2047 mtx_unlock(&cfiscsi_softc.lock);
2048 snprintf(ci->error_str, sizeof(ci->error_str),
2049 "connection not found");
2050 ci->status = CTL_ISCSI_ERROR;
2053 mtx_unlock(&cfiscsi_softc.lock);
2056 if (is->is_login_phase == false)
2060 CFISCSI_SESSION_LOCK(cs);
2061 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2062 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2064 CFISCSI_SESSION_UNLOCK(cs);
2065 snprintf(ci->error_str, sizeof(ci->error_str),
2066 "interrupted by signal");
2067 ci->status = CTL_ISCSI_ERROR;
2072 if (cs->cs_terminating) {
2073 CFISCSI_SESSION_UNLOCK(cs);
2074 snprintf(ci->error_str, sizeof(ci->error_str),
2075 "connection terminating");
2076 ci->status = CTL_ISCSI_ERROR;
2079 ip = cs->cs_login_pdu;
2080 cs->cs_login_pdu = NULL;
2081 CFISCSI_SESSION_UNLOCK(cs);
2083 if (ip->ip_data_len > cirp->data_segment_len) {
2085 snprintf(ci->error_str, sizeof(ci->error_str),
2086 "data segment too big");
2087 ci->status = CTL_ISCSI_ERROR;
2091 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2092 if (ip->ip_data_len > 0) {
2093 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2094 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2095 copyout(data, cirp->data_segment, ip->ip_data_len);
2096 free(data, M_CFISCSI);
2100 ci->status = CTL_ISCSI_OK;
2103 #endif /* !ICL_KERNEL_PROXY */
2106 cfiscsi_ioctl_port_create(struct ctl_req *req)
2108 struct cfiscsi_target *ct;
2109 struct ctl_port *port;
2110 const char *target, *alias, *val;
2111 struct scsi_vpd_id_descriptor *desc;
2112 int retval, len, idlen;
2115 target = dnvlist_get_string(req->args_nvl, "cfiscsi_target", NULL);
2116 alias = dnvlist_get_string(req->args_nvl, "cfiscsi_target_alias", NULL);
2117 val = dnvlist_get_string(req->args_nvl, "cfiscsi_portal_group_tag",
2121 if (target == NULL || val == NULL) {
2122 req->status = CTL_LUN_ERROR;
2123 snprintf(req->error_str, sizeof(req->error_str),
2124 "Missing required argument");
2128 tag = strtoul(val, NULL, 0);
2129 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2131 req->status = CTL_LUN_ERROR;
2132 snprintf(req->error_str, sizeof(req->error_str),
2133 "failed to create target \"%s\"", target);
2136 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2137 req->status = CTL_LUN_ERROR;
2138 snprintf(req->error_str, sizeof(req->error_str),
2139 "target \"%s\" for portal group tag %u already exists",
2141 cfiscsi_target_release(ct);
2144 port = &ct->ct_port;
2146 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2149 port->frontend = &cfiscsi_frontend;
2150 port->port_type = CTL_PORT_ISCSI;
2151 /* XXX KDM what should the real number be here? */
2152 port->num_requested_ctl_io = 4096;
2153 port->port_name = "iscsi";
2154 port->physical_port = (int)tag;
2155 port->virtual_port = ct->ct_target_id;
2156 port->port_online = cfiscsi_online;
2157 port->port_offline = cfiscsi_offline;
2158 port->port_info = cfiscsi_info;
2159 port->onoff_arg = ct;
2160 port->fe_datamove = cfiscsi_datamove;
2161 port->fe_done = cfiscsi_done;
2162 port->targ_port = -1;
2163 port->options = nvlist_clone(req->args_nvl);
2165 /* Generate Port ID. */
2166 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2167 idlen = roundup2(idlen, 4);
2168 len = sizeof(struct scsi_vpd_device_id) + idlen;
2169 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2170 M_CTL, M_WAITOK | M_ZERO);
2171 port->port_devid->len = len;
2172 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2173 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2174 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2175 SVPD_ID_TYPE_SCSI_NAME;
2176 desc->length = idlen;
2177 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2179 /* Generate Target ID. */
2180 idlen = strlen(target) + 1;
2181 idlen = roundup2(idlen, 4);
2182 len = sizeof(struct scsi_vpd_device_id) + idlen;
2183 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2184 M_CTL, M_WAITOK | M_ZERO);
2185 port->target_devid->len = len;
2186 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2187 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2188 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2189 SVPD_ID_TYPE_SCSI_NAME;
2190 desc->length = idlen;
2191 strlcpy(desc->identifier, target, idlen);
2193 retval = ctl_port_register(port);
2195 free(port->port_devid, M_CFISCSI);
2196 free(port->target_devid, M_CFISCSI);
2197 cfiscsi_target_release(ct);
2198 req->status = CTL_LUN_ERROR;
2199 snprintf(req->error_str, sizeof(req->error_str),
2200 "ctl_port_register() failed with error %d", retval);
2204 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2205 req->status = CTL_LUN_OK;
2206 req->result_nvl = nvlist_create(0);
2207 nvlist_add_number(req->result_nvl, "port_id", port->targ_port);
2211 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2213 struct cfiscsi_target *ct;
2214 const char *target, *val;
2217 target = dnvlist_get_string(req->args_nvl, "cfiscsi_target", NULL);
2218 val = dnvlist_get_string(req->args_nvl, "cfiscsi_portal_group_tag",
2221 if (target == NULL || val == NULL) {
2222 req->status = CTL_LUN_ERROR;
2223 snprintf(req->error_str, sizeof(req->error_str),
2224 "Missing required argument");
2228 tag = strtoul(val, NULL, 0);
2229 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2231 req->status = CTL_LUN_ERROR;
2232 snprintf(req->error_str, sizeof(req->error_str),
2233 "can't find target \"%s\"", target);
2236 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2237 req->status = CTL_LUN_ERROR;
2238 snprintf(req->error_str, sizeof(req->error_str),
2239 "target \"%s\" is already dying", target);
2243 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2244 ctl_port_offline(&ct->ct_port);
2245 cfiscsi_target_release(ct);
2246 cfiscsi_target_release(ct);
2247 req->status = CTL_LUN_OK;
2251 cfiscsi_ioctl(struct cdev *dev,
2252 u_long cmd, caddr_t addr, int flag, struct thread *td)
2254 struct ctl_iscsi *ci;
2255 struct ctl_req *req;
2257 if (cmd == CTL_PORT_REQ) {
2258 req = (struct ctl_req *)addr;
2259 switch (req->reqtype) {
2260 case CTL_REQ_CREATE:
2261 cfiscsi_ioctl_port_create(req);
2263 case CTL_REQ_REMOVE:
2264 cfiscsi_ioctl_port_remove(req);
2267 req->status = CTL_LUN_ERROR;
2268 snprintf(req->error_str, sizeof(req->error_str),
2269 "Unsupported request type %d", req->reqtype);
2274 if (cmd != CTL_ISCSI)
2277 ci = (struct ctl_iscsi *)addr;
2279 case CTL_ISCSI_HANDOFF:
2280 cfiscsi_ioctl_handoff(ci);
2282 case CTL_ISCSI_LIST:
2283 cfiscsi_ioctl_list(ci);
2285 case CTL_ISCSI_LOGOUT:
2286 cfiscsi_ioctl_logout(ci);
2288 case CTL_ISCSI_TERMINATE:
2289 cfiscsi_ioctl_terminate(ci);
2291 case CTL_ISCSI_LIMITS:
2292 cfiscsi_ioctl_limits(ci);
2294 #ifdef ICL_KERNEL_PROXY
2295 case CTL_ISCSI_LISTEN:
2296 cfiscsi_ioctl_listen(ci);
2298 case CTL_ISCSI_ACCEPT:
2299 cfiscsi_ioctl_accept(ci);
2301 case CTL_ISCSI_SEND:
2302 cfiscsi_ioctl_send(ci);
2304 case CTL_ISCSI_RECEIVE:
2305 cfiscsi_ioctl_receive(ci);
2308 case CTL_ISCSI_LISTEN:
2309 case CTL_ISCSI_ACCEPT:
2310 case CTL_ISCSI_SEND:
2311 case CTL_ISCSI_RECEIVE:
2312 ci->status = CTL_ISCSI_ERROR;
2313 snprintf(ci->error_str, sizeof(ci->error_str),
2314 "%s: CTL compiled without ICL_KERNEL_PROXY",
2317 #endif /* !ICL_KERNEL_PROXY */
2319 ci->status = CTL_ISCSI_ERROR;
2320 snprintf(ci->error_str, sizeof(ci->error_str),
2321 "%s: invalid iSCSI request type %d", __func__, ci->type);
2329 cfiscsi_target_hold(struct cfiscsi_target *ct)
2332 refcount_acquire(&ct->ct_refcount);
2336 cfiscsi_target_release(struct cfiscsi_target *ct)
2338 struct cfiscsi_softc *softc;
2340 softc = ct->ct_softc;
2341 mtx_lock(&softc->lock);
2342 if (refcount_release(&ct->ct_refcount)) {
2343 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2344 mtx_unlock(&softc->lock);
2345 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2346 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2347 if (ctl_port_deregister(&ct->ct_port) != 0)
2348 printf("%s: ctl_port_deregister() failed\n",
2351 free(ct, M_CFISCSI);
2355 mtx_unlock(&softc->lock);
2358 static struct cfiscsi_target *
2359 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2361 struct cfiscsi_target *ct;
2363 mtx_lock(&softc->lock);
2364 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2365 if (ct->ct_tag != tag ||
2366 strcmp(name, ct->ct_name) != 0 ||
2367 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2369 cfiscsi_target_hold(ct);
2370 mtx_unlock(&softc->lock);
2373 mtx_unlock(&softc->lock);
2378 static struct cfiscsi_target *
2379 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2380 const char *alias, uint16_t tag)
2382 struct cfiscsi_target *ct, *newct;
2384 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2387 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2389 mtx_lock(&softc->lock);
2390 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2391 if (ct->ct_tag != tag ||
2392 strcmp(name, ct->ct_name) != 0 ||
2393 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2395 cfiscsi_target_hold(ct);
2396 mtx_unlock(&softc->lock);
2397 free(newct, M_CFISCSI);
2401 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2403 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2404 newct->ct_tag = tag;
2405 refcount_init(&newct->ct_refcount, 1);
2406 newct->ct_softc = softc;
2407 if (TAILQ_EMPTY(&softc->targets))
2408 softc->last_target_id = 0;
2409 newct->ct_target_id = ++softc->last_target_id;
2410 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2411 mtx_unlock(&softc->lock);
2417 cfiscsi_datamove_in(union ctl_io *io)
2419 struct cfiscsi_session *cs;
2420 struct icl_pdu *request, *response;
2421 const struct iscsi_bhs_scsi_command *bhssc;
2422 struct iscsi_bhs_data_in *bhsdi;
2423 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2424 size_t len, expected_len, sg_len, buffer_offset;
2425 const char *sg_addr;
2426 int ctl_sg_count, error, i;
2428 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2429 cs = PDU_SESSION(request);
2431 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2432 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2433 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2434 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2436 if (io->scsiio.kern_sg_entries > 0) {
2437 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2438 ctl_sg_count = io->scsiio.kern_sg_entries;
2440 ctl_sglist = &ctl_sg_entry;
2441 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2442 ctl_sglist->len = io->scsiio.kern_data_len;
2447 * This is the total amount of data to be transferred within the current
2448 * SCSI command. We need to record it so that we can properly report
2449 * underflow/underflow.
2451 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2454 * This is the offset within the current SCSI command; for the first
2455 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2456 * it will be the sum of lengths of previous ones.
2458 buffer_offset = io->scsiio.kern_rel_offset;
2461 * This is the transfer length expected by the initiator. In theory,
2462 * it could be different from the correct amount of data from the SCSI
2463 * point of view, even if that doesn't make any sense.
2465 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2467 if (expected_len != io->scsiio.kern_total_len) {
2468 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2469 "actual length %zd", expected_len,
2470 (size_t)io->scsiio.kern_total_len);
2474 if (buffer_offset >= expected_len) {
2476 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2477 "already sent the expected len", buffer_offset);
2479 io->scsiio.be_move_done(io);
2489 if (response == NULL) {
2490 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2491 if (response == NULL) {
2492 CFISCSI_SESSION_WARN(cs, "failed to "
2493 "allocate memory; dropping connection");
2494 ctl_set_busy(&io->scsiio);
2495 io->scsiio.be_move_done(io);
2496 cfiscsi_session_terminate(cs);
2499 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2500 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2501 bhsdi->bhsdi_initiator_task_tag =
2502 bhssc->bhssc_initiator_task_tag;
2503 bhsdi->bhsdi_target_transfer_tag = 0xffffffff;
2504 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2505 PDU_EXPDATASN(request)++;
2506 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2509 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2511 sg_addr = ctl_sglist[i].addr;
2512 sg_len = ctl_sglist[i].len;
2513 KASSERT(sg_len > 0, ("sg_len <= 0"));
2519 * Truncate to maximum data segment length.
2521 KASSERT(response->ip_data_len < cs->cs_max_send_data_segment_length,
2522 ("ip_data_len %zd >= max_send_data_segment_length %d",
2523 response->ip_data_len, cs->cs_max_send_data_segment_length));
2524 if (response->ip_data_len + len >
2525 cs->cs_max_send_data_segment_length) {
2526 len = cs->cs_max_send_data_segment_length -
2527 response->ip_data_len;
2528 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2533 * Truncate to expected data transfer length.
2535 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2536 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2537 buffer_offset, response->ip_data_len, expected_len));
2538 if (buffer_offset + response->ip_data_len + len > expected_len) {
2539 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2540 "to expected data transfer length %zd",
2541 buffer_offset + response->ip_data_len + len, expected_len);
2542 len = expected_len - (buffer_offset + response->ip_data_len);
2543 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2547 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2549 CFISCSI_SESSION_WARN(cs, "failed to "
2550 "allocate memory; dropping connection");
2551 icl_pdu_free(response);
2552 ctl_set_busy(&io->scsiio);
2553 io->scsiio.be_move_done(io);
2554 cfiscsi_session_terminate(cs);
2559 io->scsiio.kern_data_resid -= len;
2561 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2562 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2563 buffer_offset, response->ip_data_len, expected_len));
2564 if (buffer_offset + response->ip_data_len == expected_len) {
2566 * Already have the amount of data the initiator wanted.
2573 * End of scatter-gather segment;
2574 * proceed to the next one...
2576 if (i == ctl_sg_count - 1) {
2578 * ... unless this was the last one.
2585 if (response->ip_data_len == cs->cs_max_send_data_segment_length) {
2587 * Can't stuff more data into the current PDU;
2588 * queue it. Note that's not enough to check
2589 * for kern_data_resid == 0 instead; there
2590 * may be several Data-In PDUs for the final
2591 * call to cfiscsi_datamove(), and we want
2592 * to set the F flag only on the last of them.
2594 buffer_offset += response->ip_data_len;
2595 if (buffer_offset == io->scsiio.kern_total_len ||
2596 buffer_offset == expected_len) {
2597 buffer_offset -= response->ip_data_len;
2600 cfiscsi_pdu_queue(response);
2605 if (response != NULL) {
2606 buffer_offset += response->ip_data_len;
2607 if (buffer_offset == io->scsiio.kern_total_len ||
2608 buffer_offset == expected_len) {
2609 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2610 if (io->io_hdr.status == CTL_SUCCESS) {
2611 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2612 if (PDU_TOTAL_TRANSFER_LEN(request) <
2613 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2614 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2615 bhsdi->bhsdi_residual_count =
2616 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2617 PDU_TOTAL_TRANSFER_LEN(request));
2618 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2619 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2620 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2621 bhsdi->bhsdi_residual_count =
2622 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2623 ntohl(bhssc->bhssc_expected_data_transfer_length));
2625 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2626 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2629 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2630 cfiscsi_pdu_queue(response);
2633 io->scsiio.be_move_done(io);
2637 cfiscsi_datamove_out(union ctl_io *io)
2639 struct cfiscsi_session *cs;
2640 struct icl_pdu *request, *response;
2641 const struct iscsi_bhs_scsi_command *bhssc;
2642 struct iscsi_bhs_r2t *bhsr2t;
2643 struct cfiscsi_data_wait *cdw;
2644 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2645 uint32_t expected_len, datamove_len, r2t_off, r2t_len;
2646 uint32_t target_transfer_tag;
2649 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2650 cs = PDU_SESSION(request);
2652 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2653 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2654 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2655 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2658 * We need to record it so that we can properly report
2659 * underflow/underflow.
2661 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2664 * Complete write underflow. Not a single byte to read. Return.
2666 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2667 if (io->scsiio.kern_rel_offset >= expected_len) {
2668 io->scsiio.be_move_done(io);
2671 datamove_len = MIN(io->scsiio.kern_data_len,
2672 expected_len - io->scsiio.kern_rel_offset);
2674 target_transfer_tag =
2675 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2676 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2677 &target_transfer_tag);
2679 CFISCSI_SESSION_WARN(cs, "failed to "
2680 "allocate memory; dropping connection");
2681 ctl_set_busy(&io->scsiio);
2682 io->scsiio.be_move_done(io);
2683 cfiscsi_session_terminate(cs);
2687 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2688 "task tag 0x%x, target transfer tag 0x%x",
2689 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2692 cdw->cdw_ctl_io = io;
2693 cdw->cdw_target_transfer_tag = target_transfer_tag;
2694 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2695 cdw->cdw_r2t_end = datamove_len;
2696 cdw->cdw_datasn = 0;
2698 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2699 if (io->scsiio.kern_sg_entries > 0) {
2700 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2702 ctl_sglist = &ctl_sg_entry;
2703 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2704 ctl_sglist->len = datamove_len;
2706 cdw->cdw_sg_index = 0;
2707 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2708 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2709 r2t_off = io->scsiio.ext_data_filled;
2710 while (r2t_off > 0) {
2711 if (r2t_off >= cdw->cdw_sg_len) {
2712 r2t_off -= cdw->cdw_sg_len;
2713 cdw->cdw_sg_index++;
2714 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2715 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2718 cdw->cdw_sg_addr += r2t_off;
2719 cdw->cdw_sg_len -= r2t_off;
2723 if (cs->cs_immediate_data &&
2724 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2725 icl_pdu_data_segment_length(request)) {
2726 done = cfiscsi_handle_data_segment(request, cdw);
2728 cfiscsi_data_wait_free(cs, cdw);
2729 io->scsiio.be_move_done(io);
2734 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2735 r2t_len = MIN(datamove_len - io->scsiio.ext_data_filled,
2736 cs->cs_max_burst_length);
2737 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2739 CFISCSI_SESSION_LOCK(cs);
2740 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2741 CFISCSI_SESSION_UNLOCK(cs);
2744 * XXX: We should limit the number of outstanding R2T PDUs
2745 * per task to MaxOutstandingR2T.
2747 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2748 if (response == NULL) {
2749 CFISCSI_SESSION_WARN(cs, "failed to "
2750 "allocate memory; dropping connection");
2751 ctl_set_busy(&io->scsiio);
2752 io->scsiio.be_move_done(io);
2753 cfiscsi_session_terminate(cs);
2756 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
2757 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2758 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2759 bhsr2t->bhsr2t_flags = 0x80;
2760 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2761 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2762 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2764 * XXX: Here we assume that cfiscsi_datamove() won't ever
2765 * be running concurrently on several CPUs for a given
2768 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2769 PDU_R2TSN(request)++;
2771 * This is the offset within the current SCSI command;
2772 * i.e. for the first call of datamove(), it will be 0,
2773 * and for subsequent ones it will be the sum of lengths
2776 * The ext_data_filled is to account for unsolicited
2777 * (immediate) data that might have already arrived.
2779 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2781 * This is the total length (sum of S/G lengths) this call
2782 * to cfiscsi_datamove() is supposed to handle, limited by
2785 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2786 cfiscsi_pdu_queue(response);
2790 cfiscsi_datamove(union ctl_io *io)
2793 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2794 cfiscsi_datamove_in(io);
2796 /* We hadn't received anything during this datamove yet. */
2797 io->scsiio.ext_data_filled = 0;
2798 cfiscsi_datamove_out(io);
2803 cfiscsi_scsi_command_done(union ctl_io *io)
2805 struct icl_pdu *request, *response;
2806 struct iscsi_bhs_scsi_command *bhssc;
2807 struct iscsi_bhs_scsi_response *bhssr;
2809 struct cfiscsi_data_wait *cdw;
2811 struct cfiscsi_session *cs;
2812 uint16_t sense_length;
2814 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2815 cs = PDU_SESSION(request);
2816 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2817 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2818 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2819 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2821 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2822 // bhssc->bhssc_initiator_task_tag);
2825 CFISCSI_SESSION_LOCK(cs);
2826 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2827 KASSERT(bhssc->bhssc_initiator_task_tag !=
2828 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2829 CFISCSI_SESSION_UNLOCK(cs);
2833 * Do not return status for aborted commands.
2834 * There are exceptions, but none supported by CTL yet.
2836 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2837 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2838 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2840 icl_pdu_free(request);
2844 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2845 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2846 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2847 bhssr->bhssr_flags = 0x80;
2849 * XXX: We don't deal with bidirectional under/overflows;
2850 * does anything actually support those?
2852 if (PDU_TOTAL_TRANSFER_LEN(request) <
2853 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2854 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2855 bhssr->bhssr_residual_count =
2856 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2857 PDU_TOTAL_TRANSFER_LEN(request));
2858 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2859 // ntohl(bhssr->bhssr_residual_count));
2860 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2861 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2862 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2863 bhssr->bhssr_residual_count =
2864 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2865 ntohl(bhssc->bhssc_expected_data_transfer_length));
2866 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2867 // ntohl(bhssr->bhssr_residual_count));
2869 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2870 bhssr->bhssr_status = io->scsiio.scsi_status;
2871 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2872 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2874 if (io->scsiio.sense_len > 0) {
2876 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2877 io->scsiio.sense_len);
2879 sense_length = htons(io->scsiio.sense_len);
2880 icl_pdu_append_data(response,
2881 &sense_length, sizeof(sense_length), M_WAITOK);
2882 icl_pdu_append_data(response,
2883 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2887 icl_pdu_free(request);
2888 cfiscsi_pdu_queue(response);
2892 cfiscsi_task_management_done(union ctl_io *io)
2894 struct icl_pdu *request, *response;
2895 struct iscsi_bhs_task_management_request *bhstmr;
2896 struct iscsi_bhs_task_management_response *bhstmr2;
2897 struct cfiscsi_data_wait *cdw, *tmpcdw;
2898 struct cfiscsi_session *cs, *tcs;
2899 struct cfiscsi_softc *softc;
2902 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2903 cs = PDU_SESSION(request);
2904 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2905 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2906 ISCSI_BHS_OPCODE_TASK_REQUEST,
2907 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2910 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2911 bhstmr->bhstmr_initiator_task_tag,
2912 bhstmr->bhstmr_referenced_task_tag);
2915 if ((bhstmr->bhstmr_function & ~0x80) ==
2916 BHSTMR_FUNCTION_ABORT_TASK) {
2918 * Make sure we no longer wait for Data-Out for this command.
2920 CFISCSI_SESSION_LOCK(cs);
2921 TAILQ_FOREACH_SAFE(cdw,
2922 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2923 if (bhstmr->bhstmr_referenced_task_tag !=
2924 cdw->cdw_initiator_task_tag)
2928 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2929 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2931 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2933 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
2934 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 43;
2935 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2936 cfiscsi_data_wait_free(cs, cdw);
2938 CFISCSI_SESSION_UNLOCK(cs);
2940 if ((bhstmr->bhstmr_function & ~0x80) ==
2941 BHSTMR_FUNCTION_TARGET_COLD_RESET &&
2942 io->io_hdr.status == CTL_SUCCESS)
2945 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2946 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2948 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2949 bhstmr2->bhstmr_flags = 0x80;
2950 switch (io->taskio.task_status) {
2951 case CTL_TASK_FUNCTION_COMPLETE:
2952 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2954 case CTL_TASK_FUNCTION_SUCCEEDED:
2955 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED;
2957 case CTL_TASK_LUN_DOES_NOT_EXIST:
2958 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST;
2960 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
2962 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2965 memcpy(bhstmr2->bhstmr_additional_reponse_information,
2966 io->taskio.task_resp, sizeof(io->taskio.task_resp));
2967 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2970 icl_pdu_free(request);
2971 cfiscsi_pdu_queue(response);
2974 softc = cs->cs_target->ct_softc;
2975 mtx_lock(&softc->lock);
2976 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) {
2977 if (tcs->cs_target == cs->cs_target)
2978 cfiscsi_session_terminate(tcs);
2980 mtx_unlock(&softc->lock);
2985 cfiscsi_done(union ctl_io *io)
2987 struct icl_pdu *request;
2988 struct cfiscsi_session *cs;
2990 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2991 ("invalid CTL status %#x", io->io_hdr.status));
2993 if (io->io_hdr.io_type == CTL_IO_TASK &&
2994 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2996 * Implicit task termination has just completed; nothing to do.
2998 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2999 cs->cs_tasks_aborted = true;
3000 refcount_release(&cs->cs_outstanding_ctl_pdus);
3001 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
3006 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
3007 cs = PDU_SESSION(request);
3009 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
3010 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
3011 cfiscsi_scsi_command_done(io);
3013 case ISCSI_BHS_OPCODE_TASK_REQUEST:
3014 cfiscsi_task_management_done(io);
3017 panic("cfiscsi_done called with wrong opcode 0x%x",
3018 request->ip_bhs->bhs_opcode);
3021 refcount_release(&cs->cs_outstanding_ctl_pdus);