1 // SPDX-License-Identifier: GPL-2.0+ 2 /******************************************************************************* 3 * Vhost kernel TCM fabric driver for virtio SCSI initiators 4 * 5 * (C) Copyright 2010-2013 Datera, Inc. 6 * (C) Copyright 2010-2012 IBM Corp. 7 * 8 * Authors: Nicholas A. Bellinger <nab@daterainc.com> 9 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 10 ****************************************************************************/ 11 12 #include <linux/module.h> 13 #include <linux/moduleparam.h> 14 #include <generated/utsrelease.h> 15 #include <linux/utsname.h> 16 #include <linux/init.h> 17 #include <linux/slab.h> 18 #include <linux/kthread.h> 19 #include <linux/types.h> 20 #include <linux/string.h> 21 #include <linux/configfs.h> 22 #include <linux/ctype.h> 23 #include <linux/compat.h> 24 #include <linux/eventfd.h> 25 #include <linux/fs.h> 26 #include <linux/vmalloc.h> 27 #include <linux/miscdevice.h> 28 #include <linux/blk_types.h> 29 #include <linux/bio.h> 30 #include <linux/unaligned.h> 31 #include <scsi/scsi_common.h> 32 #include <scsi/scsi_proto.h> 33 #include <target/target_core_base.h> 34 #include <target/target_core_fabric.h> 35 #include <linux/vhost.h> 36 #include <linux/virtio_scsi.h> 37 #include <linux/llist.h> 38 #include <linux/bitmap.h> 39 40 #include "vhost.h" 41 42 #define VHOST_SCSI_VERSION "v0.1" 43 #define VHOST_SCSI_NAMELEN 256 44 #define VHOST_SCSI_MAX_CDB_SIZE 32 45 #define VHOST_SCSI_PREALLOC_SGLS 2048 46 #define VHOST_SCSI_PREALLOC_UPAGES 2048 47 #define VHOST_SCSI_PREALLOC_PROT_SGLS 2048 48 /* 49 * For the legacy descriptor case we allocate an iov per byte in the 50 * virtio_scsi_cmd_resp struct. 51 */ 52 #define VHOST_SCSI_MAX_RESP_IOVS sizeof(struct virtio_scsi_cmd_resp) 53 54 static unsigned int vhost_scsi_inline_sg_cnt = VHOST_SCSI_PREALLOC_SGLS; 55 56 #ifdef CONFIG_ARCH_NO_SG_CHAIN 57 static int vhost_scsi_set_inline_sg_cnt(const char *buf, 58 const struct kernel_param *kp) 59 { 60 pr_err("Setting inline_sg_cnt is not supported.\n"); 61 return -EOPNOTSUPP; 62 } 63 #else 64 static int vhost_scsi_set_inline_sg_cnt(const char *buf, 65 const struct kernel_param *kp) 66 { 67 unsigned int cnt; 68 int ret; 69 70 ret = kstrtouint(buf, 10, &cnt); 71 if (ret) 72 return ret; 73 74 if (cnt > VHOST_SCSI_PREALLOC_SGLS) { 75 pr_err("Max inline_sg_cnt is %u\n", VHOST_SCSI_PREALLOC_SGLS); 76 return -EINVAL; 77 } 78 79 vhost_scsi_inline_sg_cnt = cnt; 80 return 0; 81 } 82 #endif 83 84 static int vhost_scsi_get_inline_sg_cnt(char *buf, 85 const struct kernel_param *kp) 86 { 87 return sprintf(buf, "%u\n", vhost_scsi_inline_sg_cnt); 88 } 89 90 static const struct kernel_param_ops vhost_scsi_inline_sg_cnt_op = { 91 .get = vhost_scsi_get_inline_sg_cnt, 92 .set = vhost_scsi_set_inline_sg_cnt, 93 }; 94 95 module_param_cb(inline_sg_cnt, &vhost_scsi_inline_sg_cnt_op, NULL, 0644); 96 MODULE_PARM_DESC(inline_sg_cnt, "Set the number of scatterlist entries to pre-allocate. The default is 2048."); 97 98 /* Max number of requests before requeueing the job. 99 * Using this limit prevents one virtqueue from starving others with 100 * request. 101 */ 102 #define VHOST_SCSI_WEIGHT 256 103 104 struct vhost_scsi_inflight { 105 /* Wait for the flush operation to finish */ 106 struct completion comp; 107 /* Refcount for the inflight reqs */ 108 struct kref kref; 109 }; 110 111 struct vhost_scsi_cmd { 112 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */ 113 int tvc_vq_desc; 114 /* The number of scatterlists associated with this cmd */ 115 u32 tvc_sgl_count; 116 u32 tvc_prot_sgl_count; 117 u32 copied_iov:1; 118 u32 tvc_need_unpin:1; 119 u32 tvc_prot_need_unpin:1; 120 const void *read_iov; 121 struct iov_iter *read_iter; 122 struct scatterlist *sgl; 123 struct sg_table table; 124 struct scatterlist *prot_sgl; 125 struct sg_table prot_table; 126 /* Fast path response header iovec used when only one vec is needed */ 127 struct iovec tvc_resp_iov; 128 /* Number of iovs for response */ 129 unsigned int tvc_resp_iovs_cnt; 130 /* Pointer to response header iovecs if more than one is needed */ 131 struct iovec *tvc_resp_iovs; 132 /* Pointer to vhost_virtqueue for the cmd */ 133 struct vhost_virtqueue *tvc_vq; 134 /* The TCM I/O descriptor that is accessed via container_of() */ 135 struct se_cmd tvc_se_cmd; 136 /* Sense buffer that will be mapped into outgoing status */ 137 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER]; 138 /* 139 * Dirty write descriptors of this command. 140 */ 141 struct vhost_log *tvc_log; 142 unsigned int tvc_log_num; 143 /* Completed commands list, serviced from vhost worker thread */ 144 struct llist_node tvc_completion_list; 145 /* Used to track inflight cmd */ 146 struct vhost_scsi_inflight *inflight; 147 }; 148 149 struct vhost_scsi_nexus { 150 /* Pointer to TCM session for I_T Nexus */ 151 struct se_session *tvn_se_sess; 152 }; 153 154 struct vhost_scsi_tpg { 155 /* Vhost port target portal group tag for TCM */ 156 u16 tport_tpgt; 157 /* Used to track number of TPG Port/Lun Links wrt to explicit I_T Nexus shutdown */ 158 int tv_tpg_port_count; 159 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */ 160 int tv_tpg_vhost_count; 161 /* Used for enabling T10-PI with legacy devices */ 162 int tv_fabric_prot_type; 163 /* list for vhost_scsi_list */ 164 struct list_head tv_tpg_list; 165 /* Used to protect access for tpg_nexus */ 166 struct mutex tv_tpg_mutex; 167 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */ 168 struct vhost_scsi_nexus *tpg_nexus; 169 /* Pointer back to vhost_scsi_tport */ 170 struct vhost_scsi_tport *tport; 171 /* Returned by vhost_scsi_make_tpg() */ 172 struct se_portal_group se_tpg; 173 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */ 174 struct vhost_scsi *vhost_scsi; 175 }; 176 177 struct vhost_scsi_tport { 178 /* SCSI protocol the tport is providing */ 179 u8 tport_proto_id; 180 /* Binary World Wide unique Port Name for Vhost Target port */ 181 u64 tport_wwpn; 182 /* ASCII formatted WWPN for Vhost Target port */ 183 char tport_name[VHOST_SCSI_NAMELEN]; 184 /* Returned by vhost_scsi_make_tport() */ 185 struct se_wwn tport_wwn; 186 }; 187 188 struct vhost_scsi_evt { 189 /* event to be sent to guest */ 190 struct virtio_scsi_event event; 191 /* event list, serviced from vhost worker thread */ 192 struct llist_node list; 193 }; 194 195 enum { 196 VHOST_SCSI_VQ_CTL = 0, 197 VHOST_SCSI_VQ_EVT = 1, 198 VHOST_SCSI_VQ_IO = 2, 199 }; 200 201 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */ 202 static const int vhost_scsi_bits[] = { 203 VHOST_FEATURES, 204 VIRTIO_SCSI_F_HOTPLUG, 205 VIRTIO_SCSI_F_T10_PI 206 }; 207 208 #define VHOST_SCSI_FEATURES VHOST_FEATURES_U64(vhost_scsi_bits, 0) 209 210 #define VHOST_SCSI_MAX_TARGET 256 211 #define VHOST_SCSI_MAX_IO_VQ 1024 212 #define VHOST_SCSI_MAX_EVENT 128 213 214 static unsigned vhost_scsi_max_io_vqs = 128; 215 216 static int vhost_scsi_set_max_io_vqs(const char *val, 217 const struct kernel_param *kp) 218 { 219 unsigned int max_io_vqs; 220 int ret; 221 222 ret = kstrtouint(val, 0, &max_io_vqs); 223 if (ret) 224 return ret; 225 226 if (max_io_vqs > VHOST_SCSI_MAX_IO_VQ) { 227 pr_err("Invalid max_io_vqs of %u. Using %u.\n", 228 max_io_vqs, VHOST_SCSI_MAX_IO_VQ); 229 max_io_vqs = VHOST_SCSI_MAX_IO_VQ; 230 } else if (!max_io_vqs) { 231 pr_err("Invalid max_io_vqs of 0. Using 1.\n"); 232 max_io_vqs = 1; 233 } 234 235 WRITE_ONCE(vhost_scsi_max_io_vqs, max_io_vqs); 236 return 0; 237 } 238 239 static const struct kernel_param_ops vhost_scsi_max_io_vqs_op = { 240 .set = vhost_scsi_set_max_io_vqs, 241 .get = param_get_uint, 242 }; 243 244 module_param_cb(max_io_vqs, &vhost_scsi_max_io_vqs_op, 245 &vhost_scsi_max_io_vqs, 0644); 246 MODULE_PARM_DESC(max_io_vqs, "Set the max number of IO virtqueues a vhost scsi device can support. The default is 128. The max is 1024."); 247 248 struct vhost_scsi_virtqueue { 249 struct vhost_virtqueue vq; 250 struct vhost_scsi *vs; 251 /* 252 * Reference counting for inflight reqs, used for flush operation. At 253 * each time, one reference tracks new commands submitted, while we 254 * wait for another one to reach 0. 255 */ 256 struct vhost_scsi_inflight inflights[2]; 257 /* 258 * Indicate current inflight in use, protected by vq->mutex. 259 * Writers must also take dev mutex and flush under it. 260 */ 261 int inflight_idx; 262 struct vhost_scsi_cmd *scsi_cmds; 263 struct sbitmap scsi_tags; 264 int max_cmds; 265 struct page **upages; 266 267 struct vhost_work completion_work; 268 struct llist_head completion_list; 269 }; 270 271 struct vhost_scsi { 272 /* Protected by vhost_scsi->dev.mutex */ 273 struct vhost_scsi_tpg **vs_tpg; 274 char vs_vhost_wwpn[TRANSPORT_IQN_LEN]; 275 276 struct vhost_dev dev; 277 struct vhost_scsi_virtqueue *vqs; 278 struct vhost_scsi_inflight **old_inflight; 279 280 struct vhost_work vs_event_work; /* evt injection work item */ 281 struct llist_head vs_event_list; /* evt injection queue */ 282 283 bool vs_events_missed; /* any missed events, protected by vq->mutex */ 284 int vs_events_nr; /* num of pending events, protected by vq->mutex */ 285 286 unsigned int inline_sg_cnt; 287 }; 288 289 struct vhost_scsi_tmf { 290 struct vhost_work vwork; 291 struct work_struct flush_work; 292 struct vhost_scsi *vhost; 293 struct vhost_scsi_virtqueue *svq; 294 295 struct se_cmd se_cmd; 296 u8 scsi_resp; 297 struct vhost_scsi_inflight *inflight; 298 struct iovec resp_iov; 299 int in_iovs; 300 int vq_desc; 301 302 /* 303 * Dirty write descriptors of this command. 304 */ 305 struct vhost_log *tmf_log; 306 unsigned int tmf_log_num; 307 }; 308 309 /* 310 * Context for processing request and control queue operations. 311 */ 312 struct vhost_scsi_ctx { 313 int head; 314 unsigned int out, in; 315 size_t req_size, rsp_size; 316 size_t out_size, in_size; 317 u8 *target, *lunp; 318 void *req; 319 struct iov_iter out_iter; 320 }; 321 322 /* 323 * Global mutex to protect vhost_scsi TPG list for vhost IOCTLs and LIO 324 * configfs management operations. 325 */ 326 static DEFINE_MUTEX(vhost_scsi_mutex); 327 static LIST_HEAD(vhost_scsi_list); 328 329 static void vhost_scsi_done_inflight(struct kref *kref) 330 { 331 struct vhost_scsi_inflight *inflight; 332 333 inflight = container_of(kref, struct vhost_scsi_inflight, kref); 334 complete(&inflight->comp); 335 } 336 337 static void vhost_scsi_init_inflight(struct vhost_scsi *vs, 338 struct vhost_scsi_inflight *old_inflight[]) 339 { 340 struct vhost_scsi_inflight *new_inflight; 341 struct vhost_virtqueue *vq; 342 int idx, i; 343 344 for (i = 0; i < vs->dev.nvqs; i++) { 345 vq = &vs->vqs[i].vq; 346 347 mutex_lock(&vq->mutex); 348 349 /* store old inflight */ 350 idx = vs->vqs[i].inflight_idx; 351 if (old_inflight) 352 old_inflight[i] = &vs->vqs[i].inflights[idx]; 353 354 /* setup new inflight */ 355 vs->vqs[i].inflight_idx = idx ^ 1; 356 new_inflight = &vs->vqs[i].inflights[idx ^ 1]; 357 kref_init(&new_inflight->kref); 358 init_completion(&new_inflight->comp); 359 360 mutex_unlock(&vq->mutex); 361 } 362 } 363 364 static struct vhost_scsi_inflight * 365 vhost_scsi_get_inflight(struct vhost_virtqueue *vq) 366 { 367 struct vhost_scsi_inflight *inflight; 368 struct vhost_scsi_virtqueue *svq; 369 370 svq = container_of(vq, struct vhost_scsi_virtqueue, vq); 371 inflight = &svq->inflights[svq->inflight_idx]; 372 kref_get(&inflight->kref); 373 374 return inflight; 375 } 376 377 static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight) 378 { 379 kref_put(&inflight->kref, vhost_scsi_done_inflight); 380 } 381 382 static int vhost_scsi_check_true(struct se_portal_group *se_tpg) 383 { 384 return 1; 385 } 386 387 static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg) 388 { 389 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 390 struct vhost_scsi_tpg, se_tpg); 391 struct vhost_scsi_tport *tport = tpg->tport; 392 393 return &tport->tport_name[0]; 394 } 395 396 static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg) 397 { 398 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 399 struct vhost_scsi_tpg, se_tpg); 400 return tpg->tport_tpgt; 401 } 402 403 static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg) 404 { 405 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 406 struct vhost_scsi_tpg, se_tpg); 407 408 return tpg->tv_fabric_prot_type; 409 } 410 411 static int vhost_scsi_copy_cmd_log(struct vhost_virtqueue *vq, 412 struct vhost_scsi_cmd *cmd, 413 struct vhost_log *log, 414 unsigned int log_num) 415 { 416 if (!cmd->tvc_log) 417 cmd->tvc_log = kmalloc_objs(*cmd->tvc_log, vq->dev->iov_limit); 418 419 if (unlikely(!cmd->tvc_log)) { 420 vq_err(vq, "Failed to alloc tvc_log\n"); 421 return -ENOMEM; 422 } 423 424 memcpy(cmd->tvc_log, log, sizeof(*cmd->tvc_log) * log_num); 425 cmd->tvc_log_num = log_num; 426 427 return 0; 428 } 429 430 static void vhost_scsi_log_write(struct vhost_virtqueue *vq, 431 struct vhost_log *log, 432 unsigned int log_num) 433 { 434 if (likely(!vhost_has_feature(vq, VHOST_F_LOG_ALL))) 435 return; 436 437 if (likely(!log_num || !log)) 438 return; 439 440 /* 441 * vhost-scsi doesn't support VIRTIO_F_ACCESS_PLATFORM. 442 * No requirement for vq->iotlb case. 443 */ 444 WARN_ON_ONCE(unlikely(vq->iotlb)); 445 vhost_log_write(vq, log, log_num, U64_MAX, NULL, 0); 446 } 447 448 static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd) 449 { 450 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd, 451 struct vhost_scsi_cmd, tvc_se_cmd); 452 struct vhost_scsi_virtqueue *svq = container_of(tv_cmd->tvc_vq, 453 struct vhost_scsi_virtqueue, vq); 454 struct vhost_scsi *vs = svq->vs; 455 struct vhost_scsi_inflight *inflight = tv_cmd->inflight; 456 struct scatterlist *sg; 457 struct page *page; 458 int i; 459 460 if (tv_cmd->tvc_sgl_count) { 461 for_each_sgtable_sg(&tv_cmd->table, sg, i) { 462 page = sg_page(sg); 463 if (!page) 464 continue; 465 466 if (tv_cmd->copied_iov) 467 __free_page(page); 468 else if (tv_cmd->tvc_need_unpin) 469 unpin_user_page(page); 470 /* 471 * Else: iov_iter_extract_pages() took neither a ref 472 * nor a pin on this page (ITER_KVEC/BVEC/etc), so 473 * there is nothing to release here. 474 */ 475 } 476 kfree(tv_cmd->read_iter); 477 kfree(tv_cmd->read_iov); 478 sg_free_table_chained(&tv_cmd->table, vs->inline_sg_cnt); 479 } 480 if (tv_cmd->tvc_prot_sgl_count) { 481 for_each_sgtable_sg(&tv_cmd->prot_table, sg, i) { 482 page = sg_page(sg); 483 if (page && tv_cmd->tvc_prot_need_unpin) 484 unpin_user_page(page); 485 } 486 sg_free_table_chained(&tv_cmd->prot_table, vs->inline_sg_cnt); 487 } 488 489 if (tv_cmd->tvc_resp_iovs != &tv_cmd->tvc_resp_iov) 490 kfree(tv_cmd->tvc_resp_iovs); 491 sbitmap_clear_bit(&svq->scsi_tags, se_cmd->map_tag); 492 vhost_scsi_put_inflight(inflight); 493 } 494 495 static void vhost_scsi_release_tmf_res(struct vhost_scsi_tmf *tmf) 496 { 497 struct vhost_scsi_inflight *inflight = tmf->inflight; 498 499 /* 500 * tmf->tmf_log is default NULL unless VHOST_F_LOG_ALL is set. 501 */ 502 kfree(tmf->tmf_log); 503 kfree(tmf); 504 vhost_scsi_put_inflight(inflight); 505 } 506 507 static void vhost_scsi_drop_cmds(struct vhost_scsi_virtqueue *svq) 508 { 509 struct vhost_scsi_cmd *cmd, *t; 510 struct llist_node *llnode; 511 512 llnode = llist_del_all(&svq->completion_list); 513 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) 514 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd); 515 } 516 517 static void vhost_scsi_release_cmd(struct se_cmd *se_cmd) 518 { 519 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) { 520 struct vhost_scsi_tmf *tmf = container_of(se_cmd, 521 struct vhost_scsi_tmf, se_cmd); 522 523 schedule_work(&tmf->flush_work); 524 } else { 525 struct vhost_scsi_cmd *cmd = container_of(se_cmd, 526 struct vhost_scsi_cmd, tvc_se_cmd); 527 struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq, 528 struct vhost_scsi_virtqueue, vq); 529 530 llist_add(&cmd->tvc_completion_list, &svq->completion_list); 531 if (!vhost_vq_work_queue(&svq->vq, &svq->completion_work)) 532 vhost_scsi_drop_cmds(svq); 533 } 534 } 535 536 static int vhost_scsi_write_pending(struct se_cmd *se_cmd) 537 { 538 /* Go ahead and process the write immediately */ 539 target_execute_cmd(se_cmd); 540 return 0; 541 } 542 543 static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd) 544 { 545 transport_generic_free_cmd(se_cmd, 0); 546 return 0; 547 } 548 549 static int vhost_scsi_queue_status(struct se_cmd *se_cmd) 550 { 551 transport_generic_free_cmd(se_cmd, 0); 552 return 0; 553 } 554 555 static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd) 556 { 557 struct vhost_scsi_tmf *tmf = container_of(se_cmd, struct vhost_scsi_tmf, 558 se_cmd); 559 560 tmf->scsi_resp = se_cmd->se_tmr_req->response; 561 transport_generic_free_cmd(&tmf->se_cmd, 0); 562 } 563 564 static void vhost_scsi_aborted_task(struct se_cmd *se_cmd) 565 { 566 return; 567 } 568 569 static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) 570 { 571 vs->vs_events_nr--; 572 kfree(evt); 573 } 574 575 static struct vhost_scsi_evt * 576 vhost_scsi_allocate_evt(struct vhost_scsi *vs, 577 u32 event, u32 reason) 578 { 579 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 580 struct vhost_scsi_evt *evt; 581 582 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) { 583 vs->vs_events_missed = true; 584 return NULL; 585 } 586 587 evt = kzalloc_obj(*evt); 588 if (!evt) { 589 vq_err(vq, "Failed to allocate vhost_scsi_evt\n"); 590 vs->vs_events_missed = true; 591 return NULL; 592 } 593 594 evt->event.event = cpu_to_vhost32(vq, event); 595 evt->event.reason = cpu_to_vhost32(vq, reason); 596 vs->vs_events_nr++; 597 598 return evt; 599 } 600 601 static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd) 602 { 603 return target_put_sess_cmd(se_cmd); 604 } 605 606 static void 607 vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) 608 { 609 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 610 struct virtio_scsi_event *event = &evt->event; 611 struct virtio_scsi_event __user *eventp; 612 struct vhost_log *vq_log; 613 unsigned int log_num; 614 unsigned out, in; 615 int head, ret; 616 617 if (!vhost_vq_get_backend(vq)) { 618 vs->vs_events_missed = true; 619 return; 620 } 621 622 again: 623 vhost_disable_notify(&vs->dev, vq); 624 625 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 626 vq->log : NULL; 627 628 /* 629 * Reset 'log_num' since vhost_get_vq_desc() may reset it only 630 * after certain condition checks. 631 */ 632 log_num = 0; 633 634 head = vhost_get_vq_desc(vq, vq->iov, 635 ARRAY_SIZE(vq->iov), &out, &in, 636 vq_log, &log_num); 637 if (head < 0) { 638 vs->vs_events_missed = true; 639 return; 640 } 641 if (head == vq->num) { 642 if (vhost_enable_notify(&vs->dev, vq)) 643 goto again; 644 vs->vs_events_missed = true; 645 return; 646 } 647 648 /* 649 * Eventq buffers are device-writable only. A nonzero in count 650 * guarantees that out is within the bounds of vq->iov. 651 */ 652 if (unlikely(out || !in)) { 653 vq_err(vq, "Invalid event buffer: out %u, in %u\n", out, in); 654 vs->vs_events_missed = true; 655 return; 656 } 657 658 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) { 659 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n", 660 vq->iov[out].iov_len); 661 vs->vs_events_missed = true; 662 return; 663 } 664 665 if (vs->vs_events_missed) { 666 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED); 667 vs->vs_events_missed = false; 668 } 669 670 eventp = vq->iov[out].iov_base; 671 ret = __copy_to_user(eventp, event, sizeof(*event)); 672 if (!ret) 673 vhost_add_used_and_signal(&vs->dev, vq, head, 0); 674 else 675 vq_err(vq, "Faulted on vhost_scsi_send_event\n"); 676 677 vhost_scsi_log_write(vq, vq_log, log_num); 678 } 679 680 /* Caller must hold the event virtqueue mutex. */ 681 static void __vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop) 682 { 683 struct vhost_scsi_evt *evt, *t; 684 struct llist_node *llnode; 685 686 llnode = llist_reverse_order(llist_del_all(&vs->vs_event_list)); 687 llist_for_each_entry_safe(evt, t, llnode, list) { 688 if (!drop) 689 vhost_scsi_do_evt_work(vs, evt); 690 vhost_scsi_free_evt(vs, evt); 691 } 692 } 693 694 static void vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop) 695 { 696 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 697 698 mutex_lock(&vq->mutex); 699 __vhost_scsi_complete_events(vs, drop); 700 mutex_unlock(&vq->mutex); 701 } 702 703 static void vhost_scsi_evt_work(struct vhost_work *work) 704 { 705 struct vhost_scsi *vs = container_of(work, struct vhost_scsi, 706 vs_event_work); 707 vhost_scsi_complete_events(vs, false); 708 } 709 710 static int vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd *cmd) 711 { 712 struct iov_iter *iter = cmd->read_iter; 713 struct scatterlist *sg; 714 struct page *page; 715 size_t len; 716 int i; 717 718 for_each_sgtable_sg(&cmd->table, sg, i) { 719 page = sg_page(sg); 720 if (!page) 721 continue; 722 723 len = sg->length; 724 725 if (copy_page_to_iter(page, 0, len, iter) != len) { 726 pr_err("Could not copy data while handling misaligned cmd. Error %zu\n", 727 len); 728 return -1; 729 } 730 } 731 732 return 0; 733 } 734 735 /* Fill in status and signal that we are done processing this command 736 * 737 * This is scheduled in the vhost work queue so we are called with the owner 738 * process mm and can access the vring. 739 */ 740 static void vhost_scsi_complete_cmd_work(struct vhost_work *work) 741 { 742 struct vhost_scsi_virtqueue *svq = container_of(work, 743 struct vhost_scsi_virtqueue, completion_work); 744 struct virtio_scsi_cmd_resp v_rsp; 745 struct vhost_scsi_cmd *cmd, *t; 746 struct llist_node *llnode; 747 struct se_cmd *se_cmd; 748 struct iov_iter iov_iter; 749 bool signal = false; 750 int ret; 751 752 llnode = llist_del_all(&svq->completion_list); 753 754 mutex_lock(&svq->vq.mutex); 755 756 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) { 757 se_cmd = &cmd->tvc_se_cmd; 758 759 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__, 760 cmd, se_cmd->residual_count, se_cmd->scsi_status); 761 memset(&v_rsp, 0, sizeof(v_rsp)); 762 763 if (cmd->read_iter && vhost_scsi_copy_sgl_to_iov(cmd)) { 764 v_rsp.response = VIRTIO_SCSI_S_BAD_TARGET; 765 } else { 766 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, 767 se_cmd->residual_count); 768 /* TODO is status_qualifier field needed? */ 769 v_rsp.status = se_cmd->scsi_status; 770 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq, 771 se_cmd->scsi_sense_length); 772 memcpy(v_rsp.sense, cmd->tvc_sense_buf, 773 se_cmd->scsi_sense_length); 774 } 775 776 iov_iter_init(&iov_iter, ITER_DEST, cmd->tvc_resp_iovs, 777 cmd->tvc_resp_iovs_cnt, sizeof(v_rsp)); 778 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter); 779 if (likely(ret == sizeof(v_rsp))) { 780 signal = true; 781 782 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0); 783 } else 784 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 785 786 vhost_scsi_log_write(cmd->tvc_vq, cmd->tvc_log, 787 cmd->tvc_log_num); 788 789 vhost_scsi_release_cmd_res(se_cmd); 790 } 791 792 mutex_unlock(&svq->vq.mutex); 793 794 if (signal) 795 vhost_signal(&svq->vs->dev, &svq->vq); 796 } 797 798 static struct vhost_scsi_cmd * 799 vhost_scsi_get_cmd(struct vhost_virtqueue *vq, u64 scsi_tag) 800 { 801 struct vhost_scsi_virtqueue *svq = container_of(vq, 802 struct vhost_scsi_virtqueue, vq); 803 struct vhost_scsi_cmd *cmd; 804 struct scatterlist *sgl, *prot_sgl; 805 struct vhost_log *log; 806 int tag; 807 808 tag = sbitmap_get(&svq->scsi_tags); 809 if (tag < 0) { 810 pr_warn_once("Guest sent too many cmds. Returning TASK_SET_FULL.\n"); 811 return ERR_PTR(-ENOMEM); 812 } 813 814 cmd = &svq->scsi_cmds[tag]; 815 sgl = cmd->sgl; 816 prot_sgl = cmd->prot_sgl; 817 log = cmd->tvc_log; 818 memset(cmd, 0, sizeof(*cmd)); 819 cmd->sgl = sgl; 820 cmd->prot_sgl = prot_sgl; 821 cmd->tvc_log = log; 822 cmd->tvc_se_cmd.map_tag = tag; 823 cmd->inflight = vhost_scsi_get_inflight(vq); 824 825 return cmd; 826 } 827 828 static void vhost_scsi_revert_map_iov_to_sgl(struct iov_iter *iter, 829 struct scatterlist *curr, 830 struct scatterlist *end, 831 bool need_unpin) 832 { 833 size_t revert_bytes = 0; 834 struct page *page; 835 836 while (curr != end) { 837 page = sg_page(curr); 838 839 if (page) { 840 if (need_unpin) 841 unpin_user_page(page); 842 revert_bytes += curr->length; 843 } 844 /* Clear so we can re-use it for the copy path */ 845 sg_set_page(curr, NULL, 0, 0); 846 curr = sg_next(curr); 847 } 848 iov_iter_revert(iter, revert_bytes); 849 } 850 851 /* 852 * Map a user memory range into a scatterlist 853 * 854 * Returns the number of scatterlist entries used or -errno on error. 855 */ 856 static int 857 vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd, 858 struct iov_iter *iter, 859 struct sg_table *sg_table, 860 struct scatterlist **sgl, 861 bool is_prot) 862 { 863 struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq, 864 struct vhost_scsi_virtqueue, vq); 865 struct page **pages = svq->upages; 866 struct scatterlist *sg = *sgl; 867 ssize_t bytes; 868 size_t offset; 869 unsigned int n, npages = 0; 870 871 bytes = iov_iter_extract_pages(iter, &pages, LONG_MAX, 872 VHOST_SCSI_PREALLOC_UPAGES, 0, &offset); 873 /* No pages were extracted */ 874 if (bytes <= 0) 875 return bytes < 0 ? bytes : -EFAULT; 876 877 while (bytes) { 878 n = min_t(unsigned int, PAGE_SIZE - offset, bytes); 879 /* 880 * The block layer requires bios/requests to be a multiple of 881 * 512 bytes, but Windows can send us vecs that are misaligned. 882 * This can result in bios and later requests with misaligned 883 * sizes if we have to break up a cmd/scatterlist into multiple 884 * bios. 885 * 886 * We currently only break up a command into multiple bios if 887 * we hit the vec/seg limit, so check if our sgl_count is 888 * greater than the max and if a vec in the cmd has a 889 * misaligned offset/size. 890 */ 891 if (!is_prot && 892 (offset & (SECTOR_SIZE - 1) || n & (SECTOR_SIZE - 1)) && 893 cmd->tvc_sgl_count > BIO_MAX_VECS) { 894 WARN_ONCE(true, 895 "vhost-scsi detected misaligned IO. Performance may be degraded."); 896 goto revert_iter_extract_pages; 897 } 898 899 sg_set_page(sg, pages[npages++], n, offset); 900 sg = sg_next(sg); 901 bytes -= n; 902 offset = 0; 903 } 904 905 *sgl = sg; 906 return npages; 907 908 revert_iter_extract_pages: 909 vhost_scsi_revert_map_iov_to_sgl(iter, *sgl, sg, 910 is_prot ? cmd->tvc_prot_need_unpin : 911 cmd->tvc_need_unpin); 912 913 iov_iter_revert(iter, bytes); 914 /* 915 * pages[npages] is the first page not added to the SG list. 916 * The remaining data starts at offset in that page. 917 */ 918 while (bytes) { 919 n = min_t(unsigned int, PAGE_SIZE - offset, bytes); 920 921 if (is_prot ? cmd->tvc_prot_need_unpin : cmd->tvc_need_unpin) 922 unpin_user_page(pages[npages]); 923 npages++; 924 bytes -= n; 925 offset = 0; 926 } 927 928 return -EINVAL; 929 } 930 931 static int 932 vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls) 933 { 934 int sgl_count = 0; 935 936 if (!iter || !iter_iov(iter)) { 937 pr_err("%s: iter->iov is NULL, but expected bytes: %zu" 938 " present\n", __func__, bytes); 939 return -EINVAL; 940 } 941 942 sgl_count = iov_iter_npages(iter, 0xffff); 943 if (sgl_count > max_sgls) { 944 pr_err("%s: requested sgl_count: %d exceeds pre-allocated" 945 " max_sgls: %d\n", __func__, sgl_count, max_sgls); 946 return -EINVAL; 947 } 948 return sgl_count; 949 } 950 951 static int 952 vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter, 953 struct sg_table *sg_table, int sg_count, 954 int data_dir) 955 { 956 size_t len = iov_iter_count(iter); 957 unsigned int nbytes = 0; 958 struct scatterlist *sg; 959 struct page *page; 960 int i, ret; 961 962 if (data_dir == DMA_FROM_DEVICE) { 963 cmd->read_iter = kzalloc_obj(*cmd->read_iter); 964 if (!cmd->read_iter) 965 return -ENOMEM; 966 967 cmd->read_iov = dup_iter(cmd->read_iter, iter, GFP_KERNEL); 968 if (!cmd->read_iov) { 969 ret = -ENOMEM; 970 goto free_iter; 971 } 972 } 973 974 for_each_sgtable_sg(sg_table, sg, i) { 975 /* 976 * Completion copies the full bounce buffer, including bytes the 977 * target did not write. This is already the slow fallback for 978 * misaligned I/O, so zeroing is acceptable here. 979 */ 980 page = alloc_page(GFP_KERNEL | __GFP_ZERO); 981 if (!page) { 982 ret = -ENOMEM; 983 goto err; 984 } 985 986 nbytes = min_t(unsigned int, PAGE_SIZE, len); 987 sg_set_page(sg, page, nbytes, 0); 988 989 if (data_dir == DMA_TO_DEVICE && 990 copy_page_from_iter(page, 0, nbytes, iter) != nbytes) { 991 ret = -EFAULT; 992 goto err; 993 } 994 995 len -= nbytes; 996 } 997 998 cmd->copied_iov = 1; 999 cmd->tvc_need_unpin = 0; 1000 return 0; 1001 1002 err: 1003 pr_err("Could not read %u bytes while handling misaligned cmd\n", 1004 nbytes); 1005 1006 for_each_sgtable_sg(sg_table, sg, i) { 1007 page = sg_page(sg); 1008 if (page) 1009 __free_page(page); 1010 } 1011 kfree(cmd->read_iov); 1012 free_iter: 1013 kfree(cmd->read_iter); 1014 return ret; 1015 } 1016 1017 static int 1018 vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter, 1019 struct sg_table *sg_table, int sg_count, bool is_prot) 1020 { 1021 struct scatterlist *sg = sg_table->sgl; 1022 int ret; 1023 1024 if (is_prot) 1025 cmd->tvc_prot_need_unpin = iov_iter_extract_will_pin(iter); 1026 else 1027 cmd->tvc_need_unpin = iov_iter_extract_will_pin(iter); 1028 1029 while (iov_iter_count(iter)) { 1030 ret = vhost_scsi_map_to_sgl(cmd, iter, sg_table, &sg, is_prot); 1031 if (ret < 0) { 1032 vhost_scsi_revert_map_iov_to_sgl(iter, sg_table->sgl, 1033 sg, is_prot ? cmd->tvc_prot_need_unpin : 1034 cmd->tvc_need_unpin); 1035 return ret; 1036 } 1037 } 1038 1039 return 0; 1040 } 1041 1042 static int 1043 vhost_scsi_mapal(struct vhost_scsi *vs, struct vhost_scsi_cmd *cmd, 1044 size_t prot_bytes, struct iov_iter *prot_iter, 1045 size_t data_bytes, struct iov_iter *data_iter, int data_dir) 1046 { 1047 int sgl_count, ret; 1048 1049 if (prot_bytes) { 1050 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes, 1051 VHOST_SCSI_PREALLOC_PROT_SGLS); 1052 if (sgl_count < 0) 1053 return sgl_count; 1054 1055 cmd->prot_table.sgl = cmd->prot_sgl; 1056 ret = sg_alloc_table_chained(&cmd->prot_table, sgl_count, 1057 cmd->prot_table.sgl, 1058 vs->inline_sg_cnt); 1059 if (ret) 1060 return ret; 1061 1062 cmd->tvc_prot_sgl_count = sgl_count; 1063 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__, 1064 cmd->prot_table.sgl, cmd->tvc_prot_sgl_count); 1065 1066 ret = vhost_scsi_map_iov_to_sgl(cmd, prot_iter, 1067 &cmd->prot_table, 1068 cmd->tvc_prot_sgl_count, true); 1069 if (ret < 0) { 1070 sg_free_table_chained(&cmd->prot_table, 1071 vs->inline_sg_cnt); 1072 cmd->tvc_prot_sgl_count = 0; 1073 return ret; 1074 } 1075 } 1076 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes, 1077 VHOST_SCSI_PREALLOC_SGLS); 1078 if (sgl_count < 0) 1079 return sgl_count; 1080 1081 cmd->table.sgl = cmd->sgl; 1082 ret = sg_alloc_table_chained(&cmd->table, sgl_count, cmd->table.sgl, 1083 vs->inline_sg_cnt); 1084 if (ret) 1085 return ret; 1086 1087 cmd->tvc_sgl_count = sgl_count; 1088 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__, 1089 cmd->table.sgl, cmd->tvc_sgl_count); 1090 1091 ret = vhost_scsi_map_iov_to_sgl(cmd, data_iter, &cmd->table, 1092 cmd->tvc_sgl_count, false); 1093 if (ret == -EINVAL) 1094 ret = vhost_scsi_copy_iov_to_sgl(cmd, data_iter, &cmd->table, 1095 cmd->tvc_sgl_count, data_dir); 1096 if (ret < 0) { 1097 sg_free_table_chained(&cmd->table, vs->inline_sg_cnt); 1098 cmd->tvc_sgl_count = 0; 1099 return ret; 1100 } 1101 return 0; 1102 } 1103 1104 static int vhost_scsi_to_tcm_attr(int attr) 1105 { 1106 switch (attr) { 1107 case VIRTIO_SCSI_S_SIMPLE: 1108 return TCM_SIMPLE_TAG; 1109 case VIRTIO_SCSI_S_ORDERED: 1110 return TCM_ORDERED_TAG; 1111 case VIRTIO_SCSI_S_HEAD: 1112 return TCM_HEAD_TAG; 1113 case VIRTIO_SCSI_S_ACA: 1114 return TCM_ACA_TAG; 1115 default: 1116 break; 1117 } 1118 return TCM_SIMPLE_TAG; 1119 } 1120 1121 static void vhost_scsi_target_queue_cmd(struct vhost_scsi_nexus *nexus, 1122 struct vhost_scsi_cmd *cmd, 1123 unsigned char *cdb, u16 lun, 1124 int task_attr, int data_dir, 1125 u32 exp_data_len) 1126 { 1127 struct se_cmd *se_cmd = &cmd->tvc_se_cmd; 1128 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL; 1129 1130 /* FIXME: BIDI operation */ 1131 if (cmd->tvc_sgl_count) { 1132 sg_ptr = cmd->table.sgl; 1133 1134 if (cmd->tvc_prot_sgl_count) 1135 sg_prot_ptr = cmd->prot_table.sgl; 1136 else 1137 se_cmd->prot_pto = true; 1138 } else { 1139 sg_ptr = NULL; 1140 } 1141 1142 se_cmd->tag = 0; 1143 target_init_cmd(se_cmd, nexus->tvn_se_sess, &cmd->tvc_sense_buf[0], 1144 lun, exp_data_len, vhost_scsi_to_tcm_attr(task_attr), 1145 data_dir, TARGET_SCF_ACK_KREF); 1146 1147 if (target_submit_prep(se_cmd, cdb, sg_ptr, 1148 cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr, 1149 cmd->tvc_prot_sgl_count, GFP_KERNEL)) 1150 return; 1151 1152 target_submit(se_cmd); 1153 } 1154 1155 static void 1156 vhost_scsi_send_status(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 1157 struct vhost_scsi_ctx *vc, u8 status) 1158 { 1159 struct virtio_scsi_cmd_resp rsp; 1160 struct iov_iter iov_iter; 1161 int ret; 1162 1163 memset(&rsp, 0, sizeof(rsp)); 1164 rsp.status = status; 1165 1166 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, 1167 sizeof(rsp)); 1168 1169 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter); 1170 1171 if (likely(ret == sizeof(rsp))) 1172 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0); 1173 else 1174 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 1175 } 1176 1177 #define TYPE_IO_CMD 0 1178 #define TYPE_CTRL_TMF 1 1179 #define TYPE_CTRL_AN 2 1180 1181 static void 1182 vhost_scsi_send_bad_target(struct vhost_scsi *vs, 1183 struct vhost_virtqueue *vq, 1184 struct vhost_scsi_ctx *vc, int type) 1185 { 1186 union { 1187 struct virtio_scsi_cmd_resp cmd; 1188 struct virtio_scsi_ctrl_tmf_resp tmf; 1189 struct virtio_scsi_ctrl_an_resp an; 1190 } rsp; 1191 struct iov_iter iov_iter; 1192 size_t rsp_size; 1193 int ret; 1194 1195 memset(&rsp, 0, sizeof(rsp)); 1196 1197 if (type == TYPE_IO_CMD) { 1198 rsp_size = sizeof(struct virtio_scsi_cmd_resp); 1199 rsp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; 1200 } else if (type == TYPE_CTRL_TMF) { 1201 rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp); 1202 rsp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; 1203 } else { 1204 rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp); 1205 rsp.an.response = VIRTIO_SCSI_S_BAD_TARGET; 1206 } 1207 1208 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, 1209 rsp_size); 1210 1211 ret = copy_to_iter(&rsp, rsp_size, &iov_iter); 1212 1213 if (likely(ret == rsp_size)) 1214 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0); 1215 else 1216 pr_err("Faulted on virtio scsi type=%d\n", type); 1217 } 1218 1219 static int 1220 vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 1221 struct vhost_scsi_ctx *vc, 1222 struct vhost_log *log, unsigned int *log_num) 1223 { 1224 int ret = -ENXIO; 1225 1226 if (likely(log_num)) 1227 *log_num = 0; 1228 1229 vc->head = vhost_get_vq_desc(vq, vq->iov, 1230 ARRAY_SIZE(vq->iov), &vc->out, &vc->in, 1231 log, log_num); 1232 1233 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", 1234 vc->head, vc->out, vc->in); 1235 1236 /* On error, stop handling until the next kick. */ 1237 if (unlikely(vc->head < 0)) 1238 goto done; 1239 1240 /* Nothing new? Wait for eventfd to tell us they refilled. */ 1241 if (vc->head == vq->num) { 1242 if (unlikely(vhost_enable_notify(&vs->dev, vq))) { 1243 vhost_disable_notify(&vs->dev, vq); 1244 ret = -EAGAIN; 1245 } 1246 goto done; 1247 } 1248 1249 /* 1250 * Get the size of request and response buffers. 1251 * FIXME: Not correct for BIDI operation 1252 */ 1253 vc->out_size = iov_length(vq->iov, vc->out); 1254 vc->in_size = iov_length(&vq->iov[vc->out], vc->in); 1255 1256 /* 1257 * Copy over the virtio-scsi request header, which for a 1258 * ANY_LAYOUT enabled guest may span multiple iovecs, or a 1259 * single iovec may contain both the header + outgoing 1260 * WRITE payloads. 1261 * 1262 * copy_from_iter() will advance out_iter, so that it will 1263 * point at the start of the outgoing WRITE payload, if 1264 * DMA_TO_DEVICE is set. 1265 */ 1266 iov_iter_init(&vc->out_iter, ITER_SOURCE, vq->iov, vc->out, vc->out_size); 1267 ret = 0; 1268 1269 done: 1270 return ret; 1271 } 1272 1273 static int 1274 vhost_scsi_chk_size(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc) 1275 { 1276 if (unlikely(vc->in_size < vc->rsp_size)) { 1277 vq_err(vq, 1278 "Response buf too small, need min %zu bytes got %zu", 1279 vc->rsp_size, vc->in_size); 1280 return -EINVAL; 1281 } else if (unlikely(vc->out_size < vc->req_size)) { 1282 vq_err(vq, 1283 "Request buf too small, need min %zu bytes got %zu", 1284 vc->req_size, vc->out_size); 1285 return -EIO; 1286 } 1287 1288 return 0; 1289 } 1290 1291 static int 1292 vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc, 1293 struct vhost_scsi_tpg **tpgp) 1294 { 1295 int ret = -EIO; 1296 1297 if (unlikely(!copy_from_iter_full(vc->req, vc->req_size, 1298 &vc->out_iter))) { 1299 vq_err(vq, "Faulted on copy_from_iter_full\n"); 1300 } else if (unlikely(*vc->lunp != 1)) { 1301 /* virtio-scsi spec requires byte 0 of the lun to be 1 */ 1302 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *vc->lunp); 1303 } else { 1304 struct vhost_scsi_tpg **vs_tpg, *tpg = NULL; 1305 1306 if (vc->target) { 1307 /* validated at handler entry */ 1308 vs_tpg = vhost_vq_get_backend(vq); 1309 tpg = READ_ONCE(vs_tpg[*vc->target]); 1310 if (unlikely(!tpg)) 1311 goto out; 1312 } 1313 1314 if (tpgp) 1315 *tpgp = tpg; 1316 ret = 0; 1317 } 1318 out: 1319 return ret; 1320 } 1321 1322 static int 1323 vhost_scsi_setup_resp_iovs(struct vhost_scsi_cmd *cmd, struct iovec *in_iovs, 1324 unsigned int in_iovs_cnt) 1325 { 1326 int i, cnt; 1327 1328 if (!in_iovs_cnt) 1329 return 0; 1330 /* 1331 * Initiators normally just put the virtio_scsi_cmd_resp in the first 1332 * iov, but just in case they wedged in some data with it we check for 1333 * greater than or equal to the response struct. 1334 */ 1335 if (in_iovs[0].iov_len >= sizeof(struct virtio_scsi_cmd_resp)) { 1336 cmd->tvc_resp_iovs = &cmd->tvc_resp_iov; 1337 cmd->tvc_resp_iovs_cnt = 1; 1338 } else { 1339 /* 1340 * Legacy descriptor layouts didn't specify that we must put 1341 * the entire response in one iov. Worst case we have a 1342 * iov per byte. 1343 */ 1344 cnt = min(VHOST_SCSI_MAX_RESP_IOVS, in_iovs_cnt); 1345 cmd->tvc_resp_iovs = kzalloc_objs(struct iovec, cnt); 1346 if (!cmd->tvc_resp_iovs) 1347 return -ENOMEM; 1348 1349 cmd->tvc_resp_iovs_cnt = cnt; 1350 } 1351 1352 for (i = 0; i < cmd->tvc_resp_iovs_cnt; i++) 1353 cmd->tvc_resp_iovs[i] = in_iovs[i]; 1354 1355 return 0; 1356 } 1357 1358 static u16 vhost_buf_to_lun(u8 *lun_buf) 1359 { 1360 return ((lun_buf[2] << 8) | lun_buf[3]) & 0x3FFF; 1361 } 1362 1363 static void 1364 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) 1365 { 1366 struct vhost_scsi_tpg **vs_tpg, *tpg; 1367 struct virtio_scsi_cmd_req v_req; 1368 struct virtio_scsi_cmd_req_pi v_req_pi; 1369 struct vhost_scsi_nexus *nexus; 1370 struct vhost_scsi_ctx vc; 1371 struct vhost_scsi_cmd *cmd; 1372 struct iov_iter in_iter, prot_iter, data_iter; 1373 u64 tag; 1374 u32 exp_data_len, data_direction; 1375 int ret, prot_bytes, c = 0; 1376 u16 lun; 1377 u8 task_attr; 1378 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI); 1379 u8 *cdb; 1380 struct vhost_log *vq_log; 1381 unsigned int log_num; 1382 1383 mutex_lock(&vq->mutex); 1384 /* 1385 * We can handle the vq only after the endpoint is setup by calling the 1386 * VHOST_SCSI_SET_ENDPOINT ioctl. 1387 */ 1388 vs_tpg = vhost_vq_get_backend(vq); 1389 if (!vs_tpg) 1390 goto out; 1391 1392 memset(&vc, 0, sizeof(vc)); 1393 vc.rsp_size = sizeof(struct virtio_scsi_cmd_resp); 1394 1395 vhost_disable_notify(&vs->dev, vq); 1396 1397 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 1398 vq->log : NULL; 1399 1400 do { 1401 ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num); 1402 if (ret) 1403 goto err; 1404 1405 /* 1406 * Setup pointers and values based upon different virtio-scsi 1407 * request header if T10_PI is enabled in KVM guest. 1408 */ 1409 if (t10_pi) { 1410 vc.req = &v_req_pi; 1411 vc.req_size = sizeof(v_req_pi); 1412 vc.lunp = &v_req_pi.lun[0]; 1413 vc.target = &v_req_pi.lun[1]; 1414 } else { 1415 vc.req = &v_req; 1416 vc.req_size = sizeof(v_req); 1417 vc.lunp = &v_req.lun[0]; 1418 vc.target = &v_req.lun[1]; 1419 } 1420 1421 /* 1422 * Validate the size of request and response buffers. 1423 * Check for a sane response buffer so we can report 1424 * early errors back to the guest. 1425 */ 1426 ret = vhost_scsi_chk_size(vq, &vc); 1427 if (ret) 1428 goto err; 1429 1430 ret = vhost_scsi_get_req(vq, &vc, &tpg); 1431 if (ret) 1432 goto err; 1433 1434 ret = -EIO; /* bad target on any error from here on */ 1435 1436 /* 1437 * Determine data_direction by calculating the total outgoing 1438 * iovec sizes + incoming iovec sizes vs. virtio-scsi request + 1439 * response headers respectively. 1440 * 1441 * For DMA_TO_DEVICE this is out_iter, which is already pointing 1442 * to the right place. 1443 * 1444 * For DMA_FROM_DEVICE, the iovec will be just past the end 1445 * of the virtio-scsi response header in either the same 1446 * or immediately following iovec. 1447 * 1448 * Any associated T10_PI bytes for the outgoing / incoming 1449 * payloads are included in calculation of exp_data_len here. 1450 */ 1451 prot_bytes = 0; 1452 1453 if (vc.out_size > vc.req_size) { 1454 data_direction = DMA_TO_DEVICE; 1455 exp_data_len = vc.out_size - vc.req_size; 1456 data_iter = vc.out_iter; 1457 } else if (vc.in_size > vc.rsp_size) { 1458 data_direction = DMA_FROM_DEVICE; 1459 exp_data_len = vc.in_size - vc.rsp_size; 1460 1461 iov_iter_init(&in_iter, ITER_DEST, &vq->iov[vc.out], vc.in, 1462 vc.rsp_size + exp_data_len); 1463 iov_iter_advance(&in_iter, vc.rsp_size); 1464 data_iter = in_iter; 1465 } else { 1466 data_direction = DMA_NONE; 1467 exp_data_len = 0; 1468 } 1469 /* 1470 * If T10_PI header + payload is present, setup prot_iter values 1471 * and recalculate data_iter for vhost_scsi_mapal() mapping to 1472 * host scatterlists via iov_iter_extract_pages(). 1473 */ 1474 if (t10_pi) { 1475 if (v_req_pi.pi_bytesout) { 1476 if (data_direction != DMA_TO_DEVICE) { 1477 vq_err(vq, "Received non zero pi_bytesout," 1478 " but wrong data_direction\n"); 1479 goto err; 1480 } 1481 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout); 1482 } else if (v_req_pi.pi_bytesin) { 1483 if (data_direction != DMA_FROM_DEVICE) { 1484 vq_err(vq, "Received non zero pi_bytesin," 1485 " but wrong data_direction\n"); 1486 goto err; 1487 } 1488 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin); 1489 } 1490 /* 1491 * Set prot_iter to data_iter and truncate it to 1492 * prot_bytes, and advance data_iter past any 1493 * preceding prot_bytes that may be present. 1494 * 1495 * Also fix up the exp_data_len to reflect only the 1496 * actual data payload length. 1497 */ 1498 if (prot_bytes) { 1499 if (prot_bytes >= exp_data_len) { 1500 vq_err(vq, "Protection data exceeds payload length\n"); 1501 goto err; 1502 } 1503 1504 exp_data_len -= prot_bytes; 1505 prot_iter = data_iter; 1506 iov_iter_truncate(&prot_iter, prot_bytes); 1507 iov_iter_advance(&data_iter, prot_bytes); 1508 } 1509 tag = vhost64_to_cpu(vq, v_req_pi.tag); 1510 task_attr = v_req_pi.task_attr; 1511 cdb = &v_req_pi.cdb[0]; 1512 lun = vhost_buf_to_lun(v_req_pi.lun); 1513 } else { 1514 tag = vhost64_to_cpu(vq, v_req.tag); 1515 task_attr = v_req.task_attr; 1516 cdb = &v_req.cdb[0]; 1517 lun = vhost_buf_to_lun(v_req.lun); 1518 } 1519 /* 1520 * Check that the received CDB size does not exceeded our 1521 * hardcoded max for vhost-scsi, then get a pre-allocated 1522 * cmd descriptor for the new virtio-scsi tag. 1523 * 1524 * TODO what if cdb was too small for varlen cdb header? 1525 */ 1526 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) { 1527 vq_err(vq, "Received SCSI CDB with command_size: %d that" 1528 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", 1529 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE); 1530 goto err; 1531 } 1532 1533 nexus = tpg->tpg_nexus; 1534 if (!nexus) { 1535 vq_err(vq, "Unable to locate active struct vhost_scsi_nexus\n"); 1536 ret = -EIO; 1537 goto err; 1538 } 1539 1540 cmd = vhost_scsi_get_cmd(vq, tag); 1541 if (IS_ERR(cmd)) { 1542 ret = PTR_ERR(cmd); 1543 vq_err(vq, "vhost_scsi_get_tag failed %d\n", ret); 1544 goto err; 1545 } 1546 cmd->tvc_vq = vq; 1547 1548 ret = vhost_scsi_setup_resp_iovs(cmd, &vq->iov[vc.out], vc.in); 1549 if (ret) { 1550 vq_err(vq, "Failed to alloc recv iovs\n"); 1551 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd); 1552 goto err; 1553 } 1554 1555 if (unlikely(vq_log && log_num)) { 1556 ret = vhost_scsi_copy_cmd_log(vq, cmd, vq_log, log_num); 1557 if (unlikely(ret)) { 1558 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd); 1559 goto err; 1560 } 1561 } 1562 1563 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", 1564 cdb[0], lun); 1565 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:" 1566 " %d\n", cmd, exp_data_len, prot_bytes, data_direction); 1567 1568 if (data_direction != DMA_NONE) { 1569 ret = vhost_scsi_mapal(vs, cmd, prot_bytes, &prot_iter, 1570 exp_data_len, &data_iter, 1571 data_direction); 1572 if (unlikely(ret)) { 1573 vq_err(vq, "Failed to map iov to sgl\n"); 1574 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd); 1575 goto err; 1576 } 1577 } 1578 /* 1579 * Save the descriptor from vhost_get_vq_desc() to be used to 1580 * complete the virtio-scsi request in TCM callback context via 1581 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status() 1582 */ 1583 cmd->tvc_vq_desc = vc.head; 1584 /* 1585 * vhost_scsi_mapal() maps the PI bytes into cmd->prot_table. 1586 * It maps the remaining exp_data_len bytes into cmd->table, 1587 * which is the data SGL passed to the target core. Its 1588 * expected length must therefore exclude prot_bytes. 1589 * 1590 * sbc_check_prot() derives the data length for protected READ 1591 * and WRITE commands from the CDB. 1592 */ 1593 vhost_scsi_target_queue_cmd(nexus, cmd, cdb, lun, task_attr, 1594 data_direction, exp_data_len); 1595 ret = 0; 1596 err: 1597 /* 1598 * ENXIO: No more requests, or read error, wait for next kick 1599 * EINVAL: Invalid response buffer, drop the request 1600 * EIO: Respond with bad target 1601 * EAGAIN: Pending request 1602 * ENOMEM: Could not allocate resources for request 1603 */ 1604 if (ret == -ENXIO) 1605 break; 1606 else if (ret == -EIO) { 1607 vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD); 1608 vhost_scsi_log_write(vq, vq_log, log_num); 1609 } else if (ret == -ENOMEM) { 1610 vhost_scsi_send_status(vs, vq, &vc, 1611 SAM_STAT_TASK_SET_FULL); 1612 vhost_scsi_log_write(vq, vq_log, log_num); 1613 } 1614 } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); 1615 out: 1616 mutex_unlock(&vq->mutex); 1617 } 1618 1619 static void 1620 vhost_scsi_send_tmf_resp(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 1621 int in_iovs, int vq_desc, struct iovec *resp_iov, 1622 int tmf_resp_code) 1623 { 1624 struct virtio_scsi_ctrl_tmf_resp rsp; 1625 struct iov_iter iov_iter; 1626 int ret; 1627 1628 pr_debug("%s\n", __func__); 1629 memset(&rsp, 0, sizeof(rsp)); 1630 rsp.response = tmf_resp_code; 1631 1632 iov_iter_init(&iov_iter, ITER_DEST, resp_iov, in_iovs, sizeof(rsp)); 1633 1634 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter); 1635 if (likely(ret == sizeof(rsp))) 1636 vhost_add_used_and_signal(&vs->dev, vq, vq_desc, 0); 1637 else 1638 pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n"); 1639 } 1640 1641 static void vhost_scsi_tmf_resp_work(struct vhost_work *work) 1642 { 1643 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf, 1644 vwork); 1645 int resp_code; 1646 1647 if (tmf->scsi_resp == TMR_FUNCTION_COMPLETE) 1648 resp_code = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 1649 else 1650 resp_code = VIRTIO_SCSI_S_FUNCTION_REJECTED; 1651 1652 mutex_lock(&tmf->svq->vq.mutex); 1653 vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs, 1654 tmf->vq_desc, &tmf->resp_iov, resp_code); 1655 vhost_scsi_log_write(&tmf->svq->vq, tmf->tmf_log, 1656 tmf->tmf_log_num); 1657 mutex_unlock(&tmf->svq->vq.mutex); 1658 1659 vhost_scsi_release_tmf_res(tmf); 1660 } 1661 1662 static void vhost_scsi_tmf_flush_work(struct work_struct *work) 1663 { 1664 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf, 1665 flush_work); 1666 struct vhost_virtqueue *vq = &tmf->svq->vq; 1667 /* 1668 * Make sure we have sent responses for other commands before we 1669 * send our response. 1670 */ 1671 vhost_dev_flush(vq->dev); 1672 if (!vhost_vq_work_queue(vq, &tmf->vwork)) 1673 vhost_scsi_release_tmf_res(tmf); 1674 } 1675 1676 static void 1677 vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg, 1678 struct vhost_virtqueue *vq, 1679 struct virtio_scsi_ctrl_tmf_req *vtmf, 1680 struct vhost_scsi_ctx *vc, 1681 struct vhost_log *log, unsigned int log_num) 1682 { 1683 struct vhost_scsi_virtqueue *svq = container_of(vq, 1684 struct vhost_scsi_virtqueue, vq); 1685 struct vhost_scsi_tmf *tmf; 1686 1687 if (vhost32_to_cpu(vq, vtmf->subtype) != 1688 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET) 1689 goto send_reject; 1690 1691 if (!tpg->tpg_nexus || !tpg->tpg_nexus->tvn_se_sess) { 1692 pr_err("Unable to locate active struct vhost_scsi_nexus for LUN RESET.\n"); 1693 goto send_reject; 1694 } 1695 1696 tmf = kzalloc_obj(*tmf); 1697 if (!tmf) 1698 goto send_reject; 1699 1700 INIT_WORK(&tmf->flush_work, vhost_scsi_tmf_flush_work); 1701 vhost_work_init(&tmf->vwork, vhost_scsi_tmf_resp_work); 1702 tmf->vhost = vs; 1703 tmf->svq = svq; 1704 tmf->resp_iov = vq->iov[vc->out]; 1705 tmf->vq_desc = vc->head; 1706 tmf->in_iovs = vc->in; 1707 tmf->inflight = vhost_scsi_get_inflight(vq); 1708 1709 if (unlikely(log && log_num)) { 1710 tmf->tmf_log = kmalloc_objs(*tmf->tmf_log, log_num); 1711 if (tmf->tmf_log) { 1712 memcpy(tmf->tmf_log, log, sizeof(*tmf->tmf_log) * log_num); 1713 tmf->tmf_log_num = log_num; 1714 } else { 1715 pr_err("vhost_scsi tmf log allocation error\n"); 1716 vhost_scsi_release_tmf_res(tmf); 1717 goto send_reject; 1718 } 1719 } 1720 1721 if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL, 1722 vhost_buf_to_lun(vtmf->lun), NULL, 1723 TMR_LUN_RESET, GFP_KERNEL, 0, 1724 TARGET_SCF_ACK_KREF) < 0) { 1725 vhost_scsi_release_tmf_res(tmf); 1726 goto send_reject; 1727 } 1728 1729 return; 1730 1731 send_reject: 1732 vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out], 1733 VIRTIO_SCSI_S_FUNCTION_REJECTED); 1734 vhost_scsi_log_write(vq, log, log_num); 1735 } 1736 1737 static void 1738 vhost_scsi_send_an_resp(struct vhost_scsi *vs, 1739 struct vhost_virtqueue *vq, 1740 struct vhost_scsi_ctx *vc) 1741 { 1742 struct virtio_scsi_ctrl_an_resp rsp; 1743 struct iov_iter iov_iter; 1744 int ret; 1745 1746 pr_debug("%s\n", __func__); 1747 memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */ 1748 rsp.response = VIRTIO_SCSI_S_OK; 1749 1750 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, sizeof(rsp)); 1751 1752 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter); 1753 if (likely(ret == sizeof(rsp))) 1754 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0); 1755 else 1756 pr_err("Faulted on virtio_scsi_ctrl_an_resp\n"); 1757 } 1758 1759 static void 1760 vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) 1761 { 1762 struct vhost_scsi_tpg *tpg; 1763 union { 1764 __virtio32 type; 1765 struct virtio_scsi_ctrl_an_req an; 1766 struct virtio_scsi_ctrl_tmf_req tmf; 1767 } v_req; 1768 struct vhost_scsi_ctx vc; 1769 size_t typ_size; 1770 int ret, c = 0; 1771 struct vhost_log *vq_log; 1772 unsigned int log_num; 1773 1774 mutex_lock(&vq->mutex); 1775 /* 1776 * We can handle the vq only after the endpoint is setup by calling the 1777 * VHOST_SCSI_SET_ENDPOINT ioctl. 1778 */ 1779 if (!vhost_vq_get_backend(vq)) 1780 goto out; 1781 1782 memset(&vc, 0, sizeof(vc)); 1783 1784 vhost_disable_notify(&vs->dev, vq); 1785 1786 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 1787 vq->log : NULL; 1788 1789 do { 1790 ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num); 1791 if (ret) 1792 goto err; 1793 1794 /* 1795 * Get the request type first in order to setup 1796 * other parameters dependent on the type. 1797 */ 1798 vc.req = &v_req.type; 1799 typ_size = sizeof(v_req.type); 1800 1801 if (unlikely(!copy_from_iter_full(vc.req, typ_size, 1802 &vc.out_iter))) { 1803 vq_err(vq, "Faulted on copy_from_iter tmf type\n"); 1804 /* 1805 * The size of the response buffer depends on the 1806 * request type and must be validated against it. 1807 * Since the request type is not known, don't send 1808 * a response. 1809 */ 1810 continue; 1811 } 1812 1813 switch (vhost32_to_cpu(vq, v_req.type)) { 1814 case VIRTIO_SCSI_T_TMF: 1815 vc.req = &v_req.tmf; 1816 vc.req_size = sizeof(struct virtio_scsi_ctrl_tmf_req); 1817 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp); 1818 vc.lunp = &v_req.tmf.lun[0]; 1819 vc.target = &v_req.tmf.lun[1]; 1820 break; 1821 case VIRTIO_SCSI_T_AN_QUERY: 1822 case VIRTIO_SCSI_T_AN_SUBSCRIBE: 1823 vc.req = &v_req.an; 1824 vc.req_size = sizeof(struct virtio_scsi_ctrl_an_req); 1825 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp); 1826 vc.lunp = &v_req.an.lun[0]; 1827 vc.target = NULL; 1828 break; 1829 default: 1830 vq_err(vq, "Unknown control request %d", v_req.type); 1831 continue; 1832 } 1833 1834 /* 1835 * Validate the size of request and response buffers. 1836 * Check for a sane response buffer so we can report 1837 * early errors back to the guest. 1838 */ 1839 ret = vhost_scsi_chk_size(vq, &vc); 1840 if (ret) 1841 goto err; 1842 1843 /* 1844 * Get the rest of the request now that its size is known. 1845 */ 1846 vc.req += typ_size; 1847 vc.req_size -= typ_size; 1848 1849 ret = vhost_scsi_get_req(vq, &vc, &tpg); 1850 if (ret) 1851 goto err; 1852 1853 if (v_req.type == VIRTIO_SCSI_T_TMF) 1854 vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc, 1855 vq_log, log_num); 1856 else { 1857 vhost_scsi_send_an_resp(vs, vq, &vc); 1858 vhost_scsi_log_write(vq, vq_log, log_num); 1859 } 1860 err: 1861 /* 1862 * ENXIO: No more requests, or read error, wait for next kick 1863 * EINVAL: Invalid response buffer, drop the request 1864 * EIO: Respond with bad target 1865 * EAGAIN: Pending request 1866 */ 1867 if (ret == -ENXIO) 1868 break; 1869 else if (ret == -EIO) { 1870 vhost_scsi_send_bad_target(vs, vq, &vc, 1871 v_req.type == VIRTIO_SCSI_T_TMF ? 1872 TYPE_CTRL_TMF : 1873 TYPE_CTRL_AN); 1874 vhost_scsi_log_write(vq, vq_log, log_num); 1875 } 1876 } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); 1877 out: 1878 mutex_unlock(&vq->mutex); 1879 } 1880 1881 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work) 1882 { 1883 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1884 poll.work); 1885 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); 1886 1887 pr_debug("%s: The handling func for control queue.\n", __func__); 1888 vhost_scsi_ctl_handle_vq(vs, vq); 1889 } 1890 1891 static void 1892 vhost_scsi_send_evt(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 1893 struct vhost_scsi_tpg *tpg, struct se_lun *lun, 1894 u32 event, u32 reason) 1895 { 1896 struct vhost_scsi_evt *evt; 1897 1898 evt = vhost_scsi_allocate_evt(vs, event, reason); 1899 if (!evt) 1900 return; 1901 1902 if (tpg && lun) { 1903 /* TODO: share lun setup code with virtio-scsi.ko */ 1904 /* 1905 * Note: evt->event is zeroed when we allocate it and 1906 * lun[4-7] need to be zero according to virtio-scsi spec. 1907 */ 1908 evt->event.lun[0] = 0x01; 1909 evt->event.lun[1] = tpg->tport_tpgt; 1910 if (lun->unpacked_lun >= 256) 1911 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ; 1912 evt->event.lun[3] = lun->unpacked_lun & 0xFF; 1913 } 1914 1915 llist_add(&evt->list, &vs->vs_event_list); 1916 if (!vhost_vq_work_queue(vq, &vs->vs_event_work)) 1917 __vhost_scsi_complete_events(vs, true); 1918 } 1919 1920 static void vhost_scsi_evt_handle_kick(struct vhost_work *work) 1921 { 1922 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1923 poll.work); 1924 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); 1925 1926 mutex_lock(&vq->mutex); 1927 if (!vhost_vq_get_backend(vq)) 1928 goto out; 1929 1930 if (vs->vs_events_missed) 1931 vhost_scsi_send_evt(vs, vq, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 1932 0); 1933 out: 1934 mutex_unlock(&vq->mutex); 1935 } 1936 1937 static void vhost_scsi_handle_kick(struct vhost_work *work) 1938 { 1939 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1940 poll.work); 1941 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); 1942 1943 vhost_scsi_handle_vq(vs, vq); 1944 } 1945 1946 /* Callers must hold dev mutex */ 1947 static void vhost_scsi_flush(struct vhost_scsi *vs) 1948 { 1949 int i; 1950 1951 /* Init new inflight and remember the old inflight */ 1952 vhost_scsi_init_inflight(vs, vs->old_inflight); 1953 1954 /* 1955 * The inflight->kref was initialized to 1. We decrement it here to 1956 * indicate the start of the flush operation so that it will reach 0 1957 * when all the reqs are finished. 1958 */ 1959 for (i = 0; i < vs->dev.nvqs; i++) 1960 kref_put(&vs->old_inflight[i]->kref, vhost_scsi_done_inflight); 1961 1962 /* Flush both the vhost poll and vhost work */ 1963 vhost_dev_flush(&vs->dev); 1964 1965 /* Wait for all reqs issued before the flush to be finished */ 1966 for (i = 0; i < vs->dev.nvqs; i++) 1967 wait_for_completion(&vs->old_inflight[i]->comp); 1968 } 1969 1970 static void vhost_scsi_destroy_vq_log(struct vhost_virtqueue *vq) 1971 { 1972 struct vhost_scsi_virtqueue *svq = container_of(vq, 1973 struct vhost_scsi_virtqueue, vq); 1974 struct vhost_scsi_cmd *tv_cmd; 1975 unsigned int i; 1976 1977 if (!svq->scsi_cmds) 1978 return; 1979 1980 for (i = 0; i < svq->max_cmds; i++) { 1981 tv_cmd = &svq->scsi_cmds[i]; 1982 kfree(tv_cmd->tvc_log); 1983 tv_cmd->tvc_log = NULL; 1984 tv_cmd->tvc_log_num = 0; 1985 } 1986 } 1987 1988 static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq) 1989 { 1990 struct vhost_scsi_virtqueue *svq = container_of(vq, 1991 struct vhost_scsi_virtqueue, vq); 1992 struct vhost_scsi_cmd *tv_cmd; 1993 unsigned int i; 1994 1995 if (!svq->scsi_cmds) 1996 return; 1997 1998 for (i = 0; i < svq->max_cmds; i++) { 1999 tv_cmd = &svq->scsi_cmds[i]; 2000 2001 kfree(tv_cmd->sgl); 2002 kfree(tv_cmd->prot_sgl); 2003 } 2004 2005 sbitmap_free(&svq->scsi_tags); 2006 kfree(svq->upages); 2007 vhost_scsi_destroy_vq_log(vq); 2008 kfree(svq->scsi_cmds); 2009 svq->scsi_cmds = NULL; 2010 } 2011 2012 static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds) 2013 { 2014 struct vhost_scsi_virtqueue *svq = container_of(vq, 2015 struct vhost_scsi_virtqueue, vq); 2016 struct vhost_scsi *vs = svq->vs; 2017 struct vhost_scsi_cmd *tv_cmd; 2018 unsigned int i; 2019 2020 if (svq->scsi_cmds) 2021 return 0; 2022 2023 if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL, 2024 NUMA_NO_NODE, false, true)) 2025 return -ENOMEM; 2026 svq->max_cmds = max_cmds; 2027 2028 svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds); 2029 if (!svq->scsi_cmds) { 2030 sbitmap_free(&svq->scsi_tags); 2031 return -ENOMEM; 2032 } 2033 2034 svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES); 2035 if (!svq->upages) 2036 goto out; 2037 2038 for (i = 0; i < max_cmds; i++) { 2039 tv_cmd = &svq->scsi_cmds[i]; 2040 2041 if (vs->inline_sg_cnt) { 2042 tv_cmd->sgl = kzalloc_objs(struct scatterlist, 2043 vs->inline_sg_cnt); 2044 if (!tv_cmd->sgl) { 2045 pr_err("Unable to allocate tv_cmd->sgl\n"); 2046 goto out; 2047 } 2048 } 2049 2050 if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) && 2051 vs->inline_sg_cnt) { 2052 tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist, 2053 vs->inline_sg_cnt); 2054 if (!tv_cmd->prot_sgl) { 2055 pr_err("Unable to allocate tv_cmd->prot_sgl\n"); 2056 goto out; 2057 } 2058 } 2059 } 2060 return 0; 2061 out: 2062 vhost_scsi_destroy_vq_cmds(vq); 2063 return -ENOMEM; 2064 } 2065 2066 /* 2067 * Called from vhost_scsi_ioctl() context to walk the list of available 2068 * vhost_scsi_tpg with an active struct vhost_scsi_nexus 2069 * 2070 * The lock nesting rule is: 2071 * vs->dev.mutex -> vhost_scsi_mutex -> tpg->tv_tpg_mutex -> vq->mutex 2072 */ 2073 static int 2074 vhost_scsi_set_endpoint(struct vhost_scsi *vs, 2075 struct vhost_scsi_target *t) 2076 { 2077 struct se_portal_group *se_tpg; 2078 struct vhost_scsi_tport *tv_tport; 2079 struct vhost_scsi_tpg *tpg; 2080 struct vhost_scsi_tpg **vs_tpg; 2081 struct vhost_virtqueue *vq; 2082 int index, ret, i, len; 2083 bool match = false; 2084 2085 mutex_lock(&vs->dev.mutex); 2086 2087 /* Verify that ring has been setup correctly. */ 2088 for (index = 0; index < vs->dev.nvqs; ++index) { 2089 /* Verify that ring has been setup correctly. */ 2090 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { 2091 ret = -EFAULT; 2092 goto out; 2093 } 2094 } 2095 2096 if (vs->vs_tpg) { 2097 pr_err("vhost-scsi endpoint already set for %s.\n", 2098 vs->vs_vhost_wwpn); 2099 ret = -EEXIST; 2100 goto out; 2101 } 2102 2103 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET; 2104 vs_tpg = kzalloc(len, GFP_KERNEL); 2105 if (!vs_tpg) { 2106 ret = -ENOMEM; 2107 goto out; 2108 } 2109 2110 mutex_lock(&vhost_scsi_mutex); 2111 list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) { 2112 mutex_lock(&tpg->tv_tpg_mutex); 2113 if (!tpg->tpg_nexus) { 2114 mutex_unlock(&tpg->tv_tpg_mutex); 2115 continue; 2116 } 2117 if (tpg->tv_tpg_vhost_count != 0) { 2118 mutex_unlock(&tpg->tv_tpg_mutex); 2119 continue; 2120 } 2121 tv_tport = tpg->tport; 2122 2123 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) { 2124 /* 2125 * In order to ensure individual vhost-scsi configfs 2126 * groups cannot be removed while in use by vhost ioctl, 2127 * go ahead and take an explicit se_tpg->tpg_group.cg_item 2128 * dependency now. 2129 */ 2130 se_tpg = &tpg->se_tpg; 2131 ret = target_depend_item(&se_tpg->tpg_group.cg_item); 2132 if (ret) { 2133 pr_warn("target_depend_item() failed: %d\n", ret); 2134 mutex_unlock(&tpg->tv_tpg_mutex); 2135 mutex_unlock(&vhost_scsi_mutex); 2136 goto undepend; 2137 } 2138 tpg->tv_tpg_vhost_count++; 2139 tpg->vhost_scsi = vs; 2140 vs_tpg[tpg->tport_tpgt] = tpg; 2141 match = true; 2142 } 2143 mutex_unlock(&tpg->tv_tpg_mutex); 2144 } 2145 mutex_unlock(&vhost_scsi_mutex); 2146 2147 if (match) { 2148 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn, 2149 sizeof(vs->vs_vhost_wwpn)); 2150 2151 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) { 2152 vq = &vs->vqs[i].vq; 2153 if (!vhost_vq_is_setup(vq)) 2154 continue; 2155 2156 ret = vhost_scsi_setup_vq_cmds(vq, vq->num); 2157 if (ret) 2158 goto destroy_vq_cmds; 2159 } 2160 2161 for (i = 0; i < vs->dev.nvqs; i++) { 2162 vq = &vs->vqs[i].vq; 2163 if (i >= VHOST_SCSI_VQ_IO && !vs->vqs[i].scsi_cmds) 2164 continue; 2165 2166 mutex_lock(&vq->mutex); 2167 vhost_vq_set_backend(vq, vs_tpg); 2168 vhost_vq_init_access(vq); 2169 mutex_unlock(&vq->mutex); 2170 } 2171 ret = 0; 2172 } else { 2173 ret = -ENODEV; 2174 goto free_tpg; 2175 } 2176 2177 /* 2178 * Act as synchronize_rcu to make sure requests after this point 2179 * see a fully setup device. 2180 */ 2181 vhost_scsi_flush(vs); 2182 vs->vs_tpg = vs_tpg; 2183 goto out; 2184 2185 destroy_vq_cmds: 2186 for (i--; i >= VHOST_SCSI_VQ_IO; i--) { 2187 if (!vhost_vq_get_backend(&vs->vqs[i].vq)) 2188 vhost_scsi_destroy_vq_cmds(&vs->vqs[i].vq); 2189 } 2190 undepend: 2191 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 2192 tpg = vs_tpg[i]; 2193 if (tpg) { 2194 mutex_lock(&tpg->tv_tpg_mutex); 2195 tpg->vhost_scsi = NULL; 2196 tpg->tv_tpg_vhost_count--; 2197 mutex_unlock(&tpg->tv_tpg_mutex); 2198 target_undepend_item(&tpg->se_tpg.tpg_group.cg_item); 2199 } 2200 } 2201 free_tpg: 2202 kfree(vs_tpg); 2203 out: 2204 mutex_unlock(&vs->dev.mutex); 2205 return ret; 2206 } 2207 2208 static int 2209 vhost_scsi_clear_endpoint(struct vhost_scsi *vs, 2210 struct vhost_scsi_target *t) 2211 { 2212 struct se_portal_group *se_tpg; 2213 struct vhost_scsi_tport *tv_tport; 2214 struct vhost_scsi_tpg *tpg; 2215 struct vhost_virtqueue *vq; 2216 bool match = false; 2217 int index, ret, i; 2218 u8 target; 2219 2220 mutex_lock(&vs->dev.mutex); 2221 /* Verify that ring has been setup correctly. */ 2222 for (index = 0; index < vs->dev.nvqs; ++index) { 2223 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { 2224 ret = -EFAULT; 2225 goto err_dev; 2226 } 2227 } 2228 2229 if (!vs->vs_tpg) { 2230 ret = 0; 2231 goto err_dev; 2232 } 2233 2234 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 2235 target = i; 2236 tpg = vs->vs_tpg[target]; 2237 if (!tpg) 2238 continue; 2239 2240 tv_tport = tpg->tport; 2241 if (!tv_tport) { 2242 ret = -ENODEV; 2243 goto err_dev; 2244 } 2245 2246 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) { 2247 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu" 2248 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n", 2249 tv_tport->tport_name, tpg->tport_tpgt, 2250 t->vhost_wwpn, t->vhost_tpgt); 2251 ret = -EINVAL; 2252 goto err_dev; 2253 } 2254 match = true; 2255 } 2256 if (!match) 2257 goto free_vs_tpg; 2258 2259 /* Prevent new cmds from starting and accessing the tpgs/sessions */ 2260 for (i = 0; i < vs->dev.nvqs; i++) { 2261 vq = &vs->vqs[i].vq; 2262 mutex_lock(&vq->mutex); 2263 vhost_vq_set_backend(vq, NULL); 2264 mutex_unlock(&vq->mutex); 2265 } 2266 /* Make sure cmds are not running before tearing them down. */ 2267 vhost_scsi_flush(vs); 2268 2269 for (i = 0; i < vs->dev.nvqs; i++) { 2270 vq = &vs->vqs[i].vq; 2271 vhost_scsi_destroy_vq_cmds(vq); 2272 } 2273 2274 /* 2275 * We can now release our hold on the tpg and sessions and userspace 2276 * can free them after this point. 2277 */ 2278 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 2279 target = i; 2280 tpg = vs->vs_tpg[target]; 2281 if (!tpg) 2282 continue; 2283 2284 mutex_lock(&tpg->tv_tpg_mutex); 2285 2286 tpg->tv_tpg_vhost_count--; 2287 tpg->vhost_scsi = NULL; 2288 vs->vs_tpg[target] = NULL; 2289 2290 mutex_unlock(&tpg->tv_tpg_mutex); 2291 2292 se_tpg = &tpg->se_tpg; 2293 target_undepend_item(&se_tpg->tpg_group.cg_item); 2294 } 2295 2296 free_vs_tpg: 2297 /* 2298 * Act as synchronize_rcu to make sure access to 2299 * old vs->vs_tpg is finished. 2300 */ 2301 vhost_scsi_flush(vs); 2302 kfree(vs->vs_tpg); 2303 vs->vs_tpg = NULL; 2304 memset(vs->vs_vhost_wwpn, 0, sizeof(vs->vs_vhost_wwpn)); 2305 WARN_ON(vs->vs_events_nr); 2306 mutex_unlock(&vs->dev.mutex); 2307 return 0; 2308 2309 err_dev: 2310 mutex_unlock(&vs->dev.mutex); 2311 return ret; 2312 } 2313 2314 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features) 2315 { 2316 struct vhost_virtqueue *vq; 2317 bool is_log, was_log; 2318 u64 old_features; 2319 int i; 2320 2321 if (features & ~VHOST_SCSI_FEATURES) 2322 return -EOPNOTSUPP; 2323 2324 mutex_lock(&vs->dev.mutex); 2325 if ((features & (1 << VHOST_F_LOG_ALL)) && 2326 !vhost_log_access_ok(&vs->dev)) { 2327 mutex_unlock(&vs->dev.mutex); 2328 return -EFAULT; 2329 } 2330 2331 if (!vs->dev.nvqs) 2332 goto out; 2333 2334 old_features = vs->vqs[0].vq.acked_features; 2335 if (vs->vs_tpg && 2336 ((features ^ old_features) & 2337 ~(1ULL << VHOST_F_LOG_ALL))) { 2338 mutex_unlock(&vs->dev.mutex); 2339 return -EBUSY; 2340 } 2341 2342 is_log = features & (1 << VHOST_F_LOG_ALL); 2343 /* 2344 * All VQs should have same feature. 2345 */ 2346 was_log = vhost_has_feature(&vs->vqs[0].vq, VHOST_F_LOG_ALL); 2347 2348 for (i = 0; i < vs->dev.nvqs; i++) { 2349 vq = &vs->vqs[i].vq; 2350 mutex_lock(&vq->mutex); 2351 vq->acked_features = features; 2352 mutex_unlock(&vq->mutex); 2353 } 2354 2355 /* 2356 * If VHOST_F_LOG_ALL is removed, free tvc_log after 2357 * vq->acked_features is committed. 2358 */ 2359 if (!is_log && was_log) { 2360 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) { 2361 if (!vs->vqs[i].scsi_cmds) 2362 continue; 2363 2364 vq = &vs->vqs[i].vq; 2365 mutex_lock(&vq->mutex); 2366 vhost_scsi_destroy_vq_log(vq); 2367 mutex_unlock(&vq->mutex); 2368 } 2369 } 2370 2371 out: 2372 mutex_unlock(&vs->dev.mutex); 2373 return 0; 2374 } 2375 2376 static int vhost_scsi_open(struct inode *inode, struct file *f) 2377 { 2378 struct vhost_scsi_virtqueue *svq; 2379 struct vhost_scsi *vs; 2380 struct vhost_virtqueue **vqs; 2381 int r = -ENOMEM, i, nvqs; 2382 2383 vs = kvzalloc_obj(*vs); 2384 if (!vs) 2385 goto err_vs; 2386 vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt; 2387 2388 nvqs = READ_ONCE(vhost_scsi_max_io_vqs); 2389 nvqs += VHOST_SCSI_VQ_IO; 2390 2391 vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs, 2392 GFP_KERNEL | __GFP_ZERO); 2393 if (!vs->old_inflight) 2394 goto err_inflight; 2395 2396 vs->vqs = kvzalloc_objs(*vs->vqs, nvqs); 2397 if (!vs->vqs) 2398 goto err_vqs; 2399 2400 vqs = kmalloc_objs(*vqs, nvqs); 2401 if (!vqs) 2402 goto err_local_vqs; 2403 2404 vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work); 2405 2406 vs->vs_events_nr = 0; 2407 vs->vs_events_missed = false; 2408 2409 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq; 2410 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 2411 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick; 2412 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick; 2413 for (i = VHOST_SCSI_VQ_IO; i < nvqs; i++) { 2414 svq = &vs->vqs[i]; 2415 2416 vqs[i] = &svq->vq; 2417 svq->vs = vs; 2418 init_llist_head(&svq->completion_list); 2419 vhost_work_init(&svq->completion_work, 2420 vhost_scsi_complete_cmd_work); 2421 svq->vq.handle_kick = vhost_scsi_handle_kick; 2422 } 2423 vhost_dev_init(&vs->dev, vqs, nvqs, UIO_MAXIOV, 2424 VHOST_SCSI_WEIGHT, 0, true, NULL); 2425 2426 vhost_scsi_init_inflight(vs, NULL); 2427 2428 f->private_data = vs; 2429 return 0; 2430 2431 err_local_vqs: 2432 kvfree(vs->vqs); 2433 err_vqs: 2434 kfree(vs->old_inflight); 2435 err_inflight: 2436 kvfree(vs); 2437 err_vs: 2438 return r; 2439 } 2440 2441 static int vhost_scsi_release(struct inode *inode, struct file *f) 2442 { 2443 struct vhost_scsi *vs = f->private_data; 2444 struct vhost_scsi_target t; 2445 2446 mutex_lock(&vs->dev.mutex); 2447 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn)); 2448 mutex_unlock(&vs->dev.mutex); 2449 vhost_scsi_clear_endpoint(vs, &t); 2450 vhost_dev_stop(&vs->dev); 2451 vhost_dev_cleanup(&vs->dev, false); 2452 kfree(vs->dev.vqs); 2453 kvfree(vs->vqs); 2454 kfree(vs->old_inflight); 2455 kvfree(vs); 2456 return 0; 2457 } 2458 2459 static long 2460 vhost_scsi_ioctl(struct file *f, 2461 unsigned int ioctl, 2462 unsigned long arg) 2463 { 2464 struct vhost_scsi *vs = f->private_data; 2465 struct vhost_scsi_target backend; 2466 void __user *argp = (void __user *)arg; 2467 u64 __user *featurep = argp; 2468 u32 __user *eventsp = argp; 2469 u32 events_missed; 2470 u64 features; 2471 int r, abi_version = VHOST_SCSI_ABI_VERSION; 2472 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 2473 2474 switch (ioctl) { 2475 case VHOST_SCSI_SET_ENDPOINT: 2476 if (copy_from_user(&backend, argp, sizeof backend)) 2477 return -EFAULT; 2478 if (backend.reserved != 0) 2479 return -EOPNOTSUPP; 2480 2481 return vhost_scsi_set_endpoint(vs, &backend); 2482 case VHOST_SCSI_CLEAR_ENDPOINT: 2483 if (copy_from_user(&backend, argp, sizeof backend)) 2484 return -EFAULT; 2485 if (backend.reserved != 0) 2486 return -EOPNOTSUPP; 2487 2488 return vhost_scsi_clear_endpoint(vs, &backend); 2489 case VHOST_SCSI_GET_ABI_VERSION: 2490 if (copy_to_user(argp, &abi_version, sizeof abi_version)) 2491 return -EFAULT; 2492 return 0; 2493 case VHOST_SCSI_SET_EVENTS_MISSED: 2494 if (get_user(events_missed, eventsp)) 2495 return -EFAULT; 2496 mutex_lock(&vq->mutex); 2497 vs->vs_events_missed = events_missed; 2498 mutex_unlock(&vq->mutex); 2499 return 0; 2500 case VHOST_SCSI_GET_EVENTS_MISSED: 2501 mutex_lock(&vq->mutex); 2502 events_missed = vs->vs_events_missed; 2503 mutex_unlock(&vq->mutex); 2504 if (put_user(events_missed, eventsp)) 2505 return -EFAULT; 2506 return 0; 2507 case VHOST_GET_FEATURES: 2508 features = VHOST_SCSI_FEATURES; 2509 if (copy_to_user(featurep, &features, sizeof features)) 2510 return -EFAULT; 2511 return 0; 2512 case VHOST_SET_FEATURES: 2513 if (copy_from_user(&features, featurep, sizeof features)) 2514 return -EFAULT; 2515 return vhost_scsi_set_features(vs, features); 2516 case VHOST_NEW_WORKER: 2517 case VHOST_FREE_WORKER: 2518 case VHOST_ATTACH_VRING_WORKER: 2519 case VHOST_GET_VRING_WORKER: 2520 mutex_lock(&vs->dev.mutex); 2521 r = vhost_worker_ioctl(&vs->dev, ioctl, argp); 2522 mutex_unlock(&vs->dev.mutex); 2523 return r; 2524 default: 2525 mutex_lock(&vs->dev.mutex); 2526 r = vhost_dev_ioctl(&vs->dev, ioctl, argp); 2527 if (r == -ENOIOCTLCMD) { 2528 if (vs->vs_tpg && 2529 (ioctl == VHOST_SET_VRING_NUM || 2530 ioctl == VHOST_SET_VRING_ADDR)) 2531 r = -EBUSY; 2532 else 2533 r = vhost_vring_ioctl(&vs->dev, ioctl, argp); 2534 } else { 2535 vhost_scsi_flush(vs); 2536 } 2537 mutex_unlock(&vs->dev.mutex); 2538 return r; 2539 } 2540 } 2541 2542 static const struct file_operations vhost_scsi_fops = { 2543 .owner = THIS_MODULE, 2544 .release = vhost_scsi_release, 2545 .unlocked_ioctl = vhost_scsi_ioctl, 2546 .compat_ioctl = compat_ptr_ioctl, 2547 .open = vhost_scsi_open, 2548 .llseek = noop_llseek, 2549 }; 2550 2551 static struct miscdevice vhost_scsi_misc = { 2552 MISC_DYNAMIC_MINOR, 2553 "vhost-scsi", 2554 &vhost_scsi_fops, 2555 }; 2556 2557 static int __init vhost_scsi_register(void) 2558 { 2559 return misc_register(&vhost_scsi_misc); 2560 } 2561 2562 static void vhost_scsi_deregister(void) 2563 { 2564 misc_deregister(&vhost_scsi_misc); 2565 } 2566 2567 static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport) 2568 { 2569 switch (tport->tport_proto_id) { 2570 case SCSI_PROTOCOL_SAS: 2571 return "SAS"; 2572 case SCSI_PROTOCOL_FCP: 2573 return "FCP"; 2574 case SCSI_PROTOCOL_ISCSI: 2575 return "iSCSI"; 2576 default: 2577 break; 2578 } 2579 2580 return "Unknown"; 2581 } 2582 2583 static void 2584 vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg, 2585 struct se_lun *lun, bool plug) 2586 { 2587 2588 struct vhost_scsi *vs = tpg->vhost_scsi; 2589 struct vhost_virtqueue *vq; 2590 u32 reason; 2591 2592 if (!vs) 2593 return; 2594 2595 if (plug) 2596 reason = VIRTIO_SCSI_EVT_RESET_RESCAN; 2597 else 2598 reason = VIRTIO_SCSI_EVT_RESET_REMOVED; 2599 2600 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 2601 mutex_lock(&vq->mutex); 2602 /* 2603 * We can't queue events if the backend has been cleared, because 2604 * we could end up queueing an event after the flush. 2605 */ 2606 if (!vhost_vq_get_backend(vq)) 2607 goto unlock; 2608 2609 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG)) 2610 vhost_scsi_send_evt(vs, vq, tpg, lun, 2611 VIRTIO_SCSI_T_TRANSPORT_RESET, reason); 2612 unlock: 2613 mutex_unlock(&vq->mutex); 2614 } 2615 2616 static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun) 2617 { 2618 vhost_scsi_do_plug(tpg, lun, true); 2619 } 2620 2621 static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun) 2622 { 2623 vhost_scsi_do_plug(tpg, lun, false); 2624 } 2625 2626 static int vhost_scsi_port_link(struct se_portal_group *se_tpg, 2627 struct se_lun *lun) 2628 { 2629 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2630 struct vhost_scsi_tpg, se_tpg); 2631 2632 mutex_lock(&tpg->tv_tpg_mutex); 2633 tpg->tv_tpg_port_count++; 2634 vhost_scsi_hotplug(tpg, lun); 2635 mutex_unlock(&tpg->tv_tpg_mutex); 2636 2637 return 0; 2638 } 2639 2640 static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg, 2641 struct se_lun *lun) 2642 { 2643 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2644 struct vhost_scsi_tpg, se_tpg); 2645 2646 mutex_lock(&tpg->tv_tpg_mutex); 2647 tpg->tv_tpg_port_count--; 2648 vhost_scsi_hotunplug(tpg, lun); 2649 mutex_unlock(&tpg->tv_tpg_mutex); 2650 } 2651 2652 static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store( 2653 struct config_item *item, const char *page, size_t count) 2654 { 2655 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2656 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2657 struct vhost_scsi_tpg, se_tpg); 2658 unsigned long val; 2659 int ret = kstrtoul(page, 0, &val); 2660 2661 if (ret) { 2662 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret); 2663 return ret; 2664 } 2665 if (val != 0 && val != 1 && val != 3) { 2666 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val); 2667 return -EINVAL; 2668 } 2669 tpg->tv_fabric_prot_type = val; 2670 2671 return count; 2672 } 2673 2674 static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show( 2675 struct config_item *item, char *page) 2676 { 2677 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2678 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2679 struct vhost_scsi_tpg, se_tpg); 2680 2681 return sysfs_emit(page, "%d\n", tpg->tv_fabric_prot_type); 2682 } 2683 2684 CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type); 2685 2686 static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = { 2687 &vhost_scsi_tpg_attrib_attr_fabric_prot_type, 2688 NULL, 2689 }; 2690 2691 static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg, 2692 const char *name) 2693 { 2694 struct vhost_scsi_nexus *tv_nexus; 2695 2696 mutex_lock(&tpg->tv_tpg_mutex); 2697 if (tpg->tpg_nexus) { 2698 mutex_unlock(&tpg->tv_tpg_mutex); 2699 pr_debug("tpg->tpg_nexus already exists\n"); 2700 return -EEXIST; 2701 } 2702 2703 tv_nexus = kzalloc_obj(*tv_nexus); 2704 if (!tv_nexus) { 2705 mutex_unlock(&tpg->tv_tpg_mutex); 2706 pr_err("Unable to allocate struct vhost_scsi_nexus\n"); 2707 return -ENOMEM; 2708 } 2709 /* 2710 * Since we are running in 'demo mode' this call will generate a 2711 * struct se_node_acl for the vhost_scsi struct se_portal_group with 2712 * the SCSI Initiator port name of the passed configfs group 'name'. 2713 */ 2714 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 0, 0, 2715 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS, 2716 (unsigned char *)name, tv_nexus, NULL); 2717 if (IS_ERR(tv_nexus->tvn_se_sess)) { 2718 mutex_unlock(&tpg->tv_tpg_mutex); 2719 kfree(tv_nexus); 2720 return -ENOMEM; 2721 } 2722 tpg->tpg_nexus = tv_nexus; 2723 2724 mutex_unlock(&tpg->tv_tpg_mutex); 2725 return 0; 2726 } 2727 2728 static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg) 2729 { 2730 struct se_session *se_sess; 2731 struct vhost_scsi_nexus *tv_nexus; 2732 2733 mutex_lock(&tpg->tv_tpg_mutex); 2734 tv_nexus = tpg->tpg_nexus; 2735 if (!tv_nexus) { 2736 mutex_unlock(&tpg->tv_tpg_mutex); 2737 return -ENODEV; 2738 } 2739 2740 se_sess = tv_nexus->tvn_se_sess; 2741 if (!se_sess) { 2742 mutex_unlock(&tpg->tv_tpg_mutex); 2743 return -ENODEV; 2744 } 2745 2746 if (tpg->tv_tpg_port_count != 0) { 2747 mutex_unlock(&tpg->tv_tpg_mutex); 2748 pr_err("Unable to remove TCM_vhost I_T Nexus with" 2749 " active TPG port count: %d\n", 2750 tpg->tv_tpg_port_count); 2751 return -EBUSY; 2752 } 2753 2754 if (tpg->tv_tpg_vhost_count != 0) { 2755 mutex_unlock(&tpg->tv_tpg_mutex); 2756 pr_err("Unable to remove TCM_vhost I_T Nexus with" 2757 " active TPG vhost count: %d\n", 2758 tpg->tv_tpg_vhost_count); 2759 return -EBUSY; 2760 } 2761 2762 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated" 2763 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport), 2764 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 2765 2766 /* 2767 * Release the SCSI I_T Nexus to the emulated vhost Target Port 2768 */ 2769 target_remove_session(se_sess); 2770 tpg->tpg_nexus = NULL; 2771 mutex_unlock(&tpg->tv_tpg_mutex); 2772 2773 kfree(tv_nexus); 2774 return 0; 2775 } 2776 2777 static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page) 2778 { 2779 struct se_portal_group *se_tpg = to_tpg(item); 2780 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2781 struct vhost_scsi_tpg, se_tpg); 2782 struct vhost_scsi_nexus *tv_nexus; 2783 ssize_t ret; 2784 2785 mutex_lock(&tpg->tv_tpg_mutex); 2786 tv_nexus = tpg->tpg_nexus; 2787 if (!tv_nexus) { 2788 mutex_unlock(&tpg->tv_tpg_mutex); 2789 return -ENODEV; 2790 } 2791 ret = sysfs_emit(page, "%s\n", 2792 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 2793 mutex_unlock(&tpg->tv_tpg_mutex); 2794 2795 return ret; 2796 } 2797 2798 static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item, 2799 const char *page, size_t count) 2800 { 2801 struct se_portal_group *se_tpg = to_tpg(item); 2802 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2803 struct vhost_scsi_tpg, se_tpg); 2804 struct vhost_scsi_tport *tport_wwn = tpg->tport; 2805 unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr; 2806 int ret; 2807 /* 2808 * Shutdown the active I_T nexus if 'NULL' is passed.. 2809 */ 2810 if (!strncmp(page, "NULL", 4)) { 2811 ret = vhost_scsi_drop_nexus(tpg); 2812 return (!ret) ? count : ret; 2813 } 2814 /* 2815 * Otherwise make sure the passed virtual Initiator port WWN matches 2816 * the fabric protocol_id set in vhost_scsi_make_tport(), and call 2817 * vhost_scsi_make_nexus(). 2818 */ 2819 if (strlen(page) >= VHOST_SCSI_NAMELEN) { 2820 pr_err("Emulated NAA Sas Address: %s, exceeds" 2821 " max: %d\n", page, VHOST_SCSI_NAMELEN); 2822 return -EINVAL; 2823 } 2824 snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page); 2825 2826 ptr = strstr(i_port, "naa."); 2827 if (ptr) { 2828 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { 2829 pr_err("Passed SAS Initiator Port %s does not" 2830 " match target port protoid: %s\n", i_port, 2831 vhost_scsi_dump_proto_id(tport_wwn)); 2832 return -EINVAL; 2833 } 2834 port_ptr = &i_port[0]; 2835 goto check_newline; 2836 } 2837 ptr = strstr(i_port, "fc."); 2838 if (ptr) { 2839 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { 2840 pr_err("Passed FCP Initiator Port %s does not" 2841 " match target port protoid: %s\n", i_port, 2842 vhost_scsi_dump_proto_id(tport_wwn)); 2843 return -EINVAL; 2844 } 2845 port_ptr = &i_port[3]; /* Skip over "fc." */ 2846 goto check_newline; 2847 } 2848 ptr = strstr(i_port, "iqn."); 2849 if (ptr) { 2850 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { 2851 pr_err("Passed iSCSI Initiator Port %s does not" 2852 " match target port protoid: %s\n", i_port, 2853 vhost_scsi_dump_proto_id(tport_wwn)); 2854 return -EINVAL; 2855 } 2856 port_ptr = &i_port[0]; 2857 goto check_newline; 2858 } 2859 pr_err("Unable to locate prefix for emulated Initiator Port:" 2860 " %s\n", i_port); 2861 return -EINVAL; 2862 /* 2863 * Clear any trailing newline for the NAA WWN 2864 */ 2865 check_newline: 2866 if (i_port[strlen(i_port)-1] == '\n') 2867 i_port[strlen(i_port)-1] = '\0'; 2868 2869 ret = vhost_scsi_make_nexus(tpg, port_ptr); 2870 if (ret < 0) 2871 return ret; 2872 2873 return count; 2874 } 2875 2876 CONFIGFS_ATTR(vhost_scsi_tpg_, nexus); 2877 2878 static struct configfs_attribute *vhost_scsi_tpg_attrs[] = { 2879 &vhost_scsi_tpg_attr_nexus, 2880 NULL, 2881 }; 2882 2883 static struct se_portal_group * 2884 vhost_scsi_make_tpg(struct se_wwn *wwn, const char *name) 2885 { 2886 struct vhost_scsi_tport *tport = container_of(wwn, 2887 struct vhost_scsi_tport, tport_wwn); 2888 2889 struct vhost_scsi_tpg *tpg; 2890 u16 tpgt; 2891 int ret; 2892 2893 if (strstr(name, "tpgt_") != name) 2894 return ERR_PTR(-EINVAL); 2895 if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET) 2896 return ERR_PTR(-EINVAL); 2897 2898 tpg = kzalloc_obj(*tpg); 2899 if (!tpg) { 2900 pr_err("Unable to allocate struct vhost_scsi_tpg"); 2901 return ERR_PTR(-ENOMEM); 2902 } 2903 mutex_init(&tpg->tv_tpg_mutex); 2904 INIT_LIST_HEAD(&tpg->tv_tpg_list); 2905 tpg->tport = tport; 2906 tpg->tport_tpgt = tpgt; 2907 2908 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id); 2909 if (ret < 0) { 2910 kfree(tpg); 2911 return NULL; 2912 } 2913 mutex_lock(&vhost_scsi_mutex); 2914 list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list); 2915 mutex_unlock(&vhost_scsi_mutex); 2916 2917 return &tpg->se_tpg; 2918 } 2919 2920 static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg) 2921 { 2922 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2923 struct vhost_scsi_tpg, se_tpg); 2924 2925 mutex_lock(&vhost_scsi_mutex); 2926 list_del(&tpg->tv_tpg_list); 2927 mutex_unlock(&vhost_scsi_mutex); 2928 /* 2929 * Release the virtual I_T Nexus for this vhost TPG 2930 */ 2931 vhost_scsi_drop_nexus(tpg); 2932 /* 2933 * Deregister the se_tpg from TCM.. 2934 */ 2935 core_tpg_deregister(se_tpg); 2936 kfree(tpg); 2937 } 2938 2939 static struct se_wwn * 2940 vhost_scsi_make_tport(struct target_fabric_configfs *tf, 2941 struct config_group *group, 2942 const char *name) 2943 { 2944 struct vhost_scsi_tport *tport; 2945 char *ptr; 2946 u64 wwpn = 0; 2947 int off = 0; 2948 2949 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0) 2950 return ERR_PTR(-EINVAL); */ 2951 2952 tport = kzalloc_obj(*tport); 2953 if (!tport) { 2954 pr_err("Unable to allocate struct vhost_scsi_tport"); 2955 return ERR_PTR(-ENOMEM); 2956 } 2957 tport->tport_wwpn = wwpn; 2958 /* 2959 * Determine the emulated Protocol Identifier and Target Port Name 2960 * based on the incoming configfs directory name. 2961 */ 2962 ptr = strstr(name, "naa."); 2963 if (ptr) { 2964 tport->tport_proto_id = SCSI_PROTOCOL_SAS; 2965 goto check_len; 2966 } 2967 ptr = strstr(name, "fc."); 2968 if (ptr) { 2969 tport->tport_proto_id = SCSI_PROTOCOL_FCP; 2970 off = 3; /* Skip over "fc." */ 2971 goto check_len; 2972 } 2973 ptr = strstr(name, "iqn."); 2974 if (ptr) { 2975 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; 2976 goto check_len; 2977 } 2978 2979 pr_err("Unable to locate prefix for emulated Target Port:" 2980 " %s\n", name); 2981 kfree(tport); 2982 return ERR_PTR(-EINVAL); 2983 2984 check_len: 2985 if (strlen(name) >= VHOST_SCSI_NAMELEN) { 2986 pr_err("Emulated %s Address: %s, exceeds" 2987 " max: %d\n", vhost_scsi_dump_proto_id(tport), name, 2988 VHOST_SCSI_NAMELEN); 2989 kfree(tport); 2990 return ERR_PTR(-EINVAL); 2991 } 2992 snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]); 2993 2994 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target" 2995 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name); 2996 2997 return &tport->tport_wwn; 2998 } 2999 3000 static void vhost_scsi_drop_tport(struct se_wwn *wwn) 3001 { 3002 struct vhost_scsi_tport *tport = container_of(wwn, 3003 struct vhost_scsi_tport, tport_wwn); 3004 3005 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target" 3006 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), 3007 tport->tport_name); 3008 3009 kfree(tport); 3010 } 3011 3012 static ssize_t 3013 vhost_scsi_wwn_version_show(struct config_item *item, char *page) 3014 { 3015 return sysfs_emit(page, "TCM_VHOST fabric module %s on %s/%s" 3016 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname, 3017 utsname()->machine); 3018 } 3019 3020 CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version); 3021 3022 static struct configfs_attribute *vhost_scsi_wwn_attrs[] = { 3023 &vhost_scsi_wwn_attr_version, 3024 NULL, 3025 }; 3026 3027 static const struct target_core_fabric_ops vhost_scsi_ops = { 3028 .module = THIS_MODULE, 3029 .fabric_name = "vhost", 3030 .max_data_sg_nents = VHOST_SCSI_PREALLOC_SGLS, 3031 .tpg_get_wwn = vhost_scsi_get_fabric_wwn, 3032 .tpg_get_tag = vhost_scsi_get_tpgt, 3033 .tpg_check_demo_mode = vhost_scsi_check_true, 3034 .tpg_check_demo_mode_cache = vhost_scsi_check_true, 3035 .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only, 3036 .release_cmd = vhost_scsi_release_cmd, 3037 .check_stop_free = vhost_scsi_check_stop_free, 3038 .sess_get_initiator_sid = NULL, 3039 .write_pending = vhost_scsi_write_pending, 3040 .queue_data_in = vhost_scsi_queue_data_in, 3041 .queue_status = vhost_scsi_queue_status, 3042 .queue_tm_rsp = vhost_scsi_queue_tm_rsp, 3043 .aborted_task = vhost_scsi_aborted_task, 3044 /* 3045 * Setup callers for generic logic in target_core_fabric_configfs.c 3046 */ 3047 .fabric_make_wwn = vhost_scsi_make_tport, 3048 .fabric_drop_wwn = vhost_scsi_drop_tport, 3049 .fabric_make_tpg = vhost_scsi_make_tpg, 3050 .fabric_drop_tpg = vhost_scsi_drop_tpg, 3051 .fabric_post_link = vhost_scsi_port_link, 3052 .fabric_pre_unlink = vhost_scsi_port_unlink, 3053 3054 .tfc_wwn_attrs = vhost_scsi_wwn_attrs, 3055 .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs, 3056 .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs, 3057 3058 .default_compl_type = TARGET_QUEUE_COMPL, 3059 .direct_compl_supp = 1, 3060 .default_submit_type = TARGET_QUEUE_SUBMIT, 3061 .direct_submit_supp = 1, 3062 }; 3063 3064 static int __init vhost_scsi_init(void) 3065 { 3066 int ret = -ENOMEM; 3067 3068 pr_debug("TCM_VHOST fabric module %s on %s/%s" 3069 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname, 3070 utsname()->machine); 3071 3072 ret = vhost_scsi_register(); 3073 if (ret < 0) 3074 goto out; 3075 3076 ret = target_register_template(&vhost_scsi_ops); 3077 if (ret < 0) 3078 goto out_vhost_scsi_deregister; 3079 3080 return 0; 3081 3082 out_vhost_scsi_deregister: 3083 vhost_scsi_deregister(); 3084 out: 3085 return ret; 3086 } 3087 3088 static void vhost_scsi_exit(void) 3089 { 3090 target_unregister_template(&vhost_scsi_ops); 3091 vhost_scsi_deregister(); 3092 } 3093 3094 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver"); 3095 MODULE_ALIAS("tcm_vhost"); 3096 MODULE_LICENSE("GPL"); 3097 module_init(vhost_scsi_init); 3098 module_exit(vhost_scsi_exit); 3099