GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / most / mostcore / core.c
1 /*
2  * core.c - Implementation of core module of MOST Linux driver stack
3  *
4  * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * This file is licensed under GPLv2.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/poll.h>
22 #include <linux/wait.h>
23 #include <linux/kobject.h>
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
26 #include <linux/sysfs.h>
27 #include <linux/kthread.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/idr.h>
30 #include "mostcore.h"
31
32 #define MAX_CHANNELS    64
33 #define STRING_SIZE     80
34
35 static struct class *most_class;
36 static struct device *core_dev;
37 static struct ida mdev_id;
38 static int dummy_num_buffers;
39
40 struct most_c_aim_obj {
41         struct most_aim *ptr;
42         int refs;
43         int num_buffers;
44 };
45
46 struct most_c_obj {
47         struct kobject kobj;
48         struct completion cleanup;
49         atomic_t mbo_ref;
50         atomic_t mbo_nq_level;
51         u16 channel_id;
52         bool is_poisoned;
53         struct mutex start_mutex;
54         struct mutex nq_mutex; /* nq thread synchronization */
55         int is_starving;
56         struct most_interface *iface;
57         struct most_inst_obj *inst;
58         struct most_channel_config cfg;
59         bool keep_mbo;
60         bool enqueue_halt;
61         struct list_head fifo;
62         spinlock_t fifo_lock;
63         struct list_head halt_fifo;
64         struct list_head list;
65         struct most_c_aim_obj aim0;
66         struct most_c_aim_obj aim1;
67         struct list_head trash_fifo;
68         struct task_struct *hdm_enqueue_task;
69         wait_queue_head_t hdm_fifo_wq;
70 };
71
72 #define to_c_obj(d) container_of(d, struct most_c_obj, kobj)
73
74 struct most_inst_obj {
75         int dev_id;
76         struct most_interface *iface;
77         struct list_head channel_list;
78         struct most_c_obj *channel[MAX_CHANNELS];
79         struct kobject kobj;
80         struct list_head list;
81 };
82
83 static const struct {
84         int most_ch_data_type;
85         const char *name;
86 } 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"},
92 };
93
94 #define to_inst_obj(d) container_of(d, struct most_inst_obj, kobj)
95
96 /**
97  * list_pop_mbo - retrieves the first MBO of the list and removes it
98  * @ptr: the list head to grab the MBO from.
99  */
100 #define list_pop_mbo(ptr)                                               \
101 ({                                                                      \
102         struct mbo *_mbo = list_first_entry(ptr, struct mbo, list);     \
103         list_del(&_mbo->list);                                          \
104         _mbo;                                                           \
105 })
106
107 /*                   ___             ___
108  *                   ___C H A N N E L___
109  */
110
111 /**
112  * struct most_c_attr - to access the attributes of a channel object
113  * @attr: attributes of a channel
114  * @show: pointer to the show function
115  * @store: pointer to the store function
116  */
117 struct most_c_attr {
118         struct attribute attr;
119         ssize_t (*show)(struct most_c_obj *d,
120                         struct most_c_attr *attr,
121                         char *buf);
122         ssize_t (*store)(struct most_c_obj *d,
123                          struct most_c_attr *attr,
124                          const char *buf,
125                          size_t count);
126 };
127
128 #define to_channel_attr(a) container_of(a, struct most_c_attr, attr)
129
130 /**
131  * channel_attr_show - show function of channel object
132  * @kobj: pointer to its kobject
133  * @attr: pointer to its attributes
134  * @buf: buffer
135  */
136 static ssize_t channel_attr_show(struct kobject *kobj, struct attribute *attr,
137                                  char *buf)
138 {
139         struct most_c_attr *channel_attr = to_channel_attr(attr);
140         struct most_c_obj *c_obj = to_c_obj(kobj);
141
142         if (!channel_attr->show)
143                 return -EIO;
144
145         return channel_attr->show(c_obj, channel_attr, buf);
146 }
147
148 /**
149  * channel_attr_store - store function of channel object
150  * @kobj: pointer to its kobject
151  * @attr: pointer to its attributes
152  * @buf: buffer
153  * @len: length of buffer
154  */
155 static ssize_t channel_attr_store(struct kobject *kobj,
156                                   struct attribute *attr,
157                                   const char *buf,
158                                   size_t len)
159 {
160         struct most_c_attr *channel_attr = to_channel_attr(attr);
161         struct most_c_obj *c_obj = to_c_obj(kobj);
162
163         if (!channel_attr->store)
164                 return -EIO;
165         return channel_attr->store(c_obj, channel_attr, buf, len);
166 }
167
168 static const struct sysfs_ops most_channel_sysfs_ops = {
169         .show = channel_attr_show,
170         .store = channel_attr_store,
171 };
172
173 /**
174  * most_free_mbo_coherent - free an MBO and its coherent buffer
175  * @mbo: buffer to be released
176  *
177  */
178 static void most_free_mbo_coherent(struct mbo *mbo)
179 {
180         struct most_c_obj *c = mbo->context;
181         u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
182
183         dma_free_coherent(NULL, coherent_buf_size, mbo->virt_address,
184                           mbo->bus_address);
185         kfree(mbo);
186         if (atomic_sub_and_test(1, &c->mbo_ref))
187                 complete(&c->cleanup);
188 }
189
190 /**
191  * flush_channel_fifos - clear the channel fifos
192  * @c: pointer to channel object
193  */
194 static void flush_channel_fifos(struct most_c_obj *c)
195 {
196         unsigned long flags, hf_flags;
197         struct mbo *mbo, *tmp;
198
199         if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
200                 return;
201
202         spin_lock_irqsave(&c->fifo_lock, flags);
203         list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
204                 list_del(&mbo->list);
205                 spin_unlock_irqrestore(&c->fifo_lock, flags);
206                 most_free_mbo_coherent(mbo);
207                 spin_lock_irqsave(&c->fifo_lock, flags);
208         }
209         spin_unlock_irqrestore(&c->fifo_lock, flags);
210
211         spin_lock_irqsave(&c->fifo_lock, hf_flags);
212         list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
213                 list_del(&mbo->list);
214                 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
215                 most_free_mbo_coherent(mbo);
216                 spin_lock_irqsave(&c->fifo_lock, hf_flags);
217         }
218         spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
219
220         if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
221                 pr_info("WARN: fifo | trash fifo not empty\n");
222 }
223
224 /**
225  * flush_trash_fifo - clear the trash fifo
226  * @c: pointer to channel object
227  */
228 static int flush_trash_fifo(struct most_c_obj *c)
229 {
230         struct mbo *mbo, *tmp;
231         unsigned long flags;
232
233         spin_lock_irqsave(&c->fifo_lock, flags);
234         list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
235                 list_del(&mbo->list);
236                 spin_unlock_irqrestore(&c->fifo_lock, flags);
237                 most_free_mbo_coherent(mbo);
238                 spin_lock_irqsave(&c->fifo_lock, flags);
239         }
240         spin_unlock_irqrestore(&c->fifo_lock, flags);
241         return 0;
242 }
243
244 /**
245  * most_channel_release - release function of channel object
246  * @kobj: pointer to channel's kobject
247  */
248 static void most_channel_release(struct kobject *kobj)
249 {
250         struct most_c_obj *c = to_c_obj(kobj);
251
252         kfree(c);
253 }
254
255 static ssize_t available_directions_show(struct most_c_obj *c,
256                                          struct most_c_attr *attr,
257                                          char *buf)
258 {
259         unsigned int i = c->channel_id;
260
261         strcpy(buf, "");
262         if (c->iface->channel_vector[i].direction & MOST_CH_RX)
263                 strcat(buf, "rx ");
264         if (c->iface->channel_vector[i].direction & MOST_CH_TX)
265                 strcat(buf, "tx ");
266         strcat(buf, "\n");
267         return strlen(buf);
268 }
269
270 static ssize_t available_datatypes_show(struct most_c_obj *c,
271                                         struct most_c_attr *attr,
272                                         char *buf)
273 {
274         unsigned int i = c->channel_id;
275
276         strcpy(buf, "");
277         if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
278                 strcat(buf, "control ");
279         if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
280                 strcat(buf, "async ");
281         if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
282                 strcat(buf, "sync ");
283         if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
284                 strcat(buf, "isoc ");
285         strcat(buf, "\n");
286         return strlen(buf);
287 }
288
289 static ssize_t number_of_packet_buffers_show(struct most_c_obj *c,
290                                              struct most_c_attr *attr,
291                                              char *buf)
292 {
293         unsigned int i = c->channel_id;
294
295         return snprintf(buf, PAGE_SIZE, "%d\n",
296                         c->iface->channel_vector[i].num_buffers_packet);
297 }
298
299 static ssize_t number_of_stream_buffers_show(struct most_c_obj *c,
300                                              struct most_c_attr *attr,
301                                              char *buf)
302 {
303         unsigned int i = c->channel_id;
304
305         return snprintf(buf, PAGE_SIZE, "%d\n",
306                         c->iface->channel_vector[i].num_buffers_streaming);
307 }
308
309 static ssize_t size_of_packet_buffer_show(struct most_c_obj *c,
310                                           struct most_c_attr *attr,
311                                           char *buf)
312 {
313         unsigned int i = c->channel_id;
314
315         return snprintf(buf, PAGE_SIZE, "%d\n",
316                         c->iface->channel_vector[i].buffer_size_packet);
317 }
318
319 static ssize_t size_of_stream_buffer_show(struct most_c_obj *c,
320                                           struct most_c_attr *attr,
321                                           char *buf)
322 {
323         unsigned int i = c->channel_id;
324
325         return snprintf(buf, PAGE_SIZE, "%d\n",
326                         c->iface->channel_vector[i].buffer_size_streaming);
327 }
328
329 static ssize_t channel_starving_show(struct most_c_obj *c,
330                                      struct most_c_attr *attr,
331                                      char *buf)
332 {
333         return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
334 }
335
336 static ssize_t set_number_of_buffers_show(struct most_c_obj *c,
337                                           struct most_c_attr *attr,
338                                           char *buf)
339 {
340         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
341 }
342
343 static ssize_t set_number_of_buffers_store(struct most_c_obj *c,
344                                            struct most_c_attr *attr,
345                                            const char *buf,
346                                            size_t count)
347 {
348         int ret = kstrtou16(buf, 0, &c->cfg.num_buffers);
349
350         if (ret)
351                 return ret;
352         return count;
353 }
354
355 static ssize_t set_buffer_size_show(struct most_c_obj *c,
356                                     struct most_c_attr *attr,
357                                     char *buf)
358 {
359         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
360 }
361
362 static ssize_t set_buffer_size_store(struct most_c_obj *c,
363                                      struct most_c_attr *attr,
364                                      const char *buf,
365                                      size_t count)
366 {
367         int ret = kstrtou16(buf, 0, &c->cfg.buffer_size);
368
369         if (ret)
370                 return ret;
371         return count;
372 }
373
374 static ssize_t set_direction_show(struct most_c_obj *c,
375                                   struct most_c_attr *attr,
376                                   char *buf)
377 {
378         if (c->cfg.direction & MOST_CH_TX)
379                 return snprintf(buf, PAGE_SIZE, "tx\n");
380         else if (c->cfg.direction & MOST_CH_RX)
381                 return snprintf(buf, PAGE_SIZE, "rx\n");
382         return snprintf(buf, PAGE_SIZE, "unconfigured\n");
383 }
384
385 static ssize_t set_direction_store(struct most_c_obj *c,
386                                    struct most_c_attr *attr,
387                                    const char *buf,
388                                    size_t count)
389 {
390         if (!strcmp(buf, "dir_rx\n")) {
391                 c->cfg.direction = MOST_CH_RX;
392         } else if (!strcmp(buf, "rx\n")) {
393                 c->cfg.direction = MOST_CH_RX;
394         } else if (!strcmp(buf, "dir_tx\n")) {
395                 c->cfg.direction = MOST_CH_TX;
396         } else if (!strcmp(buf, "tx\n")) {
397                 c->cfg.direction = MOST_CH_TX;
398         } else {
399                 pr_info("WARN: invalid attribute settings\n");
400                 return -EINVAL;
401         }
402         return count;
403 }
404
405 static ssize_t set_datatype_show(struct most_c_obj *c,
406                                  struct most_c_attr *attr,
407                                  char *buf)
408 {
409         int i;
410
411         for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
412                 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
413                         return snprintf(buf, PAGE_SIZE, ch_data_type[i].name);
414         }
415         return snprintf(buf, PAGE_SIZE, "unconfigured\n");
416 }
417
418 static ssize_t set_datatype_store(struct most_c_obj *c,
419                                   struct most_c_attr *attr,
420                                   const char *buf,
421                                   size_t count)
422 {
423         int i;
424
425         for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
426                 if (!strcmp(buf, ch_data_type[i].name)) {
427                         c->cfg.data_type = ch_data_type[i].most_ch_data_type;
428                         break;
429                 }
430         }
431
432         if (i == ARRAY_SIZE(ch_data_type)) {
433                 pr_info("WARN: invalid attribute settings\n");
434                 return -EINVAL;
435         }
436         return count;
437 }
438
439 static ssize_t set_subbuffer_size_show(struct most_c_obj *c,
440                                        struct most_c_attr *attr,
441                                        char *buf)
442 {
443         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
444 }
445
446 static ssize_t set_subbuffer_size_store(struct most_c_obj *c,
447                                         struct most_c_attr *attr,
448                                         const char *buf,
449                                         size_t count)
450 {
451         int ret = kstrtou16(buf, 0, &c->cfg.subbuffer_size);
452
453         if (ret)
454                 return ret;
455         return count;
456 }
457
458 static ssize_t set_packets_per_xact_show(struct most_c_obj *c,
459                                          struct most_c_attr *attr,
460                                          char *buf)
461 {
462         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
463 }
464
465 static ssize_t set_packets_per_xact_store(struct most_c_obj *c,
466                                           struct most_c_attr *attr,
467                                           const char *buf,
468                                           size_t count)
469 {
470         int ret = kstrtou16(buf, 0, &c->cfg.packets_per_xact);
471
472         if (ret)
473                 return ret;
474         return count;
475 }
476
477 static struct most_c_attr most_c_attrs[] = {
478         __ATTR_RO(available_directions),
479         __ATTR_RO(available_datatypes),
480         __ATTR_RO(number_of_packet_buffers),
481         __ATTR_RO(number_of_stream_buffers),
482         __ATTR_RO(size_of_stream_buffer),
483         __ATTR_RO(size_of_packet_buffer),
484         __ATTR_RO(channel_starving),
485         __ATTR_RW(set_buffer_size),
486         __ATTR_RW(set_number_of_buffers),
487         __ATTR_RW(set_direction),
488         __ATTR_RW(set_datatype),
489         __ATTR_RW(set_subbuffer_size),
490         __ATTR_RW(set_packets_per_xact),
491 };
492
493 /**
494  * most_channel_def_attrs - array of default attributes of channel object
495  */
496 static struct attribute *most_channel_def_attrs[] = {
497         &most_c_attrs[0].attr,
498         &most_c_attrs[1].attr,
499         &most_c_attrs[2].attr,
500         &most_c_attrs[3].attr,
501         &most_c_attrs[4].attr,
502         &most_c_attrs[5].attr,
503         &most_c_attrs[6].attr,
504         &most_c_attrs[7].attr,
505         &most_c_attrs[8].attr,
506         &most_c_attrs[9].attr,
507         &most_c_attrs[10].attr,
508         &most_c_attrs[11].attr,
509         &most_c_attrs[12].attr,
510         NULL,
511 };
512
513 static struct kobj_type most_channel_ktype = {
514         .sysfs_ops = &most_channel_sysfs_ops,
515         .release = most_channel_release,
516         .default_attrs = most_channel_def_attrs,
517 };
518
519 static struct kset *most_channel_kset;
520
521 /**
522  * create_most_c_obj - allocates a channel object
523  * @name: name of the channel object
524  * @parent: parent kobject
525  *
526  * This create a channel object and registers it with sysfs.
527  * Returns a pointer to the object or NULL when something went wrong.
528  */
529 static struct most_c_obj *
530 create_most_c_obj(const char *name, struct kobject *parent)
531 {
532         struct most_c_obj *c;
533         int retval;
534
535         c = kzalloc(sizeof(*c), GFP_KERNEL);
536         if (!c)
537                 return NULL;
538         c->kobj.kset = most_channel_kset;
539         retval = kobject_init_and_add(&c->kobj, &most_channel_ktype, parent,
540                                       "%s", name);
541         if (retval) {
542                 kobject_put(&c->kobj);
543                 return NULL;
544         }
545         kobject_uevent(&c->kobj, KOBJ_ADD);
546         return c;
547 }
548
549 /*                   ___               ___
550  *                   ___I N S T A N C E___
551  */
552
553 static struct list_head instance_list;
554
555 /**
556  * struct most_inst_attribute - to access the attributes of instance object
557  * @attr: attributes of an instance
558  * @show: pointer to the show function
559  * @store: pointer to the store function
560  */
561 struct most_inst_attribute {
562         struct attribute attr;
563         ssize_t (*show)(struct most_inst_obj *d,
564                         struct most_inst_attribute *attr,
565                         char *buf);
566         ssize_t (*store)(struct most_inst_obj *d,
567                          struct most_inst_attribute *attr,
568                          const char *buf,
569                          size_t count);
570 };
571
572 #define to_instance_attr(a) \
573         container_of(a, struct most_inst_attribute, attr)
574
575 /**
576  * instance_attr_show - show function for an instance object
577  * @kobj: pointer to kobject
578  * @attr: pointer to attribute struct
579  * @buf: buffer
580  */
581 static ssize_t instance_attr_show(struct kobject *kobj,
582                                   struct attribute *attr,
583                                   char *buf)
584 {
585         struct most_inst_attribute *instance_attr;
586         struct most_inst_obj *instance_obj;
587
588         instance_attr = to_instance_attr(attr);
589         instance_obj = to_inst_obj(kobj);
590
591         if (!instance_attr->show)
592                 return -EIO;
593
594         return instance_attr->show(instance_obj, instance_attr, buf);
595 }
596
597 /**
598  * instance_attr_store - store function for an instance object
599  * @kobj: pointer to kobject
600  * @attr: pointer to attribute struct
601  * @buf: buffer
602  * @len: length of buffer
603  */
604 static ssize_t instance_attr_store(struct kobject *kobj,
605                                    struct attribute *attr,
606                                    const char *buf,
607                                    size_t len)
608 {
609         struct most_inst_attribute *instance_attr;
610         struct most_inst_obj *instance_obj;
611
612         instance_attr = to_instance_attr(attr);
613         instance_obj = to_inst_obj(kobj);
614
615         if (!instance_attr->store)
616                 return -EIO;
617
618         return instance_attr->store(instance_obj, instance_attr, buf, len);
619 }
620
621 static const struct sysfs_ops most_inst_sysfs_ops = {
622         .show = instance_attr_show,
623         .store = instance_attr_store,
624 };
625
626 /**
627  * most_inst_release - release function for instance object
628  * @kobj: pointer to instance's kobject
629  *
630  * This frees the allocated memory for the instance object
631  */
632 static void most_inst_release(struct kobject *kobj)
633 {
634         struct most_inst_obj *inst = to_inst_obj(kobj);
635
636         kfree(inst);
637 }
638
639 static ssize_t description_show(struct most_inst_obj *instance_obj,
640                                 struct most_inst_attribute *attr,
641                                 char *buf)
642 {
643         return snprintf(buf, PAGE_SIZE, "%s\n",
644                         instance_obj->iface->description);
645 }
646
647 static ssize_t interface_show(struct most_inst_obj *instance_obj,
648                               struct most_inst_attribute *attr,
649                               char *buf)
650 {
651         switch (instance_obj->iface->interface) {
652         case ITYPE_LOOPBACK:
653                 return snprintf(buf, PAGE_SIZE, "loopback\n");
654         case ITYPE_I2C:
655                 return snprintf(buf, PAGE_SIZE, "i2c\n");
656         case ITYPE_I2S:
657                 return snprintf(buf, PAGE_SIZE, "i2s\n");
658         case ITYPE_TSI:
659                 return snprintf(buf, PAGE_SIZE, "tsi\n");
660         case ITYPE_HBI:
661                 return snprintf(buf, PAGE_SIZE, "hbi\n");
662         case ITYPE_MEDIALB_DIM:
663                 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
664         case ITYPE_MEDIALB_DIM2:
665                 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
666         case ITYPE_USB:
667                 return snprintf(buf, PAGE_SIZE, "usb\n");
668         case ITYPE_PCIE:
669                 return snprintf(buf, PAGE_SIZE, "pcie\n");
670         }
671         return snprintf(buf, PAGE_SIZE, "unknown\n");
672 }
673
674 static struct most_inst_attribute most_inst_attr_description =
675         __ATTR_RO(description);
676
677 static struct most_inst_attribute most_inst_attr_interface =
678         __ATTR_RO(interface);
679
680 static struct attribute *most_inst_def_attrs[] = {
681         &most_inst_attr_description.attr,
682         &most_inst_attr_interface.attr,
683         NULL,
684 };
685
686 static struct kobj_type most_inst_ktype = {
687         .sysfs_ops = &most_inst_sysfs_ops,
688         .release = most_inst_release,
689         .default_attrs = most_inst_def_attrs,
690 };
691
692 static struct kset *most_inst_kset;
693
694 /**
695  * create_most_inst_obj - creates an instance object
696  * @name: name of the object to be created
697  *
698  * This allocates memory for an instance structure, assigns the proper kset
699  * and registers it with sysfs.
700  *
701  * Returns a pointer to the instance object or NULL when something went wrong.
702  */
703 static struct most_inst_obj *create_most_inst_obj(const char *name)
704 {
705         struct most_inst_obj *inst;
706         int retval;
707
708         inst = kzalloc(sizeof(*inst), GFP_KERNEL);
709         if (!inst)
710                 return NULL;
711         inst->kobj.kset = most_inst_kset;
712         retval = kobject_init_and_add(&inst->kobj, &most_inst_ktype, NULL,
713                                       "%s", name);
714         if (retval) {
715                 kobject_put(&inst->kobj);
716                 return NULL;
717         }
718         kobject_uevent(&inst->kobj, KOBJ_ADD);
719         return inst;
720 }
721
722 /**
723  * destroy_most_inst_obj - MOST instance release function
724  * @inst: pointer to the instance object
725  *
726  * This decrements the reference counter of the instance object.
727  * If the reference count turns zero, its release function is called
728  */
729 static void destroy_most_inst_obj(struct most_inst_obj *inst)
730 {
731         struct most_c_obj *c, *tmp;
732
733         list_for_each_entry_safe(c, tmp, &inst->channel_list, list) {
734                 flush_trash_fifo(c);
735                 flush_channel_fifos(c);
736                 kobject_put(&c->kobj);
737         }
738         kobject_put(&inst->kobj);
739 }
740
741 /*                   ___     ___
742  *                   ___A I M___
743  */
744 struct most_aim_obj {
745         struct kobject kobj;
746         struct list_head list;
747         struct most_aim *driver;
748 };
749
750 #define to_aim_obj(d) container_of(d, struct most_aim_obj, kobj)
751
752 static struct list_head aim_list;
753
754 /**
755  * struct most_aim_attribute - to access the attributes of AIM object
756  * @attr: attributes of an AIM
757  * @show: pointer to the show function
758  * @store: pointer to the store function
759  */
760 struct most_aim_attribute {
761         struct attribute attr;
762         ssize_t (*show)(struct most_aim_obj *d,
763                         struct most_aim_attribute *attr,
764                         char *buf);
765         ssize_t (*store)(struct most_aim_obj *d,
766                          struct most_aim_attribute *attr,
767                          const char *buf,
768                          size_t count);
769 };
770
771 #define to_aim_attr(a) container_of(a, struct most_aim_attribute, attr)
772
773 /**
774  * aim_attr_show - show function of an AIM object
775  * @kobj: pointer to kobject
776  * @attr: pointer to attribute struct
777  * @buf: buffer
778  */
779 static ssize_t aim_attr_show(struct kobject *kobj,
780                              struct attribute *attr,
781                              char *buf)
782 {
783         struct most_aim_attribute *aim_attr;
784         struct most_aim_obj *aim_obj;
785
786         aim_attr = to_aim_attr(attr);
787         aim_obj = to_aim_obj(kobj);
788
789         if (!aim_attr->show)
790                 return -EIO;
791
792         return aim_attr->show(aim_obj, aim_attr, buf);
793 }
794
795 /**
796  * aim_attr_store - store function of an AIM object
797  * @kobj: pointer to kobject
798  * @attr: pointer to attribute struct
799  * @buf: buffer
800  * @len: length of buffer
801  */
802 static ssize_t aim_attr_store(struct kobject *kobj,
803                               struct attribute *attr,
804                               const char *buf,
805                               size_t len)
806 {
807         struct most_aim_attribute *aim_attr;
808         struct most_aim_obj *aim_obj;
809
810         aim_attr = to_aim_attr(attr);
811         aim_obj = to_aim_obj(kobj);
812
813         if (!aim_attr->store)
814                 return -EIO;
815         return aim_attr->store(aim_obj, aim_attr, buf, len);
816 }
817
818 static const struct sysfs_ops most_aim_sysfs_ops = {
819         .show = aim_attr_show,
820         .store = aim_attr_store,
821 };
822
823 /**
824  * most_aim_release - AIM release function
825  * @kobj: pointer to AIM's kobject
826  */
827 static void most_aim_release(struct kobject *kobj)
828 {
829         struct most_aim_obj *aim_obj = to_aim_obj(kobj);
830
831         kfree(aim_obj);
832 }
833
834 static ssize_t links_show(struct most_aim_obj *aim_obj,
835                           struct most_aim_attribute *attr,
836                           char *buf)
837 {
838         struct most_c_obj *c;
839         struct most_inst_obj *i;
840         int offs = 0;
841
842         list_for_each_entry(i, &instance_list, list) {
843                 list_for_each_entry(c, &i->channel_list, list) {
844                         if (c->aim0.ptr == aim_obj->driver ||
845                             c->aim1.ptr == aim_obj->driver) {
846                                 offs += snprintf(buf + offs, PAGE_SIZE - offs,
847                                                  "%s:%s\n",
848                                                  kobject_name(&i->kobj),
849                                                  kobject_name(&c->kobj));
850                         }
851                 }
852         }
853
854         return offs;
855 }
856
857 /**
858  * split_string - parses and changes string in the buffer buf and
859  * splits it into two mandatory and one optional substrings.
860  *
861  * @buf: complete string from attribute 'add_channel'
862  * @a: address of pointer to 1st substring (=instance name)
863  * @b: address of pointer to 2nd substring (=channel name)
864  * @c: optional address of pointer to 3rd substring (=user defined name)
865  *
866  * Examples:
867  *
868  * Input: "mdev0:ch6:my_channel\n" or
869  *        "mdev0:ch6:my_channel"
870  *
871  * Output: *a -> "mdev0", *b -> "ch6", *c -> "my_channel"
872  *
873  * Input: "mdev1:ep81\n"
874  * Output: *a -> "mdev1", *b -> "ep81", *c -> ""
875  *
876  * Input: "mdev1:ep81"
877  * Output: *a -> "mdev1", *b -> "ep81", *c == NULL
878  */
879 static int split_string(char *buf, char **a, char **b, char **c)
880 {
881         *a = strsep(&buf, ":");
882         if (!*a)
883                 return -EIO;
884
885         *b = strsep(&buf, ":\n");
886         if (!*b)
887                 return -EIO;
888
889         if (c)
890                 *c = strsep(&buf, ":\n");
891
892         return 0;
893 }
894
895 /**
896  * get_channel_by_name - get pointer to channel object
897  * @mdev: name of the device instance
898  * @mdev_ch: name of the respective channel
899  *
900  * This retrieves the pointer to a channel object.
901  */
902 static struct
903 most_c_obj *get_channel_by_name(char *mdev, char *mdev_ch)
904 {
905         struct most_c_obj *c, *tmp;
906         struct most_inst_obj *i, *i_tmp;
907         int found = 0;
908
909         list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
910                 if (!strcmp(kobject_name(&i->kobj), mdev)) {
911                         found++;
912                         break;
913                 }
914         }
915         if (unlikely(!found))
916                 return ERR_PTR(-EIO);
917
918         list_for_each_entry_safe(c, tmp, &i->channel_list, list) {
919                 if (!strcmp(kobject_name(&c->kobj), mdev_ch)) {
920                         found++;
921                         break;
922                 }
923         }
924         if (unlikely(found < 2))
925                 return ERR_PTR(-EIO);
926         return c;
927 }
928
929 /**
930  * add_link_store - store() function for add_link attribute
931  * @aim_obj: pointer to AIM object
932  * @attr: its attributes
933  * @buf: buffer
934  * @len: buffer length
935  *
936  * This parses the string given by buf and splits it into
937  * three substrings. Note: third substring is optional. In case a cdev
938  * AIM is loaded the optional 3rd substring will make up the name of
939  * device node in the /dev directory. If omitted, the device node will
940  * inherit the channel's name within sysfs.
941  *
942  * Searches for a pair of device and channel and probes the AIM
943  *
944  * Example:
945  * (1) echo "mdev0:ch6:my_rxchannel" >add_link
946  * (2) echo "mdev1:ep81" >add_link
947  *
948  * (1) would create the device node /dev/my_rxchannel
949  * (2) would create the device node /dev/mdev1-ep81
950  */
951 static ssize_t add_link_store(struct most_aim_obj *aim_obj,
952                               struct most_aim_attribute *attr,
953                               const char *buf,
954                               size_t len)
955 {
956         struct most_c_obj *c;
957         struct most_aim **aim_ptr;
958         char buffer[STRING_SIZE];
959         char *mdev;
960         char *mdev_ch;
961         char *mdev_devnod;
962         char devnod_buf[STRING_SIZE];
963         int ret;
964         size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
965
966         strlcpy(buffer, buf, max_len);
967
968         ret = split_string(buffer, &mdev, &mdev_ch, &mdev_devnod);
969         if (ret)
970                 return ret;
971
972         if (!mdev_devnod || *mdev_devnod == 0) {
973                 snprintf(devnod_buf, sizeof(devnod_buf), "%s-%s", mdev,
974                          mdev_ch);
975                 mdev_devnod = devnod_buf;
976         }
977
978         c = get_channel_by_name(mdev, mdev_ch);
979         if (IS_ERR(c))
980                 return -ENODEV;
981
982         if (!c->aim0.ptr)
983                 aim_ptr = &c->aim0.ptr;
984         else if (!c->aim1.ptr)
985                 aim_ptr = &c->aim1.ptr;
986         else
987                 return -ENOSPC;
988
989         *aim_ptr = aim_obj->driver;
990         ret = aim_obj->driver->probe_channel(c->iface, c->channel_id,
991                                              &c->cfg, &c->kobj, mdev_devnod);
992         if (ret) {
993                 *aim_ptr = NULL;
994                 return ret;
995         }
996
997         return len;
998 }
999
1000 /**
1001  * remove_link_store - store function for remove_link attribute
1002  * @aim_obj: pointer to AIM object
1003  * @attr: its attributes
1004  * @buf: buffer
1005  * @len: buffer length
1006  *
1007  * Example:
1008  * echo "mdev0:ep81" >remove_link
1009  */
1010 static ssize_t remove_link_store(struct most_aim_obj *aim_obj,
1011                                  struct most_aim_attribute *attr,
1012                                  const char *buf,
1013                                  size_t len)
1014 {
1015         struct most_c_obj *c;
1016         char buffer[STRING_SIZE];
1017         char *mdev;
1018         char *mdev_ch;
1019         int ret;
1020         size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
1021
1022         strlcpy(buffer, buf, max_len);
1023         ret = split_string(buffer, &mdev, &mdev_ch, NULL);
1024         if (ret)
1025                 return ret;
1026
1027         c = get_channel_by_name(mdev, mdev_ch);
1028         if (IS_ERR(c))
1029                 return -ENODEV;
1030
1031         if (aim_obj->driver->disconnect_channel(c->iface, c->channel_id))
1032                 return -EIO;
1033         if (c->aim0.ptr == aim_obj->driver)
1034                 c->aim0.ptr = NULL;
1035         if (c->aim1.ptr == aim_obj->driver)
1036                 c->aim1.ptr = NULL;
1037         return len;
1038 }
1039
1040 static struct most_aim_attribute most_aim_attrs[] = {
1041         __ATTR_RO(links),
1042         __ATTR_WO(add_link),
1043         __ATTR_WO(remove_link),
1044 };
1045
1046 static struct attribute *most_aim_def_attrs[] = {
1047         &most_aim_attrs[0].attr,
1048         &most_aim_attrs[1].attr,
1049         &most_aim_attrs[2].attr,
1050         NULL,
1051 };
1052
1053 static struct kobj_type most_aim_ktype = {
1054         .sysfs_ops = &most_aim_sysfs_ops,
1055         .release = most_aim_release,
1056         .default_attrs = most_aim_def_attrs,
1057 };
1058
1059 static struct kset *most_aim_kset;
1060
1061 /**
1062  * create_most_aim_obj - creates an AIM object
1063  * @name: name of the AIM
1064  *
1065  * This creates an AIM object assigns the proper kset and registers
1066  * it with sysfs.
1067  * Returns a pointer to the object or NULL if something went wrong.
1068  */
1069 static struct most_aim_obj *create_most_aim_obj(const char *name)
1070 {
1071         struct most_aim_obj *most_aim;
1072         int retval;
1073
1074         most_aim = kzalloc(sizeof(*most_aim), GFP_KERNEL);
1075         if (!most_aim)
1076                 return NULL;
1077         most_aim->kobj.kset = most_aim_kset;
1078         retval = kobject_init_and_add(&most_aim->kobj, &most_aim_ktype,
1079                                       NULL, "%s", name);
1080         if (retval) {
1081                 kobject_put(&most_aim->kobj);
1082                 return NULL;
1083         }
1084         kobject_uevent(&most_aim->kobj, KOBJ_ADD);
1085         return most_aim;
1086 }
1087
1088 /**
1089  * destroy_most_aim_obj - AIM release function
1090  * @p: pointer to AIM object
1091  *
1092  * This decrements the reference counter of the AIM object. If the
1093  * reference count turns zero, its release function will be called.
1094  */
1095 static void destroy_most_aim_obj(struct most_aim_obj *p)
1096 {
1097         kobject_put(&p->kobj);
1098 }
1099
1100 /*                   ___       ___
1101  *                   ___C O R E___
1102  */
1103
1104 /**
1105  * Instantiation of the MOST bus
1106  */
1107 static struct bus_type most_bus = {
1108         .name = "most",
1109 };
1110
1111 /**
1112  * Instantiation of the core driver
1113  */
1114 static struct device_driver mostcore = {
1115         .name = "mostcore",
1116         .bus = &most_bus,
1117 };
1118
1119 static inline void trash_mbo(struct mbo *mbo)
1120 {
1121         unsigned long flags;
1122         struct most_c_obj *c = mbo->context;
1123
1124         spin_lock_irqsave(&c->fifo_lock, flags);
1125         list_add(&mbo->list, &c->trash_fifo);
1126         spin_unlock_irqrestore(&c->fifo_lock, flags);
1127 }
1128
1129 static bool hdm_mbo_ready(struct most_c_obj *c)
1130 {
1131         bool empty;
1132
1133         if (c->enqueue_halt)
1134                 return false;
1135
1136         spin_lock_irq(&c->fifo_lock);
1137         empty = list_empty(&c->halt_fifo);
1138         spin_unlock_irq(&c->fifo_lock);
1139
1140         return !empty;
1141 }
1142
1143 static void nq_hdm_mbo(struct mbo *mbo)
1144 {
1145         unsigned long flags;
1146         struct most_c_obj *c = mbo->context;
1147
1148         spin_lock_irqsave(&c->fifo_lock, flags);
1149         list_add_tail(&mbo->list, &c->halt_fifo);
1150         spin_unlock_irqrestore(&c->fifo_lock, flags);
1151         wake_up_interruptible(&c->hdm_fifo_wq);
1152 }
1153
1154 static int hdm_enqueue_thread(void *data)
1155 {
1156         struct most_c_obj *c = data;
1157         struct mbo *mbo;
1158         int ret;
1159         typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
1160
1161         while (likely(!kthread_should_stop())) {
1162                 wait_event_interruptible(c->hdm_fifo_wq,
1163                                          hdm_mbo_ready(c) ||
1164                                          kthread_should_stop());
1165
1166                 mutex_lock(&c->nq_mutex);
1167                 spin_lock_irq(&c->fifo_lock);
1168                 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
1169                         spin_unlock_irq(&c->fifo_lock);
1170                         mutex_unlock(&c->nq_mutex);
1171                         continue;
1172                 }
1173
1174                 mbo = list_pop_mbo(&c->halt_fifo);
1175                 spin_unlock_irq(&c->fifo_lock);
1176
1177                 if (c->cfg.direction == MOST_CH_RX)
1178                         mbo->buffer_length = c->cfg.buffer_size;
1179
1180                 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
1181                 mutex_unlock(&c->nq_mutex);
1182
1183                 if (unlikely(ret)) {
1184                         pr_err("hdm enqueue failed\n");
1185                         nq_hdm_mbo(mbo);
1186                         c->hdm_enqueue_task = NULL;
1187                         return 0;
1188                 }
1189         }
1190
1191         return 0;
1192 }
1193
1194 static int run_enqueue_thread(struct most_c_obj *c, int channel_id)
1195 {
1196         struct task_struct *task =
1197                 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
1198                             channel_id);
1199
1200         if (IS_ERR(task))
1201                 return PTR_ERR(task);
1202
1203         c->hdm_enqueue_task = task;
1204         return 0;
1205 }
1206
1207 /**
1208  * arm_mbo - recycle MBO for further usage
1209  * @mbo: buffer object
1210  *
1211  * This puts an MBO back to the list to have it ready for up coming
1212  * tx transactions.
1213  *
1214  * In case the MBO belongs to a channel that recently has been
1215  * poisoned, the MBO is scheduled to be trashed.
1216  * Calls the completion handler of an attached AIM.
1217  */
1218 static void arm_mbo(struct mbo *mbo)
1219 {
1220         unsigned long flags;
1221         struct most_c_obj *c;
1222
1223         BUG_ON((!mbo) || (!mbo->context));
1224         c = mbo->context;
1225
1226         if (c->is_poisoned) {
1227                 trash_mbo(mbo);
1228                 return;
1229         }
1230
1231         spin_lock_irqsave(&c->fifo_lock, flags);
1232         ++*mbo->num_buffers_ptr;
1233         list_add_tail(&mbo->list, &c->fifo);
1234         spin_unlock_irqrestore(&c->fifo_lock, flags);
1235
1236         if (c->aim0.refs && c->aim0.ptr->tx_completion)
1237                 c->aim0.ptr->tx_completion(c->iface, c->channel_id);
1238
1239         if (c->aim1.refs && c->aim1.ptr->tx_completion)
1240                 c->aim1.ptr->tx_completion(c->iface, c->channel_id);
1241 }
1242
1243 /**
1244  * arm_mbo_chain - helper function that arms an MBO chain for the HDM
1245  * @c: pointer to interface channel
1246  * @dir: direction of the channel
1247  * @compl: pointer to completion function
1248  *
1249  * This allocates buffer objects including the containing DMA coherent
1250  * buffer and puts them in the fifo.
1251  * Buffers of Rx channels are put in the kthread fifo, hence immediately
1252  * submitted to the HDM.
1253  *
1254  * Returns the number of allocated and enqueued MBOs.
1255  */
1256 static int arm_mbo_chain(struct most_c_obj *c, int dir,
1257                          void (*compl)(struct mbo *))
1258 {
1259         unsigned int i;
1260         int retval;
1261         struct mbo *mbo;
1262         u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
1263
1264         atomic_set(&c->mbo_nq_level, 0);
1265
1266         for (i = 0; i < c->cfg.num_buffers; i++) {
1267                 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
1268                 if (!mbo) {
1269                         retval = i;
1270                         goto _exit;
1271                 }
1272                 mbo->context = c;
1273                 mbo->ifp = c->iface;
1274                 mbo->hdm_channel_id = c->channel_id;
1275                 mbo->virt_address = dma_alloc_coherent(NULL,
1276                                                        coherent_buf_size,
1277                                                        &mbo->bus_address,
1278                                                        GFP_KERNEL);
1279                 if (!mbo->virt_address) {
1280                         pr_info("WARN: No DMA coherent buffer.\n");
1281                         retval = i;
1282                         goto _error1;
1283                 }
1284                 mbo->complete = compl;
1285                 mbo->num_buffers_ptr = &dummy_num_buffers;
1286                 if (dir == MOST_CH_RX) {
1287                         nq_hdm_mbo(mbo);
1288                         atomic_inc(&c->mbo_nq_level);
1289                 } else {
1290                         arm_mbo(mbo);
1291                 }
1292         }
1293         return i;
1294
1295 _error1:
1296         kfree(mbo);
1297 _exit:
1298         return retval;
1299 }
1300
1301 /**
1302  * most_submit_mbo - submits an MBO to fifo
1303  * @mbo: pointer to the MBO
1304  */
1305 void most_submit_mbo(struct mbo *mbo)
1306 {
1307         if (WARN_ONCE(!mbo || !mbo->context,
1308                       "bad mbo or missing channel reference\n"))
1309                 return;
1310
1311         nq_hdm_mbo(mbo);
1312 }
1313 EXPORT_SYMBOL_GPL(most_submit_mbo);
1314
1315 /**
1316  * most_write_completion - write completion handler
1317  * @mbo: pointer to MBO
1318  *
1319  * This recycles the MBO for further usage. In case the channel has been
1320  * poisoned, the MBO is scheduled to be trashed.
1321  */
1322 static void most_write_completion(struct mbo *mbo)
1323 {
1324         struct most_c_obj *c;
1325
1326         BUG_ON((!mbo) || (!mbo->context));
1327
1328         c = mbo->context;
1329         if (mbo->status == MBO_E_INVAL)
1330                 pr_info("WARN: Tx MBO status: invalid\n");
1331         if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1332                 trash_mbo(mbo);
1333         else
1334                 arm_mbo(mbo);
1335 }
1336
1337 /**
1338  * get_channel_by_iface - get pointer to channel object
1339  * @iface: pointer to interface instance
1340  * @id: channel ID
1341  *
1342  * This retrieves a pointer to a channel of the given interface and channel ID.
1343  */
1344 static struct
1345 most_c_obj *get_channel_by_iface(struct most_interface *iface, int id)
1346 {
1347         struct most_inst_obj *i;
1348
1349         if (unlikely(!iface)) {
1350                 pr_err("Bad interface\n");
1351                 return NULL;
1352         }
1353         if (unlikely((id < 0) || (id >= iface->num_channels))) {
1354                 pr_err("Channel index (%d) out of range\n", id);
1355                 return NULL;
1356         }
1357         i = iface->priv;
1358         if (unlikely(!i)) {
1359                 pr_err("interface is not registered\n");
1360                 return NULL;
1361         }
1362         return i->channel[id];
1363 }
1364
1365 int channel_has_mbo(struct most_interface *iface, int id, struct most_aim *aim)
1366 {
1367         struct most_c_obj *c = get_channel_by_iface(iface, id);
1368         unsigned long flags;
1369         int empty;
1370
1371         if (unlikely(!c))
1372                 return -EINVAL;
1373
1374         if (c->aim0.refs && c->aim1.refs &&
1375             ((aim == c->aim0.ptr && c->aim0.num_buffers <= 0) ||
1376              (aim == c->aim1.ptr && c->aim1.num_buffers <= 0)))
1377                 return 0;
1378
1379         spin_lock_irqsave(&c->fifo_lock, flags);
1380         empty = list_empty(&c->fifo);
1381         spin_unlock_irqrestore(&c->fifo_lock, flags);
1382         return !empty;
1383 }
1384 EXPORT_SYMBOL_GPL(channel_has_mbo);
1385
1386 /**
1387  * most_get_mbo - get pointer to an MBO of pool
1388  * @iface: pointer to interface instance
1389  * @id: channel ID
1390  *
1391  * This attempts to get a free buffer out of the channel fifo.
1392  * Returns a pointer to MBO on success or NULL otherwise.
1393  */
1394 struct mbo *most_get_mbo(struct most_interface *iface, int id,
1395                          struct most_aim *aim)
1396 {
1397         struct mbo *mbo;
1398         struct most_c_obj *c;
1399         unsigned long flags;
1400         int *num_buffers_ptr;
1401
1402         c = get_channel_by_iface(iface, id);
1403         if (unlikely(!c))
1404                 return NULL;
1405
1406         if (c->aim0.refs && c->aim1.refs &&
1407             ((aim == c->aim0.ptr && c->aim0.num_buffers <= 0) ||
1408              (aim == c->aim1.ptr && c->aim1.num_buffers <= 0)))
1409                 return NULL;
1410
1411         if (aim == c->aim0.ptr)
1412                 num_buffers_ptr = &c->aim0.num_buffers;
1413         else if (aim == c->aim1.ptr)
1414                 num_buffers_ptr = &c->aim1.num_buffers;
1415         else
1416                 num_buffers_ptr = &dummy_num_buffers;
1417
1418         spin_lock_irqsave(&c->fifo_lock, flags);
1419         if (list_empty(&c->fifo)) {
1420                 spin_unlock_irqrestore(&c->fifo_lock, flags);
1421                 return NULL;
1422         }
1423         mbo = list_pop_mbo(&c->fifo);
1424         --*num_buffers_ptr;
1425         spin_unlock_irqrestore(&c->fifo_lock, flags);
1426
1427         mbo->num_buffers_ptr = num_buffers_ptr;
1428         mbo->buffer_length = c->cfg.buffer_size;
1429         return mbo;
1430 }
1431 EXPORT_SYMBOL_GPL(most_get_mbo);
1432
1433 /**
1434  * most_put_mbo - return buffer to pool
1435  * @mbo: buffer object
1436  */
1437 void most_put_mbo(struct mbo *mbo)
1438 {
1439         struct most_c_obj *c = mbo->context;
1440
1441         if (c->cfg.direction == MOST_CH_TX) {
1442                 arm_mbo(mbo);
1443                 return;
1444         }
1445         nq_hdm_mbo(mbo);
1446         atomic_inc(&c->mbo_nq_level);
1447 }
1448 EXPORT_SYMBOL_GPL(most_put_mbo);
1449
1450 /**
1451  * most_read_completion - read completion handler
1452  * @mbo: pointer to MBO
1453  *
1454  * This function is called by the HDM when data has been received from the
1455  * hardware and copied to the buffer of the MBO.
1456  *
1457  * In case the channel has been poisoned it puts the buffer in the trash queue.
1458  * Otherwise, it passes the buffer to an AIM for further processing.
1459  */
1460 static void most_read_completion(struct mbo *mbo)
1461 {
1462         struct most_c_obj *c = mbo->context;
1463
1464         if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1465                 trash_mbo(mbo);
1466                 return;
1467         }
1468
1469         if (mbo->status == MBO_E_INVAL) {
1470                 nq_hdm_mbo(mbo);
1471                 atomic_inc(&c->mbo_nq_level);
1472                 return;
1473         }
1474
1475         if (atomic_sub_and_test(1, &c->mbo_nq_level))
1476                 c->is_starving = 1;
1477
1478         if (c->aim0.refs && c->aim0.ptr->rx_completion &&
1479             c->aim0.ptr->rx_completion(mbo) == 0)
1480                 return;
1481
1482         if (c->aim1.refs && c->aim1.ptr->rx_completion &&
1483             c->aim1.ptr->rx_completion(mbo) == 0)
1484                 return;
1485
1486         most_put_mbo(mbo);
1487 }
1488
1489 /**
1490  * most_start_channel - prepares a channel for communication
1491  * @iface: pointer to interface instance
1492  * @id: channel ID
1493  *
1494  * This prepares the channel for usage. Cross-checks whether the
1495  * channel's been properly configured.
1496  *
1497  * Returns 0 on success or error code otherwise.
1498  */
1499 int most_start_channel(struct most_interface *iface, int id,
1500                        struct most_aim *aim)
1501 {
1502         int num_buffer;
1503         int ret;
1504         struct most_c_obj *c = get_channel_by_iface(iface, id);
1505
1506         if (unlikely(!c))
1507                 return -EINVAL;
1508
1509         mutex_lock(&c->start_mutex);
1510         if (c->aim0.refs + c->aim1.refs > 0)
1511                 goto out; /* already started by other aim */
1512
1513         if (!try_module_get(iface->mod)) {
1514                 pr_info("failed to acquire HDM lock\n");
1515                 mutex_unlock(&c->start_mutex);
1516                 return -ENOLCK;
1517         }
1518
1519         c->cfg.extra_len = 0;
1520         if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1521                 pr_info("channel configuration failed. Go check settings...\n");
1522                 ret = -EINVAL;
1523                 goto error;
1524         }
1525
1526         init_waitqueue_head(&c->hdm_fifo_wq);
1527
1528         if (c->cfg.direction == MOST_CH_RX)
1529                 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1530                                            most_read_completion);
1531         else
1532                 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1533                                            most_write_completion);
1534         if (unlikely(!num_buffer)) {
1535                 pr_info("failed to allocate memory\n");
1536                 ret = -ENOMEM;
1537                 goto error;
1538         }
1539
1540         ret = run_enqueue_thread(c, id);
1541         if (ret)
1542                 goto error;
1543
1544         c->is_starving = 0;
1545         c->aim0.num_buffers = c->cfg.num_buffers / 2;
1546         c->aim1.num_buffers = c->cfg.num_buffers - c->aim0.num_buffers;
1547         atomic_set(&c->mbo_ref, num_buffer);
1548
1549 out:
1550         if (aim == c->aim0.ptr)
1551                 c->aim0.refs++;
1552         if (aim == c->aim1.ptr)
1553                 c->aim1.refs++;
1554         mutex_unlock(&c->start_mutex);
1555         return 0;
1556
1557 error:
1558         module_put(iface->mod);
1559         mutex_unlock(&c->start_mutex);
1560         return ret;
1561 }
1562 EXPORT_SYMBOL_GPL(most_start_channel);
1563
1564 /**
1565  * most_stop_channel - stops a running channel
1566  * @iface: pointer to interface instance
1567  * @id: channel ID
1568  */
1569 int most_stop_channel(struct most_interface *iface, int id,
1570                       struct most_aim *aim)
1571 {
1572         struct most_c_obj *c;
1573
1574         if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1575                 pr_err("Bad interface or index out of range\n");
1576                 return -EINVAL;
1577         }
1578         c = get_channel_by_iface(iface, id);
1579         if (unlikely(!c))
1580                 return -EINVAL;
1581
1582         mutex_lock(&c->start_mutex);
1583         if (c->aim0.refs + c->aim1.refs >= 2)
1584                 goto out;
1585
1586         if (c->hdm_enqueue_task)
1587                 kthread_stop(c->hdm_enqueue_task);
1588         c->hdm_enqueue_task = NULL;
1589
1590         if (iface->mod)
1591                 module_put(iface->mod);
1592
1593         c->is_poisoned = true;
1594         if (c->iface->poison_channel(c->iface, c->channel_id)) {
1595                 pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1596                        c->iface->description);
1597                 mutex_unlock(&c->start_mutex);
1598                 return -EAGAIN;
1599         }
1600         flush_trash_fifo(c);
1601         flush_channel_fifos(c);
1602
1603 #ifdef CMPL_INTERRUPTIBLE
1604         if (wait_for_completion_interruptible(&c->cleanup)) {
1605                 pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1606                 mutex_unlock(&c->start_mutex);
1607                 return -EINTR;
1608         }
1609 #else
1610         wait_for_completion(&c->cleanup);
1611 #endif
1612         c->is_poisoned = false;
1613
1614 out:
1615         if (aim == c->aim0.ptr)
1616                 c->aim0.refs--;
1617         if (aim == c->aim1.ptr)
1618                 c->aim1.refs--;
1619         mutex_unlock(&c->start_mutex);
1620         return 0;
1621 }
1622 EXPORT_SYMBOL_GPL(most_stop_channel);
1623
1624 /**
1625  * most_register_aim - registers an AIM (driver) with the core
1626  * @aim: instance of AIM to be registered
1627  */
1628 int most_register_aim(struct most_aim *aim)
1629 {
1630         struct most_aim_obj *aim_obj;
1631
1632         if (!aim) {
1633                 pr_err("Bad driver\n");
1634                 return -EINVAL;
1635         }
1636         aim_obj = create_most_aim_obj(aim->name);
1637         if (!aim_obj) {
1638                 pr_info("failed to alloc driver object\n");
1639                 return -ENOMEM;
1640         }
1641         aim_obj->driver = aim;
1642         aim->context = aim_obj;
1643         pr_info("registered new application interfacing module %s\n",
1644                 aim->name);
1645         list_add_tail(&aim_obj->list, &aim_list);
1646         return 0;
1647 }
1648 EXPORT_SYMBOL_GPL(most_register_aim);
1649
1650 /**
1651  * most_deregister_aim - deregisters an AIM (driver) with the core
1652  * @aim: AIM to be removed
1653  */
1654 int most_deregister_aim(struct most_aim *aim)
1655 {
1656         struct most_aim_obj *aim_obj;
1657         struct most_c_obj *c, *tmp;
1658         struct most_inst_obj *i, *i_tmp;
1659
1660         if (!aim) {
1661                 pr_err("Bad driver\n");
1662                 return -EINVAL;
1663         }
1664
1665         aim_obj = aim->context;
1666         if (!aim_obj) {
1667                 pr_info("driver not registered.\n");
1668                 return -EINVAL;
1669         }
1670         list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
1671                 list_for_each_entry_safe(c, tmp, &i->channel_list, list) {
1672                         if (c->aim0.ptr == aim || c->aim1.ptr == aim)
1673                                 aim->disconnect_channel(
1674                                         c->iface, c->channel_id);
1675                         if (c->aim0.ptr == aim)
1676                                 c->aim0.ptr = NULL;
1677                         if (c->aim1.ptr == aim)
1678                                 c->aim1.ptr = NULL;
1679                 }
1680         }
1681         list_del(&aim_obj->list);
1682         destroy_most_aim_obj(aim_obj);
1683         pr_info("deregistering application interfacing module %s\n", aim->name);
1684         return 0;
1685 }
1686 EXPORT_SYMBOL_GPL(most_deregister_aim);
1687
1688 /**
1689  * most_register_interface - registers an interface with core
1690  * @iface: pointer to the instance of the interface description.
1691  *
1692  * Allocates and initializes a new interface instance and all of its channels.
1693  * Returns a pointer to kobject or an error pointer.
1694  */
1695 struct kobject *most_register_interface(struct most_interface *iface)
1696 {
1697         unsigned int i;
1698         int id;
1699         char name[STRING_SIZE];
1700         char channel_name[STRING_SIZE];
1701         struct most_c_obj *c;
1702         struct most_inst_obj *inst;
1703
1704         if (!iface || !iface->enqueue || !iface->configure ||
1705             !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1706                 pr_err("Bad interface or channel overflow\n");
1707                 return ERR_PTR(-EINVAL);
1708         }
1709
1710         id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1711         if (id < 0) {
1712                 pr_info("Failed to alloc mdev ID\n");
1713                 return ERR_PTR(id);
1714         }
1715         snprintf(name, STRING_SIZE, "mdev%d", id);
1716
1717         inst = create_most_inst_obj(name);
1718         if (!inst) {
1719                 pr_info("Failed to allocate interface instance\n");
1720                 ida_simple_remove(&mdev_id, id);
1721                 return ERR_PTR(-ENOMEM);
1722         }
1723
1724         iface->priv = inst;
1725         INIT_LIST_HEAD(&inst->channel_list);
1726         inst->iface = iface;
1727         inst->dev_id = id;
1728         list_add_tail(&inst->list, &instance_list);
1729
1730         for (i = 0; i < iface->num_channels; i++) {
1731                 const char *name_suffix = iface->channel_vector[i].name_suffix;
1732
1733                 if (!name_suffix)
1734                         snprintf(channel_name, STRING_SIZE, "ch%d", i);
1735                 else
1736                         snprintf(channel_name, STRING_SIZE, "%s", name_suffix);
1737
1738                 /* this increments the reference count of this instance */
1739                 c = create_most_c_obj(channel_name, &inst->kobj);
1740                 if (!c)
1741                         goto free_instance;
1742                 inst->channel[i] = c;
1743                 c->is_starving = 0;
1744                 c->iface = iface;
1745                 c->inst = inst;
1746                 c->channel_id = i;
1747                 c->keep_mbo = false;
1748                 c->enqueue_halt = false;
1749                 c->is_poisoned = false;
1750                 c->cfg.direction = 0;
1751                 c->cfg.data_type = 0;
1752                 c->cfg.num_buffers = 0;
1753                 c->cfg.buffer_size = 0;
1754                 c->cfg.subbuffer_size = 0;
1755                 c->cfg.packets_per_xact = 0;
1756                 spin_lock_init(&c->fifo_lock);
1757                 INIT_LIST_HEAD(&c->fifo);
1758                 INIT_LIST_HEAD(&c->trash_fifo);
1759                 INIT_LIST_HEAD(&c->halt_fifo);
1760                 init_completion(&c->cleanup);
1761                 atomic_set(&c->mbo_ref, 0);
1762                 mutex_init(&c->start_mutex);
1763                 mutex_init(&c->nq_mutex);
1764                 list_add_tail(&c->list, &inst->channel_list);
1765         }
1766         pr_info("registered new MOST device mdev%d (%s)\n",
1767                 inst->dev_id, iface->description);
1768         return &inst->kobj;
1769
1770 free_instance:
1771         pr_info("Failed allocate channel(s)\n");
1772         list_del(&inst->list);
1773         ida_simple_remove(&mdev_id, id);
1774         destroy_most_inst_obj(inst);
1775         return ERR_PTR(-ENOMEM);
1776 }
1777 EXPORT_SYMBOL_GPL(most_register_interface);
1778
1779 /**
1780  * most_deregister_interface - deregisters an interface with core
1781  * @iface: pointer to the interface instance description.
1782  *
1783  * Before removing an interface instance from the list, all running
1784  * channels are stopped and poisoned.
1785  */
1786 void most_deregister_interface(struct most_interface *iface)
1787 {
1788         struct most_inst_obj *i = iface->priv;
1789         struct most_c_obj *c;
1790
1791         if (unlikely(!i)) {
1792                 pr_info("Bad Interface\n");
1793                 return;
1794         }
1795         pr_info("deregistering MOST device %s (%s)\n", i->kobj.name,
1796                 iface->description);
1797
1798         list_for_each_entry(c, &i->channel_list, list) {
1799                 if (c->aim0.ptr)
1800                         c->aim0.ptr->disconnect_channel(c->iface,
1801                                                         c->channel_id);
1802                 if (c->aim1.ptr)
1803                         c->aim1.ptr->disconnect_channel(c->iface,
1804                                                         c->channel_id);
1805                 c->aim0.ptr = NULL;
1806                 c->aim1.ptr = NULL;
1807         }
1808
1809         ida_simple_remove(&mdev_id, i->dev_id);
1810         list_del(&i->list);
1811         destroy_most_inst_obj(i);
1812 }
1813 EXPORT_SYMBOL_GPL(most_deregister_interface);
1814
1815 /**
1816  * most_stop_enqueue - prevents core from enqueueing MBOs
1817  * @iface: pointer to interface
1818  * @id: channel id
1819  *
1820  * This is called by an HDM that _cannot_ attend to its duties and
1821  * is imminent to get run over by the core. The core is not going to
1822  * enqueue any further packets unless the flagging HDM calls
1823  * most_resume enqueue().
1824  */
1825 void most_stop_enqueue(struct most_interface *iface, int id)
1826 {
1827         struct most_c_obj *c = get_channel_by_iface(iface, id);
1828
1829         if (!c)
1830                 return;
1831
1832         mutex_lock(&c->nq_mutex);
1833         c->enqueue_halt = true;
1834         mutex_unlock(&c->nq_mutex);
1835 }
1836 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1837
1838 /**
1839  * most_resume_enqueue - allow core to enqueue MBOs again
1840  * @iface: pointer to interface
1841  * @id: channel id
1842  *
1843  * This clears the enqueue halt flag and enqueues all MBOs currently
1844  * sitting in the wait fifo.
1845  */
1846 void most_resume_enqueue(struct most_interface *iface, int id)
1847 {
1848         struct most_c_obj *c = get_channel_by_iface(iface, id);
1849
1850         if (!c)
1851                 return;
1852
1853         mutex_lock(&c->nq_mutex);
1854         c->enqueue_halt = false;
1855         mutex_unlock(&c->nq_mutex);
1856
1857         wake_up_interruptible(&c->hdm_fifo_wq);
1858 }
1859 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1860
1861 static int __init most_init(void)
1862 {
1863         int err;
1864
1865         pr_info("init()\n");
1866         INIT_LIST_HEAD(&instance_list);
1867         INIT_LIST_HEAD(&aim_list);
1868         ida_init(&mdev_id);
1869
1870         err = bus_register(&most_bus);
1871         if (err) {
1872                 pr_info("Cannot register most bus\n");
1873                 return err;
1874         }
1875
1876         most_class = class_create(THIS_MODULE, "most");
1877         if (IS_ERR(most_class)) {
1878                 pr_info("No udev support.\n");
1879                 err = PTR_ERR(most_class);
1880                 goto exit_bus;
1881         }
1882
1883         err = driver_register(&mostcore);
1884         if (err) {
1885                 pr_info("Cannot register core driver\n");
1886                 goto exit_class;
1887         }
1888
1889         core_dev = device_create(most_class, NULL, 0, NULL, "mostcore");
1890         if (IS_ERR(core_dev)) {
1891                 err = PTR_ERR(core_dev);
1892                 goto exit_driver;
1893         }
1894
1895         most_aim_kset = kset_create_and_add("aims", NULL, &core_dev->kobj);
1896         if (!most_aim_kset) {
1897                 err = -ENOMEM;
1898                 goto exit_class_container;
1899         }
1900
1901         most_inst_kset = kset_create_and_add("devices", NULL, &core_dev->kobj);
1902         if (!most_inst_kset) {
1903                 err = -ENOMEM;
1904                 goto exit_driver_kset;
1905         }
1906
1907         return 0;
1908
1909 exit_driver_kset:
1910         kset_unregister(most_aim_kset);
1911 exit_class_container:
1912         device_destroy(most_class, 0);
1913 exit_driver:
1914         driver_unregister(&mostcore);
1915 exit_class:
1916         class_destroy(most_class);
1917 exit_bus:
1918         bus_unregister(&most_bus);
1919         return err;
1920 }
1921
1922 static void __exit most_exit(void)
1923 {
1924         struct most_inst_obj *i, *i_tmp;
1925         struct most_aim_obj *d, *d_tmp;
1926
1927         pr_info("exit core module\n");
1928         list_for_each_entry_safe(d, d_tmp, &aim_list, list) {
1929                 destroy_most_aim_obj(d);
1930         }
1931
1932         list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
1933                 list_del(&i->list);
1934                 destroy_most_inst_obj(i);
1935         }
1936         kset_unregister(most_inst_kset);
1937         kset_unregister(most_aim_kset);
1938         device_destroy(most_class, 0);
1939         driver_unregister(&mostcore);
1940         class_destroy(most_class);
1941         bus_unregister(&most_bus);
1942         ida_destroy(&mdev_id);
1943 }
1944
1945 module_init(most_init);
1946 module_exit(most_exit);
1947 MODULE_LICENSE("GPL");
1948 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1949 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");