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>
19 #define TB_CTL_RX_PKG_COUNT 10
20 #define TB_CTL_RETRIES 4
23 * struct tb_cfg - thunderbolt control channel
30 struct dma_pool
*frame_pool
;
31 struct ctl_pkg
*rx_packets
[TB_CTL_RX_PKG_COUNT
];
32 struct mutex request_queue_lock
;
33 struct list_head request_queue
;
41 #define tb_ctl_WARN(ctl, format, arg...) \
42 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
44 #define tb_ctl_err(ctl, format, arg...) \
45 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
47 #define tb_ctl_warn(ctl, format, arg...) \
48 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
50 #define tb_ctl_info(ctl, format, arg...) \
51 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
53 #define tb_ctl_dbg(ctl, format, arg...) \
54 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
56 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue
);
57 /* Serializes access to request kref_get/put */
58 static DEFINE_MUTEX(tb_cfg_request_lock
);
61 * tb_cfg_request_alloc() - Allocates a new config request
63 * This is refcounted object so when you are done with this, call
64 * tb_cfg_request_put() to it.
66 struct tb_cfg_request
*tb_cfg_request_alloc(void)
68 struct tb_cfg_request
*req
;
70 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
74 kref_init(&req
->kref
);
80 * tb_cfg_request_get() - Increase refcount of a request
81 * @req: Request whose refcount is increased
83 void tb_cfg_request_get(struct tb_cfg_request
*req
)
85 mutex_lock(&tb_cfg_request_lock
);
87 mutex_unlock(&tb_cfg_request_lock
);
90 static void tb_cfg_request_destroy(struct kref
*kref
)
92 struct tb_cfg_request
*req
= container_of(kref
, typeof(*req
), kref
);
98 * tb_cfg_request_put() - Decrease refcount and possibly release the request
99 * @req: Request whose refcount is decreased
101 * Call this function when you are done with the request. When refcount
102 * goes to %0 the object is released.
104 void tb_cfg_request_put(struct tb_cfg_request
*req
)
106 mutex_lock(&tb_cfg_request_lock
);
107 kref_put(&req
->kref
, tb_cfg_request_destroy
);
108 mutex_unlock(&tb_cfg_request_lock
);
111 static int tb_cfg_request_enqueue(struct tb_ctl
*ctl
,
112 struct tb_cfg_request
*req
)
114 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
));
117 mutex_lock(&ctl
->request_queue_lock
);
119 mutex_unlock(&ctl
->request_queue_lock
);
123 list_add_tail(&req
->list
, &ctl
->request_queue
);
124 set_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
125 mutex_unlock(&ctl
->request_queue_lock
);
129 static void tb_cfg_request_dequeue(struct tb_cfg_request
*req
)
131 struct tb_ctl
*ctl
= req
->ctl
;
133 mutex_lock(&ctl
->request_queue_lock
);
134 list_del(&req
->list
);
135 clear_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
136 if (test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
137 wake_up(&tb_cfg_request_cancel_queue
);
138 mutex_unlock(&ctl
->request_queue_lock
);
141 static bool tb_cfg_request_is_active(struct tb_cfg_request
*req
)
143 return test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
146 static struct tb_cfg_request
*
147 tb_cfg_request_find(struct tb_ctl
*ctl
, struct ctl_pkg
*pkg
)
149 struct tb_cfg_request
*req
;
152 mutex_lock(&pkg
->ctl
->request_queue_lock
);
153 list_for_each_entry(req
, &pkg
->ctl
->request_queue
, list
) {
154 tb_cfg_request_get(req
);
155 if (req
->match(req
, pkg
)) {
159 tb_cfg_request_put(req
);
161 mutex_unlock(&pkg
->ctl
->request_queue_lock
);
163 return found
? req
: NULL
;
166 /* utility functions */
169 static int check_header(const struct ctl_pkg
*pkg
, u32 len
,
170 enum tb_cfg_pkg_type type
, u64 route
)
172 struct tb_cfg_header
*header
= pkg
->buffer
;
174 /* check frame, TODO: frame flags */
175 if (WARN(len
!= pkg
->frame
.size
,
176 "wrong framesize (expected %#x, got %#x)\n",
177 len
, pkg
->frame
.size
))
179 if (WARN(type
!= pkg
->frame
.eof
, "wrong eof (expected %#x, got %#x)\n",
180 type
, pkg
->frame
.eof
))
182 if (WARN(pkg
->frame
.sof
, "wrong sof (expected 0x0, got %#x)\n",
187 if (WARN(header
->unknown
!= 1 << 9,
188 "header->unknown is %#x\n", header
->unknown
))
190 if (WARN(route
!= tb_cfg_get_route(header
),
191 "wrong route (expected %llx, got %llx)",
192 route
, tb_cfg_get_route(header
)))
197 static int check_config_address(struct tb_cfg_address addr
,
198 enum tb_cfg_space space
, u32 offset
,
201 if (WARN(addr
.zero
, "addr.zero is %#x\n", addr
.zero
))
203 if (WARN(space
!= addr
.space
, "wrong space (expected %x, got %x\n)",
206 if (WARN(offset
!= addr
.offset
, "wrong offset (expected %x, got %x\n)",
207 offset
, addr
.offset
))
209 if (WARN(length
!= addr
.length
, "wrong space (expected %x, got %x\n)",
210 length
, addr
.length
))
213 * We cannot check addr->port as it is set to the upstream port of the
219 static struct tb_cfg_result
decode_error(const struct ctl_pkg
*response
)
221 struct cfg_error_pkg
*pkg
= response
->buffer
;
222 struct tb_ctl
*ctl
= response
->ctl
;
223 struct tb_cfg_result res
= { 0 };
224 res
.response_route
= tb_cfg_get_route(&pkg
->header
);
225 res
.response_port
= 0;
226 res
.err
= check_header(response
, sizeof(*pkg
), TB_CFG_PKG_ERROR
,
227 tb_cfg_get_route(&pkg
->header
));
232 tb_ctl_warn(ctl
, "pkg->zero1 is %#x\n", pkg
->zero1
);
234 tb_ctl_warn(ctl
, "pkg->zero2 is %#x\n", pkg
->zero2
);
236 tb_ctl_warn(ctl
, "pkg->zero3 is %#x\n", pkg
->zero3
);
239 res
.tb_error
= pkg
->error
;
240 res
.response_port
= pkg
->port
;
245 static struct tb_cfg_result
parse_header(const struct ctl_pkg
*pkg
, u32 len
,
246 enum tb_cfg_pkg_type type
, u64 route
)
248 struct tb_cfg_header
*header
= pkg
->buffer
;
249 struct tb_cfg_result res
= { 0 };
251 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
252 return decode_error(pkg
);
254 res
.response_port
= 0; /* will be updated later for cfg_read/write */
255 res
.response_route
= tb_cfg_get_route(header
);
256 res
.err
= check_header(pkg
, len
, type
, route
);
260 static void tb_cfg_print_error(struct tb_ctl
*ctl
,
261 const struct tb_cfg_result
*res
)
263 WARN_ON(res
->err
!= 1);
264 switch (res
->tb_error
) {
265 case TB_CFG_ERROR_PORT_NOT_CONNECTED
:
266 /* Port is not connected. This can happen during surprise
267 * removal. Do not warn. */
269 case TB_CFG_ERROR_INVALID_CONFIG_SPACE
:
271 * Invalid cfg_space/offset/length combination in
272 * cfg_read/cfg_write.
274 tb_ctl_dbg(ctl
, "%llx:%x: invalid config space or offset\n",
275 res
->response_route
, res
->response_port
);
277 case TB_CFG_ERROR_NO_SUCH_PORT
:
279 * - The route contains a non-existent port.
280 * - The route contains a non-PHY port (e.g. PCIe).
281 * - The port in cfg_read/cfg_write does not exist.
283 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Invalid port\n",
284 res
->response_route
, res
->response_port
);
286 case TB_CFG_ERROR_LOOP
:
287 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Route contains a loop\n",
288 res
->response_route
, res
->response_port
);
290 case TB_CFG_ERROR_LOCK
:
291 tb_ctl_warn(ctl
, "%llx:%x: downstream port is locked\n",
292 res
->response_route
, res
->response_port
);
295 /* 5,6,7,9 and 11 are also valid error codes */
296 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Unknown error\n",
297 res
->response_route
, res
->response_port
);
302 static __be32
tb_crc(const void *data
, size_t len
)
304 return cpu_to_be32(~__crc32c_le(~0, data
, len
));
307 static void tb_ctl_pkg_free(struct ctl_pkg
*pkg
)
310 dma_pool_free(pkg
->ctl
->frame_pool
,
311 pkg
->buffer
, pkg
->frame
.buffer_phy
);
316 static struct ctl_pkg
*tb_ctl_pkg_alloc(struct tb_ctl
*ctl
)
318 struct ctl_pkg
*pkg
= kzalloc(sizeof(*pkg
), GFP_KERNEL
);
322 pkg
->buffer
= dma_pool_alloc(ctl
->frame_pool
, GFP_KERNEL
,
323 &pkg
->frame
.buffer_phy
);
334 static void tb_ctl_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
337 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
338 tb_ctl_pkg_free(pkg
);
342 * tb_cfg_tx() - transmit a packet on the control channel
344 * len must be a multiple of four.
346 * Return: Returns 0 on success or an error code on failure.
348 static int tb_ctl_tx(struct tb_ctl
*ctl
, const void *data
, size_t len
,
349 enum tb_cfg_pkg_type type
)
353 if (len
% 4 != 0) { /* required for le->be conversion */
354 tb_ctl_WARN(ctl
, "TX: invalid size: %zu\n", len
);
357 if (len
> TB_FRAME_SIZE
- 4) { /* checksum is 4 bytes */
358 tb_ctl_WARN(ctl
, "TX: packet too large: %zu/%d\n",
359 len
, TB_FRAME_SIZE
- 4);
362 pkg
= tb_ctl_pkg_alloc(ctl
);
365 pkg
->frame
.callback
= tb_ctl_tx_callback
;
366 pkg
->frame
.size
= len
+ 4;
367 pkg
->frame
.sof
= type
;
368 pkg
->frame
.eof
= type
;
369 cpu_to_be32_array(pkg
->buffer
, data
, len
/ 4);
370 *(__be32
*) (pkg
->buffer
+ len
) = tb_crc(pkg
->buffer
, len
);
372 res
= tb_ring_tx(ctl
->tx
, &pkg
->frame
);
373 if (res
) /* ring is stopped */
374 tb_ctl_pkg_free(pkg
);
379 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
381 static bool tb_ctl_handle_event(struct tb_ctl
*ctl
, enum tb_cfg_pkg_type type
,
382 struct ctl_pkg
*pkg
, size_t size
)
384 return ctl
->callback(ctl
->callback_data
, type
, pkg
->buffer
, size
);
387 static void tb_ctl_rx_submit(struct ctl_pkg
*pkg
)
389 tb_ring_rx(pkg
->ctl
->rx
, &pkg
->frame
); /*
390 * We ignore failures during stop.
391 * All rx packets are referenced
392 * from ctl->rx_packets, so we do
397 static int tb_async_error(const struct ctl_pkg
*pkg
)
399 const struct cfg_error_pkg
*error
= (const struct cfg_error_pkg
*)pkg
;
401 if (pkg
->frame
.eof
!= TB_CFG_PKG_ERROR
)
404 switch (error
->error
) {
405 case TB_CFG_ERROR_LINK_ERROR
:
406 case TB_CFG_ERROR_HEC_ERROR_DETECTED
:
407 case TB_CFG_ERROR_FLOW_CONTROL_ERROR
:
415 static void tb_ctl_rx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
418 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
419 struct tb_cfg_request
*req
;
424 * ring is stopped, packet is referenced from
428 if (frame
->size
< 4 || frame
->size
% 4 != 0) {
429 tb_ctl_err(pkg
->ctl
, "RX: invalid size %#x, dropping packet\n",
434 frame
->size
-= 4; /* remove checksum */
435 crc32
= tb_crc(pkg
->buffer
, frame
->size
);
436 be32_to_cpu_array(pkg
->buffer
, pkg
->buffer
, frame
->size
/ 4);
438 switch (frame
->eof
) {
439 case TB_CFG_PKG_READ
:
440 case TB_CFG_PKG_WRITE
:
441 case TB_CFG_PKG_ERROR
:
442 case TB_CFG_PKG_OVERRIDE
:
443 case TB_CFG_PKG_RESET
:
444 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
446 "RX: checksum mismatch, dropping packet\n");
449 if (tb_async_error(pkg
)) {
450 tb_ctl_handle_event(pkg
->ctl
, frame
->eof
,
456 case TB_CFG_PKG_EVENT
:
457 case TB_CFG_PKG_XDOMAIN_RESP
:
458 case TB_CFG_PKG_XDOMAIN_REQ
:
459 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
461 "RX: checksum mismatch, dropping packet\n");
465 case TB_CFG_PKG_ICM_EVENT
:
466 if (tb_ctl_handle_event(pkg
->ctl
, frame
->eof
, pkg
, frame
->size
))
475 * The received packet will be processed only if there is an
476 * active request and that the packet is what is expected. This
477 * prevents packets such as replies coming after timeout has
478 * triggered from messing with the active requests.
480 req
= tb_cfg_request_find(pkg
->ctl
, pkg
);
482 if (req
->copy(req
, pkg
))
483 schedule_work(&req
->work
);
484 tb_cfg_request_put(req
);
488 tb_ctl_rx_submit(pkg
);
491 static void tb_cfg_request_work(struct work_struct
*work
)
493 struct tb_cfg_request
*req
= container_of(work
, typeof(*req
), work
);
495 if (!test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
496 req
->callback(req
->callback_data
);
498 tb_cfg_request_dequeue(req
);
499 tb_cfg_request_put(req
);
503 * tb_cfg_request() - Start control request not waiting for it to complete
504 * @ctl: Control channel to use
505 * @req: Request to start
506 * @callback: Callback called when the request is completed
507 * @callback_data: Data to be passed to @callback
509 * This queues @req on the given control channel without waiting for it
510 * to complete. When the request completes @callback is called.
512 int tb_cfg_request(struct tb_ctl
*ctl
, struct tb_cfg_request
*req
,
513 void (*callback
)(void *), void *callback_data
)
518 req
->callback
= callback
;
519 req
->callback_data
= callback_data
;
520 INIT_WORK(&req
->work
, tb_cfg_request_work
);
521 INIT_LIST_HEAD(&req
->list
);
523 tb_cfg_request_get(req
);
524 ret
= tb_cfg_request_enqueue(ctl
, req
);
528 ret
= tb_ctl_tx(ctl
, req
->request
, req
->request_size
,
534 schedule_work(&req
->work
);
539 tb_cfg_request_dequeue(req
);
541 tb_cfg_request_put(req
);
547 * tb_cfg_request_cancel() - Cancel a control request
548 * @req: Request to cancel
549 * @err: Error to assign to the request
551 * This function can be used to cancel ongoing request. It will wait
552 * until the request is not active anymore.
554 void tb_cfg_request_cancel(struct tb_cfg_request
*req
, int err
)
556 set_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
);
557 schedule_work(&req
->work
);
558 wait_event(tb_cfg_request_cancel_queue
, !tb_cfg_request_is_active(req
));
559 req
->result
.err
= err
;
562 static void tb_cfg_request_complete(void *data
)
568 * tb_cfg_request_sync() - Start control request and wait until it completes
569 * @ctl: Control channel to use
570 * @req: Request to start
571 * @timeout_msec: Timeout how long to wait @req to complete
573 * Starts a control request and waits until it completes. If timeout
574 * triggers the request is canceled before function returns. Note the
575 * caller needs to make sure only one message for given switch is active
578 struct tb_cfg_result
tb_cfg_request_sync(struct tb_ctl
*ctl
,
579 struct tb_cfg_request
*req
,
582 unsigned long timeout
= msecs_to_jiffies(timeout_msec
);
583 struct tb_cfg_result res
= { 0 };
584 DECLARE_COMPLETION_ONSTACK(done
);
587 ret
= tb_cfg_request(ctl
, req
, tb_cfg_request_complete
, &done
);
593 if (!wait_for_completion_timeout(&done
, timeout
))
594 tb_cfg_request_cancel(req
, -ETIMEDOUT
);
596 flush_work(&req
->work
);
601 /* public interface, alloc/start/stop/free */
604 * tb_ctl_alloc() - allocate a control channel
606 * cb will be invoked once for every hot plug event.
608 * Return: Returns a pointer on success or NULL on failure.
610 struct tb_ctl
*tb_ctl_alloc(struct tb_nhi
*nhi
, event_cb cb
, void *cb_data
)
613 struct tb_ctl
*ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
618 ctl
->callback_data
= cb_data
;
620 mutex_init(&ctl
->request_queue_lock
);
621 INIT_LIST_HEAD(&ctl
->request_queue
);
622 ctl
->frame_pool
= dma_pool_create("thunderbolt_ctl", &nhi
->pdev
->dev
,
623 TB_FRAME_SIZE
, 4, 0);
624 if (!ctl
->frame_pool
)
627 ctl
->tx
= tb_ring_alloc_tx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
);
631 ctl
->rx
= tb_ring_alloc_rx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
, 0, 0xffff,
636 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++) {
637 ctl
->rx_packets
[i
] = tb_ctl_pkg_alloc(ctl
);
638 if (!ctl
->rx_packets
[i
])
640 ctl
->rx_packets
[i
]->frame
.callback
= tb_ctl_rx_callback
;
643 tb_ctl_dbg(ctl
, "control channel created\n");
651 * tb_ctl_free() - free a control channel
653 * Must be called after tb_ctl_stop.
655 * Must NOT be called from ctl->callback.
657 void tb_ctl_free(struct tb_ctl
*ctl
)
665 tb_ring_free(ctl
->rx
);
667 tb_ring_free(ctl
->tx
);
669 /* free RX packets */
670 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
671 tb_ctl_pkg_free(ctl
->rx_packets
[i
]);
674 dma_pool_destroy(ctl
->frame_pool
);
679 * tb_cfg_start() - start/resume the control channel
681 void tb_ctl_start(struct tb_ctl
*ctl
)
684 tb_ctl_dbg(ctl
, "control channel starting...\n");
685 tb_ring_start(ctl
->tx
); /* is used to ack hotplug packets, start first */
686 tb_ring_start(ctl
->rx
);
687 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
688 tb_ctl_rx_submit(ctl
->rx_packets
[i
]);
694 * control() - pause the control channel
696 * All invocations of ctl->callback will have finished after this method
699 * Must NOT be called from ctl->callback.
701 void tb_ctl_stop(struct tb_ctl
*ctl
)
703 mutex_lock(&ctl
->request_queue_lock
);
704 ctl
->running
= false;
705 mutex_unlock(&ctl
->request_queue_lock
);
707 tb_ring_stop(ctl
->rx
);
708 tb_ring_stop(ctl
->tx
);
710 if (!list_empty(&ctl
->request_queue
))
711 tb_ctl_WARN(ctl
, "dangling request in request_queue\n");
712 INIT_LIST_HEAD(&ctl
->request_queue
);
713 tb_ctl_dbg(ctl
, "control channel stopped\n");
716 /* public interface, commands */
719 * tb_cfg_ack_plug() - Ack hot plug/unplug event
720 * @ctl: Control channel to use
721 * @route: Router that originated the event
722 * @port: Port where the hot plug/unplug happened
723 * @unplug: Ack hot plug or unplug
725 * Call this as response for hot plug/unplug event to ack it.
726 * Returns %0 on success or an error code on failure.
728 int tb_cfg_ack_plug(struct tb_ctl
*ctl
, u64 route
, u32 port
, bool unplug
)
730 struct cfg_error_pkg pkg
= {
731 .header
= tb_cfg_make_header(route
),
733 .error
= TB_CFG_ERROR_ACK_PLUG_EVENT
,
734 .pg
= unplug
? TB_CFG_ERROR_PG_HOT_UNPLUG
735 : TB_CFG_ERROR_PG_HOT_PLUG
,
737 tb_ctl_dbg(ctl
, "acking hot %splug event on %llx:%x\n",
738 unplug
? "un" : "", route
, port
);
739 return tb_ctl_tx(ctl
, &pkg
, sizeof(pkg
), TB_CFG_PKG_ERROR
);
742 static bool tb_cfg_match(const struct tb_cfg_request
*req
,
743 const struct ctl_pkg
*pkg
)
745 u64 route
= tb_cfg_get_route(pkg
->buffer
) & ~BIT_ULL(63);
747 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
750 if (pkg
->frame
.eof
!= req
->response_type
)
752 if (route
!= tb_cfg_get_route(req
->request
))
754 if (pkg
->frame
.size
!= req
->response_size
)
757 if (pkg
->frame
.eof
== TB_CFG_PKG_READ
||
758 pkg
->frame
.eof
== TB_CFG_PKG_WRITE
) {
759 const struct cfg_read_pkg
*req_hdr
= req
->request
;
760 const struct cfg_read_pkg
*res_hdr
= pkg
->buffer
;
762 if (req_hdr
->addr
.seq
!= res_hdr
->addr
.seq
)
769 static bool tb_cfg_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
771 struct tb_cfg_result res
;
773 /* Now make sure it is in expected format */
774 res
= parse_header(pkg
, req
->response_size
, req
->response_type
,
775 tb_cfg_get_route(req
->request
));
777 memcpy(req
->response
, pkg
->buffer
, req
->response_size
);
781 /* Always complete when first response is received */
786 * tb_cfg_reset() - send a reset packet and wait for a response
788 * If the switch at route is incorrectly configured then we will not receive a
789 * reply (even though the switch will reset). The caller should check for
790 * -ETIMEDOUT and attempt to reconfigure the switch.
792 struct tb_cfg_result
tb_cfg_reset(struct tb_ctl
*ctl
, u64 route
,
795 struct cfg_reset_pkg request
= { .header
= tb_cfg_make_header(route
) };
796 struct tb_cfg_result res
= { 0 };
797 struct tb_cfg_header reply
;
798 struct tb_cfg_request
*req
;
800 req
= tb_cfg_request_alloc();
806 req
->match
= tb_cfg_match
;
807 req
->copy
= tb_cfg_copy
;
808 req
->request
= &request
;
809 req
->request_size
= sizeof(request
);
810 req
->request_type
= TB_CFG_PKG_RESET
;
811 req
->response
= &reply
;
812 req
->response_size
= sizeof(reply
);
813 req
->response_type
= TB_CFG_PKG_RESET
;
815 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
817 tb_cfg_request_put(req
);
823 * tb_cfg_read() - read from config space into buffer
825 * Offset and length are in dwords.
827 struct tb_cfg_result
tb_cfg_read_raw(struct tb_ctl
*ctl
, void *buffer
,
828 u64 route
, u32 port
, enum tb_cfg_space space
,
829 u32 offset
, u32 length
, int timeout_msec
)
831 struct tb_cfg_result res
= { 0 };
832 struct cfg_read_pkg request
= {
833 .header
= tb_cfg_make_header(route
),
841 struct cfg_write_pkg reply
;
844 while (retries
< TB_CTL_RETRIES
) {
845 struct tb_cfg_request
*req
;
847 req
= tb_cfg_request_alloc();
853 request
.addr
.seq
= retries
++;
855 req
->match
= tb_cfg_match
;
856 req
->copy
= tb_cfg_copy
;
857 req
->request
= &request
;
858 req
->request_size
= sizeof(request
);
859 req
->request_type
= TB_CFG_PKG_READ
;
860 req
->response
= &reply
;
861 req
->response_size
= 12 + 4 * length
;
862 req
->response_type
= TB_CFG_PKG_READ
;
864 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
866 tb_cfg_request_put(req
);
868 if (res
.err
!= -ETIMEDOUT
)
871 /* Wait a bit (arbitrary time) until we send a retry */
872 usleep_range(10, 100);
878 res
.response_port
= reply
.addr
.port
;
879 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
881 memcpy(buffer
, &reply
.data
, 4 * length
);
886 * tb_cfg_write() - write from buffer into config space
888 * Offset and length are in dwords.
890 struct tb_cfg_result
tb_cfg_write_raw(struct tb_ctl
*ctl
, const void *buffer
,
891 u64 route
, u32 port
, enum tb_cfg_space space
,
892 u32 offset
, u32 length
, int timeout_msec
)
894 struct tb_cfg_result res
= { 0 };
895 struct cfg_write_pkg request
= {
896 .header
= tb_cfg_make_header(route
),
904 struct cfg_read_pkg reply
;
907 memcpy(&request
.data
, buffer
, length
* 4);
909 while (retries
< TB_CTL_RETRIES
) {
910 struct tb_cfg_request
*req
;
912 req
= tb_cfg_request_alloc();
918 request
.addr
.seq
= retries
++;
920 req
->match
= tb_cfg_match
;
921 req
->copy
= tb_cfg_copy
;
922 req
->request
= &request
;
923 req
->request_size
= 12 + 4 * length
;
924 req
->request_type
= TB_CFG_PKG_WRITE
;
925 req
->response
= &reply
;
926 req
->response_size
= sizeof(reply
);
927 req
->response_type
= TB_CFG_PKG_WRITE
;
929 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
931 tb_cfg_request_put(req
);
933 if (res
.err
!= -ETIMEDOUT
)
936 /* Wait a bit (arbitrary time) until we send a retry */
937 usleep_range(10, 100);
943 res
.response_port
= reply
.addr
.port
;
944 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
948 static int tb_cfg_get_error(struct tb_ctl
*ctl
, enum tb_cfg_space space
,
949 const struct tb_cfg_result
*res
)
952 * For unimplemented ports access to port config space may return
953 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
954 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
955 * that the caller can mark the port as disabled.
957 if (space
== TB_CFG_PORT
&&
958 res
->tb_error
== TB_CFG_ERROR_INVALID_CONFIG_SPACE
)
961 tb_cfg_print_error(ctl
, res
);
963 if (res
->tb_error
== TB_CFG_ERROR_LOCK
)
965 else if (res
->tb_error
== TB_CFG_ERROR_PORT_NOT_CONNECTED
)
971 int tb_cfg_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
, u32 port
,
972 enum tb_cfg_space space
, u32 offset
, u32 length
)
974 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, buffer
, route
, port
,
975 space
, offset
, length
, TB_CFG_DEFAULT_TIMEOUT
);
982 /* Thunderbolt error, tb_error holds the actual number */
983 return tb_cfg_get_error(ctl
, space
, &res
);
986 tb_ctl_warn(ctl
, "%llx: timeout reading config space %u from %#x\n",
987 route
, space
, offset
);
991 WARN(1, "tb_cfg_read: %d\n", res
.err
);
997 int tb_cfg_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
, u32 port
,
998 enum tb_cfg_space space
, u32 offset
, u32 length
)
1000 struct tb_cfg_result res
= tb_cfg_write_raw(ctl
, buffer
, route
, port
,
1001 space
, offset
, length
, TB_CFG_DEFAULT_TIMEOUT
);
1008 /* Thunderbolt error, tb_error holds the actual number */
1009 return tb_cfg_get_error(ctl
, space
, &res
);
1012 tb_ctl_warn(ctl
, "%llx: timeout writing config space %u to %#x\n",
1013 route
, space
, offset
);
1017 WARN(1, "tb_cfg_write: %d\n", res
.err
);
1024 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1026 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1027 * returns the port number from which the reply originated.
1029 * Return: Returns the upstream port number on success or an error code on
1032 int tb_cfg_get_upstream_port(struct tb_ctl
*ctl
, u64 route
)
1035 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, &dummy
, route
, 0,
1036 TB_CFG_SWITCH
, 0, 1,
1037 TB_CFG_DEFAULT_TIMEOUT
);
1042 return res
.response_port
;