Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Thunderbolt bus support |
| 3 | * |
| 4 | * Copyright (C) 2017, Intel Corporation |
| 5 | * Author: Mika Westerberg <mika.westerberg@linux.intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/idr.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 16 | #include <linux/random.h> |
| 17 | #include <crypto/hash.h> |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 18 | |
| 19 | #include "tb.h" |
| 20 | |
| 21 | static DEFINE_IDA(tb_domain_ida); |
| 22 | |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 23 | static bool match_service_id(const struct tb_service_id *id, |
| 24 | const struct tb_service *svc) |
| 25 | { |
| 26 | if (id->match_flags & TBSVC_MATCH_PROTOCOL_KEY) { |
| 27 | if (strcmp(id->protocol_key, svc->key)) |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) { |
| 32 | if (id->protocol_id != svc->prtcid) |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) { |
| 37 | if (id->protocol_version != svc->prtcvers) |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) { |
| 42 | if (id->protocol_revision != svc->prtcrevs) |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | static const struct tb_service_id *__tb_service_match(struct device *dev, |
| 50 | struct device_driver *drv) |
| 51 | { |
| 52 | struct tb_service_driver *driver; |
| 53 | const struct tb_service_id *ids; |
| 54 | struct tb_service *svc; |
| 55 | |
| 56 | svc = tb_to_service(dev); |
| 57 | if (!svc) |
| 58 | return NULL; |
| 59 | |
| 60 | driver = container_of(drv, struct tb_service_driver, driver); |
| 61 | if (!driver->id_table) |
| 62 | return NULL; |
| 63 | |
| 64 | for (ids = driver->id_table; ids->match_flags != 0; ids++) { |
| 65 | if (match_service_id(ids, svc)) |
| 66 | return ids; |
| 67 | } |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | static int tb_service_match(struct device *dev, struct device_driver *drv) |
| 73 | { |
| 74 | return !!__tb_service_match(dev, drv); |
| 75 | } |
| 76 | |
| 77 | static int tb_service_probe(struct device *dev) |
| 78 | { |
| 79 | struct tb_service *svc = tb_to_service(dev); |
| 80 | struct tb_service_driver *driver; |
| 81 | const struct tb_service_id *id; |
| 82 | |
| 83 | driver = container_of(dev->driver, struct tb_service_driver, driver); |
| 84 | id = __tb_service_match(dev, &driver->driver); |
| 85 | |
| 86 | return driver->probe(svc, id); |
| 87 | } |
| 88 | |
| 89 | static int tb_service_remove(struct device *dev) |
| 90 | { |
| 91 | struct tb_service *svc = tb_to_service(dev); |
| 92 | struct tb_service_driver *driver; |
| 93 | |
| 94 | driver = container_of(dev->driver, struct tb_service_driver, driver); |
| 95 | if (driver->remove) |
| 96 | driver->remove(svc); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void tb_service_shutdown(struct device *dev) |
| 102 | { |
| 103 | struct tb_service_driver *driver; |
| 104 | struct tb_service *svc; |
| 105 | |
| 106 | svc = tb_to_service(dev); |
| 107 | if (!svc || !dev->driver) |
| 108 | return; |
| 109 | |
| 110 | driver = container_of(dev->driver, struct tb_service_driver, driver); |
| 111 | if (driver->shutdown) |
| 112 | driver->shutdown(svc); |
| 113 | } |
| 114 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 115 | static const char * const tb_security_names[] = { |
| 116 | [TB_SECURITY_NONE] = "none", |
| 117 | [TB_SECURITY_USER] = "user", |
| 118 | [TB_SECURITY_SECURE] = "secure", |
| 119 | [TB_SECURITY_DPONLY] = "dponly", |
Mika Westerberg | 6fc14e1 | 2017-12-08 14:11:39 +0300 | [diff] [blame] | 120 | [TB_SECURITY_USBONLY] = "usbonly", |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 121 | }; |
| 122 | |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame] | 123 | static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr, |
| 124 | char *buf) |
| 125 | { |
| 126 | struct tb *tb = container_of(dev, struct tb, dev); |
| 127 | uuid_t *uuids; |
| 128 | ssize_t ret; |
| 129 | int i; |
| 130 | |
| 131 | uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL); |
| 132 | if (!uuids) |
| 133 | return -ENOMEM; |
| 134 | |
| 135 | if (mutex_lock_interruptible(&tb->lock)) { |
| 136 | ret = -ERESTARTSYS; |
| 137 | goto out; |
| 138 | } |
| 139 | ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl); |
| 140 | if (ret) { |
| 141 | mutex_unlock(&tb->lock); |
| 142 | goto out; |
| 143 | } |
| 144 | mutex_unlock(&tb->lock); |
| 145 | |
| 146 | for (ret = 0, i = 0; i < tb->nboot_acl; i++) { |
| 147 | if (!uuid_is_null(&uuids[i])) |
| 148 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "%pUb", |
| 149 | &uuids[i]); |
| 150 | |
| 151 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s", |
| 152 | i < tb->nboot_acl - 1 ? "," : "\n"); |
| 153 | } |
| 154 | |
| 155 | out: |
| 156 | kfree(uuids); |
| 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 | if (mutex_lock_interruptible(&tb->lock)) { |
| 212 | ret = -ERESTARTSYS; |
| 213 | goto err_free_acl; |
| 214 | } |
| 215 | ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl); |
Mika Westerberg | 007a749 | 2018-06-26 14:46:35 +0300 | [diff] [blame] | 216 | if (!ret) { |
| 217 | /* Notify userspace about the change */ |
| 218 | kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE); |
| 219 | } |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame] | 220 | mutex_unlock(&tb->lock); |
| 221 | |
| 222 | err_free_acl: |
| 223 | kfree(acl); |
| 224 | err_free_str: |
| 225 | kfree(str); |
| 226 | |
| 227 | return ret ?: count; |
| 228 | } |
| 229 | static DEVICE_ATTR_RW(boot_acl); |
| 230 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 231 | static ssize_t security_show(struct device *dev, struct device_attribute *attr, |
| 232 | char *buf) |
| 233 | { |
| 234 | struct tb *tb = container_of(dev, struct tb, dev); |
Mika Westerberg | 6fc14e1 | 2017-12-08 14:11:39 +0300 | [diff] [blame] | 235 | const char *name = "unknown"; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 236 | |
Mika Westerberg | 6fc14e1 | 2017-12-08 14:11:39 +0300 | [diff] [blame] | 237 | if (tb->security_level < ARRAY_SIZE(tb_security_names)) |
| 238 | name = tb_security_names[tb->security_level]; |
| 239 | |
| 240 | return sprintf(buf, "%s\n", name); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 241 | } |
| 242 | static DEVICE_ATTR_RO(security); |
| 243 | |
| 244 | static struct attribute *domain_attrs[] = { |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame] | 245 | &dev_attr_boot_acl.attr, |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 246 | &dev_attr_security.attr, |
| 247 | NULL, |
| 248 | }; |
| 249 | |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame] | 250 | static umode_t domain_attr_is_visible(struct kobject *kobj, |
| 251 | struct attribute *attr, int n) |
| 252 | { |
| 253 | struct device *dev = container_of(kobj, struct device, kobj); |
| 254 | struct tb *tb = container_of(dev, struct tb, dev); |
| 255 | |
| 256 | if (attr == &dev_attr_boot_acl.attr) { |
| 257 | if (tb->nboot_acl && |
| 258 | tb->cm_ops->get_boot_acl && |
| 259 | tb->cm_ops->set_boot_acl) |
| 260 | return attr->mode; |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | return attr->mode; |
| 265 | } |
| 266 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 267 | static struct attribute_group domain_attr_group = { |
Mika Westerberg | 9aaa3b8 | 2018-01-21 12:08:04 +0200 | [diff] [blame] | 268 | .is_visible = domain_attr_is_visible, |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 269 | .attrs = domain_attrs, |
| 270 | }; |
| 271 | |
| 272 | static const struct attribute_group *domain_attr_groups[] = { |
| 273 | &domain_attr_group, |
| 274 | NULL, |
| 275 | }; |
| 276 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 277 | struct bus_type tb_bus_type = { |
| 278 | .name = "thunderbolt", |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 279 | .match = tb_service_match, |
| 280 | .probe = tb_service_probe, |
| 281 | .remove = tb_service_remove, |
| 282 | .shutdown = tb_service_shutdown, |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | static void tb_domain_release(struct device *dev) |
| 286 | { |
| 287 | struct tb *tb = container_of(dev, struct tb, dev); |
| 288 | |
| 289 | tb_ctl_free(tb->ctl); |
| 290 | destroy_workqueue(tb->wq); |
| 291 | ida_simple_remove(&tb_domain_ida, tb->index); |
| 292 | mutex_destroy(&tb->lock); |
| 293 | kfree(tb); |
| 294 | } |
| 295 | |
| 296 | struct device_type tb_domain_type = { |
| 297 | .name = "thunderbolt_domain", |
| 298 | .release = tb_domain_release, |
| 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * tb_domain_alloc() - Allocate a domain |
| 303 | * @nhi: Pointer to the host controller |
| 304 | * @privsize: Size of the connection manager private data |
| 305 | * |
| 306 | * Allocates and initializes a new Thunderbolt domain. Connection |
| 307 | * managers are expected to call this and then fill in @cm_ops |
| 308 | * accordingly. |
| 309 | * |
| 310 | * Call tb_domain_put() to release the domain before it has been added |
| 311 | * to the system. |
| 312 | * |
| 313 | * Return: allocated domain structure on %NULL in case of error |
| 314 | */ |
| 315 | struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize) |
| 316 | { |
| 317 | struct tb *tb; |
| 318 | |
| 319 | /* |
| 320 | * Make sure the structure sizes map with that the hardware |
| 321 | * expects because bit-fields are being used. |
| 322 | */ |
| 323 | BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4); |
| 324 | BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4); |
| 325 | BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4); |
| 326 | |
| 327 | tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL); |
| 328 | if (!tb) |
| 329 | return NULL; |
| 330 | |
| 331 | tb->nhi = nhi; |
| 332 | mutex_init(&tb->lock); |
| 333 | |
| 334 | tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL); |
| 335 | if (tb->index < 0) |
| 336 | goto err_free; |
| 337 | |
| 338 | tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index); |
| 339 | if (!tb->wq) |
| 340 | goto err_remove_ida; |
| 341 | |
| 342 | tb->dev.parent = &nhi->pdev->dev; |
| 343 | tb->dev.bus = &tb_bus_type; |
| 344 | tb->dev.type = &tb_domain_type; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 345 | tb->dev.groups = domain_attr_groups; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 346 | dev_set_name(&tb->dev, "domain%d", tb->index); |
| 347 | device_initialize(&tb->dev); |
| 348 | |
| 349 | return tb; |
| 350 | |
| 351 | err_remove_ida: |
| 352 | ida_simple_remove(&tb_domain_ida, tb->index); |
| 353 | err_free: |
| 354 | kfree(tb); |
| 355 | |
| 356 | return NULL; |
| 357 | } |
| 358 | |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 359 | static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 360 | const void *buf, size_t size) |
| 361 | { |
| 362 | struct tb *tb = data; |
| 363 | |
| 364 | if (!tb->cm_ops->handle_event) { |
| 365 | tb_warn(tb, "domain does not have event handler\n"); |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 366 | return true; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 367 | } |
| 368 | |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 369 | switch (type) { |
| 370 | case TB_CFG_PKG_XDOMAIN_REQ: |
| 371 | case TB_CFG_PKG_XDOMAIN_RESP: |
| 372 | return tb_xdomain_handle_request(tb, type, buf, size); |
| 373 | |
| 374 | default: |
| 375 | tb->cm_ops->handle_event(tb, type, buf, size); |
| 376 | } |
| 377 | |
| 378 | return true; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 379 | } |
| 380 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 381 | /** |
| 382 | * tb_domain_add() - Add domain to the system |
| 383 | * @tb: Domain to add |
| 384 | * |
| 385 | * Starts the domain and adds it to the system. Hotplugging devices will |
| 386 | * work after this has been returned successfully. In order to remove |
| 387 | * and release the domain after this function has been called, call |
| 388 | * tb_domain_remove(). |
| 389 | * |
| 390 | * Return: %0 in case of success and negative errno in case of error |
| 391 | */ |
| 392 | int tb_domain_add(struct tb *tb) |
| 393 | { |
| 394 | int ret; |
| 395 | |
| 396 | if (WARN_ON(!tb->cm_ops)) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | mutex_lock(&tb->lock); |
| 400 | |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 401 | tb->ctl = tb_ctl_alloc(tb->nhi, tb_domain_event_cb, tb); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 402 | if (!tb->ctl) { |
| 403 | ret = -ENOMEM; |
| 404 | goto err_unlock; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * tb_schedule_hotplug_handler may be called as soon as the config |
| 409 | * channel is started. Thats why we have to hold the lock here. |
| 410 | */ |
| 411 | tb_ctl_start(tb->ctl); |
| 412 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 413 | if (tb->cm_ops->driver_ready) { |
| 414 | ret = tb->cm_ops->driver_ready(tb); |
| 415 | if (ret) |
| 416 | goto err_ctl_stop; |
| 417 | } |
| 418 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 419 | ret = device_add(&tb->dev); |
| 420 | if (ret) |
| 421 | goto err_ctl_stop; |
| 422 | |
| 423 | /* Start the domain */ |
| 424 | if (tb->cm_ops->start) { |
| 425 | ret = tb->cm_ops->start(tb); |
| 426 | if (ret) |
| 427 | goto err_domain_del; |
| 428 | } |
| 429 | |
| 430 | /* This starts event processing */ |
| 431 | mutex_unlock(&tb->lock); |
| 432 | |
| 433 | return 0; |
| 434 | |
| 435 | err_domain_del: |
| 436 | device_del(&tb->dev); |
| 437 | err_ctl_stop: |
| 438 | tb_ctl_stop(tb->ctl); |
| 439 | err_unlock: |
| 440 | mutex_unlock(&tb->lock); |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * tb_domain_remove() - Removes and releases a domain |
| 447 | * @tb: Domain to remove |
| 448 | * |
| 449 | * Stops the domain, removes it from the system and releases all |
| 450 | * resources once the last reference has been released. |
| 451 | */ |
| 452 | void tb_domain_remove(struct tb *tb) |
| 453 | { |
| 454 | mutex_lock(&tb->lock); |
| 455 | if (tb->cm_ops->stop) |
| 456 | tb->cm_ops->stop(tb); |
| 457 | /* Stop the domain control traffic */ |
| 458 | tb_ctl_stop(tb->ctl); |
| 459 | mutex_unlock(&tb->lock); |
| 460 | |
| 461 | flush_workqueue(tb->wq); |
| 462 | device_unregister(&tb->dev); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * tb_domain_suspend_noirq() - Suspend a domain |
| 467 | * @tb: Domain to suspend |
| 468 | * |
| 469 | * Suspends all devices in the domain and stops the control channel. |
| 470 | */ |
| 471 | int tb_domain_suspend_noirq(struct tb *tb) |
| 472 | { |
| 473 | int ret = 0; |
| 474 | |
| 475 | /* |
| 476 | * The control channel interrupt is left enabled during suspend |
| 477 | * and taking the lock here prevents any events happening before |
| 478 | * we actually have stopped the domain and the control channel. |
| 479 | */ |
| 480 | mutex_lock(&tb->lock); |
| 481 | if (tb->cm_ops->suspend_noirq) |
| 482 | ret = tb->cm_ops->suspend_noirq(tb); |
| 483 | if (!ret) |
| 484 | tb_ctl_stop(tb->ctl); |
| 485 | mutex_unlock(&tb->lock); |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * tb_domain_resume_noirq() - Resume a domain |
| 492 | * @tb: Domain to resume |
| 493 | * |
| 494 | * Re-starts the control channel, and resumes all devices connected to |
| 495 | * the domain. |
| 496 | */ |
| 497 | int tb_domain_resume_noirq(struct tb *tb) |
| 498 | { |
| 499 | int ret = 0; |
| 500 | |
| 501 | mutex_lock(&tb->lock); |
| 502 | tb_ctl_start(tb->ctl); |
| 503 | if (tb->cm_ops->resume_noirq) |
| 504 | ret = tb->cm_ops->resume_noirq(tb); |
| 505 | mutex_unlock(&tb->lock); |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 510 | int tb_domain_suspend(struct tb *tb) |
| 511 | { |
Mika Westerberg | 84db685 | 2018-07-25 11:03:18 +0300 | [diff] [blame^] | 512 | return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void tb_domain_complete(struct tb *tb) |
| 516 | { |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 517 | if (tb->cm_ops->complete) |
| 518 | tb->cm_ops->complete(tb); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /** |
| 522 | * tb_domain_approve_switch() - Approve switch |
| 523 | * @tb: Domain the switch belongs to |
| 524 | * @sw: Switch to approve |
| 525 | * |
| 526 | * This will approve switch by connection manager specific means. In |
| 527 | * case of success the connection manager will create tunnels for all |
| 528 | * supported protocols. |
| 529 | */ |
| 530 | int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw) |
| 531 | { |
| 532 | struct tb_switch *parent_sw; |
| 533 | |
| 534 | if (!tb->cm_ops->approve_switch) |
| 535 | return -EPERM; |
| 536 | |
| 537 | /* The parent switch must be authorized before this one */ |
| 538 | parent_sw = tb_to_switch(sw->dev.parent); |
| 539 | if (!parent_sw || !parent_sw->authorized) |
| 540 | return -EINVAL; |
| 541 | |
| 542 | return tb->cm_ops->approve_switch(tb, sw); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * tb_domain_approve_switch_key() - Approve switch and add key |
| 547 | * @tb: Domain the switch belongs to |
| 548 | * @sw: Switch to approve |
| 549 | * |
| 550 | * For switches that support secure connect, this function first adds |
| 551 | * key to the switch NVM using connection manager specific means. If |
| 552 | * adding the key is successful, the switch is approved and connected. |
| 553 | * |
| 554 | * Return: %0 on success and negative errno in case of failure. |
| 555 | */ |
| 556 | int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw) |
| 557 | { |
| 558 | struct tb_switch *parent_sw; |
| 559 | int ret; |
| 560 | |
| 561 | if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key) |
| 562 | return -EPERM; |
| 563 | |
| 564 | /* The parent switch must be authorized before this one */ |
| 565 | parent_sw = tb_to_switch(sw->dev.parent); |
| 566 | if (!parent_sw || !parent_sw->authorized) |
| 567 | return -EINVAL; |
| 568 | |
| 569 | ret = tb->cm_ops->add_switch_key(tb, sw); |
| 570 | if (ret) |
| 571 | return ret; |
| 572 | |
| 573 | return tb->cm_ops->approve_switch(tb, sw); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * tb_domain_challenge_switch_key() - Challenge and approve switch |
| 578 | * @tb: Domain the switch belongs to |
| 579 | * @sw: Switch to approve |
| 580 | * |
| 581 | * For switches that support secure connect, this function generates |
| 582 | * random challenge and sends it to the switch. The switch responds to |
| 583 | * this and if the response matches our random challenge, the switch is |
| 584 | * approved and connected. |
| 585 | * |
| 586 | * Return: %0 on success and negative errno in case of failure. |
| 587 | */ |
| 588 | int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw) |
| 589 | { |
| 590 | u8 challenge[TB_SWITCH_KEY_SIZE]; |
| 591 | u8 response[TB_SWITCH_KEY_SIZE]; |
| 592 | u8 hmac[TB_SWITCH_KEY_SIZE]; |
| 593 | struct tb_switch *parent_sw; |
| 594 | struct crypto_shash *tfm; |
| 595 | struct shash_desc *shash; |
| 596 | int ret; |
| 597 | |
| 598 | if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key) |
| 599 | return -EPERM; |
| 600 | |
| 601 | /* The parent switch must be authorized before this one */ |
| 602 | parent_sw = tb_to_switch(sw->dev.parent); |
| 603 | if (!parent_sw || !parent_sw->authorized) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | get_random_bytes(challenge, sizeof(challenge)); |
| 607 | ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response); |
| 608 | if (ret) |
| 609 | return ret; |
| 610 | |
| 611 | tfm = crypto_alloc_shash("hmac(sha256)", 0, 0); |
| 612 | if (IS_ERR(tfm)) |
| 613 | return PTR_ERR(tfm); |
| 614 | |
| 615 | ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE); |
| 616 | if (ret) |
| 617 | goto err_free_tfm; |
| 618 | |
| 619 | shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm), |
| 620 | GFP_KERNEL); |
| 621 | if (!shash) { |
| 622 | ret = -ENOMEM; |
| 623 | goto err_free_tfm; |
| 624 | } |
| 625 | |
| 626 | shash->tfm = tfm; |
| 627 | shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 628 | |
| 629 | memset(hmac, 0, sizeof(hmac)); |
| 630 | ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac); |
| 631 | if (ret) |
| 632 | goto err_free_shash; |
| 633 | |
| 634 | /* The returned HMAC must match the one we calculated */ |
| 635 | if (memcmp(response, hmac, sizeof(hmac))) { |
| 636 | ret = -EKEYREJECTED; |
| 637 | goto err_free_shash; |
| 638 | } |
| 639 | |
| 640 | crypto_free_shash(tfm); |
| 641 | kfree(shash); |
| 642 | |
| 643 | return tb->cm_ops->approve_switch(tb, sw); |
| 644 | |
| 645 | err_free_shash: |
| 646 | kfree(shash); |
| 647 | err_free_tfm: |
| 648 | crypto_free_shash(tfm); |
| 649 | |
| 650 | return ret; |
| 651 | } |
| 652 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 653 | /** |
| 654 | * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths |
| 655 | * @tb: Domain whose PCIe paths to disconnect |
| 656 | * |
| 657 | * This needs to be called in preparation for NVM upgrade of the host |
| 658 | * controller. Makes sure all PCIe paths are disconnected. |
| 659 | * |
| 660 | * Return %0 on success and negative errno in case of error. |
| 661 | */ |
| 662 | int tb_domain_disconnect_pcie_paths(struct tb *tb) |
| 663 | { |
| 664 | if (!tb->cm_ops->disconnect_pcie_paths) |
| 665 | return -EPERM; |
| 666 | |
| 667 | return tb->cm_ops->disconnect_pcie_paths(tb); |
| 668 | } |
| 669 | |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 670 | /** |
| 671 | * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain |
| 672 | * @tb: Domain enabling the DMA paths |
| 673 | * @xd: XDomain DMA paths are created to |
| 674 | * |
| 675 | * Calls connection manager specific method to enable DMA paths to the |
| 676 | * XDomain in question. |
| 677 | * |
| 678 | * Return: 0% in case of success and negative errno otherwise. In |
| 679 | * particular returns %-ENOTSUPP if the connection manager |
| 680 | * implementation does not support XDomains. |
| 681 | */ |
| 682 | int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) |
| 683 | { |
| 684 | if (!tb->cm_ops->approve_xdomain_paths) |
| 685 | return -ENOTSUPP; |
| 686 | |
| 687 | return tb->cm_ops->approve_xdomain_paths(tb, xd); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain |
| 692 | * @tb: Domain disabling the DMA paths |
| 693 | * @xd: XDomain whose DMA paths are disconnected |
| 694 | * |
| 695 | * Calls connection manager specific method to disconnect DMA paths to |
| 696 | * the XDomain in question. |
| 697 | * |
| 698 | * Return: 0% in case of success and negative errno otherwise. In |
| 699 | * particular returns %-ENOTSUPP if the connection manager |
| 700 | * implementation does not support XDomains. |
| 701 | */ |
| 702 | int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) |
| 703 | { |
| 704 | if (!tb->cm_ops->disconnect_xdomain_paths) |
| 705 | return -ENOTSUPP; |
| 706 | |
| 707 | return tb->cm_ops->disconnect_xdomain_paths(tb, xd); |
| 708 | } |
| 709 | |
| 710 | static int disconnect_xdomain(struct device *dev, void *data) |
| 711 | { |
| 712 | struct tb_xdomain *xd; |
| 713 | struct tb *tb = data; |
| 714 | int ret = 0; |
| 715 | |
| 716 | xd = tb_to_xdomain(dev); |
| 717 | if (xd && xd->tb == tb) |
| 718 | ret = tb_xdomain_disable_paths(xd); |
| 719 | |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain |
| 725 | * @tb: Domain whose paths are disconnected |
| 726 | * |
| 727 | * This function can be used to disconnect all paths (PCIe, XDomain) for |
| 728 | * example in preparation for host NVM firmware upgrade. After this is |
| 729 | * called the paths cannot be established without resetting the switch. |
| 730 | * |
| 731 | * Return: %0 in case of success and negative errno otherwise. |
| 732 | */ |
| 733 | int tb_domain_disconnect_all_paths(struct tb *tb) |
| 734 | { |
| 735 | int ret; |
| 736 | |
| 737 | ret = tb_domain_disconnect_pcie_paths(tb); |
| 738 | if (ret) |
| 739 | return ret; |
| 740 | |
| 741 | return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain); |
| 742 | } |
| 743 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 744 | int tb_domain_init(void) |
| 745 | { |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 746 | int ret; |
| 747 | |
| 748 | ret = tb_xdomain_init(); |
| 749 | if (ret) |
| 750 | return ret; |
| 751 | ret = bus_register(&tb_bus_type); |
| 752 | if (ret) |
| 753 | tb_xdomain_exit(); |
| 754 | |
| 755 | return ret; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | void tb_domain_exit(void) |
| 759 | { |
| 760 | bus_unregister(&tb_bus_type); |
| 761 | ida_destroy(&tb_domain_ida); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 762 | tb_switch_exit(); |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 763 | tb_xdomain_exit(); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 764 | } |