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 1
23 * struct tb_ctl - Thunderbolt control channel
24 * @nhi: Pointer to the NHI structure
27 * @frame_pool: DMA pool for control messages
28 * @rx_packets: Received control messages
29 * @request_queue_lock: Lock protecting @request_queue
30 * @request_queue: List of outstanding requests
31 * @running: Is the control channel running at the moment
32 * @timeout_msec: Default timeout for non-raw control messages
33 * @callback: Callback called when hotplug message is received
34 * @callback_data: Data passed to @callback
41 struct dma_pool
*frame_pool
;
42 struct ctl_pkg
*rx_packets
[TB_CTL_RX_PKG_COUNT
];
43 struct mutex request_queue_lock
;
44 struct list_head request_queue
;
53 #define tb_ctl_WARN(ctl, format, arg...) \
54 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
56 #define tb_ctl_err(ctl, format, arg...) \
57 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
59 #define tb_ctl_warn(ctl, format, arg...) \
60 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
62 #define tb_ctl_info(ctl, format, arg...) \
63 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
65 #define tb_ctl_dbg(ctl, format, arg...) \
66 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
68 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue
);
69 /* Serializes access to request kref_get/put */
70 static DEFINE_MUTEX(tb_cfg_request_lock
);
73 * tb_cfg_request_alloc() - Allocates a new config request
75 * This is refcounted object so when you are done with this, call
76 * tb_cfg_request_put() to it.
78 struct tb_cfg_request
*tb_cfg_request_alloc(void)
80 struct tb_cfg_request
*req
;
82 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
86 kref_init(&req
->kref
);
92 * tb_cfg_request_get() - Increase refcount of a request
93 * @req: Request whose refcount is increased
95 void tb_cfg_request_get(struct tb_cfg_request
*req
)
97 mutex_lock(&tb_cfg_request_lock
);
99 mutex_unlock(&tb_cfg_request_lock
);
102 static void tb_cfg_request_destroy(struct kref
*kref
)
104 struct tb_cfg_request
*req
= container_of(kref
, typeof(*req
), kref
);
110 * tb_cfg_request_put() - Decrease refcount and possibly release the request
111 * @req: Request whose refcount is decreased
113 * Call this function when you are done with the request. When refcount
114 * goes to %0 the object is released.
116 void tb_cfg_request_put(struct tb_cfg_request
*req
)
118 mutex_lock(&tb_cfg_request_lock
);
119 kref_put(&req
->kref
, tb_cfg_request_destroy
);
120 mutex_unlock(&tb_cfg_request_lock
);
123 static int tb_cfg_request_enqueue(struct tb_ctl
*ctl
,
124 struct tb_cfg_request
*req
)
126 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
));
129 mutex_lock(&ctl
->request_queue_lock
);
131 mutex_unlock(&ctl
->request_queue_lock
);
135 list_add_tail(&req
->list
, &ctl
->request_queue
);
136 set_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
137 mutex_unlock(&ctl
->request_queue_lock
);
141 static void tb_cfg_request_dequeue(struct tb_cfg_request
*req
)
143 struct tb_ctl
*ctl
= req
->ctl
;
145 mutex_lock(&ctl
->request_queue_lock
);
146 list_del(&req
->list
);
147 clear_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
148 if (test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
149 wake_up(&tb_cfg_request_cancel_queue
);
150 mutex_unlock(&ctl
->request_queue_lock
);
153 static bool tb_cfg_request_is_active(struct tb_cfg_request
*req
)
155 return test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
158 static struct tb_cfg_request
*
159 tb_cfg_request_find(struct tb_ctl
*ctl
, struct ctl_pkg
*pkg
)
161 struct tb_cfg_request
*req
;
164 mutex_lock(&pkg
->ctl
->request_queue_lock
);
165 list_for_each_entry(req
, &pkg
->ctl
->request_queue
, list
) {
166 tb_cfg_request_get(req
);
167 if (req
->match(req
, pkg
)) {
171 tb_cfg_request_put(req
);
173 mutex_unlock(&pkg
->ctl
->request_queue_lock
);
175 return found
? req
: NULL
;
178 /* utility functions */
181 static int check_header(const struct ctl_pkg
*pkg
, u32 len
,
182 enum tb_cfg_pkg_type type
, u64 route
)
184 struct tb_cfg_header
*header
= pkg
->buffer
;
186 /* check frame, TODO: frame flags */
187 if (WARN(len
!= pkg
->frame
.size
,
188 "wrong framesize (expected %#x, got %#x)\n",
189 len
, pkg
->frame
.size
))
191 if (WARN(type
!= pkg
->frame
.eof
, "wrong eof (expected %#x, got %#x)\n",
192 type
, pkg
->frame
.eof
))
194 if (WARN(pkg
->frame
.sof
, "wrong sof (expected 0x0, got %#x)\n",
199 if (WARN(header
->unknown
!= 1 << 9,
200 "header->unknown is %#x\n", header
->unknown
))
202 if (WARN(route
!= tb_cfg_get_route(header
),
203 "wrong route (expected %llx, got %llx)",
204 route
, tb_cfg_get_route(header
)))
209 static int check_config_address(struct tb_cfg_address addr
,
210 enum tb_cfg_space space
, u32 offset
,
213 if (WARN(addr
.zero
, "addr.zero is %#x\n", addr
.zero
))
215 if (WARN(space
!= addr
.space
, "wrong space (expected %x, got %x\n)",
218 if (WARN(offset
!= addr
.offset
, "wrong offset (expected %x, got %x\n)",
219 offset
, addr
.offset
))
221 if (WARN(length
!= addr
.length
, "wrong space (expected %x, got %x\n)",
222 length
, addr
.length
))
225 * We cannot check addr->port as it is set to the upstream port of the
231 static struct tb_cfg_result
decode_error(const struct ctl_pkg
*response
)
233 struct cfg_error_pkg
*pkg
= response
->buffer
;
234 struct tb_ctl
*ctl
= response
->ctl
;
235 struct tb_cfg_result res
= { 0 };
236 res
.response_route
= tb_cfg_get_route(&pkg
->header
);
237 res
.response_port
= 0;
238 res
.err
= check_header(response
, sizeof(*pkg
), TB_CFG_PKG_ERROR
,
239 tb_cfg_get_route(&pkg
->header
));
244 tb_ctl_warn(ctl
, "pkg->zero1 is %#x\n", pkg
->zero1
);
246 tb_ctl_warn(ctl
, "pkg->zero2 is %#x\n", pkg
->zero2
);
248 tb_ctl_warn(ctl
, "pkg->zero3 is %#x\n", pkg
->zero3
);
251 res
.tb_error
= pkg
->error
;
252 res
.response_port
= pkg
->port
;
257 static struct tb_cfg_result
parse_header(const struct ctl_pkg
*pkg
, u32 len
,
258 enum tb_cfg_pkg_type type
, u64 route
)
260 struct tb_cfg_header
*header
= pkg
->buffer
;
261 struct tb_cfg_result res
= { 0 };
263 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
264 return decode_error(pkg
);
266 res
.response_port
= 0; /* will be updated later for cfg_read/write */
267 res
.response_route
= tb_cfg_get_route(header
);
268 res
.err
= check_header(pkg
, len
, type
, route
);
272 static void tb_cfg_print_error(struct tb_ctl
*ctl
,
273 const struct tb_cfg_result
*res
)
275 WARN_ON(res
->err
!= 1);
276 switch (res
->tb_error
) {
277 case TB_CFG_ERROR_PORT_NOT_CONNECTED
:
278 /* Port is not connected. This can happen during surprise
279 * removal. Do not warn. */
281 case TB_CFG_ERROR_INVALID_CONFIG_SPACE
:
283 * Invalid cfg_space/offset/length combination in
284 * cfg_read/cfg_write.
286 tb_ctl_dbg(ctl
, "%llx:%x: invalid config space or offset\n",
287 res
->response_route
, res
->response_port
);
289 case TB_CFG_ERROR_NO_SUCH_PORT
:
291 * - The route contains a non-existent port.
292 * - The route contains a non-PHY port (e.g. PCIe).
293 * - The port in cfg_read/cfg_write does not exist.
295 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Invalid port\n",
296 res
->response_route
, res
->response_port
);
298 case TB_CFG_ERROR_LOOP
:
299 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Route contains a loop\n",
300 res
->response_route
, res
->response_port
);
302 case TB_CFG_ERROR_LOCK
:
303 tb_ctl_warn(ctl
, "%llx:%x: downstream port is locked\n",
304 res
->response_route
, res
->response_port
);
307 /* 5,6,7,9 and 11 are also valid error codes */
308 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Unknown error\n",
309 res
->response_route
, res
->response_port
);
314 static __be32
tb_crc(const void *data
, size_t len
)
316 return cpu_to_be32(~__crc32c_le(~0, data
, len
));
319 static void tb_ctl_pkg_free(struct ctl_pkg
*pkg
)
322 dma_pool_free(pkg
->ctl
->frame_pool
,
323 pkg
->buffer
, pkg
->frame
.buffer_phy
);
328 static struct ctl_pkg
*tb_ctl_pkg_alloc(struct tb_ctl
*ctl
)
330 struct ctl_pkg
*pkg
= kzalloc(sizeof(*pkg
), GFP_KERNEL
);
334 pkg
->buffer
= dma_pool_alloc(ctl
->frame_pool
, GFP_KERNEL
,
335 &pkg
->frame
.buffer_phy
);
346 static void tb_ctl_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
349 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
350 tb_ctl_pkg_free(pkg
);
354 * tb_cfg_tx() - transmit a packet on the control channel
356 * len must be a multiple of four.
358 * Return: Returns 0 on success or an error code on failure.
360 static int tb_ctl_tx(struct tb_ctl
*ctl
, const void *data
, size_t len
,
361 enum tb_cfg_pkg_type type
)
365 if (len
% 4 != 0) { /* required for le->be conversion */
366 tb_ctl_WARN(ctl
, "TX: invalid size: %zu\n", len
);
369 if (len
> TB_FRAME_SIZE
- 4) { /* checksum is 4 bytes */
370 tb_ctl_WARN(ctl
, "TX: packet too large: %zu/%d\n",
371 len
, TB_FRAME_SIZE
- 4);
374 pkg
= tb_ctl_pkg_alloc(ctl
);
377 pkg
->frame
.callback
= tb_ctl_tx_callback
;
378 pkg
->frame
.size
= len
+ 4;
379 pkg
->frame
.sof
= type
;
380 pkg
->frame
.eof
= type
;
381 cpu_to_be32_array(pkg
->buffer
, data
, len
/ 4);
382 *(__be32
*) (pkg
->buffer
+ len
) = tb_crc(pkg
->buffer
, len
);
384 res
= tb_ring_tx(ctl
->tx
, &pkg
->frame
);
385 if (res
) /* ring is stopped */
386 tb_ctl_pkg_free(pkg
);
391 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
393 static bool tb_ctl_handle_event(struct tb_ctl
*ctl
, enum tb_cfg_pkg_type type
,
394 struct ctl_pkg
*pkg
, size_t size
)
396 return ctl
->callback(ctl
->callback_data
, type
, pkg
->buffer
, size
);
399 static void tb_ctl_rx_submit(struct ctl_pkg
*pkg
)
401 tb_ring_rx(pkg
->ctl
->rx
, &pkg
->frame
); /*
402 * We ignore failures during stop.
403 * All rx packets are referenced
404 * from ctl->rx_packets, so we do
409 static int tb_async_error(const struct ctl_pkg
*pkg
)
411 const struct cfg_error_pkg
*error
= (const struct cfg_error_pkg
*)pkg
;
413 if (pkg
->frame
.eof
!= TB_CFG_PKG_ERROR
)
416 switch (error
->error
) {
417 case TB_CFG_ERROR_LINK_ERROR
:
418 case TB_CFG_ERROR_HEC_ERROR_DETECTED
:
419 case TB_CFG_ERROR_FLOW_CONTROL_ERROR
:
427 static void tb_ctl_rx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
430 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
431 struct tb_cfg_request
*req
;
436 * ring is stopped, packet is referenced from
440 if (frame
->size
< 4 || frame
->size
% 4 != 0) {
441 tb_ctl_err(pkg
->ctl
, "RX: invalid size %#x, dropping packet\n",
446 frame
->size
-= 4; /* remove checksum */
447 crc32
= tb_crc(pkg
->buffer
, frame
->size
);
448 be32_to_cpu_array(pkg
->buffer
, pkg
->buffer
, frame
->size
/ 4);
450 switch (frame
->eof
) {
451 case TB_CFG_PKG_READ
:
452 case TB_CFG_PKG_WRITE
:
453 case TB_CFG_PKG_ERROR
:
454 case TB_CFG_PKG_OVERRIDE
:
455 case TB_CFG_PKG_RESET
:
456 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
458 "RX: checksum mismatch, dropping packet\n");
461 if (tb_async_error(pkg
)) {
462 tb_ctl_handle_event(pkg
->ctl
, frame
->eof
,
468 case TB_CFG_PKG_EVENT
:
469 case TB_CFG_PKG_XDOMAIN_RESP
:
470 case TB_CFG_PKG_XDOMAIN_REQ
:
471 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
473 "RX: checksum mismatch, dropping packet\n");
477 case TB_CFG_PKG_ICM_EVENT
:
478 if (tb_ctl_handle_event(pkg
->ctl
, frame
->eof
, pkg
, frame
->size
))
487 * The received packet will be processed only if there is an
488 * active request and that the packet is what is expected. This
489 * prevents packets such as replies coming after timeout has
490 * triggered from messing with the active requests.
492 req
= tb_cfg_request_find(pkg
->ctl
, pkg
);
494 if (req
->copy(req
, pkg
))
495 schedule_work(&req
->work
);
496 tb_cfg_request_put(req
);
500 tb_ctl_rx_submit(pkg
);
503 static void tb_cfg_request_work(struct work_struct
*work
)
505 struct tb_cfg_request
*req
= container_of(work
, typeof(*req
), work
);
507 if (!test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
508 req
->callback(req
->callback_data
);
510 tb_cfg_request_dequeue(req
);
511 tb_cfg_request_put(req
);
515 * tb_cfg_request() - Start control request not waiting for it to complete
516 * @ctl: Control channel to use
517 * @req: Request to start
518 * @callback: Callback called when the request is completed
519 * @callback_data: Data to be passed to @callback
521 * This queues @req on the given control channel without waiting for it
522 * to complete. When the request completes @callback is called.
524 int tb_cfg_request(struct tb_ctl
*ctl
, struct tb_cfg_request
*req
,
525 void (*callback
)(void *), void *callback_data
)
530 req
->callback
= callback
;
531 req
->callback_data
= callback_data
;
532 INIT_WORK(&req
->work
, tb_cfg_request_work
);
533 INIT_LIST_HEAD(&req
->list
);
535 tb_cfg_request_get(req
);
536 ret
= tb_cfg_request_enqueue(ctl
, req
);
540 ret
= tb_ctl_tx(ctl
, req
->request
, req
->request_size
,
546 schedule_work(&req
->work
);
551 tb_cfg_request_dequeue(req
);
553 tb_cfg_request_put(req
);
559 * tb_cfg_request_cancel() - Cancel a control request
560 * @req: Request to cancel
561 * @err: Error to assign to the request
563 * This function can be used to cancel ongoing request. It will wait
564 * until the request is not active anymore.
566 void tb_cfg_request_cancel(struct tb_cfg_request
*req
, int err
)
568 set_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
);
569 schedule_work(&req
->work
);
570 wait_event(tb_cfg_request_cancel_queue
, !tb_cfg_request_is_active(req
));
571 req
->result
.err
= err
;
574 static void tb_cfg_request_complete(void *data
)
580 * tb_cfg_request_sync() - Start control request and wait until it completes
581 * @ctl: Control channel to use
582 * @req: Request to start
583 * @timeout_msec: Timeout how long to wait @req to complete
585 * Starts a control request and waits until it completes. If timeout
586 * triggers the request is canceled before function returns. Note the
587 * caller needs to make sure only one message for given switch is active
590 struct tb_cfg_result
tb_cfg_request_sync(struct tb_ctl
*ctl
,
591 struct tb_cfg_request
*req
,
594 unsigned long timeout
= msecs_to_jiffies(timeout_msec
);
595 struct tb_cfg_result res
= { 0 };
596 DECLARE_COMPLETION_ONSTACK(done
);
599 ret
= tb_cfg_request(ctl
, req
, tb_cfg_request_complete
, &done
);
605 if (!wait_for_completion_timeout(&done
, timeout
))
606 tb_cfg_request_cancel(req
, -ETIMEDOUT
);
608 flush_work(&req
->work
);
613 /* public interface, alloc/start/stop/free */
616 * tb_ctl_alloc() - allocate a control channel
617 * @nhi: Pointer to NHI
618 * @timeout_msec: Default timeout used with non-raw control messages
619 * @cb: Callback called for plug events
620 * @cb_data: Data passed to @cb
622 * cb will be invoked once for every hot plug event.
624 * Return: Returns a pointer on success or NULL on failure.
626 struct tb_ctl
*tb_ctl_alloc(struct tb_nhi
*nhi
, int timeout_msec
, event_cb cb
,
630 struct tb_ctl
*ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
634 ctl
->timeout_msec
= timeout_msec
;
636 ctl
->callback_data
= cb_data
;
638 mutex_init(&ctl
->request_queue_lock
);
639 INIT_LIST_HEAD(&ctl
->request_queue
);
640 ctl
->frame_pool
= dma_pool_create("thunderbolt_ctl", &nhi
->pdev
->dev
,
641 TB_FRAME_SIZE
, 4, 0);
642 if (!ctl
->frame_pool
)
645 ctl
->tx
= tb_ring_alloc_tx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
);
649 ctl
->rx
= tb_ring_alloc_rx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
, 0, 0xffff,
654 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++) {
655 ctl
->rx_packets
[i
] = tb_ctl_pkg_alloc(ctl
);
656 if (!ctl
->rx_packets
[i
])
658 ctl
->rx_packets
[i
]->frame
.callback
= tb_ctl_rx_callback
;
661 tb_ctl_dbg(ctl
, "control channel created\n");
669 * tb_ctl_free() - free a control channel
670 * @ctl: Control channel to free
672 * Must be called after tb_ctl_stop.
674 * Must NOT be called from ctl->callback.
676 void tb_ctl_free(struct tb_ctl
*ctl
)
684 tb_ring_free(ctl
->rx
);
686 tb_ring_free(ctl
->tx
);
688 /* free RX packets */
689 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
690 tb_ctl_pkg_free(ctl
->rx_packets
[i
]);
693 dma_pool_destroy(ctl
->frame_pool
);
698 * tb_cfg_start() - start/resume the control channel
699 * @ctl: Control channel to start
701 void tb_ctl_start(struct tb_ctl
*ctl
)
704 tb_ctl_dbg(ctl
, "control channel starting...\n");
705 tb_ring_start(ctl
->tx
); /* is used to ack hotplug packets, start first */
706 tb_ring_start(ctl
->rx
);
707 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
708 tb_ctl_rx_submit(ctl
->rx_packets
[i
]);
714 * tb_ctrl_stop() - pause the control channel
715 * @ctl: Control channel to stop
717 * All invocations of ctl->callback will have finished after this method
720 * Must NOT be called from ctl->callback.
722 void tb_ctl_stop(struct tb_ctl
*ctl
)
724 mutex_lock(&ctl
->request_queue_lock
);
725 ctl
->running
= false;
726 mutex_unlock(&ctl
->request_queue_lock
);
728 tb_ring_stop(ctl
->rx
);
729 tb_ring_stop(ctl
->tx
);
731 if (!list_empty(&ctl
->request_queue
))
732 tb_ctl_WARN(ctl
, "dangling request in request_queue\n");
733 INIT_LIST_HEAD(&ctl
->request_queue
);
734 tb_ctl_dbg(ctl
, "control channel stopped\n");
737 /* public interface, commands */
740 * tb_cfg_ack_plug() - Ack hot plug/unplug event
741 * @ctl: Control channel to use
742 * @route: Router that originated the event
743 * @port: Port where the hot plug/unplug happened
744 * @unplug: Ack hot plug or unplug
746 * Call this as response for hot plug/unplug event to ack it.
747 * Returns %0 on success or an error code on failure.
749 int tb_cfg_ack_plug(struct tb_ctl
*ctl
, u64 route
, u32 port
, bool unplug
)
751 struct cfg_error_pkg pkg
= {
752 .header
= tb_cfg_make_header(route
),
754 .error
= TB_CFG_ERROR_ACK_PLUG_EVENT
,
755 .pg
= unplug
? TB_CFG_ERROR_PG_HOT_UNPLUG
756 : TB_CFG_ERROR_PG_HOT_PLUG
,
758 tb_ctl_dbg(ctl
, "acking hot %splug event on %llx:%x\n",
759 unplug
? "un" : "", route
, port
);
760 return tb_ctl_tx(ctl
, &pkg
, sizeof(pkg
), TB_CFG_PKG_ERROR
);
763 static bool tb_cfg_match(const struct tb_cfg_request
*req
,
764 const struct ctl_pkg
*pkg
)
766 u64 route
= tb_cfg_get_route(pkg
->buffer
) & ~BIT_ULL(63);
768 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
771 if (pkg
->frame
.eof
!= req
->response_type
)
773 if (route
!= tb_cfg_get_route(req
->request
))
775 if (pkg
->frame
.size
!= req
->response_size
)
778 if (pkg
->frame
.eof
== TB_CFG_PKG_READ
||
779 pkg
->frame
.eof
== TB_CFG_PKG_WRITE
) {
780 const struct cfg_read_pkg
*req_hdr
= req
->request
;
781 const struct cfg_read_pkg
*res_hdr
= pkg
->buffer
;
783 if (req_hdr
->addr
.seq
!= res_hdr
->addr
.seq
)
790 static bool tb_cfg_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
792 struct tb_cfg_result res
;
794 /* Now make sure it is in expected format */
795 res
= parse_header(pkg
, req
->response_size
, req
->response_type
,
796 tb_cfg_get_route(req
->request
));
798 memcpy(req
->response
, pkg
->buffer
, req
->response_size
);
802 /* Always complete when first response is received */
807 * tb_cfg_reset() - send a reset packet and wait for a response
808 * @ctl: Control channel pointer
809 * @route: Router string for the router to send reset
811 * If the switch at route is incorrectly configured then we will not receive a
812 * reply (even though the switch will reset). The caller should check for
813 * -ETIMEDOUT and attempt to reconfigure the switch.
815 struct tb_cfg_result
tb_cfg_reset(struct tb_ctl
*ctl
, u64 route
)
817 struct cfg_reset_pkg request
= { .header
= tb_cfg_make_header(route
) };
818 struct tb_cfg_result res
= { 0 };
819 struct tb_cfg_header reply
;
820 struct tb_cfg_request
*req
;
822 req
= tb_cfg_request_alloc();
828 req
->match
= tb_cfg_match
;
829 req
->copy
= tb_cfg_copy
;
830 req
->request
= &request
;
831 req
->request_size
= sizeof(request
);
832 req
->request_type
= TB_CFG_PKG_RESET
;
833 req
->response
= &reply
;
834 req
->response_size
= sizeof(reply
);
835 req
->response_type
= TB_CFG_PKG_RESET
;
837 res
= tb_cfg_request_sync(ctl
, req
, ctl
->timeout_msec
);
839 tb_cfg_request_put(req
);
845 * tb_cfg_read_raw() - read from config space into buffer
846 * @ctl: Pointer to the control channel
847 * @buffer: Buffer where the data is read
848 * @route: Route string of the router
849 * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
850 * @space: Config space selector
851 * @offset: Dword word offset of the register to start reading
852 * @length: Number of dwords to read
853 * @timeout_msec: Timeout in ms how long to wait for the response
855 * Reads from router config space without translating the possible error.
857 struct tb_cfg_result
tb_cfg_read_raw(struct tb_ctl
*ctl
, void *buffer
,
858 u64 route
, u32 port
, enum tb_cfg_space space
,
859 u32 offset
, u32 length
, int timeout_msec
)
861 struct tb_cfg_result res
= { 0 };
862 struct cfg_read_pkg request
= {
863 .header
= tb_cfg_make_header(route
),
871 struct cfg_write_pkg reply
;
874 while (retries
< TB_CTL_RETRIES
) {
875 struct tb_cfg_request
*req
;
877 req
= tb_cfg_request_alloc();
883 request
.addr
.seq
= retries
++;
885 req
->match
= tb_cfg_match
;
886 req
->copy
= tb_cfg_copy
;
887 req
->request
= &request
;
888 req
->request_size
= sizeof(request
);
889 req
->request_type
= TB_CFG_PKG_READ
;
890 req
->response
= &reply
;
891 req
->response_size
= 12 + 4 * length
;
892 req
->response_type
= TB_CFG_PKG_READ
;
894 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
896 tb_cfg_request_put(req
);
898 if (res
.err
!= -ETIMEDOUT
)
901 /* Wait a bit (arbitrary time) until we send a retry */
902 usleep_range(10, 100);
908 res
.response_port
= reply
.addr
.port
;
909 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
911 memcpy(buffer
, &reply
.data
, 4 * length
);
916 * tb_cfg_write() - write from buffer into config space
917 * @ctl: Pointer to the control channel
918 * @buffer: Data to write
919 * @route: Route string of the router
920 * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
921 * @space: Config space selector
922 * @offset: Dword word offset of the register to start writing
923 * @length: Number of dwords to write
924 * @timeout_msec: Timeout in ms how long to wait for the response
926 * Writes to router config space without translating the possible error.
928 struct tb_cfg_result
tb_cfg_write_raw(struct tb_ctl
*ctl
, const void *buffer
,
929 u64 route
, u32 port
, enum tb_cfg_space space
,
930 u32 offset
, u32 length
, int timeout_msec
)
932 struct tb_cfg_result res
= { 0 };
933 struct cfg_write_pkg request
= {
934 .header
= tb_cfg_make_header(route
),
942 struct cfg_read_pkg reply
;
945 memcpy(&request
.data
, buffer
, length
* 4);
947 while (retries
< TB_CTL_RETRIES
) {
948 struct tb_cfg_request
*req
;
950 req
= tb_cfg_request_alloc();
956 request
.addr
.seq
= retries
++;
958 req
->match
= tb_cfg_match
;
959 req
->copy
= tb_cfg_copy
;
960 req
->request
= &request
;
961 req
->request_size
= 12 + 4 * length
;
962 req
->request_type
= TB_CFG_PKG_WRITE
;
963 req
->response
= &reply
;
964 req
->response_size
= sizeof(reply
);
965 req
->response_type
= TB_CFG_PKG_WRITE
;
967 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
969 tb_cfg_request_put(req
);
971 if (res
.err
!= -ETIMEDOUT
)
974 /* Wait a bit (arbitrary time) until we send a retry */
975 usleep_range(10, 100);
981 res
.response_port
= reply
.addr
.port
;
982 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
986 static int tb_cfg_get_error(struct tb_ctl
*ctl
, enum tb_cfg_space space
,
987 const struct tb_cfg_result
*res
)
990 * For unimplemented ports access to port config space may return
991 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
992 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
993 * that the caller can mark the port as disabled.
995 if (space
== TB_CFG_PORT
&&
996 res
->tb_error
== TB_CFG_ERROR_INVALID_CONFIG_SPACE
)
999 tb_cfg_print_error(ctl
, res
);
1001 if (res
->tb_error
== TB_CFG_ERROR_LOCK
)
1003 else if (res
->tb_error
== TB_CFG_ERROR_PORT_NOT_CONNECTED
)
1009 int tb_cfg_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
, u32 port
,
1010 enum tb_cfg_space space
, u32 offset
, u32 length
)
1012 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, buffer
, route
, port
,
1013 space
, offset
, length
, ctl
->timeout_msec
);
1020 /* Thunderbolt error, tb_error holds the actual number */
1021 return tb_cfg_get_error(ctl
, space
, &res
);
1024 tb_ctl_warn(ctl
, "%llx: timeout reading config space %u from %#x\n",
1025 route
, space
, offset
);
1029 WARN(1, "tb_cfg_read: %d\n", res
.err
);
1035 int tb_cfg_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
, u32 port
,
1036 enum tb_cfg_space space
, u32 offset
, u32 length
)
1038 struct tb_cfg_result res
= tb_cfg_write_raw(ctl
, buffer
, route
, port
,
1039 space
, offset
, length
, ctl
->timeout_msec
);
1046 /* Thunderbolt error, tb_error holds the actual number */
1047 return tb_cfg_get_error(ctl
, space
, &res
);
1050 tb_ctl_warn(ctl
, "%llx: timeout writing config space %u to %#x\n",
1051 route
, space
, offset
);
1055 WARN(1, "tb_cfg_write: %d\n", res
.err
);
1062 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1063 * @ctl: Pointer to the control channel
1064 * @route: Route string of the router
1066 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1067 * returns the port number from which the reply originated.
1069 * Return: Returns the upstream port number on success or an error code on
1072 int tb_cfg_get_upstream_port(struct tb_ctl
*ctl
, u64 route
)
1075 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, &dummy
, route
, 0,
1076 TB_CFG_SWITCH
, 0, 1,
1082 return res
.response_port
;