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