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