1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - Implementation of core module of MOST Linux driver stack
5 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/list.h>
15 #include <linux/poll.h>
16 #include <linux/wait.h>
17 #include <linux/kobject.h>
18 #include <linux/mutex.h>
19 #include <linux/completion.h>
20 #include <linux/sysfs.h>
21 #include <linux/kthread.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/idr.h>
24 #include <most/core.h>
26 #define MAX_CHANNELS 64
27 #define STRING_SIZE 80
29 static struct ida mdev_id;
30 static int dummy_num_buffers;
32 static struct mostcore {
34 struct device_driver drv;
36 struct list_head comp_list;
39 #define to_driver(d) container_of(d, struct mostcore, drv)
42 struct core_component *comp;
49 struct completion cleanup;
51 atomic_t mbo_nq_level;
53 char name[STRING_SIZE];
55 struct mutex start_mutex;
56 struct mutex nq_mutex; /* nq thread synchronization */
58 struct most_interface *iface;
59 struct most_channel_config cfg;
62 struct list_head fifo;
64 struct list_head halt_fifo;
65 struct list_head list;
68 struct list_head trash_fifo;
69 struct task_struct *hdm_enqueue_task;
70 wait_queue_head_t hdm_fifo_wq;
74 #define to_channel(d) container_of(d, struct most_channel, dev)
76 struct interface_private {
78 char name[STRING_SIZE];
79 struct most_channel *channel[MAX_CHANNELS];
80 struct list_head channel_list;
84 int most_ch_data_type;
87 { MOST_CH_CONTROL, "control\n" },
88 { MOST_CH_ASYNC, "async\n" },
89 { MOST_CH_SYNC, "sync\n" },
90 { MOST_CH_ISOC, "isoc\n"},
91 { MOST_CH_ISOC, "isoc_avp\n"},
95 * list_pop_mbo - retrieves the first MBO of the list and removes it
96 * @ptr: the list head to grab the MBO from.
98 #define list_pop_mbo(ptr) \
100 struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
101 list_del(&_mbo->list); \
106 * most_free_mbo_coherent - free an MBO and its coherent buffer
109 static void most_free_mbo_coherent(struct mbo *mbo)
111 struct most_channel *c = mbo->context;
112 u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
114 if (c->iface->dma_free)
115 c->iface->dma_free(mbo, coherent_buf_size);
117 kfree(mbo->virt_address);
119 if (atomic_sub_and_test(1, &c->mbo_ref))
120 complete(&c->cleanup);
124 * flush_channel_fifos - clear the channel fifos
125 * @c: pointer to channel object
127 static void flush_channel_fifos(struct most_channel *c)
129 unsigned long flags, hf_flags;
130 struct mbo *mbo, *tmp;
132 if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
135 spin_lock_irqsave(&c->fifo_lock, flags);
136 list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
137 list_del(&mbo->list);
138 spin_unlock_irqrestore(&c->fifo_lock, flags);
139 most_free_mbo_coherent(mbo);
140 spin_lock_irqsave(&c->fifo_lock, flags);
142 spin_unlock_irqrestore(&c->fifo_lock, flags);
144 spin_lock_irqsave(&c->fifo_lock, hf_flags);
145 list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
146 list_del(&mbo->list);
147 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
148 most_free_mbo_coherent(mbo);
149 spin_lock_irqsave(&c->fifo_lock, hf_flags);
151 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
153 if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
154 pr_info("WARN: fifo | trash fifo not empty\n");
158 * flush_trash_fifo - clear the trash fifo
159 * @c: pointer to channel object
161 static int flush_trash_fifo(struct most_channel *c)
163 struct mbo *mbo, *tmp;
166 spin_lock_irqsave(&c->fifo_lock, flags);
167 list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
168 list_del(&mbo->list);
169 spin_unlock_irqrestore(&c->fifo_lock, flags);
170 most_free_mbo_coherent(mbo);
171 spin_lock_irqsave(&c->fifo_lock, flags);
173 spin_unlock_irqrestore(&c->fifo_lock, flags);
177 static ssize_t available_directions_show(struct device *dev,
178 struct device_attribute *attr,
181 struct most_channel *c = to_channel(dev);
182 unsigned int i = c->channel_id;
185 if (c->iface->channel_vector[i].direction & MOST_CH_RX)
187 if (c->iface->channel_vector[i].direction & MOST_CH_TX)
193 static ssize_t available_datatypes_show(struct device *dev,
194 struct device_attribute *attr,
197 struct most_channel *c = to_channel(dev);
198 unsigned int i = c->channel_id;
201 if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
202 strcat(buf, "control ");
203 if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
204 strcat(buf, "async ");
205 if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
206 strcat(buf, "sync ");
207 if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
208 strcat(buf, "isoc ");
213 static ssize_t number_of_packet_buffers_show(struct device *dev,
214 struct device_attribute *attr,
217 struct most_channel *c = to_channel(dev);
218 unsigned int i = c->channel_id;
220 return snprintf(buf, PAGE_SIZE, "%d\n",
221 c->iface->channel_vector[i].num_buffers_packet);
224 static ssize_t number_of_stream_buffers_show(struct device *dev,
225 struct device_attribute *attr,
228 struct most_channel *c = to_channel(dev);
229 unsigned int i = c->channel_id;
231 return snprintf(buf, PAGE_SIZE, "%d\n",
232 c->iface->channel_vector[i].num_buffers_streaming);
235 static ssize_t size_of_packet_buffer_show(struct device *dev,
236 struct device_attribute *attr,
239 struct most_channel *c = to_channel(dev);
240 unsigned int i = c->channel_id;
242 return snprintf(buf, PAGE_SIZE, "%d\n",
243 c->iface->channel_vector[i].buffer_size_packet);
246 static ssize_t size_of_stream_buffer_show(struct device *dev,
247 struct device_attribute *attr,
250 struct most_channel *c = to_channel(dev);
251 unsigned int i = c->channel_id;
253 return snprintf(buf, PAGE_SIZE, "%d\n",
254 c->iface->channel_vector[i].buffer_size_streaming);
257 static ssize_t channel_starving_show(struct device *dev,
258 struct device_attribute *attr,
261 struct most_channel *c = to_channel(dev);
263 return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
266 static ssize_t set_number_of_buffers_show(struct device *dev,
267 struct device_attribute *attr,
270 struct most_channel *c = to_channel(dev);
272 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
275 static ssize_t set_number_of_buffers_store(struct device *dev,
276 struct device_attribute *attr,
280 struct most_channel *c = to_channel(dev);
281 int ret = kstrtou16(buf, 0, &c->cfg.num_buffers);
288 static ssize_t set_buffer_size_show(struct device *dev,
289 struct device_attribute *attr,
292 struct most_channel *c = to_channel(dev);
294 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
297 static ssize_t set_buffer_size_store(struct device *dev,
298 struct device_attribute *attr,
302 struct most_channel *c = to_channel(dev);
303 int ret = kstrtou16(buf, 0, &c->cfg.buffer_size);
310 static ssize_t set_direction_show(struct device *dev,
311 struct device_attribute *attr,
314 struct most_channel *c = to_channel(dev);
316 if (c->cfg.direction & MOST_CH_TX)
317 return snprintf(buf, PAGE_SIZE, "tx\n");
318 else if (c->cfg.direction & MOST_CH_RX)
319 return snprintf(buf, PAGE_SIZE, "rx\n");
320 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
323 static ssize_t set_direction_store(struct device *dev,
324 struct device_attribute *attr,
328 struct most_channel *c = to_channel(dev);
330 if (!strcmp(buf, "dir_rx\n")) {
331 c->cfg.direction = MOST_CH_RX;
332 } else if (!strcmp(buf, "rx\n")) {
333 c->cfg.direction = MOST_CH_RX;
334 } else if (!strcmp(buf, "dir_tx\n")) {
335 c->cfg.direction = MOST_CH_TX;
336 } else if (!strcmp(buf, "tx\n")) {
337 c->cfg.direction = MOST_CH_TX;
339 pr_info("WARN: invalid attribute settings\n");
345 static ssize_t set_datatype_show(struct device *dev,
346 struct device_attribute *attr,
350 struct most_channel *c = to_channel(dev);
352 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
353 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
354 return snprintf(buf, PAGE_SIZE, "%s", ch_data_type[i].name);
356 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
359 static ssize_t set_datatype_store(struct device *dev,
360 struct device_attribute *attr,
365 struct most_channel *c = to_channel(dev);
367 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
368 if (!strcmp(buf, ch_data_type[i].name)) {
369 c->cfg.data_type = ch_data_type[i].most_ch_data_type;
374 if (i == ARRAY_SIZE(ch_data_type)) {
375 pr_info("WARN: invalid attribute settings\n");
381 static ssize_t set_subbuffer_size_show(struct device *dev,
382 struct device_attribute *attr,
385 struct most_channel *c = to_channel(dev);
387 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
390 static ssize_t set_subbuffer_size_store(struct device *dev,
391 struct device_attribute *attr,
395 struct most_channel *c = to_channel(dev);
396 int ret = kstrtou16(buf, 0, &c->cfg.subbuffer_size);
403 static ssize_t set_packets_per_xact_show(struct device *dev,
404 struct device_attribute *attr,
407 struct most_channel *c = to_channel(dev);
409 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
412 static ssize_t set_packets_per_xact_store(struct device *dev,
413 struct device_attribute *attr,
417 struct most_channel *c = to_channel(dev);
418 int ret = kstrtou16(buf, 0, &c->cfg.packets_per_xact);
425 static ssize_t set_dbr_size_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct most_channel *c = to_channel(dev);
430 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
433 static ssize_t set_dbr_size_store(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf, size_t count)
437 struct most_channel *c = to_channel(dev);
438 int ret = kstrtou16(buf, 0, &c->cfg.dbr_size);
445 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
446 static umode_t channel_attr_is_visible(struct kobject *kobj,
447 struct attribute *attr, int index)
449 struct device_attribute *dev_attr = to_dev_attr(attr);
450 struct device *dev = kobj_to_dev(kobj);
451 struct most_channel *c = to_channel(dev);
453 if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
454 (c->iface->interface != ITYPE_MEDIALB_DIM2))
456 if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
457 (c->iface->interface != ITYPE_USB))
463 #define DEV_ATTR(_name) (&dev_attr_##_name.attr)
465 static DEVICE_ATTR_RO(available_directions);
466 static DEVICE_ATTR_RO(available_datatypes);
467 static DEVICE_ATTR_RO(number_of_packet_buffers);
468 static DEVICE_ATTR_RO(number_of_stream_buffers);
469 static DEVICE_ATTR_RO(size_of_stream_buffer);
470 static DEVICE_ATTR_RO(size_of_packet_buffer);
471 static DEVICE_ATTR_RO(channel_starving);
472 static DEVICE_ATTR_RW(set_buffer_size);
473 static DEVICE_ATTR_RW(set_number_of_buffers);
474 static DEVICE_ATTR_RW(set_direction);
475 static DEVICE_ATTR_RW(set_datatype);
476 static DEVICE_ATTR_RW(set_subbuffer_size);
477 static DEVICE_ATTR_RW(set_packets_per_xact);
478 static DEVICE_ATTR_RW(set_dbr_size);
480 static struct attribute *channel_attrs[] = {
481 DEV_ATTR(available_directions),
482 DEV_ATTR(available_datatypes),
483 DEV_ATTR(number_of_packet_buffers),
484 DEV_ATTR(number_of_stream_buffers),
485 DEV_ATTR(size_of_stream_buffer),
486 DEV_ATTR(size_of_packet_buffer),
487 DEV_ATTR(channel_starving),
488 DEV_ATTR(set_buffer_size),
489 DEV_ATTR(set_number_of_buffers),
490 DEV_ATTR(set_direction),
491 DEV_ATTR(set_datatype),
492 DEV_ATTR(set_subbuffer_size),
493 DEV_ATTR(set_packets_per_xact),
494 DEV_ATTR(set_dbr_size),
498 static struct attribute_group channel_attr_group = {
499 .attrs = channel_attrs,
500 .is_visible = channel_attr_is_visible,
503 static const struct attribute_group *channel_attr_groups[] = {
508 static ssize_t description_show(struct device *dev,
509 struct device_attribute *attr,
512 struct most_interface *iface = to_most_interface(dev);
514 return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
517 static ssize_t interface_show(struct device *dev,
518 struct device_attribute *attr,
521 struct most_interface *iface = to_most_interface(dev);
523 switch (iface->interface) {
525 return snprintf(buf, PAGE_SIZE, "loopback\n");
527 return snprintf(buf, PAGE_SIZE, "i2c\n");
529 return snprintf(buf, PAGE_SIZE, "i2s\n");
531 return snprintf(buf, PAGE_SIZE, "tsi\n");
533 return snprintf(buf, PAGE_SIZE, "hbi\n");
534 case ITYPE_MEDIALB_DIM:
535 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
536 case ITYPE_MEDIALB_DIM2:
537 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
539 return snprintf(buf, PAGE_SIZE, "usb\n");
541 return snprintf(buf, PAGE_SIZE, "pcie\n");
543 return snprintf(buf, PAGE_SIZE, "unknown\n");
546 static DEVICE_ATTR_RO(description);
547 static DEVICE_ATTR_RO(interface);
549 static struct attribute *interface_attrs[] = {
550 DEV_ATTR(description),
555 static struct attribute_group interface_attr_group = {
556 .attrs = interface_attrs,
559 static const struct attribute_group *interface_attr_groups[] = {
560 &interface_attr_group,
564 static struct core_component *match_component(char *name)
566 struct core_component *comp;
568 list_for_each_entry(comp, &mc.comp_list, list) {
569 if (!strcmp(comp->name, name))
575 struct show_links_data {
580 static int print_links(struct device *dev, void *data)
582 struct show_links_data *d = data;
585 struct most_channel *c;
586 struct most_interface *iface = to_most_interface(dev);
588 list_for_each_entry(c, &iface->p->channel_list, list) {
590 offs += snprintf(buf + offs,
594 dev_name(&iface->dev),
598 offs += snprintf(buf + offs,
602 dev_name(&iface->dev),
610 static ssize_t links_show(struct device_driver *drv, char *buf)
612 struct show_links_data d = { .buf = buf };
614 bus_for_each_dev(&mc.bus, NULL, &d, print_links);
618 static ssize_t components_show(struct device_driver *drv, char *buf)
620 struct core_component *comp;
623 list_for_each_entry(comp, &mc.comp_list, list) {
624 offs += snprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
631 * split_string - parses buf and extracts ':' separated substrings.
633 * @buf: complete string from attribute 'add_channel'
634 * @a: storage for 1st substring (=interface name)
635 * @b: storage for 2nd substring (=channel name)
636 * @c: storage for 3rd substring (=component name)
637 * @d: storage optional 4th substring (=user defined name)
641 * Input: "mdev0:ch6:cdev:my_channel\n" or
642 * "mdev0:ch6:cdev:my_channel"
644 * Output: *a -> "mdev0", *b -> "ch6", *c -> "cdev" *d -> "my_channel"
646 * Input: "mdev1:ep81:cdev\n"
647 * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d -> ""
649 * Input: "mdev1:ep81"
650 * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d == NULL
652 static int split_string(char *buf, char **a, char **b, char **c, char **d)
654 *a = strsep(&buf, ":");
658 *b = strsep(&buf, ":\n");
662 *c = strsep(&buf, ":\n");
667 *d = strsep(&buf, ":\n");
672 static int match_bus_dev(struct device *dev, void *data)
674 char *mdev_name = data;
676 return !strcmp(dev_name(dev), mdev_name);
680 * get_channel - get pointer to channel
681 * @mdev: name of the device interface
682 * @mdev_ch: name of channel
684 static struct most_channel *get_channel(char *mdev, char *mdev_ch)
686 struct device *dev = NULL;
687 struct most_interface *iface;
688 struct most_channel *c, *tmp;
690 dev = bus_find_device(&mc.bus, NULL, mdev, match_bus_dev);
693 iface = to_most_interface(dev);
694 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
695 if (!strcmp(dev_name(&c->dev), mdev_ch))
702 inline int link_channel_to_component(struct most_channel *c,
703 struct core_component *comp,
707 struct core_component **comp_ptr;
710 comp_ptr = &c->pipe0.comp;
711 else if (!c->pipe1.comp)
712 comp_ptr = &c->pipe1.comp;
717 ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, comp_param);
726 * add_link_store - store function for add_link attribute
727 * @drv: device driver
729 * @len: buffer length
731 * This parses the string given by buf and splits it into
732 * four substrings. Note: last substring is optional. In case a cdev
733 * component is loaded the optional 4th substring will make up the name of
734 * device node in the /dev directory. If omitted, the device node will
735 * inherit the channel's name within sysfs.
737 * Searches for (device, channel) pair and probes the component
740 * (1) echo "mdev0:ch6:cdev:my_rxchannel" >add_link
741 * (2) echo "mdev1:ep81:cdev" >add_link
743 * (1) would create the device node /dev/my_rxchannel
744 * (2) would create the device node /dev/mdev1-ep81
746 static ssize_t add_link_store(struct device_driver *drv,
750 struct most_channel *c;
751 struct core_component *comp;
752 char buffer[STRING_SIZE];
757 char devnod_buf[STRING_SIZE];
759 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
761 strlcpy(buffer, buf, max_len);
762 ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, &comp_param);
765 comp = match_component(comp_name);
768 if (!comp_param || *comp_param == 0) {
769 snprintf(devnod_buf, sizeof(devnod_buf), "%s-%s", mdev,
771 comp_param = devnod_buf;
774 c = get_channel(mdev, mdev_ch);
778 ret = link_channel_to_component(c, comp, comp_param);
785 * remove_link_store - store function for remove_link attribute
786 * @drv: device driver
788 * @len: buffer length
791 * echo "mdev0:ep81" >remove_link
793 static ssize_t remove_link_store(struct device_driver *drv,
797 struct most_channel *c;
798 struct core_component *comp;
799 char buffer[STRING_SIZE];
804 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
806 strlcpy(buffer, buf, max_len);
807 ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, NULL);
810 comp = match_component(comp_name);
813 c = get_channel(mdev, mdev_ch);
817 if (comp->disconnect_channel(c->iface, c->channel_id))
819 if (c->pipe0.comp == comp)
820 c->pipe0.comp = NULL;
821 if (c->pipe1.comp == comp)
822 c->pipe1.comp = NULL;
826 #define DRV_ATTR(_name) (&driver_attr_##_name.attr)
828 static DRIVER_ATTR_RO(links);
829 static DRIVER_ATTR_RO(components);
830 static DRIVER_ATTR_WO(add_link);
831 static DRIVER_ATTR_WO(remove_link);
833 static struct attribute *mc_attrs[] = {
835 DRV_ATTR(components),
837 DRV_ATTR(remove_link),
841 static struct attribute_group mc_attr_group = {
845 static const struct attribute_group *mc_attr_groups[] = {
850 static int most_match(struct device *dev, struct device_driver *drv)
852 if (!strcmp(dev_name(dev), "most"))
858 static inline void trash_mbo(struct mbo *mbo)
861 struct most_channel *c = mbo->context;
863 spin_lock_irqsave(&c->fifo_lock, flags);
864 list_add(&mbo->list, &c->trash_fifo);
865 spin_unlock_irqrestore(&c->fifo_lock, flags);
868 static bool hdm_mbo_ready(struct most_channel *c)
875 spin_lock_irq(&c->fifo_lock);
876 empty = list_empty(&c->halt_fifo);
877 spin_unlock_irq(&c->fifo_lock);
882 static void nq_hdm_mbo(struct mbo *mbo)
885 struct most_channel *c = mbo->context;
887 spin_lock_irqsave(&c->fifo_lock, flags);
888 list_add_tail(&mbo->list, &c->halt_fifo);
889 spin_unlock_irqrestore(&c->fifo_lock, flags);
890 wake_up_interruptible(&c->hdm_fifo_wq);
893 static int hdm_enqueue_thread(void *data)
895 struct most_channel *c = data;
898 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
900 while (likely(!kthread_should_stop())) {
901 wait_event_interruptible(c->hdm_fifo_wq,
903 kthread_should_stop());
905 mutex_lock(&c->nq_mutex);
906 spin_lock_irq(&c->fifo_lock);
907 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
908 spin_unlock_irq(&c->fifo_lock);
909 mutex_unlock(&c->nq_mutex);
913 mbo = list_pop_mbo(&c->halt_fifo);
914 spin_unlock_irq(&c->fifo_lock);
916 if (c->cfg.direction == MOST_CH_RX)
917 mbo->buffer_length = c->cfg.buffer_size;
919 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
920 mutex_unlock(&c->nq_mutex);
923 pr_err("hdm enqueue failed\n");
925 c->hdm_enqueue_task = NULL;
933 static int run_enqueue_thread(struct most_channel *c, int channel_id)
935 struct task_struct *task =
936 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
940 return PTR_ERR(task);
942 c->hdm_enqueue_task = task;
947 * arm_mbo - recycle MBO for further usage
950 * This puts an MBO back to the list to have it ready for up coming
953 * In case the MBO belongs to a channel that recently has been
954 * poisoned, the MBO is scheduled to be trashed.
955 * Calls the completion handler of an attached component.
957 static void arm_mbo(struct mbo *mbo)
960 struct most_channel *c;
964 if (c->is_poisoned) {
969 spin_lock_irqsave(&c->fifo_lock, flags);
970 ++*mbo->num_buffers_ptr;
971 list_add_tail(&mbo->list, &c->fifo);
972 spin_unlock_irqrestore(&c->fifo_lock, flags);
974 if (c->pipe0.refs && c->pipe0.comp->tx_completion)
975 c->pipe0.comp->tx_completion(c->iface, c->channel_id);
977 if (c->pipe1.refs && c->pipe1.comp->tx_completion)
978 c->pipe1.comp->tx_completion(c->iface, c->channel_id);
982 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
983 * @c: pointer to interface channel
984 * @dir: direction of the channel
985 * @compl: pointer to completion function
987 * This allocates buffer objects including the containing DMA coherent
988 * buffer and puts them in the fifo.
989 * Buffers of Rx channels are put in the kthread fifo, hence immediately
990 * submitted to the HDM.
992 * Returns the number of allocated and enqueued MBOs.
994 static int arm_mbo_chain(struct most_channel *c, int dir,
995 void (*compl)(struct mbo *))
1000 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
1002 atomic_set(&c->mbo_nq_level, 0);
1004 for (i = 0; i < c->cfg.num_buffers; i++) {
1005 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
1010 mbo->ifp = c->iface;
1011 mbo->hdm_channel_id = c->channel_id;
1012 if (c->iface->dma_alloc) {
1014 c->iface->dma_alloc(mbo, coherent_buf_size);
1017 kzalloc(coherent_buf_size, GFP_KERNEL);
1019 if (!mbo->virt_address)
1022 mbo->complete = compl;
1023 mbo->num_buffers_ptr = &dummy_num_buffers;
1024 if (dir == MOST_CH_RX) {
1026 atomic_inc(&c->mbo_nq_level);
1028 spin_lock_irqsave(&c->fifo_lock, flags);
1029 list_add_tail(&mbo->list, &c->fifo);
1030 spin_unlock_irqrestore(&c->fifo_lock, flags);
1033 return c->cfg.num_buffers;
1039 flush_channel_fifos(c);
1044 * most_submit_mbo - submits an MBO to fifo
1047 void most_submit_mbo(struct mbo *mbo)
1049 if (WARN_ONCE(!mbo || !mbo->context,
1050 "bad mbo or missing channel reference\n"))
1055 EXPORT_SYMBOL_GPL(most_submit_mbo);
1058 * most_write_completion - write completion handler
1061 * This recycles the MBO for further usage. In case the channel has been
1062 * poisoned, the MBO is scheduled to be trashed.
1064 static void most_write_completion(struct mbo *mbo)
1066 struct most_channel *c;
1069 if (mbo->status == MBO_E_INVAL)
1070 pr_info("WARN: Tx MBO status: invalid\n");
1071 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1077 int channel_has_mbo(struct most_interface *iface, int id,
1078 struct core_component *comp)
1080 struct most_channel *c = iface->p->channel[id];
1081 unsigned long flags;
1087 if (c->pipe0.refs && c->pipe1.refs &&
1088 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1089 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1092 spin_lock_irqsave(&c->fifo_lock, flags);
1093 empty = list_empty(&c->fifo);
1094 spin_unlock_irqrestore(&c->fifo_lock, flags);
1097 EXPORT_SYMBOL_GPL(channel_has_mbo);
1100 * most_get_mbo - get pointer to an MBO of pool
1101 * @iface: pointer to interface instance
1103 * @comp: driver component
1105 * This attempts to get a free buffer out of the channel fifo.
1106 * Returns a pointer to MBO on success or NULL otherwise.
1108 struct mbo *most_get_mbo(struct most_interface *iface, int id,
1109 struct core_component *comp)
1112 struct most_channel *c;
1113 unsigned long flags;
1114 int *num_buffers_ptr;
1116 c = iface->p->channel[id];
1120 if (c->pipe0.refs && c->pipe1.refs &&
1121 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1122 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1125 if (comp == c->pipe0.comp)
1126 num_buffers_ptr = &c->pipe0.num_buffers;
1127 else if (comp == c->pipe1.comp)
1128 num_buffers_ptr = &c->pipe1.num_buffers;
1130 num_buffers_ptr = &dummy_num_buffers;
1132 spin_lock_irqsave(&c->fifo_lock, flags);
1133 if (list_empty(&c->fifo)) {
1134 spin_unlock_irqrestore(&c->fifo_lock, flags);
1137 mbo = list_pop_mbo(&c->fifo);
1139 spin_unlock_irqrestore(&c->fifo_lock, flags);
1141 mbo->num_buffers_ptr = num_buffers_ptr;
1142 mbo->buffer_length = c->cfg.buffer_size;
1145 EXPORT_SYMBOL_GPL(most_get_mbo);
1148 * most_put_mbo - return buffer to pool
1151 void most_put_mbo(struct mbo *mbo)
1153 struct most_channel *c = mbo->context;
1155 if (c->cfg.direction == MOST_CH_TX) {
1160 atomic_inc(&c->mbo_nq_level);
1162 EXPORT_SYMBOL_GPL(most_put_mbo);
1165 * most_read_completion - read completion handler
1168 * This function is called by the HDM when data has been received from the
1169 * hardware and copied to the buffer of the MBO.
1171 * In case the channel has been poisoned it puts the buffer in the trash queue.
1172 * Otherwise, it passes the buffer to an component for further processing.
1174 static void most_read_completion(struct mbo *mbo)
1176 struct most_channel *c = mbo->context;
1178 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1183 if (mbo->status == MBO_E_INVAL) {
1185 atomic_inc(&c->mbo_nq_level);
1189 if (atomic_sub_and_test(1, &c->mbo_nq_level))
1192 if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1193 c->pipe0.comp->rx_completion(mbo) == 0)
1196 if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1197 c->pipe1.comp->rx_completion(mbo) == 0)
1204 * most_start_channel - prepares a channel for communication
1205 * @iface: pointer to interface instance
1207 * @comp: driver component
1209 * This prepares the channel for usage. Cross-checks whether the
1210 * channel's been properly configured.
1212 * Returns 0 on success or error code otherwise.
1214 int most_start_channel(struct most_interface *iface, int id,
1215 struct core_component *comp)
1219 struct most_channel *c = iface->p->channel[id];
1224 mutex_lock(&c->start_mutex);
1225 if (c->pipe0.refs + c->pipe1.refs > 0)
1226 goto out; /* already started by another component */
1228 if (!try_module_get(iface->mod)) {
1229 pr_info("failed to acquire HDM lock\n");
1230 mutex_unlock(&c->start_mutex);
1234 c->cfg.extra_len = 0;
1235 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1236 pr_info("channel configuration failed. Go check settings...\n");
1238 goto err_put_module;
1241 init_waitqueue_head(&c->hdm_fifo_wq);
1243 if (c->cfg.direction == MOST_CH_RX)
1244 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1245 most_read_completion);
1247 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1248 most_write_completion);
1249 if (unlikely(!num_buffer)) {
1251 goto err_put_module;
1254 ret = run_enqueue_thread(c, id);
1256 goto err_put_module;
1259 c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1260 c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
1261 atomic_set(&c->mbo_ref, num_buffer);
1264 if (comp == c->pipe0.comp)
1266 if (comp == c->pipe1.comp)
1268 mutex_unlock(&c->start_mutex);
1272 module_put(iface->mod);
1273 mutex_unlock(&c->start_mutex);
1276 EXPORT_SYMBOL_GPL(most_start_channel);
1279 * most_stop_channel - stops a running channel
1280 * @iface: pointer to interface instance
1282 * @comp: driver component
1284 int most_stop_channel(struct most_interface *iface, int id,
1285 struct core_component *comp)
1287 struct most_channel *c;
1289 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1290 pr_err("Bad interface or index out of range\n");
1293 c = iface->p->channel[id];
1297 mutex_lock(&c->start_mutex);
1298 if (c->pipe0.refs + c->pipe1.refs >= 2)
1301 if (c->hdm_enqueue_task)
1302 kthread_stop(c->hdm_enqueue_task);
1303 c->hdm_enqueue_task = NULL;
1306 module_put(iface->mod);
1308 c->is_poisoned = true;
1309 if (c->iface->poison_channel(c->iface, c->channel_id)) {
1310 pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1311 c->iface->description);
1312 mutex_unlock(&c->start_mutex);
1315 flush_trash_fifo(c);
1316 flush_channel_fifos(c);
1318 #ifdef CMPL_INTERRUPTIBLE
1319 if (wait_for_completion_interruptible(&c->cleanup)) {
1320 pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1321 mutex_unlock(&c->start_mutex);
1325 wait_for_completion(&c->cleanup);
1327 c->is_poisoned = false;
1330 if (comp == c->pipe0.comp)
1332 if (comp == c->pipe1.comp)
1334 mutex_unlock(&c->start_mutex);
1337 EXPORT_SYMBOL_GPL(most_stop_channel);
1340 * most_register_component - registers a driver component with the core
1341 * @comp: driver component
1343 int most_register_component(struct core_component *comp)
1346 pr_err("Bad component\n");
1349 list_add_tail(&comp->list, &mc.comp_list);
1350 pr_info("registered new core component %s\n", comp->name);
1353 EXPORT_SYMBOL_GPL(most_register_component);
1355 static int disconnect_channels(struct device *dev, void *data)
1357 struct most_interface *iface;
1358 struct most_channel *c, *tmp;
1359 struct core_component *comp = data;
1361 iface = to_most_interface(dev);
1362 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
1363 if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1364 comp->disconnect_channel(c->iface, c->channel_id);
1365 if (c->pipe0.comp == comp)
1366 c->pipe0.comp = NULL;
1367 if (c->pipe1.comp == comp)
1368 c->pipe1.comp = NULL;
1374 * most_deregister_component - deregisters a driver component with the core
1375 * @comp: driver component
1377 int most_deregister_component(struct core_component *comp)
1380 pr_err("Bad component\n");
1384 bus_for_each_dev(&mc.bus, NULL, comp, disconnect_channels);
1385 list_del(&comp->list);
1386 pr_info("deregistering component %s\n", comp->name);
1389 EXPORT_SYMBOL_GPL(most_deregister_component);
1391 static void release_interface(struct device *dev)
1393 pr_info("releasing interface dev %s...\n", dev_name(dev));
1396 static void release_channel(struct device *dev)
1398 pr_info("releasing channel dev %s...\n", dev_name(dev));
1402 * most_register_interface - registers an interface with core
1403 * @iface: device interface
1405 * Allocates and initializes a new interface instance and all of its channels.
1406 * Returns a pointer to kobject or an error pointer.
1408 int most_register_interface(struct most_interface *iface)
1412 struct most_channel *c;
1414 if (!iface || !iface->enqueue || !iface->configure ||
1415 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1416 pr_err("Bad interface or channel overflow\n");
1420 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1422 pr_info("Failed to alloc mdev ID\n");
1426 iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1428 ida_simple_remove(&mdev_id, id);
1432 INIT_LIST_HEAD(&iface->p->channel_list);
1433 iface->p->dev_id = id;
1434 snprintf(iface->p->name, STRING_SIZE, "mdev%d", id);
1435 iface->dev.init_name = iface->p->name;
1436 iface->dev.bus = &mc.bus;
1437 iface->dev.parent = &mc.dev;
1438 iface->dev.groups = interface_attr_groups;
1439 iface->dev.release = release_interface;
1440 if (device_register(&iface->dev)) {
1441 pr_err("registering iface->dev failed\n");
1443 ida_simple_remove(&mdev_id, id);
1447 for (i = 0; i < iface->num_channels; i++) {
1448 const char *name_suffix = iface->channel_vector[i].name_suffix;
1450 c = kzalloc(sizeof(*c), GFP_KERNEL);
1452 goto err_free_resources;
1454 snprintf(c->name, STRING_SIZE, "ch%d", i);
1456 snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1457 c->dev.init_name = c->name;
1458 c->dev.parent = &iface->dev;
1459 c->dev.groups = channel_attr_groups;
1460 c->dev.release = release_channel;
1461 iface->p->channel[i] = c;
1465 c->keep_mbo = false;
1466 c->enqueue_halt = false;
1467 c->is_poisoned = false;
1468 c->cfg.direction = 0;
1469 c->cfg.data_type = 0;
1470 c->cfg.num_buffers = 0;
1471 c->cfg.buffer_size = 0;
1472 c->cfg.subbuffer_size = 0;
1473 c->cfg.packets_per_xact = 0;
1474 spin_lock_init(&c->fifo_lock);
1475 INIT_LIST_HEAD(&c->fifo);
1476 INIT_LIST_HEAD(&c->trash_fifo);
1477 INIT_LIST_HEAD(&c->halt_fifo);
1478 init_completion(&c->cleanup);
1479 atomic_set(&c->mbo_ref, 0);
1480 mutex_init(&c->start_mutex);
1481 mutex_init(&c->nq_mutex);
1482 list_add_tail(&c->list, &iface->p->channel_list);
1483 if (device_register(&c->dev)) {
1484 pr_err("registering c->dev failed\n");
1485 goto err_free_most_channel;
1488 pr_info("registered new device mdev%d (%s)\n",
1489 id, iface->description);
1492 err_free_most_channel:
1497 c = iface->p->channel[--i];
1498 device_unregister(&c->dev);
1502 device_unregister(&iface->dev);
1503 ida_simple_remove(&mdev_id, id);
1506 EXPORT_SYMBOL_GPL(most_register_interface);
1509 * most_deregister_interface - deregisters an interface with core
1510 * @iface: device interface
1512 * Before removing an interface instance from the list, all running
1513 * channels are stopped and poisoned.
1515 void most_deregister_interface(struct most_interface *iface)
1518 struct most_channel *c;
1520 pr_info("deregistering device %s (%s)\n", dev_name(&iface->dev),
1521 iface->description);
1522 for (i = 0; i < iface->num_channels; i++) {
1523 c = iface->p->channel[i];
1525 c->pipe0.comp->disconnect_channel(c->iface,
1528 c->pipe1.comp->disconnect_channel(c->iface,
1530 c->pipe0.comp = NULL;
1531 c->pipe1.comp = NULL;
1533 device_unregister(&c->dev);
1537 ida_simple_remove(&mdev_id, iface->p->dev_id);
1539 device_unregister(&iface->dev);
1541 EXPORT_SYMBOL_GPL(most_deregister_interface);
1544 * most_stop_enqueue - prevents core from enqueueing MBOs
1545 * @iface: pointer to interface
1548 * This is called by an HDM that _cannot_ attend to its duties and
1549 * is imminent to get run over by the core. The core is not going to
1550 * enqueue any further packets unless the flagging HDM calls
1551 * most_resume enqueue().
1553 void most_stop_enqueue(struct most_interface *iface, int id)
1555 struct most_channel *c = iface->p->channel[id];
1560 mutex_lock(&c->nq_mutex);
1561 c->enqueue_halt = true;
1562 mutex_unlock(&c->nq_mutex);
1564 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1567 * most_resume_enqueue - allow core to enqueue MBOs again
1568 * @iface: pointer to interface
1571 * This clears the enqueue halt flag and enqueues all MBOs currently
1572 * sitting in the wait fifo.
1574 void most_resume_enqueue(struct most_interface *iface, int id)
1576 struct most_channel *c = iface->p->channel[id];
1581 mutex_lock(&c->nq_mutex);
1582 c->enqueue_halt = false;
1583 mutex_unlock(&c->nq_mutex);
1585 wake_up_interruptible(&c->hdm_fifo_wq);
1587 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1589 static void release_most_sub(struct device *dev)
1591 pr_info("releasing most_subsystem\n");
1594 static int __init most_init(void)
1598 pr_info("init()\n");
1599 INIT_LIST_HEAD(&mc.comp_list);
1602 mc.bus.name = "most",
1603 mc.bus.match = most_match,
1604 mc.drv.name = "most_core",
1605 mc.drv.bus = &mc.bus,
1606 mc.drv.groups = mc_attr_groups;
1608 err = bus_register(&mc.bus);
1610 pr_info("Cannot register most bus\n");
1613 err = driver_register(&mc.drv);
1615 pr_info("Cannot register core driver\n");
1616 goto err_unregister_bus;
1618 mc.dev.init_name = "most_bus";
1619 mc.dev.release = release_most_sub;
1620 if (device_register(&mc.dev)) {
1622 goto err_unregister_driver;
1627 err_unregister_driver:
1628 driver_unregister(&mc.drv);
1630 bus_unregister(&mc.bus);
1634 static void __exit most_exit(void)
1636 pr_info("exit core module\n");
1637 device_unregister(&mc.dev);
1638 driver_unregister(&mc.drv);
1639 bus_unregister(&mc.bus);
1640 ida_destroy(&mdev_id);
1643 module_init(most_init);
1644 module_exit(most_exit);
1645 MODULE_LICENSE("GPL");
1646 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1647 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");