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