2 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/crc32.h>
8 #include <linux/slab.h>
10 #include <linux/dmapool.h>
11 #include <linux/workqueue.h>
12 #include <linux/kfifo.h>
20 struct ring_frame frame;
23 #define TB_CTL_RX_PKG_COUNT 10
26 * struct tb_cfg - thunderbolt control channel
33 struct dma_pool *frame_pool;
34 struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
35 DECLARE_KFIFO(response_fifo, struct ctl_pkg*, 16);
36 struct completion response_ready;
43 #define tb_ctl_WARN(ctl, format, arg...) \
44 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
46 #define tb_ctl_err(ctl, format, arg...) \
47 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
49 #define tb_ctl_warn(ctl, format, arg...) \
50 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
52 #define tb_ctl_info(ctl, format, arg...) \
53 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
55 /* utility functions */
57 static struct tb_cfg_header make_header(u64 route)
59 struct tb_cfg_header header = {
60 .route_hi = route >> 32,
63 /* check for overflow, route_hi is not 32 bits! */
64 WARN_ON(tb_cfg_get_route(&header) != route);
68 static int check_header(struct ctl_pkg *pkg, u32 len, enum tb_cfg_pkg_type type,
71 struct tb_cfg_header *header = pkg->buffer;
73 /* check frame, TODO: frame flags */
74 if (WARN(len != pkg->frame.size,
75 "wrong framesize (expected %#x, got %#x)\n",
76 len, pkg->frame.size))
78 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
79 type, pkg->frame.eof))
81 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
86 if (WARN(header->unknown != 1 << 9,
87 "header->unknown is %#x\n", header->unknown))
89 if (WARN(route != tb_cfg_get_route(header),
90 "wrong route (expected %llx, got %llx)",
91 route, tb_cfg_get_route(header)))
96 static int check_config_address(struct tb_cfg_address addr,
97 enum tb_cfg_space space, u32 offset,
100 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
102 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
105 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
106 offset, addr.offset))
108 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
109 length, addr.length))
111 if (WARN(addr.seq, "addr.seq is %#x\n", addr.seq))
114 * We cannot check addr->port as it is set to the upstream port of the
120 static struct tb_cfg_result decode_error(struct ctl_pkg *response)
122 struct cfg_error_pkg *pkg = response->buffer;
123 struct tb_cfg_result res = { 0 };
124 res.response_route = tb_cfg_get_route(&pkg->header);
125 res.response_port = 0;
126 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
127 tb_cfg_get_route(&pkg->header));
131 WARN(pkg->zero1, "pkg->zero1 is %#x\n", pkg->zero1);
132 WARN(pkg->zero2, "pkg->zero1 is %#x\n", pkg->zero1);
133 WARN(pkg->zero3, "pkg->zero1 is %#x\n", pkg->zero1);
135 res.tb_error = pkg->error;
136 res.response_port = pkg->port;
141 static struct tb_cfg_result parse_header(struct ctl_pkg *pkg, u32 len,
142 enum tb_cfg_pkg_type type, u64 route)
144 struct tb_cfg_header *header = pkg->buffer;
145 struct tb_cfg_result res = { 0 };
147 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
148 return decode_error(pkg);
150 res.response_port = 0; /* will be updated later for cfg_read/write */
151 res.response_route = tb_cfg_get_route(header);
152 res.err = check_header(pkg, len, type, route);
156 static void tb_cfg_print_error(struct tb_ctl *ctl,
157 const struct tb_cfg_result *res)
159 WARN_ON(res->err != 1);
160 switch (res->tb_error) {
161 case TB_CFG_ERROR_PORT_NOT_CONNECTED:
162 /* Port is not connected. This can happen during surprise
163 * removal. Do not warn. */
165 case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
167 * Invalid cfg_space/offset/length combination in
168 * cfg_read/cfg_write.
171 "CFG_ERROR(%llx:%x): Invalid config space or offset\n",
172 res->response_route, res->response_port);
174 case TB_CFG_ERROR_NO_SUCH_PORT:
176 * - The route contains a non-existent port.
177 * - The route contains a non-PHY port (e.g. PCIe).
178 * - The port in cfg_read/cfg_write does not exist.
180 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
181 res->response_route, res->response_port);
183 case TB_CFG_ERROR_LOOP:
184 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
185 res->response_route, res->response_port);
188 /* 5,6,7,9 and 11 are also valid error codes */
189 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
190 res->response_route, res->response_port);
195 static void cpu_to_be32_array(__be32 *dst, const u32 *src, size_t len)
198 for (i = 0; i < len; i++)
199 dst[i] = cpu_to_be32(src[i]);
202 static void be32_to_cpu_array(u32 *dst, __be32 *src, size_t len)
205 for (i = 0; i < len; i++)
206 dst[i] = be32_to_cpu(src[i]);
209 static __be32 tb_crc(void *data, size_t len)
211 return cpu_to_be32(~__crc32c_le(~0, data, len));
214 static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
217 dma_pool_free(pkg->ctl->frame_pool,
218 pkg->buffer, pkg->frame.buffer_phy);
223 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
225 struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
229 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
230 &pkg->frame.buffer_phy);
241 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
244 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
245 tb_ctl_pkg_free(pkg);
249 * tb_cfg_tx() - transmit a packet on the control channel
251 * len must be a multiple of four.
253 * Return: Returns 0 on success or an error code on failure.
255 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
256 enum tb_cfg_pkg_type type)
260 if (len % 4 != 0) { /* required for le->be conversion */
261 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
264 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
265 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
266 len, TB_FRAME_SIZE - 4);
269 pkg = tb_ctl_pkg_alloc(ctl);
272 pkg->frame.callback = tb_ctl_tx_callback;
273 pkg->frame.size = len + 4;
274 pkg->frame.sof = type;
275 pkg->frame.eof = type;
276 cpu_to_be32_array(pkg->buffer, data, len / 4);
277 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
279 res = ring_tx(ctl->tx, &pkg->frame);
280 if (res) /* ring is stopped */
281 tb_ctl_pkg_free(pkg);
286 * tb_ctl_handle_plug_event() - acknowledge a plug event, invoke ctl->callback
288 static void tb_ctl_handle_plug_event(struct tb_ctl *ctl,
289 struct ctl_pkg *response)
291 struct cfg_event_pkg *pkg = response->buffer;
292 u64 route = tb_cfg_get_route(&pkg->header);
294 if (check_header(response, sizeof(*pkg), TB_CFG_PKG_EVENT, route)) {
295 tb_ctl_warn(ctl, "malformed TB_CFG_PKG_EVENT\n");
299 if (tb_cfg_error(ctl, route, pkg->port, TB_CFG_ERROR_ACK_PLUG_EVENT))
300 tb_ctl_warn(ctl, "could not ack plug event on %llx:%x\n",
302 WARN(pkg->zero, "pkg->zero is %#x\n", pkg->zero);
303 ctl->callback(ctl->callback_data, route, pkg->port, pkg->unplug);
306 static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
308 ring_rx(pkg->ctl->rx, &pkg->frame); /*
309 * We ignore failures during stop.
310 * All rx packets are referenced
311 * from ctl->rx_packets, so we do
316 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
319 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
323 * ring is stopped, packet is referenced from
327 if (frame->size < 4 || frame->size % 4 != 0) {
328 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
333 frame->size -= 4; /* remove checksum */
334 if (*(__be32 *) (pkg->buffer + frame->size)
335 != tb_crc(pkg->buffer, frame->size)) {
337 "RX: checksum mismatch, dropping packet\n");
340 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
342 if (frame->eof == TB_CFG_PKG_EVENT) {
343 tb_ctl_handle_plug_event(pkg->ctl, pkg);
346 if (!kfifo_put(&pkg->ctl->response_fifo, pkg)) {
347 tb_ctl_err(pkg->ctl, "RX: fifo is full\n");
350 complete(&pkg->ctl->response_ready);
353 tb_ctl_rx_submit(pkg);
357 * tb_ctl_rx() - receive a packet from the control channel
359 static struct tb_cfg_result tb_ctl_rx(struct tb_ctl *ctl, void *buffer,
360 size_t length, int timeout_msec,
361 u64 route, enum tb_cfg_pkg_type type)
363 struct tb_cfg_result res;
366 if (!wait_for_completion_timeout(&ctl->response_ready,
367 msecs_to_jiffies(timeout_msec))) {
368 tb_ctl_WARN(ctl, "RX: timeout\n");
369 return (struct tb_cfg_result) { .err = -ETIMEDOUT };
371 if (!kfifo_get(&ctl->response_fifo, &pkg)) {
372 tb_ctl_WARN(ctl, "empty kfifo\n");
373 return (struct tb_cfg_result) { .err = -EIO };
376 res = parse_header(pkg, length, type, route);
378 memcpy(buffer, pkg->buffer, length);
379 tb_ctl_rx_submit(pkg);
384 /* public interface, alloc/start/stop/free */
387 * tb_ctl_alloc() - allocate a control channel
389 * cb will be invoked once for every hot plug event.
391 * Return: Returns a pointer on success or NULL on failure.
393 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, hotplug_cb cb, void *cb_data)
396 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
401 ctl->callback_data = cb_data;
403 init_completion(&ctl->response_ready);
404 INIT_KFIFO(ctl->response_fifo);
405 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
406 TB_FRAME_SIZE, 4, 0);
407 if (!ctl->frame_pool)
410 ctl->tx = ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
414 ctl->rx = ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
418 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
419 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
420 if (!ctl->rx_packets[i])
422 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
425 tb_ctl_info(ctl, "control channel created\n");
433 * tb_ctl_free() - free a control channel
435 * Must be called after tb_ctl_stop.
437 * Must NOT be called from ctl->callback.
439 void tb_ctl_free(struct tb_ctl *ctl)
451 /* free RX packets */
452 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
453 tb_ctl_pkg_free(ctl->rx_packets[i]);
457 dma_pool_destroy(ctl->frame_pool);
462 * tb_cfg_start() - start/resume the control channel
464 void tb_ctl_start(struct tb_ctl *ctl)
467 tb_ctl_info(ctl, "control channel starting...\n");
468 ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
470 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
471 tb_ctl_rx_submit(ctl->rx_packets[i]);
475 * control() - pause the control channel
477 * All invocations of ctl->callback will have finished after this method
480 * Must NOT be called from ctl->callback.
482 void tb_ctl_stop(struct tb_ctl *ctl)
487 if (!kfifo_is_empty(&ctl->response_fifo))
488 tb_ctl_WARN(ctl, "dangling response in response_fifo\n");
489 kfifo_reset(&ctl->response_fifo);
490 tb_ctl_info(ctl, "control channel stopped\n");
493 /* public interface, commands */
496 * tb_cfg_error() - send error packet
498 * Return: Returns 0 on success or an error code on failure.
500 int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port,
501 enum tb_cfg_error error)
503 struct cfg_error_pkg pkg = {
504 .header = make_header(route),
508 tb_ctl_info(ctl, "resetting error on %llx:%x.\n", route, port);
509 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
513 * tb_cfg_reset() - send a reset packet and wait for a response
515 * If the switch at route is incorrectly configured then we will not receive a
516 * reply (even though the switch will reset). The caller should check for
517 * -ETIMEDOUT and attempt to reconfigure the switch.
519 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
523 struct cfg_reset_pkg request = { .header = make_header(route) };
524 struct tb_cfg_header reply;
526 err = tb_ctl_tx(ctl, &request, sizeof(request), TB_CFG_PKG_RESET);
528 return (struct tb_cfg_result) { .err = err };
530 return tb_ctl_rx(ctl, &reply, sizeof(reply), timeout_msec, route,
535 * tb_cfg_read() - read from config space into buffer
537 * Offset and length are in dwords.
539 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
540 u64 route, u32 port, enum tb_cfg_space space,
541 u32 offset, u32 length, int timeout_msec)
543 struct tb_cfg_result res = { 0 };
544 struct cfg_read_pkg request = {
545 .header = make_header(route),
553 struct cfg_write_pkg reply;
555 res.err = tb_ctl_tx(ctl, &request, sizeof(request), TB_CFG_PKG_READ);
559 res = tb_ctl_rx(ctl, &reply, 12 + 4 * length, timeout_msec, route,
564 res.response_port = reply.addr.port;
565 res.err = check_config_address(reply.addr, space, offset, length);
567 memcpy(buffer, &reply.data, 4 * length);
572 * tb_cfg_write() - write from buffer into config space
574 * Offset and length are in dwords.
576 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
577 u64 route, u32 port, enum tb_cfg_space space,
578 u32 offset, u32 length, int timeout_msec)
580 struct tb_cfg_result res = { 0 };
581 struct cfg_write_pkg request = {
582 .header = make_header(route),
590 struct cfg_read_pkg reply;
592 memcpy(&request.data, buffer, length * 4);
594 res.err = tb_ctl_tx(ctl, &request, 12 + 4 * length, TB_CFG_PKG_WRITE);
598 res = tb_ctl_rx(ctl, &reply, sizeof(reply), timeout_msec, route,
603 res.response_port = reply.addr.port;
604 res.err = check_config_address(reply.addr, space, offset, length);
608 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
609 enum tb_cfg_space space, u32 offset, u32 length)
611 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
612 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
614 tb_cfg_print_error(ctl, &res);
617 WARN(res.err, "tb_cfg_read: %d\n", res.err);
621 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
622 enum tb_cfg_space space, u32 offset, u32 length)
624 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
625 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
627 tb_cfg_print_error(ctl, &res);
630 WARN(res.err, "tb_cfg_write: %d\n", res.err);
635 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
637 * Reads the first dword from the switches TB_CFG_SWITCH config area and
638 * returns the port number from which the reply originated.
640 * Return: Returns the upstream port number on success or an error code on
643 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
646 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
648 TB_CFG_DEFAULT_TIMEOUT);
653 return res.response_port;