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