blob: bd3eaaf34ea11ce71772de76121c854a55691b07 [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);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +0300242
243 if (!mutex_trylock(&sw->tb->lock)) {
244 ret = restart_syscall();
245 goto out;
246 }
247
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300248 ret = dma_port_flash_read(sw->dma_port, offset, val, bytes);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +0300249 mutex_unlock(&sw->tb->lock);
250
251out:
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300252 pm_runtime_mark_last_busy(&sw->dev);
253 pm_runtime_put_autosuspend(&sw->dev);
254
255 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300256}
257
258static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
259 size_t bytes)
260{
261 struct tb_switch *sw = priv;
262 int ret = 0;
263
Mika Westerberg09f11b62019-03-19 16:48:41 +0200264 if (!mutex_trylock(&sw->tb->lock))
265 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +0300266
267 /*
268 * Since writing the NVM image might require some special steps,
269 * for example when CSS headers are written, we cache the image
270 * locally here and handle the special cases when the user asks
271 * us to authenticate the image.
272 */
273 if (!sw->nvm->buf) {
274 sw->nvm->buf = vmalloc(NVM_MAX_SIZE);
275 if (!sw->nvm->buf) {
276 ret = -ENOMEM;
277 goto unlock;
278 }
279 }
280
281 sw->nvm->buf_data_size = offset + bytes;
282 memcpy(sw->nvm->buf + offset, val, bytes);
283
284unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +0200285 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300286
287 return ret;
288}
289
290static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id,
291 size_t size, bool active)
292{
293 struct nvmem_config config;
294
295 memset(&config, 0, sizeof(config));
296
297 if (active) {
298 config.name = "nvm_active";
299 config.reg_read = tb_switch_nvm_read;
Mika Westerberg800161b2017-06-29 14:19:50 +0300300 config.read_only = true;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300301 } else {
302 config.name = "nvm_non_active";
303 config.reg_write = tb_switch_nvm_write;
Mika Westerberg800161b2017-06-29 14:19:50 +0300304 config.root_only = true;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300305 }
306
307 config.id = id;
308 config.stride = 4;
309 config.word_size = 4;
310 config.size = size;
311 config.dev = &sw->dev;
312 config.owner = THIS_MODULE;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300313 config.priv = sw;
314
315 return nvmem_register(&config);
316}
317
318static int tb_switch_nvm_add(struct tb_switch *sw)
319{
320 struct nvmem_device *nvm_dev;
321 struct tb_switch_nvm *nvm;
322 u32 val;
323 int ret;
324
325 if (!sw->dma_port)
326 return 0;
327
328 nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
329 if (!nvm)
330 return -ENOMEM;
331
332 nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL);
333
334 /*
335 * If the switch is in safe-mode the only accessible portion of
336 * the NVM is the non-active one where userspace is expected to
337 * write new functional NVM.
338 */
339 if (!sw->safe_mode) {
340 u32 nvm_size, hdr_size;
341
342 ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val,
343 sizeof(val));
344 if (ret)
345 goto err_ida;
346
347 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
348 nvm_size = (SZ_1M << (val & 7)) / 8;
349 nvm_size = (nvm_size - hdr_size) / 2;
350
351 ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val,
352 sizeof(val));
353 if (ret)
354 goto err_ida;
355
356 nvm->major = val >> 16;
357 nvm->minor = val >> 8;
358
359 nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true);
360 if (IS_ERR(nvm_dev)) {
361 ret = PTR_ERR(nvm_dev);
362 goto err_ida;
363 }
364 nvm->active = nvm_dev;
365 }
366
367 nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false);
368 if (IS_ERR(nvm_dev)) {
369 ret = PTR_ERR(nvm_dev);
370 goto err_nvm_active;
371 }
372 nvm->non_active = nvm_dev;
373
Mika Westerberge6b245c2017-06-06 15:25:17 +0300374 sw->nvm = nvm;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300375 return 0;
376
377err_nvm_active:
378 if (nvm->active)
379 nvmem_unregister(nvm->active);
380err_ida:
381 ida_simple_remove(&nvm_ida, nvm->id);
382 kfree(nvm);
383
384 return ret;
385}
386
387static void tb_switch_nvm_remove(struct tb_switch *sw)
388{
389 struct tb_switch_nvm *nvm;
390
Mika Westerberge6b245c2017-06-06 15:25:17 +0300391 nvm = sw->nvm;
392 sw->nvm = NULL;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300393
394 if (!nvm)
395 return;
396
397 /* Remove authentication status in case the switch is unplugged */
398 if (!nvm->authenticating)
399 nvm_clear_auth_status(sw);
400
401 nvmem_unregister(nvm->non_active);
402 if (nvm->active)
403 nvmem_unregister(nvm->active);
404 ida_simple_remove(&nvm_ida, nvm->id);
405 vfree(nvm->buf);
406 kfree(nvm);
407}
408
Andreas Noevera25c8b22014-06-03 22:04:02 +0200409/* port utility functions */
410
411static const char *tb_port_type(struct tb_regs_port_header *port)
412{
413 switch (port->type >> 16) {
414 case 0:
415 switch ((u8) port->type) {
416 case 0:
417 return "Inactive";
418 case 1:
419 return "Port";
420 case 2:
421 return "NHI";
422 default:
423 return "unknown";
424 }
425 case 0x2:
426 return "Ethernet";
427 case 0x8:
428 return "SATA";
429 case 0xe:
430 return "DP/HDMI";
431 case 0x10:
432 return "PCIe";
433 case 0x20:
434 return "USB";
435 default:
436 return "unknown";
437 }
438}
439
440static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
441{
Mika Westerbergdaa51402018-10-01 12:31:19 +0300442 tb_dbg(tb,
443 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
444 port->port_number, port->vendor_id, port->device_id,
445 port->revision, port->thunderbolt_version, tb_port_type(port),
446 port->type);
447 tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
448 port->max_in_hop_id, port->max_out_hop_id);
449 tb_dbg(tb, " Max counters: %d\n", port->max_counters);
450 tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200451}
452
453/**
Andreas Noever9da672a2014-06-03 22:04:05 +0200454 * tb_port_state() - get connectedness state of a port
455 *
456 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
457 *
458 * Return: Returns an enum tb_port_state on success or an error code on failure.
459 */
460static int tb_port_state(struct tb_port *port)
461{
462 struct tb_cap_phy phy;
463 int res;
464 if (port->cap_phy == 0) {
465 tb_port_WARN(port, "does not have a PHY\n");
466 return -EINVAL;
467 }
468 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
469 if (res)
470 return res;
471 return phy.state;
472}
473
474/**
475 * tb_wait_for_port() - wait for a port to become ready
476 *
477 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
478 * wait_if_unplugged is set then we also wait if the port is in state
479 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
480 * switch resume). Otherwise we only wait if a device is registered but the link
481 * has not yet been established.
482 *
483 * Return: Returns an error code on failure. Returns 0 if the port is not
484 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
485 * if the port is connected and in state TB_PORT_UP.
486 */
487int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
488{
489 int retries = 10;
490 int state;
491 if (!port->cap_phy) {
492 tb_port_WARN(port, "does not have PHY\n");
493 return -EINVAL;
494 }
495 if (tb_is_upstream_port(port)) {
496 tb_port_WARN(port, "is the upstream port\n");
497 return -EINVAL;
498 }
499
500 while (retries--) {
501 state = tb_port_state(port);
502 if (state < 0)
503 return state;
504 if (state == TB_PORT_DISABLED) {
Mika Westerberg62efe692018-09-17 16:32:13 +0300505 tb_port_dbg(port, "is disabled (state: 0)\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200506 return 0;
507 }
508 if (state == TB_PORT_UNPLUGGED) {
509 if (wait_if_unplugged) {
510 /* used during resume */
Mika Westerberg62efe692018-09-17 16:32:13 +0300511 tb_port_dbg(port,
512 "is unplugged (state: 7), retrying...\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200513 msleep(100);
514 continue;
515 }
Mika Westerberg62efe692018-09-17 16:32:13 +0300516 tb_port_dbg(port, "is unplugged (state: 7)\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200517 return 0;
518 }
519 if (state == TB_PORT_UP) {
Mika Westerberg62efe692018-09-17 16:32:13 +0300520 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200521 return 1;
522 }
523
524 /*
525 * After plug-in the state is TB_PORT_CONNECTING. Give it some
526 * time.
527 */
Mika Westerberg62efe692018-09-17 16:32:13 +0300528 tb_port_dbg(port,
529 "is connected, link is not up (state: %d), retrying...\n",
530 state);
Andreas Noever9da672a2014-06-03 22:04:05 +0200531 msleep(100);
532 }
533 tb_port_warn(port,
534 "failed to reach state TB_PORT_UP. Ignoring port...\n");
535 return 0;
536}
537
538/**
Andreas Noever520b6702014-06-03 22:04:07 +0200539 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
540 *
541 * Change the number of NFC credits allocated to @port by @credits. To remove
542 * NFC credits pass a negative amount of credits.
543 *
544 * Return: Returns 0 on success or an error code on failure.
545 */
546int tb_port_add_nfc_credits(struct tb_port *port, int credits)
547{
Mika Westerbergc5ee6fe2018-10-11 11:38:22 +0300548 u32 nfc_credits;
549
550 if (credits == 0 || port->sw->is_unplugged)
Andreas Noever520b6702014-06-03 22:04:07 +0200551 return 0;
Mika Westerbergc5ee6fe2018-10-11 11:38:22 +0300552
553 nfc_credits = port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK;
554 nfc_credits += credits;
555
556 tb_port_dbg(port, "adding %d NFC credits to %lu",
557 credits, port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK);
558
559 port->config.nfc_credits &= ~TB_PORT_NFC_CREDITS_MASK;
560 port->config.nfc_credits |= nfc_credits;
561
Andreas Noever520b6702014-06-03 22:04:07 +0200562 return tb_port_write(port, &port->config.nfc_credits,
563 TB_CFG_PORT, 4, 1);
564}
565
566/**
Mika Westerberg44242d62018-09-28 16:35:32 +0300567 * tb_port_set_initial_credits() - Set initial port link credits allocated
568 * @port: Port to set the initial credits
569 * @credits: Number of credits to to allocate
570 *
571 * Set initial credits value to be used for ingress shared buffering.
572 */
573int tb_port_set_initial_credits(struct tb_port *port, u32 credits)
574{
575 u32 data;
576 int ret;
577
578 ret = tb_port_read(port, &data, TB_CFG_PORT, 5, 1);
579 if (ret)
580 return ret;
581
582 data &= ~TB_PORT_LCA_MASK;
583 data |= (credits << TB_PORT_LCA_SHIFT) & TB_PORT_LCA_MASK;
584
585 return tb_port_write(port, &data, TB_CFG_PORT, 5, 1);
586}
587
588/**
Andreas Noever520b6702014-06-03 22:04:07 +0200589 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
590 *
591 * Return: Returns 0 on success or an error code on failure.
592 */
593int tb_port_clear_counter(struct tb_port *port, int counter)
594{
595 u32 zero[3] = { 0, 0, 0 };
Mika Westerberg62efe692018-09-17 16:32:13 +0300596 tb_port_dbg(port, "clearing counter %d\n", counter);
Andreas Noever520b6702014-06-03 22:04:07 +0200597 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
598}
599
600/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200601 * tb_init_port() - initialize a port
602 *
603 * This is a helper method for tb_switch_alloc. Does not check or initialize
604 * any downstream switches.
605 *
606 * Return: Returns 0 on success or an error code on failure.
607 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200608static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200609{
610 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200611 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200612
Andreas Noevera25c8b22014-06-03 22:04:02 +0200613 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
Mika Westerbergd94dcbb2018-07-04 08:50:01 +0300614 if (res) {
615 if (res == -ENODEV) {
616 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
617 port->port);
618 return 0;
619 }
Andreas Noevera25c8b22014-06-03 22:04:02 +0200620 return res;
Mika Westerbergd94dcbb2018-07-04 08:50:01 +0300621 }
Andreas Noevera25c8b22014-06-03 22:04:02 +0200622
Andreas Noever9da672a2014-06-03 22:04:05 +0200623 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200624 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Mika Westerbergda2da042017-06-06 15:24:58 +0300625 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
Andreas Noever9da672a2014-06-03 22:04:05 +0200626
627 if (cap > 0)
628 port->cap_phy = cap;
629 else
630 tb_port_WARN(port, "non switch port without a PHY\n");
Mika Westerberg56183c82017-02-19 10:39:34 +0200631 } else if (port->port != 0) {
632 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
633 if (cap > 0)
634 port->cap_adap = cap;
Andreas Noever9da672a2014-06-03 22:04:05 +0200635 }
636
Andreas Noever343fcb82014-06-12 23:11:47 +0200637 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200638
Mika Westerberg0b2863a2017-02-19 16:57:27 +0200639 /* Control port does not need HopID allocation */
640 if (port->port) {
641 ida_init(&port->in_hopids);
642 ida_init(&port->out_hopids);
643 }
644
Andreas Noevera25c8b22014-06-03 22:04:02 +0200645 return 0;
646
647}
648
Mika Westerberg0b2863a2017-02-19 16:57:27 +0200649static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
650 int max_hopid)
651{
652 int port_max_hopid;
653 struct ida *ida;
654
655 if (in) {
656 port_max_hopid = port->config.max_in_hop_id;
657 ida = &port->in_hopids;
658 } else {
659 port_max_hopid = port->config.max_out_hop_id;
660 ida = &port->out_hopids;
661 }
662
663 /* HopIDs 0-7 are reserved */
664 if (min_hopid < TB_PATH_MIN_HOPID)
665 min_hopid = TB_PATH_MIN_HOPID;
666
667 if (max_hopid < 0 || max_hopid > port_max_hopid)
668 max_hopid = port_max_hopid;
669
670 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
671}
672
673/**
674 * tb_port_alloc_in_hopid() - Allocate input HopID from port
675 * @port: Port to allocate HopID for
676 * @min_hopid: Minimum acceptable input HopID
677 * @max_hopid: Maximum acceptable input HopID
678 *
679 * Return: HopID between @min_hopid and @max_hopid or negative errno in
680 * case of error.
681 */
682int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
683{
684 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
685}
686
687/**
688 * tb_port_alloc_out_hopid() - Allocate output HopID from port
689 * @port: Port to allocate HopID for
690 * @min_hopid: Minimum acceptable output HopID
691 * @max_hopid: Maximum acceptable output HopID
692 *
693 * Return: HopID between @min_hopid and @max_hopid or negative errno in
694 * case of error.
695 */
696int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
697{
698 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
699}
700
701/**
702 * tb_port_release_in_hopid() - Release allocated input HopID from port
703 * @port: Port whose HopID to release
704 * @hopid: HopID to release
705 */
706void tb_port_release_in_hopid(struct tb_port *port, int hopid)
707{
708 ida_simple_remove(&port->in_hopids, hopid);
709}
710
711/**
712 * tb_port_release_out_hopid() - Release allocated output HopID from port
713 * @port: Port whose HopID to release
714 * @hopid: HopID to release
715 */
716void tb_port_release_out_hopid(struct tb_port *port, int hopid)
717{
718 ida_simple_remove(&port->out_hopids, hopid);
719}
720
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200721/**
Mika Westerbergfb19fac2017-02-19 21:51:30 +0200722 * tb_next_port_on_path() - Return next port for given port on a path
723 * @start: Start port of the walk
724 * @end: End port of the walk
725 * @prev: Previous port (%NULL if this is the first)
726 *
727 * This function can be used to walk from one port to another if they
728 * are connected through zero or more switches. If the @prev is dual
729 * link port, the function follows that link and returns another end on
730 * that same link.
731 *
732 * If the @end port has been reached, return %NULL.
733 *
734 * Domain tb->lock must be held when this function is called.
735 */
736struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
737 struct tb_port *prev)
738{
739 struct tb_port *next;
740
741 if (!prev)
742 return start;
743
744 if (prev->sw == end->sw) {
745 if (prev == end)
746 return NULL;
747 return end;
748 }
749
750 if (start->sw->config.depth < end->sw->config.depth) {
751 if (prev->remote &&
752 prev->remote->sw->config.depth > prev->sw->config.depth)
753 next = prev->remote;
754 else
755 next = tb_port_at(tb_route(end->sw), prev->sw);
756 } else {
757 if (tb_is_upstream_port(prev)) {
758 next = prev->remote;
759 } else {
760 next = tb_upstream_port(prev->sw);
761 /*
762 * Keep the same link if prev and next are both
763 * dual link ports.
764 */
765 if (next->dual_link_port &&
766 next->link_nr != prev->link_nr) {
767 next = next->dual_link_port;
768 }
769 }
770 }
771
772 return next;
773}
774
775/**
Mika Westerberge78db6f2017-10-12 16:45:50 +0300776 * tb_port_is_enabled() - Is the adapter port enabled
777 * @port: Port to check
778 */
779bool tb_port_is_enabled(struct tb_port *port)
780{
781 switch (port->config.type) {
782 case TB_TYPE_PCIE_UP:
783 case TB_TYPE_PCIE_DOWN:
784 return tb_pci_port_is_enabled(port);
785
Mika Westerberg4f807e42018-09-17 16:30:49 +0300786 case TB_TYPE_DP_HDMI_IN:
787 case TB_TYPE_DP_HDMI_OUT:
788 return tb_dp_port_is_enabled(port);
789
Mika Westerberge78db6f2017-10-12 16:45:50 +0300790 default:
791 return false;
792 }
793}
794
795/**
Mika Westerberg0414bec2017-02-19 23:43:26 +0200796 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
797 * @port: PCIe port to check
798 */
799bool tb_pci_port_is_enabled(struct tb_port *port)
800{
801 u32 data;
802
803 if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1))
804 return false;
805
806 return !!(data & TB_PCI_EN);
807}
808
809/**
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200810 * tb_pci_port_enable() - Enable PCIe adapter port
811 * @port: PCIe port to enable
812 * @enable: Enable/disable the PCIe adapter
813 */
814int tb_pci_port_enable(struct tb_port *port, bool enable)
815{
816 u32 word = enable ? TB_PCI_EN : 0x0;
817 if (!port->cap_adap)
818 return -ENXIO;
819 return tb_port_write(port, &word, TB_CFG_PORT, port->cap_adap, 1);
820}
821
Mika Westerberg4f807e42018-09-17 16:30:49 +0300822/**
823 * tb_dp_port_hpd_is_active() - Is HPD already active
824 * @port: DP out port to check
825 *
826 * Checks if the DP OUT adapter port has HDP bit already set.
827 */
828int tb_dp_port_hpd_is_active(struct tb_port *port)
829{
830 u32 data;
831 int ret;
832
833 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 2, 1);
834 if (ret)
835 return ret;
836
837 return !!(data & TB_DP_HDP);
838}
839
840/**
841 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
842 * @port: Port to clear HPD
843 *
844 * If the DP IN port has HDP set, this function can be used to clear it.
845 */
846int tb_dp_port_hpd_clear(struct tb_port *port)
847{
848 u32 data;
849 int ret;
850
851 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1);
852 if (ret)
853 return ret;
854
855 data |= TB_DP_HPDC;
856 return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1);
857}
858
859/**
860 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
861 * @port: DP IN/OUT port to set hops
862 * @video: Video Hop ID
863 * @aux_tx: AUX TX Hop ID
864 * @aux_rx: AUX RX Hop ID
865 *
866 * Programs specified Hop IDs for DP IN/OUT port.
867 */
868int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
869 unsigned int aux_tx, unsigned int aux_rx)
870{
871 u32 data[2];
872 int ret;
873
874 ret = tb_port_read(port, data, TB_CFG_PORT, port->cap_adap,
875 ARRAY_SIZE(data));
876 if (ret)
877 return ret;
878
879 data[0] &= ~TB_DP_VIDEO_HOPID_MASK;
880 data[1] &= ~(TB_DP_AUX_RX_HOPID_MASK | TB_DP_AUX_TX_HOPID_MASK);
881
882 data[0] |= (video << TB_DP_VIDEO_HOPID_SHIFT) & TB_DP_VIDEO_HOPID_MASK;
883 data[1] |= aux_tx & TB_DP_AUX_TX_HOPID_MASK;
884 data[1] |= (aux_rx << TB_DP_AUX_RX_HOPID_SHIFT) & TB_DP_AUX_RX_HOPID_MASK;
885
886 return tb_port_write(port, data, TB_CFG_PORT, port->cap_adap,
887 ARRAY_SIZE(data));
888}
889
890/**
891 * tb_dp_port_is_enabled() - Is DP adapter port enabled
892 * @port: DP adapter port to check
893 */
894bool tb_dp_port_is_enabled(struct tb_port *port)
895{
896 u32 data;
897
898 if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1))
899 return false;
900
901 return !!(data & (TB_DP_VIDEO_EN | TB_DP_AUX_EN));
902}
903
904/**
905 * tb_dp_port_enable() - Enables/disables DP paths of a port
906 * @port: DP IN/OUT port
907 * @enable: Enable/disable DP path
908 *
909 * Once Hop IDs are programmed DP paths can be enabled or disabled by
910 * calling this function.
911 */
912int tb_dp_port_enable(struct tb_port *port, bool enable)
913{
914 u32 data;
915 int ret;
916
917 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1);
918 if (ret)
919 return ret;
920
921 if (enable)
922 data |= TB_DP_VIDEO_EN | TB_DP_AUX_EN;
923 else
924 data &= ~(TB_DP_VIDEO_EN | TB_DP_AUX_EN);
925
926 return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap, 1);
927}
928
Andreas Noevera25c8b22014-06-03 22:04:02 +0200929/* switch utility functions */
930
931static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
932{
Mika Westerbergdaa51402018-10-01 12:31:19 +0300933 tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
934 sw->vendor_id, sw->device_id, sw->revision,
935 sw->thunderbolt_version);
936 tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number);
937 tb_dbg(tb, " Config:\n");
938 tb_dbg(tb,
Andreas Noevera25c8b22014-06-03 22:04:02 +0200939 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
Mika Westerbergdaa51402018-10-01 12:31:19 +0300940 sw->upstream_port_number, sw->depth,
941 (((u64) sw->route_hi) << 32) | sw->route_lo,
942 sw->enabled, sw->plug_events_delay);
943 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
944 sw->__unknown1, sw->__unknown4);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200945}
946
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200947/**
948 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
949 *
950 * Return: Returns 0 on success or an error code on failure.
951 */
952int tb_switch_reset(struct tb *tb, u64 route)
953{
954 struct tb_cfg_result res;
955 struct tb_regs_switch_header header = {
956 header.route_hi = route >> 32,
957 header.route_lo = route,
958 header.enabled = true,
959 };
Mika Westerbergdaa51402018-10-01 12:31:19 +0300960 tb_dbg(tb, "resetting switch at %llx\n", route);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200961 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
962 0, 2, 2, 2);
963 if (res.err)
964 return res.err;
965 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
966 if (res.err > 0)
967 return -EIO;
968 return res.err;
969}
970
Andreas Noevera25c8b22014-06-03 22:04:02 +0200971/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200972 * tb_plug_events_active() - enable/disable plug events on a switch
973 *
974 * Also configures a sane plug_events_delay of 255ms.
975 *
976 * Return: Returns 0 on success or an error code on failure.
977 */
978static int tb_plug_events_active(struct tb_switch *sw, bool active)
979{
980 u32 data;
981 int res;
982
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300983 if (!sw->config.enabled)
984 return 0;
985
Andreas Noeverca389f72014-06-03 22:04:04 +0200986 sw->config.plug_events_delay = 0xff;
987 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
988 if (res)
989 return res;
990
991 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
992 if (res)
993 return res;
994
995 if (active) {
996 data = data & 0xFFFFFF83;
997 switch (sw->config.device_id) {
Lukas Wunner1d111402016-03-20 13:57:20 +0100998 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
999 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1000 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
Andreas Noeverca389f72014-06-03 22:04:04 +02001001 break;
1002 default:
1003 data |= 4;
1004 }
1005 } else {
1006 data = data | 0x7c;
1007 }
1008 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
1009 sw->cap_plug_events + 1, 1);
1010}
1011
Mika Westerbergf67cf492017-06-06 15:25:16 +03001012static ssize_t authorized_show(struct device *dev,
1013 struct device_attribute *attr,
1014 char *buf)
1015{
1016 struct tb_switch *sw = tb_to_switch(dev);
1017
1018 return sprintf(buf, "%u\n", sw->authorized);
1019}
1020
1021static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1022{
1023 int ret = -EINVAL;
1024
Mika Westerberg09f11b62019-03-19 16:48:41 +02001025 if (!mutex_trylock(&sw->tb->lock))
1026 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +03001027
1028 if (sw->authorized)
1029 goto unlock;
1030
Mika Westerberga03e8282018-01-18 20:27:47 +03001031 /*
1032 * Make sure there is no PCIe rescan ongoing when a new PCIe
1033 * tunnel is created. Otherwise the PCIe rescan code might find
1034 * the new tunnel too early.
1035 */
1036 pci_lock_rescan_remove();
1037
Mika Westerbergf67cf492017-06-06 15:25:16 +03001038 switch (val) {
1039 /* Approve switch */
1040 case 1:
1041 if (sw->key)
1042 ret = tb_domain_approve_switch_key(sw->tb, sw);
1043 else
1044 ret = tb_domain_approve_switch(sw->tb, sw);
1045 break;
1046
1047 /* Challenge switch */
1048 case 2:
1049 if (sw->key)
1050 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1051 break;
1052
1053 default:
1054 break;
1055 }
1056
Mika Westerberga03e8282018-01-18 20:27:47 +03001057 pci_unlock_rescan_remove();
1058
Mika Westerbergf67cf492017-06-06 15:25:16 +03001059 if (!ret) {
1060 sw->authorized = val;
1061 /* Notify status change to the userspace */
1062 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1063 }
1064
1065unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +02001066 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001067 return ret;
1068}
1069
1070static ssize_t authorized_store(struct device *dev,
1071 struct device_attribute *attr,
1072 const char *buf, size_t count)
1073{
1074 struct tb_switch *sw = tb_to_switch(dev);
1075 unsigned int val;
1076 ssize_t ret;
1077
1078 ret = kstrtouint(buf, 0, &val);
1079 if (ret)
1080 return ret;
1081 if (val > 2)
1082 return -EINVAL;
1083
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001084 pm_runtime_get_sync(&sw->dev);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001085 ret = tb_switch_set_authorized(sw, val);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001086 pm_runtime_mark_last_busy(&sw->dev);
1087 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001088
1089 return ret ? ret : count;
1090}
1091static DEVICE_ATTR_RW(authorized);
1092
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001093static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1094 char *buf)
1095{
1096 struct tb_switch *sw = tb_to_switch(dev);
1097
1098 return sprintf(buf, "%u\n", sw->boot);
1099}
1100static DEVICE_ATTR_RO(boot);
1101
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001102static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1103 char *buf)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001104{
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001105 struct tb_switch *sw = tb_to_switch(dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001106
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001107 return sprintf(buf, "%#x\n", sw->device);
1108}
1109static DEVICE_ATTR_RO(device);
Andreas Noeverca389f72014-06-03 22:04:04 +02001110
Mika Westerberg72ee3392017-06-06 15:25:05 +03001111static ssize_t
1112device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1113{
1114 struct tb_switch *sw = tb_to_switch(dev);
1115
1116 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
1117}
1118static DEVICE_ATTR_RO(device_name);
1119
Mika Westerbergf67cf492017-06-06 15:25:16 +03001120static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1121 char *buf)
1122{
1123 struct tb_switch *sw = tb_to_switch(dev);
1124 ssize_t ret;
1125
Mika Westerberg09f11b62019-03-19 16:48:41 +02001126 if (!mutex_trylock(&sw->tb->lock))
1127 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +03001128
1129 if (sw->key)
1130 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1131 else
1132 ret = sprintf(buf, "\n");
1133
Mika Westerberg09f11b62019-03-19 16:48:41 +02001134 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001135 return ret;
1136}
1137
1138static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1139 const char *buf, size_t count)
1140{
1141 struct tb_switch *sw = tb_to_switch(dev);
1142 u8 key[TB_SWITCH_KEY_SIZE];
1143 ssize_t ret = count;
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +03001144 bool clear = false;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001145
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +03001146 if (!strcmp(buf, "\n"))
1147 clear = true;
1148 else if (hex2bin(key, buf, sizeof(key)))
Mika Westerbergf67cf492017-06-06 15:25:16 +03001149 return -EINVAL;
1150
Mika Westerberg09f11b62019-03-19 16:48:41 +02001151 if (!mutex_trylock(&sw->tb->lock))
1152 return restart_syscall();
Mika Westerbergf67cf492017-06-06 15:25:16 +03001153
1154 if (sw->authorized) {
1155 ret = -EBUSY;
1156 } else {
1157 kfree(sw->key);
Bernat, Yehezkele545f0d2017-08-15 08:19:20 +03001158 if (clear) {
1159 sw->key = NULL;
1160 } else {
1161 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1162 if (!sw->key)
1163 ret = -ENOMEM;
1164 }
Mika Westerbergf67cf492017-06-06 15:25:16 +03001165 }
1166
Mika Westerberg09f11b62019-03-19 16:48:41 +02001167 mutex_unlock(&sw->tb->lock);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001168 return ret;
1169}
Bernat, Yehezkel0956e412017-08-15 08:19:12 +03001170static DEVICE_ATTR(key, 0600, key_show, key_store);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001171
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001172static void nvm_authenticate_start(struct tb_switch *sw)
1173{
1174 struct pci_dev *root_port;
1175
1176 /*
1177 * During host router NVM upgrade we should not allow root port to
1178 * go into D3cold because some root ports cannot trigger PME
1179 * itself. To be on the safe side keep the root port in D0 during
1180 * the whole upgrade process.
1181 */
1182 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1183 if (root_port)
1184 pm_runtime_get_noresume(&root_port->dev);
1185}
1186
1187static void nvm_authenticate_complete(struct tb_switch *sw)
1188{
1189 struct pci_dev *root_port;
1190
1191 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1192 if (root_port)
1193 pm_runtime_put(&root_port->dev);
1194}
1195
Mika Westerberge6b245c2017-06-06 15:25:17 +03001196static ssize_t nvm_authenticate_show(struct device *dev,
1197 struct device_attribute *attr, char *buf)
1198{
1199 struct tb_switch *sw = tb_to_switch(dev);
1200 u32 status;
1201
1202 nvm_get_auth_status(sw, &status);
1203 return sprintf(buf, "%#x\n", status);
1204}
1205
1206static ssize_t nvm_authenticate_store(struct device *dev,
1207 struct device_attribute *attr, const char *buf, size_t count)
1208{
1209 struct tb_switch *sw = tb_to_switch(dev);
1210 bool val;
1211 int ret;
1212
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001213 pm_runtime_get_sync(&sw->dev);
1214
1215 if (!mutex_trylock(&sw->tb->lock)) {
1216 ret = restart_syscall();
1217 goto exit_rpm;
1218 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001219
1220 /* If NVMem devices are not yet added */
1221 if (!sw->nvm) {
1222 ret = -EAGAIN;
1223 goto exit_unlock;
1224 }
1225
1226 ret = kstrtobool(buf, &val);
1227 if (ret)
1228 goto exit_unlock;
1229
1230 /* Always clear the authentication status */
1231 nvm_clear_auth_status(sw);
1232
1233 if (val) {
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001234 if (!sw->nvm->buf) {
1235 ret = -EINVAL;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001236 goto exit_unlock;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001237 }
1238
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001239 ret = nvm_validate_and_write(sw);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001240 if (ret)
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001241 goto exit_unlock;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001242
1243 sw->nvm->authenticating = true;
1244
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001245 if (!tb_route(sw)) {
1246 /*
1247 * Keep root port from suspending as long as the
1248 * NVM upgrade process is running.
1249 */
1250 nvm_authenticate_start(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001251 ret = nvm_authenticate_host(sw);
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001252 if (ret)
1253 nvm_authenticate_complete(sw);
1254 } else {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001255 ret = nvm_authenticate_device(sw);
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001256 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001257 }
1258
1259exit_unlock:
Mika Westerberg09f11b62019-03-19 16:48:41 +02001260 mutex_unlock(&sw->tb->lock);
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001261exit_rpm:
1262 pm_runtime_mark_last_busy(&sw->dev);
1263 pm_runtime_put_autosuspend(&sw->dev);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001264
1265 if (ret)
1266 return ret;
1267 return count;
1268}
1269static DEVICE_ATTR_RW(nvm_authenticate);
1270
1271static ssize_t nvm_version_show(struct device *dev,
1272 struct device_attribute *attr, char *buf)
1273{
1274 struct tb_switch *sw = tb_to_switch(dev);
1275 int ret;
1276
Mika Westerberg09f11b62019-03-19 16:48:41 +02001277 if (!mutex_trylock(&sw->tb->lock))
1278 return restart_syscall();
Mika Westerberge6b245c2017-06-06 15:25:17 +03001279
1280 if (sw->safe_mode)
1281 ret = -ENODATA;
1282 else if (!sw->nvm)
1283 ret = -EAGAIN;
1284 else
1285 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1286
Mika Westerberg09f11b62019-03-19 16:48:41 +02001287 mutex_unlock(&sw->tb->lock);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001288
1289 return ret;
1290}
1291static DEVICE_ATTR_RO(nvm_version);
1292
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001293static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1294 char *buf)
1295{
1296 struct tb_switch *sw = tb_to_switch(dev);
1297
1298 return sprintf(buf, "%#x\n", sw->vendor);
1299}
1300static DEVICE_ATTR_RO(vendor);
1301
Mika Westerberg72ee3392017-06-06 15:25:05 +03001302static ssize_t
1303vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1304{
1305 struct tb_switch *sw = tb_to_switch(dev);
1306
1307 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1308}
1309static DEVICE_ATTR_RO(vendor_name);
1310
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001311static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1312 char *buf)
1313{
1314 struct tb_switch *sw = tb_to_switch(dev);
1315
1316 return sprintf(buf, "%pUb\n", sw->uuid);
1317}
1318static DEVICE_ATTR_RO(unique_id);
1319
1320static struct attribute *switch_attrs[] = {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001321 &dev_attr_authorized.attr,
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001322 &dev_attr_boot.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001323 &dev_attr_device.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +03001324 &dev_attr_device_name.attr,
Mika Westerbergf67cf492017-06-06 15:25:16 +03001325 &dev_attr_key.attr,
Mika Westerberge6b245c2017-06-06 15:25:17 +03001326 &dev_attr_nvm_authenticate.attr,
1327 &dev_attr_nvm_version.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001328 &dev_attr_vendor.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +03001329 &dev_attr_vendor_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001330 &dev_attr_unique_id.attr,
1331 NULL,
1332};
1333
Mika Westerbergf67cf492017-06-06 15:25:16 +03001334static umode_t switch_attr_is_visible(struct kobject *kobj,
1335 struct attribute *attr, int n)
1336{
1337 struct device *dev = container_of(kobj, struct device, kobj);
1338 struct tb_switch *sw = tb_to_switch(dev);
1339
Mika Westerberg58f414f2018-09-11 15:34:23 +03001340 if (attr == &dev_attr_device.attr) {
1341 if (!sw->device)
1342 return 0;
1343 } else if (attr == &dev_attr_device_name.attr) {
1344 if (!sw->device_name)
1345 return 0;
1346 } else if (attr == &dev_attr_vendor.attr) {
1347 if (!sw->vendor)
1348 return 0;
1349 } else if (attr == &dev_attr_vendor_name.attr) {
1350 if (!sw->vendor_name)
1351 return 0;
1352 } else if (attr == &dev_attr_key.attr) {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001353 if (tb_route(sw) &&
1354 sw->tb->security_level == TB_SECURITY_SECURE &&
1355 sw->security_level == TB_SECURITY_SECURE)
1356 return attr->mode;
1357 return 0;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001358 } else if (attr == &dev_attr_nvm_authenticate.attr ||
1359 attr == &dev_attr_nvm_version.attr) {
1360 if (sw->dma_port)
1361 return attr->mode;
1362 return 0;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +02001363 } else if (attr == &dev_attr_boot.attr) {
1364 if (tb_route(sw))
1365 return attr->mode;
1366 return 0;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001367 }
1368
Mika Westerberge6b245c2017-06-06 15:25:17 +03001369 return sw->safe_mode ? 0 : attr->mode;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001370}
1371
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001372static struct attribute_group switch_group = {
Mika Westerbergf67cf492017-06-06 15:25:16 +03001373 .is_visible = switch_attr_is_visible,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001374 .attrs = switch_attrs,
1375};
1376
1377static const struct attribute_group *switch_groups[] = {
1378 &switch_group,
1379 NULL,
1380};
1381
1382static void tb_switch_release(struct device *dev)
1383{
1384 struct tb_switch *sw = tb_to_switch(dev);
Mika Westerberg0b2863a2017-02-19 16:57:27 +02001385 int i;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001386
Mika Westerberg3e136762017-06-06 15:25:14 +03001387 dma_port_free(sw->dma_port);
1388
Mika Westerberg0b2863a2017-02-19 16:57:27 +02001389 for (i = 1; i <= sw->config.max_port_number; i++) {
1390 if (!sw->ports[i].disabled) {
1391 ida_destroy(&sw->ports[i].in_hopids);
1392 ida_destroy(&sw->ports[i].out_hopids);
1393 }
1394 }
1395
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001396 kfree(sw->uuid);
Mika Westerberg72ee3392017-06-06 15:25:05 +03001397 kfree(sw->device_name);
1398 kfree(sw->vendor_name);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001399 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +02001400 kfree(sw->drom);
Mika Westerbergf67cf492017-06-06 15:25:16 +03001401 kfree(sw->key);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001402 kfree(sw);
1403}
1404
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001405/*
1406 * Currently only need to provide the callbacks. Everything else is handled
1407 * in the connection manager.
1408 */
1409static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
1410{
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001411 struct tb_switch *sw = tb_to_switch(dev);
1412 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
1413
1414 if (cm_ops->runtime_suspend_switch)
1415 return cm_ops->runtime_suspend_switch(sw);
1416
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001417 return 0;
1418}
1419
1420static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
1421{
Mika Westerberg4f7c2e02019-05-28 18:56:20 +03001422 struct tb_switch *sw = tb_to_switch(dev);
1423 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
1424
1425 if (cm_ops->runtime_resume_switch)
1426 return cm_ops->runtime_resume_switch(sw);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001427 return 0;
1428}
1429
1430static const struct dev_pm_ops tb_switch_pm_ops = {
1431 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
1432 NULL)
1433};
1434
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001435struct device_type tb_switch_type = {
1436 .name = "thunderbolt_device",
1437 .release = tb_switch_release,
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001438 .pm = &tb_switch_pm_ops,
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001439};
1440
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001441static int tb_switch_get_generation(struct tb_switch *sw)
1442{
1443 switch (sw->config.device_id) {
1444 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1445 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1446 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1447 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1448 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1449 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1450 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1451 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1452 return 1;
1453
1454 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1455 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1456 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1457 return 2;
1458
1459 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1460 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1461 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1462 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1463 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
Radion Mirchevsky4bac4712017-10-04 16:43:43 +03001464 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1465 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1466 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001467 return 3;
1468
1469 default:
1470 /*
1471 * For unknown switches assume generation to be 1 to be
1472 * on the safe side.
1473 */
1474 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1475 sw->config.device_id);
1476 return 1;
1477 }
1478}
1479
Andreas Noevera25c8b22014-06-03 22:04:02 +02001480/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001481 * tb_switch_alloc() - allocate a switch
1482 * @tb: Pointer to the owning domain
1483 * @parent: Parent device for this switch
1484 * @route: Route string for this switch
Andreas Noevera25c8b22014-06-03 22:04:02 +02001485 *
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001486 * Allocates and initializes a switch. Will not upload configuration to
1487 * the switch. For that you need to call tb_switch_configure()
1488 * separately. The returned switch should be released by calling
1489 * tb_switch_put().
1490 *
Mika Westerberg444ac382018-12-30 12:17:52 +02001491 * Return: Pointer to the allocated switch or ERR_PTR() in case of
1492 * failure.
Andreas Noevera25c8b22014-06-03 22:04:02 +02001493 */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001494struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1495 u64 route)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001496{
Andreas Noevera25c8b22014-06-03 22:04:02 +02001497 struct tb_switch *sw;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001498 int upstream_port;
Mika Westerberg444ac382018-12-30 12:17:52 +02001499 int i, ret, depth;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001500
1501 /* Make sure we do not exceed maximum topology limit */
1502 depth = tb_route_length(route);
1503 if (depth > TB_SWITCH_MAX_DEPTH)
Mika Westerberg444ac382018-12-30 12:17:52 +02001504 return ERR_PTR(-EADDRNOTAVAIL);
Mika Westerbergf0342e72018-12-30 12:14:46 +02001505
1506 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001507 if (upstream_port < 0)
Mika Westerberg444ac382018-12-30 12:17:52 +02001508 return ERR_PTR(upstream_port);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001509
1510 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1511 if (!sw)
Mika Westerberg444ac382018-12-30 12:17:52 +02001512 return ERR_PTR(-ENOMEM);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001513
1514 sw->tb = tb;
Mika Westerberg444ac382018-12-30 12:17:52 +02001515 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
1516 if (ret)
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001517 goto err_free_sw_ports;
1518
Mika Westerbergdaa51402018-10-01 12:31:19 +03001519 tb_dbg(tb, "current switch config:\n");
Andreas Noevera25c8b22014-06-03 22:04:02 +02001520 tb_dump_switch(tb, &sw->config);
1521
1522 /* configure switch */
1523 sw->config.upstream_port_number = upstream_port;
Mika Westerbergf0342e72018-12-30 12:14:46 +02001524 sw->config.depth = depth;
1525 sw->config.route_hi = upper_32_bits(route);
1526 sw->config.route_lo = lower_32_bits(route);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001527 sw->config.enabled = 0;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001528
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001529 /* initialize ports */
1530 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
1531 GFP_KERNEL);
Mika Westerberg444ac382018-12-30 12:17:52 +02001532 if (!sw->ports) {
1533 ret = -ENOMEM;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001534 goto err_free_sw_ports;
Mika Westerberg444ac382018-12-30 12:17:52 +02001535 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001536
1537 for (i = 0; i <= sw->config.max_port_number; i++) {
1538 /* minimum setup for tb_find_cap and tb_drom_read to work */
1539 sw->ports[i].sw = sw;
1540 sw->ports[i].port = i;
1541 }
1542
Mika Westerberg2c3c4192017-06-06 15:25:13 +03001543 sw->generation = tb_switch_get_generation(sw);
1544
Mika Westerberg444ac382018-12-30 12:17:52 +02001545 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
1546 if (ret < 0) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001547 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1548 goto err_free_sw_ports;
1549 }
Mika Westerberg444ac382018-12-30 12:17:52 +02001550 sw->cap_plug_events = ret;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001551
Mika Westerberg444ac382018-12-30 12:17:52 +02001552 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
1553 if (ret > 0)
1554 sw->cap_lc = ret;
Mika Westerberga9be5582019-01-09 16:42:12 +02001555
Mika Westerbergf67cf492017-06-06 15:25:16 +03001556 /* Root switch is always authorized */
1557 if (!route)
1558 sw->authorized = true;
1559
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001560 device_initialize(&sw->dev);
1561 sw->dev.parent = parent;
1562 sw->dev.bus = &tb_bus_type;
1563 sw->dev.type = &tb_switch_type;
1564 sw->dev.groups = switch_groups;
1565 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1566
1567 return sw;
1568
1569err_free_sw_ports:
1570 kfree(sw->ports);
1571 kfree(sw);
1572
Mika Westerberg444ac382018-12-30 12:17:52 +02001573 return ERR_PTR(ret);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001574}
1575
1576/**
Mika Westerberge6b245c2017-06-06 15:25:17 +03001577 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1578 * @tb: Pointer to the owning domain
1579 * @parent: Parent device for this switch
1580 * @route: Route string for this switch
1581 *
1582 * This creates a switch in safe mode. This means the switch pretty much
1583 * lacks all capabilities except DMA configuration port before it is
1584 * flashed with a valid NVM firmware.
1585 *
1586 * The returned switch must be released by calling tb_switch_put().
1587 *
Mika Westerberg444ac382018-12-30 12:17:52 +02001588 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
Mika Westerberge6b245c2017-06-06 15:25:17 +03001589 */
1590struct tb_switch *
1591tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
1592{
1593 struct tb_switch *sw;
1594
1595 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1596 if (!sw)
Mika Westerberg444ac382018-12-30 12:17:52 +02001597 return ERR_PTR(-ENOMEM);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001598
1599 sw->tb = tb;
1600 sw->config.depth = tb_route_length(route);
1601 sw->config.route_hi = upper_32_bits(route);
1602 sw->config.route_lo = lower_32_bits(route);
1603 sw->safe_mode = true;
1604
1605 device_initialize(&sw->dev);
1606 sw->dev.parent = parent;
1607 sw->dev.bus = &tb_bus_type;
1608 sw->dev.type = &tb_switch_type;
1609 sw->dev.groups = switch_groups;
1610 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1611
1612 return sw;
1613}
1614
1615/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001616 * tb_switch_configure() - Uploads configuration to the switch
1617 * @sw: Switch to configure
1618 *
1619 * Call this function before the switch is added to the system. It will
1620 * upload configuration to the switch and makes it available for the
1621 * connection manager to use.
1622 *
1623 * Return: %0 in case of success and negative errno in case of failure
1624 */
1625int tb_switch_configure(struct tb_switch *sw)
1626{
1627 struct tb *tb = sw->tb;
1628 u64 route;
1629 int ret;
1630
1631 route = tb_route(sw);
Mika Westerbergdaa51402018-10-01 12:31:19 +03001632 tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1633 route, tb_route_length(route), sw->config.upstream_port_number);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001634
1635 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
Andreas Noevera25c8b22014-06-03 22:04:02 +02001636 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
1637 sw->config.vendor_id);
1638
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001639 sw->config.enabled = 1;
1640
Andreas Noevera25c8b22014-06-03 22:04:02 +02001641 /* upload configuration */
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001642 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
1643 if (ret)
1644 return ret;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001645
Mika Westerberge879a702018-10-11 12:33:08 +03001646 ret = tb_lc_configure_link(sw);
1647 if (ret)
1648 return ret;
1649
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001650 return tb_plug_events_active(sw, true);
1651}
Andreas Noevera25c8b22014-06-03 22:04:02 +02001652
Aditya Pakki2cc12752019-03-20 10:57:54 -05001653static int tb_switch_set_uuid(struct tb_switch *sw)
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001654{
1655 u32 uuid[4];
Mika Westerberga9be5582019-01-09 16:42:12 +02001656 int ret;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001657
1658 if (sw->uuid)
Mika Westerberga9be5582019-01-09 16:42:12 +02001659 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001660
1661 /*
1662 * The newer controllers include fused UUID as part of link
1663 * controller specific registers
1664 */
Mika Westerberga9be5582019-01-09 16:42:12 +02001665 ret = tb_lc_read_uuid(sw, uuid);
1666 if (ret) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001667 /*
1668 * ICM generates UUID based on UID and fills the upper
1669 * two words with ones. This is not strictly following
1670 * UUID format but we want to be compatible with it so
1671 * we do the same here.
1672 */
1673 uuid[0] = sw->uid & 0xffffffff;
1674 uuid[1] = (sw->uid >> 32) & 0xffffffff;
1675 uuid[2] = 0xffffffff;
1676 uuid[3] = 0xffffffff;
Andreas Noevera25c8b22014-06-03 22:04:02 +02001677 }
1678
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001679 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
Aditya Pakki2cc12752019-03-20 10:57:54 -05001680 if (!sw->uuid)
Mika Westerberga9be5582019-01-09 16:42:12 +02001681 return -ENOMEM;
1682 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001683}
1684
Mika Westerberge6b245c2017-06-06 15:25:17 +03001685static int tb_switch_add_dma_port(struct tb_switch *sw)
Mika Westerberg3e136762017-06-06 15:25:14 +03001686{
Mika Westerberge6b245c2017-06-06 15:25:17 +03001687 u32 status;
1688 int ret;
1689
Mika Westerberg3e136762017-06-06 15:25:14 +03001690 switch (sw->generation) {
1691 case 3:
1692 break;
1693
1694 case 2:
1695 /* Only root switch can be upgraded */
1696 if (tb_route(sw))
Mika Westerberge6b245c2017-06-06 15:25:17 +03001697 return 0;
Mika Westerberg3e136762017-06-06 15:25:14 +03001698 break;
1699
1700 default:
Mika Westerberge6b245c2017-06-06 15:25:17 +03001701 /*
1702 * DMA port is the only thing available when the switch
1703 * is in safe mode.
1704 */
1705 if (!sw->safe_mode)
1706 return 0;
1707 break;
Mika Westerberg3e136762017-06-06 15:25:14 +03001708 }
1709
Mika Westerberge6b245c2017-06-06 15:25:17 +03001710 if (sw->no_nvm_upgrade)
1711 return 0;
1712
Mika Westerberg3e136762017-06-06 15:25:14 +03001713 sw->dma_port = dma_port_alloc(sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001714 if (!sw->dma_port)
1715 return 0;
1716
1717 /*
1718 * Check status of the previous flash authentication. If there
1719 * is one we need to power cycle the switch in any case to make
1720 * it functional again.
1721 */
1722 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
1723 if (ret <= 0)
1724 return ret;
1725
Mika Westerberg1830b6e2018-11-26 12:47:46 +03001726 /* Now we can allow root port to suspend again */
1727 if (!tb_route(sw))
1728 nvm_authenticate_complete(sw);
1729
Mika Westerberge6b245c2017-06-06 15:25:17 +03001730 if (status) {
1731 tb_sw_info(sw, "switch flash authentication failed\n");
Aditya Pakki2cc12752019-03-20 10:57:54 -05001732 ret = tb_switch_set_uuid(sw);
1733 if (ret)
1734 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001735 nvm_set_auth_status(sw, status);
1736 }
1737
1738 tb_sw_info(sw, "power cycling the switch now\n");
1739 dma_port_power_cycle(sw->dma_port);
1740
1741 /*
1742 * We return error here which causes the switch adding failure.
1743 * It should appear back after power cycle is complete.
1744 */
1745 return -ESHUTDOWN;
Mika Westerberg3e136762017-06-06 15:25:14 +03001746}
1747
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001748/**
1749 * tb_switch_add() - Add a switch to the domain
1750 * @sw: Switch to add
1751 *
1752 * This is the last step in adding switch to the domain. It will read
1753 * identification information from DROM and initializes ports so that
1754 * they can be used to connect other switches. The switch will be
1755 * exposed to the userspace when this function successfully returns. To
1756 * remove and release the switch, call tb_switch_remove().
1757 *
1758 * Return: %0 in case of success and negative errno in case of failure
1759 */
1760int tb_switch_add(struct tb_switch *sw)
1761{
1762 int i, ret;
Andreas Noeverca389f72014-06-03 22:04:04 +02001763
Mika Westerberg3e136762017-06-06 15:25:14 +03001764 /*
1765 * Initialize DMA control port now before we read DROM. Recent
1766 * host controllers have more complete DROM on NVM that includes
1767 * vendor and model identification strings which we then expose
1768 * to the userspace. NVM can be accessed through DMA
1769 * configuration based mailbox.
1770 */
Mika Westerberge6b245c2017-06-06 15:25:17 +03001771 ret = tb_switch_add_dma_port(sw);
1772 if (ret)
Mika Westerbergf53e7672017-06-06 15:25:02 +03001773 return ret;
Andreas Noever343fcb82014-06-12 23:11:47 +02001774
Mika Westerberge6b245c2017-06-06 15:25:17 +03001775 if (!sw->safe_mode) {
1776 /* read drom */
1777 ret = tb_drom_read(sw);
1778 if (ret) {
1779 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001780 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001781 }
Mika Westerbergdaa51402018-10-01 12:31:19 +03001782 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001783
Aditya Pakki2cc12752019-03-20 10:57:54 -05001784 ret = tb_switch_set_uuid(sw);
1785 if (ret)
1786 return ret;
Mika Westerberge6b245c2017-06-06 15:25:17 +03001787
1788 for (i = 0; i <= sw->config.max_port_number; i++) {
1789 if (sw->ports[i].disabled) {
Mika Westerbergdaa51402018-10-01 12:31:19 +03001790 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
Mika Westerberge6b245c2017-06-06 15:25:17 +03001791 continue;
1792 }
1793 ret = tb_init_port(&sw->ports[i]);
1794 if (ret)
1795 return ret;
1796 }
Andreas Noever343fcb82014-06-12 23:11:47 +02001797 }
1798
Mika Westerberge6b245c2017-06-06 15:25:17 +03001799 ret = device_add(&sw->dev);
1800 if (ret)
1801 return ret;
1802
Mika Westerberga83bc4a2018-10-01 12:31:20 +03001803 if (tb_route(sw)) {
1804 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
1805 sw->vendor, sw->device);
1806 if (sw->vendor_name && sw->device_name)
1807 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
1808 sw->device_name);
1809 }
1810
Mika Westerberge6b245c2017-06-06 15:25:17 +03001811 ret = tb_switch_nvm_add(sw);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001812 if (ret) {
Mika Westerberge6b245c2017-06-06 15:25:17 +03001813 device_del(&sw->dev);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001814 return ret;
1815 }
Mika Westerberge6b245c2017-06-06 15:25:17 +03001816
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001817 pm_runtime_set_active(&sw->dev);
1818 if (sw->rpm) {
1819 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
1820 pm_runtime_use_autosuspend(&sw->dev);
1821 pm_runtime_mark_last_busy(&sw->dev);
1822 pm_runtime_enable(&sw->dev);
1823 pm_request_autosuspend(&sw->dev);
1824 }
1825
1826 return 0;
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001827}
Andreas Noeverc90553b2014-06-03 22:04:11 +02001828
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001829/**
1830 * tb_switch_remove() - Remove and release a switch
1831 * @sw: Switch to remove
1832 *
1833 * This will remove the switch from the domain and release it after last
1834 * reference count drops to zero. If there are switches connected below
1835 * this switch, they will be removed as well.
1836 */
1837void tb_switch_remove(struct tb_switch *sw)
1838{
1839 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +02001840
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001841 if (sw->rpm) {
1842 pm_runtime_get_sync(&sw->dev);
1843 pm_runtime_disable(&sw->dev);
1844 }
1845
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001846 /* port 0 is the switch itself and never has a remote */
1847 for (i = 1; i <= sw->config.max_port_number; i++) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001848 if (tb_port_has_remote(&sw->ports[i])) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001849 tb_switch_remove(sw->ports[i].remote->sw);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001850 sw->ports[i].remote = NULL;
1851 } else if (sw->ports[i].xdomain) {
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001852 tb_xdomain_remove(sw->ports[i].xdomain);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001853 sw->ports[i].xdomain = NULL;
1854 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001855 }
1856
1857 if (!sw->is_unplugged)
1858 tb_plug_events_active(sw, false);
Mika Westerberge879a702018-10-11 12:33:08 +03001859 tb_lc_unconfigure_link(sw);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001860
Mika Westerberge6b245c2017-06-06 15:25:17 +03001861 tb_switch_nvm_remove(sw);
Mika Westerberga83bc4a2018-10-01 12:31:20 +03001862
1863 if (tb_route(sw))
1864 dev_info(&sw->dev, "device disconnected\n");
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001865 device_unregister(&sw->dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001866}
1867
Andreas Noever053596d2014-06-03 22:04:06 +02001868/**
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001869 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
Andreas Noever053596d2014-06-03 22:04:06 +02001870 */
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001871void tb_sw_set_unplugged(struct tb_switch *sw)
Andreas Noever053596d2014-06-03 22:04:06 +02001872{
1873 int i;
1874 if (sw == sw->tb->root_switch) {
1875 tb_sw_WARN(sw, "cannot unplug root switch\n");
1876 return;
1877 }
1878 if (sw->is_unplugged) {
1879 tb_sw_WARN(sw, "is_unplugged already set\n");
1880 return;
1881 }
1882 sw->is_unplugged = true;
1883 for (i = 0; i <= sw->config.max_port_number; i++) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001884 if (tb_port_has_remote(&sw->ports[i]))
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001885 tb_sw_set_unplugged(sw->ports[i].remote->sw);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001886 else if (sw->ports[i].xdomain)
1887 sw->ports[i].xdomain->is_unplugged = true;
Andreas Noever053596d2014-06-03 22:04:06 +02001888 }
1889}
1890
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001891int tb_switch_resume(struct tb_switch *sw)
1892{
1893 int i, err;
Mika Westerbergdaa51402018-10-01 12:31:19 +03001894 tb_sw_dbg(sw, "resuming switch\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001895
Mika Westerberg08a5e4c2017-06-06 15:24:54 +03001896 /*
1897 * Check for UID of the connected switches except for root
1898 * switch which we assume cannot be removed.
1899 */
1900 if (tb_route(sw)) {
1901 u64 uid;
1902
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001903 /*
1904 * Check first that we can still read the switch config
1905 * space. It may be that there is now another domain
1906 * connected.
1907 */
1908 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
1909 if (err < 0) {
1910 tb_sw_info(sw, "switch not present anymore\n");
1911 return err;
1912 }
1913
Mika Westerberg08a5e4c2017-06-06 15:24:54 +03001914 err = tb_drom_read_uid_only(sw, &uid);
1915 if (err) {
1916 tb_sw_warn(sw, "uid read failed\n");
1917 return err;
1918 }
1919 if (sw->uid != uid) {
1920 tb_sw_info(sw,
1921 "changed while suspended (uid %#llx -> %#llx)\n",
1922 sw->uid, uid);
1923 return -ENODEV;
1924 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001925 }
1926
1927 /* upload configuration */
1928 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
1929 if (err)
1930 return err;
1931
Mika Westerberge879a702018-10-11 12:33:08 +03001932 err = tb_lc_configure_link(sw);
1933 if (err)
1934 return err;
1935
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001936 err = tb_plug_events_active(sw, true);
1937 if (err)
1938 return err;
1939
1940 /* check for surviving downstream switches */
1941 for (i = 1; i <= sw->config.max_port_number; i++) {
1942 struct tb_port *port = &sw->ports[i];
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001943
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001944 if (!tb_port_has_remote(port) && !port->xdomain)
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001945 continue;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001946
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001947 if (tb_wait_for_port(port, true) <= 0) {
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001948 tb_port_warn(port,
1949 "lost during suspend, disconnecting\n");
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001950 if (tb_port_has_remote(port))
1951 tb_sw_set_unplugged(port->remote->sw);
1952 else if (port->xdomain)
1953 port->xdomain->is_unplugged = true;
1954 } else if (tb_port_has_remote(port)) {
1955 if (tb_switch_resume(port->remote->sw)) {
1956 tb_port_warn(port,
1957 "lost during suspend, disconnecting\n");
1958 tb_sw_set_unplugged(port->remote->sw);
1959 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001960 }
1961 }
1962 return 0;
1963}
1964
1965void tb_switch_suspend(struct tb_switch *sw)
1966{
1967 int i, err;
1968 err = tb_plug_events_active(sw, false);
1969 if (err)
1970 return;
1971
1972 for (i = 1; i <= sw->config.max_port_number; i++) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001973 if (tb_port_has_remote(&sw->ports[i]))
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001974 tb_switch_suspend(sw->ports[i].remote->sw);
1975 }
Mika Westerberg5480dfc2019-01-09 17:25:43 +02001976
1977 tb_lc_set_sleep(sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001978}
Mika Westerbergf67cf492017-06-06 15:25:16 +03001979
1980struct tb_sw_lookup {
1981 struct tb *tb;
1982 u8 link;
1983 u8 depth;
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02001984 const uuid_t *uuid;
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03001985 u64 route;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001986};
1987
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +01001988static int tb_switch_match(struct device *dev, const void *data)
Mika Westerbergf67cf492017-06-06 15:25:16 +03001989{
1990 struct tb_switch *sw = tb_to_switch(dev);
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +01001991 const struct tb_sw_lookup *lookup = data;
Mika Westerbergf67cf492017-06-06 15:25:16 +03001992
1993 if (!sw)
1994 return 0;
1995 if (sw->tb != lookup->tb)
1996 return 0;
1997
1998 if (lookup->uuid)
1999 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
2000
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03002001 if (lookup->route) {
2002 return sw->config.route_lo == lower_32_bits(lookup->route) &&
2003 sw->config.route_hi == upper_32_bits(lookup->route);
2004 }
2005
Mika Westerbergf67cf492017-06-06 15:25:16 +03002006 /* Root switch is matched only by depth */
2007 if (!lookup->depth)
2008 return !sw->depth;
2009
2010 return sw->link == lookup->link && sw->depth == lookup->depth;
2011}
2012
2013/**
2014 * tb_switch_find_by_link_depth() - Find switch by link and depth
2015 * @tb: Domain the switch belongs
2016 * @link: Link number the switch is connected
2017 * @depth: Depth of the switch in link
2018 *
2019 * Returned switch has reference count increased so the caller needs to
2020 * call tb_switch_put() when done with the switch.
2021 */
2022struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
2023{
2024 struct tb_sw_lookup lookup;
2025 struct device *dev;
2026
2027 memset(&lookup, 0, sizeof(lookup));
2028 lookup.tb = tb;
2029 lookup.link = link;
2030 lookup.depth = depth;
2031
2032 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2033 if (dev)
2034 return tb_to_switch(dev);
2035
2036 return NULL;
2037}
2038
2039/**
Radion Mirchevsky432019d2017-10-04 15:07:40 +03002040 * tb_switch_find_by_uuid() - Find switch by UUID
Mika Westerbergf67cf492017-06-06 15:25:16 +03002041 * @tb: Domain the switch belongs
2042 * @uuid: UUID to look for
2043 *
2044 * Returned switch has reference count increased so the caller needs to
2045 * call tb_switch_put() when done with the switch.
2046 */
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +02002047struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
Mika Westerbergf67cf492017-06-06 15:25:16 +03002048{
2049 struct tb_sw_lookup lookup;
2050 struct device *dev;
2051
2052 memset(&lookup, 0, sizeof(lookup));
2053 lookup.tb = tb;
2054 lookup.uuid = uuid;
2055
2056 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2057 if (dev)
2058 return tb_to_switch(dev);
2059
2060 return NULL;
2061}
Mika Westerberge6b245c2017-06-06 15:25:17 +03002062
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +03002063/**
2064 * tb_switch_find_by_route() - Find switch by route string
2065 * @tb: Domain the switch belongs
2066 * @route: Route string to look for
2067 *
2068 * Returned switch has reference count increased so the caller needs to
2069 * call tb_switch_put() when done with the switch.
2070 */
2071struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
2072{
2073 struct tb_sw_lookup lookup;
2074 struct device *dev;
2075
2076 if (!route)
2077 return tb_switch_get(tb->root_switch);
2078
2079 memset(&lookup, 0, sizeof(lookup));
2080 lookup.tb = tb;
2081 lookup.route = route;
2082
2083 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2084 if (dev)
2085 return tb_to_switch(dev);
2086
2087 return NULL;
2088}
2089
Mika Westerberge6b245c2017-06-06 15:25:17 +03002090void tb_switch_exit(void)
2091{
2092 ida_destroy(&nvm_ida);
2093}