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