blob: 69c0232a22f833d0d2d23e4416deff15c8a8e8e3 [file] [log] [blame]
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02001/*
2 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
3 *
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5 */
6
7#include <linux/crc32.h>
Mika Westerbergd7f781b2017-06-06 15:25:10 +03008#include <linux/delay.h>
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02009#include <linux/slab.h>
10#include <linux/pci.h>
11#include <linux/dmapool.h>
12#include <linux/workqueue.h>
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020013
14#include "ctl.h"
15
16
Mika Westerbergd7f781b2017-06-06 15:25:10 +030017#define TB_CTL_RX_PKG_COUNT 10
18#define TB_CTL_RETRIES 4
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020019
20/**
21 * struct tb_cfg - thunderbolt control channel
22 */
23struct tb_ctl {
24 struct tb_nhi *nhi;
25 struct tb_ring *tx;
26 struct tb_ring *rx;
27
28 struct dma_pool *frame_pool;
29 struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
Mika Westerbergd7f781b2017-06-06 15:25:10 +030030 struct mutex request_queue_lock;
31 struct list_head request_queue;
32 bool running;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020033
Mika Westerberg81a54b52017-06-06 15:25:09 +030034 event_cb callback;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020035 void *callback_data;
36};
37
38
39#define tb_ctl_WARN(ctl, format, arg...) \
40 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
41
42#define tb_ctl_err(ctl, format, arg...) \
43 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
44
45#define tb_ctl_warn(ctl, format, arg...) \
46 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
47
48#define tb_ctl_info(ctl, format, arg...) \
49 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
50
Mika Westerberg81a54b52017-06-06 15:25:09 +030051#define tb_ctl_dbg(ctl, format, arg...) \
52 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
53
Mika Westerbergd7f781b2017-06-06 15:25:10 +030054static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
55/* Serializes access to request kref_get/put */
56static DEFINE_MUTEX(tb_cfg_request_lock);
57
58/**
59 * tb_cfg_request_alloc() - Allocates a new config request
60 *
61 * This is refcounted object so when you are done with this, call
62 * tb_cfg_request_put() to it.
63 */
64struct tb_cfg_request *tb_cfg_request_alloc(void)
65{
66 struct tb_cfg_request *req;
67
68 req = kzalloc(sizeof(*req), GFP_KERNEL);
69 if (!req)
70 return NULL;
71
72 kref_init(&req->kref);
73
74 return req;
75}
76
77/**
78 * tb_cfg_request_get() - Increase refcount of a request
79 * @req: Request whose refcount is increased
80 */
81void tb_cfg_request_get(struct tb_cfg_request *req)
82{
83 mutex_lock(&tb_cfg_request_lock);
84 kref_get(&req->kref);
85 mutex_unlock(&tb_cfg_request_lock);
86}
87
88static void tb_cfg_request_destroy(struct kref *kref)
89{
90 struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
91
92 kfree(req);
93}
94
95/**
96 * tb_cfg_request_put() - Decrease refcount and possibly release the request
97 * @req: Request whose refcount is decreased
98 *
99 * Call this function when you are done with the request. When refcount
100 * goes to %0 the object is released.
101 */
102void tb_cfg_request_put(struct tb_cfg_request *req)
103{
104 mutex_lock(&tb_cfg_request_lock);
105 kref_put(&req->kref, tb_cfg_request_destroy);
106 mutex_unlock(&tb_cfg_request_lock);
107}
108
109static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
110 struct tb_cfg_request *req)
111{
112 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
113 WARN_ON(req->ctl);
114
115 mutex_lock(&ctl->request_queue_lock);
116 if (!ctl->running) {
117 mutex_unlock(&ctl->request_queue_lock);
118 return -ENOTCONN;
119 }
120 req->ctl = ctl;
121 list_add_tail(&req->list, &ctl->request_queue);
122 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
123 mutex_unlock(&ctl->request_queue_lock);
124 return 0;
125}
126
127static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
128{
129 struct tb_ctl *ctl = req->ctl;
130
131 mutex_lock(&ctl->request_queue_lock);
132 list_del(&req->list);
133 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
134 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
135 wake_up(&tb_cfg_request_cancel_queue);
136 mutex_unlock(&ctl->request_queue_lock);
137}
138
139static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
140{
141 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
142}
143
144static struct tb_cfg_request *
145tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
146{
147 struct tb_cfg_request *req;
148 bool found = false;
149
150 mutex_lock(&pkg->ctl->request_queue_lock);
151 list_for_each_entry(req, &pkg->ctl->request_queue, list) {
152 tb_cfg_request_get(req);
153 if (req->match(req, pkg)) {
154 found = true;
155 break;
156 }
157 tb_cfg_request_put(req);
158 }
159 mutex_unlock(&pkg->ctl->request_queue_lock);
160
161 return found ? req : NULL;
162}
163
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200164/* utility functions */
165
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300166
167static int check_header(const struct ctl_pkg *pkg, u32 len,
168 enum tb_cfg_pkg_type type, u64 route)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200169{
170 struct tb_cfg_header *header = pkg->buffer;
171
172 /* check frame, TODO: frame flags */
173 if (WARN(len != pkg->frame.size,
174 "wrong framesize (expected %#x, got %#x)\n",
175 len, pkg->frame.size))
176 return -EIO;
177 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
178 type, pkg->frame.eof))
179 return -EIO;
180 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
181 pkg->frame.sof))
182 return -EIO;
183
184 /* check header */
185 if (WARN(header->unknown != 1 << 9,
186 "header->unknown is %#x\n", header->unknown))
187 return -EIO;
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300188 if (WARN(route != tb_cfg_get_route(header),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200189 "wrong route (expected %llx, got %llx)",
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300190 route, tb_cfg_get_route(header)))
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200191 return -EIO;
192 return 0;
193}
194
195static int check_config_address(struct tb_cfg_address addr,
196 enum tb_cfg_space space, u32 offset,
197 u32 length)
198{
199 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
200 return -EIO;
201 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
202 space, addr.space))
203 return -EIO;
204 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
205 offset, addr.offset))
206 return -EIO;
207 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
208 length, addr.length))
209 return -EIO;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200210 /*
211 * We cannot check addr->port as it is set to the upstream port of the
212 * sender.
213 */
214 return 0;
215}
216
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300217static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200218{
219 struct cfg_error_pkg *pkg = response->buffer;
220 struct tb_cfg_result res = { 0 };
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300221 res.response_route = tb_cfg_get_route(&pkg->header);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200222 res.response_port = 0;
223 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300224 tb_cfg_get_route(&pkg->header));
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200225 if (res.err)
226 return res;
227
228 WARN(pkg->zero1, "pkg->zero1 is %#x\n", pkg->zero1);
229 WARN(pkg->zero2, "pkg->zero1 is %#x\n", pkg->zero1);
230 WARN(pkg->zero3, "pkg->zero1 is %#x\n", pkg->zero1);
231 res.err = 1;
232 res.tb_error = pkg->error;
233 res.response_port = pkg->port;
234 return res;
235
236}
237
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300238static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200239 enum tb_cfg_pkg_type type, u64 route)
240{
241 struct tb_cfg_header *header = pkg->buffer;
242 struct tb_cfg_result res = { 0 };
243
244 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
245 return decode_error(pkg);
246
247 res.response_port = 0; /* will be updated later for cfg_read/write */
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300248 res.response_route = tb_cfg_get_route(header);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200249 res.err = check_header(pkg, len, type, route);
250 return res;
251}
252
253static void tb_cfg_print_error(struct tb_ctl *ctl,
254 const struct tb_cfg_result *res)
255{
256 WARN_ON(res->err != 1);
257 switch (res->tb_error) {
258 case TB_CFG_ERROR_PORT_NOT_CONNECTED:
259 /* Port is not connected. This can happen during surprise
260 * removal. Do not warn. */
261 return;
262 case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
263 /*
264 * Invalid cfg_space/offset/length combination in
265 * cfg_read/cfg_write.
266 */
267 tb_ctl_WARN(ctl,
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100268 "CFG_ERROR(%llx:%x): Invalid config space or offset\n",
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200269 res->response_route, res->response_port);
270 return;
271 case TB_CFG_ERROR_NO_SUCH_PORT:
272 /*
273 * - The route contains a non-existent port.
274 * - The route contains a non-PHY port (e.g. PCIe).
275 * - The port in cfg_read/cfg_write does not exist.
276 */
277 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
278 res->response_route, res->response_port);
279 return;
280 case TB_CFG_ERROR_LOOP:
281 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
282 res->response_route, res->response_port);
283 return;
284 default:
285 /* 5,6,7,9 and 11 are also valid error codes */
286 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
287 res->response_route, res->response_port);
288 return;
289 }
290}
291
Mika Westerberg16a12582017-06-06 15:24:53 +0300292static void cpu_to_be32_array(__be32 *dst, const u32 *src, size_t len)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200293{
294 int i;
295 for (i = 0; i < len; i++)
296 dst[i] = cpu_to_be32(src[i]);
297}
298
299static void be32_to_cpu_array(u32 *dst, __be32 *src, size_t len)
300{
301 int i;
302 for (i = 0; i < len; i++)
303 dst[i] = be32_to_cpu(src[i]);
304}
305
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300306static __be32 tb_crc(const void *data, size_t len)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200307{
308 return cpu_to_be32(~__crc32c_le(~0, data, len));
309}
310
311static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
312{
313 if (pkg) {
314 dma_pool_free(pkg->ctl->frame_pool,
315 pkg->buffer, pkg->frame.buffer_phy);
316 kfree(pkg);
317 }
318}
319
320static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
321{
322 struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
323 if (!pkg)
Sachin Kamat8db353b2014-06-20 14:32:32 +0530324 return NULL;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200325 pkg->ctl = ctl;
326 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
327 &pkg->frame.buffer_phy);
328 if (!pkg->buffer) {
329 kfree(pkg);
Sachin Kamat8db353b2014-06-20 14:32:32 +0530330 return NULL;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200331 }
332 return pkg;
333}
334
335
336/* RX/TX handling */
337
338static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
339 bool canceled)
340{
341 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
342 tb_ctl_pkg_free(pkg);
343}
344
345/**
346 * tb_cfg_tx() - transmit a packet on the control channel
347 *
348 * len must be a multiple of four.
349 *
350 * Return: Returns 0 on success or an error code on failure.
351 */
Mika Westerberg16a12582017-06-06 15:24:53 +0300352static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200353 enum tb_cfg_pkg_type type)
354{
355 int res;
356 struct ctl_pkg *pkg;
357 if (len % 4 != 0) { /* required for le->be conversion */
358 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
359 return -EINVAL;
360 }
361 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
362 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
363 len, TB_FRAME_SIZE - 4);
364 return -EINVAL;
365 }
366 pkg = tb_ctl_pkg_alloc(ctl);
367 if (!pkg)
368 return -ENOMEM;
369 pkg->frame.callback = tb_ctl_tx_callback;
370 pkg->frame.size = len + 4;
371 pkg->frame.sof = type;
372 pkg->frame.eof = type;
373 cpu_to_be32_array(pkg->buffer, data, len / 4);
Andreas Noever801dba52014-06-20 21:42:22 +0200374 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200375
376 res = ring_tx(ctl->tx, &pkg->frame);
377 if (res) /* ring is stopped */
378 tb_ctl_pkg_free(pkg);
379 return res;
380}
381
382/**
Mika Westerberg81a54b52017-06-06 15:25:09 +0300383 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200384 */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300385static void tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
386 struct ctl_pkg *pkg, size_t size)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200387{
Mika Westerberg81a54b52017-06-06 15:25:09 +0300388 ctl->callback(ctl->callback_data, type, pkg->buffer, size);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200389}
390
391static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
392{
393 ring_rx(pkg->ctl->rx, &pkg->frame); /*
394 * We ignore failures during stop.
395 * All rx packets are referenced
396 * from ctl->rx_packets, so we do
397 * not loose them.
398 */
399}
400
Mika Westerberg81a54b52017-06-06 15:25:09 +0300401static int tb_async_error(const struct ctl_pkg *pkg)
402{
403 const struct cfg_error_pkg *error = (const struct cfg_error_pkg *)pkg;
404
405 if (pkg->frame.eof != TB_CFG_PKG_ERROR)
406 return false;
407
408 switch (error->error) {
409 case TB_CFG_ERROR_LINK_ERROR:
410 case TB_CFG_ERROR_HEC_ERROR_DETECTED:
411 case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
412 return true;
413
414 default:
415 return false;
416 }
417}
418
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200419static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
420 bool canceled)
421{
422 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300423 struct tb_cfg_request *req;
Mika Westerberg81a54b52017-06-06 15:25:09 +0300424 __be32 crc32;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200425
426 if (canceled)
427 return; /*
428 * ring is stopped, packet is referenced from
429 * ctl->rx_packets.
430 */
431
432 if (frame->size < 4 || frame->size % 4 != 0) {
433 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
434 frame->size);
435 goto rx;
436 }
437
438 frame->size -= 4; /* remove checksum */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300439 crc32 = tb_crc(pkg->buffer, frame->size);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200440 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
441
Mika Westerberg81a54b52017-06-06 15:25:09 +0300442 switch (frame->eof) {
443 case TB_CFG_PKG_READ:
444 case TB_CFG_PKG_WRITE:
445 case TB_CFG_PKG_ERROR:
446 case TB_CFG_PKG_OVERRIDE:
447 case TB_CFG_PKG_RESET:
448 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
449 tb_ctl_err(pkg->ctl,
450 "RX: checksum mismatch, dropping packet\n");
451 goto rx;
452 }
453 if (tb_async_error(pkg)) {
454 tb_ctl_handle_event(pkg->ctl, frame->eof,
455 pkg, frame->size);
456 goto rx;
457 }
458 break;
459
460 case TB_CFG_PKG_EVENT:
461 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
462 tb_ctl_err(pkg->ctl,
463 "RX: checksum mismatch, dropping packet\n");
464 goto rx;
465 }
Mika Westerbergf67cf492017-06-06 15:25:16 +0300466 /* Fall through */
467 case TB_CFG_PKG_ICM_EVENT:
Mika Westerberg81a54b52017-06-06 15:25:09 +0300468 tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size);
469 goto rx;
470
471 default:
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300472 break;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200473 }
Mika Westerberg81a54b52017-06-06 15:25:09 +0300474
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300475 /*
476 * The received packet will be processed only if there is an
477 * active request and that the packet is what is expected. This
478 * prevents packets such as replies coming after timeout has
479 * triggered from messing with the active requests.
480 */
481 req = tb_cfg_request_find(pkg->ctl, pkg);
482 if (req) {
483 if (req->copy(req, pkg))
484 schedule_work(&req->work);
485 tb_cfg_request_put(req);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200486 }
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300487
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200488rx:
489 tb_ctl_rx_submit(pkg);
490}
491
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300492static void tb_cfg_request_work(struct work_struct *work)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200493{
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300494 struct tb_cfg_request *req = container_of(work, typeof(*req), work);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200495
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300496 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
497 req->callback(req->callback_data);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200498
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300499 tb_cfg_request_dequeue(req);
500 tb_cfg_request_put(req);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200501}
502
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300503/**
504 * tb_cfg_request() - Start control request not waiting for it to complete
505 * @ctl: Control channel to use
506 * @req: Request to start
507 * @callback: Callback called when the request is completed
508 * @callback_data: Data to be passed to @callback
509 *
510 * This queues @req on the given control channel without waiting for it
511 * to complete. When the request completes @callback is called.
512 */
513int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
514 void (*callback)(void *), void *callback_data)
515{
516 int ret;
517
518 req->flags = 0;
519 req->callback = callback;
520 req->callback_data = callback_data;
521 INIT_WORK(&req->work, tb_cfg_request_work);
522 INIT_LIST_HEAD(&req->list);
523
524 tb_cfg_request_get(req);
525 ret = tb_cfg_request_enqueue(ctl, req);
526 if (ret)
527 goto err_put;
528
529 ret = tb_ctl_tx(ctl, req->request, req->request_size,
530 req->request_type);
531 if (ret)
532 goto err_dequeue;
533
534 if (!req->response)
535 schedule_work(&req->work);
536
537 return 0;
538
539err_dequeue:
540 tb_cfg_request_dequeue(req);
541err_put:
542 tb_cfg_request_put(req);
543
544 return ret;
545}
546
547/**
548 * tb_cfg_request_cancel() - Cancel a control request
549 * @req: Request to cancel
550 * @err: Error to assign to the request
551 *
552 * This function can be used to cancel ongoing request. It will wait
553 * until the request is not active anymore.
554 */
555void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
556{
557 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
558 schedule_work(&req->work);
559 wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
560 req->result.err = err;
561}
562
563static void tb_cfg_request_complete(void *data)
564{
565 complete(data);
566}
567
568/**
569 * tb_cfg_request_sync() - Start control request and wait until it completes
570 * @ctl: Control channel to use
571 * @req: Request to start
572 * @timeout_msec: Timeout how long to wait @req to complete
573 *
574 * Starts a control request and waits until it completes. If timeout
575 * triggers the request is canceled before function returns. Note the
576 * caller needs to make sure only one message for given switch is active
577 * at a time.
578 */
579struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
580 struct tb_cfg_request *req,
581 int timeout_msec)
582{
583 unsigned long timeout = msecs_to_jiffies(timeout_msec);
584 struct tb_cfg_result res = { 0 };
585 DECLARE_COMPLETION_ONSTACK(done);
586 int ret;
587
588 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
589 if (ret) {
590 res.err = ret;
591 return res;
592 }
593
594 if (!wait_for_completion_timeout(&done, timeout))
595 tb_cfg_request_cancel(req, -ETIMEDOUT);
596
597 flush_work(&req->work);
598
599 return req->result;
600}
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200601
602/* public interface, alloc/start/stop/free */
603
604/**
605 * tb_ctl_alloc() - allocate a control channel
606 *
607 * cb will be invoked once for every hot plug event.
608 *
609 * Return: Returns a pointer on success or NULL on failure.
610 */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300611struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data)
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200612{
613 int i;
614 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
615 if (!ctl)
616 return NULL;
617 ctl->nhi = nhi;
618 ctl->callback = cb;
619 ctl->callback_data = cb_data;
620
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300621 mutex_init(&ctl->request_queue_lock);
622 INIT_LIST_HEAD(&ctl->request_queue);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200623 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
624 TB_FRAME_SIZE, 4, 0);
625 if (!ctl->frame_pool)
626 goto err;
627
Mika Westerberg046bee12017-06-06 15:24:57 +0300628 ctl->tx = ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200629 if (!ctl->tx)
630 goto err;
631
Mika Westerberg046bee12017-06-06 15:24:57 +0300632 ctl->rx = ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200633 if (!ctl->rx)
634 goto err;
635
636 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
637 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
638 if (!ctl->rx_packets[i])
639 goto err;
640 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
641 }
642
643 tb_ctl_info(ctl, "control channel created\n");
644 return ctl;
645err:
646 tb_ctl_free(ctl);
647 return NULL;
648}
649
650/**
651 * tb_ctl_free() - free a control channel
652 *
653 * Must be called after tb_ctl_stop.
654 *
655 * Must NOT be called from ctl->callback.
656 */
657void tb_ctl_free(struct tb_ctl *ctl)
658{
659 int i;
Mika Westerbergc9843eb2017-06-06 15:24:59 +0300660
661 if (!ctl)
662 return;
663
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200664 if (ctl->rx)
665 ring_free(ctl->rx);
666 if (ctl->tx)
667 ring_free(ctl->tx);
668
669 /* free RX packets */
670 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
671 tb_ctl_pkg_free(ctl->rx_packets[i]);
672
673
674 if (ctl->frame_pool)
675 dma_pool_destroy(ctl->frame_pool);
676 kfree(ctl);
677}
678
679/**
680 * tb_cfg_start() - start/resume the control channel
681 */
682void tb_ctl_start(struct tb_ctl *ctl)
683{
684 int i;
685 tb_ctl_info(ctl, "control channel starting...\n");
686 ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
687 ring_start(ctl->rx);
688 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
689 tb_ctl_rx_submit(ctl->rx_packets[i]);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300690
691 ctl->running = true;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200692}
693
694/**
695 * control() - pause the control channel
696 *
697 * All invocations of ctl->callback will have finished after this method
698 * returns.
699 *
700 * Must NOT be called from ctl->callback.
701 */
702void tb_ctl_stop(struct tb_ctl *ctl)
703{
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300704 mutex_lock(&ctl->request_queue_lock);
705 ctl->running = false;
706 mutex_unlock(&ctl->request_queue_lock);
707
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200708 ring_stop(ctl->rx);
709 ring_stop(ctl->tx);
710
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300711 if (!list_empty(&ctl->request_queue))
712 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
713 INIT_LIST_HEAD(&ctl->request_queue);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200714 tb_ctl_info(ctl, "control channel stopped\n");
715}
716
717/* public interface, commands */
718
719/**
720 * tb_cfg_error() - send error packet
721 *
722 * Return: Returns 0 on success or an error code on failure.
723 */
724int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port,
725 enum tb_cfg_error error)
726{
727 struct cfg_error_pkg pkg = {
Mika Westerberg05c242e2017-06-06 15:25:08 +0300728 .header = tb_cfg_make_header(route),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200729 .port = port,
730 .error = error,
731 };
732 tb_ctl_info(ctl, "resetting error on %llx:%x.\n", route, port);
733 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
734}
735
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300736static bool tb_cfg_match(const struct tb_cfg_request *req,
737 const struct ctl_pkg *pkg)
738{
739 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
740
741 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
742 return true;
743
744 if (pkg->frame.eof != req->response_type)
745 return false;
746 if (route != tb_cfg_get_route(req->request))
747 return false;
748 if (pkg->frame.size != req->response_size)
749 return false;
750
751 if (pkg->frame.eof == TB_CFG_PKG_READ ||
752 pkg->frame.eof == TB_CFG_PKG_WRITE) {
753 const struct cfg_read_pkg *req_hdr = req->request;
754 const struct cfg_read_pkg *res_hdr = pkg->buffer;
755
756 if (req_hdr->addr.seq != res_hdr->addr.seq)
757 return false;
758 }
759
760 return true;
761}
762
763static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
764{
765 struct tb_cfg_result res;
766
767 /* Now make sure it is in expected format */
768 res = parse_header(pkg, req->response_size, req->response_type,
769 tb_cfg_get_route(req->request));
770 if (!res.err)
771 memcpy(req->response, pkg->buffer, req->response_size);
772
773 req->result = res;
774
775 /* Always complete when first response is received */
776 return true;
777}
778
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200779/**
780 * tb_cfg_reset() - send a reset packet and wait for a response
781 *
782 * If the switch at route is incorrectly configured then we will not receive a
783 * reply (even though the switch will reset). The caller should check for
784 * -ETIMEDOUT and attempt to reconfigure the switch.
785 */
786struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
787 int timeout_msec)
788{
Mika Westerberg05c242e2017-06-06 15:25:08 +0300789 struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300790 struct tb_cfg_result res = { 0 };
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200791 struct tb_cfg_header reply;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300792 struct tb_cfg_request *req;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200793
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300794 req = tb_cfg_request_alloc();
795 if (!req) {
796 res.err = -ENOMEM;
797 return res;
798 }
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200799
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300800 req->match = tb_cfg_match;
801 req->copy = tb_cfg_copy;
802 req->request = &request;
803 req->request_size = sizeof(request);
804 req->request_type = TB_CFG_PKG_RESET;
805 req->response = &reply;
806 req->response_size = sizeof(reply);
807 req->response_type = sizeof(TB_CFG_PKG_RESET);
808
809 res = tb_cfg_request_sync(ctl, req, timeout_msec);
810
811 tb_cfg_request_put(req);
812
813 return res;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200814}
815
816/**
817 * tb_cfg_read() - read from config space into buffer
818 *
819 * Offset and length are in dwords.
820 */
821struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
822 u64 route, u32 port, enum tb_cfg_space space,
823 u32 offset, u32 length, int timeout_msec)
824{
825 struct tb_cfg_result res = { 0 };
826 struct cfg_read_pkg request = {
Mika Westerberg05c242e2017-06-06 15:25:08 +0300827 .header = tb_cfg_make_header(route),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200828 .addr = {
829 .port = port,
830 .space = space,
831 .offset = offset,
832 .length = length,
833 },
834 };
835 struct cfg_write_pkg reply;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300836 int retries = 0;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200837
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300838 while (retries < TB_CTL_RETRIES) {
839 struct tb_cfg_request *req;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200840
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300841 req = tb_cfg_request_alloc();
842 if (!req) {
843 res.err = -ENOMEM;
844 return res;
845 }
846
847 request.addr.seq = retries++;
848
849 req->match = tb_cfg_match;
850 req->copy = tb_cfg_copy;
851 req->request = &request;
852 req->request_size = sizeof(request);
853 req->request_type = TB_CFG_PKG_READ;
854 req->response = &reply;
855 req->response_size = 12 + 4 * length;
856 req->response_type = TB_CFG_PKG_READ;
857
858 res = tb_cfg_request_sync(ctl, req, timeout_msec);
859
860 tb_cfg_request_put(req);
861
862 if (res.err != -ETIMEDOUT)
863 break;
864
865 /* Wait a bit (arbitrary time) until we send a retry */
866 usleep_range(10, 100);
867 }
868
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200869 if (res.err)
870 return res;
871
872 res.response_port = reply.addr.port;
873 res.err = check_config_address(reply.addr, space, offset, length);
874 if (!res.err)
875 memcpy(buffer, &reply.data, 4 * length);
876 return res;
877}
878
879/**
880 * tb_cfg_write() - write from buffer into config space
881 *
882 * Offset and length are in dwords.
883 */
Mika Westerberg16a12582017-06-06 15:24:53 +0300884struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200885 u64 route, u32 port, enum tb_cfg_space space,
886 u32 offset, u32 length, int timeout_msec)
887{
888 struct tb_cfg_result res = { 0 };
889 struct cfg_write_pkg request = {
Mika Westerberg05c242e2017-06-06 15:25:08 +0300890 .header = tb_cfg_make_header(route),
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200891 .addr = {
892 .port = port,
893 .space = space,
894 .offset = offset,
895 .length = length,
896 },
897 };
898 struct cfg_read_pkg reply;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300899 int retries = 0;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200900
901 memcpy(&request.data, buffer, length * 4);
902
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300903 while (retries < TB_CTL_RETRIES) {
904 struct tb_cfg_request *req;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200905
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300906 req = tb_cfg_request_alloc();
907 if (!req) {
908 res.err = -ENOMEM;
909 return res;
910 }
911
912 request.addr.seq = retries++;
913
914 req->match = tb_cfg_match;
915 req->copy = tb_cfg_copy;
916 req->request = &request;
917 req->request_size = 12 + 4 * length;
918 req->request_type = TB_CFG_PKG_WRITE;
919 req->response = &reply;
920 req->response_size = sizeof(reply);
921 req->response_type = TB_CFG_PKG_WRITE;
922
923 res = tb_cfg_request_sync(ctl, req, timeout_msec);
924
925 tb_cfg_request_put(req);
926
927 if (res.err != -ETIMEDOUT)
928 break;
929
930 /* Wait a bit (arbitrary time) until we send a retry */
931 usleep_range(10, 100);
932 }
933
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200934 if (res.err)
935 return res;
936
937 res.response_port = reply.addr.port;
938 res.err = check_config_address(reply.addr, space, offset, length);
939 return res;
940}
941
942int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
943 enum tb_cfg_space space, u32 offset, u32 length)
944{
945 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
946 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300947 switch (res.err) {
948 case 0:
949 /* Success */
950 break;
951
952 case 1:
953 /* Thunderbolt error, tb_error holds the actual number */
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200954 tb_cfg_print_error(ctl, &res);
955 return -EIO;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300956
957 case -ETIMEDOUT:
958 tb_ctl_warn(ctl, "timeout reading config space %u from %#x\n",
959 space, offset);
960 break;
961
962 default:
963 WARN(1, "tb_cfg_read: %d\n", res.err);
964 break;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200965 }
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200966 return res.err;
967}
968
Mika Westerberg16a12582017-06-06 15:24:53 +0300969int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200970 enum tb_cfg_space space, u32 offset, u32 length)
971{
972 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
973 space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300974 switch (res.err) {
975 case 0:
976 /* Success */
977 break;
978
979 case 1:
980 /* Thunderbolt error, tb_error holds the actual number */
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200981 tb_cfg_print_error(ctl, &res);
982 return -EIO;
Mika Westerbergd7f781b2017-06-06 15:25:10 +0300983
984 case -ETIMEDOUT:
985 tb_ctl_warn(ctl, "timeout writing config space %u to %#x\n",
986 space, offset);
987 break;
988
989 default:
990 WARN(1, "tb_cfg_write: %d\n", res.err);
991 break;
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200992 }
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200993 return res.err;
994}
995
996/**
997 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
998 *
999 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1000 * returns the port number from which the reply originated.
1001 *
1002 * Return: Returns the upstream port number on success or an error code on
1003 * failure.
1004 */
1005int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1006{
1007 u32 dummy;
1008 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1009 TB_CFG_SWITCH, 0, 1,
1010 TB_CFG_DEFAULT_TIMEOUT);
1011 if (res.err == 1)
1012 return -EIO;
1013 if (res.err)
1014 return res.err;
1015 return res.response_port;
1016}