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