]>
Commit | Line | Data |
---|---|---|
f67cf491 MW |
1 | /* |
2 | * Internal Thunderbolt Connection Manager. This is a firmware running on | |
3 | * the Thunderbolt host controller performing most of the low-level | |
4 | * handling. | |
5 | * | |
6 | * Copyright (C) 2017, Intel Corporation | |
7 | * Authors: Michael Jamet <michael.jamet@intel.com> | |
8 | * Mika Westerberg <mika.westerberg@linux.intel.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | */ | |
14 | ||
15 | #include <linux/delay.h> | |
f67cf491 MW |
16 | #include <linux/mutex.h> |
17 | #include <linux/pci.h> | |
2de98e05 | 18 | #include <linux/pm_runtime.h> |
630b3aff | 19 | #include <linux/platform_data/x86/apple.h> |
f67cf491 MW |
20 | #include <linux/sizes.h> |
21 | #include <linux/slab.h> | |
22 | #include <linux/workqueue.h> | |
23 | ||
24 | #include "ctl.h" | |
25 | #include "nhi_regs.h" | |
26 | #include "tb.h" | |
27 | ||
28 | #define PCIE2CIO_CMD 0x30 | |
29 | #define PCIE2CIO_CMD_TIMEOUT BIT(31) | |
30 | #define PCIE2CIO_CMD_START BIT(30) | |
31 | #define PCIE2CIO_CMD_WRITE BIT(21) | |
32 | #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19) | |
33 | #define PCIE2CIO_CMD_CS_SHIFT 19 | |
34 | #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13) | |
35 | #define PCIE2CIO_CMD_PORT_SHIFT 13 | |
36 | ||
37 | #define PCIE2CIO_WRDATA 0x34 | |
38 | #define PCIE2CIO_RDDATA 0x38 | |
39 | ||
40 | #define PHY_PORT_CS1 0x37 | |
41 | #define PHY_PORT_CS1_LINK_DISABLE BIT(14) | |
42 | #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26) | |
43 | #define PHY_PORT_CS1_LINK_STATE_SHIFT 26 | |
44 | ||
b2b6c2eb MW |
45 | #define ICM_TIMEOUT 5000 /* ms */ |
46 | #define ICM_APPROVE_TIMEOUT 10000 /* ms */ | |
f67cf491 MW |
47 | #define ICM_MAX_LINK 4 |
48 | #define ICM_MAX_DEPTH 6 | |
49 | ||
50 | /** | |
51 | * struct icm - Internal connection manager private data | |
52 | * @request_lock: Makes sure only one message is send to ICM at time | |
53 | * @rescan_work: Work used to rescan the surviving switches after resume | |
54 | * @upstream_port: Pointer to the PCIe upstream port this host | |
55 | * controller is connected. This is only set for systems | |
56 | * where ICM needs to be started manually | |
57 | * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides | |
58 | * (only set when @upstream_port is not %NULL) | |
e6b245cc | 59 | * @safe_mode: ICM is in safe mode |
afa11ff5 | 60 | * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported) |
2de98e05 | 61 | * @rpm: Does the controller support runtime PM (RTD3) |
f67cf491 MW |
62 | * @is_supported: Checks if we can support ICM on this controller |
63 | * @get_mode: Read and return the ICM firmware mode (optional) | |
64 | * @get_route: Find a route string for given switch | |
fa3f20f6 | 65 | * @save_devices: Ask ICM to save devices to ACL when suspending (optional) |
c1c79d65 | 66 | * @driver_ready: Send driver ready message to ICM |
f67cf491 MW |
67 | * @device_connected: Handle device connected ICM message |
68 | * @device_disconnected: Handle device disconnected ICM message | |
d1ff7024 MW |
69 | * @xdomain_connected - Handle XDomain connected ICM message |
70 | * @xdomain_disconnected - Handle XDomain disconnected ICM message | |
f67cf491 MW |
71 | */ |
72 | struct icm { | |
73 | struct mutex request_lock; | |
74 | struct delayed_work rescan_work; | |
75 | struct pci_dev *upstream_port; | |
afa11ff5 | 76 | size_t max_boot_acl; |
f67cf491 | 77 | int vnd_cap; |
e6b245cc | 78 | bool safe_mode; |
2de98e05 | 79 | bool rpm; |
f67cf491 MW |
80 | bool (*is_supported)(struct tb *tb); |
81 | int (*get_mode)(struct tb *tb); | |
82 | int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route); | |
fa3f20f6 | 83 | void (*save_devices)(struct tb *tb); |
c1c79d65 | 84 | int (*driver_ready)(struct tb *tb, |
afa11ff5 | 85 | enum tb_security_level *security_level, |
2de98e05 | 86 | size_t *nboot_acl, bool *rpm); |
f67cf491 MW |
87 | void (*device_connected)(struct tb *tb, |
88 | const struct icm_pkg_header *hdr); | |
89 | void (*device_disconnected)(struct tb *tb, | |
90 | const struct icm_pkg_header *hdr); | |
d1ff7024 MW |
91 | void (*xdomain_connected)(struct tb *tb, |
92 | const struct icm_pkg_header *hdr); | |
93 | void (*xdomain_disconnected)(struct tb *tb, | |
94 | const struct icm_pkg_header *hdr); | |
f67cf491 MW |
95 | }; |
96 | ||
97 | struct icm_notification { | |
98 | struct work_struct work; | |
99 | struct icm_pkg_header *pkg; | |
100 | struct tb *tb; | |
101 | }; | |
102 | ||
2de98e05 MW |
103 | struct ep_name_entry { |
104 | u8 len; | |
105 | u8 type; | |
106 | u8 data[0]; | |
107 | }; | |
108 | ||
109 | #define EP_NAME_INTEL_VSS 0x10 | |
110 | ||
111 | /* Intel Vendor specific structure */ | |
112 | struct intel_vss { | |
113 | u16 vendor; | |
114 | u16 model; | |
115 | u8 mc; | |
116 | u8 flags; | |
117 | u16 pci_devid; | |
118 | u32 nvm_version; | |
119 | }; | |
120 | ||
121 | #define INTEL_VSS_FLAGS_RTD3 BIT(0) | |
122 | ||
123 | static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size) | |
124 | { | |
125 | const void *end = ep_name + size; | |
126 | ||
127 | while (ep_name < end) { | |
128 | const struct ep_name_entry *ep = ep_name; | |
129 | ||
130 | if (!ep->len) | |
131 | break; | |
132 | if (ep_name + ep->len > end) | |
133 | break; | |
134 | ||
135 | if (ep->type == EP_NAME_INTEL_VSS) | |
136 | return (const struct intel_vss *)ep->data; | |
137 | ||
138 | ep_name += ep->len; | |
139 | } | |
140 | ||
141 | return NULL; | |
142 | } | |
143 | ||
f67cf491 MW |
144 | static inline struct tb *icm_to_tb(struct icm *icm) |
145 | { | |
146 | return ((void *)icm - sizeof(struct tb)); | |
147 | } | |
148 | ||
149 | static inline u8 phy_port_from_route(u64 route, u8 depth) | |
150 | { | |
d1ff7024 MW |
151 | u8 link; |
152 | ||
153 | link = depth ? route >> ((depth - 1) * 8) : route; | |
154 | return tb_phy_port_from_link(link); | |
f67cf491 MW |
155 | } |
156 | ||
157 | static inline u8 dual_link_from_link(u8 link) | |
158 | { | |
159 | return link ? ((link - 1) ^ 0x01) + 1 : 0; | |
160 | } | |
161 | ||
162 | static inline u64 get_route(u32 route_hi, u32 route_lo) | |
163 | { | |
164 | return (u64)route_hi << 32 | route_lo; | |
165 | } | |
166 | ||
d1d8b263 RM |
167 | static inline u64 get_parent_route(u64 route) |
168 | { | |
169 | int depth = tb_route_length(route); | |
170 | return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0; | |
171 | } | |
172 | ||
f67cf491 MW |
173 | static bool icm_match(const struct tb_cfg_request *req, |
174 | const struct ctl_pkg *pkg) | |
175 | { | |
176 | const struct icm_pkg_header *res_hdr = pkg->buffer; | |
177 | const struct icm_pkg_header *req_hdr = req->request; | |
178 | ||
179 | if (pkg->frame.eof != req->response_type) | |
180 | return false; | |
181 | if (res_hdr->code != req_hdr->code) | |
182 | return false; | |
183 | ||
184 | return true; | |
185 | } | |
186 | ||
187 | static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg) | |
188 | { | |
189 | const struct icm_pkg_header *hdr = pkg->buffer; | |
190 | ||
191 | if (hdr->packet_id < req->npackets) { | |
192 | size_t offset = hdr->packet_id * req->response_size; | |
193 | ||
194 | memcpy(req->response + offset, pkg->buffer, req->response_size); | |
195 | } | |
196 | ||
197 | return hdr->packet_id == hdr->total_packets - 1; | |
198 | } | |
199 | ||
200 | static int icm_request(struct tb *tb, const void *request, size_t request_size, | |
201 | void *response, size_t response_size, size_t npackets, | |
202 | unsigned int timeout_msec) | |
203 | { | |
204 | struct icm *icm = tb_priv(tb); | |
205 | int retries = 3; | |
206 | ||
207 | do { | |
208 | struct tb_cfg_request *req; | |
209 | struct tb_cfg_result res; | |
210 | ||
211 | req = tb_cfg_request_alloc(); | |
212 | if (!req) | |
213 | return -ENOMEM; | |
214 | ||
215 | req->match = icm_match; | |
216 | req->copy = icm_copy; | |
217 | req->request = request; | |
218 | req->request_size = request_size; | |
219 | req->request_type = TB_CFG_PKG_ICM_CMD; | |
220 | req->response = response; | |
221 | req->npackets = npackets; | |
222 | req->response_size = response_size; | |
223 | req->response_type = TB_CFG_PKG_ICM_RESP; | |
224 | ||
225 | mutex_lock(&icm->request_lock); | |
226 | res = tb_cfg_request_sync(tb->ctl, req, timeout_msec); | |
227 | mutex_unlock(&icm->request_lock); | |
228 | ||
229 | tb_cfg_request_put(req); | |
230 | ||
231 | if (res.err != -ETIMEDOUT) | |
232 | return res.err == 1 ? -EIO : res.err; | |
233 | ||
234 | usleep_range(20, 50); | |
235 | } while (retries--); | |
236 | ||
237 | return -ETIMEDOUT; | |
238 | } | |
239 | ||
240 | static bool icm_fr_is_supported(struct tb *tb) | |
241 | { | |
630b3aff | 242 | return !x86_apple_machine; |
f67cf491 MW |
243 | } |
244 | ||
245 | static inline int icm_fr_get_switch_index(u32 port) | |
246 | { | |
247 | int index; | |
248 | ||
249 | if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT) | |
250 | return 0; | |
251 | ||
252 | index = port >> ICM_PORT_INDEX_SHIFT; | |
253 | return index != 0xff ? index : 0; | |
254 | } | |
255 | ||
256 | static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route) | |
257 | { | |
258 | struct icm_fr_pkg_get_topology_response *switches, *sw; | |
259 | struct icm_fr_pkg_get_topology request = { | |
260 | .hdr = { .code = ICM_GET_TOPOLOGY }, | |
261 | }; | |
262 | size_t npackets = ICM_GET_TOPOLOGY_PACKETS; | |
263 | int ret, index; | |
264 | u8 i; | |
265 | ||
266 | switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL); | |
267 | if (!switches) | |
268 | return -ENOMEM; | |
269 | ||
270 | ret = icm_request(tb, &request, sizeof(request), switches, | |
271 | sizeof(*switches), npackets, ICM_TIMEOUT); | |
272 | if (ret) | |
273 | goto err_free; | |
274 | ||
275 | sw = &switches[0]; | |
276 | index = icm_fr_get_switch_index(sw->ports[link]); | |
277 | if (!index) { | |
278 | ret = -ENODEV; | |
279 | goto err_free; | |
280 | } | |
281 | ||
282 | sw = &switches[index]; | |
283 | for (i = 1; i < depth; i++) { | |
284 | unsigned int j; | |
285 | ||
286 | if (!(sw->first_data & ICM_SWITCH_USED)) { | |
287 | ret = -ENODEV; | |
288 | goto err_free; | |
289 | } | |
290 | ||
291 | for (j = 0; j < ARRAY_SIZE(sw->ports); j++) { | |
292 | index = icm_fr_get_switch_index(sw->ports[j]); | |
293 | if (index > sw->switch_index) { | |
294 | sw = &switches[index]; | |
295 | break; | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | *route = get_route(sw->route_hi, sw->route_lo); | |
301 | ||
302 | err_free: | |
303 | kfree(switches); | |
304 | return ret; | |
305 | } | |
306 | ||
fa3f20f6 MW |
307 | static void icm_fr_save_devices(struct tb *tb) |
308 | { | |
309 | nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0); | |
310 | } | |
311 | ||
c1c79d65 | 312 | static int |
afa11ff5 | 313 | icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level, |
2de98e05 | 314 | size_t *nboot_acl, bool *rpm) |
c1c79d65 MW |
315 | { |
316 | struct icm_fr_pkg_driver_ready_response reply; | |
317 | struct icm_pkg_driver_ready request = { | |
318 | .hdr.code = ICM_DRIVER_READY, | |
319 | }; | |
320 | int ret; | |
321 | ||
322 | memset(&reply, 0, sizeof(reply)); | |
323 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
324 | 1, ICM_TIMEOUT); | |
325 | if (ret) | |
326 | return ret; | |
327 | ||
328 | if (security_level) | |
329 | *security_level = reply.security_level & ICM_FR_SLEVEL_MASK; | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
f67cf491 MW |
334 | static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw) |
335 | { | |
336 | struct icm_fr_pkg_approve_device request; | |
337 | struct icm_fr_pkg_approve_device reply; | |
338 | int ret; | |
339 | ||
340 | memset(&request, 0, sizeof(request)); | |
341 | memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); | |
342 | request.hdr.code = ICM_APPROVE_DEVICE; | |
343 | request.connection_id = sw->connection_id; | |
344 | request.connection_key = sw->connection_key; | |
345 | ||
346 | memset(&reply, 0, sizeof(reply)); | |
347 | /* Use larger timeout as establishing tunnels can take some time */ | |
348 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
b2b6c2eb | 349 | 1, ICM_APPROVE_TIMEOUT); |
f67cf491 MW |
350 | if (ret) |
351 | return ret; | |
352 | ||
353 | if (reply.hdr.flags & ICM_FLAGS_ERROR) { | |
354 | tb_warn(tb, "PCIe tunnel creation failed\n"); | |
355 | return -EIO; | |
356 | } | |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
361 | static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw) | |
362 | { | |
363 | struct icm_fr_pkg_add_device_key request; | |
364 | struct icm_fr_pkg_add_device_key_response reply; | |
365 | int ret; | |
366 | ||
367 | memset(&request, 0, sizeof(request)); | |
368 | memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); | |
369 | request.hdr.code = ICM_ADD_DEVICE_KEY; | |
370 | request.connection_id = sw->connection_id; | |
371 | request.connection_key = sw->connection_key; | |
372 | memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE); | |
373 | ||
374 | memset(&reply, 0, sizeof(reply)); | |
375 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
376 | 1, ICM_TIMEOUT); | |
377 | if (ret) | |
378 | return ret; | |
379 | ||
380 | if (reply.hdr.flags & ICM_FLAGS_ERROR) { | |
381 | tb_warn(tb, "Adding key to switch failed\n"); | |
382 | return -EIO; | |
383 | } | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
388 | static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw, | |
389 | const u8 *challenge, u8 *response) | |
390 | { | |
391 | struct icm_fr_pkg_challenge_device request; | |
392 | struct icm_fr_pkg_challenge_device_response reply; | |
393 | int ret; | |
394 | ||
395 | memset(&request, 0, sizeof(request)); | |
396 | memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); | |
397 | request.hdr.code = ICM_CHALLENGE_DEVICE; | |
398 | request.connection_id = sw->connection_id; | |
399 | request.connection_key = sw->connection_key; | |
400 | memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE); | |
401 | ||
402 | memset(&reply, 0, sizeof(reply)); | |
403 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
404 | 1, ICM_TIMEOUT); | |
405 | if (ret) | |
406 | return ret; | |
407 | ||
408 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
409 | return -EKEYREJECTED; | |
410 | if (reply.hdr.flags & ICM_FLAGS_NO_KEY) | |
411 | return -ENOKEY; | |
412 | ||
413 | memcpy(response, reply.response, TB_SWITCH_KEY_SIZE); | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
d1ff7024 MW |
418 | static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) |
419 | { | |
420 | struct icm_fr_pkg_approve_xdomain_response reply; | |
421 | struct icm_fr_pkg_approve_xdomain request; | |
422 | int ret; | |
423 | ||
424 | memset(&request, 0, sizeof(request)); | |
425 | request.hdr.code = ICM_APPROVE_XDOMAIN; | |
426 | request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link; | |
427 | memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); | |
428 | ||
429 | request.transmit_path = xd->transmit_path; | |
430 | request.transmit_ring = xd->transmit_ring; | |
431 | request.receive_path = xd->receive_path; | |
432 | request.receive_ring = xd->receive_ring; | |
433 | ||
434 | memset(&reply, 0, sizeof(reply)); | |
435 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
436 | 1, ICM_TIMEOUT); | |
437 | if (ret) | |
438 | return ret; | |
439 | ||
440 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
441 | return -EIO; | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) | |
447 | { | |
448 | u8 phy_port; | |
449 | u8 cmd; | |
450 | ||
451 | phy_port = tb_phy_port_from_link(xd->link); | |
452 | if (phy_port == 0) | |
453 | cmd = NHI_MAILBOX_DISCONNECT_PA; | |
454 | else | |
455 | cmd = NHI_MAILBOX_DISCONNECT_PB; | |
456 | ||
457 | nhi_mailbox_cmd(tb->nhi, cmd, 1); | |
458 | usleep_range(10, 50); | |
459 | nhi_mailbox_cmd(tb->nhi, cmd, 2); | |
460 | return 0; | |
461 | } | |
462 | ||
210f2eb5 | 463 | static void add_switch(struct tb_switch *parent_sw, u64 route, |
2de98e05 MW |
464 | const uuid_t *uuid, const u8 *ep_name, |
465 | size_t ep_name_size, u8 connection_id, u8 connection_key, | |
210f2eb5 | 466 | u8 link, u8 depth, enum tb_security_level security_level, |
269f6def | 467 | bool authorized, bool boot) |
210f2eb5 | 468 | { |
2de98e05 | 469 | const struct intel_vss *vss; |
210f2eb5 MW |
470 | struct tb_switch *sw; |
471 | ||
2de98e05 MW |
472 | pm_runtime_get_sync(&parent_sw->dev); |
473 | ||
210f2eb5 MW |
474 | sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route); |
475 | if (!sw) | |
2de98e05 | 476 | goto out; |
210f2eb5 MW |
477 | |
478 | sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL); | |
479 | sw->connection_id = connection_id; | |
480 | sw->connection_key = connection_key; | |
481 | sw->link = link; | |
482 | sw->depth = depth; | |
483 | sw->authorized = authorized; | |
484 | sw->security_level = security_level; | |
269f6def | 485 | sw->boot = boot; |
210f2eb5 | 486 | |
2de98e05 MW |
487 | vss = parse_intel_vss(ep_name, ep_name_size); |
488 | if (vss) | |
489 | sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3); | |
490 | ||
210f2eb5 MW |
491 | /* Link the two switches now */ |
492 | tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); | |
493 | tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw); | |
494 | ||
495 | if (tb_switch_add(sw)) { | |
496 | tb_port_at(tb_route(sw), parent_sw)->remote = NULL; | |
497 | tb_switch_put(sw); | |
210f2eb5 | 498 | } |
2de98e05 MW |
499 | |
500 | out: | |
501 | pm_runtime_mark_last_busy(&parent_sw->dev); | |
502 | pm_runtime_put_autosuspend(&parent_sw->dev); | |
210f2eb5 MW |
503 | } |
504 | ||
505 | static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw, | |
506 | u64 route, u8 connection_id, u8 connection_key, | |
269f6def | 507 | u8 link, u8 depth, bool boot) |
210f2eb5 MW |
508 | { |
509 | /* Disconnect from parent */ | |
510 | tb_port_at(tb_route(sw), parent_sw)->remote = NULL; | |
511 | /* Re-connect via updated port*/ | |
512 | tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); | |
513 | ||
514 | /* Update with the new addressing information */ | |
515 | sw->config.route_hi = upper_32_bits(route); | |
516 | sw->config.route_lo = lower_32_bits(route); | |
517 | sw->connection_id = connection_id; | |
518 | sw->connection_key = connection_key; | |
519 | sw->link = link; | |
520 | sw->depth = depth; | |
269f6def | 521 | sw->boot = boot; |
210f2eb5 MW |
522 | |
523 | /* This switch still exists */ | |
524 | sw->is_unplugged = false; | |
525 | } | |
526 | ||
f67cf491 MW |
527 | static void remove_switch(struct tb_switch *sw) |
528 | { | |
529 | struct tb_switch *parent_sw; | |
530 | ||
531 | parent_sw = tb_to_switch(sw->dev.parent); | |
532 | tb_port_at(tb_route(sw), parent_sw)->remote = NULL; | |
533 | tb_switch_remove(sw); | |
534 | } | |
535 | ||
210f2eb5 MW |
536 | static void add_xdomain(struct tb_switch *sw, u64 route, |
537 | const uuid_t *local_uuid, const uuid_t *remote_uuid, | |
538 | u8 link, u8 depth) | |
539 | { | |
540 | struct tb_xdomain *xd; | |
541 | ||
2de98e05 MW |
542 | pm_runtime_get_sync(&sw->dev); |
543 | ||
210f2eb5 MW |
544 | xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid); |
545 | if (!xd) | |
2de98e05 | 546 | goto out; |
210f2eb5 MW |
547 | |
548 | xd->link = link; | |
549 | xd->depth = depth; | |
550 | ||
551 | tb_port_at(route, sw)->xdomain = xd; | |
552 | ||
553 | tb_xdomain_add(xd); | |
2de98e05 MW |
554 | |
555 | out: | |
556 | pm_runtime_mark_last_busy(&sw->dev); | |
557 | pm_runtime_put_autosuspend(&sw->dev); | |
210f2eb5 MW |
558 | } |
559 | ||
560 | static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link) | |
561 | { | |
562 | xd->link = link; | |
563 | xd->route = route; | |
564 | xd->is_unplugged = false; | |
565 | } | |
566 | ||
9f3125ba MW |
567 | static void remove_xdomain(struct tb_xdomain *xd) |
568 | { | |
569 | struct tb_switch *sw; | |
570 | ||
571 | sw = tb_to_switch(xd->dev.parent); | |
572 | tb_port_at(xd->route, sw)->xdomain = NULL; | |
573 | tb_xdomain_remove(xd); | |
574 | } | |
575 | ||
f67cf491 MW |
576 | static void |
577 | icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) | |
578 | { | |
579 | const struct icm_fr_event_device_connected *pkg = | |
580 | (const struct icm_fr_event_device_connected *)hdr; | |
210f2eb5 | 581 | enum tb_security_level security_level; |
f67cf491 MW |
582 | struct tb_switch *sw, *parent_sw; |
583 | struct icm *icm = tb_priv(tb); | |
584 | bool authorized = false; | |
9f3125ba | 585 | struct tb_xdomain *xd; |
f67cf491 | 586 | u8 link, depth; |
269f6def | 587 | bool boot; |
f67cf491 MW |
588 | u64 route; |
589 | int ret; | |
590 | ||
591 | link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; | |
592 | depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> | |
593 | ICM_LINK_INFO_DEPTH_SHIFT; | |
594 | authorized = pkg->link_info & ICM_LINK_INFO_APPROVED; | |
210f2eb5 MW |
595 | security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> |
596 | ICM_FLAGS_SLEVEL_SHIFT; | |
269f6def | 597 | boot = pkg->link_info & ICM_LINK_INFO_BOOT; |
f67cf491 | 598 | |
38604142 MW |
599 | if (pkg->link_info & ICM_LINK_INFO_REJECTED) { |
600 | tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n", | |
601 | link, depth); | |
602 | return; | |
603 | } | |
604 | ||
f67cf491 MW |
605 | sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid); |
606 | if (sw) { | |
607 | u8 phy_port, sw_phy_port; | |
608 | ||
609 | parent_sw = tb_to_switch(sw->dev.parent); | |
813f35fc MW |
610 | sw_phy_port = tb_phy_port_from_link(sw->link); |
611 | phy_port = tb_phy_port_from_link(link); | |
f67cf491 MW |
612 | |
613 | /* | |
614 | * On resume ICM will send us connected events for the | |
615 | * devices that still are present. However, that | |
616 | * information might have changed for example by the | |
617 | * fact that a switch on a dual-link connection might | |
618 | * have been enumerated using the other link now. Make | |
619 | * sure our book keeping matches that. | |
620 | */ | |
621 | if (sw->depth == depth && sw_phy_port == phy_port && | |
622 | !!sw->authorized == authorized) { | |
813f35fc MW |
623 | /* |
624 | * It was enumerated through another link so update | |
625 | * route string accordingly. | |
626 | */ | |
627 | if (sw->link != link) { | |
628 | ret = icm->get_route(tb, link, depth, &route); | |
629 | if (ret) { | |
630 | tb_err(tb, "failed to update route string for switch at %u.%u\n", | |
631 | link, depth); | |
632 | tb_switch_put(sw); | |
633 | return; | |
634 | } | |
635 | } else { | |
636 | route = tb_route(sw); | |
637 | } | |
638 | ||
210f2eb5 | 639 | update_switch(parent_sw, sw, route, pkg->connection_id, |
269f6def | 640 | pkg->connection_key, link, depth, boot); |
f67cf491 MW |
641 | tb_switch_put(sw); |
642 | return; | |
643 | } | |
644 | ||
645 | /* | |
646 | * User connected the same switch to another physical | |
647 | * port or to another part of the topology. Remove the | |
648 | * existing switch now before adding the new one. | |
649 | */ | |
650 | remove_switch(sw); | |
651 | tb_switch_put(sw); | |
652 | } | |
653 | ||
654 | /* | |
655 | * If the switch was not found by UUID, look for a switch on | |
656 | * same physical port (taking possible link aggregation into | |
657 | * account) and depth. If we found one it is definitely a stale | |
658 | * one so remove it first. | |
659 | */ | |
660 | sw = tb_switch_find_by_link_depth(tb, link, depth); | |
661 | if (!sw) { | |
662 | u8 dual_link; | |
663 | ||
664 | dual_link = dual_link_from_link(link); | |
665 | if (dual_link) | |
666 | sw = tb_switch_find_by_link_depth(tb, dual_link, depth); | |
667 | } | |
668 | if (sw) { | |
669 | remove_switch(sw); | |
670 | tb_switch_put(sw); | |
671 | } | |
672 | ||
9f3125ba MW |
673 | /* Remove existing XDomain connection if found */ |
674 | xd = tb_xdomain_find_by_link_depth(tb, link, depth); | |
675 | if (xd) { | |
676 | remove_xdomain(xd); | |
677 | tb_xdomain_put(xd); | |
678 | } | |
679 | ||
f67cf491 MW |
680 | parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1); |
681 | if (!parent_sw) { | |
682 | tb_err(tb, "failed to find parent switch for %u.%u\n", | |
683 | link, depth); | |
684 | return; | |
685 | } | |
686 | ||
813f35fc MW |
687 | ret = icm->get_route(tb, link, depth, &route); |
688 | if (ret) { | |
689 | tb_err(tb, "failed to find route string for switch at %u.%u\n", | |
690 | link, depth); | |
691 | tb_switch_put(parent_sw); | |
692 | return; | |
693 | } | |
694 | ||
2de98e05 MW |
695 | add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name, |
696 | sizeof(pkg->ep_name), pkg->connection_id, | |
210f2eb5 | 697 | pkg->connection_key, link, depth, security_level, |
269f6def | 698 | authorized, boot); |
f67cf491 | 699 | |
f67cf491 MW |
700 | tb_switch_put(parent_sw); |
701 | } | |
702 | ||
703 | static void | |
704 | icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) | |
705 | { | |
706 | const struct icm_fr_event_device_disconnected *pkg = | |
707 | (const struct icm_fr_event_device_disconnected *)hdr; | |
708 | struct tb_switch *sw; | |
709 | u8 link, depth; | |
710 | ||
711 | link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; | |
712 | depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> | |
713 | ICM_LINK_INFO_DEPTH_SHIFT; | |
714 | ||
715 | if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) { | |
716 | tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth); | |
717 | return; | |
718 | } | |
719 | ||
720 | sw = tb_switch_find_by_link_depth(tb, link, depth); | |
721 | if (!sw) { | |
722 | tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link, | |
723 | depth); | |
724 | return; | |
725 | } | |
726 | ||
727 | remove_switch(sw); | |
728 | tb_switch_put(sw); | |
729 | } | |
730 | ||
d1ff7024 MW |
731 | static void |
732 | icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr) | |
733 | { | |
734 | const struct icm_fr_event_xdomain_connected *pkg = | |
735 | (const struct icm_fr_event_xdomain_connected *)hdr; | |
736 | struct tb_xdomain *xd; | |
737 | struct tb_switch *sw; | |
738 | u8 link, depth; | |
739 | bool approved; | |
740 | u64 route; | |
741 | ||
d1ff7024 MW |
742 | link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; |
743 | depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> | |
744 | ICM_LINK_INFO_DEPTH_SHIFT; | |
745 | approved = pkg->link_info & ICM_LINK_INFO_APPROVED; | |
746 | ||
747 | if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) { | |
748 | tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth); | |
749 | return; | |
750 | } | |
751 | ||
752 | route = get_route(pkg->local_route_hi, pkg->local_route_lo); | |
753 | ||
754 | xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); | |
755 | if (xd) { | |
756 | u8 xd_phy_port, phy_port; | |
757 | ||
758 | xd_phy_port = phy_port_from_route(xd->route, xd->depth); | |
759 | phy_port = phy_port_from_route(route, depth); | |
760 | ||
761 | if (xd->depth == depth && xd_phy_port == phy_port) { | |
210f2eb5 | 762 | update_xdomain(xd, route, link); |
d1ff7024 MW |
763 | tb_xdomain_put(xd); |
764 | return; | |
765 | } | |
766 | ||
767 | /* | |
768 | * If we find an existing XDomain connection remove it | |
769 | * now. We need to go through login handshake and | |
770 | * everything anyway to be able to re-establish the | |
771 | * connection. | |
772 | */ | |
773 | remove_xdomain(xd); | |
774 | tb_xdomain_put(xd); | |
775 | } | |
776 | ||
777 | /* | |
778 | * Look if there already exists an XDomain in the same place | |
779 | * than the new one and in that case remove it because it is | |
780 | * most likely another host that got disconnected. | |
781 | */ | |
782 | xd = tb_xdomain_find_by_link_depth(tb, link, depth); | |
783 | if (!xd) { | |
784 | u8 dual_link; | |
785 | ||
786 | dual_link = dual_link_from_link(link); | |
787 | if (dual_link) | |
788 | xd = tb_xdomain_find_by_link_depth(tb, dual_link, | |
789 | depth); | |
790 | } | |
791 | if (xd) { | |
792 | remove_xdomain(xd); | |
793 | tb_xdomain_put(xd); | |
794 | } | |
795 | ||
796 | /* | |
797 | * If the user disconnected a switch during suspend and | |
798 | * connected another host to the same port, remove the switch | |
799 | * first. | |
800 | */ | |
801 | sw = get_switch_at_route(tb->root_switch, route); | |
802 | if (sw) | |
803 | remove_switch(sw); | |
804 | ||
805 | sw = tb_switch_find_by_link_depth(tb, link, depth); | |
806 | if (!sw) { | |
807 | tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link, | |
808 | depth); | |
809 | return; | |
810 | } | |
811 | ||
210f2eb5 MW |
812 | add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link, |
813 | depth); | |
d1ff7024 MW |
814 | tb_switch_put(sw); |
815 | } | |
816 | ||
817 | static void | |
818 | icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) | |
819 | { | |
820 | const struct icm_fr_event_xdomain_disconnected *pkg = | |
821 | (const struct icm_fr_event_xdomain_disconnected *)hdr; | |
822 | struct tb_xdomain *xd; | |
823 | ||
824 | /* | |
825 | * If the connection is through one or multiple devices, the | |
826 | * XDomain device is removed along with them so it is fine if we | |
827 | * cannot find it here. | |
828 | */ | |
829 | xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); | |
830 | if (xd) { | |
831 | remove_xdomain(xd); | |
832 | tb_xdomain_put(xd); | |
833 | } | |
834 | } | |
835 | ||
d1d8b263 RM |
836 | static int |
837 | icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level, | |
2de98e05 | 838 | size_t *nboot_acl, bool *rpm) |
d1d8b263 RM |
839 | { |
840 | struct icm_tr_pkg_driver_ready_response reply; | |
841 | struct icm_pkg_driver_ready request = { | |
842 | .hdr.code = ICM_DRIVER_READY, | |
843 | }; | |
844 | int ret; | |
845 | ||
846 | memset(&reply, 0, sizeof(reply)); | |
847 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
848 | 1, 20000); | |
849 | if (ret) | |
850 | return ret; | |
851 | ||
852 | if (security_level) | |
853 | *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK; | |
854 | if (nboot_acl) | |
855 | *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >> | |
856 | ICM_TR_INFO_BOOT_ACL_SHIFT; | |
2de98e05 MW |
857 | if (rpm) |
858 | *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3); | |
859 | ||
d1d8b263 RM |
860 | return 0; |
861 | } | |
862 | ||
863 | static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw) | |
864 | { | |
865 | struct icm_tr_pkg_approve_device request; | |
866 | struct icm_tr_pkg_approve_device reply; | |
867 | int ret; | |
868 | ||
869 | memset(&request, 0, sizeof(request)); | |
870 | memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); | |
871 | request.hdr.code = ICM_APPROVE_DEVICE; | |
872 | request.route_lo = sw->config.route_lo; | |
873 | request.route_hi = sw->config.route_hi; | |
874 | request.connection_id = sw->connection_id; | |
875 | ||
876 | memset(&reply, 0, sizeof(reply)); | |
877 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
878 | 1, ICM_APPROVE_TIMEOUT); | |
879 | if (ret) | |
880 | return ret; | |
881 | ||
882 | if (reply.hdr.flags & ICM_FLAGS_ERROR) { | |
883 | tb_warn(tb, "PCIe tunnel creation failed\n"); | |
884 | return -EIO; | |
885 | } | |
886 | ||
887 | return 0; | |
888 | } | |
889 | ||
890 | static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw) | |
891 | { | |
892 | struct icm_tr_pkg_add_device_key_response reply; | |
893 | struct icm_tr_pkg_add_device_key request; | |
894 | int ret; | |
895 | ||
896 | memset(&request, 0, sizeof(request)); | |
897 | memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); | |
898 | request.hdr.code = ICM_ADD_DEVICE_KEY; | |
899 | request.route_lo = sw->config.route_lo; | |
900 | request.route_hi = sw->config.route_hi; | |
901 | request.connection_id = sw->connection_id; | |
902 | memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE); | |
903 | ||
904 | memset(&reply, 0, sizeof(reply)); | |
905 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
906 | 1, ICM_TIMEOUT); | |
907 | if (ret) | |
908 | return ret; | |
909 | ||
910 | if (reply.hdr.flags & ICM_FLAGS_ERROR) { | |
911 | tb_warn(tb, "Adding key to switch failed\n"); | |
912 | return -EIO; | |
913 | } | |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
918 | static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw, | |
919 | const u8 *challenge, u8 *response) | |
920 | { | |
921 | struct icm_tr_pkg_challenge_device_response reply; | |
922 | struct icm_tr_pkg_challenge_device request; | |
923 | int ret; | |
924 | ||
925 | memset(&request, 0, sizeof(request)); | |
926 | memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); | |
927 | request.hdr.code = ICM_CHALLENGE_DEVICE; | |
928 | request.route_lo = sw->config.route_lo; | |
929 | request.route_hi = sw->config.route_hi; | |
930 | request.connection_id = sw->connection_id; | |
931 | memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE); | |
932 | ||
933 | memset(&reply, 0, sizeof(reply)); | |
934 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
935 | 1, ICM_TIMEOUT); | |
936 | if (ret) | |
937 | return ret; | |
938 | ||
939 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
940 | return -EKEYREJECTED; | |
941 | if (reply.hdr.flags & ICM_FLAGS_NO_KEY) | |
942 | return -ENOKEY; | |
943 | ||
944 | memcpy(response, reply.response, TB_SWITCH_KEY_SIZE); | |
945 | ||
946 | return 0; | |
947 | } | |
948 | ||
949 | static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) | |
950 | { | |
951 | struct icm_tr_pkg_approve_xdomain_response reply; | |
952 | struct icm_tr_pkg_approve_xdomain request; | |
953 | int ret; | |
954 | ||
955 | memset(&request, 0, sizeof(request)); | |
956 | request.hdr.code = ICM_APPROVE_XDOMAIN; | |
957 | request.route_hi = upper_32_bits(xd->route); | |
958 | request.route_lo = lower_32_bits(xd->route); | |
959 | request.transmit_path = xd->transmit_path; | |
960 | request.transmit_ring = xd->transmit_ring; | |
961 | request.receive_path = xd->receive_path; | |
962 | request.receive_ring = xd->receive_ring; | |
963 | memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); | |
964 | ||
965 | memset(&reply, 0, sizeof(reply)); | |
966 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
967 | 1, ICM_TIMEOUT); | |
968 | if (ret) | |
969 | return ret; | |
970 | ||
971 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
972 | return -EIO; | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
977 | static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd, | |
978 | int stage) | |
979 | { | |
980 | struct icm_tr_pkg_disconnect_xdomain_response reply; | |
981 | struct icm_tr_pkg_disconnect_xdomain request; | |
982 | int ret; | |
983 | ||
984 | memset(&request, 0, sizeof(request)); | |
985 | request.hdr.code = ICM_DISCONNECT_XDOMAIN; | |
986 | request.stage = stage; | |
987 | request.route_hi = upper_32_bits(xd->route); | |
988 | request.route_lo = lower_32_bits(xd->route); | |
989 | memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); | |
990 | ||
991 | memset(&reply, 0, sizeof(reply)); | |
992 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
993 | 1, ICM_TIMEOUT); | |
994 | if (ret) | |
995 | return ret; | |
996 | ||
997 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
998 | return -EIO; | |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) | |
1004 | { | |
1005 | int ret; | |
1006 | ||
1007 | ret = icm_tr_xdomain_tear_down(tb, xd, 1); | |
1008 | if (ret) | |
1009 | return ret; | |
1010 | ||
1011 | usleep_range(10, 50); | |
1012 | return icm_tr_xdomain_tear_down(tb, xd, 2); | |
1013 | } | |
1014 | ||
1015 | static void | |
1016 | icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) | |
1017 | { | |
1018 | const struct icm_tr_event_device_connected *pkg = | |
1019 | (const struct icm_tr_event_device_connected *)hdr; | |
1020 | enum tb_security_level security_level; | |
1021 | struct tb_switch *sw, *parent_sw; | |
1022 | struct tb_xdomain *xd; | |
1023 | bool authorized, boot; | |
1024 | u64 route; | |
1025 | ||
1026 | /* | |
1027 | * Currently we don't use the QoS information coming with the | |
1028 | * device connected message so simply just ignore that extra | |
1029 | * packet for now. | |
1030 | */ | |
1031 | if (pkg->hdr.packet_id) | |
1032 | return; | |
1033 | ||
d1d8b263 RM |
1034 | route = get_route(pkg->route_hi, pkg->route_lo); |
1035 | authorized = pkg->link_info & ICM_LINK_INFO_APPROVED; | |
1036 | security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> | |
1037 | ICM_FLAGS_SLEVEL_SHIFT; | |
1038 | boot = pkg->link_info & ICM_LINK_INFO_BOOT; | |
1039 | ||
1040 | if (pkg->link_info & ICM_LINK_INFO_REJECTED) { | |
1041 | tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n", | |
1042 | route); | |
1043 | return; | |
1044 | } | |
1045 | ||
1046 | sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid); | |
1047 | if (sw) { | |
1048 | /* Update the switch if it is still in the same place */ | |
1049 | if (tb_route(sw) == route && !!sw->authorized == authorized) { | |
1050 | parent_sw = tb_to_switch(sw->dev.parent); | |
1051 | update_switch(parent_sw, sw, route, pkg->connection_id, | |
1052 | 0, 0, 0, boot); | |
1053 | tb_switch_put(sw); | |
1054 | return; | |
1055 | } | |
1056 | ||
1057 | remove_switch(sw); | |
1058 | tb_switch_put(sw); | |
1059 | } | |
1060 | ||
1061 | /* Another switch with the same address */ | |
1062 | sw = tb_switch_find_by_route(tb, route); | |
1063 | if (sw) { | |
1064 | remove_switch(sw); | |
1065 | tb_switch_put(sw); | |
1066 | } | |
1067 | ||
1068 | /* XDomain connection with the same address */ | |
1069 | xd = tb_xdomain_find_by_route(tb, route); | |
1070 | if (xd) { | |
1071 | remove_xdomain(xd); | |
1072 | tb_xdomain_put(xd); | |
1073 | } | |
1074 | ||
1075 | parent_sw = tb_switch_find_by_route(tb, get_parent_route(route)); | |
1076 | if (!parent_sw) { | |
1077 | tb_err(tb, "failed to find parent switch for %llx\n", route); | |
1078 | return; | |
1079 | } | |
1080 | ||
2de98e05 MW |
1081 | add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name, |
1082 | sizeof(pkg->ep_name), pkg->connection_id, | |
d1d8b263 RM |
1083 | 0, 0, 0, security_level, authorized, boot); |
1084 | ||
1085 | tb_switch_put(parent_sw); | |
1086 | } | |
1087 | ||
1088 | static void | |
1089 | icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) | |
1090 | { | |
1091 | const struct icm_tr_event_device_disconnected *pkg = | |
1092 | (const struct icm_tr_event_device_disconnected *)hdr; | |
1093 | struct tb_switch *sw; | |
1094 | u64 route; | |
1095 | ||
1096 | route = get_route(pkg->route_hi, pkg->route_lo); | |
1097 | ||
1098 | sw = tb_switch_find_by_route(tb, route); | |
1099 | if (!sw) { | |
1100 | tb_warn(tb, "no switch exists at %llx, ignoring\n", route); | |
1101 | return; | |
1102 | } | |
1103 | ||
1104 | remove_switch(sw); | |
1105 | tb_switch_put(sw); | |
1106 | } | |
1107 | ||
1108 | static void | |
1109 | icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr) | |
1110 | { | |
1111 | const struct icm_tr_event_xdomain_connected *pkg = | |
1112 | (const struct icm_tr_event_xdomain_connected *)hdr; | |
1113 | struct tb_xdomain *xd; | |
1114 | struct tb_switch *sw; | |
1115 | u64 route; | |
1116 | ||
1117 | if (!tb->root_switch) | |
1118 | return; | |
1119 | ||
1120 | route = get_route(pkg->local_route_hi, pkg->local_route_lo); | |
1121 | ||
1122 | xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); | |
1123 | if (xd) { | |
1124 | if (xd->route == route) { | |
1125 | update_xdomain(xd, route, 0); | |
1126 | tb_xdomain_put(xd); | |
1127 | return; | |
1128 | } | |
1129 | ||
1130 | remove_xdomain(xd); | |
1131 | tb_xdomain_put(xd); | |
1132 | } | |
1133 | ||
1134 | /* An existing xdomain with the same address */ | |
1135 | xd = tb_xdomain_find_by_route(tb, route); | |
1136 | if (xd) { | |
1137 | remove_xdomain(xd); | |
1138 | tb_xdomain_put(xd); | |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * If the user disconnected a switch during suspend and | |
1143 | * connected another host to the same port, remove the switch | |
1144 | * first. | |
1145 | */ | |
1146 | sw = get_switch_at_route(tb->root_switch, route); | |
1147 | if (sw) | |
1148 | remove_switch(sw); | |
1149 | ||
1150 | sw = tb_switch_find_by_route(tb, get_parent_route(route)); | |
1151 | if (!sw) { | |
1152 | tb_warn(tb, "no switch exists at %llx, ignoring\n", route); | |
1153 | return; | |
1154 | } | |
1155 | ||
1156 | add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0); | |
1157 | tb_switch_put(sw); | |
1158 | } | |
1159 | ||
1160 | static void | |
1161 | icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) | |
1162 | { | |
1163 | const struct icm_tr_event_xdomain_disconnected *pkg = | |
1164 | (const struct icm_tr_event_xdomain_disconnected *)hdr; | |
1165 | struct tb_xdomain *xd; | |
1166 | u64 route; | |
1167 | ||
1168 | route = get_route(pkg->route_hi, pkg->route_lo); | |
1169 | ||
1170 | xd = tb_xdomain_find_by_route(tb, route); | |
1171 | if (xd) { | |
1172 | remove_xdomain(xd); | |
1173 | tb_xdomain_put(xd); | |
1174 | } | |
1175 | } | |
1176 | ||
f67cf491 MW |
1177 | static struct pci_dev *get_upstream_port(struct pci_dev *pdev) |
1178 | { | |
1179 | struct pci_dev *parent; | |
1180 | ||
1181 | parent = pci_upstream_bridge(pdev); | |
1182 | while (parent) { | |
1183 | if (!pci_is_pcie(parent)) | |
1184 | return NULL; | |
1185 | if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) | |
1186 | break; | |
1187 | parent = pci_upstream_bridge(parent); | |
1188 | } | |
1189 | ||
1190 | if (!parent) | |
1191 | return NULL; | |
1192 | ||
1193 | switch (parent->device) { | |
1194 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: | |
1195 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: | |
1196 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: | |
1197 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: | |
1198 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: | |
1199 | return parent; | |
1200 | } | |
1201 | ||
1202 | return NULL; | |
1203 | } | |
1204 | ||
1205 | static bool icm_ar_is_supported(struct tb *tb) | |
1206 | { | |
1207 | struct pci_dev *upstream_port; | |
1208 | struct icm *icm = tb_priv(tb); | |
1209 | ||
1210 | /* | |
1211 | * Starting from Alpine Ridge we can use ICM on Apple machines | |
1212 | * as well. We just need to reset and re-enable it first. | |
1213 | */ | |
630b3aff | 1214 | if (!x86_apple_machine) |
f67cf491 MW |
1215 | return true; |
1216 | ||
1217 | /* | |
1218 | * Find the upstream PCIe port in case we need to do reset | |
1219 | * through its vendor specific registers. | |
1220 | */ | |
1221 | upstream_port = get_upstream_port(tb->nhi->pdev); | |
1222 | if (upstream_port) { | |
1223 | int cap; | |
1224 | ||
1225 | cap = pci_find_ext_capability(upstream_port, | |
1226 | PCI_EXT_CAP_ID_VNDR); | |
1227 | if (cap > 0) { | |
1228 | icm->upstream_port = upstream_port; | |
1229 | icm->vnd_cap = cap; | |
1230 | ||
1231 | return true; | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | return false; | |
1236 | } | |
1237 | ||
1238 | static int icm_ar_get_mode(struct tb *tb) | |
1239 | { | |
1240 | struct tb_nhi *nhi = tb->nhi; | |
3e315e54 | 1241 | int retries = 60; |
f67cf491 MW |
1242 | u32 val; |
1243 | ||
1244 | do { | |
1245 | val = ioread32(nhi->iobase + REG_FW_STS); | |
1246 | if (val & REG_FW_STS_NVM_AUTH_DONE) | |
1247 | break; | |
3e315e54 | 1248 | msleep(50); |
f67cf491 MW |
1249 | } while (--retries); |
1250 | ||
1251 | if (!retries) { | |
1252 | dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n"); | |
1253 | return -ENODEV; | |
1254 | } | |
1255 | ||
1256 | return nhi_mailbox_mode(nhi); | |
1257 | } | |
1258 | ||
afa11ff5 MW |
1259 | static int |
1260 | icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level, | |
2de98e05 | 1261 | size_t *nboot_acl, bool *rpm) |
afa11ff5 MW |
1262 | { |
1263 | struct icm_ar_pkg_driver_ready_response reply; | |
1264 | struct icm_pkg_driver_ready request = { | |
1265 | .hdr.code = ICM_DRIVER_READY, | |
1266 | }; | |
1267 | int ret; | |
1268 | ||
1269 | memset(&reply, 0, sizeof(reply)); | |
1270 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
1271 | 1, ICM_TIMEOUT); | |
1272 | if (ret) | |
1273 | return ret; | |
1274 | ||
1275 | if (security_level) | |
1276 | *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK; | |
1277 | if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED)) | |
1278 | *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >> | |
1279 | ICM_AR_INFO_BOOT_ACL_SHIFT; | |
2de98e05 MW |
1280 | if (rpm) |
1281 | *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3); | |
1282 | ||
afa11ff5 MW |
1283 | return 0; |
1284 | } | |
1285 | ||
f67cf491 MW |
1286 | static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route) |
1287 | { | |
1288 | struct icm_ar_pkg_get_route_response reply; | |
1289 | struct icm_ar_pkg_get_route request = { | |
1290 | .hdr = { .code = ICM_GET_ROUTE }, | |
1291 | .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link, | |
1292 | }; | |
1293 | int ret; | |
1294 | ||
1295 | memset(&reply, 0, sizeof(reply)); | |
1296 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
1297 | 1, ICM_TIMEOUT); | |
1298 | if (ret) | |
1299 | return ret; | |
1300 | ||
1301 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
1302 | return -EIO; | |
1303 | ||
1304 | *route = get_route(reply.route_hi, reply.route_lo); | |
1305 | return 0; | |
1306 | } | |
1307 | ||
afa11ff5 MW |
1308 | static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids) |
1309 | { | |
1310 | struct icm_ar_pkg_preboot_acl_response reply; | |
1311 | struct icm_ar_pkg_preboot_acl request = { | |
1312 | .hdr = { .code = ICM_PREBOOT_ACL }, | |
1313 | }; | |
1314 | int ret, i; | |
1315 | ||
1316 | memset(&reply, 0, sizeof(reply)); | |
1317 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
1318 | 1, ICM_TIMEOUT); | |
1319 | if (ret) | |
1320 | return ret; | |
1321 | ||
1322 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
1323 | return -EIO; | |
1324 | ||
1325 | for (i = 0; i < nuuids; i++) { | |
1326 | u32 *uuid = (u32 *)&uuids[i]; | |
1327 | ||
1328 | uuid[0] = reply.acl[i].uuid_lo; | |
1329 | uuid[1] = reply.acl[i].uuid_hi; | |
1330 | ||
1331 | if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) { | |
1332 | /* Map empty entries to null UUID */ | |
1333 | uuid[0] = 0; | |
1334 | uuid[1] = 0; | |
a78af936 | 1335 | } else if (uuid[0] != 0 || uuid[1] != 0) { |
afa11ff5 MW |
1336 | /* Upper two DWs are always one's */ |
1337 | uuid[2] = 0xffffffff; | |
1338 | uuid[3] = 0xffffffff; | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | return ret; | |
1343 | } | |
1344 | ||
1345 | static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids, | |
1346 | size_t nuuids) | |
1347 | { | |
1348 | struct icm_ar_pkg_preboot_acl_response reply; | |
1349 | struct icm_ar_pkg_preboot_acl request = { | |
1350 | .hdr = { | |
1351 | .code = ICM_PREBOOT_ACL, | |
1352 | .flags = ICM_FLAGS_WRITE, | |
1353 | }, | |
1354 | }; | |
1355 | int ret, i; | |
1356 | ||
1357 | for (i = 0; i < nuuids; i++) { | |
1358 | const u32 *uuid = (const u32 *)&uuids[i]; | |
1359 | ||
1360 | if (uuid_is_null(&uuids[i])) { | |
1361 | /* | |
1362 | * Map null UUID to the empty (all one) entries | |
1363 | * for ICM. | |
1364 | */ | |
1365 | request.acl[i].uuid_lo = 0xffffffff; | |
1366 | request.acl[i].uuid_hi = 0xffffffff; | |
1367 | } else { | |
1368 | /* Two high DWs need to be set to all one */ | |
1369 | if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff) | |
1370 | return -EINVAL; | |
1371 | ||
1372 | request.acl[i].uuid_lo = uuid[0]; | |
1373 | request.acl[i].uuid_hi = uuid[1]; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | memset(&reply, 0, sizeof(reply)); | |
1378 | ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), | |
1379 | 1, ICM_TIMEOUT); | |
1380 | if (ret) | |
1381 | return ret; | |
1382 | ||
1383 | if (reply.hdr.flags & ICM_FLAGS_ERROR) | |
1384 | return -EIO; | |
1385 | ||
1386 | return 0; | |
1387 | } | |
1388 | ||
f67cf491 MW |
1389 | static void icm_handle_notification(struct work_struct *work) |
1390 | { | |
1391 | struct icm_notification *n = container_of(work, typeof(*n), work); | |
1392 | struct tb *tb = n->tb; | |
1393 | struct icm *icm = tb_priv(tb); | |
1394 | ||
1395 | mutex_lock(&tb->lock); | |
1396 | ||
59282235 MW |
1397 | /* |
1398 | * When the domain is stopped we flush its workqueue but before | |
1399 | * that the root switch is removed. In that case we should treat | |
1400 | * the queued events as being canceled. | |
1401 | */ | |
1402 | if (tb->root_switch) { | |
1403 | switch (n->pkg->code) { | |
1404 | case ICM_EVENT_DEVICE_CONNECTED: | |
1405 | icm->device_connected(tb, n->pkg); | |
1406 | break; | |
1407 | case ICM_EVENT_DEVICE_DISCONNECTED: | |
1408 | icm->device_disconnected(tb, n->pkg); | |
1409 | break; | |
1410 | case ICM_EVENT_XDOMAIN_CONNECTED: | |
1411 | icm->xdomain_connected(tb, n->pkg); | |
1412 | break; | |
1413 | case ICM_EVENT_XDOMAIN_DISCONNECTED: | |
1414 | icm->xdomain_disconnected(tb, n->pkg); | |
1415 | break; | |
1416 | } | |
f67cf491 MW |
1417 | } |
1418 | ||
1419 | mutex_unlock(&tb->lock); | |
1420 | ||
1421 | kfree(n->pkg); | |
1422 | kfree(n); | |
1423 | } | |
1424 | ||
1425 | static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, | |
1426 | const void *buf, size_t size) | |
1427 | { | |
1428 | struct icm_notification *n; | |
1429 | ||
1430 | n = kmalloc(sizeof(*n), GFP_KERNEL); | |
1431 | if (!n) | |
1432 | return; | |
1433 | ||
1434 | INIT_WORK(&n->work, icm_handle_notification); | |
1435 | n->pkg = kmemdup(buf, size, GFP_KERNEL); | |
1436 | n->tb = tb; | |
1437 | ||
1438 | queue_work(tb->wq, &n->work); | |
1439 | } | |
1440 | ||
1441 | static int | |
afa11ff5 | 1442 | __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level, |
2de98e05 | 1443 | size_t *nboot_acl, bool *rpm) |
f67cf491 | 1444 | { |
c1c79d65 | 1445 | struct icm *icm = tb_priv(tb); |
289b62e3 | 1446 | unsigned int retries = 50; |
f67cf491 MW |
1447 | int ret; |
1448 | ||
2de98e05 | 1449 | ret = icm->driver_ready(tb, security_level, nboot_acl, rpm); |
c1c79d65 MW |
1450 | if (ret) { |
1451 | tb_err(tb, "failed to send driver ready to ICM\n"); | |
f67cf491 | 1452 | return ret; |
c1c79d65 | 1453 | } |
f67cf491 MW |
1454 | |
1455 | /* | |
1456 | * Hold on here until the switch config space is accessible so | |
1457 | * that we can read root switch config successfully. | |
1458 | */ | |
1459 | do { | |
1460 | struct tb_cfg_result res; | |
1461 | u32 tmp; | |
1462 | ||
1463 | res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH, | |
1464 | 0, 1, 100); | |
1465 | if (!res.err) | |
1466 | return 0; | |
1467 | ||
1468 | msleep(50); | |
1469 | } while (--retries); | |
1470 | ||
289b62e3 | 1471 | tb_err(tb, "failed to read root switch config space, giving up\n"); |
f67cf491 MW |
1472 | return -ETIMEDOUT; |
1473 | } | |
1474 | ||
1475 | static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec) | |
1476 | { | |
1477 | unsigned long end = jiffies + msecs_to_jiffies(timeout_msec); | |
1478 | u32 cmd; | |
1479 | ||
1480 | do { | |
1481 | pci_read_config_dword(icm->upstream_port, | |
1482 | icm->vnd_cap + PCIE2CIO_CMD, &cmd); | |
1483 | if (!(cmd & PCIE2CIO_CMD_START)) { | |
1484 | if (cmd & PCIE2CIO_CMD_TIMEOUT) | |
1485 | break; | |
1486 | return 0; | |
1487 | } | |
1488 | ||
1489 | msleep(50); | |
1490 | } while (time_before(jiffies, end)); | |
1491 | ||
1492 | return -ETIMEDOUT; | |
1493 | } | |
1494 | ||
1495 | static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs, | |
1496 | unsigned int port, unsigned int index, u32 *data) | |
1497 | { | |
1498 | struct pci_dev *pdev = icm->upstream_port; | |
1499 | int ret, vnd_cap = icm->vnd_cap; | |
1500 | u32 cmd; | |
1501 | ||
1502 | cmd = index; | |
1503 | cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; | |
1504 | cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; | |
1505 | cmd |= PCIE2CIO_CMD_START; | |
1506 | pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); | |
1507 | ||
1508 | ret = pci2cio_wait_completion(icm, 5000); | |
1509 | if (ret) | |
1510 | return ret; | |
1511 | ||
1512 | pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data); | |
1513 | return 0; | |
1514 | } | |
1515 | ||
1516 | static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs, | |
1517 | unsigned int port, unsigned int index, u32 data) | |
1518 | { | |
1519 | struct pci_dev *pdev = icm->upstream_port; | |
1520 | int vnd_cap = icm->vnd_cap; | |
1521 | u32 cmd; | |
1522 | ||
1523 | pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data); | |
1524 | ||
1525 | cmd = index; | |
1526 | cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; | |
1527 | cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; | |
1528 | cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START; | |
1529 | pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); | |
1530 | ||
1531 | return pci2cio_wait_completion(icm, 5000); | |
1532 | } | |
1533 | ||
1534 | static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi) | |
1535 | { | |
1536 | struct icm *icm = tb_priv(tb); | |
1537 | u32 val; | |
1538 | ||
77b386f3 MW |
1539 | if (!icm->upstream_port) |
1540 | return -ENODEV; | |
1541 | ||
f67cf491 MW |
1542 | /* Put ARC to wait for CIO reset event to happen */ |
1543 | val = ioread32(nhi->iobase + REG_FW_STS); | |
1544 | val |= REG_FW_STS_CIO_RESET_REQ; | |
1545 | iowrite32(val, nhi->iobase + REG_FW_STS); | |
1546 | ||
1547 | /* Re-start ARC */ | |
1548 | val = ioread32(nhi->iobase + REG_FW_STS); | |
1549 | val |= REG_FW_STS_ICM_EN_INVERT; | |
1550 | val |= REG_FW_STS_ICM_EN_CPU; | |
1551 | iowrite32(val, nhi->iobase + REG_FW_STS); | |
1552 | ||
1553 | /* Trigger CIO reset now */ | |
1554 | return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9)); | |
1555 | } | |
1556 | ||
1557 | static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi) | |
1558 | { | |
1559 | unsigned int retries = 10; | |
1560 | int ret; | |
1561 | u32 val; | |
1562 | ||
1563 | /* Check if the ICM firmware is already running */ | |
1564 | val = ioread32(nhi->iobase + REG_FW_STS); | |
1565 | if (val & REG_FW_STS_ICM_EN) | |
1566 | return 0; | |
1567 | ||
1568 | dev_info(&nhi->pdev->dev, "starting ICM firmware\n"); | |
1569 | ||
1570 | ret = icm_firmware_reset(tb, nhi); | |
1571 | if (ret) | |
1572 | return ret; | |
1573 | ||
1574 | /* Wait until the ICM firmware tells us it is up and running */ | |
1575 | do { | |
1576 | /* Check that the ICM firmware is running */ | |
1577 | val = ioread32(nhi->iobase + REG_FW_STS); | |
1578 | if (val & REG_FW_STS_NVM_AUTH_DONE) | |
1579 | return 0; | |
1580 | ||
1581 | msleep(300); | |
1582 | } while (--retries); | |
1583 | ||
1584 | return -ETIMEDOUT; | |
1585 | } | |
1586 | ||
1587 | static int icm_reset_phy_port(struct tb *tb, int phy_port) | |
1588 | { | |
1589 | struct icm *icm = tb_priv(tb); | |
1590 | u32 state0, state1; | |
1591 | int port0, port1; | |
1592 | u32 val0, val1; | |
1593 | int ret; | |
1594 | ||
1595 | if (!icm->upstream_port) | |
1596 | return 0; | |
1597 | ||
1598 | if (phy_port) { | |
1599 | port0 = 3; | |
1600 | port1 = 4; | |
1601 | } else { | |
1602 | port0 = 1; | |
1603 | port1 = 2; | |
1604 | } | |
1605 | ||
1606 | /* | |
1607 | * Read link status of both null ports belonging to a single | |
1608 | * physical port. | |
1609 | */ | |
1610 | ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0); | |
1611 | if (ret) | |
1612 | return ret; | |
1613 | ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1); | |
1614 | if (ret) | |
1615 | return ret; | |
1616 | ||
1617 | state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK; | |
1618 | state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT; | |
1619 | state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK; | |
1620 | state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT; | |
1621 | ||
1622 | /* If they are both up we need to reset them now */ | |
1623 | if (state0 != TB_PORT_UP || state1 != TB_PORT_UP) | |
1624 | return 0; | |
1625 | ||
1626 | val0 |= PHY_PORT_CS1_LINK_DISABLE; | |
1627 | ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0); | |
1628 | if (ret) | |
1629 | return ret; | |
1630 | ||
1631 | val1 |= PHY_PORT_CS1_LINK_DISABLE; | |
1632 | ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1); | |
1633 | if (ret) | |
1634 | return ret; | |
1635 | ||
1636 | /* Wait a bit and then re-enable both ports */ | |
1637 | usleep_range(10, 100); | |
1638 | ||
1639 | ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0); | |
1640 | if (ret) | |
1641 | return ret; | |
1642 | ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1); | |
1643 | if (ret) | |
1644 | return ret; | |
1645 | ||
1646 | val0 &= ~PHY_PORT_CS1_LINK_DISABLE; | |
1647 | ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0); | |
1648 | if (ret) | |
1649 | return ret; | |
1650 | ||
1651 | val1 &= ~PHY_PORT_CS1_LINK_DISABLE; | |
1652 | return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1); | |
1653 | } | |
1654 | ||
1655 | static int icm_firmware_init(struct tb *tb) | |
1656 | { | |
1657 | struct icm *icm = tb_priv(tb); | |
1658 | struct tb_nhi *nhi = tb->nhi; | |
1659 | int ret; | |
1660 | ||
1661 | ret = icm_firmware_start(tb, nhi); | |
1662 | if (ret) { | |
1663 | dev_err(&nhi->pdev->dev, "could not start ICM firmware\n"); | |
1664 | return ret; | |
1665 | } | |
1666 | ||
1667 | if (icm->get_mode) { | |
1668 | ret = icm->get_mode(tb); | |
1669 | ||
1670 | switch (ret) { | |
e6b245cc MW |
1671 | case NHI_FW_SAFE_MODE: |
1672 | icm->safe_mode = true; | |
1673 | break; | |
1674 | ||
f67cf491 MW |
1675 | case NHI_FW_CM_MODE: |
1676 | /* Ask ICM to accept all Thunderbolt devices */ | |
1677 | nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0); | |
1678 | break; | |
1679 | ||
1680 | default: | |
3e315e54 MW |
1681 | if (ret < 0) |
1682 | return ret; | |
1683 | ||
f67cf491 MW |
1684 | tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret); |
1685 | return -ENODEV; | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | /* | |
1690 | * Reset both physical ports if there is anything connected to | |
1691 | * them already. | |
1692 | */ | |
1693 | ret = icm_reset_phy_port(tb, 0); | |
1694 | if (ret) | |
1695 | dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n"); | |
1696 | ret = icm_reset_phy_port(tb, 1); | |
1697 | if (ret) | |
1698 | dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n"); | |
1699 | ||
1700 | return 0; | |
1701 | } | |
1702 | ||
1703 | static int icm_driver_ready(struct tb *tb) | |
1704 | { | |
e6b245cc | 1705 | struct icm *icm = tb_priv(tb); |
f67cf491 MW |
1706 | int ret; |
1707 | ||
1708 | ret = icm_firmware_init(tb); | |
1709 | if (ret) | |
1710 | return ret; | |
1711 | ||
e6b245cc MW |
1712 | if (icm->safe_mode) { |
1713 | tb_info(tb, "Thunderbolt host controller is in safe mode.\n"); | |
1714 | tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n"); | |
1715 | tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n"); | |
1716 | return 0; | |
1717 | } | |
1718 | ||
2de98e05 MW |
1719 | ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl, |
1720 | &icm->rpm); | |
afa11ff5 MW |
1721 | if (ret) |
1722 | return ret; | |
1723 | ||
1724 | /* | |
1725 | * Make sure the number of supported preboot ACL matches what we | |
1726 | * expect or disable the whole feature. | |
1727 | */ | |
1728 | if (tb->nboot_acl > icm->max_boot_acl) | |
1729 | tb->nboot_acl = 0; | |
1730 | ||
1731 | return 0; | |
f67cf491 MW |
1732 | } |
1733 | ||
1734 | static int icm_suspend(struct tb *tb) | |
1735 | { | |
fa3f20f6 | 1736 | struct icm *icm = tb_priv(tb); |
a684c5b1 | 1737 | |
fa3f20f6 MW |
1738 | if (icm->save_devices) |
1739 | icm->save_devices(tb); | |
a684c5b1 | 1740 | |
fa3f20f6 | 1741 | nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); |
a684c5b1 | 1742 | return 0; |
f67cf491 MW |
1743 | } |
1744 | ||
1745 | /* | |
1746 | * Mark all switches (except root switch) below this one unplugged. ICM | |
1747 | * firmware will send us an updated list of switches after we have send | |
1748 | * it driver ready command. If a switch is not in that list it will be | |
1749 | * removed when we perform rescan. | |
1750 | */ | |
1751 | static void icm_unplug_children(struct tb_switch *sw) | |
1752 | { | |
1753 | unsigned int i; | |
1754 | ||
1755 | if (tb_route(sw)) | |
1756 | sw->is_unplugged = true; | |
1757 | ||
1758 | for (i = 1; i <= sw->config.max_port_number; i++) { | |
1759 | struct tb_port *port = &sw->ports[i]; | |
1760 | ||
1761 | if (tb_is_upstream_port(port)) | |
1762 | continue; | |
d1ff7024 MW |
1763 | if (port->xdomain) { |
1764 | port->xdomain->is_unplugged = true; | |
1765 | continue; | |
1766 | } | |
f67cf491 MW |
1767 | if (!port->remote) |
1768 | continue; | |
1769 | ||
1770 | icm_unplug_children(port->remote->sw); | |
1771 | } | |
1772 | } | |
1773 | ||
1774 | static void icm_free_unplugged_children(struct tb_switch *sw) | |
1775 | { | |
1776 | unsigned int i; | |
1777 | ||
1778 | for (i = 1; i <= sw->config.max_port_number; i++) { | |
1779 | struct tb_port *port = &sw->ports[i]; | |
1780 | ||
1781 | if (tb_is_upstream_port(port)) | |
1782 | continue; | |
d1ff7024 MW |
1783 | |
1784 | if (port->xdomain && port->xdomain->is_unplugged) { | |
1785 | tb_xdomain_remove(port->xdomain); | |
1786 | port->xdomain = NULL; | |
1787 | continue; | |
1788 | } | |
1789 | ||
f67cf491 MW |
1790 | if (!port->remote) |
1791 | continue; | |
1792 | ||
1793 | if (port->remote->sw->is_unplugged) { | |
1794 | tb_switch_remove(port->remote->sw); | |
1795 | port->remote = NULL; | |
1796 | } else { | |
1797 | icm_free_unplugged_children(port->remote->sw); | |
1798 | } | |
1799 | } | |
1800 | } | |
1801 | ||
1802 | static void icm_rescan_work(struct work_struct *work) | |
1803 | { | |
1804 | struct icm *icm = container_of(work, struct icm, rescan_work.work); | |
1805 | struct tb *tb = icm_to_tb(icm); | |
1806 | ||
1807 | mutex_lock(&tb->lock); | |
1808 | if (tb->root_switch) | |
1809 | icm_free_unplugged_children(tb->root_switch); | |
1810 | mutex_unlock(&tb->lock); | |
1811 | } | |
1812 | ||
1813 | static void icm_complete(struct tb *tb) | |
1814 | { | |
1815 | struct icm *icm = tb_priv(tb); | |
1816 | ||
1817 | if (tb->nhi->going_away) | |
1818 | return; | |
1819 | ||
1820 | icm_unplug_children(tb->root_switch); | |
1821 | ||
1822 | /* | |
1823 | * Now all existing children should be resumed, start events | |
1824 | * from ICM to get updated status. | |
1825 | */ | |
2de98e05 | 1826 | __icm_driver_ready(tb, NULL, NULL, NULL); |
f67cf491 MW |
1827 | |
1828 | /* | |
1829 | * We do not get notifications of devices that have been | |
1830 | * unplugged during suspend so schedule rescan to clean them up | |
1831 | * if any. | |
1832 | */ | |
1833 | queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500)); | |
1834 | } | |
1835 | ||
2de98e05 MW |
1836 | static int icm_runtime_suspend(struct tb *tb) |
1837 | { | |
1838 | nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); | |
1839 | return 0; | |
1840 | } | |
1841 | ||
1842 | static int icm_runtime_resume(struct tb *tb) | |
1843 | { | |
1844 | /* | |
1845 | * We can reuse the same resume functionality than with system | |
1846 | * suspend. | |
1847 | */ | |
1848 | icm_complete(tb); | |
1849 | return 0; | |
1850 | } | |
1851 | ||
f67cf491 MW |
1852 | static int icm_start(struct tb *tb) |
1853 | { | |
e6b245cc | 1854 | struct icm *icm = tb_priv(tb); |
f67cf491 MW |
1855 | int ret; |
1856 | ||
e6b245cc MW |
1857 | if (icm->safe_mode) |
1858 | tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0); | |
1859 | else | |
1860 | tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); | |
f67cf491 MW |
1861 | if (!tb->root_switch) |
1862 | return -ENODEV; | |
1863 | ||
e6b245cc MW |
1864 | /* |
1865 | * NVM upgrade has not been tested on Apple systems and they | |
1866 | * don't provide images publicly either. To be on the safe side | |
1867 | * prevent root switch NVM upgrade on Macs for now. | |
1868 | */ | |
630b3aff | 1869 | tb->root_switch->no_nvm_upgrade = x86_apple_machine; |
2de98e05 | 1870 | tb->root_switch->rpm = icm->rpm; |
e6b245cc | 1871 | |
f67cf491 | 1872 | ret = tb_switch_add(tb->root_switch); |
d1ff7024 | 1873 | if (ret) { |
f67cf491 | 1874 | tb_switch_put(tb->root_switch); |
d1ff7024 MW |
1875 | tb->root_switch = NULL; |
1876 | } | |
f67cf491 MW |
1877 | |
1878 | return ret; | |
1879 | } | |
1880 | ||
1881 | static void icm_stop(struct tb *tb) | |
1882 | { | |
1883 | struct icm *icm = tb_priv(tb); | |
1884 | ||
1885 | cancel_delayed_work(&icm->rescan_work); | |
1886 | tb_switch_remove(tb->root_switch); | |
1887 | tb->root_switch = NULL; | |
1888 | nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); | |
1889 | } | |
1890 | ||
e6b245cc MW |
1891 | static int icm_disconnect_pcie_paths(struct tb *tb) |
1892 | { | |
1893 | return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0); | |
1894 | } | |
1895 | ||
afa11ff5 | 1896 | /* Falcon Ridge */ |
f67cf491 MW |
1897 | static const struct tb_cm_ops icm_fr_ops = { |
1898 | .driver_ready = icm_driver_ready, | |
1899 | .start = icm_start, | |
1900 | .stop = icm_stop, | |
1901 | .suspend = icm_suspend, | |
1902 | .complete = icm_complete, | |
1903 | .handle_event = icm_handle_event, | |
1904 | .approve_switch = icm_fr_approve_switch, | |
1905 | .add_switch_key = icm_fr_add_switch_key, | |
1906 | .challenge_switch_key = icm_fr_challenge_switch_key, | |
e6b245cc | 1907 | .disconnect_pcie_paths = icm_disconnect_pcie_paths, |
d1ff7024 MW |
1908 | .approve_xdomain_paths = icm_fr_approve_xdomain_paths, |
1909 | .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths, | |
f67cf491 MW |
1910 | }; |
1911 | ||
afa11ff5 MW |
1912 | /* Alpine Ridge */ |
1913 | static const struct tb_cm_ops icm_ar_ops = { | |
1914 | .driver_ready = icm_driver_ready, | |
1915 | .start = icm_start, | |
1916 | .stop = icm_stop, | |
1917 | .suspend = icm_suspend, | |
1918 | .complete = icm_complete, | |
2de98e05 MW |
1919 | .runtime_suspend = icm_runtime_suspend, |
1920 | .runtime_resume = icm_runtime_resume, | |
afa11ff5 MW |
1921 | .handle_event = icm_handle_event, |
1922 | .get_boot_acl = icm_ar_get_boot_acl, | |
1923 | .set_boot_acl = icm_ar_set_boot_acl, | |
1924 | .approve_switch = icm_fr_approve_switch, | |
1925 | .add_switch_key = icm_fr_add_switch_key, | |
1926 | .challenge_switch_key = icm_fr_challenge_switch_key, | |
1927 | .disconnect_pcie_paths = icm_disconnect_pcie_paths, | |
1928 | .approve_xdomain_paths = icm_fr_approve_xdomain_paths, | |
1929 | .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths, | |
1930 | }; | |
1931 | ||
d1d8b263 RM |
1932 | /* Titan Ridge */ |
1933 | static const struct tb_cm_ops icm_tr_ops = { | |
1934 | .driver_ready = icm_driver_ready, | |
1935 | .start = icm_start, | |
1936 | .stop = icm_stop, | |
1937 | .suspend = icm_suspend, | |
1938 | .complete = icm_complete, | |
2de98e05 MW |
1939 | .runtime_suspend = icm_runtime_suspend, |
1940 | .runtime_resume = icm_runtime_resume, | |
d1d8b263 RM |
1941 | .handle_event = icm_handle_event, |
1942 | .get_boot_acl = icm_ar_get_boot_acl, | |
1943 | .set_boot_acl = icm_ar_set_boot_acl, | |
1944 | .approve_switch = icm_tr_approve_switch, | |
1945 | .add_switch_key = icm_tr_add_switch_key, | |
1946 | .challenge_switch_key = icm_tr_challenge_switch_key, | |
1947 | .disconnect_pcie_paths = icm_disconnect_pcie_paths, | |
1948 | .approve_xdomain_paths = icm_tr_approve_xdomain_paths, | |
1949 | .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths, | |
1950 | }; | |
1951 | ||
f67cf491 MW |
1952 | struct tb *icm_probe(struct tb_nhi *nhi) |
1953 | { | |
1954 | struct icm *icm; | |
1955 | struct tb *tb; | |
1956 | ||
1957 | tb = tb_domain_alloc(nhi, sizeof(struct icm)); | |
1958 | if (!tb) | |
1959 | return NULL; | |
1960 | ||
1961 | icm = tb_priv(tb); | |
1962 | INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work); | |
1963 | mutex_init(&icm->request_lock); | |
1964 | ||
1965 | switch (nhi->pdev->device) { | |
1966 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI: | |
1967 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI: | |
1968 | icm->is_supported = icm_fr_is_supported; | |
1969 | icm->get_route = icm_fr_get_route; | |
fa3f20f6 | 1970 | icm->save_devices = icm_fr_save_devices; |
c1c79d65 | 1971 | icm->driver_ready = icm_fr_driver_ready; |
f67cf491 MW |
1972 | icm->device_connected = icm_fr_device_connected; |
1973 | icm->device_disconnected = icm_fr_device_disconnected; | |
d1ff7024 MW |
1974 | icm->xdomain_connected = icm_fr_xdomain_connected; |
1975 | icm->xdomain_disconnected = icm_fr_xdomain_disconnected; | |
f67cf491 MW |
1976 | tb->cm_ops = &icm_fr_ops; |
1977 | break; | |
1978 | ||
1979 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI: | |
1980 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI: | |
1981 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI: | |
1982 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI: | |
1983 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI: | |
afa11ff5 | 1984 | icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES; |
f67cf491 MW |
1985 | icm->is_supported = icm_ar_is_supported; |
1986 | icm->get_mode = icm_ar_get_mode; | |
1987 | icm->get_route = icm_ar_get_route; | |
fa3f20f6 | 1988 | icm->save_devices = icm_fr_save_devices; |
afa11ff5 | 1989 | icm->driver_ready = icm_ar_driver_ready; |
f67cf491 MW |
1990 | icm->device_connected = icm_fr_device_connected; |
1991 | icm->device_disconnected = icm_fr_device_disconnected; | |
d1ff7024 MW |
1992 | icm->xdomain_connected = icm_fr_xdomain_connected; |
1993 | icm->xdomain_disconnected = icm_fr_xdomain_disconnected; | |
afa11ff5 | 1994 | tb->cm_ops = &icm_ar_ops; |
f67cf491 | 1995 | break; |
d1d8b263 RM |
1996 | |
1997 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI: | |
1998 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI: | |
1999 | icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES; | |
2000 | icm->is_supported = icm_ar_is_supported; | |
2001 | icm->get_mode = icm_ar_get_mode; | |
2002 | icm->driver_ready = icm_tr_driver_ready; | |
2003 | icm->device_connected = icm_tr_device_connected; | |
2004 | icm->device_disconnected = icm_tr_device_disconnected; | |
2005 | icm->xdomain_connected = icm_tr_xdomain_connected; | |
2006 | icm->xdomain_disconnected = icm_tr_xdomain_disconnected; | |
2007 | tb->cm_ops = &icm_tr_ops; | |
2008 | break; | |
f67cf491 MW |
2009 | } |
2010 | ||
2011 | if (!icm->is_supported || !icm->is_supported(tb)) { | |
2012 | dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n"); | |
2013 | tb_domain_put(tb); | |
2014 | return NULL; | |
2015 | } | |
2016 | ||
2017 | return tb; | |
2018 | } |