2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * CTL frontend for the iSCSI protocol.
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/condvar.h>
38 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
52 #include <sys/unistd.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_da.h>
59 #include <cam/ctl/ctl_io.h>
60 #include <cam/ctl/ctl.h>
61 #include <cam/ctl/ctl_backend.h>
62 #include <cam/ctl/ctl_error.h>
63 #include <cam/ctl/ctl_frontend.h>
64 #include <cam/ctl/ctl_debug.h>
65 #include <cam/ctl/ctl_ha.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_private.h>
69 #include <dev/iscsi/icl.h>
70 #include <dev/iscsi/icl_wrappers.h>
71 #include <dev/iscsi/iscsi_proto.h>
72 #include <cam/ctl/ctl_frontend_iscsi.h>
74 #ifdef ICL_KERNEL_PROXY
75 #include <sys/socketvar.h>
78 #ifdef ICL_KERNEL_PROXY
79 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
82 /* Used for internal nexus reset task. */
83 #define ISCSI_BHS_OPCODE_INTERNAL 0x3e
85 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
86 static uma_zone_t cfiscsi_data_wait_zone;
88 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
89 "CAM Target Layer iSCSI Frontend");
91 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
92 &debug, 1, "Enable debug messages");
93 static int ping_timeout = 5;
94 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
95 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
96 static int login_timeout = 60;
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
98 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
99 static int maxtags = 256;
100 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN,
101 &maxtags, 0, "Max number of requests queued by initiator");
103 #define CFISCSI_DEBUG(X, ...) \
106 printf("%s: " X "\n", \
107 __func__, ## __VA_ARGS__); \
111 #define CFISCSI_WARN(X, ...) \
114 printf("WARNING: %s: " X "\n", \
115 __func__, ## __VA_ARGS__); \
119 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
122 printf("%s: %s (%s): " X "\n", \
123 __func__, S->cs_initiator_addr, \
124 S->cs_initiator_name, ## __VA_ARGS__); \
128 #define CFISCSI_SESSION_WARN(S, X, ...) \
131 printf("WARNING: %s (%s): " X "\n", \
132 S->cs_initiator_addr, \
133 S->cs_initiator_name, ## __VA_ARGS__); \
137 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
138 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
139 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
141 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
142 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
144 struct cfiscsi_priv {
150 ((struct cfiscsi_priv *)&(io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND])
151 #define PRIV_REQUEST(io) PRIV(io)->request
152 #define PRIV_EXPDATASN(io) PRIV(io)->expdatasn
153 #define PRIV_R2TSN(io) PRIV(io)->r2tsn
155 static int cfiscsi_init(void);
156 static int cfiscsi_shutdown(void);
157 static void cfiscsi_online(void *arg);
158 static void cfiscsi_offline(void *arg);
159 static int cfiscsi_info(void *arg, struct sbuf *sb);
160 static int cfiscsi_ioctl(struct cdev *dev,
161 u_long cmd, caddr_t addr, int flag, struct thread *td);
162 static void cfiscsi_datamove(union ctl_io *io);
163 static void cfiscsi_datamove_in(union ctl_io *io);
164 static void cfiscsi_datamove_out(union ctl_io *io);
165 static void cfiscsi_done(union ctl_io *io);
166 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
167 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
168 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
169 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
170 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
171 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
172 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
173 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
174 struct cfiscsi_session *cs, union ctl_io *io,
175 uint32_t initiator_task_tag,
176 uint32_t *target_transfer_tagp);
177 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
178 struct cfiscsi_data_wait *cdw);
179 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
180 *softc, const char *name, uint16_t tag);
181 static struct cfiscsi_target *cfiscsi_target_find_or_create(
182 struct cfiscsi_softc *softc, const char *name, const char *alias,
184 static void cfiscsi_target_release(struct cfiscsi_target *ct);
185 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
187 static struct cfiscsi_softc cfiscsi_softc;
189 static struct ctl_frontend cfiscsi_frontend =
192 .init = cfiscsi_init,
193 .ioctl = cfiscsi_ioctl,
194 .shutdown = cfiscsi_shutdown,
196 CTL_FRONTEND_DECLARE(cfiscsi, cfiscsi_frontend);
197 MODULE_DEPEND(cfiscsi, icl, 1, 1, 1);
199 static struct icl_pdu *
200 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
203 return (icl_pdu_new(request->ip_conn, flags));
207 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
209 const struct iscsi_bhs_scsi_command *bhssc;
210 struct cfiscsi_session *cs;
211 uint32_t cmdsn, curcmdsn;
213 cs = PDU_SESSION(request);
216 * Every incoming PDU - not just NOP-Out - resets the ping timer.
217 * The purpose of the timeout is to reset the connection when it stalls;
218 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
224 * Immediate commands carry cmdsn, but it is neither incremented nor
227 if (request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE)
231 * Data-Out PDUs don't contain CmdSN.
233 if (request->ip_bhs->bhs_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
237 * We're only using fields common for all the request
238 * (initiator -> target) PDUs.
240 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
241 curcmdsn = cmdsn = ntohl(bhssc->bhssc_cmdsn);
244 * Increment session cmdsn and exit if we received the expected value.
247 if (atomic_fcmpset_32(&cs->cs_cmdsn, &curcmdsn, cmdsn + 1))
249 } while (curcmdsn == cmdsn);
252 * The target MUST silently ignore any non-immediate command outside
255 if (ISCSI_SNLT(cmdsn, curcmdsn) ||
256 ISCSI_SNGT(cmdsn, curcmdsn - 1 + maxtags)) {
257 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
258 "while expected %u", cmdsn, curcmdsn);
263 * We don't support multiple connections now, so any discontinuity in
264 * CmdSN means lost PDUs. Since we don't support PDU retransmission --
265 * terminate the connection.
267 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
268 "while expected %u; dropping connection",
270 cfiscsi_session_terminate(cs);
275 cfiscsi_pdu_handle(struct icl_pdu *request)
277 struct cfiscsi_session *cs;
280 cs = PDU_SESSION(request);
282 ignore = cfiscsi_pdu_update_cmdsn(request);
284 icl_pdu_free(request);
289 * Handle the PDU; this includes e.g. receiving the remaining
290 * part of PDU and submitting the SCSI command to CTL
291 * or queueing a reply. The handling routine is responsible
292 * for freeing the PDU when it's no longer needed.
294 switch (request->ip_bhs->bhs_opcode &
295 ~ISCSI_BHS_OPCODE_IMMEDIATE) {
296 case ISCSI_BHS_OPCODE_NOP_OUT:
297 cfiscsi_pdu_handle_nop_out(request);
299 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
300 cfiscsi_pdu_handle_scsi_command(request);
302 case ISCSI_BHS_OPCODE_TASK_REQUEST:
303 cfiscsi_pdu_handle_task_request(request);
305 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
306 cfiscsi_pdu_handle_data_out(request);
308 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
309 cfiscsi_pdu_handle_logout_request(request);
312 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
313 "opcode 0x%x; dropping connection",
314 request->ip_bhs->bhs_opcode);
315 icl_pdu_free(request);
316 cfiscsi_session_terminate(cs);
322 cfiscsi_receive_callback(struct icl_pdu *request)
324 #ifdef ICL_KERNEL_PROXY
325 struct cfiscsi_session *cs;
327 cs = PDU_SESSION(request);
328 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
329 if (cs->cs_login_pdu == NULL)
330 cs->cs_login_pdu = request;
332 icl_pdu_free(request);
333 cv_signal(&cs->cs_login_cv);
338 cfiscsi_pdu_handle(request);
342 cfiscsi_error_callback(struct icl_conn *ic)
344 struct cfiscsi_session *cs;
346 cs = CONN_SESSION(ic);
348 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
349 cfiscsi_session_terminate(cs);
353 cfiscsi_pdu_prepare(struct icl_pdu *response)
355 struct cfiscsi_session *cs;
356 struct iscsi_bhs_scsi_response *bhssr;
357 bool advance_statsn = true;
360 cs = PDU_SESSION(response);
362 CFISCSI_SESSION_LOCK_ASSERT(cs);
365 * We're only using fields common for all the response
366 * (target -> initiator) PDUs.
368 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
371 * 10.8.3: "The StatSN for this connection is not advanced
372 * after this PDU is sent."
374 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
375 advance_statsn = false;
378 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
379 * StatSN for the connection is not advanced after this PDU is sent."
381 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
382 bhssr->bhssr_initiator_task_tag == 0xffffffff)
383 advance_statsn = false;
386 * See the comment below - StatSN is not meaningful and must
389 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
390 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
391 advance_statsn = false;
394 * 10.7.3: "The fields StatSN, Status, and Residual Count
395 * only have meaningful content if the S bit is set to 1."
397 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
398 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
399 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
400 cmdsn = cs->cs_cmdsn;
401 bhssr->bhssr_expcmdsn = htonl(cmdsn);
402 bhssr->bhssr_maxcmdsn = htonl(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_queue_cb(struct icl_pdu *response, icl_pdu_cb cb)
427 struct cfiscsi_session *cs = PDU_SESSION(response);
429 CFISCSI_SESSION_LOCK(cs);
430 cfiscsi_pdu_prepare(response);
431 icl_pdu_queue_cb(response, cb);
432 CFISCSI_SESSION_UNLOCK(cs);
436 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
438 struct cfiscsi_session *cs;
439 struct iscsi_bhs_nop_out *bhsno;
440 struct iscsi_bhs_nop_in *bhsni;
441 struct icl_pdu *response;
446 cs = PDU_SESSION(request);
447 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
449 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
451 * Nothing to do, iscsi_pdu_update_statsn() already
452 * zeroed the timeout.
454 icl_pdu_free(request);
458 datasize = icl_pdu_data_segment_length(request);
460 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
462 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
463 "dropping connection");
464 icl_pdu_free(request);
465 cfiscsi_session_terminate(cs);
468 icl_pdu_get_data(request, 0, data, datasize);
471 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
472 if (response == NULL) {
473 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
474 "droppping connection");
475 free(data, M_CFISCSI);
476 icl_pdu_free(request);
477 cfiscsi_session_terminate(cs);
480 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
481 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
482 bhsni->bhsni_flags = 0x80;
483 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
484 bhsni->bhsni_target_transfer_tag = 0xffffffff;
486 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
488 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
489 "dropping connection");
490 free(data, M_CFISCSI);
491 icl_pdu_free(request);
492 icl_pdu_free(response);
493 cfiscsi_session_terminate(cs);
496 free(data, M_CFISCSI);
499 icl_pdu_free(request);
500 cfiscsi_pdu_queue(response);
504 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
506 struct iscsi_bhs_scsi_command *bhssc;
507 struct cfiscsi_session *cs;
511 cs = PDU_SESSION(request);
512 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
513 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
514 // bhssc->bhssc_initiator_task_tag);
516 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
517 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
518 "ImmediateData=No; dropping connection");
519 icl_pdu_free(request);
520 cfiscsi_session_terminate(cs);
523 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
525 PRIV_REQUEST(io) = request;
526 io->io_hdr.io_type = CTL_IO_SCSI;
527 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
528 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
529 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhssc->bhssc_lun));
530 io->scsiio.priority = (bhssc->bhssc_pri & BHSSC_PRI_MASK) >>
532 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
533 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
534 case BHSSC_FLAGS_ATTR_UNTAGGED:
535 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
537 case BHSSC_FLAGS_ATTR_SIMPLE:
538 io->scsiio.tag_type = CTL_TAG_SIMPLE;
540 case BHSSC_FLAGS_ATTR_ORDERED:
541 io->scsiio.tag_type = CTL_TAG_ORDERED;
543 case BHSSC_FLAGS_ATTR_HOQ:
544 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
546 case BHSSC_FLAGS_ATTR_ACA:
547 io->scsiio.tag_type = CTL_TAG_ACA;
550 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
551 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
552 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
555 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
556 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
557 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
559 if (error != CTL_RETVAL_COMPLETE) {
560 CFISCSI_SESSION_WARN(cs, "ctl_run() failed; error %d; "
561 "dropping connection", error);
563 refcount_release(&cs->cs_outstanding_ctl_pdus);
564 icl_pdu_free(request);
565 cfiscsi_session_terminate(cs);
570 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
572 struct iscsi_bhs_task_management_request *bhstmr;
573 struct iscsi_bhs_task_management_response *bhstmr2;
574 struct icl_pdu *response;
575 struct cfiscsi_session *cs;
579 cs = PDU_SESSION(request);
580 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
581 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
583 PRIV_REQUEST(io) = request;
584 io->io_hdr.io_type = CTL_IO_TASK;
585 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
586 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
587 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhstmr->bhstmr_lun));
588 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
590 switch (bhstmr->bhstmr_function & ~0x80) {
591 case BHSTMR_FUNCTION_ABORT_TASK:
593 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
595 io->taskio.task_action = CTL_TASK_ABORT_TASK;
596 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
598 case BHSTMR_FUNCTION_ABORT_TASK_SET:
600 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
602 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
604 case BHSTMR_FUNCTION_CLEAR_TASK_SET:
606 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_CLEAR_TASK_SET");
608 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
610 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
612 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
614 io->taskio.task_action = CTL_TASK_LUN_RESET;
616 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
618 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
620 io->taskio.task_action = CTL_TASK_TARGET_RESET;
622 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
624 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
626 io->taskio.task_action = CTL_TASK_TARGET_RESET;
628 case BHSTMR_FUNCTION_QUERY_TASK:
630 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK");
632 io->taskio.task_action = CTL_TASK_QUERY_TASK;
633 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
635 case BHSTMR_FUNCTION_QUERY_TASK_SET:
637 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK_SET");
639 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
641 case BHSTMR_FUNCTION_I_T_NEXUS_RESET:
643 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_I_T_NEXUS_RESET");
645 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
647 case BHSTMR_FUNCTION_QUERY_ASYNC_EVENT:
649 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_ASYNC_EVENT");
651 io->taskio.task_action = CTL_TASK_QUERY_ASYNC_EVENT;
654 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
655 bhstmr->bhstmr_function & ~0x80);
658 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
659 if (response == NULL) {
660 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
661 "dropping connection");
662 icl_pdu_free(request);
663 cfiscsi_session_terminate(cs);
666 bhstmr2 = (struct iscsi_bhs_task_management_response *)
668 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
669 bhstmr2->bhstmr_flags = 0x80;
670 bhstmr2->bhstmr_response =
671 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
672 bhstmr2->bhstmr_initiator_task_tag =
673 bhstmr->bhstmr_initiator_task_tag;
674 icl_pdu_free(request);
675 cfiscsi_pdu_queue(response);
679 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
681 if (error != CTL_RETVAL_COMPLETE) {
682 CFISCSI_SESSION_WARN(cs, "ctl_run() failed; error %d; "
683 "dropping connection", error);
685 refcount_release(&cs->cs_outstanding_ctl_pdus);
686 icl_pdu_free(request);
687 cfiscsi_session_terminate(cs);
692 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
694 struct iscsi_bhs_data_out *bhsdo;
695 struct cfiscsi_session *cs;
696 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
697 size_t copy_len, len, off, buffer_offset;
701 cs = PDU_SESSION(request);
703 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
704 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
705 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
706 ISCSI_BHS_OPCODE_SCSI_COMMAND,
707 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
710 * We're only using fields common for Data-Out and SCSI Command PDUs.
712 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
714 io = cdw->cdw_ctl_io;
715 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
716 ("CTL_FLAG_DATA_IN"));
719 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
720 request->ip_data_len, io->scsiio.kern_total_len);
723 if (io->scsiio.kern_sg_entries > 0) {
724 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
725 ctl_sg_count = io->scsiio.kern_sg_entries;
727 ctl_sglist = &ctl_sg_entry;
728 ctl_sglist->addr = io->scsiio.kern_data_ptr;
729 ctl_sglist->len = io->scsiio.kern_data_len;
733 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
734 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
735 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
738 len = icl_pdu_data_segment_length(request);
741 * Make sure the offset, as sent by the initiator, matches the offset
742 * we're supposed to be at in the scatter-gather list.
745 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
746 buffer_offset + len <=
747 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
748 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
749 "expected %zd; dropping connection", buffer_offset,
750 (size_t)io->scsiio.kern_rel_offset +
751 (size_t)io->scsiio.ext_data_filled);
752 ctl_set_data_phase_error(&io->scsiio);
753 cfiscsi_session_terminate(cs);
758 * This is the offset within the PDU data segment, as opposed
759 * to buffer_offset, which is the offset within the task (SCSI
762 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
766 * Iterate over the scatter/gather segments, filling them with data
767 * from the PDU data segment. Note that this can get called multiple
768 * times for one SCSI command; the cdw structure holds state for the
769 * scatter/gather list.
772 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
773 ("cdw->cdw_sg_index >= ctl_sg_count"));
774 if (cdw->cdw_sg_len == 0) {
775 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
776 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
778 KASSERT(off <= len, ("len > off"));
779 copy_len = len - off;
780 if (copy_len > cdw->cdw_sg_len)
781 copy_len = cdw->cdw_sg_len;
783 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
784 cdw->cdw_sg_addr += copy_len;
785 cdw->cdw_sg_len -= copy_len;
787 io->scsiio.ext_data_filled += copy_len;
788 io->scsiio.kern_data_resid -= copy_len;
790 if (cdw->cdw_sg_len == 0) {
792 * End of current segment.
794 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
796 * Last segment in scatter/gather list.
805 * End of PDU payload.
813 * In case of unsolicited data, it's possible that the buffer
814 * provided by CTL is smaller than negotiated FirstBurstLength.
815 * Just ignore the superfluous data; will ask for them with R2T
816 * on next call to cfiscsi_datamove().
818 * This obviously can only happen with SCSI Command PDU.
820 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
821 ISCSI_BHS_OPCODE_SCSI_COMMAND)
824 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
825 "expected %zd; dropping connection",
826 icl_pdu_data_segment_length(request), off);
827 ctl_set_data_phase_error(&io->scsiio);
828 cfiscsi_session_terminate(cs);
832 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
833 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
834 CFISCSI_SESSION_WARN(cs, "got the final packet without "
835 "the F flag; flags = 0x%x; dropping connection",
837 ctl_set_data_phase_error(&io->scsiio);
838 cfiscsi_session_terminate(cs);
842 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
843 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
844 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
845 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
846 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
847 "transmitted size was %zd bytes instead of %d; "
848 "dropping connection",
849 (size_t)io->scsiio.ext_data_filled,
851 ctl_set_data_phase_error(&io->scsiio);
852 cfiscsi_session_terminate(cs);
856 * For SCSI Command PDU, this just means we need to
857 * solicit more data by sending R2T.
863 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
865 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
866 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
876 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
878 struct iscsi_bhs_data_out *bhsdo;
879 struct cfiscsi_session *cs;
880 struct cfiscsi_data_wait *cdw = NULL;
884 cs = PDU_SESSION(request);
885 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
887 CFISCSI_SESSION_LOCK(cs);
888 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
890 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
891 "ttt 0x%x, itt 0x%x",
892 bhsdo->bhsdo_target_transfer_tag,
893 bhsdo->bhsdo_initiator_task_tag,
894 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
896 if (bhsdo->bhsdo_target_transfer_tag ==
897 cdw->cdw_target_transfer_tag)
900 CFISCSI_SESSION_UNLOCK(cs);
902 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
903 "0x%x, not found; dropping connection",
904 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
905 icl_pdu_free(request);
906 cfiscsi_session_terminate(cs);
910 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
911 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
912 "DataSN %u, while expected %u; dropping connection",
913 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
914 icl_pdu_free(request);
915 cfiscsi_session_terminate(cs);
918 cdw->cdw_datasn += request->ip_additional_pdus + 1;
920 io = cdw->cdw_ctl_io;
921 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
922 ("CTL_FLAG_DATA_IN"));
924 done = cfiscsi_handle_data_segment(request, cdw);
926 CFISCSI_SESSION_LOCK(cs);
927 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
928 CFISCSI_SESSION_UNLOCK(cs);
929 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
930 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
931 cfiscsi_data_wait_free(cs, cdw);
932 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
934 ctl_datamove_done(io, false);
936 cfiscsi_datamove_out(io);
939 icl_pdu_free(request);
943 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
945 struct iscsi_bhs_logout_request *bhslr;
946 struct iscsi_bhs_logout_response *bhslr2;
947 struct icl_pdu *response;
948 struct cfiscsi_session *cs;
950 cs = PDU_SESSION(request);
951 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
952 switch (bhslr->bhslr_reason & 0x7f) {
953 case BHSLR_REASON_CLOSE_SESSION:
954 case BHSLR_REASON_CLOSE_CONNECTION:
955 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
956 if (response == NULL) {
957 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
958 icl_pdu_free(request);
959 cfiscsi_session_terminate(cs);
962 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
963 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
964 bhslr2->bhslr_flags = 0x80;
965 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
966 bhslr2->bhslr_initiator_task_tag =
967 bhslr->bhslr_initiator_task_tag;
968 icl_pdu_free(request);
969 cfiscsi_pdu_queue(response);
970 cfiscsi_session_terminate(cs);
972 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
973 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
974 if (response == NULL) {
975 CFISCSI_SESSION_WARN(cs,
976 "failed to allocate memory; dropping connection");
977 icl_pdu_free(request);
978 cfiscsi_session_terminate(cs);
981 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
982 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
983 bhslr2->bhslr_flags = 0x80;
984 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
985 bhslr2->bhslr_initiator_task_tag =
986 bhslr->bhslr_initiator_task_tag;
987 icl_pdu_free(request);
988 cfiscsi_pdu_queue(response);
991 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
992 bhslr->bhslr_reason);
993 icl_pdu_free(request);
994 cfiscsi_session_terminate(cs);
1000 cfiscsi_callout(void *context)
1003 struct iscsi_bhs_nop_in *bhsni;
1004 struct cfiscsi_session *cs;
1008 if (cs->cs_terminating)
1011 callout_schedule(&cs->cs_callout, 1 * hz);
1013 atomic_add_int(&cs->cs_timeout, 1);
1015 #ifdef ICL_KERNEL_PROXY
1016 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1017 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1018 CFISCSI_SESSION_WARN(cs, "login timed out after "
1019 "%d seconds; dropping connection", cs->cs_timeout);
1020 cfiscsi_session_terminate(cs);
1026 if (ping_timeout <= 0) {
1028 * Pings are disabled. Don't send NOP-In in this case;
1029 * user might have disabled pings to work around problems
1030 * with certain initiators that can't properly handle
1031 * NOP-In, such as iPXE. Reset the timeout, to avoid
1032 * triggering reconnection, should the user decide to
1039 if (cs->cs_timeout >= ping_timeout) {
1040 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1041 "dropping connection", ping_timeout);
1042 cfiscsi_session_terminate(cs);
1047 * If the ping was reset less than one second ago - which means
1048 * that we've received some PDU during the last second - assume
1049 * the traffic flows correctly and don't bother sending a NOP-Out.
1051 * (It's 2 - one for one second, and one for incrementing is_timeout
1052 * earlier in this routine.)
1054 if (cs->cs_timeout < 2)
1057 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1059 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1062 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1063 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1064 bhsni->bhsni_flags = 0x80;
1065 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1067 cfiscsi_pdu_queue(cp);
1070 static struct cfiscsi_data_wait *
1071 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1072 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1074 struct cfiscsi_data_wait *cdw;
1077 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1079 CFISCSI_SESSION_WARN(cs,
1080 "failed to allocate %zd bytes", sizeof(*cdw));
1084 error = icl_conn_transfer_setup(cs->cs_conn, PRIV_REQUEST(io), io,
1085 target_transfer_tagp, &cdw->cdw_icl_prv);
1087 CFISCSI_SESSION_WARN(cs,
1088 "icl_conn_transfer_setup() failed with error %d", error);
1089 uma_zfree(cfiscsi_data_wait_zone, cdw);
1093 cdw->cdw_ctl_io = io;
1094 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1095 cdw->cdw_initiator_task_tag = initiator_task_tag;
1101 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1102 struct cfiscsi_data_wait *cdw)
1105 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1106 uma_zfree(cfiscsi_data_wait_zone, cdw);
1110 cfiscsi_data_wait_abort(struct cfiscsi_session *cs,
1111 struct cfiscsi_data_wait *cdw, int status)
1113 union ctl_io *cdw_io;
1116 * Set nonzero port status; this prevents backends from
1117 * assuming that the data transfer actually succeeded
1118 * and writing uninitialized data to disk.
1121 cdw_io = cdw->cdw_ctl_io;
1122 cdw_io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1123 cdw_io->scsiio.io_hdr.port_status = status;
1124 cfiscsi_data_wait_free(cs, cdw);
1125 ctl_datamove_done(cdw_io, false);
1129 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1131 struct cfiscsi_data_wait *cdw;
1134 int error, last, wait;
1136 if (cs->cs_target == NULL)
1137 return; /* No target yet, so nothing to do. */
1138 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1139 ip->ip_bhs->bhs_opcode = ISCSI_BHS_OPCODE_INTERNAL;
1140 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1142 PRIV_REQUEST(io) = ip;
1143 io->io_hdr.io_type = CTL_IO_TASK;
1144 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1145 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1146 io->io_hdr.nexus.targ_lun = 0;
1147 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1148 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1149 wait = cs->cs_outstanding_ctl_pdus;
1150 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1151 error = ctl_run(io);
1152 if (error != CTL_RETVAL_COMPLETE) {
1153 CFISCSI_SESSION_WARN(cs, "ctl_run() failed; error %d", error);
1154 refcount_release(&cs->cs_outstanding_ctl_pdus);
1159 CFISCSI_SESSION_LOCK(cs);
1160 cs->cs_terminating_tasks = true;
1161 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1162 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1163 CFISCSI_SESSION_UNLOCK(cs);
1164 cfiscsi_data_wait_abort(cs, cdw, 42);
1165 CFISCSI_SESSION_LOCK(cs);
1167 CFISCSI_SESSION_UNLOCK(cs);
1170 * Wait for CTL to terminate all the tasks.
1173 CFISCSI_SESSION_WARN(cs,
1174 "waiting for CTL to terminate %d tasks", wait);
1176 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1177 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1180 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1181 0, "cfiscsi_terminate", hz / 100);
1184 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1188 cfiscsi_maintenance_thread(void *arg)
1190 struct cfiscsi_session *cs;
1195 CFISCSI_SESSION_LOCK(cs);
1196 if (cs->cs_terminating == false || cs->cs_handoff_in_progress)
1197 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1198 CFISCSI_SESSION_UNLOCK(cs);
1200 if (cs->cs_terminating && cs->cs_handoff_in_progress == false) {
1202 * We used to wait up to 30 seconds to deliver queued
1203 * PDUs to the initiator. We also tried hard to deliver
1204 * SCSI Responses for the aborted PDUs. We don't do
1205 * that anymore. We might need to revisit that.
1207 callout_drain(&cs->cs_callout);
1208 icl_conn_close(cs->cs_conn);
1211 * At this point ICL receive thread is no longer
1212 * running; no new tasks can be queued.
1214 cfiscsi_session_terminate_tasks(cs);
1215 cfiscsi_session_delete(cs);
1219 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1224 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1227 cs->cs_terminating = true;
1228 cv_signal(&cs->cs_maintenance_cv);
1229 #ifdef ICL_KERNEL_PROXY
1230 cv_signal(&cs->cs_login_cv);
1235 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1237 struct cfiscsi_target *ct;
1241 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1244 name = strdup(cs->cs_initiator_id, M_CTL);
1245 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1247 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1249 cs->cs_ctl_initid = -1;
1252 cs->cs_ctl_initid = i;
1254 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1261 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1265 if (cs->cs_ctl_initid == -1)
1268 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1270 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1273 cs->cs_ctl_initid = -1;
1276 static struct cfiscsi_session *
1277 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1279 struct cfiscsi_session *cs;
1282 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1284 CFISCSI_WARN("malloc failed");
1287 cs->cs_ctl_initid = -1;
1289 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1290 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1291 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1292 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1293 #ifdef ICL_KERNEL_PROXY
1294 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1298 * The purpose of this is to avoid racing with session shutdown.
1299 * Otherwise we could have the maintenance thread call icl_conn_close()
1300 * before we call icl_conn_handoff().
1302 cs->cs_handoff_in_progress = true;
1304 cs->cs_conn = icl_new_conn(offload, false, "cfiscsi", &cs->cs_lock);
1305 if (cs->cs_conn == NULL) {
1306 free(cs, M_CFISCSI);
1309 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1310 cs->cs_conn->ic_error = cfiscsi_error_callback;
1311 cs->cs_conn->ic_prv0 = cs;
1313 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1315 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1316 free(cs, M_CFISCSI);
1320 mtx_lock(&softc->lock);
1321 cs->cs_id = ++softc->last_session_id;
1322 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1323 mtx_unlock(&softc->lock);
1326 * Start pinging the initiator.
1328 callout_init(&cs->cs_callout, 1);
1329 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1335 cfiscsi_session_delete(struct cfiscsi_session *cs)
1337 struct cfiscsi_softc *softc;
1339 softc = &cfiscsi_softc;
1341 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1342 ("destroying session with outstanding CTL pdus"));
1343 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1344 ("destroying session with non-empty queue"));
1346 mtx_lock(&softc->lock);
1347 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1348 mtx_unlock(&softc->lock);
1350 cfiscsi_session_unregister_initiator(cs);
1351 if (cs->cs_target != NULL)
1352 cfiscsi_target_release(cs->cs_target);
1353 icl_conn_close(cs->cs_conn);
1354 icl_conn_free(cs->cs_conn);
1355 free(cs, M_CFISCSI);
1356 cv_signal(&softc->sessions_cv);
1362 struct cfiscsi_softc *softc;
1364 softc = &cfiscsi_softc;
1365 bzero(softc, sizeof(*softc));
1366 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1368 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1369 #ifdef ICL_KERNEL_PROXY
1370 cv_init(&softc->accept_cv, "cfiscsi_accept");
1372 TAILQ_INIT(&softc->sessions);
1373 TAILQ_INIT(&softc->targets);
1375 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1376 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1383 cfiscsi_shutdown(void)
1385 struct cfiscsi_softc *softc = &cfiscsi_softc;
1387 if (!TAILQ_EMPTY(&softc->sessions) || !TAILQ_EMPTY(&softc->targets))
1390 uma_zdestroy(cfiscsi_data_wait_zone);
1391 #ifdef ICL_KERNEL_PROXY
1392 cv_destroy(&softc->accept_cv);
1394 cv_destroy(&softc->sessions_cv);
1395 mtx_destroy(&softc->lock);
1399 #ifdef ICL_KERNEL_PROXY
1401 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1403 struct cfiscsi_session *cs;
1405 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1407 CFISCSI_WARN("failed to create session");
1411 icl_conn_handoff_sock(cs->cs_conn, so);
1412 cs->cs_initiator_sa = sa;
1413 cs->cs_portal_id = portal_id;
1414 cs->cs_handoff_in_progress = false;
1415 cs->cs_waiting_for_ctld = true;
1416 cv_signal(&cfiscsi_softc.accept_cv);
1418 CFISCSI_SESSION_LOCK(cs);
1420 * Wake up the maintenance thread if we got scheduled for termination
1421 * somewhere between cfiscsi_session_new() and icl_conn_handoff_sock().
1423 if (cs->cs_terminating)
1424 cfiscsi_session_terminate(cs);
1425 CFISCSI_SESSION_UNLOCK(cs);
1430 cfiscsi_online(void *arg)
1432 struct cfiscsi_softc *softc;
1433 struct cfiscsi_target *ct;
1436 ct = (struct cfiscsi_target *)arg;
1437 softc = ct->ct_softc;
1439 mtx_lock(&softc->lock);
1440 if (ct->ct_online) {
1441 mtx_unlock(&softc->lock);
1445 online = softc->online++;
1446 mtx_unlock(&softc->lock);
1450 #ifdef ICL_KERNEL_PROXY
1451 if (softc->listener != NULL)
1452 icl_listen_free(softc->listener);
1453 softc->listener = icl_listen_new(cfiscsi_accept);
1458 cfiscsi_offline(void *arg)
1460 struct cfiscsi_softc *softc;
1461 struct cfiscsi_target *ct;
1462 struct cfiscsi_session *cs;
1465 ct = (struct cfiscsi_target *)arg;
1466 softc = ct->ct_softc;
1468 mtx_lock(&softc->lock);
1469 if (!ct->ct_online) {
1470 mtx_unlock(&softc->lock);
1474 online = --softc->online;
1477 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1478 if (cs->cs_target == ct)
1479 cfiscsi_session_terminate(cs);
1481 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1482 if (cs->cs_target == ct)
1486 error = cv_wait_sig(&softc->sessions_cv, &softc->lock);
1488 CFISCSI_SESSION_DEBUG(cs,
1489 "cv_wait failed with error %d\n", error);
1493 } while (cs != NULL && ct->ct_online == 0);
1494 mtx_unlock(&softc->lock);
1498 #ifdef ICL_KERNEL_PROXY
1499 icl_listen_free(softc->listener);
1500 softc->listener = NULL;
1505 cfiscsi_info(void *arg, struct sbuf *sb)
1507 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1510 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1516 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1518 struct cfiscsi_softc *softc;
1519 struct cfiscsi_session *cs, *cs2;
1520 struct cfiscsi_target *ct;
1521 struct ctl_iscsi_handoff_params *cihp;
1524 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1525 softc = &cfiscsi_softc;
1527 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1528 cihp->initiator_name, cihp->initiator_addr,
1531 ct = cfiscsi_target_find(softc, cihp->target_name,
1532 cihp->portal_group_tag);
1534 ci->status = CTL_ISCSI_ERROR;
1535 snprintf(ci->error_str, sizeof(ci->error_str),
1536 "%s: target not found", __func__);
1540 #ifdef ICL_KERNEL_PROXY
1541 if (cihp->socket > 0 && cihp->connection_id > 0) {
1542 snprintf(ci->error_str, sizeof(ci->error_str),
1543 "both socket and connection_id set");
1544 ci->status = CTL_ISCSI_ERROR;
1545 cfiscsi_target_release(ct);
1548 if (cihp->socket == 0) {
1549 mtx_lock(&cfiscsi_softc.lock);
1550 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1551 if (cs->cs_id == cihp->connection_id)
1555 mtx_unlock(&cfiscsi_softc.lock);
1556 snprintf(ci->error_str, sizeof(ci->error_str),
1557 "connection not found");
1558 ci->status = CTL_ISCSI_ERROR;
1559 cfiscsi_target_release(ct);
1562 mtx_unlock(&cfiscsi_softc.lock);
1565 cs = cfiscsi_session_new(softc, cihp->offload);
1567 ci->status = CTL_ISCSI_ERROR;
1568 snprintf(ci->error_str, sizeof(ci->error_str),
1569 "%s: cfiscsi_session_new failed", __func__);
1570 cfiscsi_target_release(ct);
1573 #ifdef ICL_KERNEL_PROXY
1578 * First PDU of Full Feature phase has the same CmdSN as the last
1579 * PDU from the Login Phase received from the initiator. Thus,
1582 cs->cs_cmdsn = cihp->cmdsn;
1583 cs->cs_statsn = cihp->statsn;
1584 cs->cs_conn->ic_max_recv_data_segment_length =
1585 cihp->max_recv_data_segment_length;
1586 cs->cs_conn->ic_max_send_data_segment_length =
1587 cihp->max_send_data_segment_length;
1588 cs->cs_max_burst_length = cihp->max_burst_length;
1589 cs->cs_first_burst_length = cihp->first_burst_length;
1590 cs->cs_immediate_data = !!cihp->immediate_data;
1591 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1592 cs->cs_conn->ic_header_crc32c = true;
1593 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1594 cs->cs_conn->ic_data_crc32c = true;
1596 strlcpy(cs->cs_initiator_name,
1597 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1598 strlcpy(cs->cs_initiator_addr,
1599 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1600 strlcpy(cs->cs_initiator_alias,
1601 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1602 memcpy(cs->cs_initiator_isid,
1603 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1604 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1605 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1606 cihp->initiator_isid[0], cihp->initiator_isid[1],
1607 cihp->initiator_isid[2], cihp->initiator_isid[3],
1608 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1610 mtx_lock(&softc->lock);
1611 if (ct->ct_online == 0) {
1612 mtx_unlock(&softc->lock);
1613 CFISCSI_SESSION_LOCK(cs);
1614 cs->cs_handoff_in_progress = false;
1615 cfiscsi_session_terminate(cs);
1616 CFISCSI_SESSION_UNLOCK(cs);
1617 cfiscsi_target_release(ct);
1618 ci->status = CTL_ISCSI_ERROR;
1619 snprintf(ci->error_str, sizeof(ci->error_str),
1620 "%s: port offline", __func__);
1624 mtx_unlock(&softc->lock);
1627 if (!cs->cs_terminating) {
1628 mtx_lock(&softc->lock);
1629 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1630 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1631 cs->cs_target == cs2->cs_target &&
1632 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1633 if (strcmp(cs->cs_initiator_addr,
1634 cs2->cs_initiator_addr) != 0) {
1635 CFISCSI_SESSION_WARN(cs2,
1636 "session reinstatement from "
1637 "different address %s",
1638 cs->cs_initiator_addr);
1640 CFISCSI_SESSION_DEBUG(cs2,
1641 "session reinstatement");
1643 cfiscsi_session_terminate(cs2);
1644 mtx_unlock(&softc->lock);
1645 pause("cfiscsi_reinstate", 1);
1649 mtx_unlock(&softc->lock);
1653 * Register initiator with CTL.
1655 cfiscsi_session_register_initiator(cs);
1657 #ifdef ICL_KERNEL_PROXY
1658 if (cihp->socket > 0) {
1660 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1662 CFISCSI_SESSION_LOCK(cs);
1663 cs->cs_handoff_in_progress = false;
1664 cfiscsi_session_terminate(cs);
1665 CFISCSI_SESSION_UNLOCK(cs);
1666 ci->status = CTL_ISCSI_ERROR;
1667 snprintf(ci->error_str, sizeof(ci->error_str),
1668 "%s: icl_conn_handoff failed with error %d",
1672 #ifdef ICL_KERNEL_PROXY
1676 #ifdef ICL_KERNEL_PROXY
1677 cs->cs_login_phase = false;
1680 * First PDU of the Full Feature phase has likely already arrived.
1681 * We have to pick it up and execute properly.
1683 if (cs->cs_login_pdu != NULL) {
1684 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1685 cfiscsi_pdu_handle(cs->cs_login_pdu);
1686 cs->cs_login_pdu = NULL;
1690 CFISCSI_SESSION_LOCK(cs);
1691 cs->cs_handoff_in_progress = false;
1694 * Wake up the maintenance thread if we got scheduled for termination.
1696 if (cs->cs_terminating)
1697 cfiscsi_session_terminate(cs);
1698 CFISCSI_SESSION_UNLOCK(cs);
1700 ci->status = CTL_ISCSI_OK;
1704 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1706 struct ctl_iscsi_list_params *cilp;
1707 struct cfiscsi_session *cs;
1708 struct cfiscsi_softc *softc;
1712 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1713 softc = &cfiscsi_softc;
1715 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1717 ci->status = CTL_ISCSI_ERROR;
1718 snprintf(ci->error_str, sizeof(ci->error_str),
1719 "Unable to allocate %d bytes for iSCSI session list",
1724 sbuf_cat(sb, "<ctlislist>\n");
1725 mtx_lock(&softc->lock);
1726 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1727 if (cs->cs_target == NULL)
1729 error = sbuf_printf(sb, "<connection id=\"%d\">"
1730 "<initiator>%s</initiator>"
1731 "<initiator_addr>%s</initiator_addr>"
1732 "<initiator_alias>%s</initiator_alias>"
1733 "<target>%s</target>"
1734 "<target_alias>%s</target_alias>"
1735 "<target_portal_group_tag>%u</target_portal_group_tag>"
1736 "<header_digest>%s</header_digest>"
1737 "<data_digest>%s</data_digest>"
1738 "<max_recv_data_segment_length>%d</max_recv_data_segment_length>"
1739 "<max_send_data_segment_length>%d</max_send_data_segment_length>"
1740 "<max_burst_length>%d</max_burst_length>"
1741 "<first_burst_length>%d</first_burst_length>"
1742 "<immediate_data>%d</immediate_data>"
1744 "<offload>%s</offload>"
1747 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1748 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1749 cs->cs_target->ct_tag,
1750 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1751 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1752 cs->cs_conn->ic_max_recv_data_segment_length,
1753 cs->cs_conn->ic_max_send_data_segment_length,
1754 cs->cs_max_burst_length,
1755 cs->cs_first_burst_length,
1756 cs->cs_immediate_data,
1757 cs->cs_conn->ic_iser,
1758 cs->cs_conn->ic_offload);
1762 mtx_unlock(&softc->lock);
1763 error = sbuf_cat(sb, "</ctlislist>\n");
1766 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1767 snprintf(ci->error_str, sizeof(ci->error_str),
1768 "Out of space, %d bytes is too small", cilp->alloc_len);
1773 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1776 snprintf(ci->error_str, sizeof(ci->error_str),
1777 "copyout failed with error %d", error);
1778 ci->status = CTL_ISCSI_ERROR;
1781 cilp->fill_len = sbuf_len(sb) + 1;
1782 ci->status = CTL_ISCSI_OK;
1787 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1789 struct icl_pdu *response;
1790 struct iscsi_bhs_asynchronous_message *bhsam;
1791 struct ctl_iscsi_logout_params *cilp;
1792 struct cfiscsi_session *cs;
1793 struct cfiscsi_softc *softc;
1796 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1797 softc = &cfiscsi_softc;
1799 mtx_lock(&softc->lock);
1800 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1801 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1802 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1803 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1806 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1807 if (response == NULL) {
1808 ci->status = CTL_ISCSI_ERROR;
1809 snprintf(ci->error_str, sizeof(ci->error_str),
1810 "Unable to allocate memory");
1811 mtx_unlock(&softc->lock);
1815 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1816 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1817 bhsam->bhsam_flags = 0x80;
1818 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1819 bhsam->bhsam_parameter3 = htons(10);
1820 cfiscsi_pdu_queue(response);
1823 mtx_unlock(&softc->lock);
1826 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1827 snprintf(ci->error_str, sizeof(ci->error_str),
1828 "No matching connections found");
1832 ci->status = CTL_ISCSI_OK;
1836 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1838 struct icl_pdu *response;
1839 struct iscsi_bhs_asynchronous_message *bhsam;
1840 struct ctl_iscsi_terminate_params *citp;
1841 struct cfiscsi_session *cs;
1842 struct cfiscsi_softc *softc;
1845 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1846 softc = &cfiscsi_softc;
1848 mtx_lock(&softc->lock);
1849 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1850 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1851 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1852 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1855 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1856 if (response == NULL) {
1858 * Oh well. Just terminate the connection.
1861 bhsam = (struct iscsi_bhs_asynchronous_message *)
1863 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1864 bhsam->bhsam_flags = 0x80;
1865 bhsam->bhsam_0xffffffff = 0xffffffff;
1866 bhsam->bhsam_async_event =
1867 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1868 cfiscsi_pdu_queue(response);
1870 cfiscsi_session_terminate(cs);
1873 mtx_unlock(&softc->lock);
1876 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1877 snprintf(ci->error_str, sizeof(ci->error_str),
1878 "No matching connections found");
1882 ci->status = CTL_ISCSI_OK;
1886 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1888 struct ctl_iscsi_limits_params *cilp;
1889 struct icl_drv_limits idl;
1892 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1894 error = icl_limits(cilp->offload, false, cilp->socket, &idl);
1896 ci->status = CTL_ISCSI_ERROR;
1897 snprintf(ci->error_str, sizeof(ci->error_str),
1898 "%s: icl_limits failed with error %d",
1903 cilp->max_recv_data_segment_length =
1904 idl.idl_max_recv_data_segment_length;
1905 cilp->max_send_data_segment_length =
1906 idl.idl_max_send_data_segment_length;
1907 cilp->max_burst_length = idl.idl_max_burst_length;
1908 cilp->first_burst_length = idl.idl_first_burst_length;
1910 ci->status = CTL_ISCSI_OK;
1913 #ifdef ICL_KERNEL_PROXY
1915 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1917 struct ctl_iscsi_listen_params *cilp;
1918 struct sockaddr *sa;
1921 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1923 if (cfiscsi_softc.listener == NULL) {
1924 CFISCSI_DEBUG("no listener");
1925 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1926 ci->status = CTL_ISCSI_ERROR;
1930 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1932 CFISCSI_DEBUG("getsockaddr, error %d", error);
1933 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1934 ci->status = CTL_ISCSI_ERROR;
1938 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1939 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1942 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1943 snprintf(ci->error_str, sizeof(ci->error_str),
1944 "icl_listen_add failed, error %d", error);
1945 ci->status = CTL_ISCSI_ERROR;
1949 ci->status = CTL_ISCSI_OK;
1953 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1955 struct ctl_iscsi_accept_params *ciap;
1956 struct cfiscsi_session *cs;
1959 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1961 mtx_lock(&cfiscsi_softc.lock);
1963 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1964 if (cs->cs_waiting_for_ctld)
1969 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1971 mtx_unlock(&cfiscsi_softc.lock);
1972 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1973 ci->status = CTL_ISCSI_ERROR;
1977 mtx_unlock(&cfiscsi_softc.lock);
1979 cs->cs_waiting_for_ctld = false;
1980 cs->cs_login_phase = true;
1982 ciap->connection_id = cs->cs_id;
1983 ciap->portal_id = cs->cs_portal_id;
1984 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1985 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1986 cs->cs_initiator_sa->sa_len);
1988 snprintf(ci->error_str, sizeof(ci->error_str),
1989 "copyout failed with error %d", error);
1990 ci->status = CTL_ISCSI_ERROR;
1994 ci->status = CTL_ISCSI_OK;
1998 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
2000 struct ctl_iscsi_send_params *cisp;
2001 struct cfiscsi_session *cs;
2007 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
2009 mtx_lock(&cfiscsi_softc.lock);
2010 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2011 if (cs->cs_id == cisp->connection_id)
2015 mtx_unlock(&cfiscsi_softc.lock);
2016 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
2017 ci->status = CTL_ISCSI_ERROR;
2020 mtx_unlock(&cfiscsi_softc.lock);
2023 if (cs->cs_login_phase == false)
2027 if (cs->cs_terminating) {
2028 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
2029 ci->status = CTL_ISCSI_ERROR;
2033 datalen = cisp->data_segment_len;
2037 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
2038 if (datalen > 65535) {
2039 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
2040 ci->status = CTL_ISCSI_ERROR;
2044 data = malloc(datalen, M_CFISCSI, M_WAITOK);
2045 error = copyin(cisp->data_segment, data, datalen);
2047 free(data, M_CFISCSI);
2048 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
2049 ci->status = CTL_ISCSI_ERROR;
2054 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
2055 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
2057 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
2058 free(data, M_CFISCSI);
2060 CFISCSI_SESSION_LOCK(cs);
2062 CFISCSI_SESSION_UNLOCK(cs);
2063 ci->status = CTL_ISCSI_OK;
2067 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
2069 struct ctl_iscsi_receive_params *cirp;
2070 struct cfiscsi_session *cs;
2075 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
2077 mtx_lock(&cfiscsi_softc.lock);
2078 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2079 if (cs->cs_id == cirp->connection_id)
2083 mtx_unlock(&cfiscsi_softc.lock);
2084 snprintf(ci->error_str, sizeof(ci->error_str),
2085 "connection not found");
2086 ci->status = CTL_ISCSI_ERROR;
2089 mtx_unlock(&cfiscsi_softc.lock);
2092 if (is->is_login_phase == false)
2096 CFISCSI_SESSION_LOCK(cs);
2097 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2098 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2100 CFISCSI_SESSION_UNLOCK(cs);
2101 snprintf(ci->error_str, sizeof(ci->error_str),
2102 "interrupted by signal");
2103 ci->status = CTL_ISCSI_ERROR;
2108 if (cs->cs_terminating) {
2109 CFISCSI_SESSION_UNLOCK(cs);
2110 snprintf(ci->error_str, sizeof(ci->error_str),
2111 "connection terminating");
2112 ci->status = CTL_ISCSI_ERROR;
2115 ip = cs->cs_login_pdu;
2116 cs->cs_login_pdu = NULL;
2117 CFISCSI_SESSION_UNLOCK(cs);
2119 if (ip->ip_data_len > cirp->data_segment_len) {
2121 snprintf(ci->error_str, sizeof(ci->error_str),
2122 "data segment too big");
2123 ci->status = CTL_ISCSI_ERROR;
2127 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2128 if (ip->ip_data_len > 0) {
2129 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2130 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2131 copyout(data, cirp->data_segment, ip->ip_data_len);
2132 free(data, M_CFISCSI);
2136 ci->status = CTL_ISCSI_OK;
2139 #endif /* !ICL_KERNEL_PROXY */
2142 cfiscsi_ioctl_port_create(struct ctl_req *req)
2144 struct cfiscsi_target *ct;
2145 struct ctl_port *port;
2146 const char *target, *alias, *val;
2147 struct scsi_vpd_id_descriptor *desc;
2148 int retval, len, idlen;
2151 target = dnvlist_get_string(req->args_nvl, "cfiscsi_target", NULL);
2152 alias = dnvlist_get_string(req->args_nvl, "cfiscsi_target_alias", NULL);
2153 val = dnvlist_get_string(req->args_nvl, "cfiscsi_portal_group_tag",
2156 if (target == NULL || val == NULL) {
2157 req->status = CTL_LUN_ERROR;
2158 snprintf(req->error_str, sizeof(req->error_str),
2159 "Missing required argument");
2163 tag = strtoul(val, NULL, 0);
2164 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2166 req->status = CTL_LUN_ERROR;
2167 snprintf(req->error_str, sizeof(req->error_str),
2168 "failed to create target \"%s\"", target);
2171 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2172 req->status = CTL_LUN_ERROR;
2173 snprintf(req->error_str, sizeof(req->error_str),
2174 "target \"%s\" for portal group tag %u already exists",
2176 cfiscsi_target_release(ct);
2179 port = &ct->ct_port;
2181 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2184 port->frontend = &cfiscsi_frontend;
2185 port->port_type = CTL_PORT_ISCSI;
2186 /* XXX KDM what should the real number be here? */
2187 port->num_requested_ctl_io = 4096;
2188 port->port_name = "iscsi";
2189 port->physical_port = (int)tag;
2190 port->virtual_port = ct->ct_target_id;
2191 port->port_online = cfiscsi_online;
2192 port->port_offline = cfiscsi_offline;
2193 port->port_info = cfiscsi_info;
2194 port->onoff_arg = ct;
2195 port->fe_datamove = cfiscsi_datamove;
2196 port->fe_done = cfiscsi_done;
2197 port->targ_port = -1;
2198 port->options = nvlist_clone(req->args_nvl);
2200 /* Generate Port ID. */
2201 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2202 idlen = roundup2(idlen, 4);
2203 len = sizeof(struct scsi_vpd_device_id) + idlen;
2204 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2205 M_CTL, M_WAITOK | M_ZERO);
2206 port->port_devid->len = len;
2207 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2208 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2209 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2210 SVPD_ID_TYPE_SCSI_NAME;
2211 desc->length = idlen;
2212 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2214 /* Generate Target ID. */
2215 idlen = strlen(target) + 1;
2216 idlen = roundup2(idlen, 4);
2217 len = sizeof(struct scsi_vpd_device_id) + idlen;
2218 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2219 M_CTL, M_WAITOK | M_ZERO);
2220 port->target_devid->len = len;
2221 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2222 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2223 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2224 SVPD_ID_TYPE_SCSI_NAME;
2225 desc->length = idlen;
2226 strlcpy(desc->identifier, target, idlen);
2228 retval = ctl_port_register(port);
2230 free(port->port_devid, M_CFISCSI);
2231 free(port->target_devid, M_CFISCSI);
2232 cfiscsi_target_release(ct);
2233 req->status = CTL_LUN_ERROR;
2234 snprintf(req->error_str, sizeof(req->error_str),
2235 "ctl_port_register() failed with error %d", retval);
2239 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2240 req->status = CTL_LUN_OK;
2241 req->result_nvl = nvlist_create(0);
2242 nvlist_add_number(req->result_nvl, "port_id", port->targ_port);
2246 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2248 struct cfiscsi_target *ct;
2249 const char *target, *val;
2252 target = dnvlist_get_string(req->args_nvl, "cfiscsi_target", NULL);
2253 val = dnvlist_get_string(req->args_nvl, "cfiscsi_portal_group_tag",
2256 if (target == NULL || val == NULL) {
2257 req->status = CTL_LUN_ERROR;
2258 snprintf(req->error_str, sizeof(req->error_str),
2259 "Missing required argument");
2263 tag = strtoul(val, NULL, 0);
2264 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2266 req->status = CTL_LUN_ERROR;
2267 snprintf(req->error_str, sizeof(req->error_str),
2268 "can't find target \"%s\"", target);
2272 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2273 ctl_port_offline(&ct->ct_port);
2274 cfiscsi_target_release(ct);
2275 cfiscsi_target_release(ct);
2276 req->status = CTL_LUN_OK;
2280 cfiscsi_ioctl(struct cdev *dev,
2281 u_long cmd, caddr_t addr, int flag, struct thread *td)
2283 struct ctl_iscsi *ci;
2284 struct ctl_req *req;
2286 if (cmd == CTL_PORT_REQ) {
2287 req = (struct ctl_req *)addr;
2288 switch (req->reqtype) {
2289 case CTL_REQ_CREATE:
2290 cfiscsi_ioctl_port_create(req);
2292 case CTL_REQ_REMOVE:
2293 cfiscsi_ioctl_port_remove(req);
2296 req->status = CTL_LUN_ERROR;
2297 snprintf(req->error_str, sizeof(req->error_str),
2298 "Unsupported request type %d", req->reqtype);
2303 if (cmd != CTL_ISCSI)
2306 ci = (struct ctl_iscsi *)addr;
2308 case CTL_ISCSI_HANDOFF:
2309 cfiscsi_ioctl_handoff(ci);
2311 case CTL_ISCSI_LIST:
2312 cfiscsi_ioctl_list(ci);
2314 case CTL_ISCSI_LOGOUT:
2315 cfiscsi_ioctl_logout(ci);
2317 case CTL_ISCSI_TERMINATE:
2318 cfiscsi_ioctl_terminate(ci);
2320 case CTL_ISCSI_LIMITS:
2321 cfiscsi_ioctl_limits(ci);
2323 #ifdef ICL_KERNEL_PROXY
2324 case CTL_ISCSI_LISTEN:
2325 cfiscsi_ioctl_listen(ci);
2327 case CTL_ISCSI_ACCEPT:
2328 cfiscsi_ioctl_accept(ci);
2330 case CTL_ISCSI_SEND:
2331 cfiscsi_ioctl_send(ci);
2333 case CTL_ISCSI_RECEIVE:
2334 cfiscsi_ioctl_receive(ci);
2337 case CTL_ISCSI_LISTEN:
2338 case CTL_ISCSI_ACCEPT:
2339 case CTL_ISCSI_SEND:
2340 case CTL_ISCSI_RECEIVE:
2341 ci->status = CTL_ISCSI_ERROR;
2342 snprintf(ci->error_str, sizeof(ci->error_str),
2343 "%s: CTL compiled without ICL_KERNEL_PROXY",
2346 #endif /* !ICL_KERNEL_PROXY */
2348 ci->status = CTL_ISCSI_ERROR;
2349 snprintf(ci->error_str, sizeof(ci->error_str),
2350 "%s: invalid iSCSI request type %d", __func__, ci->type);
2358 cfiscsi_target_hold(struct cfiscsi_target *ct)
2361 refcount_acquire(&ct->ct_refcount);
2365 cfiscsi_target_release(struct cfiscsi_target *ct)
2367 struct cfiscsi_softc *softc;
2369 softc = ct->ct_softc;
2370 mtx_lock(&softc->lock);
2371 if (refcount_release(&ct->ct_refcount)) {
2372 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2373 mtx_unlock(&softc->lock);
2374 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2375 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2376 if (ctl_port_deregister(&ct->ct_port) != 0)
2377 printf("%s: ctl_port_deregister() failed\n",
2380 free(ct, M_CFISCSI);
2384 mtx_unlock(&softc->lock);
2387 static struct cfiscsi_target *
2388 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2390 struct cfiscsi_target *ct;
2392 mtx_lock(&softc->lock);
2393 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2394 if (ct->ct_tag != tag ||
2395 strcmp(name, ct->ct_name) != 0 ||
2396 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2398 cfiscsi_target_hold(ct);
2399 mtx_unlock(&softc->lock);
2402 mtx_unlock(&softc->lock);
2407 static struct cfiscsi_target *
2408 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2409 const char *alias, uint16_t tag)
2411 struct cfiscsi_target *ct, *newct;
2413 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2416 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2418 mtx_lock(&softc->lock);
2419 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2420 if (ct->ct_tag != tag ||
2421 strcmp(name, ct->ct_name) != 0 ||
2422 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2424 cfiscsi_target_hold(ct);
2425 mtx_unlock(&softc->lock);
2426 free(newct, M_CFISCSI);
2430 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2432 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2433 newct->ct_tag = tag;
2434 refcount_init(&newct->ct_refcount, 1);
2435 newct->ct_softc = softc;
2436 if (TAILQ_EMPTY(&softc->targets))
2437 softc->last_target_id = 0;
2438 newct->ct_target_id = ++softc->last_target_id;
2439 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2440 mtx_unlock(&softc->lock);
2446 cfiscsi_pdu_done(struct icl_pdu *ip, int error)
2450 ; // XXX: Do something on error?
2451 ((ctl_ref)ip->ip_prv0)(ip->ip_prv1, -1);
2455 cfiscsi_datamove_in(union ctl_io *io)
2457 struct cfiscsi_session *cs;
2458 struct icl_pdu *request, *response;
2459 const struct iscsi_bhs_scsi_command *bhssc;
2460 struct iscsi_bhs_data_in *bhsdi;
2461 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2462 size_t len, expected_len, sg_len, buffer_offset;
2463 size_t max_send_data_segment_length;
2464 const char *sg_addr;
2466 int ctl_sg_count, error, i;
2468 request = PRIV_REQUEST(io);
2469 cs = PDU_SESSION(request);
2471 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2472 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2473 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2474 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2476 if (io->scsiio.kern_sg_entries > 0) {
2477 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2478 ctl_sg_count = io->scsiio.kern_sg_entries;
2480 ctl_sglist = &ctl_sg_entry;
2481 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2482 ctl_sglist->len = io->scsiio.kern_data_len;
2487 * This is the offset within the current SCSI command; for the first
2488 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2489 * it will be the sum of lengths of previous ones.
2491 buffer_offset = io->scsiio.kern_rel_offset;
2494 * This is the transfer length expected by the initiator. It can be
2495 * different from the amount of data from the SCSI point of view.
2497 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2500 * If the transfer is outside of expected length -- we are done.
2502 if (buffer_offset >= expected_len) {
2504 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2505 "already sent the expected len", buffer_offset);
2507 ctl_datamove_done(io, true);
2511 if (io->scsiio.kern_data_ref != NULL)
2512 cb = cfiscsi_pdu_done;
2521 if (cs->cs_conn->ic_hw_isomax != 0)
2522 max_send_data_segment_length = cs->cs_conn->ic_hw_isomax;
2524 max_send_data_segment_length =
2525 cs->cs_conn->ic_max_send_data_segment_length;
2527 if (response == NULL) {
2528 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2529 if (response == NULL) {
2530 CFISCSI_SESSION_WARN(cs, "failed to "
2531 "allocate memory; dropping connection");
2532 ctl_set_busy(&io->scsiio);
2533 ctl_datamove_done(io, true);
2534 cfiscsi_session_terminate(cs);
2537 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2538 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2539 bhsdi->bhsdi_initiator_task_tag =
2540 bhssc->bhssc_initiator_task_tag;
2541 bhsdi->bhsdi_target_transfer_tag = 0xffffffff;
2542 bhsdi->bhsdi_datasn = htonl(PRIV_EXPDATASN(io));
2543 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2546 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2548 sg_addr = ctl_sglist[i].addr;
2549 sg_len = ctl_sglist[i].len;
2550 KASSERT(sg_len > 0, ("sg_len <= 0"));
2556 * Truncate to maximum data segment length.
2558 KASSERT(response->ip_data_len < max_send_data_segment_length,
2559 ("ip_data_len %zd >= max_send_data_segment_length %zd",
2560 response->ip_data_len, max_send_data_segment_length));
2561 if (response->ip_data_len + len > max_send_data_segment_length) {
2562 len = max_send_data_segment_length - response->ip_data_len;
2563 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2568 * Truncate to expected data transfer length.
2570 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2571 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2572 buffer_offset, response->ip_data_len, expected_len));
2573 if (buffer_offset + response->ip_data_len + len > expected_len) {
2574 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2575 "to expected data transfer length %zd",
2576 buffer_offset + response->ip_data_len + len, expected_len);
2577 len = expected_len - (buffer_offset + response->ip_data_len);
2578 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2582 error = icl_pdu_append_data(response, sg_addr, len,
2583 M_NOWAIT | (cb ? ICL_NOCOPY : 0));
2585 CFISCSI_SESSION_WARN(cs, "failed to "
2586 "allocate memory; dropping connection");
2587 icl_pdu_free(response);
2588 ctl_set_busy(&io->scsiio);
2589 ctl_datamove_done(io, true);
2590 cfiscsi_session_terminate(cs);
2595 io->scsiio.kern_data_resid -= len;
2597 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2598 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2599 buffer_offset, response->ip_data_len, expected_len));
2600 if (buffer_offset + response->ip_data_len == expected_len) {
2602 * Already have the amount of data the initiator wanted.
2609 * End of scatter-gather segment;
2610 * proceed to the next one...
2612 if (i == ctl_sg_count - 1) {
2614 * ... unless this was the last one.
2621 if (response->ip_data_len == max_send_data_segment_length) {
2623 * Can't stuff more data into the current PDU;
2624 * queue it. Note that's not enough to check
2625 * for kern_data_resid == 0 instead; there
2626 * may be several Data-In PDUs for the final
2627 * call to cfiscsi_datamove(), and we want
2628 * to set the F flag only on the last of them.
2630 buffer_offset += response->ip_data_len;
2631 if (buffer_offset == io->scsiio.kern_total_len ||
2632 buffer_offset == expected_len) {
2633 buffer_offset -= response->ip_data_len;
2636 PRIV_EXPDATASN(io) += howmany(response->ip_data_len,
2637 cs->cs_conn->ic_max_send_data_segment_length);
2639 response->ip_prv0 = io->scsiio.kern_data_ref;
2640 response->ip_prv1 = io->scsiio.kern_data_arg;
2641 io->scsiio.kern_data_ref(io->scsiio.kern_data_arg, 1);
2643 cfiscsi_pdu_queue_cb(response, cb);
2648 if (response != NULL) {
2649 buffer_offset += response->ip_data_len;
2650 if (buffer_offset == io->scsiio.kern_total_len ||
2651 buffer_offset == expected_len) {
2652 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2653 if (io->io_hdr.status == CTL_SUCCESS) {
2654 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2655 if (io->scsiio.kern_total_len <
2656 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2657 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2658 bhsdi->bhsdi_residual_count =
2659 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2660 io->scsiio.kern_total_len);
2661 } else if (io->scsiio.kern_total_len >
2662 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2663 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2664 bhsdi->bhsdi_residual_count =
2665 htonl(io->scsiio.kern_total_len -
2666 ntohl(bhssc->bhssc_expected_data_transfer_length));
2668 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2669 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2672 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2673 PRIV_EXPDATASN(io) += howmany(response->ip_data_len,
2674 cs->cs_conn->ic_max_send_data_segment_length);
2676 response->ip_prv0 = io->scsiio.kern_data_ref;
2677 response->ip_prv1 = io->scsiio.kern_data_arg;
2678 io->scsiio.kern_data_ref(io->scsiio.kern_data_arg, 1);
2680 cfiscsi_pdu_queue_cb(response, cb);
2683 ctl_datamove_done(io, true);
2687 cfiscsi_datamove_out(union ctl_io *io)
2689 struct cfiscsi_session *cs;
2690 struct icl_pdu *request, *response;
2691 const struct iscsi_bhs_scsi_command *bhssc;
2692 struct iscsi_bhs_r2t *bhsr2t;
2693 struct cfiscsi_data_wait *cdw;
2694 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2695 uint32_t expected_len, datamove_len, r2t_off, r2t_len;
2696 uint32_t target_transfer_tag;
2699 request = PRIV_REQUEST(io);
2700 cs = PDU_SESSION(request);
2702 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2703 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2704 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2705 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2708 * Complete write underflow. Not a single byte to read. Return.
2710 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2711 if (io->scsiio.kern_rel_offset >= expected_len) {
2712 ctl_datamove_done(io, true);
2716 datamove_len = MIN(io->scsiio.kern_data_len,
2717 expected_len - io->scsiio.kern_rel_offset);
2719 target_transfer_tag =
2720 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2721 if (target_transfer_tag == 0xffffffff) {
2722 target_transfer_tag =
2723 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2725 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2726 &target_transfer_tag);
2728 CFISCSI_SESSION_WARN(cs, "failed to "
2729 "allocate memory; dropping connection");
2730 ctl_set_busy(&io->scsiio);
2731 ctl_datamove_done(io, true);
2732 cfiscsi_session_terminate(cs);
2736 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2737 "task tag 0x%x, target transfer tag 0x%x",
2738 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2741 cdw->cdw_ctl_io = io;
2742 cdw->cdw_target_transfer_tag = target_transfer_tag;
2743 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2744 cdw->cdw_r2t_end = datamove_len;
2745 cdw->cdw_datasn = 0;
2747 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2748 if (io->scsiio.kern_sg_entries > 0) {
2749 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2751 ctl_sglist = &ctl_sg_entry;
2752 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2753 ctl_sglist->len = datamove_len;
2755 cdw->cdw_sg_index = 0;
2756 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2757 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2758 r2t_off = io->scsiio.ext_data_filled;
2759 while (r2t_off > 0) {
2760 if (r2t_off >= cdw->cdw_sg_len) {
2761 r2t_off -= cdw->cdw_sg_len;
2762 cdw->cdw_sg_index++;
2763 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2764 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2767 cdw->cdw_sg_addr += r2t_off;
2768 cdw->cdw_sg_len -= r2t_off;
2772 if (cs->cs_immediate_data &&
2773 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2774 icl_pdu_data_segment_length(request)) {
2775 done = cfiscsi_handle_data_segment(request, cdw);
2777 cfiscsi_data_wait_free(cs, cdw);
2778 ctl_datamove_done(io, true);
2783 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2784 r2t_len = MIN(datamove_len - io->scsiio.ext_data_filled,
2785 cs->cs_max_burst_length);
2786 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2788 CFISCSI_SESSION_LOCK(cs);
2789 if (cs->cs_terminating_tasks) {
2790 CFISCSI_SESSION_UNLOCK(cs);
2791 KASSERT((io->io_hdr.flags & CTL_FLAG_ABORT) != 0,
2792 ("%s: I/O request %p on termating session %p not aborted",
2794 CFISCSI_SESSION_WARN(cs, "aborting data_wait for aborted I/O");
2795 cfiscsi_data_wait_abort(cs, cdw, 44);
2798 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2799 CFISCSI_SESSION_UNLOCK(cs);
2802 * XXX: We should limit the number of outstanding R2T PDUs
2803 * per task to MaxOutstandingR2T.
2805 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2806 if (response == NULL) {
2807 CFISCSI_SESSION_WARN(cs, "failed to "
2808 "allocate memory; dropping connection");
2809 ctl_set_busy(&io->scsiio);
2810 ctl_datamove_done(io, true);
2811 cfiscsi_session_terminate(cs);
2814 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
2815 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2816 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2817 bhsr2t->bhsr2t_flags = 0x80;
2818 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2819 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2820 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2822 * XXX: Here we assume that cfiscsi_datamove() won't ever
2823 * be running concurrently on several CPUs for a given
2826 bhsr2t->bhsr2t_r2tsn = htonl(PRIV_R2TSN(io)++);
2828 * This is the offset within the current SCSI command;
2829 * i.e. for the first call of datamove(), it will be 0,
2830 * and for subsequent ones it will be the sum of lengths
2833 * The ext_data_filled is to account for unsolicited
2834 * (immediate) data that might have already arrived.
2836 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2838 * This is the total length (sum of S/G lengths) this call
2839 * to cfiscsi_datamove() is supposed to handle, limited by
2842 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2843 cfiscsi_pdu_queue(response);
2847 cfiscsi_datamove(union ctl_io *io)
2850 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2851 cfiscsi_datamove_in(io);
2853 /* We hadn't received anything during this datamove yet. */
2854 io->scsiio.ext_data_filled = 0;
2855 cfiscsi_datamove_out(io);
2860 cfiscsi_scsi_command_done(union ctl_io *io)
2862 struct icl_pdu *request, *response;
2863 struct iscsi_bhs_scsi_command *bhssc;
2864 struct iscsi_bhs_scsi_response *bhssr;
2866 struct cfiscsi_data_wait *cdw;
2867 struct cfiscsi_session *cs;
2869 uint16_t sense_length;
2871 request = PRIV_REQUEST(io);
2872 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2873 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2874 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2875 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2877 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2878 // bhssc->bhssc_initiator_task_tag);
2881 cs = PDU_SESSION(request);
2882 CFISCSI_SESSION_LOCK(cs);
2883 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2884 KASSERT(bhssc->bhssc_initiator_task_tag !=
2885 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2886 CFISCSI_SESSION_UNLOCK(cs);
2890 * Do not return status for aborted commands.
2891 * There are exceptions, but none supported by CTL yet.
2893 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2894 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2895 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2897 icl_pdu_free(request);
2901 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2902 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2903 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2904 bhssr->bhssr_flags = 0x80;
2906 * XXX: We don't deal with bidirectional under/overflows;
2907 * does anything actually support those?
2909 if (io->scsiio.kern_total_len <
2910 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2911 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2912 bhssr->bhssr_residual_count =
2913 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2914 io->scsiio.kern_total_len);
2915 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2916 // ntohl(bhssr->bhssr_residual_count));
2917 } else if (io->scsiio.kern_total_len >
2918 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2919 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2920 bhssr->bhssr_residual_count = htonl(io->scsiio.kern_total_len -
2921 ntohl(bhssc->bhssc_expected_data_transfer_length));
2922 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2923 // ntohl(bhssr->bhssr_residual_count));
2925 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2926 bhssr->bhssr_status = io->scsiio.scsi_status;
2927 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2928 bhssr->bhssr_expdatasn = htonl(PRIV_EXPDATASN(io));
2930 if (io->scsiio.sense_len > 0) {
2932 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2933 io->scsiio.sense_len);
2935 sense_length = htons(io->scsiio.sense_len);
2936 icl_pdu_append_data(response,
2937 &sense_length, sizeof(sense_length), M_WAITOK);
2938 icl_pdu_append_data(response,
2939 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2943 icl_pdu_free(request);
2944 cfiscsi_pdu_queue(response);
2948 cfiscsi_task_management_done(union ctl_io *io)
2950 struct icl_pdu *request, *response;
2951 struct iscsi_bhs_task_management_request *bhstmr;
2952 struct iscsi_bhs_task_management_response *bhstmr2;
2953 struct cfiscsi_data_wait *cdw, *tmpcdw;
2954 struct cfiscsi_session *cs, *tcs;
2955 struct cfiscsi_softc *softc;
2958 request = PRIV_REQUEST(io);
2959 cs = PDU_SESSION(request);
2960 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2961 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2962 ISCSI_BHS_OPCODE_TASK_REQUEST,
2963 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2966 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2967 bhstmr->bhstmr_initiator_task_tag,
2968 bhstmr->bhstmr_referenced_task_tag);
2971 if ((bhstmr->bhstmr_function & ~0x80) ==
2972 BHSTMR_FUNCTION_ABORT_TASK) {
2974 * Make sure we no longer wait for Data-Out for this command.
2976 CFISCSI_SESSION_LOCK(cs);
2977 TAILQ_FOREACH_SAFE(cdw,
2978 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2979 if (bhstmr->bhstmr_referenced_task_tag !=
2980 cdw->cdw_initiator_task_tag)
2984 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2985 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2987 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2989 cfiscsi_data_wait_abort(cs, cdw, 43);
2991 CFISCSI_SESSION_UNLOCK(cs);
2993 if ((bhstmr->bhstmr_function & ~0x80) ==
2994 BHSTMR_FUNCTION_TARGET_COLD_RESET &&
2995 io->io_hdr.status == CTL_SUCCESS)
2998 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2999 bhstmr2 = (struct iscsi_bhs_task_management_response *)
3001 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
3002 bhstmr2->bhstmr_flags = 0x80;
3003 switch (io->taskio.task_status) {
3004 case CTL_TASK_FUNCTION_COMPLETE:
3005 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
3007 case CTL_TASK_FUNCTION_SUCCEEDED:
3008 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED;
3010 case CTL_TASK_LUN_DOES_NOT_EXIST:
3011 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST;
3013 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
3015 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
3018 memcpy(bhstmr2->bhstmr_additional_reponse_information,
3019 io->taskio.task_resp, sizeof(io->taskio.task_resp));
3020 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
3023 icl_pdu_free(request);
3024 cfiscsi_pdu_queue(response);
3027 softc = cs->cs_target->ct_softc;
3028 mtx_lock(&softc->lock);
3029 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) {
3030 if (tcs->cs_target == cs->cs_target)
3031 cfiscsi_session_terminate(tcs);
3033 mtx_unlock(&softc->lock);
3038 cfiscsi_done(union ctl_io *io)
3040 struct icl_pdu *request;
3041 struct cfiscsi_session *cs;
3043 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
3044 ("invalid CTL status %#x", io->io_hdr.status));
3046 request = PRIV_REQUEST(io);
3047 cs = PDU_SESSION(request);
3049 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
3050 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
3051 cfiscsi_scsi_command_done(io);
3053 case ISCSI_BHS_OPCODE_TASK_REQUEST:
3054 cfiscsi_task_management_done(io);
3056 case ISCSI_BHS_OPCODE_INTERNAL:
3058 * Implicit task termination has just completed; nothing to do.
3060 icl_pdu_free(request);
3061 cs->cs_tasks_aborted = true;
3062 refcount_release(&cs->cs_outstanding_ctl_pdus);
3063 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
3067 panic("cfiscsi_done called with wrong opcode 0x%x",
3068 request->ip_bhs->bhs_opcode);
3071 refcount_release(&cs->cs_outstanding_ctl_pdus);