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