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