1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Thunderbolt driver - control channel and configuration commands
4 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2018, Intel Corporation
7 */
8
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/pci.h>
13 #include <linux/dmapool.h>
14 #include <linux/workqueue.h>
15
16 #include "ctl.h"
17
18 #define CREATE_TRACE_POINTS
19 #include "trace.h"
20
21 #define TB_CTL_RX_PKG_COUNT 10
22 #define TB_CTL_RETRIES 4
23
24 /**
25 * struct tb_ctl - Thunderbolt control channel
26 * @nhi: Pointer to the NHI structure
27 * @tx: Transmit ring
28 * @rx: Receive ring
29 * @frame_pool: DMA pool for control messages
30 * @rx_packets: Received control messages
31 * @request_queue_lock: Lock protecting @request_queue
32 * @request_queue: List of outstanding requests
33 * @running: Is the control channel running at the moment
34 * @timeout_msec: Default timeout for non-raw control messages
35 * @callback: Callback called when hotplug message is received
36 * @callback_data: Data passed to @callback
37 * @index: Domain number. This will be output with the trace record.
38 */
39 struct tb_ctl {
40 struct tb_nhi *nhi;
41 struct tb_ring *tx;
42 struct tb_ring *rx;
43
44 struct dma_pool *frame_pool;
45 struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
46 struct mutex request_queue_lock;
47 struct list_head request_queue;
48 bool running;
49
50 int timeout_msec;
51 event_cb callback;
52 void *callback_data;
53
54 int index;
55 };
56
57
58 #define tb_ctl_WARN(ctl, format, arg...) \
59 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
60
61 #define tb_ctl_err(ctl, format, arg...) \
62 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
63
64 #define tb_ctl_warn(ctl, format, arg...) \
65 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
66
67 #define tb_ctl_info(ctl, format, arg...) \
68 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
69
70 #define tb_ctl_dbg(ctl, format, arg...) \
71 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
72
73 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
74 /* Serializes access to request kref_get/put */
75 static DEFINE_MUTEX(tb_cfg_request_lock);
76
77 /**
78 * tb_cfg_request_alloc() - Allocates a new config request
79 *
80 * This is refcounted object so when you are done with this, call
81 * tb_cfg_request_put() to it.
82 */
tb_cfg_request_alloc(void)83 struct tb_cfg_request *tb_cfg_request_alloc(void)
84 {
85 struct tb_cfg_request *req;
86
87 req = kzalloc(sizeof(*req), GFP_KERNEL);
88 if (!req)
89 return NULL;
90
91 kref_init(&req->kref);
92
93 return req;
94 }
95
96 /**
97 * tb_cfg_request_get() - Increase refcount of a request
98 * @req: Request whose refcount is increased
99 */
tb_cfg_request_get(struct tb_cfg_request * req)100 void tb_cfg_request_get(struct tb_cfg_request *req)
101 {
102 mutex_lock(&tb_cfg_request_lock);
103 kref_get(&req->kref);
104 mutex_unlock(&tb_cfg_request_lock);
105 }
106
tb_cfg_request_destroy(struct kref * kref)107 static void tb_cfg_request_destroy(struct kref *kref)
108 {
109 struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
110
111 kfree(req);
112 }
113
114 /**
115 * tb_cfg_request_put() - Decrease refcount and possibly release the request
116 * @req: Request whose refcount is decreased
117 *
118 * Call this function when you are done with the request. When refcount
119 * goes to %0 the object is released.
120 */
tb_cfg_request_put(struct tb_cfg_request * req)121 void tb_cfg_request_put(struct tb_cfg_request *req)
122 {
123 mutex_lock(&tb_cfg_request_lock);
124 kref_put(&req->kref, tb_cfg_request_destroy);
125 mutex_unlock(&tb_cfg_request_lock);
126 }
127
tb_cfg_request_enqueue(struct tb_ctl * ctl,struct tb_cfg_request * req)128 static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
129 struct tb_cfg_request *req)
130 {
131 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
132 WARN_ON(req->ctl);
133
134 mutex_lock(&ctl->request_queue_lock);
135 if (!ctl->running) {
136 mutex_unlock(&ctl->request_queue_lock);
137 return -ENOTCONN;
138 }
139 req->ctl = ctl;
140 list_add_tail(&req->list, &ctl->request_queue);
141 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
142 mutex_unlock(&ctl->request_queue_lock);
143 return 0;
144 }
145
tb_cfg_request_dequeue(struct tb_cfg_request * req)146 static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
147 {
148 struct tb_ctl *ctl = req->ctl;
149
150 mutex_lock(&ctl->request_queue_lock);
151 if (!test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags)) {
152 mutex_unlock(&ctl->request_queue_lock);
153 return;
154 }
155
156 list_del(&req->list);
157 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
158 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
159 wake_up(&tb_cfg_request_cancel_queue);
160 mutex_unlock(&ctl->request_queue_lock);
161 }
162
tb_cfg_request_is_active(struct tb_cfg_request * req)163 static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
164 {
165 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
166 }
167
168 static struct tb_cfg_request *
tb_cfg_request_find(struct tb_ctl * ctl,struct ctl_pkg * pkg)169 tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
170 {
171 struct tb_cfg_request *req = NULL, *iter;
172
173 mutex_lock(&pkg->ctl->request_queue_lock);
174 list_for_each_entry(iter, &pkg->ctl->request_queue, list) {
175 tb_cfg_request_get(iter);
176 if (iter->match(iter, pkg)) {
177 req = iter;
178 break;
179 }
180 tb_cfg_request_put(iter);
181 }
182 mutex_unlock(&pkg->ctl->request_queue_lock);
183
184 return req;
185 }
186
187 /* utility functions */
188
189
check_header(const struct ctl_pkg * pkg,u32 len,enum tb_cfg_pkg_type type,u64 route)190 static int check_header(const struct ctl_pkg *pkg, u32 len,
191 enum tb_cfg_pkg_type type, u64 route)
192 {
193 struct tb_cfg_header *header = pkg->buffer;
194
195 /* check frame, TODO: frame flags */
196 if (WARN(len != pkg->frame.size,
197 "wrong framesize (expected %#x, got %#x)\n",
198 len, pkg->frame.size))
199 return -EIO;
200 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
201 type, pkg->frame.eof))
202 return -EIO;
203 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
204 pkg->frame.sof))
205 return -EIO;
206
207 /* check header */
208 if (WARN(header->unknown != 1 << 9,
209 "header->unknown is %#x\n", header->unknown))
210 return -EIO;
211 if (WARN(route != tb_cfg_get_route(header),
212 "wrong route (expected %llx, got %llx)",
213 route, tb_cfg_get_route(header)))
214 return -EIO;
215 return 0;
216 }
217
check_config_address(struct tb_cfg_address addr,enum tb_cfg_space space,u32 offset,u32 length)218 static int check_config_address(struct tb_cfg_address addr,
219 enum tb_cfg_space space, u32 offset,
220 u32 length)
221 {
222 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
223 return -EIO;
224 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
225 space, addr.space))
226 return -EIO;
227 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
228 offset, addr.offset))
229 return -EIO;
230 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
231 length, addr.length))
232 return -EIO;
233 /*
234 * We cannot check addr->port as it is set to the upstream port of the
235 * sender.
236 */
237 return 0;
238 }
239
decode_error(const struct ctl_pkg * response)240 static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
241 {
242 struct cfg_error_pkg *pkg = response->buffer;
243 struct tb_cfg_result res = { 0 };
244 res.response_route = tb_cfg_get_route(&pkg->header);
245 res.response_port = 0;
246 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
247 tb_cfg_get_route(&pkg->header));
248 if (res.err)
249 return res;
250
251 res.err = 1;
252 res.tb_error = pkg->error;
253 res.response_port = pkg->port;
254 return res;
255
256 }
257
parse_header(const struct ctl_pkg * pkg,u32 len,enum tb_cfg_pkg_type type,u64 route)258 static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
259 enum tb_cfg_pkg_type type, u64 route)
260 {
261 struct tb_cfg_header *header = pkg->buffer;
262 struct tb_cfg_result res = { 0 };
263
264 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
265 return decode_error(pkg);
266
267 res.response_port = 0; /* will be updated later for cfg_read/write */
268 res.response_route = tb_cfg_get_route(header);
269 res.err = check_header(pkg, len, type, route);
270 return res;
271 }
272
tb_cfg_print_error(struct tb_ctl * ctl,const struct tb_cfg_result * res)273 static void tb_cfg_print_error(struct tb_ctl *ctl,
274 const struct tb_cfg_result *res)
275 {
276 WARN_ON(res->err != 1);
277 switch (res->tb_error) {
278 case TB_CFG_ERROR_PORT_NOT_CONNECTED:
279 /* Port is not connected. This can happen during surprise
280 * removal. Do not warn. */
281 return;
282 case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
283 /*
284 * Invalid cfg_space/offset/length combination in
285 * cfg_read/cfg_write.
286 */
287 tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
288 res->response_route, res->response_port);
289 return;
290 case TB_CFG_ERROR_NO_SUCH_PORT:
291 /*
292 * - The route contains a non-existent port.
293 * - The route contains a non-PHY port (e.g. PCIe).
294 * - The port in cfg_read/cfg_write does not exist.
295 */
296 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
297 res->response_route, res->response_port);
298 return;
299 case TB_CFG_ERROR_LOOP:
300 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
301 res->response_route, res->response_port);
302 return;
303 case TB_CFG_ERROR_LOCK:
304 tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n",
305 res->response_route, res->response_port);
306 return;
307 default:
308 /* 5,6,7,9 and 11 are also valid error codes */
309 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
310 res->response_route, res->response_port);
311 return;
312 }
313 }
314
tb_crc(const void * data,size_t len)315 static __be32 tb_crc(const void *data, size_t len)
316 {
317 return cpu_to_be32(~__crc32c_le(~0, data, len));
318 }
319
tb_ctl_pkg_free(struct ctl_pkg * pkg)320 static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
321 {
322 if (pkg) {
323 dma_pool_free(pkg->ctl->frame_pool,
324 pkg->buffer, pkg->frame.buffer_phy);
325 kfree(pkg);
326 }
327 }
328
tb_ctl_pkg_alloc(struct tb_ctl * ctl)329 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
330 {
331 struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
332 if (!pkg)
333 return NULL;
334 pkg->ctl = ctl;
335 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
336 &pkg->frame.buffer_phy);
337 if (!pkg->buffer) {
338 kfree(pkg);
339 return NULL;
340 }
341 return pkg;
342 }
343
344
345 /* RX/TX handling */
346
tb_ctl_tx_callback(struct tb_ring * ring,struct ring_frame * frame,bool canceled)347 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
348 bool canceled)
349 {
350 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
351 tb_ctl_pkg_free(pkg);
352 }
353
354 /*
355 * tb_cfg_tx() - transmit a packet on the control channel
356 *
357 * len must be a multiple of four.
358 *
359 * Return: Returns 0 on success or an error code on failure.
360 */
tb_ctl_tx(struct tb_ctl * ctl,const void * data,size_t len,enum tb_cfg_pkg_type type)361 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
362 enum tb_cfg_pkg_type type)
363 {
364 int res;
365 struct ctl_pkg *pkg;
366 if (len % 4 != 0) { /* required for le->be conversion */
367 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
368 return -EINVAL;
369 }
370 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
371 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
372 len, TB_FRAME_SIZE - 4);
373 return -EINVAL;
374 }
375 pkg = tb_ctl_pkg_alloc(ctl);
376 if (!pkg)
377 return -ENOMEM;
378 pkg->frame.callback = tb_ctl_tx_callback;
379 pkg->frame.size = len + 4;
380 pkg->frame.sof = type;
381 pkg->frame.eof = type;
382
383 trace_tb_tx(ctl->index, type, data, len);
384
385 cpu_to_be32_array(pkg->buffer, data, len / 4);
386 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
387
388 res = tb_ring_tx(ctl->tx, &pkg->frame);
389 if (res) /* ring is stopped */
390 tb_ctl_pkg_free(pkg);
391 return res;
392 }
393
394 /*
395 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
396 */
tb_ctl_handle_event(struct tb_ctl * ctl,enum tb_cfg_pkg_type type,struct ctl_pkg * pkg,size_t size)397 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
398 struct ctl_pkg *pkg, size_t size)
399 {
400 trace_tb_event(ctl->index, type, pkg->buffer, size);
401 return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
402 }
403
tb_ctl_rx_submit(struct ctl_pkg * pkg)404 static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
405 {
406 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
407 * We ignore failures during stop.
408 * All rx packets are referenced
409 * from ctl->rx_packets, so we do
410 * not loose them.
411 */
412 }
413
tb_async_error(const struct ctl_pkg * pkg)414 static int tb_async_error(const struct ctl_pkg *pkg)
415 {
416 const struct cfg_error_pkg *error = pkg->buffer;
417
418 if (pkg->frame.eof != TB_CFG_PKG_ERROR)
419 return false;
420
421 switch (error->error) {
422 case TB_CFG_ERROR_LINK_ERROR:
423 case TB_CFG_ERROR_HEC_ERROR_DETECTED:
424 case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
425 case TB_CFG_ERROR_DP_BW:
426 case TB_CFG_ERROR_ROP_CMPLT:
427 case TB_CFG_ERROR_POP_CMPLT:
428 case TB_CFG_ERROR_PCIE_WAKE:
429 case TB_CFG_ERROR_DP_CON_CHANGE:
430 case TB_CFG_ERROR_DPTX_DISCOVERY:
431 case TB_CFG_ERROR_LINK_RECOVERY:
432 case TB_CFG_ERROR_ASYM_LINK:
433 return true;
434
435 default:
436 return false;
437 }
438 }
439
tb_ctl_rx_callback(struct tb_ring * ring,struct ring_frame * frame,bool canceled)440 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
441 bool canceled)
442 {
443 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
444 struct tb_cfg_request *req;
445 __be32 crc32;
446
447 if (canceled)
448 return; /*
449 * ring is stopped, packet is referenced from
450 * ctl->rx_packets.
451 */
452
453 if (frame->size < 4 || frame->size % 4 != 0) {
454 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
455 frame->size);
456 goto rx;
457 }
458
459 frame->size -= 4; /* remove checksum */
460 crc32 = tb_crc(pkg->buffer, frame->size);
461 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
462
463 switch (frame->eof) {
464 case TB_CFG_PKG_READ:
465 case TB_CFG_PKG_WRITE:
466 case TB_CFG_PKG_ERROR:
467 case TB_CFG_PKG_OVERRIDE:
468 case TB_CFG_PKG_RESET:
469 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
470 tb_ctl_err(pkg->ctl,
471 "RX: checksum mismatch, dropping packet\n");
472 goto rx;
473 }
474 if (tb_async_error(pkg)) {
475 tb_ctl_handle_event(pkg->ctl, frame->eof,
476 pkg, frame->size);
477 goto rx;
478 }
479 break;
480
481 case TB_CFG_PKG_EVENT:
482 case TB_CFG_PKG_XDOMAIN_RESP:
483 case TB_CFG_PKG_XDOMAIN_REQ:
484 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
485 tb_ctl_err(pkg->ctl,
486 "RX: checksum mismatch, dropping packet\n");
487 goto rx;
488 }
489 fallthrough;
490 case TB_CFG_PKG_ICM_EVENT:
491 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
492 goto rx;
493 break;
494
495 default:
496 break;
497 }
498
499 /*
500 * The received packet will be processed only if there is an
501 * active request and that the packet is what is expected. This
502 * prevents packets such as replies coming after timeout has
503 * triggered from messing with the active requests.
504 */
505 req = tb_cfg_request_find(pkg->ctl, pkg);
506
507 trace_tb_rx(pkg->ctl->index, frame->eof, pkg->buffer, frame->size, !req);
508
509 if (req) {
510 if (req->copy(req, pkg))
511 schedule_work(&req->work);
512 tb_cfg_request_put(req);
513 }
514
515 rx:
516 tb_ctl_rx_submit(pkg);
517 }
518
tb_cfg_request_work(struct work_struct * work)519 static void tb_cfg_request_work(struct work_struct *work)
520 {
521 struct tb_cfg_request *req = container_of(work, typeof(*req), work);
522
523 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
524 req->callback(req->callback_data);
525
526 tb_cfg_request_dequeue(req);
527 tb_cfg_request_put(req);
528 }
529
530 /**
531 * tb_cfg_request() - Start control request not waiting for it to complete
532 * @ctl: Control channel to use
533 * @req: Request to start
534 * @callback: Callback called when the request is completed
535 * @callback_data: Data to be passed to @callback
536 *
537 * This queues @req on the given control channel without waiting for it
538 * to complete. When the request completes @callback is called.
539 */
tb_cfg_request(struct tb_ctl * ctl,struct tb_cfg_request * req,void (* callback)(void *),void * callback_data)540 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
541 void (*callback)(void *), void *callback_data)
542 {
543 int ret;
544
545 req->flags = 0;
546 req->callback = callback;
547 req->callback_data = callback_data;
548 INIT_WORK(&req->work, tb_cfg_request_work);
549 INIT_LIST_HEAD(&req->list);
550
551 tb_cfg_request_get(req);
552 ret = tb_cfg_request_enqueue(ctl, req);
553 if (ret)
554 goto err_put;
555
556 ret = tb_ctl_tx(ctl, req->request, req->request_size,
557 req->request_type);
558 if (ret)
559 goto err_dequeue;
560
561 if (!req->response)
562 schedule_work(&req->work);
563
564 return 0;
565
566 err_dequeue:
567 tb_cfg_request_dequeue(req);
568 err_put:
569 tb_cfg_request_put(req);
570
571 return ret;
572 }
573
574 /**
575 * tb_cfg_request_cancel() - Cancel a control request
576 * @req: Request to cancel
577 * @err: Error to assign to the request
578 *
579 * This function can be used to cancel ongoing request. It will wait
580 * until the request is not active anymore.
581 */
tb_cfg_request_cancel(struct tb_cfg_request * req,int err)582 void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
583 {
584 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
585 schedule_work(&req->work);
586 wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
587 req->result.err = err;
588 }
589
tb_cfg_request_complete(void * data)590 static void tb_cfg_request_complete(void *data)
591 {
592 complete(data);
593 }
594
595 /**
596 * tb_cfg_request_sync() - Start control request and wait until it completes
597 * @ctl: Control channel to use
598 * @req: Request to start
599 * @timeout_msec: Timeout how long to wait @req to complete
600 *
601 * Starts a control request and waits until it completes. If timeout
602 * triggers the request is canceled before function returns. Note the
603 * caller needs to make sure only one message for given switch is active
604 * at a time.
605 */
tb_cfg_request_sync(struct tb_ctl * ctl,struct tb_cfg_request * req,int timeout_msec)606 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
607 struct tb_cfg_request *req,
608 int timeout_msec)
609 {
610 unsigned long timeout = msecs_to_jiffies(timeout_msec);
611 struct tb_cfg_result res = { 0 };
612 DECLARE_COMPLETION_ONSTACK(done);
613 int ret;
614
615 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
616 if (ret) {
617 res.err = ret;
618 return res;
619 }
620
621 if (!wait_for_completion_timeout(&done, timeout))
622 tb_cfg_request_cancel(req, -ETIMEDOUT);
623
624 flush_work(&req->work);
625
626 return req->result;
627 }
628
629 /* public interface, alloc/start/stop/free */
630
631 /**
632 * tb_ctl_alloc() - allocate a control channel
633 * @nhi: Pointer to NHI
634 * @index: Domain number
635 * @timeout_msec: Default timeout used with non-raw control messages
636 * @cb: Callback called for plug events
637 * @cb_data: Data passed to @cb
638 *
639 * cb will be invoked once for every hot plug event.
640 *
641 * Return: Returns a pointer on success or NULL on failure.
642 */
tb_ctl_alloc(struct tb_nhi * nhi,int index,int timeout_msec,event_cb cb,void * cb_data)643 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int index, int timeout_msec,
644 event_cb cb, void *cb_data)
645 {
646 int i;
647 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
648 if (!ctl)
649 return NULL;
650
651 ctl->nhi = nhi;
652 ctl->index = index;
653 ctl->timeout_msec = timeout_msec;
654 ctl->callback = cb;
655 ctl->callback_data = cb_data;
656
657 mutex_init(&ctl->request_queue_lock);
658 INIT_LIST_HEAD(&ctl->request_queue);
659 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
660 TB_FRAME_SIZE, 4, 0);
661 if (!ctl->frame_pool)
662 goto err;
663
664 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
665 if (!ctl->tx)
666 goto err;
667
668 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
669 0xffff, NULL, NULL);
670 if (!ctl->rx)
671 goto err;
672
673 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
674 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
675 if (!ctl->rx_packets[i])
676 goto err;
677 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
678 }
679
680 tb_ctl_dbg(ctl, "control channel created\n");
681 return ctl;
682 err:
683 tb_ctl_free(ctl);
684 return NULL;
685 }
686
687 /**
688 * tb_ctl_free() - free a control channel
689 * @ctl: Control channel to free
690 *
691 * Must be called after tb_ctl_stop.
692 *
693 * Must NOT be called from ctl->callback.
694 */
tb_ctl_free(struct tb_ctl * ctl)695 void tb_ctl_free(struct tb_ctl *ctl)
696 {
697 int i;
698
699 if (!ctl)
700 return;
701
702 if (ctl->rx)
703 tb_ring_free(ctl->rx);
704 if (ctl->tx)
705 tb_ring_free(ctl->tx);
706
707 /* free RX packets */
708 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
709 tb_ctl_pkg_free(ctl->rx_packets[i]);
710
711
712 dma_pool_destroy(ctl->frame_pool);
713 kfree(ctl);
714 }
715
716 /**
717 * tb_ctl_start() - start/resume the control channel
718 * @ctl: Control channel to start
719 */
tb_ctl_start(struct tb_ctl * ctl)720 void tb_ctl_start(struct tb_ctl *ctl)
721 {
722 int i;
723 tb_ctl_dbg(ctl, "control channel starting...\n");
724 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
725 tb_ring_start(ctl->rx);
726 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
727 tb_ctl_rx_submit(ctl->rx_packets[i]);
728
729 ctl->running = true;
730 }
731
732 /**
733 * tb_ctl_stop() - pause the control channel
734 * @ctl: Control channel to stop
735 *
736 * All invocations of ctl->callback will have finished after this method
737 * returns.
738 *
739 * Must NOT be called from ctl->callback.
740 */
tb_ctl_stop(struct tb_ctl * ctl)741 void tb_ctl_stop(struct tb_ctl *ctl)
742 {
743 mutex_lock(&ctl->request_queue_lock);
744 ctl->running = false;
745 mutex_unlock(&ctl->request_queue_lock);
746
747 tb_ring_stop(ctl->rx);
748 tb_ring_stop(ctl->tx);
749
750 if (!list_empty(&ctl->request_queue))
751 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
752 INIT_LIST_HEAD(&ctl->request_queue);
753 tb_ctl_dbg(ctl, "control channel stopped\n");
754 }
755
756 /* public interface, commands */
757
758 /**
759 * tb_cfg_ack_notification() - Ack notification
760 * @ctl: Control channel to use
761 * @route: Router that originated the event
762 * @error: Pointer to the notification package
763 *
764 * Call this as response for non-plug notification to ack it. Returns
765 * %0 on success or an error code on failure.
766 */
tb_cfg_ack_notification(struct tb_ctl * ctl,u64 route,const struct cfg_error_pkg * error)767 int tb_cfg_ack_notification(struct tb_ctl *ctl, u64 route,
768 const struct cfg_error_pkg *error)
769 {
770 struct cfg_ack_pkg pkg = {
771 .header = tb_cfg_make_header(route),
772 };
773 const char *name;
774
775 switch (error->error) {
776 case TB_CFG_ERROR_LINK_ERROR:
777 name = "link error";
778 break;
779 case TB_CFG_ERROR_HEC_ERROR_DETECTED:
780 name = "HEC error";
781 break;
782 case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
783 name = "flow control error";
784 break;
785 case TB_CFG_ERROR_DP_BW:
786 name = "DP_BW";
787 break;
788 case TB_CFG_ERROR_ROP_CMPLT:
789 name = "router operation completion";
790 break;
791 case TB_CFG_ERROR_POP_CMPLT:
792 name = "port operation completion";
793 break;
794 case TB_CFG_ERROR_PCIE_WAKE:
795 name = "PCIe wake";
796 break;
797 case TB_CFG_ERROR_DP_CON_CHANGE:
798 name = "DP connector change";
799 break;
800 case TB_CFG_ERROR_DPTX_DISCOVERY:
801 name = "DPTX discovery";
802 break;
803 case TB_CFG_ERROR_LINK_RECOVERY:
804 name = "link recovery";
805 break;
806 case TB_CFG_ERROR_ASYM_LINK:
807 name = "asymmetric link";
808 break;
809 default:
810 name = "unknown";
811 break;
812 }
813
814 tb_ctl_dbg(ctl, "acking %s (%#x) notification on %llx\n", name,
815 error->error, route);
816
817 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_NOTIFY_ACK);
818 }
819
820 /**
821 * tb_cfg_ack_plug() - Ack hot plug/unplug event
822 * @ctl: Control channel to use
823 * @route: Router that originated the event
824 * @port: Port where the hot plug/unplug happened
825 * @unplug: Ack hot plug or unplug
826 *
827 * Call this as response for hot plug/unplug event to ack it.
828 * Returns %0 on success or an error code on failure.
829 */
tb_cfg_ack_plug(struct tb_ctl * ctl,u64 route,u32 port,bool unplug)830 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
831 {
832 struct cfg_error_pkg pkg = {
833 .header = tb_cfg_make_header(route),
834 .port = port,
835 .error = TB_CFG_ERROR_ACK_PLUG_EVENT,
836 .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
837 : TB_CFG_ERROR_PG_HOT_PLUG,
838 };
839 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%u\n",
840 unplug ? "un" : "", route, port);
841 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
842 }
843
tb_cfg_match(const struct tb_cfg_request * req,const struct ctl_pkg * pkg)844 static bool tb_cfg_match(const struct tb_cfg_request *req,
845 const struct ctl_pkg *pkg)
846 {
847 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
848
849 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
850 return true;
851
852 if (pkg->frame.eof != req->response_type)
853 return false;
854 if (route != tb_cfg_get_route(req->request))
855 return false;
856 if (pkg->frame.size != req->response_size)
857 return false;
858
859 if (pkg->frame.eof == TB_CFG_PKG_READ ||
860 pkg->frame.eof == TB_CFG_PKG_WRITE) {
861 const struct cfg_read_pkg *req_hdr = req->request;
862 const struct cfg_read_pkg *res_hdr = pkg->buffer;
863
864 if (req_hdr->addr.seq != res_hdr->addr.seq)
865 return false;
866 }
867
868 return true;
869 }
870
tb_cfg_copy(struct tb_cfg_request * req,const struct ctl_pkg * pkg)871 static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
872 {
873 struct tb_cfg_result res;
874
875 /* Now make sure it is in expected format */
876 res = parse_header(pkg, req->response_size, req->response_type,
877 tb_cfg_get_route(req->request));
878 if (!res.err)
879 memcpy(req->response, pkg->buffer, req->response_size);
880
881 req->result = res;
882
883 /* Always complete when first response is received */
884 return true;
885 }
886
887 /**
888 * tb_cfg_reset() - send a reset packet and wait for a response
889 * @ctl: Control channel pointer
890 * @route: Router string for the router to send reset
891 *
892 * If the switch at route is incorrectly configured then we will not receive a
893 * reply (even though the switch will reset). The caller should check for
894 * -ETIMEDOUT and attempt to reconfigure the switch.
895 */
tb_cfg_reset(struct tb_ctl * ctl,u64 route)896 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route)
897 {
898 struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
899 struct tb_cfg_result res = { 0 };
900 struct tb_cfg_header reply;
901 struct tb_cfg_request *req;
902
903 req = tb_cfg_request_alloc();
904 if (!req) {
905 res.err = -ENOMEM;
906 return res;
907 }
908
909 req->match = tb_cfg_match;
910 req->copy = tb_cfg_copy;
911 req->request = &request;
912 req->request_size = sizeof(request);
913 req->request_type = TB_CFG_PKG_RESET;
914 req->response = &reply;
915 req->response_size = sizeof(reply);
916 req->response_type = TB_CFG_PKG_RESET;
917
918 res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec);
919
920 tb_cfg_request_put(req);
921
922 return res;
923 }
924
925 /**
926 * tb_cfg_read_raw() - read from config space into buffer
927 * @ctl: Pointer to the control channel
928 * @buffer: Buffer where the data is read
929 * @route: Route string of the router
930 * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
931 * @space: Config space selector
932 * @offset: Dword word offset of the register to start reading
933 * @length: Number of dwords to read
934 * @timeout_msec: Timeout in ms how long to wait for the response
935 *
936 * Reads from router config space without translating the possible error.
937 */
tb_cfg_read_raw(struct tb_ctl * ctl,void * buffer,u64 route,u32 port,enum tb_cfg_space space,u32 offset,u32 length,int timeout_msec)938 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
939 u64 route, u32 port, enum tb_cfg_space space,
940 u32 offset, u32 length, int timeout_msec)
941 {
942 struct tb_cfg_result res = { 0 };
943 struct cfg_read_pkg request = {
944 .header = tb_cfg_make_header(route),
945 .addr = {
946 .port = port,
947 .space = space,
948 .offset = offset,
949 .length = length,
950 },
951 };
952 struct cfg_write_pkg reply;
953 int retries = 0;
954
955 while (retries < TB_CTL_RETRIES) {
956 struct tb_cfg_request *req;
957
958 req = tb_cfg_request_alloc();
959 if (!req) {
960 res.err = -ENOMEM;
961 return res;
962 }
963
964 request.addr.seq = retries++;
965
966 req->match = tb_cfg_match;
967 req->copy = tb_cfg_copy;
968 req->request = &request;
969 req->request_size = sizeof(request);
970 req->request_type = TB_CFG_PKG_READ;
971 req->response = &reply;
972 req->response_size = 12 + 4 * length;
973 req->response_type = TB_CFG_PKG_READ;
974
975 res = tb_cfg_request_sync(ctl, req, timeout_msec);
976
977 tb_cfg_request_put(req);
978
979 if (res.err != -ETIMEDOUT)
980 break;
981
982 /* Wait a bit (arbitrary time) until we send a retry */
983 usleep_range(10, 100);
984 }
985
986 if (res.err)
987 return res;
988
989 res.response_port = reply.addr.port;
990 res.err = check_config_address(reply.addr, space, offset, length);
991 if (!res.err)
992 memcpy(buffer, &reply.data, 4 * length);
993 return res;
994 }
995
996 /**
997 * tb_cfg_write_raw() - write from buffer into config space
998 * @ctl: Pointer to the control channel
999 * @buffer: Data to write
1000 * @route: Route string of the router
1001 * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
1002 * @space: Config space selector
1003 * @offset: Dword word offset of the register to start writing
1004 * @length: Number of dwords to write
1005 * @timeout_msec: Timeout in ms how long to wait for the response
1006 *
1007 * Writes to router config space without translating the possible error.
1008 */
tb_cfg_write_raw(struct tb_ctl * ctl,const void * buffer,u64 route,u32 port,enum tb_cfg_space space,u32 offset,u32 length,int timeout_msec)1009 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
1010 u64 route, u32 port, enum tb_cfg_space space,
1011 u32 offset, u32 length, int timeout_msec)
1012 {
1013 struct tb_cfg_result res = { 0 };
1014 struct cfg_write_pkg request = {
1015 .header = tb_cfg_make_header(route),
1016 .addr = {
1017 .port = port,
1018 .space = space,
1019 .offset = offset,
1020 .length = length,
1021 },
1022 };
1023 struct cfg_read_pkg reply;
1024 int retries = 0;
1025
1026 memcpy(&request.data, buffer, length * 4);
1027
1028 while (retries < TB_CTL_RETRIES) {
1029 struct tb_cfg_request *req;
1030
1031 req = tb_cfg_request_alloc();
1032 if (!req) {
1033 res.err = -ENOMEM;
1034 return res;
1035 }
1036
1037 request.addr.seq = retries++;
1038
1039 req->match = tb_cfg_match;
1040 req->copy = tb_cfg_copy;
1041 req->request = &request;
1042 req->request_size = 12 + 4 * length;
1043 req->request_type = TB_CFG_PKG_WRITE;
1044 req->response = &reply;
1045 req->response_size = sizeof(reply);
1046 req->response_type = TB_CFG_PKG_WRITE;
1047
1048 res = tb_cfg_request_sync(ctl, req, timeout_msec);
1049
1050 tb_cfg_request_put(req);
1051
1052 if (res.err != -ETIMEDOUT)
1053 break;
1054
1055 /* Wait a bit (arbitrary time) until we send a retry */
1056 usleep_range(10, 100);
1057 }
1058
1059 if (res.err)
1060 return res;
1061
1062 res.response_port = reply.addr.port;
1063 res.err = check_config_address(reply.addr, space, offset, length);
1064 return res;
1065 }
1066
tb_cfg_get_error(struct tb_ctl * ctl,enum tb_cfg_space space,const struct tb_cfg_result * res)1067 static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
1068 const struct tb_cfg_result *res)
1069 {
1070 /*
1071 * For unimplemented ports access to port config space may return
1072 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
1073 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
1074 * that the caller can mark the port as disabled.
1075 */
1076 if (space == TB_CFG_PORT &&
1077 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
1078 return -ENODEV;
1079
1080 tb_cfg_print_error(ctl, res);
1081
1082 if (res->tb_error == TB_CFG_ERROR_LOCK)
1083 return -EACCES;
1084 if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED)
1085 return -ENOTCONN;
1086
1087 return -EIO;
1088 }
1089
tb_cfg_read(struct tb_ctl * ctl,void * buffer,u64 route,u32 port,enum tb_cfg_space space,u32 offset,u32 length)1090 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
1091 enum tb_cfg_space space, u32 offset, u32 length)
1092 {
1093 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
1094 space, offset, length, ctl->timeout_msec);
1095 switch (res.err) {
1096 case 0:
1097 /* Success */
1098 break;
1099
1100 case 1:
1101 /* Thunderbolt error, tb_error holds the actual number */
1102 return tb_cfg_get_error(ctl, space, &res);
1103
1104 case -ETIMEDOUT:
1105 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
1106 route, space, offset);
1107 break;
1108
1109 default:
1110 WARN(1, "tb_cfg_read: %d\n", res.err);
1111 break;
1112 }
1113 return res.err;
1114 }
1115
tb_cfg_write(struct tb_ctl * ctl,const void * buffer,u64 route,u32 port,enum tb_cfg_space space,u32 offset,u32 length)1116 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
1117 enum tb_cfg_space space, u32 offset, u32 length)
1118 {
1119 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
1120 space, offset, length, ctl->timeout_msec);
1121 switch (res.err) {
1122 case 0:
1123 /* Success */
1124 break;
1125
1126 case 1:
1127 /* Thunderbolt error, tb_error holds the actual number */
1128 return tb_cfg_get_error(ctl, space, &res);
1129
1130 case -ETIMEDOUT:
1131 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
1132 route, space, offset);
1133 break;
1134
1135 default:
1136 WARN(1, "tb_cfg_write: %d\n", res.err);
1137 break;
1138 }
1139 return res.err;
1140 }
1141
1142 /**
1143 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1144 * @ctl: Pointer to the control channel
1145 * @route: Route string of the router
1146 *
1147 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1148 * returns the port number from which the reply originated.
1149 *
1150 * Return: Returns the upstream port number on success or an error code on
1151 * failure.
1152 */
tb_cfg_get_upstream_port(struct tb_ctl * ctl,u64 route)1153 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1154 {
1155 u32 dummy;
1156 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1157 TB_CFG_SWITCH, 0, 1,
1158 ctl->timeout_msec);
1159 if (res.err == 1)
1160 return -EIO;
1161 if (res.err)
1162 return res.err;
1163 return res.response_port;
1164 }
1165