1
2
3
4
5
6
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
26#define NVM_DEVID 0x05
27#define NVM_VERSION 0x08
28#define NVM_FLASH_SIZE 0x45
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48struct 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
61enum tb_nvm_write_ops {
62 WRITE_AND_AUTHENTICATE = 1,
63 WRITE_ONLY = 2,
64 AUTHENTICATE_ONLY = 3,
65};
66
67#define TB_SWITCH_KEY_SIZE 32
68#define TB_SWITCH_MAX_DEPTH 6
69#define USB4_SWITCH_MAX_DEPTH 5
70
71
72
73
74
75
76
77
78
79
80
81enum tb_switch_tmu_rate {
82 TB_SWITCH_TMU_RATE_OFF = 0,
83 TB_SWITCH_TMU_RATE_HIFI = 16,
84 TB_SWITCH_TMU_RATE_NORMAL = 1000,
85};
86
87
88
89
90
91
92
93
94
95
96struct tb_switch_tmu {
97 int cap;
98 bool has_ucap;
99 enum tb_switch_tmu_rate rate;
100 bool unidirectional;
101};
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157struct tb_switch {
158 struct device dev;
159 struct tb_regs_switch_header config;
160 struct tb_port *ports;
161 struct tb_dma_port *dma_port;
162 struct tb_switch_tmu tmu;
163 struct tb *tb;
164 u64 uid;
165 uuid_t *uuid;
166 u16 vendor;
167 u16 device;
168 const char *vendor_name;
169 const char *device_name;
170 unsigned int link_speed;
171 unsigned int link_width;
172 bool link_usb4;
173 unsigned int generation;
174 int cap_plug_events;
175 int cap_lc;
176 bool is_unplugged;
177 u8 *drom;
178 struct tb_nvm *nvm;
179 bool no_nvm_upgrade;
180 bool safe_mode;
181 bool boot;
182 bool rpm;
183 unsigned int authorized;
184 enum tb_security_level security_level;
185 struct dentry *debugfs_dir;
186 u8 *key;
187 u8 connection_id;
188 u8 connection_key;
189 u8 link;
190 u8 depth;
191 struct completion rpm_complete;
192 unsigned long quirks;
193 bool credit_allocation;
194 unsigned int max_usb3_credits;
195 unsigned int min_dp_aux_credits;
196 unsigned int min_dp_main_credits;
197 unsigned int max_pcie_credits;
198 unsigned int max_dma_credits;
199};
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229struct tb_port {
230 struct tb_regs_port_header config;
231 struct tb_switch *sw;
232 struct tb_port *remote;
233 struct tb_xdomain *xdomain;
234 int cap_phy;
235 int cap_tmu;
236 int cap_adap;
237 int cap_usb4;
238 struct usb4_port *usb4;
239 u8 port;
240 bool disabled;
241 bool bonded;
242 struct tb_port *dual_link_port;
243 u8 link_nr:1;
244 struct ida in_hopids;
245 struct ida out_hopids;
246 struct list_head list;
247 unsigned int total_credits;
248 unsigned int ctl_credits;
249 unsigned int dma_credits;
250};
251
252
253
254
255
256
257
258
259
260struct usb4_port {
261 struct device dev;
262 struct tb_port *port;
263 bool can_offline;
264 bool offline;
265};
266
267
268
269
270
271
272
273
274
275
276
277
278struct tb_retimer {
279 struct device dev;
280 struct tb *tb;
281 u8 index;
282 u32 vendor;
283 u32 device;
284 struct tb_port *port;
285 struct tb_nvm *nvm;
286 u32 auth_status;
287};
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315struct tb_path_hop {
316 struct tb_port *in_port;
317 struct tb_port *out_port;
318 int in_hop_index;
319 int in_counter_index;
320 int next_hop_index;
321 unsigned int initial_credits;
322 unsigned int nfc_credits;
323};
324
325
326
327
328
329
330
331
332
333enum tb_path_port {
334 TB_PATH_NONE = 0,
335 TB_PATH_SOURCE = 1,
336 TB_PATH_INTERNAL = 2,
337 TB_PATH_DESTINATION = 4,
338 TB_PATH_ALL = 7,
339};
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362struct tb_path {
363 struct tb *tb;
364 const char *name;
365 enum tb_path_port ingress_shared_buffer;
366 enum tb_path_port egress_shared_buffer;
367 enum tb_path_port ingress_fc_enable;
368 enum tb_path_port egress_fc_enable;
369
370 unsigned int priority:3;
371 int weight:4;
372 bool drop_packages;
373 bool activated;
374 bool clear_fc;
375 struct tb_path_hop *hops;
376 int path_length;
377};
378
379
380#define TB_PATH_MIN_HOPID 8
381
382
383
384
385#define TB_PATH_MAX_HOPS (7 * 2)
386
387
388#define TB_WAKE_ON_CONNECT BIT(0)
389#define TB_WAKE_ON_DISCONNECT BIT(1)
390#define TB_WAKE_ON_USB4 BIT(2)
391#define TB_WAKE_ON_USB3 BIT(3)
392#define TB_WAKE_ON_PCIE BIT(4)
393#define TB_WAKE_ON_DP BIT(5)
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430struct tb_cm_ops {
431 int (*driver_ready)(struct tb *tb);
432 int (*start)(struct tb *tb);
433 void (*stop)(struct tb *tb);
434 int (*suspend_noirq)(struct tb *tb);
435 int (*resume_noirq)(struct tb *tb);
436 int (*suspend)(struct tb *tb);
437 int (*freeze_noirq)(struct tb *tb);
438 int (*thaw_noirq)(struct tb *tb);
439 void (*complete)(struct tb *tb);
440 int (*runtime_suspend)(struct tb *tb);
441 int (*runtime_resume)(struct tb *tb);
442 int (*runtime_suspend_switch)(struct tb_switch *sw);
443 int (*runtime_resume_switch)(struct tb_switch *sw);
444 void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
445 const void *buf, size_t size);
446 int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
447 int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
448 int (*disapprove_switch)(struct tb *tb, struct tb_switch *sw);
449 int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
450 int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
451 int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
452 const u8 *challenge, u8 *response);
453 int (*disconnect_pcie_paths)(struct tb *tb);
454 int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
455 int transmit_path, int transmit_ring,
456 int receive_path, int receive_ring);
457 int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
458 int transmit_path, int transmit_ring,
459 int receive_path, int receive_ring);
460 int (*usb4_switch_op)(struct tb_switch *sw, u16 opcode, u32 *metadata,
461 u8 *status, const void *tx_data, size_t tx_data_len,
462 void *rx_data, size_t rx_data_len);
463 int (*usb4_switch_nvm_authenticate_status)(struct tb_switch *sw,
464 u32 *status);
465};
466
467static inline void *tb_priv(struct tb *tb)
468{
469 return (void *)tb->privdata;
470}
471
472#define TB_AUTOSUSPEND_DELAY 15000
473
474
475
476
477
478
479
480
481
482
483
484
485
486static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
487{
488 return &sw->ports[sw->config.upstream_port_number];
489}
490
491
492
493
494
495
496
497
498static inline bool tb_is_upstream_port(const struct tb_port *port)
499{
500 const struct tb_port *upstream_port = tb_upstream_port(port->sw);
501 return port == upstream_port || port->dual_link_port == upstream_port;
502}
503
504static inline u64 tb_route(const struct tb_switch *sw)
505{
506 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
507}
508
509static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
510{
511 u8 port;
512
513 port = route >> (sw->config.depth * 8);
514 if (WARN_ON(port > sw->config.max_port_number))
515 return NULL;
516 return &sw->ports[port];
517}
518
519
520
521
522
523
524
525static inline bool tb_port_has_remote(const struct tb_port *port)
526{
527 if (tb_is_upstream_port(port))
528 return false;
529 if (!port->remote)
530 return false;
531 if (port->dual_link_port && port->link_nr)
532 return false;
533
534 return true;
535}
536
537static inline bool tb_port_is_null(const struct tb_port *port)
538{
539 return port && port->port && port->config.type == TB_TYPE_PORT;
540}
541
542static inline bool tb_port_is_nhi(const struct tb_port *port)
543{
544 return port && port->config.type == TB_TYPE_NHI;
545}
546
547static inline bool tb_port_is_pcie_down(const struct tb_port *port)
548{
549 return port && port->config.type == TB_TYPE_PCIE_DOWN;
550}
551
552static inline bool tb_port_is_pcie_up(const struct tb_port *port)
553{
554 return port && port->config.type == TB_TYPE_PCIE_UP;
555}
556
557static inline bool tb_port_is_dpin(const struct tb_port *port)
558{
559 return port && port->config.type == TB_TYPE_DP_HDMI_IN;
560}
561
562static inline bool tb_port_is_dpout(const struct tb_port *port)
563{
564 return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
565}
566
567static inline bool tb_port_is_usb3_down(const struct tb_port *port)
568{
569 return port && port->config.type == TB_TYPE_USB3_DOWN;
570}
571
572static inline bool tb_port_is_usb3_up(const struct tb_port *port)
573{
574 return port && port->config.type == TB_TYPE_USB3_UP;
575}
576
577static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
578 enum tb_cfg_space space, u32 offset, u32 length)
579{
580 if (sw->is_unplugged)
581 return -ENODEV;
582 return tb_cfg_read(sw->tb->ctl,
583 buffer,
584 tb_route(sw),
585 0,
586 space,
587 offset,
588 length);
589}
590
591static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
592 enum tb_cfg_space space, u32 offset, u32 length)
593{
594 if (sw->is_unplugged)
595 return -ENODEV;
596 return tb_cfg_write(sw->tb->ctl,
597 buffer,
598 tb_route(sw),
599 0,
600 space,
601 offset,
602 length);
603}
604
605static inline int tb_port_read(struct tb_port *port, void *buffer,
606 enum tb_cfg_space space, u32 offset, u32 length)
607{
608 if (port->sw->is_unplugged)
609 return -ENODEV;
610 return tb_cfg_read(port->sw->tb->ctl,
611 buffer,
612 tb_route(port->sw),
613 port->port,
614 space,
615 offset,
616 length);
617}
618
619static inline int tb_port_write(struct tb_port *port, const void *buffer,
620 enum tb_cfg_space space, u32 offset, u32 length)
621{
622 if (port->sw->is_unplugged)
623 return -ENODEV;
624 return tb_cfg_write(port->sw->tb->ctl,
625 buffer,
626 tb_route(port->sw),
627 port->port,
628 space,
629 offset,
630 length);
631}
632
633#define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
634#define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
635#define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
636#define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
637#define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
638
639#define __TB_SW_PRINT(level, sw, fmt, arg...) \
640 do { \
641 const struct tb_switch *__sw = (sw); \
642 level(__sw->tb, "%llx: " fmt, \
643 tb_route(__sw), ## arg); \
644 } while (0)
645#define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
646#define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
647#define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
648#define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
649
650#define __TB_PORT_PRINT(level, _port, fmt, arg...) \
651 do { \
652 const struct tb_port *__port = (_port); \
653 level(__port->sw->tb, "%llx:%x: " fmt, \
654 tb_route(__port->sw), __port->port, ## arg); \
655 } while (0)
656#define tb_port_WARN(port, fmt, arg...) \
657 __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
658#define tb_port_warn(port, fmt, arg...) \
659 __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
660#define tb_port_info(port, fmt, arg...) \
661 __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
662#define tb_port_dbg(port, fmt, arg...) \
663 __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
664
665struct tb *icm_probe(struct tb_nhi *nhi);
666struct tb *tb_probe(struct tb_nhi *nhi);
667
668extern struct device_type tb_domain_type;
669extern struct device_type tb_retimer_type;
670extern struct device_type tb_switch_type;
671extern struct device_type usb4_port_device_type;
672
673int tb_domain_init(void);
674void tb_domain_exit(void);
675int tb_xdomain_init(void);
676void tb_xdomain_exit(void);
677
678struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize);
679int tb_domain_add(struct tb *tb);
680void tb_domain_remove(struct tb *tb);
681int tb_domain_suspend_noirq(struct tb *tb);
682int tb_domain_resume_noirq(struct tb *tb);
683int tb_domain_suspend(struct tb *tb);
684int tb_domain_freeze_noirq(struct tb *tb);
685int tb_domain_thaw_noirq(struct tb *tb);
686void tb_domain_complete(struct tb *tb);
687int tb_domain_runtime_suspend(struct tb *tb);
688int tb_domain_runtime_resume(struct tb *tb);
689int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw);
690int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
691int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
692int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
693int tb_domain_disconnect_pcie_paths(struct tb *tb);
694int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
695 int transmit_path, int transmit_ring,
696 int receive_path, int receive_ring);
697int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
698 int transmit_path, int transmit_ring,
699 int receive_path, int receive_ring);
700int tb_domain_disconnect_all_paths(struct tb *tb);
701
702static inline struct tb *tb_domain_get(struct tb *tb)
703{
704 if (tb)
705 get_device(&tb->dev);
706 return tb;
707}
708
709static inline void tb_domain_put(struct tb *tb)
710{
711 put_device(&tb->dev);
712}
713
714struct tb_nvm *tb_nvm_alloc(struct device *dev);
715int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
716int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
717 size_t bytes);
718int tb_nvm_add_non_active(struct tb_nvm *nvm, size_t size,
719 nvmem_reg_write_t reg_write);
720void tb_nvm_free(struct tb_nvm *nvm);
721void tb_nvm_exit(void);
722
723typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
724typedef int (*write_block_fn)(void *, unsigned int, const void *, size_t);
725
726int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
727 unsigned int retries, read_block_fn read_block,
728 void *read_block_data);
729int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
730 unsigned int retries, write_block_fn write_next_block,
731 void *write_block_data);
732
733struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
734 u64 route);
735struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
736 struct device *parent, u64 route);
737int tb_switch_configure(struct tb_switch *sw);
738int tb_switch_add(struct tb_switch *sw);
739void tb_switch_remove(struct tb_switch *sw);
740void tb_switch_suspend(struct tb_switch *sw, bool runtime);
741int tb_switch_resume(struct tb_switch *sw);
742int tb_switch_reset(struct tb_switch *sw);
743void tb_sw_set_unplugged(struct tb_switch *sw);
744struct tb_port *tb_switch_find_port(struct tb_switch *sw,
745 enum tb_port_type type);
746struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
747 u8 depth);
748struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
749struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
750
751
752
753
754
755
756
757
758#define tb_switch_for_each_port(sw, p) \
759 for ((p) = &(sw)->ports[1]; \
760 (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
761
762static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
763{
764 if (sw)
765 get_device(&sw->dev);
766 return sw;
767}
768
769static inline void tb_switch_put(struct tb_switch *sw)
770{
771 put_device(&sw->dev);
772}
773
774static inline bool tb_is_switch(const struct device *dev)
775{
776 return dev->type == &tb_switch_type;
777}
778
779static inline struct tb_switch *tb_to_switch(struct device *dev)
780{
781 if (tb_is_switch(dev))
782 return container_of(dev, struct tb_switch, dev);
783 return NULL;
784}
785
786static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
787{
788 return tb_to_switch(sw->dev.parent);
789}
790
791static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
792{
793 return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
794 sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
795}
796
797static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
798{
799 return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
800 sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
801}
802
803static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
804{
805 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
806 switch (sw->config.device_id) {
807 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
808 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
809 return true;
810 }
811 }
812 return false;
813}
814
815static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
816{
817 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
818 switch (sw->config.device_id) {
819 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
820 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
821 return true;
822 }
823 }
824 return false;
825}
826
827static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
828{
829 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
830 switch (sw->config.device_id) {
831 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
832 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
833 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
834 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
835 return true;
836 }
837 }
838 return false;
839}
840
841static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
842{
843 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
844 switch (sw->config.device_id) {
845 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
846 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
847 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
848 return true;
849 }
850 }
851 return false;
852}
853
854
855
856
857
858
859
860static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
861{
862 return sw->config.thunderbolt_version == USB4_VERSION_1_0;
863}
864
865
866
867
868
869
870
871
872
873
874static inline bool tb_switch_is_icm(const struct tb_switch *sw)
875{
876 return !sw->config.enabled;
877}
878
879int tb_switch_lane_bonding_enable(struct tb_switch *sw);
880void tb_switch_lane_bonding_disable(struct tb_switch *sw);
881int tb_switch_configure_link(struct tb_switch *sw);
882void tb_switch_unconfigure_link(struct tb_switch *sw);
883
884bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
885int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
886void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
887
888int tb_switch_tmu_init(struct tb_switch *sw);
889int tb_switch_tmu_post_time(struct tb_switch *sw);
890int tb_switch_tmu_disable(struct tb_switch *sw);
891int tb_switch_tmu_enable(struct tb_switch *sw);
892
893static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw)
894{
895 return sw->tmu.rate == TB_SWITCH_TMU_RATE_HIFI &&
896 !sw->tmu.unidirectional;
897}
898
899int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
900int tb_port_add_nfc_credits(struct tb_port *port, int credits);
901int tb_port_clear_counter(struct tb_port *port, int counter);
902int tb_port_unlock(struct tb_port *port);
903int tb_port_enable(struct tb_port *port);
904int tb_port_disable(struct tb_port *port);
905int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
906void tb_port_release_in_hopid(struct tb_port *port, int hopid);
907int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
908void tb_port_release_out_hopid(struct tb_port *port, int hopid);
909struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
910 struct tb_port *prev);
911
912static inline bool tb_port_use_credit_allocation(const struct tb_port *port)
913{
914 return tb_port_is_null(port) && port->sw->credit_allocation;
915}
916
917
918
919
920
921
922
923
924
925#define tb_for_each_port_on_path(src, dst, p) \
926 for ((p) = tb_next_port_on_path((src), (dst), NULL); (p); \
927 (p) = tb_next_port_on_path((src), (dst), (p)))
928
929int tb_port_get_link_speed(struct tb_port *port);
930int tb_port_get_link_width(struct tb_port *port);
931int tb_port_state(struct tb_port *port);
932int tb_port_lane_bonding_enable(struct tb_port *port);
933void tb_port_lane_bonding_disable(struct tb_port *port);
934int tb_port_wait_for_link_width(struct tb_port *port, int width,
935 int timeout_msec);
936int tb_port_update_credits(struct tb_port *port);
937
938int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
939int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
940int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
941int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
942int tb_port_next_cap(struct tb_port *port, unsigned int offset);
943bool tb_port_is_enabled(struct tb_port *port);
944
945bool tb_usb3_port_is_enabled(struct tb_port *port);
946int tb_usb3_port_enable(struct tb_port *port, bool enable);
947
948bool tb_pci_port_is_enabled(struct tb_port *port);
949int tb_pci_port_enable(struct tb_port *port, bool enable);
950
951int tb_dp_port_hpd_is_active(struct tb_port *port);
952int tb_dp_port_hpd_clear(struct tb_port *port);
953int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
954 unsigned int aux_tx, unsigned int aux_rx);
955bool tb_dp_port_is_enabled(struct tb_port *port);
956int tb_dp_port_enable(struct tb_port *port, bool enable);
957
958struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
959 struct tb_port *dst, int dst_hopid,
960 struct tb_port **last, const char *name);
961struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
962 struct tb_port *dst, int dst_hopid, int link_nr,
963 const char *name);
964void tb_path_free(struct tb_path *path);
965int tb_path_activate(struct tb_path *path);
966void tb_path_deactivate(struct tb_path *path);
967bool tb_path_is_invalid(struct tb_path *path);
968bool tb_path_port_on_path(const struct tb_path *path,
969 const struct tb_port *port);
970
971
972
973
974
975
976
977
978#define tb_path_for_each_hop(path, hop) \
979 for ((hop) = &(path)->hops[0]; \
980 (hop) <= &(path)->hops[(path)->path_length - 1]; (hop)++)
981
982int tb_drom_read(struct tb_switch *sw);
983int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
984
985int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
986int tb_lc_configure_port(struct tb_port *port);
987void tb_lc_unconfigure_port(struct tb_port *port);
988int tb_lc_configure_xdomain(struct tb_port *port);
989void tb_lc_unconfigure_xdomain(struct tb_port *port);
990int tb_lc_start_lane_initialization(struct tb_port *port);
991int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags);
992int tb_lc_set_sleep(struct tb_switch *sw);
993bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
994bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
995int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
996int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
997int tb_lc_force_power(struct tb_switch *sw);
998
999static inline int tb_route_length(u64 route)
1000{
1001 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
1002}
1003
1004
1005
1006
1007
1008
1009
1010
1011static inline u64 tb_downstream_route(struct tb_port *port)
1012{
1013 return tb_route(port->sw)
1014 | ((u64) port->port << (port->sw->config.depth * 8));
1015}
1016
1017bool tb_is_xdomain_enabled(void);
1018bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1019 const void *buf, size_t size);
1020struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1021 u64 route, const uuid_t *local_uuid,
1022 const uuid_t *remote_uuid);
1023void tb_xdomain_add(struct tb_xdomain *xd);
1024void tb_xdomain_remove(struct tb_xdomain *xd);
1025struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1026 u8 depth);
1027
1028int tb_retimer_scan(struct tb_port *port, bool add);
1029void tb_retimer_remove_all(struct tb_port *port);
1030
1031static inline bool tb_is_retimer(const struct device *dev)
1032{
1033 return dev->type == &tb_retimer_type;
1034}
1035
1036static inline struct tb_retimer *tb_to_retimer(struct device *dev)
1037{
1038 if (tb_is_retimer(dev))
1039 return container_of(dev, struct tb_retimer, dev);
1040 return NULL;
1041}
1042
1043int usb4_switch_setup(struct tb_switch *sw);
1044int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
1045int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
1046 size_t size);
1047bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
1048int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags);
1049int usb4_switch_set_sleep(struct tb_switch *sw);
1050int usb4_switch_nvm_sector_size(struct tb_switch *sw);
1051int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
1052 size_t size);
1053int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address);
1054int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
1055 const void *buf, size_t size);
1056int usb4_switch_nvm_authenticate(struct tb_switch *sw);
1057int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status);
1058int usb4_switch_credits_init(struct tb_switch *sw);
1059bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
1060int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1061int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1062struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
1063 const struct tb_port *port);
1064struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
1065 const struct tb_port *port);
1066int usb4_switch_add_ports(struct tb_switch *sw);
1067void usb4_switch_remove_ports(struct tb_switch *sw);
1068
1069int usb4_port_unlock(struct tb_port *port);
1070int usb4_port_configure(struct tb_port *port);
1071void usb4_port_unconfigure(struct tb_port *port);
1072int usb4_port_configure_xdomain(struct tb_port *port);
1073void usb4_port_unconfigure_xdomain(struct tb_port *port);
1074int usb4_port_router_offline(struct tb_port *port);
1075int usb4_port_router_online(struct tb_port *port);
1076int usb4_port_enumerate_retimers(struct tb_port *port);
1077
1078int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index);
1079int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1080 u8 size);
1081int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1082 const void *buf, u8 size);
1083int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
1084int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
1085int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1086 unsigned int address);
1087int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
1088 unsigned int address, const void *buf,
1089 size_t size);
1090int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
1091int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1092 u32 *status);
1093int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1094 unsigned int address, void *buf, size_t size);
1095
1096int usb4_usb3_port_max_link_rate(struct tb_port *port);
1097int usb4_usb3_port_actual_link_rate(struct tb_port *port);
1098int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1099 int *downstream_bw);
1100int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1101 int *downstream_bw);
1102int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1103 int *downstream_bw);
1104
1105static inline bool tb_is_usb4_port_device(const struct device *dev)
1106{
1107 return dev->type == &usb4_port_device_type;
1108}
1109
1110static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
1111{
1112 if (tb_is_usb4_port_device(dev))
1113 return container_of(dev, struct usb4_port, dev);
1114 return NULL;
1115}
1116
1117struct usb4_port *usb4_port_device_add(struct tb_port *port);
1118void usb4_port_device_remove(struct usb4_port *usb4);
1119int usb4_port_device_resume(struct usb4_port *usb4);
1120
1121
1122#define QUIRK_FORCE_POWER_LINK_CONTROLLER BIT(0)
1123
1124void tb_check_quirks(struct tb_switch *sw);
1125
1126#ifdef CONFIG_ACPI
1127void tb_acpi_add_links(struct tb_nhi *nhi);
1128
1129bool tb_acpi_is_native(void);
1130bool tb_acpi_may_tunnel_usb3(void);
1131bool tb_acpi_may_tunnel_dp(void);
1132bool tb_acpi_may_tunnel_pcie(void);
1133bool tb_acpi_is_xdomain_allowed(void);
1134
1135int tb_acpi_init(void);
1136void tb_acpi_exit(void);
1137int tb_acpi_power_on_retimers(struct tb_port *port);
1138int tb_acpi_power_off_retimers(struct tb_port *port);
1139#else
1140static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
1141
1142static inline bool tb_acpi_is_native(void) { return true; }
1143static inline bool tb_acpi_may_tunnel_usb3(void) { return true; }
1144static inline bool tb_acpi_may_tunnel_dp(void) { return true; }
1145static inline bool tb_acpi_may_tunnel_pcie(void) { return true; }
1146static inline bool tb_acpi_is_xdomain_allowed(void) { return true; }
1147
1148static inline int tb_acpi_init(void) { return 0; }
1149static inline void tb_acpi_exit(void) { }
1150static inline int tb_acpi_power_on_retimers(struct tb_port *port) { return 0; }
1151static inline int tb_acpi_power_off_retimers(struct tb_port *port) { return 0; }
1152#endif
1153
1154#ifdef CONFIG_DEBUG_FS
1155void tb_debugfs_init(void);
1156void tb_debugfs_exit(void);
1157void tb_switch_debugfs_init(struct tb_switch *sw);
1158void tb_switch_debugfs_remove(struct tb_switch *sw);
1159void tb_service_debugfs_init(struct tb_service *svc);
1160void tb_service_debugfs_remove(struct tb_service *svc);
1161#else
1162static inline void tb_debugfs_init(void) { }
1163static inline void tb_debugfs_exit(void) { }
1164static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
1165static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
1166static inline void tb_service_debugfs_init(struct tb_service *svc) { }
1167static inline void tb_service_debugfs_remove(struct tb_service *svc) { }
1168#endif
1169
1170#ifdef CONFIG_USB4_KUNIT_TEST
1171int tb_test_init(void);
1172void tb_test_exit(void);
1173#else
1174static inline int tb_test_init(void) { return 0; }
1175static inline void tb_test_exit(void) { }
1176#endif
1177
1178#endif
1179