1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 #include <linux/crc32.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/pci.h>
12 #include <linux/dmapool.h>
13 #include <linux/workqueue.h>
18 #define TB_CTL_RX_PKG_COUNT 10
19 #define TB_CTL_RETRIES 4
22 * struct tb_cfg - thunderbolt control channel
29 struct dma_pool
*frame_pool
;
30 struct ctl_pkg
*rx_packets
[TB_CTL_RX_PKG_COUNT
];
31 struct mutex request_queue_lock
;
32 struct list_head request_queue
;
40 #define tb_ctl_WARN(ctl, format, arg...) \
41 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
43 #define tb_ctl_err(ctl, format, arg...) \
44 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
46 #define tb_ctl_warn(ctl, format, arg...) \
47 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
49 #define tb_ctl_info(ctl, format, arg...) \
50 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
52 #define tb_ctl_dbg(ctl, format, arg...) \
53 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
55 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue
);
56 /* Serializes access to request kref_get/put */
57 static DEFINE_MUTEX(tb_cfg_request_lock
);
60 * tb_cfg_request_alloc() - Allocates a new config request
62 * This is refcounted object so when you are done with this, call
63 * tb_cfg_request_put() to it.
65 struct tb_cfg_request
*tb_cfg_request_alloc(void)
67 struct tb_cfg_request
*req
;
69 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
73 kref_init(&req
->kref
);
79 * tb_cfg_request_get() - Increase refcount of a request
80 * @req: Request whose refcount is increased
82 void tb_cfg_request_get(struct tb_cfg_request
*req
)
84 mutex_lock(&tb_cfg_request_lock
);
86 mutex_unlock(&tb_cfg_request_lock
);
89 static void tb_cfg_request_destroy(struct kref
*kref
)
91 struct tb_cfg_request
*req
= container_of(kref
, typeof(*req
), kref
);
97 * tb_cfg_request_put() - Decrease refcount and possibly release the request
98 * @req: Request whose refcount is decreased
100 * Call this function when you are done with the request. When refcount
101 * goes to %0 the object is released.
103 void tb_cfg_request_put(struct tb_cfg_request
*req
)
105 mutex_lock(&tb_cfg_request_lock
);
106 kref_put(&req
->kref
, tb_cfg_request_destroy
);
107 mutex_unlock(&tb_cfg_request_lock
);
110 static int tb_cfg_request_enqueue(struct tb_ctl
*ctl
,
111 struct tb_cfg_request
*req
)
113 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
));
116 mutex_lock(&ctl
->request_queue_lock
);
118 mutex_unlock(&ctl
->request_queue_lock
);
122 list_add_tail(&req
->list
, &ctl
->request_queue
);
123 set_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
124 mutex_unlock(&ctl
->request_queue_lock
);
128 static void tb_cfg_request_dequeue(struct tb_cfg_request
*req
)
130 struct tb_ctl
*ctl
= req
->ctl
;
132 mutex_lock(&ctl
->request_queue_lock
);
133 list_del(&req
->list
);
134 clear_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
135 if (test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
136 wake_up(&tb_cfg_request_cancel_queue
);
137 mutex_unlock(&ctl
->request_queue_lock
);
140 static bool tb_cfg_request_is_active(struct tb_cfg_request
*req
)
142 return test_bit(TB_CFG_REQUEST_ACTIVE
, &req
->flags
);
145 static struct tb_cfg_request
*
146 tb_cfg_request_find(struct tb_ctl
*ctl
, struct ctl_pkg
*pkg
)
148 struct tb_cfg_request
*req
;
151 mutex_lock(&pkg
->ctl
->request_queue_lock
);
152 list_for_each_entry(req
, &pkg
->ctl
->request_queue
, list
) {
153 tb_cfg_request_get(req
);
154 if (req
->match(req
, pkg
)) {
158 tb_cfg_request_put(req
);
160 mutex_unlock(&pkg
->ctl
->request_queue_lock
);
162 return found
? req
: NULL
;
165 /* utility functions */
168 static int check_header(const struct ctl_pkg
*pkg
, u32 len
,
169 enum tb_cfg_pkg_type type
, u64 route
)
171 struct tb_cfg_header
*header
= pkg
->buffer
;
173 /* check frame, TODO: frame flags */
174 if (WARN(len
!= pkg
->frame
.size
,
175 "wrong framesize (expected %#x, got %#x)\n",
176 len
, pkg
->frame
.size
))
178 if (WARN(type
!= pkg
->frame
.eof
, "wrong eof (expected %#x, got %#x)\n",
179 type
, pkg
->frame
.eof
))
181 if (WARN(pkg
->frame
.sof
, "wrong sof (expected 0x0, got %#x)\n",
186 if (WARN(header
->unknown
!= 1 << 9,
187 "header->unknown is %#x\n", header
->unknown
))
189 if (WARN(route
!= tb_cfg_get_route(header
),
190 "wrong route (expected %llx, got %llx)",
191 route
, tb_cfg_get_route(header
)))
196 static int check_config_address(struct tb_cfg_address addr
,
197 enum tb_cfg_space space
, u32 offset
,
200 if (WARN(addr
.zero
, "addr.zero is %#x\n", addr
.zero
))
202 if (WARN(space
!= addr
.space
, "wrong space (expected %x, got %x\n)",
205 if (WARN(offset
!= addr
.offset
, "wrong offset (expected %x, got %x\n)",
206 offset
, addr
.offset
))
208 if (WARN(length
!= addr
.length
, "wrong space (expected %x, got %x\n)",
209 length
, addr
.length
))
212 * We cannot check addr->port as it is set to the upstream port of the
218 static struct tb_cfg_result
decode_error(const struct ctl_pkg
*response
)
220 struct cfg_error_pkg
*pkg
= response
->buffer
;
221 struct tb_cfg_result res
= { 0 };
222 res
.response_route
= tb_cfg_get_route(&pkg
->header
);
223 res
.response_port
= 0;
224 res
.err
= check_header(response
, sizeof(*pkg
), TB_CFG_PKG_ERROR
,
225 tb_cfg_get_route(&pkg
->header
));
229 WARN(pkg
->zero1
, "pkg->zero1 is %#x\n", pkg
->zero1
);
230 WARN(pkg
->zero2
, "pkg->zero1 is %#x\n", pkg
->zero1
);
231 WARN(pkg
->zero3
, "pkg->zero1 is %#x\n", pkg
->zero1
);
233 res
.tb_error
= pkg
->error
;
234 res
.response_port
= pkg
->port
;
239 static struct tb_cfg_result
parse_header(const struct ctl_pkg
*pkg
, u32 len
,
240 enum tb_cfg_pkg_type type
, u64 route
)
242 struct tb_cfg_header
*header
= pkg
->buffer
;
243 struct tb_cfg_result res
= { 0 };
245 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
246 return decode_error(pkg
);
248 res
.response_port
= 0; /* will be updated later for cfg_read/write */
249 res
.response_route
= tb_cfg_get_route(header
);
250 res
.err
= check_header(pkg
, len
, type
, route
);
254 static void tb_cfg_print_error(struct tb_ctl
*ctl
,
255 const struct tb_cfg_result
*res
)
257 WARN_ON(res
->err
!= 1);
258 switch (res
->tb_error
) {
259 case TB_CFG_ERROR_PORT_NOT_CONNECTED
:
260 /* Port is not connected. This can happen during surprise
261 * removal. Do not warn. */
263 case TB_CFG_ERROR_INVALID_CONFIG_SPACE
:
265 * Invalid cfg_space/offset/length combination in
266 * cfg_read/cfg_write.
269 "CFG_ERROR(%llx:%x): Invalid config space or offset\n",
270 res
->response_route
, res
->response_port
);
272 case TB_CFG_ERROR_NO_SUCH_PORT
:
274 * - The route contains a non-existent port.
275 * - The route contains a non-PHY port (e.g. PCIe).
276 * - The port in cfg_read/cfg_write does not exist.
278 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Invalid port\n",
279 res
->response_route
, res
->response_port
);
281 case TB_CFG_ERROR_LOOP
:
282 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Route contains a loop\n",
283 res
->response_route
, res
->response_port
);
286 /* 5,6,7,9 and 11 are also valid error codes */
287 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Unknown error\n",
288 res
->response_route
, res
->response_port
);
293 static __be32
tb_crc(const void *data
, size_t len
)
295 return cpu_to_be32(~__crc32c_le(~0, data
, len
));
298 static void tb_ctl_pkg_free(struct ctl_pkg
*pkg
)
301 dma_pool_free(pkg
->ctl
->frame_pool
,
302 pkg
->buffer
, pkg
->frame
.buffer_phy
);
307 static struct ctl_pkg
*tb_ctl_pkg_alloc(struct tb_ctl
*ctl
)
309 struct ctl_pkg
*pkg
= kzalloc(sizeof(*pkg
), GFP_KERNEL
);
313 pkg
->buffer
= dma_pool_alloc(ctl
->frame_pool
, GFP_KERNEL
,
314 &pkg
->frame
.buffer_phy
);
325 static void tb_ctl_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
328 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
329 tb_ctl_pkg_free(pkg
);
333 * tb_cfg_tx() - transmit a packet on the control channel
335 * len must be a multiple of four.
337 * Return: Returns 0 on success or an error code on failure.
339 static int tb_ctl_tx(struct tb_ctl
*ctl
, const void *data
, size_t len
,
340 enum tb_cfg_pkg_type type
)
344 if (len
% 4 != 0) { /* required for le->be conversion */
345 tb_ctl_WARN(ctl
, "TX: invalid size: %zu\n", len
);
348 if (len
> TB_FRAME_SIZE
- 4) { /* checksum is 4 bytes */
349 tb_ctl_WARN(ctl
, "TX: packet too large: %zu/%d\n",
350 len
, TB_FRAME_SIZE
- 4);
353 pkg
= tb_ctl_pkg_alloc(ctl
);
356 pkg
->frame
.callback
= tb_ctl_tx_callback
;
357 pkg
->frame
.size
= len
+ 4;
358 pkg
->frame
.sof
= type
;
359 pkg
->frame
.eof
= type
;
360 cpu_to_be32_array(pkg
->buffer
, data
, len
/ 4);
361 *(__be32
*) (pkg
->buffer
+ len
) = tb_crc(pkg
->buffer
, len
);
363 res
= tb_ring_tx(ctl
->tx
, &pkg
->frame
);
364 if (res
) /* ring is stopped */
365 tb_ctl_pkg_free(pkg
);
370 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
372 static bool tb_ctl_handle_event(struct tb_ctl
*ctl
, enum tb_cfg_pkg_type type
,
373 struct ctl_pkg
*pkg
, size_t size
)
375 return ctl
->callback(ctl
->callback_data
, type
, pkg
->buffer
, size
);
378 static void tb_ctl_rx_submit(struct ctl_pkg
*pkg
)
380 tb_ring_rx(pkg
->ctl
->rx
, &pkg
->frame
); /*
381 * We ignore failures during stop.
382 * All rx packets are referenced
383 * from ctl->rx_packets, so we do
388 static int tb_async_error(const struct ctl_pkg
*pkg
)
390 const struct cfg_error_pkg
*error
= (const struct cfg_error_pkg
*)pkg
;
392 if (pkg
->frame
.eof
!= TB_CFG_PKG_ERROR
)
395 switch (error
->error
) {
396 case TB_CFG_ERROR_LINK_ERROR
:
397 case TB_CFG_ERROR_HEC_ERROR_DETECTED
:
398 case TB_CFG_ERROR_FLOW_CONTROL_ERROR
:
406 static void tb_ctl_rx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
409 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
410 struct tb_cfg_request
*req
;
415 * ring is stopped, packet is referenced from
419 if (frame
->size
< 4 || frame
->size
% 4 != 0) {
420 tb_ctl_err(pkg
->ctl
, "RX: invalid size %#x, dropping packet\n",
425 frame
->size
-= 4; /* remove checksum */
426 crc32
= tb_crc(pkg
->buffer
, frame
->size
);
427 be32_to_cpu_array(pkg
->buffer
, pkg
->buffer
, frame
->size
/ 4);
429 switch (frame
->eof
) {
430 case TB_CFG_PKG_READ
:
431 case TB_CFG_PKG_WRITE
:
432 case TB_CFG_PKG_ERROR
:
433 case TB_CFG_PKG_OVERRIDE
:
434 case TB_CFG_PKG_RESET
:
435 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
437 "RX: checksum mismatch, dropping packet\n");
440 if (tb_async_error(pkg
)) {
441 tb_ctl_handle_event(pkg
->ctl
, frame
->eof
,
447 case TB_CFG_PKG_EVENT
:
448 case TB_CFG_PKG_XDOMAIN_RESP
:
449 case TB_CFG_PKG_XDOMAIN_REQ
:
450 if (*(__be32
*)(pkg
->buffer
+ frame
->size
) != crc32
) {
452 "RX: checksum mismatch, dropping packet\n");
456 case TB_CFG_PKG_ICM_EVENT
:
457 if (tb_ctl_handle_event(pkg
->ctl
, frame
->eof
, pkg
, frame
->size
))
466 * The received packet will be processed only if there is an
467 * active request and that the packet is what is expected. This
468 * prevents packets such as replies coming after timeout has
469 * triggered from messing with the active requests.
471 req
= tb_cfg_request_find(pkg
->ctl
, pkg
);
473 if (req
->copy(req
, pkg
))
474 schedule_work(&req
->work
);
475 tb_cfg_request_put(req
);
479 tb_ctl_rx_submit(pkg
);
482 static void tb_cfg_request_work(struct work_struct
*work
)
484 struct tb_cfg_request
*req
= container_of(work
, typeof(*req
), work
);
486 if (!test_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
))
487 req
->callback(req
->callback_data
);
489 tb_cfg_request_dequeue(req
);
490 tb_cfg_request_put(req
);
494 * tb_cfg_request() - Start control request not waiting for it to complete
495 * @ctl: Control channel to use
496 * @req: Request to start
497 * @callback: Callback called when the request is completed
498 * @callback_data: Data to be passed to @callback
500 * This queues @req on the given control channel without waiting for it
501 * to complete. When the request completes @callback is called.
503 int tb_cfg_request(struct tb_ctl
*ctl
, struct tb_cfg_request
*req
,
504 void (*callback
)(void *), void *callback_data
)
509 req
->callback
= callback
;
510 req
->callback_data
= callback_data
;
511 INIT_WORK(&req
->work
, tb_cfg_request_work
);
512 INIT_LIST_HEAD(&req
->list
);
514 tb_cfg_request_get(req
);
515 ret
= tb_cfg_request_enqueue(ctl
, req
);
519 ret
= tb_ctl_tx(ctl
, req
->request
, req
->request_size
,
525 schedule_work(&req
->work
);
530 tb_cfg_request_dequeue(req
);
532 tb_cfg_request_put(req
);
538 * tb_cfg_request_cancel() - Cancel a control request
539 * @req: Request to cancel
540 * @err: Error to assign to the request
542 * This function can be used to cancel ongoing request. It will wait
543 * until the request is not active anymore.
545 void tb_cfg_request_cancel(struct tb_cfg_request
*req
, int err
)
547 set_bit(TB_CFG_REQUEST_CANCELED
, &req
->flags
);
548 schedule_work(&req
->work
);
549 wait_event(tb_cfg_request_cancel_queue
, !tb_cfg_request_is_active(req
));
550 req
->result
.err
= err
;
553 static void tb_cfg_request_complete(void *data
)
559 * tb_cfg_request_sync() - Start control request and wait until it completes
560 * @ctl: Control channel to use
561 * @req: Request to start
562 * @timeout_msec: Timeout how long to wait @req to complete
564 * Starts a control request and waits until it completes. If timeout
565 * triggers the request is canceled before function returns. Note the
566 * caller needs to make sure only one message for given switch is active
569 struct tb_cfg_result
tb_cfg_request_sync(struct tb_ctl
*ctl
,
570 struct tb_cfg_request
*req
,
573 unsigned long timeout
= msecs_to_jiffies(timeout_msec
);
574 struct tb_cfg_result res
= { 0 };
575 DECLARE_COMPLETION_ONSTACK(done
);
578 ret
= tb_cfg_request(ctl
, req
, tb_cfg_request_complete
, &done
);
584 if (!wait_for_completion_timeout(&done
, timeout
))
585 tb_cfg_request_cancel(req
, -ETIMEDOUT
);
587 flush_work(&req
->work
);
592 /* public interface, alloc/start/stop/free */
595 * tb_ctl_alloc() - allocate a control channel
597 * cb will be invoked once for every hot plug event.
599 * Return: Returns a pointer on success or NULL on failure.
601 struct tb_ctl
*tb_ctl_alloc(struct tb_nhi
*nhi
, event_cb cb
, void *cb_data
)
604 struct tb_ctl
*ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
609 ctl
->callback_data
= cb_data
;
611 mutex_init(&ctl
->request_queue_lock
);
612 INIT_LIST_HEAD(&ctl
->request_queue
);
613 ctl
->frame_pool
= dma_pool_create("thunderbolt_ctl", &nhi
->pdev
->dev
,
614 TB_FRAME_SIZE
, 4, 0);
615 if (!ctl
->frame_pool
)
618 ctl
->tx
= tb_ring_alloc_tx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
);
622 ctl
->rx
= tb_ring_alloc_rx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
, 0xffff,
627 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++) {
628 ctl
->rx_packets
[i
] = tb_ctl_pkg_alloc(ctl
);
629 if (!ctl
->rx_packets
[i
])
631 ctl
->rx_packets
[i
]->frame
.callback
= tb_ctl_rx_callback
;
634 tb_ctl_info(ctl
, "control channel created\n");
642 * tb_ctl_free() - free a control channel
644 * Must be called after tb_ctl_stop.
646 * Must NOT be called from ctl->callback.
648 void tb_ctl_free(struct tb_ctl
*ctl
)
656 tb_ring_free(ctl
->rx
);
658 tb_ring_free(ctl
->tx
);
660 /* free RX packets */
661 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
662 tb_ctl_pkg_free(ctl
->rx_packets
[i
]);
666 dma_pool_destroy(ctl
->frame_pool
);
671 * tb_cfg_start() - start/resume the control channel
673 void tb_ctl_start(struct tb_ctl
*ctl
)
676 tb_ctl_info(ctl
, "control channel starting...\n");
677 tb_ring_start(ctl
->tx
); /* is used to ack hotplug packets, start first */
678 tb_ring_start(ctl
->rx
);
679 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
680 tb_ctl_rx_submit(ctl
->rx_packets
[i
]);
686 * control() - pause the control channel
688 * All invocations of ctl->callback will have finished after this method
691 * Must NOT be called from ctl->callback.
693 void tb_ctl_stop(struct tb_ctl
*ctl
)
695 mutex_lock(&ctl
->request_queue_lock
);
696 ctl
->running
= false;
697 mutex_unlock(&ctl
->request_queue_lock
);
699 tb_ring_stop(ctl
->rx
);
700 tb_ring_stop(ctl
->tx
);
702 if (!list_empty(&ctl
->request_queue
))
703 tb_ctl_WARN(ctl
, "dangling request in request_queue\n");
704 INIT_LIST_HEAD(&ctl
->request_queue
);
705 tb_ctl_info(ctl
, "control channel stopped\n");
708 /* public interface, commands */
711 * tb_cfg_error() - send error packet
713 * Return: Returns 0 on success or an error code on failure.
715 int tb_cfg_error(struct tb_ctl
*ctl
, u64 route
, u32 port
,
716 enum tb_cfg_error error
)
718 struct cfg_error_pkg pkg
= {
719 .header
= tb_cfg_make_header(route
),
723 tb_ctl_info(ctl
, "resetting error on %llx:%x.\n", route
, port
);
724 return tb_ctl_tx(ctl
, &pkg
, sizeof(pkg
), TB_CFG_PKG_ERROR
);
727 static bool tb_cfg_match(const struct tb_cfg_request
*req
,
728 const struct ctl_pkg
*pkg
)
730 u64 route
= tb_cfg_get_route(pkg
->buffer
) & ~BIT_ULL(63);
732 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
735 if (pkg
->frame
.eof
!= req
->response_type
)
737 if (route
!= tb_cfg_get_route(req
->request
))
739 if (pkg
->frame
.size
!= req
->response_size
)
742 if (pkg
->frame
.eof
== TB_CFG_PKG_READ
||
743 pkg
->frame
.eof
== TB_CFG_PKG_WRITE
) {
744 const struct cfg_read_pkg
*req_hdr
= req
->request
;
745 const struct cfg_read_pkg
*res_hdr
= pkg
->buffer
;
747 if (req_hdr
->addr
.seq
!= res_hdr
->addr
.seq
)
754 static bool tb_cfg_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
756 struct tb_cfg_result res
;
758 /* Now make sure it is in expected format */
759 res
= parse_header(pkg
, req
->response_size
, req
->response_type
,
760 tb_cfg_get_route(req
->request
));
762 memcpy(req
->response
, pkg
->buffer
, req
->response_size
);
766 /* Always complete when first response is received */
771 * tb_cfg_reset() - send a reset packet and wait for a response
773 * If the switch at route is incorrectly configured then we will not receive a
774 * reply (even though the switch will reset). The caller should check for
775 * -ETIMEDOUT and attempt to reconfigure the switch.
777 struct tb_cfg_result
tb_cfg_reset(struct tb_ctl
*ctl
, u64 route
,
780 struct cfg_reset_pkg request
= { .header
= tb_cfg_make_header(route
) };
781 struct tb_cfg_result res
= { 0 };
782 struct tb_cfg_header reply
;
783 struct tb_cfg_request
*req
;
785 req
= tb_cfg_request_alloc();
791 req
->match
= tb_cfg_match
;
792 req
->copy
= tb_cfg_copy
;
793 req
->request
= &request
;
794 req
->request_size
= sizeof(request
);
795 req
->request_type
= TB_CFG_PKG_RESET
;
796 req
->response
= &reply
;
797 req
->response_size
= sizeof(reply
);
798 req
->response_type
= TB_CFG_PKG_RESET
;
800 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
802 tb_cfg_request_put(req
);
808 * tb_cfg_read() - read from config space into buffer
810 * Offset and length are in dwords.
812 struct tb_cfg_result
tb_cfg_read_raw(struct tb_ctl
*ctl
, void *buffer
,
813 u64 route
, u32 port
, enum tb_cfg_space space
,
814 u32 offset
, u32 length
, int timeout_msec
)
816 struct tb_cfg_result res
= { 0 };
817 struct cfg_read_pkg request
= {
818 .header
= tb_cfg_make_header(route
),
826 struct cfg_write_pkg reply
;
829 while (retries
< TB_CTL_RETRIES
) {
830 struct tb_cfg_request
*req
;
832 req
= tb_cfg_request_alloc();
838 request
.addr
.seq
= retries
++;
840 req
->match
= tb_cfg_match
;
841 req
->copy
= tb_cfg_copy
;
842 req
->request
= &request
;
843 req
->request_size
= sizeof(request
);
844 req
->request_type
= TB_CFG_PKG_READ
;
845 req
->response
= &reply
;
846 req
->response_size
= 12 + 4 * length
;
847 req
->response_type
= TB_CFG_PKG_READ
;
849 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
851 tb_cfg_request_put(req
);
853 if (res
.err
!= -ETIMEDOUT
)
856 /* Wait a bit (arbitrary time) until we send a retry */
857 usleep_range(10, 100);
863 res
.response_port
= reply
.addr
.port
;
864 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
866 memcpy(buffer
, &reply
.data
, 4 * length
);
871 * tb_cfg_write() - write from buffer into config space
873 * Offset and length are in dwords.
875 struct tb_cfg_result
tb_cfg_write_raw(struct tb_ctl
*ctl
, const void *buffer
,
876 u64 route
, u32 port
, enum tb_cfg_space space
,
877 u32 offset
, u32 length
, int timeout_msec
)
879 struct tb_cfg_result res
= { 0 };
880 struct cfg_write_pkg request
= {
881 .header
= tb_cfg_make_header(route
),
889 struct cfg_read_pkg reply
;
892 memcpy(&request
.data
, buffer
, length
* 4);
894 while (retries
< TB_CTL_RETRIES
) {
895 struct tb_cfg_request
*req
;
897 req
= tb_cfg_request_alloc();
903 request
.addr
.seq
= retries
++;
905 req
->match
= tb_cfg_match
;
906 req
->copy
= tb_cfg_copy
;
907 req
->request
= &request
;
908 req
->request_size
= 12 + 4 * length
;
909 req
->request_type
= TB_CFG_PKG_WRITE
;
910 req
->response
= &reply
;
911 req
->response_size
= sizeof(reply
);
912 req
->response_type
= TB_CFG_PKG_WRITE
;
914 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
916 tb_cfg_request_put(req
);
918 if (res
.err
!= -ETIMEDOUT
)
921 /* Wait a bit (arbitrary time) until we send a retry */
922 usleep_range(10, 100);
928 res
.response_port
= reply
.addr
.port
;
929 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
933 int tb_cfg_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
, u32 port
,
934 enum tb_cfg_space space
, u32 offset
, u32 length
)
936 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, buffer
, route
, port
,
937 space
, offset
, length
, TB_CFG_DEFAULT_TIMEOUT
);
944 /* Thunderbolt error, tb_error holds the actual number */
945 tb_cfg_print_error(ctl
, &res
);
949 tb_ctl_warn(ctl
, "timeout reading config space %u from %#x\n",
954 WARN(1, "tb_cfg_read: %d\n", res
.err
);
960 int tb_cfg_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
, u32 port
,
961 enum tb_cfg_space space
, u32 offset
, u32 length
)
963 struct tb_cfg_result res
= tb_cfg_write_raw(ctl
, buffer
, route
, port
,
964 space
, offset
, length
, TB_CFG_DEFAULT_TIMEOUT
);
971 /* Thunderbolt error, tb_error holds the actual number */
972 tb_cfg_print_error(ctl
, &res
);
976 tb_ctl_warn(ctl
, "timeout writing config space %u to %#x\n",
981 WARN(1, "tb_cfg_write: %d\n", res
.err
);
988 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
990 * Reads the first dword from the switches TB_CFG_SWITCH config area and
991 * returns the port number from which the reply originated.
993 * Return: Returns the upstream port number on success or an error code on
996 int tb_cfg_get_upstream_port(struct tb_ctl
*ctl
, u64 route
)
999 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, &dummy
, route
, 0,
1000 TB_CFG_SWITCH
, 0, 1,
1001 TB_CFG_DEFAULT_TIMEOUT
);
1006 return res
.response_port
;