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_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 = NULL, *iter;
163 mutex_lock(&pkg->ctl->request_queue_lock);
164 list_for_each_entry(iter, &pkg->ctl->request_queue, list) {
165 tb_cfg_request_get(iter);
166 if (iter->match(iter, pkg)) {
170 tb_cfg_request_put(iter);
172 mutex_unlock(&pkg->ctl->request_queue_lock);
177 /* utility functions */
180 static int check_header(const struct ctl_pkg *pkg, u32 len,
181 enum tb_cfg_pkg_type type, u64 route)
183 struct tb_cfg_header *header = pkg->buffer;
185 /* check frame, TODO: frame flags */
186 if (WARN(len != pkg->frame.size,
187 "wrong framesize (expected %#x, got %#x)\n",
188 len, pkg->frame.size))
190 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
191 type, pkg->frame.eof))
193 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
198 if (WARN(header->unknown != 1 << 9,
199 "header->unknown is %#x\n", header->unknown))
201 if (WARN(route != tb_cfg_get_route(header),
202 "wrong route (expected %llx, got %llx)",
203 route, tb_cfg_get_route(header)))
208 static int check_config_address(struct tb_cfg_address addr,
209 enum tb_cfg_space space, u32 offset,
212 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
214 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
217 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
218 offset, addr.offset))
220 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
221 length, addr.length))
224 * We cannot check addr->port as it is set to the upstream port of the
230 static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
232 struct cfg_error_pkg *pkg = response->buffer;
233 struct tb_ctl *ctl = response->ctl;
234 struct tb_cfg_result res = { 0 };
235 res.response_route = tb_cfg_get_route(&pkg->header);
236 res.response_port = 0;
237 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
238 tb_cfg_get_route(&pkg->header));
243 tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1);
245 tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2);
247 tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3);
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,
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(ctl, "%llx:%x: invalid config space or offset\n",
286 res->response_route, res->response_port);
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;
380 cpu_to_be32_array(pkg->buffer, data, len / 4);
381 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
383 res = tb_ring_tx(ctl->tx, &pkg->frame);
384 if (res) /* ring is stopped */
385 tb_ctl_pkg_free(pkg);
390 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
392 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
393 struct ctl_pkg *pkg, size_t size)
395 return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
398 static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
400 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
401 * We ignore failures during stop.
402 * All rx packets are referenced
403 * from ctl->rx_packets, so we do
408 static int tb_async_error(const struct ctl_pkg *pkg)
410 const struct cfg_error_pkg *error = pkg->buffer;
412 if (pkg->frame.eof != TB_CFG_PKG_ERROR)
415 switch (error->error) {
416 case TB_CFG_ERROR_LINK_ERROR:
417 case TB_CFG_ERROR_HEC_ERROR_DETECTED:
418 case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
426 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
429 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
430 struct tb_cfg_request *req;
435 * ring is stopped, packet is referenced from
439 if (frame->size < 4 || frame->size % 4 != 0) {
440 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
445 frame->size -= 4; /* remove checksum */
446 crc32 = tb_crc(pkg->buffer, frame->size);
447 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
449 switch (frame->eof) {
450 case TB_CFG_PKG_READ:
451 case TB_CFG_PKG_WRITE:
452 case TB_CFG_PKG_ERROR:
453 case TB_CFG_PKG_OVERRIDE:
454 case TB_CFG_PKG_RESET:
455 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
457 "RX: checksum mismatch, dropping packet\n");
460 if (tb_async_error(pkg)) {
461 tb_ctl_handle_event(pkg->ctl, frame->eof,
467 case TB_CFG_PKG_EVENT:
468 case TB_CFG_PKG_XDOMAIN_RESP:
469 case TB_CFG_PKG_XDOMAIN_REQ:
470 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
472 "RX: checksum mismatch, dropping packet\n");
476 case TB_CFG_PKG_ICM_EVENT:
477 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
486 * The received packet will be processed only if there is an
487 * active request and that the packet is what is expected. This
488 * prevents packets such as replies coming after timeout has
489 * triggered from messing with the active requests.
491 req = tb_cfg_request_find(pkg->ctl, pkg);
493 if (req->copy(req, pkg))
494 schedule_work(&req->work);
495 tb_cfg_request_put(req);
499 tb_ctl_rx_submit(pkg);
502 static void tb_cfg_request_work(struct work_struct *work)
504 struct tb_cfg_request *req = container_of(work, typeof(*req), work);
506 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
507 req->callback(req->callback_data);
509 tb_cfg_request_dequeue(req);
510 tb_cfg_request_put(req);
514 * tb_cfg_request() - Start control request not waiting for it to complete
515 * @ctl: Control channel to use
516 * @req: Request to start
517 * @callback: Callback called when the request is completed
518 * @callback_data: Data to be passed to @callback
520 * This queues @req on the given control channel without waiting for it
521 * to complete. When the request completes @callback is called.
523 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
524 void (*callback)(void *), void *callback_data)
529 req->callback = callback;
530 req->callback_data = callback_data;
531 INIT_WORK(&req->work, tb_cfg_request_work);
532 INIT_LIST_HEAD(&req->list);
534 tb_cfg_request_get(req);
535 ret = tb_cfg_request_enqueue(ctl, req);
539 ret = tb_ctl_tx(ctl, req->request, req->request_size,
545 schedule_work(&req->work);
550 tb_cfg_request_dequeue(req);
552 tb_cfg_request_put(req);
558 * tb_cfg_request_cancel() - Cancel a control request
559 * @req: Request to cancel
560 * @err: Error to assign to the request
562 * This function can be used to cancel ongoing request. It will wait
563 * until the request is not active anymore.
565 void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
567 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
568 schedule_work(&req->work);
569 wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
570 req->result.err = err;
573 static void tb_cfg_request_complete(void *data)
579 * tb_cfg_request_sync() - Start control request and wait until it completes
580 * @ctl: Control channel to use
581 * @req: Request to start
582 * @timeout_msec: Timeout how long to wait @req to complete
584 * Starts a control request and waits until it completes. If timeout
585 * triggers the request is canceled before function returns. Note the
586 * caller needs to make sure only one message for given switch is active
589 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
590 struct tb_cfg_request *req,
593 unsigned long timeout = msecs_to_jiffies(timeout_msec);
594 struct tb_cfg_result res = { 0 };
595 DECLARE_COMPLETION_ONSTACK(done);
598 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
604 if (!wait_for_completion_timeout(&done, timeout))
605 tb_cfg_request_cancel(req, -ETIMEDOUT);
607 flush_work(&req->work);
612 /* public interface, alloc/start/stop/free */
615 * tb_ctl_alloc() - allocate a control channel
616 * @nhi: Pointer to NHI
617 * @timeout_msec: Default timeout used with non-raw control messages
618 * @cb: Callback called for plug events
619 * @cb_data: Data passed to @cb
621 * cb will be invoked once for every hot plug event.
623 * Return: Returns a pointer on success or NULL on failure.
625 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int timeout_msec, event_cb cb,
629 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
633 ctl->timeout_msec = timeout_msec;
635 ctl->callback_data = cb_data;
637 mutex_init(&ctl->request_queue_lock);
638 INIT_LIST_HEAD(&ctl->request_queue);
639 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
640 TB_FRAME_SIZE, 4, 0);
641 if (!ctl->frame_pool)
644 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
648 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
653 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
654 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
655 if (!ctl->rx_packets[i])
657 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
660 tb_ctl_dbg(ctl, "control channel created\n");
668 * tb_ctl_free() - free a control channel
669 * @ctl: Control channel to free
671 * Must be called after tb_ctl_stop.
673 * Must NOT be called from ctl->callback.
675 void tb_ctl_free(struct tb_ctl *ctl)
683 tb_ring_free(ctl->rx);
685 tb_ring_free(ctl->tx);
687 /* free RX packets */
688 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
689 tb_ctl_pkg_free(ctl->rx_packets[i]);
692 dma_pool_destroy(ctl->frame_pool);
697 * tb_ctl_start() - start/resume the control channel
698 * @ctl: Control channel to start
700 void tb_ctl_start(struct tb_ctl *ctl)
703 tb_ctl_dbg(ctl, "control channel starting...\n");
704 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
705 tb_ring_start(ctl->rx);
706 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
707 tb_ctl_rx_submit(ctl->rx_packets[i]);
713 * tb_ctl_stop() - pause the control channel
714 * @ctl: Control channel to stop
716 * All invocations of ctl->callback will have finished after this method
719 * Must NOT be called from ctl->callback.
721 void tb_ctl_stop(struct tb_ctl *ctl)
723 mutex_lock(&ctl->request_queue_lock);
724 ctl->running = false;
725 mutex_unlock(&ctl->request_queue_lock);
727 tb_ring_stop(ctl->rx);
728 tb_ring_stop(ctl->tx);
730 if (!list_empty(&ctl->request_queue))
731 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
732 INIT_LIST_HEAD(&ctl->request_queue);
733 tb_ctl_dbg(ctl, "control channel stopped\n");
736 /* public interface, commands */
739 * tb_cfg_ack_plug() - Ack hot plug/unplug event
740 * @ctl: Control channel to use
741 * @route: Router that originated the event
742 * @port: Port where the hot plug/unplug happened
743 * @unplug: Ack hot plug or unplug
745 * Call this as response for hot plug/unplug event to ack it.
746 * Returns %0 on success or an error code on failure.
748 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
750 struct cfg_error_pkg pkg = {
751 .header = tb_cfg_make_header(route),
753 .error = TB_CFG_ERROR_ACK_PLUG_EVENT,
754 .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
755 : TB_CFG_ERROR_PG_HOT_PLUG,
757 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n",
758 unplug ? "un" : "", route, port);
759 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
762 static bool tb_cfg_match(const struct tb_cfg_request *req,
763 const struct ctl_pkg *pkg)
765 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
767 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
770 if (pkg->frame.eof != req->response_type)
772 if (route != tb_cfg_get_route(req->request))
774 if (pkg->frame.size != req->response_size)
777 if (pkg->frame.eof == TB_CFG_PKG_READ ||
778 pkg->frame.eof == TB_CFG_PKG_WRITE) {
779 const struct cfg_read_pkg *req_hdr = req->request;
780 const struct cfg_read_pkg *res_hdr = pkg->buffer;
782 if (req_hdr->addr.seq != res_hdr->addr.seq)
789 static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
791 struct tb_cfg_result res;
793 /* Now make sure it is in expected format */
794 res = parse_header(pkg, req->response_size, req->response_type,
795 tb_cfg_get_route(req->request));
797 memcpy(req->response, pkg->buffer, req->response_size);
801 /* Always complete when first response is received */
806 * tb_cfg_reset() - send a reset packet and wait for a response
807 * @ctl: Control channel pointer
808 * @route: Router string for the router to send reset
810 * If the switch at route is incorrectly configured then we will not receive a
811 * reply (even though the switch will reset). The caller should check for
812 * -ETIMEDOUT and attempt to reconfigure the switch.
814 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route)
816 struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
817 struct tb_cfg_result res = { 0 };
818 struct tb_cfg_header reply;
819 struct tb_cfg_request *req;
821 req = tb_cfg_request_alloc();
827 req->match = tb_cfg_match;
828 req->copy = tb_cfg_copy;
829 req->request = &request;
830 req->request_size = sizeof(request);
831 req->request_type = TB_CFG_PKG_RESET;
832 req->response = &reply;
833 req->response_size = sizeof(reply);
834 req->response_type = TB_CFG_PKG_RESET;
836 res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec);
838 tb_cfg_request_put(req);
844 * tb_cfg_read_raw() - read from config space into buffer
845 * @ctl: Pointer to the control channel
846 * @buffer: Buffer where the data is read
847 * @route: Route string of the router
848 * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
849 * @space: Config space selector
850 * @offset: Dword word offset of the register to start reading
851 * @length: Number of dwords to read
852 * @timeout_msec: Timeout in ms how long to wait for the response
854 * Reads from router config space without translating the possible error.
856 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
857 u64 route, u32 port, enum tb_cfg_space space,
858 u32 offset, u32 length, int timeout_msec)
860 struct tb_cfg_result res = { 0 };
861 struct cfg_read_pkg request = {
862 .header = tb_cfg_make_header(route),
870 struct cfg_write_pkg reply;
873 while (retries < TB_CTL_RETRIES) {
874 struct tb_cfg_request *req;
876 req = tb_cfg_request_alloc();
882 request.addr.seq = retries++;
884 req->match = tb_cfg_match;
885 req->copy = tb_cfg_copy;
886 req->request = &request;
887 req->request_size = sizeof(request);
888 req->request_type = TB_CFG_PKG_READ;
889 req->response = &reply;
890 req->response_size = 12 + 4 * length;
891 req->response_type = TB_CFG_PKG_READ;
893 res = tb_cfg_request_sync(ctl, req, timeout_msec);
895 tb_cfg_request_put(req);
897 if (res.err != -ETIMEDOUT)
900 /* Wait a bit (arbitrary time) until we send a retry */
901 usleep_range(10, 100);
907 res.response_port = reply.addr.port;
908 res.err = check_config_address(reply.addr, space, offset, length);
910 memcpy(buffer, &reply.data, 4 * length);
915 * tb_cfg_write_raw() - write from buffer into config space
916 * @ctl: Pointer to the control channel
917 * @buffer: Data to write
918 * @route: Route string of the router
919 * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
920 * @space: Config space selector
921 * @offset: Dword word offset of the register to start writing
922 * @length: Number of dwords to write
923 * @timeout_msec: Timeout in ms how long to wait for the response
925 * Writes to router config space without translating the possible error.
927 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
928 u64 route, u32 port, enum tb_cfg_space space,
929 u32 offset, u32 length, int timeout_msec)
931 struct tb_cfg_result res = { 0 };
932 struct cfg_write_pkg request = {
933 .header = tb_cfg_make_header(route),
941 struct cfg_read_pkg reply;
944 memcpy(&request.data, buffer, length * 4);
946 while (retries < TB_CTL_RETRIES) {
947 struct tb_cfg_request *req;
949 req = tb_cfg_request_alloc();
955 request.addr.seq = retries++;
957 req->match = tb_cfg_match;
958 req->copy = tb_cfg_copy;
959 req->request = &request;
960 req->request_size = 12 + 4 * length;
961 req->request_type = TB_CFG_PKG_WRITE;
962 req->response = &reply;
963 req->response_size = sizeof(reply);
964 req->response_type = TB_CFG_PKG_WRITE;
966 res = tb_cfg_request_sync(ctl, req, timeout_msec);
968 tb_cfg_request_put(req);
970 if (res.err != -ETIMEDOUT)
973 /* Wait a bit (arbitrary time) until we send a retry */
974 usleep_range(10, 100);
980 res.response_port = reply.addr.port;
981 res.err = check_config_address(reply.addr, space, offset, length);
985 static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
986 const struct tb_cfg_result *res)
989 * For unimplemented ports access to port config space may return
990 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
991 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
992 * that the caller can mark the port as disabled.
994 if (space == TB_CFG_PORT &&
995 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
998 tb_cfg_print_error(ctl, res);
1000 if (res->tb_error == TB_CFG_ERROR_LOCK)
1002 else if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED)
1008 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
1009 enum tb_cfg_space space, u32 offset, u32 length)
1011 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
1012 space, offset, length, ctl->timeout_msec);
1019 /* Thunderbolt error, tb_error holds the actual number */
1020 return tb_cfg_get_error(ctl, space, &res);
1023 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
1024 route, space, offset);
1028 WARN(1, "tb_cfg_read: %d\n", res.err);
1034 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
1035 enum tb_cfg_space space, u32 offset, u32 length)
1037 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
1038 space, offset, length, ctl->timeout_msec);
1045 /* Thunderbolt error, tb_error holds the actual number */
1046 return tb_cfg_get_error(ctl, space, &res);
1049 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
1050 route, space, offset);
1054 WARN(1, "tb_cfg_write: %d\n", res.err);
1061 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1062 * @ctl: Pointer to the control channel
1063 * @route: Route string of the router
1065 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1066 * returns the port number from which the reply originated.
1068 * Return: Returns the upstream port number on success or an error code on
1071 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1074 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1075 TB_CFG_SWITCH, 0, 1,
1081 return res.response_port;