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