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;
37 struct list_head comp_list;
40 #define to_driver(d) container_of(d, struct mostcore, drv);
43 struct core_component *comp;
50 struct completion cleanup;
52 atomic_t mbo_nq_level;
54 char name[STRING_SIZE];
56 struct mutex start_mutex;
57 struct mutex nq_mutex; /* nq thread synchronization */
59 struct most_interface *iface;
60 struct most_channel_config cfg;
63 struct list_head fifo;
65 struct list_head halt_fifo;
66 struct list_head list;
69 struct list_head trash_fifo;
70 struct task_struct *hdm_enqueue_task;
71 wait_queue_head_t hdm_fifo_wq;
75 #define to_channel(d) container_of(d, struct most_channel, dev)
77 struct interface_private {
79 char name[STRING_SIZE];
80 struct most_channel *channel[MAX_CHANNELS];
81 struct list_head channel_list;
85 int most_ch_data_type;
88 { MOST_CH_CONTROL, "control\n" },
89 { MOST_CH_ASYNC, "async\n" },
90 { MOST_CH_SYNC, "sync\n" },
91 { MOST_CH_ISOC, "isoc\n"},
92 { MOST_CH_ISOC, "isoc_avp\n"},
96 * list_pop_mbo - retrieves the first MBO of the list and removes it
97 * @ptr: the list head to grab the MBO from.
99 #define list_pop_mbo(ptr) \
101 struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
102 list_del(&_mbo->list); \
107 * most_free_mbo_coherent - free an MBO and its coherent buffer
110 static void most_free_mbo_coherent(struct mbo *mbo)
112 struct most_channel *c = mbo->context;
113 u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
115 dma_free_coherent(NULL, coherent_buf_size, mbo->virt_address,
118 if (atomic_sub_and_test(1, &c->mbo_ref))
119 complete(&c->cleanup);
123 * flush_channel_fifos - clear the channel fifos
124 * @c: pointer to channel object
126 static void flush_channel_fifos(struct most_channel *c)
128 unsigned long flags, hf_flags;
129 struct mbo *mbo, *tmp;
131 if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
134 spin_lock_irqsave(&c->fifo_lock, flags);
135 list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
136 list_del(&mbo->list);
137 spin_unlock_irqrestore(&c->fifo_lock, flags);
138 most_free_mbo_coherent(mbo);
139 spin_lock_irqsave(&c->fifo_lock, flags);
141 spin_unlock_irqrestore(&c->fifo_lock, flags);
143 spin_lock_irqsave(&c->fifo_lock, hf_flags);
144 list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
145 list_del(&mbo->list);
146 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
147 most_free_mbo_coherent(mbo);
148 spin_lock_irqsave(&c->fifo_lock, hf_flags);
150 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
152 if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
153 pr_info("WARN: fifo | trash fifo not empty\n");
157 * flush_trash_fifo - clear the trash fifo
158 * @c: pointer to channel object
160 static int flush_trash_fifo(struct most_channel *c)
162 struct mbo *mbo, *tmp;
165 spin_lock_irqsave(&c->fifo_lock, flags);
166 list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
167 list_del(&mbo->list);
168 spin_unlock_irqrestore(&c->fifo_lock, flags);
169 most_free_mbo_coherent(mbo);
170 spin_lock_irqsave(&c->fifo_lock, flags);
172 spin_unlock_irqrestore(&c->fifo_lock, flags);
176 static ssize_t available_directions_show(struct device *dev,
177 struct device_attribute *attr,
180 struct most_channel *c = to_channel(dev);
181 unsigned int i = c->channel_id;
184 if (c->iface->channel_vector[i].direction & MOST_CH_RX)
186 if (c->iface->channel_vector[i].direction & MOST_CH_TX)
192 static ssize_t available_datatypes_show(struct device *dev,
193 struct device_attribute *attr,
196 struct most_channel *c = to_channel(dev);
197 unsigned int i = c->channel_id;
200 if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
201 strcat(buf, "control ");
202 if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
203 strcat(buf, "async ");
204 if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
205 strcat(buf, "sync ");
206 if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
207 strcat(buf, "isoc ");
212 static ssize_t number_of_packet_buffers_show(struct device *dev,
213 struct device_attribute *attr,
216 struct most_channel *c = to_channel(dev);
217 unsigned int i = c->channel_id;
219 return snprintf(buf, PAGE_SIZE, "%d\n",
220 c->iface->channel_vector[i].num_buffers_packet);
223 static ssize_t number_of_stream_buffers_show(struct device *dev,
224 struct device_attribute *attr,
227 struct most_channel *c = to_channel(dev);
228 unsigned int i = c->channel_id;
230 return snprintf(buf, PAGE_SIZE, "%d\n",
231 c->iface->channel_vector[i].num_buffers_streaming);
234 static ssize_t size_of_packet_buffer_show(struct device *dev,
235 struct device_attribute *attr,
238 struct most_channel *c = to_channel(dev);
239 unsigned int i = c->channel_id;
241 return snprintf(buf, PAGE_SIZE, "%d\n",
242 c->iface->channel_vector[i].buffer_size_packet);
245 static ssize_t size_of_stream_buffer_show(struct device *dev,
246 struct device_attribute *attr,
249 struct most_channel *c = to_channel(dev);
250 unsigned int i = c->channel_id;
252 return snprintf(buf, PAGE_SIZE, "%d\n",
253 c->iface->channel_vector[i].buffer_size_streaming);
256 static ssize_t channel_starving_show(struct device *dev,
257 struct device_attribute *attr,
260 struct most_channel *c = to_channel(dev);
262 return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
265 static ssize_t set_number_of_buffers_show(struct device *dev,
266 struct device_attribute *attr,
269 struct most_channel *c = to_channel(dev);
271 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
274 static ssize_t set_number_of_buffers_store(struct device *dev,
275 struct device_attribute *attr,
279 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, 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 #define DEV_ATTR(_name) (&dev_attr_##_name.attr)
427 static DEVICE_ATTR_RO(available_directions);
428 static DEVICE_ATTR_RO(available_datatypes);
429 static DEVICE_ATTR_RO(number_of_packet_buffers);
430 static DEVICE_ATTR_RO(number_of_stream_buffers);
431 static DEVICE_ATTR_RO(size_of_stream_buffer);
432 static DEVICE_ATTR_RO(size_of_packet_buffer);
433 static DEVICE_ATTR_RO(channel_starving);
434 static DEVICE_ATTR_RW(set_buffer_size);
435 static DEVICE_ATTR_RW(set_number_of_buffers);
436 static DEVICE_ATTR_RW(set_direction);
437 static DEVICE_ATTR_RW(set_datatype);
438 static DEVICE_ATTR_RW(set_subbuffer_size);
439 static DEVICE_ATTR_RW(set_packets_per_xact);
441 static struct attribute *channel_attrs[] = {
442 DEV_ATTR(available_directions),
443 DEV_ATTR(available_datatypes),
444 DEV_ATTR(number_of_packet_buffers),
445 DEV_ATTR(number_of_stream_buffers),
446 DEV_ATTR(size_of_stream_buffer),
447 DEV_ATTR(size_of_packet_buffer),
448 DEV_ATTR(channel_starving),
449 DEV_ATTR(set_buffer_size),
450 DEV_ATTR(set_number_of_buffers),
451 DEV_ATTR(set_direction),
452 DEV_ATTR(set_datatype),
453 DEV_ATTR(set_subbuffer_size),
454 DEV_ATTR(set_packets_per_xact),
458 static struct attribute_group channel_attr_group = {
459 .attrs = channel_attrs,
462 static const struct attribute_group *channel_attr_groups[] = {
467 static ssize_t description_show(struct device *dev,
468 struct device_attribute *attr,
471 struct most_interface *iface = to_most_interface(dev);
473 return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
476 static ssize_t interface_show(struct device *dev,
477 struct device_attribute *attr,
480 struct most_interface *iface = to_most_interface(dev);
482 switch (iface->interface) {
484 return snprintf(buf, PAGE_SIZE, "loopback\n");
486 return snprintf(buf, PAGE_SIZE, "i2c\n");
488 return snprintf(buf, PAGE_SIZE, "i2s\n");
490 return snprintf(buf, PAGE_SIZE, "tsi\n");
492 return snprintf(buf, PAGE_SIZE, "hbi\n");
493 case ITYPE_MEDIALB_DIM:
494 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
495 case ITYPE_MEDIALB_DIM2:
496 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
498 return snprintf(buf, PAGE_SIZE, "usb\n");
500 return snprintf(buf, PAGE_SIZE, "pcie\n");
502 return snprintf(buf, PAGE_SIZE, "unknown\n");
505 static DEVICE_ATTR_RO(description);
506 static DEVICE_ATTR_RO(interface);
508 static struct attribute *interface_attrs[] = {
509 DEV_ATTR(description),
514 static struct attribute_group interface_attr_group = {
515 .attrs = interface_attrs,
518 static const struct attribute_group *interface_attr_groups[] = {
519 &interface_attr_group,
523 static struct core_component *match_module(char *name)
525 struct core_component *comp;
527 list_for_each_entry(comp, &mc.comp_list, list) {
528 if (!strcmp(comp->name, name))
534 int print_links(struct device *dev, void *data)
538 struct most_channel *c;
539 struct most_interface *iface = to_most_interface(dev);
541 list_for_each_entry(c, &iface->p->channel_list, list) {
543 offs += snprintf(buf + offs,
547 dev_name(&iface->dev),
551 offs += snprintf(buf + offs,
555 dev_name(&iface->dev),
562 static ssize_t links_show(struct device_driver *drv, char *buf)
564 bus_for_each_dev(&mc.bus, NULL, buf, print_links);
568 static ssize_t modules_show(struct device_driver *drv, char *buf)
570 struct core_component *comp;
573 list_for_each_entry(comp, &mc.comp_list, list) {
574 offs += snprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
580 * split_string - parses buf and extracts ':' separated substrings.
582 * @buf: complete string from attribute 'add_channel'
583 * @a: storage for 1st substring (=interface name)
584 * @b: storage for 2nd substring (=channel name)
585 * @c: storage for 3rd substring (=component name)
586 * @d: storage optional 4th substring (=user defined name)
590 * Input: "mdev0:ch6:cdev:my_channel\n" or
591 * "mdev0:ch6:cdev:my_channel"
593 * Output: *a -> "mdev0", *b -> "ch6", *c -> "cdev" *d -> "my_channel"
595 * Input: "mdev1:ep81:cdev\n"
596 * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d -> ""
598 * Input: "mdev1:ep81"
599 * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d == NULL
601 static int split_string(char *buf, char **a, char **b, char **c, char **d)
603 *a = strsep(&buf, ":");
607 *b = strsep(&buf, ":\n");
611 *c = strsep(&buf, ":\n");
616 *d = strsep(&buf, ":\n");
621 static int match_bus_dev(struct device *dev, void *data)
623 char *mdev_name = data;
625 return !strcmp(dev_name(dev), mdev_name);
629 * get_channel - get pointer to channel
630 * @mdev: name of the device interface
631 * @mdev_ch: name of channel
633 static struct most_channel *get_channel(char *mdev, char *mdev_ch)
635 struct device *dev = NULL;
636 struct most_interface *iface;
637 struct most_channel *c, *tmp;
639 dev = bus_find_device(&mc.bus, NULL, mdev, match_bus_dev);
642 iface = to_most_interface(dev);
643 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
644 if (!strcmp(dev_name(&c->dev), mdev_ch))
651 inline int link_channel_to_component(struct most_channel *c,
652 struct core_component *comp,
656 struct core_component **comp_ptr;
659 comp_ptr = &c->pipe0.comp;
660 else if (!c->pipe1.comp)
661 comp_ptr = &c->pipe1.comp;
666 ret = comp->probe_channel(c->iface, c->channel_id,
667 &c->cfg, comp_param);
677 * add_link_store - store function for add_link attribute
678 * @drv: device driver
680 * @len: buffer length
682 * This parses the string given by buf and splits it into
683 * four substrings. Note: last substring is optional. In case a cdev
684 * component is loaded the optional 4th substring will make up the name of
685 * device node in the /dev directory. If omitted, the device node will
686 * inherit the channel's name within sysfs.
688 * Searches for (device, channel) pair and probes the component
691 * (1) echo "mdev0:ch6:cdev:my_rxchannel" >add_link
692 * (2) echo "mdev1:ep81:cdev" >add_link
694 * (1) would create the device node /dev/my_rxchannel
695 * (2) would create the device node /dev/mdev1-ep81
697 static ssize_t add_link_store(struct device_driver *drv,
701 struct most_channel *c;
702 struct core_component *comp;
703 char buffer[STRING_SIZE];
708 char devnod_buf[STRING_SIZE];
710 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
712 strlcpy(buffer, buf, max_len);
714 ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, &comp_param);
717 comp = match_module(comp_name);
718 if (!comp_param || *comp_param == 0) {
719 snprintf(devnod_buf, sizeof(devnod_buf), "%s-%s", mdev,
721 comp_param = devnod_buf;
724 c = get_channel(mdev, mdev_ch);
728 ret = link_channel_to_component(c, comp, comp_param);
736 * remove_link_store - store function for remove_link attribute
737 * @drv: device driver
739 * @len: buffer length
742 * echo "mdev0:ep81" >remove_link
744 static ssize_t remove_link_store(struct device_driver *drv,
748 struct most_channel *c;
749 struct core_component *comp;
750 char buffer[STRING_SIZE];
755 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
757 strlcpy(buffer, buf, max_len);
758 ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, NULL);
761 comp = match_module(comp_name);
762 c = get_channel(mdev, mdev_ch);
766 if (comp->disconnect_channel(c->iface, c->channel_id))
768 if (c->pipe0.comp == comp)
769 c->pipe0.comp = NULL;
770 if (c->pipe1.comp == comp)
771 c->pipe1.comp = NULL;
775 #define DRV_ATTR(_name) (&driver_attr_##_name.attr)
777 static DRIVER_ATTR_RO(links);
778 static DRIVER_ATTR_RO(modules);
779 static DRIVER_ATTR_WO(add_link);
780 static DRIVER_ATTR_WO(remove_link);
782 static struct attribute *module_attrs[] = {
786 DRV_ATTR(remove_link),
790 static struct attribute_group module_attr_group = {
791 .attrs = module_attrs,
794 static const struct attribute_group *module_attr_groups[] = {
799 int most_match(struct device *dev, struct device_driver *drv)
801 if (!strcmp(dev_name(dev), "most"))
807 static inline void trash_mbo(struct mbo *mbo)
810 struct most_channel *c = mbo->context;
812 spin_lock_irqsave(&c->fifo_lock, flags);
813 list_add(&mbo->list, &c->trash_fifo);
814 spin_unlock_irqrestore(&c->fifo_lock, flags);
817 static bool hdm_mbo_ready(struct most_channel *c)
824 spin_lock_irq(&c->fifo_lock);
825 empty = list_empty(&c->halt_fifo);
826 spin_unlock_irq(&c->fifo_lock);
831 static void nq_hdm_mbo(struct mbo *mbo)
834 struct most_channel *c = mbo->context;
836 spin_lock_irqsave(&c->fifo_lock, flags);
837 list_add_tail(&mbo->list, &c->halt_fifo);
838 spin_unlock_irqrestore(&c->fifo_lock, flags);
839 wake_up_interruptible(&c->hdm_fifo_wq);
842 static int hdm_enqueue_thread(void *data)
844 struct most_channel *c = data;
847 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
849 while (likely(!kthread_should_stop())) {
850 wait_event_interruptible(c->hdm_fifo_wq,
852 kthread_should_stop());
854 mutex_lock(&c->nq_mutex);
855 spin_lock_irq(&c->fifo_lock);
856 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
857 spin_unlock_irq(&c->fifo_lock);
858 mutex_unlock(&c->nq_mutex);
862 mbo = list_pop_mbo(&c->halt_fifo);
863 spin_unlock_irq(&c->fifo_lock);
865 if (c->cfg.direction == MOST_CH_RX)
866 mbo->buffer_length = c->cfg.buffer_size;
868 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
869 mutex_unlock(&c->nq_mutex);
872 pr_err("hdm enqueue failed\n");
874 c->hdm_enqueue_task = NULL;
882 static int run_enqueue_thread(struct most_channel *c, int channel_id)
884 struct task_struct *task =
885 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
889 return PTR_ERR(task);
891 c->hdm_enqueue_task = task;
896 * arm_mbo - recycle MBO for further usage
899 * This puts an MBO back to the list to have it ready for up coming
902 * In case the MBO belongs to a channel that recently has been
903 * poisoned, the MBO is scheduled to be trashed.
904 * Calls the completion handler of an attached component.
906 static void arm_mbo(struct mbo *mbo)
909 struct most_channel *c;
911 BUG_ON((!mbo) || (!mbo->context));
914 if (c->is_poisoned) {
919 spin_lock_irqsave(&c->fifo_lock, flags);
920 ++*mbo->num_buffers_ptr;
921 list_add_tail(&mbo->list, &c->fifo);
922 spin_unlock_irqrestore(&c->fifo_lock, flags);
924 if (c->pipe0.refs && c->pipe0.comp->tx_completion)
925 c->pipe0.comp->tx_completion(c->iface, c->channel_id);
927 if (c->pipe1.refs && c->pipe1.comp->tx_completion)
928 c->pipe1.comp->tx_completion(c->iface, c->channel_id);
932 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
933 * @c: pointer to interface channel
934 * @dir: direction of the channel
935 * @compl: pointer to completion function
937 * This allocates buffer objects including the containing DMA coherent
938 * buffer and puts them in the fifo.
939 * Buffers of Rx channels are put in the kthread fifo, hence immediately
940 * submitted to the HDM.
942 * Returns the number of allocated and enqueued MBOs.
944 static int arm_mbo_chain(struct most_channel *c, int dir,
945 void (*compl)(struct mbo *))
950 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
952 atomic_set(&c->mbo_nq_level, 0);
954 for (i = 0; i < c->cfg.num_buffers; i++) {
955 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
962 mbo->hdm_channel_id = c->channel_id;
963 mbo->virt_address = dma_alloc_coherent(NULL,
967 if (!mbo->virt_address) {
968 pr_info("WARN: No DMA coherent buffer.\n");
972 mbo->complete = compl;
973 mbo->num_buffers_ptr = &dummy_num_buffers;
974 if (dir == MOST_CH_RX) {
976 atomic_inc(&c->mbo_nq_level);
990 * most_submit_mbo - submits an MBO to fifo
993 void most_submit_mbo(struct mbo *mbo)
995 if (WARN_ONCE(!mbo || !mbo->context,
996 "bad mbo or missing channel reference\n"))
1001 EXPORT_SYMBOL_GPL(most_submit_mbo);
1004 * most_write_completion - write completion handler
1007 * This recycles the MBO for further usage. In case the channel has been
1008 * poisoned, the MBO is scheduled to be trashed.
1010 static void most_write_completion(struct mbo *mbo)
1012 struct most_channel *c;
1014 BUG_ON((!mbo) || (!mbo->context));
1017 if (mbo->status == MBO_E_INVAL)
1018 pr_info("WARN: Tx MBO status: invalid\n");
1019 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1025 int channel_has_mbo(struct most_interface *iface, int id,
1026 struct core_component *comp)
1028 struct most_channel *c = iface->p->channel[id];
1029 unsigned long flags;
1035 if (c->pipe0.refs && c->pipe1.refs &&
1036 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1037 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1040 spin_lock_irqsave(&c->fifo_lock, flags);
1041 empty = list_empty(&c->fifo);
1042 spin_unlock_irqrestore(&c->fifo_lock, flags);
1045 EXPORT_SYMBOL_GPL(channel_has_mbo);
1048 * most_get_mbo - get pointer to an MBO of pool
1049 * @iface: pointer to interface instance
1051 * @comp: driver component
1053 * This attempts to get a free buffer out of the channel fifo.
1054 * Returns a pointer to MBO on success or NULL otherwise.
1056 struct mbo *most_get_mbo(struct most_interface *iface, int id,
1057 struct core_component *comp)
1060 struct most_channel *c;
1061 unsigned long flags;
1062 int *num_buffers_ptr;
1064 c = iface->p->channel[id];
1068 if (c->pipe0.refs && c->pipe1.refs &&
1069 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1070 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1073 if (comp == c->pipe0.comp)
1074 num_buffers_ptr = &c->pipe0.num_buffers;
1075 else if (comp == c->pipe1.comp)
1076 num_buffers_ptr = &c->pipe1.num_buffers;
1078 num_buffers_ptr = &dummy_num_buffers;
1080 spin_lock_irqsave(&c->fifo_lock, flags);
1081 if (list_empty(&c->fifo)) {
1082 spin_unlock_irqrestore(&c->fifo_lock, flags);
1085 mbo = list_pop_mbo(&c->fifo);
1087 spin_unlock_irqrestore(&c->fifo_lock, flags);
1089 mbo->num_buffers_ptr = num_buffers_ptr;
1090 mbo->buffer_length = c->cfg.buffer_size;
1093 EXPORT_SYMBOL_GPL(most_get_mbo);
1096 * most_put_mbo - return buffer to pool
1099 void most_put_mbo(struct mbo *mbo)
1101 struct most_channel *c = mbo->context;
1103 if (c->cfg.direction == MOST_CH_TX) {
1108 atomic_inc(&c->mbo_nq_level);
1110 EXPORT_SYMBOL_GPL(most_put_mbo);
1113 * most_read_completion - read completion handler
1116 * This function is called by the HDM when data has been received from the
1117 * hardware and copied to the buffer of the MBO.
1119 * In case the channel has been poisoned it puts the buffer in the trash queue.
1120 * Otherwise, it passes the buffer to an component for further processing.
1122 static void most_read_completion(struct mbo *mbo)
1124 struct most_channel *c = mbo->context;
1126 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1131 if (mbo->status == MBO_E_INVAL) {
1133 atomic_inc(&c->mbo_nq_level);
1137 if (atomic_sub_and_test(1, &c->mbo_nq_level))
1140 if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1141 c->pipe0.comp->rx_completion(mbo) == 0)
1144 if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1145 c->pipe1.comp->rx_completion(mbo) == 0)
1152 * most_start_channel - prepares a channel for communication
1153 * @iface: pointer to interface instance
1155 * @comp: driver component
1157 * This prepares the channel for usage. Cross-checks whether the
1158 * channel's been properly configured.
1160 * Returns 0 on success or error code otherwise.
1162 int most_start_channel(struct most_interface *iface, int id,
1163 struct core_component *comp)
1167 struct most_channel *c = iface->p->channel[id];
1172 mutex_lock(&c->start_mutex);
1173 if (c->pipe0.refs + c->pipe1.refs > 0)
1174 goto out; /* already started by another component */
1176 if (!try_module_get(iface->mod)) {
1177 pr_info("failed to acquire HDM lock\n");
1178 mutex_unlock(&c->start_mutex);
1182 c->cfg.extra_len = 0;
1183 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1184 pr_info("channel configuration failed. Go check settings...\n");
1189 init_waitqueue_head(&c->hdm_fifo_wq);
1191 if (c->cfg.direction == MOST_CH_RX)
1192 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1193 most_read_completion);
1195 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1196 most_write_completion);
1197 if (unlikely(!num_buffer)) {
1198 pr_info("failed to allocate memory\n");
1203 ret = run_enqueue_thread(c, id);
1208 c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1209 c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
1210 atomic_set(&c->mbo_ref, num_buffer);
1213 if (comp == c->pipe0.comp)
1215 if (comp == c->pipe1.comp)
1217 mutex_unlock(&c->start_mutex);
1221 module_put(iface->mod);
1222 mutex_unlock(&c->start_mutex);
1225 EXPORT_SYMBOL_GPL(most_start_channel);
1228 * most_stop_channel - stops a running channel
1229 * @iface: pointer to interface instance
1231 * @comp: driver component
1233 int most_stop_channel(struct most_interface *iface, int id,
1234 struct core_component *comp)
1236 struct most_channel *c;
1238 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1239 pr_err("Bad interface or index out of range\n");
1242 c = iface->p->channel[id];
1246 mutex_lock(&c->start_mutex);
1247 if (c->pipe0.refs + c->pipe1.refs >= 2)
1250 if (c->hdm_enqueue_task)
1251 kthread_stop(c->hdm_enqueue_task);
1252 c->hdm_enqueue_task = NULL;
1255 module_put(iface->mod);
1257 c->is_poisoned = true;
1258 if (c->iface->poison_channel(c->iface, c->channel_id)) {
1259 pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1260 c->iface->description);
1261 mutex_unlock(&c->start_mutex);
1264 flush_trash_fifo(c);
1265 flush_channel_fifos(c);
1267 #ifdef CMPL_INTERRUPTIBLE
1268 if (wait_for_completion_interruptible(&c->cleanup)) {
1269 pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1270 mutex_unlock(&c->start_mutex);
1274 wait_for_completion(&c->cleanup);
1276 c->is_poisoned = false;
1279 if (comp == c->pipe0.comp)
1281 if (comp == c->pipe1.comp)
1283 mutex_unlock(&c->start_mutex);
1286 EXPORT_SYMBOL_GPL(most_stop_channel);
1289 * most_register_component - registers a driver component with the core
1290 * @comp: driver component
1292 int most_register_component(struct core_component *comp)
1295 pr_err("Bad component\n");
1298 list_add_tail(&comp->list, &mc.comp_list);
1299 pr_info("registered new core component %s\n", comp->name);
1302 EXPORT_SYMBOL_GPL(most_register_component);
1304 static int disconnect_channels(struct device *dev, void *data)
1306 struct most_interface *iface;
1307 struct most_channel *c, *tmp;
1308 struct core_component *comp = data;
1310 iface = to_most_interface(dev);
1311 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
1312 if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1313 comp->disconnect_channel(c->iface, c->channel_id);
1314 if (c->pipe0.comp == comp)
1315 c->pipe0.comp = NULL;
1316 if (c->pipe1.comp == comp)
1317 c->pipe1.comp = NULL;
1323 * most_deregister_component - deregisters a driver component with the core
1324 * @comp: driver component
1326 int most_deregister_component(struct core_component *comp)
1329 pr_err("Bad component\n");
1333 bus_for_each_dev(&mc.bus, NULL, comp, disconnect_channels);
1334 list_del(&comp->list);
1335 pr_info("deregistering component %s\n", comp->name);
1338 EXPORT_SYMBOL_GPL(most_deregister_component);
1340 static void release_interface(struct device *dev)
1342 pr_info("releasing interface dev %s...\n", dev_name(dev));
1345 static void release_channel(struct device *dev)
1347 pr_info("releasing channel dev %s...\n", dev_name(dev));
1351 * most_register_interface - registers an interface with core
1352 * @iface: device interface
1354 * Allocates and initializes a new interface instance and all of its channels.
1355 * Returns a pointer to kobject or an error pointer.
1357 int most_register_interface(struct most_interface *iface)
1361 struct most_channel *c;
1363 if (!iface || !iface->enqueue || !iface->configure ||
1364 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1365 pr_err("Bad interface or channel overflow\n");
1369 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1371 pr_info("Failed to alloc mdev ID\n");
1375 iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1377 pr_info("Failed to allocate interface instance\n");
1378 ida_simple_remove(&mdev_id, id);
1382 INIT_LIST_HEAD(&iface->p->channel_list);
1383 iface->p->dev_id = id;
1384 snprintf(iface->p->name, STRING_SIZE, "mdev%d", id);
1385 iface->dev.init_name = iface->p->name;
1386 iface->dev.bus = &mc.bus;
1387 iface->dev.parent = &mc.dev;
1388 iface->dev.groups = interface_attr_groups;
1389 iface->dev.release = release_interface;
1390 if (device_register(&iface->dev)) {
1391 pr_err("registering iface->dev failed\n");
1393 ida_simple_remove(&mdev_id, id);
1397 for (i = 0; i < iface->num_channels; i++) {
1398 const char *name_suffix = iface->channel_vector[i].name_suffix;
1400 c = kzalloc(sizeof(*c), GFP_KERNEL);
1404 snprintf(c->name, STRING_SIZE, "ch%d", i);
1406 snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1407 c->dev.init_name = c->name;
1408 c->dev.parent = &iface->dev;
1409 c->dev.groups = channel_attr_groups;
1410 c->dev.release = release_channel;
1411 if (device_register(&c->dev)) {
1412 pr_err("registering c->dev failed\n");
1413 goto free_instance_nodev;
1415 iface->p->channel[i] = c;
1419 c->keep_mbo = false;
1420 c->enqueue_halt = false;
1421 c->is_poisoned = false;
1422 c->cfg.direction = 0;
1423 c->cfg.data_type = 0;
1424 c->cfg.num_buffers = 0;
1425 c->cfg.buffer_size = 0;
1426 c->cfg.subbuffer_size = 0;
1427 c->cfg.packets_per_xact = 0;
1428 spin_lock_init(&c->fifo_lock);
1429 INIT_LIST_HEAD(&c->fifo);
1430 INIT_LIST_HEAD(&c->trash_fifo);
1431 INIT_LIST_HEAD(&c->halt_fifo);
1432 init_completion(&c->cleanup);
1433 atomic_set(&c->mbo_ref, 0);
1434 mutex_init(&c->start_mutex);
1435 mutex_init(&c->nq_mutex);
1436 list_add_tail(&c->list, &iface->p->channel_list);
1438 pr_info("registered new device mdev%d (%s)\n",
1439 id, iface->description);
1442 free_instance_nodev:
1447 c = iface->p->channel[--i];
1448 device_unregister(&c->dev);
1452 device_unregister(&iface->dev);
1453 ida_simple_remove(&mdev_id, id);
1456 EXPORT_SYMBOL_GPL(most_register_interface);
1459 * most_deregister_interface - deregisters an interface with core
1460 * @iface: device interface
1462 * Before removing an interface instance from the list, all running
1463 * channels are stopped and poisoned.
1465 void most_deregister_interface(struct most_interface *iface)
1468 struct most_channel *c;
1470 pr_info("deregistering device %s (%s)\n", dev_name(&iface->dev), iface->description);
1471 for (i = 0; i < iface->num_channels; i++) {
1472 c = iface->p->channel[i];
1474 c->pipe0.comp->disconnect_channel(c->iface,
1477 c->pipe1.comp->disconnect_channel(c->iface,
1479 c->pipe0.comp = NULL;
1480 c->pipe1.comp = NULL;
1482 device_unregister(&c->dev);
1486 ida_simple_remove(&mdev_id, iface->p->dev_id);
1488 device_unregister(&iface->dev);
1490 EXPORT_SYMBOL_GPL(most_deregister_interface);
1493 * most_stop_enqueue - prevents core from enqueueing MBOs
1494 * @iface: pointer to interface
1497 * This is called by an HDM that _cannot_ attend to its duties and
1498 * is imminent to get run over by the core. The core is not going to
1499 * enqueue any further packets unless the flagging HDM calls
1500 * most_resume enqueue().
1502 void most_stop_enqueue(struct most_interface *iface, int id)
1504 struct most_channel *c = iface->p->channel[id];
1509 mutex_lock(&c->nq_mutex);
1510 c->enqueue_halt = true;
1511 mutex_unlock(&c->nq_mutex);
1513 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1516 * most_resume_enqueue - allow core to enqueue MBOs again
1517 * @iface: pointer to interface
1520 * This clears the enqueue halt flag and enqueues all MBOs currently
1521 * sitting in the wait fifo.
1523 void most_resume_enqueue(struct most_interface *iface, int id)
1525 struct most_channel *c = iface->p->channel[id];
1530 mutex_lock(&c->nq_mutex);
1531 c->enqueue_halt = false;
1532 mutex_unlock(&c->nq_mutex);
1534 wake_up_interruptible(&c->hdm_fifo_wq);
1536 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1538 static void release_most_sub(struct device *dev)
1540 pr_info("releasing most_subsystem\n");
1543 static int __init most_init(void)
1547 pr_info("init()\n");
1548 INIT_LIST_HEAD(&mc.comp_list);
1551 mc.bus.name = "most",
1552 mc.bus.match = most_match,
1553 mc.drv.name = "most_core",
1554 mc.drv.bus = &mc.bus,
1555 mc.drv.groups = module_attr_groups;
1557 err = bus_register(&mc.bus);
1559 pr_info("Cannot register most bus\n");
1562 mc.class = class_create(THIS_MODULE, "most");
1563 if (IS_ERR(mc.class)) {
1564 pr_info("No udev support.\n");
1565 err = PTR_ERR(mc.class);
1569 err = driver_register(&mc.drv);
1571 pr_info("Cannot register core driver\n");
1574 mc.dev.init_name = "most_bus";
1575 mc.dev.release = release_most_sub;
1576 if (device_register(&mc.dev)) {
1584 driver_unregister(&mc.drv);
1586 class_destroy(mc.class);
1588 bus_unregister(&mc.bus);
1592 static void __exit most_exit(void)
1594 pr_info("exit core module\n");
1595 device_unregister(&mc.dev);
1596 driver_unregister(&mc.drv);
1597 class_destroy(mc.class);
1598 bus_unregister(&mc.bus);
1599 ida_destroy(&mdev_id);
1602 module_init(most_init);
1603 module_exit(most_exit);
1604 MODULE_LICENSE("GPL");
1605 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1606 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");