treewide: use get_random_u32() when possible
[linux-block.git] / drivers / thunderbolt / xdomain.c
CommitLineData
fd3b339c 1// SPDX-License-Identifier: GPL-2.0
d1ff7024
MW
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>
d1ff7024
MW
8 */
9
10#include <linux/device.h>
5cc0df9c 11#include <linux/delay.h>
d1ff7024
MW
12#include <linux/kmod.h>
13#include <linux/module.h>
2d8ff0b5 14#include <linux/pm_runtime.h>
a6932c3f 15#include <linux/prandom.h>
d1ff7024
MW
16#include <linux/utsname.h>
17#include <linux/uuid.h>
18#include <linux/workqueue.h>
19
20#include "tb.h"
21
8e1de704
MW
22#define XDOMAIN_SHORT_TIMEOUT 100 /* ms */
23#define XDOMAIN_DEFAULT_TIMEOUT 1000 /* ms */
24#define XDOMAIN_BONDING_TIMEOUT 10000 /* ms */
25#define XDOMAIN_RETRIES 10
46b494f2 26#define XDOMAIN_DEFAULT_MAX_HOPID 15
d1ff7024 27
8e1de704
MW
28enum {
29 XDOMAIN_STATE_INIT,
30 XDOMAIN_STATE_UUID,
31 XDOMAIN_STATE_LINK_STATUS,
32 XDOMAIN_STATE_LINK_STATE_CHANGE,
33 XDOMAIN_STATE_LINK_STATUS2,
34 XDOMAIN_STATE_BONDING_UUID_LOW,
35 XDOMAIN_STATE_BONDING_UUID_HIGH,
36 XDOMAIN_STATE_PROPERTIES,
37 XDOMAIN_STATE_ENUMERATED,
38 XDOMAIN_STATE_ERROR,
39};
40
41static const char * const state_names[] = {
42 [XDOMAIN_STATE_INIT] = "INIT",
43 [XDOMAIN_STATE_UUID] = "UUID",
44 [XDOMAIN_STATE_LINK_STATUS] = "LINK_STATUS",
45 [XDOMAIN_STATE_LINK_STATE_CHANGE] = "LINK_STATE_CHANGE",
46 [XDOMAIN_STATE_LINK_STATUS2] = "LINK_STATUS2",
47 [XDOMAIN_STATE_BONDING_UUID_LOW] = "BONDING_UUID_LOW",
48 [XDOMAIN_STATE_BONDING_UUID_HIGH] = "BONDING_UUID_HIGH",
49 [XDOMAIN_STATE_PROPERTIES] = "PROPERTIES",
50 [XDOMAIN_STATE_ENUMERATED] = "ENUMERATED",
51 [XDOMAIN_STATE_ERROR] = "ERROR",
52};
53
d1ff7024
MW
54struct xdomain_request_work {
55 struct work_struct work;
56 struct tb_xdp_header *pkg;
57 struct tb *tb;
58};
59
5ca67688
MW
60static bool tb_xdomain_enabled = true;
61module_param_named(xdomain, tb_xdomain_enabled, bool, 0444);
62MODULE_PARM_DESC(xdomain, "allow XDomain protocol (default: true)");
63
46b494f2
MW
64/*
65 * Serializes access to the properties and protocol handlers below. If
66 * you need to take both this lock and the struct tb_xdomain lock, take
67 * this one first.
68 */
d1ff7024
MW
69static DEFINE_MUTEX(xdomain_lock);
70
71/* Properties exposed to the remote domains */
72static struct tb_property_dir *xdomain_property_dir;
d1ff7024
MW
73static u32 xdomain_property_block_gen;
74
75/* Additional protocol handlers */
76static LIST_HEAD(protocol_handlers);
77
78/* UUID for XDomain discovery protocol: b638d70e-42ff-40bb-97c2-90e2c0b2ff07 */
79static const uuid_t tb_xdp_uuid =
80 UUID_INIT(0xb638d70e, 0x42ff, 0x40bb,
81 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07);
82
5ca67688
MW
83bool tb_is_xdomain_enabled(void)
84{
c6da62a2 85 return tb_xdomain_enabled && tb_acpi_is_xdomain_allowed();
5ca67688
MW
86}
87
d1ff7024
MW
88static bool tb_xdomain_match(const struct tb_cfg_request *req,
89 const struct ctl_pkg *pkg)
90{
91 switch (pkg->frame.eof) {
92 case TB_CFG_PKG_ERROR:
93 return true;
94
95 case TB_CFG_PKG_XDOMAIN_RESP: {
96 const struct tb_xdp_header *res_hdr = pkg->buffer;
97 const struct tb_xdp_header *req_hdr = req->request;
d1ff7024
MW
98
99 if (pkg->frame.size < req->response_size / 4)
100 return false;
101
102 /* Make sure route matches */
103 if ((res_hdr->xd_hdr.route_hi & ~BIT(31)) !=
104 req_hdr->xd_hdr.route_hi)
105 return false;
106 if ((res_hdr->xd_hdr.route_lo) != req_hdr->xd_hdr.route_lo)
107 return false;
108
d1ff7024
MW
109 /* Check that the XDomain protocol matches */
110 if (!uuid_equal(&res_hdr->uuid, &req_hdr->uuid))
111 return false;
112
113 return true;
114 }
115
116 default:
117 return false;
118 }
119}
120
121static bool tb_xdomain_copy(struct tb_cfg_request *req,
122 const struct ctl_pkg *pkg)
123{
124 memcpy(req->response, pkg->buffer, req->response_size);
125 req->result.err = 0;
126 return true;
127}
128
129static void response_ready(void *data)
130{
131 tb_cfg_request_put(data);
132}
133
134static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
135 size_t size, enum tb_cfg_pkg_type type)
136{
137 struct tb_cfg_request *req;
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 = response;
146 req->request_size = size;
147 req->request_type = type;
148
149 return tb_cfg_request(ctl, req, response_ready, req);
150}
151
152/**
153 * tb_xdomain_response() - Send a XDomain response message
154 * @xd: XDomain to send the message
155 * @response: Response to send
156 * @size: Size of the response
157 * @type: PDF type of the response
158 *
159 * This can be used to send a XDomain response message to the other
160 * domain. No response for the message is expected.
161 *
162 * Return: %0 in case of success and negative errno in case of failure
163 */
164int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
165 size_t size, enum tb_cfg_pkg_type type)
166{
167 return __tb_xdomain_response(xd->tb->ctl, response, size, type);
168}
169EXPORT_SYMBOL_GPL(tb_xdomain_response);
170
171static int __tb_xdomain_request(struct tb_ctl *ctl, const void *request,
172 size_t request_size, enum tb_cfg_pkg_type request_type, void *response,
173 size_t response_size, enum tb_cfg_pkg_type response_type,
174 unsigned int timeout_msec)
175{
176 struct tb_cfg_request *req;
177 struct tb_cfg_result res;
178
179 req = tb_cfg_request_alloc();
180 if (!req)
181 return -ENOMEM;
182
183 req->match = tb_xdomain_match;
184 req->copy = tb_xdomain_copy;
185 req->request = request;
186 req->request_size = request_size;
187 req->request_type = request_type;
188 req->response = response;
189 req->response_size = response_size;
190 req->response_type = response_type;
191
192 res = tb_cfg_request_sync(ctl, req, timeout_msec);
193
194 tb_cfg_request_put(req);
195
196 return res.err == 1 ? -EIO : res.err;
197}
198
199/**
200 * tb_xdomain_request() - Send a XDomain request
201 * @xd: XDomain to send the request
202 * @request: Request to send
203 * @request_size: Size of the request in bytes
204 * @request_type: PDF type of the request
205 * @response: Response is copied here
206 * @response_size: Expected size of the response in bytes
207 * @response_type: Expected PDF type of the response
208 * @timeout_msec: Timeout in milliseconds to wait for the response
209 *
210 * This function can be used to send XDomain control channel messages to
211 * the other domain. The function waits until the response is received
212 * or when timeout triggers. Whichever comes first.
213 *
214 * Return: %0 in case of success and negative errno in case of failure
215 */
216int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
217 size_t request_size, enum tb_cfg_pkg_type request_type,
218 void *response, size_t response_size,
219 enum tb_cfg_pkg_type response_type, unsigned int timeout_msec)
220{
221 return __tb_xdomain_request(xd->tb->ctl, request, request_size,
222 request_type, response, response_size,
223 response_type, timeout_msec);
224}
225EXPORT_SYMBOL_GPL(tb_xdomain_request);
226
227static inline void tb_xdp_fill_header(struct tb_xdp_header *hdr, u64 route,
228 u8 sequence, enum tb_xdp_type type, size_t size)
229{
230 u32 length_sn;
231
232 length_sn = (size - sizeof(hdr->xd_hdr)) / 4;
233 length_sn |= (sequence << TB_XDOMAIN_SN_SHIFT) & TB_XDOMAIN_SN_MASK;
234
235 hdr->xd_hdr.route_hi = upper_32_bits(route);
236 hdr->xd_hdr.route_lo = lower_32_bits(route);
237 hdr->xd_hdr.length_sn = length_sn;
238 hdr->type = type;
239 memcpy(&hdr->uuid, &tb_xdp_uuid, sizeof(tb_xdp_uuid));
240}
241
19813551 242static int tb_xdp_handle_error(const struct tb_xdp_error_response *res)
d1ff7024 243{
19813551 244 if (res->hdr.type != ERROR_RESPONSE)
d1ff7024
MW
245 return 0;
246
19813551 247 switch (res->error) {
d1ff7024
MW
248 case ERROR_UNKNOWN_PACKET:
249 case ERROR_UNKNOWN_DOMAIN:
250 return -EIO;
251 case ERROR_NOT_SUPPORTED:
252 return -ENOTSUPP;
253 case ERROR_NOT_READY:
254 return -EAGAIN;
255 default:
256 break;
257 }
258
259 return 0;
260}
261
3b4b3235 262static int tb_xdp_uuid_request(struct tb_ctl *ctl, u64 route, int retry,
8e1de704 263 uuid_t *uuid, u64 *remote_route)
3b4b3235
MW
264{
265 struct tb_xdp_uuid_response res;
266 struct tb_xdp_uuid req;
267 int ret;
268
269 memset(&req, 0, sizeof(req));
270 tb_xdp_fill_header(&req.hdr, route, retry % 4, UUID_REQUEST,
271 sizeof(req));
272
273 memset(&res, 0, sizeof(res));
274 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
275 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
276 TB_CFG_PKG_XDOMAIN_RESP,
277 XDOMAIN_DEFAULT_TIMEOUT);
278 if (ret)
279 return ret;
280
19813551 281 ret = tb_xdp_handle_error(&res.err);
3b4b3235
MW
282 if (ret)
283 return ret;
284
285 uuid_copy(uuid, &res.src_uuid);
8e1de704
MW
286 *remote_route = (u64)res.src_route_hi << 32 | res.src_route_lo;
287
3b4b3235
MW
288 return 0;
289}
290
291static int tb_xdp_uuid_response(struct tb_ctl *ctl, u64 route, u8 sequence,
292 const uuid_t *uuid)
293{
294 struct tb_xdp_uuid_response res;
295
296 memset(&res, 0, sizeof(res));
297 tb_xdp_fill_header(&res.hdr, route, sequence, UUID_RESPONSE,
298 sizeof(res));
299
300 uuid_copy(&res.src_uuid, uuid);
301 res.src_route_hi = upper_32_bits(route);
302 res.src_route_lo = lower_32_bits(route);
303
304 return __tb_xdomain_response(ctl, &res, sizeof(res),
305 TB_CFG_PKG_XDOMAIN_RESP);
306}
307
d1ff7024
MW
308static int tb_xdp_error_response(struct tb_ctl *ctl, u64 route, u8 sequence,
309 enum tb_xdp_error error)
310{
311 struct tb_xdp_error_response res;
312
313 memset(&res, 0, sizeof(res));
314 tb_xdp_fill_header(&res.hdr, route, sequence, ERROR_RESPONSE,
315 sizeof(res));
316 res.error = error;
317
318 return __tb_xdomain_response(ctl, &res, sizeof(res),
319 TB_CFG_PKG_XDOMAIN_RESP);
320}
321
322static int tb_xdp_properties_request(struct tb_ctl *ctl, u64 route,
323 const uuid_t *src_uuid, const uuid_t *dst_uuid, int retry,
324 u32 **block, u32 *generation)
325{
326 struct tb_xdp_properties_response *res;
327 struct tb_xdp_properties req;
328 u16 data_len, len;
329 size_t total_size;
330 u32 *data = NULL;
331 int ret;
332
333 total_size = sizeof(*res) + TB_XDP_PROPERTIES_MAX_DATA_LENGTH * 4;
334 res = kzalloc(total_size, GFP_KERNEL);
335 if (!res)
336 return -ENOMEM;
337
338 memset(&req, 0, sizeof(req));
339 tb_xdp_fill_header(&req.hdr, route, retry % 4, PROPERTIES_REQUEST,
340 sizeof(req));
341 memcpy(&req.src_uuid, src_uuid, sizeof(*src_uuid));
342 memcpy(&req.dst_uuid, dst_uuid, sizeof(*dst_uuid));
343
344 len = 0;
345 data_len = 0;
346
347 do {
348 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
349 TB_CFG_PKG_XDOMAIN_REQ, res,
350 total_size, TB_CFG_PKG_XDOMAIN_RESP,
351 XDOMAIN_DEFAULT_TIMEOUT);
352 if (ret)
353 goto err;
354
19813551 355 ret = tb_xdp_handle_error(&res->err);
d1ff7024
MW
356 if (ret)
357 goto err;
358
359 /*
360 * Package length includes the whole payload without the
361 * XDomain header. Validate first that the package is at
362 * least size of the response structure.
363 */
364 len = res->hdr.xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
365 if (len < sizeof(*res) / 4) {
366 ret = -EINVAL;
367 goto err;
368 }
369
370 len += sizeof(res->hdr.xd_hdr) / 4;
371 len -= sizeof(*res) / 4;
372
373 if (res->offset != req.offset) {
374 ret = -EINVAL;
375 goto err;
376 }
377
378 /*
379 * First time allocate block that has enough space for
380 * the whole properties block.
381 */
382 if (!data) {
383 data_len = res->data_length;
384 if (data_len > TB_XDP_PROPERTIES_MAX_LENGTH) {
385 ret = -E2BIG;
386 goto err;
387 }
388
389 data = kcalloc(data_len, sizeof(u32), GFP_KERNEL);
390 if (!data) {
391 ret = -ENOMEM;
392 goto err;
393 }
394 }
395
396 memcpy(data + req.offset, res->data, len * 4);
397 req.offset += len;
398 } while (!data_len || req.offset < data_len);
399
400 *block = data;
401 *generation = res->generation;
402
403 kfree(res);
404
405 return data_len;
406
407err:
408 kfree(data);
409 kfree(res);
410
411 return ret;
412}
413
414static int tb_xdp_properties_response(struct tb *tb, struct tb_ctl *ctl,
46b494f2 415 struct tb_xdomain *xd, u8 sequence, const struct tb_xdp_properties *req)
d1ff7024
MW
416{
417 struct tb_xdp_properties_response *res;
418 size_t total_size;
419 u16 len;
420 int ret;
421
422 /*
423 * Currently we expect all requests to be directed to us. The
424 * protocol supports forwarding, though which we might add
425 * support later on.
426 */
46b494f2
MW
427 if (!uuid_equal(xd->local_uuid, &req->dst_uuid)) {
428 tb_xdp_error_response(ctl, xd->route, sequence,
d1ff7024
MW
429 ERROR_UNKNOWN_DOMAIN);
430 return 0;
431 }
432
46b494f2 433 mutex_lock(&xd->lock);
d1ff7024 434
46b494f2
MW
435 if (req->offset >= xd->local_property_block_len) {
436 mutex_unlock(&xd->lock);
d1ff7024
MW
437 return -EINVAL;
438 }
439
46b494f2 440 len = xd->local_property_block_len - req->offset;
d1ff7024
MW
441 len = min_t(u16, len, TB_XDP_PROPERTIES_MAX_DATA_LENGTH);
442 total_size = sizeof(*res) + len * 4;
443
444 res = kzalloc(total_size, GFP_KERNEL);
445 if (!res) {
46b494f2 446 mutex_unlock(&xd->lock);
d1ff7024
MW
447 return -ENOMEM;
448 }
449
46b494f2 450 tb_xdp_fill_header(&res->hdr, xd->route, sequence, PROPERTIES_RESPONSE,
d1ff7024 451 total_size);
46b494f2
MW
452 res->generation = xd->local_property_block_gen;
453 res->data_length = xd->local_property_block_len;
d1ff7024 454 res->offset = req->offset;
46b494f2 455 uuid_copy(&res->src_uuid, xd->local_uuid);
d1ff7024 456 uuid_copy(&res->dst_uuid, &req->src_uuid);
46b494f2 457 memcpy(res->data, &xd->local_property_block[req->offset], len * 4);
d1ff7024 458
46b494f2 459 mutex_unlock(&xd->lock);
d1ff7024
MW
460
461 ret = __tb_xdomain_response(ctl, res, total_size,
462 TB_CFG_PKG_XDOMAIN_RESP);
463
464 kfree(res);
465 return ret;
466}
467
468static int tb_xdp_properties_changed_request(struct tb_ctl *ctl, u64 route,
469 int retry, const uuid_t *uuid)
470{
471 struct tb_xdp_properties_changed_response res;
472 struct tb_xdp_properties_changed req;
473 int ret;
474
475 memset(&req, 0, sizeof(req));
476 tb_xdp_fill_header(&req.hdr, route, retry % 4,
477 PROPERTIES_CHANGED_REQUEST, sizeof(req));
478 uuid_copy(&req.src_uuid, uuid);
479
480 memset(&res, 0, sizeof(res));
481 ret = __tb_xdomain_request(ctl, &req, sizeof(req),
482 TB_CFG_PKG_XDOMAIN_REQ, &res, sizeof(res),
483 TB_CFG_PKG_XDOMAIN_RESP,
484 XDOMAIN_DEFAULT_TIMEOUT);
485 if (ret)
486 return ret;
487
19813551 488 return tb_xdp_handle_error(&res.err);
d1ff7024
MW
489}
490
491static int
492tb_xdp_properties_changed_response(struct tb_ctl *ctl, u64 route, u8 sequence)
493{
494 struct tb_xdp_properties_changed_response res;
495
496 memset(&res, 0, sizeof(res));
497 tb_xdp_fill_header(&res.hdr, route, sequence,
498 PROPERTIES_CHANGED_RESPONSE, sizeof(res));
499 return __tb_xdomain_response(ctl, &res, sizeof(res),
500 TB_CFG_PKG_XDOMAIN_RESP);
501}
502
8e1de704
MW
503static int tb_xdp_link_state_status_request(struct tb_ctl *ctl, u64 route,
504 u8 sequence, u8 *slw, u8 *tlw,
505 u8 *sls, u8 *tls)
506{
507 struct tb_xdp_link_state_status_response res;
508 struct tb_xdp_link_state_status req;
509 int ret;
510
511 memset(&req, 0, sizeof(req));
512 tb_xdp_fill_header(&req.hdr, route, sequence, LINK_STATE_STATUS_REQUEST,
513 sizeof(req));
514
515 memset(&res, 0, sizeof(res));
516 ret = __tb_xdomain_request(ctl, &req, sizeof(req), TB_CFG_PKG_XDOMAIN_REQ,
517 &res, sizeof(res), TB_CFG_PKG_XDOMAIN_RESP,
518 XDOMAIN_DEFAULT_TIMEOUT);
519 if (ret)
520 return ret;
521
522 ret = tb_xdp_handle_error(&res.err);
523 if (ret)
524 return ret;
525
526 if (res.status != 0)
527 return -EREMOTEIO;
528
529 *slw = res.slw;
530 *tlw = res.tlw;
531 *sls = res.sls;
532 *tls = res.tls;
533
534 return 0;
535}
536
537static int tb_xdp_link_state_status_response(struct tb *tb, struct tb_ctl *ctl,
538 struct tb_xdomain *xd, u8 sequence)
539{
540 struct tb_switch *sw = tb_to_switch(xd->dev.parent);
541 struct tb_xdp_link_state_status_response res;
542 struct tb_port *port = tb_port_at(xd->route, sw);
543 u32 val[2];
544 int ret;
545
546 memset(&res, 0, sizeof(res));
547 tb_xdp_fill_header(&res.hdr, xd->route, sequence,
548 LINK_STATE_STATUS_RESPONSE, sizeof(res));
549
550 ret = tb_port_read(port, val, TB_CFG_PORT,
551 port->cap_phy + LANE_ADP_CS_0, ARRAY_SIZE(val));
552 if (ret)
553 return ret;
554
555 res.slw = (val[0] & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
556 LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
557 res.sls = (val[0] & LANE_ADP_CS_0_SUPPORTED_SPEED_MASK) >>
558 LANE_ADP_CS_0_SUPPORTED_SPEED_SHIFT;
559 res.tls = val[1] & LANE_ADP_CS_1_TARGET_SPEED_MASK;
560 res.tlw = (val[1] & LANE_ADP_CS_1_TARGET_WIDTH_MASK) >>
561 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
562
563 return __tb_xdomain_response(ctl, &res, sizeof(res),
564 TB_CFG_PKG_XDOMAIN_RESP);
565}
566
567static int tb_xdp_link_state_change_request(struct tb_ctl *ctl, u64 route,
568 u8 sequence, u8 tlw, u8 tls)
569{
570 struct tb_xdp_link_state_change_response res;
571 struct tb_xdp_link_state_change req;
572 int ret;
573
574 memset(&req, 0, sizeof(req));
575 tb_xdp_fill_header(&req.hdr, route, sequence, LINK_STATE_CHANGE_REQUEST,
576 sizeof(req));
577 req.tlw = tlw;
578 req.tls = tls;
579
580 memset(&res, 0, sizeof(res));
581 ret = __tb_xdomain_request(ctl, &req, sizeof(req), TB_CFG_PKG_XDOMAIN_REQ,
582 &res, sizeof(res), TB_CFG_PKG_XDOMAIN_RESP,
583 XDOMAIN_DEFAULT_TIMEOUT);
584 if (ret)
585 return ret;
586
587 ret = tb_xdp_handle_error(&res.err);
588 if (ret)
589 return ret;
590
591 return res.status != 0 ? -EREMOTEIO : 0;
592}
593
594static int tb_xdp_link_state_change_response(struct tb_ctl *ctl, u64 route,
595 u8 sequence, u32 status)
596{
597 struct tb_xdp_link_state_change_response res;
598
599 memset(&res, 0, sizeof(res));
600 tb_xdp_fill_header(&res.hdr, route, sequence, LINK_STATE_CHANGE_RESPONSE,
601 sizeof(res));
602
603 res.status = status;
604
605 return __tb_xdomain_response(ctl, &res, sizeof(res),
606 TB_CFG_PKG_XDOMAIN_RESP);
607}
608
d1ff7024
MW
609/**
610 * tb_register_protocol_handler() - Register protocol handler
611 * @handler: Handler to register
612 *
613 * This allows XDomain service drivers to hook into incoming XDomain
614 * messages. After this function is called the service driver needs to
615 * be able to handle calls to callback whenever a package with the
616 * registered protocol is received.
617 */
618int tb_register_protocol_handler(struct tb_protocol_handler *handler)
619{
620 if (!handler->uuid || !handler->callback)
621 return -EINVAL;
622 if (uuid_equal(handler->uuid, &tb_xdp_uuid))
623 return -EINVAL;
624
625 mutex_lock(&xdomain_lock);
626 list_add_tail(&handler->list, &protocol_handlers);
627 mutex_unlock(&xdomain_lock);
628
629 return 0;
630}
631EXPORT_SYMBOL_GPL(tb_register_protocol_handler);
632
633/**
634 * tb_unregister_protocol_handler() - Unregister protocol handler
635 * @handler: Handler to unregister
636 *
637 * Removes the previously registered protocol handler.
638 */
639void tb_unregister_protocol_handler(struct tb_protocol_handler *handler)
640{
641 mutex_lock(&xdomain_lock);
642 list_del_init(&handler->list);
643 mutex_unlock(&xdomain_lock);
644}
645EXPORT_SYMBOL_GPL(tb_unregister_protocol_handler);
646
46b494f2 647static void update_property_block(struct tb_xdomain *xd)
8a00c67e 648{
46b494f2
MW
649 mutex_lock(&xdomain_lock);
650 mutex_lock(&xd->lock);
651 /*
652 * If the local property block is not up-to-date, rebuild it now
653 * based on the global property template.
654 */
655 if (!xd->local_property_block ||
656 xd->local_property_block_gen < xdomain_property_block_gen) {
657 struct tb_property_dir *dir;
658 int ret, block_len;
659 u32 *block;
660
661 dir = tb_property_copy_dir(xdomain_property_dir);
662 if (!dir) {
663 dev_warn(&xd->dev, "failed to copy properties\n");
664 goto out_unlock;
665 }
8a00c67e 666
46b494f2
MW
667 /* Fill in non-static properties now */
668 tb_property_add_text(dir, "deviceid", utsname()->nodename);
669 tb_property_add_immediate(dir, "maxhopid", xd->local_max_hopid);
8a00c67e 670
46b494f2
MW
671 ret = tb_property_format_dir(dir, NULL, 0);
672 if (ret < 0) {
673 dev_warn(&xd->dev, "local property block creation failed\n");
674 tb_property_free_dir(dir);
675 goto out_unlock;
676 }
8a00c67e 677
46b494f2
MW
678 block_len = ret;
679 block = kcalloc(block_len, sizeof(*block), GFP_KERNEL);
680 if (!block) {
681 tb_property_free_dir(dir);
682 goto out_unlock;
683 }
8a00c67e 684
46b494f2
MW
685 ret = tb_property_format_dir(dir, block, block_len);
686 if (ret) {
687 dev_warn(&xd->dev, "property block generation failed\n");
688 tb_property_free_dir(dir);
689 kfree(block);
690 goto out_unlock;
691 }
8a00c67e 692
46b494f2
MW
693 tb_property_free_dir(dir);
694 /* Release the previous block */
695 kfree(xd->local_property_block);
696 /* Assign new one */
697 xd->local_property_block = block;
698 xd->local_property_block_len = block_len;
699 xd->local_property_block_gen = xdomain_property_block_gen;
8a00c67e 700 }
46b494f2
MW
701
702out_unlock:
703 mutex_unlock(&xd->lock);
8a00c67e
MW
704 mutex_unlock(&xdomain_lock);
705}
706
d1ff7024
MW
707static void tb_xdp_handle_request(struct work_struct *work)
708{
709 struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
710 const struct tb_xdp_header *pkg = xw->pkg;
711 const struct tb_xdomain_header *xhdr = &pkg->xd_hdr;
712 struct tb *tb = xw->tb;
713 struct tb_ctl *ctl = tb->ctl;
46b494f2 714 struct tb_xdomain *xd;
d1ff7024
MW
715 const uuid_t *uuid;
716 int ret = 0;
9a03c3d3 717 u32 sequence;
d1ff7024
MW
718 u64 route;
719
720 route = ((u64)xhdr->route_hi << 32 | xhdr->route_lo) & ~BIT_ULL(63);
721 sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
722 sequence >>= TB_XDOMAIN_SN_SHIFT;
723
724 mutex_lock(&tb->lock);
725 if (tb->root_switch)
726 uuid = tb->root_switch->uuid;
727 else
728 uuid = NULL;
729 mutex_unlock(&tb->lock);
730
731 if (!uuid) {
732 tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
733 goto out;
734 }
735
46b494f2
MW
736 xd = tb_xdomain_find_by_route_locked(tb, route);
737 if (xd)
738 update_property_block(xd);
739
d1ff7024
MW
740 switch (pkg->type) {
741 case PROPERTIES_REQUEST:
8e1de704 742 tb_dbg(tb, "%llx: received XDomain properties request\n", route);
46b494f2
MW
743 if (xd) {
744 ret = tb_xdp_properties_response(tb, ctl, xd, sequence,
745 (const struct tb_xdp_properties *)pkg);
746 }
d1ff7024
MW
747 break;
748
46b494f2 749 case PROPERTIES_CHANGED_REQUEST:
8e1de704
MW
750 tb_dbg(tb, "%llx: received XDomain properties changed request\n",
751 route);
752
d1ff7024
MW
753 ret = tb_xdp_properties_changed_response(ctl, route, sequence);
754
755 /*
756 * Since the properties have been changed, let's update
757 * the xdomain related to this connection as well in
758 * case there is a change in services it offers.
759 */
8e1de704
MW
760 if (xd && device_is_registered(&xd->dev))
761 queue_delayed_work(tb->wq, &xd->state_work,
762 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
d1ff7024 763 break;
d1ff7024 764
3b4b3235
MW
765 case UUID_REQUEST_OLD:
766 case UUID_REQUEST:
8e1de704 767 tb_dbg(tb, "%llx: received XDomain UUID request\n", route);
3b4b3235
MW
768 ret = tb_xdp_uuid_response(ctl, route, sequence, uuid);
769 break;
770
8e1de704
MW
771 case LINK_STATE_STATUS_REQUEST:
772 tb_dbg(tb, "%llx: received XDomain link state status request\n",
773 route);
774
775 if (xd) {
776 ret = tb_xdp_link_state_status_response(tb, ctl, xd,
777 sequence);
778 } else {
779 tb_xdp_error_response(ctl, route, sequence,
780 ERROR_NOT_READY);
781 }
782 break;
783
784 case LINK_STATE_CHANGE_REQUEST:
785 tb_dbg(tb, "%llx: received XDomain link state change request\n",
786 route);
787
788 if (xd && xd->state == XDOMAIN_STATE_BONDING_UUID_HIGH) {
789 const struct tb_xdp_link_state_change *lsc =
790 (const struct tb_xdp_link_state_change *)pkg;
791
792 ret = tb_xdp_link_state_change_response(ctl, route,
793 sequence, 0);
794 xd->target_link_width = lsc->tlw;
795 queue_delayed_work(tb->wq, &xd->state_work,
796 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
797 } else {
798 tb_xdp_error_response(ctl, route, sequence,
799 ERROR_NOT_READY);
800 }
801 break;
802
d1ff7024 803 default:
8e1de704 804 tb_dbg(tb, "%llx: unknown XDomain request %#x\n", route, pkg->type);
3b4b3235
MW
805 tb_xdp_error_response(ctl, route, sequence,
806 ERROR_NOT_SUPPORTED);
d1ff7024
MW
807 break;
808 }
809
46b494f2
MW
810 tb_xdomain_put(xd);
811
d1ff7024
MW
812 if (ret) {
813 tb_warn(tb, "failed to send XDomain response for %#x\n",
814 pkg->type);
815 }
816
817out:
818 kfree(xw->pkg);
819 kfree(xw);
559c1e1e
MW
820
821 tb_domain_put(tb);
d1ff7024
MW
822}
823
48f40b96 824static bool
d1ff7024
MW
825tb_xdp_schedule_request(struct tb *tb, const struct tb_xdp_header *hdr,
826 size_t size)
827{
828 struct xdomain_request_work *xw;
829
830 xw = kmalloc(sizeof(*xw), GFP_KERNEL);
831 if (!xw)
48f40b96 832 return false;
d1ff7024
MW
833
834 INIT_WORK(&xw->work, tb_xdp_handle_request);
835 xw->pkg = kmemdup(hdr, size, GFP_KERNEL);
48f40b96
AP
836 if (!xw->pkg) {
837 kfree(xw);
838 return false;
839 }
559c1e1e 840 xw->tb = tb_domain_get(tb);
d1ff7024 841
559c1e1e 842 schedule_work(&xw->work);
48f40b96 843 return true;
d1ff7024
MW
844}
845
846/**
847 * tb_register_service_driver() - Register XDomain service driver
848 * @drv: Driver to register
849 *
850 * Registers new service driver from @drv to the bus.
851 */
852int tb_register_service_driver(struct tb_service_driver *drv)
853{
854 drv->driver.bus = &tb_bus_type;
855 return driver_register(&drv->driver);
856}
857EXPORT_SYMBOL_GPL(tb_register_service_driver);
858
859/**
860 * tb_unregister_service_driver() - Unregister XDomain service driver
925a2af9 861 * @drv: Driver to unregister
d1ff7024
MW
862 *
863 * Unregisters XDomain service driver from the bus.
864 */
865void tb_unregister_service_driver(struct tb_service_driver *drv)
866{
867 driver_unregister(&drv->driver);
868}
869EXPORT_SYMBOL_GPL(tb_unregister_service_driver);
870
871static ssize_t key_show(struct device *dev, struct device_attribute *attr,
872 char *buf)
873{
874 struct tb_service *svc = container_of(dev, struct tb_service, dev);
875
876 /*
877 * It should be null terminated but anything else is pretty much
878 * allowed.
879 */
8283fb57 880 return sysfs_emit(buf, "%*pE\n", (int)strlen(svc->key), svc->key);
d1ff7024
MW
881}
882static DEVICE_ATTR_RO(key);
883
884static int get_modalias(struct tb_service *svc, char *buf, size_t size)
885{
886 return snprintf(buf, size, "tbsvc:k%sp%08Xv%08Xr%08X", svc->key,
887 svc->prtcid, svc->prtcvers, svc->prtcrevs);
888}
889
890static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
891 char *buf)
892{
893 struct tb_service *svc = container_of(dev, struct tb_service, dev);
894
895 /* Full buffer size except new line and null termination */
896 get_modalias(svc, buf, PAGE_SIZE - 2);
0a0624a2 897 return strlen(strcat(buf, "\n"));
d1ff7024
MW
898}
899static DEVICE_ATTR_RO(modalias);
900
901static ssize_t prtcid_show(struct device *dev, struct device_attribute *attr,
902 char *buf)
903{
904 struct tb_service *svc = container_of(dev, struct tb_service, dev);
905
8283fb57 906 return sysfs_emit(buf, "%u\n", svc->prtcid);
d1ff7024
MW
907}
908static DEVICE_ATTR_RO(prtcid);
909
910static ssize_t prtcvers_show(struct device *dev, struct device_attribute *attr,
911 char *buf)
912{
913 struct tb_service *svc = container_of(dev, struct tb_service, dev);
914
8283fb57 915 return sysfs_emit(buf, "%u\n", svc->prtcvers);
d1ff7024
MW
916}
917static DEVICE_ATTR_RO(prtcvers);
918
919static ssize_t prtcrevs_show(struct device *dev, struct device_attribute *attr,
920 char *buf)
921{
922 struct tb_service *svc = container_of(dev, struct tb_service, dev);
923
8283fb57 924 return sysfs_emit(buf, "%u\n", svc->prtcrevs);
d1ff7024
MW
925}
926static DEVICE_ATTR_RO(prtcrevs);
927
928static ssize_t prtcstns_show(struct device *dev, struct device_attribute *attr,
929 char *buf)
930{
931 struct tb_service *svc = container_of(dev, struct tb_service, dev);
932
8283fb57 933 return sysfs_emit(buf, "0x%08x\n", svc->prtcstns);
d1ff7024
MW
934}
935static DEVICE_ATTR_RO(prtcstns);
936
937static struct attribute *tb_service_attrs[] = {
938 &dev_attr_key.attr,
939 &dev_attr_modalias.attr,
940 &dev_attr_prtcid.attr,
941 &dev_attr_prtcvers.attr,
942 &dev_attr_prtcrevs.attr,
943 &dev_attr_prtcstns.attr,
944 NULL,
945};
946
6889e00f 947static const struct attribute_group tb_service_attr_group = {
d1ff7024
MW
948 .attrs = tb_service_attrs,
949};
950
951static const struct attribute_group *tb_service_attr_groups[] = {
952 &tb_service_attr_group,
953 NULL,
954};
955
956static int tb_service_uevent(struct device *dev, struct kobj_uevent_env *env)
957{
958 struct tb_service *svc = container_of(dev, struct tb_service, dev);
959 char modalias[64];
960
961 get_modalias(svc, modalias, sizeof(modalias));
962 return add_uevent_var(env, "MODALIAS=%s", modalias);
963}
964
965static void tb_service_release(struct device *dev)
966{
967 struct tb_service *svc = container_of(dev, struct tb_service, dev);
968 struct tb_xdomain *xd = tb_service_parent(svc);
969
407ac931 970 tb_service_debugfs_remove(svc);
d1ff7024
MW
971 ida_simple_remove(&xd->service_ids, svc->id);
972 kfree(svc->key);
973 kfree(svc);
974}
975
976struct device_type tb_service_type = {
977 .name = "thunderbolt_service",
978 .groups = tb_service_attr_groups,
979 .uevent = tb_service_uevent,
980 .release = tb_service_release,
981};
982EXPORT_SYMBOL_GPL(tb_service_type);
983
984static int remove_missing_service(struct device *dev, void *data)
985{
986 struct tb_xdomain *xd = data;
987 struct tb_service *svc;
988
989 svc = tb_to_service(dev);
990 if (!svc)
991 return 0;
992
46b494f2 993 if (!tb_property_find(xd->remote_properties, svc->key,
d1ff7024
MW
994 TB_PROPERTY_TYPE_DIRECTORY))
995 device_unregister(dev);
996
997 return 0;
998}
999
1000static int find_service(struct device *dev, void *data)
1001{
1002 const struct tb_property *p = data;
1003 struct tb_service *svc;
1004
1005 svc = tb_to_service(dev);
1006 if (!svc)
1007 return 0;
1008
1009 return !strcmp(svc->key, p->key);
1010}
1011
1012static int populate_service(struct tb_service *svc,
1013 struct tb_property *property)
1014{
1015 struct tb_property_dir *dir = property->value.dir;
1016 struct tb_property *p;
1017
1018 /* Fill in standard properties */
1019 p = tb_property_find(dir, "prtcid", TB_PROPERTY_TYPE_VALUE);
1020 if (p)
1021 svc->prtcid = p->value.immediate;
1022 p = tb_property_find(dir, "prtcvers", TB_PROPERTY_TYPE_VALUE);
1023 if (p)
1024 svc->prtcvers = p->value.immediate;
1025 p = tb_property_find(dir, "prtcrevs", TB_PROPERTY_TYPE_VALUE);
1026 if (p)
1027 svc->prtcrevs = p->value.immediate;
1028 p = tb_property_find(dir, "prtcstns", TB_PROPERTY_TYPE_VALUE);
1029 if (p)
1030 svc->prtcstns = p->value.immediate;
1031
1032 svc->key = kstrdup(property->key, GFP_KERNEL);
1033 if (!svc->key)
1034 return -ENOMEM;
1035
1036 return 0;
1037}
1038
1039static void enumerate_services(struct tb_xdomain *xd)
1040{
1041 struct tb_service *svc;
1042 struct tb_property *p;
1043 struct device *dev;
9aabb685 1044 int id;
d1ff7024
MW
1045
1046 /*
1047 * First remove all services that are not available anymore in
1048 * the updated property block.
1049 */
1050 device_for_each_child_reverse(&xd->dev, xd, remove_missing_service);
1051
1052 /* Then re-enumerate properties creating new services as we go */
46b494f2 1053 tb_property_for_each(xd->remote_properties, p) {
d1ff7024
MW
1054 if (p->type != TB_PROPERTY_TYPE_DIRECTORY)
1055 continue;
1056
1057 /* If the service exists already we are fine */
1058 dev = device_find_child(&xd->dev, p, find_service);
1059 if (dev) {
1060 put_device(dev);
1061 continue;
1062 }
1063
1064 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1065 if (!svc)
1066 break;
1067
1068 if (populate_service(svc, p)) {
1069 kfree(svc);
1070 break;
1071 }
1072
9aabb685
AP
1073 id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
1074 if (id < 0) {
a663e0df 1075 kfree(svc->key);
9aabb685
AP
1076 kfree(svc);
1077 break;
1078 }
1079 svc->id = id;
d1ff7024
MW
1080 svc->dev.bus = &tb_bus_type;
1081 svc->dev.type = &tb_service_type;
1082 svc->dev.parent = &xd->dev;
1083 dev_set_name(&svc->dev, "%s.%d", dev_name(&xd->dev), svc->id);
1084
407ac931
MW
1085 tb_service_debugfs_init(svc);
1086
d1ff7024
MW
1087 if (device_register(&svc->dev)) {
1088 put_device(&svc->dev);
1089 break;
1090 }
1091 }
1092}
1093
1094static int populate_properties(struct tb_xdomain *xd,
1095 struct tb_property_dir *dir)
1096{
1097 const struct tb_property *p;
1098
1099 /* Required properties */
1100 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_VALUE);
1101 if (!p)
1102 return -EINVAL;
1103 xd->device = p->value.immediate;
1104
1105 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_VALUE);
1106 if (!p)
1107 return -EINVAL;
1108 xd->vendor = p->value.immediate;
1109
46b494f2
MW
1110 p = tb_property_find(dir, "maxhopid", TB_PROPERTY_TYPE_VALUE);
1111 /*
1112 * USB4 inter-domain spec suggests using 15 as HopID if the
1113 * other end does not announce it in a property. This is for
1114 * TBT3 compatibility.
1115 */
1116 xd->remote_max_hopid = p ? p->value.immediate : XDOMAIN_DEFAULT_MAX_HOPID;
1117
d1ff7024
MW
1118 kfree(xd->device_name);
1119 xd->device_name = NULL;
1120 kfree(xd->vendor_name);
1121 xd->vendor_name = NULL;
1122
1123 /* Optional properties */
1124 p = tb_property_find(dir, "deviceid", TB_PROPERTY_TYPE_TEXT);
1125 if (p)
1126 xd->device_name = kstrdup(p->value.text, GFP_KERNEL);
1127 p = tb_property_find(dir, "vendorid", TB_PROPERTY_TYPE_TEXT);
1128 if (p)
1129 xd->vendor_name = kstrdup(p->value.text, GFP_KERNEL);
1130
1131 return 0;
1132}
1133
4210d50f
IH
1134static int tb_xdomain_update_link_attributes(struct tb_xdomain *xd)
1135{
1136 bool change = false;
1137 struct tb_port *port;
1138 int ret;
1139
1140 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1141
1142 ret = tb_port_get_link_speed(port);
1143 if (ret < 0)
1144 return ret;
1145
1146 if (xd->link_speed != ret)
1147 change = true;
1148
1149 xd->link_speed = ret;
1150
1151 ret = tb_port_get_link_width(port);
1152 if (ret < 0)
1153 return ret;
1154
1155 if (xd->link_width != ret)
1156 change = true;
1157
1158 xd->link_width = ret;
1159
1160 if (change)
1161 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1162
1163 return 0;
1164}
1165
8e1de704 1166static int tb_xdomain_get_uuid(struct tb_xdomain *xd)
3b4b3235 1167{
3b4b3235
MW
1168 struct tb *tb = xd->tb;
1169 uuid_t uuid;
8e1de704 1170 u64 route;
3b4b3235
MW
1171 int ret;
1172
d29c59b1
MW
1173 dev_dbg(&xd->dev, "requesting remote UUID\n");
1174
8e1de704
MW
1175 ret = tb_xdp_uuid_request(tb->ctl, xd->route, xd->state_retries, &uuid,
1176 &route);
3b4b3235 1177 if (ret < 0) {
8e1de704 1178 if (xd->state_retries-- > 0) {
d29c59b1 1179 dev_dbg(&xd->dev, "failed to request UUID, retrying\n");
8e1de704 1180 return -EAGAIN;
3b4b3235
MW
1181 } else {
1182 dev_dbg(&xd->dev, "failed to read remote UUID\n");
1183 }
8e1de704 1184 return ret;
3b4b3235
MW
1185 }
1186
d29c59b1
MW
1187 dev_dbg(&xd->dev, "got remote UUID %pUb\n", &uuid);
1188
8e1de704
MW
1189 if (uuid_equal(&uuid, xd->local_uuid)) {
1190 if (route == xd->route)
1191 dev_dbg(&xd->dev, "loop back detected\n");
1192 else
1193 dev_dbg(&xd->dev, "intra-domain loop detected\n");
1194
1195 /* Don't bond lanes automatically for loops */
1196 xd->bonding_possible = false;
1197 }
3b4b3235
MW
1198
1199 /*
1200 * If the UUID is different, there is another domain connected
1201 * so mark this one unplugged and wait for the connection
1202 * manager to replace it.
1203 */
1204 if (xd->remote_uuid && !uuid_equal(&uuid, xd->remote_uuid)) {
1205 dev_dbg(&xd->dev, "remote UUID is different, unplugging\n");
1206 xd->is_unplugged = true;
8e1de704 1207 return -ENODEV;
3b4b3235
MW
1208 }
1209
1210 /* First time fill in the missing UUID */
1211 if (!xd->remote_uuid) {
1212 xd->remote_uuid = kmemdup(&uuid, sizeof(uuid_t), GFP_KERNEL);
1213 if (!xd->remote_uuid)
8e1de704 1214 return -ENOMEM;
3b4b3235
MW
1215 }
1216
8e1de704 1217 return 0;
3b4b3235
MW
1218}
1219
8e1de704
MW
1220static int tb_xdomain_get_link_status(struct tb_xdomain *xd)
1221{
1222 struct tb *tb = xd->tb;
1223 u8 slw, tlw, sls, tls;
1224 int ret;
1225
1226 dev_dbg(&xd->dev, "sending link state status request to %pUb\n",
1227 xd->remote_uuid);
1228
1229 ret = tb_xdp_link_state_status_request(tb->ctl, xd->route,
1230 xd->state_retries, &slw, &tlw, &sls,
1231 &tls);
1232 if (ret) {
1233 if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) {
1234 dev_dbg(&xd->dev,
1235 "failed to request remote link status, retrying\n");
1236 return -EAGAIN;
1237 }
1238 dev_dbg(&xd->dev, "failed to receive remote link status\n");
1239 return ret;
1240 }
1241
1242 dev_dbg(&xd->dev, "remote link supports width %#x speed %#x\n", slw, sls);
1243
1244 if (slw < LANE_ADP_CS_0_SUPPORTED_WIDTH_DUAL) {
1245 dev_dbg(&xd->dev, "remote adapter is single lane only\n");
1246 return -EOPNOTSUPP;
1247 }
1248
1249 return 0;
1250}
1251
1252static int tb_xdomain_link_state_change(struct tb_xdomain *xd,
1253 unsigned int width)
1254{
1255 struct tb_switch *sw = tb_to_switch(xd->dev.parent);
1256 struct tb_port *port = tb_port_at(xd->route, sw);
1257 struct tb *tb = xd->tb;
1258 u8 tlw, tls;
1259 u32 val;
1260 int ret;
1261
1262 if (width == 2)
1263 tlw = LANE_ADP_CS_1_TARGET_WIDTH_DUAL;
1264 else if (width == 1)
1265 tlw = LANE_ADP_CS_1_TARGET_WIDTH_SINGLE;
1266 else
1267 return -EINVAL;
1268
1269 /* Use the current target speed */
1270 ret = tb_port_read(port, &val, TB_CFG_PORT, port->cap_phy + LANE_ADP_CS_1, 1);
1271 if (ret)
1272 return ret;
1273 tls = val & LANE_ADP_CS_1_TARGET_SPEED_MASK;
1274
1275 dev_dbg(&xd->dev, "sending link state change request with width %#x speed %#x\n",
1276 tlw, tls);
1277
1278 ret = tb_xdp_link_state_change_request(tb->ctl, xd->route,
1279 xd->state_retries, tlw, tls);
1280 if (ret) {
1281 if (ret != -EOPNOTSUPP && xd->state_retries-- > 0) {
1282 dev_dbg(&xd->dev,
1283 "failed to change remote link state, retrying\n");
1284 return -EAGAIN;
1285 }
1286 dev_err(&xd->dev, "failed request link state change, aborting\n");
1287 return ret;
1288 }
1289
1290 dev_dbg(&xd->dev, "received link state change response\n");
1291 return 0;
1292}
1293
1294static int tb_xdomain_bond_lanes_uuid_high(struct tb_xdomain *xd)
1295{
1296 struct tb_port *port;
1297 int ret, width;
1298
1299 if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_SINGLE) {
1300 width = 1;
1301 } else if (xd->target_link_width == LANE_ADP_CS_1_TARGET_WIDTH_DUAL) {
1302 width = 2;
1303 } else {
1304 if (xd->state_retries-- > 0) {
1305 dev_dbg(&xd->dev,
1306 "link state change request not received yet, retrying\n");
1307 return -EAGAIN;
1308 }
1309 dev_dbg(&xd->dev, "timeout waiting for link change request\n");
1310 return -ETIMEDOUT;
1311 }
1312
1313 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1314
1315 /*
1316 * We can't use tb_xdomain_lane_bonding_enable() here because it
1317 * is the other side that initiates lane bonding. So here we
1318 * just set the width to both lane adapters and wait for the
1319 * link to transition bonded.
1320 */
1321 ret = tb_port_set_link_width(port->dual_link_port, width);
1322 if (ret) {
1323 tb_port_warn(port->dual_link_port,
1324 "failed to set link width to %d\n", width);
1325 return ret;
1326 }
1327
1328 ret = tb_port_set_link_width(port, width);
1329 if (ret) {
1330 tb_port_warn(port, "failed to set link width to %d\n", width);
1331 return ret;
1332 }
1333
1334 ret = tb_port_wait_for_link_width(port, width, XDOMAIN_BONDING_TIMEOUT);
1335 if (ret) {
1336 dev_warn(&xd->dev, "error waiting for link width to become %d\n",
1337 width);
1338 return ret;
1339 }
1340
1341 port->bonded = width == 2;
1342 port->dual_link_port->bonded = width == 2;
1343
1344 tb_port_update_credits(port);
1345 tb_xdomain_update_link_attributes(xd);
1346
1347 dev_dbg(&xd->dev, "lane bonding %sabled\n", width == 2 ? "en" : "dis");
1348 return 0;
1349}
1350
1351static int tb_xdomain_get_properties(struct tb_xdomain *xd)
d1ff7024 1352{
d1ff7024
MW
1353 struct tb_property_dir *dir;
1354 struct tb *tb = xd->tb;
1355 bool update = false;
1356 u32 *block = NULL;
1357 u32 gen = 0;
1358 int ret;
1359
d29c59b1
MW
1360 dev_dbg(&xd->dev, "requesting remote properties\n");
1361
d1ff7024 1362 ret = tb_xdp_properties_request(tb->ctl, xd->route, xd->local_uuid,
8e1de704 1363 xd->remote_uuid, xd->state_retries,
d1ff7024
MW
1364 &block, &gen);
1365 if (ret < 0) {
8e1de704 1366 if (xd->state_retries-- > 0) {
d29c59b1
MW
1367 dev_dbg(&xd->dev,
1368 "failed to request remote properties, retrying\n");
8e1de704 1369 return -EAGAIN;
d1ff7024
MW
1370 } else {
1371 /* Give up now */
1372 dev_err(&xd->dev,
1373 "failed read XDomain properties from %pUb\n",
1374 xd->remote_uuid);
1375 }
d1ff7024 1376
8e1de704
MW
1377 return ret;
1378 }
d1ff7024
MW
1379
1380 mutex_lock(&xd->lock);
1381
1382 /* Only accept newer generation properties */
8e1de704
MW
1383 if (xd->remote_properties && gen <= xd->remote_property_block_gen) {
1384 ret = 0;
d1ff7024 1385 goto err_free_block;
8e1de704 1386 }
d1ff7024
MW
1387
1388 dir = tb_property_parse_dir(block, ret);
1389 if (!dir) {
1390 dev_err(&xd->dev, "failed to parse XDomain properties\n");
8e1de704 1391 ret = -ENOMEM;
d1ff7024
MW
1392 goto err_free_block;
1393 }
1394
1395 ret = populate_properties(xd, dir);
1396 if (ret) {
1397 dev_err(&xd->dev, "missing XDomain properties in response\n");
1398 goto err_free_dir;
1399 }
1400
1401 /* Release the existing one */
46b494f2
MW
1402 if (xd->remote_properties) {
1403 tb_property_free_dir(xd->remote_properties);
d1ff7024
MW
1404 update = true;
1405 }
1406
46b494f2
MW
1407 xd->remote_properties = dir;
1408 xd->remote_property_block_gen = gen;
d1ff7024 1409
4210d50f
IH
1410 tb_xdomain_update_link_attributes(xd);
1411
d1ff7024
MW
1412 mutex_unlock(&xd->lock);
1413
1414 kfree(block);
1415
1416 /*
1417 * Now the device should be ready enough so we can add it to the
1418 * bus and let userspace know about it. If the device is already
1419 * registered, we notify the userspace that it has changed.
1420 */
1421 if (!update) {
8e1de704
MW
1422 struct tb_port *port;
1423
1424 /* Now disable lane 1 if bonding was not enabled */
1425 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1426 if (!port->bonded)
1427 tb_port_disable(port->dual_link_port);
1428
d1ff7024
MW
1429 if (device_add(&xd->dev)) {
1430 dev_err(&xd->dev, "failed to add XDomain device\n");
8e1de704 1431 return -ENODEV;
d1ff7024 1432 }
d29c59b1
MW
1433 dev_info(&xd->dev, "new host found, vendor=%#x device=%#x\n",
1434 xd->vendor, xd->device);
1435 if (xd->vendor_name && xd->device_name)
1436 dev_info(&xd->dev, "%s %s\n", xd->vendor_name,
1437 xd->device_name);
d0f1e0c2
MW
1438
1439 tb_xdomain_debugfs_init(xd);
d1ff7024
MW
1440 } else {
1441 kobject_uevent(&xd->dev.kobj, KOBJ_CHANGE);
1442 }
1443
1444 enumerate_services(xd);
8e1de704 1445 return 0;
d1ff7024
MW
1446
1447err_free_dir:
1448 tb_property_free_dir(dir);
1449err_free_block:
1450 kfree(block);
1451 mutex_unlock(&xd->lock);
8e1de704
MW
1452
1453 return ret;
1454}
1455
1456static void tb_xdomain_queue_uuid(struct tb_xdomain *xd)
1457{
1458 xd->state = XDOMAIN_STATE_UUID;
1459 xd->state_retries = XDOMAIN_RETRIES;
1460 queue_delayed_work(xd->tb->wq, &xd->state_work,
1461 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
1462}
1463
1464static void tb_xdomain_queue_link_status(struct tb_xdomain *xd)
1465{
1466 xd->state = XDOMAIN_STATE_LINK_STATUS;
1467 xd->state_retries = XDOMAIN_RETRIES;
1468 queue_delayed_work(xd->tb->wq, &xd->state_work,
1469 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
1470}
1471
1472static void tb_xdomain_queue_link_status2(struct tb_xdomain *xd)
1473{
1474 xd->state = XDOMAIN_STATE_LINK_STATUS2;
1475 xd->state_retries = XDOMAIN_RETRIES;
1476 queue_delayed_work(xd->tb->wq, &xd->state_work,
1477 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
1478}
1479
1480static void tb_xdomain_queue_bonding(struct tb_xdomain *xd)
1481{
1482 if (memcmp(xd->local_uuid, xd->remote_uuid, UUID_SIZE) > 0) {
1483 dev_dbg(&xd->dev, "we have higher UUID, other side bonds the lanes\n");
1484 xd->state = XDOMAIN_STATE_BONDING_UUID_HIGH;
1485 } else {
1486 dev_dbg(&xd->dev, "we have lower UUID, bonding lanes\n");
1487 xd->state = XDOMAIN_STATE_LINK_STATE_CHANGE;
1488 }
1489
1490 xd->state_retries = XDOMAIN_RETRIES;
1491 queue_delayed_work(xd->tb->wq, &xd->state_work,
1492 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
1493}
1494
1495static void tb_xdomain_queue_bonding_uuid_low(struct tb_xdomain *xd)
1496{
1497 xd->state = XDOMAIN_STATE_BONDING_UUID_LOW;
1498 xd->state_retries = XDOMAIN_RETRIES;
1499 queue_delayed_work(xd->tb->wq, &xd->state_work,
1500 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
1501}
1502
1503static void tb_xdomain_queue_properties(struct tb_xdomain *xd)
1504{
1505 xd->state = XDOMAIN_STATE_PROPERTIES;
1506 xd->state_retries = XDOMAIN_RETRIES;
1507 queue_delayed_work(xd->tb->wq, &xd->state_work,
1508 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
1509}
1510
1511static void tb_xdomain_queue_properties_changed(struct tb_xdomain *xd)
1512{
1513 xd->properties_changed_retries = XDOMAIN_RETRIES;
1514 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
1515 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
1516}
1517
1518static void tb_xdomain_state_work(struct work_struct *work)
1519{
1520 struct tb_xdomain *xd = container_of(work, typeof(*xd), state_work.work);
1521 int ret, state = xd->state;
1522
1523 if (WARN_ON_ONCE(state < XDOMAIN_STATE_INIT ||
1524 state > XDOMAIN_STATE_ERROR))
1525 return;
1526
1527 dev_dbg(&xd->dev, "running state %s\n", state_names[state]);
1528
1529 switch (state) {
1530 case XDOMAIN_STATE_INIT:
1531 if (xd->needs_uuid) {
1532 tb_xdomain_queue_uuid(xd);
1533 } else {
1534 tb_xdomain_queue_properties_changed(xd);
1535 tb_xdomain_queue_properties(xd);
1536 }
1537 break;
1538
1539 case XDOMAIN_STATE_UUID:
1540 ret = tb_xdomain_get_uuid(xd);
1541 if (ret) {
1542 if (ret == -EAGAIN)
1543 goto retry_state;
1544 xd->state = XDOMAIN_STATE_ERROR;
1545 } else {
1546 tb_xdomain_queue_properties_changed(xd);
1547 if (xd->bonding_possible)
1548 tb_xdomain_queue_link_status(xd);
1549 else
1550 tb_xdomain_queue_properties(xd);
1551 }
1552 break;
1553
1554 case XDOMAIN_STATE_LINK_STATUS:
1555 ret = tb_xdomain_get_link_status(xd);
1556 if (ret) {
1557 if (ret == -EAGAIN)
1558 goto retry_state;
1559
1560 /*
1561 * If any of the lane bonding states fail we skip
1562 * bonding completely and try to continue from
1563 * reading properties.
1564 */
1565 tb_xdomain_queue_properties(xd);
1566 } else {
1567 tb_xdomain_queue_bonding(xd);
1568 }
1569 break;
1570
1571 case XDOMAIN_STATE_LINK_STATE_CHANGE:
1572 ret = tb_xdomain_link_state_change(xd, 2);
1573 if (ret) {
1574 if (ret == -EAGAIN)
1575 goto retry_state;
1576 tb_xdomain_queue_properties(xd);
1577 } else {
1578 tb_xdomain_queue_link_status2(xd);
1579 }
1580 break;
1581
1582 case XDOMAIN_STATE_LINK_STATUS2:
1583 ret = tb_xdomain_get_link_status(xd);
1584 if (ret) {
1585 if (ret == -EAGAIN)
1586 goto retry_state;
1587 tb_xdomain_queue_properties(xd);
1588 } else {
1589 tb_xdomain_queue_bonding_uuid_low(xd);
1590 }
1591 break;
1592
1593 case XDOMAIN_STATE_BONDING_UUID_LOW:
1594 tb_xdomain_lane_bonding_enable(xd);
1595 tb_xdomain_queue_properties(xd);
1596 break;
1597
1598 case XDOMAIN_STATE_BONDING_UUID_HIGH:
1599 if (tb_xdomain_bond_lanes_uuid_high(xd) == -EAGAIN)
1600 goto retry_state;
1601 tb_xdomain_queue_properties(xd);
1602 break;
1603
1604 case XDOMAIN_STATE_PROPERTIES:
1605 ret = tb_xdomain_get_properties(xd);
1606 if (ret) {
1607 if (ret == -EAGAIN)
1608 goto retry_state;
1609 xd->state = XDOMAIN_STATE_ERROR;
1610 } else {
1611 xd->state = XDOMAIN_STATE_ENUMERATED;
1612 }
1613 break;
1614
1615 case XDOMAIN_STATE_ENUMERATED:
1616 tb_xdomain_queue_properties(xd);
1617 break;
1618
1619 case XDOMAIN_STATE_ERROR:
1620 break;
1621
1622 default:
1623 dev_warn(&xd->dev, "unexpected state %d\n", state);
1624 break;
1625 }
1626
1627 return;
1628
1629retry_state:
1630 queue_delayed_work(xd->tb->wq, &xd->state_work,
1631 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
d1ff7024
MW
1632}
1633
1634static void tb_xdomain_properties_changed(struct work_struct *work)
1635{
1636 struct tb_xdomain *xd = container_of(work, typeof(*xd),
1637 properties_changed_work.work);
1638 int ret;
1639
d29c59b1
MW
1640 dev_dbg(&xd->dev, "sending properties changed notification\n");
1641
d1ff7024
MW
1642 ret = tb_xdp_properties_changed_request(xd->tb->ctl, xd->route,
1643 xd->properties_changed_retries, xd->local_uuid);
1644 if (ret) {
d29c59b1
MW
1645 if (xd->properties_changed_retries-- > 0) {
1646 dev_dbg(&xd->dev,
1647 "failed to send properties changed notification, retrying\n");
d1ff7024
MW
1648 queue_delayed_work(xd->tb->wq,
1649 &xd->properties_changed_work,
8e1de704 1650 msecs_to_jiffies(XDOMAIN_DEFAULT_TIMEOUT));
d29c59b1
MW
1651 }
1652 dev_err(&xd->dev, "failed to send properties changed notification\n");
d1ff7024
MW
1653 return;
1654 }
1655
8e1de704 1656 xd->properties_changed_retries = XDOMAIN_RETRIES;
d1ff7024
MW
1657}
1658
1659static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1660 char *buf)
1661{
1662 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1663
8283fb57 1664 return sysfs_emit(buf, "%#x\n", xd->device);
d1ff7024
MW
1665}
1666static DEVICE_ATTR_RO(device);
1667
1668static ssize_t
1669device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1670{
1671 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1672 int ret;
1673
1674 if (mutex_lock_interruptible(&xd->lock))
1675 return -ERESTARTSYS;
8283fb57 1676 ret = sysfs_emit(buf, "%s\n", xd->device_name ?: "");
d1ff7024
MW
1677 mutex_unlock(&xd->lock);
1678
1679 return ret;
1680}
1681static DEVICE_ATTR_RO(device_name);
1682
46b494f2
MW
1683static ssize_t maxhopid_show(struct device *dev, struct device_attribute *attr,
1684 char *buf)
1685{
1686 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1687
8283fb57 1688 return sysfs_emit(buf, "%d\n", xd->remote_max_hopid);
46b494f2
MW
1689}
1690static DEVICE_ATTR_RO(maxhopid);
1691
d1ff7024
MW
1692static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1693 char *buf)
1694{
1695 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1696
8283fb57 1697 return sysfs_emit(buf, "%#x\n", xd->vendor);
d1ff7024
MW
1698}
1699static DEVICE_ATTR_RO(vendor);
1700
1701static ssize_t
1702vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1703{
1704 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1705 int ret;
1706
1707 if (mutex_lock_interruptible(&xd->lock))
1708 return -ERESTARTSYS;
8283fb57 1709 ret = sysfs_emit(buf, "%s\n", xd->vendor_name ?: "");
d1ff7024
MW
1710 mutex_unlock(&xd->lock);
1711
1712 return ret;
1713}
1714static DEVICE_ATTR_RO(vendor_name);
1715
1716static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1717 char *buf)
1718{
1719 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1720
8283fb57 1721 return sysfs_emit(buf, "%pUb\n", xd->remote_uuid);
d1ff7024
MW
1722}
1723static DEVICE_ATTR_RO(unique_id);
1724
4210d50f
IH
1725static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1726 char *buf)
1727{
1728 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1729
8283fb57 1730 return sysfs_emit(buf, "%u.0 Gb/s\n", xd->link_speed);
4210d50f
IH
1731}
1732
1733static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1734static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1735
1736static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1737 char *buf)
1738{
1739 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1740
8283fb57 1741 return sysfs_emit(buf, "%u\n", xd->link_width);
4210d50f
IH
1742}
1743
1744static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1745static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1746
d1ff7024
MW
1747static struct attribute *xdomain_attrs[] = {
1748 &dev_attr_device.attr,
1749 &dev_attr_device_name.attr,
46b494f2 1750 &dev_attr_maxhopid.attr,
4210d50f
IH
1751 &dev_attr_rx_lanes.attr,
1752 &dev_attr_rx_speed.attr,
1753 &dev_attr_tx_lanes.attr,
1754 &dev_attr_tx_speed.attr,
d1ff7024
MW
1755 &dev_attr_unique_id.attr,
1756 &dev_attr_vendor.attr,
1757 &dev_attr_vendor_name.attr,
1758 NULL,
1759};
1760
6889e00f 1761static const struct attribute_group xdomain_attr_group = {
d1ff7024
MW
1762 .attrs = xdomain_attrs,
1763};
1764
1765static const struct attribute_group *xdomain_attr_groups[] = {
1766 &xdomain_attr_group,
1767 NULL,
1768};
1769
1770static void tb_xdomain_release(struct device *dev)
1771{
1772 struct tb_xdomain *xd = container_of(dev, struct tb_xdomain, dev);
1773
1774 put_device(xd->dev.parent);
1775
46b494f2
MW
1776 kfree(xd->local_property_block);
1777 tb_property_free_dir(xd->remote_properties);
180b0689
MW
1778 ida_destroy(&xd->out_hopids);
1779 ida_destroy(&xd->in_hopids);
d1ff7024
MW
1780 ida_destroy(&xd->service_ids);
1781
1782 kfree(xd->local_uuid);
1783 kfree(xd->remote_uuid);
1784 kfree(xd->device_name);
1785 kfree(xd->vendor_name);
1786 kfree(xd);
1787}
1788
1789static void start_handshake(struct tb_xdomain *xd)
1790{
8e1de704
MW
1791 xd->state = XDOMAIN_STATE_INIT;
1792 queue_delayed_work(xd->tb->wq, &xd->state_work,
1793 msecs_to_jiffies(XDOMAIN_SHORT_TIMEOUT));
d1ff7024
MW
1794}
1795
1796static void stop_handshake(struct tb_xdomain *xd)
1797{
d1ff7024 1798 cancel_delayed_work_sync(&xd->properties_changed_work);
8e1de704
MW
1799 cancel_delayed_work_sync(&xd->state_work);
1800 xd->properties_changed_retries = 0;
1801 xd->state_retries = 0;
d1ff7024
MW
1802}
1803
1804static int __maybe_unused tb_xdomain_suspend(struct device *dev)
1805{
1806 stop_handshake(tb_to_xdomain(dev));
1807 return 0;
1808}
1809
1810static int __maybe_unused tb_xdomain_resume(struct device *dev)
1811{
8ccbed24 1812 start_handshake(tb_to_xdomain(dev));
d1ff7024
MW
1813 return 0;
1814}
1815
1816static const struct dev_pm_ops tb_xdomain_pm_ops = {
1817 SET_SYSTEM_SLEEP_PM_OPS(tb_xdomain_suspend, tb_xdomain_resume)
1818};
1819
1820struct device_type tb_xdomain_type = {
1821 .name = "thunderbolt_xdomain",
1822 .release = tb_xdomain_release,
1823 .pm = &tb_xdomain_pm_ops,
1824};
1825EXPORT_SYMBOL_GPL(tb_xdomain_type);
1826
1827/**
1828 * tb_xdomain_alloc() - Allocate new XDomain object
1829 * @tb: Domain where the XDomain belongs
1830 * @parent: Parent device (the switch through the connection to the
1831 * other domain is reached).
1832 * @route: Route string used to reach the other domain
1833 * @local_uuid: Our local domain UUID
3b4b3235 1834 * @remote_uuid: UUID of the other domain (optional)
d1ff7024
MW
1835 *
1836 * Allocates new XDomain structure and returns pointer to that. The
1837 * object must be released by calling tb_xdomain_put().
1838 */
1839struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1840 u64 route, const uuid_t *local_uuid,
1841 const uuid_t *remote_uuid)
1842{
b0407983 1843 struct tb_switch *parent_sw = tb_to_switch(parent);
d1ff7024 1844 struct tb_xdomain *xd;
b0407983
MW
1845 struct tb_port *down;
1846
1847 /* Make sure the downstream domain is accessible */
1848 down = tb_port_at(route, parent_sw);
1849 tb_port_unlock(down);
d1ff7024
MW
1850
1851 xd = kzalloc(sizeof(*xd), GFP_KERNEL);
1852 if (!xd)
1853 return NULL;
1854
1855 xd->tb = tb;
1856 xd->route = route;
46b494f2 1857 xd->local_max_hopid = down->config.max_in_hop_id;
d1ff7024 1858 ida_init(&xd->service_ids);
180b0689
MW
1859 ida_init(&xd->in_hopids);
1860 ida_init(&xd->out_hopids);
d1ff7024 1861 mutex_init(&xd->lock);
8e1de704 1862 INIT_DELAYED_WORK(&xd->state_work, tb_xdomain_state_work);
d1ff7024
MW
1863 INIT_DELAYED_WORK(&xd->properties_changed_work,
1864 tb_xdomain_properties_changed);
1865
1866 xd->local_uuid = kmemdup(local_uuid, sizeof(uuid_t), GFP_KERNEL);
1867 if (!xd->local_uuid)
1868 goto err_free;
1869
3b4b3235
MW
1870 if (remote_uuid) {
1871 xd->remote_uuid = kmemdup(remote_uuid, sizeof(uuid_t),
1872 GFP_KERNEL);
1873 if (!xd->remote_uuid)
1874 goto err_free_local_uuid;
1875 } else {
1876 xd->needs_uuid = true;
8e1de704 1877 xd->bonding_possible = !!down->dual_link_port;
3b4b3235 1878 }
d1ff7024
MW
1879
1880 device_initialize(&xd->dev);
1881 xd->dev.parent = get_device(parent);
1882 xd->dev.bus = &tb_bus_type;
1883 xd->dev.type = &tb_xdomain_type;
1884 xd->dev.groups = xdomain_attr_groups;
1885 dev_set_name(&xd->dev, "%u-%llx", tb->index, route);
1886
d29c59b1
MW
1887 dev_dbg(&xd->dev, "local UUID %pUb\n", local_uuid);
1888 if (remote_uuid)
1889 dev_dbg(&xd->dev, "remote UUID %pUb\n", remote_uuid);
1890
2d8ff0b5
MW
1891 /*
1892 * This keeps the DMA powered on as long as we have active
1893 * connection to another host.
1894 */
1895 pm_runtime_set_active(&xd->dev);
1896 pm_runtime_get_noresume(&xd->dev);
1897 pm_runtime_enable(&xd->dev);
1898
d1ff7024
MW
1899 return xd;
1900
1901err_free_local_uuid:
1902 kfree(xd->local_uuid);
1903err_free:
1904 kfree(xd);
1905
1906 return NULL;
1907}
1908
1909/**
1910 * tb_xdomain_add() - Add XDomain to the bus
1911 * @xd: XDomain to add
1912 *
1913 * This function starts XDomain discovery protocol handshake and
1914 * eventually adds the XDomain to the bus. After calling this function
1915 * the caller needs to call tb_xdomain_remove() in order to remove and
1916 * release the object regardless whether the handshake succeeded or not.
1917 */
1918void tb_xdomain_add(struct tb_xdomain *xd)
1919{
1920 /* Start exchanging properties with the other host */
1921 start_handshake(xd);
1922}
1923
1924static int unregister_service(struct device *dev, void *data)
1925{
1926 device_unregister(dev);
1927 return 0;
1928}
1929
1930/**
1931 * tb_xdomain_remove() - Remove XDomain from the bus
1932 * @xd: XDomain to remove
1933 *
1934 * This will stop all ongoing configuration work and remove the XDomain
1935 * along with any services from the bus. When the last reference to @xd
1936 * is released the object will be released as well.
1937 */
1938void tb_xdomain_remove(struct tb_xdomain *xd)
1939{
d0f1e0c2
MW
1940 tb_xdomain_debugfs_remove(xd);
1941
d1ff7024
MW
1942 stop_handshake(xd);
1943
1944 device_for_each_child_reverse(&xd->dev, xd, unregister_service);
1945
2d8ff0b5
MW
1946 /*
1947 * Undo runtime PM here explicitly because it is possible that
1948 * the XDomain was never added to the bus and thus device_del()
1949 * is not called for it (device_del() would handle this otherwise).
1950 */
1951 pm_runtime_disable(&xd->dev);
1952 pm_runtime_put_noidle(&xd->dev);
1953 pm_runtime_set_suspended(&xd->dev);
1954
d29c59b1 1955 if (!device_is_registered(&xd->dev)) {
d1ff7024 1956 put_device(&xd->dev);
d29c59b1
MW
1957 } else {
1958 dev_info(&xd->dev, "host disconnected\n");
d1ff7024 1959 device_unregister(&xd->dev);
d29c59b1 1960 }
d1ff7024
MW
1961}
1962
5cc0df9c
IH
1963/**
1964 * tb_xdomain_lane_bonding_enable() - Enable lane bonding on XDomain
1965 * @xd: XDomain connection
1966 *
1967 * Lane bonding is disabled by default for XDomains. This function tries
1968 * to enable bonding by first enabling the port and waiting for the CL0
1969 * state.
1970 *
1971 * Return: %0 in case of success and negative errno in case of error.
1972 */
1973int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd)
1974{
1975 struct tb_port *port;
1976 int ret;
1977
1978 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
1979 if (!port->dual_link_port)
1980 return -ENODEV;
1981
1982 ret = tb_port_enable(port->dual_link_port);
1983 if (ret)
1984 return ret;
1985
1986 ret = tb_wait_for_port(port->dual_link_port, true);
1987 if (ret < 0)
1988 return ret;
1989 if (!ret)
1990 return -ENOTCONN;
1991
1992 ret = tb_port_lane_bonding_enable(port);
1993 if (ret) {
1994 tb_port_warn(port, "failed to enable lane bonding\n");
1995 return ret;
1996 }
1997
8e1de704 1998 ret = tb_port_wait_for_link_width(port, 2, XDOMAIN_BONDING_TIMEOUT);
e7051bea 1999 if (ret) {
8e1de704 2000 tb_port_warn(port, "failed to enable lane bonding\n");
e7051bea
MW
2001 return ret;
2002 }
2003
69fea377 2004 tb_port_update_credits(port);
5cc0df9c
IH
2005 tb_xdomain_update_link_attributes(xd);
2006
2007 dev_dbg(&xd->dev, "lane bonding enabled\n");
2008 return 0;
2009}
2010EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_enable);
2011
2012/**
2013 * tb_xdomain_lane_bonding_disable() - Disable lane bonding
2014 * @xd: XDomain connection
2015 *
2016 * Lane bonding is disabled by default for XDomains. If bonding has been
2017 * enabled, this function can be used to disable it.
2018 */
2019void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd)
2020{
2021 struct tb_port *port;
2022
2023 port = tb_port_at(xd->route, tb_xdomain_parent(xd));
2024 if (port->dual_link_port) {
2025 tb_port_lane_bonding_disable(port);
e7051bea
MW
2026 if (tb_port_wait_for_link_width(port, 1, 100) == -ETIMEDOUT)
2027 tb_port_warn(port, "timeout disabling lane bonding\n");
5cc0df9c 2028 tb_port_disable(port->dual_link_port);
69fea377 2029 tb_port_update_credits(port);
5cc0df9c
IH
2030 tb_xdomain_update_link_attributes(xd);
2031
2032 dev_dbg(&xd->dev, "lane bonding disabled\n");
2033 }
2034}
2035EXPORT_SYMBOL_GPL(tb_xdomain_lane_bonding_disable);
2036
d1ff7024 2037/**
180b0689 2038 * tb_xdomain_alloc_in_hopid() - Allocate input HopID for tunneling
d1ff7024 2039 * @xd: XDomain connection
180b0689 2040 * @hopid: Preferred HopID or %-1 for next available
d1ff7024 2041 *
180b0689
MW
2042 * Returns allocated HopID or negative errno. Specifically returns
2043 * %-ENOSPC if there are no more available HopIDs. Returned HopID is
2044 * guaranteed to be within range supported by the input lane adapter.
2045 * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
d1ff7024 2046 */
180b0689 2047int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid)
d1ff7024 2048{
180b0689
MW
2049 if (hopid < 0)
2050 hopid = TB_PATH_MIN_HOPID;
2051 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->local_max_hopid)
2052 return -EINVAL;
d1ff7024 2053
180b0689
MW
2054 return ida_alloc_range(&xd->in_hopids, hopid, xd->local_max_hopid,
2055 GFP_KERNEL);
2056}
2057EXPORT_SYMBOL_GPL(tb_xdomain_alloc_in_hopid);
d1ff7024 2058
180b0689
MW
2059/**
2060 * tb_xdomain_alloc_out_hopid() - Allocate output HopID for tunneling
2061 * @xd: XDomain connection
2062 * @hopid: Preferred HopID or %-1 for next available
2063 *
2064 * Returns allocated HopID or negative errno. Specifically returns
2065 * %-ENOSPC if there are no more available HopIDs. Returned HopID is
2066 * guaranteed to be within range supported by the output lane adapter.
2067 * Call tb_xdomain_release_in_hopid() to release the allocated HopID.
2068 */
2069int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid)
2070{
2071 if (hopid < 0)
2072 hopid = TB_PATH_MIN_HOPID;
2073 if (hopid < TB_PATH_MIN_HOPID || hopid > xd->remote_max_hopid)
2074 return -EINVAL;
d1ff7024 2075
180b0689
MW
2076 return ida_alloc_range(&xd->out_hopids, hopid, xd->remote_max_hopid,
2077 GFP_KERNEL);
2078}
2079EXPORT_SYMBOL_GPL(tb_xdomain_alloc_out_hopid);
d1ff7024 2080
180b0689
MW
2081/**
2082 * tb_xdomain_release_in_hopid() - Release input HopID
2083 * @xd: XDomain connection
2084 * @hopid: HopID to release
2085 */
2086void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid)
2087{
2088 ida_free(&xd->in_hopids, hopid);
2089}
2090EXPORT_SYMBOL_GPL(tb_xdomain_release_in_hopid);
d1ff7024 2091
180b0689
MW
2092/**
2093 * tb_xdomain_release_out_hopid() - Release output HopID
2094 * @xd: XDomain connection
2095 * @hopid: HopID to release
2096 */
2097void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid)
2098{
2099 ida_free(&xd->out_hopids, hopid);
2100}
2101EXPORT_SYMBOL_GPL(tb_xdomain_release_out_hopid);
d1ff7024 2102
180b0689
MW
2103/**
2104 * tb_xdomain_enable_paths() - Enable DMA paths for XDomain connection
2105 * @xd: XDomain connection
2106 * @transmit_path: HopID we are using to send out packets
2107 * @transmit_ring: DMA ring used to send out packets
2108 * @receive_path: HopID the other end is using to send packets to us
2109 * @receive_ring: DMA ring used to receive packets from @receive_path
2110 *
2111 * The function enables DMA paths accordingly so that after successful
2112 * return the caller can send and receive packets using high-speed DMA
2113 * path. If a transmit or receive path is not needed, pass %-1 for those
2114 * parameters.
2115 *
2116 * Return: %0 in case of success and negative errno in case of error
2117 */
2118int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
2119 int transmit_ring, int receive_path,
2120 int receive_ring)
2121{
2122 return tb_domain_approve_xdomain_paths(xd->tb, xd, transmit_path,
2123 transmit_ring, receive_path,
2124 receive_ring);
d1ff7024
MW
2125}
2126EXPORT_SYMBOL_GPL(tb_xdomain_enable_paths);
2127
2128/**
2129 * tb_xdomain_disable_paths() - Disable DMA paths for XDomain connection
2130 * @xd: XDomain connection
180b0689
MW
2131 * @transmit_path: HopID we are using to send out packets
2132 * @transmit_ring: DMA ring used to send out packets
2133 * @receive_path: HopID the other end is using to send packets to us
2134 * @receive_ring: DMA ring used to receive packets from @receive_path
d1ff7024
MW
2135 *
2136 * This does the opposite of tb_xdomain_enable_paths(). After call to
180b0689
MW
2137 * this the caller is not expected to use the rings anymore. Passing %-1
2138 * as path/ring parameter means don't care. Normally the callers should
2139 * pass the same values here as they do when paths are enabled.
d1ff7024
MW
2140 *
2141 * Return: %0 in case of success and negative errno in case of error
2142 */
180b0689
MW
2143int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
2144 int transmit_ring, int receive_path,
2145 int receive_ring)
d1ff7024 2146{
180b0689
MW
2147 return tb_domain_disconnect_xdomain_paths(xd->tb, xd, transmit_path,
2148 transmit_ring, receive_path,
2149 receive_ring);
d1ff7024
MW
2150}
2151EXPORT_SYMBOL_GPL(tb_xdomain_disable_paths);
2152
2153struct tb_xdomain_lookup {
2154 const uuid_t *uuid;
2155 u8 link;
2156 u8 depth;
484cb153 2157 u64 route;
d1ff7024
MW
2158};
2159
2160static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
2161 const struct tb_xdomain_lookup *lookup)
2162{
b433d010 2163 struct tb_port *port;
d1ff7024 2164
b433d010 2165 tb_switch_for_each_port(sw, port) {
d1ff7024
MW
2166 struct tb_xdomain *xd;
2167
d1ff7024
MW
2168 if (port->xdomain) {
2169 xd = port->xdomain;
2170
2171 if (lookup->uuid) {
3b4b3235
MW
2172 if (xd->remote_uuid &&
2173 uuid_equal(xd->remote_uuid, lookup->uuid))
d1ff7024 2174 return xd;
484cb153
RM
2175 } else if (lookup->link &&
2176 lookup->link == xd->link &&
d1ff7024
MW
2177 lookup->depth == xd->depth) {
2178 return xd;
484cb153
RM
2179 } else if (lookup->route &&
2180 lookup->route == xd->route) {
2181 return xd;
d1ff7024 2182 }
dfe40ca4 2183 } else if (tb_port_has_remote(port)) {
d1ff7024
MW
2184 xd = switch_find_xdomain(port->remote->sw, lookup);
2185 if (xd)
2186 return xd;
2187 }
2188 }
2189
2190 return NULL;
2191}
2192
2193/**
2194 * tb_xdomain_find_by_uuid() - Find an XDomain by UUID
2195 * @tb: Domain where the XDomain belongs to
2196 * @uuid: UUID to look for
2197 *
2198 * Finds XDomain by walking through the Thunderbolt topology below @tb.
2199 * The returned XDomain will have its reference count increased so the
2200 * caller needs to call tb_xdomain_put() when it is done with the
2201 * object.
2202 *
2203 * This will find all XDomains including the ones that are not yet added
2204 * to the bus (handshake is still in progress).
2205 *
2206 * The caller needs to hold @tb->lock.
2207 */
2208struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid)
2209{
2210 struct tb_xdomain_lookup lookup;
2211 struct tb_xdomain *xd;
2212
2213 memset(&lookup, 0, sizeof(lookup));
2214 lookup.uuid = uuid;
2215
2216 xd = switch_find_xdomain(tb->root_switch, &lookup);
484cb153 2217 return tb_xdomain_get(xd);
d1ff7024
MW
2218}
2219EXPORT_SYMBOL_GPL(tb_xdomain_find_by_uuid);
2220
2221/**
2222 * tb_xdomain_find_by_link_depth() - Find an XDomain by link and depth
2223 * @tb: Domain where the XDomain belongs to
2224 * @link: Root switch link number
2225 * @depth: Depth in the link
2226 *
2227 * Finds XDomain by walking through the Thunderbolt topology below @tb.
2228 * The returned XDomain will have its reference count increased so the
2229 * caller needs to call tb_xdomain_put() when it is done with the
2230 * object.
2231 *
2232 * This will find all XDomains including the ones that are not yet added
2233 * to the bus (handshake is still in progress).
2234 *
2235 * The caller needs to hold @tb->lock.
2236 */
2237struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
2238 u8 depth)
2239{
2240 struct tb_xdomain_lookup lookup;
2241 struct tb_xdomain *xd;
2242
2243 memset(&lookup, 0, sizeof(lookup));
2244 lookup.link = link;
2245 lookup.depth = depth;
2246
2247 xd = switch_find_xdomain(tb->root_switch, &lookup);
484cb153
RM
2248 return tb_xdomain_get(xd);
2249}
d1ff7024 2250
484cb153
RM
2251/**
2252 * tb_xdomain_find_by_route() - Find an XDomain by route string
2253 * @tb: Domain where the XDomain belongs to
2254 * @route: XDomain route string
2255 *
2256 * Finds XDomain by walking through the Thunderbolt topology below @tb.
2257 * The returned XDomain will have its reference count increased so the
2258 * caller needs to call tb_xdomain_put() when it is done with the
2259 * object.
2260 *
2261 * This will find all XDomains including the ones that are not yet added
2262 * to the bus (handshake is still in progress).
2263 *
2264 * The caller needs to hold @tb->lock.
2265 */
2266struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route)
2267{
2268 struct tb_xdomain_lookup lookup;
2269 struct tb_xdomain *xd;
2270
2271 memset(&lookup, 0, sizeof(lookup));
2272 lookup.route = route;
2273
2274 xd = switch_find_xdomain(tb->root_switch, &lookup);
2275 return tb_xdomain_get(xd);
d1ff7024 2276}
484cb153 2277EXPORT_SYMBOL_GPL(tb_xdomain_find_by_route);
d1ff7024
MW
2278
2279bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
2280 const void *buf, size_t size)
2281{
2282 const struct tb_protocol_handler *handler, *tmp;
2283 const struct tb_xdp_header *hdr = buf;
2284 unsigned int length;
2285 int ret = 0;
2286
2287 /* We expect the packet is at least size of the header */
2288 length = hdr->xd_hdr.length_sn & TB_XDOMAIN_LENGTH_MASK;
2289 if (length != size / 4 - sizeof(hdr->xd_hdr) / 4)
2290 return true;
2291 if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
2292 return true;
2293
2294 /*
2295 * Handle XDomain discovery protocol packets directly here. For
2296 * other protocols (based on their UUID) we call registered
2297 * handlers in turn.
2298 */
2299 if (uuid_equal(&hdr->uuid, &tb_xdp_uuid)) {
48f40b96
AP
2300 if (type == TB_CFG_PKG_XDOMAIN_REQ)
2301 return tb_xdp_schedule_request(tb, hdr, size);
d1ff7024
MW
2302 return false;
2303 }
2304
2305 mutex_lock(&xdomain_lock);
2306 list_for_each_entry_safe(handler, tmp, &protocol_handlers, list) {
2307 if (!uuid_equal(&hdr->uuid, handler->uuid))
2308 continue;
2309
2310 mutex_unlock(&xdomain_lock);
2311 ret = handler->callback(buf, size, handler->data);
2312 mutex_lock(&xdomain_lock);
2313
2314 if (ret)
2315 break;
2316 }
2317 mutex_unlock(&xdomain_lock);
2318
2319 return ret > 0;
2320}
2321
d1ff7024
MW
2322static int update_xdomain(struct device *dev, void *data)
2323{
2324 struct tb_xdomain *xd;
2325
2326 xd = tb_to_xdomain(dev);
2327 if (xd) {
2328 queue_delayed_work(xd->tb->wq, &xd->properties_changed_work,
2329 msecs_to_jiffies(50));
2330 }
2331
2332 return 0;
2333}
2334
2335static void update_all_xdomains(void)
2336{
2337 bus_for_each_dev(&tb_bus_type, NULL, NULL, update_xdomain);
2338}
2339
2340static bool remove_directory(const char *key, const struct tb_property_dir *dir)
2341{
2342 struct tb_property *p;
2343
2344 p = tb_property_find(xdomain_property_dir, key,
2345 TB_PROPERTY_TYPE_DIRECTORY);
2346 if (p && p->value.dir == dir) {
2347 tb_property_remove(p);
2348 return true;
2349 }
2350 return false;
2351}
2352
2353/**
2354 * tb_register_property_dir() - Register property directory to the host
2355 * @key: Key (name) of the directory to add
2356 * @dir: Directory to add
2357 *
2358 * Service drivers can use this function to add new property directory
2359 * to the host available properties. The other connected hosts are
2360 * notified so they can re-read properties of this host if they are
2361 * interested.
2362 *
2363 * Return: %0 on success and negative errno on failure
2364 */
2365int tb_register_property_dir(const char *key, struct tb_property_dir *dir)
2366{
2367 int ret;
2368
acb40d84
MW
2369 if (WARN_ON(!xdomain_property_dir))
2370 return -EAGAIN;
2371
d1ff7024
MW
2372 if (!key || strlen(key) > 8)
2373 return -EINVAL;
2374
2375 mutex_lock(&xdomain_lock);
2376 if (tb_property_find(xdomain_property_dir, key,
2377 TB_PROPERTY_TYPE_DIRECTORY)) {
2378 ret = -EEXIST;
2379 goto err_unlock;
2380 }
2381
2382 ret = tb_property_add_dir(xdomain_property_dir, key, dir);
2383 if (ret)
2384 goto err_unlock;
2385
46b494f2 2386 xdomain_property_block_gen++;
d1ff7024
MW
2387
2388 mutex_unlock(&xdomain_lock);
2389 update_all_xdomains();
2390 return 0;
2391
2392err_unlock:
2393 mutex_unlock(&xdomain_lock);
2394 return ret;
2395}
2396EXPORT_SYMBOL_GPL(tb_register_property_dir);
2397
2398/**
2399 * tb_unregister_property_dir() - Removes property directory from host
2400 * @key: Key (name) of the directory
2401 * @dir: Directory to remove
2402 *
2403 * This will remove the existing directory from this host and notify the
2404 * connected hosts about the change.
2405 */
2406void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir)
2407{
2408 int ret = 0;
2409
2410 mutex_lock(&xdomain_lock);
2411 if (remove_directory(key, dir))
46b494f2 2412 xdomain_property_block_gen++;
d1ff7024
MW
2413 mutex_unlock(&xdomain_lock);
2414
2415 if (!ret)
2416 update_all_xdomains();
2417}
2418EXPORT_SYMBOL_GPL(tb_unregister_property_dir);
2419
2420int tb_xdomain_init(void)
2421{
d1ff7024
MW
2422 xdomain_property_dir = tb_property_create_dir(NULL);
2423 if (!xdomain_property_dir)
2424 return -ENOMEM;
2425
2426 /*
2427 * Initialize standard set of properties without any service
2428 * directories. Those will be added by service drivers
2429 * themselves when they are loaded.
8a00c67e 2430 *
46b494f2
MW
2431 * Rest of the properties are filled dynamically based on these
2432 * when the P2P connection is made.
d1ff7024
MW
2433 */
2434 tb_property_add_immediate(xdomain_property_dir, "vendorid",
2435 PCI_VENDOR_ID_INTEL);
2436 tb_property_add_text(xdomain_property_dir, "vendorid", "Intel Corp.");
2437 tb_property_add_immediate(xdomain_property_dir, "deviceid", 0x1);
d1ff7024
MW
2438 tb_property_add_immediate(xdomain_property_dir, "devicerv", 0x80000100);
2439
a251c17a 2440 xdomain_property_block_gen = get_random_u32();
8a00c67e 2441 return 0;
d1ff7024
MW
2442}
2443
2444void tb_xdomain_exit(void)
2445{
d1ff7024
MW
2446 tb_property_free_dir(xdomain_property_dir);
2447}