]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/crypto/qat/qat_common/adf_dev_mgr.c
Merge tag 'armsoc-cleanup' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-eoan-kernel.git] / drivers / crypto / qat / qat_common / adf_dev_mgr.c
CommitLineData
d8cba25d
TS
1/*
2 This file is provided under a dual BSD/GPLv2 license. When using or
3 redistributing this file, you may do so under either license.
4
5 GPL LICENSE SUMMARY
6 Copyright(c) 2014 Intel Corporation.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of version 2 of the GNU General Public License as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 Contact Information:
17 qat-linux@intel.com
18
19 BSD LICENSE
20 Copyright(c) 2014 Intel Corporation.
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions
23 are met:
24
25 * Redistributions of source code must retain the above copyright
26 notice, this list of conditions and the following disclaimer.
27 * Redistributions in binary form must reproduce the above copyright
28 notice, this list of conditions and the following disclaimer in
29 the documentation and/or other materials provided with the
30 distribution.
31 * Neither the name of Intel Corporation nor the names of its
32 contributors may be used to endorse or promote products derived
33 from this software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46*/
47#include <linux/mutex.h>
48#include <linux/list.h>
49#include "adf_cfg.h"
50#include "adf_common_drv.h"
51
52static LIST_HEAD(accel_table);
ed8ccaef 53static LIST_HEAD(vfs_table);
d8cba25d
TS
54static DEFINE_MUTEX(table_lock);
55static uint32_t num_devices;
56
ed8ccaef
TS
57struct vf_id_map {
58 u32 bdf;
59 u32 id;
60 u32 fake_id;
61 bool attached;
62 struct list_head list;
63};
64
65static int adf_get_vf_id(struct adf_accel_dev *vf)
66{
67 return ((7 * (PCI_SLOT(accel_to_pci_dev(vf)->devfn) - 1)) +
68 PCI_FUNC(accel_to_pci_dev(vf)->devfn) +
69 (PCI_SLOT(accel_to_pci_dev(vf)->devfn) - 1));
70}
71
72static int adf_get_vf_num(struct adf_accel_dev *vf)
73{
74 return (accel_to_pci_dev(vf)->bus->number << 8) | adf_get_vf_id(vf);
75}
76
77static struct vf_id_map *adf_find_vf(u32 bdf)
78{
79 struct list_head *itr;
80
81 list_for_each(itr, &vfs_table) {
82 struct vf_id_map *ptr =
83 list_entry(itr, struct vf_id_map, list);
84
85 if (ptr->bdf == bdf)
86 return ptr;
87 }
88 return NULL;
89}
90
91static int adf_get_vf_real_id(u32 fake)
92{
93 struct list_head *itr;
94
95 list_for_each(itr, &vfs_table) {
96 struct vf_id_map *ptr =
97 list_entry(itr, struct vf_id_map, list);
98 if (ptr->fake_id == fake)
99 return ptr->id;
100 }
101 return -1;
102}
103
104/**
105 * adf_clean_vf_map() - Cleans VF id mapings
106 *
107 * Function cleans internal ids for virtual functions.
108 * @vf: flag indicating whether mappings is cleaned
109 * for vfs only or for vfs and pfs
110 */
111void adf_clean_vf_map(bool vf)
112{
113 struct vf_id_map *map;
114 struct list_head *ptr, *tmp;
115
116 mutex_lock(&table_lock);
117 list_for_each_safe(ptr, tmp, &vfs_table) {
118 map = list_entry(ptr, struct vf_id_map, list);
119 if (map->bdf != -1)
120 num_devices--;
121
122 if (vf && map->bdf == -1)
123 continue;
124
125 list_del(ptr);
126 kfree(map);
127 }
128 mutex_unlock(&table_lock);
129}
130EXPORT_SYMBOL_GPL(adf_clean_vf_map);
131
132/**
133 * adf_devmgr_update_class_index() - Update internal index
134 * @hw_data: Pointer to internal device data.
135 *
136 * Function updates internal dev index for VFs
137 */
138void adf_devmgr_update_class_index(struct adf_hw_device_data *hw_data)
139{
140 struct adf_hw_device_class *class = hw_data->dev_class;
141 struct list_head *itr;
142 int i = 0;
143
144 list_for_each(itr, &accel_table) {
145 struct adf_accel_dev *ptr =
146 list_entry(itr, struct adf_accel_dev, list);
147
148 if (ptr->hw_device->dev_class == class)
149 ptr->hw_device->instance_id = i++;
150
151 if (i == class->instances)
152 break;
153 }
154}
155EXPORT_SYMBOL_GPL(adf_devmgr_update_class_index);
156
d8cba25d
TS
157/**
158 * adf_devmgr_add_dev() - Add accel_dev to the acceleration framework
159 * @accel_dev: Pointer to acceleration device.
ed8ccaef 160 * @pf: Corresponding PF if the accel_dev is a VF
d8cba25d
TS
161 *
162 * Function adds acceleration device to the acceleration framework.
163 * To be used by QAT device specific drivers.
164 *
ec0d6fa3 165 * Return: 0 on success, error code otherwise.
d8cba25d 166 */
ed8ccaef
TS
167int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev,
168 struct adf_accel_dev *pf)
d8cba25d
TS
169{
170 struct list_head *itr;
ed8ccaef 171 int ret = 0;
d8cba25d
TS
172
173 if (num_devices == ADF_MAX_DEVICES) {
66550304
AB
174 dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
175 ADF_MAX_DEVICES);
d8cba25d
TS
176 return -EFAULT;
177 }
178
179 mutex_lock(&table_lock);
ed8ccaef
TS
180 atomic_set(&accel_dev->ref_count, 0);
181
182 /* PF on host or VF on guest */
183 if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
184 struct vf_id_map *map;
185
186 list_for_each(itr, &accel_table) {
187 struct adf_accel_dev *ptr =
d8cba25d
TS
188 list_entry(itr, struct adf_accel_dev, list);
189
ed8ccaef
TS
190 if (ptr == accel_dev) {
191 ret = -EEXIST;
192 goto unlock;
193 }
d8cba25d 194 }
ed8ccaef
TS
195
196 list_add_tail(&accel_dev->list, &accel_table);
197 accel_dev->accel_id = num_devices++;
198
199 map = kzalloc(sizeof(*map), GFP_KERNEL);
200 if (!map) {
201 ret = -ENOMEM;
202 goto unlock;
203 }
204 map->bdf = ~0;
205 map->id = accel_dev->accel_id;
206 map->fake_id = map->id;
207 map->attached = true;
208 list_add_tail(&map->list, &vfs_table);
209 } else if (accel_dev->is_vf && pf) {
210 /* VF on host */
211 struct adf_accel_vf_info *vf_info;
212 struct vf_id_map *map;
213
214 vf_info = pf->pf.vf_info + adf_get_vf_id(accel_dev);
215
216 map = adf_find_vf(adf_get_vf_num(accel_dev));
217 if (map) {
218 struct vf_id_map *next;
219
220 accel_dev->accel_id = map->id;
221 list_add_tail(&accel_dev->list, &accel_table);
222 map->fake_id++;
223 map->attached = true;
224 next = list_next_entry(map, list);
225 while (next && &next->list != &vfs_table) {
226 next->fake_id++;
227 next = list_next_entry(next, list);
228 }
229
230 ret = 0;
231 goto unlock;
232 }
233
234 map = kzalloc(sizeof(*map), GFP_KERNEL);
235 if (!map) {
236 ret = -ENOMEM;
237 goto unlock;
238 }
239
240 accel_dev->accel_id = num_devices++;
241 list_add_tail(&accel_dev->list, &accel_table);
242 map->bdf = adf_get_vf_num(accel_dev);
243 map->id = accel_dev->accel_id;
244 map->fake_id = map->id;
245 map->attached = true;
246 list_add_tail(&map->list, &vfs_table);
d8cba25d 247 }
ed8ccaef 248unlock:
d8cba25d 249 mutex_unlock(&table_lock);
ed8ccaef 250 return ret;
d8cba25d
TS
251}
252EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
253
254struct list_head *adf_devmgr_get_head(void)
255{
256 return &accel_table;
257}
258
259/**
260 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
261 * @accel_dev: Pointer to acceleration device.
ed8ccaef 262 * @pf: Corresponding PF if the accel_dev is a VF
d8cba25d
TS
263 *
264 * Function removes acceleration device from the acceleration framework.
265 * To be used by QAT device specific drivers.
266 *
267 * Return: void
268 */
ed8ccaef
TS
269void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev,
270 struct adf_accel_dev *pf)
d8cba25d
TS
271{
272 mutex_lock(&table_lock);
ed8ccaef
TS
273 if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
274 num_devices--;
275 } else if (accel_dev->is_vf && pf) {
276 struct vf_id_map *map, *next;
277
278 map = adf_find_vf(adf_get_vf_num(accel_dev));
279 if (!map) {
280 dev_err(&GET_DEV(accel_dev), "Failed to find VF map\n");
281 goto unlock;
282 }
283 map->fake_id--;
284 map->attached = false;
285 next = list_next_entry(map, list);
286 while (next && &next->list != &vfs_table) {
287 next->fake_id--;
288 next = list_next_entry(next, list);
289 }
290 }
291unlock:
d8cba25d 292 list_del(&accel_dev->list);
d8cba25d
TS
293 mutex_unlock(&table_lock);
294}
295EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
296
297struct adf_accel_dev *adf_devmgr_get_first(void)
298{
299 struct adf_accel_dev *dev = NULL;
300
301 if (!list_empty(&accel_table))
302 dev = list_first_entry(&accel_table, struct adf_accel_dev,
303 list);
304 return dev;
305}
306
307/**
308 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
309 * @accel_dev: Pointer to pci device.
310 *
311 * Function returns acceleration device associated with the given pci device.
312 * To be used by QAT device specific drivers.
313 *
8c4cef46 314 * Return: pointer to accel_dev or NULL if not found.
d8cba25d
TS
315 */
316struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
317{
318 struct list_head *itr;
319
8c4cef46 320 mutex_lock(&table_lock);
d8cba25d
TS
321 list_for_each(itr, &accel_table) {
322 struct adf_accel_dev *ptr =
323 list_entry(itr, struct adf_accel_dev, list);
324
325 if (ptr->accel_pci_dev.pci_dev == pci_dev) {
326 mutex_unlock(&table_lock);
327 return ptr;
328 }
329 }
8c4cef46 330 mutex_unlock(&table_lock);
d8cba25d
TS
331 return NULL;
332}
333EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
334
335struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
336{
337 struct list_head *itr;
ed8ccaef 338 int real_id;
d8cba25d 339
8c4cef46 340 mutex_lock(&table_lock);
ed8ccaef
TS
341 real_id = adf_get_vf_real_id(id);
342 if (real_id < 0)
343 goto unlock;
344
345 id = real_id;
346
d8cba25d
TS
347 list_for_each(itr, &accel_table) {
348 struct adf_accel_dev *ptr =
349 list_entry(itr, struct adf_accel_dev, list);
d8cba25d
TS
350 if (ptr->accel_id == id) {
351 mutex_unlock(&table_lock);
352 return ptr;
353 }
354 }
ed8ccaef 355unlock:
8c4cef46 356 mutex_unlock(&table_lock);
d8cba25d
TS
357 return NULL;
358}
359
360int adf_devmgr_verify_id(uint32_t id)
361{
362 if (id == ADF_CFG_ALL_DEVICES)
363 return 0;
364
365 if (adf_devmgr_get_dev_by_id(id))
366 return 0;
367
368 return -ENODEV;
369}
370
ed8ccaef
TS
371static int adf_get_num_dettached_vfs(void)
372{
373 struct list_head *itr;
374 int vfs = 0;
375
376 mutex_lock(&table_lock);
377 list_for_each(itr, &vfs_table) {
378 struct vf_id_map *ptr =
379 list_entry(itr, struct vf_id_map, list);
380 if (ptr->bdf != ~0 && !ptr->attached)
381 vfs++;
382 }
383 mutex_unlock(&table_lock);
384 return vfs;
385}
386
d8cba25d
TS
387void adf_devmgr_get_num_dev(uint32_t *num)
388{
ed8ccaef 389 *num = num_devices - adf_get_num_dettached_vfs();
d8cba25d
TS
390}
391
ed8ccaef
TS
392/**
393 * adf_dev_in_use() - Check whether accel_dev is currently in use
394 * @accel_dev: Pointer to acceleration device.
395 *
396 * To be used by QAT device specific drivers.
397 *
398 * Return: 1 when device is in use, 0 otherwise.
399 */
d8cba25d
TS
400int adf_dev_in_use(struct adf_accel_dev *accel_dev)
401{
402 return atomic_read(&accel_dev->ref_count) != 0;
403}
ed8ccaef 404EXPORT_SYMBOL_GPL(adf_dev_in_use);
d8cba25d 405
ed8ccaef
TS
406/**
407 * adf_dev_get() - Increment accel_dev reference count
408 * @accel_dev: Pointer to acceleration device.
409 *
410 * Increment the accel_dev refcount and if this is the first time
411 * incrementing it during this period the accel_dev is in use,
412 * increment the module refcount too.
413 * To be used by QAT device specific drivers.
414 *
415 * Return: 0 when successful, EFAULT when fail to bump module refcount
416 */
d8cba25d
TS
417int adf_dev_get(struct adf_accel_dev *accel_dev)
418{
419 if (atomic_add_return(1, &accel_dev->ref_count) == 1)
420 if (!try_module_get(accel_dev->owner))
421 return -EFAULT;
422 return 0;
423}
ed8ccaef 424EXPORT_SYMBOL_GPL(adf_dev_get);
d8cba25d 425
ed8ccaef
TS
426/**
427 * adf_dev_put() - Decrement accel_dev reference count
428 * @accel_dev: Pointer to acceleration device.
429 *
430 * Decrement the accel_dev refcount and if this is the last time
431 * decrementing it during this period the accel_dev is in use,
432 * decrement the module refcount too.
433 * To be used by QAT device specific drivers.
434 *
435 * Return: void
436 */
d8cba25d
TS
437void adf_dev_put(struct adf_accel_dev *accel_dev)
438{
439 if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
440 module_put(accel_dev->owner);
441}
ed8ccaef 442EXPORT_SYMBOL_GPL(adf_dev_put);
d8cba25d 443
ed8ccaef
TS
444/**
445 * adf_devmgr_in_reset() - Check whether device is in reset
446 * @accel_dev: Pointer to acceleration device.
447 *
448 * To be used by QAT device specific drivers.
449 *
450 * Return: 1 when the device is being reset, 0 otherwise.
451 */
d8cba25d
TS
452int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
453{
454 return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
455}
ed8ccaef 456EXPORT_SYMBOL_GPL(adf_devmgr_in_reset);
d8cba25d 457
ed8ccaef
TS
458/**
459 * adf_dev_started() - Check whether device has started
460 * @accel_dev: Pointer to acceleration device.
461 *
462 * To be used by QAT device specific drivers.
463 *
464 * Return: 1 when the device has started, 0 otherwise
465 */
d8cba25d
TS
466int adf_dev_started(struct adf_accel_dev *accel_dev)
467{
468 return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
469}
ed8ccaef 470EXPORT_SYMBOL_GPL(adf_dev_started);