Lines Matching full:ctl
16 #include "ctl.h"
41 #define tb_ctl_WARN(ctl, format, arg...) \ argument
42 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
44 #define tb_ctl_err(ctl, format, arg...) \ argument
45 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
47 #define tb_ctl_warn(ctl, format, arg...) \ argument
48 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
50 #define tb_ctl_info(ctl, format, arg...) \ argument
51 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
53 #define tb_ctl_dbg(ctl, format, arg...) \ argument
54 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
111 static int tb_cfg_request_enqueue(struct tb_ctl *ctl, in tb_cfg_request_enqueue() argument
115 WARN_ON(req->ctl); in tb_cfg_request_enqueue()
117 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
118 if (!ctl->running) { in tb_cfg_request_enqueue()
119 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
122 req->ctl = ctl; in tb_cfg_request_enqueue()
123 list_add_tail(&req->list, &ctl->request_queue); in tb_cfg_request_enqueue()
125 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
131 struct tb_ctl *ctl = req->ctl; in tb_cfg_request_dequeue() local
133 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
138 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
147 tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg) in tb_cfg_request_find() argument
152 mutex_lock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
153 list_for_each_entry(req, &pkg->ctl->request_queue, list) { in tb_cfg_request_find()
161 mutex_unlock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
222 struct tb_ctl *ctl = response->ctl; in decode_error() local
232 tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1); in decode_error()
234 tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2); in decode_error()
236 tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3); in decode_error()
260 static void tb_cfg_print_error(struct tb_ctl *ctl, in tb_cfg_print_error() argument
274 tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n", in tb_cfg_print_error()
283 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n", in tb_cfg_print_error()
287 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n", in tb_cfg_print_error()
291 tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n", in tb_cfg_print_error()
296 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n", in tb_cfg_print_error()
310 dma_pool_free(pkg->ctl->frame_pool, in tb_ctl_pkg_free()
316 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl) in tb_ctl_pkg_alloc() argument
321 pkg->ctl = ctl; in tb_ctl_pkg_alloc()
322 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, in tb_ctl_pkg_alloc()
348 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len, in tb_ctl_tx() argument
354 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len); in tb_ctl_tx()
358 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n", in tb_ctl_tx()
362 pkg = tb_ctl_pkg_alloc(ctl); in tb_ctl_tx()
372 res = tb_ring_tx(ctl->tx, &pkg->frame); in tb_ctl_tx()
379 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
381 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type, in tb_ctl_handle_event() argument
384 return ctl->callback(ctl->callback_data, type, pkg->buffer, size); in tb_ctl_handle_event()
389 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /* in tb_ctl_rx_submit()
392 * from ctl->rx_packets, so we do in tb_ctl_rx_submit()
425 * ctl->rx_packets. in tb_ctl_rx_callback()
429 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", in tb_ctl_rx_callback()
445 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
450 tb_ctl_handle_event(pkg->ctl, frame->eof, in tb_ctl_rx_callback()
460 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
466 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size)) in tb_ctl_rx_callback()
480 req = tb_cfg_request_find(pkg->ctl, pkg); in tb_ctl_rx_callback()
504 * @ctl: Control channel to use
512 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req, in tb_cfg_request() argument
524 ret = tb_cfg_request_enqueue(ctl, req); in tb_cfg_request()
528 ret = tb_ctl_tx(ctl, req->request, req->request_size, in tb_cfg_request()
569 * @ctl: Control channel to use
578 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, in tb_cfg_request_sync() argument
587 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done); in tb_cfg_request_sync()
613 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); in tb_ctl_alloc() local
614 if (!ctl) in tb_ctl_alloc()
616 ctl->nhi = nhi; in tb_ctl_alloc()
617 ctl->callback = cb; in tb_ctl_alloc()
618 ctl->callback_data = cb_data; in tb_ctl_alloc()
620 mutex_init(&ctl->request_queue_lock); in tb_ctl_alloc()
621 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_alloc()
622 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, in tb_ctl_alloc()
624 if (!ctl->frame_pool) in tb_ctl_alloc()
627 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); in tb_ctl_alloc()
628 if (!ctl->tx) in tb_ctl_alloc()
631 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0xffff, in tb_ctl_alloc()
633 if (!ctl->rx) in tb_ctl_alloc()
637 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); in tb_ctl_alloc()
638 if (!ctl->rx_packets[i]) in tb_ctl_alloc()
640 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; in tb_ctl_alloc()
643 tb_ctl_dbg(ctl, "control channel created\n"); in tb_ctl_alloc()
644 return ctl; in tb_ctl_alloc()
646 tb_ctl_free(ctl); in tb_ctl_alloc()
655 * Must NOT be called from ctl->callback.
657 void tb_ctl_free(struct tb_ctl *ctl) in tb_ctl_free() argument
661 if (!ctl) in tb_ctl_free()
664 if (ctl->rx) in tb_ctl_free()
665 tb_ring_free(ctl->rx); in tb_ctl_free()
666 if (ctl->tx) in tb_ctl_free()
667 tb_ring_free(ctl->tx); in tb_ctl_free()
671 tb_ctl_pkg_free(ctl->rx_packets[i]); in tb_ctl_free()
674 dma_pool_destroy(ctl->frame_pool); in tb_ctl_free()
675 kfree(ctl); in tb_ctl_free()
681 void tb_ctl_start(struct tb_ctl *ctl) in tb_ctl_start() argument
684 tb_ctl_dbg(ctl, "control channel starting...\n"); in tb_ctl_start()
685 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ in tb_ctl_start()
686 tb_ring_start(ctl->rx); in tb_ctl_start()
688 tb_ctl_rx_submit(ctl->rx_packets[i]); in tb_ctl_start()
690 ctl->running = true; in tb_ctl_start()
696 * All invocations of ctl->callback will have finished after this method
699 * Must NOT be called from ctl->callback.
701 void tb_ctl_stop(struct tb_ctl *ctl) in tb_ctl_stop() argument
703 mutex_lock(&ctl->request_queue_lock); in tb_ctl_stop()
704 ctl->running = false; in tb_ctl_stop()
705 mutex_unlock(&ctl->request_queue_lock); in tb_ctl_stop()
707 tb_ring_stop(ctl->rx); in tb_ctl_stop()
708 tb_ring_stop(ctl->tx); in tb_ctl_stop()
710 if (!list_empty(&ctl->request_queue)) in tb_ctl_stop()
711 tb_ctl_WARN(ctl, "dangling request in request_queue\n"); in tb_ctl_stop()
712 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_stop()
713 tb_ctl_dbg(ctl, "control channel stopped\n"); in tb_ctl_stop()
720 * @ctl: Control channel to use
728 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug) in tb_cfg_ack_plug() argument
737 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n", in tb_cfg_ack_plug()
739 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR); in tb_cfg_ack_plug()
792 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route, in tb_cfg_reset() argument
815 res = tb_cfg_request_sync(ctl, req, timeout_msec); in tb_cfg_reset()
827 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, in tb_cfg_read_raw() argument
864 res = tb_cfg_request_sync(ctl, req, timeout_msec); in tb_cfg_read_raw()
890 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, in tb_cfg_write_raw() argument
929 res = tb_cfg_request_sync(ctl, req, timeout_msec); in tb_cfg_write_raw()
948 static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space, in tb_cfg_get_error() argument
961 tb_cfg_print_error(ctl, res); in tb_cfg_get_error()
968 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, in tb_cfg_read() argument
971 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, in tb_cfg_read()
980 return tb_cfg_get_error(ctl, space, &res); in tb_cfg_read()
983 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n", in tb_cfg_read()
994 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, in tb_cfg_write() argument
997 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, in tb_cfg_write()
1006 return tb_cfg_get_error(ctl, space, &res); in tb_cfg_write()
1009 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n", in tb_cfg_write()
1029 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route) in tb_cfg_get_upstream_port() argument
1032 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0, in tb_cfg_get_upstream_port()