Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Vhost kernel TCM fabric driver for virtio SCSI initiators |
| 3 | * |
| 4 | * (C) Copyright 2010-2012 RisingTide Systems LLC. |
| 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@risingtidesystems.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> |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 37 | #include <linux/fs.h> |
| 38 | #include <linux/miscdevice.h> |
| 39 | #include <asm/unaligned.h> |
| 40 | #include <scsi/scsi.h> |
| 41 | #include <scsi/scsi_tcq.h> |
| 42 | #include <target/target_core_base.h> |
| 43 | #include <target/target_core_fabric.h> |
| 44 | #include <target/target_core_fabric_configfs.h> |
| 45 | #include <target/target_core_configfs.h> |
| 46 | #include <target/configfs_macros.h> |
| 47 | #include <linux/vhost.h> |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 48 | #include <linux/virtio_scsi.h> |
Asias He | 9d6064a | 2013-01-06 14:36:13 +0800 | [diff] [blame] | 49 | #include <linux/llist.h> |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 50 | #include <linux/bitmap.h> |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 51 | |
| 52 | #include "vhost.c" |
| 53 | #include "vhost.h" |
| 54 | #include "tcm_vhost.h" |
| 55 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 56 | enum { |
| 57 | VHOST_SCSI_VQ_CTL = 0, |
| 58 | VHOST_SCSI_VQ_EVT = 1, |
| 59 | VHOST_SCSI_VQ_IO = 2, |
| 60 | }; |
| 61 | |
Nicholas Bellinger | 5dade71 | 2013-03-27 17:23:41 -0700 | [diff] [blame] | 62 | /* |
| 63 | * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in |
| 64 | * kernel but disabling it helps. |
| 65 | * TODO: debug and remove the workaround. |
| 66 | */ |
| 67 | enum { |
Asias He | 04b59ba | 2013-04-25 15:35:23 +0800 | [diff] [blame] | 68 | VHOST_SCSI_FEATURES = (VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)) | |
| 69 | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
Nicholas Bellinger | 5dade71 | 2013-03-27 17:23:41 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 72 | #define VHOST_SCSI_MAX_TARGET 256 |
| 73 | #define VHOST_SCSI_MAX_VQ 128 |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 74 | #define VHOST_SCSI_MAX_EVENT 128 |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 75 | |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 76 | struct vhost_scsi_inflight { |
| 77 | /* Wait for the flush operation to finish */ |
| 78 | struct completion comp; |
| 79 | /* Refcount for the inflight reqs */ |
| 80 | struct kref kref; |
| 81 | }; |
| 82 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 83 | struct vhost_scsi_virtqueue { |
| 84 | struct vhost_virtqueue vq; |
Michael S. Tsirkin | 3dfbff3 | 2013-04-28 15:38:52 +0300 | [diff] [blame] | 85 | /* |
| 86 | * Reference counting for inflight reqs, used for flush operation. At |
| 87 | * each time, one reference tracks new commands submitted, while we |
| 88 | * wait for another one to reach 0. |
| 89 | */ |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 90 | struct vhost_scsi_inflight inflights[2]; |
Michael S. Tsirkin | 3dfbff3 | 2013-04-28 15:38:52 +0300 | [diff] [blame] | 91 | /* |
| 92 | * Indicate current inflight in use, protected by vq->mutex. |
| 93 | * Writers must also take dev mutex and flush under it. |
| 94 | */ |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 95 | int inflight_idx; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 96 | }; |
| 97 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 98 | struct vhost_scsi { |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 99 | /* Protected by vhost_scsi->dev.mutex */ |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 100 | struct tcm_vhost_tpg **vs_tpg; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 101 | char vs_vhost_wwpn[TRANSPORT_IQN_LEN]; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 102 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 103 | struct vhost_dev dev; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 104 | struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ]; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 105 | |
| 106 | struct vhost_work vs_completion_work; /* cmd completion work item */ |
Asias He | 9d6064a | 2013-01-06 14:36:13 +0800 | [diff] [blame] | 107 | struct llist_head vs_completion_list; /* cmd completion queue */ |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 108 | |
| 109 | struct vhost_work vs_event_work; /* evt injection work item */ |
| 110 | struct llist_head vs_event_list; /* evt injection queue */ |
| 111 | |
| 112 | bool vs_events_missed; /* any missed events, protected by vq->mutex */ |
| 113 | int vs_events_nr; /* num of pending events, protected by vq->mutex */ |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* Local pointer to allocated TCM configfs fabric module */ |
| 117 | static struct target_fabric_configfs *tcm_vhost_fabric_configfs; |
| 118 | |
| 119 | static struct workqueue_struct *tcm_vhost_workqueue; |
| 120 | |
| 121 | /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */ |
| 122 | static DEFINE_MUTEX(tcm_vhost_mutex); |
| 123 | static LIST_HEAD(tcm_vhost_list); |
| 124 | |
Asias He | 765b34f | 2013-01-22 11:20:25 +0800 | [diff] [blame] | 125 | static int iov_num_pages(struct iovec *iov) |
| 126 | { |
| 127 | return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) - |
| 128 | ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT; |
| 129 | } |
| 130 | |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 131 | void tcm_vhost_done_inflight(struct kref *kref) |
| 132 | { |
| 133 | struct vhost_scsi_inflight *inflight; |
| 134 | |
| 135 | inflight = container_of(kref, struct vhost_scsi_inflight, kref); |
| 136 | complete(&inflight->comp); |
| 137 | } |
| 138 | |
| 139 | static void tcm_vhost_init_inflight(struct vhost_scsi *vs, |
| 140 | struct vhost_scsi_inflight *old_inflight[]) |
| 141 | { |
| 142 | struct vhost_scsi_inflight *new_inflight; |
| 143 | struct vhost_virtqueue *vq; |
| 144 | int idx, i; |
| 145 | |
| 146 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { |
| 147 | vq = &vs->vqs[i].vq; |
| 148 | |
| 149 | mutex_lock(&vq->mutex); |
| 150 | |
| 151 | /* store old infight */ |
| 152 | idx = vs->vqs[i].inflight_idx; |
| 153 | if (old_inflight) |
| 154 | old_inflight[i] = &vs->vqs[i].inflights[idx]; |
| 155 | |
| 156 | /* setup new infight */ |
| 157 | vs->vqs[i].inflight_idx = idx ^ 1; |
| 158 | new_inflight = &vs->vqs[i].inflights[idx ^ 1]; |
| 159 | kref_init(&new_inflight->kref); |
| 160 | init_completion(&new_inflight->comp); |
| 161 | |
| 162 | mutex_unlock(&vq->mutex); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static struct vhost_scsi_inflight * |
| 167 | tcm_vhost_get_inflight(struct vhost_virtqueue *vq) |
| 168 | { |
| 169 | struct vhost_scsi_inflight *inflight; |
| 170 | struct vhost_scsi_virtqueue *svq; |
| 171 | |
| 172 | svq = container_of(vq, struct vhost_scsi_virtqueue, vq); |
| 173 | inflight = &svq->inflights[svq->inflight_idx]; |
| 174 | kref_get(&inflight->kref); |
| 175 | |
| 176 | return inflight; |
| 177 | } |
| 178 | |
| 179 | static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight) |
| 180 | { |
| 181 | kref_put(&inflight->kref, tcm_vhost_done_inflight); |
| 182 | } |
| 183 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 184 | static int tcm_vhost_check_true(struct se_portal_group *se_tpg) |
| 185 | { |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | static int tcm_vhost_check_false(struct se_portal_group *se_tpg) |
| 190 | { |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static char *tcm_vhost_get_fabric_name(void) |
| 195 | { |
| 196 | return "vhost"; |
| 197 | } |
| 198 | |
| 199 | static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg) |
| 200 | { |
| 201 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 202 | struct tcm_vhost_tpg, se_tpg); |
| 203 | struct tcm_vhost_tport *tport = tpg->tport; |
| 204 | |
| 205 | switch (tport->tport_proto_id) { |
| 206 | case SCSI_PROTOCOL_SAS: |
| 207 | return sas_get_fabric_proto_ident(se_tpg); |
| 208 | case SCSI_PROTOCOL_FCP: |
| 209 | return fc_get_fabric_proto_ident(se_tpg); |
| 210 | case SCSI_PROTOCOL_ISCSI: |
| 211 | return iscsi_get_fabric_proto_ident(se_tpg); |
| 212 | default: |
| 213 | pr_err("Unknown tport_proto_id: 0x%02x, using" |
| 214 | " SAS emulation\n", tport->tport_proto_id); |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | return sas_get_fabric_proto_ident(se_tpg); |
| 219 | } |
| 220 | |
| 221 | static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg) |
| 222 | { |
| 223 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 224 | struct tcm_vhost_tpg, se_tpg); |
| 225 | struct tcm_vhost_tport *tport = tpg->tport; |
| 226 | |
| 227 | return &tport->tport_name[0]; |
| 228 | } |
| 229 | |
| 230 | static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg) |
| 231 | { |
| 232 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 233 | struct tcm_vhost_tpg, se_tpg); |
| 234 | return tpg->tport_tpgt; |
| 235 | } |
| 236 | |
| 237 | static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg) |
| 238 | { |
| 239 | return 1; |
| 240 | } |
| 241 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 242 | static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 243 | struct se_node_acl *se_nacl, |
| 244 | struct t10_pr_registration *pr_reg, |
| 245 | int *format_code, |
| 246 | unsigned char *buf) |
| 247 | { |
| 248 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 249 | struct tcm_vhost_tpg, se_tpg); |
| 250 | struct tcm_vhost_tport *tport = tpg->tport; |
| 251 | |
| 252 | switch (tport->tport_proto_id) { |
| 253 | case SCSI_PROTOCOL_SAS: |
| 254 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 255 | format_code, buf); |
| 256 | case SCSI_PROTOCOL_FCP: |
| 257 | return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 258 | format_code, buf); |
| 259 | case SCSI_PROTOCOL_ISCSI: |
| 260 | return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 261 | format_code, buf); |
| 262 | default: |
| 263 | pr_err("Unknown tport_proto_id: 0x%02x, using" |
| 264 | " SAS emulation\n", tport->tport_proto_id); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg, |
| 269 | format_code, buf); |
| 270 | } |
| 271 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 272 | static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 273 | struct se_node_acl *se_nacl, |
| 274 | struct t10_pr_registration *pr_reg, |
| 275 | int *format_code) |
| 276 | { |
| 277 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 278 | struct tcm_vhost_tpg, se_tpg); |
| 279 | struct tcm_vhost_tport *tport = tpg->tport; |
| 280 | |
| 281 | switch (tport->tport_proto_id) { |
| 282 | case SCSI_PROTOCOL_SAS: |
| 283 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 284 | format_code); |
| 285 | case SCSI_PROTOCOL_FCP: |
| 286 | return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 287 | format_code); |
| 288 | case SCSI_PROTOCOL_ISCSI: |
| 289 | return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 290 | format_code); |
| 291 | default: |
| 292 | pr_err("Unknown tport_proto_id: 0x%02x, using" |
| 293 | " SAS emulation\n", tport->tport_proto_id); |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg, |
| 298 | format_code); |
| 299 | } |
| 300 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 301 | static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 302 | const char *buf, |
| 303 | u32 *out_tid_len, |
| 304 | char **port_nexus_ptr) |
| 305 | { |
| 306 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 307 | struct tcm_vhost_tpg, se_tpg); |
| 308 | struct tcm_vhost_tport *tport = tpg->tport; |
| 309 | |
| 310 | switch (tport->tport_proto_id) { |
| 311 | case SCSI_PROTOCOL_SAS: |
| 312 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 313 | port_nexus_ptr); |
| 314 | case SCSI_PROTOCOL_FCP: |
| 315 | return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 316 | port_nexus_ptr); |
| 317 | case SCSI_PROTOCOL_ISCSI: |
| 318 | return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 319 | port_nexus_ptr); |
| 320 | default: |
| 321 | pr_err("Unknown tport_proto_id: 0x%02x, using" |
| 322 | " SAS emulation\n", tport->tport_proto_id); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len, |
| 327 | port_nexus_ptr); |
| 328 | } |
| 329 | |
| 330 | static struct se_node_acl *tcm_vhost_alloc_fabric_acl( |
| 331 | struct se_portal_group *se_tpg) |
| 332 | { |
| 333 | struct tcm_vhost_nacl *nacl; |
| 334 | |
| 335 | nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL); |
| 336 | if (!nacl) { |
Masanari Iida | 744627e9 | 2012-11-05 23:30:40 +0900 | [diff] [blame] | 337 | pr_err("Unable to allocate struct tcm_vhost_nacl\n"); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | return &nacl->se_node_acl; |
| 342 | } |
| 343 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 344 | static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 345 | struct se_node_acl *se_nacl) |
| 346 | { |
| 347 | struct tcm_vhost_nacl *nacl = container_of(se_nacl, |
| 348 | struct tcm_vhost_nacl, se_node_acl); |
| 349 | kfree(nacl); |
| 350 | } |
| 351 | |
| 352 | static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg) |
| 353 | { |
| 354 | return 1; |
| 355 | } |
| 356 | |
| 357 | static void tcm_vhost_release_cmd(struct se_cmd *se_cmd) |
| 358 | { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | static int tcm_vhost_shutdown_session(struct se_session *se_sess) |
| 363 | { |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static void tcm_vhost_close_session(struct se_session *se_sess) |
| 368 | { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | static u32 tcm_vhost_sess_get_index(struct se_session *se_sess) |
| 373 | { |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int tcm_vhost_write_pending(struct se_cmd *se_cmd) |
| 378 | { |
| 379 | /* Go ahead and process the write immediately */ |
| 380 | target_execute_cmd(se_cmd); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd) |
| 385 | { |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl) |
| 390 | { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd) |
| 395 | { |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd) |
| 400 | { |
| 401 | return 0; |
| 402 | } |
| 403 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 404 | static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd) |
| 405 | { |
| 406 | struct vhost_scsi *vs = tv_cmd->tvc_vhost; |
| 407 | |
Asias He | 9d6064a | 2013-01-06 14:36:13 +0800 | [diff] [blame] | 408 | llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list); |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 409 | |
| 410 | vhost_work_queue(&vs->dev, &vs->vs_completion_work); |
| 411 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 412 | |
| 413 | static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd) |
| 414 | { |
| 415 | struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, |
| 416 | struct tcm_vhost_cmd, tvc_se_cmd); |
| 417 | vhost_scsi_complete_cmd(tv_cmd); |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static int tcm_vhost_queue_status(struct se_cmd *se_cmd) |
| 422 | { |
| 423 | struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, |
| 424 | struct tcm_vhost_cmd, tvc_se_cmd); |
| 425 | vhost_scsi_complete_cmd(tv_cmd); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd) |
| 430 | { |
| 431 | return 0; |
| 432 | } |
| 433 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 434 | static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt) |
| 435 | { |
| 436 | vs->vs_events_nr--; |
| 437 | kfree(evt); |
| 438 | } |
| 439 | |
| 440 | static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs, |
| 441 | u32 event, u32 reason) |
| 442 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 443 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 444 | struct tcm_vhost_evt *evt; |
| 445 | |
| 446 | if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) { |
| 447 | vs->vs_events_missed = true; |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | evt = kzalloc(sizeof(*evt), GFP_KERNEL); |
| 452 | if (!evt) { |
| 453 | vq_err(vq, "Failed to allocate tcm_vhost_evt\n"); |
| 454 | vs->vs_events_missed = true; |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | evt->event.event = event; |
| 459 | evt->event.reason = reason; |
| 460 | vs->vs_events_nr++; |
| 461 | |
| 462 | return evt; |
| 463 | } |
| 464 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 465 | static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd) |
| 466 | { |
| 467 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; |
| 468 | |
| 469 | /* TODO locking against target/backend threads? */ |
| 470 | transport_generic_free_cmd(se_cmd, 1); |
| 471 | |
| 472 | if (tv_cmd->tvc_sgl_count) { |
| 473 | u32 i; |
| 474 | for (i = 0; i < tv_cmd->tvc_sgl_count; i++) |
| 475 | put_page(sg_page(&tv_cmd->tvc_sgl[i])); |
| 476 | |
| 477 | kfree(tv_cmd->tvc_sgl); |
| 478 | } |
| 479 | |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 480 | tcm_vhost_put_inflight(tv_cmd->inflight); |
| 481 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 482 | kfree(tv_cmd); |
| 483 | } |
| 484 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 485 | static void tcm_vhost_do_evt_work(struct vhost_scsi *vs, |
| 486 | struct tcm_vhost_evt *evt) |
| 487 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 488 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 489 | struct virtio_scsi_event *event = &evt->event; |
| 490 | struct virtio_scsi_event __user *eventp; |
| 491 | unsigned out, in; |
| 492 | int head, ret; |
| 493 | |
| 494 | if (!vq->private_data) { |
| 495 | vs->vs_events_missed = true; |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | again: |
| 500 | vhost_disable_notify(&vs->dev, vq); |
| 501 | head = vhost_get_vq_desc(&vs->dev, vq, vq->iov, |
| 502 | ARRAY_SIZE(vq->iov), &out, &in, |
| 503 | NULL, NULL); |
| 504 | if (head < 0) { |
| 505 | vs->vs_events_missed = true; |
| 506 | return; |
| 507 | } |
| 508 | if (head == vq->num) { |
| 509 | if (vhost_enable_notify(&vs->dev, vq)) |
| 510 | goto again; |
| 511 | vs->vs_events_missed = true; |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) { |
| 516 | vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n", |
| 517 | vq->iov[out].iov_len); |
| 518 | vs->vs_events_missed = true; |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (vs->vs_events_missed) { |
| 523 | event->event |= VIRTIO_SCSI_T_EVENTS_MISSED; |
| 524 | vs->vs_events_missed = false; |
| 525 | } |
| 526 | |
| 527 | eventp = vq->iov[out].iov_base; |
| 528 | ret = __copy_to_user(eventp, event, sizeof(*event)); |
| 529 | if (!ret) |
| 530 | vhost_add_used_and_signal(&vs->dev, vq, head, 0); |
| 531 | else |
| 532 | vq_err(vq, "Faulted on tcm_vhost_send_event\n"); |
| 533 | } |
| 534 | |
| 535 | static void tcm_vhost_evt_work(struct vhost_work *work) |
| 536 | { |
| 537 | struct vhost_scsi *vs = container_of(work, struct vhost_scsi, |
| 538 | vs_event_work); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 539 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 540 | struct tcm_vhost_evt *evt; |
| 541 | struct llist_node *llnode; |
| 542 | |
| 543 | mutex_lock(&vq->mutex); |
| 544 | llnode = llist_del_all(&vs->vs_event_list); |
| 545 | while (llnode) { |
| 546 | evt = llist_entry(llnode, struct tcm_vhost_evt, list); |
| 547 | llnode = llist_next(llnode); |
| 548 | tcm_vhost_do_evt_work(vs, evt); |
| 549 | tcm_vhost_free_evt(vs, evt); |
| 550 | } |
| 551 | mutex_unlock(&vq->mutex); |
| 552 | } |
| 553 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 554 | /* Fill in status and signal that we are done processing this command |
| 555 | * |
| 556 | * This is scheduled in the vhost work queue so we are called with the owner |
| 557 | * process mm and can access the vring. |
| 558 | */ |
| 559 | static void vhost_scsi_complete_cmd_work(struct vhost_work *work) |
| 560 | { |
| 561 | struct vhost_scsi *vs = container_of(work, struct vhost_scsi, |
| 562 | vs_completion_work); |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 563 | DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ); |
Asias He | 9d6064a | 2013-01-06 14:36:13 +0800 | [diff] [blame] | 564 | struct virtio_scsi_cmd_resp v_rsp; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 565 | struct tcm_vhost_cmd *tv_cmd; |
Asias He | 9d6064a | 2013-01-06 14:36:13 +0800 | [diff] [blame] | 566 | struct llist_node *llnode; |
| 567 | struct se_cmd *se_cmd; |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 568 | int ret, vq; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 569 | |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 570 | bitmap_zero(signal, VHOST_SCSI_MAX_VQ); |
Asias He | 9d6064a | 2013-01-06 14:36:13 +0800 | [diff] [blame] | 571 | llnode = llist_del_all(&vs->vs_completion_list); |
| 572 | while (llnode) { |
| 573 | tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd, |
| 574 | tvc_completion_list); |
| 575 | llnode = llist_next(llnode); |
| 576 | se_cmd = &tv_cmd->tvc_se_cmd; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 577 | |
| 578 | pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__, |
| 579 | tv_cmd, se_cmd->residual_count, se_cmd->scsi_status); |
| 580 | |
| 581 | memset(&v_rsp, 0, sizeof(v_rsp)); |
| 582 | v_rsp.resid = se_cmd->residual_count; |
| 583 | /* TODO is status_qualifier field needed? */ |
| 584 | v_rsp.status = se_cmd->scsi_status; |
| 585 | v_rsp.sense_len = se_cmd->scsi_sense_length; |
| 586 | memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf, |
| 587 | v_rsp.sense_len); |
| 588 | ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp)); |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 589 | if (likely(ret == 0)) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 590 | struct vhost_scsi_virtqueue *q; |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 591 | vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 592 | q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq); |
| 593 | vq = q - vs->vqs; |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 594 | __set_bit(vq, signal); |
| 595 | } else |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 596 | pr_err("Faulted on virtio_scsi_cmd_resp\n"); |
| 597 | |
| 598 | vhost_scsi_free_cmd(tv_cmd); |
| 599 | } |
| 600 | |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 601 | vq = -1; |
| 602 | while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1)) |
| 603 | < VHOST_SCSI_MAX_VQ) |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 604 | vhost_signal(&vs->dev, &vs->vqs[vq].vq); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 607 | static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd( |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 608 | struct vhost_virtqueue *vq, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 609 | struct tcm_vhost_tpg *tv_tpg, |
| 610 | struct virtio_scsi_cmd_req *v_req, |
| 611 | u32 exp_data_len, |
| 612 | int data_direction) |
| 613 | { |
| 614 | struct tcm_vhost_cmd *tv_cmd; |
| 615 | struct tcm_vhost_nexus *tv_nexus; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 616 | |
| 617 | tv_nexus = tv_tpg->tpg_nexus; |
| 618 | if (!tv_nexus) { |
| 619 | pr_err("Unable to locate active struct tcm_vhost_nexus\n"); |
| 620 | return ERR_PTR(-EIO); |
| 621 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 622 | |
| 623 | tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC); |
| 624 | if (!tv_cmd) { |
| 625 | pr_err("Unable to allocate struct tcm_vhost_cmd\n"); |
| 626 | return ERR_PTR(-ENOMEM); |
| 627 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 628 | tv_cmd->tvc_tag = v_req->tag; |
Nicholas Bellinger | 9f0abc1 | 2012-10-01 18:40:55 -0700 | [diff] [blame] | 629 | tv_cmd->tvc_task_attr = v_req->task_attr; |
| 630 | tv_cmd->tvc_exp_data_len = exp_data_len; |
| 631 | tv_cmd->tvc_data_direction = data_direction; |
| 632 | tv_cmd->tvc_nexus = tv_nexus; |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 633 | tv_cmd->inflight = tcm_vhost_get_inflight(vq); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 634 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 635 | return tv_cmd; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Map a user memory range into a scatterlist |
| 640 | * |
| 641 | * Returns the number of scatterlist entries used or -errno on error. |
| 642 | */ |
| 643 | static int vhost_scsi_map_to_sgl(struct scatterlist *sgl, |
Asias He | 1810053 | 2013-01-22 11:20:27 +0800 | [diff] [blame] | 644 | unsigned int sgl_count, struct iovec *iov, int write) |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 645 | { |
Asias He | 1810053 | 2013-01-22 11:20:27 +0800 | [diff] [blame] | 646 | unsigned int npages = 0, pages_nr, offset, nbytes; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 647 | struct scatterlist *sg = sgl; |
Asias He | 1810053 | 2013-01-22 11:20:27 +0800 | [diff] [blame] | 648 | void __user *ptr = iov->iov_base; |
| 649 | size_t len = iov->iov_len; |
| 650 | struct page **pages; |
| 651 | int ret, i; |
| 652 | |
| 653 | pages_nr = iov_num_pages(iov); |
| 654 | if (pages_nr > sgl_count) |
| 655 | return -ENOBUFS; |
| 656 | |
| 657 | pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL); |
| 658 | if (!pages) |
| 659 | return -ENOMEM; |
| 660 | |
| 661 | ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages); |
| 662 | /* No pages were pinned */ |
| 663 | if (ret < 0) |
| 664 | goto out; |
| 665 | /* Less pages pinned than wanted */ |
| 666 | if (ret != pages_nr) { |
| 667 | for (i = 0; i < ret; i++) |
| 668 | put_page(pages[i]); |
| 669 | ret = -EFAULT; |
| 670 | goto out; |
| 671 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 672 | |
| 673 | while (len > 0) { |
Asias He | 1810053 | 2013-01-22 11:20:27 +0800 | [diff] [blame] | 674 | offset = (uintptr_t)ptr & ~PAGE_MASK; |
| 675 | nbytes = min_t(unsigned int, PAGE_SIZE - offset, len); |
| 676 | sg_set_page(sg, pages[npages], nbytes, offset); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 677 | ptr += nbytes; |
| 678 | len -= nbytes; |
| 679 | sg++; |
| 680 | npages++; |
| 681 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 682 | |
Asias He | 1810053 | 2013-01-22 11:20:27 +0800 | [diff] [blame] | 683 | out: |
| 684 | kfree(pages); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd, |
| 689 | struct iovec *iov, unsigned int niov, int write) |
| 690 | { |
| 691 | int ret; |
| 692 | unsigned int i; |
| 693 | u32 sgl_count; |
| 694 | struct scatterlist *sg; |
| 695 | |
| 696 | /* |
| 697 | * Find out how long sglist needs to be |
| 698 | */ |
| 699 | sgl_count = 0; |
Asias He | f3158f3 | 2013-01-22 11:20:26 +0800 | [diff] [blame] | 700 | for (i = 0; i < niov; i++) |
| 701 | sgl_count += iov_num_pages(&iov[i]); |
| 702 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 703 | /* TODO overflow checking */ |
| 704 | |
| 705 | sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC); |
| 706 | if (!sg) |
| 707 | return -ENOMEM; |
Fengguang Wu | f0e0e9b | 2012-07-30 13:19:07 -0700 | [diff] [blame] | 708 | pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__, |
| 709 | sg, sgl_count, !sg); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 710 | sg_init_table(sg, sgl_count); |
| 711 | |
| 712 | tv_cmd->tvc_sgl = sg; |
| 713 | tv_cmd->tvc_sgl_count = sgl_count; |
| 714 | |
| 715 | pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count); |
| 716 | for (i = 0; i < niov; i++) { |
Asias He | 1810053 | 2013-01-22 11:20:27 +0800 | [diff] [blame] | 717 | ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 718 | if (ret < 0) { |
| 719 | for (i = 0; i < tv_cmd->tvc_sgl_count; i++) |
| 720 | put_page(sg_page(&tv_cmd->tvc_sgl[i])); |
| 721 | kfree(tv_cmd->tvc_sgl); |
| 722 | tv_cmd->tvc_sgl = NULL; |
| 723 | tv_cmd->tvc_sgl_count = 0; |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | sg += ret; |
| 728 | sgl_count -= ret; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static void tcm_vhost_submission_work(struct work_struct *work) |
| 734 | { |
| 735 | struct tcm_vhost_cmd *tv_cmd = |
| 736 | container_of(work, struct tcm_vhost_cmd, work); |
Nicholas Bellinger | 9f0abc1 | 2012-10-01 18:40:55 -0700 | [diff] [blame] | 737 | struct tcm_vhost_nexus *tv_nexus; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 738 | struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; |
| 739 | struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL; |
| 740 | int rc, sg_no_bidi = 0; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 741 | |
| 742 | if (tv_cmd->tvc_sgl_count) { |
| 743 | sg_ptr = tv_cmd->tvc_sgl; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 744 | /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */ |
| 745 | #if 0 |
| 746 | if (se_cmd->se_cmd_flags & SCF_BIDI) { |
| 747 | sg_bidi_ptr = NULL; |
| 748 | sg_no_bidi = 0; |
| 749 | } |
| 750 | #endif |
| 751 | } else { |
| 752 | sg_ptr = NULL; |
| 753 | } |
Nicholas Bellinger | 9f0abc1 | 2012-10-01 18:40:55 -0700 | [diff] [blame] | 754 | tv_nexus = tv_cmd->tvc_nexus; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 755 | |
Nicholas Bellinger | 9f0abc1 | 2012-10-01 18:40:55 -0700 | [diff] [blame] | 756 | rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess, |
| 757 | tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0], |
| 758 | tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len, |
| 759 | tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction, |
| 760 | 0, sg_ptr, tv_cmd->tvc_sgl_count, |
| 761 | sg_bidi_ptr, sg_no_bidi); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 762 | if (rc < 0) { |
| 763 | transport_send_check_condition_and_sense(se_cmd, |
Nicholas Bellinger | 9f0abc1 | 2012-10-01 18:40:55 -0700 | [diff] [blame] | 764 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 765 | transport_generic_free_cmd(se_cmd, 0); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 766 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Asias He | 637ab21 | 2013-04-10 15:06:15 +0800 | [diff] [blame] | 769 | static void vhost_scsi_send_bad_target(struct vhost_scsi *vs, |
| 770 | struct vhost_virtqueue *vq, int head, unsigned out) |
| 771 | { |
| 772 | struct virtio_scsi_cmd_resp __user *resp; |
| 773 | struct virtio_scsi_cmd_resp rsp; |
| 774 | int ret; |
| 775 | |
| 776 | memset(&rsp, 0, sizeof(rsp)); |
| 777 | rsp.response = VIRTIO_SCSI_S_BAD_TARGET; |
| 778 | resp = vq->iov[out].iov_base; |
| 779 | ret = __copy_to_user(resp, &rsp, sizeof(rsp)); |
| 780 | if (!ret) |
| 781 | vhost_add_used_and_signal(&vs->dev, vq, head, 0); |
| 782 | else |
| 783 | pr_err("Faulted on virtio_scsi_cmd_resp\n"); |
| 784 | } |
| 785 | |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 786 | static void vhost_scsi_handle_vq(struct vhost_scsi *vs, |
| 787 | struct vhost_virtqueue *vq) |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 788 | { |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 789 | struct tcm_vhost_tpg **vs_tpg; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 790 | struct virtio_scsi_cmd_req v_req; |
| 791 | struct tcm_vhost_tpg *tv_tpg; |
| 792 | struct tcm_vhost_cmd *tv_cmd; |
| 793 | u32 exp_data_len, data_first, data_num, data_direction; |
| 794 | unsigned out, in, i; |
| 795 | int head, ret; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 796 | u8 target; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 797 | |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 798 | /* |
| 799 | * We can handle the vq only after the endpoint is setup by calling the |
| 800 | * VHOST_SCSI_SET_ENDPOINT ioctl. |
| 801 | * |
| 802 | * TODO: Check that we are running from vhost_worker which acts |
| 803 | * as read-side critical section for vhost kind of RCU. |
| 804 | * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h |
| 805 | */ |
| 806 | vs_tpg = rcu_dereference_check(vq->private_data, 1); |
| 807 | if (!vs_tpg) |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 808 | return; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 809 | |
| 810 | mutex_lock(&vq->mutex); |
| 811 | vhost_disable_notify(&vs->dev, vq); |
| 812 | |
| 813 | for (;;) { |
| 814 | head = vhost_get_vq_desc(&vs->dev, vq, vq->iov, |
| 815 | ARRAY_SIZE(vq->iov), &out, &in, |
| 816 | NULL, NULL); |
| 817 | pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", |
| 818 | head, out, in); |
| 819 | /* On error, stop handling until the next kick. */ |
| 820 | if (unlikely(head < 0)) |
| 821 | break; |
| 822 | /* Nothing new? Wait for eventfd to tell us they refilled. */ |
| 823 | if (head == vq->num) { |
| 824 | if (unlikely(vhost_enable_notify(&vs->dev, vq))) { |
| 825 | vhost_disable_notify(&vs->dev, vq); |
| 826 | continue; |
| 827 | } |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | /* FIXME: BIDI operation */ |
| 832 | if (out == 1 && in == 1) { |
| 833 | data_direction = DMA_NONE; |
| 834 | data_first = 0; |
| 835 | data_num = 0; |
| 836 | } else if (out == 1 && in > 1) { |
| 837 | data_direction = DMA_FROM_DEVICE; |
| 838 | data_first = out + 1; |
| 839 | data_num = in - 1; |
| 840 | } else if (out > 1 && in == 1) { |
| 841 | data_direction = DMA_TO_DEVICE; |
| 842 | data_first = 1; |
| 843 | data_num = out - 1; |
| 844 | } else { |
| 845 | vq_err(vq, "Invalid buffer layout out: %u in: %u\n", |
| 846 | out, in); |
| 847 | break; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Check for a sane resp buffer so we can report errors to |
| 852 | * the guest. |
| 853 | */ |
| 854 | if (unlikely(vq->iov[out].iov_len != |
| 855 | sizeof(struct virtio_scsi_cmd_resp))) { |
| 856 | vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu" |
| 857 | " bytes\n", vq->iov[out].iov_len); |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) { |
| 862 | vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu" |
| 863 | " bytes\n", vq->iov[0].iov_len); |
| 864 | break; |
| 865 | } |
| 866 | pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p," |
| 867 | " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req)); |
| 868 | ret = __copy_from_user(&v_req, vq->iov[0].iov_base, |
| 869 | sizeof(v_req)); |
| 870 | if (unlikely(ret)) { |
| 871 | vq_err(vq, "Faulted on virtio_scsi_cmd_req\n"); |
| 872 | break; |
| 873 | } |
| 874 | |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 875 | /* Extract the tpgt */ |
| 876 | target = v_req.lun[1]; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 877 | tv_tpg = ACCESS_ONCE(vs_tpg[target]); |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 878 | |
| 879 | /* Target does not exist, fail the request */ |
| 880 | if (unlikely(!tv_tpg)) { |
Asias He | 637ab21 | 2013-04-10 15:06:15 +0800 | [diff] [blame] | 881 | vhost_scsi_send_bad_target(vs, vq, head, out); |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 882 | continue; |
| 883 | } |
| 884 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 885 | exp_data_len = 0; |
| 886 | for (i = 0; i < data_num; i++) |
| 887 | exp_data_len += vq->iov[data_first + i].iov_len; |
| 888 | |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 889 | tv_cmd = vhost_scsi_allocate_cmd(vq, tv_tpg, &v_req, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 890 | exp_data_len, data_direction); |
| 891 | if (IS_ERR(tv_cmd)) { |
| 892 | vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n", |
| 893 | PTR_ERR(tv_cmd)); |
Asias He | 055f648 | 2013-04-10 15:06:16 +0800 | [diff] [blame] | 894 | goto err_cmd; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 895 | } |
| 896 | pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction" |
| 897 | ": %d\n", tv_cmd, exp_data_len, data_direction); |
| 898 | |
| 899 | tv_cmd->tvc_vhost = vs; |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 900 | tv_cmd->tvc_vq = vq; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 901 | tv_cmd->tvc_resp = vq->iov[out].iov_base; |
| 902 | |
| 903 | /* |
| 904 | * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb |
| 905 | * that will be used by tcm_vhost_new_cmd_map() and down into |
| 906 | * target_setup_cmd_from_cdb() |
| 907 | */ |
| 908 | memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE); |
| 909 | /* |
| 910 | * Check that the recieved CDB size does not exceeded our |
| 911 | * hardcoded max for tcm_vhost |
| 912 | */ |
| 913 | /* TODO what if cdb was too small for varlen cdb header? */ |
| 914 | if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) > |
| 915 | TCM_VHOST_MAX_CDB_SIZE)) { |
| 916 | vq_err(vq, "Received SCSI CDB with command_size: %d that" |
| 917 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", |
| 918 | scsi_command_size(tv_cmd->tvc_cdb), |
| 919 | TCM_VHOST_MAX_CDB_SIZE); |
Asias He | 055f648 | 2013-04-10 15:06:16 +0800 | [diff] [blame] | 920 | goto err_free; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 921 | } |
| 922 | tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF; |
| 923 | |
| 924 | pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", |
| 925 | tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun); |
| 926 | |
| 927 | if (data_direction != DMA_NONE) { |
| 928 | ret = vhost_scsi_map_iov_to_sgl(tv_cmd, |
| 929 | &vq->iov[data_first], data_num, |
| 930 | data_direction == DMA_TO_DEVICE); |
| 931 | if (unlikely(ret)) { |
| 932 | vq_err(vq, "Failed to map iov to sgl\n"); |
Asias He | 055f648 | 2013-04-10 15:06:16 +0800 | [diff] [blame] | 933 | goto err_free; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Save the descriptor from vhost_get_vq_desc() to be used to |
| 939 | * complete the virtio-scsi request in TCM callback context via |
| 940 | * tcm_vhost_queue_data_in() and tcm_vhost_queue_status() |
| 941 | */ |
| 942 | tv_cmd->tvc_vq_desc = head; |
| 943 | /* |
| 944 | * Dispatch tv_cmd descriptor for cmwq execution in process |
| 945 | * context provided by tcm_vhost_workqueue. This also ensures |
| 946 | * tv_cmd is executed on the same kworker CPU as this vhost |
| 947 | * thread to gain positive L2 cache locality effects.. |
| 948 | */ |
| 949 | INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work); |
| 950 | queue_work(tcm_vhost_workqueue, &tv_cmd->work); |
| 951 | } |
| 952 | |
| 953 | mutex_unlock(&vq->mutex); |
Asias He | 7ea206c | 2013-04-10 15:06:14 +0800 | [diff] [blame] | 954 | return; |
| 955 | |
Asias He | 055f648 | 2013-04-10 15:06:16 +0800 | [diff] [blame] | 956 | err_free: |
Asias He | 7ea206c | 2013-04-10 15:06:14 +0800 | [diff] [blame] | 957 | vhost_scsi_free_cmd(tv_cmd); |
Asias He | 055f648 | 2013-04-10 15:06:16 +0800 | [diff] [blame] | 958 | err_cmd: |
| 959 | vhost_scsi_send_bad_target(vs, vq, head, out); |
Asias He | 7ea206c | 2013-04-10 15:06:14 +0800 | [diff] [blame] | 960 | mutex_unlock(&vq->mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static void vhost_scsi_ctl_handle_kick(struct vhost_work *work) |
| 964 | { |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 965 | pr_debug("%s: The handling func for control queue.\n", __func__); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 968 | static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg, |
| 969 | struct se_lun *lun, u32 event, u32 reason) |
| 970 | { |
| 971 | struct tcm_vhost_evt *evt; |
| 972 | |
| 973 | evt = tcm_vhost_allocate_evt(vs, event, reason); |
| 974 | if (!evt) |
| 975 | return; |
| 976 | |
| 977 | if (tpg && lun) { |
| 978 | /* TODO: share lun setup code with virtio-scsi.ko */ |
| 979 | /* |
| 980 | * Note: evt->event is zeroed when we allocate it and |
| 981 | * lun[4-7] need to be zero according to virtio-scsi spec. |
| 982 | */ |
| 983 | evt->event.lun[0] = 0x01; |
| 984 | evt->event.lun[1] = tpg->tport_tpgt & 0xFF; |
| 985 | if (lun->unpacked_lun >= 256) |
| 986 | evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ; |
| 987 | evt->event.lun[3] = lun->unpacked_lun & 0xFF; |
| 988 | } |
| 989 | |
| 990 | llist_add(&evt->list, &vs->vs_event_list); |
| 991 | vhost_work_queue(&vs->dev, &vs->vs_event_work); |
| 992 | } |
| 993 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 994 | static void vhost_scsi_evt_handle_kick(struct vhost_work *work) |
| 995 | { |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 996 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
| 997 | poll.work); |
| 998 | struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); |
| 999 | |
| 1000 | mutex_lock(&vq->mutex); |
| 1001 | if (!vq->private_data) |
| 1002 | goto out; |
| 1003 | |
| 1004 | if (vs->vs_events_missed) |
| 1005 | tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); |
| 1006 | out: |
| 1007 | mutex_unlock(&vq->mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | static void vhost_scsi_handle_kick(struct vhost_work *work) |
| 1011 | { |
| 1012 | struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, |
| 1013 | poll.work); |
| 1014 | struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev); |
| 1015 | |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 1016 | vhost_scsi_handle_vq(vs, vq); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1019 | static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index) |
| 1020 | { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1021 | vhost_poll_flush(&vs->vqs[index].vq.poll); |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1022 | } |
| 1023 | |
Michael S. Tsirkin | 3dfbff3 | 2013-04-28 15:38:52 +0300 | [diff] [blame] | 1024 | /* Callers must hold dev mutex */ |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1025 | static void vhost_scsi_flush(struct vhost_scsi *vs) |
| 1026 | { |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 1027 | struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ]; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1028 | int i; |
| 1029 | |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 1030 | /* Init new inflight and remember the old inflight */ |
| 1031 | tcm_vhost_init_inflight(vs, old_inflight); |
| 1032 | |
| 1033 | /* |
| 1034 | * The inflight->kref was initialized to 1. We decrement it here to |
| 1035 | * indicate the start of the flush operation so that it will reach 0 |
| 1036 | * when all the reqs are finished. |
| 1037 | */ |
| 1038 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) |
| 1039 | kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight); |
| 1040 | |
| 1041 | /* Flush both the vhost poll and vhost work */ |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1042 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) |
| 1043 | vhost_scsi_flush_vq(vs, i); |
| 1044 | vhost_work_flush(&vs->dev, &vs->vs_completion_work); |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1045 | vhost_work_flush(&vs->dev, &vs->vs_event_work); |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 1046 | |
| 1047 | /* Wait for all reqs issued before the flush to be finished */ |
| 1048 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) |
| 1049 | wait_for_completion(&old_inflight[i]->comp); |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1052 | /* |
| 1053 | * Called from vhost_scsi_ioctl() context to walk the list of available |
| 1054 | * tcm_vhost_tpg with an active struct tcm_vhost_nexus |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1055 | * |
| 1056 | * The lock nesting rule is: |
| 1057 | * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1058 | */ |
| 1059 | static int vhost_scsi_set_endpoint( |
| 1060 | struct vhost_scsi *vs, |
| 1061 | struct vhost_scsi_target *t) |
| 1062 | { |
| 1063 | struct tcm_vhost_tport *tv_tport; |
| 1064 | struct tcm_vhost_tpg *tv_tpg; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1065 | struct tcm_vhost_tpg **vs_tpg; |
| 1066 | struct vhost_virtqueue *vq; |
| 1067 | int index, ret, i, len; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1068 | bool match = false; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1069 | |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1070 | mutex_lock(&tcm_vhost_mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1071 | mutex_lock(&vs->dev.mutex); |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1072 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1073 | /* Verify that ring has been setup correctly. */ |
| 1074 | for (index = 0; index < vs->dev.nvqs; ++index) { |
| 1075 | /* Verify that ring has been setup correctly. */ |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1076 | if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1077 | ret = -EFAULT; |
| 1078 | goto out; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1081 | |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1082 | len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET; |
| 1083 | vs_tpg = kzalloc(len, GFP_KERNEL); |
| 1084 | if (!vs_tpg) { |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1085 | ret = -ENOMEM; |
| 1086 | goto out; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1087 | } |
| 1088 | if (vs->vs_tpg) |
| 1089 | memcpy(vs_tpg, vs->vs_tpg, len); |
| 1090 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1091 | list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) { |
| 1092 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
| 1093 | if (!tv_tpg->tpg_nexus) { |
| 1094 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1095 | continue; |
| 1096 | } |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1097 | if (tv_tpg->tv_tpg_vhost_count != 0) { |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1098 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1099 | continue; |
| 1100 | } |
| 1101 | tv_tport = tv_tpg->tport; |
| 1102 | |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1103 | if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) { |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1104 | if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) { |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1105 | kfree(vs_tpg); |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1106 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1107 | ret = -EEXIST; |
| 1108 | goto out; |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1109 | } |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1110 | tv_tpg->tv_tpg_vhost_count++; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1111 | tv_tpg->vhost_scsi = vs; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1112 | vs_tpg[tv_tpg->tport_tpgt] = tv_tpg; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1113 | smp_mb__after_atomic_inc(); |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1114 | match = true; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1115 | } |
| 1116 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1117 | } |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1118 | |
| 1119 | if (match) { |
| 1120 | memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn, |
| 1121 | sizeof(vs->vs_vhost_wwpn)); |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1122 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1123 | vq = &vs->vqs[i].vq; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1124 | /* Flushing the vhost_work acts as synchronize_rcu */ |
| 1125 | mutex_lock(&vq->mutex); |
| 1126 | rcu_assign_pointer(vq->private_data, vs_tpg); |
Asias He | dfd5d56 | 2013-04-03 14:17:38 +0800 | [diff] [blame] | 1127 | vhost_init_used(vq); |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1128 | mutex_unlock(&vq->mutex); |
| 1129 | } |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1130 | ret = 0; |
| 1131 | } else { |
| 1132 | ret = -EEXIST; |
| 1133 | } |
| 1134 | |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1135 | /* |
| 1136 | * Act as synchronize_rcu to make sure access to |
| 1137 | * old vs->vs_tpg is finished. |
| 1138 | */ |
| 1139 | vhost_scsi_flush(vs); |
| 1140 | kfree(vs->vs_tpg); |
| 1141 | vs->vs_tpg = vs_tpg; |
| 1142 | |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1143 | out: |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1144 | mutex_unlock(&vs->dev.mutex); |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1145 | mutex_unlock(&tcm_vhost_mutex); |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1146 | return ret; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | static int vhost_scsi_clear_endpoint( |
| 1150 | struct vhost_scsi *vs, |
| 1151 | struct vhost_scsi_target *t) |
| 1152 | { |
| 1153 | struct tcm_vhost_tport *tv_tport; |
| 1154 | struct tcm_vhost_tpg *tv_tpg; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1155 | struct vhost_virtqueue *vq; |
| 1156 | bool match = false; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1157 | int index, ret, i; |
| 1158 | u8 target; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1159 | |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1160 | mutex_lock(&tcm_vhost_mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1161 | mutex_lock(&vs->dev.mutex); |
| 1162 | /* Verify that ring has been setup correctly. */ |
| 1163 | for (index = 0; index < vs->dev.nvqs; ++index) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1164 | if (!vhost_vq_access_ok(&vs->vqs[index].vq)) { |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1165 | ret = -EFAULT; |
Asias He | 038e0af | 2013-03-15 09:14:05 +0800 | [diff] [blame] | 1166 | goto err_dev; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1169 | |
| 1170 | if (!vs->vs_tpg) { |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1171 | ret = 0; |
| 1172 | goto err_dev; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1175 | for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { |
| 1176 | target = i; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1177 | tv_tpg = vs->vs_tpg[target]; |
| 1178 | if (!tv_tpg) |
| 1179 | continue; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1180 | |
Asias He | 038e0af | 2013-03-15 09:14:05 +0800 | [diff] [blame] | 1181 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1182 | tv_tport = tv_tpg->tport; |
| 1183 | if (!tv_tport) { |
| 1184 | ret = -ENODEV; |
Asias He | 038e0af | 2013-03-15 09:14:05 +0800 | [diff] [blame] | 1185 | goto err_tpg; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) { |
| 1189 | pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu" |
| 1190 | " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n", |
| 1191 | tv_tport->tport_name, tv_tpg->tport_tpgt, |
| 1192 | t->vhost_wwpn, t->vhost_tpgt); |
| 1193 | ret = -EINVAL; |
Asias He | 038e0af | 2013-03-15 09:14:05 +0800 | [diff] [blame] | 1194 | goto err_tpg; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1195 | } |
| 1196 | tv_tpg->tv_tpg_vhost_count--; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1197 | tv_tpg->vhost_scsi = NULL; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1198 | vs->vs_tpg[target] = NULL; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1199 | match = true; |
Asias He | 038e0af | 2013-03-15 09:14:05 +0800 | [diff] [blame] | 1200 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1201 | } |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1202 | if (match) { |
| 1203 | for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1204 | vq = &vs->vqs[i].vq; |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1205 | /* Flushing the vhost_work acts as synchronize_rcu */ |
| 1206 | mutex_lock(&vq->mutex); |
| 1207 | rcu_assign_pointer(vq->private_data, NULL); |
| 1208 | mutex_unlock(&vq->mutex); |
| 1209 | } |
| 1210 | } |
| 1211 | /* |
| 1212 | * Act as synchronize_rcu to make sure access to |
| 1213 | * old vs->vs_tpg is finished. |
| 1214 | */ |
| 1215 | vhost_scsi_flush(vs); |
| 1216 | kfree(vs->vs_tpg); |
| 1217 | vs->vs_tpg = NULL; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1218 | WARN_ON(vs->vs_events_nr); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1219 | mutex_unlock(&vs->dev.mutex); |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1220 | mutex_unlock(&tcm_vhost_mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1221 | return 0; |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1222 | |
Asias He | 038e0af | 2013-03-15 09:14:05 +0800 | [diff] [blame] | 1223 | err_tpg: |
| 1224 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1225 | err_dev: |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1226 | mutex_unlock(&vs->dev.mutex); |
Asias He | f2b7daf | 2013-04-25 15:35:20 +0800 | [diff] [blame] | 1227 | mutex_unlock(&tcm_vhost_mutex); |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1228 | return ret; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Asias He | 4f7f46d | 2013-04-03 14:17:37 +0800 | [diff] [blame] | 1231 | static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features) |
| 1232 | { |
| 1233 | if (features & ~VHOST_SCSI_FEATURES) |
| 1234 | return -EOPNOTSUPP; |
| 1235 | |
| 1236 | mutex_lock(&vs->dev.mutex); |
| 1237 | if ((features & (1 << VHOST_F_LOG_ALL)) && |
| 1238 | !vhost_log_access_ok(&vs->dev)) { |
| 1239 | mutex_unlock(&vs->dev.mutex); |
| 1240 | return -EFAULT; |
| 1241 | } |
| 1242 | vs->dev.acked_features = features; |
| 1243 | smp_wmb(); |
| 1244 | vhost_scsi_flush(vs); |
| 1245 | mutex_unlock(&vs->dev.mutex); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1249 | static int vhost_scsi_open(struct inode *inode, struct file *f) |
| 1250 | { |
| 1251 | struct vhost_scsi *s; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1252 | struct vhost_virtqueue **vqs; |
Asias He | 1b7f390 | 2013-02-06 13:20:59 +0800 | [diff] [blame] | 1253 | int r, i; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1254 | |
| 1255 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
| 1256 | if (!s) |
| 1257 | return -ENOMEM; |
| 1258 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1259 | vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL); |
| 1260 | if (!vqs) { |
| 1261 | kfree(s); |
| 1262 | return -ENOMEM; |
| 1263 | } |
| 1264 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1265 | vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work); |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1266 | vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work); |
| 1267 | |
| 1268 | s->vs_events_nr = 0; |
| 1269 | s->vs_events_missed = false; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1270 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1271 | vqs[VHOST_SCSI_VQ_CTL] = &s->vqs[VHOST_SCSI_VQ_CTL].vq; |
| 1272 | vqs[VHOST_SCSI_VQ_EVT] = &s->vqs[VHOST_SCSI_VQ_EVT].vq; |
| 1273 | s->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick; |
| 1274 | s->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick; |
| 1275 | for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) { |
| 1276 | vqs[i] = &s->vqs[i].vq; |
| 1277 | s->vqs[i].vq.handle_kick = vhost_scsi_handle_kick; |
| 1278 | } |
| 1279 | r = vhost_dev_init(&s->dev, vqs, VHOST_SCSI_MAX_VQ); |
Asias He | f2f0173d | 2013-04-27 11:16:49 +0800 | [diff] [blame] | 1280 | |
| 1281 | tcm_vhost_init_inflight(s, NULL); |
| 1282 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1283 | if (r < 0) { |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1284 | kfree(vqs); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1285 | kfree(s); |
| 1286 | return r; |
| 1287 | } |
| 1288 | |
| 1289 | f->private_data = s; |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | static int vhost_scsi_release(struct inode *inode, struct file *f) |
| 1294 | { |
| 1295 | struct vhost_scsi *s = f->private_data; |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1296 | struct vhost_scsi_target t; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1297 | |
Asias He | 67e18cf | 2013-02-05 12:31:57 +0800 | [diff] [blame] | 1298 | mutex_lock(&s->dev.mutex); |
| 1299 | memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn)); |
| 1300 | mutex_unlock(&s->dev.mutex); |
| 1301 | vhost_scsi_clear_endpoint(s, &t); |
Michael S. Tsirkin | b211616 | 2012-11-01 09:16:46 +0000 | [diff] [blame] | 1302 | vhost_dev_stop(&s->dev); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1303 | vhost_dev_cleanup(&s->dev, false); |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1304 | /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */ |
| 1305 | vhost_scsi_flush(s); |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1306 | kfree(s->dev.vqs); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1307 | kfree(s); |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1311 | static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl, |
| 1312 | unsigned long arg) |
| 1313 | { |
| 1314 | struct vhost_scsi *vs = f->private_data; |
| 1315 | struct vhost_scsi_target backend; |
| 1316 | void __user *argp = (void __user *)arg; |
| 1317 | u64 __user *featurep = argp; |
Asias He | 11c6341 | 2013-04-25 15:35:22 +0800 | [diff] [blame] | 1318 | u32 __user *eventsp = argp; |
| 1319 | u32 events_missed; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1320 | u64 features; |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1321 | int r, abi_version = VHOST_SCSI_ABI_VERSION; |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1322 | struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1323 | |
| 1324 | switch (ioctl) { |
| 1325 | case VHOST_SCSI_SET_ENDPOINT: |
| 1326 | if (copy_from_user(&backend, argp, sizeof backend)) |
| 1327 | return -EFAULT; |
Michael S. Tsirkin | 6de7145 | 2012-08-18 15:44:09 -0700 | [diff] [blame] | 1328 | if (backend.reserved != 0) |
| 1329 | return -EOPNOTSUPP; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1330 | |
| 1331 | return vhost_scsi_set_endpoint(vs, &backend); |
| 1332 | case VHOST_SCSI_CLEAR_ENDPOINT: |
| 1333 | if (copy_from_user(&backend, argp, sizeof backend)) |
| 1334 | return -EFAULT; |
Michael S. Tsirkin | 6de7145 | 2012-08-18 15:44:09 -0700 | [diff] [blame] | 1335 | if (backend.reserved != 0) |
| 1336 | return -EOPNOTSUPP; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1337 | |
| 1338 | return vhost_scsi_clear_endpoint(vs, &backend); |
| 1339 | case VHOST_SCSI_GET_ABI_VERSION: |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1340 | if (copy_to_user(argp, &abi_version, sizeof abi_version)) |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1341 | return -EFAULT; |
| 1342 | return 0; |
Asias He | 11c6341 | 2013-04-25 15:35:22 +0800 | [diff] [blame] | 1343 | case VHOST_SCSI_SET_EVENTS_MISSED: |
| 1344 | if (get_user(events_missed, eventsp)) |
| 1345 | return -EFAULT; |
| 1346 | mutex_lock(&vq->mutex); |
| 1347 | vs->vs_events_missed = events_missed; |
| 1348 | mutex_unlock(&vq->mutex); |
| 1349 | return 0; |
| 1350 | case VHOST_SCSI_GET_EVENTS_MISSED: |
| 1351 | mutex_lock(&vq->mutex); |
| 1352 | events_missed = vs->vs_events_missed; |
| 1353 | mutex_unlock(&vq->mutex); |
| 1354 | if (put_user(events_missed, eventsp)) |
| 1355 | return -EFAULT; |
| 1356 | return 0; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1357 | case VHOST_GET_FEATURES: |
Nicholas Bellinger | 5dade71 | 2013-03-27 17:23:41 -0700 | [diff] [blame] | 1358 | features = VHOST_SCSI_FEATURES; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1359 | if (copy_to_user(featurep, &features, sizeof features)) |
| 1360 | return -EFAULT; |
| 1361 | return 0; |
| 1362 | case VHOST_SET_FEATURES: |
| 1363 | if (copy_from_user(&features, featurep, sizeof features)) |
| 1364 | return -EFAULT; |
| 1365 | return vhost_scsi_set_features(vs, features); |
| 1366 | default: |
| 1367 | mutex_lock(&vs->dev.mutex); |
Michael S. Tsirkin | 935cdee | 2012-12-06 14:03:34 +0200 | [diff] [blame] | 1368 | r = vhost_dev_ioctl(&vs->dev, ioctl, argp); |
| 1369 | /* TODO: flush backend after dev ioctl. */ |
| 1370 | if (r == -ENOIOCTLCMD) |
| 1371 | r = vhost_vring_ioctl(&vs->dev, ioctl, argp); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1372 | mutex_unlock(&vs->dev.mutex); |
| 1373 | return r; |
| 1374 | } |
| 1375 | } |
| 1376 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1377 | #ifdef CONFIG_COMPAT |
| 1378 | static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl, |
| 1379 | unsigned long arg) |
| 1380 | { |
| 1381 | return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg)); |
| 1382 | } |
| 1383 | #endif |
| 1384 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1385 | static const struct file_operations vhost_scsi_fops = { |
| 1386 | .owner = THIS_MODULE, |
| 1387 | .release = vhost_scsi_release, |
| 1388 | .unlocked_ioctl = vhost_scsi_ioctl, |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1389 | #ifdef CONFIG_COMPAT |
| 1390 | .compat_ioctl = vhost_scsi_compat_ioctl, |
| 1391 | #endif |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1392 | .open = vhost_scsi_open, |
| 1393 | .llseek = noop_llseek, |
| 1394 | }; |
| 1395 | |
| 1396 | static struct miscdevice vhost_scsi_misc = { |
| 1397 | MISC_DYNAMIC_MINOR, |
| 1398 | "vhost-scsi", |
| 1399 | &vhost_scsi_fops, |
| 1400 | }; |
| 1401 | |
| 1402 | static int __init vhost_scsi_register(void) |
| 1403 | { |
| 1404 | return misc_register(&vhost_scsi_misc); |
| 1405 | } |
| 1406 | |
| 1407 | static int vhost_scsi_deregister(void) |
| 1408 | { |
| 1409 | return misc_deregister(&vhost_scsi_misc); |
| 1410 | } |
| 1411 | |
| 1412 | static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport) |
| 1413 | { |
| 1414 | switch (tport->tport_proto_id) { |
| 1415 | case SCSI_PROTOCOL_SAS: |
| 1416 | return "SAS"; |
| 1417 | case SCSI_PROTOCOL_FCP: |
| 1418 | return "FCP"; |
| 1419 | case SCSI_PROTOCOL_ISCSI: |
| 1420 | return "iSCSI"; |
| 1421 | default: |
| 1422 | break; |
| 1423 | } |
| 1424 | |
| 1425 | return "Unknown"; |
| 1426 | } |
| 1427 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1428 | static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg, |
| 1429 | struct se_lun *lun, bool plug) |
| 1430 | { |
| 1431 | |
| 1432 | struct vhost_scsi *vs = tpg->vhost_scsi; |
| 1433 | struct vhost_virtqueue *vq; |
| 1434 | u32 reason; |
| 1435 | |
| 1436 | if (!vs) |
| 1437 | return; |
| 1438 | |
| 1439 | mutex_lock(&vs->dev.mutex); |
| 1440 | if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) { |
| 1441 | mutex_unlock(&vs->dev.mutex); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | if (plug) |
| 1446 | reason = VIRTIO_SCSI_EVT_RESET_RESCAN; |
| 1447 | else |
| 1448 | reason = VIRTIO_SCSI_EVT_RESET_REMOVED; |
| 1449 | |
Asias He | 3ab2e42 | 2013-04-27 11:16:48 +0800 | [diff] [blame] | 1450 | vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1451 | mutex_lock(&vq->mutex); |
| 1452 | tcm_vhost_send_evt(vs, tpg, lun, |
| 1453 | VIRTIO_SCSI_T_TRANSPORT_RESET, reason); |
| 1454 | mutex_unlock(&vq->mutex); |
| 1455 | mutex_unlock(&vs->dev.mutex); |
| 1456 | } |
| 1457 | |
| 1458 | static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun) |
| 1459 | { |
| 1460 | tcm_vhost_do_plug(tpg, lun, true); |
| 1461 | } |
| 1462 | |
| 1463 | static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun) |
| 1464 | { |
| 1465 | tcm_vhost_do_plug(tpg, lun, false); |
| 1466 | } |
| 1467 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1468 | static int tcm_vhost_port_link(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1469 | struct se_lun *lun) |
| 1470 | { |
| 1471 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, |
| 1472 | struct tcm_vhost_tpg, se_tpg); |
| 1473 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1474 | mutex_lock(&tcm_vhost_mutex); |
| 1475 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1476 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
| 1477 | tv_tpg->tv_tpg_port_count++; |
| 1478 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1479 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1480 | tcm_vhost_hotplug(tv_tpg, lun); |
| 1481 | |
| 1482 | mutex_unlock(&tcm_vhost_mutex); |
| 1483 | |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1487 | static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg, |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1488 | struct se_lun *lun) |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1489 | { |
| 1490 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, |
| 1491 | struct tcm_vhost_tpg, se_tpg); |
| 1492 | |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1493 | mutex_lock(&tcm_vhost_mutex); |
| 1494 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1495 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
| 1496 | tv_tpg->tv_tpg_port_count--; |
| 1497 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
Asias He | a6c9af8 | 2013-04-25 15:35:21 +0800 | [diff] [blame] | 1498 | |
| 1499 | tcm_vhost_hotunplug(tv_tpg, lun); |
| 1500 | |
| 1501 | mutex_unlock(&tcm_vhost_mutex); |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | static struct se_node_acl *tcm_vhost_make_nodeacl( |
| 1505 | struct se_portal_group *se_tpg, |
| 1506 | struct config_group *group, |
| 1507 | const char *name) |
| 1508 | { |
| 1509 | struct se_node_acl *se_nacl, *se_nacl_new; |
| 1510 | struct tcm_vhost_nacl *nacl; |
| 1511 | u64 wwpn = 0; |
| 1512 | u32 nexus_depth; |
| 1513 | |
| 1514 | /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0) |
| 1515 | return ERR_PTR(-EINVAL); */ |
| 1516 | se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg); |
| 1517 | if (!se_nacl_new) |
| 1518 | return ERR_PTR(-ENOMEM); |
| 1519 | |
| 1520 | nexus_depth = 1; |
| 1521 | /* |
| 1522 | * se_nacl_new may be released by core_tpg_add_initiator_node_acl() |
| 1523 | * when converting a NodeACL from demo mode -> explict |
| 1524 | */ |
| 1525 | se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new, |
| 1526 | name, nexus_depth); |
| 1527 | if (IS_ERR(se_nacl)) { |
| 1528 | tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new); |
| 1529 | return se_nacl; |
| 1530 | } |
| 1531 | /* |
| 1532 | * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN |
| 1533 | */ |
| 1534 | nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl); |
| 1535 | nacl->iport_wwpn = wwpn; |
| 1536 | |
| 1537 | return se_nacl; |
| 1538 | } |
| 1539 | |
| 1540 | static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl) |
| 1541 | { |
| 1542 | struct tcm_vhost_nacl *nacl = container_of(se_acl, |
| 1543 | struct tcm_vhost_nacl, se_node_acl); |
| 1544 | core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1); |
| 1545 | kfree(nacl); |
| 1546 | } |
| 1547 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1548 | static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1549 | const char *name) |
| 1550 | { |
| 1551 | struct se_portal_group *se_tpg; |
| 1552 | struct tcm_vhost_nexus *tv_nexus; |
| 1553 | |
| 1554 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
| 1555 | if (tv_tpg->tpg_nexus) { |
| 1556 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1557 | pr_debug("tv_tpg->tpg_nexus already exists\n"); |
| 1558 | return -EEXIST; |
| 1559 | } |
| 1560 | se_tpg = &tv_tpg->se_tpg; |
| 1561 | |
| 1562 | tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL); |
| 1563 | if (!tv_nexus) { |
| 1564 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1565 | pr_err("Unable to allocate struct tcm_vhost_nexus\n"); |
| 1566 | return -ENOMEM; |
| 1567 | } |
| 1568 | /* |
| 1569 | * Initialize the struct se_session pointer |
| 1570 | */ |
| 1571 | tv_nexus->tvn_se_sess = transport_init_session(); |
| 1572 | if (IS_ERR(tv_nexus->tvn_se_sess)) { |
| 1573 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1574 | kfree(tv_nexus); |
| 1575 | return -ENOMEM; |
| 1576 | } |
| 1577 | /* |
| 1578 | * Since we are running in 'demo mode' this call with generate a |
| 1579 | * struct se_node_acl for the tcm_vhost struct se_portal_group with |
| 1580 | * the SCSI Initiator port name of the passed configfs group 'name'. |
| 1581 | */ |
| 1582 | tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( |
| 1583 | se_tpg, (unsigned char *)name); |
| 1584 | if (!tv_nexus->tvn_se_sess->se_node_acl) { |
| 1585 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1586 | pr_debug("core_tpg_check_initiator_node_acl() failed" |
| 1587 | " for %s\n", name); |
| 1588 | transport_free_session(tv_nexus->tvn_se_sess); |
| 1589 | kfree(tv_nexus); |
| 1590 | return -ENOMEM; |
| 1591 | } |
| 1592 | /* |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1593 | * Now register the TCM vhost virtual I_T Nexus as active with the |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1594 | * call to __transport_register_session() |
| 1595 | */ |
| 1596 | __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, |
| 1597 | tv_nexus->tvn_se_sess, tv_nexus); |
| 1598 | tv_tpg->tpg_nexus = tv_nexus; |
| 1599 | |
| 1600 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1604 | static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg) |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1605 | { |
| 1606 | struct se_session *se_sess; |
| 1607 | struct tcm_vhost_nexus *tv_nexus; |
| 1608 | |
| 1609 | mutex_lock(&tpg->tv_tpg_mutex); |
| 1610 | tv_nexus = tpg->tpg_nexus; |
| 1611 | if (!tv_nexus) { |
| 1612 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1613 | return -ENODEV; |
| 1614 | } |
| 1615 | |
| 1616 | se_sess = tv_nexus->tvn_se_sess; |
| 1617 | if (!se_sess) { |
| 1618 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1619 | return -ENODEV; |
| 1620 | } |
| 1621 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1622 | if (tpg->tv_tpg_port_count != 0) { |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1623 | mutex_unlock(&tpg->tv_tpg_mutex); |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1624 | pr_err("Unable to remove TCM_vhost I_T Nexus with" |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1625 | " active TPG port count: %d\n", |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1626 | tpg->tv_tpg_port_count); |
| 1627 | return -EBUSY; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1630 | if (tpg->tv_tpg_vhost_count != 0) { |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1631 | mutex_unlock(&tpg->tv_tpg_mutex); |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1632 | pr_err("Unable to remove TCM_vhost I_T Nexus with" |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1633 | " active TPG vhost count: %d\n", |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1634 | tpg->tv_tpg_vhost_count); |
| 1635 | return -EBUSY; |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1638 | pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated" |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1639 | " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport), |
| 1640 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); |
| 1641 | /* |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1642 | * Release the SCSI I_T Nexus to the emulated vhost Target Port |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1643 | */ |
| 1644 | transport_deregister_session(tv_nexus->tvn_se_sess); |
| 1645 | tpg->tpg_nexus = NULL; |
| 1646 | mutex_unlock(&tpg->tv_tpg_mutex); |
| 1647 | |
| 1648 | kfree(tv_nexus); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1652 | static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1653 | char *page) |
| 1654 | { |
| 1655 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, |
| 1656 | struct tcm_vhost_tpg, se_tpg); |
| 1657 | struct tcm_vhost_nexus *tv_nexus; |
| 1658 | ssize_t ret; |
| 1659 | |
| 1660 | mutex_lock(&tv_tpg->tv_tpg_mutex); |
| 1661 | tv_nexus = tv_tpg->tpg_nexus; |
| 1662 | if (!tv_nexus) { |
| 1663 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1664 | return -ENODEV; |
| 1665 | } |
| 1666 | ret = snprintf(page, PAGE_SIZE, "%s\n", |
| 1667 | tv_nexus->tvn_se_sess->se_node_acl->initiatorname); |
| 1668 | mutex_unlock(&tv_tpg->tv_tpg_mutex); |
| 1669 | |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1673 | static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1674 | const char *page, |
| 1675 | size_t count) |
| 1676 | { |
| 1677 | struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, |
| 1678 | struct tcm_vhost_tpg, se_tpg); |
| 1679 | struct tcm_vhost_tport *tport_wwn = tv_tpg->tport; |
| 1680 | unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr; |
| 1681 | int ret; |
| 1682 | /* |
| 1683 | * Shutdown the active I_T nexus if 'NULL' is passed.. |
| 1684 | */ |
| 1685 | if (!strncmp(page, "NULL", 4)) { |
| 1686 | ret = tcm_vhost_drop_nexus(tv_tpg); |
| 1687 | return (!ret) ? count : ret; |
| 1688 | } |
| 1689 | /* |
| 1690 | * Otherwise make sure the passed virtual Initiator port WWN matches |
| 1691 | * the fabric protocol_id set in tcm_vhost_make_tport(), and call |
| 1692 | * tcm_vhost_make_nexus(). |
| 1693 | */ |
| 1694 | if (strlen(page) >= TCM_VHOST_NAMELEN) { |
| 1695 | pr_err("Emulated NAA Sas Address: %s, exceeds" |
| 1696 | " max: %d\n", page, TCM_VHOST_NAMELEN); |
| 1697 | return -EINVAL; |
| 1698 | } |
| 1699 | snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page); |
| 1700 | |
| 1701 | ptr = strstr(i_port, "naa."); |
| 1702 | if (ptr) { |
| 1703 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) { |
| 1704 | pr_err("Passed SAS Initiator Port %s does not" |
| 1705 | " match target port protoid: %s\n", i_port, |
| 1706 | tcm_vhost_dump_proto_id(tport_wwn)); |
| 1707 | return -EINVAL; |
| 1708 | } |
| 1709 | port_ptr = &i_port[0]; |
| 1710 | goto check_newline; |
| 1711 | } |
| 1712 | ptr = strstr(i_port, "fc."); |
| 1713 | if (ptr) { |
| 1714 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) { |
| 1715 | pr_err("Passed FCP Initiator Port %s does not" |
| 1716 | " match target port protoid: %s\n", i_port, |
| 1717 | tcm_vhost_dump_proto_id(tport_wwn)); |
| 1718 | return -EINVAL; |
| 1719 | } |
| 1720 | port_ptr = &i_port[3]; /* Skip over "fc." */ |
| 1721 | goto check_newline; |
| 1722 | } |
| 1723 | ptr = strstr(i_port, "iqn."); |
| 1724 | if (ptr) { |
| 1725 | if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) { |
| 1726 | pr_err("Passed iSCSI Initiator Port %s does not" |
| 1727 | " match target port protoid: %s\n", i_port, |
| 1728 | tcm_vhost_dump_proto_id(tport_wwn)); |
| 1729 | return -EINVAL; |
| 1730 | } |
| 1731 | port_ptr = &i_port[0]; |
| 1732 | goto check_newline; |
| 1733 | } |
| 1734 | pr_err("Unable to locate prefix for emulated Initiator Port:" |
| 1735 | " %s\n", i_port); |
| 1736 | return -EINVAL; |
| 1737 | /* |
| 1738 | * Clear any trailing newline for the NAA WWN |
| 1739 | */ |
| 1740 | check_newline: |
| 1741 | if (i_port[strlen(i_port)-1] == '\n') |
| 1742 | i_port[strlen(i_port)-1] = '\0'; |
| 1743 | |
| 1744 | ret = tcm_vhost_make_nexus(tv_tpg, port_ptr); |
| 1745 | if (ret < 0) |
| 1746 | return ret; |
| 1747 | |
| 1748 | return count; |
| 1749 | } |
| 1750 | |
| 1751 | TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR); |
| 1752 | |
| 1753 | static struct configfs_attribute *tcm_vhost_tpg_attrs[] = { |
| 1754 | &tcm_vhost_tpg_nexus.attr, |
| 1755 | NULL, |
| 1756 | }; |
| 1757 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1758 | static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1759 | struct config_group *group, |
| 1760 | const char *name) |
| 1761 | { |
| 1762 | struct tcm_vhost_tport *tport = container_of(wwn, |
| 1763 | struct tcm_vhost_tport, tport_wwn); |
| 1764 | |
| 1765 | struct tcm_vhost_tpg *tpg; |
| 1766 | unsigned long tpgt; |
| 1767 | int ret; |
| 1768 | |
| 1769 | if (strstr(name, "tpgt_") != name) |
| 1770 | return ERR_PTR(-EINVAL); |
| 1771 | if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX) |
| 1772 | return ERR_PTR(-EINVAL); |
| 1773 | |
| 1774 | tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL); |
| 1775 | if (!tpg) { |
| 1776 | pr_err("Unable to allocate struct tcm_vhost_tpg"); |
| 1777 | return ERR_PTR(-ENOMEM); |
| 1778 | } |
| 1779 | mutex_init(&tpg->tv_tpg_mutex); |
| 1780 | INIT_LIST_HEAD(&tpg->tv_tpg_list); |
| 1781 | tpg->tport = tport; |
| 1782 | tpg->tport_tpgt = tpgt; |
| 1783 | |
| 1784 | ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn, |
| 1785 | &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL); |
| 1786 | if (ret < 0) { |
| 1787 | kfree(tpg); |
| 1788 | return NULL; |
| 1789 | } |
| 1790 | mutex_lock(&tcm_vhost_mutex); |
| 1791 | list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list); |
| 1792 | mutex_unlock(&tcm_vhost_mutex); |
| 1793 | |
| 1794 | return &tpg->se_tpg; |
| 1795 | } |
| 1796 | |
| 1797 | static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg) |
| 1798 | { |
| 1799 | struct tcm_vhost_tpg *tpg = container_of(se_tpg, |
| 1800 | struct tcm_vhost_tpg, se_tpg); |
| 1801 | |
| 1802 | mutex_lock(&tcm_vhost_mutex); |
| 1803 | list_del(&tpg->tv_tpg_list); |
| 1804 | mutex_unlock(&tcm_vhost_mutex); |
| 1805 | /* |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1806 | * Release the virtual I_T Nexus for this vhost TPG |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1807 | */ |
| 1808 | tcm_vhost_drop_nexus(tpg); |
| 1809 | /* |
| 1810 | * Deregister the se_tpg from TCM.. |
| 1811 | */ |
| 1812 | core_tpg_deregister(se_tpg); |
| 1813 | kfree(tpg); |
| 1814 | } |
| 1815 | |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 1816 | static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1817 | struct config_group *group, |
| 1818 | const char *name) |
| 1819 | { |
| 1820 | struct tcm_vhost_tport *tport; |
| 1821 | char *ptr; |
| 1822 | u64 wwpn = 0; |
| 1823 | int off = 0; |
| 1824 | |
| 1825 | /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0) |
| 1826 | return ERR_PTR(-EINVAL); */ |
| 1827 | |
| 1828 | tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL); |
| 1829 | if (!tport) { |
| 1830 | pr_err("Unable to allocate struct tcm_vhost_tport"); |
| 1831 | return ERR_PTR(-ENOMEM); |
| 1832 | } |
| 1833 | tport->tport_wwpn = wwpn; |
| 1834 | /* |
| 1835 | * Determine the emulated Protocol Identifier and Target Port Name |
| 1836 | * based on the incoming configfs directory name. |
| 1837 | */ |
| 1838 | ptr = strstr(name, "naa."); |
| 1839 | if (ptr) { |
| 1840 | tport->tport_proto_id = SCSI_PROTOCOL_SAS; |
| 1841 | goto check_len; |
| 1842 | } |
| 1843 | ptr = strstr(name, "fc."); |
| 1844 | if (ptr) { |
| 1845 | tport->tport_proto_id = SCSI_PROTOCOL_FCP; |
| 1846 | off = 3; /* Skip over "fc." */ |
| 1847 | goto check_len; |
| 1848 | } |
| 1849 | ptr = strstr(name, "iqn."); |
| 1850 | if (ptr) { |
| 1851 | tport->tport_proto_id = SCSI_PROTOCOL_ISCSI; |
| 1852 | goto check_len; |
| 1853 | } |
| 1854 | |
| 1855 | pr_err("Unable to locate prefix for emulated Target Port:" |
| 1856 | " %s\n", name); |
| 1857 | kfree(tport); |
| 1858 | return ERR_PTR(-EINVAL); |
| 1859 | |
| 1860 | check_len: |
| 1861 | if (strlen(name) >= TCM_VHOST_NAMELEN) { |
| 1862 | pr_err("Emulated %s Address: %s, exceeds" |
| 1863 | " max: %d\n", name, tcm_vhost_dump_proto_id(tport), |
| 1864 | TCM_VHOST_NAMELEN); |
| 1865 | kfree(tport); |
| 1866 | return ERR_PTR(-EINVAL); |
| 1867 | } |
| 1868 | snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]); |
| 1869 | |
| 1870 | pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target" |
| 1871 | " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name); |
| 1872 | |
| 1873 | return &tport->tport_wwn; |
| 1874 | } |
| 1875 | |
| 1876 | static void tcm_vhost_drop_tport(struct se_wwn *wwn) |
| 1877 | { |
| 1878 | struct tcm_vhost_tport *tport = container_of(wwn, |
| 1879 | struct tcm_vhost_tport, tport_wwn); |
| 1880 | |
| 1881 | pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target" |
| 1882 | " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), |
| 1883 | tport->tport_name); |
| 1884 | |
| 1885 | kfree(tport); |
| 1886 | } |
| 1887 | |
| 1888 | static ssize_t tcm_vhost_wwn_show_attr_version( |
| 1889 | struct target_fabric_configfs *tf, |
| 1890 | char *page) |
| 1891 | { |
| 1892 | return sprintf(page, "TCM_VHOST fabric module %s on %s/%s" |
| 1893 | "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, |
| 1894 | utsname()->machine); |
| 1895 | } |
| 1896 | |
| 1897 | TF_WWN_ATTR_RO(tcm_vhost, version); |
| 1898 | |
| 1899 | static struct configfs_attribute *tcm_vhost_wwn_attrs[] = { |
| 1900 | &tcm_vhost_wwn_version.attr, |
| 1901 | NULL, |
| 1902 | }; |
| 1903 | |
| 1904 | static struct target_core_fabric_ops tcm_vhost_ops = { |
| 1905 | .get_fabric_name = tcm_vhost_get_fabric_name, |
| 1906 | .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident, |
| 1907 | .tpg_get_wwn = tcm_vhost_get_fabric_wwn, |
| 1908 | .tpg_get_tag = tcm_vhost_get_tag, |
| 1909 | .tpg_get_default_depth = tcm_vhost_get_default_depth, |
| 1910 | .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id, |
| 1911 | .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len, |
| 1912 | .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id, |
| 1913 | .tpg_check_demo_mode = tcm_vhost_check_true, |
| 1914 | .tpg_check_demo_mode_cache = tcm_vhost_check_true, |
| 1915 | .tpg_check_demo_mode_write_protect = tcm_vhost_check_false, |
| 1916 | .tpg_check_prod_mode_write_protect = tcm_vhost_check_false, |
| 1917 | .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl, |
| 1918 | .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl, |
| 1919 | .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index, |
| 1920 | .release_cmd = tcm_vhost_release_cmd, |
| 1921 | .shutdown_session = tcm_vhost_shutdown_session, |
| 1922 | .close_session = tcm_vhost_close_session, |
| 1923 | .sess_get_index = tcm_vhost_sess_get_index, |
| 1924 | .sess_get_initiator_sid = NULL, |
| 1925 | .write_pending = tcm_vhost_write_pending, |
| 1926 | .write_pending_status = tcm_vhost_write_pending_status, |
| 1927 | .set_default_node_attributes = tcm_vhost_set_default_node_attrs, |
| 1928 | .get_task_tag = tcm_vhost_get_task_tag, |
| 1929 | .get_cmd_state = tcm_vhost_get_cmd_state, |
| 1930 | .queue_data_in = tcm_vhost_queue_data_in, |
| 1931 | .queue_status = tcm_vhost_queue_status, |
| 1932 | .queue_tm_rsp = tcm_vhost_queue_tm_rsp, |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 1933 | /* |
| 1934 | * Setup callers for generic logic in target_core_fabric_configfs.c |
| 1935 | */ |
| 1936 | .fabric_make_wwn = tcm_vhost_make_tport, |
| 1937 | .fabric_drop_wwn = tcm_vhost_drop_tport, |
| 1938 | .fabric_make_tpg = tcm_vhost_make_tpg, |
| 1939 | .fabric_drop_tpg = tcm_vhost_drop_tpg, |
| 1940 | .fabric_post_link = tcm_vhost_port_link, |
| 1941 | .fabric_pre_unlink = tcm_vhost_port_unlink, |
| 1942 | .fabric_make_np = NULL, |
| 1943 | .fabric_drop_np = NULL, |
| 1944 | .fabric_make_nodeacl = tcm_vhost_make_nodeacl, |
| 1945 | .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl, |
| 1946 | }; |
| 1947 | |
| 1948 | static int tcm_vhost_register_configfs(void) |
| 1949 | { |
| 1950 | struct target_fabric_configfs *fabric; |
| 1951 | int ret; |
| 1952 | |
| 1953 | pr_debug("TCM_VHOST fabric module %s on %s/%s" |
| 1954 | " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, |
| 1955 | utsname()->machine); |
| 1956 | /* |
| 1957 | * Register the top level struct config_item_type with TCM core |
| 1958 | */ |
| 1959 | fabric = target_fabric_configfs_init(THIS_MODULE, "vhost"); |
| 1960 | if (IS_ERR(fabric)) { |
| 1961 | pr_err("target_fabric_configfs_init() failed\n"); |
| 1962 | return PTR_ERR(fabric); |
| 1963 | } |
| 1964 | /* |
| 1965 | * Setup fabric->tf_ops from our local tcm_vhost_ops |
| 1966 | */ |
| 1967 | fabric->tf_ops = tcm_vhost_ops; |
| 1968 | /* |
| 1969 | * Setup default attribute lists for various fabric->tf_cit_tmpl |
| 1970 | */ |
| 1971 | TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs; |
| 1972 | TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs; |
| 1973 | TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL; |
| 1974 | TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL; |
| 1975 | TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL; |
| 1976 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL; |
| 1977 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL; |
| 1978 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL; |
| 1979 | TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL; |
| 1980 | /* |
| 1981 | * Register the fabric for use within TCM |
| 1982 | */ |
| 1983 | ret = target_fabric_configfs_register(fabric); |
| 1984 | if (ret < 0) { |
| 1985 | pr_err("target_fabric_configfs_register() failed" |
| 1986 | " for TCM_VHOST\n"); |
| 1987 | return ret; |
| 1988 | } |
| 1989 | /* |
| 1990 | * Setup our local pointer to *fabric |
| 1991 | */ |
| 1992 | tcm_vhost_fabric_configfs = fabric; |
| 1993 | pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n"); |
| 1994 | return 0; |
| 1995 | }; |
| 1996 | |
| 1997 | static void tcm_vhost_deregister_configfs(void) |
| 1998 | { |
| 1999 | if (!tcm_vhost_fabric_configfs) |
| 2000 | return; |
| 2001 | |
| 2002 | target_fabric_configfs_deregister(tcm_vhost_fabric_configfs); |
| 2003 | tcm_vhost_fabric_configfs = NULL; |
| 2004 | pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n"); |
| 2005 | }; |
| 2006 | |
| 2007 | static int __init tcm_vhost_init(void) |
| 2008 | { |
| 2009 | int ret = -ENOMEM; |
Nicholas Bellinger | 101998f | 2012-07-30 13:30:00 -0700 | [diff] [blame] | 2010 | /* |
| 2011 | * Use our own dedicated workqueue for submitting I/O into |
| 2012 | * target core to avoid contention within system_wq. |
| 2013 | */ |
Nicholas Bellinger | 057cbf4 | 2012-07-18 14:31:32 -0700 | [diff] [blame] | 2014 | tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0); |
| 2015 | if (!tcm_vhost_workqueue) |
| 2016 | goto out; |
| 2017 | |
| 2018 | ret = vhost_scsi_register(); |
| 2019 | if (ret < 0) |
| 2020 | goto out_destroy_workqueue; |
| 2021 | |
| 2022 | ret = tcm_vhost_register_configfs(); |
| 2023 | if (ret < 0) |
| 2024 | goto out_vhost_scsi_deregister; |
| 2025 | |
| 2026 | return 0; |
| 2027 | |
| 2028 | out_vhost_scsi_deregister: |
| 2029 | vhost_scsi_deregister(); |
| 2030 | out_destroy_workqueue: |
| 2031 | destroy_workqueue(tcm_vhost_workqueue); |
| 2032 | out: |
| 2033 | return ret; |
| 2034 | }; |
| 2035 | |
| 2036 | static void tcm_vhost_exit(void) |
| 2037 | { |
| 2038 | tcm_vhost_deregister_configfs(); |
| 2039 | vhost_scsi_deregister(); |
| 2040 | destroy_workqueue(tcm_vhost_workqueue); |
| 2041 | }; |
| 2042 | |
| 2043 | MODULE_DESCRIPTION("TCM_VHOST series fabric driver"); |
| 2044 | MODULE_LICENSE("GPL"); |
| 2045 | module_init(tcm_vhost_init); |
| 2046 | module_exit(tcm_vhost_exit); |