1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* Thunderbolt alternate mode support
* Refer to USB Type-C Cable and Connector Specification Release 2.0 Section F
*/
#include <stdbool.h>
#include <stdint.h>
#include "compile_time_macros.h"
#include "console.h"
#include "tcpm.h"
#include "usb_mux.h"
#include "usb_pd.h"
#include "usb_pd_dpm.h"
#include "usb_pd_tbt.h"
#include "usb_pe_sm.h"
#include "usb_tbt_alt_mode.h"
/*
* Enter/Exit TBT mode with active cable
*
*
* TBT_START |------------
* retry_done = false | |
* | v |
* |<------------------| Exit Mode SOP |
* | retry_done = true | | |
* v | | ACK/NAK |
* Enter Mode SOP' | --------|--------- |
* ACK | NAK | Exit Mode SOP'' |
* |------|------| | | |
* | | | | ACK/NAK |
* v | | --------|--------- |
* Enter Mode SOP'' | | Exit Mode SOP' |
* | | | | |
* ACK | NAK | | | ACK/NAK |
* |------|------| | | ------------------ |
* | | | | retry_done == true? |
* v | | | | |
* Enter Mode SOP | | | No | |
* | | | |----------- |
* ACK | NAK | | |Yes |
* |-------|------| | | v |
* | | | | TBT_INACTIVE |
* v | | | retry_done = false |
* TBT_ACTIVE | | | |
* retry_done = true | | | |
* | | | | |
* v v v v |
* -----------------------------------------------------------------|
*/
#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args)
#else
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#endif
/*
* If a partner sends an Enter Mode NAK, Exit Mode and try again. This has
* happened when the EC loses state after previously entering an alt mode
* with a partner. It may be fixed in b/159495742, in which case this
* logic is unneeded.
*/
static bool retry_done;
static int tbt_prints(const char *string, int port)
{
return CPRINTS("C%d: TBT %s", port, string);
}
/* The states of Thunderbolt negotiation */
enum tbt_states {
TBT_START = 0,
TBT_ENTER_SOP,
TBT_ACTIVE,
TBT_EXIT_SOP,
TBT_INACTIVE,
/* Active cable only */
TBT_ENTER_SOP_PRIME,
TBT_ENTER_SOP_PRIME_PRIME,
TBT_EXIT_SOP_PRIME,
TBT_EXIT_SOP_PRIME_PRIME,
TBT_STATE_COUNT,
};
static enum tbt_states tbt_state[CONFIG_USB_PD_PORT_MAX_COUNT];
static const uint8_t state_vdm_cmd[TBT_STATE_COUNT] = {
[TBT_ENTER_SOP] = CMD_ENTER_MODE,
[TBT_ACTIVE] = CMD_EXIT_MODE,
[TBT_EXIT_SOP] = CMD_EXIT_MODE,
/* Active cable only */
[TBT_ENTER_SOP_PRIME] = CMD_ENTER_MODE,
[TBT_ENTER_SOP_PRIME_PRIME] = CMD_ENTER_MODE,
[TBT_EXIT_SOP_PRIME] = CMD_EXIT_MODE,
[TBT_EXIT_SOP_PRIME_PRIME] = CMD_EXIT_MODE,
};
void tbt_init(int port)
{
tbt_state[port] = TBT_START;
retry_done = false;
}
bool tbt_is_active(int port)
{
return tbt_state[port] == TBT_ACTIVE;
}
void tbt_teardown(int port)
{
tbt_prints("teardown", port);
tbt_state[port] = TBT_INACTIVE;
retry_done = false;
}
static void tbt_entry_failed(int port)
{
tbt_prints("alt mode protocol failed!", port);
tbt_state[port] = TBT_INACTIVE;
retry_done = false;
dpm_set_mode_entry_done(port);
}
static bool tbt_response_valid(int port, enum tcpm_transmit_type type,
char *cmdt, int vdm_cmd)
{
enum tbt_states st = tbt_state[port];
/*
* Check for an unexpected response.
* 1. invalid command
* 2. invalid Tx type for passive cable
* If Thunderbolt is inactive, ignore the command.
*/
if ((st != TBT_INACTIVE && state_vdm_cmd[st] != vdm_cmd) ||
(get_usb_pd_cable_type(port) == IDH_PTYPE_PCABLE &&
type != TCPC_TX_SOP)) {
tbt_entry_failed(port);
return false;
}
return true;
}
/* Exit Mode process is complete, but retry Enter Mode process */
static void tbt_retry_enter_mode(int port)
{
tbt_state[port] = TBT_START;
retry_done = true;
}
/* Send Exit Mode to SOP''(if supported), or SOP' */
static void tbt_active_cable_exit_mode(int port)
{
struct pd_discovery *disc;
disc = pd_get_am_discovery(port, TCPC_TX_SOP_PRIME);
if (disc->identity.product_t1.a_rev20.sop_p_p)
tbt_state[port] = TBT_EXIT_SOP_PRIME_PRIME;
else
tbt_state[port] = TBT_EXIT_SOP_PRIME;
}
void intel_vdm_acked(int port, enum tcpm_transmit_type type, int vdo_count,
uint32_t *vdm)
{
struct pd_discovery *disc;
const uint8_t vdm_cmd = PD_VDO_CMD(vdm[0]);
if (!tbt_response_valid(port, type, "ACK", vdm_cmd))
return;
disc = pd_get_am_discovery(port, TCPC_TX_SOP_PRIME);
switch (tbt_state[port]) {
case TBT_ENTER_SOP_PRIME:
if (disc->identity.product_t1.a_rev20.sop_p_p)
tbt_state[port] = TBT_ENTER_SOP_PRIME_PRIME;
else
tbt_state[port] = TBT_ENTER_SOP;
break;
case TBT_ENTER_SOP_PRIME_PRIME:
tbt_state[port] = TBT_ENTER_SOP;
break;
case TBT_ENTER_SOP:
set_tbt_compat_mode_ready(port);
dpm_set_mode_entry_done(port);
tbt_state[port] = TBT_ACTIVE;
retry_done = true;
tbt_prints("enter mode SOP", port);
break;
case TBT_ACTIVE:
if (get_usb_pd_cable_type(port) == IDH_PTYPE_ACABLE)
tbt_active_cable_exit_mode(port);
else {
/*
* Exit Mode process is complete; go to inactive state.
*/
tbt_prints("exit mode SOP", port);
tbt_state[port] = TBT_INACTIVE;
retry_done = false;
}
break;
case TBT_EXIT_SOP:
if (get_usb_pd_cable_type(port) == IDH_PTYPE_ACABLE)
tbt_active_cable_exit_mode(port);
else {
if (retry_done)
/* retried enter mode, still failed, give up */
tbt_entry_failed(port);
else
tbt_retry_enter_mode(port);
}
break;
case TBT_EXIT_SOP_PRIME_PRIME:
tbt_state[port] = TBT_EXIT_SOP_PRIME;
break;
case TBT_EXIT_SOP_PRIME:
if (retry_done) {
/*
* Exit mode process is complete; go to inactive state.
*/
tbt_prints("exit mode SOP'", port);
tbt_entry_failed(port);
} else {
tbt_retry_enter_mode(port);
}
break;
case TBT_INACTIVE:
/*
* This can occur if the mode is shutdown because
* the CPU is being turned off, and an exit mode
* command has been sent.
*/
break;
default:
/* Invalid or unexpected negotiation state */
CPRINTF("%s called with invalid state %d\n",
__func__, tbt_state[port]);
tbt_entry_failed(port);
break;
}
}
void intel_vdm_naked(int port, enum tcpm_transmit_type type, uint8_t vdm_cmd)
{
if (!tbt_response_valid(port, type, "NAK", vdm_cmd))
return;
switch (tbt_state[port]) {
case TBT_ENTER_SOP_PRIME:
case TBT_ENTER_SOP_PRIME_PRIME:
case TBT_ENTER_SOP:
/*
* If a request to enter Thunderbolt mode is NAK'ed, this
* likely means the partner is already in Thunderbolt alt mode,
* so request to exit the mode first before retrying the enter
* command. This can happen if the EC is restarted
*/
tbt_state[port] = TBT_EXIT_SOP;
break;
case TBT_ACTIVE:
/* Exit SOP got NAK'ed */
if (get_usb_pd_cable_type(port) == IDH_PTYPE_ACABLE)
tbt_active_cable_exit_mode(port);
else {
tbt_prints("exit mode SOP failed", port);
tbt_state[port] = TBT_INACTIVE;
retry_done = false;
}
break;
case TBT_EXIT_SOP:
/* Exit SOP got NAK'ed */
if (get_usb_pd_cable_type(port) == IDH_PTYPE_ACABLE)
tbt_active_cable_exit_mode(port);
else {
if (retry_done)
/* Retried enter mode, still failed, give up */
tbt_entry_failed(port);
else
tbt_retry_enter_mode(port);
}
break;
case TBT_EXIT_SOP_PRIME_PRIME:
tbt_prints("exit mode SOP'' failed", port);
tbt_state[port] = TBT_EXIT_SOP_PRIME;
break;
case TBT_EXIT_SOP_PRIME:
if (retry_done) {
/*
* Exit mode process is complete; go to inactive state.
*/
tbt_prints("exit mode SOP' failed", port);
tbt_entry_failed(port);
} else {
tbt_retry_enter_mode(port);
}
break;
default:
CPRINTS("C%d: NAK for cmd %d in state %d", port,
vdm_cmd, tbt_state[port]);
tbt_entry_failed(port);
break;
}
}
static bool tbt_mode_is_supported(int port, int vdo_count)
{
const struct pd_discovery *disc =
pd_get_am_discovery(port, TCPC_TX_SOP);
return disc->identity.idh.modal_support &&
is_tbt_cable_superspeed(port) &&
get_tbt_cable_speed(port) >= TBT_SS_U31_GEN1;
}
int tbt_setup_next_vdm(int port, int vdo_count, uint32_t *vdm,
enum tcpm_transmit_type *tx_type)
{
struct svdm_amode_data *modep;
int vdo_count_ret = 0;
*tx_type = TCPC_TX_SOP;
if (vdo_count < VDO_MAX_SIZE)
return -1;
switch (tbt_state[port]) {
case TBT_START:
if (!tbt_mode_is_supported(port, vdo_count))
return 0;
if (!retry_done)
tbt_prints("attempt to enter mode", port);
else
tbt_prints("retry to enter mode", port);
/* Active cable send Enter Mode SOP' first */
if (get_usb_pd_cable_type(port) == IDH_PTYPE_ACABLE) {
vdo_count_ret =
enter_tbt_compat_mode(
port, TCPC_TX_SOP_PRIME, vdm);
*tx_type = TCPC_TX_SOP_PRIME;
tbt_state[port] = TBT_ENTER_SOP_PRIME;
} else {
/* Passive cable send Enter Mode SOP */
vdo_count_ret =
enter_tbt_compat_mode(port, TCPC_TX_SOP, vdm);
tbt_state[port] = TBT_ENTER_SOP;
}
break;
case TBT_ENTER_SOP_PRIME:
vdo_count_ret =
enter_tbt_compat_mode(port, TCPC_TX_SOP_PRIME, vdm);
*tx_type = TCPC_TX_SOP_PRIME;
break;
case TBT_ENTER_SOP_PRIME_PRIME:
vdo_count_ret =
enter_tbt_compat_mode(
port, TCPC_TX_SOP_PRIME_PRIME, vdm);
*tx_type = TCPC_TX_SOP_PRIME_PRIME;
break;
case TBT_ENTER_SOP:
vdo_count_ret =
enter_tbt_compat_mode(port, TCPC_TX_SOP, vdm);
break;
case TBT_EXIT_SOP:
case TBT_ACTIVE:
/*
* Called to exit Thunderbolt alt mode, either when the mode is
* active and the system is shutting down, or when an initial
* request to enter the mode is NAK'ed. This can happen if EC
* is restarted while Thunderbolt mode is active.
*/
modep = pd_get_amode_data(port,
TCPC_TX_SOP, USB_VID_INTEL);
if (!(modep && modep->opos))
return -1;
vdm[0] = VDO(USB_VID_INTEL, 1, CMD_EXIT_MODE) |
VDO_OPOS(modep->opos) |
VDO_CMDT(CMDT_INIT) |
VDO_SVDM_VERS(
pd_get_vdo_ver(port, TCPC_TX_SOP));
vdo_count_ret = 1;
break;
case TBT_EXIT_SOP_PRIME_PRIME:
modep = pd_get_amode_data(port,
TCPC_TX_SOP_PRIME, USB_VID_INTEL);
if (!(modep && modep->opos))
return -1;
vdm[0] = VDO(USB_VID_INTEL, 1, CMD_EXIT_MODE) |
VDO_OPOS(modep->opos) |
VDO_CMDT(CMDT_INIT) |
VDO_SVDM_VERS(pd_get_vdo_ver(port,
TCPC_TX_SOP_PRIME_PRIME));
vdo_count_ret = 1;
*tx_type = TCPC_TX_SOP_PRIME_PRIME;
break;
case TBT_EXIT_SOP_PRIME:
modep = pd_get_amode_data(port,
TCPC_TX_SOP_PRIME, USB_VID_INTEL);
if (!(modep && modep->opos))
return -1;
vdm[0] = VDO(USB_VID_INTEL, 1, CMD_EXIT_MODE) |
VDO_OPOS(modep->opos) |
VDO_CMDT(CMDT_INIT) |
VDO_SVDM_VERS(pd_get_vdo_ver(port, TCPC_TX_SOP_PRIME));
vdo_count_ret = 1;
*tx_type = TCPC_TX_SOP_PRIME;
break;
case TBT_INACTIVE:
/* Thunderbolt mode is inactive */
return -1;
default:
CPRINTF("%s called with invalid state %d\n",
__func__, tbt_state[port]);
return -1;
}
return vdo_count_ret;
}
|