crypto: qat - Intel(R) QAT driver framework
[deliverable/linux.git] / drivers / crypto / qat / qat_common / adf_dev_mgr.c
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.
4
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.
10
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.
15
16 Contact Information:
17 qat-linux@intel.com
18
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:
24
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.
34
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.
46 */
47 #include <linux/mutex.h>
48 #include <linux/list.h>
49 #include "adf_cfg.h"
50 #include "adf_common_drv.h"
51
52 static LIST_HEAD(accel_table);
53 static DEFINE_MUTEX(table_lock);
54 static uint32_t num_devices;
55
56 /**
57 * adf_devmgr_add_dev() - Add accel_dev to the acceleration framework
58 * @accel_dev: Pointer to acceleration device.
59 *
60 * Function adds acceleration device to the acceleration framework.
61 * To be used by QAT device specific drivers.
62 *
63 * Return: 0 on success, error code othewise.
64 */
65 int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev)
66 {
67 struct list_head *itr;
68
69 if (num_devices == ADF_MAX_DEVICES) {
70 pr_err("QAT: Only support up to %d devices\n", ADF_MAX_DEVICES);
71 return -EFAULT;
72 }
73
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);
78
79 if (ptr == accel_dev) {
80 mutex_unlock(&table_lock);
81 return -EEXIST;
82 }
83 }
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);
88 return 0;
89 }
90 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
91
92 struct list_head *adf_devmgr_get_head(void)
93 {
94 return &accel_table;
95 }
96
97 /**
98 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
99 * @accel_dev: Pointer to acceleration device.
100 *
101 * Function removes acceleration device from the acceleration framework.
102 * To be used by QAT device specific drivers.
103 *
104 * Return: void
105 */
106 void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev)
107 {
108 mutex_lock(&table_lock);
109 list_del(&accel_dev->list);
110 num_devices--;
111 mutex_unlock(&table_lock);
112 }
113 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
114
115 struct adf_accel_dev *adf_devmgr_get_first(void)
116 {
117 struct adf_accel_dev *dev = NULL;
118
119 if (!list_empty(&accel_table))
120 dev = list_first_entry(&accel_table, struct adf_accel_dev,
121 list);
122 return dev;
123 }
124
125 /**
126 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
127 * @accel_dev: Pointer to pci device.
128 *
129 * Function returns acceleration device associated with the given pci device.
130 * To be used by QAT device specific drivers.
131 *
132 * Return: pinter to accel_dev or NULL if not found.
133 */
134 struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
135 {
136 struct list_head *itr;
137
138 list_for_each(itr, &accel_table) {
139 struct adf_accel_dev *ptr =
140 list_entry(itr, struct adf_accel_dev, list);
141
142 if (ptr->accel_pci_dev.pci_dev == pci_dev) {
143 mutex_unlock(&table_lock);
144 return ptr;
145 }
146 }
147 return NULL;
148 }
149 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
150
151 struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
152 {
153 struct list_head *itr;
154
155 list_for_each(itr, &accel_table) {
156 struct adf_accel_dev *ptr =
157 list_entry(itr, struct adf_accel_dev, list);
158
159 if (ptr->accel_id == id) {
160 mutex_unlock(&table_lock);
161 return ptr;
162 }
163 }
164 return NULL;
165 }
166
167 int adf_devmgr_verify_id(uint32_t id)
168 {
169 if (id == ADF_CFG_ALL_DEVICES)
170 return 0;
171
172 if (adf_devmgr_get_dev_by_id(id))
173 return 0;
174
175 return -ENODEV;
176 }
177
178 void adf_devmgr_get_num_dev(uint32_t *num)
179 {
180 struct list_head *itr;
181
182 *num = 0;
183 list_for_each(itr, &accel_table) {
184 (*num)++;
185 }
186 }
187
188 int adf_dev_in_use(struct adf_accel_dev *accel_dev)
189 {
190 return atomic_read(&accel_dev->ref_count) != 0;
191 }
192
193 int adf_dev_get(struct adf_accel_dev *accel_dev)
194 {
195 if (atomic_add_return(1, &accel_dev->ref_count) == 1)
196 if (!try_module_get(accel_dev->owner))
197 return -EFAULT;
198 return 0;
199 }
200
201 void adf_dev_put(struct adf_accel_dev *accel_dev)
202 {
203 if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
204 module_put(accel_dev->owner);
205 }
206
207 int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
208 {
209 return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
210 }
211
212 int adf_dev_started(struct adf_accel_dev *accel_dev)
213 {
214 return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
215 }
This page took 0.036879 seconds and 5 git commands to generate.