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