Lines Matching defs:ctl

16 #include "ctl.h"
53 #define tb_ctl_WARN(ctl, format, arg...) \
54 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
56 #define tb_ctl_err(ctl, format, arg...) \
57 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
59 #define tb_ctl_warn(ctl, format, arg...) \
60 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
62 #define tb_ctl_info(ctl, format, arg...) \
63 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
65 #define tb_ctl_dbg(ctl, format, arg...) \
66 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
123 static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
127 WARN_ON(req->ctl);
129 mutex_lock(&ctl->request_queue_lock);
130 if (!ctl->running) {
131 mutex_unlock(&ctl->request_queue_lock);
134 req->ctl = ctl;
135 list_add_tail(&req->list, &ctl->request_queue);
137 mutex_unlock(&ctl->request_queue_lock);
143 struct tb_ctl *ctl = req->ctl;
145 mutex_lock(&ctl->request_queue_lock);
150 mutex_unlock(&ctl->request_queue_lock);
159 tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
163 mutex_lock(&pkg->ctl->request_queue_lock);
164 list_for_each_entry(iter, &pkg->ctl->request_queue, list) {
172 mutex_unlock(&pkg->ctl->request_queue_lock);
263 static void tb_cfg_print_error(struct tb_ctl *ctl,
277 tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
286 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
290 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
294 tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n",
299 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
313 dma_pool_free(pkg->ctl->frame_pool,
319 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
324 pkg->ctl = ctl;
325 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
351 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
357 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
361 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
365 pkg = tb_ctl_pkg_alloc(ctl);
375 res = tb_ring_tx(ctl->tx, &pkg->frame);
382 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
384 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
387 return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
392 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
395 * from ctl->rx_packets, so we do
436 * ctl->rx_packets.
440 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
456 tb_ctl_err(pkg->ctl,
461 tb_ctl_handle_event(pkg->ctl, frame->eof,
471 tb_ctl_err(pkg->ctl,
477 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
491 req = tb_cfg_request_find(pkg->ctl, pkg);
515 * @ctl: Control channel to use
523 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
535 ret = tb_cfg_request_enqueue(ctl, req);
539 ret = tb_ctl_tx(ctl, req->request, req->request_size,
580 * @ctl: Control channel to use
589 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
598 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
629 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
630 if (!ctl)
632 ctl->nhi = nhi;
633 ctl->timeout_msec = timeout_msec;
634 ctl->callback = cb;
635 ctl->callback_data = cb_data;
637 mutex_init(&ctl->request_queue_lock);
638 INIT_LIST_HEAD(&ctl->request_queue);
639 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
641 if (!ctl->frame_pool)
644 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
645 if (!ctl->tx)
648 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
650 if (!ctl->rx)
654 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
655 if (!ctl->rx_packets[i])
657 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
660 tb_ctl_dbg(ctl, "control channel created\n");
661 return ctl;
663 tb_ctl_free(ctl);
669 * @ctl: Control channel to free
673 * Must NOT be called from ctl->callback.
675 void tb_ctl_free(struct tb_ctl *ctl)
679 if (!ctl)
682 if (ctl->rx)
683 tb_ring_free(ctl->rx);
684 if (ctl->tx)
685 tb_ring_free(ctl->tx);
689 tb_ctl_pkg_free(ctl->rx_packets[i]);
692 dma_pool_destroy(ctl->frame_pool);
693 kfree(ctl);
698 * @ctl: Control channel to start
700 void tb_ctl_start(struct tb_ctl *ctl)
703 tb_ctl_dbg(ctl, "control channel starting...\n");
704 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
705 tb_ring_start(ctl->rx);
707 tb_ctl_rx_submit(ctl->rx_packets[i]);
709 ctl->running = true;
714 * @ctl: Control channel to stop
716 * All invocations of ctl->callback will have finished after this method
719 * Must NOT be called from ctl->callback.
721 void tb_ctl_stop(struct tb_ctl *ctl)
723 mutex_lock(&ctl->request_queue_lock);
724 ctl->running = false;
725 mutex_unlock(&ctl->request_queue_lock);
727 tb_ring_stop(ctl->rx);
728 tb_ring_stop(ctl->tx);
730 if (!list_empty(&ctl->request_queue))
731 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
732 INIT_LIST_HEAD(&ctl->request_queue);
733 tb_ctl_dbg(ctl, "control channel stopped\n");
740 * @ctl: Control channel to use
747 int tb_cfg_ack_notification(struct tb_ctl *ctl, u64 route,
794 tb_ctl_dbg(ctl, "acking %s (%#x) notification on %llx\n", name,
797 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_NOTIFY_ACK);
802 * @ctl: Control channel to use
810 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
819 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%u\n",
821 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
869 * @ctl: Control channel pointer
876 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route)
898 res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec);
907 * @ctl: Pointer to the control channel
918 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
955 res = tb_cfg_request_sync(ctl, req, timeout_msec);
978 * @ctl: Pointer to the control channel
989 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
1028 res = tb_cfg_request_sync(ctl, req, timeout_msec);
1047 static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
1060 tb_cfg_print_error(ctl, res);
1070 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
1073 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
1074 space, offset, length, ctl->timeout_msec);
1082 return tb_cfg_get_error(ctl, space, &res);
1085 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
1096 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
1099 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
1100 space, offset, length, ctl->timeout_msec);
1108 return tb_cfg_get_error(ctl, space, &res);
1111 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
1124 * @ctl: Pointer to the control channel
1133 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1136 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1138 ctl->timeout_msec);