Lines Matching full:ctl
15 #include "ctl.h"
40 #define tb_ctl_WARN(ctl, format, arg...) \ argument
41 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
43 #define tb_ctl_err(ctl, format, arg...) \ argument
44 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
46 #define tb_ctl_warn(ctl, format, arg...) \ argument
47 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
49 #define tb_ctl_info(ctl, format, arg...) \ argument
50 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
52 #define tb_ctl_dbg(ctl, format, arg...) \ argument
53 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
110 static int tb_cfg_request_enqueue(struct tb_ctl *ctl, in tb_cfg_request_enqueue() argument
114 WARN_ON(req->ctl); in tb_cfg_request_enqueue()
116 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
117 if (!ctl->running) { in tb_cfg_request_enqueue()
118 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
121 req->ctl = ctl; in tb_cfg_request_enqueue()
122 list_add_tail(&req->list, &ctl->request_queue); in tb_cfg_request_enqueue()
124 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
130 struct tb_ctl *ctl = req->ctl; in tb_cfg_request_dequeue() local
132 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
137 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
146 tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg) in tb_cfg_request_find() argument
151 mutex_lock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
152 list_for_each_entry(req, &pkg->ctl->request_queue, list) { in tb_cfg_request_find()
160 mutex_unlock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
254 static void tb_cfg_print_error(struct tb_ctl *ctl, in tb_cfg_print_error() argument
268 tb_ctl_WARN(ctl, in tb_cfg_print_error()
278 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n", in tb_cfg_print_error()
282 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n", in tb_cfg_print_error()
287 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n", in tb_cfg_print_error()
301 dma_pool_free(pkg->ctl->frame_pool, in tb_ctl_pkg_free()
307 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl) in tb_ctl_pkg_alloc() argument
312 pkg->ctl = ctl; in tb_ctl_pkg_alloc()
313 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, in tb_ctl_pkg_alloc()
339 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len, in tb_ctl_tx() argument
345 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len); in tb_ctl_tx()
349 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n", in tb_ctl_tx()
353 pkg = tb_ctl_pkg_alloc(ctl); in tb_ctl_tx()
363 res = tb_ring_tx(ctl->tx, &pkg->frame); in tb_ctl_tx()
370 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
372 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type, in tb_ctl_handle_event() argument
375 return ctl->callback(ctl->callback_data, type, pkg->buffer, size); in tb_ctl_handle_event()
380 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /* in tb_ctl_rx_submit()
383 * from ctl->rx_packets, so we do in tb_ctl_rx_submit()
416 * ctl->rx_packets. in tb_ctl_rx_callback()
420 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", in tb_ctl_rx_callback()
436 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
441 tb_ctl_handle_event(pkg->ctl, frame->eof, in tb_ctl_rx_callback()
451 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
457 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size)) in tb_ctl_rx_callback()
471 req = tb_cfg_request_find(pkg->ctl, pkg); in tb_ctl_rx_callback()
495 * @ctl: Control channel to use
503 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req, in tb_cfg_request() argument
515 ret = tb_cfg_request_enqueue(ctl, req); in tb_cfg_request()
519 ret = tb_ctl_tx(ctl, req->request, req->request_size, in tb_cfg_request()
560 * @ctl: Control channel to use
569 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, in tb_cfg_request_sync() argument
578 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done); in tb_cfg_request_sync()
604 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); in tb_ctl_alloc() local
605 if (!ctl) in tb_ctl_alloc()
607 ctl->nhi = nhi; in tb_ctl_alloc()
608 ctl->callback = cb; in tb_ctl_alloc()
609 ctl->callback_data = cb_data; in tb_ctl_alloc()
611 mutex_init(&ctl->request_queue_lock); in tb_ctl_alloc()
612 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_alloc()
613 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, in tb_ctl_alloc()
615 if (!ctl->frame_pool) in tb_ctl_alloc()
618 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); in tb_ctl_alloc()
619 if (!ctl->tx) in tb_ctl_alloc()
622 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0xffff, in tb_ctl_alloc()
624 if (!ctl->rx) in tb_ctl_alloc()
628 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); in tb_ctl_alloc()
629 if (!ctl->rx_packets[i]) in tb_ctl_alloc()
631 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; in tb_ctl_alloc()
634 tb_ctl_info(ctl, "control channel created\n"); in tb_ctl_alloc()
635 return ctl; in tb_ctl_alloc()
637 tb_ctl_free(ctl); in tb_ctl_alloc()
646 * Must NOT be called from ctl->callback.
648 void tb_ctl_free(struct tb_ctl *ctl) in tb_ctl_free() argument
652 if (!ctl) in tb_ctl_free()
655 if (ctl->rx) in tb_ctl_free()
656 tb_ring_free(ctl->rx); in tb_ctl_free()
657 if (ctl->tx) in tb_ctl_free()
658 tb_ring_free(ctl->tx); in tb_ctl_free()
662 tb_ctl_pkg_free(ctl->rx_packets[i]); in tb_ctl_free()
665 if (ctl->frame_pool) in tb_ctl_free()
666 dma_pool_destroy(ctl->frame_pool); in tb_ctl_free()
667 kfree(ctl); in tb_ctl_free()
673 void tb_ctl_start(struct tb_ctl *ctl) in tb_ctl_start() argument
676 tb_ctl_info(ctl, "control channel starting...\n"); in tb_ctl_start()
677 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ in tb_ctl_start()
678 tb_ring_start(ctl->rx); in tb_ctl_start()
680 tb_ctl_rx_submit(ctl->rx_packets[i]); in tb_ctl_start()
682 ctl->running = true; in tb_ctl_start()
688 * All invocations of ctl->callback will have finished after this method
691 * Must NOT be called from ctl->callback.
693 void tb_ctl_stop(struct tb_ctl *ctl) in tb_ctl_stop() argument
695 mutex_lock(&ctl->request_queue_lock); in tb_ctl_stop()
696 ctl->running = false; in tb_ctl_stop()
697 mutex_unlock(&ctl->request_queue_lock); in tb_ctl_stop()
699 tb_ring_stop(ctl->rx); in tb_ctl_stop()
700 tb_ring_stop(ctl->tx); in tb_ctl_stop()
702 if (!list_empty(&ctl->request_queue)) in tb_ctl_stop()
703 tb_ctl_WARN(ctl, "dangling request in request_queue\n"); in tb_ctl_stop()
704 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_stop()
705 tb_ctl_info(ctl, "control channel stopped\n"); in tb_ctl_stop()
715 int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port, in tb_cfg_error() argument
723 tb_ctl_info(ctl, "resetting error on %llx:%x.\n", route, port); in tb_cfg_error()
724 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR); in tb_cfg_error()
777 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route, in tb_cfg_reset() argument
800 res = tb_cfg_request_sync(ctl, req, timeout_msec); in tb_cfg_reset()
812 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, in tb_cfg_read_raw() argument
849 res = tb_cfg_request_sync(ctl, req, timeout_msec); in tb_cfg_read_raw()
875 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, in tb_cfg_write_raw() argument
914 res = tb_cfg_request_sync(ctl, req, timeout_msec); in tb_cfg_write_raw()
933 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, in tb_cfg_read() argument
936 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, in tb_cfg_read()
945 tb_cfg_print_error(ctl, &res); in tb_cfg_read()
949 tb_ctl_warn(ctl, "timeout reading config space %u from %#x\n", in tb_cfg_read()
960 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, in tb_cfg_write() argument
963 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, in tb_cfg_write()
972 tb_cfg_print_error(ctl, &res); in tb_cfg_write()
976 tb_ctl_warn(ctl, "timeout writing config space %u to %#x\n", in tb_cfg_write()
996 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route) in tb_cfg_get_upstream_port() argument
999 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0, in tb_cfg_get_upstream_port()