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