treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pci / endpoint / pci-epc-core.c
blob2091508c16204d43e42fa879f700ccea1538e3e2
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * PCI Endpoint *Controller* (EPC) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
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;
24 pci_epc_destroy(epc);
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;
34 /**
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))
43 return;
45 module_put(epc->ops->owner);
46 put_device(&epc->dev);
48 EXPORT_SYMBOL_GPL(pci_epc_put);
50 /**
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
55 * endpoint controller
57 struct pci_epc *pci_epc_get(const char *epc_name)
59 int ret = -EINVAL;
60 struct pci_epc *epc;
61 struct device *dev;
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)))
67 continue;
69 epc = to_pci_epc(dev);
70 if (!try_module_get(epc->ops->owner)) {
71 ret = -EINVAL;
72 goto err;
75 class_dev_iter_exit(&iter);
76 get_device(&epc->dev);
77 return epc;
80 err:
81 class_dev_iter_exit(&iter);
82 return ERR_PTR(ret);
84 EXPORT_SYMBOL_GPL(pci_epc_get);
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
90 * Invoke to get the first unreserved BAR that can be used for endpoint
91 * function. For any incorrect value in reserved_bar return '0'.
93 unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features
94 *epc_features)
96 int free_bar;
98 if (!epc_features)
99 return 0;
101 free_bar = ffz(epc_features->reserved_bar);
102 if (free_bar > 5)
103 return 0;
105 return free_bar;
107 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
110 * pci_epc_get_features() - get the features supported by EPC
111 * @epc: the features supported by *this* EPC device will be returned
112 * @func_no: the features supported by the EPC device specific to the
113 * endpoint function with func_no will be returned
115 * Invoke to get the features provided by the EPC which may be
116 * specific to an endpoint function. Returns pci_epc_features on success
117 * and NULL for any failures.
119 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
120 u8 func_no)
122 const struct pci_epc_features *epc_features;
123 unsigned long flags;
125 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
126 return NULL;
128 if (!epc->ops->get_features)
129 return NULL;
131 spin_lock_irqsave(&epc->lock, flags);
132 epc_features = epc->ops->get_features(epc, func_no);
133 spin_unlock_irqrestore(&epc->lock, flags);
135 return epc_features;
137 EXPORT_SYMBOL_GPL(pci_epc_get_features);
140 * pci_epc_stop() - stop the PCI link
141 * @epc: the link of the EPC device that has to be stopped
143 * Invoke to stop the PCI link
145 void pci_epc_stop(struct pci_epc *epc)
147 unsigned long flags;
149 if (IS_ERR(epc) || !epc->ops->stop)
150 return;
152 spin_lock_irqsave(&epc->lock, flags);
153 epc->ops->stop(epc);
154 spin_unlock_irqrestore(&epc->lock, flags);
156 EXPORT_SYMBOL_GPL(pci_epc_stop);
159 * pci_epc_start() - start the PCI link
160 * @epc: the link of *this* EPC device has to be started
162 * Invoke to start the PCI link
164 int pci_epc_start(struct pci_epc *epc)
166 int ret;
167 unsigned long flags;
169 if (IS_ERR(epc))
170 return -EINVAL;
172 if (!epc->ops->start)
173 return 0;
175 spin_lock_irqsave(&epc->lock, flags);
176 ret = epc->ops->start(epc);
177 spin_unlock_irqrestore(&epc->lock, flags);
179 return ret;
181 EXPORT_SYMBOL_GPL(pci_epc_start);
184 * pci_epc_raise_irq() - interrupt the host system
185 * @epc: the EPC device which has to interrupt the host
186 * @func_no: the endpoint function number in the EPC device
187 * @type: specify the type of interrupt; legacy, MSI or MSI-X
188 * @interrupt_num: the MSI or MSI-X interrupt number
190 * Invoke to raise an legacy, MSI or MSI-X interrupt
192 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,
193 enum pci_epc_irq_type type, u16 interrupt_num)
195 int ret;
196 unsigned long flags;
198 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
199 return -EINVAL;
201 if (!epc->ops->raise_irq)
202 return 0;
204 spin_lock_irqsave(&epc->lock, flags);
205 ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num);
206 spin_unlock_irqrestore(&epc->lock, flags);
208 return ret;
210 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
213 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
214 * @epc: the EPC device to which MSI interrupts was requested
215 * @func_no: the endpoint function number in the EPC device
217 * Invoke to get the number of MSI interrupts allocated by the RC
219 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)
221 int interrupt;
222 unsigned long flags;
224 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
225 return 0;
227 if (!epc->ops->get_msi)
228 return 0;
230 spin_lock_irqsave(&epc->lock, flags);
231 interrupt = epc->ops->get_msi(epc, func_no);
232 spin_unlock_irqrestore(&epc->lock, flags);
234 if (interrupt < 0)
235 return 0;
237 interrupt = 1 << interrupt;
239 return interrupt;
241 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
244 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
245 * @epc: the EPC device on which MSI has to be configured
246 * @func_no: the endpoint function number in the EPC device
247 * @interrupts: number of MSI interrupts required by the EPF
249 * Invoke to set the required number of MSI interrupts.
251 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
253 int ret;
254 u8 encode_int;
255 unsigned long flags;
257 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
258 interrupts > 32)
259 return -EINVAL;
261 if (!epc->ops->set_msi)
262 return 0;
264 encode_int = order_base_2(interrupts);
266 spin_lock_irqsave(&epc->lock, flags);
267 ret = epc->ops->set_msi(epc, func_no, encode_int);
268 spin_unlock_irqrestore(&epc->lock, flags);
270 return ret;
272 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
275 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
276 * @epc: the EPC device to which MSI-X interrupts was requested
277 * @func_no: the endpoint function number in the EPC device
279 * Invoke to get the number of MSI-X interrupts allocated by the RC
281 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)
283 int interrupt;
284 unsigned long flags;
286 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
287 return 0;
289 if (!epc->ops->get_msix)
290 return 0;
292 spin_lock_irqsave(&epc->lock, flags);
293 interrupt = epc->ops->get_msix(epc, func_no);
294 spin_unlock_irqrestore(&epc->lock, flags);
296 if (interrupt < 0)
297 return 0;
299 return interrupt + 1;
301 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
304 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
305 * @epc: the EPC device on which MSI-X has to be configured
306 * @func_no: the endpoint function number in the EPC device
307 * @interrupts: number of MSI-X interrupts required by the EPF
309 * Invoke to set the required number of MSI-X interrupts.
311 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts)
313 int ret;
314 unsigned long flags;
316 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
317 interrupts < 1 || interrupts > 2048)
318 return -EINVAL;
320 if (!epc->ops->set_msix)
321 return 0;
323 spin_lock_irqsave(&epc->lock, flags);
324 ret = epc->ops->set_msix(epc, func_no, interrupts - 1);
325 spin_unlock_irqrestore(&epc->lock, flags);
327 return ret;
329 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
332 * pci_epc_unmap_addr() - unmap CPU address from PCI address
333 * @epc: the EPC device on which address is allocated
334 * @func_no: the endpoint function number in the EPC device
335 * @phys_addr: physical address of the local system
337 * Invoke to unmap the CPU address from PCI address.
339 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
340 phys_addr_t phys_addr)
342 unsigned long flags;
344 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
345 return;
347 if (!epc->ops->unmap_addr)
348 return;
350 spin_lock_irqsave(&epc->lock, flags);
351 epc->ops->unmap_addr(epc, func_no, phys_addr);
352 spin_unlock_irqrestore(&epc->lock, flags);
354 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
357 * pci_epc_map_addr() - map CPU address to PCI address
358 * @epc: the EPC device on which address is allocated
359 * @func_no: the endpoint function number in the EPC device
360 * @phys_addr: physical address of the local system
361 * @pci_addr: PCI address to which the physical address should be mapped
362 * @size: the size of the allocation
364 * Invoke to map CPU address with PCI address.
366 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
367 phys_addr_t phys_addr, u64 pci_addr, size_t size)
369 int ret;
370 unsigned long flags;
372 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
373 return -EINVAL;
375 if (!epc->ops->map_addr)
376 return 0;
378 spin_lock_irqsave(&epc->lock, flags);
379 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
380 spin_unlock_irqrestore(&epc->lock, flags);
382 return ret;
384 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
387 * pci_epc_clear_bar() - reset the BAR
388 * @epc: the EPC device for which the BAR has to be cleared
389 * @func_no: the endpoint function number in the EPC device
390 * @epf_bar: the struct epf_bar that contains the BAR information
392 * Invoke to reset the BAR of the endpoint device.
394 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
395 struct pci_epf_bar *epf_bar)
397 unsigned long flags;
399 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
400 (epf_bar->barno == BAR_5 &&
401 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
402 return;
404 if (!epc->ops->clear_bar)
405 return;
407 spin_lock_irqsave(&epc->lock, flags);
408 epc->ops->clear_bar(epc, func_no, epf_bar);
409 spin_unlock_irqrestore(&epc->lock, flags);
411 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
414 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
415 * @epc: the EPC device on which BAR has to be configured
416 * @func_no: the endpoint function number in the EPC device
417 * @epf_bar: the struct epf_bar that contains the BAR information
419 * Invoke to configure the BAR of the endpoint device.
421 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
422 struct pci_epf_bar *epf_bar)
424 int ret;
425 unsigned long irq_flags;
426 int flags = epf_bar->flags;
428 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
429 (epf_bar->barno == BAR_5 &&
430 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
431 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
432 flags & PCI_BASE_ADDRESS_IO_MASK) ||
433 (upper_32_bits(epf_bar->size) &&
434 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
435 return -EINVAL;
437 if (!epc->ops->set_bar)
438 return 0;
440 spin_lock_irqsave(&epc->lock, irq_flags);
441 ret = epc->ops->set_bar(epc, func_no, epf_bar);
442 spin_unlock_irqrestore(&epc->lock, irq_flags);
444 return ret;
446 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
449 * pci_epc_write_header() - write standard configuration header
450 * @epc: the EPC device to which the configuration header should be written
451 * @func_no: the endpoint function number in the EPC device
452 * @header: standard configuration header fields
454 * Invoke to write the configuration header to the endpoint controller. Every
455 * endpoint controller will have a dedicated location to which the standard
456 * configuration header would be written. The callback function should write
457 * the header fields to this dedicated location.
459 int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
460 struct pci_epf_header *header)
462 int ret;
463 unsigned long flags;
465 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
466 return -EINVAL;
468 if (!epc->ops->write_header)
469 return 0;
471 spin_lock_irqsave(&epc->lock, flags);
472 ret = epc->ops->write_header(epc, func_no, header);
473 spin_unlock_irqrestore(&epc->lock, flags);
475 return ret;
477 EXPORT_SYMBOL_GPL(pci_epc_write_header);
480 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
481 * @epc: the EPC device to which the endpoint function should be added
482 * @epf: the endpoint function to be added
484 * A PCI endpoint device can have one or more functions. In the case of PCIe,
485 * the specification allows up to 8 PCIe endpoint functions. Invoke
486 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
488 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
490 unsigned long flags;
492 if (epf->epc)
493 return -EBUSY;
495 if (IS_ERR(epc))
496 return -EINVAL;
498 if (epf->func_no > epc->max_functions - 1)
499 return -EINVAL;
501 epf->epc = epc;
503 spin_lock_irqsave(&epc->lock, flags);
504 list_add_tail(&epf->list, &epc->pci_epf);
505 spin_unlock_irqrestore(&epc->lock, flags);
507 return 0;
509 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
512 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
513 * @epc: the EPC device from which the endpoint function should be removed
514 * @epf: the endpoint function to be removed
516 * Invoke to remove PCI endpoint function from the endpoint controller.
518 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
520 unsigned long flags;
522 if (!epc || IS_ERR(epc) || !epf)
523 return;
525 spin_lock_irqsave(&epc->lock, flags);
526 list_del(&epf->list);
527 epf->epc = NULL;
528 spin_unlock_irqrestore(&epc->lock, flags);
530 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
533 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
534 * connection with the Root Complex.
535 * @epc: the EPC device which has established link with the host
537 * Invoke to Notify the EPF device that the EPC device has established a
538 * connection with the Root Complex.
540 void pci_epc_linkup(struct pci_epc *epc)
542 unsigned long flags;
543 struct pci_epf *epf;
545 if (!epc || IS_ERR(epc))
546 return;
548 spin_lock_irqsave(&epc->lock, flags);
549 list_for_each_entry(epf, &epc->pci_epf, list)
550 pci_epf_linkup(epf);
551 spin_unlock_irqrestore(&epc->lock, flags);
553 EXPORT_SYMBOL_GPL(pci_epc_linkup);
556 * pci_epc_destroy() - destroy the EPC device
557 * @epc: the EPC device that has to be destroyed
559 * Invoke to destroy the PCI EPC device
561 void pci_epc_destroy(struct pci_epc *epc)
563 pci_ep_cfs_remove_epc_group(epc->group);
564 device_unregister(&epc->dev);
565 kfree(epc);
567 EXPORT_SYMBOL_GPL(pci_epc_destroy);
570 * devm_pci_epc_destroy() - destroy the EPC device
571 * @dev: device that wants to destroy the EPC
572 * @epc: the EPC device that has to be destroyed
574 * Invoke to destroy the devres associated with this
575 * pci_epc and destroy the EPC device.
577 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
579 int r;
581 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
582 epc);
583 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
585 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
588 * __pci_epc_create() - create a new endpoint controller (EPC) device
589 * @dev: device that is creating the new EPC
590 * @ops: function pointers for performing EPC operations
591 * @owner: the owner of the module that creates the EPC device
593 * Invoke to create a new EPC device and add it to pci_epc class.
595 struct pci_epc *
596 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
597 struct module *owner)
599 int ret;
600 struct pci_epc *epc;
602 if (WARN_ON(!dev)) {
603 ret = -EINVAL;
604 goto err_ret;
607 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
608 if (!epc) {
609 ret = -ENOMEM;
610 goto err_ret;
613 spin_lock_init(&epc->lock);
614 INIT_LIST_HEAD(&epc->pci_epf);
616 device_initialize(&epc->dev);
617 epc->dev.class = pci_epc_class;
618 epc->dev.parent = dev;
619 epc->ops = ops;
621 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
622 if (ret)
623 goto put_dev;
625 ret = device_add(&epc->dev);
626 if (ret)
627 goto put_dev;
629 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
631 return epc;
633 put_dev:
634 put_device(&epc->dev);
635 kfree(epc);
637 err_ret:
638 return ERR_PTR(ret);
640 EXPORT_SYMBOL_GPL(__pci_epc_create);
643 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
644 * @dev: device that is creating the new EPC
645 * @ops: function pointers for performing EPC operations
646 * @owner: the owner of the module that creates the EPC device
648 * Invoke to create a new EPC device and add it to pci_epc class.
649 * While at that, it also associates the device with the pci_epc using devres.
650 * On driver detach, release function is invoked on the devres data,
651 * then, devres data is freed.
653 struct pci_epc *
654 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
655 struct module *owner)
657 struct pci_epc **ptr, *epc;
659 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
660 if (!ptr)
661 return ERR_PTR(-ENOMEM);
663 epc = __pci_epc_create(dev, ops, owner);
664 if (!IS_ERR(epc)) {
665 *ptr = epc;
666 devres_add(dev, ptr);
667 } else {
668 devres_free(ptr);
671 return epc;
673 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
675 static int __init pci_epc_init(void)
677 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
678 if (IS_ERR(pci_epc_class)) {
679 pr_err("failed to create pci epc class --> %ld\n",
680 PTR_ERR(pci_epc_class));
681 return PTR_ERR(pci_epc_class);
684 return 0;
686 module_init(pci_epc_init);
688 static void __exit pci_epc_exit(void)
690 class_destroy(pci_epc_class);
692 module_exit(pci_epc_exit);
694 MODULE_DESCRIPTION("PCI EPC Library");
695 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
696 MODULE_LICENSE("GPL v2");