linux/drivers/staging/most/core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * core.c - Implementation of core module of MOST Linux driver stack
   4 *
   5 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
   6 */
   7
   8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   9#include <linux/module.h>
  10#include <linux/fs.h>
  11#include <linux/slab.h>
  12#include <linux/init.h>
  13#include <linux/device.h>
  14#include <linux/list.h>
  15#include <linux/poll.h>
  16#include <linux/wait.h>
  17#include <linux/kobject.h>
  18#include <linux/mutex.h>
  19#include <linux/completion.h>
  20#include <linux/sysfs.h>
  21#include <linux/kthread.h>
  22#include <linux/dma-mapping.h>
  23#include <linux/idr.h>
  24#include <most/core.h>
  25
  26#define MAX_CHANNELS    64
  27#define STRING_SIZE     80
  28
  29static struct ida mdev_id;
  30static int dummy_num_buffers;
  31
  32static struct mostcore {
  33        struct device dev;
  34        struct device_driver drv;
  35        struct bus_type bus;
  36        struct list_head comp_list;
  37} mc;
  38
  39#define to_driver(d) container_of(d, struct mostcore, drv)
  40
  41struct pipe {
  42        struct core_component *comp;
  43        int refs;
  44        int num_buffers;
  45};
  46
  47struct most_channel {
  48        struct device dev;
  49        struct completion cleanup;
  50        atomic_t mbo_ref;
  51        atomic_t mbo_nq_level;
  52        u16 channel_id;
  53        char name[STRING_SIZE];
  54        bool is_poisoned;
  55        struct mutex start_mutex;
  56        struct mutex nq_mutex; /* nq thread synchronization */
  57        int is_starving;
  58        struct most_interface *iface;
  59        struct most_channel_config cfg;
  60        bool keep_mbo;
  61        bool enqueue_halt;
  62        struct list_head fifo;
  63        spinlock_t fifo_lock;
  64        struct list_head halt_fifo;
  65        struct list_head list;
  66        struct pipe pipe0;
  67        struct pipe pipe1;
  68        struct list_head trash_fifo;
  69        struct task_struct *hdm_enqueue_task;
  70        wait_queue_head_t hdm_fifo_wq;
  71
  72};
  73
  74#define to_channel(d) container_of(d, struct most_channel, dev)
  75
  76struct interface_private {
  77        int dev_id;
  78        char name[STRING_SIZE];
  79        struct most_channel *channel[MAX_CHANNELS];
  80        struct list_head channel_list;
  81};
  82
  83static const struct {
  84        int most_ch_data_type;
  85        const char *name;
  86} ch_data_type[] = {
  87        { MOST_CH_CONTROL, "control\n" },
  88        { MOST_CH_ASYNC, "async\n" },
  89        { MOST_CH_SYNC, "sync\n" },
  90        { MOST_CH_ISOC, "isoc\n"},
  91        { MOST_CH_ISOC, "isoc_avp\n"},
  92};
  93
  94/**
  95 * list_pop_mbo - retrieves the first MBO of the list and removes it
  96 * @ptr: the list head to grab the MBO from.
  97 */
  98#define list_pop_mbo(ptr)                                               \
  99({                                                                      \
 100        struct mbo *_mbo = list_first_entry(ptr, struct mbo, list);     \
 101        list_del(&_mbo->list);                                          \
 102        _mbo;                                                           \
 103})
 104
 105/**
 106 * most_free_mbo_coherent - free an MBO and its coherent buffer
 107 * @mbo: most buffer
 108 */
 109static void most_free_mbo_coherent(struct mbo *mbo)
 110{
 111        struct most_channel *c = mbo->context;
 112        u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
 113
 114        if (c->iface->dma_free)
 115                c->iface->dma_free(mbo, coherent_buf_size);
 116        else
 117                kfree(mbo->virt_address);
 118        kfree(mbo);
 119        if (atomic_sub_and_test(1, &c->mbo_ref))
 120                complete(&c->cleanup);
 121}
 122
 123/**
 124 * flush_channel_fifos - clear the channel fifos
 125 * @c: pointer to channel object
 126 */
 127static void flush_channel_fifos(struct most_channel *c)
 128{
 129        unsigned long flags, hf_flags;
 130        struct mbo *mbo, *tmp;
 131
 132        if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
 133                return;
 134
 135        spin_lock_irqsave(&c->fifo_lock, flags);
 136        list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
 137                list_del(&mbo->list);
 138                spin_unlock_irqrestore(&c->fifo_lock, flags);
 139                most_free_mbo_coherent(mbo);
 140                spin_lock_irqsave(&c->fifo_lock, flags);
 141        }
 142        spin_unlock_irqrestore(&c->fifo_lock, flags);
 143
 144        spin_lock_irqsave(&c->fifo_lock, hf_flags);
 145        list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
 146                list_del(&mbo->list);
 147                spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
 148                most_free_mbo_coherent(mbo);
 149                spin_lock_irqsave(&c->fifo_lock, hf_flags);
 150        }
 151        spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
 152
 153        if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
 154                pr_info("WARN: fifo | trash fifo not empty\n");
 155}
 156
 157/**
 158 * flush_trash_fifo - clear the trash fifo
 159 * @c: pointer to channel object
 160 */
 161static int flush_trash_fifo(struct most_channel *c)
 162{
 163        struct mbo *mbo, *tmp;
 164        unsigned long flags;
 165
 166        spin_lock_irqsave(&c->fifo_lock, flags);
 167        list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
 168                list_del(&mbo->list);
 169                spin_unlock_irqrestore(&c->fifo_lock, flags);
 170                most_free_mbo_coherent(mbo);
 171                spin_lock_irqsave(&c->fifo_lock, flags);
 172        }
 173        spin_unlock_irqrestore(&c->fifo_lock, flags);
 174        return 0;
 175}
 176
 177static ssize_t available_directions_show(struct device *dev,
 178                                         struct device_attribute *attr,
 179                                         char *buf)
 180{
 181        struct most_channel *c = to_channel(dev);
 182        unsigned int i = c->channel_id;
 183
 184        strcpy(buf, "");
 185        if (c->iface->channel_vector[i].direction & MOST_CH_RX)
 186                strcat(buf, "rx ");
 187        if (c->iface->channel_vector[i].direction & MOST_CH_TX)
 188                strcat(buf, "tx ");
 189        strcat(buf, "\n");
 190        return strlen(buf);
 191}
 192
 193static ssize_t available_datatypes_show(struct device *dev,
 194                                        struct device_attribute *attr,
 195                                        char *buf)
 196{
 197        struct most_channel *c = to_channel(dev);
 198        unsigned int i = c->channel_id;
 199
 200        strcpy(buf, "");
 201        if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
 202                strcat(buf, "control ");
 203        if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
 204                strcat(buf, "async ");
 205        if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
 206                strcat(buf, "sync ");
 207        if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
 208                strcat(buf, "isoc ");
 209        strcat(buf, "\n");
 210        return strlen(buf);
 211}
 212
 213static ssize_t number_of_packet_buffers_show(struct device *dev,
 214                                             struct device_attribute *attr,
 215                                             char *buf)
 216{
 217        struct most_channel *c = to_channel(dev);
 218        unsigned int i = c->channel_id;
 219
 220        return snprintf(buf, PAGE_SIZE, "%d\n",
 221                        c->iface->channel_vector[i].num_buffers_packet);
 222}
 223
 224static ssize_t number_of_stream_buffers_show(struct device *dev,
 225                                             struct device_attribute *attr,
 226                                             char *buf)
 227{
 228        struct most_channel *c = to_channel(dev);
 229        unsigned int i = c->channel_id;
 230
 231        return snprintf(buf, PAGE_SIZE, "%d\n",
 232                        c->iface->channel_vector[i].num_buffers_streaming);
 233}
 234
 235static ssize_t size_of_packet_buffer_show(struct device *dev,
 236                                          struct device_attribute *attr,
 237                                          char *buf)
 238{
 239        struct most_channel *c = to_channel(dev);
 240        unsigned int i = c->channel_id;
 241
 242        return snprintf(buf, PAGE_SIZE, "%d\n",
 243                        c->iface->channel_vector[i].buffer_size_packet);
 244}
 245
 246static ssize_t size_of_stream_buffer_show(struct device *dev,
 247                                          struct device_attribute *attr,
 248                                          char *buf)
 249{
 250        struct most_channel *c = to_channel(dev);
 251        unsigned int i = c->channel_id;
 252
 253        return snprintf(buf, PAGE_SIZE, "%d\n",
 254                        c->iface->channel_vector[i].buffer_size_streaming);
 255}
 256
 257static ssize_t channel_starving_show(struct device *dev,
 258                                     struct device_attribute *attr,
 259                                     char *buf)
 260{
 261        struct most_channel *c = to_channel(dev);
 262
 263        return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
 264}
 265
 266static ssize_t set_number_of_buffers_show(struct device *dev,
 267                                          struct device_attribute *attr,
 268                                          char *buf)
 269{
 270        struct most_channel *c = to_channel(dev);
 271
 272        return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
 273}
 274
 275static ssize_t set_buffer_size_show(struct device *dev,
 276                                    struct device_attribute *attr,
 277                                    char *buf)
 278{
 279        struct most_channel *c = to_channel(dev);
 280
 281        return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
 282}
 283
 284static ssize_t set_direction_show(struct device *dev,
 285                                  struct device_attribute *attr,
 286                                  char *buf)
 287{
 288        struct most_channel *c = to_channel(dev);
 289
 290        if (c->cfg.direction & MOST_CH_TX)
 291                return snprintf(buf, PAGE_SIZE, "tx\n");
 292        else if (c->cfg.direction & MOST_CH_RX)
 293                return snprintf(buf, PAGE_SIZE, "rx\n");
 294        return snprintf(buf, PAGE_SIZE, "unconfigured\n");
 295}
 296
 297static ssize_t set_datatype_show(struct device *dev,
 298                                 struct device_attribute *attr,
 299                                 char *buf)
 300{
 301        int i;
 302        struct most_channel *c = to_channel(dev);
 303
 304        for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
 305                if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
 306                        return snprintf(buf, PAGE_SIZE, "%s", ch_data_type[i].name);
 307        }
 308        return snprintf(buf, PAGE_SIZE, "unconfigured\n");
 309}
 310
 311static ssize_t set_subbuffer_size_show(struct device *dev,
 312                                       struct device_attribute *attr,
 313                                       char *buf)
 314{
 315        struct most_channel *c = to_channel(dev);
 316
 317        return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
 318}
 319
 320static ssize_t set_packets_per_xact_show(struct device *dev,
 321                                         struct device_attribute *attr,
 322                                         char *buf)
 323{
 324        struct most_channel *c = to_channel(dev);
 325
 326        return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
 327}
 328
 329static ssize_t set_dbr_size_show(struct device *dev,
 330                                 struct device_attribute *attr, char *buf)
 331{
 332        struct most_channel *c = to_channel(dev);
 333
 334        return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
 335}
 336
 337#define to_dev_attr(a) container_of(a, struct device_attribute, attr)
 338static umode_t channel_attr_is_visible(struct kobject *kobj,
 339                                       struct attribute *attr, int index)
 340{
 341        struct device_attribute *dev_attr = to_dev_attr(attr);
 342        struct device *dev = kobj_to_dev(kobj);
 343        struct most_channel *c = to_channel(dev);
 344
 345        if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
 346            (c->iface->interface != ITYPE_MEDIALB_DIM2))
 347                return 0;
 348        if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
 349            (c->iface->interface != ITYPE_USB))
 350                return 0;
 351
 352        return attr->mode;
 353}
 354
 355#define DEV_ATTR(_name)  (&dev_attr_##_name.attr)
 356
 357static DEVICE_ATTR_RO(available_directions);
 358static DEVICE_ATTR_RO(available_datatypes);
 359static DEVICE_ATTR_RO(number_of_packet_buffers);
 360static DEVICE_ATTR_RO(number_of_stream_buffers);
 361static DEVICE_ATTR_RO(size_of_stream_buffer);
 362static DEVICE_ATTR_RO(size_of_packet_buffer);
 363static DEVICE_ATTR_RO(channel_starving);
 364static DEVICE_ATTR_RO(set_buffer_size);
 365static DEVICE_ATTR_RO(set_number_of_buffers);
 366static DEVICE_ATTR_RO(set_direction);
 367static DEVICE_ATTR_RO(set_datatype);
 368static DEVICE_ATTR_RO(set_subbuffer_size);
 369static DEVICE_ATTR_RO(set_packets_per_xact);
 370static DEVICE_ATTR_RO(set_dbr_size);
 371
 372static struct attribute *channel_attrs[] = {
 373        DEV_ATTR(available_directions),
 374        DEV_ATTR(available_datatypes),
 375        DEV_ATTR(number_of_packet_buffers),
 376        DEV_ATTR(number_of_stream_buffers),
 377        DEV_ATTR(size_of_stream_buffer),
 378        DEV_ATTR(size_of_packet_buffer),
 379        DEV_ATTR(channel_starving),
 380        DEV_ATTR(set_buffer_size),
 381        DEV_ATTR(set_number_of_buffers),
 382        DEV_ATTR(set_direction),
 383        DEV_ATTR(set_datatype),
 384        DEV_ATTR(set_subbuffer_size),
 385        DEV_ATTR(set_packets_per_xact),
 386        DEV_ATTR(set_dbr_size),
 387        NULL,
 388};
 389
 390static struct attribute_group channel_attr_group = {
 391        .attrs = channel_attrs,
 392        .is_visible = channel_attr_is_visible,
 393};
 394
 395static const struct attribute_group *channel_attr_groups[] = {
 396        &channel_attr_group,
 397        NULL,
 398};
 399
 400static ssize_t description_show(struct device *dev,
 401                                struct device_attribute *attr,
 402                                char *buf)
 403{
 404        struct most_interface *iface = to_most_interface(dev);
 405
 406        return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
 407}
 408
 409static ssize_t interface_show(struct device *dev,
 410                              struct device_attribute *attr,
 411                              char *buf)
 412{
 413        struct most_interface *iface = to_most_interface(dev);
 414
 415        switch (iface->interface) {
 416        case ITYPE_LOOPBACK:
 417                return snprintf(buf, PAGE_SIZE, "loopback\n");
 418        case ITYPE_I2C:
 419                return snprintf(buf, PAGE_SIZE, "i2c\n");
 420        case ITYPE_I2S:
 421                return snprintf(buf, PAGE_SIZE, "i2s\n");
 422        case ITYPE_TSI:
 423                return snprintf(buf, PAGE_SIZE, "tsi\n");
 424        case ITYPE_HBI:
 425                return snprintf(buf, PAGE_SIZE, "hbi\n");
 426        case ITYPE_MEDIALB_DIM:
 427                return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
 428        case ITYPE_MEDIALB_DIM2:
 429                return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
 430        case ITYPE_USB:
 431                return snprintf(buf, PAGE_SIZE, "usb\n");
 432        case ITYPE_PCIE:
 433                return snprintf(buf, PAGE_SIZE, "pcie\n");
 434        }
 435        return snprintf(buf, PAGE_SIZE, "unknown\n");
 436}
 437
 438static DEVICE_ATTR_RO(description);
 439static DEVICE_ATTR_RO(interface);
 440
 441static struct attribute *interface_attrs[] = {
 442        DEV_ATTR(description),
 443        DEV_ATTR(interface),
 444        NULL,
 445};
 446
 447static struct attribute_group interface_attr_group = {
 448        .attrs = interface_attrs,
 449};
 450
 451static const struct attribute_group *interface_attr_groups[] = {
 452        &interface_attr_group,
 453        NULL,
 454};
 455
 456static struct core_component *match_component(char *name)
 457{
 458        struct core_component *comp;
 459
 460        list_for_each_entry(comp, &mc.comp_list, list) {
 461                if (!strcmp(comp->name, name))
 462                        return comp;
 463        }
 464        return NULL;
 465}
 466
 467struct show_links_data {
 468        int offs;
 469        char *buf;
 470};
 471
 472static int print_links(struct device *dev, void *data)
 473{
 474        struct show_links_data *d = data;
 475        int offs = d->offs;
 476        char *buf = d->buf;
 477        struct most_channel *c;
 478        struct most_interface *iface = to_most_interface(dev);
 479
 480        list_for_each_entry(c, &iface->p->channel_list, list) {
 481                if (c->pipe0.comp) {
 482                        offs += snprintf(buf + offs,
 483                                         PAGE_SIZE - offs,
 484                                         "%s:%s:%s\n",
 485                                         c->pipe0.comp->name,
 486                                         dev_name(&iface->dev),
 487                                         dev_name(&c->dev));
 488                }
 489                if (c->pipe1.comp) {
 490                        offs += snprintf(buf + offs,
 491                                         PAGE_SIZE - offs,
 492                                         "%s:%s:%s\n",
 493                                         c->pipe1.comp->name,
 494                                         dev_name(&iface->dev),
 495                                         dev_name(&c->dev));
 496                }
 497        }
 498        d->offs = offs;
 499        return 0;
 500}
 501
 502static ssize_t links_show(struct device_driver *drv, char *buf)
 503{
 504        struct show_links_data d = { .buf = buf };
 505
 506        bus_for_each_dev(&mc.bus, NULL, &d, print_links);
 507        return d.offs;
 508}
 509
 510static ssize_t components_show(struct device_driver *drv, char *buf)
 511{
 512        struct core_component *comp;
 513        int offs = 0;
 514
 515        list_for_each_entry(comp, &mc.comp_list, list) {
 516                offs += snprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
 517                                 comp->name);
 518        }
 519        return offs;
 520}
 521
 522/**
 523 * split_string - parses buf and extracts ':' separated substrings.
 524 *
 525 * @buf: complete string from attribute 'add_channel'
 526 * @a: storage for 1st substring (=interface name)
 527 * @b: storage for 2nd substring (=channel name)
 528 * @c: storage for 3rd substring (=component name)
 529 * @d: storage optional 4th substring (=user defined name)
 530 *
 531 * Examples:
 532 *
 533 * Input: "mdev0:ch6:cdev:my_channel\n" or
 534 *        "mdev0:ch6:cdev:my_channel"
 535 *
 536 * Output: *a -> "mdev0", *b -> "ch6", *c -> "cdev" *d -> "my_channel"
 537 *
 538 * Input: "mdev1:ep81:cdev\n"
 539 * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d -> ""
 540 *
 541 * Input: "mdev1:ep81"
 542 * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d == NULL
 543 */
 544static int split_string(char *buf, char **a, char **b, char **c, char **d)
 545{
 546        *a = strsep(&buf, ":");
 547        if (!*a)
 548                return -EIO;
 549
 550        *b = strsep(&buf, ":\n");
 551        if (!*b)
 552                return -EIO;
 553
 554        *c = strsep(&buf, ":\n");
 555        if (!*c)
 556                return -EIO;
 557
 558        if (d)
 559                *d = strsep(&buf, ":\n");
 560
 561        return 0;
 562}
 563
 564/**
 565 * get_channel - get pointer to channel
 566 * @mdev: name of the device interface
 567 * @mdev_ch: name of channel
 568 */
 569static struct most_channel *get_channel(char *mdev, char *mdev_ch)
 570{
 571        struct device *dev = NULL;
 572        struct most_interface *iface;
 573        struct most_channel *c, *tmp;
 574
 575        dev = bus_find_device_by_name(&mc.bus, NULL, mdev);
 576        if (!dev)
 577                return NULL;
 578        iface = to_most_interface(dev);
 579        list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
 580                if (!strcmp(dev_name(&c->dev), mdev_ch))
 581                        return c;
 582        }
 583        return NULL;
 584}
 585
 586static
 587inline int link_channel_to_component(struct most_channel *c,
 588                                     struct core_component *comp,
 589                                     char *name,
 590                                     char *comp_param)
 591{
 592        int ret;
 593        struct core_component **comp_ptr;
 594
 595        if (!c->pipe0.comp)
 596                comp_ptr = &c->pipe0.comp;
 597        else if (!c->pipe1.comp)
 598                comp_ptr = &c->pipe1.comp;
 599        else
 600                return -ENOSPC;
 601
 602        *comp_ptr = comp;
 603        ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, name,
 604                                  comp_param);
 605        if (ret) {
 606                *comp_ptr = NULL;
 607                return ret;
 608        }
 609        return 0;
 610}
 611
 612int most_set_cfg_buffer_size(char *mdev, char *mdev_ch, u16 val)
 613{
 614        struct most_channel *c = get_channel(mdev, mdev_ch);
 615
 616        if (!c)
 617                return -ENODEV;
 618        c->cfg.buffer_size = val;
 619        return 0;
 620}
 621
 622int most_set_cfg_subbuffer_size(char *mdev, char *mdev_ch, u16 val)
 623{
 624        struct most_channel *c = get_channel(mdev, mdev_ch);
 625
 626        if (!c)
 627                return -ENODEV;
 628        c->cfg.subbuffer_size = val;
 629        return 0;
 630}
 631
 632int most_set_cfg_dbr_size(char *mdev, char *mdev_ch, u16 val)
 633{
 634        struct most_channel *c = get_channel(mdev, mdev_ch);
 635
 636        if (!c)
 637                return -ENODEV;
 638        c->cfg.dbr_size = val;
 639        return 0;
 640}
 641
 642int most_set_cfg_num_buffers(char *mdev, char *mdev_ch, u16 val)
 643{
 644        struct most_channel *c = get_channel(mdev, mdev_ch);
 645
 646        if (!c)
 647                return -ENODEV;
 648        c->cfg.num_buffers = val;
 649        return 0;
 650}
 651
 652int most_set_cfg_datatype(char *mdev, char *mdev_ch, char *buf)
 653{
 654        int i;
 655        struct most_channel *c = get_channel(mdev, mdev_ch);
 656
 657        if (!c)
 658                return -ENODEV;
 659        for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
 660                if (!strcmp(buf, ch_data_type[i].name)) {
 661                        c->cfg.data_type = ch_data_type[i].most_ch_data_type;
 662                        break;
 663                }
 664        }
 665
 666        if (i == ARRAY_SIZE(ch_data_type))
 667                pr_info("WARN: invalid attribute settings\n");
 668        return 0;
 669}
 670
 671int most_set_cfg_direction(char *mdev, char *mdev_ch, char *buf)
 672{
 673        struct most_channel *c = get_channel(mdev, mdev_ch);
 674
 675        if (!c)
 676                return -ENODEV;
 677        if (!strcmp(buf, "dir_rx\n")) {
 678                c->cfg.direction = MOST_CH_RX;
 679        } else if (!strcmp(buf, "rx\n")) {
 680                c->cfg.direction = MOST_CH_RX;
 681        } else if (!strcmp(buf, "dir_tx\n")) {
 682                c->cfg.direction = MOST_CH_TX;
 683        } else if (!strcmp(buf, "tx\n")) {
 684                c->cfg.direction = MOST_CH_TX;
 685        } else {
 686                pr_info("Invalid direction\n");
 687                return -ENODATA;
 688        }
 689        return 0;
 690}
 691
 692int most_set_cfg_packets_xact(char *mdev, char *mdev_ch, u16 val)
 693{
 694        struct most_channel *c = get_channel(mdev, mdev_ch);
 695
 696        if (!c)
 697                return -ENODEV;
 698        c->cfg.packets_per_xact = val;
 699        return 0;
 700}
 701
 702int most_cfg_complete(char *comp_name)
 703{
 704        struct core_component *comp;
 705
 706        comp = match_component(comp_name);
 707        if (!comp)
 708                return -ENODEV;
 709
 710        return comp->cfg_complete();
 711}
 712
 713int most_add_link(char *mdev, char *mdev_ch, char *comp_name, char *link_name,
 714                  char *comp_param)
 715{
 716        struct most_channel *c = get_channel(mdev, mdev_ch);
 717        struct core_component *comp = match_component(comp_name);
 718
 719        if (!c || !comp)
 720                return -ENODEV;
 721
 722        return link_channel_to_component(c, comp, link_name, comp_param);
 723}
 724/**
 725 * remove_link_store - store function for remove_link attribute
 726 * @drv: device driver
 727 * @buf: buffer
 728 * @len: buffer length
 729 *
 730 * Example:
 731 * echo "mdev0:ep81" >remove_link
 732 */
 733static ssize_t remove_link_store(struct device_driver *drv,
 734                                 const char *buf,
 735                                 size_t len)
 736{
 737        struct most_channel *c;
 738        struct core_component *comp;
 739        char buffer[STRING_SIZE];
 740        char *mdev;
 741        char *mdev_ch;
 742        char *comp_name;
 743        int ret;
 744        size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
 745
 746        strlcpy(buffer, buf, max_len);
 747        ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, NULL);
 748        if (ret)
 749                return ret;
 750        comp = match_component(comp_name);
 751        if (!comp)
 752                return -ENODEV;
 753        c = get_channel(mdev, mdev_ch);
 754        if (!c)
 755                return -ENODEV;
 756
 757        if (comp->disconnect_channel(c->iface, c->channel_id))
 758                return -EIO;
 759        if (c->pipe0.comp == comp)
 760                c->pipe0.comp = NULL;
 761        if (c->pipe1.comp == comp)
 762                c->pipe1.comp = NULL;
 763        return len;
 764}
 765
 766int most_remove_link(char *mdev, char *mdev_ch, char *comp_name)
 767{
 768        struct most_channel *c;
 769        struct core_component *comp;
 770
 771        comp = match_component(comp_name);
 772        if (!comp)
 773                return -ENODEV;
 774        c = get_channel(mdev, mdev_ch);
 775        if (!c)
 776                return -ENODEV;
 777
 778        if (comp->disconnect_channel(c->iface, c->channel_id))
 779                return -EIO;
 780        if (c->pipe0.comp == comp)
 781                c->pipe0.comp = NULL;
 782        if (c->pipe1.comp == comp)
 783                c->pipe1.comp = NULL;
 784        return 0;
 785}
 786
 787#define DRV_ATTR(_name)  (&driver_attr_##_name.attr)
 788
 789static DRIVER_ATTR_RO(links);
 790static DRIVER_ATTR_RO(components);
 791static DRIVER_ATTR_WO(remove_link);
 792
 793static struct attribute *mc_attrs[] = {
 794        DRV_ATTR(links),
 795        DRV_ATTR(components),
 796        DRV_ATTR(remove_link),
 797        NULL,
 798};
 799
 800static struct attribute_group mc_attr_group = {
 801        .attrs = mc_attrs,
 802};
 803
 804static const struct attribute_group *mc_attr_groups[] = {
 805        &mc_attr_group,
 806        NULL,
 807};
 808
 809static int most_match(struct device *dev, struct device_driver *drv)
 810{
 811        if (!strcmp(dev_name(dev), "most"))
 812                return 0;
 813        else
 814                return 1;
 815}
 816
 817static inline void trash_mbo(struct mbo *mbo)
 818{
 819        unsigned long flags;
 820        struct most_channel *c = mbo->context;
 821
 822        spin_lock_irqsave(&c->fifo_lock, flags);
 823        list_add(&mbo->list, &c->trash_fifo);
 824        spin_unlock_irqrestore(&c->fifo_lock, flags);
 825}
 826
 827static bool hdm_mbo_ready(struct most_channel *c)
 828{
 829        bool empty;
 830
 831        if (c->enqueue_halt)
 832                return false;
 833
 834        spin_lock_irq(&c->fifo_lock);
 835        empty = list_empty(&c->halt_fifo);
 836        spin_unlock_irq(&c->fifo_lock);
 837
 838        return !empty;
 839}
 840
 841static void nq_hdm_mbo(struct mbo *mbo)
 842{
 843        unsigned long flags;
 844        struct most_channel *c = mbo->context;
 845
 846        spin_lock_irqsave(&c->fifo_lock, flags);
 847        list_add_tail(&mbo->list, &c->halt_fifo);
 848        spin_unlock_irqrestore(&c->fifo_lock, flags);
 849        wake_up_interruptible(&c->hdm_fifo_wq);
 850}
 851
 852static int hdm_enqueue_thread(void *data)
 853{
 854        struct most_channel *c = data;
 855        struct mbo *mbo;
 856        int ret;
 857        typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
 858
 859        while (likely(!kthread_should_stop())) {
 860                wait_event_interruptible(c->hdm_fifo_wq,
 861                                         hdm_mbo_ready(c) ||
 862                                         kthread_should_stop());
 863
 864                mutex_lock(&c->nq_mutex);
 865                spin_lock_irq(&c->fifo_lock);
 866                if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
 867                        spin_unlock_irq(&c->fifo_lock);
 868                        mutex_unlock(&c->nq_mutex);
 869                        continue;
 870                }
 871
 872                mbo = list_pop_mbo(&c->halt_fifo);
 873                spin_unlock_irq(&c->fifo_lock);
 874
 875                if (c->cfg.direction == MOST_CH_RX)
 876                        mbo->buffer_length = c->cfg.buffer_size;
 877
 878                ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
 879                mutex_unlock(&c->nq_mutex);
 880
 881                if (unlikely(ret)) {
 882                        pr_err("hdm enqueue failed\n");
 883                        nq_hdm_mbo(mbo);
 884                        c->hdm_enqueue_task = NULL;
 885                        return 0;
 886                }
 887        }
 888
 889        return 0;
 890}
 891
 892static int run_enqueue_thread(struct most_channel *c, int channel_id)
 893{
 894        struct task_struct *task =
 895                kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
 896                            channel_id);
 897
 898        if (IS_ERR(task))
 899                return PTR_ERR(task);
 900
 901        c->hdm_enqueue_task = task;
 902        return 0;
 903}
 904
 905/**
 906 * arm_mbo - recycle MBO for further usage
 907 * @mbo: most buffer
 908 *
 909 * This puts an MBO back to the list to have it ready for up coming
 910 * tx transactions.
 911 *
 912 * In case the MBO belongs to a channel that recently has been
 913 * poisoned, the MBO is scheduled to be trashed.
 914 * Calls the completion handler of an attached component.
 915 */
 916static void arm_mbo(struct mbo *mbo)
 917{
 918        unsigned long flags;
 919        struct most_channel *c;
 920
 921        c = mbo->context;
 922
 923        if (c->is_poisoned) {
 924                trash_mbo(mbo);
 925                return;
 926        }
 927
 928        spin_lock_irqsave(&c->fifo_lock, flags);
 929        ++*mbo->num_buffers_ptr;
 930        list_add_tail(&mbo->list, &c->fifo);
 931        spin_unlock_irqrestore(&c->fifo_lock, flags);
 932
 933        if (c->pipe0.refs && c->pipe0.comp->tx_completion)
 934                c->pipe0.comp->tx_completion(c->iface, c->channel_id);
 935
 936        if (c->pipe1.refs && c->pipe1.comp->tx_completion)
 937                c->pipe1.comp->tx_completion(c->iface, c->channel_id);
 938}
 939
 940/**
 941 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
 942 * @c: pointer to interface channel
 943 * @dir: direction of the channel
 944 * @compl: pointer to completion function
 945 *
 946 * This allocates buffer objects including the containing DMA coherent
 947 * buffer and puts them in the fifo.
 948 * Buffers of Rx channels are put in the kthread fifo, hence immediately
 949 * submitted to the HDM.
 950 *
 951 * Returns the number of allocated and enqueued MBOs.
 952 */
 953static int arm_mbo_chain(struct most_channel *c, int dir,
 954                         void (*compl)(struct mbo *))
 955{
 956        unsigned int i;
 957        struct mbo *mbo;
 958        unsigned long flags;
 959        u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
 960
 961        atomic_set(&c->mbo_nq_level, 0);
 962
 963        for (i = 0; i < c->cfg.num_buffers; i++) {
 964                mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
 965                if (!mbo)
 966                        goto flush_fifos;
 967
 968                mbo->context = c;
 969                mbo->ifp = c->iface;
 970                mbo->hdm_channel_id = c->channel_id;
 971                if (c->iface->dma_alloc) {
 972                        mbo->virt_address =
 973                                c->iface->dma_alloc(mbo, coherent_buf_size);
 974                } else {
 975                        mbo->virt_address =
 976                                kzalloc(coherent_buf_size, GFP_KERNEL);
 977                }
 978                if (!mbo->virt_address)
 979                        goto release_mbo;
 980
 981                mbo->complete = compl;
 982                mbo->num_buffers_ptr = &dummy_num_buffers;
 983                if (dir == MOST_CH_RX) {
 984                        nq_hdm_mbo(mbo);
 985                        atomic_inc(&c->mbo_nq_level);
 986                } else {
 987                        spin_lock_irqsave(&c->fifo_lock, flags);
 988                        list_add_tail(&mbo->list, &c->fifo);
 989                        spin_unlock_irqrestore(&c->fifo_lock, flags);
 990                }
 991        }
 992        return c->cfg.num_buffers;
 993
 994release_mbo:
 995        kfree(mbo);
 996
 997flush_fifos:
 998        flush_channel_fifos(c);
 999        return 0;
1000}
1001
1002/**
1003 * most_submit_mbo - submits an MBO to fifo
1004 * @mbo: most buffer
1005 */
1006void most_submit_mbo(struct mbo *mbo)
1007{
1008        if (WARN_ONCE(!mbo || !mbo->context,
1009                      "bad mbo or missing channel reference\n"))
1010                return;
1011
1012        nq_hdm_mbo(mbo);
1013}
1014EXPORT_SYMBOL_GPL(most_submit_mbo);
1015
1016/**
1017 * most_write_completion - write completion handler
1018 * @mbo: most buffer
1019 *
1020 * This recycles the MBO for further usage. In case the channel has been
1021 * poisoned, the MBO is scheduled to be trashed.
1022 */
1023static void most_write_completion(struct mbo *mbo)
1024{
1025        struct most_channel *c;
1026
1027        c = mbo->context;
1028        if (mbo->status == MBO_E_INVAL)
1029                pr_info("WARN: Tx MBO status: invalid\n");
1030        if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1031                trash_mbo(mbo);
1032        else
1033                arm_mbo(mbo);
1034}
1035
1036int channel_has_mbo(struct most_interface *iface, int id,
1037                    struct core_component *comp)
1038{
1039        struct most_channel *c = iface->p->channel[id];
1040        unsigned long flags;
1041        int empty;
1042
1043        if (unlikely(!c))
1044                return -EINVAL;
1045
1046        if (c->pipe0.refs && c->pipe1.refs &&
1047            ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1048             (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1049                return 0;
1050
1051        spin_lock_irqsave(&c->fifo_lock, flags);
1052        empty = list_empty(&c->fifo);
1053        spin_unlock_irqrestore(&c->fifo_lock, flags);
1054        return !empty;
1055}
1056EXPORT_SYMBOL_GPL(channel_has_mbo);
1057
1058/**
1059 * most_get_mbo - get pointer to an MBO of pool
1060 * @iface: pointer to interface instance
1061 * @id: channel ID
1062 * @comp: driver component
1063 *
1064 * This attempts to get a free buffer out of the channel fifo.
1065 * Returns a pointer to MBO on success or NULL otherwise.
1066 */
1067struct mbo *most_get_mbo(struct most_interface *iface, int id,
1068                         struct core_component *comp)
1069{
1070        struct mbo *mbo;
1071        struct most_channel *c;
1072        unsigned long flags;
1073        int *num_buffers_ptr;
1074
1075        c = iface->p->channel[id];
1076        if (unlikely(!c))
1077                return NULL;
1078
1079        if (c->pipe0.refs && c->pipe1.refs &&
1080            ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1081             (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1082                return NULL;
1083
1084        if (comp == c->pipe0.comp)
1085                num_buffers_ptr = &c->pipe0.num_buffers;
1086        else if (comp == c->pipe1.comp)
1087                num_buffers_ptr = &c->pipe1.num_buffers;
1088        else
1089                num_buffers_ptr = &dummy_num_buffers;
1090
1091        spin_lock_irqsave(&c->fifo_lock, flags);
1092        if (list_empty(&c->fifo)) {
1093                spin_unlock_irqrestore(&c->fifo_lock, flags);
1094                return NULL;
1095        }
1096        mbo = list_pop_mbo(&c->fifo);
1097        --*num_buffers_ptr;
1098        spin_unlock_irqrestore(&c->fifo_lock, flags);
1099
1100        mbo->num_buffers_ptr = num_buffers_ptr;
1101        mbo->buffer_length = c->cfg.buffer_size;
1102        return mbo;
1103}
1104EXPORT_SYMBOL_GPL(most_get_mbo);
1105
1106/**
1107 * most_put_mbo - return buffer to pool
1108 * @mbo: most buffer
1109 */
1110void most_put_mbo(struct mbo *mbo)
1111{
1112        struct most_channel *c = mbo->context;
1113
1114        if (c->cfg.direction == MOST_CH_TX) {
1115                arm_mbo(mbo);
1116                return;
1117        }
1118        nq_hdm_mbo(mbo);
1119        atomic_inc(&c->mbo_nq_level);
1120}
1121EXPORT_SYMBOL_GPL(most_put_mbo);
1122
1123/**
1124 * most_read_completion - read completion handler
1125 * @mbo: most buffer
1126 *
1127 * This function is called by the HDM when data has been received from the
1128 * hardware and copied to the buffer of the MBO.
1129 *
1130 * In case the channel has been poisoned it puts the buffer in the trash queue.
1131 * Otherwise, it passes the buffer to an component for further processing.
1132 */
1133static void most_read_completion(struct mbo *mbo)
1134{
1135        struct most_channel *c = mbo->context;
1136
1137        if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1138                trash_mbo(mbo);
1139                return;
1140        }
1141
1142        if (mbo->status == MBO_E_INVAL) {
1143                nq_hdm_mbo(mbo);
1144                atomic_inc(&c->mbo_nq_level);
1145                return;
1146        }
1147
1148        if (atomic_sub_and_test(1, &c->mbo_nq_level))
1149                c->is_starving = 1;
1150
1151        if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1152            c->pipe0.comp->rx_completion(mbo) == 0)
1153                return;
1154
1155        if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1156            c->pipe1.comp->rx_completion(mbo) == 0)
1157                return;
1158
1159        most_put_mbo(mbo);
1160}
1161
1162/**
1163 * most_start_channel - prepares a channel for communication
1164 * @iface: pointer to interface instance
1165 * @id: channel ID
1166 * @comp: driver component
1167 *
1168 * This prepares the channel for usage. Cross-checks whether the
1169 * channel's been properly configured.
1170 *
1171 * Returns 0 on success or error code otherwise.
1172 */
1173int most_start_channel(struct most_interface *iface, int id,
1174                       struct core_component *comp)
1175{
1176        int num_buffer;
1177        int ret;
1178        struct most_channel *c = iface->p->channel[id];
1179
1180        if (unlikely(!c))
1181                return -EINVAL;
1182
1183        mutex_lock(&c->start_mutex);
1184        if (c->pipe0.refs + c->pipe1.refs > 0)
1185                goto out; /* already started by another component */
1186
1187        if (!try_module_get(iface->mod)) {
1188                pr_info("failed to acquire HDM lock\n");
1189                mutex_unlock(&c->start_mutex);
1190                return -ENOLCK;
1191        }
1192
1193        c->cfg.extra_len = 0;
1194        if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1195                pr_info("channel configuration failed. Go check settings...\n");
1196                ret = -EINVAL;
1197                goto err_put_module;
1198        }
1199
1200        init_waitqueue_head(&c->hdm_fifo_wq);
1201
1202        if (c->cfg.direction == MOST_CH_RX)
1203                num_buffer = arm_mbo_chain(c, c->cfg.direction,
1204                                           most_read_completion);
1205        else
1206                num_buffer = arm_mbo_chain(c, c->cfg.direction,
1207                                           most_write_completion);
1208        if (unlikely(!num_buffer)) {
1209                ret = -ENOMEM;
1210                goto err_put_module;
1211        }
1212
1213        ret = run_enqueue_thread(c, id);
1214        if (ret)
1215                goto err_put_module;
1216
1217        c->is_starving = 0;
1218        c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1219        c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
1220        atomic_set(&c->mbo_ref, num_buffer);
1221
1222out:
1223        if (comp == c->pipe0.comp)
1224                c->pipe0.refs++;
1225        if (comp == c->pipe1.comp)
1226                c->pipe1.refs++;
1227        mutex_unlock(&c->start_mutex);
1228        return 0;
1229
1230err_put_module:
1231        module_put(iface->mod);
1232        mutex_unlock(&c->start_mutex);
1233        return ret;
1234}
1235EXPORT_SYMBOL_GPL(most_start_channel);
1236
1237/**
1238 * most_stop_channel - stops a running channel
1239 * @iface: pointer to interface instance
1240 * @id: channel ID
1241 * @comp: driver component
1242 */
1243int most_stop_channel(struct most_interface *iface, int id,
1244                      struct core_component *comp)
1245{
1246        struct most_channel *c;
1247
1248        if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1249                pr_err("Bad interface or index out of range\n");
1250                return -EINVAL;
1251        }
1252        c = iface->p->channel[id];
1253        if (unlikely(!c))
1254                return -EINVAL;
1255
1256        mutex_lock(&c->start_mutex);
1257        if (c->pipe0.refs + c->pipe1.refs >= 2)
1258                goto out;
1259
1260        if (c->hdm_enqueue_task)
1261                kthread_stop(c->hdm_enqueue_task);
1262        c->hdm_enqueue_task = NULL;
1263
1264        if (iface->mod)
1265                module_put(iface->mod);
1266
1267        c->is_poisoned = true;
1268        if (c->iface->poison_channel(c->iface, c->channel_id)) {
1269                pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1270                       c->iface->description);
1271                mutex_unlock(&c->start_mutex);
1272                return -EAGAIN;
1273        }
1274        flush_trash_fifo(c);
1275        flush_channel_fifos(c);
1276
1277#ifdef CMPL_INTERRUPTIBLE
1278        if (wait_for_completion_interruptible(&c->cleanup)) {
1279                pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1280                mutex_unlock(&c->start_mutex);
1281                return -EINTR;
1282        }
1283#else
1284        wait_for_completion(&c->cleanup);
1285#endif
1286        c->is_poisoned = false;
1287
1288out:
1289        if (comp == c->pipe0.comp)
1290                c->pipe0.refs--;
1291        if (comp == c->pipe1.comp)
1292                c->pipe1.refs--;
1293        mutex_unlock(&c->start_mutex);
1294        return 0;
1295}
1296EXPORT_SYMBOL_GPL(most_stop_channel);
1297
1298/**
1299 * most_register_component - registers a driver component with the core
1300 * @comp: driver component
1301 */
1302int most_register_component(struct core_component *comp)
1303{
1304        if (!comp) {
1305                pr_err("Bad component\n");
1306                return -EINVAL;
1307        }
1308        list_add_tail(&comp->list, &mc.comp_list);
1309        pr_info("registered new core component %s\n", comp->name);
1310        return 0;
1311}
1312EXPORT_SYMBOL_GPL(most_register_component);
1313
1314static int disconnect_channels(struct device *dev, void *data)
1315{
1316        struct most_interface *iface;
1317        struct most_channel *c, *tmp;
1318        struct core_component *comp = data;
1319
1320        iface = to_most_interface(dev);
1321        list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
1322                if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1323                        comp->disconnect_channel(c->iface, c->channel_id);
1324                if (c->pipe0.comp == comp)
1325                        c->pipe0.comp = NULL;
1326                if (c->pipe1.comp == comp)
1327                        c->pipe1.comp = NULL;
1328        }
1329        return 0;
1330}
1331
1332/**
1333 * most_deregister_component - deregisters a driver component with the core
1334 * @comp: driver component
1335 */
1336int most_deregister_component(struct core_component *comp)
1337{
1338        if (!comp) {
1339                pr_err("Bad component\n");
1340                return -EINVAL;
1341        }
1342
1343        bus_for_each_dev(&mc.bus, NULL, comp, disconnect_channels);
1344        list_del(&comp->list);
1345        pr_info("deregistering component %s\n", comp->name);
1346        return 0;
1347}
1348EXPORT_SYMBOL_GPL(most_deregister_component);
1349
1350static void release_interface(struct device *dev)
1351{
1352        pr_info("releasing interface dev %s...\n", dev_name(dev));
1353}
1354
1355static void release_channel(struct device *dev)
1356{
1357        pr_info("releasing channel dev %s...\n", dev_name(dev));
1358}
1359
1360/**
1361 * most_register_interface - registers an interface with core
1362 * @iface: device interface
1363 *
1364 * Allocates and initializes a new interface instance and all of its channels.
1365 * Returns a pointer to kobject or an error pointer.
1366 */
1367int most_register_interface(struct most_interface *iface)
1368{
1369        unsigned int i;
1370        int id;
1371        struct most_channel *c;
1372
1373        if (!iface || !iface->enqueue || !iface->configure ||
1374            !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1375                pr_err("Bad interface or channel overflow\n");
1376                return -EINVAL;
1377        }
1378
1379        id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1380        if (id < 0) {
1381                pr_info("Failed to alloc mdev ID\n");
1382                return id;
1383        }
1384
1385        iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1386        if (!iface->p) {
1387                ida_simple_remove(&mdev_id, id);
1388                return -ENOMEM;
1389        }
1390
1391        INIT_LIST_HEAD(&iface->p->channel_list);
1392        iface->p->dev_id = id;
1393        strscpy(iface->p->name, iface->description, sizeof(iface->p->name));
1394        iface->dev.init_name = iface->p->name;
1395        iface->dev.bus = &mc.bus;
1396        iface->dev.parent = &mc.dev;
1397        iface->dev.groups = interface_attr_groups;
1398        iface->dev.release = release_interface;
1399        if (device_register(&iface->dev)) {
1400                pr_err("registering iface->dev failed\n");
1401                kfree(iface->p);
1402                ida_simple_remove(&mdev_id, id);
1403                return -ENOMEM;
1404        }
1405
1406        for (i = 0; i < iface->num_channels; i++) {
1407                const char *name_suffix = iface->channel_vector[i].name_suffix;
1408
1409                c = kzalloc(sizeof(*c), GFP_KERNEL);
1410                if (!c)
1411                        goto err_free_resources;
1412                if (!name_suffix)
1413                        snprintf(c->name, STRING_SIZE, "ch%d", i);
1414                else
1415                        snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1416                c->dev.init_name = c->name;
1417                c->dev.parent = &iface->dev;
1418                c->dev.groups = channel_attr_groups;
1419                c->dev.release = release_channel;
1420                iface->p->channel[i] = c;
1421                c->is_starving = 0;
1422                c->iface = iface;
1423                c->channel_id = i;
1424                c->keep_mbo = false;
1425                c->enqueue_halt = false;
1426                c->is_poisoned = false;
1427                c->cfg.direction = 0;
1428                c->cfg.data_type = 0;
1429                c->cfg.num_buffers = 0;
1430                c->cfg.buffer_size = 0;
1431                c->cfg.subbuffer_size = 0;
1432                c->cfg.packets_per_xact = 0;
1433                spin_lock_init(&c->fifo_lock);
1434                INIT_LIST_HEAD(&c->fifo);
1435                INIT_LIST_HEAD(&c->trash_fifo);
1436                INIT_LIST_HEAD(&c->halt_fifo);
1437                init_completion(&c->cleanup);
1438                atomic_set(&c->mbo_ref, 0);
1439                mutex_init(&c->start_mutex);
1440                mutex_init(&c->nq_mutex);
1441                list_add_tail(&c->list, &iface->p->channel_list);
1442                if (device_register(&c->dev)) {
1443                        pr_err("registering c->dev failed\n");
1444                        goto err_free_most_channel;
1445                }
1446        }
1447        pr_info("registered new device mdev%d (%s)\n",
1448                id, iface->description);
1449        most_interface_register_notify(iface->description);
1450        return 0;
1451
1452err_free_most_channel:
1453        kfree(c);
1454
1455err_free_resources:
1456        while (i > 0) {
1457                c = iface->p->channel[--i];
1458                device_unregister(&c->dev);
1459                kfree(c);
1460        }
1461        kfree(iface->p);
1462        device_unregister(&iface->dev);
1463        ida_simple_remove(&mdev_id, id);
1464        return -ENOMEM;
1465}
1466EXPORT_SYMBOL_GPL(most_register_interface);
1467
1468/**
1469 * most_deregister_interface - deregisters an interface with core
1470 * @iface: device interface
1471 *
1472 * Before removing an interface instance from the list, all running
1473 * channels are stopped and poisoned.
1474 */
1475void most_deregister_interface(struct most_interface *iface)
1476{
1477        int i;
1478        struct most_channel *c;
1479
1480        pr_info("deregistering device %s (%s)\n", dev_name(&iface->dev),
1481                iface->description);
1482        for (i = 0; i < iface->num_channels; i++) {
1483                c = iface->p->channel[i];
1484                if (c->pipe0.comp)
1485                        c->pipe0.comp->disconnect_channel(c->iface,
1486                                                        c->channel_id);
1487                if (c->pipe1.comp)
1488                        c->pipe1.comp->disconnect_channel(c->iface,
1489                                                        c->channel_id);
1490                c->pipe0.comp = NULL;
1491                c->pipe1.comp = NULL;
1492                list_del(&c->list);
1493                device_unregister(&c->dev);
1494                kfree(c);
1495        }
1496
1497        ida_simple_remove(&mdev_id, iface->p->dev_id);
1498        kfree(iface->p);
1499        device_unregister(&iface->dev);
1500}
1501EXPORT_SYMBOL_GPL(most_deregister_interface);
1502
1503/**
1504 * most_stop_enqueue - prevents core from enqueueing MBOs
1505 * @iface: pointer to interface
1506 * @id: channel id
1507 *
1508 * This is called by an HDM that _cannot_ attend to its duties and
1509 * is imminent to get run over by the core. The core is not going to
1510 * enqueue any further packets unless the flagging HDM calls
1511 * most_resume enqueue().
1512 */
1513void most_stop_enqueue(struct most_interface *iface, int id)
1514{
1515        struct most_channel *c = iface->p->channel[id];
1516
1517        if (!c)
1518                return;
1519
1520        mutex_lock(&c->nq_mutex);
1521        c->enqueue_halt = true;
1522        mutex_unlock(&c->nq_mutex);
1523}
1524EXPORT_SYMBOL_GPL(most_stop_enqueue);
1525
1526/**
1527 * most_resume_enqueue - allow core to enqueue MBOs again
1528 * @iface: pointer to interface
1529 * @id: channel id
1530 *
1531 * This clears the enqueue halt flag and enqueues all MBOs currently
1532 * sitting in the wait fifo.
1533 */
1534void most_resume_enqueue(struct most_interface *iface, int id)
1535{
1536        struct most_channel *c = iface->p->channel[id];
1537
1538        if (!c)
1539                return;
1540
1541        mutex_lock(&c->nq_mutex);
1542        c->enqueue_halt = false;
1543        mutex_unlock(&c->nq_mutex);
1544
1545        wake_up_interruptible(&c->hdm_fifo_wq);
1546}
1547EXPORT_SYMBOL_GPL(most_resume_enqueue);
1548
1549static void release_most_sub(struct device *dev)
1550{
1551        pr_info("releasing most_subsystem\n");
1552}
1553
1554static int __init most_init(void)
1555{
1556        int err;
1557
1558        pr_info("init()\n");
1559        INIT_LIST_HEAD(&mc.comp_list);
1560        ida_init(&mdev_id);
1561
1562        mc.bus.name = "most",
1563        mc.bus.match = most_match,
1564        mc.drv.name = "most_core",
1565        mc.drv.bus = &mc.bus,
1566        mc.drv.groups = mc_attr_groups;
1567
1568        err = bus_register(&mc.bus);
1569        if (err) {
1570                pr_info("Cannot register most bus\n");
1571                return err;
1572        }
1573        err = driver_register(&mc.drv);
1574        if (err) {
1575                pr_info("Cannot register core driver\n");
1576                goto err_unregister_bus;
1577        }
1578        mc.dev.init_name = "most_bus";
1579        mc.dev.release = release_most_sub;
1580        if (device_register(&mc.dev)) {
1581                err = -ENOMEM;
1582                goto err_unregister_driver;
1583        }
1584        configfs_init();
1585        return 0;
1586
1587err_unregister_driver:
1588        driver_unregister(&mc.drv);
1589err_unregister_bus:
1590        bus_unregister(&mc.bus);
1591        return err;
1592}
1593
1594static void __exit most_exit(void)
1595{
1596        pr_info("exit core module\n");
1597        device_unregister(&mc.dev);
1598        driver_unregister(&mc.drv);
1599        bus_unregister(&mc.bus);
1600        ida_destroy(&mdev_id);
1601}
1602
1603module_init(most_init);
1604module_exit(most_exit);
1605MODULE_LICENSE("GPL");
1606MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1607MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");
1608