1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - control channel and configuration commands
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2018, Intel Corporation
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/pci.h>
13 #include <linux/dmapool.h>
14 #include <linux/workqueue.h>
18 #define CREATE_TRACE_POINTS
21 #define TB_CTL_RX_PKG_COUNT 10
22 #define TB_CTL_RETRIES 4
25 * struct tb_ctl - Thunderbolt control channel
26 * @nhi: Pointer to the NHI structure
29 * @frame_pool: DMA pool for control messages
30 * @rx_packets: Received control messages
31 * @request_queue_lock: Lock protecting @request_queue
32 * @request_queue: List of outstanding requests
33 * @running: Is the control channel running at the moment
34 * @timeout_msec: Default timeout for non-raw control messages
35 * @callback: Callback called when hotplug message is received
36 * @callback_data: Data passed to @callback
37 * @index: Domain number. This will be output with the trace record.
44 struct dma_pool
*frame_pool
;
45 struct ctl_pkg
*rx_packets
[TB_CTL_RX_PKG_COUNT
];
46 struct mutex request_queue_lock
;
47 struct list_head request_queue
;
58 #define tb_ctl_WARN(ctl, format, arg...) \
59 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
61 #define tb_ctl_err(ctl, format, arg...) \
62 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
64 #define tb_ctl_warn(ctl, format, arg...) \
65 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
67 #define tb_ctl_info(ctl, format, arg...) \
68 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
70 #define tb_ctl_dbg(ctl, format, arg...) \
71 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
73 #define tb_ctl_dbg_once(ctl, format, arg...) \
74 dev_dbg_once(&(ctl)->nhi->pdev->dev, format, ## arg)
76 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue
);
77 /* Serializes access to request kref_get/put */
78 static DEFINE_MUTEX(tb_cfg_request_lock
);
81 * tb_cfg_request_alloc() - Allocates a new config request
83 * This is refcounted object so when you are done with this, call
84 * tb_cfg_request_put() to it.
86 struct tb_cfg_request
*tb_cfg_request_alloc(void)
88 struct tb_cfg_request
*req
;
90 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
94 kref_init(&req
->kref
);
100 * tb_cfg_request_get() - Increase refcount of a request
101 * @req: Request whose refcount is increased
103 void tb_cfg_request_get(struct tb_cfg_request
*req
)
105 mutex_lock(&tb_cfg_request_lock
);
106 kref_get(&req
->kref
);
107 mutex_unlock(&tb_cfg_request_lock
);
110 static void tb_cfg_request_destroy(struct kref
*kref
)
112 struct tb_cfg_request
*req
= container_of(kref
, typeof(*req
), kref
);
118 * tb_cfg_request_put() - Decrease refcount and possibly release the request
119 * @req: Request whose refcount is decreased
121 * Call this function when you are done with the request. When refcount
122 * goes to %0 the object is released.
124 void tb_cfg_request_put(struct tb_cfg_request
*req
)
126 mutex_lock(&tb_cfg_request_lock
);
127 kref_put(&req
->kref
, tb_cfg_request_destroy
);
128 mutex_unlock(&tb_cfg_request_lock
);
131 static int tb_cfg_request_enqueue(struct tb_ctl
*ctl
,
132 struct tb_cfg_request
*req
)
134 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
));
137 mutex_lock(&ctl
->request_queue_lock
);
139 mutex_unlock(&ctl
->request_queue_lock
);
143 list_add_tail(&req
->list
, &ctl
->request_queue
);
144 set_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
145 mutex_unlock(&ctl
->request_queue_lock
);
149 static void tb_cfg_request_dequeue(struct tb_cfg_request
*req
)
151 struct tb_ctl
*ctl
= req
->ctl
;
153 mutex_lock(&ctl
->request_queue_lock
);
154 list_del(&req
->list
);
155 clear_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
156 if (test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
157 wake_up(&tb_cfg_request_cancel_queue
);
158 mutex_unlock(&ctl
->request_queue_lock
);
161 static bool tb_cfg_request_is_active(struct tb_cfg_request
*req
)
163 return test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
166 static struct tb_cfg_request
*
167 tb_cfg_request_find(struct tb_ctl
*ctl
, struct ctl_pkg
*pkg
)
169 struct tb_cfg_request
*req
= NULL
, *iter
;
171 mutex_lock(&pkg
->ctl
->request_queue_lock
);
172 list_for_each_entry(iter
, &pkg
->ctl
->request_queue
, list
) {
173 tb_cfg_request_get(iter
);
174 if (iter
->match(iter
, pkg
)) {
178 tb_cfg_request_put(iter
);
180 mutex_unlock(&pkg
->ctl
->request_queue_lock
);
185 /* utility functions */
188 static int check_header(const struct ctl_pkg
*pkg
, u32 len
,
189 enum tb_cfg_pkg_type type
, u64 route
)
191 struct tb_cfg_header
*header
= pkg
->buffer
;
193 /* check frame, TODO: frame flags */
194 if (WARN(len
!= pkg
->frame
.size
,
195 "wrong framesize (expected %#x, got %#x)\n",
196 len
, pkg
->frame
.size
))
198 if (WARN(type
!= pkg
->frame
.eof
, "wrong eof (expected %#x, got %#x)\n",
199 type
, pkg
->frame
.eof
))
201 if (WARN(pkg
->frame
.sof
, "wrong sof (expected 0x0, got %#x)\n",
206 if (WARN(header
->unknown
!= 1 << 9,
207 "header->unknown is %#x\n", header
->unknown
))
209 if (WARN(route
!= tb_cfg_get_route(header
),
210 "wrong route (expected %llx, got %llx)",
211 route
, tb_cfg_get_route(header
)))
216 static int check_config_address(struct tb_cfg_address addr
,
217 enum tb_cfg_space space
, u32 offset
,
220 if (WARN(addr
.zero
, "addr.zero is %#x\n", addr
.zero
))
222 if (WARN(space
!= addr
.space
, "wrong space (expected %x, got %x\n)",
225 if (WARN(offset
!= addr
.offset
, "wrong offset (expected %x, got %x\n)",
226 offset
, addr
.offset
))
228 if (WARN(length
!= addr
.length
, "wrong space (expected %x, got %x\n)",
229 length
, addr
.length
))
232 * We cannot check addr->port as it is set to the upstream port of the
238 static struct tb_cfg_result
decode_error(const struct ctl_pkg
*response
)
240 struct cfg_error_pkg
*pkg
= response
->buffer
;
241 struct tb_cfg_result res
= { 0 };
242 res
.response_route
= tb_cfg_get_route(&pkg
->header
);
243 res
.response_port
= 0;
244 res
.err
= check_header(response
, sizeof(*pkg
), TB_CFG_PKG_ERROR
,
245 tb_cfg_get_route(&pkg
->header
));
250 res
.tb_error
= pkg
->error
;
251 res
.response_port
= pkg
->port
;
256 static struct tb_cfg_result
parse_header(const struct ctl_pkg
*pkg
, u32 len
,
257 enum tb_cfg_pkg_type type
, u64 route
)
259 struct tb_cfg_header
*header
= pkg
->buffer
;
260 struct tb_cfg_result res
= { 0 };
262 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
263 return decode_error(pkg
);
265 res
.response_port
= 0; /* will be updated later for cfg_read/write */
266 res
.response_route
= tb_cfg_get_route(header
);
267 res
.err
= check_header(pkg
, len
, type
, route
);
271 static void tb_cfg_print_error(struct tb_ctl
*ctl
, enum tb_cfg_space space
,
272 const struct tb_cfg_result
*res
)
274 WARN_ON(res
->err
!= 1);
275 switch (res
->tb_error
) {
276 case TB_CFG_ERROR_PORT_NOT_CONNECTED
:
277 /* Port is not connected. This can happen during surprise
278 * removal. Do not warn. */
280 case TB_CFG_ERROR_INVALID_CONFIG_SPACE
:
282 * Invalid cfg_space/offset/length combination in
283 * cfg_read/cfg_write.
285 tb_ctl_dbg_once(ctl
, "%llx:%x: invalid config space (%u) or offset\n",
286 res
->response_route
, res
->response_port
, space
);
288 case TB_CFG_ERROR_NO_SUCH_PORT
:
290 * - The route contains a non-existent port.
291 * - The route contains a non-PHY port (e.g. PCIe).
292 * - The port in cfg_read/cfg_write does not exist.
294 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Invalid port\n",
295 res
->response_route
, res
->response_port
);
297 case TB_CFG_ERROR_LOOP
:
298 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Route contains a loop\n",
299 res
->response_route
, res
->response_port
);
301 case TB_CFG_ERROR_LOCK
:
302 tb_ctl_warn(ctl
, "%llx:%x: downstream port is locked\n",
303 res
->response_route
, res
->response_port
);
306 /* 5,6,7,9 and 11 are also valid error codes */
307 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Unknown error\n",
308 res
->response_route
, res
->response_port
);
313 static __be32
tb_crc(const void *data
, size_t len
)
315 return cpu_to_be32(~__crc32c_le(~0, data
, len
));
318 static void tb_ctl_pkg_free(struct ctl_pkg
*pkg
)
321 dma_pool_free(pkg
->ctl
->frame_pool
,
322 pkg
->buffer
, pkg
->frame
.buffer_phy
);
327 static struct ctl_pkg
*tb_ctl_pkg_alloc(struct tb_ctl
*ctl
)
329 struct ctl_pkg
*pkg
= kzalloc(sizeof(*pkg
), GFP_KERNEL
);
333 pkg
->buffer
= dma_pool_alloc(ctl
->frame_pool
, GFP_KERNEL
,
334 &pkg
->frame
.buffer_phy
);
345 static void tb_ctl_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
348 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
349 tb_ctl_pkg_free(pkg
);
353 * tb_cfg_tx() - transmit a packet on the control channel
355 * len must be a multiple of four.
357 * Return: Returns 0 on success or an error code on failure.
359 static int tb_ctl_tx(struct tb_ctl
*ctl
, const void *data
, size_t len
,
360 enum tb_cfg_pkg_type type
)
364 if (len
% 4 != 0) { /* required for le->be conversion */
365 tb_ctl_WARN(ctl
, "TX: invalid size: %zu\n", len
);
368 if (len
> TB_FRAME_SIZE
- 4) { /* checksum is 4 bytes */
369 tb_ctl_WARN(ctl
, "TX: packet too large: %zu/%d\n",
370 len
, TB_FRAME_SIZE
- 4);
373 pkg
= tb_ctl_pkg_alloc(ctl
);
376 pkg
->frame
.callback
= tb_ctl_tx_callback
;
377 pkg
->frame
.size
= len
+ 4;
378 pkg
->frame
.sof
= type
;
379 pkg
->frame
.eof
= type
;
381 trace_tb_tx(ctl
->index
, type
, data
, len
);
383 cpu_to_be32_array(pkg
->buffer
, data
, len
/ 4);
384 *(__be32
*) (pkg
->buffer
+ len
) = tb_crc(pkg
->buffer
, len
);
386 res
= tb_ring_tx(ctl
->tx
, &pkg
->frame
);
387 if (res
) /* ring is stopped */
388 tb_ctl_pkg_free(pkg
);
393 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
395 static bool tb_ctl_handle_event(struct tb_ctl
*ctl
, enum tb_cfg_pkg_type type
,
396 struct ctl_pkg
*pkg
, size_t size
)
398 trace_tb_event(ctl
->index
, type
, pkg
->buffer
, size
);
399 return ctl
->callback(ctl
->callback_data
, type
, pkg
->buffer
, size
);
402 static void tb_ctl_rx_submit(struct ctl_pkg
*pkg
)
404 tb_ring_rx(pkg
->ctl
->rx
, &pkg
->frame
); /*
405 * We ignore failures during stop.
406 * All rx packets are referenced
407 * from ctl->rx_packets, so we do
412 static int tb_async_error(const struct ctl_pkg
*pkg
)
414 const struct cfg_error_pkg
*error
= pkg
->buffer
;
416 if (pkg
->frame
.eof
!= TB_CFG_PKG_ERROR
)
419 switch (error
->error
) {
420 case TB_CFG_ERROR_LINK_ERROR
:
421 case TB_CFG_ERROR_HEC_ERROR_DETECTED
:
422 case TB_CFG_ERROR_FLOW_CONTROL_ERROR
:
423 case TB_CFG_ERROR_DP_BW
:
424 case TB_CFG_ERROR_ROP_CMPLT
:
425 case TB_CFG_ERROR_POP_CMPLT
:
426 case TB_CFG_ERROR_PCIE_WAKE
:
427 case TB_CFG_ERROR_DP_CON_CHANGE
:
428 case TB_CFG_ERROR_DPTX_DISCOVERY
:
429 case TB_CFG_ERROR_LINK_RECOVERY
:
430 case TB_CFG_ERROR_ASYM_LINK
:
438 static void tb_ctl_rx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
441 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
442 struct tb_cfg_request
*req
;
447 * ring is stopped, packet is referenced from
451 if (frame
->size
< 4 || frame
->size
% 4 != 0) {
452 tb_ctl_err(pkg
->ctl
, "RX: invalid size %#x, dropping packet\n",
457 frame
->size
-= 4; /* remove checksum */
458 crc32
= tb_crc(pkg
->buffer
, frame
->size
);
459 be32_to_cpu_array(pkg
->buffer
, pkg
->buffer
, frame
->size
/ 4);
461 switch (frame
->eof
) {
462 case TB_CFG_PKG_READ
:
463 case TB_CFG_PKG_WRITE
:
464 case TB_CFG_PKG_ERROR
:
465 case TB_CFG_PKG_OVERRIDE
:
466 case TB_CFG_PKG_RESET
:
467 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
469 "RX: checksum mismatch, dropping packet\n");
472 if (tb_async_error(pkg
)) {
473 tb_ctl_handle_event(pkg
->ctl
, frame
->eof
,
479 case TB_CFG_PKG_EVENT
:
480 case TB_CFG_PKG_XDOMAIN_RESP
:
481 case TB_CFG_PKG_XDOMAIN_REQ
:
482 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
484 "RX: checksum mismatch, dropping packet\n");
488 case TB_CFG_PKG_ICM_EVENT
:
489 if (tb_ctl_handle_event(pkg
->ctl
, frame
->eof
, pkg
, frame
->size
))
498 * The received packet will be processed only if there is an
499 * active request and that the packet is what is expected. This
500 * prevents packets such as replies coming after timeout has
501 * triggered from messing with the active requests.
503 req
= tb_cfg_request_find(pkg
->ctl
, pkg
);
505 trace_tb_rx(pkg
->ctl
->index
, frame
->eof
, pkg
->buffer
, frame
->size
, !req
);
508 if (req
->copy(req
, pkg
))
509 schedule_work(&req
->work
);
510 tb_cfg_request_put(req
);
514 tb_ctl_rx_submit(pkg
);
517 static void tb_cfg_request_work(struct work_struct
*work
)
519 struct tb_cfg_request
*req
= container_of(work
, typeof(*req
), work
);
521 if (!test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
522 req
->callback(req
->callback_data
);
524 tb_cfg_request_dequeue(req
);
525 tb_cfg_request_put(req
);
529 * tb_cfg_request() - Start control request not waiting for it to complete
530 * @ctl: Control channel to use
531 * @req: Request to start
532 * @callback: Callback called when the request is completed
533 * @callback_data: Data to be passed to @callback
535 * This queues @req on the given control channel without waiting for it
536 * to complete. When the request completes @callback is called.
538 int tb_cfg_request(struct tb_ctl
*ctl
, struct tb_cfg_request
*req
,
539 void (*callback
)(void *), void *callback_data
)
544 req
->callback
= callback
;
545 req
->callback_data
= callback_data
;
546 INIT_WORK(&req
->work
, tb_cfg_request_work
);
547 INIT_LIST_HEAD(&req
->list
);
549 tb_cfg_request_get(req
);
550 ret
= tb_cfg_request_enqueue(ctl
, req
);
554 ret
= tb_ctl_tx(ctl
, req
->request
, req
->request_size
,
560 schedule_work(&req
->work
);
565 tb_cfg_request_dequeue(req
);
567 tb_cfg_request_put(req
);
573 * tb_cfg_request_cancel() - Cancel a control request
574 * @req: Request to cancel
575 * @err: Error to assign to the request
577 * This function can be used to cancel ongoing request. It will wait
578 * until the request is not active anymore.
580 void tb_cfg_request_cancel(struct tb_cfg_request
*req
, int err
)
582 set_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
);
583 schedule_work(&req
->work
);
584 wait_event(tb_cfg_request_cancel_queue
, !tb_cfg_request_is_active(req
));
585 req
->result
.err
= err
;
588 static void tb_cfg_request_complete(void *data
)
594 * tb_cfg_request_sync() - Start control request and wait until it completes
595 * @ctl: Control channel to use
596 * @req: Request to start
597 * @timeout_msec: Timeout how long to wait @req to complete
599 * Starts a control request and waits until it completes. If timeout
600 * triggers the request is canceled before function returns. Note the
601 * caller needs to make sure only one message for given switch is active
604 struct tb_cfg_result
tb_cfg_request_sync(struct tb_ctl
*ctl
,
605 struct tb_cfg_request
*req
,
608 unsigned long timeout
= msecs_to_jiffies(timeout_msec
);
609 struct tb_cfg_result res
= { 0 };
610 DECLARE_COMPLETION_ONSTACK(done
);
613 ret
= tb_cfg_request(ctl
, req
, tb_cfg_request_complete
, &done
);
619 if (!wait_for_completion_timeout(&done
, timeout
))
620 tb_cfg_request_cancel(req
, -ETIMEDOUT
);
622 flush_work(&req
->work
);
627 /* public interface, alloc/start/stop/free */
630 * tb_ctl_alloc() - allocate a control channel
631 * @nhi: Pointer to NHI
632 * @index: Domain number
633 * @timeout_msec: Default timeout used with non-raw control messages
634 * @cb: Callback called for plug events
635 * @cb_data: Data passed to @cb
637 * cb will be invoked once for every hot plug event.
639 * Return: Returns a pointer on success or NULL on failure.
641 struct tb_ctl
*tb_ctl_alloc(struct tb_nhi
*nhi
, int index
, int timeout_msec
,
642 event_cb cb
, void *cb_data
)
645 struct tb_ctl
*ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
651 ctl
->timeout_msec
= timeout_msec
;
653 ctl
->callback_data
= cb_data
;
655 mutex_init(&ctl
->request_queue_lock
);
656 INIT_LIST_HEAD(&ctl
->request_queue
);
657 ctl
->frame_pool
= dma_pool_create("thunderbolt_ctl", &nhi
->pdev
->dev
,
658 TB_FRAME_SIZE
, 4, 0);
659 if (!ctl
->frame_pool
)
662 ctl
->tx
= tb_ring_alloc_tx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
);
666 ctl
->rx
= tb_ring_alloc_rx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
, 0, 0xffff,
671 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++) {
672 ctl
->rx_packets
[i
] = tb_ctl_pkg_alloc(ctl
);
673 if (!ctl
->rx_packets
[i
])
675 ctl
->rx_packets
[i
]->frame
.callback
= tb_ctl_rx_callback
;
678 tb_ctl_dbg(ctl
, "control channel created\n");
686 * tb_ctl_free() - free a control channel
687 * @ctl: Control channel to free
689 * Must be called after tb_ctl_stop.
691 * Must NOT be called from ctl->callback.
693 void tb_ctl_free(struct tb_ctl
*ctl
)
701 tb_ring_free(ctl
->rx
);
703 tb_ring_free(ctl
->tx
);
705 /* free RX packets */
706 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
707 tb_ctl_pkg_free(ctl
->rx_packets
[i
]);
710 dma_pool_destroy(ctl
->frame_pool
);
715 * tb_ctl_start() - start/resume the control channel
716 * @ctl: Control channel to start
718 void tb_ctl_start(struct tb_ctl
*ctl
)
721 tb_ctl_dbg(ctl
, "control channel starting...\n");
722 tb_ring_start(ctl
->tx
); /* is used to ack hotplug packets, start first */
723 tb_ring_start(ctl
->rx
);
724 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
725 tb_ctl_rx_submit(ctl
->rx_packets
[i
]);
731 * tb_ctl_stop() - pause the control channel
732 * @ctl: Control channel to stop
734 * All invocations of ctl->callback will have finished after this method
737 * Must NOT be called from ctl->callback.
739 void tb_ctl_stop(struct tb_ctl
*ctl
)
741 mutex_lock(&ctl
->request_queue_lock
);
742 ctl
->running
= false;
743 mutex_unlock(&ctl
->request_queue_lock
);
745 tb_ring_stop(ctl
->rx
);
746 tb_ring_stop(ctl
->tx
);
748 if (!list_empty(&ctl
->request_queue
))
749 tb_ctl_WARN(ctl
, "dangling request in request_queue\n");
750 INIT_LIST_HEAD(&ctl
->request_queue
);
751 tb_ctl_dbg(ctl
, "control channel stopped\n");
754 /* public interface, commands */
757 * tb_cfg_ack_notification() - Ack notification
758 * @ctl: Control channel to use
759 * @route: Router that originated the event
760 * @error: Pointer to the notification package
762 * Call this as response for non-plug notification to ack it. Returns
763 * %0 on success or an error code on failure.
765 int tb_cfg_ack_notification(struct tb_ctl
*ctl
, u64 route
,
766 const struct cfg_error_pkg
*error
)
768 struct cfg_ack_pkg pkg
= {
769 .header
= tb_cfg_make_header(route
),
773 switch (error
->error
) {
774 case TB_CFG_ERROR_LINK_ERROR
:
777 case TB_CFG_ERROR_HEC_ERROR_DETECTED
:
780 case TB_CFG_ERROR_FLOW_CONTROL_ERROR
:
781 name
= "flow control error";
783 case TB_CFG_ERROR_DP_BW
:
786 case TB_CFG_ERROR_ROP_CMPLT
:
787 name
= "router operation completion";
789 case TB_CFG_ERROR_POP_CMPLT
:
790 name
= "port operation completion";
792 case TB_CFG_ERROR_PCIE_WAKE
:
795 case TB_CFG_ERROR_DP_CON_CHANGE
:
796 name
= "DP connector change";
798 case TB_CFG_ERROR_DPTX_DISCOVERY
:
799 name
= "DPTX discovery";
801 case TB_CFG_ERROR_LINK_RECOVERY
:
802 name
= "link recovery";
804 case TB_CFG_ERROR_ASYM_LINK
:
805 name
= "asymmetric link";
812 tb_ctl_dbg(ctl
, "acking %s (%#x) notification on %llx\n", name
,
813 error
->error
, route
);
815 return tb_ctl_tx(ctl
, &pkg
, sizeof(pkg
), TB_CFG_PKG_NOTIFY_ACK
);
819 * tb_cfg_ack_plug() - Ack hot plug/unplug event
820 * @ctl: Control channel to use
821 * @route: Router that originated the event
822 * @port: Port where the hot plug/unplug happened
823 * @unplug: Ack hot plug or unplug
825 * Call this as response for hot plug/unplug event to ack it.
826 * Returns %0 on success or an error code on failure.
828 int tb_cfg_ack_plug(struct tb_ctl
*ctl
, u64 route
, u32 port
, bool unplug
)
830 struct cfg_error_pkg pkg
= {
831 .header
= tb_cfg_make_header(route
),
833 .error
= TB_CFG_ERROR_ACK_PLUG_EVENT
,
834 .pg
= unplug
? TB_CFG_ERROR_PG_HOT_UNPLUG
835 : TB_CFG_ERROR_PG_HOT_PLUG
,
837 tb_ctl_dbg(ctl
, "acking hot %splug event on %llx:%u\n",
838 unplug
? "un" : "", route
, port
);
839 return tb_ctl_tx(ctl
, &pkg
, sizeof(pkg
), TB_CFG_PKG_ERROR
);
842 static bool tb_cfg_match(const struct tb_cfg_request
*req
,
843 const struct ctl_pkg
*pkg
)
845 u64 route
= tb_cfg_get_route(pkg
->buffer
) & ~BIT_ULL(63);
847 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
850 if (pkg
->frame
.eof
!= req
->response_type
)
852 if (route
!= tb_cfg_get_route(req
->request
))
854 if (pkg
->frame
.size
!= req
->response_size
)
857 if (pkg
->frame
.eof
== TB_CFG_PKG_READ
||
858 pkg
->frame
.eof
== TB_CFG_PKG_WRITE
) {
859 const struct cfg_read_pkg
*req_hdr
= req
->request
;
860 const struct cfg_read_pkg
*res_hdr
= pkg
->buffer
;
862 if (req_hdr
->addr
.seq
!= res_hdr
->addr
.seq
)
869 static bool tb_cfg_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
871 struct tb_cfg_result res
;
873 /* Now make sure it is in expected format */
874 res
= parse_header(pkg
, req
->response_size
, req
->response_type
,
875 tb_cfg_get_route(req
->request
));
877 memcpy(req
->response
, pkg
->buffer
, req
->response_size
);
881 /* Always complete when first response is received */
886 * tb_cfg_reset() - send a reset packet and wait for a response
887 * @ctl: Control channel pointer
888 * @route: Router string for the router to send reset
890 * If the switch at route is incorrectly configured then we will not receive a
891 * reply (even though the switch will reset). The caller should check for
892 * -ETIMEDOUT and attempt to reconfigure the switch.
894 struct tb_cfg_result
tb_cfg_reset(struct tb_ctl
*ctl
, u64 route
)
896 struct cfg_reset_pkg request
= { .header
= tb_cfg_make_header(route
) };
897 struct tb_cfg_result res
= { 0 };
898 struct tb_cfg_header reply
;
899 struct tb_cfg_request
*req
;
901 req
= tb_cfg_request_alloc();
907 req
->match
= tb_cfg_match
;
908 req
->copy
= tb_cfg_copy
;
909 req
->request
= &request
;
910 req
->request_size
= sizeof(request
);
911 req
->request_type
= TB_CFG_PKG_RESET
;
912 req
->response
= &reply
;
913 req
->response_size
= sizeof(reply
);
914 req
->response_type
= TB_CFG_PKG_RESET
;
916 res
= tb_cfg_request_sync(ctl
, req
, ctl
->timeout_msec
);
918 tb_cfg_request_put(req
);
924 * tb_cfg_read_raw() - read from config space into buffer
925 * @ctl: Pointer to the control channel
926 * @buffer: Buffer where the data is read
927 * @route: Route string of the router
928 * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
929 * @space: Config space selector
930 * @offset: Dword word offset of the register to start reading
931 * @length: Number of dwords to read
932 * @timeout_msec: Timeout in ms how long to wait for the response
934 * Reads from router config space without translating the possible error.
936 struct tb_cfg_result
tb_cfg_read_raw(struct tb_ctl
*ctl
, void *buffer
,
937 u64 route
, u32 port
, enum tb_cfg_space space
,
938 u32 offset
, u32 length
, int timeout_msec
)
940 struct tb_cfg_result res
= { 0 };
941 struct cfg_read_pkg request
= {
942 .header
= tb_cfg_make_header(route
),
950 struct cfg_write_pkg reply
;
953 while (retries
< TB_CTL_RETRIES
) {
954 struct tb_cfg_request
*req
;
956 req
= tb_cfg_request_alloc();
962 request
.addr
.seq
= retries
++;
964 req
->match
= tb_cfg_match
;
965 req
->copy
= tb_cfg_copy
;
966 req
->request
= &request
;
967 req
->request_size
= sizeof(request
);
968 req
->request_type
= TB_CFG_PKG_READ
;
969 req
->response
= &reply
;
970 req
->response_size
= 12 + 4 * length
;
971 req
->response_type
= TB_CFG_PKG_READ
;
973 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
975 tb_cfg_request_put(req
);
977 if (res
.err
!= -ETIMEDOUT
)
980 /* Wait a bit (arbitrary time) until we send a retry */
981 usleep_range(10, 100);
987 res
.response_port
= reply
.addr
.port
;
988 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
990 memcpy(buffer
, &reply
.data
, 4 * length
);
995 * tb_cfg_write_raw() - write from buffer into config space
996 * @ctl: Pointer to the control channel
997 * @buffer: Data to write
998 * @route: Route string of the router
999 * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
1000 * @space: Config space selector
1001 * @offset: Dword word offset of the register to start writing
1002 * @length: Number of dwords to write
1003 * @timeout_msec: Timeout in ms how long to wait for the response
1005 * Writes to router config space without translating the possible error.
1007 struct tb_cfg_result
tb_cfg_write_raw(struct tb_ctl
*ctl
, const void *buffer
,
1008 u64 route
, u32 port
, enum tb_cfg_space space
,
1009 u32 offset
, u32 length
, int timeout_msec
)
1011 struct tb_cfg_result res
= { 0 };
1012 struct cfg_write_pkg request
= {
1013 .header
= tb_cfg_make_header(route
),
1021 struct cfg_read_pkg reply
;
1024 memcpy(&request
.data
, buffer
, length
* 4);
1026 while (retries
< TB_CTL_RETRIES
) {
1027 struct tb_cfg_request
*req
;
1029 req
= tb_cfg_request_alloc();
1035 request
.addr
.seq
= retries
++;
1037 req
->match
= tb_cfg_match
;
1038 req
->copy
= tb_cfg_copy
;
1039 req
->request
= &request
;
1040 req
->request_size
= 12 + 4 * length
;
1041 req
->request_type
= TB_CFG_PKG_WRITE
;
1042 req
->response
= &reply
;
1043 req
->response_size
= sizeof(reply
);
1044 req
->response_type
= TB_CFG_PKG_WRITE
;
1046 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
1048 tb_cfg_request_put(req
);
1050 if (res
.err
!= -ETIMEDOUT
)
1053 /* Wait a bit (arbitrary time) until we send a retry */
1054 usleep_range(10, 100);
1060 res
.response_port
= reply
.addr
.port
;
1061 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
1065 static int tb_cfg_get_error(struct tb_ctl
*ctl
, enum tb_cfg_space space
,
1066 const struct tb_cfg_result
*res
)
1069 * For unimplemented ports access to port config space may return
1070 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
1071 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
1072 * that the caller can mark the port as disabled.
1074 if (space
== TB_CFG_PORT
&&
1075 res
->tb_error
== TB_CFG_ERROR_INVALID_CONFIG_SPACE
)
1078 tb_cfg_print_error(ctl
, space
, res
);
1080 if (res
->tb_error
== TB_CFG_ERROR_LOCK
)
1082 if (res
->tb_error
== TB_CFG_ERROR_PORT_NOT_CONNECTED
)
1088 int tb_cfg_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
, u32 port
,
1089 enum tb_cfg_space space
, u32 offset
, u32 length
)
1091 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, buffer
, route
, port
,
1092 space
, offset
, length
, ctl
->timeout_msec
);
1099 /* Thunderbolt error, tb_error holds the actual number */
1100 return tb_cfg_get_error(ctl
, space
, &res
);
1103 tb_ctl_warn(ctl
, "%llx: timeout reading config space %u from %#x\n",
1104 route
, space
, offset
);
1108 WARN(1, "tb_cfg_read: %d\n", res
.err
);
1114 int tb_cfg_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
, u32 port
,
1115 enum tb_cfg_space space
, u32 offset
, u32 length
)
1117 struct tb_cfg_result res
= tb_cfg_write_raw(ctl
, buffer
, route
, port
,
1118 space
, offset
, length
, ctl
->timeout_msec
);
1125 /* Thunderbolt error, tb_error holds the actual number */
1126 return tb_cfg_get_error(ctl
, space
, &res
);
1129 tb_ctl_warn(ctl
, "%llx: timeout writing config space %u to %#x\n",
1130 route
, space
, offset
);
1134 WARN(1, "tb_cfg_write: %d\n", res
.err
);
1141 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1142 * @ctl: Pointer to the control channel
1143 * @route: Route string of the router
1145 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1146 * returns the port number from which the reply originated.
1148 * Return: Returns the upstream port number on success or an error code on
1151 int tb_cfg_get_upstream_port(struct tb_ctl
*ctl
, u64 route
)
1154 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, &dummy
, route
, 0,
1155 TB_CFG_SWITCH
, 0, 1,
1161 return res
.response_port
;