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