blob: 30f3d6a9fe90a0d86ba4a73193e34125405b8f40 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Noevera25c8b22014-06-03 22:04:02 +02002/*
Mika Westerberg15c67842018-10-01 12:31:22 +03003 * Thunderbolt driver - switch/port utility functions
Andreas Noevera25c8b22014-06-03 22:04:02 +02004 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
Mika Westerberg15c67842018-10-01 12:31:22 +03006 * Copyright (C) 2018, Intel Corporation
Andreas Noevera25c8b22014-06-03 22:04:02 +02007 */
8
9#include <linux/delay.h>
Mika Westerberge6b245c2017-06-06 15:25:17 +030010#include <linux/idr.h>
11#include <linux/nvmem-provider.h>
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030012#include <linux/pm_runtime.h>
Mika Westerberg09f11b62019-03-19 16:48:41 +020013#include <linux/sched/signal.h>
Mika Westerberge6b245c2017-06-06 15:25:17 +030014#include <linux/sizes.h>
Sachin Kamat10fefe52014-06-20 14:32:30 +053015#include <linux/slab.h>
Mika Westerberge6b245c2017-06-06 15:25:17 +030016#include <linux/vmalloc.h>
Andreas Noevera25c8b22014-06-03 22:04:02 +020017
18#include "tb.h"
19
Mika Westerberge6b245c2017-06-06 15:25:17 +030020/* Switch NVM support */
21
22#define NVM_DEVID 0x05
23#define NVM_VERSION 0x08
24#define NVM_CSS 0x10
25#define NVM_FLASH_SIZE 0x45
26
27#define NVM_MIN_SIZE SZ_32K
28#define NVM_MAX_SIZE SZ_512K
29
30static DEFINE_IDA(nvm_ida);
31
32struct nvm_auth_status {
33 struct list_head list;
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +020034 uuid_t uuid;
Mika Westerberge6b245c2017-06-06 15:25:17 +030035 u32 status;
36};
37
38/*
39 * Hold NVM authentication failure status per switch This information
40 * needs to stay around even when the switch gets power cycled so we
41 * keep it separately.
42 */
43static LIST_HEAD(nvm_auth_status_cache);
44static DEFINE_MUTEX(nvm_auth_status_lock);
45
46static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
47{
48 struct nvm_auth_status *st;
49
50 list_for_each_entry(st, &nvm_auth_status_cache, list) {
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +020051 if (uuid_equal(&st->uuid, sw->uuid))
Mika Westerberge6b245c2017-06-06 15:25:17 +030052 return st;
53 }
54
55 return NULL;
56}
57
58static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
59{
60 struct nvm_auth_status *st;
61
62 mutex_lock(&nvm_auth_status_lock);
63 st = __nvm_get_auth_status(sw);
64 mutex_unlock(&nvm_auth_status_lock);
65
66 *status = st ? st->status : 0;
67}
68
69static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
70{
71 struct nvm_auth_status *st;
72
73 if (WARN_ON(!sw->uuid))
74 return;
75
76 mutex_lock(&nvm_auth_status_lock);
77 st = __nvm_get_auth_status(sw);
78
79 if (!st) {
80 st = kzalloc(sizeof(*st), GFP_KERNEL);
81 if (!st)
82 goto unlock;
83
84 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
85 INIT_LIST_HEAD(&st->list);
86 list_add_tail(&st->list, &nvm_auth_status_cache);
87 }
88
89 st->status = status;
90unlock:
91 mutex_unlock(&nvm_auth_status_lock);
92}
93
94static void nvm_clear_auth_status(const struct tb_switch *sw)
95{
96 struct nvm_auth_status *st;
97
98 mutex_lock(&nvm_auth_status_lock);
99 st = __nvm_get_auth_status(sw);
100 if (st) {
101 list_del(&st->list);
102 kfree(st);
103 }
104 mutex_unlock(&nvm_auth_status_lock);
105}
106
107static int nvm_validate_and_write(struct tb_switch *sw)
108{
109 unsigned int image_size, hdr_size;
110 const u8 *buf = sw->nvm->buf;
111 u16 ds_size;
112 int ret;
113
114 if (!buf)
115 return -EINVAL;
116
117 image_size = sw->nvm->buf_data_size;
118 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
119 return -EINVAL;
120
121 /*
122 * FARB pointer must point inside the image and must at least
123 * contain parts of the digital section we will be reading here.
124 */
125 hdr_size = (*(u32 *)buf) & 0xffffff;
126 if (hdr_size + NVM_DEVID + 2 >= image_size)
127 return -EINVAL;
128
129 /* Digital section start should be aligned to 4k page */
130 if (!IS_ALIGNED(hdr_size, SZ_4K))
131 return -EINVAL;
132
133 /*
134 * Read digital section size and check that it also fits inside
135 * the image.
136 */
137 ds_size = *(u16 *)(buf + hdr_size);
138 if (ds_size >= image_size)
139 return -EINVAL;
140
141 if (!sw->safe_mode) {
142 u16 device_id;
143
144 /*
145 * Make sure the device ID in the image matches the one
146 * we read from the switch config space.
147 */
148 device_id = *(u16 *)(buf + hdr_size + NVM_DEVID);
149 if (device_id != sw->config.device_id)
150 return -EINVAL;
151
152 if (sw->generation < 3) {
153 /* Write CSS headers first */
154 ret = dma_port_flash_write(sw->dma_port,
155 DMA_PORT_CSS_ADDRESS, buf + NVM_CSS,
156 DMA_PORT_CSS_MAX_SIZE);
157 if (ret)
158 return ret;
159 }
160
161 /* Skip headers in the image */
162 buf += hdr_size;
163 image_size -= hdr_size;
164 }
165
166 return dma_port_flash_write(sw->dma_port, 0, buf, image_size);
167}
168
169static int nvm_authenticate_host(struct tb_switch *sw)
170{
171 int ret;
172
173 /*
174 * Root switch NVM upgrade requires that we disconnect the
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300175 * existing paths first (in case it is not in safe mode
Mika Westerberge6b245c2017-06-06 15:25:17 +0300176 * already).
177 */
178 if (!sw->safe_mode) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300179 ret = tb_domain_disconnect_all_paths(sw->tb);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300180 if (ret)
181 return ret;
182 /*
183 * The host controller goes away pretty soon after this if
184 * everything goes well so getting timeout is expected.
185 */
186 ret = dma_port_flash_update_auth(sw->dma_port);
187 return ret == -ETIMEDOUT ? 0 : ret;
188 }
189
190 /*
191 * From safe mode we can get out by just power cycling the
192 * switch.
193 */
194 dma_port_power_cycle(sw->dma_port);
195 return 0;
196}
197
198static int nvm_authenticate_device(struct tb_switch *sw)
199{
200 int ret, retries = 10;
201
202 ret = dma_port_flash_update_auth(sw->dma_port);
203 if (ret && ret != -ETIMEDOUT)
204 return ret;
205
206 /*
207 * Poll here for the authentication status. It takes some time
208 * for the device to respond (we get timeout for a while). Once
209 * we get response the device needs to be power cycled in order
210 * to the new NVM to be taken into use.
211 */
212 do {
213 u32 status;
214
215 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
216 if (ret < 0 && ret != -ETIMEDOUT)
217 return ret;
218 if (ret > 0) {
219 if (status) {
220 tb_sw_warn(sw, "failed to authenticate NVM\n");
221 nvm_set_auth_status(sw, status);
222 }
223
224 tb_sw_info(sw, "power cycling the switch now\n");
225 dma_port_power_cycle(sw->dma_port);
226 return 0;
227 }
228
229 msleep(500);
230 } while (--retries);
231
232 return -ETIMEDOUT;
233}
234
235static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
236 size_t bytes)
237{
238 struct tb_switch *sw = priv;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300239 int ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300240
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300241 pm_runtime_get_sync(&sw->dev);
242 ret = dma_port_flash_read(sw->dma_port, offset, val, bytes);
243 pm_runtime_mark_last_busy(&sw->dev);
244 pm_runtime_put_autosuspend(&sw->dev);
245
246 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300247}
248
249static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
250 size_t bytes)
251{
252 struct tb_switch *sw = priv;
253 int ret = 0;
254
Mika Westerberg09f11b62019-03-19 16:48:41 +0200255 if (!mutex_trylock(&sw->tb->lock))
256 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +0300257
258 /*
259 * Since writing the NVM image might require some special steps,
260 * for example when CSS headers are written, we cache the image
261 * locally here and handle the special cases when the user asks
262 * us to authenticate the image.
263 */
264 if (!sw->nvm->buf) {
265 sw->nvm->buf = vmalloc(NVM_MAX_SIZE);
266 if (!sw->nvm->buf) {
267 ret = -ENOMEM;
268 goto unlock;
269 }
270 }
271
272 sw->nvm->buf_data_size = offset + bytes;
273 memcpy(sw->nvm->buf + offset, val, bytes);
274
275unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +0200276 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300277
278 return ret;
279}
280
281static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id,
282 size_t size, bool active)
283{
284 struct nvmem_config config;
285
286 memset(&config, 0, sizeof(config));
287
288 if (active) {
289 config.name = "nvm_active";
290 config.reg_read = tb_switch_nvm_read;
Mika Westerberg800161b2017-06-29 14:19:50 +0300291 config.read_only = true;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300292 } else {
293 config.name = "nvm_non_active";
294 config.reg_write = tb_switch_nvm_write;
Mika Westerberg800161b2017-06-29 14:19:50 +0300295 config.root_only = true;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300296 }
297
298 config.id = id;
299 config.stride = 4;
300 config.word_size = 4;
301 config.size = size;
302 config.dev = &sw->dev;
303 config.owner = THIS_MODULE;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300304 config.priv = sw;
305
306 return nvmem_register(&config);
307}
308
309static int tb_switch_nvm_add(struct tb_switch *sw)
310{
311 struct nvmem_device *nvm_dev;
312 struct tb_switch_nvm *nvm;
313 u32 val;
314 int ret;
315
316 if (!sw->dma_port)
317 return 0;
318
319 nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
320 if (!nvm)
321 return -ENOMEM;
322
323 nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL);
324
325 /*
326 * If the switch is in safe-mode the only accessible portion of
327 * the NVM is the non-active one where userspace is expected to
328 * write new functional NVM.
329 */
330 if (!sw->safe_mode) {
331 u32 nvm_size, hdr_size;
332
333 ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val,
334 sizeof(val));
335 if (ret)
336 goto err_ida;
337
338 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
339 nvm_size = (SZ_1M << (val & 7)) / 8;
340 nvm_size = (nvm_size - hdr_size) / 2;
341
342 ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val,
343 sizeof(val));
344 if (ret)
345 goto err_ida;
346
347 nvm->major = val >> 16;
348 nvm->minor = val >> 8;
349
350 nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true);
351 if (IS_ERR(nvm_dev)) {
352 ret = PTR_ERR(nvm_dev);
353 goto err_ida;
354 }
355 nvm->active = nvm_dev;
356 }
357
358 nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false);
359 if (IS_ERR(nvm_dev)) {
360 ret = PTR_ERR(nvm_dev);
361 goto err_nvm_active;
362 }
363 nvm->non_active = nvm_dev;
364
Mika Westerberge6b245c2017-06-06 15:25:17 +0300365 sw->nvm = nvm;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300366 return 0;
367
368err_nvm_active:
369 if (nvm->active)
370 nvmem_unregister(nvm->active);
371err_ida:
372 ida_simple_remove(&nvm_ida, nvm->id);
373 kfree(nvm);
374
375 return ret;
376}
377
378static void tb_switch_nvm_remove(struct tb_switch *sw)
379{
380 struct tb_switch_nvm *nvm;
381
Mika Westerberge6b245c2017-06-06 15:25:17 +0300382 nvm = sw->nvm;
383 sw->nvm = NULL;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300384
385 if (!nvm)
386 return;
387
388 /* Remove authentication status in case the switch is unplugged */
389 if (!nvm->authenticating)
390 nvm_clear_auth_status(sw);
391
392 nvmem_unregister(nvm->non_active);
393 if (nvm->active)
394 nvmem_unregister(nvm->active);
395 ida_simple_remove(&nvm_ida, nvm->id);
396 vfree(nvm->buf);
397 kfree(nvm);
398}
399
Andreas Noevera25c8b22014-06-03 22:04:02 +0200400/* port utility functions */
401
402static const char *tb_port_type(struct tb_regs_port_header *port)
403{
404 switch (port->type >> 16) {
405 case 0:
406 switch ((u8) port->type) {
407 case 0:
408 return "Inactive";
409 case 1:
410 return "Port";
411 case 2:
412 return "NHI";
413 default:
414 return "unknown";
415 }
416 case 0x2:
417 return "Ethernet";
418 case 0x8:
419 return "SATA";
420 case 0xe:
421 return "DP/HDMI";
422 case 0x10:
423 return "PCIe";
424 case 0x20:
425 return "USB";
426 default:
427 return "unknown";
428 }
429}
430
431static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
432{
Mika Westerbergdaa51402018-10-01 12:31:19 +0300433 tb_dbg(tb,
434 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
435 port->port_number, port->vendor_id, port->device_id,
436 port->revision, port->thunderbolt_version, tb_port_type(port),
437 port->type);
438 tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
439 port->max_in_hop_id, port->max_out_hop_id);
440 tb_dbg(tb, " Max counters: %d\n", port->max_counters);
441 tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200442}
443
444/**
Andreas Noever9da672a2014-06-03 22:04:05 +0200445 * tb_port_state() - get connectedness state of a port
446 *
447 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
448 *
449 * Return: Returns an enum tb_port_state on success or an error code on failure.
450 */
451static int tb_port_state(struct tb_port *port)
452{
453 struct tb_cap_phy phy;
454 int res;
455 if (port->cap_phy == 0) {
456 tb_port_WARN(port, "does not have a PHY\n");
457 return -EINVAL;
458 }
459 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
460 if (res)
461 return res;
462 return phy.state;
463}
464
465/**
466 * tb_wait_for_port() - wait for a port to become ready
467 *
468 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
469 * wait_if_unplugged is set then we also wait if the port is in state
470 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
471 * switch resume). Otherwise we only wait if a device is registered but the link
472 * has not yet been established.
473 *
474 * Return: Returns an error code on failure. Returns 0 if the port is not
475 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
476 * if the port is connected and in state TB_PORT_UP.
477 */
478int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
479{
480 int retries = 10;
481 int state;
482 if (!port->cap_phy) {
483 tb_port_WARN(port, "does not have PHY\n");
484 return -EINVAL;
485 }
486 if (tb_is_upstream_port(port)) {
487 tb_port_WARN(port, "is the upstream port\n");
488 return -EINVAL;
489 }
490
491 while (retries--) {
492 state = tb_port_state(port);
493 if (state < 0)
494 return state;
495 if (state == TB_PORT_DISABLED) {
496 tb_port_info(port, "is disabled (state: 0)\n");
497 return 0;
498 }
499 if (state == TB_PORT_UNPLUGGED) {
500 if (wait_if_unplugged) {
501 /* used during resume */
502 tb_port_info(port,
503 "is unplugged (state: 7), retrying...\n");
504 msleep(100);
505 continue;
506 }
507 tb_port_info(port, "is unplugged (state: 7)\n");
508 return 0;
509 }
510 if (state == TB_PORT_UP) {
511 tb_port_info(port,
512 "is connected, link is up (state: 2)\n");
513 return 1;
514 }
515
516 /*
517 * After plug-in the state is TB_PORT_CONNECTING. Give it some
518 * time.
519 */
520 tb_port_info(port,
521 "is connected, link is not up (state: %d), retrying...\n",
522 state);
523 msleep(100);
524 }
525 tb_port_warn(port,
526 "failed to reach state TB_PORT_UP. Ignoring port...\n");
527 return 0;
528}
529
530/**
Andreas Noever520b6702014-06-03 22:04:07 +0200531 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
532 *
533 * Change the number of NFC credits allocated to @port by @credits. To remove
534 * NFC credits pass a negative amount of credits.
535 *
536 * Return: Returns 0 on success or an error code on failure.
537 */
538int tb_port_add_nfc_credits(struct tb_port *port, int credits)
539{
540 if (credits == 0)
541 return 0;
542 tb_port_info(port,
543 "adding %#x NFC credits (%#x -> %#x)",
544 credits,
545 port->config.nfc_credits,
546 port->config.nfc_credits + credits);
547 port->config.nfc_credits += credits;
548 return tb_port_write(port, &port->config.nfc_credits,
549 TB_CFG_PORT, 4, 1);
550}
551
552/**
553 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
554 *
555 * Return: Returns 0 on success or an error code on failure.
556 */
557int tb_port_clear_counter(struct tb_port *port, int counter)
558{
559 u32 zero[3] = { 0, 0, 0 };
560 tb_port_info(port, "clearing counter %d\n", counter);
561 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
562}
563
564/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200565 * tb_init_port() - initialize a port
566 *
567 * This is a helper method for tb_switch_alloc. Does not check or initialize
568 * any downstream switches.
569 *
570 * Return: Returns 0 on success or an error code on failure.
571 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200572static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200573{
574 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200575 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200576
Andreas Noevera25c8b22014-06-03 22:04:02 +0200577 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
578 if (res)
579 return res;
580
Andreas Noever9da672a2014-06-03 22:04:05 +0200581 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200582 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Mika Westerbergda2da042017-06-06 15:24:58 +0300583 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
Andreas Noever9da672a2014-06-03 22:04:05 +0200584
585 if (cap > 0)
586 port->cap_phy = cap;
587 else
588 tb_port_WARN(port, "non switch port without a PHY\n");
Mika Westerberg56183c82017-02-19 10:39:34 +0200589 } else if (port->port != 0) {
590 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
591 if (cap > 0)
592 port->cap_adap = cap;
Andreas Noever9da672a2014-06-03 22:04:05 +0200593 }
594
Andreas Noever343fcb82014-06-12 23:11:47 +0200595 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200596
Mika Westerberg0b2863a2017-02-19 16:57:27 +0200597 /* Control port does not need HopID allocation */
598 if (port->port) {
599 ida_init(&port->in_hopids);
600 ida_init(&port->out_hopids);
601 }
602
Andreas Noevera25c8b22014-06-03 22:04:02 +0200603 return 0;
604
605}
606
Mika Westerberg0b2863a2017-02-19 16:57:27 +0200607static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
608 int max_hopid)
609{
610 int port_max_hopid;
611 struct ida *ida;
612
613 if (in) {
614 port_max_hopid = port->config.max_in_hop_id;
615 ida = &port->in_hopids;
616 } else {
617 port_max_hopid = port->config.max_out_hop_id;
618 ida = &port->out_hopids;
619 }
620
621 /* HopIDs 0-7 are reserved */
622 if (min_hopid < TB_PATH_MIN_HOPID)
623 min_hopid = TB_PATH_MIN_HOPID;
624
625 if (max_hopid < 0 || max_hopid > port_max_hopid)
626 max_hopid = port_max_hopid;
627
628 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
629}
630
631/**
632 * tb_port_alloc_in_hopid() - Allocate input HopID from port
633 * @port: Port to allocate HopID for
634 * @min_hopid: Minimum acceptable input HopID
635 * @max_hopid: Maximum acceptable input HopID
636 *
637 * Return: HopID between @min_hopid and @max_hopid or negative errno in
638 * case of error.
639 */
640int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
641{
642 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
643}
644
645/**
646 * tb_port_alloc_out_hopid() - Allocate output HopID from port
647 * @port: Port to allocate HopID for
648 * @min_hopid: Minimum acceptable output HopID
649 * @max_hopid: Maximum acceptable output HopID
650 *
651 * Return: HopID between @min_hopid and @max_hopid or negative errno in
652 * case of error.
653 */
654int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
655{
656 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
657}
658
659/**
660 * tb_port_release_in_hopid() - Release allocated input HopID from port
661 * @port: Port whose HopID to release
662 * @hopid: HopID to release
663 */
664void tb_port_release_in_hopid(struct tb_port *port, int hopid)
665{
666 ida_simple_remove(&port->in_hopids, hopid);
667}
668
669/**
670 * tb_port_release_out_hopid() - Release allocated output HopID from port
671 * @port: Port whose HopID to release
672 * @hopid: HopID to release
673 */
674void tb_port_release_out_hopid(struct tb_port *port, int hopid)
675{
676 ida_simple_remove(&port->out_hopids, hopid);
677}
678
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200679/**
680 * tb_pci_port_enable() - Enable PCIe adapter port
681 * @port: PCIe port to enable
682 * @enable: Enable/disable the PCIe adapter
683 */
684int tb_pci_port_enable(struct tb_port *port, bool enable)
685{
686 u32 word = enable ? TB_PCI_EN : 0x0;
687 if (!port->cap_adap)
688 return -ENXIO;
689 return tb_port_write(port, &word, TB_CFG_PORT, port->cap_adap, 1);
690}
691
Andreas Noevera25c8b22014-06-03 22:04:02 +0200692/* switch utility functions */
693
694static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
695{
Mika Westerbergdaa51402018-10-01 12:31:19 +0300696 tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
697 sw->vendor_id, sw->device_id, sw->revision,
698 sw->thunderbolt_version);
699 tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number);
700 tb_dbg(tb, " Config:\n");
701 tb_dbg(tb,
Andreas Noevera25c8b22014-06-03 22:04:02 +0200702 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
Mika Westerbergdaa51402018-10-01 12:31:19 +0300703 sw->upstream_port_number, sw->depth,
704 (((u64) sw->route_hi) << 32) | sw->route_lo,
705 sw->enabled, sw->plug_events_delay);
706 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
707 sw->__unknown1, sw->__unknown4);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200708}
709
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200710/**
711 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
712 *
713 * Return: Returns 0 on success or an error code on failure.
714 */
715int tb_switch_reset(struct tb *tb, u64 route)
716{
717 struct tb_cfg_result res;
718 struct tb_regs_switch_header header = {
719 header.route_hi = route >> 32,
720 header.route_lo = route,
721 header.enabled = true,
722 };
Mika Westerbergdaa51402018-10-01 12:31:19 +0300723 tb_dbg(tb, "resetting switch at %llx\n", route);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200724 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
725 0, 2, 2, 2);
726 if (res.err)
727 return res.err;
728 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
729 if (res.err > 0)
730 return -EIO;
731 return res.err;
732}
733
Andreas Noevera25c8b22014-06-03 22:04:02 +0200734/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200735 * tb_plug_events_active() - enable/disable plug events on a switch
736 *
737 * Also configures a sane plug_events_delay of 255ms.
738 *
739 * Return: Returns 0 on success or an error code on failure.
740 */
741static int tb_plug_events_active(struct tb_switch *sw, bool active)
742{
743 u32 data;
744 int res;
745
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300746 if (!sw->config.enabled)
747 return 0;
748
Andreas Noeverca389f72014-06-03 22:04:04 +0200749 sw->config.plug_events_delay = 0xff;
750 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
751 if (res)
752 return res;
753
754 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
755 if (res)
756 return res;
757
758 if (active) {
759 data = data & 0xFFFFFF83;
760 switch (sw->config.device_id) {
Lukas Wunner1d111402016-03-20 13:57:20 +0100761 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
762 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
763 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
Andreas Noeverca389f72014-06-03 22:04:04 +0200764 break;
765 default:
766 data |= 4;
767 }
768 } else {
769 data = data | 0x7c;
770 }
771 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
772 sw->cap_plug_events + 1, 1);
773}
774
Mika Westerbergf67cf492017-06-06 15:25:16 +0300775static ssize_t authorized_show(struct device *dev,
776 struct device_attribute *attr,
777 char *buf)
778{
779 struct tb_switch *sw = tb_to_switch(dev);
780
781 return sprintf(buf, "%u\n", sw->authorized);
782}
783
784static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
785{
786 int ret = -EINVAL;
787
Mika Westerberg09f11b62019-03-19 16:48:41 +0200788 if (!mutex_trylock(&sw->tb->lock))
789 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +0300790
791 if (sw->authorized)
792 goto unlock;
793
Mika Westerberga03e8282018-01-18 20:27:47 +0300794 /*
795 * Make sure there is no PCIe rescan ongoing when a new PCIe
796 * tunnel is created. Otherwise the PCIe rescan code might find
797 * the new tunnel too early.
798 */
799 pci_lock_rescan_remove();
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300800 pm_runtime_get_sync(&sw->dev);
Mika Westerberga03e8282018-01-18 20:27:47 +0300801
Mika Westerbergf67cf492017-06-06 15:25:16 +0300802 switch (val) {
803 /* Approve switch */
804 case 1:
805 if (sw->key)
806 ret = tb_domain_approve_switch_key(sw->tb, sw);
807 else
808 ret = tb_domain_approve_switch(sw->tb, sw);
809 break;
810
811 /* Challenge switch */
812 case 2:
813 if (sw->key)
814 ret = tb_domain_challenge_switch_key(sw->tb, sw);
815 break;
816
817 default:
818 break;
819 }
820
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300821 pm_runtime_mark_last_busy(&sw->dev);
822 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerberga03e8282018-01-18 20:27:47 +0300823 pci_unlock_rescan_remove();
824
Mika Westerbergf67cf492017-06-06 15:25:16 +0300825 if (!ret) {
826 sw->authorized = val;
827 /* Notify status change to the userspace */
828 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
829 }
830
831unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +0200832 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300833 return ret;
834}
835
836static ssize_t authorized_store(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf, size_t count)
839{
840 struct tb_switch *sw = tb_to_switch(dev);
841 unsigned int val;
842 ssize_t ret;
843
844 ret = kstrtouint(buf, 0, &val);
845 if (ret)
846 return ret;
847 if (val > 2)
848 return -EINVAL;
849
850 ret = tb_switch_set_authorized(sw, val);
851
852 return ret ? ret : count;
853}
854static DEVICE_ATTR_RW(authorized);
855
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200856static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
857 char *buf)
858{
859 struct tb_switch *sw = tb_to_switch(dev);
860
861 return sprintf(buf, "%u\n", sw->boot);
862}
863static DEVICE_ATTR_RO(boot);
864
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300865static ssize_t device_show(struct device *dev, struct device_attribute *attr,
866 char *buf)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200867{
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300868 struct tb_switch *sw = tb_to_switch(dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200869
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300870 return sprintf(buf, "%#x\n", sw->device);
871}
872static DEVICE_ATTR_RO(device);
Andreas Noeverca389f72014-06-03 22:04:04 +0200873
Mika Westerberg72ee3392017-06-06 15:25:05 +0300874static ssize_t
875device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
876{
877 struct tb_switch *sw = tb_to_switch(dev);
878
879 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
880}
881static DEVICE_ATTR_RO(device_name);
882
Mika Westerbergf67cf492017-06-06 15:25:16 +0300883static ssize_t key_show(struct device *dev, struct device_attribute *attr,
884 char *buf)
885{
886 struct tb_switch *sw = tb_to_switch(dev);
887 ssize_t ret;
888
Mika Westerberg09f11b62019-03-19 16:48:41 +0200889 if (!mutex_trylock(&sw->tb->lock))
890 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +0300891
892 if (sw->key)
893 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
894 else
895 ret = sprintf(buf, "\n");
896
Mika Westerberg09f11b62019-03-19 16:48:41 +0200897 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300898 return ret;
899}
900
901static ssize_t key_store(struct device *dev, struct device_attribute *attr,
902 const char *buf, size_t count)
903{
904 struct tb_switch *sw = tb_to_switch(dev);
905 u8 key[TB_SWITCH_KEY_SIZE];
906 ssize_t ret = count;
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +0300907 bool clear = false;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300908
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +0300909 if (!strcmp(buf, "\n"))
910 clear = true;
911 else if (hex2bin(key, buf, sizeof(key)))
Mika Westerbergf67cf492017-06-06 15:25:16 +0300912 return -EINVAL;
913
Mika Westerberg09f11b62019-03-19 16:48:41 +0200914 if (!mutex_trylock(&sw->tb->lock))
915 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +0300916
917 if (sw->authorized) {
918 ret = -EBUSY;
919 } else {
920 kfree(sw->key);
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +0300921 if (clear) {
922 sw->key = NULL;
923 } else {
924 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
925 if (!sw->key)
926 ret = -ENOMEM;
927 }
Mika Westerbergf67cf492017-06-06 15:25:16 +0300928 }
929
Mika Westerberg09f11b62019-03-19 16:48:41 +0200930 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300931 return ret;
932}
Bernat, Yehezkel0956e412017-08-15 08:19:12 +0300933static DEVICE_ATTR(key, 0600, key_show, key_store);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300934
Mika Westerberg1830b6e2018-11-26 12:47:46 +0300935static void nvm_authenticate_start(struct tb_switch *sw)
936{
937 struct pci_dev *root_port;
938
939 /*
940 * During host router NVM upgrade we should not allow root port to
941 * go into D3cold because some root ports cannot trigger PME
942 * itself. To be on the safe side keep the root port in D0 during
943 * the whole upgrade process.
944 */
945 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
946 if (root_port)
947 pm_runtime_get_noresume(&root_port->dev);
948}
949
950static void nvm_authenticate_complete(struct tb_switch *sw)
951{
952 struct pci_dev *root_port;
953
954 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
955 if (root_port)
956 pm_runtime_put(&root_port->dev);
957}
958
Mika Westerberge6b245c2017-06-06 15:25:17 +0300959static ssize_t nvm_authenticate_show(struct device *dev,
960 struct device_attribute *attr, char *buf)
961{
962 struct tb_switch *sw = tb_to_switch(dev);
963 u32 status;
964
965 nvm_get_auth_status(sw, &status);
966 return sprintf(buf, "%#x\n", status);
967}
968
969static ssize_t nvm_authenticate_store(struct device *dev,
970 struct device_attribute *attr, const char *buf, size_t count)
971{
972 struct tb_switch *sw = tb_to_switch(dev);
973 bool val;
974 int ret;
975
Mika Westerberg09f11b62019-03-19 16:48:41 +0200976 if (!mutex_trylock(&sw->tb->lock))
977 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +0300978
979 /* If NVMem devices are not yet added */
980 if (!sw->nvm) {
981 ret = -EAGAIN;
982 goto exit_unlock;
983 }
984
985 ret = kstrtobool(buf, &val);
986 if (ret)
987 goto exit_unlock;
988
989 /* Always clear the authentication status */
990 nvm_clear_auth_status(sw);
991
992 if (val) {
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300993 if (!sw->nvm->buf) {
994 ret = -EINVAL;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300995 goto exit_unlock;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300996 }
997
998 pm_runtime_get_sync(&sw->dev);
999 ret = nvm_validate_and_write(sw);
1000 if (ret) {
1001 pm_runtime_mark_last_busy(&sw->dev);
1002 pm_runtime_put_autosuspend(&sw->dev);
1003 goto exit_unlock;
1004 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001005
1006 sw->nvm->authenticating = true;
1007
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001008 if (!tb_route(sw)) {
1009 /*
1010 * Keep root port from suspending as long as the
1011 * NVM upgrade process is running.
1012 */
1013 nvm_authenticate_start(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001014 ret = nvm_authenticate_host(sw);
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001015 if (ret)
1016 nvm_authenticate_complete(sw);
1017 } else {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001018 ret = nvm_authenticate_device(sw);
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001019 }
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001020 pm_runtime_mark_last_busy(&sw->dev);
1021 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001022 }
1023
1024exit_unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +02001025 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001026
1027 if (ret)
1028 return ret;
1029 return count;
1030}
1031static DEVICE_ATTR_RW(nvm_authenticate);
1032
1033static ssize_t nvm_version_show(struct device *dev,
1034 struct device_attribute *attr, char *buf)
1035{
1036 struct tb_switch *sw = tb_to_switch(dev);
1037 int ret;
1038
Mika Westerberg09f11b62019-03-19 16:48:41 +02001039 if (!mutex_trylock(&sw->tb->lock))
1040 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +03001041
1042 if (sw->safe_mode)
1043 ret = -ENODATA;
1044 else if (!sw->nvm)
1045 ret = -EAGAIN;
1046 else
1047 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1048
Mika Westerberg09f11b62019-03-19 16:48:41 +02001049 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001050
1051 return ret;
1052}
1053static DEVICE_ATTR_RO(nvm_version);
1054
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001055static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1056 char *buf)
1057{
1058 struct tb_switch *sw = tb_to_switch(dev);
1059
1060 return sprintf(buf, "%#x\n", sw->vendor);
1061}
1062static DEVICE_ATTR_RO(vendor);
1063
Mika Westerberg72ee3392017-06-06 15:25:05 +03001064static ssize_t
1065vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1066{
1067 struct tb_switch *sw = tb_to_switch(dev);
1068
1069 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1070}
1071static DEVICE_ATTR_RO(vendor_name);
1072
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001073static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1074 char *buf)
1075{
1076 struct tb_switch *sw = tb_to_switch(dev);
1077
1078 return sprintf(buf, "%pUb\n", sw->uuid);
1079}
1080static DEVICE_ATTR_RO(unique_id);
1081
1082static struct attribute *switch_attrs[] = {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001083 &dev_attr_authorized.attr,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001084 &dev_attr_boot.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001085 &dev_attr_device.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +03001086 &dev_attr_device_name.attr,
Mika Westerbergf67cf492017-06-06 15:25:16 +03001087 &dev_attr_key.attr,
Mika Westerberge6b245c2017-06-06 15:25:17 +03001088 &dev_attr_nvm_authenticate.attr,
1089 &dev_attr_nvm_version.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001090 &dev_attr_vendor.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +03001091 &dev_attr_vendor_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001092 &dev_attr_unique_id.attr,
1093 NULL,
1094};
1095
Mika Westerbergf67cf492017-06-06 15:25:16 +03001096static umode_t switch_attr_is_visible(struct kobject *kobj,
1097 struct attribute *attr, int n)
1098{
1099 struct device *dev = container_of(kobj, struct device, kobj);
1100 struct tb_switch *sw = tb_to_switch(dev);
1101
1102 if (attr == &dev_attr_key.attr) {
1103 if (tb_route(sw) &&
1104 sw->tb->security_level == TB_SECURITY_SECURE &&
1105 sw->security_level == TB_SECURITY_SECURE)
1106 return attr->mode;
1107 return 0;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001108 } else if (attr == &dev_attr_nvm_authenticate.attr ||
1109 attr == &dev_attr_nvm_version.attr) {
1110 if (sw->dma_port)
1111 return attr->mode;
1112 return 0;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001113 } else if (attr == &dev_attr_boot.attr) {
1114 if (tb_route(sw))
1115 return attr->mode;
1116 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001117 }
1118
Mika Westerberge6b245c2017-06-06 15:25:17 +03001119 return sw->safe_mode ? 0 : attr->mode;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001120}
1121
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001122static struct attribute_group switch_group = {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001123 .is_visible = switch_attr_is_visible,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001124 .attrs = switch_attrs,
1125};
1126
1127static const struct attribute_group *switch_groups[] = {
1128 &switch_group,
1129 NULL,
1130};
1131
1132static void tb_switch_release(struct device *dev)
1133{
1134 struct tb_switch *sw = tb_to_switch(dev);
Mika Westerberg0b2863a2017-02-19 16:57:27 +02001135 int i;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001136
Mika Westerberg3e136762017-06-06 15:25:14 +03001137 dma_port_free(sw->dma_port);
1138
Mika Westerberg0b2863a2017-02-19 16:57:27 +02001139 for (i = 1; i <= sw->config.max_port_number; i++) {
1140 if (!sw->ports[i].disabled) {
1141 ida_destroy(&sw->ports[i].in_hopids);
1142 ida_destroy(&sw->ports[i].out_hopids);
1143 }
1144 }
1145
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001146 kfree(sw->uuid);
Mika Westerberg72ee3392017-06-06 15:25:05 +03001147 kfree(sw->device_name);
1148 kfree(sw->vendor_name);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001149 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +02001150 kfree(sw->drom);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001151 kfree(sw->key);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001152 kfree(sw);
1153}
1154
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001155/*
1156 * Currently only need to provide the callbacks. Everything else is handled
1157 * in the connection manager.
1158 */
1159static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
1160{
1161 return 0;
1162}
1163
1164static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
1165{
1166 return 0;
1167}
1168
1169static const struct dev_pm_ops tb_switch_pm_ops = {
1170 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
1171 NULL)
1172};
1173
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001174struct device_type tb_switch_type = {
1175 .name = "thunderbolt_device",
1176 .release = tb_switch_release,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001177 .pm = &tb_switch_pm_ops,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001178};
1179
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001180static int tb_switch_get_generation(struct tb_switch *sw)
1181{
1182 switch (sw->config.device_id) {
1183 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1184 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1185 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1186 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1187 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1188 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1189 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1190 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1191 return 1;
1192
1193 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1194 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1195 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1196 return 2;
1197
1198 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1199 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1200 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1201 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1202 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001203 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1204 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1205 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001206 return 3;
1207
1208 default:
1209 /*
1210 * For unknown switches assume generation to be 1 to be
1211 * on the safe side.
1212 */
1213 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1214 sw->config.device_id);
1215 return 1;
1216 }
1217}
1218
Andreas Noevera25c8b22014-06-03 22:04:02 +02001219/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001220 * tb_switch_alloc() - allocate a switch
1221 * @tb: Pointer to the owning domain
1222 * @parent: Parent device for this switch
1223 * @route: Route string for this switch
Andreas Noevera25c8b22014-06-03 22:04:02 +02001224 *
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001225 * Allocates and initializes a switch. Will not upload configuration to
1226 * the switch. For that you need to call tb_switch_configure()
1227 * separately. The returned switch should be released by calling
1228 * tb_switch_put().
1229 *
1230 * Return: Pointer to the allocated switch or %NULL in case of failure
Andreas Noevera25c8b22014-06-03 22:04:02 +02001231 */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001232struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1233 u64 route)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001234{
Andreas Noevera25c8b22014-06-03 22:04:02 +02001235 struct tb_switch *sw;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001236 int upstream_port;
1237 int i, cap, depth;
1238
1239 /* Make sure we do not exceed maximum topology limit */
1240 depth = tb_route_length(route);
1241 if (depth > TB_SWITCH_MAX_DEPTH)
1242 return NULL;
1243
1244 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001245 if (upstream_port < 0)
1246 return NULL;
1247
1248 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1249 if (!sw)
1250 return NULL;
1251
1252 sw->tb = tb;
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001253 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001254 goto err_free_sw_ports;
1255
Mika Westerbergdaa51402018-10-01 12:31:19 +03001256 tb_dbg(tb, "current switch config:\n");
Andreas Noevera25c8b22014-06-03 22:04:02 +02001257 tb_dump_switch(tb, &sw->config);
1258
1259 /* configure switch */
1260 sw->config.upstream_port_number = upstream_port;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001261 sw->config.depth = depth;
1262 sw->config.route_hi = upper_32_bits(route);
1263 sw->config.route_lo = lower_32_bits(route);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001264 sw->config.enabled = 0;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001265
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001266 /* initialize ports */
1267 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
1268 GFP_KERNEL);
1269 if (!sw->ports)
1270 goto err_free_sw_ports;
1271
1272 for (i = 0; i <= sw->config.max_port_number; i++) {
1273 /* minimum setup for tb_find_cap and tb_drom_read to work */
1274 sw->ports[i].sw = sw;
1275 sw->ports[i].port = i;
1276 }
1277
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001278 sw->generation = tb_switch_get_generation(sw);
1279
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001280 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
1281 if (cap < 0) {
1282 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1283 goto err_free_sw_ports;
1284 }
1285 sw->cap_plug_events = cap;
1286
Mika Westerberga9be5582019-01-09 16:42:12 +02001287 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
1288 if (cap > 0)
1289 sw->cap_lc = cap;
1290
Mika Westerbergf67cf492017-06-06 15:25:16 +03001291 /* Root switch is always authorized */
1292 if (!route)
1293 sw->authorized = true;
1294
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001295 device_initialize(&sw->dev);
1296 sw->dev.parent = parent;
1297 sw->dev.bus = &tb_bus_type;
1298 sw->dev.type = &tb_switch_type;
1299 sw->dev.groups = switch_groups;
1300 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1301
1302 return sw;
1303
1304err_free_sw_ports:
1305 kfree(sw->ports);
1306 kfree(sw);
1307
1308 return NULL;
1309}
1310
1311/**
Mika Westerberge6b245c2017-06-06 15:25:17 +03001312 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1313 * @tb: Pointer to the owning domain
1314 * @parent: Parent device for this switch
1315 * @route: Route string for this switch
1316 *
1317 * This creates a switch in safe mode. This means the switch pretty much
1318 * lacks all capabilities except DMA configuration port before it is
1319 * flashed with a valid NVM firmware.
1320 *
1321 * The returned switch must be released by calling tb_switch_put().
1322 *
1323 * Return: Pointer to the allocated switch or %NULL in case of failure
1324 */
1325struct tb_switch *
1326tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
1327{
1328 struct tb_switch *sw;
1329
1330 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1331 if (!sw)
1332 return NULL;
1333
1334 sw->tb = tb;
1335 sw->config.depth = tb_route_length(route);
1336 sw->config.route_hi = upper_32_bits(route);
1337 sw->config.route_lo = lower_32_bits(route);
1338 sw->safe_mode = true;
1339
1340 device_initialize(&sw->dev);
1341 sw->dev.parent = parent;
1342 sw->dev.bus = &tb_bus_type;
1343 sw->dev.type = &tb_switch_type;
1344 sw->dev.groups = switch_groups;
1345 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1346
1347 return sw;
1348}
1349
1350/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001351 * tb_switch_configure() - Uploads configuration to the switch
1352 * @sw: Switch to configure
1353 *
1354 * Call this function before the switch is added to the system. It will
1355 * upload configuration to the switch and makes it available for the
1356 * connection manager to use.
1357 *
1358 * Return: %0 in case of success and negative errno in case of failure
1359 */
1360int tb_switch_configure(struct tb_switch *sw)
1361{
1362 struct tb *tb = sw->tb;
1363 u64 route;
1364 int ret;
1365
1366 route = tb_route(sw);
Mika Westerbergdaa51402018-10-01 12:31:19 +03001367 tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1368 route, tb_route_length(route), sw->config.upstream_port_number);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001369
1370 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001371 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
1372 sw->config.vendor_id);
1373
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001374 sw->config.enabled = 1;
1375
Andreas Noevera25c8b22014-06-03 22:04:02 +02001376 /* upload configuration */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001377 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
1378 if (ret)
1379 return ret;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001380
Mika Westerberge879a702018-10-11 12:33:08 +03001381 ret = tb_lc_configure_link(sw);
1382 if (ret)
1383 return ret;
1384
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001385 return tb_plug_events_active(sw, true);
1386}
Andreas Noevera25c8b22014-06-03 22:04:02 +02001387
Aditya Pakki2cc12752019-03-20 10:57:54 -05001388static int tb_switch_set_uuid(struct tb_switch *sw)
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001389{
1390 u32 uuid[4];
Mika Westerberga9be5582019-01-09 16:42:12 +02001391 int ret;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001392
1393 if (sw->uuid)
Mika Westerberga9be5582019-01-09 16:42:12 +02001394 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001395
1396 /*
1397 * The newer controllers include fused UUID as part of link
1398 * controller specific registers
1399 */
Mika Westerberga9be5582019-01-09 16:42:12 +02001400 ret = tb_lc_read_uuid(sw, uuid);
1401 if (ret) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001402 /*
1403 * ICM generates UUID based on UID and fills the upper
1404 * two words with ones. This is not strictly following
1405 * UUID format but we want to be compatible with it so
1406 * we do the same here.
1407 */
1408 uuid[0] = sw->uid & 0xffffffff;
1409 uuid[1] = (sw->uid >> 32) & 0xffffffff;
1410 uuid[2] = 0xffffffff;
1411 uuid[3] = 0xffffffff;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001412 }
1413
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001414 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
Aditya Pakki2cc12752019-03-20 10:57:54 -05001415 if (!sw->uuid)
Mika Westerberga9be5582019-01-09 16:42:12 +02001416 return -ENOMEM;
1417 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001418}
1419
Mika Westerberge6b245c2017-06-06 15:25:17 +03001420static int tb_switch_add_dma_port(struct tb_switch *sw)
Mika Westerberg3e136762017-06-06 15:25:14 +03001421{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001422 u32 status;
1423 int ret;
1424
Mika Westerberg3e136762017-06-06 15:25:14 +03001425 switch (sw->generation) {
1426 case 3:
1427 break;
1428
1429 case 2:
1430 /* Only root switch can be upgraded */
1431 if (tb_route(sw))
Mika Westerberge6b245c2017-06-06 15:25:17 +03001432 return 0;
Mika Westerberg3e136762017-06-06 15:25:14 +03001433 break;
1434
1435 default:
Mika Westerberge6b245c2017-06-06 15:25:17 +03001436 /*
1437 * DMA port is the only thing available when the switch
1438 * is in safe mode.
1439 */
1440 if (!sw->safe_mode)
1441 return 0;
1442 break;
Mika Westerberg3e136762017-06-06 15:25:14 +03001443 }
1444
Mika Westerberge6b245c2017-06-06 15:25:17 +03001445 if (sw->no_nvm_upgrade)
1446 return 0;
1447
Mika Westerberg3e136762017-06-06 15:25:14 +03001448 sw->dma_port = dma_port_alloc(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001449 if (!sw->dma_port)
1450 return 0;
1451
1452 /*
1453 * Check status of the previous flash authentication. If there
1454 * is one we need to power cycle the switch in any case to make
1455 * it functional again.
1456 */
1457 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
1458 if (ret <= 0)
1459 return ret;
1460
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001461 /* Now we can allow root port to suspend again */
1462 if (!tb_route(sw))
1463 nvm_authenticate_complete(sw);
1464
Mika Westerberge6b245c2017-06-06 15:25:17 +03001465 if (status) {
1466 tb_sw_info(sw, "switch flash authentication failed\n");
Aditya Pakki2cc12752019-03-20 10:57:54 -05001467 ret = tb_switch_set_uuid(sw);
1468 if (ret)
1469 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001470 nvm_set_auth_status(sw, status);
1471 }
1472
1473 tb_sw_info(sw, "power cycling the switch now\n");
1474 dma_port_power_cycle(sw->dma_port);
1475
1476 /*
1477 * We return error here which causes the switch adding failure.
1478 * It should appear back after power cycle is complete.
1479 */
1480 return -ESHUTDOWN;
Mika Westerberg3e136762017-06-06 15:25:14 +03001481}
1482
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001483/**
1484 * tb_switch_add() - Add a switch to the domain
1485 * @sw: Switch to add
1486 *
1487 * This is the last step in adding switch to the domain. It will read
1488 * identification information from DROM and initializes ports so that
1489 * they can be used to connect other switches. The switch will be
1490 * exposed to the userspace when this function successfully returns. To
1491 * remove and release the switch, call tb_switch_remove().
1492 *
1493 * Return: %0 in case of success and negative errno in case of failure
1494 */
1495int tb_switch_add(struct tb_switch *sw)
1496{
1497 int i, ret;
Andreas Noeverca389f72014-06-03 22:04:04 +02001498
Mika Westerberg3e136762017-06-06 15:25:14 +03001499 /*
1500 * Initialize DMA control port now before we read DROM. Recent
1501 * host controllers have more complete DROM on NVM that includes
1502 * vendor and model identification strings which we then expose
1503 * to the userspace. NVM can be accessed through DMA
1504 * configuration based mailbox.
1505 */
Mika Westerberge6b245c2017-06-06 15:25:17 +03001506 ret = tb_switch_add_dma_port(sw);
1507 if (ret)
Mika Westerbergf53e7672017-06-06 15:25:02 +03001508 return ret;
Andreas Noever343fcb82014-06-12 23:11:47 +02001509
Mika Westerberge6b245c2017-06-06 15:25:17 +03001510 if (!sw->safe_mode) {
1511 /* read drom */
1512 ret = tb_drom_read(sw);
1513 if (ret) {
1514 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001515 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001516 }
Mika Westerbergdaa51402018-10-01 12:31:19 +03001517 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001518
Aditya Pakki2cc12752019-03-20 10:57:54 -05001519 ret = tb_switch_set_uuid(sw);
1520 if (ret)
1521 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001522
1523 for (i = 0; i <= sw->config.max_port_number; i++) {
1524 if (sw->ports[i].disabled) {
Mika Westerbergdaa51402018-10-01 12:31:19 +03001525 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
Mika Westerberge6b245c2017-06-06 15:25:17 +03001526 continue;
1527 }
1528 ret = tb_init_port(&sw->ports[i]);
1529 if (ret)
1530 return ret;
1531 }
Andreas Noever343fcb82014-06-12 23:11:47 +02001532 }
1533
Mika Westerberge6b245c2017-06-06 15:25:17 +03001534 ret = device_add(&sw->dev);
1535 if (ret)
1536 return ret;
1537
Mika Westerberga83bc4a2018-10-01 12:31:20 +03001538 if (tb_route(sw)) {
1539 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
1540 sw->vendor, sw->device);
1541 if (sw->vendor_name && sw->device_name)
1542 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
1543 sw->device_name);
1544 }
1545
Mika Westerberge6b245c2017-06-06 15:25:17 +03001546 ret = tb_switch_nvm_add(sw);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001547 if (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001548 device_del(&sw->dev);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001549 return ret;
1550 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001551
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001552 pm_runtime_set_active(&sw->dev);
1553 if (sw->rpm) {
1554 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
1555 pm_runtime_use_autosuspend(&sw->dev);
1556 pm_runtime_mark_last_busy(&sw->dev);
1557 pm_runtime_enable(&sw->dev);
1558 pm_request_autosuspend(&sw->dev);
1559 }
1560
1561 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001562}
Andreas Noeverc90553b2014-06-03 22:04:11 +02001563
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001564/**
1565 * tb_switch_remove() - Remove and release a switch
1566 * @sw: Switch to remove
1567 *
1568 * This will remove the switch from the domain and release it after last
1569 * reference count drops to zero. If there are switches connected below
1570 * this switch, they will be removed as well.
1571 */
1572void tb_switch_remove(struct tb_switch *sw)
1573{
1574 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +02001575
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001576 if (sw->rpm) {
1577 pm_runtime_get_sync(&sw->dev);
1578 pm_runtime_disable(&sw->dev);
1579 }
1580
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001581 /* port 0 is the switch itself and never has a remote */
1582 for (i = 1; i <= sw->config.max_port_number; i++) {
1583 if (tb_is_upstream_port(&sw->ports[i]))
1584 continue;
1585 if (sw->ports[i].remote)
1586 tb_switch_remove(sw->ports[i].remote->sw);
1587 sw->ports[i].remote = NULL;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001588 if (sw->ports[i].xdomain)
1589 tb_xdomain_remove(sw->ports[i].xdomain);
1590 sw->ports[i].xdomain = NULL;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001591 }
1592
1593 if (!sw->is_unplugged)
1594 tb_plug_events_active(sw, false);
Mika Westerberge879a702018-10-11 12:33:08 +03001595 tb_lc_unconfigure_link(sw);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001596
Mika Westerberge6b245c2017-06-06 15:25:17 +03001597 tb_switch_nvm_remove(sw);
Mika Westerberga83bc4a2018-10-01 12:31:20 +03001598
1599 if (tb_route(sw))
1600 dev_info(&sw->dev, "device disconnected\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001601 device_unregister(&sw->dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001602}
1603
Andreas Noever053596d2014-06-03 22:04:06 +02001604/**
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001605 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
Andreas Noever053596d2014-06-03 22:04:06 +02001606 */
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001607void tb_sw_set_unplugged(struct tb_switch *sw)
Andreas Noever053596d2014-06-03 22:04:06 +02001608{
1609 int i;
1610 if (sw == sw->tb->root_switch) {
1611 tb_sw_WARN(sw, "cannot unplug root switch\n");
1612 return;
1613 }
1614 if (sw->is_unplugged) {
1615 tb_sw_WARN(sw, "is_unplugged already set\n");
1616 return;
1617 }
1618 sw->is_unplugged = true;
1619 for (i = 0; i <= sw->config.max_port_number; i++) {
1620 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001621 tb_sw_set_unplugged(sw->ports[i].remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +02001622 }
1623}
1624
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001625int tb_switch_resume(struct tb_switch *sw)
1626{
1627 int i, err;
Mika Westerbergdaa51402018-10-01 12:31:19 +03001628 tb_sw_dbg(sw, "resuming switch\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001629
Mika Westerberg08a5e4c2017-06-06 15:24:54 +03001630 /*
1631 * Check for UID of the connected switches except for root
1632 * switch which we assume cannot be removed.
1633 */
1634 if (tb_route(sw)) {
1635 u64 uid;
1636
1637 err = tb_drom_read_uid_only(sw, &uid);
1638 if (err) {
1639 tb_sw_warn(sw, "uid read failed\n");
1640 return err;
1641 }
1642 if (sw->uid != uid) {
1643 tb_sw_info(sw,
1644 "changed while suspended (uid %#llx -> %#llx)\n",
1645 sw->uid, uid);
1646 return -ENODEV;
1647 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001648 }
1649
1650 /* upload configuration */
1651 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
1652 if (err)
1653 return err;
1654
Mika Westerberge879a702018-10-11 12:33:08 +03001655 err = tb_lc_configure_link(sw);
1656 if (err)
1657 return err;
1658
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001659 err = tb_plug_events_active(sw, true);
1660 if (err)
1661 return err;
1662
1663 /* check for surviving downstream switches */
1664 for (i = 1; i <= sw->config.max_port_number; i++) {
1665 struct tb_port *port = &sw->ports[i];
1666 if (tb_is_upstream_port(port))
1667 continue;
1668 if (!port->remote)
1669 continue;
1670 if (tb_wait_for_port(port, true) <= 0
1671 || tb_switch_resume(port->remote->sw)) {
1672 tb_port_warn(port,
1673 "lost during suspend, disconnecting\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001674 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001675 }
1676 }
1677 return 0;
1678}
1679
1680void tb_switch_suspend(struct tb_switch *sw)
1681{
1682 int i, err;
1683 err = tb_plug_events_active(sw, false);
1684 if (err)
1685 return;
1686
1687 for (i = 1; i <= sw->config.max_port_number; i++) {
1688 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
1689 tb_switch_suspend(sw->ports[i].remote->sw);
1690 }
Mika Westerberg5480dfc2019-01-09 17:25:43 +02001691
1692 tb_lc_set_sleep(sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001693}
Mika Westerbergf67cf492017-06-06 15:25:16 +03001694
1695struct tb_sw_lookup {
1696 struct tb *tb;
1697 u8 link;
1698 u8 depth;
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02001699 const uuid_t *uuid;
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03001700 u64 route;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001701};
1702
1703static int tb_switch_match(struct device *dev, void *data)
1704{
1705 struct tb_switch *sw = tb_to_switch(dev);
1706 struct tb_sw_lookup *lookup = data;
1707
1708 if (!sw)
1709 return 0;
1710 if (sw->tb != lookup->tb)
1711 return 0;
1712
1713 if (lookup->uuid)
1714 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
1715
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03001716 if (lookup->route) {
1717 return sw->config.route_lo == lower_32_bits(lookup->route) &&
1718 sw->config.route_hi == upper_32_bits(lookup->route);
1719 }
1720
Mika Westerbergf67cf492017-06-06 15:25:16 +03001721 /* Root switch is matched only by depth */
1722 if (!lookup->depth)
1723 return !sw->depth;
1724
1725 return sw->link == lookup->link && sw->depth == lookup->depth;
1726}
1727
1728/**
1729 * tb_switch_find_by_link_depth() - Find switch by link and depth
1730 * @tb: Domain the switch belongs
1731 * @link: Link number the switch is connected
1732 * @depth: Depth of the switch in link
1733 *
1734 * Returned switch has reference count increased so the caller needs to
1735 * call tb_switch_put() when done with the switch.
1736 */
1737struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
1738{
1739 struct tb_sw_lookup lookup;
1740 struct device *dev;
1741
1742 memset(&lookup, 0, sizeof(lookup));
1743 lookup.tb = tb;
1744 lookup.link = link;
1745 lookup.depth = depth;
1746
1747 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1748 if (dev)
1749 return tb_to_switch(dev);
1750
1751 return NULL;
1752}
1753
1754/**
Radion Mirchevsky432019d2017-10-04 15:07:40 +03001755 * tb_switch_find_by_uuid() - Find switch by UUID
Mika Westerbergf67cf492017-06-06 15:25:16 +03001756 * @tb: Domain the switch belongs
1757 * @uuid: UUID to look for
1758 *
1759 * Returned switch has reference count increased so the caller needs to
1760 * call tb_switch_put() when done with the switch.
1761 */
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02001762struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001763{
1764 struct tb_sw_lookup lookup;
1765 struct device *dev;
1766
1767 memset(&lookup, 0, sizeof(lookup));
1768 lookup.tb = tb;
1769 lookup.uuid = uuid;
1770
1771 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1772 if (dev)
1773 return tb_to_switch(dev);
1774
1775 return NULL;
1776}
Mika Westerberge6b245c2017-06-06 15:25:17 +03001777
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03001778/**
1779 * tb_switch_find_by_route() - Find switch by route string
1780 * @tb: Domain the switch belongs
1781 * @route: Route string to look for
1782 *
1783 * Returned switch has reference count increased so the caller needs to
1784 * call tb_switch_put() when done with the switch.
1785 */
1786struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
1787{
1788 struct tb_sw_lookup lookup;
1789 struct device *dev;
1790
1791 if (!route)
1792 return tb_switch_get(tb->root_switch);
1793
1794 memset(&lookup, 0, sizeof(lookup));
1795 lookup.tb = tb;
1796 lookup.route = route;
1797
1798 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1799 if (dev)
1800 return tb_to_switch(dev);
1801
1802 return NULL;
1803}
1804
Mika Westerberge6b245c2017-06-06 15:25:17 +03001805void tb_switch_exit(void)
1806{
1807 ida_destroy(&nvm_ida);
1808}