master/device.c
changeset 1851 a56bd34e20a1
parent 1554 e07a0f8de03d
child 1852 1ec9f781f3ef
equal deleted inserted replaced
1850:fa112b8a371b 1851:a56bd34e20a1
    52             (result)->tv_usec += 1000000; \
    52             (result)->tv_usec += 1000000; \
    53         } \
    53         } \
    54     } while (0)
    54     } while (0)
    55 #endif
    55 #endif
    56 
    56 
       
    57 static const unsigned int rate_intervals[] = {
       
    58     1, 10, 60
       
    59 };
       
    60 
    57 /*****************************************************************************/
    61 /*****************************************************************************/
    58 
    62 
    59 /** Constructor.
    63 /** Constructor.
    60  * 
    64  * 
    61  * \return 0 in case of success, else < 0
    65  * \return 0 in case of success, else < 0
   194     device->module = NULL;
   198     device->module = NULL;
   195     device->open = 0;
   199     device->open = 0;
   196     device->link_state = 0; // down
   200     device->link_state = 0; // down
   197     device->tx_count = 0;
   201     device->tx_count = 0;
   198     device->rx_count = 0;
   202     device->rx_count = 0;
       
   203     device->last_tx_count = 0;
       
   204     device->last_loss = 0;
       
   205     for (i = 0; i < EC_RATE_COUNT; i++) {
       
   206         device->tx_rates[i] = 0;
       
   207         device->loss_rates[i] = 0;
       
   208     }
       
   209     device->stats_jiffies = 0;
   199     for (i = 0; i < EC_TX_RING_SIZE; i++)
   210     for (i = 0; i < EC_TX_RING_SIZE; i++)
   200         device->tx_skb[i]->dev = NULL;
   211         device->tx_skb[i]->dev = NULL;
   201 }
   212 }
   202 
   213 
   203 /*****************************************************************************/
   214 /*****************************************************************************/
   209 int ec_device_open(
   220 int ec_device_open(
   210         ec_device_t *device /**< EtherCAT device */
   221         ec_device_t *device /**< EtherCAT device */
   211         )
   222         )
   212 {
   223 {
   213     int ret;
   224     int ret;
       
   225     unsigned int i;
   214 
   226 
   215     if (!device->dev) {
   227     if (!device->dev) {
   216         EC_ERR("No net_device to open!\n");
   228         EC_ERR("No net_device to open!\n");
   217         return -ENODEV;
   229         return -ENODEV;
   218     }
   230     }
   223     }
   235     }
   224 
   236 
   225     device->link_state = 0;
   237     device->link_state = 0;
   226     device->tx_count = 0;
   238     device->tx_count = 0;
   227     device->rx_count = 0;
   239     device->rx_count = 0;
       
   240     device->last_tx_count = 0;
       
   241     device->last_loss = 0;
       
   242     for (i = 0; i < EC_RATE_COUNT; i++) {
       
   243         device->tx_rates[i] = 0;
       
   244         device->loss_rates[i] = 0;
       
   245     }
       
   246     device->stats_jiffies = 0;
   228 
   247 
   229 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
   248 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
   230     ret = device->dev->netdev_ops->ndo_open(device->dev);
   249     ret = device->dev->netdev_ops->ndo_open(device->dev);
   231 #else
   250 #else
   232     ret = device->dev->open(device->dev);
   251     ret = device->dev->open(device->dev);
   299         ec_device_t *device, /**< EtherCAT device */
   318         ec_device_t *device, /**< EtherCAT device */
   300         size_t size /**< number of bytes to send */
   319         size_t size /**< number of bytes to send */
   301         )
   320         )
   302 {
   321 {
   303     struct sk_buff *skb = device->tx_skb[device->tx_ring_index];
   322     struct sk_buff *skb = device->tx_skb[device->tx_ring_index];
       
   323 
       
   324     // frame statistics
       
   325     if (unlikely(jiffies - device->stats_jiffies >= HZ)) {
       
   326         unsigned int i;
       
   327         unsigned int tx_rate = device->tx_count - device->last_tx_count;
       
   328         int loss = device->tx_count - device->rx_count;
       
   329         int loss_rate = (loss - device->last_loss) * 1000;
       
   330         for (i = 0; i < EC_RATE_COUNT; i++) {
       
   331             unsigned int n = rate_intervals[i];
       
   332             device->tx_rates[i] =
       
   333                 device->tx_rates[i] * (n - 1) +
       
   334                 tx_rate * n;
       
   335             device->loss_rates[i] =
       
   336                 device->loss_rates[i] * (n - 1) +
       
   337                 loss_rate * n;
       
   338         }
       
   339         device->last_tx_count = device->tx_count;
       
   340         device->last_loss = loss;
       
   341         device->stats_jiffies += HZ;
       
   342     }
   304 
   343 
   305     if (unlikely(!device->link_state)) // Link down
   344     if (unlikely(!device->link_state)) // Link down
   306         return;
   345         return;
   307 
   346 
   308     // set the right length for the data
   347     // set the right length for the data