1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - Implementation of core module of MOST Linux driver stack
5 * Copyright (C) 2013-2020 Microchip Technology Germany II GmbH & Co. KG
8 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/list.h>
14 #include <linux/poll.h>
15 #include <linux/wait.h>
16 #include <linux/kobject.h>
17 #include <linux/mutex.h>
18 #include <linux/completion.h>
19 #include <linux/sysfs.h>
20 #include <linux/kthread.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/idr.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 most_component *comp;
49 struct completion cleanup;
51 atomic_t mbo_nq_level;
53 char name[STRING_SIZE];
55 struct mutex start_mutex; /* channel activation synchronization */
56 struct mutex nq_mutex; /* nq thread synchronization */
58 struct most_interface *iface;
59 struct most_channel_config cfg;
62 struct list_head fifo;
63 spinlock_t fifo_lock; /* fifo access synchronization */
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" },
88 { MOST_CH_ASYNC, "async" },
89 { MOST_CH_SYNC, "sync" },
90 { MOST_CH_ISOC, "isoc"},
91 { MOST_CH_ISOC, "isoc_avp"},
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 dev_warn(&mc.dev, "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_buffer_size_show(struct device *dev,
276 struct device_attribute *attr,
279 struct most_channel *c = to_channel(dev);
281 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
284 static ssize_t set_direction_show(struct device *dev,
285 struct device_attribute *attr,
288 struct most_channel *c = to_channel(dev);
290 if (c->cfg.direction & MOST_CH_TX)
291 return snprintf(buf, PAGE_SIZE, "tx\n");
292 else if (c->cfg.direction & MOST_CH_RX)
293 return snprintf(buf, PAGE_SIZE, "rx\n");
294 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
297 static ssize_t set_datatype_show(struct device *dev,
298 struct device_attribute *attr,
302 struct most_channel *c = to_channel(dev);
304 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
305 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
306 return snprintf(buf, PAGE_SIZE, "%s",
307 ch_data_type[i].name);
309 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
312 static ssize_t set_subbuffer_size_show(struct device *dev,
313 struct device_attribute *attr,
316 struct most_channel *c = to_channel(dev);
318 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
321 static ssize_t set_packets_per_xact_show(struct device *dev,
322 struct device_attribute *attr,
325 struct most_channel *c = to_channel(dev);
327 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
330 static ssize_t set_dbr_size_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
333 struct most_channel *c = to_channel(dev);
335 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
338 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
339 static umode_t channel_attr_is_visible(struct kobject *kobj,
340 struct attribute *attr, int index)
342 struct device_attribute *dev_attr = to_dev_attr(attr);
343 struct device *dev = kobj_to_dev(kobj);
344 struct most_channel *c = to_channel(dev);
346 if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
347 (c->iface->interface != ITYPE_MEDIALB_DIM2))
349 if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
350 (c->iface->interface != ITYPE_USB))
356 #define DEV_ATTR(_name) (&dev_attr_##_name.attr)
358 static DEVICE_ATTR_RO(available_directions);
359 static DEVICE_ATTR_RO(available_datatypes);
360 static DEVICE_ATTR_RO(number_of_packet_buffers);
361 static DEVICE_ATTR_RO(number_of_stream_buffers);
362 static DEVICE_ATTR_RO(size_of_stream_buffer);
363 static DEVICE_ATTR_RO(size_of_packet_buffer);
364 static DEVICE_ATTR_RO(channel_starving);
365 static DEVICE_ATTR_RO(set_buffer_size);
366 static DEVICE_ATTR_RO(set_number_of_buffers);
367 static DEVICE_ATTR_RO(set_direction);
368 static DEVICE_ATTR_RO(set_datatype);
369 static DEVICE_ATTR_RO(set_subbuffer_size);
370 static DEVICE_ATTR_RO(set_packets_per_xact);
371 static DEVICE_ATTR_RO(set_dbr_size);
373 static struct attribute *channel_attrs[] = {
374 DEV_ATTR(available_directions),
375 DEV_ATTR(available_datatypes),
376 DEV_ATTR(number_of_packet_buffers),
377 DEV_ATTR(number_of_stream_buffers),
378 DEV_ATTR(size_of_stream_buffer),
379 DEV_ATTR(size_of_packet_buffer),
380 DEV_ATTR(channel_starving),
381 DEV_ATTR(set_buffer_size),
382 DEV_ATTR(set_number_of_buffers),
383 DEV_ATTR(set_direction),
384 DEV_ATTR(set_datatype),
385 DEV_ATTR(set_subbuffer_size),
386 DEV_ATTR(set_packets_per_xact),
387 DEV_ATTR(set_dbr_size),
391 static struct attribute_group channel_attr_group = {
392 .attrs = channel_attrs,
393 .is_visible = channel_attr_is_visible,
396 static const struct attribute_group *channel_attr_groups[] = {
401 static ssize_t description_show(struct device *dev,
402 struct device_attribute *attr,
405 struct most_interface *iface = to_most_interface(dev);
407 return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
410 static ssize_t interface_show(struct device *dev,
411 struct device_attribute *attr,
414 struct most_interface *iface = to_most_interface(dev);
416 switch (iface->interface) {
418 return snprintf(buf, PAGE_SIZE, "loopback\n");
420 return snprintf(buf, PAGE_SIZE, "i2c\n");
422 return snprintf(buf, PAGE_SIZE, "i2s\n");
424 return snprintf(buf, PAGE_SIZE, "tsi\n");
426 return snprintf(buf, PAGE_SIZE, "hbi\n");
427 case ITYPE_MEDIALB_DIM:
428 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
429 case ITYPE_MEDIALB_DIM2:
430 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
432 return snprintf(buf, PAGE_SIZE, "usb\n");
434 return snprintf(buf, PAGE_SIZE, "pcie\n");
436 return snprintf(buf, PAGE_SIZE, "unknown\n");
439 static DEVICE_ATTR_RO(description);
440 static DEVICE_ATTR_RO(interface);
442 static struct attribute *interface_attrs[] = {
443 DEV_ATTR(description),
448 static struct attribute_group interface_attr_group = {
449 .attrs = interface_attrs,
452 static const struct attribute_group *interface_attr_groups[] = {
453 &interface_attr_group,
457 static struct most_component *match_component(char *name)
459 struct most_component *comp;
461 list_for_each_entry(comp, &mc.comp_list, list) {
462 if (!strcmp(comp->name, name))
468 struct show_links_data {
473 static int print_links(struct device *dev, void *data)
475 struct show_links_data *d = data;
478 struct most_channel *c;
479 struct most_interface *iface = to_most_interface(dev);
481 list_for_each_entry(c, &iface->p->channel_list, list) {
483 offs += snprintf(buf + offs,
487 dev_name(&iface->dev),
491 offs += snprintf(buf + offs,
495 dev_name(&iface->dev),
503 static ssize_t links_show(struct device_driver *drv, char *buf)
505 struct show_links_data d = { .buf = buf };
507 bus_for_each_dev(&mc.bus, NULL, &d, print_links);
511 static ssize_t components_show(struct device_driver *drv, char *buf)
513 struct most_component *comp;
516 list_for_each_entry(comp, &mc.comp_list, list) {
517 offs += snprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
524 * get_channel - get pointer to channel
525 * @mdev: name of the device interface
526 * @mdev_ch: name of channel
528 static struct most_channel *get_channel(char *mdev, char *mdev_ch)
530 struct device *dev = NULL;
531 struct most_interface *iface;
532 struct most_channel *c, *tmp;
534 dev = bus_find_device_by_name(&mc.bus, NULL, mdev);
537 iface = to_most_interface(dev);
538 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
539 if (!strcmp(dev_name(&c->dev), mdev_ch))
546 inline int link_channel_to_component(struct most_channel *c,
547 struct most_component *comp,
552 struct most_component **comp_ptr;
555 comp_ptr = &c->pipe0.comp;
556 else if (!c->pipe1.comp)
557 comp_ptr = &c->pipe1.comp;
562 ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, name,
571 int most_set_cfg_buffer_size(char *mdev, char *mdev_ch, u16 val)
573 struct most_channel *c = get_channel(mdev, mdev_ch);
577 c->cfg.buffer_size = val;
581 int most_set_cfg_subbuffer_size(char *mdev, char *mdev_ch, u16 val)
583 struct most_channel *c = get_channel(mdev, mdev_ch);
587 c->cfg.subbuffer_size = val;
591 int most_set_cfg_dbr_size(char *mdev, char *mdev_ch, u16 val)
593 struct most_channel *c = get_channel(mdev, mdev_ch);
597 c->cfg.dbr_size = val;
601 int most_set_cfg_num_buffers(char *mdev, char *mdev_ch, u16 val)
603 struct most_channel *c = get_channel(mdev, mdev_ch);
607 c->cfg.num_buffers = val;
611 int most_set_cfg_datatype(char *mdev, char *mdev_ch, char *buf)
614 struct most_channel *c = get_channel(mdev, mdev_ch);
618 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
619 if (!strcmp(buf, ch_data_type[i].name)) {
620 c->cfg.data_type = ch_data_type[i].most_ch_data_type;
625 if (i == ARRAY_SIZE(ch_data_type))
626 dev_warn(&mc.dev, "invalid attribute settings\n");
630 int most_set_cfg_direction(char *mdev, char *mdev_ch, char *buf)
632 struct most_channel *c = get_channel(mdev, mdev_ch);
636 if (!strcmp(buf, "dir_rx")) {
637 c->cfg.direction = MOST_CH_RX;
638 } else if (!strcmp(buf, "rx")) {
639 c->cfg.direction = MOST_CH_RX;
640 } else if (!strcmp(buf, "dir_tx")) {
641 c->cfg.direction = MOST_CH_TX;
642 } else if (!strcmp(buf, "tx")) {
643 c->cfg.direction = MOST_CH_TX;
645 dev_err(&mc.dev, "Invalid direction\n");
651 int most_set_cfg_packets_xact(char *mdev, char *mdev_ch, u16 val)
653 struct most_channel *c = get_channel(mdev, mdev_ch);
657 c->cfg.packets_per_xact = val;
661 int most_cfg_complete(char *comp_name)
663 struct most_component *comp;
665 comp = match_component(comp_name);
669 return comp->cfg_complete();
672 int most_add_link(char *mdev, char *mdev_ch, char *comp_name, char *link_name,
675 struct most_channel *c = get_channel(mdev, mdev_ch);
676 struct most_component *comp = match_component(comp_name);
681 return link_channel_to_component(c, comp, link_name, comp_param);
684 int most_remove_link(char *mdev, char *mdev_ch, char *comp_name)
686 struct most_channel *c;
687 struct most_component *comp;
689 comp = match_component(comp_name);
692 c = get_channel(mdev, mdev_ch);
696 if (comp->disconnect_channel(c->iface, c->channel_id))
698 if (c->pipe0.comp == comp)
699 c->pipe0.comp = NULL;
700 if (c->pipe1.comp == comp)
701 c->pipe1.comp = NULL;
705 #define DRV_ATTR(_name) (&driver_attr_##_name.attr)
707 static DRIVER_ATTR_RO(links);
708 static DRIVER_ATTR_RO(components);
710 static struct attribute *mc_attrs[] = {
712 DRV_ATTR(components),
716 static struct attribute_group mc_attr_group = {
720 static const struct attribute_group *mc_attr_groups[] = {
725 static int most_match(struct device *dev, struct device_driver *drv)
727 if (!strcmp(dev_name(dev), "most"))
733 static inline void trash_mbo(struct mbo *mbo)
736 struct most_channel *c = mbo->context;
738 spin_lock_irqsave(&c->fifo_lock, flags);
739 list_add(&mbo->list, &c->trash_fifo);
740 spin_unlock_irqrestore(&c->fifo_lock, flags);
743 static bool hdm_mbo_ready(struct most_channel *c)
750 spin_lock_irq(&c->fifo_lock);
751 empty = list_empty(&c->halt_fifo);
752 spin_unlock_irq(&c->fifo_lock);
757 static void nq_hdm_mbo(struct mbo *mbo)
760 struct most_channel *c = mbo->context;
762 spin_lock_irqsave(&c->fifo_lock, flags);
763 list_add_tail(&mbo->list, &c->halt_fifo);
764 spin_unlock_irqrestore(&c->fifo_lock, flags);
765 wake_up_interruptible(&c->hdm_fifo_wq);
768 static int hdm_enqueue_thread(void *data)
770 struct most_channel *c = data;
773 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
775 while (likely(!kthread_should_stop())) {
776 wait_event_interruptible(c->hdm_fifo_wq,
778 kthread_should_stop());
780 mutex_lock(&c->nq_mutex);
781 spin_lock_irq(&c->fifo_lock);
782 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
783 spin_unlock_irq(&c->fifo_lock);
784 mutex_unlock(&c->nq_mutex);
788 mbo = list_pop_mbo(&c->halt_fifo);
789 spin_unlock_irq(&c->fifo_lock);
791 if (c->cfg.direction == MOST_CH_RX)
792 mbo->buffer_length = c->cfg.buffer_size;
794 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
795 mutex_unlock(&c->nq_mutex);
798 dev_err(&mc.dev, "hdm enqueue failed\n");
800 c->hdm_enqueue_task = NULL;
808 static int run_enqueue_thread(struct most_channel *c, int channel_id)
810 struct task_struct *task =
811 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
815 return PTR_ERR(task);
817 c->hdm_enqueue_task = task;
822 * arm_mbo - recycle MBO for further usage
825 * This puts an MBO back to the list to have it ready for up coming
828 * In case the MBO belongs to a channel that recently has been
829 * poisoned, the MBO is scheduled to be trashed.
830 * Calls the completion handler of an attached component.
832 static void arm_mbo(struct mbo *mbo)
835 struct most_channel *c;
839 if (c->is_poisoned) {
844 spin_lock_irqsave(&c->fifo_lock, flags);
845 ++*mbo->num_buffers_ptr;
846 list_add_tail(&mbo->list, &c->fifo);
847 spin_unlock_irqrestore(&c->fifo_lock, flags);
849 if (c->pipe0.refs && c->pipe0.comp->tx_completion)
850 c->pipe0.comp->tx_completion(c->iface, c->channel_id);
852 if (c->pipe1.refs && c->pipe1.comp->tx_completion)
853 c->pipe1.comp->tx_completion(c->iface, c->channel_id);
857 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
858 * @c: pointer to interface channel
859 * @dir: direction of the channel
860 * @compl: pointer to completion function
862 * This allocates buffer objects including the containing DMA coherent
863 * buffer and puts them in the fifo.
864 * Buffers of Rx channels are put in the kthread fifo, hence immediately
865 * submitted to the HDM.
867 * Returns the number of allocated and enqueued MBOs.
869 static int arm_mbo_chain(struct most_channel *c, int dir,
870 void (*compl)(struct mbo *))
875 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
877 atomic_set(&c->mbo_nq_level, 0);
879 for (i = 0; i < c->cfg.num_buffers; i++) {
880 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
886 mbo->hdm_channel_id = c->channel_id;
887 if (c->iface->dma_alloc) {
889 c->iface->dma_alloc(mbo, coherent_buf_size);
892 kzalloc(coherent_buf_size, GFP_KERNEL);
894 if (!mbo->virt_address)
897 mbo->complete = compl;
898 mbo->num_buffers_ptr = &dummy_num_buffers;
899 if (dir == MOST_CH_RX) {
901 atomic_inc(&c->mbo_nq_level);
903 spin_lock_irqsave(&c->fifo_lock, flags);
904 list_add_tail(&mbo->list, &c->fifo);
905 spin_unlock_irqrestore(&c->fifo_lock, flags);
908 return c->cfg.num_buffers;
914 flush_channel_fifos(c);
919 * most_submit_mbo - submits an MBO to fifo
922 void most_submit_mbo(struct mbo *mbo)
924 if (WARN_ONCE(!mbo || !mbo->context,
925 "bad mbo or missing channel reference\n"))
930 EXPORT_SYMBOL_GPL(most_submit_mbo);
933 * most_write_completion - write completion handler
936 * This recycles the MBO for further usage. In case the channel has been
937 * poisoned, the MBO is scheduled to be trashed.
939 static void most_write_completion(struct mbo *mbo)
941 struct most_channel *c;
944 if (mbo->status == MBO_E_INVAL)
945 dev_warn(&mc.dev, "Tx MBO status: invalid\n");
946 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
952 int channel_has_mbo(struct most_interface *iface, int id,
953 struct most_component *comp)
955 struct most_channel *c = iface->p->channel[id];
962 if (c->pipe0.refs && c->pipe1.refs &&
963 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
964 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
967 spin_lock_irqsave(&c->fifo_lock, flags);
968 empty = list_empty(&c->fifo);
969 spin_unlock_irqrestore(&c->fifo_lock, flags);
972 EXPORT_SYMBOL_GPL(channel_has_mbo);
975 * most_get_mbo - get pointer to an MBO of pool
976 * @iface: pointer to interface instance
978 * @comp: driver component
980 * This attempts to get a free buffer out of the channel fifo.
981 * Returns a pointer to MBO on success or NULL otherwise.
983 struct mbo *most_get_mbo(struct most_interface *iface, int id,
984 struct most_component *comp)
987 struct most_channel *c;
989 int *num_buffers_ptr;
991 c = iface->p->channel[id];
995 if (c->pipe0.refs && c->pipe1.refs &&
996 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
997 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1000 if (comp == c->pipe0.comp)
1001 num_buffers_ptr = &c->pipe0.num_buffers;
1002 else if (comp == c->pipe1.comp)
1003 num_buffers_ptr = &c->pipe1.num_buffers;
1005 num_buffers_ptr = &dummy_num_buffers;
1007 spin_lock_irqsave(&c->fifo_lock, flags);
1008 if (list_empty(&c->fifo)) {
1009 spin_unlock_irqrestore(&c->fifo_lock, flags);
1012 mbo = list_pop_mbo(&c->fifo);
1014 spin_unlock_irqrestore(&c->fifo_lock, flags);
1016 mbo->num_buffers_ptr = num_buffers_ptr;
1017 mbo->buffer_length = c->cfg.buffer_size;
1020 EXPORT_SYMBOL_GPL(most_get_mbo);
1023 * most_put_mbo - return buffer to pool
1026 void most_put_mbo(struct mbo *mbo)
1028 struct most_channel *c = mbo->context;
1030 if (c->cfg.direction == MOST_CH_TX) {
1035 atomic_inc(&c->mbo_nq_level);
1037 EXPORT_SYMBOL_GPL(most_put_mbo);
1040 * most_read_completion - read completion handler
1043 * This function is called by the HDM when data has been received from the
1044 * hardware and copied to the buffer of the MBO.
1046 * In case the channel has been poisoned it puts the buffer in the trash queue.
1047 * Otherwise, it passes the buffer to an component for further processing.
1049 static void most_read_completion(struct mbo *mbo)
1051 struct most_channel *c = mbo->context;
1053 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1058 if (mbo->status == MBO_E_INVAL) {
1060 atomic_inc(&c->mbo_nq_level);
1064 if (atomic_sub_and_test(1, &c->mbo_nq_level))
1067 if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1068 c->pipe0.comp->rx_completion(mbo) == 0)
1071 if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1072 c->pipe1.comp->rx_completion(mbo) == 0)
1079 * most_start_channel - prepares a channel for communication
1080 * @iface: pointer to interface instance
1082 * @comp: driver component
1084 * This prepares the channel for usage. Cross-checks whether the
1085 * channel's been properly configured.
1087 * Returns 0 on success or error code otherwise.
1089 int most_start_channel(struct most_interface *iface, int id,
1090 struct most_component *comp)
1094 struct most_channel *c = iface->p->channel[id];
1099 mutex_lock(&c->start_mutex);
1100 if (c->pipe0.refs + c->pipe1.refs > 0)
1101 goto out; /* already started by another component */
1103 if (!try_module_get(iface->mod)) {
1104 dev_err(&mc.dev, "failed to acquire HDM lock\n");
1105 mutex_unlock(&c->start_mutex);
1109 c->cfg.extra_len = 0;
1110 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1111 dev_err(&mc.dev, "channel configuration failed. Go check settings...\n");
1113 goto err_put_module;
1116 init_waitqueue_head(&c->hdm_fifo_wq);
1118 if (c->cfg.direction == MOST_CH_RX)
1119 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1120 most_read_completion);
1122 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1123 most_write_completion);
1124 if (unlikely(!num_buffer)) {
1126 goto err_put_module;
1129 ret = run_enqueue_thread(c, id);
1131 goto err_put_module;
1134 c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1135 c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
1136 atomic_set(&c->mbo_ref, num_buffer);
1139 if (comp == c->pipe0.comp)
1141 if (comp == c->pipe1.comp)
1143 mutex_unlock(&c->start_mutex);
1147 module_put(iface->mod);
1148 mutex_unlock(&c->start_mutex);
1151 EXPORT_SYMBOL_GPL(most_start_channel);
1154 * most_stop_channel - stops a running channel
1155 * @iface: pointer to interface instance
1157 * @comp: driver component
1159 int most_stop_channel(struct most_interface *iface, int id,
1160 struct most_component *comp)
1162 struct most_channel *c;
1164 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1165 dev_err(&mc.dev, "Bad interface or index out of range\n");
1168 c = iface->p->channel[id];
1172 mutex_lock(&c->start_mutex);
1173 if (c->pipe0.refs + c->pipe1.refs >= 2)
1176 if (c->hdm_enqueue_task)
1177 kthread_stop(c->hdm_enqueue_task);
1178 c->hdm_enqueue_task = NULL;
1181 module_put(iface->mod);
1183 c->is_poisoned = true;
1184 if (c->iface->poison_channel(c->iface, c->channel_id)) {
1185 dev_err(&mc.dev, "Cannot stop channel %d of mdev %s\n", c->channel_id,
1186 c->iface->description);
1187 mutex_unlock(&c->start_mutex);
1190 flush_trash_fifo(c);
1191 flush_channel_fifos(c);
1193 #ifdef CMPL_INTERRUPTIBLE
1194 if (wait_for_completion_interruptible(&c->cleanup)) {
1195 dev_err(&mc.dev, "Interrupted while clean up ch %d\n", c->channel_id);
1196 mutex_unlock(&c->start_mutex);
1200 wait_for_completion(&c->cleanup);
1202 c->is_poisoned = false;
1205 if (comp == c->pipe0.comp)
1207 if (comp == c->pipe1.comp)
1209 mutex_unlock(&c->start_mutex);
1212 EXPORT_SYMBOL_GPL(most_stop_channel);
1215 * most_register_component - registers a driver component with the core
1216 * @comp: driver component
1218 int most_register_component(struct most_component *comp)
1221 dev_err(&mc.dev, "Bad component\n");
1224 list_add_tail(&comp->list, &mc.comp_list);
1227 EXPORT_SYMBOL_GPL(most_register_component);
1229 static int disconnect_channels(struct device *dev, void *data)
1231 struct most_interface *iface;
1232 struct most_channel *c, *tmp;
1233 struct most_component *comp = data;
1235 iface = to_most_interface(dev);
1236 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
1237 if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1238 comp->disconnect_channel(c->iface, c->channel_id);
1239 if (c->pipe0.comp == comp)
1240 c->pipe0.comp = NULL;
1241 if (c->pipe1.comp == comp)
1242 c->pipe1.comp = NULL;
1248 * most_deregister_component - deregisters a driver component with the core
1249 * @comp: driver component
1251 int most_deregister_component(struct most_component *comp)
1254 dev_err(&mc.dev, "Bad component\n");
1258 bus_for_each_dev(&mc.bus, NULL, comp, disconnect_channels);
1259 list_del(&comp->list);
1262 EXPORT_SYMBOL_GPL(most_deregister_component);
1264 static void release_interface(struct device *dev)
1266 dev_info(&mc.dev, "releasing interface dev %s...\n", dev_name(dev));
1269 static void release_channel(struct device *dev)
1271 dev_info(&mc.dev, "releasing channel dev %s...\n", dev_name(dev));
1275 * most_register_interface - registers an interface with core
1276 * @iface: device interface
1278 * Allocates and initializes a new interface instance and all of its channels.
1279 * Returns a pointer to kobject or an error pointer.
1281 int most_register_interface(struct most_interface *iface)
1285 struct most_channel *c;
1287 if (!iface || !iface->enqueue || !iface->configure ||
1288 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1289 dev_err(&mc.dev, "Bad interface or channel overflow\n");
1293 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1295 dev_err(&mc.dev, "Failed to alloc mdev ID\n");
1299 iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1301 ida_simple_remove(&mdev_id, id);
1305 INIT_LIST_HEAD(&iface->p->channel_list);
1306 iface->p->dev_id = id;
1307 strscpy(iface->p->name, iface->description, sizeof(iface->p->name));
1308 iface->dev.init_name = iface->p->name;
1309 iface->dev.bus = &mc.bus;
1310 iface->dev.parent = &mc.dev;
1311 iface->dev.groups = interface_attr_groups;
1312 iface->dev.release = release_interface;
1313 if (device_register(&iface->dev)) {
1314 dev_err(&mc.dev, "registering iface->dev failed\n");
1316 ida_simple_remove(&mdev_id, id);
1320 for (i = 0; i < iface->num_channels; i++) {
1321 const char *name_suffix = iface->channel_vector[i].name_suffix;
1323 c = kzalloc(sizeof(*c), GFP_KERNEL);
1325 goto err_free_resources;
1327 snprintf(c->name, STRING_SIZE, "ch%d", i);
1329 snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1330 c->dev.init_name = c->name;
1331 c->dev.parent = &iface->dev;
1332 c->dev.groups = channel_attr_groups;
1333 c->dev.release = release_channel;
1334 iface->p->channel[i] = c;
1338 c->keep_mbo = false;
1339 c->enqueue_halt = false;
1340 c->is_poisoned = false;
1341 c->cfg.direction = 0;
1342 c->cfg.data_type = 0;
1343 c->cfg.num_buffers = 0;
1344 c->cfg.buffer_size = 0;
1345 c->cfg.subbuffer_size = 0;
1346 c->cfg.packets_per_xact = 0;
1347 spin_lock_init(&c->fifo_lock);
1348 INIT_LIST_HEAD(&c->fifo);
1349 INIT_LIST_HEAD(&c->trash_fifo);
1350 INIT_LIST_HEAD(&c->halt_fifo);
1351 init_completion(&c->cleanup);
1352 atomic_set(&c->mbo_ref, 0);
1353 mutex_init(&c->start_mutex);
1354 mutex_init(&c->nq_mutex);
1355 list_add_tail(&c->list, &iface->p->channel_list);
1356 if (device_register(&c->dev)) {
1357 dev_err(&mc.dev, "registering c->dev failed\n");
1358 goto err_free_most_channel;
1361 most_interface_register_notify(iface->description);
1364 err_free_most_channel:
1369 c = iface->p->channel[--i];
1370 device_unregister(&c->dev);
1374 device_unregister(&iface->dev);
1375 ida_simple_remove(&mdev_id, id);
1378 EXPORT_SYMBOL_GPL(most_register_interface);
1381 * most_deregister_interface - deregisters an interface with core
1382 * @iface: device interface
1384 * Before removing an interface instance from the list, all running
1385 * channels are stopped and poisoned.
1387 void most_deregister_interface(struct most_interface *iface)
1390 struct most_channel *c;
1392 for (i = 0; i < iface->num_channels; i++) {
1393 c = iface->p->channel[i];
1395 c->pipe0.comp->disconnect_channel(c->iface,
1398 c->pipe1.comp->disconnect_channel(c->iface,
1400 c->pipe0.comp = NULL;
1401 c->pipe1.comp = NULL;
1403 device_unregister(&c->dev);
1407 ida_simple_remove(&mdev_id, iface->p->dev_id);
1409 device_unregister(&iface->dev);
1411 EXPORT_SYMBOL_GPL(most_deregister_interface);
1414 * most_stop_enqueue - prevents core from enqueueing MBOs
1415 * @iface: pointer to interface
1418 * This is called by an HDM that _cannot_ attend to its duties and
1419 * is imminent to get run over by the core. The core is not going to
1420 * enqueue any further packets unless the flagging HDM calls
1421 * most_resume enqueue().
1423 void most_stop_enqueue(struct most_interface *iface, int id)
1425 struct most_channel *c = iface->p->channel[id];
1430 mutex_lock(&c->nq_mutex);
1431 c->enqueue_halt = true;
1432 mutex_unlock(&c->nq_mutex);
1434 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1437 * most_resume_enqueue - allow core to enqueue MBOs again
1438 * @iface: pointer to interface
1441 * This clears the enqueue halt flag and enqueues all MBOs currently
1442 * sitting in the wait fifo.
1444 void most_resume_enqueue(struct most_interface *iface, int id)
1446 struct most_channel *c = iface->p->channel[id];
1451 mutex_lock(&c->nq_mutex);
1452 c->enqueue_halt = false;
1453 mutex_unlock(&c->nq_mutex);
1455 wake_up_interruptible(&c->hdm_fifo_wq);
1457 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1459 static void release_most_sub(struct device *dev)
1461 dev_info(&mc.dev, "releasing most_subsystem\n");
1464 static int __init most_init(void)
1468 INIT_LIST_HEAD(&mc.comp_list);
1471 mc.bus.name = "most",
1472 mc.bus.match = most_match,
1473 mc.drv.name = "most_core",
1474 mc.drv.bus = &mc.bus,
1475 mc.drv.groups = mc_attr_groups;
1477 err = bus_register(&mc.bus);
1479 dev_err(&mc.dev, "Cannot register most bus\n");
1482 err = driver_register(&mc.drv);
1484 dev_err(&mc.dev, "Cannot register core driver\n");
1485 goto err_unregister_bus;
1487 mc.dev.init_name = "most_bus";
1488 mc.dev.release = release_most_sub;
1489 if (device_register(&mc.dev)) {
1491 goto err_unregister_driver;
1496 err_unregister_driver:
1497 driver_unregister(&mc.drv);
1499 bus_unregister(&mc.bus);
1503 static void __exit most_exit(void)
1505 device_unregister(&mc.dev);
1506 driver_unregister(&mc.drv);
1507 bus_unregister(&mc.bus);
1508 ida_destroy(&mdev_id);
1511 module_init(most_init);
1512 module_exit(most_exit);
1513 MODULE_LICENSE("GPL");
1514 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1515 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");