treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / crypto / qat / qat_common / adf_dev_mgr.c
blob2d06409bd3c4e558a8e642a1cb4ce4aed9329458
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 vf_id_map *map;
233 map = adf_find_vf(adf_get_vf_num(accel_dev));
234 if (map) {
235 struct vf_id_map *next;
237 accel_dev->accel_id = map->id;
238 list_add_tail(&accel_dev->list, &accel_table);
239 map->fake_id++;
240 map->attached = true;
241 next = list_next_entry(map, list);
242 while (next && &next->list != &vfs_table) {
243 next->fake_id++;
244 next = list_next_entry(next, list);
247 ret = 0;
248 goto unlock;
251 map = kzalloc(sizeof(*map), GFP_KERNEL);
252 if (!map) {
253 ret = -ENOMEM;
254 goto unlock;
256 accel_dev->accel_id = adf_find_free_id();
257 if (accel_dev->accel_id > ADF_MAX_DEVICES) {
258 kfree(map);
259 ret = -EFAULT;
260 goto unlock;
262 num_devices++;
263 list_add_tail(&accel_dev->list, &accel_table);
264 map->bdf = adf_get_vf_num(accel_dev);
265 map->id = accel_dev->accel_id;
266 map->fake_id = map->id;
267 map->attached = true;
268 list_add_tail(&map->list, &vfs_table);
270 unlock:
271 mutex_unlock(&table_lock);
272 return ret;
274 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
276 struct list_head *adf_devmgr_get_head(void)
278 return &accel_table;
282 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
283 * @accel_dev: Pointer to acceleration device.
284 * @pf: Corresponding PF if the accel_dev is a VF
286 * Function removes acceleration device from the acceleration framework.
287 * To be used by QAT device specific drivers.
289 * Return: void
291 void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev,
292 struct adf_accel_dev *pf)
294 mutex_lock(&table_lock);
295 if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
296 id_map[accel_dev->accel_id] = 0;
297 num_devices--;
298 } else if (accel_dev->is_vf && pf) {
299 struct vf_id_map *map, *next;
301 map = adf_find_vf(adf_get_vf_num(accel_dev));
302 if (!map) {
303 dev_err(&GET_DEV(accel_dev), "Failed to find VF map\n");
304 goto unlock;
306 map->fake_id--;
307 map->attached = false;
308 next = list_next_entry(map, list);
309 while (next && &next->list != &vfs_table) {
310 next->fake_id--;
311 next = list_next_entry(next, list);
314 unlock:
315 list_del(&accel_dev->list);
316 mutex_unlock(&table_lock);
318 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
320 struct adf_accel_dev *adf_devmgr_get_first(void)
322 struct adf_accel_dev *dev = NULL;
324 if (!list_empty(&accel_table))
325 dev = list_first_entry(&accel_table, struct adf_accel_dev,
326 list);
327 return dev;
331 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
332 * @accel_dev: Pointer to pci device.
334 * Function returns acceleration device associated with the given pci device.
335 * To be used by QAT device specific drivers.
337 * Return: pointer to accel_dev or NULL if not found.
339 struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
341 struct list_head *itr;
343 mutex_lock(&table_lock);
344 list_for_each(itr, &accel_table) {
345 struct adf_accel_dev *ptr =
346 list_entry(itr, struct adf_accel_dev, list);
348 if (ptr->accel_pci_dev.pci_dev == pci_dev) {
349 mutex_unlock(&table_lock);
350 return ptr;
353 mutex_unlock(&table_lock);
354 return NULL;
356 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
358 struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
360 struct list_head *itr;
361 int real_id;
363 mutex_lock(&table_lock);
364 real_id = adf_get_vf_real_id(id);
365 if (real_id < 0)
366 goto unlock;
368 id = real_id;
370 list_for_each(itr, &accel_table) {
371 struct adf_accel_dev *ptr =
372 list_entry(itr, struct adf_accel_dev, list);
373 if (ptr->accel_id == id) {
374 mutex_unlock(&table_lock);
375 return ptr;
378 unlock:
379 mutex_unlock(&table_lock);
380 return NULL;
383 int adf_devmgr_verify_id(uint32_t id)
385 if (id == ADF_CFG_ALL_DEVICES)
386 return 0;
388 if (adf_devmgr_get_dev_by_id(id))
389 return 0;
391 return -ENODEV;
394 static int adf_get_num_dettached_vfs(void)
396 struct list_head *itr;
397 int vfs = 0;
399 mutex_lock(&table_lock);
400 list_for_each(itr, &vfs_table) {
401 struct vf_id_map *ptr =
402 list_entry(itr, struct vf_id_map, list);
403 if (ptr->bdf != ~0 && !ptr->attached)
404 vfs++;
406 mutex_unlock(&table_lock);
407 return vfs;
410 void adf_devmgr_get_num_dev(uint32_t *num)
412 *num = num_devices - adf_get_num_dettached_vfs();
416 * adf_dev_in_use() - Check whether accel_dev is currently in use
417 * @accel_dev: Pointer to acceleration device.
419 * To be used by QAT device specific drivers.
421 * Return: 1 when device is in use, 0 otherwise.
423 int adf_dev_in_use(struct adf_accel_dev *accel_dev)
425 return atomic_read(&accel_dev->ref_count) != 0;
427 EXPORT_SYMBOL_GPL(adf_dev_in_use);
430 * adf_dev_get() - Increment accel_dev reference count
431 * @accel_dev: Pointer to acceleration device.
433 * Increment the accel_dev refcount and if this is the first time
434 * incrementing it during this period the accel_dev is in use,
435 * increment the module refcount too.
436 * To be used by QAT device specific drivers.
438 * Return: 0 when successful, EFAULT when fail to bump module refcount
440 int adf_dev_get(struct adf_accel_dev *accel_dev)
442 if (atomic_add_return(1, &accel_dev->ref_count) == 1)
443 if (!try_module_get(accel_dev->owner))
444 return -EFAULT;
445 return 0;
447 EXPORT_SYMBOL_GPL(adf_dev_get);
450 * adf_dev_put() - Decrement accel_dev reference count
451 * @accel_dev: Pointer to acceleration device.
453 * Decrement the accel_dev refcount and if this is the last time
454 * decrementing it during this period the accel_dev is in use,
455 * decrement the module refcount too.
456 * To be used by QAT device specific drivers.
458 * Return: void
460 void adf_dev_put(struct adf_accel_dev *accel_dev)
462 if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
463 module_put(accel_dev->owner);
465 EXPORT_SYMBOL_GPL(adf_dev_put);
468 * adf_devmgr_in_reset() - Check whether device is in reset
469 * @accel_dev: Pointer to acceleration device.
471 * To be used by QAT device specific drivers.
473 * Return: 1 when the device is being reset, 0 otherwise.
475 int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
477 return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
479 EXPORT_SYMBOL_GPL(adf_devmgr_in_reset);
482 * adf_dev_started() - Check whether device has started
483 * @accel_dev: Pointer to acceleration device.
485 * To be used by QAT device specific drivers.
487 * Return: 1 when the device has started, 0 otherwise
489 int adf_dev_started(struct adf_accel_dev *accel_dev)
491 return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
493 EXPORT_SYMBOL_GPL(adf_dev_started);