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