• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LIST_HEAD(vfs_table);
54 static DEFINE_MUTEX(table_lock);
55 static uint32_t num_devices;
56 
57 struct vf_id_map {
58 	u32 bdf;
59 	u32 id;
60 	u32 fake_id;
61 	bool attached;
62 	struct list_head list;
63 };
64 
adf_get_vf_id(struct adf_accel_dev * vf)65 static int adf_get_vf_id(struct adf_accel_dev *vf)
66 {
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));
70 }
71 
adf_get_vf_num(struct adf_accel_dev * vf)72 static int adf_get_vf_num(struct adf_accel_dev *vf)
73 {
74 	return (accel_to_pci_dev(vf)->bus->number << 8) | adf_get_vf_id(vf);
75 }
76 
adf_find_vf(u32 bdf)77 static struct vf_id_map *adf_find_vf(u32 bdf)
78 {
79 	struct list_head *itr;
80 
81 	list_for_each(itr, &vfs_table) {
82 		struct vf_id_map *ptr =
83 			list_entry(itr, struct vf_id_map, list);
84 
85 		if (ptr->bdf == bdf)
86 			return ptr;
87 	}
88 	return NULL;
89 }
90 
adf_get_vf_real_id(u32 fake)91 static int adf_get_vf_real_id(u32 fake)
92 {
93 	struct list_head *itr;
94 
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)
99 			return ptr->id;
100 	}
101 	return -1;
102 }
103 
104 /**
105  * adf_clean_vf_map() - Cleans VF id mapings
106  *
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
110  */
adf_clean_vf_map(bool vf)111 void adf_clean_vf_map(bool vf)
112 {
113 	struct vf_id_map *map;
114 	struct list_head *ptr, *tmp;
115 
116 	mutex_lock(&table_lock);
117 	list_for_each_safe(ptr, tmp, &vfs_table) {
118 		map = list_entry(ptr, struct vf_id_map, list);
119 		if (map->bdf != -1)
120 			num_devices--;
121 
122 		if (vf && map->bdf == -1)
123 			continue;
124 
125 		list_del(ptr);
126 		kfree(map);
127 	}
128 	mutex_unlock(&table_lock);
129 }
130 EXPORT_SYMBOL_GPL(adf_clean_vf_map);
131 
132 /**
133  * adf_devmgr_update_class_index() - Update internal index
134  * @hw_data:  Pointer to internal device data.
135  *
136  * Function updates internal dev index for VFs
137  */
adf_devmgr_update_class_index(struct adf_hw_device_data * hw_data)138 void adf_devmgr_update_class_index(struct adf_hw_device_data *hw_data)
139 {
140 	struct adf_hw_device_class *class = hw_data->dev_class;
141 	struct list_head *itr;
142 	int i = 0;
143 
144 	list_for_each(itr, &accel_table) {
145 		struct adf_accel_dev *ptr =
146 				list_entry(itr, struct adf_accel_dev, list);
147 
148 		if (ptr->hw_device->dev_class == class)
149 			ptr->hw_device->instance_id = i++;
150 
151 		if (i == class->instances)
152 				break;
153 	}
154 }
155 EXPORT_SYMBOL_GPL(adf_devmgr_update_class_index);
156 
157 /**
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
161  *
162  * Function adds acceleration device to the acceleration framework.
163  * To be used by QAT device specific drivers.
164  *
165  * Return: 0 on success, error code otherwise.
166  */
adf_devmgr_add_dev(struct adf_accel_dev * accel_dev,struct adf_accel_dev * pf)167 int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev,
168 		       struct adf_accel_dev *pf)
169 {
170 	struct list_head *itr;
171 	int ret = 0;
172 
173 	if (num_devices == ADF_MAX_DEVICES) {
174 		dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
175 			ADF_MAX_DEVICES);
176 		return -EFAULT;
177 	}
178 
179 	mutex_lock(&table_lock);
180 	atomic_set(&accel_dev->ref_count, 0);
181 
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;
185 
186 		list_for_each(itr, &accel_table) {
187 			struct adf_accel_dev *ptr =
188 				list_entry(itr, struct adf_accel_dev, list);
189 
190 			if (ptr == accel_dev) {
191 				ret = -EEXIST;
192 				goto unlock;
193 			}
194 		}
195 
196 		list_add_tail(&accel_dev->list, &accel_table);
197 		accel_dev->accel_id = num_devices++;
198 
199 		map = kzalloc(sizeof(*map), GFP_KERNEL);
200 		if (!map) {
201 			ret = -ENOMEM;
202 			goto unlock;
203 		}
204 		map->bdf = ~0;
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) {
210 		/* VF on host */
211 		struct adf_accel_vf_info *vf_info;
212 		struct vf_id_map *map;
213 
214 		vf_info = pf->pf.vf_info + adf_get_vf_id(accel_dev);
215 
216 		map = adf_find_vf(adf_get_vf_num(accel_dev));
217 		if (map) {
218 			struct vf_id_map *next;
219 
220 			accel_dev->accel_id = map->id;
221 			list_add_tail(&accel_dev->list, &accel_table);
222 			map->fake_id++;
223 			map->attached = true;
224 			next = list_next_entry(map, list);
225 			while (next && &next->list != &vfs_table) {
226 				next->fake_id++;
227 				next = list_next_entry(next, list);
228 			}
229 
230 			ret = 0;
231 			goto unlock;
232 		}
233 
234 		map = kzalloc(sizeof(*map), GFP_KERNEL);
235 		if (!map) {
236 			ret = -ENOMEM;
237 			goto unlock;
238 		}
239 
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);
247 	}
248 unlock:
249 	mutex_unlock(&table_lock);
250 	return ret;
251 }
252 EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
253 
adf_devmgr_get_head(void)254 struct list_head *adf_devmgr_get_head(void)
255 {
256 	return &accel_table;
257 }
258 
259 /**
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
263  *
264  * Function removes acceleration device from the acceleration framework.
265  * To be used by QAT device specific drivers.
266  *
267  * Return: void
268  */
adf_devmgr_rm_dev(struct adf_accel_dev * accel_dev,struct adf_accel_dev * pf)269 void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev,
270 		       struct adf_accel_dev *pf)
271 {
272 	mutex_lock(&table_lock);
273 	if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
274 		num_devices--;
275 	} else if (accel_dev->is_vf && pf) {
276 		struct vf_id_map *map, *next;
277 
278 		map = adf_find_vf(adf_get_vf_num(accel_dev));
279 		if (!map) {
280 			dev_err(&GET_DEV(accel_dev), "Failed to find VF map\n");
281 			goto unlock;
282 		}
283 		map->fake_id--;
284 		map->attached = false;
285 		next = list_next_entry(map, list);
286 		while (next && &next->list != &vfs_table) {
287 			next->fake_id--;
288 			next = list_next_entry(next, list);
289 		}
290 	}
291 unlock:
292 	list_del(&accel_dev->list);
293 	mutex_unlock(&table_lock);
294 }
295 EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
296 
adf_devmgr_get_first(void)297 struct adf_accel_dev *adf_devmgr_get_first(void)
298 {
299 	struct adf_accel_dev *dev = NULL;
300 
301 	if (!list_empty(&accel_table))
302 		dev = list_first_entry(&accel_table, struct adf_accel_dev,
303 				       list);
304 	return dev;
305 }
306 
307 /**
308  * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
309  * @accel_dev:  Pointer to pci device.
310  *
311  * Function returns acceleration device associated with the given pci device.
312  * To be used by QAT device specific drivers.
313  *
314  * Return: pointer to accel_dev or NULL if not found.
315  */
adf_devmgr_pci_to_accel_dev(struct pci_dev * pci_dev)316 struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
317 {
318 	struct list_head *itr;
319 
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);
324 
325 		if (ptr->accel_pci_dev.pci_dev == pci_dev) {
326 			mutex_unlock(&table_lock);
327 			return ptr;
328 		}
329 	}
330 	mutex_unlock(&table_lock);
331 	return NULL;
332 }
333 EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
334 
adf_devmgr_get_dev_by_id(uint32_t id)335 struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
336 {
337 	struct list_head *itr;
338 	int real_id;
339 
340 	mutex_lock(&table_lock);
341 	real_id = adf_get_vf_real_id(id);
342 	if (real_id < 0)
343 		goto unlock;
344 
345 	id = real_id;
346 
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);
352 			return ptr;
353 		}
354 	}
355 unlock:
356 	mutex_unlock(&table_lock);
357 	return NULL;
358 }
359 
adf_devmgr_verify_id(uint32_t id)360 int adf_devmgr_verify_id(uint32_t id)
361 {
362 	if (id == ADF_CFG_ALL_DEVICES)
363 		return 0;
364 
365 	if (adf_devmgr_get_dev_by_id(id))
366 		return 0;
367 
368 	return -ENODEV;
369 }
370 
adf_get_num_dettached_vfs(void)371 static int adf_get_num_dettached_vfs(void)
372 {
373 	struct list_head *itr;
374 	int vfs = 0;
375 
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)
381 			vfs++;
382 	}
383 	mutex_unlock(&table_lock);
384 	return vfs;
385 }
386 
adf_devmgr_get_num_dev(uint32_t * num)387 void adf_devmgr_get_num_dev(uint32_t *num)
388 {
389 	*num = num_devices - adf_get_num_dettached_vfs();
390 }
391 
392 /**
393  * adf_dev_in_use() - Check whether accel_dev is currently in use
394  * @accel_dev: Pointer to acceleration device.
395  *
396  * To be used by QAT device specific drivers.
397  *
398  * Return: 1 when device is in use, 0 otherwise.
399  */
adf_dev_in_use(struct adf_accel_dev * accel_dev)400 int adf_dev_in_use(struct adf_accel_dev *accel_dev)
401 {
402 	return atomic_read(&accel_dev->ref_count) != 0;
403 }
404 EXPORT_SYMBOL_GPL(adf_dev_in_use);
405 
406 /**
407  * adf_dev_get() - Increment accel_dev reference count
408  * @accel_dev: Pointer to acceleration device.
409  *
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.
414  *
415  * Return: 0 when successful, EFAULT when fail to bump module refcount
416  */
adf_dev_get(struct adf_accel_dev * accel_dev)417 int adf_dev_get(struct adf_accel_dev *accel_dev)
418 {
419 	if (atomic_add_return(1, &accel_dev->ref_count) == 1)
420 		if (!try_module_get(accel_dev->owner))
421 			return -EFAULT;
422 	return 0;
423 }
424 EXPORT_SYMBOL_GPL(adf_dev_get);
425 
426 /**
427  * adf_dev_put() - Decrement accel_dev reference count
428  * @accel_dev: Pointer to acceleration device.
429  *
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.
434  *
435  * Return: void
436  */
adf_dev_put(struct adf_accel_dev * accel_dev)437 void adf_dev_put(struct adf_accel_dev *accel_dev)
438 {
439 	if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
440 		module_put(accel_dev->owner);
441 }
442 EXPORT_SYMBOL_GPL(adf_dev_put);
443 
444 /**
445  * adf_devmgr_in_reset() - Check whether device is in reset
446  * @accel_dev: Pointer to acceleration device.
447  *
448  * To be used by QAT device specific drivers.
449  *
450  * Return: 1 when the device is being reset, 0 otherwise.
451  */
adf_devmgr_in_reset(struct adf_accel_dev * accel_dev)452 int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
453 {
454 	return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
455 }
456 EXPORT_SYMBOL_GPL(adf_devmgr_in_reset);
457 
458 /**
459  * adf_dev_started() - Check whether device has started
460  * @accel_dev: Pointer to acceleration device.
461  *
462  * To be used by QAT device specific drivers.
463  *
464  * Return: 1 when the device has started, 0 otherwise
465  */
adf_dev_started(struct adf_accel_dev * accel_dev)466 int adf_dev_started(struct adf_accel_dev *accel_dev)
467 {
468 	return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
469 }
470 EXPORT_SYMBOL_GPL(adf_dev_started);
471