1 // SPDX-License-Identifier: GPL-2.0+
2 /*******************************************************************************
3 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 *
5 * (C) Copyright 2010-2013 Datera, Inc.
6 * (C) Copyright 2010-2012 IBM Corp.
7 *
8 * Authors: Nicholas A. Bellinger <nab@daterainc.com>
9 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
10 ****************************************************************************/
11
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <generated/utsrelease.h>
15 #include <linux/utsname.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/kthread.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/configfs.h>
22 #include <linux/ctype.h>
23 #include <linux/compat.h>
24 #include <linux/eventfd.h>
25 #include <linux/fs.h>
26 #include <linux/vmalloc.h>
27 #include <linux/miscdevice.h>
28 #include <linux/blk_types.h>
29 #include <linux/bio.h>
30 #include <linux/unaligned.h>
31 #include <scsi/scsi_common.h>
32 #include <scsi/scsi_proto.h>
33 #include <target/target_core_base.h>
34 #include <target/target_core_fabric.h>
35 #include <linux/vhost.h>
36 #include <linux/virtio_scsi.h>
37 #include <linux/llist.h>
38 #include <linux/bitmap.h>
39
40 #include "vhost.h"
41
42 #define VHOST_SCSI_VERSION "v0.1"
43 #define VHOST_SCSI_NAMELEN 256
44 #define VHOST_SCSI_MAX_CDB_SIZE 32
45 #define VHOST_SCSI_PREALLOC_SGLS 2048
46 #define VHOST_SCSI_PREALLOC_UPAGES 2048
47 #define VHOST_SCSI_PREALLOC_PROT_SGLS 2048
48 /*
49 * For the legacy descriptor case we allocate an iov per byte in the
50 * virtio_scsi_cmd_resp struct.
51 */
52 #define VHOST_SCSI_MAX_RESP_IOVS sizeof(struct virtio_scsi_cmd_resp)
53
54 static unsigned int vhost_scsi_inline_sg_cnt = VHOST_SCSI_PREALLOC_SGLS;
55
56 #ifdef CONFIG_ARCH_NO_SG_CHAIN
vhost_scsi_set_inline_sg_cnt(const char * buf,const struct kernel_param * kp)57 static int vhost_scsi_set_inline_sg_cnt(const char *buf,
58 const struct kernel_param *kp)
59 {
60 pr_err("Setting inline_sg_cnt is not supported.\n");
61 return -EOPNOTSUPP;
62 }
63 #else
vhost_scsi_set_inline_sg_cnt(const char * buf,const struct kernel_param * kp)64 static int vhost_scsi_set_inline_sg_cnt(const char *buf,
65 const struct kernel_param *kp)
66 {
67 unsigned int cnt;
68 int ret;
69
70 ret = kstrtouint(buf, 10, &cnt);
71 if (ret)
72 return ret;
73
74 if (cnt > VHOST_SCSI_PREALLOC_SGLS) {
75 pr_err("Max inline_sg_cnt is %u\n", VHOST_SCSI_PREALLOC_SGLS);
76 return -EINVAL;
77 }
78
79 vhost_scsi_inline_sg_cnt = cnt;
80 return 0;
81 }
82 #endif
83
vhost_scsi_get_inline_sg_cnt(char * buf,const struct kernel_param * kp)84 static int vhost_scsi_get_inline_sg_cnt(char *buf,
85 const struct kernel_param *kp)
86 {
87 return sprintf(buf, "%u\n", vhost_scsi_inline_sg_cnt);
88 }
89
90 static const struct kernel_param_ops vhost_scsi_inline_sg_cnt_op = {
91 .get = vhost_scsi_get_inline_sg_cnt,
92 .set = vhost_scsi_set_inline_sg_cnt,
93 };
94
95 module_param_cb(inline_sg_cnt, &vhost_scsi_inline_sg_cnt_op, NULL, 0644);
96 MODULE_PARM_DESC(inline_sg_cnt, "Set the number of scatterlist entries to pre-allocate. The default is 2048.");
97
98 /* Max number of requests before requeueing the job.
99 * Using this limit prevents one virtqueue from starving others with
100 * request.
101 */
102 #define VHOST_SCSI_WEIGHT 256
103
104 struct vhost_scsi_inflight {
105 /* Wait for the flush operation to finish */
106 struct completion comp;
107 /* Refcount for the inflight reqs */
108 struct kref kref;
109 };
110
111 struct vhost_scsi_cmd {
112 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
113 int tvc_vq_desc;
114 /* The number of scatterlists associated with this cmd */
115 u32 tvc_sgl_count;
116 u32 tvc_prot_sgl_count;
117 u32 copied_iov:1;
118 const void *read_iov;
119 struct iov_iter *read_iter;
120 struct scatterlist *sgl;
121 struct sg_table table;
122 struct scatterlist *prot_sgl;
123 struct sg_table prot_table;
124 /* Fast path response header iovec used when only one vec is needed */
125 struct iovec tvc_resp_iov;
126 /* Number of iovs for response */
127 unsigned int tvc_resp_iovs_cnt;
128 /* Pointer to response header iovecs if more than one is needed */
129 struct iovec *tvc_resp_iovs;
130 /* Pointer to vhost_virtqueue for the cmd */
131 struct vhost_virtqueue *tvc_vq;
132 /* The TCM I/O descriptor that is accessed via container_of() */
133 struct se_cmd tvc_se_cmd;
134 /* Sense buffer that will be mapped into outgoing status */
135 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
136 /*
137 * Dirty write descriptors of this command.
138 */
139 struct vhost_log *tvc_log;
140 unsigned int tvc_log_num;
141 /* Completed commands list, serviced from vhost worker thread */
142 struct llist_node tvc_completion_list;
143 /* Used to track inflight cmd */
144 struct vhost_scsi_inflight *inflight;
145 };
146
147 struct vhost_scsi_nexus {
148 /* Pointer to TCM session for I_T Nexus */
149 struct se_session *tvn_se_sess;
150 };
151
152 struct vhost_scsi_tpg {
153 /* Vhost port target portal group tag for TCM */
154 u16 tport_tpgt;
155 /* Used to track number of TPG Port/Lun Links wrt to explicit I_T Nexus shutdown */
156 int tv_tpg_port_count;
157 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
158 int tv_tpg_vhost_count;
159 /* Used for enabling T10-PI with legacy devices */
160 int tv_fabric_prot_type;
161 /* list for vhost_scsi_list */
162 struct list_head tv_tpg_list;
163 /* Used to protect access for tpg_nexus */
164 struct mutex tv_tpg_mutex;
165 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
166 struct vhost_scsi_nexus *tpg_nexus;
167 /* Pointer back to vhost_scsi_tport */
168 struct vhost_scsi_tport *tport;
169 /* Returned by vhost_scsi_make_tpg() */
170 struct se_portal_group se_tpg;
171 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
172 struct vhost_scsi *vhost_scsi;
173 };
174
175 struct vhost_scsi_tport {
176 /* SCSI protocol the tport is providing */
177 u8 tport_proto_id;
178 /* Binary World Wide unique Port Name for Vhost Target port */
179 u64 tport_wwpn;
180 /* ASCII formatted WWPN for Vhost Target port */
181 char tport_name[VHOST_SCSI_NAMELEN];
182 /* Returned by vhost_scsi_make_tport() */
183 struct se_wwn tport_wwn;
184 };
185
186 struct vhost_scsi_evt {
187 /* event to be sent to guest */
188 struct virtio_scsi_event event;
189 /* event list, serviced from vhost worker thread */
190 struct llist_node list;
191 };
192
193 enum {
194 VHOST_SCSI_VQ_CTL = 0,
195 VHOST_SCSI_VQ_EVT = 1,
196 VHOST_SCSI_VQ_IO = 2,
197 };
198
199 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
200 static const int vhost_scsi_bits[] = {
201 VHOST_FEATURES,
202 VIRTIO_SCSI_F_HOTPLUG,
203 VIRTIO_SCSI_F_T10_PI
204 };
205
206 #define VHOST_SCSI_FEATURES VHOST_FEATURES_U64(vhost_scsi_bits, 0)
207
208 #define VHOST_SCSI_MAX_TARGET 256
209 #define VHOST_SCSI_MAX_IO_VQ 1024
210 #define VHOST_SCSI_MAX_EVENT 128
211
212 static unsigned vhost_scsi_max_io_vqs = 128;
213 module_param_named(max_io_vqs, vhost_scsi_max_io_vqs, uint, 0644);
214 MODULE_PARM_DESC(max_io_vqs, "Set the max number of IO virtqueues a vhost scsi device can support. The default is 128. The max is 1024.");
215
216 struct vhost_scsi_virtqueue {
217 struct vhost_virtqueue vq;
218 struct vhost_scsi *vs;
219 /*
220 * Reference counting for inflight reqs, used for flush operation. At
221 * each time, one reference tracks new commands submitted, while we
222 * wait for another one to reach 0.
223 */
224 struct vhost_scsi_inflight inflights[2];
225 /*
226 * Indicate current inflight in use, protected by vq->mutex.
227 * Writers must also take dev mutex and flush under it.
228 */
229 int inflight_idx;
230 struct vhost_scsi_cmd *scsi_cmds;
231 struct sbitmap scsi_tags;
232 int max_cmds;
233 struct page **upages;
234
235 struct vhost_work completion_work;
236 struct llist_head completion_list;
237 };
238
239 struct vhost_scsi {
240 /* Protected by vhost_scsi->dev.mutex */
241 struct vhost_scsi_tpg **vs_tpg;
242 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
243
244 struct vhost_dev dev;
245 struct vhost_scsi_virtqueue *vqs;
246 struct vhost_scsi_inflight **old_inflight;
247
248 struct vhost_work vs_event_work; /* evt injection work item */
249 struct llist_head vs_event_list; /* evt injection queue */
250
251 bool vs_events_missed; /* any missed events, protected by vq->mutex */
252 int vs_events_nr; /* num of pending events, protected by vq->mutex */
253
254 unsigned int inline_sg_cnt;
255 };
256
257 struct vhost_scsi_tmf {
258 struct vhost_work vwork;
259 struct work_struct flush_work;
260 struct vhost_scsi *vhost;
261 struct vhost_scsi_virtqueue *svq;
262
263 struct se_cmd se_cmd;
264 u8 scsi_resp;
265 struct vhost_scsi_inflight *inflight;
266 struct iovec resp_iov;
267 int in_iovs;
268 int vq_desc;
269
270 /*
271 * Dirty write descriptors of this command.
272 */
273 struct vhost_log *tmf_log;
274 unsigned int tmf_log_num;
275 };
276
277 /*
278 * Context for processing request and control queue operations.
279 */
280 struct vhost_scsi_ctx {
281 int head;
282 unsigned int out, in;
283 size_t req_size, rsp_size;
284 size_t out_size, in_size;
285 u8 *target, *lunp;
286 void *req;
287 struct iov_iter out_iter;
288 };
289
290 /*
291 * Global mutex to protect vhost_scsi TPG list for vhost IOCTLs and LIO
292 * configfs management operations.
293 */
294 static DEFINE_MUTEX(vhost_scsi_mutex);
295 static LIST_HEAD(vhost_scsi_list);
296
vhost_scsi_done_inflight(struct kref * kref)297 static void vhost_scsi_done_inflight(struct kref *kref)
298 {
299 struct vhost_scsi_inflight *inflight;
300
301 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
302 complete(&inflight->comp);
303 }
304
vhost_scsi_init_inflight(struct vhost_scsi * vs,struct vhost_scsi_inflight * old_inflight[])305 static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
306 struct vhost_scsi_inflight *old_inflight[])
307 {
308 struct vhost_scsi_inflight *new_inflight;
309 struct vhost_virtqueue *vq;
310 int idx, i;
311
312 for (i = 0; i < vs->dev.nvqs; i++) {
313 vq = &vs->vqs[i].vq;
314
315 mutex_lock(&vq->mutex);
316
317 /* store old inflight */
318 idx = vs->vqs[i].inflight_idx;
319 if (old_inflight)
320 old_inflight[i] = &vs->vqs[i].inflights[idx];
321
322 /* setup new inflight */
323 vs->vqs[i].inflight_idx = idx ^ 1;
324 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
325 kref_init(&new_inflight->kref);
326 init_completion(&new_inflight->comp);
327
328 mutex_unlock(&vq->mutex);
329 }
330 }
331
332 static struct vhost_scsi_inflight *
vhost_scsi_get_inflight(struct vhost_virtqueue * vq)333 vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
334 {
335 struct vhost_scsi_inflight *inflight;
336 struct vhost_scsi_virtqueue *svq;
337
338 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
339 inflight = &svq->inflights[svq->inflight_idx];
340 kref_get(&inflight->kref);
341
342 return inflight;
343 }
344
vhost_scsi_put_inflight(struct vhost_scsi_inflight * inflight)345 static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
346 {
347 kref_put(&inflight->kref, vhost_scsi_done_inflight);
348 }
349
vhost_scsi_check_true(struct se_portal_group * se_tpg)350 static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
351 {
352 return 1;
353 }
354
vhost_scsi_get_fabric_wwn(struct se_portal_group * se_tpg)355 static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
356 {
357 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
358 struct vhost_scsi_tpg, se_tpg);
359 struct vhost_scsi_tport *tport = tpg->tport;
360
361 return &tport->tport_name[0];
362 }
363
vhost_scsi_get_tpgt(struct se_portal_group * se_tpg)364 static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
365 {
366 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
367 struct vhost_scsi_tpg, se_tpg);
368 return tpg->tport_tpgt;
369 }
370
vhost_scsi_check_prot_fabric_only(struct se_portal_group * se_tpg)371 static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
372 {
373 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
374 struct vhost_scsi_tpg, se_tpg);
375
376 return tpg->tv_fabric_prot_type;
377 }
378
vhost_scsi_copy_cmd_log(struct vhost_virtqueue * vq,struct vhost_scsi_cmd * cmd,struct vhost_log * log,unsigned int log_num)379 static int vhost_scsi_copy_cmd_log(struct vhost_virtqueue *vq,
380 struct vhost_scsi_cmd *cmd,
381 struct vhost_log *log,
382 unsigned int log_num)
383 {
384 if (!cmd->tvc_log)
385 cmd->tvc_log = kmalloc_objs(*cmd->tvc_log, vq->dev->iov_limit);
386
387 if (unlikely(!cmd->tvc_log)) {
388 vq_err(vq, "Failed to alloc tvc_log\n");
389 return -ENOMEM;
390 }
391
392 memcpy(cmd->tvc_log, log, sizeof(*cmd->tvc_log) * log_num);
393 cmd->tvc_log_num = log_num;
394
395 return 0;
396 }
397
vhost_scsi_log_write(struct vhost_virtqueue * vq,struct vhost_log * log,unsigned int log_num)398 static void vhost_scsi_log_write(struct vhost_virtqueue *vq,
399 struct vhost_log *log,
400 unsigned int log_num)
401 {
402 if (likely(!vhost_has_feature(vq, VHOST_F_LOG_ALL)))
403 return;
404
405 if (likely(!log_num || !log))
406 return;
407
408 /*
409 * vhost-scsi doesn't support VIRTIO_F_ACCESS_PLATFORM.
410 * No requirement for vq->iotlb case.
411 */
412 WARN_ON_ONCE(unlikely(vq->iotlb));
413 vhost_log_write(vq, log, log_num, U64_MAX, NULL, 0);
414 }
415
vhost_scsi_release_cmd_res(struct se_cmd * se_cmd)416 static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
417 {
418 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
419 struct vhost_scsi_cmd, tvc_se_cmd);
420 struct vhost_scsi_virtqueue *svq = container_of(tv_cmd->tvc_vq,
421 struct vhost_scsi_virtqueue, vq);
422 struct vhost_scsi *vs = svq->vs;
423 struct vhost_scsi_inflight *inflight = tv_cmd->inflight;
424 struct scatterlist *sg;
425 struct page *page;
426 int i;
427
428 if (tv_cmd->tvc_sgl_count) {
429 for_each_sgtable_sg(&tv_cmd->table, sg, i) {
430 page = sg_page(sg);
431 if (!page)
432 continue;
433
434 if (tv_cmd->copied_iov)
435 __free_page(page);
436 else
437 put_page(page);
438 }
439 kfree(tv_cmd->read_iter);
440 kfree(tv_cmd->read_iov);
441 sg_free_table_chained(&tv_cmd->table, vs->inline_sg_cnt);
442 }
443 if (tv_cmd->tvc_prot_sgl_count) {
444 for_each_sgtable_sg(&tv_cmd->prot_table, sg, i) {
445 page = sg_page(sg);
446 if (page)
447 put_page(page);
448 }
449 sg_free_table_chained(&tv_cmd->prot_table, vs->inline_sg_cnt);
450 }
451
452 if (tv_cmd->tvc_resp_iovs != &tv_cmd->tvc_resp_iov)
453 kfree(tv_cmd->tvc_resp_iovs);
454 sbitmap_clear_bit(&svq->scsi_tags, se_cmd->map_tag);
455 vhost_scsi_put_inflight(inflight);
456 }
457
vhost_scsi_release_tmf_res(struct vhost_scsi_tmf * tmf)458 static void vhost_scsi_release_tmf_res(struct vhost_scsi_tmf *tmf)
459 {
460 struct vhost_scsi_inflight *inflight = tmf->inflight;
461
462 /*
463 * tmf->tmf_log is default NULL unless VHOST_F_LOG_ALL is set.
464 */
465 kfree(tmf->tmf_log);
466 kfree(tmf);
467 vhost_scsi_put_inflight(inflight);
468 }
469
vhost_scsi_drop_cmds(struct vhost_scsi_virtqueue * svq)470 static void vhost_scsi_drop_cmds(struct vhost_scsi_virtqueue *svq)
471 {
472 struct vhost_scsi_cmd *cmd, *t;
473 struct llist_node *llnode;
474
475 llnode = llist_del_all(&svq->completion_list);
476 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list)
477 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
478 }
479
vhost_scsi_release_cmd(struct se_cmd * se_cmd)480 static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
481 {
482 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
483 struct vhost_scsi_tmf *tmf = container_of(se_cmd,
484 struct vhost_scsi_tmf, se_cmd);
485
486 schedule_work(&tmf->flush_work);
487 } else {
488 struct vhost_scsi_cmd *cmd = container_of(se_cmd,
489 struct vhost_scsi_cmd, tvc_se_cmd);
490 struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq,
491 struct vhost_scsi_virtqueue, vq);
492
493 llist_add(&cmd->tvc_completion_list, &svq->completion_list);
494 if (!vhost_vq_work_queue(&svq->vq, &svq->completion_work))
495 vhost_scsi_drop_cmds(svq);
496 }
497 }
498
vhost_scsi_write_pending(struct se_cmd * se_cmd)499 static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
500 {
501 /* Go ahead and process the write immediately */
502 target_execute_cmd(se_cmd);
503 return 0;
504 }
505
vhost_scsi_queue_data_in(struct se_cmd * se_cmd)506 static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
507 {
508 transport_generic_free_cmd(se_cmd, 0);
509 return 0;
510 }
511
vhost_scsi_queue_status(struct se_cmd * se_cmd)512 static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
513 {
514 transport_generic_free_cmd(se_cmd, 0);
515 return 0;
516 }
517
vhost_scsi_queue_tm_rsp(struct se_cmd * se_cmd)518 static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
519 {
520 struct vhost_scsi_tmf *tmf = container_of(se_cmd, struct vhost_scsi_tmf,
521 se_cmd);
522
523 tmf->scsi_resp = se_cmd->se_tmr_req->response;
524 transport_generic_free_cmd(&tmf->se_cmd, 0);
525 }
526
vhost_scsi_aborted_task(struct se_cmd * se_cmd)527 static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
528 {
529 return;
530 }
531
vhost_scsi_free_evt(struct vhost_scsi * vs,struct vhost_scsi_evt * evt)532 static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
533 {
534 vs->vs_events_nr--;
535 kfree(evt);
536 }
537
538 static struct vhost_scsi_evt *
vhost_scsi_allocate_evt(struct vhost_scsi * vs,u32 event,u32 reason)539 vhost_scsi_allocate_evt(struct vhost_scsi *vs,
540 u32 event, u32 reason)
541 {
542 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
543 struct vhost_scsi_evt *evt;
544
545 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
546 vs->vs_events_missed = true;
547 return NULL;
548 }
549
550 evt = kzalloc_obj(*evt);
551 if (!evt) {
552 vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
553 vs->vs_events_missed = true;
554 return NULL;
555 }
556
557 evt->event.event = cpu_to_vhost32(vq, event);
558 evt->event.reason = cpu_to_vhost32(vq, reason);
559 vs->vs_events_nr++;
560
561 return evt;
562 }
563
vhost_scsi_check_stop_free(struct se_cmd * se_cmd)564 static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
565 {
566 return target_put_sess_cmd(se_cmd);
567 }
568
569 static void
vhost_scsi_do_evt_work(struct vhost_scsi * vs,struct vhost_scsi_evt * evt)570 vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
571 {
572 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
573 struct virtio_scsi_event *event = &evt->event;
574 struct virtio_scsi_event __user *eventp;
575 struct vhost_log *vq_log;
576 unsigned int log_num;
577 unsigned out, in;
578 int head, ret;
579
580 if (!vhost_vq_get_backend(vq)) {
581 vs->vs_events_missed = true;
582 return;
583 }
584
585 again:
586 vhost_disable_notify(&vs->dev, vq);
587
588 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
589 vq->log : NULL;
590
591 /*
592 * Reset 'log_num' since vhost_get_vq_desc() may reset it only
593 * after certain condition checks.
594 */
595 log_num = 0;
596
597 head = vhost_get_vq_desc(vq, vq->iov,
598 ARRAY_SIZE(vq->iov), &out, &in,
599 vq_log, &log_num);
600 if (head < 0) {
601 vs->vs_events_missed = true;
602 return;
603 }
604 if (head == vq->num) {
605 if (vhost_enable_notify(&vs->dev, vq))
606 goto again;
607 vs->vs_events_missed = true;
608 return;
609 }
610
611 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
612 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
613 vq->iov[out].iov_len);
614 vs->vs_events_missed = true;
615 return;
616 }
617
618 if (vs->vs_events_missed) {
619 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
620 vs->vs_events_missed = false;
621 }
622
623 eventp = vq->iov[out].iov_base;
624 ret = __copy_to_user(eventp, event, sizeof(*event));
625 if (!ret)
626 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
627 else
628 vq_err(vq, "Faulted on vhost_scsi_send_event\n");
629
630 vhost_scsi_log_write(vq, vq_log, log_num);
631 }
632
vhost_scsi_complete_events(struct vhost_scsi * vs,bool drop)633 static void vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop)
634 {
635 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
636 struct vhost_scsi_evt *evt, *t;
637 struct llist_node *llnode;
638
639 mutex_lock(&vq->mutex);
640 llnode = llist_del_all(&vs->vs_event_list);
641 llist_for_each_entry_safe(evt, t, llnode, list) {
642 if (!drop)
643 vhost_scsi_do_evt_work(vs, evt);
644 vhost_scsi_free_evt(vs, evt);
645 }
646 mutex_unlock(&vq->mutex);
647 }
648
vhost_scsi_evt_work(struct vhost_work * work)649 static void vhost_scsi_evt_work(struct vhost_work *work)
650 {
651 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
652 vs_event_work);
653 vhost_scsi_complete_events(vs, false);
654 }
655
vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd * cmd)656 static int vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd *cmd)
657 {
658 struct iov_iter *iter = cmd->read_iter;
659 struct scatterlist *sg;
660 struct page *page;
661 size_t len;
662 int i;
663
664 for_each_sgtable_sg(&cmd->table, sg, i) {
665 page = sg_page(sg);
666 if (!page)
667 continue;
668
669 len = sg->length;
670
671 if (copy_page_to_iter(page, 0, len, iter) != len) {
672 pr_err("Could not copy data while handling misaligned cmd. Error %zu\n",
673 len);
674 return -1;
675 }
676 }
677
678 return 0;
679 }
680
681 /* Fill in status and signal that we are done processing this command
682 *
683 * This is scheduled in the vhost work queue so we are called with the owner
684 * process mm and can access the vring.
685 */
vhost_scsi_complete_cmd_work(struct vhost_work * work)686 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
687 {
688 struct vhost_scsi_virtqueue *svq = container_of(work,
689 struct vhost_scsi_virtqueue, completion_work);
690 struct virtio_scsi_cmd_resp v_rsp;
691 struct vhost_scsi_cmd *cmd, *t;
692 struct llist_node *llnode;
693 struct se_cmd *se_cmd;
694 struct iov_iter iov_iter;
695 bool signal = false;
696 int ret;
697
698 llnode = llist_del_all(&svq->completion_list);
699
700 mutex_lock(&svq->vq.mutex);
701
702 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) {
703 se_cmd = &cmd->tvc_se_cmd;
704
705 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
706 cmd, se_cmd->residual_count, se_cmd->scsi_status);
707 memset(&v_rsp, 0, sizeof(v_rsp));
708
709 if (cmd->read_iter && vhost_scsi_copy_sgl_to_iov(cmd)) {
710 v_rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
711 } else {
712 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq,
713 se_cmd->residual_count);
714 /* TODO is status_qualifier field needed? */
715 v_rsp.status = se_cmd->scsi_status;
716 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
717 se_cmd->scsi_sense_length);
718 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
719 se_cmd->scsi_sense_length);
720 }
721
722 iov_iter_init(&iov_iter, ITER_DEST, cmd->tvc_resp_iovs,
723 cmd->tvc_resp_iovs_cnt, sizeof(v_rsp));
724 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
725 if (likely(ret == sizeof(v_rsp))) {
726 signal = true;
727
728 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
729 } else
730 pr_err("Faulted on virtio_scsi_cmd_resp\n");
731
732 vhost_scsi_log_write(cmd->tvc_vq, cmd->tvc_log,
733 cmd->tvc_log_num);
734
735 vhost_scsi_release_cmd_res(se_cmd);
736 }
737
738 mutex_unlock(&svq->vq.mutex);
739
740 if (signal)
741 vhost_signal(&svq->vs->dev, &svq->vq);
742 }
743
744 static struct vhost_scsi_cmd *
vhost_scsi_get_cmd(struct vhost_virtqueue * vq,u64 scsi_tag)745 vhost_scsi_get_cmd(struct vhost_virtqueue *vq, u64 scsi_tag)
746 {
747 struct vhost_scsi_virtqueue *svq = container_of(vq,
748 struct vhost_scsi_virtqueue, vq);
749 struct vhost_scsi_cmd *cmd;
750 struct scatterlist *sgl, *prot_sgl;
751 struct vhost_log *log;
752 int tag;
753
754 tag = sbitmap_get(&svq->scsi_tags);
755 if (tag < 0) {
756 pr_warn_once("Guest sent too many cmds. Returning TASK_SET_FULL.\n");
757 return ERR_PTR(-ENOMEM);
758 }
759
760 cmd = &svq->scsi_cmds[tag];
761 sgl = cmd->sgl;
762 prot_sgl = cmd->prot_sgl;
763 log = cmd->tvc_log;
764 memset(cmd, 0, sizeof(*cmd));
765 cmd->sgl = sgl;
766 cmd->prot_sgl = prot_sgl;
767 cmd->tvc_log = log;
768 cmd->tvc_se_cmd.map_tag = tag;
769 cmd->inflight = vhost_scsi_get_inflight(vq);
770
771 return cmd;
772 }
773
vhost_scsi_revert_map_iov_to_sgl(struct iov_iter * iter,struct scatterlist * curr,struct scatterlist * end)774 static void vhost_scsi_revert_map_iov_to_sgl(struct iov_iter *iter,
775 struct scatterlist *curr,
776 struct scatterlist *end)
777 {
778 size_t revert_bytes = 0;
779 struct page *page;
780
781 while (curr != end) {
782 page = sg_page(curr);
783
784 if (page) {
785 put_page(page);
786 revert_bytes += curr->length;
787 }
788 /* Clear so we can re-use it for the copy path */
789 sg_set_page(curr, NULL, 0, 0);
790 curr = sg_next(curr);
791 }
792 iov_iter_revert(iter, revert_bytes);
793 }
794
795 /*
796 * Map a user memory range into a scatterlist
797 *
798 * Returns the number of scatterlist entries used or -errno on error.
799 */
800 static int
vhost_scsi_map_to_sgl(struct vhost_scsi_cmd * cmd,struct iov_iter * iter,struct sg_table * sg_table,struct scatterlist ** sgl,bool is_prot)801 vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
802 struct iov_iter *iter,
803 struct sg_table *sg_table,
804 struct scatterlist **sgl,
805 bool is_prot)
806 {
807 struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq,
808 struct vhost_scsi_virtqueue, vq);
809 struct page **pages = svq->upages;
810 struct scatterlist *sg = *sgl;
811 ssize_t bytes;
812 size_t offset;
813 unsigned int n, npages = 0;
814
815 bytes = iov_iter_get_pages2(iter, pages, LONG_MAX,
816 VHOST_SCSI_PREALLOC_UPAGES, &offset);
817 /* No pages were pinned */
818 if (bytes <= 0)
819 return bytes < 0 ? bytes : -EFAULT;
820
821 while (bytes) {
822 n = min_t(unsigned int, PAGE_SIZE - offset, bytes);
823 /*
824 * The block layer requires bios/requests to be a multiple of
825 * 512 bytes, but Windows can send us vecs that are misaligned.
826 * This can result in bios and later requests with misaligned
827 * sizes if we have to break up a cmd/scatterlist into multiple
828 * bios.
829 *
830 * We currently only break up a command into multiple bios if
831 * we hit the vec/seg limit, so check if our sgl_count is
832 * greater than the max and if a vec in the cmd has a
833 * misaligned offset/size.
834 */
835 if (!is_prot &&
836 (offset & (SECTOR_SIZE - 1) || n & (SECTOR_SIZE - 1)) &&
837 cmd->tvc_sgl_count > BIO_MAX_VECS) {
838 WARN_ONCE(true,
839 "vhost-scsi detected misaligned IO. Performance may be degraded.");
840 goto revert_iter_get_pages;
841 }
842
843 sg_set_page(sg, pages[npages++], n, offset);
844 sg = sg_next(sg);
845 bytes -= n;
846 offset = 0;
847 }
848
849 *sgl = sg;
850 return npages;
851
852 revert_iter_get_pages:
853 vhost_scsi_revert_map_iov_to_sgl(iter, *sgl, sg);
854
855 iov_iter_revert(iter, bytes);
856 while (bytes) {
857 n = min_t(unsigned int, PAGE_SIZE, bytes);
858
859 put_page(pages[npages++]);
860 bytes -= n;
861 }
862
863 return -EINVAL;
864 }
865
866 static int
vhost_scsi_calc_sgls(struct iov_iter * iter,size_t bytes,int max_sgls)867 vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
868 {
869 int sgl_count = 0;
870
871 if (!iter || !iter_iov(iter)) {
872 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
873 " present\n", __func__, bytes);
874 return -EINVAL;
875 }
876
877 sgl_count = iov_iter_npages(iter, 0xffff);
878 if (sgl_count > max_sgls) {
879 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
880 " max_sgls: %d\n", __func__, sgl_count, max_sgls);
881 return -EINVAL;
882 }
883 return sgl_count;
884 }
885
886 static int
vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd * cmd,struct iov_iter * iter,struct sg_table * sg_table,int sg_count,int data_dir)887 vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
888 struct sg_table *sg_table, int sg_count,
889 int data_dir)
890 {
891 size_t len = iov_iter_count(iter);
892 unsigned int nbytes = 0;
893 struct scatterlist *sg;
894 struct page *page;
895 int i, ret;
896
897 if (data_dir == DMA_FROM_DEVICE) {
898 cmd->read_iter = kzalloc_obj(*cmd->read_iter);
899 if (!cmd->read_iter)
900 return -ENOMEM;
901
902 cmd->read_iov = dup_iter(cmd->read_iter, iter, GFP_KERNEL);
903 if (!cmd->read_iov) {
904 ret = -ENOMEM;
905 goto free_iter;
906 }
907 }
908
909 for_each_sgtable_sg(sg_table, sg, i) {
910 page = alloc_page(GFP_KERNEL);
911 if (!page) {
912 ret = -ENOMEM;
913 goto err;
914 }
915
916 nbytes = min_t(unsigned int, PAGE_SIZE, len);
917 sg_set_page(sg, page, nbytes, 0);
918
919 if (data_dir == DMA_TO_DEVICE &&
920 copy_page_from_iter(page, 0, nbytes, iter) != nbytes) {
921 ret = -EFAULT;
922 goto err;
923 }
924
925 len -= nbytes;
926 }
927
928 cmd->copied_iov = 1;
929 return 0;
930
931 err:
932 pr_err("Could not read %u bytes while handling misaligned cmd\n",
933 nbytes);
934
935 for_each_sgtable_sg(sg_table, sg, i) {
936 page = sg_page(sg);
937 if (page)
938 __free_page(page);
939 }
940 kfree(cmd->read_iov);
941 free_iter:
942 kfree(cmd->read_iter);
943 return ret;
944 }
945
946 static int
vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd * cmd,struct iov_iter * iter,struct sg_table * sg_table,int sg_count,bool is_prot)947 vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
948 struct sg_table *sg_table, int sg_count, bool is_prot)
949 {
950 struct scatterlist *sg = sg_table->sgl;
951 int ret;
952
953 while (iov_iter_count(iter)) {
954 ret = vhost_scsi_map_to_sgl(cmd, iter, sg_table, &sg, is_prot);
955 if (ret < 0) {
956 vhost_scsi_revert_map_iov_to_sgl(iter, sg_table->sgl,
957 sg);
958 return ret;
959 }
960 }
961
962 return 0;
963 }
964
965 static int
vhost_scsi_mapal(struct vhost_scsi * vs,struct vhost_scsi_cmd * cmd,size_t prot_bytes,struct iov_iter * prot_iter,size_t data_bytes,struct iov_iter * data_iter,int data_dir)966 vhost_scsi_mapal(struct vhost_scsi *vs, struct vhost_scsi_cmd *cmd,
967 size_t prot_bytes, struct iov_iter *prot_iter,
968 size_t data_bytes, struct iov_iter *data_iter, int data_dir)
969 {
970 int sgl_count, ret;
971
972 if (prot_bytes) {
973 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
974 VHOST_SCSI_PREALLOC_PROT_SGLS);
975 if (sgl_count < 0)
976 return sgl_count;
977
978 cmd->prot_table.sgl = cmd->prot_sgl;
979 ret = sg_alloc_table_chained(&cmd->prot_table, sgl_count,
980 cmd->prot_table.sgl,
981 vs->inline_sg_cnt);
982 if (ret)
983 return ret;
984
985 cmd->tvc_prot_sgl_count = sgl_count;
986 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
987 cmd->prot_table.sgl, cmd->tvc_prot_sgl_count);
988
989 ret = vhost_scsi_map_iov_to_sgl(cmd, prot_iter,
990 &cmd->prot_table,
991 cmd->tvc_prot_sgl_count, true);
992 if (ret < 0) {
993 sg_free_table_chained(&cmd->prot_table,
994 vs->inline_sg_cnt);
995 cmd->tvc_prot_sgl_count = 0;
996 return ret;
997 }
998 }
999 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
1000 VHOST_SCSI_PREALLOC_SGLS);
1001 if (sgl_count < 0)
1002 return sgl_count;
1003
1004 cmd->table.sgl = cmd->sgl;
1005 ret = sg_alloc_table_chained(&cmd->table, sgl_count, cmd->table.sgl,
1006 vs->inline_sg_cnt);
1007 if (ret)
1008 return ret;
1009
1010 cmd->tvc_sgl_count = sgl_count;
1011 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
1012 cmd->table.sgl, cmd->tvc_sgl_count);
1013
1014 ret = vhost_scsi_map_iov_to_sgl(cmd, data_iter, &cmd->table,
1015 cmd->tvc_sgl_count, false);
1016 if (ret == -EINVAL)
1017 ret = vhost_scsi_copy_iov_to_sgl(cmd, data_iter, &cmd->table,
1018 cmd->tvc_sgl_count, data_dir);
1019 if (ret < 0) {
1020 sg_free_table_chained(&cmd->table, vs->inline_sg_cnt);
1021 cmd->tvc_sgl_count = 0;
1022 return ret;
1023 }
1024 return 0;
1025 }
1026
vhost_scsi_to_tcm_attr(int attr)1027 static int vhost_scsi_to_tcm_attr(int attr)
1028 {
1029 switch (attr) {
1030 case VIRTIO_SCSI_S_SIMPLE:
1031 return TCM_SIMPLE_TAG;
1032 case VIRTIO_SCSI_S_ORDERED:
1033 return TCM_ORDERED_TAG;
1034 case VIRTIO_SCSI_S_HEAD:
1035 return TCM_HEAD_TAG;
1036 case VIRTIO_SCSI_S_ACA:
1037 return TCM_ACA_TAG;
1038 default:
1039 break;
1040 }
1041 return TCM_SIMPLE_TAG;
1042 }
1043
vhost_scsi_target_queue_cmd(struct vhost_scsi_nexus * nexus,struct vhost_scsi_cmd * cmd,unsigned char * cdb,u16 lun,int task_attr,int data_dir,u32 exp_data_len)1044 static void vhost_scsi_target_queue_cmd(struct vhost_scsi_nexus *nexus,
1045 struct vhost_scsi_cmd *cmd,
1046 unsigned char *cdb, u16 lun,
1047 int task_attr, int data_dir,
1048 u32 exp_data_len)
1049 {
1050 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
1051 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
1052
1053 /* FIXME: BIDI operation */
1054 if (cmd->tvc_sgl_count) {
1055 sg_ptr = cmd->table.sgl;
1056
1057 if (cmd->tvc_prot_sgl_count)
1058 sg_prot_ptr = cmd->prot_table.sgl;
1059 else
1060 se_cmd->prot_pto = true;
1061 } else {
1062 sg_ptr = NULL;
1063 }
1064
1065 se_cmd->tag = 0;
1066 target_init_cmd(se_cmd, nexus->tvn_se_sess, &cmd->tvc_sense_buf[0],
1067 lun, exp_data_len, vhost_scsi_to_tcm_attr(task_attr),
1068 data_dir, TARGET_SCF_ACK_KREF);
1069
1070 if (target_submit_prep(se_cmd, cdb, sg_ptr,
1071 cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
1072 cmd->tvc_prot_sgl_count, GFP_KERNEL))
1073 return;
1074
1075 target_submit(se_cmd);
1076 }
1077
1078 static void
vhost_scsi_send_status(struct vhost_scsi * vs,struct vhost_virtqueue * vq,struct vhost_scsi_ctx * vc,u8 status)1079 vhost_scsi_send_status(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
1080 struct vhost_scsi_ctx *vc, u8 status)
1081 {
1082 struct virtio_scsi_cmd_resp rsp;
1083 struct iov_iter iov_iter;
1084 int ret;
1085
1086 memset(&rsp, 0, sizeof(rsp));
1087 rsp.status = status;
1088
1089 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
1090 sizeof(rsp));
1091
1092 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1093
1094 if (likely(ret == sizeof(rsp)))
1095 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
1096 else
1097 pr_err("Faulted on virtio_scsi_cmd_resp\n");
1098 }
1099
1100 #define TYPE_IO_CMD 0
1101 #define TYPE_CTRL_TMF 1
1102 #define TYPE_CTRL_AN 2
1103
1104 static void
vhost_scsi_send_bad_target(struct vhost_scsi * vs,struct vhost_virtqueue * vq,struct vhost_scsi_ctx * vc,int type)1105 vhost_scsi_send_bad_target(struct vhost_scsi *vs,
1106 struct vhost_virtqueue *vq,
1107 struct vhost_scsi_ctx *vc, int type)
1108 {
1109 union {
1110 struct virtio_scsi_cmd_resp cmd;
1111 struct virtio_scsi_ctrl_tmf_resp tmf;
1112 struct virtio_scsi_ctrl_an_resp an;
1113 } rsp;
1114 struct iov_iter iov_iter;
1115 size_t rsp_size;
1116 int ret;
1117
1118 memset(&rsp, 0, sizeof(rsp));
1119
1120 if (type == TYPE_IO_CMD) {
1121 rsp_size = sizeof(struct virtio_scsi_cmd_resp);
1122 rsp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
1123 } else if (type == TYPE_CTRL_TMF) {
1124 rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
1125 rsp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
1126 } else {
1127 rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
1128 rsp.an.response = VIRTIO_SCSI_S_BAD_TARGET;
1129 }
1130
1131 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
1132 rsp_size);
1133
1134 ret = copy_to_iter(&rsp, rsp_size, &iov_iter);
1135
1136 if (likely(ret == rsp_size))
1137 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
1138 else
1139 pr_err("Faulted on virtio scsi type=%d\n", type);
1140 }
1141
1142 static int
vhost_scsi_get_desc(struct vhost_scsi * vs,struct vhost_virtqueue * vq,struct vhost_scsi_ctx * vc,struct vhost_log * log,unsigned int * log_num)1143 vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
1144 struct vhost_scsi_ctx *vc,
1145 struct vhost_log *log, unsigned int *log_num)
1146 {
1147 int ret = -ENXIO;
1148
1149 if (likely(log_num))
1150 *log_num = 0;
1151
1152 vc->head = vhost_get_vq_desc(vq, vq->iov,
1153 ARRAY_SIZE(vq->iov), &vc->out, &vc->in,
1154 log, log_num);
1155
1156 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1157 vc->head, vc->out, vc->in);
1158
1159 /* On error, stop handling until the next kick. */
1160 if (unlikely(vc->head < 0))
1161 goto done;
1162
1163 /* Nothing new? Wait for eventfd to tell us they refilled. */
1164 if (vc->head == vq->num) {
1165 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1166 vhost_disable_notify(&vs->dev, vq);
1167 ret = -EAGAIN;
1168 }
1169 goto done;
1170 }
1171
1172 /*
1173 * Get the size of request and response buffers.
1174 * FIXME: Not correct for BIDI operation
1175 */
1176 vc->out_size = iov_length(vq->iov, vc->out);
1177 vc->in_size = iov_length(&vq->iov[vc->out], vc->in);
1178
1179 /*
1180 * Copy over the virtio-scsi request header, which for a
1181 * ANY_LAYOUT enabled guest may span multiple iovecs, or a
1182 * single iovec may contain both the header + outgoing
1183 * WRITE payloads.
1184 *
1185 * copy_from_iter() will advance out_iter, so that it will
1186 * point at the start of the outgoing WRITE payload, if
1187 * DMA_TO_DEVICE is set.
1188 */
1189 iov_iter_init(&vc->out_iter, ITER_SOURCE, vq->iov, vc->out, vc->out_size);
1190 ret = 0;
1191
1192 done:
1193 return ret;
1194 }
1195
1196 static int
vhost_scsi_chk_size(struct vhost_virtqueue * vq,struct vhost_scsi_ctx * vc)1197 vhost_scsi_chk_size(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc)
1198 {
1199 if (unlikely(vc->in_size < vc->rsp_size)) {
1200 vq_err(vq,
1201 "Response buf too small, need min %zu bytes got %zu",
1202 vc->rsp_size, vc->in_size);
1203 return -EINVAL;
1204 } else if (unlikely(vc->out_size < vc->req_size)) {
1205 vq_err(vq,
1206 "Request buf too small, need min %zu bytes got %zu",
1207 vc->req_size, vc->out_size);
1208 return -EIO;
1209 }
1210
1211 return 0;
1212 }
1213
1214 static int
vhost_scsi_get_req(struct vhost_virtqueue * vq,struct vhost_scsi_ctx * vc,struct vhost_scsi_tpg ** tpgp)1215 vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc,
1216 struct vhost_scsi_tpg **tpgp)
1217 {
1218 int ret = -EIO;
1219
1220 if (unlikely(!copy_from_iter_full(vc->req, vc->req_size,
1221 &vc->out_iter))) {
1222 vq_err(vq, "Faulted on copy_from_iter_full\n");
1223 } else if (unlikely(*vc->lunp != 1)) {
1224 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1225 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *vc->lunp);
1226 } else {
1227 struct vhost_scsi_tpg **vs_tpg, *tpg = NULL;
1228
1229 if (vc->target) {
1230 /* validated at handler entry */
1231 vs_tpg = vhost_vq_get_backend(vq);
1232 tpg = READ_ONCE(vs_tpg[*vc->target]);
1233 if (unlikely(!tpg))
1234 goto out;
1235 }
1236
1237 if (tpgp)
1238 *tpgp = tpg;
1239 ret = 0;
1240 }
1241 out:
1242 return ret;
1243 }
1244
1245 static int
vhost_scsi_setup_resp_iovs(struct vhost_scsi_cmd * cmd,struct iovec * in_iovs,unsigned int in_iovs_cnt)1246 vhost_scsi_setup_resp_iovs(struct vhost_scsi_cmd *cmd, struct iovec *in_iovs,
1247 unsigned int in_iovs_cnt)
1248 {
1249 int i, cnt;
1250
1251 if (!in_iovs_cnt)
1252 return 0;
1253 /*
1254 * Initiators normally just put the virtio_scsi_cmd_resp in the first
1255 * iov, but just in case they wedged in some data with it we check for
1256 * greater than or equal to the response struct.
1257 */
1258 if (in_iovs[0].iov_len >= sizeof(struct virtio_scsi_cmd_resp)) {
1259 cmd->tvc_resp_iovs = &cmd->tvc_resp_iov;
1260 cmd->tvc_resp_iovs_cnt = 1;
1261 } else {
1262 /*
1263 * Legacy descriptor layouts didn't specify that we must put
1264 * the entire response in one iov. Worst case we have a
1265 * iov per byte.
1266 */
1267 cnt = min(VHOST_SCSI_MAX_RESP_IOVS, in_iovs_cnt);
1268 cmd->tvc_resp_iovs = kzalloc_objs(struct iovec, cnt);
1269 if (!cmd->tvc_resp_iovs)
1270 return -ENOMEM;
1271
1272 cmd->tvc_resp_iovs_cnt = cnt;
1273 }
1274
1275 for (i = 0; i < cmd->tvc_resp_iovs_cnt; i++)
1276 cmd->tvc_resp_iovs[i] = in_iovs[i];
1277
1278 return 0;
1279 }
1280
vhost_buf_to_lun(u8 * lun_buf)1281 static u16 vhost_buf_to_lun(u8 *lun_buf)
1282 {
1283 return ((lun_buf[2] << 8) | lun_buf[3]) & 0x3FFF;
1284 }
1285
1286 static void
vhost_scsi_handle_vq(struct vhost_scsi * vs,struct vhost_virtqueue * vq)1287 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1288 {
1289 struct vhost_scsi_tpg **vs_tpg, *tpg;
1290 struct virtio_scsi_cmd_req v_req;
1291 struct virtio_scsi_cmd_req_pi v_req_pi;
1292 struct vhost_scsi_nexus *nexus;
1293 struct vhost_scsi_ctx vc;
1294 struct vhost_scsi_cmd *cmd;
1295 struct iov_iter in_iter, prot_iter, data_iter;
1296 u64 tag;
1297 u32 exp_data_len, data_direction;
1298 int ret, prot_bytes, c = 0;
1299 u16 lun;
1300 u8 task_attr;
1301 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
1302 u8 *cdb;
1303 struct vhost_log *vq_log;
1304 unsigned int log_num;
1305
1306 mutex_lock(&vq->mutex);
1307 /*
1308 * We can handle the vq only after the endpoint is setup by calling the
1309 * VHOST_SCSI_SET_ENDPOINT ioctl.
1310 */
1311 vs_tpg = vhost_vq_get_backend(vq);
1312 if (!vs_tpg)
1313 goto out;
1314
1315 memset(&vc, 0, sizeof(vc));
1316 vc.rsp_size = sizeof(struct virtio_scsi_cmd_resp);
1317
1318 vhost_disable_notify(&vs->dev, vq);
1319
1320 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
1321 vq->log : NULL;
1322
1323 do {
1324 ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num);
1325 if (ret)
1326 goto err;
1327
1328 /*
1329 * Setup pointers and values based upon different virtio-scsi
1330 * request header if T10_PI is enabled in KVM guest.
1331 */
1332 if (t10_pi) {
1333 vc.req = &v_req_pi;
1334 vc.req_size = sizeof(v_req_pi);
1335 vc.lunp = &v_req_pi.lun[0];
1336 vc.target = &v_req_pi.lun[1];
1337 } else {
1338 vc.req = &v_req;
1339 vc.req_size = sizeof(v_req);
1340 vc.lunp = &v_req.lun[0];
1341 vc.target = &v_req.lun[1];
1342 }
1343
1344 /*
1345 * Validate the size of request and response buffers.
1346 * Check for a sane response buffer so we can report
1347 * early errors back to the guest.
1348 */
1349 ret = vhost_scsi_chk_size(vq, &vc);
1350 if (ret)
1351 goto err;
1352
1353 ret = vhost_scsi_get_req(vq, &vc, &tpg);
1354 if (ret)
1355 goto err;
1356
1357 ret = -EIO; /* bad target on any error from here on */
1358
1359 /*
1360 * Determine data_direction by calculating the total outgoing
1361 * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
1362 * response headers respectively.
1363 *
1364 * For DMA_TO_DEVICE this is out_iter, which is already pointing
1365 * to the right place.
1366 *
1367 * For DMA_FROM_DEVICE, the iovec will be just past the end
1368 * of the virtio-scsi response header in either the same
1369 * or immediately following iovec.
1370 *
1371 * Any associated T10_PI bytes for the outgoing / incoming
1372 * payloads are included in calculation of exp_data_len here.
1373 */
1374 prot_bytes = 0;
1375
1376 if (vc.out_size > vc.req_size) {
1377 data_direction = DMA_TO_DEVICE;
1378 exp_data_len = vc.out_size - vc.req_size;
1379 data_iter = vc.out_iter;
1380 } else if (vc.in_size > vc.rsp_size) {
1381 data_direction = DMA_FROM_DEVICE;
1382 exp_data_len = vc.in_size - vc.rsp_size;
1383
1384 iov_iter_init(&in_iter, ITER_DEST, &vq->iov[vc.out], vc.in,
1385 vc.rsp_size + exp_data_len);
1386 iov_iter_advance(&in_iter, vc.rsp_size);
1387 data_iter = in_iter;
1388 } else {
1389 data_direction = DMA_NONE;
1390 exp_data_len = 0;
1391 }
1392 /*
1393 * If T10_PI header + payload is present, setup prot_iter values
1394 * and recalculate data_iter for vhost_scsi_mapal() mapping to
1395 * host scatterlists via get_user_pages_fast().
1396 */
1397 if (t10_pi) {
1398 if (v_req_pi.pi_bytesout) {
1399 if (data_direction != DMA_TO_DEVICE) {
1400 vq_err(vq, "Received non zero pi_bytesout,"
1401 " but wrong data_direction\n");
1402 goto err;
1403 }
1404 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
1405 } else if (v_req_pi.pi_bytesin) {
1406 if (data_direction != DMA_FROM_DEVICE) {
1407 vq_err(vq, "Received non zero pi_bytesin,"
1408 " but wrong data_direction\n");
1409 goto err;
1410 }
1411 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
1412 }
1413 /*
1414 * Set prot_iter to data_iter and truncate it to
1415 * prot_bytes, and advance data_iter past any
1416 * preceding prot_bytes that may be present.
1417 *
1418 * Also fix up the exp_data_len to reflect only the
1419 * actual data payload length.
1420 */
1421 if (prot_bytes) {
1422 if (prot_bytes >= exp_data_len) {
1423 vq_err(vq, "Protection data exceeds payload length\n");
1424 goto err;
1425 }
1426
1427 exp_data_len -= prot_bytes;
1428 prot_iter = data_iter;
1429 iov_iter_truncate(&prot_iter, prot_bytes);
1430 iov_iter_advance(&data_iter, prot_bytes);
1431 }
1432 tag = vhost64_to_cpu(vq, v_req_pi.tag);
1433 task_attr = v_req_pi.task_attr;
1434 cdb = &v_req_pi.cdb[0];
1435 lun = vhost_buf_to_lun(v_req_pi.lun);
1436 } else {
1437 tag = vhost64_to_cpu(vq, v_req.tag);
1438 task_attr = v_req.task_attr;
1439 cdb = &v_req.cdb[0];
1440 lun = vhost_buf_to_lun(v_req.lun);
1441 }
1442 /*
1443 * Check that the received CDB size does not exceeded our
1444 * hardcoded max for vhost-scsi, then get a pre-allocated
1445 * cmd descriptor for the new virtio-scsi tag.
1446 *
1447 * TODO what if cdb was too small for varlen cdb header?
1448 */
1449 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
1450 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1451 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1452 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
1453 goto err;
1454 }
1455
1456 nexus = tpg->tpg_nexus;
1457 if (!nexus) {
1458 vq_err(vq, "Unable to locate active struct vhost_scsi_nexus\n");
1459 ret = -EIO;
1460 goto err;
1461 }
1462
1463 cmd = vhost_scsi_get_cmd(vq, tag);
1464 if (IS_ERR(cmd)) {
1465 ret = PTR_ERR(cmd);
1466 vq_err(vq, "vhost_scsi_get_tag failed %d\n", ret);
1467 goto err;
1468 }
1469 cmd->tvc_vq = vq;
1470
1471 ret = vhost_scsi_setup_resp_iovs(cmd, &vq->iov[vc.out], vc.in);
1472 if (ret) {
1473 vq_err(vq, "Failed to alloc recv iovs\n");
1474 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
1475 goto err;
1476 }
1477
1478 if (unlikely(vq_log && log_num)) {
1479 ret = vhost_scsi_copy_cmd_log(vq, cmd, vq_log, log_num);
1480 if (unlikely(ret)) {
1481 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
1482 goto err;
1483 }
1484 }
1485
1486 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1487 cdb[0], lun);
1488 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1489 " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
1490
1491 if (data_direction != DMA_NONE) {
1492 ret = vhost_scsi_mapal(vs, cmd, prot_bytes, &prot_iter,
1493 exp_data_len, &data_iter,
1494 data_direction);
1495 if (unlikely(ret)) {
1496 vq_err(vq, "Failed to map iov to sgl\n");
1497 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
1498 goto err;
1499 }
1500 }
1501 /*
1502 * Save the descriptor from vhost_get_vq_desc() to be used to
1503 * complete the virtio-scsi request in TCM callback context via
1504 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
1505 */
1506 cmd->tvc_vq_desc = vc.head;
1507 vhost_scsi_target_queue_cmd(nexus, cmd, cdb, lun, task_attr,
1508 data_direction,
1509 exp_data_len + prot_bytes);
1510 ret = 0;
1511 err:
1512 /*
1513 * ENXIO: No more requests, or read error, wait for next kick
1514 * EINVAL: Invalid response buffer, drop the request
1515 * EIO: Respond with bad target
1516 * EAGAIN: Pending request
1517 * ENOMEM: Could not allocate resources for request
1518 */
1519 if (ret == -ENXIO)
1520 break;
1521 else if (ret == -EIO) {
1522 vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD);
1523 vhost_scsi_log_write(vq, vq_log, log_num);
1524 } else if (ret == -ENOMEM) {
1525 vhost_scsi_send_status(vs, vq, &vc,
1526 SAM_STAT_TASK_SET_FULL);
1527 vhost_scsi_log_write(vq, vq_log, log_num);
1528 }
1529 } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
1530 out:
1531 mutex_unlock(&vq->mutex);
1532 }
1533
1534 static void
vhost_scsi_send_tmf_resp(struct vhost_scsi * vs,struct vhost_virtqueue * vq,int in_iovs,int vq_desc,struct iovec * resp_iov,int tmf_resp_code)1535 vhost_scsi_send_tmf_resp(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
1536 int in_iovs, int vq_desc, struct iovec *resp_iov,
1537 int tmf_resp_code)
1538 {
1539 struct virtio_scsi_ctrl_tmf_resp rsp;
1540 struct iov_iter iov_iter;
1541 int ret;
1542
1543 pr_debug("%s\n", __func__);
1544 memset(&rsp, 0, sizeof(rsp));
1545 rsp.response = tmf_resp_code;
1546
1547 iov_iter_init(&iov_iter, ITER_DEST, resp_iov, in_iovs, sizeof(rsp));
1548
1549 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1550 if (likely(ret == sizeof(rsp)))
1551 vhost_add_used_and_signal(&vs->dev, vq, vq_desc, 0);
1552 else
1553 pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n");
1554 }
1555
vhost_scsi_tmf_resp_work(struct vhost_work * work)1556 static void vhost_scsi_tmf_resp_work(struct vhost_work *work)
1557 {
1558 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf,
1559 vwork);
1560 int resp_code;
1561
1562 if (tmf->scsi_resp == TMR_FUNCTION_COMPLETE)
1563 resp_code = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
1564 else
1565 resp_code = VIRTIO_SCSI_S_FUNCTION_REJECTED;
1566
1567 mutex_lock(&tmf->svq->vq.mutex);
1568 vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs,
1569 tmf->vq_desc, &tmf->resp_iov, resp_code);
1570 vhost_scsi_log_write(&tmf->svq->vq, tmf->tmf_log,
1571 tmf->tmf_log_num);
1572 mutex_unlock(&tmf->svq->vq.mutex);
1573
1574 vhost_scsi_release_tmf_res(tmf);
1575 }
1576
vhost_scsi_tmf_flush_work(struct work_struct * work)1577 static void vhost_scsi_tmf_flush_work(struct work_struct *work)
1578 {
1579 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf,
1580 flush_work);
1581 struct vhost_virtqueue *vq = &tmf->svq->vq;
1582 /*
1583 * Make sure we have sent responses for other commands before we
1584 * send our response.
1585 */
1586 vhost_dev_flush(vq->dev);
1587 if (!vhost_vq_work_queue(vq, &tmf->vwork))
1588 vhost_scsi_release_tmf_res(tmf);
1589 }
1590
1591 static void
vhost_scsi_handle_tmf(struct vhost_scsi * vs,struct vhost_scsi_tpg * tpg,struct vhost_virtqueue * vq,struct virtio_scsi_ctrl_tmf_req * vtmf,struct vhost_scsi_ctx * vc,struct vhost_log * log,unsigned int log_num)1592 vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg,
1593 struct vhost_virtqueue *vq,
1594 struct virtio_scsi_ctrl_tmf_req *vtmf,
1595 struct vhost_scsi_ctx *vc,
1596 struct vhost_log *log, unsigned int log_num)
1597 {
1598 struct vhost_scsi_virtqueue *svq = container_of(vq,
1599 struct vhost_scsi_virtqueue, vq);
1600 struct vhost_scsi_tmf *tmf;
1601
1602 if (vhost32_to_cpu(vq, vtmf->subtype) !=
1603 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET)
1604 goto send_reject;
1605
1606 if (!tpg->tpg_nexus || !tpg->tpg_nexus->tvn_se_sess) {
1607 pr_err("Unable to locate active struct vhost_scsi_nexus for LUN RESET.\n");
1608 goto send_reject;
1609 }
1610
1611 tmf = kzalloc_obj(*tmf);
1612 if (!tmf)
1613 goto send_reject;
1614
1615 INIT_WORK(&tmf->flush_work, vhost_scsi_tmf_flush_work);
1616 vhost_work_init(&tmf->vwork, vhost_scsi_tmf_resp_work);
1617 tmf->vhost = vs;
1618 tmf->svq = svq;
1619 tmf->resp_iov = vq->iov[vc->out];
1620 tmf->vq_desc = vc->head;
1621 tmf->in_iovs = vc->in;
1622 tmf->inflight = vhost_scsi_get_inflight(vq);
1623
1624 if (unlikely(log && log_num)) {
1625 tmf->tmf_log = kmalloc_objs(*tmf->tmf_log, log_num);
1626 if (tmf->tmf_log) {
1627 memcpy(tmf->tmf_log, log, sizeof(*tmf->tmf_log) * log_num);
1628 tmf->tmf_log_num = log_num;
1629 } else {
1630 pr_err("vhost_scsi tmf log allocation error\n");
1631 vhost_scsi_release_tmf_res(tmf);
1632 goto send_reject;
1633 }
1634 }
1635
1636 if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL,
1637 vhost_buf_to_lun(vtmf->lun), NULL,
1638 TMR_LUN_RESET, GFP_KERNEL, 0,
1639 TARGET_SCF_ACK_KREF) < 0) {
1640 vhost_scsi_release_tmf_res(tmf);
1641 goto send_reject;
1642 }
1643
1644 return;
1645
1646 send_reject:
1647 vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out],
1648 VIRTIO_SCSI_S_FUNCTION_REJECTED);
1649 vhost_scsi_log_write(vq, log, log_num);
1650 }
1651
1652 static void
vhost_scsi_send_an_resp(struct vhost_scsi * vs,struct vhost_virtqueue * vq,struct vhost_scsi_ctx * vc)1653 vhost_scsi_send_an_resp(struct vhost_scsi *vs,
1654 struct vhost_virtqueue *vq,
1655 struct vhost_scsi_ctx *vc)
1656 {
1657 struct virtio_scsi_ctrl_an_resp rsp;
1658 struct iov_iter iov_iter;
1659 int ret;
1660
1661 pr_debug("%s\n", __func__);
1662 memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */
1663 rsp.response = VIRTIO_SCSI_S_OK;
1664
1665 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, sizeof(rsp));
1666
1667 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1668 if (likely(ret == sizeof(rsp)))
1669 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
1670 else
1671 pr_err("Faulted on virtio_scsi_ctrl_an_resp\n");
1672 }
1673
1674 static void
vhost_scsi_ctl_handle_vq(struct vhost_scsi * vs,struct vhost_virtqueue * vq)1675 vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1676 {
1677 struct vhost_scsi_tpg *tpg;
1678 union {
1679 __virtio32 type;
1680 struct virtio_scsi_ctrl_an_req an;
1681 struct virtio_scsi_ctrl_tmf_req tmf;
1682 } v_req;
1683 struct vhost_scsi_ctx vc;
1684 size_t typ_size;
1685 int ret, c = 0;
1686 struct vhost_log *vq_log;
1687 unsigned int log_num;
1688
1689 mutex_lock(&vq->mutex);
1690 /*
1691 * We can handle the vq only after the endpoint is setup by calling the
1692 * VHOST_SCSI_SET_ENDPOINT ioctl.
1693 */
1694 if (!vhost_vq_get_backend(vq))
1695 goto out;
1696
1697 memset(&vc, 0, sizeof(vc));
1698
1699 vhost_disable_notify(&vs->dev, vq);
1700
1701 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
1702 vq->log : NULL;
1703
1704 do {
1705 ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num);
1706 if (ret)
1707 goto err;
1708
1709 /*
1710 * Get the request type first in order to setup
1711 * other parameters dependent on the type.
1712 */
1713 vc.req = &v_req.type;
1714 typ_size = sizeof(v_req.type);
1715
1716 if (unlikely(!copy_from_iter_full(vc.req, typ_size,
1717 &vc.out_iter))) {
1718 vq_err(vq, "Faulted on copy_from_iter tmf type\n");
1719 /*
1720 * The size of the response buffer depends on the
1721 * request type and must be validated against it.
1722 * Since the request type is not known, don't send
1723 * a response.
1724 */
1725 continue;
1726 }
1727
1728 switch (vhost32_to_cpu(vq, v_req.type)) {
1729 case VIRTIO_SCSI_T_TMF:
1730 vc.req = &v_req.tmf;
1731 vc.req_size = sizeof(struct virtio_scsi_ctrl_tmf_req);
1732 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
1733 vc.lunp = &v_req.tmf.lun[0];
1734 vc.target = &v_req.tmf.lun[1];
1735 break;
1736 case VIRTIO_SCSI_T_AN_QUERY:
1737 case VIRTIO_SCSI_T_AN_SUBSCRIBE:
1738 vc.req = &v_req.an;
1739 vc.req_size = sizeof(struct virtio_scsi_ctrl_an_req);
1740 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
1741 vc.lunp = &v_req.an.lun[0];
1742 vc.target = NULL;
1743 break;
1744 default:
1745 vq_err(vq, "Unknown control request %d", v_req.type);
1746 continue;
1747 }
1748
1749 /*
1750 * Validate the size of request and response buffers.
1751 * Check for a sane response buffer so we can report
1752 * early errors back to the guest.
1753 */
1754 ret = vhost_scsi_chk_size(vq, &vc);
1755 if (ret)
1756 goto err;
1757
1758 /*
1759 * Get the rest of the request now that its size is known.
1760 */
1761 vc.req += typ_size;
1762 vc.req_size -= typ_size;
1763
1764 ret = vhost_scsi_get_req(vq, &vc, &tpg);
1765 if (ret)
1766 goto err;
1767
1768 if (v_req.type == VIRTIO_SCSI_T_TMF)
1769 vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc,
1770 vq_log, log_num);
1771 else {
1772 vhost_scsi_send_an_resp(vs, vq, &vc);
1773 vhost_scsi_log_write(vq, vq_log, log_num);
1774 }
1775 err:
1776 /*
1777 * ENXIO: No more requests, or read error, wait for next kick
1778 * EINVAL: Invalid response buffer, drop the request
1779 * EIO: Respond with bad target
1780 * EAGAIN: Pending request
1781 */
1782 if (ret == -ENXIO)
1783 break;
1784 else if (ret == -EIO) {
1785 vhost_scsi_send_bad_target(vs, vq, &vc,
1786 v_req.type == VIRTIO_SCSI_T_TMF ?
1787 TYPE_CTRL_TMF :
1788 TYPE_CTRL_AN);
1789 vhost_scsi_log_write(vq, vq_log, log_num);
1790 }
1791 } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
1792 out:
1793 mutex_unlock(&vq->mutex);
1794 }
1795
vhost_scsi_ctl_handle_kick(struct vhost_work * work)1796 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1797 {
1798 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1799 poll.work);
1800 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1801
1802 pr_debug("%s: The handling func for control queue.\n", __func__);
1803 vhost_scsi_ctl_handle_vq(vs, vq);
1804 }
1805
1806 static void
vhost_scsi_send_evt(struct vhost_scsi * vs,struct vhost_virtqueue * vq,struct vhost_scsi_tpg * tpg,struct se_lun * lun,u32 event,u32 reason)1807 vhost_scsi_send_evt(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
1808 struct vhost_scsi_tpg *tpg, struct se_lun *lun,
1809 u32 event, u32 reason)
1810 {
1811 struct vhost_scsi_evt *evt;
1812
1813 evt = vhost_scsi_allocate_evt(vs, event, reason);
1814 if (!evt)
1815 return;
1816
1817 if (tpg && lun) {
1818 /* TODO: share lun setup code with virtio-scsi.ko */
1819 /*
1820 * Note: evt->event is zeroed when we allocate it and
1821 * lun[4-7] need to be zero according to virtio-scsi spec.
1822 */
1823 evt->event.lun[0] = 0x01;
1824 evt->event.lun[1] = tpg->tport_tpgt;
1825 if (lun->unpacked_lun >= 256)
1826 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1827 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1828 }
1829
1830 llist_add(&evt->list, &vs->vs_event_list);
1831 if (!vhost_vq_work_queue(vq, &vs->vs_event_work))
1832 vhost_scsi_complete_events(vs, true);
1833 }
1834
vhost_scsi_evt_handle_kick(struct vhost_work * work)1835 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1836 {
1837 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1838 poll.work);
1839 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1840
1841 mutex_lock(&vq->mutex);
1842 if (!vhost_vq_get_backend(vq))
1843 goto out;
1844
1845 if (vs->vs_events_missed)
1846 vhost_scsi_send_evt(vs, vq, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT,
1847 0);
1848 out:
1849 mutex_unlock(&vq->mutex);
1850 }
1851
vhost_scsi_handle_kick(struct vhost_work * work)1852 static void vhost_scsi_handle_kick(struct vhost_work *work)
1853 {
1854 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1855 poll.work);
1856 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1857
1858 vhost_scsi_handle_vq(vs, vq);
1859 }
1860
1861 /* Callers must hold dev mutex */
vhost_scsi_flush(struct vhost_scsi * vs)1862 static void vhost_scsi_flush(struct vhost_scsi *vs)
1863 {
1864 int i;
1865
1866 /* Init new inflight and remember the old inflight */
1867 vhost_scsi_init_inflight(vs, vs->old_inflight);
1868
1869 /*
1870 * The inflight->kref was initialized to 1. We decrement it here to
1871 * indicate the start of the flush operation so that it will reach 0
1872 * when all the reqs are finished.
1873 */
1874 for (i = 0; i < vs->dev.nvqs; i++)
1875 kref_put(&vs->old_inflight[i]->kref, vhost_scsi_done_inflight);
1876
1877 /* Flush both the vhost poll and vhost work */
1878 vhost_dev_flush(&vs->dev);
1879
1880 /* Wait for all reqs issued before the flush to be finished */
1881 for (i = 0; i < vs->dev.nvqs; i++)
1882 wait_for_completion(&vs->old_inflight[i]->comp);
1883 }
1884
vhost_scsi_destroy_vq_log(struct vhost_virtqueue * vq)1885 static void vhost_scsi_destroy_vq_log(struct vhost_virtqueue *vq)
1886 {
1887 struct vhost_scsi_virtqueue *svq = container_of(vq,
1888 struct vhost_scsi_virtqueue, vq);
1889 struct vhost_scsi_cmd *tv_cmd;
1890 unsigned int i;
1891
1892 if (!svq->scsi_cmds)
1893 return;
1894
1895 for (i = 0; i < svq->max_cmds; i++) {
1896 tv_cmd = &svq->scsi_cmds[i];
1897 kfree(tv_cmd->tvc_log);
1898 tv_cmd->tvc_log = NULL;
1899 tv_cmd->tvc_log_num = 0;
1900 }
1901 }
1902
vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue * vq)1903 static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq)
1904 {
1905 struct vhost_scsi_virtqueue *svq = container_of(vq,
1906 struct vhost_scsi_virtqueue, vq);
1907 struct vhost_scsi_cmd *tv_cmd;
1908 unsigned int i;
1909
1910 if (!svq->scsi_cmds)
1911 return;
1912
1913 for (i = 0; i < svq->max_cmds; i++) {
1914 tv_cmd = &svq->scsi_cmds[i];
1915
1916 kfree(tv_cmd->sgl);
1917 kfree(tv_cmd->prot_sgl);
1918 }
1919
1920 sbitmap_free(&svq->scsi_tags);
1921 kfree(svq->upages);
1922 vhost_scsi_destroy_vq_log(vq);
1923 kfree(svq->scsi_cmds);
1924 svq->scsi_cmds = NULL;
1925 }
1926
vhost_scsi_setup_vq_cmds(struct vhost_virtqueue * vq,int max_cmds)1927 static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
1928 {
1929 struct vhost_scsi_virtqueue *svq = container_of(vq,
1930 struct vhost_scsi_virtqueue, vq);
1931 struct vhost_scsi *vs = svq->vs;
1932 struct vhost_scsi_cmd *tv_cmd;
1933 unsigned int i;
1934
1935 if (svq->scsi_cmds)
1936 return 0;
1937
1938 if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
1939 NUMA_NO_NODE, false, true))
1940 return -ENOMEM;
1941 svq->max_cmds = max_cmds;
1942
1943 svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds);
1944 if (!svq->scsi_cmds) {
1945 sbitmap_free(&svq->scsi_tags);
1946 return -ENOMEM;
1947 }
1948
1949 svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES);
1950 if (!svq->upages)
1951 goto out;
1952
1953 for (i = 0; i < max_cmds; i++) {
1954 tv_cmd = &svq->scsi_cmds[i];
1955
1956 if (vs->inline_sg_cnt) {
1957 tv_cmd->sgl = kzalloc_objs(struct scatterlist,
1958 vs->inline_sg_cnt);
1959 if (!tv_cmd->sgl) {
1960 pr_err("Unable to allocate tv_cmd->sgl\n");
1961 goto out;
1962 }
1963 }
1964
1965 if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
1966 vs->inline_sg_cnt) {
1967 tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist,
1968 vs->inline_sg_cnt);
1969 if (!tv_cmd->prot_sgl) {
1970 pr_err("Unable to allocate tv_cmd->prot_sgl\n");
1971 goto out;
1972 }
1973 }
1974 }
1975 return 0;
1976 out:
1977 vhost_scsi_destroy_vq_cmds(vq);
1978 return -ENOMEM;
1979 }
1980
1981 /*
1982 * Called from vhost_scsi_ioctl() context to walk the list of available
1983 * vhost_scsi_tpg with an active struct vhost_scsi_nexus
1984 *
1985 * The lock nesting rule is:
1986 * vs->dev.mutex -> vhost_scsi_mutex -> tpg->tv_tpg_mutex -> vq->mutex
1987 */
1988 static int
vhost_scsi_set_endpoint(struct vhost_scsi * vs,struct vhost_scsi_target * t)1989 vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1990 struct vhost_scsi_target *t)
1991 {
1992 struct se_portal_group *se_tpg;
1993 struct vhost_scsi_tport *tv_tport;
1994 struct vhost_scsi_tpg *tpg;
1995 struct vhost_scsi_tpg **vs_tpg;
1996 struct vhost_virtqueue *vq;
1997 int index, ret, i, len;
1998 bool match = false;
1999
2000 mutex_lock(&vs->dev.mutex);
2001
2002 /* Verify that ring has been setup correctly. */
2003 for (index = 0; index < vs->dev.nvqs; ++index) {
2004 /* Verify that ring has been setup correctly. */
2005 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
2006 ret = -EFAULT;
2007 goto out;
2008 }
2009 }
2010
2011 if (vs->vs_tpg) {
2012 pr_err("vhost-scsi endpoint already set for %s.\n",
2013 vs->vs_vhost_wwpn);
2014 ret = -EEXIST;
2015 goto out;
2016 }
2017
2018 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
2019 vs_tpg = kzalloc(len, GFP_KERNEL);
2020 if (!vs_tpg) {
2021 ret = -ENOMEM;
2022 goto out;
2023 }
2024
2025 mutex_lock(&vhost_scsi_mutex);
2026 list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
2027 mutex_lock(&tpg->tv_tpg_mutex);
2028 if (!tpg->tpg_nexus) {
2029 mutex_unlock(&tpg->tv_tpg_mutex);
2030 continue;
2031 }
2032 if (tpg->tv_tpg_vhost_count != 0) {
2033 mutex_unlock(&tpg->tv_tpg_mutex);
2034 continue;
2035 }
2036 tv_tport = tpg->tport;
2037
2038 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
2039 /*
2040 * In order to ensure individual vhost-scsi configfs
2041 * groups cannot be removed while in use by vhost ioctl,
2042 * go ahead and take an explicit se_tpg->tpg_group.cg_item
2043 * dependency now.
2044 */
2045 se_tpg = &tpg->se_tpg;
2046 ret = target_depend_item(&se_tpg->tpg_group.cg_item);
2047 if (ret) {
2048 pr_warn("target_depend_item() failed: %d\n", ret);
2049 mutex_unlock(&tpg->tv_tpg_mutex);
2050 mutex_unlock(&vhost_scsi_mutex);
2051 goto undepend;
2052 }
2053 tpg->tv_tpg_vhost_count++;
2054 tpg->vhost_scsi = vs;
2055 vs_tpg[tpg->tport_tpgt] = tpg;
2056 match = true;
2057 }
2058 mutex_unlock(&tpg->tv_tpg_mutex);
2059 }
2060 mutex_unlock(&vhost_scsi_mutex);
2061
2062 if (match) {
2063 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
2064 sizeof(vs->vs_vhost_wwpn));
2065
2066 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) {
2067 vq = &vs->vqs[i].vq;
2068 if (!vhost_vq_is_setup(vq))
2069 continue;
2070
2071 ret = vhost_scsi_setup_vq_cmds(vq, vq->num);
2072 if (ret)
2073 goto destroy_vq_cmds;
2074 }
2075
2076 for (i = 0; i < vs->dev.nvqs; i++) {
2077 vq = &vs->vqs[i].vq;
2078 mutex_lock(&vq->mutex);
2079 vhost_vq_set_backend(vq, vs_tpg);
2080 vhost_vq_init_access(vq);
2081 mutex_unlock(&vq->mutex);
2082 }
2083 ret = 0;
2084 } else {
2085 ret = -ENODEV;
2086 goto free_tpg;
2087 }
2088
2089 /*
2090 * Act as synchronize_rcu to make sure requests after this point
2091 * see a fully setup device.
2092 */
2093 vhost_scsi_flush(vs);
2094 vs->vs_tpg = vs_tpg;
2095 goto out;
2096
2097 destroy_vq_cmds:
2098 for (i--; i >= VHOST_SCSI_VQ_IO; i--) {
2099 if (!vhost_vq_get_backend(&vs->vqs[i].vq))
2100 vhost_scsi_destroy_vq_cmds(&vs->vqs[i].vq);
2101 }
2102 undepend:
2103 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
2104 tpg = vs_tpg[i];
2105 if (tpg) {
2106 mutex_lock(&tpg->tv_tpg_mutex);
2107 tpg->vhost_scsi = NULL;
2108 tpg->tv_tpg_vhost_count--;
2109 mutex_unlock(&tpg->tv_tpg_mutex);
2110 target_undepend_item(&tpg->se_tpg.tpg_group.cg_item);
2111 }
2112 }
2113 free_tpg:
2114 kfree(vs_tpg);
2115 out:
2116 mutex_unlock(&vs->dev.mutex);
2117 return ret;
2118 }
2119
2120 static int
vhost_scsi_clear_endpoint(struct vhost_scsi * vs,struct vhost_scsi_target * t)2121 vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
2122 struct vhost_scsi_target *t)
2123 {
2124 struct se_portal_group *se_tpg;
2125 struct vhost_scsi_tport *tv_tport;
2126 struct vhost_scsi_tpg *tpg;
2127 struct vhost_virtqueue *vq;
2128 bool match = false;
2129 int index, ret, i;
2130 u8 target;
2131
2132 mutex_lock(&vs->dev.mutex);
2133 /* Verify that ring has been setup correctly. */
2134 for (index = 0; index < vs->dev.nvqs; ++index) {
2135 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
2136 ret = -EFAULT;
2137 goto err_dev;
2138 }
2139 }
2140
2141 if (!vs->vs_tpg) {
2142 ret = 0;
2143 goto err_dev;
2144 }
2145
2146 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
2147 target = i;
2148 tpg = vs->vs_tpg[target];
2149 if (!tpg)
2150 continue;
2151
2152 tv_tport = tpg->tport;
2153 if (!tv_tport) {
2154 ret = -ENODEV;
2155 goto err_dev;
2156 }
2157
2158 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
2159 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
2160 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
2161 tv_tport->tport_name, tpg->tport_tpgt,
2162 t->vhost_wwpn, t->vhost_tpgt);
2163 ret = -EINVAL;
2164 goto err_dev;
2165 }
2166 match = true;
2167 }
2168 if (!match)
2169 goto free_vs_tpg;
2170
2171 /* Prevent new cmds from starting and accessing the tpgs/sessions */
2172 for (i = 0; i < vs->dev.nvqs; i++) {
2173 vq = &vs->vqs[i].vq;
2174 mutex_lock(&vq->mutex);
2175 vhost_vq_set_backend(vq, NULL);
2176 mutex_unlock(&vq->mutex);
2177 }
2178 /* Make sure cmds are not running before tearing them down. */
2179 vhost_scsi_flush(vs);
2180
2181 for (i = 0; i < vs->dev.nvqs; i++) {
2182 vq = &vs->vqs[i].vq;
2183 vhost_scsi_destroy_vq_cmds(vq);
2184 }
2185
2186 /*
2187 * We can now release our hold on the tpg and sessions and userspace
2188 * can free them after this point.
2189 */
2190 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
2191 target = i;
2192 tpg = vs->vs_tpg[target];
2193 if (!tpg)
2194 continue;
2195
2196 mutex_lock(&tpg->tv_tpg_mutex);
2197
2198 tpg->tv_tpg_vhost_count--;
2199 tpg->vhost_scsi = NULL;
2200 vs->vs_tpg[target] = NULL;
2201
2202 mutex_unlock(&tpg->tv_tpg_mutex);
2203
2204 se_tpg = &tpg->se_tpg;
2205 target_undepend_item(&se_tpg->tpg_group.cg_item);
2206 }
2207
2208 free_vs_tpg:
2209 /*
2210 * Act as synchronize_rcu to make sure access to
2211 * old vs->vs_tpg is finished.
2212 */
2213 vhost_scsi_flush(vs);
2214 kfree(vs->vs_tpg);
2215 vs->vs_tpg = NULL;
2216 memset(vs->vs_vhost_wwpn, 0, sizeof(vs->vs_vhost_wwpn));
2217 WARN_ON(vs->vs_events_nr);
2218 mutex_unlock(&vs->dev.mutex);
2219 return 0;
2220
2221 err_dev:
2222 mutex_unlock(&vs->dev.mutex);
2223 return ret;
2224 }
2225
vhost_scsi_set_features(struct vhost_scsi * vs,u64 features)2226 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
2227 {
2228 struct vhost_virtqueue *vq;
2229 bool is_log, was_log;
2230 u64 old_features;
2231 int i;
2232
2233 if (features & ~VHOST_SCSI_FEATURES)
2234 return -EOPNOTSUPP;
2235
2236 mutex_lock(&vs->dev.mutex);
2237 if ((features & (1 << VHOST_F_LOG_ALL)) &&
2238 !vhost_log_access_ok(&vs->dev)) {
2239 mutex_unlock(&vs->dev.mutex);
2240 return -EFAULT;
2241 }
2242
2243 if (!vs->dev.nvqs)
2244 goto out;
2245
2246 old_features = vs->vqs[0].vq.acked_features;
2247 if (vs->vs_tpg &&
2248 ((features ^ old_features) &
2249 ~(1ULL << VHOST_F_LOG_ALL))) {
2250 mutex_unlock(&vs->dev.mutex);
2251 return -EBUSY;
2252 }
2253
2254 is_log = features & (1 << VHOST_F_LOG_ALL);
2255 /*
2256 * All VQs should have same feature.
2257 */
2258 was_log = vhost_has_feature(&vs->vqs[0].vq, VHOST_F_LOG_ALL);
2259
2260 for (i = 0; i < vs->dev.nvqs; i++) {
2261 vq = &vs->vqs[i].vq;
2262 mutex_lock(&vq->mutex);
2263 vq->acked_features = features;
2264 mutex_unlock(&vq->mutex);
2265 }
2266
2267 /*
2268 * If VHOST_F_LOG_ALL is removed, free tvc_log after
2269 * vq->acked_features is committed.
2270 */
2271 if (!is_log && was_log) {
2272 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) {
2273 if (!vs->vqs[i].scsi_cmds)
2274 continue;
2275
2276 vq = &vs->vqs[i].vq;
2277 mutex_lock(&vq->mutex);
2278 vhost_scsi_destroy_vq_log(vq);
2279 mutex_unlock(&vq->mutex);
2280 }
2281 }
2282
2283 out:
2284 mutex_unlock(&vs->dev.mutex);
2285 return 0;
2286 }
2287
vhost_scsi_open(struct inode * inode,struct file * f)2288 static int vhost_scsi_open(struct inode *inode, struct file *f)
2289 {
2290 struct vhost_scsi_virtqueue *svq;
2291 struct vhost_scsi *vs;
2292 struct vhost_virtqueue **vqs;
2293 int r = -ENOMEM, i, nvqs = vhost_scsi_max_io_vqs;
2294
2295 vs = kvzalloc_obj(*vs);
2296 if (!vs)
2297 goto err_vs;
2298 vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
2299
2300 if (nvqs > VHOST_SCSI_MAX_IO_VQ) {
2301 pr_err("Invalid max_io_vqs of %d. Using %d.\n", nvqs,
2302 VHOST_SCSI_MAX_IO_VQ);
2303 nvqs = VHOST_SCSI_MAX_IO_VQ;
2304 } else if (nvqs == 0) {
2305 pr_err("Invalid max_io_vqs of %d. Using 1.\n", nvqs);
2306 nvqs = 1;
2307 }
2308 nvqs += VHOST_SCSI_VQ_IO;
2309
2310 vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
2311 GFP_KERNEL | __GFP_ZERO);
2312 if (!vs->old_inflight)
2313 goto err_inflight;
2314
2315 vs->vqs = kmalloc_objs(*vs->vqs, nvqs, GFP_KERNEL | __GFP_ZERO);
2316 if (!vs->vqs)
2317 goto err_vqs;
2318
2319 vqs = kmalloc_objs(*vqs, nvqs);
2320 if (!vqs)
2321 goto err_local_vqs;
2322
2323 vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
2324
2325 vs->vs_events_nr = 0;
2326 vs->vs_events_missed = false;
2327
2328 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
2329 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
2330 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
2331 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
2332 for (i = VHOST_SCSI_VQ_IO; i < nvqs; i++) {
2333 svq = &vs->vqs[i];
2334
2335 vqs[i] = &svq->vq;
2336 svq->vs = vs;
2337 init_llist_head(&svq->completion_list);
2338 vhost_work_init(&svq->completion_work,
2339 vhost_scsi_complete_cmd_work);
2340 svq->vq.handle_kick = vhost_scsi_handle_kick;
2341 }
2342 vhost_dev_init(&vs->dev, vqs, nvqs, UIO_MAXIOV,
2343 VHOST_SCSI_WEIGHT, 0, true, NULL);
2344
2345 vhost_scsi_init_inflight(vs, NULL);
2346
2347 f->private_data = vs;
2348 return 0;
2349
2350 err_local_vqs:
2351 kfree(vs->vqs);
2352 err_vqs:
2353 kfree(vs->old_inflight);
2354 err_inflight:
2355 kvfree(vs);
2356 err_vs:
2357 return r;
2358 }
2359
vhost_scsi_release(struct inode * inode,struct file * f)2360 static int vhost_scsi_release(struct inode *inode, struct file *f)
2361 {
2362 struct vhost_scsi *vs = f->private_data;
2363 struct vhost_scsi_target t;
2364
2365 mutex_lock(&vs->dev.mutex);
2366 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
2367 mutex_unlock(&vs->dev.mutex);
2368 vhost_scsi_clear_endpoint(vs, &t);
2369 vhost_dev_stop(&vs->dev);
2370 vhost_dev_cleanup(&vs->dev);
2371 kfree(vs->dev.vqs);
2372 kfree(vs->vqs);
2373 kfree(vs->old_inflight);
2374 kvfree(vs);
2375 return 0;
2376 }
2377
2378 static long
vhost_scsi_ioctl(struct file * f,unsigned int ioctl,unsigned long arg)2379 vhost_scsi_ioctl(struct file *f,
2380 unsigned int ioctl,
2381 unsigned long arg)
2382 {
2383 struct vhost_scsi *vs = f->private_data;
2384 struct vhost_scsi_target backend;
2385 void __user *argp = (void __user *)arg;
2386 u64 __user *featurep = argp;
2387 u32 __user *eventsp = argp;
2388 u32 events_missed;
2389 u64 features;
2390 int r, abi_version = VHOST_SCSI_ABI_VERSION;
2391 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
2392
2393 switch (ioctl) {
2394 case VHOST_SCSI_SET_ENDPOINT:
2395 if (copy_from_user(&backend, argp, sizeof backend))
2396 return -EFAULT;
2397 if (backend.reserved != 0)
2398 return -EOPNOTSUPP;
2399
2400 return vhost_scsi_set_endpoint(vs, &backend);
2401 case VHOST_SCSI_CLEAR_ENDPOINT:
2402 if (copy_from_user(&backend, argp, sizeof backend))
2403 return -EFAULT;
2404 if (backend.reserved != 0)
2405 return -EOPNOTSUPP;
2406
2407 return vhost_scsi_clear_endpoint(vs, &backend);
2408 case VHOST_SCSI_GET_ABI_VERSION:
2409 if (copy_to_user(argp, &abi_version, sizeof abi_version))
2410 return -EFAULT;
2411 return 0;
2412 case VHOST_SCSI_SET_EVENTS_MISSED:
2413 if (get_user(events_missed, eventsp))
2414 return -EFAULT;
2415 mutex_lock(&vq->mutex);
2416 vs->vs_events_missed = events_missed;
2417 mutex_unlock(&vq->mutex);
2418 return 0;
2419 case VHOST_SCSI_GET_EVENTS_MISSED:
2420 mutex_lock(&vq->mutex);
2421 events_missed = vs->vs_events_missed;
2422 mutex_unlock(&vq->mutex);
2423 if (put_user(events_missed, eventsp))
2424 return -EFAULT;
2425 return 0;
2426 case VHOST_GET_FEATURES:
2427 features = VHOST_SCSI_FEATURES;
2428 if (copy_to_user(featurep, &features, sizeof features))
2429 return -EFAULT;
2430 return 0;
2431 case VHOST_SET_FEATURES:
2432 if (copy_from_user(&features, featurep, sizeof features))
2433 return -EFAULT;
2434 return vhost_scsi_set_features(vs, features);
2435 case VHOST_NEW_WORKER:
2436 case VHOST_FREE_WORKER:
2437 case VHOST_ATTACH_VRING_WORKER:
2438 case VHOST_GET_VRING_WORKER:
2439 mutex_lock(&vs->dev.mutex);
2440 r = vhost_worker_ioctl(&vs->dev, ioctl, argp);
2441 mutex_unlock(&vs->dev.mutex);
2442 return r;
2443 default:
2444 mutex_lock(&vs->dev.mutex);
2445 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
2446 if (r == -ENOIOCTLCMD)
2447 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
2448 else
2449 vhost_scsi_flush(vs);
2450 mutex_unlock(&vs->dev.mutex);
2451 return r;
2452 }
2453 }
2454
2455 static const struct file_operations vhost_scsi_fops = {
2456 .owner = THIS_MODULE,
2457 .release = vhost_scsi_release,
2458 .unlocked_ioctl = vhost_scsi_ioctl,
2459 .compat_ioctl = compat_ptr_ioctl,
2460 .open = vhost_scsi_open,
2461 .llseek = noop_llseek,
2462 };
2463
2464 static struct miscdevice vhost_scsi_misc = {
2465 MISC_DYNAMIC_MINOR,
2466 "vhost-scsi",
2467 &vhost_scsi_fops,
2468 };
2469
vhost_scsi_register(void)2470 static int __init vhost_scsi_register(void)
2471 {
2472 return misc_register(&vhost_scsi_misc);
2473 }
2474
vhost_scsi_deregister(void)2475 static void vhost_scsi_deregister(void)
2476 {
2477 misc_deregister(&vhost_scsi_misc);
2478 }
2479
vhost_scsi_dump_proto_id(struct vhost_scsi_tport * tport)2480 static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
2481 {
2482 switch (tport->tport_proto_id) {
2483 case SCSI_PROTOCOL_SAS:
2484 return "SAS";
2485 case SCSI_PROTOCOL_FCP:
2486 return "FCP";
2487 case SCSI_PROTOCOL_ISCSI:
2488 return "iSCSI";
2489 default:
2490 break;
2491 }
2492
2493 return "Unknown";
2494 }
2495
2496 static void
vhost_scsi_do_plug(struct vhost_scsi_tpg * tpg,struct se_lun * lun,bool plug)2497 vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
2498 struct se_lun *lun, bool plug)
2499 {
2500
2501 struct vhost_scsi *vs = tpg->vhost_scsi;
2502 struct vhost_virtqueue *vq;
2503 u32 reason;
2504
2505 if (!vs)
2506 return;
2507
2508 if (plug)
2509 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
2510 else
2511 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
2512
2513 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
2514 mutex_lock(&vq->mutex);
2515 /*
2516 * We can't queue events if the backend has been cleared, because
2517 * we could end up queueing an event after the flush.
2518 */
2519 if (!vhost_vq_get_backend(vq))
2520 goto unlock;
2521
2522 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
2523 vhost_scsi_send_evt(vs, vq, tpg, lun,
2524 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
2525 unlock:
2526 mutex_unlock(&vq->mutex);
2527 }
2528
vhost_scsi_hotplug(struct vhost_scsi_tpg * tpg,struct se_lun * lun)2529 static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
2530 {
2531 vhost_scsi_do_plug(tpg, lun, true);
2532 }
2533
vhost_scsi_hotunplug(struct vhost_scsi_tpg * tpg,struct se_lun * lun)2534 static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
2535 {
2536 vhost_scsi_do_plug(tpg, lun, false);
2537 }
2538
vhost_scsi_port_link(struct se_portal_group * se_tpg,struct se_lun * lun)2539 static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
2540 struct se_lun *lun)
2541 {
2542 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2543 struct vhost_scsi_tpg, se_tpg);
2544
2545 mutex_lock(&tpg->tv_tpg_mutex);
2546 tpg->tv_tpg_port_count++;
2547 vhost_scsi_hotplug(tpg, lun);
2548 mutex_unlock(&tpg->tv_tpg_mutex);
2549
2550 return 0;
2551 }
2552
vhost_scsi_port_unlink(struct se_portal_group * se_tpg,struct se_lun * lun)2553 static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
2554 struct se_lun *lun)
2555 {
2556 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2557 struct vhost_scsi_tpg, se_tpg);
2558
2559 mutex_lock(&tpg->tv_tpg_mutex);
2560 tpg->tv_tpg_port_count--;
2561 vhost_scsi_hotunplug(tpg, lun);
2562 mutex_unlock(&tpg->tv_tpg_mutex);
2563 }
2564
vhost_scsi_tpg_attrib_fabric_prot_type_store(struct config_item * item,const char * page,size_t count)2565 static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store(
2566 struct config_item *item, const char *page, size_t count)
2567 {
2568 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2569 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2570 struct vhost_scsi_tpg, se_tpg);
2571 unsigned long val;
2572 int ret = kstrtoul(page, 0, &val);
2573
2574 if (ret) {
2575 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
2576 return ret;
2577 }
2578 if (val != 0 && val != 1 && val != 3) {
2579 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
2580 return -EINVAL;
2581 }
2582 tpg->tv_fabric_prot_type = val;
2583
2584 return count;
2585 }
2586
vhost_scsi_tpg_attrib_fabric_prot_type_show(struct config_item * item,char * page)2587 static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show(
2588 struct config_item *item, char *page)
2589 {
2590 struct se_portal_group *se_tpg = attrib_to_tpg(item);
2591 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2592 struct vhost_scsi_tpg, se_tpg);
2593
2594 return sysfs_emit(page, "%d\n", tpg->tv_fabric_prot_type);
2595 }
2596
2597 CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type);
2598
2599 static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
2600 &vhost_scsi_tpg_attrib_attr_fabric_prot_type,
2601 NULL,
2602 };
2603
vhost_scsi_make_nexus(struct vhost_scsi_tpg * tpg,const char * name)2604 static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
2605 const char *name)
2606 {
2607 struct vhost_scsi_nexus *tv_nexus;
2608
2609 mutex_lock(&tpg->tv_tpg_mutex);
2610 if (tpg->tpg_nexus) {
2611 mutex_unlock(&tpg->tv_tpg_mutex);
2612 pr_debug("tpg->tpg_nexus already exists\n");
2613 return -EEXIST;
2614 }
2615
2616 tv_nexus = kzalloc_obj(*tv_nexus);
2617 if (!tv_nexus) {
2618 mutex_unlock(&tpg->tv_tpg_mutex);
2619 pr_err("Unable to allocate struct vhost_scsi_nexus\n");
2620 return -ENOMEM;
2621 }
2622 /*
2623 * Since we are running in 'demo mode' this call will generate a
2624 * struct se_node_acl for the vhost_scsi struct se_portal_group with
2625 * the SCSI Initiator port name of the passed configfs group 'name'.
2626 */
2627 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 0, 0,
2628 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
2629 (unsigned char *)name, tv_nexus, NULL);
2630 if (IS_ERR(tv_nexus->tvn_se_sess)) {
2631 mutex_unlock(&tpg->tv_tpg_mutex);
2632 kfree(tv_nexus);
2633 return -ENOMEM;
2634 }
2635 tpg->tpg_nexus = tv_nexus;
2636
2637 mutex_unlock(&tpg->tv_tpg_mutex);
2638 return 0;
2639 }
2640
vhost_scsi_drop_nexus(struct vhost_scsi_tpg * tpg)2641 static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
2642 {
2643 struct se_session *se_sess;
2644 struct vhost_scsi_nexus *tv_nexus;
2645
2646 mutex_lock(&tpg->tv_tpg_mutex);
2647 tv_nexus = tpg->tpg_nexus;
2648 if (!tv_nexus) {
2649 mutex_unlock(&tpg->tv_tpg_mutex);
2650 return -ENODEV;
2651 }
2652
2653 se_sess = tv_nexus->tvn_se_sess;
2654 if (!se_sess) {
2655 mutex_unlock(&tpg->tv_tpg_mutex);
2656 return -ENODEV;
2657 }
2658
2659 if (tpg->tv_tpg_port_count != 0) {
2660 mutex_unlock(&tpg->tv_tpg_mutex);
2661 pr_err("Unable to remove TCM_vhost I_T Nexus with"
2662 " active TPG port count: %d\n",
2663 tpg->tv_tpg_port_count);
2664 return -EBUSY;
2665 }
2666
2667 if (tpg->tv_tpg_vhost_count != 0) {
2668 mutex_unlock(&tpg->tv_tpg_mutex);
2669 pr_err("Unable to remove TCM_vhost I_T Nexus with"
2670 " active TPG vhost count: %d\n",
2671 tpg->tv_tpg_vhost_count);
2672 return -EBUSY;
2673 }
2674
2675 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
2676 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
2677 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
2678
2679 /*
2680 * Release the SCSI I_T Nexus to the emulated vhost Target Port
2681 */
2682 target_remove_session(se_sess);
2683 tpg->tpg_nexus = NULL;
2684 mutex_unlock(&tpg->tv_tpg_mutex);
2685
2686 kfree(tv_nexus);
2687 return 0;
2688 }
2689
vhost_scsi_tpg_nexus_show(struct config_item * item,char * page)2690 static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page)
2691 {
2692 struct se_portal_group *se_tpg = to_tpg(item);
2693 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2694 struct vhost_scsi_tpg, se_tpg);
2695 struct vhost_scsi_nexus *tv_nexus;
2696 ssize_t ret;
2697
2698 mutex_lock(&tpg->tv_tpg_mutex);
2699 tv_nexus = tpg->tpg_nexus;
2700 if (!tv_nexus) {
2701 mutex_unlock(&tpg->tv_tpg_mutex);
2702 return -ENODEV;
2703 }
2704 ret = sysfs_emit(page, "%s\n",
2705 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
2706 mutex_unlock(&tpg->tv_tpg_mutex);
2707
2708 return ret;
2709 }
2710
vhost_scsi_tpg_nexus_store(struct config_item * item,const char * page,size_t count)2711 static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item,
2712 const char *page, size_t count)
2713 {
2714 struct se_portal_group *se_tpg = to_tpg(item);
2715 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2716 struct vhost_scsi_tpg, se_tpg);
2717 struct vhost_scsi_tport *tport_wwn = tpg->tport;
2718 unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
2719 int ret;
2720 /*
2721 * Shutdown the active I_T nexus if 'NULL' is passed..
2722 */
2723 if (!strncmp(page, "NULL", 4)) {
2724 ret = vhost_scsi_drop_nexus(tpg);
2725 return (!ret) ? count : ret;
2726 }
2727 /*
2728 * Otherwise make sure the passed virtual Initiator port WWN matches
2729 * the fabric protocol_id set in vhost_scsi_make_tport(), and call
2730 * vhost_scsi_make_nexus().
2731 */
2732 if (strlen(page) >= VHOST_SCSI_NAMELEN) {
2733 pr_err("Emulated NAA Sas Address: %s, exceeds"
2734 " max: %d\n", page, VHOST_SCSI_NAMELEN);
2735 return -EINVAL;
2736 }
2737 snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
2738
2739 ptr = strstr(i_port, "naa.");
2740 if (ptr) {
2741 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2742 pr_err("Passed SAS Initiator Port %s does not"
2743 " match target port protoid: %s\n", i_port,
2744 vhost_scsi_dump_proto_id(tport_wwn));
2745 return -EINVAL;
2746 }
2747 port_ptr = &i_port[0];
2748 goto check_newline;
2749 }
2750 ptr = strstr(i_port, "fc.");
2751 if (ptr) {
2752 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2753 pr_err("Passed FCP Initiator Port %s does not"
2754 " match target port protoid: %s\n", i_port,
2755 vhost_scsi_dump_proto_id(tport_wwn));
2756 return -EINVAL;
2757 }
2758 port_ptr = &i_port[3]; /* Skip over "fc." */
2759 goto check_newline;
2760 }
2761 ptr = strstr(i_port, "iqn.");
2762 if (ptr) {
2763 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2764 pr_err("Passed iSCSI Initiator Port %s does not"
2765 " match target port protoid: %s\n", i_port,
2766 vhost_scsi_dump_proto_id(tport_wwn));
2767 return -EINVAL;
2768 }
2769 port_ptr = &i_port[0];
2770 goto check_newline;
2771 }
2772 pr_err("Unable to locate prefix for emulated Initiator Port:"
2773 " %s\n", i_port);
2774 return -EINVAL;
2775 /*
2776 * Clear any trailing newline for the NAA WWN
2777 */
2778 check_newline:
2779 if (i_port[strlen(i_port)-1] == '\n')
2780 i_port[strlen(i_port)-1] = '\0';
2781
2782 ret = vhost_scsi_make_nexus(tpg, port_ptr);
2783 if (ret < 0)
2784 return ret;
2785
2786 return count;
2787 }
2788
2789 CONFIGFS_ATTR(vhost_scsi_tpg_, nexus);
2790
2791 static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
2792 &vhost_scsi_tpg_attr_nexus,
2793 NULL,
2794 };
2795
2796 static struct se_portal_group *
vhost_scsi_make_tpg(struct se_wwn * wwn,const char * name)2797 vhost_scsi_make_tpg(struct se_wwn *wwn, const char *name)
2798 {
2799 struct vhost_scsi_tport *tport = container_of(wwn,
2800 struct vhost_scsi_tport, tport_wwn);
2801
2802 struct vhost_scsi_tpg *tpg;
2803 u16 tpgt;
2804 int ret;
2805
2806 if (strstr(name, "tpgt_") != name)
2807 return ERR_PTR(-EINVAL);
2808 if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
2809 return ERR_PTR(-EINVAL);
2810
2811 tpg = kzalloc_obj(*tpg);
2812 if (!tpg) {
2813 pr_err("Unable to allocate struct vhost_scsi_tpg");
2814 return ERR_PTR(-ENOMEM);
2815 }
2816 mutex_init(&tpg->tv_tpg_mutex);
2817 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2818 tpg->tport = tport;
2819 tpg->tport_tpgt = tpgt;
2820
2821 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
2822 if (ret < 0) {
2823 kfree(tpg);
2824 return NULL;
2825 }
2826 mutex_lock(&vhost_scsi_mutex);
2827 list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
2828 mutex_unlock(&vhost_scsi_mutex);
2829
2830 return &tpg->se_tpg;
2831 }
2832
vhost_scsi_drop_tpg(struct se_portal_group * se_tpg)2833 static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
2834 {
2835 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2836 struct vhost_scsi_tpg, se_tpg);
2837
2838 mutex_lock(&vhost_scsi_mutex);
2839 list_del(&tpg->tv_tpg_list);
2840 mutex_unlock(&vhost_scsi_mutex);
2841 /*
2842 * Release the virtual I_T Nexus for this vhost TPG
2843 */
2844 vhost_scsi_drop_nexus(tpg);
2845 /*
2846 * Deregister the se_tpg from TCM..
2847 */
2848 core_tpg_deregister(se_tpg);
2849 kfree(tpg);
2850 }
2851
2852 static struct se_wwn *
vhost_scsi_make_tport(struct target_fabric_configfs * tf,struct config_group * group,const char * name)2853 vhost_scsi_make_tport(struct target_fabric_configfs *tf,
2854 struct config_group *group,
2855 const char *name)
2856 {
2857 struct vhost_scsi_tport *tport;
2858 char *ptr;
2859 u64 wwpn = 0;
2860 int off = 0;
2861
2862 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
2863 return ERR_PTR(-EINVAL); */
2864
2865 tport = kzalloc_obj(*tport);
2866 if (!tport) {
2867 pr_err("Unable to allocate struct vhost_scsi_tport");
2868 return ERR_PTR(-ENOMEM);
2869 }
2870 tport->tport_wwpn = wwpn;
2871 /*
2872 * Determine the emulated Protocol Identifier and Target Port Name
2873 * based on the incoming configfs directory name.
2874 */
2875 ptr = strstr(name, "naa.");
2876 if (ptr) {
2877 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2878 goto check_len;
2879 }
2880 ptr = strstr(name, "fc.");
2881 if (ptr) {
2882 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2883 off = 3; /* Skip over "fc." */
2884 goto check_len;
2885 }
2886 ptr = strstr(name, "iqn.");
2887 if (ptr) {
2888 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2889 goto check_len;
2890 }
2891
2892 pr_err("Unable to locate prefix for emulated Target Port:"
2893 " %s\n", name);
2894 kfree(tport);
2895 return ERR_PTR(-EINVAL);
2896
2897 check_len:
2898 if (strlen(name) >= VHOST_SCSI_NAMELEN) {
2899 pr_err("Emulated %s Address: %s, exceeds"
2900 " max: %d\n", vhost_scsi_dump_proto_id(tport), name,
2901 VHOST_SCSI_NAMELEN);
2902 kfree(tport);
2903 return ERR_PTR(-EINVAL);
2904 }
2905 snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
2906
2907 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2908 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
2909
2910 return &tport->tport_wwn;
2911 }
2912
vhost_scsi_drop_tport(struct se_wwn * wwn)2913 static void vhost_scsi_drop_tport(struct se_wwn *wwn)
2914 {
2915 struct vhost_scsi_tport *tport = container_of(wwn,
2916 struct vhost_scsi_tport, tport_wwn);
2917
2918 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2919 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
2920 tport->tport_name);
2921
2922 kfree(tport);
2923 }
2924
2925 static ssize_t
vhost_scsi_wwn_version_show(struct config_item * item,char * page)2926 vhost_scsi_wwn_version_show(struct config_item *item, char *page)
2927 {
2928 return sysfs_emit(page, "TCM_VHOST fabric module %s on %s/%s"
2929 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
2930 utsname()->machine);
2931 }
2932
2933 CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version);
2934
2935 static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
2936 &vhost_scsi_wwn_attr_version,
2937 NULL,
2938 };
2939
2940 static const struct target_core_fabric_ops vhost_scsi_ops = {
2941 .module = THIS_MODULE,
2942 .fabric_name = "vhost",
2943 .max_data_sg_nents = VHOST_SCSI_PREALLOC_SGLS,
2944 .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
2945 .tpg_get_tag = vhost_scsi_get_tpgt,
2946 .tpg_check_demo_mode = vhost_scsi_check_true,
2947 .tpg_check_demo_mode_cache = vhost_scsi_check_true,
2948 .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only,
2949 .release_cmd = vhost_scsi_release_cmd,
2950 .check_stop_free = vhost_scsi_check_stop_free,
2951 .sess_get_initiator_sid = NULL,
2952 .write_pending = vhost_scsi_write_pending,
2953 .queue_data_in = vhost_scsi_queue_data_in,
2954 .queue_status = vhost_scsi_queue_status,
2955 .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
2956 .aborted_task = vhost_scsi_aborted_task,
2957 /*
2958 * Setup callers for generic logic in target_core_fabric_configfs.c
2959 */
2960 .fabric_make_wwn = vhost_scsi_make_tport,
2961 .fabric_drop_wwn = vhost_scsi_drop_tport,
2962 .fabric_make_tpg = vhost_scsi_make_tpg,
2963 .fabric_drop_tpg = vhost_scsi_drop_tpg,
2964 .fabric_post_link = vhost_scsi_port_link,
2965 .fabric_pre_unlink = vhost_scsi_port_unlink,
2966
2967 .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
2968 .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
2969 .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
2970
2971 .default_compl_type = TARGET_QUEUE_COMPL,
2972 .direct_compl_supp = 1,
2973 .default_submit_type = TARGET_QUEUE_SUBMIT,
2974 .direct_submit_supp = 1,
2975 };
2976
vhost_scsi_init(void)2977 static int __init vhost_scsi_init(void)
2978 {
2979 int ret = -ENOMEM;
2980
2981 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2982 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
2983 utsname()->machine);
2984
2985 ret = vhost_scsi_register();
2986 if (ret < 0)
2987 goto out;
2988
2989 ret = target_register_template(&vhost_scsi_ops);
2990 if (ret < 0)
2991 goto out_vhost_scsi_deregister;
2992
2993 return 0;
2994
2995 out_vhost_scsi_deregister:
2996 vhost_scsi_deregister();
2997 out:
2998 return ret;
2999 }
3000
vhost_scsi_exit(void)3001 static void vhost_scsi_exit(void)
3002 {
3003 target_unregister_template(&vhost_scsi_ops);
3004 vhost_scsi_deregister();
3005 }
3006
3007 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
3008 MODULE_ALIAS("tcm_vhost");
3009 MODULE_LICENSE("GPL");
3010 module_init(vhost_scsi_init);
3011 module_exit(vhost_scsi_exit);
3012