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
52static LIST_HEAD(accel_table);
53static DEFINE_MUTEX(table_lock);
54static 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 */
65int 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		dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
71			ADF_MAX_DEVICES);
72		return -EFAULT;
73	}
74
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);
79
80		if (ptr == accel_dev) {
81			mutex_unlock(&table_lock);
82			return -EEXIST;
83		}
84	}
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);
89	return 0;
90}
91EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
92
93struct list_head *adf_devmgr_get_head(void)
94{
95	return &accel_table;
96}
97
98/**
99 * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
100 * @accel_dev:  Pointer to acceleration device.
101 *
102 * Function removes acceleration device from the acceleration framework.
103 * To be used by QAT device specific drivers.
104 *
105 * Return: void
106 */
107void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev)
108{
109	mutex_lock(&table_lock);
110	list_del(&accel_dev->list);
111	num_devices--;
112	mutex_unlock(&table_lock);
113}
114EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
115
116struct adf_accel_dev *adf_devmgr_get_first(void)
117{
118	struct adf_accel_dev *dev = NULL;
119
120	if (!list_empty(&accel_table))
121		dev = list_first_entry(&accel_table, struct adf_accel_dev,
122				       list);
123	return dev;
124}
125
126/**
127 * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
128 * @accel_dev:  Pointer to pci device.
129 *
130 * Function returns acceleration device associated with the given pci device.
131 * To be used by QAT device specific drivers.
132 *
133 * Return: pointer to accel_dev or NULL if not found.
134 */
135struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
136{
137	struct list_head *itr;
138
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);
143
144		if (ptr->accel_pci_dev.pci_dev == pci_dev) {
145			mutex_unlock(&table_lock);
146			return ptr;
147		}
148	}
149	mutex_unlock(&table_lock);
150	return NULL;
151}
152EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
153
154struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
155{
156	struct list_head *itr;
157
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);
162
163		if (ptr->accel_id == id) {
164			mutex_unlock(&table_lock);
165			return ptr;
166		}
167	}
168	mutex_unlock(&table_lock);
169	return NULL;
170}
171
172int adf_devmgr_verify_id(uint32_t id)
173{
174	if (id == ADF_CFG_ALL_DEVICES)
175		return 0;
176
177	if (adf_devmgr_get_dev_by_id(id))
178		return 0;
179
180	return -ENODEV;
181}
182
183void adf_devmgr_get_num_dev(uint32_t *num)
184{
185	struct list_head *itr;
186
187	*num = 0;
188	list_for_each(itr, &accel_table) {
189		(*num)++;
190	}
191}
192
193int adf_dev_in_use(struct adf_accel_dev *accel_dev)
194{
195	return atomic_read(&accel_dev->ref_count) != 0;
196}
197
198int adf_dev_get(struct adf_accel_dev *accel_dev)
199{
200	if (atomic_add_return(1, &accel_dev->ref_count) == 1)
201		if (!try_module_get(accel_dev->owner))
202			return -EFAULT;
203	return 0;
204}
205
206void adf_dev_put(struct adf_accel_dev *accel_dev)
207{
208	if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
209		module_put(accel_dev->owner);
210}
211
212int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
213{
214	return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
215}
216
217int adf_dev_started(struct adf_accel_dev *accel_dev)
218{
219	return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
220}
221