Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Thunderbolt service API |
| 3 | * |
Mika Westerberg | eaf8ff3 | 2017-10-02 13:38:31 +0300 | [diff] [blame] | 4 | * Copyright (C) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 5 | * Copyright (C) 2017, Intel Corporation |
| 6 | * Authors: Michael Jamet <michael.jamet@intel.com> |
| 7 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #ifndef THUNDERBOLT_H_ |
| 15 | #define THUNDERBOLT_H_ |
| 16 | |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 17 | #include <linux/device.h> |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 18 | #include <linux/idr.h> |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 19 | #include <linux/list.h> |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 21 | #include <linux/mod_devicetable.h> |
Mika Westerberg | 3304559 | 2017-10-02 13:38:42 +0300 | [diff] [blame] | 22 | #include <linux/pci.h> |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 23 | #include <linux/uuid.h> |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 25 | |
Mika Westerberg | eaf8ff3 | 2017-10-02 13:38:31 +0300 | [diff] [blame] | 26 | enum tb_cfg_pkg_type { |
| 27 | TB_CFG_PKG_READ = 1, |
| 28 | TB_CFG_PKG_WRITE = 2, |
| 29 | TB_CFG_PKG_ERROR = 3, |
| 30 | TB_CFG_PKG_NOTIFY_ACK = 4, |
| 31 | TB_CFG_PKG_EVENT = 5, |
| 32 | TB_CFG_PKG_XDOMAIN_REQ = 6, |
| 33 | TB_CFG_PKG_XDOMAIN_RESP = 7, |
| 34 | TB_CFG_PKG_OVERRIDE = 8, |
| 35 | TB_CFG_PKG_RESET = 9, |
| 36 | TB_CFG_PKG_ICM_EVENT = 10, |
| 37 | TB_CFG_PKG_ICM_CMD = 11, |
| 38 | TB_CFG_PKG_ICM_RESP = 12, |
| 39 | TB_CFG_PKG_PREPARE_TO_SLEEP = 13, |
| 40 | }; |
| 41 | |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 42 | /** |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 43 | * enum tb_security_level - Thunderbolt security level |
| 44 | * @TB_SECURITY_NONE: No security, legacy mode |
| 45 | * @TB_SECURITY_USER: User approval required at minimum |
| 46 | * @TB_SECURITY_SECURE: One time saved key required at minimum |
| 47 | * @TB_SECURITY_DPONLY: Only tunnel Display port (and USB) |
| 48 | */ |
| 49 | enum tb_security_level { |
| 50 | TB_SECURITY_NONE, |
| 51 | TB_SECURITY_USER, |
| 52 | TB_SECURITY_SECURE, |
| 53 | TB_SECURITY_DPONLY, |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * struct tb - main thunderbolt bus structure |
| 58 | * @dev: Domain device |
| 59 | * @lock: Big lock. Must be held when accessing any struct |
| 60 | * tb_switch / struct tb_port. |
| 61 | * @nhi: Pointer to the NHI structure |
| 62 | * @ctl: Control channel for this domain |
| 63 | * @wq: Ordered workqueue for all domain specific work |
| 64 | * @root_switch: Root switch of this domain |
| 65 | * @cm_ops: Connection manager specific operations vector |
| 66 | * @index: Linux assigned domain number |
| 67 | * @security_level: Current security level |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame^] | 68 | * @nboot_acl: Number of boot ACLs the domain supports |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 69 | * @privdata: Private connection manager specific data |
| 70 | */ |
| 71 | struct tb { |
| 72 | struct device dev; |
| 73 | struct mutex lock; |
| 74 | struct tb_nhi *nhi; |
| 75 | struct tb_ctl *ctl; |
| 76 | struct workqueue_struct *wq; |
| 77 | struct tb_switch *root_switch; |
| 78 | const struct tb_cm_ops *cm_ops; |
| 79 | int index; |
| 80 | enum tb_security_level security_level; |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame^] | 81 | size_t nboot_acl; |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 82 | unsigned long privdata[0]; |
| 83 | }; |
| 84 | |
| 85 | extern struct bus_type tb_bus_type; |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 86 | extern struct device_type tb_service_type; |
| 87 | extern struct device_type tb_xdomain_type; |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 88 | |
Mika Westerberg | e69b71f | 2017-10-02 13:38:33 +0300 | [diff] [blame] | 89 | #define TB_LINKS_PER_PHY_PORT 2 |
| 90 | |
| 91 | static inline unsigned int tb_phy_port_from_link(unsigned int link) |
| 92 | { |
| 93 | return (link - 1) / TB_LINKS_PER_PHY_PORT; |
| 94 | } |
| 95 | |
Mika Westerberg | 9e99b9f | 2017-10-02 13:38:32 +0300 | [diff] [blame] | 96 | /** |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 97 | * struct tb_property_dir - XDomain property directory |
| 98 | * @uuid: Directory UUID or %NULL if root directory |
| 99 | * @properties: List of properties in this directory |
| 100 | * |
| 101 | * User needs to provide serialization if needed. |
| 102 | */ |
| 103 | struct tb_property_dir { |
| 104 | const uuid_t *uuid; |
| 105 | struct list_head properties; |
| 106 | }; |
| 107 | |
| 108 | enum tb_property_type { |
| 109 | TB_PROPERTY_TYPE_UNKNOWN = 0x00, |
| 110 | TB_PROPERTY_TYPE_DIRECTORY = 0x44, |
| 111 | TB_PROPERTY_TYPE_DATA = 0x64, |
| 112 | TB_PROPERTY_TYPE_TEXT = 0x74, |
| 113 | TB_PROPERTY_TYPE_VALUE = 0x76, |
| 114 | }; |
| 115 | |
| 116 | #define TB_PROPERTY_KEY_SIZE 8 |
| 117 | |
| 118 | /** |
| 119 | * struct tb_property - XDomain property |
| 120 | * @list: Used to link properties together in a directory |
| 121 | * @key: Key for the property (always terminated). |
| 122 | * @type: Type of the property |
| 123 | * @length: Length of the property data in dwords |
| 124 | * @value: Property value |
| 125 | * |
| 126 | * Users use @type to determine which field in @value is filled. |
| 127 | */ |
| 128 | struct tb_property { |
| 129 | struct list_head list; |
| 130 | char key[TB_PROPERTY_KEY_SIZE + 1]; |
| 131 | enum tb_property_type type; |
| 132 | size_t length; |
| 133 | union { |
| 134 | struct tb_property_dir *dir; |
| 135 | u8 *data; |
| 136 | char *text; |
| 137 | u32 immediate; |
| 138 | } value; |
| 139 | }; |
| 140 | |
| 141 | struct tb_property_dir *tb_property_parse_dir(const u32 *block, |
| 142 | size_t block_len); |
| 143 | ssize_t tb_property_format_dir(const struct tb_property_dir *dir, u32 *block, |
| 144 | size_t block_len); |
| 145 | struct tb_property_dir *tb_property_create_dir(const uuid_t *uuid); |
| 146 | void tb_property_free_dir(struct tb_property_dir *dir); |
| 147 | int tb_property_add_immediate(struct tb_property_dir *parent, const char *key, |
| 148 | u32 value); |
| 149 | int tb_property_add_data(struct tb_property_dir *parent, const char *key, |
| 150 | const void *buf, size_t buflen); |
| 151 | int tb_property_add_text(struct tb_property_dir *parent, const char *key, |
| 152 | const char *text); |
| 153 | int tb_property_add_dir(struct tb_property_dir *parent, const char *key, |
| 154 | struct tb_property_dir *dir); |
| 155 | void tb_property_remove(struct tb_property *tb_property); |
| 156 | struct tb_property *tb_property_find(struct tb_property_dir *dir, |
| 157 | const char *key, enum tb_property_type type); |
| 158 | struct tb_property *tb_property_get_next(struct tb_property_dir *dir, |
| 159 | struct tb_property *prev); |
| 160 | |
| 161 | #define tb_property_for_each(dir, property) \ |
| 162 | for (property = tb_property_get_next(dir, NULL); \ |
| 163 | property; \ |
| 164 | property = tb_property_get_next(dir, property)) |
| 165 | |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 166 | int tb_register_property_dir(const char *key, struct tb_property_dir *dir); |
| 167 | void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir); |
| 168 | |
| 169 | /** |
| 170 | * struct tb_xdomain - Cross-domain (XDomain) connection |
| 171 | * @dev: XDomain device |
| 172 | * @tb: Pointer to the domain |
| 173 | * @remote_uuid: UUID of the remote domain (host) |
| 174 | * @local_uuid: Cached local UUID |
| 175 | * @route: Route string the other domain can be reached |
| 176 | * @vendor: Vendor ID of the remote domain |
| 177 | * @device: Device ID of the demote domain |
| 178 | * @lock: Lock to serialize access to the following fields of this structure |
| 179 | * @vendor_name: Name of the vendor (or %NULL if not known) |
| 180 | * @device_name: Name of the device (or %NULL if not known) |
| 181 | * @is_unplugged: The XDomain is unplugged |
| 182 | * @resume: The XDomain is being resumed |
| 183 | * @transmit_path: HopID which the remote end expects us to transmit |
| 184 | * @transmit_ring: Local ring (hop) where outgoing packets are pushed |
| 185 | * @receive_path: HopID which we expect the remote end to transmit |
| 186 | * @receive_ring: Local ring (hop) where incoming packets arrive |
| 187 | * @service_ids: Used to generate IDs for the services |
| 188 | * @properties: Properties exported by the remote domain |
| 189 | * @property_block_gen: Generation of @properties |
| 190 | * @properties_lock: Lock protecting @properties. |
| 191 | * @get_properties_work: Work used to get remote domain properties |
| 192 | * @properties_retries: Number of times left to read properties |
| 193 | * @properties_changed_work: Work used to notify the remote domain that |
| 194 | * our properties have changed |
| 195 | * @properties_changed_retries: Number of times left to send properties |
| 196 | * changed notification |
| 197 | * @link: Root switch link the remote domain is connected (ICM only) |
| 198 | * @depth: Depth in the chain the remote domain is connected (ICM only) |
| 199 | * |
| 200 | * This structure represents connection across two domains (hosts). |
| 201 | * Each XDomain contains zero or more services which are exposed as |
| 202 | * &struct tb_service objects. |
| 203 | * |
| 204 | * Service drivers may access this structure if they need to enumerate |
| 205 | * non-standard properties but they need hold @lock when doing so |
| 206 | * because properties can be changed asynchronously in response to |
| 207 | * changes in the remote domain. |
| 208 | */ |
| 209 | struct tb_xdomain { |
| 210 | struct device dev; |
| 211 | struct tb *tb; |
| 212 | uuid_t *remote_uuid; |
| 213 | const uuid_t *local_uuid; |
| 214 | u64 route; |
| 215 | u16 vendor; |
| 216 | u16 device; |
| 217 | struct mutex lock; |
| 218 | const char *vendor_name; |
| 219 | const char *device_name; |
| 220 | bool is_unplugged; |
| 221 | bool resume; |
| 222 | u16 transmit_path; |
| 223 | u16 transmit_ring; |
| 224 | u16 receive_path; |
| 225 | u16 receive_ring; |
| 226 | struct ida service_ids; |
| 227 | struct tb_property_dir *properties; |
| 228 | u32 property_block_gen; |
| 229 | struct delayed_work get_properties_work; |
| 230 | int properties_retries; |
| 231 | struct delayed_work properties_changed_work; |
| 232 | int properties_changed_retries; |
| 233 | u8 link; |
| 234 | u8 depth; |
| 235 | }; |
| 236 | |
| 237 | int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path, |
| 238 | u16 transmit_ring, u16 receive_path, |
| 239 | u16 receive_ring); |
| 240 | int tb_xdomain_disable_paths(struct tb_xdomain *xd); |
| 241 | struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid); |
Radion Mirchevsky | 484cb15 | 2017-10-04 14:53:54 +0300 | [diff] [blame] | 242 | struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route); |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 243 | |
| 244 | static inline struct tb_xdomain * |
| 245 | tb_xdomain_find_by_uuid_locked(struct tb *tb, const uuid_t *uuid) |
| 246 | { |
| 247 | struct tb_xdomain *xd; |
| 248 | |
| 249 | mutex_lock(&tb->lock); |
| 250 | xd = tb_xdomain_find_by_uuid(tb, uuid); |
| 251 | mutex_unlock(&tb->lock); |
| 252 | |
| 253 | return xd; |
| 254 | } |
| 255 | |
Radion Mirchevsky | 484cb15 | 2017-10-04 14:53:54 +0300 | [diff] [blame] | 256 | static inline struct tb_xdomain * |
| 257 | tb_xdomain_find_by_route_locked(struct tb *tb, u64 route) |
| 258 | { |
| 259 | struct tb_xdomain *xd; |
| 260 | |
| 261 | mutex_lock(&tb->lock); |
| 262 | xd = tb_xdomain_find_by_route(tb, route); |
| 263 | mutex_unlock(&tb->lock); |
| 264 | |
| 265 | return xd; |
| 266 | } |
| 267 | |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 268 | static inline struct tb_xdomain *tb_xdomain_get(struct tb_xdomain *xd) |
| 269 | { |
| 270 | if (xd) |
| 271 | get_device(&xd->dev); |
| 272 | return xd; |
| 273 | } |
| 274 | |
| 275 | static inline void tb_xdomain_put(struct tb_xdomain *xd) |
| 276 | { |
| 277 | if (xd) |
| 278 | put_device(&xd->dev); |
| 279 | } |
| 280 | |
| 281 | static inline bool tb_is_xdomain(const struct device *dev) |
| 282 | { |
| 283 | return dev->type == &tb_xdomain_type; |
| 284 | } |
| 285 | |
| 286 | static inline struct tb_xdomain *tb_to_xdomain(struct device *dev) |
| 287 | { |
| 288 | if (tb_is_xdomain(dev)) |
| 289 | return container_of(dev, struct tb_xdomain, dev); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | int tb_xdomain_response(struct tb_xdomain *xd, const void *response, |
| 294 | size_t size, enum tb_cfg_pkg_type type); |
| 295 | int tb_xdomain_request(struct tb_xdomain *xd, const void *request, |
| 296 | size_t request_size, enum tb_cfg_pkg_type request_type, |
| 297 | void *response, size_t response_size, |
| 298 | enum tb_cfg_pkg_type response_type, |
| 299 | unsigned int timeout_msec); |
| 300 | |
| 301 | /** |
| 302 | * tb_protocol_handler - Protocol specific handler |
| 303 | * @uuid: XDomain messages with this UUID are dispatched to this handler |
| 304 | * @callback: Callback called with the XDomain message. Returning %1 |
| 305 | * here tells the XDomain core that the message was handled |
| 306 | * by this handler and should not be forwared to other |
| 307 | * handlers. |
| 308 | * @data: Data passed with the callback |
| 309 | * @list: Handlers are linked using this |
| 310 | * |
| 311 | * Thunderbolt services can hook into incoming XDomain requests by |
| 312 | * registering protocol handler. Only limitation is that the XDomain |
| 313 | * discovery protocol UUID cannot be registered since it is handled by |
| 314 | * the core XDomain code. |
| 315 | * |
| 316 | * The @callback must check that the message is really directed to the |
| 317 | * service the driver implements. |
| 318 | */ |
| 319 | struct tb_protocol_handler { |
| 320 | const uuid_t *uuid; |
| 321 | int (*callback)(const void *buf, size_t size, void *data); |
| 322 | void *data; |
| 323 | struct list_head list; |
| 324 | }; |
| 325 | |
| 326 | int tb_register_protocol_handler(struct tb_protocol_handler *handler); |
| 327 | void tb_unregister_protocol_handler(struct tb_protocol_handler *handler); |
| 328 | |
| 329 | /** |
| 330 | * struct tb_service - Thunderbolt service |
| 331 | * @dev: XDomain device |
| 332 | * @id: ID of the service (shown in sysfs) |
| 333 | * @key: Protocol key from the properties directory |
| 334 | * @prtcid: Protocol ID from the properties directory |
| 335 | * @prtcvers: Protocol version from the properties directory |
| 336 | * @prtcrevs: Protocol software revision from the properties directory |
| 337 | * @prtcstns: Protocol settings mask from the properties directory |
| 338 | * |
| 339 | * Each domain exposes set of services it supports as collection of |
| 340 | * properties. For each service there will be one corresponding |
| 341 | * &struct tb_service. Service drivers are bound to these. |
| 342 | */ |
| 343 | struct tb_service { |
| 344 | struct device dev; |
| 345 | int id; |
| 346 | const char *key; |
| 347 | u32 prtcid; |
| 348 | u32 prtcvers; |
| 349 | u32 prtcrevs; |
| 350 | u32 prtcstns; |
| 351 | }; |
| 352 | |
| 353 | static inline struct tb_service *tb_service_get(struct tb_service *svc) |
| 354 | { |
| 355 | if (svc) |
| 356 | get_device(&svc->dev); |
| 357 | return svc; |
| 358 | } |
| 359 | |
| 360 | static inline void tb_service_put(struct tb_service *svc) |
| 361 | { |
| 362 | if (svc) |
| 363 | put_device(&svc->dev); |
| 364 | } |
| 365 | |
| 366 | static inline bool tb_is_service(const struct device *dev) |
| 367 | { |
| 368 | return dev->type == &tb_service_type; |
| 369 | } |
| 370 | |
| 371 | static inline struct tb_service *tb_to_service(struct device *dev) |
| 372 | { |
| 373 | if (tb_is_service(dev)) |
| 374 | return container_of(dev, struct tb_service, dev); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * tb_service_driver - Thunderbolt service driver |
| 380 | * @driver: Driver structure |
| 381 | * @probe: Called when the driver is probed |
| 382 | * @remove: Called when the driver is removed (optional) |
| 383 | * @shutdown: Called at shutdown time to stop the service (optional) |
| 384 | * @id_table: Table of service identifiers the driver supports |
| 385 | */ |
| 386 | struct tb_service_driver { |
| 387 | struct device_driver driver; |
| 388 | int (*probe)(struct tb_service *svc, const struct tb_service_id *id); |
| 389 | void (*remove)(struct tb_service *svc); |
| 390 | void (*shutdown)(struct tb_service *svc); |
| 391 | const struct tb_service_id *id_table; |
| 392 | }; |
| 393 | |
| 394 | #define TB_SERVICE(key, id) \ |
| 395 | .match_flags = TBSVC_MATCH_PROTOCOL_KEY | \ |
| 396 | TBSVC_MATCH_PROTOCOL_ID, \ |
| 397 | .protocol_key = (key), \ |
| 398 | .protocol_id = (id) |
| 399 | |
| 400 | int tb_register_service_driver(struct tb_service_driver *drv); |
| 401 | void tb_unregister_service_driver(struct tb_service_driver *drv); |
| 402 | |
| 403 | static inline void *tb_service_get_drvdata(const struct tb_service *svc) |
| 404 | { |
| 405 | return dev_get_drvdata(&svc->dev); |
| 406 | } |
| 407 | |
| 408 | static inline void tb_service_set_drvdata(struct tb_service *svc, void *data) |
| 409 | { |
| 410 | dev_set_drvdata(&svc->dev, data); |
| 411 | } |
| 412 | |
| 413 | static inline struct tb_xdomain *tb_service_parent(struct tb_service *svc) |
| 414 | { |
| 415 | return tb_to_xdomain(svc->dev.parent); |
| 416 | } |
| 417 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 418 | /** |
| 419 | * struct tb_nhi - thunderbolt native host interface |
| 420 | * @lock: Must be held during ring creation/destruction. Is acquired by |
| 421 | * interrupt_work when dispatching interrupts to individual rings. |
| 422 | * @pdev: Pointer to the PCI device |
| 423 | * @iobase: MMIO space of the NHI |
| 424 | * @tx_rings: All Tx rings available on this host controller |
| 425 | * @rx_rings: All Rx rings available on this host controller |
| 426 | * @msix_ida: Used to allocate MSI-X vectors for rings |
| 427 | * @going_away: The host controller device is about to disappear so when |
| 428 | * this flag is set, avoid touching the hardware anymore. |
| 429 | * @interrupt_work: Work scheduled to handle ring interrupt when no |
| 430 | * MSI-X is used. |
| 431 | * @hop_count: Number of rings (end point hops) supported by NHI. |
| 432 | */ |
| 433 | struct tb_nhi { |
Mika Westerberg | 59120e0 | 2017-10-02 13:38:40 +0300 | [diff] [blame] | 434 | spinlock_t lock; |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 435 | struct pci_dev *pdev; |
| 436 | void __iomem *iobase; |
| 437 | struct tb_ring **tx_rings; |
| 438 | struct tb_ring **rx_rings; |
| 439 | struct ida msix_ida; |
| 440 | bool going_away; |
| 441 | struct work_struct interrupt_work; |
| 442 | u32 hop_count; |
| 443 | }; |
| 444 | |
| 445 | /** |
| 446 | * struct tb_ring - thunderbolt TX or RX ring associated with a NHI |
| 447 | * @lock: Lock serializing actions to this ring. Must be acquired after |
| 448 | * nhi->lock. |
| 449 | * @nhi: Pointer to the native host controller interface |
| 450 | * @size: Size of the ring |
| 451 | * @hop: Hop (DMA channel) associated with this ring |
| 452 | * @head: Head of the ring (write next descriptor here) |
| 453 | * @tail: Tail of the ring (complete next descriptor here) |
| 454 | * @descriptors: Allocated descriptors for this ring |
| 455 | * @queue: Queue holding frames to be transferred over this ring |
| 456 | * @in_flight: Queue holding frames that are currently in flight |
| 457 | * @work: Interrupt work structure |
| 458 | * @is_tx: Is the ring Tx or Rx |
| 459 | * @running: Is the ring running |
| 460 | * @irq: MSI-X irq number if the ring uses MSI-X. %0 otherwise. |
| 461 | * @vector: MSI-X vector number the ring uses (only set if @irq is > 0) |
| 462 | * @flags: Ring specific flags |
| 463 | * @sof_mask: Bit mask used to detect start of frame PDF |
| 464 | * @eof_mask: Bit mask used to detect end of frame PDF |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 465 | * @start_poll: Called when ring interrupt is triggered to start |
| 466 | * polling. Passing %NULL keeps the ring in interrupt mode. |
| 467 | * @poll_data: Data passed to @start_poll |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 468 | */ |
| 469 | struct tb_ring { |
Mika Westerberg | 22b7de1 | 2017-10-02 13:38:39 +0300 | [diff] [blame] | 470 | spinlock_t lock; |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 471 | struct tb_nhi *nhi; |
| 472 | int size; |
| 473 | int hop; |
| 474 | int head; |
| 475 | int tail; |
| 476 | struct ring_desc *descriptors; |
| 477 | dma_addr_t descriptors_dma; |
| 478 | struct list_head queue; |
| 479 | struct list_head in_flight; |
| 480 | struct work_struct work; |
| 481 | bool is_tx:1; |
| 482 | bool running:1; |
| 483 | int irq; |
| 484 | u8 vector; |
| 485 | unsigned int flags; |
| 486 | u16 sof_mask; |
| 487 | u16 eof_mask; |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 488 | void (*start_poll)(void *data); |
| 489 | void *poll_data; |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | /* Leave ring interrupt enabled on suspend */ |
| 493 | #define RING_FLAG_NO_SUSPEND BIT(0) |
| 494 | /* Configure the ring to be in frame mode */ |
| 495 | #define RING_FLAG_FRAME BIT(1) |
| 496 | /* Enable end-to-end flow control */ |
| 497 | #define RING_FLAG_E2E BIT(2) |
| 498 | |
| 499 | struct ring_frame; |
| 500 | typedef void (*ring_cb)(struct tb_ring *, struct ring_frame *, bool canceled); |
| 501 | |
| 502 | /** |
Mika Westerberg | 2a91ec6 | 2017-10-02 13:38:38 +0300 | [diff] [blame] | 503 | * enum ring_desc_flags - Flags for DMA ring descriptor |
| 504 | * %RING_DESC_ISOCH: Enable isonchronous DMA (Tx only) |
| 505 | * %RING_DESC_CRC_ERROR: In frame mode CRC check failed for the frame (Rx only) |
| 506 | * %RING_DESC_COMPLETED: Descriptor completed (set by NHI) |
| 507 | * %RING_DESC_POSTED: Always set this |
| 508 | * %RING_DESC_BUFFER_OVERRUN: RX buffer overrun |
| 509 | * %RING_DESC_INTERRUPT: Request an interrupt on completion |
| 510 | */ |
| 511 | enum ring_desc_flags { |
| 512 | RING_DESC_ISOCH = 0x1, |
| 513 | RING_DESC_CRC_ERROR = 0x1, |
| 514 | RING_DESC_COMPLETED = 0x2, |
| 515 | RING_DESC_POSTED = 0x4, |
| 516 | RING_DESC_BUFFER_OVERRUN = 0x04, |
| 517 | RING_DESC_INTERRUPT = 0x8, |
| 518 | }; |
| 519 | |
| 520 | /** |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 521 | * struct ring_frame - For use with ring_rx/ring_tx |
| 522 | * @buffer_phy: DMA mapped address of the frame |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 523 | * @callback: Callback called when the frame is finished (optional) |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 524 | * @list: Frame is linked to a queue using this |
| 525 | * @size: Size of the frame in bytes (%0 means %4096) |
| 526 | * @flags: Flags for the frame (see &enum ring_desc_flags) |
| 527 | * @eof: End of frame protocol defined field |
| 528 | * @sof: Start of frame protocol defined field |
| 529 | */ |
| 530 | struct ring_frame { |
| 531 | dma_addr_t buffer_phy; |
| 532 | ring_cb callback; |
| 533 | struct list_head list; |
| 534 | u32 size:12; |
| 535 | u32 flags:12; |
| 536 | u32 eof:4; |
| 537 | u32 sof:4; |
| 538 | }; |
| 539 | |
| 540 | /* Minimum size for ring_rx */ |
| 541 | #define TB_FRAME_SIZE 0x100 |
| 542 | |
| 543 | struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size, |
| 544 | unsigned int flags); |
| 545 | struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size, |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 546 | unsigned int flags, u16 sof_mask, u16 eof_mask, |
| 547 | void (*start_poll)(void *), void *poll_data); |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 548 | void tb_ring_start(struct tb_ring *ring); |
| 549 | void tb_ring_stop(struct tb_ring *ring); |
| 550 | void tb_ring_free(struct tb_ring *ring); |
| 551 | |
| 552 | int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame); |
| 553 | |
| 554 | /** |
| 555 | * tb_ring_rx() - enqueue a frame on an RX ring |
| 556 | * @ring: Ring to enqueue the frame |
| 557 | * @frame: Frame to enqueue |
| 558 | * |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 559 | * @frame->buffer, @frame->buffer_phy have to be set. The buffer must |
| 560 | * contain at least %TB_FRAME_SIZE bytes. |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 561 | * |
| 562 | * @frame->callback will be invoked with @frame->size, @frame->flags, |
| 563 | * @frame->eof, @frame->sof set once the frame has been received. |
| 564 | * |
| 565 | * If ring_stop() is called after the packet has been enqueued |
| 566 | * @frame->callback will be called with canceled set to true. |
| 567 | * |
| 568 | * Return: Returns %-ESHUTDOWN if ring_stop has been called. Zero otherwise. |
| 569 | */ |
| 570 | static inline int tb_ring_rx(struct tb_ring *ring, struct ring_frame *frame) |
| 571 | { |
| 572 | WARN_ON(ring->is_tx); |
| 573 | return __tb_ring_enqueue(ring, frame); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * tb_ring_tx() - enqueue a frame on an TX ring |
| 578 | * @ring: Ring the enqueue the frame |
| 579 | * @frame: Frame to enqueue |
| 580 | * |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 581 | * @frame->buffer, @frame->buffer_phy, @frame->size, @frame->eof and |
| 582 | * @frame->sof have to be set. |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 583 | * |
| 584 | * @frame->callback will be invoked with once the frame has been transmitted. |
| 585 | * |
| 586 | * If ring_stop() is called after the packet has been enqueued @frame->callback |
| 587 | * will be called with canceled set to true. |
| 588 | * |
| 589 | * Return: Returns %-ESHUTDOWN if ring_stop has been called. Zero otherwise. |
| 590 | */ |
| 591 | static inline int tb_ring_tx(struct tb_ring *ring, struct ring_frame *frame) |
| 592 | { |
| 593 | WARN_ON(!ring->is_tx); |
| 594 | return __tb_ring_enqueue(ring, frame); |
| 595 | } |
| 596 | |
Mika Westerberg | 4ffe722 | 2017-10-02 13:38:41 +0300 | [diff] [blame] | 597 | /* Used only when the ring is in polling mode */ |
| 598 | struct ring_frame *tb_ring_poll(struct tb_ring *ring); |
| 599 | void tb_ring_poll_complete(struct tb_ring *ring); |
| 600 | |
Mika Westerberg | 3304559 | 2017-10-02 13:38:42 +0300 | [diff] [blame] | 601 | /** |
| 602 | * tb_ring_dma_device() - Return device used for DMA mapping |
| 603 | * @ring: Ring whose DMA device is retrieved |
| 604 | * |
| 605 | * Use this function when you are mapping DMA for buffers that are |
| 606 | * passed to the ring for sending/receiving. |
| 607 | */ |
| 608 | static inline struct device *tb_ring_dma_device(struct tb_ring *ring) |
| 609 | { |
| 610 | return &ring->nhi->pdev->dev; |
| 611 | } |
| 612 | |
Mika Westerberg | cdae7c0 | 2017-10-02 13:38:30 +0300 | [diff] [blame] | 613 | #endif /* THUNDERBOLT_H_ */ |