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