1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * PCI Endpoint *Function* (EPF) header file
4 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 */
8
9#ifndef __LINUX_PCI_EPF_H
10#define __LINUX_PCI_EPF_H
11
12#include <linux/configfs.h>
13#include <linux/device.h>
14#include <linux/mod_devicetable.h>
15#include <linux/pci.h>
16
17struct pci_epf;
18struct pci_epc_features;
19enum pci_epc_interface_type;
20
21enum pci_barno {
22	NO_BAR = -1,
23	BAR_0,
24	BAR_1,
25	BAR_2,
26	BAR_3,
27	BAR_4,
28	BAR_5,
29};
30
31/**
32 * struct pci_epf_header - represents standard configuration header
33 * @vendorid: identifies device manufacturer
34 * @deviceid: identifies a particular device
35 * @revid: specifies a device-specific revision identifier
36 * @progif_code: identifies a specific register-level programming interface
37 * @subclass_code: identifies more specifically the function of the device
38 * @baseclass_code: broadly classifies the type of function the device performs
39 * @cache_line_size: specifies the system cacheline size in units of DWORDs
40 * @subsys_vendor_id: vendor of the add-in card or subsystem
41 * @subsys_id: id specific to vendor
42 * @interrupt_pin: interrupt pin the device (or device function) uses
43 */
44struct pci_epf_header {
45	u16	vendorid;
46	u16	deviceid;
47	u8	revid;
48	u8	progif_code;
49	u8	subclass_code;
50	u8	baseclass_code;
51	u8	cache_line_size;
52	u16	subsys_vendor_id;
53	u16	subsys_id;
54	enum pci_interrupt_pin interrupt_pin;
55};
56
57/**
58 * struct pci_epf_ops - set of function pointers for performing EPF operations
59 * @bind: ops to perform when a EPC device has been bound to EPF device
60 * @unbind: ops to perform when a binding has been lost between a EPC device
61 *	    and EPF device
62 * @add_cfs: ops to initialize function specific configfs attributes
63 */
64struct pci_epf_ops {
65	int	(*bind)(struct pci_epf *epf);
66	void	(*unbind)(struct pci_epf *epf);
67	struct config_group *(*add_cfs)(struct pci_epf *epf,
68					struct config_group *group);
69};
70
71/**
72 * struct pci_epc_event_ops - Callbacks for capturing the EPC events
73 * @core_init: Callback for the EPC initialization complete event
74 * @link_up: Callback for the EPC link up event
75 * @link_down: Callback for the EPC link down event
76 * @bme: Callback for the EPC BME (Bus Master Enable) event
77 */
78struct pci_epc_event_ops {
79	int (*core_init)(struct pci_epf *epf);
80	int (*link_up)(struct pci_epf *epf);
81	int (*link_down)(struct pci_epf *epf);
82	int (*bme)(struct pci_epf *epf);
83};
84
85/**
86 * struct pci_epf_driver - represents the PCI EPF driver
87 * @probe: ops to perform when a new EPF device has been bound to the EPF driver
88 * @remove: ops to perform when the binding between the EPF device and EPF
89 *	    driver is broken
90 * @driver: PCI EPF driver
91 * @ops: set of function pointers for performing EPF operations
92 * @owner: the owner of the module that registers the PCI EPF driver
93 * @epf_group: list of configfs group corresponding to the PCI EPF driver
94 * @id_table: identifies EPF devices for probing
95 */
96struct pci_epf_driver {
97	int	(*probe)(struct pci_epf *epf,
98			 const struct pci_epf_device_id *id);
99	void	(*remove)(struct pci_epf *epf);
100
101	struct device_driver	driver;
102	const struct pci_epf_ops *ops;
103	struct module		*owner;
104	struct list_head	epf_group;
105	const struct pci_epf_device_id	*id_table;
106};
107
108#define to_pci_epf_driver(drv) (container_of((drv), struct pci_epf_driver, \
109				driver))
110
111/**
112 * struct pci_epf_bar - represents the BAR of EPF device
113 * @phys_addr: physical address that should be mapped to the BAR
114 * @addr: virtual address corresponding to the @phys_addr
115 * @size: the size of the address space present in BAR
116 * @barno: BAR number
117 * @flags: flags that are set for the BAR
118 */
119struct pci_epf_bar {
120	dma_addr_t	phys_addr;
121	void		*addr;
122	size_t		size;
123	enum pci_barno	barno;
124	int		flags;
125};
126
127/**
128 * struct pci_epf - represents the PCI EPF device
129 * @dev: the PCI EPF device
130 * @name: the name of the PCI EPF device
131 * @header: represents standard configuration header
132 * @bar: represents the BAR of EPF device
133 * @msi_interrupts: number of MSI interrupts required by this function
134 * @msix_interrupts: number of MSI-X interrupts required by this function
135 * @func_no: unique (physical) function number within this endpoint device
136 * @vfunc_no: unique virtual function number within a physical function
137 * @epc: the EPC device to which this EPF device is bound
138 * @epf_pf: the physical EPF device to which this virtual EPF device is bound
139 * @driver: the EPF driver to which this EPF device is bound
140 * @id: Pointer to the EPF device ID
141 * @list: to add pci_epf as a list of PCI endpoint functions to pci_epc
142 * @lock: mutex to protect pci_epf_ops
143 * @sec_epc: the secondary EPC device to which this EPF device is bound
144 * @sec_epc_list: to add pci_epf as list of PCI endpoint functions to secondary
145 *   EPC device
146 * @sec_epc_bar: represents the BAR of EPF device associated with secondary EPC
147 * @sec_epc_func_no: unique (physical) function number within the secondary EPC
148 * @group: configfs group associated with the EPF device
149 * @is_bound: indicates if bind notification to function driver has been invoked
150 * @is_vf: true - virtual function, false - physical function
151 * @vfunction_num_map: bitmap to manage virtual function number
152 * @pci_vepf: list of virtual endpoint functions associated with this function
153 * @event_ops: Callbacks for capturing the EPC events
154 */
155struct pci_epf {
156	struct device		dev;
157	const char		*name;
158	struct pci_epf_header	*header;
159	struct pci_epf_bar	bar[6];
160	u8			msi_interrupts;
161	u16			msix_interrupts;
162	u8			func_no;
163	u8			vfunc_no;
164
165	struct pci_epc		*epc;
166	struct pci_epf		*epf_pf;
167	struct pci_epf_driver	*driver;
168	const struct pci_epf_device_id *id;
169	struct list_head	list;
170	/* mutex to protect against concurrent access of pci_epf_ops */
171	struct mutex		lock;
172
173	/* Below members are to attach secondary EPC to an endpoint function */
174	struct pci_epc		*sec_epc;
175	struct list_head	sec_epc_list;
176	struct pci_epf_bar	sec_epc_bar[6];
177	u8			sec_epc_func_no;
178	struct config_group	*group;
179	unsigned int		is_bound;
180	unsigned int		is_vf;
181	unsigned long		vfunction_num_map;
182	struct list_head	pci_vepf;
183	const struct pci_epc_event_ops *event_ops;
184};
185
186/**
187 * struct pci_epf_msix_tbl - represents the MSIX table entry structure
188 * @msg_addr: Writes to this address will trigger MSIX interrupt in host
189 * @msg_data: Data that should be written to @msg_addr to trigger MSIX interrupt
190 * @vector_ctrl: Identifies if the function is prohibited from sending a message
191 * using this MSIX table entry
192 */
193struct pci_epf_msix_tbl {
194	u64 msg_addr;
195	u32 msg_data;
196	u32 vector_ctrl;
197};
198
199#define to_pci_epf(epf_dev) container_of((epf_dev), struct pci_epf, dev)
200
201#define pci_epf_register_driver(driver)    \
202		__pci_epf_register_driver((driver), THIS_MODULE)
203
204static inline void epf_set_drvdata(struct pci_epf *epf, void *data)
205{
206	dev_set_drvdata(&epf->dev, data);
207}
208
209static inline void *epf_get_drvdata(struct pci_epf *epf)
210{
211	return dev_get_drvdata(&epf->dev);
212}
213
214struct pci_epf *pci_epf_create(const char *name);
215void pci_epf_destroy(struct pci_epf *epf);
216int __pci_epf_register_driver(struct pci_epf_driver *driver,
217			      struct module *owner);
218void pci_epf_unregister_driver(struct pci_epf_driver *driver);
219void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
220			  const struct pci_epc_features *epc_features,
221			  enum pci_epc_interface_type type);
222void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
223			enum pci_epc_interface_type type);
224int pci_epf_bind(struct pci_epf *epf);
225void pci_epf_unbind(struct pci_epf *epf);
226int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf);
227void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf);
228#endif /* __LINUX_PCI_EPF_H */
229