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