Merge branch 'for-2.6.28' of git://linux-nfs.org/~bfields/linux
[linux-2.6-block.git] / drivers / dca / dca-core.c
CommitLineData
7589670f
SN
1/*
2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
20 */
21
22/*
23 * This driver supports an interface for DCA clients and providers to meet.
24 */
25
26#include <linux/kernel.h>
27#include <linux/notifier.h>
28#include <linux/device.h>
29#include <linux/dca.h>
30
7f1b358a 31#define DCA_VERSION "1.4"
7589670f 32
7f1b358a
MS
33MODULE_VERSION(DCA_VERSION);
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("Intel Corporation");
7589670f
SN
36
37static DEFINE_SPINLOCK(dca_lock);
38
7f1b358a
MS
39static LIST_HEAD(dca_providers);
40
41static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
42{
43 struct dca_provider *dca, *ret = NULL;
44
45 list_for_each_entry(dca, &dca_providers, node) {
46 if ((!dev) || (dca->ops->dev_managed(dca, dev))) {
47 ret = dca;
48 break;
49 }
50 }
51
52 return ret;
53}
7589670f
SN
54
55/**
56 * dca_add_requester - add a dca client to the list
57 * @dev - the device that wants dca service
58 */
59int dca_add_requester(struct device *dev)
60{
7f1b358a
MS
61 struct dca_provider *dca;
62 int err, slot = -ENODEV;
7589670f 63
7f1b358a
MS
64 if (!dev)
65 return -EFAULT;
7589670f
SN
66
67 spin_lock(&dca_lock);
7f1b358a
MS
68
69 /* check if the requester has not been added already */
70 dca = dca_find_provider_by_dev(dev);
71 if (dca) {
72 spin_unlock(&dca_lock);
73 return -EEXIST;
74 }
75
76 list_for_each_entry(dca, &dca_providers, node) {
77 slot = dca->ops->add_requester(dca, dev);
78 if (slot >= 0)
79 break;
80 }
81 if (slot < 0) {
82 spin_unlock(&dca_lock);
7589670f 83 return slot;
7f1b358a 84 }
7589670f 85
7f1b358a 86 err = dca_sysfs_add_req(dca, dev, slot);
7589670f 87 if (err) {
7f1b358a 88 dca->ops->remove_requester(dca, dev);
7589670f
SN
89 spin_unlock(&dca_lock);
90 return err;
91 }
92
7f1b358a 93 spin_unlock(&dca_lock);
7589670f
SN
94 return 0;
95}
96EXPORT_SYMBOL_GPL(dca_add_requester);
97
98/**
99 * dca_remove_requester - remove a dca client from the list
100 * @dev - the device that wants dca service
101 */
102int dca_remove_requester(struct device *dev)
103{
7f1b358a 104 struct dca_provider *dca;
7589670f 105 int slot;
7f1b358a
MS
106
107 if (!dev)
108 return -EFAULT;
7589670f
SN
109
110 spin_lock(&dca_lock);
7f1b358a
MS
111 dca = dca_find_provider_by_dev(dev);
112 if (!dca) {
113 spin_unlock(&dca_lock);
114 return -ENODEV;
115 }
116 slot = dca->ops->remove_requester(dca, dev);
117 if (slot < 0) {
118 spin_unlock(&dca_lock);
7589670f 119 return slot;
7f1b358a 120 }
7589670f 121
7f1b358a
MS
122 dca_sysfs_remove_req(dca, slot);
123
124 spin_unlock(&dca_lock);
7589670f
SN
125 return 0;
126}
127EXPORT_SYMBOL_GPL(dca_remove_requester);
128
129/**
7f1b358a
MS
130 * dca_common_get_tag - return the dca tag (serves both new and old api)
131 * @dev - the device that wants dca service
7589670f
SN
132 * @cpu - the cpuid as returned by get_cpu()
133 */
7f1b358a 134u8 dca_common_get_tag(struct device *dev, int cpu)
7589670f 135{
7f1b358a
MS
136 struct dca_provider *dca;
137 u8 tag;
138
139 spin_lock(&dca_lock);
140
141 dca = dca_find_provider_by_dev(dev);
142 if (!dca) {
143 spin_unlock(&dca_lock);
7589670f 144 return -ENODEV;
7f1b358a
MS
145 }
146 tag = dca->ops->get_tag(dca, dev, cpu);
147
148 spin_unlock(&dca_lock);
149 return tag;
150}
151
152/**
153 * dca3_get_tag - return the dca tag to the requester device
154 * for the given cpu (new api)
155 * @dev - the device that wants dca service
156 * @cpu - the cpuid as returned by get_cpu()
157 */
158u8 dca3_get_tag(struct device *dev, int cpu)
159{
160 if (!dev)
161 return -EFAULT;
162
163 return dca_common_get_tag(dev, cpu);
164}
165EXPORT_SYMBOL_GPL(dca3_get_tag);
166
167/**
168 * dca_get_tag - return the dca tag for the given cpu (old api)
169 * @cpu - the cpuid as returned by get_cpu()
170 */
171u8 dca_get_tag(int cpu)
172{
173 struct device *dev = NULL;
174
175 return dca_common_get_tag(dev, cpu);
7589670f
SN
176}
177EXPORT_SYMBOL_GPL(dca_get_tag);
178
179/**
180 * alloc_dca_provider - get data struct for describing a dca provider
181 * @ops - pointer to struct of dca operation function pointers
182 * @priv_size - size of extra mem to be added for provider's needs
183 */
184struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
185{
186 struct dca_provider *dca;
187 int alloc_size;
188
189 alloc_size = (sizeof(*dca) + priv_size);
190 dca = kzalloc(alloc_size, GFP_KERNEL);
191 if (!dca)
192 return NULL;
193 dca->ops = ops;
194
195 return dca;
196}
197EXPORT_SYMBOL_GPL(alloc_dca_provider);
198
199/**
200 * free_dca_provider - release the dca provider data struct
201 * @ops - pointer to struct of dca operation function pointers
202 * @priv_size - size of extra mem to be added for provider's needs
203 */
204void free_dca_provider(struct dca_provider *dca)
205{
206 kfree(dca);
207}
208EXPORT_SYMBOL_GPL(free_dca_provider);
209
210static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
211
212/**
213 * register_dca_provider - register a dca provider
214 * @dca - struct created by alloc_dca_provider()
215 * @dev - device providing dca services
216 */
217int register_dca_provider(struct dca_provider *dca, struct device *dev)
218{
219 int err;
220
7589670f
SN
221 err = dca_sysfs_add_provider(dca, dev);
222 if (err)
223 return err;
7f1b358a 224 list_add(&dca->node, &dca_providers);
7589670f
SN
225 blocking_notifier_call_chain(&dca_provider_chain,
226 DCA_PROVIDER_ADD, NULL);
227 return 0;
228}
229EXPORT_SYMBOL_GPL(register_dca_provider);
230
231/**
232 * unregister_dca_provider - remove a dca provider
233 * @dca - struct created by alloc_dca_provider()
234 */
235void unregister_dca_provider(struct dca_provider *dca)
236{
7589670f
SN
237 blocking_notifier_call_chain(&dca_provider_chain,
238 DCA_PROVIDER_REMOVE, NULL);
7f1b358a 239 list_del(&dca->node);
7589670f
SN
240 dca_sysfs_remove_provider(dca);
241}
242EXPORT_SYMBOL_GPL(unregister_dca_provider);
243
244/**
245 * dca_register_notify - register a client's notifier callback
246 */
247void dca_register_notify(struct notifier_block *nb)
248{
249 blocking_notifier_chain_register(&dca_provider_chain, nb);
250}
251EXPORT_SYMBOL_GPL(dca_register_notify);
252
253/**
254 * dca_unregister_notify - remove a client's notifier callback
255 */
256void dca_unregister_notify(struct notifier_block *nb)
257{
258 blocking_notifier_chain_unregister(&dca_provider_chain, nb);
259}
260EXPORT_SYMBOL_GPL(dca_unregister_notify);
261
262static int __init dca_init(void)
263{
7f1b358a 264 printk(KERN_ERR "dca service started, version %s\n", DCA_VERSION);
7589670f
SN
265 return dca_sysfs_init();
266}
267
268static void __exit dca_exit(void)
269{
270 dca_sysfs_exit();
271}
272
1207e795 273subsys_initcall(dca_init);
7589670f
SN
274module_exit(dca_exit);
275