1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Controller* (EPC) library
5 * Copyright (C) 2017 Texas Instruments
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
18 static struct class *pci_epc_class;
20 static void devm_pci_epc_release(struct device *dev, void *res)
22 struct pci_epc *epc = *(struct pci_epc **)res;
27 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
29 struct pci_epc **epc = res;
31 return *epc == match_data;
35 * pci_epc_put() - release the PCI endpoint controller
36 * @epc: epc returned by pci_epc_get()
38 * release the refcount the caller obtained by invoking pci_epc_get()
40 void pci_epc_put(struct pci_epc *epc)
42 if (!epc || IS_ERR(epc))
45 module_put(epc->ops->owner);
46 put_device(&epc->dev);
48 EXPORT_SYMBOL_GPL(pci_epc_put);
51 * pci_epc_get() - get the PCI endpoint controller
52 * @epc_name: device name of the endpoint controller
54 * Invoke to get struct pci_epc * corresponding to the device name of the
57 struct pci_epc *pci_epc_get(const char *epc_name)
62 struct class_dev_iter iter;
64 class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
65 while ((dev = class_dev_iter_next(&iter))) {
66 if (strcmp(epc_name, dev_name(dev)))
69 epc = to_pci_epc(dev);
70 if (!try_module_get(epc->ops->owner)) {
75 class_dev_iter_exit(&iter);
76 get_device(&epc->dev);
81 class_dev_iter_exit(&iter);
84 EXPORT_SYMBOL_GPL(pci_epc_get);
87 * pci_epc_get_first_free_bar() - helper to get first unreserved BAR
88 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
90 * Invoke to get the first unreserved BAR that can be used by the endpoint
91 * function. For any incorrect value in reserved_bar return '0'.
94 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
96 return pci_epc_get_next_free_bar(epc_features, BAR_0);
98 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
101 * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
102 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
103 * @bar: the starting BAR number from where unreserved BAR should be searched
105 * Invoke to get the next unreserved BAR starting from @bar that can be used
106 * for endpoint function. For any incorrect value in reserved_bar return '0'.
108 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
109 *epc_features, enum pci_barno bar)
111 unsigned long free_bar;
116 /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
117 if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
120 /* Find if the reserved BAR is also a 64-bit BAR */
121 free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
123 /* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */
125 free_bar |= epc_features->reserved_bar;
127 free_bar = find_next_zero_bit(&free_bar, 6, bar);
133 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
136 * pci_epc_get_features() - get the features supported by EPC
137 * @epc: the features supported by *this* EPC device will be returned
138 * @func_no: the features supported by the EPC device specific to the
139 * endpoint function with func_no will be returned
140 * @vfunc_no: the features supported by the EPC device specific to the
141 * virtual endpoint function with vfunc_no will be returned
143 * Invoke to get the features provided by the EPC which may be
144 * specific to an endpoint function. Returns pci_epc_features on success
145 * and NULL for any failures.
147 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
148 u8 func_no, u8 vfunc_no)
150 const struct pci_epc_features *epc_features;
152 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
155 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
158 if (!epc->ops->get_features)
161 mutex_lock(&epc->lock);
162 epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
163 mutex_unlock(&epc->lock);
167 EXPORT_SYMBOL_GPL(pci_epc_get_features);
170 * pci_epc_stop() - stop the PCI link
171 * @epc: the link of the EPC device that has to be stopped
173 * Invoke to stop the PCI link
175 void pci_epc_stop(struct pci_epc *epc)
177 if (IS_ERR(epc) || !epc->ops->stop)
180 mutex_lock(&epc->lock);
182 mutex_unlock(&epc->lock);
184 EXPORT_SYMBOL_GPL(pci_epc_stop);
187 * pci_epc_start() - start the PCI link
188 * @epc: the link of *this* EPC device has to be started
190 * Invoke to start the PCI link
192 int pci_epc_start(struct pci_epc *epc)
199 if (!epc->ops->start)
202 mutex_lock(&epc->lock);
203 ret = epc->ops->start(epc);
204 mutex_unlock(&epc->lock);
208 EXPORT_SYMBOL_GPL(pci_epc_start);
211 * pci_epc_raise_irq() - interrupt the host system
212 * @epc: the EPC device which has to interrupt the host
213 * @func_no: the physical endpoint function number in the EPC device
214 * @vfunc_no: the virtual endpoint function number in the physical function
215 * @type: specify the type of interrupt; legacy, MSI or MSI-X
216 * @interrupt_num: the MSI or MSI-X interrupt number with range (1-N)
218 * Invoke to raise an legacy, MSI or MSI-X interrupt
220 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
221 enum pci_epc_irq_type type, u16 interrupt_num)
225 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
228 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
231 if (!epc->ops->raise_irq)
234 mutex_lock(&epc->lock);
235 ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
236 mutex_unlock(&epc->lock);
240 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
243 * pci_epc_map_msi_irq() - Map physical address to MSI address and return
245 * @epc: the EPC device which has the MSI capability
246 * @func_no: the physical endpoint function number in the EPC device
247 * @vfunc_no: the virtual endpoint function number in the physical function
248 * @phys_addr: the physical address of the outbound region
249 * @interrupt_num: the MSI interrupt number with range (1-N)
250 * @entry_size: Size of Outbound address region for each interrupt
251 * @msi_data: the data that should be written in order to raise MSI interrupt
252 * with interrupt number as 'interrupt num'
253 * @msi_addr_offset: Offset of MSI address from the aligned outbound address
254 * to which the MSI address is mapped
256 * Invoke to map physical address to MSI address and return MSI data. The
257 * physical address should be an address in the outbound region. This is
258 * required to implement doorbell functionality of NTB wherein EPC on either
259 * side of the interface (primary and secondary) can directly write to the
260 * physical address (in outbound region) of the other interface to ring
263 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
264 phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
265 u32 *msi_data, u32 *msi_addr_offset)
269 if (IS_ERR_OR_NULL(epc))
272 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
275 if (!epc->ops->map_msi_irq)
278 mutex_lock(&epc->lock);
279 ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
280 interrupt_num, entry_size, msi_data,
282 mutex_unlock(&epc->lock);
286 EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
289 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
290 * @epc: the EPC device to which MSI interrupts was requested
291 * @func_no: the physical endpoint function number in the EPC device
292 * @vfunc_no: the virtual endpoint function number in the physical function
294 * Invoke to get the number of MSI interrupts allocated by the RC
296 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
300 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
303 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
306 if (!epc->ops->get_msi)
309 mutex_lock(&epc->lock);
310 interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
311 mutex_unlock(&epc->lock);
316 interrupt = 1 << interrupt;
320 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
323 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
324 * @epc: the EPC device on which MSI has to be configured
325 * @func_no: the physical endpoint function number in the EPC device
326 * @vfunc_no: the virtual endpoint function number in the physical function
327 * @interrupts: number of MSI interrupts required by the EPF
329 * Invoke to set the required number of MSI interrupts.
331 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
336 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
337 interrupts < 1 || interrupts > 32)
340 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
343 if (!epc->ops->set_msi)
346 encode_int = order_base_2(interrupts);
348 mutex_lock(&epc->lock);
349 ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int);
350 mutex_unlock(&epc->lock);
354 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
357 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
358 * @epc: the EPC device to which MSI-X interrupts was requested
359 * @func_no: the physical endpoint function number in the EPC device
360 * @vfunc_no: the virtual endpoint function number in the physical function
362 * Invoke to get the number of MSI-X interrupts allocated by the RC
364 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
368 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
371 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
374 if (!epc->ops->get_msix)
377 mutex_lock(&epc->lock);
378 interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
379 mutex_unlock(&epc->lock);
384 return interrupt + 1;
386 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
389 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
390 * @epc: the EPC device on which MSI-X has to be configured
391 * @func_no: the physical endpoint function number in the EPC device
392 * @vfunc_no: the virtual endpoint function number in the physical function
393 * @interrupts: number of MSI-X interrupts required by the EPF
394 * @bir: BAR where the MSI-X table resides
395 * @offset: Offset pointing to the start of MSI-X table
397 * Invoke to set the required number of MSI-X interrupts.
399 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
400 u16 interrupts, enum pci_barno bir, u32 offset)
404 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
405 interrupts < 1 || interrupts > 2048)
408 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
411 if (!epc->ops->set_msix)
414 mutex_lock(&epc->lock);
415 ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir,
417 mutex_unlock(&epc->lock);
421 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
424 * pci_epc_unmap_addr() - unmap CPU address from PCI address
425 * @epc: the EPC device on which address is allocated
426 * @func_no: the physical endpoint function number in the EPC device
427 * @vfunc_no: the virtual endpoint function number in the physical function
428 * @phys_addr: physical address of the local system
430 * Invoke to unmap the CPU address from PCI address.
432 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
433 phys_addr_t phys_addr)
435 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
438 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
441 if (!epc->ops->unmap_addr)
444 mutex_lock(&epc->lock);
445 epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
446 mutex_unlock(&epc->lock);
448 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
451 * pci_epc_map_addr() - map CPU address to PCI address
452 * @epc: the EPC device on which address is allocated
453 * @func_no: the physical endpoint function number in the EPC device
454 * @vfunc_no: the virtual endpoint function number in the physical function
455 * @phys_addr: physical address of the local system
456 * @pci_addr: PCI address to which the physical address should be mapped
457 * @size: the size of the allocation
459 * Invoke to map CPU address with PCI address.
461 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
462 phys_addr_t phys_addr, u64 pci_addr, size_t size)
466 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
469 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
472 if (!epc->ops->map_addr)
475 mutex_lock(&epc->lock);
476 ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
478 mutex_unlock(&epc->lock);
482 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
485 * pci_epc_clear_bar() - reset the BAR
486 * @epc: the EPC device for which the BAR has to be cleared
487 * @func_no: the physical endpoint function number in the EPC device
488 * @vfunc_no: the virtual endpoint function number in the physical function
489 * @epf_bar: the struct epf_bar that contains the BAR information
491 * Invoke to reset the BAR of the endpoint device.
493 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
494 struct pci_epf_bar *epf_bar)
496 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
497 (epf_bar->barno == BAR_5 &&
498 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
501 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
504 if (!epc->ops->clear_bar)
507 mutex_lock(&epc->lock);
508 epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
509 mutex_unlock(&epc->lock);
511 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
514 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
515 * @epc: the EPC device on which BAR has to be configured
516 * @func_no: the physical endpoint function number in the EPC device
517 * @vfunc_no: the virtual endpoint function number in the physical function
518 * @epf_bar: the struct epf_bar that contains the BAR information
520 * Invoke to configure the BAR of the endpoint device.
522 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
523 struct pci_epf_bar *epf_bar)
526 int flags = epf_bar->flags;
528 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
529 (epf_bar->barno == BAR_5 &&
530 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
531 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
532 flags & PCI_BASE_ADDRESS_IO_MASK) ||
533 (upper_32_bits(epf_bar->size) &&
534 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
537 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
540 if (!epc->ops->set_bar)
543 mutex_lock(&epc->lock);
544 ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
545 mutex_unlock(&epc->lock);
549 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
552 * pci_epc_write_header() - write standard configuration header
553 * @epc: the EPC device to which the configuration header should be written
554 * @func_no: the physical endpoint function number in the EPC device
555 * @vfunc_no: the virtual endpoint function number in the physical function
556 * @header: standard configuration header fields
558 * Invoke to write the configuration header to the endpoint controller. Every
559 * endpoint controller will have a dedicated location to which the standard
560 * configuration header would be written. The callback function should write
561 * the header fields to this dedicated location.
563 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
564 struct pci_epf_header *header)
568 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
571 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
574 /* Only Virtual Function #1 has deviceID */
578 if (!epc->ops->write_header)
581 mutex_lock(&epc->lock);
582 ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
583 mutex_unlock(&epc->lock);
587 EXPORT_SYMBOL_GPL(pci_epc_write_header);
590 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
591 * @epc: the EPC device to which the endpoint function should be added
592 * @epf: the endpoint function to be added
593 * @type: Identifies if the EPC is connected to the primary or secondary
596 * A PCI endpoint device can have one or more functions. In the case of PCIe,
597 * the specification allows up to 8 PCIe endpoint functions. Invoke
598 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
600 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
601 enum pci_epc_interface_type type)
603 struct list_head *list;
607 if (IS_ERR_OR_NULL(epc) || epf->is_vf)
610 if (type == PRIMARY_INTERFACE && epf->epc)
613 if (type == SECONDARY_INTERFACE && epf->sec_epc)
616 mutex_lock(&epc->list_lock);
617 func_no = find_first_zero_bit(&epc->function_num_map,
619 if (func_no >= BITS_PER_LONG) {
624 if (func_no > epc->max_functions - 1) {
625 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
630 set_bit(func_no, &epc->function_num_map);
631 if (type == PRIMARY_INTERFACE) {
632 epf->func_no = func_no;
636 epf->sec_epc_func_no = func_no;
638 list = &epf->sec_epc_list;
641 list_add_tail(list, &epc->pci_epf);
643 mutex_unlock(&epc->list_lock);
647 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
650 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
651 * @epc: the EPC device from which the endpoint function should be removed
652 * @epf: the endpoint function to be removed
653 * @type: identifies if the EPC is connected to the primary or secondary
656 * Invoke to remove PCI endpoint function from the endpoint controller.
658 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
659 enum pci_epc_interface_type type)
661 struct list_head *list;
664 if (!epc || IS_ERR(epc) || !epf)
667 if (type == PRIMARY_INTERFACE) {
668 func_no = epf->func_no;
671 func_no = epf->sec_epc_func_no;
672 list = &epf->sec_epc_list;
675 mutex_lock(&epc->list_lock);
676 clear_bit(func_no, &epc->function_num_map);
679 mutex_unlock(&epc->list_lock);
681 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
684 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
685 * connection with the Root Complex.
686 * @epc: the EPC device which has established link with the host
688 * Invoke to Notify the EPF device that the EPC device has established a
689 * connection with the Root Complex.
691 void pci_epc_linkup(struct pci_epc *epc)
695 if (!epc || IS_ERR(epc))
698 mutex_lock(&epc->list_lock);
699 list_for_each_entry(epf, &epc->pci_epf, list) {
700 mutex_lock(&epf->lock);
701 if (epf->event_ops && epf->event_ops->link_up)
702 epf->event_ops->link_up(epf);
703 mutex_unlock(&epf->lock);
705 mutex_unlock(&epc->list_lock);
707 EXPORT_SYMBOL_GPL(pci_epc_linkup);
710 * pci_epc_linkdown() - Notify the EPF device that EPC device has dropped the
711 * connection with the Root Complex.
712 * @epc: the EPC device which has dropped the link with the host
714 * Invoke to Notify the EPF device that the EPC device has dropped the
715 * connection with the Root Complex.
717 void pci_epc_linkdown(struct pci_epc *epc)
721 if (!epc || IS_ERR(epc))
724 mutex_lock(&epc->list_lock);
725 list_for_each_entry(epf, &epc->pci_epf, list) {
726 mutex_lock(&epf->lock);
727 if (epf->event_ops && epf->event_ops->link_down)
728 epf->event_ops->link_down(epf);
729 mutex_unlock(&epf->lock);
731 mutex_unlock(&epc->list_lock);
733 EXPORT_SYMBOL_GPL(pci_epc_linkdown);
736 * pci_epc_init_notify() - Notify the EPF device that EPC device's core
737 * initialization is completed.
738 * @epc: the EPC device whose core initialization is completed
740 * Invoke to Notify the EPF device that the EPC device's initialization
743 void pci_epc_init_notify(struct pci_epc *epc)
747 if (!epc || IS_ERR(epc))
750 mutex_lock(&epc->list_lock);
751 list_for_each_entry(epf, &epc->pci_epf, list) {
752 mutex_lock(&epf->lock);
753 if (epf->event_ops && epf->event_ops->core_init)
754 epf->event_ops->core_init(epf);
755 mutex_unlock(&epf->lock);
757 mutex_unlock(&epc->list_lock);
759 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
762 * pci_epc_bme_notify() - Notify the EPF device that the EPC device has received
763 * the BME event from the Root complex
764 * @epc: the EPC device that received the BME event
766 * Invoke to Notify the EPF device that the EPC device has received the Bus
767 * Master Enable (BME) event from the Root complex
769 void pci_epc_bme_notify(struct pci_epc *epc)
773 if (!epc || IS_ERR(epc))
776 mutex_lock(&epc->list_lock);
777 list_for_each_entry(epf, &epc->pci_epf, list) {
778 mutex_lock(&epf->lock);
779 if (epf->event_ops && epf->event_ops->bme)
780 epf->event_ops->bme(epf);
781 mutex_unlock(&epf->lock);
783 mutex_unlock(&epc->list_lock);
785 EXPORT_SYMBOL_GPL(pci_epc_bme_notify);
788 * pci_epc_destroy() - destroy the EPC device
789 * @epc: the EPC device that has to be destroyed
791 * Invoke to destroy the PCI EPC device
793 void pci_epc_destroy(struct pci_epc *epc)
795 pci_ep_cfs_remove_epc_group(epc->group);
796 device_unregister(&epc->dev);
798 EXPORT_SYMBOL_GPL(pci_epc_destroy);
801 * devm_pci_epc_destroy() - destroy the EPC device
802 * @dev: device that wants to destroy the EPC
803 * @epc: the EPC device that has to be destroyed
805 * Invoke to destroy the devres associated with this
806 * pci_epc and destroy the EPC device.
808 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
812 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
814 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
816 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
818 static void pci_epc_release(struct device *dev)
820 kfree(to_pci_epc(dev));
824 * __pci_epc_create() - create a new endpoint controller (EPC) device
825 * @dev: device that is creating the new EPC
826 * @ops: function pointers for performing EPC operations
827 * @owner: the owner of the module that creates the EPC device
829 * Invoke to create a new EPC device and add it to pci_epc class.
832 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
833 struct module *owner)
843 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
849 mutex_init(&epc->lock);
850 mutex_init(&epc->list_lock);
851 INIT_LIST_HEAD(&epc->pci_epf);
853 device_initialize(&epc->dev);
854 epc->dev.class = pci_epc_class;
855 epc->dev.parent = dev;
856 epc->dev.release = pci_epc_release;
859 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
863 ret = device_add(&epc->dev);
867 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
872 put_device(&epc->dev);
878 EXPORT_SYMBOL_GPL(__pci_epc_create);
881 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
882 * @dev: device that is creating the new EPC
883 * @ops: function pointers for performing EPC operations
884 * @owner: the owner of the module that creates the EPC device
886 * Invoke to create a new EPC device and add it to pci_epc class.
887 * While at that, it also associates the device with the pci_epc using devres.
888 * On driver detach, release function is invoked on the devres data,
889 * then, devres data is freed.
892 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
893 struct module *owner)
895 struct pci_epc **ptr, *epc;
897 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
899 return ERR_PTR(-ENOMEM);
901 epc = __pci_epc_create(dev, ops, owner);
904 devres_add(dev, ptr);
911 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
913 static int __init pci_epc_init(void)
915 pci_epc_class = class_create("pci_epc");
916 if (IS_ERR(pci_epc_class)) {
917 pr_err("failed to create pci epc class --> %ld\n",
918 PTR_ERR(pci_epc_class));
919 return PTR_ERR(pci_epc_class);
924 module_init(pci_epc_init);
926 static void __exit pci_epc_exit(void)
928 class_destroy(pci_epc_class);
930 module_exit(pci_epc_exit);
932 MODULE_DESCRIPTION("PCI EPC Library");