1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Intel MIC Platform Software Stack (MPSS)
4  *
5  * Copyright(c) 2016 Intel Corporation.
6  *
7  * Intel Virtio Over PCIe (VOP) driver.
8  */
9 #ifndef _VOP_MAIN_H_
10 #define _VOP_MAIN_H_
11 
12 #include <linux/vringh.h>
13 #include <linux/virtio_config.h>
14 #include <linux/virtio.h>
15 #include <linux/miscdevice.h>
16 
17 #include <linux/mic_common.h>
18 #include "../common/mic_dev.h"
19 
20 #include "../bus/vop_bus.h"
21 
22 /*
23  * Note on endianness.
24  * 1. Host can be both BE or LE
25  * 2. Guest/card is LE. Host uses le_to_cpu to access desc/avail
26  *    rings and ioreadXX/iowriteXX to access used ring.
27  * 3. Device page exposed by host to guest contains LE values. Guest
28  *    accesses these using ioreadXX/iowriteXX etc. This way in general we
29  *    obey the virtio spec according to which guest works with native
30  *    endianness and host is aware of guest endianness and does all
31  *    required endianness conversion.
32  * 4. Data provided from user space to guest (in ADD_DEVICE and
33  *    CONFIG_CHANGE ioctl's) is not interpreted by the driver and should be
34  *    in guest endianness.
35  */
36 
37 /*
38  * vop_info - Allocated per invocation of VOP probe
39  *
40  * @vpdev: VOP device
41  * @hotplug_work: Handle virtio device creation, deletion and configuration
42  * @cookie: Cookie received upon requesting a virtio configuration interrupt
43  * @h2c_config_db: The doorbell used by the peer to indicate a config change
44  * @vdev_list: List of "active" virtio devices injected in the peer node
45  * @vop_mutex: Synchronize access to the device page as well as serialize
46  *             creation/deletion of virtio devices on the peer node
47  * @dp: Peer device page information
48  * @dbg: Debugfs entry
49  * @dma_ch: The DMA channel used by this transport for data transfers.
50  * @name: Name for this transport used in misc device creation.
51  * @miscdev: The misc device registered.
52  */
53 struct vop_info {
54 	struct vop_device *vpdev;
55 	struct work_struct hotplug_work;
56 	struct mic_irq *cookie;
57 	int h2c_config_db;
58 	struct list_head vdev_list;
59 	struct mutex vop_mutex;
60 	void __iomem *dp;
61 	struct dentry *dbg;
62 	struct dma_chan *dma_ch;
63 	char name[16];
64 	struct miscdevice miscdev;
65 };
66 
67 /**
68  * struct vop_vringh - Virtio ring host information.
69  *
70  * @vring: The VOP vring used for setting up user space mappings.
71  * @vrh: The host VRINGH used for accessing the card vrings.
72  * @riov: The VRINGH read kernel IOV.
73  * @wiov: The VRINGH write kernel IOV.
74  * @head: The VRINGH head index address passed to vringh_getdesc_kern(..).
75  * @vr_mutex: Mutex for synchronizing access to the VRING.
76  * @buf: Temporary kernel buffer used to copy in/out data
77  * from/to the card via DMA.
78  * @buf_da: dma address of buf.
79  * @vdev: Back pointer to VOP virtio device for vringh_notify(..).
80  */
81 struct vop_vringh {
82 	struct mic_vring vring;
83 	struct vringh vrh;
84 	struct vringh_kiov riov;
85 	struct vringh_kiov wiov;
86 	u16 head;
87 	struct mutex vr_mutex;
88 	void *buf;
89 	dma_addr_t buf_da;
90 	struct vop_vdev *vdev;
91 };
92 
93 /**
94  * struct vop_vdev - Host information for a card Virtio device.
95  *
96  * @virtio_id - Virtio device id.
97  * @waitq - Waitqueue to allow ring3 apps to poll.
98  * @vpdev - pointer to VOP bus device.
99  * @poll_wake - Used for waking up threads blocked in poll.
100  * @out_bytes - Debug stats for number of bytes copied from host to card.
101  * @in_bytes - Debug stats for number of bytes copied from card to host.
102  * @out_bytes_dma - Debug stats for number of bytes copied from host to card
103  * using DMA.
104  * @in_bytes_dma - Debug stats for number of bytes copied from card to host
105  * using DMA.
106  * @tx_len_unaligned - Debug stats for number of bytes copied to the card where
107  * the transfer length did not have the required DMA alignment.
108  * @tx_dst_unaligned - Debug stats for number of bytes copied where the
109  * destination address on the card did not have the required DMA alignment.
110  * @vvr - Store per VRING data structures.
111  * @virtio_bh_work - Work struct used to schedule virtio bottom half handling.
112  * @dd - Virtio device descriptor.
113  * @dc - Virtio device control fields.
114  * @list - List of Virtio devices.
115  * @virtio_db - The doorbell used by the card to interrupt the host.
116  * @virtio_cookie - The cookie returned while requesting interrupts.
117  * @vi: Transport information.
118  * @vdev_mutex: Mutex synchronizing virtio device injection,
119  *              removal and data transfers.
120  * @destroy: Track if a virtio device is being destroyed.
121  * @deleted: The virtio device has been deleted.
122  */
123 struct vop_vdev {
124 	int virtio_id;
125 	wait_queue_head_t waitq;
126 	struct vop_device *vpdev;
127 	int poll_wake;
128 	unsigned long out_bytes;
129 	unsigned long in_bytes;
130 	unsigned long out_bytes_dma;
131 	unsigned long in_bytes_dma;
132 	unsigned long tx_len_unaligned;
133 	unsigned long tx_dst_unaligned;
134 	unsigned long rx_dst_unaligned;
135 	struct vop_vringh vvr[MIC_MAX_VRINGS];
136 	struct work_struct virtio_bh_work;
137 	struct mic_device_desc *dd;
138 	struct mic_device_ctrl *dc;
139 	struct list_head list;
140 	int virtio_db;
141 	struct mic_irq *virtio_cookie;
142 	struct vop_info *vi;
143 	struct mutex vdev_mutex;
144 	struct completion destroy;
145 	bool deleted;
146 };
147 
148 /* Helper API to check if a virtio device is running */
vop_vdevup(struct vop_vdev * vdev)149 static inline bool vop_vdevup(struct vop_vdev *vdev)
150 {
151 	return !!vdev->dd->status;
152 }
153 
154 void vop_init_debugfs(struct vop_info *vi);
155 void vop_exit_debugfs(struct vop_info *vi);
156 int vop_host_init(struct vop_info *vi);
157 void vop_host_uninit(struct vop_info *vi);
158 #endif
159