ctl_frontend_iscsi.c revision 288697
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 288697 2015-10-05 07:29:26Z 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 288697 2015-10-05 07:29:26Z mav $"); 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_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, int lun_id); 154static int cfiscsi_lun_disable(void *arg, int lun_id); 155static int cfiscsi_ioctl(struct cdev *dev, 156 u_long cmd, caddr_t addr, int flag, struct thread *td); 157static void cfiscsi_datamove(union ctl_io *io); 158static void cfiscsi_datamove_in(union ctl_io *io); 159static void cfiscsi_datamove_out(union ctl_io *io); 160static void cfiscsi_done(union ctl_io *io); 161static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request); 162static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request); 163static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request); 164static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request); 165static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request); 166static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request); 167static void cfiscsi_session_terminate(struct cfiscsi_session *cs); 168static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc 169 *softc, const char *name, uint16_t tag); 170static struct cfiscsi_target *cfiscsi_target_find_or_create( 171 struct cfiscsi_softc *softc, const char *name, const char *alias, 172 uint16_t tag); 173static void cfiscsi_target_release(struct cfiscsi_target *ct); 174static void cfiscsi_session_delete(struct cfiscsi_session *cs); 175 176static struct cfiscsi_softc cfiscsi_softc; 177extern struct ctl_softc *control_softc; 178 179static struct ctl_frontend cfiscsi_frontend = 180{ 181 .name = "iscsi", 182 .init = cfiscsi_init, 183 .ioctl = cfiscsi_ioctl, 184}; 185CTL_FRONTEND_DECLARE(ctlcfiscsi, cfiscsi_frontend); 186MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1); 187 188static struct icl_pdu * 189cfiscsi_pdu_new_response(struct icl_pdu *request, int flags) 190{ 191 192 return (icl_pdu_new(request->ip_conn, flags)); 193} 194 195static bool 196cfiscsi_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 272static void 273cfiscsi_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 319static void 320cfiscsi_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 340static void 341cfiscsi_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 351static int 352cfiscsi_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 407static void 408cfiscsi_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 420static uint32_t 421cfiscsi_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 475static void 476cfiscsi_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 543static void 544cfiscsi_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.id = cs->cs_ctl_initid; 568 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port; 569 io->io_hdr.nexus.targ_target.id = 0; 570 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun); 571 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag; 572 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) { 573 case BHSSC_FLAGS_ATTR_UNTAGGED: 574 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 575 break; 576 case BHSSC_FLAGS_ATTR_SIMPLE: 577 io->scsiio.tag_type = CTL_TAG_SIMPLE; 578 break; 579 case BHSSC_FLAGS_ATTR_ORDERED: 580 io->scsiio.tag_type = CTL_TAG_ORDERED; 581 break; 582 case BHSSC_FLAGS_ATTR_HOQ: 583 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE; 584 break; 585 case BHSSC_FLAGS_ATTR_ACA: 586 io->scsiio.tag_type = CTL_TAG_ACA; 587 break; 588 default: 589 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 590 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d", 591 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR); 592 break; 593 } 594 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */ 595 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb)); 596 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 597 error = ctl_queue(io); 598 if (error != CTL_RETVAL_COMPLETE) { 599 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; " 600 "dropping connection", error); 601 ctl_free_io(io); 602 refcount_release(&cs->cs_outstanding_ctl_pdus); 603 icl_pdu_free(request); 604 cfiscsi_session_terminate(cs); 605 } 606} 607 608static void 609cfiscsi_pdu_handle_task_request(struct icl_pdu *request) 610{ 611 struct iscsi_bhs_task_management_request *bhstmr; 612 struct iscsi_bhs_task_management_response *bhstmr2; 613 struct icl_pdu *response; 614 struct cfiscsi_session *cs; 615 union ctl_io *io; 616 int error; 617 618 cs = PDU_SESSION(request); 619 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 620 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref); 621 ctl_zero_io(io); 622 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request; 623 io->io_hdr.io_type = CTL_IO_TASK; 624 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid; 625 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port; 626 io->io_hdr.nexus.targ_target.id = 0; 627 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun); 628 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */ 629 630 switch (bhstmr->bhstmr_function & ~0x80) { 631 case BHSTMR_FUNCTION_ABORT_TASK: 632#if 0 633 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK"); 634#endif 635 io->taskio.task_action = CTL_TASK_ABORT_TASK; 636 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag; 637 break; 638 case BHSTMR_FUNCTION_ABORT_TASK_SET: 639#if 0 640 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK_SET"); 641#endif 642 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET; 643 break; 644 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET: 645#if 0 646 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET"); 647#endif 648 io->taskio.task_action = CTL_TASK_LUN_RESET; 649 break; 650 case BHSTMR_FUNCTION_TARGET_WARM_RESET: 651#if 0 652 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET"); 653#endif 654 io->taskio.task_action = CTL_TASK_TARGET_RESET; 655 break; 656 default: 657 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x", 658 bhstmr->bhstmr_function & ~0x80); 659 ctl_free_io(io); 660 661 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 662 if (response == NULL) { 663 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; " 664 "dropping connection"); 665 icl_pdu_free(request); 666 cfiscsi_session_terminate(cs); 667 return; 668 } 669 bhstmr2 = (struct iscsi_bhs_task_management_response *) 670 response->ip_bhs; 671 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE; 672 bhstmr2->bhstmr_flags = 0x80; 673 bhstmr2->bhstmr_response = 674 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED; 675 bhstmr2->bhstmr_initiator_task_tag = 676 bhstmr->bhstmr_initiator_task_tag; 677 icl_pdu_free(request); 678 cfiscsi_pdu_queue(response); 679 return; 680 } 681 682 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 683 error = ctl_queue(io); 684 if (error != CTL_RETVAL_COMPLETE) { 685 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; " 686 "dropping connection", error); 687 ctl_free_io(io); 688 refcount_release(&cs->cs_outstanding_ctl_pdus); 689 icl_pdu_free(request); 690 cfiscsi_session_terminate(cs); 691 } 692} 693 694static bool 695cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw) 696{ 697 struct iscsi_bhs_data_out *bhsdo; 698 struct cfiscsi_session *cs; 699 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 700 size_t copy_len, len, off, buffer_offset; 701 int ctl_sg_count; 702 union ctl_io *io; 703 704 cs = PDU_SESSION(request); 705 706 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 707 ISCSI_BHS_OPCODE_SCSI_DATA_OUT || 708 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 709 ISCSI_BHS_OPCODE_SCSI_COMMAND, 710 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode)); 711 712 /* 713 * We're only using fields common for Data-Out and SCSI Command PDUs. 714 */ 715 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs; 716 717 io = cdw->cdw_ctl_io; 718 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN, 719 ("CTL_FLAG_DATA_IN")); 720 721#if 0 722 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d", 723 request->ip_data_len, io->scsiio.kern_total_len); 724#endif 725 726 if (io->scsiio.kern_sg_entries > 0) { 727 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 728 ctl_sg_count = io->scsiio.kern_sg_entries; 729 } else { 730 ctl_sglist = &ctl_sg_entry; 731 ctl_sglist->addr = io->scsiio.kern_data_ptr; 732 ctl_sglist->len = io->scsiio.kern_data_len; 733 ctl_sg_count = 1; 734 } 735 736 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 737 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) 738 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset); 739 else 740 buffer_offset = 0; 741 len = icl_pdu_data_segment_length(request); 742 743 /* 744 * Make sure the offset, as sent by the initiator, matches the offset 745 * we're supposed to be at in the scatter-gather list. 746 */ 747 if (buffer_offset > 748 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled || 749 buffer_offset + len <= 750 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) { 751 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, " 752 "expected %zd; dropping connection", buffer_offset, 753 (size_t)io->scsiio.kern_rel_offset + 754 (size_t)io->scsiio.ext_data_filled); 755 ctl_set_data_phase_error(&io->scsiio); 756 cfiscsi_session_terminate(cs); 757 return (true); 758 } 759 760 /* 761 * This is the offset within the PDU data segment, as opposed 762 * to buffer_offset, which is the offset within the task (SCSI 763 * command). 764 */ 765 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled - 766 buffer_offset; 767 768 /* 769 * Iterate over the scatter/gather segments, filling them with data 770 * from the PDU data segment. Note that this can get called multiple 771 * times for one SCSI command; the cdw structure holds state for the 772 * scatter/gather list. 773 */ 774 for (;;) { 775 KASSERT(cdw->cdw_sg_index < ctl_sg_count, 776 ("cdw->cdw_sg_index >= ctl_sg_count")); 777 if (cdw->cdw_sg_len == 0) { 778 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr; 779 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len; 780 } 781 KASSERT(off <= len, ("len > off")); 782 copy_len = len - off; 783 if (copy_len > cdw->cdw_sg_len) 784 copy_len = cdw->cdw_sg_len; 785 786 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len); 787 cdw->cdw_sg_addr += copy_len; 788 cdw->cdw_sg_len -= copy_len; 789 off += copy_len; 790 io->scsiio.ext_data_filled += copy_len; 791 792 if (cdw->cdw_sg_len == 0) { 793 /* 794 * End of current segment. 795 */ 796 if (cdw->cdw_sg_index == ctl_sg_count - 1) { 797 /* 798 * Last segment in scatter/gather list. 799 */ 800 break; 801 } 802 cdw->cdw_sg_index++; 803 } 804 805 if (off == len) { 806 /* 807 * End of PDU payload. 808 */ 809 break; 810 } 811 } 812 813 if (len > off) { 814 /* 815 * In case of unsolicited data, it's possible that the buffer 816 * provided by CTL is smaller than negotiated FirstBurstLength. 817 * Just ignore the superfluous data; will ask for them with R2T 818 * on next call to cfiscsi_datamove(). 819 * 820 * This obviously can only happen with SCSI Command PDU. 821 */ 822 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 823 ISCSI_BHS_OPCODE_SCSI_COMMAND) 824 return (true); 825 826 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, " 827 "expected %zd; dropping connection", 828 icl_pdu_data_segment_length(request), off); 829 ctl_set_data_phase_error(&io->scsiio); 830 cfiscsi_session_terminate(cs); 831 return (true); 832 } 833 834 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end && 835 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) { 836 CFISCSI_SESSION_WARN(cs, "got the final packet without " 837 "the F flag; flags = 0x%x; dropping connection", 838 bhsdo->bhsdo_flags); 839 ctl_set_data_phase_error(&io->scsiio); 840 cfiscsi_session_terminate(cs); 841 return (true); 842 } 843 844 if (io->scsiio.ext_data_filled != cdw->cdw_r2t_end && 845 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) { 846 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 847 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) { 848 CFISCSI_SESSION_WARN(cs, "got the final packet, but the " 849 "transmitted size was %zd bytes instead of %d; " 850 "dropping connection", 851 (size_t)io->scsiio.ext_data_filled, 852 cdw->cdw_r2t_end); 853 ctl_set_data_phase_error(&io->scsiio); 854 cfiscsi_session_terminate(cs); 855 return (true); 856 } else { 857 /* 858 * For SCSI Command PDU, this just means we need to 859 * solicit more data by sending R2T. 860 */ 861 return (false); 862 } 863 } 864 865 if (io->scsiio.ext_data_filled == cdw->cdw_r2t_end) { 866#if 0 867 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target " 868 "transfer tag 0x%x", cdw->cdw_target_transfer_tag); 869#endif 870 871 return (true); 872 } 873 874 return (false); 875} 876 877static void 878cfiscsi_pdu_handle_data_out(struct icl_pdu *request) 879{ 880 struct iscsi_bhs_data_out *bhsdo; 881 struct cfiscsi_session *cs; 882 struct cfiscsi_data_wait *cdw = NULL; 883 union ctl_io *io; 884 bool done; 885 886 cs = PDU_SESSION(request); 887 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs; 888 889 CFISCSI_SESSION_LOCK(cs); 890 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) { 891#if 0 892 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for " 893 "ttt 0x%x, itt 0x%x", 894 bhsdo->bhsdo_target_transfer_tag, 895 bhsdo->bhsdo_initiator_task_tag, 896 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag)); 897#endif 898 if (bhsdo->bhsdo_target_transfer_tag == 899 cdw->cdw_target_transfer_tag) 900 break; 901 } 902 CFISCSI_SESSION_UNLOCK(cs); 903 if (cdw == NULL) { 904 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag " 905 "0x%x, not found; dropping connection", 906 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag); 907 icl_pdu_free(request); 908 cfiscsi_session_terminate(cs); 909 return; 910 } 911 912 if (cdw->cdw_datasn != ntohl(bhsdo->bhsdo_datasn)) { 913 CFISCSI_SESSION_WARN(cs, "received Data-Out PDU with " 914 "DataSN %u, while expected %u; dropping connection", 915 ntohl(bhsdo->bhsdo_datasn), cdw->cdw_datasn); 916 icl_pdu_free(request); 917 cfiscsi_session_terminate(cs); 918 return; 919 } 920 cdw->cdw_datasn++; 921 922 io = cdw->cdw_ctl_io; 923 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN, 924 ("CTL_FLAG_DATA_IN")); 925 926 done = cfiscsi_handle_data_segment(request, cdw); 927 if (done) { 928 CFISCSI_SESSION_LOCK(cs); 929 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next); 930 CFISCSI_SESSION_UNLOCK(cs); 931 done = (io->scsiio.ext_data_filled != cdw->cdw_r2t_end || 932 io->scsiio.ext_data_filled == io->scsiio.kern_data_len); 933 uma_zfree(cfiscsi_data_wait_zone, cdw); 934 if (done) 935 io->scsiio.be_move_done(io); 936 else 937 cfiscsi_datamove_out(io); 938 } 939 940 icl_pdu_free(request); 941} 942 943static void 944cfiscsi_pdu_handle_logout_request(struct icl_pdu *request) 945{ 946 struct iscsi_bhs_logout_request *bhslr; 947 struct iscsi_bhs_logout_response *bhslr2; 948 struct icl_pdu *response; 949 struct cfiscsi_session *cs; 950 951 cs = PDU_SESSION(request); 952 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs; 953 switch (bhslr->bhslr_reason & 0x7f) { 954 case BHSLR_REASON_CLOSE_SESSION: 955 case BHSLR_REASON_CLOSE_CONNECTION: 956 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 957 if (response == NULL) { 958 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory"); 959 icl_pdu_free(request); 960 cfiscsi_session_terminate(cs); 961 return; 962 } 963 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs; 964 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE; 965 bhslr2->bhslr_flags = 0x80; 966 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY; 967 bhslr2->bhslr_initiator_task_tag = 968 bhslr->bhslr_initiator_task_tag; 969 icl_pdu_free(request); 970 cfiscsi_pdu_queue(response); 971 cfiscsi_session_terminate(cs); 972 break; 973 case BHSLR_REASON_REMOVE_FOR_RECOVERY: 974 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 975 if (response == NULL) { 976 CFISCSI_SESSION_WARN(cs, 977 "failed to allocate memory; dropping connection"); 978 icl_pdu_free(request); 979 cfiscsi_session_terminate(cs); 980 return; 981 } 982 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs; 983 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE; 984 bhslr2->bhslr_flags = 0x80; 985 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED; 986 bhslr2->bhslr_initiator_task_tag = 987 bhslr->bhslr_initiator_task_tag; 988 icl_pdu_free(request); 989 cfiscsi_pdu_queue(response); 990 break; 991 default: 992 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection", 993 bhslr->bhslr_reason); 994 icl_pdu_free(request); 995 cfiscsi_session_terminate(cs); 996 break; 997 } 998} 999 1000static void 1001cfiscsi_callout(void *context) 1002{ 1003 struct icl_pdu *cp; 1004 struct iscsi_bhs_nop_in *bhsni; 1005 struct cfiscsi_session *cs; 1006 1007 cs = context; 1008 1009 if (cs->cs_terminating) 1010 return; 1011 1012 callout_schedule(&cs->cs_callout, 1 * hz); 1013 1014 atomic_add_int(&cs->cs_timeout, 1); 1015 1016#ifdef ICL_KERNEL_PROXY 1017 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) { 1018 if (login_timeout > 0 && cs->cs_timeout > login_timeout) { 1019 CFISCSI_SESSION_WARN(cs, "login timed out after " 1020 "%d seconds; dropping connection", cs->cs_timeout); 1021 cfiscsi_session_terminate(cs); 1022 } 1023 return; 1024 } 1025#endif 1026 1027 if (ping_timeout <= 0) { 1028 /* 1029 * Pings are disabled. Don't send NOP-In in this case; 1030 * user might have disabled pings to work around problems 1031 * with certain initiators that can't properly handle 1032 * NOP-In, such as iPXE. Reset the timeout, to avoid 1033 * triggering reconnection, should the user decide to 1034 * reenable them. 1035 */ 1036 cs->cs_timeout = 0; 1037 return; 1038 } 1039 1040 if (cs->cs_timeout >= ping_timeout) { 1041 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; " 1042 "dropping connection", ping_timeout); 1043 cfiscsi_session_terminate(cs); 1044 return; 1045 } 1046 1047 /* 1048 * If the ping was reset less than one second ago - which means 1049 * that we've received some PDU during the last second - assume 1050 * the traffic flows correctly and don't bother sending a NOP-Out. 1051 * 1052 * (It's 2 - one for one second, and one for incrementing is_timeout 1053 * earlier in this routine.) 1054 */ 1055 if (cs->cs_timeout < 2) 1056 return; 1057 1058 cp = icl_pdu_new(cs->cs_conn, M_NOWAIT); 1059 if (cp == NULL) { 1060 CFISCSI_SESSION_WARN(cs, "failed to allocate memory"); 1061 return; 1062 } 1063 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs; 1064 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN; 1065 bhsni->bhsni_flags = 0x80; 1066 bhsni->bhsni_initiator_task_tag = 0xffffffff; 1067 1068 cfiscsi_pdu_queue(cp); 1069} 1070 1071static void 1072cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs) 1073{ 1074 struct cfiscsi_data_wait *cdw; 1075 union ctl_io *io; 1076 int error, last, wait; 1077 1078 if (cs->cs_target == NULL) 1079 return; /* No target yet, so nothing to do. */ 1080 io = ctl_alloc_io(cs->cs_target->ct_port.ctl_pool_ref); 1081 ctl_zero_io(io); 1082 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = cs; 1083 io->io_hdr.io_type = CTL_IO_TASK; 1084 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid; 1085 io->io_hdr.nexus.targ_port = cs->cs_target->ct_port.targ_port; 1086 io->io_hdr.nexus.targ_target.id = 0; 1087 io->io_hdr.nexus.targ_lun = 0; 1088 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */ 1089 io->taskio.task_action = CTL_TASK_I_T_NEXUS_RESET; 1090 wait = cs->cs_outstanding_ctl_pdus; 1091 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 1092 error = ctl_queue(io); 1093 if (error != CTL_RETVAL_COMPLETE) { 1094 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error); 1095 refcount_release(&cs->cs_outstanding_ctl_pdus); 1096 ctl_free_io(io); 1097 } 1098 1099 CFISCSI_SESSION_LOCK(cs); 1100 while ((cdw = TAILQ_FIRST(&cs->cs_waiting_for_data_out)) != NULL) { 1101 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next); 1102 CFISCSI_SESSION_UNLOCK(cs); 1103 /* 1104 * Set nonzero port status; this prevents backends from 1105 * assuming that the data transfer actually succeeded 1106 * and writing uninitialized data to disk. 1107 */ 1108 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42; 1109 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io); 1110 uma_zfree(cfiscsi_data_wait_zone, cdw); 1111 CFISCSI_SESSION_LOCK(cs); 1112 } 1113 CFISCSI_SESSION_UNLOCK(cs); 1114 1115 /* 1116 * Wait for CTL to terminate all the tasks. 1117 */ 1118 if (wait > 0) 1119 CFISCSI_SESSION_WARN(cs, 1120 "waiting for CTL to terminate %d tasks", wait); 1121 for (;;) { 1122 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 1123 last = refcount_release(&cs->cs_outstanding_ctl_pdus); 1124 if (last != 0) 1125 break; 1126 tsleep(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus), 1127 0, "cfiscsi_terminate", hz / 100); 1128 } 1129 if (wait > 0) 1130 CFISCSI_SESSION_WARN(cs, "tasks terminated"); 1131} 1132 1133static void 1134cfiscsi_maintenance_thread(void *arg) 1135{ 1136 struct cfiscsi_session *cs; 1137 1138 cs = arg; 1139 1140 for (;;) { 1141 CFISCSI_SESSION_LOCK(cs); 1142 if (cs->cs_terminating == false) 1143 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock); 1144 CFISCSI_SESSION_UNLOCK(cs); 1145 1146 if (cs->cs_terminating) { 1147 1148 /* 1149 * We used to wait up to 30 seconds to deliver queued 1150 * PDUs to the initiator. We also tried hard to deliver 1151 * SCSI Responses for the aborted PDUs. We don't do 1152 * that anymore. We might need to revisit that. 1153 */ 1154 callout_drain(&cs->cs_callout); 1155 icl_conn_close(cs->cs_conn); 1156 1157 /* 1158 * At this point ICL receive thread is no longer 1159 * running; no new tasks can be queued. 1160 */ 1161 cfiscsi_session_terminate_tasks(cs); 1162 cfiscsi_session_delete(cs); 1163 kthread_exit(); 1164 return; 1165 } 1166 CFISCSI_SESSION_DEBUG(cs, "nothing to do"); 1167 } 1168} 1169 1170static void 1171cfiscsi_session_terminate(struct cfiscsi_session *cs) 1172{ 1173 1174 if (cs->cs_terminating) 1175 return; 1176 cs->cs_terminating = true; 1177 cv_signal(&cs->cs_maintenance_cv); 1178#ifdef ICL_KERNEL_PROXY 1179 cv_signal(&cs->cs_login_cv); 1180#endif 1181} 1182 1183static int 1184cfiscsi_session_register_initiator(struct cfiscsi_session *cs) 1185{ 1186 struct cfiscsi_target *ct; 1187 char *name; 1188 int i; 1189 1190 KASSERT(cs->cs_ctl_initid == -1, ("already registered")); 1191 1192 ct = cs->cs_target; 1193 name = strdup(cs->cs_initiator_id, M_CTL); 1194 i = ctl_add_initiator(&ct->ct_port, -1, 0, name); 1195 if (i < 0) { 1196 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", 1197 i); 1198 cs->cs_ctl_initid = -1; 1199 return (1); 1200 } 1201 cs->cs_ctl_initid = i; 1202#if 0 1203 CFISCSI_SESSION_DEBUG(cs, "added initiator id %d", i); 1204#endif 1205 1206 return (0); 1207} 1208 1209static void 1210cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs) 1211{ 1212 int error; 1213 1214 if (cs->cs_ctl_initid == -1) 1215 return; 1216 1217 error = ctl_remove_initiator(&cs->cs_target->ct_port, cs->cs_ctl_initid); 1218 if (error != 0) { 1219 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d", 1220 error); 1221 } 1222 cs->cs_ctl_initid = -1; 1223} 1224 1225static struct cfiscsi_session * 1226cfiscsi_session_new(struct cfiscsi_softc *softc) 1227{ 1228 struct cfiscsi_session *cs; 1229 int error; 1230 1231 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO); 1232 if (cs == NULL) { 1233 CFISCSI_WARN("malloc failed"); 1234 return (NULL); 1235 } 1236 cs->cs_ctl_initid = -1; 1237 1238 refcount_init(&cs->cs_outstanding_ctl_pdus, 0); 1239 TAILQ_INIT(&cs->cs_waiting_for_data_out); 1240 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF); 1241 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt"); 1242#ifdef ICL_KERNEL_PROXY 1243 cv_init(&cs->cs_login_cv, "cfiscsi_login"); 1244#endif 1245 1246 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock); 1247 cs->cs_conn->ic_receive = cfiscsi_receive_callback; 1248 cs->cs_conn->ic_error = cfiscsi_error_callback; 1249 cs->cs_conn->ic_prv0 = cs; 1250 1251 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt"); 1252 if (error != 0) { 1253 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error); 1254 free(cs, M_CFISCSI); 1255 return (NULL); 1256 } 1257 1258 mtx_lock(&softc->lock); 1259 cs->cs_id = ++softc->last_session_id; 1260 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next); 1261 mtx_unlock(&softc->lock); 1262 1263 /* 1264 * Start pinging the initiator. 1265 */ 1266 callout_init(&cs->cs_callout, 1); 1267 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs); 1268 1269 return (cs); 1270} 1271 1272static void 1273cfiscsi_session_delete(struct cfiscsi_session *cs) 1274{ 1275 struct cfiscsi_softc *softc; 1276 1277 softc = &cfiscsi_softc; 1278 1279 KASSERT(cs->cs_outstanding_ctl_pdus == 0, 1280 ("destroying session with outstanding CTL pdus")); 1281 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out), 1282 ("destroying session with non-empty queue")); 1283 1284 cfiscsi_session_unregister_initiator(cs); 1285 if (cs->cs_target != NULL) 1286 cfiscsi_target_release(cs->cs_target); 1287 icl_conn_close(cs->cs_conn); 1288 icl_conn_free(cs->cs_conn); 1289 1290 mtx_lock(&softc->lock); 1291 TAILQ_REMOVE(&softc->sessions, cs, cs_next); 1292 cv_signal(&softc->sessions_cv); 1293 mtx_unlock(&softc->lock); 1294 1295 free(cs, M_CFISCSI); 1296} 1297 1298int 1299cfiscsi_init(void) 1300{ 1301 struct cfiscsi_softc *softc; 1302 int retval; 1303 1304 softc = &cfiscsi_softc; 1305 retval = 0; 1306 bzero(softc, sizeof(*softc)); 1307 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF); 1308 1309 cv_init(&softc->sessions_cv, "cfiscsi_sessions"); 1310#ifdef ICL_KERNEL_PROXY 1311 cv_init(&softc->accept_cv, "cfiscsi_accept"); 1312#endif 1313 TAILQ_INIT(&softc->sessions); 1314 TAILQ_INIT(&softc->targets); 1315 1316 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait", 1317 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL, 1318 UMA_ALIGN_PTR, 0); 1319 1320 return (0); 1321} 1322 1323#ifdef ICL_KERNEL_PROXY 1324static void 1325cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id) 1326{ 1327 struct cfiscsi_session *cs; 1328 1329 cs = cfiscsi_session_new(&cfiscsi_softc); 1330 if (cs == NULL) { 1331 CFISCSI_WARN("failed to create session"); 1332 return; 1333 } 1334 1335 icl_conn_handoff_sock(cs->cs_conn, so); 1336 cs->cs_initiator_sa = sa; 1337 cs->cs_portal_id = portal_id; 1338 cs->cs_waiting_for_ctld = true; 1339 cv_signal(&cfiscsi_softc.accept_cv); 1340} 1341#endif 1342 1343static void 1344cfiscsi_online(void *arg) 1345{ 1346 struct cfiscsi_softc *softc; 1347 struct cfiscsi_target *ct; 1348 int online; 1349 1350 ct = (struct cfiscsi_target *)arg; 1351 softc = ct->ct_softc; 1352 1353 mtx_lock(&softc->lock); 1354 if (ct->ct_online) { 1355 mtx_unlock(&softc->lock); 1356 return; 1357 } 1358 ct->ct_online = 1; 1359 online = softc->online++; 1360 mtx_unlock(&softc->lock); 1361 if (online > 0) 1362 return; 1363 1364#ifdef ICL_KERNEL_PROXY 1365 if (softc->listener != NULL) 1366 icl_listen_free(softc->listener); 1367 softc->listener = icl_listen_new(cfiscsi_accept); 1368#endif 1369} 1370 1371static void 1372cfiscsi_offline(void *arg) 1373{ 1374 struct cfiscsi_softc *softc; 1375 struct cfiscsi_target *ct; 1376 struct cfiscsi_session *cs; 1377 int online; 1378 1379 ct = (struct cfiscsi_target *)arg; 1380 softc = ct->ct_softc; 1381 1382 mtx_lock(&softc->lock); 1383 if (!ct->ct_online) { 1384 mtx_unlock(&softc->lock); 1385 return; 1386 } 1387 ct->ct_online = 0; 1388 online = --softc->online; 1389 1390 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1391 if (cs->cs_target == ct) 1392 cfiscsi_session_terminate(cs); 1393 } 1394 do { 1395 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1396 if (cs->cs_target == ct) 1397 break; 1398 } 1399 if (cs != NULL) 1400 cv_wait(&softc->sessions_cv, &softc->lock); 1401 } while (cs != NULL && ct->ct_online == 0); 1402 mtx_unlock(&softc->lock); 1403 if (online > 0) 1404 return; 1405 1406#ifdef ICL_KERNEL_PROXY 1407 icl_listen_free(softc->listener); 1408 softc->listener = NULL; 1409#endif 1410} 1411 1412static int 1413cfiscsi_info(void *arg, struct sbuf *sb) 1414{ 1415 struct cfiscsi_target *ct = (struct cfiscsi_target *)arg; 1416 int retval; 1417 1418 retval = sbuf_printf(sb, "\t<cfiscsi_state>%d</cfiscsi_state>\n", 1419 ct->ct_state); 1420 return (retval); 1421} 1422 1423static void 1424cfiscsi_ioctl_handoff(struct ctl_iscsi *ci) 1425{ 1426 struct cfiscsi_softc *softc; 1427 struct cfiscsi_session *cs, *cs2; 1428 struct cfiscsi_target *ct; 1429 struct ctl_iscsi_handoff_params *cihp; 1430 int error; 1431 1432 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data); 1433 softc = &cfiscsi_softc; 1434 1435 CFISCSI_DEBUG("new connection from %s (%s) to %s", 1436 cihp->initiator_name, cihp->initiator_addr, 1437 cihp->target_name); 1438 1439 ct = cfiscsi_target_find(softc, cihp->target_name, 1440 cihp->portal_group_tag); 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_cmdsn = cihp->cmdsn; 1491 cs->cs_statsn = cihp->statsn; 1492 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length; 1493 cs->cs_max_burst_length = cihp->max_burst_length; 1494 cs->cs_immediate_data = !!cihp->immediate_data; 1495 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C) 1496 cs->cs_conn->ic_header_crc32c = true; 1497 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C) 1498 cs->cs_conn->ic_data_crc32c = true; 1499 1500 strlcpy(cs->cs_initiator_name, 1501 cihp->initiator_name, sizeof(cs->cs_initiator_name)); 1502 strlcpy(cs->cs_initiator_addr, 1503 cihp->initiator_addr, sizeof(cs->cs_initiator_addr)); 1504 strlcpy(cs->cs_initiator_alias, 1505 cihp->initiator_alias, sizeof(cs->cs_initiator_alias)); 1506 memcpy(cs->cs_initiator_isid, 1507 cihp->initiator_isid, sizeof(cs->cs_initiator_isid)); 1508 snprintf(cs->cs_initiator_id, sizeof(cs->cs_initiator_id), 1509 "%s,i,0x%02x%02x%02x%02x%02x%02x", cs->cs_initiator_name, 1510 cihp->initiator_isid[0], cihp->initiator_isid[1], 1511 cihp->initiator_isid[2], cihp->initiator_isid[3], 1512 cihp->initiator_isid[4], cihp->initiator_isid[5]); 1513 1514 mtx_lock(&softc->lock); 1515 if (ct->ct_online == 0) { 1516 mtx_unlock(&softc->lock); 1517 cfiscsi_session_terminate(cs); 1518 cfiscsi_target_release(ct); 1519 ci->status = CTL_ISCSI_ERROR; 1520 snprintf(ci->error_str, sizeof(ci->error_str), 1521 "%s: port offline", __func__); 1522 return; 1523 } 1524 cs->cs_target = ct; 1525 mtx_unlock(&softc->lock); 1526 1527 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 1528restart: 1529 if (!cs->cs_terminating) { 1530 mtx_lock(&softc->lock); 1531 TAILQ_FOREACH(cs2, &softc->sessions, cs_next) { 1532 if (cs2 != cs && cs2->cs_tasks_aborted == false && 1533 cs->cs_target == cs2->cs_target && 1534 strcmp(cs->cs_initiator_id, cs2->cs_initiator_id) == 0) { 1535 cfiscsi_session_terminate(cs2); 1536 mtx_unlock(&softc->lock); 1537 pause("cfiscsi_reinstate", 1); 1538 goto restart; 1539 } 1540 } 1541 mtx_unlock(&softc->lock); 1542 } 1543 1544 /* 1545 * Register initiator with CTL. 1546 */ 1547 cfiscsi_session_register_initiator(cs); 1548 1549#ifdef ICL_KERNEL_PROXY 1550 if (cihp->socket > 0) { 1551#endif 1552 error = icl_conn_handoff(cs->cs_conn, cihp->socket); 1553 if (error != 0) { 1554 cfiscsi_session_terminate(cs); 1555 refcount_release(&cs->cs_outstanding_ctl_pdus); 1556 ci->status = CTL_ISCSI_ERROR; 1557 snprintf(ci->error_str, sizeof(ci->error_str), 1558 "%s: icl_conn_handoff failed with error %d", 1559 __func__, error); 1560 return; 1561 } 1562#ifdef ICL_KERNEL_PROXY 1563 } 1564#endif 1565 1566#ifdef ICL_KERNEL_PROXY 1567 cs->cs_login_phase = false; 1568 1569 /* 1570 * First PDU of the Full Feature phase has likely already arrived. 1571 * We have to pick it up and execute properly. 1572 */ 1573 if (cs->cs_login_pdu != NULL) { 1574 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU"); 1575 cfiscsi_pdu_handle(cs->cs_login_pdu); 1576 cs->cs_login_pdu = NULL; 1577 } 1578#endif 1579 1580 refcount_release(&cs->cs_outstanding_ctl_pdus); 1581 ci->status = CTL_ISCSI_OK; 1582} 1583 1584static void 1585cfiscsi_ioctl_list(struct ctl_iscsi *ci) 1586{ 1587 struct ctl_iscsi_list_params *cilp; 1588 struct cfiscsi_session *cs; 1589 struct cfiscsi_softc *softc; 1590 struct sbuf *sb; 1591 int error; 1592 1593 cilp = (struct ctl_iscsi_list_params *)&(ci->data); 1594 softc = &cfiscsi_softc; 1595 1596 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN); 1597 if (sb == NULL) { 1598 ci->status = CTL_ISCSI_ERROR; 1599 snprintf(ci->error_str, sizeof(ci->error_str), 1600 "Unable to allocate %d bytes for iSCSI session list", 1601 cilp->alloc_len); 1602 return; 1603 } 1604 1605 sbuf_printf(sb, "<ctlislist>\n"); 1606 mtx_lock(&softc->lock); 1607 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1608#ifdef ICL_KERNEL_PROXY 1609 if (cs->cs_target == NULL) 1610 continue; 1611#endif 1612 error = sbuf_printf(sb, "<connection id=\"%d\">" 1613 "<initiator>%s</initiator>" 1614 "<initiator_addr>%s</initiator_addr>" 1615 "<initiator_alias>%s</initiator_alias>" 1616 "<target>%s</target>" 1617 "<target_alias>%s</target_alias>" 1618 "<target_portal_group_tag>%u</target_portal_group_tag>" 1619 "<header_digest>%s</header_digest>" 1620 "<data_digest>%s</data_digest>" 1621 "<max_data_segment_length>%zd</max_data_segment_length>" 1622 "<immediate_data>%d</immediate_data>" 1623 "<iser>%d</iser>" 1624 "</connection>\n", 1625 cs->cs_id, 1626 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias, 1627 cs->cs_target->ct_name, cs->cs_target->ct_alias, 1628 cs->cs_target->ct_tag, 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_logout(struct ctl_iscsi *ci) 1656{ 1657 struct icl_pdu *response; 1658 struct iscsi_bhs_asynchronous_message *bhsam; 1659 struct ctl_iscsi_logout_params *cilp; 1660 struct cfiscsi_session *cs; 1661 struct cfiscsi_softc *softc; 1662 int found = 0; 1663 1664 cilp = (struct ctl_iscsi_logout_params *)&(ci->data); 1665 softc = &cfiscsi_softc; 1666 1667 mtx_lock(&softc->lock); 1668 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1669 if (cilp->all == 0 && cs->cs_id != cilp->connection_id && 1670 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 && 1671 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0) 1672 continue; 1673 1674 response = icl_pdu_new(cs->cs_conn, M_NOWAIT); 1675 if (response == NULL) { 1676 ci->status = CTL_ISCSI_ERROR; 1677 snprintf(ci->error_str, sizeof(ci->error_str), 1678 "Unable to allocate memory"); 1679 mtx_unlock(&softc->lock); 1680 return; 1681 } 1682 bhsam = 1683 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs; 1684 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE; 1685 bhsam->bhsam_flags = 0x80; 1686 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT; 1687 bhsam->bhsam_parameter3 = htons(10); 1688 cfiscsi_pdu_queue(response); 1689 found++; 1690 } 1691 mtx_unlock(&softc->lock); 1692 1693 if (found == 0) { 1694 ci->status = CTL_ISCSI_SESSION_NOT_FOUND; 1695 snprintf(ci->error_str, sizeof(ci->error_str), 1696 "No matching connections found"); 1697 return; 1698 } 1699 1700 ci->status = CTL_ISCSI_OK; 1701} 1702 1703static void 1704cfiscsi_ioctl_terminate(struct ctl_iscsi *ci) 1705{ 1706 struct icl_pdu *response; 1707 struct iscsi_bhs_asynchronous_message *bhsam; 1708 struct ctl_iscsi_terminate_params *citp; 1709 struct cfiscsi_session *cs; 1710 struct cfiscsi_softc *softc; 1711 int found = 0; 1712 1713 citp = (struct ctl_iscsi_terminate_params *)&(ci->data); 1714 softc = &cfiscsi_softc; 1715 1716 mtx_lock(&softc->lock); 1717 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1718 if (citp->all == 0 && cs->cs_id != citp->connection_id && 1719 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 && 1720 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0) 1721 continue; 1722 1723 response = icl_pdu_new(cs->cs_conn, M_NOWAIT); 1724 if (response == NULL) { 1725 /* 1726 * Oh well. Just terminate the connection. 1727 */ 1728 } else { 1729 bhsam = (struct iscsi_bhs_asynchronous_message *) 1730 response->ip_bhs; 1731 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE; 1732 bhsam->bhsam_flags = 0x80; 1733 bhsam->bhsam_0xffffffff = 0xffffffff; 1734 bhsam->bhsam_async_event = 1735 BHSAM_EVENT_TARGET_TERMINATES_SESSION; 1736 cfiscsi_pdu_queue(response); 1737 } 1738 cfiscsi_session_terminate(cs); 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, *tags; 1987 struct scsi_vpd_id_descriptor *desc; 1988 ctl_options_t opts; 1989 int retval, len, idlen; 1990 uint16_t tag; 1991 1992 ctl_init_opts(&opts, req->num_args, req->kern_args); 1993 target = ctl_get_opt(&opts, "cfiscsi_target"); 1994 alias = ctl_get_opt(&opts, "cfiscsi_target_alias"); 1995 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag"); 1996 if (target == NULL || tags == NULL) { 1997 req->status = CTL_LUN_ERROR; 1998 snprintf(req->error_str, sizeof(req->error_str), 1999 "Missing required argument"); 2000 ctl_free_opts(&opts); 2001 return; 2002 } 2003 tag = strtol(tags, (char **)NULL, 10); 2004 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag); 2005 if (ct == NULL) { 2006 req->status = CTL_LUN_ERROR; 2007 snprintf(req->error_str, sizeof(req->error_str), 2008 "failed to create target \"%s\"", target); 2009 ctl_free_opts(&opts); 2010 return; 2011 } 2012 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) { 2013 req->status = CTL_LUN_ERROR; 2014 snprintf(req->error_str, sizeof(req->error_str), 2015 "target \"%s\" already exists", target); 2016 cfiscsi_target_release(ct); 2017 ctl_free_opts(&opts); 2018 return; 2019 } 2020 port = &ct->ct_port; 2021 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING) 2022 goto done; 2023 2024 port->frontend = &cfiscsi_frontend; 2025 port->port_type = CTL_PORT_ISCSI; 2026 /* XXX KDM what should the real number be here? */ 2027 port->num_requested_ctl_io = 4096; 2028 port->port_name = "iscsi"; 2029 port->physical_port = tag; 2030 port->virtual_port = ct->ct_target_id; 2031 port->port_online = cfiscsi_online; 2032 port->port_offline = cfiscsi_offline; 2033 port->port_info = cfiscsi_info; 2034 port->onoff_arg = ct; 2035 port->lun_enable = cfiscsi_lun_enable; 2036 port->lun_disable = cfiscsi_lun_disable; 2037 port->targ_lun_arg = ct; 2038 port->fe_datamove = cfiscsi_datamove; 2039 port->fe_done = cfiscsi_done; 2040 2041 /* XXX KDM what should we report here? */ 2042 /* XXX These should probably be fetched from CTL. */ 2043 port->max_targets = 1; 2044 port->max_target_id = 15; 2045 2046 port->options = opts; 2047 STAILQ_INIT(&opts); 2048 2049 /* Generate Port ID. */ 2050 idlen = strlen(target) + strlen(",t,0x0001") + 1; 2051 idlen = roundup2(idlen, 4); 2052 len = sizeof(struct scsi_vpd_device_id) + idlen; 2053 port->port_devid = malloc(sizeof(struct ctl_devid) + len, 2054 M_CTL, M_WAITOK | M_ZERO); 2055 port->port_devid->len = len; 2056 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data; 2057 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2058 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2059 SVPD_ID_TYPE_SCSI_NAME; 2060 desc->length = idlen; 2061 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag); 2062 2063 /* Generate Target ID. */ 2064 idlen = strlen(target) + 1; 2065 idlen = roundup2(idlen, 4); 2066 len = sizeof(struct scsi_vpd_device_id) + idlen; 2067 port->target_devid = malloc(sizeof(struct ctl_devid) + len, 2068 M_CTL, M_WAITOK | M_ZERO); 2069 port->target_devid->len = len; 2070 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data; 2071 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2072 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET | 2073 SVPD_ID_TYPE_SCSI_NAME; 2074 desc->length = idlen; 2075 strlcpy(desc->identifier, target, idlen); 2076 2077 retval = ctl_port_register(port); 2078 if (retval != 0) { 2079 ctl_free_opts(&port->options); 2080 cfiscsi_target_release(ct); 2081 free(port->port_devid, M_CFISCSI); 2082 free(port->target_devid, M_CFISCSI); 2083 req->status = CTL_LUN_ERROR; 2084 snprintf(req->error_str, sizeof(req->error_str), 2085 "ctl_port_register() failed with error %d", retval); 2086 return; 2087 } 2088done: 2089 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE; 2090 req->status = CTL_LUN_OK; 2091 memcpy(req->kern_args[0].kvalue, &port->targ_port, 2092 sizeof(port->targ_port)); //XXX 2093} 2094 2095static void 2096cfiscsi_ioctl_port_remove(struct ctl_req *req) 2097{ 2098 struct cfiscsi_target *ct; 2099 const char *target, *tags; 2100 ctl_options_t opts; 2101 uint16_t tag; 2102 2103 ctl_init_opts(&opts, req->num_args, req->kern_args); 2104 target = ctl_get_opt(&opts, "cfiscsi_target"); 2105 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag"); 2106 if (target == NULL || tags == NULL) { 2107 ctl_free_opts(&opts); 2108 req->status = CTL_LUN_ERROR; 2109 snprintf(req->error_str, sizeof(req->error_str), 2110 "Missing required argument"); 2111 return; 2112 } 2113 tag = strtol(tags, (char **)NULL, 10); 2114 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag); 2115 if (ct == NULL) { 2116 ctl_free_opts(&opts); 2117 req->status = CTL_LUN_ERROR; 2118 snprintf(req->error_str, sizeof(req->error_str), 2119 "can't find target \"%s\"", target); 2120 return; 2121 } 2122 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) { 2123 ctl_free_opts(&opts); 2124 req->status = CTL_LUN_ERROR; 2125 snprintf(req->error_str, sizeof(req->error_str), 2126 "target \"%s\" is already dying", target); 2127 return; 2128 } 2129 ctl_free_opts(&opts); 2130 2131 ct->ct_state = CFISCSI_TARGET_STATE_DYING; 2132 ctl_port_offline(&ct->ct_port); 2133 cfiscsi_target_release(ct); 2134 cfiscsi_target_release(ct); 2135 req->status = CTL_LUN_OK; 2136} 2137 2138static int 2139cfiscsi_ioctl(struct cdev *dev, 2140 u_long cmd, caddr_t addr, int flag, struct thread *td) 2141{ 2142 struct ctl_iscsi *ci; 2143 struct ctl_req *req; 2144 2145 if (cmd == CTL_PORT_REQ) { 2146 req = (struct ctl_req *)addr; 2147 switch (req->reqtype) { 2148 case CTL_REQ_CREATE: 2149 cfiscsi_ioctl_port_create(req); 2150 break; 2151 case CTL_REQ_REMOVE: 2152 cfiscsi_ioctl_port_remove(req); 2153 break; 2154 default: 2155 req->status = CTL_LUN_ERROR; 2156 snprintf(req->error_str, sizeof(req->error_str), 2157 "Unsupported request type %d", req->reqtype); 2158 } 2159 return (0); 2160 } 2161 2162 if (cmd != CTL_ISCSI) 2163 return (ENOTTY); 2164 2165 ci = (struct ctl_iscsi *)addr; 2166 switch (ci->type) { 2167 case CTL_ISCSI_HANDOFF: 2168 cfiscsi_ioctl_handoff(ci); 2169 break; 2170 case CTL_ISCSI_LIST: 2171 cfiscsi_ioctl_list(ci); 2172 break; 2173 case CTL_ISCSI_LOGOUT: 2174 cfiscsi_ioctl_logout(ci); 2175 break; 2176 case CTL_ISCSI_TERMINATE: 2177 cfiscsi_ioctl_terminate(ci); 2178 break; 2179#ifdef ICL_KERNEL_PROXY 2180 case CTL_ISCSI_LISTEN: 2181 cfiscsi_ioctl_listen(ci); 2182 break; 2183 case CTL_ISCSI_ACCEPT: 2184 cfiscsi_ioctl_accept(ci); 2185 break; 2186 case CTL_ISCSI_SEND: 2187 cfiscsi_ioctl_send(ci); 2188 break; 2189 case CTL_ISCSI_RECEIVE: 2190 cfiscsi_ioctl_receive(ci); 2191 break; 2192#else 2193 case CTL_ISCSI_LISTEN: 2194 case CTL_ISCSI_ACCEPT: 2195 case CTL_ISCSI_SEND: 2196 case CTL_ISCSI_RECEIVE: 2197 ci->status = CTL_ISCSI_ERROR; 2198 snprintf(ci->error_str, sizeof(ci->error_str), 2199 "%s: CTL compiled without ICL_KERNEL_PROXY", 2200 __func__); 2201 break; 2202#endif /* !ICL_KERNEL_PROXY */ 2203 default: 2204 ci->status = CTL_ISCSI_ERROR; 2205 snprintf(ci->error_str, sizeof(ci->error_str), 2206 "%s: invalid iSCSI request type %d", __func__, ci->type); 2207 break; 2208 } 2209 2210 return (0); 2211} 2212 2213static void 2214cfiscsi_target_hold(struct cfiscsi_target *ct) 2215{ 2216 2217 refcount_acquire(&ct->ct_refcount); 2218} 2219 2220static void 2221cfiscsi_target_release(struct cfiscsi_target *ct) 2222{ 2223 struct cfiscsi_softc *softc; 2224 2225 softc = ct->ct_softc; 2226 mtx_lock(&softc->lock); 2227 if (refcount_release(&ct->ct_refcount)) { 2228 TAILQ_REMOVE(&softc->targets, ct, ct_next); 2229 mtx_unlock(&softc->lock); 2230 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) { 2231 ct->ct_state = CFISCSI_TARGET_STATE_INVALID; 2232 if (ctl_port_deregister(&ct->ct_port) != 0) 2233 printf("%s: ctl_port_deregister() failed\n", 2234 __func__); 2235 } 2236 free(ct, M_CFISCSI); 2237 2238 return; 2239 } 2240 mtx_unlock(&softc->lock); 2241} 2242 2243static struct cfiscsi_target * 2244cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag) 2245{ 2246 struct cfiscsi_target *ct; 2247 2248 mtx_lock(&softc->lock); 2249 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2250 if (ct->ct_tag != tag || 2251 strcmp(name, ct->ct_name) != 0 || 2252 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) 2253 continue; 2254 cfiscsi_target_hold(ct); 2255 mtx_unlock(&softc->lock); 2256 return (ct); 2257 } 2258 mtx_unlock(&softc->lock); 2259 2260 return (NULL); 2261} 2262 2263static struct cfiscsi_target * 2264cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name, 2265 const char *alias, uint16_t tag) 2266{ 2267 struct cfiscsi_target *ct, *newct; 2268 2269 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN) 2270 return (NULL); 2271 2272 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO); 2273 2274 mtx_lock(&softc->lock); 2275 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2276 if (ct->ct_tag != tag || 2277 strcmp(name, ct->ct_name) != 0 || 2278 ct->ct_state == CFISCSI_TARGET_STATE_INVALID) 2279 continue; 2280 cfiscsi_target_hold(ct); 2281 mtx_unlock(&softc->lock); 2282 free(newct, M_CFISCSI); 2283 return (ct); 2284 } 2285 2286 strlcpy(newct->ct_name, name, sizeof(newct->ct_name)); 2287 if (alias != NULL) 2288 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias)); 2289 newct->ct_tag = tag; 2290 refcount_init(&newct->ct_refcount, 1); 2291 newct->ct_softc = softc; 2292 if (TAILQ_EMPTY(&softc->targets)) 2293 softc->last_target_id = 0; 2294 newct->ct_target_id = ++softc->last_target_id; 2295 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next); 2296 mtx_unlock(&softc->lock); 2297 2298 return (newct); 2299} 2300 2301static int 2302cfiscsi_lun_enable(void *arg, int lun_id) 2303{ 2304 2305 return (0); 2306} 2307 2308static int 2309cfiscsi_lun_disable(void *arg, int lun_id) 2310{ 2311 2312 return (0); 2313} 2314 2315static void 2316cfiscsi_datamove_in(union ctl_io *io) 2317{ 2318 struct cfiscsi_session *cs; 2319 struct icl_pdu *request, *response; 2320 const struct iscsi_bhs_scsi_command *bhssc; 2321 struct iscsi_bhs_data_in *bhsdi; 2322 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 2323 size_t len, expected_len, sg_len, buffer_offset; 2324 const char *sg_addr; 2325 int ctl_sg_count, error, i; 2326 2327 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2328 cs = PDU_SESSION(request); 2329 2330 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2331 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2332 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2333 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2334 2335 if (io->scsiio.kern_sg_entries > 0) { 2336 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 2337 ctl_sg_count = io->scsiio.kern_sg_entries; 2338 } else { 2339 ctl_sglist = &ctl_sg_entry; 2340 ctl_sglist->addr = io->scsiio.kern_data_ptr; 2341 ctl_sglist->len = io->scsiio.kern_data_len; 2342 ctl_sg_count = 1; 2343 } 2344 2345 /* 2346 * This is the total amount of data to be transferred within the current 2347 * SCSI command. We need to record it so that we can properly report 2348 * underflow/underflow. 2349 */ 2350 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2351 2352 /* 2353 * This is the offset within the current SCSI command; for the first 2354 * call to cfiscsi_datamove() it will be 0, and for subsequent ones 2355 * it will be the sum of lengths of previous ones. 2356 */ 2357 buffer_offset = io->scsiio.kern_rel_offset; 2358 2359 /* 2360 * This is the transfer length expected by the initiator. In theory, 2361 * it could be different from the correct amount of data from the SCSI 2362 * point of view, even if that doesn't make any sense. 2363 */ 2364 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length); 2365#if 0 2366 if (expected_len != io->scsiio.kern_total_len) { 2367 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, " 2368 "actual length %zd", expected_len, 2369 (size_t)io->scsiio.kern_total_len); 2370 } 2371#endif 2372 2373 if (buffer_offset >= expected_len) { 2374#if 0 2375 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, " 2376 "already sent the expected len", buffer_offset); 2377#endif 2378 io->scsiio.be_move_done(io); 2379 return; 2380 } 2381 2382 i = 0; 2383 sg_addr = NULL; 2384 sg_len = 0; 2385 response = NULL; 2386 bhsdi = NULL; 2387 for (;;) { 2388 if (response == NULL) { 2389 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2390 if (response == NULL) { 2391 CFISCSI_SESSION_WARN(cs, "failed to " 2392 "allocate memory; dropping connection"); 2393 ctl_set_busy(&io->scsiio); 2394 io->scsiio.be_move_done(io); 2395 cfiscsi_session_terminate(cs); 2396 return; 2397 } 2398 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs; 2399 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN; 2400 bhsdi->bhsdi_initiator_task_tag = 2401 bhssc->bhssc_initiator_task_tag; 2402 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request)); 2403 PDU_EXPDATASN(request)++; 2404 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset); 2405 } 2406 2407 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count")); 2408 if (sg_len == 0) { 2409 sg_addr = ctl_sglist[i].addr; 2410 sg_len = ctl_sglist[i].len; 2411 KASSERT(sg_len > 0, ("sg_len <= 0")); 2412 } 2413 2414 len = sg_len; 2415 2416 /* 2417 * Truncate to maximum data segment length. 2418 */ 2419 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length, 2420 ("ip_data_len %zd >= max_data_segment_length %zd", 2421 response->ip_data_len, cs->cs_max_data_segment_length)); 2422 if (response->ip_data_len + len > 2423 cs->cs_max_data_segment_length) { 2424 len = cs->cs_max_data_segment_length - 2425 response->ip_data_len; 2426 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2427 len, sg_len)); 2428 } 2429 2430 /* 2431 * Truncate to expected data transfer length. 2432 */ 2433 KASSERT(buffer_offset + response->ip_data_len < expected_len, 2434 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd", 2435 buffer_offset, response->ip_data_len, expected_len)); 2436 if (buffer_offset + response->ip_data_len + len > expected_len) { 2437 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd " 2438 "to expected data transfer length %zd", 2439 buffer_offset + response->ip_data_len + len, expected_len); 2440 len = expected_len - (buffer_offset + response->ip_data_len); 2441 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2442 len, sg_len)); 2443 } 2444 2445 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT); 2446 if (error != 0) { 2447 CFISCSI_SESSION_WARN(cs, "failed to " 2448 "allocate memory; dropping connection"); 2449 icl_pdu_free(response); 2450 ctl_set_busy(&io->scsiio); 2451 io->scsiio.be_move_done(io); 2452 cfiscsi_session_terminate(cs); 2453 return; 2454 } 2455 sg_addr += len; 2456 sg_len -= len; 2457 2458 KASSERT(buffer_offset + response->ip_data_len <= expected_len, 2459 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd", 2460 buffer_offset, response->ip_data_len, expected_len)); 2461 if (buffer_offset + response->ip_data_len == expected_len) { 2462 /* 2463 * Already have the amount of data the initiator wanted. 2464 */ 2465 break; 2466 } 2467 2468 if (sg_len == 0) { 2469 /* 2470 * End of scatter-gather segment; 2471 * proceed to the next one... 2472 */ 2473 if (i == ctl_sg_count - 1) { 2474 /* 2475 * ... unless this was the last one. 2476 */ 2477 break; 2478 } 2479 i++; 2480 } 2481 2482 if (response->ip_data_len == cs->cs_max_data_segment_length) { 2483 /* 2484 * Can't stuff more data into the current PDU; 2485 * queue it. Note that's not enough to check 2486 * for kern_data_resid == 0 instead; there 2487 * may be several Data-In PDUs for the final 2488 * call to cfiscsi_datamove(), and we want 2489 * to set the F flag only on the last of them. 2490 */ 2491 buffer_offset += response->ip_data_len; 2492 if (buffer_offset == io->scsiio.kern_total_len || 2493 buffer_offset == expected_len) { 2494 buffer_offset -= response->ip_data_len; 2495 break; 2496 } 2497 cfiscsi_pdu_queue(response); 2498 response = NULL; 2499 bhsdi = NULL; 2500 } 2501 } 2502 if (response != NULL) { 2503 buffer_offset += response->ip_data_len; 2504 if (buffer_offset == io->scsiio.kern_total_len || 2505 buffer_offset == expected_len) { 2506 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F; 2507 if (io->io_hdr.status == CTL_SUCCESS) { 2508 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S; 2509 if (PDU_TOTAL_TRANSFER_LEN(request) < 2510 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2511 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW; 2512 bhsdi->bhsdi_residual_count = 2513 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) - 2514 PDU_TOTAL_TRANSFER_LEN(request)); 2515 } else if (PDU_TOTAL_TRANSFER_LEN(request) > 2516 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2517 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW; 2518 bhsdi->bhsdi_residual_count = 2519 htonl(PDU_TOTAL_TRANSFER_LEN(request) - 2520 ntohl(bhssc->bhssc_expected_data_transfer_length)); 2521 } 2522 bhsdi->bhsdi_status = io->scsiio.scsi_status; 2523 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 2524 } 2525 } 2526 KASSERT(response->ip_data_len > 0, ("sending empty Data-In")); 2527 cfiscsi_pdu_queue(response); 2528 } 2529 2530 io->scsiio.be_move_done(io); 2531} 2532 2533static void 2534cfiscsi_datamove_out(union ctl_io *io) 2535{ 2536 struct cfiscsi_session *cs; 2537 struct icl_pdu *request, *response; 2538 const struct iscsi_bhs_scsi_command *bhssc; 2539 struct iscsi_bhs_r2t *bhsr2t; 2540 struct cfiscsi_data_wait *cdw; 2541 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 2542 uint32_t expected_len, r2t_off, r2t_len; 2543 uint32_t target_transfer_tag; 2544 bool done; 2545 2546 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2547 cs = PDU_SESSION(request); 2548 2549 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2550 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2551 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2552 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2553 2554 /* 2555 * We need to record it so that we can properly report 2556 * underflow/underflow. 2557 */ 2558 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2559 2560 /* 2561 * Report write underflow as error since CTL and backends don't 2562 * really support it, and SCSI does not tell how to do it right. 2563 */ 2564 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length); 2565 if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len > 2566 expected_len) { 2567 io->scsiio.io_hdr.port_status = 43; 2568 io->scsiio.be_move_done(io); 2569 return; 2570 } 2571 2572 target_transfer_tag = 2573 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1); 2574 2575#if 0 2576 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator " 2577 "task tag 0x%x, target transfer tag 0x%x", 2578 bhssc->bhssc_initiator_task_tag, target_transfer_tag); 2579#endif 2580 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO); 2581 if (cdw == NULL) { 2582 CFISCSI_SESSION_WARN(cs, "failed to " 2583 "allocate memory; dropping connection"); 2584 ctl_set_busy(&io->scsiio); 2585 io->scsiio.be_move_done(io); 2586 cfiscsi_session_terminate(cs); 2587 return; 2588 } 2589 cdw->cdw_ctl_io = io; 2590 cdw->cdw_target_transfer_tag = target_transfer_tag; 2591 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2592 cdw->cdw_r2t_end = io->scsiio.kern_data_len; 2593 cdw->cdw_datasn = 0; 2594 2595 /* Set initial data pointer for the CDW respecting ext_data_filled. */ 2596 if (io->scsiio.kern_sg_entries > 0) { 2597 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 2598 } else { 2599 ctl_sglist = &ctl_sg_entry; 2600 ctl_sglist->addr = io->scsiio.kern_data_ptr; 2601 ctl_sglist->len = io->scsiio.kern_data_len; 2602 } 2603 cdw->cdw_sg_index = 0; 2604 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr; 2605 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len; 2606 r2t_off = io->scsiio.ext_data_filled; 2607 while (r2t_off > 0) { 2608 if (r2t_off >= cdw->cdw_sg_len) { 2609 r2t_off -= cdw->cdw_sg_len; 2610 cdw->cdw_sg_index++; 2611 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr; 2612 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len; 2613 continue; 2614 } 2615 cdw->cdw_sg_addr += r2t_off; 2616 cdw->cdw_sg_len -= r2t_off; 2617 r2t_off = 0; 2618 } 2619 2620 if (cs->cs_immediate_data && 2621 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled < 2622 icl_pdu_data_segment_length(request)) { 2623 done = cfiscsi_handle_data_segment(request, cdw); 2624 if (done) { 2625 uma_zfree(cfiscsi_data_wait_zone, cdw); 2626 io->scsiio.be_move_done(io); 2627 return; 2628 } 2629 } 2630 2631 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled; 2632 r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled, 2633 cs->cs_max_burst_length); 2634 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len; 2635 2636 CFISCSI_SESSION_LOCK(cs); 2637 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next); 2638 CFISCSI_SESSION_UNLOCK(cs); 2639 2640 /* 2641 * XXX: We should limit the number of outstanding R2T PDUs 2642 * per task to MaxOutstandingR2T. 2643 */ 2644 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2645 if (response == NULL) { 2646 CFISCSI_SESSION_WARN(cs, "failed to " 2647 "allocate memory; dropping connection"); 2648 ctl_set_busy(&io->scsiio); 2649 io->scsiio.be_move_done(io); 2650 cfiscsi_session_terminate(cs); 2651 return; 2652 } 2653 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs; 2654 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T; 2655 bhsr2t->bhsr2t_flags = 0x80; 2656 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun; 2657 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2658 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag; 2659 /* 2660 * XXX: Here we assume that cfiscsi_datamove() won't ever 2661 * be running concurrently on several CPUs for a given 2662 * command. 2663 */ 2664 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request)); 2665 PDU_R2TSN(request)++; 2666 /* 2667 * This is the offset within the current SCSI command; 2668 * i.e. for the first call of datamove(), it will be 0, 2669 * and for subsequent ones it will be the sum of lengths 2670 * of previous ones. 2671 * 2672 * The ext_data_filled is to account for unsolicited 2673 * (immediate) data that might have already arrived. 2674 */ 2675 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off); 2676 /* 2677 * This is the total length (sum of S/G lengths) this call 2678 * to cfiscsi_datamove() is supposed to handle, limited by 2679 * MaxBurstLength. 2680 */ 2681 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len); 2682 cfiscsi_pdu_queue(response); 2683} 2684 2685static void 2686cfiscsi_datamove(union ctl_io *io) 2687{ 2688 2689 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 2690 cfiscsi_datamove_in(io); 2691 else { 2692 /* We hadn't received anything during this datamove yet. */ 2693 io->scsiio.ext_data_filled = 0; 2694 cfiscsi_datamove_out(io); 2695 } 2696} 2697 2698static void 2699cfiscsi_scsi_command_done(union ctl_io *io) 2700{ 2701 struct icl_pdu *request, *response; 2702 struct iscsi_bhs_scsi_command *bhssc; 2703 struct iscsi_bhs_scsi_response *bhssr; 2704#ifdef DIAGNOSTIC 2705 struct cfiscsi_data_wait *cdw; 2706#endif 2707 struct cfiscsi_session *cs; 2708 uint16_t sense_length; 2709 2710 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2711 cs = PDU_SESSION(request); 2712 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 2713 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2714 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2715 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode)); 2716 2717 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x", 2718 // bhssc->bhssc_initiator_task_tag); 2719 2720#ifdef DIAGNOSTIC 2721 CFISCSI_SESSION_LOCK(cs); 2722 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) 2723 KASSERT(bhssc->bhssc_initiator_task_tag != 2724 cdw->cdw_initiator_task_tag, ("dangling cdw")); 2725 CFISCSI_SESSION_UNLOCK(cs); 2726#endif 2727 2728 /* 2729 * Do not return status for aborted commands. 2730 * There are exceptions, but none supported by CTL yet. 2731 */ 2732 if (((io->io_hdr.flags & CTL_FLAG_ABORT) && 2733 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) || 2734 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) { 2735 ctl_free_io(io); 2736 icl_pdu_free(request); 2737 return; 2738 } 2739 2740 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2741 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 2742 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE; 2743 bhssr->bhssr_flags = 0x80; 2744 /* 2745 * XXX: We don't deal with bidirectional under/overflows; 2746 * does anything actually support those? 2747 */ 2748 if (PDU_TOTAL_TRANSFER_LEN(request) < 2749 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2750 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW; 2751 bhssr->bhssr_residual_count = 2752 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) - 2753 PDU_TOTAL_TRANSFER_LEN(request)); 2754 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d", 2755 // ntohl(bhssr->bhssr_residual_count)); 2756 } else if (PDU_TOTAL_TRANSFER_LEN(request) > 2757 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2758 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW; 2759 bhssr->bhssr_residual_count = 2760 htonl(PDU_TOTAL_TRANSFER_LEN(request) - 2761 ntohl(bhssc->bhssc_expected_data_transfer_length)); 2762 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d", 2763 // ntohl(bhssr->bhssr_residual_count)); 2764 } 2765 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED; 2766 bhssr->bhssr_status = io->scsiio.scsi_status; 2767 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2768 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request)); 2769 2770 if (io->scsiio.sense_len > 0) { 2771#if 0 2772 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data", 2773 io->scsiio.sense_len); 2774#endif 2775 sense_length = htons(io->scsiio.sense_len); 2776 icl_pdu_append_data(response, 2777 &sense_length, sizeof(sense_length), M_WAITOK); 2778 icl_pdu_append_data(response, 2779 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK); 2780 } 2781 2782 ctl_free_io(io); 2783 icl_pdu_free(request); 2784 cfiscsi_pdu_queue(response); 2785} 2786 2787static void 2788cfiscsi_task_management_done(union ctl_io *io) 2789{ 2790 struct icl_pdu *request, *response; 2791 struct iscsi_bhs_task_management_request *bhstmr; 2792 struct iscsi_bhs_task_management_response *bhstmr2; 2793 struct cfiscsi_data_wait *cdw, *tmpcdw; 2794 struct cfiscsi_session *cs; 2795 2796 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2797 cs = PDU_SESSION(request); 2798 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 2799 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2800 ISCSI_BHS_OPCODE_TASK_REQUEST, 2801 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode)); 2802 2803#if 0 2804 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x", 2805 bhstmr->bhstmr_initiator_task_tag, 2806 bhstmr->bhstmr_referenced_task_tag); 2807#endif 2808 2809 if ((bhstmr->bhstmr_function & ~0x80) == 2810 BHSTMR_FUNCTION_ABORT_TASK) { 2811 /* 2812 * Make sure we no longer wait for Data-Out for this command. 2813 */ 2814 CFISCSI_SESSION_LOCK(cs); 2815 TAILQ_FOREACH_SAFE(cdw, 2816 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) { 2817 if (bhstmr->bhstmr_referenced_task_tag != 2818 cdw->cdw_initiator_task_tag) 2819 continue; 2820 2821#if 0 2822 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task " 2823 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag); 2824#endif 2825 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, 2826 cdw, cdw_next); 2827 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io); 2828 uma_zfree(cfiscsi_data_wait_zone, cdw); 2829 } 2830 CFISCSI_SESSION_UNLOCK(cs); 2831 } 2832 2833 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2834 bhstmr2 = (struct iscsi_bhs_task_management_response *) 2835 response->ip_bhs; 2836 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE; 2837 bhstmr2->bhstmr_flags = 0x80; 2838 if (io->io_hdr.status == CTL_SUCCESS) { 2839 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE; 2840 } else { 2841 /* 2842 * XXX: How to figure out what exactly went wrong? iSCSI spec 2843 * expects us to provide detailed error, e.g. "Task does 2844 * not exist" or "LUN does not exist". 2845 */ 2846 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED"); 2847 bhstmr2->bhstmr_response = 2848 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED; 2849 } 2850 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag; 2851 2852 ctl_free_io(io); 2853 icl_pdu_free(request); 2854 cfiscsi_pdu_queue(response); 2855} 2856 2857static void 2858cfiscsi_done(union ctl_io *io) 2859{ 2860 struct icl_pdu *request; 2861 struct cfiscsi_session *cs; 2862 2863 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE), 2864 ("invalid CTL status %#x", io->io_hdr.status)); 2865 2866 if (io->io_hdr.io_type == CTL_IO_TASK && 2867 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) { 2868 /* 2869 * Implicit task termination has just completed; nothing to do. 2870 */ 2871 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2872 cs->cs_tasks_aborted = true; 2873 refcount_release(&cs->cs_outstanding_ctl_pdus); 2874 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus)); 2875 ctl_free_io(io); 2876 return; 2877 } 2878 2879 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2880 cs = PDU_SESSION(request); 2881 refcount_release(&cs->cs_outstanding_ctl_pdus); 2882 2883 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) { 2884 case ISCSI_BHS_OPCODE_SCSI_COMMAND: 2885 cfiscsi_scsi_command_done(io); 2886 break; 2887 case ISCSI_BHS_OPCODE_TASK_REQUEST: 2888 cfiscsi_task_management_done(io); 2889 break; 2890 default: 2891 panic("cfiscsi_done called with wrong opcode 0x%x", 2892 request->ip_bhs->bhs_opcode); 2893 } 2894} 2895