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