fp@145: /****************************************************************************** fp@145: * fp@145: * $Id$ fp@145: * fp@1618: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1618: * fp@1618: * This file is part of the IgH EtherCAT Master. fp@1618: * fp@1618: * The IgH EtherCAT Master is free software; you can redistribute it fp@1618: * and/or modify it under the terms of the GNU General Public License fp@1619: * as published by the Free Software Foundation; either version 2 of the fp@1619: * License, or (at your option) any later version. fp@1618: * fp@1618: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1618: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1618: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1618: * GNU General Public License for more details. fp@1618: * fp@1618: * You should have received a copy of the GNU General Public License fp@1618: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1618: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1618: * fp@1619: * The right to use EtherCAT Technology is granted and comes free of fp@1619: * charge under condition of compatibility of product made by fp@1619: * Licensee. People intending to distribute/sell products based on the fp@1619: * code, have to sign an agreement to guarantee that products using fp@1619: * software based on IgH EtherCAT master stay compatible with the actual fp@1619: * EtherCAT specification (which are released themselves as an open fp@1619: * standard) as the (only) precondition to have the right to use EtherCAT fp@1619: * Technology, IP and trade marks. fp@1619: * fp@145: *****************************************************************************/ fp@145: fp@1618: /** fp@1618: \file fp@1618: Ethernet-over-EtherCAT (EoE). fp@1618: */ fp@1618: fp@1618: /*****************************************************************************/ fp@1618: fp@1624: #include fp@1619: #include fp@1619: fp@145: #include "../include/ecrt.h" fp@145: #include "globals.h" fp@145: #include "master.h" fp@145: #include "slave.h" fp@145: #include "mailbox.h" fp@145: #include "ethernet.h" fp@145: fp@1623: /** fp@1623: Defines the debug level of EoE processing fp@1623: fp@1623: 0 = No debug messages. fp@1623: 1 = Output actions. fp@1623: 2 = Output actions and frame data. fp@1623: */ fp@1623: fp@1619: #define EOE_DEBUG_LEVEL 0 fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: void ec_eoe_flush(ec_eoe_t *); fp@1619: fp@1619: // state functions fp@1619: void ec_eoe_state_rx_start(ec_eoe_t *); fp@1619: void ec_eoe_state_rx_check(ec_eoe_t *); fp@1619: void ec_eoe_state_rx_fetch(ec_eoe_t *); fp@1619: void ec_eoe_state_tx_start(ec_eoe_t *); fp@1619: void ec_eoe_state_tx_sent(ec_eoe_t *); fp@1619: fp@1619: // net_device functions fp@1619: int ec_eoedev_open(struct net_device *); fp@1619: int ec_eoedev_stop(struct net_device *); fp@1619: int ec_eoedev_tx(struct sk_buff *, struct net_device *); fp@1619: struct net_device_stats *ec_eoedev_stats(struct net_device *); fp@1619: fp@145: /*****************************************************************************/ fp@145: fp@1618: /** fp@1618: EoE constructor. fp@1619: Initializes the EoE handler, creates a net_device and registeres it. fp@1619: */ fp@1619: fp@1619: int ec_eoe_init(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: ec_eoe_t **priv; fp@1619: int result, i; fp@1619: fp@1619: eoe->slave = NULL; fp@1624: ec_datagram_init(&eoe->datagram); fp@1619: eoe->state = ec_eoe_state_rx_start; fp@1619: eoe->opened = 0; fp@1619: eoe->rx_skb = NULL; fp@1619: eoe->rx_expected_fragment = 0; fp@1619: INIT_LIST_HEAD(&eoe->tx_queue); fp@1619: eoe->tx_frame = NULL; fp@1619: eoe->tx_queue_active = 0; fp@1619: eoe->tx_queued_frames = 0; fp@1619: eoe->tx_queue_lock = SPIN_LOCK_UNLOCKED; fp@1619: eoe->tx_frame_number = 0xFF; fp@1619: memset(&eoe->stats, 0, sizeof(struct net_device_stats)); fp@1619: fp@1619: if (!(eoe->dev = fp@1619: alloc_netdev(sizeof(ec_eoe_t *), "eoe%d", ether_setup))) { fp@1619: EC_ERR("Unable to allocate net_device for EoE handler!\n"); fp@1619: goto out_return; fp@1619: } fp@1619: fp@1619: // initialize net_device fp@1619: eoe->dev->open = ec_eoedev_open; fp@1619: eoe->dev->stop = ec_eoedev_stop; fp@1619: eoe->dev->hard_start_xmit = ec_eoedev_tx; fp@1619: eoe->dev->get_stats = ec_eoedev_stats; fp@1619: fp@1619: for (i = 0; i < ETH_ALEN; i++) fp@1619: eoe->dev->dev_addr[i] = i | (i << 4); fp@1619: fp@1619: // initialize private data fp@1619: priv = netdev_priv(eoe->dev); fp@1619: *priv = eoe; fp@1619: fp@1619: // Usually setting the MTU appropriately makes the upper layers fp@1619: // do the frame fragmenting. In some cases this doesn't work fp@1619: // so the MTU is left on the Ethernet standard value and fragmenting fp@1619: // is done "manually". fp@1619: #if 0 fp@1619: eoe->dev->mtu = slave->sii_rx_mailbox_size - ETH_HLEN - 10; fp@1619: #endif fp@1619: fp@1619: // connect the net_device to the kernel fp@1619: if ((result = register_netdev(eoe->dev))) { fp@1619: EC_ERR("Unable to register net_device: error %i\n", result); fp@1619: goto out_free; fp@1619: } fp@1619: fp@1619: // make the last address octet unique fp@1619: eoe->dev->dev_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex; fp@1619: fp@1619: return 0; fp@1619: fp@1619: out_free: fp@1619: free_netdev(eoe->dev); fp@1619: eoe->dev = NULL; fp@1619: out_return: fp@1619: return -1; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@1618: /** fp@1618: EoE destructor. fp@1619: Unregisteres the net_device and frees allocated memory. fp@1619: */ fp@1619: fp@1619: void ec_eoe_clear(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1624: if (eoe->dev) { // TODO: dev never NULL? fp@1619: unregister_netdev(eoe->dev); fp@1619: free_netdev(eoe->dev); fp@1619: } fp@1619: fp@1619: // empty transmit queue fp@1619: ec_eoe_flush(eoe); fp@1619: fp@1619: if (eoe->tx_frame) { fp@1619: dev_kfree_skb(eoe->tx_frame->skb); fp@1619: kfree(eoe->tx_frame); fp@1619: } fp@1619: fp@1619: if (eoe->rx_skb) dev_kfree_skb(eoe->rx_skb); fp@1621: fp@1624: ec_datagram_clear(&eoe->datagram); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Empties the transmit queue. fp@1619: */ fp@1619: fp@1619: void ec_eoe_flush(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: ec_eoe_frame_t *frame, *next; fp@1619: fp@1619: spin_lock_bh(&eoe->tx_queue_lock); fp@1619: fp@1619: list_for_each_entry_safe(frame, next, &eoe->tx_queue, queue) { fp@1619: list_del(&frame->queue); fp@1619: dev_kfree_skb(frame->skb); fp@1619: kfree(frame); fp@1619: } fp@1619: eoe->tx_queued_frames = 0; fp@1619: fp@1619: spin_unlock_bh(&eoe->tx_queue_lock); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Sends a frame or the next fragment. fp@1619: */ fp@1619: fp@1619: int ec_eoe_send(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: size_t remaining_size, current_size, complete_offset; fp@1619: unsigned int last_fragment; fp@1619: uint8_t *data; fp@1619: #if EOE_DEBUG_LEVEL > 1 fp@1619: unsigned int i; fp@1619: #endif fp@1619: fp@1619: remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset; fp@1619: fp@1619: if (remaining_size <= eoe->slave->sii_tx_mailbox_size - 10) { fp@1619: current_size = remaining_size; fp@1619: last_fragment = 1; fp@1619: } fp@1619: else { fp@1619: current_size = ((eoe->slave->sii_tx_mailbox_size - 10) / 32) * 32; fp@1619: last_fragment = 0; fp@1619: } fp@1619: fp@1619: if (eoe->tx_fragment_number) { fp@1619: complete_offset = eoe->tx_offset / 32; fp@1619: } fp@1619: else { fp@1624: // complete size in 32 bit blocks, rounded up. fp@1619: complete_offset = remaining_size / 32 + 1; fp@1619: } fp@1619: fp@1619: #if EOE_DEBUG_LEVEL > 0 fp@1619: EC_DBG("EoE TX sending %sfragment %i with %i octets (%i)." fp@1619: " %i frames queued.\n", last_fragment ? "last " : "", fp@1619: eoe->tx_fragment_number, current_size, complete_offset, fp@1619: eoe->tx_queued_frames); fp@1619: #endif fp@1619: fp@1619: #if EOE_DEBUG_LEVEL > 1 fp@1619: EC_DBG(""); fp@1619: for (i = 0; i < current_size; i++) { fp@1619: printk("%02X ", frame->skb->data[eoe->tx_offset + i]); fp@1619: if ((i + 1) % 16 == 0) { fp@1619: printk("\n"); fp@1619: EC_DBG(""); fp@1619: } fp@1619: } fp@1619: printk("\n"); fp@1619: #endif fp@1619: fp@1624: if (!(data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram, fp@1621: 0x02, current_size + 4))) fp@1619: return -1; fp@1619: fp@1619: EC_WRITE_U8 (data, 0x00); // eoe fragment req. fp@1619: EC_WRITE_U8 (data + 1, last_fragment); fp@1619: EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) | fp@1619: (complete_offset & 0x3F) << 6 | fp@1619: (eoe->tx_frame_number & 0x0F) << 12)); fp@1619: fp@1619: memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size); fp@1624: ec_master_queue_datagram(eoe->slave->master, &eoe->datagram); fp@1619: fp@1619: eoe->tx_offset += current_size; fp@1619: eoe->tx_fragment_number++; fp@1619: return 0; fp@145: } fp@145: fp@145: /*****************************************************************************/ fp@145: fp@1618: /** fp@1618: Runs the EoE state machine. fp@1618: */ fp@1618: fp@1619: void ec_eoe_run(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: if (!eoe->opened) return; fp@1619: fp@1619: // call state function fp@1619: eoe->state(eoe); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Returns the state of the device. fp@1619: \return 1 if the device is "up", 0 if it is "down" fp@1619: */ fp@1619: fp@1619: unsigned int ec_eoe_active(const ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: return eoe->slave && eoe->opened; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Prints EoE handler information. fp@1619: */ fp@1619: fp@1619: void ec_eoe_print(const ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: EC_INFO(" EoE handler %s\n", eoe->dev->name); fp@1619: EC_INFO(" State: %s\n", eoe->opened ? "opened" : "closed"); fp@1619: if (eoe->slave) fp@1619: EC_INFO(" Coupled to slave %i.\n", eoe->slave->ring_position); fp@1619: else fp@1619: EC_INFO(" Not coupled.\n"); fp@1619: } fp@1619: fp@1619: /****************************************************************************** fp@1619: * STATE PROCESSING FUNCTIONS fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: State: RX_START. fp@1624: Starts a new receiving sequence by queueing a datagram that checks the fp@1624: slave's mailbox for a new EoE datagram. fp@1619: */ fp@1619: fp@1619: void ec_eoe_state_rx_start(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: if (!eoe->slave->online || !eoe->slave->master->device->link_state) fp@1619: return; fp@1619: fp@1624: ec_slave_mbox_prepare_check(eoe->slave, &eoe->datagram); fp@1624: ec_master_queue_datagram(eoe->slave->master, &eoe->datagram); fp@1619: eoe->state = ec_eoe_state_rx_check; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: State: RX_CHECK. fp@1624: Processes the checking datagram sent in RX_START and issues a receive fp@1624: datagram, if new data is available. fp@1619: */ fp@1619: fp@1619: void ec_eoe_state_rx_check(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1624: if (eoe->datagram.state != EC_CMD_RECEIVED) { fp@1619: eoe->stats.rx_errors++; fp@1619: eoe->state = ec_eoe_state_tx_start; fp@1619: return; fp@1619: } fp@1619: fp@1624: if (!ec_slave_mbox_check(&eoe->datagram)) { fp@1619: eoe->state = ec_eoe_state_tx_start; fp@1619: return; fp@1619: } fp@1619: fp@1624: ec_slave_mbox_prepare_fetch(eoe->slave, &eoe->datagram); fp@1624: ec_master_queue_datagram(eoe->slave->master, &eoe->datagram); fp@1619: eoe->state = ec_eoe_state_rx_fetch; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: State: RX_FETCH. fp@1619: Checks if the requested data of RX_CHECK was received and processes the fp@1624: EoE datagram. fp@1619: */ fp@1619: fp@1619: void ec_eoe_state_rx_fetch(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: size_t rec_size, data_size; fp@1619: uint8_t *data, frame_type, last_fragment, time_appended; fp@1619: uint8_t frame_number, fragment_offset, fragment_number; fp@1619: off_t offset; fp@1619: fp@1624: if (eoe->datagram.state != EC_CMD_RECEIVED) { fp@1619: eoe->stats.rx_errors++; fp@1619: eoe->state = ec_eoe_state_tx_start; fp@1619: return; fp@1619: } fp@1619: fp@1624: if (!(data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram, fp@1621: 0x02, &rec_size))) { fp@1619: eoe->stats.rx_errors++; fp@1619: eoe->state = ec_eoe_state_tx_start; fp@1619: return; fp@1619: } fp@1619: fp@1619: frame_type = EC_READ_U16(data) & 0x000F; fp@1619: fp@1624: if (frame_type != 0x00) { fp@1619: #if EOE_DEBUG_LEVEL > 0 fp@1619: EC_DBG("other frame received.\n"); fp@1619: #endif fp@1619: eoe->stats.rx_dropped++; fp@1619: eoe->state = ec_eoe_state_tx_start; fp@1624: return; fp@1624: } fp@1624: fp@1624: // EoE Fragment Request received fp@1624: fp@1624: last_fragment = (EC_READ_U16(data) >> 8) & 0x0001; fp@1624: time_appended = (EC_READ_U16(data) >> 9) & 0x0001; fp@1624: fragment_number = EC_READ_U16(data + 2) & 0x003F; fp@1624: fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F; fp@1624: frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F; fp@1624: fp@1624: #if EOE_DEBUG_LEVEL > 0 fp@1624: EC_DBG("EoE RX fragment %i, offset %i, frame %i%s%s," fp@1624: " %i octets\n", fragment_number, fragment_offset, fp@1624: frame_number, fp@1624: last_fragment ? ", last fragment" : "", fp@1624: time_appended ? ", + timestamp" : "", fp@1624: time_appended ? rec_size - 8 : rec_size - 4); fp@1624: #endif fp@1624: fp@1624: #if EOE_DEBUG_LEVEL > 1 fp@1624: EC_DBG(""); fp@1624: for (i = 0; i < rec_size - 4; i++) { fp@1624: printk("%02X ", data[i + 4]); fp@1624: if ((i + 1) % 16 == 0) { fp@1624: printk("\n"); fp@1624: EC_DBG(""); fp@1624: } fp@1624: } fp@1624: printk("\n"); fp@1624: #endif fp@1624: fp@1624: data_size = time_appended ? rec_size - 8 : rec_size - 4; fp@1624: fp@1624: if (!fragment_number) { fp@1624: if (eoe->rx_skb) { fp@1624: EC_WARN("EoE RX freeing old socket buffer...\n"); fp@1624: dev_kfree_skb(eoe->rx_skb); fp@1624: } fp@1624: fp@1624: // new socket buffer fp@1624: if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) { fp@1624: if (printk_ratelimit()) fp@1624: EC_WARN("EoE RX low on mem. frame dropped.\n"); fp@1624: eoe->stats.rx_dropped++; fp@1624: eoe->state = ec_eoe_state_tx_start; fp@1624: return; fp@1624: } fp@1624: fp@1624: eoe->rx_skb_offset = 0; fp@1624: eoe->rx_skb_size = fragment_offset * 32; fp@1624: eoe->rx_expected_fragment = 0; fp@1624: } fp@1624: else { fp@1624: if (!eoe->rx_skb) { fp@1624: eoe->stats.rx_dropped++; fp@1624: eoe->state = ec_eoe_state_tx_start; fp@1624: return; fp@1624: } fp@1624: fp@1624: offset = fragment_offset * 32; fp@1624: if (offset != eoe->rx_skb_offset || fp@1624: offset + data_size > eoe->rx_skb_size || fp@1624: fragment_number != eoe->rx_expected_fragment) { fp@1624: dev_kfree_skb(eoe->rx_skb); fp@1624: eoe->rx_skb = NULL; fp@1624: eoe->stats.rx_errors++; fp@1624: eoe->state = ec_eoe_state_tx_start; fp@1624: return; fp@1624: } fp@1624: } fp@1624: fp@1624: // copy fragment into socket buffer fp@1624: memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size); fp@1624: eoe->rx_skb_offset += data_size; fp@1624: fp@1624: if (last_fragment) { fp@1624: // update statistics fp@1624: eoe->stats.rx_packets++; fp@1624: eoe->stats.rx_bytes += eoe->rx_skb->len; fp@1624: fp@1624: #if EOE_DEBUG_LEVEL > 0 fp@1624: EC_DBG("EoE RX frame completed with %u octets.\n", fp@1624: eoe->rx_skb->len); fp@1624: #endif fp@1624: fp@1624: // pass socket buffer to network stack fp@1624: eoe->rx_skb->dev = eoe->dev; fp@1624: eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev); fp@1624: eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY; fp@1624: if (netif_rx(eoe->rx_skb)) { fp@1624: EC_WARN("EoE RX netif_rx failed.\n"); fp@1624: } fp@1624: eoe->rx_skb = NULL; fp@1624: fp@1624: eoe->state = ec_eoe_state_tx_start; fp@1624: } fp@1624: else { fp@1624: eoe->rx_expected_fragment++; fp@1624: #if EOE_DEBUG_LEVEL > 0 fp@1624: EC_DBG("EoE RX expecting fragment %i\n", fp@1624: eoe->rx_expected_fragment); fp@1624: #endif fp@1624: eoe->state = ec_eoe_state_rx_start; fp@1619: } fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: State: TX START. fp@1619: Starts a new transmit sequence. If no data is available, a new receive fp@1619: sequence is started instead. fp@1619: */ fp@1619: fp@1619: void ec_eoe_state_tx_start(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1619: #if EOE_DEBUG_LEVEL > 0 fp@1619: unsigned int wakeup; fp@1619: #endif fp@1619: fp@1619: if (!eoe->slave->online || !eoe->slave->master->device->link_state) fp@1619: return; fp@1619: fp@1619: spin_lock_bh(&eoe->tx_queue_lock); fp@1619: fp@1619: if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) { fp@1619: spin_unlock_bh(&eoe->tx_queue_lock); fp@1619: // no data available. fp@1619: // start a new receive immediately. fp@1619: ec_eoe_state_rx_start(eoe); fp@1619: return; fp@1619: } fp@1619: fp@1619: // take the first frame out of the queue fp@1619: eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue); fp@1619: list_del(&eoe->tx_frame->queue); fp@1619: if (!eoe->tx_queue_active && fp@1619: eoe->tx_queued_frames == EC_EOE_TX_QUEUE_SIZE / 2) { fp@1619: netif_wake_queue(eoe->dev); fp@1619: eoe->tx_queue_active = 1; fp@1619: #if EOE_DEBUG_LEVEL > 0 fp@1619: wakeup = 1; fp@1619: #endif fp@1619: } fp@1619: fp@1619: eoe->tx_queued_frames--; fp@1619: spin_unlock_bh(&eoe->tx_queue_lock); fp@1619: fp@1619: eoe->tx_frame_number++; fp@1619: eoe->tx_frame_number %= 16; fp@1619: eoe->tx_fragment_number = 0; fp@1619: eoe->tx_offset = 0; fp@1619: fp@1619: if (ec_eoe_send(eoe)) { fp@1619: dev_kfree_skb(eoe->tx_frame->skb); fp@1619: kfree(eoe->tx_frame); fp@1619: eoe->tx_frame = NULL; fp@1619: eoe->stats.tx_errors++; fp@1619: eoe->state = ec_eoe_state_rx_start; fp@1619: return; fp@1619: } fp@1619: fp@1619: #if EOE_DEBUG_LEVEL > 0 fp@1619: if (wakeup) EC_DBG("waking up TX queue...\n"); fp@1619: #endif fp@1619: fp@1619: eoe->state = ec_eoe_state_tx_sent; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: State: TX SENT. fp@1624: Checks is the previous transmit datagram succeded and sends the next fp@1619: fragment, if necessary. fp@1619: */ fp@1619: fp@1619: void ec_eoe_state_tx_sent(ec_eoe_t *eoe /**< EoE handler */) fp@1619: { fp@1624: if (eoe->datagram.state != EC_CMD_RECEIVED) { fp@1619: eoe->stats.tx_errors++; fp@1619: eoe->state = ec_eoe_state_rx_start; fp@1619: return; fp@1619: } fp@1619: fp@1624: if (eoe->datagram.working_counter != 1) { fp@1619: eoe->stats.tx_errors++; fp@1619: eoe->state = ec_eoe_state_rx_start; fp@1619: return; fp@1619: } fp@1619: fp@1619: // frame completely sent fp@1619: if (eoe->tx_offset >= eoe->tx_frame->skb->len) { fp@1619: eoe->stats.tx_packets++; fp@1619: eoe->stats.tx_bytes += eoe->tx_frame->skb->len; fp@1619: dev_kfree_skb(eoe->tx_frame->skb); fp@1619: kfree(eoe->tx_frame); fp@1619: eoe->tx_frame = NULL; fp@1619: eoe->state = ec_eoe_state_rx_start; fp@1619: } fp@1619: else { // send next fragment fp@1619: if (ec_eoe_send(eoe)) { fp@1619: dev_kfree_skb(eoe->tx_frame->skb); fp@1619: kfree(eoe->tx_frame); fp@1619: eoe->tx_frame = NULL; fp@1619: eoe->stats.tx_errors++; fp@1619: eoe->state = ec_eoe_state_rx_start; fp@1619: } fp@1619: } fp@1619: } fp@1619: fp@1619: /****************************************************************************** fp@1619: * NET_DEVICE functions fp@1619: *****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Opens the virtual network device. fp@1619: */ fp@1619: fp@1619: int ec_eoedev_open(struct net_device *dev /**< EoE net_device */) fp@1619: { fp@1619: ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev)); fp@1619: ec_eoe_flush(eoe); fp@1619: eoe->opened = 1; fp@1619: netif_start_queue(dev); fp@1619: eoe->tx_queue_active = 1; fp@1619: EC_INFO("%s opened.\n", dev->name); fp@1619: if (!eoe->slave) fp@1619: EC_WARN("device %s is not coupled to any EoE slave!\n", dev->name); fp@1619: else { fp@1619: eoe->slave->requested_state = EC_SLAVE_STATE_OP; fp@1624: eoe->slave->error_flag = 0; fp@1619: } fp@1619: return 0; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Stops the virtual network device. fp@1619: */ fp@1619: fp@1619: int ec_eoedev_stop(struct net_device *dev /**< EoE net_device */) fp@1619: { fp@1619: ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev)); fp@1619: netif_stop_queue(dev); fp@1619: eoe->tx_queue_active = 0; fp@1619: eoe->opened = 0; fp@1619: ec_eoe_flush(eoe); fp@1619: EC_INFO("%s stopped.\n", dev->name); fp@1619: if (!eoe->slave) fp@1619: EC_WARN("device %s is not coupled to any EoE slave!\n", dev->name); fp@1619: else { fp@1619: eoe->slave->requested_state = EC_SLAVE_STATE_INIT; fp@1624: eoe->slave->error_flag = 0; fp@1619: } fp@1619: return 0; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Transmits data via the virtual network device. fp@1619: */ fp@1619: fp@1619: int ec_eoedev_tx(struct sk_buff *skb, /**< transmit socket buffer */ fp@1619: struct net_device *dev /**< EoE net_device */ fp@1619: ) fp@1619: { fp@1619: ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev)); fp@1619: ec_eoe_frame_t *frame; fp@1619: fp@1619: #if 0 fp@1619: if (skb->len > eoe->slave->sii_tx_mailbox_size - 10) { fp@1619: EC_WARN("EoE TX frame (%i octets) exceeds MTU. dropping.\n", skb->len); fp@1619: dev_kfree_skb(skb); fp@1619: eoe->stats.tx_dropped++; fp@1619: return 0; fp@1619: } fp@1619: #endif fp@1619: fp@1619: if (!(frame = fp@1619: (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) { fp@1619: if (printk_ratelimit()) fp@1619: EC_WARN("EoE TX: low on mem. frame dropped.\n"); fp@1619: return 1; fp@1619: } fp@1619: fp@1619: frame->skb = skb; fp@1619: fp@1619: spin_lock_bh(&eoe->tx_queue_lock); fp@1619: list_add_tail(&frame->queue, &eoe->tx_queue); fp@1619: eoe->tx_queued_frames++; fp@1619: if (eoe->tx_queued_frames == EC_EOE_TX_QUEUE_SIZE) { fp@1619: netif_stop_queue(dev); fp@1619: eoe->tx_queue_active = 0; fp@1619: } fp@1619: spin_unlock_bh(&eoe->tx_queue_lock); fp@1619: fp@1619: #if EOE_DEBUG_LEVEL > 0 fp@1619: EC_DBG("EoE TX queued frame with %i octets (%i frames queued).\n", fp@1619: skb->len, eoe->tx_queued_frames); fp@1619: if (!eoe->tx_queue_active) fp@1619: EC_WARN("EoE TX queue is now full.\n"); fp@1619: #endif fp@1619: fp@1619: return 0; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: /** fp@1619: Gets statistics about the virtual network device. fp@1619: */ fp@1619: fp@1619: struct net_device_stats *ec_eoedev_stats(struct net_device *dev fp@1619: /**< EoE net_device */) fp@1619: { fp@1619: ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev)); fp@1619: return &eoe->stats; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/