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