linux/drivers/pci/endpoint/pci-epc-core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * PCI Endpoint *Controller* (EPC) library
   4 *
   5 * Copyright (C) 2017 Texas Instruments
   6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
   7 */
   8
   9#include <linux/device.h>
  10#include <linux/slab.h>
  11#include <linux/module.h>
  12#include <linux/of_device.h>
  13
  14#include <linux/pci-epc.h>
  15#include <linux/pci-epf.h>
  16#include <linux/pci-ep-cfs.h>
  17
  18static struct class *pci_epc_class;
  19
  20static void devm_pci_epc_release(struct device *dev, void *res)
  21{
  22        struct pci_epc *epc = *(struct pci_epc **)res;
  23
  24        pci_epc_destroy(epc);
  25}
  26
  27static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
  28{
  29        struct pci_epc **epc = res;
  30
  31        return *epc == match_data;
  32}
  33
  34/**
  35 * pci_epc_put() - release the PCI endpoint controller
  36 * @epc: epc returned by pci_epc_get()
  37 *
  38 * release the refcount the caller obtained by invoking pci_epc_get()
  39 */
  40void pci_epc_put(struct pci_epc *epc)
  41{
  42        if (!epc || IS_ERR(epc))
  43                return;
  44
  45        module_put(epc->ops->owner);
  46        put_device(&epc->dev);
  47}
  48EXPORT_SYMBOL_GPL(pci_epc_put);
  49
  50/**
  51 * pci_epc_get() - get the PCI endpoint controller
  52 * @epc_name: device name of the endpoint controller
  53 *
  54 * Invoke to get struct pci_epc * corresponding to the device name of the
  55 * endpoint controller
  56 */
  57struct pci_epc *pci_epc_get(const char *epc_name)
  58{
  59        int ret = -EINVAL;
  60        struct pci_epc *epc;
  61        struct device *dev;
  62        struct class_dev_iter iter;
  63
  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)))
  67                        continue;
  68
  69                epc = to_pci_epc(dev);
  70                if (!try_module_get(epc->ops->owner)) {
  71                        ret = -EINVAL;
  72                        goto err;
  73                }
  74
  75                class_dev_iter_exit(&iter);
  76                get_device(&epc->dev);
  77                return epc;
  78        }
  79
  80err:
  81        class_dev_iter_exit(&iter);
  82        return ERR_PTR(ret);
  83}
  84EXPORT_SYMBOL_GPL(pci_epc_get);
  85
  86/**
  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
  89 *
  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'.
  92 */
  93enum pci_barno
  94pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
  95{
  96        return pci_epc_get_next_free_bar(epc_features, BAR_0);
  97}
  98EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
  99
 100/**
 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
 104 *
 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'.
 107 */
 108enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
 109                                         *epc_features, enum pci_barno bar)
 110{
 111        unsigned long free_bar;
 112
 113        if (!epc_features)
 114                return BAR_0;
 115
 116        /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
 117        if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
 118                bar++;
 119
 120        /* Find if the reserved BAR is also a 64-bit BAR */
 121        free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
 122
 123        /* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */
 124        free_bar <<= 1;
 125        free_bar |= epc_features->reserved_bar;
 126
 127        free_bar = find_next_zero_bit(&free_bar, 6, bar);
 128        if (free_bar > 5)
 129                return NO_BAR;
 130
 131        return free_bar;
 132}
 133EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
 134
 135/**
 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 *
 141 * Invoke to get the features provided by the EPC which may be
 142 * specific to an endpoint function. Returns pci_epc_features on success
 143 * and NULL for any failures.
 144 */
 145const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
 146                                                    u8 func_no)
 147{
 148        const struct pci_epc_features *epc_features;
 149
 150        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 151                return NULL;
 152
 153        if (!epc->ops->get_features)
 154                return NULL;
 155
 156        mutex_lock(&epc->lock);
 157        epc_features = epc->ops->get_features(epc, func_no);
 158        mutex_unlock(&epc->lock);
 159
 160        return epc_features;
 161}
 162EXPORT_SYMBOL_GPL(pci_epc_get_features);
 163
 164/**
 165 * pci_epc_stop() - stop the PCI link
 166 * @epc: the link of the EPC device that has to be stopped
 167 *
 168 * Invoke to stop the PCI link
 169 */
 170void pci_epc_stop(struct pci_epc *epc)
 171{
 172        if (IS_ERR(epc) || !epc->ops->stop)
 173                return;
 174
 175        mutex_lock(&epc->lock);
 176        epc->ops->stop(epc);
 177        mutex_unlock(&epc->lock);
 178}
 179EXPORT_SYMBOL_GPL(pci_epc_stop);
 180
 181/**
 182 * pci_epc_start() - start the PCI link
 183 * @epc: the link of *this* EPC device has to be started
 184 *
 185 * Invoke to start the PCI link
 186 */
 187int pci_epc_start(struct pci_epc *epc)
 188{
 189        int ret;
 190
 191        if (IS_ERR(epc))
 192                return -EINVAL;
 193
 194        if (!epc->ops->start)
 195                return 0;
 196
 197        mutex_lock(&epc->lock);
 198        ret = epc->ops->start(epc);
 199        mutex_unlock(&epc->lock);
 200
 201        return ret;
 202}
 203EXPORT_SYMBOL_GPL(pci_epc_start);
 204
 205/**
 206 * pci_epc_raise_irq() - interrupt the host system
 207 * @epc: the EPC device which has to interrupt the host
 208 * @func_no: the endpoint function number in the EPC device
 209 * @type: specify the type of interrupt; legacy, MSI or MSI-X
 210 * @interrupt_num: the MSI or MSI-X interrupt number
 211 *
 212 * Invoke to raise an legacy, MSI or MSI-X interrupt
 213 */
 214int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,
 215                      enum pci_epc_irq_type type, u16 interrupt_num)
 216{
 217        int ret;
 218
 219        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 220                return -EINVAL;
 221
 222        if (!epc->ops->raise_irq)
 223                return 0;
 224
 225        mutex_lock(&epc->lock);
 226        ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num);
 227        mutex_unlock(&epc->lock);
 228
 229        return ret;
 230}
 231EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
 232
 233/**
 234 * pci_epc_map_msi_irq() - Map physical address to MSI address and return
 235 *                         MSI data
 236 * @epc: the EPC device which has the MSI capability
 237 * @func_no: the physical endpoint function number in the EPC device
 238 * @phys_addr: the physical address of the outbound region
 239 * @interrupt_num: the MSI interrupt number
 240 * @entry_size: Size of Outbound address region for each interrupt
 241 * @msi_data: the data that should be written in order to raise MSI interrupt
 242 *            with interrupt number as 'interrupt num'
 243 * @msi_addr_offset: Offset of MSI address from the aligned outbound address
 244 *                   to which the MSI address is mapped
 245 *
 246 * Invoke to map physical address to MSI address and return MSI data. The
 247 * physical address should be an address in the outbound region. This is
 248 * required to implement doorbell functionality of NTB wherein EPC on either
 249 * side of the interface (primary and secondary) can directly write to the
 250 * physical address (in outbound region) of the other interface to ring
 251 * doorbell.
 252 */
 253int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, phys_addr_t phys_addr,
 254                        u8 interrupt_num, u32 entry_size, u32 *msi_data,
 255                        u32 *msi_addr_offset)
 256{
 257        int ret;
 258
 259        if (IS_ERR_OR_NULL(epc))
 260                return -EINVAL;
 261
 262        if (!epc->ops->map_msi_irq)
 263                return -EINVAL;
 264
 265        mutex_lock(&epc->lock);
 266        ret = epc->ops->map_msi_irq(epc, func_no, phys_addr, interrupt_num,
 267                                    entry_size, msi_data, msi_addr_offset);
 268        mutex_unlock(&epc->lock);
 269
 270        return ret;
 271}
 272EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
 273
 274/**
 275 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
 276 * @epc: the EPC device to which MSI interrupts was requested
 277 * @func_no: the endpoint function number in the EPC device
 278 *
 279 * Invoke to get the number of MSI interrupts allocated by the RC
 280 */
 281int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)
 282{
 283        int interrupt;
 284
 285        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 286                return 0;
 287
 288        if (!epc->ops->get_msi)
 289                return 0;
 290
 291        mutex_lock(&epc->lock);
 292        interrupt = epc->ops->get_msi(epc, func_no);
 293        mutex_unlock(&epc->lock);
 294
 295        if (interrupt < 0)
 296                return 0;
 297
 298        interrupt = 1 << interrupt;
 299
 300        return interrupt;
 301}
 302EXPORT_SYMBOL_GPL(pci_epc_get_msi);
 303
 304/**
 305 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
 306 * @epc: the EPC device on which MSI has to be configured
 307 * @func_no: the endpoint function number in the EPC device
 308 * @interrupts: number of MSI interrupts required by the EPF
 309 *
 310 * Invoke to set the required number of MSI interrupts.
 311 */
 312int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
 313{
 314        int ret;
 315        u8 encode_int;
 316
 317        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
 318            interrupts > 32)
 319                return -EINVAL;
 320
 321        if (!epc->ops->set_msi)
 322                return 0;
 323
 324        encode_int = order_base_2(interrupts);
 325
 326        mutex_lock(&epc->lock);
 327        ret = epc->ops->set_msi(epc, func_no, encode_int);
 328        mutex_unlock(&epc->lock);
 329
 330        return ret;
 331}
 332EXPORT_SYMBOL_GPL(pci_epc_set_msi);
 333
 334/**
 335 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
 336 * @epc: the EPC device to which MSI-X interrupts was requested
 337 * @func_no: the endpoint function number in the EPC device
 338 *
 339 * Invoke to get the number of MSI-X interrupts allocated by the RC
 340 */
 341int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)
 342{
 343        int interrupt;
 344
 345        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 346                return 0;
 347
 348        if (!epc->ops->get_msix)
 349                return 0;
 350
 351        mutex_lock(&epc->lock);
 352        interrupt = epc->ops->get_msix(epc, func_no);
 353        mutex_unlock(&epc->lock);
 354
 355        if (interrupt < 0)
 356                return 0;
 357
 358        return interrupt + 1;
 359}
 360EXPORT_SYMBOL_GPL(pci_epc_get_msix);
 361
 362/**
 363 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
 364 * @epc: the EPC device on which MSI-X has to be configured
 365 * @func_no: the endpoint function number in the EPC device
 366 * @interrupts: number of MSI-X interrupts required by the EPF
 367 * @bir: BAR where the MSI-X table resides
 368 * @offset: Offset pointing to the start of MSI-X table
 369 *
 370 * Invoke to set the required number of MSI-X interrupts.
 371 */
 372int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
 373                     enum pci_barno bir, u32 offset)
 374{
 375        int ret;
 376
 377        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
 378            interrupts < 1 || interrupts > 2048)
 379                return -EINVAL;
 380
 381        if (!epc->ops->set_msix)
 382                return 0;
 383
 384        mutex_lock(&epc->lock);
 385        ret = epc->ops->set_msix(epc, func_no, interrupts - 1, bir, offset);
 386        mutex_unlock(&epc->lock);
 387
 388        return ret;
 389}
 390EXPORT_SYMBOL_GPL(pci_epc_set_msix);
 391
 392/**
 393 * pci_epc_unmap_addr() - unmap CPU address from PCI address
 394 * @epc: the EPC device on which address is allocated
 395 * @func_no: the endpoint function number in the EPC device
 396 * @phys_addr: physical address of the local system
 397 *
 398 * Invoke to unmap the CPU address from PCI address.
 399 */
 400void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
 401                        phys_addr_t phys_addr)
 402{
 403        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 404                return;
 405
 406        if (!epc->ops->unmap_addr)
 407                return;
 408
 409        mutex_lock(&epc->lock);
 410        epc->ops->unmap_addr(epc, func_no, phys_addr);
 411        mutex_unlock(&epc->lock);
 412}
 413EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
 414
 415/**
 416 * pci_epc_map_addr() - map CPU address to PCI address
 417 * @epc: the EPC device on which address is allocated
 418 * @func_no: the endpoint function number in the EPC device
 419 * @phys_addr: physical address of the local system
 420 * @pci_addr: PCI address to which the physical address should be mapped
 421 * @size: the size of the allocation
 422 *
 423 * Invoke to map CPU address with PCI address.
 424 */
 425int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
 426                     phys_addr_t phys_addr, u64 pci_addr, size_t size)
 427{
 428        int ret;
 429
 430        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 431                return -EINVAL;
 432
 433        if (!epc->ops->map_addr)
 434                return 0;
 435
 436        mutex_lock(&epc->lock);
 437        ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
 438        mutex_unlock(&epc->lock);
 439
 440        return ret;
 441}
 442EXPORT_SYMBOL_GPL(pci_epc_map_addr);
 443
 444/**
 445 * pci_epc_clear_bar() - reset the BAR
 446 * @epc: the EPC device for which the BAR has to be cleared
 447 * @func_no: the endpoint function number in the EPC device
 448 * @epf_bar: the struct epf_bar that contains the BAR information
 449 *
 450 * Invoke to reset the BAR of the endpoint device.
 451 */
 452void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
 453                       struct pci_epf_bar *epf_bar)
 454{
 455        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
 456            (epf_bar->barno == BAR_5 &&
 457             epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
 458                return;
 459
 460        if (!epc->ops->clear_bar)
 461                return;
 462
 463        mutex_lock(&epc->lock);
 464        epc->ops->clear_bar(epc, func_no, epf_bar);
 465        mutex_unlock(&epc->lock);
 466}
 467EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
 468
 469/**
 470 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
 471 * @epc: the EPC device on which BAR has to be configured
 472 * @func_no: the endpoint function number in the EPC device
 473 * @epf_bar: the struct epf_bar that contains the BAR information
 474 *
 475 * Invoke to configure the BAR of the endpoint device.
 476 */
 477int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
 478                    struct pci_epf_bar *epf_bar)
 479{
 480        int ret;
 481        int flags = epf_bar->flags;
 482
 483        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
 484            (epf_bar->barno == BAR_5 &&
 485             flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
 486            (flags & PCI_BASE_ADDRESS_SPACE_IO &&
 487             flags & PCI_BASE_ADDRESS_IO_MASK) ||
 488            (upper_32_bits(epf_bar->size) &&
 489             !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
 490                return -EINVAL;
 491
 492        if (!epc->ops->set_bar)
 493                return 0;
 494
 495        mutex_lock(&epc->lock);
 496        ret = epc->ops->set_bar(epc, func_no, epf_bar);
 497        mutex_unlock(&epc->lock);
 498
 499        return ret;
 500}
 501EXPORT_SYMBOL_GPL(pci_epc_set_bar);
 502
 503/**
 504 * pci_epc_write_header() - write standard configuration header
 505 * @epc: the EPC device to which the configuration header should be written
 506 * @func_no: the endpoint function number in the EPC device
 507 * @header: standard configuration header fields
 508 *
 509 * Invoke to write the configuration header to the endpoint controller. Every
 510 * endpoint controller will have a dedicated location to which the standard
 511 * configuration header would be written. The callback function should write
 512 * the header fields to this dedicated location.
 513 */
 514int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
 515                         struct pci_epf_header *header)
 516{
 517        int ret;
 518
 519        if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
 520                return -EINVAL;
 521
 522        if (!epc->ops->write_header)
 523                return 0;
 524
 525        mutex_lock(&epc->lock);
 526        ret = epc->ops->write_header(epc, func_no, header);
 527        mutex_unlock(&epc->lock);
 528
 529        return ret;
 530}
 531EXPORT_SYMBOL_GPL(pci_epc_write_header);
 532
 533/**
 534 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
 535 * @epc: the EPC device to which the endpoint function should be added
 536 * @epf: the endpoint function to be added
 537 * @type: Identifies if the EPC is connected to the primary or secondary
 538 *        interface of EPF
 539 *
 540 * A PCI endpoint device can have one or more functions. In the case of PCIe,
 541 * the specification allows up to 8 PCIe endpoint functions. Invoke
 542 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
 543 */
 544int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
 545                    enum pci_epc_interface_type type)
 546{
 547        struct list_head *list;
 548        u32 func_no;
 549        int ret = 0;
 550
 551        if (IS_ERR_OR_NULL(epc))
 552                return -EINVAL;
 553
 554        if (type == PRIMARY_INTERFACE && epf->epc)
 555                return -EBUSY;
 556
 557        if (type == SECONDARY_INTERFACE && epf->sec_epc)
 558                return -EBUSY;
 559
 560        mutex_lock(&epc->lock);
 561        func_no = find_first_zero_bit(&epc->function_num_map,
 562                                      BITS_PER_LONG);
 563        if (func_no >= BITS_PER_LONG) {
 564                ret = -EINVAL;
 565                goto ret;
 566        }
 567
 568        if (func_no > epc->max_functions - 1) {
 569                dev_err(&epc->dev, "Exceeding max supported Function Number\n");
 570                ret = -EINVAL;
 571                goto ret;
 572        }
 573
 574        set_bit(func_no, &epc->function_num_map);
 575        if (type == PRIMARY_INTERFACE) {
 576                epf->func_no = func_no;
 577                epf->epc = epc;
 578                list = &epf->list;
 579        } else {
 580                epf->sec_epc_func_no = func_no;
 581                epf->sec_epc = epc;
 582                list = &epf->sec_epc_list;
 583        }
 584
 585        list_add_tail(list, &epc->pci_epf);
 586ret:
 587        mutex_unlock(&epc->lock);
 588
 589        return ret;
 590}
 591EXPORT_SYMBOL_GPL(pci_epc_add_epf);
 592
 593/**
 594 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
 595 * @epc: the EPC device from which the endpoint function should be removed
 596 * @epf: the endpoint function to be removed
 597 * @type: identifies if the EPC is connected to the primary or secondary
 598 *        interface of EPF
 599 *
 600 * Invoke to remove PCI endpoint function from the endpoint controller.
 601 */
 602void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
 603                        enum pci_epc_interface_type type)
 604{
 605        struct list_head *list;
 606        u32 func_no = 0;
 607
 608        if (!epc || IS_ERR(epc) || !epf)
 609                return;
 610
 611        if (type == PRIMARY_INTERFACE) {
 612                func_no = epf->func_no;
 613                list = &epf->list;
 614        } else {
 615                func_no = epf->sec_epc_func_no;
 616                list = &epf->sec_epc_list;
 617        }
 618
 619        mutex_lock(&epc->lock);
 620        clear_bit(func_no, &epc->function_num_map);
 621        list_del(list);
 622        epf->epc = NULL;
 623        mutex_unlock(&epc->lock);
 624}
 625EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
 626
 627/**
 628 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
 629 *                    connection with the Root Complex.
 630 * @epc: the EPC device which has established link with the host
 631 *
 632 * Invoke to Notify the EPF device that the EPC device has established a
 633 * connection with the Root Complex.
 634 */
 635void pci_epc_linkup(struct pci_epc *epc)
 636{
 637        if (!epc || IS_ERR(epc))
 638                return;
 639
 640        atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
 641}
 642EXPORT_SYMBOL_GPL(pci_epc_linkup);
 643
 644/**
 645 * pci_epc_init_notify() - Notify the EPF device that EPC device's core
 646 *                         initialization is completed.
 647 * @epc: the EPC device whose core initialization is completeds
 648 *
 649 * Invoke to Notify the EPF device that the EPC device's initialization
 650 * is completed.
 651 */
 652void pci_epc_init_notify(struct pci_epc *epc)
 653{
 654        if (!epc || IS_ERR(epc))
 655                return;
 656
 657        atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
 658}
 659EXPORT_SYMBOL_GPL(pci_epc_init_notify);
 660
 661/**
 662 * pci_epc_destroy() - destroy the EPC device
 663 * @epc: the EPC device that has to be destroyed
 664 *
 665 * Invoke to destroy the PCI EPC device
 666 */
 667void pci_epc_destroy(struct pci_epc *epc)
 668{
 669        pci_ep_cfs_remove_epc_group(epc->group);
 670        device_unregister(&epc->dev);
 671        kfree(epc);
 672}
 673EXPORT_SYMBOL_GPL(pci_epc_destroy);
 674
 675/**
 676 * devm_pci_epc_destroy() - destroy the EPC device
 677 * @dev: device that wants to destroy the EPC
 678 * @epc: the EPC device that has to be destroyed
 679 *
 680 * Invoke to destroy the devres associated with this
 681 * pci_epc and destroy the EPC device.
 682 */
 683void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
 684{
 685        int r;
 686
 687        r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
 688                           epc);
 689        dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
 690}
 691EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
 692
 693/**
 694 * __pci_epc_create() - create a new endpoint controller (EPC) device
 695 * @dev: device that is creating the new EPC
 696 * @ops: function pointers for performing EPC operations
 697 * @owner: the owner of the module that creates the EPC device
 698 *
 699 * Invoke to create a new EPC device and add it to pci_epc class.
 700 */
 701struct pci_epc *
 702__pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
 703                 struct module *owner)
 704{
 705        int ret;
 706        struct pci_epc *epc;
 707
 708        if (WARN_ON(!dev)) {
 709                ret = -EINVAL;
 710                goto err_ret;
 711        }
 712
 713        epc = kzalloc(sizeof(*epc), GFP_KERNEL);
 714        if (!epc) {
 715                ret = -ENOMEM;
 716                goto err_ret;
 717        }
 718
 719        mutex_init(&epc->lock);
 720        INIT_LIST_HEAD(&epc->pci_epf);
 721        ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
 722
 723        device_initialize(&epc->dev);
 724        epc->dev.class = pci_epc_class;
 725        epc->dev.parent = dev;
 726        epc->ops = ops;
 727
 728        ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
 729        if (ret)
 730                goto put_dev;
 731
 732        ret = device_add(&epc->dev);
 733        if (ret)
 734                goto put_dev;
 735
 736        epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
 737
 738        return epc;
 739
 740put_dev:
 741        put_device(&epc->dev);
 742        kfree(epc);
 743
 744err_ret:
 745        return ERR_PTR(ret);
 746}
 747EXPORT_SYMBOL_GPL(__pci_epc_create);
 748
 749/**
 750 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
 751 * @dev: device that is creating the new EPC
 752 * @ops: function pointers for performing EPC operations
 753 * @owner: the owner of the module that creates the EPC device
 754 *
 755 * Invoke to create a new EPC device and add it to pci_epc class.
 756 * While at that, it also associates the device with the pci_epc using devres.
 757 * On driver detach, release function is invoked on the devres data,
 758 * then, devres data is freed.
 759 */
 760struct pci_epc *
 761__devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
 762                      struct module *owner)
 763{
 764        struct pci_epc **ptr, *epc;
 765
 766        ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
 767        if (!ptr)
 768                return ERR_PTR(-ENOMEM);
 769
 770        epc = __pci_epc_create(dev, ops, owner);
 771        if (!IS_ERR(epc)) {
 772                *ptr = epc;
 773                devres_add(dev, ptr);
 774        } else {
 775                devres_free(ptr);
 776        }
 777
 778        return epc;
 779}
 780EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
 781
 782static int __init pci_epc_init(void)
 783{
 784        pci_epc_class = class_create(THIS_MODULE, "pci_epc");
 785        if (IS_ERR(pci_epc_class)) {
 786                pr_err("failed to create pci epc class --> %ld\n",
 787                       PTR_ERR(pci_epc_class));
 788                return PTR_ERR(pci_epc_class);
 789        }
 790
 791        return 0;
 792}
 793module_init(pci_epc_init);
 794
 795static void __exit pci_epc_exit(void)
 796{
 797        class_destroy(pci_epc_class);
 798}
 799module_exit(pci_epc_exit);
 800
 801MODULE_DESCRIPTION("PCI EPC Library");
 802MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
 803MODULE_LICENSE("GPL v2");
 804