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