1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - control channel and configuration commands
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 * @callback: Callback called when hotplug message is received
33 * @callback_data: Data passed to @callback
40 struct dma_pool *frame_pool;
41 struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
42 struct mutex request_queue_lock;
43 struct list_head request_queue;
51 #define tb_ctl_WARN(ctl, format, arg...) \
52 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
54 #define tb_ctl_err(ctl, format, arg...) \
55 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
57 #define tb_ctl_warn(ctl, format, arg...) \
58 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
60 #define tb_ctl_info(ctl, format, arg...) \
61 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
63 #define tb_ctl_dbg(ctl, format, arg...) \
64 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
66 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
67 /* Serializes access to request kref_get/put */
68 static DEFINE_MUTEX(tb_cfg_request_lock);
71 * tb_cfg_request_alloc() - Allocates a new config request
73 * This is refcounted object so when you are done with this, call
74 * tb_cfg_request_put() to it.
76 struct tb_cfg_request *tb_cfg_request_alloc(void)
78 struct tb_cfg_request *req;
80 req = kzalloc(sizeof(*req), GFP_KERNEL);
84 kref_init(&req->kref);
90 * tb_cfg_request_get() - Increase refcount of a request
91 * @req: Request whose refcount is increased
93 void tb_cfg_request_get(struct tb_cfg_request *req)
95 mutex_lock(&tb_cfg_request_lock);
97 mutex_unlock(&tb_cfg_request_lock);
100 static void tb_cfg_request_destroy(struct kref *kref)
102 struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
108 * tb_cfg_request_put() - Decrease refcount and possibly release the request
109 * @req: Request whose refcount is decreased
111 * Call this function when you are done with the request. When refcount
112 * goes to %0 the object is released.
114 void tb_cfg_request_put(struct tb_cfg_request *req)
116 mutex_lock(&tb_cfg_request_lock);
117 kref_put(&req->kref, tb_cfg_request_destroy);
118 mutex_unlock(&tb_cfg_request_lock);
121 static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
122 struct tb_cfg_request *req)
124 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
127 mutex_lock(&ctl->request_queue_lock);
129 mutex_unlock(&ctl->request_queue_lock);
133 list_add_tail(&req->list, &ctl->request_queue);
134 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
135 mutex_unlock(&ctl->request_queue_lock);
139 static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
141 struct tb_ctl *ctl = req->ctl;
143 mutex_lock(&ctl->request_queue_lock);
144 list_del(&req->list);
145 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
146 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
147 wake_up(&tb_cfg_request_cancel_queue);
148 mutex_unlock(&ctl->request_queue_lock);
151 static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
153 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
156 static struct tb_cfg_request *
157 tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
159 struct tb_cfg_request *req;
162 mutex_lock(&pkg->ctl->request_queue_lock);
163 list_for_each_entry(req, &pkg->ctl->request_queue, list) {
164 tb_cfg_request_get(req);
165 if (req->match(req, pkg)) {
169 tb_cfg_request_put(req);
171 mutex_unlock(&pkg->ctl->request_queue_lock);
173 return found ? req : NULL;
176 /* utility functions */
179 static int check_header(const struct ctl_pkg *pkg, u32 len,
180 enum tb_cfg_pkg_type type, u64 route)
182 struct tb_cfg_header *header = pkg->buffer;
184 /* check frame, TODO: frame flags */
185 if (WARN(len != pkg->frame.size,
186 "wrong framesize (expected %#x, got %#x)\n",
187 len, pkg->frame.size))
189 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
190 type, pkg->frame.eof))
192 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
197 if (WARN(header->unknown != 1 << 9,
198 "header->unknown is %#x\n", header->unknown))
200 if (WARN(route != tb_cfg_get_route(header),
201 "wrong route (expected %llx, got %llx)",
202 route, tb_cfg_get_route(header)))
207 static int check_config_address(struct tb_cfg_address addr,
208 enum tb_cfg_space space, u32 offset,
211 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
213 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
216 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
217 offset, addr.offset))
219 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
220 length, addr.length))
223 * We cannot check addr->port as it is set to the upstream port of the
229 static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
231 struct cfg_error_pkg *pkg = response->buffer;
232 struct tb_ctl *ctl = response->ctl;
233 struct tb_cfg_result res = { 0 };
234 res.response_route = tb_cfg_get_route(&pkg->header);
235 res.response_port = 0;
236 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
237 tb_cfg_get_route(&pkg->header));
242 tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1);
244 tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2);
246 tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3);
249 res.tb_error = pkg->error;
250 res.response_port = pkg->port;
255 static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
256 enum tb_cfg_pkg_type type, u64 route)
258 struct tb_cfg_header *header = pkg->buffer;
259 struct tb_cfg_result res = { 0 };
261 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
262 return decode_error(pkg);
264 res.response_port = 0; /* will be updated later for cfg_read/write */
265 res.response_route = tb_cfg_get_route(header);
266 res.err = check_header(pkg, len, type, route);
270 static void tb_cfg_print_error(struct tb_ctl *ctl,
271 const struct tb_cfg_result *res)
273 WARN_ON(res->err != 1);
274 switch (res->tb_error) {
275 case TB_CFG_ERROR_PORT_NOT_CONNECTED:
276 /* Port is not connected. This can happen during surprise
277 * removal. Do not warn. */
279 case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
281 * Invalid cfg_space/offset/length combination in
282 * cfg_read/cfg_write.
284 tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
285 res->response_route, res->response_port);
287 case TB_CFG_ERROR_NO_SUCH_PORT:
289 * - The route contains a non-existent port.
290 * - The route contains a non-PHY port (e.g. PCIe).
291 * - The port in cfg_read/cfg_write does not exist.
293 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
294 res->response_route, res->response_port);
296 case TB_CFG_ERROR_LOOP:
297 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
298 res->response_route, res->response_port);
300 case TB_CFG_ERROR_LOCK:
301 tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n",
302 res->response_route, res->response_port);
305 /* 5,6,7,9 and 11 are also valid error codes */
306 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
307 res->response_route, res->response_port);
312 static __be32 tb_crc(const void *data, size_t len)
314 return cpu_to_be32(~__crc32c_le(~0, data, len));
317 static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
320 dma_pool_free(pkg->ctl->frame_pool,
321 pkg->buffer, pkg->frame.buffer_phy);
326 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
328 struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
332 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
333 &pkg->frame.buffer_phy);
344 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
347 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
348 tb_ctl_pkg_free(pkg);
352 * tb_cfg_tx() - transmit a packet on the control channel
354 * len must be a multiple of four.
356 * Return: Returns 0 on success or an error code on failure.
358 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
359 enum tb_cfg_pkg_type type)
363 if (len % 4 != 0) { /* required for le->be conversion */
364 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
367 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
368 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
369 len, TB_FRAME_SIZE - 4);
372 pkg = tb_ctl_pkg_alloc(ctl);
375 pkg->frame.callback = tb_ctl_tx_callback;
376 pkg->frame.size = len + 4;
377 pkg->frame.sof = type;
378 pkg->frame.eof = type;
379 cpu_to_be32_array(pkg->buffer, data, len / 4);
380 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
382 res = tb_ring_tx(ctl->tx, &pkg->frame);
383 if (res) /* ring is stopped */
384 tb_ctl_pkg_free(pkg);
389 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
391 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
392 struct ctl_pkg *pkg, size_t size)
394 return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
397 static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
399 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
400 * We ignore failures during stop.
401 * All rx packets are referenced
402 * from ctl->rx_packets, so we do
407 static int tb_async_error(const struct ctl_pkg *pkg)
409 const struct cfg_error_pkg *error = (const struct cfg_error_pkg *)pkg;
411 if (pkg->frame.eof != TB_CFG_PKG_ERROR)
414 switch (error->error) {
415 case TB_CFG_ERROR_LINK_ERROR:
416 case TB_CFG_ERROR_HEC_ERROR_DETECTED:
417 case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
425 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
428 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
429 struct tb_cfg_request *req;
434 * ring is stopped, packet is referenced from
438 if (frame->size < 4 || frame->size % 4 != 0) {
439 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
444 frame->size -= 4; /* remove checksum */
445 crc32 = tb_crc(pkg->buffer, frame->size);
446 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
448 switch (frame->eof) {
449 case TB_CFG_PKG_READ:
450 case TB_CFG_PKG_WRITE:
451 case TB_CFG_PKG_ERROR:
452 case TB_CFG_PKG_OVERRIDE:
453 case TB_CFG_PKG_RESET:
454 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
456 "RX: checksum mismatch, dropping packet\n");
459 if (tb_async_error(pkg)) {
460 tb_ctl_handle_event(pkg->ctl, frame->eof,
466 case TB_CFG_PKG_EVENT:
467 case TB_CFG_PKG_XDOMAIN_RESP:
468 case TB_CFG_PKG_XDOMAIN_REQ:
469 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
471 "RX: checksum mismatch, dropping packet\n");
475 case TB_CFG_PKG_ICM_EVENT:
476 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
485 * The received packet will be processed only if there is an
486 * active request and that the packet is what is expected. This
487 * prevents packets such as replies coming after timeout has
488 * triggered from messing with the active requests.
490 req = tb_cfg_request_find(pkg->ctl, pkg);
492 if (req->copy(req, pkg))
493 schedule_work(&req->work);
494 tb_cfg_request_put(req);
498 tb_ctl_rx_submit(pkg);
501 static void tb_cfg_request_work(struct work_struct *work)
503 struct tb_cfg_request *req = container_of(work, typeof(*req), work);
505 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
506 req->callback(req->callback_data);
508 tb_cfg_request_dequeue(req);
509 tb_cfg_request_put(req);
513 * tb_cfg_request() - Start control request not waiting for it to complete
514 * @ctl: Control channel to use
515 * @req: Request to start
516 * @callback: Callback called when the request is completed
517 * @callback_data: Data to be passed to @callback
519 * This queues @req on the given control channel without waiting for it
520 * to complete. When the request completes @callback is called.
522 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
523 void (*callback)(void *), void *callback_data)
528 req->callback = callback;
529 req->callback_data = callback_data;
530 INIT_WORK(&req->work, tb_cfg_request_work);
531 INIT_LIST_HEAD(&req->list);
533 tb_cfg_request_get(req);
534 ret = tb_cfg_request_enqueue(ctl, req);
538 ret = tb_ctl_tx(ctl, req->request, req->request_size,
544 schedule_work(&req->work);
549 tb_cfg_request_dequeue(req);
551 tb_cfg_request_put(req);
557 * tb_cfg_request_cancel() - Cancel a control request
558 * @req: Request to cancel
559 * @err: Error to assign to the request
561 * This function can be used to cancel ongoing request. It will wait
562 * until the request is not active anymore.
564 void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
566 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
567 schedule_work(&req->work);
568 wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
569 req->result.err = err;
572 static void tb_cfg_request_complete(void *data)
578 * tb_cfg_request_sync() - Start control request and wait until it completes
579 * @ctl: Control channel to use
580 * @req: Request to start
581 * @timeout_msec: Timeout how long to wait @req to complete
583 * Starts a control request and waits until it completes. If timeout
584 * triggers the request is canceled before function returns. Note the
585 * caller needs to make sure only one message for given switch is active
588 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
589 struct tb_cfg_request *req,
592 unsigned long timeout = msecs_to_jiffies(timeout_msec);
593 struct tb_cfg_result res = { 0 };
594 DECLARE_COMPLETION_ONSTACK(done);
597 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
603 if (!wait_for_completion_timeout(&done, timeout))
604 tb_cfg_request_cancel(req, -ETIMEDOUT);
606 flush_work(&req->work);
611 /* public interface, alloc/start/stop/free */
614 * tb_ctl_alloc() - allocate a control channel
615 * @nhi: Pointer to NHI
616 * @cb: Callback called for plug events
617 * @cb_data: Data passed to @cb
619 * cb will be invoked once for every hot plug event.
621 * Return: Returns a pointer on success or NULL on failure.
623 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data)
626 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
631 ctl->callback_data = cb_data;
633 mutex_init(&ctl->request_queue_lock);
634 INIT_LIST_HEAD(&ctl->request_queue);
635 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
636 TB_FRAME_SIZE, 4, 0);
637 if (!ctl->frame_pool)
640 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
644 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
649 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
650 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
651 if (!ctl->rx_packets[i])
653 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
656 tb_ctl_dbg(ctl, "control channel created\n");
664 * tb_ctl_free() - free a control channel
665 * @ctl: Control channel to free
667 * Must be called after tb_ctl_stop.
669 * Must NOT be called from ctl->callback.
671 void tb_ctl_free(struct tb_ctl *ctl)
679 tb_ring_free(ctl->rx);
681 tb_ring_free(ctl->tx);
683 /* free RX packets */
684 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
685 tb_ctl_pkg_free(ctl->rx_packets[i]);
688 dma_pool_destroy(ctl->frame_pool);
693 * tb_cfg_start() - start/resume the control channel
694 * @ctl: Control channel to start
696 void tb_ctl_start(struct tb_ctl *ctl)
699 tb_ctl_dbg(ctl, "control channel starting...\n");
700 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
701 tb_ring_start(ctl->rx);
702 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
703 tb_ctl_rx_submit(ctl->rx_packets[i]);
709 * tb_ctrl_stop() - pause the control channel
710 * @ctl: Control channel to stop
712 * All invocations of ctl->callback will have finished after this method
715 * Must NOT be called from ctl->callback.
717 void tb_ctl_stop(struct tb_ctl *ctl)
719 mutex_lock(&ctl->request_queue_lock);
720 ctl->running = false;
721 mutex_unlock(&ctl->request_queue_lock);
723 tb_ring_stop(ctl->rx);
724 tb_ring_stop(ctl->tx);
726 if (!list_empty(&ctl->request_queue))
727 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
728 INIT_LIST_HEAD(&ctl->request_queue);
729 tb_ctl_dbg(ctl, "control channel stopped\n");
732 /* public interface, commands */
735 * tb_cfg_ack_plug() - Ack hot plug/unplug event
736 * @ctl: Control channel to use
737 * @route: Router that originated the event
738 * @port: Port where the hot plug/unplug happened
739 * @unplug: Ack hot plug or unplug
741 * Call this as response for hot plug/unplug event to ack it.
742 * Returns %0 on success or an error code on failure.
744 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
746 struct cfg_error_pkg pkg = {
747 .header = tb_cfg_make_header(route),
749 .error = TB_CFG_ERROR_ACK_PLUG_EVENT,
750 .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
751 : TB_CFG_ERROR_PG_HOT_PLUG,
753 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n",
754 unplug ? "un" : "", route, port);
755 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
758 static bool tb_cfg_match(const struct tb_cfg_request *req,
759 const struct ctl_pkg *pkg)
761 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
763 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
766 if (pkg->frame.eof != req->response_type)
768 if (route != tb_cfg_get_route(req->request))
770 if (pkg->frame.size != req->response_size)
773 if (pkg->frame.eof == TB_CFG_PKG_READ ||
774 pkg->frame.eof == TB_CFG_PKG_WRITE) {
775 const struct cfg_read_pkg *req_hdr = req->request;
776 const struct cfg_read_pkg *res_hdr = pkg->buffer;
778 if (req_hdr->addr.seq != res_hdr->addr.seq)
785 static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
787 struct tb_cfg_result res;
789 /* Now make sure it is in expected format */
790 res = parse_header(pkg, req->response_size, req->response_type,
791 tb_cfg_get_route(req->request));
793 memcpy(req->response, pkg->buffer, req->response_size);
797 /* Always complete when first response is received */
802 * tb_cfg_reset() - send a reset packet and wait for a response
803 * @ctl: Control channel pointer
804 * @route: Router string for the router to send reset
805 * @timeout_msec: Timeout in ms how long to wait for the response
807 * If the switch at route is incorrectly configured then we will not receive a
808 * reply (even though the switch will reset). The caller should check for
809 * -ETIMEDOUT and attempt to reconfigure the switch.
811 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
814 struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
815 struct tb_cfg_result res = { 0 };
816 struct tb_cfg_header reply;
817 struct tb_cfg_request *req;
819 req = tb_cfg_request_alloc();
825 req->match = tb_cfg_match;
826 req->copy = tb_cfg_copy;
827 req->request = &request;
828 req->request_size = sizeof(request);
829 req->request_type = TB_CFG_PKG_RESET;
830 req->response = &reply;
831 req->response_size = sizeof(reply);
832 req->response_type = TB_CFG_PKG_RESET;
834 res = tb_cfg_request_sync(ctl, req, timeout_msec);
836 tb_cfg_request_put(req);
842 * tb_cfg_read_raw() - read from config space into buffer
843 * @ctl: Pointer to the control channel
844 * @buffer: Buffer where the data is read
845 * @route: Route string of the router
846 * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
847 * @space: Config space selector
848 * @offset: Dword word offset of the register to start reading
849 * @length: Number of dwords to read
850 * @timeout_msec: Timeout in ms how long to wait for the response
852 * Reads from router config space without translating the possible error.
854 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
855 u64 route, u32 port, enum tb_cfg_space space,
856 u32 offset, u32 length, int timeout_msec)
858 struct tb_cfg_result res = { 0 };
859 struct cfg_read_pkg request = {
860 .header = tb_cfg_make_header(route),
868 struct cfg_write_pkg reply;
871 while (retries < TB_CTL_RETRIES) {
872 struct tb_cfg_request *req;
874 req = tb_cfg_request_alloc();
880 request.addr.seq = retries++;
882 req->match = tb_cfg_match;
883 req->copy = tb_cfg_copy;
884 req->request = &request;
885 req->request_size = sizeof(request);
886 req->request_type = TB_CFG_PKG_READ;
887 req->response = &reply;
888 req->response_size = 12 + 4 * length;
889 req->response_type = TB_CFG_PKG_READ;
891 res = tb_cfg_request_sync(ctl, req, timeout_msec);
893 tb_cfg_request_put(req);
895 if (res.err != -ETIMEDOUT)
898 /* Wait a bit (arbitrary time) until we send a retry */
899 usleep_range(10, 100);
905 res.response_port = reply.addr.port;
906 res.err = check_config_address(reply.addr, space, offset, length);
908 memcpy(buffer, &reply.data, 4 * length);
913 * tb_cfg_write() - write from buffer into config space
914 * @ctl: Pointer to the control channel
915 * @buffer: Data to write
916 * @route: Route string of the router
917 * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
918 * @space: Config space selector
919 * @offset: Dword word offset of the register to start writing
920 * @length: Number of dwords to write
921 * @timeout_msec: Timeout in ms how long to wait for the response
923 * Writes to router config space without translating the possible error.
925 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
926 u64 route, u32 port, enum tb_cfg_space space,
927 u32 offset, u32 length, int timeout_msec)
929 struct tb_cfg_result res = { 0 };
930 struct cfg_write_pkg request = {
931 .header = tb_cfg_make_header(route),
939 struct cfg_read_pkg reply;
942 memcpy(&request.data, buffer, length * 4);
944 while (retries < TB_CTL_RETRIES) {
945 struct tb_cfg_request *req;
947 req = tb_cfg_request_alloc();
953 request.addr.seq = retries++;
955 req->match = tb_cfg_match;
956 req->copy = tb_cfg_copy;
957 req->request = &request;
958 req->request_size = 12 + 4 * length;
959 req->request_type = TB_CFG_PKG_WRITE;
960 req->response = &reply;
961 req->response_size = sizeof(reply);
962 req->response_type = TB_CFG_PKG_WRITE;
964 res = tb_cfg_request_sync(ctl, req, timeout_msec);
966 tb_cfg_request_put(req);
968 if (res.err != -ETIMEDOUT)
971 /* Wait a bit (arbitrary time) until we send a retry */
972 usleep_range(10, 100);
978 res.response_port = reply.addr.port;
979 res.err = check_config_address(reply.addr, space, offset, length);
983 static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
984 const struct tb_cfg_result *res)
987 * For unimplemented ports access to port config space may return
988 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
989 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
990 * that the caller can mark the port as disabled.
992 if (space == TB_CFG_PORT &&
993 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
996 tb_cfg_print_error(ctl, res);
998 if (res->tb_error == TB_CFG_ERROR_LOCK)
1000 else if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED)
1006 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
1007 enum tb_cfg_space space, u32 offset, u32 length)
1009 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
1010 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
1017 /* Thunderbolt error, tb_error holds the actual number */
1018 return tb_cfg_get_error(ctl, space, &res);
1021 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
1022 route, space, offset);
1026 WARN(1, "tb_cfg_read: %d\n", res.err);
1032 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
1033 enum tb_cfg_space space, u32 offset, u32 length)
1035 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
1036 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
1043 /* Thunderbolt error, tb_error holds the actual number */
1044 return tb_cfg_get_error(ctl, space, &res);
1047 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
1048 route, space, offset);
1052 WARN(1, "tb_cfg_write: %d\n", res.err);
1059 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1060 * @ctl: Pointer to the control channel
1061 * @route: Route string of the router
1063 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1064 * returns the port number from which the reply originated.
1066 * Return: Returns the upstream port number on success or an error code on
1069 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1072 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1073 TB_CFG_SWITCH, 0, 1,
1074 TB_CFG_DEFAULT_TIMEOUT);
1079 return res.response_port;