blob: 1cc79785ce429b453e90c7f21ac05d735b2bc88e [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/*
3 * Thunderbolt Cactus Ridge driver - switch/port utility functions
4 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 */
7
8#include <linux/delay.h>
Mika Westerberge6b245c2017-06-06 15:25:17 +03009#include <linux/idr.h>
10#include <linux/nvmem-provider.h>
11#include <linux/sizes.h>
Sachin Kamat10fefe52014-06-20 14:32:30 +053012#include <linux/slab.h>
Mika Westerberge6b245c2017-06-06 15:25:17 +030013#include <linux/vmalloc.h>
Andreas Noevera25c8b22014-06-03 22:04:02 +020014
15#include "tb.h"
16
Mika Westerbergf67cf492017-06-06 15:25:16 +030017/* Switch authorization from userspace is serialized by this lock */
18static DEFINE_MUTEX(switch_lock);
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;
239
240 return dma_port_flash_read(sw->dma_port, offset, val, bytes);
241}
242
243static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
244 size_t bytes)
245{
246 struct tb_switch *sw = priv;
247 int ret = 0;
248
249 if (mutex_lock_interruptible(&switch_lock))
250 return -ERESTARTSYS;
251
252 /*
253 * Since writing the NVM image might require some special steps,
254 * for example when CSS headers are written, we cache the image
255 * locally here and handle the special cases when the user asks
256 * us to authenticate the image.
257 */
258 if (!sw->nvm->buf) {
259 sw->nvm->buf = vmalloc(NVM_MAX_SIZE);
260 if (!sw->nvm->buf) {
261 ret = -ENOMEM;
262 goto unlock;
263 }
264 }
265
266 sw->nvm->buf_data_size = offset + bytes;
267 memcpy(sw->nvm->buf + offset, val, bytes);
268
269unlock:
270 mutex_unlock(&switch_lock);
271
272 return ret;
273}
274
275static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id,
276 size_t size, bool active)
277{
278 struct nvmem_config config;
279
280 memset(&config, 0, sizeof(config));
281
282 if (active) {
283 config.name = "nvm_active";
284 config.reg_read = tb_switch_nvm_read;
Mika Westerberg800161b2017-06-29 14:19:50 +0300285 config.read_only = true;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300286 } else {
287 config.name = "nvm_non_active";
288 config.reg_write = tb_switch_nvm_write;
Mika Westerberg800161b2017-06-29 14:19:50 +0300289 config.root_only = true;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300290 }
291
292 config.id = id;
293 config.stride = 4;
294 config.word_size = 4;
295 config.size = size;
296 config.dev = &sw->dev;
297 config.owner = THIS_MODULE;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300298 config.priv = sw;
299
300 return nvmem_register(&config);
301}
302
303static int tb_switch_nvm_add(struct tb_switch *sw)
304{
305 struct nvmem_device *nvm_dev;
306 struct tb_switch_nvm *nvm;
307 u32 val;
308 int ret;
309
310 if (!sw->dma_port)
311 return 0;
312
313 nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
314 if (!nvm)
315 return -ENOMEM;
316
317 nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL);
318
319 /*
320 * If the switch is in safe-mode the only accessible portion of
321 * the NVM is the non-active one where userspace is expected to
322 * write new functional NVM.
323 */
324 if (!sw->safe_mode) {
325 u32 nvm_size, hdr_size;
326
327 ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val,
328 sizeof(val));
329 if (ret)
330 goto err_ida;
331
332 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
333 nvm_size = (SZ_1M << (val & 7)) / 8;
334 nvm_size = (nvm_size - hdr_size) / 2;
335
336 ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val,
337 sizeof(val));
338 if (ret)
339 goto err_ida;
340
341 nvm->major = val >> 16;
342 nvm->minor = val >> 8;
343
344 nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true);
345 if (IS_ERR(nvm_dev)) {
346 ret = PTR_ERR(nvm_dev);
347 goto err_ida;
348 }
349 nvm->active = nvm_dev;
350 }
351
352 nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false);
353 if (IS_ERR(nvm_dev)) {
354 ret = PTR_ERR(nvm_dev);
355 goto err_nvm_active;
356 }
357 nvm->non_active = nvm_dev;
358
359 mutex_lock(&switch_lock);
360 sw->nvm = nvm;
361 mutex_unlock(&switch_lock);
362
363 return 0;
364
365err_nvm_active:
366 if (nvm->active)
367 nvmem_unregister(nvm->active);
368err_ida:
369 ida_simple_remove(&nvm_ida, nvm->id);
370 kfree(nvm);
371
372 return ret;
373}
374
375static void tb_switch_nvm_remove(struct tb_switch *sw)
376{
377 struct tb_switch_nvm *nvm;
378
379 mutex_lock(&switch_lock);
380 nvm = sw->nvm;
381 sw->nvm = NULL;
382 mutex_unlock(&switch_lock);
383
384 if (!nvm)
385 return;
386
387 /* Remove authentication status in case the switch is unplugged */
388 if (!nvm->authenticating)
389 nvm_clear_auth_status(sw);
390
391 nvmem_unregister(nvm->non_active);
392 if (nvm->active)
393 nvmem_unregister(nvm->active);
394 ida_simple_remove(&nvm_ida, nvm->id);
395 vfree(nvm->buf);
396 kfree(nvm);
397}
398
Andreas Noevera25c8b22014-06-03 22:04:02 +0200399/* port utility functions */
400
401static const char *tb_port_type(struct tb_regs_port_header *port)
402{
403 switch (port->type >> 16) {
404 case 0:
405 switch ((u8) port->type) {
406 case 0:
407 return "Inactive";
408 case 1:
409 return "Port";
410 case 2:
411 return "NHI";
412 default:
413 return "unknown";
414 }
415 case 0x2:
416 return "Ethernet";
417 case 0x8:
418 return "SATA";
419 case 0xe:
420 return "DP/HDMI";
421 case 0x10:
422 return "PCIe";
423 case 0x20:
424 return "USB";
425 default:
426 return "unknown";
427 }
428}
429
430static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
431{
432 tb_info(tb,
433 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
434 port->port_number, port->vendor_id, port->device_id,
435 port->revision, port->thunderbolt_version, tb_port_type(port),
436 port->type);
437 tb_info(tb, " Max hop id (in/out): %d/%d\n",
438 port->max_in_hop_id, port->max_out_hop_id);
439 tb_info(tb, " Max counters: %d\n", port->max_counters);
440 tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits);
441}
442
443/**
Andreas Noever9da672a2014-06-03 22:04:05 +0200444 * tb_port_state() - get connectedness state of a port
445 *
446 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
447 *
448 * Return: Returns an enum tb_port_state on success or an error code on failure.
449 */
450static int tb_port_state(struct tb_port *port)
451{
452 struct tb_cap_phy phy;
453 int res;
454 if (port->cap_phy == 0) {
455 tb_port_WARN(port, "does not have a PHY\n");
456 return -EINVAL;
457 }
458 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
459 if (res)
460 return res;
461 return phy.state;
462}
463
464/**
465 * tb_wait_for_port() - wait for a port to become ready
466 *
467 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
468 * wait_if_unplugged is set then we also wait if the port is in state
469 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
470 * switch resume). Otherwise we only wait if a device is registered but the link
471 * has not yet been established.
472 *
473 * Return: Returns an error code on failure. Returns 0 if the port is not
474 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
475 * if the port is connected and in state TB_PORT_UP.
476 */
477int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
478{
479 int retries = 10;
480 int state;
481 if (!port->cap_phy) {
482 tb_port_WARN(port, "does not have PHY\n");
483 return -EINVAL;
484 }
485 if (tb_is_upstream_port(port)) {
486 tb_port_WARN(port, "is the upstream port\n");
487 return -EINVAL;
488 }
489
490 while (retries--) {
491 state = tb_port_state(port);
492 if (state < 0)
493 return state;
494 if (state == TB_PORT_DISABLED) {
495 tb_port_info(port, "is disabled (state: 0)\n");
496 return 0;
497 }
498 if (state == TB_PORT_UNPLUGGED) {
499 if (wait_if_unplugged) {
500 /* used during resume */
501 tb_port_info(port,
502 "is unplugged (state: 7), retrying...\n");
503 msleep(100);
504 continue;
505 }
506 tb_port_info(port, "is unplugged (state: 7)\n");
507 return 0;
508 }
509 if (state == TB_PORT_UP) {
510 tb_port_info(port,
511 "is connected, link is up (state: 2)\n");
512 return 1;
513 }
514
515 /*
516 * After plug-in the state is TB_PORT_CONNECTING. Give it some
517 * time.
518 */
519 tb_port_info(port,
520 "is connected, link is not up (state: %d), retrying...\n",
521 state);
522 msleep(100);
523 }
524 tb_port_warn(port,
525 "failed to reach state TB_PORT_UP. Ignoring port...\n");
526 return 0;
527}
528
529/**
Andreas Noever520b6702014-06-03 22:04:07 +0200530 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
531 *
532 * Change the number of NFC credits allocated to @port by @credits. To remove
533 * NFC credits pass a negative amount of credits.
534 *
535 * Return: Returns 0 on success or an error code on failure.
536 */
537int tb_port_add_nfc_credits(struct tb_port *port, int credits)
538{
539 if (credits == 0)
540 return 0;
541 tb_port_info(port,
542 "adding %#x NFC credits (%#x -> %#x)",
543 credits,
544 port->config.nfc_credits,
545 port->config.nfc_credits + credits);
546 port->config.nfc_credits += credits;
547 return tb_port_write(port, &port->config.nfc_credits,
548 TB_CFG_PORT, 4, 1);
549}
550
551/**
552 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
553 *
554 * Return: Returns 0 on success or an error code on failure.
555 */
556int tb_port_clear_counter(struct tb_port *port, int counter)
557{
558 u32 zero[3] = { 0, 0, 0 };
559 tb_port_info(port, "clearing counter %d\n", counter);
560 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
561}
562
563/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200564 * tb_init_port() - initialize a port
565 *
566 * This is a helper method for tb_switch_alloc. Does not check or initialize
567 * any downstream switches.
568 *
569 * Return: Returns 0 on success or an error code on failure.
570 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200571static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200572{
573 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200574 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200575
Andreas Noevera25c8b22014-06-03 22:04:02 +0200576 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
577 if (res)
578 return res;
579
Andreas Noever9da672a2014-06-03 22:04:05 +0200580 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200581 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Mika Westerbergda2da042017-06-06 15:24:58 +0300582 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
Andreas Noever9da672a2014-06-03 22:04:05 +0200583
584 if (cap > 0)
585 port->cap_phy = cap;
586 else
587 tb_port_WARN(port, "non switch port without a PHY\n");
588 }
589
Andreas Noever343fcb82014-06-12 23:11:47 +0200590 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200591
592 /* TODO: Read dual link port, DP port and more from EEPROM. */
593 return 0;
594
595}
596
597/* switch utility functions */
598
599static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
600{
601 tb_info(tb,
602 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
603 sw->vendor_id, sw->device_id, sw->revision,
604 sw->thunderbolt_version);
605 tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
606 tb_info(tb, " Config:\n");
607 tb_info(tb,
608 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
609 sw->upstream_port_number, sw->depth,
610 (((u64) sw->route_hi) << 32) | sw->route_lo,
611 sw->enabled, sw->plug_events_delay);
612 tb_info(tb,
613 " unknown1: %#x unknown4: %#x\n",
614 sw->__unknown1, sw->__unknown4);
615}
616
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200617/**
618 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
619 *
620 * Return: Returns 0 on success or an error code on failure.
621 */
622int tb_switch_reset(struct tb *tb, u64 route)
623{
624 struct tb_cfg_result res;
625 struct tb_regs_switch_header header = {
626 header.route_hi = route >> 32,
627 header.route_lo = route,
628 header.enabled = true,
629 };
630 tb_info(tb, "resetting switch at %llx\n", route);
631 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
632 0, 2, 2, 2);
633 if (res.err)
634 return res.err;
635 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
636 if (res.err > 0)
637 return -EIO;
638 return res.err;
639}
640
Andreas Noever053596d2014-06-03 22:04:06 +0200641struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
642{
643 u8 next_port = route; /*
644 * Routes use a stride of 8 bits,
645 * eventhough a port index has 6 bits at most.
646 * */
647 if (route == 0)
648 return sw;
649 if (next_port > sw->config.max_port_number)
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530650 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200651 if (tb_is_upstream_port(&sw->ports[next_port]))
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530652 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200653 if (!sw->ports[next_port].remote)
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530654 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200655 return get_switch_at_route(sw->ports[next_port].remote->sw,
656 route >> TB_ROUTE_SHIFT);
657}
658
Andreas Noevera25c8b22014-06-03 22:04:02 +0200659/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200660 * tb_plug_events_active() - enable/disable plug events on a switch
661 *
662 * Also configures a sane plug_events_delay of 255ms.
663 *
664 * Return: Returns 0 on success or an error code on failure.
665 */
666static int tb_plug_events_active(struct tb_switch *sw, bool active)
667{
668 u32 data;
669 int res;
670
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300671 if (!sw->config.enabled)
672 return 0;
673
Andreas Noeverca389f72014-06-03 22:04:04 +0200674 sw->config.plug_events_delay = 0xff;
675 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
676 if (res)
677 return res;
678
679 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
680 if (res)
681 return res;
682
683 if (active) {
684 data = data & 0xFFFFFF83;
685 switch (sw->config.device_id) {
Lukas Wunner1d111402016-03-20 13:57:20 +0100686 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
687 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
688 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
Andreas Noeverca389f72014-06-03 22:04:04 +0200689 break;
690 default:
691 data |= 4;
692 }
693 } else {
694 data = data | 0x7c;
695 }
696 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
697 sw->cap_plug_events + 1, 1);
698}
699
Mika Westerbergf67cf492017-06-06 15:25:16 +0300700static ssize_t authorized_show(struct device *dev,
701 struct device_attribute *attr,
702 char *buf)
703{
704 struct tb_switch *sw = tb_to_switch(dev);
705
706 return sprintf(buf, "%u\n", sw->authorized);
707}
708
709static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
710{
711 int ret = -EINVAL;
712
713 if (mutex_lock_interruptible(&switch_lock))
714 return -ERESTARTSYS;
715
716 if (sw->authorized)
717 goto unlock;
718
Mika Westerberga03e8282018-01-18 20:27:47 +0300719 /*
720 * Make sure there is no PCIe rescan ongoing when a new PCIe
721 * tunnel is created. Otherwise the PCIe rescan code might find
722 * the new tunnel too early.
723 */
724 pci_lock_rescan_remove();
725
Mika Westerbergf67cf492017-06-06 15:25:16 +0300726 switch (val) {
727 /* Approve switch */
728 case 1:
729 if (sw->key)
730 ret = tb_domain_approve_switch_key(sw->tb, sw);
731 else
732 ret = tb_domain_approve_switch(sw->tb, sw);
733 break;
734
735 /* Challenge switch */
736 case 2:
737 if (sw->key)
738 ret = tb_domain_challenge_switch_key(sw->tb, sw);
739 break;
740
741 default:
742 break;
743 }
744
Mika Westerberga03e8282018-01-18 20:27:47 +0300745 pci_unlock_rescan_remove();
746
Mika Westerbergf67cf492017-06-06 15:25:16 +0300747 if (!ret) {
748 sw->authorized = val;
749 /* Notify status change to the userspace */
750 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
751 }
752
753unlock:
754 mutex_unlock(&switch_lock);
755 return ret;
756}
757
758static ssize_t authorized_store(struct device *dev,
759 struct device_attribute *attr,
760 const char *buf, size_t count)
761{
762 struct tb_switch *sw = tb_to_switch(dev);
763 unsigned int val;
764 ssize_t ret;
765
766 ret = kstrtouint(buf, 0, &val);
767 if (ret)
768 return ret;
769 if (val > 2)
770 return -EINVAL;
771
772 ret = tb_switch_set_authorized(sw, val);
773
774 return ret ? ret : count;
775}
776static DEVICE_ATTR_RW(authorized);
777
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300778static ssize_t device_show(struct device *dev, struct device_attribute *attr,
779 char *buf)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200780{
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300781 struct tb_switch *sw = tb_to_switch(dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200782
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300783 return sprintf(buf, "%#x\n", sw->device);
784}
785static DEVICE_ATTR_RO(device);
Andreas Noeverca389f72014-06-03 22:04:04 +0200786
Mika Westerberg72ee3392017-06-06 15:25:05 +0300787static ssize_t
788device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
789{
790 struct tb_switch *sw = tb_to_switch(dev);
791
792 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
793}
794static DEVICE_ATTR_RO(device_name);
795
Mika Westerbergf67cf492017-06-06 15:25:16 +0300796static ssize_t key_show(struct device *dev, struct device_attribute *attr,
797 char *buf)
798{
799 struct tb_switch *sw = tb_to_switch(dev);
800 ssize_t ret;
801
802 if (mutex_lock_interruptible(&switch_lock))
803 return -ERESTARTSYS;
804
805 if (sw->key)
806 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
807 else
808 ret = sprintf(buf, "\n");
809
810 mutex_unlock(&switch_lock);
811 return ret;
812}
813
814static ssize_t key_store(struct device *dev, struct device_attribute *attr,
815 const char *buf, size_t count)
816{
817 struct tb_switch *sw = tb_to_switch(dev);
818 u8 key[TB_SWITCH_KEY_SIZE];
819 ssize_t ret = count;
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +0300820 bool clear = false;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300821
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +0300822 if (!strcmp(buf, "\n"))
823 clear = true;
824 else if (hex2bin(key, buf, sizeof(key)))
Mika Westerbergf67cf492017-06-06 15:25:16 +0300825 return -EINVAL;
826
827 if (mutex_lock_interruptible(&switch_lock))
828 return -ERESTARTSYS;
829
830 if (sw->authorized) {
831 ret = -EBUSY;
832 } else {
833 kfree(sw->key);
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +0300834 if (clear) {
835 sw->key = NULL;
836 } else {
837 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
838 if (!sw->key)
839 ret = -ENOMEM;
840 }
Mika Westerbergf67cf492017-06-06 15:25:16 +0300841 }
842
843 mutex_unlock(&switch_lock);
844 return ret;
845}
Bernat, Yehezkel0956e412017-08-15 08:19:12 +0300846static DEVICE_ATTR(key, 0600, key_show, key_store);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300847
Mika Westerberge6b245c2017-06-06 15:25:17 +0300848static ssize_t nvm_authenticate_show(struct device *dev,
849 struct device_attribute *attr, char *buf)
850{
851 struct tb_switch *sw = tb_to_switch(dev);
852 u32 status;
853
854 nvm_get_auth_status(sw, &status);
855 return sprintf(buf, "%#x\n", status);
856}
857
858static ssize_t nvm_authenticate_store(struct device *dev,
859 struct device_attribute *attr, const char *buf, size_t count)
860{
861 struct tb_switch *sw = tb_to_switch(dev);
862 bool val;
863 int ret;
864
865 if (mutex_lock_interruptible(&switch_lock))
866 return -ERESTARTSYS;
867
868 /* If NVMem devices are not yet added */
869 if (!sw->nvm) {
870 ret = -EAGAIN;
871 goto exit_unlock;
872 }
873
874 ret = kstrtobool(buf, &val);
875 if (ret)
876 goto exit_unlock;
877
878 /* Always clear the authentication status */
879 nvm_clear_auth_status(sw);
880
881 if (val) {
882 ret = nvm_validate_and_write(sw);
883 if (ret)
884 goto exit_unlock;
885
886 sw->nvm->authenticating = true;
887
888 if (!tb_route(sw))
889 ret = nvm_authenticate_host(sw);
890 else
891 ret = nvm_authenticate_device(sw);
892 }
893
894exit_unlock:
895 mutex_unlock(&switch_lock);
896
897 if (ret)
898 return ret;
899 return count;
900}
901static DEVICE_ATTR_RW(nvm_authenticate);
902
903static ssize_t nvm_version_show(struct device *dev,
904 struct device_attribute *attr, char *buf)
905{
906 struct tb_switch *sw = tb_to_switch(dev);
907 int ret;
908
909 if (mutex_lock_interruptible(&switch_lock))
910 return -ERESTARTSYS;
911
912 if (sw->safe_mode)
913 ret = -ENODATA;
914 else if (!sw->nvm)
915 ret = -EAGAIN;
916 else
917 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
918
919 mutex_unlock(&switch_lock);
920
921 return ret;
922}
923static DEVICE_ATTR_RO(nvm_version);
924
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300925static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
926 char *buf)
927{
928 struct tb_switch *sw = tb_to_switch(dev);
929
930 return sprintf(buf, "%#x\n", sw->vendor);
931}
932static DEVICE_ATTR_RO(vendor);
933
Mika Westerberg72ee3392017-06-06 15:25:05 +0300934static ssize_t
935vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
936{
937 struct tb_switch *sw = tb_to_switch(dev);
938
939 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
940}
941static DEVICE_ATTR_RO(vendor_name);
942
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300943static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
944 char *buf)
945{
946 struct tb_switch *sw = tb_to_switch(dev);
947
948 return sprintf(buf, "%pUb\n", sw->uuid);
949}
950static DEVICE_ATTR_RO(unique_id);
951
952static struct attribute *switch_attrs[] = {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300953 &dev_attr_authorized.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300954 &dev_attr_device.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +0300955 &dev_attr_device_name.attr,
Mika Westerbergf67cf492017-06-06 15:25:16 +0300956 &dev_attr_key.attr,
Mika Westerberge6b245c2017-06-06 15:25:17 +0300957 &dev_attr_nvm_authenticate.attr,
958 &dev_attr_nvm_version.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300959 &dev_attr_vendor.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +0300960 &dev_attr_vendor_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300961 &dev_attr_unique_id.attr,
962 NULL,
963};
964
Mika Westerbergf67cf492017-06-06 15:25:16 +0300965static umode_t switch_attr_is_visible(struct kobject *kobj,
966 struct attribute *attr, int n)
967{
968 struct device *dev = container_of(kobj, struct device, kobj);
969 struct tb_switch *sw = tb_to_switch(dev);
970
971 if (attr == &dev_attr_key.attr) {
972 if (tb_route(sw) &&
973 sw->tb->security_level == TB_SECURITY_SECURE &&
974 sw->security_level == TB_SECURITY_SECURE)
975 return attr->mode;
976 return 0;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300977 } else if (attr == &dev_attr_nvm_authenticate.attr ||
978 attr == &dev_attr_nvm_version.attr) {
979 if (sw->dma_port)
980 return attr->mode;
981 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300982 }
983
Mika Westerberge6b245c2017-06-06 15:25:17 +0300984 return sw->safe_mode ? 0 : attr->mode;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300985}
986
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300987static struct attribute_group switch_group = {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300988 .is_visible = switch_attr_is_visible,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300989 .attrs = switch_attrs,
990};
991
992static const struct attribute_group *switch_groups[] = {
993 &switch_group,
994 NULL,
995};
996
997static void tb_switch_release(struct device *dev)
998{
999 struct tb_switch *sw = tb_to_switch(dev);
1000
Mika Westerberg3e136762017-06-06 15:25:14 +03001001 dma_port_free(sw->dma_port);
1002
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001003 kfree(sw->uuid);
Mika Westerberg72ee3392017-06-06 15:25:05 +03001004 kfree(sw->device_name);
1005 kfree(sw->vendor_name);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001006 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +02001007 kfree(sw->drom);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001008 kfree(sw->key);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001009 kfree(sw);
1010}
1011
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001012struct device_type tb_switch_type = {
1013 .name = "thunderbolt_device",
1014 .release = tb_switch_release,
1015};
1016
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001017static int tb_switch_get_generation(struct tb_switch *sw)
1018{
1019 switch (sw->config.device_id) {
1020 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1021 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1022 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1023 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1024 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1025 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1026 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1027 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1028 return 1;
1029
1030 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1031 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1032 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1033 return 2;
1034
1035 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1036 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1037 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1038 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1039 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1040 return 3;
1041
1042 default:
1043 /*
1044 * For unknown switches assume generation to be 1 to be
1045 * on the safe side.
1046 */
1047 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1048 sw->config.device_id);
1049 return 1;
1050 }
1051}
1052
Andreas Noevera25c8b22014-06-03 22:04:02 +02001053/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001054 * tb_switch_alloc() - allocate a switch
1055 * @tb: Pointer to the owning domain
1056 * @parent: Parent device for this switch
1057 * @route: Route string for this switch
Andreas Noevera25c8b22014-06-03 22:04:02 +02001058 *
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001059 * Allocates and initializes a switch. Will not upload configuration to
1060 * the switch. For that you need to call tb_switch_configure()
1061 * separately. The returned switch should be released by calling
1062 * tb_switch_put().
1063 *
1064 * Return: Pointer to the allocated switch or %NULL in case of failure
Andreas Noevera25c8b22014-06-03 22:04:02 +02001065 */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001066struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1067 u64 route)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001068{
1069 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +02001070 int cap;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001071 struct tb_switch *sw;
1072 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
1073 if (upstream_port < 0)
1074 return NULL;
1075
1076 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1077 if (!sw)
1078 return NULL;
1079
1080 sw->tb = tb;
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001081 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001082 goto err_free_sw_ports;
1083
1084 tb_info(tb, "current switch config:\n");
Andreas Noevera25c8b22014-06-03 22:04:02 +02001085 tb_dump_switch(tb, &sw->config);
1086
1087 /* configure switch */
1088 sw->config.upstream_port_number = upstream_port;
1089 sw->config.depth = tb_route_length(route);
1090 sw->config.route_lo = route;
1091 sw->config.route_hi = route >> 32;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001092 sw->config.enabled = 0;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001093
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001094 /* initialize ports */
1095 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
1096 GFP_KERNEL);
1097 if (!sw->ports)
1098 goto err_free_sw_ports;
1099
1100 for (i = 0; i <= sw->config.max_port_number; i++) {
1101 /* minimum setup for tb_find_cap and tb_drom_read to work */
1102 sw->ports[i].sw = sw;
1103 sw->ports[i].port = i;
1104 }
1105
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001106 sw->generation = tb_switch_get_generation(sw);
1107
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001108 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
1109 if (cap < 0) {
1110 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1111 goto err_free_sw_ports;
1112 }
1113 sw->cap_plug_events = cap;
1114
Mika Westerbergf67cf492017-06-06 15:25:16 +03001115 /* Root switch is always authorized */
1116 if (!route)
1117 sw->authorized = true;
1118
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001119 device_initialize(&sw->dev);
1120 sw->dev.parent = parent;
1121 sw->dev.bus = &tb_bus_type;
1122 sw->dev.type = &tb_switch_type;
1123 sw->dev.groups = switch_groups;
1124 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1125
1126 return sw;
1127
1128err_free_sw_ports:
1129 kfree(sw->ports);
1130 kfree(sw);
1131
1132 return NULL;
1133}
1134
1135/**
Mika Westerberge6b245c2017-06-06 15:25:17 +03001136 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1137 * @tb: Pointer to the owning domain
1138 * @parent: Parent device for this switch
1139 * @route: Route string for this switch
1140 *
1141 * This creates a switch in safe mode. This means the switch pretty much
1142 * lacks all capabilities except DMA configuration port before it is
1143 * flashed with a valid NVM firmware.
1144 *
1145 * The returned switch must be released by calling tb_switch_put().
1146 *
1147 * Return: Pointer to the allocated switch or %NULL in case of failure
1148 */
1149struct tb_switch *
1150tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
1151{
1152 struct tb_switch *sw;
1153
1154 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1155 if (!sw)
1156 return NULL;
1157
1158 sw->tb = tb;
1159 sw->config.depth = tb_route_length(route);
1160 sw->config.route_hi = upper_32_bits(route);
1161 sw->config.route_lo = lower_32_bits(route);
1162 sw->safe_mode = true;
1163
1164 device_initialize(&sw->dev);
1165 sw->dev.parent = parent;
1166 sw->dev.bus = &tb_bus_type;
1167 sw->dev.type = &tb_switch_type;
1168 sw->dev.groups = switch_groups;
1169 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1170
1171 return sw;
1172}
1173
1174/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001175 * tb_switch_configure() - Uploads configuration to the switch
1176 * @sw: Switch to configure
1177 *
1178 * Call this function before the switch is added to the system. It will
1179 * upload configuration to the switch and makes it available for the
1180 * connection manager to use.
1181 *
1182 * Return: %0 in case of success and negative errno in case of failure
1183 */
1184int tb_switch_configure(struct tb_switch *sw)
1185{
1186 struct tb *tb = sw->tb;
1187 u64 route;
1188 int ret;
1189
1190 route = tb_route(sw);
1191 tb_info(tb,
1192 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1193 route, tb_route_length(route), sw->config.upstream_port_number);
1194
1195 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001196 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
1197 sw->config.vendor_id);
1198
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001199 sw->config.enabled = 1;
1200
Andreas Noevera25c8b22014-06-03 22:04:02 +02001201 /* upload configuration */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001202 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
1203 if (ret)
1204 return ret;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001205
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001206 return tb_plug_events_active(sw, true);
1207}
Andreas Noevera25c8b22014-06-03 22:04:02 +02001208
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001209static void tb_switch_set_uuid(struct tb_switch *sw)
1210{
1211 u32 uuid[4];
1212 int cap;
1213
1214 if (sw->uuid)
1215 return;
1216
1217 /*
1218 * The newer controllers include fused UUID as part of link
1219 * controller specific registers
1220 */
1221 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
1222 if (cap > 0) {
1223 tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4);
1224 } else {
1225 /*
1226 * ICM generates UUID based on UID and fills the upper
1227 * two words with ones. This is not strictly following
1228 * UUID format but we want to be compatible with it so
1229 * we do the same here.
1230 */
1231 uuid[0] = sw->uid & 0xffffffff;
1232 uuid[1] = (sw->uid >> 32) & 0xffffffff;
1233 uuid[2] = 0xffffffff;
1234 uuid[3] = 0xffffffff;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001235 }
1236
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001237 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1238}
1239
Mika Westerberge6b245c2017-06-06 15:25:17 +03001240static int tb_switch_add_dma_port(struct tb_switch *sw)
Mika Westerberg3e136762017-06-06 15:25:14 +03001241{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001242 u32 status;
1243 int ret;
1244
Mika Westerberg3e136762017-06-06 15:25:14 +03001245 switch (sw->generation) {
1246 case 3:
1247 break;
1248
1249 case 2:
1250 /* Only root switch can be upgraded */
1251 if (tb_route(sw))
Mika Westerberge6b245c2017-06-06 15:25:17 +03001252 return 0;
Mika Westerberg3e136762017-06-06 15:25:14 +03001253 break;
1254
1255 default:
Mika Westerberge6b245c2017-06-06 15:25:17 +03001256 /*
1257 * DMA port is the only thing available when the switch
1258 * is in safe mode.
1259 */
1260 if (!sw->safe_mode)
1261 return 0;
1262 break;
Mika Westerberg3e136762017-06-06 15:25:14 +03001263 }
1264
Mika Westerberge6b245c2017-06-06 15:25:17 +03001265 if (sw->no_nvm_upgrade)
1266 return 0;
1267
Mika Westerberg3e136762017-06-06 15:25:14 +03001268 sw->dma_port = dma_port_alloc(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001269 if (!sw->dma_port)
1270 return 0;
1271
1272 /*
1273 * Check status of the previous flash authentication. If there
1274 * is one we need to power cycle the switch in any case to make
1275 * it functional again.
1276 */
1277 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
1278 if (ret <= 0)
1279 return ret;
1280
1281 if (status) {
1282 tb_sw_info(sw, "switch flash authentication failed\n");
1283 tb_switch_set_uuid(sw);
1284 nvm_set_auth_status(sw, status);
1285 }
1286
1287 tb_sw_info(sw, "power cycling the switch now\n");
1288 dma_port_power_cycle(sw->dma_port);
1289
1290 /*
1291 * We return error here which causes the switch adding failure.
1292 * It should appear back after power cycle is complete.
1293 */
1294 return -ESHUTDOWN;
Mika Westerberg3e136762017-06-06 15:25:14 +03001295}
1296
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001297/**
1298 * tb_switch_add() - Add a switch to the domain
1299 * @sw: Switch to add
1300 *
1301 * This is the last step in adding switch to the domain. It will read
1302 * identification information from DROM and initializes ports so that
1303 * they can be used to connect other switches. The switch will be
1304 * exposed to the userspace when this function successfully returns. To
1305 * remove and release the switch, call tb_switch_remove().
1306 *
1307 * Return: %0 in case of success and negative errno in case of failure
1308 */
1309int tb_switch_add(struct tb_switch *sw)
1310{
1311 int i, ret;
Andreas Noeverca389f72014-06-03 22:04:04 +02001312
Mika Westerberg3e136762017-06-06 15:25:14 +03001313 /*
1314 * Initialize DMA control port now before we read DROM. Recent
1315 * host controllers have more complete DROM on NVM that includes
1316 * vendor and model identification strings which we then expose
1317 * to the userspace. NVM can be accessed through DMA
1318 * configuration based mailbox.
1319 */
Mika Westerberge6b245c2017-06-06 15:25:17 +03001320 ret = tb_switch_add_dma_port(sw);
1321 if (ret)
Mika Westerbergf53e7672017-06-06 15:25:02 +03001322 return ret;
Andreas Noever343fcb82014-06-12 23:11:47 +02001323
Mika Westerberge6b245c2017-06-06 15:25:17 +03001324 if (!sw->safe_mode) {
1325 /* read drom */
1326 ret = tb_drom_read(sw);
1327 if (ret) {
1328 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001329 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001330 }
1331 tb_sw_info(sw, "uid: %#llx\n", sw->uid);
1332
1333 tb_switch_set_uuid(sw);
1334
1335 for (i = 0; i <= sw->config.max_port_number; i++) {
1336 if (sw->ports[i].disabled) {
1337 tb_port_info(&sw->ports[i], "disabled by eeprom\n");
1338 continue;
1339 }
1340 ret = tb_init_port(&sw->ports[i]);
1341 if (ret)
1342 return ret;
1343 }
Andreas Noever343fcb82014-06-12 23:11:47 +02001344 }
1345
Mika Westerberge6b245c2017-06-06 15:25:17 +03001346 ret = device_add(&sw->dev);
1347 if (ret)
1348 return ret;
1349
1350 ret = tb_switch_nvm_add(sw);
1351 if (ret)
1352 device_del(&sw->dev);
1353
1354 return ret;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001355}
Andreas Noeverc90553b2014-06-03 22:04:11 +02001356
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001357/**
1358 * tb_switch_remove() - Remove and release a switch
1359 * @sw: Switch to remove
1360 *
1361 * This will remove the switch from the domain and release it after last
1362 * reference count drops to zero. If there are switches connected below
1363 * this switch, they will be removed as well.
1364 */
1365void tb_switch_remove(struct tb_switch *sw)
1366{
1367 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +02001368
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001369 /* port 0 is the switch itself and never has a remote */
1370 for (i = 1; i <= sw->config.max_port_number; i++) {
1371 if (tb_is_upstream_port(&sw->ports[i]))
1372 continue;
1373 if (sw->ports[i].remote)
1374 tb_switch_remove(sw->ports[i].remote->sw);
1375 sw->ports[i].remote = NULL;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001376 if (sw->ports[i].xdomain)
1377 tb_xdomain_remove(sw->ports[i].xdomain);
1378 sw->ports[i].xdomain = NULL;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001379 }
1380
1381 if (!sw->is_unplugged)
1382 tb_plug_events_active(sw, false);
1383
Mika Westerberge6b245c2017-06-06 15:25:17 +03001384 tb_switch_nvm_remove(sw);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001385 device_unregister(&sw->dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001386}
1387
Andreas Noever053596d2014-06-03 22:04:06 +02001388/**
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001389 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
Andreas Noever053596d2014-06-03 22:04:06 +02001390 */
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001391void tb_sw_set_unplugged(struct tb_switch *sw)
Andreas Noever053596d2014-06-03 22:04:06 +02001392{
1393 int i;
1394 if (sw == sw->tb->root_switch) {
1395 tb_sw_WARN(sw, "cannot unplug root switch\n");
1396 return;
1397 }
1398 if (sw->is_unplugged) {
1399 tb_sw_WARN(sw, "is_unplugged already set\n");
1400 return;
1401 }
1402 sw->is_unplugged = true;
1403 for (i = 0; i <= sw->config.max_port_number; i++) {
1404 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001405 tb_sw_set_unplugged(sw->ports[i].remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +02001406 }
1407}
1408
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001409int tb_switch_resume(struct tb_switch *sw)
1410{
1411 int i, err;
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001412 tb_sw_info(sw, "resuming switch\n");
1413
Mika Westerberg08a5e4c2017-06-06 15:24:54 +03001414 /*
1415 * Check for UID of the connected switches except for root
1416 * switch which we assume cannot be removed.
1417 */
1418 if (tb_route(sw)) {
1419 u64 uid;
1420
1421 err = tb_drom_read_uid_only(sw, &uid);
1422 if (err) {
1423 tb_sw_warn(sw, "uid read failed\n");
1424 return err;
1425 }
1426 if (sw->uid != uid) {
1427 tb_sw_info(sw,
1428 "changed while suspended (uid %#llx -> %#llx)\n",
1429 sw->uid, uid);
1430 return -ENODEV;
1431 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001432 }
1433
1434 /* upload configuration */
1435 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
1436 if (err)
1437 return err;
1438
1439 err = tb_plug_events_active(sw, true);
1440 if (err)
1441 return err;
1442
1443 /* check for surviving downstream switches */
1444 for (i = 1; i <= sw->config.max_port_number; i++) {
1445 struct tb_port *port = &sw->ports[i];
1446 if (tb_is_upstream_port(port))
1447 continue;
1448 if (!port->remote)
1449 continue;
1450 if (tb_wait_for_port(port, true) <= 0
1451 || tb_switch_resume(port->remote->sw)) {
1452 tb_port_warn(port,
1453 "lost during suspend, disconnecting\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001454 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001455 }
1456 }
1457 return 0;
1458}
1459
1460void tb_switch_suspend(struct tb_switch *sw)
1461{
1462 int i, err;
1463 err = tb_plug_events_active(sw, false);
1464 if (err)
1465 return;
1466
1467 for (i = 1; i <= sw->config.max_port_number; i++) {
1468 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
1469 tb_switch_suspend(sw->ports[i].remote->sw);
1470 }
1471 /*
1472 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
1473 * effect?
1474 */
1475}
Mika Westerbergf67cf492017-06-06 15:25:16 +03001476
1477struct tb_sw_lookup {
1478 struct tb *tb;
1479 u8 link;
1480 u8 depth;
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02001481 const uuid_t *uuid;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001482};
1483
1484static int tb_switch_match(struct device *dev, void *data)
1485{
1486 struct tb_switch *sw = tb_to_switch(dev);
1487 struct tb_sw_lookup *lookup = data;
1488
1489 if (!sw)
1490 return 0;
1491 if (sw->tb != lookup->tb)
1492 return 0;
1493
1494 if (lookup->uuid)
1495 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
1496
1497 /* Root switch is matched only by depth */
1498 if (!lookup->depth)
1499 return !sw->depth;
1500
1501 return sw->link == lookup->link && sw->depth == lookup->depth;
1502}
1503
1504/**
1505 * tb_switch_find_by_link_depth() - Find switch by link and depth
1506 * @tb: Domain the switch belongs
1507 * @link: Link number the switch is connected
1508 * @depth: Depth of the switch in link
1509 *
1510 * Returned switch has reference count increased so the caller needs to
1511 * call tb_switch_put() when done with the switch.
1512 */
1513struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
1514{
1515 struct tb_sw_lookup lookup;
1516 struct device *dev;
1517
1518 memset(&lookup, 0, sizeof(lookup));
1519 lookup.tb = tb;
1520 lookup.link = link;
1521 lookup.depth = depth;
1522
1523 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1524 if (dev)
1525 return tb_to_switch(dev);
1526
1527 return NULL;
1528}
1529
1530/**
1531 * tb_switch_find_by_link_depth() - Find switch by UUID
1532 * @tb: Domain the switch belongs
1533 * @uuid: UUID to look for
1534 *
1535 * Returned switch has reference count increased so the caller needs to
1536 * call tb_switch_put() when done with the switch.
1537 */
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02001538struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001539{
1540 struct tb_sw_lookup lookup;
1541 struct device *dev;
1542
1543 memset(&lookup, 0, sizeof(lookup));
1544 lookup.tb = tb;
1545 lookup.uuid = uuid;
1546
1547 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1548 if (dev)
1549 return tb_to_switch(dev);
1550
1551 return NULL;
1552}
Mika Westerberge6b245c2017-06-06 15:25:17 +03001553
1554void tb_switch_exit(void)
1555{
1556 ida_destroy(&nvm_ida);
1557}