]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/ctl/ctl_frontend_iscsi.c
Since contrib/libc++'s ancestry was never correct, subversion 1.8 and
[FreeBSD/FreeBSD.git] / sys / cam / ctl / ctl_frontend_iscsi.c
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 /*
33  * CTL frontend for the iSCSI protocol.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/condvar.h>
42 #include <sys/file.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/queue.h>
50 #include <sys/sbuf.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #include <sys/uio.h>
54 #include <sys/unistd.h>
55 #include <vm/uma.h>
56
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>
68
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>
73
74 #ifdef ICL_KERNEL_PROXY
75 #include <sys/socketvar.h>
76 #endif
77
78 #ifdef ICL_KERNEL_PROXY
79 FEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
80 #endif
81
82 static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
83 static uma_zone_t cfiscsi_data_wait_zone;
84
85 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
86     "CAM Target Layer iSCSI Frontend");
87 static int debug = 1;
88 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
89     &debug, 1, "Enable debug messages");
90 static int ping_timeout = 5;
91 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
92     &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
93 static int login_timeout = 60;
94 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
95     &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
96 static int maxcmdsn_delta = 256;
97 SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN,
98     &maxcmdsn_delta, 256, "Number of commands the initiator can send "
99     "without confirmation");
100
101 #define CFISCSI_DEBUG(X, ...)                                           \
102         do {                                                            \
103                 if (debug > 1) {                                        \
104                         printf("%s: " X "\n",                           \
105                             __func__, ## __VA_ARGS__);                  \
106                 }                                                       \
107         } while (0)
108
109 #define CFISCSI_WARN(X, ...)                                            \
110         do {                                                            \
111                 if (debug > 0) {                                        \
112                         printf("WARNING: %s: " X "\n",                  \
113                             __func__, ## __VA_ARGS__);                  \
114                 }                                                       \
115         } while (0)
116
117 #define CFISCSI_SESSION_DEBUG(S, X, ...)                                \
118         do {                                                            \
119                 if (debug > 1) {                                        \
120                         printf("%s: %s (%s): " X "\n",                  \
121                             __func__, S->cs_initiator_addr,             \
122                             S->cs_initiator_name, ## __VA_ARGS__);      \
123                 }                                                       \
124         } while (0)
125
126 #define CFISCSI_SESSION_WARN(S, X, ...)                                 \
127         do  {                                                           \
128                 if (debug > 0) {                                        \
129                         printf("WARNING: %s (%s): " X "\n",             \
130                             S->cs_initiator_addr,                       \
131                             S->cs_initiator_name, ## __VA_ARGS__);      \
132                 }                                                       \
133         } while (0)
134
135 #define CFISCSI_SESSION_LOCK(X)         mtx_lock(&X->cs_lock)
136 #define CFISCSI_SESSION_UNLOCK(X)       mtx_unlock(&X->cs_lock)
137 #define CFISCSI_SESSION_LOCK_ASSERT(X)  mtx_assert(&X->cs_lock, MA_OWNED)
138
139 #define CONN_SESSION(X)                 ((struct cfiscsi_session *)(X)->ic_prv0)
140 #define PDU_SESSION(X)                  CONN_SESSION((X)->ip_conn)
141 #define PDU_EXPDATASN(X)                (X)->ip_prv0
142 #define PDU_TOTAL_TRANSFER_LEN(X)       (X)->ip_prv1
143 #define PDU_R2TSN(X)                    (X)->ip_prv2
144
145 int             cfiscsi_init(void);
146 static void     cfiscsi_online(void *arg);
147 static void     cfiscsi_offline(void *arg);
148 static int      cfiscsi_info(void *arg, struct sbuf *sb);
149 static int      cfiscsi_ioctl(struct cdev *dev,
150                     u_long cmd, caddr_t addr, int flag, struct thread *td);
151 static void     cfiscsi_datamove(union ctl_io *io);
152 static void     cfiscsi_datamove_in(union ctl_io *io);
153 static void     cfiscsi_datamove_out(union ctl_io *io);
154 static void     cfiscsi_done(union ctl_io *io);
155 static bool     cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
156 static void     cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
157 static void     cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
158 static void     cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
159 static void     cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
160 static void     cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
161 static void     cfiscsi_session_terminate(struct cfiscsi_session *cs);
162 static struct cfiscsi_data_wait *cfiscsi_data_wait_new(
163                     struct cfiscsi_session *cs, union ctl_io *io,
164                     uint32_t initiator_task_tag,
165                     uint32_t *target_transfer_tagp);
166 static void     cfiscsi_data_wait_free(struct cfiscsi_session *cs,
167                     struct cfiscsi_data_wait *cdw);
168 static struct cfiscsi_target    *cfiscsi_target_find(struct cfiscsi_softc
169                     *softc, const char *name, uint16_t tag);
170 static struct cfiscsi_target    *cfiscsi_target_find_or_create(
171     struct cfiscsi_softc *softc, const char *name, const char *alias,
172     uint16_t tag);
173 static void     cfiscsi_target_release(struct cfiscsi_target *ct);
174 static void     cfiscsi_session_delete(struct cfiscsi_session *cs);
175
176 static struct cfiscsi_softc cfiscsi_softc;
177 extern struct ctl_softc *control_softc;
178
179 static struct ctl_frontend cfiscsi_frontend =
180 {
181         .name = "iscsi",
182         .init = cfiscsi_init,
183         .ioctl = cfiscsi_ioctl,
184 };
185 CTL_FRONTEND_DECLARE(ctlcfiscsi, cfiscsi_frontend);
186 MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
187
188 static struct icl_pdu *
189 cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
190 {
191
192         return (icl_pdu_new(request->ip_conn, flags));
193 }
194
195 static bool
196 cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
197 {
198         const struct iscsi_bhs_scsi_command *bhssc;
199         struct cfiscsi_session *cs;
200         uint32_t cmdsn, expstatsn;
201
202         cs = PDU_SESSION(request);
203
204         /*
205          * Every incoming PDU - not just NOP-Out - resets the ping timer.
206          * The purpose of the timeout is to reset the connection when it stalls;
207          * we don't want this to happen when NOP-In or NOP-Out ends up delayed
208          * in some queue.
209          *
210          * XXX: Locking?
211          */
212         cs->cs_timeout = 0;
213
214         /*
215          * Data-Out PDUs don't contain CmdSN.
216          */
217         if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
218             ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
219                 return (false);
220
221         /*
222          * We're only using fields common for all the request
223          * (initiator -> target) PDUs.
224          */
225         bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
226         cmdsn = ntohl(bhssc->bhssc_cmdsn);
227         expstatsn = ntohl(bhssc->bhssc_expstatsn);
228
229         CFISCSI_SESSION_LOCK(cs);
230 #if 0
231         if (expstatsn != cs->cs_statsn) {
232                 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
233                     "while current StatSN is %d", expstatsn,
234                     cs->cs_statsn);
235         }
236 #endif
237
238         if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) {
239                 /*
240                  * The target MUST silently ignore any non-immediate command
241                  * outside of this range.
242                  */
243                 if (ISCSI_SNLT(cmdsn, cs->cs_cmdsn) ||
244                     ISCSI_SNGT(cmdsn, cs->cs_cmdsn + maxcmdsn_delta)) {
245                         CFISCSI_SESSION_UNLOCK(cs);
246                         CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
247                             "while expected %u", cmdsn, cs->cs_cmdsn);
248                         return (true);
249                 }
250
251                 /*
252                  * We don't support multiple connections now, so any
253                  * discontinuity in CmdSN means lost PDUs.  Since we don't
254                  * support PDU retransmission -- terminate the connection.
255                  */
256                 if (cmdsn != cs->cs_cmdsn) {
257                         CFISCSI_SESSION_UNLOCK(cs);
258                         CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %u, "
259                             "while expected %u; dropping connection",
260                             cmdsn, cs->cs_cmdsn);
261                         cfiscsi_session_terminate(cs);
262                         return (true);
263                 }
264                 cs->cs_cmdsn++;
265         }
266
267         CFISCSI_SESSION_UNLOCK(cs);
268
269         return (false);
270 }
271
272 static void
273 cfiscsi_pdu_handle(struct icl_pdu *request)
274 {
275         struct cfiscsi_session *cs;
276         bool ignore;
277
278         cs = PDU_SESSION(request);
279
280         ignore = cfiscsi_pdu_update_cmdsn(request);
281         if (ignore) {
282                 icl_pdu_free(request);
283                 return;
284         }
285
286         /*
287          * Handle the PDU; this includes e.g. receiving the remaining
288          * part of PDU and submitting the SCSI command to CTL
289          * or queueing a reply.  The handling routine is responsible
290          * for freeing the PDU when it's no longer needed.
291          */
292         switch (request->ip_bhs->bhs_opcode &
293             ~ISCSI_BHS_OPCODE_IMMEDIATE) {
294         case ISCSI_BHS_OPCODE_NOP_OUT:
295                 cfiscsi_pdu_handle_nop_out(request);
296                 break;
297         case ISCSI_BHS_OPCODE_SCSI_COMMAND:
298                 cfiscsi_pdu_handle_scsi_command(request);
299                 break;
300         case ISCSI_BHS_OPCODE_TASK_REQUEST:
301                 cfiscsi_pdu_handle_task_request(request);
302                 break;
303         case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
304                 cfiscsi_pdu_handle_data_out(request);
305                 break;
306         case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
307                 cfiscsi_pdu_handle_logout_request(request);
308                 break;
309         default:
310                 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
311                     "opcode 0x%x; dropping connection",
312                     request->ip_bhs->bhs_opcode);
313                 icl_pdu_free(request);
314                 cfiscsi_session_terminate(cs);
315         }
316
317 }
318
319 static void
320 cfiscsi_receive_callback(struct icl_pdu *request)
321 {
322         struct cfiscsi_session *cs;
323
324         cs = PDU_SESSION(request);
325
326 #ifdef ICL_KERNEL_PROXY
327         if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
328                 if (cs->cs_login_pdu == NULL)
329                         cs->cs_login_pdu = request;
330                 else
331                         icl_pdu_free(request);
332                 cv_signal(&cs->cs_login_cv);
333                 return;
334         }
335 #endif
336
337         cfiscsi_pdu_handle(request);
338 }
339
340 static void
341 cfiscsi_error_callback(struct icl_conn *ic)
342 {
343         struct cfiscsi_session *cs;
344
345         cs = CONN_SESSION(ic);
346
347         CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
348         cfiscsi_session_terminate(cs);
349 }
350
351 static int
352 cfiscsi_pdu_prepare(struct icl_pdu *response)
353 {
354         struct cfiscsi_session *cs;
355         struct iscsi_bhs_scsi_response *bhssr;
356         bool advance_statsn = true;
357
358         cs = PDU_SESSION(response);
359
360         CFISCSI_SESSION_LOCK_ASSERT(cs);
361
362         /*
363          * We're only using fields common for all the response
364          * (target -> initiator) PDUs.
365          */
366         bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
367
368         /*
369          * 10.8.3: "The StatSN for this connection is not advanced
370          * after this PDU is sent."
371          */
372         if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
373                 advance_statsn = false;
374
375         /*
376          * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
377          * StatSN for the connection is not advanced after this PDU is sent."
378          */
379         if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN && 
380             bhssr->bhssr_initiator_task_tag == 0xffffffff)
381                 advance_statsn = false;
382
383         /*
384          * See the comment below - StatSN is not meaningful and must
385          * not be advanced.
386          */
387         if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN &&
388             (bhssr->bhssr_flags & BHSDI_FLAGS_S) == 0)
389                 advance_statsn = false;
390
391         /*
392          * 10.7.3: "The fields StatSN, Status, and Residual Count
393          * only have meaningful content if the S bit is set to 1."
394          */
395         if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN ||
396             (bhssr->bhssr_flags & BHSDI_FLAGS_S))
397                 bhssr->bhssr_statsn = htonl(cs->cs_statsn);
398         bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
399         bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
400
401         if (advance_statsn)
402                 cs->cs_statsn++;
403
404         return (0);
405 }
406
407 static void
408 cfiscsi_pdu_queue(struct icl_pdu *response)
409 {
410         struct cfiscsi_session *cs;
411
412         cs = PDU_SESSION(response);
413
414         CFISCSI_SESSION_LOCK(cs);
415         cfiscsi_pdu_prepare(response);
416         icl_pdu_queue(response);
417         CFISCSI_SESSION_UNLOCK(cs);
418 }
419
420 static uint32_t
421 cfiscsi_decode_lun(uint64_t encoded)
422 {
423         uint8_t lun[8];
424         uint32_t result;
425
426         /*
427          * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
428          * but is in fact an evil, multidimensional structure defined
429          * in SCSI Architecture Model 5 (SAM-5), section 4.6.
430          */
431         memcpy(lun, &encoded, sizeof(lun));
432         switch (lun[0] & 0xC0) {
433         case 0x00:
434                 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
435                     lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
436                         CFISCSI_WARN("malformed LUN "
437                             "(peripheral device addressing method): 0x%jx",
438                             (uintmax_t)encoded);
439                         result = 0xffffffff;
440                         break;
441                 }
442                 result = lun[1];
443                 break;
444         case 0x40:
445                 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
446                     lun[6] != 0 || lun[7] != 0) {
447                         CFISCSI_WARN("malformed LUN "
448                             "(flat address space addressing method): 0x%jx",
449                             (uintmax_t)encoded);
450                         result = 0xffffffff;
451                         break;
452                 }
453                 result = ((lun[0] & 0x3f) << 8) + lun[1];
454                 break;
455         case 0xC0:
456                 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
457                     lun[6] != 0 || lun[7] != 0) {
458                         CFISCSI_WARN("malformed LUN (extended flat "
459                             "address space addressing method): 0x%jx",
460                             (uintmax_t)encoded);
461                         result = 0xffffffff;
462                         break;
463                 }
464                 result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
465         default:
466                 CFISCSI_WARN("unsupported LUN format 0x%jx",
467                     (uintmax_t)encoded);
468                 result = 0xffffffff;
469                 break;
470         }
471
472         return (result);
473 }
474
475 static void
476 cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
477 {
478         struct cfiscsi_session *cs;
479         struct iscsi_bhs_nop_out *bhsno;
480         struct iscsi_bhs_nop_in *bhsni;
481         struct icl_pdu *response;
482         void *data = NULL;
483         size_t datasize;
484         int error;
485
486         cs = PDU_SESSION(request);
487         bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
488
489         if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
490                 /*
491                  * Nothing to do, iscsi_pdu_update_statsn() already
492                  * zeroed the timeout.
493                  */
494                 icl_pdu_free(request);
495                 return;
496         }
497
498         datasize = icl_pdu_data_segment_length(request);
499         if (datasize > 0) {
500                 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
501                 if (data == NULL) {
502                         CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
503                             "dropping connection");
504                         icl_pdu_free(request);
505                         cfiscsi_session_terminate(cs);
506                         return;
507                 }
508                 icl_pdu_get_data(request, 0, data, datasize);
509         }
510
511         response = cfiscsi_pdu_new_response(request, M_NOWAIT);
512         if (response == NULL) {
513                 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
514                     "droppping connection");
515                 free(data, M_CFISCSI);
516                 icl_pdu_free(request);
517                 cfiscsi_session_terminate(cs);
518                 return;
519         }
520         bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
521         bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
522         bhsni->bhsni_flags = 0x80;
523         bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
524         bhsni->bhsni_target_transfer_tag = 0xffffffff;
525         if (datasize > 0) {
526                 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
527                 if (error != 0) {
528                         CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
529                             "dropping connection");
530                         free(data, M_CFISCSI);
531                         icl_pdu_free(request);
532                         icl_pdu_free(response);
533                         cfiscsi_session_terminate(cs);
534                         return;
535                 }
536                 free(data, M_CFISCSI);
537         }
538
539         icl_pdu_free(request);
540         cfiscsi_pdu_queue(response);
541 }
542
543 static void
544 cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
545 {
546         struct iscsi_bhs_scsi_command *bhssc;
547         struct cfiscsi_session *cs;
548         union ctl_io *io;
549         int error;
550
551         cs = PDU_SESSION(request);
552         bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
553         //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
554         //    bhssc->bhssc_initiator_task_tag);
555
556         if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
557                 CFISCSI_SESSION_WARN(cs, "unsolicited data with "
558                     "ImmediateData=No; dropping connection");
559                 icl_pdu_free(request);
560                 cfiscsi_session_terminate(cs);
561                 return;
562         }
563         io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
564         ctl_zero_io(io);
565         io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
566         io->io_hdr.io_type = CTL_IO_SCSI;
567         io->io_hdr.nexus.initid = cs->cs_ctl_initid;
568         io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
569         io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
570         io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
571         switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
572         case BHSSC_FLAGS_ATTR_UNTAGGED:
573                 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
574                 break;
575         case BHSSC_FLAGS_ATTR_SIMPLE:
576                 io->scsiio.tag_type = CTL_TAG_SIMPLE;
577                 break;
578         case BHSSC_FLAGS_ATTR_ORDERED:
579                 io->scsiio.tag_type = CTL_TAG_ORDERED;
580                 break;
581         case BHSSC_FLAGS_ATTR_HOQ:
582                 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
583                 break;
584         case BHSSC_FLAGS_ATTR_ACA:
585                 io->scsiio.tag_type = CTL_TAG_ACA;
586                 break;
587         default:
588                 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
589                 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
590                     bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
591                 break;
592         }
593         io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
594         memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
595         refcount_acquire(&cs->cs_outstanding_ctl_pdus);
596         error = ctl_queue(io);
597         if (error != CTL_RETVAL_COMPLETE) {
598                 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
599                     "dropping connection", error);
600                 ctl_free_io(io);
601                 refcount_release(&cs->cs_outstanding_ctl_pdus);
602                 icl_pdu_free(request);
603                 cfiscsi_session_terminate(cs);
604         }
605 }
606
607 static void
608 cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
609 {
610         struct iscsi_bhs_task_management_request *bhstmr;
611         struct iscsi_bhs_task_management_response *bhstmr2;
612         struct icl_pdu *response;
613         struct cfiscsi_session *cs;
614         union ctl_io *io;
615         int error;
616
617         cs = PDU_SESSION(request);
618         bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
619         io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
620         ctl_zero_io(io);
621         io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
622         io->io_hdr.io_type = CTL_IO_TASK;
623         io->io_hdr.nexus.initid = cs->cs_ctl_initid;
624         io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
625         io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
626         io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
627
628         switch (bhstmr->bhstmr_function & ~0x80) {
629         case BHSTMR_FUNCTION_ABORT_TASK:
630 #if 0
631                 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
632 #endif
633                 io->taskio.task_action = CTL_TASK_ABORT_TASK;
634                 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
635                 break;
636         case BHSTMR_FUNCTION_ABORT_TASK_SET:
637 #if 0
638                 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET");
639 #endif
640                 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
641                 break;
642         case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
643 #if 0
644                 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
645 #endif
646                 io->taskio.task_action = CTL_TASK_LUN_RESET;
647                 break;
648         case BHSTMR_FUNCTION_TARGET_WARM_RESET:
649 #if 0
650                 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
651 #endif
652                 io->taskio.task_action = CTL_TASK_TARGET_RESET;
653                 break;
654         default:
655                 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
656                     bhstmr->bhstmr_function & ~0x80);
657                 ctl_free_io(io);
658
659                 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
660                 if (response == NULL) {
661                         CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
662                             "dropping connection");
663                         icl_pdu_free(request);
664                         cfiscsi_session_terminate(cs);
665                         return;
666                 }
667                 bhstmr2 = (struct iscsi_bhs_task_management_response *)
668                     response->ip_bhs;
669                 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
670                 bhstmr2->bhstmr_flags = 0x80;
671                 bhstmr2->bhstmr_response =
672                     BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
673                 bhstmr2->bhstmr_initiator_task_tag =
674                     bhstmr->bhstmr_initiator_task_tag;
675                 icl_pdu_free(request);
676                 cfiscsi_pdu_queue(response);
677                 return;
678         }
679
680         refcount_acquire(&cs->cs_outstanding_ctl_pdus);
681         error = ctl_queue(io);
682         if (error != CTL_RETVAL_COMPLETE) {
683                 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
684                     "dropping connection", error);
685                 ctl_free_io(io);
686                 refcount_release(&cs->cs_outstanding_ctl_pdus);
687                 icl_pdu_free(request);
688                 cfiscsi_session_terminate(cs);
689         }
690 }
691
692 static bool
693 cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
694 {
695         struct iscsi_bhs_data_out *bhsdo;
696         struct cfiscsi_session *cs;
697         struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
698         size_t copy_len, len, off, buffer_offset;
699         int ctl_sg_count;
700         union ctl_io *io;
701
702         cs = PDU_SESSION(request);
703
704         KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
705             ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
706             (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
707             ISCSI_BHS_OPCODE_SCSI_COMMAND,
708             ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
709
710         /*
711          * We're only using fields common for Data-Out and SCSI Command PDUs.
712          */
713         bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
714
715         io = cdw->cdw_ctl_io;
716         KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
717             ("CTL_FLAG_DATA_IN"));
718
719 #if 0
720         CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
721             request->ip_data_len, io->scsiio.kern_total_len);
722 #endif
723
724         if (io->scsiio.kern_sg_entries > 0) {
725                 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
726                 ctl_sg_count = io->scsiio.kern_sg_entries;
727         } else {
728                 ctl_sglist = &ctl_sg_entry;
729                 ctl_sglist->addr = io->scsiio.kern_data_ptr;
730                 ctl_sglist->len = io->scsiio.kern_data_len;
731                 ctl_sg_count = 1;
732         }
733
734         if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
735             ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
736                 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
737         else
738                 buffer_offset = 0;
739         len = icl_pdu_data_segment_length(request);
740
741         /*
742          * Make sure the offset, as sent by the initiator, matches the offset
743          * we're supposed to be at in the scatter-gather list.
744          */
745         if (buffer_offset >
746             io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
747             buffer_offset + len <=
748             io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
749                 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
750                     "expected %zd; dropping connection", buffer_offset,
751                     (size_t)io->scsiio.kern_rel_offset +
752                     (size_t)io->scsiio.ext_data_filled);
753                 ctl_set_data_phase_error(&io->scsiio);
754                 cfiscsi_session_terminate(cs);
755                 return (true);
756         }
757
758         /*
759          * This is the offset within the PDU data segment, as opposed
760          * to buffer_offset, which is the offset within the task (SCSI
761          * command).
762          */
763         off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
764             buffer_offset;
765
766         /*
767          * Iterate over the scatter/gather segments, filling them with data
768          * from the PDU data segment.  Note that this can get called multiple
769          * times for one SCSI command; the cdw structure holds state for the
770          * scatter/gather list.
771          */
772         for (;;) {
773                 KASSERT(cdw->cdw_sg_index < ctl_sg_count,
774                     ("cdw->cdw_sg_index >= ctl_sg_count"));
775                 if (cdw->cdw_sg_len == 0) {
776                         cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
777                         cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
778                 }
779                 KASSERT(off <= len, ("len > off"));
780                 copy_len = len - off;
781                 if (copy_len > cdw->cdw_sg_len)
782                         copy_len = cdw->cdw_sg_len;
783
784                 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
785                 cdw->cdw_sg_addr += copy_len;
786                 cdw->cdw_sg_len -= copy_len;
787                 off += copy_len;
788                 io->scsiio.ext_data_filled += copy_len;
789
790                 if (cdw->cdw_sg_len == 0) {
791                         /*
792                          * End of current segment.
793                          */
794                         if (cdw->cdw_sg_index == ctl_sg_count - 1) {
795                                 /*
796                                  * Last segment in scatter/gather list.
797                                  */
798                                 break;
799                         }
800                         cdw->cdw_sg_index++;
801                 }
802
803                 if (off == len) {
804                         /*
805                          * End of PDU payload.
806                          */
807                         break;
808                 }
809         }
810
811         if (len > off) {
812                 /*
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().
817                  *
818                  * This obviously can only happen with SCSI Command PDU. 
819                  */
820                 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
821                     ISCSI_BHS_OPCODE_SCSI_COMMAND)
822                         return (true);
823
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);
829                 return (true);
830         }
831
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",
836                     bhsdo->bhsdo_flags);
837                 ctl_set_data_phase_error(&io->scsiio);
838                 cfiscsi_session_terminate(cs);
839                 return (true);
840         }
841
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,
850                             cdw->cdw_r2t_end);
851                         ctl_set_data_phase_error(&io->scsiio);
852                         cfiscsi_session_terminate(cs);
853                         return (true);
854                 } else {
855                         /*
856                          * For SCSI Command PDU, this just means we need to
857                          * solicit more data by sending R2T.
858                          */
859                         return (false);
860                 }
861         }
862
863         if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) {
864 #if 0
865                 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
866                     "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
867 #endif
868
869                 return (true);
870         }
871
872         return (false);
873 }
874
875 static void
876 cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
877 {
878         struct iscsi_bhs_data_out *bhsdo;
879         struct cfiscsi_session *cs;
880         struct cfiscsi_data_wait *cdw = NULL;
881         union ctl_io *io;
882         bool done;
883
884         cs = PDU_SESSION(request);
885         bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
886
887         CFISCSI_SESSION_LOCK(cs);
888         TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
889 #if 0
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));
895 #endif
896                 if (bhsdo->bhsdo_target_transfer_tag ==
897                     cdw->cdw_target_transfer_tag)
898                         break;
899         }
900         CFISCSI_SESSION_UNLOCK(cs);
901         if (cdw == NULL) {
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);
907                 return;
908         }
909
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);
916                 return;
917         }
918         cdw->cdw_datasn++;
919
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"));
923
924         done = cfiscsi_handle_data_segment(request, cdw);
925         if (done) {
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                 if (done)
933                         io->scsiio.be_move_done(io);
934                 else
935                         cfiscsi_datamove_out(io);
936         }
937
938         icl_pdu_free(request);
939 }
940
941 static void
942 cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
943 {
944         struct iscsi_bhs_logout_request *bhslr;
945         struct iscsi_bhs_logout_response *bhslr2;
946         struct icl_pdu *response;
947         struct cfiscsi_session *cs;
948
949         cs = PDU_SESSION(request);
950         bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
951         switch (bhslr->bhslr_reason & 0x7f) {
952         case BHSLR_REASON_CLOSE_SESSION:
953         case BHSLR_REASON_CLOSE_CONNECTION:
954                 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
955                 if (response == NULL) {
956                         CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
957                         icl_pdu_free(request);
958                         cfiscsi_session_terminate(cs);
959                         return;
960                 }
961                 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
962                 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
963                 bhslr2->bhslr_flags = 0x80;
964                 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
965                 bhslr2->bhslr_initiator_task_tag =
966                     bhslr->bhslr_initiator_task_tag;
967                 icl_pdu_free(request);
968                 cfiscsi_pdu_queue(response);
969                 cfiscsi_session_terminate(cs);
970                 break;
971         case BHSLR_REASON_REMOVE_FOR_RECOVERY:
972                 response = cfiscsi_pdu_new_response(request, M_NOWAIT);
973                 if (response == NULL) {
974                         CFISCSI_SESSION_WARN(cs,
975                             "failed to allocate memory; dropping connection");
976                         icl_pdu_free(request);
977                         cfiscsi_session_terminate(cs);
978                         return;
979                 }
980                 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
981                 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
982                 bhslr2->bhslr_flags = 0x80;
983                 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
984                 bhslr2->bhslr_initiator_task_tag =
985                     bhslr->bhslr_initiator_task_tag;
986                 icl_pdu_free(request);
987                 cfiscsi_pdu_queue(response);
988                 break;
989         default:
990                 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
991                     bhslr->bhslr_reason);
992                 icl_pdu_free(request);
993                 cfiscsi_session_terminate(cs);
994                 break;
995         }
996 }
997
998 static void
999 cfiscsi_callout(void *context)
1000 {
1001         struct icl_pdu *cp;
1002         struct iscsi_bhs_nop_in *bhsni;
1003         struct cfiscsi_session *cs;
1004
1005         cs = context;
1006
1007         if (cs->cs_terminating) 
1008                 return;
1009
1010         callout_schedule(&cs->cs_callout, 1 * hz);
1011
1012         atomic_add_int(&cs->cs_timeout, 1);
1013
1014 #ifdef ICL_KERNEL_PROXY
1015         if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1016                 if (login_timeout > 0 && cs->cs_timeout > login_timeout) {
1017                         CFISCSI_SESSION_WARN(cs, "login timed out after "
1018                             "%d seconds; dropping connection", cs->cs_timeout);
1019                         cfiscsi_session_terminate(cs);
1020                 }
1021                 return;
1022         }
1023 #endif
1024
1025         if (ping_timeout <= 0) {
1026                 /*
1027                  * Pings are disabled.  Don't send NOP-In in this case;
1028                  * user might have disabled pings to work around problems
1029                  * with certain initiators that can't properly handle
1030                  * NOP-In, such as iPXE.  Reset the timeout, to avoid
1031                  * triggering reconnection, should the user decide to
1032                  * reenable them.
1033                  */
1034                 cs->cs_timeout = 0;
1035                 return;
1036         }
1037
1038         if (cs->cs_timeout >= ping_timeout) {
1039                 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1040                     "dropping connection",  ping_timeout);
1041                 cfiscsi_session_terminate(cs);
1042                 return;
1043         }
1044
1045         /*
1046          * If the ping was reset less than one second ago - which means
1047          * that we've received some PDU during the last second - assume
1048          * the traffic flows correctly and don't bother sending a NOP-Out.
1049          *
1050          * (It's 2 - one for one second, and one for incrementing is_timeout
1051          * earlier in this routine.)
1052          */
1053         if (cs->cs_timeout < 2)
1054                 return;
1055
1056         cp = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1057         if (cp == NULL) {
1058                 CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1059                 return;
1060         }
1061         bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1062         bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1063         bhsni->bhsni_flags = 0x80;
1064         bhsni->bhsni_initiator_task_tag = 0xffffffff;
1065
1066         cfiscsi_pdu_queue(cp);
1067 }
1068
1069 static struct cfiscsi_data_wait *
1070 cfiscsi_data_wait_new(struct cfiscsi_session *cs, union ctl_io *io,
1071     uint32_t initiator_task_tag, uint32_t *target_transfer_tagp)
1072 {
1073         struct cfiscsi_data_wait *cdw;
1074         int error;
1075
1076         cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
1077         if (cdw == NULL) {
1078                 CFISCSI_SESSION_WARN(cs,
1079                     "failed to allocate %zd bytes", sizeof(*cdw));
1080                 return (NULL);
1081         }
1082
1083         error = icl_conn_transfer_setup(cs->cs_conn, io, target_transfer_tagp,
1084             &cdw->cdw_icl_prv);
1085         if (error != 0) {
1086                 CFISCSI_SESSION_WARN(cs,
1087                     "icl_conn_transfer_setup() failed with error %d", error);
1088                 uma_zfree(cfiscsi_data_wait_zone, cdw);
1089                 return (NULL);
1090         }
1091
1092         cdw->cdw_ctl_io = io;
1093         cdw->cdw_target_transfer_tag = *target_transfer_tagp;
1094         cdw->cdw_initiator_task_tag = initiator_task_tag;
1095
1096         return (cdw);
1097 }
1098
1099 static void
1100 cfiscsi_data_wait_free(struct cfiscsi_session *cs,
1101     struct cfiscsi_data_wait *cdw)
1102 {
1103
1104         icl_conn_transfer_done(cs->cs_conn, cdw->cdw_icl_prv);
1105         uma_zfree(cfiscsi_data_wait_zone, cdw);
1106 }
1107
1108 static void
1109 cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1110 {
1111         struct cfiscsi_data_wait *cdw;
1112         union ctl_io *io;
1113         int error, last, wait;
1114
1115         if (cs->cs_target == NULL)
1116                 return;         /* No target yet, so nothing to do. */
1117         io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref);
1118         ctl_zero_io(io);
1119         io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs;
1120         io->io_hdr.io_type = CTL_IO_TASK;
1121         io->io_hdr.nexus.initid = cs->cs_ctl_initid;
1122         io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port;
1123         io->io_hdr.nexus.targ_lun = 0;
1124         io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1125         io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET;
1126         wait = cs->cs_outstanding_ctl_pdus;
1127         refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1128         error = ctl_queue(io);
1129         if (error != CTL_RETVAL_COMPLETE) {
1130                 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1131                 refcount_release(&cs->cs_outstanding_ctl_pdus);
1132                 ctl_free_io(io);
1133         }
1134
1135         CFISCSI_SESSION_LOCK(cs);
1136         while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) {
1137                 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1138                 CFISCSI_SESSION_UNLOCK(cs);
1139                 /*
1140                  * Set nonzero port status; this prevents backends from
1141                  * assuming that the data transfer actually succeeded
1142                  * and writing uninitialized data to disk.
1143                  */
1144                 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1145                 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1146                 cfiscsi_data_wait_free(cs, cdw);
1147                 CFISCSI_SESSION_LOCK(cs);
1148         }
1149         CFISCSI_SESSION_UNLOCK(cs);
1150
1151         /*
1152          * Wait for CTL to terminate all the tasks.
1153          */
1154         if (wait > 0)
1155                 CFISCSI_SESSION_WARN(cs,
1156                     "waiting for CTL to terminate %d tasks", wait);
1157         for (;;) {
1158                 refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1159                 last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1160                 if (last != 0)
1161                         break;
1162                 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus),
1163                     0, "cfiscsi_terminate", hz / 100);
1164         }
1165         if (wait > 0)
1166                 CFISCSI_SESSION_WARN(cs, "tasks terminated");
1167 }
1168
1169 static void
1170 cfiscsi_maintenance_thread(void *arg)
1171 {
1172         struct cfiscsi_session *cs;
1173
1174         cs = arg;
1175
1176         for (;;) {
1177                 CFISCSI_SESSION_LOCK(cs);
1178                 if (cs->cs_terminating == false)
1179                         cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1180                 CFISCSI_SESSION_UNLOCK(cs);
1181
1182                 if (cs->cs_terminating) {
1183
1184                         /*
1185                          * We used to wait up to 30 seconds to deliver queued
1186                          * PDUs to the initiator.  We also tried hard to deliver
1187                          * SCSI Responses for the aborted PDUs.  We don't do
1188                          * that anymore.  We might need to revisit that.
1189                          */
1190                         callout_drain(&cs->cs_callout);
1191                         icl_conn_close(cs->cs_conn);
1192
1193                         /*
1194                          * At this point ICL receive thread is no longer
1195                          * running; no new tasks can be queued.
1196                          */
1197                         cfiscsi_session_terminate_tasks(cs);
1198                         cfiscsi_session_delete(cs);
1199                         kthread_exit();
1200                         return;
1201                 }
1202                 CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1203         }
1204 }
1205
1206 static void
1207 cfiscsi_session_terminate(struct cfiscsi_session *cs)
1208 {
1209
1210         if (cs->cs_terminating)
1211                 return;
1212         cs->cs_terminating = true;
1213         cv_signal(&cs->cs_maintenance_cv);
1214 #ifdef ICL_KERNEL_PROXY
1215         cv_signal(&cs->cs_login_cv);
1216 #endif
1217 }
1218
1219 static int
1220 cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1221 {
1222         struct cfiscsi_target *ct;
1223         char *name;
1224         int i;
1225
1226         KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1227
1228         ct = cs->cs_target;
1229         name = strdup(cs->cs_initiator_id, M_CTL);
1230         i = ctl_add_initiator(&ct->ct_port, -1, 0, name);
1231         if (i < 0) {
1232                 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d",
1233                     i);
1234                 cs->cs_ctl_initid = -1;
1235                 return (1);
1236         }
1237         cs->cs_ctl_initid = i;
1238 #if 0
1239         CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i);
1240 #endif
1241
1242         return (0);
1243 }
1244
1245 static void
1246 cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1247 {
1248         int error;
1249
1250         if (cs->cs_ctl_initid == -1)
1251                 return;
1252
1253         error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid);
1254         if (error != 0) {
1255                 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1256                     error);
1257         }
1258         cs->cs_ctl_initid = -1;
1259 }
1260
1261 static struct cfiscsi_session *
1262 cfiscsi_session_new(struct cfiscsi_softc *softc, const char *offload)
1263 {
1264         struct cfiscsi_session *cs;
1265         int error;
1266
1267         cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1268         if (cs == NULL) {
1269                 CFISCSI_WARN("malloc failed");
1270                 return (NULL);
1271         }
1272         cs->cs_ctl_initid = -1;
1273
1274         refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1275         TAILQ_INIT(&cs->cs_waiting_for_data_out);
1276         mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1277         cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1278 #ifdef ICL_KERNEL_PROXY
1279         cv_init(&cs->cs_login_cv, "cfiscsi_login");
1280 #endif
1281
1282         cs->cs_conn = icl_new_conn(offload, "cfiscsi", &cs->cs_lock);
1283         if (cs->cs_conn == NULL) {
1284                 free(cs, M_CFISCSI);
1285                 return (NULL);
1286         }
1287         cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1288         cs->cs_conn->ic_error = cfiscsi_error_callback;
1289         cs->cs_conn->ic_prv0 = cs;
1290
1291         error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1292         if (error != 0) {
1293                 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1294                 free(cs, M_CFISCSI);
1295                 return (NULL);
1296         }
1297
1298         mtx_lock(&softc->lock);
1299         cs->cs_id = ++softc->last_session_id;
1300         TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1301         mtx_unlock(&softc->lock);
1302
1303         /*
1304          * Start pinging the initiator.
1305          */
1306         callout_init(&cs->cs_callout, 1);
1307         callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1308
1309         return (cs);
1310 }
1311
1312 static void
1313 cfiscsi_session_delete(struct cfiscsi_session *cs)
1314 {
1315         struct cfiscsi_softc *softc;
1316
1317         softc = &cfiscsi_softc;
1318
1319         KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1320             ("destroying session with outstanding CTL pdus"));
1321         KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1322             ("destroying session with non-empty queue"));
1323
1324         cfiscsi_session_unregister_initiator(cs);
1325         if (cs->cs_target != NULL)
1326                 cfiscsi_target_release(cs->cs_target);
1327         icl_conn_close(cs->cs_conn);
1328         icl_conn_free(cs->cs_conn);
1329
1330         mtx_lock(&softc->lock);
1331         TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1332         cv_signal(&softc->sessions_cv);
1333         mtx_unlock(&softc->lock);
1334
1335         free(cs, M_CFISCSI);
1336 }
1337
1338 int
1339 cfiscsi_init(void)
1340 {
1341         struct cfiscsi_softc *softc;
1342         int retval;
1343
1344         softc = &cfiscsi_softc;
1345         retval = 0;
1346         bzero(softc, sizeof(*softc));
1347         mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1348
1349         cv_init(&softc->sessions_cv, "cfiscsi_sessions");
1350 #ifdef ICL_KERNEL_PROXY
1351         cv_init(&softc->accept_cv, "cfiscsi_accept");
1352 #endif
1353         TAILQ_INIT(&softc->sessions);
1354         TAILQ_INIT(&softc->targets);
1355
1356         cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1357             sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1358             UMA_ALIGN_PTR, 0);
1359
1360         return (0);
1361 }
1362
1363 #ifdef ICL_KERNEL_PROXY
1364 static void
1365 cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1366 {
1367         struct cfiscsi_session *cs;
1368
1369         cs = cfiscsi_session_new(&cfiscsi_softc, NULL);
1370         if (cs == NULL) {
1371                 CFISCSI_WARN("failed to create session");
1372                 return;
1373         }
1374
1375         icl_conn_handoff_sock(cs->cs_conn, so);
1376         cs->cs_initiator_sa = sa;
1377         cs->cs_portal_id = portal_id;
1378         cs->cs_waiting_for_ctld = true;
1379         cv_signal(&cfiscsi_softc.accept_cv);
1380 }
1381 #endif
1382
1383 static void
1384 cfiscsi_online(void *arg)
1385 {
1386         struct cfiscsi_softc *softc;
1387         struct cfiscsi_target *ct;
1388         int online;
1389
1390         ct = (struct cfiscsi_target *)arg;
1391         softc = ct->ct_softc;
1392
1393         mtx_lock(&softc->lock);
1394         if (ct->ct_online) {
1395                 mtx_unlock(&softc->lock);
1396                 return;
1397         }
1398         ct->ct_online = 1;
1399         online = softc->online++;
1400         mtx_unlock(&softc->lock);
1401         if (online > 0)
1402                 return;
1403
1404 #ifdef ICL_KERNEL_PROXY
1405         if (softc->listener != NULL)
1406                 icl_listen_free(softc->listener);
1407         softc->listener = icl_listen_new(cfiscsi_accept);
1408 #endif
1409 }
1410
1411 static void
1412 cfiscsi_offline(void *arg)
1413 {
1414         struct cfiscsi_softc *softc;
1415         struct cfiscsi_target *ct;
1416         struct cfiscsi_session *cs;
1417         int online;
1418
1419         ct = (struct cfiscsi_target *)arg;
1420         softc = ct->ct_softc;
1421
1422         mtx_lock(&softc->lock);
1423         if (!ct->ct_online) {
1424                 mtx_unlock(&softc->lock);
1425                 return;
1426         }
1427         ct->ct_online = 0;
1428         online = --softc->online;
1429
1430         TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1431                 if (cs->cs_target == ct)
1432                         cfiscsi_session_terminate(cs);
1433         }
1434         do {
1435                 TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1436                         if (cs->cs_target == ct)
1437                                 break;
1438                 }
1439                 if (cs != NULL)
1440                         cv_wait(&softc->sessions_cv, &softc->lock);
1441         } while (cs != NULL && ct->ct_online == 0);
1442         mtx_unlock(&softc->lock);
1443         if (online > 0)
1444                 return;
1445
1446 #ifdef ICL_KERNEL_PROXY
1447         icl_listen_free(softc->listener);
1448         softc->listener = NULL;
1449 #endif
1450 }
1451
1452 static int
1453 cfiscsi_info(void *arg, struct sbuf *sb)
1454 {
1455         struct cfiscsi_target *ct = (struct cfiscsi_target *)arg;
1456         int retval;
1457
1458         retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n",
1459             ct->ct_state);
1460         return (retval);
1461 }
1462
1463 static void
1464 cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1465 {
1466         struct cfiscsi_softc *softc;
1467         struct cfiscsi_session *cs, *cs2;
1468         struct cfiscsi_target *ct;
1469         struct ctl_iscsi_handoff_params *cihp;
1470         int error;
1471
1472         cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1473         softc = &cfiscsi_softc;
1474
1475         CFISCSI_DEBUG("new connection from %s (%s) to %s",
1476             cihp->initiator_name, cihp->initiator_addr,
1477             cihp->target_name);
1478
1479         ct = cfiscsi_target_find(softc, cihp->target_name,
1480             cihp->portal_group_tag);
1481         if (ct == NULL) {
1482                 ci->status = CTL_ISCSI_ERROR;
1483                 snprintf(ci->error_str, sizeof(ci->error_str),
1484                     "%s: target not found", __func__);
1485                 return;
1486         }
1487
1488 #ifdef ICL_KERNEL_PROXY
1489         if (cihp->socket > 0 && cihp->connection_id > 0) {
1490                 snprintf(ci->error_str, sizeof(ci->error_str),
1491                     "both socket and connection_id set");
1492                 ci->status = CTL_ISCSI_ERROR;
1493                 cfiscsi_target_release(ct);
1494                 return;
1495         }
1496         if (cihp->socket == 0) {
1497                 mtx_lock(&cfiscsi_softc.lock);
1498                 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1499                         if (cs->cs_id == cihp->connection_id)
1500                                 break;
1501                 }
1502                 if (cs == NULL) {
1503                         mtx_unlock(&cfiscsi_softc.lock);
1504                         snprintf(ci->error_str, sizeof(ci->error_str),
1505                             "connection not found");
1506                         ci->status = CTL_ISCSI_ERROR;
1507                         cfiscsi_target_release(ct);
1508                         return;
1509                 }
1510                 mtx_unlock(&cfiscsi_softc.lock);
1511         } else {
1512 #endif
1513                 cs = cfiscsi_session_new(softc, cihp->offload);
1514                 if (cs == NULL) {
1515                         ci->status = CTL_ISCSI_ERROR;
1516                         snprintf(ci->error_str, sizeof(ci->error_str),
1517                             "%s: cfiscsi_session_new failed", __func__);
1518                         cfiscsi_target_release(ct);
1519                         return;
1520                 }
1521 #ifdef ICL_KERNEL_PROXY
1522         }
1523 #endif
1524
1525         /*
1526          * First PDU of Full Feature phase has the same CmdSN as the last
1527          * PDU from the Login Phase received from the initiator.  Thus,
1528          * the -1 below.
1529          */
1530         cs->cs_cmdsn = cihp->cmdsn;
1531         cs->cs_statsn = cihp->statsn;
1532         cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1533         cs->cs_max_burst_length = cihp->max_burst_length;
1534         cs->cs_immediate_data = !!cihp->immediate_data;
1535         if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1536                 cs->cs_conn->ic_header_crc32c = true;
1537         if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1538                 cs->cs_conn->ic_data_crc32c = true;
1539
1540         strlcpy(cs->cs_initiator_name,
1541             cihp->initiator_name, sizeof(cs->cs_initiator_name));
1542         strlcpy(cs->cs_initiator_addr,
1543             cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1544         strlcpy(cs->cs_initiator_alias,
1545             cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1546         memcpy(cs->cs_initiator_isid,
1547             cihp->initiator_isid, sizeof(cs->cs_initiator_isid));
1548         snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id),
1549             "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name,
1550             cihp->initiator_isid[0], cihp->initiator_isid[1],
1551             cihp->initiator_isid[2], cihp->initiator_isid[3],
1552             cihp->initiator_isid[4], cihp->initiator_isid[5]);
1553
1554         mtx_lock(&softc->lock);
1555         if (ct->ct_online == 0) {
1556                 mtx_unlock(&softc->lock);
1557                 cfiscsi_session_terminate(cs);
1558                 cfiscsi_target_release(ct);
1559                 ci->status = CTL_ISCSI_ERROR;
1560                 snprintf(ci->error_str, sizeof(ci->error_str),
1561                     "%s: port offline", __func__);
1562                 return;
1563         }
1564         cs->cs_target = ct;
1565         mtx_unlock(&softc->lock);
1566
1567         refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1568 restart:
1569         if (!cs->cs_terminating) {
1570                 mtx_lock(&softc->lock);
1571                 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) {
1572                         if (cs2 != cs && cs2->cs_tasks_aborted == false &&
1573                             cs->cs_target == cs2->cs_target &&
1574                             strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) {
1575                                 cfiscsi_session_terminate(cs2);
1576                                 mtx_unlock(&softc->lock);
1577                                 pause("cfiscsi_reinstate", 1);
1578                                 goto restart;
1579                         }
1580                 }
1581                 mtx_unlock(&softc->lock);
1582         }
1583
1584         /*
1585          * Register initiator with CTL.
1586          */
1587         cfiscsi_session_register_initiator(cs);
1588
1589 #ifdef ICL_KERNEL_PROXY
1590         if (cihp->socket > 0) {
1591 #endif
1592                 error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1593                 if (error != 0) {
1594                         cfiscsi_session_terminate(cs);
1595                         refcount_release(&cs->cs_outstanding_ctl_pdus);
1596                         ci->status = CTL_ISCSI_ERROR;
1597                         snprintf(ci->error_str, sizeof(ci->error_str),
1598                             "%s: icl_conn_handoff failed with error %d",
1599                             __func__, error);
1600                         return;
1601                 }
1602 #ifdef ICL_KERNEL_PROXY
1603         }
1604 #endif
1605
1606 #ifdef ICL_KERNEL_PROXY
1607         cs->cs_login_phase = false;
1608
1609         /*
1610          * First PDU of the Full Feature phase has likely already arrived.
1611          * We have to pick it up and execute properly.
1612          */
1613         if (cs->cs_login_pdu != NULL) {
1614                 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1615                 cfiscsi_pdu_handle(cs->cs_login_pdu);
1616                 cs->cs_login_pdu = NULL;
1617         }
1618 #endif
1619
1620         refcount_release(&cs->cs_outstanding_ctl_pdus);
1621         ci->status = CTL_ISCSI_OK;
1622 }
1623
1624 static void
1625 cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1626 {
1627         struct ctl_iscsi_list_params *cilp;
1628         struct cfiscsi_session *cs;
1629         struct cfiscsi_softc *softc;
1630         struct sbuf *sb;
1631         int error;
1632
1633         cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1634         softc = &cfiscsi_softc;
1635
1636         sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1637         if (sb == NULL) {
1638                 ci->status = CTL_ISCSI_ERROR;
1639                 snprintf(ci->error_str, sizeof(ci->error_str),
1640                     "Unable to allocate %d bytes for iSCSI session list",
1641                     cilp->alloc_len);
1642                 return;
1643         }
1644
1645         sbuf_printf(sb, "<ctlislist>\n");
1646         mtx_lock(&softc->lock);
1647         TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1648 #ifdef ICL_KERNEL_PROXY
1649                 if (cs->cs_target == NULL)
1650                         continue;
1651 #endif
1652                 error = sbuf_printf(sb, "<connection id=\"%d\">"
1653                     "<initiator>%s</initiator>"
1654                     "<initiator_addr>%s</initiator_addr>"
1655                     "<initiator_alias>%s</initiator_alias>"
1656                     "<target>%s</target>"
1657                     "<target_alias>%s</target_alias>"
1658                     "<target_portal_group_tag>%u</target_portal_group_tag>"
1659                     "<header_digest>%s</header_digest>"
1660                     "<data_digest>%s</data_digest>"
1661                     "<max_data_segment_length>%zd</max_data_segment_length>"
1662                     "<immediate_data>%d</immediate_data>"
1663                     "<iser>%d</iser>"
1664                     "<offload>%s</offload>"
1665                     "</connection>\n",
1666                     cs->cs_id,
1667                     cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1668                     cs->cs_target->ct_name, cs->cs_target->ct_alias,
1669                     cs->cs_target->ct_tag,
1670                     cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1671                     cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1672                     cs->cs_max_data_segment_length,
1673                     cs->cs_immediate_data,
1674                     cs->cs_conn->ic_iser,
1675                     cs->cs_conn->ic_offload);
1676                 if (error != 0)
1677                         break;
1678         }
1679         mtx_unlock(&softc->lock);
1680         error = sbuf_printf(sb, "</ctlislist>\n");
1681         if (error != 0) {
1682                 sbuf_delete(sb);
1683                 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1684                 snprintf(ci->error_str, sizeof(ci->error_str),
1685                     "Out of space, %d bytes is too small", cilp->alloc_len);
1686                 return;
1687         }
1688         sbuf_finish(sb);
1689
1690         error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1691         cilp->fill_len = sbuf_len(sb) + 1;
1692         ci->status = CTL_ISCSI_OK;
1693         sbuf_delete(sb);
1694 }
1695
1696 static void
1697 cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1698 {
1699         struct icl_pdu *response;
1700         struct iscsi_bhs_asynchronous_message *bhsam;
1701         struct ctl_iscsi_logout_params *cilp;
1702         struct cfiscsi_session *cs;
1703         struct cfiscsi_softc *softc;
1704         int found = 0;
1705
1706         cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1707         softc = &cfiscsi_softc;
1708
1709         mtx_lock(&softc->lock);
1710         TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1711                 if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1712                     strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1713                     strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1714                         continue;
1715
1716                 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1717                 if (response == NULL) {
1718                         ci->status = CTL_ISCSI_ERROR;
1719                         snprintf(ci->error_str, sizeof(ci->error_str),
1720                             "Unable to allocate memory");
1721                         mtx_unlock(&softc->lock);
1722                         return;
1723                 }
1724                 bhsam =
1725                     (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1726                 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1727                 bhsam->bhsam_flags = 0x80;
1728                 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1729                 bhsam->bhsam_parameter3 = htons(10);
1730                 cfiscsi_pdu_queue(response);
1731                 found++;
1732         }
1733         mtx_unlock(&softc->lock);
1734
1735         if (found == 0) {
1736                 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1737                 snprintf(ci->error_str, sizeof(ci->error_str),
1738                     "No matching connections found");
1739                 return;
1740         }
1741
1742         ci->status = CTL_ISCSI_OK;
1743 }
1744
1745 static void
1746 cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1747 {
1748         struct icl_pdu *response;
1749         struct iscsi_bhs_asynchronous_message *bhsam;
1750         struct ctl_iscsi_terminate_params *citp;
1751         struct cfiscsi_session *cs;
1752         struct cfiscsi_softc *softc;
1753         int found = 0;
1754
1755         citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1756         softc = &cfiscsi_softc;
1757
1758         mtx_lock(&softc->lock);
1759         TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1760                 if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1761                     strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1762                     strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1763                         continue;
1764
1765                 response = icl_pdu_new(cs->cs_conn, M_NOWAIT);
1766                 if (response == NULL) {
1767                         /*
1768                          * Oh well.  Just terminate the connection.
1769                          */
1770                 } else {
1771                         bhsam = (struct iscsi_bhs_asynchronous_message *)
1772                             response->ip_bhs;
1773                         bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1774                         bhsam->bhsam_flags = 0x80;
1775                         bhsam->bhsam_0xffffffff = 0xffffffff;
1776                         bhsam->bhsam_async_event =
1777                             BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1778                         cfiscsi_pdu_queue(response);
1779                 }
1780                 cfiscsi_session_terminate(cs);
1781                 found++;
1782         }
1783         mtx_unlock(&softc->lock);
1784
1785         if (found == 0) {
1786                 ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1787                 snprintf(ci->error_str, sizeof(ci->error_str),
1788                     "No matching connections found");
1789                 return;
1790         }
1791
1792         ci->status = CTL_ISCSI_OK;
1793 }
1794
1795 static void
1796 cfiscsi_ioctl_limits(struct ctl_iscsi *ci)
1797 {
1798         struct ctl_iscsi_limits_params *cilp;
1799         int error;
1800
1801         cilp = (struct ctl_iscsi_limits_params *)&(ci->data);
1802
1803         error = icl_limits(cilp->offload, &cilp->data_segment_limit);
1804         if (error != 0) {
1805                 ci->status = CTL_ISCSI_ERROR;
1806                 snprintf(ci->error_str, sizeof(ci->error_str),
1807                         "%s: icl_limits failed with error %d",
1808                         __func__, error);
1809                 return;
1810         }
1811
1812         ci->status = CTL_ISCSI_OK;
1813 }
1814
1815 #ifdef ICL_KERNEL_PROXY
1816 static void
1817 cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1818 {
1819         struct ctl_iscsi_listen_params *cilp;
1820         struct sockaddr *sa;
1821         int error;
1822
1823         cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1824
1825         if (cfiscsi_softc.listener == NULL) {
1826                 CFISCSI_DEBUG("no listener");
1827                 snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1828                 ci->status = CTL_ISCSI_ERROR;
1829                 return;
1830         }
1831
1832         error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1833         if (error != 0) {
1834                 CFISCSI_DEBUG("getsockaddr, error %d", error);
1835                 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1836                 ci->status = CTL_ISCSI_ERROR;
1837                 return;
1838         }
1839
1840         error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1841             cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1842         if (error != 0) {
1843                 free(sa, M_SONAME);
1844                 CFISCSI_DEBUG("icl_listen_add, error %d", error);
1845                 snprintf(ci->error_str, sizeof(ci->error_str),
1846                     "icl_listen_add failed, error %d", error);
1847                 ci->status = CTL_ISCSI_ERROR;
1848                 return;
1849         }
1850
1851         ci->status = CTL_ISCSI_OK;
1852 }
1853
1854 static void
1855 cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1856 {
1857         struct ctl_iscsi_accept_params *ciap;
1858         struct cfiscsi_session *cs;
1859         int error;
1860
1861         ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1862
1863         mtx_lock(&cfiscsi_softc.lock);
1864         for (;;) {
1865                 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1866                         if (cs->cs_waiting_for_ctld)
1867                                 break;
1868                 }
1869                 if (cs != NULL)
1870                         break;
1871                 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1872                 if (error != 0) {
1873                         mtx_unlock(&cfiscsi_softc.lock);
1874                         snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1875                         ci->status = CTL_ISCSI_ERROR;
1876                         return;
1877                 }
1878         }
1879         mtx_unlock(&cfiscsi_softc.lock);
1880
1881         cs->cs_waiting_for_ctld = false;
1882         cs->cs_login_phase = true;
1883
1884         ciap->connection_id = cs->cs_id;
1885         ciap->portal_id = cs->cs_portal_id;
1886         ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1887         error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1888             cs->cs_initiator_sa->sa_len);
1889         if (error != 0) {
1890                 snprintf(ci->error_str, sizeof(ci->error_str),
1891                     "copyout failed with error %d", error);
1892                 ci->status = CTL_ISCSI_ERROR;
1893                 return;
1894         }
1895
1896         ci->status = CTL_ISCSI_OK;
1897 }
1898
1899 static void
1900 cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1901 {
1902         struct ctl_iscsi_send_params *cisp;
1903         struct cfiscsi_session *cs;
1904         struct icl_pdu *ip;
1905         size_t datalen;
1906         void *data;
1907         int error;
1908
1909         cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1910
1911         mtx_lock(&cfiscsi_softc.lock);
1912         TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1913                 if (cs->cs_id == cisp->connection_id)
1914                         break;
1915         }
1916         if (cs == NULL) {
1917                 mtx_unlock(&cfiscsi_softc.lock);
1918                 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1919                 ci->status = CTL_ISCSI_ERROR;
1920                 return;
1921         }
1922         mtx_unlock(&cfiscsi_softc.lock);
1923
1924 #if 0
1925         if (cs->cs_login_phase == false)
1926                 return (EBUSY);
1927 #endif
1928
1929         if (cs->cs_terminating) {
1930                 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1931                 ci->status = CTL_ISCSI_ERROR;
1932                 return;
1933         }
1934
1935         datalen = cisp->data_segment_len;
1936         /*
1937          * XXX
1938          */
1939         //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1940         if (datalen > 65535) {
1941                 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1942                 ci->status = CTL_ISCSI_ERROR;
1943                 return;
1944         }
1945         if (datalen > 0) {
1946                 data = malloc(datalen, M_CFISCSI, M_WAITOK);
1947                 error = copyin(cisp->data_segment, data, datalen);
1948                 if (error != 0) {
1949                         free(data, M_CFISCSI);
1950                         snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1951                         ci->status = CTL_ISCSI_ERROR;
1952                         return;
1953                 }
1954         }
1955
1956         ip = icl_pdu_new(cs->cs_conn, M_WAITOK);
1957         memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1958         if (datalen > 0) {
1959                 icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1960                 free(data, M_CFISCSI);
1961         }
1962         CFISCSI_SESSION_LOCK(cs);
1963         icl_pdu_queue(ip);
1964         CFISCSI_SESSION_UNLOCK(cs);
1965         ci->status = CTL_ISCSI_OK;
1966 }
1967
1968 static void
1969 cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1970 {
1971         struct ctl_iscsi_receive_params *cirp;
1972         struct cfiscsi_session *cs;
1973         struct icl_pdu *ip;
1974         void *data;
1975         int error;
1976
1977         cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1978
1979         mtx_lock(&cfiscsi_softc.lock);
1980         TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1981                 if (cs->cs_id == cirp->connection_id)
1982                         break;
1983         }
1984         if (cs == NULL) {
1985                 mtx_unlock(&cfiscsi_softc.lock);
1986                 snprintf(ci->error_str, sizeof(ci->error_str),
1987                     "connection not found");
1988                 ci->status = CTL_ISCSI_ERROR;
1989                 return;
1990         }
1991         mtx_unlock(&cfiscsi_softc.lock);
1992
1993 #if 0
1994         if (is->is_login_phase == false)
1995                 return (EBUSY);
1996 #endif
1997
1998         CFISCSI_SESSION_LOCK(cs);
1999         while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
2000                 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
2001                 if (error != 0) {
2002                         CFISCSI_SESSION_UNLOCK(cs);
2003                         snprintf(ci->error_str, sizeof(ci->error_str),
2004                             "interrupted by signal");
2005                         ci->status = CTL_ISCSI_ERROR;
2006                         return;
2007                 }
2008         }
2009
2010         if (cs->cs_terminating) {
2011                 CFISCSI_SESSION_UNLOCK(cs);
2012                 snprintf(ci->error_str, sizeof(ci->error_str),
2013                     "connection terminating");
2014                 ci->status = CTL_ISCSI_ERROR;
2015                 return;
2016         }
2017         ip = cs->cs_login_pdu;
2018         cs->cs_login_pdu = NULL;
2019         CFISCSI_SESSION_UNLOCK(cs);
2020
2021         if (ip->ip_data_len > cirp->data_segment_len) {
2022                 icl_pdu_free(ip);
2023                 snprintf(ci->error_str, sizeof(ci->error_str),
2024                     "data segment too big");
2025                 ci->status = CTL_ISCSI_ERROR;
2026                 return;
2027         }
2028
2029         copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
2030         if (ip->ip_data_len > 0) {
2031                 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
2032                 icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
2033                 copyout(data, cirp->data_segment, ip->ip_data_len);
2034                 free(data, M_CFISCSI);
2035         }
2036
2037         icl_pdu_free(ip);
2038         ci->status = CTL_ISCSI_OK;
2039 }
2040
2041 #endif /* !ICL_KERNEL_PROXY */
2042
2043 static void
2044 cfiscsi_ioctl_port_create(struct ctl_req *req)
2045 {
2046         struct cfiscsi_target *ct;
2047         struct ctl_port *port;
2048         const char *target, *alias, *tags;
2049         struct scsi_vpd_id_descriptor *desc;
2050         ctl_options_t opts;
2051         int retval, len, idlen;
2052         uint16_t tag;
2053
2054         ctl_init_opts(&opts, req->num_args, req->kern_args);
2055         target = ctl_get_opt(&opts, "cfiscsi_target");
2056         alias = ctl_get_opt(&opts, "cfiscsi_target_alias");
2057         tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2058         if (target == NULL || tags == NULL) {
2059                 req->status = CTL_LUN_ERROR;
2060                 snprintf(req->error_str, sizeof(req->error_str),
2061                     "Missing required argument");
2062                 ctl_free_opts(&opts);
2063                 return;
2064         }
2065         tag = strtol(tags, (char **)NULL, 10);
2066         ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag);
2067         if (ct == NULL) {
2068                 req->status = CTL_LUN_ERROR;
2069                 snprintf(req->error_str, sizeof(req->error_str),
2070                     "failed to create target \"%s\"", target);
2071                 ctl_free_opts(&opts);
2072                 return;
2073         }
2074         if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) {
2075                 req->status = CTL_LUN_ERROR;
2076                 snprintf(req->error_str, sizeof(req->error_str),
2077                     "target \"%s\" already exists", target);
2078                 cfiscsi_target_release(ct);
2079                 ctl_free_opts(&opts);
2080                 return;
2081         }
2082         port = &ct->ct_port;
2083         // WAT
2084         if (ct->ct_state == CFISCSI_TARGET_STATE_DYING)
2085                 goto done;
2086
2087         port->frontend = &cfiscsi_frontend;
2088         port->port_type = CTL_PORT_ISCSI;
2089         /* XXX KDM what should the real number be here? */
2090         port->num_requested_ctl_io = 4096;
2091         port->port_name = "iscsi";
2092         port->physical_port = tag;
2093         port->virtual_port = ct->ct_target_id;
2094         port->port_online = cfiscsi_online;
2095         port->port_offline = cfiscsi_offline;
2096         port->port_info = cfiscsi_info;
2097         port->onoff_arg = ct;
2098         port->fe_datamove = cfiscsi_datamove;
2099         port->fe_done = cfiscsi_done;
2100
2101         /* XXX KDM what should we report here? */
2102         /* XXX These should probably be fetched from CTL. */
2103         port->max_targets = 1;
2104         port->max_target_id = 15;
2105         port->targ_port = -1;
2106
2107         port->options = opts;
2108         STAILQ_INIT(&opts);
2109
2110         /* Generate Port ID. */
2111         idlen = strlen(target) + strlen(",t,0x0001") + 1;
2112         idlen = roundup2(idlen, 4);
2113         len = sizeof(struct scsi_vpd_device_id) + idlen;
2114         port->port_devid = malloc(sizeof(struct ctl_devid) + len,
2115             M_CTL, M_WAITOK | M_ZERO);
2116         port->port_devid->len = len;
2117         desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data;
2118         desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2119         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2120             SVPD_ID_TYPE_SCSI_NAME;
2121         desc->length = idlen;
2122         snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag);
2123
2124         /* Generate Target ID. */
2125         idlen = strlen(target) + 1;
2126         idlen = roundup2(idlen, 4);
2127         len = sizeof(struct scsi_vpd_device_id) + idlen;
2128         port->target_devid = malloc(sizeof(struct ctl_devid) + len,
2129             M_CTL, M_WAITOK | M_ZERO);
2130         port->target_devid->len = len;
2131         desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data;
2132         desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2133         desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2134             SVPD_ID_TYPE_SCSI_NAME;
2135         desc->length = idlen;
2136         strlcpy(desc->identifier, target, idlen);
2137
2138         retval = ctl_port_register(port);
2139         if (retval != 0) {
2140                 ctl_free_opts(&port->options);
2141                 cfiscsi_target_release(ct);
2142                 free(port->port_devid, M_CFISCSI);
2143                 free(port->target_devid, M_CFISCSI);
2144                 req->status = CTL_LUN_ERROR;
2145                 snprintf(req->error_str, sizeof(req->error_str),
2146                     "ctl_port_register() failed with error %d", retval);
2147                 return;
2148         }
2149 done:
2150         ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2151         req->status = CTL_LUN_OK;
2152         memcpy(req->kern_args[0].kvalue, &port->targ_port,
2153             sizeof(port->targ_port)); //XXX
2154 }
2155
2156 static void
2157 cfiscsi_ioctl_port_remove(struct ctl_req *req)
2158 {
2159         struct cfiscsi_target *ct;
2160         const char *target, *tags;
2161         ctl_options_t opts;
2162         uint16_t tag;
2163
2164         ctl_init_opts(&opts, req->num_args, req->kern_args);
2165         target = ctl_get_opt(&opts, "cfiscsi_target");
2166         tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag");
2167         if (target == NULL || tags == NULL) {
2168                 ctl_free_opts(&opts);
2169                 req->status = CTL_LUN_ERROR;
2170                 snprintf(req->error_str, sizeof(req->error_str),
2171                     "Missing required argument");
2172                 return;
2173         }
2174         tag = strtol(tags, (char **)NULL, 10);
2175         ct = cfiscsi_target_find(&cfiscsi_softc, target, tag);
2176         if (ct == NULL) {
2177                 ctl_free_opts(&opts);
2178                 req->status = CTL_LUN_ERROR;
2179                 snprintf(req->error_str, sizeof(req->error_str),
2180                     "can't find target \"%s\"", target);
2181                 return;
2182         }
2183         if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) {
2184                 ctl_free_opts(&opts);
2185                 req->status = CTL_LUN_ERROR;
2186                 snprintf(req->error_str, sizeof(req->error_str),
2187                     "target \"%s\" is already dying", target);
2188                 return;
2189         }
2190         ctl_free_opts(&opts);
2191
2192         ct->ct_state = CFISCSI_TARGET_STATE_DYING;
2193         ctl_port_offline(&ct->ct_port);
2194         cfiscsi_target_release(ct);
2195         cfiscsi_target_release(ct);
2196         req->status = CTL_LUN_OK;
2197 }
2198
2199 static int
2200 cfiscsi_ioctl(struct cdev *dev,
2201     u_long cmd, caddr_t addr, int flag, struct thread *td)
2202 {
2203         struct ctl_iscsi *ci;
2204         struct ctl_req *req;
2205
2206         if (cmd == CTL_PORT_REQ) {
2207                 req = (struct ctl_req *)addr;
2208                 switch (req->reqtype) {
2209                 case CTL_REQ_CREATE:
2210                         cfiscsi_ioctl_port_create(req);
2211                         break;
2212                 case CTL_REQ_REMOVE:
2213                         cfiscsi_ioctl_port_remove(req);
2214                         break;
2215                 default:
2216                         req->status = CTL_LUN_ERROR;
2217                         snprintf(req->error_str, sizeof(req->error_str),
2218                             "Unsupported request type %d", req->reqtype);
2219                 }
2220                 return (0);
2221         }
2222
2223         if (cmd != CTL_ISCSI)
2224                 return (ENOTTY);
2225
2226         ci = (struct ctl_iscsi *)addr;
2227         switch (ci->type) {
2228         case CTL_ISCSI_HANDOFF:
2229                 cfiscsi_ioctl_handoff(ci);
2230                 break;
2231         case CTL_ISCSI_LIST:
2232                 cfiscsi_ioctl_list(ci);
2233                 break;
2234         case CTL_ISCSI_LOGOUT:
2235                 cfiscsi_ioctl_logout(ci);
2236                 break;
2237         case CTL_ISCSI_TERMINATE:
2238                 cfiscsi_ioctl_terminate(ci);
2239                 break;
2240         case CTL_ISCSI_LIMITS:
2241                 cfiscsi_ioctl_limits(ci);
2242                 break;
2243 #ifdef ICL_KERNEL_PROXY
2244         case CTL_ISCSI_LISTEN:
2245                 cfiscsi_ioctl_listen(ci);
2246                 break;
2247         case CTL_ISCSI_ACCEPT:
2248                 cfiscsi_ioctl_accept(ci);
2249                 break;
2250         case CTL_ISCSI_SEND:
2251                 cfiscsi_ioctl_send(ci);
2252                 break;
2253         case CTL_ISCSI_RECEIVE:
2254                 cfiscsi_ioctl_receive(ci);
2255                 break;
2256 #else
2257         case CTL_ISCSI_LISTEN:
2258         case CTL_ISCSI_ACCEPT:
2259         case CTL_ISCSI_SEND:
2260         case CTL_ISCSI_RECEIVE:
2261                 ci->status = CTL_ISCSI_ERROR;
2262                 snprintf(ci->error_str, sizeof(ci->error_str),
2263                     "%s: CTL compiled without ICL_KERNEL_PROXY",
2264                     __func__);
2265                 break;
2266 #endif /* !ICL_KERNEL_PROXY */
2267         default:
2268                 ci->status = CTL_ISCSI_ERROR;
2269                 snprintf(ci->error_str, sizeof(ci->error_str),
2270                     "%s: invalid iSCSI request type %d", __func__, ci->type);
2271                 break;
2272         }
2273
2274         return (0);
2275 }
2276
2277 static void
2278 cfiscsi_target_hold(struct cfiscsi_target *ct)
2279 {
2280
2281         refcount_acquire(&ct->ct_refcount);
2282 }
2283
2284 static void
2285 cfiscsi_target_release(struct cfiscsi_target *ct)
2286 {
2287         struct cfiscsi_softc *softc;
2288
2289         softc = ct->ct_softc;
2290         mtx_lock(&softc->lock);
2291         if (refcount_release(&ct->ct_refcount)) {
2292                 TAILQ_REMOVE(&softc->targets, ct, ct_next);
2293                 mtx_unlock(&softc->lock);
2294                 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) {
2295                         ct->ct_state = CFISCSI_TARGET_STATE_INVALID;
2296                         if (ctl_port_deregister(&ct->ct_port) != 0)
2297                                 printf("%s: ctl_port_deregister() failed\n",
2298                                     __func__);
2299                 }
2300                 free(ct, M_CFISCSI);
2301
2302                 return;
2303         }
2304         mtx_unlock(&softc->lock);
2305 }
2306
2307 static struct cfiscsi_target *
2308 cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag)
2309 {
2310         struct cfiscsi_target *ct;
2311
2312         mtx_lock(&softc->lock);
2313         TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2314                 if (ct->ct_tag != tag ||
2315                     strcmp(name, ct->ct_name) != 0 ||
2316                     ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE)
2317                         continue;
2318                 cfiscsi_target_hold(ct);
2319                 mtx_unlock(&softc->lock);
2320                 return (ct);
2321         }
2322         mtx_unlock(&softc->lock);
2323
2324         return (NULL);
2325 }
2326
2327 static struct cfiscsi_target *
2328 cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2329     const char *alias, uint16_t tag)
2330 {
2331         struct cfiscsi_target *ct, *newct;
2332
2333         if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2334                 return (NULL);
2335
2336         newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2337
2338         mtx_lock(&softc->lock);
2339         TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2340                 if (ct->ct_tag != tag ||
2341                     strcmp(name, ct->ct_name) != 0 ||
2342                     ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2343                         continue;
2344                 cfiscsi_target_hold(ct);
2345                 mtx_unlock(&softc->lock);
2346                 free(newct, M_CFISCSI);
2347                 return (ct);
2348         }
2349
2350         strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2351         if (alias != NULL)
2352                 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2353         newct->ct_tag = tag;
2354         refcount_init(&newct->ct_refcount, 1);
2355         newct->ct_softc = softc;
2356         if (TAILQ_EMPTY(&softc->targets))
2357                 softc->last_target_id = 0;
2358         newct->ct_target_id = ++softc->last_target_id;
2359         TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2360         mtx_unlock(&softc->lock);
2361
2362         return (newct);
2363 }
2364
2365 static void
2366 cfiscsi_datamove_in(union ctl_io *io)
2367 {
2368         struct cfiscsi_session *cs;
2369         struct icl_pdu *request, *response;
2370         const struct iscsi_bhs_scsi_command *bhssc;
2371         struct iscsi_bhs_data_in *bhsdi;
2372         struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2373         size_t len, expected_len, sg_len, buffer_offset;
2374         const char *sg_addr;
2375         int ctl_sg_count, error, i;
2376
2377         request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2378         cs = PDU_SESSION(request);
2379
2380         bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2381         KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2382             ISCSI_BHS_OPCODE_SCSI_COMMAND,
2383             ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2384
2385         if (io->scsiio.kern_sg_entries > 0) {
2386                 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2387                 ctl_sg_count = io->scsiio.kern_sg_entries;
2388         } else {
2389                 ctl_sglist = &ctl_sg_entry;
2390                 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2391                 ctl_sglist->len = io->scsiio.kern_data_len;
2392                 ctl_sg_count = 1;
2393         }
2394
2395         /*
2396          * This is the total amount of data to be transferred within the current
2397          * SCSI command.  We need to record it so that we can properly report
2398          * underflow/underflow.
2399          */
2400         PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2401
2402         /*
2403          * This is the offset within the current SCSI command; for the first
2404          * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2405          * it will be the sum of lengths of previous ones.
2406          */
2407         buffer_offset = io->scsiio.kern_rel_offset;
2408
2409         /*
2410          * This is the transfer length expected by the initiator.  In theory,
2411          * it could be different from the correct amount of data from the SCSI
2412          * point of view, even if that doesn't make any sense.
2413          */
2414         expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2415 #if 0
2416         if (expected_len != io->scsiio.kern_total_len) {
2417                 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2418                     "actual length %zd", expected_len,
2419                     (size_t)io->scsiio.kern_total_len);
2420         }
2421 #endif
2422
2423         if (buffer_offset >= expected_len) {
2424 #if 0
2425                 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2426                     "already sent the expected len", buffer_offset);
2427 #endif
2428                 io->scsiio.be_move_done(io);
2429                 return;
2430         }
2431
2432         i = 0;
2433         sg_addr = NULL;
2434         sg_len = 0;
2435         response = NULL;
2436         bhsdi = NULL;
2437         for (;;) {
2438                 if (response == NULL) {
2439                         response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2440                         if (response == NULL) {
2441                                 CFISCSI_SESSION_WARN(cs, "failed to "
2442                                     "allocate memory; dropping connection");
2443                                 ctl_set_busy(&io->scsiio);
2444                                 io->scsiio.be_move_done(io);
2445                                 cfiscsi_session_terminate(cs);
2446                                 return;
2447                         }
2448                         bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2449                         bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2450                         bhsdi->bhsdi_initiator_task_tag =
2451                             bhssc->bhssc_initiator_task_tag;
2452                         bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2453                         PDU_EXPDATASN(request)++;
2454                         bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2455                 }
2456
2457                 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2458                 if (sg_len == 0) {
2459                         sg_addr = ctl_sglist[i].addr;
2460                         sg_len = ctl_sglist[i].len;
2461                         KASSERT(sg_len > 0, ("sg_len <= 0"));
2462                 }
2463
2464                 len = sg_len;
2465
2466                 /*
2467                  * Truncate to maximum data segment length.
2468                  */
2469                 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2470                     ("ip_data_len %zd >= max_data_segment_length %zd",
2471                     response->ip_data_len, cs->cs_max_data_segment_length));
2472                 if (response->ip_data_len + len >
2473                     cs->cs_max_data_segment_length) {
2474                         len = cs->cs_max_data_segment_length -
2475                             response->ip_data_len;
2476                         KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2477                             len, sg_len));
2478                 }
2479
2480                 /*
2481                  * Truncate to expected data transfer length.
2482                  */
2483                 KASSERT(buffer_offset + response->ip_data_len < expected_len,
2484                     ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2485                     buffer_offset, response->ip_data_len, expected_len));
2486                 if (buffer_offset + response->ip_data_len + len > expected_len) {
2487                         CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2488                             "to expected data transfer length %zd",
2489                             buffer_offset + response->ip_data_len + len, expected_len);
2490                         len = expected_len - (buffer_offset + response->ip_data_len);
2491                         KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2492                             len, sg_len));
2493                 }
2494
2495                 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2496                 if (error != 0) {
2497                         CFISCSI_SESSION_WARN(cs, "failed to "
2498                             "allocate memory; dropping connection");
2499                         icl_pdu_free(response);
2500                         ctl_set_busy(&io->scsiio);
2501                         io->scsiio.be_move_done(io);
2502                         cfiscsi_session_terminate(cs);
2503                         return;
2504                 }
2505                 sg_addr += len;
2506                 sg_len -= len;
2507
2508                 KASSERT(buffer_offset + response->ip_data_len <= expected_len,
2509                     ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2510                     buffer_offset, response->ip_data_len, expected_len));
2511                 if (buffer_offset + response->ip_data_len == expected_len) {
2512                         /*
2513                          * Already have the amount of data the initiator wanted.
2514                          */
2515                         break;
2516                 }
2517
2518                 if (sg_len == 0) {
2519                         /*
2520                          * End of scatter-gather segment;
2521                          * proceed to the next one...
2522                          */
2523                         if (i == ctl_sg_count - 1) {
2524                                 /*
2525                                  * ... unless this was the last one.
2526                                  */
2527                                 break;
2528                         }
2529                         i++;
2530                 }
2531
2532                 if (response->ip_data_len == cs->cs_max_data_segment_length) {
2533                         /*
2534                          * Can't stuff more data into the current PDU;
2535                          * queue it.  Note that's not enough to check
2536                          * for kern_data_resid == 0 instead; there
2537                          * may be several Data-In PDUs for the final
2538                          * call to cfiscsi_datamove(), and we want
2539                          * to set the F flag only on the last of them.
2540                          */
2541                         buffer_offset += response->ip_data_len;
2542                         if (buffer_offset == io->scsiio.kern_total_len ||
2543                             buffer_offset == expected_len) {
2544                                 buffer_offset -= response->ip_data_len;
2545                                 break;
2546                         }
2547                         cfiscsi_pdu_queue(response);
2548                         response = NULL;
2549                         bhsdi = NULL;
2550                 }
2551         }
2552         if (response != NULL) {
2553                 buffer_offset += response->ip_data_len;
2554                 if (buffer_offset == io->scsiio.kern_total_len ||
2555                     buffer_offset == expected_len) {
2556                         bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2557                         if (io->io_hdr.status == CTL_SUCCESS) {
2558                                 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S;
2559                                 if (PDU_TOTAL_TRANSFER_LEN(request) <
2560                                     ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2561                                         bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2562                                         bhsdi->bhsdi_residual_count =
2563                                             htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2564                                             PDU_TOTAL_TRANSFER_LEN(request));
2565                                 } else if (PDU_TOTAL_TRANSFER_LEN(request) >
2566                                     ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2567                                         bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2568                                         bhsdi->bhsdi_residual_count =
2569                                             htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2570                                             ntohl(bhssc->bhssc_expected_data_transfer_length));
2571                                 }
2572                                 bhsdi->bhsdi_status = io->scsiio.scsi_status;
2573                                 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
2574                         }
2575                 }
2576                 KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2577                 cfiscsi_pdu_queue(response);
2578         }
2579
2580         io->scsiio.be_move_done(io);
2581 }
2582
2583 static void
2584 cfiscsi_datamove_out(union ctl_io *io)
2585 {
2586         struct cfiscsi_session *cs;
2587         struct icl_pdu *request, *response;
2588         const struct iscsi_bhs_scsi_command *bhssc;
2589         struct iscsi_bhs_r2t *bhsr2t;
2590         struct cfiscsi_data_wait *cdw;
2591         struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2592         uint32_t expected_len, r2t_off, r2t_len;
2593         uint32_t target_transfer_tag;
2594         bool done;
2595
2596         request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2597         cs = PDU_SESSION(request);
2598
2599         bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2600         KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2601             ISCSI_BHS_OPCODE_SCSI_COMMAND,
2602             ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2603
2604         /*
2605          * We need to record it so that we can properly report
2606          * underflow/underflow.
2607          */
2608         PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2609
2610         /*
2611          * Report write underflow as error since CTL and backends don't
2612          * really support it, and SCSI does not tell how to do it right.
2613          */
2614         expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2615         if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len >
2616             expected_len) {
2617                 io->scsiio.io_hdr.port_status = 43;
2618                 io->scsiio.be_move_done(io);
2619                 return;
2620         }
2621
2622         target_transfer_tag =
2623             atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2624         cdw = cfiscsi_data_wait_new(cs, io, bhssc->bhssc_initiator_task_tag,
2625             &target_transfer_tag);
2626         if (cdw == NULL) {
2627                 CFISCSI_SESSION_WARN(cs, "failed to "
2628                     "allocate memory; dropping connection");
2629                 ctl_set_busy(&io->scsiio);
2630                 io->scsiio.be_move_done(io);
2631                 cfiscsi_session_terminate(cs);
2632                 return;
2633         }
2634 #if 0
2635         CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2636             "task tag 0x%x, target transfer tag 0x%x",
2637             bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2638 #endif
2639
2640         cdw->cdw_ctl_io = io;
2641         cdw->cdw_target_transfer_tag = target_transfer_tag;
2642         cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2643         cdw->cdw_r2t_end = io->scsiio.kern_data_len;
2644         cdw->cdw_datasn = 0;
2645
2646         /* Set initial data pointer for the CDW respecting ext_data_filled. */
2647         if (io->scsiio.kern_sg_entries > 0) {
2648                 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2649         } else {
2650                 ctl_sglist = &ctl_sg_entry;
2651                 ctl_sglist->addr = io->scsiio.kern_data_ptr;
2652                 ctl_sglist->len = io->scsiio.kern_data_len;
2653         }
2654         cdw->cdw_sg_index = 0;
2655         cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2656         cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2657         r2t_off = io->scsiio.ext_data_filled;
2658         while (r2t_off > 0) {
2659                 if (r2t_off >= cdw->cdw_sg_len) {
2660                         r2t_off -= cdw->cdw_sg_len;
2661                         cdw->cdw_sg_index++;
2662                         cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
2663                         cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
2664                         continue;
2665                 }
2666                 cdw->cdw_sg_addr += r2t_off;
2667                 cdw->cdw_sg_len -= r2t_off;
2668                 r2t_off = 0;
2669         }
2670
2671         if (cs->cs_immediate_data &&
2672             io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled <
2673             icl_pdu_data_segment_length(request)) {
2674                 done = cfiscsi_handle_data_segment(request, cdw);
2675                 if (done) {
2676                         cfiscsi_data_wait_free(cs, cdw);
2677                         io->scsiio.be_move_done(io);
2678                         return;
2679                 }
2680         }
2681
2682         r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled;
2683         r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled,
2684             cs->cs_max_burst_length);
2685         cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len;
2686
2687         CFISCSI_SESSION_LOCK(cs);
2688         TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2689         CFISCSI_SESSION_UNLOCK(cs);
2690
2691         /*
2692          * XXX: We should limit the number of outstanding R2T PDUs
2693          *      per task to MaxOutstandingR2T.
2694          */
2695         response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2696         if (response == NULL) {
2697                 CFISCSI_SESSION_WARN(cs, "failed to "
2698                     "allocate memory; dropping connection");
2699                 ctl_set_busy(&io->scsiio);
2700                 io->scsiio.be_move_done(io);
2701                 cfiscsi_session_terminate(cs);
2702                 return;
2703         }
2704         bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2705         bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2706         bhsr2t->bhsr2t_flags = 0x80;
2707         bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2708         bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2709         bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2710         /*
2711          * XXX: Here we assume that cfiscsi_datamove() won't ever
2712          *      be running concurrently on several CPUs for a given
2713          *      command.
2714          */
2715         bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2716         PDU_R2TSN(request)++;
2717         /*
2718          * This is the offset within the current SCSI command;
2719          * i.e. for the first call of datamove(), it will be 0,
2720          * and for subsequent ones it will be the sum of lengths
2721          * of previous ones.
2722          *
2723          * The ext_data_filled is to account for unsolicited
2724          * (immediate) data that might have already arrived.
2725          */
2726         bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off);
2727         /*
2728          * This is the total length (sum of S/G lengths) this call
2729          * to cfiscsi_datamove() is supposed to handle, limited by
2730          * MaxBurstLength.
2731          */
2732         bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len);
2733         cfiscsi_pdu_queue(response);
2734 }
2735
2736 static void
2737 cfiscsi_datamove(union ctl_io *io)
2738 {
2739
2740         if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2741                 cfiscsi_datamove_in(io);
2742         else {
2743                 /* We hadn't received anything during this datamove yet. */
2744                 io->scsiio.ext_data_filled = 0;
2745                 cfiscsi_datamove_out(io);
2746         }
2747 }
2748
2749 static void
2750 cfiscsi_scsi_command_done(union ctl_io *io)
2751 {
2752         struct icl_pdu *request, *response;
2753         struct iscsi_bhs_scsi_command *bhssc;
2754         struct iscsi_bhs_scsi_response *bhssr;
2755 #ifdef DIAGNOSTIC
2756         struct cfiscsi_data_wait *cdw;
2757 #endif
2758         struct cfiscsi_session *cs;
2759         uint16_t sense_length;
2760
2761         request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2762         cs = PDU_SESSION(request);
2763         bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2764         KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2765             ISCSI_BHS_OPCODE_SCSI_COMMAND,
2766             ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2767
2768         //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2769         //    bhssc->bhssc_initiator_task_tag);
2770
2771 #ifdef DIAGNOSTIC
2772         CFISCSI_SESSION_LOCK(cs);
2773         TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2774                 KASSERT(bhssc->bhssc_initiator_task_tag !=
2775                     cdw->cdw_initiator_task_tag, ("dangling cdw"));
2776         CFISCSI_SESSION_UNLOCK(cs);
2777 #endif
2778
2779         /*
2780          * Do not return status for aborted commands.
2781          * There are exceptions, but none supported by CTL yet.
2782          */
2783         if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
2784              (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
2785             (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
2786                 ctl_free_io(io);
2787                 icl_pdu_free(request);
2788                 return;
2789         }
2790
2791         response = cfiscsi_pdu_new_response(request, M_WAITOK);
2792         bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2793         bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2794         bhssr->bhssr_flags = 0x80;
2795         /*
2796          * XXX: We don't deal with bidirectional under/overflows;
2797          *      does anything actually support those?
2798          */
2799         if (PDU_TOTAL_TRANSFER_LEN(request) <
2800             ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2801                 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2802                 bhssr->bhssr_residual_count =
2803                     htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2804                     PDU_TOTAL_TRANSFER_LEN(request));
2805                 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2806                 //    ntohl(bhssr->bhssr_residual_count));
2807         } else if (PDU_TOTAL_TRANSFER_LEN(request) > 
2808             ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2809                 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2810                 bhssr->bhssr_residual_count =
2811                     htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2812                     ntohl(bhssc->bhssc_expected_data_transfer_length));
2813                 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2814                 //    ntohl(bhssr->bhssr_residual_count));
2815         }
2816         bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2817         bhssr->bhssr_status = io->scsiio.scsi_status;
2818         bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2819         bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2820
2821         if (io->scsiio.sense_len > 0) {
2822 #if 0
2823                 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2824                     io->scsiio.sense_len);
2825 #endif
2826                 sense_length = htons(io->scsiio.sense_len);
2827                 icl_pdu_append_data(response,
2828                     &sense_length, sizeof(sense_length), M_WAITOK);
2829                 icl_pdu_append_data(response,
2830                     &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2831         }
2832
2833         ctl_free_io(io);
2834         icl_pdu_free(request);
2835         cfiscsi_pdu_queue(response);
2836 }
2837
2838 static void
2839 cfiscsi_task_management_done(union ctl_io *io)
2840 {
2841         struct icl_pdu *request, *response;
2842         struct iscsi_bhs_task_management_request *bhstmr;
2843         struct iscsi_bhs_task_management_response *bhstmr2;
2844         struct cfiscsi_data_wait *cdw, *tmpcdw;
2845         struct cfiscsi_session *cs;
2846
2847         request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2848         cs = PDU_SESSION(request);
2849         bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2850         KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2851             ISCSI_BHS_OPCODE_TASK_REQUEST,
2852             ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2853
2854 #if 0
2855         CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2856             bhstmr->bhstmr_initiator_task_tag,
2857             bhstmr->bhstmr_referenced_task_tag);
2858 #endif
2859
2860         if ((bhstmr->bhstmr_function & ~0x80) ==
2861             BHSTMR_FUNCTION_ABORT_TASK) {
2862                 /*
2863                  * Make sure we no longer wait for Data-Out for this command.
2864                  */
2865                 CFISCSI_SESSION_LOCK(cs);
2866                 TAILQ_FOREACH_SAFE(cdw,
2867                     &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2868                         if (bhstmr->bhstmr_referenced_task_tag !=
2869                             cdw->cdw_initiator_task_tag)
2870                                 continue;
2871
2872 #if 0
2873                         CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2874                             "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2875 #endif
2876                         TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2877                             cdw, cdw_next);
2878                         cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2879                         cfiscsi_data_wait_free(cs, cdw);
2880                 }
2881                 CFISCSI_SESSION_UNLOCK(cs);
2882         }
2883
2884         response = cfiscsi_pdu_new_response(request, M_WAITOK);
2885         bhstmr2 = (struct iscsi_bhs_task_management_response *)
2886             response->ip_bhs;
2887         bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2888         bhstmr2->bhstmr_flags = 0x80;
2889         if (io->io_hdr.status == CTL_SUCCESS) {
2890                 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2891         } else {
2892                 /*
2893                  * XXX: How to figure out what exactly went wrong?  iSCSI spec
2894                  *      expects us to provide detailed error, e.g. "Task does
2895                  *      not exist" or "LUN does not exist".
2896                  */
2897                 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2898                 bhstmr2->bhstmr_response =
2899                     BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2900         }
2901         bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2902
2903         ctl_free_io(io);
2904         icl_pdu_free(request);
2905         cfiscsi_pdu_queue(response);
2906 }
2907
2908 static void
2909 cfiscsi_done(union ctl_io *io)
2910 {
2911         struct icl_pdu *request;
2912         struct cfiscsi_session *cs;
2913
2914         KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2915                 ("invalid CTL status %#x", io->io_hdr.status));
2916
2917         if (io->io_hdr.io_type == CTL_IO_TASK &&
2918             io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
2919                 /*
2920                  * Implicit task termination has just completed; nothing to do.
2921                  */
2922                 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2923                 cs->cs_tasks_aborted = true;
2924                 refcount_release(&cs->cs_outstanding_ctl_pdus);
2925                 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus));
2926                 ctl_free_io(io);
2927                 return;
2928         }
2929
2930         request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2931         cs = PDU_SESSION(request);
2932         refcount_release(&cs->cs_outstanding_ctl_pdus);
2933
2934         switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2935         case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2936                 cfiscsi_scsi_command_done(io);
2937                 break;
2938         case ISCSI_BHS_OPCODE_TASK_REQUEST:
2939                 cfiscsi_task_management_done(io);
2940                 break;
2941         default:
2942                 panic("cfiscsi_done called with wrong opcode 0x%x",
2943                     request->ip_bhs->bhs_opcode);
2944         }
2945 }