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