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.
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.
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
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
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>
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;
62 struct list_head list;
65 static int adf_get_vf_id(struct adf_accel_dev *vf)
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));
72 static int adf_get_vf_num(struct adf_accel_dev *vf)
74 return (accel_to_pci_dev(vf)->bus->number << 8) | adf_get_vf_id(vf);
77 static struct vf_id_map *adf_find_vf(u32 bdf)
79 struct list_head *itr;
81 list_for_each(itr, &vfs_table) {
82 struct vf_id_map *ptr =
83 list_entry(itr, struct vf_id_map, list);
91 static int adf_get_vf_real_id(u32 fake)
93 struct list_head *itr;
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)
105 * adf_clean_vf_map() - Cleans VF id mapings
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
111 void adf_clean_vf_map(bool vf)
113 struct vf_id_map *map;
114 struct list_head *ptr, *tmp;
116 mutex_lock(&table_lock);
117 list_for_each_safe(ptr, tmp, &vfs_table) {
118 map = list_entry(ptr, struct vf_id_map, list);
122 if (vf && map->bdf == -1)
128 mutex_unlock(&table_lock);
130 EXPORT_SYMBOL_GPL(adf_clean_vf_map);
133 * adf_devmgr_update_class_index() - Update internal index
134 * @hw_data: Pointer to internal device data.
136 * Function updates internal dev index for VFs
138 void adf_devmgr_update_class_index(struct adf_hw_device_data *hw_data)
140 struct adf_hw_device_class *class = hw_data->dev_class;
141 struct list_head *itr;
144 list_for_each(itr, &accel_table) {
145 struct adf_accel_dev *ptr =
146 list_entry(itr, struct adf_accel_dev, list);
148 if (ptr->hw_device->dev_class == class)
149 ptr->hw_device->instance_id = i++;
151 if (i == class->instances)
155 EXPORT_SYMBOL_GPL(adf_devmgr_update_class_index);
158 * adf_devmgr_add_dev() - Add accel_dev to the acceleration framework
159 * @accel_dev: Pointer to acceleration device.
160 * @pf: Corresponding PF if the accel_dev is a VF
162 * Function adds acceleration device to the acceleration framework.
163 * To be used by QAT device specific drivers.
165 * Return: 0 on success, error code otherwise.
167 int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev,
168 struct adf_accel_dev *pf)
170 struct list_head *itr;
173 if (num_devices == ADF_MAX_DEVICES) {
174 dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
179 mutex_lock(&table_lock);
180 atomic_set(&accel_dev->ref_count, 0);
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;
186 list_for_each(itr, &accel_table) {
187 struct adf_accel_dev *ptr =
188 list_entry(itr, struct adf_accel_dev, list);
190 if (ptr == accel_dev) {
196 list_add_tail(&accel_dev->list, &accel_table);
197 accel_dev->accel_id = num_devices++;
199 map = kzalloc(sizeof(*map), GFP_KERNEL);
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) {
211 struct adf_accel_vf_info *vf_info;
212 struct vf_id_map *map;
214 vf_info = pf->pf.vf_info + adf_get_vf_id(accel_dev);
216 map = adf_find_vf(adf_get_vf_num(accel_dev));
218 struct vf_id_map *next;
220 accel_dev->accel_id = map->id;
221 list_add_tail(&accel_dev->list, &accel_table);
223 map->attached = true;
224 next = list_next_entry(map, list);
225 while (next && &next->list != &vfs_table) {
227 next = list_next_entry(next, list);
234 map = kzalloc(sizeof(*map), GFP_KERNEL);
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);
249 mutex_unlock(&table_lock);
252 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
254 struct list_head *adf_devmgr_get_head(void)
260 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
261 * @accel_dev: Pointer to acceleration device.
262 * @pf: Corresponding PF if the accel_dev is a VF
264 * Function removes acceleration device from the acceleration framework.
265 * To be used by QAT device specific drivers.
269 void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev,
270 struct adf_accel_dev *pf)
272 mutex_lock(&table_lock);
273 if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
275 } else if (accel_dev->is_vf && pf) {
276 struct vf_id_map *map, *next;
278 map = adf_find_vf(adf_get_vf_num(accel_dev));
280 dev_err(&GET_DEV(accel_dev), "Failed to find VF map\n");
284 map->attached = false;
285 next = list_next_entry(map, list);
286 while (next && &next->list != &vfs_table) {
288 next = list_next_entry(next, list);
292 list_del(&accel_dev->list);
293 mutex_unlock(&table_lock);
295 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
297 struct adf_accel_dev *adf_devmgr_get_first(void)
299 struct adf_accel_dev *dev = NULL;
301 if (!list_empty(&accel_table))
302 dev = list_first_entry(&accel_table, struct adf_accel_dev,
308 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
309 * @accel_dev: Pointer to pci device.
311 * Function returns acceleration device associated with the given pci device.
312 * To be used by QAT device specific drivers.
314 * Return: pointer to accel_dev or NULL if not found.
316 struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
318 struct list_head *itr;
320 mutex_lock(&table_lock);
321 list_for_each(itr, &accel_table) {
322 struct adf_accel_dev *ptr =
323 list_entry(itr, struct adf_accel_dev, list);
325 if (ptr->accel_pci_dev.pci_dev == pci_dev) {
326 mutex_unlock(&table_lock);
330 mutex_unlock(&table_lock);
333 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
335 struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
337 struct list_head *itr;
340 mutex_lock(&table_lock);
341 real_id = adf_get_vf_real_id(id);
347 list_for_each(itr, &accel_table) {
348 struct adf_accel_dev *ptr =
349 list_entry(itr, struct adf_accel_dev, list);
350 if (ptr->accel_id == id) {
351 mutex_unlock(&table_lock);
356 mutex_unlock(&table_lock);
360 int adf_devmgr_verify_id(uint32_t id)
362 if (id == ADF_CFG_ALL_DEVICES)
365 if (adf_devmgr_get_dev_by_id(id))
371 static int adf_get_num_dettached_vfs(void)
373 struct list_head *itr;
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)
383 mutex_unlock(&table_lock);
387 void adf_devmgr_get_num_dev(uint32_t *num)
389 *num = num_devices - adf_get_num_dettached_vfs();
393 * adf_dev_in_use() - Check whether accel_dev is currently in use
394 * @accel_dev: Pointer to acceleration device.
396 * To be used by QAT device specific drivers.
398 * Return: 1 when device is in use, 0 otherwise.
400 int adf_dev_in_use(struct adf_accel_dev *accel_dev)
402 return atomic_read(&accel_dev->ref_count) != 0;
404 EXPORT_SYMBOL_GPL(adf_dev_in_use);
407 * adf_dev_get() - Increment accel_dev reference count
408 * @accel_dev: Pointer to acceleration device.
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.
415 * Return: 0 when successful, EFAULT when fail to bump module refcount
417 int adf_dev_get(struct adf_accel_dev *accel_dev)
419 if (atomic_add_return(1, &accel_dev->ref_count) == 1)
420 if (!try_module_get(accel_dev->owner))
424 EXPORT_SYMBOL_GPL(adf_dev_get);
427 * adf_dev_put() - Decrement accel_dev reference count
428 * @accel_dev: Pointer to acceleration device.
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.
437 void adf_dev_put(struct adf_accel_dev *accel_dev)
439 if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
440 module_put(accel_dev->owner);
442 EXPORT_SYMBOL_GPL(adf_dev_put);
445 * adf_devmgr_in_reset() - Check whether device is in reset
446 * @accel_dev: Pointer to acceleration device.
448 * To be used by QAT device specific drivers.
450 * Return: 1 when the device is being reset, 0 otherwise.
452 int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
454 return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
456 EXPORT_SYMBOL_GPL(adf_devmgr_in_reset);
459 * adf_dev_started() - Check whether device has started
460 * @accel_dev: Pointer to acceleration device.
462 * To be used by QAT device specific drivers.
464 * Return: 1 when the device has started, 0 otherwise
466 int adf_dev_started(struct adf_accel_dev *accel_dev)
468 return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
470 EXPORT_SYMBOL_GPL(adf_dev_started);