blob: 8490a1b6b61564320768012605f4837c77cf0663 [file] [log] [blame]
Mika Westerbergf67cf492017-06-06 15:25:16 +03001/*
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>
Mika Westerbergf67cf492017-06-06 15:25:16 +030016#include <linux/mutex.h>
17#include <linux/pci.h>
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030018#include <linux/pm_runtime.h>
Lukas Wunner630b3af2017-08-01 14:10:41 +020019#include <linux/platform_data/x86/apple.h>
Mika Westerbergf67cf492017-06-06 15:25:16 +030020#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
Mika Westerberg0b0a0bd2017-10-07 10:54:09 +030045#define ICM_TIMEOUT 5000 /* ms */
46#define ICM_APPROVE_TIMEOUT 10000 /* ms */
Mika Westerbergf67cf492017-06-06 15:25:16 +030047#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)
Mika Westerberge6b245c2017-06-06 15:25:17 +030059 * @safe_mode: ICM is in safe mode
Mika Westerberg9aaa3b82018-01-21 12:08:04 +020060 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030061 * @rpm: Does the controller support runtime PM (RTD3)
Mika Westerbergf67cf492017-06-06 15:25:16 +030062 * @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
Mika Westerbergd04522fa2018-07-25 11:03:19 +030065 * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
Mika Westerberg3080e192017-10-06 18:14:13 +030066 * @driver_ready: Send driver ready message to ICM
Mika Westerbergf67cf492017-06-06 15:25:16 +030067 * @device_connected: Handle device connected ICM message
68 * @device_disconnected: Handle device disconnected ICM message
Mika Westerbergd1ff7022017-10-02 13:38:34 +030069 * @xdomain_connected - Handle XDomain connected ICM message
70 * @xdomain_disconnected - Handle XDomain disconnected ICM message
Mika Westerbergf67cf492017-06-06 15:25:16 +030071 */
72struct icm {
73 struct mutex request_lock;
74 struct delayed_work rescan_work;
75 struct pci_dev *upstream_port;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +020076 size_t max_boot_acl;
Mika Westerbergf67cf492017-06-06 15:25:16 +030077 int vnd_cap;
Mika Westerberge6b245c2017-06-06 15:25:17 +030078 bool safe_mode;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030079 bool rpm;
Mika Westerbergf67cf492017-06-06 15:25:16 +030080 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);
Mika Westerbergd04522fa2018-07-25 11:03:19 +030083 void (*save_devices)(struct tb *tb);
Mika Westerberg3080e192017-10-06 18:14:13 +030084 int (*driver_ready)(struct tb *tb,
Mika Westerberg9aaa3b82018-01-21 12:08:04 +020085 enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030086 size_t *nboot_acl, bool *rpm);
Mika Westerbergf67cf492017-06-06 15:25:16 +030087 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);
Mika Westerbergd1ff7022017-10-02 13:38:34 +030091 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);
Mika Westerbergf67cf492017-06-06 15:25:16 +030095};
96
97struct icm_notification {
98 struct work_struct work;
99 struct icm_pkg_header *pkg;
100 struct tb *tb;
101};
102
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300103struct 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 */
112struct 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
123static 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
Mika Westerbergf67cf492017-06-06 15:25:16 +0300144static inline struct tb *icm_to_tb(struct icm *icm)
145{
146 return ((void *)icm - sizeof(struct tb));
147}
148
149static inline u8 phy_port_from_route(u64 route, u8 depth)
150{
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300151 u8 link;
152
153 link = depth ? route >> ((depth - 1) * 8) : route;
154 return tb_phy_port_from_link(link);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300155}
156
157static inline u8 dual_link_from_link(u8 link)
158{
159 return link ? ((link - 1) ^ 0x01) + 1 : 0;
160}
161
162static inline u64 get_route(u32 route_hi, u32 route_lo)
163{
164 return (u64)route_hi << 32 | route_lo;
165}
166
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300167static 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
Mika Westerbergf67cf492017-06-06 15:25:16 +0300173static 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
187static 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
200static 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
240static bool icm_fr_is_supported(struct tb *tb)
241{
Lukas Wunner630b3af2017-08-01 14:10:41 +0200242 return !x86_apple_machine;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300243}
244
245static 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
256static 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
302err_free:
303 kfree(switches);
304 return ret;
305}
306
Mika Westerbergd04522fa2018-07-25 11:03:19 +0300307static void icm_fr_save_devices(struct tb *tb)
308{
309 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
310}
311
Mika Westerberg3080e192017-10-06 18:14:13 +0300312static int
Mika Westerberg9aaa3b82018-01-21 12:08:04 +0200313icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300314 size_t *nboot_acl, bool *rpm)
Mika Westerberg3080e192017-10-06 18:14:13 +0300315{
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
Mika Westerbergf67cf492017-06-06 15:25:16 +0300334static 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),
Mika Westerberg0b0a0bd2017-10-07 10:54:09 +0300349 1, ICM_APPROVE_TIMEOUT);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300350 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
361static 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
388static 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
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300418static 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
446static 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
Mika Westerbergee487dd2017-10-04 14:42:20 +0300463static void add_switch(struct tb_switch *parent_sw, u64 route,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300464 const uuid_t *uuid, const u8 *ep_name,
465 size_t ep_name_size, u8 connection_id, u8 connection_key,
Mika Westerbergee487dd2017-10-04 14:42:20 +0300466 u8 link, u8 depth, enum tb_security_level security_level,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200467 bool authorized, bool boot)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300468{
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300469 const struct intel_vss *vss;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300470 struct tb_switch *sw;
471
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300472 pm_runtime_get_sync(&parent_sw->dev);
473
Mika Westerbergee487dd2017-10-04 14:42:20 +0300474 sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route);
475 if (!sw)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300476 goto out;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300477
478 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
Aditya Pakkiee40c8a2019-03-25 16:25:22 -0500479 if (!sw->uuid) {
480 tb_sw_warn(sw, "cannot allocate memory for switch\n");
481 tb_switch_put(sw);
482 goto out;
483 }
Mika Westerbergee487dd2017-10-04 14:42:20 +0300484 sw->connection_id = connection_id;
485 sw->connection_key = connection_key;
486 sw->link = link;
487 sw->depth = depth;
488 sw->authorized = authorized;
489 sw->security_level = security_level;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200490 sw->boot = boot;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300491
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300492 vss = parse_intel_vss(ep_name, ep_name_size);
493 if (vss)
494 sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
495
Mika Westerbergee487dd2017-10-04 14:42:20 +0300496 /* Link the two switches now */
497 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
498 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
499
500 if (tb_switch_add(sw)) {
501 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
502 tb_switch_put(sw);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300503 }
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300504
505out:
506 pm_runtime_mark_last_busy(&parent_sw->dev);
507 pm_runtime_put_autosuspend(&parent_sw->dev);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300508}
509
510static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
511 u64 route, u8 connection_id, u8 connection_key,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200512 u8 link, u8 depth, bool boot)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300513{
514 /* Disconnect from parent */
515 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
516 /* Re-connect via updated port*/
517 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
518
519 /* Update with the new addressing information */
520 sw->config.route_hi = upper_32_bits(route);
521 sw->config.route_lo = lower_32_bits(route);
522 sw->connection_id = connection_id;
523 sw->connection_key = connection_key;
524 sw->link = link;
525 sw->depth = depth;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200526 sw->boot = boot;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300527
528 /* This switch still exists */
529 sw->is_unplugged = false;
530}
531
Mika Westerbergf67cf492017-06-06 15:25:16 +0300532static void remove_switch(struct tb_switch *sw)
533{
534 struct tb_switch *parent_sw;
535
536 parent_sw = tb_to_switch(sw->dev.parent);
537 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
538 tb_switch_remove(sw);
539}
540
Mika Westerbergee487dd2017-10-04 14:42:20 +0300541static void add_xdomain(struct tb_switch *sw, u64 route,
542 const uuid_t *local_uuid, const uuid_t *remote_uuid,
543 u8 link, u8 depth)
544{
545 struct tb_xdomain *xd;
546
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300547 pm_runtime_get_sync(&sw->dev);
548
Mika Westerbergee487dd2017-10-04 14:42:20 +0300549 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
550 if (!xd)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300551 goto out;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300552
553 xd->link = link;
554 xd->depth = depth;
555
556 tb_port_at(route, sw)->xdomain = xd;
557
558 tb_xdomain_add(xd);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300559
560out:
561 pm_runtime_mark_last_busy(&sw->dev);
562 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300563}
564
565static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
566{
567 xd->link = link;
568 xd->route = route;
569 xd->is_unplugged = false;
570}
571
Mika Westerberg79fae982018-02-09 17:29:38 +0300572static void remove_xdomain(struct tb_xdomain *xd)
573{
574 struct tb_switch *sw;
575
576 sw = tb_to_switch(xd->dev.parent);
577 tb_port_at(xd->route, sw)->xdomain = NULL;
578 tb_xdomain_remove(xd);
579}
580
Mika Westerbergf67cf492017-06-06 15:25:16 +0300581static void
582icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
583{
584 const struct icm_fr_event_device_connected *pkg =
585 (const struct icm_fr_event_device_connected *)hdr;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300586 enum tb_security_level security_level;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300587 struct tb_switch *sw, *parent_sw;
588 struct icm *icm = tb_priv(tb);
589 bool authorized = false;
Mika Westerberg79fae982018-02-09 17:29:38 +0300590 struct tb_xdomain *xd;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300591 u8 link, depth;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200592 bool boot;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300593 u64 route;
594 int ret;
595
596 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
597 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
598 ICM_LINK_INFO_DEPTH_SHIFT;
599 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300600 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
601 ICM_FLAGS_SLEVEL_SHIFT;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200602 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300603
Mika Westerbergcb653ee2017-09-14 13:59:10 +0300604 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
605 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
606 link, depth);
607 return;
608 }
609
Mika Westerbergf67cf492017-06-06 15:25:16 +0300610 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
611 if (sw) {
612 u8 phy_port, sw_phy_port;
613
614 parent_sw = tb_to_switch(sw->dev.parent);
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300615 sw_phy_port = tb_phy_port_from_link(sw->link);
616 phy_port = tb_phy_port_from_link(link);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300617
618 /*
619 * On resume ICM will send us connected events for the
620 * devices that still are present. However, that
621 * information might have changed for example by the
622 * fact that a switch on a dual-link connection might
623 * have been enumerated using the other link now. Make
624 * sure our book keeping matches that.
625 */
626 if (sw->depth == depth && sw_phy_port == phy_port &&
627 !!sw->authorized == authorized) {
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300628 /*
629 * It was enumerated through another link so update
630 * route string accordingly.
631 */
632 if (sw->link != link) {
633 ret = icm->get_route(tb, link, depth, &route);
634 if (ret) {
635 tb_err(tb, "failed to update route string for switch at %u.%u\n",
636 link, depth);
637 tb_switch_put(sw);
638 return;
639 }
640 } else {
641 route = tb_route(sw);
642 }
643
Mika Westerbergee487dd2017-10-04 14:42:20 +0300644 update_switch(parent_sw, sw, route, pkg->connection_id,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200645 pkg->connection_key, link, depth, boot);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300646 tb_switch_put(sw);
647 return;
648 }
649
650 /*
651 * User connected the same switch to another physical
652 * port or to another part of the topology. Remove the
653 * existing switch now before adding the new one.
654 */
655 remove_switch(sw);
656 tb_switch_put(sw);
657 }
658
659 /*
660 * If the switch was not found by UUID, look for a switch on
661 * same physical port (taking possible link aggregation into
662 * account) and depth. If we found one it is definitely a stale
663 * one so remove it first.
664 */
665 sw = tb_switch_find_by_link_depth(tb, link, depth);
666 if (!sw) {
667 u8 dual_link;
668
669 dual_link = dual_link_from_link(link);
670 if (dual_link)
671 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
672 }
673 if (sw) {
674 remove_switch(sw);
675 tb_switch_put(sw);
676 }
677
Mika Westerberg79fae982018-02-09 17:29:38 +0300678 /* Remove existing XDomain connection if found */
679 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
680 if (xd) {
681 remove_xdomain(xd);
682 tb_xdomain_put(xd);
683 }
684
Mika Westerbergf67cf492017-06-06 15:25:16 +0300685 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
686 if (!parent_sw) {
687 tb_err(tb, "failed to find parent switch for %u.%u\n",
688 link, depth);
689 return;
690 }
691
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300692 ret = icm->get_route(tb, link, depth, &route);
693 if (ret) {
694 tb_err(tb, "failed to find route string for switch at %u.%u\n",
695 link, depth);
696 tb_switch_put(parent_sw);
697 return;
698 }
699
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300700 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
701 sizeof(pkg->ep_name), pkg->connection_id,
Mika Westerbergee487dd2017-10-04 14:42:20 +0300702 pkg->connection_key, link, depth, security_level,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200703 authorized, boot);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300704
Mika Westerbergf67cf492017-06-06 15:25:16 +0300705 tb_switch_put(parent_sw);
706}
707
708static void
709icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
710{
711 const struct icm_fr_event_device_disconnected *pkg =
712 (const struct icm_fr_event_device_disconnected *)hdr;
713 struct tb_switch *sw;
714 u8 link, depth;
715
716 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
717 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
718 ICM_LINK_INFO_DEPTH_SHIFT;
719
720 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
721 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
722 return;
723 }
724
725 sw = tb_switch_find_by_link_depth(tb, link, depth);
726 if (!sw) {
727 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
728 depth);
729 return;
730 }
731
732 remove_switch(sw);
733 tb_switch_put(sw);
734}
735
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300736static void
737icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
738{
739 const struct icm_fr_event_xdomain_connected *pkg =
740 (const struct icm_fr_event_xdomain_connected *)hdr;
741 struct tb_xdomain *xd;
742 struct tb_switch *sw;
743 u8 link, depth;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300744 u64 route;
745
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300746 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
747 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
748 ICM_LINK_INFO_DEPTH_SHIFT;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300749
750 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
751 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
752 return;
753 }
754
755 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
756
757 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
758 if (xd) {
759 u8 xd_phy_port, phy_port;
760
761 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
762 phy_port = phy_port_from_route(route, depth);
763
764 if (xd->depth == depth && xd_phy_port == phy_port) {
Mika Westerbergee487dd2017-10-04 14:42:20 +0300765 update_xdomain(xd, route, link);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300766 tb_xdomain_put(xd);
767 return;
768 }
769
770 /*
771 * If we find an existing XDomain connection remove it
772 * now. We need to go through login handshake and
773 * everything anyway to be able to re-establish the
774 * connection.
775 */
776 remove_xdomain(xd);
777 tb_xdomain_put(xd);
778 }
779
780 /*
781 * Look if there already exists an XDomain in the same place
782 * than the new one and in that case remove it because it is
783 * most likely another host that got disconnected.
784 */
785 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
786 if (!xd) {
787 u8 dual_link;
788
789 dual_link = dual_link_from_link(link);
790 if (dual_link)
791 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
792 depth);
793 }
794 if (xd) {
795 remove_xdomain(xd);
796 tb_xdomain_put(xd);
797 }
798
799 /*
800 * If the user disconnected a switch during suspend and
801 * connected another host to the same port, remove the switch
802 * first.
803 */
804 sw = get_switch_at_route(tb->root_switch, route);
805 if (sw)
806 remove_switch(sw);
807
808 sw = tb_switch_find_by_link_depth(tb, link, depth);
809 if (!sw) {
810 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
811 depth);
812 return;
813 }
814
Mika Westerbergee487dd2017-10-04 14:42:20 +0300815 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
816 depth);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300817 tb_switch_put(sw);
818}
819
820static void
821icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
822{
823 const struct icm_fr_event_xdomain_disconnected *pkg =
824 (const struct icm_fr_event_xdomain_disconnected *)hdr;
825 struct tb_xdomain *xd;
826
827 /*
828 * If the connection is through one or multiple devices, the
829 * XDomain device is removed along with them so it is fine if we
830 * cannot find it here.
831 */
832 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
833 if (xd) {
834 remove_xdomain(xd);
835 tb_xdomain_put(xd);
836 }
837}
838
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300839static int
840icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300841 size_t *nboot_acl, bool *rpm)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300842{
843 struct icm_tr_pkg_driver_ready_response reply;
844 struct icm_pkg_driver_ready request = {
845 .hdr.code = ICM_DRIVER_READY,
846 };
847 int ret;
848
849 memset(&reply, 0, sizeof(reply));
850 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
851 1, 20000);
852 if (ret)
853 return ret;
854
855 if (security_level)
856 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
857 if (nboot_acl)
858 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
859 ICM_TR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300860 if (rpm)
861 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
862
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300863 return 0;
864}
865
866static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
867{
868 struct icm_tr_pkg_approve_device request;
869 struct icm_tr_pkg_approve_device reply;
870 int ret;
871
872 memset(&request, 0, sizeof(request));
873 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
874 request.hdr.code = ICM_APPROVE_DEVICE;
875 request.route_lo = sw->config.route_lo;
876 request.route_hi = sw->config.route_hi;
877 request.connection_id = sw->connection_id;
878
879 memset(&reply, 0, sizeof(reply));
880 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
881 1, ICM_APPROVE_TIMEOUT);
882 if (ret)
883 return ret;
884
885 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
886 tb_warn(tb, "PCIe tunnel creation failed\n");
887 return -EIO;
888 }
889
890 return 0;
891}
892
893static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
894{
895 struct icm_tr_pkg_add_device_key_response reply;
896 struct icm_tr_pkg_add_device_key request;
897 int ret;
898
899 memset(&request, 0, sizeof(request));
900 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
901 request.hdr.code = ICM_ADD_DEVICE_KEY;
902 request.route_lo = sw->config.route_lo;
903 request.route_hi = sw->config.route_hi;
904 request.connection_id = sw->connection_id;
905 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
906
907 memset(&reply, 0, sizeof(reply));
908 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
909 1, ICM_TIMEOUT);
910 if (ret)
911 return ret;
912
913 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
914 tb_warn(tb, "Adding key to switch failed\n");
915 return -EIO;
916 }
917
918 return 0;
919}
920
921static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
922 const u8 *challenge, u8 *response)
923{
924 struct icm_tr_pkg_challenge_device_response reply;
925 struct icm_tr_pkg_challenge_device request;
926 int ret;
927
928 memset(&request, 0, sizeof(request));
929 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
930 request.hdr.code = ICM_CHALLENGE_DEVICE;
931 request.route_lo = sw->config.route_lo;
932 request.route_hi = sw->config.route_hi;
933 request.connection_id = sw->connection_id;
934 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
935
936 memset(&reply, 0, sizeof(reply));
937 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
938 1, ICM_TIMEOUT);
939 if (ret)
940 return ret;
941
942 if (reply.hdr.flags & ICM_FLAGS_ERROR)
943 return -EKEYREJECTED;
944 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
945 return -ENOKEY;
946
947 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
948
949 return 0;
950}
951
952static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
953{
954 struct icm_tr_pkg_approve_xdomain_response reply;
955 struct icm_tr_pkg_approve_xdomain request;
956 int ret;
957
958 memset(&request, 0, sizeof(request));
959 request.hdr.code = ICM_APPROVE_XDOMAIN;
960 request.route_hi = upper_32_bits(xd->route);
961 request.route_lo = lower_32_bits(xd->route);
962 request.transmit_path = xd->transmit_path;
963 request.transmit_ring = xd->transmit_ring;
964 request.receive_path = xd->receive_path;
965 request.receive_ring = xd->receive_ring;
966 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
967
968 memset(&reply, 0, sizeof(reply));
969 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
970 1, ICM_TIMEOUT);
971 if (ret)
972 return ret;
973
974 if (reply.hdr.flags & ICM_FLAGS_ERROR)
975 return -EIO;
976
977 return 0;
978}
979
980static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
981 int stage)
982{
983 struct icm_tr_pkg_disconnect_xdomain_response reply;
984 struct icm_tr_pkg_disconnect_xdomain request;
985 int ret;
986
987 memset(&request, 0, sizeof(request));
988 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
989 request.stage = stage;
990 request.route_hi = upper_32_bits(xd->route);
991 request.route_lo = lower_32_bits(xd->route);
992 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
993
994 memset(&reply, 0, sizeof(reply));
995 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
996 1, ICM_TIMEOUT);
997 if (ret)
998 return ret;
999
1000 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1001 return -EIO;
1002
1003 return 0;
1004}
1005
1006static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1007{
1008 int ret;
1009
1010 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1011 if (ret)
1012 return ret;
1013
1014 usleep_range(10, 50);
1015 return icm_tr_xdomain_tear_down(tb, xd, 2);
1016}
1017
1018static void
1019icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1020{
1021 const struct icm_tr_event_device_connected *pkg =
1022 (const struct icm_tr_event_device_connected *)hdr;
1023 enum tb_security_level security_level;
1024 struct tb_switch *sw, *parent_sw;
1025 struct tb_xdomain *xd;
1026 bool authorized, boot;
1027 u64 route;
1028
1029 /*
1030 * Currently we don't use the QoS information coming with the
1031 * device connected message so simply just ignore that extra
1032 * packet for now.
1033 */
1034 if (pkg->hdr.packet_id)
1035 return;
1036
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001037 route = get_route(pkg->route_hi, pkg->route_lo);
1038 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1039 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1040 ICM_FLAGS_SLEVEL_SHIFT;
1041 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
1042
1043 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1044 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1045 route);
1046 return;
1047 }
1048
1049 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1050 if (sw) {
1051 /* Update the switch if it is still in the same place */
1052 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1053 parent_sw = tb_to_switch(sw->dev.parent);
1054 update_switch(parent_sw, sw, route, pkg->connection_id,
1055 0, 0, 0, boot);
1056 tb_switch_put(sw);
1057 return;
1058 }
1059
1060 remove_switch(sw);
1061 tb_switch_put(sw);
1062 }
1063
1064 /* Another switch with the same address */
1065 sw = tb_switch_find_by_route(tb, route);
1066 if (sw) {
1067 remove_switch(sw);
1068 tb_switch_put(sw);
1069 }
1070
1071 /* XDomain connection with the same address */
1072 xd = tb_xdomain_find_by_route(tb, route);
1073 if (xd) {
1074 remove_xdomain(xd);
1075 tb_xdomain_put(xd);
1076 }
1077
1078 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1079 if (!parent_sw) {
1080 tb_err(tb, "failed to find parent switch for %llx\n", route);
1081 return;
1082 }
1083
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001084 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
1085 sizeof(pkg->ep_name), pkg->connection_id,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001086 0, 0, 0, security_level, authorized, boot);
1087
1088 tb_switch_put(parent_sw);
1089}
1090
1091static void
1092icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1093{
1094 const struct icm_tr_event_device_disconnected *pkg =
1095 (const struct icm_tr_event_device_disconnected *)hdr;
1096 struct tb_switch *sw;
1097 u64 route;
1098
1099 route = get_route(pkg->route_hi, pkg->route_lo);
1100
1101 sw = tb_switch_find_by_route(tb, route);
1102 if (!sw) {
1103 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1104 return;
1105 }
1106
1107 remove_switch(sw);
1108 tb_switch_put(sw);
1109}
1110
1111static void
1112icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1113{
1114 const struct icm_tr_event_xdomain_connected *pkg =
1115 (const struct icm_tr_event_xdomain_connected *)hdr;
1116 struct tb_xdomain *xd;
1117 struct tb_switch *sw;
1118 u64 route;
1119
1120 if (!tb->root_switch)
1121 return;
1122
1123 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1124
1125 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1126 if (xd) {
1127 if (xd->route == route) {
1128 update_xdomain(xd, route, 0);
1129 tb_xdomain_put(xd);
1130 return;
1131 }
1132
1133 remove_xdomain(xd);
1134 tb_xdomain_put(xd);
1135 }
1136
1137 /* An existing xdomain with the same address */
1138 xd = tb_xdomain_find_by_route(tb, route);
1139 if (xd) {
1140 remove_xdomain(xd);
1141 tb_xdomain_put(xd);
1142 }
1143
1144 /*
1145 * If the user disconnected a switch during suspend and
1146 * connected another host to the same port, remove the switch
1147 * first.
1148 */
1149 sw = get_switch_at_route(tb->root_switch, route);
1150 if (sw)
1151 remove_switch(sw);
1152
1153 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1154 if (!sw) {
1155 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1156 return;
1157 }
1158
1159 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1160 tb_switch_put(sw);
1161}
1162
1163static void
1164icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1165{
1166 const struct icm_tr_event_xdomain_disconnected *pkg =
1167 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1168 struct tb_xdomain *xd;
1169 u64 route;
1170
1171 route = get_route(pkg->route_hi, pkg->route_lo);
1172
1173 xd = tb_xdomain_find_by_route(tb, route);
1174 if (xd) {
1175 remove_xdomain(xd);
1176 tb_xdomain_put(xd);
1177 }
1178}
1179
Mika Westerbergf67cf492017-06-06 15:25:16 +03001180static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1181{
1182 struct pci_dev *parent;
1183
1184 parent = pci_upstream_bridge(pdev);
1185 while (parent) {
1186 if (!pci_is_pcie(parent))
1187 return NULL;
1188 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1189 break;
1190 parent = pci_upstream_bridge(parent);
1191 }
1192
1193 if (!parent)
1194 return NULL;
1195
1196 switch (parent->device) {
1197 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1198 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1199 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1200 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1201 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1202 return parent;
1203 }
1204
1205 return NULL;
1206}
1207
1208static bool icm_ar_is_supported(struct tb *tb)
1209{
1210 struct pci_dev *upstream_port;
1211 struct icm *icm = tb_priv(tb);
1212
1213 /*
1214 * Starting from Alpine Ridge we can use ICM on Apple machines
1215 * as well. We just need to reset and re-enable it first.
1216 */
Lukas Wunner630b3af2017-08-01 14:10:41 +02001217 if (!x86_apple_machine)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001218 return true;
1219
1220 /*
1221 * Find the upstream PCIe port in case we need to do reset
1222 * through its vendor specific registers.
1223 */
1224 upstream_port = get_upstream_port(tb->nhi->pdev);
1225 if (upstream_port) {
1226 int cap;
1227
1228 cap = pci_find_ext_capability(upstream_port,
1229 PCI_EXT_CAP_ID_VNDR);
1230 if (cap > 0) {
1231 icm->upstream_port = upstream_port;
1232 icm->vnd_cap = cap;
1233
1234 return true;
1235 }
1236 }
1237
1238 return false;
1239}
1240
1241static int icm_ar_get_mode(struct tb *tb)
1242{
1243 struct tb_nhi *nhi = tb->nhi;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001244 int retries = 60;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001245 u32 val;
1246
1247 do {
1248 val = ioread32(nhi->iobase + REG_FW_STS);
1249 if (val & REG_FW_STS_NVM_AUTH_DONE)
1250 break;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001251 msleep(50);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001252 } while (--retries);
1253
1254 if (!retries) {
1255 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1256 return -ENODEV;
1257 }
1258
1259 return nhi_mailbox_mode(nhi);
1260}
1261
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001262static int
1263icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001264 size_t *nboot_acl, bool *rpm)
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001265{
1266 struct icm_ar_pkg_driver_ready_response reply;
1267 struct icm_pkg_driver_ready request = {
1268 .hdr.code = ICM_DRIVER_READY,
1269 };
1270 int ret;
1271
1272 memset(&reply, 0, sizeof(reply));
1273 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1274 1, ICM_TIMEOUT);
1275 if (ret)
1276 return ret;
1277
1278 if (security_level)
1279 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1280 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1281 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1282 ICM_AR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001283 if (rpm)
1284 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1285
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001286 return 0;
1287}
1288
Mika Westerbergf67cf492017-06-06 15:25:16 +03001289static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1290{
1291 struct icm_ar_pkg_get_route_response reply;
1292 struct icm_ar_pkg_get_route request = {
1293 .hdr = { .code = ICM_GET_ROUTE },
1294 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1295 };
1296 int ret;
1297
1298 memset(&reply, 0, sizeof(reply));
1299 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1300 1, ICM_TIMEOUT);
1301 if (ret)
1302 return ret;
1303
1304 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1305 return -EIO;
1306
1307 *route = get_route(reply.route_hi, reply.route_lo);
1308 return 0;
1309}
1310
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001311static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1312{
1313 struct icm_ar_pkg_preboot_acl_response reply;
1314 struct icm_ar_pkg_preboot_acl request = {
1315 .hdr = { .code = ICM_PREBOOT_ACL },
1316 };
1317 int ret, i;
1318
1319 memset(&reply, 0, sizeof(reply));
1320 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1321 1, ICM_TIMEOUT);
1322 if (ret)
1323 return ret;
1324
1325 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1326 return -EIO;
1327
1328 for (i = 0; i < nuuids; i++) {
1329 u32 *uuid = (u32 *)&uuids[i];
1330
1331 uuid[0] = reply.acl[i].uuid_lo;
1332 uuid[1] = reply.acl[i].uuid_hi;
1333
1334 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1335 /* Map empty entries to null UUID */
1336 uuid[0] = 0;
1337 uuid[1] = 0;
Mika Westerbergdd010bd2018-05-15 16:04:25 +03001338 } else if (uuid[0] != 0 || uuid[1] != 0) {
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001339 /* Upper two DWs are always one's */
1340 uuid[2] = 0xffffffff;
1341 uuid[3] = 0xffffffff;
1342 }
1343 }
1344
1345 return ret;
1346}
1347
1348static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1349 size_t nuuids)
1350{
1351 struct icm_ar_pkg_preboot_acl_response reply;
1352 struct icm_ar_pkg_preboot_acl request = {
1353 .hdr = {
1354 .code = ICM_PREBOOT_ACL,
1355 .flags = ICM_FLAGS_WRITE,
1356 },
1357 };
1358 int ret, i;
1359
1360 for (i = 0; i < nuuids; i++) {
1361 const u32 *uuid = (const u32 *)&uuids[i];
1362
1363 if (uuid_is_null(&uuids[i])) {
1364 /*
1365 * Map null UUID to the empty (all one) entries
1366 * for ICM.
1367 */
1368 request.acl[i].uuid_lo = 0xffffffff;
1369 request.acl[i].uuid_hi = 0xffffffff;
1370 } else {
1371 /* Two high DWs need to be set to all one */
1372 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1373 return -EINVAL;
1374
1375 request.acl[i].uuid_lo = uuid[0];
1376 request.acl[i].uuid_hi = uuid[1];
1377 }
1378 }
1379
1380 memset(&reply, 0, sizeof(reply));
1381 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1382 1, ICM_TIMEOUT);
1383 if (ret)
1384 return ret;
1385
1386 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1387 return -EIO;
1388
1389 return 0;
1390}
1391
Mika Westerbergf67cf492017-06-06 15:25:16 +03001392static void icm_handle_notification(struct work_struct *work)
1393{
1394 struct icm_notification *n = container_of(work, typeof(*n), work);
1395 struct tb *tb = n->tb;
1396 struct icm *icm = tb_priv(tb);
1397
1398 mutex_lock(&tb->lock);
1399
Mika Westerberg86da8092018-09-24 13:20:44 +03001400 /*
1401 * When the domain is stopped we flush its workqueue but before
1402 * that the root switch is removed. In that case we should treat
1403 * the queued events as being canceled.
1404 */
1405 if (tb->root_switch) {
1406 switch (n->pkg->code) {
1407 case ICM_EVENT_DEVICE_CONNECTED:
1408 icm->device_connected(tb, n->pkg);
1409 break;
1410 case ICM_EVENT_DEVICE_DISCONNECTED:
1411 icm->device_disconnected(tb, n->pkg);
1412 break;
1413 case ICM_EVENT_XDOMAIN_CONNECTED:
1414 icm->xdomain_connected(tb, n->pkg);
1415 break;
1416 case ICM_EVENT_XDOMAIN_DISCONNECTED:
1417 icm->xdomain_disconnected(tb, n->pkg);
1418 break;
1419 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001420 }
1421
1422 mutex_unlock(&tb->lock);
1423
1424 kfree(n->pkg);
1425 kfree(n);
1426}
1427
1428static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1429 const void *buf, size_t size)
1430{
1431 struct icm_notification *n;
1432
1433 n = kmalloc(sizeof(*n), GFP_KERNEL);
1434 if (!n)
1435 return;
1436
1437 INIT_WORK(&n->work, icm_handle_notification);
1438 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1439 n->tb = tb;
1440
1441 queue_work(tb->wq, &n->work);
1442}
1443
1444static int
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001445__icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001446 size_t *nboot_acl, bool *rpm)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001447{
Mika Westerberg3080e192017-10-06 18:14:13 +03001448 struct icm *icm = tb_priv(tb);
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001449 unsigned int retries = 50;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001450 int ret;
1451
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001452 ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
Mika Westerberg3080e192017-10-06 18:14:13 +03001453 if (ret) {
1454 tb_err(tb, "failed to send driver ready to ICM\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001455 return ret;
Mika Westerberg3080e192017-10-06 18:14:13 +03001456 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001457
1458 /*
1459 * Hold on here until the switch config space is accessible so
1460 * that we can read root switch config successfully.
1461 */
1462 do {
1463 struct tb_cfg_result res;
1464 u32 tmp;
1465
1466 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1467 0, 1, 100);
1468 if (!res.err)
1469 return 0;
1470
1471 msleep(50);
1472 } while (--retries);
1473
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001474 tb_err(tb, "failed to read root switch config space, giving up\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001475 return -ETIMEDOUT;
1476}
1477
1478static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
1479{
1480 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
1481 u32 cmd;
1482
1483 do {
1484 pci_read_config_dword(icm->upstream_port,
1485 icm->vnd_cap + PCIE2CIO_CMD, &cmd);
1486 if (!(cmd & PCIE2CIO_CMD_START)) {
1487 if (cmd & PCIE2CIO_CMD_TIMEOUT)
1488 break;
1489 return 0;
1490 }
1491
1492 msleep(50);
1493 } while (time_before(jiffies, end));
1494
1495 return -ETIMEDOUT;
1496}
1497
1498static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
1499 unsigned int port, unsigned int index, u32 *data)
1500{
1501 struct pci_dev *pdev = icm->upstream_port;
1502 int ret, vnd_cap = icm->vnd_cap;
1503 u32 cmd;
1504
1505 cmd = index;
1506 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
1507 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
1508 cmd |= PCIE2CIO_CMD_START;
1509 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
1510
1511 ret = pci2cio_wait_completion(icm, 5000);
1512 if (ret)
1513 return ret;
1514
1515 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
1516 return 0;
1517}
1518
1519static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
1520 unsigned int port, unsigned int index, u32 data)
1521{
1522 struct pci_dev *pdev = icm->upstream_port;
1523 int vnd_cap = icm->vnd_cap;
1524 u32 cmd;
1525
1526 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
1527
1528 cmd = index;
1529 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
1530 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
1531 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
1532 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
1533
1534 return pci2cio_wait_completion(icm, 5000);
1535}
1536
1537static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1538{
1539 struct icm *icm = tb_priv(tb);
1540 u32 val;
1541
Mika Westerbergea9d7bb2018-03-09 13:17:01 +03001542 if (!icm->upstream_port)
1543 return -ENODEV;
1544
Mika Westerbergf67cf492017-06-06 15:25:16 +03001545 /* Put ARC to wait for CIO reset event to happen */
1546 val = ioread32(nhi->iobase + REG_FW_STS);
1547 val |= REG_FW_STS_CIO_RESET_REQ;
1548 iowrite32(val, nhi->iobase + REG_FW_STS);
1549
1550 /* Re-start ARC */
1551 val = ioread32(nhi->iobase + REG_FW_STS);
1552 val |= REG_FW_STS_ICM_EN_INVERT;
1553 val |= REG_FW_STS_ICM_EN_CPU;
1554 iowrite32(val, nhi->iobase + REG_FW_STS);
1555
1556 /* Trigger CIO reset now */
1557 return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
1558}
1559
1560static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1561{
1562 unsigned int retries = 10;
1563 int ret;
1564 u32 val;
1565
1566 /* Check if the ICM firmware is already running */
1567 val = ioread32(nhi->iobase + REG_FW_STS);
1568 if (val & REG_FW_STS_ICM_EN)
1569 return 0;
1570
1571 dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
1572
1573 ret = icm_firmware_reset(tb, nhi);
1574 if (ret)
1575 return ret;
1576
1577 /* Wait until the ICM firmware tells us it is up and running */
1578 do {
1579 /* Check that the ICM firmware is running */
1580 val = ioread32(nhi->iobase + REG_FW_STS);
1581 if (val & REG_FW_STS_NVM_AUTH_DONE)
1582 return 0;
1583
1584 msleep(300);
1585 } while (--retries);
1586
1587 return -ETIMEDOUT;
1588}
1589
1590static int icm_reset_phy_port(struct tb *tb, int phy_port)
1591{
1592 struct icm *icm = tb_priv(tb);
1593 u32 state0, state1;
1594 int port0, port1;
1595 u32 val0, val1;
1596 int ret;
1597
1598 if (!icm->upstream_port)
1599 return 0;
1600
1601 if (phy_port) {
1602 port0 = 3;
1603 port1 = 4;
1604 } else {
1605 port0 = 1;
1606 port1 = 2;
1607 }
1608
1609 /*
1610 * Read link status of both null ports belonging to a single
1611 * physical port.
1612 */
1613 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1614 if (ret)
1615 return ret;
1616 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1617 if (ret)
1618 return ret;
1619
1620 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1621 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1622 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1623 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1624
1625 /* If they are both up we need to reset them now */
1626 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1627 return 0;
1628
1629 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1630 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1631 if (ret)
1632 return ret;
1633
1634 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1635 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1636 if (ret)
1637 return ret;
1638
1639 /* Wait a bit and then re-enable both ports */
1640 usleep_range(10, 100);
1641
1642 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1643 if (ret)
1644 return ret;
1645 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1646 if (ret)
1647 return ret;
1648
1649 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1650 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1651 if (ret)
1652 return ret;
1653
1654 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1655 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1656}
1657
1658static int icm_firmware_init(struct tb *tb)
1659{
1660 struct icm *icm = tb_priv(tb);
1661 struct tb_nhi *nhi = tb->nhi;
1662 int ret;
1663
1664 ret = icm_firmware_start(tb, nhi);
1665 if (ret) {
1666 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1667 return ret;
1668 }
1669
1670 if (icm->get_mode) {
1671 ret = icm->get_mode(tb);
1672
1673 switch (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001674 case NHI_FW_SAFE_MODE:
1675 icm->safe_mode = true;
1676 break;
1677
Mika Westerbergf67cf492017-06-06 15:25:16 +03001678 case NHI_FW_CM_MODE:
1679 /* Ask ICM to accept all Thunderbolt devices */
1680 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1681 break;
1682
1683 default:
Mika Westerberge4be8c92017-11-24 17:51:12 +03001684 if (ret < 0)
1685 return ret;
1686
Mika Westerbergf67cf492017-06-06 15:25:16 +03001687 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1688 return -ENODEV;
1689 }
1690 }
1691
1692 /*
1693 * Reset both physical ports if there is anything connected to
1694 * them already.
1695 */
1696 ret = icm_reset_phy_port(tb, 0);
1697 if (ret)
1698 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1699 ret = icm_reset_phy_port(tb, 1);
1700 if (ret)
1701 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1702
1703 return 0;
1704}
1705
1706static int icm_driver_ready(struct tb *tb)
1707{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001708 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001709 int ret;
1710
1711 ret = icm_firmware_init(tb);
1712 if (ret)
1713 return ret;
1714
Mika Westerberge6b245c2017-06-06 15:25:17 +03001715 if (icm->safe_mode) {
1716 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1717 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1718 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1719 return 0;
1720 }
1721
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001722 ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
1723 &icm->rpm);
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001724 if (ret)
1725 return ret;
1726
1727 /*
1728 * Make sure the number of supported preboot ACL matches what we
1729 * expect or disable the whole feature.
1730 */
1731 if (tb->nboot_acl > icm->max_boot_acl)
1732 tb->nboot_acl = 0;
1733
1734 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001735}
1736
1737static int icm_suspend(struct tb *tb)
1738{
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001739 struct icm *icm = tb_priv(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001740
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001741 if (icm->save_devices)
1742 icm->save_devices(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001743
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001744 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001745 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001746}
1747
1748/*
1749 * Mark all switches (except root switch) below this one unplugged. ICM
1750 * firmware will send us an updated list of switches after we have send
1751 * it driver ready command. If a switch is not in that list it will be
1752 * removed when we perform rescan.
1753 */
1754static void icm_unplug_children(struct tb_switch *sw)
1755{
1756 unsigned int i;
1757
1758 if (tb_route(sw))
1759 sw->is_unplugged = true;
1760
1761 for (i = 1; i <= sw->config.max_port_number; i++) {
1762 struct tb_port *port = &sw->ports[i];
1763
1764 if (tb_is_upstream_port(port))
1765 continue;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001766 if (port->xdomain) {
1767 port->xdomain->is_unplugged = true;
1768 continue;
1769 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001770 if (!port->remote)
1771 continue;
1772
1773 icm_unplug_children(port->remote->sw);
1774 }
1775}
1776
1777static void icm_free_unplugged_children(struct tb_switch *sw)
1778{
1779 unsigned int i;
1780
1781 for (i = 1; i <= sw->config.max_port_number; i++) {
1782 struct tb_port *port = &sw->ports[i];
1783
1784 if (tb_is_upstream_port(port))
1785 continue;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001786
1787 if (port->xdomain && port->xdomain->is_unplugged) {
1788 tb_xdomain_remove(port->xdomain);
1789 port->xdomain = NULL;
1790 continue;
1791 }
1792
Mika Westerbergf67cf492017-06-06 15:25:16 +03001793 if (!port->remote)
1794 continue;
1795
1796 if (port->remote->sw->is_unplugged) {
1797 tb_switch_remove(port->remote->sw);
1798 port->remote = NULL;
1799 } else {
1800 icm_free_unplugged_children(port->remote->sw);
1801 }
1802 }
1803}
1804
1805static void icm_rescan_work(struct work_struct *work)
1806{
1807 struct icm *icm = container_of(work, struct icm, rescan_work.work);
1808 struct tb *tb = icm_to_tb(icm);
1809
1810 mutex_lock(&tb->lock);
1811 if (tb->root_switch)
1812 icm_free_unplugged_children(tb->root_switch);
1813 mutex_unlock(&tb->lock);
1814}
1815
1816static void icm_complete(struct tb *tb)
1817{
1818 struct icm *icm = tb_priv(tb);
1819
1820 if (tb->nhi->going_away)
1821 return;
1822
1823 icm_unplug_children(tb->root_switch);
1824
1825 /*
1826 * Now all existing children should be resumed, start events
1827 * from ICM to get updated status.
1828 */
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001829 __icm_driver_ready(tb, NULL, NULL, NULL);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001830
1831 /*
1832 * We do not get notifications of devices that have been
1833 * unplugged during suspend so schedule rescan to clean them up
1834 * if any.
1835 */
1836 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
1837}
1838
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001839static int icm_runtime_suspend(struct tb *tb)
1840{
1841 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1842 return 0;
1843}
1844
1845static int icm_runtime_resume(struct tb *tb)
1846{
1847 /*
1848 * We can reuse the same resume functionality than with system
1849 * suspend.
1850 */
1851 icm_complete(tb);
1852 return 0;
1853}
1854
Mika Westerbergf67cf492017-06-06 15:25:16 +03001855static int icm_start(struct tb *tb)
1856{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001857 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001858 int ret;
1859
Mika Westerberge6b245c2017-06-06 15:25:17 +03001860 if (icm->safe_mode)
1861 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
1862 else
1863 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001864 if (!tb->root_switch)
1865 return -ENODEV;
1866
Mika Westerberge6b245c2017-06-06 15:25:17 +03001867 /*
1868 * NVM upgrade has not been tested on Apple systems and they
1869 * don't provide images publicly either. To be on the safe side
1870 * prevent root switch NVM upgrade on Macs for now.
1871 */
Lukas Wunner630b3af2017-08-01 14:10:41 +02001872 tb->root_switch->no_nvm_upgrade = x86_apple_machine;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001873 tb->root_switch->rpm = icm->rpm;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001874
Mika Westerbergf67cf492017-06-06 15:25:16 +03001875 ret = tb_switch_add(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001876 if (ret) {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001877 tb_switch_put(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001878 tb->root_switch = NULL;
1879 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001880
1881 return ret;
1882}
1883
1884static void icm_stop(struct tb *tb)
1885{
1886 struct icm *icm = tb_priv(tb);
1887
1888 cancel_delayed_work(&icm->rescan_work);
1889 tb_switch_remove(tb->root_switch);
1890 tb->root_switch = NULL;
1891 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1892}
1893
Mika Westerberge6b245c2017-06-06 15:25:17 +03001894static int icm_disconnect_pcie_paths(struct tb *tb)
1895{
1896 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
1897}
1898
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001899/* Falcon Ridge */
Mika Westerbergf67cf492017-06-06 15:25:16 +03001900static const struct tb_cm_ops icm_fr_ops = {
1901 .driver_ready = icm_driver_ready,
1902 .start = icm_start,
1903 .stop = icm_stop,
1904 .suspend = icm_suspend,
1905 .complete = icm_complete,
1906 .handle_event = icm_handle_event,
1907 .approve_switch = icm_fr_approve_switch,
1908 .add_switch_key = icm_fr_add_switch_key,
1909 .challenge_switch_key = icm_fr_challenge_switch_key,
Mika Westerberge6b245c2017-06-06 15:25:17 +03001910 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001911 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1912 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
Mika Westerbergf67cf492017-06-06 15:25:16 +03001913};
1914
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001915/* Alpine Ridge */
1916static const struct tb_cm_ops icm_ar_ops = {
1917 .driver_ready = icm_driver_ready,
1918 .start = icm_start,
1919 .stop = icm_stop,
1920 .suspend = icm_suspend,
1921 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001922 .runtime_suspend = icm_runtime_suspend,
1923 .runtime_resume = icm_runtime_resume,
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001924 .handle_event = icm_handle_event,
1925 .get_boot_acl = icm_ar_get_boot_acl,
1926 .set_boot_acl = icm_ar_set_boot_acl,
1927 .approve_switch = icm_fr_approve_switch,
1928 .add_switch_key = icm_fr_add_switch_key,
1929 .challenge_switch_key = icm_fr_challenge_switch_key,
1930 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1931 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1932 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1933};
1934
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001935/* Titan Ridge */
1936static const struct tb_cm_ops icm_tr_ops = {
1937 .driver_ready = icm_driver_ready,
1938 .start = icm_start,
1939 .stop = icm_stop,
1940 .suspend = icm_suspend,
1941 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001942 .runtime_suspend = icm_runtime_suspend,
1943 .runtime_resume = icm_runtime_resume,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001944 .handle_event = icm_handle_event,
1945 .get_boot_acl = icm_ar_get_boot_acl,
1946 .set_boot_acl = icm_ar_set_boot_acl,
1947 .approve_switch = icm_tr_approve_switch,
1948 .add_switch_key = icm_tr_add_switch_key,
1949 .challenge_switch_key = icm_tr_challenge_switch_key,
1950 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1951 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
1952 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
1953};
1954
Mika Westerbergf67cf492017-06-06 15:25:16 +03001955struct tb *icm_probe(struct tb_nhi *nhi)
1956{
1957 struct icm *icm;
1958 struct tb *tb;
1959
1960 tb = tb_domain_alloc(nhi, sizeof(struct icm));
1961 if (!tb)
1962 return NULL;
1963
1964 icm = tb_priv(tb);
1965 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
1966 mutex_init(&icm->request_lock);
1967
1968 switch (nhi->pdev->device) {
1969 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1970 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1971 icm->is_supported = icm_fr_is_supported;
1972 icm->get_route = icm_fr_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001973 icm->save_devices = icm_fr_save_devices;
Mika Westerberg3080e192017-10-06 18:14:13 +03001974 icm->driver_ready = icm_fr_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001975 icm->device_connected = icm_fr_device_connected;
1976 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001977 icm->xdomain_connected = icm_fr_xdomain_connected;
1978 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001979 tb->cm_ops = &icm_fr_ops;
1980 break;
1981
1982 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
1983 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
1984 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
1985 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
1986 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001987 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001988 icm->is_supported = icm_ar_is_supported;
1989 icm->get_mode = icm_ar_get_mode;
1990 icm->get_route = icm_ar_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001991 icm->save_devices = icm_fr_save_devices;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001992 icm->driver_ready = icm_ar_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001993 icm->device_connected = icm_fr_device_connected;
1994 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001995 icm->xdomain_connected = icm_fr_xdomain_connected;
1996 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001997 tb->cm_ops = &icm_ar_ops;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001998 break;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001999
2000 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2001 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2002 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2003 icm->is_supported = icm_ar_is_supported;
2004 icm->get_mode = icm_ar_get_mode;
2005 icm->driver_ready = icm_tr_driver_ready;
2006 icm->device_connected = icm_tr_device_connected;
2007 icm->device_disconnected = icm_tr_device_disconnected;
2008 icm->xdomain_connected = icm_tr_xdomain_connected;
2009 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2010 tb->cm_ops = &icm_tr_ops;
2011 break;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002012 }
2013
2014 if (!icm->is_supported || !icm->is_supported(tb)) {
2015 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
2016 tb_domain_put(tb);
2017 return NULL;
2018 }
2019
2020 return tb;
2021}