Documentation / thunderbolt: Clean up entries
[linux-2.6-microblaze.git] / drivers / thunderbolt / tb.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Thunderbolt driver - bus logic (NHI independent)
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2018, Intel Corporation
7  */
8
9 #ifndef TB_H_
10 #define TB_H_
11
12 #include <linux/nvmem-provider.h>
13 #include <linux/pci.h>
14 #include <linux/thunderbolt.h>
15 #include <linux/uuid.h>
16
17 #include "tb_regs.h"
18 #include "ctl.h"
19 #include "dma_port.h"
20
21 #define NVM_MIN_SIZE            SZ_32K
22 #define NVM_MAX_SIZE            SZ_512K
23 #define NVM_DATA_DWORDS         16
24
25 /* Intel specific NVM offsets */
26 #define NVM_DEVID               0x05
27 #define NVM_VERSION             0x08
28 #define NVM_FLASH_SIZE          0x45
29
30 /**
31  * struct tb_nvm - Structure holding NVM information
32  * @dev: Owner of the NVM
33  * @major: Major version number of the active NVM portion
34  * @minor: Minor version number of the active NVM portion
35  * @id: Identifier used with both NVM portions
36  * @active: Active portion NVMem device
37  * @non_active: Non-active portion NVMem device
38  * @buf: Buffer where the NVM image is stored before it is written to
39  *       the actual NVM flash device
40  * @buf_data_size: Number of bytes actually consumed by the new NVM
41  *                 image
42  * @authenticating: The device is authenticating the new NVM
43  * @flushed: The image has been flushed to the storage area
44  *
45  * The user of this structure needs to handle serialization of possible
46  * concurrent access.
47  */
48 struct tb_nvm {
49         struct device *dev;
50         u8 major;
51         u8 minor;
52         int id;
53         struct nvmem_device *active;
54         struct nvmem_device *non_active;
55         void *buf;
56         size_t buf_data_size;
57         bool authenticating;
58         bool flushed;
59 };
60
61 #define TB_SWITCH_KEY_SIZE              32
62 #define TB_SWITCH_MAX_DEPTH             6
63 #define USB4_SWITCH_MAX_DEPTH           5
64
65 /**
66  * enum tb_switch_tmu_rate - TMU refresh rate
67  * @TB_SWITCH_TMU_RATE_OFF: %0 (Disable Time Sync handshake)
68  * @TB_SWITCH_TMU_RATE_HIFI: %16 us time interval between successive
69  *                           transmission of the Delay Request TSNOS
70  *                           (Time Sync Notification Ordered Set) on a Link
71  * @TB_SWITCH_TMU_RATE_NORMAL: %1 ms time interval between successive
72  *                             transmission of the Delay Request TSNOS on
73  *                             a Link
74  */
75 enum tb_switch_tmu_rate {
76         TB_SWITCH_TMU_RATE_OFF = 0,
77         TB_SWITCH_TMU_RATE_HIFI = 16,
78         TB_SWITCH_TMU_RATE_NORMAL = 1000,
79 };
80
81 /**
82  * struct tb_switch_tmu - Structure holding switch TMU configuration
83  * @cap: Offset to the TMU capability (%0 if not found)
84  * @has_ucap: Does the switch support uni-directional mode
85  * @rate: TMU refresh rate related to upstream switch. In case of root
86  *        switch this holds the domain rate.
87  * @unidirectional: Is the TMU in uni-directional or bi-directional mode
88  *                  related to upstream switch. Don't case for root switch.
89  */
90 struct tb_switch_tmu {
91         int cap;
92         bool has_ucap;
93         enum tb_switch_tmu_rate rate;
94         bool unidirectional;
95 };
96
97 /**
98  * struct tb_switch - a thunderbolt switch
99  * @dev: Device for the switch
100  * @config: Switch configuration
101  * @ports: Ports in this switch
102  * @dma_port: If the switch has port supporting DMA configuration based
103  *            mailbox this will hold the pointer to that (%NULL
104  *            otherwise). If set it also means the switch has
105  *            upgradeable NVM.
106  * @tmu: The switch TMU configuration
107  * @tb: Pointer to the domain the switch belongs to
108  * @uid: Unique ID of the switch
109  * @uuid: UUID of the switch (or %NULL if not supported)
110  * @vendor: Vendor ID of the switch
111  * @device: Device ID of the switch
112  * @vendor_name: Name of the vendor (or %NULL if not known)
113  * @device_name: Name of the device (or %NULL if not known)
114  * @link_speed: Speed of the link in Gb/s
115  * @link_width: Width of the link (1 or 2)
116  * @link_usb4: Upstream link is USB4
117  * @generation: Switch Thunderbolt generation
118  * @cap_plug_events: Offset to the plug events capability (%0 if not found)
119  * @cap_lc: Offset to the link controller capability (%0 if not found)
120  * @is_unplugged: The switch is going away
121  * @drom: DROM of the switch (%NULL if not found)
122  * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
123  * @no_nvm_upgrade: Prevent NVM upgrade of this switch
124  * @safe_mode: The switch is in safe-mode
125  * @boot: Whether the switch was already authorized on boot or not
126  * @rpm: The switch supports runtime PM
127  * @authorized: Whether the switch is authorized by user or policy
128  * @security_level: Switch supported security level
129  * @debugfs_dir: Pointer to the debugfs structure
130  * @key: Contains the key used to challenge the device or %NULL if not
131  *       supported. Size of the key is %TB_SWITCH_KEY_SIZE.
132  * @connection_id: Connection ID used with ICM messaging
133  * @connection_key: Connection key used with ICM messaging
134  * @link: Root switch link this switch is connected (ICM only)
135  * @depth: Depth in the chain this switch is connected (ICM only)
136  * @rpm_complete: Completion used to wait for runtime resume to
137  *                complete (ICM only)
138  * @quirks: Quirks used for this Thunderbolt switch
139  *
140  * When the switch is being added or removed to the domain (other
141  * switches) you need to have domain lock held.
142  *
143  * In USB4 terminology this structure represents a router.
144  */
145 struct tb_switch {
146         struct device dev;
147         struct tb_regs_switch_header config;
148         struct tb_port *ports;
149         struct tb_dma_port *dma_port;
150         struct tb_switch_tmu tmu;
151         struct tb *tb;
152         u64 uid;
153         uuid_t *uuid;
154         u16 vendor;
155         u16 device;
156         const char *vendor_name;
157         const char *device_name;
158         unsigned int link_speed;
159         unsigned int link_width;
160         bool link_usb4;
161         unsigned int generation;
162         int cap_plug_events;
163         int cap_lc;
164         bool is_unplugged;
165         u8 *drom;
166         struct tb_nvm *nvm;
167         bool no_nvm_upgrade;
168         bool safe_mode;
169         bool boot;
170         bool rpm;
171         unsigned int authorized;
172         enum tb_security_level security_level;
173         struct dentry *debugfs_dir;
174         u8 *key;
175         u8 connection_id;
176         u8 connection_key;
177         u8 link;
178         u8 depth;
179         struct completion rpm_complete;
180         unsigned long quirks;
181 };
182
183 /**
184  * struct tb_port - a thunderbolt port, part of a tb_switch
185  * @config: Cached port configuration read from registers
186  * @sw: Switch the port belongs to
187  * @remote: Remote port (%NULL if not connected)
188  * @xdomain: Remote host (%NULL if not connected)
189  * @cap_phy: Offset, zero if not found
190  * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
191  * @cap_adap: Offset of the adapter specific capability (%0 if not present)
192  * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
193  * @port: Port number on switch
194  * @disabled: Disabled by eeprom or enabled but not implemented
195  * @bonded: true if the port is bonded (two lanes combined as one)
196  * @dual_link_port: If the switch is connected using two ports, points
197  *                  to the other port.
198  * @link_nr: Is this primary or secondary port on the dual_link.
199  * @in_hopids: Currently allocated input HopIDs
200  * @out_hopids: Currently allocated output HopIDs
201  * @list: Used to link ports to DP resources list
202  *
203  * In USB4 terminology this structure represents an adapter (protocol or
204  * lane adapter).
205  */
206 struct tb_port {
207         struct tb_regs_port_header config;
208         struct tb_switch *sw;
209         struct tb_port *remote;
210         struct tb_xdomain *xdomain;
211         int cap_phy;
212         int cap_tmu;
213         int cap_adap;
214         int cap_usb4;
215         u8 port;
216         bool disabled;
217         bool bonded;
218         struct tb_port *dual_link_port;
219         u8 link_nr:1;
220         struct ida in_hopids;
221         struct ida out_hopids;
222         struct list_head list;
223 };
224
225 /**
226  * tb_retimer: Thunderbolt retimer
227  * @dev: Device for the retimer
228  * @tb: Pointer to the domain the retimer belongs to
229  * @index: Retimer index facing the router USB4 port
230  * @vendor: Vendor ID of the retimer
231  * @device: Device ID of the retimer
232  * @port: Pointer to the lane 0 adapter
233  * @nvm: Pointer to the NVM if the retimer has one (%NULL otherwise)
234  * @auth_status: Status of last NVM authentication
235  */
236 struct tb_retimer {
237         struct device dev;
238         struct tb *tb;
239         u8 index;
240         u32 vendor;
241         u32 device;
242         struct tb_port *port;
243         struct tb_nvm *nvm;
244         u32 auth_status;
245 };
246
247 /**
248  * struct tb_path_hop - routing information for a tb_path
249  * @in_port: Ingress port of a switch
250  * @out_port: Egress port of a switch where the packet is routed out
251  *            (must be on the same switch than @in_port)
252  * @in_hop_index: HopID where the path configuration entry is placed in
253  *                the path config space of @in_port.
254  * @in_counter_index: Used counter index (not used in the driver
255  *                    currently, %-1 to disable)
256  * @next_hop_index: HopID of the packet when it is routed out from @out_port
257  * @initial_credits: Number of initial flow control credits allocated for
258  *                   the path
259  *
260  * Hop configuration is always done on the IN port of a switch.
261  * in_port and out_port have to be on the same switch. Packets arriving on
262  * in_port with "hop" = in_hop_index will get routed to through out_port. The
263  * next hop to take (on out_port->remote) is determined by
264  * next_hop_index. When routing packet to another switch (out->remote is
265  * set) the @next_hop_index must match the @in_hop_index of that next
266  * hop to make routing possible.
267  *
268  * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
269  * port.
270  */
271 struct tb_path_hop {
272         struct tb_port *in_port;
273         struct tb_port *out_port;
274         int in_hop_index;
275         int in_counter_index;
276         int next_hop_index;
277         unsigned int initial_credits;
278 };
279
280 /**
281  * enum tb_path_port - path options mask
282  * @TB_PATH_NONE: Do not activate on any hop on path
283  * @TB_PATH_SOURCE: Activate on the first hop (out of src)
284  * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
285  * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
286  * @TB_PATH_ALL: Activate on all hops on the path
287  */
288 enum tb_path_port {
289         TB_PATH_NONE = 0,
290         TB_PATH_SOURCE = 1,
291         TB_PATH_INTERNAL = 2,
292         TB_PATH_DESTINATION = 4,
293         TB_PATH_ALL = 7,
294 };
295
296 /**
297  * struct tb_path - a unidirectional path between two ports
298  * @tb: Pointer to the domain structure
299  * @name: Name of the path (used for debugging)
300  * @nfc_credits: Number of non flow controlled credits allocated for the path
301  * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
302  * @egress_shared_buffer: Shared buffering used for egress ports on the path
303  * @ingress_fc_enable: Flow control for ingress ports on the path
304  * @egress_fc_enable: Flow control for egress ports on the path
305  * @priority: Priority group if the path
306  * @weight: Weight of the path inside the priority group
307  * @drop_packages: Drop packages from queue tail or head
308  * @activated: Is the path active
309  * @clear_fc: Clear all flow control from the path config space entries
310  *            when deactivating this path
311  * @hops: Path hops
312  * @path_length: How many hops the path uses
313  *
314  * A path consists of a number of hops (see &struct tb_path_hop). To
315  * establish a PCIe tunnel two paths have to be created between the two
316  * PCIe ports.
317  */
318 struct tb_path {
319         struct tb *tb;
320         const char *name;
321         int nfc_credits;
322         enum tb_path_port ingress_shared_buffer;
323         enum tb_path_port egress_shared_buffer;
324         enum tb_path_port ingress_fc_enable;
325         enum tb_path_port egress_fc_enable;
326
327         unsigned int priority:3;
328         int weight:4;
329         bool drop_packages;
330         bool activated;
331         bool clear_fc;
332         struct tb_path_hop *hops;
333         int path_length;
334 };
335
336 /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
337 #define TB_PATH_MIN_HOPID       8
338 /*
339  * Support paths from the farthest (depth 6) router to the host and back
340  * to the same level (not necessarily to the same router).
341  */
342 #define TB_PATH_MAX_HOPS        (7 * 2)
343
344 /* Possible wake types */
345 #define TB_WAKE_ON_CONNECT      BIT(0)
346 #define TB_WAKE_ON_DISCONNECT   BIT(1)
347 #define TB_WAKE_ON_USB4         BIT(2)
348 #define TB_WAKE_ON_USB3         BIT(3)
349 #define TB_WAKE_ON_PCIE         BIT(4)
350
351 /**
352  * struct tb_cm_ops - Connection manager specific operations vector
353  * @driver_ready: Called right after control channel is started. Used by
354  *                ICM to send driver ready message to the firmware.
355  * @start: Starts the domain
356  * @stop: Stops the domain
357  * @suspend_noirq: Connection manager specific suspend_noirq
358  * @resume_noirq: Connection manager specific resume_noirq
359  * @suspend: Connection manager specific suspend
360  * @freeze_noirq: Connection manager specific freeze_noirq
361  * @thaw_noirq: Connection manager specific thaw_noirq
362  * @complete: Connection manager specific complete
363  * @runtime_suspend: Connection manager specific runtime_suspend
364  * @runtime_resume: Connection manager specific runtime_resume
365  * @runtime_suspend_switch: Runtime suspend a switch
366  * @runtime_resume_switch: Runtime resume a switch
367  * @handle_event: Handle thunderbolt event
368  * @get_boot_acl: Get boot ACL list
369  * @set_boot_acl: Set boot ACL list
370  * @disapprove_switch: Disapprove switch (disconnect PCIe tunnel)
371  * @approve_switch: Approve switch
372  * @add_switch_key: Add key to switch
373  * @challenge_switch_key: Challenge switch using key
374  * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
375  * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
376  * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
377  * @usb4_switch_op: Optional proxy for USB4 router operations. If set
378  *                  this will be called whenever USB4 router operation is
379  *                  performed. If this returns %-EOPNOTSUPP then the
380  *                  native USB4 router operation is called.
381  * @usb4_switch_nvm_authenticate_status: Optional callback that the CM
382  *                                       implementation can be used to
383  *                                       return status of USB4 NVM_AUTH
384  *                                       router operation.
385  */
386 struct tb_cm_ops {
387         int (*driver_ready)(struct tb *tb);
388         int (*start)(struct tb *tb);
389         void (*stop)(struct tb *tb);
390         int (*suspend_noirq)(struct tb *tb);
391         int (*resume_noirq)(struct tb *tb);
392         int (*suspend)(struct tb *tb);
393         int (*freeze_noirq)(struct tb *tb);
394         int (*thaw_noirq)(struct tb *tb);
395         void (*complete)(struct tb *tb);
396         int (*runtime_suspend)(struct tb *tb);
397         int (*runtime_resume)(struct tb *tb);
398         int (*runtime_suspend_switch)(struct tb_switch *sw);
399         int (*runtime_resume_switch)(struct tb_switch *sw);
400         void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
401                              const void *buf, size_t size);
402         int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
403         int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
404         int (*disapprove_switch)(struct tb *tb, struct tb_switch *sw);
405         int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
406         int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
407         int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
408                                     const u8 *challenge, u8 *response);
409         int (*disconnect_pcie_paths)(struct tb *tb);
410         int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
411                                      int transmit_path, int transmit_ring,
412                                      int receive_path, int receive_ring);
413         int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
414                                         int transmit_path, int transmit_ring,
415                                         int receive_path, int receive_ring);
416         int (*usb4_switch_op)(struct tb_switch *sw, u16 opcode, u32 *metadata,
417                               u8 *status, const void *tx_data, size_t tx_data_len,
418                               void *rx_data, size_t rx_data_len);
419         int (*usb4_switch_nvm_authenticate_status)(struct tb_switch *sw,
420                                                    u32 *status);
421 };
422
423 static inline void *tb_priv(struct tb *tb)
424 {
425         return (void *)tb->privdata;
426 }
427
428 #define TB_AUTOSUSPEND_DELAY            15000 /* ms */
429
430 /* helper functions & macros */
431
432 /**
433  * tb_upstream_port() - return the upstream port of a switch
434  *
435  * Every switch has an upstream port (for the root switch it is the NHI).
436  *
437  * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
438  * non root switches (on the NHI port remote is always NULL).
439  *
440  * Return: Returns the upstream port of the switch.
441  */
442 static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
443 {
444         return &sw->ports[sw->config.upstream_port_number];
445 }
446
447 /**
448  * tb_is_upstream_port() - Is the port upstream facing
449  * @port: Port to check
450  *
451  * Returns true if @port is upstream facing port. In case of dual link
452  * ports both return true.
453  */
454 static inline bool tb_is_upstream_port(const struct tb_port *port)
455 {
456         const struct tb_port *upstream_port = tb_upstream_port(port->sw);
457         return port == upstream_port || port->dual_link_port == upstream_port;
458 }
459
460 static inline u64 tb_route(const struct tb_switch *sw)
461 {
462         return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
463 }
464
465 static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
466 {
467         u8 port;
468
469         port = route >> (sw->config.depth * 8);
470         if (WARN_ON(port > sw->config.max_port_number))
471                 return NULL;
472         return &sw->ports[port];
473 }
474
475 /**
476  * tb_port_has_remote() - Does the port have switch connected downstream
477  * @port: Port to check
478  *
479  * Returns true only when the port is primary port and has remote set.
480  */
481 static inline bool tb_port_has_remote(const struct tb_port *port)
482 {
483         if (tb_is_upstream_port(port))
484                 return false;
485         if (!port->remote)
486                 return false;
487         if (port->dual_link_port && port->link_nr)
488                 return false;
489
490         return true;
491 }
492
493 static inline bool tb_port_is_null(const struct tb_port *port)
494 {
495         return port && port->port && port->config.type == TB_TYPE_PORT;
496 }
497
498 static inline bool tb_port_is_nhi(const struct tb_port *port)
499 {
500         return port && port->config.type == TB_TYPE_NHI;
501 }
502
503 static inline bool tb_port_is_pcie_down(const struct tb_port *port)
504 {
505         return port && port->config.type == TB_TYPE_PCIE_DOWN;
506 }
507
508 static inline bool tb_port_is_pcie_up(const struct tb_port *port)
509 {
510         return port && port->config.type == TB_TYPE_PCIE_UP;
511 }
512
513 static inline bool tb_port_is_dpin(const struct tb_port *port)
514 {
515         return port && port->config.type == TB_TYPE_DP_HDMI_IN;
516 }
517
518 static inline bool tb_port_is_dpout(const struct tb_port *port)
519 {
520         return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
521 }
522
523 static inline bool tb_port_is_usb3_down(const struct tb_port *port)
524 {
525         return port && port->config.type == TB_TYPE_USB3_DOWN;
526 }
527
528 static inline bool tb_port_is_usb3_up(const struct tb_port *port)
529 {
530         return port && port->config.type == TB_TYPE_USB3_UP;
531 }
532
533 static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
534                              enum tb_cfg_space space, u32 offset, u32 length)
535 {
536         if (sw->is_unplugged)
537                 return -ENODEV;
538         return tb_cfg_read(sw->tb->ctl,
539                            buffer,
540                            tb_route(sw),
541                            0,
542                            space,
543                            offset,
544                            length);
545 }
546
547 static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
548                               enum tb_cfg_space space, u32 offset, u32 length)
549 {
550         if (sw->is_unplugged)
551                 return -ENODEV;
552         return tb_cfg_write(sw->tb->ctl,
553                             buffer,
554                             tb_route(sw),
555                             0,
556                             space,
557                             offset,
558                             length);
559 }
560
561 static inline int tb_port_read(struct tb_port *port, void *buffer,
562                                enum tb_cfg_space space, u32 offset, u32 length)
563 {
564         if (port->sw->is_unplugged)
565                 return -ENODEV;
566         return tb_cfg_read(port->sw->tb->ctl,
567                            buffer,
568                            tb_route(port->sw),
569                            port->port,
570                            space,
571                            offset,
572                            length);
573 }
574
575 static inline int tb_port_write(struct tb_port *port, const void *buffer,
576                                 enum tb_cfg_space space, u32 offset, u32 length)
577 {
578         if (port->sw->is_unplugged)
579                 return -ENODEV;
580         return tb_cfg_write(port->sw->tb->ctl,
581                             buffer,
582                             tb_route(port->sw),
583                             port->port,
584                             space,
585                             offset,
586                             length);
587 }
588
589 #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
590 #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
591 #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
592 #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
593 #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
594
595 #define __TB_SW_PRINT(level, sw, fmt, arg...)           \
596         do {                                            \
597                 const struct tb_switch *__sw = (sw);    \
598                 level(__sw->tb, "%llx: " fmt,           \
599                       tb_route(__sw), ## arg);          \
600         } while (0)
601 #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
602 #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
603 #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
604 #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
605
606 #define __TB_PORT_PRINT(level, _port, fmt, arg...)                      \
607         do {                                                            \
608                 const struct tb_port *__port = (_port);                 \
609                 level(__port->sw->tb, "%llx:%x: " fmt,                  \
610                       tb_route(__port->sw), __port->port, ## arg);      \
611         } while (0)
612 #define tb_port_WARN(port, fmt, arg...) \
613         __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
614 #define tb_port_warn(port, fmt, arg...) \
615         __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
616 #define tb_port_info(port, fmt, arg...) \
617         __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
618 #define tb_port_dbg(port, fmt, arg...) \
619         __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
620
621 struct tb *icm_probe(struct tb_nhi *nhi);
622 struct tb *tb_probe(struct tb_nhi *nhi);
623
624 extern struct device_type tb_domain_type;
625 extern struct device_type tb_retimer_type;
626 extern struct device_type tb_switch_type;
627
628 int tb_domain_init(void);
629 void tb_domain_exit(void);
630 int tb_xdomain_init(void);
631 void tb_xdomain_exit(void);
632
633 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize);
634 int tb_domain_add(struct tb *tb);
635 void tb_domain_remove(struct tb *tb);
636 int tb_domain_suspend_noirq(struct tb *tb);
637 int tb_domain_resume_noirq(struct tb *tb);
638 int tb_domain_suspend(struct tb *tb);
639 int tb_domain_freeze_noirq(struct tb *tb);
640 int tb_domain_thaw_noirq(struct tb *tb);
641 void tb_domain_complete(struct tb *tb);
642 int tb_domain_runtime_suspend(struct tb *tb);
643 int tb_domain_runtime_resume(struct tb *tb);
644 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw);
645 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
646 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
647 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
648 int tb_domain_disconnect_pcie_paths(struct tb *tb);
649 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
650                                     int transmit_path, int transmit_ring,
651                                     int receive_path, int receive_ring);
652 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
653                                        int transmit_path, int transmit_ring,
654                                        int receive_path, int receive_ring);
655 int tb_domain_disconnect_all_paths(struct tb *tb);
656
657 static inline struct tb *tb_domain_get(struct tb *tb)
658 {
659         if (tb)
660                 get_device(&tb->dev);
661         return tb;
662 }
663
664 static inline void tb_domain_put(struct tb *tb)
665 {
666         put_device(&tb->dev);
667 }
668
669 struct tb_nvm *tb_nvm_alloc(struct device *dev);
670 int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
671 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
672                      size_t bytes);
673 int tb_nvm_add_non_active(struct tb_nvm *nvm, size_t size,
674                           nvmem_reg_write_t reg_write);
675 void tb_nvm_free(struct tb_nvm *nvm);
676 void tb_nvm_exit(void);
677
678 typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
679 typedef int (*write_block_fn)(void *, unsigned int, const void *, size_t);
680
681 int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
682                      unsigned int retries, read_block_fn read_block,
683                      void *read_block_data);
684 int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
685                       unsigned int retries, write_block_fn write_next_block,
686                       void *write_block_data);
687
688 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
689                                   u64 route);
690 struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
691                         struct device *parent, u64 route);
692 int tb_switch_configure(struct tb_switch *sw);
693 int tb_switch_add(struct tb_switch *sw);
694 void tb_switch_remove(struct tb_switch *sw);
695 void tb_switch_suspend(struct tb_switch *sw, bool runtime);
696 int tb_switch_resume(struct tb_switch *sw);
697 int tb_switch_reset(struct tb_switch *sw);
698 void tb_sw_set_unplugged(struct tb_switch *sw);
699 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
700                                     enum tb_port_type type);
701 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
702                                                u8 depth);
703 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
704 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
705
706 /**
707  * tb_switch_for_each_port() - Iterate over each switch port
708  * @sw: Switch whose ports to iterate
709  * @p: Port used as iterator
710  *
711  * Iterates over each switch port skipping the control port (port %0).
712  */
713 #define tb_switch_for_each_port(sw, p)                                  \
714         for ((p) = &(sw)->ports[1];                                     \
715              (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
716
717 static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
718 {
719         if (sw)
720                 get_device(&sw->dev);
721         return sw;
722 }
723
724 static inline void tb_switch_put(struct tb_switch *sw)
725 {
726         put_device(&sw->dev);
727 }
728
729 static inline bool tb_is_switch(const struct device *dev)
730 {
731         return dev->type == &tb_switch_type;
732 }
733
734 static inline struct tb_switch *tb_to_switch(struct device *dev)
735 {
736         if (tb_is_switch(dev))
737                 return container_of(dev, struct tb_switch, dev);
738         return NULL;
739 }
740
741 static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
742 {
743         return tb_to_switch(sw->dev.parent);
744 }
745
746 static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
747 {
748         return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
749                sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
750 }
751
752 static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
753 {
754         return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
755                sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
756 }
757
758 static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
759 {
760         if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
761                 switch (sw->config.device_id) {
762                 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
763                 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
764                         return true;
765                 }
766         }
767         return false;
768 }
769
770 static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
771 {
772         if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
773                 switch (sw->config.device_id) {
774                 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
775                 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
776                         return true;
777                 }
778         }
779         return false;
780 }
781
782 static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
783 {
784         if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
785                 switch (sw->config.device_id) {
786                 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
787                 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
788                 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
789                 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
790                         return true;
791                 }
792         }
793         return false;
794 }
795
796 static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
797 {
798         if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
799                 switch (sw->config.device_id) {
800                 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
801                 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
802                 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
803                         return true;
804                 }
805         }
806         return false;
807 }
808
809 /**
810  * tb_switch_is_usb4() - Is the switch USB4 compliant
811  * @sw: Switch to check
812  *
813  * Returns true if the @sw is USB4 compliant router, false otherwise.
814  */
815 static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
816 {
817         return sw->config.thunderbolt_version == USB4_VERSION_1_0;
818 }
819
820 /**
821  * tb_switch_is_icm() - Is the switch handled by ICM firmware
822  * @sw: Switch to check
823  *
824  * In case there is a need to differentiate whether ICM firmware or SW CM
825  * is handling @sw this function can be called. It is valid to call this
826  * after tb_switch_alloc() and tb_switch_configure() has been called
827  * (latter only for SW CM case).
828  */
829 static inline bool tb_switch_is_icm(const struct tb_switch *sw)
830 {
831         return !sw->config.enabled;
832 }
833
834 int tb_switch_lane_bonding_enable(struct tb_switch *sw);
835 void tb_switch_lane_bonding_disable(struct tb_switch *sw);
836 int tb_switch_configure_link(struct tb_switch *sw);
837 void tb_switch_unconfigure_link(struct tb_switch *sw);
838
839 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
840 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
841 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
842
843 int tb_switch_tmu_init(struct tb_switch *sw);
844 int tb_switch_tmu_post_time(struct tb_switch *sw);
845 int tb_switch_tmu_disable(struct tb_switch *sw);
846 int tb_switch_tmu_enable(struct tb_switch *sw);
847
848 static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw)
849 {
850         return sw->tmu.rate == TB_SWITCH_TMU_RATE_HIFI &&
851                !sw->tmu.unidirectional;
852 }
853
854 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
855 int tb_port_add_nfc_credits(struct tb_port *port, int credits);
856 int tb_port_clear_counter(struct tb_port *port, int counter);
857 int tb_port_unlock(struct tb_port *port);
858 int tb_port_enable(struct tb_port *port);
859 int tb_port_disable(struct tb_port *port);
860 int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
861 void tb_port_release_in_hopid(struct tb_port *port, int hopid);
862 int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
863 void tb_port_release_out_hopid(struct tb_port *port, int hopid);
864 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
865                                      struct tb_port *prev);
866
867 /**
868  * tb_for_each_port_on_path() - Iterate over each port on path
869  * @src: Source port
870  * @dst: Destination port
871  * @p: Port used as iterator
872  *
873  * Walks over each port on path from @src to @dst.
874  */
875 #define tb_for_each_port_on_path(src, dst, p)                           \
876         for ((p) = tb_next_port_on_path((src), (dst), NULL); (p);       \
877              (p) = tb_next_port_on_path((src), (dst), (p)))
878
879 int tb_port_get_link_speed(struct tb_port *port);
880 int tb_port_get_link_width(struct tb_port *port);
881 int tb_port_state(struct tb_port *port);
882 int tb_port_lane_bonding_enable(struct tb_port *port);
883 void tb_port_lane_bonding_disable(struct tb_port *port);
884
885 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
886 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
887 int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
888 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
889 int tb_port_next_cap(struct tb_port *port, unsigned int offset);
890 bool tb_port_is_enabled(struct tb_port *port);
891
892 bool tb_usb3_port_is_enabled(struct tb_port *port);
893 int tb_usb3_port_enable(struct tb_port *port, bool enable);
894
895 bool tb_pci_port_is_enabled(struct tb_port *port);
896 int tb_pci_port_enable(struct tb_port *port, bool enable);
897
898 int tb_dp_port_hpd_is_active(struct tb_port *port);
899 int tb_dp_port_hpd_clear(struct tb_port *port);
900 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
901                         unsigned int aux_tx, unsigned int aux_rx);
902 bool tb_dp_port_is_enabled(struct tb_port *port);
903 int tb_dp_port_enable(struct tb_port *port, bool enable);
904
905 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
906                                  struct tb_port *dst, int dst_hopid,
907                                  struct tb_port **last, const char *name);
908 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
909                               struct tb_port *dst, int dst_hopid, int link_nr,
910                               const char *name);
911 void tb_path_free(struct tb_path *path);
912 int tb_path_activate(struct tb_path *path);
913 void tb_path_deactivate(struct tb_path *path);
914 bool tb_path_is_invalid(struct tb_path *path);
915 bool tb_path_port_on_path(const struct tb_path *path,
916                           const struct tb_port *port);
917
918 int tb_drom_read(struct tb_switch *sw);
919 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
920
921 int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
922 int tb_lc_configure_port(struct tb_port *port);
923 void tb_lc_unconfigure_port(struct tb_port *port);
924 int tb_lc_configure_xdomain(struct tb_port *port);
925 void tb_lc_unconfigure_xdomain(struct tb_port *port);
926 int tb_lc_start_lane_initialization(struct tb_port *port);
927 int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags);
928 int tb_lc_set_sleep(struct tb_switch *sw);
929 bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
930 bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
931 int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
932 int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
933 int tb_lc_force_power(struct tb_switch *sw);
934
935 static inline int tb_route_length(u64 route)
936 {
937         return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
938 }
939
940 /**
941  * tb_downstream_route() - get route to downstream switch
942  *
943  * Port must not be the upstream port (otherwise a loop is created).
944  *
945  * Return: Returns a route to the switch behind @port.
946  */
947 static inline u64 tb_downstream_route(struct tb_port *port)
948 {
949         return tb_route(port->sw)
950                | ((u64) port->port << (port->sw->config.depth * 8));
951 }
952
953 bool tb_is_xdomain_enabled(void);
954 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
955                                const void *buf, size_t size);
956 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
957                                     u64 route, const uuid_t *local_uuid,
958                                     const uuid_t *remote_uuid);
959 void tb_xdomain_add(struct tb_xdomain *xd);
960 void tb_xdomain_remove(struct tb_xdomain *xd);
961 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
962                                                  u8 depth);
963
964 int tb_retimer_scan(struct tb_port *port);
965 void tb_retimer_remove_all(struct tb_port *port);
966
967 static inline bool tb_is_retimer(const struct device *dev)
968 {
969         return dev->type == &tb_retimer_type;
970 }
971
972 static inline struct tb_retimer *tb_to_retimer(struct device *dev)
973 {
974         if (tb_is_retimer(dev))
975                 return container_of(dev, struct tb_retimer, dev);
976         return NULL;
977 }
978
979 int usb4_switch_setup(struct tb_switch *sw);
980 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
981 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
982                           size_t size);
983 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
984 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags);
985 int usb4_switch_set_sleep(struct tb_switch *sw);
986 int usb4_switch_nvm_sector_size(struct tb_switch *sw);
987 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
988                          size_t size);
989 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
990                           const void *buf, size_t size);
991 int usb4_switch_nvm_authenticate(struct tb_switch *sw);
992 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status);
993 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
994 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
995 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
996 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
997                                           const struct tb_port *port);
998 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
999                                           const struct tb_port *port);
1000
1001 int usb4_port_unlock(struct tb_port *port);
1002 int usb4_port_configure(struct tb_port *port);
1003 void usb4_port_unconfigure(struct tb_port *port);
1004 int usb4_port_configure_xdomain(struct tb_port *port);
1005 void usb4_port_unconfigure_xdomain(struct tb_port *port);
1006 int usb4_port_enumerate_retimers(struct tb_port *port);
1007
1008 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1009                            u8 size);
1010 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1011                             const void *buf, u8 size);
1012 int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
1013 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
1014 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
1015                                 unsigned int address, const void *buf,
1016                                 size_t size);
1017 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
1018 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1019                                               u32 *status);
1020 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1021                                unsigned int address, void *buf, size_t size);
1022
1023 int usb4_usb3_port_max_link_rate(struct tb_port *port);
1024 int usb4_usb3_port_actual_link_rate(struct tb_port *port);
1025 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1026                                        int *downstream_bw);
1027 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1028                                       int *downstream_bw);
1029 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1030                                      int *downstream_bw);
1031
1032 /* Keep link controller awake during update */
1033 #define QUIRK_FORCE_POWER_LINK_CONTROLLER               BIT(0)
1034
1035 void tb_check_quirks(struct tb_switch *sw);
1036
1037 #ifdef CONFIG_ACPI
1038 void tb_acpi_add_links(struct tb_nhi *nhi);
1039
1040 bool tb_acpi_is_native(void);
1041 bool tb_acpi_may_tunnel_usb3(void);
1042 bool tb_acpi_may_tunnel_dp(void);
1043 bool tb_acpi_may_tunnel_pcie(void);
1044 bool tb_acpi_is_xdomain_allowed(void);
1045 #else
1046 static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
1047
1048 static inline bool tb_acpi_is_native(void) { return true; }
1049 static inline bool tb_acpi_may_tunnel_usb3(void) { return true; }
1050 static inline bool tb_acpi_may_tunnel_dp(void) { return true; }
1051 static inline bool tb_acpi_may_tunnel_pcie(void) { return true; }
1052 static inline bool tb_acpi_is_xdomain_allowed(void) { return true; }
1053 #endif
1054
1055 #ifdef CONFIG_DEBUG_FS
1056 void tb_debugfs_init(void);
1057 void tb_debugfs_exit(void);
1058 void tb_switch_debugfs_init(struct tb_switch *sw);
1059 void tb_switch_debugfs_remove(struct tb_switch *sw);
1060 void tb_service_debugfs_init(struct tb_service *svc);
1061 void tb_service_debugfs_remove(struct tb_service *svc);
1062 #else
1063 static inline void tb_debugfs_init(void) { }
1064 static inline void tb_debugfs_exit(void) { }
1065 static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
1066 static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
1067 static inline void tb_service_debugfs_init(struct tb_service *svc) { }
1068 static inline void tb_service_debugfs_remove(struct tb_service *svc) { }
1069 #endif
1070
1071 #ifdef CONFIG_USB4_KUNIT_TEST
1072 int tb_test_init(void);
1073 void tb_test_exit(void);
1074 #else
1075 static inline int tb_test_init(void) { return 0; }
1076 static inline void tb_test_exit(void) { }
1077 #endif
1078
1079 #endif