blob: e1e264a9a4c76f11f1553327d59deb0fd8f43168 [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);
479 sw->connection_id = connection_id;
480 sw->connection_key = connection_key;
481 sw->link = link;
482 sw->depth = depth;
483 sw->authorized = authorized;
484 sw->security_level = security_level;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200485 sw->boot = boot;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300486
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300487 vss = parse_intel_vss(ep_name, ep_name_size);
488 if (vss)
489 sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
490
Mika Westerbergee487dd2017-10-04 14:42:20 +0300491 /* Link the two switches now */
492 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
493 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
494
495 if (tb_switch_add(sw)) {
496 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
497 tb_switch_put(sw);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300498 }
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300499
500out:
501 pm_runtime_mark_last_busy(&parent_sw->dev);
502 pm_runtime_put_autosuspend(&parent_sw->dev);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300503}
504
505static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
506 u64 route, u8 connection_id, u8 connection_key,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200507 u8 link, u8 depth, bool boot)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300508{
509 /* Disconnect from parent */
510 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
511 /* Re-connect via updated port*/
512 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
513
514 /* Update with the new addressing information */
515 sw->config.route_hi = upper_32_bits(route);
516 sw->config.route_lo = lower_32_bits(route);
517 sw->connection_id = connection_id;
518 sw->connection_key = connection_key;
519 sw->link = link;
520 sw->depth = depth;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200521 sw->boot = boot;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300522
523 /* This switch still exists */
524 sw->is_unplugged = false;
525}
526
Mika Westerbergf67cf492017-06-06 15:25:16 +0300527static void remove_switch(struct tb_switch *sw)
528{
529 struct tb_switch *parent_sw;
530
531 parent_sw = tb_to_switch(sw->dev.parent);
532 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
533 tb_switch_remove(sw);
534}
535
Mika Westerbergee487dd2017-10-04 14:42:20 +0300536static void add_xdomain(struct tb_switch *sw, u64 route,
537 const uuid_t *local_uuid, const uuid_t *remote_uuid,
538 u8 link, u8 depth)
539{
540 struct tb_xdomain *xd;
541
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300542 pm_runtime_get_sync(&sw->dev);
543
Mika Westerbergee487dd2017-10-04 14:42:20 +0300544 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
545 if (!xd)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300546 goto out;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300547
548 xd->link = link;
549 xd->depth = depth;
550
551 tb_port_at(route, sw)->xdomain = xd;
552
553 tb_xdomain_add(xd);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300554
555out:
556 pm_runtime_mark_last_busy(&sw->dev);
557 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300558}
559
560static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
561{
562 xd->link = link;
563 xd->route = route;
564 xd->is_unplugged = false;
565}
566
Mika Westerberg79fae982018-02-09 17:29:38 +0300567static void remove_xdomain(struct tb_xdomain *xd)
568{
569 struct tb_switch *sw;
570
571 sw = tb_to_switch(xd->dev.parent);
572 tb_port_at(xd->route, sw)->xdomain = NULL;
573 tb_xdomain_remove(xd);
574}
575
Mika Westerbergf67cf492017-06-06 15:25:16 +0300576static void
577icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
578{
579 const struct icm_fr_event_device_connected *pkg =
580 (const struct icm_fr_event_device_connected *)hdr;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300581 enum tb_security_level security_level;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300582 struct tb_switch *sw, *parent_sw;
583 struct icm *icm = tb_priv(tb);
584 bool authorized = false;
Mika Westerberg79fae982018-02-09 17:29:38 +0300585 struct tb_xdomain *xd;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300586 u8 link, depth;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200587 bool boot;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300588 u64 route;
589 int ret;
590
591 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
592 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
593 ICM_LINK_INFO_DEPTH_SHIFT;
594 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300595 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
596 ICM_FLAGS_SLEVEL_SHIFT;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200597 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300598
Mika Westerbergcb653ee2017-09-14 13:59:10 +0300599 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
600 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
601 link, depth);
602 return;
603 }
604
Mika Westerbergf67cf492017-06-06 15:25:16 +0300605 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
606 if (sw) {
607 u8 phy_port, sw_phy_port;
608
609 parent_sw = tb_to_switch(sw->dev.parent);
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300610 sw_phy_port = tb_phy_port_from_link(sw->link);
611 phy_port = tb_phy_port_from_link(link);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300612
613 /*
614 * On resume ICM will send us connected events for the
615 * devices that still are present. However, that
616 * information might have changed for example by the
617 * fact that a switch on a dual-link connection might
618 * have been enumerated using the other link now. Make
619 * sure our book keeping matches that.
620 */
621 if (sw->depth == depth && sw_phy_port == phy_port &&
622 !!sw->authorized == authorized) {
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300623 /*
624 * It was enumerated through another link so update
625 * route string accordingly.
626 */
627 if (sw->link != link) {
628 ret = icm->get_route(tb, link, depth, &route);
629 if (ret) {
630 tb_err(tb, "failed to update route string for switch at %u.%u\n",
631 link, depth);
632 tb_switch_put(sw);
633 return;
634 }
635 } else {
636 route = tb_route(sw);
637 }
638
Mika Westerbergee487dd2017-10-04 14:42:20 +0300639 update_switch(parent_sw, sw, route, pkg->connection_id,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200640 pkg->connection_key, link, depth, boot);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300641 tb_switch_put(sw);
642 return;
643 }
644
645 /*
646 * User connected the same switch to another physical
647 * port or to another part of the topology. Remove the
648 * existing switch now before adding the new one.
649 */
650 remove_switch(sw);
651 tb_switch_put(sw);
652 }
653
654 /*
655 * If the switch was not found by UUID, look for a switch on
656 * same physical port (taking possible link aggregation into
657 * account) and depth. If we found one it is definitely a stale
658 * one so remove it first.
659 */
660 sw = tb_switch_find_by_link_depth(tb, link, depth);
661 if (!sw) {
662 u8 dual_link;
663
664 dual_link = dual_link_from_link(link);
665 if (dual_link)
666 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
667 }
668 if (sw) {
669 remove_switch(sw);
670 tb_switch_put(sw);
671 }
672
Mika Westerberg79fae982018-02-09 17:29:38 +0300673 /* Remove existing XDomain connection if found */
674 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
675 if (xd) {
676 remove_xdomain(xd);
677 tb_xdomain_put(xd);
678 }
679
Mika Westerbergf67cf492017-06-06 15:25:16 +0300680 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
681 if (!parent_sw) {
682 tb_err(tb, "failed to find parent switch for %u.%u\n",
683 link, depth);
684 return;
685 }
686
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300687 ret = icm->get_route(tb, link, depth, &route);
688 if (ret) {
689 tb_err(tb, "failed to find route string for switch at %u.%u\n",
690 link, depth);
691 tb_switch_put(parent_sw);
692 return;
693 }
694
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300695 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
696 sizeof(pkg->ep_name), pkg->connection_id,
Mika Westerbergee487dd2017-10-04 14:42:20 +0300697 pkg->connection_key, link, depth, security_level,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200698 authorized, boot);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300699
Mika Westerbergf67cf492017-06-06 15:25:16 +0300700 tb_switch_put(parent_sw);
701}
702
703static void
704icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
705{
706 const struct icm_fr_event_device_disconnected *pkg =
707 (const struct icm_fr_event_device_disconnected *)hdr;
708 struct tb_switch *sw;
709 u8 link, depth;
710
711 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
712 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
713 ICM_LINK_INFO_DEPTH_SHIFT;
714
715 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
716 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
717 return;
718 }
719
720 sw = tb_switch_find_by_link_depth(tb, link, depth);
721 if (!sw) {
722 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
723 depth);
724 return;
725 }
726
727 remove_switch(sw);
728 tb_switch_put(sw);
729}
730
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300731static void
732icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
733{
734 const struct icm_fr_event_xdomain_connected *pkg =
735 (const struct icm_fr_event_xdomain_connected *)hdr;
736 struct tb_xdomain *xd;
737 struct tb_switch *sw;
738 u8 link, depth;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300739 u64 route;
740
741 /*
742 * After NVM upgrade adding root switch device fails because we
743 * initiated reset. During that time ICM might still send
744 * XDomain connected message which we ignore here.
745 */
746 if (!tb->root_switch)
747 return;
748
749 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
750 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
751 ICM_LINK_INFO_DEPTH_SHIFT;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300752
753 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
754 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
755 return;
756 }
757
758 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
759
760 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
761 if (xd) {
762 u8 xd_phy_port, phy_port;
763
764 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
765 phy_port = phy_port_from_route(route, depth);
766
767 if (xd->depth == depth && xd_phy_port == phy_port) {
Mika Westerbergee487dd2017-10-04 14:42:20 +0300768 update_xdomain(xd, route, link);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300769 tb_xdomain_put(xd);
770 return;
771 }
772
773 /*
774 * If we find an existing XDomain connection remove it
775 * now. We need to go through login handshake and
776 * everything anyway to be able to re-establish the
777 * connection.
778 */
779 remove_xdomain(xd);
780 tb_xdomain_put(xd);
781 }
782
783 /*
784 * Look if there already exists an XDomain in the same place
785 * than the new one and in that case remove it because it is
786 * most likely another host that got disconnected.
787 */
788 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
789 if (!xd) {
790 u8 dual_link;
791
792 dual_link = dual_link_from_link(link);
793 if (dual_link)
794 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
795 depth);
796 }
797 if (xd) {
798 remove_xdomain(xd);
799 tb_xdomain_put(xd);
800 }
801
802 /*
803 * If the user disconnected a switch during suspend and
804 * connected another host to the same port, remove the switch
805 * first.
806 */
807 sw = get_switch_at_route(tb->root_switch, route);
808 if (sw)
809 remove_switch(sw);
810
811 sw = tb_switch_find_by_link_depth(tb, link, depth);
812 if (!sw) {
813 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
814 depth);
815 return;
816 }
817
Mika Westerbergee487dd2017-10-04 14:42:20 +0300818 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
819 depth);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300820 tb_switch_put(sw);
821}
822
823static void
824icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
825{
826 const struct icm_fr_event_xdomain_disconnected *pkg =
827 (const struct icm_fr_event_xdomain_disconnected *)hdr;
828 struct tb_xdomain *xd;
829
830 /*
831 * If the connection is through one or multiple devices, the
832 * XDomain device is removed along with them so it is fine if we
833 * cannot find it here.
834 */
835 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
836 if (xd) {
837 remove_xdomain(xd);
838 tb_xdomain_put(xd);
839 }
840}
841
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300842static int
843icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300844 size_t *nboot_acl, bool *rpm)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300845{
846 struct icm_tr_pkg_driver_ready_response reply;
847 struct icm_pkg_driver_ready request = {
848 .hdr.code = ICM_DRIVER_READY,
849 };
850 int ret;
851
852 memset(&reply, 0, sizeof(reply));
853 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
854 1, 20000);
855 if (ret)
856 return ret;
857
858 if (security_level)
859 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
860 if (nboot_acl)
861 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
862 ICM_TR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300863 if (rpm)
864 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
865
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300866 return 0;
867}
868
869static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
870{
871 struct icm_tr_pkg_approve_device request;
872 struct icm_tr_pkg_approve_device reply;
873 int ret;
874
875 memset(&request, 0, sizeof(request));
876 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
877 request.hdr.code = ICM_APPROVE_DEVICE;
878 request.route_lo = sw->config.route_lo;
879 request.route_hi = sw->config.route_hi;
880 request.connection_id = sw->connection_id;
881
882 memset(&reply, 0, sizeof(reply));
883 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
884 1, ICM_APPROVE_TIMEOUT);
885 if (ret)
886 return ret;
887
888 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
889 tb_warn(tb, "PCIe tunnel creation failed\n");
890 return -EIO;
891 }
892
893 return 0;
894}
895
896static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
897{
898 struct icm_tr_pkg_add_device_key_response reply;
899 struct icm_tr_pkg_add_device_key request;
900 int ret;
901
902 memset(&request, 0, sizeof(request));
903 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
904 request.hdr.code = ICM_ADD_DEVICE_KEY;
905 request.route_lo = sw->config.route_lo;
906 request.route_hi = sw->config.route_hi;
907 request.connection_id = sw->connection_id;
908 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
909
910 memset(&reply, 0, sizeof(reply));
911 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
912 1, ICM_TIMEOUT);
913 if (ret)
914 return ret;
915
916 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
917 tb_warn(tb, "Adding key to switch failed\n");
918 return -EIO;
919 }
920
921 return 0;
922}
923
924static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
925 const u8 *challenge, u8 *response)
926{
927 struct icm_tr_pkg_challenge_device_response reply;
928 struct icm_tr_pkg_challenge_device request;
929 int ret;
930
931 memset(&request, 0, sizeof(request));
932 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
933 request.hdr.code = ICM_CHALLENGE_DEVICE;
934 request.route_lo = sw->config.route_lo;
935 request.route_hi = sw->config.route_hi;
936 request.connection_id = sw->connection_id;
937 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
938
939 memset(&reply, 0, sizeof(reply));
940 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
941 1, ICM_TIMEOUT);
942 if (ret)
943 return ret;
944
945 if (reply.hdr.flags & ICM_FLAGS_ERROR)
946 return -EKEYREJECTED;
947 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
948 return -ENOKEY;
949
950 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
951
952 return 0;
953}
954
955static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
956{
957 struct icm_tr_pkg_approve_xdomain_response reply;
958 struct icm_tr_pkg_approve_xdomain request;
959 int ret;
960
961 memset(&request, 0, sizeof(request));
962 request.hdr.code = ICM_APPROVE_XDOMAIN;
963 request.route_hi = upper_32_bits(xd->route);
964 request.route_lo = lower_32_bits(xd->route);
965 request.transmit_path = xd->transmit_path;
966 request.transmit_ring = xd->transmit_ring;
967 request.receive_path = xd->receive_path;
968 request.receive_ring = xd->receive_ring;
969 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
970
971 memset(&reply, 0, sizeof(reply));
972 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
973 1, ICM_TIMEOUT);
974 if (ret)
975 return ret;
976
977 if (reply.hdr.flags & ICM_FLAGS_ERROR)
978 return -EIO;
979
980 return 0;
981}
982
983static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
984 int stage)
985{
986 struct icm_tr_pkg_disconnect_xdomain_response reply;
987 struct icm_tr_pkg_disconnect_xdomain request;
988 int ret;
989
990 memset(&request, 0, sizeof(request));
991 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
992 request.stage = stage;
993 request.route_hi = upper_32_bits(xd->route);
994 request.route_lo = lower_32_bits(xd->route);
995 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
996
997 memset(&reply, 0, sizeof(reply));
998 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
999 1, ICM_TIMEOUT);
1000 if (ret)
1001 return ret;
1002
1003 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1004 return -EIO;
1005
1006 return 0;
1007}
1008
1009static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1010{
1011 int ret;
1012
1013 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1014 if (ret)
1015 return ret;
1016
1017 usleep_range(10, 50);
1018 return icm_tr_xdomain_tear_down(tb, xd, 2);
1019}
1020
1021static void
1022icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1023{
1024 const struct icm_tr_event_device_connected *pkg =
1025 (const struct icm_tr_event_device_connected *)hdr;
1026 enum tb_security_level security_level;
1027 struct tb_switch *sw, *parent_sw;
1028 struct tb_xdomain *xd;
1029 bool authorized, boot;
1030 u64 route;
1031
1032 /*
1033 * Currently we don't use the QoS information coming with the
1034 * device connected message so simply just ignore that extra
1035 * packet for now.
1036 */
1037 if (pkg->hdr.packet_id)
1038 return;
1039
1040 /*
1041 * After NVM upgrade adding root switch device fails because we
1042 * initiated reset. During that time ICM might still send device
1043 * connected message which we ignore here.
1044 */
1045 if (!tb->root_switch)
1046 return;
1047
1048 route = get_route(pkg->route_hi, pkg->route_lo);
1049 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1050 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1051 ICM_FLAGS_SLEVEL_SHIFT;
1052 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
1053
1054 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1055 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1056 route);
1057 return;
1058 }
1059
1060 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1061 if (sw) {
1062 /* Update the switch if it is still in the same place */
1063 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1064 parent_sw = tb_to_switch(sw->dev.parent);
1065 update_switch(parent_sw, sw, route, pkg->connection_id,
1066 0, 0, 0, boot);
1067 tb_switch_put(sw);
1068 return;
1069 }
1070
1071 remove_switch(sw);
1072 tb_switch_put(sw);
1073 }
1074
1075 /* Another switch with the same address */
1076 sw = tb_switch_find_by_route(tb, route);
1077 if (sw) {
1078 remove_switch(sw);
1079 tb_switch_put(sw);
1080 }
1081
1082 /* XDomain connection with the same address */
1083 xd = tb_xdomain_find_by_route(tb, route);
1084 if (xd) {
1085 remove_xdomain(xd);
1086 tb_xdomain_put(xd);
1087 }
1088
1089 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1090 if (!parent_sw) {
1091 tb_err(tb, "failed to find parent switch for %llx\n", route);
1092 return;
1093 }
1094
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001095 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
1096 sizeof(pkg->ep_name), pkg->connection_id,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001097 0, 0, 0, security_level, authorized, boot);
1098
1099 tb_switch_put(parent_sw);
1100}
1101
1102static void
1103icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1104{
1105 const struct icm_tr_event_device_disconnected *pkg =
1106 (const struct icm_tr_event_device_disconnected *)hdr;
1107 struct tb_switch *sw;
1108 u64 route;
1109
1110 route = get_route(pkg->route_hi, pkg->route_lo);
1111
1112 sw = tb_switch_find_by_route(tb, route);
1113 if (!sw) {
1114 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1115 return;
1116 }
1117
1118 remove_switch(sw);
1119 tb_switch_put(sw);
1120}
1121
1122static void
1123icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1124{
1125 const struct icm_tr_event_xdomain_connected *pkg =
1126 (const struct icm_tr_event_xdomain_connected *)hdr;
1127 struct tb_xdomain *xd;
1128 struct tb_switch *sw;
1129 u64 route;
1130
1131 if (!tb->root_switch)
1132 return;
1133
1134 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1135
1136 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1137 if (xd) {
1138 if (xd->route == route) {
1139 update_xdomain(xd, route, 0);
1140 tb_xdomain_put(xd);
1141 return;
1142 }
1143
1144 remove_xdomain(xd);
1145 tb_xdomain_put(xd);
1146 }
1147
1148 /* An existing xdomain with the same address */
1149 xd = tb_xdomain_find_by_route(tb, route);
1150 if (xd) {
1151 remove_xdomain(xd);
1152 tb_xdomain_put(xd);
1153 }
1154
1155 /*
1156 * If the user disconnected a switch during suspend and
1157 * connected another host to the same port, remove the switch
1158 * first.
1159 */
1160 sw = get_switch_at_route(tb->root_switch, route);
1161 if (sw)
1162 remove_switch(sw);
1163
1164 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1165 if (!sw) {
1166 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1167 return;
1168 }
1169
1170 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1171 tb_switch_put(sw);
1172}
1173
1174static void
1175icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1176{
1177 const struct icm_tr_event_xdomain_disconnected *pkg =
1178 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1179 struct tb_xdomain *xd;
1180 u64 route;
1181
1182 route = get_route(pkg->route_hi, pkg->route_lo);
1183
1184 xd = tb_xdomain_find_by_route(tb, route);
1185 if (xd) {
1186 remove_xdomain(xd);
1187 tb_xdomain_put(xd);
1188 }
1189}
1190
Mika Westerbergf67cf492017-06-06 15:25:16 +03001191static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1192{
1193 struct pci_dev *parent;
1194
1195 parent = pci_upstream_bridge(pdev);
1196 while (parent) {
1197 if (!pci_is_pcie(parent))
1198 return NULL;
1199 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1200 break;
1201 parent = pci_upstream_bridge(parent);
1202 }
1203
1204 if (!parent)
1205 return NULL;
1206
1207 switch (parent->device) {
1208 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1209 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1210 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1211 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1212 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1213 return parent;
1214 }
1215
1216 return NULL;
1217}
1218
1219static bool icm_ar_is_supported(struct tb *tb)
1220{
1221 struct pci_dev *upstream_port;
1222 struct icm *icm = tb_priv(tb);
1223
1224 /*
1225 * Starting from Alpine Ridge we can use ICM on Apple machines
1226 * as well. We just need to reset and re-enable it first.
1227 */
Lukas Wunner630b3af2017-08-01 14:10:41 +02001228 if (!x86_apple_machine)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001229 return true;
1230
1231 /*
1232 * Find the upstream PCIe port in case we need to do reset
1233 * through its vendor specific registers.
1234 */
1235 upstream_port = get_upstream_port(tb->nhi->pdev);
1236 if (upstream_port) {
1237 int cap;
1238
1239 cap = pci_find_ext_capability(upstream_port,
1240 PCI_EXT_CAP_ID_VNDR);
1241 if (cap > 0) {
1242 icm->upstream_port = upstream_port;
1243 icm->vnd_cap = cap;
1244
1245 return true;
1246 }
1247 }
1248
1249 return false;
1250}
1251
1252static int icm_ar_get_mode(struct tb *tb)
1253{
1254 struct tb_nhi *nhi = tb->nhi;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001255 int retries = 60;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001256 u32 val;
1257
1258 do {
1259 val = ioread32(nhi->iobase + REG_FW_STS);
1260 if (val & REG_FW_STS_NVM_AUTH_DONE)
1261 break;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001262 msleep(50);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001263 } while (--retries);
1264
1265 if (!retries) {
1266 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1267 return -ENODEV;
1268 }
1269
1270 return nhi_mailbox_mode(nhi);
1271}
1272
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001273static int
1274icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001275 size_t *nboot_acl, bool *rpm)
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001276{
1277 struct icm_ar_pkg_driver_ready_response reply;
1278 struct icm_pkg_driver_ready request = {
1279 .hdr.code = ICM_DRIVER_READY,
1280 };
1281 int ret;
1282
1283 memset(&reply, 0, sizeof(reply));
1284 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1285 1, ICM_TIMEOUT);
1286 if (ret)
1287 return ret;
1288
1289 if (security_level)
1290 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1291 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1292 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1293 ICM_AR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001294 if (rpm)
1295 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1296
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001297 return 0;
1298}
1299
Mika Westerbergf67cf492017-06-06 15:25:16 +03001300static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1301{
1302 struct icm_ar_pkg_get_route_response reply;
1303 struct icm_ar_pkg_get_route request = {
1304 .hdr = { .code = ICM_GET_ROUTE },
1305 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1306 };
1307 int ret;
1308
1309 memset(&reply, 0, sizeof(reply));
1310 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1311 1, ICM_TIMEOUT);
1312 if (ret)
1313 return ret;
1314
1315 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1316 return -EIO;
1317
1318 *route = get_route(reply.route_hi, reply.route_lo);
1319 return 0;
1320}
1321
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001322static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1323{
1324 struct icm_ar_pkg_preboot_acl_response reply;
1325 struct icm_ar_pkg_preboot_acl request = {
1326 .hdr = { .code = ICM_PREBOOT_ACL },
1327 };
1328 int ret, i;
1329
1330 memset(&reply, 0, sizeof(reply));
1331 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1332 1, ICM_TIMEOUT);
1333 if (ret)
1334 return ret;
1335
1336 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1337 return -EIO;
1338
1339 for (i = 0; i < nuuids; i++) {
1340 u32 *uuid = (u32 *)&uuids[i];
1341
1342 uuid[0] = reply.acl[i].uuid_lo;
1343 uuid[1] = reply.acl[i].uuid_hi;
1344
1345 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1346 /* Map empty entries to null UUID */
1347 uuid[0] = 0;
1348 uuid[1] = 0;
Mika Westerbergdd010bd2018-05-15 16:04:25 +03001349 } else if (uuid[0] != 0 || uuid[1] != 0) {
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001350 /* Upper two DWs are always one's */
1351 uuid[2] = 0xffffffff;
1352 uuid[3] = 0xffffffff;
1353 }
1354 }
1355
1356 return ret;
1357}
1358
1359static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1360 size_t nuuids)
1361{
1362 struct icm_ar_pkg_preboot_acl_response reply;
1363 struct icm_ar_pkg_preboot_acl request = {
1364 .hdr = {
1365 .code = ICM_PREBOOT_ACL,
1366 .flags = ICM_FLAGS_WRITE,
1367 },
1368 };
1369 int ret, i;
1370
1371 for (i = 0; i < nuuids; i++) {
1372 const u32 *uuid = (const u32 *)&uuids[i];
1373
1374 if (uuid_is_null(&uuids[i])) {
1375 /*
1376 * Map null UUID to the empty (all one) entries
1377 * for ICM.
1378 */
1379 request.acl[i].uuid_lo = 0xffffffff;
1380 request.acl[i].uuid_hi = 0xffffffff;
1381 } else {
1382 /* Two high DWs need to be set to all one */
1383 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1384 return -EINVAL;
1385
1386 request.acl[i].uuid_lo = uuid[0];
1387 request.acl[i].uuid_hi = uuid[1];
1388 }
1389 }
1390
1391 memset(&reply, 0, sizeof(reply));
1392 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1393 1, ICM_TIMEOUT);
1394 if (ret)
1395 return ret;
1396
1397 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1398 return -EIO;
1399
1400 return 0;
1401}
1402
Mika Westerbergf67cf492017-06-06 15:25:16 +03001403static void icm_handle_notification(struct work_struct *work)
1404{
1405 struct icm_notification *n = container_of(work, typeof(*n), work);
1406 struct tb *tb = n->tb;
1407 struct icm *icm = tb_priv(tb);
1408
1409 mutex_lock(&tb->lock);
1410
1411 switch (n->pkg->code) {
1412 case ICM_EVENT_DEVICE_CONNECTED:
1413 icm->device_connected(tb, n->pkg);
1414 break;
1415 case ICM_EVENT_DEVICE_DISCONNECTED:
1416 icm->device_disconnected(tb, n->pkg);
1417 break;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001418 case ICM_EVENT_XDOMAIN_CONNECTED:
1419 icm->xdomain_connected(tb, n->pkg);
1420 break;
1421 case ICM_EVENT_XDOMAIN_DISCONNECTED:
1422 icm->xdomain_disconnected(tb, n->pkg);
1423 break;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001424 }
1425
1426 mutex_unlock(&tb->lock);
1427
1428 kfree(n->pkg);
1429 kfree(n);
1430}
1431
1432static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1433 const void *buf, size_t size)
1434{
1435 struct icm_notification *n;
1436
1437 n = kmalloc(sizeof(*n), GFP_KERNEL);
1438 if (!n)
1439 return;
1440
1441 INIT_WORK(&n->work, icm_handle_notification);
1442 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1443 n->tb = tb;
1444
1445 queue_work(tb->wq, &n->work);
1446}
1447
1448static int
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001449__icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001450 size_t *nboot_acl, bool *rpm)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001451{
Mika Westerberg3080e192017-10-06 18:14:13 +03001452 struct icm *icm = tb_priv(tb);
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001453 unsigned int retries = 50;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001454 int ret;
1455
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001456 ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
Mika Westerberg3080e192017-10-06 18:14:13 +03001457 if (ret) {
1458 tb_err(tb, "failed to send driver ready to ICM\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001459 return ret;
Mika Westerberg3080e192017-10-06 18:14:13 +03001460 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001461
1462 /*
1463 * Hold on here until the switch config space is accessible so
1464 * that we can read root switch config successfully.
1465 */
1466 do {
1467 struct tb_cfg_result res;
1468 u32 tmp;
1469
1470 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1471 0, 1, 100);
1472 if (!res.err)
1473 return 0;
1474
1475 msleep(50);
1476 } while (--retries);
1477
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001478 tb_err(tb, "failed to read root switch config space, giving up\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001479 return -ETIMEDOUT;
1480}
1481
1482static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
1483{
1484 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
1485 u32 cmd;
1486
1487 do {
1488 pci_read_config_dword(icm->upstream_port,
1489 icm->vnd_cap + PCIE2CIO_CMD, &cmd);
1490 if (!(cmd & PCIE2CIO_CMD_START)) {
1491 if (cmd & PCIE2CIO_CMD_TIMEOUT)
1492 break;
1493 return 0;
1494 }
1495
1496 msleep(50);
1497 } while (time_before(jiffies, end));
1498
1499 return -ETIMEDOUT;
1500}
1501
1502static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
1503 unsigned int port, unsigned int index, u32 *data)
1504{
1505 struct pci_dev *pdev = icm->upstream_port;
1506 int ret, vnd_cap = icm->vnd_cap;
1507 u32 cmd;
1508
1509 cmd = index;
1510 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
1511 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
1512 cmd |= PCIE2CIO_CMD_START;
1513 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
1514
1515 ret = pci2cio_wait_completion(icm, 5000);
1516 if (ret)
1517 return ret;
1518
1519 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
1520 return 0;
1521}
1522
1523static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
1524 unsigned int port, unsigned int index, u32 data)
1525{
1526 struct pci_dev *pdev = icm->upstream_port;
1527 int vnd_cap = icm->vnd_cap;
1528 u32 cmd;
1529
1530 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
1531
1532 cmd = index;
1533 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
1534 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
1535 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
1536 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
1537
1538 return pci2cio_wait_completion(icm, 5000);
1539}
1540
1541static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1542{
1543 struct icm *icm = tb_priv(tb);
1544 u32 val;
1545
Mika Westerbergea9d7bb2018-03-09 13:17:01 +03001546 if (!icm->upstream_port)
1547 return -ENODEV;
1548
Mika Westerbergf67cf492017-06-06 15:25:16 +03001549 /* Put ARC to wait for CIO reset event to happen */
1550 val = ioread32(nhi->iobase + REG_FW_STS);
1551 val |= REG_FW_STS_CIO_RESET_REQ;
1552 iowrite32(val, nhi->iobase + REG_FW_STS);
1553
1554 /* Re-start ARC */
1555 val = ioread32(nhi->iobase + REG_FW_STS);
1556 val |= REG_FW_STS_ICM_EN_INVERT;
1557 val |= REG_FW_STS_ICM_EN_CPU;
1558 iowrite32(val, nhi->iobase + REG_FW_STS);
1559
1560 /* Trigger CIO reset now */
1561 return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
1562}
1563
1564static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1565{
1566 unsigned int retries = 10;
1567 int ret;
1568 u32 val;
1569
1570 /* Check if the ICM firmware is already running */
1571 val = ioread32(nhi->iobase + REG_FW_STS);
1572 if (val & REG_FW_STS_ICM_EN)
1573 return 0;
1574
1575 dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
1576
1577 ret = icm_firmware_reset(tb, nhi);
1578 if (ret)
1579 return ret;
1580
1581 /* Wait until the ICM firmware tells us it is up and running */
1582 do {
1583 /* Check that the ICM firmware is running */
1584 val = ioread32(nhi->iobase + REG_FW_STS);
1585 if (val & REG_FW_STS_NVM_AUTH_DONE)
1586 return 0;
1587
1588 msleep(300);
1589 } while (--retries);
1590
1591 return -ETIMEDOUT;
1592}
1593
1594static int icm_reset_phy_port(struct tb *tb, int phy_port)
1595{
1596 struct icm *icm = tb_priv(tb);
1597 u32 state0, state1;
1598 int port0, port1;
1599 u32 val0, val1;
1600 int ret;
1601
1602 if (!icm->upstream_port)
1603 return 0;
1604
1605 if (phy_port) {
1606 port0 = 3;
1607 port1 = 4;
1608 } else {
1609 port0 = 1;
1610 port1 = 2;
1611 }
1612
1613 /*
1614 * Read link status of both null ports belonging to a single
1615 * physical port.
1616 */
1617 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1618 if (ret)
1619 return ret;
1620 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1621 if (ret)
1622 return ret;
1623
1624 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1625 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1626 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1627 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1628
1629 /* If they are both up we need to reset them now */
1630 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1631 return 0;
1632
1633 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1634 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1635 if (ret)
1636 return ret;
1637
1638 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1639 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1640 if (ret)
1641 return ret;
1642
1643 /* Wait a bit and then re-enable both ports */
1644 usleep_range(10, 100);
1645
1646 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1647 if (ret)
1648 return ret;
1649 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1650 if (ret)
1651 return ret;
1652
1653 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1654 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1655 if (ret)
1656 return ret;
1657
1658 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1659 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1660}
1661
1662static int icm_firmware_init(struct tb *tb)
1663{
1664 struct icm *icm = tb_priv(tb);
1665 struct tb_nhi *nhi = tb->nhi;
1666 int ret;
1667
1668 ret = icm_firmware_start(tb, nhi);
1669 if (ret) {
1670 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1671 return ret;
1672 }
1673
1674 if (icm->get_mode) {
1675 ret = icm->get_mode(tb);
1676
1677 switch (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001678 case NHI_FW_SAFE_MODE:
1679 icm->safe_mode = true;
1680 break;
1681
Mika Westerbergf67cf492017-06-06 15:25:16 +03001682 case NHI_FW_CM_MODE:
1683 /* Ask ICM to accept all Thunderbolt devices */
1684 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1685 break;
1686
1687 default:
Mika Westerberge4be8c92017-11-24 17:51:12 +03001688 if (ret < 0)
1689 return ret;
1690
Mika Westerbergf67cf492017-06-06 15:25:16 +03001691 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1692 return -ENODEV;
1693 }
1694 }
1695
1696 /*
1697 * Reset both physical ports if there is anything connected to
1698 * them already.
1699 */
1700 ret = icm_reset_phy_port(tb, 0);
1701 if (ret)
1702 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1703 ret = icm_reset_phy_port(tb, 1);
1704 if (ret)
1705 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1706
1707 return 0;
1708}
1709
1710static int icm_driver_ready(struct tb *tb)
1711{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001712 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001713 int ret;
1714
1715 ret = icm_firmware_init(tb);
1716 if (ret)
1717 return ret;
1718
Mika Westerberge6b245c2017-06-06 15:25:17 +03001719 if (icm->safe_mode) {
1720 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1721 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1722 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1723 return 0;
1724 }
1725
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001726 ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
1727 &icm->rpm);
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001728 if (ret)
1729 return ret;
1730
1731 /*
1732 * Make sure the number of supported preboot ACL matches what we
1733 * expect or disable the whole feature.
1734 */
1735 if (tb->nboot_acl > icm->max_boot_acl)
1736 tb->nboot_acl = 0;
1737
1738 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001739}
1740
1741static int icm_suspend(struct tb *tb)
1742{
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001743 struct icm *icm = tb_priv(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001744
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001745 if (icm->save_devices)
1746 icm->save_devices(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001747
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001748 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001749 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001750}
1751
1752/*
1753 * Mark all switches (except root switch) below this one unplugged. ICM
1754 * firmware will send us an updated list of switches after we have send
1755 * it driver ready command. If a switch is not in that list it will be
1756 * removed when we perform rescan.
1757 */
1758static void icm_unplug_children(struct tb_switch *sw)
1759{
1760 unsigned int i;
1761
1762 if (tb_route(sw))
1763 sw->is_unplugged = true;
1764
1765 for (i = 1; i <= sw->config.max_port_number; i++) {
1766 struct tb_port *port = &sw->ports[i];
1767
1768 if (tb_is_upstream_port(port))
1769 continue;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001770 if (port->xdomain) {
1771 port->xdomain->is_unplugged = true;
1772 continue;
1773 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001774 if (!port->remote)
1775 continue;
1776
1777 icm_unplug_children(port->remote->sw);
1778 }
1779}
1780
1781static void icm_free_unplugged_children(struct tb_switch *sw)
1782{
1783 unsigned int i;
1784
1785 for (i = 1; i <= sw->config.max_port_number; i++) {
1786 struct tb_port *port = &sw->ports[i];
1787
1788 if (tb_is_upstream_port(port))
1789 continue;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001790
1791 if (port->xdomain && port->xdomain->is_unplugged) {
1792 tb_xdomain_remove(port->xdomain);
1793 port->xdomain = NULL;
1794 continue;
1795 }
1796
Mika Westerbergf67cf492017-06-06 15:25:16 +03001797 if (!port->remote)
1798 continue;
1799
1800 if (port->remote->sw->is_unplugged) {
1801 tb_switch_remove(port->remote->sw);
1802 port->remote = NULL;
1803 } else {
1804 icm_free_unplugged_children(port->remote->sw);
1805 }
1806 }
1807}
1808
1809static void icm_rescan_work(struct work_struct *work)
1810{
1811 struct icm *icm = container_of(work, struct icm, rescan_work.work);
1812 struct tb *tb = icm_to_tb(icm);
1813
1814 mutex_lock(&tb->lock);
1815 if (tb->root_switch)
1816 icm_free_unplugged_children(tb->root_switch);
1817 mutex_unlock(&tb->lock);
1818}
1819
1820static void icm_complete(struct tb *tb)
1821{
1822 struct icm *icm = tb_priv(tb);
1823
1824 if (tb->nhi->going_away)
1825 return;
1826
1827 icm_unplug_children(tb->root_switch);
1828
1829 /*
1830 * Now all existing children should be resumed, start events
1831 * from ICM to get updated status.
1832 */
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001833 __icm_driver_ready(tb, NULL, NULL, NULL);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001834
1835 /*
1836 * We do not get notifications of devices that have been
1837 * unplugged during suspend so schedule rescan to clean them up
1838 * if any.
1839 */
1840 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
1841}
1842
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001843static int icm_runtime_suspend(struct tb *tb)
1844{
1845 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1846 return 0;
1847}
1848
1849static int icm_runtime_resume(struct tb *tb)
1850{
1851 /*
1852 * We can reuse the same resume functionality than with system
1853 * suspend.
1854 */
1855 icm_complete(tb);
1856 return 0;
1857}
1858
Mika Westerbergf67cf492017-06-06 15:25:16 +03001859static int icm_start(struct tb *tb)
1860{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001861 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001862 int ret;
1863
Mika Westerberge6b245c2017-06-06 15:25:17 +03001864 if (icm->safe_mode)
1865 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
1866 else
1867 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001868 if (!tb->root_switch)
1869 return -ENODEV;
1870
Mika Westerberge6b245c2017-06-06 15:25:17 +03001871 /*
1872 * NVM upgrade has not been tested on Apple systems and they
1873 * don't provide images publicly either. To be on the safe side
1874 * prevent root switch NVM upgrade on Macs for now.
1875 */
Lukas Wunner630b3af2017-08-01 14:10:41 +02001876 tb->root_switch->no_nvm_upgrade = x86_apple_machine;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001877 tb->root_switch->rpm = icm->rpm;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001878
Mika Westerbergf67cf492017-06-06 15:25:16 +03001879 ret = tb_switch_add(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001880 if (ret) {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001881 tb_switch_put(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001882 tb->root_switch = NULL;
1883 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001884
1885 return ret;
1886}
1887
1888static void icm_stop(struct tb *tb)
1889{
1890 struct icm *icm = tb_priv(tb);
1891
1892 cancel_delayed_work(&icm->rescan_work);
1893 tb_switch_remove(tb->root_switch);
1894 tb->root_switch = NULL;
1895 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1896}
1897
Mika Westerberge6b245c2017-06-06 15:25:17 +03001898static int icm_disconnect_pcie_paths(struct tb *tb)
1899{
1900 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
1901}
1902
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001903/* Falcon Ridge */
Mika Westerbergf67cf492017-06-06 15:25:16 +03001904static const struct tb_cm_ops icm_fr_ops = {
1905 .driver_ready = icm_driver_ready,
1906 .start = icm_start,
1907 .stop = icm_stop,
1908 .suspend = icm_suspend,
1909 .complete = icm_complete,
1910 .handle_event = icm_handle_event,
1911 .approve_switch = icm_fr_approve_switch,
1912 .add_switch_key = icm_fr_add_switch_key,
1913 .challenge_switch_key = icm_fr_challenge_switch_key,
Mika Westerberge6b245c2017-06-06 15:25:17 +03001914 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001915 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1916 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
Mika Westerbergf67cf492017-06-06 15:25:16 +03001917};
1918
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001919/* Alpine Ridge */
1920static const struct tb_cm_ops icm_ar_ops = {
1921 .driver_ready = icm_driver_ready,
1922 .start = icm_start,
1923 .stop = icm_stop,
1924 .suspend = icm_suspend,
1925 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001926 .runtime_suspend = icm_runtime_suspend,
1927 .runtime_resume = icm_runtime_resume,
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001928 .handle_event = icm_handle_event,
1929 .get_boot_acl = icm_ar_get_boot_acl,
1930 .set_boot_acl = icm_ar_set_boot_acl,
1931 .approve_switch = icm_fr_approve_switch,
1932 .add_switch_key = icm_fr_add_switch_key,
1933 .challenge_switch_key = icm_fr_challenge_switch_key,
1934 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1935 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1936 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1937};
1938
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001939/* Titan Ridge */
1940static const struct tb_cm_ops icm_tr_ops = {
1941 .driver_ready = icm_driver_ready,
1942 .start = icm_start,
1943 .stop = icm_stop,
1944 .suspend = icm_suspend,
1945 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001946 .runtime_suspend = icm_runtime_suspend,
1947 .runtime_resume = icm_runtime_resume,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001948 .handle_event = icm_handle_event,
1949 .get_boot_acl = icm_ar_get_boot_acl,
1950 .set_boot_acl = icm_ar_set_boot_acl,
1951 .approve_switch = icm_tr_approve_switch,
1952 .add_switch_key = icm_tr_add_switch_key,
1953 .challenge_switch_key = icm_tr_challenge_switch_key,
1954 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1955 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
1956 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
1957};
1958
Mika Westerbergf67cf492017-06-06 15:25:16 +03001959struct tb *icm_probe(struct tb_nhi *nhi)
1960{
1961 struct icm *icm;
1962 struct tb *tb;
1963
1964 tb = tb_domain_alloc(nhi, sizeof(struct icm));
1965 if (!tb)
1966 return NULL;
1967
1968 icm = tb_priv(tb);
1969 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
1970 mutex_init(&icm->request_lock);
1971
1972 switch (nhi->pdev->device) {
1973 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1974 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1975 icm->is_supported = icm_fr_is_supported;
1976 icm->get_route = icm_fr_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001977 icm->save_devices = icm_fr_save_devices;
Mika Westerberg3080e192017-10-06 18:14:13 +03001978 icm->driver_ready = icm_fr_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001979 icm->device_connected = icm_fr_device_connected;
1980 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001981 icm->xdomain_connected = icm_fr_xdomain_connected;
1982 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001983 tb->cm_ops = &icm_fr_ops;
1984 break;
1985
1986 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
1987 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
1988 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
1989 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
1990 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001991 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001992 icm->is_supported = icm_ar_is_supported;
1993 icm->get_mode = icm_ar_get_mode;
1994 icm->get_route = icm_ar_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001995 icm->save_devices = icm_fr_save_devices;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001996 icm->driver_ready = icm_ar_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001997 icm->device_connected = icm_fr_device_connected;
1998 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001999 icm->xdomain_connected = icm_fr_xdomain_connected;
2000 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002001 tb->cm_ops = &icm_ar_ops;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002002 break;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002003
2004 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2005 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2006 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2007 icm->is_supported = icm_ar_is_supported;
2008 icm->get_mode = icm_ar_get_mode;
2009 icm->driver_ready = icm_tr_driver_ready;
2010 icm->device_connected = icm_tr_device_connected;
2011 icm->device_disconnected = icm_tr_device_disconnected;
2012 icm->xdomain_connected = icm_tr_xdomain_connected;
2013 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2014 tb->cm_ops = &icm_tr_ops;
2015 break;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002016 }
2017
2018 if (!icm->is_supported || !icm->is_supported(tb)) {
2019 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
2020 tb_domain_put(tb);
2021 return NULL;
2022 }
2023
2024 return tb;
2025}