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