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