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