ctl_frontend_iscsi.c revision 268265
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: head/sys/cam/ctl/ctl_frontend_iscsi.c 268265 2014-07-04 19:19:03Z mav $ 30 */ 31 32/* 33 * CTL frontend for the iSCSI protocol. 34 */ 35 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_frontend_iscsi.c 268265 2014-07-04 19:19:03Z 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_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; 88SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN, 89 &debug, 1, "Enable debug messages"); 90static int ping_timeout = 5; 91SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN, 92 &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds"); 93static int login_timeout = 60; 94SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN, 95 &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds"); 96static int maxcmdsn_delta = 256; 97SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN, 98 &maxcmdsn_delta, 256, "Number of commands the initiator can send " 99 "without confirmation"); 100 101#define CFISCSI_DEBUG(X, ...) \ 102 do { \ 103 if (debug > 1) { \ 104 printf("%s: " X "\n", \ 105 __func__, ## __VA_ARGS__); \ 106 } \ 107 } while (0) 108 109#define CFISCSI_WARN(X, ...) \ 110 do { \ 111 if (debug > 0) { \ 112 printf("WARNING: %s: " X "\n", \ 113 __func__, ## __VA_ARGS__); \ 114 } \ 115 } while (0) 116 117#define CFISCSI_SESSION_DEBUG(S, X, ...) \ 118 do { \ 119 if (debug > 1) { \ 120 printf("%s: %s (%s): " X "\n", \ 121 __func__, S->cs_initiator_addr, \ 122 S->cs_initiator_name, ## __VA_ARGS__); \ 123 } \ 124 } while (0) 125 126#define CFISCSI_SESSION_WARN(S, X, ...) \ 127 do { \ 128 if (debug > 0) { \ 129 printf("WARNING: %s (%s): " X "\n", \ 130 S->cs_initiator_addr, \ 131 S->cs_initiator_name, ## __VA_ARGS__); \ 132 } \ 133 } while (0) 134 135#define CFISCSI_SESSION_LOCK(X) mtx_lock(&X->cs_lock) 136#define CFISCSI_SESSION_UNLOCK(X) mtx_unlock(&X->cs_lock) 137#define CFISCSI_SESSION_LOCK_ASSERT(X) mtx_assert(&X->cs_lock, MA_OWNED) 138 139#define CONN_SESSION(X) ((struct cfiscsi_session *)(X)->ic_prv0) 140#define PDU_SESSION(X) CONN_SESSION((X)->ip_conn) 141#define PDU_EXPDATASN(X) (X)->ip_prv0 142#define PDU_TOTAL_TRANSFER_LEN(X) (X)->ip_prv1 143#define PDU_R2TSN(X) (X)->ip_prv2 144 145int cfiscsi_init(void); 146static void cfiscsi_online(void *arg); 147static void cfiscsi_offline(void *arg); 148static int cfiscsi_lun_enable(void *arg, 149 struct ctl_id target_id, int lun_id); 150static int cfiscsi_lun_disable(void *arg, 151 struct ctl_id target_id, int lun_id); 152static int cfiscsi_ioctl(struct cdev *dev, 153 u_long cmd, caddr_t addr, int flag, struct thread *td); 154static int cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len); 155static void cfiscsi_datamove(union ctl_io *io); 156static void cfiscsi_done(union ctl_io *io); 157static uint32_t cfiscsi_map_lun(void *arg, uint32_t lun); 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); 167static void cfiscsi_target_release(struct cfiscsi_target *ct); 168static void cfiscsi_session_delete(struct cfiscsi_session *cs); 169 170static struct cfiscsi_softc cfiscsi_softc; 171extern struct ctl_softc *control_softc; 172 173static int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *); 174 175static moduledata_t cfiscsi_moduledata = { 176 "ctlcfiscsi", 177 cfiscsi_module_event_handler, 178 NULL 179}; 180 181DECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); 182MODULE_VERSION(ctlcfiscsi, 1); 183MODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1); 184MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1); 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_softc->fe.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_softc->fe.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_softc->fe.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_softc->fe.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_softc->fe.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_softc->fe.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_softc->fe.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 = 1082 cs->cs_target->ct_softc->fe.targ_port; 1083 io->io_hdr.nexus.targ_target.id = 0; 1084 //io->io_hdr.nexus.targ_lun = lun; /* Not needed? */ 1085 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */ 1086 io->taskio.task_action = CTL_TASK_ABORT_TASK; 1087 io->taskio.tag_num = cdw->cdw_initiator_task_tag; 1088 error = ctl_queue(io); 1089 if (error != CTL_RETVAL_COMPLETE) { 1090 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error); 1091 ctl_free_io(io); 1092 return; 1093 } 1094#if 0 1095 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag " 1096 "0x%x", cdw->cdw_initiator_task_tag); 1097#endif 1098 /* 1099 * Set nonzero port status; this prevents backends from 1100 * assuming that the data transfer actually succeeded 1101 * and writing uninitialized data to disk. 1102 */ 1103 cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42; 1104 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io); 1105 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next); 1106 uma_zfree(cfiscsi_data_wait_zone, cdw); 1107 } 1108 CFISCSI_SESSION_UNLOCK(cs); 1109#endif 1110 1111 /* 1112 * Wait for CTL to terminate all the tasks. 1113 */ 1114 for (;;) { 1115 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 1116 last = refcount_release(&cs->cs_outstanding_ctl_pdus); 1117 if (last != 0) 1118 break; 1119 CFISCSI_SESSION_WARN(cs, "waiting for CTL to terminate tasks, " 1120 "%d remaining", cs->cs_outstanding_ctl_pdus); 1121 pause("cfiscsi_terminate", 1); 1122 } 1123} 1124 1125static void 1126cfiscsi_maintenance_thread(void *arg) 1127{ 1128 struct cfiscsi_session *cs; 1129 1130 cs = arg; 1131 1132 for (;;) { 1133 CFISCSI_SESSION_LOCK(cs); 1134 if (cs->cs_terminating == false) 1135 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock); 1136 CFISCSI_SESSION_UNLOCK(cs); 1137 1138 if (cs->cs_terminating) { 1139 1140 /* 1141 * We used to wait up to 30 seconds to deliver queued 1142 * PDUs to the initiator. We also tried hard to deliver 1143 * SCSI Responses for the aborted PDUs. We don't do 1144 * that anymore. We might need to revisit that. 1145 */ 1146 callout_drain(&cs->cs_callout); 1147 icl_conn_shutdown(cs->cs_conn); 1148 icl_conn_close(cs->cs_conn); 1149 1150 /* 1151 * At this point ICL receive thread is no longer 1152 * running; no new tasks can be queued. 1153 */ 1154 cfiscsi_session_terminate_tasks(cs); 1155 cfiscsi_session_delete(cs); 1156 kthread_exit(); 1157 return; 1158 } 1159 CFISCSI_SESSION_DEBUG(cs, "nothing to do"); 1160 } 1161} 1162 1163static void 1164cfiscsi_session_terminate(struct cfiscsi_session *cs) 1165{ 1166 1167 if (cs->cs_terminating) 1168 return; 1169 cs->cs_terminating = true; 1170 cv_signal(&cs->cs_maintenance_cv); 1171#ifdef ICL_KERNEL_PROXY 1172 cv_signal(&cs->cs_login_cv); 1173#endif 1174} 1175 1176static int 1177cfiscsi_session_register_initiator(struct cfiscsi_session *cs) 1178{ 1179 int error, i; 1180 struct cfiscsi_softc *softc; 1181 1182 KASSERT(cs->cs_ctl_initid == -1, ("already registered")); 1183 1184 softc = &cfiscsi_softc; 1185 1186 mtx_lock(&softc->lock); 1187 for (i = 0; i < softc->max_initiators; i++) { 1188 if (softc->ctl_initids[i] == 0) 1189 break; 1190 } 1191 if (i == softc->max_initiators) { 1192 CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)", 1193 softc->max_initiators); 1194 mtx_unlock(&softc->lock); 1195 return (1); 1196 } 1197 softc->ctl_initids[i] = 1; 1198 mtx_unlock(&softc->lock); 1199 1200#if 0 1201 CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d", 1202 i, softc->max_initiators); 1203#endif 1204 cs->cs_ctl_initid = i; 1205 error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid); 1206 if (error != 0) { 1207 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error); 1208 mtx_lock(&softc->lock); 1209 softc->ctl_initids[cs->cs_ctl_initid] = 0; 1210 mtx_unlock(&softc->lock); 1211 cs->cs_ctl_initid = -1; 1212 return (1); 1213 } 1214 1215 return (0); 1216} 1217 1218static void 1219cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs) 1220{ 1221 int error; 1222 struct cfiscsi_softc *softc; 1223 1224 if (cs->cs_ctl_initid == -1) 1225 return; 1226 1227 softc = &cfiscsi_softc; 1228 1229 error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid); 1230 if (error != 0) { 1231 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d", 1232 error); 1233 } 1234 mtx_lock(&softc->lock); 1235 softc->ctl_initids[cs->cs_ctl_initid] = 0; 1236 mtx_unlock(&softc->lock); 1237 cs->cs_ctl_initid = -1; 1238} 1239 1240static struct cfiscsi_session * 1241cfiscsi_session_new(struct cfiscsi_softc *softc) 1242{ 1243 struct cfiscsi_session *cs; 1244 int error; 1245 1246 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO); 1247 if (cs == NULL) { 1248 CFISCSI_WARN("malloc failed"); 1249 return (NULL); 1250 } 1251 cs->cs_ctl_initid = -1; 1252 1253 refcount_init(&cs->cs_outstanding_ctl_pdus, 0); 1254 TAILQ_INIT(&cs->cs_waiting_for_data_out); 1255 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF); 1256 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt"); 1257#ifdef ICL_KERNEL_PROXY 1258 cv_init(&cs->cs_login_cv, "cfiscsi_login"); 1259#endif 1260 1261 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock); 1262 cs->cs_conn->ic_receive = cfiscsi_receive_callback; 1263 cs->cs_conn->ic_error = cfiscsi_error_callback; 1264 cs->cs_conn->ic_prv0 = cs; 1265 1266 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt"); 1267 if (error != 0) { 1268 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error); 1269 free(cs, M_CFISCSI); 1270 return (NULL); 1271 } 1272 1273 mtx_lock(&softc->lock); 1274 cs->cs_id = softc->last_session_id + 1; 1275 softc->last_session_id++; 1276 mtx_unlock(&softc->lock); 1277 1278 mtx_lock(&softc->lock); 1279 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next); 1280 mtx_unlock(&softc->lock); 1281 1282 /* 1283 * Start pinging the initiator. 1284 */ 1285 callout_init(&cs->cs_callout, 1); 1286 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs); 1287 1288 return (cs); 1289} 1290 1291static void 1292cfiscsi_session_delete(struct cfiscsi_session *cs) 1293{ 1294 struct cfiscsi_softc *softc; 1295 1296 softc = &cfiscsi_softc; 1297 1298 KASSERT(cs->cs_outstanding_ctl_pdus == 0, 1299 ("destroying session with outstanding CTL pdus")); 1300 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out), 1301 ("destroying session with non-empty queue")); 1302 1303 cfiscsi_session_unregister_initiator(cs); 1304 if (cs->cs_target != NULL) 1305 cfiscsi_target_release(cs->cs_target); 1306 icl_conn_close(cs->cs_conn); 1307 icl_conn_free(cs->cs_conn); 1308 1309 mtx_lock(&softc->lock); 1310 TAILQ_REMOVE(&softc->sessions, cs, cs_next); 1311 mtx_unlock(&softc->lock); 1312 1313 free(cs, M_CFISCSI); 1314} 1315 1316int 1317cfiscsi_init(void) 1318{ 1319 struct cfiscsi_softc *softc; 1320 struct ctl_frontend *fe; 1321 int retval; 1322 1323 softc = &cfiscsi_softc; 1324 retval = 0; 1325 bzero(softc, sizeof(*softc)); 1326 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF); 1327 1328#ifdef ICL_KERNEL_PROXY 1329 cv_init(&softc->accept_cv, "cfiscsi_accept"); 1330#endif 1331 TAILQ_INIT(&softc->sessions); 1332 TAILQ_INIT(&softc->targets); 1333 1334 fe = &softc->fe; 1335 fe->port_type = CTL_PORT_ISCSI; 1336 /* XXX KDM what should the real number be here? */ 1337 fe->num_requested_ctl_io = 4096; 1338 snprintf(softc->port_name, sizeof(softc->port_name), "iscsi"); 1339 fe->port_name = softc->port_name; 1340 fe->port_online = cfiscsi_online; 1341 fe->port_offline = cfiscsi_offline; 1342 fe->onoff_arg = softc; 1343 fe->lun_enable = cfiscsi_lun_enable; 1344 fe->lun_disable = cfiscsi_lun_disable; 1345 fe->targ_lun_arg = softc; 1346 fe->ioctl = cfiscsi_ioctl; 1347 fe->devid = cfiscsi_devid; 1348 fe->fe_datamove = cfiscsi_datamove; 1349 fe->fe_done = cfiscsi_done; 1350 1351 /* XXX KDM what should we report here? */ 1352 /* XXX These should probably be fetched from CTL. */ 1353 fe->max_targets = 1; 1354 fe->max_target_id = 15; 1355 1356 retval = ctl_frontend_register(fe, /*master_SC*/ 1); 1357 if (retval != 0) { 1358 CFISCSI_WARN("ctl_frontend_register() failed with error %d", 1359 retval); 1360 retval = 1; 1361 goto bailout; 1362 } 1363 1364 softc->max_initiators = fe->max_initiators; 1365 1366 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait", 1367 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL, 1368 UMA_ALIGN_PTR, 0); 1369 1370 return (0); 1371 1372bailout: 1373 return (retval); 1374} 1375 1376static int 1377cfiscsi_module_event_handler(module_t mod, int what, void *arg) 1378{ 1379 1380 switch (what) { 1381 case MOD_LOAD: 1382 return (cfiscsi_init()); 1383 case MOD_UNLOAD: 1384 return (EBUSY); 1385 default: 1386 return (EOPNOTSUPP); 1387 } 1388} 1389 1390#ifdef ICL_KERNEL_PROXY 1391static void 1392cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id) 1393{ 1394 struct cfiscsi_session *cs; 1395 1396 cs = cfiscsi_session_new(&cfiscsi_softc); 1397 if (cs == NULL) { 1398 CFISCSI_WARN("failed to create session"); 1399 return; 1400 } 1401 1402 icl_conn_handoff_sock(cs->cs_conn, so); 1403 cs->cs_initiator_sa = sa; 1404 cs->cs_portal_id = portal_id; 1405 cs->cs_waiting_for_ctld = true; 1406 cv_signal(&cfiscsi_softc.accept_cv); 1407} 1408#endif 1409 1410static void 1411cfiscsi_online(void *arg) 1412{ 1413 struct cfiscsi_softc *softc; 1414 1415 softc = (struct cfiscsi_softc *)arg; 1416 1417 softc->online = 1; 1418#ifdef ICL_KERNEL_PROXY 1419 if (softc->listener != NULL) 1420 icl_listen_free(softc->listener); 1421 softc->listener = icl_listen_new(cfiscsi_accept); 1422#endif 1423} 1424 1425static void 1426cfiscsi_offline(void *arg) 1427{ 1428 struct cfiscsi_softc *softc; 1429 struct cfiscsi_session *cs; 1430 1431 softc = (struct cfiscsi_softc *)arg; 1432 1433 softc->online = 0; 1434 1435 mtx_lock(&softc->lock); 1436 TAILQ_FOREACH(cs, &softc->sessions, cs_next) 1437 cfiscsi_session_terminate(cs); 1438 mtx_unlock(&softc->lock); 1439 1440#ifdef ICL_KERNEL_PROXY 1441 icl_listen_free(softc->listener); 1442 softc->listener = NULL; 1443#endif 1444} 1445 1446static void 1447cfiscsi_ioctl_handoff(struct ctl_iscsi *ci) 1448{ 1449 struct cfiscsi_softc *softc; 1450 struct cfiscsi_session *cs; 1451 struct cfiscsi_target *ct; 1452 struct ctl_iscsi_handoff_params *cihp; 1453 int error; 1454 1455 cihp = (struct ctl_iscsi_handoff_params *)&(ci->data); 1456 softc = &cfiscsi_softc; 1457 1458 CFISCSI_DEBUG("new connection from %s (%s) to %s", 1459 cihp->initiator_name, cihp->initiator_addr, 1460 cihp->target_name); 1461 1462 if (softc->online == 0) { 1463 ci->status = CTL_ISCSI_ERROR; 1464 snprintf(ci->error_str, sizeof(ci->error_str), 1465 "%s: port offline", __func__); 1466 return; 1467 } 1468 1469 ct = cfiscsi_target_find(softc, cihp->target_name); 1470 if (ct == NULL) { 1471 ci->status = CTL_ISCSI_ERROR; 1472 snprintf(ci->error_str, sizeof(ci->error_str), 1473 "%s: target not found", __func__); 1474 return; 1475 } 1476 1477#ifdef ICL_KERNEL_PROXY 1478 if (cihp->socket > 0 && cihp->connection_id > 0) { 1479 snprintf(ci->error_str, sizeof(ci->error_str), 1480 "both socket and connection_id set"); 1481 ci->status = CTL_ISCSI_ERROR; 1482 cfiscsi_target_release(ct); 1483 return; 1484 } 1485 if (cihp->socket == 0) { 1486 mtx_lock(&cfiscsi_softc.lock); 1487 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1488 if (cs->cs_id == cihp->socket) 1489 break; 1490 } 1491 if (cs == NULL) { 1492 mtx_unlock(&cfiscsi_softc.lock); 1493 snprintf(ci->error_str, sizeof(ci->error_str), 1494 "connection not found"); 1495 ci->status = CTL_ISCSI_ERROR; 1496 cfiscsi_target_release(ct); 1497 return; 1498 } 1499 mtx_unlock(&cfiscsi_softc.lock); 1500 } else { 1501#endif 1502 cs = cfiscsi_session_new(softc); 1503 if (cs == NULL) { 1504 ci->status = CTL_ISCSI_ERROR; 1505 snprintf(ci->error_str, sizeof(ci->error_str), 1506 "%s: cfiscsi_session_new failed", __func__); 1507 cfiscsi_target_release(ct); 1508 return; 1509 } 1510#ifdef ICL_KERNEL_PROXY 1511 } 1512#endif 1513 cs->cs_target = ct; 1514 1515 /* 1516 * First PDU of Full Feature phase has the same CmdSN as the last 1517 * PDU from the Login Phase received from the initiator. Thus, 1518 * the -1 below. 1519 */ 1520 cs->cs_portal_group_tag = cihp->portal_group_tag; 1521 cs->cs_cmdsn = cihp->cmdsn; 1522 cs->cs_statsn = cihp->statsn; 1523 cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length; 1524 cs->cs_max_burst_length = cihp->max_burst_length; 1525 cs->cs_immediate_data = !!cihp->immediate_data; 1526 if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C) 1527 cs->cs_conn->ic_header_crc32c = true; 1528 if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C) 1529 cs->cs_conn->ic_data_crc32c = true; 1530 1531 strlcpy(cs->cs_initiator_name, 1532 cihp->initiator_name, sizeof(cs->cs_initiator_name)); 1533 strlcpy(cs->cs_initiator_addr, 1534 cihp->initiator_addr, sizeof(cs->cs_initiator_addr)); 1535 strlcpy(cs->cs_initiator_alias, 1536 cihp->initiator_alias, sizeof(cs->cs_initiator_alias)); 1537 1538#ifdef ICL_KERNEL_PROXY 1539 if (cihp->socket > 0) { 1540#endif 1541 error = icl_conn_handoff(cs->cs_conn, cihp->socket); 1542 if (error != 0) { 1543 cfiscsi_session_delete(cs); 1544 ci->status = CTL_ISCSI_ERROR; 1545 snprintf(ci->error_str, sizeof(ci->error_str), 1546 "%s: icl_conn_handoff failed with error %d", 1547 __func__, error); 1548 return; 1549 } 1550#ifdef ICL_KERNEL_PROXY 1551 } 1552#endif 1553 1554 /* 1555 * Register initiator with CTL. 1556 */ 1557 cfiscsi_session_register_initiator(cs); 1558 1559#ifdef ICL_KERNEL_PROXY 1560 cs->cs_login_phase = false; 1561 1562 /* 1563 * First PDU of the Full Feature phase has likely already arrived. 1564 * We have to pick it up and execute properly. 1565 */ 1566 if (cs->cs_login_pdu != NULL) { 1567 CFISCSI_SESSION_DEBUG(cs, "picking up first PDU"); 1568 cfiscsi_pdu_handle(cs->cs_login_pdu); 1569 cs->cs_login_pdu = NULL; 1570 } 1571#endif 1572 1573 ci->status = CTL_ISCSI_OK; 1574} 1575 1576static void 1577cfiscsi_ioctl_list(struct ctl_iscsi *ci) 1578{ 1579 struct ctl_iscsi_list_params *cilp; 1580 struct cfiscsi_session *cs; 1581 struct cfiscsi_softc *softc; 1582 struct sbuf *sb; 1583 int error; 1584 1585 cilp = (struct ctl_iscsi_list_params *)&(ci->data); 1586 softc = &cfiscsi_softc; 1587 1588 sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN); 1589 if (sb == NULL) { 1590 ci->status = CTL_ISCSI_ERROR; 1591 snprintf(ci->error_str, sizeof(ci->error_str), 1592 "Unable to allocate %d bytes for iSCSI session list", 1593 cilp->alloc_len); 1594 return; 1595 } 1596 1597 sbuf_printf(sb, "<ctlislist>\n"); 1598 mtx_lock(&softc->lock); 1599 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1600#ifdef ICL_KERNEL_PROXY 1601 if (cs->cs_target == NULL) 1602 continue; 1603#endif 1604 error = sbuf_printf(sb, "<connection id=\"%d\">" 1605 "<initiator>%s</initiator>" 1606 "<initiator_addr>%s</initiator_addr>" 1607 "<initiator_alias>%s</initiator_alias>" 1608 "<target>%s</target>" 1609 "<target_alias>%s</target_alias>" 1610 "<header_digest>%s</header_digest>" 1611 "<data_digest>%s</data_digest>" 1612 "<max_data_segment_length>%zd</max_data_segment_length>" 1613 "<immediate_data>%d</immediate_data>" 1614 "<iser>%d</iser>" 1615 "</connection>\n", 1616 cs->cs_id, 1617 cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias, 1618 cs->cs_target->ct_name, cs->cs_target->ct_alias, 1619 cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None", 1620 cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None", 1621 cs->cs_max_data_segment_length, 1622 cs->cs_immediate_data, 1623 cs->cs_conn->ic_iser); 1624 if (error != 0) 1625 break; 1626 } 1627 mtx_unlock(&softc->lock); 1628 error = sbuf_printf(sb, "</ctlislist>\n"); 1629 if (error != 0) { 1630 sbuf_delete(sb); 1631 ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE; 1632 snprintf(ci->error_str, sizeof(ci->error_str), 1633 "Out of space, %d bytes is too small", cilp->alloc_len); 1634 return; 1635 } 1636 sbuf_finish(sb); 1637 1638 error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1); 1639 cilp->fill_len = sbuf_len(sb) + 1; 1640 ci->status = CTL_ISCSI_OK; 1641 sbuf_delete(sb); 1642} 1643 1644static void 1645cfiscsi_ioctl_terminate(struct ctl_iscsi *ci) 1646{ 1647 struct icl_pdu *response; 1648 struct iscsi_bhs_asynchronous_message *bhsam; 1649 struct ctl_iscsi_terminate_params *citp; 1650 struct cfiscsi_session *cs; 1651 struct cfiscsi_softc *softc; 1652 int found = 0; 1653 1654 citp = (struct ctl_iscsi_terminate_params *)&(ci->data); 1655 softc = &cfiscsi_softc; 1656 1657 mtx_lock(&softc->lock); 1658 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1659 if (citp->all == 0 && cs->cs_id != citp->connection_id && 1660 strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 && 1661 strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0) 1662 continue; 1663 1664 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT); 1665 if (response == NULL) { 1666 /* 1667 * Oh well. Just terminate the connection. 1668 */ 1669 } else { 1670 bhsam = (struct iscsi_bhs_asynchronous_message *) 1671 response->ip_bhs; 1672 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE; 1673 bhsam->bhsam_flags = 0x80; 1674 bhsam->bhsam_0xffffffff = 0xffffffff; 1675 bhsam->bhsam_async_event = 1676 BHSAM_EVENT_TARGET_TERMINATES_SESSION; 1677 cfiscsi_pdu_queue(response); 1678 } 1679 cfiscsi_session_terminate(cs); 1680 found++; 1681 } 1682 mtx_unlock(&softc->lock); 1683 1684 if (found == 0) { 1685 ci->status = CTL_ISCSI_SESSION_NOT_FOUND; 1686 snprintf(ci->error_str, sizeof(ci->error_str), 1687 "No matching connections found"); 1688 return; 1689 } 1690 1691 ci->status = CTL_ISCSI_OK; 1692} 1693 1694static void 1695cfiscsi_ioctl_logout(struct ctl_iscsi *ci) 1696{ 1697 struct icl_pdu *response; 1698 struct iscsi_bhs_asynchronous_message *bhsam; 1699 struct ctl_iscsi_logout_params *cilp; 1700 struct cfiscsi_session *cs; 1701 struct cfiscsi_softc *softc; 1702 int found = 0; 1703 1704 cilp = (struct ctl_iscsi_logout_params *)&(ci->data); 1705 softc = &cfiscsi_softc; 1706 1707 mtx_lock(&softc->lock); 1708 TAILQ_FOREACH(cs, &softc->sessions, cs_next) { 1709 if (cilp->all == 0 && cs->cs_id != cilp->connection_id && 1710 strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 && 1711 strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0) 1712 continue; 1713 1714 response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT); 1715 if (response == NULL) { 1716 ci->status = CTL_ISCSI_ERROR; 1717 snprintf(ci->error_str, sizeof(ci->error_str), 1718 "Unable to allocate memory"); 1719 mtx_unlock(&softc->lock); 1720 return; 1721 } 1722 bhsam = 1723 (struct iscsi_bhs_asynchronous_message *)response->ip_bhs; 1724 bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE; 1725 bhsam->bhsam_flags = 0x80; 1726 bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT; 1727 bhsam->bhsam_parameter3 = htons(10); 1728 cfiscsi_pdu_queue(response); 1729 found++; 1730 } 1731 mtx_unlock(&softc->lock); 1732 1733 if (found == 0) { 1734 ci->status = CTL_ISCSI_SESSION_NOT_FOUND; 1735 snprintf(ci->error_str, sizeof(ci->error_str), 1736 "No matching connections found"); 1737 return; 1738 } 1739 1740 ci->status = CTL_ISCSI_OK; 1741} 1742 1743#ifdef ICL_KERNEL_PROXY 1744static void 1745cfiscsi_ioctl_listen(struct ctl_iscsi *ci) 1746{ 1747 struct ctl_iscsi_listen_params *cilp; 1748 struct sockaddr *sa; 1749 int error; 1750 1751 cilp = (struct ctl_iscsi_listen_params *)&(ci->data); 1752 1753 if (cfiscsi_softc.listener == NULL) { 1754 CFISCSI_DEBUG("no listener"); 1755 snprintf(ci->error_str, sizeof(ci->error_str), "no listener"); 1756 ci->status = CTL_ISCSI_ERROR; 1757 return; 1758 } 1759 1760 error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen); 1761 if (error != 0) { 1762 CFISCSI_DEBUG("getsockaddr, error %d", error); 1763 snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed"); 1764 ci->status = CTL_ISCSI_ERROR; 1765 return; 1766 } 1767 1768 error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain, 1769 cilp->socktype, cilp->protocol, sa, cilp->portal_id); 1770 if (error != 0) { 1771 free(sa, M_SONAME); 1772 CFISCSI_DEBUG("icl_listen_add, error %d", error); 1773 snprintf(ci->error_str, sizeof(ci->error_str), 1774 "icl_listen_add failed, error %d", error); 1775 ci->status = CTL_ISCSI_ERROR; 1776 return; 1777 } 1778 1779 ci->status = CTL_ISCSI_OK; 1780} 1781 1782static void 1783cfiscsi_ioctl_accept(struct ctl_iscsi *ci) 1784{ 1785 struct ctl_iscsi_accept_params *ciap; 1786 struct cfiscsi_session *cs; 1787 int error; 1788 1789 ciap = (struct ctl_iscsi_accept_params *)&(ci->data); 1790 1791 mtx_lock(&cfiscsi_softc.lock); 1792 for (;;) { 1793 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1794 if (cs->cs_waiting_for_ctld) 1795 break; 1796 } 1797 if (cs != NULL) 1798 break; 1799 error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock); 1800 if (error != 0) { 1801 mtx_unlock(&cfiscsi_softc.lock); 1802 snprintf(ci->error_str, sizeof(ci->error_str), "interrupted"); 1803 ci->status = CTL_ISCSI_ERROR; 1804 return; 1805 } 1806 } 1807 mtx_unlock(&cfiscsi_softc.lock); 1808 1809 cs->cs_waiting_for_ctld = false; 1810 cs->cs_login_phase = true; 1811 1812 ciap->connection_id = cs->cs_id; 1813 ciap->portal_id = cs->cs_portal_id; 1814 ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len; 1815 error = copyout(cs->cs_initiator_sa, ciap->initiator_addr, 1816 cs->cs_initiator_sa->sa_len); 1817 if (error != 0) { 1818 snprintf(ci->error_str, sizeof(ci->error_str), 1819 "copyout failed with error %d", error); 1820 ci->status = CTL_ISCSI_ERROR; 1821 return; 1822 } 1823 1824 ci->status = CTL_ISCSI_OK; 1825} 1826 1827static void 1828cfiscsi_ioctl_send(struct ctl_iscsi *ci) 1829{ 1830 struct ctl_iscsi_send_params *cisp; 1831 struct cfiscsi_session *cs; 1832 struct icl_pdu *ip; 1833 size_t datalen; 1834 void *data; 1835 int error; 1836 1837 cisp = (struct ctl_iscsi_send_params *)&(ci->data); 1838 1839 mtx_lock(&cfiscsi_softc.lock); 1840 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1841 if (cs->cs_id == cisp->connection_id) 1842 break; 1843 } 1844 if (cs == NULL) { 1845 mtx_unlock(&cfiscsi_softc.lock); 1846 snprintf(ci->error_str, sizeof(ci->error_str), "connection not found"); 1847 ci->status = CTL_ISCSI_ERROR; 1848 return; 1849 } 1850 mtx_unlock(&cfiscsi_softc.lock); 1851 1852#if 0 1853 if (cs->cs_login_phase == false) 1854 return (EBUSY); 1855#endif 1856 1857 if (cs->cs_terminating) { 1858 snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating"); 1859 ci->status = CTL_ISCSI_ERROR; 1860 return; 1861 } 1862 1863 datalen = cisp->data_segment_len; 1864 /* 1865 * XXX 1866 */ 1867 //if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) { 1868 if (datalen > 65535) { 1869 snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big"); 1870 ci->status = CTL_ISCSI_ERROR; 1871 return; 1872 } 1873 if (datalen > 0) { 1874 data = malloc(datalen, M_CFISCSI, M_WAITOK); 1875 error = copyin(cisp->data_segment, data, datalen); 1876 if (error != 0) { 1877 free(data, M_CFISCSI); 1878 snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error); 1879 ci->status = CTL_ISCSI_ERROR; 1880 return; 1881 } 1882 } 1883 1884 ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK); 1885 memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs)); 1886 if (datalen > 0) { 1887 icl_pdu_append_data(ip, data, datalen, M_WAITOK); 1888 free(data, M_CFISCSI); 1889 } 1890 CFISCSI_SESSION_LOCK(cs); 1891 icl_pdu_queue(ip); 1892 CFISCSI_SESSION_UNLOCK(cs); 1893 ci->status = CTL_ISCSI_OK; 1894} 1895 1896static void 1897cfiscsi_ioctl_receive(struct ctl_iscsi *ci) 1898{ 1899 struct ctl_iscsi_receive_params *cirp; 1900 struct cfiscsi_session *cs; 1901 struct icl_pdu *ip; 1902 void *data; 1903 int error; 1904 1905 cirp = (struct ctl_iscsi_receive_params *)&(ci->data); 1906 1907 mtx_lock(&cfiscsi_softc.lock); 1908 TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) { 1909 if (cs->cs_id == cirp->connection_id) 1910 break; 1911 } 1912 if (cs == NULL) { 1913 mtx_unlock(&cfiscsi_softc.lock); 1914 snprintf(ci->error_str, sizeof(ci->error_str), 1915 "connection not found"); 1916 ci->status = CTL_ISCSI_ERROR; 1917 return; 1918 } 1919 mtx_unlock(&cfiscsi_softc.lock); 1920 1921#if 0 1922 if (is->is_login_phase == false) 1923 return (EBUSY); 1924#endif 1925 1926 CFISCSI_SESSION_LOCK(cs); 1927 while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) { 1928 error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock); 1929 if (error != 0) { 1930 CFISCSI_SESSION_UNLOCK(cs); 1931 snprintf(ci->error_str, sizeof(ci->error_str), 1932 "interrupted by signal"); 1933 ci->status = CTL_ISCSI_ERROR; 1934 return; 1935 } 1936 } 1937 1938 if (cs->cs_terminating) { 1939 CFISCSI_SESSION_UNLOCK(cs); 1940 snprintf(ci->error_str, sizeof(ci->error_str), 1941 "connection terminating"); 1942 ci->status = CTL_ISCSI_ERROR; 1943 return; 1944 } 1945 ip = cs->cs_login_pdu; 1946 cs->cs_login_pdu = NULL; 1947 CFISCSI_SESSION_UNLOCK(cs); 1948 1949 if (ip->ip_data_len > cirp->data_segment_len) { 1950 icl_pdu_free(ip); 1951 snprintf(ci->error_str, sizeof(ci->error_str), 1952 "data segment too big"); 1953 ci->status = CTL_ISCSI_ERROR; 1954 return; 1955 } 1956 1957 copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs)); 1958 if (ip->ip_data_len > 0) { 1959 data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK); 1960 icl_pdu_get_data(ip, 0, data, ip->ip_data_len); 1961 copyout(data, cirp->data_segment, ip->ip_data_len); 1962 free(data, M_CFISCSI); 1963 } 1964 1965 icl_pdu_free(ip); 1966 ci->status = CTL_ISCSI_OK; 1967} 1968 1969#endif /* !ICL_KERNEL_PROXY */ 1970 1971static int 1972cfiscsi_ioctl(struct cdev *dev, 1973 u_long cmd, caddr_t addr, int flag, struct thread *td) 1974{ 1975 struct ctl_iscsi *ci; 1976 1977 if (cmd != CTL_ISCSI) 1978 return (ENOTTY); 1979 1980 ci = (struct ctl_iscsi *)addr; 1981 switch (ci->type) { 1982 case CTL_ISCSI_HANDOFF: 1983 cfiscsi_ioctl_handoff(ci); 1984 break; 1985 case CTL_ISCSI_LIST: 1986 cfiscsi_ioctl_list(ci); 1987 break; 1988 case CTL_ISCSI_TERMINATE: 1989 cfiscsi_ioctl_terminate(ci); 1990 break; 1991 case CTL_ISCSI_LOGOUT: 1992 cfiscsi_ioctl_logout(ci); 1993 break; 1994#ifdef ICL_KERNEL_PROXY 1995 case CTL_ISCSI_LISTEN: 1996 cfiscsi_ioctl_listen(ci); 1997 break; 1998 case CTL_ISCSI_ACCEPT: 1999 cfiscsi_ioctl_accept(ci); 2000 break; 2001 case CTL_ISCSI_SEND: 2002 cfiscsi_ioctl_send(ci); 2003 break; 2004 case CTL_ISCSI_RECEIVE: 2005 cfiscsi_ioctl_receive(ci); 2006 break; 2007#else 2008 case CTL_ISCSI_LISTEN: 2009 case CTL_ISCSI_ACCEPT: 2010 case CTL_ISCSI_SEND: 2011 case CTL_ISCSI_RECEIVE: 2012 ci->status = CTL_ISCSI_ERROR; 2013 snprintf(ci->error_str, sizeof(ci->error_str), 2014 "%s: CTL compiled without ICL_KERNEL_PROXY", 2015 __func__); 2016 break; 2017#endif /* !ICL_KERNEL_PROXY */ 2018 default: 2019 ci->status = CTL_ISCSI_ERROR; 2020 snprintf(ci->error_str, sizeof(ci->error_str), 2021 "%s: invalid iSCSI request type %d", __func__, ci->type); 2022 break; 2023 } 2024 2025 return (0); 2026} 2027 2028static int 2029cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len) 2030{ 2031 struct cfiscsi_session *cs; 2032 struct scsi_vpd_device_id *devid_ptr; 2033 struct scsi_vpd_id_descriptor *desc, *desc1, *desc2, *desc3, *desc4; 2034 struct scsi_vpd_id_descriptor *desc5; 2035 struct scsi_vpd_id_t10 *t10id; 2036 struct ctl_lun *lun; 2037 const struct icl_pdu *request; 2038 int i, ret; 2039 char *val; 2040 size_t data_len, devid_len, wwnn_len, wwpn_len, lun_name_len; 2041 2042 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 2043 request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2044 cs = PDU_SESSION(request); 2045 2046 wwpn_len = strlen(cs->cs_target->ct_name); 2047 wwpn_len += strlen(",t,0x0001"); 2048 wwpn_len += 1; /* '\0' */ 2049 if ((wwpn_len % 4) != 0) 2050 wwpn_len += (4 - (wwpn_len % 4)); 2051 2052 wwnn_len = strlen(cs->cs_target->ct_name); 2053 wwnn_len += 1; /* '\0' */ 2054 if ((wwnn_len % 4) != 0) 2055 wwnn_len += (4 - (wwnn_len % 4)); 2056 2057 if (lun == NULL) { 2058 devid_len = CTL_DEVID_MIN_LEN; 2059 lun_name_len = 0; 2060 } else { 2061 devid_len = max(CTL_DEVID_MIN_LEN, 2062 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN)); 2063 lun_name_len = strlen(cs->cs_target->ct_name); 2064 lun_name_len += strlen(",lun,XXXXXXXX"); 2065 lun_name_len += 1; /* '\0' */ 2066 if ((lun_name_len % 4) != 0) 2067 lun_name_len += (4 - (lun_name_len % 4)); 2068 } 2069 2070 data_len = sizeof(struct scsi_vpd_device_id) + 2071 sizeof(struct scsi_vpd_id_descriptor) + 2072 sizeof(struct scsi_vpd_id_t10) + devid_len + 2073 sizeof(struct scsi_vpd_id_descriptor) + lun_name_len + 2074 sizeof(struct scsi_vpd_id_descriptor) + wwnn_len + 2075 sizeof(struct scsi_vpd_id_descriptor) + wwpn_len + 2076 sizeof(struct scsi_vpd_id_descriptor) + 2077 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 2078 sizeof(struct scsi_vpd_id_descriptor) + 2079 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 2080 2081 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 2082 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 2083 ctsio->kern_sg_entries = 0; 2084 2085 if (data_len < alloc_len) { 2086 ctsio->residual = alloc_len - data_len; 2087 ctsio->kern_data_len = data_len; 2088 ctsio->kern_total_len = data_len; 2089 } else { 2090 ctsio->residual = 0; 2091 ctsio->kern_data_len = alloc_len; 2092 ctsio->kern_total_len = alloc_len; 2093 } 2094 ctsio->kern_data_resid = 0; 2095 ctsio->kern_rel_offset = 0; 2096 ctsio->kern_sg_entries = 0; 2097 2098 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 2099 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 2100 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 2101 sizeof(struct scsi_vpd_id_t10) + devid_len); 2102 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 2103 lun_name_len); 2104 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 2105 wwnn_len); 2106 desc4 = (struct scsi_vpd_id_descriptor *)(&desc3->identifier[0] + 2107 wwpn_len); 2108 desc5 = (struct scsi_vpd_id_descriptor *)(&desc4->identifier[0] + 2109 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 2110 2111 if (lun != NULL) 2112 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 2113 lun->be_lun->lun_type; 2114 else 2115 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 2116 2117 devid_ptr->page_code = SVPD_DEVICE_ID; 2118 2119 scsi_ulto2b(data_len - 4, devid_ptr->length); 2120 2121 /* 2122 * We're using a LUN association here. i.e., this device ID is a 2123 * per-LUN identifier. 2124 */ 2125 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII; 2126 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 2127 desc->length = sizeof(*t10id) + devid_len; 2128 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 2129 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 2130 } else { 2131 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 2132 strncpy(t10id->vendor, val, 2133 min(sizeof(t10id->vendor), strlen(val))); 2134 } 2135 2136 /* 2137 * If we've actually got a backend, copy the device id from the 2138 * per-LUN data. Otherwise, set it to all spaces. 2139 */ 2140 if (lun != NULL) { 2141 /* 2142 * Copy the backend's LUN ID. 2143 */ 2144 strncpy((char *)t10id->vendor_spec_id, 2145 (char *)lun->be_lun->device_id, devid_len); 2146 } else { 2147 /* 2148 * No backend, set this to spaces. 2149 */ 2150 memset(t10id->vendor_spec_id, 0x20, devid_len); 2151 } 2152 2153 /* 2154 * desc1 is for the unique LUN name. 2155 * 2156 * XXX: According to SPC-3, LUN must report the same ID through 2157 * all the ports. The code below, however, reports the 2158 * ID only via iSCSI. 2159 */ 2160 desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2161 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 2162 SVPD_ID_TYPE_SCSI_NAME; 2163 desc1->length = lun_name_len; 2164 if (lun != NULL) { 2165 /* 2166 * Find the per-target LUN number. 2167 */ 2168 for (i = 0; i < CTL_MAX_LUNS; i++) { 2169 if (cs->cs_target->ct_luns[i] == lun->lun) 2170 break; 2171 } 2172 KASSERT(i < CTL_MAX_LUNS, 2173 ("lun %jd not found", (uintmax_t)lun->lun)); 2174 ret = snprintf(desc1->identifier, lun_name_len, "%s,lun,%d", 2175 cs->cs_target->ct_name, i); 2176 KASSERT(ret > 0 && ret <= lun_name_len, ("bad snprintf")); 2177 } else { 2178 KASSERT(lun_name_len == 0, ("no lun, but lun_name_len != 0")); 2179 } 2180 2181 /* 2182 * desc2 is for the Target Name. 2183 */ 2184 desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2185 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET | 2186 SVPD_ID_TYPE_SCSI_NAME; 2187 desc2->length = wwnn_len; 2188 snprintf(desc2->identifier, wwnn_len, "%s", cs->cs_target->ct_name); 2189 2190 /* 2191 * desc3 is for the WWPN which is a port asscociation. 2192 */ 2193 desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2194 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2195 SVPD_ID_TYPE_SCSI_NAME; 2196 desc3->length = wwpn_len; 2197 snprintf(desc3->identifier, wwpn_len, "%s,t,0x%4.4x", 2198 cs->cs_target->ct_name, cs->cs_portal_group_tag); 2199 2200 /* 2201 * desc3 is for the Relative Target Port(type 4h) identifier 2202 */ 2203 desc4->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY; 2204 desc4->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2205 SVPD_ID_TYPE_RELTARG; 2206 desc4->length = 4; 2207 desc4->identifier[3] = 1; 2208 2209 /* 2210 * desc4 is for the Target Port Group(type 5h) identifier 2211 */ 2212 desc5->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY; 2213 desc5->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2214 SVPD_ID_TYPE_TPORTGRP; 2215 desc5->length = 4; 2216 desc5->identifier[3] = 1; 2217 2218 ctsio->scsi_status = SCSI_STATUS_OK; 2219 2220 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 2221 ctsio->be_move_done = ctl_config_move_done; 2222 ctl_datamove((union ctl_io *)ctsio); 2223 2224 return (CTL_RETVAL_COMPLETE); 2225} 2226 2227static void 2228cfiscsi_target_hold(struct cfiscsi_target *ct) 2229{ 2230 2231 refcount_acquire(&ct->ct_refcount); 2232} 2233 2234static void 2235cfiscsi_target_release(struct cfiscsi_target *ct) 2236{ 2237 struct cfiscsi_softc *softc; 2238 2239 softc = ct->ct_softc; 2240 mtx_lock(&softc->lock); 2241 if (refcount_release(&ct->ct_refcount)) { 2242 TAILQ_REMOVE(&softc->targets, ct, ct_next); 2243 mtx_unlock(&softc->lock); 2244 free(ct, M_CFISCSI); 2245 2246 return; 2247 } 2248 mtx_unlock(&softc->lock); 2249} 2250 2251static struct cfiscsi_target * 2252cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name) 2253{ 2254 struct cfiscsi_target *ct; 2255 2256 mtx_lock(&softc->lock); 2257 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2258 if (strcmp(name, ct->ct_name) != 0) 2259 continue; 2260 cfiscsi_target_hold(ct); 2261 mtx_unlock(&softc->lock); 2262 return (ct); 2263 } 2264 mtx_unlock(&softc->lock); 2265 2266 return (NULL); 2267} 2268 2269static struct cfiscsi_target * 2270cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name, 2271 const char *alias) 2272{ 2273 struct cfiscsi_target *ct, *newct; 2274 int i; 2275 2276 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN) 2277 return (NULL); 2278 2279 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO); 2280 2281 mtx_lock(&softc->lock); 2282 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2283 if (strcmp(name, ct->ct_name) != 0) 2284 continue; 2285 cfiscsi_target_hold(ct); 2286 mtx_unlock(&softc->lock); 2287 free(newct, M_CFISCSI); 2288 return (ct); 2289 } 2290 2291 for (i = 0; i < CTL_MAX_LUNS; i++) 2292 newct->ct_luns[i] = -1; 2293 2294 strlcpy(newct->ct_name, name, sizeof(newct->ct_name)); 2295 if (alias != NULL) 2296 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias)); 2297 refcount_init(&newct->ct_refcount, 1); 2298 newct->ct_softc = softc; 2299 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next); 2300 mtx_unlock(&softc->lock); 2301 2302 return (newct); 2303} 2304 2305/* 2306 * Takes LUN from the target space and returns LUN from the CTL space. 2307 */ 2308static uint32_t 2309cfiscsi_map_lun(void *arg, uint32_t lun) 2310{ 2311 struct cfiscsi_session *cs; 2312 2313 cs = arg; 2314 2315 if (lun >= CTL_MAX_LUNS) { 2316 CFISCSI_DEBUG("requested lun number %d is higher " 2317 "than maximum %d", lun, CTL_MAX_LUNS - 1); 2318 return (0xffffffff); 2319 } 2320 2321 if (cs->cs_target->ct_luns[lun] < 0) 2322 return (0xffffffff); 2323 2324 return (cs->cs_target->ct_luns[lun]); 2325} 2326 2327static int 2328cfiscsi_target_set_lun(struct cfiscsi_target *ct, 2329 unsigned long lun_id, unsigned long ctl_lun_id) 2330{ 2331 2332 if (lun_id >= CTL_MAX_LUNS) { 2333 CFISCSI_WARN("requested lun number %ld is higher " 2334 "than maximum %d", lun_id, CTL_MAX_LUNS - 1); 2335 return (-1); 2336 } 2337 2338 if (ct->ct_luns[lun_id] >= 0) { 2339 /* 2340 * CTL calls cfiscsi_lun_enable() twice for each LUN - once 2341 * when the LUN is created, and a second time just before 2342 * the port is brought online; don't emit warnings 2343 * for that case. 2344 */ 2345 if (ct->ct_luns[lun_id] == ctl_lun_id) 2346 return (0); 2347 CFISCSI_WARN("lun %ld already allocated", lun_id); 2348 return (-1); 2349 } 2350 2351#if 0 2352 CFISCSI_DEBUG("adding mapping for lun %ld, target %s " 2353 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id); 2354#endif 2355 2356 ct->ct_luns[lun_id] = ctl_lun_id; 2357 cfiscsi_target_hold(ct); 2358 2359 return (0); 2360} 2361 2362static int 2363cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id) 2364{ 2365 2366 if (ct->ct_luns[lun_id] < 0) { 2367 CFISCSI_WARN("lun %ld not allocated", lun_id); 2368 return (-1); 2369 } 2370 2371 ct->ct_luns[lun_id] = -1; 2372 cfiscsi_target_release(ct); 2373 2374 return (0); 2375} 2376 2377static int 2378cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id) 2379{ 2380 struct cfiscsi_softc *softc; 2381 struct cfiscsi_target *ct; 2382 const char *target = NULL, *target_alias = NULL; 2383 const char *lun = NULL; 2384 unsigned long tmp; 2385 2386 softc = (struct cfiscsi_softc *)arg; 2387 2388 target = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun, 2389 "cfiscsi_target"); 2390 target_alias = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun, 2391 "cfiscsi_target_alias"); 2392 lun = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun, 2393 "cfiscsi_lun"); 2394 2395 if (target == NULL && lun == NULL) 2396 return (0); 2397 2398 if (target == NULL || lun == NULL) { 2399 CFISCSI_WARN("lun added with cfiscsi_target, but without " 2400 "cfiscsi_lun, or the other way around; ignoring"); 2401 return (0); 2402 } 2403 2404 ct = cfiscsi_target_find_or_create(softc, target, target_alias); 2405 if (ct == NULL) { 2406 CFISCSI_WARN("failed to create target \"%s\"", target); 2407 return (0); 2408 } 2409 2410 tmp = strtoul(lun, NULL, 10); 2411 cfiscsi_target_set_lun(ct, tmp, lun_id); 2412 cfiscsi_target_release(ct); 2413 return (0); 2414} 2415 2416static int 2417cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id) 2418{ 2419 struct cfiscsi_softc *softc; 2420 struct cfiscsi_target *ct; 2421 int i; 2422 2423 softc = (struct cfiscsi_softc *)arg; 2424 2425 mtx_lock(&softc->lock); 2426 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2427 for (i = 0; i < CTL_MAX_LUNS; i++) { 2428 if (ct->ct_luns[i] < 0) 2429 continue; 2430 if (ct->ct_luns[i] != lun_id) 2431 continue; 2432 mtx_unlock(&softc->lock); 2433 cfiscsi_target_unset_lun(ct, i); 2434 return (0); 2435 } 2436 } 2437 mtx_unlock(&softc->lock); 2438 return (0); 2439} 2440 2441static void 2442cfiscsi_datamove_in(union ctl_io *io) 2443{ 2444 struct cfiscsi_session *cs; 2445 struct icl_pdu *request, *response; 2446 const struct iscsi_bhs_scsi_command *bhssc; 2447 struct iscsi_bhs_data_in *bhsdi; 2448 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 2449 size_t len, expected_len, sg_len, buffer_offset; 2450 const char *sg_addr; 2451 int ctl_sg_count, error, i; 2452 2453 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2454 cs = PDU_SESSION(request); 2455 2456 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2457 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2458 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2459 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2460 2461 if (io->scsiio.kern_sg_entries > 0) { 2462 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 2463 ctl_sg_count = io->scsiio.kern_sg_entries; 2464 } else { 2465 ctl_sglist = &ctl_sg_entry; 2466 ctl_sglist->addr = io->scsiio.kern_data_ptr; 2467 ctl_sglist->len = io->scsiio.kern_data_len; 2468 ctl_sg_count = 1; 2469 } 2470 2471 /* 2472 * This is the total amount of data to be transferred within the current 2473 * SCSI command. We need to record it so that we can properly report 2474 * underflow/underflow. 2475 */ 2476 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2477 2478 /* 2479 * This is the offset within the current SCSI command; for the first 2480 * call to cfiscsi_datamove() it will be 0, and for subsequent ones 2481 * it will be the sum of lengths of previous ones. 2482 */ 2483 buffer_offset = io->scsiio.kern_rel_offset; 2484 2485 /* 2486 * This is the transfer length expected by the initiator. In theory, 2487 * it could be different from the correct amount of data from the SCSI 2488 * point of view, even if that doesn't make any sense. 2489 */ 2490 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length); 2491#if 0 2492 if (expected_len != io->scsiio.kern_total_len) { 2493 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, " 2494 "actual length %zd", expected_len, 2495 (size_t)io->scsiio.kern_total_len); 2496 } 2497#endif 2498 2499 if (buffer_offset >= expected_len) { 2500#if 0 2501 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, " 2502 "already sent the expected len", buffer_offset); 2503#endif 2504 io->scsiio.be_move_done(io); 2505 return; 2506 } 2507 2508 i = 0; 2509 sg_addr = NULL; 2510 sg_len = 0; 2511 response = NULL; 2512 bhsdi = NULL; 2513 for (;;) { 2514 if (response == NULL) { 2515 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2516 if (response == NULL) { 2517 CFISCSI_SESSION_WARN(cs, "failed to " 2518 "allocate memory; dropping connection"); 2519 ctl_set_busy(&io->scsiio); 2520 io->scsiio.be_move_done(io); 2521 cfiscsi_session_terminate(cs); 2522 return; 2523 } 2524 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs; 2525 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN; 2526 bhsdi->bhsdi_initiator_task_tag = 2527 bhssc->bhssc_initiator_task_tag; 2528 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request)); 2529 PDU_EXPDATASN(request)++; 2530 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset); 2531 } 2532 2533 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count")); 2534 if (sg_len == 0) { 2535 sg_addr = ctl_sglist[i].addr; 2536 sg_len = ctl_sglist[i].len; 2537 KASSERT(sg_len > 0, ("sg_len <= 0")); 2538 } 2539 2540 len = sg_len; 2541 2542 /* 2543 * Truncate to maximum data segment length. 2544 */ 2545 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length, 2546 ("ip_data_len %zd >= max_data_segment_length %zd", 2547 response->ip_data_len, cs->cs_max_data_segment_length)); 2548 if (response->ip_data_len + len > 2549 cs->cs_max_data_segment_length) { 2550 len = cs->cs_max_data_segment_length - 2551 response->ip_data_len; 2552 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2553 len, sg_len)); 2554 } 2555 2556 /* 2557 * Truncate to expected data transfer length. 2558 */ 2559 KASSERT(buffer_offset + response->ip_data_len < expected_len, 2560 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd", 2561 buffer_offset, response->ip_data_len, expected_len)); 2562 if (buffer_offset + response->ip_data_len + len > expected_len) { 2563 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd " 2564 "to expected data transfer length %zd", 2565 buffer_offset + response->ip_data_len + len, expected_len); 2566 len = expected_len - (buffer_offset + response->ip_data_len); 2567 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2568 len, sg_len)); 2569 } 2570 2571 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT); 2572 if (error != 0) { 2573 CFISCSI_SESSION_WARN(cs, "failed to " 2574 "allocate memory; dropping connection"); 2575 icl_pdu_free(response); 2576 ctl_set_busy(&io->scsiio); 2577 io->scsiio.be_move_done(io); 2578 cfiscsi_session_terminate(cs); 2579 return; 2580 } 2581 sg_addr += len; 2582 sg_len -= len; 2583 2584 KASSERT(buffer_offset + request->ip_data_len <= expected_len, 2585 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd", 2586 buffer_offset, request->ip_data_len, expected_len)); 2587 if (buffer_offset + request->ip_data_len == expected_len) { 2588 /* 2589 * Already have the amount of data the initiator wanted. 2590 */ 2591 break; 2592 } 2593 2594 if (sg_len == 0) { 2595 /* 2596 * End of scatter-gather segment; 2597 * proceed to the next one... 2598 */ 2599 if (i == ctl_sg_count - 1) { 2600 /* 2601 * ... unless this was the last one. 2602 */ 2603 break; 2604 } 2605 i++; 2606 } 2607 2608 if (response->ip_data_len == cs->cs_max_data_segment_length) { 2609 /* 2610 * Can't stuff more data into the current PDU; 2611 * queue it. Note that's not enough to check 2612 * for kern_data_resid == 0 instead; there 2613 * may be several Data-In PDUs for the final 2614 * call to cfiscsi_datamove(), and we want 2615 * to set the F flag only on the last of them. 2616 */ 2617 buffer_offset += response->ip_data_len; 2618 if (buffer_offset == io->scsiio.kern_total_len || 2619 buffer_offset == expected_len) 2620 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F; 2621 cfiscsi_pdu_queue(response); 2622 response = NULL; 2623 bhsdi = NULL; 2624 } 2625 } 2626 if (response != NULL) { 2627 buffer_offset += response->ip_data_len; 2628 if (buffer_offset == io->scsiio.kern_total_len || 2629 buffer_offset == expected_len) 2630 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F; 2631 KASSERT(response->ip_data_len > 0, ("sending empty Data-In")); 2632 cfiscsi_pdu_queue(response); 2633 } 2634 2635 io->scsiio.be_move_done(io); 2636} 2637 2638static void 2639cfiscsi_datamove_out(union ctl_io *io) 2640{ 2641 struct cfiscsi_session *cs; 2642 struct icl_pdu *request, *response; 2643 const struct iscsi_bhs_scsi_command *bhssc; 2644 struct iscsi_bhs_r2t *bhsr2t; 2645 struct cfiscsi_data_wait *cdw; 2646 uint32_t target_transfer_tag; 2647 bool done; 2648 2649 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2650 cs = PDU_SESSION(request); 2651 2652 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2653 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2654 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2655 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2656 2657 /* 2658 * We need to record it so that we can properly report 2659 * underflow/underflow. 2660 */ 2661 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2662 2663 /* 2664 * We hadn't received anything during this datamove yet. 2665 */ 2666 io->scsiio.ext_data_filled = 0; 2667 2668 target_transfer_tag = 2669 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1); 2670 2671#if 0 2672 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator " 2673 "task tag 0x%x, target transfer tag 0x%x", 2674 bhssc->bhssc_initiator_task_tag, target_transfer_tag); 2675#endif 2676 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO); 2677 if (cdw == NULL) { 2678 CFISCSI_SESSION_WARN(cs, "failed to " 2679 "allocate memory; dropping connection"); 2680 ctl_set_busy(&io->scsiio); 2681 io->scsiio.be_move_done(io); 2682 cfiscsi_session_terminate(cs); 2683 return; 2684 } 2685 cdw->cdw_ctl_io = io; 2686 cdw->cdw_target_transfer_tag = target_transfer_tag; 2687 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2688 2689 if (cs->cs_immediate_data && io->scsiio.kern_rel_offset < 2690 icl_pdu_data_segment_length(request)) { 2691 done = cfiscsi_handle_data_segment(request, cdw); 2692 if (done) { 2693 uma_zfree(cfiscsi_data_wait_zone, cdw); 2694 io->scsiio.be_move_done(io); 2695 return; 2696 } 2697 } 2698 2699 CFISCSI_SESSION_LOCK(cs); 2700 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next); 2701 CFISCSI_SESSION_UNLOCK(cs); 2702 2703 /* 2704 * XXX: We should limit the number of outstanding R2T PDUs 2705 * per task to MaxOutstandingR2T. 2706 */ 2707 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2708 if (response == NULL) { 2709 CFISCSI_SESSION_WARN(cs, "failed to " 2710 "allocate memory; dropping connection"); 2711 ctl_set_busy(&io->scsiio); 2712 io->scsiio.be_move_done(io); 2713 cfiscsi_session_terminate(cs); 2714 return; 2715 } 2716 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs; 2717 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T; 2718 bhsr2t->bhsr2t_flags = 0x80; 2719 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun; 2720 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2721 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag; 2722 /* 2723 * XXX: Here we assume that cfiscsi_datamove() won't ever 2724 * be running concurrently on several CPUs for a given 2725 * command. 2726 */ 2727 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request)); 2728 PDU_R2TSN(request)++; 2729 /* 2730 * This is the offset within the current SCSI command; 2731 * i.e. for the first call of datamove(), it will be 0, 2732 * and for subsequent ones it will be the sum of lengths 2733 * of previous ones. 2734 * 2735 * The ext_data_filled is to account for unsolicited 2736 * (immediate) data that might have already arrived. 2737 */ 2738 bhsr2t->bhsr2t_buffer_offset = 2739 htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled); 2740 /* 2741 * This is the total length (sum of S/G lengths) this call 2742 * to cfiscsi_datamove() is supposed to handle. 2743 * 2744 * XXX: Limit it to MaxBurstLength. 2745 */ 2746 bhsr2t->bhsr2t_desired_data_transfer_length = 2747 htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled); 2748 cfiscsi_pdu_queue(response); 2749} 2750 2751static void 2752cfiscsi_datamove(union ctl_io *io) 2753{ 2754 2755 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 2756 cfiscsi_datamove_in(io); 2757 else 2758 cfiscsi_datamove_out(io); 2759} 2760 2761static void 2762cfiscsi_scsi_command_done(union ctl_io *io) 2763{ 2764 struct icl_pdu *request, *response; 2765 struct iscsi_bhs_scsi_command *bhssc; 2766 struct iscsi_bhs_scsi_response *bhssr; 2767#ifdef DIAGNOSTIC 2768 struct cfiscsi_data_wait *cdw; 2769#endif 2770 struct cfiscsi_session *cs; 2771 uint16_t sense_length; 2772 2773 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2774 cs = PDU_SESSION(request); 2775 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 2776 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2777 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2778 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode)); 2779 2780 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x", 2781 // bhssc->bhssc_initiator_task_tag); 2782 2783#ifdef DIAGNOSTIC 2784 CFISCSI_SESSION_LOCK(cs); 2785 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) 2786 KASSERT(bhssc->bhssc_initiator_task_tag != 2787 cdw->cdw_initiator_task_tag, ("dangling cdw")); 2788 CFISCSI_SESSION_UNLOCK(cs); 2789#endif 2790 2791 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2792 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 2793 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE; 2794 bhssr->bhssr_flags = 0x80; 2795 /* 2796 * XXX: We don't deal with bidirectional under/overflows; 2797 * does anything actually support those? 2798 */ 2799 if (PDU_TOTAL_TRANSFER_LEN(request) < 2800 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2801 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW; 2802 bhssr->bhssr_residual_count = 2803 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) - 2804 PDU_TOTAL_TRANSFER_LEN(request)); 2805 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d", 2806 // ntohl(bhssr->bhssr_residual_count)); 2807 } else if (PDU_TOTAL_TRANSFER_LEN(request) > 2808 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2809 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW; 2810 bhssr->bhssr_residual_count = 2811 htonl(PDU_TOTAL_TRANSFER_LEN(request) - 2812 ntohl(bhssc->bhssc_expected_data_transfer_length)); 2813 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d", 2814 // ntohl(bhssr->bhssr_residual_count)); 2815 } 2816 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED; 2817 bhssr->bhssr_status = io->scsiio.scsi_status; 2818 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2819 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request)); 2820 2821 if (io->scsiio.sense_len > 0) { 2822#if 0 2823 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data", 2824 io->scsiio.sense_len); 2825#endif 2826 sense_length = htons(io->scsiio.sense_len); 2827 icl_pdu_append_data(response, 2828 &sense_length, sizeof(sense_length), M_WAITOK); 2829 icl_pdu_append_data(response, 2830 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK); 2831 } 2832 2833 ctl_free_io(io); 2834 icl_pdu_free(request); 2835 cfiscsi_pdu_queue(response); 2836} 2837 2838static void 2839cfiscsi_task_management_done(union ctl_io *io) 2840{ 2841 struct icl_pdu *request, *response; 2842 struct iscsi_bhs_task_management_request *bhstmr; 2843 struct iscsi_bhs_task_management_response *bhstmr2; 2844 struct cfiscsi_data_wait *cdw, *tmpcdw; 2845 struct cfiscsi_session *cs; 2846 2847 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2848 cs = PDU_SESSION(request); 2849 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 2850 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2851 ISCSI_BHS_OPCODE_TASK_REQUEST, 2852 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode)); 2853 2854#if 0 2855 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x", 2856 bhstmr->bhstmr_initiator_task_tag, 2857 bhstmr->bhstmr_referenced_task_tag); 2858#endif 2859 2860 if ((bhstmr->bhstmr_function & ~0x80) == 2861 BHSTMR_FUNCTION_ABORT_TASK) { 2862 /* 2863 * Make sure we no longer wait for Data-Out for this command. 2864 */ 2865 CFISCSI_SESSION_LOCK(cs); 2866 TAILQ_FOREACH_SAFE(cdw, 2867 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) { 2868 if (bhstmr->bhstmr_referenced_task_tag != 2869 cdw->cdw_initiator_task_tag) 2870 continue; 2871 2872#if 0 2873 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task " 2874 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag); 2875#endif 2876 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, 2877 cdw, cdw_next); 2878 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io); 2879 uma_zfree(cfiscsi_data_wait_zone, cdw); 2880 } 2881 CFISCSI_SESSION_UNLOCK(cs); 2882 } 2883 2884 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2885 bhstmr2 = (struct iscsi_bhs_task_management_response *) 2886 response->ip_bhs; 2887 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE; 2888 bhstmr2->bhstmr_flags = 0x80; 2889 if (io->io_hdr.status == CTL_SUCCESS) { 2890 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE; 2891 } else { 2892 /* 2893 * XXX: How to figure out what exactly went wrong? iSCSI spec 2894 * expects us to provide detailed error, e.g. "Task does 2895 * not exist" or "LUN does not exist". 2896 */ 2897 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED"); 2898 bhstmr2->bhstmr_response = 2899 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED; 2900 } 2901 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag; 2902 2903 ctl_free_io(io); 2904 icl_pdu_free(request); 2905 cfiscsi_pdu_queue(response); 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 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2918 if (request == NULL) { 2919 /* 2920 * Implicit task termination has just completed; nothing to do. 2921 */ 2922 return; 2923 } 2924 2925 cs = PDU_SESSION(request); 2926 refcount_release(&cs->cs_outstanding_ctl_pdus); 2927 2928 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) { 2929 case ISCSI_BHS_OPCODE_SCSI_COMMAND: 2930 cfiscsi_scsi_command_done(io); 2931 break; 2932 case ISCSI_BHS_OPCODE_TASK_REQUEST: 2933 cfiscsi_task_management_done(io); 2934 break; 2935 default: 2936 panic("cfiscsi_done called with wrong opcode 0x%x", 2937 request->ip_bhs->bhs_opcode); 2938 } 2939} 2940