IgH EtherCAT Master  1.6.10
ethernet.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2006-2024 Florian Pose, Ingenieurgemeinschaft IgH
4  *
5  * This file is part of the IgH EtherCAT Master.
6  *
7  * The IgH EtherCAT Master is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version 2, as
9  * published by the Free Software Foundation.
10  *
11  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with the IgH EtherCAT Master; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  ****************************************************************************/
21 
27 /****************************************************************************/
28 
29 #include <linux/version.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/lockdep.h>
33 #include <linux/skbuff.h>
34 
35 #include "globals.h"
36 #include "master.h"
37 #include "slave.h"
38 #include "mailbox.h"
39 #include "ethernet.h"
40 
41 #if defined(CONFIG_SUSE_KERNEL) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
42 #include <linux/suse_version.h>
43 #else
44 # ifndef SUSE_VERSION
45 # define SUSE_VERSION 0
46 # endif
47 # ifndef SUSE_PATCHLEVEL
48 # define SUSE_PATCHLEVEL 0
49 # endif
50 #endif
51 
52 /****************************************************************************/
53 
61 #define EOE_DEBUG_LEVEL 1
62 
65 #define EC_EOE_TX_QUEUE_SIZE 100
66 
69 #define EC_EOE_TRIES 100
70 
71 /****************************************************************************/
72 
73 // prototypes for private methods
74 void ec_eoe_flush(ec_eoe_t *);
75 int ec_eoe_send(ec_eoe_t *);
76 
77 /****************************************************************************/
78 
79 // state functions
85 
86 // net_device functions
87 int ec_eoedev_open(struct net_device *);
88 int ec_eoedev_stop(struct net_device *);
89 int ec_eoedev_tx(struct sk_buff *, struct net_device *);
90 struct net_device_stats *ec_eoedev_stats(struct net_device *);
91 
92 /****************************************************************************/
93 
96 static const struct net_device_ops ec_eoedev_ops = {
97  .ndo_open = ec_eoedev_open,
98  .ndo_stop = ec_eoedev_stop,
99  .ndo_start_xmit = ec_eoedev_tx,
100  .ndo_get_stats = ec_eoedev_stats,
101 };
102 
103 /****************************************************************************/
104 
112  ec_eoe_t *eoe,
113  ec_slave_t *slave
114  )
115 {
116  ec_eoe_t **priv;
117  int ret = 0;
118  char name[EC_DATAGRAM_NAME_SIZE];
119  u8 mac_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
120 
121  eoe->slave = slave;
122 
123  ec_datagram_init(&eoe->datagram);
124  eoe->queue_datagram = 0;
126  eoe->opened = 0;
127  eoe->rx_skb = NULL;
128  eoe->rx_expected_fragment = 0;
129  INIT_LIST_HEAD(&eoe->tx_queue);
130  eoe->tx_frame = NULL;
131  eoe->tx_queue_active = 0;
133  eoe->tx_queued_frames = 0;
134 
135  eoe->tx_frame_number = 0xFF;
136  memset(&eoe->stats, 0, sizeof(struct net_device_stats));
137 
138  eoe->rx_counter = 0;
139  eoe->tx_counter = 0;
140  eoe->rx_rate = 0;
141  eoe->tx_rate = 0;
142  eoe->rate_jiffies = 0;
143  eoe->rx_idle = 1;
144  eoe->tx_idle = 1;
145 
146  /* device name eoe<MASTER>[as]<SLAVE>, because networking scripts don't
147  * like hyphens etc. in interface names. */
148  if (slave->effective_alias) {
149  snprintf(name, EC_DATAGRAM_NAME_SIZE,
150  "eoe%ua%u", slave->master->index, slave->effective_alias);
151  } else {
152  snprintf(name, EC_DATAGRAM_NAME_SIZE,
153  "eoe%us%u", slave->master->index, slave->ring_position);
154  }
155 
156  snprintf(eoe->datagram.name, EC_DATAGRAM_NAME_SIZE, name);
157 
158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
159  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, NET_NAME_UNKNOWN,
160  ether_setup);
161 #else
162  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup);
163 #endif
164  if (!eoe->dev) {
165  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
166  " for EoE handler!\n", name);
167  ret = -ENODEV;
168  goto out_return;
169  }
170 
171  // initialize net_device
172  eoe->dev->netdev_ops = &ec_eoedev_ops;
173 
174 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
175  eth_hw_addr_set(eoe->dev, mac_addr);
176 #else
177  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
178 #endif
179 
180  // initialize private data
181  priv = netdev_priv(eoe->dev);
182  *priv = eoe;
183 
184  // Usually setting the MTU appropriately makes the upper layers
185  // do the frame fragmenting. In some cases this doesn't work
186  // so the MTU is left on the Ethernet standard value and fragmenting
187  // is done "manually".
188 #if 0
189  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
190 #endif
191 
192  // connect the net_device to the kernel
193  ret = register_netdev(eoe->dev);
194  if (ret) {
195  EC_SLAVE_ERR(slave, "Unable to register net_device:"
196  " error %i\n", ret);
197  goto out_free;
198  }
199 
200  // make the last address octet unique
201  mac_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
202 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
203  eth_hw_addr_set(eoe->dev, mac_addr);
204 #else
205  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
206 #endif
207 
208  return 0;
209 
210  out_free:
211  free_netdev(eoe->dev);
212  eoe->dev = NULL;
213  out_return:
214  return ret;
215 }
216 
217 /****************************************************************************/
218 
224 {
225  unregister_netdev(eoe->dev); // possibly calls close callback
226 
227  // empty transmit queue
228  ec_eoe_flush(eoe);
229 
230  if (eoe->tx_frame) {
231  dev_kfree_skb(eoe->tx_frame->skb);
232  kfree(eoe->tx_frame);
233  }
234 
235  if (eoe->rx_skb)
236  dev_kfree_skb(eoe->rx_skb);
237 
238  free_netdev(eoe->dev);
239 
241 }
242 
243 /****************************************************************************/
244 
248 {
249  ec_eoe_frame_t *frame, *next;
250  struct list_head tx_queue;
251 
252  netif_tx_lock_bh(eoe->dev);
253 
254  list_replace_init(&eoe->tx_queue, &tx_queue);
255  eoe->tx_queued_frames = 0;
256 
257  netif_tx_unlock_bh(eoe->dev);
258 
259  list_for_each_entry_safe(frame, next, &tx_queue, queue) {
260  list_del(&frame->queue);
261  dev_kfree_skb(frame->skb);
262  kfree(frame);
263  }
264 }
265 
266 /****************************************************************************/
267 
273 {
274  size_t remaining_size, current_size, complete_offset;
275  unsigned int last_fragment;
276  uint8_t *data;
277 #if EOE_DEBUG_LEVEL >= 3
278  unsigned int i;
279 #endif
280 
281  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
282 
283  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
284  current_size = remaining_size;
285  last_fragment = 1;
286  } else {
287  current_size = ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
288  last_fragment = 0;
289  }
290 
291  if (eoe->tx_fragment_number) {
292  complete_offset = eoe->tx_offset / 32;
293  }
294  else {
295  // complete size in 32 bit blocks, rounded up.
296  complete_offset = remaining_size / 32 + 1;
297  }
298 
299 #if EOE_DEBUG_LEVEL >= 2
300  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX sending fragment %u%s"
301  " with %zu octets (%zu). %u frames queued.\n",
302  eoe->dev->name, eoe->tx_fragment_number,
303  last_fragment ? "" : "+", current_size, complete_offset,
304  eoe->tx_queued_frames);
305 #endif
306 
307 #if EOE_DEBUG_LEVEL >= 3
308  EC_SLAVE_DBG(eoe->slave, 0, "");
309  for (i = 0; i < current_size; i++) {
310  printk(KERN_CONT "%02X ",
311  eoe->tx_frame->skb->data[eoe->tx_offset + i]);
312  if ((i + 1) % 16 == 0) {
313  printk(KERN_CONT "\n");
314  EC_SLAVE_DBG(eoe->slave, 0, "");
315  }
316  }
317  printk(KERN_CONT "\n");
318 #endif
319 
320  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
321  EC_MBOX_TYPE_EOE, current_size + 4);
322  if (IS_ERR(data))
323  return PTR_ERR(data);
324 
325  EC_WRITE_U8 (data, EC_EOE_FRAMETYPE_INIT_REQ); // Initiate EoE Request
326  EC_WRITE_U8 (data + 1, last_fragment);
327  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
328  (complete_offset & 0x3F) << 6 |
329  (eoe->tx_frame_number & 0x0F) << 12));
330 
331  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
332  eoe->queue_datagram = 1;
333 
334  eoe->tx_offset += current_size;
335  eoe->tx_fragment_number++;
336  return 0;
337 }
338 
339 /****************************************************************************/
340 
343 void ec_eoe_run(ec_eoe_t *eoe )
344 {
345  if (!eoe->opened)
346  return;
347 
348  // if the datagram was not sent, or is not yet received, skip this cycle
349  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
350  return;
351 
352  // call state function
353  eoe->state(eoe);
354 
355  // update statistics
356  if (jiffies - eoe->rate_jiffies > HZ) {
357  eoe->rx_rate = eoe->rx_counter;
358  eoe->tx_rate = eoe->tx_counter;
359  eoe->rx_counter = 0;
360  eoe->tx_counter = 0;
361  eoe->rate_jiffies = jiffies;
362  }
363 
365 }
366 
367 /****************************************************************************/
368 
372 {
373  if (eoe->queue_datagram) {
375  eoe->queue_datagram = 0;
376  }
377 }
378 
379 /****************************************************************************/
380 
385 int ec_eoe_is_open(const ec_eoe_t *eoe )
386 {
387  return eoe->opened;
388 }
389 
390 /****************************************************************************/
391 
397 int ec_eoe_is_idle(const ec_eoe_t *eoe )
398 {
399  return eoe->rx_idle && eoe->tx_idle;
400 }
401 
402 /*****************************************************************************
403  * STATE PROCESSING FUNCTIONS
404  ****************************************************************************/
405 
414 {
415  if (eoe->slave->error_flag ||
417  eoe->rx_idle = 1;
418  eoe->tx_idle = 1;
419  return;
420  }
421 
423  eoe->queue_datagram = 1;
425 }
426 
427 /****************************************************************************/
428 
435 {
436  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
437  eoe->stats.rx_errors++;
438 #if EOE_DEBUG_LEVEL >= 1
439  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
440  " check datagram for %s.\n", eoe->dev->name);
441 #endif
443  return;
444  }
445 
446  if (!ec_slave_mbox_check(&eoe->datagram)) {
447  eoe->rx_idle = 1;
449  return;
450  }
451 
452  eoe->rx_idle = 0;
454  eoe->queue_datagram = 1;
456 }
457 
458 /****************************************************************************/
459 
466 {
467  size_t rec_size, data_size;
468  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
469  uint8_t size_or_offset, fragment_number;
470 #if EOE_DEBUG_LEVEL >= 2
471  uint8_t frame_number;
472 #endif
473 #if EOE_DEBUG_LEVEL >= 3
474  unsigned int i;
475 #endif
476 
477  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
478  eoe->stats.rx_errors++;
479 #if EOE_DEBUG_LEVEL >= 1
480  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
481  " fetch datagram for %s.\n", eoe->dev->name);
482 #endif
484  return;
485  }
486 
487  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
488  &mbox_prot, &rec_size);
489  if (IS_ERR(data)) {
490  eoe->stats.rx_errors++;
491 #if EOE_DEBUG_LEVEL >= 1
492  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
493  eoe->dev->name);
494 #endif
496  return;
497  }
498 
499  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
500  eoe->stats.rx_errors++;
501 #if EOE_DEBUG_LEVEL >= 1
502  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
503  eoe->dev->name);
504 #endif
506  return;
507  }
508 
509  frame_type = EC_READ_U16(data) & 0x000F;
510 
511  if (frame_type != EC_EOE_FRAMETYPE_INIT_REQ) { // EoE Fragment Data
512 #if EOE_DEBUG_LEVEL >= 1
513  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
514  " Dropping.\n", eoe->dev->name);
515 #endif
516  eoe->stats.rx_dropped++;
518  return;
519  }
520 
521  // EoE Fragment Request received
522 
523  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
524  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
525  fragment_number = EC_READ_U16(data + 2) & 0x003F;
526  size_or_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
527 #if EOE_DEBUG_LEVEL >= 2
528  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
529 #endif
530 
531  data_size = time_appended ? rec_size - 8 : rec_size - 4;
532 
533 #if EOE_DEBUG_LEVEL >= 2
534  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
535  " frame %u%s, %zu octets\n", eoe->dev->name, fragment_number,
536  last_fragment ? "" : "+", size_or_offset, frame_number,
537  time_appended ? ", + timestamp" : "", data_size);
538 #endif
539 
540 #if EOE_DEBUG_LEVEL >= 3
541  EC_SLAVE_DBG(eoe->slave, 0, "");
542  for (i = 0; i < rec_size - 4; i++) {
543  printk(KERN_CONT "%02X ", data[i + 4]);
544  if ((i + 1) % 16 == 0) {
545  printk(KERN_CONT "\n");
546  EC_SLAVE_DBG(eoe->slave, 0, "");
547  }
548  }
549  printk(KERN_CONT "\n");
550 #endif
551 
552  if (!fragment_number) {
553  size_t complete_size = size_or_offset * 32;
554 
555  if (eoe->rx_skb) {
556  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
557  dev_kfree_skb(eoe->rx_skb);
558  eoe->rx_skb = NULL;
559  }
560 
561  if (!complete_size || data_size > complete_size) {
562  EC_SLAVE_ERR(eoe->slave, "Received corrupted EoE first fragment"
563  " (%zu of %zu bytes)!\n", data_size, complete_size);
564  eoe->stats.rx_errors++;
566  return;
567  }
568 
569  // new socket buffer
570  if (!(eoe->rx_skb = dev_alloc_skb(complete_size))) {
571  if (printk_ratelimit()) {
572  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
573  " frame dropped.\n");
574  }
575  eoe->stats.rx_dropped++;
577  return;
578  }
579 
580  eoe->rx_skb_offset = 0;
581  eoe->rx_skb_size = complete_size;
582  eoe->rx_expected_fragment = 0;
583  }
584  else {
585  off_t offset = size_or_offset * 32;
586 
587  if (!eoe->rx_skb) {
588  eoe->stats.rx_dropped++;
590  return;
591  }
592 
593  if (offset != eoe->rx_skb_offset ||
594  offset + data_size > eoe->rx_skb_size ||
595  fragment_number != eoe->rx_expected_fragment) {
596  dev_kfree_skb(eoe->rx_skb);
597  eoe->rx_skb = NULL;
598  eoe->stats.rx_errors++;
599 #if EOE_DEBUG_LEVEL >= 1
600  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
601  eoe->dev->name);
602 #endif
604  return;
605  }
606  }
607 
608  // copy fragment into socket buffer
609  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
610  eoe->rx_skb_offset += data_size;
611 
612  if (last_fragment) {
613  // update statistics
614  eoe->stats.rx_packets++;
615  eoe->stats.rx_bytes += eoe->rx_skb->len;
616  eoe->rx_counter += eoe->rx_skb->len;
617 
618 #if EOE_DEBUG_LEVEL >= 2
619  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
620  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
621 #endif
622 
623  // pass socket buffer to network stack
624  eoe->rx_skb->dev = eoe->dev;
625  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
626  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
627  if (netif_rx(eoe->rx_skb)) {
628  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
629  }
630  eoe->rx_skb = NULL;
631 
633  }
634  else {
635  eoe->rx_expected_fragment++;
636 #if EOE_DEBUG_LEVEL >= 2
637  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
638  eoe->dev->name, eoe->rx_expected_fragment);
639 #endif
641  }
642 }
643 
644 /****************************************************************************/
645 
654 {
655 #if EOE_DEBUG_LEVEL >= 2
656  unsigned int wakeup = 0;
657 #endif
658 
659  if (eoe->slave->error_flag ||
661  eoe->rx_idle = 1;
662  eoe->tx_idle = 1;
663  return;
664  }
665 
666  netif_tx_lock_bh(eoe->dev);
667 
668  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
669  netif_tx_unlock_bh(eoe->dev);
670  eoe->tx_idle = 1;
671  // no data available.
672  // start a new receive immediately.
674  return;
675  }
676 
677  // take the first frame out of the queue
678  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
679  list_del(&eoe->tx_frame->queue);
680  if (!eoe->tx_queue_active &&
681  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
682  netif_wake_queue(eoe->dev);
683  eoe->tx_queue_active = 1;
684 #if EOE_DEBUG_LEVEL >= 2
685  wakeup = 1;
686 #endif
687  }
688 
689  eoe->tx_queued_frames--;
690  netif_tx_unlock_bh(eoe->dev);
691 
692  eoe->tx_idle = 0;
693 
694  eoe->tx_frame_number++;
695  eoe->tx_frame_number %= 16;
696  eoe->tx_fragment_number = 0;
697  eoe->tx_offset = 0;
698 
699  if (ec_eoe_send(eoe)) {
700  dev_kfree_skb(eoe->tx_frame->skb);
701  kfree(eoe->tx_frame);
702  eoe->tx_frame = NULL;
703  eoe->stats.tx_errors++;
705 #if EOE_DEBUG_LEVEL >= 1
706  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
707 #endif
708  return;
709  }
710 
711 #if EOE_DEBUG_LEVEL >= 2
712  if (wakeup)
713  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
714  eoe->dev->name);
715 #endif
716 
717  eoe->tries = EC_EOE_TRIES;
719 }
720 
721 /****************************************************************************/
722 
729 {
730  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
731  if (eoe->tries) {
732  eoe->tries--; // try again
733  eoe->queue_datagram = 1;
734  } else {
735  eoe->stats.tx_errors++;
736 #if EOE_DEBUG_LEVEL >= 1
737  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
738  " datagram for %s after %u tries.\n",
739  eoe->dev->name, EC_EOE_TRIES);
740 #endif
742  }
743  return;
744  }
745 
746  if (eoe->datagram.working_counter != 1) {
747  if (eoe->tries) {
748  eoe->tries--; // try again
749  eoe->queue_datagram = 1;
750  } else {
751  eoe->stats.tx_errors++;
752 #if EOE_DEBUG_LEVEL >= 1
753  EC_SLAVE_WARN(eoe->slave, "No sending response"
754  " for %s after %u tries.\n",
755  eoe->dev->name, EC_EOE_TRIES);
756 #endif
758  }
759  return;
760  }
761 
762  // frame completely sent
763  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
764  eoe->stats.tx_packets++;
765  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
766  eoe->tx_counter += eoe->tx_frame->skb->len;
767  dev_kfree_skb(eoe->tx_frame->skb);
768  kfree(eoe->tx_frame);
769  eoe->tx_frame = NULL;
771  }
772  else { // send next fragment
773  if (ec_eoe_send(eoe)) {
774  dev_kfree_skb(eoe->tx_frame->skb);
775  kfree(eoe->tx_frame);
776  eoe->tx_frame = NULL;
777  eoe->stats.tx_errors++;
778 #if EOE_DEBUG_LEVEL >= 1
779  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
780 #endif
782  }
783  }
784 }
785 
786 /*****************************************************************************
787  * NET_DEVICE functions
788  ****************************************************************************/
789 
794 int ec_eoedev_open(struct net_device *dev )
795 {
796  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
797  ec_eoe_flush(eoe);
798  eoe->opened = 1;
799  eoe->rx_idle = 0;
800  eoe->tx_idle = 0;
801  netif_start_queue(dev);
802  eoe->tx_queue_active = 1;
803 #if EOE_DEBUG_LEVEL >= 2
804  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
805 #endif
806  return 0;
807 }
808 
809 /****************************************************************************/
810 
815 int ec_eoedev_stop(struct net_device *dev )
816 {
817  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
818  netif_stop_queue(dev);
819  eoe->rx_idle = 1;
820  eoe->tx_idle = 1;
821  eoe->tx_queue_active = 0;
822  eoe->opened = 0;
823  ec_eoe_flush(eoe);
824 #if EOE_DEBUG_LEVEL >= 2
825  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
826 #endif
827  return 0;
828 }
829 
830 /****************************************************************************/
831 
836 int ec_eoedev_tx(struct sk_buff *skb,
837  struct net_device *dev
838  )
839 {
840  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
841  ec_eoe_frame_t *frame;
842 
843 #if 0
844  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
845  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
846  " exceeds MTU. dropping.\n", skb->len);
847  dev_kfree_skb(skb);
848  eoe->stats.tx_dropped++;
849  return 0;
850  }
851 #endif
852 
853  WARN_ON_ONCE(skb_get_queue_mapping(skb) != 0);
854  lockdep_assert_held(&netdev_get_tx_queue(dev, 0)->_xmit_lock);
855 
856  if (!(frame =
857  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
858  if (printk_ratelimit())
859  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
860  return 1;
861  }
862 
863  frame->skb = skb;
864 
865  list_add_tail(&frame->queue, &eoe->tx_queue);
866  eoe->tx_queued_frames++;
867  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
868  netif_stop_queue(dev);
869  eoe->tx_queue_active = 0;
870  }
871 
872 #if EOE_DEBUG_LEVEL >= 2
873  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
874  " with %u octets (%u frames queued).\n",
875  eoe->dev->name, skb->len, eoe->tx_queued_frames);
876  if (!eoe->tx_queue_active)
877  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
878 #endif
879 
880  return 0;
881 }
882 
883 /****************************************************************************/
884 
889 struct net_device_stats *ec_eoedev_stats(
890  struct net_device *dev
891  )
892 {
893  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
894  return &eoe->stats;
895 }
896 
897 /****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:371
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:836
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:43
uint16_t ring_position
Ring position.
Definition: slave.h:175
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
Definition: globals.h:104
Queued frame structure.
Definition: ethernet.h:57
uint32_t tx_counter
octets transmitted during last second
Definition: ethernet.h:102
#define EC_EOE_TX_QUEUE_SIZE
Size of the EoE tx queue.
Definition: ethernet.c:65
uint8_t * ec_slave_mbox_fetch(const ec_slave_t *slave, const ec_datagram_t *datagram, uint8_t *type, size_t *size)
Processes received mailbox data.
Definition: mailbox.c:157
static const struct net_device_ops ec_eoedev_ops
Device operations for EoE interfaces.
Definition: ethernet.c:96
struct sk_buff * skb
socket buffer
Definition: ethernet.h:60
uint16_t configured_tx_mailbox_size
Configured send mailbox size.
Definition: slave.h:193
void ec_eoe_state_rx_fetch(ec_eoe_t *)
State: RX_FETCH.
Definition: ethernet.c:465
struct list_head tx_queue
queue for frames to send
Definition: ethernet.h:94
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:98
uint8_t tx_fragment_number
number of the fragment
Definition: ethernet.h:100
int ec_eoe_send(ec_eoe_t *)
Sends a frame or the next fragment.
Definition: ethernet.c:272
ec_slave_t * slave
pointer to the corresponding slave
Definition: ethernet.h:77
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:981
unsigned int tx_queue_size
Transmit queue size.
Definition: ethernet.h:95
size_t rx_skb_size
size of the allocated socket buffer memory
Definition: ethernet.h:88
size_t tx_offset
number of octets sent
Definition: ethernet.h:101
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:119
void ec_eoe_state_tx_start(ec_eoe_t *)
State: TX START.
Definition: ethernet.c:653
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
Definition: slave.h:82
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3137
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
Definition: ethernet.c:434
unsigned int tx_queue_active
kernel netif queue started
Definition: ethernet.h:96
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
Definition: datagram.h:106
uint16_t working_counter
Working counter.
Definition: datagram.h:93
int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
EoE constructor.
Definition: ethernet.c:111
uint8_t link_state
device link state
Definition: device.h:80
#define EC_EOE_TRIES
Number of tries.
Definition: ethernet.c:69
Sent (still in the queue).
Definition: datagram.h:69
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
Definition: ethernet.c:247
uint32_t tx_rate
transmit rate (bps)
Definition: ethernet.h:103
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
Definition: datagram.c:622
Global definitions and macros.
uint8_t rx_expected_fragment
next expected fragment number
Definition: ethernet.h:89
EtherCAT master structure.
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
Definition: ethernet.c:413
EtherCAT slave.
Definition: slave.h:168
Ethernet over EtherCAT (EoE)
ec_datagram_state_t state
State.
Definition: datagram.h:94
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
Definition: ethernet.c:397
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
Definition: slave.h:68
unsigned long rate_jiffies
time of last rate output
Definition: ethernet.h:84
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3154
Main device.
Definition: globals.h:198
ec_master_t * master
Master owning the slave.
Definition: slave.h:170
struct list_head queue
list item
Definition: ethernet.h:59
void ec_eoe_state_tx_sent(ec_eoe_t *)
State: TX SENT.
Definition: ethernet.c:728
unsigned int opened
net_device is opened
Definition: ethernet.h:83
off_t rx_skb_offset
current write pointer in the socket buffer
Definition: ethernet.h:87
ec_datagram_t datagram
datagram
Definition: ethernet.h:78
struct net_device_stats stats
device statistics
Definition: ethernet.h:82
uint16_t effective_alias
Effective alias address.
Definition: slave.h:177
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:88
unsigned int tx_idle
Idle flag.
Definition: ethernet.h:104
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
Definition: ecrt.h:3045
uint32_t rx_rate
receive rate (bps)
Definition: ethernet.h:91
Mailbox functionality.
struct sk_buff * rx_skb
current rx socket buffer
Definition: ethernet.h:86
void(* state)(ec_eoe_t *)
state function for the state machine
Definition: ethernet.h:80
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
Definition: datagram.c:80
uint8_t tx_frame_number
number of the transmitted frame
Definition: ethernet.h:99
uint32_t rx_counter
octets received during last second
Definition: ethernet.h:90
uint16_t configured_rx_mailbox_size
Configured receive mailbox size.
Definition: slave.h:189
int ec_eoedev_open(struct net_device *)
Opens the virtual network device.
Definition: ethernet.c:794
ec_eoe_frame_t * tx_frame
current TX frame
Definition: ethernet.h:98
unsigned int tries
Tries.
Definition: ethernet.h:106
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
Definition: ethernet.c:343
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:815
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:223
unsigned int index
Index.
Definition: master.h:188
Received (dequeued).
Definition: datagram.h:70
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:74
unsigned int error_flag
Stop processing after an error.
Definition: slave.h:185
unsigned int rx_idle
Idle flag.
Definition: ethernet.h:92
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition: master.h:200
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
Definition: ethernet.c:889
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:385
int ec_slave_mbox_check(const ec_datagram_t *datagram)
Processes a mailbox state checking datagram.
Definition: mailbox.c:107
struct net_device * dev
net_device for virtual ethernet device
Definition: ethernet.h:81
unsigned int queue_datagram
the datagram is ready for queuing
Definition: ethernet.h:79
unsigned int tx_queued_frames
number of frames in the queue
Definition: ethernet.h:97
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
Definition: datagram.c:110