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 pr_err("QAT: Only support up to %d devices\n", ADF_MAX_DEVICES
);
74 mutex_lock(&table_lock
);
75 list_for_each(itr
, &accel_table
) {
76 struct adf_accel_dev
*ptr
=
77 list_entry(itr
, struct adf_accel_dev
, list
);
79 if (ptr
== accel_dev
) {
80 mutex_unlock(&table_lock
);
84 atomic_set(&accel_dev
->ref_count
, 0);
85 list_add_tail(&accel_dev
->list
, &accel_table
);
86 accel_dev
->accel_id
= num_devices
++;
87 mutex_unlock(&table_lock
);
90 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev
);
92 struct list_head
*adf_devmgr_get_head(void)
98 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
99 * @accel_dev: Pointer to acceleration device.
101 * Function removes acceleration device from the acceleration framework.
102 * To be used by QAT device specific drivers.
106 void adf_devmgr_rm_dev(struct adf_accel_dev
*accel_dev
)
108 mutex_lock(&table_lock
);
109 list_del(&accel_dev
->list
);
111 mutex_unlock(&table_lock
);
113 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev
);
115 struct adf_accel_dev
*adf_devmgr_get_first(void)
117 struct adf_accel_dev
*dev
= NULL
;
119 if (!list_empty(&accel_table
))
120 dev
= list_first_entry(&accel_table
, struct adf_accel_dev
,
126 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
127 * @accel_dev: Pointer to pci device.
129 * Function returns acceleration device associated with the given pci device.
130 * To be used by QAT device specific drivers.
132 * Return: pinter to accel_dev or NULL if not found.
134 struct adf_accel_dev
*adf_devmgr_pci_to_accel_dev(struct pci_dev
*pci_dev
)
136 struct list_head
*itr
;
138 list_for_each(itr
, &accel_table
) {
139 struct adf_accel_dev
*ptr
=
140 list_entry(itr
, struct adf_accel_dev
, list
);
142 if (ptr
->accel_pci_dev
.pci_dev
== pci_dev
) {
143 mutex_unlock(&table_lock
);
149 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev
);
151 struct adf_accel_dev
*adf_devmgr_get_dev_by_id(uint32_t id
)
153 struct list_head
*itr
;
155 list_for_each(itr
, &accel_table
) {
156 struct adf_accel_dev
*ptr
=
157 list_entry(itr
, struct adf_accel_dev
, list
);
159 if (ptr
->accel_id
== id
) {
160 mutex_unlock(&table_lock
);
167 int adf_devmgr_verify_id(uint32_t id
)
169 if (id
== ADF_CFG_ALL_DEVICES
)
172 if (adf_devmgr_get_dev_by_id(id
))
178 void adf_devmgr_get_num_dev(uint32_t *num
)
180 struct list_head
*itr
;
183 list_for_each(itr
, &accel_table
) {
188 int adf_dev_in_use(struct adf_accel_dev
*accel_dev
)
190 return atomic_read(&accel_dev
->ref_count
) != 0;
193 int adf_dev_get(struct adf_accel_dev
*accel_dev
)
195 if (atomic_add_return(1, &accel_dev
->ref_count
) == 1)
196 if (!try_module_get(accel_dev
->owner
))
201 void adf_dev_put(struct adf_accel_dev
*accel_dev
)
203 if (atomic_sub_return(1, &accel_dev
->ref_count
) == 0)
204 module_put(accel_dev
->owner
);
207 int adf_devmgr_in_reset(struct adf_accel_dev
*accel_dev
)
209 return test_bit(ADF_STATUS_RESTARTING
, &accel_dev
->status
);
212 int adf_dev_started(struct adf_accel_dev
*accel_dev
)
214 return test_bit(ADF_STATUS_STARTED
, &accel_dev
->status
);