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