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