1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Controller* (EPC) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
15 #include <linux/pci-ep-cfs.h>
17 static struct class *pci_epc_class;
19 static void devm_pci_epc_release(struct device *dev, void *res)
21 struct pci_epc *epc = *(struct pci_epc **)res;
26 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
28 struct pci_epc **epc = res;
30 return *epc == match_data;
34 * pci_epc_put() - release the PCI endpoint controller
35 * @epc: epc returned by pci_epc_get()
37 * release the refcount the caller obtained by invoking pci_epc_get()
39 void pci_epc_put(struct pci_epc *epc)
41 if (IS_ERR_OR_NULL(epc))
44 module_put(epc->ops->owner);
45 put_device(&epc->dev);
47 EXPORT_SYMBOL_GPL(pci_epc_put);
50 * pci_epc_get() - get the PCI endpoint controller
51 * @epc_name: device name of the endpoint controller
53 * Invoke to get struct pci_epc * corresponding to the device name of the
56 struct pci_epc *pci_epc_get(const char *epc_name)
61 struct class_dev_iter iter;
63 class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
64 while ((dev = class_dev_iter_next(&iter))) {
65 if (strcmp(epc_name, dev_name(dev)))
68 epc = to_pci_epc(dev);
69 if (!try_module_get(epc->ops->owner)) {
74 class_dev_iter_exit(&iter);
75 get_device(&epc->dev);
80 class_dev_iter_exit(&iter);
83 EXPORT_SYMBOL_GPL(pci_epc_get);
86 * pci_epc_get_first_free_bar() - helper to get first unreserved BAR
87 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
89 * Invoke to get the first unreserved BAR that can be used by the endpoint
93 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
95 return pci_epc_get_next_free_bar(epc_features, BAR_0);
97 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
100 * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
101 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
102 * @bar: the starting BAR number from where unreserved BAR should be searched
104 * Invoke to get the next unreserved BAR starting from @bar that can be used
105 * for endpoint function.
107 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
108 *epc_features, enum pci_barno bar)
115 /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
116 if (bar > 0 && epc_features->bar[bar - 1].only_64bit)
119 for (i = bar; i < PCI_STD_NUM_BARS; i++) {
120 /* If the BAR is not reserved, return it. */
121 if (epc_features->bar[i].type != BAR_RESERVED)
127 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
130 * pci_epc_get_features() - get the features supported by EPC
131 * @epc: the features supported by *this* EPC device will be returned
132 * @func_no: the features supported by the EPC device specific to the
133 * endpoint function with func_no will be returned
134 * @vfunc_no: the features supported by the EPC device specific to the
135 * virtual endpoint function with vfunc_no will be returned
137 * Invoke to get the features provided by the EPC which may be
138 * specific to an endpoint function. Returns pci_epc_features on success
139 * and NULL for any failures.
141 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
142 u8 func_no, u8 vfunc_no)
144 const struct pci_epc_features *epc_features;
146 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
149 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
152 if (!epc->ops->get_features)
155 mutex_lock(&epc->lock);
156 epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
157 mutex_unlock(&epc->lock);
161 EXPORT_SYMBOL_GPL(pci_epc_get_features);
164 * pci_epc_stop() - stop the PCI link
165 * @epc: the link of the EPC device that has to be stopped
167 * Invoke to stop the PCI link
169 void pci_epc_stop(struct pci_epc *epc)
171 if (IS_ERR(epc) || !epc->ops->stop)
174 mutex_lock(&epc->lock);
176 mutex_unlock(&epc->lock);
178 EXPORT_SYMBOL_GPL(pci_epc_stop);
181 * pci_epc_start() - start the PCI link
182 * @epc: the link of *this* EPC device has to be started
184 * Invoke to start the PCI link
186 int pci_epc_start(struct pci_epc *epc)
193 if (!epc->ops->start)
196 mutex_lock(&epc->lock);
197 ret = epc->ops->start(epc);
198 mutex_unlock(&epc->lock);
202 EXPORT_SYMBOL_GPL(pci_epc_start);
205 * pci_epc_raise_irq() - interrupt the host system
206 * @epc: the EPC device which has to interrupt the host
207 * @func_no: the physical endpoint function number in the EPC device
208 * @vfunc_no: the virtual endpoint function number in the physical function
209 * @type: specify the type of interrupt; INTX, MSI or MSI-X
210 * @interrupt_num: the MSI or MSI-X interrupt number with range (1-N)
212 * Invoke to raise an INTX, MSI or MSI-X interrupt
214 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
215 unsigned int type, u16 interrupt_num)
219 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
222 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
225 if (!epc->ops->raise_irq)
228 mutex_lock(&epc->lock);
229 ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
230 mutex_unlock(&epc->lock);
234 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
237 * pci_epc_map_msi_irq() - Map physical address to MSI address and return
239 * @epc: the EPC device which has the MSI capability
240 * @func_no: the physical endpoint function number in the EPC device
241 * @vfunc_no: the virtual endpoint function number in the physical function
242 * @phys_addr: the physical address of the outbound region
243 * @interrupt_num: the MSI interrupt number with range (1-N)
244 * @entry_size: Size of Outbound address region for each interrupt
245 * @msi_data: the data that should be written in order to raise MSI interrupt
246 * with interrupt number as 'interrupt num'
247 * @msi_addr_offset: Offset of MSI address from the aligned outbound address
248 * to which the MSI address is mapped
250 * Invoke to map physical address to MSI address and return MSI data. The
251 * physical address should be an address in the outbound region. This is
252 * required to implement doorbell functionality of NTB wherein EPC on either
253 * side of the interface (primary and secondary) can directly write to the
254 * physical address (in outbound region) of the other interface to ring
257 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
258 phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
259 u32 *msi_data, u32 *msi_addr_offset)
263 if (IS_ERR_OR_NULL(epc))
266 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
269 if (!epc->ops->map_msi_irq)
272 mutex_lock(&epc->lock);
273 ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
274 interrupt_num, entry_size, msi_data,
276 mutex_unlock(&epc->lock);
280 EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
283 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
284 * @epc: the EPC device to which MSI interrupts was requested
285 * @func_no: the physical endpoint function number in the EPC device
286 * @vfunc_no: the virtual endpoint function number in the physical function
288 * Invoke to get the number of MSI interrupts allocated by the RC
290 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
294 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
297 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
300 if (!epc->ops->get_msi)
303 mutex_lock(&epc->lock);
304 interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
305 mutex_unlock(&epc->lock);
310 interrupt = 1 << interrupt;
314 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
317 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
318 * @epc: the EPC device on which MSI has to be configured
319 * @func_no: the physical endpoint function number in the EPC device
320 * @vfunc_no: the virtual endpoint function number in the physical function
321 * @interrupts: number of MSI interrupts required by the EPF
323 * Invoke to set the required number of MSI interrupts.
325 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
330 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
331 interrupts < 1 || interrupts > 32)
334 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
337 if (!epc->ops->set_msi)
340 encode_int = order_base_2(interrupts);
342 mutex_lock(&epc->lock);
343 ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int);
344 mutex_unlock(&epc->lock);
348 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
351 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
352 * @epc: the EPC device to which MSI-X interrupts was requested
353 * @func_no: the physical endpoint function number in the EPC device
354 * @vfunc_no: the virtual endpoint function number in the physical function
356 * Invoke to get the number of MSI-X interrupts allocated by the RC
358 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
362 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
365 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
368 if (!epc->ops->get_msix)
371 mutex_lock(&epc->lock);
372 interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
373 mutex_unlock(&epc->lock);
378 return interrupt + 1;
380 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
383 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
384 * @epc: the EPC device on which MSI-X has to be configured
385 * @func_no: the physical endpoint function number in the EPC device
386 * @vfunc_no: the virtual endpoint function number in the physical function
387 * @interrupts: number of MSI-X interrupts required by the EPF
388 * @bir: BAR where the MSI-X table resides
389 * @offset: Offset pointing to the start of MSI-X table
391 * Invoke to set the required number of MSI-X interrupts.
393 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
394 u16 interrupts, enum pci_barno bir, u32 offset)
398 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
399 interrupts < 1 || interrupts > 2048)
402 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
405 if (!epc->ops->set_msix)
408 mutex_lock(&epc->lock);
409 ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir,
411 mutex_unlock(&epc->lock);
415 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
418 * pci_epc_unmap_addr() - unmap CPU address from PCI address
419 * @epc: the EPC device on which address is allocated
420 * @func_no: the physical endpoint function number in the EPC device
421 * @vfunc_no: the virtual endpoint function number in the physical function
422 * @phys_addr: physical address of the local system
424 * Invoke to unmap the CPU address from PCI address.
426 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
427 phys_addr_t phys_addr)
429 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
432 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
435 if (!epc->ops->unmap_addr)
438 mutex_lock(&epc->lock);
439 epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
440 mutex_unlock(&epc->lock);
442 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
445 * pci_epc_map_addr() - map CPU address to PCI address
446 * @epc: the EPC device on which address is allocated
447 * @func_no: the physical endpoint function number in the EPC device
448 * @vfunc_no: the virtual endpoint function number in the physical function
449 * @phys_addr: physical address of the local system
450 * @pci_addr: PCI address to which the physical address should be mapped
451 * @size: the size of the allocation
453 * Invoke to map CPU address with PCI address.
455 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
456 phys_addr_t phys_addr, u64 pci_addr, size_t size)
460 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
463 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
466 if (!epc->ops->map_addr)
469 mutex_lock(&epc->lock);
470 ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
472 mutex_unlock(&epc->lock);
476 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
479 * pci_epc_clear_bar() - reset the BAR
480 * @epc: the EPC device for which the BAR has to be cleared
481 * @func_no: the physical endpoint function number in the EPC device
482 * @vfunc_no: the virtual endpoint function number in the physical function
483 * @epf_bar: the struct epf_bar that contains the BAR information
485 * Invoke to reset the BAR of the endpoint device.
487 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
488 struct pci_epf_bar *epf_bar)
490 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
491 (epf_bar->barno == BAR_5 &&
492 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
495 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
498 if (!epc->ops->clear_bar)
501 mutex_lock(&epc->lock);
502 epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
503 mutex_unlock(&epc->lock);
505 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
508 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
509 * @epc: the EPC device on which BAR has to be configured
510 * @func_no: the physical endpoint function number in the EPC device
511 * @vfunc_no: the virtual endpoint function number in the physical function
512 * @epf_bar: the struct epf_bar that contains the BAR information
514 * Invoke to configure the BAR of the endpoint device.
516 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
517 struct pci_epf_bar *epf_bar)
520 int flags = epf_bar->flags;
522 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
523 (epf_bar->barno == BAR_5 &&
524 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
525 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
526 flags & PCI_BASE_ADDRESS_IO_MASK) ||
527 (upper_32_bits(epf_bar->size) &&
528 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
531 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
534 if (!epc->ops->set_bar)
537 mutex_lock(&epc->lock);
538 ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
539 mutex_unlock(&epc->lock);
543 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
546 * pci_epc_write_header() - write standard configuration header
547 * @epc: the EPC device to which the configuration header should be written
548 * @func_no: the physical endpoint function number in the EPC device
549 * @vfunc_no: the virtual endpoint function number in the physical function
550 * @header: standard configuration header fields
552 * Invoke to write the configuration header to the endpoint controller. Every
553 * endpoint controller will have a dedicated location to which the standard
554 * configuration header would be written. The callback function should write
555 * the header fields to this dedicated location.
557 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
558 struct pci_epf_header *header)
562 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
565 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
568 /* Only Virtual Function #1 has deviceID */
572 if (!epc->ops->write_header)
575 mutex_lock(&epc->lock);
576 ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
577 mutex_unlock(&epc->lock);
581 EXPORT_SYMBOL_GPL(pci_epc_write_header);
584 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
585 * @epc: the EPC device to which the endpoint function should be added
586 * @epf: the endpoint function to be added
587 * @type: Identifies if the EPC is connected to the primary or secondary
590 * A PCI endpoint device can have one or more functions. In the case of PCIe,
591 * the specification allows up to 8 PCIe endpoint functions. Invoke
592 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
594 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
595 enum pci_epc_interface_type type)
597 struct list_head *list;
601 if (IS_ERR_OR_NULL(epc) || epf->is_vf)
604 if (type == PRIMARY_INTERFACE && epf->epc)
607 if (type == SECONDARY_INTERFACE && epf->sec_epc)
610 mutex_lock(&epc->list_lock);
611 func_no = find_first_zero_bit(&epc->function_num_map,
613 if (func_no >= BITS_PER_LONG) {
618 if (func_no > epc->max_functions - 1) {
619 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
624 set_bit(func_no, &epc->function_num_map);
625 if (type == PRIMARY_INTERFACE) {
626 epf->func_no = func_no;
630 epf->sec_epc_func_no = func_no;
632 list = &epf->sec_epc_list;
635 list_add_tail(list, &epc->pci_epf);
637 mutex_unlock(&epc->list_lock);
641 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
644 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
645 * @epc: the EPC device from which the endpoint function should be removed
646 * @epf: the endpoint function to be removed
647 * @type: identifies if the EPC is connected to the primary or secondary
650 * Invoke to remove PCI endpoint function from the endpoint controller.
652 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
653 enum pci_epc_interface_type type)
655 struct list_head *list;
658 if (IS_ERR_OR_NULL(epc) || !epf)
661 if (type == PRIMARY_INTERFACE) {
662 func_no = epf->func_no;
665 func_no = epf->sec_epc_func_no;
666 list = &epf->sec_epc_list;
669 mutex_lock(&epc->list_lock);
670 clear_bit(func_no, &epc->function_num_map);
673 mutex_unlock(&epc->list_lock);
675 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
678 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
679 * connection with the Root Complex.
680 * @epc: the EPC device which has established link with the host
682 * Invoke to Notify the EPF device that the EPC device has established a
683 * connection with the Root Complex.
685 void pci_epc_linkup(struct pci_epc *epc)
689 if (IS_ERR_OR_NULL(epc))
692 mutex_lock(&epc->list_lock);
693 list_for_each_entry(epf, &epc->pci_epf, list) {
694 mutex_lock(&epf->lock);
695 if (epf->event_ops && epf->event_ops->link_up)
696 epf->event_ops->link_up(epf);
697 mutex_unlock(&epf->lock);
699 mutex_unlock(&epc->list_lock);
701 EXPORT_SYMBOL_GPL(pci_epc_linkup);
704 * pci_epc_linkdown() - Notify the EPF device that EPC device has dropped the
705 * connection with the Root Complex.
706 * @epc: the EPC device which has dropped the link with the host
708 * Invoke to Notify the EPF device that the EPC device has dropped the
709 * connection with the Root Complex.
711 void pci_epc_linkdown(struct pci_epc *epc)
715 if (IS_ERR_OR_NULL(epc))
718 mutex_lock(&epc->list_lock);
719 list_for_each_entry(epf, &epc->pci_epf, list) {
720 mutex_lock(&epf->lock);
721 if (epf->event_ops && epf->event_ops->link_down)
722 epf->event_ops->link_down(epf);
723 mutex_unlock(&epf->lock);
725 mutex_unlock(&epc->list_lock);
727 EXPORT_SYMBOL_GPL(pci_epc_linkdown);
730 * pci_epc_init_notify() - Notify the EPF device that EPC device's core
731 * initialization is completed.
732 * @epc: the EPC device whose core initialization is completed
734 * Invoke to Notify the EPF device that the EPC device's initialization
737 void pci_epc_init_notify(struct pci_epc *epc)
741 if (IS_ERR_OR_NULL(epc))
744 mutex_lock(&epc->list_lock);
745 list_for_each_entry(epf, &epc->pci_epf, list) {
746 mutex_lock(&epf->lock);
747 if (epf->event_ops && epf->event_ops->core_init)
748 epf->event_ops->core_init(epf);
749 mutex_unlock(&epf->lock);
751 mutex_unlock(&epc->list_lock);
753 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
756 * pci_epc_bme_notify() - Notify the EPF device that the EPC device has received
757 * the BME event from the Root complex
758 * @epc: the EPC device that received the BME event
760 * Invoke to Notify the EPF device that the EPC device has received the Bus
761 * Master Enable (BME) event from the Root complex
763 void pci_epc_bme_notify(struct pci_epc *epc)
767 if (IS_ERR_OR_NULL(epc))
770 mutex_lock(&epc->list_lock);
771 list_for_each_entry(epf, &epc->pci_epf, list) {
772 mutex_lock(&epf->lock);
773 if (epf->event_ops && epf->event_ops->bme)
774 epf->event_ops->bme(epf);
775 mutex_unlock(&epf->lock);
777 mutex_unlock(&epc->list_lock);
779 EXPORT_SYMBOL_GPL(pci_epc_bme_notify);
782 * pci_epc_destroy() - destroy the EPC device
783 * @epc: the EPC device that has to be destroyed
785 * Invoke to destroy the PCI EPC device
787 void pci_epc_destroy(struct pci_epc *epc)
789 pci_ep_cfs_remove_epc_group(epc->group);
790 device_unregister(&epc->dev);
792 EXPORT_SYMBOL_GPL(pci_epc_destroy);
795 * devm_pci_epc_destroy() - destroy the EPC device
796 * @dev: device that wants to destroy the EPC
797 * @epc: the EPC device that has to be destroyed
799 * Invoke to destroy the devres associated with this
800 * pci_epc and destroy the EPC device.
802 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
806 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
808 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
810 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
812 static void pci_epc_release(struct device *dev)
814 kfree(to_pci_epc(dev));
818 * __pci_epc_create() - create a new endpoint controller (EPC) device
819 * @dev: device that is creating the new EPC
820 * @ops: function pointers for performing EPC operations
821 * @owner: the owner of the module that creates the EPC device
823 * Invoke to create a new EPC device and add it to pci_epc class.
826 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
827 struct module *owner)
837 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
843 mutex_init(&epc->lock);
844 mutex_init(&epc->list_lock);
845 INIT_LIST_HEAD(&epc->pci_epf);
847 device_initialize(&epc->dev);
848 epc->dev.class = pci_epc_class;
849 epc->dev.parent = dev;
850 epc->dev.release = pci_epc_release;
853 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
857 ret = device_add(&epc->dev);
861 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
866 put_device(&epc->dev);
871 EXPORT_SYMBOL_GPL(__pci_epc_create);
874 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
875 * @dev: device that is creating the new EPC
876 * @ops: function pointers for performing EPC operations
877 * @owner: the owner of the module that creates the EPC device
879 * Invoke to create a new EPC device and add it to pci_epc class.
880 * While at that, it also associates the device with the pci_epc using devres.
881 * On driver detach, release function is invoked on the devres data,
882 * then, devres data is freed.
885 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
886 struct module *owner)
888 struct pci_epc **ptr, *epc;
890 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
892 return ERR_PTR(-ENOMEM);
894 epc = __pci_epc_create(dev, ops, owner);
897 devres_add(dev, ptr);
904 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
906 static int __init pci_epc_init(void)
908 pci_epc_class = class_create("pci_epc");
909 if (IS_ERR(pci_epc_class)) {
910 pr_err("failed to create pci epc class --> %ld\n",
911 PTR_ERR(pci_epc_class));
912 return PTR_ERR(pci_epc_class);
917 module_init(pci_epc_init);
919 static void __exit pci_epc_exit(void)
921 class_destroy(pci_epc_class);
923 module_exit(pci_epc_exit);
925 MODULE_DESCRIPTION("PCI EPC Library");
926 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");