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