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