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