-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtio_accel-mgr.c
246 lines (222 loc) · 6.17 KB
/
virtio_accel-mgr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-License-Identifier: GPL-2.0
/* Management for virtio accel devices (refer to adf_dev_mgr.c).
*/
#include <linux/device.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/virtio.h>
#include "accel.h"
#include "virtio_accel-common.h"
static LIST_HEAD(virtaccel_table);
static uint32_t num_devices;
/* The table_lock protects the above global list and num_devices */
static DEFINE_MUTEX(table_lock);
#define VIRTIO_ACCEL_MAX_DEVICES 32
/*
* virtaccel_devmgr_add_dev() - Add vaccel_dev to the acceleration
* framework.
* @vaccel_dev: Pointer to virtio accel device.
*
* Function adds virtio accel device to the global list.
* To be used by virtio accel device specific drivers.
*
* Return: 0 on success, error code otherwise.
*/
int virtaccel_devmgr_add_dev(struct virtio_accel *vaccel_dev)
{
struct list_head *itr;
mutex_lock(&table_lock);
if (num_devices == VIRTIO_ACCEL_MAX_DEVICES) {
virtaccel_info("Only up to %d devices are supported\n",
VIRTIO_ACCEL_MAX_DEVICES);
mutex_unlock(&table_lock);
return -EFAULT;
}
list_for_each(itr, &virtaccel_table)
{
struct virtio_accel *ptr =
list_entry(itr, struct virtio_accel, list);
if (ptr == vaccel_dev) {
mutex_unlock(&table_lock);
return -EEXIST;
}
}
atomic_set(&vaccel_dev->ref_count, 0);
list_add_tail(&vaccel_dev->list, &virtaccel_table);
vaccel_dev->dev_id = num_devices++;
mutex_unlock(&table_lock);
return 0;
}
struct list_head *virtaccel_devmgr_get_head(void)
{
return &virtaccel_table;
}
/*
* virtaccel_devmgr_rm_dev() - Remove vaccel_dev from the acceleration
* framework.
* @vaccel_dev: Pointer to virtio accel device.
*
* Function removes virtio accel device from the acceleration framework.
* To be used by virtio accel device specific drivers.
*
* Return: void
*/
void virtaccel_devmgr_rm_dev(struct virtio_accel *vaccel_dev)
{
mutex_lock(&table_lock);
list_del(&vaccel_dev->list);
num_devices--;
mutex_unlock(&table_lock);
}
/*
* virtaccel_devmgr_get_first()
*
* Function returns the first virtio accel device from the acceleration
* framework.
*
* To be used by virtio accel device specific drivers.
*
* Return: pointer to vaccel_dev or NULL if not found.
*/
struct virtio_accel *virtaccel_devmgr_get_first(void)
{
struct virtio_accel *dev = NULL;
mutex_lock(&table_lock);
if (!list_empty(&virtaccel_table))
dev = list_first_entry(&virtaccel_table, struct virtio_accel,
list);
mutex_unlock(&table_lock);
return dev;
}
/*
* virtaccel_dev_in_use() - Check whether vaccel_dev is currently in use
* @vaccel_dev: Pointer to virtio accel device.
*
* To be used by virtio accel device specific drivers.
*
* Return: 1 when device is in use, 0 otherwise.
*/
int virtaccel_dev_in_use(struct virtio_accel *vaccel_dev)
{
return atomic_read(&vaccel_dev->ref_count) != 0;
}
/*
* virtaccel_dev_get() - Increment vaccel_dev reference count
* @vaccel_dev: Pointer to virtio accel device.
*
* Increment the vaccel_dev refcount and if this is the first time
* incrementing it during this period the vaccel_dev is in use,
* increment the module refcount too.
* To be used by virtio accel device specific drivers.
*
* Return: 0 when successful, EFAULT when fail to bump module refcount
*/
int virtaccel_dev_get(struct virtio_accel *vaccel_dev)
{
if (atomic_add_return(1, &vaccel_dev->ref_count) == 1)
if (!try_module_get(vaccel_dev->owner))
return -EFAULT;
return 0;
}
/*
* virtaccel_dev_put() - Decrement vaccel_dev reference count
* @vaccel_dev: Pointer to virtio accel device.
*
* Decrement the vaccel_dev refcount and if this is the last time
* decrementing it during this period the vaccel_dev is in use,
* decrement the module refcount too.
* To be used by virtio accel device specific drivers.
*
* Return: void
*/
void virtaccel_dev_put(struct virtio_accel *vaccel_dev)
{
if (atomic_sub_return(1, &vaccel_dev->ref_count) == 0)
module_put(vaccel_dev->owner);
}
/*
* virtaccel_dev_started() - Check whether device has started
* @vaccel_dev: Pointer to virtio accel device.
*
* To be used by virtio accel device specific drivers.
*
* Return: 1 when the device has started, 0 otherwise
*/
int virtaccel_dev_started(struct virtio_accel *vaccel_dev)
{
return (vaccel_dev->status & VIRTIO_ACCEL_S_HW_READY);
}
/*
* virtaccel_get_dev_node() - Get vaccel_dev on the node.
* @node: Node id the driver works.
*
* Function returns the virtio accel device used fewest on the node.
*
* To be used by virtio accel device specific drivers.
*
* Return: pointer to vaccel_dev or NULL if not found.
*/
struct virtio_accel *virtaccel_get_dev_node(int node)
{
struct virtio_accel *vaccel_dev = NULL;
struct virtio_accel *tmp_dev;
unsigned long best = ~0;
unsigned long ctr;
mutex_lock(&table_lock);
list_for_each_entry(tmp_dev, virtaccel_devmgr_get_head(), list)
{
if ((node == dev_to_node(&tmp_dev->vdev->dev) ||
dev_to_node(&tmp_dev->vdev->dev) < 0) &&
virtaccel_dev_started(tmp_dev)) {
ctr = atomic_read(&tmp_dev->ref_count);
if (best > ctr) {
vaccel_dev = tmp_dev;
best = ctr;
}
}
}
if (!vaccel_dev) {
virtaccel_info("Could not find a device on node %d\n", node);
/* Get any started device */
list_for_each_entry(tmp_dev, virtaccel_devmgr_get_head(), list)
{
if (virtaccel_dev_started(tmp_dev)) {
vaccel_dev = tmp_dev;
break;
}
}
}
mutex_unlock(&table_lock);
if (!vaccel_dev)
return NULL;
virtaccel_dev_get(vaccel_dev);
return vaccel_dev;
}
/*
* virtaccel_dev_start() - Start virtio accel device
* @vaccel: Pointer to virtio accel device.
*
* Function notifies all the registered services that the virtio accel device
* is ready to be used.
* To be used by virtio accel device specific drivers.
*
* Return: 0 on success, EFAULT when fail to register algorithms
*/
int virtaccel_dev_start(struct virtio_accel *vaccel)
{
return 0;
}
/*
* virtaccel_dev_stop() - Stop virtio accel device
* @vaccel: Pointer to virtio accel device.
*
* Function notifies all the registered services that the virtio accel device
* is ready to be used.
* To be used by virtio accel device specific drivers.
*
* Return: void
*/
void virtaccel_dev_stop(struct virtio_accel *vaccel)
{
}