thunderbolt: Add wake from DisplayPort
[linux-2.6-microblaze.git] / drivers / thunderbolt / usb4.c
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
13 #include "sb_regs.h"
14 #include "tb.h"
15
16 #define USB4_DATA_RETRIES               3
17
18 enum usb4_sb_target {
19         USB4_SB_TARGET_ROUTER,
20         USB4_SB_TARGET_PARTNER,
21         USB4_SB_TARGET_RETIMER,
22 };
23
24 #define USB4_NVM_READ_OFFSET_MASK       GENMASK(23, 2)
25 #define USB4_NVM_READ_OFFSET_SHIFT      2
26 #define USB4_NVM_READ_LENGTH_MASK       GENMASK(27, 24)
27 #define USB4_NVM_READ_LENGTH_SHIFT      24
28
29 #define USB4_NVM_SET_OFFSET_MASK        USB4_NVM_READ_OFFSET_MASK
30 #define USB4_NVM_SET_OFFSET_SHIFT       USB4_NVM_READ_OFFSET_SHIFT
31
32 #define USB4_DROM_ADDRESS_MASK          GENMASK(14, 2)
33 #define USB4_DROM_ADDRESS_SHIFT         2
34 #define USB4_DROM_SIZE_MASK             GENMASK(19, 15)
35 #define USB4_DROM_SIZE_SHIFT            15
36
37 #define USB4_NVM_SECTOR_SIZE_MASK       GENMASK(23, 0)
38
39 static int usb4_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
40                                     u32 value, int timeout_msec)
41 {
42         ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
43
44         do {
45                 u32 val;
46                 int ret;
47
48                 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
49                 if (ret)
50                         return ret;
51
52                 if ((val & bit) == value)
53                         return 0;
54
55                 usleep_range(50, 100);
56         } while (ktime_before(ktime_get(), timeout));
57
58         return -ETIMEDOUT;
59 }
60
61 static int usb4_native_switch_op(struct tb_switch *sw, u16 opcode,
62                                  u32 *metadata, u8 *status,
63                                  const void *tx_data, size_t tx_dwords,
64                                  void *rx_data, size_t rx_dwords)
65 {
66         u32 val;
67         int ret;
68
69         if (metadata) {
70                 ret = tb_sw_write(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
71                 if (ret)
72                         return ret;
73         }
74         if (tx_dwords) {
75                 ret = tb_sw_write(sw, tx_data, TB_CFG_SWITCH, ROUTER_CS_9,
76                                   tx_dwords);
77                 if (ret)
78                         return ret;
79         }
80
81         val = opcode | ROUTER_CS_26_OV;
82         ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
83         if (ret)
84                 return ret;
85
86         ret = usb4_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
87         if (ret)
88                 return ret;
89
90         ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
91         if (ret)
92                 return ret;
93
94         if (val & ROUTER_CS_26_ONS)
95                 return -EOPNOTSUPP;
96
97         if (status)
98                 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
99                         ROUTER_CS_26_STATUS_SHIFT;
100
101         if (metadata) {
102                 ret = tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
103                 if (ret)
104                         return ret;
105         }
106         if (rx_dwords) {
107                 ret = tb_sw_read(sw, rx_data, TB_CFG_SWITCH, ROUTER_CS_9,
108                                  rx_dwords);
109                 if (ret)
110                         return ret;
111         }
112
113         return 0;
114 }
115
116 static int __usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
117                             u8 *status, const void *tx_data, size_t tx_dwords,
118                             void *rx_data, size_t rx_dwords)
119 {
120         const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
121
122         if (tx_dwords > NVM_DATA_DWORDS || rx_dwords > NVM_DATA_DWORDS)
123                 return -EINVAL;
124
125         /*
126          * If the connection manager implementation provides USB4 router
127          * operation proxy callback, call it here instead of running the
128          * operation natively.
129          */
130         if (cm_ops->usb4_switch_op) {
131                 int ret;
132
133                 ret = cm_ops->usb4_switch_op(sw, opcode, metadata, status,
134                                              tx_data, tx_dwords, rx_data,
135                                              rx_dwords);
136                 if (ret != -EOPNOTSUPP)
137                         return ret;
138
139                 /*
140                  * If the proxy was not supported then run the native
141                  * router operation instead.
142                  */
143         }
144
145         return usb4_native_switch_op(sw, opcode, metadata, status, tx_data,
146                                      tx_dwords, rx_data, rx_dwords);
147 }
148
149 static inline int usb4_switch_op(struct tb_switch *sw, u16 opcode,
150                                  u32 *metadata, u8 *status)
151 {
152         return __usb4_switch_op(sw, opcode, metadata, status, NULL, 0, NULL, 0);
153 }
154
155 static inline int usb4_switch_op_data(struct tb_switch *sw, u16 opcode,
156                                       u32 *metadata, u8 *status,
157                                       const void *tx_data, size_t tx_dwords,
158                                       void *rx_data, size_t rx_dwords)
159 {
160         return __usb4_switch_op(sw, opcode, metadata, status, tx_data,
161                                 tx_dwords, rx_data, rx_dwords);
162 }
163
164 static void usb4_switch_check_wakes(struct tb_switch *sw)
165 {
166         struct tb_port *port;
167         bool wakeup = false;
168         u32 val;
169
170         if (!device_may_wakeup(&sw->dev))
171                 return;
172
173         if (tb_route(sw)) {
174                 if (tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1))
175                         return;
176
177                 tb_sw_dbg(sw, "PCIe wake: %s, USB3 wake: %s\n",
178                           (val & ROUTER_CS_6_WOPS) ? "yes" : "no",
179                           (val & ROUTER_CS_6_WOUS) ? "yes" : "no");
180
181                 wakeup = val & (ROUTER_CS_6_WOPS | ROUTER_CS_6_WOUS);
182         }
183
184         /* Check for any connected downstream ports for USB4 wake */
185         tb_switch_for_each_port(sw, port) {
186                 if (!tb_port_has_remote(port))
187                         continue;
188
189                 if (tb_port_read(port, &val, TB_CFG_PORT,
190                                  port->cap_usb4 + PORT_CS_18, 1))
191                         break;
192
193                 tb_port_dbg(port, "USB4 wake: %s\n",
194                             (val & PORT_CS_18_WOU4S) ? "yes" : "no");
195
196                 if (val & PORT_CS_18_WOU4S)
197                         wakeup = true;
198         }
199
200         if (wakeup)
201                 pm_wakeup_event(&sw->dev, 0);
202 }
203
204 static bool link_is_usb4(struct tb_port *port)
205 {
206         u32 val;
207
208         if (!port->cap_usb4)
209                 return false;
210
211         if (tb_port_read(port, &val, TB_CFG_PORT,
212                          port->cap_usb4 + PORT_CS_18, 1))
213                 return false;
214
215         return !(val & PORT_CS_18_TCM);
216 }
217
218 /**
219  * usb4_switch_setup() - Additional setup for USB4 device
220  * @sw: USB4 router to setup
221  *
222  * USB4 routers need additional settings in order to enable all the
223  * tunneling. This function enables USB and PCIe tunneling if it can be
224  * enabled (e.g the parent switch also supports them). If USB tunneling
225  * is not available for some reason (like that there is Thunderbolt 3
226  * switch upstream) then the internal xHCI controller is enabled
227  * instead.
228  */
229 int usb4_switch_setup(struct tb_switch *sw)
230 {
231         struct tb_port *downstream_port;
232         struct tb_switch *parent;
233         bool tbt3, xhci;
234         u32 val = 0;
235         int ret;
236
237         usb4_switch_check_wakes(sw);
238
239         if (!tb_route(sw))
240                 return 0;
241
242         ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
243         if (ret)
244                 return ret;
245
246         parent = tb_switch_parent(sw);
247         downstream_port = tb_port_at(tb_route(sw), parent);
248         sw->link_usb4 = link_is_usb4(downstream_port);
249         tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT3");
250
251         xhci = val & ROUTER_CS_6_HCI;
252         tbt3 = !(val & ROUTER_CS_6_TNS);
253
254         tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
255                   tbt3 ? "yes" : "no", xhci ? "yes" : "no");
256
257         ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
258         if (ret)
259                 return ret;
260
261         if (tb_acpi_may_tunnel_usb3() && sw->link_usb4 &&
262             tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
263                 val |= ROUTER_CS_5_UTO;
264                 xhci = false;
265         }
266
267         /*
268          * Only enable PCIe tunneling if the parent router supports it
269          * and it is not disabled.
270          */
271         if (tb_acpi_may_tunnel_pcie() &&
272             tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
273                 val |= ROUTER_CS_5_PTO;
274                 /*
275                  * xHCI can be enabled if PCIe tunneling is supported
276                  * and the parent does not have any USB3 dowstream
277                  * adapters (so we cannot do USB 3.x tunneling).
278                  */
279                 if (xhci)
280                         val |= ROUTER_CS_5_HCO;
281         }
282
283         /* TBT3 supported by the CM */
284         val |= ROUTER_CS_5_C3S;
285         /* Tunneling configuration is ready now */
286         val |= ROUTER_CS_5_CV;
287
288         ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
289         if (ret)
290                 return ret;
291
292         return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
293                                         ROUTER_CS_6_CR, 50);
294 }
295
296 /**
297  * usb4_switch_read_uid() - Read UID from USB4 router
298  * @sw: USB4 router
299  * @uid: UID is stored here
300  *
301  * Reads 64-bit UID from USB4 router config space.
302  */
303 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
304 {
305         return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
306 }
307
308 static int usb4_switch_drom_read_block(void *data,
309                                        unsigned int dwaddress, void *buf,
310                                        size_t dwords)
311 {
312         struct tb_switch *sw = data;
313         u8 status = 0;
314         u32 metadata;
315         int ret;
316
317         metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
318         metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
319                 USB4_DROM_ADDRESS_MASK;
320
321         ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_DROM_READ, &metadata,
322                                   &status, NULL, 0, buf, dwords);
323         if (ret)
324                 return ret;
325
326         return status ? -EIO : 0;
327 }
328
329 /**
330  * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
331  * @sw: USB4 router
332  * @address: Byte address inside DROM to start reading
333  * @buf: Buffer where the DROM content is stored
334  * @size: Number of bytes to read from DROM
335  *
336  * Uses USB4 router operations to read router DROM. For devices this
337  * should always work but for hosts it may return %-EOPNOTSUPP in which
338  * case the host router does not have DROM.
339  */
340 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
341                           size_t size)
342 {
343         return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
344                                 usb4_switch_drom_read_block, sw);
345 }
346
347 /**
348  * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
349  * @sw: USB4 router
350  *
351  * Checks whether conditions are met so that lane bonding can be
352  * established with the upstream router. Call only for device routers.
353  */
354 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
355 {
356         struct tb_port *up;
357         int ret;
358         u32 val;
359
360         up = tb_upstream_port(sw);
361         ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
362         if (ret)
363                 return false;
364
365         return !!(val & PORT_CS_18_BE);
366 }
367
368 /**
369  * usb4_switch_set_wake() - Enabled/disable wake
370  * @sw: USB4 router
371  * @flags: Wakeup flags (%0 to disable)
372  *
373  * Enables/disables router to wake up from sleep.
374  */
375 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags)
376 {
377         struct tb_port *port;
378         u64 route = tb_route(sw);
379         u32 val;
380         int ret;
381
382         /*
383          * Enable wakes coming from all USB4 downstream ports (from
384          * child routers). For device routers do this also for the
385          * upstream USB4 port.
386          */
387         tb_switch_for_each_port(sw, port) {
388                 if (!tb_port_is_null(port))
389                         continue;
390                 if (!route && tb_is_upstream_port(port))
391                         continue;
392                 if (!port->cap_usb4)
393                         continue;
394
395                 ret = tb_port_read(port, &val, TB_CFG_PORT,
396                                    port->cap_usb4 + PORT_CS_19, 1);
397                 if (ret)
398                         return ret;
399
400                 val &= ~(PORT_CS_19_WOC | PORT_CS_19_WOD | PORT_CS_19_WOU4);
401
402                 if (flags & TB_WAKE_ON_CONNECT)
403                         val |= PORT_CS_19_WOC;
404                 if (flags & TB_WAKE_ON_DISCONNECT)
405                         val |= PORT_CS_19_WOD;
406                 if (flags & TB_WAKE_ON_USB4)
407                         val |= PORT_CS_19_WOU4;
408
409                 ret = tb_port_write(port, &val, TB_CFG_PORT,
410                                     port->cap_usb4 + PORT_CS_19, 1);
411                 if (ret)
412                         return ret;
413         }
414
415         /*
416          * Enable wakes from PCIe, USB 3.x and DP on this router. Only
417          * needed for device routers.
418          */
419         if (route) {
420                 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
421                 if (ret)
422                         return ret;
423
424                 val &= ~(ROUTER_CS_5_WOP | ROUTER_CS_5_WOU | ROUTER_CS_5_WOD);
425                 if (flags & TB_WAKE_ON_USB3)
426                         val |= ROUTER_CS_5_WOU;
427                 if (flags & TB_WAKE_ON_PCIE)
428                         val |= ROUTER_CS_5_WOP;
429                 if (flags & TB_WAKE_ON_DP)
430                         val |= ROUTER_CS_5_WOD;
431
432                 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
433                 if (ret)
434                         return ret;
435         }
436
437         return 0;
438 }
439
440 /**
441  * usb4_switch_set_sleep() - Prepare the router to enter sleep
442  * @sw: USB4 router
443  *
444  * Sets sleep bit for the router. Returns when the router sleep ready
445  * bit has been asserted.
446  */
447 int usb4_switch_set_sleep(struct tb_switch *sw)
448 {
449         int ret;
450         u32 val;
451
452         /* Set sleep bit and wait for sleep ready to be asserted */
453         ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
454         if (ret)
455                 return ret;
456
457         val |= ROUTER_CS_5_SLP;
458
459         ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
460         if (ret)
461                 return ret;
462
463         return usb4_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
464                                         ROUTER_CS_6_SLPR, 500);
465 }
466
467 /**
468  * usb4_switch_nvm_sector_size() - Return router NVM sector size
469  * @sw: USB4 router
470  *
471  * If the router supports NVM operations this function returns the NVM
472  * sector size in bytes. If NVM operations are not supported returns
473  * %-EOPNOTSUPP.
474  */
475 int usb4_switch_nvm_sector_size(struct tb_switch *sw)
476 {
477         u32 metadata;
478         u8 status;
479         int ret;
480
481         ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &metadata,
482                              &status);
483         if (ret)
484                 return ret;
485
486         if (status)
487                 return status == 0x2 ? -EOPNOTSUPP : -EIO;
488
489         return metadata & USB4_NVM_SECTOR_SIZE_MASK;
490 }
491
492 static int usb4_switch_nvm_read_block(void *data,
493         unsigned int dwaddress, void *buf, size_t dwords)
494 {
495         struct tb_switch *sw = data;
496         u8 status = 0;
497         u32 metadata;
498         int ret;
499
500         metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
501                    USB4_NVM_READ_LENGTH_MASK;
502         metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
503                    USB4_NVM_READ_OFFSET_MASK;
504
505         ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_READ, &metadata,
506                                   &status, NULL, 0, buf, dwords);
507         if (ret)
508                 return ret;
509
510         return status ? -EIO : 0;
511 }
512
513 /**
514  * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
515  * @sw: USB4 router
516  * @address: Starting address in bytes
517  * @buf: Read data is placed here
518  * @size: How many bytes to read
519  *
520  * Reads NVM contents of the router. If NVM is not supported returns
521  * %-EOPNOTSUPP.
522  */
523 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
524                          size_t size)
525 {
526         return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
527                                 usb4_switch_nvm_read_block, sw);
528 }
529
530 static int usb4_switch_nvm_set_offset(struct tb_switch *sw,
531                                       unsigned int address)
532 {
533         u32 metadata, dwaddress;
534         u8 status = 0;
535         int ret;
536
537         dwaddress = address / 4;
538         metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
539                    USB4_NVM_SET_OFFSET_MASK;
540
541         ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &metadata,
542                              &status);
543         if (ret)
544                 return ret;
545
546         return status ? -EIO : 0;
547 }
548
549 static int usb4_switch_nvm_write_next_block(void *data, unsigned int dwaddress,
550                                             const void *buf, size_t dwords)
551 {
552         struct tb_switch *sw = data;
553         u8 status;
554         int ret;
555
556         ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_WRITE, NULL, &status,
557                                   buf, dwords, NULL, 0);
558         if (ret)
559                 return ret;
560
561         return status ? -EIO : 0;
562 }
563
564 /**
565  * usb4_switch_nvm_write() - Write to the router NVM
566  * @sw: USB4 router
567  * @address: Start address where to write in bytes
568  * @buf: Pointer to the data to write
569  * @size: Size of @buf in bytes
570  *
571  * Writes @buf to the router NVM using USB4 router operations. If NVM
572  * write is not supported returns %-EOPNOTSUPP.
573  */
574 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
575                           const void *buf, size_t size)
576 {
577         int ret;
578
579         ret = usb4_switch_nvm_set_offset(sw, address);
580         if (ret)
581                 return ret;
582
583         return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
584                                  usb4_switch_nvm_write_next_block, sw);
585 }
586
587 /**
588  * usb4_switch_nvm_authenticate() - Authenticate new NVM
589  * @sw: USB4 router
590  *
591  * After the new NVM has been written via usb4_switch_nvm_write(), this
592  * function triggers NVM authentication process. The router gets power
593  * cycled and if the authentication is successful the new NVM starts
594  * running. In case of failure returns negative errno.
595  *
596  * The caller should call usb4_switch_nvm_authenticate_status() to read
597  * the status of the authentication after power cycle. It should be the
598  * first router operation to avoid the status being lost.
599  */
600 int usb4_switch_nvm_authenticate(struct tb_switch *sw)
601 {
602         int ret;
603
604         ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, NULL, NULL);
605         switch (ret) {
606         /*
607          * The router is power cycled once NVM_AUTH is started so it is
608          * expected to get any of the following errors back.
609          */
610         case -EACCES:
611         case -ENOTCONN:
612         case -ETIMEDOUT:
613                 return 0;
614
615         default:
616                 return ret;
617         }
618 }
619
620 /**
621  * usb4_switch_nvm_authenticate_status() - Read status of last NVM authenticate
622  * @sw: USB4 router
623  * @status: Status code of the operation
624  *
625  * The function checks if there is status available from the last NVM
626  * authenticate router operation. If there is status then %0 is returned
627  * and the status code is placed in @status. Returns negative errno in case
628  * of failure.
629  *
630  * Must be called before any other router operation.
631  */
632 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status)
633 {
634         const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
635         u16 opcode;
636         u32 val;
637         int ret;
638
639         if (cm_ops->usb4_switch_nvm_authenticate_status) {
640                 ret = cm_ops->usb4_switch_nvm_authenticate_status(sw, status);
641                 if (ret != -EOPNOTSUPP)
642                         return ret;
643         }
644
645         ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
646         if (ret)
647                 return ret;
648
649         /* Check that the opcode is correct */
650         opcode = val & ROUTER_CS_26_OPCODE_MASK;
651         if (opcode == USB4_SWITCH_OP_NVM_AUTH) {
652                 if (val & ROUTER_CS_26_OV)
653                         return -EBUSY;
654                 if (val & ROUTER_CS_26_ONS)
655                         return -EOPNOTSUPP;
656
657                 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
658                         ROUTER_CS_26_STATUS_SHIFT;
659         } else {
660                 *status = 0;
661         }
662
663         return 0;
664 }
665
666 /**
667  * usb4_switch_query_dp_resource() - Query availability of DP IN resource
668  * @sw: USB4 router
669  * @in: DP IN adapter
670  *
671  * For DP tunneling this function can be used to query availability of
672  * DP IN resource. Returns true if the resource is available for DP
673  * tunneling, false otherwise.
674  */
675 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
676 {
677         u32 metadata = in->port;
678         u8 status;
679         int ret;
680
681         ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &metadata,
682                              &status);
683         /*
684          * If DP resource allocation is not supported assume it is
685          * always available.
686          */
687         if (ret == -EOPNOTSUPP)
688                 return true;
689         else if (ret)
690                 return false;
691
692         return !status;
693 }
694
695 /**
696  * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
697  * @sw: USB4 router
698  * @in: DP IN adapter
699  *
700  * Allocates DP IN resource for DP tunneling using USB4 router
701  * operations. If the resource was allocated returns %0. Otherwise
702  * returns negative errno, in particular %-EBUSY if the resource is
703  * already allocated.
704  */
705 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
706 {
707         u32 metadata = in->port;
708         u8 status;
709         int ret;
710
711         ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &metadata,
712                              &status);
713         if (ret == -EOPNOTSUPP)
714                 return 0;
715         else if (ret)
716                 return ret;
717
718         return status ? -EBUSY : 0;
719 }
720
721 /**
722  * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
723  * @sw: USB4 router
724  * @in: DP IN adapter
725  *
726  * Releases the previously allocated DP IN resource.
727  */
728 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
729 {
730         u32 metadata = in->port;
731         u8 status;
732         int ret;
733
734         ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &metadata,
735                              &status);
736         if (ret == -EOPNOTSUPP)
737                 return 0;
738         else if (ret)
739                 return ret;
740
741         return status ? -EIO : 0;
742 }
743
744 static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
745 {
746         struct tb_port *p;
747         int usb4_idx = 0;
748
749         /* Assume port is primary */
750         tb_switch_for_each_port(sw, p) {
751                 if (!tb_port_is_null(p))
752                         continue;
753                 if (tb_is_upstream_port(p))
754                         continue;
755                 if (!p->link_nr) {
756                         if (p == port)
757                                 break;
758                         usb4_idx++;
759                 }
760         }
761
762         return usb4_idx;
763 }
764
765 /**
766  * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
767  * @sw: USB4 router
768  * @port: USB4 port
769  *
770  * USB4 routers have direct mapping between USB4 ports and PCIe
771  * downstream adapters where the PCIe topology is extended. This
772  * function returns the corresponding downstream PCIe adapter or %NULL
773  * if no such mapping was possible.
774  */
775 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
776                                           const struct tb_port *port)
777 {
778         int usb4_idx = usb4_port_idx(sw, port);
779         struct tb_port *p;
780         int pcie_idx = 0;
781
782         /* Find PCIe down port matching usb4_port */
783         tb_switch_for_each_port(sw, p) {
784                 if (!tb_port_is_pcie_down(p))
785                         continue;
786
787                 if (pcie_idx == usb4_idx)
788                         return p;
789
790                 pcie_idx++;
791         }
792
793         return NULL;
794 }
795
796 /**
797  * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
798  * @sw: USB4 router
799  * @port: USB4 port
800  *
801  * USB4 routers have direct mapping between USB4 ports and USB 3.x
802  * downstream adapters where the USB 3.x topology is extended. This
803  * function returns the corresponding downstream USB 3.x adapter or
804  * %NULL if no such mapping was possible.
805  */
806 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
807                                           const struct tb_port *port)
808 {
809         int usb4_idx = usb4_port_idx(sw, port);
810         struct tb_port *p;
811         int usb_idx = 0;
812
813         /* Find USB3 down port matching usb4_port */
814         tb_switch_for_each_port(sw, p) {
815                 if (!tb_port_is_usb3_down(p))
816                         continue;
817
818                 if (usb_idx == usb4_idx)
819                         return p;
820
821                 usb_idx++;
822         }
823
824         return NULL;
825 }
826
827 /**
828  * usb4_port_unlock() - Unlock USB4 downstream port
829  * @port: USB4 port to unlock
830  *
831  * Unlocks USB4 downstream port so that the connection manager can
832  * access the router below this port.
833  */
834 int usb4_port_unlock(struct tb_port *port)
835 {
836         int ret;
837         u32 val;
838
839         ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
840         if (ret)
841                 return ret;
842
843         val &= ~ADP_CS_4_LCK;
844         return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
845 }
846
847 static int usb4_port_set_configured(struct tb_port *port, bool configured)
848 {
849         int ret;
850         u32 val;
851
852         if (!port->cap_usb4)
853                 return -EINVAL;
854
855         ret = tb_port_read(port, &val, TB_CFG_PORT,
856                            port->cap_usb4 + PORT_CS_19, 1);
857         if (ret)
858                 return ret;
859
860         if (configured)
861                 val |= PORT_CS_19_PC;
862         else
863                 val &= ~PORT_CS_19_PC;
864
865         return tb_port_write(port, &val, TB_CFG_PORT,
866                              port->cap_usb4 + PORT_CS_19, 1);
867 }
868
869 /**
870  * usb4_port_configure() - Set USB4 port configured
871  * @port: USB4 router
872  *
873  * Sets the USB4 link to be configured for power management purposes.
874  */
875 int usb4_port_configure(struct tb_port *port)
876 {
877         return usb4_port_set_configured(port, true);
878 }
879
880 /**
881  * usb4_port_unconfigure() - Set USB4 port unconfigured
882  * @port: USB4 router
883  *
884  * Sets the USB4 link to be unconfigured for power management purposes.
885  */
886 void usb4_port_unconfigure(struct tb_port *port)
887 {
888         usb4_port_set_configured(port, false);
889 }
890
891 static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
892 {
893         int ret;
894         u32 val;
895
896         if (!port->cap_usb4)
897                 return -EINVAL;
898
899         ret = tb_port_read(port, &val, TB_CFG_PORT,
900                            port->cap_usb4 + PORT_CS_19, 1);
901         if (ret)
902                 return ret;
903
904         if (configured)
905                 val |= PORT_CS_19_PID;
906         else
907                 val &= ~PORT_CS_19_PID;
908
909         return tb_port_write(port, &val, TB_CFG_PORT,
910                              port->cap_usb4 + PORT_CS_19, 1);
911 }
912
913 /**
914  * usb4_port_configure_xdomain() - Configure port for XDomain
915  * @port: USB4 port connected to another host
916  *
917  * Marks the USB4 port as being connected to another host. Returns %0 in
918  * success and negative errno in failure.
919  */
920 int usb4_port_configure_xdomain(struct tb_port *port)
921 {
922         return usb4_set_xdomain_configured(port, true);
923 }
924
925 /**
926  * usb4_port_unconfigure_xdomain() - Unconfigure port for XDomain
927  * @port: USB4 port that was connected to another host
928  *
929  * Clears USB4 port from being marked as XDomain.
930  */
931 void usb4_port_unconfigure_xdomain(struct tb_port *port)
932 {
933         usb4_set_xdomain_configured(port, false);
934 }
935
936 static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
937                                   u32 value, int timeout_msec)
938 {
939         ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
940
941         do {
942                 u32 val;
943                 int ret;
944
945                 ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
946                 if (ret)
947                         return ret;
948
949                 if ((val & bit) == value)
950                         return 0;
951
952                 usleep_range(50, 100);
953         } while (ktime_before(ktime_get(), timeout));
954
955         return -ETIMEDOUT;
956 }
957
958 static int usb4_port_read_data(struct tb_port *port, void *data, size_t dwords)
959 {
960         if (dwords > NVM_DATA_DWORDS)
961                 return -EINVAL;
962
963         return tb_port_read(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
964                             dwords);
965 }
966
967 static int usb4_port_write_data(struct tb_port *port, const void *data,
968                                 size_t dwords)
969 {
970         if (dwords > NVM_DATA_DWORDS)
971                 return -EINVAL;
972
973         return tb_port_write(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
974                              dwords);
975 }
976
977 static int usb4_port_sb_read(struct tb_port *port, enum usb4_sb_target target,
978                              u8 index, u8 reg, void *buf, u8 size)
979 {
980         size_t dwords = DIV_ROUND_UP(size, 4);
981         int ret;
982         u32 val;
983
984         if (!port->cap_usb4)
985                 return -EINVAL;
986
987         val = reg;
988         val |= size << PORT_CS_1_LENGTH_SHIFT;
989         val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
990         if (target == USB4_SB_TARGET_RETIMER)
991                 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
992         val |= PORT_CS_1_PND;
993
994         ret = tb_port_write(port, &val, TB_CFG_PORT,
995                             port->cap_usb4 + PORT_CS_1, 1);
996         if (ret)
997                 return ret;
998
999         ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1000                                      PORT_CS_1_PND, 0, 500);
1001         if (ret)
1002                 return ret;
1003
1004         ret = tb_port_read(port, &val, TB_CFG_PORT,
1005                             port->cap_usb4 + PORT_CS_1, 1);
1006         if (ret)
1007                 return ret;
1008
1009         if (val & PORT_CS_1_NR)
1010                 return -ENODEV;
1011         if (val & PORT_CS_1_RC)
1012                 return -EIO;
1013
1014         return buf ? usb4_port_read_data(port, buf, dwords) : 0;
1015 }
1016
1017 static int usb4_port_sb_write(struct tb_port *port, enum usb4_sb_target target,
1018                               u8 index, u8 reg, const void *buf, u8 size)
1019 {
1020         size_t dwords = DIV_ROUND_UP(size, 4);
1021         int ret;
1022         u32 val;
1023
1024         if (!port->cap_usb4)
1025                 return -EINVAL;
1026
1027         if (buf) {
1028                 ret = usb4_port_write_data(port, buf, dwords);
1029                 if (ret)
1030                         return ret;
1031         }
1032
1033         val = reg;
1034         val |= size << PORT_CS_1_LENGTH_SHIFT;
1035         val |= PORT_CS_1_WNR_WRITE;
1036         val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1037         if (target == USB4_SB_TARGET_RETIMER)
1038                 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1039         val |= PORT_CS_1_PND;
1040
1041         ret = tb_port_write(port, &val, TB_CFG_PORT,
1042                             port->cap_usb4 + PORT_CS_1, 1);
1043         if (ret)
1044                 return ret;
1045
1046         ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1047                                      PORT_CS_1_PND, 0, 500);
1048         if (ret)
1049                 return ret;
1050
1051         ret = tb_port_read(port, &val, TB_CFG_PORT,
1052                             port->cap_usb4 + PORT_CS_1, 1);
1053         if (ret)
1054                 return ret;
1055
1056         if (val & PORT_CS_1_NR)
1057                 return -ENODEV;
1058         if (val & PORT_CS_1_RC)
1059                 return -EIO;
1060
1061         return 0;
1062 }
1063
1064 static int usb4_port_sb_op(struct tb_port *port, enum usb4_sb_target target,
1065                            u8 index, enum usb4_sb_opcode opcode, int timeout_msec)
1066 {
1067         ktime_t timeout;
1068         u32 val;
1069         int ret;
1070
1071         val = opcode;
1072         ret = usb4_port_sb_write(port, target, index, USB4_SB_OPCODE, &val,
1073                                  sizeof(val));
1074         if (ret)
1075                 return ret;
1076
1077         timeout = ktime_add_ms(ktime_get(), timeout_msec);
1078
1079         do {
1080                 /* Check results */
1081                 ret = usb4_port_sb_read(port, target, index, USB4_SB_OPCODE,
1082                                         &val, sizeof(val));
1083                 if (ret)
1084                         return ret;
1085
1086                 switch (val) {
1087                 case 0:
1088                         return 0;
1089
1090                 case USB4_SB_OPCODE_ERR:
1091                         return -EAGAIN;
1092
1093                 case USB4_SB_OPCODE_ONS:
1094                         return -EOPNOTSUPP;
1095
1096                 default:
1097                         if (val != opcode)
1098                                 return -EIO;
1099                         break;
1100                 }
1101         } while (ktime_before(ktime_get(), timeout));
1102
1103         return -ETIMEDOUT;
1104 }
1105
1106 /**
1107  * usb4_port_enumerate_retimers() - Send RT broadcast transaction
1108  * @port: USB4 port
1109  *
1110  * This forces the USB4 port to send broadcast RT transaction which
1111  * makes the retimers on the link to assign index to themselves. Returns
1112  * %0 in case of success and negative errno if there was an error.
1113  */
1114 int usb4_port_enumerate_retimers(struct tb_port *port)
1115 {
1116         u32 val;
1117
1118         val = USB4_SB_OPCODE_ENUMERATE_RETIMERS;
1119         return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1120                                   USB4_SB_OPCODE, &val, sizeof(val));
1121 }
1122
1123 static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
1124                                        enum usb4_sb_opcode opcode,
1125                                        int timeout_msec)
1126 {
1127         return usb4_port_sb_op(port, USB4_SB_TARGET_RETIMER, index, opcode,
1128                                timeout_msec);
1129 }
1130
1131 /**
1132  * usb4_port_retimer_read() - Read from retimer sideband registers
1133  * @port: USB4 port
1134  * @index: Retimer index
1135  * @reg: Sideband register to read
1136  * @buf: Data from @reg is stored here
1137  * @size: Number of bytes to read
1138  *
1139  * Function reads retimer sideband registers starting from @reg. The
1140  * retimer is connected to @port at @index. Returns %0 in case of
1141  * success, and read data is copied to @buf. If there is no retimer
1142  * present at given @index returns %-ENODEV. In any other failure
1143  * returns negative errno.
1144  */
1145 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1146                            u8 size)
1147 {
1148         return usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1149                                  size);
1150 }
1151
1152 /**
1153  * usb4_port_retimer_write() - Write to retimer sideband registers
1154  * @port: USB4 port
1155  * @index: Retimer index
1156  * @reg: Sideband register to write
1157  * @buf: Data that is written starting from @reg
1158  * @size: Number of bytes to write
1159  *
1160  * Writes retimer sideband registers starting from @reg. The retimer is
1161  * connected to @port at @index. Returns %0 in case of success. If there
1162  * is no retimer present at given @index returns %-ENODEV. In any other
1163  * failure returns negative errno.
1164  */
1165 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1166                             const void *buf, u8 size)
1167 {
1168         return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1169                                   size);
1170 }
1171
1172 /**
1173  * usb4_port_retimer_is_last() - Is the retimer last on-board retimer
1174  * @port: USB4 port
1175  * @index: Retimer index
1176  *
1177  * If the retimer at @index is last one (connected directly to the
1178  * Type-C port) this function returns %1. If it is not returns %0. If
1179  * the retimer is not present returns %-ENODEV. Otherwise returns
1180  * negative errno.
1181  */
1182 int usb4_port_retimer_is_last(struct tb_port *port, u8 index)
1183 {
1184         u32 metadata;
1185         int ret;
1186
1187         ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_LAST_RETIMER,
1188                                    500);
1189         if (ret)
1190                 return ret;
1191
1192         ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1193                                      sizeof(metadata));
1194         return ret ? ret : metadata & 1;
1195 }
1196
1197 /**
1198  * usb4_port_retimer_nvm_sector_size() - Read retimer NVM sector size
1199  * @port: USB4 port
1200  * @index: Retimer index
1201  *
1202  * Reads NVM sector size (in bytes) of a retimer at @index. This
1203  * operation can be used to determine whether the retimer supports NVM
1204  * upgrade for example. Returns sector size in bytes or negative errno
1205  * in case of error. Specifically returns %-ENODEV if there is no
1206  * retimer at @index.
1207  */
1208 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
1209 {
1210         u32 metadata;
1211         int ret;
1212
1213         ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE,
1214                                    500);
1215         if (ret)
1216                 return ret;
1217
1218         ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1219                                      sizeof(metadata));
1220         return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
1221 }
1222
1223 static int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1224                                             unsigned int address)
1225 {
1226         u32 metadata, dwaddress;
1227         int ret;
1228
1229         dwaddress = address / 4;
1230         metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
1231                   USB4_NVM_SET_OFFSET_MASK;
1232
1233         ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1234                                       sizeof(metadata));
1235         if (ret)
1236                 return ret;
1237
1238         return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_SET_OFFSET,
1239                                     500);
1240 }
1241
1242 struct retimer_info {
1243         struct tb_port *port;
1244         u8 index;
1245 };
1246
1247 static int usb4_port_retimer_nvm_write_next_block(void *data,
1248         unsigned int dwaddress, const void *buf, size_t dwords)
1249
1250 {
1251         const struct retimer_info *info = data;
1252         struct tb_port *port = info->port;
1253         u8 index = info->index;
1254         int ret;
1255
1256         ret = usb4_port_retimer_write(port, index, USB4_SB_DATA,
1257                                       buf, dwords * 4);
1258         if (ret)
1259                 return ret;
1260
1261         return usb4_port_retimer_op(port, index,
1262                         USB4_SB_OPCODE_NVM_BLOCK_WRITE, 1000);
1263 }
1264
1265 /**
1266  * usb4_port_retimer_nvm_write() - Write to retimer NVM
1267  * @port: USB4 port
1268  * @index: Retimer index
1269  * @address: Byte address where to start the write
1270  * @buf: Data to write
1271  * @size: Size in bytes how much to write
1272  *
1273  * Writes @size bytes from @buf to the retimer NVM. Used for NVM
1274  * upgrade. Returns %0 if the data was written successfully and negative
1275  * errno in case of failure. Specifically returns %-ENODEV if there is
1276  * no retimer at @index.
1277  */
1278 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index, unsigned int address,
1279                                 const void *buf, size_t size)
1280 {
1281         struct retimer_info info = { .port = port, .index = index };
1282         int ret;
1283
1284         ret = usb4_port_retimer_nvm_set_offset(port, index, address);
1285         if (ret)
1286                 return ret;
1287
1288         return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
1289                                  usb4_port_retimer_nvm_write_next_block, &info);
1290 }
1291
1292 /**
1293  * usb4_port_retimer_nvm_authenticate() - Start retimer NVM upgrade
1294  * @port: USB4 port
1295  * @index: Retimer index
1296  *
1297  * After the new NVM image has been written via usb4_port_retimer_nvm_write()
1298  * this function can be used to trigger the NVM upgrade process. If
1299  * successful the retimer restarts with the new NVM and may not have the
1300  * index set so one needs to call usb4_port_enumerate_retimers() to
1301  * force index to be assigned.
1302  */
1303 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index)
1304 {
1305         u32 val;
1306
1307         /*
1308          * We need to use the raw operation here because once the
1309          * authentication completes the retimer index is not set anymore
1310          * so we do not get back the status now.
1311          */
1312         val = USB4_SB_OPCODE_NVM_AUTH_WRITE;
1313         return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1314                                   USB4_SB_OPCODE, &val, sizeof(val));
1315 }
1316
1317 /**
1318  * usb4_port_retimer_nvm_authenticate_status() - Read status of NVM upgrade
1319  * @port: USB4 port
1320  * @index: Retimer index
1321  * @status: Raw status code read from metadata
1322  *
1323  * This can be called after usb4_port_retimer_nvm_authenticate() and
1324  * usb4_port_enumerate_retimers() to fetch status of the NVM upgrade.
1325  *
1326  * Returns %0 if the authentication status was successfully read. The
1327  * completion metadata (the result) is then stored into @status. If
1328  * reading the status fails, returns negative errno.
1329  */
1330 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1331                                               u32 *status)
1332 {
1333         u32 metadata, val;
1334         int ret;
1335
1336         ret = usb4_port_retimer_read(port, index, USB4_SB_OPCODE, &val,
1337                                      sizeof(val));
1338         if (ret)
1339                 return ret;
1340
1341         switch (val) {
1342         case 0:
1343                 *status = 0;
1344                 return 0;
1345
1346         case USB4_SB_OPCODE_ERR:
1347                 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA,
1348                                              &metadata, sizeof(metadata));
1349                 if (ret)
1350                         return ret;
1351
1352                 *status = metadata & USB4_SB_METADATA_NVM_AUTH_WRITE_MASK;
1353                 return 0;
1354
1355         case USB4_SB_OPCODE_ONS:
1356                 return -EOPNOTSUPP;
1357
1358         default:
1359                 return -EIO;
1360         }
1361 }
1362
1363 static int usb4_port_retimer_nvm_read_block(void *data, unsigned int dwaddress,
1364                                             void *buf, size_t dwords)
1365 {
1366         const struct retimer_info *info = data;
1367         struct tb_port *port = info->port;
1368         u8 index = info->index;
1369         u32 metadata;
1370         int ret;
1371
1372         metadata = dwaddress << USB4_NVM_READ_OFFSET_SHIFT;
1373         if (dwords < NVM_DATA_DWORDS)
1374                 metadata |= dwords << USB4_NVM_READ_LENGTH_SHIFT;
1375
1376         ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1377                                       sizeof(metadata));
1378         if (ret)
1379                 return ret;
1380
1381         ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_READ, 500);
1382         if (ret)
1383                 return ret;
1384
1385         return usb4_port_retimer_read(port, index, USB4_SB_DATA, buf,
1386                                       dwords * 4);
1387 }
1388
1389 /**
1390  * usb4_port_retimer_nvm_read() - Read contents of retimer NVM
1391  * @port: USB4 port
1392  * @index: Retimer index
1393  * @address: NVM address (in bytes) to start reading
1394  * @buf: Data read from NVM is stored here
1395  * @size: Number of bytes to read
1396  *
1397  * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
1398  * read was successful and negative errno in case of failure.
1399  * Specifically returns %-ENODEV if there is no retimer at @index.
1400  */
1401 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1402                                unsigned int address, void *buf, size_t size)
1403 {
1404         struct retimer_info info = { .port = port, .index = index };
1405
1406         return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
1407                                 usb4_port_retimer_nvm_read_block, &info);
1408 }
1409
1410 /**
1411  * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate
1412  * @port: USB3 adapter port
1413  *
1414  * Return maximum supported link rate of a USB3 adapter in Mb/s.
1415  * Negative errno in case of error.
1416  */
1417 int usb4_usb3_port_max_link_rate(struct tb_port *port)
1418 {
1419         int ret, lr;
1420         u32 val;
1421
1422         if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
1423                 return -EINVAL;
1424
1425         ret = tb_port_read(port, &val, TB_CFG_PORT,
1426                            port->cap_adap + ADP_USB3_CS_4, 1);
1427         if (ret)
1428                 return ret;
1429
1430         lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
1431         return lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
1432 }
1433
1434 /**
1435  * usb4_usb3_port_actual_link_rate() - Established USB3 link rate
1436  * @port: USB3 adapter port
1437  *
1438  * Return actual established link rate of a USB3 adapter in Mb/s. If the
1439  * link is not up returns %0 and negative errno in case of failure.
1440  */
1441 int usb4_usb3_port_actual_link_rate(struct tb_port *port)
1442 {
1443         int ret, lr;
1444         u32 val;
1445
1446         if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
1447                 return -EINVAL;
1448
1449         ret = tb_port_read(port, &val, TB_CFG_PORT,
1450                            port->cap_adap + ADP_USB3_CS_4, 1);
1451         if (ret)
1452                 return ret;
1453
1454         if (!(val & ADP_USB3_CS_4_ULV))
1455                 return 0;
1456
1457         lr = val & ADP_USB3_CS_4_ALR_MASK;
1458         return lr == ADP_USB3_CS_4_ALR_20G ? 20000 : 10000;
1459 }
1460
1461 static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
1462 {
1463         int ret;
1464         u32 val;
1465
1466         if (!tb_port_is_usb3_down(port))
1467                 return -EINVAL;
1468         if (tb_route(port->sw))
1469                 return -EINVAL;
1470
1471         ret = tb_port_read(port, &val, TB_CFG_PORT,
1472                            port->cap_adap + ADP_USB3_CS_2, 1);
1473         if (ret)
1474                 return ret;
1475
1476         if (request)
1477                 val |= ADP_USB3_CS_2_CMR;
1478         else
1479                 val &= ~ADP_USB3_CS_2_CMR;
1480
1481         ret = tb_port_write(port, &val, TB_CFG_PORT,
1482                             port->cap_adap + ADP_USB3_CS_2, 1);
1483         if (ret)
1484                 return ret;
1485
1486         /*
1487          * We can use val here directly as the CMR bit is in the same place
1488          * as HCA. Just mask out others.
1489          */
1490         val &= ADP_USB3_CS_2_CMR;
1491         return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
1492                                       ADP_USB3_CS_1_HCA, val, 1500);
1493 }
1494
1495 static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
1496 {
1497         return usb4_usb3_port_cm_request(port, true);
1498 }
1499
1500 static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
1501 {
1502         return usb4_usb3_port_cm_request(port, false);
1503 }
1504
1505 static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
1506 {
1507         unsigned long uframes;
1508
1509         uframes = bw * 512UL << scale;
1510         return DIV_ROUND_CLOSEST(uframes * 8000, 1000 * 1000);
1511 }
1512
1513 static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
1514 {
1515         unsigned long uframes;
1516
1517         /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
1518         uframes = ((unsigned long)mbps * 1000 *  1000) / 8000;
1519         return DIV_ROUND_UP(uframes, 512UL << scale);
1520 }
1521
1522 static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
1523                                                    int *upstream_bw,
1524                                                    int *downstream_bw)
1525 {
1526         u32 val, bw, scale;
1527         int ret;
1528
1529         ret = tb_port_read(port, &val, TB_CFG_PORT,
1530                            port->cap_adap + ADP_USB3_CS_2, 1);
1531         if (ret)
1532                 return ret;
1533
1534         ret = tb_port_read(port, &scale, TB_CFG_PORT,
1535                            port->cap_adap + ADP_USB3_CS_3, 1);
1536         if (ret)
1537                 return ret;
1538
1539         scale &= ADP_USB3_CS_3_SCALE_MASK;
1540
1541         bw = val & ADP_USB3_CS_2_AUBW_MASK;
1542         *upstream_bw = usb3_bw_to_mbps(bw, scale);
1543
1544         bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
1545         *downstream_bw = usb3_bw_to_mbps(bw, scale);
1546
1547         return 0;
1548 }
1549
1550 /**
1551  * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
1552  * @port: USB3 adapter port
1553  * @upstream_bw: Allocated upstream bandwidth is stored here
1554  * @downstream_bw: Allocated downstream bandwidth is stored here
1555  *
1556  * Stores currently allocated USB3 bandwidth into @upstream_bw and
1557  * @downstream_bw in Mb/s. Returns %0 in case of success and negative
1558  * errno in failure.
1559  */
1560 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1561                                        int *downstream_bw)
1562 {
1563         int ret;
1564
1565         ret = usb4_usb3_port_set_cm_request(port);
1566         if (ret)
1567                 return ret;
1568
1569         ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
1570                                                       downstream_bw);
1571         usb4_usb3_port_clear_cm_request(port);
1572
1573         return ret;
1574 }
1575
1576 static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
1577                                                   int *upstream_bw,
1578                                                   int *downstream_bw)
1579 {
1580         u32 val, bw, scale;
1581         int ret;
1582
1583         ret = tb_port_read(port, &val, TB_CFG_PORT,
1584                            port->cap_adap + ADP_USB3_CS_1, 1);
1585         if (ret)
1586                 return ret;
1587
1588         ret = tb_port_read(port, &scale, TB_CFG_PORT,
1589                            port->cap_adap + ADP_USB3_CS_3, 1);
1590         if (ret)
1591                 return ret;
1592
1593         scale &= ADP_USB3_CS_3_SCALE_MASK;
1594
1595         bw = val & ADP_USB3_CS_1_CUBW_MASK;
1596         *upstream_bw = usb3_bw_to_mbps(bw, scale);
1597
1598         bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
1599         *downstream_bw = usb3_bw_to_mbps(bw, scale);
1600
1601         return 0;
1602 }
1603
1604 static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
1605                                                     int upstream_bw,
1606                                                     int downstream_bw)
1607 {
1608         u32 val, ubw, dbw, scale;
1609         int ret;
1610
1611         /* Read the used scale, hardware default is 0 */
1612         ret = tb_port_read(port, &scale, TB_CFG_PORT,
1613                            port->cap_adap + ADP_USB3_CS_3, 1);
1614         if (ret)
1615                 return ret;
1616
1617         scale &= ADP_USB3_CS_3_SCALE_MASK;
1618         ubw = mbps_to_usb3_bw(upstream_bw, scale);
1619         dbw = mbps_to_usb3_bw(downstream_bw, scale);
1620
1621         ret = tb_port_read(port, &val, TB_CFG_PORT,
1622                            port->cap_adap + ADP_USB3_CS_2, 1);
1623         if (ret)
1624                 return ret;
1625
1626         val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
1627         val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
1628         val |= ubw;
1629
1630         return tb_port_write(port, &val, TB_CFG_PORT,
1631                              port->cap_adap + ADP_USB3_CS_2, 1);
1632 }
1633
1634 /**
1635  * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
1636  * @port: USB3 adapter port
1637  * @upstream_bw: New upstream bandwidth
1638  * @downstream_bw: New downstream bandwidth
1639  *
1640  * This can be used to set how much bandwidth is allocated for the USB3
1641  * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
1642  * new values programmed to the USB3 adapter allocation registers. If
1643  * the values are lower than what is currently consumed the allocation
1644  * is set to what is currently consumed instead (consumed bandwidth
1645  * cannot be taken away by CM). The actual new values are returned in
1646  * @upstream_bw and @downstream_bw.
1647  *
1648  * Returns %0 in case of success and negative errno if there was a
1649  * failure.
1650  */
1651 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1652                                       int *downstream_bw)
1653 {
1654         int ret, consumed_up, consumed_down, allocate_up, allocate_down;
1655
1656         ret = usb4_usb3_port_set_cm_request(port);
1657         if (ret)
1658                 return ret;
1659
1660         ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
1661                                                      &consumed_down);
1662         if (ret)
1663                 goto err_request;
1664
1665         /* Don't allow it go lower than what is consumed */
1666         allocate_up = max(*upstream_bw, consumed_up);
1667         allocate_down = max(*downstream_bw, consumed_down);
1668
1669         ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
1670                                                        allocate_down);
1671         if (ret)
1672                 goto err_request;
1673
1674         *upstream_bw = allocate_up;
1675         *downstream_bw = allocate_down;
1676
1677 err_request:
1678         usb4_usb3_port_clear_cm_request(port);
1679         return ret;
1680 }
1681
1682 /**
1683  * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
1684  * @port: USB3 adapter port
1685  * @upstream_bw: New allocated upstream bandwidth
1686  * @downstream_bw: New allocated downstream bandwidth
1687  *
1688  * Releases USB3 allocated bandwidth down to what is actually consumed.
1689  * The new bandwidth is returned in @upstream_bw and @downstream_bw.
1690  *
1691  * Returns 0% in success and negative errno in case of failure.
1692  */
1693 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1694                                      int *downstream_bw)
1695 {
1696         int ret, consumed_up, consumed_down;
1697
1698         ret = usb4_usb3_port_set_cm_request(port);
1699         if (ret)
1700                 return ret;
1701
1702         ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
1703                                                      &consumed_down);
1704         if (ret)
1705                 goto err_request;
1706
1707         /*
1708          * Always keep 1000 Mb/s to make sure xHCI has at least some
1709          * bandwidth available for isochronous traffic.
1710          */
1711         if (consumed_up < 1000)
1712                 consumed_up = 1000;
1713         if (consumed_down < 1000)
1714                 consumed_down = 1000;
1715
1716         ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
1717                                                        consumed_down);
1718         if (ret)
1719                 goto err_request;
1720
1721         *upstream_bw = consumed_up;
1722         *downstream_bw = consumed_down;
1723
1724 err_request:
1725         usb4_usb3_port_clear_cm_request(port);
1726         return ret;
1727 }