blob: 6255f1ef95994cedc65455e7732583f87504b5b2 [file] [log] [blame]
Mika Westerbergfd3b3392018-10-01 12:31:21 +03001// SPDX-License-Identifier: GPL-2.0
Mika Westerbergf67cf492017-06-06 15:25:16 +03002/*
3 * Internal Thunderbolt Connection Manager. This is a firmware running on
4 * the Thunderbolt host controller performing most of the low-level
5 * handling.
6 *
7 * Copyright (C) 2017, Intel Corporation
8 * Authors: Michael Jamet <michael.jamet@intel.com>
9 * Mika Westerberg <mika.westerberg@linux.intel.com>
Mika Westerbergf67cf492017-06-06 15:25:16 +030010 */
11
12#include <linux/delay.h>
Mika Westerbergf67cf492017-06-06 15:25:16 +030013#include <linux/mutex.h>
Mika Westerberg354a7a72019-03-21 15:31:14 +020014#include <linux/moduleparam.h>
Mika Westerbergf67cf492017-06-06 15:25:16 +030015#include <linux/pci.h>
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030016#include <linux/pm_runtime.h>
Lukas Wunner630b3af2017-08-01 14:10:41 +020017#include <linux/platform_data/x86/apple.h>
Mika Westerbergf67cf492017-06-06 15:25:16 +030018#include <linux/sizes.h>
19#include <linux/slab.h>
20#include <linux/workqueue.h>
21
22#include "ctl.h"
23#include "nhi_regs.h"
24#include "tb.h"
25
26#define PCIE2CIO_CMD 0x30
27#define PCIE2CIO_CMD_TIMEOUT BIT(31)
28#define PCIE2CIO_CMD_START BIT(30)
29#define PCIE2CIO_CMD_WRITE BIT(21)
30#define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
31#define PCIE2CIO_CMD_CS_SHIFT 19
32#define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
33#define PCIE2CIO_CMD_PORT_SHIFT 13
34
35#define PCIE2CIO_WRDATA 0x34
36#define PCIE2CIO_RDDATA 0x38
37
38#define PHY_PORT_CS1 0x37
39#define PHY_PORT_CS1_LINK_DISABLE BIT(14)
40#define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
41#define PHY_PORT_CS1_LINK_STATE_SHIFT 26
42
Mika Westerberg0b0a0bd2017-10-07 10:54:09 +030043#define ICM_TIMEOUT 5000 /* ms */
44#define ICM_APPROVE_TIMEOUT 10000 /* ms */
Mika Westerbergf67cf492017-06-06 15:25:16 +030045#define ICM_MAX_LINK 4
Mika Westerbergf67cf492017-06-06 15:25:16 +030046
Mika Westerberg354a7a72019-03-21 15:31:14 +020047static bool start_icm;
48module_param(start_icm, bool, 0444);
49MODULE_PARM_DESC(start_icm, "start ICM firmware if it is not running (default: false)");
50
Mika Westerbergf67cf492017-06-06 15:25:16 +030051/**
Mika Westerberg90393872020-11-03 13:55:32 +020052 * struct usb4_switch_nvm_auth - Holds USB4 NVM_AUTH status
53 * @reply: Reply from ICM firmware is placed here
54 * @request: Request that is sent to ICM firmware
55 * @icm: Pointer to ICM private data
56 */
57struct usb4_switch_nvm_auth {
58 struct icm_usb4_switch_op_response reply;
59 struct icm_usb4_switch_op request;
60 struct icm *icm;
61};
62
63/**
Mika Westerbergf67cf492017-06-06 15:25:16 +030064 * struct icm - Internal connection manager private data
65 * @request_lock: Makes sure only one message is send to ICM at time
66 * @rescan_work: Work used to rescan the surviving switches after resume
67 * @upstream_port: Pointer to the PCIe upstream port this host
68 * controller is connected. This is only set for systems
69 * where ICM needs to be started manually
70 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
71 * (only set when @upstream_port is not %NULL)
Mika Westerberge6b245c2017-06-06 15:25:17 +030072 * @safe_mode: ICM is in safe mode
Mika Westerberg9aaa3b82018-01-21 12:08:04 +020073 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030074 * @rpm: Does the controller support runtime PM (RTD3)
Mika Westerbergf437c242018-01-16 22:30:40 +020075 * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
Mika Westerberg90393872020-11-03 13:55:32 +020076 * @proto_version: Firmware protocol version
77 * @last_nvm_auth: Last USB4 router NVM_AUTH result (or %NULL if not set)
Mika Westerberg3cdb9442018-01-16 22:19:00 +020078 * @veto: Is RTD3 veto in effect
Mika Westerbergf67cf492017-06-06 15:25:16 +030079 * @is_supported: Checks if we can support ICM on this controller
Mika Westerberg0d538272019-05-31 13:31:54 +030080 * @cio_reset: Trigger CIO reset
Mika Westerbergf67cf492017-06-06 15:25:16 +030081 * @get_mode: Read and return the ICM firmware mode (optional)
82 * @get_route: Find a route string for given switch
Mika Westerbergd04522fa2018-07-25 11:03:19 +030083 * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
Mika Westerberg3080e192017-10-06 18:14:13 +030084 * @driver_ready: Send driver ready message to ICM
Mika Westerberg3cdb9442018-01-16 22:19:00 +020085 * @set_uuid: Set UUID for the root switch (optional)
Mika Westerbergf67cf492017-06-06 15:25:16 +030086 * @device_connected: Handle device connected ICM message
87 * @device_disconnected: Handle device disconnected ICM message
Lee Jonesaf6bd592021-01-27 11:25:52 +000088 * @xdomain_connected: Handle XDomain connected ICM message
89 * @xdomain_disconnected: Handle XDomain disconnected ICM message
Mika Westerberg3cdb9442018-01-16 22:19:00 +020090 * @rtd3_veto: Handle RTD3 veto notification ICM message
Mika Westerbergf67cf492017-06-06 15:25:16 +030091 */
92struct icm {
93 struct mutex request_lock;
94 struct delayed_work rescan_work;
95 struct pci_dev *upstream_port;
96 int vnd_cap;
Mika Westerberge6b245c2017-06-06 15:25:17 +030097 bool safe_mode;
Mika Westerberg45ef5612020-11-03 13:59:31 +020098 size_t max_boot_acl;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030099 bool rpm;
Mika Westerbergf437c242018-01-16 22:30:40 +0200100 bool can_upgrade_nvm;
Mika Westerberg90393872020-11-03 13:55:32 +0200101 u8 proto_version;
102 struct usb4_switch_nvm_auth *last_nvm_auth;
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200103 bool veto;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300104 bool (*is_supported)(struct tb *tb);
Mika Westerberg0d538272019-05-31 13:31:54 +0300105 int (*cio_reset)(struct tb *tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300106 int (*get_mode)(struct tb *tb);
107 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
Mika Westerbergd04522fa2018-07-25 11:03:19 +0300108 void (*save_devices)(struct tb *tb);
Mika Westerberg3080e192017-10-06 18:14:13 +0300109 int (*driver_ready)(struct tb *tb,
Mika Westerberg9aaa3b82018-01-21 12:08:04 +0200110 enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +0200111 u8 *proto_version, size_t *nboot_acl, bool *rpm);
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200112 void (*set_uuid)(struct tb *tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300113 void (*device_connected)(struct tb *tb,
114 const struct icm_pkg_header *hdr);
115 void (*device_disconnected)(struct tb *tb,
116 const struct icm_pkg_header *hdr);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300117 void (*xdomain_connected)(struct tb *tb,
118 const struct icm_pkg_header *hdr);
119 void (*xdomain_disconnected)(struct tb *tb,
120 const struct icm_pkg_header *hdr);
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200121 void (*rtd3_veto)(struct tb *tb, const struct icm_pkg_header *hdr);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300122};
123
124struct icm_notification {
125 struct work_struct work;
126 struct icm_pkg_header *pkg;
127 struct tb *tb;
128};
129
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300130struct ep_name_entry {
131 u8 len;
132 u8 type;
Gustavo A. R. Silva3084ea92020-02-11 17:20:09 -0600133 u8 data[];
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300134};
135
136#define EP_NAME_INTEL_VSS 0x10
137
138/* Intel Vendor specific structure */
139struct intel_vss {
140 u16 vendor;
141 u16 model;
142 u8 mc;
143 u8 flags;
144 u16 pci_devid;
145 u32 nvm_version;
146};
147
148#define INTEL_VSS_FLAGS_RTD3 BIT(0)
149
150static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size)
151{
152 const void *end = ep_name + size;
153
154 while (ep_name < end) {
155 const struct ep_name_entry *ep = ep_name;
156
157 if (!ep->len)
158 break;
159 if (ep_name + ep->len > end)
160 break;
161
162 if (ep->type == EP_NAME_INTEL_VSS)
163 return (const struct intel_vss *)ep->data;
164
165 ep_name += ep->len;
166 }
167
168 return NULL;
169}
170
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300171static bool intel_vss_is_rtd3(const void *ep_name, size_t size)
172{
173 const struct intel_vss *vss;
174
175 vss = parse_intel_vss(ep_name, size);
176 if (vss)
177 return !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
178
179 return false;
180}
181
Mika Westerbergf67cf492017-06-06 15:25:16 +0300182static inline struct tb *icm_to_tb(struct icm *icm)
183{
184 return ((void *)icm - sizeof(struct tb));
185}
186
187static inline u8 phy_port_from_route(u64 route, u8 depth)
188{
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300189 u8 link;
190
191 link = depth ? route >> ((depth - 1) * 8) : route;
192 return tb_phy_port_from_link(link);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300193}
194
195static inline u8 dual_link_from_link(u8 link)
196{
197 return link ? ((link - 1) ^ 0x01) + 1 : 0;
198}
199
200static inline u64 get_route(u32 route_hi, u32 route_lo)
201{
202 return (u64)route_hi << 32 | route_lo;
203}
204
Radion Mirchevsky4bac4712017-10-04 16:43:43 +0300205static inline u64 get_parent_route(u64 route)
206{
207 int depth = tb_route_length(route);
208 return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
209}
210
Mika Westerberg0d538272019-05-31 13:31:54 +0300211static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
212{
213 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
214 u32 cmd;
215
216 do {
217 pci_read_config_dword(icm->upstream_port,
218 icm->vnd_cap + PCIE2CIO_CMD, &cmd);
219 if (!(cmd & PCIE2CIO_CMD_START)) {
220 if (cmd & PCIE2CIO_CMD_TIMEOUT)
221 break;
222 return 0;
223 }
224
225 msleep(50);
226 } while (time_before(jiffies, end));
227
228 return -ETIMEDOUT;
229}
230
231static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
232 unsigned int port, unsigned int index, u32 *data)
233{
234 struct pci_dev *pdev = icm->upstream_port;
235 int ret, vnd_cap = icm->vnd_cap;
236 u32 cmd;
237
238 cmd = index;
239 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
240 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
241 cmd |= PCIE2CIO_CMD_START;
242 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
243
244 ret = pci2cio_wait_completion(icm, 5000);
245 if (ret)
246 return ret;
247
248 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
249 return 0;
250}
251
252static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
253 unsigned int port, unsigned int index, u32 data)
254{
255 struct pci_dev *pdev = icm->upstream_port;
256 int vnd_cap = icm->vnd_cap;
257 u32 cmd;
258
259 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
260
261 cmd = index;
262 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
263 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
264 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
265 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
266
267 return pci2cio_wait_completion(icm, 5000);
268}
269
Mika Westerbergf67cf492017-06-06 15:25:16 +0300270static bool icm_match(const struct tb_cfg_request *req,
271 const struct ctl_pkg *pkg)
272{
273 const struct icm_pkg_header *res_hdr = pkg->buffer;
274 const struct icm_pkg_header *req_hdr = req->request;
275
276 if (pkg->frame.eof != req->response_type)
277 return false;
278 if (res_hdr->code != req_hdr->code)
279 return false;
280
281 return true;
282}
283
284static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
285{
286 const struct icm_pkg_header *hdr = pkg->buffer;
287
288 if (hdr->packet_id < req->npackets) {
289 size_t offset = hdr->packet_id * req->response_size;
290
291 memcpy(req->response + offset, pkg->buffer, req->response_size);
292 }
293
294 return hdr->packet_id == hdr->total_packets - 1;
295}
296
297static int icm_request(struct tb *tb, const void *request, size_t request_size,
298 void *response, size_t response_size, size_t npackets,
299 unsigned int timeout_msec)
300{
301 struct icm *icm = tb_priv(tb);
302 int retries = 3;
303
304 do {
305 struct tb_cfg_request *req;
306 struct tb_cfg_result res;
307
308 req = tb_cfg_request_alloc();
309 if (!req)
310 return -ENOMEM;
311
312 req->match = icm_match;
313 req->copy = icm_copy;
314 req->request = request;
315 req->request_size = request_size;
316 req->request_type = TB_CFG_PKG_ICM_CMD;
317 req->response = response;
318 req->npackets = npackets;
319 req->response_size = response_size;
320 req->response_type = TB_CFG_PKG_ICM_RESP;
321
322 mutex_lock(&icm->request_lock);
323 res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
324 mutex_unlock(&icm->request_lock);
325
326 tb_cfg_request_put(req);
327
328 if (res.err != -ETIMEDOUT)
329 return res.err == 1 ? -EIO : res.err;
330
331 usleep_range(20, 50);
332 } while (retries--);
333
334 return -ETIMEDOUT;
335}
336
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200337/*
338 * If rescan is queued to run (we are resuming), postpone it to give the
339 * firmware some more time to send device connected notifications for next
340 * devices in the chain.
341 */
342static void icm_postpone_rescan(struct tb *tb)
343{
344 struct icm *icm = tb_priv(tb);
345
346 if (delayed_work_pending(&icm->rescan_work))
347 mod_delayed_work(tb->wq, &icm->rescan_work,
348 msecs_to_jiffies(500));
349}
350
351static void icm_veto_begin(struct tb *tb)
352{
353 struct icm *icm = tb_priv(tb);
354
355 if (!icm->veto) {
356 icm->veto = true;
357 /* Keep the domain powered while veto is in effect */
358 pm_runtime_get(&tb->dev);
359 }
360}
361
362static void icm_veto_end(struct tb *tb)
363{
364 struct icm *icm = tb_priv(tb);
365
366 if (icm->veto) {
367 icm->veto = false;
368 /* Allow the domain suspend now */
369 pm_runtime_mark_last_busy(&tb->dev);
370 pm_runtime_put_autosuspend(&tb->dev);
371 }
372}
373
Mika Westerberg354a7a72019-03-21 15:31:14 +0200374static bool icm_firmware_running(const struct tb_nhi *nhi)
375{
376 u32 val;
377
378 val = ioread32(nhi->iobase + REG_FW_STS);
379 return !!(val & REG_FW_STS_ICM_EN);
380}
381
Mika Westerbergf67cf492017-06-06 15:25:16 +0300382static bool icm_fr_is_supported(struct tb *tb)
383{
Lukas Wunner630b3af2017-08-01 14:10:41 +0200384 return !x86_apple_machine;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300385}
386
387static inline int icm_fr_get_switch_index(u32 port)
388{
389 int index;
390
391 if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
392 return 0;
393
394 index = port >> ICM_PORT_INDEX_SHIFT;
395 return index != 0xff ? index : 0;
396}
397
398static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
399{
400 struct icm_fr_pkg_get_topology_response *switches, *sw;
401 struct icm_fr_pkg_get_topology request = {
402 .hdr = { .code = ICM_GET_TOPOLOGY },
403 };
404 size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
405 int ret, index;
406 u8 i;
407
408 switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
409 if (!switches)
410 return -ENOMEM;
411
412 ret = icm_request(tb, &request, sizeof(request), switches,
413 sizeof(*switches), npackets, ICM_TIMEOUT);
414 if (ret)
415 goto err_free;
416
417 sw = &switches[0];
418 index = icm_fr_get_switch_index(sw->ports[link]);
419 if (!index) {
420 ret = -ENODEV;
421 goto err_free;
422 }
423
424 sw = &switches[index];
425 for (i = 1; i < depth; i++) {
426 unsigned int j;
427
428 if (!(sw->first_data & ICM_SWITCH_USED)) {
429 ret = -ENODEV;
430 goto err_free;
431 }
432
433 for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
434 index = icm_fr_get_switch_index(sw->ports[j]);
435 if (index > sw->switch_index) {
436 sw = &switches[index];
437 break;
438 }
439 }
440 }
441
442 *route = get_route(sw->route_hi, sw->route_lo);
443
444err_free:
445 kfree(switches);
446 return ret;
447}
448
Mika Westerbergd04522fa2018-07-25 11:03:19 +0300449static void icm_fr_save_devices(struct tb *tb)
450{
451 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
452}
453
Mika Westerberg3080e192017-10-06 18:14:13 +0300454static int
Mika Westerberg9aaa3b82018-01-21 12:08:04 +0200455icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +0200456 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerberg3080e192017-10-06 18:14:13 +0300457{
458 struct icm_fr_pkg_driver_ready_response reply;
459 struct icm_pkg_driver_ready request = {
460 .hdr.code = ICM_DRIVER_READY,
461 };
462 int ret;
463
464 memset(&reply, 0, sizeof(reply));
465 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
466 1, ICM_TIMEOUT);
467 if (ret)
468 return ret;
469
470 if (security_level)
471 *security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
472
473 return 0;
474}
475
Mika Westerbergf67cf492017-06-06 15:25:16 +0300476static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
477{
478 struct icm_fr_pkg_approve_device request;
479 struct icm_fr_pkg_approve_device reply;
480 int ret;
481
482 memset(&request, 0, sizeof(request));
483 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
484 request.hdr.code = ICM_APPROVE_DEVICE;
485 request.connection_id = sw->connection_id;
486 request.connection_key = sw->connection_key;
487
488 memset(&reply, 0, sizeof(reply));
489 /* Use larger timeout as establishing tunnels can take some time */
490 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
Mika Westerberg0b0a0bd2017-10-07 10:54:09 +0300491 1, ICM_APPROVE_TIMEOUT);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300492 if (ret)
493 return ret;
494
495 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
496 tb_warn(tb, "PCIe tunnel creation failed\n");
497 return -EIO;
498 }
499
500 return 0;
501}
502
503static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
504{
505 struct icm_fr_pkg_add_device_key request;
506 struct icm_fr_pkg_add_device_key_response reply;
507 int ret;
508
509 memset(&request, 0, sizeof(request));
510 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
511 request.hdr.code = ICM_ADD_DEVICE_KEY;
512 request.connection_id = sw->connection_id;
513 request.connection_key = sw->connection_key;
514 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
515
516 memset(&reply, 0, sizeof(reply));
517 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
518 1, ICM_TIMEOUT);
519 if (ret)
520 return ret;
521
522 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
523 tb_warn(tb, "Adding key to switch failed\n");
524 return -EIO;
525 }
526
527 return 0;
528}
529
530static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
531 const u8 *challenge, u8 *response)
532{
533 struct icm_fr_pkg_challenge_device request;
534 struct icm_fr_pkg_challenge_device_response reply;
535 int ret;
536
537 memset(&request, 0, sizeof(request));
538 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
539 request.hdr.code = ICM_CHALLENGE_DEVICE;
540 request.connection_id = sw->connection_id;
541 request.connection_key = sw->connection_key;
542 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
543
544 memset(&reply, 0, sizeof(reply));
545 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
546 1, ICM_TIMEOUT);
547 if (ret)
548 return ret;
549
550 if (reply.hdr.flags & ICM_FLAGS_ERROR)
551 return -EKEYREJECTED;
552 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
553 return -ENOKEY;
554
555 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
556
557 return 0;
558}
559
Mika Westerberg180b0682021-01-08 16:25:39 +0200560static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
561 int transmit_path, int transmit_ring,
562 int receive_path, int receive_ring)
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300563{
564 struct icm_fr_pkg_approve_xdomain_response reply;
565 struct icm_fr_pkg_approve_xdomain request;
566 int ret;
567
568 memset(&request, 0, sizeof(request));
569 request.hdr.code = ICM_APPROVE_XDOMAIN;
570 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
571 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
572
Mika Westerberg180b0682021-01-08 16:25:39 +0200573 request.transmit_path = transmit_path;
574 request.transmit_ring = transmit_ring;
575 request.receive_path = receive_path;
576 request.receive_ring = receive_ring;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300577
578 memset(&reply, 0, sizeof(reply));
579 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
580 1, ICM_TIMEOUT);
581 if (ret)
582 return ret;
583
584 if (reply.hdr.flags & ICM_FLAGS_ERROR)
585 return -EIO;
586
587 return 0;
588}
589
Mika Westerberg180b0682021-01-08 16:25:39 +0200590static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
591 int transmit_path, int transmit_ring,
592 int receive_path, int receive_ring)
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300593{
594 u8 phy_port;
595 u8 cmd;
596
597 phy_port = tb_phy_port_from_link(xd->link);
598 if (phy_port == 0)
599 cmd = NHI_MAILBOX_DISCONNECT_PA;
600 else
601 cmd = NHI_MAILBOX_DISCONNECT_PB;
602
603 nhi_mailbox_cmd(tb->nhi, cmd, 1);
604 usleep_range(10, 50);
605 nhi_mailbox_cmd(tb->nhi, cmd, 2);
606 return 0;
607}
608
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300609static struct tb_switch *alloc_switch(struct tb_switch *parent_sw, u64 route,
610 const uuid_t *uuid)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300611{
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300612 struct tb *tb = parent_sw->tb;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300613 struct tb_switch *sw;
614
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300615 sw = tb_switch_alloc(tb, &parent_sw->dev, route);
616 if (IS_ERR(sw)) {
617 tb_warn(tb, "failed to allocate switch at %llx\n", route);
618 return sw;
619 }
Mika Westerbergee487dd2017-10-04 14:42:20 +0300620
621 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
Aditya Pakkifd21b792019-03-25 16:25:22 -0500622 if (!sw->uuid) {
Aditya Pakkifd21b792019-03-25 16:25:22 -0500623 tb_switch_put(sw);
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300624 return ERR_PTR(-ENOMEM);
Aditya Pakkifd21b792019-03-25 16:25:22 -0500625 }
Mika Westerbergee487dd2017-10-04 14:42:20 +0300626
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300627 init_completion(&sw->rpm_complete);
628 return sw;
629}
630
631static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw)
632{
633 u64 route = tb_route(sw);
634 int ret;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300635
Mika Westerbergee487dd2017-10-04 14:42:20 +0300636 /* Link the two switches now */
637 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
638 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
639
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200640 ret = tb_switch_add(sw);
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300641 if (ret)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300642 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300643
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300644 return ret;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300645}
646
647static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
648 u64 route, u8 connection_id, u8 connection_key,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200649 u8 link, u8 depth, bool boot)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300650{
651 /* Disconnect from parent */
652 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
653 /* Re-connect via updated port*/
654 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
655
656 /* Update with the new addressing information */
657 sw->config.route_hi = upper_32_bits(route);
658 sw->config.route_lo = lower_32_bits(route);
659 sw->connection_id = connection_id;
660 sw->connection_key = connection_key;
661 sw->link = link;
662 sw->depth = depth;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200663 sw->boot = boot;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300664
665 /* This switch still exists */
666 sw->is_unplugged = false;
Mika Westerberg4f7c2e02019-05-28 18:56:20 +0300667
668 /* Runtime resume is now complete */
669 complete(&sw->rpm_complete);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300670}
671
Mika Westerbergf67cf492017-06-06 15:25:16 +0300672static void remove_switch(struct tb_switch *sw)
673{
674 struct tb_switch *parent_sw;
675
676 parent_sw = tb_to_switch(sw->dev.parent);
677 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
678 tb_switch_remove(sw);
679}
680
Mika Westerbergee487dd2017-10-04 14:42:20 +0300681static void add_xdomain(struct tb_switch *sw, u64 route,
682 const uuid_t *local_uuid, const uuid_t *remote_uuid,
683 u8 link, u8 depth)
684{
685 struct tb_xdomain *xd;
686
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300687 pm_runtime_get_sync(&sw->dev);
688
Mika Westerbergee487dd2017-10-04 14:42:20 +0300689 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
690 if (!xd)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300691 goto out;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300692
693 xd->link = link;
694 xd->depth = depth;
695
696 tb_port_at(route, sw)->xdomain = xd;
697
698 tb_xdomain_add(xd);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300699
700out:
701 pm_runtime_mark_last_busy(&sw->dev);
702 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300703}
704
705static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
706{
707 xd->link = link;
708 xd->route = route;
709 xd->is_unplugged = false;
710}
711
Mika Westerberg79fae982018-02-09 17:29:38 +0300712static void remove_xdomain(struct tb_xdomain *xd)
713{
714 struct tb_switch *sw;
715
716 sw = tb_to_switch(xd->dev.parent);
717 tb_port_at(xd->route, sw)->xdomain = NULL;
718 tb_xdomain_remove(xd);
719}
720
Mika Westerbergf67cf492017-06-06 15:25:16 +0300721static void
722icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
723{
724 const struct icm_fr_event_device_connected *pkg =
725 (const struct icm_fr_event_device_connected *)hdr;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300726 enum tb_security_level security_level;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300727 struct tb_switch *sw, *parent_sw;
Mika Westerberg91c0c122019-03-21 19:03:00 +0200728 bool boot, dual_lane, speed_gen3;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300729 struct icm *icm = tb_priv(tb);
730 bool authorized = false;
Mika Westerberg79fae982018-02-09 17:29:38 +0300731 struct tb_xdomain *xd;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300732 u8 link, depth;
733 u64 route;
734 int ret;
735
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200736 icm_postpone_rescan(tb);
737
Mika Westerbergf67cf492017-06-06 15:25:16 +0300738 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
739 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
740 ICM_LINK_INFO_DEPTH_SHIFT;
741 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300742 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
743 ICM_FLAGS_SLEVEL_SHIFT;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200744 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
Mika Westerberg91c0c122019-03-21 19:03:00 +0200745 dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
746 speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300747
Mika Westerbergcb653ee2017-09-14 13:59:10 +0300748 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
749 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
750 link, depth);
751 return;
752 }
753
Mika Westerbergf67cf492017-06-06 15:25:16 +0300754 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
755 if (sw) {
756 u8 phy_port, sw_phy_port;
757
758 parent_sw = tb_to_switch(sw->dev.parent);
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300759 sw_phy_port = tb_phy_port_from_link(sw->link);
760 phy_port = tb_phy_port_from_link(link);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300761
762 /*
763 * On resume ICM will send us connected events for the
764 * devices that still are present. However, that
765 * information might have changed for example by the
766 * fact that a switch on a dual-link connection might
767 * have been enumerated using the other link now. Make
768 * sure our book keeping matches that.
769 */
770 if (sw->depth == depth && sw_phy_port == phy_port &&
771 !!sw->authorized == authorized) {
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300772 /*
773 * It was enumerated through another link so update
774 * route string accordingly.
775 */
776 if (sw->link != link) {
777 ret = icm->get_route(tb, link, depth, &route);
778 if (ret) {
779 tb_err(tb, "failed to update route string for switch at %u.%u\n",
780 link, depth);
781 tb_switch_put(sw);
782 return;
783 }
784 } else {
785 route = tb_route(sw);
786 }
787
Mika Westerbergee487dd2017-10-04 14:42:20 +0300788 update_switch(parent_sw, sw, route, pkg->connection_id,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200789 pkg->connection_key, link, depth, boot);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300790 tb_switch_put(sw);
791 return;
792 }
793
794 /*
795 * User connected the same switch to another physical
796 * port or to another part of the topology. Remove the
797 * existing switch now before adding the new one.
798 */
799 remove_switch(sw);
800 tb_switch_put(sw);
801 }
802
803 /*
804 * If the switch was not found by UUID, look for a switch on
805 * same physical port (taking possible link aggregation into
806 * account) and depth. If we found one it is definitely a stale
807 * one so remove it first.
808 */
809 sw = tb_switch_find_by_link_depth(tb, link, depth);
810 if (!sw) {
811 u8 dual_link;
812
813 dual_link = dual_link_from_link(link);
814 if (dual_link)
815 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
816 }
817 if (sw) {
818 remove_switch(sw);
819 tb_switch_put(sw);
820 }
821
Mika Westerberg79fae982018-02-09 17:29:38 +0300822 /* Remove existing XDomain connection if found */
823 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
824 if (xd) {
825 remove_xdomain(xd);
826 tb_xdomain_put(xd);
827 }
828
Mika Westerbergf67cf492017-06-06 15:25:16 +0300829 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
830 if (!parent_sw) {
831 tb_err(tb, "failed to find parent switch for %u.%u\n",
832 link, depth);
833 return;
834 }
835
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300836 ret = icm->get_route(tb, link, depth, &route);
837 if (ret) {
838 tb_err(tb, "failed to find route string for switch at %u.%u\n",
839 link, depth);
840 tb_switch_put(parent_sw);
841 return;
842 }
843
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300844 pm_runtime_get_sync(&parent_sw->dev);
845
846 sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
847 if (!IS_ERR(sw)) {
848 sw->connection_id = pkg->connection_id;
849 sw->connection_key = pkg->connection_key;
850 sw->link = link;
851 sw->depth = depth;
852 sw->authorized = authorized;
853 sw->security_level = security_level;
854 sw->boot = boot;
Mika Westerberg91c0c122019-03-21 19:03:00 +0200855 sw->link_speed = speed_gen3 ? 20 : 10;
856 sw->link_width = dual_lane ? 2 : 1;
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300857 sw->rpm = intel_vss_is_rtd3(pkg->ep_name, sizeof(pkg->ep_name));
858
859 if (add_switch(parent_sw, sw))
860 tb_switch_put(sw);
861 }
862
863 pm_runtime_mark_last_busy(&parent_sw->dev);
864 pm_runtime_put_autosuspend(&parent_sw->dev);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300865
Mika Westerbergf67cf492017-06-06 15:25:16 +0300866 tb_switch_put(parent_sw);
867}
868
869static void
870icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
871{
872 const struct icm_fr_event_device_disconnected *pkg =
873 (const struct icm_fr_event_device_disconnected *)hdr;
874 struct tb_switch *sw;
875 u8 link, depth;
876
877 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
878 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
879 ICM_LINK_INFO_DEPTH_SHIFT;
880
Mika Westerbergf0342e72018-12-30 12:14:46 +0200881 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300882 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
883 return;
884 }
885
886 sw = tb_switch_find_by_link_depth(tb, link, depth);
887 if (!sw) {
888 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
889 depth);
890 return;
891 }
892
Mika Westerbergb658eb92020-11-10 11:04:31 +0300893 pm_runtime_get_sync(sw->dev.parent);
894
Mika Westerbergf67cf492017-06-06 15:25:16 +0300895 remove_switch(sw);
Mika Westerbergb658eb92020-11-10 11:04:31 +0300896
897 pm_runtime_mark_last_busy(sw->dev.parent);
898 pm_runtime_put_autosuspend(sw->dev.parent);
899
Mika Westerbergf67cf492017-06-06 15:25:16 +0300900 tb_switch_put(sw);
901}
902
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300903static void
904icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
905{
906 const struct icm_fr_event_xdomain_connected *pkg =
907 (const struct icm_fr_event_xdomain_connected *)hdr;
908 struct tb_xdomain *xd;
909 struct tb_switch *sw;
910 u8 link, depth;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300911 u64 route;
912
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300913 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
914 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
915 ICM_LINK_INFO_DEPTH_SHIFT;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300916
Mika Westerbergf0342e72018-12-30 12:14:46 +0200917 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300918 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
919 return;
920 }
921
922 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
923
924 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
925 if (xd) {
926 u8 xd_phy_port, phy_port;
927
928 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
929 phy_port = phy_port_from_route(route, depth);
930
931 if (xd->depth == depth && xd_phy_port == phy_port) {
Mika Westerbergee487dd2017-10-04 14:42:20 +0300932 update_xdomain(xd, route, link);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300933 tb_xdomain_put(xd);
934 return;
935 }
936
937 /*
938 * If we find an existing XDomain connection remove it
939 * now. We need to go through login handshake and
940 * everything anyway to be able to re-establish the
941 * connection.
942 */
943 remove_xdomain(xd);
944 tb_xdomain_put(xd);
945 }
946
947 /*
948 * Look if there already exists an XDomain in the same place
949 * than the new one and in that case remove it because it is
950 * most likely another host that got disconnected.
951 */
952 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
953 if (!xd) {
954 u8 dual_link;
955
956 dual_link = dual_link_from_link(link);
957 if (dual_link)
958 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
959 depth);
960 }
961 if (xd) {
962 remove_xdomain(xd);
963 tb_xdomain_put(xd);
964 }
965
966 /*
967 * If the user disconnected a switch during suspend and
968 * connected another host to the same port, remove the switch
969 * first.
970 */
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200971 sw = tb_switch_find_by_route(tb, route);
972 if (sw) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300973 remove_switch(sw);
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200974 tb_switch_put(sw);
975 }
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300976
977 sw = tb_switch_find_by_link_depth(tb, link, depth);
978 if (!sw) {
979 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
980 depth);
981 return;
982 }
983
Mika Westerbergee487dd2017-10-04 14:42:20 +0300984 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
985 depth);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300986 tb_switch_put(sw);
987}
988
989static void
990icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
991{
992 const struct icm_fr_event_xdomain_disconnected *pkg =
993 (const struct icm_fr_event_xdomain_disconnected *)hdr;
994 struct tb_xdomain *xd;
995
996 /*
997 * If the connection is through one or multiple devices, the
998 * XDomain device is removed along with them so it is fine if we
999 * cannot find it here.
1000 */
1001 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1002 if (xd) {
1003 remove_xdomain(xd);
1004 tb_xdomain_put(xd);
1005 }
1006}
1007
Mika Westerberg0d538272019-05-31 13:31:54 +03001008static int icm_tr_cio_reset(struct tb *tb)
1009{
1010 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
1011}
1012
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001013static int
1014icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001015 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001016{
1017 struct icm_tr_pkg_driver_ready_response reply;
1018 struct icm_pkg_driver_ready request = {
1019 .hdr.code = ICM_DRIVER_READY,
1020 };
1021 int ret;
1022
1023 memset(&reply, 0, sizeof(reply));
1024 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1025 1, 20000);
1026 if (ret)
1027 return ret;
1028
1029 if (security_level)
1030 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
Mika Westerberg90393872020-11-03 13:55:32 +02001031 if (proto_version)
1032 *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
1033 ICM_TR_INFO_PROTO_VERSION_SHIFT;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001034 if (nboot_acl)
1035 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
1036 ICM_TR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001037 if (rpm)
1038 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
1039
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001040 return 0;
1041}
1042
1043static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
1044{
1045 struct icm_tr_pkg_approve_device request;
1046 struct icm_tr_pkg_approve_device reply;
1047 int ret;
1048
1049 memset(&request, 0, sizeof(request));
1050 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1051 request.hdr.code = ICM_APPROVE_DEVICE;
1052 request.route_lo = sw->config.route_lo;
1053 request.route_hi = sw->config.route_hi;
1054 request.connection_id = sw->connection_id;
1055
1056 memset(&reply, 0, sizeof(reply));
1057 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1058 1, ICM_APPROVE_TIMEOUT);
1059 if (ret)
1060 return ret;
1061
1062 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1063 tb_warn(tb, "PCIe tunnel creation failed\n");
1064 return -EIO;
1065 }
1066
1067 return 0;
1068}
1069
1070static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
1071{
1072 struct icm_tr_pkg_add_device_key_response reply;
1073 struct icm_tr_pkg_add_device_key request;
1074 int ret;
1075
1076 memset(&request, 0, sizeof(request));
1077 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1078 request.hdr.code = ICM_ADD_DEVICE_KEY;
1079 request.route_lo = sw->config.route_lo;
1080 request.route_hi = sw->config.route_hi;
1081 request.connection_id = sw->connection_id;
1082 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
1083
1084 memset(&reply, 0, sizeof(reply));
1085 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1086 1, ICM_TIMEOUT);
1087 if (ret)
1088 return ret;
1089
1090 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1091 tb_warn(tb, "Adding key to switch failed\n");
1092 return -EIO;
1093 }
1094
1095 return 0;
1096}
1097
1098static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
1099 const u8 *challenge, u8 *response)
1100{
1101 struct icm_tr_pkg_challenge_device_response reply;
1102 struct icm_tr_pkg_challenge_device request;
1103 int ret;
1104
1105 memset(&request, 0, sizeof(request));
1106 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1107 request.hdr.code = ICM_CHALLENGE_DEVICE;
1108 request.route_lo = sw->config.route_lo;
1109 request.route_hi = sw->config.route_hi;
1110 request.connection_id = sw->connection_id;
1111 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
1112
1113 memset(&reply, 0, sizeof(reply));
1114 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1115 1, ICM_TIMEOUT);
1116 if (ret)
1117 return ret;
1118
1119 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1120 return -EKEYREJECTED;
1121 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
1122 return -ENOKEY;
1123
1124 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
1125
1126 return 0;
1127}
1128
Mika Westerberg180b0682021-01-08 16:25:39 +02001129static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1130 int transmit_path, int transmit_ring,
1131 int receive_path, int receive_ring)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001132{
1133 struct icm_tr_pkg_approve_xdomain_response reply;
1134 struct icm_tr_pkg_approve_xdomain request;
1135 int ret;
1136
1137 memset(&request, 0, sizeof(request));
1138 request.hdr.code = ICM_APPROVE_XDOMAIN;
1139 request.route_hi = upper_32_bits(xd->route);
1140 request.route_lo = lower_32_bits(xd->route);
Mika Westerberg180b0682021-01-08 16:25:39 +02001141 request.transmit_path = transmit_path;
1142 request.transmit_ring = transmit_ring;
1143 request.receive_path = receive_path;
1144 request.receive_ring = receive_ring;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001145 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1146
1147 memset(&reply, 0, sizeof(reply));
1148 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1149 1, ICM_TIMEOUT);
1150 if (ret)
1151 return ret;
1152
1153 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1154 return -EIO;
1155
1156 return 0;
1157}
1158
1159static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
1160 int stage)
1161{
1162 struct icm_tr_pkg_disconnect_xdomain_response reply;
1163 struct icm_tr_pkg_disconnect_xdomain request;
1164 int ret;
1165
1166 memset(&request, 0, sizeof(request));
1167 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
1168 request.stage = stage;
1169 request.route_hi = upper_32_bits(xd->route);
1170 request.route_lo = lower_32_bits(xd->route);
1171 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1172
1173 memset(&reply, 0, sizeof(reply));
1174 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1175 1, ICM_TIMEOUT);
1176 if (ret)
1177 return ret;
1178
1179 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1180 return -EIO;
1181
1182 return 0;
1183}
1184
Mika Westerberg180b0682021-01-08 16:25:39 +02001185static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1186 int transmit_path, int transmit_ring,
1187 int receive_path, int receive_ring)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001188{
1189 int ret;
1190
1191 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1192 if (ret)
1193 return ret;
1194
1195 usleep_range(10, 50);
1196 return icm_tr_xdomain_tear_down(tb, xd, 2);
1197}
1198
1199static void
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001200__icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
1201 bool force_rtd3)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001202{
1203 const struct icm_tr_event_device_connected *pkg =
1204 (const struct icm_tr_event_device_connected *)hdr;
Mika Westerberg91c0c122019-03-21 19:03:00 +02001205 bool authorized, boot, dual_lane, speed_gen3;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001206 enum tb_security_level security_level;
1207 struct tb_switch *sw, *parent_sw;
1208 struct tb_xdomain *xd;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001209 u64 route;
1210
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001211 icm_postpone_rescan(tb);
1212
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001213 /*
1214 * Currently we don't use the QoS information coming with the
1215 * device connected message so simply just ignore that extra
1216 * packet for now.
1217 */
1218 if (pkg->hdr.packet_id)
1219 return;
1220
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001221 route = get_route(pkg->route_hi, pkg->route_lo);
1222 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1223 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1224 ICM_FLAGS_SLEVEL_SHIFT;
1225 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
Mika Westerberg91c0c122019-03-21 19:03:00 +02001226 dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
1227 speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001228
1229 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1230 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1231 route);
1232 return;
1233 }
1234
1235 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1236 if (sw) {
1237 /* Update the switch if it is still in the same place */
1238 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1239 parent_sw = tb_to_switch(sw->dev.parent);
1240 update_switch(parent_sw, sw, route, pkg->connection_id,
1241 0, 0, 0, boot);
1242 tb_switch_put(sw);
1243 return;
1244 }
1245
1246 remove_switch(sw);
1247 tb_switch_put(sw);
1248 }
1249
1250 /* Another switch with the same address */
1251 sw = tb_switch_find_by_route(tb, route);
1252 if (sw) {
1253 remove_switch(sw);
1254 tb_switch_put(sw);
1255 }
1256
1257 /* XDomain connection with the same address */
1258 xd = tb_xdomain_find_by_route(tb, route);
1259 if (xd) {
1260 remove_xdomain(xd);
1261 tb_xdomain_put(xd);
1262 }
1263
1264 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1265 if (!parent_sw) {
1266 tb_err(tb, "failed to find parent switch for %llx\n", route);
1267 return;
1268 }
1269
Mika Westerbergb5db76d2019-10-08 19:03:34 +03001270 pm_runtime_get_sync(&parent_sw->dev);
1271
1272 sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
1273 if (!IS_ERR(sw)) {
1274 sw->connection_id = pkg->connection_id;
1275 sw->authorized = authorized;
1276 sw->security_level = security_level;
1277 sw->boot = boot;
Mika Westerberg91c0c122019-03-21 19:03:00 +02001278 sw->link_speed = speed_gen3 ? 20 : 10;
1279 sw->link_width = dual_lane ? 2 : 1;
Mika Westerbergb5db76d2019-10-08 19:03:34 +03001280 sw->rpm = force_rtd3;
1281 if (!sw->rpm)
1282 sw->rpm = intel_vss_is_rtd3(pkg->ep_name,
1283 sizeof(pkg->ep_name));
1284
1285 if (add_switch(parent_sw, sw))
1286 tb_switch_put(sw);
1287 }
1288
1289 pm_runtime_mark_last_busy(&parent_sw->dev);
1290 pm_runtime_put_autosuspend(&parent_sw->dev);
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001291
1292 tb_switch_put(parent_sw);
1293}
1294
1295static void
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001296icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1297{
1298 __icm_tr_device_connected(tb, hdr, false);
1299}
1300
1301static void
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001302icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1303{
1304 const struct icm_tr_event_device_disconnected *pkg =
1305 (const struct icm_tr_event_device_disconnected *)hdr;
1306 struct tb_switch *sw;
1307 u64 route;
1308
1309 route = get_route(pkg->route_hi, pkg->route_lo);
1310
1311 sw = tb_switch_find_by_route(tb, route);
1312 if (!sw) {
1313 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1314 return;
1315 }
Mika Westerbergb658eb92020-11-10 11:04:31 +03001316 pm_runtime_get_sync(sw->dev.parent);
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001317
1318 remove_switch(sw);
Mika Westerbergb658eb92020-11-10 11:04:31 +03001319
1320 pm_runtime_mark_last_busy(sw->dev.parent);
1321 pm_runtime_put_autosuspend(sw->dev.parent);
1322
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001323 tb_switch_put(sw);
1324}
1325
1326static void
1327icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1328{
1329 const struct icm_tr_event_xdomain_connected *pkg =
1330 (const struct icm_tr_event_xdomain_connected *)hdr;
1331 struct tb_xdomain *xd;
1332 struct tb_switch *sw;
1333 u64 route;
1334
1335 if (!tb->root_switch)
1336 return;
1337
1338 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1339
1340 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1341 if (xd) {
1342 if (xd->route == route) {
1343 update_xdomain(xd, route, 0);
1344 tb_xdomain_put(xd);
1345 return;
1346 }
1347
1348 remove_xdomain(xd);
1349 tb_xdomain_put(xd);
1350 }
1351
1352 /* An existing xdomain with the same address */
1353 xd = tb_xdomain_find_by_route(tb, route);
1354 if (xd) {
1355 remove_xdomain(xd);
1356 tb_xdomain_put(xd);
1357 }
1358
1359 /*
1360 * If the user disconnected a switch during suspend and
1361 * connected another host to the same port, remove the switch
1362 * first.
1363 */
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001364 sw = tb_switch_find_by_route(tb, route);
1365 if (sw) {
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001366 remove_switch(sw);
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001367 tb_switch_put(sw);
1368 }
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001369
1370 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1371 if (!sw) {
1372 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1373 return;
1374 }
1375
1376 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1377 tb_switch_put(sw);
1378}
1379
1380static void
1381icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1382{
1383 const struct icm_tr_event_xdomain_disconnected *pkg =
1384 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1385 struct tb_xdomain *xd;
1386 u64 route;
1387
1388 route = get_route(pkg->route_hi, pkg->route_lo);
1389
1390 xd = tb_xdomain_find_by_route(tb, route);
1391 if (xd) {
1392 remove_xdomain(xd);
1393 tb_xdomain_put(xd);
1394 }
1395}
1396
Mika Westerbergf67cf492017-06-06 15:25:16 +03001397static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1398{
1399 struct pci_dev *parent;
1400
1401 parent = pci_upstream_bridge(pdev);
1402 while (parent) {
1403 if (!pci_is_pcie(parent))
1404 return NULL;
1405 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1406 break;
1407 parent = pci_upstream_bridge(parent);
1408 }
1409
1410 if (!parent)
1411 return NULL;
1412
1413 switch (parent->device) {
1414 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1415 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1416 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1417 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1418 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
Mika Westerbergc4630d62019-01-21 16:41:27 +03001419 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1420 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
Mika Westerbergf67cf492017-06-06 15:25:16 +03001421 return parent;
1422 }
1423
1424 return NULL;
1425}
1426
1427static bool icm_ar_is_supported(struct tb *tb)
1428{
1429 struct pci_dev *upstream_port;
1430 struct icm *icm = tb_priv(tb);
1431
1432 /*
1433 * Starting from Alpine Ridge we can use ICM on Apple machines
1434 * as well. We just need to reset and re-enable it first.
Mika Westerberg354a7a72019-03-21 15:31:14 +02001435 * However, only start it if explicitly asked by the user.
Mika Westerbergf67cf492017-06-06 15:25:16 +03001436 */
Mika Westerberg354a7a72019-03-21 15:31:14 +02001437 if (icm_firmware_running(tb->nhi))
Mika Westerbergf67cf492017-06-06 15:25:16 +03001438 return true;
Mika Westerberg354a7a72019-03-21 15:31:14 +02001439 if (!start_icm)
1440 return false;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001441
1442 /*
1443 * Find the upstream PCIe port in case we need to do reset
1444 * through its vendor specific registers.
1445 */
1446 upstream_port = get_upstream_port(tb->nhi->pdev);
1447 if (upstream_port) {
1448 int cap;
1449
1450 cap = pci_find_ext_capability(upstream_port,
1451 PCI_EXT_CAP_ID_VNDR);
1452 if (cap > 0) {
1453 icm->upstream_port = upstream_port;
1454 icm->vnd_cap = cap;
1455
1456 return true;
1457 }
1458 }
1459
1460 return false;
1461}
1462
Mika Westerberg0d538272019-05-31 13:31:54 +03001463static int icm_ar_cio_reset(struct tb *tb)
1464{
1465 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
1466}
1467
Mika Westerbergf67cf492017-06-06 15:25:16 +03001468static int icm_ar_get_mode(struct tb *tb)
1469{
1470 struct tb_nhi *nhi = tb->nhi;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001471 int retries = 60;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001472 u32 val;
1473
1474 do {
1475 val = ioread32(nhi->iobase + REG_FW_STS);
1476 if (val & REG_FW_STS_NVM_AUTH_DONE)
1477 break;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001478 msleep(50);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001479 } while (--retries);
1480
1481 if (!retries) {
1482 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1483 return -ENODEV;
1484 }
1485
1486 return nhi_mailbox_mode(nhi);
1487}
1488
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001489static int
1490icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001491 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001492{
1493 struct icm_ar_pkg_driver_ready_response reply;
1494 struct icm_pkg_driver_ready request = {
1495 .hdr.code = ICM_DRIVER_READY,
1496 };
1497 int ret;
1498
1499 memset(&reply, 0, sizeof(reply));
1500 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1501 1, ICM_TIMEOUT);
1502 if (ret)
1503 return ret;
1504
1505 if (security_level)
1506 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1507 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1508 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1509 ICM_AR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001510 if (rpm)
1511 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1512
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001513 return 0;
1514}
1515
Mika Westerbergf67cf492017-06-06 15:25:16 +03001516static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1517{
1518 struct icm_ar_pkg_get_route_response reply;
1519 struct icm_ar_pkg_get_route request = {
1520 .hdr = { .code = ICM_GET_ROUTE },
1521 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1522 };
1523 int ret;
1524
1525 memset(&reply, 0, sizeof(reply));
1526 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1527 1, ICM_TIMEOUT);
1528 if (ret)
1529 return ret;
1530
1531 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1532 return -EIO;
1533
1534 *route = get_route(reply.route_hi, reply.route_lo);
1535 return 0;
1536}
1537
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001538static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1539{
1540 struct icm_ar_pkg_preboot_acl_response reply;
1541 struct icm_ar_pkg_preboot_acl request = {
1542 .hdr = { .code = ICM_PREBOOT_ACL },
1543 };
1544 int ret, i;
1545
1546 memset(&reply, 0, sizeof(reply));
1547 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1548 1, ICM_TIMEOUT);
1549 if (ret)
1550 return ret;
1551
1552 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1553 return -EIO;
1554
1555 for (i = 0; i < nuuids; i++) {
1556 u32 *uuid = (u32 *)&uuids[i];
1557
1558 uuid[0] = reply.acl[i].uuid_lo;
1559 uuid[1] = reply.acl[i].uuid_hi;
1560
1561 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1562 /* Map empty entries to null UUID */
1563 uuid[0] = 0;
1564 uuid[1] = 0;
Mika Westerbergdd010bd2018-05-15 16:04:25 +03001565 } else if (uuid[0] != 0 || uuid[1] != 0) {
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001566 /* Upper two DWs are always one's */
1567 uuid[2] = 0xffffffff;
1568 uuid[3] = 0xffffffff;
1569 }
1570 }
1571
1572 return ret;
1573}
1574
1575static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1576 size_t nuuids)
1577{
1578 struct icm_ar_pkg_preboot_acl_response reply;
1579 struct icm_ar_pkg_preboot_acl request = {
1580 .hdr = {
1581 .code = ICM_PREBOOT_ACL,
1582 .flags = ICM_FLAGS_WRITE,
1583 },
1584 };
1585 int ret, i;
1586
1587 for (i = 0; i < nuuids; i++) {
1588 const u32 *uuid = (const u32 *)&uuids[i];
1589
1590 if (uuid_is_null(&uuids[i])) {
1591 /*
1592 * Map null UUID to the empty (all one) entries
1593 * for ICM.
1594 */
1595 request.acl[i].uuid_lo = 0xffffffff;
1596 request.acl[i].uuid_hi = 0xffffffff;
1597 } else {
1598 /* Two high DWs need to be set to all one */
1599 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1600 return -EINVAL;
1601
1602 request.acl[i].uuid_lo = uuid[0];
1603 request.acl[i].uuid_hi = uuid[1];
1604 }
1605 }
1606
1607 memset(&reply, 0, sizeof(reply));
1608 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1609 1, ICM_TIMEOUT);
1610 if (ret)
1611 return ret;
1612
1613 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1614 return -EIO;
1615
1616 return 0;
1617}
1618
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001619static int
1620icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001621 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001622{
1623 struct icm_tr_pkg_driver_ready_response reply;
1624 struct icm_pkg_driver_ready request = {
1625 .hdr.code = ICM_DRIVER_READY,
1626 };
1627 int ret;
1628
1629 memset(&reply, 0, sizeof(reply));
1630 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1631 1, 20000);
1632 if (ret)
1633 return ret;
1634
Mika Westerberg90393872020-11-03 13:55:32 +02001635 if (proto_version)
1636 *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
1637 ICM_TR_INFO_PROTO_VERSION_SHIFT;
1638
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001639 /* Ice Lake always supports RTD3 */
1640 if (rpm)
1641 *rpm = true;
1642
1643 return 0;
1644}
1645
1646static void icm_icl_set_uuid(struct tb *tb)
1647{
1648 struct tb_nhi *nhi = tb->nhi;
1649 u32 uuid[4];
1650
1651 pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]);
1652 pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]);
1653 uuid[2] = 0xffffffff;
1654 uuid[3] = 0xffffffff;
1655
1656 tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1657}
1658
1659static void
1660icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1661{
1662 __icm_tr_device_connected(tb, hdr, true);
1663}
1664
1665static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr)
1666{
1667 const struct icm_icl_event_rtd3_veto *pkg =
1668 (const struct icm_icl_event_rtd3_veto *)hdr;
1669
1670 tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason);
1671
1672 if (pkg->veto_reason)
1673 icm_veto_begin(tb);
1674 else
1675 icm_veto_end(tb);
1676}
1677
Mika Westerberg57d8df62018-09-14 12:58:37 +03001678static bool icm_tgl_is_supported(struct tb *tb)
1679{
Gil Fine0172e412021-06-01 16:32:29 +03001680 unsigned long end = jiffies + msecs_to_jiffies(10);
Mika Westerberg49f2a7f2020-06-17 19:04:09 +03001681
Gil Fine0172e412021-06-01 16:32:29 +03001682 do {
1683 u32 val;
1684
1685 val = ioread32(tb->nhi->iobase + REG_FW_STS);
1686 if (val & REG_FW_STS_NVM_AUTH_DONE)
1687 return true;
1688 usleep_range(100, 500);
1689 } while (time_before(jiffies, end));
1690
1691 return false;
Mika Westerberg57d8df62018-09-14 12:58:37 +03001692}
1693
Mika Westerbergf67cf492017-06-06 15:25:16 +03001694static void icm_handle_notification(struct work_struct *work)
1695{
1696 struct icm_notification *n = container_of(work, typeof(*n), work);
1697 struct tb *tb = n->tb;
1698 struct icm *icm = tb_priv(tb);
1699
1700 mutex_lock(&tb->lock);
1701
Mika Westerberg86da8092018-09-24 13:20:44 +03001702 /*
1703 * When the domain is stopped we flush its workqueue but before
1704 * that the root switch is removed. In that case we should treat
1705 * the queued events as being canceled.
1706 */
1707 if (tb->root_switch) {
1708 switch (n->pkg->code) {
1709 case ICM_EVENT_DEVICE_CONNECTED:
1710 icm->device_connected(tb, n->pkg);
1711 break;
1712 case ICM_EVENT_DEVICE_DISCONNECTED:
1713 icm->device_disconnected(tb, n->pkg);
1714 break;
1715 case ICM_EVENT_XDOMAIN_CONNECTED:
Mika Westerberg5ca67682020-10-22 13:22:06 +03001716 if (tb_is_xdomain_enabled())
1717 icm->xdomain_connected(tb, n->pkg);
Mika Westerberg86da8092018-09-24 13:20:44 +03001718 break;
1719 case ICM_EVENT_XDOMAIN_DISCONNECTED:
Mika Westerberg5ca67682020-10-22 13:22:06 +03001720 if (tb_is_xdomain_enabled())
1721 icm->xdomain_disconnected(tb, n->pkg);
Mika Westerberg86da8092018-09-24 13:20:44 +03001722 break;
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001723 case ICM_EVENT_RTD3_VETO:
1724 icm->rtd3_veto(tb, n->pkg);
1725 break;
Mika Westerberg86da8092018-09-24 13:20:44 +03001726 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001727 }
1728
1729 mutex_unlock(&tb->lock);
1730
1731 kfree(n->pkg);
1732 kfree(n);
1733}
1734
1735static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1736 const void *buf, size_t size)
1737{
1738 struct icm_notification *n;
1739
1740 n = kmalloc(sizeof(*n), GFP_KERNEL);
1741 if (!n)
1742 return;
1743
1744 INIT_WORK(&n->work, icm_handle_notification);
1745 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1746 n->tb = tb;
1747
1748 queue_work(tb->wq, &n->work);
1749}
1750
1751static int
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001752__icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001753 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001754{
Mika Westerberg3080e192017-10-06 18:14:13 +03001755 struct icm *icm = tb_priv(tb);
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001756 unsigned int retries = 50;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001757 int ret;
1758
Mika Westerberg90393872020-11-03 13:55:32 +02001759 ret = icm->driver_ready(tb, security_level, proto_version, nboot_acl,
1760 rpm);
Mika Westerberg3080e192017-10-06 18:14:13 +03001761 if (ret) {
1762 tb_err(tb, "failed to send driver ready to ICM\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001763 return ret;
Mika Westerberg3080e192017-10-06 18:14:13 +03001764 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001765
1766 /*
1767 * Hold on here until the switch config space is accessible so
1768 * that we can read root switch config successfully.
1769 */
1770 do {
1771 struct tb_cfg_result res;
1772 u32 tmp;
1773
1774 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1775 0, 1, 100);
1776 if (!res.err)
1777 return 0;
1778
1779 msleep(50);
1780 } while (--retries);
1781
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001782 tb_err(tb, "failed to read root switch config space, giving up\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001783 return -ETIMEDOUT;
1784}
1785
Mika Westerbergf67cf492017-06-06 15:25:16 +03001786static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1787{
1788 struct icm *icm = tb_priv(tb);
1789 u32 val;
1790
Mika Westerbergea9d7bb2018-03-09 13:17:01 +03001791 if (!icm->upstream_port)
1792 return -ENODEV;
1793
Mika Westerbergf67cf492017-06-06 15:25:16 +03001794 /* Put ARC to wait for CIO reset event to happen */
1795 val = ioread32(nhi->iobase + REG_FW_STS);
1796 val |= REG_FW_STS_CIO_RESET_REQ;
1797 iowrite32(val, nhi->iobase + REG_FW_STS);
1798
1799 /* Re-start ARC */
1800 val = ioread32(nhi->iobase + REG_FW_STS);
1801 val |= REG_FW_STS_ICM_EN_INVERT;
1802 val |= REG_FW_STS_ICM_EN_CPU;
1803 iowrite32(val, nhi->iobase + REG_FW_STS);
1804
1805 /* Trigger CIO reset now */
Mika Westerberg0d538272019-05-31 13:31:54 +03001806 return icm->cio_reset(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001807}
1808
1809static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1810{
1811 unsigned int retries = 10;
1812 int ret;
1813 u32 val;
1814
1815 /* Check if the ICM firmware is already running */
Mika Westerberg354a7a72019-03-21 15:31:14 +02001816 if (icm_firmware_running(nhi))
Mika Westerbergf67cf492017-06-06 15:25:16 +03001817 return 0;
1818
Mika Westerberg62efe692018-09-17 16:32:13 +03001819 dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001820
1821 ret = icm_firmware_reset(tb, nhi);
1822 if (ret)
1823 return ret;
1824
1825 /* Wait until the ICM firmware tells us it is up and running */
1826 do {
1827 /* Check that the ICM firmware is running */
1828 val = ioread32(nhi->iobase + REG_FW_STS);
1829 if (val & REG_FW_STS_NVM_AUTH_DONE)
1830 return 0;
1831
1832 msleep(300);
1833 } while (--retries);
1834
1835 return -ETIMEDOUT;
1836}
1837
1838static int icm_reset_phy_port(struct tb *tb, int phy_port)
1839{
1840 struct icm *icm = tb_priv(tb);
1841 u32 state0, state1;
1842 int port0, port1;
1843 u32 val0, val1;
1844 int ret;
1845
1846 if (!icm->upstream_port)
1847 return 0;
1848
1849 if (phy_port) {
1850 port0 = 3;
1851 port1 = 4;
1852 } else {
1853 port0 = 1;
1854 port1 = 2;
1855 }
1856
1857 /*
1858 * Read link status of both null ports belonging to a single
1859 * physical port.
1860 */
1861 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1862 if (ret)
1863 return ret;
1864 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1865 if (ret)
1866 return ret;
1867
1868 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1869 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1870 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1871 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1872
1873 /* If they are both up we need to reset them now */
1874 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1875 return 0;
1876
1877 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1878 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1879 if (ret)
1880 return ret;
1881
1882 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1883 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1884 if (ret)
1885 return ret;
1886
1887 /* Wait a bit and then re-enable both ports */
1888 usleep_range(10, 100);
1889
1890 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1891 if (ret)
1892 return ret;
1893 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1894 if (ret)
1895 return ret;
1896
1897 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1898 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1899 if (ret)
1900 return ret;
1901
1902 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1903 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1904}
1905
1906static int icm_firmware_init(struct tb *tb)
1907{
1908 struct icm *icm = tb_priv(tb);
1909 struct tb_nhi *nhi = tb->nhi;
1910 int ret;
1911
1912 ret = icm_firmware_start(tb, nhi);
1913 if (ret) {
1914 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1915 return ret;
1916 }
1917
1918 if (icm->get_mode) {
1919 ret = icm->get_mode(tb);
1920
1921 switch (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001922 case NHI_FW_SAFE_MODE:
1923 icm->safe_mode = true;
1924 break;
1925
Mika Westerbergf67cf492017-06-06 15:25:16 +03001926 case NHI_FW_CM_MODE:
1927 /* Ask ICM to accept all Thunderbolt devices */
1928 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1929 break;
1930
1931 default:
Mika Westerberge4be8c92017-11-24 17:51:12 +03001932 if (ret < 0)
1933 return ret;
1934
Mika Westerbergf67cf492017-06-06 15:25:16 +03001935 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1936 return -ENODEV;
1937 }
1938 }
1939
1940 /*
1941 * Reset both physical ports if there is anything connected to
1942 * them already.
1943 */
1944 ret = icm_reset_phy_port(tb, 0);
1945 if (ret)
1946 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1947 ret = icm_reset_phy_port(tb, 1);
1948 if (ret)
1949 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1950
1951 return 0;
1952}
1953
1954static int icm_driver_ready(struct tb *tb)
1955{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001956 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001957 int ret;
1958
1959 ret = icm_firmware_init(tb);
1960 if (ret)
1961 return ret;
1962
Mika Westerberge6b245c2017-06-06 15:25:17 +03001963 if (icm->safe_mode) {
1964 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1965 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1966 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1967 return 0;
1968 }
1969
Mika Westerberg90393872020-11-03 13:55:32 +02001970 ret = __icm_driver_ready(tb, &tb->security_level, &icm->proto_version,
1971 &tb->nboot_acl, &icm->rpm);
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001972 if (ret)
1973 return ret;
1974
1975 /*
1976 * Make sure the number of supported preboot ACL matches what we
1977 * expect or disable the whole feature.
1978 */
1979 if (tb->nboot_acl > icm->max_boot_acl)
1980 tb->nboot_acl = 0;
1981
Mika Westerberg90393872020-11-03 13:55:32 +02001982 if (icm->proto_version >= 3)
1983 tb_dbg(tb, "USB4 proxy operations supported\n");
1984
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001985 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001986}
1987
1988static int icm_suspend(struct tb *tb)
1989{
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001990 struct icm *icm = tb_priv(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001991
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001992 if (icm->save_devices)
1993 icm->save_devices(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001994
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001995 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001996 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001997}
1998
1999/*
2000 * Mark all switches (except root switch) below this one unplugged. ICM
2001 * firmware will send us an updated list of switches after we have send
2002 * it driver ready command. If a switch is not in that list it will be
2003 * removed when we perform rescan.
2004 */
2005static void icm_unplug_children(struct tb_switch *sw)
2006{
Mika Westerbergb433d012019-09-30 14:07:22 +03002007 struct tb_port *port;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002008
2009 if (tb_route(sw))
2010 sw->is_unplugged = true;
2011
Mika Westerbergb433d012019-09-30 14:07:22 +03002012 tb_switch_for_each_port(sw, port) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002013 if (port->xdomain)
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002014 port->xdomain->is_unplugged = true;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002015 else if (tb_port_has_remote(port))
2016 icm_unplug_children(port->remote->sw);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002017 }
2018}
2019
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002020static int complete_rpm(struct device *dev, void *data)
2021{
2022 struct tb_switch *sw = tb_to_switch(dev);
2023
2024 if (sw)
2025 complete(&sw->rpm_complete);
2026 return 0;
2027}
2028
2029static void remove_unplugged_switch(struct tb_switch *sw)
2030{
Mika Westerberg600c0842020-11-18 13:08:21 +02002031 struct device *parent = get_device(sw->dev.parent);
2032
2033 pm_runtime_get_sync(parent);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002034
2035 /*
2036 * Signal this and switches below for rpm_complete because
2037 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
2038 * for it.
2039 */
2040 complete_rpm(&sw->dev, NULL);
2041 bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
2042 tb_switch_remove(sw);
2043
Mika Westerberg600c0842020-11-18 13:08:21 +02002044 pm_runtime_mark_last_busy(parent);
2045 pm_runtime_put_autosuspend(parent);
2046
2047 put_device(parent);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002048}
2049
Mika Westerbergf67cf492017-06-06 15:25:16 +03002050static void icm_free_unplugged_children(struct tb_switch *sw)
2051{
Mika Westerbergb433d012019-09-30 14:07:22 +03002052 struct tb_port *port;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002053
Mika Westerbergb433d012019-09-30 14:07:22 +03002054 tb_switch_for_each_port(sw, port) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002055 if (port->xdomain && port->xdomain->is_unplugged) {
2056 tb_xdomain_remove(port->xdomain);
2057 port->xdomain = NULL;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002058 } else if (tb_port_has_remote(port)) {
2059 if (port->remote->sw->is_unplugged) {
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002060 remove_unplugged_switch(port->remote->sw);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002061 port->remote = NULL;
2062 } else {
2063 icm_free_unplugged_children(port->remote->sw);
2064 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03002065 }
2066 }
2067}
2068
2069static void icm_rescan_work(struct work_struct *work)
2070{
2071 struct icm *icm = container_of(work, struct icm, rescan_work.work);
2072 struct tb *tb = icm_to_tb(icm);
2073
2074 mutex_lock(&tb->lock);
2075 if (tb->root_switch)
2076 icm_free_unplugged_children(tb->root_switch);
2077 mutex_unlock(&tb->lock);
2078}
2079
2080static void icm_complete(struct tb *tb)
2081{
2082 struct icm *icm = tb_priv(tb);
2083
2084 if (tb->nhi->going_away)
2085 return;
2086
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002087 /*
2088 * If RTD3 was vetoed before we entered system suspend allow it
2089 * again now before driver ready is sent. Firmware sends a new RTD3
2090 * veto if it is still the case after we have sent it driver ready
2091 * command.
2092 */
2093 icm_veto_end(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002094 icm_unplug_children(tb->root_switch);
2095
2096 /*
2097 * Now all existing children should be resumed, start events
2098 * from ICM to get updated status.
2099 */
Mika Westerberg90393872020-11-03 13:55:32 +02002100 __icm_driver_ready(tb, NULL, NULL, NULL, NULL);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002101
2102 /*
2103 * We do not get notifications of devices that have been
2104 * unplugged during suspend so schedule rescan to clean them up
2105 * if any.
2106 */
2107 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
2108}
2109
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002110static int icm_runtime_suspend(struct tb *tb)
2111{
2112 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
2113 return 0;
2114}
2115
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002116static int icm_runtime_suspend_switch(struct tb_switch *sw)
2117{
2118 if (tb_route(sw))
2119 reinit_completion(&sw->rpm_complete);
2120 return 0;
2121}
2122
2123static int icm_runtime_resume_switch(struct tb_switch *sw)
2124{
2125 if (tb_route(sw)) {
2126 if (!wait_for_completion_timeout(&sw->rpm_complete,
2127 msecs_to_jiffies(500))) {
2128 dev_dbg(&sw->dev, "runtime resuming timed out\n");
2129 }
2130 }
2131 return 0;
2132}
2133
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002134static int icm_runtime_resume(struct tb *tb)
2135{
2136 /*
2137 * We can reuse the same resume functionality than with system
2138 * suspend.
2139 */
2140 icm_complete(tb);
2141 return 0;
2142}
2143
Mika Westerbergf67cf492017-06-06 15:25:16 +03002144static int icm_start(struct tb *tb)
2145{
Mika Westerberge6b245c2017-06-06 15:25:17 +03002146 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002147 int ret;
2148
Mika Westerberge6b245c2017-06-06 15:25:17 +03002149 if (icm->safe_mode)
2150 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
2151 else
2152 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Mika Westerberg444ac382018-12-30 12:17:52 +02002153 if (IS_ERR(tb->root_switch))
2154 return PTR_ERR(tb->root_switch);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002155
Mika Westerbergf437c242018-01-16 22:30:40 +02002156 tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002157 tb->root_switch->rpm = icm->rpm;
Mika Westerberge6b245c2017-06-06 15:25:17 +03002158
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002159 if (icm->set_uuid)
2160 icm->set_uuid(tb);
2161
Mika Westerbergf67cf492017-06-06 15:25:16 +03002162 ret = tb_switch_add(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002163 if (ret) {
Mika Westerbergf67cf492017-06-06 15:25:16 +03002164 tb_switch_put(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002165 tb->root_switch = NULL;
2166 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03002167
2168 return ret;
2169}
2170
2171static void icm_stop(struct tb *tb)
2172{
2173 struct icm *icm = tb_priv(tb);
2174
2175 cancel_delayed_work(&icm->rescan_work);
2176 tb_switch_remove(tb->root_switch);
2177 tb->root_switch = NULL;
2178 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
Mika Westerberg90393872020-11-03 13:55:32 +02002179 kfree(icm->last_nvm_auth);
2180 icm->last_nvm_auth = NULL;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002181}
2182
Mika Westerberge6b245c2017-06-06 15:25:17 +03002183static int icm_disconnect_pcie_paths(struct tb *tb)
2184{
2185 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
2186}
2187
Mika Westerberg90393872020-11-03 13:55:32 +02002188static void icm_usb4_switch_nvm_auth_complete(void *data)
2189{
2190 struct usb4_switch_nvm_auth *auth = data;
2191 struct icm *icm = auth->icm;
2192 struct tb *tb = icm_to_tb(icm);
2193
2194 tb_dbg(tb, "NVM_AUTH response for %llx flags %#x status %#x\n",
2195 get_route(auth->reply.route_hi, auth->reply.route_lo),
2196 auth->reply.hdr.flags, auth->reply.status);
2197
2198 mutex_lock(&tb->lock);
2199 if (WARN_ON(icm->last_nvm_auth))
2200 kfree(icm->last_nvm_auth);
2201 icm->last_nvm_auth = auth;
2202 mutex_unlock(&tb->lock);
2203}
2204
2205static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
2206{
2207 struct usb4_switch_nvm_auth *auth;
2208 struct icm *icm = tb_priv(tb);
2209 struct tb_cfg_request *req;
2210 int ret;
2211
2212 auth = kzalloc(sizeof(*auth), GFP_KERNEL);
2213 if (!auth)
2214 return -ENOMEM;
2215
2216 auth->icm = icm;
2217 auth->request.hdr.code = ICM_USB4_SWITCH_OP;
2218 auth->request.route_hi = upper_32_bits(route);
2219 auth->request.route_lo = lower_32_bits(route);
2220 auth->request.opcode = USB4_SWITCH_OP_NVM_AUTH;
2221
2222 req = tb_cfg_request_alloc();
2223 if (!req) {
2224 ret = -ENOMEM;
2225 goto err_free_auth;
2226 }
2227
2228 req->match = icm_match;
2229 req->copy = icm_copy;
2230 req->request = &auth->request;
2231 req->request_size = sizeof(auth->request);
2232 req->request_type = TB_CFG_PKG_ICM_CMD;
2233 req->response = &auth->reply;
2234 req->npackets = 1;
2235 req->response_size = sizeof(auth->reply);
2236 req->response_type = TB_CFG_PKG_ICM_RESP;
2237
2238 tb_dbg(tb, "NVM_AUTH request for %llx\n", route);
2239
2240 mutex_lock(&icm->request_lock);
2241 ret = tb_cfg_request(tb->ctl, req, icm_usb4_switch_nvm_auth_complete,
2242 auth);
2243 mutex_unlock(&icm->request_lock);
2244
2245 tb_cfg_request_put(req);
2246 if (ret)
2247 goto err_free_auth;
2248 return 0;
2249
2250err_free_auth:
2251 kfree(auth);
2252 return ret;
2253}
2254
2255static int icm_usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
2256 u8 *status, const void *tx_data, size_t tx_data_len,
2257 void *rx_data, size_t rx_data_len)
2258{
2259 struct icm_usb4_switch_op_response reply;
2260 struct icm_usb4_switch_op request;
2261 struct tb *tb = sw->tb;
2262 struct icm *icm = tb_priv(tb);
2263 u64 route = tb_route(sw);
2264 int ret;
2265
2266 /*
2267 * USB4 router operation proxy is supported in firmware if the
2268 * protocol version is 3 or higher.
2269 */
2270 if (icm->proto_version < 3)
2271 return -EOPNOTSUPP;
2272
2273 /*
2274 * NVM_AUTH is a special USB4 proxy operation that does not
2275 * return immediately so handle it separately.
2276 */
2277 if (opcode == USB4_SWITCH_OP_NVM_AUTH)
2278 return icm_usb4_switch_nvm_authenticate(tb, route);
2279
2280 memset(&request, 0, sizeof(request));
2281 request.hdr.code = ICM_USB4_SWITCH_OP;
2282 request.route_hi = upper_32_bits(route);
2283 request.route_lo = lower_32_bits(route);
2284 request.opcode = opcode;
2285 if (metadata)
2286 request.metadata = *metadata;
2287
2288 if (tx_data_len) {
2289 request.data_len_valid |= ICM_USB4_SWITCH_DATA_VALID;
2290 if (tx_data_len < ARRAY_SIZE(request.data))
2291 request.data_len_valid =
2292 tx_data_len & ICM_USB4_SWITCH_DATA_LEN_MASK;
2293 memcpy(request.data, tx_data, tx_data_len * sizeof(u32));
2294 }
2295
2296 memset(&reply, 0, sizeof(reply));
2297 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
2298 1, ICM_TIMEOUT);
2299 if (ret)
2300 return ret;
2301
2302 if (reply.hdr.flags & ICM_FLAGS_ERROR)
2303 return -EIO;
2304
2305 if (status)
2306 *status = reply.status;
2307
2308 if (metadata)
2309 *metadata = reply.metadata;
2310
2311 if (rx_data_len)
2312 memcpy(rx_data, reply.data, rx_data_len * sizeof(u32));
2313
2314 return 0;
2315}
2316
2317static int icm_usb4_switch_nvm_authenticate_status(struct tb_switch *sw,
2318 u32 *status)
2319{
2320 struct usb4_switch_nvm_auth *auth;
2321 struct tb *tb = sw->tb;
2322 struct icm *icm = tb_priv(tb);
2323 int ret = 0;
2324
2325 if (icm->proto_version < 3)
2326 return -EOPNOTSUPP;
2327
2328 auth = icm->last_nvm_auth;
2329 icm->last_nvm_auth = NULL;
2330
2331 if (auth && auth->reply.route_hi == sw->config.route_hi &&
2332 auth->reply.route_lo == sw->config.route_lo) {
Mika Westerbergc14556f2020-12-17 12:39:11 +03002333 tb_dbg(tb, "NVM_AUTH found for %llx flags %#x status %#x\n",
Mika Westerberg90393872020-11-03 13:55:32 +02002334 tb_route(sw), auth->reply.hdr.flags, auth->reply.status);
2335 if (auth->reply.hdr.flags & ICM_FLAGS_ERROR)
2336 ret = -EIO;
2337 else
2338 *status = auth->reply.status;
2339 } else {
2340 *status = 0;
2341 }
2342
2343 kfree(auth);
2344 return ret;
2345}
2346
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002347/* Falcon Ridge */
Mika Westerbergf67cf492017-06-06 15:25:16 +03002348static const struct tb_cm_ops icm_fr_ops = {
2349 .driver_ready = icm_driver_ready,
2350 .start = icm_start,
2351 .stop = icm_stop,
2352 .suspend = icm_suspend,
2353 .complete = icm_complete,
2354 .handle_event = icm_handle_event,
2355 .approve_switch = icm_fr_approve_switch,
2356 .add_switch_key = icm_fr_add_switch_key,
2357 .challenge_switch_key = icm_fr_challenge_switch_key,
Mika Westerberge6b245c2017-06-06 15:25:17 +03002358 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002359 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2360 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
Mika Westerbergf67cf492017-06-06 15:25:16 +03002361};
2362
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002363/* Alpine Ridge */
2364static const struct tb_cm_ops icm_ar_ops = {
2365 .driver_ready = icm_driver_ready,
2366 .start = icm_start,
2367 .stop = icm_stop,
2368 .suspend = icm_suspend,
2369 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002370 .runtime_suspend = icm_runtime_suspend,
2371 .runtime_resume = icm_runtime_resume,
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002372 .runtime_suspend_switch = icm_runtime_suspend_switch,
2373 .runtime_resume_switch = icm_runtime_resume_switch,
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002374 .handle_event = icm_handle_event,
2375 .get_boot_acl = icm_ar_get_boot_acl,
2376 .set_boot_acl = icm_ar_set_boot_acl,
2377 .approve_switch = icm_fr_approve_switch,
2378 .add_switch_key = icm_fr_add_switch_key,
2379 .challenge_switch_key = icm_fr_challenge_switch_key,
2380 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2381 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2382 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
2383};
2384
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002385/* Titan Ridge */
2386static const struct tb_cm_ops icm_tr_ops = {
2387 .driver_ready = icm_driver_ready,
2388 .start = icm_start,
2389 .stop = icm_stop,
2390 .suspend = icm_suspend,
2391 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002392 .runtime_suspend = icm_runtime_suspend,
2393 .runtime_resume = icm_runtime_resume,
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002394 .runtime_suspend_switch = icm_runtime_suspend_switch,
2395 .runtime_resume_switch = icm_runtime_resume_switch,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002396 .handle_event = icm_handle_event,
2397 .get_boot_acl = icm_ar_get_boot_acl,
2398 .set_boot_acl = icm_ar_set_boot_acl,
2399 .approve_switch = icm_tr_approve_switch,
2400 .add_switch_key = icm_tr_add_switch_key,
2401 .challenge_switch_key = icm_tr_challenge_switch_key,
2402 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2403 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2404 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
Mika Westerberg90393872020-11-03 13:55:32 +02002405 .usb4_switch_op = icm_usb4_switch_op,
2406 .usb4_switch_nvm_authenticate_status =
2407 icm_usb4_switch_nvm_authenticate_status,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002408};
2409
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002410/* Ice Lake */
2411static const struct tb_cm_ops icm_icl_ops = {
2412 .driver_ready = icm_driver_ready,
2413 .start = icm_start,
2414 .stop = icm_stop,
2415 .complete = icm_complete,
2416 .runtime_suspend = icm_runtime_suspend,
2417 .runtime_resume = icm_runtime_resume,
2418 .handle_event = icm_handle_event,
2419 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2420 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
Mika Westerberg90393872020-11-03 13:55:32 +02002421 .usb4_switch_op = icm_usb4_switch_op,
2422 .usb4_switch_nvm_authenticate_status =
2423 icm_usb4_switch_nvm_authenticate_status,
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002424};
2425
Mika Westerbergf67cf492017-06-06 15:25:16 +03002426struct tb *icm_probe(struct tb_nhi *nhi)
2427{
2428 struct icm *icm;
2429 struct tb *tb;
2430
Mika Westerberg7f0a34d2020-12-29 13:44:57 +02002431 tb = tb_domain_alloc(nhi, ICM_TIMEOUT, sizeof(struct icm));
Mika Westerbergf67cf492017-06-06 15:25:16 +03002432 if (!tb)
2433 return NULL;
2434
2435 icm = tb_priv(tb);
2436 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
2437 mutex_init(&icm->request_lock);
2438
2439 switch (nhi->pdev->device) {
2440 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2441 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
Mika Westerbergf437c242018-01-16 22:30:40 +02002442 icm->can_upgrade_nvm = true;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002443 icm->is_supported = icm_fr_is_supported;
2444 icm->get_route = icm_fr_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03002445 icm->save_devices = icm_fr_save_devices;
Mika Westerberg3080e192017-10-06 18:14:13 +03002446 icm->driver_ready = icm_fr_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002447 icm->device_connected = icm_fr_device_connected;
2448 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002449 icm->xdomain_connected = icm_fr_xdomain_connected;
2450 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002451 tb->cm_ops = &icm_fr_ops;
2452 break;
2453
2454 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
2455 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
2456 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
2457 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
2458 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002459 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
Mika Westerbergf437c242018-01-16 22:30:40 +02002460 /*
2461 * NVM upgrade has not been tested on Apple systems and
2462 * they don't provide images publicly either. To be on
2463 * the safe side prevent root switch NVM upgrade on Macs
2464 * for now.
2465 */
2466 icm->can_upgrade_nvm = !x86_apple_machine;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002467 icm->is_supported = icm_ar_is_supported;
Mika Westerberg0d538272019-05-31 13:31:54 +03002468 icm->cio_reset = icm_ar_cio_reset;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002469 icm->get_mode = icm_ar_get_mode;
2470 icm->get_route = icm_ar_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03002471 icm->save_devices = icm_fr_save_devices;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002472 icm->driver_ready = icm_ar_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002473 icm->device_connected = icm_fr_device_connected;
2474 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002475 icm->xdomain_connected = icm_fr_xdomain_connected;
2476 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002477 tb->cm_ops = &icm_ar_ops;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002478 break;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002479
2480 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2481 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2482 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
Mika Westerbergf437c242018-01-16 22:30:40 +02002483 icm->can_upgrade_nvm = !x86_apple_machine;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002484 icm->is_supported = icm_ar_is_supported;
Mika Westerberg0d538272019-05-31 13:31:54 +03002485 icm->cio_reset = icm_tr_cio_reset;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002486 icm->get_mode = icm_ar_get_mode;
2487 icm->driver_ready = icm_tr_driver_ready;
2488 icm->device_connected = icm_tr_device_connected;
2489 icm->device_disconnected = icm_tr_device_disconnected;
2490 icm->xdomain_connected = icm_tr_xdomain_connected;
2491 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2492 tb->cm_ops = &icm_tr_ops;
2493 break;
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002494
2495 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
2496 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
Mika Westerberg354a7a72019-03-21 15:31:14 +02002497 icm->is_supported = icm_fr_is_supported;
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002498 icm->driver_ready = icm_icl_driver_ready;
2499 icm->set_uuid = icm_icl_set_uuid;
2500 icm->device_connected = icm_icl_device_connected;
2501 icm->device_disconnected = icm_tr_device_disconnected;
2502 icm->xdomain_connected = icm_tr_xdomain_connected;
2503 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2504 icm->rtd3_veto = icm_icl_rtd3_veto;
2505 tb->cm_ops = &icm_icl_ops;
2506 break;
Mika Westerberg57d8df62018-09-14 12:58:37 +03002507
2508 case PCI_DEVICE_ID_INTEL_TGL_NHI0:
2509 case PCI_DEVICE_ID_INTEL_TGL_NHI1:
Mika Westerbergf6439c52020-03-28 11:59:11 +02002510 case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
2511 case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
Azhar Shaikh13579482021-04-22 14:46:16 -07002512 case PCI_DEVICE_ID_INTEL_ADL_NHI0:
2513 case PCI_DEVICE_ID_INTEL_ADL_NHI1:
Mika Westerberg57d8df62018-09-14 12:58:37 +03002514 icm->is_supported = icm_tgl_is_supported;
2515 icm->driver_ready = icm_icl_driver_ready;
2516 icm->set_uuid = icm_icl_set_uuid;
2517 icm->device_connected = icm_icl_device_connected;
2518 icm->device_disconnected = icm_tr_device_disconnected;
2519 icm->xdomain_connected = icm_tr_xdomain_connected;
2520 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2521 icm->rtd3_veto = icm_icl_rtd3_veto;
2522 tb->cm_ops = &icm_icl_ops;
2523 break;
Mika Westerbergdb0746e2020-01-31 19:24:30 +03002524
2525 case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_4C_NHI:
2526 icm->is_supported = icm_tgl_is_supported;
2527 icm->get_mode = icm_ar_get_mode;
2528 icm->driver_ready = icm_tr_driver_ready;
2529 icm->device_connected = icm_tr_device_connected;
2530 icm->device_disconnected = icm_tr_device_disconnected;
2531 icm->xdomain_connected = icm_tr_xdomain_connected;
2532 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2533 tb->cm_ops = &icm_tr_ops;
2534 break;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002535 }
2536
2537 if (!icm->is_supported || !icm->is_supported(tb)) {
2538 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
2539 tb_domain_put(tb);
2540 return NULL;
2541 }
2542
Mika Westerberge0258802020-11-10 11:02:31 +03002543 tb_dbg(tb, "using firmware connection manager\n");
2544
Mika Westerbergf67cf492017-06-06 15:25:16 +03002545 return tb;
2546}