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 u64
get_route(struct tb_cfg_header header
)
59 return (u64
) header
.route_hi
<< 32 | header
.route_lo
;
62 static struct tb_cfg_header
make_header(u64 route
)
64 struct tb_cfg_header header
= {
65 .route_hi
= route
>> 32,
68 /* check for overflow, route_hi is not 32 bits! */
69 WARN_ON(get_route(header
) != route
);
73 static int check_header(struct ctl_pkg
*pkg
, u32 len
, enum tb_cfg_pkg_type type
,
76 struct tb_cfg_header
*header
= pkg
->buffer
;
78 /* check frame, TODO: frame flags */
79 if (WARN(len
!= pkg
->frame
.size
,
80 "wrong framesize (expected %#x, got %#x)\n",
81 len
, pkg
->frame
.size
))
83 if (WARN(type
!= pkg
->frame
.eof
, "wrong eof (expected %#x, got %#x)\n",
84 type
, pkg
->frame
.eof
))
86 if (WARN(pkg
->frame
.sof
, "wrong sof (expected 0x0, got %#x)\n",
91 if (WARN(header
->unknown
!= 1 << 9,
92 "header->unknown is %#x\n", header
->unknown
))
94 if (WARN(route
!= get_route(*header
),
95 "wrong route (expected %llx, got %llx)",
96 route
, get_route(*header
)))
101 static int check_config_address(struct tb_cfg_address addr
,
102 enum tb_cfg_space space
, u32 offset
,
105 if (WARN(addr
.zero
, "addr.zero is %#x\n", addr
.zero
))
107 if (WARN(space
!= addr
.space
, "wrong space (expected %x, got %x\n)",
110 if (WARN(offset
!= addr
.offset
, "wrong offset (expected %x, got %x\n)",
111 offset
, addr
.offset
))
113 if (WARN(length
!= addr
.length
, "wrong space (expected %x, got %x\n)",
114 length
, addr
.length
))
116 if (WARN(addr
.seq
, "addr.seq is %#x\n", addr
.seq
))
119 * We cannot check addr->port as it is set to the upstream port of the
125 static struct tb_cfg_result
decode_error(struct ctl_pkg
*response
)
127 struct cfg_error_pkg
*pkg
= response
->buffer
;
128 struct tb_cfg_result res
= { 0 };
129 res
.response_route
= get_route(pkg
->header
);
130 res
.response_port
= 0;
131 res
.err
= check_header(response
, sizeof(*pkg
), TB_CFG_PKG_ERROR
,
132 get_route(pkg
->header
));
136 WARN(pkg
->zero1
, "pkg->zero1 is %#x\n", pkg
->zero1
);
137 WARN(pkg
->zero2
, "pkg->zero1 is %#x\n", pkg
->zero1
);
138 WARN(pkg
->zero3
, "pkg->zero1 is %#x\n", pkg
->zero1
);
140 res
.tb_error
= pkg
->error
;
141 res
.response_port
= pkg
->port
;
146 static struct tb_cfg_result
parse_header(struct ctl_pkg
*pkg
, u32 len
,
147 enum tb_cfg_pkg_type type
, u64 route
)
149 struct tb_cfg_header
*header
= pkg
->buffer
;
150 struct tb_cfg_result res
= { 0 };
152 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
153 return decode_error(pkg
);
155 res
.response_port
= 0; /* will be updated later for cfg_read/write */
156 res
.response_route
= get_route(*header
);
157 res
.err
= check_header(pkg
, len
, type
, route
);
161 static void tb_cfg_print_error(struct tb_ctl
*ctl
,
162 const struct tb_cfg_result
*res
)
164 WARN_ON(res
->err
!= 1);
165 switch (res
->tb_error
) {
166 case TB_CFG_ERROR_PORT_NOT_CONNECTED
:
167 /* Port is not connected. This can happen during surprise
168 * removal. Do not warn. */
170 case TB_CFG_ERROR_INVALID_CONFIG_SPACE
:
172 * Invalid cfg_space/offset/length combination in
173 * cfg_read/cfg_write.
176 "CFG_ERROR(%llx:%x): Invalid config space or offset\n",
177 res
->response_route
, res
->response_port
);
179 case TB_CFG_ERROR_NO_SUCH_PORT
:
181 * - The route contains a non-existent port.
182 * - The route contains a non-PHY port (e.g. PCIe).
183 * - The port in cfg_read/cfg_write does not exist.
185 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Invalid port\n",
186 res
->response_route
, res
->response_port
);
188 case TB_CFG_ERROR_LOOP
:
189 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Route contains a loop\n",
190 res
->response_route
, res
->response_port
);
193 /* 5,6,7,9 and 11 are also valid error codes */
194 tb_ctl_WARN(ctl
, "CFG_ERROR(%llx:%x): Unknown error\n",
195 res
->response_route
, res
->response_port
);
200 static void cpu_to_be32_array(__be32
*dst
, const u32
*src
, size_t len
)
203 for (i
= 0; i
< len
; i
++)
204 dst
[i
] = cpu_to_be32(src
[i
]);
207 static void be32_to_cpu_array(u32
*dst
, __be32
*src
, size_t len
)
210 for (i
= 0; i
< len
; i
++)
211 dst
[i
] = be32_to_cpu(src
[i
]);
214 static __be32
tb_crc(void *data
, size_t len
)
216 return cpu_to_be32(~__crc32c_le(~0, data
, len
));
219 static void tb_ctl_pkg_free(struct ctl_pkg
*pkg
)
222 dma_pool_free(pkg
->ctl
->frame_pool
,
223 pkg
->buffer
, pkg
->frame
.buffer_phy
);
228 static struct ctl_pkg
*tb_ctl_pkg_alloc(struct tb_ctl
*ctl
)
230 struct ctl_pkg
*pkg
= kzalloc(sizeof(*pkg
), GFP_KERNEL
);
234 pkg
->buffer
= dma_pool_alloc(ctl
->frame_pool
, GFP_KERNEL
,
235 &pkg
->frame
.buffer_phy
);
246 static void tb_ctl_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
249 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
250 tb_ctl_pkg_free(pkg
);
254 * tb_cfg_tx() - transmit a packet on the control channel
256 * len must be a multiple of four.
258 * Return: Returns 0 on success or an error code on failure.
260 static int tb_ctl_tx(struct tb_ctl
*ctl
, const void *data
, size_t len
,
261 enum tb_cfg_pkg_type type
)
265 if (len
% 4 != 0) { /* required for le->be conversion */
266 tb_ctl_WARN(ctl
, "TX: invalid size: %zu\n", len
);
269 if (len
> TB_FRAME_SIZE
- 4) { /* checksum is 4 bytes */
270 tb_ctl_WARN(ctl
, "TX: packet too large: %zu/%d\n",
271 len
, TB_FRAME_SIZE
- 4);
274 pkg
= tb_ctl_pkg_alloc(ctl
);
277 pkg
->frame
.callback
= tb_ctl_tx_callback
;
278 pkg
->frame
.size
= len
+ 4;
279 pkg
->frame
.sof
= type
;
280 pkg
->frame
.eof
= type
;
281 cpu_to_be32_array(pkg
->buffer
, data
, len
/ 4);
282 *(__be32
*) (pkg
->buffer
+ len
) = tb_crc(pkg
->buffer
, len
);
284 res
= ring_tx(ctl
->tx
, &pkg
->frame
);
285 if (res
) /* ring is stopped */
286 tb_ctl_pkg_free(pkg
);
291 * tb_ctl_handle_plug_event() - acknowledge a plug event, invoke ctl->callback
293 static void tb_ctl_handle_plug_event(struct tb_ctl
*ctl
,
294 struct ctl_pkg
*response
)
296 struct cfg_event_pkg
*pkg
= response
->buffer
;
297 u64 route
= get_route(pkg
->header
);
299 if (check_header(response
, sizeof(*pkg
), TB_CFG_PKG_EVENT
, route
)) {
300 tb_ctl_warn(ctl
, "malformed TB_CFG_PKG_EVENT\n");
304 if (tb_cfg_error(ctl
, route
, pkg
->port
, TB_CFG_ERROR_ACK_PLUG_EVENT
))
305 tb_ctl_warn(ctl
, "could not ack plug event on %llx:%x\n",
307 WARN(pkg
->zero
, "pkg->zero is %#x\n", pkg
->zero
);
308 ctl
->callback(ctl
->callback_data
, route
, pkg
->port
, pkg
->unplug
);
311 static void tb_ctl_rx_submit(struct ctl_pkg
*pkg
)
313 ring_rx(pkg
->ctl
->rx
, &pkg
->frame
); /*
314 * We ignore failures during stop.
315 * All rx packets are referenced
316 * from ctl->rx_packets, so we do
321 static void tb_ctl_rx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
324 struct ctl_pkg
*pkg
= container_of(frame
, typeof(*pkg
), frame
);
328 * ring is stopped, packet is referenced from
332 if (frame
->size
< 4 || frame
->size
% 4 != 0) {
333 tb_ctl_err(pkg
->ctl
, "RX: invalid size %#x, dropping packet\n",
338 frame
->size
-= 4; /* remove checksum */
339 if (*(__be32
*) (pkg
->buffer
+ frame
->size
)
340 != tb_crc(pkg
->buffer
, frame
->size
)) {
342 "RX: checksum mismatch, dropping packet\n");
345 be32_to_cpu_array(pkg
->buffer
, pkg
->buffer
, frame
->size
/ 4);
347 if (frame
->eof
== TB_CFG_PKG_EVENT
) {
348 tb_ctl_handle_plug_event(pkg
->ctl
, pkg
);
351 if (!kfifo_put(&pkg
->ctl
->response_fifo
, pkg
)) {
352 tb_ctl_err(pkg
->ctl
, "RX: fifo is full\n");
355 complete(&pkg
->ctl
->response_ready
);
358 tb_ctl_rx_submit(pkg
);
362 * tb_ctl_rx() - receive a packet from the control channel
364 static struct tb_cfg_result
tb_ctl_rx(struct tb_ctl
*ctl
, void *buffer
,
365 size_t length
, int timeout_msec
,
366 u64 route
, enum tb_cfg_pkg_type type
)
368 struct tb_cfg_result res
;
371 if (!wait_for_completion_timeout(&ctl
->response_ready
,
372 msecs_to_jiffies(timeout_msec
))) {
373 tb_ctl_WARN(ctl
, "RX: timeout\n");
374 return (struct tb_cfg_result
) { .err
= -ETIMEDOUT
};
376 if (!kfifo_get(&ctl
->response_fifo
, &pkg
)) {
377 tb_ctl_WARN(ctl
, "empty kfifo\n");
378 return (struct tb_cfg_result
) { .err
= -EIO
};
381 res
= parse_header(pkg
, length
, type
, route
);
383 memcpy(buffer
, pkg
->buffer
, length
);
384 tb_ctl_rx_submit(pkg
);
389 /* public interface, alloc/start/stop/free */
392 * tb_ctl_alloc() - allocate a control channel
394 * cb will be invoked once for every hot plug event.
396 * Return: Returns a pointer on success or NULL on failure.
398 struct tb_ctl
*tb_ctl_alloc(struct tb_nhi
*nhi
, hotplug_cb cb
, void *cb_data
)
401 struct tb_ctl
*ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
406 ctl
->callback_data
= cb_data
;
408 init_completion(&ctl
->response_ready
);
409 INIT_KFIFO(ctl
->response_fifo
);
410 ctl
->frame_pool
= dma_pool_create("thunderbolt_ctl", &nhi
->pdev
->dev
,
411 TB_FRAME_SIZE
, 4, 0);
412 if (!ctl
->frame_pool
)
415 ctl
->tx
= ring_alloc_tx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
);
419 ctl
->rx
= ring_alloc_rx(nhi
, 0, 10, RING_FLAG_NO_SUSPEND
);
423 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++) {
424 ctl
->rx_packets
[i
] = tb_ctl_pkg_alloc(ctl
);
425 if (!ctl
->rx_packets
[i
])
427 ctl
->rx_packets
[i
]->frame
.callback
= tb_ctl_rx_callback
;
430 tb_ctl_info(ctl
, "control channel created\n");
438 * tb_ctl_free() - free a control channel
440 * Must be called after tb_ctl_stop.
442 * Must NOT be called from ctl->callback.
444 void tb_ctl_free(struct tb_ctl
*ctl
)
456 /* free RX packets */
457 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
458 tb_ctl_pkg_free(ctl
->rx_packets
[i
]);
462 dma_pool_destroy(ctl
->frame_pool
);
467 * tb_cfg_start() - start/resume the control channel
469 void tb_ctl_start(struct tb_ctl
*ctl
)
472 tb_ctl_info(ctl
, "control channel starting...\n");
473 ring_start(ctl
->tx
); /* is used to ack hotplug packets, start first */
475 for (i
= 0; i
< TB_CTL_RX_PKG_COUNT
; i
++)
476 tb_ctl_rx_submit(ctl
->rx_packets
[i
]);
480 * control() - pause the control channel
482 * All invocations of ctl->callback will have finished after this method
485 * Must NOT be called from ctl->callback.
487 void tb_ctl_stop(struct tb_ctl
*ctl
)
492 if (!kfifo_is_empty(&ctl
->response_fifo
))
493 tb_ctl_WARN(ctl
, "dangling response in response_fifo\n");
494 kfifo_reset(&ctl
->response_fifo
);
495 tb_ctl_info(ctl
, "control channel stopped\n");
498 /* public interface, commands */
501 * tb_cfg_error() - send error packet
503 * Return: Returns 0 on success or an error code on failure.
505 int tb_cfg_error(struct tb_ctl
*ctl
, u64 route
, u32 port
,
506 enum tb_cfg_error error
)
508 struct cfg_error_pkg pkg
= {
509 .header
= make_header(route
),
513 tb_ctl_info(ctl
, "resetting error on %llx:%x.\n", route
, port
);
514 return tb_ctl_tx(ctl
, &pkg
, sizeof(pkg
), TB_CFG_PKG_ERROR
);
518 * tb_cfg_reset() - send a reset packet and wait for a response
520 * If the switch at route is incorrectly configured then we will not receive a
521 * reply (even though the switch will reset). The caller should check for
522 * -ETIMEDOUT and attempt to reconfigure the switch.
524 struct tb_cfg_result
tb_cfg_reset(struct tb_ctl
*ctl
, u64 route
,
528 struct cfg_reset_pkg request
= { .header
= make_header(route
) };
529 struct tb_cfg_header reply
;
531 err
= tb_ctl_tx(ctl
, &request
, sizeof(request
), TB_CFG_PKG_RESET
);
533 return (struct tb_cfg_result
) { .err
= err
};
535 return tb_ctl_rx(ctl
, &reply
, sizeof(reply
), timeout_msec
, route
,
540 * tb_cfg_read() - read from config space into buffer
542 * Offset and length are in dwords.
544 struct tb_cfg_result
tb_cfg_read_raw(struct tb_ctl
*ctl
, void *buffer
,
545 u64 route
, u32 port
, enum tb_cfg_space space
,
546 u32 offset
, u32 length
, int timeout_msec
)
548 struct tb_cfg_result res
= { 0 };
549 struct cfg_read_pkg request
= {
550 .header
= make_header(route
),
558 struct cfg_write_pkg reply
;
560 res
.err
= tb_ctl_tx(ctl
, &request
, sizeof(request
), TB_CFG_PKG_READ
);
564 res
= tb_ctl_rx(ctl
, &reply
, 12 + 4 * length
, timeout_msec
, route
,
569 res
.response_port
= reply
.addr
.port
;
570 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
572 memcpy(buffer
, &reply
.data
, 4 * length
);
577 * tb_cfg_write() - write from buffer into config space
579 * Offset and length are in dwords.
581 struct tb_cfg_result
tb_cfg_write_raw(struct tb_ctl
*ctl
, const void *buffer
,
582 u64 route
, u32 port
, enum tb_cfg_space space
,
583 u32 offset
, u32 length
, int timeout_msec
)
585 struct tb_cfg_result res
= { 0 };
586 struct cfg_write_pkg request
= {
587 .header
= make_header(route
),
595 struct cfg_read_pkg reply
;
597 memcpy(&request
.data
, buffer
, length
* 4);
599 res
.err
= tb_ctl_tx(ctl
, &request
, 12 + 4 * length
, TB_CFG_PKG_WRITE
);
603 res
= tb_ctl_rx(ctl
, &reply
, sizeof(reply
), timeout_msec
, route
,
608 res
.response_port
= reply
.addr
.port
;
609 res
.err
= check_config_address(reply
.addr
, space
, offset
, length
);
613 int tb_cfg_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
, u32 port
,
614 enum tb_cfg_space space
, u32 offset
, u32 length
)
616 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, buffer
, route
, port
,
617 space
, offset
, length
, TB_CFG_DEFAULT_TIMEOUT
);
619 tb_cfg_print_error(ctl
, &res
);
622 WARN(res
.err
, "tb_cfg_read: %d\n", res
.err
);
626 int tb_cfg_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
, u32 port
,
627 enum tb_cfg_space space
, u32 offset
, u32 length
)
629 struct tb_cfg_result res
= tb_cfg_write_raw(ctl
, buffer
, route
, port
,
630 space
, offset
, length
, TB_CFG_DEFAULT_TIMEOUT
);
632 tb_cfg_print_error(ctl
, &res
);
635 WARN(res
.err
, "tb_cfg_write: %d\n", res
.err
);
640 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
642 * Reads the first dword from the switches TB_CFG_SWITCH config area and
643 * returns the port number from which the reply originated.
645 * Return: Returns the upstream port number on success or an error code on
648 int tb_cfg_get_upstream_port(struct tb_ctl
*ctl
, u64 route
)
651 struct tb_cfg_result res
= tb_cfg_read_raw(ctl
, &dummy
, route
, 0,
653 TB_CFG_DEFAULT_TIMEOUT
);
658 return res
.response_port
;