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