watchdog: pic32-dmt: Remove .owner field for driver
[linux/fpc-iii.git] / drivers / crypto / qat / qat_common / adf_dev_mgr.c
blobb3ebb25f9ca75194357f6e7a4b356bcf88fe2928
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.
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.
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.
16 Contact Information:
17 qat-linux@intel.com
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:
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.
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.
47 #include <linux/mutex.h>
48 #include <linux/list.h>
49 #include "adf_cfg.h"
50 #include "adf_common_drv.h"
52 static LIST_HEAD(accel_table);
53 static LIST_HEAD(vfs_table);
54 static DEFINE_MUTEX(table_lock);
55 static uint32_t num_devices;
56 static u8 id_map[ADF_MAX_DEVICES];
58 struct vf_id_map {
59 u32 bdf;
60 u32 id;
61 u32 fake_id;
62 bool attached;
63 struct list_head list;
66 static int adf_get_vf_id(struct adf_accel_dev *vf)
68 return ((7 * (PCI_SLOT(accel_to_pci_dev(vf)->devfn) - 1)) +
69 PCI_FUNC(accel_to_pci_dev(vf)->devfn) +
70 (PCI_SLOT(accel_to_pci_dev(vf)->devfn) - 1));
73 static int adf_get_vf_num(struct adf_accel_dev *vf)
75 return (accel_to_pci_dev(vf)->bus->number << 8) | adf_get_vf_id(vf);
78 static struct vf_id_map *adf_find_vf(u32 bdf)
80 struct list_head *itr;
82 list_for_each(itr, &vfs_table) {
83 struct vf_id_map *ptr =
84 list_entry(itr, struct vf_id_map, list);
86 if (ptr->bdf == bdf)
87 return ptr;
89 return NULL;
92 static int adf_get_vf_real_id(u32 fake)
94 struct list_head *itr;
96 list_for_each(itr, &vfs_table) {
97 struct vf_id_map *ptr =
98 list_entry(itr, struct vf_id_map, list);
99 if (ptr->fake_id == fake)
100 return ptr->id;
102 return -1;
106 * adf_clean_vf_map() - Cleans VF id mapings
108 * Function cleans internal ids for virtual functions.
109 * @vf: flag indicating whether mappings is cleaned
110 * for vfs only or for vfs and pfs
112 void adf_clean_vf_map(bool vf)
114 struct vf_id_map *map;
115 struct list_head *ptr, *tmp;
117 mutex_lock(&table_lock);
118 list_for_each_safe(ptr, tmp, &vfs_table) {
119 map = list_entry(ptr, struct vf_id_map, list);
120 if (map->bdf != -1) {
121 id_map[map->id] = 0;
122 num_devices--;
125 if (vf && map->bdf == -1)
126 continue;
128 list_del(ptr);
129 kfree(map);
131 mutex_unlock(&table_lock);
133 EXPORT_SYMBOL_GPL(adf_clean_vf_map);
136 * adf_devmgr_update_class_index() - Update internal index
137 * @hw_data: Pointer to internal device data.
139 * Function updates internal dev index for VFs
141 void adf_devmgr_update_class_index(struct adf_hw_device_data *hw_data)
143 struct adf_hw_device_class *class = hw_data->dev_class;
144 struct list_head *itr;
145 int i = 0;
147 list_for_each(itr, &accel_table) {
148 struct adf_accel_dev *ptr =
149 list_entry(itr, struct adf_accel_dev, list);
151 if (ptr->hw_device->dev_class == class)
152 ptr->hw_device->instance_id = i++;
154 if (i == class->instances)
155 break;
158 EXPORT_SYMBOL_GPL(adf_devmgr_update_class_index);
160 static unsigned int adf_find_free_id(void)
162 unsigned int i;
164 for (i = 0; i < ADF_MAX_DEVICES; i++) {
165 if (!id_map[i]) {
166 id_map[i] = 1;
167 return i;
170 return ADF_MAX_DEVICES + 1;
174 * adf_devmgr_add_dev() - Add accel_dev to the acceleration framework
175 * @accel_dev: Pointer to acceleration device.
176 * @pf: Corresponding PF if the accel_dev is a VF
178 * Function adds acceleration device to the acceleration framework.
179 * To be used by QAT device specific drivers.
181 * Return: 0 on success, error code otherwise.
183 int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev,
184 struct adf_accel_dev *pf)
186 struct list_head *itr;
187 int ret = 0;
189 if (num_devices == ADF_MAX_DEVICES) {
190 dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
191 ADF_MAX_DEVICES);
192 return -EFAULT;
195 mutex_lock(&table_lock);
196 atomic_set(&accel_dev->ref_count, 0);
198 /* PF on host or VF on guest */
199 if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
200 struct vf_id_map *map;
202 list_for_each(itr, &accel_table) {
203 struct adf_accel_dev *ptr =
204 list_entry(itr, struct adf_accel_dev, list);
206 if (ptr == accel_dev) {
207 ret = -EEXIST;
208 goto unlock;
212 list_add_tail(&accel_dev->list, &accel_table);
213 accel_dev->accel_id = adf_find_free_id();
214 if (accel_dev->accel_id > ADF_MAX_DEVICES) {
215 ret = -EFAULT;
216 goto unlock;
218 num_devices++;
219 map = kzalloc(sizeof(*map), GFP_KERNEL);
220 if (!map) {
221 ret = -ENOMEM;
222 goto unlock;
224 map->bdf = ~0;
225 map->id = accel_dev->accel_id;
226 map->fake_id = map->id;
227 map->attached = true;
228 list_add_tail(&map->list, &vfs_table);
229 } else if (accel_dev->is_vf && pf) {
230 /* VF on host */
231 struct adf_accel_vf_info *vf_info;
232 struct vf_id_map *map;
234 vf_info = pf->pf.vf_info + adf_get_vf_id(accel_dev);
236 map = adf_find_vf(adf_get_vf_num(accel_dev));
237 if (map) {
238 struct vf_id_map *next;
240 accel_dev->accel_id = map->id;
241 list_add_tail(&accel_dev->list, &accel_table);
242 map->fake_id++;
243 map->attached = true;
244 next = list_next_entry(map, list);
245 while (next && &next->list != &vfs_table) {
246 next->fake_id++;
247 next = list_next_entry(next, list);
250 ret = 0;
251 goto unlock;
254 map = kzalloc(sizeof(*map), GFP_KERNEL);
255 if (!map) {
256 ret = -ENOMEM;
257 goto unlock;
259 accel_dev->accel_id = adf_find_free_id();
260 if (accel_dev->accel_id > ADF_MAX_DEVICES) {
261 kfree(map);
262 ret = -EFAULT;
263 goto unlock;
265 num_devices++;
266 list_add_tail(&accel_dev->list, &accel_table);
267 map->bdf = adf_get_vf_num(accel_dev);
268 map->id = accel_dev->accel_id;
269 map->fake_id = map->id;
270 map->attached = true;
271 list_add_tail(&map->list, &vfs_table);
273 unlock:
274 mutex_unlock(&table_lock);
275 return ret;
277 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
279 struct list_head *adf_devmgr_get_head(void)
281 return &accel_table;
285 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
286 * @accel_dev: Pointer to acceleration device.
287 * @pf: Corresponding PF if the accel_dev is a VF
289 * Function removes acceleration device from the acceleration framework.
290 * To be used by QAT device specific drivers.
292 * Return: void
294 void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev,
295 struct adf_accel_dev *pf)
297 mutex_lock(&table_lock);
298 if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
299 id_map[accel_dev->accel_id] = 0;
300 num_devices--;
301 } else if (accel_dev->is_vf && pf) {
302 struct vf_id_map *map, *next;
304 map = adf_find_vf(adf_get_vf_num(accel_dev));
305 if (!map) {
306 dev_err(&GET_DEV(accel_dev), "Failed to find VF map\n");
307 goto unlock;
309 map->fake_id--;
310 map->attached = false;
311 next = list_next_entry(map, list);
312 while (next && &next->list != &vfs_table) {
313 next->fake_id--;
314 next = list_next_entry(next, list);
317 unlock:
318 list_del(&accel_dev->list);
319 mutex_unlock(&table_lock);
321 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
323 struct adf_accel_dev *adf_devmgr_get_first(void)
325 struct adf_accel_dev *dev = NULL;
327 if (!list_empty(&accel_table))
328 dev = list_first_entry(&accel_table, struct adf_accel_dev,
329 list);
330 return dev;
334 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
335 * @accel_dev: Pointer to pci device.
337 * Function returns acceleration device associated with the given pci device.
338 * To be used by QAT device specific drivers.
340 * Return: pointer to accel_dev or NULL if not found.
342 struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
344 struct list_head *itr;
346 mutex_lock(&table_lock);
347 list_for_each(itr, &accel_table) {
348 struct adf_accel_dev *ptr =
349 list_entry(itr, struct adf_accel_dev, list);
351 if (ptr->accel_pci_dev.pci_dev == pci_dev) {
352 mutex_unlock(&table_lock);
353 return ptr;
356 mutex_unlock(&table_lock);
357 return NULL;
359 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
361 struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
363 struct list_head *itr;
364 int real_id;
366 mutex_lock(&table_lock);
367 real_id = adf_get_vf_real_id(id);
368 if (real_id < 0)
369 goto unlock;
371 id = real_id;
373 list_for_each(itr, &accel_table) {
374 struct adf_accel_dev *ptr =
375 list_entry(itr, struct adf_accel_dev, list);
376 if (ptr->accel_id == id) {
377 mutex_unlock(&table_lock);
378 return ptr;
381 unlock:
382 mutex_unlock(&table_lock);
383 return NULL;
386 int adf_devmgr_verify_id(uint32_t id)
388 if (id == ADF_CFG_ALL_DEVICES)
389 return 0;
391 if (adf_devmgr_get_dev_by_id(id))
392 return 0;
394 return -ENODEV;
397 static int adf_get_num_dettached_vfs(void)
399 struct list_head *itr;
400 int vfs = 0;
402 mutex_lock(&table_lock);
403 list_for_each(itr, &vfs_table) {
404 struct vf_id_map *ptr =
405 list_entry(itr, struct vf_id_map, list);
406 if (ptr->bdf != ~0 && !ptr->attached)
407 vfs++;
409 mutex_unlock(&table_lock);
410 return vfs;
413 void adf_devmgr_get_num_dev(uint32_t *num)
415 *num = num_devices - adf_get_num_dettached_vfs();
419 * adf_dev_in_use() - Check whether accel_dev is currently in use
420 * @accel_dev: Pointer to acceleration device.
422 * To be used by QAT device specific drivers.
424 * Return: 1 when device is in use, 0 otherwise.
426 int adf_dev_in_use(struct adf_accel_dev *accel_dev)
428 return atomic_read(&accel_dev->ref_count) != 0;
430 EXPORT_SYMBOL_GPL(adf_dev_in_use);
433 * adf_dev_get() - Increment accel_dev reference count
434 * @accel_dev: Pointer to acceleration device.
436 * Increment the accel_dev refcount and if this is the first time
437 * incrementing it during this period the accel_dev is in use,
438 * increment the module refcount too.
439 * To be used by QAT device specific drivers.
441 * Return: 0 when successful, EFAULT when fail to bump module refcount
443 int adf_dev_get(struct adf_accel_dev *accel_dev)
445 if (atomic_add_return(1, &accel_dev->ref_count) == 1)
446 if (!try_module_get(accel_dev->owner))
447 return -EFAULT;
448 return 0;
450 EXPORT_SYMBOL_GPL(adf_dev_get);
453 * adf_dev_put() - Decrement accel_dev reference count
454 * @accel_dev: Pointer to acceleration device.
456 * Decrement the accel_dev refcount and if this is the last time
457 * decrementing it during this period the accel_dev is in use,
458 * decrement the module refcount too.
459 * To be used by QAT device specific drivers.
461 * Return: void
463 void adf_dev_put(struct adf_accel_dev *accel_dev)
465 if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
466 module_put(accel_dev->owner);
468 EXPORT_SYMBOL_GPL(adf_dev_put);
471 * adf_devmgr_in_reset() - Check whether device is in reset
472 * @accel_dev: Pointer to acceleration device.
474 * To be used by QAT device specific drivers.
476 * Return: 1 when the device is being reset, 0 otherwise.
478 int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
480 return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
482 EXPORT_SYMBOL_GPL(adf_devmgr_in_reset);
485 * adf_dev_started() - Check whether device has started
486 * @accel_dev: Pointer to acceleration device.
488 * To be used by QAT device specific drivers.
490 * Return: 1 when the device has started, 0 otherwise
492 int adf_dev_started(struct adf_accel_dev *accel_dev)
494 return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
496 EXPORT_SYMBOL_GPL(adf_dev_started);