blob: 2364efa2399121f49f47781df00445b77067d490 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02002/*
Mika Westerberg15c67842018-10-01 12:31:22 +03003 * Thunderbolt driver - control channel and configuration commands
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02004 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
Mika Westerberg15c67842018-10-01 12:31:22 +03006 * Copyright (C) 2018, Intel Corporation
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02007 */
8
9#include <linux/crc32.h>
Mika Westerbergd7f781b2017-06-06 15:25:10 +030010#include <linux/delay.h>
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020011#include <linux/slab.h>
12#include <linux/pci.h>
13#include <linux/dmapool.h>
14#include <linux/workqueue.h>
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020015
16#include "ctl.h"
17
18
Mika Westerbergd7f781b2017-06-06 15:25:10 +030019#define TB_CTL_RX_PKG_COUNT 10
20#define TB_CTL_RETRIES 4
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020021
22/**
23 * struct tb_cfg - thunderbolt control channel
24 */
25struct tb_ctl {
26 struct tb_nhi *nhi;
27 struct tb_ring *tx;
28 struct tb_ring *rx;
29
30 struct dma_pool *frame_pool;
31 struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
Mika Westerbergd7f781b2017-06-06 15:25:10 +030032 struct mutex request_queue_lock;
33 struct list_head request_queue;
34 bool running;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020035
Mika Westerberg81a54b52017-06-06 15:25:09 +030036 event_cb callback;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020037 void *callback_data;
38};
39
40
41#define tb_ctl_WARN(ctl, format, arg...) \
42 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
43
44#define tb_ctl_err(ctl, format, arg...) \
45 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
46
47#define tb_ctl_warn(ctl, format, arg...) \
48 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
49
50#define tb_ctl_info(ctl, format, arg...) \
51 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
52
Mika Westerberg81a54b52017-06-06 15:25:09 +030053#define tb_ctl_dbg(ctl, format, arg...) \
54 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
55
Mika Westerbergd7f781b2017-06-06 15:25:10 +030056static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
57/* Serializes access to request kref_get/put */
58static DEFINE_MUTEX(tb_cfg_request_lock);
59
60/**
61 * tb_cfg_request_alloc() - Allocates a new config request
62 *
63 * This is refcounted object so when you are done with this, call
64 * tb_cfg_request_put() to it.
65 */
66struct tb_cfg_request *tb_cfg_request_alloc(void)
67{
68 struct tb_cfg_request *req;
69
70 req = kzalloc(sizeof(*req), GFP_KERNEL);
71 if (!req)
72 return NULL;
73
74 kref_init(&req->kref);
75
76 return req;
77}
78
79/**
80 * tb_cfg_request_get() - Increase refcount of a request
81 * @req: Request whose refcount is increased
82 */
83void tb_cfg_request_get(struct tb_cfg_request *req)
84{
85 mutex_lock(&tb_cfg_request_lock);
86 kref_get(&req->kref);
87 mutex_unlock(&tb_cfg_request_lock);
88}
89
90static void tb_cfg_request_destroy(struct kref *kref)
91{
92 struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
93
94 kfree(req);
95}
96
97/**
98 * tb_cfg_request_put() - Decrease refcount and possibly release the request
99 * @req: Request whose refcount is decreased
100 *
101 * Call this function when you are done with the request. When refcount
102 * goes to %0 the object is released.
103 */
104void tb_cfg_request_put(struct tb_cfg_request *req)
105{
106 mutex_lock(&tb_cfg_request_lock);
107 kref_put(&req->kref, tb_cfg_request_destroy);
108 mutex_unlock(&tb_cfg_request_lock);
109}
110
111static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
112 struct tb_cfg_request *req)
113{
114 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
115 WARN_ON(req->ctl);
116
117 mutex_lock(&ctl->request_queue_lock);
118 if (!ctl->running) {
119 mutex_unlock(&ctl->request_queue_lock);
120 return -ENOTCONN;
121 }
122 req->ctl = ctl;
123 list_add_tail(&req->list, &ctl->request_queue);
124 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
125 mutex_unlock(&ctl->request_queue_lock);
126 return 0;
127}
128
129static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
130{
131 struct tb_ctl *ctl = req->ctl;
132
133 mutex_lock(&ctl->request_queue_lock);
134 list_del(&req->list);
135 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
136 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
137 wake_up(&tb_cfg_request_cancel_queue);
138 mutex_unlock(&ctl->request_queue_lock);
139}
140
141static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
142{
143 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
144}
145
146static struct tb_cfg_request *
147tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
148{
149 struct tb_cfg_request *req;
150 bool found = false;
151
152 mutex_lock(&pkg->ctl->request_queue_lock);
153 list_for_each_entry(req, &pkg->ctl->request_queue, list) {
154 tb_cfg_request_get(req);
155 if (req->match(req, pkg)) {
156 found = true;
157 break;
158 }
159 tb_cfg_request_put(req);
160 }
161 mutex_unlock(&pkg->ctl->request_queue_lock);
162
163 return found ? req : NULL;
164}
165
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200166/* utility functions */
167
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300168
169static int check_header(const struct ctl_pkg *pkg, u32 len,
170 enum tb_cfg_pkg_type type, u64 route)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200171{
172 struct tb_cfg_header *header = pkg->buffer;
173
174 /* check frame, TODO: frame flags */
175 if (WARN(len != pkg->frame.size,
176 "wrong framesize (expected %#x, got %#x)\n",
177 len, pkg->frame.size))
178 return -EIO;
179 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
180 type, pkg->frame.eof))
181 return -EIO;
182 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
183 pkg->frame.sof))
184 return -EIO;
185
186 /* check header */
187 if (WARN(header->unknown != 1 << 9,
188 "header->unknown is %#x\n", header->unknown))
189 return -EIO;
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300190 if (WARN(route != tb_cfg_get_route(header),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200191 "wrong route (expected %llx, got %llx)",
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300192 route, tb_cfg_get_route(header)))
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200193 return -EIO;
194 return 0;
195}
196
197static int check_config_address(struct tb_cfg_address addr,
198 enum tb_cfg_space space, u32 offset,
199 u32 length)
200{
201 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
202 return -EIO;
203 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
204 space, addr.space))
205 return -EIO;
206 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
207 offset, addr.offset))
208 return -EIO;
209 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
210 length, addr.length))
211 return -EIO;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200212 /*
213 * We cannot check addr->port as it is set to the upstream port of the
214 * sender.
215 */
216 return 0;
217}
218
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300219static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200220{
221 struct cfg_error_pkg *pkg = response->buffer;
222 struct tb_cfg_result res = { 0 };
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300223 res.response_route = tb_cfg_get_route(&pkg->header);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200224 res.response_port = 0;
225 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300226 tb_cfg_get_route(&pkg->header));
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200227 if (res.err)
228 return res;
229
230 WARN(pkg->zero1, "pkg->zero1 is %#x\n", pkg->zero1);
231 WARN(pkg->zero2, "pkg->zero1 is %#x\n", pkg->zero1);
232 WARN(pkg->zero3, "pkg->zero1 is %#x\n", pkg->zero1);
233 res.err = 1;
234 res.tb_error = pkg->error;
235 res.response_port = pkg->port;
236 return res;
237
238}
239
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300240static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200241 enum tb_cfg_pkg_type type, u64 route)
242{
243 struct tb_cfg_header *header = pkg->buffer;
244 struct tb_cfg_result res = { 0 };
245
246 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
247 return decode_error(pkg);
248
249 res.response_port = 0; /* will be updated later for cfg_read/write */
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300250 res.response_route = tb_cfg_get_route(header);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200251 res.err = check_header(pkg, len, type, route);
252 return res;
253}
254
255static void tb_cfg_print_error(struct tb_ctl *ctl,
256 const struct tb_cfg_result *res)
257{
258 WARN_ON(res->err != 1);
259 switch (res->tb_error) {
260 case TB_CFG_ERROR_PORT_NOT_CONNECTED:
261 /* Port is not connected. This can happen during surprise
262 * removal. Do not warn. */
263 return;
264 case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
265 /*
266 * Invalid cfg_space/offset/length combination in
267 * cfg_read/cfg_write.
268 */
Mika Westerbergfa1653d2020-06-29 20:23:58 +0300269 tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
270 res->response_route, res->response_port);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200271 return;
272 case TB_CFG_ERROR_NO_SUCH_PORT:
273 /*
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.
277 */
278 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
279 res->response_route, res->response_port);
280 return;
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);
284 return;
285 default:
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);
289 return;
290 }
291}
292
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300293static __be32 tb_crc(const void *data, size_t len)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200294{
295 return cpu_to_be32(~__crc32c_le(~0, data, len));
296}
297
298static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
299{
300 if (pkg) {
301 dma_pool_free(pkg->ctl->frame_pool,
302 pkg->buffer, pkg->frame.buffer_phy);
303 kfree(pkg);
304 }
305}
306
307static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
308{
309 struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
310 if (!pkg)
Sachin Kamat8db353b2014-06-20 14:32:32 +0530311 return NULL;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200312 pkg->ctl = ctl;
313 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
314 &pkg->frame.buffer_phy);
315 if (!pkg->buffer) {
316 kfree(pkg);
Sachin Kamat8db353b2014-06-20 14:32:32 +0530317 return NULL;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200318 }
319 return pkg;
320}
321
322
323/* RX/TX handling */
324
325static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
326 bool canceled)
327{
328 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
329 tb_ctl_pkg_free(pkg);
330}
331
332/**
333 * tb_cfg_tx() - transmit a packet on the control channel
334 *
335 * len must be a multiple of four.
336 *
337 * Return: Returns 0 on success or an error code on failure.
338 */
Mika Westerberg16a12582017-06-06 15:24:53 +0300339static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200340 enum tb_cfg_pkg_type type)
341{
342 int res;
343 struct ctl_pkg *pkg;
344 if (len % 4 != 0) { /* required for le->be conversion */
345 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
346 return -EINVAL;
347 }
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);
351 return -EINVAL;
352 }
353 pkg = tb_ctl_pkg_alloc(ctl);
354 if (!pkg)
355 return -ENOMEM;
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);
Andreas Noever801dba52014-06-20 21:42:22 +0200361 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200362
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300363 res = tb_ring_tx(ctl->tx, &pkg->frame);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200364 if (res) /* ring is stopped */
365 tb_ctl_pkg_free(pkg);
366 return res;
367}
368
369/**
Mika Westerberg81a54b52017-06-06 15:25:09 +0300370 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200371 */
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300372static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
Mika Westerberg81a54b52017-06-06 15:25:09 +0300373 struct ctl_pkg *pkg, size_t size)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200374{
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300375 return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200376}
377
378static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
379{
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300380 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200381 * We ignore failures during stop.
382 * All rx packets are referenced
383 * from ctl->rx_packets, so we do
384 * not loose them.
385 */
386}
387
Mika Westerberg81a54b52017-06-06 15:25:09 +0300388static int tb_async_error(const struct ctl_pkg *pkg)
389{
390 const struct cfg_error_pkg *error = (const struct cfg_error_pkg *)pkg;
391
392 if (pkg->frame.eof != TB_CFG_PKG_ERROR)
393 return false;
394
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:
399 return true;
400
401 default:
402 return false;
403 }
404}
405
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200406static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
407 bool canceled)
408{
409 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300410 struct tb_cfg_request *req;
Mika Westerberg81a54b52017-06-06 15:25:09 +0300411 __be32 crc32;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200412
413 if (canceled)
414 return; /*
415 * ring is stopped, packet is referenced from
416 * ctl->rx_packets.
417 */
418
419 if (frame->size < 4 || frame->size % 4 != 0) {
420 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
421 frame->size);
422 goto rx;
423 }
424
425 frame->size -= 4; /* remove checksum */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300426 crc32 = tb_crc(pkg->buffer, frame->size);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200427 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
428
Mika Westerberg81a54b52017-06-06 15:25:09 +0300429 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) {
436 tb_ctl_err(pkg->ctl,
437 "RX: checksum mismatch, dropping packet\n");
438 goto rx;
439 }
440 if (tb_async_error(pkg)) {
441 tb_ctl_handle_event(pkg->ctl, frame->eof,
442 pkg, frame->size);
443 goto rx;
444 }
445 break;
446
447 case TB_CFG_PKG_EVENT:
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300448 case TB_CFG_PKG_XDOMAIN_RESP:
449 case TB_CFG_PKG_XDOMAIN_REQ:
Mika Westerberg81a54b52017-06-06 15:25:09 +0300450 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
451 tb_ctl_err(pkg->ctl,
452 "RX: checksum mismatch, dropping packet\n");
453 goto rx;
454 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500455 fallthrough;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300456 case TB_CFG_PKG_ICM_EVENT:
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300457 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
458 goto rx;
459 break;
Mika Westerberg81a54b52017-06-06 15:25:09 +0300460
461 default:
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300462 break;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200463 }
Mika Westerberg81a54b52017-06-06 15:25:09 +0300464
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300465 /*
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.
470 */
471 req = tb_cfg_request_find(pkg->ctl, pkg);
472 if (req) {
473 if (req->copy(req, pkg))
474 schedule_work(&req->work);
475 tb_cfg_request_put(req);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200476 }
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300477
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200478rx:
479 tb_ctl_rx_submit(pkg);
480}
481
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300482static void tb_cfg_request_work(struct work_struct *work)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200483{
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300484 struct tb_cfg_request *req = container_of(work, typeof(*req), work);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200485
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300486 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
487 req->callback(req->callback_data);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200488
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300489 tb_cfg_request_dequeue(req);
490 tb_cfg_request_put(req);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200491}
492
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300493/**
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
499 *
500 * This queues @req on the given control channel without waiting for it
501 * to complete. When the request completes @callback is called.
502 */
503int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
504 void (*callback)(void *), void *callback_data)
505{
506 int ret;
507
508 req->flags = 0;
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);
513
514 tb_cfg_request_get(req);
515 ret = tb_cfg_request_enqueue(ctl, req);
516 if (ret)
517 goto err_put;
518
519 ret = tb_ctl_tx(ctl, req->request, req->request_size,
520 req->request_type);
521 if (ret)
522 goto err_dequeue;
523
524 if (!req->response)
525 schedule_work(&req->work);
526
527 return 0;
528
529err_dequeue:
530 tb_cfg_request_dequeue(req);
531err_put:
532 tb_cfg_request_put(req);
533
534 return ret;
535}
536
537/**
538 * tb_cfg_request_cancel() - Cancel a control request
539 * @req: Request to cancel
540 * @err: Error to assign to the request
541 *
542 * This function can be used to cancel ongoing request. It will wait
543 * until the request is not active anymore.
544 */
545void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
546{
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;
551}
552
553static void tb_cfg_request_complete(void *data)
554{
555 complete(data);
556}
557
558/**
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
563 *
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
567 * at a time.
568 */
569struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
570 struct tb_cfg_request *req,
571 int timeout_msec)
572{
573 unsigned long timeout = msecs_to_jiffies(timeout_msec);
574 struct tb_cfg_result res = { 0 };
575 DECLARE_COMPLETION_ONSTACK(done);
576 int ret;
577
578 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
579 if (ret) {
580 res.err = ret;
581 return res;
582 }
583
584 if (!wait_for_completion_timeout(&done, timeout))
585 tb_cfg_request_cancel(req, -ETIMEDOUT);
586
587 flush_work(&req->work);
588
589 return req->result;
590}
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200591
592/* public interface, alloc/start/stop/free */
593
594/**
595 * tb_ctl_alloc() - allocate a control channel
596 *
597 * cb will be invoked once for every hot plug event.
598 *
599 * Return: Returns a pointer on success or NULL on failure.
600 */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300601struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200602{
603 int i;
604 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
605 if (!ctl)
606 return NULL;
607 ctl->nhi = nhi;
608 ctl->callback = cb;
609 ctl->callback_data = cb_data;
610
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300611 mutex_init(&ctl->request_queue_lock);
612 INIT_LIST_HEAD(&ctl->request_queue);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200613 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
614 TB_FRAME_SIZE, 4, 0);
615 if (!ctl->frame_pool)
616 goto err;
617
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300618 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200619 if (!ctl->tx)
620 goto err;
621
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300622 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0xffff,
Mika Westerberg4ffe7222017-10-02 13:38:41 +0300623 0xffff, NULL, NULL);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200624 if (!ctl->rx)
625 goto err;
626
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])
630 goto err;
631 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
632 }
633
Mika Westerbergdaa51402018-10-01 12:31:19 +0300634 tb_ctl_dbg(ctl, "control channel created\n");
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200635 return ctl;
636err:
637 tb_ctl_free(ctl);
638 return NULL;
639}
640
641/**
642 * tb_ctl_free() - free a control channel
643 *
644 * Must be called after tb_ctl_stop.
645 *
646 * Must NOT be called from ctl->callback.
647 */
648void tb_ctl_free(struct tb_ctl *ctl)
649{
650 int i;
Mika Westerbergc9843eb2017-06-06 15:24:59 +0300651
652 if (!ctl)
653 return;
654
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200655 if (ctl->rx)
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300656 tb_ring_free(ctl->rx);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200657 if (ctl->tx)
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300658 tb_ring_free(ctl->tx);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200659
660 /* free RX packets */
661 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
662 tb_ctl_pkg_free(ctl->rx_packets[i]);
663
664
zhong jiang0bb5a1a2018-10-01 12:31:18 +0300665 dma_pool_destroy(ctl->frame_pool);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200666 kfree(ctl);
667}
668
669/**
670 * tb_cfg_start() - start/resume the control channel
671 */
672void tb_ctl_start(struct tb_ctl *ctl)
673{
674 int i;
Mika Westerbergdaa51402018-10-01 12:31:19 +0300675 tb_ctl_dbg(ctl, "control channel starting...\n");
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300676 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
677 tb_ring_start(ctl->rx);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200678 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
679 tb_ctl_rx_submit(ctl->rx_packets[i]);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300680
681 ctl->running = true;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200682}
683
684/**
685 * control() - pause the control channel
686 *
687 * All invocations of ctl->callback will have finished after this method
688 * returns.
689 *
690 * Must NOT be called from ctl->callback.
691 */
692void tb_ctl_stop(struct tb_ctl *ctl)
693{
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300694 mutex_lock(&ctl->request_queue_lock);
695 ctl->running = false;
696 mutex_unlock(&ctl->request_queue_lock);
697
Mika Westerberg3b3d9f42017-10-02 13:38:37 +0300698 tb_ring_stop(ctl->rx);
699 tb_ring_stop(ctl->tx);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200700
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300701 if (!list_empty(&ctl->request_queue))
702 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
703 INIT_LIST_HEAD(&ctl->request_queue);
Mika Westerbergdaa51402018-10-01 12:31:19 +0300704 tb_ctl_dbg(ctl, "control channel stopped\n");
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200705}
706
707/* public interface, commands */
708
709/**
Mika Westerberg210e9f52019-12-17 15:33:39 +0300710 * tb_cfg_ack_plug() - Ack hot plug/unplug event
711 * @ctl: Control channel to use
712 * @route: Router that originated the event
713 * @port: Port where the hot plug/unplug happened
714 * @unplug: Ack hot plug or unplug
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200715 *
Mika Westerberg210e9f52019-12-17 15:33:39 +0300716 * Call this as response for hot plug/unplug event to ack it.
717 * Returns %0 on success or an error code on failure.
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200718 */
Mika Westerberg210e9f52019-12-17 15:33:39 +0300719int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200720{
721 struct cfg_error_pkg pkg = {
Mika Westerberg05c242e2017-06-06 15:25:08 +0300722 .header = tb_cfg_make_header(route),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200723 .port = port,
Mika Westerberg210e9f52019-12-17 15:33:39 +0300724 .error = TB_CFG_ERROR_ACK_PLUG_EVENT,
725 .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
726 : TB_CFG_ERROR_PG_HOT_PLUG,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200727 };
Mika Westerberg210e9f52019-12-17 15:33:39 +0300728 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n",
729 unplug ? "un" : "", route, port);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200730 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
731}
732
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300733static bool tb_cfg_match(const struct tb_cfg_request *req,
734 const struct ctl_pkg *pkg)
735{
736 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
737
738 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
739 return true;
740
741 if (pkg->frame.eof != req->response_type)
742 return false;
743 if (route != tb_cfg_get_route(req->request))
744 return false;
745 if (pkg->frame.size != req->response_size)
746 return false;
747
748 if (pkg->frame.eof == TB_CFG_PKG_READ ||
749 pkg->frame.eof == TB_CFG_PKG_WRITE) {
750 const struct cfg_read_pkg *req_hdr = req->request;
751 const struct cfg_read_pkg *res_hdr = pkg->buffer;
752
753 if (req_hdr->addr.seq != res_hdr->addr.seq)
754 return false;
755 }
756
757 return true;
758}
759
760static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
761{
762 struct tb_cfg_result res;
763
764 /* Now make sure it is in expected format */
765 res = parse_header(pkg, req->response_size, req->response_type,
766 tb_cfg_get_route(req->request));
767 if (!res.err)
768 memcpy(req->response, pkg->buffer, req->response_size);
769
770 req->result = res;
771
772 /* Always complete when first response is received */
773 return true;
774}
775
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200776/**
777 * tb_cfg_reset() - send a reset packet and wait for a response
778 *
779 * If the switch at route is incorrectly configured then we will not receive a
780 * reply (even though the switch will reset). The caller should check for
781 * -ETIMEDOUT and attempt to reconfigure the switch.
782 */
783struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
784 int timeout_msec)
785{
Mika Westerberg05c242e2017-06-06 15:25:08 +0300786 struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300787 struct tb_cfg_result res = { 0 };
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200788 struct tb_cfg_header reply;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300789 struct tb_cfg_request *req;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200790
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300791 req = tb_cfg_request_alloc();
792 if (!req) {
793 res.err = -ENOMEM;
794 return res;
795 }
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200796
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300797 req->match = tb_cfg_match;
798 req->copy = tb_cfg_copy;
799 req->request = &request;
800 req->request_size = sizeof(request);
801 req->request_type = TB_CFG_PKG_RESET;
802 req->response = &reply;
803 req->response_size = sizeof(reply);
Dan Carpenter02729d12017-08-16 11:54:17 +0300804 req->response_type = TB_CFG_PKG_RESET;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300805
806 res = tb_cfg_request_sync(ctl, req, timeout_msec);
807
808 tb_cfg_request_put(req);
809
810 return res;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200811}
812
813/**
814 * tb_cfg_read() - read from config space into buffer
815 *
816 * Offset and length are in dwords.
817 */
818struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
819 u64 route, u32 port, enum tb_cfg_space space,
820 u32 offset, u32 length, int timeout_msec)
821{
822 struct tb_cfg_result res = { 0 };
823 struct cfg_read_pkg request = {
Mika Westerberg05c242e2017-06-06 15:25:08 +0300824 .header = tb_cfg_make_header(route),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200825 .addr = {
826 .port = port,
827 .space = space,
828 .offset = offset,
829 .length = length,
830 },
831 };
832 struct cfg_write_pkg reply;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300833 int retries = 0;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200834
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300835 while (retries < TB_CTL_RETRIES) {
836 struct tb_cfg_request *req;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200837
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300838 req = tb_cfg_request_alloc();
839 if (!req) {
840 res.err = -ENOMEM;
841 return res;
842 }
843
844 request.addr.seq = retries++;
845
846 req->match = tb_cfg_match;
847 req->copy = tb_cfg_copy;
848 req->request = &request;
849 req->request_size = sizeof(request);
850 req->request_type = TB_CFG_PKG_READ;
851 req->response = &reply;
852 req->response_size = 12 + 4 * length;
853 req->response_type = TB_CFG_PKG_READ;
854
855 res = tb_cfg_request_sync(ctl, req, timeout_msec);
856
857 tb_cfg_request_put(req);
858
859 if (res.err != -ETIMEDOUT)
860 break;
861
862 /* Wait a bit (arbitrary time) until we send a retry */
863 usleep_range(10, 100);
864 }
865
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200866 if (res.err)
867 return res;
868
869 res.response_port = reply.addr.port;
870 res.err = check_config_address(reply.addr, space, offset, length);
871 if (!res.err)
872 memcpy(buffer, &reply.data, 4 * length);
873 return res;
874}
875
876/**
877 * tb_cfg_write() - write from buffer into config space
878 *
879 * Offset and length are in dwords.
880 */
Mika Westerberg16a12582017-06-06 15:24:53 +0300881struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200882 u64 route, u32 port, enum tb_cfg_space space,
883 u32 offset, u32 length, int timeout_msec)
884{
885 struct tb_cfg_result res = { 0 };
886 struct cfg_write_pkg request = {
Mika Westerberg05c242e2017-06-06 15:25:08 +0300887 .header = tb_cfg_make_header(route),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200888 .addr = {
889 .port = port,
890 .space = space,
891 .offset = offset,
892 .length = length,
893 },
894 };
895 struct cfg_read_pkg reply;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300896 int retries = 0;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200897
898 memcpy(&request.data, buffer, length * 4);
899
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300900 while (retries < TB_CTL_RETRIES) {
901 struct tb_cfg_request *req;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200902
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300903 req = tb_cfg_request_alloc();
904 if (!req) {
905 res.err = -ENOMEM;
906 return res;
907 }
908
909 request.addr.seq = retries++;
910
911 req->match = tb_cfg_match;
912 req->copy = tb_cfg_copy;
913 req->request = &request;
914 req->request_size = 12 + 4 * length;
915 req->request_type = TB_CFG_PKG_WRITE;
916 req->response = &reply;
917 req->response_size = sizeof(reply);
918 req->response_type = TB_CFG_PKG_WRITE;
919
920 res = tb_cfg_request_sync(ctl, req, timeout_msec);
921
922 tb_cfg_request_put(req);
923
924 if (res.err != -ETIMEDOUT)
925 break;
926
927 /* Wait a bit (arbitrary time) until we send a retry */
928 usleep_range(10, 100);
929 }
930
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200931 if (res.err)
932 return res;
933
934 res.response_port = reply.addr.port;
935 res.err = check_config_address(reply.addr, space, offset, length);
936 return res;
937}
938
Mika Westerbergd94dcbb2018-07-04 08:50:01 +0300939static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
940 const struct tb_cfg_result *res)
941{
942 /*
943 * For unimplemented ports access to port config space may return
944 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
945 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
946 * that the caller can mark the port as disabled.
947 */
948 if (space == TB_CFG_PORT &&
949 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
950 return -ENODEV;
951
952 tb_cfg_print_error(ctl, res);
953 return -EIO;
954}
955
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200956int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
957 enum tb_cfg_space space, u32 offset, u32 length)
958{
959 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
960 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300961 switch (res.err) {
962 case 0:
963 /* Success */
964 break;
965
966 case 1:
967 /* Thunderbolt error, tb_error holds the actual number */
Mika Westerbergd94dcbb2018-07-04 08:50:01 +0300968 return tb_cfg_get_error(ctl, space, &res);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300969
970 case -ETIMEDOUT:
Mika Westerberg68b91292019-03-22 14:28:11 +0200971 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
972 route, space, offset);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300973 break;
974
975 default:
976 WARN(1, "tb_cfg_read: %d\n", res.err);
977 break;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200978 }
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200979 return res.err;
980}
981
Mika Westerberg16a12582017-06-06 15:24:53 +0300982int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200983 enum tb_cfg_space space, u32 offset, u32 length)
984{
985 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
986 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300987 switch (res.err) {
988 case 0:
989 /* Success */
990 break;
991
992 case 1:
993 /* Thunderbolt error, tb_error holds the actual number */
Mika Westerbergd94dcbb2018-07-04 08:50:01 +0300994 return tb_cfg_get_error(ctl, space, &res);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300995
996 case -ETIMEDOUT:
Mika Westerberg68b91292019-03-22 14:28:11 +0200997 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
998 route, space, offset);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300999 break;
1000
1001 default:
1002 WARN(1, "tb_cfg_write: %d\n", res.err);
1003 break;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02001004 }
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02001005 return res.err;
1006}
1007
1008/**
1009 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1010 *
1011 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1012 * returns the port number from which the reply originated.
1013 *
1014 * Return: Returns the upstream port number on success or an error code on
1015 * failure.
1016 */
1017int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1018{
1019 u32 dummy;
1020 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1021 TB_CFG_SWITCH, 0, 1,
1022 TB_CFG_DEFAULT_TIMEOUT);
1023 if (res.err == 1)
1024 return -EIO;
1025 if (res.err)
1026 return res.err;
1027 return res.response_port;
1028}