ctl_frontend_iscsi.c revision 268258
1/*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_iscsi.c 268258 2014-07-04 16:07:57Z mav $ 30 */ 31 32/* 33 * CTL frontend for the iSCSI protocol. 34 */ 35 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_iscsi.c 268258 2014-07-04 16:07:57Z mav $"); 38 39#include <sys/param.h> 40#include <sys/capability.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; 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_targ_enable(void *arg, struct ctl_id targ_id); 153static int cfiscsi_targ_disable(void *arg, struct ctl_id targ_id); 154static int cfiscsi_lun_enable(void *arg, 155 struct ctl_id target_id, int lun_id); 156static int cfiscsi_lun_disable(void *arg, 157 struct ctl_id target_id, int lun_id); 158static int cfiscsi_ioctl(struct cdev *dev, 159 u_long cmd, caddr_t addr, int flag, struct thread *td); 160static int cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len); 161static void cfiscsi_datamove(union ctl_io *io); 162static void cfiscsi_done(union ctl_io *io); 163static uint32_t cfiscsi_map_lun(void *arg, uint32_t lun); 164static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request); 165static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request); 166static void cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request); 167static void cfiscsi_pdu_handle_task_request(struct icl_pdu *request); 168static void cfiscsi_pdu_handle_data_out(struct icl_pdu *request); 169static void cfiscsi_pdu_handle_logout_request(struct icl_pdu *request); 170static void cfiscsi_session_terminate(struct cfiscsi_session *cs); 171static struct cfiscsi_target *cfiscsi_target_find(struct cfiscsi_softc 172 *softc, const char *name); 173static void cfiscsi_target_release(struct cfiscsi_target *ct); 174static void cfiscsi_session_delete(struct cfiscsi_session *cs); 175 176static struct cfiscsi_softc cfiscsi_softc; 177extern struct ctl_softc *control_softc; 178 179static int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *); 180 181static moduledata_t cfiscsi_moduledata = { 182 "ctlcfiscsi", 183 cfiscsi_module_event_handler, 184 NULL 185}; 186 187DECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); 188MODULE_VERSION(ctlcfiscsi, 1); 189MODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1); 190MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1); 191 192static struct icl_pdu * 193cfiscsi_pdu_new_response(struct icl_pdu *request, int flags) 194{ 195 196 return (icl_pdu_new_bhs(request->ip_conn, flags)); 197} 198 199static bool 200cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request) 201{ 202 const struct iscsi_bhs_scsi_command *bhssc; 203 struct cfiscsi_session *cs; 204 uint32_t cmdsn, expstatsn; 205 206 cs = PDU_SESSION(request); 207 208 /* 209 * Every incoming PDU - not just NOP-Out - resets the ping timer. 210 * The purpose of the timeout is to reset the connection when it stalls; 211 * we don't want this to happen when NOP-In or NOP-Out ends up delayed 212 * in some queue. 213 * 214 * XXX: Locking? 215 */ 216 cs->cs_timeout = 0; 217 218 /* 219 * Data-Out PDUs don't contain CmdSN. 220 */ 221 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 222 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) 223 return (false); 224 225 /* 226 * We're only using fields common for all the request 227 * (initiator -> target) PDUs. 228 */ 229 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 230 cmdsn = ntohl(bhssc->bhssc_cmdsn); 231 expstatsn = ntohl(bhssc->bhssc_expstatsn); 232 233 CFISCSI_SESSION_LOCK(cs); 234#if 0 235 if (expstatsn != cs->cs_statsn) { 236 CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, " 237 "while current StatSN is %d", expstatsn, 238 cs->cs_statsn); 239 } 240#endif 241 242 /* 243 * The target MUST silently ignore any non-immediate command outside 244 * of this range. 245 */ 246 if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) { 247 CFISCSI_SESSION_UNLOCK(cs); 248 CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, " 249 "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn); 250 return (true); 251 } 252 253 if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0) 254 cs->cs_cmdsn++; 255 256 CFISCSI_SESSION_UNLOCK(cs); 257 258 return (false); 259} 260 261static void 262cfiscsi_pdu_handle(struct icl_pdu *request) 263{ 264 struct cfiscsi_session *cs; 265 bool ignore; 266 267 cs = PDU_SESSION(request); 268 269 ignore = cfiscsi_pdu_update_cmdsn(request); 270 if (ignore) { 271 icl_pdu_free(request); 272 return; 273 } 274 275 /* 276 * Handle the PDU; this includes e.g. receiving the remaining 277 * part of PDU and submitting the SCSI command to CTL 278 * or queueing a reply. The handling routine is responsible 279 * for freeing the PDU when it's no longer needed. 280 */ 281 switch (request->ip_bhs->bhs_opcode & 282 ~ISCSI_BHS_OPCODE_IMMEDIATE) { 283 case ISCSI_BHS_OPCODE_NOP_OUT: 284 cfiscsi_pdu_handle_nop_out(request); 285 break; 286 case ISCSI_BHS_OPCODE_SCSI_COMMAND: 287 cfiscsi_pdu_handle_scsi_command(request); 288 break; 289 case ISCSI_BHS_OPCODE_TASK_REQUEST: 290 cfiscsi_pdu_handle_task_request(request); 291 break; 292 case ISCSI_BHS_OPCODE_SCSI_DATA_OUT: 293 cfiscsi_pdu_handle_data_out(request); 294 break; 295 case ISCSI_BHS_OPCODE_LOGOUT_REQUEST: 296 cfiscsi_pdu_handle_logout_request(request); 297 break; 298 default: 299 CFISCSI_SESSION_WARN(cs, "received PDU with unsupported " 300 "opcode 0x%x; dropping connection", 301 request->ip_bhs->bhs_opcode); 302 icl_pdu_free(request); 303 cfiscsi_session_terminate(cs); 304 } 305 306} 307 308static void 309cfiscsi_receive_callback(struct icl_pdu *request) 310{ 311 struct cfiscsi_session *cs; 312 313 cs = PDU_SESSION(request); 314 315#ifdef ICL_KERNEL_PROXY 316 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) { 317 if (cs->cs_login_pdu == NULL) 318 cs->cs_login_pdu = request; 319 else 320 icl_pdu_free(request); 321 cv_signal(&cs->cs_login_cv); 322 return; 323 } 324#endif 325 326 cfiscsi_pdu_handle(request); 327} 328 329static void 330cfiscsi_error_callback(struct icl_conn *ic) 331{ 332 struct cfiscsi_session *cs; 333 334 cs = CONN_SESSION(ic); 335 336 CFISCSI_SESSION_WARN(cs, "connection error; dropping connection"); 337 cfiscsi_session_terminate(cs); 338} 339 340static int 341cfiscsi_pdu_prepare(struct icl_pdu *response) 342{ 343 struct cfiscsi_session *cs; 344 struct iscsi_bhs_scsi_response *bhssr; 345 bool advance_statsn = true; 346 347 cs = PDU_SESSION(response); 348 349 CFISCSI_SESSION_LOCK_ASSERT(cs); 350 351 /* 352 * We're only using fields common for all the response 353 * (target -> initiator) PDUs. 354 */ 355 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 356 357 /* 358 * 10.8.3: "The StatSN for this connection is not advanced 359 * after this PDU is sent." 360 */ 361 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T) 362 advance_statsn = false; 363 364 /* 365 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff, 366 * StatSN for the connection is not advanced after this PDU is sent." 367 */ 368 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN && 369 bhssr->bhssr_initiator_task_tag == 0xffffffff) 370 advance_statsn = false; 371 372 /* 373 * See the comment below - StatSN is not meaningful and must 374 * not be advanced. 375 */ 376 if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN) 377 advance_statsn = false; 378 379 /* 380 * 10.7.3: "The fields StatSN, Status, and Residual Count 381 * only have meaningful content if the S bit is set to 1." 382 */ 383 if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN) 384 bhssr->bhssr_statsn = htonl(cs->cs_statsn); 385 bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn); 386 bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta); 387 388 if (advance_statsn) 389 cs->cs_statsn++; 390 391 return (0); 392} 393 394static void 395cfiscsi_pdu_queue(struct icl_pdu *response) 396{ 397 struct cfiscsi_session *cs; 398 399 cs = PDU_SESSION(response); 400 401 CFISCSI_SESSION_LOCK(cs); 402 cfiscsi_pdu_prepare(response); 403 icl_pdu_queue(response); 404 CFISCSI_SESSION_UNLOCK(cs); 405} 406 407static uint32_t 408cfiscsi_decode_lun(uint64_t encoded) 409{ 410 uint8_t lun[8]; 411 uint32_t result; 412 413 /* 414 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number, 415 * but is in fact an evil, multidimensional structure defined 416 * in SCSI Architecture Model 5 (SAM-5), section 4.6. 417 */ 418 memcpy(lun, &encoded, sizeof(lun)); 419 switch (lun[0] & 0xC0) { 420 case 0x00: 421 if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 || 422 lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) { 423 CFISCSI_WARN("malformed LUN " 424 "(peripheral device addressing method): 0x%jx", 425 (uintmax_t)encoded); 426 result = 0xffffffff; 427 break; 428 } 429 result = lun[1]; 430 break; 431 case 0x40: 432 if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 || 433 lun[6] != 0 || lun[7] != 0) { 434 CFISCSI_WARN("malformed LUN " 435 "(flat address space addressing method): 0x%jx", 436 (uintmax_t)encoded); 437 result = 0xffffffff; 438 break; 439 } 440 result = ((lun[0] & 0x3f) << 8) + lun[1]; 441 break; 442 case 0xC0: 443 if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 || 444 lun[6] != 0 || lun[7] != 0) { 445 CFISCSI_WARN("malformed LUN (extended flat " 446 "address space addressing method): 0x%jx", 447 (uintmax_t)encoded); 448 result = 0xffffffff; 449 break; 450 } 451 result = (lun[1] << 16) + (lun[2] << 8) + lun[3]; 452 default: 453 CFISCSI_WARN("unsupported LUN format 0x%jx", 454 (uintmax_t)encoded); 455 result = 0xffffffff; 456 break; 457 } 458 459 return (result); 460} 461 462static void 463cfiscsi_pdu_handle_nop_out(struct icl_pdu *request) 464{ 465 struct cfiscsi_session *cs; 466 struct iscsi_bhs_nop_out *bhsno; 467 struct iscsi_bhs_nop_in *bhsni; 468 struct icl_pdu *response; 469 void *data = NULL; 470 size_t datasize; 471 int error; 472 473 cs = PDU_SESSION(request); 474 bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs; 475 476 if (bhsno->bhsno_initiator_task_tag == 0xffffffff) { 477 /* 478 * Nothing to do, iscsi_pdu_update_statsn() already 479 * zeroed the timeout. 480 */ 481 icl_pdu_free(request); 482 return; 483 } 484 485 datasize = icl_pdu_data_segment_length(request); 486 if (datasize > 0) { 487 data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO); 488 if (data == NULL) { 489 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; " 490 "dropping connection"); 491 icl_pdu_free(request); 492 cfiscsi_session_terminate(cs); 493 return; 494 } 495 icl_pdu_get_data(request, 0, data, datasize); 496 } 497 498 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 499 if (response == NULL) { 500 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; " 501 "droppping connection"); 502 free(data, M_CFISCSI); 503 icl_pdu_free(request); 504 cfiscsi_session_terminate(cs); 505 return; 506 } 507 bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs; 508 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN; 509 bhsni->bhsni_flags = 0x80; 510 bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag; 511 bhsni->bhsni_target_transfer_tag = 0xffffffff; 512 if (datasize > 0) { 513 error = icl_pdu_append_data(response, data, datasize, M_NOWAIT); 514 if (error != 0) { 515 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; " 516 "dropping connection"); 517 free(data, M_CFISCSI); 518 icl_pdu_free(request); 519 icl_pdu_free(response); 520 cfiscsi_session_terminate(cs); 521 return; 522 } 523 free(data, M_CFISCSI); 524 } 525 526 icl_pdu_free(request); 527 cfiscsi_pdu_queue(response); 528} 529 530static void 531cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request) 532{ 533 struct iscsi_bhs_scsi_command *bhssc; 534 struct cfiscsi_session *cs; 535 union ctl_io *io; 536 int error; 537 538 cs = PDU_SESSION(request); 539 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 540 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x", 541 // bhssc->bhssc_initiator_task_tag); 542 543 if (request->ip_data_len > 0 && cs->cs_immediate_data == false) { 544 CFISCSI_SESSION_WARN(cs, "unsolicited data with " 545 "ImmediateData=No; dropping connection"); 546 icl_pdu_free(request); 547 cfiscsi_session_terminate(cs); 548 return; 549 } 550 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref); 551 if (io == NULL) { 552 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; " 553 "dropping connection"); 554 icl_pdu_free(request); 555 cfiscsi_session_terminate(cs); 556 return; 557 } 558 ctl_zero_io(io); 559 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request; 560 io->io_hdr.io_type = CTL_IO_SCSI; 561 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid; 562 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port; 563 io->io_hdr.nexus.targ_target.id = 0; 564 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun); 565 io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun; 566 io->io_hdr.nexus.lun_map_arg = cs; 567 io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag; 568 switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) { 569 case BHSSC_FLAGS_ATTR_UNTAGGED: 570 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 571 break; 572 case BHSSC_FLAGS_ATTR_SIMPLE: 573 io->scsiio.tag_type = CTL_TAG_SIMPLE; 574 break; 575 case BHSSC_FLAGS_ATTR_ORDERED: 576 io->scsiio.tag_type = CTL_TAG_ORDERED; 577 break; 578 case BHSSC_FLAGS_ATTR_HOQ: 579 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE; 580 break; 581 case BHSSC_FLAGS_ATTR_ACA: 582 io->scsiio.tag_type = CTL_TAG_ACA; 583 break; 584 default: 585 io->scsiio.tag_type = CTL_TAG_UNTAGGED; 586 CFISCSI_SESSION_WARN(cs, "unhandled tag type %d", 587 bhssc->bhssc_flags & BHSSC_FLAGS_ATTR); 588 break; 589 } 590 io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */ 591 memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb)); 592 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 593 error = ctl_queue(io); 594 if (error != CTL_RETVAL_COMPLETE) { 595 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; " 596 "dropping connection", error); 597 ctl_free_io(io); 598 refcount_release(&cs->cs_outstanding_ctl_pdus); 599 icl_pdu_free(request); 600 cfiscsi_session_terminate(cs); 601 } 602} 603 604static void 605cfiscsi_pdu_handle_task_request(struct icl_pdu *request) 606{ 607 struct iscsi_bhs_task_management_request *bhstmr; 608 struct iscsi_bhs_task_management_response *bhstmr2; 609 struct icl_pdu *response; 610 struct cfiscsi_session *cs; 611 union ctl_io *io; 612 int error; 613 614 cs = PDU_SESSION(request); 615 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 616 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref); 617 if (io == NULL) { 618 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;" 619 "dropping connection"); 620 icl_pdu_free(request); 621 cfiscsi_session_terminate(cs); 622 return; 623 } 624 ctl_zero_io(io); 625 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request; 626 io->io_hdr.io_type = CTL_IO_TASK; 627 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid; 628 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port; 629 io->io_hdr.nexus.targ_target.id = 0; 630 io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun); 631 io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun; 632 io->io_hdr.nexus.lun_map_arg = cs; 633 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */ 634 635 switch (bhstmr->bhstmr_function & ~0x80) { 636 case BHSTMR_FUNCTION_ABORT_TASK: 637#if 0 638 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK"); 639#endif 640 io->taskio.task_action = CTL_TASK_ABORT_TASK; 641 io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag; 642 break; 643 case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET: 644#if 0 645 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET"); 646#endif 647 io->taskio.task_action = CTL_TASK_LUN_RESET; 648 break; 649 case BHSTMR_FUNCTION_TARGET_WARM_RESET: 650#if 0 651 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET"); 652#endif 653 io->taskio.task_action = CTL_TASK_TARGET_RESET; 654 break; 655 default: 656 CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x", 657 bhstmr->bhstmr_function & ~0x80); 658 ctl_free_io(io); 659 660 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 661 if (response == NULL) { 662 CFISCSI_SESSION_WARN(cs, "failed to allocate memory; " 663 "dropping connection"); 664 icl_pdu_free(request); 665 cfiscsi_session_terminate(cs); 666 return; 667 } 668 bhstmr2 = (struct iscsi_bhs_task_management_response *) 669 response->ip_bhs; 670 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE; 671 bhstmr2->bhstmr_flags = 0x80; 672 bhstmr2->bhstmr_response = 673 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED; 674 bhstmr2->bhstmr_initiator_task_tag = 675 bhstmr->bhstmr_initiator_task_tag; 676 icl_pdu_free(request); 677 cfiscsi_pdu_queue(response); 678 return; 679 } 680 681 refcount_acquire(&cs->cs_outstanding_ctl_pdus); 682 error = ctl_queue(io); 683 if (error != CTL_RETVAL_COMPLETE) { 684 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; " 685 "dropping connection", error); 686 ctl_free_io(io); 687 refcount_release(&cs->cs_outstanding_ctl_pdus); 688 icl_pdu_free(request); 689 cfiscsi_session_terminate(cs); 690 } 691} 692 693static bool 694cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw) 695{ 696 struct iscsi_bhs_data_out *bhsdo; 697 struct cfiscsi_session *cs; 698 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 699 size_t copy_len, len, off, buffer_offset; 700 int ctl_sg_count; 701 union ctl_io *io; 702 703 cs = PDU_SESSION(request); 704 705 KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 706 ISCSI_BHS_OPCODE_SCSI_DATA_OUT || 707 (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 708 ISCSI_BHS_OPCODE_SCSI_COMMAND, 709 ("bad opcode 0x%x", request->ip_bhs->bhs_opcode)); 710 711 /* 712 * We're only using fields common for Data-Out and SCSI Command PDUs. 713 */ 714 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs; 715 716 io = cdw->cdw_ctl_io; 717 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN, 718 ("CTL_FLAG_DATA_IN")); 719 720#if 0 721 CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d", 722 request->ip_data_len, io->scsiio.kern_total_len); 723#endif 724 725 if (io->scsiio.kern_sg_entries > 0) { 726 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 727 ctl_sg_count = io->scsiio.kern_sg_entries; 728 } else { 729 ctl_sglist = &ctl_sg_entry; 730 ctl_sglist->addr = io->scsiio.kern_data_ptr; 731 ctl_sglist->len = io->scsiio.kern_data_len; 732 ctl_sg_count = 1; 733 } 734 735 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 736 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) 737 buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset); 738 else 739 buffer_offset = 0; 740 len = icl_pdu_data_segment_length(request); 741 742 /* 743 * Make sure the offset, as sent by the initiator, matches the offset 744 * we're supposed to be at in the scatter-gather list. 745 */ 746 if (buffer_offset > 747 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled || 748 buffer_offset + len <= 749 io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) { 750 CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, " 751 "expected %zd; dropping connection", buffer_offset, 752 (size_t)io->scsiio.kern_rel_offset + 753 (size_t)io->scsiio.ext_data_filled); 754 ctl_set_data_phase_error(&io->scsiio); 755 cfiscsi_session_terminate(cs); 756 return (true); 757 } 758 759 /* 760 * This is the offset within the PDU data segment, as opposed 761 * to buffer_offset, which is the offset within the task (SCSI 762 * command). 763 */ 764 off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled - 765 buffer_offset; 766 767 /* 768 * Iterate over the scatter/gather segments, filling them with data 769 * from the PDU data segment. Note that this can get called multiple 770 * times for one SCSI command; the cdw structure holds state for the 771 * scatter/gather list. 772 */ 773 for (;;) { 774 KASSERT(cdw->cdw_sg_index < ctl_sg_count, 775 ("cdw->cdw_sg_index >= ctl_sg_count")); 776 if (cdw->cdw_sg_len == 0) { 777 cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr; 778 cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len; 779 } 780 KASSERT(off <= len, ("len > off")); 781 copy_len = len - off; 782 if (copy_len > cdw->cdw_sg_len) 783 copy_len = cdw->cdw_sg_len; 784 785 icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len); 786 cdw->cdw_sg_addr += copy_len; 787 cdw->cdw_sg_len -= copy_len; 788 off += copy_len; 789 io->scsiio.ext_data_filled += copy_len; 790 791 if (cdw->cdw_sg_len == 0) { 792 /* 793 * End of current segment. 794 */ 795 if (cdw->cdw_sg_index == ctl_sg_count - 1) { 796 /* 797 * Last segment in scatter/gather list. 798 */ 799 break; 800 } 801 cdw->cdw_sg_index++; 802 } 803 804 if (off == len) { 805 /* 806 * End of PDU payload. 807 */ 808 break; 809 } 810 } 811 812 if (len > off) { 813 /* 814 * In case of unsolicited data, it's possible that the buffer 815 * provided by CTL is smaller than negotiated FirstBurstLength. 816 * Just ignore the superfluous data; will ask for them with R2T 817 * on next call to cfiscsi_datamove(). 818 * 819 * This obviously can only happen with SCSI Command PDU. 820 */ 821 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 822 ISCSI_BHS_OPCODE_SCSI_COMMAND) 823 return (true); 824 825 CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, " 826 "expected %zd; dropping connection", 827 icl_pdu_data_segment_length(request), off); 828 ctl_set_data_phase_error(&io->scsiio); 829 cfiscsi_session_terminate(cs); 830 return (true); 831 } 832 833 if (io->scsiio.ext_data_filled == io->scsiio.kern_data_len && 834 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) { 835 CFISCSI_SESSION_WARN(cs, "got the final packet without " 836 "the F flag; flags = 0x%x; dropping connection", 837 bhsdo->bhsdo_flags); 838 ctl_set_data_phase_error(&io->scsiio); 839 cfiscsi_session_terminate(cs); 840 return (true); 841 } 842 843 if (io->scsiio.ext_data_filled != io->scsiio.kern_data_len && 844 (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) { 845 if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 846 ISCSI_BHS_OPCODE_SCSI_DATA_OUT) { 847 CFISCSI_SESSION_WARN(cs, "got the final packet, but the " 848 "transmitted size was %zd bytes instead of %d; " 849 "dropping connection", 850 (size_t)io->scsiio.ext_data_filled, 851 io->scsiio.kern_data_len); 852 ctl_set_data_phase_error(&io->scsiio); 853 cfiscsi_session_terminate(cs); 854 return (true); 855 } else { 856 /* 857 * For SCSI Command PDU, this just means we need to 858 * solicit more data by sending R2T. 859 */ 860 return (false); 861 } 862 } 863 864 if (io->scsiio.ext_data_filled == io->scsiio.kern_data_len) { 865#if 0 866 CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target " 867 "transfer tag 0x%x", cdw->cdw_target_transfer_tag); 868#endif 869 870 return (true); 871 } 872 873 return (false); 874} 875 876static void 877cfiscsi_pdu_handle_data_out(struct icl_pdu *request) 878{ 879 struct iscsi_bhs_data_out *bhsdo; 880 struct cfiscsi_session *cs; 881 struct cfiscsi_data_wait *cdw = NULL; 882 union ctl_io *io; 883 bool done; 884 885 cs = PDU_SESSION(request); 886 bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs; 887 888 CFISCSI_SESSION_LOCK(cs); 889 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) { 890#if 0 891 CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for " 892 "ttt 0x%x, itt 0x%x", 893 bhsdo->bhsdo_target_transfer_tag, 894 bhsdo->bhsdo_initiator_task_tag, 895 cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag)); 896#endif 897 if (bhsdo->bhsdo_target_transfer_tag == 898 cdw->cdw_target_transfer_tag) 899 break; 900 } 901 CFISCSI_SESSION_UNLOCK(cs); 902 if (cdw == NULL) { 903 CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag " 904 "0x%x, not found; dropping connection", 905 bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag); 906 icl_pdu_free(request); 907 cfiscsi_session_terminate(cs); 908 return; 909 } 910 911 io = cdw->cdw_ctl_io; 912 KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN, 913 ("CTL_FLAG_DATA_IN")); 914 915 done = cfiscsi_handle_data_segment(request, cdw); 916 if (done) { 917 CFISCSI_SESSION_LOCK(cs); 918 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next); 919 CFISCSI_SESSION_UNLOCK(cs); 920 uma_zfree(cfiscsi_data_wait_zone, cdw); 921 io->scsiio.be_move_done(io); 922 } 923 924 icl_pdu_free(request); 925} 926 927static void 928cfiscsi_pdu_handle_logout_request(struct icl_pdu *request) 929{ 930 struct iscsi_bhs_logout_request *bhslr; 931 struct iscsi_bhs_logout_response *bhslr2; 932 struct icl_pdu *response; 933 struct cfiscsi_session *cs; 934 935 cs = PDU_SESSION(request); 936 bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs; 937 switch (bhslr->bhslr_reason & 0x7f) { 938 case BHSLR_REASON_CLOSE_SESSION: 939 case BHSLR_REASON_CLOSE_CONNECTION: 940 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 941 if (response == NULL) { 942 CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory"); 943 icl_pdu_free(request); 944 cfiscsi_session_terminate(cs); 945 return; 946 } 947 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs; 948 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE; 949 bhslr2->bhslr_flags = 0x80; 950 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY; 951 bhslr2->bhslr_initiator_task_tag = 952 bhslr->bhslr_initiator_task_tag; 953 icl_pdu_free(request); 954 cfiscsi_pdu_queue(response); 955 cfiscsi_session_terminate(cs); 956 break; 957 case BHSLR_REASON_REMOVE_FOR_RECOVERY: 958 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 959 if (response == NULL) { 960 CFISCSI_SESSION_WARN(cs, 961 "failed to allocate memory; dropping connection"); 962 icl_pdu_free(request); 963 cfiscsi_session_terminate(cs); 964 return; 965 } 966 bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs; 967 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE; 968 bhslr2->bhslr_flags = 0x80; 969 bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED; 970 bhslr2->bhslr_initiator_task_tag = 971 bhslr->bhslr_initiator_task_tag; 972 icl_pdu_free(request); 973 cfiscsi_pdu_queue(response); 974 break; 975 default: 976 CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection", 977 bhslr->bhslr_reason); 978 icl_pdu_free(request); 979 cfiscsi_session_terminate(cs); 980 break; 981 } 982} 983 984static void 985cfiscsi_callout(void *context) 986{ 987 struct icl_pdu *cp; 988 struct iscsi_bhs_nop_in *bhsni; 989 struct cfiscsi_session *cs; 990 991 cs = context; 992 993 if (cs->cs_terminating) 994 return; 995 996 callout_schedule(&cs->cs_callout, 1 * hz); 997 998 atomic_add_int(&cs->cs_timeout, 1); 999 1000#ifdef ICL_KERNEL_PROXY 1001 if (cs->cs_waiting_for_ctld || cs->cs_login_phase) { 1002 if (cs->cs_timeout > login_timeout) { 1003 CFISCSI_SESSION_WARN(cs, "login timed out after " 1004 "%d seconds; dropping connection", cs->cs_timeout); 1005 cfiscsi_session_terminate(cs); 1006 } 1007 return; 1008 } 1009#endif 1010 1011 if (cs->cs_timeout >= ping_timeout) { 1012 CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; " 1013 "dropping connection", ping_timeout); 1014 cfiscsi_session_terminate(cs); 1015 return; 1016 } 1017 1018 /* 1019 * If the ping was reset less than one second ago - which means 1020 * that we've received some PDU during the last second - assume 1021 * the traffic flows correctly and don't bother sending a NOP-Out. 1022 * 1023 * (It's 2 - one for one second, and one for incrementing is_timeout 1024 * earlier in this routine.) 1025 */ 1026 if (cs->cs_timeout < 2) 1027 return; 1028 1029 cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT); 1030 if (cp == NULL) { 1031 CFISCSI_SESSION_WARN(cs, "failed to allocate memory"); 1032 return; 1033 } 1034 bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs; 1035 bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN; 1036 bhsni->bhsni_flags = 0x80; 1037 bhsni->bhsni_initiator_task_tag = 0xffffffff; 1038 1039 cfiscsi_pdu_queue(cp); 1040} 1041 1042static void 1043cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs) 1044{ 1045 struct cfiscsi_data_wait *cdw, *tmpcdw; 1046 union ctl_io *io; 1047 int error; 1048 1049#ifdef notyet 1050 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref); 1051 if (io == NULL) { 1052 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io"); 1053 return; 1054 } 1055 ctl_zero_io(io); 1056 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL; 1057 io->io_hdr.io_type = CTL_IO_TASK; 1058 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid; 1059 io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port; 1060 io->io_hdr.nexus.targ_target.id = 0; 1061 io->io_hdr.nexus.targ_lun = lun; 1062 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */ 1063 io->taskio.task_action = CTL_TASK_ABORT_TASK_SET; 1064 error = ctl_queue(io); 1065 if (error != CTL_RETVAL_COMPLETE) { 1066 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error); 1067 ctl_free_io(io); 1068 } 1069#else 1070 /* 1071 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead 1072 * just iterate over tasks that are waiting for something - data - and 1073 * terminate those. 1074 */ 1075 CFISCSI_SESSION_LOCK(cs); 1076 TAILQ_FOREACH_SAFE(cdw, 1077 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) { 1078 io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref); 1079 if (io == NULL) { 1080 CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io"); 1081 return; 1082 } 1083 ctl_zero_io(io); 1084 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL; 1085 io->io_hdr.io_type = CTL_IO_TASK; 1086 io->io_hdr.nexus.initid.id = cs->cs_ctl_initid; 1087 io->io_hdr.nexus.targ_port = 1088 cs->cs_target->ct_softc->fe.targ_port; 1089 io->io_hdr.nexus.targ_target.id = 0; 1090 //io->io_hdr.nexus.targ_lun = lun; /* Not needed? */ 1091 io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */ 1092 io->taskio.task_action = CTL_TASK_ABORT_TASK; 1093 io->taskio.tag_num = cdw->cdw_initiator_task_tag; 1094 error = ctl_queue(io); 1095 if (error != CTL_RETVAL_COMPLETE) { 1096 CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error); 1097 ctl_free_io(io); 1098 return; 1099 } 1100#if 0 1101 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag " 1102 "0x%x", cdw->cdw_initiator_task_tag); 1103#endif 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 1112static void 1113cfiscsi_maintenance_thread(void *arg) 1114{ 1115 struct cfiscsi_session *cs; 1116 1117 cs = arg; 1118 1119 for (;;) { 1120 CFISCSI_SESSION_LOCK(cs); 1121 if (cs->cs_terminating == false) 1122 cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock); 1123 CFISCSI_SESSION_UNLOCK(cs); 1124 1125 if (cs->cs_terminating) { 1126 cfiscsi_session_terminate_tasks(cs); 1127 callout_drain(&cs->cs_callout); 1128 1129 icl_conn_shutdown(cs->cs_conn); 1130 icl_conn_close(cs->cs_conn); 1131 1132 /* 1133 * XXX: We used to wait up to 30 seconds to deliver queued PDUs 1134 * to the initiator. We also tried hard to deliver SCSI Responses 1135 * for the aborted PDUs. We don't do that anymore. We might need 1136 * to revisit that. 1137 */ 1138 1139 cfiscsi_session_delete(cs); 1140 kthread_exit(); 1141 return; 1142 } 1143 CFISCSI_SESSION_DEBUG(cs, "nothing to do"); 1144 } 1145} 1146 1147static void 1148cfiscsi_session_terminate(struct cfiscsi_session *cs) 1149{ 1150 1151 if (cs->cs_terminating) 1152 return; 1153 cs->cs_terminating = true; 1154 cv_signal(&cs->cs_maintenance_cv); 1155#ifdef ICL_KERNEL_PROXY 1156 cv_signal(&cs->cs_login_cv); 1157#endif 1158} 1159 1160static int 1161cfiscsi_session_register_initiator(struct cfiscsi_session *cs) 1162{ 1163 int error, i; 1164 struct cfiscsi_softc *softc; 1165 1166 KASSERT(cs->cs_ctl_initid == -1, ("already registered")); 1167 1168 softc = &cfiscsi_softc; 1169 1170 mtx_lock(&softc->lock); 1171 for (i = 0; i < softc->max_initiators; i++) { 1172 if (softc->ctl_initids[i] == 0) 1173 break; 1174 } 1175 if (i == softc->max_initiators) { 1176 CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)", 1177 softc->max_initiators); 1178 mtx_unlock(&softc->lock); 1179 return (1); 1180 } 1181 softc->ctl_initids[i] = 1; 1182 mtx_unlock(&softc->lock); 1183 1184#if 0 1185 CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d", 1186 i, softc->max_initiators); 1187#endif 1188 cs->cs_ctl_initid = i; 1189 error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid); 1190 if (error != 0) { 1191 CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error); 1192 mtx_lock(&softc->lock); 1193 softc->ctl_initids[cs->cs_ctl_initid] = 0; 1194 mtx_unlock(&softc->lock); 1195 cs->cs_ctl_initid = -1; 1196 return (1); 1197 } 1198 1199 return (0); 1200} 1201 1202static void 1203cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs) 1204{ 1205 int error; 1206 struct cfiscsi_softc *softc; 1207 1208 if (cs->cs_ctl_initid == -1) 1209 return; 1210 1211 softc = &cfiscsi_softc; 1212 1213 error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid); 1214 if (error != 0) { 1215 CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d", 1216 error); 1217 } 1218 mtx_lock(&softc->lock); 1219 softc->ctl_initids[cs->cs_ctl_initid] = 0; 1220 mtx_unlock(&softc->lock); 1221 cs->cs_ctl_initid = -1; 1222} 1223 1224static struct cfiscsi_session * 1225cfiscsi_session_new(struct cfiscsi_softc *softc) 1226{ 1227 struct cfiscsi_session *cs; 1228 int error; 1229 1230 cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO); 1231 if (cs == NULL) { 1232 CFISCSI_WARN("malloc failed"); 1233 return (NULL); 1234 } 1235 cs->cs_ctl_initid = -1; 1236 1237 refcount_init(&cs->cs_outstanding_ctl_pdus, 0); 1238 TAILQ_INIT(&cs->cs_waiting_for_data_out); 1239 mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF); 1240 cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt"); 1241#ifdef ICL_KERNEL_PROXY 1242 cv_init(&cs->cs_login_cv, "cfiscsi_login"); 1243#endif 1244 1245 cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock); 1246 cs->cs_conn->ic_receive = cfiscsi_receive_callback; 1247 cs->cs_conn->ic_error = cfiscsi_error_callback; 1248 cs->cs_conn->ic_prv0 = cs; 1249 1250 error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt"); 1251 if (error != 0) { 1252 CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error); 1253 free(cs, M_CFISCSI); 1254 return (NULL); 1255 } 1256 1257 mtx_lock(&softc->lock); 1258 cs->cs_id = softc->last_session_id + 1; 1259 softc->last_session_id++; 1260 mtx_unlock(&softc->lock); 1261 1262 mtx_lock(&softc->lock); 1263 TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next); 1264 mtx_unlock(&softc->lock); 1265 1266 /* 1267 * Start pinging the initiator. 1268 */ 1269 callout_init(&cs->cs_callout, 1); 1270 callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs); 1271 1272 return (cs); 1273} 1274 1275static void 1276cfiscsi_session_delete(struct cfiscsi_session *cs) 1277{ 1278 struct cfiscsi_softc *softc; 1279 1280 softc = &cfiscsi_softc; 1281 1282 KASSERT(cs->cs_outstanding_ctl_pdus == 0, 1283 ("destroying session with outstanding CTL pdus")); 1284 KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out), 1285 ("destroying session with non-empty queue")); 1286 1287 cfiscsi_session_unregister_initiator(cs); 1288 if (cs->cs_target != NULL) 1289 cfiscsi_target_release(cs->cs_target); 1290 icl_conn_close(cs->cs_conn); 1291 icl_conn_free(cs->cs_conn); 1292 1293 mtx_lock(&softc->lock); 1294 TAILQ_REMOVE(&softc->sessions, cs, cs_next); 1295 mtx_unlock(&softc->lock); 1296 1297 free(cs, M_CFISCSI); 1298} 1299 1300int 1301cfiscsi_init(void) 1302{ 1303 struct cfiscsi_softc *softc; 1304 struct ctl_frontend *fe; 1305 int retval; 1306 1307 softc = &cfiscsi_softc; 1308 retval = 0; 1309 bzero(softc, sizeof(*softc)); 1310 mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF); 1311 1312#ifdef ICL_KERNEL_PROXY 1313 cv_init(&softc->accept_cv, "cfiscsi_accept"); 1314#endif 1315 TAILQ_INIT(&softc->sessions); 1316 TAILQ_INIT(&softc->targets); 1317 1318 fe = &softc->fe; 1319 fe->port_type = CTL_PORT_ISCSI; 1320 /* XXX KDM what should the real number be here? */ 1321 fe->num_requested_ctl_io = 4096; 1322 snprintf(softc->port_name, sizeof(softc->port_name), "iscsi"); 1323 fe->port_name = softc->port_name; 1324 fe->port_online = cfiscsi_online; 1325 fe->port_offline = cfiscsi_offline; 1326 fe->onoff_arg = softc; 1327 fe->targ_enable = cfiscsi_targ_enable; 1328 fe->targ_disable = cfiscsi_targ_disable; 1329 fe->lun_enable = cfiscsi_lun_enable; 1330 fe->lun_disable = cfiscsi_lun_disable; 1331 fe->targ_lun_arg = softc; 1332 fe->ioctl = cfiscsi_ioctl; 1333 fe->devid = cfiscsi_devid; 1334 fe->fe_datamove = cfiscsi_datamove; 1335 fe->fe_done = cfiscsi_done; 1336 1337 /* XXX KDM what should we report here? */ 1338 /* XXX These should probably be fetched from CTL. */ 1339 fe->max_targets = 1; 1340 fe->max_target_id = 15; 1341 1342 retval = ctl_frontend_register(fe, /*master_SC*/ 1); 1343 if (retval != 0) { 1344 CFISCSI_WARN("ctl_frontend_register() failed with error %d", 1345 retval); 1346 retval = 1; 1347 goto bailout; 1348 } 1349 1350 softc->max_initiators = fe->max_initiators; 1351 1352 cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait", 1353 sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL, 1354 UMA_ALIGN_PTR, 0); 1355 1356 return (0); 1357 1358bailout: 1359 return (retval); 1360} 1361 1362static int 1363cfiscsi_module_event_handler(module_t mod, int what, void *arg) 1364{ 1365 1366 switch (what) { 1367 case MOD_LOAD: 1368 return (cfiscsi_init()); 1369 case MOD_UNLOAD: 1370 return (EBUSY); 1371 default: 1372 return (EOPNOTSUPP); 1373 } 1374} 1375 1376#ifdef ICL_KERNEL_PROXY 1377static void 1378cfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id) 1379{ 1380 struct cfiscsi_session *cs; 1381 1382 cs = cfiscsi_session_new(&cfiscsi_softc); 1383 if (cs == NULL) { 1384 CFISCSI_WARN("failed to create session"); 1385 return; 1386 } 1387 1388 icl_conn_handoff_sock(cs->cs_conn, so); 1389 cs->cs_initiator_sa = sa; 1390 cs->cs_portal_id = portal_id; 1391 cs->cs_waiting_for_ctld = true; 1392 cv_signal(&cfiscsi_softc.accept_cv); 1393} 1394#endif 1395 1396static void 1397cfiscsi_online(void *arg) 1398{ 1399 struct cfiscsi_softc *softc; 1400 1401 softc = (struct cfiscsi_softc *)arg; 1402 1403 softc->online = 1; 1404#ifdef ICL_KERNEL_PROXY 1405 if (softc->listener != NULL) 1406 icl_listen_free(softc->listener); 1407 softc->listener = icl_listen_new(cfiscsi_accept); 1408#endif 1409} 1410 1411static void 1412cfiscsi_offline(void *arg) 1413{ 1414 struct cfiscsi_softc *softc; 1415 struct cfiscsi_session *cs; 1416 1417 softc = (struct cfiscsi_softc *)arg; 1418 1419 softc->online = 0; 1420 1421 mtx_lock(&softc->lock); 1422 TAILQ_FOREACH(cs, &softc->sessions, cs_next) 1423 cfiscsi_session_terminate(cs); 1424 mtx_unlock(&softc->lock); 1425 1426#ifdef ICL_KERNEL_PROXY 1427 icl_listen_free(softc->listener); 1428 softc->listener = NULL; 1429#endif 1430} 1431 1432static int 1433cfiscsi_targ_enable(void *arg, struct ctl_id targ_id) 1434{ 1435 1436 return (0); 1437} 1438 1439static int 1440cfiscsi_targ_disable(void *arg, struct ctl_id targ_id) 1441{ 1442 1443 return (0); 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_t10 *t10id; 2035 struct ctl_lun *lun; 2036 const struct icl_pdu *request; 2037 int i, ret; 2038 char *val; 2039 size_t devid_len, wwpn_len, lun_name_len; 2040 2041 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 2042 request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2043 cs = PDU_SESSION(request); 2044 2045 wwpn_len = strlen(cs->cs_target->ct_name); 2046 wwpn_len += strlen(",t,0x0001"); 2047 wwpn_len += 1; /* '\0' */ 2048 if ((wwpn_len % 4) != 0) 2049 wwpn_len += (4 - (wwpn_len % 4)); 2050 2051 if (lun == NULL) { 2052 lun_name_len = 0; 2053 } else { 2054 lun_name_len = strlen(cs->cs_target->ct_name); 2055 lun_name_len += strlen(",lun,XXXXXXXX"); 2056 lun_name_len += 1; /* '\0' */ 2057 if ((lun_name_len % 4) != 0) 2058 lun_name_len += (4 - (lun_name_len % 4)); 2059 } 2060 2061 devid_len = sizeof(struct scsi_vpd_device_id) + 2062 sizeof(struct scsi_vpd_id_descriptor) + 2063 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN + 2064 sizeof(struct scsi_vpd_id_descriptor) + lun_name_len + 2065 sizeof(struct scsi_vpd_id_descriptor) + wwpn_len + 2066 sizeof(struct scsi_vpd_id_descriptor) + 2067 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 2068 sizeof(struct scsi_vpd_id_descriptor) + 2069 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 2070 2071 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO); 2072 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 2073 ctsio->kern_sg_entries = 0; 2074 2075 if (devid_len < alloc_len) { 2076 ctsio->residual = alloc_len - devid_len; 2077 ctsio->kern_data_len = devid_len; 2078 ctsio->kern_total_len = devid_len; 2079 } else { 2080 ctsio->residual = 0; 2081 ctsio->kern_data_len = alloc_len; 2082 ctsio->kern_total_len = alloc_len; 2083 } 2084 ctsio->kern_data_resid = 0; 2085 ctsio->kern_rel_offset = 0; 2086 ctsio->kern_sg_entries = 0; 2087 2088 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 2089 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 2090 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 2091 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN); 2092 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 2093 lun_name_len); 2094 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 2095 wwpn_len); 2096 desc4 = (struct scsi_vpd_id_descriptor *)(&desc3->identifier[0] + 2097 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 2098 2099 if (lun != NULL) 2100 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 2101 lun->be_lun->lun_type; 2102 else 2103 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 2104 2105 devid_ptr->page_code = SVPD_DEVICE_ID; 2106 2107 scsi_ulto2b(devid_len - 4, devid_ptr->length); 2108 2109 /* 2110 * We're using a LUN association here. i.e., this device ID is a 2111 * per-LUN identifier. 2112 */ 2113 desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII; 2114 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 2115 desc->length = sizeof(*t10id) + CTL_DEVID_LEN; 2116 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 2117 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 2118 } else { 2119 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 2120 strncpy(t10id->vendor, val, 2121 min(sizeof(t10id->vendor), strlen(val))); 2122 } 2123 2124 /* 2125 * If we've actually got a backend, copy the device id from the 2126 * per-LUN data. Otherwise, set it to all spaces. 2127 */ 2128 if (lun != NULL) { 2129 /* 2130 * Copy the backend's LUN ID. 2131 */ 2132 strncpy((char *)t10id->vendor_spec_id, 2133 (char *)lun->be_lun->device_id, CTL_DEVID_LEN); 2134 } else { 2135 /* 2136 * No backend, set this to spaces. 2137 */ 2138 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN); 2139 } 2140 2141 /* 2142 * desc1 is for the unique LUN name. 2143 * 2144 * XXX: According to SPC-3, LUN must report the same ID through 2145 * all the ports. The code below, however, reports the 2146 * ID only via iSCSI. 2147 */ 2148 desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2149 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 2150 SVPD_ID_TYPE_SCSI_NAME; 2151 desc1->length = lun_name_len; 2152 if (lun != NULL) { 2153 /* 2154 * Find the per-target LUN number. 2155 */ 2156 for (i = 0; i < CTL_MAX_LUNS; i++) { 2157 if (cs->cs_target->ct_luns[i] == lun->lun) 2158 break; 2159 } 2160 KASSERT(i < CTL_MAX_LUNS, 2161 ("lun %jd not found", (uintmax_t)lun->lun)); 2162 ret = snprintf(desc1->identifier, lun_name_len, "%s,lun,%d", 2163 cs->cs_target->ct_name, i); 2164 KASSERT(ret > 0 && ret <= lun_name_len, ("bad snprintf")); 2165 } else { 2166 KASSERT(lun_name_len == 0, ("no lun, but lun_name_len != 0")); 2167 } 2168 2169 /* 2170 * desc2 is for the WWPN which is a port asscociation. 2171 */ 2172 desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8; 2173 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2174 SVPD_ID_TYPE_SCSI_NAME; 2175 desc2->length = wwpn_len; 2176 snprintf(desc2->identifier, wwpn_len, "%s,t,0x%4.4x", 2177 cs->cs_target->ct_name, cs->cs_portal_group_tag); 2178 2179 /* 2180 * desc3 is for the Relative Target Port(type 4h) identifier 2181 */ 2182 desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY; 2183 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2184 SVPD_ID_TYPE_RELTARG; 2185 desc3->length = 4; 2186 desc3->identifier[3] = 1; 2187 2188 /* 2189 * desc4 is for the Target Port Group(type 5h) identifier 2190 */ 2191 desc4->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY; 2192 desc4->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 2193 SVPD_ID_TYPE_TPORTGRP; 2194 desc4->length = 4; 2195 desc4->identifier[3] = 1; 2196 2197 ctsio->scsi_status = SCSI_STATUS_OK; 2198 2199 ctsio->be_move_done = ctl_config_move_done; 2200 ctl_datamove((union ctl_io *)ctsio); 2201 2202 return (CTL_RETVAL_COMPLETE); 2203} 2204 2205static void 2206cfiscsi_target_hold(struct cfiscsi_target *ct) 2207{ 2208 2209 refcount_acquire(&ct->ct_refcount); 2210} 2211 2212static void 2213cfiscsi_target_release(struct cfiscsi_target *ct) 2214{ 2215 struct cfiscsi_softc *softc; 2216 2217 softc = ct->ct_softc; 2218 mtx_lock(&softc->lock); 2219 if (refcount_release(&ct->ct_refcount)) { 2220 TAILQ_REMOVE(&softc->targets, ct, ct_next); 2221 mtx_unlock(&softc->lock); 2222 free(ct, M_CFISCSI); 2223 2224 return; 2225 } 2226 mtx_unlock(&softc->lock); 2227} 2228 2229static struct cfiscsi_target * 2230cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name) 2231{ 2232 struct cfiscsi_target *ct; 2233 2234 mtx_lock(&softc->lock); 2235 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2236 if (strcmp(name, ct->ct_name) != 0) 2237 continue; 2238 cfiscsi_target_hold(ct); 2239 mtx_unlock(&softc->lock); 2240 return (ct); 2241 } 2242 mtx_unlock(&softc->lock); 2243 2244 return (NULL); 2245} 2246 2247static struct cfiscsi_target * 2248cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name, 2249 const char *alias) 2250{ 2251 struct cfiscsi_target *ct, *newct; 2252 int i; 2253 2254 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN) 2255 return (NULL); 2256 2257 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO); 2258 2259 mtx_lock(&softc->lock); 2260 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2261 if (strcmp(name, ct->ct_name) != 0) 2262 continue; 2263 cfiscsi_target_hold(ct); 2264 mtx_unlock(&softc->lock); 2265 free(newct, M_CFISCSI); 2266 return (ct); 2267 } 2268 2269 for (i = 0; i < CTL_MAX_LUNS; i++) 2270 newct->ct_luns[i] = -1; 2271 2272 strlcpy(newct->ct_name, name, sizeof(newct->ct_name)); 2273 if (alias != NULL) 2274 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias)); 2275 refcount_init(&newct->ct_refcount, 1); 2276 newct->ct_softc = softc; 2277 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next); 2278 mtx_unlock(&softc->lock); 2279 2280 return (newct); 2281} 2282 2283/* 2284 * Takes LUN from the target space and returns LUN from the CTL space. 2285 */ 2286static uint32_t 2287cfiscsi_map_lun(void *arg, uint32_t lun) 2288{ 2289 struct cfiscsi_session *cs; 2290 2291 cs = arg; 2292 2293 if (lun >= CTL_MAX_LUNS) { 2294 CFISCSI_DEBUG("requested lun number %d is higher " 2295 "than maximum %d", lun, CTL_MAX_LUNS - 1); 2296 return (0xffffffff); 2297 } 2298 2299 if (cs->cs_target->ct_luns[lun] < 0) 2300 return (0xffffffff); 2301 2302 return (cs->cs_target->ct_luns[lun]); 2303} 2304 2305static int 2306cfiscsi_target_set_lun(struct cfiscsi_target *ct, 2307 unsigned long lun_id, unsigned long ctl_lun_id) 2308{ 2309 2310 if (lun_id >= CTL_MAX_LUNS) { 2311 CFISCSI_WARN("requested lun number %ld is higher " 2312 "than maximum %d", lun_id, CTL_MAX_LUNS - 1); 2313 return (-1); 2314 } 2315 2316 if (ct->ct_luns[lun_id] >= 0) { 2317 /* 2318 * CTL calls cfiscsi_lun_enable() twice for each LUN - once 2319 * when the LUN is created, and a second time just before 2320 * the port is brought online; don't emit warnings 2321 * for that case. 2322 */ 2323 if (ct->ct_luns[lun_id] == ctl_lun_id) 2324 return (0); 2325 CFISCSI_WARN("lun %ld already allocated", lun_id); 2326 return (-1); 2327 } 2328 2329#if 0 2330 CFISCSI_DEBUG("adding mapping for lun %ld, target %s " 2331 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id); 2332#endif 2333 2334 ct->ct_luns[lun_id] = ctl_lun_id; 2335 cfiscsi_target_hold(ct); 2336 2337 return (0); 2338} 2339 2340static int 2341cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id) 2342{ 2343 2344 if (ct->ct_luns[lun_id] < 0) { 2345 CFISCSI_WARN("lun %ld not allocated", lun_id); 2346 return (-1); 2347 } 2348 2349 ct->ct_luns[lun_id] = -1; 2350 cfiscsi_target_release(ct); 2351 2352 return (0); 2353} 2354 2355static int 2356cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id) 2357{ 2358 struct cfiscsi_softc *softc; 2359 struct cfiscsi_target *ct; 2360 const char *target = NULL, *target_alias = NULL; 2361 const char *lun = NULL; 2362 unsigned long tmp; 2363 2364 softc = (struct cfiscsi_softc *)arg; 2365 2366 target = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun, 2367 "cfiscsi_target"); 2368 target_alias = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun, 2369 "cfiscsi_target_alias"); 2370 lun = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun, 2371 "cfiscsi_lun"); 2372 2373 if (target == NULL && lun == NULL) 2374 return (0); 2375 2376 if (target == NULL || lun == NULL) { 2377 CFISCSI_WARN("lun added with cfiscsi_target, but without " 2378 "cfiscsi_lun, or the other way around; ignoring"); 2379 return (0); 2380 } 2381 2382 ct = cfiscsi_target_find_or_create(softc, target, target_alias); 2383 if (ct == NULL) { 2384 CFISCSI_WARN("failed to create target \"%s\"", target); 2385 return (0); 2386 } 2387 2388 tmp = strtoul(lun, NULL, 10); 2389 cfiscsi_target_set_lun(ct, tmp, lun_id); 2390 cfiscsi_target_release(ct); 2391 return (0); 2392} 2393 2394static int 2395cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id) 2396{ 2397 struct cfiscsi_softc *softc; 2398 struct cfiscsi_target *ct; 2399 int i; 2400 2401 softc = (struct cfiscsi_softc *)arg; 2402 2403 mtx_lock(&softc->lock); 2404 TAILQ_FOREACH(ct, &softc->targets, ct_next) { 2405 for (i = 0; i < CTL_MAX_LUNS; i++) { 2406 if (ct->ct_luns[i] < 0) 2407 continue; 2408 if (ct->ct_luns[i] != lun_id) 2409 continue; 2410 mtx_unlock(&softc->lock); 2411 cfiscsi_target_unset_lun(ct, i); 2412 return (0); 2413 } 2414 } 2415 mtx_unlock(&softc->lock); 2416 return (0); 2417} 2418 2419static void 2420cfiscsi_datamove_in(union ctl_io *io) 2421{ 2422 struct cfiscsi_session *cs; 2423 struct icl_pdu *request, *response; 2424 const struct iscsi_bhs_scsi_command *bhssc; 2425 struct iscsi_bhs_data_in *bhsdi; 2426 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist; 2427 size_t len, expected_len, sg_len, buffer_offset; 2428 const char *sg_addr; 2429 int ctl_sg_count, error, i; 2430 2431 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2432 cs = PDU_SESSION(request); 2433 2434 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2435 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2436 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2437 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2438 2439 if (io->scsiio.kern_sg_entries > 0) { 2440 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 2441 ctl_sg_count = io->scsiio.kern_sg_entries; 2442 } else { 2443 ctl_sglist = &ctl_sg_entry; 2444 ctl_sglist->addr = io->scsiio.kern_data_ptr; 2445 ctl_sglist->len = io->scsiio.kern_data_len; 2446 ctl_sg_count = 1; 2447 } 2448 2449 /* 2450 * This is the total amount of data to be transferred within the current 2451 * SCSI command. We need to record it so that we can properly report 2452 * underflow/underflow. 2453 */ 2454 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2455 2456 /* 2457 * This is the offset within the current SCSI command; for the first 2458 * call to cfiscsi_datamove() it will be 0, and for subsequent ones 2459 * it will be the sum of lengths of previous ones. 2460 */ 2461 buffer_offset = io->scsiio.kern_rel_offset; 2462 2463 /* 2464 * This is the transfer length expected by the initiator. In theory, 2465 * it could be different from the correct amount of data from the SCSI 2466 * point of view, even if that doesn't make any sense. 2467 */ 2468 expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length); 2469#if 0 2470 if (expected_len != io->scsiio.kern_total_len) { 2471 CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, " 2472 "actual length %zd", expected_len, 2473 (size_t)io->scsiio.kern_total_len); 2474 } 2475#endif 2476 2477 if (buffer_offset >= expected_len) { 2478#if 0 2479 CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, " 2480 "already sent the expected len", buffer_offset); 2481#endif 2482 io->scsiio.be_move_done(io); 2483 return; 2484 } 2485 2486 i = 0; 2487 sg_addr = NULL; 2488 sg_len = 0; 2489 response = NULL; 2490 bhsdi = NULL; 2491 for (;;) { 2492 if (response == NULL) { 2493 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2494 if (response == NULL) { 2495 CFISCSI_SESSION_WARN(cs, "failed to " 2496 "allocate memory; dropping connection"); 2497 ctl_set_busy(&io->scsiio); 2498 io->scsiio.be_move_done(io); 2499 cfiscsi_session_terminate(cs); 2500 return; 2501 } 2502 bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs; 2503 bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN; 2504 bhsdi->bhsdi_initiator_task_tag = 2505 bhssc->bhssc_initiator_task_tag; 2506 bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request)); 2507 PDU_EXPDATASN(request)++; 2508 bhsdi->bhsdi_buffer_offset = htonl(buffer_offset); 2509 } 2510 2511 KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count")); 2512 if (sg_len == 0) { 2513 sg_addr = ctl_sglist[i].addr; 2514 sg_len = ctl_sglist[i].len; 2515 KASSERT(sg_len > 0, ("sg_len <= 0")); 2516 } 2517 2518 len = sg_len; 2519 2520 /* 2521 * Truncate to maximum data segment length. 2522 */ 2523 KASSERT(response->ip_data_len < cs->cs_max_data_segment_length, 2524 ("ip_data_len %zd >= max_data_segment_length %zd", 2525 response->ip_data_len, cs->cs_max_data_segment_length)); 2526 if (response->ip_data_len + len > 2527 cs->cs_max_data_segment_length) { 2528 len = cs->cs_max_data_segment_length - 2529 response->ip_data_len; 2530 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2531 len, sg_len)); 2532 } 2533 2534 /* 2535 * Truncate to expected data transfer length. 2536 */ 2537 KASSERT(buffer_offset + response->ip_data_len < expected_len, 2538 ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd", 2539 buffer_offset, response->ip_data_len, expected_len)); 2540 if (buffer_offset + response->ip_data_len + len > expected_len) { 2541 CFISCSI_SESSION_DEBUG(cs, "truncating from %zd " 2542 "to expected data transfer length %zd", 2543 buffer_offset + response->ip_data_len + len, expected_len); 2544 len = expected_len - (buffer_offset + response->ip_data_len); 2545 KASSERT(len <= sg_len, ("len %zd > sg_len %zd", 2546 len, sg_len)); 2547 } 2548 2549 error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT); 2550 if (error != 0) { 2551 CFISCSI_SESSION_WARN(cs, "failed to " 2552 "allocate memory; dropping connection"); 2553 icl_pdu_free(response); 2554 ctl_set_busy(&io->scsiio); 2555 io->scsiio.be_move_done(io); 2556 cfiscsi_session_terminate(cs); 2557 return; 2558 } 2559 sg_addr += len; 2560 sg_len -= len; 2561 2562 KASSERT(buffer_offset + request->ip_data_len <= expected_len, 2563 ("buffer_offset %zd + ip_data_len %zd > expected_len %zd", 2564 buffer_offset, request->ip_data_len, expected_len)); 2565 if (buffer_offset + request->ip_data_len == expected_len) { 2566 /* 2567 * Already have the amount of data the initiator wanted. 2568 */ 2569 break; 2570 } 2571 2572 if (sg_len == 0) { 2573 /* 2574 * End of scatter-gather segment; 2575 * proceed to the next one... 2576 */ 2577 if (i == ctl_sg_count - 1) { 2578 /* 2579 * ... unless this was the last one. 2580 */ 2581 break; 2582 } 2583 i++; 2584 } 2585 2586 if (response->ip_data_len == cs->cs_max_data_segment_length) { 2587 /* 2588 * Can't stuff more data into the current PDU; 2589 * queue it. Note that's not enough to check 2590 * for kern_data_resid == 0 instead; there 2591 * may be several Data-In PDUs for the final 2592 * call to cfiscsi_datamove(), and we want 2593 * to set the F flag only on the last of them. 2594 */ 2595 buffer_offset += response->ip_data_len; 2596 if (buffer_offset == io->scsiio.kern_total_len || 2597 buffer_offset == expected_len) 2598 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F; 2599 cfiscsi_pdu_queue(response); 2600 response = NULL; 2601 bhsdi = NULL; 2602 } 2603 } 2604 if (response != NULL) { 2605 buffer_offset += response->ip_data_len; 2606 if (buffer_offset == io->scsiio.kern_total_len || 2607 buffer_offset == expected_len) 2608 bhsdi->bhsdi_flags |= BHSDI_FLAGS_F; 2609 KASSERT(response->ip_data_len > 0, ("sending empty Data-In")); 2610 cfiscsi_pdu_queue(response); 2611 } 2612 2613 io->scsiio.be_move_done(io); 2614} 2615 2616static void 2617cfiscsi_datamove_out(union ctl_io *io) 2618{ 2619 struct cfiscsi_session *cs; 2620 struct icl_pdu *request, *response; 2621 const struct iscsi_bhs_scsi_command *bhssc; 2622 struct iscsi_bhs_r2t *bhsr2t; 2623 struct cfiscsi_data_wait *cdw; 2624 uint32_t target_transfer_tag; 2625 bool done; 2626 2627 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2628 cs = PDU_SESSION(request); 2629 2630 bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs; 2631 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2632 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2633 ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND")); 2634 2635 /* 2636 * We need to record it so that we can properly report 2637 * underflow/underflow. 2638 */ 2639 PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len; 2640 2641 /* 2642 * We hadn't received anything during this datamove yet. 2643 */ 2644 io->scsiio.ext_data_filled = 0; 2645 2646 target_transfer_tag = 2647 atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1); 2648 2649#if 0 2650 CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator " 2651 "task tag 0x%x, target transfer tag 0x%x", 2652 bhssc->bhssc_initiator_task_tag, target_transfer_tag); 2653#endif 2654 cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO); 2655 if (cdw == NULL) { 2656 CFISCSI_SESSION_WARN(cs, "failed to " 2657 "allocate memory; dropping connection"); 2658 ctl_set_busy(&io->scsiio); 2659 io->scsiio.be_move_done(io); 2660 cfiscsi_session_terminate(cs); 2661 return; 2662 } 2663 cdw->cdw_ctl_io = io; 2664 cdw->cdw_target_transfer_tag = target_transfer_tag; 2665 cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2666 2667 if (cs->cs_immediate_data && io->scsiio.kern_rel_offset < 2668 icl_pdu_data_segment_length(request)) { 2669 done = cfiscsi_handle_data_segment(request, cdw); 2670 if (done) { 2671 uma_zfree(cfiscsi_data_wait_zone, cdw); 2672 io->scsiio.be_move_done(io); 2673 return; 2674 } 2675 } 2676 2677 CFISCSI_SESSION_LOCK(cs); 2678 TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next); 2679 CFISCSI_SESSION_UNLOCK(cs); 2680 2681 /* 2682 * XXX: We should limit the number of outstanding R2T PDUs 2683 * per task to MaxOutstandingR2T. 2684 */ 2685 response = cfiscsi_pdu_new_response(request, M_NOWAIT); 2686 if (response == NULL) { 2687 CFISCSI_SESSION_WARN(cs, "failed to " 2688 "allocate memory; dropping connection"); 2689 ctl_set_busy(&io->scsiio); 2690 io->scsiio.be_move_done(io); 2691 cfiscsi_session_terminate(cs); 2692 return; 2693 } 2694 bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs; 2695 bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T; 2696 bhsr2t->bhsr2t_flags = 0x80; 2697 bhsr2t->bhsr2t_lun = bhssc->bhssc_lun; 2698 bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2699 bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag; 2700 /* 2701 * XXX: Here we assume that cfiscsi_datamove() won't ever 2702 * be running concurrently on several CPUs for a given 2703 * command. 2704 */ 2705 bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request)); 2706 PDU_R2TSN(request)++; 2707 /* 2708 * This is the offset within the current SCSI command; 2709 * i.e. for the first call of datamove(), it will be 0, 2710 * and for subsequent ones it will be the sum of lengths 2711 * of previous ones. 2712 * 2713 * The ext_data_filled is to account for unsolicited 2714 * (immediate) data that might have already arrived. 2715 */ 2716 bhsr2t->bhsr2t_buffer_offset = 2717 htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled); 2718 /* 2719 * This is the total length (sum of S/G lengths) this call 2720 * to cfiscsi_datamove() is supposed to handle. 2721 * 2722 * XXX: Limit it to MaxBurstLength. 2723 */ 2724 bhsr2t->bhsr2t_desired_data_transfer_length = 2725 htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled); 2726 cfiscsi_pdu_queue(response); 2727} 2728 2729static void 2730cfiscsi_datamove(union ctl_io *io) 2731{ 2732 2733 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 2734 cfiscsi_datamove_in(io); 2735 else 2736 cfiscsi_datamove_out(io); 2737} 2738 2739static void 2740cfiscsi_scsi_command_done(union ctl_io *io) 2741{ 2742 struct icl_pdu *request, *response; 2743 struct iscsi_bhs_scsi_command *bhssc; 2744 struct iscsi_bhs_scsi_response *bhssr; 2745#ifdef DIAGNOSTIC 2746 struct cfiscsi_data_wait *cdw; 2747#endif 2748 struct cfiscsi_session *cs; 2749 uint16_t sense_length; 2750 2751 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2752 cs = PDU_SESSION(request); 2753 bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs; 2754 KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2755 ISCSI_BHS_OPCODE_SCSI_COMMAND, 2756 ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode)); 2757 2758 //CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x", 2759 // bhssc->bhssc_initiator_task_tag); 2760 2761#ifdef DIAGNOSTIC 2762 CFISCSI_SESSION_LOCK(cs); 2763 TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) 2764 KASSERT(bhssc->bhssc_initiator_task_tag != 2765 cdw->cdw_initiator_task_tag, ("dangling cdw")); 2766 CFISCSI_SESSION_UNLOCK(cs); 2767#endif 2768 2769 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2770 bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs; 2771 bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE; 2772 bhssr->bhssr_flags = 0x80; 2773 /* 2774 * XXX: We don't deal with bidirectional under/overflows; 2775 * does anything actually support those? 2776 */ 2777 if (PDU_TOTAL_TRANSFER_LEN(request) < 2778 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2779 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW; 2780 bhssr->bhssr_residual_count = 2781 htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) - 2782 PDU_TOTAL_TRANSFER_LEN(request)); 2783 //CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d", 2784 // ntohl(bhssr->bhssr_residual_count)); 2785 } else if (PDU_TOTAL_TRANSFER_LEN(request) > 2786 ntohl(bhssc->bhssc_expected_data_transfer_length)) { 2787 bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW; 2788 bhssr->bhssr_residual_count = 2789 htonl(PDU_TOTAL_TRANSFER_LEN(request) - 2790 ntohl(bhssc->bhssc_expected_data_transfer_length)); 2791 //CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d", 2792 // ntohl(bhssr->bhssr_residual_count)); 2793 } 2794 bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED; 2795 bhssr->bhssr_status = io->scsiio.scsi_status; 2796 bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag; 2797 bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request)); 2798 2799 if (io->scsiio.sense_len > 0) { 2800#if 0 2801 CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data", 2802 io->scsiio.sense_len); 2803#endif 2804 sense_length = htons(io->scsiio.sense_len); 2805 icl_pdu_append_data(response, 2806 &sense_length, sizeof(sense_length), M_WAITOK); 2807 icl_pdu_append_data(response, 2808 &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK); 2809 } 2810 2811 ctl_free_io(io); 2812 icl_pdu_free(request); 2813 cfiscsi_pdu_queue(response); 2814} 2815 2816static void 2817cfiscsi_task_management_done(union ctl_io *io) 2818{ 2819 struct icl_pdu *request, *response; 2820 struct iscsi_bhs_task_management_request *bhstmr; 2821 struct iscsi_bhs_task_management_response *bhstmr2; 2822 struct cfiscsi_data_wait *cdw, *tmpcdw; 2823 struct cfiscsi_session *cs; 2824 2825 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2826 cs = PDU_SESSION(request); 2827 bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs; 2828 KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) == 2829 ISCSI_BHS_OPCODE_TASK_REQUEST, 2830 ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode)); 2831 2832#if 0 2833 CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x", 2834 bhstmr->bhstmr_initiator_task_tag, 2835 bhstmr->bhstmr_referenced_task_tag); 2836#endif 2837 2838 if ((bhstmr->bhstmr_function & ~0x80) == 2839 BHSTMR_FUNCTION_ABORT_TASK) { 2840 /* 2841 * Make sure we no longer wait for Data-Out for this command. 2842 */ 2843 CFISCSI_SESSION_LOCK(cs); 2844 TAILQ_FOREACH_SAFE(cdw, 2845 &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) { 2846 if (bhstmr->bhstmr_referenced_task_tag != 2847 cdw->cdw_initiator_task_tag) 2848 continue; 2849 2850#if 0 2851 CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task " 2852 "tag 0x%x", bhstmr->bhstmr_initiator_task_tag); 2853#endif 2854 TAILQ_REMOVE(&cs->cs_waiting_for_data_out, 2855 cdw, cdw_next); 2856 cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io); 2857 uma_zfree(cfiscsi_data_wait_zone, cdw); 2858 } 2859 CFISCSI_SESSION_UNLOCK(cs); 2860 } 2861 2862 response = cfiscsi_pdu_new_response(request, M_WAITOK); 2863 bhstmr2 = (struct iscsi_bhs_task_management_response *) 2864 response->ip_bhs; 2865 bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE; 2866 bhstmr2->bhstmr_flags = 0x80; 2867 if (io->io_hdr.status == CTL_SUCCESS) { 2868 bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE; 2869 } else { 2870 /* 2871 * XXX: How to figure out what exactly went wrong? iSCSI spec 2872 * expects us to provide detailed error, e.g. "Task does 2873 * not exist" or "LUN does not exist". 2874 */ 2875 CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED"); 2876 bhstmr2->bhstmr_response = 2877 BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED; 2878 } 2879 bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag; 2880 2881 ctl_free_io(io); 2882 icl_pdu_free(request); 2883 cfiscsi_pdu_queue(response); 2884} 2885 2886static void 2887cfiscsi_done(union ctl_io *io) 2888{ 2889 struct icl_pdu *request; 2890 struct cfiscsi_session *cs; 2891 2892 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE), 2893 ("invalid CTL status %#x", io->io_hdr.status)); 2894 2895 request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2896 if (request == NULL) { 2897 /* 2898 * Implicit task termination has just completed; nothing to do. 2899 */ 2900 return; 2901 } 2902 2903 cs = PDU_SESSION(request); 2904 refcount_release(&cs->cs_outstanding_ctl_pdus); 2905 2906 switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) { 2907 case ISCSI_BHS_OPCODE_SCSI_COMMAND: 2908 cfiscsi_scsi_command_done(io); 2909 break; 2910 case ISCSI_BHS_OPCODE_TASK_REQUEST: 2911 cfiscsi_task_management_done(io); 2912 break; 2913 default: 2914 panic("cfiscsi_done called with wrong opcode 0x%x", 2915 request->ip_bhs->bhs_opcode); 2916 } 2917} 2918