Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * PCI Endpoint *Controller* (EPC) library
0004  *
0005  * Copyright (C) 2017 Texas Instruments
0006  * Author: Kishon Vijay Abraham I <kishon@ti.com>
0007  */
0008 
0009 #include <linux/device.h>
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013 
0014 #include <linux/pci-epc.h>
0015 #include <linux/pci-epf.h>
0016 #include <linux/pci-ep-cfs.h>
0017 
0018 static struct class *pci_epc_class;
0019 
0020 static void devm_pci_epc_release(struct device *dev, void *res)
0021 {
0022     struct pci_epc *epc = *(struct pci_epc **)res;
0023 
0024     pci_epc_destroy(epc);
0025 }
0026 
0027 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
0028 {
0029     struct pci_epc **epc = res;
0030 
0031     return *epc == match_data;
0032 }
0033 
0034 /**
0035  * pci_epc_put() - release the PCI endpoint controller
0036  * @epc: epc returned by pci_epc_get()
0037  *
0038  * release the refcount the caller obtained by invoking pci_epc_get()
0039  */
0040 void pci_epc_put(struct pci_epc *epc)
0041 {
0042     if (!epc || IS_ERR(epc))
0043         return;
0044 
0045     module_put(epc->ops->owner);
0046     put_device(&epc->dev);
0047 }
0048 EXPORT_SYMBOL_GPL(pci_epc_put);
0049 
0050 /**
0051  * pci_epc_get() - get the PCI endpoint controller
0052  * @epc_name: device name of the endpoint controller
0053  *
0054  * Invoke to get struct pci_epc * corresponding to the device name of the
0055  * endpoint controller
0056  */
0057 struct pci_epc *pci_epc_get(const char *epc_name)
0058 {
0059     int ret = -EINVAL;
0060     struct pci_epc *epc;
0061     struct device *dev;
0062     struct class_dev_iter iter;
0063 
0064     class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
0065     while ((dev = class_dev_iter_next(&iter))) {
0066         if (strcmp(epc_name, dev_name(dev)))
0067             continue;
0068 
0069         epc = to_pci_epc(dev);
0070         if (!try_module_get(epc->ops->owner)) {
0071             ret = -EINVAL;
0072             goto err;
0073         }
0074 
0075         class_dev_iter_exit(&iter);
0076         get_device(&epc->dev);
0077         return epc;
0078     }
0079 
0080 err:
0081     class_dev_iter_exit(&iter);
0082     return ERR_PTR(ret);
0083 }
0084 EXPORT_SYMBOL_GPL(pci_epc_get);
0085 
0086 /**
0087  * pci_epc_get_first_free_bar() - helper to get first unreserved BAR
0088  * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
0089  *
0090  * Invoke to get the first unreserved BAR that can be used by the endpoint
0091  * function. For any incorrect value in reserved_bar return '0'.
0092  */
0093 enum pci_barno
0094 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
0095 {
0096     return pci_epc_get_next_free_bar(epc_features, BAR_0);
0097 }
0098 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
0099 
0100 /**
0101  * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
0102  * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
0103  * @bar: the starting BAR number from where unreserved BAR should be searched
0104  *
0105  * Invoke to get the next unreserved BAR starting from @bar that can be used
0106  * for endpoint function. For any incorrect value in reserved_bar return '0'.
0107  */
0108 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
0109                      *epc_features, enum pci_barno bar)
0110 {
0111     unsigned long free_bar;
0112 
0113     if (!epc_features)
0114         return BAR_0;
0115 
0116     /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
0117     if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
0118         bar++;
0119 
0120     /* Find if the reserved BAR is also a 64-bit BAR */
0121     free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
0122 
0123     /* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */
0124     free_bar <<= 1;
0125     free_bar |= epc_features->reserved_bar;
0126 
0127     free_bar = find_next_zero_bit(&free_bar, 6, bar);
0128     if (free_bar > 5)
0129         return NO_BAR;
0130 
0131     return free_bar;
0132 }
0133 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
0134 
0135 /**
0136  * pci_epc_get_features() - get the features supported by EPC
0137  * @epc: the features supported by *this* EPC device will be returned
0138  * @func_no: the features supported by the EPC device specific to the
0139  *       endpoint function with func_no will be returned
0140  * @vfunc_no: the features supported by the EPC device specific to the
0141  *       virtual endpoint function with vfunc_no will be returned
0142  *
0143  * Invoke to get the features provided by the EPC which may be
0144  * specific to an endpoint function. Returns pci_epc_features on success
0145  * and NULL for any failures.
0146  */
0147 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
0148                             u8 func_no, u8 vfunc_no)
0149 {
0150     const struct pci_epc_features *epc_features;
0151 
0152     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0153         return NULL;
0154 
0155     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0156         return NULL;
0157 
0158     if (!epc->ops->get_features)
0159         return NULL;
0160 
0161     mutex_lock(&epc->lock);
0162     epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
0163     mutex_unlock(&epc->lock);
0164 
0165     return epc_features;
0166 }
0167 EXPORT_SYMBOL_GPL(pci_epc_get_features);
0168 
0169 /**
0170  * pci_epc_stop() - stop the PCI link
0171  * @epc: the link of the EPC device that has to be stopped
0172  *
0173  * Invoke to stop the PCI link
0174  */
0175 void pci_epc_stop(struct pci_epc *epc)
0176 {
0177     if (IS_ERR(epc) || !epc->ops->stop)
0178         return;
0179 
0180     mutex_lock(&epc->lock);
0181     epc->ops->stop(epc);
0182     mutex_unlock(&epc->lock);
0183 }
0184 EXPORT_SYMBOL_GPL(pci_epc_stop);
0185 
0186 /**
0187  * pci_epc_start() - start the PCI link
0188  * @epc: the link of *this* EPC device has to be started
0189  *
0190  * Invoke to start the PCI link
0191  */
0192 int pci_epc_start(struct pci_epc *epc)
0193 {
0194     int ret;
0195 
0196     if (IS_ERR(epc))
0197         return -EINVAL;
0198 
0199     if (!epc->ops->start)
0200         return 0;
0201 
0202     mutex_lock(&epc->lock);
0203     ret = epc->ops->start(epc);
0204     mutex_unlock(&epc->lock);
0205 
0206     return ret;
0207 }
0208 EXPORT_SYMBOL_GPL(pci_epc_start);
0209 
0210 /**
0211  * pci_epc_raise_irq() - interrupt the host system
0212  * @epc: the EPC device which has to interrupt the host
0213  * @func_no: the physical endpoint function number in the EPC device
0214  * @vfunc_no: the virtual endpoint function number in the physical function
0215  * @type: specify the type of interrupt; legacy, MSI or MSI-X
0216  * @interrupt_num: the MSI or MSI-X interrupt number
0217  *
0218  * Invoke to raise an legacy, MSI or MSI-X interrupt
0219  */
0220 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0221               enum pci_epc_irq_type type, u16 interrupt_num)
0222 {
0223     int ret;
0224 
0225     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0226         return -EINVAL;
0227 
0228     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0229         return -EINVAL;
0230 
0231     if (!epc->ops->raise_irq)
0232         return 0;
0233 
0234     mutex_lock(&epc->lock);
0235     ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
0236     mutex_unlock(&epc->lock);
0237 
0238     return ret;
0239 }
0240 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
0241 
0242 /**
0243  * pci_epc_map_msi_irq() - Map physical address to MSI address and return
0244  *                         MSI data
0245  * @epc: the EPC device which has the MSI capability
0246  * @func_no: the physical endpoint function number in the EPC device
0247  * @vfunc_no: the virtual endpoint function number in the physical function
0248  * @phys_addr: the physical address of the outbound region
0249  * @interrupt_num: the MSI interrupt number
0250  * @entry_size: Size of Outbound address region for each interrupt
0251  * @msi_data: the data that should be written in order to raise MSI interrupt
0252  *            with interrupt number as 'interrupt num'
0253  * @msi_addr_offset: Offset of MSI address from the aligned outbound address
0254  *                   to which the MSI address is mapped
0255  *
0256  * Invoke to map physical address to MSI address and return MSI data. The
0257  * physical address should be an address in the outbound region. This is
0258  * required to implement doorbell functionality of NTB wherein EPC on either
0259  * side of the interface (primary and secondary) can directly write to the
0260  * physical address (in outbound region) of the other interface to ring
0261  * doorbell.
0262  */
0263 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0264             phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
0265             u32 *msi_data, u32 *msi_addr_offset)
0266 {
0267     int ret;
0268 
0269     if (IS_ERR_OR_NULL(epc))
0270         return -EINVAL;
0271 
0272     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0273         return -EINVAL;
0274 
0275     if (!epc->ops->map_msi_irq)
0276         return -EINVAL;
0277 
0278     mutex_lock(&epc->lock);
0279     ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
0280                     interrupt_num, entry_size, msi_data,
0281                     msi_addr_offset);
0282     mutex_unlock(&epc->lock);
0283 
0284     return ret;
0285 }
0286 EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
0287 
0288 /**
0289  * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
0290  * @epc: the EPC device to which MSI interrupts was requested
0291  * @func_no: the physical endpoint function number in the EPC device
0292  * @vfunc_no: the virtual endpoint function number in the physical function
0293  *
0294  * Invoke to get the number of MSI interrupts allocated by the RC
0295  */
0296 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0297 {
0298     int interrupt;
0299 
0300     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0301         return 0;
0302 
0303     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0304         return 0;
0305 
0306     if (!epc->ops->get_msi)
0307         return 0;
0308 
0309     mutex_lock(&epc->lock);
0310     interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
0311     mutex_unlock(&epc->lock);
0312 
0313     if (interrupt < 0)
0314         return 0;
0315 
0316     interrupt = 1 << interrupt;
0317 
0318     return interrupt;
0319 }
0320 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
0321 
0322 /**
0323  * pci_epc_set_msi() - set the number of MSI interrupt numbers required
0324  * @epc: the EPC device on which MSI has to be configured
0325  * @func_no: the physical endpoint function number in the EPC device
0326  * @vfunc_no: the virtual endpoint function number in the physical function
0327  * @interrupts: number of MSI interrupts required by the EPF
0328  *
0329  * Invoke to set the required number of MSI interrupts.
0330  */
0331 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
0332 {
0333     int ret;
0334     u8 encode_int;
0335 
0336     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0337         interrupts < 1 || interrupts > 32)
0338         return -EINVAL;
0339 
0340     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0341         return -EINVAL;
0342 
0343     if (!epc->ops->set_msi)
0344         return 0;
0345 
0346     encode_int = order_base_2(interrupts);
0347 
0348     mutex_lock(&epc->lock);
0349     ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int);
0350     mutex_unlock(&epc->lock);
0351 
0352     return ret;
0353 }
0354 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
0355 
0356 /**
0357  * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
0358  * @epc: the EPC device to which MSI-X interrupts was requested
0359  * @func_no: the physical endpoint function number in the EPC device
0360  * @vfunc_no: the virtual endpoint function number in the physical function
0361  *
0362  * Invoke to get the number of MSI-X interrupts allocated by the RC
0363  */
0364 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0365 {
0366     int interrupt;
0367 
0368     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0369         return 0;
0370 
0371     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0372         return 0;
0373 
0374     if (!epc->ops->get_msix)
0375         return 0;
0376 
0377     mutex_lock(&epc->lock);
0378     interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
0379     mutex_unlock(&epc->lock);
0380 
0381     if (interrupt < 0)
0382         return 0;
0383 
0384     return interrupt + 1;
0385 }
0386 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
0387 
0388 /**
0389  * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
0390  * @epc: the EPC device on which MSI-X has to be configured
0391  * @func_no: the physical endpoint function number in the EPC device
0392  * @vfunc_no: the virtual endpoint function number in the physical function
0393  * @interrupts: number of MSI-X interrupts required by the EPF
0394  * @bir: BAR where the MSI-X table resides
0395  * @offset: Offset pointing to the start of MSI-X table
0396  *
0397  * Invoke to set the required number of MSI-X interrupts.
0398  */
0399 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0400              u16 interrupts, enum pci_barno bir, u32 offset)
0401 {
0402     int ret;
0403 
0404     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0405         interrupts < 1 || interrupts > 2048)
0406         return -EINVAL;
0407 
0408     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0409         return -EINVAL;
0410 
0411     if (!epc->ops->set_msix)
0412         return 0;
0413 
0414     mutex_lock(&epc->lock);
0415     ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir,
0416                  offset);
0417     mutex_unlock(&epc->lock);
0418 
0419     return ret;
0420 }
0421 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
0422 
0423 /**
0424  * pci_epc_unmap_addr() - unmap CPU address from PCI address
0425  * @epc: the EPC device on which address is allocated
0426  * @func_no: the physical endpoint function number in the EPC device
0427  * @vfunc_no: the virtual endpoint function number in the physical function
0428  * @phys_addr: physical address of the local system
0429  *
0430  * Invoke to unmap the CPU address from PCI address.
0431  */
0432 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0433             phys_addr_t phys_addr)
0434 {
0435     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0436         return;
0437 
0438     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0439         return;
0440 
0441     if (!epc->ops->unmap_addr)
0442         return;
0443 
0444     mutex_lock(&epc->lock);
0445     epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
0446     mutex_unlock(&epc->lock);
0447 }
0448 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
0449 
0450 /**
0451  * pci_epc_map_addr() - map CPU address to PCI address
0452  * @epc: the EPC device on which address is allocated
0453  * @func_no: the physical endpoint function number in the EPC device
0454  * @vfunc_no: the virtual endpoint function number in the physical function
0455  * @phys_addr: physical address of the local system
0456  * @pci_addr: PCI address to which the physical address should be mapped
0457  * @size: the size of the allocation
0458  *
0459  * Invoke to map CPU address with PCI address.
0460  */
0461 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0462              phys_addr_t phys_addr, u64 pci_addr, size_t size)
0463 {
0464     int ret;
0465 
0466     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0467         return -EINVAL;
0468 
0469     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0470         return -EINVAL;
0471 
0472     if (!epc->ops->map_addr)
0473         return 0;
0474 
0475     mutex_lock(&epc->lock);
0476     ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
0477                  size);
0478     mutex_unlock(&epc->lock);
0479 
0480     return ret;
0481 }
0482 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
0483 
0484 /**
0485  * pci_epc_clear_bar() - reset the BAR
0486  * @epc: the EPC device for which the BAR has to be cleared
0487  * @func_no: the physical endpoint function number in the EPC device
0488  * @vfunc_no: the virtual endpoint function number in the physical function
0489  * @epf_bar: the struct epf_bar that contains the BAR information
0490  *
0491  * Invoke to reset the BAR of the endpoint device.
0492  */
0493 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0494                struct pci_epf_bar *epf_bar)
0495 {
0496     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0497         (epf_bar->barno == BAR_5 &&
0498          epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
0499         return;
0500 
0501     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0502         return;
0503 
0504     if (!epc->ops->clear_bar)
0505         return;
0506 
0507     mutex_lock(&epc->lock);
0508     epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
0509     mutex_unlock(&epc->lock);
0510 }
0511 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
0512 
0513 /**
0514  * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
0515  * @epc: the EPC device on which BAR has to be configured
0516  * @func_no: the physical endpoint function number in the EPC device
0517  * @vfunc_no: the virtual endpoint function number in the physical function
0518  * @epf_bar: the struct epf_bar that contains the BAR information
0519  *
0520  * Invoke to configure the BAR of the endpoint device.
0521  */
0522 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0523             struct pci_epf_bar *epf_bar)
0524 {
0525     int ret;
0526     int flags = epf_bar->flags;
0527 
0528     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0529         (epf_bar->barno == BAR_5 &&
0530          flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
0531         (flags & PCI_BASE_ADDRESS_SPACE_IO &&
0532          flags & PCI_BASE_ADDRESS_IO_MASK) ||
0533         (upper_32_bits(epf_bar->size) &&
0534          !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
0535         return -EINVAL;
0536 
0537     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0538         return -EINVAL;
0539 
0540     if (!epc->ops->set_bar)
0541         return 0;
0542 
0543     mutex_lock(&epc->lock);
0544     ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
0545     mutex_unlock(&epc->lock);
0546 
0547     return ret;
0548 }
0549 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
0550 
0551 /**
0552  * pci_epc_write_header() - write standard configuration header
0553  * @epc: the EPC device to which the configuration header should be written
0554  * @func_no: the physical endpoint function number in the EPC device
0555  * @vfunc_no: the virtual endpoint function number in the physical function
0556  * @header: standard configuration header fields
0557  *
0558  * Invoke to write the configuration header to the endpoint controller. Every
0559  * endpoint controller will have a dedicated location to which the standard
0560  * configuration header would be written. The callback function should write
0561  * the header fields to this dedicated location.
0562  */
0563 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0564              struct pci_epf_header *header)
0565 {
0566     int ret;
0567 
0568     if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0569         return -EINVAL;
0570 
0571     if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0572         return -EINVAL;
0573 
0574     /* Only Virtual Function #1 has deviceID */
0575     if (vfunc_no > 1)
0576         return -EINVAL;
0577 
0578     if (!epc->ops->write_header)
0579         return 0;
0580 
0581     mutex_lock(&epc->lock);
0582     ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
0583     mutex_unlock(&epc->lock);
0584 
0585     return ret;
0586 }
0587 EXPORT_SYMBOL_GPL(pci_epc_write_header);
0588 
0589 /**
0590  * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
0591  * @epc: the EPC device to which the endpoint function should be added
0592  * @epf: the endpoint function to be added
0593  * @type: Identifies if the EPC is connected to the primary or secondary
0594  *        interface of EPF
0595  *
0596  * A PCI endpoint device can have one or more functions. In the case of PCIe,
0597  * the specification allows up to 8 PCIe endpoint functions. Invoke
0598  * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
0599  */
0600 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
0601             enum pci_epc_interface_type type)
0602 {
0603     struct list_head *list;
0604     u32 func_no;
0605     int ret = 0;
0606 
0607     if (IS_ERR_OR_NULL(epc) || epf->is_vf)
0608         return -EINVAL;
0609 
0610     if (type == PRIMARY_INTERFACE && epf->epc)
0611         return -EBUSY;
0612 
0613     if (type == SECONDARY_INTERFACE && epf->sec_epc)
0614         return -EBUSY;
0615 
0616     mutex_lock(&epc->lock);
0617     func_no = find_first_zero_bit(&epc->function_num_map,
0618                       BITS_PER_LONG);
0619     if (func_no >= BITS_PER_LONG) {
0620         ret = -EINVAL;
0621         goto ret;
0622     }
0623 
0624     if (func_no > epc->max_functions - 1) {
0625         dev_err(&epc->dev, "Exceeding max supported Function Number\n");
0626         ret = -EINVAL;
0627         goto ret;
0628     }
0629 
0630     set_bit(func_no, &epc->function_num_map);
0631     if (type == PRIMARY_INTERFACE) {
0632         epf->func_no = func_no;
0633         epf->epc = epc;
0634         list = &epf->list;
0635     } else {
0636         epf->sec_epc_func_no = func_no;
0637         epf->sec_epc = epc;
0638         list = &epf->sec_epc_list;
0639     }
0640 
0641     list_add_tail(list, &epc->pci_epf);
0642 ret:
0643     mutex_unlock(&epc->lock);
0644 
0645     return ret;
0646 }
0647 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
0648 
0649 /**
0650  * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
0651  * @epc: the EPC device from which the endpoint function should be removed
0652  * @epf: the endpoint function to be removed
0653  * @type: identifies if the EPC is connected to the primary or secondary
0654  *        interface of EPF
0655  *
0656  * Invoke to remove PCI endpoint function from the endpoint controller.
0657  */
0658 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
0659             enum pci_epc_interface_type type)
0660 {
0661     struct list_head *list;
0662     u32 func_no = 0;
0663 
0664     if (!epc || IS_ERR(epc) || !epf)
0665         return;
0666 
0667     if (type == PRIMARY_INTERFACE) {
0668         func_no = epf->func_no;
0669         list = &epf->list;
0670     } else {
0671         func_no = epf->sec_epc_func_no;
0672         list = &epf->sec_epc_list;
0673     }
0674 
0675     mutex_lock(&epc->lock);
0676     clear_bit(func_no, &epc->function_num_map);
0677     list_del(list);
0678     epf->epc = NULL;
0679     mutex_unlock(&epc->lock);
0680 }
0681 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
0682 
0683 /**
0684  * pci_epc_linkup() - Notify the EPF device that EPC device has established a
0685  *            connection with the Root Complex.
0686  * @epc: the EPC device which has established link with the host
0687  *
0688  * Invoke to Notify the EPF device that the EPC device has established a
0689  * connection with the Root Complex.
0690  */
0691 void pci_epc_linkup(struct pci_epc *epc)
0692 {
0693     if (!epc || IS_ERR(epc))
0694         return;
0695 
0696     atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
0697 }
0698 EXPORT_SYMBOL_GPL(pci_epc_linkup);
0699 
0700 /**
0701  * pci_epc_init_notify() - Notify the EPF device that EPC device's core
0702  *             initialization is completed.
0703  * @epc: the EPC device whose core initialization is completed
0704  *
0705  * Invoke to Notify the EPF device that the EPC device's initialization
0706  * is completed.
0707  */
0708 void pci_epc_init_notify(struct pci_epc *epc)
0709 {
0710     if (!epc || IS_ERR(epc))
0711         return;
0712 
0713     atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
0714 }
0715 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
0716 
0717 /**
0718  * pci_epc_destroy() - destroy the EPC device
0719  * @epc: the EPC device that has to be destroyed
0720  *
0721  * Invoke to destroy the PCI EPC device
0722  */
0723 void pci_epc_destroy(struct pci_epc *epc)
0724 {
0725     pci_ep_cfs_remove_epc_group(epc->group);
0726     device_unregister(&epc->dev);
0727     kfree(epc);
0728 }
0729 EXPORT_SYMBOL_GPL(pci_epc_destroy);
0730 
0731 /**
0732  * devm_pci_epc_destroy() - destroy the EPC device
0733  * @dev: device that wants to destroy the EPC
0734  * @epc: the EPC device that has to be destroyed
0735  *
0736  * Invoke to destroy the devres associated with this
0737  * pci_epc and destroy the EPC device.
0738  */
0739 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
0740 {
0741     int r;
0742 
0743     r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
0744                epc);
0745     dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
0746 }
0747 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
0748 
0749 /**
0750  * __pci_epc_create() - create a new endpoint controller (EPC) device
0751  * @dev: device that is creating the new EPC
0752  * @ops: function pointers for performing EPC operations
0753  * @owner: the owner of the module that creates the EPC device
0754  *
0755  * Invoke to create a new EPC device and add it to pci_epc class.
0756  */
0757 struct pci_epc *
0758 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
0759          struct module *owner)
0760 {
0761     int ret;
0762     struct pci_epc *epc;
0763 
0764     if (WARN_ON(!dev)) {
0765         ret = -EINVAL;
0766         goto err_ret;
0767     }
0768 
0769     epc = kzalloc(sizeof(*epc), GFP_KERNEL);
0770     if (!epc) {
0771         ret = -ENOMEM;
0772         goto err_ret;
0773     }
0774 
0775     mutex_init(&epc->lock);
0776     INIT_LIST_HEAD(&epc->pci_epf);
0777     ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
0778 
0779     device_initialize(&epc->dev);
0780     epc->dev.class = pci_epc_class;
0781     epc->dev.parent = dev;
0782     epc->ops = ops;
0783 
0784     ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
0785     if (ret)
0786         goto put_dev;
0787 
0788     ret = device_add(&epc->dev);
0789     if (ret)
0790         goto put_dev;
0791 
0792     epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
0793 
0794     return epc;
0795 
0796 put_dev:
0797     put_device(&epc->dev);
0798     kfree(epc);
0799 
0800 err_ret:
0801     return ERR_PTR(ret);
0802 }
0803 EXPORT_SYMBOL_GPL(__pci_epc_create);
0804 
0805 /**
0806  * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
0807  * @dev: device that is creating the new EPC
0808  * @ops: function pointers for performing EPC operations
0809  * @owner: the owner of the module that creates the EPC device
0810  *
0811  * Invoke to create a new EPC device and add it to pci_epc class.
0812  * While at that, it also associates the device with the pci_epc using devres.
0813  * On driver detach, release function is invoked on the devres data,
0814  * then, devres data is freed.
0815  */
0816 struct pci_epc *
0817 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
0818               struct module *owner)
0819 {
0820     struct pci_epc **ptr, *epc;
0821 
0822     ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
0823     if (!ptr)
0824         return ERR_PTR(-ENOMEM);
0825 
0826     epc = __pci_epc_create(dev, ops, owner);
0827     if (!IS_ERR(epc)) {
0828         *ptr = epc;
0829         devres_add(dev, ptr);
0830     } else {
0831         devres_free(ptr);
0832     }
0833 
0834     return epc;
0835 }
0836 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
0837 
0838 static int __init pci_epc_init(void)
0839 {
0840     pci_epc_class = class_create(THIS_MODULE, "pci_epc");
0841     if (IS_ERR(pci_epc_class)) {
0842         pr_err("failed to create pci epc class --> %ld\n",
0843                PTR_ERR(pci_epc_class));
0844         return PTR_ERR(pci_epc_class);
0845     }
0846 
0847     return 0;
0848 }
0849 module_init(pci_epc_init);
0850 
0851 static void __exit pci_epc_exit(void)
0852 {
0853     class_destroy(pci_epc_class);
0854 }
0855 module_exit(pci_epc_exit);
0856 
0857 MODULE_DESCRIPTION("PCI EPC Library");
0858 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
0859 MODULE_LICENSE("GPL v2");