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