ctl_frontend_iscsi.c revision 288786
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 288786 2015-10-05 10:50:35Z 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 288786 2015-10-05 10:50:35Z 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 if (strcmp(cs->cs_initiator_addr, 1569 cs2->cs_initiator_addr) != 0) { 1570 CFISCSI_SESSION_WARN(cs2, 1571 "session reinstatement from " 1572 "different address %s", 1573 cs->cs_initiator_addr); 1574 } else { 1575 CFISCSI_SESSION_DEBUG(cs2, 1576 "session reinstatement"); 1577 } 1578 cfiscsi_session_terminate(cs2); 1579 mtx_unlock(&softc->lock); 1580 pause("cfiscsi_reinstate", 1); 1581 goto restart; 1582 } 1583 } 1584 mtx_unlock(&softc->lock); 1585 } 1586 1587 /* 1588 * Register initiator with CTL. 1589 */ 1590 cfiscsi_session_register_initiator(cs); 1591 1592#ifdef ICL_KERNEL_PROXY 1593 if (cihp->socket > 0) { 1594#endif 1595 error = icl_conn_handoff(cs->cs_conn, cihp->socket); 1596 if (error != 0) { 1597 cfiscsi_session_terminate(cs); 1598 refcount_release(&cs->cs_outstanding_ctl_pdus); 1599 ci->status = CTL_ISCSI_ERROR; 1600 snprintf(ci->error_str, sizeof(ci->error_str), 1601 "%s: icl_conn_handoff failed with error %d", 1602 __func__, error); 1603 return; 1604 } 1605#ifdef ICL_KERNEL_PROXY 1606 } 1607#endif 1608 1609#ifdef ICL_KERNEL_PROXY 1610 cs->cs_login_phase = false; 1611 1612 /* 1613 * First PDU of the Full Feature phase has likely already arrived. 1614 * We have to pick it up and execute properly. 1615 */ 1616 if (cs->cs_login_pdu != NULL) { 1617 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU"); 1618 cfiscsi_pdu_handle(cs->cs_login_pdu); 1619 cs->cs_login_pdu = NULL; 1620 } 1621#endif 1622 1623 refcount_release(&cs->cs_outstanding_ctl_pdus); 1624 ci->status = CTL_ISCSI_OK; 1625} 1626 1627static void 1628cfiscsi_ioctl_list(struct ctl_iscsi *ci) 1629{ 1630 struct ctl_iscsi_list_params *cilp; 1631 struct cfiscsi_session *cs; 1632 struct cfiscsi_softc *softc; 1633 struct sbuf *sb; 1634 int error; 1635 1636 cilp = (struct ctl_iscsi_list_params *)&(ci->data); 1637 softc = &cfiscsi_softc; 1638 1639 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN); 1640 if (sb == NULL) { 1641 ci->status = CTL_ISCSI_ERROR; 1642 snprintf(ci->error_str, sizeof(ci->error_str), 1643 "Unable to allocate %d bytes for iSCSI session list", 1644 cilp->alloc_len); 1645 return; 1646 } 1647 1648 sbuf_printf(sb, "<ctlislist>\n"); 1649 mtx_lock(&softc->lock); 1650 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1651#ifdef ICL_KERNEL_PROXY 1652 if (cs->cs_target == NULL) 1653 continue; 1654#endif 1655 error = sbuf_printf(sb, "<connection id=\"%d\">" 1656 "<initiator>%s</initiator>" 1657 "<initiator_addr>%s</initiator_addr>" 1658 "<initiator_alias>%s</initiator_alias>" 1659 "<target>%s</target>" 1660 "<target_alias>%s</target_alias>" 1661 "<target_portal_group_tag>%u</target_portal_group_tag>" 1662 "<header_digest>%s</header_digest>" 1663 "<data_digest>%s</data_digest>" 1664 "<max_data_segment_length>%zd</max_data_segment_length>" 1665 "<immediate_data>%d</immediate_data>" 1666 "<iser>%d</iser>" 1667 "</connection>\n", 1668 cs->cs_id, 1669 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias, 1670 cs->cs_target->ct_name, cs->cs_target->ct_alias, 1671 cs->cs_target->ct_tag, 1672 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None", 1673 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None", 1674 cs->cs_max_data_segment_length, 1675 cs->cs_immediate_data, 1676 cs->cs_conn->ic_iser); 1677 if (error != 0) 1678 break; 1679 } 1680 mtx_unlock(&softc->lock); 1681 error = sbuf_printf(sb, "</ctlislist>\n"); 1682 if (error != 0) { 1683 sbuf_delete(sb); 1684 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE; 1685 snprintf(ci->error_str, sizeof(ci->error_str), 1686 "Out of space, %d bytes is too small", cilp->alloc_len); 1687 return; 1688 } 1689 sbuf_finish(sb); 1690 1691 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1); 1692 cilp->fill_len = sbuf_len(sb) + 1; 1693 ci->status = CTL_ISCSI_OK; 1694 sbuf_delete(sb); 1695} 1696 1697static void 1698cfiscsi_ioctl_logout(struct ctl_iscsi *ci) 1699{ 1700 struct icl_pdu *response; 1701 struct iscsi_bhs_asynchronous_message *bhsam; 1702 struct ctl_iscsi_logout_params *cilp; 1703 struct cfiscsi_session *cs; 1704 struct cfiscsi_softc *softc; 1705 int found = 0; 1706 1707 cilp = (struct ctl_iscsi_logout_params *)&(ci->data); 1708 softc = &cfiscsi_softc; 1709 1710 mtx_lock(&softc->lock); 1711 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1712 if (cilp->all == 0 && cs->cs_id != cilp->connection_id && 1713 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 && 1714 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0) 1715 continue; 1716 1717 response = icl_pdu_new(cs->cs_conn, M_NOWAIT); 1718 if (response == NULL) { 1719 ci->status = CTL_ISCSI_ERROR; 1720 snprintf(ci->error_str, sizeof(ci->error_str), 1721 "Unable to allocate memory"); 1722 mtx_unlock(&softc->lock); 1723 return; 1724 } 1725 bhsam = 1726 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs; 1727 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE; 1728 bhsam->bhsam_flags = 0x80; 1729 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT; 1730 bhsam->bhsam_parameter3 = htons(10); 1731 cfiscsi_pdu_queue(response); 1732 found++; 1733 } 1734 mtx_unlock(&softc->lock); 1735 1736 if (found == 0) { 1737 ci->status = CTL_ISCSI_SESSION_NOT_FOUND; 1738 snprintf(ci->error_str, sizeof(ci->error_str), 1739 "No matching connections found"); 1740 return; 1741 } 1742 1743 ci->status = CTL_ISCSI_OK; 1744} 1745 1746static void 1747cfiscsi_ioctl_terminate(struct ctl_iscsi *ci) 1748{ 1749 struct icl_pdu *response; 1750 struct iscsi_bhs_asynchronous_message *bhsam; 1751 struct ctl_iscsi_terminate_params *citp; 1752 struct cfiscsi_session *cs; 1753 struct cfiscsi_softc *softc; 1754 int found = 0; 1755 1756 citp = (struct ctl_iscsi_terminate_params *)&(ci->data); 1757 softc = &cfiscsi_softc; 1758 1759 mtx_lock(&softc->lock); 1760 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1761 if (citp->all == 0 && cs->cs_id != citp->connection_id && 1762 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 && 1763 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0) 1764 continue; 1765 1766 response = icl_pdu_new(cs->cs_conn, M_NOWAIT); 1767 if (response == NULL) { 1768 /* 1769 * Oh well. Just terminate the connection. 1770 */ 1771 } else { 1772 bhsam = (struct iscsi_bhs_asynchronous_message *) 1773 response->ip_bhs; 1774 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE; 1775 bhsam->bhsam_flags = 0x80; 1776 bhsam->bhsam_0xffffffff = 0xffffffff; 1777 bhsam->bhsam_async_event = 1778 BHSAM_EVENT_TARGET_TERMINATES_SESSION; 1779 cfiscsi_pdu_queue(response); 1780 } 1781 cfiscsi_session_terminate(cs); 1782 found++; 1783 } 1784 mtx_unlock(&softc->lock); 1785 1786 if (found == 0) { 1787 ci->status = CTL_ISCSI_SESSION_NOT_FOUND; 1788 snprintf(ci->error_str, sizeof(ci->error_str), 1789 "No matching connections found"); 1790 return; 1791 } 1792 1793 ci->status = CTL_ISCSI_OK; 1794} 1795 1796#ifdef ICL_KERNEL_PROXY 1797static void 1798cfiscsi_ioctl_listen(struct ctl_iscsi *ci) 1799{ 1800 struct ctl_iscsi_listen_params *cilp; 1801 struct sockaddr *sa; 1802 int error; 1803 1804 cilp = (struct ctl_iscsi_listen_params *)&(ci->data); 1805 1806 if (cfiscsi_softc.listener == NULL) { 1807 CFISCSI_DEBUG("no listener"); 1808 snprintf(ci->error_str, sizeof(ci->error_str), "no listener"); 1809 ci->status = CTL_ISCSI_ERROR; 1810 return; 1811 } 1812 1813 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen); 1814 if (error != 0) { 1815 CFISCSI_DEBUG("getsockaddr, error %d", error); 1816 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed"); 1817 ci->status = CTL_ISCSI_ERROR; 1818 return; 1819 } 1820 1821 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain, 1822 cilp->socktype, cilp->protocol, sa, cilp->portal_id); 1823 if (error != 0) { 1824 free(sa, M_SONAME); 1825 CFISCSI_DEBUG("icl_listen_add, error %d", error); 1826 snprintf(ci->error_str, sizeof(ci->error_str), 1827 "icl_listen_add failed, error %d", error); 1828 ci->status = CTL_ISCSI_ERROR; 1829 return; 1830 } 1831 1832 ci->status = CTL_ISCSI_OK; 1833} 1834 1835static void 1836cfiscsi_ioctl_accept(struct ctl_iscsi *ci) 1837{ 1838 struct ctl_iscsi_accept_params *ciap; 1839 struct cfiscsi_session *cs; 1840 int error; 1841 1842 ciap = (struct ctl_iscsi_accept_params *)&(ci->data); 1843 1844 mtx_lock(&cfiscsi_softc.lock); 1845 for (;;) { 1846 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1847 if (cs->cs_waiting_for_ctld) 1848 break; 1849 } 1850 if (cs != NULL) 1851 break; 1852 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock); 1853 if (error != 0) { 1854 mtx_unlock(&cfiscsi_softc.lock); 1855 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted"); 1856 ci->status = CTL_ISCSI_ERROR; 1857 return; 1858 } 1859 } 1860 mtx_unlock(&cfiscsi_softc.lock); 1861 1862 cs->cs_waiting_for_ctld = false; 1863 cs->cs_login_phase = true; 1864 1865 ciap->connection_id = cs->cs_id; 1866 ciap->portal_id = cs->cs_portal_id; 1867 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len; 1868 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr, 1869 cs->cs_initiator_sa->sa_len); 1870 if (error != 0) { 1871 snprintf(ci->error_str, sizeof(ci->error_str), 1872 "copyout failed with error %d", error); 1873 ci->status = CTL_ISCSI_ERROR; 1874 return; 1875 } 1876 1877 ci->status = CTL_ISCSI_OK; 1878} 1879 1880static void 1881cfiscsi_ioctl_send(struct ctl_iscsi *ci) 1882{ 1883 struct ctl_iscsi_send_params *cisp; 1884 struct cfiscsi_session *cs; 1885 struct icl_pdu *ip; 1886 size_t datalen; 1887 void *data; 1888 int error; 1889 1890 cisp = (struct ctl_iscsi_send_params *)&(ci->data); 1891 1892 mtx_lock(&cfiscsi_softc.lock); 1893 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1894 if (cs->cs_id == cisp->connection_id) 1895 break; 1896 } 1897 if (cs == NULL) { 1898 mtx_unlock(&cfiscsi_softc.lock); 1899 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found"); 1900 ci->status = CTL_ISCSI_ERROR; 1901 return; 1902 } 1903 mtx_unlock(&cfiscsi_softc.lock); 1904 1905#if 0 1906 if (cs->cs_login_phase == false) 1907 return (EBUSY); 1908#endif 1909 1910 if (cs->cs_terminating) { 1911 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating"); 1912 ci->status = CTL_ISCSI_ERROR; 1913 return; 1914 } 1915 1916 datalen = cisp->data_segment_len; 1917 /* 1918 * XXX 1919 */ 1920 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) { 1921 if (datalen > 65535) { 1922 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big"); 1923 ci->status = CTL_ISCSI_ERROR; 1924 return; 1925 } 1926 if (datalen > 0) { 1927 data = malloc(datalen, M_CFISCSI, M_WAITOK); 1928 error = copyin(cisp->data_segment, data, datalen); 1929 if (error != 0) { 1930 free(data, M_CFISCSI); 1931 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error); 1932 ci->status = CTL_ISCSI_ERROR; 1933 return; 1934 } 1935 } 1936 1937 ip = icl_pdu_new(cs->cs_conn, M_WAITOK); 1938 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs)); 1939 if (datalen > 0) { 1940 icl_pdu_append_data(ip, data, datalen, M_WAITOK); 1941 free(data, M_CFISCSI); 1942 } 1943 CFISCSI_SESSION_LOCK(cs); 1944 icl_pdu_queue(ip); 1945 CFISCSI_SESSION_UNLOCK(cs); 1946 ci->status = CTL_ISCSI_OK; 1947} 1948 1949static void 1950cfiscsi_ioctl_receive(struct ctl_iscsi *ci) 1951{ 1952 struct ctl_iscsi_receive_params *cirp; 1953 struct cfiscsi_session *cs; 1954 struct icl_pdu *ip; 1955 void *data; 1956 int error; 1957 1958 cirp = (struct ctl_iscsi_receive_params *)&(ci->data); 1959 1960 mtx_lock(&cfiscsi_softc.lock); 1961 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1962 if (cs->cs_id == cirp->connection_id) 1963 break; 1964 } 1965 if (cs == NULL) { 1966 mtx_unlock(&cfiscsi_softc.lock); 1967 snprintf(ci->error_str, sizeof(ci->error_str), 1968 "connection not found"); 1969 ci->status = CTL_ISCSI_ERROR; 1970 return; 1971 } 1972 mtx_unlock(&cfiscsi_softc.lock); 1973 1974#if 0 1975 if (is->is_login_phase == false) 1976 return (EBUSY); 1977#endif 1978 1979 CFISCSI_SESSION_LOCK(cs); 1980 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) { 1981 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock); 1982 if (error != 0) { 1983 CFISCSI_SESSION_UNLOCK(cs); 1984 snprintf(ci->error_str, sizeof(ci->error_str), 1985 "interrupted by signal"); 1986 ci->status = CTL_ISCSI_ERROR; 1987 return; 1988 } 1989 } 1990 1991 if (cs->cs_terminating) { 1992 CFISCSI_SESSION_UNLOCK(cs); 1993 snprintf(ci->error_str, sizeof(ci->error_str), 1994 "connection terminating"); 1995 ci->status = CTL_ISCSI_ERROR; 1996 return; 1997 } 1998 ip = cs->cs_login_pdu; 1999 cs->cs_login_pdu = NULL; 2000 CFISCSI_SESSION_UNLOCK(cs); 2001 2002 if (ip->ip_data_len > cirp->data_segment_len) { 2003 icl_pdu_free(ip); 2004 snprintf(ci->error_str, sizeof(ci->error_str), 2005 "data segment too big"); 2006 ci->status = CTL_ISCSI_ERROR; 2007 return; 2008 } 2009 2010 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs)); 2011 if (ip->ip_data_len > 0) { 2012 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK); 2013 icl_pdu_get_data(ip, 0, data, ip->ip_data_len); 2014 copyout(data, cirp->data_segment, ip->ip_data_len); 2015 free(data, M_CFISCSI); 2016 } 2017 2018 icl_pdu_free(ip); 2019 ci->status = CTL_ISCSI_OK; 2020} 2021 2022#endif /* !ICL_KERNEL_PROXY */ 2023 2024static void 2025cfiscsi_ioctl_port_create(struct ctl_req *req) 2026{ 2027 struct cfiscsi_target *ct; 2028 struct ctl_port *port; 2029 const char *target, *alias, *tags; 2030 struct scsi_vpd_id_descriptor *desc; 2031 ctl_options_t opts; 2032 int retval, len, idlen; 2033 uint16_t tag; 2034 2035 ctl_init_opts(&opts, req->num_args, req->kern_args); 2036 target = ctl_get_opt(&opts, "cfiscsi_target"); 2037 alias = ctl_get_opt(&opts, "cfiscsi_target_alias"); 2038 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag"); 2039 if (target == NULL || tags == NULL) { 2040 req->status = CTL_LUN_ERROR; 2041 snprintf(req->error_str, sizeof(req->error_str), 2042 "Missing required argument"); 2043 ctl_free_opts(&opts); 2044 return; 2045 } 2046 tag = strtol(tags, (char **)NULL, 10); 2047 ct = cfiscsi_target_find_or_create(&cfiscsi_softc, target, alias, tag); 2048 if (ct == NULL) { 2049 req->status = CTL_LUN_ERROR; 2050 snprintf(req->error_str, sizeof(req->error_str), 2051 "failed to create target \"%s\"", target); 2052 ctl_free_opts(&opts); 2053 return; 2054 } 2055 if (ct->ct_state == CFISCSI_TARGET_STATE_ACTIVE) { 2056 req->status = CTL_LUN_ERROR; 2057 snprintf(req->error_str, sizeof(req->error_str), 2058 "target \"%s\" already exists", target); 2059 cfiscsi_target_release(ct); 2060 ctl_free_opts(&opts); 2061 return; 2062 } 2063 port = &ct->ct_port; 2064 if (ct->ct_state == CFISCSI_TARGET_STATE_DYING) 2065 goto done; 2066 2067 port->frontend = &cfiscsi_frontend; 2068 port->port_type = CTL_PORT_ISCSI; 2069 /* XXX KDM what should the real number be here? */ 2070 port->num_requested_ctl_io = 4096; 2071 port->port_name = "iscsi"; 2072 port->physical_port = tag; 2073 port->virtual_port = ct->ct_target_id; 2074 port->port_online = cfiscsi_online; 2075 port->port_offline = cfiscsi_offline; 2076 port->port_info = cfiscsi_info; 2077 port->onoff_arg = ct; 2078 port->fe_datamove = cfiscsi_datamove; 2079 port->fe_done = cfiscsi_done; 2080 2081 /* XXX KDM what should we report here? */ 2082 /* XXX These should probably be fetched from CTL. */ 2083 port->max_targets = 1; 2084 port->max_target_id = 15; 2085 port->targ_port = -1; 2086 2087 port->options = opts; 2088 STAILQ_INIT(&opts); 2089 2090 /* Generate Port ID. */ 2091 idlen = strlen(target) + strlen(",t,0x0001") + 1; 2092 idlen = roundup2(idlen, 4); 2093 len = sizeof(struct scsi_vpd_device_id) + idlen; 2094 port->port_devid = malloc(sizeof(struct ctl_devid) + len, 2095 M_CTL, M_WAITOK | M_ZERO); 2096 port->port_devid->len = len; 2097 desc = (struct scsi_vpd_id_descriptor *)port->port_devid->data; 2098 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2099 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2100 SVPD_ID_TYPE_SCSI_NAME; 2101 desc->length = idlen; 2102 snprintf(desc->identifier, idlen, "%s,t,0x%4.4x", target, tag); 2103 2104 /* Generate Target ID. */ 2105 idlen = strlen(target) + 1; 2106 idlen = roundup2(idlen, 4); 2107 len = sizeof(struct scsi_vpd_device_id) + idlen; 2108 port->target_devid = malloc(sizeof(struct ctl_devid) + len, 2109 M_CTL, M_WAITOK | M_ZERO); 2110 port->target_devid->len = len; 2111 desc = (struct scsi_vpd_id_descriptor *)port->target_devid->data; 2112 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2113 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET | 2114 SVPD_ID_TYPE_SCSI_NAME; 2115 desc->length = idlen; 2116 strlcpy(desc->identifier, target, idlen); 2117 2118 retval = ctl_port_register(port); 2119 if (retval != 0) { 2120 ctl_free_opts(&port->options); 2121 cfiscsi_target_release(ct); 2122 free(port->port_devid, M_CFISCSI); 2123 free(port->target_devid, M_CFISCSI); 2124 req->status = CTL_LUN_ERROR; 2125 snprintf(req->error_str, sizeof(req->error_str), 2126 "ctl_port_register() failed with error %d", retval); 2127 return; 2128 } 2129done: 2130 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE; 2131 req->status = CTL_LUN_OK; 2132 memcpy(req->kern_args[0].kvalue, &port->targ_port, 2133 sizeof(port->targ_port)); //XXX 2134} 2135 2136static void 2137cfiscsi_ioctl_port_remove(struct ctl_req *req) 2138{ 2139 struct cfiscsi_target *ct; 2140 const char *target, *tags; 2141 ctl_options_t opts; 2142 uint16_t tag; 2143 2144 ctl_init_opts(&opts, req->num_args, req->kern_args); 2145 target = ctl_get_opt(&opts, "cfiscsi_target"); 2146 tags = ctl_get_opt(&opts, "cfiscsi_portal_group_tag"); 2147 if (target == NULL || tags == NULL) { 2148 ctl_free_opts(&opts); 2149 req->status = CTL_LUN_ERROR; 2150 snprintf(req->error_str, sizeof(req->error_str), 2151 "Missing required argument"); 2152 return; 2153 } 2154 tag = strtol(tags, (char **)NULL, 10); 2155 ct = cfiscsi_target_find(&cfiscsi_softc, target, tag); 2156 if (ct == NULL) { 2157 ctl_free_opts(&opts); 2158 req->status = CTL_LUN_ERROR; 2159 snprintf(req->error_str, sizeof(req->error_str), 2160 "can't find target \"%s\"", target); 2161 return; 2162 } 2163 if (ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) { 2164 ctl_free_opts(&opts); 2165 req->status = CTL_LUN_ERROR; 2166 snprintf(req->error_str, sizeof(req->error_str), 2167 "target \"%s\" is already dying", target); 2168 return; 2169 } 2170 ctl_free_opts(&opts); 2171 2172 ct->ct_state = CFISCSI_TARGET_STATE_DYING; 2173 ctl_port_offline(&ct->ct_port); 2174 cfiscsi_target_release(ct); 2175 cfiscsi_target_release(ct); 2176 req->status = CTL_LUN_OK; 2177} 2178 2179static int 2180cfiscsi_ioctl(struct cdev *dev, 2181 u_long cmd, caddr_t addr, int flag, struct thread *td) 2182{ 2183 struct ctl_iscsi *ci; 2184 struct ctl_req *req; 2185 2186 if (cmd == CTL_PORT_REQ) { 2187 req = (struct ctl_req *)addr; 2188 switch (req->reqtype) { 2189 case CTL_REQ_CREATE: 2190 cfiscsi_ioctl_port_create(req); 2191 break; 2192 case CTL_REQ_REMOVE: 2193 cfiscsi_ioctl_port_remove(req); 2194 break; 2195 default: 2196 req->status = CTL_LUN_ERROR; 2197 snprintf(req->error_str, sizeof(req->error_str), 2198 "Unsupported request type %d", req->reqtype); 2199 } 2200 return (0); 2201 } 2202 2203 if (cmd != CTL_ISCSI) 2204 return (ENOTTY); 2205 2206 ci = (struct ctl_iscsi *)addr; 2207 switch (ci->type) { 2208 case CTL_ISCSI_HANDOFF: 2209 cfiscsi_ioctl_handoff(ci); 2210 break; 2211 case CTL_ISCSI_LIST: 2212 cfiscsi_ioctl_list(ci); 2213 break; 2214 case CTL_ISCSI_LOGOUT: 2215 cfiscsi_ioctl_logout(ci); 2216 break; 2217 case CTL_ISCSI_TERMINATE: 2218 cfiscsi_ioctl_terminate(ci); 2219 break; 2220#ifdef ICL_KERNEL_PROXY 2221 case CTL_ISCSI_LISTEN: 2222 cfiscsi_ioctl_listen(ci); 2223 break; 2224 case CTL_ISCSI_ACCEPT: 2225 cfiscsi_ioctl_accept(ci); 2226 break; 2227 case CTL_ISCSI_SEND: 2228 cfiscsi_ioctl_send(ci); 2229 break; 2230 case CTL_ISCSI_RECEIVE: 2231 cfiscsi_ioctl_receive(ci); 2232 break; 2233#else 2234 case CTL_ISCSI_LISTEN: 2235 case CTL_ISCSI_ACCEPT: 2236 case CTL_ISCSI_SEND: 2237 case CTL_ISCSI_RECEIVE: 2238 ci->status = CTL_ISCSI_ERROR; 2239 snprintf(ci->error_str, sizeof(ci->error_str), 2240 "%s: CTL compiled without ICL_KERNEL_PROXY", 2241 __func__); 2242 break; 2243#endif /* !ICL_KERNEL_PROXY */ 2244 default: 2245 ci->status = CTL_ISCSI_ERROR; 2246 snprintf(ci->error_str, sizeof(ci->error_str), 2247 "%s: invalid iSCSI request type %d", __func__, ci->type); 2248 break; 2249 } 2250 2251 return (0); 2252} 2253 2254static void 2255cfiscsi_target_hold(struct cfiscsi_target *ct) 2256{ 2257 2258 refcount_acquire(&ct->ct_refcount); 2259} 2260 2261static void 2262cfiscsi_target_release(struct cfiscsi_target *ct) 2263{ 2264 struct cfiscsi_softc *softc; 2265 2266 softc = ct->ct_softc; 2267 mtx_lock(&softc->lock); 2268 if (refcount_release(&ct->ct_refcount)) { 2269 TAILQ_REMOVE(&softc->targets, ct, ct_next); 2270 mtx_unlock(&softc->lock); 2271 if (ct->ct_state != CFISCSI_TARGET_STATE_INVALID) { 2272 ct->ct_state = CFISCSI_TARGET_STATE_INVALID; 2273 if (ctl_port_deregister(&ct->ct_port) != 0) 2274 printf("%s: ctl_port_deregister() failed\n", 2275 __func__); 2276 } 2277 free(ct, M_CFISCSI); 2278 2279 return; 2280 } 2281 mtx_unlock(&softc->lock); 2282} 2283 2284static struct cfiscsi_target * 2285cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name, uint16_t tag) 2286{ 2287 struct cfiscsi_target *ct; 2288 2289 mtx_lock(&softc->lock); 2290 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2291 if (ct->ct_tag != tag || 2292 strcmp(name, ct->ct_name) != 0 || 2293 ct->ct_state != CFISCSI_TARGET_STATE_ACTIVE) 2294 continue; 2295 cfiscsi_target_hold(ct); 2296 mtx_unlock(&softc->lock); 2297 return (ct); 2298 } 2299 mtx_unlock(&softc->lock); 2300 2301 return (NULL); 2302} 2303 2304static struct cfiscsi_target * 2305cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name, 2306 const char *alias, uint16_t tag) 2307{ 2308 struct cfiscsi_target *ct, *newct; 2309 2310 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN) 2311 return (NULL); 2312 2313 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO); 2314 2315 mtx_lock(&softc->lock); 2316 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2317 if (ct->ct_tag != tag || 2318 strcmp(name, ct->ct_name) != 0 || 2319 ct->ct_state == CFISCSI_TARGET_STATE_INVALID) 2320 continue; 2321 cfiscsi_target_hold(ct); 2322 mtx_unlock(&softc->lock); 2323 free(newct, M_CFISCSI); 2324 return (ct); 2325 } 2326 2327 strlcpy(newct->ct_name, name, sizeof(newct->ct_name)); 2328 if (alias != NULL) 2329 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias)); 2330 newct->ct_tag = tag; 2331 refcount_init(&newct->ct_refcount, 1); 2332 newct->ct_softc = softc; 2333 if (TAILQ_EMPTY(&softc->targets)) 2334 softc->last_target_id = 0; 2335 newct->ct_target_id = ++softc->last_target_id; 2336 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next); 2337 mtx_unlock(&softc->lock); 2338 2339 return (newct); 2340} 2341 2342static void 2343cfiscsi_datamove_in(union ctl_io *io) 2344{ 2345 struct cfiscsi_session *cs; 2346 struct icl_pdu *request, *response; 2347 const struct iscsi_bhs_scsi_command *bhssc; 2348 struct iscsi_bhs_data_in *bhsdi; 2349 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 2350 size_t len, expected_len, sg_len, buffer_offset; 2351 const char *sg_addr; 2352 int ctl_sg_count, error, i; 2353 2354 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2355 cs = PDU_SESSION(request); 2356 2357 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2358 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2359 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2360 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2361 2362 if (io->scsiio.kern_sg_entries > 0) { 2363 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 2364 ctl_sg_count = io->scsiio.kern_sg_entries; 2365 } else { 2366 ctl_sglist = &ctl_sg_entry; 2367 ctl_sglist->addr = io->scsiio.kern_data_ptr; 2368 ctl_sglist->len = io->scsiio.kern_data_len; 2369 ctl_sg_count = 1; 2370 } 2371 2372 /* 2373 * This is the total amount of data to be transferred within the current 2374 * SCSI command. We need to record it so that we can properly report 2375 * underflow/underflow. 2376 */ 2377 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2378 2379 /* 2380 * This is the offset within the current SCSI command; for the first 2381 * call to cfiscsi_datamove() it will be 0, and for subsequent ones 2382 * it will be the sum of lengths of previous ones. 2383 */ 2384 buffer_offset = io->scsiio.kern_rel_offset; 2385 2386 /* 2387 * This is the transfer length expected by the initiator. In theory, 2388 * it could be different from the correct amount of data from the SCSI 2389 * point of view, even if that doesn't make any sense. 2390 */ 2391 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length); 2392#if 0 2393 if (expected_len != io->scsiio.kern_total_len) { 2394 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, " 2395 "actual length %zd", expected_len, 2396 (size_t)io->scsiio.kern_total_len); 2397 } 2398#endif 2399 2400 if (buffer_offset >= expected_len) { 2401#if 0 2402 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, " 2403 "already sent the expected len", buffer_offset); 2404#endif 2405 io->scsiio.be_move_done(io); 2406 return; 2407 } 2408 2409 i = 0; 2410 sg_addr = NULL; 2411 sg_len = 0; 2412 response = NULL; 2413 bhsdi = NULL; 2414 for (;;) { 2415 if (response == NULL) { 2416 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2417 if (response == NULL) { 2418 CFISCSI_SESSION_WARN(cs, "failed to " 2419 "allocate memory; dropping connection"); 2420 ctl_set_busy(&io->scsiio); 2421 io->scsiio.be_move_done(io); 2422 cfiscsi_session_terminate(cs); 2423 return; 2424 } 2425 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs; 2426 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN; 2427 bhsdi->bhsdi_initiator_task_tag = 2428 bhssc->bhssc_initiator_task_tag; 2429 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request)); 2430 PDU_EXPDATASN(request)++; 2431 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset); 2432 } 2433 2434 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count")); 2435 if (sg_len == 0) { 2436 sg_addr = ctl_sglist[i].addr; 2437 sg_len = ctl_sglist[i].len; 2438 KASSERT(sg_len > 0, ("sg_len <= 0")); 2439 } 2440 2441 len = sg_len; 2442 2443 /* 2444 * Truncate to maximum data segment length. 2445 */ 2446 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length, 2447 ("ip_data_len %zd >= max_data_segment_length %zd", 2448 response->ip_data_len, cs->cs_max_data_segment_length)); 2449 if (response->ip_data_len + len > 2450 cs->cs_max_data_segment_length) { 2451 len = cs->cs_max_data_segment_length - 2452 response->ip_data_len; 2453 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2454 len, sg_len)); 2455 } 2456 2457 /* 2458 * Truncate to expected data transfer length. 2459 */ 2460 KASSERT(buffer_offset + response->ip_data_len < expected_len, 2461 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd", 2462 buffer_offset, response->ip_data_len, expected_len)); 2463 if (buffer_offset + response->ip_data_len + len > expected_len) { 2464 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd " 2465 "to expected data transfer length %zd", 2466 buffer_offset + response->ip_data_len + len, expected_len); 2467 len = expected_len - (buffer_offset + response->ip_data_len); 2468 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2469 len, sg_len)); 2470 } 2471 2472 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT); 2473 if (error != 0) { 2474 CFISCSI_SESSION_WARN(cs, "failed to " 2475 "allocate memory; dropping connection"); 2476 icl_pdu_free(response); 2477 ctl_set_busy(&io->scsiio); 2478 io->scsiio.be_move_done(io); 2479 cfiscsi_session_terminate(cs); 2480 return; 2481 } 2482 sg_addr += len; 2483 sg_len -= len; 2484 2485 KASSERT(buffer_offset + response->ip_data_len <= expected_len, 2486 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd", 2487 buffer_offset, response->ip_data_len, expected_len)); 2488 if (buffer_offset + response->ip_data_len == expected_len) { 2489 /* 2490 * Already have the amount of data the initiator wanted. 2491 */ 2492 break; 2493 } 2494 2495 if (sg_len == 0) { 2496 /* 2497 * End of scatter-gather segment; 2498 * proceed to the next one... 2499 */ 2500 if (i == ctl_sg_count - 1) { 2501 /* 2502 * ... unless this was the last one. 2503 */ 2504 break; 2505 } 2506 i++; 2507 } 2508 2509 if (response->ip_data_len == cs->cs_max_data_segment_length) { 2510 /* 2511 * Can't stuff more data into the current PDU; 2512 * queue it. Note that's not enough to check 2513 * for kern_data_resid == 0 instead; there 2514 * may be several Data-In PDUs for the final 2515 * call to cfiscsi_datamove(), and we want 2516 * to set the F flag only on the last of them. 2517 */ 2518 buffer_offset += response->ip_data_len; 2519 if (buffer_offset == io->scsiio.kern_total_len || 2520 buffer_offset == expected_len) { 2521 buffer_offset -= response->ip_data_len; 2522 break; 2523 } 2524 cfiscsi_pdu_queue(response); 2525 response = NULL; 2526 bhsdi = NULL; 2527 } 2528 } 2529 if (response != NULL) { 2530 buffer_offset += response->ip_data_len; 2531 if (buffer_offset == io->scsiio.kern_total_len || 2532 buffer_offset == expected_len) { 2533 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F; 2534 if (io->io_hdr.status == CTL_SUCCESS) { 2535 bhsdi->bhsdi_flags |= BHSDI_FLAGS_S; 2536 if (PDU_TOTAL_TRANSFER_LEN(request) < 2537 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2538 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW; 2539 bhsdi->bhsdi_residual_count = 2540 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) - 2541 PDU_TOTAL_TRANSFER_LEN(request)); 2542 } else if (PDU_TOTAL_TRANSFER_LEN(request) > 2543 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2544 bhsdi->bhsdi_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW; 2545 bhsdi->bhsdi_residual_count = 2546 htonl(PDU_TOTAL_TRANSFER_LEN(request) - 2547 ntohl(bhssc->bhssc_expected_data_transfer_length)); 2548 } 2549 bhsdi->bhsdi_status = io->scsiio.scsi_status; 2550 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 2551 } 2552 } 2553 KASSERT(response->ip_data_len > 0, ("sending empty Data-In")); 2554 cfiscsi_pdu_queue(response); 2555 } 2556 2557 io->scsiio.be_move_done(io); 2558} 2559 2560static void 2561cfiscsi_datamove_out(union ctl_io *io) 2562{ 2563 struct cfiscsi_session *cs; 2564 struct icl_pdu *request, *response; 2565 const struct iscsi_bhs_scsi_command *bhssc; 2566 struct iscsi_bhs_r2t *bhsr2t; 2567 struct cfiscsi_data_wait *cdw; 2568 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 2569 uint32_t expected_len, r2t_off, r2t_len; 2570 uint32_t target_transfer_tag; 2571 bool done; 2572 2573 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2574 cs = PDU_SESSION(request); 2575 2576 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2577 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2578 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2579 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2580 2581 /* 2582 * We need to record it so that we can properly report 2583 * underflow/underflow. 2584 */ 2585 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2586 2587 /* 2588 * Report write underflow as error since CTL and backends don't 2589 * really support it, and SCSI does not tell how to do it right. 2590 */ 2591 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length); 2592 if (io->scsiio.kern_rel_offset + io->scsiio.kern_data_len > 2593 expected_len) { 2594 io->scsiio.io_hdr.port_status = 43; 2595 io->scsiio.be_move_done(io); 2596 return; 2597 } 2598 2599 target_transfer_tag = 2600 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1); 2601 2602#if 0 2603 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator " 2604 "task tag 0x%x, target transfer tag 0x%x", 2605 bhssc->bhssc_initiator_task_tag, target_transfer_tag); 2606#endif 2607 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO); 2608 if (cdw == NULL) { 2609 CFISCSI_SESSION_WARN(cs, "failed to " 2610 "allocate memory; dropping connection"); 2611 ctl_set_busy(&io->scsiio); 2612 io->scsiio.be_move_done(io); 2613 cfiscsi_session_terminate(cs); 2614 return; 2615 } 2616 cdw->cdw_ctl_io = io; 2617 cdw->cdw_target_transfer_tag = target_transfer_tag; 2618 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2619 cdw->cdw_r2t_end = io->scsiio.kern_data_len; 2620 cdw->cdw_datasn = 0; 2621 2622 /* Set initial data pointer for the CDW respecting ext_data_filled. */ 2623 if (io->scsiio.kern_sg_entries > 0) { 2624 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 2625 } else { 2626 ctl_sglist = &ctl_sg_entry; 2627 ctl_sglist->addr = io->scsiio.kern_data_ptr; 2628 ctl_sglist->len = io->scsiio.kern_data_len; 2629 } 2630 cdw->cdw_sg_index = 0; 2631 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr; 2632 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len; 2633 r2t_off = io->scsiio.ext_data_filled; 2634 while (r2t_off > 0) { 2635 if (r2t_off >= cdw->cdw_sg_len) { 2636 r2t_off -= cdw->cdw_sg_len; 2637 cdw->cdw_sg_index++; 2638 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr; 2639 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len; 2640 continue; 2641 } 2642 cdw->cdw_sg_addr += r2t_off; 2643 cdw->cdw_sg_len -= r2t_off; 2644 r2t_off = 0; 2645 } 2646 2647 if (cs->cs_immediate_data && 2648 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled < 2649 icl_pdu_data_segment_length(request)) { 2650 done = cfiscsi_handle_data_segment(request, cdw); 2651 if (done) { 2652 uma_zfree(cfiscsi_data_wait_zone, cdw); 2653 io->scsiio.be_move_done(io); 2654 return; 2655 } 2656 } 2657 2658 r2t_off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled; 2659 r2t_len = MIN(io->scsiio.kern_data_len - io->scsiio.ext_data_filled, 2660 cs->cs_max_burst_length); 2661 cdw->cdw_r2t_end = io->scsiio.ext_data_filled + r2t_len; 2662 2663 CFISCSI_SESSION_LOCK(cs); 2664 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next); 2665 CFISCSI_SESSION_UNLOCK(cs); 2666 2667 /* 2668 * XXX: We should limit the number of outstanding R2T PDUs 2669 * per task to MaxOutstandingR2T. 2670 */ 2671 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2672 if (response == NULL) { 2673 CFISCSI_SESSION_WARN(cs, "failed to " 2674 "allocate memory; dropping connection"); 2675 ctl_set_busy(&io->scsiio); 2676 io->scsiio.be_move_done(io); 2677 cfiscsi_session_terminate(cs); 2678 return; 2679 } 2680 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 2681 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs; 2682 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T; 2683 bhsr2t->bhsr2t_flags = 0x80; 2684 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun; 2685 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2686 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag; 2687 /* 2688 * XXX: Here we assume that cfiscsi_datamove() won't ever 2689 * be running concurrently on several CPUs for a given 2690 * command. 2691 */ 2692 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request)); 2693 PDU_R2TSN(request)++; 2694 /* 2695 * This is the offset within the current SCSI command; 2696 * i.e. for the first call of datamove(), it will be 0, 2697 * and for subsequent ones it will be the sum of lengths 2698 * of previous ones. 2699 * 2700 * The ext_data_filled is to account for unsolicited 2701 * (immediate) data that might have already arrived. 2702 */ 2703 bhsr2t->bhsr2t_buffer_offset = htonl(r2t_off); 2704 /* 2705 * This is the total length (sum of S/G lengths) this call 2706 * to cfiscsi_datamove() is supposed to handle, limited by 2707 * MaxBurstLength. 2708 */ 2709 bhsr2t->bhsr2t_desired_data_transfer_length = htonl(r2t_len); 2710 cfiscsi_pdu_queue(response); 2711} 2712 2713static void 2714cfiscsi_datamove(union ctl_io *io) 2715{ 2716 2717 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 2718 cfiscsi_datamove_in(io); 2719 else { 2720 /* We hadn't received anything during this datamove yet. */ 2721 io->scsiio.ext_data_filled = 0; 2722 cfiscsi_datamove_out(io); 2723 } 2724} 2725 2726static void 2727cfiscsi_scsi_command_done(union ctl_io *io) 2728{ 2729 struct icl_pdu *request, *response; 2730 struct iscsi_bhs_scsi_command *bhssc; 2731 struct iscsi_bhs_scsi_response *bhssr; 2732#ifdef DIAGNOSTIC 2733 struct cfiscsi_data_wait *cdw; 2734#endif 2735 struct cfiscsi_session *cs; 2736 uint16_t sense_length; 2737 2738 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2739 cs = PDU_SESSION(request); 2740 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 2741 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2742 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2743 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode)); 2744 2745 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x", 2746 // bhssc->bhssc_initiator_task_tag); 2747 2748#ifdef DIAGNOSTIC 2749 CFISCSI_SESSION_LOCK(cs); 2750 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) 2751 KASSERT(bhssc->bhssc_initiator_task_tag != 2752 cdw->cdw_initiator_task_tag, ("dangling cdw")); 2753 CFISCSI_SESSION_UNLOCK(cs); 2754#endif 2755 2756 /* 2757 * Do not return status for aborted commands. 2758 * There are exceptions, but none supported by CTL yet. 2759 */ 2760 if (((io->io_hdr.flags & CTL_FLAG_ABORT) && 2761 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) || 2762 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) { 2763 ctl_free_io(io); 2764 icl_pdu_free(request); 2765 return; 2766 } 2767 2768 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2769 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 2770 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE; 2771 bhssr->bhssr_flags = 0x80; 2772 /* 2773 * XXX: We don't deal with bidirectional under/overflows; 2774 * does anything actually support those? 2775 */ 2776 if (PDU_TOTAL_TRANSFER_LEN(request) < 2777 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2778 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW; 2779 bhssr->bhssr_residual_count = 2780 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) - 2781 PDU_TOTAL_TRANSFER_LEN(request)); 2782 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d", 2783 // ntohl(bhssr->bhssr_residual_count)); 2784 } else if (PDU_TOTAL_TRANSFER_LEN(request) > 2785 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2786 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW; 2787 bhssr->bhssr_residual_count = 2788 htonl(PDU_TOTAL_TRANSFER_LEN(request) - 2789 ntohl(bhssc->bhssc_expected_data_transfer_length)); 2790 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d", 2791 // ntohl(bhssr->bhssr_residual_count)); 2792 } 2793 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED; 2794 bhssr->bhssr_status = io->scsiio.scsi_status; 2795 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2796 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request)); 2797 2798 if (io->scsiio.sense_len > 0) { 2799#if 0 2800 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data", 2801 io->scsiio.sense_len); 2802#endif 2803 sense_length = htons(io->scsiio.sense_len); 2804 icl_pdu_append_data(response, 2805 &sense_length, sizeof(sense_length), M_WAITOK); 2806 icl_pdu_append_data(response, 2807 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK); 2808 } 2809 2810 ctl_free_io(io); 2811 icl_pdu_free(request); 2812 cfiscsi_pdu_queue(response); 2813} 2814 2815static void 2816cfiscsi_task_management_done(union ctl_io *io) 2817{ 2818 struct icl_pdu *request, *response; 2819 struct iscsi_bhs_task_management_request *bhstmr; 2820 struct iscsi_bhs_task_management_response *bhstmr2; 2821 struct cfiscsi_data_wait *cdw, *tmpcdw; 2822 struct cfiscsi_session *cs, *tcs; 2823 struct cfiscsi_softc *softc; 2824 int cold_reset = 0; 2825 2826 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2827 cs = PDU_SESSION(request); 2828 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 2829 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2830 ISCSI_BHS_OPCODE_TASK_REQUEST, 2831 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode)); 2832 2833#if 0 2834 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x", 2835 bhstmr->bhstmr_initiator_task_tag, 2836 bhstmr->bhstmr_referenced_task_tag); 2837#endif 2838 2839 if ((bhstmr->bhstmr_function & ~0x80) == 2840 BHSTMR_FUNCTION_ABORT_TASK) { 2841 /* 2842 * Make sure we no longer wait for Data-Out for this command. 2843 */ 2844 CFISCSI_SESSION_LOCK(cs); 2845 TAILQ_FOREACH_SAFE(cdw, 2846 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) { 2847 if (bhstmr->bhstmr_referenced_task_tag != 2848 cdw->cdw_initiator_task_tag) 2849 continue; 2850 2851#if 0 2852 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task " 2853 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag); 2854#endif 2855 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, 2856 cdw, cdw_next); 2857 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 2858 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 43; 2859 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io); 2860 uma_zfree(cfiscsi_data_wait_zone, cdw); 2861 } 2862 CFISCSI_SESSION_UNLOCK(cs); 2863 } 2864 if ((bhstmr->bhstmr_function & ~0x80) == 2865 BHSTMR_FUNCTION_TARGET_COLD_RESET && 2866 io->io_hdr.status == CTL_SUCCESS) 2867 cold_reset = 1; 2868 2869 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2870 bhstmr2 = (struct iscsi_bhs_task_management_response *) 2871 response->ip_bhs; 2872 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE; 2873 bhstmr2->bhstmr_flags = 0x80; 2874 switch (io->taskio.task_status) { 2875 case CTL_TASK_FUNCTION_COMPLETE: 2876 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE; 2877 break; 2878 case CTL_TASK_FUNCTION_SUCCEEDED: 2879 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_SUCCEEDED; 2880 break; 2881 case CTL_TASK_LUN_DOES_NOT_EXIST: 2882 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST; 2883 break; 2884 case CTL_TASK_FUNCTION_NOT_SUPPORTED: 2885 default: 2886 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED; 2887 break; 2888 } 2889 memcpy(bhstmr2->bhstmr_additional_reponse_information, 2890 io->taskio.task_resp, sizeof(io->taskio.task_resp)); 2891 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag; 2892 2893 ctl_free_io(io); 2894 icl_pdu_free(request); 2895 cfiscsi_pdu_queue(response); 2896 2897 if (cold_reset) { 2898 softc = cs->cs_target->ct_softc; 2899 mtx_lock(&softc->lock); 2900 TAILQ_FOREACH(tcs, &softc->sessions, cs_next) { 2901 if (tcs->cs_target == cs->cs_target) 2902 cfiscsi_session_terminate(tcs); 2903 } 2904 mtx_unlock(&softc->lock); 2905 } 2906} 2907 2908static void 2909cfiscsi_done(union ctl_io *io) 2910{ 2911 struct icl_pdu *request; 2912 struct cfiscsi_session *cs; 2913 2914 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE), 2915 ("invalid CTL status %#x", io->io_hdr.status)); 2916 2917 if (io->io_hdr.io_type == CTL_IO_TASK && 2918 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) { 2919 /* 2920 * Implicit task termination has just completed; nothing to do. 2921 */ 2922 cs = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2923 cs->cs_tasks_aborted = true; 2924 refcount_release(&cs->cs_outstanding_ctl_pdus); 2925 wakeup(__DEVOLATILE(void *, &cs->cs_outstanding_ctl_pdus)); 2926 ctl_free_io(io); 2927 return; 2928 } 2929 2930 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2931 cs = PDU_SESSION(request); 2932 refcount_release(&cs->cs_outstanding_ctl_pdus); 2933 2934 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) { 2935 case ISCSI_BHS_OPCODE_SCSI_COMMAND: 2936 cfiscsi_scsi_command_done(io); 2937 break; 2938 case ISCSI_BHS_OPCODE_TASK_REQUEST: 2939 cfiscsi_task_management_done(io); 2940 break; 2941 default: 2942 panic("cfiscsi_done called with wrong opcode 0x%x", 2943 request->ip_bhs->bhs_opcode); 2944 } 2945} 2946