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>
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
15 #include <linux/pci-ep-cfs.h>
17 static const struct class pci_epc_class = {
21 static void devm_pci_epc_release(struct device *dev, void *res)
23 struct pci_epc *epc = *(struct pci_epc **)res;
28 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
30 struct pci_epc **epc = res;
32 return *epc == match_data;
36 * pci_epc_put() - release the PCI endpoint controller
37 * @epc: epc returned by pci_epc_get()
39 * release the refcount the caller obtained by invoking pci_epc_get()
41 void pci_epc_put(struct pci_epc *epc)
43 if (IS_ERR_OR_NULL(epc))
46 module_put(epc->ops->owner);
47 put_device(&epc->dev);
49 EXPORT_SYMBOL_GPL(pci_epc_put);
52 * pci_epc_get() - get the PCI endpoint controller
53 * @epc_name: device name of the endpoint controller
55 * Invoke to get struct pci_epc * corresponding to the device name of the
58 struct pci_epc *pci_epc_get(const char *epc_name)
63 struct class_dev_iter iter;
65 class_dev_iter_init(&iter, &pci_epc_class, NULL, NULL);
66 while ((dev = class_dev_iter_next(&iter))) {
67 if (strcmp(epc_name, dev_name(dev)))
70 epc = to_pci_epc(dev);
71 if (!try_module_get(epc->ops->owner)) {
76 class_dev_iter_exit(&iter);
77 get_device(&epc->dev);
82 class_dev_iter_exit(&iter);
85 EXPORT_SYMBOL_GPL(pci_epc_get);
88 * pci_epc_get_first_free_bar() - helper to get first unreserved BAR
89 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
91 * Invoke to get the first unreserved BAR that can be used by the endpoint
95 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
97 return pci_epc_get_next_free_bar(epc_features, BAR_0);
99 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
102 * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
103 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
104 * @bar: the starting BAR number from where unreserved BAR should be searched
106 * Invoke to get the next unreserved BAR starting from @bar that can be used
107 * for endpoint function.
109 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
110 *epc_features, enum pci_barno bar)
117 /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
118 if (bar > 0 && epc_features->bar[bar - 1].only_64bit)
121 for (i = bar; i < PCI_STD_NUM_BARS; i++) {
122 /* If the BAR is not reserved, return it. */
123 if (epc_features->bar[i].type != BAR_RESERVED)
129 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
132 * pci_epc_get_features() - get the features supported by EPC
133 * @epc: the features supported by *this* EPC device will be returned
134 * @func_no: the features supported by the EPC device specific to the
135 * endpoint function with func_no will be returned
136 * @vfunc_no: the features supported by the EPC device specific to the
137 * virtual endpoint function with vfunc_no will be returned
139 * Invoke to get the features provided by the EPC which may be
140 * specific to an endpoint function. Returns pci_epc_features on success
141 * and NULL for any failures.
143 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
144 u8 func_no, u8 vfunc_no)
146 const struct pci_epc_features *epc_features;
148 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
151 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
154 if (!epc->ops->get_features)
157 mutex_lock(&epc->lock);
158 epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
159 mutex_unlock(&epc->lock);
163 EXPORT_SYMBOL_GPL(pci_epc_get_features);
166 * pci_epc_stop() - stop the PCI link
167 * @epc: the link of the EPC device that has to be stopped
169 * Invoke to stop the PCI link
171 void pci_epc_stop(struct pci_epc *epc)
173 if (IS_ERR(epc) || !epc->ops->stop)
176 mutex_lock(&epc->lock);
178 mutex_unlock(&epc->lock);
180 EXPORT_SYMBOL_GPL(pci_epc_stop);
183 * pci_epc_start() - start the PCI link
184 * @epc: the link of *this* EPC device has to be started
186 * Invoke to start the PCI link
188 int pci_epc_start(struct pci_epc *epc)
195 if (!epc->ops->start)
198 mutex_lock(&epc->lock);
199 ret = epc->ops->start(epc);
200 mutex_unlock(&epc->lock);
204 EXPORT_SYMBOL_GPL(pci_epc_start);
207 * pci_epc_raise_irq() - interrupt the host system
208 * @epc: the EPC device which has to interrupt the host
209 * @func_no: the physical endpoint function number in the EPC device
210 * @vfunc_no: the virtual endpoint function number in the physical function
211 * @type: specify the type of interrupt; INTX, MSI or MSI-X
212 * @interrupt_num: the MSI or MSI-X interrupt number with range (1-N)
214 * Invoke to raise an INTX, MSI or MSI-X interrupt
216 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
217 unsigned int type, u16 interrupt_num)
221 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
224 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
227 if (!epc->ops->raise_irq)
230 mutex_lock(&epc->lock);
231 ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
232 mutex_unlock(&epc->lock);
236 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
239 * pci_epc_map_msi_irq() - Map physical address to MSI address and return
241 * @epc: the EPC device which has the MSI capability
242 * @func_no: the physical endpoint function number in the EPC device
243 * @vfunc_no: the virtual endpoint function number in the physical function
244 * @phys_addr: the physical address of the outbound region
245 * @interrupt_num: the MSI interrupt number with range (1-N)
246 * @entry_size: Size of Outbound address region for each interrupt
247 * @msi_data: the data that should be written in order to raise MSI interrupt
248 * with interrupt number as 'interrupt num'
249 * @msi_addr_offset: Offset of MSI address from the aligned outbound address
250 * to which the MSI address is mapped
252 * Invoke to map physical address to MSI address and return MSI data. The
253 * physical address should be an address in the outbound region. This is
254 * required to implement doorbell functionality of NTB wherein EPC on either
255 * side of the interface (primary and secondary) can directly write to the
256 * physical address (in outbound region) of the other interface to ring
259 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
260 phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
261 u32 *msi_data, u32 *msi_addr_offset)
265 if (IS_ERR_OR_NULL(epc))
268 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
271 if (!epc->ops->map_msi_irq)
274 mutex_lock(&epc->lock);
275 ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
276 interrupt_num, entry_size, msi_data,
278 mutex_unlock(&epc->lock);
282 EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
285 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
286 * @epc: the EPC device to which MSI interrupts was requested
287 * @func_no: the physical endpoint function number in the EPC device
288 * @vfunc_no: the virtual endpoint function number in the physical function
290 * Invoke to get the number of MSI interrupts allocated by the RC
292 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
296 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
299 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
302 if (!epc->ops->get_msi)
305 mutex_lock(&epc->lock);
306 interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
307 mutex_unlock(&epc->lock);
312 interrupt = 1 << interrupt;
316 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
319 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
320 * @epc: the EPC device on which MSI has to be configured
321 * @func_no: the physical endpoint function number in the EPC device
322 * @vfunc_no: the virtual endpoint function number in the physical function
323 * @interrupts: number of MSI interrupts required by the EPF
325 * Invoke to set the required number of MSI interrupts.
327 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
332 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
333 interrupts < 1 || interrupts > 32)
336 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
339 if (!epc->ops->set_msi)
342 encode_int = order_base_2(interrupts);
344 mutex_lock(&epc->lock);
345 ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int);
346 mutex_unlock(&epc->lock);
350 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
353 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
354 * @epc: the EPC device to which MSI-X interrupts was requested
355 * @func_no: the physical endpoint function number in the EPC device
356 * @vfunc_no: the virtual endpoint function number in the physical function
358 * Invoke to get the number of MSI-X interrupts allocated by the RC
360 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
364 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
367 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
370 if (!epc->ops->get_msix)
373 mutex_lock(&epc->lock);
374 interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
375 mutex_unlock(&epc->lock);
380 return interrupt + 1;
382 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
385 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
386 * @epc: the EPC device on which MSI-X has to be configured
387 * @func_no: the physical endpoint function number in the EPC device
388 * @vfunc_no: the virtual endpoint function number in the physical function
389 * @interrupts: number of MSI-X interrupts required by the EPF
390 * @bir: BAR where the MSI-X table resides
391 * @offset: Offset pointing to the start of MSI-X table
393 * Invoke to set the required number of MSI-X interrupts.
395 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
396 u16 interrupts, enum pci_barno bir, u32 offset)
400 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
401 interrupts < 1 || interrupts > 2048)
404 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
407 if (!epc->ops->set_msix)
410 mutex_lock(&epc->lock);
411 ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir,
413 mutex_unlock(&epc->lock);
417 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
420 * pci_epc_unmap_addr() - unmap CPU address from PCI address
421 * @epc: the EPC device on which address is allocated
422 * @func_no: the physical endpoint function number in the EPC device
423 * @vfunc_no: the virtual endpoint function number in the physical function
424 * @phys_addr: physical address of the local system
426 * Invoke to unmap the CPU address from PCI address.
428 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
429 phys_addr_t phys_addr)
431 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
434 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
437 if (!epc->ops->unmap_addr)
440 mutex_lock(&epc->lock);
441 epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
442 mutex_unlock(&epc->lock);
444 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
447 * pci_epc_map_addr() - map CPU address to PCI address
448 * @epc: the EPC device on which address is allocated
449 * @func_no: the physical endpoint function number in the EPC device
450 * @vfunc_no: the virtual endpoint function number in the physical function
451 * @phys_addr: physical address of the local system
452 * @pci_addr: PCI address to which the physical address should be mapped
453 * @size: the size of the allocation
455 * Invoke to map CPU address with PCI address.
457 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
458 phys_addr_t phys_addr, u64 pci_addr, size_t size)
462 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
465 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
468 if (!epc->ops->map_addr)
471 mutex_lock(&epc->lock);
472 ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
474 mutex_unlock(&epc->lock);
478 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
481 * pci_epc_clear_bar() - reset the BAR
482 * @epc: the EPC device for which the BAR has to be cleared
483 * @func_no: the physical endpoint function number in the EPC device
484 * @vfunc_no: the virtual endpoint function number in the physical function
485 * @epf_bar: the struct epf_bar that contains the BAR information
487 * Invoke to reset the BAR of the endpoint device.
489 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
490 struct pci_epf_bar *epf_bar)
492 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
493 (epf_bar->barno == BAR_5 &&
494 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
497 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
500 if (!epc->ops->clear_bar)
503 mutex_lock(&epc->lock);
504 epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
505 mutex_unlock(&epc->lock);
507 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
510 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
511 * @epc: the EPC device on which BAR has to be configured
512 * @func_no: the physical endpoint function number in the EPC device
513 * @vfunc_no: the virtual endpoint function number in the physical function
514 * @epf_bar: the struct epf_bar that contains the BAR information
516 * Invoke to configure the BAR of the endpoint device.
518 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
519 struct pci_epf_bar *epf_bar)
522 int flags = epf_bar->flags;
524 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
525 (epf_bar->barno == BAR_5 &&
526 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
527 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
528 flags & PCI_BASE_ADDRESS_IO_MASK) ||
529 (upper_32_bits(epf_bar->size) &&
530 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
533 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
536 if (!epc->ops->set_bar)
539 mutex_lock(&epc->lock);
540 ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
541 mutex_unlock(&epc->lock);
545 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
548 * pci_epc_write_header() - write standard configuration header
549 * @epc: the EPC device to which the configuration header should be written
550 * @func_no: the physical endpoint function number in the EPC device
551 * @vfunc_no: the virtual endpoint function number in the physical function
552 * @header: standard configuration header fields
554 * Invoke to write the configuration header to the endpoint controller. Every
555 * endpoint controller will have a dedicated location to which the standard
556 * configuration header would be written. The callback function should write
557 * the header fields to this dedicated location.
559 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
560 struct pci_epf_header *header)
564 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
567 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
570 /* Only Virtual Function #1 has deviceID */
574 if (!epc->ops->write_header)
577 mutex_lock(&epc->lock);
578 ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
579 mutex_unlock(&epc->lock);
583 EXPORT_SYMBOL_GPL(pci_epc_write_header);
586 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
587 * @epc: the EPC device to which the endpoint function should be added
588 * @epf: the endpoint function to be added
589 * @type: Identifies if the EPC is connected to the primary or secondary
592 * A PCI endpoint device can have one or more functions. In the case of PCIe,
593 * the specification allows up to 8 PCIe endpoint functions. Invoke
594 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
596 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
597 enum pci_epc_interface_type type)
599 struct list_head *list;
603 if (IS_ERR_OR_NULL(epc) || epf->is_vf)
606 if (type == PRIMARY_INTERFACE && epf->epc)
609 if (type == SECONDARY_INTERFACE && epf->sec_epc)
612 mutex_lock(&epc->list_lock);
613 func_no = find_first_zero_bit(&epc->function_num_map,
615 if (func_no >= BITS_PER_LONG) {
620 if (func_no > epc->max_functions - 1) {
621 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
626 set_bit(func_no, &epc->function_num_map);
627 if (type == PRIMARY_INTERFACE) {
628 epf->func_no = func_no;
632 epf->sec_epc_func_no = func_no;
634 list = &epf->sec_epc_list;
637 list_add_tail(list, &epc->pci_epf);
639 mutex_unlock(&epc->list_lock);
643 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
646 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
647 * @epc: the EPC device from which the endpoint function should be removed
648 * @epf: the endpoint function to be removed
649 * @type: identifies if the EPC is connected to the primary or secondary
652 * Invoke to remove PCI endpoint function from the endpoint controller.
654 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
655 enum pci_epc_interface_type type)
657 struct list_head *list;
660 if (IS_ERR_OR_NULL(epc) || !epf)
663 if (type == PRIMARY_INTERFACE) {
664 func_no = epf->func_no;
667 func_no = epf->sec_epc_func_no;
668 list = &epf->sec_epc_list;
671 mutex_lock(&epc->list_lock);
672 clear_bit(func_no, &epc->function_num_map);
675 mutex_unlock(&epc->list_lock);
677 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
680 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
681 * connection with the Root Complex.
682 * @epc: the EPC device which has established link with the host
684 * Invoke to Notify the EPF device that the EPC device has established a
685 * connection with the Root Complex.
687 void pci_epc_linkup(struct pci_epc *epc)
691 if (IS_ERR_OR_NULL(epc))
694 mutex_lock(&epc->list_lock);
695 list_for_each_entry(epf, &epc->pci_epf, list) {
696 mutex_lock(&epf->lock);
697 if (epf->event_ops && epf->event_ops->link_up)
698 epf->event_ops->link_up(epf);
699 mutex_unlock(&epf->lock);
701 mutex_unlock(&epc->list_lock);
703 EXPORT_SYMBOL_GPL(pci_epc_linkup);
706 * pci_epc_linkdown() - Notify the EPF device that EPC device has dropped the
707 * connection with the Root Complex.
708 * @epc: the EPC device which has dropped the link with the host
710 * Invoke to Notify the EPF device that the EPC device has dropped the
711 * connection with the Root Complex.
713 void pci_epc_linkdown(struct pci_epc *epc)
717 if (IS_ERR_OR_NULL(epc))
720 mutex_lock(&epc->list_lock);
721 list_for_each_entry(epf, &epc->pci_epf, list) {
722 mutex_lock(&epf->lock);
723 if (epf->event_ops && epf->event_ops->link_down)
724 epf->event_ops->link_down(epf);
725 mutex_unlock(&epf->lock);
727 mutex_unlock(&epc->list_lock);
729 EXPORT_SYMBOL_GPL(pci_epc_linkdown);
732 * pci_epc_init_notify() - Notify the EPF device that EPC device initialization
734 * @epc: the EPC device whose initialization is completed
736 * Invoke to Notify the EPF device that the EPC device's initialization
739 void pci_epc_init_notify(struct pci_epc *epc)
743 if (IS_ERR_OR_NULL(epc))
746 mutex_lock(&epc->list_lock);
747 list_for_each_entry(epf, &epc->pci_epf, list) {
748 mutex_lock(&epf->lock);
749 if (epf->event_ops && epf->event_ops->epc_init)
750 epf->event_ops->epc_init(epf);
751 mutex_unlock(&epf->lock);
753 epc->init_complete = true;
754 mutex_unlock(&epc->list_lock);
756 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
759 * pci_epc_notify_pending_init() - Notify the pending EPC device initialization
760 * complete to the EPF device
761 * @epc: the EPC device whose initialization is pending to be notified
762 * @epf: the EPF device to be notified
764 * Invoke to notify the pending EPC device initialization complete to the EPF
765 * device. This is used to deliver the notification if the EPC initialization
766 * got completed before the EPF driver bind.
768 void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf)
770 if (epc->init_complete) {
771 mutex_lock(&epf->lock);
772 if (epf->event_ops && epf->event_ops->epc_init)
773 epf->event_ops->epc_init(epf);
774 mutex_unlock(&epf->lock);
777 EXPORT_SYMBOL_GPL(pci_epc_notify_pending_init);
780 * pci_epc_deinit_notify() - Notify the EPF device about EPC deinitialization
781 * @epc: the EPC device whose deinitialization is completed
783 * Invoke to notify the EPF device that the EPC deinitialization is completed.
785 void pci_epc_deinit_notify(struct pci_epc *epc)
789 if (IS_ERR_OR_NULL(epc))
792 mutex_lock(&epc->list_lock);
793 list_for_each_entry(epf, &epc->pci_epf, list) {
794 mutex_lock(&epf->lock);
795 if (epf->event_ops && epf->event_ops->epc_deinit)
796 epf->event_ops->epc_deinit(epf);
797 mutex_unlock(&epf->lock);
799 epc->init_complete = false;
800 mutex_unlock(&epc->list_lock);
802 EXPORT_SYMBOL_GPL(pci_epc_deinit_notify);
805 * pci_epc_bus_master_enable_notify() - Notify the EPF device that the EPC
806 * device has received the Bus Master
807 * Enable event from the Root complex
808 * @epc: the EPC device that received the Bus Master Enable event
810 * Notify the EPF device that the EPC device has generated the Bus Master Enable
811 * event due to host setting the Bus Master Enable bit in the Command register.
813 void pci_epc_bus_master_enable_notify(struct pci_epc *epc)
817 if (IS_ERR_OR_NULL(epc))
820 mutex_lock(&epc->list_lock);
821 list_for_each_entry(epf, &epc->pci_epf, list) {
822 mutex_lock(&epf->lock);
823 if (epf->event_ops && epf->event_ops->bus_master_enable)
824 epf->event_ops->bus_master_enable(epf);
825 mutex_unlock(&epf->lock);
827 mutex_unlock(&epc->list_lock);
829 EXPORT_SYMBOL_GPL(pci_epc_bus_master_enable_notify);
832 * pci_epc_destroy() - destroy the EPC device
833 * @epc: the EPC device that has to be destroyed
835 * Invoke to destroy the PCI EPC device
837 void pci_epc_destroy(struct pci_epc *epc)
839 pci_ep_cfs_remove_epc_group(epc->group);
840 device_unregister(&epc->dev);
842 EXPORT_SYMBOL_GPL(pci_epc_destroy);
845 * devm_pci_epc_destroy() - destroy the EPC device
846 * @dev: device that wants to destroy the EPC
847 * @epc: the EPC device that has to be destroyed
849 * Invoke to destroy the devres associated with this
850 * pci_epc and destroy the EPC device.
852 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
856 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
858 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
860 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
862 static void pci_epc_release(struct device *dev)
864 kfree(to_pci_epc(dev));
868 * __pci_epc_create() - create a new endpoint controller (EPC) device
869 * @dev: device that is creating the new EPC
870 * @ops: function pointers for performing EPC operations
871 * @owner: the owner of the module that creates the EPC device
873 * Invoke to create a new EPC device and add it to pci_epc class.
876 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
877 struct module *owner)
887 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
893 mutex_init(&epc->lock);
894 mutex_init(&epc->list_lock);
895 INIT_LIST_HEAD(&epc->pci_epf);
897 device_initialize(&epc->dev);
898 epc->dev.class = &pci_epc_class;
899 epc->dev.parent = dev;
900 epc->dev.release = pci_epc_release;
903 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
907 ret = device_add(&epc->dev);
911 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
916 put_device(&epc->dev);
921 EXPORT_SYMBOL_GPL(__pci_epc_create);
924 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
925 * @dev: device that is creating the new EPC
926 * @ops: function pointers for performing EPC operations
927 * @owner: the owner of the module that creates the EPC device
929 * Invoke to create a new EPC device and add it to pci_epc class.
930 * While at that, it also associates the device with the pci_epc using devres.
931 * On driver detach, release function is invoked on the devres data,
932 * then, devres data is freed.
935 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
936 struct module *owner)
938 struct pci_epc **ptr, *epc;
940 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
942 return ERR_PTR(-ENOMEM);
944 epc = __pci_epc_create(dev, ops, owner);
947 devres_add(dev, ptr);
954 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
956 static int __init pci_epc_init(void)
958 return class_register(&pci_epc_class);
960 module_init(pci_epc_init);
962 static void __exit pci_epc_exit(void)
964 class_unregister(&pci_epc_class);
966 module_exit(pci_epc_exit);
968 MODULE_DESCRIPTION("PCI EPC Library");