blob: adc7b61937a107f9428797f8bb5fe49609c5f54f [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
Mika Westerbergd1ff7022017-10-02 13:38:34 +030088 * @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 Westerbergd1ff7022017-10-02 13:38:34 +0300560static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
561{
562 struct icm_fr_pkg_approve_xdomain_response reply;
563 struct icm_fr_pkg_approve_xdomain request;
564 int ret;
565
566 memset(&request, 0, sizeof(request));
567 request.hdr.code = ICM_APPROVE_XDOMAIN;
568 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
569 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
570
571 request.transmit_path = xd->transmit_path;
572 request.transmit_ring = xd->transmit_ring;
573 request.receive_path = xd->receive_path;
574 request.receive_ring = xd->receive_ring;
575
576 memset(&reply, 0, sizeof(reply));
577 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
578 1, ICM_TIMEOUT);
579 if (ret)
580 return ret;
581
582 if (reply.hdr.flags & ICM_FLAGS_ERROR)
583 return -EIO;
584
585 return 0;
586}
587
588static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
589{
590 u8 phy_port;
591 u8 cmd;
592
593 phy_port = tb_phy_port_from_link(xd->link);
594 if (phy_port == 0)
595 cmd = NHI_MAILBOX_DISCONNECT_PA;
596 else
597 cmd = NHI_MAILBOX_DISCONNECT_PB;
598
599 nhi_mailbox_cmd(tb->nhi, cmd, 1);
600 usleep_range(10, 50);
601 nhi_mailbox_cmd(tb->nhi, cmd, 2);
602 return 0;
603}
604
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300605static struct tb_switch *alloc_switch(struct tb_switch *parent_sw, u64 route,
606 const uuid_t *uuid)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300607{
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300608 struct tb *tb = parent_sw->tb;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300609 struct tb_switch *sw;
610
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300611 sw = tb_switch_alloc(tb, &parent_sw->dev, route);
612 if (IS_ERR(sw)) {
613 tb_warn(tb, "failed to allocate switch at %llx\n", route);
614 return sw;
615 }
Mika Westerbergee487dd2017-10-04 14:42:20 +0300616
617 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
Aditya Pakkifd21b792019-03-25 16:25:22 -0500618 if (!sw->uuid) {
Aditya Pakkifd21b792019-03-25 16:25:22 -0500619 tb_switch_put(sw);
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300620 return ERR_PTR(-ENOMEM);
Aditya Pakkifd21b792019-03-25 16:25:22 -0500621 }
Mika Westerbergee487dd2017-10-04 14:42:20 +0300622
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300623 init_completion(&sw->rpm_complete);
624 return sw;
625}
626
627static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw)
628{
629 u64 route = tb_route(sw);
630 int ret;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300631
Mika Westerbergee487dd2017-10-04 14:42:20 +0300632 /* Link the two switches now */
633 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
634 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
635
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200636 ret = tb_switch_add(sw);
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300637 if (ret)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300638 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300639
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300640 return ret;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300641}
642
643static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
644 u64 route, u8 connection_id, u8 connection_key,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200645 u8 link, u8 depth, bool boot)
Mika Westerbergee487dd2017-10-04 14:42:20 +0300646{
647 /* Disconnect from parent */
648 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
649 /* Re-connect via updated port*/
650 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
651
652 /* Update with the new addressing information */
653 sw->config.route_hi = upper_32_bits(route);
654 sw->config.route_lo = lower_32_bits(route);
655 sw->connection_id = connection_id;
656 sw->connection_key = connection_key;
657 sw->link = link;
658 sw->depth = depth;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200659 sw->boot = boot;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300660
661 /* This switch still exists */
662 sw->is_unplugged = false;
Mika Westerberg4f7c2e02019-05-28 18:56:20 +0300663
664 /* Runtime resume is now complete */
665 complete(&sw->rpm_complete);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300666}
667
Mika Westerbergf67cf492017-06-06 15:25:16 +0300668static void remove_switch(struct tb_switch *sw)
669{
670 struct tb_switch *parent_sw;
671
672 parent_sw = tb_to_switch(sw->dev.parent);
673 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
674 tb_switch_remove(sw);
675}
676
Mika Westerbergee487dd2017-10-04 14:42:20 +0300677static void add_xdomain(struct tb_switch *sw, u64 route,
678 const uuid_t *local_uuid, const uuid_t *remote_uuid,
679 u8 link, u8 depth)
680{
681 struct tb_xdomain *xd;
682
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300683 pm_runtime_get_sync(&sw->dev);
684
Mika Westerbergee487dd2017-10-04 14:42:20 +0300685 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
686 if (!xd)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300687 goto out;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300688
689 xd->link = link;
690 xd->depth = depth;
691
692 tb_port_at(route, sw)->xdomain = xd;
693
694 tb_xdomain_add(xd);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300695
696out:
697 pm_runtime_mark_last_busy(&sw->dev);
698 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerbergee487dd2017-10-04 14:42:20 +0300699}
700
701static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
702{
703 xd->link = link;
704 xd->route = route;
705 xd->is_unplugged = false;
706}
707
Mika Westerberg79fae982018-02-09 17:29:38 +0300708static void remove_xdomain(struct tb_xdomain *xd)
709{
710 struct tb_switch *sw;
711
712 sw = tb_to_switch(xd->dev.parent);
713 tb_port_at(xd->route, sw)->xdomain = NULL;
714 tb_xdomain_remove(xd);
715}
716
Mika Westerbergf67cf492017-06-06 15:25:16 +0300717static void
718icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
719{
720 const struct icm_fr_event_device_connected *pkg =
721 (const struct icm_fr_event_device_connected *)hdr;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300722 enum tb_security_level security_level;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300723 struct tb_switch *sw, *parent_sw;
Mika Westerberg91c0c122019-03-21 19:03:00 +0200724 bool boot, dual_lane, speed_gen3;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300725 struct icm *icm = tb_priv(tb);
726 bool authorized = false;
Mika Westerberg79fae982018-02-09 17:29:38 +0300727 struct tb_xdomain *xd;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300728 u8 link, depth;
729 u64 route;
730 int ret;
731
Mika Westerberg3cdb9442018-01-16 22:19:00 +0200732 icm_postpone_rescan(tb);
733
Mika Westerbergf67cf492017-06-06 15:25:16 +0300734 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
735 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
736 ICM_LINK_INFO_DEPTH_SHIFT;
737 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
Mika Westerbergee487dd2017-10-04 14:42:20 +0300738 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
739 ICM_FLAGS_SLEVEL_SHIFT;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200740 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
Mika Westerberg91c0c122019-03-21 19:03:00 +0200741 dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
742 speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300743
Mika Westerbergcb653ee2017-09-14 13:59:10 +0300744 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
745 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
746 link, depth);
747 return;
748 }
749
Mika Westerbergf67cf492017-06-06 15:25:16 +0300750 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
751 if (sw) {
752 u8 phy_port, sw_phy_port;
753
754 parent_sw = tb_to_switch(sw->dev.parent);
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300755 sw_phy_port = tb_phy_port_from_link(sw->link);
756 phy_port = tb_phy_port_from_link(link);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300757
758 /*
759 * On resume ICM will send us connected events for the
760 * devices that still are present. However, that
761 * information might have changed for example by the
762 * fact that a switch on a dual-link connection might
763 * have been enumerated using the other link now. Make
764 * sure our book keeping matches that.
765 */
766 if (sw->depth == depth && sw_phy_port == phy_port &&
767 !!sw->authorized == authorized) {
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300768 /*
769 * It was enumerated through another link so update
770 * route string accordingly.
771 */
772 if (sw->link != link) {
773 ret = icm->get_route(tb, link, depth, &route);
774 if (ret) {
775 tb_err(tb, "failed to update route string for switch at %u.%u\n",
776 link, depth);
777 tb_switch_put(sw);
778 return;
779 }
780 } else {
781 route = tb_route(sw);
782 }
783
Mika Westerbergee487dd2017-10-04 14:42:20 +0300784 update_switch(parent_sw, sw, route, pkg->connection_id,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200785 pkg->connection_key, link, depth, boot);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300786 tb_switch_put(sw);
787 return;
788 }
789
790 /*
791 * User connected the same switch to another physical
792 * port or to another part of the topology. Remove the
793 * existing switch now before adding the new one.
794 */
795 remove_switch(sw);
796 tb_switch_put(sw);
797 }
798
799 /*
800 * If the switch was not found by UUID, look for a switch on
801 * same physical port (taking possible link aggregation into
802 * account) and depth. If we found one it is definitely a stale
803 * one so remove it first.
804 */
805 sw = tb_switch_find_by_link_depth(tb, link, depth);
806 if (!sw) {
807 u8 dual_link;
808
809 dual_link = dual_link_from_link(link);
810 if (dual_link)
811 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
812 }
813 if (sw) {
814 remove_switch(sw);
815 tb_switch_put(sw);
816 }
817
Mika Westerberg79fae982018-02-09 17:29:38 +0300818 /* Remove existing XDomain connection if found */
819 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
820 if (xd) {
821 remove_xdomain(xd);
822 tb_xdomain_put(xd);
823 }
824
Mika Westerbergf67cf492017-06-06 15:25:16 +0300825 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
826 if (!parent_sw) {
827 tb_err(tb, "failed to find parent switch for %u.%u\n",
828 link, depth);
829 return;
830 }
831
Mika Westerbergfdd92e82018-07-25 11:03:17 +0300832 ret = icm->get_route(tb, link, depth, &route);
833 if (ret) {
834 tb_err(tb, "failed to find route string for switch at %u.%u\n",
835 link, depth);
836 tb_switch_put(parent_sw);
837 return;
838 }
839
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300840 pm_runtime_get_sync(&parent_sw->dev);
841
842 sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
843 if (!IS_ERR(sw)) {
844 sw->connection_id = pkg->connection_id;
845 sw->connection_key = pkg->connection_key;
846 sw->link = link;
847 sw->depth = depth;
848 sw->authorized = authorized;
849 sw->security_level = security_level;
850 sw->boot = boot;
Mika Westerberg91c0c122019-03-21 19:03:00 +0200851 sw->link_speed = speed_gen3 ? 20 : 10;
852 sw->link_width = dual_lane ? 2 : 1;
Mika Westerbergb5db76d2019-10-08 19:03:34 +0300853 sw->rpm = intel_vss_is_rtd3(pkg->ep_name, sizeof(pkg->ep_name));
854
855 if (add_switch(parent_sw, sw))
856 tb_switch_put(sw);
857 }
858
859 pm_runtime_mark_last_busy(&parent_sw->dev);
860 pm_runtime_put_autosuspend(&parent_sw->dev);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300861
Mika Westerbergf67cf492017-06-06 15:25:16 +0300862 tb_switch_put(parent_sw);
863}
864
865static void
866icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
867{
868 const struct icm_fr_event_device_disconnected *pkg =
869 (const struct icm_fr_event_device_disconnected *)hdr;
870 struct tb_switch *sw;
871 u8 link, depth;
872
873 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
874 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
875 ICM_LINK_INFO_DEPTH_SHIFT;
876
Mika Westerbergf0342e72018-12-30 12:14:46 +0200877 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300878 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
879 return;
880 }
881
882 sw = tb_switch_find_by_link_depth(tb, link, depth);
883 if (!sw) {
884 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
885 depth);
886 return;
887 }
888
Mika Westerbergb658eb92020-11-10 11:04:31 +0300889 pm_runtime_get_sync(sw->dev.parent);
890
Mika Westerbergf67cf492017-06-06 15:25:16 +0300891 remove_switch(sw);
Mika Westerbergb658eb92020-11-10 11:04:31 +0300892
893 pm_runtime_mark_last_busy(sw->dev.parent);
894 pm_runtime_put_autosuspend(sw->dev.parent);
895
Mika Westerbergf67cf492017-06-06 15:25:16 +0300896 tb_switch_put(sw);
897}
898
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300899static void
900icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
901{
902 const struct icm_fr_event_xdomain_connected *pkg =
903 (const struct icm_fr_event_xdomain_connected *)hdr;
904 struct tb_xdomain *xd;
905 struct tb_switch *sw;
906 u8 link, depth;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300907 u64 route;
908
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300909 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
910 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
911 ICM_LINK_INFO_DEPTH_SHIFT;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300912
Mika Westerbergf0342e72018-12-30 12:14:46 +0200913 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300914 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
915 return;
916 }
917
918 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
919
920 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
921 if (xd) {
922 u8 xd_phy_port, phy_port;
923
924 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
925 phy_port = phy_port_from_route(route, depth);
926
927 if (xd->depth == depth && xd_phy_port == phy_port) {
Mika Westerbergee487dd2017-10-04 14:42:20 +0300928 update_xdomain(xd, route, link);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300929 tb_xdomain_put(xd);
930 return;
931 }
932
933 /*
934 * If we find an existing XDomain connection remove it
935 * now. We need to go through login handshake and
936 * everything anyway to be able to re-establish the
937 * connection.
938 */
939 remove_xdomain(xd);
940 tb_xdomain_put(xd);
941 }
942
943 /*
944 * Look if there already exists an XDomain in the same place
945 * than the new one and in that case remove it because it is
946 * most likely another host that got disconnected.
947 */
948 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
949 if (!xd) {
950 u8 dual_link;
951
952 dual_link = dual_link_from_link(link);
953 if (dual_link)
954 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
955 depth);
956 }
957 if (xd) {
958 remove_xdomain(xd);
959 tb_xdomain_put(xd);
960 }
961
962 /*
963 * If the user disconnected a switch during suspend and
964 * connected another host to the same port, remove the switch
965 * first.
966 */
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200967 sw = tb_switch_find_by_route(tb, route);
968 if (sw) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300969 remove_switch(sw);
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200970 tb_switch_put(sw);
971 }
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300972
973 sw = tb_switch_find_by_link_depth(tb, link, depth);
974 if (!sw) {
975 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
976 depth);
977 return;
978 }
979
Mika Westerbergee487dd2017-10-04 14:42:20 +0300980 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
981 depth);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300982 tb_switch_put(sw);
983}
984
985static void
986icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
987{
988 const struct icm_fr_event_xdomain_disconnected *pkg =
989 (const struct icm_fr_event_xdomain_disconnected *)hdr;
990 struct tb_xdomain *xd;
991
992 /*
993 * If the connection is through one or multiple devices, the
994 * XDomain device is removed along with them so it is fine if we
995 * cannot find it here.
996 */
997 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
998 if (xd) {
999 remove_xdomain(xd);
1000 tb_xdomain_put(xd);
1001 }
1002}
1003
Mika Westerberg0d538272019-05-31 13:31:54 +03001004static int icm_tr_cio_reset(struct tb *tb)
1005{
1006 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
1007}
1008
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001009static int
1010icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001011 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001012{
1013 struct icm_tr_pkg_driver_ready_response reply;
1014 struct icm_pkg_driver_ready request = {
1015 .hdr.code = ICM_DRIVER_READY,
1016 };
1017 int ret;
1018
1019 memset(&reply, 0, sizeof(reply));
1020 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1021 1, 20000);
1022 if (ret)
1023 return ret;
1024
1025 if (security_level)
1026 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
Mika Westerberg90393872020-11-03 13:55:32 +02001027 if (proto_version)
1028 *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
1029 ICM_TR_INFO_PROTO_VERSION_SHIFT;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001030 if (nboot_acl)
1031 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
1032 ICM_TR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001033 if (rpm)
1034 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
1035
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001036 return 0;
1037}
1038
1039static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
1040{
1041 struct icm_tr_pkg_approve_device request;
1042 struct icm_tr_pkg_approve_device reply;
1043 int ret;
1044
1045 memset(&request, 0, sizeof(request));
1046 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1047 request.hdr.code = ICM_APPROVE_DEVICE;
1048 request.route_lo = sw->config.route_lo;
1049 request.route_hi = sw->config.route_hi;
1050 request.connection_id = sw->connection_id;
1051
1052 memset(&reply, 0, sizeof(reply));
1053 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1054 1, ICM_APPROVE_TIMEOUT);
1055 if (ret)
1056 return ret;
1057
1058 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1059 tb_warn(tb, "PCIe tunnel creation failed\n");
1060 return -EIO;
1061 }
1062
1063 return 0;
1064}
1065
1066static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
1067{
1068 struct icm_tr_pkg_add_device_key_response reply;
1069 struct icm_tr_pkg_add_device_key request;
1070 int ret;
1071
1072 memset(&request, 0, sizeof(request));
1073 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1074 request.hdr.code = ICM_ADD_DEVICE_KEY;
1075 request.route_lo = sw->config.route_lo;
1076 request.route_hi = sw->config.route_hi;
1077 request.connection_id = sw->connection_id;
1078 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
1079
1080 memset(&reply, 0, sizeof(reply));
1081 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1082 1, ICM_TIMEOUT);
1083 if (ret)
1084 return ret;
1085
1086 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1087 tb_warn(tb, "Adding key to switch failed\n");
1088 return -EIO;
1089 }
1090
1091 return 0;
1092}
1093
1094static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
1095 const u8 *challenge, u8 *response)
1096{
1097 struct icm_tr_pkg_challenge_device_response reply;
1098 struct icm_tr_pkg_challenge_device request;
1099 int ret;
1100
1101 memset(&request, 0, sizeof(request));
1102 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1103 request.hdr.code = ICM_CHALLENGE_DEVICE;
1104 request.route_lo = sw->config.route_lo;
1105 request.route_hi = sw->config.route_hi;
1106 request.connection_id = sw->connection_id;
1107 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
1108
1109 memset(&reply, 0, sizeof(reply));
1110 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1111 1, ICM_TIMEOUT);
1112 if (ret)
1113 return ret;
1114
1115 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1116 return -EKEYREJECTED;
1117 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
1118 return -ENOKEY;
1119
1120 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
1121
1122 return 0;
1123}
1124
1125static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1126{
1127 struct icm_tr_pkg_approve_xdomain_response reply;
1128 struct icm_tr_pkg_approve_xdomain request;
1129 int ret;
1130
1131 memset(&request, 0, sizeof(request));
1132 request.hdr.code = ICM_APPROVE_XDOMAIN;
1133 request.route_hi = upper_32_bits(xd->route);
1134 request.route_lo = lower_32_bits(xd->route);
1135 request.transmit_path = xd->transmit_path;
1136 request.transmit_ring = xd->transmit_ring;
1137 request.receive_path = xd->receive_path;
1138 request.receive_ring = xd->receive_ring;
1139 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1140
1141 memset(&reply, 0, sizeof(reply));
1142 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1143 1, ICM_TIMEOUT);
1144 if (ret)
1145 return ret;
1146
1147 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1148 return -EIO;
1149
1150 return 0;
1151}
1152
1153static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
1154 int stage)
1155{
1156 struct icm_tr_pkg_disconnect_xdomain_response reply;
1157 struct icm_tr_pkg_disconnect_xdomain request;
1158 int ret;
1159
1160 memset(&request, 0, sizeof(request));
1161 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
1162 request.stage = stage;
1163 request.route_hi = upper_32_bits(xd->route);
1164 request.route_lo = lower_32_bits(xd->route);
1165 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1166
1167 memset(&reply, 0, sizeof(reply));
1168 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1169 1, ICM_TIMEOUT);
1170 if (ret)
1171 return ret;
1172
1173 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1174 return -EIO;
1175
1176 return 0;
1177}
1178
1179static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1180{
1181 int ret;
1182
1183 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1184 if (ret)
1185 return ret;
1186
1187 usleep_range(10, 50);
1188 return icm_tr_xdomain_tear_down(tb, xd, 2);
1189}
1190
1191static void
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001192__icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
1193 bool force_rtd3)
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001194{
1195 const struct icm_tr_event_device_connected *pkg =
1196 (const struct icm_tr_event_device_connected *)hdr;
Mika Westerberg91c0c122019-03-21 19:03:00 +02001197 bool authorized, boot, dual_lane, speed_gen3;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001198 enum tb_security_level security_level;
1199 struct tb_switch *sw, *parent_sw;
1200 struct tb_xdomain *xd;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001201 u64 route;
1202
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001203 icm_postpone_rescan(tb);
1204
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001205 /*
1206 * Currently we don't use the QoS information coming with the
1207 * device connected message so simply just ignore that extra
1208 * packet for now.
1209 */
1210 if (pkg->hdr.packet_id)
1211 return;
1212
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001213 route = get_route(pkg->route_hi, pkg->route_lo);
1214 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1215 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1216 ICM_FLAGS_SLEVEL_SHIFT;
1217 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
Mika Westerberg91c0c122019-03-21 19:03:00 +02001218 dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
1219 speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001220
1221 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1222 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1223 route);
1224 return;
1225 }
1226
1227 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1228 if (sw) {
1229 /* Update the switch if it is still in the same place */
1230 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1231 parent_sw = tb_to_switch(sw->dev.parent);
1232 update_switch(parent_sw, sw, route, pkg->connection_id,
1233 0, 0, 0, boot);
1234 tb_switch_put(sw);
1235 return;
1236 }
1237
1238 remove_switch(sw);
1239 tb_switch_put(sw);
1240 }
1241
1242 /* Another switch with the same address */
1243 sw = tb_switch_find_by_route(tb, route);
1244 if (sw) {
1245 remove_switch(sw);
1246 tb_switch_put(sw);
1247 }
1248
1249 /* XDomain connection with the same address */
1250 xd = tb_xdomain_find_by_route(tb, route);
1251 if (xd) {
1252 remove_xdomain(xd);
1253 tb_xdomain_put(xd);
1254 }
1255
1256 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1257 if (!parent_sw) {
1258 tb_err(tb, "failed to find parent switch for %llx\n", route);
1259 return;
1260 }
1261
Mika Westerbergb5db76d2019-10-08 19:03:34 +03001262 pm_runtime_get_sync(&parent_sw->dev);
1263
1264 sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
1265 if (!IS_ERR(sw)) {
1266 sw->connection_id = pkg->connection_id;
1267 sw->authorized = authorized;
1268 sw->security_level = security_level;
1269 sw->boot = boot;
Mika Westerberg91c0c122019-03-21 19:03:00 +02001270 sw->link_speed = speed_gen3 ? 20 : 10;
1271 sw->link_width = dual_lane ? 2 : 1;
Mika Westerbergb5db76d2019-10-08 19:03:34 +03001272 sw->rpm = force_rtd3;
1273 if (!sw->rpm)
1274 sw->rpm = intel_vss_is_rtd3(pkg->ep_name,
1275 sizeof(pkg->ep_name));
1276
1277 if (add_switch(parent_sw, sw))
1278 tb_switch_put(sw);
1279 }
1280
1281 pm_runtime_mark_last_busy(&parent_sw->dev);
1282 pm_runtime_put_autosuspend(&parent_sw->dev);
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001283
1284 tb_switch_put(parent_sw);
1285}
1286
1287static void
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001288icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1289{
1290 __icm_tr_device_connected(tb, hdr, false);
1291}
1292
1293static void
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001294icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1295{
1296 const struct icm_tr_event_device_disconnected *pkg =
1297 (const struct icm_tr_event_device_disconnected *)hdr;
1298 struct tb_switch *sw;
1299 u64 route;
1300
1301 route = get_route(pkg->route_hi, pkg->route_lo);
1302
1303 sw = tb_switch_find_by_route(tb, route);
1304 if (!sw) {
1305 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1306 return;
1307 }
Mika Westerbergb658eb92020-11-10 11:04:31 +03001308 pm_runtime_get_sync(sw->dev.parent);
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001309
1310 remove_switch(sw);
Mika Westerbergb658eb92020-11-10 11:04:31 +03001311
1312 pm_runtime_mark_last_busy(sw->dev.parent);
1313 pm_runtime_put_autosuspend(sw->dev.parent);
1314
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001315 tb_switch_put(sw);
1316}
1317
1318static void
1319icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1320{
1321 const struct icm_tr_event_xdomain_connected *pkg =
1322 (const struct icm_tr_event_xdomain_connected *)hdr;
1323 struct tb_xdomain *xd;
1324 struct tb_switch *sw;
1325 u64 route;
1326
1327 if (!tb->root_switch)
1328 return;
1329
1330 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1331
1332 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1333 if (xd) {
1334 if (xd->route == route) {
1335 update_xdomain(xd, route, 0);
1336 tb_xdomain_put(xd);
1337 return;
1338 }
1339
1340 remove_xdomain(xd);
1341 tb_xdomain_put(xd);
1342 }
1343
1344 /* An existing xdomain with the same address */
1345 xd = tb_xdomain_find_by_route(tb, route);
1346 if (xd) {
1347 remove_xdomain(xd);
1348 tb_xdomain_put(xd);
1349 }
1350
1351 /*
1352 * If the user disconnected a switch during suspend and
1353 * connected another host to the same port, remove the switch
1354 * first.
1355 */
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001356 sw = tb_switch_find_by_route(tb, route);
1357 if (sw) {
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001358 remove_switch(sw);
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001359 tb_switch_put(sw);
1360 }
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001361
1362 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1363 if (!sw) {
1364 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1365 return;
1366 }
1367
1368 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1369 tb_switch_put(sw);
1370}
1371
1372static void
1373icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1374{
1375 const struct icm_tr_event_xdomain_disconnected *pkg =
1376 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1377 struct tb_xdomain *xd;
1378 u64 route;
1379
1380 route = get_route(pkg->route_hi, pkg->route_lo);
1381
1382 xd = tb_xdomain_find_by_route(tb, route);
1383 if (xd) {
1384 remove_xdomain(xd);
1385 tb_xdomain_put(xd);
1386 }
1387}
1388
Mika Westerbergf67cf492017-06-06 15:25:16 +03001389static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1390{
1391 struct pci_dev *parent;
1392
1393 parent = pci_upstream_bridge(pdev);
1394 while (parent) {
1395 if (!pci_is_pcie(parent))
1396 return NULL;
1397 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1398 break;
1399 parent = pci_upstream_bridge(parent);
1400 }
1401
1402 if (!parent)
1403 return NULL;
1404
1405 switch (parent->device) {
1406 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1407 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1408 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1409 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1410 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
Mika Westerbergc4630d62019-01-21 16:41:27 +03001411 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1412 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
Mika Westerbergf67cf492017-06-06 15:25:16 +03001413 return parent;
1414 }
1415
1416 return NULL;
1417}
1418
1419static bool icm_ar_is_supported(struct tb *tb)
1420{
1421 struct pci_dev *upstream_port;
1422 struct icm *icm = tb_priv(tb);
1423
1424 /*
1425 * Starting from Alpine Ridge we can use ICM on Apple machines
1426 * as well. We just need to reset and re-enable it first.
Mika Westerberg354a7a72019-03-21 15:31:14 +02001427 * However, only start it if explicitly asked by the user.
Mika Westerbergf67cf492017-06-06 15:25:16 +03001428 */
Mika Westerberg354a7a72019-03-21 15:31:14 +02001429 if (icm_firmware_running(tb->nhi))
Mika Westerbergf67cf492017-06-06 15:25:16 +03001430 return true;
Mika Westerberg354a7a72019-03-21 15:31:14 +02001431 if (!start_icm)
1432 return false;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001433
1434 /*
1435 * Find the upstream PCIe port in case we need to do reset
1436 * through its vendor specific registers.
1437 */
1438 upstream_port = get_upstream_port(tb->nhi->pdev);
1439 if (upstream_port) {
1440 int cap;
1441
1442 cap = pci_find_ext_capability(upstream_port,
1443 PCI_EXT_CAP_ID_VNDR);
1444 if (cap > 0) {
1445 icm->upstream_port = upstream_port;
1446 icm->vnd_cap = cap;
1447
1448 return true;
1449 }
1450 }
1451
1452 return false;
1453}
1454
Mika Westerberg0d538272019-05-31 13:31:54 +03001455static int icm_ar_cio_reset(struct tb *tb)
1456{
1457 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
1458}
1459
Mika Westerbergf67cf492017-06-06 15:25:16 +03001460static int icm_ar_get_mode(struct tb *tb)
1461{
1462 struct tb_nhi *nhi = tb->nhi;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001463 int retries = 60;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001464 u32 val;
1465
1466 do {
1467 val = ioread32(nhi->iobase + REG_FW_STS);
1468 if (val & REG_FW_STS_NVM_AUTH_DONE)
1469 break;
Mika Westerberge4be8c92017-11-24 17:51:12 +03001470 msleep(50);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001471 } while (--retries);
1472
1473 if (!retries) {
1474 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1475 return -ENODEV;
1476 }
1477
1478 return nhi_mailbox_mode(nhi);
1479}
1480
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001481static int
1482icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001483 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001484{
1485 struct icm_ar_pkg_driver_ready_response reply;
1486 struct icm_pkg_driver_ready request = {
1487 .hdr.code = ICM_DRIVER_READY,
1488 };
1489 int ret;
1490
1491 memset(&reply, 0, sizeof(reply));
1492 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1493 1, ICM_TIMEOUT);
1494 if (ret)
1495 return ret;
1496
1497 if (security_level)
1498 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1499 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1500 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1501 ICM_AR_INFO_BOOT_ACL_SHIFT;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001502 if (rpm)
1503 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1504
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001505 return 0;
1506}
1507
Mika Westerbergf67cf492017-06-06 15:25:16 +03001508static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1509{
1510 struct icm_ar_pkg_get_route_response reply;
1511 struct icm_ar_pkg_get_route request = {
1512 .hdr = { .code = ICM_GET_ROUTE },
1513 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1514 };
1515 int ret;
1516
1517 memset(&reply, 0, sizeof(reply));
1518 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1519 1, ICM_TIMEOUT);
1520 if (ret)
1521 return ret;
1522
1523 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1524 return -EIO;
1525
1526 *route = get_route(reply.route_hi, reply.route_lo);
1527 return 0;
1528}
1529
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001530static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1531{
1532 struct icm_ar_pkg_preboot_acl_response reply;
1533 struct icm_ar_pkg_preboot_acl request = {
1534 .hdr = { .code = ICM_PREBOOT_ACL },
1535 };
1536 int ret, i;
1537
1538 memset(&reply, 0, sizeof(reply));
1539 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1540 1, ICM_TIMEOUT);
1541 if (ret)
1542 return ret;
1543
1544 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1545 return -EIO;
1546
1547 for (i = 0; i < nuuids; i++) {
1548 u32 *uuid = (u32 *)&uuids[i];
1549
1550 uuid[0] = reply.acl[i].uuid_lo;
1551 uuid[1] = reply.acl[i].uuid_hi;
1552
1553 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1554 /* Map empty entries to null UUID */
1555 uuid[0] = 0;
1556 uuid[1] = 0;
Mika Westerbergdd010bd2018-05-15 16:04:25 +03001557 } else if (uuid[0] != 0 || uuid[1] != 0) {
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001558 /* Upper two DWs are always one's */
1559 uuid[2] = 0xffffffff;
1560 uuid[3] = 0xffffffff;
1561 }
1562 }
1563
1564 return ret;
1565}
1566
1567static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1568 size_t nuuids)
1569{
1570 struct icm_ar_pkg_preboot_acl_response reply;
1571 struct icm_ar_pkg_preboot_acl request = {
1572 .hdr = {
1573 .code = ICM_PREBOOT_ACL,
1574 .flags = ICM_FLAGS_WRITE,
1575 },
1576 };
1577 int ret, i;
1578
1579 for (i = 0; i < nuuids; i++) {
1580 const u32 *uuid = (const u32 *)&uuids[i];
1581
1582 if (uuid_is_null(&uuids[i])) {
1583 /*
1584 * Map null UUID to the empty (all one) entries
1585 * for ICM.
1586 */
1587 request.acl[i].uuid_lo = 0xffffffff;
1588 request.acl[i].uuid_hi = 0xffffffff;
1589 } else {
1590 /* Two high DWs need to be set to all one */
1591 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1592 return -EINVAL;
1593
1594 request.acl[i].uuid_lo = uuid[0];
1595 request.acl[i].uuid_hi = uuid[1];
1596 }
1597 }
1598
1599 memset(&reply, 0, sizeof(reply));
1600 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1601 1, ICM_TIMEOUT);
1602 if (ret)
1603 return ret;
1604
1605 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1606 return -EIO;
1607
1608 return 0;
1609}
1610
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001611static int
1612icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001613 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001614{
1615 struct icm_tr_pkg_driver_ready_response reply;
1616 struct icm_pkg_driver_ready request = {
1617 .hdr.code = ICM_DRIVER_READY,
1618 };
1619 int ret;
1620
1621 memset(&reply, 0, sizeof(reply));
1622 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1623 1, 20000);
1624 if (ret)
1625 return ret;
1626
Mika Westerberg90393872020-11-03 13:55:32 +02001627 if (proto_version)
1628 *proto_version = (reply.info & ICM_TR_INFO_PROTO_VERSION_MASK) >>
1629 ICM_TR_INFO_PROTO_VERSION_SHIFT;
1630
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001631 /* Ice Lake always supports RTD3 */
1632 if (rpm)
1633 *rpm = true;
1634
1635 return 0;
1636}
1637
1638static void icm_icl_set_uuid(struct tb *tb)
1639{
1640 struct tb_nhi *nhi = tb->nhi;
1641 u32 uuid[4];
1642
1643 pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]);
1644 pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]);
1645 uuid[2] = 0xffffffff;
1646 uuid[3] = 0xffffffff;
1647
1648 tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1649}
1650
1651static void
1652icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1653{
1654 __icm_tr_device_connected(tb, hdr, true);
1655}
1656
1657static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr)
1658{
1659 const struct icm_icl_event_rtd3_veto *pkg =
1660 (const struct icm_icl_event_rtd3_veto *)hdr;
1661
1662 tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason);
1663
1664 if (pkg->veto_reason)
1665 icm_veto_begin(tb);
1666 else
1667 icm_veto_end(tb);
1668}
1669
Mika Westerberg57d8df62018-09-14 12:58:37 +03001670static bool icm_tgl_is_supported(struct tb *tb)
1671{
Mika Westerberg49f2a7f2020-06-17 19:04:09 +03001672 u32 val;
1673
Mika Westerberg57d8df62018-09-14 12:58:37 +03001674 /*
1675 * If the firmware is not running use software CM. This platform
1676 * should fully support both.
1677 */
Mika Westerberg49f2a7f2020-06-17 19:04:09 +03001678 val = ioread32(tb->nhi->iobase + REG_FW_STS);
1679 return !!(val & REG_FW_STS_NVM_AUTH_DONE);
Mika Westerberg57d8df62018-09-14 12:58:37 +03001680}
1681
Mika Westerbergf67cf492017-06-06 15:25:16 +03001682static void icm_handle_notification(struct work_struct *work)
1683{
1684 struct icm_notification *n = container_of(work, typeof(*n), work);
1685 struct tb *tb = n->tb;
1686 struct icm *icm = tb_priv(tb);
1687
1688 mutex_lock(&tb->lock);
1689
Mika Westerberg86da8092018-09-24 13:20:44 +03001690 /*
1691 * When the domain is stopped we flush its workqueue but before
1692 * that the root switch is removed. In that case we should treat
1693 * the queued events as being canceled.
1694 */
1695 if (tb->root_switch) {
1696 switch (n->pkg->code) {
1697 case ICM_EVENT_DEVICE_CONNECTED:
1698 icm->device_connected(tb, n->pkg);
1699 break;
1700 case ICM_EVENT_DEVICE_DISCONNECTED:
1701 icm->device_disconnected(tb, n->pkg);
1702 break;
1703 case ICM_EVENT_XDOMAIN_CONNECTED:
1704 icm->xdomain_connected(tb, n->pkg);
1705 break;
1706 case ICM_EVENT_XDOMAIN_DISCONNECTED:
1707 icm->xdomain_disconnected(tb, n->pkg);
1708 break;
Mika Westerberg3cdb9442018-01-16 22:19:00 +02001709 case ICM_EVENT_RTD3_VETO:
1710 icm->rtd3_veto(tb, n->pkg);
1711 break;
Mika Westerberg86da8092018-09-24 13:20:44 +03001712 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001713 }
1714
1715 mutex_unlock(&tb->lock);
1716
1717 kfree(n->pkg);
1718 kfree(n);
1719}
1720
1721static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1722 const void *buf, size_t size)
1723{
1724 struct icm_notification *n;
1725
1726 n = kmalloc(sizeof(*n), GFP_KERNEL);
1727 if (!n)
1728 return;
1729
1730 INIT_WORK(&n->work, icm_handle_notification);
1731 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1732 n->tb = tb;
1733
1734 queue_work(tb->wq, &n->work);
1735}
1736
1737static int
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001738__icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
Mika Westerberg90393872020-11-03 13:55:32 +02001739 u8 *proto_version, size_t *nboot_acl, bool *rpm)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001740{
Mika Westerberg3080e192017-10-06 18:14:13 +03001741 struct icm *icm = tb_priv(tb);
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001742 unsigned int retries = 50;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001743 int ret;
1744
Mika Westerberg90393872020-11-03 13:55:32 +02001745 ret = icm->driver_ready(tb, security_level, proto_version, nboot_acl,
1746 rpm);
Mika Westerberg3080e192017-10-06 18:14:13 +03001747 if (ret) {
1748 tb_err(tb, "failed to send driver ready to ICM\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001749 return ret;
Mika Westerberg3080e192017-10-06 18:14:13 +03001750 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001751
1752 /*
1753 * Hold on here until the switch config space is accessible so
1754 * that we can read root switch config successfully.
1755 */
1756 do {
1757 struct tb_cfg_result res;
1758 u32 tmp;
1759
1760 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1761 0, 1, 100);
1762 if (!res.err)
1763 return 0;
1764
1765 msleep(50);
1766 } while (--retries);
1767
Mika Westerberg44b51bb2017-09-14 13:44:07 +03001768 tb_err(tb, "failed to read root switch config space, giving up\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001769 return -ETIMEDOUT;
1770}
1771
Mika Westerbergf67cf492017-06-06 15:25:16 +03001772static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1773{
1774 struct icm *icm = tb_priv(tb);
1775 u32 val;
1776
Mika Westerbergea9d7bb2018-03-09 13:17:01 +03001777 if (!icm->upstream_port)
1778 return -ENODEV;
1779
Mika Westerbergf67cf492017-06-06 15:25:16 +03001780 /* Put ARC to wait for CIO reset event to happen */
1781 val = ioread32(nhi->iobase + REG_FW_STS);
1782 val |= REG_FW_STS_CIO_RESET_REQ;
1783 iowrite32(val, nhi->iobase + REG_FW_STS);
1784
1785 /* Re-start ARC */
1786 val = ioread32(nhi->iobase + REG_FW_STS);
1787 val |= REG_FW_STS_ICM_EN_INVERT;
1788 val |= REG_FW_STS_ICM_EN_CPU;
1789 iowrite32(val, nhi->iobase + REG_FW_STS);
1790
1791 /* Trigger CIO reset now */
Mika Westerberg0d538272019-05-31 13:31:54 +03001792 return icm->cio_reset(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001793}
1794
1795static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1796{
1797 unsigned int retries = 10;
1798 int ret;
1799 u32 val;
1800
1801 /* Check if the ICM firmware is already running */
Mika Westerberg354a7a72019-03-21 15:31:14 +02001802 if (icm_firmware_running(nhi))
Mika Westerbergf67cf492017-06-06 15:25:16 +03001803 return 0;
1804
Mika Westerberg62efe692018-09-17 16:32:13 +03001805 dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
Mika Westerbergf67cf492017-06-06 15:25:16 +03001806
1807 ret = icm_firmware_reset(tb, nhi);
1808 if (ret)
1809 return ret;
1810
1811 /* Wait until the ICM firmware tells us it is up and running */
1812 do {
1813 /* Check that the ICM firmware is running */
1814 val = ioread32(nhi->iobase + REG_FW_STS);
1815 if (val & REG_FW_STS_NVM_AUTH_DONE)
1816 return 0;
1817
1818 msleep(300);
1819 } while (--retries);
1820
1821 return -ETIMEDOUT;
1822}
1823
1824static int icm_reset_phy_port(struct tb *tb, int phy_port)
1825{
1826 struct icm *icm = tb_priv(tb);
1827 u32 state0, state1;
1828 int port0, port1;
1829 u32 val0, val1;
1830 int ret;
1831
1832 if (!icm->upstream_port)
1833 return 0;
1834
1835 if (phy_port) {
1836 port0 = 3;
1837 port1 = 4;
1838 } else {
1839 port0 = 1;
1840 port1 = 2;
1841 }
1842
1843 /*
1844 * Read link status of both null ports belonging to a single
1845 * physical port.
1846 */
1847 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1848 if (ret)
1849 return ret;
1850 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1851 if (ret)
1852 return ret;
1853
1854 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1855 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1856 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1857 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1858
1859 /* If they are both up we need to reset them now */
1860 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1861 return 0;
1862
1863 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1864 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1865 if (ret)
1866 return ret;
1867
1868 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1869 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1870 if (ret)
1871 return ret;
1872
1873 /* Wait a bit and then re-enable both ports */
1874 usleep_range(10, 100);
1875
1876 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1877 if (ret)
1878 return ret;
1879 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1880 if (ret)
1881 return ret;
1882
1883 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1884 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1885 if (ret)
1886 return ret;
1887
1888 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1889 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1890}
1891
1892static int icm_firmware_init(struct tb *tb)
1893{
1894 struct icm *icm = tb_priv(tb);
1895 struct tb_nhi *nhi = tb->nhi;
1896 int ret;
1897
1898 ret = icm_firmware_start(tb, nhi);
1899 if (ret) {
1900 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1901 return ret;
1902 }
1903
1904 if (icm->get_mode) {
1905 ret = icm->get_mode(tb);
1906
1907 switch (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001908 case NHI_FW_SAFE_MODE:
1909 icm->safe_mode = true;
1910 break;
1911
Mika Westerbergf67cf492017-06-06 15:25:16 +03001912 case NHI_FW_CM_MODE:
1913 /* Ask ICM to accept all Thunderbolt devices */
1914 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1915 break;
1916
1917 default:
Mika Westerberge4be8c92017-11-24 17:51:12 +03001918 if (ret < 0)
1919 return ret;
1920
Mika Westerbergf67cf492017-06-06 15:25:16 +03001921 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1922 return -ENODEV;
1923 }
1924 }
1925
1926 /*
1927 * Reset both physical ports if there is anything connected to
1928 * them already.
1929 */
1930 ret = icm_reset_phy_port(tb, 0);
1931 if (ret)
1932 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1933 ret = icm_reset_phy_port(tb, 1);
1934 if (ret)
1935 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1936
1937 return 0;
1938}
1939
1940static int icm_driver_ready(struct tb *tb)
1941{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001942 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001943 int ret;
1944
1945 ret = icm_firmware_init(tb);
1946 if (ret)
1947 return ret;
1948
Mika Westerberge6b245c2017-06-06 15:25:17 +03001949 if (icm->safe_mode) {
1950 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1951 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1952 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1953 return 0;
1954 }
1955
Mika Westerberg90393872020-11-03 13:55:32 +02001956 ret = __icm_driver_ready(tb, &tb->security_level, &icm->proto_version,
1957 &tb->nboot_acl, &icm->rpm);
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001958 if (ret)
1959 return ret;
1960
1961 /*
1962 * Make sure the number of supported preboot ACL matches what we
1963 * expect or disable the whole feature.
1964 */
1965 if (tb->nboot_acl > icm->max_boot_acl)
1966 tb->nboot_acl = 0;
1967
Mika Westerberg90393872020-11-03 13:55:32 +02001968 if (icm->proto_version >= 3)
1969 tb_dbg(tb, "USB4 proxy operations supported\n");
1970
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02001971 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001972}
1973
1974static int icm_suspend(struct tb *tb)
1975{
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001976 struct icm *icm = tb_priv(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001977
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001978 if (icm->save_devices)
1979 icm->save_devices(tb);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001980
Mika Westerbergd04522fa2018-07-25 11:03:19 +03001981 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
Rafael J. Wysockia684c5b2017-07-25 01:31:00 +02001982 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001983}
1984
1985/*
1986 * Mark all switches (except root switch) below this one unplugged. ICM
1987 * firmware will send us an updated list of switches after we have send
1988 * it driver ready command. If a switch is not in that list it will be
1989 * removed when we perform rescan.
1990 */
1991static void icm_unplug_children(struct tb_switch *sw)
1992{
Mika Westerbergb433d012019-09-30 14:07:22 +03001993 struct tb_port *port;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001994
1995 if (tb_route(sw))
1996 sw->is_unplugged = true;
1997
Mika Westerbergb433d012019-09-30 14:07:22 +03001998 tb_switch_for_each_port(sw, port) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001999 if (port->xdomain)
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002000 port->xdomain->is_unplugged = true;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002001 else if (tb_port_has_remote(port))
2002 icm_unplug_children(port->remote->sw);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002003 }
2004}
2005
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002006static int complete_rpm(struct device *dev, void *data)
2007{
2008 struct tb_switch *sw = tb_to_switch(dev);
2009
2010 if (sw)
2011 complete(&sw->rpm_complete);
2012 return 0;
2013}
2014
2015static void remove_unplugged_switch(struct tb_switch *sw)
2016{
2017 pm_runtime_get_sync(sw->dev.parent);
2018
2019 /*
2020 * Signal this and switches below for rpm_complete because
2021 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
2022 * for it.
2023 */
2024 complete_rpm(&sw->dev, NULL);
2025 bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
2026 tb_switch_remove(sw);
2027
2028 pm_runtime_mark_last_busy(sw->dev.parent);
2029 pm_runtime_put_autosuspend(sw->dev.parent);
2030}
2031
Mika Westerbergf67cf492017-06-06 15:25:16 +03002032static void icm_free_unplugged_children(struct tb_switch *sw)
2033{
Mika Westerbergb433d012019-09-30 14:07:22 +03002034 struct tb_port *port;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002035
Mika Westerbergb433d012019-09-30 14:07:22 +03002036 tb_switch_for_each_port(sw, port) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002037 if (port->xdomain && port->xdomain->is_unplugged) {
2038 tb_xdomain_remove(port->xdomain);
2039 port->xdomain = NULL;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002040 } else if (tb_port_has_remote(port)) {
2041 if (port->remote->sw->is_unplugged) {
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002042 remove_unplugged_switch(port->remote->sw);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02002043 port->remote = NULL;
2044 } else {
2045 icm_free_unplugged_children(port->remote->sw);
2046 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03002047 }
2048 }
2049}
2050
2051static void icm_rescan_work(struct work_struct *work)
2052{
2053 struct icm *icm = container_of(work, struct icm, rescan_work.work);
2054 struct tb *tb = icm_to_tb(icm);
2055
2056 mutex_lock(&tb->lock);
2057 if (tb->root_switch)
2058 icm_free_unplugged_children(tb->root_switch);
2059 mutex_unlock(&tb->lock);
2060}
2061
2062static void icm_complete(struct tb *tb)
2063{
2064 struct icm *icm = tb_priv(tb);
2065
2066 if (tb->nhi->going_away)
2067 return;
2068
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002069 /*
2070 * If RTD3 was vetoed before we entered system suspend allow it
2071 * again now before driver ready is sent. Firmware sends a new RTD3
2072 * veto if it is still the case after we have sent it driver ready
2073 * command.
2074 */
2075 icm_veto_end(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002076 icm_unplug_children(tb->root_switch);
2077
2078 /*
2079 * Now all existing children should be resumed, start events
2080 * from ICM to get updated status.
2081 */
Mika Westerberg90393872020-11-03 13:55:32 +02002082 __icm_driver_ready(tb, NULL, NULL, NULL, NULL);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002083
2084 /*
2085 * We do not get notifications of devices that have been
2086 * unplugged during suspend so schedule rescan to clean them up
2087 * if any.
2088 */
2089 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
2090}
2091
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002092static int icm_runtime_suspend(struct tb *tb)
2093{
2094 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
2095 return 0;
2096}
2097
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002098static int icm_runtime_suspend_switch(struct tb_switch *sw)
2099{
2100 if (tb_route(sw))
2101 reinit_completion(&sw->rpm_complete);
2102 return 0;
2103}
2104
2105static int icm_runtime_resume_switch(struct tb_switch *sw)
2106{
2107 if (tb_route(sw)) {
2108 if (!wait_for_completion_timeout(&sw->rpm_complete,
2109 msecs_to_jiffies(500))) {
2110 dev_dbg(&sw->dev, "runtime resuming timed out\n");
2111 }
2112 }
2113 return 0;
2114}
2115
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002116static int icm_runtime_resume(struct tb *tb)
2117{
2118 /*
2119 * We can reuse the same resume functionality than with system
2120 * suspend.
2121 */
2122 icm_complete(tb);
2123 return 0;
2124}
2125
Mika Westerbergf67cf492017-06-06 15:25:16 +03002126static int icm_start(struct tb *tb)
2127{
Mika Westerberge6b245c2017-06-06 15:25:17 +03002128 struct icm *icm = tb_priv(tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002129 int ret;
2130
Mika Westerberge6b245c2017-06-06 15:25:17 +03002131 if (icm->safe_mode)
2132 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
2133 else
2134 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Mika Westerberg444ac382018-12-30 12:17:52 +02002135 if (IS_ERR(tb->root_switch))
2136 return PTR_ERR(tb->root_switch);
Mika Westerbergf67cf492017-06-06 15:25:16 +03002137
Mika Westerbergf437c242018-01-16 22:30:40 +02002138 tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002139 tb->root_switch->rpm = icm->rpm;
Mika Westerberge6b245c2017-06-06 15:25:17 +03002140
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002141 if (icm->set_uuid)
2142 icm->set_uuid(tb);
2143
Mika Westerbergf67cf492017-06-06 15:25:16 +03002144 ret = tb_switch_add(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002145 if (ret) {
Mika Westerbergf67cf492017-06-06 15:25:16 +03002146 tb_switch_put(tb->root_switch);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002147 tb->root_switch = NULL;
2148 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03002149
2150 return ret;
2151}
2152
2153static void icm_stop(struct tb *tb)
2154{
2155 struct icm *icm = tb_priv(tb);
2156
2157 cancel_delayed_work(&icm->rescan_work);
2158 tb_switch_remove(tb->root_switch);
2159 tb->root_switch = NULL;
2160 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
Mika Westerberg90393872020-11-03 13:55:32 +02002161 kfree(icm->last_nvm_auth);
2162 icm->last_nvm_auth = NULL;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002163}
2164
Mika Westerberge6b245c2017-06-06 15:25:17 +03002165static int icm_disconnect_pcie_paths(struct tb *tb)
2166{
2167 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
2168}
2169
Mika Westerberg90393872020-11-03 13:55:32 +02002170static void icm_usb4_switch_nvm_auth_complete(void *data)
2171{
2172 struct usb4_switch_nvm_auth *auth = data;
2173 struct icm *icm = auth->icm;
2174 struct tb *tb = icm_to_tb(icm);
2175
2176 tb_dbg(tb, "NVM_AUTH response for %llx flags %#x status %#x\n",
2177 get_route(auth->reply.route_hi, auth->reply.route_lo),
2178 auth->reply.hdr.flags, auth->reply.status);
2179
2180 mutex_lock(&tb->lock);
2181 if (WARN_ON(icm->last_nvm_auth))
2182 kfree(icm->last_nvm_auth);
2183 icm->last_nvm_auth = auth;
2184 mutex_unlock(&tb->lock);
2185}
2186
2187static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
2188{
2189 struct usb4_switch_nvm_auth *auth;
2190 struct icm *icm = tb_priv(tb);
2191 struct tb_cfg_request *req;
2192 int ret;
2193
2194 auth = kzalloc(sizeof(*auth), GFP_KERNEL);
2195 if (!auth)
2196 return -ENOMEM;
2197
2198 auth->icm = icm;
2199 auth->request.hdr.code = ICM_USB4_SWITCH_OP;
2200 auth->request.route_hi = upper_32_bits(route);
2201 auth->request.route_lo = lower_32_bits(route);
2202 auth->request.opcode = USB4_SWITCH_OP_NVM_AUTH;
2203
2204 req = tb_cfg_request_alloc();
2205 if (!req) {
2206 ret = -ENOMEM;
2207 goto err_free_auth;
2208 }
2209
2210 req->match = icm_match;
2211 req->copy = icm_copy;
2212 req->request = &auth->request;
2213 req->request_size = sizeof(auth->request);
2214 req->request_type = TB_CFG_PKG_ICM_CMD;
2215 req->response = &auth->reply;
2216 req->npackets = 1;
2217 req->response_size = sizeof(auth->reply);
2218 req->response_type = TB_CFG_PKG_ICM_RESP;
2219
2220 tb_dbg(tb, "NVM_AUTH request for %llx\n", route);
2221
2222 mutex_lock(&icm->request_lock);
2223 ret = tb_cfg_request(tb->ctl, req, icm_usb4_switch_nvm_auth_complete,
2224 auth);
2225 mutex_unlock(&icm->request_lock);
2226
2227 tb_cfg_request_put(req);
2228 if (ret)
2229 goto err_free_auth;
2230 return 0;
2231
2232err_free_auth:
2233 kfree(auth);
2234 return ret;
2235}
2236
2237static int icm_usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
2238 u8 *status, const void *tx_data, size_t tx_data_len,
2239 void *rx_data, size_t rx_data_len)
2240{
2241 struct icm_usb4_switch_op_response reply;
2242 struct icm_usb4_switch_op request;
2243 struct tb *tb = sw->tb;
2244 struct icm *icm = tb_priv(tb);
2245 u64 route = tb_route(sw);
2246 int ret;
2247
2248 /*
2249 * USB4 router operation proxy is supported in firmware if the
2250 * protocol version is 3 or higher.
2251 */
2252 if (icm->proto_version < 3)
2253 return -EOPNOTSUPP;
2254
2255 /*
2256 * NVM_AUTH is a special USB4 proxy operation that does not
2257 * return immediately so handle it separately.
2258 */
2259 if (opcode == USB4_SWITCH_OP_NVM_AUTH)
2260 return icm_usb4_switch_nvm_authenticate(tb, route);
2261
2262 memset(&request, 0, sizeof(request));
2263 request.hdr.code = ICM_USB4_SWITCH_OP;
2264 request.route_hi = upper_32_bits(route);
2265 request.route_lo = lower_32_bits(route);
2266 request.opcode = opcode;
2267 if (metadata)
2268 request.metadata = *metadata;
2269
2270 if (tx_data_len) {
2271 request.data_len_valid |= ICM_USB4_SWITCH_DATA_VALID;
2272 if (tx_data_len < ARRAY_SIZE(request.data))
2273 request.data_len_valid =
2274 tx_data_len & ICM_USB4_SWITCH_DATA_LEN_MASK;
2275 memcpy(request.data, tx_data, tx_data_len * sizeof(u32));
2276 }
2277
2278 memset(&reply, 0, sizeof(reply));
2279 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
2280 1, ICM_TIMEOUT);
2281 if (ret)
2282 return ret;
2283
2284 if (reply.hdr.flags & ICM_FLAGS_ERROR)
2285 return -EIO;
2286
2287 if (status)
2288 *status = reply.status;
2289
2290 if (metadata)
2291 *metadata = reply.metadata;
2292
2293 if (rx_data_len)
2294 memcpy(rx_data, reply.data, rx_data_len * sizeof(u32));
2295
2296 return 0;
2297}
2298
2299static int icm_usb4_switch_nvm_authenticate_status(struct tb_switch *sw,
2300 u32 *status)
2301{
2302 struct usb4_switch_nvm_auth *auth;
2303 struct tb *tb = sw->tb;
2304 struct icm *icm = tb_priv(tb);
2305 int ret = 0;
2306
2307 if (icm->proto_version < 3)
2308 return -EOPNOTSUPP;
2309
2310 auth = icm->last_nvm_auth;
2311 icm->last_nvm_auth = NULL;
2312
2313 if (auth && auth->reply.route_hi == sw->config.route_hi &&
2314 auth->reply.route_lo == sw->config.route_lo) {
2315 tb_dbg(tb, "NVM_AUTH found for %llx flags 0x%#x status %#x\n",
2316 tb_route(sw), auth->reply.hdr.flags, auth->reply.status);
2317 if (auth->reply.hdr.flags & ICM_FLAGS_ERROR)
2318 ret = -EIO;
2319 else
2320 *status = auth->reply.status;
2321 } else {
2322 *status = 0;
2323 }
2324
2325 kfree(auth);
2326 return ret;
2327}
2328
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002329/* Falcon Ridge */
Mika Westerbergf67cf492017-06-06 15:25:16 +03002330static const struct tb_cm_ops icm_fr_ops = {
2331 .driver_ready = icm_driver_ready,
2332 .start = icm_start,
2333 .stop = icm_stop,
2334 .suspend = icm_suspend,
2335 .complete = icm_complete,
2336 .handle_event = icm_handle_event,
2337 .approve_switch = icm_fr_approve_switch,
2338 .add_switch_key = icm_fr_add_switch_key,
2339 .challenge_switch_key = icm_fr_challenge_switch_key,
Mika Westerberge6b245c2017-06-06 15:25:17 +03002340 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002341 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2342 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
Mika Westerbergf67cf492017-06-06 15:25:16 +03002343};
2344
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002345/* Alpine Ridge */
2346static const struct tb_cm_ops icm_ar_ops = {
2347 .driver_ready = icm_driver_ready,
2348 .start = icm_start,
2349 .stop = icm_stop,
2350 .suspend = icm_suspend,
2351 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002352 .runtime_suspend = icm_runtime_suspend,
2353 .runtime_resume = icm_runtime_resume,
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002354 .runtime_suspend_switch = icm_runtime_suspend_switch,
2355 .runtime_resume_switch = icm_runtime_resume_switch,
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002356 .handle_event = icm_handle_event,
2357 .get_boot_acl = icm_ar_get_boot_acl,
2358 .set_boot_acl = icm_ar_set_boot_acl,
2359 .approve_switch = icm_fr_approve_switch,
2360 .add_switch_key = icm_fr_add_switch_key,
2361 .challenge_switch_key = icm_fr_challenge_switch_key,
2362 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2363 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2364 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
2365};
2366
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002367/* Titan Ridge */
2368static const struct tb_cm_ops icm_tr_ops = {
2369 .driver_ready = icm_driver_ready,
2370 .start = icm_start,
2371 .stop = icm_stop,
2372 .suspend = icm_suspend,
2373 .complete = icm_complete,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03002374 .runtime_suspend = icm_runtime_suspend,
2375 .runtime_resume = icm_runtime_resume,
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03002376 .runtime_suspend_switch = icm_runtime_suspend_switch,
2377 .runtime_resume_switch = icm_runtime_resume_switch,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002378 .handle_event = icm_handle_event,
2379 .get_boot_acl = icm_ar_get_boot_acl,
2380 .set_boot_acl = icm_ar_set_boot_acl,
2381 .approve_switch = icm_tr_approve_switch,
2382 .add_switch_key = icm_tr_add_switch_key,
2383 .challenge_switch_key = icm_tr_challenge_switch_key,
2384 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2385 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2386 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
Mika Westerberg90393872020-11-03 13:55:32 +02002387 .usb4_switch_op = icm_usb4_switch_op,
2388 .usb4_switch_nvm_authenticate_status =
2389 icm_usb4_switch_nvm_authenticate_status,
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002390};
2391
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002392/* Ice Lake */
2393static const struct tb_cm_ops icm_icl_ops = {
2394 .driver_ready = icm_driver_ready,
2395 .start = icm_start,
2396 .stop = icm_stop,
2397 .complete = icm_complete,
2398 .runtime_suspend = icm_runtime_suspend,
2399 .runtime_resume = icm_runtime_resume,
2400 .handle_event = icm_handle_event,
2401 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2402 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
Mika Westerberg90393872020-11-03 13:55:32 +02002403 .usb4_switch_op = icm_usb4_switch_op,
2404 .usb4_switch_nvm_authenticate_status =
2405 icm_usb4_switch_nvm_authenticate_status,
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002406};
2407
Mika Westerbergf67cf492017-06-06 15:25:16 +03002408struct tb *icm_probe(struct tb_nhi *nhi)
2409{
2410 struct icm *icm;
2411 struct tb *tb;
2412
2413 tb = tb_domain_alloc(nhi, sizeof(struct icm));
2414 if (!tb)
2415 return NULL;
2416
2417 icm = tb_priv(tb);
2418 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
2419 mutex_init(&icm->request_lock);
2420
2421 switch (nhi->pdev->device) {
2422 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2423 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
Mika Westerbergf437c242018-01-16 22:30:40 +02002424 icm->can_upgrade_nvm = true;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002425 icm->is_supported = icm_fr_is_supported;
2426 icm->get_route = icm_fr_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03002427 icm->save_devices = icm_fr_save_devices;
Mika Westerberg3080e192017-10-06 18:14:13 +03002428 icm->driver_ready = icm_fr_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002429 icm->device_connected = icm_fr_device_connected;
2430 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002431 icm->xdomain_connected = icm_fr_xdomain_connected;
2432 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002433 tb->cm_ops = &icm_fr_ops;
2434 break;
2435
2436 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
2437 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
2438 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
2439 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
2440 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002441 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
Mika Westerbergf437c242018-01-16 22:30:40 +02002442 /*
2443 * NVM upgrade has not been tested on Apple systems and
2444 * they don't provide images publicly either. To be on
2445 * the safe side prevent root switch NVM upgrade on Macs
2446 * for now.
2447 */
2448 icm->can_upgrade_nvm = !x86_apple_machine;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002449 icm->is_supported = icm_ar_is_supported;
Mika Westerberg0d538272019-05-31 13:31:54 +03002450 icm->cio_reset = icm_ar_cio_reset;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002451 icm->get_mode = icm_ar_get_mode;
2452 icm->get_route = icm_ar_get_route;
Mika Westerbergd04522fa2018-07-25 11:03:19 +03002453 icm->save_devices = icm_fr_save_devices;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002454 icm->driver_ready = icm_ar_driver_ready;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002455 icm->device_connected = icm_fr_device_connected;
2456 icm->device_disconnected = icm_fr_device_disconnected;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002457 icm->xdomain_connected = icm_fr_xdomain_connected;
2458 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
Mika Westerberg9aaa3b82018-01-21 12:08:04 +02002459 tb->cm_ops = &icm_ar_ops;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002460 break;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002461
2462 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2463 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2464 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
Mika Westerbergf437c242018-01-16 22:30:40 +02002465 icm->can_upgrade_nvm = !x86_apple_machine;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002466 icm->is_supported = icm_ar_is_supported;
Mika Westerberg0d538272019-05-31 13:31:54 +03002467 icm->cio_reset = icm_tr_cio_reset;
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03002468 icm->get_mode = icm_ar_get_mode;
2469 icm->driver_ready = icm_tr_driver_ready;
2470 icm->device_connected = icm_tr_device_connected;
2471 icm->device_disconnected = icm_tr_device_disconnected;
2472 icm->xdomain_connected = icm_tr_xdomain_connected;
2473 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2474 tb->cm_ops = &icm_tr_ops;
2475 break;
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002476
2477 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
2478 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
Mika Westerberg354a7a72019-03-21 15:31:14 +02002479 icm->is_supported = icm_fr_is_supported;
Mika Westerberg3cdb9442018-01-16 22:19:00 +02002480 icm->driver_ready = icm_icl_driver_ready;
2481 icm->set_uuid = icm_icl_set_uuid;
2482 icm->device_connected = icm_icl_device_connected;
2483 icm->device_disconnected = icm_tr_device_disconnected;
2484 icm->xdomain_connected = icm_tr_xdomain_connected;
2485 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2486 icm->rtd3_veto = icm_icl_rtd3_veto;
2487 tb->cm_ops = &icm_icl_ops;
2488 break;
Mika Westerberg57d8df62018-09-14 12:58:37 +03002489
2490 case PCI_DEVICE_ID_INTEL_TGL_NHI0:
2491 case PCI_DEVICE_ID_INTEL_TGL_NHI1:
2492 icm->is_supported = icm_tgl_is_supported;
2493 icm->driver_ready = icm_icl_driver_ready;
2494 icm->set_uuid = icm_icl_set_uuid;
2495 icm->device_connected = icm_icl_device_connected;
2496 icm->device_disconnected = icm_tr_device_disconnected;
2497 icm->xdomain_connected = icm_tr_xdomain_connected;
2498 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2499 icm->rtd3_veto = icm_icl_rtd3_veto;
2500 tb->cm_ops = &icm_icl_ops;
2501 break;
Mika Westerbergdb0746e2020-01-31 19:24:30 +03002502
2503 case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_4C_NHI:
2504 icm->is_supported = icm_tgl_is_supported;
2505 icm->get_mode = icm_ar_get_mode;
2506 icm->driver_ready = icm_tr_driver_ready;
2507 icm->device_connected = icm_tr_device_connected;
2508 icm->device_disconnected = icm_tr_device_disconnected;
2509 icm->xdomain_connected = icm_tr_xdomain_connected;
2510 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2511 tb->cm_ops = &icm_tr_ops;
2512 break;
Mika Westerbergf67cf492017-06-06 15:25:16 +03002513 }
2514
2515 if (!icm->is_supported || !icm->is_supported(tb)) {
2516 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
2517 tb_domain_put(tb);
2518 return NULL;
2519 }
2520
Mika Westerberge0258802020-11-10 11:02:31 +03002521 tb_dbg(tb, "using firmware connection manager\n");
2522
Mika Westerbergf67cf492017-06-06 15:25:16 +03002523 return tb;
2524}