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