master/domain.c
author Florian Pose <fp@igh-essen.com>
Wed, 30 Apr 2008 09:52:41 +0000
branchstable-1.3
changeset 1753 124eb40a2818
parent 1752 b01562a45602
permissions -rw-r--r--
Sync manager size is always the total size of the assigned Pdos, if
there are any.
/******************************************************************************
 *
 *  $Id$
 *
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
 *
 *  This file is part of the IgH EtherCAT Master.
 *
 *  The IgH EtherCAT Master is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  The IgH EtherCAT Master is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  The right to use EtherCAT Technology is granted and comes free of
 *  charge under condition of compatibility of product made by
 *  Licensee. People intending to distribute/sell products based on the
 *  code, have to sign an agreement to guarantee that products using
 *  software based on IgH EtherCAT master stay compatible with the actual
 *  EtherCAT specification (which are released themselves as an open
 *  standard) as the (only) precondition to have the right to use EtherCAT
 *  Technology, IP and trade marks.
 *
 *****************************************************************************/

/**
   \file
   EtherCAT domain methods.
*/

/*****************************************************************************/

#include <linux/module.h>

#include "globals.h"
#include "domain.h"
#include "master.h"

/*****************************************************************************/

/**
   Data registration type.
*/

typedef struct
{
    struct list_head list; /**< list item */
    ec_slave_t *slave; /**< slave */
    const ec_sync_t *sync; /**< sync manager */
    off_t sync_offset; /**< pdo offset */
    void **data_ptr; /**< pointer to process data pointer(s) */
}
ec_data_reg_t;

/*****************************************************************************/

void ec_domain_clear(struct kobject *);
void ec_domain_clear_data_regs(ec_domain_t *);
ssize_t ec_show_domain_attribute(struct kobject *, struct attribute *, char *);

/*****************************************************************************/

/** \cond */

EC_SYSFS_READ_ATTR(size);
EC_SYSFS_READ_ATTR(data);

static struct attribute *def_attrs[] = {
    &attr_size,
    &attr_data,
    NULL,
};

static struct sysfs_ops sysfs_ops = {
    .show = &ec_show_domain_attribute,
    .store = NULL
};

static struct kobj_type ktype_ec_domain = {
    .release = ec_domain_clear,
    .sysfs_ops = &sysfs_ops,
    .default_attrs = def_attrs
};

/** \endcond */

/*****************************************************************************/

/**
   Domain constructor.
   \return 0 in case of success, else < 0
*/

int ec_domain_init(ec_domain_t *domain, /**< EtherCAT domain */
                   ec_master_t *master, /**< owning master */
                   unsigned int index /**< domain index */
                   )
{
    domain->master = master;
    domain->index = index;
    domain->data_size = 0;
    domain->base_address = 0;
    domain->response_count = 0xFFFFFFFF;
    domain->notify_jiffies = 0;
    domain->working_counter_changes = 0;

    INIT_LIST_HEAD(&domain->data_regs);
    INIT_LIST_HEAD(&domain->datagrams);

    // init kobject and add it to the hierarchy
    memset(&domain->kobj, 0x00, sizeof(struct kobject));
    kobject_init(&domain->kobj);
    domain->kobj.ktype = &ktype_ec_domain;
    domain->kobj.parent = &master->kobj;
    if (kobject_set_name(&domain->kobj, "domain%i", index)) {
        EC_ERR("Failed to set kobj name.\n");
        kobject_put(&domain->kobj);
        return -1;
    }
    if (kobject_add(&domain->kobj)) {
        EC_ERR("Failed to add domain kobject.\n");
        kobject_put(&domain->kobj);
        return -1;
    }

    return 0;
}

/*****************************************************************************/

/**
   Domain destructor.
   Clears and frees a domain object.
*/

void ec_domain_destroy(ec_domain_t *domain /**< EtherCAT domain */)
{
    ec_datagram_t *datagram;

    // dequeue datagrams
    list_for_each_entry(datagram, &domain->datagrams, list) {
        if (!list_empty(&datagram->queue)) // datagram queued?
            list_del_init(&datagram->queue);
    }

    // destroy self
    kobject_del(&domain->kobj);
    kobject_put(&domain->kobj);
}

/*****************************************************************************/

/**
   Clear and free domain.
   This method is called by the kobject,
   once there are no more references to it.
*/

void ec_domain_clear(struct kobject *kobj /**< kobject of the domain */)
{
    ec_datagram_t *datagram, *next;
    ec_domain_t *domain;

    domain = container_of(kobj, ec_domain_t, kobj);

    list_for_each_entry_safe(datagram, next, &domain->datagrams, list) {
        ec_datagram_clear(datagram);
        kfree(datagram);
    }

    ec_domain_clear_data_regs(domain);

    kfree(domain);
}

/*****************************************************************************/

/**
 * Registers a PDO entry.
 * \return 0 in case of success, else < 0
 */

int ec_domain_reg_pdo_entry(
        ec_domain_t *domain, /**< EtherCAT domain */
        ec_sync_t *sync, /**< sync manager */
        const ec_pdo_entry_t *entry, /**< PDO entry to register */
        void **data_ptr /**< pointer to the process data pointer */
        )
{
    ec_data_reg_t *data_reg;
    const ec_pdo_t *other_pdo;
    const ec_pdo_entry_t *other_entry;
    unsigned int bit_offset, byte_offset;

    // Calculate offset (in sync manager) for process data pointer
    bit_offset = 0;
    byte_offset = 0;
    list_for_each_entry(other_pdo, &sync->pdos, list) {
        list_for_each_entry(other_entry, &other_pdo->entries, list) {
            if (other_entry == entry) {
                byte_offset = bit_offset / 8;
                break;
            }
            bit_offset += other_entry->bit_length;
        }
    }

    // Allocate memory for data registration object
    if (!(data_reg =
          (ec_data_reg_t *) kmalloc(sizeof(ec_data_reg_t), GFP_KERNEL))) {
        EC_ERR("Failed to allocate data registration.\n");
        return -1;
    }

    if (ec_slave_prepare_fmmu(sync->slave, domain, sync)) {
        EC_ERR("FMMU configuration failed.\n");
        kfree(data_reg);
        return -1;
    }

    data_reg->slave = sync->slave;
    data_reg->sync = sync;
    data_reg->sync_offset = byte_offset;
    data_reg->data_ptr = data_ptr;
    list_add_tail(&data_reg->list, &domain->data_regs);

    return 0;
}

/*****************************************************************************/

/**
   Clears the list of the data registrations.
*/

void ec_domain_clear_data_regs(ec_domain_t *domain /**< EtherCAT domain */)
{
    ec_data_reg_t *data_reg, *next;

    list_for_each_entry_safe(data_reg, next, &domain->data_regs, list) {
        list_del(&data_reg->list);
        kfree(data_reg);
    }
}

/*****************************************************************************/

/**
   Allocates a process data datagram and appends it to the list.
   \return 0 in case of success, else < 0
*/

int ec_domain_add_datagram(ec_domain_t *domain, /**< EtherCAT domain */
                           uint32_t offset, /**< logical offset */
                           size_t data_size /**< size of the datagram data */
                           )
{
    ec_datagram_t *datagram;

    if (!(datagram = kmalloc(sizeof(ec_datagram_t), GFP_KERNEL))) {
        EC_ERR("Failed to allocate domain datagram!\n");
        return -1;
    }

    ec_datagram_init(datagram);
    snprintf(datagram->name, EC_DATAGRAM_NAME_SIZE,
            "domain%u-%u", domain->index, offset);

    if (ec_datagram_lrw(datagram, offset, data_size)) {
        kfree(datagram);
        return -1;
    }

    list_add_tail(&datagram->list, &domain->datagrams);
    return 0;
}

/*****************************************************************************/

/**
   Creates a domain.
   Reserves domain memory, calculates the logical addresses of the
   corresponding FMMUs and sets the process data pointer of the registered
   process data.
   \return 0 in case of success, else < 0
*/

int ec_domain_alloc(ec_domain_t *domain, /**< EtherCAT domain */
                    uint32_t base_address /**< logical base address */
                    )
{
    ec_data_reg_t *data_reg;
    ec_slave_t *slave;
    ec_fmmu_t *fmmu;
    unsigned int i, j, datagram_count;
    uint32_t pdo_off, pdo_off_datagram;
    uint32_t datagram_offset, log_addr;
    size_t datagram_data_size, sync_size;
    ec_datagram_t *datagram;

    domain->base_address = base_address;

    // calculate size of process data and allocate memory
    domain->data_size = 0;
    datagram_offset = base_address;
    datagram_data_size = 0;
    datagram_count = 0;
    list_for_each_entry(slave, &domain->master->slaves, list) {
        for (j = 0; j < slave->fmmu_count; j++) {
            fmmu = &slave->fmmus[j];
            if (fmmu->domain == domain) {
                fmmu->logical_start_address = base_address + domain->data_size;
                sync_size = ec_sync_size(fmmu->sync);
                domain->data_size += sync_size;
                if (datagram_data_size + sync_size > EC_MAX_DATA_SIZE) {
                    if (ec_domain_add_datagram(domain, datagram_offset,
                                               datagram_data_size)) return -1;
                    datagram_offset += datagram_data_size;
                    datagram_data_size = 0;
                    datagram_count++;
                }
                datagram_data_size += sync_size;
            }
        }
    }

    // allocate last datagram
    if (datagram_data_size) {
        if (ec_domain_add_datagram(domain, datagram_offset,
                                   datagram_data_size))
            return -1;
        datagram_count++;
    }

    if (!datagram_count) {
        EC_WARN("Domain %i contains no data!\n", domain->index);
        ec_domain_clear_data_regs(domain);
        return 0;
    }

    // set all process data pointers
    list_for_each_entry(data_reg, &domain->data_regs, list) {
        for (i = 0; i < data_reg->slave->fmmu_count; i++) {
            fmmu = &data_reg->slave->fmmus[i];
            if (fmmu->domain == domain && fmmu->sync == data_reg->sync) {
                pdo_off = fmmu->logical_start_address + data_reg->sync_offset;
                // search datagram
                list_for_each_entry(datagram, &domain->datagrams, list) {
                    log_addr = EC_READ_U32(datagram->address);
                    pdo_off_datagram = pdo_off - log_addr;
                    if (pdo_off >= log_addr &&
                        pdo_off_datagram < datagram->mem_size) {
                        *data_reg->data_ptr = datagram->data +
                            pdo_off_datagram;
                    }
                }
                if (!data_reg->data_ptr) {
                    EC_ERR("Failed to assign data pointer!\n");
                    return -1;
                }
                break;
            }
        }
    }

    EC_INFO("Domain %i - Allocated %i bytes in %i datagram%s.\n",
            domain->index, domain->data_size, datagram_count,
            datagram_count == 1 ? "" : "s");

    ec_domain_clear_data_regs(domain);

    return 0;
}

/*****************************************************************************/

/** Outputs domain data.
 */
size_t ec_domain_output_data(
        const ec_domain_t *domain, /**< EtherCAT domain. */
        char *buffer /**< Output buffer */
        )
{
    off_t off = 0;
    ec_datagram_t *datagram;

    list_for_each_entry(datagram, &domain->datagrams, list) {
        if (off + datagram->data_size > PAGE_SIZE)
            return -EFBIG; // file too large
        memcpy(buffer + off, datagram->data, datagram->data_size);
        off += datagram->data_size;
    }

    return off;
}

/*****************************************************************************/

/**
   Formats attribute data for SysFS reading.
   \return number of bytes to read
*/

ssize_t ec_show_domain_attribute(struct kobject *kobj, /**< kobject */
                                 struct attribute *attr, /**< attribute */
                                 char *buffer /**< memory to store data in */
                                 )
{
    ec_domain_t *domain = container_of(kobj, ec_domain_t, kobj);

    if (attr == &attr_size) {
        return sprintf(buffer, "%u\n", domain->data_size);
    } else if (attr == &attr_data) {
	return ec_domain_output_data(domain, buffer);
    }

    return 0;
}

/******************************************************************************
 *  Realtime interface
 *****************************************************************************/

/**
 * Registers a PDO for a domain.
 * \return 0 on success, else non-zero
 * \ingroup RealtimeInterface
 */

int ecrt_domain_register_pdo(
        ec_domain_t *domain, /**< EtherCAT domain */
        ec_slave_t *slave, /**< EtherCAT slave */
        uint16_t pdo_entry_index, /**< PDO entry index */
        uint8_t pdo_entry_subindex, /**< PDO entry subindex */
        void **data_ptr /**< address of the process data pointer */
        )
{
    ec_sync_t *sync;
    const ec_pdo_t *pdo;
    const ec_pdo_entry_t *entry;
    unsigned int i;

    // search for PDO entry
    for (i = 0; i < slave->sii_sync_count; i++) {
        sync = &slave->sii_syncs[i];
        list_for_each_entry(pdo, &sync->pdos, list) {
            list_for_each_entry(entry, &pdo->entries, list) {
                if (entry->index != pdo_entry_index ||
                        entry->subindex != pdo_entry_subindex) continue;

                // PDO entry found
                if (ec_domain_reg_pdo_entry(domain, sync, entry, data_ptr)) {
                    return -1;
                }

                return 0;
            }
        }
    }

    EC_ERR("PDO entry 0x%04X:%u is not mapped in slave %u.\n",
           pdo_entry_index, pdo_entry_subindex, slave->ring_position);
    return -1;
}

/*****************************************************************************/

/**
 * Registers a bunch of data fields.
 * \attention The list has to be terminated with a NULL structure ({})!
 * \return 0 in case of success, else < 0
 * \ingroup RealtimeInterface
 */

int ecrt_domain_register_pdo_list(
        ec_domain_t *domain, /**< EtherCAT domain */
        const ec_pdo_reg_t *pdo_regs /**< array of PDO registrations */
        )
{
    const ec_pdo_reg_t *reg;
    ec_slave_t *slave;
    
    for (reg = pdo_regs; reg->slave_address; reg++) {
        if (!(slave = ecrt_master_get_slave(domain->master,
                        reg->slave_address, reg->vendor_id,
                        reg->product_code)))
            return -1;

        if (ecrt_domain_register_pdo(domain, slave, reg->pdo_entry_index,
                    reg->pdo_entry_subindex, reg->data_ptr))
            return -1;
    }

    return 0;
}

/*****************************************************************************/

/**
 * Registers a PDO range in a domain.
 * \return 0 on success, else non-zero
 * \ingroup RealtimeInterface
 */

int ecrt_domain_register_pdo_range(
        ec_domain_t *domain, /**< EtherCAT domain */
        ec_slave_t *slave, /**< EtherCAT slave */
        ec_direction_t dir, /**< data direction */
        uint16_t offset, /**< offset in slave's PDO range */
        uint16_t length, /**< length of this range */
        void **data_ptr /**< address of the process data pointer */
        )
{
    ec_data_reg_t *data_reg;
    ec_sync_t *sync;
    uint16_t sync_length;

    if (!(sync = ec_slave_get_pdo_sync(slave, dir))) {
        EC_ERR("No sync manager found for PDO range.\n");
        return -1;
    }

    // Allocate memory for data registration object
    if (!(data_reg =
          (ec_data_reg_t *) kmalloc(sizeof(ec_data_reg_t), GFP_KERNEL))) {
        EC_ERR("Failed to allocate data registration.\n");
        return -1;
    }

    if (ec_slave_prepare_fmmu(slave, domain, sync)) {
        EC_ERR("FMMU configuration failed.\n");
        kfree(data_reg);
        return -1;
    }

    data_reg->slave = slave;
    data_reg->sync = sync;
    data_reg->sync_offset = offset;
    data_reg->data_ptr = data_ptr;

    // estimate sync manager length
    sync_length = offset + length;
    if (sync->est_length < sync_length) {
        sync->est_length = sync_length;
        if (domain->master->debug_level) {
            EC_DBG("Estimating length of sync manager %i of slave %i to %i.\n",
                   sync->index, slave->ring_position, sync_length);
        }
    }

    list_add_tail(&data_reg->list, &domain->data_regs);
    return 0;
}

/*****************************************************************************/

/**
   Processes received process data and requeues the domain datagram(s).
   \ingroup RealtimeInterface
*/

void ecrt_domain_process(ec_domain_t *domain /**< EtherCAT domain */)
{
    unsigned int working_counter_sum;
    ec_datagram_t *datagram;

    working_counter_sum = 0;
    domain->state = 0;
    list_for_each_entry(datagram, &domain->datagrams, list) {
        ec_datagram_output_stats(datagram);
        if (datagram->state == EC_DATAGRAM_RECEIVED) {
            working_counter_sum += datagram->working_counter;
        }
        else {
            domain->state = -1;
        }
    }

    if (working_counter_sum != domain->response_count) {
        domain->working_counter_changes++;
        domain->response_count = working_counter_sum;
    }

    if (domain->working_counter_changes &&
        jiffies - domain->notify_jiffies > HZ) {
        domain->notify_jiffies = jiffies;
        if (domain->working_counter_changes == 1) {
            EC_INFO("Domain %i working counter change: %i\n", domain->index,
                    domain->response_count);
        }
        else {
            EC_INFO("Domain %i: %u WC changes. Current response count: %i\n",
                    domain->index, domain->working_counter_changes,
                    domain->response_count);
        }
        domain->working_counter_changes = 0;
    }
}

/*****************************************************************************/

/**
   Places all process data datagrams in the masters datagram queue.
   \ingroup RealtimeInterface
*/

void ecrt_domain_queue(ec_domain_t *domain /**< EtherCAT domain */)
{
    ec_datagram_t *datagram;

    list_for_each_entry(datagram, &domain->datagrams, list) {
        ec_master_queue_datagram(domain->master, datagram);
    }
}

/*****************************************************************************/

/**
   Returns the state of a domain.
   \return 0 if all datagrams were received, else -1.
   \ingroup RealtimeInterface
*/

int ecrt_domain_state(const ec_domain_t *domain /**< EtherCAT domain */)
{
    return domain->state;
}

/*****************************************************************************/

/** \cond */

EXPORT_SYMBOL(ecrt_domain_register_pdo);
EXPORT_SYMBOL(ecrt_domain_register_pdo_list);
EXPORT_SYMBOL(ecrt_domain_register_pdo_range);
EXPORT_SYMBOL(ecrt_domain_process);
EXPORT_SYMBOL(ecrt_domain_queue);
EXPORT_SYMBOL(ecrt_domain_state);

/** \endcond */

/*****************************************************************************/