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