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