2 * Thunderbolt service API
4 * Copyright (C) 2014 Andreas Noever <andreas.noever@gmail.com>
5 * Copyright (C) 2017, Intel Corporation
6 * Authors: Michael Jamet <michael.jamet@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
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.
14 #ifndef THUNDERBOLT_H_
15 #define THUNDERBOLT_H_
17 #include <linux/device.h>
18 #include <linux/idr.h>
19 #include <linux/list.h>
20 #include <linux/mutex.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/pci.h>
23 #include <linux/uuid.h>
24 #include <linux/workqueue.h>
26 enum tb_cfg_pkg_type
{
30 TB_CFG_PKG_NOTIFY_ACK
= 4,
32 TB_CFG_PKG_XDOMAIN_REQ
= 6,
33 TB_CFG_PKG_XDOMAIN_RESP
= 7,
34 TB_CFG_PKG_OVERRIDE
= 8,
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,
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)
49 enum tb_security_level
{
57 * struct tb - main thunderbolt bus structure
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
68 * @privdata: Private connection manager specific data
75 struct workqueue_struct
*wq
;
76 struct tb_switch
*root_switch
;
77 const struct tb_cm_ops
*cm_ops
;
79 enum tb_security_level security_level
;
80 unsigned long privdata
[0];
83 extern struct bus_type tb_bus_type
;
84 extern struct device_type tb_service_type
;
85 extern struct device_type tb_xdomain_type
;
87 #define TB_LINKS_PER_PHY_PORT 2
89 static inline unsigned int tb_phy_port_from_link(unsigned int link
)
91 return (link
- 1) / TB_LINKS_PER_PHY_PORT
;
95 * struct tb_property_dir - XDomain property directory
96 * @uuid: Directory UUID or %NULL if root directory
97 * @properties: List of properties in this directory
99 * User needs to provide serialization if needed.
101 struct tb_property_dir
{
103 struct list_head properties
;
106 enum tb_property_type
{
107 TB_PROPERTY_TYPE_UNKNOWN
= 0x00,
108 TB_PROPERTY_TYPE_DIRECTORY
= 0x44,
109 TB_PROPERTY_TYPE_DATA
= 0x64,
110 TB_PROPERTY_TYPE_TEXT
= 0x74,
111 TB_PROPERTY_TYPE_VALUE
= 0x76,
114 #define TB_PROPERTY_KEY_SIZE 8
117 * struct tb_property - XDomain property
118 * @list: Used to link properties together in a directory
119 * @key: Key for the property (always terminated).
120 * @type: Type of the property
121 * @length: Length of the property data in dwords
122 * @value: Property value
124 * Users use @type to determine which field in @value is filled.
127 struct list_head list
;
128 char key
[TB_PROPERTY_KEY_SIZE
+ 1];
129 enum tb_property_type type
;
132 struct tb_property_dir
*dir
;
139 struct tb_property_dir
*tb_property_parse_dir(const u32
*block
,
141 ssize_t
tb_property_format_dir(const struct tb_property_dir
*dir
, u32
*block
,
143 struct tb_property_dir
*tb_property_create_dir(const uuid_t
*uuid
);
144 void tb_property_free_dir(struct tb_property_dir
*dir
);
145 int tb_property_add_immediate(struct tb_property_dir
*parent
, const char *key
,
147 int tb_property_add_data(struct tb_property_dir
*parent
, const char *key
,
148 const void *buf
, size_t buflen
);
149 int tb_property_add_text(struct tb_property_dir
*parent
, const char *key
,
151 int tb_property_add_dir(struct tb_property_dir
*parent
, const char *key
,
152 struct tb_property_dir
*dir
);
153 void tb_property_remove(struct tb_property
*tb_property
);
154 struct tb_property
*tb_property_find(struct tb_property_dir
*dir
,
155 const char *key
, enum tb_property_type type
);
156 struct tb_property
*tb_property_get_next(struct tb_property_dir
*dir
,
157 struct tb_property
*prev
);
159 #define tb_property_for_each(dir, property) \
160 for (property = tb_property_get_next(dir, NULL); \
162 property = tb_property_get_next(dir, property))
164 int tb_register_property_dir(const char *key
, struct tb_property_dir
*dir
);
165 void tb_unregister_property_dir(const char *key
, struct tb_property_dir
*dir
);
168 * struct tb_xdomain - Cross-domain (XDomain) connection
169 * @dev: XDomain device
170 * @tb: Pointer to the domain
171 * @remote_uuid: UUID of the remote domain (host)
172 * @local_uuid: Cached local UUID
173 * @route: Route string the other domain can be reached
174 * @vendor: Vendor ID of the remote domain
175 * @device: Device ID of the demote domain
176 * @lock: Lock to serialize access to the following fields of this structure
177 * @vendor_name: Name of the vendor (or %NULL if not known)
178 * @device_name: Name of the device (or %NULL if not known)
179 * @is_unplugged: The XDomain is unplugged
180 * @resume: The XDomain is being resumed
181 * @transmit_path: HopID which the remote end expects us to transmit
182 * @transmit_ring: Local ring (hop) where outgoing packets are pushed
183 * @receive_path: HopID which we expect the remote end to transmit
184 * @receive_ring: Local ring (hop) where incoming packets arrive
185 * @service_ids: Used to generate IDs for the services
186 * @properties: Properties exported by the remote domain
187 * @property_block_gen: Generation of @properties
188 * @properties_lock: Lock protecting @properties.
189 * @get_properties_work: Work used to get remote domain properties
190 * @properties_retries: Number of times left to read properties
191 * @properties_changed_work: Work used to notify the remote domain that
192 * our properties have changed
193 * @properties_changed_retries: Number of times left to send properties
194 * changed notification
195 * @link: Root switch link the remote domain is connected (ICM only)
196 * @depth: Depth in the chain the remote domain is connected (ICM only)
198 * This structure represents connection across two domains (hosts).
199 * Each XDomain contains zero or more services which are exposed as
200 * &struct tb_service objects.
202 * Service drivers may access this structure if they need to enumerate
203 * non-standard properties but they need hold @lock when doing so
204 * because properties can be changed asynchronously in response to
205 * changes in the remote domain.
211 const uuid_t
*local_uuid
;
216 const char *vendor_name
;
217 const char *device_name
;
224 struct ida service_ids
;
225 struct tb_property_dir
*properties
;
226 u32 property_block_gen
;
227 struct delayed_work get_properties_work
;
228 int properties_retries
;
229 struct delayed_work properties_changed_work
;
230 int properties_changed_retries
;
235 int tb_xdomain_enable_paths(struct tb_xdomain
*xd
, u16 transmit_path
,
236 u16 transmit_ring
, u16 receive_path
,
238 int tb_xdomain_disable_paths(struct tb_xdomain
*xd
);
239 struct tb_xdomain
*tb_xdomain_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
);
241 static inline struct tb_xdomain
*
242 tb_xdomain_find_by_uuid_locked(struct tb
*tb
, const uuid_t
*uuid
)
244 struct tb_xdomain
*xd
;
246 mutex_lock(&tb
->lock
);
247 xd
= tb_xdomain_find_by_uuid(tb
, uuid
);
248 mutex_unlock(&tb
->lock
);
253 static inline struct tb_xdomain
*tb_xdomain_get(struct tb_xdomain
*xd
)
256 get_device(&xd
->dev
);
260 static inline void tb_xdomain_put(struct tb_xdomain
*xd
)
263 put_device(&xd
->dev
);
266 static inline bool tb_is_xdomain(const struct device
*dev
)
268 return dev
->type
== &tb_xdomain_type
;
271 static inline struct tb_xdomain
*tb_to_xdomain(struct device
*dev
)
273 if (tb_is_xdomain(dev
))
274 return container_of(dev
, struct tb_xdomain
, dev
);
278 int tb_xdomain_response(struct tb_xdomain
*xd
, const void *response
,
279 size_t size
, enum tb_cfg_pkg_type type
);
280 int tb_xdomain_request(struct tb_xdomain
*xd
, const void *request
,
281 size_t request_size
, enum tb_cfg_pkg_type request_type
,
282 void *response
, size_t response_size
,
283 enum tb_cfg_pkg_type response_type
,
284 unsigned int timeout_msec
);
287 * tb_protocol_handler - Protocol specific handler
288 * @uuid: XDomain messages with this UUID are dispatched to this handler
289 * @callback: Callback called with the XDomain message. Returning %1
290 * here tells the XDomain core that the message was handled
291 * by this handler and should not be forwared to other
293 * @data: Data passed with the callback
294 * @list: Handlers are linked using this
296 * Thunderbolt services can hook into incoming XDomain requests by
297 * registering protocol handler. Only limitation is that the XDomain
298 * discovery protocol UUID cannot be registered since it is handled by
299 * the core XDomain code.
301 * The @callback must check that the message is really directed to the
302 * service the driver implements.
304 struct tb_protocol_handler
{
306 int (*callback
)(const void *buf
, size_t size
, void *data
);
308 struct list_head list
;
311 int tb_register_protocol_handler(struct tb_protocol_handler
*handler
);
312 void tb_unregister_protocol_handler(struct tb_protocol_handler
*handler
);
315 * struct tb_service - Thunderbolt service
316 * @dev: XDomain device
317 * @id: ID of the service (shown in sysfs)
318 * @key: Protocol key from the properties directory
319 * @prtcid: Protocol ID from the properties directory
320 * @prtcvers: Protocol version from the properties directory
321 * @prtcrevs: Protocol software revision from the properties directory
322 * @prtcstns: Protocol settings mask from the properties directory
324 * Each domain exposes set of services it supports as collection of
325 * properties. For each service there will be one corresponding
326 * &struct tb_service. Service drivers are bound to these.
338 static inline struct tb_service
*tb_service_get(struct tb_service
*svc
)
341 get_device(&svc
->dev
);
345 static inline void tb_service_put(struct tb_service
*svc
)
348 put_device(&svc
->dev
);
351 static inline bool tb_is_service(const struct device
*dev
)
353 return dev
->type
== &tb_service_type
;
356 static inline struct tb_service
*tb_to_service(struct device
*dev
)
358 if (tb_is_service(dev
))
359 return container_of(dev
, struct tb_service
, dev
);
364 * tb_service_driver - Thunderbolt service driver
365 * @driver: Driver structure
366 * @probe: Called when the driver is probed
367 * @remove: Called when the driver is removed (optional)
368 * @shutdown: Called at shutdown time to stop the service (optional)
369 * @id_table: Table of service identifiers the driver supports
371 struct tb_service_driver
{
372 struct device_driver driver
;
373 int (*probe
)(struct tb_service
*svc
, const struct tb_service_id
*id
);
374 void (*remove
)(struct tb_service
*svc
);
375 void (*shutdown
)(struct tb_service
*svc
);
376 const struct tb_service_id
*id_table
;
379 #define TB_SERVICE(key, id) \
380 .match_flags = TBSVC_MATCH_PROTOCOL_KEY | \
381 TBSVC_MATCH_PROTOCOL_ID, \
382 .protocol_key = (key), \
385 int tb_register_service_driver(struct tb_service_driver
*drv
);
386 void tb_unregister_service_driver(struct tb_service_driver
*drv
);
388 static inline void *tb_service_get_drvdata(const struct tb_service
*svc
)
390 return dev_get_drvdata(&svc
->dev
);
393 static inline void tb_service_set_drvdata(struct tb_service
*svc
, void *data
)
395 dev_set_drvdata(&svc
->dev
, data
);
398 static inline struct tb_xdomain
*tb_service_parent(struct tb_service
*svc
)
400 return tb_to_xdomain(svc
->dev
.parent
);
404 * struct tb_nhi - thunderbolt native host interface
405 * @lock: Must be held during ring creation/destruction. Is acquired by
406 * interrupt_work when dispatching interrupts to individual rings.
407 * @pdev: Pointer to the PCI device
408 * @iobase: MMIO space of the NHI
409 * @tx_rings: All Tx rings available on this host controller
410 * @rx_rings: All Rx rings available on this host controller
411 * @msix_ida: Used to allocate MSI-X vectors for rings
412 * @going_away: The host controller device is about to disappear so when
413 * this flag is set, avoid touching the hardware anymore.
414 * @interrupt_work: Work scheduled to handle ring interrupt when no
416 * @hop_count: Number of rings (end point hops) supported by NHI.
420 struct pci_dev
*pdev
;
421 void __iomem
*iobase
;
422 struct tb_ring
**tx_rings
;
423 struct tb_ring
**rx_rings
;
426 struct work_struct interrupt_work
;
431 * struct tb_ring - thunderbolt TX or RX ring associated with a NHI
432 * @lock: Lock serializing actions to this ring. Must be acquired after
434 * @nhi: Pointer to the native host controller interface
435 * @size: Size of the ring
436 * @hop: Hop (DMA channel) associated with this ring
437 * @head: Head of the ring (write next descriptor here)
438 * @tail: Tail of the ring (complete next descriptor here)
439 * @descriptors: Allocated descriptors for this ring
440 * @queue: Queue holding frames to be transferred over this ring
441 * @in_flight: Queue holding frames that are currently in flight
442 * @work: Interrupt work structure
443 * @is_tx: Is the ring Tx or Rx
444 * @running: Is the ring running
445 * @irq: MSI-X irq number if the ring uses MSI-X. %0 otherwise.
446 * @vector: MSI-X vector number the ring uses (only set if @irq is > 0)
447 * @flags: Ring specific flags
448 * @sof_mask: Bit mask used to detect start of frame PDF
449 * @eof_mask: Bit mask used to detect end of frame PDF
450 * @start_poll: Called when ring interrupt is triggered to start
451 * polling. Passing %NULL keeps the ring in interrupt mode.
452 * @poll_data: Data passed to @start_poll
461 struct ring_desc
*descriptors
;
462 dma_addr_t descriptors_dma
;
463 struct list_head queue
;
464 struct list_head in_flight
;
465 struct work_struct work
;
473 void (*start_poll
)(void *data
);
477 /* Leave ring interrupt enabled on suspend */
478 #define RING_FLAG_NO_SUSPEND BIT(0)
479 /* Configure the ring to be in frame mode */
480 #define RING_FLAG_FRAME BIT(1)
481 /* Enable end-to-end flow control */
482 #define RING_FLAG_E2E BIT(2)
485 typedef void (*ring_cb
)(struct tb_ring
*, struct ring_frame
*, bool canceled
);
488 * enum ring_desc_flags - Flags for DMA ring descriptor
489 * %RING_DESC_ISOCH: Enable isonchronous DMA (Tx only)
490 * %RING_DESC_CRC_ERROR: In frame mode CRC check failed for the frame (Rx only)
491 * %RING_DESC_COMPLETED: Descriptor completed (set by NHI)
492 * %RING_DESC_POSTED: Always set this
493 * %RING_DESC_BUFFER_OVERRUN: RX buffer overrun
494 * %RING_DESC_INTERRUPT: Request an interrupt on completion
496 enum ring_desc_flags
{
497 RING_DESC_ISOCH
= 0x1,
498 RING_DESC_CRC_ERROR
= 0x1,
499 RING_DESC_COMPLETED
= 0x2,
500 RING_DESC_POSTED
= 0x4,
501 RING_DESC_BUFFER_OVERRUN
= 0x04,
502 RING_DESC_INTERRUPT
= 0x8,
506 * struct ring_frame - For use with ring_rx/ring_tx
507 * @buffer_phy: DMA mapped address of the frame
508 * @callback: Callback called when the frame is finished (optional)
509 * @list: Frame is linked to a queue using this
510 * @size: Size of the frame in bytes (%0 means %4096)
511 * @flags: Flags for the frame (see &enum ring_desc_flags)
512 * @eof: End of frame protocol defined field
513 * @sof: Start of frame protocol defined field
516 dma_addr_t buffer_phy
;
518 struct list_head list
;
525 /* Minimum size for ring_rx */
526 #define TB_FRAME_SIZE 0x100
528 struct tb_ring
*tb_ring_alloc_tx(struct tb_nhi
*nhi
, int hop
, int size
,
530 struct tb_ring
*tb_ring_alloc_rx(struct tb_nhi
*nhi
, int hop
, int size
,
531 unsigned int flags
, u16 sof_mask
, u16 eof_mask
,
532 void (*start_poll
)(void *), void *poll_data
);
533 void tb_ring_start(struct tb_ring
*ring
);
534 void tb_ring_stop(struct tb_ring
*ring
);
535 void tb_ring_free(struct tb_ring
*ring
);
537 int __tb_ring_enqueue(struct tb_ring
*ring
, struct ring_frame
*frame
);
540 * tb_ring_rx() - enqueue a frame on an RX ring
541 * @ring: Ring to enqueue the frame
542 * @frame: Frame to enqueue
544 * @frame->buffer, @frame->buffer_phy have to be set. The buffer must
545 * contain at least %TB_FRAME_SIZE bytes.
547 * @frame->callback will be invoked with @frame->size, @frame->flags,
548 * @frame->eof, @frame->sof set once the frame has been received.
550 * If ring_stop() is called after the packet has been enqueued
551 * @frame->callback will be called with canceled set to true.
553 * Return: Returns %-ESHUTDOWN if ring_stop has been called. Zero otherwise.
555 static inline int tb_ring_rx(struct tb_ring
*ring
, struct ring_frame
*frame
)
557 WARN_ON(ring
->is_tx
);
558 return __tb_ring_enqueue(ring
, frame
);
562 * tb_ring_tx() - enqueue a frame on an TX ring
563 * @ring: Ring the enqueue the frame
564 * @frame: Frame to enqueue
566 * @frame->buffer, @frame->buffer_phy, @frame->size, @frame->eof and
567 * @frame->sof have to be set.
569 * @frame->callback will be invoked with once the frame has been transmitted.
571 * If ring_stop() is called after the packet has been enqueued @frame->callback
572 * will be called with canceled set to true.
574 * Return: Returns %-ESHUTDOWN if ring_stop has been called. Zero otherwise.
576 static inline int tb_ring_tx(struct tb_ring
*ring
, struct ring_frame
*frame
)
578 WARN_ON(!ring
->is_tx
);
579 return __tb_ring_enqueue(ring
, frame
);
582 /* Used only when the ring is in polling mode */
583 struct ring_frame
*tb_ring_poll(struct tb_ring
*ring
);
584 void tb_ring_poll_complete(struct tb_ring
*ring
);
587 * tb_ring_dma_device() - Return device used for DMA mapping
588 * @ring: Ring whose DMA device is retrieved
590 * Use this function when you are mapping DMA for buffers that are
591 * passed to the ring for sending/receiving.
593 static inline struct device
*tb_ring_dma_device(struct tb_ring
*ring
)
595 return &ring
->nhi
->pdev
->dev
;
598 #endif /* THUNDERBOLT_H_ */