Improved EoE handling: Avoided skipping of datagrams and release lock
authorFlorian Pose <fp@igh-essen.com>
Wed, 12 Sep 2007 14:48:53 +0000
changeset 721 ebc2fd3e09e5
parent 720 9664a19978dd
child 722 35d1bb508d09
Improved EoE handling: Avoided skipping of datagrams and release lock
while processing.
master/ethernet.c
master/ethernet.h
master/master.c
--- a/master/ethernet.c	Wed Sep 12 14:07:28 2007 +0000
+++ b/master/ethernet.c	Wed Sep 12 14:48:53 2007 +0000
@@ -99,6 +99,7 @@
     eoe->slave = slave;
 
     ec_datagram_init(&eoe->datagram);
+    eoe->queue_datagram = 0;
     eoe->state = ec_eoe_state_rx_start;
     eoe->opened = 0;
     eoe->rx_skb = NULL;
@@ -283,7 +284,7 @@
                             (eoe->tx_frame_number & 0x0F) << 12));
 
     memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
-    ec_master_queue_datagram(eoe->slave->master, &eoe->datagram);
+    eoe->queue_datagram = 1;
 
     eoe->tx_offset += current_size;
     eoe->tx_fragment_number++;
@@ -300,6 +301,10 @@
 {
     if (!eoe->opened) return;
 
+    // if the datagram was not sent, or is not yet received, skip this cycle
+    if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
+        return;
+
     // call state function
     eoe->state(eoe);
 
@@ -311,12 +316,27 @@
         eoe->tx_counter = 0;
         eoe->rate_jiffies = jiffies;
     }
+
     ec_datagram_output_stats(&eoe->datagram);
 }
 
 /*****************************************************************************/
 
 /**
+ * Queues the datagram, if necessary.
+ */
+
+void ec_eoe_queue(ec_eoe_t *eoe /**< EoE handler */)
+{
+   if (eoe->queue_datagram) {
+       ec_master_queue_datagram(eoe->slave->master, &eoe->datagram);
+       eoe->queue_datagram = 0;
+   }
+}
+
+/*****************************************************************************/
+
+/**
    Returns the state of the device.
    \return 1 if the device is "up", 0 if it is "down"
 */
@@ -343,7 +363,7 @@
         return;
 
     ec_slave_mbox_prepare_check(eoe->slave, &eoe->datagram);
-    ec_master_queue_datagram(eoe->slave->master, &eoe->datagram);
+    eoe->queue_datagram = 1;
     eoe->state = ec_eoe_state_rx_check;
 }
 
@@ -369,7 +389,7 @@
     }
 
     ec_slave_mbox_prepare_fetch(eoe->slave, &eoe->datagram);
-    ec_master_queue_datagram(eoe->slave->master, &eoe->datagram);
+    eoe->queue_datagram = 1;
     eoe->state = ec_eoe_state_rx_fetch;
 }
 
--- a/master/ethernet.h	Wed Sep 12 14:07:28 2007 +0000
+++ b/master/ethernet.h	Wed Sep 12 14:48:53 2007 +0000
@@ -74,6 +74,7 @@
     struct list_head list; /**< list item */
     ec_slave_t *slave; /**< pointer to the corresponding slave */
     ec_datagram_t datagram; /**< datagram */
+    unsigned int queue_datagram; /**< the datagram is ready for queuing */
     void (*state)(ec_eoe_t *); /**< state function for the state machine */
     struct net_device *dev; /**< net_device for virtual ethernet device */
     struct net_device_stats stats; /**< device statistics */
@@ -102,6 +103,7 @@
 int ec_eoe_init(ec_eoe_t *, ec_slave_t *);
 void ec_eoe_clear(ec_eoe_t *);
 void ec_eoe_run(ec_eoe_t *);
+void ec_eoe_queue(ec_eoe_t *);
 int ec_eoe_is_open(const ec_eoe_t *);
 
 /*****************************************************************************/
--- a/master/master.c	Wed Sep 12 14:07:28 2007 +0000
+++ b/master/master.c	Wed Sep 12 14:48:53 2007 +0000
@@ -1162,7 +1162,6 @@
     cycles_t cycles_start, cycles_end;
     unsigned long restart_jiffies;
 
-
     list_for_each_entry(eoe, &master->eoe_handlers, list) {
         if (ec_eoe_is_open(eoe)) {
             none_open = 0;
@@ -1172,11 +1171,11 @@
     if (none_open)
         goto queue_timer;
 
+    // receive datagrams
     if (master->request_cb(master->cb_data)) goto queue_timer;
-
-    // receive datagrams
     cycles_start = get_cycles();
     ecrt_master_receive(master);
+    master->release_cb(master->cb_data);
 
     // actual EoE processing
     list_for_each_entry(eoe, &master->eoe_handlers, list) {
@@ -1184,11 +1183,16 @@
     }
 
     // send datagrams
+    if (master->request_cb(master->cb_data)) {
+        goto queue_timer;
+    }
+    list_for_each_entry(eoe, &master->eoe_handlers, list) {
+        ec_eoe_queue(eoe);
+    }
     ecrt_master_send(master);
+    master->release_cb(master->cb_data);
     cycles_end = get_cycles();
 
-    master->release_cb(master->cb_data);
-
     master->eoe_cycle_times[master->eoe_cycle_time_pos]
         = (u32) (cycles_end - cycles_start) * 1000 / cpu_khz;
     master->eoe_cycle_time_pos++;
@@ -1197,7 +1201,7 @@
  queue_timer:
     restart_jiffies = HZ / EC_EOE_FREQUENCY;
     if (!restart_jiffies) restart_jiffies = 1;
-    master->eoe_timer.expires += restart_jiffies;
+    master->eoe_timer.expires = jiffies + restart_jiffies;
     add_timer(&master->eoe_timer);
 }
 #endif