IgH EtherCAT Master  1.6.0-rc1
ethernet.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * $Id$
4  *
5  * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
6  *
7  * This file is part of the IgH EtherCAT Master.
8  *
9  * The IgH EtherCAT Master is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2, as
11  * published by the Free Software Foundation.
12  *
13  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * 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  *
22  * ---
23  *
24  * The license mentioned above concerns the source code only. Using the
25  * EtherCAT technology and brand is only permitted in compliance with the
26  * industrial property and similar rights of Beckhoff Automation GmbH.
27  *
28  *****************************************************************************/
29 
35 /*****************************************************************************/
36 
37 #include <linux/version.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 
41 #include "globals.h"
42 #include "master.h"
43 #include "slave.h"
44 #include "mailbox.h"
45 #include "ethernet.h"
46 
47 /*****************************************************************************/
48 
56 #define EOE_DEBUG_LEVEL 1
57 
60 #define EC_EOE_TX_QUEUE_SIZE 100
61 
64 #define EC_EOE_TRIES 100
65 
66 /*****************************************************************************/
67 
68 void ec_eoe_flush(ec_eoe_t *);
69 
70 // state functions
77 
78 // net_device functions
79 int ec_eoedev_open(struct net_device *);
80 int ec_eoedev_stop(struct net_device *);
81 int ec_eoedev_tx(struct sk_buff *, struct net_device *);
82 struct net_device_stats *ec_eoedev_stats(struct net_device *);
83 static int ec_eoedev_set_mac(struct net_device *netdev, void *p);
84 
85 /*****************************************************************************/
86 
87 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
88 
90 static const struct net_device_ops ec_eoedev_ops = {
91  .ndo_open = ec_eoedev_open,
92  .ndo_stop = ec_eoedev_stop,
93  .ndo_start_xmit = ec_eoedev_tx,
94  .ndo_get_stats = ec_eoedev_stats,
95  .ndo_set_mac_address = ec_eoedev_set_mac,
96 };
97 #endif
98 
99 /*****************************************************************************/
100 
108 static int
109 ec_eoedev_set_mac(struct net_device *netdev, void *p)
110 {
111  struct sockaddr *addr = p;
112 
113  if (!is_valid_ether_addr(addr->sa_data)) {
114  return -EADDRNOTAVAIL;
115  }
116 
117 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
118  eth_hw_addr_set(netdev, addr->sa_data);
119 #else
120  memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
121 #endif
122 
123  return 0;
124 }
125 
126 /*****************************************************************************/
127 
135  ec_eoe_t *eoe,
136  ec_slave_t *slave
137  )
138 {
139  ec_eoe_t **priv;
140  int ret = 0;
141  char name[EC_DATAGRAM_NAME_SIZE];
142  u8 mac_addr[ETH_ALEN];
143 
144  struct net_device *dev;
145  unsigned char lo_mac[ETH_ALEN] = {0};
146  unsigned int use_master_mac = 0;
147 
148  eoe->slave = slave;
149 
150  ec_datagram_init(&eoe->datagram);
151  eoe->queue_datagram = 0;
153  eoe->opened = 0;
154  eoe->rx_skb = NULL;
155  eoe->rx_expected_fragment = 0;
156  INIT_LIST_HEAD(&eoe->tx_queue);
157  eoe->tx_frame = NULL;
158  eoe->tx_queue_active = 0;
160  eoe->tx_queued_frames = 0;
161 
162  ec_lock_init(&eoe->tx_queue_sem);
163  eoe->tx_frame_number = 0xFF;
164  memset(&eoe->stats, 0, sizeof(struct net_device_stats));
165 
166  eoe->rx_counter = 0;
167  eoe->tx_counter = 0;
168  eoe->rx_rate = 0;
169  eoe->tx_rate = 0;
170  eoe->rate_jiffies = 0;
171  eoe->rx_idle = 1;
172  eoe->tx_idle = 1;
173 
174  /* device name eoe<MASTER>[as]<SLAVE>, because networking scripts don't
175  * like hyphens etc. in interface names. */
176  if (slave->effective_alias) {
177  snprintf(name, EC_DATAGRAM_NAME_SIZE,
178  "eoe%ua%u", slave->master->index, slave->effective_alias);
179  } else {
180  snprintf(name, EC_DATAGRAM_NAME_SIZE,
181  "eoe%us%u", slave->master->index, slave->ring_position);
182  }
183 
184  snprintf(eoe->datagram.name, EC_DATAGRAM_NAME_SIZE, name);
185 
186 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
187  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, NET_NAME_UNKNOWN,
188  ether_setup);
189 #else
190  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup);
191 #endif
192  if (!eoe->dev) {
193  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
194  " for EoE handler!\n", name);
195  ret = -ENODEV;
196  goto out_return;
197  }
198 
199  // initialize net_device
200 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
201  eoe->dev->netdev_ops = &ec_eoedev_ops;
202 #else
203  eoe->dev->open = ec_eoedev_open;
204  eoe->dev->stop = ec_eoedev_stop;
205  eoe->dev->hard_start_xmit = ec_eoedev_tx;
206  eoe->dev->get_stats = ec_eoedev_stats;
207 #endif
208 
209  // First check if the MAC address assigned to the master is globally
210  // unique
211  if ((slave->master->devices[EC_DEVICE_MAIN].dev->dev_addr[0] & 0x02) !=
212  0x02) {
213  // The master MAC is unique and the NIC part can be used for the EoE
214  // interface MAC
215  use_master_mac = 1;
216  }
217  else {
218  // The master MAC is not unique, so we check for unique MAC in other
219  // interfaces
220  dev = first_net_device(&init_net);
221  while (dev) {
222  // Check if globally unique MAC address
223  if (dev->addr_len == ETH_ALEN) {
224  if (memcmp(dev->dev_addr, lo_mac, ETH_ALEN) != 0) {
225  if ((dev->dev_addr[0] & 0x02) != 0x02) {
226  // The first globally unique MAC address has been
227  // identified
228  break;
229  }
230  }
231  }
232  dev = next_net_device(dev);
233  }
234  if (eoe->dev->addr_len == ETH_ALEN) {
235  if (dev) {
236  // A unique MAC were identified in one of the other network
237  // interfaces and the NIC part can be used for the EoE
238  // interface MAC.
239  EC_SLAVE_INFO(slave, "%s MAC address derived from"
240  " NIC part of %s MAC address\n",
241  eoe->dev->name, dev->name);
242  mac_addr[1] = dev->dev_addr[3];
243  mac_addr[2] = dev->dev_addr[4];
244  mac_addr[3] = dev->dev_addr[5];
245  }
246  else {
247  use_master_mac = 1;
248  }
249  }
250  }
251  if (eoe->dev->addr_len == ETH_ALEN) {
252  if (use_master_mac) {
253  EC_SLAVE_INFO(slave, "%s MAC address derived"
254  " from NIC part of %s MAC address\n",
255  eoe->dev->name,
256  slave->master->devices[EC_DEVICE_MAIN].dev->name);
257  mac_addr[1] =
258  slave->master->devices[EC_DEVICE_MAIN].dev->dev_addr[3];
259  mac_addr[2] =
260  slave->master->devices[EC_DEVICE_MAIN].dev->dev_addr[4];
261  mac_addr[3] =
262  slave->master->devices[EC_DEVICE_MAIN].dev->dev_addr[5];
263  }
264  //eoe->dev->dev_addr[0] = 0x02;
265  mac_addr[0] = 0x02;
266  mac_addr[4] = (uint8_t)(slave->ring_position >> 8);
267  mac_addr[5] = (uint8_t)(slave->ring_position);
268 
269 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
270  eth_hw_addr_set(eoe->dev, mac_addr);
271 #else
272  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
273 #endif
274  }
275 
276  // initialize private data
277  priv = netdev_priv(eoe->dev);
278  *priv = eoe;
279 
280  // Usually setting the MTU appropriately makes the upper layers
281  // do the frame fragmenting. In some cases this doesn't work
282  // so the MTU is left on the Ethernet standard value and fragmenting
283  // is done "manually".
284 #if 0
285  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
286 #endif
287 
288  // connect the net_device to the kernel
289  ret = register_netdev(eoe->dev);
290  if (ret) {
291  EC_SLAVE_ERR(slave, "Unable to register net_device:"
292  " error %i\n", ret);
293  goto out_free;
294  }
295 
296  return 0;
297 
298  out_free:
299  free_netdev(eoe->dev);
300  eoe->dev = NULL;
301  out_return:
302  return ret;
303 }
304 
305 /*****************************************************************************/
306 
312 {
313  unregister_netdev(eoe->dev); // possibly calls close callback
314 
315  // empty transmit queue
316  ec_eoe_flush(eoe);
317 
318  if (eoe->tx_frame) {
319  dev_kfree_skb(eoe->tx_frame->skb);
320  kfree(eoe->tx_frame);
321  }
322 
323  if (eoe->rx_skb)
324  dev_kfree_skb(eoe->rx_skb);
325 
326  free_netdev(eoe->dev);
327 
329 }
330 
331 /*****************************************************************************/
332 
336 {
337  ec_eoe_frame_t *frame, *next;
338 
339  ec_lock_down(&eoe->tx_queue_sem);
340 
341  list_for_each_entry_safe(frame, next, &eoe->tx_queue, queue) {
342  list_del(&frame->queue);
343  dev_kfree_skb(frame->skb);
344  kfree(frame);
345  }
346  eoe->tx_queued_frames = 0;
347 
348  ec_lock_up(&eoe->tx_queue_sem);
349 }
350 
351 /*****************************************************************************/
352 
358 {
359  size_t remaining_size, current_size, complete_offset;
360  unsigned int last_fragment;
361  uint8_t *data;
362 #if EOE_DEBUG_LEVEL >= 3
363  unsigned int i;
364 #endif
365 
366  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
367 
368  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
369  current_size = remaining_size;
370  last_fragment = 1;
371  } else {
372  current_size =
373  ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
374  last_fragment = 0;
375  }
376 
377  if (eoe->tx_fragment_number) {
378  complete_offset = eoe->tx_offset / 32;
379  }
380  else {
381  // complete size in 32 bit blocks, rounded up.
382  complete_offset = remaining_size / 32 + 1;
383  }
384 
385 #if EOE_DEBUG_LEVEL >= 2
386  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX sending fragment %u%s"
387  " with %zu octets (%zu). %u frames queued.\n",
388  eoe->dev->name, eoe->tx_fragment_number,
389  last_fragment ? "" : "+", current_size, complete_offset,
390  eoe->tx_queued_frames);
391 #endif
392 
393 #if EOE_DEBUG_LEVEL >= 3
394  EC_SLAVE_DBG(eoe->slave, 0, "");
395  for (i = 0; i < current_size; i++) {
396  printk(KERN_CONT "%02X ",
397  eoe->tx_frame->skb->data[eoe->tx_offset + i]);
398  if ((i + 1) % 16 == 0) {
399  printk(KERN_CONT "\n");
400  EC_SLAVE_DBG(eoe->slave, 0, "");
401  }
402  }
403  printk(KERN_CONT "\n");
404 #endif
405 
406  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
407  EC_MBOX_TYPE_EOE, current_size + 4);
408  if (IS_ERR(data)) {
409  return PTR_ERR(data);
410  }
411 
412  EC_WRITE_U8 (data, EC_EOE_TYPE_FRAME_FRAG); // Initiate EoE Tx Request
413  EC_WRITE_U8 (data + 1, last_fragment);
414  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
415  (complete_offset & 0x3F) << 6 |
416  (eoe->tx_frame_number & 0x0F) << 12));
417 
418  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
419  eoe->queue_datagram = 1;
420 
421  eoe->tx_offset += current_size;
422  eoe->tx_fragment_number++;
423  return 0;
424 }
425 
426 /*****************************************************************************/
427 
430 void ec_eoe_run(ec_eoe_t *eoe )
431 {
432  if (!eoe->opened) {
433  return;
434  }
435 
436  // if the datagram was not sent, or is not yet received, skip this cycle
437  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT) {
438  return;
439  }
440 
441  // call state function
442  eoe->state(eoe);
443 
444  // update statistics
445  if (jiffies - eoe->rate_jiffies > HZ) {
446  eoe->rx_rate = eoe->rx_counter;
447  eoe->tx_rate = eoe->tx_counter;
448  eoe->rx_counter = 0;
449  eoe->tx_counter = 0;
450  eoe->rate_jiffies = jiffies;
451  }
452 
454 }
455 
456 /*****************************************************************************/
457 
461 {
462  if (eoe->queue_datagram) {
464  eoe->queue_datagram = 0;
465  }
466 }
467 
468 /*****************************************************************************/
469 
474 int ec_eoe_is_open(const ec_eoe_t *eoe )
475 {
476  return eoe->opened;
477 }
478 
479 /*****************************************************************************/
480 
486 int ec_eoe_is_idle(const ec_eoe_t *eoe )
487 {
488  return eoe->rx_idle && eoe->tx_idle;
489 }
490 
491 /******************************************************************************
492  * STATE PROCESSING FUNCTIONS
493  *****************************************************************************/
494 
503 {
504  if (eoe->slave->error_flag ||
506  eoe->rx_idle = 1;
507  eoe->tx_idle = 1;
508  return;
509  }
510 
511  // mailbox read check is skipped if a read request is already ongoing
512  if (ec_read_mbox_locked(eoe->slave)) {
514  } else {
516  eoe->queue_datagram = 1;
518  }
519 }
520 
521 /*****************************************************************************/
522 
529 {
530  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
531  eoe->stats.rx_errors++;
532 #if EOE_DEBUG_LEVEL >= 1
533  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
534  " check datagram for %s.\n", eoe->dev->name);
535 #endif
538  return;
539  }
540 
541  if (!ec_slave_mbox_check(&eoe->datagram)) {
542  eoe->rx_idle = 1;
544  // check that data is not already received by another read request
545  if (eoe->slave->mbox_eoe_frag_data.payload_size > 0) {
547  eoe->state(eoe);
548  } else {
550  }
551  return;
552  }
553 
554  eoe->rx_idle = 0;
556  eoe->queue_datagram = 1;
558 }
559 
560 /*****************************************************************************/
561 
568 {
569 
570  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
571  eoe->stats.rx_errors++;
572 #if EOE_DEBUG_LEVEL >= 1
573  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
574  " fetch datagram for %s.\n", eoe->dev->name);
575 #endif
578  return;
579  }
582  eoe->state(eoe);
583 }
584 
585 
586 
587 /*****************************************************************************/
588 
594 {
595  size_t rec_size, data_size;
596  uint8_t *data, eoe_type, last_fragment, time_appended, mbox_prot;
597  uint8_t fragment_offset, fragment_number;
598 #if EOE_DEBUG_LEVEL >= 2
599  uint8_t frame_number;
600 #endif
601  off_t offset;
602 #if EOE_DEBUG_LEVEL >= 3
603  unsigned int i;
604 #endif
605 
606  if (eoe->slave->mbox_eoe_frag_data.payload_size > 0) {
608  } else {
609  // initiate a new mailbox read check if required data is not available
610  if (!ec_read_mbox_locked(eoe->slave)) {
612  eoe->queue_datagram = 1;
614  }
615  return;
616  }
617 
619  &mbox_prot, &rec_size);
620  if (IS_ERR(data)) {
621  eoe->stats.rx_errors++;
622 #if EOE_DEBUG_LEVEL >= 1
623  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
624  eoe->dev->name);
625 #endif
627  return;
628  }
629 
630  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
631  eoe->stats.rx_errors++;
632 #if EOE_DEBUG_LEVEL >= 1
633  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
634  eoe->dev->name);
635 #endif
637  return;
638  }
639 
640  eoe_type = EC_READ_U8(data) & 0x0F;
641 
642  if (eoe_type != EC_EOE_TYPE_FRAME_FRAG) {
643  EC_SLAVE_ERR(eoe->slave, "%s: EoE iface handler received other EoE type"
644  " response (type %x). Dropping.\n", eoe->dev->name, eoe_type);
645  eoe->stats.rx_dropped++;
647  return;
648  }
649 
650  // EoE Fragment Request received
651 
652  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
653  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
654  fragment_number = EC_READ_U16(data + 2) & 0x003F;
655  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
656 #if EOE_DEBUG_LEVEL >= 2
657  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
658 #endif
659 
660 #if EOE_DEBUG_LEVEL >= 2
661  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
662  " frame %u%s, %zu octets\n", eoe->dev->name, fragment_number,
663  last_fragment ? "" : "+", fragment_offset, frame_number,
664  time_appended ? ", + timestamp" : "",
665  time_appended ? rec_size - 8 : rec_size - 4);
666 #endif
667 
668 #if EOE_DEBUG_LEVEL >= 3
669  EC_SLAVE_DBG(eoe->slave, 0, "");
670  for (i = 0; i < rec_size - 4; i++) {
671  printk(KERN_CONT "%02X ", data[i + 4]);
672  if ((i + 1) % 16 == 0) {
673  printk(KERN_CONT "\n");
674  EC_SLAVE_DBG(eoe->slave, 0, "");
675  }
676  }
677  printk(KERN_CONT "\n");
678 #endif
679 
680  data_size = time_appended ? rec_size - 8 : rec_size - 4;
681 
682  if (!fragment_number) {
683  if (eoe->rx_skb) {
684  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
685  dev_kfree_skb(eoe->rx_skb);
686  }
687 
688  // new socket buffer
689  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
690  if (printk_ratelimit())
691  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
692  " frame dropped.\n");
693  eoe->stats.rx_dropped++;
695  return;
696  }
697 
698  eoe->rx_skb_offset = 0;
699  eoe->rx_skb_size = fragment_offset * 32;
700  eoe->rx_expected_fragment = 0;
701  }
702  else {
703  if (!eoe->rx_skb) {
704  eoe->stats.rx_dropped++;
706  return;
707  }
708 
709  offset = fragment_offset * 32;
710  if (offset != eoe->rx_skb_offset ||
711  offset + data_size > eoe->rx_skb_size ||
712  fragment_number != eoe->rx_expected_fragment) {
713  dev_kfree_skb(eoe->rx_skb);
714  eoe->rx_skb = NULL;
715  eoe->stats.rx_errors++;
716 #if EOE_DEBUG_LEVEL >= 1
717  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
718  eoe->dev->name);
719 #endif
721  return;
722  }
723  }
724 
725  // copy fragment into socket buffer
726  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
727  eoe->rx_skb_offset += data_size;
728 
729  if (last_fragment) {
730  // update statistics
731  eoe->stats.rx_packets++;
732  eoe->stats.rx_bytes += eoe->rx_skb->len;
733  eoe->rx_counter += eoe->rx_skb->len;
734 
735 #if EOE_DEBUG_LEVEL >= 2
736  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
737  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
738 #endif
739 
740  // pass socket buffer to network stack
741  eoe->rx_skb->dev = eoe->dev;
742  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
743  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
744 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)
745  if (netif_rx(eoe->rx_skb)) {
746 #else
747  if (netif_rx_ni(eoe->rx_skb)) {
748 #endif
749  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
750  }
751  eoe->rx_skb = NULL;
752 
754  }
755  else {
756  eoe->rx_expected_fragment++;
757 #if EOE_DEBUG_LEVEL >= 2
758  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
759  eoe->dev->name, eoe->rx_expected_fragment);
760 #endif
762  }
763 }
764 
765 /*****************************************************************************/
766 
775 {
776 #if EOE_DEBUG_LEVEL >= 2
777  unsigned int wakeup = 0;
778 #endif
779 
780  if (eoe->slave->error_flag ||
782  eoe->rx_idle = 1;
783  eoe->tx_idle = 1;
784  return;
785  }
786 
787  ec_lock_down(&eoe->tx_queue_sem);
788 
789  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
790  ec_lock_up(&eoe->tx_queue_sem);
791  eoe->tx_idle = 1;
792  // no data available.
793  // start a new receive immediately.
795  return;
796  }
797 
798  // take the first frame out of the queue
799  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
800  list_del(&eoe->tx_frame->queue);
801  if (!eoe->tx_queue_active &&
802  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
803  netif_wake_queue(eoe->dev);
804  eoe->tx_queue_active = 1;
805 #if EOE_DEBUG_LEVEL >= 2
806  wakeup = 1;
807 #endif
808  }
809 
810  eoe->tx_queued_frames--;
811  ec_lock_up(&eoe->tx_queue_sem);
812 
813  eoe->tx_idle = 0;
814 
815  eoe->tx_frame_number++;
816  eoe->tx_frame_number %= 16;
817  eoe->tx_fragment_number = 0;
818  eoe->tx_offset = 0;
819 
820  if (ec_eoe_send(eoe)) {
821  dev_kfree_skb(eoe->tx_frame->skb);
822  kfree(eoe->tx_frame);
823  eoe->tx_frame = NULL;
824  eoe->stats.tx_errors++;
826 #if EOE_DEBUG_LEVEL >= 1
827  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
828 #endif
829  return;
830  }
831 
832 #if EOE_DEBUG_LEVEL >= 2
833  if (wakeup)
834  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
835  eoe->dev->name);
836 #endif
837 
838  eoe->tries = EC_EOE_TRIES;
840 }
841 
842 /*****************************************************************************/
843 
850 {
851  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
852  if (eoe->tries) {
853  eoe->tries--; // try again
854  eoe->queue_datagram = 1;
855  } else {
856  eoe->stats.tx_errors++;
857 #if EOE_DEBUG_LEVEL >= 1
858  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
859  " datagram for %s after %u tries.\n",
860  eoe->dev->name, EC_EOE_TRIES);
861 #endif
863  }
864  return;
865  }
866 
867  if (eoe->datagram.working_counter != 1) {
868  if (eoe->tries) {
869  eoe->tries--; // try again
870  eoe->queue_datagram = 1;
871  } else {
872  eoe->stats.tx_errors++;
873 #if EOE_DEBUG_LEVEL >= 1
874  EC_SLAVE_WARN(eoe->slave, "No sending response"
875  " for %s after %u tries.\n",
876  eoe->dev->name, EC_EOE_TRIES);
877 #endif
879  }
880  return;
881  }
882 
883  // frame completely sent
884  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
885  eoe->stats.tx_packets++;
886  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
887  eoe->tx_counter += eoe->tx_frame->skb->len;
888  dev_kfree_skb(eoe->tx_frame->skb);
889  kfree(eoe->tx_frame);
890  eoe->tx_frame = NULL;
892  }
893  else { // send next fragment
894  if (ec_eoe_send(eoe)) {
895  dev_kfree_skb(eoe->tx_frame->skb);
896  kfree(eoe->tx_frame);
897  eoe->tx_frame = NULL;
898  eoe->stats.tx_errors++;
899 #if EOE_DEBUG_LEVEL >= 1
900  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
901 #endif
903  }
904  }
905 }
906 
907 /******************************************************************************
908  * NET_DEVICE functions
909  *****************************************************************************/
910 
915 int ec_eoedev_open(struct net_device *dev )
916 {
917  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
918  ec_eoe_flush(eoe);
919  eoe->opened = 1;
920  eoe->rx_idle = 0;
921  eoe->tx_idle = 0;
922  netif_start_queue(dev);
923  eoe->tx_queue_active = 1;
924 #if EOE_DEBUG_LEVEL >= 2
925  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
926 #endif
927  return 0;
928 }
929 
930 /*****************************************************************************/
931 
936 int ec_eoedev_stop(struct net_device *dev )
937 {
938  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
939  netif_stop_queue(dev);
940  eoe->rx_idle = 1;
941  eoe->tx_idle = 1;
942  eoe->tx_queue_active = 0;
943  eoe->opened = 0;
944  ec_eoe_flush(eoe);
945 #if EOE_DEBUG_LEVEL >= 2
946  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
947 #endif
948  return 0;
949 }
950 
951 /*****************************************************************************/
952 
957 int ec_eoedev_tx(struct sk_buff *skb,
958  struct net_device *dev
959  )
960 {
961  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
962  ec_eoe_frame_t *frame;
963 
964 #if 0
965  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
966  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
967  " exceeds MTU. dropping.\n", skb->len);
968  dev_kfree_skb(skb);
969  eoe->stats.tx_dropped++;
970  return 0;
971  }
972 #endif
973 
974  if (!(frame =
975  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
976  if (printk_ratelimit())
977  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
978  return 1;
979  }
980 
981  frame->skb = skb;
982 
983  ec_lock_down(&eoe->tx_queue_sem);
984  list_add_tail(&frame->queue, &eoe->tx_queue);
985  eoe->tx_queued_frames++;
986  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
987  netif_stop_queue(dev);
988  eoe->tx_queue_active = 0;
989  }
990  ec_lock_up(&eoe->tx_queue_sem);
991 
992 #if EOE_DEBUG_LEVEL >= 2
993  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
994  " with %u octets (%u frames queued).\n",
995  eoe->dev->name, skb->len, eoe->tx_queued_frames);
996  if (!eoe->tx_queue_active) {
997  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
998  }
999 #endif
1000 
1001  return 0;
1002 }
1003 
1004 /*****************************************************************************/
1005 
1010 struct net_device_stats *ec_eoedev_stats(
1011  struct net_device *dev
1012  )
1013 {
1014  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
1015  return &eoe->stats;
1016 }
1017 
1018 /*****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:460
ec_mbox_data_t mbox_eoe_frag_data
Received mailbox data for EoE, type frame fragment.
Definition: slave.h:265
uint8_t * ec_slave_mbox_prepare_send(const ec_slave_t *slave, ec_datagram_t *datagram, uint8_t type, size_t size)
Prepares a mailbox-send datagram.
Definition: mailbox.c:51
uint16_t ring_position
Ring position.
Definition: slave.h:206
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
Definition: globals.h:110
Queued frame structure.
Definition: ethernet.h:67
uint32_t tx_counter
octets transmitted during last second
Definition: ethernet.h:113
#define EC_EOE_TX_QUEUE_SIZE
Size of the EoE tx queue.
Definition: ethernet.c:60
static const struct net_device_ops ec_eoedev_ops
Device operations for EoE interfaces.
Definition: ethernet.c:90
struct sk_buff * skb
socket buffer
Definition: ethernet.h:70
uint16_t configured_tx_mailbox_size
Configured send mailbox size.
Definition: slave.h:224
void ec_eoe_state_rx_fetch(ec_eoe_t *)
State: RX_FETCH.
Definition: ethernet.c:567
struct list_head tx_queue
queue for frames to send
Definition: ethernet.h:104
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:107
uint8_t tx_fragment_number
number of the fragment
Definition: ethernet.h:111
int ec_eoe_send(ec_eoe_t *eoe)
Sends a frame or the next fragment.
Definition: ethernet.c:357
ec_slave_t * slave
pointer to the corresponding slave
Definition: ethernet.h:87
void ec_master_queue_datagram_ext(ec_master_t *master, ec_datagram_t *datagram)
Places a datagram in the non-application datagram queue.
Definition: master.c:982
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
Definition: ethernet.c:1010
unsigned int tx_queue_size
Transmit queue size.
Definition: ethernet.h:105
size_t rx_skb_size
size of the allocated socket buffer memory
Definition: ethernet.h:98
size_t tx_offset
number of octets sent
Definition: ethernet.h:112
EtherCAT slave structure.
int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, ec_datagram_t *datagram)
Prepares a datagram to fetch mailbox data.
Definition: mailbox.c:128
void ec_eoe_state_tx_start(ec_eoe_t *)
State: TX START.
Definition: ethernet.c:774
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
Definition: slave.h:91
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition: ecrt.h:2338
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
Definition: ethernet.c:528
unsigned int tx_queue_active
kernel netif queue started
Definition: ethernet.h:106
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
Definition: datagram.h:114
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:957
uint8_t * ec_slave_mbox_fetch(const ec_slave_t *slave, ec_mbox_data_t *response_data, uint8_t *type, size_t *size)
Processes received mailbox data.
Definition: mailbox.c:166
uint16_t working_counter
Working counter.
Definition: datagram.h:100
int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
EoE constructor.
Definition: ethernet.c:134
uint8_t link_state
device link state
Definition: device.h:88
#define EC_EOE_TRIES
Number of tries.
Definition: ethernet.c:64
Sent (still in the queue).
Definition: datagram.h:77
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
Definition: ethernet.c:335
uint32_t tx_rate
transmit rate (bps)
Definition: ethernet.h:114
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
Definition: datagram.c:626
Global definitions and macros.
uint8_t rx_expected_fragment
next expected fragment number
Definition: ethernet.h:99
EtherCAT master structure.
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
Definition: ethernet.c:502
EtherCAT slave.
Definition: slave.h:199
Ethernet over EtherCAT (EoE)
ec_datagram_state_t state
State.
Definition: datagram.h:101
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
Definition: ethernet.c:486
ec_lock_t tx_queue_sem
Semaphore for the send queue.
Definition: ethernet.h:108
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
Definition: slave.h:77
unsigned long rate_jiffies
time of last rate output
Definition: ethernet.h:94
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:2355
Main device.
Definition: globals.h:205
void ec_eoe_state_rx_fetch_data(ec_eoe_t *)
State: RX_FETCH DATA.
Definition: ethernet.c:593
ec_master_t * master
Master owning the slave.
Definition: slave.h:201
struct list_head queue
list item
Definition: ethernet.h:69
int ec_read_mbox_locked(ec_slave_t *slave)
Return the current mailbox lock status and lock it if not locked.
Definition: slave.c:203
void ec_eoe_state_tx_sent(ec_eoe_t *)
State: TX SENT.
Definition: ethernet.c:849
unsigned int opened
net_device is opened
Definition: ethernet.h:93
static int ec_eoedev_set_mac(struct net_device *netdev, void *p)
ec_eoedev_set_mac - Change the Ethernet Address of the NIC : network interface device structure : poi...
Definition: ethernet.c:109
#define EC_SLAVE_INFO(slave, fmt, args...)
Convenience macro for printing slave-specific information to syslog.
Definition: slave.h:63
off_t rx_skb_offset
current write pointer in the socket buffer
Definition: ethernet.h:97
ec_datagram_t datagram
datagram
Definition: ethernet.h:88
void ec_read_mbox_lock_clear(ec_slave_t *slave)
Clears the mailbox lock.
Definition: slave.c:190
struct net_device_stats stats
device statistics
Definition: ethernet.h:92
uint16_t effective_alias
Effective alias address.
Definition: slave.h:208
int ec_slave_mbox_prepare_check(const ec_slave_t *slave, ec_datagram_t *datagram)
Prepares a datagram for checking the mailbox state.
Definition: mailbox.c:97
unsigned int tx_idle
Idle flag.
Definition: ethernet.h:115
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
Definition: ecrt.h:2250
uint32_t rx_rate
receive rate (bps)
Definition: ethernet.h:101
Mailbox functionality.
struct sk_buff * rx_skb
current rx socket buffer
Definition: ethernet.h:96
void(* state)(ec_eoe_t *)
state function for the state machine
Definition: ethernet.h:90
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
Definition: datagram.c:88
uint8_t tx_frame_number
number of the transmitted frame
Definition: ethernet.h:110
uint32_t rx_counter
octets received during last second
Definition: ethernet.h:100
uint16_t configured_rx_mailbox_size
Configured receive mailbox size.
Definition: slave.h:220
int ec_eoedev_open(struct net_device *)
Opens the virtual network device.
Definition: ethernet.c:915
ec_eoe_frame_t * tx_frame
current TX frame
Definition: ethernet.h:109
unsigned int tries
Tries.
Definition: ethernet.h:117
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
Definition: ethernet.c:430
#define EC_READ_U8(DATA)
Read an 8-bit unsigned value from EtherCAT data.
Definition: ecrt.h:2234
struct net_device * dev
pointer to the assigned net_device
Definition: device.h:84
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:936
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:311
unsigned int index
Index.
Definition: master.h:190
Received (dequeued).
Definition: datagram.h:78
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:84
unsigned int error_flag
Stop processing after an error.
Definition: slave.h:216
unsigned int rx_idle
Idle flag.
Definition: ethernet.h:102
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition: master.h:206
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:474
size_t payload_size
Size of the mailbox response payload data.
Definition: datagram.h:125
int ec_slave_mbox_check(const ec_datagram_t *datagram)
Processes a mailbox state checking datagram.
Definition: mailbox.c:116
struct net_device * dev
net_device for virtual ethernet device
Definition: ethernet.h:91
unsigned int queue_datagram
the datagram is ready for queuing
Definition: ethernet.h:89
unsigned int tx_queued_frames
number of frames in the queue
Definition: ethernet.h:107
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
Definition: datagram.c:119