x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / pci / endpoint / pci-ep-cfs.c
blob018ea3433cb58f7b742dec1048f439dff0d90c01
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * configfs to configure the PCI endpoint
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
9 #include <linux/module.h>
10 #include <linux/idr.h>
11 #include <linux/slab.h>
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
15 #include <linux/pci-ep-cfs.h>
17 static DEFINE_IDR(functions_idr);
18 static DEFINE_MUTEX(functions_mutex);
19 static struct config_group *functions_group;
20 static struct config_group *controllers_group;
22 struct pci_epf_group {
23 struct config_group group;
24 struct pci_epf *epf;
25 int index;
28 struct pci_epc_group {
29 struct config_group group;
30 struct pci_epc *epc;
31 bool start;
32 unsigned long function_num_map;
35 static inline struct pci_epf_group *to_pci_epf_group(struct config_item *item)
37 return container_of(to_config_group(item), struct pci_epf_group, group);
40 static inline struct pci_epc_group *to_pci_epc_group(struct config_item *item)
42 return container_of(to_config_group(item), struct pci_epc_group, group);
45 static ssize_t pci_epc_start_store(struct config_item *item, const char *page,
46 size_t len)
48 int ret;
49 bool start;
50 struct pci_epc *epc;
51 struct pci_epc_group *epc_group = to_pci_epc_group(item);
53 epc = epc_group->epc;
55 ret = kstrtobool(page, &start);
56 if (ret)
57 return ret;
59 if (!start) {
60 pci_epc_stop(epc);
61 return len;
64 ret = pci_epc_start(epc);
65 if (ret) {
66 dev_err(&epc->dev, "failed to start endpoint controller\n");
67 return -EINVAL;
70 epc_group->start = start;
72 return len;
75 static ssize_t pci_epc_start_show(struct config_item *item, char *page)
77 return sprintf(page, "%d\n",
78 to_pci_epc_group(item)->start);
81 CONFIGFS_ATTR(pci_epc_, start);
83 static struct configfs_attribute *pci_epc_attrs[] = {
84 &pci_epc_attr_start,
85 NULL,
88 static int pci_epc_epf_link(struct config_item *epc_item,
89 struct config_item *epf_item)
91 int ret;
92 u32 func_no = 0;
93 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item);
94 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
95 struct pci_epc *epc = epc_group->epc;
96 struct pci_epf *epf = epf_group->epf;
98 func_no = find_first_zero_bit(&epc_group->function_num_map,
99 BITS_PER_LONG);
100 if (func_no >= BITS_PER_LONG)
101 return -EINVAL;
103 set_bit(func_no, &epc_group->function_num_map);
104 epf->func_no = func_no;
106 ret = pci_epc_add_epf(epc, epf);
107 if (ret)
108 goto err_add_epf;
110 ret = pci_epf_bind(epf);
111 if (ret)
112 goto err_epf_bind;
114 return 0;
116 err_epf_bind:
117 pci_epc_remove_epf(epc, epf);
119 err_add_epf:
120 clear_bit(func_no, &epc_group->function_num_map);
122 return ret;
125 static void pci_epc_epf_unlink(struct config_item *epc_item,
126 struct config_item *epf_item)
128 struct pci_epc *epc;
129 struct pci_epf *epf;
130 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item);
131 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
133 WARN_ON_ONCE(epc_group->start);
135 epc = epc_group->epc;
136 epf = epf_group->epf;
137 clear_bit(epf->func_no, &epc_group->function_num_map);
138 pci_epf_unbind(epf);
139 pci_epc_remove_epf(epc, epf);
142 static struct configfs_item_operations pci_epc_item_ops = {
143 .allow_link = pci_epc_epf_link,
144 .drop_link = pci_epc_epf_unlink,
147 static const struct config_item_type pci_epc_type = {
148 .ct_item_ops = &pci_epc_item_ops,
149 .ct_attrs = pci_epc_attrs,
150 .ct_owner = THIS_MODULE,
153 struct config_group *pci_ep_cfs_add_epc_group(const char *name)
155 int ret;
156 struct pci_epc *epc;
157 struct config_group *group;
158 struct pci_epc_group *epc_group;
160 epc_group = kzalloc(sizeof(*epc_group), GFP_KERNEL);
161 if (!epc_group) {
162 ret = -ENOMEM;
163 goto err;
166 group = &epc_group->group;
168 config_group_init_type_name(group, name, &pci_epc_type);
169 ret = configfs_register_group(controllers_group, group);
170 if (ret) {
171 pr_err("failed to register configfs group for %s\n", name);
172 goto err_register_group;
175 epc = pci_epc_get(name);
176 if (IS_ERR(epc)) {
177 ret = PTR_ERR(epc);
178 goto err_epc_get;
181 epc_group->epc = epc;
183 return group;
185 err_epc_get:
186 configfs_unregister_group(group);
188 err_register_group:
189 kfree(epc_group);
191 err:
192 return ERR_PTR(ret);
194 EXPORT_SYMBOL(pci_ep_cfs_add_epc_group);
196 void pci_ep_cfs_remove_epc_group(struct config_group *group)
198 struct pci_epc_group *epc_group;
200 if (!group)
201 return;
203 epc_group = container_of(group, struct pci_epc_group, group);
204 pci_epc_put(epc_group->epc);
205 configfs_unregister_group(&epc_group->group);
206 kfree(epc_group);
208 EXPORT_SYMBOL(pci_ep_cfs_remove_epc_group);
210 #define PCI_EPF_HEADER_R(_name) \
211 static ssize_t pci_epf_##_name##_show(struct config_item *item, char *page) \
213 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
214 if (WARN_ON_ONCE(!epf->header)) \
215 return -EINVAL; \
216 return sprintf(page, "0x%04x\n", epf->header->_name); \
219 #define PCI_EPF_HEADER_W_u32(_name) \
220 static ssize_t pci_epf_##_name##_store(struct config_item *item, \
221 const char *page, size_t len) \
223 u32 val; \
224 int ret; \
225 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
226 if (WARN_ON_ONCE(!epf->header)) \
227 return -EINVAL; \
228 ret = kstrtou32(page, 0, &val); \
229 if (ret) \
230 return ret; \
231 epf->header->_name = val; \
232 return len; \
235 #define PCI_EPF_HEADER_W_u16(_name) \
236 static ssize_t pci_epf_##_name##_store(struct config_item *item, \
237 const char *page, size_t len) \
239 u16 val; \
240 int ret; \
241 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
242 if (WARN_ON_ONCE(!epf->header)) \
243 return -EINVAL; \
244 ret = kstrtou16(page, 0, &val); \
245 if (ret) \
246 return ret; \
247 epf->header->_name = val; \
248 return len; \
251 #define PCI_EPF_HEADER_W_u8(_name) \
252 static ssize_t pci_epf_##_name##_store(struct config_item *item, \
253 const char *page, size_t len) \
255 u8 val; \
256 int ret; \
257 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
258 if (WARN_ON_ONCE(!epf->header)) \
259 return -EINVAL; \
260 ret = kstrtou8(page, 0, &val); \
261 if (ret) \
262 return ret; \
263 epf->header->_name = val; \
264 return len; \
267 static ssize_t pci_epf_msi_interrupts_store(struct config_item *item,
268 const char *page, size_t len)
270 u8 val;
271 int ret;
273 ret = kstrtou8(page, 0, &val);
274 if (ret)
275 return ret;
277 to_pci_epf_group(item)->epf->msi_interrupts = val;
279 return len;
282 static ssize_t pci_epf_msi_interrupts_show(struct config_item *item,
283 char *page)
285 return sprintf(page, "%d\n",
286 to_pci_epf_group(item)->epf->msi_interrupts);
289 PCI_EPF_HEADER_R(vendorid)
290 PCI_EPF_HEADER_W_u16(vendorid)
292 PCI_EPF_HEADER_R(deviceid)
293 PCI_EPF_HEADER_W_u16(deviceid)
295 PCI_EPF_HEADER_R(revid)
296 PCI_EPF_HEADER_W_u8(revid)
298 PCI_EPF_HEADER_R(progif_code)
299 PCI_EPF_HEADER_W_u8(progif_code)
301 PCI_EPF_HEADER_R(subclass_code)
302 PCI_EPF_HEADER_W_u8(subclass_code)
304 PCI_EPF_HEADER_R(baseclass_code)
305 PCI_EPF_HEADER_W_u8(baseclass_code)
307 PCI_EPF_HEADER_R(cache_line_size)
308 PCI_EPF_HEADER_W_u8(cache_line_size)
310 PCI_EPF_HEADER_R(subsys_vendor_id)
311 PCI_EPF_HEADER_W_u16(subsys_vendor_id)
313 PCI_EPF_HEADER_R(subsys_id)
314 PCI_EPF_HEADER_W_u16(subsys_id)
316 PCI_EPF_HEADER_R(interrupt_pin)
317 PCI_EPF_HEADER_W_u8(interrupt_pin)
319 CONFIGFS_ATTR(pci_epf_, vendorid);
320 CONFIGFS_ATTR(pci_epf_, deviceid);
321 CONFIGFS_ATTR(pci_epf_, revid);
322 CONFIGFS_ATTR(pci_epf_, progif_code);
323 CONFIGFS_ATTR(pci_epf_, subclass_code);
324 CONFIGFS_ATTR(pci_epf_, baseclass_code);
325 CONFIGFS_ATTR(pci_epf_, cache_line_size);
326 CONFIGFS_ATTR(pci_epf_, subsys_vendor_id);
327 CONFIGFS_ATTR(pci_epf_, subsys_id);
328 CONFIGFS_ATTR(pci_epf_, interrupt_pin);
329 CONFIGFS_ATTR(pci_epf_, msi_interrupts);
331 static struct configfs_attribute *pci_epf_attrs[] = {
332 &pci_epf_attr_vendorid,
333 &pci_epf_attr_deviceid,
334 &pci_epf_attr_revid,
335 &pci_epf_attr_progif_code,
336 &pci_epf_attr_subclass_code,
337 &pci_epf_attr_baseclass_code,
338 &pci_epf_attr_cache_line_size,
339 &pci_epf_attr_subsys_vendor_id,
340 &pci_epf_attr_subsys_id,
341 &pci_epf_attr_interrupt_pin,
342 &pci_epf_attr_msi_interrupts,
343 NULL,
346 static void pci_epf_release(struct config_item *item)
348 struct pci_epf_group *epf_group = to_pci_epf_group(item);
350 mutex_lock(&functions_mutex);
351 idr_remove(&functions_idr, epf_group->index);
352 mutex_unlock(&functions_mutex);
353 pci_epf_destroy(epf_group->epf);
354 kfree(epf_group);
357 static struct configfs_item_operations pci_epf_ops = {
358 .release = pci_epf_release,
361 static const struct config_item_type pci_epf_type = {
362 .ct_item_ops = &pci_epf_ops,
363 .ct_attrs = pci_epf_attrs,
364 .ct_owner = THIS_MODULE,
367 static struct config_group *pci_epf_make(struct config_group *group,
368 const char *name)
370 struct pci_epf_group *epf_group;
371 struct pci_epf *epf;
372 char *epf_name;
373 int index, err;
375 epf_group = kzalloc(sizeof(*epf_group), GFP_KERNEL);
376 if (!epf_group)
377 return ERR_PTR(-ENOMEM);
379 mutex_lock(&functions_mutex);
380 index = idr_alloc(&functions_idr, epf_group, 0, 0, GFP_KERNEL);
381 mutex_unlock(&functions_mutex);
382 if (index < 0) {
383 err = index;
384 goto free_group;
387 epf_group->index = index;
389 config_group_init_type_name(&epf_group->group, name, &pci_epf_type);
391 epf_name = kasprintf(GFP_KERNEL, "%s.%d",
392 group->cg_item.ci_name, epf_group->index);
393 if (!epf_name) {
394 err = -ENOMEM;
395 goto remove_idr;
398 epf = pci_epf_create(epf_name);
399 if (IS_ERR(epf)) {
400 pr_err("failed to create endpoint function device\n");
401 err = -EINVAL;
402 goto free_name;
405 epf_group->epf = epf;
407 kfree(epf_name);
409 return &epf_group->group;
411 free_name:
412 kfree(epf_name);
414 remove_idr:
415 mutex_lock(&functions_mutex);
416 idr_remove(&functions_idr, epf_group->index);
417 mutex_unlock(&functions_mutex);
419 free_group:
420 kfree(epf_group);
422 return ERR_PTR(err);
425 static void pci_epf_drop(struct config_group *group, struct config_item *item)
427 config_item_put(item);
430 static struct configfs_group_operations pci_epf_group_ops = {
431 .make_group = &pci_epf_make,
432 .drop_item = &pci_epf_drop,
435 static const struct config_item_type pci_epf_group_type = {
436 .ct_group_ops = &pci_epf_group_ops,
437 .ct_owner = THIS_MODULE,
440 struct config_group *pci_ep_cfs_add_epf_group(const char *name)
442 struct config_group *group;
444 group = configfs_register_default_group(functions_group, name,
445 &pci_epf_group_type);
446 if (IS_ERR(group))
447 pr_err("failed to register configfs group for %s function\n",
448 name);
450 return group;
452 EXPORT_SYMBOL(pci_ep_cfs_add_epf_group);
454 void pci_ep_cfs_remove_epf_group(struct config_group *group)
456 if (IS_ERR_OR_NULL(group))
457 return;
459 configfs_unregister_default_group(group);
461 EXPORT_SYMBOL(pci_ep_cfs_remove_epf_group);
463 static const struct config_item_type pci_functions_type = {
464 .ct_owner = THIS_MODULE,
467 static const struct config_item_type pci_controllers_type = {
468 .ct_owner = THIS_MODULE,
471 static const struct config_item_type pci_ep_type = {
472 .ct_owner = THIS_MODULE,
475 static struct configfs_subsystem pci_ep_cfs_subsys = {
476 .su_group = {
477 .cg_item = {
478 .ci_namebuf = "pci_ep",
479 .ci_type = &pci_ep_type,
482 .su_mutex = __MUTEX_INITIALIZER(pci_ep_cfs_subsys.su_mutex),
485 static int __init pci_ep_cfs_init(void)
487 int ret;
488 struct config_group *root = &pci_ep_cfs_subsys.su_group;
490 config_group_init(root);
492 ret = configfs_register_subsystem(&pci_ep_cfs_subsys);
493 if (ret) {
494 pr_err("Error %d while registering subsystem %s\n",
495 ret, root->cg_item.ci_namebuf);
496 goto err;
499 functions_group = configfs_register_default_group(root, "functions",
500 &pci_functions_type);
501 if (IS_ERR(functions_group)) {
502 ret = PTR_ERR(functions_group);
503 pr_err("Error %d while registering functions group\n",
504 ret);
505 goto err_functions_group;
508 controllers_group =
509 configfs_register_default_group(root, "controllers",
510 &pci_controllers_type);
511 if (IS_ERR(controllers_group)) {
512 ret = PTR_ERR(controllers_group);
513 pr_err("Error %d while registering controllers group\n",
514 ret);
515 goto err_controllers_group;
518 return 0;
520 err_controllers_group:
521 configfs_unregister_default_group(functions_group);
523 err_functions_group:
524 configfs_unregister_subsystem(&pci_ep_cfs_subsys);
526 err:
527 return ret;
529 module_init(pci_ep_cfs_init);
531 static void __exit pci_ep_cfs_exit(void)
533 configfs_unregister_default_group(controllers_group);
534 configfs_unregister_default_group(functions_group);
535 configfs_unregister_subsystem(&pci_ep_cfs_subsys);
537 module_exit(pci_ep_cfs_exit);
539 MODULE_DESCRIPTION("PCI EP CONFIGFS");
540 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
541 MODULE_LICENSE("GPL v2");