blob: e2fc4543142d5c9037184db959545a088b3af3b3 [file] [log] [blame]
Mika Westerbergfd3b3392018-10-01 12:31:21 +03001// SPDX-License-Identifier: GPL-2.0
Mika Westerbergd1ff7022017-10-02 13:38:34 +03002/*
3 * Thunderbolt XDomain discovery protocol support
4 *
5 * Copyright (C) 2017, Intel Corporation
6 * Authors: Michael Jamet <michael.jamet@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
Mika Westerbergd1ff7022017-10-02 13:38:34 +03008 */
9
10#include <linux/device.h>
11#include <linux/kmod.h>
12#include <linux/module.h>
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030013#include <linux/pm_runtime.h>
Mika Westerbergd1ff7022017-10-02 13:38:34 +030014#include <linux/utsname.h>
15#include <linux/uuid.h>
16#include <linux/workqueue.h>
17
18#include "tb.h"
19
20#define XDOMAIN_DEFAULT_TIMEOUT 5000 /* ms */
21#define XDOMAIN_PROPERTIES_RETRIES 60
22#define XDOMAIN_PROPERTIES_CHANGED_RETRIES 10
23
24struct xdomain_request_work {
25 struct work_struct work;
26 struct tb_xdp_header *pkg;
27 struct tb *tb;
28};
29
30/* Serializes access to the properties and protocol handlers below */
31static DEFINE_MUTEX(xdomain_lock);
32
33/* Properties exposed to the remote domains */
34static struct tb_property_dir *xdomain_property_dir;
35static u32 *xdomain_property_block;
36static u32 xdomain_property_block_len;
37static u32 xdomain_property_block_gen;
38
39/* Additional protocol handlers */
40static LIST_HEAD(protocol_handlers);
41
42/* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
43static const uuid_t tb_xdp_uuid =
44 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
45 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
46
47static bool tb_xdomain_match(const struct tb_cfg_request *req,
48 const struct ctl_pkg *pkg)
49{
50 switch (pkg->frame.eof) {
51 case TB_CFG_PKG_ERROR:
52 return true;
53
54 case TB_CFG_PKG_XDOMAIN_RESP: {
55 const struct tb_xdp_header *res_hdr = pkg->buffer;
56 const struct tb_xdp_header *req_hdr = req->request;
Mika Westerbergd1ff7022017-10-02 13:38:34 +030057
58 if (pkg->frame.size < req->response_size / 4)
59 return false;
60
61 /* Make sure route matches */
62 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
63 req_hdr->xd_hdr.route_hi)
64 return false;
65 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
66 return false;
67
Mika Westerbergd1ff7022017-10-02 13:38:34 +030068 /* Check that the XDomain protocol matches */
69 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
70 return false;
71
72 return true;
73 }
74
75 default:
76 return false;
77 }
78}
79
80static bool tb_xdomain_copy(struct tb_cfg_request *req,
81 const struct ctl_pkg *pkg)
82{
83 memcpy(req->response, pkg->buffer, req->response_size);
84 req->result.err = 0;
85 return true;
86}
87
88static void response_ready(void *data)
89{
90 tb_cfg_request_put(data);
91}
92
93static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
94 size_t size, enum tb_cfg_pkg_type type)
95{
96 struct tb_cfg_request *req;
97
98 req = tb_cfg_request_alloc();
99 if (!req)
100 return -ENOMEM;
101
102 req->match = tb_xdomain_match;
103 req->copy = tb_xdomain_copy;
104 req->request = response;
105 req->request_size = size;
106 req->request_type = type;
107
108 return tb_cfg_request(ctl, req, response_ready, req);
109}
110
111/**
112 * tb_xdomain_response() - Send a XDomain response message
113 * @xd: XDomain to send the message
114 * @response: Response to send
115 * @size: Size of the response
116 * @type: PDF type of the response
117 *
118 * This can be used to send a XDomain response message to the other
119 * domain. No response for the message is expected.
120 *
121 * Return: %0 in case of success and negative errno in case of failure
122 */
123int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
124 size_t size, enum tb_cfg_pkg_type type)
125{
126 return __tb_xdomain_response(xd->tb->ctl, response, size, type);
127}
128EXPORT_SYMBOL_GPL(tb_xdomain_response);
129
130static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
131 size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
132 size_t response_size, enum tb_cfg_pkg_type response_type,
133 unsigned int timeout_msec)
134{
135 struct tb_cfg_request *req;
136 struct tb_cfg_result res;
137
138 req = tb_cfg_request_alloc();
139 if (!req)
140 return -ENOMEM;
141
142 req->match = tb_xdomain_match;
143 req->copy = tb_xdomain_copy;
144 req->request = request;
145 req->request_size = request_size;
146 req->request_type = request_type;
147 req->response = response;
148 req->response_size = response_size;
149 req->response_type = response_type;
150
151 res = tb_cfg_request_sync(ctl, req, timeout_msec);
152
153 tb_cfg_request_put(req);
154
155 return res.err == 1 ? -EIO : res.err;
156}
157
158/**
159 * tb_xdomain_request() - Send a XDomain request
160 * @xd: XDomain to send the request
161 * @request: Request to send
162 * @request_size: Size of the request in bytes
163 * @request_type: PDF type of the request
164 * @response: Response is copied here
165 * @response_size: Expected size of the response in bytes
166 * @response_type: Expected PDF type of the response
167 * @timeout_msec: Timeout in milliseconds to wait for the response
168 *
169 * This function can be used to send XDomain control channel messages to
170 * the other domain. The function waits until the response is received
171 * or when timeout triggers. Whichever comes first.
172 *
173 * Return: %0 in case of success and negative errno in case of failure
174 */
175int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
176 size_t request_size, enum tb_cfg_pkg_type request_type,
177 void *response, size_t response_size,
178 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
179{
180 return __tb_xdomain_request(xd->tb->ctl, request, request_size,
181 request_type, response, response_size,
182 response_type, timeout_msec);
183}
184EXPORT_SYMBOL_GPL(tb_xdomain_request);
185
186static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
187 u8 sequence, enum tb_xdp_type type, size_t size)
188{
189 u32 length_sn;
190
191 length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
192 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
193
194 hdr->xd_hdr.route_hi = upper_32_bits(route);
195 hdr->xd_hdr.route_lo = lower_32_bits(route);
196 hdr->xd_hdr.length_sn = length_sn;
197 hdr->type = type;
198 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
199}
200
201static int tb_xdp_handle_error(const struct tb_xdp_header *hdr)
202{
203 const struct tb_xdp_error_response *error;
204
205 if (hdr->type != ERROR_RESPONSE)
206 return 0;
207
208 error = (const struct tb_xdp_error_response *)hdr;
209
210 switch (error->error) {
211 case ERROR_UNKNOWN_PACKET:
212 case ERROR_UNKNOWN_DOMAIN:
213 return -EIO;
214 case ERROR_NOT_SUPPORTED:
215 return -ENOTSUPP;
216 case ERROR_NOT_READY:
217 return -EAGAIN;
218 default:
219 break;
220 }
221
222 return 0;
223}
224
225static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
226 enum tb_xdp_error error)
227{
228 struct tb_xdp_error_response res;
229
230 memset(&res, 0, sizeof(res));
231 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
232 sizeof(res));
233 res.error = error;
234
235 return __tb_xdomain_response(ctl, &res, sizeof(res),
236 TB_CFG_PKG_XDOMAIN_RESP);
237}
238
239static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
240 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
241 u32 **block, u32 *generation)
242{
243 struct tb_xdp_properties_response *res;
244 struct tb_xdp_properties req;
245 u16 data_len, len;
246 size_t total_size;
247 u32 *data = NULL;
248 int ret;
249
250 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
251 res = kzalloc(total_size, GFP_KERNEL);
252 if (!res)
253 return -ENOMEM;
254
255 memset(&req, 0, sizeof(req));
256 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
257 sizeof(req));
258 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
259 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
260
261 len = 0;
262 data_len = 0;
263
264 do {
265 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
266 TB_CFG_PKG_XDOMAIN_REQ, res,
267 total_size, TB_CFG_PKG_XDOMAIN_RESP,
268 XDOMAIN_DEFAULT_TIMEOUT);
269 if (ret)
270 goto err;
271
272 ret = tb_xdp_handle_error(&res->hdr);
273 if (ret)
274 goto err;
275
276 /*
277 * Package length includes the whole payload without the
278 * XDomain header. Validate first that the package is at
279 * least size of the response structure.
280 */
281 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
282 if (len < sizeof(*res) / 4) {
283 ret = -EINVAL;
284 goto err;
285 }
286
287 len += sizeof(res->hdr.xd_hdr) / 4;
288 len -= sizeof(*res) / 4;
289
290 if (res->offset != req.offset) {
291 ret = -EINVAL;
292 goto err;
293 }
294
295 /*
296 * First time allocate block that has enough space for
297 * the whole properties block.
298 */
299 if (!data) {
300 data_len = res->data_length;
301 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
302 ret = -E2BIG;
303 goto err;
304 }
305
306 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
307 if (!data) {
308 ret = -ENOMEM;
309 goto err;
310 }
311 }
312
313 memcpy(data + req.offset, res->data, len * 4);
314 req.offset += len;
315 } while (!data_len || req.offset < data_len);
316
317 *block = data;
318 *generation = res->generation;
319
320 kfree(res);
321
322 return data_len;
323
324err:
325 kfree(data);
326 kfree(res);
327
328 return ret;
329}
330
331static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
332 u64 route, u8 sequence, const uuid_t *src_uuid,
333 const struct tb_xdp_properties *req)
334{
335 struct tb_xdp_properties_response *res;
336 size_t total_size;
337 u16 len;
338 int ret;
339
340 /*
341 * Currently we expect all requests to be directed to us. The
342 * protocol supports forwarding, though which we might add
343 * support later on.
344 */
345 if (!uuid_equal(src_uuid, &req->dst_uuid)) {
346 tb_xdp_error_response(ctl, route, sequence,
347 ERROR_UNKNOWN_DOMAIN);
348 return 0;
349 }
350
351 mutex_lock(&xdomain_lock);
352
353 if (req->offset >= xdomain_property_block_len) {
354 mutex_unlock(&xdomain_lock);
355 return -EINVAL;
356 }
357
358 len = xdomain_property_block_len - req->offset;
359 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
360 total_size = sizeof(*res) + len * 4;
361
362 res = kzalloc(total_size, GFP_KERNEL);
363 if (!res) {
364 mutex_unlock(&xdomain_lock);
365 return -ENOMEM;
366 }
367
368 tb_xdp_fill_header(&res->hdr, route, sequence, PROPERTIES_RESPONSE,
369 total_size);
370 res->generation = xdomain_property_block_gen;
371 res->data_length = xdomain_property_block_len;
372 res->offset = req->offset;
373 uuid_copy(&res->src_uuid, src_uuid);
374 uuid_copy(&res->dst_uuid, &req->src_uuid);
375 memcpy(res->data, &xdomain_property_block[req->offset], len * 4);
376
377 mutex_unlock(&xdomain_lock);
378
379 ret = __tb_xdomain_response(ctl, res, total_size,
380 TB_CFG_PKG_XDOMAIN_RESP);
381
382 kfree(res);
383 return ret;
384}
385
386static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
387 int retry, const uuid_t *uuid)
388{
389 struct tb_xdp_properties_changed_response res;
390 struct tb_xdp_properties_changed req;
391 int ret;
392
393 memset(&req, 0, sizeof(req));
394 tb_xdp_fill_header(&req.hdr, route, retry % 4,
395 PROPERTIES_CHANGED_REQUEST, sizeof(req));
396 uuid_copy(&req.src_uuid, uuid);
397
398 memset(&res, 0, sizeof(res));
399 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
400 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
401 TB_CFG_PKG_XDOMAIN_RESP,
402 XDOMAIN_DEFAULT_TIMEOUT);
403 if (ret)
404 return ret;
405
406 return tb_xdp_handle_error(&res.hdr);
407}
408
409static int
410tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
411{
412 struct tb_xdp_properties_changed_response res;
413
414 memset(&res, 0, sizeof(res));
415 tb_xdp_fill_header(&res.hdr, route, sequence,
416 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
417 return __tb_xdomain_response(ctl, &res, sizeof(res),
418 TB_CFG_PKG_XDOMAIN_RESP);
419}
420
421/**
422 * tb_register_protocol_handler() - Register protocol handler
423 * @handler: Handler to register
424 *
425 * This allows XDomain service drivers to hook into incoming XDomain
426 * messages. After this function is called the service driver needs to
427 * be able to handle calls to callback whenever a package with the
428 * registered protocol is received.
429 */
430int tb_register_protocol_handler(struct tb_protocol_handler *handler)
431{
432 if (!handler->uuid || !handler->callback)
433 return -EINVAL;
434 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
435 return -EINVAL;
436
437 mutex_lock(&xdomain_lock);
438 list_add_tail(&handler->list, &protocol_handlers);
439 mutex_unlock(&xdomain_lock);
440
441 return 0;
442}
443EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
444
445/**
446 * tb_unregister_protocol_handler() - Unregister protocol handler
447 * @handler: Handler to unregister
448 *
449 * Removes the previously registered protocol handler.
450 */
451void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
452{
453 mutex_lock(&xdomain_lock);
454 list_del_init(&handler->list);
455 mutex_unlock(&xdomain_lock);
456}
457EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
458
459static void tb_xdp_handle_request(struct work_struct *work)
460{
461 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
462 const struct tb_xdp_header *pkg = xw->pkg;
463 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
464 struct tb *tb = xw->tb;
465 struct tb_ctl *ctl = tb->ctl;
466 const uuid_t *uuid;
467 int ret = 0;
Dan Carpenter9a03c3d2017-10-17 15:32:17 +0300468 u32 sequence;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300469 u64 route;
470
471 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
472 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
473 sequence >>= TB_XDOMAIN_SN_SHIFT;
474
475 mutex_lock(&tb->lock);
476 if (tb->root_switch)
477 uuid = tb->root_switch->uuid;
478 else
479 uuid = NULL;
480 mutex_unlock(&tb->lock);
481
482 if (!uuid) {
483 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
484 goto out;
485 }
486
487 switch (pkg->type) {
488 case PROPERTIES_REQUEST:
489 ret = tb_xdp_properties_response(tb, ctl, route, sequence, uuid,
490 (const struct tb_xdp_properties *)pkg);
491 break;
492
493 case PROPERTIES_CHANGED_REQUEST: {
494 const struct tb_xdp_properties_changed *xchg =
495 (const struct tb_xdp_properties_changed *)pkg;
496 struct tb_xdomain *xd;
497
498 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
499
500 /*
501 * Since the properties have been changed, let's update
502 * the xdomain related to this connection as well in
503 * case there is a change in services it offers.
504 */
505 xd = tb_xdomain_find_by_uuid_locked(tb, &xchg->src_uuid);
506 if (xd) {
507 queue_delayed_work(tb->wq, &xd->get_properties_work,
508 msecs_to_jiffies(50));
509 tb_xdomain_put(xd);
510 }
511
512 break;
513 }
514
515 default:
516 break;
517 }
518
519 if (ret) {
520 tb_warn(tb, "failed to send XDomain response for %#x\n",
521 pkg->type);
522 }
523
524out:
525 kfree(xw->pkg);
526 kfree(xw);
527}
528
Aditya Pakki48f40b92019-03-20 11:47:20 -0500529static bool
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300530tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
531 size_t size)
532{
533 struct xdomain_request_work *xw;
534
535 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
536 if (!xw)
Aditya Pakki48f40b92019-03-20 11:47:20 -0500537 return false;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300538
539 INIT_WORK(&xw->work, tb_xdp_handle_request);
540 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
Aditya Pakki48f40b92019-03-20 11:47:20 -0500541 if (!xw->pkg) {
542 kfree(xw);
543 return false;
544 }
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300545 xw->tb = tb;
546
547 queue_work(tb->wq, &xw->work);
Aditya Pakki48f40b92019-03-20 11:47:20 -0500548 return true;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300549}
550
551/**
552 * tb_register_service_driver() - Register XDomain service driver
553 * @drv: Driver to register
554 *
555 * Registers new service driver from @drv to the bus.
556 */
557int tb_register_service_driver(struct tb_service_driver *drv)
558{
559 drv->driver.bus = &tb_bus_type;
560 return driver_register(&drv->driver);
561}
562EXPORT_SYMBOL_GPL(tb_register_service_driver);
563
564/**
565 * tb_unregister_service_driver() - Unregister XDomain service driver
566 * @xdrv: Driver to unregister
567 *
568 * Unregisters XDomain service driver from the bus.
569 */
570void tb_unregister_service_driver(struct tb_service_driver *drv)
571{
572 driver_unregister(&drv->driver);
573}
574EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
575
576static ssize_t key_show(struct device *dev, struct device_attribute *attr,
577 char *buf)
578{
579 struct tb_service *svc = container_of(dev, struct tb_service, dev);
580
581 /*
582 * It should be null terminated but anything else is pretty much
583 * allowed.
584 */
585 return sprintf(buf, "%*pEp\n", (int)strlen(svc->key), svc->key);
586}
587static DEVICE_ATTR_RO(key);
588
589static int get_modalias(struct tb_service *svc, char *buf, size_t size)
590{
591 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
592 svc->prtcid, svc->prtcvers, svc->prtcrevs);
593}
594
595static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
596 char *buf)
597{
598 struct tb_service *svc = container_of(dev, struct tb_service, dev);
599
600 /* Full buffer size except new line and null termination */
601 get_modalias(svc, buf, PAGE_SIZE - 2);
602 return sprintf(buf, "%s\n", buf);
603}
604static DEVICE_ATTR_RO(modalias);
605
606static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
607 char *buf)
608{
609 struct tb_service *svc = container_of(dev, struct tb_service, dev);
610
611 return sprintf(buf, "%u\n", svc->prtcid);
612}
613static DEVICE_ATTR_RO(prtcid);
614
615static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
616 char *buf)
617{
618 struct tb_service *svc = container_of(dev, struct tb_service, dev);
619
620 return sprintf(buf, "%u\n", svc->prtcvers);
621}
622static DEVICE_ATTR_RO(prtcvers);
623
624static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
625 char *buf)
626{
627 struct tb_service *svc = container_of(dev, struct tb_service, dev);
628
629 return sprintf(buf, "%u\n", svc->prtcrevs);
630}
631static DEVICE_ATTR_RO(prtcrevs);
632
633static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
634 char *buf)
635{
636 struct tb_service *svc = container_of(dev, struct tb_service, dev);
637
638 return sprintf(buf, "0x%08x\n", svc->prtcstns);
639}
640static DEVICE_ATTR_RO(prtcstns);
641
642static struct attribute *tb_service_attrs[] = {
643 &dev_attr_key.attr,
644 &dev_attr_modalias.attr,
645 &dev_attr_prtcid.attr,
646 &dev_attr_prtcvers.attr,
647 &dev_attr_prtcrevs.attr,
648 &dev_attr_prtcstns.attr,
649 NULL,
650};
651
652static struct attribute_group tb_service_attr_group = {
653 .attrs = tb_service_attrs,
654};
655
656static const struct attribute_group *tb_service_attr_groups[] = {
657 &tb_service_attr_group,
658 NULL,
659};
660
661static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
662{
663 struct tb_service *svc = container_of(dev, struct tb_service, dev);
664 char modalias[64];
665
666 get_modalias(svc, modalias, sizeof(modalias));
667 return add_uevent_var(env, "MODALIAS=%s", modalias);
668}
669
670static void tb_service_release(struct device *dev)
671{
672 struct tb_service *svc = container_of(dev, struct tb_service, dev);
673 struct tb_xdomain *xd = tb_service_parent(svc);
674
675 ida_simple_remove(&xd->service_ids, svc->id);
676 kfree(svc->key);
677 kfree(svc);
678}
679
680struct device_type tb_service_type = {
681 .name = "thunderbolt_service",
682 .groups = tb_service_attr_groups,
683 .uevent = tb_service_uevent,
684 .release = tb_service_release,
685};
686EXPORT_SYMBOL_GPL(tb_service_type);
687
688static int remove_missing_service(struct device *dev, void *data)
689{
690 struct tb_xdomain *xd = data;
691 struct tb_service *svc;
692
693 svc = tb_to_service(dev);
694 if (!svc)
695 return 0;
696
697 if (!tb_property_find(xd->properties, svc->key,
698 TB_PROPERTY_TYPE_DIRECTORY))
699 device_unregister(dev);
700
701 return 0;
702}
703
704static int find_service(struct device *dev, void *data)
705{
706 const struct tb_property *p = data;
707 struct tb_service *svc;
708
709 svc = tb_to_service(dev);
710 if (!svc)
711 return 0;
712
713 return !strcmp(svc->key, p->key);
714}
715
716static int populate_service(struct tb_service *svc,
717 struct tb_property *property)
718{
719 struct tb_property_dir *dir = property->value.dir;
720 struct tb_property *p;
721
722 /* Fill in standard properties */
723 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
724 if (p)
725 svc->prtcid = p->value.immediate;
726 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
727 if (p)
728 svc->prtcvers = p->value.immediate;
729 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
730 if (p)
731 svc->prtcrevs = p->value.immediate;
732 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
733 if (p)
734 svc->prtcstns = p->value.immediate;
735
736 svc->key = kstrdup(property->key, GFP_KERNEL);
737 if (!svc->key)
738 return -ENOMEM;
739
740 return 0;
741}
742
743static void enumerate_services(struct tb_xdomain *xd)
744{
745 struct tb_service *svc;
746 struct tb_property *p;
747 struct device *dev;
Aditya Pakki9aabb682019-03-20 11:34:09 -0500748 int id;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300749
750 /*
751 * First remove all services that are not available anymore in
752 * the updated property block.
753 */
754 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
755
756 /* Then re-enumerate properties creating new services as we go */
757 tb_property_for_each(xd->properties, p) {
758 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
759 continue;
760
761 /* If the service exists already we are fine */
762 dev = device_find_child(&xd->dev, p, find_service);
763 if (dev) {
764 put_device(dev);
765 continue;
766 }
767
768 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
769 if (!svc)
770 break;
771
772 if (populate_service(svc, p)) {
773 kfree(svc);
774 break;
775 }
776
Aditya Pakki9aabb682019-03-20 11:34:09 -0500777 id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
778 if (id < 0) {
779 kfree(svc);
780 break;
781 }
782 svc->id = id;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300783 svc->dev.bus = &tb_bus_type;
784 svc->dev.type = &tb_service_type;
785 svc->dev.parent = &xd->dev;
786 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
787
788 if (device_register(&svc->dev)) {
789 put_device(&svc->dev);
790 break;
791 }
792 }
793}
794
795static int populate_properties(struct tb_xdomain *xd,
796 struct tb_property_dir *dir)
797{
798 const struct tb_property *p;
799
800 /* Required properties */
801 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
802 if (!p)
803 return -EINVAL;
804 xd->device = p->value.immediate;
805
806 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
807 if (!p)
808 return -EINVAL;
809 xd->vendor = p->value.immediate;
810
811 kfree(xd->device_name);
812 xd->device_name = NULL;
813 kfree(xd->vendor_name);
814 xd->vendor_name = NULL;
815
816 /* Optional properties */
817 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
818 if (p)
819 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
820 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
821 if (p)
822 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
823
824 return 0;
825}
826
827/* Called with @xd->lock held */
828static void tb_xdomain_restore_paths(struct tb_xdomain *xd)
829{
830 if (!xd->resume)
831 return;
832
833 xd->resume = false;
834 if (xd->transmit_path) {
835 dev_dbg(&xd->dev, "re-establishing DMA path\n");
836 tb_domain_approve_xdomain_paths(xd->tb, xd);
837 }
838}
839
840static void tb_xdomain_get_properties(struct work_struct *work)
841{
842 struct tb_xdomain *xd = container_of(work, typeof(*xd),
843 get_properties_work.work);
844 struct tb_property_dir *dir;
845 struct tb *tb = xd->tb;
846 bool update = false;
847 u32 *block = NULL;
848 u32 gen = 0;
849 int ret;
850
851 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
852 xd->remote_uuid, xd->properties_retries,
853 &block, &gen);
854 if (ret < 0) {
855 if (xd->properties_retries-- > 0) {
856 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
857 msecs_to_jiffies(1000));
858 } else {
859 /* Give up now */
860 dev_err(&xd->dev,
861 "failed read XDomain properties from %pUb\n",
862 xd->remote_uuid);
863 }
864 return;
865 }
866
867 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
868
869 mutex_lock(&xd->lock);
870
871 /* Only accept newer generation properties */
872 if (xd->properties && gen <= xd->property_block_gen) {
873 /*
874 * On resume it is likely that the properties block is
875 * not changed (unless the other end added or removed
876 * services). However, we need to make sure the existing
877 * DMA paths are restored properly.
878 */
879 tb_xdomain_restore_paths(xd);
880 goto err_free_block;
881 }
882
883 dir = tb_property_parse_dir(block, ret);
884 if (!dir) {
885 dev_err(&xd->dev, "failed to parse XDomain properties\n");
886 goto err_free_block;
887 }
888
889 ret = populate_properties(xd, dir);
890 if (ret) {
891 dev_err(&xd->dev, "missing XDomain properties in response\n");
892 goto err_free_dir;
893 }
894
895 /* Release the existing one */
896 if (xd->properties) {
897 tb_property_free_dir(xd->properties);
898 update = true;
899 }
900
901 xd->properties = dir;
902 xd->property_block_gen = gen;
903
904 tb_xdomain_restore_paths(xd);
905
906 mutex_unlock(&xd->lock);
907
908 kfree(block);
909
910 /*
911 * Now the device should be ready enough so we can add it to the
912 * bus and let userspace know about it. If the device is already
913 * registered, we notify the userspace that it has changed.
914 */
915 if (!update) {
916 if (device_add(&xd->dev)) {
917 dev_err(&xd->dev, "failed to add XDomain device\n");
918 return;
919 }
920 } else {
921 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
922 }
923
924 enumerate_services(xd);
925 return;
926
927err_free_dir:
928 tb_property_free_dir(dir);
929err_free_block:
930 kfree(block);
931 mutex_unlock(&xd->lock);
932}
933
934static void tb_xdomain_properties_changed(struct work_struct *work)
935{
936 struct tb_xdomain *xd = container_of(work, typeof(*xd),
937 properties_changed_work.work);
938 int ret;
939
940 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
941 xd->properties_changed_retries, xd->local_uuid);
942 if (ret) {
943 if (xd->properties_changed_retries-- > 0)
944 queue_delayed_work(xd->tb->wq,
945 &xd->properties_changed_work,
946 msecs_to_jiffies(1000));
947 return;
948 }
949
950 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
951}
952
953static ssize_t device_show(struct device *dev, struct device_attribute *attr,
954 char *buf)
955{
956 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
957
958 return sprintf(buf, "%#x\n", xd->device);
959}
960static DEVICE_ATTR_RO(device);
961
962static ssize_t
963device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
964{
965 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
966 int ret;
967
968 if (mutex_lock_interruptible(&xd->lock))
969 return -ERESTARTSYS;
970 ret = sprintf(buf, "%s\n", xd->device_name ? xd->device_name : "");
971 mutex_unlock(&xd->lock);
972
973 return ret;
974}
975static DEVICE_ATTR_RO(device_name);
976
977static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
978 char *buf)
979{
980 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
981
982 return sprintf(buf, "%#x\n", xd->vendor);
983}
984static DEVICE_ATTR_RO(vendor);
985
986static ssize_t
987vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
988{
989 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
990 int ret;
991
992 if (mutex_lock_interruptible(&xd->lock))
993 return -ERESTARTSYS;
994 ret = sprintf(buf, "%s\n", xd->vendor_name ? xd->vendor_name : "");
995 mutex_unlock(&xd->lock);
996
997 return ret;
998}
999static DEVICE_ATTR_RO(vendor_name);
1000
1001static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1002 char *buf)
1003{
1004 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1005
1006 return sprintf(buf, "%pUb\n", xd->remote_uuid);
1007}
1008static DEVICE_ATTR_RO(unique_id);
1009
1010static struct attribute *xdomain_attrs[] = {
1011 &dev_attr_device.attr,
1012 &dev_attr_device_name.attr,
1013 &dev_attr_unique_id.attr,
1014 &dev_attr_vendor.attr,
1015 &dev_attr_vendor_name.attr,
1016 NULL,
1017};
1018
1019static struct attribute_group xdomain_attr_group = {
1020 .attrs = xdomain_attrs,
1021};
1022
1023static const struct attribute_group *xdomain_attr_groups[] = {
1024 &xdomain_attr_group,
1025 NULL,
1026};
1027
1028static void tb_xdomain_release(struct device *dev)
1029{
1030 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1031
1032 put_device(xd->dev.parent);
1033
1034 tb_property_free_dir(xd->properties);
1035 ida_destroy(&xd->service_ids);
1036
1037 kfree(xd->local_uuid);
1038 kfree(xd->remote_uuid);
1039 kfree(xd->device_name);
1040 kfree(xd->vendor_name);
1041 kfree(xd);
1042}
1043
1044static void start_handshake(struct tb_xdomain *xd)
1045{
1046 xd->properties_retries = XDOMAIN_PROPERTIES_RETRIES;
1047 xd->properties_changed_retries = XDOMAIN_PROPERTIES_CHANGED_RETRIES;
1048
1049 /* Start exchanging properties with the other host */
1050 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1051 msecs_to_jiffies(100));
1052 queue_delayed_work(xd->tb->wq, &xd->get_properties_work,
1053 msecs_to_jiffies(1000));
1054}
1055
1056static void stop_handshake(struct tb_xdomain *xd)
1057{
1058 xd->properties_retries = 0;
1059 xd->properties_changed_retries = 0;
1060
1061 cancel_delayed_work_sync(&xd->get_properties_work);
1062 cancel_delayed_work_sync(&xd->properties_changed_work);
1063}
1064
1065static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1066{
1067 stop_handshake(tb_to_xdomain(dev));
1068 return 0;
1069}
1070
1071static int __maybe_unused tb_xdomain_resume(struct device *dev)
1072{
1073 struct tb_xdomain *xd = tb_to_xdomain(dev);
1074
1075 /*
1076 * Ask tb_xdomain_get_properties() restore any existing DMA
1077 * paths after properties are re-read.
1078 */
1079 xd->resume = true;
1080 start_handshake(xd);
1081
1082 return 0;
1083}
1084
1085static const struct dev_pm_ops tb_xdomain_pm_ops = {
1086 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1087};
1088
1089struct device_type tb_xdomain_type = {
1090 .name = "thunderbolt_xdomain",
1091 .release = tb_xdomain_release,
1092 .pm = &tb_xdomain_pm_ops,
1093};
1094EXPORT_SYMBOL_GPL(tb_xdomain_type);
1095
1096/**
1097 * tb_xdomain_alloc() - Allocate new XDomain object
1098 * @tb: Domain where the XDomain belongs
1099 * @parent: Parent device (the switch through the connection to the
1100 * other domain is reached).
1101 * @route: Route string used to reach the other domain
1102 * @local_uuid: Our local domain UUID
1103 * @remote_uuid: UUID of the other domain
1104 *
1105 * Allocates new XDomain structure and returns pointer to that. The
1106 * object must be released by calling tb_xdomain_put().
1107 */
1108struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1109 u64 route, const uuid_t *local_uuid,
1110 const uuid_t *remote_uuid)
1111{
1112 struct tb_xdomain *xd;
1113
1114 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1115 if (!xd)
1116 return NULL;
1117
1118 xd->tb = tb;
1119 xd->route = route;
1120 ida_init(&xd->service_ids);
1121 mutex_init(&xd->lock);
1122 INIT_DELAYED_WORK(&xd->get_properties_work, tb_xdomain_get_properties);
1123 INIT_DELAYED_WORK(&xd->properties_changed_work,
1124 tb_xdomain_properties_changed);
1125
1126 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1127 if (!xd->local_uuid)
1128 goto err_free;
1129
1130 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t), GFP_KERNEL);
1131 if (!xd->remote_uuid)
1132 goto err_free_local_uuid;
1133
1134 device_initialize(&xd->dev);
1135 xd->dev.parent = get_device(parent);
1136 xd->dev.bus = &tb_bus_type;
1137 xd->dev.type = &tb_xdomain_type;
1138 xd->dev.groups = xdomain_attr_groups;
1139 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1140
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001141 /*
1142 * This keeps the DMA powered on as long as we have active
1143 * connection to another host.
1144 */
1145 pm_runtime_set_active(&xd->dev);
1146 pm_runtime_get_noresume(&xd->dev);
1147 pm_runtime_enable(&xd->dev);
1148
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001149 return xd;
1150
1151err_free_local_uuid:
1152 kfree(xd->local_uuid);
1153err_free:
1154 kfree(xd);
1155
1156 return NULL;
1157}
1158
1159/**
1160 * tb_xdomain_add() - Add XDomain to the bus
1161 * @xd: XDomain to add
1162 *
1163 * This function starts XDomain discovery protocol handshake and
1164 * eventually adds the XDomain to the bus. After calling this function
1165 * the caller needs to call tb_xdomain_remove() in order to remove and
1166 * release the object regardless whether the handshake succeeded or not.
1167 */
1168void tb_xdomain_add(struct tb_xdomain *xd)
1169{
1170 /* Start exchanging properties with the other host */
1171 start_handshake(xd);
1172}
1173
1174static int unregister_service(struct device *dev, void *data)
1175{
1176 device_unregister(dev);
1177 return 0;
1178}
1179
1180/**
1181 * tb_xdomain_remove() - Remove XDomain from the bus
1182 * @xd: XDomain to remove
1183 *
1184 * This will stop all ongoing configuration work and remove the XDomain
1185 * along with any services from the bus. When the last reference to @xd
1186 * is released the object will be released as well.
1187 */
1188void tb_xdomain_remove(struct tb_xdomain *xd)
1189{
1190 stop_handshake(xd);
1191
1192 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1193
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +03001194 /*
1195 * Undo runtime PM here explicitly because it is possible that
1196 * the XDomain was never added to the bus and thus device_del()
1197 * is not called for it (device_del() would handle this otherwise).
1198 */
1199 pm_runtime_disable(&xd->dev);
1200 pm_runtime_put_noidle(&xd->dev);
1201 pm_runtime_set_suspended(&xd->dev);
1202
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001203 if (!device_is_registered(&xd->dev))
1204 put_device(&xd->dev);
1205 else
1206 device_unregister(&xd->dev);
1207}
1208
1209/**
1210 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
1211 * @xd: XDomain connection
1212 * @transmit_path: HopID of the transmit path the other end is using to
1213 * send packets
1214 * @transmit_ring: DMA ring used to receive packets from the other end
1215 * @receive_path: HopID of the receive path the other end is using to
1216 * receive packets
1217 * @receive_ring: DMA ring used to send packets to the other end
1218 *
1219 * The function enables DMA paths accordingly so that after successful
1220 * return the caller can send and receive packets using high-speed DMA
1221 * path.
1222 *
1223 * Return: %0 in case of success and negative errno in case of error
1224 */
1225int tb_xdomain_enable_paths(struct tb_xdomain *xd, u16 transmit_path,
1226 u16 transmit_ring, u16 receive_path,
1227 u16 receive_ring)
1228{
1229 int ret;
1230
1231 mutex_lock(&xd->lock);
1232
1233 if (xd->transmit_path) {
1234 ret = xd->transmit_path == transmit_path ? 0 : -EBUSY;
1235 goto exit_unlock;
1236 }
1237
1238 xd->transmit_path = transmit_path;
1239 xd->transmit_ring = transmit_ring;
1240 xd->receive_path = receive_path;
1241 xd->receive_ring = receive_ring;
1242
1243 ret = tb_domain_approve_xdomain_paths(xd->tb, xd);
1244
1245exit_unlock:
1246 mutex_unlock(&xd->lock);
1247
1248 return ret;
1249}
1250EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
1251
1252/**
1253 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
1254 * @xd: XDomain connection
1255 *
1256 * This does the opposite of tb_xdomain_enable_paths(). After call to
1257 * this the caller is not expected to use the rings anymore.
1258 *
1259 * Return: %0 in case of success and negative errno in case of error
1260 */
1261int tb_xdomain_disable_paths(struct tb_xdomain *xd)
1262{
1263 int ret = 0;
1264
1265 mutex_lock(&xd->lock);
1266 if (xd->transmit_path) {
1267 xd->transmit_path = 0;
1268 xd->transmit_ring = 0;
1269 xd->receive_path = 0;
1270 xd->receive_ring = 0;
1271
1272 ret = tb_domain_disconnect_xdomain_paths(xd->tb, xd);
1273 }
1274 mutex_unlock(&xd->lock);
1275
1276 return ret;
1277}
1278EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
1279
1280struct tb_xdomain_lookup {
1281 const uuid_t *uuid;
1282 u8 link;
1283 u8 depth;
Radion Mirchevsky484cb152017-10-04 14:53:54 +03001284 u64 route;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001285};
1286
1287static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
1288 const struct tb_xdomain_lookup *lookup)
1289{
1290 int i;
1291
1292 for (i = 1; i <= sw->config.max_port_number; i++) {
1293 struct tb_port *port = &sw->ports[i];
1294 struct tb_xdomain *xd;
1295
1296 if (tb_is_upstream_port(port))
1297 continue;
1298
1299 if (port->xdomain) {
1300 xd = port->xdomain;
1301
1302 if (lookup->uuid) {
1303 if (uuid_equal(xd->remote_uuid, lookup->uuid))
1304 return xd;
Radion Mirchevsky484cb152017-10-04 14:53:54 +03001305 } else if (lookup->link &&
1306 lookup->link == xd->link &&
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001307 lookup->depth == xd->depth) {
1308 return xd;
Radion Mirchevsky484cb152017-10-04 14:53:54 +03001309 } else if (lookup->route &&
1310 lookup->route == xd->route) {
1311 return xd;
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001312 }
1313 } else if (port->remote) {
1314 xd = switch_find_xdomain(port->remote->sw, lookup);
1315 if (xd)
1316 return xd;
1317 }
1318 }
1319
1320 return NULL;
1321}
1322
1323/**
1324 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
1325 * @tb: Domain where the XDomain belongs to
1326 * @uuid: UUID to look for
1327 *
1328 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1329 * The returned XDomain will have its reference count increased so the
1330 * caller needs to call tb_xdomain_put() when it is done with the
1331 * object.
1332 *
1333 * This will find all XDomains including the ones that are not yet added
1334 * to the bus (handshake is still in progress).
1335 *
1336 * The caller needs to hold @tb->lock.
1337 */
1338struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
1339{
1340 struct tb_xdomain_lookup lookup;
1341 struct tb_xdomain *xd;
1342
1343 memset(&lookup, 0, sizeof(lookup));
1344 lookup.uuid = uuid;
1345
1346 xd = switch_find_xdomain(tb->root_switch, &lookup);
Radion Mirchevsky484cb152017-10-04 14:53:54 +03001347 return tb_xdomain_get(xd);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001348}
1349EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
1350
1351/**
1352 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
1353 * @tb: Domain where the XDomain belongs to
1354 * @link: Root switch link number
1355 * @depth: Depth in the link
1356 *
1357 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1358 * The returned XDomain will have its reference count increased so the
1359 * caller needs to call tb_xdomain_put() when it is done with the
1360 * object.
1361 *
1362 * This will find all XDomains including the ones that are not yet added
1363 * to the bus (handshake is still in progress).
1364 *
1365 * The caller needs to hold @tb->lock.
1366 */
1367struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1368 u8 depth)
1369{
1370 struct tb_xdomain_lookup lookup;
1371 struct tb_xdomain *xd;
1372
1373 memset(&lookup, 0, sizeof(lookup));
1374 lookup.link = link;
1375 lookup.depth = depth;
1376
1377 xd = switch_find_xdomain(tb->root_switch, &lookup);
Radion Mirchevsky484cb152017-10-04 14:53:54 +03001378 return tb_xdomain_get(xd);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001379}
1380
Radion Mirchevsky484cb152017-10-04 14:53:54 +03001381/**
1382 * tb_xdomain_find_by_route() - Find an XDomain by route string
1383 * @tb: Domain where the XDomain belongs to
1384 * @route: XDomain route string
1385 *
1386 * Finds XDomain by walking through the Thunderbolt topology below @tb.
1387 * The returned XDomain will have its reference count increased so the
1388 * caller needs to call tb_xdomain_put() when it is done with the
1389 * object.
1390 *
1391 * This will find all XDomains including the ones that are not yet added
1392 * to the bus (handshake is still in progress).
1393 *
1394 * The caller needs to hold @tb->lock.
1395 */
1396struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
1397{
1398 struct tb_xdomain_lookup lookup;
1399 struct tb_xdomain *xd;
1400
1401 memset(&lookup, 0, sizeof(lookup));
1402 lookup.route = route;
1403
1404 xd = switch_find_xdomain(tb->root_switch, &lookup);
1405 return tb_xdomain_get(xd);
1406}
1407EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
1408
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001409bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1410 const void *buf, size_t size)
1411{
1412 const struct tb_protocol_handler *handler, *tmp;
1413 const struct tb_xdp_header *hdr = buf;
1414 unsigned int length;
1415 int ret = 0;
1416
1417 /* We expect the packet is at least size of the header */
1418 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
1419 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
1420 return true;
1421 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
1422 return true;
1423
1424 /*
1425 * Handle XDomain discovery protocol packets directly here. For
1426 * other protocols (based on their UUID) we call registered
1427 * handlers in turn.
1428 */
1429 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
Aditya Pakki48f40b92019-03-20 11:47:20 -05001430 if (type == TB_CFG_PKG_XDOMAIN_REQ)
1431 return tb_xdp_schedule_request(tb, hdr, size);
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001432 return false;
1433 }
1434
1435 mutex_lock(&xdomain_lock);
1436 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
1437 if (!uuid_equal(&hdr->uuid, handler->uuid))
1438 continue;
1439
1440 mutex_unlock(&xdomain_lock);
1441 ret = handler->callback(buf, size, handler->data);
1442 mutex_lock(&xdomain_lock);
1443
1444 if (ret)
1445 break;
1446 }
1447 mutex_unlock(&xdomain_lock);
1448
1449 return ret > 0;
1450}
1451
1452static int rebuild_property_block(void)
1453{
1454 u32 *block, len;
1455 int ret;
1456
1457 ret = tb_property_format_dir(xdomain_property_dir, NULL, 0);
1458 if (ret < 0)
1459 return ret;
1460
1461 len = ret;
1462
1463 block = kcalloc(len, sizeof(u32), GFP_KERNEL);
1464 if (!block)
1465 return -ENOMEM;
1466
1467 ret = tb_property_format_dir(xdomain_property_dir, block, len);
1468 if (ret) {
1469 kfree(block);
1470 return ret;
1471 }
1472
1473 kfree(xdomain_property_block);
1474 xdomain_property_block = block;
1475 xdomain_property_block_len = len;
1476 xdomain_property_block_gen++;
1477
1478 return 0;
1479}
1480
1481static int update_xdomain(struct device *dev, void *data)
1482{
1483 struct tb_xdomain *xd;
1484
1485 xd = tb_to_xdomain(dev);
1486 if (xd) {
1487 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1488 msecs_to_jiffies(50));
1489 }
1490
1491 return 0;
1492}
1493
1494static void update_all_xdomains(void)
1495{
1496 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
1497}
1498
1499static bool remove_directory(const char *key, const struct tb_property_dir *dir)
1500{
1501 struct tb_property *p;
1502
1503 p = tb_property_find(xdomain_property_dir, key,
1504 TB_PROPERTY_TYPE_DIRECTORY);
1505 if (p && p->value.dir == dir) {
1506 tb_property_remove(p);
1507 return true;
1508 }
1509 return false;
1510}
1511
1512/**
1513 * tb_register_property_dir() - Register property directory to the host
1514 * @key: Key (name) of the directory to add
1515 * @dir: Directory to add
1516 *
1517 * Service drivers can use this function to add new property directory
1518 * to the host available properties. The other connected hosts are
1519 * notified so they can re-read properties of this host if they are
1520 * interested.
1521 *
1522 * Return: %0 on success and negative errno on failure
1523 */
1524int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
1525{
1526 int ret;
1527
Mika Westerbergacb40d82017-10-09 16:22:34 +03001528 if (WARN_ON(!xdomain_property_dir))
1529 return -EAGAIN;
1530
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001531 if (!key || strlen(key) > 8)
1532 return -EINVAL;
1533
1534 mutex_lock(&xdomain_lock);
1535 if (tb_property_find(xdomain_property_dir, key,
1536 TB_PROPERTY_TYPE_DIRECTORY)) {
1537 ret = -EEXIST;
1538 goto err_unlock;
1539 }
1540
1541 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
1542 if (ret)
1543 goto err_unlock;
1544
1545 ret = rebuild_property_block();
1546 if (ret) {
1547 remove_directory(key, dir);
1548 goto err_unlock;
1549 }
1550
1551 mutex_unlock(&xdomain_lock);
1552 update_all_xdomains();
1553 return 0;
1554
1555err_unlock:
1556 mutex_unlock(&xdomain_lock);
1557 return ret;
1558}
1559EXPORT_SYMBOL_GPL(tb_register_property_dir);
1560
1561/**
1562 * tb_unregister_property_dir() - Removes property directory from host
1563 * @key: Key (name) of the directory
1564 * @dir: Directory to remove
1565 *
1566 * This will remove the existing directory from this host and notify the
1567 * connected hosts about the change.
1568 */
1569void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
1570{
1571 int ret = 0;
1572
1573 mutex_lock(&xdomain_lock);
1574 if (remove_directory(key, dir))
1575 ret = rebuild_property_block();
1576 mutex_unlock(&xdomain_lock);
1577
1578 if (!ret)
1579 update_all_xdomains();
1580}
1581EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
1582
1583int tb_xdomain_init(void)
1584{
1585 int ret;
1586
1587 xdomain_property_dir = tb_property_create_dir(NULL);
1588 if (!xdomain_property_dir)
1589 return -ENOMEM;
1590
1591 /*
1592 * Initialize standard set of properties without any service
1593 * directories. Those will be added by service drivers
1594 * themselves when they are loaded.
1595 */
1596 tb_property_add_immediate(xdomain_property_dir, "vendorid",
1597 PCI_VENDOR_ID_INTEL);
1598 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
1599 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
1600 tb_property_add_text(xdomain_property_dir, "deviceid",
1601 utsname()->nodename);
1602 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
1603
1604 ret = rebuild_property_block();
1605 if (ret) {
1606 tb_property_free_dir(xdomain_property_dir);
1607 xdomain_property_dir = NULL;
1608 }
1609
1610 return ret;
1611}
1612
1613void tb_xdomain_exit(void)
1614{
1615 kfree(xdomain_property_block);
1616 tb_property_free_dir(xdomain_property_dir);
1617}