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