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