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