1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thunderbolt bus support
4  *
5  * Copyright (C) 2017, Intel Corporation
6  * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7  */
8 
9 #include <linux/device.h>
10 #include <linux/idr.h>
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14 #include <linux/random.h>
15 #include <crypto/hash.h>
16 
17 #include "tb.h"
18 
19 static DEFINE_IDA(tb_domain_ida);
20 
match_service_id(const struct tb_service_id * id,const struct tb_service * svc)21 static bool match_service_id(const struct tb_service_id *id,
22 			     const struct tb_service *svc)
23 {
24 	if (id->match_flags & TBSVC_MATCH_PROTOCOL_KEY) {
25 		if (strcmp(id->protocol_key, svc->key))
26 			return false;
27 	}
28 
29 	if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) {
30 		if (id->protocol_id != svc->prtcid)
31 			return false;
32 	}
33 
34 	if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
35 		if (id->protocol_version != svc->prtcvers)
36 			return false;
37 	}
38 
39 	if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
40 		if (id->protocol_revision != svc->prtcrevs)
41 			return false;
42 	}
43 
44 	return true;
45 }
46 
__tb_service_match(struct device * dev,struct device_driver * drv)47 static const struct tb_service_id *__tb_service_match(struct device *dev,
48 						      struct device_driver *drv)
49 {
50 	struct tb_service_driver *driver;
51 	const struct tb_service_id *ids;
52 	struct tb_service *svc;
53 
54 	svc = tb_to_service(dev);
55 	if (!svc)
56 		return NULL;
57 
58 	driver = container_of(drv, struct tb_service_driver, driver);
59 	if (!driver->id_table)
60 		return NULL;
61 
62 	for (ids = driver->id_table; ids->match_flags != 0; ids++) {
63 		if (match_service_id(ids, svc))
64 			return ids;
65 	}
66 
67 	return NULL;
68 }
69 
tb_service_match(struct device * dev,struct device_driver * drv)70 static int tb_service_match(struct device *dev, struct device_driver *drv)
71 {
72 	return !!__tb_service_match(dev, drv);
73 }
74 
tb_service_probe(struct device * dev)75 static int tb_service_probe(struct device *dev)
76 {
77 	struct tb_service *svc = tb_to_service(dev);
78 	struct tb_service_driver *driver;
79 	const struct tb_service_id *id;
80 
81 	driver = container_of(dev->driver, struct tb_service_driver, driver);
82 	id = __tb_service_match(dev, &driver->driver);
83 
84 	return driver->probe(svc, id);
85 }
86 
tb_service_remove(struct device * dev)87 static void tb_service_remove(struct device *dev)
88 {
89 	struct tb_service *svc = tb_to_service(dev);
90 	struct tb_service_driver *driver;
91 
92 	driver = container_of(dev->driver, struct tb_service_driver, driver);
93 	if (driver->remove)
94 		driver->remove(svc);
95 }
96 
tb_service_shutdown(struct device * dev)97 static void tb_service_shutdown(struct device *dev)
98 {
99 	struct tb_service_driver *driver;
100 	struct tb_service *svc;
101 
102 	svc = tb_to_service(dev);
103 	if (!svc || !dev->driver)
104 		return;
105 
106 	driver = container_of(dev->driver, struct tb_service_driver, driver);
107 	if (driver->shutdown)
108 		driver->shutdown(svc);
109 }
110 
111 static const char * const tb_security_names[] = {
112 	[TB_SECURITY_NONE] = "none",
113 	[TB_SECURITY_USER] = "user",
114 	[TB_SECURITY_SECURE] = "secure",
115 	[TB_SECURITY_DPONLY] = "dponly",
116 	[TB_SECURITY_USBONLY] = "usbonly",
117 	[TB_SECURITY_NOPCIE] = "nopcie",
118 };
119 
boot_acl_show(struct device * dev,struct device_attribute * attr,char * buf)120 static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,
121 			     char *buf)
122 {
123 	struct tb *tb = container_of(dev, struct tb, dev);
124 	uuid_t *uuids;
125 	ssize_t ret;
126 	int i;
127 
128 	uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
129 	if (!uuids)
130 		return -ENOMEM;
131 
132 	pm_runtime_get_sync(&tb->dev);
133 
134 	if (mutex_lock_interruptible(&tb->lock)) {
135 		ret = -ERESTARTSYS;
136 		goto out;
137 	}
138 	ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
139 	if (ret) {
140 		mutex_unlock(&tb->lock);
141 		goto out;
142 	}
143 	mutex_unlock(&tb->lock);
144 
145 	for (ret = 0, i = 0; i < tb->nboot_acl; i++) {
146 		if (!uuid_is_null(&uuids[i]))
147 			ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%pUb",
148 					&uuids[i]);
149 
150 		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s",
151 			       i < tb->nboot_acl - 1 ? "," : "\n");
152 	}
153 
154 out:
155 	pm_runtime_mark_last_busy(&tb->dev);
156 	pm_runtime_put_autosuspend(&tb->dev);
157 	kfree(uuids);
158 
159 	return ret;
160 }
161 
boot_acl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)162 static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr,
163 			      const char *buf, size_t count)
164 {
165 	struct tb *tb = container_of(dev, struct tb, dev);
166 	char *str, *s, *uuid_str;
167 	ssize_t ret = 0;
168 	uuid_t *acl;
169 	int i = 0;
170 
171 	/*
172 	 * Make sure the value is not bigger than tb->nboot_acl * UUID
173 	 * length + commas and optional "\n". Also the smallest allowable
174 	 * string is tb->nboot_acl * ",".
175 	 */
176 	if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1)
177 		return -EINVAL;
178 	if (count < tb->nboot_acl - 1)
179 		return -EINVAL;
180 
181 	str = kstrdup(buf, GFP_KERNEL);
182 	if (!str)
183 		return -ENOMEM;
184 
185 	acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
186 	if (!acl) {
187 		ret = -ENOMEM;
188 		goto err_free_str;
189 	}
190 
191 	uuid_str = strim(str);
192 	while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
193 		size_t len = strlen(s);
194 
195 		if (len) {
196 			if (len != UUID_STRING_LEN) {
197 				ret = -EINVAL;
198 				goto err_free_acl;
199 			}
200 			ret = uuid_parse(s, &acl[i]);
201 			if (ret)
202 				goto err_free_acl;
203 		}
204 
205 		i++;
206 	}
207 
208 	if (s || i < tb->nboot_acl) {
209 		ret = -EINVAL;
210 		goto err_free_acl;
211 	}
212 
213 	pm_runtime_get_sync(&tb->dev);
214 
215 	if (mutex_lock_interruptible(&tb->lock)) {
216 		ret = -ERESTARTSYS;
217 		goto err_rpm_put;
218 	}
219 	ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
220 	if (!ret) {
221 		/* Notify userspace about the change */
222 		kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE);
223 	}
224 	mutex_unlock(&tb->lock);
225 
226 err_rpm_put:
227 	pm_runtime_mark_last_busy(&tb->dev);
228 	pm_runtime_put_autosuspend(&tb->dev);
229 err_free_acl:
230 	kfree(acl);
231 err_free_str:
232 	kfree(str);
233 
234 	return ret ?: count;
235 }
236 static DEVICE_ATTR_RW(boot_acl);
237 
deauthorization_show(struct device * dev,struct device_attribute * attr,char * buf)238 static ssize_t deauthorization_show(struct device *dev,
239 				    struct device_attribute *attr,
240 				    char *buf)
241 {
242 	const struct tb *tb = container_of(dev, struct tb, dev);
243 	bool deauthorization = false;
244 
245 	/* Only meaningful if authorization is supported */
246 	if (tb->security_level == TB_SECURITY_USER ||
247 	    tb->security_level == TB_SECURITY_SECURE)
248 		deauthorization = !!tb->cm_ops->disapprove_switch;
249 
250 	return sprintf(buf, "%d\n", deauthorization);
251 }
252 static DEVICE_ATTR_RO(deauthorization);
253 
iommu_dma_protection_show(struct device * dev,struct device_attribute * attr,char * buf)254 static ssize_t iommu_dma_protection_show(struct device *dev,
255 					 struct device_attribute *attr,
256 					 char *buf)
257 {
258 	struct tb *tb = container_of(dev, struct tb, dev);
259 
260 	return sysfs_emit(buf, "%d\n", tb->nhi->iommu_dma_protection);
261 }
262 static DEVICE_ATTR_RO(iommu_dma_protection);
263 
security_show(struct device * dev,struct device_attribute * attr,char * buf)264 static ssize_t security_show(struct device *dev, struct device_attribute *attr,
265 			     char *buf)
266 {
267 	struct tb *tb = container_of(dev, struct tb, dev);
268 	const char *name = "unknown";
269 
270 	if (tb->security_level < ARRAY_SIZE(tb_security_names))
271 		name = tb_security_names[tb->security_level];
272 
273 	return sprintf(buf, "%s\n", name);
274 }
275 static DEVICE_ATTR_RO(security);
276 
277 static struct attribute *domain_attrs[] = {
278 	&dev_attr_boot_acl.attr,
279 	&dev_attr_deauthorization.attr,
280 	&dev_attr_iommu_dma_protection.attr,
281 	&dev_attr_security.attr,
282 	NULL,
283 };
284 
domain_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)285 static umode_t domain_attr_is_visible(struct kobject *kobj,
286 				      struct attribute *attr, int n)
287 {
288 	struct device *dev = kobj_to_dev(kobj);
289 	struct tb *tb = container_of(dev, struct tb, dev);
290 
291 	if (attr == &dev_attr_boot_acl.attr) {
292 		if (tb->nboot_acl &&
293 		    tb->cm_ops->get_boot_acl &&
294 		    tb->cm_ops->set_boot_acl)
295 			return attr->mode;
296 		return 0;
297 	}
298 
299 	return attr->mode;
300 }
301 
302 static const struct attribute_group domain_attr_group = {
303 	.is_visible = domain_attr_is_visible,
304 	.attrs = domain_attrs,
305 };
306 
307 static const struct attribute_group *domain_attr_groups[] = {
308 	&domain_attr_group,
309 	NULL,
310 };
311 
312 struct bus_type tb_bus_type = {
313 	.name = "thunderbolt",
314 	.match = tb_service_match,
315 	.probe = tb_service_probe,
316 	.remove = tb_service_remove,
317 	.shutdown = tb_service_shutdown,
318 };
319 
tb_domain_release(struct device * dev)320 static void tb_domain_release(struct device *dev)
321 {
322 	struct tb *tb = container_of(dev, struct tb, dev);
323 
324 	tb_ctl_free(tb->ctl);
325 	destroy_workqueue(tb->wq);
326 	ida_simple_remove(&tb_domain_ida, tb->index);
327 	mutex_destroy(&tb->lock);
328 	kfree(tb);
329 }
330 
331 struct device_type tb_domain_type = {
332 	.name = "thunderbolt_domain",
333 	.release = tb_domain_release,
334 };
335 
tb_domain_event_cb(void * data,enum tb_cfg_pkg_type type,const void * buf,size_t size)336 static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
337 			       const void *buf, size_t size)
338 {
339 	struct tb *tb = data;
340 
341 	if (!tb->cm_ops->handle_event) {
342 		tb_warn(tb, "domain does not have event handler\n");
343 		return true;
344 	}
345 
346 	switch (type) {
347 	case TB_CFG_PKG_XDOMAIN_REQ:
348 	case TB_CFG_PKG_XDOMAIN_RESP:
349 		if (tb_is_xdomain_enabled())
350 			return tb_xdomain_handle_request(tb, type, buf, size);
351 		break;
352 
353 	default:
354 		tb->cm_ops->handle_event(tb, type, buf, size);
355 	}
356 
357 	return true;
358 }
359 
360 /**
361  * tb_domain_alloc() - Allocate a domain
362  * @nhi: Pointer to the host controller
363  * @timeout_msec: Control channel timeout for non-raw messages
364  * @privsize: Size of the connection manager private data
365  *
366  * Allocates and initializes a new Thunderbolt domain. Connection
367  * managers are expected to call this and then fill in @cm_ops
368  * accordingly.
369  *
370  * Call tb_domain_put() to release the domain before it has been added
371  * to the system.
372  *
373  * Return: allocated domain structure on %NULL in case of error
374  */
tb_domain_alloc(struct tb_nhi * nhi,int timeout_msec,size_t privsize)375 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize)
376 {
377 	struct tb *tb;
378 
379 	/*
380 	 * Make sure the structure sizes map with that the hardware
381 	 * expects because bit-fields are being used.
382 	 */
383 	BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
384 	BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
385 	BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
386 
387 	tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
388 	if (!tb)
389 		return NULL;
390 
391 	tb->nhi = nhi;
392 	mutex_init(&tb->lock);
393 
394 	tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL);
395 	if (tb->index < 0)
396 		goto err_free;
397 
398 	tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
399 	if (!tb->wq)
400 		goto err_remove_ida;
401 
402 	tb->ctl = tb_ctl_alloc(nhi, timeout_msec, tb_domain_event_cb, tb);
403 	if (!tb->ctl)
404 		goto err_destroy_wq;
405 
406 	tb->dev.parent = &nhi->pdev->dev;
407 	tb->dev.bus = &tb_bus_type;
408 	tb->dev.type = &tb_domain_type;
409 	tb->dev.groups = domain_attr_groups;
410 	dev_set_name(&tb->dev, "domain%d", tb->index);
411 	device_initialize(&tb->dev);
412 
413 	return tb;
414 
415 err_destroy_wq:
416 	destroy_workqueue(tb->wq);
417 err_remove_ida:
418 	ida_simple_remove(&tb_domain_ida, tb->index);
419 err_free:
420 	kfree(tb);
421 
422 	return NULL;
423 }
424 
425 /**
426  * tb_domain_add() - Add domain to the system
427  * @tb: Domain to add
428  *
429  * Starts the domain and adds it to the system. Hotplugging devices will
430  * work after this has been returned successfully. In order to remove
431  * and release the domain after this function has been called, call
432  * tb_domain_remove().
433  *
434  * Return: %0 in case of success and negative errno in case of error
435  */
tb_domain_add(struct tb * tb)436 int tb_domain_add(struct tb *tb)
437 {
438 	int ret;
439 
440 	if (WARN_ON(!tb->cm_ops))
441 		return -EINVAL;
442 
443 	mutex_lock(&tb->lock);
444 	/*
445 	 * tb_schedule_hotplug_handler may be called as soon as the config
446 	 * channel is started. Thats why we have to hold the lock here.
447 	 */
448 	tb_ctl_start(tb->ctl);
449 
450 	if (tb->cm_ops->driver_ready) {
451 		ret = tb->cm_ops->driver_ready(tb);
452 		if (ret)
453 			goto err_ctl_stop;
454 	}
455 
456 	tb_dbg(tb, "security level set to %s\n",
457 	       tb_security_names[tb->security_level]);
458 
459 	ret = device_add(&tb->dev);
460 	if (ret)
461 		goto err_ctl_stop;
462 
463 	/* Start the domain */
464 	if (tb->cm_ops->start) {
465 		ret = tb->cm_ops->start(tb);
466 		if (ret)
467 			goto err_domain_del;
468 	}
469 
470 	/* This starts event processing */
471 	mutex_unlock(&tb->lock);
472 
473 	device_init_wakeup(&tb->dev, true);
474 
475 	pm_runtime_no_callbacks(&tb->dev);
476 	pm_runtime_set_active(&tb->dev);
477 	pm_runtime_enable(&tb->dev);
478 	pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY);
479 	pm_runtime_mark_last_busy(&tb->dev);
480 	pm_runtime_use_autosuspend(&tb->dev);
481 
482 	return 0;
483 
484 err_domain_del:
485 	device_del(&tb->dev);
486 err_ctl_stop:
487 	tb_ctl_stop(tb->ctl);
488 	mutex_unlock(&tb->lock);
489 
490 	return ret;
491 }
492 
493 /**
494  * tb_domain_remove() - Removes and releases a domain
495  * @tb: Domain to remove
496  *
497  * Stops the domain, removes it from the system and releases all
498  * resources once the last reference has been released.
499  */
tb_domain_remove(struct tb * tb)500 void tb_domain_remove(struct tb *tb)
501 {
502 	mutex_lock(&tb->lock);
503 	if (tb->cm_ops->stop)
504 		tb->cm_ops->stop(tb);
505 	/* Stop the domain control traffic */
506 	tb_ctl_stop(tb->ctl);
507 	mutex_unlock(&tb->lock);
508 
509 	flush_workqueue(tb->wq);
510 	device_unregister(&tb->dev);
511 }
512 
513 /**
514  * tb_domain_suspend_noirq() - Suspend a domain
515  * @tb: Domain to suspend
516  *
517  * Suspends all devices in the domain and stops the control channel.
518  */
tb_domain_suspend_noirq(struct tb * tb)519 int tb_domain_suspend_noirq(struct tb *tb)
520 {
521 	int ret = 0;
522 
523 	/*
524 	 * The control channel interrupt is left enabled during suspend
525 	 * and taking the lock here prevents any events happening before
526 	 * we actually have stopped the domain and the control channel.
527 	 */
528 	mutex_lock(&tb->lock);
529 	if (tb->cm_ops->suspend_noirq)
530 		ret = tb->cm_ops->suspend_noirq(tb);
531 	if (!ret)
532 		tb_ctl_stop(tb->ctl);
533 	mutex_unlock(&tb->lock);
534 
535 	return ret;
536 }
537 
538 /**
539  * tb_domain_resume_noirq() - Resume a domain
540  * @tb: Domain to resume
541  *
542  * Re-starts the control channel, and resumes all devices connected to
543  * the domain.
544  */
tb_domain_resume_noirq(struct tb * tb)545 int tb_domain_resume_noirq(struct tb *tb)
546 {
547 	int ret = 0;
548 
549 	mutex_lock(&tb->lock);
550 	tb_ctl_start(tb->ctl);
551 	if (tb->cm_ops->resume_noirq)
552 		ret = tb->cm_ops->resume_noirq(tb);
553 	mutex_unlock(&tb->lock);
554 
555 	return ret;
556 }
557 
tb_domain_suspend(struct tb * tb)558 int tb_domain_suspend(struct tb *tb)
559 {
560 	return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0;
561 }
562 
tb_domain_freeze_noirq(struct tb * tb)563 int tb_domain_freeze_noirq(struct tb *tb)
564 {
565 	int ret = 0;
566 
567 	mutex_lock(&tb->lock);
568 	if (tb->cm_ops->freeze_noirq)
569 		ret = tb->cm_ops->freeze_noirq(tb);
570 	if (!ret)
571 		tb_ctl_stop(tb->ctl);
572 	mutex_unlock(&tb->lock);
573 
574 	return ret;
575 }
576 
tb_domain_thaw_noirq(struct tb * tb)577 int tb_domain_thaw_noirq(struct tb *tb)
578 {
579 	int ret = 0;
580 
581 	mutex_lock(&tb->lock);
582 	tb_ctl_start(tb->ctl);
583 	if (tb->cm_ops->thaw_noirq)
584 		ret = tb->cm_ops->thaw_noirq(tb);
585 	mutex_unlock(&tb->lock);
586 
587 	return ret;
588 }
589 
tb_domain_complete(struct tb * tb)590 void tb_domain_complete(struct tb *tb)
591 {
592 	if (tb->cm_ops->complete)
593 		tb->cm_ops->complete(tb);
594 }
595 
tb_domain_runtime_suspend(struct tb * tb)596 int tb_domain_runtime_suspend(struct tb *tb)
597 {
598 	if (tb->cm_ops->runtime_suspend) {
599 		int ret = tb->cm_ops->runtime_suspend(tb);
600 		if (ret)
601 			return ret;
602 	}
603 	tb_ctl_stop(tb->ctl);
604 	return 0;
605 }
606 
tb_domain_runtime_resume(struct tb * tb)607 int tb_domain_runtime_resume(struct tb *tb)
608 {
609 	tb_ctl_start(tb->ctl);
610 	if (tb->cm_ops->runtime_resume) {
611 		int ret = tb->cm_ops->runtime_resume(tb);
612 		if (ret)
613 			return ret;
614 	}
615 	return 0;
616 }
617 
618 /**
619  * tb_domain_disapprove_switch() - Disapprove switch
620  * @tb: Domain the switch belongs to
621  * @sw: Switch to disapprove
622  *
623  * This will disconnect PCIe tunnel from parent to this @sw.
624  *
625  * Return: %0 on success and negative errno in case of failure.
626  */
tb_domain_disapprove_switch(struct tb * tb,struct tb_switch * sw)627 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw)
628 {
629 	if (!tb->cm_ops->disapprove_switch)
630 		return -EPERM;
631 
632 	return tb->cm_ops->disapprove_switch(tb, sw);
633 }
634 
635 /**
636  * tb_domain_approve_switch() - Approve switch
637  * @tb: Domain the switch belongs to
638  * @sw: Switch to approve
639  *
640  * This will approve switch by connection manager specific means. In
641  * case of success the connection manager will create PCIe tunnel from
642  * parent to @sw.
643  */
tb_domain_approve_switch(struct tb * tb,struct tb_switch * sw)644 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
645 {
646 	struct tb_switch *parent_sw;
647 
648 	if (!tb->cm_ops->approve_switch)
649 		return -EPERM;
650 
651 	/* The parent switch must be authorized before this one */
652 	parent_sw = tb_to_switch(sw->dev.parent);
653 	if (!parent_sw || !parent_sw->authorized)
654 		return -EINVAL;
655 
656 	return tb->cm_ops->approve_switch(tb, sw);
657 }
658 
659 /**
660  * tb_domain_approve_switch_key() - Approve switch and add key
661  * @tb: Domain the switch belongs to
662  * @sw: Switch to approve
663  *
664  * For switches that support secure connect, this function first adds
665  * key to the switch NVM using connection manager specific means. If
666  * adding the key is successful, the switch is approved and connected.
667  *
668  * Return: %0 on success and negative errno in case of failure.
669  */
tb_domain_approve_switch_key(struct tb * tb,struct tb_switch * sw)670 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
671 {
672 	struct tb_switch *parent_sw;
673 	int ret;
674 
675 	if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
676 		return -EPERM;
677 
678 	/* The parent switch must be authorized before this one */
679 	parent_sw = tb_to_switch(sw->dev.parent);
680 	if (!parent_sw || !parent_sw->authorized)
681 		return -EINVAL;
682 
683 	ret = tb->cm_ops->add_switch_key(tb, sw);
684 	if (ret)
685 		return ret;
686 
687 	return tb->cm_ops->approve_switch(tb, sw);
688 }
689 
690 /**
691  * tb_domain_challenge_switch_key() - Challenge and approve switch
692  * @tb: Domain the switch belongs to
693  * @sw: Switch to approve
694  *
695  * For switches that support secure connect, this function generates
696  * random challenge and sends it to the switch. The switch responds to
697  * this and if the response matches our random challenge, the switch is
698  * approved and connected.
699  *
700  * Return: %0 on success and negative errno in case of failure.
701  */
tb_domain_challenge_switch_key(struct tb * tb,struct tb_switch * sw)702 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
703 {
704 	u8 challenge[TB_SWITCH_KEY_SIZE];
705 	u8 response[TB_SWITCH_KEY_SIZE];
706 	u8 hmac[TB_SWITCH_KEY_SIZE];
707 	struct tb_switch *parent_sw;
708 	struct crypto_shash *tfm;
709 	struct shash_desc *shash;
710 	int ret;
711 
712 	if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
713 		return -EPERM;
714 
715 	/* The parent switch must be authorized before this one */
716 	parent_sw = tb_to_switch(sw->dev.parent);
717 	if (!parent_sw || !parent_sw->authorized)
718 		return -EINVAL;
719 
720 	get_random_bytes(challenge, sizeof(challenge));
721 	ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
722 	if (ret)
723 		return ret;
724 
725 	tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
726 	if (IS_ERR(tfm))
727 		return PTR_ERR(tfm);
728 
729 	ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
730 	if (ret)
731 		goto err_free_tfm;
732 
733 	shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
734 			GFP_KERNEL);
735 	if (!shash) {
736 		ret = -ENOMEM;
737 		goto err_free_tfm;
738 	}
739 
740 	shash->tfm = tfm;
741 
742 	memset(hmac, 0, sizeof(hmac));
743 	ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
744 	if (ret)
745 		goto err_free_shash;
746 
747 	/* The returned HMAC must match the one we calculated */
748 	if (memcmp(response, hmac, sizeof(hmac))) {
749 		ret = -EKEYREJECTED;
750 		goto err_free_shash;
751 	}
752 
753 	crypto_free_shash(tfm);
754 	kfree(shash);
755 
756 	return tb->cm_ops->approve_switch(tb, sw);
757 
758 err_free_shash:
759 	kfree(shash);
760 err_free_tfm:
761 	crypto_free_shash(tfm);
762 
763 	return ret;
764 }
765 
766 /**
767  * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
768  * @tb: Domain whose PCIe paths to disconnect
769  *
770  * This needs to be called in preparation for NVM upgrade of the host
771  * controller. Makes sure all PCIe paths are disconnected.
772  *
773  * Return %0 on success and negative errno in case of error.
774  */
tb_domain_disconnect_pcie_paths(struct tb * tb)775 int tb_domain_disconnect_pcie_paths(struct tb *tb)
776 {
777 	if (!tb->cm_ops->disconnect_pcie_paths)
778 		return -EPERM;
779 
780 	return tb->cm_ops->disconnect_pcie_paths(tb);
781 }
782 
783 /**
784  * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
785  * @tb: Domain enabling the DMA paths
786  * @xd: XDomain DMA paths are created to
787  * @transmit_path: HopID we are using to send out packets
788  * @transmit_ring: DMA ring used to send out packets
789  * @receive_path: HopID the other end is using to send packets to us
790  * @receive_ring: DMA ring used to receive packets from @receive_path
791  *
792  * Calls connection manager specific method to enable DMA paths to the
793  * XDomain in question.
794  *
795  * Return: 0% in case of success and negative errno otherwise. In
796  * particular returns %-ENOTSUPP if the connection manager
797  * implementation does not support XDomains.
798  */
tb_domain_approve_xdomain_paths(struct tb * tb,struct tb_xdomain * xd,int transmit_path,int transmit_ring,int receive_path,int receive_ring)799 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
800 				    int transmit_path, int transmit_ring,
801 				    int receive_path, int receive_ring)
802 {
803 	if (!tb->cm_ops->approve_xdomain_paths)
804 		return -ENOTSUPP;
805 
806 	return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path,
807 			transmit_ring, receive_path, receive_ring);
808 }
809 
810 /**
811  * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
812  * @tb: Domain disabling the DMA paths
813  * @xd: XDomain whose DMA paths are disconnected
814  * @transmit_path: HopID we are using to send out packets
815  * @transmit_ring: DMA ring used to send out packets
816  * @receive_path: HopID the other end is using to send packets to us
817  * @receive_ring: DMA ring used to receive packets from @receive_path
818  *
819  * Calls connection manager specific method to disconnect DMA paths to
820  * the XDomain in question.
821  *
822  * Return: 0% in case of success and negative errno otherwise. In
823  * particular returns %-ENOTSUPP if the connection manager
824  * implementation does not support XDomains.
825  */
tb_domain_disconnect_xdomain_paths(struct tb * tb,struct tb_xdomain * xd,int transmit_path,int transmit_ring,int receive_path,int receive_ring)826 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
827 				       int transmit_path, int transmit_ring,
828 				       int receive_path, int receive_ring)
829 {
830 	if (!tb->cm_ops->disconnect_xdomain_paths)
831 		return -ENOTSUPP;
832 
833 	return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path,
834 			transmit_ring, receive_path, receive_ring);
835 }
836 
disconnect_xdomain(struct device * dev,void * data)837 static int disconnect_xdomain(struct device *dev, void *data)
838 {
839 	struct tb_xdomain *xd;
840 	struct tb *tb = data;
841 	int ret = 0;
842 
843 	xd = tb_to_xdomain(dev);
844 	if (xd && xd->tb == tb)
845 		ret = tb_xdomain_disable_all_paths(xd);
846 
847 	return ret;
848 }
849 
850 /**
851  * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
852  * @tb: Domain whose paths are disconnected
853  *
854  * This function can be used to disconnect all paths (PCIe, XDomain) for
855  * example in preparation for host NVM firmware upgrade. After this is
856  * called the paths cannot be established without resetting the switch.
857  *
858  * Return: %0 in case of success and negative errno otherwise.
859  */
tb_domain_disconnect_all_paths(struct tb * tb)860 int tb_domain_disconnect_all_paths(struct tb *tb)
861 {
862 	int ret;
863 
864 	ret = tb_domain_disconnect_pcie_paths(tb);
865 	if (ret)
866 		return ret;
867 
868 	return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
869 }
870 
tb_domain_init(void)871 int tb_domain_init(void)
872 {
873 	int ret;
874 
875 	tb_test_init();
876 	tb_debugfs_init();
877 	tb_acpi_init();
878 
879 	ret = tb_xdomain_init();
880 	if (ret)
881 		goto err_acpi;
882 	ret = bus_register(&tb_bus_type);
883 	if (ret)
884 		goto err_xdomain;
885 
886 	return 0;
887 
888 err_xdomain:
889 	tb_xdomain_exit();
890 err_acpi:
891 	tb_acpi_exit();
892 	tb_debugfs_exit();
893 	tb_test_exit();
894 
895 	return ret;
896 }
897 
tb_domain_exit(void)898 void tb_domain_exit(void)
899 {
900 	bus_unregister(&tb_bus_type);
901 	ida_destroy(&tb_domain_ida);
902 	tb_nvm_exit();
903 	tb_xdomain_exit();
904 	tb_acpi_exit();
905 	tb_debugfs_exit();
906 	tb_test_exit();
907 }
908