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