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