master/ethernet.c
branchstable-1.0
changeset 1619 0d4119024f55
parent 1618 5cff10efb927
child 1621 4bbe090553f7
equal deleted inserted replaced
1618:5cff10efb927 1619:0d4119024f55
     6  *
     6  *
     7  *  This file is part of the IgH EtherCAT Master.
     7  *  This file is part of the IgH EtherCAT Master.
     8  *
     8  *
     9  *  The IgH EtherCAT Master is free software; you can redistribute it
     9  *  The IgH EtherCAT Master is free software; you can redistribute it
    10  *  and/or modify it under the terms of the GNU General Public License
    10  *  and/or modify it under the terms of the GNU General Public License
    11  *  as published by the Free Software Foundation; version 2 of the License.
    11  *  as published by the Free Software Foundation; either version 2 of the
       
    12  *  License, or (at your option) any later version.
    12  *
    13  *
    13  *  The IgH EtherCAT Master is distributed in the hope that it will be
    14  *  The IgH EtherCAT Master is distributed in the hope that it will be
    14  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  *  GNU General Public License for more details.
    17  *  GNU General Public License for more details.
    17  *
    18  *
    18  *  You should have received a copy of the GNU General Public License
    19  *  You should have received a copy of the GNU General Public License
    19  *  along with the IgH EtherCAT Master; if not, write to the Free Software
    20  *  along with the IgH EtherCAT Master; if not, write to the Free Software
    20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    21  *
    22  *
       
    23  *  The right to use EtherCAT Technology is granted and comes free of
       
    24  *  charge under condition of compatibility of product made by
       
    25  *  Licensee. People intending to distribute/sell products based on the
       
    26  *  code, have to sign an agreement to guarantee that products using
       
    27  *  software based on IgH EtherCAT master stay compatible with the actual
       
    28  *  EtherCAT specification (which are released themselves as an open
       
    29  *  standard) as the (only) precondition to have the right to use EtherCAT
       
    30  *  Technology, IP and trade marks.
       
    31  *
    22  *****************************************************************************/
    32  *****************************************************************************/
    23 
    33 
    24 /**
    34 /**
    25    \file
    35    \file
    26    Ethernet-over-EtherCAT (EoE).
    36    Ethernet-over-EtherCAT (EoE).
    27 */
    37 */
    28 
    38 
    29 /*****************************************************************************/
    39 /*****************************************************************************/
       
    40 
       
    41 #include <linux/etherdevice.h>
    30 
    42 
    31 #include "../include/ecrt.h"
    43 #include "../include/ecrt.h"
    32 #include "globals.h"
    44 #include "globals.h"
    33 #include "master.h"
    45 #include "master.h"
    34 #include "slave.h"
    46 #include "slave.h"
    35 #include "mailbox.h"
    47 #include "mailbox.h"
    36 #include "ethernet.h"
    48 #include "ethernet.h"
    37 
    49 
       
    50 #define EOE_DEBUG_LEVEL 0
       
    51 
       
    52 /*****************************************************************************/
       
    53 
       
    54 void ec_eoe_flush(ec_eoe_t *);
       
    55 
       
    56 // state functions
       
    57 void ec_eoe_state_rx_start(ec_eoe_t *);
       
    58 void ec_eoe_state_rx_check(ec_eoe_t *);
       
    59 void ec_eoe_state_rx_fetch(ec_eoe_t *);
       
    60 void ec_eoe_state_tx_start(ec_eoe_t *);
       
    61 void ec_eoe_state_tx_sent(ec_eoe_t *);
       
    62 
       
    63 // net_device functions
       
    64 int ec_eoedev_open(struct net_device *);
       
    65 int ec_eoedev_stop(struct net_device *);
       
    66 int ec_eoedev_tx(struct sk_buff *, struct net_device *);
       
    67 struct net_device_stats *ec_eoedev_stats(struct net_device *);
       
    68 
    38 /*****************************************************************************/
    69 /*****************************************************************************/
    39 
    70 
    40 /**
    71 /**
    41    EoE constructor.
    72    EoE constructor.
    42 */
    73    Initializes the EoE handler, creates a net_device and registeres it.
    43 
    74 */
    44 void ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
    75 
    45 {
    76 int ec_eoe_init(ec_eoe_t *eoe /**< EoE handler */)
    46     eoe->slave = slave;
    77 {
    47     eoe->rx_state = EC_EOE_IDLE;
    78     ec_eoe_t **priv;
       
    79     int result, i;
       
    80 
       
    81     eoe->slave = NULL;
       
    82     eoe->state = ec_eoe_state_rx_start;
       
    83     eoe->opened = 0;
       
    84     eoe->rx_skb = NULL;
       
    85     eoe->rx_expected_fragment = 0;
       
    86     INIT_LIST_HEAD(&eoe->tx_queue);
       
    87     eoe->tx_frame = NULL;
       
    88     eoe->tx_queue_active = 0;
       
    89     eoe->tx_queued_frames = 0;
       
    90     eoe->tx_queue_lock = SPIN_LOCK_UNLOCKED;
       
    91     eoe->tx_frame_number = 0xFF;
       
    92     memset(&eoe->stats, 0, sizeof(struct net_device_stats));
       
    93 
       
    94     if (!(eoe->dev =
       
    95           alloc_netdev(sizeof(ec_eoe_t *), "eoe%d", ether_setup))) {
       
    96         EC_ERR("Unable to allocate net_device for EoE handler!\n");
       
    97         goto out_return;
       
    98     }
       
    99 
       
   100     // initialize net_device
       
   101     eoe->dev->open = ec_eoedev_open;
       
   102     eoe->dev->stop = ec_eoedev_stop;
       
   103     eoe->dev->hard_start_xmit = ec_eoedev_tx;
       
   104     eoe->dev->get_stats = ec_eoedev_stats;
       
   105 
       
   106     for (i = 0; i < ETH_ALEN; i++)
       
   107         eoe->dev->dev_addr[i] = i | (i << 4);
       
   108 
       
   109     // initialize private data
       
   110     priv = netdev_priv(eoe->dev);
       
   111     *priv = eoe;
       
   112 
       
   113     // Usually setting the MTU appropriately makes the upper layers
       
   114     // do the frame fragmenting. In some cases this doesn't work
       
   115     // so the MTU is left on the Ethernet standard value and fragmenting
       
   116     // is done "manually".
       
   117 #if 0
       
   118     eoe->dev->mtu = slave->sii_rx_mailbox_size - ETH_HLEN - 10;
       
   119 #endif
       
   120 
       
   121     // connect the net_device to the kernel
       
   122     if ((result = register_netdev(eoe->dev))) {
       
   123         EC_ERR("Unable to register net_device: error %i\n", result);
       
   124         goto out_free;
       
   125     }
       
   126 
       
   127     // make the last address octet unique
       
   128     eoe->dev->dev_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
       
   129 
       
   130     return 0;
       
   131 
       
   132  out_free:
       
   133     free_netdev(eoe->dev);
       
   134     eoe->dev = NULL;
       
   135  out_return:
       
   136     return -1;
    48 }
   137 }
    49 
   138 
    50 /*****************************************************************************/
   139 /*****************************************************************************/
    51 
   140 
    52 /**
   141 /**
    53    EoE destructor.
   142    EoE destructor.
    54 */
   143    Unregisteres the net_device and frees allocated memory.
    55 
   144 */
    56 void ec_eoe_clear(ec_eoe_t *eoe)
   145 
    57 {
   146 void ec_eoe_clear(ec_eoe_t *eoe /**< EoE handler */)
       
   147 {
       
   148     if (eoe->dev) {
       
   149         unregister_netdev(eoe->dev);
       
   150         free_netdev(eoe->dev);
       
   151     }
       
   152 
       
   153     // empty transmit queue
       
   154     ec_eoe_flush(eoe);
       
   155 
       
   156     if (eoe->tx_frame) {
       
   157         dev_kfree_skb(eoe->tx_frame->skb);
       
   158         kfree(eoe->tx_frame);
       
   159     }
       
   160 
       
   161     if (eoe->rx_skb) dev_kfree_skb(eoe->rx_skb);
       
   162 }
       
   163 
       
   164 /*****************************************************************************/
       
   165 
       
   166 /**
       
   167    Empties the transmit queue.
       
   168 */
       
   169 
       
   170 void ec_eoe_flush(ec_eoe_t *eoe /**< EoE handler */)
       
   171 {
       
   172     ec_eoe_frame_t *frame, *next;
       
   173 
       
   174     spin_lock_bh(&eoe->tx_queue_lock);
       
   175 
       
   176     list_for_each_entry_safe(frame, next, &eoe->tx_queue, queue) {
       
   177         list_del(&frame->queue);
       
   178         dev_kfree_skb(frame->skb);
       
   179         kfree(frame);
       
   180     }
       
   181     eoe->tx_queued_frames = 0;
       
   182 
       
   183     spin_unlock_bh(&eoe->tx_queue_lock);
       
   184 }
       
   185 
       
   186 /*****************************************************************************/
       
   187 
       
   188 /**
       
   189    Sends a frame or the next fragment.
       
   190 */
       
   191 
       
   192 int ec_eoe_send(ec_eoe_t *eoe /**< EoE handler */)
       
   193 {
       
   194     size_t remaining_size, current_size, complete_offset;
       
   195     unsigned int last_fragment;
       
   196     uint8_t *data;
       
   197 #if EOE_DEBUG_LEVEL > 1
       
   198     unsigned int i;
       
   199 #endif
       
   200 
       
   201     remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
       
   202 
       
   203     if (remaining_size <= eoe->slave->sii_tx_mailbox_size - 10) {
       
   204         current_size = remaining_size;
       
   205         last_fragment = 1;
       
   206     }
       
   207     else {
       
   208         current_size = ((eoe->slave->sii_tx_mailbox_size - 10) / 32) * 32;
       
   209         last_fragment = 0;
       
   210     }
       
   211 
       
   212     if (eoe->tx_fragment_number) {
       
   213         complete_offset = eoe->tx_offset / 32;
       
   214     }
       
   215     else {
       
   216         complete_offset = remaining_size / 32 + 1;
       
   217     }
       
   218 
       
   219 #if EOE_DEBUG_LEVEL > 0
       
   220     EC_DBG("EoE TX sending %sfragment %i with %i octets (%i)."
       
   221            " %i frames queued.\n", last_fragment ? "last " : "",
       
   222            eoe->tx_fragment_number, current_size, complete_offset,
       
   223            eoe->tx_queued_frames);
       
   224 #endif
       
   225 
       
   226 #if EOE_DEBUG_LEVEL > 1
       
   227     EC_DBG("");
       
   228     for (i = 0; i < current_size; i++) {
       
   229         printk("%02X ", frame->skb->data[eoe->tx_offset + i]);
       
   230         if ((i + 1) % 16 == 0) {
       
   231             printk("\n");
       
   232             EC_DBG("");
       
   233         }
       
   234     }
       
   235     printk("\n");
       
   236 #endif
       
   237 
       
   238     if (!(data = ec_slave_mbox_prepare_send(eoe->slave, 0x02,
       
   239                                             current_size + 4)))
       
   240         return -1;
       
   241 
       
   242     EC_WRITE_U8 (data,     0x00); // eoe fragment req.
       
   243     EC_WRITE_U8 (data + 1, last_fragment);
       
   244     EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
       
   245                             (complete_offset & 0x3F) << 6 |
       
   246                             (eoe->tx_frame_number & 0x0F) << 12));
       
   247 
       
   248     memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
       
   249     ec_master_queue_command(eoe->slave->master, &eoe->slave->mbox_command);
       
   250 
       
   251     eoe->tx_offset += current_size;
       
   252     eoe->tx_fragment_number++;
       
   253     return 0;
    58 }
   254 }
    59 
   255 
    60 /*****************************************************************************/
   256 /*****************************************************************************/
    61 
   257 
    62 /**
   258 /**
    63    Runs the EoE state machine.
   259    Runs the EoE state machine.
    64 */
   260 */
    65 
   261 
    66 void ec_eoe_run(ec_eoe_t *eoe)
   262 void ec_eoe_run(ec_eoe_t *eoe /**< EoE handler */)
    67 {
   263 {
    68     uint8_t *data;
   264     if (!eoe->opened) return;
    69     ec_master_t *master;
   265 
    70     size_t rec_size;
   266     // call state function
    71 #if 0
   267     eoe->state(eoe);
    72     unsigned int i;
   268 }
    73     uint8_t fragment_number;
   269 
    74     uint8_t complete_size;
   270 /*****************************************************************************/
    75     uint8_t frame_number;
   271 
    76     uint8_t last_fragment;
   272 /**
    77 #endif
   273    Returns the state of the device.
    78 
   274    \return 1 if the device is "up", 0 if it is "down"
    79     master = eoe->slave->master;
   275 */
    80 
   276 
    81     if (eoe->rx_state == EC_EOE_IDLE) {
   277 unsigned int ec_eoe_active(const ec_eoe_t *eoe /**< EoE handler */)
    82         ec_slave_mbox_prepare_check(eoe->slave);
   278 {
    83         ec_master_queue_command(master, &eoe->slave->mbox_command);
   279     return eoe->slave && eoe->opened;
    84         eoe->rx_state = EC_EOE_CHECKING;
   280 }
    85         return;
   281 
    86     }
   282 /*****************************************************************************/
    87 
   283 
    88     if (eoe->rx_state == EC_EOE_CHECKING) {
   284 /**
    89         if (eoe->slave->mbox_command.state != EC_CMD_RECEIVED) {
   285    Prints EoE handler information.
    90             master->stats.eoe_errors++;
   286 */
    91             eoe->rx_state = EC_EOE_IDLE;
   287 
    92             return;
   288 void ec_eoe_print(const ec_eoe_t *eoe /**< EoE handler */)
    93         }
   289 {
    94         if (!ec_slave_mbox_check(eoe->slave)) {
   290     EC_INFO("  EoE handler %s\n", eoe->dev->name);
    95             eoe->rx_state = EC_EOE_IDLE;
   291     EC_INFO("    State: %s\n", eoe->opened ? "opened" : "closed");
    96             return;
   292     if (eoe->slave)
    97         }
   293         EC_INFO("    Coupled to slave %i.\n", eoe->slave->ring_position);
    98         ec_slave_mbox_prepare_fetch(eoe->slave);
   294     else
    99         ec_master_queue_command(master, &eoe->slave->mbox_command);
   295         EC_INFO("    Not coupled.\n");
   100         eoe->rx_state = EC_EOE_FETCHING;
   296 }
   101         return;
   297 
   102     }
   298 /******************************************************************************
   103 
   299  *  STATE PROCESSING FUNCTIONS
   104     if (eoe->rx_state == EC_EOE_FETCHING) {
   300  *****************************************************************************/
   105         if (eoe->slave->mbox_command.state != EC_CMD_RECEIVED) {
   301 
   106             master->stats.eoe_errors++;
   302 /**
   107             eoe->rx_state = EC_EOE_IDLE;
   303    State: RX_START.
   108             return;
   304    Starts a new receiving sequence by queueing a command that checks the
   109         }
   305    slave's mailbox for a new EoE command.
   110         if (!(data = ec_slave_mbox_fetch(eoe->slave, 0x02, &rec_size))) {
   306 */
   111             master->stats.eoe_errors++;
   307 
   112             eoe->rx_state = EC_EOE_IDLE;
   308 void ec_eoe_state_rx_start(ec_eoe_t *eoe /**< EoE handler */)
   113             return;
   309 {
   114         }
   310     if (!eoe->slave->online || !eoe->slave->master->device->link_state)
   115 
   311         return;
   116 #if 0
   312 
       
   313     ec_slave_mbox_prepare_check(eoe->slave);
       
   314     ec_master_queue_command(eoe->slave->master, &eoe->slave->mbox_command);
       
   315     eoe->state = ec_eoe_state_rx_check;
       
   316 }
       
   317 
       
   318 /*****************************************************************************/
       
   319 
       
   320 /**
       
   321    State: RX_CHECK.
       
   322    Processes the checking command sent in RX_START and issues a receive
       
   323    command, if new data is available.
       
   324 */
       
   325 
       
   326 void ec_eoe_state_rx_check(ec_eoe_t *eoe /**< EoE handler */)
       
   327 {
       
   328     if (eoe->slave->mbox_command.state != EC_CMD_RECEIVED) {
       
   329         eoe->stats.rx_errors++;
       
   330         eoe->state = ec_eoe_state_tx_start;
       
   331         return;
       
   332     }
       
   333 
       
   334     if (!ec_slave_mbox_check(eoe->slave)) {
       
   335         eoe->state = ec_eoe_state_tx_start;
       
   336         return;
       
   337     }
       
   338 
       
   339     ec_slave_mbox_prepare_fetch(eoe->slave);
       
   340     ec_master_queue_command(eoe->slave->master, &eoe->slave->mbox_command);
       
   341     eoe->state = ec_eoe_state_rx_fetch;
       
   342 }
       
   343 
       
   344 /*****************************************************************************/
       
   345 
       
   346 /**
       
   347    State: RX_FETCH.
       
   348    Checks if the requested data of RX_CHECK was received and processes the
       
   349    EoE command.
       
   350 */
       
   351 
       
   352 void ec_eoe_state_rx_fetch(ec_eoe_t *eoe /**< EoE handler */)
       
   353 {
       
   354     size_t rec_size, data_size;
       
   355     uint8_t *data, frame_type, last_fragment, time_appended;
       
   356     uint8_t frame_number, fragment_offset, fragment_number;
       
   357     off_t offset;
       
   358 
       
   359     if (eoe->slave->mbox_command.state != EC_CMD_RECEIVED) {
       
   360         eoe->stats.rx_errors++;
       
   361         eoe->state = ec_eoe_state_tx_start;
       
   362         return;
       
   363     }
       
   364 
       
   365     if (!(data = ec_slave_mbox_fetch(eoe->slave, 0x02, &rec_size))) {
       
   366         eoe->stats.rx_errors++;
       
   367         eoe->state = ec_eoe_state_tx_start;
       
   368         return;
       
   369     }
       
   370 
       
   371     frame_type = EC_READ_U16(data) & 0x000F;
       
   372 
       
   373     if (frame_type == 0x00) { // EoE Fragment Request
       
   374         last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
       
   375         time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
   117         fragment_number = EC_READ_U16(data + 2) & 0x003F;
   376         fragment_number = EC_READ_U16(data + 2) & 0x003F;
   118         complete_size = (EC_READ_U16(data + 2) >> 6) & 0x003F;
   377         fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
   119         frame_number = (EC_READ_U16(data + 2) >> 12) & 0x0003;
   378         frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
   120         last_fragment = (EC_READ_U16(data + 2) >> 15) & 0x0001;
   379 
   121 
   380 #if EOE_DEBUG_LEVEL > 0
   122         EC_DBG("EOE %s received, fragment: %i, complete size: %i (0x%02X),"
   381         EC_DBG("EoE RX fragment %i, offset %i, frame %i%s%s,"
   123                " frame %i%s\n",
   382                " %i octets\n", fragment_number, fragment_offset,
   124                fragment_number ? "fragment" : "initiate", fragment_number,
   383                frame_number,
   125                (complete_size - 31) / 32, complete_size, frame_number,
   384                last_fragment ? ", last fragment" : "",
   126                last_fragment ? ", last fragment" : "");
   385                time_appended ? ", + timestamp" : "",
       
   386                time_appended ? rec_size - 8 : rec_size - 4);
       
   387 #endif
       
   388 
       
   389 #if EOE_DEBUG_LEVEL > 1
   127         EC_DBG("");
   390         EC_DBG("");
   128         for (i = 0; i < rec_size - 2; i++) {
   391         for (i = 0; i < rec_size - 4; i++) {
   129             printk("%02X ", data[i + 2]);
   392             printk("%02X ", data[i + 4]);
   130             if ((i + 1) % 16 == 0) {
   393             if ((i + 1) % 16 == 0) {
   131                 printk("\n");
   394                 printk("\n");
   132                 EC_DBG("");
   395                 EC_DBG("");
   133             }
   396             }
   134         }
   397         }
   135         printk("\n");
   398         printk("\n");
   136 #endif
   399 #endif
   137 
   400 
   138         eoe->rx_state = EC_EOE_IDLE;
   401         data_size = time_appended ? rec_size - 8 : rec_size - 4;
   139         return;
   402 
   140     }
   403         if (!fragment_number) {
   141 }
   404             if (eoe->rx_skb) {
   142 
   405                 EC_WARN("EoE RX freeing old socket buffer...\n");
   143 /*****************************************************************************/
   406                 dev_kfree_skb(eoe->rx_skb);
   144 
   407             }
   145 /**
   408 
   146    Prints EoE object information.
   409             // new socket buffer
   147 */
   410             if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
   148 
   411                 if (printk_ratelimit())
   149 void ec_eoe_print(const ec_eoe_t *eoe)
   412                     EC_WARN("EoE RX low on mem. frame dropped.\n");
   150 {
   413                 eoe->stats.rx_dropped++;
   151     EC_INFO("  EoE slave %i\n", eoe->slave->ring_position);
   414                 eoe->state = ec_eoe_state_tx_start;
   152     EC_INFO("    RX State %i\n", eoe->rx_state);
   415                 return;
   153 }
   416             }
   154 
   417 
   155 /*****************************************************************************/
   418             eoe->rx_skb_offset = 0;
       
   419             eoe->rx_skb_size = fragment_offset * 32;
       
   420             eoe->rx_expected_fragment = 0;
       
   421         }
       
   422         else {
       
   423             if (!eoe->rx_skb) {
       
   424                 eoe->stats.rx_dropped++;
       
   425                 eoe->state = ec_eoe_state_tx_start;
       
   426                 return;
       
   427             }
       
   428 
       
   429             offset = fragment_offset * 32;
       
   430             if (offset != eoe->rx_skb_offset ||
       
   431                 offset + data_size > eoe->rx_skb_size ||
       
   432                 fragment_number != eoe->rx_expected_fragment) {
       
   433                 eoe->stats.rx_errors++;
       
   434                 eoe->state = ec_eoe_state_tx_start;
       
   435                 dev_kfree_skb(eoe->rx_skb);
       
   436                 eoe->rx_skb = NULL;
       
   437                 return;
       
   438             }
       
   439         }
       
   440 
       
   441         // copy fragment into socket buffer
       
   442         memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
       
   443         eoe->rx_skb_offset += data_size;
       
   444 
       
   445         if (last_fragment) {
       
   446             // update statistics
       
   447             eoe->stats.rx_packets++;
       
   448             eoe->stats.rx_bytes += eoe->rx_skb->len;
       
   449 
       
   450 #if EOE_DEBUG_LEVEL > 0
       
   451             EC_DBG("EoE RX frame completed with %u octets.\n",
       
   452                    eoe->rx_skb->len);
       
   453 #endif
       
   454 
       
   455             // pass socket buffer to network stack
       
   456             eoe->rx_skb->dev = eoe->dev;
       
   457             eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
       
   458             eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
       
   459             if (netif_rx(eoe->rx_skb)) {
       
   460                 EC_WARN("EoE RX netif_rx failed.\n");
       
   461             }
       
   462             eoe->rx_skb = NULL;
       
   463 
       
   464             eoe->state = ec_eoe_state_tx_start;
       
   465         }
       
   466         else {
       
   467             eoe->rx_expected_fragment++;
       
   468 #if EOE_DEBUG_LEVEL > 0
       
   469             EC_DBG("EoE RX expecting fragment %i\n",
       
   470                    eoe->rx_expected_fragment);
       
   471 #endif
       
   472             eoe->state = ec_eoe_state_rx_start;
       
   473         }
       
   474     }
       
   475     else {
       
   476 #if EOE_DEBUG_LEVEL > 0
       
   477         EC_DBG("other frame received.\n");
       
   478 #endif
       
   479         eoe->stats.rx_dropped++;
       
   480         eoe->state = ec_eoe_state_tx_start;
       
   481     }
       
   482 }
       
   483 
       
   484 /*****************************************************************************/
       
   485 
       
   486 /**
       
   487    State: TX START.
       
   488    Starts a new transmit sequence. If no data is available, a new receive
       
   489    sequence is started instead.
       
   490 */
       
   491 
       
   492 void ec_eoe_state_tx_start(ec_eoe_t *eoe /**< EoE handler */)
       
   493 {
       
   494 #if EOE_DEBUG_LEVEL > 0
       
   495     unsigned int wakeup;
       
   496 #endif
       
   497 
       
   498     if (!eoe->slave->online || !eoe->slave->master->device->link_state)
       
   499         return;
       
   500 
       
   501     spin_lock_bh(&eoe->tx_queue_lock);
       
   502 
       
   503     if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
       
   504         spin_unlock_bh(&eoe->tx_queue_lock);
       
   505         // no data available.
       
   506         // start a new receive immediately.
       
   507         ec_eoe_state_rx_start(eoe);
       
   508         return;
       
   509     }
       
   510 
       
   511     // take the first frame out of the queue
       
   512     eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
       
   513     list_del(&eoe->tx_frame->queue);
       
   514     if (!eoe->tx_queue_active &&
       
   515         eoe->tx_queued_frames == EC_EOE_TX_QUEUE_SIZE / 2) {
       
   516         netif_wake_queue(eoe->dev);
       
   517         eoe->tx_queue_active = 1;
       
   518 #if EOE_DEBUG_LEVEL > 0
       
   519         wakeup = 1;
       
   520 #endif
       
   521     }
       
   522 
       
   523     eoe->tx_queued_frames--;
       
   524     spin_unlock_bh(&eoe->tx_queue_lock);
       
   525 
       
   526     eoe->tx_frame_number++;
       
   527     eoe->tx_frame_number %= 16;
       
   528     eoe->tx_fragment_number = 0;
       
   529     eoe->tx_offset = 0;
       
   530 
       
   531     if (ec_eoe_send(eoe)) {
       
   532         dev_kfree_skb(eoe->tx_frame->skb);
       
   533         kfree(eoe->tx_frame);
       
   534         eoe->tx_frame = NULL;
       
   535         eoe->stats.tx_errors++;
       
   536         eoe->state = ec_eoe_state_rx_start;
       
   537         return;
       
   538     }
       
   539 
       
   540 #if EOE_DEBUG_LEVEL > 0
       
   541     if (wakeup) EC_DBG("waking up TX queue...\n");
       
   542 #endif
       
   543 
       
   544     eoe->state = ec_eoe_state_tx_sent;
       
   545 }
       
   546 
       
   547 /*****************************************************************************/
       
   548 
       
   549 /**
       
   550    State: TX SENT.
       
   551    Checks is the previous transmit command succeded and sends the next
       
   552    fragment, if necessary.
       
   553 */
       
   554 
       
   555 void ec_eoe_state_tx_sent(ec_eoe_t *eoe /**< EoE handler */)
       
   556 {
       
   557     if (eoe->slave->mbox_command.state != EC_CMD_RECEIVED) {
       
   558         eoe->stats.tx_errors++;
       
   559         eoe->state = ec_eoe_state_rx_start;
       
   560         return;
       
   561     }
       
   562 
       
   563     if (eoe->slave->mbox_command.working_counter != 1) {
       
   564         eoe->stats.tx_errors++;
       
   565         eoe->state = ec_eoe_state_rx_start;
       
   566         return;
       
   567     }
       
   568 
       
   569     // frame completely sent
       
   570     if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
       
   571         eoe->stats.tx_packets++;
       
   572         eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
       
   573         dev_kfree_skb(eoe->tx_frame->skb);
       
   574         kfree(eoe->tx_frame);
       
   575         eoe->tx_frame = NULL;
       
   576         eoe->state = ec_eoe_state_rx_start;
       
   577     }
       
   578     else { // send next fragment
       
   579         if (ec_eoe_send(eoe)) {
       
   580             dev_kfree_skb(eoe->tx_frame->skb);
       
   581             kfree(eoe->tx_frame);
       
   582             eoe->tx_frame = NULL;
       
   583             eoe->stats.tx_errors++;
       
   584             eoe->state = ec_eoe_state_rx_start;
       
   585         }
       
   586     }
       
   587 }
       
   588 
       
   589 /******************************************************************************
       
   590  *  NET_DEVICE functions
       
   591  *****************************************************************************/
       
   592 
       
   593 /**
       
   594    Opens the virtual network device.
       
   595 */
       
   596 
       
   597 int ec_eoedev_open(struct net_device *dev /**< EoE net_device */)
       
   598 {
       
   599     ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
       
   600     ec_eoe_flush(eoe);
       
   601     eoe->opened = 1;
       
   602     netif_start_queue(dev);
       
   603     eoe->tx_queue_active = 1;
       
   604     EC_INFO("%s opened.\n", dev->name);
       
   605     if (!eoe->slave)
       
   606         EC_WARN("device %s is not coupled to any EoE slave!\n", dev->name);
       
   607     else {
       
   608         eoe->slave->requested_state = EC_SLAVE_STATE_OP;
       
   609         eoe->slave->state_error = 0;
       
   610     }
       
   611     return 0;
       
   612 }
       
   613 
       
   614 /*****************************************************************************/
       
   615 
       
   616 /**
       
   617    Stops the virtual network device.
       
   618 */
       
   619 
       
   620 int ec_eoedev_stop(struct net_device *dev /**< EoE net_device */)
       
   621 {
       
   622     ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
       
   623     netif_stop_queue(dev);
       
   624     eoe->tx_queue_active = 0;
       
   625     eoe->opened = 0;
       
   626     ec_eoe_flush(eoe);
       
   627     EC_INFO("%s stopped.\n", dev->name);
       
   628     if (!eoe->slave)
       
   629         EC_WARN("device %s is not coupled to any EoE slave!\n", dev->name);
       
   630     else {
       
   631         eoe->slave->requested_state = EC_SLAVE_STATE_INIT;
       
   632         eoe->slave->state_error = 0;
       
   633     }
       
   634     return 0;
       
   635 }
       
   636 
       
   637 /*****************************************************************************/
       
   638 
       
   639 /**
       
   640    Transmits data via the virtual network device.
       
   641 */
       
   642 
       
   643 int ec_eoedev_tx(struct sk_buff *skb, /**< transmit socket buffer */
       
   644                  struct net_device *dev /**< EoE net_device */
       
   645                 )
       
   646 {
       
   647     ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
       
   648     ec_eoe_frame_t *frame;
       
   649 
       
   650 #if 0
       
   651     if (skb->len > eoe->slave->sii_tx_mailbox_size - 10) {
       
   652         EC_WARN("EoE TX frame (%i octets) exceeds MTU. dropping.\n", skb->len);
       
   653         dev_kfree_skb(skb);
       
   654         eoe->stats.tx_dropped++;
       
   655         return 0;
       
   656     }
       
   657 #endif
       
   658 
       
   659     if (!(frame =
       
   660           (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
       
   661         if (printk_ratelimit())
       
   662             EC_WARN("EoE TX: low on mem. frame dropped.\n");
       
   663         return 1;
       
   664     }
       
   665 
       
   666     frame->skb = skb;
       
   667 
       
   668     spin_lock_bh(&eoe->tx_queue_lock);
       
   669     list_add_tail(&frame->queue, &eoe->tx_queue);
       
   670     eoe->tx_queued_frames++;
       
   671     if (eoe->tx_queued_frames == EC_EOE_TX_QUEUE_SIZE) {
       
   672         netif_stop_queue(dev);
       
   673         eoe->tx_queue_active = 0;
       
   674     }
       
   675     spin_unlock_bh(&eoe->tx_queue_lock);
       
   676 
       
   677 #if EOE_DEBUG_LEVEL > 0
       
   678     EC_DBG("EoE TX queued frame with %i octets (%i frames queued).\n",
       
   679            skb->len, eoe->tx_queued_frames);
       
   680     if (!eoe->tx_queue_active)
       
   681         EC_WARN("EoE TX queue is now full.\n");
       
   682 #endif
       
   683 
       
   684     return 0;
       
   685 }
       
   686 
       
   687 /*****************************************************************************/
       
   688 
       
   689 /**
       
   690    Gets statistics about the virtual network device.
       
   691 */
       
   692 
       
   693 struct net_device_stats *ec_eoedev_stats(struct net_device *dev
       
   694                                          /**< EoE net_device */)
       
   695 {
       
   696     ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
       
   697     return &eoe->stats;
       
   698 }
       
   699 
       
   700 /*****************************************************************************/