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