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 <asm/unaligned.h> 29 #include <scsi/scsi_common.h> 30 #include <scsi/scsi_proto.h> 31 #include <target/target_core_base.h> 32 #include <target/target_core_fabric.h> 33 #include <linux/vhost.h> 34 #include <linux/virtio_scsi.h> 35 #include <linux/llist.h> 36 #include <linux/bitmap.h> 37 38 #include "vhost.h" 39 40 #define VHOST_SCSI_VERSION "v0.1" 41 #define VHOST_SCSI_NAMELEN 256 42 #define VHOST_SCSI_MAX_CDB_SIZE 32 43 #define VHOST_SCSI_PREALLOC_SGLS 2048 44 #define VHOST_SCSI_PREALLOC_UPAGES 2048 45 #define VHOST_SCSI_PREALLOC_PROT_SGLS 2048 46 47 /* Max number of requests before requeueing the job. 48 * Using this limit prevents one virtqueue from starving others with 49 * request. 50 */ 51 #define VHOST_SCSI_WEIGHT 256 52 53 struct vhost_scsi_inflight { 54 /* Wait for the flush operation to finish */ 55 struct completion comp; 56 /* Refcount for the inflight reqs */ 57 struct kref kref; 58 }; 59 60 struct vhost_scsi_cmd { 61 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */ 62 int tvc_vq_desc; 63 /* virtio-scsi initiator task attribute */ 64 int tvc_task_attr; 65 /* virtio-scsi response incoming iovecs */ 66 int tvc_in_iovs; 67 /* virtio-scsi initiator data direction */ 68 enum dma_data_direction tvc_data_direction; 69 /* Expected data transfer length from virtio-scsi header */ 70 u32 tvc_exp_data_len; 71 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */ 72 u64 tvc_tag; 73 /* The number of scatterlists associated with this cmd */ 74 u32 tvc_sgl_count; 75 u32 tvc_prot_sgl_count; 76 /* Saved unpacked SCSI LUN for vhost_scsi_target_queue_cmd() */ 77 u32 tvc_lun; 78 /* Pointer to the SGL formatted memory from virtio-scsi */ 79 struct scatterlist *tvc_sgl; 80 struct scatterlist *tvc_prot_sgl; 81 struct page **tvc_upages; 82 /* Pointer to response header iovec */ 83 struct iovec *tvc_resp_iov; 84 /* Pointer to vhost_scsi for our device */ 85 struct vhost_scsi *tvc_vhost; 86 /* Pointer to vhost_virtqueue for the cmd */ 87 struct vhost_virtqueue *tvc_vq; 88 /* Pointer to vhost nexus memory */ 89 struct vhost_scsi_nexus *tvc_nexus; 90 /* The TCM I/O descriptor that is accessed via container_of() */ 91 struct se_cmd tvc_se_cmd; 92 /* Copy of the incoming SCSI command descriptor block (CDB) */ 93 unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE]; 94 /* Sense buffer that will be mapped into outgoing status */ 95 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER]; 96 /* Completed commands list, serviced from vhost worker thread */ 97 struct llist_node tvc_completion_list; 98 /* Used to track inflight cmd */ 99 struct vhost_scsi_inflight *inflight; 100 }; 101 102 struct vhost_scsi_nexus { 103 /* Pointer to TCM session for I_T Nexus */ 104 struct se_session *tvn_se_sess; 105 }; 106 107 struct vhost_scsi_tpg { 108 /* Vhost port target portal group tag for TCM */ 109 u16 tport_tpgt; 110 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */ 111 int tv_tpg_port_count; 112 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */ 113 int tv_tpg_vhost_count; 114 /* Used for enabling T10-PI with legacy devices */ 115 int tv_fabric_prot_type; 116 /* list for vhost_scsi_list */ 117 struct list_head tv_tpg_list; 118 /* Used to protect access for tpg_nexus */ 119 struct mutex tv_tpg_mutex; 120 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */ 121 struct vhost_scsi_nexus *tpg_nexus; 122 /* Pointer back to vhost_scsi_tport */ 123 struct vhost_scsi_tport *tport; 124 /* Returned by vhost_scsi_make_tpg() */ 125 struct se_portal_group se_tpg; 126 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */ 127 struct vhost_scsi *vhost_scsi; 128 }; 129 130 struct vhost_scsi_tport { 131 /* SCSI protocol the tport is providing */ 132 u8 tport_proto_id; 133 /* Binary World Wide unique Port Name for Vhost Target port */ 134 u64 tport_wwpn; 135 /* ASCII formatted WWPN for Vhost Target port */ 136 char tport_name[VHOST_SCSI_NAMELEN]; 137 /* Returned by vhost_scsi_make_tport() */ 138 struct se_wwn tport_wwn; 139 }; 140 141 struct vhost_scsi_evt { 142 /* event to be sent to guest */ 143 struct virtio_scsi_event event; 144 /* event list, serviced from vhost worker thread */ 145 struct llist_node list; 146 }; 147 148 enum { 149 VHOST_SCSI_VQ_CTL = 0, 150 VHOST_SCSI_VQ_EVT = 1, 151 VHOST_SCSI_VQ_IO = 2, 152 }; 153 154 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */ 155 enum { 156 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) | 157 (1ULL << VIRTIO_SCSI_F_T10_PI) 158 }; 159 160 #define VHOST_SCSI_MAX_TARGET 256 161 #define VHOST_SCSI_MAX_IO_VQ 1024 162 #define VHOST_SCSI_MAX_EVENT 128 163 164 static unsigned vhost_scsi_max_io_vqs = 128; 165 module_param_named(max_io_vqs, vhost_scsi_max_io_vqs, uint, 0644); 166 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."); 167 168 struct vhost_scsi_virtqueue { 169 struct vhost_virtqueue vq; 170 /* 171 * Reference counting for inflight reqs, used for flush operation. At 172 * each time, one reference tracks new commands submitted, while we 173 * wait for another one to reach 0. 174 */ 175 struct vhost_scsi_inflight inflights[2]; 176 /* 177 * Indicate current inflight in use, protected by vq->mutex. 178 * Writers must also take dev mutex and flush under it. 179 */ 180 int inflight_idx; 181 struct vhost_scsi_cmd *scsi_cmds; 182 struct sbitmap scsi_tags; 183 int max_cmds; 184 }; 185 186 struct vhost_scsi { 187 /* Protected by vhost_scsi->dev.mutex */ 188 struct vhost_scsi_tpg **vs_tpg; 189 char vs_vhost_wwpn[TRANSPORT_IQN_LEN]; 190 191 struct vhost_dev dev; 192 struct vhost_scsi_virtqueue *vqs; 193 unsigned long *compl_bitmap; 194 struct vhost_scsi_inflight **old_inflight; 195 196 struct vhost_work vs_completion_work; /* cmd completion work item */ 197 struct llist_head vs_completion_list; /* cmd completion queue */ 198 199 struct vhost_work vs_event_work; /* evt injection work item */ 200 struct llist_head vs_event_list; /* evt injection queue */ 201 202 bool vs_events_missed; /* any missed events, protected by vq->mutex */ 203 int vs_events_nr; /* num of pending events, protected by vq->mutex */ 204 }; 205 206 struct vhost_scsi_tmf { 207 struct vhost_work vwork; 208 struct vhost_scsi *vhost; 209 struct vhost_scsi_virtqueue *svq; 210 211 struct se_cmd se_cmd; 212 u8 scsi_resp; 213 struct vhost_scsi_inflight *inflight; 214 struct iovec resp_iov; 215 int in_iovs; 216 int vq_desc; 217 }; 218 219 /* 220 * Context for processing request and control queue operations. 221 */ 222 struct vhost_scsi_ctx { 223 int head; 224 unsigned int out, in; 225 size_t req_size, rsp_size; 226 size_t out_size, in_size; 227 u8 *target, *lunp; 228 void *req; 229 struct iov_iter out_iter; 230 }; 231 232 /* 233 * Global mutex to protect vhost_scsi TPG list for vhost IOCTLs and LIO 234 * configfs management operations. 235 */ 236 static DEFINE_MUTEX(vhost_scsi_mutex); 237 static LIST_HEAD(vhost_scsi_list); 238 239 static void vhost_scsi_done_inflight(struct kref *kref) 240 { 241 struct vhost_scsi_inflight *inflight; 242 243 inflight = container_of(kref, struct vhost_scsi_inflight, kref); 244 complete(&inflight->comp); 245 } 246 247 static void vhost_scsi_init_inflight(struct vhost_scsi *vs, 248 struct vhost_scsi_inflight *old_inflight[]) 249 { 250 struct vhost_scsi_inflight *new_inflight; 251 struct vhost_virtqueue *vq; 252 int idx, i; 253 254 for (i = 0; i < vs->dev.nvqs; i++) { 255 vq = &vs->vqs[i].vq; 256 257 mutex_lock(&vq->mutex); 258 259 /* store old infight */ 260 idx = vs->vqs[i].inflight_idx; 261 if (old_inflight) 262 old_inflight[i] = &vs->vqs[i].inflights[idx]; 263 264 /* setup new infight */ 265 vs->vqs[i].inflight_idx = idx ^ 1; 266 new_inflight = &vs->vqs[i].inflights[idx ^ 1]; 267 kref_init(&new_inflight->kref); 268 init_completion(&new_inflight->comp); 269 270 mutex_unlock(&vq->mutex); 271 } 272 } 273 274 static struct vhost_scsi_inflight * 275 vhost_scsi_get_inflight(struct vhost_virtqueue *vq) 276 { 277 struct vhost_scsi_inflight *inflight; 278 struct vhost_scsi_virtqueue *svq; 279 280 svq = container_of(vq, struct vhost_scsi_virtqueue, vq); 281 inflight = &svq->inflights[svq->inflight_idx]; 282 kref_get(&inflight->kref); 283 284 return inflight; 285 } 286 287 static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight) 288 { 289 kref_put(&inflight->kref, vhost_scsi_done_inflight); 290 } 291 292 static int vhost_scsi_check_true(struct se_portal_group *se_tpg) 293 { 294 return 1; 295 } 296 297 static int vhost_scsi_check_false(struct se_portal_group *se_tpg) 298 { 299 return 0; 300 } 301 302 static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg) 303 { 304 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 305 struct vhost_scsi_tpg, se_tpg); 306 struct vhost_scsi_tport *tport = tpg->tport; 307 308 return &tport->tport_name[0]; 309 } 310 311 static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg) 312 { 313 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 314 struct vhost_scsi_tpg, se_tpg); 315 return tpg->tport_tpgt; 316 } 317 318 static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg) 319 { 320 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 321 struct vhost_scsi_tpg, se_tpg); 322 323 return tpg->tv_fabric_prot_type; 324 } 325 326 static u32 vhost_scsi_tpg_get_inst_index(struct se_portal_group *se_tpg) 327 { 328 return 1; 329 } 330 331 static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd) 332 { 333 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd, 334 struct vhost_scsi_cmd, tvc_se_cmd); 335 struct vhost_scsi_virtqueue *svq = container_of(tv_cmd->tvc_vq, 336 struct vhost_scsi_virtqueue, vq); 337 struct vhost_scsi_inflight *inflight = tv_cmd->inflight; 338 int i; 339 340 if (tv_cmd->tvc_sgl_count) { 341 for (i = 0; i < tv_cmd->tvc_sgl_count; i++) 342 put_page(sg_page(&tv_cmd->tvc_sgl[i])); 343 } 344 if (tv_cmd->tvc_prot_sgl_count) { 345 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++) 346 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i])); 347 } 348 349 sbitmap_clear_bit(&svq->scsi_tags, se_cmd->map_tag); 350 vhost_scsi_put_inflight(inflight); 351 } 352 353 static void vhost_scsi_release_tmf_res(struct vhost_scsi_tmf *tmf) 354 { 355 struct vhost_scsi_inflight *inflight = tmf->inflight; 356 357 kfree(tmf); 358 vhost_scsi_put_inflight(inflight); 359 } 360 361 static void vhost_scsi_release_cmd(struct se_cmd *se_cmd) 362 { 363 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) { 364 struct vhost_scsi_tmf *tmf = container_of(se_cmd, 365 struct vhost_scsi_tmf, se_cmd); 366 367 vhost_work_queue(&tmf->vhost->dev, &tmf->vwork); 368 } else { 369 struct vhost_scsi_cmd *cmd = container_of(se_cmd, 370 struct vhost_scsi_cmd, tvc_se_cmd); 371 struct vhost_scsi *vs = cmd->tvc_vhost; 372 373 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list); 374 vhost_work_queue(&vs->dev, &vs->vs_completion_work); 375 } 376 } 377 378 static u32 vhost_scsi_sess_get_index(struct se_session *se_sess) 379 { 380 return 0; 381 } 382 383 static int vhost_scsi_write_pending(struct se_cmd *se_cmd) 384 { 385 /* Go ahead and process the write immediately */ 386 target_execute_cmd(se_cmd); 387 return 0; 388 } 389 390 static void vhost_scsi_set_default_node_attrs(struct se_node_acl *nacl) 391 { 392 return; 393 } 394 395 static int vhost_scsi_get_cmd_state(struct se_cmd *se_cmd) 396 { 397 return 0; 398 } 399 400 static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd) 401 { 402 transport_generic_free_cmd(se_cmd, 0); 403 return 0; 404 } 405 406 static int vhost_scsi_queue_status(struct se_cmd *se_cmd) 407 { 408 transport_generic_free_cmd(se_cmd, 0); 409 return 0; 410 } 411 412 static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd) 413 { 414 struct vhost_scsi_tmf *tmf = container_of(se_cmd, struct vhost_scsi_tmf, 415 se_cmd); 416 417 tmf->scsi_resp = se_cmd->se_tmr_req->response; 418 transport_generic_free_cmd(&tmf->se_cmd, 0); 419 } 420 421 static void vhost_scsi_aborted_task(struct se_cmd *se_cmd) 422 { 423 return; 424 } 425 426 static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) 427 { 428 vs->vs_events_nr--; 429 kfree(evt); 430 } 431 432 static struct vhost_scsi_evt * 433 vhost_scsi_allocate_evt(struct vhost_scsi *vs, 434 u32 event, u32 reason) 435 { 436 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 437 struct vhost_scsi_evt *evt; 438 439 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) { 440 vs->vs_events_missed = true; 441 return NULL; 442 } 443 444 evt = kzalloc(sizeof(*evt), GFP_KERNEL); 445 if (!evt) { 446 vq_err(vq, "Failed to allocate vhost_scsi_evt\n"); 447 vs->vs_events_missed = true; 448 return NULL; 449 } 450 451 evt->event.event = cpu_to_vhost32(vq, event); 452 evt->event.reason = cpu_to_vhost32(vq, reason); 453 vs->vs_events_nr++; 454 455 return evt; 456 } 457 458 static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd) 459 { 460 return target_put_sess_cmd(se_cmd); 461 } 462 463 static void 464 vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) 465 { 466 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 467 struct virtio_scsi_event *event = &evt->event; 468 struct virtio_scsi_event __user *eventp; 469 unsigned out, in; 470 int head, ret; 471 472 if (!vhost_vq_get_backend(vq)) { 473 vs->vs_events_missed = true; 474 return; 475 } 476 477 again: 478 vhost_disable_notify(&vs->dev, vq); 479 head = vhost_get_vq_desc(vq, vq->iov, 480 ARRAY_SIZE(vq->iov), &out, &in, 481 NULL, NULL); 482 if (head < 0) { 483 vs->vs_events_missed = true; 484 return; 485 } 486 if (head == vq->num) { 487 if (vhost_enable_notify(&vs->dev, vq)) 488 goto again; 489 vs->vs_events_missed = true; 490 return; 491 } 492 493 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) { 494 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n", 495 vq->iov[out].iov_len); 496 vs->vs_events_missed = true; 497 return; 498 } 499 500 if (vs->vs_events_missed) { 501 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED); 502 vs->vs_events_missed = false; 503 } 504 505 eventp = vq->iov[out].iov_base; 506 ret = __copy_to_user(eventp, event, sizeof(*event)); 507 if (!ret) 508 vhost_add_used_and_signal(&vs->dev, vq, head, 0); 509 else 510 vq_err(vq, "Faulted on vhost_scsi_send_event\n"); 511 } 512 513 static void vhost_scsi_evt_work(struct vhost_work *work) 514 { 515 struct vhost_scsi *vs = container_of(work, struct vhost_scsi, 516 vs_event_work); 517 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 518 struct vhost_scsi_evt *evt, *t; 519 struct llist_node *llnode; 520 521 mutex_lock(&vq->mutex); 522 llnode = llist_del_all(&vs->vs_event_list); 523 llist_for_each_entry_safe(evt, t, llnode, list) { 524 vhost_scsi_do_evt_work(vs, evt); 525 vhost_scsi_free_evt(vs, evt); 526 } 527 mutex_unlock(&vq->mutex); 528 } 529 530 /* Fill in status and signal that we are done processing this command 531 * 532 * This is scheduled in the vhost work queue so we are called with the owner 533 * process mm and can access the vring. 534 */ 535 static void vhost_scsi_complete_cmd_work(struct vhost_work *work) 536 { 537 struct vhost_scsi *vs = container_of(work, struct vhost_scsi, 538 vs_completion_work); 539 struct virtio_scsi_cmd_resp v_rsp; 540 struct vhost_scsi_cmd *cmd, *t; 541 struct llist_node *llnode; 542 struct se_cmd *se_cmd; 543 struct iov_iter iov_iter; 544 int ret, vq; 545 546 bitmap_zero(vs->compl_bitmap, vs->dev.nvqs); 547 llnode = llist_del_all(&vs->vs_completion_list); 548 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) { 549 se_cmd = &cmd->tvc_se_cmd; 550 551 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__, 552 cmd, se_cmd->residual_count, se_cmd->scsi_status); 553 554 memset(&v_rsp, 0, sizeof(v_rsp)); 555 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count); 556 /* TODO is status_qualifier field needed? */ 557 v_rsp.status = se_cmd->scsi_status; 558 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq, 559 se_cmd->scsi_sense_length); 560 memcpy(v_rsp.sense, cmd->tvc_sense_buf, 561 se_cmd->scsi_sense_length); 562 563 iov_iter_init(&iov_iter, ITER_DEST, cmd->tvc_resp_iov, 564 cmd->tvc_in_iovs, sizeof(v_rsp)); 565 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter); 566 if (likely(ret == sizeof(v_rsp))) { 567 struct vhost_scsi_virtqueue *q; 568 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0); 569 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq); 570 vq = q - vs->vqs; 571 __set_bit(vq, vs->compl_bitmap); 572 } else 573 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 574 575 vhost_scsi_release_cmd_res(se_cmd); 576 } 577 578 vq = -1; 579 while ((vq = find_next_bit(vs->compl_bitmap, vs->dev.nvqs, vq + 1)) 580 < vs->dev.nvqs) 581 vhost_signal(&vs->dev, &vs->vqs[vq].vq); 582 } 583 584 static struct vhost_scsi_cmd * 585 vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, 586 unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr, 587 u32 exp_data_len, int data_direction) 588 { 589 struct vhost_scsi_virtqueue *svq = container_of(vq, 590 struct vhost_scsi_virtqueue, vq); 591 struct vhost_scsi_cmd *cmd; 592 struct vhost_scsi_nexus *tv_nexus; 593 struct scatterlist *sg, *prot_sg; 594 struct iovec *tvc_resp_iov; 595 struct page **pages; 596 int tag; 597 598 tv_nexus = tpg->tpg_nexus; 599 if (!tv_nexus) { 600 pr_err("Unable to locate active struct vhost_scsi_nexus\n"); 601 return ERR_PTR(-EIO); 602 } 603 604 tag = sbitmap_get(&svq->scsi_tags); 605 if (tag < 0) { 606 pr_err("Unable to obtain tag for vhost_scsi_cmd\n"); 607 return ERR_PTR(-ENOMEM); 608 } 609 610 cmd = &svq->scsi_cmds[tag]; 611 sg = cmd->tvc_sgl; 612 prot_sg = cmd->tvc_prot_sgl; 613 pages = cmd->tvc_upages; 614 tvc_resp_iov = cmd->tvc_resp_iov; 615 memset(cmd, 0, sizeof(*cmd)); 616 cmd->tvc_sgl = sg; 617 cmd->tvc_prot_sgl = prot_sg; 618 cmd->tvc_upages = pages; 619 cmd->tvc_se_cmd.map_tag = tag; 620 cmd->tvc_tag = scsi_tag; 621 cmd->tvc_lun = lun; 622 cmd->tvc_task_attr = task_attr; 623 cmd->tvc_exp_data_len = exp_data_len; 624 cmd->tvc_data_direction = data_direction; 625 cmd->tvc_nexus = tv_nexus; 626 cmd->inflight = vhost_scsi_get_inflight(vq); 627 cmd->tvc_resp_iov = tvc_resp_iov; 628 629 memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE); 630 631 return cmd; 632 } 633 634 /* 635 * Map a user memory range into a scatterlist 636 * 637 * Returns the number of scatterlist entries used or -errno on error. 638 */ 639 static int 640 vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd, 641 struct iov_iter *iter, 642 struct scatterlist *sgl, 643 bool write) 644 { 645 struct page **pages = cmd->tvc_upages; 646 struct scatterlist *sg = sgl; 647 ssize_t bytes; 648 size_t offset; 649 unsigned int npages = 0; 650 651 bytes = iov_iter_get_pages2(iter, pages, LONG_MAX, 652 VHOST_SCSI_PREALLOC_UPAGES, &offset); 653 /* No pages were pinned */ 654 if (bytes <= 0) 655 return bytes < 0 ? bytes : -EFAULT; 656 657 while (bytes) { 658 unsigned n = min_t(unsigned, PAGE_SIZE - offset, bytes); 659 sg_set_page(sg++, pages[npages++], n, offset); 660 bytes -= n; 661 offset = 0; 662 } 663 return npages; 664 } 665 666 static int 667 vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls) 668 { 669 int sgl_count = 0; 670 671 if (!iter || !iter->iov) { 672 pr_err("%s: iter->iov is NULL, but expected bytes: %zu" 673 " present\n", __func__, bytes); 674 return -EINVAL; 675 } 676 677 sgl_count = iov_iter_npages(iter, 0xffff); 678 if (sgl_count > max_sgls) { 679 pr_err("%s: requested sgl_count: %d exceeds pre-allocated" 680 " max_sgls: %d\n", __func__, sgl_count, max_sgls); 681 return -EINVAL; 682 } 683 return sgl_count; 684 } 685 686 static int 687 vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd *cmd, bool write, 688 struct iov_iter *iter, 689 struct scatterlist *sg, int sg_count) 690 { 691 struct scatterlist *p = sg; 692 int ret; 693 694 while (iov_iter_count(iter)) { 695 ret = vhost_scsi_map_to_sgl(cmd, iter, sg, write); 696 if (ret < 0) { 697 while (p < sg) { 698 struct page *page = sg_page(p++); 699 if (page) 700 put_page(page); 701 } 702 return ret; 703 } 704 sg += ret; 705 } 706 return 0; 707 } 708 709 static int 710 vhost_scsi_mapal(struct vhost_scsi_cmd *cmd, 711 size_t prot_bytes, struct iov_iter *prot_iter, 712 size_t data_bytes, struct iov_iter *data_iter) 713 { 714 int sgl_count, ret; 715 bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE); 716 717 if (prot_bytes) { 718 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes, 719 VHOST_SCSI_PREALLOC_PROT_SGLS); 720 if (sgl_count < 0) 721 return sgl_count; 722 723 sg_init_table(cmd->tvc_prot_sgl, sgl_count); 724 cmd->tvc_prot_sgl_count = sgl_count; 725 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__, 726 cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count); 727 728 ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter, 729 cmd->tvc_prot_sgl, 730 cmd->tvc_prot_sgl_count); 731 if (ret < 0) { 732 cmd->tvc_prot_sgl_count = 0; 733 return ret; 734 } 735 } 736 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes, 737 VHOST_SCSI_PREALLOC_SGLS); 738 if (sgl_count < 0) 739 return sgl_count; 740 741 sg_init_table(cmd->tvc_sgl, sgl_count); 742 cmd->tvc_sgl_count = sgl_count; 743 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__, 744 cmd->tvc_sgl, cmd->tvc_sgl_count); 745 746 ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter, 747 cmd->tvc_sgl, cmd->tvc_sgl_count); 748 if (ret < 0) { 749 cmd->tvc_sgl_count = 0; 750 return ret; 751 } 752 return 0; 753 } 754 755 static int vhost_scsi_to_tcm_attr(int attr) 756 { 757 switch (attr) { 758 case VIRTIO_SCSI_S_SIMPLE: 759 return TCM_SIMPLE_TAG; 760 case VIRTIO_SCSI_S_ORDERED: 761 return TCM_ORDERED_TAG; 762 case VIRTIO_SCSI_S_HEAD: 763 return TCM_HEAD_TAG; 764 case VIRTIO_SCSI_S_ACA: 765 return TCM_ACA_TAG; 766 default: 767 break; 768 } 769 return TCM_SIMPLE_TAG; 770 } 771 772 static void vhost_scsi_target_queue_cmd(struct vhost_scsi_cmd *cmd) 773 { 774 struct se_cmd *se_cmd = &cmd->tvc_se_cmd; 775 struct vhost_scsi_nexus *tv_nexus; 776 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL; 777 778 /* FIXME: BIDI operation */ 779 if (cmd->tvc_sgl_count) { 780 sg_ptr = cmd->tvc_sgl; 781 782 if (cmd->tvc_prot_sgl_count) 783 sg_prot_ptr = cmd->tvc_prot_sgl; 784 else 785 se_cmd->prot_pto = true; 786 } else { 787 sg_ptr = NULL; 788 } 789 tv_nexus = cmd->tvc_nexus; 790 791 se_cmd->tag = 0; 792 target_init_cmd(se_cmd, tv_nexus->tvn_se_sess, &cmd->tvc_sense_buf[0], 793 cmd->tvc_lun, cmd->tvc_exp_data_len, 794 vhost_scsi_to_tcm_attr(cmd->tvc_task_attr), 795 cmd->tvc_data_direction, TARGET_SCF_ACK_KREF); 796 797 if (target_submit_prep(se_cmd, cmd->tvc_cdb, sg_ptr, 798 cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr, 799 cmd->tvc_prot_sgl_count, GFP_KERNEL)) 800 return; 801 802 target_queue_submission(se_cmd); 803 } 804 805 static void 806 vhost_scsi_send_bad_target(struct vhost_scsi *vs, 807 struct vhost_virtqueue *vq, 808 int head, unsigned out) 809 { 810 struct virtio_scsi_cmd_resp __user *resp; 811 struct virtio_scsi_cmd_resp rsp; 812 int ret; 813 814 memset(&rsp, 0, sizeof(rsp)); 815 rsp.response = VIRTIO_SCSI_S_BAD_TARGET; 816 resp = vq->iov[out].iov_base; 817 ret = __copy_to_user(resp, &rsp, sizeof(rsp)); 818 if (!ret) 819 vhost_add_used_and_signal(&vs->dev, vq, head, 0); 820 else 821 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 822 } 823 824 static int 825 vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 826 struct vhost_scsi_ctx *vc) 827 { 828 int ret = -ENXIO; 829 830 vc->head = vhost_get_vq_desc(vq, vq->iov, 831 ARRAY_SIZE(vq->iov), &vc->out, &vc->in, 832 NULL, NULL); 833 834 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", 835 vc->head, vc->out, vc->in); 836 837 /* On error, stop handling until the next kick. */ 838 if (unlikely(vc->head < 0)) 839 goto done; 840 841 /* Nothing new? Wait for eventfd to tell us they refilled. */ 842 if (vc->head == vq->num) { 843 if (unlikely(vhost_enable_notify(&vs->dev, vq))) { 844 vhost_disable_notify(&vs->dev, vq); 845 ret = -EAGAIN; 846 } 847 goto done; 848 } 849 850 /* 851 * Get the size of request and response buffers. 852 * FIXME: Not correct for BIDI operation 853 */ 854 vc->out_size = iov_length(vq->iov, vc->out); 855 vc->in_size = iov_length(&vq->iov[vc->out], vc->in); 856 857 /* 858 * Copy over the virtio-scsi request header, which for a 859 * ANY_LAYOUT enabled guest may span multiple iovecs, or a 860 * single iovec may contain both the header + outgoing 861 * WRITE payloads. 862 * 863 * copy_from_iter() will advance out_iter, so that it will 864 * point at the start of the outgoing WRITE payload, if 865 * DMA_TO_DEVICE is set. 866 */ 867 iov_iter_init(&vc->out_iter, ITER_SOURCE, vq->iov, vc->out, vc->out_size); 868 ret = 0; 869 870 done: 871 return ret; 872 } 873 874 static int 875 vhost_scsi_chk_size(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc) 876 { 877 if (unlikely(vc->in_size < vc->rsp_size)) { 878 vq_err(vq, 879 "Response buf too small, need min %zu bytes got %zu", 880 vc->rsp_size, vc->in_size); 881 return -EINVAL; 882 } else if (unlikely(vc->out_size < vc->req_size)) { 883 vq_err(vq, 884 "Request buf too small, need min %zu bytes got %zu", 885 vc->req_size, vc->out_size); 886 return -EIO; 887 } 888 889 return 0; 890 } 891 892 static int 893 vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc, 894 struct vhost_scsi_tpg **tpgp) 895 { 896 int ret = -EIO; 897 898 if (unlikely(!copy_from_iter_full(vc->req, vc->req_size, 899 &vc->out_iter))) { 900 vq_err(vq, "Faulted on copy_from_iter_full\n"); 901 } else if (unlikely(*vc->lunp != 1)) { 902 /* virtio-scsi spec requires byte 0 of the lun to be 1 */ 903 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *vc->lunp); 904 } else { 905 struct vhost_scsi_tpg **vs_tpg, *tpg; 906 907 vs_tpg = vhost_vq_get_backend(vq); /* validated at handler entry */ 908 909 tpg = READ_ONCE(vs_tpg[*vc->target]); 910 if (unlikely(!tpg)) { 911 vq_err(vq, "Target 0x%x does not exist\n", *vc->target); 912 } else { 913 if (tpgp) 914 *tpgp = tpg; 915 ret = 0; 916 } 917 } 918 919 return ret; 920 } 921 922 static u16 vhost_buf_to_lun(u8 *lun_buf) 923 { 924 return ((lun_buf[2] << 8) | lun_buf[3]) & 0x3FFF; 925 } 926 927 static void 928 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) 929 { 930 struct vhost_scsi_tpg **vs_tpg, *tpg; 931 struct virtio_scsi_cmd_req v_req; 932 struct virtio_scsi_cmd_req_pi v_req_pi; 933 struct vhost_scsi_ctx vc; 934 struct vhost_scsi_cmd *cmd; 935 struct iov_iter in_iter, prot_iter, data_iter; 936 u64 tag; 937 u32 exp_data_len, data_direction; 938 int ret, prot_bytes, i, c = 0; 939 u16 lun; 940 u8 task_attr; 941 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI); 942 void *cdb; 943 944 mutex_lock(&vq->mutex); 945 /* 946 * We can handle the vq only after the endpoint is setup by calling the 947 * VHOST_SCSI_SET_ENDPOINT ioctl. 948 */ 949 vs_tpg = vhost_vq_get_backend(vq); 950 if (!vs_tpg) 951 goto out; 952 953 memset(&vc, 0, sizeof(vc)); 954 vc.rsp_size = sizeof(struct virtio_scsi_cmd_resp); 955 956 vhost_disable_notify(&vs->dev, vq); 957 958 do { 959 ret = vhost_scsi_get_desc(vs, vq, &vc); 960 if (ret) 961 goto err; 962 963 /* 964 * Setup pointers and values based upon different virtio-scsi 965 * request header if T10_PI is enabled in KVM guest. 966 */ 967 if (t10_pi) { 968 vc.req = &v_req_pi; 969 vc.req_size = sizeof(v_req_pi); 970 vc.lunp = &v_req_pi.lun[0]; 971 vc.target = &v_req_pi.lun[1]; 972 } else { 973 vc.req = &v_req; 974 vc.req_size = sizeof(v_req); 975 vc.lunp = &v_req.lun[0]; 976 vc.target = &v_req.lun[1]; 977 } 978 979 /* 980 * Validate the size of request and response buffers. 981 * Check for a sane response buffer so we can report 982 * early errors back to the guest. 983 */ 984 ret = vhost_scsi_chk_size(vq, &vc); 985 if (ret) 986 goto err; 987 988 ret = vhost_scsi_get_req(vq, &vc, &tpg); 989 if (ret) 990 goto err; 991 992 ret = -EIO; /* bad target on any error from here on */ 993 994 /* 995 * Determine data_direction by calculating the total outgoing 996 * iovec sizes + incoming iovec sizes vs. virtio-scsi request + 997 * response headers respectively. 998 * 999 * For DMA_TO_DEVICE this is out_iter, which is already pointing 1000 * to the right place. 1001 * 1002 * For DMA_FROM_DEVICE, the iovec will be just past the end 1003 * of the virtio-scsi response header in either the same 1004 * or immediately following iovec. 1005 * 1006 * Any associated T10_PI bytes for the outgoing / incoming 1007 * payloads are included in calculation of exp_data_len here. 1008 */ 1009 prot_bytes = 0; 1010 1011 if (vc.out_size > vc.req_size) { 1012 data_direction = DMA_TO_DEVICE; 1013 exp_data_len = vc.out_size - vc.req_size; 1014 data_iter = vc.out_iter; 1015 } else if (vc.in_size > vc.rsp_size) { 1016 data_direction = DMA_FROM_DEVICE; 1017 exp_data_len = vc.in_size - vc.rsp_size; 1018 1019 iov_iter_init(&in_iter, ITER_DEST, &vq->iov[vc.out], vc.in, 1020 vc.rsp_size + exp_data_len); 1021 iov_iter_advance(&in_iter, vc.rsp_size); 1022 data_iter = in_iter; 1023 } else { 1024 data_direction = DMA_NONE; 1025 exp_data_len = 0; 1026 } 1027 /* 1028 * If T10_PI header + payload is present, setup prot_iter values 1029 * and recalculate data_iter for vhost_scsi_mapal() mapping to 1030 * host scatterlists via get_user_pages_fast(). 1031 */ 1032 if (t10_pi) { 1033 if (v_req_pi.pi_bytesout) { 1034 if (data_direction != DMA_TO_DEVICE) { 1035 vq_err(vq, "Received non zero pi_bytesout," 1036 " but wrong data_direction\n"); 1037 goto err; 1038 } 1039 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout); 1040 } else if (v_req_pi.pi_bytesin) { 1041 if (data_direction != DMA_FROM_DEVICE) { 1042 vq_err(vq, "Received non zero pi_bytesin," 1043 " but wrong data_direction\n"); 1044 goto err; 1045 } 1046 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin); 1047 } 1048 /* 1049 * Set prot_iter to data_iter and truncate it to 1050 * prot_bytes, and advance data_iter past any 1051 * preceeding prot_bytes that may be present. 1052 * 1053 * Also fix up the exp_data_len to reflect only the 1054 * actual data payload length. 1055 */ 1056 if (prot_bytes) { 1057 exp_data_len -= prot_bytes; 1058 prot_iter = data_iter; 1059 iov_iter_truncate(&prot_iter, prot_bytes); 1060 iov_iter_advance(&data_iter, prot_bytes); 1061 } 1062 tag = vhost64_to_cpu(vq, v_req_pi.tag); 1063 task_attr = v_req_pi.task_attr; 1064 cdb = &v_req_pi.cdb[0]; 1065 lun = vhost_buf_to_lun(v_req_pi.lun); 1066 } else { 1067 tag = vhost64_to_cpu(vq, v_req.tag); 1068 task_attr = v_req.task_attr; 1069 cdb = &v_req.cdb[0]; 1070 lun = vhost_buf_to_lun(v_req.lun); 1071 } 1072 /* 1073 * Check that the received CDB size does not exceeded our 1074 * hardcoded max for vhost-scsi, then get a pre-allocated 1075 * cmd descriptor for the new virtio-scsi tag. 1076 * 1077 * TODO what if cdb was too small for varlen cdb header? 1078 */ 1079 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) { 1080 vq_err(vq, "Received SCSI CDB with command_size: %d that" 1081 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", 1082 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE); 1083 goto err; 1084 } 1085 cmd = vhost_scsi_get_cmd(vq, tpg, cdb, tag, lun, task_attr, 1086 exp_data_len + prot_bytes, 1087 data_direction); 1088 if (IS_ERR(cmd)) { 1089 vq_err(vq, "vhost_scsi_get_cmd failed %ld\n", 1090 PTR_ERR(cmd)); 1091 goto err; 1092 } 1093 cmd->tvc_vhost = vs; 1094 cmd->tvc_vq = vq; 1095 for (i = 0; i < vc.in ; i++) 1096 cmd->tvc_resp_iov[i] = vq->iov[vc.out + i]; 1097 cmd->tvc_in_iovs = vc.in; 1098 1099 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", 1100 cmd->tvc_cdb[0], cmd->tvc_lun); 1101 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:" 1102 " %d\n", cmd, exp_data_len, prot_bytes, data_direction); 1103 1104 if (data_direction != DMA_NONE) { 1105 if (unlikely(vhost_scsi_mapal(cmd, prot_bytes, 1106 &prot_iter, exp_data_len, 1107 &data_iter))) { 1108 vq_err(vq, "Failed to map iov to sgl\n"); 1109 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd); 1110 goto err; 1111 } 1112 } 1113 /* 1114 * Save the descriptor from vhost_get_vq_desc() to be used to 1115 * complete the virtio-scsi request in TCM callback context via 1116 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status() 1117 */ 1118 cmd->tvc_vq_desc = vc.head; 1119 vhost_scsi_target_queue_cmd(cmd); 1120 ret = 0; 1121 err: 1122 /* 1123 * ENXIO: No more requests, or read error, wait for next kick 1124 * EINVAL: Invalid response buffer, drop the request 1125 * EIO: Respond with bad target 1126 * EAGAIN: Pending request 1127 */ 1128 if (ret == -ENXIO) 1129 break; 1130 else if (ret == -EIO) 1131 vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out); 1132 } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); 1133 out: 1134 mutex_unlock(&vq->mutex); 1135 } 1136 1137 static void 1138 vhost_scsi_send_tmf_resp(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 1139 int in_iovs, int vq_desc, struct iovec *resp_iov, 1140 int tmf_resp_code) 1141 { 1142 struct virtio_scsi_ctrl_tmf_resp rsp; 1143 struct iov_iter iov_iter; 1144 int ret; 1145 1146 pr_debug("%s\n", __func__); 1147 memset(&rsp, 0, sizeof(rsp)); 1148 rsp.response = tmf_resp_code; 1149 1150 iov_iter_init(&iov_iter, ITER_DEST, resp_iov, in_iovs, sizeof(rsp)); 1151 1152 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter); 1153 if (likely(ret == sizeof(rsp))) 1154 vhost_add_used_and_signal(&vs->dev, vq, vq_desc, 0); 1155 else 1156 pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n"); 1157 } 1158 1159 static void vhost_scsi_tmf_resp_work(struct vhost_work *work) 1160 { 1161 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf, 1162 vwork); 1163 int resp_code; 1164 1165 if (tmf->scsi_resp == TMR_FUNCTION_COMPLETE) 1166 resp_code = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 1167 else 1168 resp_code = VIRTIO_SCSI_S_FUNCTION_REJECTED; 1169 1170 vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs, 1171 tmf->vq_desc, &tmf->resp_iov, resp_code); 1172 vhost_scsi_release_tmf_res(tmf); 1173 } 1174 1175 static void 1176 vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg, 1177 struct vhost_virtqueue *vq, 1178 struct virtio_scsi_ctrl_tmf_req *vtmf, 1179 struct vhost_scsi_ctx *vc) 1180 { 1181 struct vhost_scsi_virtqueue *svq = container_of(vq, 1182 struct vhost_scsi_virtqueue, vq); 1183 struct vhost_scsi_tmf *tmf; 1184 1185 if (vhost32_to_cpu(vq, vtmf->subtype) != 1186 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET) 1187 goto send_reject; 1188 1189 if (!tpg->tpg_nexus || !tpg->tpg_nexus->tvn_se_sess) { 1190 pr_err("Unable to locate active struct vhost_scsi_nexus for LUN RESET.\n"); 1191 goto send_reject; 1192 } 1193 1194 tmf = kzalloc(sizeof(*tmf), GFP_KERNEL); 1195 if (!tmf) 1196 goto send_reject; 1197 1198 vhost_work_init(&tmf->vwork, vhost_scsi_tmf_resp_work); 1199 tmf->vhost = vs; 1200 tmf->svq = svq; 1201 tmf->resp_iov = vq->iov[vc->out]; 1202 tmf->vq_desc = vc->head; 1203 tmf->in_iovs = vc->in; 1204 tmf->inflight = vhost_scsi_get_inflight(vq); 1205 1206 if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL, 1207 vhost_buf_to_lun(vtmf->lun), NULL, 1208 TMR_LUN_RESET, GFP_KERNEL, 0, 1209 TARGET_SCF_ACK_KREF) < 0) { 1210 vhost_scsi_release_tmf_res(tmf); 1211 goto send_reject; 1212 } 1213 1214 return; 1215 1216 send_reject: 1217 vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out], 1218 VIRTIO_SCSI_S_FUNCTION_REJECTED); 1219 } 1220 1221 static void 1222 vhost_scsi_send_an_resp(struct vhost_scsi *vs, 1223 struct vhost_virtqueue *vq, 1224 struct vhost_scsi_ctx *vc) 1225 { 1226 struct virtio_scsi_ctrl_an_resp rsp; 1227 struct iov_iter iov_iter; 1228 int ret; 1229 1230 pr_debug("%s\n", __func__); 1231 memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */ 1232 rsp.response = VIRTIO_SCSI_S_OK; 1233 1234 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, sizeof(rsp)); 1235 1236 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter); 1237 if (likely(ret == sizeof(rsp))) 1238 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0); 1239 else 1240 pr_err("Faulted on virtio_scsi_ctrl_an_resp\n"); 1241 } 1242 1243 static void 1244 vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) 1245 { 1246 struct vhost_scsi_tpg *tpg; 1247 union { 1248 __virtio32 type; 1249 struct virtio_scsi_ctrl_an_req an; 1250 struct virtio_scsi_ctrl_tmf_req tmf; 1251 } v_req; 1252 struct vhost_scsi_ctx vc; 1253 size_t typ_size; 1254 int ret, c = 0; 1255 1256 mutex_lock(&vq->mutex); 1257 /* 1258 * We can handle the vq only after the endpoint is setup by calling the 1259 * VHOST_SCSI_SET_ENDPOINT ioctl. 1260 */ 1261 if (!vhost_vq_get_backend(vq)) 1262 goto out; 1263 1264 memset(&vc, 0, sizeof(vc)); 1265 1266 vhost_disable_notify(&vs->dev, vq); 1267 1268 do { 1269 ret = vhost_scsi_get_desc(vs, vq, &vc); 1270 if (ret) 1271 goto err; 1272 1273 /* 1274 * Get the request type first in order to setup 1275 * other parameters dependent on the type. 1276 */ 1277 vc.req = &v_req.type; 1278 typ_size = sizeof(v_req.type); 1279 1280 if (unlikely(!copy_from_iter_full(vc.req, typ_size, 1281 &vc.out_iter))) { 1282 vq_err(vq, "Faulted on copy_from_iter tmf type\n"); 1283 /* 1284 * The size of the response buffer depends on the 1285 * request type and must be validated against it. 1286 * Since the request type is not known, don't send 1287 * a response. 1288 */ 1289 continue; 1290 } 1291 1292 switch (vhost32_to_cpu(vq, v_req.type)) { 1293 case VIRTIO_SCSI_T_TMF: 1294 vc.req = &v_req.tmf; 1295 vc.req_size = sizeof(struct virtio_scsi_ctrl_tmf_req); 1296 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp); 1297 vc.lunp = &v_req.tmf.lun[0]; 1298 vc.target = &v_req.tmf.lun[1]; 1299 break; 1300 case VIRTIO_SCSI_T_AN_QUERY: 1301 case VIRTIO_SCSI_T_AN_SUBSCRIBE: 1302 vc.req = &v_req.an; 1303 vc.req_size = sizeof(struct virtio_scsi_ctrl_an_req); 1304 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp); 1305 vc.lunp = &v_req.an.lun[0]; 1306 vc.target = NULL; 1307 break; 1308 default: 1309 vq_err(vq, "Unknown control request %d", v_req.type); 1310 continue; 1311 } 1312 1313 /* 1314 * Validate the size of request and response buffers. 1315 * Check for a sane response buffer so we can report 1316 * early errors back to the guest. 1317 */ 1318 ret = vhost_scsi_chk_size(vq, &vc); 1319 if (ret) 1320 goto err; 1321 1322 /* 1323 * Get the rest of the request now that its size is known. 1324 */ 1325 vc.req += typ_size; 1326 vc.req_size -= typ_size; 1327 1328 ret = vhost_scsi_get_req(vq, &vc, &tpg); 1329 if (ret) 1330 goto err; 1331 1332 if (v_req.type == VIRTIO_SCSI_T_TMF) 1333 vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc); 1334 else 1335 vhost_scsi_send_an_resp(vs, vq, &vc); 1336 err: 1337 /* 1338 * ENXIO: No more requests, or read error, wait for next kick 1339 * EINVAL: Invalid response buffer, drop the request 1340 * EIO: Respond with bad target 1341 * EAGAIN: Pending request 1342 */ 1343 if (ret == -ENXIO) 1344 break; 1345 else if (ret == -EIO) 1346 vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out); 1347 } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); 1348 out: 1349 mutex_unlock(&vq->mutex); 1350 } 1351 1352 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work) 1353 { 1354 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1355 poll.work); 1356 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); 1357 1358 pr_debug("%s: The handling func for control queue.\n", __func__); 1359 vhost_scsi_ctl_handle_vq(vs, vq); 1360 } 1361 1362 static void 1363 vhost_scsi_send_evt(struct vhost_scsi *vs, 1364 struct vhost_scsi_tpg *tpg, 1365 struct se_lun *lun, 1366 u32 event, 1367 u32 reason) 1368 { 1369 struct vhost_scsi_evt *evt; 1370 1371 evt = vhost_scsi_allocate_evt(vs, event, reason); 1372 if (!evt) 1373 return; 1374 1375 if (tpg && lun) { 1376 /* TODO: share lun setup code with virtio-scsi.ko */ 1377 /* 1378 * Note: evt->event is zeroed when we allocate it and 1379 * lun[4-7] need to be zero according to virtio-scsi spec. 1380 */ 1381 evt->event.lun[0] = 0x01; 1382 evt->event.lun[1] = tpg->tport_tpgt; 1383 if (lun->unpacked_lun >= 256) 1384 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ; 1385 evt->event.lun[3] = lun->unpacked_lun & 0xFF; 1386 } 1387 1388 llist_add(&evt->list, &vs->vs_event_list); 1389 vhost_work_queue(&vs->dev, &vs->vs_event_work); 1390 } 1391 1392 static void vhost_scsi_evt_handle_kick(struct vhost_work *work) 1393 { 1394 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1395 poll.work); 1396 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); 1397 1398 mutex_lock(&vq->mutex); 1399 if (!vhost_vq_get_backend(vq)) 1400 goto out; 1401 1402 if (vs->vs_events_missed) 1403 vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); 1404 out: 1405 mutex_unlock(&vq->mutex); 1406 } 1407 1408 static void vhost_scsi_handle_kick(struct vhost_work *work) 1409 { 1410 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, 1411 poll.work); 1412 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); 1413 1414 vhost_scsi_handle_vq(vs, vq); 1415 } 1416 1417 /* Callers must hold dev mutex */ 1418 static void vhost_scsi_flush(struct vhost_scsi *vs) 1419 { 1420 int i; 1421 1422 /* Init new inflight and remember the old inflight */ 1423 vhost_scsi_init_inflight(vs, vs->old_inflight); 1424 1425 /* 1426 * The inflight->kref was initialized to 1. We decrement it here to 1427 * indicate the start of the flush operation so that it will reach 0 1428 * when all the reqs are finished. 1429 */ 1430 for (i = 0; i < vs->dev.nvqs; i++) 1431 kref_put(&vs->old_inflight[i]->kref, vhost_scsi_done_inflight); 1432 1433 /* Flush both the vhost poll and vhost work */ 1434 vhost_dev_flush(&vs->dev); 1435 1436 /* Wait for all reqs issued before the flush to be finished */ 1437 for (i = 0; i < vs->dev.nvqs; i++) 1438 wait_for_completion(&vs->old_inflight[i]->comp); 1439 } 1440 1441 static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq) 1442 { 1443 struct vhost_scsi_virtqueue *svq = container_of(vq, 1444 struct vhost_scsi_virtqueue, vq); 1445 struct vhost_scsi_cmd *tv_cmd; 1446 unsigned int i; 1447 1448 if (!svq->scsi_cmds) 1449 return; 1450 1451 for (i = 0; i < svq->max_cmds; i++) { 1452 tv_cmd = &svq->scsi_cmds[i]; 1453 1454 kfree(tv_cmd->tvc_sgl); 1455 kfree(tv_cmd->tvc_prot_sgl); 1456 kfree(tv_cmd->tvc_upages); 1457 kfree(tv_cmd->tvc_resp_iov); 1458 } 1459 1460 sbitmap_free(&svq->scsi_tags); 1461 kfree(svq->scsi_cmds); 1462 svq->scsi_cmds = NULL; 1463 } 1464 1465 static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds) 1466 { 1467 struct vhost_scsi_virtqueue *svq = container_of(vq, 1468 struct vhost_scsi_virtqueue, vq); 1469 struct vhost_scsi_cmd *tv_cmd; 1470 unsigned int i; 1471 1472 if (svq->scsi_cmds) 1473 return 0; 1474 1475 if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL, 1476 NUMA_NO_NODE, false, true)) 1477 return -ENOMEM; 1478 svq->max_cmds = max_cmds; 1479 1480 svq->scsi_cmds = kcalloc(max_cmds, sizeof(*tv_cmd), GFP_KERNEL); 1481 if (!svq->scsi_cmds) { 1482 sbitmap_free(&svq->scsi_tags); 1483 return -ENOMEM; 1484 } 1485 1486 for (i = 0; i < max_cmds; i++) { 1487 tv_cmd = &svq->scsi_cmds[i]; 1488 1489 tv_cmd->tvc_sgl = kcalloc(VHOST_SCSI_PREALLOC_SGLS, 1490 sizeof(struct scatterlist), 1491 GFP_KERNEL); 1492 if (!tv_cmd->tvc_sgl) { 1493 pr_err("Unable to allocate tv_cmd->tvc_sgl\n"); 1494 goto out; 1495 } 1496 1497 tv_cmd->tvc_upages = kcalloc(VHOST_SCSI_PREALLOC_UPAGES, 1498 sizeof(struct page *), 1499 GFP_KERNEL); 1500 if (!tv_cmd->tvc_upages) { 1501 pr_err("Unable to allocate tv_cmd->tvc_upages\n"); 1502 goto out; 1503 } 1504 1505 tv_cmd->tvc_resp_iov = kcalloc(UIO_MAXIOV, 1506 sizeof(struct iovec), 1507 GFP_KERNEL); 1508 if (!tv_cmd->tvc_resp_iov) { 1509 pr_err("Unable to allocate tv_cmd->tvc_resp_iov\n"); 1510 goto out; 1511 } 1512 1513 tv_cmd->tvc_prot_sgl = kcalloc(VHOST_SCSI_PREALLOC_PROT_SGLS, 1514 sizeof(struct scatterlist), 1515 GFP_KERNEL); 1516 if (!tv_cmd->tvc_prot_sgl) { 1517 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n"); 1518 goto out; 1519 } 1520 } 1521 return 0; 1522 out: 1523 vhost_scsi_destroy_vq_cmds(vq); 1524 return -ENOMEM; 1525 } 1526 1527 /* 1528 * Called from vhost_scsi_ioctl() context to walk the list of available 1529 * vhost_scsi_tpg with an active struct vhost_scsi_nexus 1530 * 1531 * The lock nesting rule is: 1532 * vs->dev.mutex -> vhost_scsi_mutex -> tpg->tv_tpg_mutex -> vq->mutex 1533 */ 1534 static int 1535 vhost_scsi_set_endpoint(struct vhost_scsi *vs, 1536 struct vhost_scsi_target *t) 1537 { 1538 struct se_portal_group *se_tpg; 1539 struct vhost_scsi_tport *tv_tport; 1540 struct vhost_scsi_tpg *tpg; 1541 struct vhost_scsi_tpg **vs_tpg; 1542 struct vhost_virtqueue *vq; 1543 int index, ret, i, len; 1544 bool match = false; 1545 1546 mutex_lock(&vs->dev.mutex); 1547 1548 /* Verify that ring has been setup correctly. */ 1549 for (index = 0; index < vs->dev.nvqs; ++index) { 1550 /* Verify that ring has been setup correctly. */ 1551 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { 1552 ret = -EFAULT; 1553 goto out; 1554 } 1555 } 1556 1557 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET; 1558 vs_tpg = kzalloc(len, GFP_KERNEL); 1559 if (!vs_tpg) { 1560 ret = -ENOMEM; 1561 goto out; 1562 } 1563 if (vs->vs_tpg) 1564 memcpy(vs_tpg, vs->vs_tpg, len); 1565 1566 mutex_lock(&vhost_scsi_mutex); 1567 list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) { 1568 mutex_lock(&tpg->tv_tpg_mutex); 1569 if (!tpg->tpg_nexus) { 1570 mutex_unlock(&tpg->tv_tpg_mutex); 1571 continue; 1572 } 1573 if (tpg->tv_tpg_vhost_count != 0) { 1574 mutex_unlock(&tpg->tv_tpg_mutex); 1575 continue; 1576 } 1577 tv_tport = tpg->tport; 1578 1579 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) { 1580 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) { 1581 mutex_unlock(&tpg->tv_tpg_mutex); 1582 mutex_unlock(&vhost_scsi_mutex); 1583 ret = -EEXIST; 1584 goto undepend; 1585 } 1586 /* 1587 * In order to ensure individual vhost-scsi configfs 1588 * groups cannot be removed while in use by vhost ioctl, 1589 * go ahead and take an explicit se_tpg->tpg_group.cg_item 1590 * dependency now. 1591 */ 1592 se_tpg = &tpg->se_tpg; 1593 ret = target_depend_item(&se_tpg->tpg_group.cg_item); 1594 if (ret) { 1595 pr_warn("target_depend_item() failed: %d\n", ret); 1596 mutex_unlock(&tpg->tv_tpg_mutex); 1597 mutex_unlock(&vhost_scsi_mutex); 1598 goto undepend; 1599 } 1600 tpg->tv_tpg_vhost_count++; 1601 tpg->vhost_scsi = vs; 1602 vs_tpg[tpg->tport_tpgt] = tpg; 1603 match = true; 1604 } 1605 mutex_unlock(&tpg->tv_tpg_mutex); 1606 } 1607 mutex_unlock(&vhost_scsi_mutex); 1608 1609 if (match) { 1610 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn, 1611 sizeof(vs->vs_vhost_wwpn)); 1612 1613 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) { 1614 vq = &vs->vqs[i].vq; 1615 if (!vhost_vq_is_setup(vq)) 1616 continue; 1617 1618 ret = vhost_scsi_setup_vq_cmds(vq, vq->num); 1619 if (ret) 1620 goto destroy_vq_cmds; 1621 } 1622 1623 for (i = 0; i < vs->dev.nvqs; i++) { 1624 vq = &vs->vqs[i].vq; 1625 mutex_lock(&vq->mutex); 1626 vhost_vq_set_backend(vq, vs_tpg); 1627 vhost_vq_init_access(vq); 1628 mutex_unlock(&vq->mutex); 1629 } 1630 ret = 0; 1631 } else { 1632 ret = -EEXIST; 1633 } 1634 1635 /* 1636 * Act as synchronize_rcu to make sure access to 1637 * old vs->vs_tpg is finished. 1638 */ 1639 vhost_scsi_flush(vs); 1640 kfree(vs->vs_tpg); 1641 vs->vs_tpg = vs_tpg; 1642 goto out; 1643 1644 destroy_vq_cmds: 1645 for (i--; i >= VHOST_SCSI_VQ_IO; i--) { 1646 if (!vhost_vq_get_backend(&vs->vqs[i].vq)) 1647 vhost_scsi_destroy_vq_cmds(&vs->vqs[i].vq); 1648 } 1649 undepend: 1650 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 1651 tpg = vs_tpg[i]; 1652 if (tpg) { 1653 mutex_lock(&tpg->tv_tpg_mutex); 1654 tpg->vhost_scsi = NULL; 1655 tpg->tv_tpg_vhost_count--; 1656 mutex_unlock(&tpg->tv_tpg_mutex); 1657 target_undepend_item(&tpg->se_tpg.tpg_group.cg_item); 1658 } 1659 } 1660 kfree(vs_tpg); 1661 out: 1662 mutex_unlock(&vs->dev.mutex); 1663 return ret; 1664 } 1665 1666 static int 1667 vhost_scsi_clear_endpoint(struct vhost_scsi *vs, 1668 struct vhost_scsi_target *t) 1669 { 1670 struct se_portal_group *se_tpg; 1671 struct vhost_scsi_tport *tv_tport; 1672 struct vhost_scsi_tpg *tpg; 1673 struct vhost_virtqueue *vq; 1674 bool match = false; 1675 int index, ret, i; 1676 u8 target; 1677 1678 mutex_lock(&vs->dev.mutex); 1679 /* Verify that ring has been setup correctly. */ 1680 for (index = 0; index < vs->dev.nvqs; ++index) { 1681 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { 1682 ret = -EFAULT; 1683 goto err_dev; 1684 } 1685 } 1686 1687 if (!vs->vs_tpg) { 1688 ret = 0; 1689 goto err_dev; 1690 } 1691 1692 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 1693 target = i; 1694 tpg = vs->vs_tpg[target]; 1695 if (!tpg) 1696 continue; 1697 1698 tv_tport = tpg->tport; 1699 if (!tv_tport) { 1700 ret = -ENODEV; 1701 goto err_dev; 1702 } 1703 1704 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) { 1705 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu" 1706 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n", 1707 tv_tport->tport_name, tpg->tport_tpgt, 1708 t->vhost_wwpn, t->vhost_tpgt); 1709 ret = -EINVAL; 1710 goto err_dev; 1711 } 1712 match = true; 1713 } 1714 if (!match) 1715 goto free_vs_tpg; 1716 1717 /* Prevent new cmds from starting and accessing the tpgs/sessions */ 1718 for (i = 0; i < vs->dev.nvqs; i++) { 1719 vq = &vs->vqs[i].vq; 1720 mutex_lock(&vq->mutex); 1721 vhost_vq_set_backend(vq, NULL); 1722 mutex_unlock(&vq->mutex); 1723 } 1724 /* Make sure cmds are not running before tearing them down. */ 1725 vhost_scsi_flush(vs); 1726 1727 for (i = 0; i < vs->dev.nvqs; i++) { 1728 vq = &vs->vqs[i].vq; 1729 vhost_scsi_destroy_vq_cmds(vq); 1730 } 1731 1732 /* 1733 * We can now release our hold on the tpg and sessions and userspace 1734 * can free them after this point. 1735 */ 1736 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 1737 target = i; 1738 tpg = vs->vs_tpg[target]; 1739 if (!tpg) 1740 continue; 1741 1742 mutex_lock(&tpg->tv_tpg_mutex); 1743 1744 tpg->tv_tpg_vhost_count--; 1745 tpg->vhost_scsi = NULL; 1746 vs->vs_tpg[target] = NULL; 1747 1748 mutex_unlock(&tpg->tv_tpg_mutex); 1749 1750 se_tpg = &tpg->se_tpg; 1751 target_undepend_item(&se_tpg->tpg_group.cg_item); 1752 } 1753 1754 free_vs_tpg: 1755 /* 1756 * Act as synchronize_rcu to make sure access to 1757 * old vs->vs_tpg is finished. 1758 */ 1759 vhost_scsi_flush(vs); 1760 kfree(vs->vs_tpg); 1761 vs->vs_tpg = NULL; 1762 WARN_ON(vs->vs_events_nr); 1763 mutex_unlock(&vs->dev.mutex); 1764 return 0; 1765 1766 err_dev: 1767 mutex_unlock(&vs->dev.mutex); 1768 return ret; 1769 } 1770 1771 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features) 1772 { 1773 struct vhost_virtqueue *vq; 1774 int i; 1775 1776 if (features & ~VHOST_SCSI_FEATURES) 1777 return -EOPNOTSUPP; 1778 1779 mutex_lock(&vs->dev.mutex); 1780 if ((features & (1 << VHOST_F_LOG_ALL)) && 1781 !vhost_log_access_ok(&vs->dev)) { 1782 mutex_unlock(&vs->dev.mutex); 1783 return -EFAULT; 1784 } 1785 1786 for (i = 0; i < vs->dev.nvqs; i++) { 1787 vq = &vs->vqs[i].vq; 1788 mutex_lock(&vq->mutex); 1789 vq->acked_features = features; 1790 mutex_unlock(&vq->mutex); 1791 } 1792 mutex_unlock(&vs->dev.mutex); 1793 return 0; 1794 } 1795 1796 static int vhost_scsi_open(struct inode *inode, struct file *f) 1797 { 1798 struct vhost_scsi *vs; 1799 struct vhost_virtqueue **vqs; 1800 int r = -ENOMEM, i, nvqs = vhost_scsi_max_io_vqs; 1801 1802 vs = kvzalloc(sizeof(*vs), GFP_KERNEL); 1803 if (!vs) 1804 goto err_vs; 1805 1806 if (nvqs > VHOST_SCSI_MAX_IO_VQ) { 1807 pr_err("Invalid max_io_vqs of %d. Using %d.\n", nvqs, 1808 VHOST_SCSI_MAX_IO_VQ); 1809 nvqs = VHOST_SCSI_MAX_IO_VQ; 1810 } else if (nvqs == 0) { 1811 pr_err("Invalid max_io_vqs of %d. Using 1.\n", nvqs); 1812 nvqs = 1; 1813 } 1814 nvqs += VHOST_SCSI_VQ_IO; 1815 1816 vs->compl_bitmap = bitmap_alloc(nvqs, GFP_KERNEL); 1817 if (!vs->compl_bitmap) 1818 goto err_compl_bitmap; 1819 1820 vs->old_inflight = kmalloc_array(nvqs, sizeof(*vs->old_inflight), 1821 GFP_KERNEL | __GFP_ZERO); 1822 if (!vs->old_inflight) 1823 goto err_inflight; 1824 1825 vs->vqs = kmalloc_array(nvqs, sizeof(*vs->vqs), 1826 GFP_KERNEL | __GFP_ZERO); 1827 if (!vs->vqs) 1828 goto err_vqs; 1829 1830 vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL); 1831 if (!vqs) 1832 goto err_local_vqs; 1833 1834 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work); 1835 vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work); 1836 1837 vs->vs_events_nr = 0; 1838 vs->vs_events_missed = false; 1839 1840 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq; 1841 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 1842 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick; 1843 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick; 1844 for (i = VHOST_SCSI_VQ_IO; i < nvqs; i++) { 1845 vqs[i] = &vs->vqs[i].vq; 1846 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick; 1847 } 1848 vhost_dev_init(&vs->dev, vqs, nvqs, UIO_MAXIOV, 1849 VHOST_SCSI_WEIGHT, 0, true, NULL); 1850 1851 vhost_scsi_init_inflight(vs, NULL); 1852 1853 f->private_data = vs; 1854 return 0; 1855 1856 err_local_vqs: 1857 kfree(vs->vqs); 1858 err_vqs: 1859 kfree(vs->old_inflight); 1860 err_inflight: 1861 bitmap_free(vs->compl_bitmap); 1862 err_compl_bitmap: 1863 kvfree(vs); 1864 err_vs: 1865 return r; 1866 } 1867 1868 static int vhost_scsi_release(struct inode *inode, struct file *f) 1869 { 1870 struct vhost_scsi *vs = f->private_data; 1871 struct vhost_scsi_target t; 1872 1873 mutex_lock(&vs->dev.mutex); 1874 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn)); 1875 mutex_unlock(&vs->dev.mutex); 1876 vhost_scsi_clear_endpoint(vs, &t); 1877 vhost_dev_stop(&vs->dev); 1878 vhost_dev_cleanup(&vs->dev); 1879 kfree(vs->dev.vqs); 1880 kfree(vs->vqs); 1881 kfree(vs->old_inflight); 1882 bitmap_free(vs->compl_bitmap); 1883 kvfree(vs); 1884 return 0; 1885 } 1886 1887 static long 1888 vhost_scsi_ioctl(struct file *f, 1889 unsigned int ioctl, 1890 unsigned long arg) 1891 { 1892 struct vhost_scsi *vs = f->private_data; 1893 struct vhost_scsi_target backend; 1894 void __user *argp = (void __user *)arg; 1895 u64 __user *featurep = argp; 1896 u32 __user *eventsp = argp; 1897 u32 events_missed; 1898 u64 features; 1899 int r, abi_version = VHOST_SCSI_ABI_VERSION; 1900 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 1901 1902 switch (ioctl) { 1903 case VHOST_SCSI_SET_ENDPOINT: 1904 if (copy_from_user(&backend, argp, sizeof backend)) 1905 return -EFAULT; 1906 if (backend.reserved != 0) 1907 return -EOPNOTSUPP; 1908 1909 return vhost_scsi_set_endpoint(vs, &backend); 1910 case VHOST_SCSI_CLEAR_ENDPOINT: 1911 if (copy_from_user(&backend, argp, sizeof backend)) 1912 return -EFAULT; 1913 if (backend.reserved != 0) 1914 return -EOPNOTSUPP; 1915 1916 return vhost_scsi_clear_endpoint(vs, &backend); 1917 case VHOST_SCSI_GET_ABI_VERSION: 1918 if (copy_to_user(argp, &abi_version, sizeof abi_version)) 1919 return -EFAULT; 1920 return 0; 1921 case VHOST_SCSI_SET_EVENTS_MISSED: 1922 if (get_user(events_missed, eventsp)) 1923 return -EFAULT; 1924 mutex_lock(&vq->mutex); 1925 vs->vs_events_missed = events_missed; 1926 mutex_unlock(&vq->mutex); 1927 return 0; 1928 case VHOST_SCSI_GET_EVENTS_MISSED: 1929 mutex_lock(&vq->mutex); 1930 events_missed = vs->vs_events_missed; 1931 mutex_unlock(&vq->mutex); 1932 if (put_user(events_missed, eventsp)) 1933 return -EFAULT; 1934 return 0; 1935 case VHOST_GET_FEATURES: 1936 features = VHOST_SCSI_FEATURES; 1937 if (copy_to_user(featurep, &features, sizeof features)) 1938 return -EFAULT; 1939 return 0; 1940 case VHOST_SET_FEATURES: 1941 if (copy_from_user(&features, featurep, sizeof features)) 1942 return -EFAULT; 1943 return vhost_scsi_set_features(vs, features); 1944 default: 1945 mutex_lock(&vs->dev.mutex); 1946 r = vhost_dev_ioctl(&vs->dev, ioctl, argp); 1947 /* TODO: flush backend after dev ioctl. */ 1948 if (r == -ENOIOCTLCMD) 1949 r = vhost_vring_ioctl(&vs->dev, ioctl, argp); 1950 mutex_unlock(&vs->dev.mutex); 1951 return r; 1952 } 1953 } 1954 1955 static const struct file_operations vhost_scsi_fops = { 1956 .owner = THIS_MODULE, 1957 .release = vhost_scsi_release, 1958 .unlocked_ioctl = vhost_scsi_ioctl, 1959 .compat_ioctl = compat_ptr_ioctl, 1960 .open = vhost_scsi_open, 1961 .llseek = noop_llseek, 1962 }; 1963 1964 static struct miscdevice vhost_scsi_misc = { 1965 MISC_DYNAMIC_MINOR, 1966 "vhost-scsi", 1967 &vhost_scsi_fops, 1968 }; 1969 1970 static int __init vhost_scsi_register(void) 1971 { 1972 return misc_register(&vhost_scsi_misc); 1973 } 1974 1975 static void vhost_scsi_deregister(void) 1976 { 1977 misc_deregister(&vhost_scsi_misc); 1978 } 1979 1980 static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport) 1981 { 1982 switch (tport->tport_proto_id) { 1983 case SCSI_PROTOCOL_SAS: 1984 return "SAS"; 1985 case SCSI_PROTOCOL_FCP: 1986 return "FCP"; 1987 case SCSI_PROTOCOL_ISCSI: 1988 return "iSCSI"; 1989 default: 1990 break; 1991 } 1992 1993 return "Unknown"; 1994 } 1995 1996 static void 1997 vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg, 1998 struct se_lun *lun, bool plug) 1999 { 2000 2001 struct vhost_scsi *vs = tpg->vhost_scsi; 2002 struct vhost_virtqueue *vq; 2003 u32 reason; 2004 2005 if (!vs) 2006 return; 2007 2008 if (plug) 2009 reason = VIRTIO_SCSI_EVT_RESET_RESCAN; 2010 else 2011 reason = VIRTIO_SCSI_EVT_RESET_REMOVED; 2012 2013 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 2014 mutex_lock(&vq->mutex); 2015 /* 2016 * We can't queue events if the backend has been cleared, because 2017 * we could end up queueing an event after the flush. 2018 */ 2019 if (!vhost_vq_get_backend(vq)) 2020 goto unlock; 2021 2022 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG)) 2023 vhost_scsi_send_evt(vs, tpg, lun, 2024 VIRTIO_SCSI_T_TRANSPORT_RESET, reason); 2025 unlock: 2026 mutex_unlock(&vq->mutex); 2027 } 2028 2029 static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun) 2030 { 2031 vhost_scsi_do_plug(tpg, lun, true); 2032 } 2033 2034 static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun) 2035 { 2036 vhost_scsi_do_plug(tpg, lun, false); 2037 } 2038 2039 static int vhost_scsi_port_link(struct se_portal_group *se_tpg, 2040 struct se_lun *lun) 2041 { 2042 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2043 struct vhost_scsi_tpg, se_tpg); 2044 2045 mutex_lock(&tpg->tv_tpg_mutex); 2046 tpg->tv_tpg_port_count++; 2047 vhost_scsi_hotplug(tpg, lun); 2048 mutex_unlock(&tpg->tv_tpg_mutex); 2049 2050 return 0; 2051 } 2052 2053 static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg, 2054 struct se_lun *lun) 2055 { 2056 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2057 struct vhost_scsi_tpg, se_tpg); 2058 2059 mutex_lock(&tpg->tv_tpg_mutex); 2060 tpg->tv_tpg_port_count--; 2061 vhost_scsi_hotunplug(tpg, lun); 2062 mutex_unlock(&tpg->tv_tpg_mutex); 2063 } 2064 2065 static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store( 2066 struct config_item *item, const char *page, size_t count) 2067 { 2068 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2069 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2070 struct vhost_scsi_tpg, se_tpg); 2071 unsigned long val; 2072 int ret = kstrtoul(page, 0, &val); 2073 2074 if (ret) { 2075 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret); 2076 return ret; 2077 } 2078 if (val != 0 && val != 1 && val != 3) { 2079 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val); 2080 return -EINVAL; 2081 } 2082 tpg->tv_fabric_prot_type = val; 2083 2084 return count; 2085 } 2086 2087 static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show( 2088 struct config_item *item, char *page) 2089 { 2090 struct se_portal_group *se_tpg = attrib_to_tpg(item); 2091 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2092 struct vhost_scsi_tpg, se_tpg); 2093 2094 return sysfs_emit(page, "%d\n", tpg->tv_fabric_prot_type); 2095 } 2096 2097 CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type); 2098 2099 static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = { 2100 &vhost_scsi_tpg_attrib_attr_fabric_prot_type, 2101 NULL, 2102 }; 2103 2104 static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg, 2105 const char *name) 2106 { 2107 struct vhost_scsi_nexus *tv_nexus; 2108 2109 mutex_lock(&tpg->tv_tpg_mutex); 2110 if (tpg->tpg_nexus) { 2111 mutex_unlock(&tpg->tv_tpg_mutex); 2112 pr_debug("tpg->tpg_nexus already exists\n"); 2113 return -EEXIST; 2114 } 2115 2116 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL); 2117 if (!tv_nexus) { 2118 mutex_unlock(&tpg->tv_tpg_mutex); 2119 pr_err("Unable to allocate struct vhost_scsi_nexus\n"); 2120 return -ENOMEM; 2121 } 2122 /* 2123 * Since we are running in 'demo mode' this call with generate a 2124 * struct se_node_acl for the vhost_scsi struct se_portal_group with 2125 * the SCSI Initiator port name of the passed configfs group 'name'. 2126 */ 2127 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 0, 0, 2128 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS, 2129 (unsigned char *)name, tv_nexus, NULL); 2130 if (IS_ERR(tv_nexus->tvn_se_sess)) { 2131 mutex_unlock(&tpg->tv_tpg_mutex); 2132 kfree(tv_nexus); 2133 return -ENOMEM; 2134 } 2135 tpg->tpg_nexus = tv_nexus; 2136 2137 mutex_unlock(&tpg->tv_tpg_mutex); 2138 return 0; 2139 } 2140 2141 static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg) 2142 { 2143 struct se_session *se_sess; 2144 struct vhost_scsi_nexus *tv_nexus; 2145 2146 mutex_lock(&tpg->tv_tpg_mutex); 2147 tv_nexus = tpg->tpg_nexus; 2148 if (!tv_nexus) { 2149 mutex_unlock(&tpg->tv_tpg_mutex); 2150 return -ENODEV; 2151 } 2152 2153 se_sess = tv_nexus->tvn_se_sess; 2154 if (!se_sess) { 2155 mutex_unlock(&tpg->tv_tpg_mutex); 2156 return -ENODEV; 2157 } 2158 2159 if (tpg->tv_tpg_port_count != 0) { 2160 mutex_unlock(&tpg->tv_tpg_mutex); 2161 pr_err("Unable to remove TCM_vhost I_T Nexus with" 2162 " active TPG port count: %d\n", 2163 tpg->tv_tpg_port_count); 2164 return -EBUSY; 2165 } 2166 2167 if (tpg->tv_tpg_vhost_count != 0) { 2168 mutex_unlock(&tpg->tv_tpg_mutex); 2169 pr_err("Unable to remove TCM_vhost I_T Nexus with" 2170 " active TPG vhost count: %d\n", 2171 tpg->tv_tpg_vhost_count); 2172 return -EBUSY; 2173 } 2174 2175 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated" 2176 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport), 2177 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 2178 2179 /* 2180 * Release the SCSI I_T Nexus to the emulated vhost Target Port 2181 */ 2182 target_remove_session(se_sess); 2183 tpg->tpg_nexus = NULL; 2184 mutex_unlock(&tpg->tv_tpg_mutex); 2185 2186 kfree(tv_nexus); 2187 return 0; 2188 } 2189 2190 static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page) 2191 { 2192 struct se_portal_group *se_tpg = to_tpg(item); 2193 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2194 struct vhost_scsi_tpg, se_tpg); 2195 struct vhost_scsi_nexus *tv_nexus; 2196 ssize_t ret; 2197 2198 mutex_lock(&tpg->tv_tpg_mutex); 2199 tv_nexus = tpg->tpg_nexus; 2200 if (!tv_nexus) { 2201 mutex_unlock(&tpg->tv_tpg_mutex); 2202 return -ENODEV; 2203 } 2204 ret = sysfs_emit(page, "%s\n", 2205 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 2206 mutex_unlock(&tpg->tv_tpg_mutex); 2207 2208 return ret; 2209 } 2210 2211 static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item, 2212 const char *page, size_t count) 2213 { 2214 struct se_portal_group *se_tpg = to_tpg(item); 2215 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2216 struct vhost_scsi_tpg, se_tpg); 2217 struct vhost_scsi_tport *tport_wwn = tpg->tport; 2218 unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr; 2219 int ret; 2220 /* 2221 * Shutdown the active I_T nexus if 'NULL' is passed.. 2222 */ 2223 if (!strncmp(page, "NULL", 4)) { 2224 ret = vhost_scsi_drop_nexus(tpg); 2225 return (!ret) ? count : ret; 2226 } 2227 /* 2228 * Otherwise make sure the passed virtual Initiator port WWN matches 2229 * the fabric protocol_id set in vhost_scsi_make_tport(), and call 2230 * vhost_scsi_make_nexus(). 2231 */ 2232 if (strlen(page) >= VHOST_SCSI_NAMELEN) { 2233 pr_err("Emulated NAA Sas Address: %s, exceeds" 2234 " max: %d\n", page, VHOST_SCSI_NAMELEN); 2235 return -EINVAL; 2236 } 2237 snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page); 2238 2239 ptr = strstr(i_port, "naa."); 2240 if (ptr) { 2241 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { 2242 pr_err("Passed SAS Initiator Port %s does not" 2243 " match target port protoid: %s\n", i_port, 2244 vhost_scsi_dump_proto_id(tport_wwn)); 2245 return -EINVAL; 2246 } 2247 port_ptr = &i_port[0]; 2248 goto check_newline; 2249 } 2250 ptr = strstr(i_port, "fc."); 2251 if (ptr) { 2252 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { 2253 pr_err("Passed FCP Initiator Port %s does not" 2254 " match target port protoid: %s\n", i_port, 2255 vhost_scsi_dump_proto_id(tport_wwn)); 2256 return -EINVAL; 2257 } 2258 port_ptr = &i_port[3]; /* Skip over "fc." */ 2259 goto check_newline; 2260 } 2261 ptr = strstr(i_port, "iqn."); 2262 if (ptr) { 2263 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { 2264 pr_err("Passed iSCSI Initiator Port %s does not" 2265 " match target port protoid: %s\n", i_port, 2266 vhost_scsi_dump_proto_id(tport_wwn)); 2267 return -EINVAL; 2268 } 2269 port_ptr = &i_port[0]; 2270 goto check_newline; 2271 } 2272 pr_err("Unable to locate prefix for emulated Initiator Port:" 2273 " %s\n", i_port); 2274 return -EINVAL; 2275 /* 2276 * Clear any trailing newline for the NAA WWN 2277 */ 2278 check_newline: 2279 if (i_port[strlen(i_port)-1] == '\n') 2280 i_port[strlen(i_port)-1] = '\0'; 2281 2282 ret = vhost_scsi_make_nexus(tpg, port_ptr); 2283 if (ret < 0) 2284 return ret; 2285 2286 return count; 2287 } 2288 2289 CONFIGFS_ATTR(vhost_scsi_tpg_, nexus); 2290 2291 static struct configfs_attribute *vhost_scsi_tpg_attrs[] = { 2292 &vhost_scsi_tpg_attr_nexus, 2293 NULL, 2294 }; 2295 2296 static struct se_portal_group * 2297 vhost_scsi_make_tpg(struct se_wwn *wwn, const char *name) 2298 { 2299 struct vhost_scsi_tport *tport = container_of(wwn, 2300 struct vhost_scsi_tport, tport_wwn); 2301 2302 struct vhost_scsi_tpg *tpg; 2303 u16 tpgt; 2304 int ret; 2305 2306 if (strstr(name, "tpgt_") != name) 2307 return ERR_PTR(-EINVAL); 2308 if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET) 2309 return ERR_PTR(-EINVAL); 2310 2311 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL); 2312 if (!tpg) { 2313 pr_err("Unable to allocate struct vhost_scsi_tpg"); 2314 return ERR_PTR(-ENOMEM); 2315 } 2316 mutex_init(&tpg->tv_tpg_mutex); 2317 INIT_LIST_HEAD(&tpg->tv_tpg_list); 2318 tpg->tport = tport; 2319 tpg->tport_tpgt = tpgt; 2320 2321 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id); 2322 if (ret < 0) { 2323 kfree(tpg); 2324 return NULL; 2325 } 2326 mutex_lock(&vhost_scsi_mutex); 2327 list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list); 2328 mutex_unlock(&vhost_scsi_mutex); 2329 2330 return &tpg->se_tpg; 2331 } 2332 2333 static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg) 2334 { 2335 struct vhost_scsi_tpg *tpg = container_of(se_tpg, 2336 struct vhost_scsi_tpg, se_tpg); 2337 2338 mutex_lock(&vhost_scsi_mutex); 2339 list_del(&tpg->tv_tpg_list); 2340 mutex_unlock(&vhost_scsi_mutex); 2341 /* 2342 * Release the virtual I_T Nexus for this vhost TPG 2343 */ 2344 vhost_scsi_drop_nexus(tpg); 2345 /* 2346 * Deregister the se_tpg from TCM.. 2347 */ 2348 core_tpg_deregister(se_tpg); 2349 kfree(tpg); 2350 } 2351 2352 static struct se_wwn * 2353 vhost_scsi_make_tport(struct target_fabric_configfs *tf, 2354 struct config_group *group, 2355 const char *name) 2356 { 2357 struct vhost_scsi_tport *tport; 2358 char *ptr; 2359 u64 wwpn = 0; 2360 int off = 0; 2361 2362 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0) 2363 return ERR_PTR(-EINVAL); */ 2364 2365 tport = kzalloc(sizeof(*tport), GFP_KERNEL); 2366 if (!tport) { 2367 pr_err("Unable to allocate struct vhost_scsi_tport"); 2368 return ERR_PTR(-ENOMEM); 2369 } 2370 tport->tport_wwpn = wwpn; 2371 /* 2372 * Determine the emulated Protocol Identifier and Target Port Name 2373 * based on the incoming configfs directory name. 2374 */ 2375 ptr = strstr(name, "naa."); 2376 if (ptr) { 2377 tport->tport_proto_id = SCSI_PROTOCOL_SAS; 2378 goto check_len; 2379 } 2380 ptr = strstr(name, "fc."); 2381 if (ptr) { 2382 tport->tport_proto_id = SCSI_PROTOCOL_FCP; 2383 off = 3; /* Skip over "fc." */ 2384 goto check_len; 2385 } 2386 ptr = strstr(name, "iqn."); 2387 if (ptr) { 2388 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; 2389 goto check_len; 2390 } 2391 2392 pr_err("Unable to locate prefix for emulated Target Port:" 2393 " %s\n", name); 2394 kfree(tport); 2395 return ERR_PTR(-EINVAL); 2396 2397 check_len: 2398 if (strlen(name) >= VHOST_SCSI_NAMELEN) { 2399 pr_err("Emulated %s Address: %s, exceeds" 2400 " max: %d\n", name, vhost_scsi_dump_proto_id(tport), 2401 VHOST_SCSI_NAMELEN); 2402 kfree(tport); 2403 return ERR_PTR(-EINVAL); 2404 } 2405 snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]); 2406 2407 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target" 2408 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name); 2409 2410 return &tport->tport_wwn; 2411 } 2412 2413 static void vhost_scsi_drop_tport(struct se_wwn *wwn) 2414 { 2415 struct vhost_scsi_tport *tport = container_of(wwn, 2416 struct vhost_scsi_tport, tport_wwn); 2417 2418 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target" 2419 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), 2420 tport->tport_name); 2421 2422 kfree(tport); 2423 } 2424 2425 static ssize_t 2426 vhost_scsi_wwn_version_show(struct config_item *item, char *page) 2427 { 2428 return sysfs_emit(page, "TCM_VHOST fabric module %s on %s/%s" 2429 "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname, 2430 utsname()->machine); 2431 } 2432 2433 CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version); 2434 2435 static struct configfs_attribute *vhost_scsi_wwn_attrs[] = { 2436 &vhost_scsi_wwn_attr_version, 2437 NULL, 2438 }; 2439 2440 static const struct target_core_fabric_ops vhost_scsi_ops = { 2441 .module = THIS_MODULE, 2442 .fabric_name = "vhost", 2443 .max_data_sg_nents = VHOST_SCSI_PREALLOC_SGLS, 2444 .tpg_get_wwn = vhost_scsi_get_fabric_wwn, 2445 .tpg_get_tag = vhost_scsi_get_tpgt, 2446 .tpg_check_demo_mode = vhost_scsi_check_true, 2447 .tpg_check_demo_mode_cache = vhost_scsi_check_true, 2448 .tpg_check_demo_mode_write_protect = vhost_scsi_check_false, 2449 .tpg_check_prod_mode_write_protect = vhost_scsi_check_false, 2450 .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only, 2451 .tpg_get_inst_index = vhost_scsi_tpg_get_inst_index, 2452 .release_cmd = vhost_scsi_release_cmd, 2453 .check_stop_free = vhost_scsi_check_stop_free, 2454 .sess_get_index = vhost_scsi_sess_get_index, 2455 .sess_get_initiator_sid = NULL, 2456 .write_pending = vhost_scsi_write_pending, 2457 .set_default_node_attributes = vhost_scsi_set_default_node_attrs, 2458 .get_cmd_state = vhost_scsi_get_cmd_state, 2459 .queue_data_in = vhost_scsi_queue_data_in, 2460 .queue_status = vhost_scsi_queue_status, 2461 .queue_tm_rsp = vhost_scsi_queue_tm_rsp, 2462 .aborted_task = vhost_scsi_aborted_task, 2463 /* 2464 * Setup callers for generic logic in target_core_fabric_configfs.c 2465 */ 2466 .fabric_make_wwn = vhost_scsi_make_tport, 2467 .fabric_drop_wwn = vhost_scsi_drop_tport, 2468 .fabric_make_tpg = vhost_scsi_make_tpg, 2469 .fabric_drop_tpg = vhost_scsi_drop_tpg, 2470 .fabric_post_link = vhost_scsi_port_link, 2471 .fabric_pre_unlink = vhost_scsi_port_unlink, 2472 2473 .tfc_wwn_attrs = vhost_scsi_wwn_attrs, 2474 .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs, 2475 .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs, 2476 }; 2477 2478 static int __init vhost_scsi_init(void) 2479 { 2480 int ret = -ENOMEM; 2481 2482 pr_debug("TCM_VHOST fabric module %s on %s/%s" 2483 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname, 2484 utsname()->machine); 2485 2486 ret = vhost_scsi_register(); 2487 if (ret < 0) 2488 goto out; 2489 2490 ret = target_register_template(&vhost_scsi_ops); 2491 if (ret < 0) 2492 goto out_vhost_scsi_deregister; 2493 2494 return 0; 2495 2496 out_vhost_scsi_deregister: 2497 vhost_scsi_deregister(); 2498 out: 2499 return ret; 2500 }; 2501 2502 static void vhost_scsi_exit(void) 2503 { 2504 target_unregister_template(&vhost_scsi_ops); 2505 vhost_scsi_deregister(); 2506 }; 2507 2508 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver"); 2509 MODULE_ALIAS("tcm_vhost"); 2510 MODULE_LICENSE("GPL"); 2511 module_init(vhost_scsi_init); 2512 module_exit(vhost_scsi_exit); 2513