blob: dd1c0498a8eee588955c5e0be69c6011681a3b76 [file] [log] [blame]
Mika Westerbergb0407982019-12-17 15:33:40 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB4 specific functionality
4 *
5 * Copyright (C) 2019, Intel Corporation
6 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * Rajmohan Mani <rajmohan.mani@intel.com>
8 */
9
10#include <linux/delay.h>
11#include <linux/ktime.h>
12
13#include "tb.h"
14
15#define USB4_DATA_DWORDS 16
16#define USB4_DATA_RETRIES 3
17
18enum usb4_switch_op {
19 USB4_SWITCH_OP_QUERY_DP_RESOURCE = 0x10,
20 USB4_SWITCH_OP_ALLOC_DP_RESOURCE = 0x11,
21 USB4_SWITCH_OP_DEALLOC_DP_RESOURCE = 0x12,
22 USB4_SWITCH_OP_NVM_WRITE = 0x20,
23 USB4_SWITCH_OP_NVM_AUTH = 0x21,
24 USB4_SWITCH_OP_NVM_READ = 0x22,
25 USB4_SWITCH_OP_NVM_SET_OFFSET = 0x23,
26 USB4_SWITCH_OP_DROM_READ = 0x24,
27 USB4_SWITCH_OP_NVM_SECTOR_SIZE = 0x25,
28};
29
30#define USB4_NVM_READ_OFFSET_MASK GENMASK(23, 2)
31#define USB4_NVM_READ_OFFSET_SHIFT 2
32#define USB4_NVM_READ_LENGTH_MASK GENMASK(27, 24)
33#define USB4_NVM_READ_LENGTH_SHIFT 24
34
35#define USB4_NVM_SET_OFFSET_MASK USB4_NVM_READ_OFFSET_MASK
36#define USB4_NVM_SET_OFFSET_SHIFT USB4_NVM_READ_OFFSET_SHIFT
37
38#define USB4_DROM_ADDRESS_MASK GENMASK(14, 2)
39#define USB4_DROM_ADDRESS_SHIFT 2
40#define USB4_DROM_SIZE_MASK GENMASK(19, 15)
41#define USB4_DROM_SIZE_SHIFT 15
42
43#define USB4_NVM_SECTOR_SIZE_MASK GENMASK(23, 0)
44
45typedef int (*read_block_fn)(struct tb_switch *, unsigned int, void *, size_t);
46typedef int (*write_block_fn)(struct tb_switch *, const void *, size_t);
47
48static int usb4_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
49 u32 value, int timeout_msec)
50{
51 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
52
53 do {
54 u32 val;
55 int ret;
56
57 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
58 if (ret)
59 return ret;
60
61 if ((val & bit) == value)
62 return 0;
63
64 usleep_range(50, 100);
65 } while (ktime_before(ktime_get(), timeout));
66
67 return -ETIMEDOUT;
68}
69
70static int usb4_switch_op_read_data(struct tb_switch *sw, void *data,
71 size_t dwords)
72{
73 if (dwords > USB4_DATA_DWORDS)
74 return -EINVAL;
75
76 return tb_sw_read(sw, data, TB_CFG_SWITCH, ROUTER_CS_9, dwords);
77}
78
79static int usb4_switch_op_write_data(struct tb_switch *sw, const void *data,
80 size_t dwords)
81{
82 if (dwords > USB4_DATA_DWORDS)
83 return -EINVAL;
84
85 return tb_sw_write(sw, data, TB_CFG_SWITCH, ROUTER_CS_9, dwords);
86}
87
88static int usb4_switch_op_read_metadata(struct tb_switch *sw, u32 *metadata)
89{
90 return tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
91}
92
93static int usb4_switch_op_write_metadata(struct tb_switch *sw, u32 metadata)
94{
95 return tb_sw_write(sw, &metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
96}
97
98static int usb4_switch_do_read_data(struct tb_switch *sw, u16 address,
99 void *buf, size_t size, read_block_fn read_block)
100{
101 unsigned int retries = USB4_DATA_RETRIES;
102 unsigned int offset;
103
104 offset = address & 3;
105 address = address & ~3;
106
107 do {
108 size_t nbytes = min_t(size_t, size, USB4_DATA_DWORDS * 4);
109 unsigned int dwaddress, dwords;
110 u8 data[USB4_DATA_DWORDS * 4];
111 int ret;
112
113 dwaddress = address / 4;
114 dwords = ALIGN(nbytes, 4) / 4;
115
116 ret = read_block(sw, dwaddress, data, dwords);
117 if (ret) {
118 if (ret == -ETIMEDOUT) {
119 if (retries--)
120 continue;
121 ret = -EIO;
122 }
123 return ret;
124 }
125
126 memcpy(buf, data + offset, nbytes);
127
128 size -= nbytes;
129 address += nbytes;
130 buf += nbytes;
131 } while (size > 0);
132
133 return 0;
134}
135
136static int usb4_switch_do_write_data(struct tb_switch *sw, u16 address,
137 const void *buf, size_t size, write_block_fn write_next_block)
138{
139 unsigned int retries = USB4_DATA_RETRIES;
140 unsigned int offset;
141
142 offset = address & 3;
143 address = address & ~3;
144
145 do {
146 u32 nbytes = min_t(u32, size, USB4_DATA_DWORDS * 4);
147 u8 data[USB4_DATA_DWORDS * 4];
148 int ret;
149
150 memcpy(data + offset, buf, nbytes);
151
152 ret = write_next_block(sw, data, nbytes / 4);
153 if (ret) {
154 if (ret == -ETIMEDOUT) {
155 if (retries--)
156 continue;
157 ret = -EIO;
158 }
159 return ret;
160 }
161
162 size -= nbytes;
163 address += nbytes;
164 buf += nbytes;
165 } while (size > 0);
166
167 return 0;
168}
169
170static int usb4_switch_op(struct tb_switch *sw, u16 opcode, u8 *status)
171{
172 u32 val;
173 int ret;
174
175 val = opcode | ROUTER_CS_26_OV;
176 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
177 if (ret)
178 return ret;
179
180 ret = usb4_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
181 if (ret)
182 return ret;
183
184 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
Mika Westerbergc3bf9932020-04-09 10:18:10 +0300185 if (ret)
186 return ret;
187
Mika Westerbergb0407982019-12-17 15:33:40 +0300188 if (val & ROUTER_CS_26_ONS)
189 return -EOPNOTSUPP;
190
191 *status = (val & ROUTER_CS_26_STATUS_MASK) >> ROUTER_CS_26_STATUS_SHIFT;
192 return 0;
193}
194
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200195static bool link_is_usb4(struct tb_port *port)
196{
197 u32 val;
198
199 if (!port->cap_usb4)
200 return false;
201
202 if (tb_port_read(port, &val, TB_CFG_PORT,
203 port->cap_usb4 + PORT_CS_18, 1))
204 return false;
205
206 return !(val & PORT_CS_18_TCM);
207}
208
Mika Westerbergb0407982019-12-17 15:33:40 +0300209/**
210 * usb4_switch_setup() - Additional setup for USB4 device
211 * @sw: USB4 router to setup
212 *
213 * USB4 routers need additional settings in order to enable all the
214 * tunneling. This function enables USB and PCIe tunneling if it can be
215 * enabled (e.g the parent switch also supports them). If USB tunneling
216 * is not available for some reason (like that there is Thunderbolt 3
217 * switch upstream) then the internal xHCI controller is enabled
218 * instead.
219 */
220int usb4_switch_setup(struct tb_switch *sw)
221{
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200222 struct tb_port *downstream_port;
Mika Westerbergb0407982019-12-17 15:33:40 +0300223 struct tb_switch *parent;
224 bool tbt3, xhci;
225 u32 val = 0;
226 int ret;
227
228 if (!tb_route(sw))
229 return 0;
230
231 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
232 if (ret)
233 return ret;
234
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200235 parent = tb_switch_parent(sw);
236 downstream_port = tb_port_at(tb_route(sw), parent);
237 sw->link_usb4 = link_is_usb4(downstream_port);
238 tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT3");
239
Mika Westerbergb0407982019-12-17 15:33:40 +0300240 xhci = val & ROUTER_CS_6_HCI;
241 tbt3 = !(val & ROUTER_CS_6_TNS);
242
243 tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
244 tbt3 ? "yes" : "no", xhci ? "yes" : "no");
245
246 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
247 if (ret)
248 return ret;
249
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200250 if (sw->link_usb4 && tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
Rajmohan Manie6f81852019-12-17 15:33:44 +0300251 val |= ROUTER_CS_5_UTO;
252 xhci = false;
253 }
254
Mika Westerbergb0407982019-12-17 15:33:40 +0300255 /* Only enable PCIe tunneling if the parent router supports it */
256 if (tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
257 val |= ROUTER_CS_5_PTO;
Rajmohan Manie6f81852019-12-17 15:33:44 +0300258 /*
259 * xHCI can be enabled if PCIe tunneling is supported
260 * and the parent does not have any USB3 dowstream
261 * adapters (so we cannot do USB 3.x tunneling).
262 */
Mika Westerbergc7a7ac82020-01-08 15:53:16 +0300263 if (xhci)
Mika Westerbergb0407982019-12-17 15:33:40 +0300264 val |= ROUTER_CS_5_HCO;
265 }
266
267 /* TBT3 supported by the CM */
268 val |= ROUTER_CS_5_C3S;
269 /* Tunneling configuration is ready now */
270 val |= ROUTER_CS_5_CV;
271
272 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
273 if (ret)
274 return ret;
275
276 return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
277 ROUTER_CS_6_CR, 50);
278}
279
280/**
281 * usb4_switch_read_uid() - Read UID from USB4 router
282 * @sw: USB4 router
Mika Westerberg21d78d82020-02-14 15:16:38 +0300283 * @uid: UID is stored here
Mika Westerbergb0407982019-12-17 15:33:40 +0300284 *
285 * Reads 64-bit UID from USB4 router config space.
286 */
287int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
288{
289 return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
290}
291
292static int usb4_switch_drom_read_block(struct tb_switch *sw,
293 unsigned int dwaddress, void *buf,
294 size_t dwords)
295{
296 u8 status = 0;
297 u32 metadata;
298 int ret;
299
300 metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
301 metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
302 USB4_DROM_ADDRESS_MASK;
303
304 ret = usb4_switch_op_write_metadata(sw, metadata);
305 if (ret)
306 return ret;
307
308 ret = usb4_switch_op(sw, USB4_SWITCH_OP_DROM_READ, &status);
309 if (ret)
310 return ret;
311
312 if (status)
313 return -EIO;
314
315 return usb4_switch_op_read_data(sw, buf, dwords);
316}
317
318/**
319 * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
320 * @sw: USB4 router
Mika Westerberg21d78d82020-02-14 15:16:38 +0300321 * @address: Byte address inside DROM to start reading
322 * @buf: Buffer where the DROM content is stored
323 * @size: Number of bytes to read from DROM
Mika Westerbergb0407982019-12-17 15:33:40 +0300324 *
325 * Uses USB4 router operations to read router DROM. For devices this
326 * should always work but for hosts it may return %-EOPNOTSUPP in which
327 * case the host router does not have DROM.
328 */
329int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
330 size_t size)
331{
332 return usb4_switch_do_read_data(sw, address, buf, size,
333 usb4_switch_drom_read_block);
334}
335
336static int usb4_set_port_configured(struct tb_port *port, bool configured)
337{
338 int ret;
339 u32 val;
340
341 ret = tb_port_read(port, &val, TB_CFG_PORT,
342 port->cap_usb4 + PORT_CS_19, 1);
343 if (ret)
344 return ret;
345
346 if (configured)
347 val |= PORT_CS_19_PC;
348 else
349 val &= ~PORT_CS_19_PC;
350
351 return tb_port_write(port, &val, TB_CFG_PORT,
352 port->cap_usb4 + PORT_CS_19, 1);
353}
354
355/**
356 * usb4_switch_configure_link() - Set upstream USB4 link configured
357 * @sw: USB4 router
358 *
359 * Sets the upstream USB4 link to be configured for power management
360 * purposes.
361 */
362int usb4_switch_configure_link(struct tb_switch *sw)
363{
364 struct tb_port *up;
365
366 if (!tb_route(sw))
367 return 0;
368
369 up = tb_upstream_port(sw);
370 return usb4_set_port_configured(up, true);
371}
372
373/**
374 * usb4_switch_unconfigure_link() - Un-set upstream USB4 link configuration
375 * @sw: USB4 router
376 *
377 * Reverse of usb4_switch_configure_link().
378 */
379void usb4_switch_unconfigure_link(struct tb_switch *sw)
380{
381 struct tb_port *up;
382
383 if (sw->is_unplugged || !tb_route(sw))
384 return;
385
386 up = tb_upstream_port(sw);
387 usb4_set_port_configured(up, false);
388}
389
390/**
391 * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
392 * @sw: USB4 router
393 *
394 * Checks whether conditions are met so that lane bonding can be
395 * established with the upstream router. Call only for device routers.
396 */
397bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
398{
399 struct tb_port *up;
400 int ret;
401 u32 val;
402
403 up = tb_upstream_port(sw);
404 ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
405 if (ret)
406 return false;
407
408 return !!(val & PORT_CS_18_BE);
409}
410
411/**
412 * usb4_switch_set_sleep() - Prepare the router to enter sleep
413 * @sw: USB4 router
414 *
415 * Enables wakes and sets sleep bit for the router. Returns when the
416 * router sleep ready bit has been asserted.
417 */
418int usb4_switch_set_sleep(struct tb_switch *sw)
419{
420 int ret;
421 u32 val;
422
423 /* Set sleep bit and wait for sleep ready to be asserted */
424 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
425 if (ret)
426 return ret;
427
428 val |= ROUTER_CS_5_SLP;
429
430 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
431 if (ret)
432 return ret;
433
434 return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
435 ROUTER_CS_6_SLPR, 500);
436}
437
438/**
439 * usb4_switch_nvm_sector_size() - Return router NVM sector size
440 * @sw: USB4 router
441 *
442 * If the router supports NVM operations this function returns the NVM
443 * sector size in bytes. If NVM operations are not supported returns
444 * %-EOPNOTSUPP.
445 */
446int usb4_switch_nvm_sector_size(struct tb_switch *sw)
447{
448 u32 metadata;
449 u8 status;
450 int ret;
451
452 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &status);
453 if (ret)
454 return ret;
455
456 if (status)
457 return status == 0x2 ? -EOPNOTSUPP : -EIO;
458
459 ret = usb4_switch_op_read_metadata(sw, &metadata);
460 if (ret)
461 return ret;
462
463 return metadata & USB4_NVM_SECTOR_SIZE_MASK;
464}
465
466static int usb4_switch_nvm_read_block(struct tb_switch *sw,
467 unsigned int dwaddress, void *buf, size_t dwords)
468{
469 u8 status = 0;
470 u32 metadata;
471 int ret;
472
473 metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
474 USB4_NVM_READ_LENGTH_MASK;
475 metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
476 USB4_NVM_READ_OFFSET_MASK;
477
478 ret = usb4_switch_op_write_metadata(sw, metadata);
479 if (ret)
480 return ret;
481
482 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_READ, &status);
483 if (ret)
484 return ret;
485
486 if (status)
487 return -EIO;
488
489 return usb4_switch_op_read_data(sw, buf, dwords);
490}
491
492/**
493 * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
494 * @sw: USB4 router
495 * @address: Starting address in bytes
496 * @buf: Read data is placed here
497 * @size: How many bytes to read
498 *
499 * Reads NVM contents of the router. If NVM is not supported returns
500 * %-EOPNOTSUPP.
501 */
502int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
503 size_t size)
504{
505 return usb4_switch_do_read_data(sw, address, buf, size,
506 usb4_switch_nvm_read_block);
507}
508
509static int usb4_switch_nvm_set_offset(struct tb_switch *sw,
510 unsigned int address)
511{
512 u32 metadata, dwaddress;
513 u8 status = 0;
514 int ret;
515
516 dwaddress = address / 4;
517 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
518 USB4_NVM_SET_OFFSET_MASK;
519
520 ret = usb4_switch_op_write_metadata(sw, metadata);
521 if (ret)
522 return ret;
523
524 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &status);
525 if (ret)
526 return ret;
527
528 return status ? -EIO : 0;
529}
530
531static int usb4_switch_nvm_write_next_block(struct tb_switch *sw,
532 const void *buf, size_t dwords)
533{
534 u8 status;
535 int ret;
536
537 ret = usb4_switch_op_write_data(sw, buf, dwords);
538 if (ret)
539 return ret;
540
541 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_WRITE, &status);
542 if (ret)
543 return ret;
544
545 return status ? -EIO : 0;
546}
547
548/**
549 * usb4_switch_nvm_write() - Write to the router NVM
550 * @sw: USB4 router
551 * @address: Start address where to write in bytes
552 * @buf: Pointer to the data to write
553 * @size: Size of @buf in bytes
554 *
555 * Writes @buf to the router NVM using USB4 router operations. If NVM
556 * write is not supported returns %-EOPNOTSUPP.
557 */
558int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
559 const void *buf, size_t size)
560{
561 int ret;
562
563 ret = usb4_switch_nvm_set_offset(sw, address);
564 if (ret)
565 return ret;
566
567 return usb4_switch_do_write_data(sw, address, buf, size,
568 usb4_switch_nvm_write_next_block);
569}
570
571/**
572 * usb4_switch_nvm_authenticate() - Authenticate new NVM
573 * @sw: USB4 router
574 *
575 * After the new NVM has been written via usb4_switch_nvm_write(), this
576 * function triggers NVM authentication process. If the authentication
577 * is successful the router is power cycled and the new NVM starts
578 * running. In case of failure returns negative errno.
579 */
580int usb4_switch_nvm_authenticate(struct tb_switch *sw)
581{
582 u8 status = 0;
583 int ret;
584
585 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, &status);
586 if (ret)
587 return ret;
588
589 switch (status) {
590 case 0x0:
591 tb_sw_dbg(sw, "NVM authentication successful\n");
592 return 0;
593 case 0x1:
594 return -EINVAL;
595 case 0x2:
596 return -EAGAIN;
597 case 0x3:
598 return -EOPNOTSUPP;
599 default:
600 return -EIO;
601 }
602}
603
604/**
605 * usb4_switch_query_dp_resource() - Query availability of DP IN resource
606 * @sw: USB4 router
607 * @in: DP IN adapter
608 *
609 * For DP tunneling this function can be used to query availability of
610 * DP IN resource. Returns true if the resource is available for DP
611 * tunneling, false otherwise.
612 */
613bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
614{
615 u8 status;
616 int ret;
617
618 ret = usb4_switch_op_write_metadata(sw, in->port);
619 if (ret)
620 return false;
621
622 ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &status);
623 /*
624 * If DP resource allocation is not supported assume it is
625 * always available.
626 */
627 if (ret == -EOPNOTSUPP)
628 return true;
629 else if (ret)
630 return false;
631
632 return !status;
633}
634
635/**
636 * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
637 * @sw: USB4 router
638 * @in: DP IN adapter
639 *
640 * Allocates DP IN resource for DP tunneling using USB4 router
641 * operations. If the resource was allocated returns %0. Otherwise
642 * returns negative errno, in particular %-EBUSY if the resource is
643 * already allocated.
644 */
645int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
646{
647 u8 status;
648 int ret;
649
650 ret = usb4_switch_op_write_metadata(sw, in->port);
651 if (ret)
652 return ret;
653
654 ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &status);
655 if (ret == -EOPNOTSUPP)
656 return 0;
657 else if (ret)
658 return ret;
659
660 return status ? -EBUSY : 0;
661}
662
663/**
664 * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
665 * @sw: USB4 router
666 * @in: DP IN adapter
667 *
668 * Releases the previously allocated DP IN resource.
669 */
670int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
671{
672 u8 status;
673 int ret;
674
675 ret = usb4_switch_op_write_metadata(sw, in->port);
676 if (ret)
677 return ret;
678
679 ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &status);
680 if (ret == -EOPNOTSUPP)
681 return 0;
682 else if (ret)
683 return ret;
684
685 return status ? -EIO : 0;
686}
687
688static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
689{
690 struct tb_port *p;
691 int usb4_idx = 0;
692
693 /* Assume port is primary */
694 tb_switch_for_each_port(sw, p) {
695 if (!tb_port_is_null(p))
696 continue;
697 if (tb_is_upstream_port(p))
698 continue;
699 if (!p->link_nr) {
700 if (p == port)
701 break;
702 usb4_idx++;
703 }
704 }
705
706 return usb4_idx;
707}
708
709/**
710 * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
711 * @sw: USB4 router
712 * @port: USB4 port
713 *
714 * USB4 routers have direct mapping between USB4 ports and PCIe
715 * downstream adapters where the PCIe topology is extended. This
716 * function returns the corresponding downstream PCIe adapter or %NULL
717 * if no such mapping was possible.
718 */
719struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
720 const struct tb_port *port)
721{
722 int usb4_idx = usb4_port_idx(sw, port);
723 struct tb_port *p;
724 int pcie_idx = 0;
725
726 /* Find PCIe down port matching usb4_port */
727 tb_switch_for_each_port(sw, p) {
728 if (!tb_port_is_pcie_down(p))
729 continue;
730
Mika Westerberg9cac51a2020-03-11 16:12:50 +0300731 if (pcie_idx == usb4_idx)
Mika Westerbergb0407982019-12-17 15:33:40 +0300732 return p;
733
734 pcie_idx++;
735 }
736
737 return NULL;
738}
739
740/**
Rajmohan Manie6f81852019-12-17 15:33:44 +0300741 * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
742 * @sw: USB4 router
743 * @port: USB4 port
744 *
745 * USB4 routers have direct mapping between USB4 ports and USB 3.x
746 * downstream adapters where the USB 3.x topology is extended. This
747 * function returns the corresponding downstream USB 3.x adapter or
748 * %NULL if no such mapping was possible.
749 */
750struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
751 const struct tb_port *port)
752{
753 int usb4_idx = usb4_port_idx(sw, port);
754 struct tb_port *p;
755 int usb_idx = 0;
756
757 /* Find USB3 down port matching usb4_port */
758 tb_switch_for_each_port(sw, p) {
759 if (!tb_port_is_usb3_down(p))
760 continue;
761
Mika Westerberg77cfa402020-03-11 16:00:46 +0300762 if (usb_idx == usb4_idx)
Rajmohan Manie6f81852019-12-17 15:33:44 +0300763 return p;
764
765 usb_idx++;
766 }
767
768 return NULL;
769}
770
771/**
Mika Westerbergb0407982019-12-17 15:33:40 +0300772 * usb4_port_unlock() - Unlock USB4 downstream port
773 * @port: USB4 port to unlock
774 *
775 * Unlocks USB4 downstream port so that the connection manager can
776 * access the router below this port.
777 */
778int usb4_port_unlock(struct tb_port *port)
779{
780 int ret;
781 u32 val;
782
783 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
784 if (ret)
785 return ret;
786
787 val &= ~ADP_CS_4_LCK;
788 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
789}