blob: 142c7244bdb12fa5f6ddd876ea2a82450288c481 [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
Mika Westerberg7e728462020-02-14 19:23:03 +020045typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
46typedef int (*write_block_fn)(void *, const void *, size_t);
Mika Westerbergb0407982019-12-17 15:33:40 +030047
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
Mika Westerberg7e728462020-02-14 19:23:03 +020098static int usb4_do_read_data(u16 address, void *buf, size_t size,
99 read_block_fn read_block, void *read_block_data)
Mika Westerbergb0407982019-12-17 15:33:40 +0300100{
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
Mika Westerberg7e728462020-02-14 19:23:03 +0200116 ret = read_block(read_block_data, dwaddress, data, dwords);
Mika Westerbergb0407982019-12-17 15:33:40 +0300117 if (ret) {
Mika Westerberg6bfe3342020-02-14 19:25:34 +0200118 if (ret != -ENODEV && retries--)
119 continue;
Mika Westerbergb0407982019-12-17 15:33:40 +0300120 return ret;
121 }
122
123 memcpy(buf, data + offset, nbytes);
124
125 size -= nbytes;
126 address += nbytes;
127 buf += nbytes;
128 } while (size > 0);
129
130 return 0;
131}
132
Mika Westerberg7e728462020-02-14 19:23:03 +0200133static int usb4_do_write_data(unsigned int address, const void *buf, size_t size,
134 write_block_fn write_next_block, void *write_block_data)
Mika Westerbergb0407982019-12-17 15:33:40 +0300135{
136 unsigned int retries = USB4_DATA_RETRIES;
137 unsigned int offset;
138
139 offset = address & 3;
140 address = address & ~3;
141
142 do {
143 u32 nbytes = min_t(u32, size, USB4_DATA_DWORDS * 4);
144 u8 data[USB4_DATA_DWORDS * 4];
145 int ret;
146
147 memcpy(data + offset, buf, nbytes);
148
Mika Westerberg7e728462020-02-14 19:23:03 +0200149 ret = write_next_block(write_block_data, data, nbytes / 4);
Mika Westerbergb0407982019-12-17 15:33:40 +0300150 if (ret) {
151 if (ret == -ETIMEDOUT) {
152 if (retries--)
153 continue;
154 ret = -EIO;
155 }
156 return ret;
157 }
158
159 size -= nbytes;
160 address += nbytes;
161 buf += nbytes;
162 } while (size > 0);
163
164 return 0;
165}
166
167static int usb4_switch_op(struct tb_switch *sw, u16 opcode, u8 *status)
168{
169 u32 val;
170 int ret;
171
172 val = opcode | ROUTER_CS_26_OV;
173 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
174 if (ret)
175 return ret;
176
177 ret = usb4_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
178 if (ret)
179 return ret;
180
181 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
Mika Westerbergc3bf9932020-04-09 10:18:10 +0300182 if (ret)
183 return ret;
184
Mika Westerbergb0407982019-12-17 15:33:40 +0300185 if (val & ROUTER_CS_26_ONS)
186 return -EOPNOTSUPP;
187
188 *status = (val & ROUTER_CS_26_STATUS_MASK) >> ROUTER_CS_26_STATUS_SHIFT;
189 return 0;
190}
191
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200192static bool link_is_usb4(struct tb_port *port)
193{
194 u32 val;
195
196 if (!port->cap_usb4)
197 return false;
198
199 if (tb_port_read(port, &val, TB_CFG_PORT,
200 port->cap_usb4 + PORT_CS_18, 1))
201 return false;
202
203 return !(val & PORT_CS_18_TCM);
204}
205
Mika Westerbergb0407982019-12-17 15:33:40 +0300206/**
207 * usb4_switch_setup() - Additional setup for USB4 device
208 * @sw: USB4 router to setup
209 *
210 * USB4 routers need additional settings in order to enable all the
211 * tunneling. This function enables USB and PCIe tunneling if it can be
212 * enabled (e.g the parent switch also supports them). If USB tunneling
213 * is not available for some reason (like that there is Thunderbolt 3
214 * switch upstream) then the internal xHCI controller is enabled
215 * instead.
216 */
217int usb4_switch_setup(struct tb_switch *sw)
218{
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200219 struct tb_port *downstream_port;
Mika Westerbergb0407982019-12-17 15:33:40 +0300220 struct tb_switch *parent;
221 bool tbt3, xhci;
222 u32 val = 0;
223 int ret;
224
225 if (!tb_route(sw))
226 return 0;
227
228 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
229 if (ret)
230 return ret;
231
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200232 parent = tb_switch_parent(sw);
233 downstream_port = tb_port_at(tb_route(sw), parent);
234 sw->link_usb4 = link_is_usb4(downstream_port);
235 tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT3");
236
Mika Westerbergb0407982019-12-17 15:33:40 +0300237 xhci = val & ROUTER_CS_6_HCI;
238 tbt3 = !(val & ROUTER_CS_6_TNS);
239
240 tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
241 tbt3 ? "yes" : "no", xhci ? "yes" : "no");
242
243 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
244 if (ret)
245 return ret;
246
Mika Westerbergbbcf40b2020-03-04 17:09:14 +0200247 if (sw->link_usb4 && tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
Rajmohan Manie6f81852019-12-17 15:33:44 +0300248 val |= ROUTER_CS_5_UTO;
249 xhci = false;
250 }
251
Mika Westerbergb0407982019-12-17 15:33:40 +0300252 /* Only enable PCIe tunneling if the parent router supports it */
253 if (tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
254 val |= ROUTER_CS_5_PTO;
Rajmohan Manie6f81852019-12-17 15:33:44 +0300255 /*
256 * xHCI can be enabled if PCIe tunneling is supported
257 * and the parent does not have any USB3 dowstream
258 * adapters (so we cannot do USB 3.x tunneling).
259 */
Mika Westerbergc7a7ac82020-01-08 15:53:16 +0300260 if (xhci)
Mika Westerbergb0407982019-12-17 15:33:40 +0300261 val |= ROUTER_CS_5_HCO;
262 }
263
264 /* TBT3 supported by the CM */
265 val |= ROUTER_CS_5_C3S;
266 /* Tunneling configuration is ready now */
267 val |= ROUTER_CS_5_CV;
268
269 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
270 if (ret)
271 return ret;
272
273 return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
274 ROUTER_CS_6_CR, 50);
275}
276
277/**
278 * usb4_switch_read_uid() - Read UID from USB4 router
279 * @sw: USB4 router
Mika Westerberg21d78d82020-02-14 15:16:38 +0300280 * @uid: UID is stored here
Mika Westerbergb0407982019-12-17 15:33:40 +0300281 *
282 * Reads 64-bit UID from USB4 router config space.
283 */
284int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
285{
286 return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
287}
288
Mika Westerberg7e728462020-02-14 19:23:03 +0200289static int usb4_switch_drom_read_block(void *data,
Mika Westerbergb0407982019-12-17 15:33:40 +0300290 unsigned int dwaddress, void *buf,
291 size_t dwords)
292{
Mika Westerberg7e728462020-02-14 19:23:03 +0200293 struct tb_switch *sw = data;
Mika Westerbergb0407982019-12-17 15:33:40 +0300294 u8 status = 0;
295 u32 metadata;
296 int ret;
297
298 metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
299 metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
300 USB4_DROM_ADDRESS_MASK;
301
302 ret = usb4_switch_op_write_metadata(sw, metadata);
303 if (ret)
304 return ret;
305
306 ret = usb4_switch_op(sw, USB4_SWITCH_OP_DROM_READ, &status);
307 if (ret)
308 return ret;
309
310 if (status)
311 return -EIO;
312
313 return usb4_switch_op_read_data(sw, buf, dwords);
314}
315
316/**
317 * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
318 * @sw: USB4 router
Mika Westerberg21d78d82020-02-14 15:16:38 +0300319 * @address: Byte address inside DROM to start reading
320 * @buf: Buffer where the DROM content is stored
321 * @size: Number of bytes to read from DROM
Mika Westerbergb0407982019-12-17 15:33:40 +0300322 *
323 * Uses USB4 router operations to read router DROM. For devices this
324 * should always work but for hosts it may return %-EOPNOTSUPP in which
325 * case the host router does not have DROM.
326 */
327int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
328 size_t size)
329{
Mika Westerberg7e728462020-02-14 19:23:03 +0200330 return usb4_do_read_data(address, buf, size,
331 usb4_switch_drom_read_block, sw);
Mika Westerbergb0407982019-12-17 15:33:40 +0300332}
333
334static int usb4_set_port_configured(struct tb_port *port, bool configured)
335{
336 int ret;
337 u32 val;
338
339 ret = tb_port_read(port, &val, TB_CFG_PORT,
340 port->cap_usb4 + PORT_CS_19, 1);
341 if (ret)
342 return ret;
343
344 if (configured)
345 val |= PORT_CS_19_PC;
346 else
347 val &= ~PORT_CS_19_PC;
348
349 return tb_port_write(port, &val, TB_CFG_PORT,
350 port->cap_usb4 + PORT_CS_19, 1);
351}
352
353/**
354 * usb4_switch_configure_link() - Set upstream USB4 link configured
355 * @sw: USB4 router
356 *
357 * Sets the upstream USB4 link to be configured for power management
358 * purposes.
359 */
360int usb4_switch_configure_link(struct tb_switch *sw)
361{
362 struct tb_port *up;
363
364 if (!tb_route(sw))
365 return 0;
366
367 up = tb_upstream_port(sw);
368 return usb4_set_port_configured(up, true);
369}
370
371/**
372 * usb4_switch_unconfigure_link() - Un-set upstream USB4 link configuration
373 * @sw: USB4 router
374 *
375 * Reverse of usb4_switch_configure_link().
376 */
377void usb4_switch_unconfigure_link(struct tb_switch *sw)
378{
379 struct tb_port *up;
380
381 if (sw->is_unplugged || !tb_route(sw))
382 return;
383
384 up = tb_upstream_port(sw);
385 usb4_set_port_configured(up, false);
386}
387
388/**
389 * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
390 * @sw: USB4 router
391 *
392 * Checks whether conditions are met so that lane bonding can be
393 * established with the upstream router. Call only for device routers.
394 */
395bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
396{
397 struct tb_port *up;
398 int ret;
399 u32 val;
400
401 up = tb_upstream_port(sw);
402 ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
403 if (ret)
404 return false;
405
406 return !!(val & PORT_CS_18_BE);
407}
408
409/**
410 * usb4_switch_set_sleep() - Prepare the router to enter sleep
411 * @sw: USB4 router
412 *
413 * Enables wakes and sets sleep bit for the router. Returns when the
414 * router sleep ready bit has been asserted.
415 */
416int usb4_switch_set_sleep(struct tb_switch *sw)
417{
418 int ret;
419 u32 val;
420
421 /* Set sleep bit and wait for sleep ready to be asserted */
422 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
423 if (ret)
424 return ret;
425
426 val |= ROUTER_CS_5_SLP;
427
428 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
429 if (ret)
430 return ret;
431
432 return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
433 ROUTER_CS_6_SLPR, 500);
434}
435
436/**
437 * usb4_switch_nvm_sector_size() - Return router NVM sector size
438 * @sw: USB4 router
439 *
440 * If the router supports NVM operations this function returns the NVM
441 * sector size in bytes. If NVM operations are not supported returns
442 * %-EOPNOTSUPP.
443 */
444int usb4_switch_nvm_sector_size(struct tb_switch *sw)
445{
446 u32 metadata;
447 u8 status;
448 int ret;
449
450 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &status);
451 if (ret)
452 return ret;
453
454 if (status)
455 return status == 0x2 ? -EOPNOTSUPP : -EIO;
456
457 ret = usb4_switch_op_read_metadata(sw, &metadata);
458 if (ret)
459 return ret;
460
461 return metadata & USB4_NVM_SECTOR_SIZE_MASK;
462}
463
Mika Westerberg7e728462020-02-14 19:23:03 +0200464static int usb4_switch_nvm_read_block(void *data,
Mika Westerbergb0407982019-12-17 15:33:40 +0300465 unsigned int dwaddress, void *buf, size_t dwords)
466{
Mika Westerberg7e728462020-02-14 19:23:03 +0200467 struct tb_switch *sw = data;
Mika Westerbergb0407982019-12-17 15:33:40 +0300468 u8 status = 0;
469 u32 metadata;
470 int ret;
471
472 metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
473 USB4_NVM_READ_LENGTH_MASK;
474 metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
475 USB4_NVM_READ_OFFSET_MASK;
476
477 ret = usb4_switch_op_write_metadata(sw, metadata);
478 if (ret)
479 return ret;
480
481 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_READ, &status);
482 if (ret)
483 return ret;
484
485 if (status)
486 return -EIO;
487
488 return usb4_switch_op_read_data(sw, buf, dwords);
489}
490
491/**
492 * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
493 * @sw: USB4 router
494 * @address: Starting address in bytes
495 * @buf: Read data is placed here
496 * @size: How many bytes to read
497 *
498 * Reads NVM contents of the router. If NVM is not supported returns
499 * %-EOPNOTSUPP.
500 */
501int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
502 size_t size)
503{
Mika Westerberg7e728462020-02-14 19:23:03 +0200504 return usb4_do_read_data(address, buf, size,
505 usb4_switch_nvm_read_block, sw);
Mika Westerbergb0407982019-12-17 15:33:40 +0300506}
507
508static int usb4_switch_nvm_set_offset(struct tb_switch *sw,
509 unsigned int address)
510{
511 u32 metadata, dwaddress;
512 u8 status = 0;
513 int ret;
514
515 dwaddress = address / 4;
516 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
517 USB4_NVM_SET_OFFSET_MASK;
518
519 ret = usb4_switch_op_write_metadata(sw, metadata);
520 if (ret)
521 return ret;
522
523 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &status);
524 if (ret)
525 return ret;
526
527 return status ? -EIO : 0;
528}
529
Mika Westerberg7e728462020-02-14 19:23:03 +0200530static int usb4_switch_nvm_write_next_block(void *data, const void *buf,
531 size_t dwords)
Mika Westerbergb0407982019-12-17 15:33:40 +0300532{
Mika Westerberg7e728462020-02-14 19:23:03 +0200533 struct tb_switch *sw = data;
Mika Westerbergb0407982019-12-17 15:33:40 +0300534 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
Mika Westerberg7e728462020-02-14 19:23:03 +0200567 return usb4_do_write_data(address, buf, size,
568 usb4_switch_nvm_write_next_block, sw);
Mika Westerbergb0407982019-12-17 15:33:40 +0300569}
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}
Mika Westerberg3b1d8d52020-02-21 23:14:41 +0200790
791static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
792 u32 value, int timeout_msec)
793{
794 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
795
796 do {
797 u32 val;
798 int ret;
799
800 ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
801 if (ret)
802 return ret;
803
804 if ((val & bit) == value)
805 return 0;
806
807 usleep_range(50, 100);
808 } while (ktime_before(ktime_get(), timeout));
809
810 return -ETIMEDOUT;
811}
812
813/**
814 * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate
815 * @port: USB3 adapter port
816 *
817 * Return maximum supported link rate of a USB3 adapter in Mb/s.
818 * Negative errno in case of error.
819 */
820int usb4_usb3_port_max_link_rate(struct tb_port *port)
821{
822 int ret, lr;
823 u32 val;
824
825 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
826 return -EINVAL;
827
828 ret = tb_port_read(port, &val, TB_CFG_PORT,
829 port->cap_adap + ADP_USB3_CS_4, 1);
830 if (ret)
831 return ret;
832
833 lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
834 return lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
835}
836
837/**
838 * usb4_usb3_port_actual_link_rate() - Established USB3 link rate
839 * @port: USB3 adapter port
840 *
841 * Return actual established link rate of a USB3 adapter in Mb/s. If the
842 * link is not up returns %0 and negative errno in case of failure.
843 */
844int usb4_usb3_port_actual_link_rate(struct tb_port *port)
845{
846 int ret, lr;
847 u32 val;
848
849 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
850 return -EINVAL;
851
852 ret = tb_port_read(port, &val, TB_CFG_PORT,
853 port->cap_adap + ADP_USB3_CS_4, 1);
854 if (ret)
855 return ret;
856
857 if (!(val & ADP_USB3_CS_4_ULV))
858 return 0;
859
860 lr = val & ADP_USB3_CS_4_ALR_MASK;
861 return lr == ADP_USB3_CS_4_ALR_20G ? 20000 : 10000;
862}
863
864static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
865{
866 int ret;
867 u32 val;
868
869 if (!tb_port_is_usb3_down(port))
870 return -EINVAL;
871 if (tb_route(port->sw))
872 return -EINVAL;
873
874 ret = tb_port_read(port, &val, TB_CFG_PORT,
875 port->cap_adap + ADP_USB3_CS_2, 1);
876 if (ret)
877 return ret;
878
879 if (request)
880 val |= ADP_USB3_CS_2_CMR;
881 else
882 val &= ~ADP_USB3_CS_2_CMR;
883
884 ret = tb_port_write(port, &val, TB_CFG_PORT,
885 port->cap_adap + ADP_USB3_CS_2, 1);
886 if (ret)
887 return ret;
888
889 /*
890 * We can use val here directly as the CMR bit is in the same place
891 * as HCA. Just mask out others.
892 */
893 val &= ADP_USB3_CS_2_CMR;
894 return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
895 ADP_USB3_CS_1_HCA, val, 1500);
896}
897
898static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
899{
900 return usb4_usb3_port_cm_request(port, true);
901}
902
903static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
904{
905 return usb4_usb3_port_cm_request(port, false);
906}
907
908static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
909{
910 unsigned long uframes;
911
912 uframes = bw * 512 << scale;
913 return DIV_ROUND_CLOSEST(uframes * 8000, 1000 * 1000);
914}
915
916static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
917{
918 unsigned long uframes;
919
920 /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
921 uframes = ((unsigned long)mbps * 1000 * 1000) / 8000;
922 return DIV_ROUND_UP(uframes, 512 << scale);
923}
924
925static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
926 int *upstream_bw,
927 int *downstream_bw)
928{
929 u32 val, bw, scale;
930 int ret;
931
932 ret = tb_port_read(port, &val, TB_CFG_PORT,
933 port->cap_adap + ADP_USB3_CS_2, 1);
934 if (ret)
935 return ret;
936
937 ret = tb_port_read(port, &scale, TB_CFG_PORT,
938 port->cap_adap + ADP_USB3_CS_3, 1);
939 if (ret)
940 return ret;
941
942 scale &= ADP_USB3_CS_3_SCALE_MASK;
943
944 bw = val & ADP_USB3_CS_2_AUBW_MASK;
945 *upstream_bw = usb3_bw_to_mbps(bw, scale);
946
947 bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
948 *downstream_bw = usb3_bw_to_mbps(bw, scale);
949
950 return 0;
951}
952
953/**
954 * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
955 * @port: USB3 adapter port
956 * @upstream_bw: Allocated upstream bandwidth is stored here
957 * @downstream_bw: Allocated downstream bandwidth is stored here
958 *
959 * Stores currently allocated USB3 bandwidth into @upstream_bw and
960 * @downstream_bw in Mb/s. Returns %0 in case of success and negative
961 * errno in failure.
962 */
963int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
964 int *downstream_bw)
965{
966 int ret;
967
968 ret = usb4_usb3_port_set_cm_request(port);
969 if (ret)
970 return ret;
971
972 ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
973 downstream_bw);
974 usb4_usb3_port_clear_cm_request(port);
975
976 return ret;
977}
978
979static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
980 int *upstream_bw,
981 int *downstream_bw)
982{
983 u32 val, bw, scale;
984 int ret;
985
986 ret = tb_port_read(port, &val, TB_CFG_PORT,
987 port->cap_adap + ADP_USB3_CS_1, 1);
988 if (ret)
989 return ret;
990
991 ret = tb_port_read(port, &scale, TB_CFG_PORT,
992 port->cap_adap + ADP_USB3_CS_3, 1);
993 if (ret)
994 return ret;
995
996 scale &= ADP_USB3_CS_3_SCALE_MASK;
997
998 bw = val & ADP_USB3_CS_1_CUBW_MASK;
999 *upstream_bw = usb3_bw_to_mbps(bw, scale);
1000
1001 bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
1002 *downstream_bw = usb3_bw_to_mbps(bw, scale);
1003
1004 return 0;
1005}
1006
1007static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
1008 int upstream_bw,
1009 int downstream_bw)
1010{
1011 u32 val, ubw, dbw, scale;
1012 int ret;
1013
1014 /* Read the used scale, hardware default is 0 */
1015 ret = tb_port_read(port, &scale, TB_CFG_PORT,
1016 port->cap_adap + ADP_USB3_CS_3, 1);
1017 if (ret)
1018 return ret;
1019
1020 scale &= ADP_USB3_CS_3_SCALE_MASK;
1021 ubw = mbps_to_usb3_bw(upstream_bw, scale);
1022 dbw = mbps_to_usb3_bw(downstream_bw, scale);
1023
1024 ret = tb_port_read(port, &val, TB_CFG_PORT,
1025 port->cap_adap + ADP_USB3_CS_2, 1);
1026 if (ret)
1027 return ret;
1028
1029 val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
1030 val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
1031 val |= ubw;
1032
1033 return tb_port_write(port, &val, TB_CFG_PORT,
1034 port->cap_adap + ADP_USB3_CS_2, 1);
1035}
1036
1037/**
1038 * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
1039 * @port: USB3 adapter port
1040 * @upstream_bw: New upstream bandwidth
1041 * @downstream_bw: New downstream bandwidth
1042 *
1043 * This can be used to set how much bandwidth is allocated for the USB3
1044 * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
1045 * new values programmed to the USB3 adapter allocation registers. If
1046 * the values are lower than what is currently consumed the allocation
1047 * is set to what is currently consumed instead (consumed bandwidth
1048 * cannot be taken away by CM). The actual new values are returned in
1049 * @upstream_bw and @downstream_bw.
1050 *
1051 * Returns %0 in case of success and negative errno if there was a
1052 * failure.
1053 */
1054int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1055 int *downstream_bw)
1056{
1057 int ret, consumed_up, consumed_down, allocate_up, allocate_down;
1058
1059 ret = usb4_usb3_port_set_cm_request(port);
1060 if (ret)
1061 return ret;
1062
1063 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
1064 &consumed_down);
1065 if (ret)
1066 goto err_request;
1067
1068 /* Don't allow it go lower than what is consumed */
1069 allocate_up = max(*upstream_bw, consumed_up);
1070 allocate_down = max(*downstream_bw, consumed_down);
1071
1072 ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
1073 allocate_down);
1074 if (ret)
1075 goto err_request;
1076
1077 *upstream_bw = allocate_up;
1078 *downstream_bw = allocate_down;
1079
1080err_request:
1081 usb4_usb3_port_clear_cm_request(port);
1082 return ret;
1083}
1084
1085/**
1086 * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
1087 * @port: USB3 adapter port
1088 * @upstream_bw: New allocated upstream bandwidth
1089 * @downstream_bw: New allocated downstream bandwidth
1090 *
1091 * Releases USB3 allocated bandwidth down to what is actually consumed.
1092 * The new bandwidth is returned in @upstream_bw and @downstream_bw.
1093 *
1094 * Returns 0% in success and negative errno in case of failure.
1095 */
1096int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1097 int *downstream_bw)
1098{
1099 int ret, consumed_up, consumed_down;
1100
1101 ret = usb4_usb3_port_set_cm_request(port);
1102 if (ret)
1103 return ret;
1104
1105 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
1106 &consumed_down);
1107 if (ret)
1108 goto err_request;
1109
1110 /*
1111 * Always keep 1000 Mb/s to make sure xHCI has at least some
1112 * bandwidth available for isochronous traffic.
1113 */
1114 if (consumed_up < 1000)
1115 consumed_up = 1000;
1116 if (consumed_down < 1000)
1117 consumed_down = 1000;
1118
1119 ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
1120 consumed_down);
1121 if (ret)
1122 goto err_request;
1123
1124 *upstream_bw = consumed_up;
1125 *downstream_bw = consumed_down;
1126
1127err_request:
1128 usb4_usb3_port_clear_cm_request(port);
1129 return ret;
1130}