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