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 DEFINE_MUTEX(table_lock);
54 static uint32_t num_devices;
57 * adf_devmgr_add_dev() - Add accel_dev to the acceleration framework
58 * @accel_dev: Pointer to acceleration device.
60 * Function adds acceleration device to the acceleration framework.
61 * To be used by QAT device specific drivers.
63 * Return: 0 on success, error code othewise.
65 int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev)
67 struct list_head *itr;
69 if (num_devices == ADF_MAX_DEVICES) {
70 dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
75 mutex_lock(&table_lock);
76 list_for_each(itr, &accel_table) {
77 struct adf_accel_dev *ptr =
78 list_entry(itr, struct adf_accel_dev, list);
80 if (ptr == accel_dev) {
81 mutex_unlock(&table_lock);
85 atomic_set(&accel_dev->ref_count, 0);
86 list_add_tail(&accel_dev->list, &accel_table);
87 accel_dev->accel_id = num_devices++;
88 mutex_unlock(&table_lock);
91 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
93 struct list_head *adf_devmgr_get_head(void)
99 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
100 * @accel_dev: Pointer to acceleration device.
102 * Function removes acceleration device from the acceleration framework.
103 * To be used by QAT device specific drivers.
107 void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev)
109 mutex_lock(&table_lock);
110 list_del(&accel_dev->list);
112 mutex_unlock(&table_lock);
114 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
116 struct adf_accel_dev *adf_devmgr_get_first(void)
118 struct adf_accel_dev *dev = NULL;
120 if (!list_empty(&accel_table))
121 dev = list_first_entry(&accel_table, struct adf_accel_dev,
127 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
128 * @accel_dev: Pointer to pci device.
130 * Function returns acceleration device associated with the given pci device.
131 * To be used by QAT device specific drivers.
133 * Return: pointer to accel_dev or NULL if not found.
135 struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
137 struct list_head *itr;
139 mutex_lock(&table_lock);
140 list_for_each(itr, &accel_table) {
141 struct adf_accel_dev *ptr =
142 list_entry(itr, struct adf_accel_dev, list);
144 if (ptr->accel_pci_dev.pci_dev == pci_dev) {
145 mutex_unlock(&table_lock);
149 mutex_unlock(&table_lock);
152 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
154 struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
156 struct list_head *itr;
158 mutex_lock(&table_lock);
159 list_for_each(itr, &accel_table) {
160 struct adf_accel_dev *ptr =
161 list_entry(itr, struct adf_accel_dev, list);
163 if (ptr->accel_id == id) {
164 mutex_unlock(&table_lock);
168 mutex_unlock(&table_lock);
172 int adf_devmgr_verify_id(uint32_t id)
174 if (id == ADF_CFG_ALL_DEVICES)
177 if (adf_devmgr_get_dev_by_id(id))
183 void adf_devmgr_get_num_dev(uint32_t *num)
185 struct list_head *itr;
188 list_for_each(itr, &accel_table) {
193 int adf_dev_in_use(struct adf_accel_dev *accel_dev)
195 return atomic_read(&accel_dev->ref_count) != 0;
198 int adf_dev_get(struct adf_accel_dev *accel_dev)
200 if (atomic_add_return(1, &accel_dev->ref_count) == 1)
201 if (!try_module_get(accel_dev->owner))
206 void adf_dev_put(struct adf_accel_dev *accel_dev)
208 if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
209 module_put(accel_dev->owner);
212 int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
214 return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
217 int adf_dev_started(struct adf_accel_dev *accel_dev)
219 return test_bit(ADF_STATUS_STARTED, &accel_dev->status);