1 // 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 #include <linux/units.h>
13
14 #include "sb_regs.h"
15 #include "tb.h"
16
17 #define USB4_DATA_RETRIES 3
18 #define USB4_DATA_DWORDS 16
19
20 enum usb4_sb_target {
21 USB4_SB_TARGET_ROUTER,
22 USB4_SB_TARGET_PARTNER,
23 USB4_SB_TARGET_RETIMER,
24 };
25
26 #define USB4_NVM_READ_OFFSET_MASK GENMASK(23, 2)
27 #define USB4_NVM_READ_OFFSET_SHIFT 2
28 #define USB4_NVM_READ_LENGTH_MASK GENMASK(27, 24)
29 #define USB4_NVM_READ_LENGTH_SHIFT 24
30
31 #define USB4_NVM_SET_OFFSET_MASK USB4_NVM_READ_OFFSET_MASK
32 #define USB4_NVM_SET_OFFSET_SHIFT USB4_NVM_READ_OFFSET_SHIFT
33
34 #define USB4_DROM_ADDRESS_MASK GENMASK(14, 2)
35 #define USB4_DROM_ADDRESS_SHIFT 2
36 #define USB4_DROM_SIZE_MASK GENMASK(19, 15)
37 #define USB4_DROM_SIZE_SHIFT 15
38
39 #define USB4_NVM_SECTOR_SIZE_MASK GENMASK(23, 0)
40
41 #define USB4_BA_LENGTH_MASK GENMASK(7, 0)
42 #define USB4_BA_INDEX_MASK GENMASK(15, 0)
43
44 enum usb4_ba_index {
45 USB4_BA_MAX_USB3 = 0x1,
46 USB4_BA_MIN_DP_AUX = 0x2,
47 USB4_BA_MIN_DP_MAIN = 0x3,
48 USB4_BA_MAX_PCIE = 0x4,
49 USB4_BA_MAX_HI = 0x5,
50 };
51
52 #define USB4_BA_VALUE_MASK GENMASK(31, 16)
53 #define USB4_BA_VALUE_SHIFT 16
54
usb4_native_switch_op(struct tb_switch * sw,u16 opcode,u32 * metadata,u8 * status,const void * tx_data,size_t tx_dwords,void * rx_data,size_t rx_dwords)55 static int usb4_native_switch_op(struct tb_switch *sw, u16 opcode,
56 u32 *metadata, u8 *status,
57 const void *tx_data, size_t tx_dwords,
58 void *rx_data, size_t rx_dwords)
59 {
60 u32 val;
61 int ret;
62
63 if (metadata) {
64 ret = tb_sw_write(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
65 if (ret)
66 return ret;
67 }
68 if (tx_dwords) {
69 ret = tb_sw_write(sw, tx_data, TB_CFG_SWITCH, ROUTER_CS_9,
70 tx_dwords);
71 if (ret)
72 return ret;
73 }
74
75 val = opcode | ROUTER_CS_26_OV;
76 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
77 if (ret)
78 return ret;
79
80 ret = tb_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
81 if (ret)
82 return ret;
83
84 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
85 if (ret)
86 return ret;
87
88 if (val & ROUTER_CS_26_ONS)
89 return -EOPNOTSUPP;
90
91 if (status)
92 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
93 ROUTER_CS_26_STATUS_SHIFT;
94
95 if (metadata) {
96 ret = tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
97 if (ret)
98 return ret;
99 }
100 if (rx_dwords) {
101 ret = tb_sw_read(sw, rx_data, TB_CFG_SWITCH, ROUTER_CS_9,
102 rx_dwords);
103 if (ret)
104 return ret;
105 }
106
107 return 0;
108 }
109
__usb4_switch_op(struct tb_switch * sw,u16 opcode,u32 * metadata,u8 * status,const void * tx_data,size_t tx_dwords,void * rx_data,size_t rx_dwords)110 static int __usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
111 u8 *status, const void *tx_data, size_t tx_dwords,
112 void *rx_data, size_t rx_dwords)
113 {
114 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
115
116 if (tx_dwords > USB4_DATA_DWORDS || rx_dwords > USB4_DATA_DWORDS)
117 return -EINVAL;
118
119 /*
120 * If the connection manager implementation provides USB4 router
121 * operation proxy callback, call it here instead of running the
122 * operation natively.
123 */
124 if (cm_ops->usb4_switch_op) {
125 int ret;
126
127 ret = cm_ops->usb4_switch_op(sw, opcode, metadata, status,
128 tx_data, tx_dwords, rx_data,
129 rx_dwords);
130 if (ret != -EOPNOTSUPP)
131 return ret;
132
133 /*
134 * If the proxy was not supported then run the native
135 * router operation instead.
136 */
137 }
138
139 return usb4_native_switch_op(sw, opcode, metadata, status, tx_data,
140 tx_dwords, rx_data, rx_dwords);
141 }
142
usb4_switch_op(struct tb_switch * sw,u16 opcode,u32 * metadata,u8 * status)143 static inline int usb4_switch_op(struct tb_switch *sw, u16 opcode,
144 u32 *metadata, u8 *status)
145 {
146 return __usb4_switch_op(sw, opcode, metadata, status, NULL, 0, NULL, 0);
147 }
148
usb4_switch_op_data(struct tb_switch * sw,u16 opcode,u32 * metadata,u8 * status,const void * tx_data,size_t tx_dwords,void * rx_data,size_t rx_dwords)149 static inline int usb4_switch_op_data(struct tb_switch *sw, u16 opcode,
150 u32 *metadata, u8 *status,
151 const void *tx_data, size_t tx_dwords,
152 void *rx_data, size_t rx_dwords)
153 {
154 return __usb4_switch_op(sw, opcode, metadata, status, tx_data,
155 tx_dwords, rx_data, rx_dwords);
156 }
157
158 /**
159 * usb4_switch_check_wakes() - Check for wakes and notify PM core about them
160 * @sw: Router whose wakes to check
161 *
162 * Checks wakes occurred during suspend and notify the PM core about them.
163 */
usb4_switch_check_wakes(struct tb_switch * sw)164 void usb4_switch_check_wakes(struct tb_switch *sw)
165 {
166 bool wakeup_usb4 = false;
167 struct usb4_port *usb4;
168 struct tb_port *port;
169 bool wakeup = false;
170 u32 val;
171
172 if (tb_route(sw)) {
173 if (tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1))
174 return;
175
176 tb_sw_dbg(sw, "PCIe wake: %s, USB3 wake: %s\n",
177 (val & ROUTER_CS_6_WOPS) ? "yes" : "no",
178 (val & ROUTER_CS_6_WOUS) ? "yes" : "no");
179
180 wakeup = val & (ROUTER_CS_6_WOPS | ROUTER_CS_6_WOUS);
181 }
182
183 /*
184 * Check for any downstream ports for USB4 wake,
185 * connection wake and disconnection wake.
186 */
187 tb_switch_for_each_port(sw, port) {
188 if (!port->cap_usb4)
189 continue;
190
191 if (tb_port_read(port, &val, TB_CFG_PORT,
192 port->cap_usb4 + PORT_CS_18, 1))
193 break;
194
195 tb_port_dbg(port, "USB4 wake: %s, connection wake: %s, disconnection wake: %s\n",
196 (val & PORT_CS_18_WOU4S) ? "yes" : "no",
197 (val & PORT_CS_18_WOCS) ? "yes" : "no",
198 (val & PORT_CS_18_WODS) ? "yes" : "no");
199
200 wakeup_usb4 = val & (PORT_CS_18_WOU4S | PORT_CS_18_WOCS |
201 PORT_CS_18_WODS);
202
203 usb4 = port->usb4;
204 if (device_may_wakeup(&usb4->dev) && wakeup_usb4)
205 pm_wakeup_event(&usb4->dev, 0);
206
207 wakeup |= wakeup_usb4;
208 }
209
210 if (wakeup)
211 pm_wakeup_event(&sw->dev, 0);
212 }
213
link_is_usb4(struct tb_port * port)214 static bool link_is_usb4(struct tb_port *port)
215 {
216 u32 val;
217
218 if (!port->cap_usb4)
219 return false;
220
221 if (tb_port_read(port, &val, TB_CFG_PORT,
222 port->cap_usb4 + PORT_CS_18, 1))
223 return false;
224
225 return !(val & PORT_CS_18_TCM);
226 }
227
228 /**
229 * usb4_switch_setup() - Additional setup for USB4 device
230 * @sw: USB4 router to setup
231 *
232 * USB4 routers need additional settings in order to enable all the
233 * tunneling. This function enables USB and PCIe tunneling if it can be
234 * enabled (e.g the parent switch also supports them). If USB tunneling
235 * is not available for some reason (like that there is Thunderbolt 3
236 * switch upstream) then the internal xHCI controller is enabled
237 * instead.
238 *
239 * This does not set the configuration valid bit of the router. To do
240 * that call usb4_switch_configuration_valid().
241 */
usb4_switch_setup(struct tb_switch * sw)242 int usb4_switch_setup(struct tb_switch *sw)
243 {
244 struct tb_switch *parent = tb_switch_parent(sw);
245 struct tb_port *down;
246 bool tbt3, xhci;
247 u32 val = 0;
248 int ret;
249
250 if (!tb_route(sw))
251 return 0;
252
253 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
254 if (ret)
255 return ret;
256
257 down = tb_switch_downstream_port(sw);
258 sw->link_usb4 = link_is_usb4(down);
259 tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT");
260
261 xhci = val & ROUTER_CS_6_HCI;
262 tbt3 = !(val & ROUTER_CS_6_TNS);
263
264 tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
265 tbt3 ? "yes" : "no", xhci ? "yes" : "no");
266
267 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
268 if (ret)
269 return ret;
270
271 if (tb_acpi_may_tunnel_usb3() && sw->link_usb4 &&
272 tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
273 val |= ROUTER_CS_5_UTO;
274 xhci = false;
275 }
276
277 /*
278 * Only enable PCIe tunneling if the parent router supports it
279 * and it is not disabled.
280 */
281 if (tb_acpi_may_tunnel_pcie() &&
282 tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
283 val |= ROUTER_CS_5_PTO;
284 /*
285 * xHCI can be enabled if PCIe tunneling is supported
286 * and the parent does not have any USB3 dowstream
287 * adapters (so we cannot do USB 3.x tunneling).
288 */
289 if (xhci)
290 val |= ROUTER_CS_5_HCO;
291 }
292
293 /* TBT3 supported by the CM */
294 val &= ~ROUTER_CS_5_CNS;
295
296 return tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
297 }
298
299 /**
300 * usb4_switch_configuration_valid() - Set tunneling configuration to be valid
301 * @sw: USB4 router
302 *
303 * Sets configuration valid bit for the router. Must be called before
304 * any tunnels can be set through the router and after
305 * usb4_switch_setup() has been called. Can be called to host and device
306 * routers (does nothing for the latter).
307 *
308 * Returns %0 in success and negative errno otherwise.
309 */
usb4_switch_configuration_valid(struct tb_switch * sw)310 int usb4_switch_configuration_valid(struct tb_switch *sw)
311 {
312 u32 val;
313 int ret;
314
315 if (!tb_route(sw))
316 return 0;
317
318 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
319 if (ret)
320 return ret;
321
322 val |= ROUTER_CS_5_CV;
323
324 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
325 if (ret)
326 return ret;
327
328 return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
329 ROUTER_CS_6_CR, 50);
330 }
331
332 /**
333 * usb4_switch_read_uid() - Read UID from USB4 router
334 * @sw: USB4 router
335 * @uid: UID is stored here
336 *
337 * Reads 64-bit UID from USB4 router config space.
338 */
usb4_switch_read_uid(struct tb_switch * sw,u64 * uid)339 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
340 {
341 return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
342 }
343
usb4_switch_drom_read_block(void * data,unsigned int dwaddress,void * buf,size_t dwords)344 static int usb4_switch_drom_read_block(void *data,
345 unsigned int dwaddress, void *buf,
346 size_t dwords)
347 {
348 struct tb_switch *sw = data;
349 u8 status = 0;
350 u32 metadata;
351 int ret;
352
353 metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
354 metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
355 USB4_DROM_ADDRESS_MASK;
356
357 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_DROM_READ, &metadata,
358 &status, NULL, 0, buf, dwords);
359 if (ret)
360 return ret;
361
362 return status ? -EIO : 0;
363 }
364
365 /**
366 * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
367 * @sw: USB4 router
368 * @address: Byte address inside DROM to start reading
369 * @buf: Buffer where the DROM content is stored
370 * @size: Number of bytes to read from DROM
371 *
372 * Uses USB4 router operations to read router DROM. For devices this
373 * should always work but for hosts it may return %-EOPNOTSUPP in which
374 * case the host router does not have DROM.
375 */
usb4_switch_drom_read(struct tb_switch * sw,unsigned int address,void * buf,size_t size)376 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
377 size_t size)
378 {
379 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
380 usb4_switch_drom_read_block, sw);
381 }
382
383 /**
384 * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
385 * @sw: USB4 router
386 *
387 * Checks whether conditions are met so that lane bonding can be
388 * established with the upstream router. Call only for device routers.
389 */
usb4_switch_lane_bonding_possible(struct tb_switch * sw)390 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
391 {
392 struct tb_port *up;
393 int ret;
394 u32 val;
395
396 up = tb_upstream_port(sw);
397 ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
398 if (ret)
399 return false;
400
401 return !!(val & PORT_CS_18_BE);
402 }
403
404 /**
405 * usb4_switch_set_wake() - Enabled/disable wake
406 * @sw: USB4 router
407 * @flags: Wakeup flags (%0 to disable)
408 * @runtime: Wake is being programmed during system runtime
409 *
410 * Enables/disables router to wake up from sleep.
411 */
usb4_switch_set_wake(struct tb_switch * sw,unsigned int flags,bool runtime)412 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags, bool runtime)
413 {
414 struct tb_port *port;
415 u64 route = tb_route(sw);
416 u32 val;
417 int ret;
418
419 /*
420 * Enable wakes coming from all USB4 downstream ports (from
421 * child routers). For device routers do this also for the
422 * upstream USB4 port.
423 */
424 tb_switch_for_each_port(sw, port) {
425 if (!tb_port_is_null(port))
426 continue;
427 if (!route && tb_is_upstream_port(port))
428 continue;
429 if (!port->cap_usb4)
430 continue;
431
432 ret = tb_port_read(port, &val, TB_CFG_PORT,
433 port->cap_usb4 + PORT_CS_19, 1);
434 if (ret)
435 return ret;
436
437 val &= ~(PORT_CS_19_WOC | PORT_CS_19_WOD | PORT_CS_19_WOU4);
438
439 if (tb_is_upstream_port(port)) {
440 val |= PORT_CS_19_WOU4;
441 } else {
442 bool configured = val & PORT_CS_19_PC;
443 bool wakeup = runtime || device_may_wakeup(&port->usb4->dev);
444
445 if ((flags & TB_WAKE_ON_CONNECT) && wakeup && !configured)
446 val |= PORT_CS_19_WOC;
447 if ((flags & TB_WAKE_ON_DISCONNECT) && wakeup && configured)
448 val |= PORT_CS_19_WOD;
449 if ((flags & TB_WAKE_ON_USB4) && configured)
450 val |= PORT_CS_19_WOU4;
451 }
452
453 ret = tb_port_write(port, &val, TB_CFG_PORT,
454 port->cap_usb4 + PORT_CS_19, 1);
455 if (ret)
456 return ret;
457 }
458
459 /*
460 * Enable wakes from PCIe, USB 3.x and DP on this router. Only
461 * needed for device routers.
462 */
463 if (route) {
464 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
465 if (ret)
466 return ret;
467
468 val &= ~(ROUTER_CS_5_WOP | ROUTER_CS_5_WOU | ROUTER_CS_5_WOD);
469 if (flags & TB_WAKE_ON_USB3)
470 val |= ROUTER_CS_5_WOU;
471 if (flags & TB_WAKE_ON_PCIE)
472 val |= ROUTER_CS_5_WOP;
473 if (flags & TB_WAKE_ON_DP)
474 val |= ROUTER_CS_5_WOD;
475
476 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
477 if (ret)
478 return ret;
479 }
480
481 return 0;
482 }
483
484 /**
485 * usb4_switch_set_sleep() - Prepare the router to enter sleep
486 * @sw: USB4 router
487 *
488 * Sets sleep bit for the router. Returns when the router sleep ready
489 * bit has been asserted.
490 */
usb4_switch_set_sleep(struct tb_switch * sw)491 int usb4_switch_set_sleep(struct tb_switch *sw)
492 {
493 int ret;
494 u32 val;
495
496 /* Set sleep bit and wait for sleep ready to be asserted */
497 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
498 if (ret)
499 return ret;
500
501 val |= ROUTER_CS_5_SLP;
502
503 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
504 if (ret)
505 return ret;
506
507 return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
508 ROUTER_CS_6_SLPR, 500);
509 }
510
511 /**
512 * usb4_switch_nvm_sector_size() - Return router NVM sector size
513 * @sw: USB4 router
514 *
515 * If the router supports NVM operations this function returns the NVM
516 * sector size in bytes. If NVM operations are not supported returns
517 * %-EOPNOTSUPP.
518 */
usb4_switch_nvm_sector_size(struct tb_switch * sw)519 int usb4_switch_nvm_sector_size(struct tb_switch *sw)
520 {
521 u32 metadata;
522 u8 status;
523 int ret;
524
525 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &metadata,
526 &status);
527 if (ret)
528 return ret;
529
530 if (status)
531 return status == 0x2 ? -EOPNOTSUPP : -EIO;
532
533 return metadata & USB4_NVM_SECTOR_SIZE_MASK;
534 }
535
usb4_switch_nvm_read_block(void * data,unsigned int dwaddress,void * buf,size_t dwords)536 static int usb4_switch_nvm_read_block(void *data,
537 unsigned int dwaddress, void *buf, size_t dwords)
538 {
539 struct tb_switch *sw = data;
540 u8 status = 0;
541 u32 metadata;
542 int ret;
543
544 metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
545 USB4_NVM_READ_LENGTH_MASK;
546 metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
547 USB4_NVM_READ_OFFSET_MASK;
548
549 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_READ, &metadata,
550 &status, NULL, 0, buf, dwords);
551 if (ret)
552 return ret;
553
554 return status ? -EIO : 0;
555 }
556
557 /**
558 * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
559 * @sw: USB4 router
560 * @address: Starting address in bytes
561 * @buf: Read data is placed here
562 * @size: How many bytes to read
563 *
564 * Reads NVM contents of the router. If NVM is not supported returns
565 * %-EOPNOTSUPP.
566 */
usb4_switch_nvm_read(struct tb_switch * sw,unsigned int address,void * buf,size_t size)567 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
568 size_t size)
569 {
570 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
571 usb4_switch_nvm_read_block, sw);
572 }
573
574 /**
575 * usb4_switch_nvm_set_offset() - Set NVM write offset
576 * @sw: USB4 router
577 * @address: Start offset
578 *
579 * Explicitly sets NVM write offset. Normally when writing to NVM this
580 * is done automatically by usb4_switch_nvm_write().
581 *
582 * Returns %0 in success and negative errno if there was a failure.
583 */
usb4_switch_nvm_set_offset(struct tb_switch * sw,unsigned int address)584 int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address)
585 {
586 u32 metadata, dwaddress;
587 u8 status = 0;
588 int ret;
589
590 dwaddress = address / 4;
591 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
592 USB4_NVM_SET_OFFSET_MASK;
593
594 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &metadata,
595 &status);
596 if (ret)
597 return ret;
598
599 return status ? -EIO : 0;
600 }
601
usb4_switch_nvm_write_next_block(void * data,unsigned int dwaddress,const void * buf,size_t dwords)602 static int usb4_switch_nvm_write_next_block(void *data, unsigned int dwaddress,
603 const void *buf, size_t dwords)
604 {
605 struct tb_switch *sw = data;
606 u8 status;
607 int ret;
608
609 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_WRITE, NULL, &status,
610 buf, dwords, NULL, 0);
611 if (ret)
612 return ret;
613
614 return status ? -EIO : 0;
615 }
616
617 /**
618 * usb4_switch_nvm_write() - Write to the router NVM
619 * @sw: USB4 router
620 * @address: Start address where to write in bytes
621 * @buf: Pointer to the data to write
622 * @size: Size of @buf in bytes
623 *
624 * Writes @buf to the router NVM using USB4 router operations. If NVM
625 * write is not supported returns %-EOPNOTSUPP.
626 */
usb4_switch_nvm_write(struct tb_switch * sw,unsigned int address,const void * buf,size_t size)627 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
628 const void *buf, size_t size)
629 {
630 int ret;
631
632 ret = usb4_switch_nvm_set_offset(sw, address);
633 if (ret)
634 return ret;
635
636 return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
637 usb4_switch_nvm_write_next_block, sw);
638 }
639
640 /**
641 * usb4_switch_nvm_authenticate() - Authenticate new NVM
642 * @sw: USB4 router
643 *
644 * After the new NVM has been written via usb4_switch_nvm_write(), this
645 * function triggers NVM authentication process. The router gets power
646 * cycled and if the authentication is successful the new NVM starts
647 * running. In case of failure returns negative errno.
648 *
649 * The caller should call usb4_switch_nvm_authenticate_status() to read
650 * the status of the authentication after power cycle. It should be the
651 * first router operation to avoid the status being lost.
652 */
usb4_switch_nvm_authenticate(struct tb_switch * sw)653 int usb4_switch_nvm_authenticate(struct tb_switch *sw)
654 {
655 int ret;
656
657 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, NULL, NULL);
658 switch (ret) {
659 /*
660 * The router is power cycled once NVM_AUTH is started so it is
661 * expected to get any of the following errors back.
662 */
663 case -EACCES:
664 case -ENOTCONN:
665 case -ETIMEDOUT:
666 return 0;
667
668 default:
669 return ret;
670 }
671 }
672
673 /**
674 * usb4_switch_nvm_authenticate_status() - Read status of last NVM authenticate
675 * @sw: USB4 router
676 * @status: Status code of the operation
677 *
678 * The function checks if there is status available from the last NVM
679 * authenticate router operation. If there is status then %0 is returned
680 * and the status code is placed in @status. Returns negative errno in case
681 * of failure.
682 *
683 * Must be called before any other router operation.
684 */
usb4_switch_nvm_authenticate_status(struct tb_switch * sw,u32 * status)685 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status)
686 {
687 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
688 u16 opcode;
689 u32 val;
690 int ret;
691
692 if (cm_ops->usb4_switch_nvm_authenticate_status) {
693 ret = cm_ops->usb4_switch_nvm_authenticate_status(sw, status);
694 if (ret != -EOPNOTSUPP)
695 return ret;
696 }
697
698 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
699 if (ret)
700 return ret;
701
702 /* Check that the opcode is correct */
703 opcode = val & ROUTER_CS_26_OPCODE_MASK;
704 if (opcode == USB4_SWITCH_OP_NVM_AUTH) {
705 if (val & ROUTER_CS_26_OV)
706 return -EBUSY;
707 if (val & ROUTER_CS_26_ONS)
708 return -EOPNOTSUPP;
709
710 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
711 ROUTER_CS_26_STATUS_SHIFT;
712 } else {
713 *status = 0;
714 }
715
716 return 0;
717 }
718
719 /**
720 * usb4_switch_credits_init() - Read buffer allocation parameters
721 * @sw: USB4 router
722 *
723 * Reads @sw buffer allocation parameters and initializes @sw buffer
724 * allocation fields accordingly. Specifically @sw->credits_allocation
725 * is set to %true if these parameters can be used in tunneling.
726 *
727 * Returns %0 on success and negative errno otherwise.
728 */
usb4_switch_credits_init(struct tb_switch * sw)729 int usb4_switch_credits_init(struct tb_switch *sw)
730 {
731 int max_usb3, min_dp_aux, min_dp_main, max_pcie, max_dma;
732 int ret, length, i, nports;
733 const struct tb_port *port;
734 u32 data[USB4_DATA_DWORDS];
735 u32 metadata = 0;
736 u8 status = 0;
737
738 memset(data, 0, sizeof(data));
739 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_BUFFER_ALLOC, &metadata,
740 &status, NULL, 0, data, ARRAY_SIZE(data));
741 if (ret)
742 return ret;
743 if (status)
744 return -EIO;
745
746 length = metadata & USB4_BA_LENGTH_MASK;
747 if (WARN_ON(length > ARRAY_SIZE(data)))
748 return -EMSGSIZE;
749
750 max_usb3 = -1;
751 min_dp_aux = -1;
752 min_dp_main = -1;
753 max_pcie = -1;
754 max_dma = -1;
755
756 tb_sw_dbg(sw, "credit allocation parameters:\n");
757
758 for (i = 0; i < length; i++) {
759 u16 index, value;
760
761 index = data[i] & USB4_BA_INDEX_MASK;
762 value = (data[i] & USB4_BA_VALUE_MASK) >> USB4_BA_VALUE_SHIFT;
763
764 switch (index) {
765 case USB4_BA_MAX_USB3:
766 tb_sw_dbg(sw, " USB3: %u\n", value);
767 max_usb3 = value;
768 break;
769 case USB4_BA_MIN_DP_AUX:
770 tb_sw_dbg(sw, " DP AUX: %u\n", value);
771 min_dp_aux = value;
772 break;
773 case USB4_BA_MIN_DP_MAIN:
774 tb_sw_dbg(sw, " DP main: %u\n", value);
775 min_dp_main = value;
776 break;
777 case USB4_BA_MAX_PCIE:
778 tb_sw_dbg(sw, " PCIe: %u\n", value);
779 max_pcie = value;
780 break;
781 case USB4_BA_MAX_HI:
782 tb_sw_dbg(sw, " DMA: %u\n", value);
783 max_dma = value;
784 break;
785 default:
786 tb_sw_dbg(sw, " unknown credit allocation index %#x, skipping\n",
787 index);
788 break;
789 }
790 }
791
792 /*
793 * Validate the buffer allocation preferences. If we find
794 * issues, log a warning and fall back using the hard-coded
795 * values.
796 */
797
798 /* Host router must report baMaxHI */
799 if (!tb_route(sw) && max_dma < 0) {
800 tb_sw_warn(sw, "host router is missing baMaxHI\n");
801 goto err_invalid;
802 }
803
804 nports = 0;
805 tb_switch_for_each_port(sw, port) {
806 if (tb_port_is_null(port))
807 nports++;
808 }
809
810 /* Must have DP buffer allocation (multiple USB4 ports) */
811 if (nports > 2 && (min_dp_aux < 0 || min_dp_main < 0)) {
812 tb_sw_warn(sw, "multiple USB4 ports require baMinDPaux/baMinDPmain\n");
813 goto err_invalid;
814 }
815
816 tb_switch_for_each_port(sw, port) {
817 if (tb_port_is_dpout(port) && min_dp_main < 0) {
818 tb_sw_warn(sw, "missing baMinDPmain");
819 goto err_invalid;
820 }
821 if ((tb_port_is_dpin(port) || tb_port_is_dpout(port)) &&
822 min_dp_aux < 0) {
823 tb_sw_warn(sw, "missing baMinDPaux");
824 goto err_invalid;
825 }
826 if ((tb_port_is_usb3_down(port) || tb_port_is_usb3_up(port)) &&
827 max_usb3 < 0) {
828 tb_sw_warn(sw, "missing baMaxUSB3");
829 goto err_invalid;
830 }
831 if ((tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) &&
832 max_pcie < 0) {
833 tb_sw_warn(sw, "missing baMaxPCIe");
834 goto err_invalid;
835 }
836 }
837
838 /*
839 * Buffer allocation passed the validation so we can use it in
840 * path creation.
841 */
842 sw->credit_allocation = true;
843 if (max_usb3 > 0)
844 sw->max_usb3_credits = max_usb3;
845 if (min_dp_aux > 0)
846 sw->min_dp_aux_credits = min_dp_aux;
847 if (min_dp_main > 0)
848 sw->min_dp_main_credits = min_dp_main;
849 if (max_pcie > 0)
850 sw->max_pcie_credits = max_pcie;
851 if (max_dma > 0)
852 sw->max_dma_credits = max_dma;
853
854 return 0;
855
856 err_invalid:
857 return -EINVAL;
858 }
859
860 /**
861 * usb4_switch_query_dp_resource() - Query availability of DP IN resource
862 * @sw: USB4 router
863 * @in: DP IN adapter
864 *
865 * For DP tunneling this function can be used to query availability of
866 * DP IN resource. Returns true if the resource is available for DP
867 * tunneling, false otherwise.
868 */
usb4_switch_query_dp_resource(struct tb_switch * sw,struct tb_port * in)869 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
870 {
871 u32 metadata = in->port;
872 u8 status;
873 int ret;
874
875 ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &metadata,
876 &status);
877 /*
878 * If DP resource allocation is not supported assume it is
879 * always available.
880 */
881 if (ret == -EOPNOTSUPP)
882 return true;
883 if (ret)
884 return false;
885
886 return !status;
887 }
888
889 /**
890 * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
891 * @sw: USB4 router
892 * @in: DP IN adapter
893 *
894 * Allocates DP IN resource for DP tunneling using USB4 router
895 * operations. If the resource was allocated returns %0. Otherwise
896 * returns negative errno, in particular %-EBUSY if the resource is
897 * already allocated.
898 */
usb4_switch_alloc_dp_resource(struct tb_switch * sw,struct tb_port * in)899 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
900 {
901 u32 metadata = in->port;
902 u8 status;
903 int ret;
904
905 ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &metadata,
906 &status);
907 if (ret == -EOPNOTSUPP)
908 return 0;
909 if (ret)
910 return ret;
911
912 return status ? -EBUSY : 0;
913 }
914
915 /**
916 * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
917 * @sw: USB4 router
918 * @in: DP IN adapter
919 *
920 * Releases the previously allocated DP IN resource.
921 */
usb4_switch_dealloc_dp_resource(struct tb_switch * sw,struct tb_port * in)922 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
923 {
924 u32 metadata = in->port;
925 u8 status;
926 int ret;
927
928 ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &metadata,
929 &status);
930 if (ret == -EOPNOTSUPP)
931 return 0;
932 if (ret)
933 return ret;
934
935 return status ? -EIO : 0;
936 }
937
usb4_port_idx(const struct tb_switch * sw,const struct tb_port * port)938 static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
939 {
940 struct tb_port *p;
941 int usb4_idx = 0;
942
943 /* Assume port is primary */
944 tb_switch_for_each_port(sw, p) {
945 if (!tb_port_is_null(p))
946 continue;
947 if (tb_is_upstream_port(p))
948 continue;
949 if (!p->link_nr) {
950 if (p == port)
951 break;
952 usb4_idx++;
953 }
954 }
955
956 return usb4_idx;
957 }
958
959 /**
960 * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
961 * @sw: USB4 router
962 * @port: USB4 port
963 *
964 * USB4 routers have direct mapping between USB4 ports and PCIe
965 * downstream adapters where the PCIe topology is extended. This
966 * function returns the corresponding downstream PCIe adapter or %NULL
967 * if no such mapping was possible.
968 */
usb4_switch_map_pcie_down(struct tb_switch * sw,const struct tb_port * port)969 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
970 const struct tb_port *port)
971 {
972 int usb4_idx = usb4_port_idx(sw, port);
973 struct tb_port *p;
974 int pcie_idx = 0;
975
976 /* Find PCIe down port matching usb4_port */
977 tb_switch_for_each_port(sw, p) {
978 if (!tb_port_is_pcie_down(p))
979 continue;
980
981 if (pcie_idx == usb4_idx)
982 return p;
983
984 pcie_idx++;
985 }
986
987 return NULL;
988 }
989
990 /**
991 * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
992 * @sw: USB4 router
993 * @port: USB4 port
994 *
995 * USB4 routers have direct mapping between USB4 ports and USB 3.x
996 * downstream adapters where the USB 3.x topology is extended. This
997 * function returns the corresponding downstream USB 3.x adapter or
998 * %NULL if no such mapping was possible.
999 */
usb4_switch_map_usb3_down(struct tb_switch * sw,const struct tb_port * port)1000 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
1001 const struct tb_port *port)
1002 {
1003 int usb4_idx = usb4_port_idx(sw, port);
1004 struct tb_port *p;
1005 int usb_idx = 0;
1006
1007 /* Find USB3 down port matching usb4_port */
1008 tb_switch_for_each_port(sw, p) {
1009 if (!tb_port_is_usb3_down(p))
1010 continue;
1011
1012 if (usb_idx == usb4_idx)
1013 return p;
1014
1015 usb_idx++;
1016 }
1017
1018 return NULL;
1019 }
1020
1021 /**
1022 * usb4_switch_add_ports() - Add USB4 ports for this router
1023 * @sw: USB4 router
1024 *
1025 * For USB4 router finds all USB4 ports and registers devices for each.
1026 * Can be called to any router.
1027 *
1028 * Return %0 in case of success and negative errno in case of failure.
1029 */
usb4_switch_add_ports(struct tb_switch * sw)1030 int usb4_switch_add_ports(struct tb_switch *sw)
1031 {
1032 struct tb_port *port;
1033
1034 if (tb_switch_is_icm(sw) || !tb_switch_is_usb4(sw))
1035 return 0;
1036
1037 tb_switch_for_each_port(sw, port) {
1038 struct usb4_port *usb4;
1039
1040 if (!tb_port_is_null(port))
1041 continue;
1042 if (!port->cap_usb4)
1043 continue;
1044
1045 usb4 = usb4_port_device_add(port);
1046 if (IS_ERR(usb4)) {
1047 usb4_switch_remove_ports(sw);
1048 return PTR_ERR(usb4);
1049 }
1050
1051 port->usb4 = usb4;
1052 }
1053
1054 return 0;
1055 }
1056
1057 /**
1058 * usb4_switch_remove_ports() - Removes USB4 ports from this router
1059 * @sw: USB4 router
1060 *
1061 * Unregisters previously registered USB4 ports.
1062 */
usb4_switch_remove_ports(struct tb_switch * sw)1063 void usb4_switch_remove_ports(struct tb_switch *sw)
1064 {
1065 struct tb_port *port;
1066
1067 tb_switch_for_each_port(sw, port) {
1068 if (port->usb4) {
1069 usb4_port_device_remove(port->usb4);
1070 port->usb4 = NULL;
1071 }
1072 }
1073 }
1074
1075 /**
1076 * usb4_port_unlock() - Unlock USB4 downstream port
1077 * @port: USB4 port to unlock
1078 *
1079 * Unlocks USB4 downstream port so that the connection manager can
1080 * access the router below this port.
1081 */
usb4_port_unlock(struct tb_port * port)1082 int usb4_port_unlock(struct tb_port *port)
1083 {
1084 int ret;
1085 u32 val;
1086
1087 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
1088 if (ret)
1089 return ret;
1090
1091 val &= ~ADP_CS_4_LCK;
1092 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
1093 }
1094
1095 /**
1096 * usb4_port_hotplug_enable() - Enables hotplug for a port
1097 * @port: USB4 port to operate on
1098 *
1099 * Enables hot plug events on a given port. This is only intended
1100 * to be used on lane, DP-IN, and DP-OUT adapters.
1101 */
usb4_port_hotplug_enable(struct tb_port * port)1102 int usb4_port_hotplug_enable(struct tb_port *port)
1103 {
1104 int ret;
1105 u32 val;
1106
1107 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
1108 if (ret)
1109 return ret;
1110
1111 val &= ~ADP_CS_5_DHP;
1112 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
1113 }
1114
1115 /**
1116 * usb4_port_reset() - Issue downstream port reset
1117 * @port: USB4 port to reset
1118 *
1119 * Issues downstream port reset to @port.
1120 */
usb4_port_reset(struct tb_port * port)1121 int usb4_port_reset(struct tb_port *port)
1122 {
1123 int ret;
1124 u32 val;
1125
1126 if (!port->cap_usb4)
1127 return -EINVAL;
1128
1129 ret = tb_port_read(port, &val, TB_CFG_PORT,
1130 port->cap_usb4 + PORT_CS_19, 1);
1131 if (ret)
1132 return ret;
1133
1134 val |= PORT_CS_19_DPR;
1135
1136 ret = tb_port_write(port, &val, TB_CFG_PORT,
1137 port->cap_usb4 + PORT_CS_19, 1);
1138 if (ret)
1139 return ret;
1140
1141 fsleep(10000);
1142
1143 ret = tb_port_read(port, &val, TB_CFG_PORT,
1144 port->cap_usb4 + PORT_CS_19, 1);
1145 if (ret)
1146 return ret;
1147
1148 val &= ~PORT_CS_19_DPR;
1149
1150 return tb_port_write(port, &val, TB_CFG_PORT,
1151 port->cap_usb4 + PORT_CS_19, 1);
1152 }
1153
usb4_port_set_configured(struct tb_port * port,bool configured)1154 static int usb4_port_set_configured(struct tb_port *port, bool configured)
1155 {
1156 int ret;
1157 u32 val;
1158
1159 if (!port->cap_usb4)
1160 return -EINVAL;
1161
1162 ret = tb_port_read(port, &val, TB_CFG_PORT,
1163 port->cap_usb4 + PORT_CS_19, 1);
1164 if (ret)
1165 return ret;
1166
1167 if (configured)
1168 val |= PORT_CS_19_PC;
1169 else
1170 val &= ~PORT_CS_19_PC;
1171
1172 return tb_port_write(port, &val, TB_CFG_PORT,
1173 port->cap_usb4 + PORT_CS_19, 1);
1174 }
1175
1176 /**
1177 * usb4_port_configure() - Set USB4 port configured
1178 * @port: USB4 router
1179 *
1180 * Sets the USB4 link to be configured for power management purposes.
1181 */
usb4_port_configure(struct tb_port * port)1182 int usb4_port_configure(struct tb_port *port)
1183 {
1184 return usb4_port_set_configured(port, true);
1185 }
1186
1187 /**
1188 * usb4_port_unconfigure() - Set USB4 port unconfigured
1189 * @port: USB4 router
1190 *
1191 * Sets the USB4 link to be unconfigured for power management purposes.
1192 */
usb4_port_unconfigure(struct tb_port * port)1193 void usb4_port_unconfigure(struct tb_port *port)
1194 {
1195 usb4_port_set_configured(port, false);
1196 }
1197
usb4_set_xdomain_configured(struct tb_port * port,bool configured)1198 static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
1199 {
1200 int ret;
1201 u32 val;
1202
1203 if (!port->cap_usb4)
1204 return -EINVAL;
1205
1206 ret = tb_port_read(port, &val, TB_CFG_PORT,
1207 port->cap_usb4 + PORT_CS_19, 1);
1208 if (ret)
1209 return ret;
1210
1211 if (configured)
1212 val |= PORT_CS_19_PID;
1213 else
1214 val &= ~PORT_CS_19_PID;
1215
1216 return tb_port_write(port, &val, TB_CFG_PORT,
1217 port->cap_usb4 + PORT_CS_19, 1);
1218 }
1219
1220 /**
1221 * usb4_port_configure_xdomain() - Configure port for XDomain
1222 * @port: USB4 port connected to another host
1223 * @xd: XDomain that is connected to the port
1224 *
1225 * Marks the USB4 port as being connected to another host and updates
1226 * the link type. Returns %0 in success and negative errno in failure.
1227 */
usb4_port_configure_xdomain(struct tb_port * port,struct tb_xdomain * xd)1228 int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
1229 {
1230 xd->link_usb4 = link_is_usb4(port);
1231 return usb4_set_xdomain_configured(port, true);
1232 }
1233
1234 /**
1235 * usb4_port_unconfigure_xdomain() - Unconfigure port for XDomain
1236 * @port: USB4 port that was connected to another host
1237 *
1238 * Clears USB4 port from being marked as XDomain.
1239 */
usb4_port_unconfigure_xdomain(struct tb_port * port)1240 void usb4_port_unconfigure_xdomain(struct tb_port *port)
1241 {
1242 usb4_set_xdomain_configured(port, false);
1243 }
1244
usb4_port_wait_for_bit(struct tb_port * port,u32 offset,u32 bit,u32 value,int timeout_msec)1245 static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
1246 u32 value, int timeout_msec)
1247 {
1248 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1249
1250 do {
1251 u32 val;
1252 int ret;
1253
1254 ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
1255 if (ret)
1256 return ret;
1257
1258 if ((val & bit) == value)
1259 return 0;
1260
1261 usleep_range(50, 100);
1262 } while (ktime_before(ktime_get(), timeout));
1263
1264 return -ETIMEDOUT;
1265 }
1266
usb4_port_read_data(struct tb_port * port,void * data,size_t dwords)1267 static int usb4_port_read_data(struct tb_port *port, void *data, size_t dwords)
1268 {
1269 if (dwords > USB4_DATA_DWORDS)
1270 return -EINVAL;
1271
1272 return tb_port_read(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
1273 dwords);
1274 }
1275
usb4_port_write_data(struct tb_port * port,const void * data,size_t dwords)1276 static int usb4_port_write_data(struct tb_port *port, const void *data,
1277 size_t dwords)
1278 {
1279 if (dwords > USB4_DATA_DWORDS)
1280 return -EINVAL;
1281
1282 return tb_port_write(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
1283 dwords);
1284 }
1285
usb4_port_sb_read(struct tb_port * port,enum usb4_sb_target target,u8 index,u8 reg,void * buf,u8 size)1286 static int usb4_port_sb_read(struct tb_port *port, enum usb4_sb_target target,
1287 u8 index, u8 reg, void *buf, u8 size)
1288 {
1289 size_t dwords = DIV_ROUND_UP(size, 4);
1290 int ret;
1291 u32 val;
1292
1293 if (!port->cap_usb4)
1294 return -EINVAL;
1295
1296 val = reg;
1297 val |= size << PORT_CS_1_LENGTH_SHIFT;
1298 val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1299 if (target == USB4_SB_TARGET_RETIMER)
1300 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1301 val |= PORT_CS_1_PND;
1302
1303 ret = tb_port_write(port, &val, TB_CFG_PORT,
1304 port->cap_usb4 + PORT_CS_1, 1);
1305 if (ret)
1306 return ret;
1307
1308 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1309 PORT_CS_1_PND, 0, 500);
1310 if (ret)
1311 return ret;
1312
1313 ret = tb_port_read(port, &val, TB_CFG_PORT,
1314 port->cap_usb4 + PORT_CS_1, 1);
1315 if (ret)
1316 return ret;
1317
1318 if (val & PORT_CS_1_NR)
1319 return -ENODEV;
1320 if (val & PORT_CS_1_RC)
1321 return -EIO;
1322
1323 return buf ? usb4_port_read_data(port, buf, dwords) : 0;
1324 }
1325
usb4_port_sb_write(struct tb_port * port,enum usb4_sb_target target,u8 index,u8 reg,const void * buf,u8 size)1326 static int usb4_port_sb_write(struct tb_port *port, enum usb4_sb_target target,
1327 u8 index, u8 reg, const void *buf, u8 size)
1328 {
1329 size_t dwords = DIV_ROUND_UP(size, 4);
1330 int ret;
1331 u32 val;
1332
1333 if (!port->cap_usb4)
1334 return -EINVAL;
1335
1336 if (buf) {
1337 ret = usb4_port_write_data(port, buf, dwords);
1338 if (ret)
1339 return ret;
1340 }
1341
1342 val = reg;
1343 val |= size << PORT_CS_1_LENGTH_SHIFT;
1344 val |= PORT_CS_1_WNR_WRITE;
1345 val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1346 if (target == USB4_SB_TARGET_RETIMER)
1347 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1348 val |= PORT_CS_1_PND;
1349
1350 ret = tb_port_write(port, &val, TB_CFG_PORT,
1351 port->cap_usb4 + PORT_CS_1, 1);
1352 if (ret)
1353 return ret;
1354
1355 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1356 PORT_CS_1_PND, 0, 500);
1357 if (ret)
1358 return ret;
1359
1360 ret = tb_port_read(port, &val, TB_CFG_PORT,
1361 port->cap_usb4 + PORT_CS_1, 1);
1362 if (ret)
1363 return ret;
1364
1365 if (val & PORT_CS_1_NR)
1366 return -ENODEV;
1367 if (val & PORT_CS_1_RC)
1368 return -EIO;
1369
1370 return 0;
1371 }
1372
usb4_port_sb_opcode_err_to_errno(u32 val)1373 static int usb4_port_sb_opcode_err_to_errno(u32 val)
1374 {
1375 switch (val) {
1376 case 0:
1377 return 0;
1378 case USB4_SB_OPCODE_ERR:
1379 return -EAGAIN;
1380 case USB4_SB_OPCODE_ONS:
1381 return -EOPNOTSUPP;
1382 default:
1383 return -EIO;
1384 }
1385 }
1386
usb4_port_sb_op(struct tb_port * port,enum usb4_sb_target target,u8 index,enum usb4_sb_opcode opcode,int timeout_msec)1387 static int usb4_port_sb_op(struct tb_port *port, enum usb4_sb_target target,
1388 u8 index, enum usb4_sb_opcode opcode, int timeout_msec)
1389 {
1390 ktime_t timeout;
1391 u32 val;
1392 int ret;
1393
1394 val = opcode;
1395 ret = usb4_port_sb_write(port, target, index, USB4_SB_OPCODE, &val,
1396 sizeof(val));
1397 if (ret)
1398 return ret;
1399
1400 timeout = ktime_add_ms(ktime_get(), timeout_msec);
1401
1402 do {
1403 /* Check results */
1404 ret = usb4_port_sb_read(port, target, index, USB4_SB_OPCODE,
1405 &val, sizeof(val));
1406 if (ret)
1407 return ret;
1408
1409 if (val != opcode)
1410 return usb4_port_sb_opcode_err_to_errno(val);
1411 } while (ktime_before(ktime_get(), timeout));
1412
1413 return -ETIMEDOUT;
1414 }
1415
usb4_port_set_router_offline(struct tb_port * port,bool offline)1416 static int usb4_port_set_router_offline(struct tb_port *port, bool offline)
1417 {
1418 u32 val = !offline;
1419 int ret;
1420
1421 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1422 USB4_SB_METADATA, &val, sizeof(val));
1423 if (ret)
1424 return ret;
1425
1426 val = USB4_SB_OPCODE_ROUTER_OFFLINE;
1427 return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1428 USB4_SB_OPCODE, &val, sizeof(val));
1429 }
1430
1431 /**
1432 * usb4_port_router_offline() - Put the USB4 port to offline mode
1433 * @port: USB4 port
1434 *
1435 * This function puts the USB4 port into offline mode. In this mode the
1436 * port does not react on hotplug events anymore. This needs to be
1437 * called before retimer access is done when the USB4 links is not up.
1438 *
1439 * Returns %0 in case of success and negative errno if there was an
1440 * error.
1441 */
usb4_port_router_offline(struct tb_port * port)1442 int usb4_port_router_offline(struct tb_port *port)
1443 {
1444 return usb4_port_set_router_offline(port, true);
1445 }
1446
1447 /**
1448 * usb4_port_router_online() - Put the USB4 port back to online
1449 * @port: USB4 port
1450 *
1451 * Makes the USB4 port functional again.
1452 */
usb4_port_router_online(struct tb_port * port)1453 int usb4_port_router_online(struct tb_port *port)
1454 {
1455 return usb4_port_set_router_offline(port, false);
1456 }
1457
1458 /**
1459 * usb4_port_enumerate_retimers() - Send RT broadcast transaction
1460 * @port: USB4 port
1461 *
1462 * This forces the USB4 port to send broadcast RT transaction which
1463 * makes the retimers on the link to assign index to themselves. Returns
1464 * %0 in case of success and negative errno if there was an error.
1465 */
usb4_port_enumerate_retimers(struct tb_port * port)1466 int usb4_port_enumerate_retimers(struct tb_port *port)
1467 {
1468 u32 val;
1469
1470 val = USB4_SB_OPCODE_ENUMERATE_RETIMERS;
1471 return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1472 USB4_SB_OPCODE, &val, sizeof(val));
1473 }
1474
1475 /**
1476 * usb4_port_clx_supported() - Check if CLx is supported by the link
1477 * @port: Port to check for CLx support for
1478 *
1479 * PORT_CS_18_CPS bit reflects if the link supports CLx including
1480 * active cables (if connected on the link).
1481 */
usb4_port_clx_supported(struct tb_port * port)1482 bool usb4_port_clx_supported(struct tb_port *port)
1483 {
1484 int ret;
1485 u32 val;
1486
1487 ret = tb_port_read(port, &val, TB_CFG_PORT,
1488 port->cap_usb4 + PORT_CS_18, 1);
1489 if (ret)
1490 return false;
1491
1492 return !!(val & PORT_CS_18_CPS);
1493 }
1494
1495 /**
1496 * usb4_port_asym_supported() - If the port supports asymmetric link
1497 * @port: USB4 port
1498 *
1499 * Checks if the port and the cable supports asymmetric link and returns
1500 * %true in that case.
1501 */
usb4_port_asym_supported(struct tb_port * port)1502 bool usb4_port_asym_supported(struct tb_port *port)
1503 {
1504 u32 val;
1505
1506 if (!port->cap_usb4)
1507 return false;
1508
1509 if (tb_port_read(port, &val, TB_CFG_PORT, port->cap_usb4 + PORT_CS_18, 1))
1510 return false;
1511
1512 return !!(val & PORT_CS_18_CSA);
1513 }
1514
1515 /**
1516 * usb4_port_asym_set_link_width() - Set link width to asymmetric or symmetric
1517 * @port: USB4 port
1518 * @width: Asymmetric width to configure
1519 *
1520 * Sets USB4 port link width to @width. Can be called for widths where
1521 * usb4_port_asym_width_supported() returned @true.
1522 */
usb4_port_asym_set_link_width(struct tb_port * port,enum tb_link_width width)1523 int usb4_port_asym_set_link_width(struct tb_port *port, enum tb_link_width width)
1524 {
1525 u32 val;
1526 int ret;
1527
1528 if (!port->cap_phy)
1529 return -EINVAL;
1530
1531 ret = tb_port_read(port, &val, TB_CFG_PORT,
1532 port->cap_phy + LANE_ADP_CS_1, 1);
1533 if (ret)
1534 return ret;
1535
1536 val &= ~LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK;
1537 switch (width) {
1538 case TB_LINK_WIDTH_DUAL:
1539 val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
1540 LANE_ADP_CS_1_TARGET_WIDTH_ASYM_DUAL);
1541 break;
1542 case TB_LINK_WIDTH_ASYM_TX:
1543 val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
1544 LANE_ADP_CS_1_TARGET_WIDTH_ASYM_TX);
1545 break;
1546 case TB_LINK_WIDTH_ASYM_RX:
1547 val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
1548 LANE_ADP_CS_1_TARGET_WIDTH_ASYM_RX);
1549 break;
1550 default:
1551 return -EINVAL;
1552 }
1553
1554 return tb_port_write(port, &val, TB_CFG_PORT,
1555 port->cap_phy + LANE_ADP_CS_1, 1);
1556 }
1557
1558 /**
1559 * usb4_port_asym_start() - Start symmetry change and wait for completion
1560 * @port: USB4 port
1561 *
1562 * Start symmetry change of the link to asymmetric or symmetric
1563 * (according to what was previously set in tb_port_set_link_width().
1564 * Wait for completion of the change.
1565 *
1566 * Returns %0 in case of success, %-ETIMEDOUT if case of timeout or
1567 * a negative errno in case of a failure.
1568 */
usb4_port_asym_start(struct tb_port * port)1569 int usb4_port_asym_start(struct tb_port *port)
1570 {
1571 int ret;
1572 u32 val;
1573
1574 ret = tb_port_read(port, &val, TB_CFG_PORT,
1575 port->cap_usb4 + PORT_CS_19, 1);
1576 if (ret)
1577 return ret;
1578
1579 val &= ~PORT_CS_19_START_ASYM;
1580 val |= FIELD_PREP(PORT_CS_19_START_ASYM, 1);
1581
1582 ret = tb_port_write(port, &val, TB_CFG_PORT,
1583 port->cap_usb4 + PORT_CS_19, 1);
1584 if (ret)
1585 return ret;
1586
1587 /*
1588 * Wait for PORT_CS_19_START_ASYM to be 0. This means the USB4
1589 * port started the symmetry transition.
1590 */
1591 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_19,
1592 PORT_CS_19_START_ASYM, 0, 1000);
1593 if (ret)
1594 return ret;
1595
1596 /* Then wait for the transtion to be completed */
1597 return usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_18,
1598 PORT_CS_18_TIP, 0, 5000);
1599 }
1600
1601 /**
1602 * usb4_port_margining_caps() - Read USB4 port marginig capabilities
1603 * @port: USB4 port
1604 * @caps: Array with at least two elements to hold the results
1605 *
1606 * Reads the USB4 port lane margining capabilities into @caps.
1607 */
usb4_port_margining_caps(struct tb_port * port,u32 * caps)1608 int usb4_port_margining_caps(struct tb_port *port, u32 *caps)
1609 {
1610 int ret;
1611
1612 ret = usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1613 USB4_SB_OPCODE_READ_LANE_MARGINING_CAP, 500);
1614 if (ret)
1615 return ret;
1616
1617 return usb4_port_sb_read(port, USB4_SB_TARGET_ROUTER, 0,
1618 USB4_SB_DATA, caps, sizeof(*caps) * 2);
1619 }
1620
1621 /**
1622 * usb4_port_hw_margin() - Run hardware lane margining on port
1623 * @port: USB4 port
1624 * @lanes: Which lanes to run (must match the port capabilities). Can be
1625 * %0, %1 or %7.
1626 * @ber_level: BER level contour value
1627 * @timing: Perform timing margining instead of voltage
1628 * @right_high: Use Right/high margin instead of left/low
1629 * @results: Array with at least two elements to hold the results
1630 *
1631 * Runs hardware lane margining on USB4 port and returns the result in
1632 * @results.
1633 */
usb4_port_hw_margin(struct tb_port * port,unsigned int lanes,unsigned int ber_level,bool timing,bool right_high,u32 * results)1634 int usb4_port_hw_margin(struct tb_port *port, unsigned int lanes,
1635 unsigned int ber_level, bool timing, bool right_high,
1636 u32 *results)
1637 {
1638 u32 val;
1639 int ret;
1640
1641 val = lanes;
1642 if (timing)
1643 val |= USB4_MARGIN_HW_TIME;
1644 if (right_high)
1645 val |= USB4_MARGIN_HW_RH;
1646 if (ber_level)
1647 val |= (ber_level << USB4_MARGIN_HW_BER_SHIFT) &
1648 USB4_MARGIN_HW_BER_MASK;
1649
1650 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1651 USB4_SB_METADATA, &val, sizeof(val));
1652 if (ret)
1653 return ret;
1654
1655 ret = usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1656 USB4_SB_OPCODE_RUN_HW_LANE_MARGINING, 2500);
1657 if (ret)
1658 return ret;
1659
1660 return usb4_port_sb_read(port, USB4_SB_TARGET_ROUTER, 0,
1661 USB4_SB_DATA, results, sizeof(*results) * 2);
1662 }
1663
1664 /**
1665 * usb4_port_sw_margin() - Run software lane margining on port
1666 * @port: USB4 port
1667 * @lanes: Which lanes to run (must match the port capabilities). Can be
1668 * %0, %1 or %7.
1669 * @timing: Perform timing margining instead of voltage
1670 * @right_high: Use Right/high margin instead of left/low
1671 * @counter: What to do with the error counter
1672 *
1673 * Runs software lane margining on USB4 port. Read back the error
1674 * counters by calling usb4_port_sw_margin_errors(). Returns %0 in
1675 * success and negative errno otherwise.
1676 */
usb4_port_sw_margin(struct tb_port * port,unsigned int lanes,bool timing,bool right_high,u32 counter)1677 int usb4_port_sw_margin(struct tb_port *port, unsigned int lanes, bool timing,
1678 bool right_high, u32 counter)
1679 {
1680 u32 val;
1681 int ret;
1682
1683 val = lanes;
1684 if (timing)
1685 val |= USB4_MARGIN_SW_TIME;
1686 if (right_high)
1687 val |= USB4_MARGIN_SW_RH;
1688 val |= (counter << USB4_MARGIN_SW_COUNTER_SHIFT) &
1689 USB4_MARGIN_SW_COUNTER_MASK;
1690
1691 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1692 USB4_SB_METADATA, &val, sizeof(val));
1693 if (ret)
1694 return ret;
1695
1696 return usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1697 USB4_SB_OPCODE_RUN_SW_LANE_MARGINING, 2500);
1698 }
1699
1700 /**
1701 * usb4_port_sw_margin_errors() - Read the software margining error counters
1702 * @port: USB4 port
1703 * @errors: Error metadata is copied here.
1704 *
1705 * This reads back the software margining error counters from the port.
1706 * Returns %0 in success and negative errno otherwise.
1707 */
usb4_port_sw_margin_errors(struct tb_port * port,u32 * errors)1708 int usb4_port_sw_margin_errors(struct tb_port *port, u32 *errors)
1709 {
1710 int ret;
1711
1712 ret = usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1713 USB4_SB_OPCODE_READ_SW_MARGIN_ERR, 150);
1714 if (ret)
1715 return ret;
1716
1717 return usb4_port_sb_read(port, USB4_SB_TARGET_ROUTER, 0,
1718 USB4_SB_METADATA, errors, sizeof(*errors));
1719 }
1720
usb4_port_retimer_op(struct tb_port * port,u8 index,enum usb4_sb_opcode opcode,int timeout_msec)1721 static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
1722 enum usb4_sb_opcode opcode,
1723 int timeout_msec)
1724 {
1725 return usb4_port_sb_op(port, USB4_SB_TARGET_RETIMER, index, opcode,
1726 timeout_msec);
1727 }
1728
1729 /**
1730 * usb4_port_retimer_set_inbound_sbtx() - Enable sideband channel transactions
1731 * @port: USB4 port
1732 * @index: Retimer index
1733 *
1734 * Enables sideband channel transations on SBTX. Can be used when USB4
1735 * link does not go up, for example if there is no device connected.
1736 */
usb4_port_retimer_set_inbound_sbtx(struct tb_port * port,u8 index)1737 int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index)
1738 {
1739 int ret;
1740
1741 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
1742 500);
1743
1744 if (ret != -ENODEV)
1745 return ret;
1746
1747 /*
1748 * Per the USB4 retimer spec, the retimer is not required to
1749 * send an RT (Retimer Transaction) response for the first
1750 * SET_INBOUND_SBTX command
1751 */
1752 return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
1753 500);
1754 }
1755
1756 /**
1757 * usb4_port_retimer_unset_inbound_sbtx() - Disable sideband channel transactions
1758 * @port: USB4 port
1759 * @index: Retimer index
1760 *
1761 * Disables sideband channel transations on SBTX. The reverse of
1762 * usb4_port_retimer_set_inbound_sbtx().
1763 */
usb4_port_retimer_unset_inbound_sbtx(struct tb_port * port,u8 index)1764 int usb4_port_retimer_unset_inbound_sbtx(struct tb_port *port, u8 index)
1765 {
1766 return usb4_port_retimer_op(port, index,
1767 USB4_SB_OPCODE_UNSET_INBOUND_SBTX, 500);
1768 }
1769
1770 /**
1771 * usb4_port_retimer_read() - Read from retimer sideband registers
1772 * @port: USB4 port
1773 * @index: Retimer index
1774 * @reg: Sideband register to read
1775 * @buf: Data from @reg is stored here
1776 * @size: Number of bytes to read
1777 *
1778 * Function reads retimer sideband registers starting from @reg. The
1779 * retimer is connected to @port at @index. Returns %0 in case of
1780 * success, and read data is copied to @buf. If there is no retimer
1781 * present at given @index returns %-ENODEV. In any other failure
1782 * returns negative errno.
1783 */
usb4_port_retimer_read(struct tb_port * port,u8 index,u8 reg,void * buf,u8 size)1784 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1785 u8 size)
1786 {
1787 return usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1788 size);
1789 }
1790
1791 /**
1792 * usb4_port_retimer_write() - Write to retimer sideband registers
1793 * @port: USB4 port
1794 * @index: Retimer index
1795 * @reg: Sideband register to write
1796 * @buf: Data that is written starting from @reg
1797 * @size: Number of bytes to write
1798 *
1799 * Writes retimer sideband registers starting from @reg. The retimer is
1800 * connected to @port at @index. Returns %0 in case of success. If there
1801 * is no retimer present at given @index returns %-ENODEV. In any other
1802 * failure returns negative errno.
1803 */
usb4_port_retimer_write(struct tb_port * port,u8 index,u8 reg,const void * buf,u8 size)1804 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1805 const void *buf, u8 size)
1806 {
1807 return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1808 size);
1809 }
1810
1811 /**
1812 * usb4_port_retimer_is_last() - Is the retimer last on-board retimer
1813 * @port: USB4 port
1814 * @index: Retimer index
1815 *
1816 * If the retimer at @index is last one (connected directly to the
1817 * Type-C port) this function returns %1. If it is not returns %0. If
1818 * the retimer is not present returns %-ENODEV. Otherwise returns
1819 * negative errno.
1820 */
usb4_port_retimer_is_last(struct tb_port * port,u8 index)1821 int usb4_port_retimer_is_last(struct tb_port *port, u8 index)
1822 {
1823 u32 metadata;
1824 int ret;
1825
1826 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_LAST_RETIMER,
1827 500);
1828 if (ret)
1829 return ret;
1830
1831 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1832 sizeof(metadata));
1833 return ret ? ret : metadata & 1;
1834 }
1835
1836 /**
1837 * usb4_port_retimer_nvm_sector_size() - Read retimer NVM sector size
1838 * @port: USB4 port
1839 * @index: Retimer index
1840 *
1841 * Reads NVM sector size (in bytes) of a retimer at @index. This
1842 * operation can be used to determine whether the retimer supports NVM
1843 * upgrade for example. Returns sector size in bytes or negative errno
1844 * in case of error. Specifically returns %-ENODEV if there is no
1845 * retimer at @index.
1846 */
usb4_port_retimer_nvm_sector_size(struct tb_port * port,u8 index)1847 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
1848 {
1849 u32 metadata;
1850 int ret;
1851
1852 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE,
1853 500);
1854 if (ret)
1855 return ret;
1856
1857 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1858 sizeof(metadata));
1859 return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
1860 }
1861
1862 /**
1863 * usb4_port_retimer_nvm_set_offset() - Set NVM write offset
1864 * @port: USB4 port
1865 * @index: Retimer index
1866 * @address: Start offset
1867 *
1868 * Exlicitly sets NVM write offset. Normally when writing to NVM this is
1869 * done automatically by usb4_port_retimer_nvm_write().
1870 *
1871 * Returns %0 in success and negative errno if there was a failure.
1872 */
usb4_port_retimer_nvm_set_offset(struct tb_port * port,u8 index,unsigned int address)1873 int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1874 unsigned int address)
1875 {
1876 u32 metadata, dwaddress;
1877 int ret;
1878
1879 dwaddress = address / 4;
1880 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
1881 USB4_NVM_SET_OFFSET_MASK;
1882
1883 ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1884 sizeof(metadata));
1885 if (ret)
1886 return ret;
1887
1888 return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_SET_OFFSET,
1889 500);
1890 }
1891
1892 struct retimer_info {
1893 struct tb_port *port;
1894 u8 index;
1895 };
1896
usb4_port_retimer_nvm_write_next_block(void * data,unsigned int dwaddress,const void * buf,size_t dwords)1897 static int usb4_port_retimer_nvm_write_next_block(void *data,
1898 unsigned int dwaddress, const void *buf, size_t dwords)
1899
1900 {
1901 const struct retimer_info *info = data;
1902 struct tb_port *port = info->port;
1903 u8 index = info->index;
1904 int ret;
1905
1906 ret = usb4_port_retimer_write(port, index, USB4_SB_DATA,
1907 buf, dwords * 4);
1908 if (ret)
1909 return ret;
1910
1911 return usb4_port_retimer_op(port, index,
1912 USB4_SB_OPCODE_NVM_BLOCK_WRITE, 1000);
1913 }
1914
1915 /**
1916 * usb4_port_retimer_nvm_write() - Write to retimer NVM
1917 * @port: USB4 port
1918 * @index: Retimer index
1919 * @address: Byte address where to start the write
1920 * @buf: Data to write
1921 * @size: Size in bytes how much to write
1922 *
1923 * Writes @size bytes from @buf to the retimer NVM. Used for NVM
1924 * upgrade. Returns %0 if the data was written successfully and negative
1925 * errno in case of failure. Specifically returns %-ENODEV if there is
1926 * no retimer at @index.
1927 */
usb4_port_retimer_nvm_write(struct tb_port * port,u8 index,unsigned int address,const void * buf,size_t size)1928 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index, unsigned int address,
1929 const void *buf, size_t size)
1930 {
1931 struct retimer_info info = { .port = port, .index = index };
1932 int ret;
1933
1934 ret = usb4_port_retimer_nvm_set_offset(port, index, address);
1935 if (ret)
1936 return ret;
1937
1938 return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
1939 usb4_port_retimer_nvm_write_next_block, &info);
1940 }
1941
1942 /**
1943 * usb4_port_retimer_nvm_authenticate() - Start retimer NVM upgrade
1944 * @port: USB4 port
1945 * @index: Retimer index
1946 *
1947 * After the new NVM image has been written via usb4_port_retimer_nvm_write()
1948 * this function can be used to trigger the NVM upgrade process. If
1949 * successful the retimer restarts with the new NVM and may not have the
1950 * index set so one needs to call usb4_port_enumerate_retimers() to
1951 * force index to be assigned.
1952 */
usb4_port_retimer_nvm_authenticate(struct tb_port * port,u8 index)1953 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index)
1954 {
1955 u32 val;
1956
1957 /*
1958 * We need to use the raw operation here because once the
1959 * authentication completes the retimer index is not set anymore
1960 * so we do not get back the status now.
1961 */
1962 val = USB4_SB_OPCODE_NVM_AUTH_WRITE;
1963 return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1964 USB4_SB_OPCODE, &val, sizeof(val));
1965 }
1966
1967 /**
1968 * usb4_port_retimer_nvm_authenticate_status() - Read status of NVM upgrade
1969 * @port: USB4 port
1970 * @index: Retimer index
1971 * @status: Raw status code read from metadata
1972 *
1973 * This can be called after usb4_port_retimer_nvm_authenticate() and
1974 * usb4_port_enumerate_retimers() to fetch status of the NVM upgrade.
1975 *
1976 * Returns %0 if the authentication status was successfully read. The
1977 * completion metadata (the result) is then stored into @status. If
1978 * reading the status fails, returns negative errno.
1979 */
usb4_port_retimer_nvm_authenticate_status(struct tb_port * port,u8 index,u32 * status)1980 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1981 u32 *status)
1982 {
1983 u32 metadata, val;
1984 int ret;
1985
1986 ret = usb4_port_retimer_read(port, index, USB4_SB_OPCODE, &val,
1987 sizeof(val));
1988 if (ret)
1989 return ret;
1990
1991 ret = usb4_port_sb_opcode_err_to_errno(val);
1992 switch (ret) {
1993 case 0:
1994 *status = 0;
1995 return 0;
1996
1997 case -EAGAIN:
1998 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA,
1999 &metadata, sizeof(metadata));
2000 if (ret)
2001 return ret;
2002
2003 *status = metadata & USB4_SB_METADATA_NVM_AUTH_WRITE_MASK;
2004 return 0;
2005
2006 default:
2007 return ret;
2008 }
2009 }
2010
usb4_port_retimer_nvm_read_block(void * data,unsigned int dwaddress,void * buf,size_t dwords)2011 static int usb4_port_retimer_nvm_read_block(void *data, unsigned int dwaddress,
2012 void *buf, size_t dwords)
2013 {
2014 const struct retimer_info *info = data;
2015 struct tb_port *port = info->port;
2016 u8 index = info->index;
2017 u32 metadata;
2018 int ret;
2019
2020 metadata = dwaddress << USB4_NVM_READ_OFFSET_SHIFT;
2021 if (dwords < USB4_DATA_DWORDS)
2022 metadata |= dwords << USB4_NVM_READ_LENGTH_SHIFT;
2023
2024 ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
2025 sizeof(metadata));
2026 if (ret)
2027 return ret;
2028
2029 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_READ, 500);
2030 if (ret)
2031 return ret;
2032
2033 return usb4_port_retimer_read(port, index, USB4_SB_DATA, buf,
2034 dwords * 4);
2035 }
2036
2037 /**
2038 * usb4_port_retimer_nvm_read() - Read contents of retimer NVM
2039 * @port: USB4 port
2040 * @index: Retimer index
2041 * @address: NVM address (in bytes) to start reading
2042 * @buf: Data read from NVM is stored here
2043 * @size: Number of bytes to read
2044 *
2045 * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
2046 * read was successful and negative errno in case of failure.
2047 * Specifically returns %-ENODEV if there is no retimer at @index.
2048 */
usb4_port_retimer_nvm_read(struct tb_port * port,u8 index,unsigned int address,void * buf,size_t size)2049 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
2050 unsigned int address, void *buf, size_t size)
2051 {
2052 struct retimer_info info = { .port = port, .index = index };
2053
2054 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
2055 usb4_port_retimer_nvm_read_block, &info);
2056 }
2057
2058 static inline unsigned int
usb4_usb3_port_max_bandwidth(const struct tb_port * port,unsigned int bw)2059 usb4_usb3_port_max_bandwidth(const struct tb_port *port, unsigned int bw)
2060 {
2061 /* Take the possible bandwidth limitation into account */
2062 if (port->max_bw)
2063 return min(bw, port->max_bw);
2064 return bw;
2065 }
2066
2067 /**
2068 * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate
2069 * @port: USB3 adapter port
2070 *
2071 * Return maximum supported link rate of a USB3 adapter in Mb/s.
2072 * Negative errno in case of error.
2073 */
usb4_usb3_port_max_link_rate(struct tb_port * port)2074 int usb4_usb3_port_max_link_rate(struct tb_port *port)
2075 {
2076 int ret, lr;
2077 u32 val;
2078
2079 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
2080 return -EINVAL;
2081
2082 ret = tb_port_read(port, &val, TB_CFG_PORT,
2083 port->cap_adap + ADP_USB3_CS_4, 1);
2084 if (ret)
2085 return ret;
2086
2087 lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
2088 ret = lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
2089
2090 return usb4_usb3_port_max_bandwidth(port, ret);
2091 }
2092
2093 /**
2094 * usb4_usb3_port_actual_link_rate() - Established USB3 link rate
2095 * @port: USB3 adapter port
2096 *
2097 * Return actual established link rate of a USB3 adapter in Mb/s. If the
2098 * link is not up returns %0 and negative errno in case of failure.
2099 */
usb4_usb3_port_actual_link_rate(struct tb_port * port)2100 int usb4_usb3_port_actual_link_rate(struct tb_port *port)
2101 {
2102 int ret, lr;
2103 u32 val;
2104
2105 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
2106 return -EINVAL;
2107
2108 ret = tb_port_read(port, &val, TB_CFG_PORT,
2109 port->cap_adap + ADP_USB3_CS_4, 1);
2110 if (ret)
2111 return ret;
2112
2113 if (!(val & ADP_USB3_CS_4_ULV))
2114 return 0;
2115
2116 lr = val & ADP_USB3_CS_4_ALR_MASK;
2117 ret = lr == ADP_USB3_CS_4_ALR_20G ? 20000 : 10000;
2118
2119 return usb4_usb3_port_max_bandwidth(port, ret);
2120 }
2121
usb4_usb3_port_cm_request(struct tb_port * port,bool request)2122 static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
2123 {
2124 int ret;
2125 u32 val;
2126
2127 if (!tb_port_is_usb3_down(port))
2128 return -EINVAL;
2129 if (tb_route(port->sw))
2130 return -EINVAL;
2131
2132 ret = tb_port_read(port, &val, TB_CFG_PORT,
2133 port->cap_adap + ADP_USB3_CS_2, 1);
2134 if (ret)
2135 return ret;
2136
2137 if (request)
2138 val |= ADP_USB3_CS_2_CMR;
2139 else
2140 val &= ~ADP_USB3_CS_2_CMR;
2141
2142 ret = tb_port_write(port, &val, TB_CFG_PORT,
2143 port->cap_adap + ADP_USB3_CS_2, 1);
2144 if (ret)
2145 return ret;
2146
2147 /*
2148 * We can use val here directly as the CMR bit is in the same place
2149 * as HCA. Just mask out others.
2150 */
2151 val &= ADP_USB3_CS_2_CMR;
2152 return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
2153 ADP_USB3_CS_1_HCA, val, 1500);
2154 }
2155
usb4_usb3_port_set_cm_request(struct tb_port * port)2156 static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
2157 {
2158 return usb4_usb3_port_cm_request(port, true);
2159 }
2160
usb4_usb3_port_clear_cm_request(struct tb_port * port)2161 static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
2162 {
2163 return usb4_usb3_port_cm_request(port, false);
2164 }
2165
usb3_bw_to_mbps(u32 bw,u8 scale)2166 static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
2167 {
2168 unsigned long uframes;
2169
2170 uframes = bw * 512UL << scale;
2171 return DIV_ROUND_CLOSEST(uframes * 8000, MEGA);
2172 }
2173
mbps_to_usb3_bw(unsigned int mbps,u8 scale)2174 static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
2175 {
2176 unsigned long uframes;
2177
2178 /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
2179 uframes = ((unsigned long)mbps * MEGA) / 8000;
2180 return DIV_ROUND_UP(uframes, 512UL << scale);
2181 }
2182
usb4_usb3_port_read_allocated_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)2183 static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
2184 int *upstream_bw,
2185 int *downstream_bw)
2186 {
2187 u32 val, bw, scale;
2188 int ret;
2189
2190 ret = tb_port_read(port, &val, TB_CFG_PORT,
2191 port->cap_adap + ADP_USB3_CS_2, 1);
2192 if (ret)
2193 return ret;
2194
2195 ret = tb_port_read(port, &scale, TB_CFG_PORT,
2196 port->cap_adap + ADP_USB3_CS_3, 1);
2197 if (ret)
2198 return ret;
2199
2200 scale &= ADP_USB3_CS_3_SCALE_MASK;
2201
2202 bw = val & ADP_USB3_CS_2_AUBW_MASK;
2203 *upstream_bw = usb3_bw_to_mbps(bw, scale);
2204
2205 bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
2206 *downstream_bw = usb3_bw_to_mbps(bw, scale);
2207
2208 return 0;
2209 }
2210
2211 /**
2212 * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
2213 * @port: USB3 adapter port
2214 * @upstream_bw: Allocated upstream bandwidth is stored here
2215 * @downstream_bw: Allocated downstream bandwidth is stored here
2216 *
2217 * Stores currently allocated USB3 bandwidth into @upstream_bw and
2218 * @downstream_bw in Mb/s. Returns %0 in case of success and negative
2219 * errno in failure.
2220 */
usb4_usb3_port_allocated_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)2221 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
2222 int *downstream_bw)
2223 {
2224 int ret;
2225
2226 ret = usb4_usb3_port_set_cm_request(port);
2227 if (ret)
2228 return ret;
2229
2230 ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
2231 downstream_bw);
2232 usb4_usb3_port_clear_cm_request(port);
2233
2234 return ret;
2235 }
2236
usb4_usb3_port_read_consumed_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)2237 static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
2238 int *upstream_bw,
2239 int *downstream_bw)
2240 {
2241 u32 val, bw, scale;
2242 int ret;
2243
2244 ret = tb_port_read(port, &val, TB_CFG_PORT,
2245 port->cap_adap + ADP_USB3_CS_1, 1);
2246 if (ret)
2247 return ret;
2248
2249 ret = tb_port_read(port, &scale, TB_CFG_PORT,
2250 port->cap_adap + ADP_USB3_CS_3, 1);
2251 if (ret)
2252 return ret;
2253
2254 scale &= ADP_USB3_CS_3_SCALE_MASK;
2255
2256 bw = val & ADP_USB3_CS_1_CUBW_MASK;
2257 *upstream_bw = usb3_bw_to_mbps(bw, scale);
2258
2259 bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
2260 *downstream_bw = usb3_bw_to_mbps(bw, scale);
2261
2262 return 0;
2263 }
2264
usb4_usb3_port_write_allocated_bandwidth(struct tb_port * port,int upstream_bw,int downstream_bw)2265 static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
2266 int upstream_bw,
2267 int downstream_bw)
2268 {
2269 u32 val, ubw, dbw, scale;
2270 int ret, max_bw;
2271
2272 /* Figure out suitable scale */
2273 scale = 0;
2274 max_bw = max(upstream_bw, downstream_bw);
2275 while (scale < 64) {
2276 if (mbps_to_usb3_bw(max_bw, scale) < 4096)
2277 break;
2278 scale++;
2279 }
2280
2281 if (WARN_ON(scale >= 64))
2282 return -EINVAL;
2283
2284 ret = tb_port_write(port, &scale, TB_CFG_PORT,
2285 port->cap_adap + ADP_USB3_CS_3, 1);
2286 if (ret)
2287 return ret;
2288
2289 ubw = mbps_to_usb3_bw(upstream_bw, scale);
2290 dbw = mbps_to_usb3_bw(downstream_bw, scale);
2291
2292 tb_port_dbg(port, "scaled bandwidth %u/%u, scale %u\n", ubw, dbw, scale);
2293
2294 ret = tb_port_read(port, &val, TB_CFG_PORT,
2295 port->cap_adap + ADP_USB3_CS_2, 1);
2296 if (ret)
2297 return ret;
2298
2299 val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
2300 val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
2301 val |= ubw;
2302
2303 return tb_port_write(port, &val, TB_CFG_PORT,
2304 port->cap_adap + ADP_USB3_CS_2, 1);
2305 }
2306
2307 /**
2308 * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
2309 * @port: USB3 adapter port
2310 * @upstream_bw: New upstream bandwidth
2311 * @downstream_bw: New downstream bandwidth
2312 *
2313 * This can be used to set how much bandwidth is allocated for the USB3
2314 * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
2315 * new values programmed to the USB3 adapter allocation registers. If
2316 * the values are lower than what is currently consumed the allocation
2317 * is set to what is currently consumed instead (consumed bandwidth
2318 * cannot be taken away by CM). The actual new values are returned in
2319 * @upstream_bw and @downstream_bw.
2320 *
2321 * Returns %0 in case of success and negative errno if there was a
2322 * failure.
2323 */
usb4_usb3_port_allocate_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)2324 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
2325 int *downstream_bw)
2326 {
2327 int ret, consumed_up, consumed_down, allocate_up, allocate_down;
2328
2329 ret = usb4_usb3_port_set_cm_request(port);
2330 if (ret)
2331 return ret;
2332
2333 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
2334 &consumed_down);
2335 if (ret)
2336 goto err_request;
2337
2338 /* Don't allow it go lower than what is consumed */
2339 allocate_up = max(*upstream_bw, consumed_up);
2340 allocate_down = max(*downstream_bw, consumed_down);
2341
2342 ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
2343 allocate_down);
2344 if (ret)
2345 goto err_request;
2346
2347 *upstream_bw = allocate_up;
2348 *downstream_bw = allocate_down;
2349
2350 err_request:
2351 usb4_usb3_port_clear_cm_request(port);
2352 return ret;
2353 }
2354
2355 /**
2356 * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
2357 * @port: USB3 adapter port
2358 * @upstream_bw: New allocated upstream bandwidth
2359 * @downstream_bw: New allocated downstream bandwidth
2360 *
2361 * Releases USB3 allocated bandwidth down to what is actually consumed.
2362 * The new bandwidth is returned in @upstream_bw and @downstream_bw.
2363 *
2364 * Returns 0% in success and negative errno in case of failure.
2365 */
usb4_usb3_port_release_bandwidth(struct tb_port * port,int * upstream_bw,int * downstream_bw)2366 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
2367 int *downstream_bw)
2368 {
2369 int ret, consumed_up, consumed_down;
2370
2371 ret = usb4_usb3_port_set_cm_request(port);
2372 if (ret)
2373 return ret;
2374
2375 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
2376 &consumed_down);
2377 if (ret)
2378 goto err_request;
2379
2380 /*
2381 * Always keep 900 Mb/s to make sure xHCI has at least some
2382 * bandwidth available for isochronous traffic.
2383 */
2384 if (consumed_up < 900)
2385 consumed_up = 900;
2386 if (consumed_down < 900)
2387 consumed_down = 900;
2388
2389 ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
2390 consumed_down);
2391 if (ret)
2392 goto err_request;
2393
2394 *upstream_bw = consumed_up;
2395 *downstream_bw = consumed_down;
2396
2397 err_request:
2398 usb4_usb3_port_clear_cm_request(port);
2399 return ret;
2400 }
2401
is_usb4_dpin(const struct tb_port * port)2402 static bool is_usb4_dpin(const struct tb_port *port)
2403 {
2404 if (!tb_port_is_dpin(port))
2405 return false;
2406 if (!tb_switch_is_usb4(port->sw))
2407 return false;
2408 return true;
2409 }
2410
2411 /**
2412 * usb4_dp_port_set_cm_id() - Assign CM ID to the DP IN adapter
2413 * @port: DP IN adapter
2414 * @cm_id: CM ID to assign
2415 *
2416 * Sets CM ID for the @port. Returns %0 on success and negative errno
2417 * otherwise. Speficially returns %-EOPNOTSUPP if the @port does not
2418 * support this.
2419 */
usb4_dp_port_set_cm_id(struct tb_port * port,int cm_id)2420 int usb4_dp_port_set_cm_id(struct tb_port *port, int cm_id)
2421 {
2422 u32 val;
2423 int ret;
2424
2425 if (!is_usb4_dpin(port))
2426 return -EOPNOTSUPP;
2427
2428 ret = tb_port_read(port, &val, TB_CFG_PORT,
2429 port->cap_adap + ADP_DP_CS_2, 1);
2430 if (ret)
2431 return ret;
2432
2433 val &= ~ADP_DP_CS_2_CM_ID_MASK;
2434 val |= cm_id << ADP_DP_CS_2_CM_ID_SHIFT;
2435
2436 return tb_port_write(port, &val, TB_CFG_PORT,
2437 port->cap_adap + ADP_DP_CS_2, 1);
2438 }
2439
2440 /**
2441 * usb4_dp_port_bandwidth_mode_supported() - Is the bandwidth allocation mode
2442 * supported
2443 * @port: DP IN adapter to check
2444 *
2445 * Can be called to any DP IN adapter. Returns true if the adapter
2446 * supports USB4 bandwidth allocation mode, false otherwise.
2447 */
usb4_dp_port_bandwidth_mode_supported(struct tb_port * port)2448 bool usb4_dp_port_bandwidth_mode_supported(struct tb_port *port)
2449 {
2450 int ret;
2451 u32 val;
2452
2453 if (!is_usb4_dpin(port))
2454 return false;
2455
2456 ret = tb_port_read(port, &val, TB_CFG_PORT,
2457 port->cap_adap + DP_LOCAL_CAP, 1);
2458 if (ret)
2459 return false;
2460
2461 return !!(val & DP_COMMON_CAP_BW_MODE);
2462 }
2463
2464 /**
2465 * usb4_dp_port_bandwidth_mode_enabled() - Is the bandwidth allocation mode
2466 * enabled
2467 * @port: DP IN adapter to check
2468 *
2469 * Can be called to any DP IN adapter. Returns true if the bandwidth
2470 * allocation mode has been enabled, false otherwise.
2471 */
usb4_dp_port_bandwidth_mode_enabled(struct tb_port * port)2472 bool usb4_dp_port_bandwidth_mode_enabled(struct tb_port *port)
2473 {
2474 int ret;
2475 u32 val;
2476
2477 if (!is_usb4_dpin(port))
2478 return false;
2479
2480 ret = tb_port_read(port, &val, TB_CFG_PORT,
2481 port->cap_adap + ADP_DP_CS_8, 1);
2482 if (ret)
2483 return false;
2484
2485 return !!(val & ADP_DP_CS_8_DPME);
2486 }
2487
2488 /**
2489 * usb4_dp_port_set_cm_bandwidth_mode_supported() - Set/clear CM support for
2490 * bandwidth allocation mode
2491 * @port: DP IN adapter
2492 * @supported: Does the CM support bandwidth allocation mode
2493 *
2494 * Can be called to any DP IN adapter. Sets or clears the CM support bit
2495 * of the DP IN adapter. Returns %0 in success and negative errno
2496 * otherwise. Specifically returns %-OPNOTSUPP if the passed in adapter
2497 * does not support this.
2498 */
usb4_dp_port_set_cm_bandwidth_mode_supported(struct tb_port * port,bool supported)2499 int usb4_dp_port_set_cm_bandwidth_mode_supported(struct tb_port *port,
2500 bool supported)
2501 {
2502 u32 val;
2503 int ret;
2504
2505 if (!is_usb4_dpin(port))
2506 return -EOPNOTSUPP;
2507
2508 ret = tb_port_read(port, &val, TB_CFG_PORT,
2509 port->cap_adap + ADP_DP_CS_2, 1);
2510 if (ret)
2511 return ret;
2512
2513 if (supported)
2514 val |= ADP_DP_CS_2_CMMS;
2515 else
2516 val &= ~ADP_DP_CS_2_CMMS;
2517
2518 return tb_port_write(port, &val, TB_CFG_PORT,
2519 port->cap_adap + ADP_DP_CS_2, 1);
2520 }
2521
2522 /**
2523 * usb4_dp_port_group_id() - Return Group ID assigned for the adapter
2524 * @port: DP IN adapter
2525 *
2526 * Reads bandwidth allocation Group ID from the DP IN adapter and
2527 * returns it. If the adapter does not support setting Group_ID
2528 * %-EOPNOTSUPP is returned.
2529 */
usb4_dp_port_group_id(struct tb_port * port)2530 int usb4_dp_port_group_id(struct tb_port *port)
2531 {
2532 u32 val;
2533 int ret;
2534
2535 if (!is_usb4_dpin(port))
2536 return -EOPNOTSUPP;
2537
2538 ret = tb_port_read(port, &val, TB_CFG_PORT,
2539 port->cap_adap + ADP_DP_CS_2, 1);
2540 if (ret)
2541 return ret;
2542
2543 return (val & ADP_DP_CS_2_GROUP_ID_MASK) >> ADP_DP_CS_2_GROUP_ID_SHIFT;
2544 }
2545
2546 /**
2547 * usb4_dp_port_set_group_id() - Set adapter Group ID
2548 * @port: DP IN adapter
2549 * @group_id: Group ID for the adapter
2550 *
2551 * Sets bandwidth allocation mode Group ID for the DP IN adapter.
2552 * Returns %0 in case of success and negative errno otherwise.
2553 * Specifically returns %-EOPNOTSUPP if the adapter does not support
2554 * this.
2555 */
usb4_dp_port_set_group_id(struct tb_port * port,int group_id)2556 int usb4_dp_port_set_group_id(struct tb_port *port, int group_id)
2557 {
2558 u32 val;
2559 int ret;
2560
2561 if (!is_usb4_dpin(port))
2562 return -EOPNOTSUPP;
2563
2564 ret = tb_port_read(port, &val, TB_CFG_PORT,
2565 port->cap_adap + ADP_DP_CS_2, 1);
2566 if (ret)
2567 return ret;
2568
2569 val &= ~ADP_DP_CS_2_GROUP_ID_MASK;
2570 val |= group_id << ADP_DP_CS_2_GROUP_ID_SHIFT;
2571
2572 return tb_port_write(port, &val, TB_CFG_PORT,
2573 port->cap_adap + ADP_DP_CS_2, 1);
2574 }
2575
2576 /**
2577 * usb4_dp_port_nrd() - Read non-reduced rate and lanes
2578 * @port: DP IN adapter
2579 * @rate: Non-reduced rate in Mb/s is placed here
2580 * @lanes: Non-reduced lanes are placed here
2581 *
2582 * Reads the non-reduced rate and lanes from the DP IN adapter. Returns
2583 * %0 in success and negative errno otherwise. Specifically returns
2584 * %-EOPNOTSUPP if the adapter does not support this.
2585 */
usb4_dp_port_nrd(struct tb_port * port,int * rate,int * lanes)2586 int usb4_dp_port_nrd(struct tb_port *port, int *rate, int *lanes)
2587 {
2588 u32 val, tmp;
2589 int ret;
2590
2591 if (!is_usb4_dpin(port))
2592 return -EOPNOTSUPP;
2593
2594 ret = tb_port_read(port, &val, TB_CFG_PORT,
2595 port->cap_adap + ADP_DP_CS_2, 1);
2596 if (ret)
2597 return ret;
2598
2599 tmp = (val & ADP_DP_CS_2_NRD_MLR_MASK) >> ADP_DP_CS_2_NRD_MLR_SHIFT;
2600 switch (tmp) {
2601 case DP_COMMON_CAP_RATE_RBR:
2602 *rate = 1620;
2603 break;
2604 case DP_COMMON_CAP_RATE_HBR:
2605 *rate = 2700;
2606 break;
2607 case DP_COMMON_CAP_RATE_HBR2:
2608 *rate = 5400;
2609 break;
2610 case DP_COMMON_CAP_RATE_HBR3:
2611 *rate = 8100;
2612 break;
2613 }
2614
2615 tmp = val & ADP_DP_CS_2_NRD_MLC_MASK;
2616 switch (tmp) {
2617 case DP_COMMON_CAP_1_LANE:
2618 *lanes = 1;
2619 break;
2620 case DP_COMMON_CAP_2_LANES:
2621 *lanes = 2;
2622 break;
2623 case DP_COMMON_CAP_4_LANES:
2624 *lanes = 4;
2625 break;
2626 }
2627
2628 return 0;
2629 }
2630
2631 /**
2632 * usb4_dp_port_set_nrd() - Set non-reduced rate and lanes
2633 * @port: DP IN adapter
2634 * @rate: Non-reduced rate in Mb/s
2635 * @lanes: Non-reduced lanes
2636 *
2637 * Before the capabilities reduction this function can be used to set
2638 * the non-reduced values for the DP IN adapter. Returns %0 in success
2639 * and negative errno otherwise. If the adapter does not support this
2640 * %-EOPNOTSUPP is returned.
2641 */
usb4_dp_port_set_nrd(struct tb_port * port,int rate,int lanes)2642 int usb4_dp_port_set_nrd(struct tb_port *port, int rate, int lanes)
2643 {
2644 u32 val;
2645 int ret;
2646
2647 if (!is_usb4_dpin(port))
2648 return -EOPNOTSUPP;
2649
2650 ret = tb_port_read(port, &val, TB_CFG_PORT,
2651 port->cap_adap + ADP_DP_CS_2, 1);
2652 if (ret)
2653 return ret;
2654
2655 val &= ~ADP_DP_CS_2_NRD_MLR_MASK;
2656
2657 switch (rate) {
2658 case 1620:
2659 break;
2660 case 2700:
2661 val |= (DP_COMMON_CAP_RATE_HBR << ADP_DP_CS_2_NRD_MLR_SHIFT)
2662 & ADP_DP_CS_2_NRD_MLR_MASK;
2663 break;
2664 case 5400:
2665 val |= (DP_COMMON_CAP_RATE_HBR2 << ADP_DP_CS_2_NRD_MLR_SHIFT)
2666 & ADP_DP_CS_2_NRD_MLR_MASK;
2667 break;
2668 case 8100:
2669 val |= (DP_COMMON_CAP_RATE_HBR3 << ADP_DP_CS_2_NRD_MLR_SHIFT)
2670 & ADP_DP_CS_2_NRD_MLR_MASK;
2671 break;
2672 default:
2673 return -EINVAL;
2674 }
2675
2676 val &= ~ADP_DP_CS_2_NRD_MLC_MASK;
2677
2678 switch (lanes) {
2679 case 1:
2680 break;
2681 case 2:
2682 val |= DP_COMMON_CAP_2_LANES;
2683 break;
2684 case 4:
2685 val |= DP_COMMON_CAP_4_LANES;
2686 break;
2687 default:
2688 return -EINVAL;
2689 }
2690
2691 return tb_port_write(port, &val, TB_CFG_PORT,
2692 port->cap_adap + ADP_DP_CS_2, 1);
2693 }
2694
2695 /**
2696 * usb4_dp_port_granularity() - Return granularity for the bandwidth values
2697 * @port: DP IN adapter
2698 *
2699 * Reads the programmed granularity from @port. If the DP IN adapter does
2700 * not support bandwidth allocation mode returns %-EOPNOTSUPP and negative
2701 * errno in other error cases.
2702 */
usb4_dp_port_granularity(struct tb_port * port)2703 int usb4_dp_port_granularity(struct tb_port *port)
2704 {
2705 u32 val;
2706 int ret;
2707
2708 if (!is_usb4_dpin(port))
2709 return -EOPNOTSUPP;
2710
2711 ret = tb_port_read(port, &val, TB_CFG_PORT,
2712 port->cap_adap + ADP_DP_CS_2, 1);
2713 if (ret)
2714 return ret;
2715
2716 val &= ADP_DP_CS_2_GR_MASK;
2717 val >>= ADP_DP_CS_2_GR_SHIFT;
2718
2719 switch (val) {
2720 case ADP_DP_CS_2_GR_0_25G:
2721 return 250;
2722 case ADP_DP_CS_2_GR_0_5G:
2723 return 500;
2724 case ADP_DP_CS_2_GR_1G:
2725 return 1000;
2726 }
2727
2728 return -EINVAL;
2729 }
2730
2731 /**
2732 * usb4_dp_port_set_granularity() - Set granularity for the bandwidth values
2733 * @port: DP IN adapter
2734 * @granularity: Granularity in Mb/s. Supported values: 1000, 500 and 250.
2735 *
2736 * Sets the granularity used with the estimated, allocated and requested
2737 * bandwidth. Returns %0 in success and negative errno otherwise. If the
2738 * adapter does not support this %-EOPNOTSUPP is returned.
2739 */
usb4_dp_port_set_granularity(struct tb_port * port,int granularity)2740 int usb4_dp_port_set_granularity(struct tb_port *port, int granularity)
2741 {
2742 u32 val;
2743 int ret;
2744
2745 if (!is_usb4_dpin(port))
2746 return -EOPNOTSUPP;
2747
2748 ret = tb_port_read(port, &val, TB_CFG_PORT,
2749 port->cap_adap + ADP_DP_CS_2, 1);
2750 if (ret)
2751 return ret;
2752
2753 val &= ~ADP_DP_CS_2_GR_MASK;
2754
2755 switch (granularity) {
2756 case 250:
2757 val |= ADP_DP_CS_2_GR_0_25G << ADP_DP_CS_2_GR_SHIFT;
2758 break;
2759 case 500:
2760 val |= ADP_DP_CS_2_GR_0_5G << ADP_DP_CS_2_GR_SHIFT;
2761 break;
2762 case 1000:
2763 val |= ADP_DP_CS_2_GR_1G << ADP_DP_CS_2_GR_SHIFT;
2764 break;
2765 default:
2766 return -EINVAL;
2767 }
2768
2769 return tb_port_write(port, &val, TB_CFG_PORT,
2770 port->cap_adap + ADP_DP_CS_2, 1);
2771 }
2772
2773 /**
2774 * usb4_dp_port_set_estimated_bandwidth() - Set estimated bandwidth
2775 * @port: DP IN adapter
2776 * @bw: Estimated bandwidth in Mb/s.
2777 *
2778 * Sets the estimated bandwidth to @bw. Set the granularity by calling
2779 * usb4_dp_port_set_granularity() before calling this. The @bw is round
2780 * down to the closest granularity multiplier. Returns %0 in success
2781 * and negative errno otherwise. Specifically returns %-EOPNOTSUPP if
2782 * the adapter does not support this.
2783 */
usb4_dp_port_set_estimated_bandwidth(struct tb_port * port,int bw)2784 int usb4_dp_port_set_estimated_bandwidth(struct tb_port *port, int bw)
2785 {
2786 u32 val, granularity;
2787 int ret;
2788
2789 if (!is_usb4_dpin(port))
2790 return -EOPNOTSUPP;
2791
2792 ret = usb4_dp_port_granularity(port);
2793 if (ret < 0)
2794 return ret;
2795 granularity = ret;
2796
2797 ret = tb_port_read(port, &val, TB_CFG_PORT,
2798 port->cap_adap + ADP_DP_CS_2, 1);
2799 if (ret)
2800 return ret;
2801
2802 val &= ~ADP_DP_CS_2_ESTIMATED_BW_MASK;
2803 val |= (bw / granularity) << ADP_DP_CS_2_ESTIMATED_BW_SHIFT;
2804
2805 return tb_port_write(port, &val, TB_CFG_PORT,
2806 port->cap_adap + ADP_DP_CS_2, 1);
2807 }
2808
2809 /**
2810 * usb4_dp_port_allocated_bandwidth() - Return allocated bandwidth
2811 * @port: DP IN adapter
2812 *
2813 * Reads and returns allocated bandwidth for @port in Mb/s (taking into
2814 * account the programmed granularity). Returns negative errno in case
2815 * of error.
2816 */
usb4_dp_port_allocated_bandwidth(struct tb_port * port)2817 int usb4_dp_port_allocated_bandwidth(struct tb_port *port)
2818 {
2819 u32 val, granularity;
2820 int ret;
2821
2822 if (!is_usb4_dpin(port))
2823 return -EOPNOTSUPP;
2824
2825 ret = usb4_dp_port_granularity(port);
2826 if (ret < 0)
2827 return ret;
2828 granularity = ret;
2829
2830 ret = tb_port_read(port, &val, TB_CFG_PORT,
2831 port->cap_adap + DP_STATUS, 1);
2832 if (ret)
2833 return ret;
2834
2835 val &= DP_STATUS_ALLOCATED_BW_MASK;
2836 val >>= DP_STATUS_ALLOCATED_BW_SHIFT;
2837
2838 return val * granularity;
2839 }
2840
__usb4_dp_port_set_cm_ack(struct tb_port * port,bool ack)2841 static int __usb4_dp_port_set_cm_ack(struct tb_port *port, bool ack)
2842 {
2843 u32 val;
2844 int ret;
2845
2846 ret = tb_port_read(port, &val, TB_CFG_PORT,
2847 port->cap_adap + ADP_DP_CS_2, 1);
2848 if (ret)
2849 return ret;
2850
2851 if (ack)
2852 val |= ADP_DP_CS_2_CA;
2853 else
2854 val &= ~ADP_DP_CS_2_CA;
2855
2856 return tb_port_write(port, &val, TB_CFG_PORT,
2857 port->cap_adap + ADP_DP_CS_2, 1);
2858 }
2859
usb4_dp_port_set_cm_ack(struct tb_port * port)2860 static inline int usb4_dp_port_set_cm_ack(struct tb_port *port)
2861 {
2862 return __usb4_dp_port_set_cm_ack(port, true);
2863 }
2864
usb4_dp_port_wait_and_clear_cm_ack(struct tb_port * port,int timeout_msec)2865 static int usb4_dp_port_wait_and_clear_cm_ack(struct tb_port *port,
2866 int timeout_msec)
2867 {
2868 ktime_t end;
2869 u32 val;
2870 int ret;
2871
2872 ret = __usb4_dp_port_set_cm_ack(port, false);
2873 if (ret)
2874 return ret;
2875
2876 end = ktime_add_ms(ktime_get(), timeout_msec);
2877 do {
2878 ret = tb_port_read(port, &val, TB_CFG_PORT,
2879 port->cap_adap + ADP_DP_CS_8, 1);
2880 if (ret)
2881 return ret;
2882
2883 if (!(val & ADP_DP_CS_8_DR))
2884 break;
2885
2886 usleep_range(50, 100);
2887 } while (ktime_before(ktime_get(), end));
2888
2889 if (val & ADP_DP_CS_8_DR)
2890 return -ETIMEDOUT;
2891
2892 ret = tb_port_read(port, &val, TB_CFG_PORT,
2893 port->cap_adap + ADP_DP_CS_2, 1);
2894 if (ret)
2895 return ret;
2896
2897 val &= ~ADP_DP_CS_2_CA;
2898 return tb_port_write(port, &val, TB_CFG_PORT,
2899 port->cap_adap + ADP_DP_CS_2, 1);
2900 }
2901
2902 /**
2903 * usb4_dp_port_allocate_bandwidth() - Set allocated bandwidth
2904 * @port: DP IN adapter
2905 * @bw: New allocated bandwidth in Mb/s
2906 *
2907 * Communicates the new allocated bandwidth with the DPCD (graphics
2908 * driver). Takes into account the programmed granularity. Returns %0 in
2909 * success and negative errno in case of error.
2910 */
usb4_dp_port_allocate_bandwidth(struct tb_port * port,int bw)2911 int usb4_dp_port_allocate_bandwidth(struct tb_port *port, int bw)
2912 {
2913 u32 val, granularity;
2914 int ret;
2915
2916 if (!is_usb4_dpin(port))
2917 return -EOPNOTSUPP;
2918
2919 ret = usb4_dp_port_granularity(port);
2920 if (ret < 0)
2921 return ret;
2922 granularity = ret;
2923
2924 ret = tb_port_read(port, &val, TB_CFG_PORT,
2925 port->cap_adap + DP_STATUS, 1);
2926 if (ret)
2927 return ret;
2928
2929 val &= ~DP_STATUS_ALLOCATED_BW_MASK;
2930 val |= (bw / granularity) << DP_STATUS_ALLOCATED_BW_SHIFT;
2931
2932 ret = tb_port_write(port, &val, TB_CFG_PORT,
2933 port->cap_adap + DP_STATUS, 1);
2934 if (ret)
2935 return ret;
2936
2937 ret = usb4_dp_port_set_cm_ack(port);
2938 if (ret)
2939 return ret;
2940
2941 return usb4_dp_port_wait_and_clear_cm_ack(port, 500);
2942 }
2943
2944 /**
2945 * usb4_dp_port_requested_bandwidth() - Read requested bandwidth
2946 * @port: DP IN adapter
2947 *
2948 * Reads the DPCD (graphics driver) requested bandwidth and returns it
2949 * in Mb/s. Takes the programmed granularity into account. In case of
2950 * error returns negative errno. Specifically returns %-EOPNOTSUPP if
2951 * the adapter does not support bandwidth allocation mode, and %ENODATA
2952 * if there is no active bandwidth request from the graphics driver.
2953 */
usb4_dp_port_requested_bandwidth(struct tb_port * port)2954 int usb4_dp_port_requested_bandwidth(struct tb_port *port)
2955 {
2956 u32 val, granularity;
2957 int ret;
2958
2959 if (!is_usb4_dpin(port))
2960 return -EOPNOTSUPP;
2961
2962 ret = usb4_dp_port_granularity(port);
2963 if (ret < 0)
2964 return ret;
2965 granularity = ret;
2966
2967 ret = tb_port_read(port, &val, TB_CFG_PORT,
2968 port->cap_adap + ADP_DP_CS_8, 1);
2969 if (ret)
2970 return ret;
2971
2972 if (!(val & ADP_DP_CS_8_DR))
2973 return -ENODATA;
2974
2975 return (val & ADP_DP_CS_8_REQUESTED_BW_MASK) * granularity;
2976 }
2977
2978 /**
2979 * usb4_pci_port_set_ext_encapsulation() - Enable/disable extended encapsulation
2980 * @port: PCIe adapter
2981 * @enable: Enable/disable extended encapsulation
2982 *
2983 * Enables or disables extended encapsulation used in PCIe tunneling. Caller
2984 * needs to make sure both adapters support this before enabling. Returns %0 on
2985 * success and negative errno otherwise.
2986 */
usb4_pci_port_set_ext_encapsulation(struct tb_port * port,bool enable)2987 int usb4_pci_port_set_ext_encapsulation(struct tb_port *port, bool enable)
2988 {
2989 u32 val;
2990 int ret;
2991
2992 if (!tb_port_is_pcie_up(port) && !tb_port_is_pcie_down(port))
2993 return -EINVAL;
2994
2995 ret = tb_port_read(port, &val, TB_CFG_PORT,
2996 port->cap_adap + ADP_PCIE_CS_1, 1);
2997 if (ret)
2998 return ret;
2999
3000 if (enable)
3001 val |= ADP_PCIE_CS_1_EE;
3002 else
3003 val &= ~ADP_PCIE_CS_1_EE;
3004
3005 return tb_port_write(port, &val, TB_CFG_PORT,
3006 port->cap_adap + ADP_PCIE_CS_1, 1);
3007 }
3008