2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * CTL frontend for the iSCSI protocol.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/condvar.h>
42 #include <sys/endian.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/queue.h>
52 #include <sys/socket.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
56 #include <sys/unistd.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_da.h>
61 #include <cam/ctl/ctl_io.h>
62 #include <cam/ctl/ctl.h>
63 #include <cam/ctl/ctl_backend.h>
64 #include <cam/ctl/ctl_error.h>
65 #include <cam/ctl/ctl_frontend.h>
66 #include <cam/ctl/ctl_debug.h>
67 #include <cam/ctl/ctl_ha.h>
68 #include <cam/ctl/ctl_ioctl.h>
69 #include <cam/ctl/ctl_private.h>
71 #include <dev/iscsi/icl.h>
72 #include <dev/iscsi/icl_wrappers.h>
73 #include <dev/iscsi/iscsi_proto.h>
74 #include <cam/ctl/ctl_frontend_iscsi.h>
76 #ifdef ICL_KERNEL_PROXY
77 #include <sys/socketvar.h>
80 #ifdef ICL_KERNEL_PROXY
81 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
84 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
85 static uma_zone_t cfiscsi_data_wait_zone;
87 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
88 "CAM Target Layer iSCSI Frontend");
90 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
91 &debug, 1, "Enable debug messages");
92 static int ping_timeout = 5;
93 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
94 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
95 static int login_timeout = 60;
96 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
97 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
98 static int maxtags = 256;
99 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxtags, CTLFLAG_RWTUN,
100 &maxtags, 0, "Max number of requests queued by initiator");
102 #define CFISCSI_DEBUG(X, ...) \
105 printf("%s: " X "\n", \
106 __func__, ## __VA_ARGS__); \
110 #define CFISCSI_WARN(X, ...) \
113 printf("WARNING: %s: " X "\n", \
114 __func__, ## __VA_ARGS__); \
118 #define CFISCSI_SESSION_DEBUG(S, X, ...) \
121 printf("%s: %s (%s): " X "\n", \
122 __func__, S->cs_initiator_addr, \
123 S->cs_initiator_name, ## __VA_ARGS__); \
127 #define CFISCSI_SESSION_WARN(S, X, ...) \
130 printf("WARNING: %s (%s): " X "\n", \
131 S->cs_initiator_addr, \
132 S->cs_initiator_name, ## __VA_ARGS__); \
136 #define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock)
137 #define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock)
138 #define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED)
140 #define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0)
141 #define PDU_SESSION(X) CONN_SESSION((X)->ip_conn)
142 #define PDU_EXPDATASN(X) (X)->ip_prv0
143 #define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1
144 #define PDU_R2TSN(X) (X)->ip_prv2
146 static int cfiscsi_init(void);
147 static int cfiscsi_shutdown(void);
148 static void cfiscsi_online(void *arg);
149 static void cfiscsi_offline(void *arg);
150 static int cfiscsi_info(void *arg, struct sbuf *sb);
151 static int cfiscsi_ioctl(struct cdev *dev,
152 u_long cmd, caddr_t addr, int flag, struct thread *td);
153 static void cfiscsi_datamove(union ctl_io *io);
154 static void cfiscsi_datamove_in(union ctl_io *io);
155 static void cfiscsi_datamove_out(union ctl_io *io);
156 static void cfiscsi_done(union ctl_io *io);
157 static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
158 static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
159 static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
160 static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
161 static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
162 static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
163 static void cfiscsi_session_terminate(struct cfiscsi_session *cs);
164 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
165 struct cfiscsi_session *cs, union ctl_io *io,
166 uint32_t initiator_task_tag,
167 uint32_t *target_transfer_tagp);
168 static void cfiscsi_data_wait_free(struct cfiscsi_session *cs,
169 struct cfiscsi_data_wait *cdw);
170 static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc
171 *softc, const char *name, uint16_t tag);
172 static struct cfiscsi_target *cfiscsi_target_find_or_create(
173 struct cfiscsi_softc *softc, const char *name, const char *alias,
175 static void cfiscsi_target_release(struct cfiscsi_target *ct);
176 static void cfiscsi_session_delete(struct cfiscsi_session *cs);
178 static struct cfiscsi_softc cfiscsi_softc;
180 static struct ctl_frontend cfiscsi_frontend =
183 .init = cfiscsi_init,
184 .ioctl = cfiscsi_ioctl,
185 .shutdown = cfiscsi_shutdown,
187 CTL_FRONTEND_DECLARE(cfiscsi, cfiscsi_frontend);
188 MODULE_DEPEND(cfiscsi, icl, 1, 1, 1);
190 static struct icl_pdu *
191 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
194 return (icl_pdu_new(request->ip_conn, flags));
198 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
200 const struct iscsi_bhs_scsi_command *bhssc;
201 struct cfiscsi_session *cs;
202 uint32_t cmdsn, expstatsn;
204 cs = PDU_SESSION(request);
207 * Every incoming PDU - not just NOP-Out - resets the ping timer.
208 * The purpose of the timeout is to reset the connection when it stalls;
209 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
217 * Data-Out PDUs don't contain CmdSN.
219 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
220 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
224 * We're only using fields common for all the request
225 * (initiator -> target) PDUs.
227 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
228 cmdsn = ntohl(bhssc->bhssc_cmdsn);
229 expstatsn = ntohl(bhssc->bhssc_expstatsn);
231 CFISCSI_SESSION_LOCK(cs);
233 if (expstatsn != cs->cs_statsn) {
234 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
235 "while current StatSN is %d", expstatsn,
240 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
242 * The target MUST silently ignore any non-immediate command
243 * outside of this range.
245 if (ISCSI_SNLT(cmdsn, cs->cs_cmdsn) ||
246 ISCSI_SNGT(cmdsn, cs->cs_cmdsn - 1 + maxtags)) {
247 CFISCSI_SESSION_UNLOCK(cs);
248 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
249 "while expected %u", cmdsn, cs->cs_cmdsn);
254 * We don't support multiple connections now, so any
255 * discontinuity in CmdSN means lost PDUs. Since we don't
256 * support PDU retransmission -- terminate the connection.
258 if (cmdsn != cs->cs_cmdsn) {
259 CFISCSI_SESSION_UNLOCK(cs);
260 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
261 "while expected %u; dropping connection",
262 cmdsn, cs->cs_cmdsn);
263 cfiscsi_session_terminate(cs);
269 CFISCSI_SESSION_UNLOCK(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;
359 cs = PDU_SESSION(response);
361 CFISCSI_SESSION_LOCK_ASSERT(cs);
364 * We're only using fields common for all the response
365 * (target -> initiator) PDUs.
367 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
370 * 10.8.3: "The StatSN for this connection is not advanced
371 * after this PDU is sent."
373 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
374 advance_statsn = false;
377 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
378 * StatSN for the connection is not advanced after this PDU is sent."
380 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
381 bhssr->bhssr_initiator_task_tag == 0xffffffff)
382 advance_statsn = false;
385 * See the comment below - StatSN is not meaningful and must
388 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
389 (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
390 advance_statsn = false;
393 * 10.7.3: "The fields StatSN, Status, and Residual Count
394 * only have meaningful content if the S bit is set to 1."
396 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
397 (bhssr->bhssr_flags & BHSDI_FLAGS_S))
398 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
399 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
400 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn - 1 +
401 imax(0, maxtags - cs->cs_outstanding_ctl_pdus));
410 cfiscsi_pdu_queue(struct icl_pdu *response)
412 struct cfiscsi_session *cs;
414 cs = PDU_SESSION(response);
416 CFISCSI_SESSION_LOCK(cs);
417 cfiscsi_pdu_prepare(response);
418 icl_pdu_queue(response);
419 CFISCSI_SESSION_UNLOCK(cs);
423 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
425 struct cfiscsi_session *cs;
426 struct iscsi_bhs_nop_out *bhsno;
427 struct iscsi_bhs_nop_in *bhsni;
428 struct icl_pdu *response;
433 cs = PDU_SESSION(request);
434 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
436 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
438 * Nothing to do, iscsi_pdu_update_statsn() already
439 * zeroed the timeout.
441 icl_pdu_free(request);
445 datasize = icl_pdu_data_segment_length(request);
447 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
449 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
450 "dropping connection");
451 icl_pdu_free(request);
452 cfiscsi_session_terminate(cs);
455 icl_pdu_get_data(request, 0, data, datasize);
458 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
459 if (response == NULL) {
460 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
461 "droppping connection");
462 free(data, M_CFISCSI);
463 icl_pdu_free(request);
464 cfiscsi_session_terminate(cs);
467 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
468 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
469 bhsni->bhsni_flags = 0x80;
470 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
471 bhsni->bhsni_target_transfer_tag = 0xffffffff;
473 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
475 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
476 "dropping connection");
477 free(data, M_CFISCSI);
478 icl_pdu_free(request);
479 icl_pdu_free(response);
480 cfiscsi_session_terminate(cs);
483 free(data, M_CFISCSI);
486 icl_pdu_free(request);
487 cfiscsi_pdu_queue(response);
491 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
493 struct iscsi_bhs_scsi_command *bhssc;
494 struct cfiscsi_session *cs;
498 cs = PDU_SESSION(request);
499 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
500 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
501 // bhssc->bhssc_initiator_task_tag);
503 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
504 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
505 "ImmediateData=No; dropping connection");
506 icl_pdu_free(request);
507 cfiscsi_session_terminate(cs);
510 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
512 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
513 io->io_hdr.io_type = CTL_IO_SCSI;
514 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
515 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
516 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhssc->bhssc_lun));
517 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
518 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
519 case BHSSC_FLAGS_ATTR_UNTAGGED:
520 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
522 case BHSSC_FLAGS_ATTR_SIMPLE:
523 io->scsiio.tag_type = CTL_TAG_SIMPLE;
525 case BHSSC_FLAGS_ATTR_ORDERED:
526 io->scsiio.tag_type = CTL_TAG_ORDERED;
528 case BHSSC_FLAGS_ATTR_HOQ:
529 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
531 case BHSSC_FLAGS_ATTR_ACA:
532 io->scsiio.tag_type = CTL_TAG_ACA;
535 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
536 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
537 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
540 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
541 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
542 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
543 error = ctl_queue(io);
544 if (error != CTL_RETVAL_COMPLETE) {
545 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
546 "dropping connection", error);
548 refcount_release(&cs->cs_outstanding_ctl_pdus);
549 icl_pdu_free(request);
550 cfiscsi_session_terminate(cs);
555 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
557 struct iscsi_bhs_task_management_request *bhstmr;
558 struct iscsi_bhs_task_management_response *bhstmr2;
559 struct icl_pdu *response;
560 struct cfiscsi_session *cs;
564 cs = PDU_SESSION(request);
565 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
566 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
568 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
569 io->io_hdr.io_type = CTL_IO_TASK;
570 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
571 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
572 io->io_hdr.nexus.targ_lun = ctl_decode_lun(be64toh(bhstmr->bhstmr_lun));
573 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
575 switch (bhstmr->bhstmr_function & ~0x80) {
576 case BHSTMR_FUNCTION_ABORT_TASK:
578 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
580 io->taskio.task_action = CTL_TASK_ABORT_TASK;
581 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
583 case BHSTMR_FUNCTION_ABORT_TASK_SET:
585 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
587 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
589 case BHSTMR_FUNCTION_CLEAR_TASK_SET:
591 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_CLEAR_TASK_SET");
593 io->taskio.task_action = CTL_TASK_CLEAR_TASK_SET;
595 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
597 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
599 io->taskio.task_action = CTL_TASK_LUN_RESET;
601 case BHSTMR_FUNCTION_TARGET_WARM_RESET:
603 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
605 io->taskio.task_action = CTL_TASK_TARGET_RESET;
607 case BHSTMR_FUNCTION_TARGET_COLD_RESET:
609 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_COLD_RESET");
611 io->taskio.task_action = CTL_TASK_TARGET_RESET;
613 case BHSTMR_FUNCTION_QUERY_TASK:
615 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK");
617 io->taskio.task_action = CTL_TASK_QUERY_TASK;
618 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
620 case BHSTMR_FUNCTION_QUERY_TASK_SET:
622 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_TASK_SET");
624 io->taskio.task_action = CTL_TASK_QUERY_TASK_SET;
626 case BHSTMR_FUNCTION_I_T_NEXUS_RESET:
628 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_I_T_NEXUS_RESET");
630 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
632 case BHSTMR_FUNCTION_QUERY_ASYNC_EVENT:
634 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_QUERY_ASYNC_EVENT");
636 io->taskio.task_action = CTL_TASK_QUERY_ASYNC_EVENT;
639 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
640 bhstmr->bhstmr_function & ~0x80);
643 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
644 if (response == NULL) {
645 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
646 "dropping connection");
647 icl_pdu_free(request);
648 cfiscsi_session_terminate(cs);
651 bhstmr2 = (struct iscsi_bhs_task_management_response *)
653 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
654 bhstmr2->bhstmr_flags = 0x80;
655 bhstmr2->bhstmr_response =
656 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
657 bhstmr2->bhstmr_initiator_task_tag =
658 bhstmr->bhstmr_initiator_task_tag;
659 icl_pdu_free(request);
660 cfiscsi_pdu_queue(response);
664 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
665 error = ctl_queue(io);
666 if (error != CTL_RETVAL_COMPLETE) {
667 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
668 "dropping connection", error);
670 refcount_release(&cs->cs_outstanding_ctl_pdus);
671 icl_pdu_free(request);
672 cfiscsi_session_terminate(cs);
677 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
679 struct iscsi_bhs_data_out *bhsdo;
680 struct cfiscsi_session *cs;
681 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
682 size_t copy_len, len, off, buffer_offset;
686 cs = PDU_SESSION(request);
688 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
689 ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
690 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
691 ISCSI_BHS_OPCODE_SCSI_COMMAND,
692 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
695 * We're only using fields common for Data-Out and SCSI Command PDUs.
697 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
699 io = cdw->cdw_ctl_io;
700 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
701 ("CTL_FLAG_DATA_IN"));
704 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
705 request->ip_data_len, io->scsiio.kern_total_len);
708 if (io->scsiio.kern_sg_entries > 0) {
709 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
710 ctl_sg_count = io->scsiio.kern_sg_entries;
712 ctl_sglist = &ctl_sg_entry;
713 ctl_sglist->addr = io->scsiio.kern_data_ptr;
714 ctl_sglist->len = io->scsiio.kern_data_len;
718 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
719 ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
720 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
723 len = icl_pdu_data_segment_length(request);
726 * Make sure the offset, as sent by the initiator, matches the offset
727 * we're supposed to be at in the scatter-gather list.
730 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
731 buffer_offset + len <=
732 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
733 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
734 "expected %zd; dropping connection", buffer_offset,
735 (size_t)io->scsiio.kern_rel_offset +
736 (size_t)io->scsiio.ext_data_filled);
737 ctl_set_data_phase_error(&io->scsiio);
738 cfiscsi_session_terminate(cs);
743 * This is the offset within the PDU data segment, as opposed
744 * to buffer_offset, which is the offset within the task (SCSI
747 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
751 * Iterate over the scatter/gather segments, filling them with data
752 * from the PDU data segment. Note that this can get called multiple
753 * times for one SCSI command; the cdw structure holds state for the
754 * scatter/gather list.
757 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
758 ("cdw->cdw_sg_index >= ctl_sg_count"));
759 if (cdw->cdw_sg_len == 0) {
760 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
761 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
763 KASSERT(off <= len, ("len > off"));
764 copy_len = len - off;
765 if (copy_len > cdw->cdw_sg_len)
766 copy_len = cdw->cdw_sg_len;
768 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
769 cdw->cdw_sg_addr += copy_len;
770 cdw->cdw_sg_len -= copy_len;
772 io->scsiio.ext_data_filled += copy_len;
773 io->scsiio.kern_data_resid -= copy_len;
775 if (cdw->cdw_sg_len == 0) {
777 * End of current segment.
779 if (cdw->cdw_sg_index == ctl_sg_count - 1) {
781 * Last segment in scatter/gather list.
790 * End of PDU payload.
798 * In case of unsolicited data, it's possible that the buffer
799 * provided by CTL is smaller than negotiated FirstBurstLength.
800 * Just ignore the superfluous data; will ask for them with R2T
801 * on next call to cfiscsi_datamove().
803 * This obviously can only happen with SCSI Command PDU.
805 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
806 ISCSI_BHS_OPCODE_SCSI_COMMAND)
809 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
810 "expected %zd; dropping connection",
811 icl_pdu_data_segment_length(request), off);
812 ctl_set_data_phase_error(&io->scsiio);
813 cfiscsi_session_terminate(cs);
817 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end &&
818 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
819 CFISCSI_SESSION_WARN(cs, "got the final packet without "
820 "the F flag; flags = 0x%x; dropping connection",
822 ctl_set_data_phase_error(&io->scsiio);
823 cfiscsi_session_terminate(cs);
827 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end &&
828 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
829 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
830 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
831 CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
832 "transmitted size was %zd bytes instead of %d; "
833 "dropping connection",
834 (size_t)io->scsiio.ext_data_filled,
836 ctl_set_data_phase_error(&io->scsiio);
837 cfiscsi_session_terminate(cs);
841 * For SCSI Command PDU, this just means we need to
842 * solicit more data by sending R2T.
848 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
850 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
851 "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
861 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
863 struct iscsi_bhs_data_out *bhsdo;
864 struct cfiscsi_session *cs;
865 struct cfiscsi_data_wait *cdw = NULL;
869 cs = PDU_SESSION(request);
870 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
872 CFISCSI_SESSION_LOCK(cs);
873 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
875 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
876 "ttt 0x%x, itt 0x%x",
877 bhsdo->bhsdo_target_transfer_tag,
878 bhsdo->bhsdo_initiator_task_tag,
879 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
881 if (bhsdo->bhsdo_target_transfer_tag ==
882 cdw->cdw_target_transfer_tag)
885 CFISCSI_SESSION_UNLOCK(cs);
887 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
888 "0x%x, not found; dropping connection",
889 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
890 icl_pdu_free(request);
891 cfiscsi_session_terminate(cs);
895 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) {
896 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with "
897 "DataSN %u, while expected %u; dropping connection",
898 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn);
899 icl_pdu_free(request);
900 cfiscsi_session_terminate(cs);
905 io = cdw->cdw_ctl_io;
906 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
907 ("CTL_FLAG_DATA_IN"));
909 done = cfiscsi_handle_data_segment(request, cdw);
911 CFISCSI_SESSION_LOCK(cs);
912 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
913 CFISCSI_SESSION_UNLOCK(cs);
914 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end ||
915 io->scsiio.ext_data_filled == io->scsiio.kern_data_len);
916 cfiscsi_data_wait_free(cs, cdw);
917 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
919 io->scsiio.be_move_done(io);
921 cfiscsi_datamove_out(io);
924 icl_pdu_free(request);
928 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
930 struct iscsi_bhs_logout_request *bhslr;
931 struct iscsi_bhs_logout_response *bhslr2;
932 struct icl_pdu *response;
933 struct cfiscsi_session *cs;
935 cs = PDU_SESSION(request);
936 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
937 switch (bhslr->bhslr_reason & 0x7f) {
938 case BHSLR_REASON_CLOSE_SESSION:
939 case BHSLR_REASON_CLOSE_CONNECTION:
940 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
941 if (response == NULL) {
942 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
943 icl_pdu_free(request);
944 cfiscsi_session_terminate(cs);
947 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
948 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
949 bhslr2->bhslr_flags = 0x80;
950 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
951 bhslr2->bhslr_initiator_task_tag =
952 bhslr->bhslr_initiator_task_tag;
953 icl_pdu_free(request);
954 cfiscsi_pdu_queue(response);
955 cfiscsi_session_terminate(cs);
957 case BHSLR_REASON_REMOVE_FOR_RECOVERY:
958 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
959 if (response == NULL) {
960 CFISCSI_SESSION_WARN(cs,
961 "failed to allocate memory; dropping connection");
962 icl_pdu_free(request);
963 cfiscsi_session_terminate(cs);
966 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
967 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
968 bhslr2->bhslr_flags = 0x80;
969 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
970 bhslr2->bhslr_initiator_task_tag =
971 bhslr->bhslr_initiator_task_tag;
972 icl_pdu_free(request);
973 cfiscsi_pdu_queue(response);
976 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
977 bhslr->bhslr_reason);
978 icl_pdu_free(request);
979 cfiscsi_session_terminate(cs);
985 cfiscsi_callout(void *context)
988 struct iscsi_bhs_nop_in *bhsni;
989 struct cfiscsi_session *cs;
993 if (cs->cs_terminating)
996 callout_schedule(&cs->cs_callout, 1 * hz);
998 atomic_add_int(&cs->cs_timeout, 1);
1000 #ifdef ICL_KERNEL_PROXY
1001 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1002 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1003 CFISCSI_SESSION_WARN(cs, "login timed out after "
1004 "%d seconds; dropping connection", cs->cs_timeout);
1005 cfiscsi_session_terminate(cs);
1011 if (ping_timeout <= 0) {
1013 * Pings are disabled. Don't send NOP-In in this case;
1014 * user might have disabled pings to work around problems
1015 * with certain initiators that can't properly handle
1016 * NOP-In, such as iPXE. Reset the timeout, to avoid
1017 * triggering reconnection, should the user decide to
1024 if (cs->cs_timeout >= ping_timeout) {
1025 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1026 "dropping connection", ping_timeout);
1027 cfiscsi_session_terminate(cs);
1032 * If the ping was reset less than one second ago - which means
1033 * that we've received some PDU during the last second - assume
1034 * the traffic flows correctly and don't bother sending a NOP-Out.
1036 * (It's 2 - one for one second, and one for incrementing is_timeout
1037 * earlier in this routine.)
1039 if (cs->cs_timeout < 2)
1042 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1044 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1047 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1048 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1049 bhsni->bhsni_flags = 0x80;
1050 bhsni->bhsni_initiator_task_tag = 0xffffffff;
1052 cfiscsi_pdu_queue(cp);
1055 static struct cfiscsi_data_wait *
1056 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1057 uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1059 struct cfiscsi_data_wait *cdw;
1062 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1064 CFISCSI_SESSION_WARN(cs,
1065 "failed to allocate %zd bytes", sizeof(*cdw));
1069 error = icl_conn_transfer_setup(cs->cs_conn, io, target_transfer_tagp,
1072 CFISCSI_SESSION_WARN(cs,
1073 "icl_conn_transfer_setup() failed with error %d", error);
1074 uma_zfree(cfiscsi_data_wait_zone, cdw);
1078 cdw->cdw_ctl_io = io;
1079 cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1080 cdw->cdw_initiator_task_tag = initiator_task_tag;
1086 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1087 struct cfiscsi_data_wait *cdw)
1090 icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1091 uma_zfree(cfiscsi_data_wait_zone, cdw);
1095 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1097 struct cfiscsi_data_wait *cdw;
1099 int error, last, wait;
1101 if (cs->cs_target == NULL)
1102 return; /* No target yet, so nothing to do. */
1103 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1105 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1106 io->io_hdr.io_type = CTL_IO_TASK;
1107 io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1108 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1109 io->io_hdr.nexus.targ_lun = 0;
1110 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1111 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1112 wait = cs->cs_outstanding_ctl_pdus;
1113 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1114 error = ctl_queue(io);
1115 if (error != CTL_RETVAL_COMPLETE) {
1116 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1117 refcount_release(&cs->cs_outstanding_ctl_pdus);
1121 CFISCSI_SESSION_LOCK(cs);
1122 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1123 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1124 CFISCSI_SESSION_UNLOCK(cs);
1126 * Set nonzero port status; this prevents backends from
1127 * assuming that the data transfer actually succeeded
1128 * and writing uninitialized data to disk.
1130 cdw->cdw_ctl_io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1131 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1132 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1133 cfiscsi_data_wait_free(cs, cdw);
1134 CFISCSI_SESSION_LOCK(cs);
1136 CFISCSI_SESSION_UNLOCK(cs);
1139 * Wait for CTL to terminate all the tasks.
1142 CFISCSI_SESSION_WARN(cs,
1143 "waiting for CTL to terminate %d tasks", wait);
1145 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1146 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1149 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1150 0, "cfiscsi_terminate", hz / 100);
1153 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1157 cfiscsi_maintenance_thread(void *arg)
1159 struct cfiscsi_session *cs;
1164 CFISCSI_SESSION_LOCK(cs);
1165 if (cs->cs_terminating == false)
1166 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1167 CFISCSI_SESSION_UNLOCK(cs);
1169 if (cs->cs_terminating) {
1172 * We used to wait up to 30 seconds to deliver queued
1173 * PDUs to the initiator. We also tried hard to deliver
1174 * SCSI Responses for the aborted PDUs. We don't do
1175 * that anymore. We might need to revisit that.
1177 callout_drain(&cs->cs_callout);
1178 icl_conn_close(cs->cs_conn);
1181 * At this point ICL receive thread is no longer
1182 * running; no new tasks can be queued.
1184 cfiscsi_session_terminate_tasks(cs);
1185 cfiscsi_session_delete(cs);
1189 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1194 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1197 if (cs->cs_terminating)
1199 cs->cs_terminating = true;
1200 cv_signal(&cs->cs_maintenance_cv);
1201 #ifdef ICL_KERNEL_PROXY
1202 cv_signal(&cs->cs_login_cv);
1207 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1209 struct cfiscsi_target *ct;
1213 KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1216 name = strdup(cs->cs_initiator_id, M_CTL);
1217 i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1219 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1221 cs->cs_ctl_initid = -1;
1224 cs->cs_ctl_initid = i;
1226 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1233 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1237 if (cs->cs_ctl_initid == -1)
1240 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1242 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1245 cs->cs_ctl_initid = -1;
1248 static struct cfiscsi_session *
1249 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1251 struct cfiscsi_session *cs;
1254 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1256 CFISCSI_WARN("malloc failed");
1259 cs->cs_ctl_initid = -1;
1261 refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1262 TAILQ_INIT(&cs->cs_waiting_for_data_out);
1263 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1264 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1265 #ifdef ICL_KERNEL_PROXY
1266 cv_init(&cs->cs_login_cv, "cfiscsi_login");
1269 cs->cs_conn = icl_new_conn(offload, false, "cfiscsi", &cs->cs_lock);
1270 if (cs->cs_conn == NULL) {
1271 free(cs, M_CFISCSI);
1274 cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1275 cs->cs_conn->ic_error = cfiscsi_error_callback;
1276 cs->cs_conn->ic_prv0 = cs;
1278 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1280 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1281 free(cs, M_CFISCSI);
1285 mtx_lock(&softc->lock);
1286 cs->cs_id = ++softc->last_session_id;
1287 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1288 mtx_unlock(&softc->lock);
1291 * Start pinging the initiator.
1293 callout_init(&cs->cs_callout, 1);
1294 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1300 cfiscsi_session_delete(struct cfiscsi_session *cs)
1302 struct cfiscsi_softc *softc;
1304 softc = &cfiscsi_softc;
1306 KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1307 ("destroying session with outstanding CTL pdus"));
1308 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1309 ("destroying session with non-empty queue"));
1311 cfiscsi_session_unregister_initiator(cs);
1312 if (cs->cs_target != NULL)
1313 cfiscsi_target_release(cs->cs_target);
1314 icl_conn_close(cs->cs_conn);
1315 icl_conn_free(cs->cs_conn);
1317 mtx_lock(&softc->lock);
1318 TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1319 cv_signal(&softc->sessions_cv);
1320 mtx_unlock(&softc->lock);
1322 free(cs, M_CFISCSI);
1328 struct cfiscsi_softc *softc;
1330 softc = &cfiscsi_softc;
1331 bzero(softc, sizeof(*softc));
1332 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1334 cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1335 #ifdef ICL_KERNEL_PROXY
1336 cv_init(&softc->accept_cv, "cfiscsi_accept");
1338 TAILQ_INIT(&softc->sessions);
1339 TAILQ_INIT(&softc->targets);
1341 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1342 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1349 cfiscsi_shutdown(void)
1351 struct cfiscsi_softc *softc = &cfiscsi_softc;
1353 if (!TAILQ_EMPTY(&softc->sessions) || !TAILQ_EMPTY(&softc->targets))
1356 uma_zdestroy(cfiscsi_data_wait_zone);
1357 #ifdef ICL_KERNEL_PROXY
1358 cv_destroy(&softc->accept_cv);
1360 cv_destroy(&softc->sessions_cv);
1361 mtx_destroy(&softc->lock);
1365 #ifdef ICL_KERNEL_PROXY
1367 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1369 struct cfiscsi_session *cs;
1371 cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1373 CFISCSI_WARN("failed to create session");
1377 icl_conn_handoff_sock(cs->cs_conn, so);
1378 cs->cs_initiator_sa = sa;
1379 cs->cs_portal_id = portal_id;
1380 cs->cs_waiting_for_ctld = true;
1381 cv_signal(&cfiscsi_softc.accept_cv);
1386 cfiscsi_online(void *arg)
1388 struct cfiscsi_softc *softc;
1389 struct cfiscsi_target *ct;
1392 ct = (struct cfiscsi_target *)arg;
1393 softc = ct->ct_softc;
1395 mtx_lock(&softc->lock);
1396 if (ct->ct_online) {
1397 mtx_unlock(&softc->lock);
1401 online = softc->online++;
1402 mtx_unlock(&softc->lock);
1406 #ifdef ICL_KERNEL_PROXY
1407 if (softc->listener != NULL)
1408 icl_listen_free(softc->listener);
1409 softc->listener = icl_listen_new(cfiscsi_accept);
1414 cfiscsi_offline(void *arg)
1416 struct cfiscsi_softc *softc;
1417 struct cfiscsi_target *ct;
1418 struct cfiscsi_session *cs;
1421 ct = (struct cfiscsi_target *)arg;
1422 softc = ct->ct_softc;
1424 mtx_lock(&softc->lock);
1425 if (!ct->ct_online) {
1426 mtx_unlock(&softc->lock);
1430 online = --softc->online;
1432 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1433 if (cs->cs_target == ct)
1434 cfiscsi_session_terminate(cs);
1437 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1438 if (cs->cs_target == ct)
1442 cv_wait(&softc->sessions_cv, &softc->lock);
1443 } while (cs != NULL && ct->ct_online == 0);
1444 mtx_unlock(&softc->lock);
1448 #ifdef ICL_KERNEL_PROXY
1449 icl_listen_free(softc->listener);
1450 softc->listener = NULL;
1455 cfiscsi_info(void *arg, struct sbuf *sb)
1457 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1460 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1466 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1468 struct cfiscsi_softc *softc;
1469 struct cfiscsi_session *cs, *cs2;
1470 struct cfiscsi_target *ct;
1471 struct ctl_iscsi_handoff_params *cihp;
1474 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1475 softc = &cfiscsi_softc;
1477 CFISCSI_DEBUG("new connection from %s (%s) to %s",
1478 cihp->initiator_name, cihp->initiator_addr,
1481 ct = cfiscsi_target_find(softc, cihp->target_name,
1482 cihp->portal_group_tag);
1484 ci->status = CTL_ISCSI_ERROR;
1485 snprintf(ci->error_str, sizeof(ci->error_str),
1486 "%s: target not found", __func__);
1490 #ifdef ICL_KERNEL_PROXY
1491 if (cihp->socket > 0 && cihp->connection_id > 0) {
1492 snprintf(ci->error_str, sizeof(ci->error_str),
1493 "both socket and connection_id set");
1494 ci->status = CTL_ISCSI_ERROR;
1495 cfiscsi_target_release(ct);
1498 if (cihp->socket == 0) {
1499 mtx_lock(&cfiscsi_softc.lock);
1500 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1501 if (cs->cs_id == cihp->connection_id)
1505 mtx_unlock(&cfiscsi_softc.lock);
1506 snprintf(ci->error_str, sizeof(ci->error_str),
1507 "connection not found");
1508 ci->status = CTL_ISCSI_ERROR;
1509 cfiscsi_target_release(ct);
1512 mtx_unlock(&cfiscsi_softc.lock);
1515 cs = cfiscsi_session_new(softc, cihp->offload);
1517 ci->status = CTL_ISCSI_ERROR;
1518 snprintf(ci->error_str, sizeof(ci->error_str),
1519 "%s: cfiscsi_session_new failed", __func__);
1520 cfiscsi_target_release(ct);
1523 #ifdef ICL_KERNEL_PROXY
1528 * First PDU of Full Feature phase has the same CmdSN as the last
1529 * PDU from the Login Phase received from the initiator. Thus,
1532 cs->cs_cmdsn = cihp->cmdsn;
1533 cs->cs_statsn = cihp->statsn;
1534 cs->cs_max_recv_data_segment_length = cihp->max_recv_data_segment_length;
1535 cs->cs_max_send_data_segment_length = cihp->max_send_data_segment_length;
1536 cs->cs_max_burst_length = cihp->max_burst_length;
1537 cs->cs_first_burst_length = cihp->first_burst_length;
1538 cs->cs_immediate_data = !!cihp->immediate_data;
1539 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1540 cs->cs_conn->ic_header_crc32c = true;
1541 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1542 cs->cs_conn->ic_data_crc32c = true;
1544 strlcpy(cs->cs_initiator_name,
1545 cihp->initiator_name, sizeof(cs->cs_initiator_name));
1546 strlcpy(cs->cs_initiator_addr,
1547 cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1548 strlcpy(cs->cs_initiator_alias,
1549 cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1550 memcpy(cs->cs_initiator_isid,
1551 cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1552 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1553 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1554 cihp->initiator_isid[0], cihp->initiator_isid[1],
1555 cihp->initiator_isid[2], cihp->initiator_isid[3],
1556 cihp->initiator_isid[4], cihp->initiator_isid[5]);
1558 mtx_lock(&softc->lock);
1559 if (ct->ct_online == 0) {
1560 mtx_unlock(&softc->lock);
1561 cfiscsi_session_terminate(cs);
1562 cfiscsi_target_release(ct);
1563 ci->status = CTL_ISCSI_ERROR;
1564 snprintf(ci->error_str, sizeof(ci->error_str),
1565 "%s: port offline", __func__);
1569 mtx_unlock(&softc->lock);
1571 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1573 if (!cs->cs_terminating) {
1574 mtx_lock(&softc->lock);
1575 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1576 if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1577 cs->cs_target == cs2->cs_target &&
1578 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1579 if (strcmp(cs->cs_initiator_addr,
1580 cs2->cs_initiator_addr) != 0) {
1581 CFISCSI_SESSION_WARN(cs2,
1582 "session reinstatement from "
1583 "different address %s",
1584 cs->cs_initiator_addr);
1586 CFISCSI_SESSION_DEBUG(cs2,
1587 "session reinstatement");
1589 cfiscsi_session_terminate(cs2);
1590 mtx_unlock(&softc->lock);
1591 pause("cfiscsi_reinstate", 1);
1595 mtx_unlock(&softc->lock);
1599 * Register initiator with CTL.
1601 cfiscsi_session_register_initiator(cs);
1603 #ifdef ICL_KERNEL_PROXY
1604 if (cihp->socket > 0) {
1606 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1608 cfiscsi_session_terminate(cs);
1609 refcount_release(&cs->cs_outstanding_ctl_pdus);
1610 ci->status = CTL_ISCSI_ERROR;
1611 snprintf(ci->error_str, sizeof(ci->error_str),
1612 "%s: icl_conn_handoff failed with error %d",
1616 #ifdef ICL_KERNEL_PROXY
1620 #ifdef ICL_KERNEL_PROXY
1621 cs->cs_login_phase = false;
1624 * First PDU of the Full Feature phase has likely already arrived.
1625 * We have to pick it up and execute properly.
1627 if (cs->cs_login_pdu != NULL) {
1628 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1629 cfiscsi_pdu_handle(cs->cs_login_pdu);
1630 cs->cs_login_pdu = NULL;
1634 refcount_release(&cs->cs_outstanding_ctl_pdus);
1635 ci->status = CTL_ISCSI_OK;
1639 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1641 struct ctl_iscsi_list_params *cilp;
1642 struct cfiscsi_session *cs;
1643 struct cfiscsi_softc *softc;
1647 cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1648 softc = &cfiscsi_softc;
1650 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1652 ci->status = CTL_ISCSI_ERROR;
1653 snprintf(ci->error_str, sizeof(ci->error_str),
1654 "Unable to allocate %d bytes for iSCSI session list",
1659 sbuf_printf(sb, "<ctlislist>\n");
1660 mtx_lock(&softc->lock);
1661 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1662 #ifdef ICL_KERNEL_PROXY
1663 if (cs->cs_target == NULL)
1666 error = sbuf_printf(sb, "<connection id=\"%d\">"
1667 "<initiator>%s</initiator>"
1668 "<initiator_addr>%s</initiator_addr>"
1669 "<initiator_alias>%s</initiator_alias>"
1670 "<target>%s</target>"
1671 "<target_alias>%s</target_alias>"
1672 "<target_portal_group_tag>%u</target_portal_group_tag>"
1673 "<header_digest>%s</header_digest>"
1674 "<data_digest>%s</data_digest>"
1675 "<max_recv_data_segment_length>%d</max_recv_data_segment_length>"
1676 "<max_send_data_segment_length>%d</max_send_data_segment_length>"
1677 "<max_burst_length>%d</max_burst_length>"
1678 "<first_burst_length>%d</first_burst_length>"
1679 "<immediate_data>%d</immediate_data>"
1681 "<offload>%s</offload>"
1684 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1685 cs->cs_target->ct_name, cs->cs_target->ct_alias,
1686 cs->cs_target->ct_tag,
1687 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1688 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1689 cs->cs_max_recv_data_segment_length,
1690 cs->cs_max_send_data_segment_length,
1691 cs->cs_max_burst_length,
1692 cs->cs_first_burst_length,
1693 cs->cs_immediate_data,
1694 cs->cs_conn->ic_iser,
1695 cs->cs_conn->ic_offload);
1699 mtx_unlock(&softc->lock);
1700 error = sbuf_printf(sb, "</ctlislist>\n");
1703 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1704 snprintf(ci->error_str, sizeof(ci->error_str),
1705 "Out of space, %d bytes is too small", cilp->alloc_len);
1710 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1713 snprintf(ci->error_str, sizeof(ci->error_str),
1714 "copyout failed with error %d", error);
1715 ci->status = CTL_ISCSI_ERROR;
1718 cilp->fill_len = sbuf_len(sb) + 1;
1719 ci->status = CTL_ISCSI_OK;
1724 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1726 struct icl_pdu *response;
1727 struct iscsi_bhs_asynchronous_message *bhsam;
1728 struct ctl_iscsi_logout_params *cilp;
1729 struct cfiscsi_session *cs;
1730 struct cfiscsi_softc *softc;
1733 cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1734 softc = &cfiscsi_softc;
1736 mtx_lock(&softc->lock);
1737 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1738 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1739 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1740 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1743 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1744 if (response == NULL) {
1745 ci->status = CTL_ISCSI_ERROR;
1746 snprintf(ci->error_str, sizeof(ci->error_str),
1747 "Unable to allocate memory");
1748 mtx_unlock(&softc->lock);
1752 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1753 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1754 bhsam->bhsam_flags = 0x80;
1755 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1756 bhsam->bhsam_parameter3 = htons(10);
1757 cfiscsi_pdu_queue(response);
1760 mtx_unlock(&softc->lock);
1763 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1764 snprintf(ci->error_str, sizeof(ci->error_str),
1765 "No matching connections found");
1769 ci->status = CTL_ISCSI_OK;
1773 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1775 struct icl_pdu *response;
1776 struct iscsi_bhs_asynchronous_message *bhsam;
1777 struct ctl_iscsi_terminate_params *citp;
1778 struct cfiscsi_session *cs;
1779 struct cfiscsi_softc *softc;
1782 citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1783 softc = &cfiscsi_softc;
1785 mtx_lock(&softc->lock);
1786 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1787 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1788 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1789 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1792 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1793 if (response == NULL) {
1795 * Oh well. Just terminate the connection.
1798 bhsam = (struct iscsi_bhs_asynchronous_message *)
1800 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1801 bhsam->bhsam_flags = 0x80;
1802 bhsam->bhsam_0xffffffff = 0xffffffff;
1803 bhsam->bhsam_async_event =
1804 BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1805 cfiscsi_pdu_queue(response);
1807 cfiscsi_session_terminate(cs);
1810 mtx_unlock(&softc->lock);
1813 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1814 snprintf(ci->error_str, sizeof(ci->error_str),
1815 "No matching connections found");
1819 ci->status = CTL_ISCSI_OK;
1823 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1825 struct ctl_iscsi_limits_params *cilp;
1826 struct icl_drv_limits idl;
1829 cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1831 error = icl_limits(cilp->offload, false, &idl);
1833 ci->status = CTL_ISCSI_ERROR;
1834 snprintf(ci->error_str, sizeof(ci->error_str),
1835 "%s: icl_limits failed with error %d",
1840 cilp->max_recv_data_segment_length =
1841 idl.idl_max_recv_data_segment_length;
1842 cilp->max_send_data_segment_length =
1843 idl.idl_max_send_data_segment_length;
1844 cilp->max_burst_length = idl.idl_max_burst_length;
1845 cilp->first_burst_length = idl.idl_first_burst_length;
1847 ci->status = CTL_ISCSI_OK;
1850 #ifdef ICL_KERNEL_PROXY
1852 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1854 struct ctl_iscsi_listen_params *cilp;
1855 struct sockaddr *sa;
1858 cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1860 if (cfiscsi_softc.listener == NULL) {
1861 CFISCSI_DEBUG("no listener");
1862 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1863 ci->status = CTL_ISCSI_ERROR;
1867 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1869 CFISCSI_DEBUG("getsockaddr, error %d", error);
1870 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1871 ci->status = CTL_ISCSI_ERROR;
1875 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1876 cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1879 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1880 snprintf(ci->error_str, sizeof(ci->error_str),
1881 "icl_listen_add failed, error %d", error);
1882 ci->status = CTL_ISCSI_ERROR;
1886 ci->status = CTL_ISCSI_OK;
1890 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1892 struct ctl_iscsi_accept_params *ciap;
1893 struct cfiscsi_session *cs;
1896 ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1898 mtx_lock(&cfiscsi_softc.lock);
1900 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1901 if (cs->cs_waiting_for_ctld)
1906 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1908 mtx_unlock(&cfiscsi_softc.lock);
1909 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1910 ci->status = CTL_ISCSI_ERROR;
1914 mtx_unlock(&cfiscsi_softc.lock);
1916 cs->cs_waiting_for_ctld = false;
1917 cs->cs_login_phase = true;
1919 ciap->connection_id = cs->cs_id;
1920 ciap->portal_id = cs->cs_portal_id;
1921 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1922 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1923 cs->cs_initiator_sa->sa_len);
1925 snprintf(ci->error_str, sizeof(ci->error_str),
1926 "copyout failed with error %d", error);
1927 ci->status = CTL_ISCSI_ERROR;
1931 ci->status = CTL_ISCSI_OK;
1935 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1937 struct ctl_iscsi_send_params *cisp;
1938 struct cfiscsi_session *cs;
1944 cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1946 mtx_lock(&cfiscsi_softc.lock);
1947 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1948 if (cs->cs_id == cisp->connection_id)
1952 mtx_unlock(&cfiscsi_softc.lock);
1953 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1954 ci->status = CTL_ISCSI_ERROR;
1957 mtx_unlock(&cfiscsi_softc.lock);
1960 if (cs->cs_login_phase == false)
1964 if (cs->cs_terminating) {
1965 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1966 ci->status = CTL_ISCSI_ERROR;
1970 datalen = cisp->data_segment_len;
1974 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1975 if (datalen > 65535) {
1976 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1977 ci->status = CTL_ISCSI_ERROR;
1981 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1982 error = copyin(cisp->data_segment, data, datalen);
1984 free(data, M_CFISCSI);
1985 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1986 ci->status = CTL_ISCSI_ERROR;
1991 ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1992 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1994 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1995 free(data, M_CFISCSI);
1997 CFISCSI_SESSION_LOCK(cs);
1999 CFISCSI_SESSION_UNLOCK(cs);
2000 ci->status = CTL_ISCSI_OK;
2004 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
2006 struct ctl_iscsi_receive_params *cirp;
2007 struct cfiscsi_session *cs;
2012 cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
2014 mtx_lock(&cfiscsi_softc.lock);
2015 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
2016 if (cs->cs_id == cirp->connection_id)
2020 mtx_unlock(&cfiscsi_softc.lock);
2021 snprintf(ci->error_str, sizeof(ci->error_str),
2022 "connection not found");
2023 ci->status = CTL_ISCSI_ERROR;
2026 mtx_unlock(&cfiscsi_softc.lock);
2029 if (is->is_login_phase == false)
2033 CFISCSI_SESSION_LOCK(cs);
2034 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2035 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2037 CFISCSI_SESSION_UNLOCK(cs);
2038 snprintf(ci->error_str, sizeof(ci->error_str),
2039 "interrupted by signal");
2040 ci->status = CTL_ISCSI_ERROR;
2045 if (cs->cs_terminating) {
2046 CFISCSI_SESSION_UNLOCK(cs);
2047 snprintf(ci->error_str, sizeof(ci->error_str),
2048 "connection terminating");
2049 ci->status = CTL_ISCSI_ERROR;
2052 ip = cs->cs_login_pdu;
2053 cs->cs_login_pdu = NULL;
2054 CFISCSI_SESSION_UNLOCK(cs);
2056 if (ip->ip_data_len > cirp->data_segment_len) {
2058 snprintf(ci->error_str, sizeof(ci->error_str),
2059 "data segment too big");
2060 ci->status = CTL_ISCSI_ERROR;
2064 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2065 if (ip->ip_data_len > 0) {
2066 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2067 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2068 copyout(data, cirp->data_segment, ip->ip_data_len);
2069 free(data, M_CFISCSI);
2073 ci->status = CTL_ISCSI_OK;
2076 #endif /* !ICL_KERNEL_PROXY */
2079 cfiscsi_ioctl_port_create(struct ctl_req *req)
2081 struct cfiscsi_target *ct;
2082 struct ctl_port *port;
2083 const char *target, *alias, *tags;
2084 struct scsi_vpd_id_descriptor *desc;
2086 int retval, len, idlen;
2089 ctl_init_opts(&opts, req->num_args, req->kern_args);
2090 target = ctl_get_opt(&opts, "cfiscsi_target");
2091 alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
2092 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2093 if (target == NULL || tags == NULL) {
2094 req->status = CTL_LUN_ERROR;
2095 snprintf(req->error_str, sizeof(req->error_str),
2096 "Missing required argument");
2097 ctl_free_opts(&opts);
2100 tag = strtol(tags, (char **)NULL, 10);
2101 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2103 req->status = CTL_LUN_ERROR;
2104 snprintf(req->error_str, sizeof(req->error_str),
2105 "failed to create target \"%s\"", target);
2106 ctl_free_opts(&opts);
2109 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2110 req->status = CTL_LUN_ERROR;
2111 snprintf(req->error_str, sizeof(req->error_str),
2112 "target \"%s\" for portal group tag %u already exists",
2114 cfiscsi_target_release(ct);
2115 ctl_free_opts(&opts);
2118 port = &ct->ct_port;
2120 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2123 port->frontend = &cfiscsi_frontend;
2124 port->port_type = CTL_PORT_ISCSI;
2125 /* XXX KDM what should the real number be here? */
2126 port->num_requested_ctl_io = 4096;
2127 port->port_name = "iscsi";
2128 port->physical_port = tag;
2129 port->virtual_port = ct->ct_target_id;
2130 port->port_online = cfiscsi_online;
2131 port->port_offline = cfiscsi_offline;
2132 port->port_info = cfiscsi_info;
2133 port->onoff_arg = ct;
2134 port->fe_datamove = cfiscsi_datamove;
2135 port->fe_done = cfiscsi_done;
2136 port->targ_port = -1;
2138 port->options = opts;
2141 /* Generate Port ID. */
2142 idlen = strlen(target) + strlen(",t,0x0001") + 1;
2143 idlen = roundup2(idlen, 4);
2144 len = sizeof(struct scsi_vpd_device_id) + idlen;
2145 port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2146 M_CTL, M_WAITOK | M_ZERO);
2147 port->port_devid->len = len;
2148 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2149 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2150 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2151 SVPD_ID_TYPE_SCSI_NAME;
2152 desc->length = idlen;
2153 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2155 /* Generate Target ID. */
2156 idlen = strlen(target) + 1;
2157 idlen = roundup2(idlen, 4);
2158 len = sizeof(struct scsi_vpd_device_id) + idlen;
2159 port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2160 M_CTL, M_WAITOK | M_ZERO);
2161 port->target_devid->len = len;
2162 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2163 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2164 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2165 SVPD_ID_TYPE_SCSI_NAME;
2166 desc->length = idlen;
2167 strlcpy(desc->identifier, target, idlen);
2169 retval = ctl_port_register(port);
2171 ctl_free_opts(&port->options);
2172 free(port->port_devid, M_CFISCSI);
2173 free(port->target_devid, M_CFISCSI);
2174 cfiscsi_target_release(ct);
2175 req->status = CTL_LUN_ERROR;
2176 snprintf(req->error_str, sizeof(req->error_str),
2177 "ctl_port_register() failed with error %d", retval);
2181 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2182 req->status = CTL_LUN_OK;
2183 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2184 sizeof(port->targ_port)); //XXX
2188 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2190 struct cfiscsi_target *ct;
2191 const char *target, *tags;
2195 ctl_init_opts(&opts, req->num_args, req->kern_args);
2196 target = ctl_get_opt(&opts, "cfiscsi_target");
2197 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2198 if (target == NULL || tags == NULL) {
2199 ctl_free_opts(&opts);
2200 req->status = CTL_LUN_ERROR;
2201 snprintf(req->error_str, sizeof(req->error_str),
2202 "Missing required argument");
2205 tag = strtol(tags, (char **)NULL, 10);
2206 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2208 ctl_free_opts(&opts);
2209 req->status = CTL_LUN_ERROR;
2210 snprintf(req->error_str, sizeof(req->error_str),
2211 "can't find target \"%s\"", target);
2214 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2215 ctl_free_opts(&opts);
2216 req->status = CTL_LUN_ERROR;
2217 snprintf(req->error_str, sizeof(req->error_str),
2218 "target \"%s\" is already dying", target);
2221 ctl_free_opts(&opts);
2223 ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2224 ctl_port_offline(&ct->ct_port);
2225 cfiscsi_target_release(ct);
2226 cfiscsi_target_release(ct);
2227 req->status = CTL_LUN_OK;
2231 cfiscsi_ioctl(struct cdev *dev,
2232 u_long cmd, caddr_t addr, int flag, struct thread *td)
2234 struct ctl_iscsi *ci;
2235 struct ctl_req *req;
2237 if (cmd == CTL_PORT_REQ) {
2238 req = (struct ctl_req *)addr;
2239 switch (req->reqtype) {
2240 case CTL_REQ_CREATE:
2241 cfiscsi_ioctl_port_create(req);
2243 case CTL_REQ_REMOVE:
2244 cfiscsi_ioctl_port_remove(req);
2247 req->status = CTL_LUN_ERROR;
2248 snprintf(req->error_str, sizeof(req->error_str),
2249 "Unsupported request type %d", req->reqtype);
2254 if (cmd != CTL_ISCSI)
2257 ci = (struct ctl_iscsi *)addr;
2259 case CTL_ISCSI_HANDOFF:
2260 cfiscsi_ioctl_handoff(ci);
2262 case CTL_ISCSI_LIST:
2263 cfiscsi_ioctl_list(ci);
2265 case CTL_ISCSI_LOGOUT:
2266 cfiscsi_ioctl_logout(ci);
2268 case CTL_ISCSI_TERMINATE:
2269 cfiscsi_ioctl_terminate(ci);
2271 case CTL_ISCSI_LIMITS:
2272 cfiscsi_ioctl_limits(ci);
2274 #ifdef ICL_KERNEL_PROXY
2275 case CTL_ISCSI_LISTEN:
2276 cfiscsi_ioctl_listen(ci);
2278 case CTL_ISCSI_ACCEPT:
2279 cfiscsi_ioctl_accept(ci);
2281 case CTL_ISCSI_SEND:
2282 cfiscsi_ioctl_send(ci);
2284 case CTL_ISCSI_RECEIVE:
2285 cfiscsi_ioctl_receive(ci);
2288 case CTL_ISCSI_LISTEN:
2289 case CTL_ISCSI_ACCEPT:
2290 case CTL_ISCSI_SEND:
2291 case CTL_ISCSI_RECEIVE:
2292 ci->status = CTL_ISCSI_ERROR;
2293 snprintf(ci->error_str, sizeof(ci->error_str),
2294 "%s: CTL compiled without ICL_KERNEL_PROXY",
2297 #endif /* !ICL_KERNEL_PROXY */
2299 ci->status = CTL_ISCSI_ERROR;
2300 snprintf(ci->error_str, sizeof(ci->error_str),
2301 "%s: invalid iSCSI request type %d", __func__, ci->type);
2309 cfiscsi_target_hold(struct cfiscsi_target *ct)
2312 refcount_acquire(&ct->ct_refcount);
2316 cfiscsi_target_release(struct cfiscsi_target *ct)
2318 struct cfiscsi_softc *softc;
2320 softc = ct->ct_softc;
2321 mtx_lock(&softc->lock);
2322 if (refcount_release(&ct->ct_refcount)) {
2323 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2324 mtx_unlock(&softc->lock);
2325 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2326 ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2327 if (ctl_port_deregister(&ct->ct_port) != 0)
2328 printf("%s: ctl_port_deregister() failed\n",
2331 free(ct, M_CFISCSI);
2335 mtx_unlock(&softc->lock);
2338 static struct cfiscsi_target *
2339 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2341 struct cfiscsi_target *ct;
2343 mtx_lock(&softc->lock);
2344 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2345 if (ct->ct_tag != tag ||
2346 strcmp(name, ct->ct_name) != 0 ||
2347 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2349 cfiscsi_target_hold(ct);
2350 mtx_unlock(&softc->lock);
2353 mtx_unlock(&softc->lock);
2358 static struct cfiscsi_target *
2359 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2360 const char *alias, uint16_t tag)
2362 struct cfiscsi_target *ct, *newct;
2364 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2367 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2369 mtx_lock(&softc->lock);
2370 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2371 if (ct->ct_tag != tag ||
2372 strcmp(name, ct->ct_name) != 0 ||
2373 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2375 cfiscsi_target_hold(ct);
2376 mtx_unlock(&softc->lock);
2377 free(newct, M_CFISCSI);
2381 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2383 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2384 newct->ct_tag = tag;
2385 refcount_init(&newct->ct_refcount, 1);
2386 newct->ct_softc = softc;
2387 if (TAILQ_EMPTY(&softc->targets))
2388 softc->last_target_id = 0;
2389 newct->ct_target_id = ++softc->last_target_id;
2390 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2391 mtx_unlock(&softc->lock);
2397 cfiscsi_datamove_in(union ctl_io *io)
2399 struct cfiscsi_session *cs;
2400 struct icl_pdu *request, *response;
2401 const struct iscsi_bhs_scsi_command *bhssc;
2402 struct iscsi_bhs_data_in *bhsdi;
2403 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2404 size_t len, expected_len, sg_len, buffer_offset;
2405 const char *sg_addr;
2406 int ctl_sg_count, error, i;
2408 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2409 cs = PDU_SESSION(request);
2411 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2412 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2413 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2414 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2416 if (io->scsiio.kern_sg_entries > 0) {
2417 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2418 ctl_sg_count = io->scsiio.kern_sg_entries;
2420 ctl_sglist = &ctl_sg_entry;
2421 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2422 ctl_sglist->len = io->scsiio.kern_data_len;
2427 * This is the total amount of data to be transferred within the current
2428 * SCSI command. We need to record it so that we can properly report
2429 * underflow/underflow.
2431 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2434 * This is the offset within the current SCSI command; for the first
2435 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2436 * it will be the sum of lengths of previous ones.
2438 buffer_offset = io->scsiio.kern_rel_offset;
2441 * This is the transfer length expected by the initiator. In theory,
2442 * it could be different from the correct amount of data from the SCSI
2443 * point of view, even if that doesn't make any sense.
2445 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2447 if (expected_len != io->scsiio.kern_total_len) {
2448 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2449 "actual length %zd", expected_len,
2450 (size_t)io->scsiio.kern_total_len);
2454 if (buffer_offset >= expected_len) {
2456 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2457 "already sent the expected len", buffer_offset);
2459 io->scsiio.be_move_done(io);
2469 if (response == NULL) {
2470 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2471 if (response == NULL) {
2472 CFISCSI_SESSION_WARN(cs, "failed to "
2473 "allocate memory; dropping connection");
2474 ctl_set_busy(&io->scsiio);
2475 io->scsiio.be_move_done(io);
2476 cfiscsi_session_terminate(cs);
2479 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2480 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2481 bhsdi->bhsdi_initiator_task_tag =
2482 bhssc->bhssc_initiator_task_tag;
2483 bhsdi->bhsdi_target_transfer_tag = 0xffffffff;
2484 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2485 PDU_EXPDATASN(request)++;
2486 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2489 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2491 sg_addr = ctl_sglist[i].addr;
2492 sg_len = ctl_sglist[i].len;
2493 KASSERT(sg_len > 0, ("sg_len <= 0"));
2499 * Truncate to maximum data segment length.
2501 KASSERT(response->ip_data_len < cs->cs_max_send_data_segment_length,
2502 ("ip_data_len %zd >= max_send_data_segment_length %d",
2503 response->ip_data_len, cs->cs_max_send_data_segment_length));
2504 if (response->ip_data_len + len >
2505 cs->cs_max_send_data_segment_length) {
2506 len = cs->cs_max_send_data_segment_length -
2507 response->ip_data_len;
2508 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2513 * Truncate to expected data transfer length.
2515 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2516 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2517 buffer_offset, response->ip_data_len, expected_len));
2518 if (buffer_offset + response->ip_data_len + len > expected_len) {
2519 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2520 "to expected data transfer length %zd",
2521 buffer_offset + response->ip_data_len + len, expected_len);
2522 len = expected_len - (buffer_offset + response->ip_data_len);
2523 KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2527 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2529 CFISCSI_SESSION_WARN(cs, "failed to "
2530 "allocate memory; dropping connection");
2531 icl_pdu_free(response);
2532 ctl_set_busy(&io->scsiio);
2533 io->scsiio.be_move_done(io);
2534 cfiscsi_session_terminate(cs);
2539 io->scsiio.kern_data_resid -= len;
2541 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2542 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2543 buffer_offset, response->ip_data_len, expected_len));
2544 if (buffer_offset + response->ip_data_len == expected_len) {
2546 * Already have the amount of data the initiator wanted.
2553 * End of scatter-gather segment;
2554 * proceed to the next one...
2556 if (i == ctl_sg_count - 1) {
2558 * ... unless this was the last one.
2565 if (response->ip_data_len == cs->cs_max_send_data_segment_length) {
2567 * Can't stuff more data into the current PDU;
2568 * queue it. Note that's not enough to check
2569 * for kern_data_resid == 0 instead; there
2570 * may be several Data-In PDUs for the final
2571 * call to cfiscsi_datamove(), and we want
2572 * to set the F flag only on the last of them.
2574 buffer_offset += response->ip_data_len;
2575 if (buffer_offset == io->scsiio.kern_total_len ||
2576 buffer_offset == expected_len) {
2577 buffer_offset -= response->ip_data_len;
2580 cfiscsi_pdu_queue(response);
2585 if (response != NULL) {
2586 buffer_offset += response->ip_data_len;
2587 if (buffer_offset == io->scsiio.kern_total_len ||
2588 buffer_offset == expected_len) {
2589 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2590 if (io->io_hdr.status == CTL_SUCCESS) {
2591 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2592 if (PDU_TOTAL_TRANSFER_LEN(request) <
2593 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2594 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2595 bhsdi->bhsdi_residual_count =
2596 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2597 PDU_TOTAL_TRANSFER_LEN(request));
2598 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2599 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2600 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2601 bhsdi->bhsdi_residual_count =
2602 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2603 ntohl(bhssc->bhssc_expected_data_transfer_length));
2605 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2606 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2609 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2610 cfiscsi_pdu_queue(response);
2613 io->scsiio.be_move_done(io);
2617 cfiscsi_datamove_out(union ctl_io *io)
2619 struct cfiscsi_session *cs;
2620 struct icl_pdu *request, *response;
2621 const struct iscsi_bhs_scsi_command *bhssc;
2622 struct iscsi_bhs_r2t *bhsr2t;
2623 struct cfiscsi_data_wait *cdw;
2624 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2625 uint32_t expected_len, datamove_len, r2t_off, r2t_len;
2626 uint32_t target_transfer_tag;
2629 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2630 cs = PDU_SESSION(request);
2632 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2633 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2634 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2635 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2638 * We need to record it so that we can properly report
2639 * underflow/underflow.
2641 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2644 * Complete write underflow. Not a single byte to read. Return.
2646 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2647 if (io->scsiio.kern_rel_offset >= expected_len) {
2648 io->scsiio.be_move_done(io);
2651 datamove_len = MIN(io->scsiio.kern_data_len,
2652 expected_len - io->scsiio.kern_rel_offset);
2654 target_transfer_tag =
2655 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2656 cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2657 &target_transfer_tag);
2659 CFISCSI_SESSION_WARN(cs, "failed to "
2660 "allocate memory; dropping connection");
2661 ctl_set_busy(&io->scsiio);
2662 io->scsiio.be_move_done(io);
2663 cfiscsi_session_terminate(cs);
2667 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2668 "task tag 0x%x, target transfer tag 0x%x",
2669 bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2672 cdw->cdw_ctl_io = io;
2673 cdw->cdw_target_transfer_tag = target_transfer_tag;
2674 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2675 cdw->cdw_r2t_end = datamove_len;
2676 cdw->cdw_datasn = 0;
2678 /* Set initial data pointer for the CDW respecting ext_data_filled. */
2679 if (io->scsiio.kern_sg_entries > 0) {
2680 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2682 ctl_sglist = &ctl_sg_entry;
2683 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2684 ctl_sglist->len = datamove_len;
2686 cdw->cdw_sg_index = 0;
2687 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2688 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2689 r2t_off = io->scsiio.ext_data_filled;
2690 while (r2t_off > 0) {
2691 if (r2t_off >= cdw->cdw_sg_len) {
2692 r2t_off -= cdw->cdw_sg_len;
2693 cdw->cdw_sg_index++;
2694 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2695 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2698 cdw->cdw_sg_addr += r2t_off;
2699 cdw->cdw_sg_len -= r2t_off;
2703 if (cs->cs_immediate_data &&
2704 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2705 icl_pdu_data_segment_length(request)) {
2706 done = cfiscsi_handle_data_segment(request, cdw);
2708 cfiscsi_data_wait_free(cs, cdw);
2709 io->scsiio.be_move_done(io);
2714 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2715 r2t_len = MIN(datamove_len - io->scsiio.ext_data_filled,
2716 cs->cs_max_burst_length);
2717 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2719 CFISCSI_SESSION_LOCK(cs);
2720 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2721 CFISCSI_SESSION_UNLOCK(cs);
2724 * XXX: We should limit the number of outstanding R2T PDUs
2725 * per task to MaxOutstandingR2T.
2727 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2728 if (response == NULL) {
2729 CFISCSI_SESSION_WARN(cs, "failed to "
2730 "allocate memory; dropping connection");
2731 ctl_set_busy(&io->scsiio);
2732 io->scsiio.be_move_done(io);
2733 cfiscsi_session_terminate(cs);
2736 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
2737 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2738 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2739 bhsr2t->bhsr2t_flags = 0x80;
2740 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2741 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2742 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2744 * XXX: Here we assume that cfiscsi_datamove() won't ever
2745 * be running concurrently on several CPUs for a given
2748 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2749 PDU_R2TSN(request)++;
2751 * This is the offset within the current SCSI command;
2752 * i.e. for the first call of datamove(), it will be 0,
2753 * and for subsequent ones it will be the sum of lengths
2756 * The ext_data_filled is to account for unsolicited
2757 * (immediate) data that might have already arrived.
2759 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2761 * This is the total length (sum of S/G lengths) this call
2762 * to cfiscsi_datamove() is supposed to handle, limited by
2765 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2766 cfiscsi_pdu_queue(response);
2770 cfiscsi_datamove(union ctl_io *io)
2773 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2774 cfiscsi_datamove_in(io);
2776 /* We hadn't received anything during this datamove yet. */
2777 io->scsiio.ext_data_filled = 0;
2778 cfiscsi_datamove_out(io);
2783 cfiscsi_scsi_command_done(union ctl_io *io)
2785 struct icl_pdu *request, *response;
2786 struct iscsi_bhs_scsi_command *bhssc;
2787 struct iscsi_bhs_scsi_response *bhssr;
2789 struct cfiscsi_data_wait *cdw;
2791 struct cfiscsi_session *cs;
2792 uint16_t sense_length;
2794 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2795 cs = PDU_SESSION(request);
2796 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2797 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2798 ISCSI_BHS_OPCODE_SCSI_COMMAND,
2799 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2801 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2802 // bhssc->bhssc_initiator_task_tag);
2805 CFISCSI_SESSION_LOCK(cs);
2806 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2807 KASSERT(bhssc->bhssc_initiator_task_tag !=
2808 cdw->cdw_initiator_task_tag, ("dangling cdw"));
2809 CFISCSI_SESSION_UNLOCK(cs);
2813 * Do not return status for aborted commands.
2814 * There are exceptions, but none supported by CTL yet.
2816 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2817 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2818 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2820 icl_pdu_free(request);
2824 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2825 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2826 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2827 bhssr->bhssr_flags = 0x80;
2829 * XXX: We don't deal with bidirectional under/overflows;
2830 * does anything actually support those?
2832 if (PDU_TOTAL_TRANSFER_LEN(request) <
2833 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2834 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2835 bhssr->bhssr_residual_count =
2836 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2837 PDU_TOTAL_TRANSFER_LEN(request));
2838 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2839 // ntohl(bhssr->bhssr_residual_count));
2840 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2841 ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2842 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2843 bhssr->bhssr_residual_count =
2844 htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2845 ntohl(bhssc->bhssc_expected_data_transfer_length));
2846 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2847 // ntohl(bhssr->bhssr_residual_count));
2849 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2850 bhssr->bhssr_status = io->scsiio.scsi_status;
2851 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2852 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2854 if (io->scsiio.sense_len > 0) {
2856 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2857 io->scsiio.sense_len);
2859 sense_length = htons(io->scsiio.sense_len);
2860 icl_pdu_append_data(response,
2861 &sense_length, sizeof(sense_length), M_WAITOK);
2862 icl_pdu_append_data(response,
2863 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2867 icl_pdu_free(request);
2868 cfiscsi_pdu_queue(response);
2872 cfiscsi_task_management_done(union ctl_io *io)
2874 struct icl_pdu *request, *response;
2875 struct iscsi_bhs_task_management_request *bhstmr;
2876 struct iscsi_bhs_task_management_response *bhstmr2;
2877 struct cfiscsi_data_wait *cdw, *tmpcdw;
2878 struct cfiscsi_session *cs, *tcs;
2879 struct cfiscsi_softc *softc;
2882 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2883 cs = PDU_SESSION(request);
2884 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2885 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2886 ISCSI_BHS_OPCODE_TASK_REQUEST,
2887 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2890 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2891 bhstmr->bhstmr_initiator_task_tag,
2892 bhstmr->bhstmr_referenced_task_tag);
2895 if ((bhstmr->bhstmr_function & ~0x80) ==
2896 BHSTMR_FUNCTION_ABORT_TASK) {
2898 * Make sure we no longer wait for Data-Out for this command.
2900 CFISCSI_SESSION_LOCK(cs);
2901 TAILQ_FOREACH_SAFE(cdw,
2902 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2903 if (bhstmr->bhstmr_referenced_task_tag !=
2904 cdw->cdw_initiator_task_tag)
2908 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2909 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2911 TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2913 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
2914 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 43;
2915 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2916 cfiscsi_data_wait_free(cs, cdw);
2918 CFISCSI_SESSION_UNLOCK(cs);
2920 if ((bhstmr->bhstmr_function & ~0x80) ==
2921 BHSTMR_FUNCTION_TARGET_COLD_RESET &&
2922 io->io_hdr.status == CTL_SUCCESS)
2925 response = cfiscsi_pdu_new_response(request, M_WAITOK);
2926 bhstmr2 = (struct iscsi_bhs_task_management_response *)
2928 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2929 bhstmr2->bhstmr_flags = 0x80;
2930 switch (io->taskio.task_status) {
2931 case CTL_TASK_FUNCTION_COMPLETE:
2932 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2934 case CTL_TASK_FUNCTION_SUCCEEDED:
2935 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED;
2937 case CTL_TASK_LUN_DOES_NOT_EXIST:
2938 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST;
2940 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
2942 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2945 memcpy(bhstmr2->bhstmr_additional_reponse_information,
2946 io->taskio.task_resp, sizeof(io->taskio.task_resp));
2947 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2950 icl_pdu_free(request);
2951 cfiscsi_pdu_queue(response);
2954 softc = cs->cs_target->ct_softc;
2955 mtx_lock(&softc->lock);
2956 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) {
2957 if (tcs->cs_target == cs->cs_target)
2958 cfiscsi_session_terminate(tcs);
2960 mtx_unlock(&softc->lock);
2965 cfiscsi_done(union ctl_io *io)
2967 struct icl_pdu *request;
2968 struct cfiscsi_session *cs;
2970 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2971 ("invalid CTL status %#x", io->io_hdr.status));
2973 if (io->io_hdr.io_type == CTL_IO_TASK &&
2974 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2976 * Implicit task termination has just completed; nothing to do.
2978 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2979 cs->cs_tasks_aborted = true;
2980 refcount_release(&cs->cs_outstanding_ctl_pdus);
2981 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2986 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2987 cs = PDU_SESSION(request);
2989 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2990 case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2991 cfiscsi_scsi_command_done(io);
2993 case ISCSI_BHS_OPCODE_TASK_REQUEST:
2994 cfiscsi_task_management_done(io);
2997 panic("cfiscsi_done called with wrong opcode 0x%x",
2998 request->ip_bhs->bhs_opcode);
3001 refcount_release(&cs->cs_outstanding_ctl_pdus);