blob: c9a7e4a779cd0e1f7048efa71f15821c208b8bc8 [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) {
Mika Westerberg62efe692018-09-17 16:32:13 +0300496 tb_port_dbg(port, "is disabled (state: 0)\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200497 return 0;
498 }
499 if (state == TB_PORT_UNPLUGGED) {
500 if (wait_if_unplugged) {
501 /* used during resume */
Mika Westerberg62efe692018-09-17 16:32:13 +0300502 tb_port_dbg(port,
503 "is unplugged (state: 7), retrying...\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200504 msleep(100);
505 continue;
506 }
Mika Westerberg62efe692018-09-17 16:32:13 +0300507 tb_port_dbg(port, "is unplugged (state: 7)\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200508 return 0;
509 }
510 if (state == TB_PORT_UP) {
Mika Westerberg62efe692018-09-17 16:32:13 +0300511 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200512 return 1;
513 }
514
515 /*
516 * After plug-in the state is TB_PORT_CONNECTING. Give it some
517 * time.
518 */
Mika Westerberg62efe692018-09-17 16:32:13 +0300519 tb_port_dbg(port,
520 "is connected, link is not up (state: %d), retrying...\n",
521 state);
Andreas Noever9da672a2014-06-03 22:04:05 +0200522 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{
Mika Westerbergc5ee6fe2018-10-11 11:38:22 +0300539 u32 nfc_credits;
540
541 if (credits == 0 || port->sw->is_unplugged)
Andreas Noever520b6702014-06-03 22:04:07 +0200542 return 0;
Mika Westerbergc5ee6fe2018-10-11 11:38:22 +0300543
544 nfc_credits = port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK;
545 nfc_credits += credits;
546
547 tb_port_dbg(port, "adding %d NFC credits to %lu",
548 credits, port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK);
549
550 port->config.nfc_credits &= ~TB_PORT_NFC_CREDITS_MASK;
551 port->config.nfc_credits |= nfc_credits;
552
Andreas Noever520b6702014-06-03 22:04:07 +0200553 return tb_port_write(port, &port->config.nfc_credits,
554 TB_CFG_PORT, 4, 1);
555}
556
557/**
Mika Westerberg44242d62018-09-28 16:35:32 +0300558 * tb_port_set_initial_credits() - Set initial port link credits allocated
559 * @port: Port to set the initial credits
560 * @credits: Number of credits to to allocate
561 *
562 * Set initial credits value to be used for ingress shared buffering.
563 */
564int tb_port_set_initial_credits(struct tb_port *port, u32 credits)
565{
566 u32 data;
567 int ret;
568
569 ret = tb_port_read(port, &data, TB_CFG_PORT, 5, 1);
570 if (ret)
571 return ret;
572
573 data &= ~TB_PORT_LCA_MASK;
574 data |= (credits << TB_PORT_LCA_SHIFT) & TB_PORT_LCA_MASK;
575
576 return tb_port_write(port, &data, TB_CFG_PORT, 5, 1);
577}
578
579/**
Andreas Noever520b6702014-06-03 22:04:07 +0200580 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
581 *
582 * Return: Returns 0 on success or an error code on failure.
583 */
584int tb_port_clear_counter(struct tb_port *port, int counter)
585{
586 u32 zero[3] = { 0, 0, 0 };
Mika Westerberg62efe692018-09-17 16:32:13 +0300587 tb_port_dbg(port, "clearing counter %d\n", counter);
Andreas Noever520b6702014-06-03 22:04:07 +0200588 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
589}
590
591/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200592 * tb_init_port() - initialize a port
593 *
594 * This is a helper method for tb_switch_alloc. Does not check or initialize
595 * any downstream switches.
596 *
597 * Return: Returns 0 on success or an error code on failure.
598 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200599static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200600{
601 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200602 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200603
Andreas Noevera25c8b22014-06-03 22:04:02 +0200604 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
605 if (res)
606 return res;
607
Andreas Noever9da672a2014-06-03 22:04:05 +0200608 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200609 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Mika Westerbergda2da042017-06-06 15:24:58 +0300610 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
Andreas Noever9da672a2014-06-03 22:04:05 +0200611
612 if (cap > 0)
613 port->cap_phy = cap;
614 else
615 tb_port_WARN(port, "non switch port without a PHY\n");
Mika Westerberg56183c82017-02-19 10:39:34 +0200616 } else if (port->port != 0) {
617 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
618 if (cap > 0)
619 port->cap_adap = cap;
Andreas Noever9da672a2014-06-03 22:04:05 +0200620 }
621
Andreas Noever343fcb82014-06-12 23:11:47 +0200622 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200623
Mika Westerberg0b2863a2017-02-19 16:57:27 +0200624 /* Control port does not need HopID allocation */
625 if (port->port) {
626 ida_init(&port->in_hopids);
627 ida_init(&port->out_hopids);
628 }
629
Andreas Noevera25c8b22014-06-03 22:04:02 +0200630 return 0;
631
632}
633
Mika Westerberg0b2863a2017-02-19 16:57:27 +0200634static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
635 int max_hopid)
636{
637 int port_max_hopid;
638 struct ida *ida;
639
640 if (in) {
641 port_max_hopid = port->config.max_in_hop_id;
642 ida = &port->in_hopids;
643 } else {
644 port_max_hopid = port->config.max_out_hop_id;
645 ida = &port->out_hopids;
646 }
647
648 /* HopIDs 0-7 are reserved */
649 if (min_hopid < TB_PATH_MIN_HOPID)
650 min_hopid = TB_PATH_MIN_HOPID;
651
652 if (max_hopid < 0 || max_hopid > port_max_hopid)
653 max_hopid = port_max_hopid;
654
655 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
656}
657
658/**
659 * tb_port_alloc_in_hopid() - Allocate input HopID from port
660 * @port: Port to allocate HopID for
661 * @min_hopid: Minimum acceptable input HopID
662 * @max_hopid: Maximum acceptable input HopID
663 *
664 * Return: HopID between @min_hopid and @max_hopid or negative errno in
665 * case of error.
666 */
667int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
668{
669 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
670}
671
672/**
673 * tb_port_alloc_out_hopid() - Allocate output HopID from port
674 * @port: Port to allocate HopID for
675 * @min_hopid: Minimum acceptable output HopID
676 * @max_hopid: Maximum acceptable output HopID
677 *
678 * Return: HopID between @min_hopid and @max_hopid or negative errno in
679 * case of error.
680 */
681int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
682{
683 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
684}
685
686/**
687 * tb_port_release_in_hopid() - Release allocated input HopID from port
688 * @port: Port whose HopID to release
689 * @hopid: HopID to release
690 */
691void tb_port_release_in_hopid(struct tb_port *port, int hopid)
692{
693 ida_simple_remove(&port->in_hopids, hopid);
694}
695
696/**
697 * tb_port_release_out_hopid() - Release allocated output HopID from port
698 * @port: Port whose HopID to release
699 * @hopid: HopID to release
700 */
701void tb_port_release_out_hopid(struct tb_port *port, int hopid)
702{
703 ida_simple_remove(&port->out_hopids, hopid);
704}
705
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200706/**
Mika Westerbergfb19fac2017-02-19 21:51:30 +0200707 * tb_next_port_on_path() - Return next port for given port on a path
708 * @start: Start port of the walk
709 * @end: End port of the walk
710 * @prev: Previous port (%NULL if this is the first)
711 *
712 * This function can be used to walk from one port to another if they
713 * are connected through zero or more switches. If the @prev is dual
714 * link port, the function follows that link and returns another end on
715 * that same link.
716 *
717 * If the @end port has been reached, return %NULL.
718 *
719 * Domain tb->lock must be held when this function is called.
720 */
721struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
722 struct tb_port *prev)
723{
724 struct tb_port *next;
725
726 if (!prev)
727 return start;
728
729 if (prev->sw == end->sw) {
730 if (prev == end)
731 return NULL;
732 return end;
733 }
734
735 if (start->sw->config.depth < end->sw->config.depth) {
736 if (prev->remote &&
737 prev->remote->sw->config.depth > prev->sw->config.depth)
738 next = prev->remote;
739 else
740 next = tb_port_at(tb_route(end->sw), prev->sw);
741 } else {
742 if (tb_is_upstream_port(prev)) {
743 next = prev->remote;
744 } else {
745 next = tb_upstream_port(prev->sw);
746 /*
747 * Keep the same link if prev and next are both
748 * dual link ports.
749 */
750 if (next->dual_link_port &&
751 next->link_nr != prev->link_nr) {
752 next = next->dual_link_port;
753 }
754 }
755 }
756
757 return next;
758}
759
760/**
Mika Westerberge78db6f2017-10-12 16:45:50 +0300761 * tb_port_is_enabled() - Is the adapter port enabled
762 * @port: Port to check
763 */
764bool tb_port_is_enabled(struct tb_port *port)
765{
766 switch (port->config.type) {
767 case TB_TYPE_PCIE_UP:
768 case TB_TYPE_PCIE_DOWN:
769 return tb_pci_port_is_enabled(port);
770
Mika Westerberg4f807e42018-09-17 16:30:49 +0300771 case TB_TYPE_DP_HDMI_IN:
772 case TB_TYPE_DP_HDMI_OUT:
773 return tb_dp_port_is_enabled(port);
774
Mika Westerberge78db6f2017-10-12 16:45:50 +0300775 default:
776 return false;
777 }
778}
779
780/**
Mika Westerberg0414bec2017-02-19 23:43:26 +0200781 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
782 * @port: PCIe port to check
783 */
784bool tb_pci_port_is_enabled(struct tb_port *port)
785{
786 u32 data;
787
788 if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1))
789 return false;
790
791 return !!(data & TB_PCI_EN);
792}
793
794/**
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200795 * tb_pci_port_enable() - Enable PCIe adapter port
796 * @port: PCIe port to enable
797 * @enable: Enable/disable the PCIe adapter
798 */
799int tb_pci_port_enable(struct tb_port *port, bool enable)
800{
801 u32 word = enable ? TB_PCI_EN : 0x0;
802 if (!port->cap_adap)
803 return -ENXIO;
804 return tb_port_write(port, &word, TB_CFG_PORT, port->cap_adap, 1);
805}
806
Mika Westerberg4f807e42018-09-17 16:30:49 +0300807/**
808 * tb_dp_port_hpd_is_active() - Is HPD already active
809 * @port: DP out port to check
810 *
811 * Checks if the DP OUT adapter port has HDP bit already set.
812 */
813int tb_dp_port_hpd_is_active(struct tb_port *port)
814{
815 u32 data;
816 int ret;
817
818 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 2, 1);
819 if (ret)
820 return ret;
821
822 return !!(data & TB_DP_HDP);
823}
824
825/**
826 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
827 * @port: Port to clear HPD
828 *
829 * If the DP IN port has HDP set, this function can be used to clear it.
830 */
831int tb_dp_port_hpd_clear(struct tb_port *port)
832{
833 u32 data;
834 int ret;
835
836 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1);
837 if (ret)
838 return ret;
839
840 data |= TB_DP_HPDC;
841 return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1);
842}
843
844/**
845 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
846 * @port: DP IN/OUT port to set hops
847 * @video: Video Hop ID
848 * @aux_tx: AUX TX Hop ID
849 * @aux_rx: AUX RX Hop ID
850 *
851 * Programs specified Hop IDs for DP IN/OUT port.
852 */
853int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
854 unsigned int aux_tx, unsigned int aux_rx)
855{
856 u32 data[2];
857 int ret;
858
859 ret = tb_port_read(port, data, TB_CFG_PORT, port->cap_adap,
860 ARRAY_SIZE(data));
861 if (ret)
862 return ret;
863
864 data[0] &= ~TB_DP_VIDEO_HOPID_MASK;
865 data[1] &= ~(TB_DP_AUX_RX_HOPID_MASK | TB_DP_AUX_TX_HOPID_MASK);
866
867 data[0] |= (video << TB_DP_VIDEO_HOPID_SHIFT) & TB_DP_VIDEO_HOPID_MASK;
868 data[1] |= aux_tx & TB_DP_AUX_TX_HOPID_MASK;
869 data[1] |= (aux_rx << TB_DP_AUX_RX_HOPID_SHIFT) & TB_DP_AUX_RX_HOPID_MASK;
870
871 return tb_port_write(port, data, TB_CFG_PORT, port->cap_adap,
872 ARRAY_SIZE(data));
873}
874
875/**
876 * tb_dp_port_is_enabled() - Is DP adapter port enabled
877 * @port: DP adapter port to check
878 */
879bool tb_dp_port_is_enabled(struct tb_port *port)
880{
881 u32 data;
882
883 if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1))
884 return false;
885
886 return !!(data & (TB_DP_VIDEO_EN | TB_DP_AUX_EN));
887}
888
889/**
890 * tb_dp_port_enable() - Enables/disables DP paths of a port
891 * @port: DP IN/OUT port
892 * @enable: Enable/disable DP path
893 *
894 * Once Hop IDs are programmed DP paths can be enabled or disabled by
895 * calling this function.
896 */
897int tb_dp_port_enable(struct tb_port *port, bool enable)
898{
899 u32 data;
900 int ret;
901
902 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1);
903 if (ret)
904 return ret;
905
906 if (enable)
907 data |= TB_DP_VIDEO_EN | TB_DP_AUX_EN;
908 else
909 data &= ~(TB_DP_VIDEO_EN | TB_DP_AUX_EN);
910
911 return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap, 1);
912}
913
Andreas Noevera25c8b22014-06-03 22:04:02 +0200914/* switch utility functions */
915
916static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
917{
Mika Westerbergdaa51402018-10-01 12:31:19 +0300918 tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
919 sw->vendor_id, sw->device_id, sw->revision,
920 sw->thunderbolt_version);
921 tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number);
922 tb_dbg(tb, " Config:\n");
923 tb_dbg(tb,
Andreas Noevera25c8b22014-06-03 22:04:02 +0200924 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
Mika Westerbergdaa51402018-10-01 12:31:19 +0300925 sw->upstream_port_number, sw->depth,
926 (((u64) sw->route_hi) << 32) | sw->route_lo,
927 sw->enabled, sw->plug_events_delay);
928 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
929 sw->__unknown1, sw->__unknown4);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200930}
931
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200932/**
933 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
934 *
935 * Return: Returns 0 on success or an error code on failure.
936 */
937int tb_switch_reset(struct tb *tb, u64 route)
938{
939 struct tb_cfg_result res;
940 struct tb_regs_switch_header header = {
941 header.route_hi = route >> 32,
942 header.route_lo = route,
943 header.enabled = true,
944 };
Mika Westerbergdaa51402018-10-01 12:31:19 +0300945 tb_dbg(tb, "resetting switch at %llx\n", route);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200946 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
947 0, 2, 2, 2);
948 if (res.err)
949 return res.err;
950 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
951 if (res.err > 0)
952 return -EIO;
953 return res.err;
954}
955
Andreas Noevera25c8b22014-06-03 22:04:02 +0200956/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200957 * tb_plug_events_active() - enable/disable plug events on a switch
958 *
959 * Also configures a sane plug_events_delay of 255ms.
960 *
961 * Return: Returns 0 on success or an error code on failure.
962 */
963static int tb_plug_events_active(struct tb_switch *sw, bool active)
964{
965 u32 data;
966 int res;
967
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300968 if (!sw->config.enabled)
969 return 0;
970
Andreas Noeverca389f72014-06-03 22:04:04 +0200971 sw->config.plug_events_delay = 0xff;
972 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
973 if (res)
974 return res;
975
976 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
977 if (res)
978 return res;
979
980 if (active) {
981 data = data & 0xFFFFFF83;
982 switch (sw->config.device_id) {
Lukas Wunner1d111402016-03-20 13:57:20 +0100983 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
984 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
985 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
Andreas Noeverca389f72014-06-03 22:04:04 +0200986 break;
987 default:
988 data |= 4;
989 }
990 } else {
991 data = data | 0x7c;
992 }
993 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
994 sw->cap_plug_events + 1, 1);
995}
996
Mika Westerbergf67cf492017-06-06 15:25:16 +0300997static ssize_t authorized_show(struct device *dev,
998 struct device_attribute *attr,
999 char *buf)
1000{
1001 struct tb_switch *sw = tb_to_switch(dev);
1002
1003 return sprintf(buf, "%u\n", sw->authorized);
1004}
1005
1006static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1007{
1008 int ret = -EINVAL;
1009
Mika Westerberg09f11b62019-03-19 16:48:41 +02001010 if (!mutex_trylock(&sw->tb->lock))
1011 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +03001012
1013 if (sw->authorized)
1014 goto unlock;
1015
Mika Westerberga03e8282018-01-18 20:27:47 +03001016 /*
1017 * Make sure there is no PCIe rescan ongoing when a new PCIe
1018 * tunnel is created. Otherwise the PCIe rescan code might find
1019 * the new tunnel too early.
1020 */
1021 pci_lock_rescan_remove();
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001022 pm_runtime_get_sync(&sw->dev);
Mika Westerberga03e8282018-01-18 20:27:47 +03001023
Mika Westerbergf67cf492017-06-06 15:25:16 +03001024 switch (val) {
1025 /* Approve switch */
1026 case 1:
1027 if (sw->key)
1028 ret = tb_domain_approve_switch_key(sw->tb, sw);
1029 else
1030 ret = tb_domain_approve_switch(sw->tb, sw);
1031 break;
1032
1033 /* Challenge switch */
1034 case 2:
1035 if (sw->key)
1036 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1037 break;
1038
1039 default:
1040 break;
1041 }
1042
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001043 pm_runtime_mark_last_busy(&sw->dev);
1044 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerberga03e8282018-01-18 20:27:47 +03001045 pci_unlock_rescan_remove();
1046
Mika Westerbergf67cf492017-06-06 15:25:16 +03001047 if (!ret) {
1048 sw->authorized = val;
1049 /* Notify status change to the userspace */
1050 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1051 }
1052
1053unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +02001054 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001055 return ret;
1056}
1057
1058static ssize_t authorized_store(struct device *dev,
1059 struct device_attribute *attr,
1060 const char *buf, size_t count)
1061{
1062 struct tb_switch *sw = tb_to_switch(dev);
1063 unsigned int val;
1064 ssize_t ret;
1065
1066 ret = kstrtouint(buf, 0, &val);
1067 if (ret)
1068 return ret;
1069 if (val > 2)
1070 return -EINVAL;
1071
1072 ret = tb_switch_set_authorized(sw, val);
1073
1074 return ret ? ret : count;
1075}
1076static DEVICE_ATTR_RW(authorized);
1077
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001078static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1079 char *buf)
1080{
1081 struct tb_switch *sw = tb_to_switch(dev);
1082
1083 return sprintf(buf, "%u\n", sw->boot);
1084}
1085static DEVICE_ATTR_RO(boot);
1086
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001087static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1088 char *buf)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001089{
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001090 struct tb_switch *sw = tb_to_switch(dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001091
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001092 return sprintf(buf, "%#x\n", sw->device);
1093}
1094static DEVICE_ATTR_RO(device);
Andreas Noeverca389f72014-06-03 22:04:04 +02001095
Mika Westerberg72ee3392017-06-06 15:25:05 +03001096static ssize_t
1097device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1098{
1099 struct tb_switch *sw = tb_to_switch(dev);
1100
1101 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
1102}
1103static DEVICE_ATTR_RO(device_name);
1104
Mika Westerbergf67cf492017-06-06 15:25:16 +03001105static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1106 char *buf)
1107{
1108 struct tb_switch *sw = tb_to_switch(dev);
1109 ssize_t ret;
1110
Mika Westerberg09f11b62019-03-19 16:48:41 +02001111 if (!mutex_trylock(&sw->tb->lock))
1112 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +03001113
1114 if (sw->key)
1115 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1116 else
1117 ret = sprintf(buf, "\n");
1118
Mika Westerberg09f11b62019-03-19 16:48:41 +02001119 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001120 return ret;
1121}
1122
1123static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1124 const char *buf, size_t count)
1125{
1126 struct tb_switch *sw = tb_to_switch(dev);
1127 u8 key[TB_SWITCH_KEY_SIZE];
1128 ssize_t ret = count;
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +03001129 bool clear = false;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001130
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +03001131 if (!strcmp(buf, "\n"))
1132 clear = true;
1133 else if (hex2bin(key, buf, sizeof(key)))
Mika Westerbergf67cf492017-06-06 15:25:16 +03001134 return -EINVAL;
1135
Mika Westerberg09f11b62019-03-19 16:48:41 +02001136 if (!mutex_trylock(&sw->tb->lock))
1137 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +03001138
1139 if (sw->authorized) {
1140 ret = -EBUSY;
1141 } else {
1142 kfree(sw->key);
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +03001143 if (clear) {
1144 sw->key = NULL;
1145 } else {
1146 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1147 if (!sw->key)
1148 ret = -ENOMEM;
1149 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001150 }
1151
Mika Westerberg09f11b62019-03-19 16:48:41 +02001152 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001153 return ret;
1154}
Bernat, Yehezkel0956e412017-08-15 08:19:12 +03001155static DEVICE_ATTR(key, 0600, key_show, key_store);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001156
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001157static void nvm_authenticate_start(struct tb_switch *sw)
1158{
1159 struct pci_dev *root_port;
1160
1161 /*
1162 * During host router NVM upgrade we should not allow root port to
1163 * go into D3cold because some root ports cannot trigger PME
1164 * itself. To be on the safe side keep the root port in D0 during
1165 * the whole upgrade process.
1166 */
1167 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1168 if (root_port)
1169 pm_runtime_get_noresume(&root_port->dev);
1170}
1171
1172static void nvm_authenticate_complete(struct tb_switch *sw)
1173{
1174 struct pci_dev *root_port;
1175
1176 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1177 if (root_port)
1178 pm_runtime_put(&root_port->dev);
1179}
1180
Mika Westerberge6b245c2017-06-06 15:25:17 +03001181static ssize_t nvm_authenticate_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1183{
1184 struct tb_switch *sw = tb_to_switch(dev);
1185 u32 status;
1186
1187 nvm_get_auth_status(sw, &status);
1188 return sprintf(buf, "%#x\n", status);
1189}
1190
1191static ssize_t nvm_authenticate_store(struct device *dev,
1192 struct device_attribute *attr, const char *buf, size_t count)
1193{
1194 struct tb_switch *sw = tb_to_switch(dev);
1195 bool val;
1196 int ret;
1197
Mika Westerberg09f11b62019-03-19 16:48:41 +02001198 if (!mutex_trylock(&sw->tb->lock))
1199 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +03001200
1201 /* If NVMem devices are not yet added */
1202 if (!sw->nvm) {
1203 ret = -EAGAIN;
1204 goto exit_unlock;
1205 }
1206
1207 ret = kstrtobool(buf, &val);
1208 if (ret)
1209 goto exit_unlock;
1210
1211 /* Always clear the authentication status */
1212 nvm_clear_auth_status(sw);
1213
1214 if (val) {
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001215 if (!sw->nvm->buf) {
1216 ret = -EINVAL;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001217 goto exit_unlock;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001218 }
1219
1220 pm_runtime_get_sync(&sw->dev);
1221 ret = nvm_validate_and_write(sw);
1222 if (ret) {
1223 pm_runtime_mark_last_busy(&sw->dev);
1224 pm_runtime_put_autosuspend(&sw->dev);
1225 goto exit_unlock;
1226 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001227
1228 sw->nvm->authenticating = true;
1229
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001230 if (!tb_route(sw)) {
1231 /*
1232 * Keep root port from suspending as long as the
1233 * NVM upgrade process is running.
1234 */
1235 nvm_authenticate_start(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001236 ret = nvm_authenticate_host(sw);
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001237 if (ret)
1238 nvm_authenticate_complete(sw);
1239 } else {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001240 ret = nvm_authenticate_device(sw);
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001241 }
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001242 pm_runtime_mark_last_busy(&sw->dev);
1243 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001244 }
1245
1246exit_unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +02001247 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001248
1249 if (ret)
1250 return ret;
1251 return count;
1252}
1253static DEVICE_ATTR_RW(nvm_authenticate);
1254
1255static ssize_t nvm_version_show(struct device *dev,
1256 struct device_attribute *attr, char *buf)
1257{
1258 struct tb_switch *sw = tb_to_switch(dev);
1259 int ret;
1260
Mika Westerberg09f11b62019-03-19 16:48:41 +02001261 if (!mutex_trylock(&sw->tb->lock))
1262 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +03001263
1264 if (sw->safe_mode)
1265 ret = -ENODATA;
1266 else if (!sw->nvm)
1267 ret = -EAGAIN;
1268 else
1269 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1270
Mika Westerberg09f11b62019-03-19 16:48:41 +02001271 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001272
1273 return ret;
1274}
1275static DEVICE_ATTR_RO(nvm_version);
1276
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001277static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1278 char *buf)
1279{
1280 struct tb_switch *sw = tb_to_switch(dev);
1281
1282 return sprintf(buf, "%#x\n", sw->vendor);
1283}
1284static DEVICE_ATTR_RO(vendor);
1285
Mika Westerberg72ee3392017-06-06 15:25:05 +03001286static ssize_t
1287vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1288{
1289 struct tb_switch *sw = tb_to_switch(dev);
1290
1291 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1292}
1293static DEVICE_ATTR_RO(vendor_name);
1294
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001295static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1296 char *buf)
1297{
1298 struct tb_switch *sw = tb_to_switch(dev);
1299
1300 return sprintf(buf, "%pUb\n", sw->uuid);
1301}
1302static DEVICE_ATTR_RO(unique_id);
1303
1304static struct attribute *switch_attrs[] = {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001305 &dev_attr_authorized.attr,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001306 &dev_attr_boot.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001307 &dev_attr_device.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +03001308 &dev_attr_device_name.attr,
Mika Westerbergf67cf492017-06-06 15:25:16 +03001309 &dev_attr_key.attr,
Mika Westerberge6b245c2017-06-06 15:25:17 +03001310 &dev_attr_nvm_authenticate.attr,
1311 &dev_attr_nvm_version.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001312 &dev_attr_vendor.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +03001313 &dev_attr_vendor_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001314 &dev_attr_unique_id.attr,
1315 NULL,
1316};
1317
Mika Westerbergf67cf492017-06-06 15:25:16 +03001318static umode_t switch_attr_is_visible(struct kobject *kobj,
1319 struct attribute *attr, int n)
1320{
1321 struct device *dev = container_of(kobj, struct device, kobj);
1322 struct tb_switch *sw = tb_to_switch(dev);
1323
1324 if (attr == &dev_attr_key.attr) {
1325 if (tb_route(sw) &&
1326 sw->tb->security_level == TB_SECURITY_SECURE &&
1327 sw->security_level == TB_SECURITY_SECURE)
1328 return attr->mode;
1329 return 0;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001330 } else if (attr == &dev_attr_nvm_authenticate.attr ||
1331 attr == &dev_attr_nvm_version.attr) {
1332 if (sw->dma_port)
1333 return attr->mode;
1334 return 0;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001335 } else if (attr == &dev_attr_boot.attr) {
1336 if (tb_route(sw))
1337 return attr->mode;
1338 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001339 }
1340
Mika Westerberge6b245c2017-06-06 15:25:17 +03001341 return sw->safe_mode ? 0 : attr->mode;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001342}
1343
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001344static struct attribute_group switch_group = {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001345 .is_visible = switch_attr_is_visible,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001346 .attrs = switch_attrs,
1347};
1348
1349static const struct attribute_group *switch_groups[] = {
1350 &switch_group,
1351 NULL,
1352};
1353
1354static void tb_switch_release(struct device *dev)
1355{
1356 struct tb_switch *sw = tb_to_switch(dev);
Mika Westerberg0b2863a2017-02-19 16:57:27 +02001357 int i;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001358
Mika Westerberg3e136762017-06-06 15:25:14 +03001359 dma_port_free(sw->dma_port);
1360
Mika Westerberg0b2863a2017-02-19 16:57:27 +02001361 for (i = 1; i <= sw->config.max_port_number; i++) {
1362 if (!sw->ports[i].disabled) {
1363 ida_destroy(&sw->ports[i].in_hopids);
1364 ida_destroy(&sw->ports[i].out_hopids);
1365 }
1366 }
1367
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001368 kfree(sw->uuid);
Mika Westerberg72ee3392017-06-06 15:25:05 +03001369 kfree(sw->device_name);
1370 kfree(sw->vendor_name);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001371 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +02001372 kfree(sw->drom);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001373 kfree(sw->key);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001374 kfree(sw);
1375}
1376
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001377/*
1378 * Currently only need to provide the callbacks. Everything else is handled
1379 * in the connection manager.
1380 */
1381static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
1382{
1383 return 0;
1384}
1385
1386static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
1387{
1388 return 0;
1389}
1390
1391static const struct dev_pm_ops tb_switch_pm_ops = {
1392 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
1393 NULL)
1394};
1395
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001396struct device_type tb_switch_type = {
1397 .name = "thunderbolt_device",
1398 .release = tb_switch_release,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001399 .pm = &tb_switch_pm_ops,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001400};
1401
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001402static int tb_switch_get_generation(struct tb_switch *sw)
1403{
1404 switch (sw->config.device_id) {
1405 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1406 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1407 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1408 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1409 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1410 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1411 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1412 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1413 return 1;
1414
1415 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1416 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1417 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1418 return 2;
1419
1420 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1421 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1422 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1423 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1424 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001425 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1426 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1427 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001428 return 3;
1429
1430 default:
1431 /*
1432 * For unknown switches assume generation to be 1 to be
1433 * on the safe side.
1434 */
1435 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1436 sw->config.device_id);
1437 return 1;
1438 }
1439}
1440
Andreas Noevera25c8b22014-06-03 22:04:02 +02001441/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001442 * tb_switch_alloc() - allocate a switch
1443 * @tb: Pointer to the owning domain
1444 * @parent: Parent device for this switch
1445 * @route: Route string for this switch
Andreas Noevera25c8b22014-06-03 22:04:02 +02001446 *
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001447 * Allocates and initializes a switch. Will not upload configuration to
1448 * the switch. For that you need to call tb_switch_configure()
1449 * separately. The returned switch should be released by calling
1450 * tb_switch_put().
1451 *
Mika Westerberg444ac382018-12-30 12:17:52 +02001452 * Return: Pointer to the allocated switch or ERR_PTR() in case of
1453 * failure.
Andreas Noevera25c8b22014-06-03 22:04:02 +02001454 */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001455struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1456 u64 route)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001457{
Andreas Noevera25c8b22014-06-03 22:04:02 +02001458 struct tb_switch *sw;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001459 int upstream_port;
Mika Westerberg444ac382018-12-30 12:17:52 +02001460 int i, ret, depth;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001461
1462 /* Make sure we do not exceed maximum topology limit */
1463 depth = tb_route_length(route);
1464 if (depth > TB_SWITCH_MAX_DEPTH)
Mika Westerberg444ac382018-12-30 12:17:52 +02001465 return ERR_PTR(-EADDRNOTAVAIL);
Mika Westerbergf0342e72018-12-30 12:14:46 +02001466
1467 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001468 if (upstream_port < 0)
Mika Westerberg444ac382018-12-30 12:17:52 +02001469 return ERR_PTR(upstream_port);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001470
1471 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1472 if (!sw)
Mika Westerberg444ac382018-12-30 12:17:52 +02001473 return ERR_PTR(-ENOMEM);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001474
1475 sw->tb = tb;
Mika Westerberg444ac382018-12-30 12:17:52 +02001476 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
1477 if (ret)
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001478 goto err_free_sw_ports;
1479
Mika Westerbergdaa51402018-10-01 12:31:19 +03001480 tb_dbg(tb, "current switch config:\n");
Andreas Noevera25c8b22014-06-03 22:04:02 +02001481 tb_dump_switch(tb, &sw->config);
1482
1483 /* configure switch */
1484 sw->config.upstream_port_number = upstream_port;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001485 sw->config.depth = depth;
1486 sw->config.route_hi = upper_32_bits(route);
1487 sw->config.route_lo = lower_32_bits(route);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001488 sw->config.enabled = 0;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001489
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001490 /* initialize ports */
1491 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
1492 GFP_KERNEL);
Mika Westerberg444ac382018-12-30 12:17:52 +02001493 if (!sw->ports) {
1494 ret = -ENOMEM;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001495 goto err_free_sw_ports;
Mika Westerberg444ac382018-12-30 12:17:52 +02001496 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001497
1498 for (i = 0; i <= sw->config.max_port_number; i++) {
1499 /* minimum setup for tb_find_cap and tb_drom_read to work */
1500 sw->ports[i].sw = sw;
1501 sw->ports[i].port = i;
1502 }
1503
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001504 sw->generation = tb_switch_get_generation(sw);
1505
Mika Westerberg444ac382018-12-30 12:17:52 +02001506 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
1507 if (ret < 0) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001508 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1509 goto err_free_sw_ports;
1510 }
Mika Westerberg444ac382018-12-30 12:17:52 +02001511 sw->cap_plug_events = ret;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001512
Mika Westerberg444ac382018-12-30 12:17:52 +02001513 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
1514 if (ret > 0)
1515 sw->cap_lc = ret;
Mika Westerberga9be5582019-01-09 16:42:12 +02001516
Mika Westerbergf67cf492017-06-06 15:25:16 +03001517 /* Root switch is always authorized */
1518 if (!route)
1519 sw->authorized = true;
1520
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001521 device_initialize(&sw->dev);
1522 sw->dev.parent = parent;
1523 sw->dev.bus = &tb_bus_type;
1524 sw->dev.type = &tb_switch_type;
1525 sw->dev.groups = switch_groups;
1526 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1527
1528 return sw;
1529
1530err_free_sw_ports:
1531 kfree(sw->ports);
1532 kfree(sw);
1533
Mika Westerberg444ac382018-12-30 12:17:52 +02001534 return ERR_PTR(ret);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001535}
1536
1537/**
Mika Westerberge6b245c2017-06-06 15:25:17 +03001538 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1539 * @tb: Pointer to the owning domain
1540 * @parent: Parent device for this switch
1541 * @route: Route string for this switch
1542 *
1543 * This creates a switch in safe mode. This means the switch pretty much
1544 * lacks all capabilities except DMA configuration port before it is
1545 * flashed with a valid NVM firmware.
1546 *
1547 * The returned switch must be released by calling tb_switch_put().
1548 *
Mika Westerberg444ac382018-12-30 12:17:52 +02001549 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
Mika Westerberge6b245c2017-06-06 15:25:17 +03001550 */
1551struct tb_switch *
1552tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
1553{
1554 struct tb_switch *sw;
1555
1556 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1557 if (!sw)
Mika Westerberg444ac382018-12-30 12:17:52 +02001558 return ERR_PTR(-ENOMEM);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001559
1560 sw->tb = tb;
1561 sw->config.depth = tb_route_length(route);
1562 sw->config.route_hi = upper_32_bits(route);
1563 sw->config.route_lo = lower_32_bits(route);
1564 sw->safe_mode = true;
1565
1566 device_initialize(&sw->dev);
1567 sw->dev.parent = parent;
1568 sw->dev.bus = &tb_bus_type;
1569 sw->dev.type = &tb_switch_type;
1570 sw->dev.groups = switch_groups;
1571 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1572
1573 return sw;
1574}
1575
1576/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001577 * tb_switch_configure() - Uploads configuration to the switch
1578 * @sw: Switch to configure
1579 *
1580 * Call this function before the switch is added to the system. It will
1581 * upload configuration to the switch and makes it available for the
1582 * connection manager to use.
1583 *
1584 * Return: %0 in case of success and negative errno in case of failure
1585 */
1586int tb_switch_configure(struct tb_switch *sw)
1587{
1588 struct tb *tb = sw->tb;
1589 u64 route;
1590 int ret;
1591
1592 route = tb_route(sw);
Mika Westerbergdaa51402018-10-01 12:31:19 +03001593 tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1594 route, tb_route_length(route), sw->config.upstream_port_number);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001595
1596 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001597 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
1598 sw->config.vendor_id);
1599
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001600 sw->config.enabled = 1;
1601
Andreas Noevera25c8b22014-06-03 22:04:02 +02001602 /* upload configuration */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001603 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
1604 if (ret)
1605 return ret;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001606
Mika Westerberge879a702018-10-11 12:33:08 +03001607 ret = tb_lc_configure_link(sw);
1608 if (ret)
1609 return ret;
1610
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001611 return tb_plug_events_active(sw, true);
1612}
Andreas Noevera25c8b22014-06-03 22:04:02 +02001613
Aditya Pakki2cc12752019-03-20 10:57:54 -05001614static int tb_switch_set_uuid(struct tb_switch *sw)
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001615{
1616 u32 uuid[4];
Mika Westerberga9be5582019-01-09 16:42:12 +02001617 int ret;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001618
1619 if (sw->uuid)
Mika Westerberga9be5582019-01-09 16:42:12 +02001620 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001621
1622 /*
1623 * The newer controllers include fused UUID as part of link
1624 * controller specific registers
1625 */
Mika Westerberga9be5582019-01-09 16:42:12 +02001626 ret = tb_lc_read_uuid(sw, uuid);
1627 if (ret) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001628 /*
1629 * ICM generates UUID based on UID and fills the upper
1630 * two words with ones. This is not strictly following
1631 * UUID format but we want to be compatible with it so
1632 * we do the same here.
1633 */
1634 uuid[0] = sw->uid & 0xffffffff;
1635 uuid[1] = (sw->uid >> 32) & 0xffffffff;
1636 uuid[2] = 0xffffffff;
1637 uuid[3] = 0xffffffff;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001638 }
1639
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001640 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
Aditya Pakki2cc12752019-03-20 10:57:54 -05001641 if (!sw->uuid)
Mika Westerberga9be5582019-01-09 16:42:12 +02001642 return -ENOMEM;
1643 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001644}
1645
Mika Westerberge6b245c2017-06-06 15:25:17 +03001646static int tb_switch_add_dma_port(struct tb_switch *sw)
Mika Westerberg3e136762017-06-06 15:25:14 +03001647{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001648 u32 status;
1649 int ret;
1650
Mika Westerberg3e136762017-06-06 15:25:14 +03001651 switch (sw->generation) {
1652 case 3:
1653 break;
1654
1655 case 2:
1656 /* Only root switch can be upgraded */
1657 if (tb_route(sw))
Mika Westerberge6b245c2017-06-06 15:25:17 +03001658 return 0;
Mika Westerberg3e136762017-06-06 15:25:14 +03001659 break;
1660
1661 default:
Mika Westerberge6b245c2017-06-06 15:25:17 +03001662 /*
1663 * DMA port is the only thing available when the switch
1664 * is in safe mode.
1665 */
1666 if (!sw->safe_mode)
1667 return 0;
1668 break;
Mika Westerberg3e136762017-06-06 15:25:14 +03001669 }
1670
Mika Westerberge6b245c2017-06-06 15:25:17 +03001671 if (sw->no_nvm_upgrade)
1672 return 0;
1673
Mika Westerberg3e136762017-06-06 15:25:14 +03001674 sw->dma_port = dma_port_alloc(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001675 if (!sw->dma_port)
1676 return 0;
1677
1678 /*
1679 * Check status of the previous flash authentication. If there
1680 * is one we need to power cycle the switch in any case to make
1681 * it functional again.
1682 */
1683 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
1684 if (ret <= 0)
1685 return ret;
1686
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001687 /* Now we can allow root port to suspend again */
1688 if (!tb_route(sw))
1689 nvm_authenticate_complete(sw);
1690
Mika Westerberge6b245c2017-06-06 15:25:17 +03001691 if (status) {
1692 tb_sw_info(sw, "switch flash authentication failed\n");
Aditya Pakki2cc12752019-03-20 10:57:54 -05001693 ret = tb_switch_set_uuid(sw);
1694 if (ret)
1695 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001696 nvm_set_auth_status(sw, status);
1697 }
1698
1699 tb_sw_info(sw, "power cycling the switch now\n");
1700 dma_port_power_cycle(sw->dma_port);
1701
1702 /*
1703 * We return error here which causes the switch adding failure.
1704 * It should appear back after power cycle is complete.
1705 */
1706 return -ESHUTDOWN;
Mika Westerberg3e136762017-06-06 15:25:14 +03001707}
1708
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001709/**
1710 * tb_switch_add() - Add a switch to the domain
1711 * @sw: Switch to add
1712 *
1713 * This is the last step in adding switch to the domain. It will read
1714 * identification information from DROM and initializes ports so that
1715 * they can be used to connect other switches. The switch will be
1716 * exposed to the userspace when this function successfully returns. To
1717 * remove and release the switch, call tb_switch_remove().
1718 *
1719 * Return: %0 in case of success and negative errno in case of failure
1720 */
1721int tb_switch_add(struct tb_switch *sw)
1722{
1723 int i, ret;
Andreas Noeverca389f72014-06-03 22:04:04 +02001724
Mika Westerberg3e136762017-06-06 15:25:14 +03001725 /*
1726 * Initialize DMA control port now before we read DROM. Recent
1727 * host controllers have more complete DROM on NVM that includes
1728 * vendor and model identification strings which we then expose
1729 * to the userspace. NVM can be accessed through DMA
1730 * configuration based mailbox.
1731 */
Mika Westerberge6b245c2017-06-06 15:25:17 +03001732 ret = tb_switch_add_dma_port(sw);
1733 if (ret)
Mika Westerbergf53e7672017-06-06 15:25:02 +03001734 return ret;
Andreas Noever343fcb82014-06-12 23:11:47 +02001735
Mika Westerberge6b245c2017-06-06 15:25:17 +03001736 if (!sw->safe_mode) {
1737 /* read drom */
1738 ret = tb_drom_read(sw);
1739 if (ret) {
1740 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001741 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001742 }
Mika Westerbergdaa51402018-10-01 12:31:19 +03001743 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001744
Aditya Pakki2cc12752019-03-20 10:57:54 -05001745 ret = tb_switch_set_uuid(sw);
1746 if (ret)
1747 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001748
1749 for (i = 0; i <= sw->config.max_port_number; i++) {
1750 if (sw->ports[i].disabled) {
Mika Westerbergdaa51402018-10-01 12:31:19 +03001751 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
Mika Westerberge6b245c2017-06-06 15:25:17 +03001752 continue;
1753 }
1754 ret = tb_init_port(&sw->ports[i]);
1755 if (ret)
1756 return ret;
1757 }
Andreas Noever343fcb82014-06-12 23:11:47 +02001758 }
1759
Mika Westerberge6b245c2017-06-06 15:25:17 +03001760 ret = device_add(&sw->dev);
1761 if (ret)
1762 return ret;
1763
Mika Westerberga83bc4a2018-10-01 12:31:20 +03001764 if (tb_route(sw)) {
1765 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
1766 sw->vendor, sw->device);
1767 if (sw->vendor_name && sw->device_name)
1768 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
1769 sw->device_name);
1770 }
1771
Mika Westerberge6b245c2017-06-06 15:25:17 +03001772 ret = tb_switch_nvm_add(sw);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001773 if (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001774 device_del(&sw->dev);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001775 return ret;
1776 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001777
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001778 pm_runtime_set_active(&sw->dev);
1779 if (sw->rpm) {
1780 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
1781 pm_runtime_use_autosuspend(&sw->dev);
1782 pm_runtime_mark_last_busy(&sw->dev);
1783 pm_runtime_enable(&sw->dev);
1784 pm_request_autosuspend(&sw->dev);
1785 }
1786
1787 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001788}
Andreas Noeverc90553b2014-06-03 22:04:11 +02001789
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001790/**
1791 * tb_switch_remove() - Remove and release a switch
1792 * @sw: Switch to remove
1793 *
1794 * This will remove the switch from the domain and release it after last
1795 * reference count drops to zero. If there are switches connected below
1796 * this switch, they will be removed as well.
1797 */
1798void tb_switch_remove(struct tb_switch *sw)
1799{
1800 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +02001801
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001802 if (sw->rpm) {
1803 pm_runtime_get_sync(&sw->dev);
1804 pm_runtime_disable(&sw->dev);
1805 }
1806
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001807 /* port 0 is the switch itself and never has a remote */
1808 for (i = 1; i <= sw->config.max_port_number; i++) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001809 if (tb_port_has_remote(&sw->ports[i])) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001810 tb_switch_remove(sw->ports[i].remote->sw);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001811 sw->ports[i].remote = NULL;
1812 } else if (sw->ports[i].xdomain) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001813 tb_xdomain_remove(sw->ports[i].xdomain);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001814 sw->ports[i].xdomain = NULL;
1815 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001816 }
1817
1818 if (!sw->is_unplugged)
1819 tb_plug_events_active(sw, false);
Mika Westerberge879a702018-10-11 12:33:08 +03001820 tb_lc_unconfigure_link(sw);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001821
Mika Westerberge6b245c2017-06-06 15:25:17 +03001822 tb_switch_nvm_remove(sw);
Mika Westerberga83bc4a2018-10-01 12:31:20 +03001823
1824 if (tb_route(sw))
1825 dev_info(&sw->dev, "device disconnected\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001826 device_unregister(&sw->dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001827}
1828
Andreas Noever053596d2014-06-03 22:04:06 +02001829/**
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001830 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
Andreas Noever053596d2014-06-03 22:04:06 +02001831 */
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001832void tb_sw_set_unplugged(struct tb_switch *sw)
Andreas Noever053596d2014-06-03 22:04:06 +02001833{
1834 int i;
1835 if (sw == sw->tb->root_switch) {
1836 tb_sw_WARN(sw, "cannot unplug root switch\n");
1837 return;
1838 }
1839 if (sw->is_unplugged) {
1840 tb_sw_WARN(sw, "is_unplugged already set\n");
1841 return;
1842 }
1843 sw->is_unplugged = true;
1844 for (i = 0; i <= sw->config.max_port_number; i++) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001845 if (tb_port_has_remote(&sw->ports[i]))
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001846 tb_sw_set_unplugged(sw->ports[i].remote->sw);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001847 else if (sw->ports[i].xdomain)
1848 sw->ports[i].xdomain->is_unplugged = true;
Andreas Noever053596d2014-06-03 22:04:06 +02001849 }
1850}
1851
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001852int tb_switch_resume(struct tb_switch *sw)
1853{
1854 int i, err;
Mika Westerbergdaa51402018-10-01 12:31:19 +03001855 tb_sw_dbg(sw, "resuming switch\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001856
Mika Westerberg08a5e4c2017-06-06 15:24:54 +03001857 /*
1858 * Check for UID of the connected switches except for root
1859 * switch which we assume cannot be removed.
1860 */
1861 if (tb_route(sw)) {
1862 u64 uid;
1863
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001864 /*
1865 * Check first that we can still read the switch config
1866 * space. It may be that there is now another domain
1867 * connected.
1868 */
1869 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
1870 if (err < 0) {
1871 tb_sw_info(sw, "switch not present anymore\n");
1872 return err;
1873 }
1874
Mika Westerberg08a5e4c2017-06-06 15:24:54 +03001875 err = tb_drom_read_uid_only(sw, &uid);
1876 if (err) {
1877 tb_sw_warn(sw, "uid read failed\n");
1878 return err;
1879 }
1880 if (sw->uid != uid) {
1881 tb_sw_info(sw,
1882 "changed while suspended (uid %#llx -> %#llx)\n",
1883 sw->uid, uid);
1884 return -ENODEV;
1885 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001886 }
1887
1888 /* upload configuration */
1889 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
1890 if (err)
1891 return err;
1892
Mika Westerberge879a702018-10-11 12:33:08 +03001893 err = tb_lc_configure_link(sw);
1894 if (err)
1895 return err;
1896
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001897 err = tb_plug_events_active(sw, true);
1898 if (err)
1899 return err;
1900
1901 /* check for surviving downstream switches */
1902 for (i = 1; i <= sw->config.max_port_number; i++) {
1903 struct tb_port *port = &sw->ports[i];
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001904
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001905 if (!tb_port_has_remote(port) && !port->xdomain)
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001906 continue;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001907
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001908 if (tb_wait_for_port(port, true) <= 0) {
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001909 tb_port_warn(port,
1910 "lost during suspend, disconnecting\n");
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001911 if (tb_port_has_remote(port))
1912 tb_sw_set_unplugged(port->remote->sw);
1913 else if (port->xdomain)
1914 port->xdomain->is_unplugged = true;
1915 } else if (tb_port_has_remote(port)) {
1916 if (tb_switch_resume(port->remote->sw)) {
1917 tb_port_warn(port,
1918 "lost during suspend, disconnecting\n");
1919 tb_sw_set_unplugged(port->remote->sw);
1920 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001921 }
1922 }
1923 return 0;
1924}
1925
1926void tb_switch_suspend(struct tb_switch *sw)
1927{
1928 int i, err;
1929 err = tb_plug_events_active(sw, false);
1930 if (err)
1931 return;
1932
1933 for (i = 1; i <= sw->config.max_port_number; i++) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001934 if (tb_port_has_remote(&sw->ports[i]))
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001935 tb_switch_suspend(sw->ports[i].remote->sw);
1936 }
Mika Westerberg5480dfc2019-01-09 17:25:43 +02001937
1938 tb_lc_set_sleep(sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001939}
Mika Westerbergf67cf492017-06-06 15:25:16 +03001940
1941struct tb_sw_lookup {
1942 struct tb *tb;
1943 u8 link;
1944 u8 depth;
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02001945 const uuid_t *uuid;
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03001946 u64 route;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001947};
1948
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +01001949static int tb_switch_match(struct device *dev, const void *data)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001950{
1951 struct tb_switch *sw = tb_to_switch(dev);
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +01001952 const struct tb_sw_lookup *lookup = data;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001953
1954 if (!sw)
1955 return 0;
1956 if (sw->tb != lookup->tb)
1957 return 0;
1958
1959 if (lookup->uuid)
1960 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
1961
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03001962 if (lookup->route) {
1963 return sw->config.route_lo == lower_32_bits(lookup->route) &&
1964 sw->config.route_hi == upper_32_bits(lookup->route);
1965 }
1966
Mika Westerbergf67cf492017-06-06 15:25:16 +03001967 /* Root switch is matched only by depth */
1968 if (!lookup->depth)
1969 return !sw->depth;
1970
1971 return sw->link == lookup->link && sw->depth == lookup->depth;
1972}
1973
1974/**
1975 * tb_switch_find_by_link_depth() - Find switch by link and depth
1976 * @tb: Domain the switch belongs
1977 * @link: Link number the switch is connected
1978 * @depth: Depth of the switch in link
1979 *
1980 * Returned switch has reference count increased so the caller needs to
1981 * call tb_switch_put() when done with the switch.
1982 */
1983struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
1984{
1985 struct tb_sw_lookup lookup;
1986 struct device *dev;
1987
1988 memset(&lookup, 0, sizeof(lookup));
1989 lookup.tb = tb;
1990 lookup.link = link;
1991 lookup.depth = depth;
1992
1993 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1994 if (dev)
1995 return tb_to_switch(dev);
1996
1997 return NULL;
1998}
1999
2000/**
Radion Mirchevsky432019d2017-10-04 15:07:40 +03002001 * tb_switch_find_by_uuid() - Find switch by UUID
Mika Westerbergf67cf492017-06-06 15:25:16 +03002002 * @tb: Domain the switch belongs
2003 * @uuid: UUID to look for
2004 *
2005 * Returned switch has reference count increased so the caller needs to
2006 * call tb_switch_put() when done with the switch.
2007 */
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02002008struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
Mika Westerbergf67cf492017-06-06 15:25:16 +03002009{
2010 struct tb_sw_lookup lookup;
2011 struct device *dev;
2012
2013 memset(&lookup, 0, sizeof(lookup));
2014 lookup.tb = tb;
2015 lookup.uuid = uuid;
2016
2017 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2018 if (dev)
2019 return tb_to_switch(dev);
2020
2021 return NULL;
2022}
Mika Westerberge6b245c2017-06-06 15:25:17 +03002023
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03002024/**
2025 * tb_switch_find_by_route() - Find switch by route string
2026 * @tb: Domain the switch belongs
2027 * @route: Route string to look for
2028 *
2029 * Returned switch has reference count increased so the caller needs to
2030 * call tb_switch_put() when done with the switch.
2031 */
2032struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
2033{
2034 struct tb_sw_lookup lookup;
2035 struct device *dev;
2036
2037 if (!route)
2038 return tb_switch_get(tb->root_switch);
2039
2040 memset(&lookup, 0, sizeof(lookup));
2041 lookup.tb = tb;
2042 lookup.route = route;
2043
2044 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2045 if (dev)
2046 return tb_to_switch(dev);
2047
2048 return NULL;
2049}
2050
Mika Westerberge6b245c2017-06-06 15:25:17 +03002051void tb_switch_exit(void)
2052{
2053 ida_destroy(&nvm_ida);
2054}