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