1 // SPDX-License-Identifier: GPL-2.0
3 * USB4 specific functionality
5 * Copyright (C) 2019, Intel Corporation
10 #include <linux/delay.h>
11 #include <linux/ktime.h>
12 #include <linux/units.h>
17 #define USB4_DATA_RETRIES 3
18 #define USB4_DATA_DWORDS 16
20 #define USB4_NVM_READ_OFFSET_MASK GENMASK(23, 2)
21 #define USB4_NVM_READ_OFFSET_SHIFT 2
22 #define USB4_NVM_READ_LENGTH_MASK GENMASK(27, 24)
23 #define USB4_NVM_READ_LENGTH_SHIFT 24
25 #define USB4_NVM_SET_OFFSET_MASK USB4_NVM_READ_OFFSET_MASK
26 #define USB4_NVM_SET_OFFSET_SHIFT USB4_NVM_READ_OFFSET_SHIFT
28 #define USB4_DROM_ADDRESS_MASK GENMASK(14, 2)
29 #define USB4_DROM_ADDRESS_SHIFT 2
30 #define USB4_DROM_SIZE_MASK GENMASK(19, 15)
31 #define USB4_DROM_SIZE_SHIFT 15
33 #define USB4_NVM_SECTOR_SIZE_MASK GENMASK(23, 0)
35 #define USB4_BA_LENGTH_MASK GENMASK(7, 0)
36 #define USB4_BA_INDEX_MASK GENMASK(15, 0)
39 USB4_BA_MAX_USB3 = 0x1,
40 USB4_BA_MIN_DP_AUX = 0x2,
41 USB4_BA_MIN_DP_MAIN = 0x3,
42 USB4_BA_MAX_PCIE = 0x4,
46 #define USB4_BA_VALUE_MASK GENMASK(31, 16)
47 #define USB4_BA_VALUE_SHIFT 16
49 /* Delays in us used with usb4_port_wait_for_bit() */
50 #define USB4_PORT_DELAY 50
51 #define USB4_PORT_SB_DELAY 1000
53 static int usb4_native_switch_op(struct tb_switch *sw, u16 opcode,
54 u32 *metadata, u8 *status,
55 const void *tx_data, size_t tx_dwords,
56 void *rx_data, size_t rx_dwords)
62 ret = tb_sw_write(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
67 ret = tb_sw_write(sw, tx_data, TB_CFG_SWITCH, ROUTER_CS_9,
73 val = opcode | ROUTER_CS_26_OV;
74 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
78 ret = tb_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
82 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
86 if (val & ROUTER_CS_26_ONS)
90 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
91 ROUTER_CS_26_STATUS_SHIFT;
94 ret = tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
99 ret = tb_sw_read(sw, rx_data, TB_CFG_SWITCH, ROUTER_CS_9,
108 static int __usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
109 u8 *status, const void *tx_data, size_t tx_dwords,
110 void *rx_data, size_t rx_dwords)
112 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
114 if (tx_dwords > USB4_DATA_DWORDS || rx_dwords > USB4_DATA_DWORDS)
118 * If the connection manager implementation provides USB4 router
119 * operation proxy callback, call it here instead of running the
120 * operation natively.
122 if (cm_ops->usb4_switch_op) {
125 ret = cm_ops->usb4_switch_op(sw, opcode, metadata, status,
126 tx_data, tx_dwords, rx_data,
128 if (ret != -EOPNOTSUPP)
132 * If the proxy was not supported then run the native
133 * router operation instead.
137 return usb4_native_switch_op(sw, opcode, metadata, status, tx_data,
138 tx_dwords, rx_data, rx_dwords);
141 static inline int usb4_switch_op(struct tb_switch *sw, u16 opcode,
142 u32 *metadata, u8 *status)
144 return __usb4_switch_op(sw, opcode, metadata, status, NULL, 0, NULL, 0);
147 static inline int usb4_switch_op_data(struct tb_switch *sw, u16 opcode,
148 u32 *metadata, u8 *status,
149 const void *tx_data, size_t tx_dwords,
150 void *rx_data, size_t rx_dwords)
152 return __usb4_switch_op(sw, opcode, metadata, status, tx_data,
153 tx_dwords, rx_data, rx_dwords);
157 * usb4_switch_check_wakes() - Check for wakes and notify PM core about them
158 * @sw: Router whose wakes to check
160 * Checks wakes occurred during suspend and notify the PM core about them.
162 void usb4_switch_check_wakes(struct tb_switch *sw)
164 bool wakeup_usb4 = false;
165 struct usb4_port *usb4;
166 struct tb_port *port;
171 if (tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1))
174 tb_sw_dbg(sw, "PCIe wake: %s, USB3 wake: %s\n",
175 (val & ROUTER_CS_6_WOPS) ? "yes" : "no",
176 (val & ROUTER_CS_6_WOUS) ? "yes" : "no");
178 wakeup = val & (ROUTER_CS_6_WOPS | ROUTER_CS_6_WOUS);
182 * Check for any downstream ports for USB4 wake,
183 * connection wake and disconnection wake.
185 tb_switch_for_each_port(sw, port) {
189 if (tb_port_read(port, &val, TB_CFG_PORT,
190 port->cap_usb4 + PORT_CS_18, 1))
193 tb_port_dbg(port, "USB4 wake: %s, connection wake: %s, disconnection wake: %s\n",
194 (val & PORT_CS_18_WOU4S) ? "yes" : "no",
195 (val & PORT_CS_18_WOCS) ? "yes" : "no",
196 (val & PORT_CS_18_WODS) ? "yes" : "no");
198 wakeup_usb4 = val & (PORT_CS_18_WOU4S | PORT_CS_18_WOCS |
202 if (device_may_wakeup(&usb4->dev) && wakeup_usb4)
203 pm_wakeup_event(&usb4->dev, 0);
205 wakeup |= wakeup_usb4;
209 pm_wakeup_event(&sw->dev, 0);
212 static bool link_is_usb4(struct tb_port *port)
219 if (tb_port_read(port, &val, TB_CFG_PORT,
220 port->cap_usb4 + PORT_CS_18, 1))
223 return !(val & PORT_CS_18_TCM);
227 * usb4_switch_setup() - Additional setup for USB4 device
228 * @sw: USB4 router to setup
230 * USB4 routers need additional settings in order to enable all the
231 * tunneling. This function enables USB and PCIe tunneling if it can be
232 * enabled (e.g the parent switch also supports them). If USB tunneling
233 * is not available for some reason (like that there is Thunderbolt 3
234 * switch upstream) then the internal xHCI controller is enabled
237 * This does not set the configuration valid bit of the router. To do
238 * that call usb4_switch_configuration_valid().
240 int usb4_switch_setup(struct tb_switch *sw)
242 struct tb_switch *parent = tb_switch_parent(sw);
243 struct tb_port *down;
251 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
255 down = tb_switch_downstream_port(sw);
256 sw->link_usb4 = link_is_usb4(down);
257 tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT");
259 xhci = val & ROUTER_CS_6_HCI;
260 tbt3 = !(val & ROUTER_CS_6_TNS);
262 tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
263 tbt3 ? "yes" : "no", xhci ? "yes" : "no");
265 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
269 if (tb_acpi_may_tunnel_usb3() && sw->link_usb4 &&
270 tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
271 val |= ROUTER_CS_5_UTO;
276 * Only enable PCIe tunneling if the parent router supports it
277 * and it is not disabled.
279 if (tb_acpi_may_tunnel_pcie() &&
280 tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
281 val |= ROUTER_CS_5_PTO;
283 * xHCI can be enabled if PCIe tunneling is supported
284 * and the parent does not have any USB3 dowstream
285 * adapters (so we cannot do USB 3.x tunneling).
288 val |= ROUTER_CS_5_HCO;
291 /* TBT3 supported by the CM */
292 val &= ~ROUTER_CS_5_CNS;
294 return tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
298 * usb4_switch_configuration_valid() - Set tunneling configuration to be valid
301 * Sets configuration valid bit for the router. Must be called before
302 * any tunnels can be set through the router and after
303 * usb4_switch_setup() has been called. Can be called to host and device
304 * routers (does nothing for the latter).
306 * Returns %0 in success and negative errno otherwise.
308 int usb4_switch_configuration_valid(struct tb_switch *sw)
316 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
320 val |= ROUTER_CS_5_CV;
322 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
326 return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
331 * usb4_switch_read_uid() - Read UID from USB4 router
333 * @uid: UID is stored here
335 * Reads 64-bit UID from USB4 router config space.
337 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
339 return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
342 static int usb4_switch_drom_read_block(void *data,
343 unsigned int dwaddress, void *buf,
346 struct tb_switch *sw = data;
351 metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
352 metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
353 USB4_DROM_ADDRESS_MASK;
355 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_DROM_READ, &metadata,
356 &status, NULL, 0, buf, dwords);
360 return status ? -EIO : 0;
364 * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
366 * @address: Byte address inside DROM to start reading
367 * @buf: Buffer where the DROM content is stored
368 * @size: Number of bytes to read from DROM
370 * Uses USB4 router operations to read router DROM. For devices this
371 * should always work but for hosts it may return %-EOPNOTSUPP in which
372 * case the host router does not have DROM.
374 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
377 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
378 usb4_switch_drom_read_block, sw);
382 * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
385 * Checks whether conditions are met so that lane bonding can be
386 * established with the upstream router. Call only for device routers.
388 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
394 up = tb_upstream_port(sw);
395 ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
399 return !!(val & PORT_CS_18_BE);
403 * usb4_switch_set_wake() - Enabled/disable wake
405 * @flags: Wakeup flags (%0 to disable)
407 * Enables/disables router to wake up from sleep.
409 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags)
411 struct usb4_port *usb4;
412 struct tb_port *port;
413 u64 route = tb_route(sw);
418 * Enable wakes coming from all USB4 downstream ports (from
419 * child routers). For device routers do this also for the
420 * upstream USB4 port.
422 tb_switch_for_each_port(sw, port) {
423 if (!tb_port_is_null(port))
425 if (!route && tb_is_upstream_port(port))
430 ret = tb_port_read(port, &val, TB_CFG_PORT,
431 port->cap_usb4 + PORT_CS_19, 1);
435 val &= ~(PORT_CS_19_WOC | PORT_CS_19_WOD | PORT_CS_19_WOU4);
437 if (tb_is_upstream_port(port)) {
438 val |= PORT_CS_19_WOU4;
440 bool configured = val & PORT_CS_19_PC;
443 if (((flags & TB_WAKE_ON_CONNECT) |
444 device_may_wakeup(&usb4->dev)) && !configured)
445 val |= PORT_CS_19_WOC;
446 if (((flags & TB_WAKE_ON_DISCONNECT) |
447 device_may_wakeup(&usb4->dev)) && configured)
448 val |= PORT_CS_19_WOD;
449 if ((flags & TB_WAKE_ON_USB4) && configured)
450 val |= PORT_CS_19_WOU4;
453 ret = tb_port_write(port, &val, TB_CFG_PORT,
454 port->cap_usb4 + PORT_CS_19, 1);
460 * Enable wakes from PCIe, USB 3.x and DP on this router. Only
461 * needed for device routers.
464 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
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;
476 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
485 * usb4_switch_set_sleep() - Prepare the router to enter sleep
488 * Sets sleep bit for the router. Returns when the router sleep ready
489 * bit has been asserted.
491 int usb4_switch_set_sleep(struct tb_switch *sw)
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);
501 val |= ROUTER_CS_5_SLP;
503 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
507 return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
508 ROUTER_CS_6_SLPR, 500);
512 * usb4_switch_nvm_sector_size() - Return router NVM sector size
515 * If the router supports NVM operations this function returns the NVM
516 * sector size in bytes. If NVM operations are not supported returns
519 int usb4_switch_nvm_sector_size(struct tb_switch *sw)
525 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &metadata,
531 return status == 0x2 ? -EOPNOTSUPP : -EIO;
533 return metadata & USB4_NVM_SECTOR_SIZE_MASK;
536 static int usb4_switch_nvm_read_block(void *data,
537 unsigned int dwaddress, void *buf, size_t dwords)
539 struct tb_switch *sw = data;
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;
549 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_READ, &metadata,
550 &status, NULL, 0, buf, dwords);
554 return status ? -EIO : 0;
558 * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
560 * @address: Starting address in bytes
561 * @buf: Read data is placed here
562 * @size: How many bytes to read
564 * Reads NVM contents of the router. If NVM is not supported returns
567 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
570 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
571 usb4_switch_nvm_read_block, sw);
575 * usb4_switch_nvm_set_offset() - Set NVM write offset
577 * @address: Start offset
579 * Explicitly sets NVM write offset. Normally when writing to NVM this
580 * is done automatically by usb4_switch_nvm_write().
582 * Returns %0 in success and negative errno if there was a failure.
584 int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address)
586 u32 metadata, dwaddress;
590 dwaddress = address / 4;
591 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
592 USB4_NVM_SET_OFFSET_MASK;
594 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &metadata,
599 return status ? -EIO : 0;
602 static int usb4_switch_nvm_write_next_block(void *data, unsigned int dwaddress,
603 const void *buf, size_t dwords)
605 struct tb_switch *sw = data;
609 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_WRITE, NULL, &status,
610 buf, dwords, NULL, 0);
614 return status ? -EIO : 0;
618 * usb4_switch_nvm_write() - Write to the router NVM
620 * @address: Start address where to write in bytes
621 * @buf: Pointer to the data to write
622 * @size: Size of @buf in bytes
624 * Writes @buf to the router NVM using USB4 router operations. If NVM
625 * write is not supported returns %-EOPNOTSUPP.
627 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
628 const void *buf, size_t size)
632 ret = usb4_switch_nvm_set_offset(sw, address);
636 return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
637 usb4_switch_nvm_write_next_block, sw);
641 * usb4_switch_nvm_authenticate() - Authenticate new NVM
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.
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.
653 int usb4_switch_nvm_authenticate(struct tb_switch *sw)
657 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, NULL, NULL);
660 * The router is power cycled once NVM_AUTH is started so it is
661 * expected to get any of the following errors back.
674 * usb4_switch_nvm_authenticate_status() - Read status of last NVM authenticate
676 * @status: Status code of the operation
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
683 * Must be called before any other router operation.
685 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status)
687 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
692 if (cm_ops->usb4_switch_nvm_authenticate_status) {
693 ret = cm_ops->usb4_switch_nvm_authenticate_status(sw, status);
694 if (ret != -EOPNOTSUPP)
698 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
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)
707 if (val & ROUTER_CS_26_ONS)
710 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
711 ROUTER_CS_26_STATUS_SHIFT;
720 * usb4_switch_credits_init() - Read buffer allocation parameters
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.
727 * Returns %0 on success and negative errno otherwise.
729 int usb4_switch_credits_init(struct tb_switch *sw)
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];
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));
746 length = metadata & USB4_BA_LENGTH_MASK;
747 if (WARN_ON(length > ARRAY_SIZE(data)))
756 tb_sw_dbg(sw, "credit allocation parameters:\n");
758 for (i = 0; i < length; i++) {
761 index = data[i] & USB4_BA_INDEX_MASK;
762 value = (data[i] & USB4_BA_VALUE_MASK) >> USB4_BA_VALUE_SHIFT;
765 case USB4_BA_MAX_USB3:
766 tb_sw_dbg(sw, " USB3: %u\n", value);
769 case USB4_BA_MIN_DP_AUX:
770 tb_sw_dbg(sw, " DP AUX: %u\n", value);
773 case USB4_BA_MIN_DP_MAIN:
774 tb_sw_dbg(sw, " DP main: %u\n", value);
777 case USB4_BA_MAX_PCIE:
778 tb_sw_dbg(sw, " PCIe: %u\n", value);
782 tb_sw_dbg(sw, " DMA: %u\n", value);
786 tb_sw_dbg(sw, " unknown credit allocation index %#x, skipping\n",
793 * Validate the buffer allocation preferences. If we find
794 * issues, log a warning and fall back using the hard-coded
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");
805 tb_switch_for_each_port(sw, port) {
806 if (tb_port_is_null(port))
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");
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");
821 if ((tb_port_is_dpin(port) || tb_port_is_dpout(port)) &&
823 tb_sw_warn(sw, "missing baMinDPaux");
826 if ((tb_port_is_usb3_down(port) || tb_port_is_usb3_up(port)) &&
828 tb_sw_warn(sw, "missing baMaxUSB3");
831 if ((tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) &&
833 tb_sw_warn(sw, "missing baMaxPCIe");
839 * Buffer allocation passed the validation so we can use it in
842 sw->credit_allocation = true;
844 sw->max_usb3_credits = max_usb3;
846 sw->min_dp_aux_credits = min_dp_aux;
848 sw->min_dp_main_credits = min_dp_main;
850 sw->max_pcie_credits = max_pcie;
852 sw->max_dma_credits = max_dma;
861 * usb4_switch_query_dp_resource() - Query availability of DP IN resource
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.
869 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
871 u32 metadata = in->port;
875 ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &metadata,
878 * If DP resource allocation is not supported assume it is
881 if (ret == -EOPNOTSUPP)
890 * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
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
899 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
901 u32 metadata = in->port;
905 ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &metadata,
907 if (ret == -EOPNOTSUPP)
912 return status ? -EBUSY : 0;
916 * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
920 * Releases the previously allocated DP IN resource.
922 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
924 u32 metadata = in->port;
928 ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &metadata,
930 if (ret == -EOPNOTSUPP)
935 return status ? -EIO : 0;
938 static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
943 /* Assume port is primary */
944 tb_switch_for_each_port(sw, p) {
945 if (!tb_port_is_null(p))
947 if (tb_is_upstream_port(p))
960 * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
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.
969 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
970 const struct tb_port *port)
972 int usb4_idx = usb4_port_idx(sw, port);
976 /* Find PCIe down port matching usb4_port */
977 tb_switch_for_each_port(sw, p) {
978 if (!tb_port_is_pcie_down(p))
981 if (pcie_idx == usb4_idx)
991 * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
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.
1000 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
1001 const struct tb_port *port)
1003 int usb4_idx = usb4_port_idx(sw, port);
1007 /* Find USB3 down port matching usb4_port */
1008 tb_switch_for_each_port(sw, p) {
1009 if (!tb_port_is_usb3_down(p))
1012 if (usb_idx == usb4_idx)
1022 * usb4_switch_add_ports() - Add USB4 ports for this router
1025 * For USB4 router finds all USB4 ports and registers devices for each.
1026 * Can be called to any router.
1028 * Return %0 in case of success and negative errno in case of failure.
1030 int usb4_switch_add_ports(struct tb_switch *sw)
1032 struct tb_port *port;
1034 if (tb_switch_is_icm(sw) || !tb_switch_is_usb4(sw))
1037 tb_switch_for_each_port(sw, port) {
1038 struct usb4_port *usb4;
1040 if (!tb_port_is_null(port))
1042 if (!port->cap_usb4)
1045 usb4 = usb4_port_device_add(port);
1047 usb4_switch_remove_ports(sw);
1048 return PTR_ERR(usb4);
1058 * usb4_switch_remove_ports() - Removes USB4 ports from this router
1061 * Unregisters previously registered USB4 ports.
1063 void usb4_switch_remove_ports(struct tb_switch *sw)
1065 struct tb_port *port;
1067 tb_switch_for_each_port(sw, port) {
1069 usb4_port_device_remove(port->usb4);
1076 * usb4_port_unlock() - Unlock USB4 downstream port
1077 * @port: USB4 port to unlock
1079 * Unlocks USB4 downstream port so that the connection manager can
1080 * access the router below this port.
1082 int usb4_port_unlock(struct tb_port *port)
1087 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
1091 val &= ~ADP_CS_4_LCK;
1092 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
1096 * usb4_port_hotplug_enable() - Enables hotplug for a port
1097 * @port: USB4 port to operate on
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.
1102 int usb4_port_hotplug_enable(struct tb_port *port)
1107 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
1111 val &= ~ADP_CS_5_DHP;
1112 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
1116 * usb4_port_reset() - Issue downstream port reset
1117 * @port: USB4 port to reset
1119 * Issues downstream port reset to @port.
1121 int usb4_port_reset(struct tb_port *port)
1126 if (!port->cap_usb4)
1129 ret = tb_port_read(port, &val, TB_CFG_PORT,
1130 port->cap_usb4 + PORT_CS_19, 1);
1134 val |= PORT_CS_19_DPR;
1136 ret = tb_port_write(port, &val, TB_CFG_PORT,
1137 port->cap_usb4 + PORT_CS_19, 1);
1143 ret = tb_port_read(port, &val, TB_CFG_PORT,
1144 port->cap_usb4 + PORT_CS_19, 1);
1148 val &= ~PORT_CS_19_DPR;
1150 return tb_port_write(port, &val, TB_CFG_PORT,
1151 port->cap_usb4 + PORT_CS_19, 1);
1154 static int usb4_port_set_configured(struct tb_port *port, bool configured)
1159 if (!port->cap_usb4)
1162 ret = tb_port_read(port, &val, TB_CFG_PORT,
1163 port->cap_usb4 + PORT_CS_19, 1);
1168 val |= PORT_CS_19_PC;
1170 val &= ~PORT_CS_19_PC;
1172 return tb_port_write(port, &val, TB_CFG_PORT,
1173 port->cap_usb4 + PORT_CS_19, 1);
1177 * usb4_port_configure() - Set USB4 port configured
1178 * @port: USB4 router
1180 * Sets the USB4 link to be configured for power management purposes.
1182 int usb4_port_configure(struct tb_port *port)
1184 return usb4_port_set_configured(port, true);
1188 * usb4_port_unconfigure() - Set USB4 port unconfigured
1189 * @port: USB4 router
1191 * Sets the USB4 link to be unconfigured for power management purposes.
1193 void usb4_port_unconfigure(struct tb_port *port)
1195 usb4_port_set_configured(port, false);
1198 static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
1203 if (!port->cap_usb4)
1206 ret = tb_port_read(port, &val, TB_CFG_PORT,
1207 port->cap_usb4 + PORT_CS_19, 1);
1212 val |= PORT_CS_19_PID;
1214 val &= ~PORT_CS_19_PID;
1216 return tb_port_write(port, &val, TB_CFG_PORT,
1217 port->cap_usb4 + PORT_CS_19, 1);
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
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.
1228 int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
1230 xd->link_usb4 = link_is_usb4(port);
1231 return usb4_set_xdomain_configured(port, true);
1235 * usb4_port_unconfigure_xdomain() - Unconfigure port for XDomain
1236 * @port: USB4 port that was connected to another host
1238 * Clears USB4 port from being marked as XDomain.
1240 void usb4_port_unconfigure_xdomain(struct tb_port *port)
1242 usb4_set_xdomain_configured(port, false);
1245 static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
1246 u32 value, int timeout_msec, unsigned long delay_usec)
1248 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1254 ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
1258 if ((val & bit) == value)
1262 } while (ktime_before(ktime_get(), timeout));
1267 static int usb4_port_read_data(struct tb_port *port, void *data, size_t dwords)
1269 if (dwords > USB4_DATA_DWORDS)
1272 return tb_port_read(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
1276 static int usb4_port_write_data(struct tb_port *port, const void *data,
1279 if (dwords > USB4_DATA_DWORDS)
1282 return tb_port_write(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
1287 * usb4_port_sb_read() - Read from sideband register
1288 * @port: USB4 port to read
1289 * @target: Sideband target
1290 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
1291 * @reg: Sideband register index
1292 * @buf: Buffer where the sideband data is copied
1293 * @size: Size of @buf
1295 * Reads data from sideband register @reg and copies it into @buf.
1296 * Returns %0 in case of success and negative errno in case of failure.
1298 int usb4_port_sb_read(struct tb_port *port, enum usb4_sb_target target, u8 index,
1299 u8 reg, void *buf, u8 size)
1301 size_t dwords = DIV_ROUND_UP(size, 4);
1305 if (!port->cap_usb4)
1309 val |= size << PORT_CS_1_LENGTH_SHIFT;
1310 val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1311 if (target == USB4_SB_TARGET_RETIMER)
1312 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1313 val |= PORT_CS_1_PND;
1315 ret = tb_port_write(port, &val, TB_CFG_PORT,
1316 port->cap_usb4 + PORT_CS_1, 1);
1320 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1321 PORT_CS_1_PND, 0, 500, USB4_PORT_SB_DELAY);
1325 ret = tb_port_read(port, &val, TB_CFG_PORT,
1326 port->cap_usb4 + PORT_CS_1, 1);
1330 if (val & PORT_CS_1_NR)
1332 if (val & PORT_CS_1_RC)
1335 return buf ? usb4_port_read_data(port, buf, dwords) : 0;
1339 * usb4_port_sb_write() - Write to sideband register
1340 * @port: USB4 port to write
1341 * @target: Sideband target
1342 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
1343 * @reg: Sideband register index
1344 * @buf: Data to write
1345 * @size: Size of @buf
1347 * Writes @buf to sideband register @reg. Returns %0 in case of success
1348 * and negative errno in case of failure.
1350 int usb4_port_sb_write(struct tb_port *port, enum usb4_sb_target target,
1351 u8 index, u8 reg, const void *buf, u8 size)
1353 size_t dwords = DIV_ROUND_UP(size, 4);
1357 if (!port->cap_usb4)
1361 ret = usb4_port_write_data(port, buf, dwords);
1367 val |= size << PORT_CS_1_LENGTH_SHIFT;
1368 val |= PORT_CS_1_WNR_WRITE;
1369 val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1370 if (target == USB4_SB_TARGET_RETIMER)
1371 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1372 val |= PORT_CS_1_PND;
1374 ret = tb_port_write(port, &val, TB_CFG_PORT,
1375 port->cap_usb4 + PORT_CS_1, 1);
1379 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1380 PORT_CS_1_PND, 0, 500, USB4_PORT_SB_DELAY);
1384 ret = tb_port_read(port, &val, TB_CFG_PORT,
1385 port->cap_usb4 + PORT_CS_1, 1);
1389 if (val & PORT_CS_1_NR)
1391 if (val & PORT_CS_1_RC)
1397 static int usb4_port_sb_opcode_err_to_errno(u32 val)
1402 case USB4_SB_OPCODE_ERR:
1404 case USB4_SB_OPCODE_ONS:
1411 static int usb4_port_sb_op(struct tb_port *port, enum usb4_sb_target target,
1412 u8 index, enum usb4_sb_opcode opcode, int timeout_msec)
1419 ret = usb4_port_sb_write(port, target, index, USB4_SB_OPCODE, &val,
1424 timeout = ktime_add_ms(ktime_get(), timeout_msec);
1428 ret = usb4_port_sb_read(port, target, index, USB4_SB_OPCODE,
1434 return usb4_port_sb_opcode_err_to_errno(val);
1436 fsleep(USB4_PORT_SB_DELAY);
1437 } while (ktime_before(ktime_get(), timeout));
1442 static int usb4_port_set_router_offline(struct tb_port *port, bool offline)
1447 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1448 USB4_SB_METADATA, &val, sizeof(val));
1452 val = USB4_SB_OPCODE_ROUTER_OFFLINE;
1453 return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1454 USB4_SB_OPCODE, &val, sizeof(val));
1458 * usb4_port_router_offline() - Put the USB4 port to offline mode
1461 * This function puts the USB4 port into offline mode. In this mode the
1462 * port does not react on hotplug events anymore. This needs to be
1463 * called before retimer access is done when the USB4 links is not up.
1465 * Returns %0 in case of success and negative errno if there was an
1468 int usb4_port_router_offline(struct tb_port *port)
1470 return usb4_port_set_router_offline(port, true);
1474 * usb4_port_router_online() - Put the USB4 port back to online
1477 * Makes the USB4 port functional again.
1479 int usb4_port_router_online(struct tb_port *port)
1481 return usb4_port_set_router_offline(port, false);
1485 * usb4_port_enumerate_retimers() - Send RT broadcast transaction
1488 * This forces the USB4 port to send broadcast RT transaction which
1489 * makes the retimers on the link to assign index to themselves. Returns
1490 * %0 in case of success and negative errno if there was an error.
1492 int usb4_port_enumerate_retimers(struct tb_port *port)
1496 val = USB4_SB_OPCODE_ENUMERATE_RETIMERS;
1497 return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1498 USB4_SB_OPCODE, &val, sizeof(val));
1502 * usb4_port_clx_supported() - Check if CLx is supported by the link
1503 * @port: Port to check for CLx support for
1505 * PORT_CS_18_CPS bit reflects if the link supports CLx including
1506 * active cables (if connected on the link).
1508 bool usb4_port_clx_supported(struct tb_port *port)
1513 ret = tb_port_read(port, &val, TB_CFG_PORT,
1514 port->cap_usb4 + PORT_CS_18, 1);
1518 return !!(val & PORT_CS_18_CPS);
1522 * usb4_port_asym_supported() - If the port supports asymmetric link
1525 * Checks if the port and the cable supports asymmetric link and returns
1526 * %true in that case.
1528 bool usb4_port_asym_supported(struct tb_port *port)
1532 if (!port->cap_usb4)
1535 if (tb_port_read(port, &val, TB_CFG_PORT, port->cap_usb4 + PORT_CS_18, 1))
1538 return !!(val & PORT_CS_18_CSA);
1542 * usb4_port_asym_set_link_width() - Set link width to asymmetric or symmetric
1544 * @width: Asymmetric width to configure
1546 * Sets USB4 port link width to @width. Can be called for widths where
1547 * usb4_port_asym_width_supported() returned @true.
1549 int usb4_port_asym_set_link_width(struct tb_port *port, enum tb_link_width width)
1557 ret = tb_port_read(port, &val, TB_CFG_PORT,
1558 port->cap_phy + LANE_ADP_CS_1, 1);
1562 val &= ~LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK;
1564 case TB_LINK_WIDTH_DUAL:
1565 val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
1566 LANE_ADP_CS_1_TARGET_WIDTH_ASYM_DUAL);
1568 case TB_LINK_WIDTH_ASYM_TX:
1569 val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
1570 LANE_ADP_CS_1_TARGET_WIDTH_ASYM_TX);
1572 case TB_LINK_WIDTH_ASYM_RX:
1573 val |= FIELD_PREP(LANE_ADP_CS_1_TARGET_WIDTH_ASYM_MASK,
1574 LANE_ADP_CS_1_TARGET_WIDTH_ASYM_RX);
1580 return tb_port_write(port, &val, TB_CFG_PORT,
1581 port->cap_phy + LANE_ADP_CS_1, 1);
1585 * usb4_port_asym_start() - Start symmetry change and wait for completion
1588 * Start symmetry change of the link to asymmetric or symmetric
1589 * (according to what was previously set in tb_port_set_link_width().
1590 * Wait for completion of the change.
1592 * Returns %0 in case of success, %-ETIMEDOUT if case of timeout or
1593 * a negative errno in case of a failure.
1595 int usb4_port_asym_start(struct tb_port *port)
1600 ret = tb_port_read(port, &val, TB_CFG_PORT,
1601 port->cap_usb4 + PORT_CS_19, 1);
1605 val &= ~PORT_CS_19_START_ASYM;
1606 val |= FIELD_PREP(PORT_CS_19_START_ASYM, 1);
1608 ret = tb_port_write(port, &val, TB_CFG_PORT,
1609 port->cap_usb4 + PORT_CS_19, 1);
1614 * Wait for PORT_CS_19_START_ASYM to be 0. This means the USB4
1615 * port started the symmetry transition.
1617 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_19,
1618 PORT_CS_19_START_ASYM, 0, 1000,
1623 /* Then wait for the transtion to be completed */
1624 return usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_18,
1625 PORT_CS_18_TIP, 0, 5000, USB4_PORT_DELAY);
1629 * usb4_port_margining_caps() - Read USB4 port marginig capabilities
1631 * @target: Sideband target
1632 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
1633 * @caps: Array with at least two elements to hold the results
1634 * @ncaps: Number of elements in the caps array
1636 * Reads the USB4 port lane margining capabilities into @caps.
1638 int usb4_port_margining_caps(struct tb_port *port, enum usb4_sb_target target,
1639 u8 index, u32 *caps, size_t ncaps)
1643 ret = usb4_port_sb_op(port, target, index,
1644 USB4_SB_OPCODE_READ_LANE_MARGINING_CAP, 500);
1648 return usb4_port_sb_read(port, target, index, USB4_SB_DATA, caps,
1649 sizeof(*caps) * ncaps);
1653 * usb4_port_hw_margin() - Run hardware lane margining on port
1655 * @target: Sideband target
1656 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
1657 * @params: Parameters for USB4 hardware margining
1658 * @results: Array to hold the results
1659 * @nresults: Number of elements in the results array
1661 * Runs hardware lane margining on USB4 port and returns the result in
1664 int usb4_port_hw_margin(struct tb_port *port, enum usb4_sb_target target,
1665 u8 index, const struct usb4_port_margining_params *params,
1666 u32 *results, size_t nresults)
1671 if (WARN_ON_ONCE(!params))
1674 val = params->lanes;
1676 val |= USB4_MARGIN_HW_TIME;
1677 if (params->right_high || params->upper_eye)
1678 val |= USB4_MARGIN_HW_RHU;
1679 if (params->ber_level)
1680 val |= FIELD_PREP(USB4_MARGIN_HW_BER_MASK, params->ber_level);
1681 if (params->optional_voltage_offset_range)
1682 val |= USB4_MARGIN_HW_OPT_VOLTAGE;
1684 ret = usb4_port_sb_write(port, target, index, USB4_SB_METADATA, &val,
1689 ret = usb4_port_sb_op(port, target, index,
1690 USB4_SB_OPCODE_RUN_HW_LANE_MARGINING, 2500);
1694 return usb4_port_sb_read(port, target, index, USB4_SB_DATA, results,
1695 sizeof(*results) * nresults);
1699 * usb4_port_sw_margin() - Run software lane margining on port
1701 * @target: Sideband target
1702 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
1703 * @params: Parameters for USB4 software margining
1704 * @results: Data word for the operation completion data
1706 * Runs software lane margining on USB4 port. Read back the error
1707 * counters by calling usb4_port_sw_margin_errors(). Returns %0 in
1708 * success and negative errno otherwise.
1710 int usb4_port_sw_margin(struct tb_port *port, enum usb4_sb_target target,
1711 u8 index, const struct usb4_port_margining_params *params,
1717 if (WARN_ON_ONCE(!params))
1720 val = params->lanes;
1722 val |= USB4_MARGIN_SW_TIME;
1723 if (params->optional_voltage_offset_range)
1724 val |= USB4_MARGIN_SW_OPT_VOLTAGE;
1725 if (params->right_high)
1726 val |= USB4_MARGIN_SW_RH;
1727 if (params->upper_eye)
1728 val |= USB4_MARGIN_SW_UPPER_EYE;
1729 val |= FIELD_PREP(USB4_MARGIN_SW_COUNTER_MASK, params->error_counter);
1730 val |= FIELD_PREP(USB4_MARGIN_SW_VT_MASK, params->voltage_time_offset);
1732 ret = usb4_port_sb_write(port, target, index, USB4_SB_METADATA, &val,
1737 ret = usb4_port_sb_op(port, target, index,
1738 USB4_SB_OPCODE_RUN_SW_LANE_MARGINING, 2500);
1742 return usb4_port_sb_read(port, target, index, USB4_SB_DATA, results,
1748 * usb4_port_sw_margin_errors() - Read the software margining error counters
1750 * @target: Sideband target
1751 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
1752 * @errors: Error metadata is copied here.
1754 * This reads back the software margining error counters from the port.
1755 * Returns %0 in success and negative errno otherwise.
1757 int usb4_port_sw_margin_errors(struct tb_port *port, enum usb4_sb_target target,
1758 u8 index, u32 *errors)
1762 ret = usb4_port_sb_op(port, target, index,
1763 USB4_SB_OPCODE_READ_SW_MARGIN_ERR, 150);
1767 return usb4_port_sb_read(port, target, index, USB4_SB_METADATA, errors,
1771 static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
1772 enum usb4_sb_opcode opcode,
1775 return usb4_port_sb_op(port, USB4_SB_TARGET_RETIMER, index, opcode,
1780 * usb4_port_retimer_set_inbound_sbtx() - Enable sideband channel transactions
1782 * @index: Retimer index
1784 * Enables sideband channel transations on SBTX. Can be used when USB4
1785 * link does not go up, for example if there is no device connected.
1787 int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index)
1791 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
1798 * Per the USB4 retimer spec, the retimer is not required to
1799 * send an RT (Retimer Transaction) response for the first
1800 * SET_INBOUND_SBTX command
1802 return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
1807 * usb4_port_retimer_unset_inbound_sbtx() - Disable sideband channel transactions
1809 * @index: Retimer index
1811 * Disables sideband channel transations on SBTX. The reverse of
1812 * usb4_port_retimer_set_inbound_sbtx().
1814 int usb4_port_retimer_unset_inbound_sbtx(struct tb_port *port, u8 index)
1816 return usb4_port_retimer_op(port, index,
1817 USB4_SB_OPCODE_UNSET_INBOUND_SBTX, 500);
1821 * usb4_port_retimer_is_last() - Is the retimer last on-board retimer
1823 * @index: Retimer index
1825 * If the retimer at @index is last one (connected directly to the
1826 * Type-C port) this function returns %1. If it is not returns %0. If
1827 * the retimer is not present returns %-ENODEV. Otherwise returns
1830 int usb4_port_retimer_is_last(struct tb_port *port, u8 index)
1835 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_LAST_RETIMER,
1840 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
1841 USB4_SB_METADATA, &metadata, sizeof(metadata));
1842 return ret ? ret : metadata & 1;
1846 * usb4_port_retimer_is_cable() - Is the retimer cable retimer
1848 * @index: Retimer index
1850 * If the retimer at @index is last cable retimer this function returns
1851 * %1 and %0 if it is on-board retimer. In case a retimer is not present
1852 * at @index returns %-ENODEV. Otherwise returns negative errno.
1854 int usb4_port_retimer_is_cable(struct tb_port *port, u8 index)
1859 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_CABLE_RETIMER,
1864 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
1865 USB4_SB_METADATA, &metadata, sizeof(metadata));
1866 return ret ? ret : metadata & 1;
1870 * usb4_port_retimer_nvm_sector_size() - Read retimer NVM sector size
1872 * @index: Retimer index
1874 * Reads NVM sector size (in bytes) of a retimer at @index. This
1875 * operation can be used to determine whether the retimer supports NVM
1876 * upgrade for example. Returns sector size in bytes or negative errno
1877 * in case of error. Specifically returns %-ENODEV if there is no
1878 * retimer at @index.
1880 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
1885 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE,
1890 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
1891 USB4_SB_METADATA, &metadata, sizeof(metadata));
1892 return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
1896 * usb4_port_retimer_nvm_set_offset() - Set NVM write offset
1898 * @index: Retimer index
1899 * @address: Start offset
1901 * Exlicitly sets NVM write offset. Normally when writing to NVM this is
1902 * done automatically by usb4_port_retimer_nvm_write().
1904 * Returns %0 in success and negative errno if there was a failure.
1906 int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1907 unsigned int address)
1909 u32 metadata, dwaddress;
1912 dwaddress = address / 4;
1913 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
1914 USB4_NVM_SET_OFFSET_MASK;
1916 ret = usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1917 USB4_SB_METADATA, &metadata, sizeof(metadata));
1921 return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_SET_OFFSET,
1925 struct retimer_info {
1926 struct tb_port *port;
1930 static int usb4_port_retimer_nvm_write_next_block(void *data,
1931 unsigned int dwaddress, const void *buf, size_t dwords)
1934 const struct retimer_info *info = data;
1935 struct tb_port *port = info->port;
1936 u8 index = info->index;
1939 ret = usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1940 USB4_SB_DATA, buf, dwords * 4);
1944 return usb4_port_retimer_op(port, index,
1945 USB4_SB_OPCODE_NVM_BLOCK_WRITE, 1000);
1949 * usb4_port_retimer_nvm_write() - Write to retimer NVM
1951 * @index: Retimer index
1952 * @address: Byte address where to start the write
1953 * @buf: Data to write
1954 * @size: Size in bytes how much to write
1956 * Writes @size bytes from @buf to the retimer NVM. Used for NVM
1957 * upgrade. Returns %0 if the data was written successfully and negative
1958 * errno in case of failure. Specifically returns %-ENODEV if there is
1959 * no retimer at @index.
1961 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index, unsigned int address,
1962 const void *buf, size_t size)
1964 struct retimer_info info = { .port = port, .index = index };
1967 ret = usb4_port_retimer_nvm_set_offset(port, index, address);
1971 return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
1972 usb4_port_retimer_nvm_write_next_block, &info);
1976 * usb4_port_retimer_nvm_authenticate() - Start retimer NVM upgrade
1978 * @index: Retimer index
1980 * After the new NVM image has been written via usb4_port_retimer_nvm_write()
1981 * this function can be used to trigger the NVM upgrade process. If
1982 * successful the retimer restarts with the new NVM and may not have the
1983 * index set so one needs to call usb4_port_enumerate_retimers() to
1984 * force index to be assigned.
1986 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index)
1991 * We need to use the raw operation here because once the
1992 * authentication completes the retimer index is not set anymore
1993 * so we do not get back the status now.
1995 val = USB4_SB_OPCODE_NVM_AUTH_WRITE;
1996 return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1997 USB4_SB_OPCODE, &val, sizeof(val));
2001 * usb4_port_retimer_nvm_authenticate_status() - Read status of NVM upgrade
2003 * @index: Retimer index
2004 * @status: Raw status code read from metadata
2006 * This can be called after usb4_port_retimer_nvm_authenticate() and
2007 * usb4_port_enumerate_retimers() to fetch status of the NVM upgrade.
2009 * Returns %0 if the authentication status was successfully read. The
2010 * completion metadata (the result) is then stored into @status. If
2011 * reading the status fails, returns negative errno.
2013 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
2019 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
2020 USB4_SB_OPCODE, &val, sizeof(val));
2024 ret = usb4_port_sb_opcode_err_to_errno(val);
2031 ret = usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
2032 USB4_SB_METADATA, &metadata,
2037 *status = metadata & USB4_SB_METADATA_NVM_AUTH_WRITE_MASK;
2045 static int usb4_port_retimer_nvm_read_block(void *data, unsigned int dwaddress,
2046 void *buf, size_t dwords)
2048 const struct retimer_info *info = data;
2049 struct tb_port *port = info->port;
2050 u8 index = info->index;
2054 metadata = dwaddress << USB4_NVM_READ_OFFSET_SHIFT;
2055 if (dwords < USB4_DATA_DWORDS)
2056 metadata |= dwords << USB4_NVM_READ_LENGTH_SHIFT;
2058 ret = usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
2059 USB4_SB_METADATA, &metadata, sizeof(metadata));
2063 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_READ, 500);
2067 return usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index,
2068 USB4_SB_DATA, buf, dwords * 4);
2072 * usb4_port_retimer_nvm_read() - Read contents of retimer NVM
2074 * @index: Retimer index
2075 * @address: NVM address (in bytes) to start reading
2076 * @buf: Data read from NVM is stored here
2077 * @size: Number of bytes to read
2079 * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
2080 * read was successful and negative errno in case of failure.
2081 * Specifically returns %-ENODEV if there is no retimer at @index.
2083 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
2084 unsigned int address, void *buf, size_t size)
2086 struct retimer_info info = { .port = port, .index = index };
2088 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
2089 usb4_port_retimer_nvm_read_block, &info);
2092 static inline unsigned int
2093 usb4_usb3_port_max_bandwidth(const struct tb_port *port, unsigned int bw)
2095 /* Take the possible bandwidth limitation into account */
2097 return min(bw, port->max_bw);
2102 * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate
2103 * @port: USB3 adapter port
2105 * Return maximum supported link rate of a USB3 adapter in Mb/s.
2106 * Negative errno in case of error.
2108 int usb4_usb3_port_max_link_rate(struct tb_port *port)
2113 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
2116 ret = tb_port_read(port, &val, TB_CFG_PORT,
2117 port->cap_adap + ADP_USB3_CS_4, 1);
2121 lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
2122 ret = lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
2124 return usb4_usb3_port_max_bandwidth(port, ret);
2127 static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
2132 if (!tb_port_is_usb3_down(port))
2134 if (tb_route(port->sw))
2137 ret = tb_port_read(port, &val, TB_CFG_PORT,
2138 port->cap_adap + ADP_USB3_CS_2, 1);
2143 val |= ADP_USB3_CS_2_CMR;
2145 val &= ~ADP_USB3_CS_2_CMR;
2147 ret = tb_port_write(port, &val, TB_CFG_PORT,
2148 port->cap_adap + ADP_USB3_CS_2, 1);
2153 * We can use val here directly as the CMR bit is in the same place
2154 * as HCA. Just mask out others.
2156 val &= ADP_USB3_CS_2_CMR;
2157 return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
2158 ADP_USB3_CS_1_HCA, val, 1500,
2162 static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
2164 return usb4_usb3_port_cm_request(port, true);
2167 static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
2169 return usb4_usb3_port_cm_request(port, false);
2172 static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
2174 unsigned long uframes;
2176 uframes = bw * 512UL << scale;
2177 return DIV_ROUND_CLOSEST(uframes * 8000, MEGA);
2180 static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
2182 unsigned long uframes;
2184 /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
2185 uframes = ((unsigned long)mbps * MEGA) / 8000;
2186 return DIV_ROUND_UP(uframes, 512UL << scale);
2189 static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
2196 ret = tb_port_read(port, &val, TB_CFG_PORT,
2197 port->cap_adap + ADP_USB3_CS_2, 1);
2201 ret = tb_port_read(port, &scale, TB_CFG_PORT,
2202 port->cap_adap + ADP_USB3_CS_3, 1);
2206 scale &= ADP_USB3_CS_3_SCALE_MASK;
2208 bw = val & ADP_USB3_CS_2_AUBW_MASK;
2209 *upstream_bw = usb3_bw_to_mbps(bw, scale);
2211 bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
2212 *downstream_bw = usb3_bw_to_mbps(bw, scale);
2218 * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
2219 * @port: USB3 adapter port
2220 * @upstream_bw: Allocated upstream bandwidth is stored here
2221 * @downstream_bw: Allocated downstream bandwidth is stored here
2223 * Stores currently allocated USB3 bandwidth into @upstream_bw and
2224 * @downstream_bw in Mb/s. Returns %0 in case of success and negative
2227 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
2232 ret = usb4_usb3_port_set_cm_request(port);
2236 ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
2238 usb4_usb3_port_clear_cm_request(port);
2243 static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
2250 ret = tb_port_read(port, &val, TB_CFG_PORT,
2251 port->cap_adap + ADP_USB3_CS_1, 1);
2255 ret = tb_port_read(port, &scale, TB_CFG_PORT,
2256 port->cap_adap + ADP_USB3_CS_3, 1);
2260 scale &= ADP_USB3_CS_3_SCALE_MASK;
2262 bw = val & ADP_USB3_CS_1_CUBW_MASK;
2263 *upstream_bw = usb3_bw_to_mbps(bw, scale);
2265 bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
2266 *downstream_bw = usb3_bw_to_mbps(bw, scale);
2271 static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
2275 u32 val, ubw, dbw, scale;
2278 /* Figure out suitable scale */
2280 max_bw = max(upstream_bw, downstream_bw);
2281 while (scale < 64) {
2282 if (mbps_to_usb3_bw(max_bw, scale) < 4096)
2287 if (WARN_ON(scale >= 64))
2290 ret = tb_port_write(port, &scale, TB_CFG_PORT,
2291 port->cap_adap + ADP_USB3_CS_3, 1);
2295 ubw = mbps_to_usb3_bw(upstream_bw, scale);
2296 dbw = mbps_to_usb3_bw(downstream_bw, scale);
2298 tb_port_dbg(port, "scaled bandwidth %u/%u, scale %u\n", ubw, dbw, scale);
2300 ret = tb_port_read(port, &val, TB_CFG_PORT,
2301 port->cap_adap + ADP_USB3_CS_2, 1);
2305 val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
2306 val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
2309 return tb_port_write(port, &val, TB_CFG_PORT,
2310 port->cap_adap + ADP_USB3_CS_2, 1);
2314 * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
2315 * @port: USB3 adapter port
2316 * @upstream_bw: New upstream bandwidth
2317 * @downstream_bw: New downstream bandwidth
2319 * This can be used to set how much bandwidth is allocated for the USB3
2320 * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
2321 * new values programmed to the USB3 adapter allocation registers. If
2322 * the values are lower than what is currently consumed the allocation
2323 * is set to what is currently consumed instead (consumed bandwidth
2324 * cannot be taken away by CM). The actual new values are returned in
2325 * @upstream_bw and @downstream_bw.
2327 * Returns %0 in case of success and negative errno if there was a
2330 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
2333 int ret, consumed_up, consumed_down, allocate_up, allocate_down;
2335 ret = usb4_usb3_port_set_cm_request(port);
2339 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
2344 /* Don't allow it go lower than what is consumed */
2345 allocate_up = max(*upstream_bw, consumed_up);
2346 allocate_down = max(*downstream_bw, consumed_down);
2348 ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
2353 *upstream_bw = allocate_up;
2354 *downstream_bw = allocate_down;
2357 usb4_usb3_port_clear_cm_request(port);
2362 * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
2363 * @port: USB3 adapter port
2364 * @upstream_bw: New allocated upstream bandwidth
2365 * @downstream_bw: New allocated downstream bandwidth
2367 * Releases USB3 allocated bandwidth down to what is actually consumed.
2368 * The new bandwidth is returned in @upstream_bw and @downstream_bw.
2370 * Returns 0% in success and negative errno in case of failure.
2372 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
2375 int ret, consumed_up, consumed_down;
2377 ret = usb4_usb3_port_set_cm_request(port);
2381 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
2387 * Always keep 900 Mb/s to make sure xHCI has at least some
2388 * bandwidth available for isochronous traffic.
2390 if (consumed_up < 900)
2392 if (consumed_down < 900)
2393 consumed_down = 900;
2395 ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
2400 *upstream_bw = consumed_up;
2401 *downstream_bw = consumed_down;
2404 usb4_usb3_port_clear_cm_request(port);
2408 static bool is_usb4_dpin(const struct tb_port *port)
2410 if (!tb_port_is_dpin(port))
2412 if (!tb_switch_is_usb4(port->sw))
2418 * usb4_dp_port_set_cm_id() - Assign CM ID to the DP IN adapter
2419 * @port: DP IN adapter
2420 * @cm_id: CM ID to assign
2422 * Sets CM ID for the @port. Returns %0 on success and negative errno
2423 * otherwise. Speficially returns %-EOPNOTSUPP if the @port does not
2426 int usb4_dp_port_set_cm_id(struct tb_port *port, int cm_id)
2431 if (!is_usb4_dpin(port))
2434 ret = tb_port_read(port, &val, TB_CFG_PORT,
2435 port->cap_adap + ADP_DP_CS_2, 1);
2439 val &= ~ADP_DP_CS_2_CM_ID_MASK;
2440 val |= cm_id << ADP_DP_CS_2_CM_ID_SHIFT;
2442 return tb_port_write(port, &val, TB_CFG_PORT,
2443 port->cap_adap + ADP_DP_CS_2, 1);
2447 * usb4_dp_port_bandwidth_mode_supported() - Is the bandwidth allocation mode
2449 * @port: DP IN adapter to check
2451 * Can be called to any DP IN adapter. Returns true if the adapter
2452 * supports USB4 bandwidth allocation mode, false otherwise.
2454 bool usb4_dp_port_bandwidth_mode_supported(struct tb_port *port)
2459 if (!is_usb4_dpin(port))
2462 ret = tb_port_read(port, &val, TB_CFG_PORT,
2463 port->cap_adap + DP_LOCAL_CAP, 1);
2467 return !!(val & DP_COMMON_CAP_BW_MODE);
2471 * usb4_dp_port_bandwidth_mode_enabled() - Is the bandwidth allocation mode
2473 * @port: DP IN adapter to check
2475 * Can be called to any DP IN adapter. Returns true if the bandwidth
2476 * allocation mode has been enabled, false otherwise.
2478 bool usb4_dp_port_bandwidth_mode_enabled(struct tb_port *port)
2483 if (!is_usb4_dpin(port))
2486 ret = tb_port_read(port, &val, TB_CFG_PORT,
2487 port->cap_adap + ADP_DP_CS_8, 1);
2491 return !!(val & ADP_DP_CS_8_DPME);
2495 * usb4_dp_port_set_cm_bandwidth_mode_supported() - Set/clear CM support for
2496 * bandwidth allocation mode
2497 * @port: DP IN adapter
2498 * @supported: Does the CM support bandwidth allocation mode
2500 * Can be called to any DP IN adapter. Sets or clears the CM support bit
2501 * of the DP IN adapter. Returns %0 in success and negative errno
2502 * otherwise. Specifically returns %-OPNOTSUPP if the passed in adapter
2503 * does not support this.
2505 int usb4_dp_port_set_cm_bandwidth_mode_supported(struct tb_port *port,
2511 if (!is_usb4_dpin(port))
2514 ret = tb_port_read(port, &val, TB_CFG_PORT,
2515 port->cap_adap + ADP_DP_CS_2, 1);
2520 val |= ADP_DP_CS_2_CMMS;
2522 val &= ~ADP_DP_CS_2_CMMS;
2524 return tb_port_write(port, &val, TB_CFG_PORT,
2525 port->cap_adap + ADP_DP_CS_2, 1);
2529 * usb4_dp_port_group_id() - Return Group ID assigned for the adapter
2530 * @port: DP IN adapter
2532 * Reads bandwidth allocation Group ID from the DP IN adapter and
2533 * returns it. If the adapter does not support setting Group_ID
2534 * %-EOPNOTSUPP is returned.
2536 int usb4_dp_port_group_id(struct tb_port *port)
2541 if (!is_usb4_dpin(port))
2544 ret = tb_port_read(port, &val, TB_CFG_PORT,
2545 port->cap_adap + ADP_DP_CS_2, 1);
2549 return (val & ADP_DP_CS_2_GROUP_ID_MASK) >> ADP_DP_CS_2_GROUP_ID_SHIFT;
2553 * usb4_dp_port_set_group_id() - Set adapter Group ID
2554 * @port: DP IN adapter
2555 * @group_id: Group ID for the adapter
2557 * Sets bandwidth allocation mode Group ID for the DP IN adapter.
2558 * Returns %0 in case of success and negative errno otherwise.
2559 * Specifically returns %-EOPNOTSUPP if the adapter does not support
2562 int usb4_dp_port_set_group_id(struct tb_port *port, int group_id)
2567 if (!is_usb4_dpin(port))
2570 ret = tb_port_read(port, &val, TB_CFG_PORT,
2571 port->cap_adap + ADP_DP_CS_2, 1);
2575 val &= ~ADP_DP_CS_2_GROUP_ID_MASK;
2576 val |= group_id << ADP_DP_CS_2_GROUP_ID_SHIFT;
2578 return tb_port_write(port, &val, TB_CFG_PORT,
2579 port->cap_adap + ADP_DP_CS_2, 1);
2583 * usb4_dp_port_nrd() - Read non-reduced rate and lanes
2584 * @port: DP IN adapter
2585 * @rate: Non-reduced rate in Mb/s is placed here
2586 * @lanes: Non-reduced lanes are placed here
2588 * Reads the non-reduced rate and lanes from the DP IN adapter. Returns
2589 * %0 in success and negative errno otherwise. Specifically returns
2590 * %-EOPNOTSUPP if the adapter does not support this.
2592 int usb4_dp_port_nrd(struct tb_port *port, int *rate, int *lanes)
2597 if (!is_usb4_dpin(port))
2600 ret = tb_port_read(port, &val, TB_CFG_PORT,
2601 port->cap_adap + ADP_DP_CS_2, 1);
2605 tmp = (val & ADP_DP_CS_2_NRD_MLR_MASK) >> ADP_DP_CS_2_NRD_MLR_SHIFT;
2607 case DP_COMMON_CAP_RATE_RBR:
2610 case DP_COMMON_CAP_RATE_HBR:
2613 case DP_COMMON_CAP_RATE_HBR2:
2616 case DP_COMMON_CAP_RATE_HBR3:
2621 tmp = val & ADP_DP_CS_2_NRD_MLC_MASK;
2623 case DP_COMMON_CAP_1_LANE:
2626 case DP_COMMON_CAP_2_LANES:
2629 case DP_COMMON_CAP_4_LANES:
2638 * usb4_dp_port_set_nrd() - Set non-reduced rate and lanes
2639 * @port: DP IN adapter
2640 * @rate: Non-reduced rate in Mb/s
2641 * @lanes: Non-reduced lanes
2643 * Before the capabilities reduction this function can be used to set
2644 * the non-reduced values for the DP IN adapter. Returns %0 in success
2645 * and negative errno otherwise. If the adapter does not support this
2646 * %-EOPNOTSUPP is returned.
2648 int usb4_dp_port_set_nrd(struct tb_port *port, int rate, int lanes)
2653 if (!is_usb4_dpin(port))
2656 ret = tb_port_read(port, &val, TB_CFG_PORT,
2657 port->cap_adap + ADP_DP_CS_2, 1);
2661 val &= ~ADP_DP_CS_2_NRD_MLR_MASK;
2667 val |= (DP_COMMON_CAP_RATE_HBR << ADP_DP_CS_2_NRD_MLR_SHIFT)
2668 & ADP_DP_CS_2_NRD_MLR_MASK;
2671 val |= (DP_COMMON_CAP_RATE_HBR2 << ADP_DP_CS_2_NRD_MLR_SHIFT)
2672 & ADP_DP_CS_2_NRD_MLR_MASK;
2675 val |= (DP_COMMON_CAP_RATE_HBR3 << ADP_DP_CS_2_NRD_MLR_SHIFT)
2676 & ADP_DP_CS_2_NRD_MLR_MASK;
2682 val &= ~ADP_DP_CS_2_NRD_MLC_MASK;
2688 val |= DP_COMMON_CAP_2_LANES;
2691 val |= DP_COMMON_CAP_4_LANES;
2697 return tb_port_write(port, &val, TB_CFG_PORT,
2698 port->cap_adap + ADP_DP_CS_2, 1);
2702 * usb4_dp_port_granularity() - Return granularity for the bandwidth values
2703 * @port: DP IN adapter
2705 * Reads the programmed granularity from @port. If the DP IN adapter does
2706 * not support bandwidth allocation mode returns %-EOPNOTSUPP and negative
2707 * errno in other error cases.
2709 int usb4_dp_port_granularity(struct tb_port *port)
2714 if (!is_usb4_dpin(port))
2717 ret = tb_port_read(port, &val, TB_CFG_PORT,
2718 port->cap_adap + ADP_DP_CS_2, 1);
2722 val &= ADP_DP_CS_2_GR_MASK;
2723 val >>= ADP_DP_CS_2_GR_SHIFT;
2726 case ADP_DP_CS_2_GR_0_25G:
2728 case ADP_DP_CS_2_GR_0_5G:
2730 case ADP_DP_CS_2_GR_1G:
2738 * usb4_dp_port_set_granularity() - Set granularity for the bandwidth values
2739 * @port: DP IN adapter
2740 * @granularity: Granularity in Mb/s. Supported values: 1000, 500 and 250.
2742 * Sets the granularity used with the estimated, allocated and requested
2743 * bandwidth. Returns %0 in success and negative errno otherwise. If the
2744 * adapter does not support this %-EOPNOTSUPP is returned.
2746 int usb4_dp_port_set_granularity(struct tb_port *port, int granularity)
2751 if (!is_usb4_dpin(port))
2754 ret = tb_port_read(port, &val, TB_CFG_PORT,
2755 port->cap_adap + ADP_DP_CS_2, 1);
2759 val &= ~ADP_DP_CS_2_GR_MASK;
2761 switch (granularity) {
2763 val |= ADP_DP_CS_2_GR_0_25G << ADP_DP_CS_2_GR_SHIFT;
2766 val |= ADP_DP_CS_2_GR_0_5G << ADP_DP_CS_2_GR_SHIFT;
2769 val |= ADP_DP_CS_2_GR_1G << ADP_DP_CS_2_GR_SHIFT;
2775 return tb_port_write(port, &val, TB_CFG_PORT,
2776 port->cap_adap + ADP_DP_CS_2, 1);
2780 * usb4_dp_port_set_estimated_bandwidth() - Set estimated bandwidth
2781 * @port: DP IN adapter
2782 * @bw: Estimated bandwidth in Mb/s.
2784 * Sets the estimated bandwidth to @bw. Set the granularity by calling
2785 * usb4_dp_port_set_granularity() before calling this. The @bw is round
2786 * down to the closest granularity multiplier. Returns %0 in success
2787 * and negative errno otherwise. Specifically returns %-EOPNOTSUPP if
2788 * the adapter does not support this.
2790 int usb4_dp_port_set_estimated_bandwidth(struct tb_port *port, int bw)
2792 u32 val, granularity;
2795 if (!is_usb4_dpin(port))
2798 ret = usb4_dp_port_granularity(port);
2803 ret = tb_port_read(port, &val, TB_CFG_PORT,
2804 port->cap_adap + ADP_DP_CS_2, 1);
2808 val &= ~ADP_DP_CS_2_ESTIMATED_BW_MASK;
2809 val |= (bw / granularity) << ADP_DP_CS_2_ESTIMATED_BW_SHIFT;
2811 return tb_port_write(port, &val, TB_CFG_PORT,
2812 port->cap_adap + ADP_DP_CS_2, 1);
2816 * usb4_dp_port_allocated_bandwidth() - Return allocated bandwidth
2817 * @port: DP IN adapter
2819 * Reads and returns allocated bandwidth for @port in Mb/s (taking into
2820 * account the programmed granularity). Returns negative errno in case
2823 int usb4_dp_port_allocated_bandwidth(struct tb_port *port)
2825 u32 val, granularity;
2828 if (!is_usb4_dpin(port))
2831 ret = usb4_dp_port_granularity(port);
2836 ret = tb_port_read(port, &val, TB_CFG_PORT,
2837 port->cap_adap + DP_STATUS, 1);
2841 val &= DP_STATUS_ALLOCATED_BW_MASK;
2842 val >>= DP_STATUS_ALLOCATED_BW_SHIFT;
2844 return val * granularity;
2847 static int __usb4_dp_port_set_cm_ack(struct tb_port *port, bool ack)
2852 ret = tb_port_read(port, &val, TB_CFG_PORT,
2853 port->cap_adap + ADP_DP_CS_2, 1);
2858 val |= ADP_DP_CS_2_CA;
2860 val &= ~ADP_DP_CS_2_CA;
2862 return tb_port_write(port, &val, TB_CFG_PORT,
2863 port->cap_adap + ADP_DP_CS_2, 1);
2866 static inline int usb4_dp_port_set_cm_ack(struct tb_port *port)
2868 return __usb4_dp_port_set_cm_ack(port, true);
2871 static int usb4_dp_port_wait_and_clear_cm_ack(struct tb_port *port,
2878 ret = __usb4_dp_port_set_cm_ack(port, false);
2882 end = ktime_add_ms(ktime_get(), timeout_msec);
2884 ret = tb_port_read(port, &val, TB_CFG_PORT,
2885 port->cap_adap + ADP_DP_CS_8, 1);
2889 if (!(val & ADP_DP_CS_8_DR))
2892 usleep_range(50, 100);
2893 } while (ktime_before(ktime_get(), end));
2895 if (val & ADP_DP_CS_8_DR) {
2896 tb_port_warn(port, "timeout waiting for DPTX request to clear\n");
2900 ret = tb_port_read(port, &val, TB_CFG_PORT,
2901 port->cap_adap + ADP_DP_CS_2, 1);
2905 val &= ~ADP_DP_CS_2_CA;
2906 return tb_port_write(port, &val, TB_CFG_PORT,
2907 port->cap_adap + ADP_DP_CS_2, 1);
2911 * usb4_dp_port_allocate_bandwidth() - Set allocated bandwidth
2912 * @port: DP IN adapter
2913 * @bw: New allocated bandwidth in Mb/s
2915 * Communicates the new allocated bandwidth with the DPCD (graphics
2916 * driver). Takes into account the programmed granularity. Returns %0 in
2917 * success and negative errno in case of error.
2919 int usb4_dp_port_allocate_bandwidth(struct tb_port *port, int bw)
2921 u32 val, granularity;
2924 if (!is_usb4_dpin(port))
2927 ret = usb4_dp_port_granularity(port);
2932 ret = tb_port_read(port, &val, TB_CFG_PORT,
2933 port->cap_adap + DP_STATUS, 1);
2937 val &= ~DP_STATUS_ALLOCATED_BW_MASK;
2938 val |= (bw / granularity) << DP_STATUS_ALLOCATED_BW_SHIFT;
2940 ret = tb_port_write(port, &val, TB_CFG_PORT,
2941 port->cap_adap + DP_STATUS, 1);
2945 ret = usb4_dp_port_set_cm_ack(port);
2949 return usb4_dp_port_wait_and_clear_cm_ack(port, 500);
2953 * usb4_dp_port_requested_bandwidth() - Read requested bandwidth
2954 * @port: DP IN adapter
2956 * Reads the DPCD (graphics driver) requested bandwidth and returns it
2957 * in Mb/s. Takes the programmed granularity into account. In case of
2958 * error returns negative errno. Specifically returns %-EOPNOTSUPP if
2959 * the adapter does not support bandwidth allocation mode, and %ENODATA
2960 * if there is no active bandwidth request from the graphics driver.
2962 int usb4_dp_port_requested_bandwidth(struct tb_port *port)
2964 u32 val, granularity;
2967 if (!is_usb4_dpin(port))
2970 ret = usb4_dp_port_granularity(port);
2975 ret = tb_port_read(port, &val, TB_CFG_PORT,
2976 port->cap_adap + ADP_DP_CS_8, 1);
2980 if (!(val & ADP_DP_CS_8_DR))
2983 return (val & ADP_DP_CS_8_REQUESTED_BW_MASK) * granularity;
2987 * usb4_pci_port_set_ext_encapsulation() - Enable/disable extended encapsulation
2988 * @port: PCIe adapter
2989 * @enable: Enable/disable extended encapsulation
2991 * Enables or disables extended encapsulation used in PCIe tunneling. Caller
2992 * needs to make sure both adapters support this before enabling. Returns %0 on
2993 * success and negative errno otherwise.
2995 int usb4_pci_port_set_ext_encapsulation(struct tb_port *port, bool enable)
3000 if (!tb_port_is_pcie_up(port) && !tb_port_is_pcie_down(port))
3003 ret = tb_port_read(port, &val, TB_CFG_PORT,
3004 port->cap_adap + ADP_PCIE_CS_1, 1);
3009 val |= ADP_PCIE_CS_1_EE;
3011 val &= ~ADP_PCIE_CS_1_EE;
3013 return tb_port_write(port, &val, TB_CFG_PORT,
3014 port->cap_adap + ADP_PCIE_CS_1, 1);