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