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