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