IgH EtherCAT Master  1.7.0
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) && \
42  LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
43 #include <linux/suse_version.h>
44 #else
45 # ifndef SUSE_VERSION
46 # define SUSE_VERSION 0
47 # endif
48 # ifndef SUSE_PATCHLEVEL
49 # define SUSE_PATCHLEVEL 0
50 # endif
51 #endif
52 
53 /****************************************************************************/
54 
62 #define EOE_DEBUG_LEVEL 1
63 
66 #define EC_EOE_TX_QUEUE_SIZE 100
67 
70 #define EC_EOE_TRIES 100
71 
72 /****************************************************************************/
73 
74 // prototypes for private methods
75 void ec_eoe_flush(ec_eoe_t *);
76 int ec_eoe_send(ec_eoe_t *);
77 
78 /****************************************************************************/
79 
80 // 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  ec_fsm_mbox_init(&eoe->fsm_mbox);
127  eoe->opened = 0;
128  eoe->rx_skb = NULL;
129  eoe->rx_expected_fragment = 0;
130  INIT_LIST_HEAD(&eoe->tx_queue);
131  eoe->tx_frame = NULL;
132  eoe->tx_queue_active = 0;
134  eoe->tx_queued_frames = 0;
135 
136  eoe->tx_frame_number = 0xFF;
137  memset(&eoe->stats, 0, sizeof(struct net_device_stats));
138 
139  eoe->rx_counter = 0;
140  eoe->tx_counter = 0;
141  eoe->rx_rate = 0;
142  eoe->tx_rate = 0;
143  eoe->rate_jiffies = 0;
144  eoe->rx_idle = 1;
145  eoe->tx_idle = 1;
146 
147  /* device name eoe<MASTER>[as]<SLAVE>, because networking scripts don't
148  * like hyphens etc. in interface names. */
149  if (slave->effective_alias) {
150  snprintf(name, EC_DATAGRAM_NAME_SIZE,
151  "eoe%ua%u", slave->master->index, slave->effective_alias);
152  } else {
153  snprintf(name, EC_DATAGRAM_NAME_SIZE,
154  "eoe%us%u", slave->master->index, slave->ring_position);
155  }
156 
157  snprintf(eoe->datagram.name, EC_DATAGRAM_NAME_SIZE, name);
158 
159 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
160  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, NET_NAME_UNKNOWN,
161  ether_setup);
162 #else
163  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup);
164 #endif
165  if (!eoe->dev) {
166  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
167  " for EoE handler!\n", name);
168  ret = -ENODEV;
169  goto out_return;
170  }
171 
172  // initialize net_device
173  eoe->dev->netdev_ops = &ec_eoedev_ops;
174 
175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || \
176  (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
177  eth_hw_addr_set(eoe->dev, mac_addr);
178 #else
179  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
180 #endif
181 
182  // initialize private data
183  priv = netdev_priv(eoe->dev);
184  *priv = eoe;
185 
186  // Usually setting the MTU appropriately makes the upper layers
187  // do the frame fragmenting. In some cases this doesn't work
188  // so the MTU is left on the Ethernet standard value and fragmenting
189  // is done "manually".
190 #if 0
191  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
192 #endif
193 
194  // connect the net_device to the kernel
195  ret = register_netdev(eoe->dev);
196  if (ret) {
197  EC_SLAVE_ERR(slave, "Unable to register net_device:"
198  " error %i\n", ret);
199  goto out_free;
200  }
201 
202  // make the last address octet unique
203  mac_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
204 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || \
205  (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
206  eth_hw_addr_set(eoe->dev, mac_addr);
207 #else
208  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
209 #endif
210 
211  return 0;
212 
213  out_free:
214  free_netdev(eoe->dev);
215  eoe->dev = NULL;
216  out_return:
217  return ret;
218 }
219 
220 /****************************************************************************/
221 
227 {
228  unregister_netdev(eoe->dev); // possibly calls close callback
229 
230  // empty transmit queue
231  ec_eoe_flush(eoe);
232 
233  if (eoe->tx_frame) {
234  dev_kfree_skb(eoe->tx_frame->skb);
235  kfree(eoe->tx_frame);
236  }
237 
238  if (eoe->rx_skb)
239  dev_kfree_skb(eoe->rx_skb);
240 
241  free_netdev(eoe->dev);
242 
245 }
246 
247 /****************************************************************************/
248 
252 {
253  ec_eoe_frame_t *frame, *next;
254  struct list_head tx_queue;
255 
256  netif_tx_lock_bh(eoe->dev);
257 
258  list_replace_init(&eoe->tx_queue, &tx_queue);
259  eoe->tx_queued_frames = 0;
260 
261  netif_tx_unlock_bh(eoe->dev);
262 
263  list_for_each_entry_safe(frame, next, &tx_queue, queue) {
264  list_del(&frame->queue);
265  dev_kfree_skb(frame->skb);
266  kfree(frame);
267  }
268 }
269 
270 /****************************************************************************/
271 
277 {
278  size_t remaining_size, current_size, complete_offset;
279  unsigned int last_fragment;
280  uint8_t *data;
281 #if EOE_DEBUG_LEVEL >= 3
282  unsigned int i;
283 #endif
284 
285  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
286 
287  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
288  current_size = remaining_size;
289  last_fragment = 1;
290  } else {
291  current_size =
292  ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
293  last_fragment = 0;
294  }
295 
296  if (eoe->tx_fragment_number) {
297  complete_offset = eoe->tx_offset / 32;
298  }
299  else {
300  // complete size in 32 bit blocks, rounded up.
301  complete_offset = remaining_size / 32 + 1;
302  }
303 
304 #if EOE_DEBUG_LEVEL >= 2
305  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX sending fragment %u%s"
306  " with %zu octets (%zu). %u frames queued.\n",
307  eoe->dev->name, eoe->tx_fragment_number,
308  last_fragment ? "" : "+", current_size, complete_offset,
309  eoe->tx_queued_frames);
310 #endif
311 
312 #if EOE_DEBUG_LEVEL >= 3
313  EC_SLAVE_DBG(eoe->slave, 0, "");
314  for (i = 0; i < current_size; i++) {
315  printk(KERN_CONT "%02X ",
316  eoe->tx_frame->skb->data[eoe->tx_offset + i]);
317  if ((i + 1) % 16 == 0) {
318  printk(KERN_CONT "\n");
319  EC_SLAVE_DBG(eoe->slave, 0, "");
320  }
321  }
322  printk(KERN_CONT "\n");
323 #endif
324 
325  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
326  EC_MBOX_TYPE_EOE, current_size + 4);
327  if (IS_ERR(data))
328  return PTR_ERR(data);
329 
330  EC_WRITE_U8 (data, EC_EOE_FRAMETYPE_INIT_REQ); // Initiate EoE Request
331  EC_WRITE_U8 (data + 1, last_fragment);
332  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
333  (complete_offset & 0x3F) << 6 |
334  (eoe->tx_frame_number & 0x0F) << 12));
335 
336  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
337  eoe->queue_datagram = 1;
338 
339  eoe->tx_offset += current_size;
340  eoe->tx_fragment_number++;
341  return 0;
342 }
343 
344 /****************************************************************************/
345 
348 void ec_eoe_run(ec_eoe_t *eoe )
349 {
350  if (!eoe->opened) {
351  if (!ec_eoe_holds_mbox_lock(eoe))
352  return;
353 
354  // the device was closed during a receive sequence. release the
355  // mailbox read lock, as soon as the datagram is not in use any more.
356  if (eoe->queue_datagram) {
357  eoe->queue_datagram = 0; // not handed over yet
358  } else if (eoe->datagram.state == EC_DATAGRAM_QUEUED ||
359  eoe->datagram.state == EC_DATAGRAM_SENT) {
360  return;
361  }
362 
365  return;
366  }
367 
368  // if the datagram was not sent, or is not yet received, skip this cycle
369  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
370  return;
371 
372  // call state function
373  eoe->state(eoe);
374 
375  // update statistics
376  if (jiffies - eoe->rate_jiffies > HZ) {
377  eoe->rx_rate = eoe->rx_counter;
378  eoe->tx_rate = eoe->tx_counter;
379  eoe->rx_counter = 0;
380  eoe->tx_counter = 0;
381  eoe->rate_jiffies = jiffies;
382  }
383 
385 }
386 
387 /****************************************************************************/
388 
392 {
393  if (eoe->queue_datagram) {
395  eoe->queue_datagram = 0;
396  }
397 }
398 
399 /****************************************************************************/
400 
405 int ec_eoe_is_open(const ec_eoe_t *eoe )
406 {
407  return eoe->opened;
408 }
409 
410 /****************************************************************************/
411 
419 {
420  return ec_fsm_mbox_holds_lock(&eoe->fsm_mbox);
421 }
422 
423 /****************************************************************************/
424 
430 int ec_eoe_is_idle(const ec_eoe_t *eoe )
431 {
432  return eoe->rx_idle && eoe->tx_idle;
433 }
434 
435 /*****************************************************************************
436  * STATE PROCESSING FUNCTIONS
437  ****************************************************************************/
438 
447 {
448  if (eoe->slave->error_flag ||
450  eoe->rx_idle = 1;
451  eoe->tx_idle = 1;
452  return;
453  }
454 
455  ec_fsm_mbox_poll(&eoe->fsm_mbox, eoe->slave, &eoe->slave->mbox_eoe_data);
457  eoe->state(eoe);
458 }
459 
460 /****************************************************************************/
461 
467 {
468  size_t rec_size, data_size;
469  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
470  uint8_t size_or_offset, fragment_number;
471 #if EOE_DEBUG_LEVEL >= 2
472  uint8_t frame_number;
473 #endif
474 #if EOE_DEBUG_LEVEL >= 3
475  unsigned int i;
476 #endif
477 
478  if (ec_fsm_mbox_exec(&eoe->fsm_mbox, &eoe->datagram)) {
479  if (eoe->datagram.state != EC_DATAGRAM_UNUSED) {
480  eoe->queue_datagram = 1;
481  }
482  return;
483  }
484 
485  switch (ec_fsm_mbox_result(&eoe->fsm_mbox)) {
486  case 0:
487  eoe->rx_idle = 0;
488  break;
489  case -EAGAIN: // no EoE data available
490  case -EBUSY: // do not wait for the other read request, but transmit
491  eoe->rx_idle = 1;
493  return;
494  default:
495  eoe->stats.rx_errors++;
496 #if EOE_DEBUG_LEVEL >= 1
497  EC_SLAVE_WARN(eoe->slave, "Failed to receive mailbox"
498  " data for %s.\n", eoe->dev->name);
499 #endif
501  return;
502  }
503 
504  data = ec_slave_mbox_fetch(eoe->slave, &eoe->slave->mbox_eoe_data,
505  &mbox_prot, &rec_size);
506  if (IS_ERR(data)) {
507  eoe->stats.rx_errors++;
508 #if EOE_DEBUG_LEVEL >= 1
509  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
510  eoe->dev->name);
511 #endif
513  return;
514  }
515 
516  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
517  eoe->stats.rx_errors++;
518 #if EOE_DEBUG_LEVEL >= 1
519  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
520  eoe->dev->name);
521 #endif
523  return;
524  }
525 
526  frame_type = EC_READ_U16(data) & 0x000F;
527 
528  if (frame_type != EC_EOE_FRAMETYPE_INIT_REQ) { // EoE Fragment Data
529 #if EOE_DEBUG_LEVEL >= 1
530  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
531  " Dropping.\n", eoe->dev->name);
532 #endif
533  eoe->stats.rx_dropped++;
535  return;
536  }
537 
538  // EoE Fragment Request received
539 
540  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
541  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
542  fragment_number = EC_READ_U16(data + 2) & 0x003F;
543  size_or_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
544 #if EOE_DEBUG_LEVEL >= 2
545  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
546 #endif
547 
548  data_size = time_appended ? rec_size - 8 : rec_size - 4;
549 
550 #if EOE_DEBUG_LEVEL >= 2
551  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
552  " frame %u%s, %zu octets\n", eoe->dev->name, fragment_number,
553  last_fragment ? "" : "+", size_or_offset, frame_number,
554  time_appended ? ", + timestamp" : "", data_size);
555 #endif
556 
557 #if EOE_DEBUG_LEVEL >= 3
558  EC_SLAVE_DBG(eoe->slave, 0, "");
559  for (i = 0; i < rec_size - 4; i++) {
560  printk(KERN_CONT "%02X ", data[i + 4]);
561  if ((i + 1) % 16 == 0) {
562  printk(KERN_CONT "\n");
563  EC_SLAVE_DBG(eoe->slave, 0, "");
564  }
565  }
566  printk(KERN_CONT "\n");
567 #endif
568 
569  if (!fragment_number) {
570  size_t complete_size = size_or_offset * 32;
571 
572  if (eoe->rx_skb) {
573  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
574  dev_kfree_skb(eoe->rx_skb);
575  eoe->rx_skb = NULL;
576  }
577 
578  if (!complete_size || data_size > complete_size) {
579  EC_SLAVE_ERR(eoe->slave, "Received corrupted EoE first fragment"
580  " (%zu of %zu bytes)!\n", data_size, complete_size);
581  eoe->stats.rx_errors++;
583  return;
584  }
585 
586  // new socket buffer
587  if (!(eoe->rx_skb = dev_alloc_skb(complete_size))) {
588  if (printk_ratelimit()) {
589  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
590  " frame dropped.\n");
591  }
592  eoe->stats.rx_dropped++;
594  return;
595  }
596 
597  eoe->rx_skb_offset = 0;
598  eoe->rx_skb_size = complete_size;
599  eoe->rx_expected_fragment = 0;
600  }
601  else {
602  off_t offset = size_or_offset * 32;
603 
604  if (!eoe->rx_skb) {
605  eoe->stats.rx_dropped++;
607  return;
608  }
609 
610  if (offset != eoe->rx_skb_offset ||
611  offset + data_size > eoe->rx_skb_size ||
612  fragment_number != eoe->rx_expected_fragment) {
613  dev_kfree_skb(eoe->rx_skb);
614  eoe->rx_skb = NULL;
615  eoe->stats.rx_errors++;
616 #if EOE_DEBUG_LEVEL >= 1
617  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
618  eoe->dev->name);
619 #endif
621  return;
622  }
623  }
624 
625  // copy fragment into socket buffer
626  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
627  eoe->rx_skb_offset += data_size;
628 
629  if (last_fragment) {
630  // update statistics
631  eoe->stats.rx_packets++;
632  eoe->stats.rx_bytes += eoe->rx_skb->len;
633  eoe->rx_counter += eoe->rx_skb->len;
634 
635 #if EOE_DEBUG_LEVEL >= 2
636  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
637  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
638 #endif
639 
640  // pass socket buffer to network stack
641  eoe->rx_skb->dev = eoe->dev;
642  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
643  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
644  if (netif_rx(eoe->rx_skb)) {
645  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
646  }
647  eoe->rx_skb = NULL;
648 
650  }
651  else {
652  eoe->rx_expected_fragment++;
653 #if EOE_DEBUG_LEVEL >= 2
654  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
655  eoe->dev->name, eoe->rx_expected_fragment);
656 #endif
658  }
659 }
660 
661 /****************************************************************************/
662 
671 {
672 #if EOE_DEBUG_LEVEL >= 2
673  unsigned int wakeup = 0;
674 #endif
675 
676  if (eoe->slave->error_flag ||
678  eoe->rx_idle = 1;
679  eoe->tx_idle = 1;
680  return;
681  }
682 
683  netif_tx_lock_bh(eoe->dev);
684 
685  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
686  netif_tx_unlock_bh(eoe->dev);
687  eoe->tx_idle = 1;
688  // no data available.
689  // start a new receive immediately.
691  return;
692  }
693 
694  // take the first frame out of the queue
695  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
696  list_del(&eoe->tx_frame->queue);
697  if (!eoe->tx_queue_active &&
698  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
699  netif_wake_queue(eoe->dev);
700  eoe->tx_queue_active = 1;
701 #if EOE_DEBUG_LEVEL >= 2
702  wakeup = 1;
703 #endif
704  }
705 
706  eoe->tx_queued_frames--;
707  netif_tx_unlock_bh(eoe->dev);
708 
709  eoe->tx_idle = 0;
710 
711  eoe->tx_frame_number++;
712  eoe->tx_frame_number %= 16;
713  eoe->tx_fragment_number = 0;
714  eoe->tx_offset = 0;
715 
716  if (ec_eoe_send(eoe)) {
717  dev_kfree_skb(eoe->tx_frame->skb);
718  kfree(eoe->tx_frame);
719  eoe->tx_frame = NULL;
720  eoe->stats.tx_errors++;
722 #if EOE_DEBUG_LEVEL >= 1
723  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
724 #endif
725  return;
726  }
727 
728 #if EOE_DEBUG_LEVEL >= 2
729  if (wakeup)
730  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
731  eoe->dev->name);
732 #endif
733 
734  eoe->tries = EC_EOE_TRIES;
736 }
737 
738 /****************************************************************************/
739 
746 {
747  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
748  if (eoe->tries) {
749  eoe->tries--; // try again
750  eoe->queue_datagram = 1;
751  } else {
752  eoe->stats.tx_errors++;
753 #if EOE_DEBUG_LEVEL >= 1
754  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
755  " datagram for %s after %u tries.\n",
756  eoe->dev->name, EC_EOE_TRIES);
757 #endif
759  }
760  return;
761  }
762 
763  if (eoe->datagram.working_counter != 1) {
764  if (eoe->tries) {
765  eoe->tries--; // try again
766  eoe->queue_datagram = 1;
767  } else {
768  eoe->stats.tx_errors++;
769 #if EOE_DEBUG_LEVEL >= 1
770  EC_SLAVE_WARN(eoe->slave, "No sending response"
771  " for %s after %u tries.\n",
772  eoe->dev->name, EC_EOE_TRIES);
773 #endif
775  }
776  return;
777  }
778 
779  // frame completely sent
780  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
781  eoe->stats.tx_packets++;
782  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
783  eoe->tx_counter += eoe->tx_frame->skb->len;
784  dev_kfree_skb(eoe->tx_frame->skb);
785  kfree(eoe->tx_frame);
786  eoe->tx_frame = NULL;
788  }
789  else { // send next fragment
790  if (ec_eoe_send(eoe)) {
791  dev_kfree_skb(eoe->tx_frame->skb);
792  kfree(eoe->tx_frame);
793  eoe->tx_frame = NULL;
794  eoe->stats.tx_errors++;
795 #if EOE_DEBUG_LEVEL >= 1
796  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
797 #endif
799  }
800  }
801 }
802 
803 /*****************************************************************************
804  * NET_DEVICE functions
805  ****************************************************************************/
806 
811 int ec_eoedev_open(struct net_device *dev )
812 {
813  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
814  ec_eoe_flush(eoe);
815  eoe->opened = 1;
816  eoe->rx_idle = 0;
817  eoe->tx_idle = 0;
818  netif_start_queue(dev);
819  eoe->tx_queue_active = 1;
820 #if EOE_DEBUG_LEVEL >= 2
821  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
822 #endif
823  return 0;
824 }
825 
826 /****************************************************************************/
827 
832 int ec_eoedev_stop(struct net_device *dev )
833 {
834  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
835  netif_stop_queue(dev);
836  eoe->rx_idle = 1;
837  eoe->tx_idle = 1;
838  eoe->tx_queue_active = 0;
839  eoe->opened = 0;
840  ec_eoe_flush(eoe);
841 #if EOE_DEBUG_LEVEL >= 2
842  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
843 #endif
844  return 0;
845 }
846 
847 /****************************************************************************/
848 
853 int ec_eoedev_tx(struct sk_buff *skb,
854  struct net_device *dev
855  )
856 {
857  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
858  ec_eoe_frame_t *frame;
859 
860 #if 0
861  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
862  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
863  " exceeds MTU. dropping.\n", skb->len);
864  dev_kfree_skb(skb);
865  eoe->stats.tx_dropped++;
866  return 0;
867  }
868 #endif
869 
870  WARN_ON_ONCE(skb_get_queue_mapping(skb) != 0);
871  lockdep_assert_held(&netdev_get_tx_queue(dev, 0)->_xmit_lock);
872 
873  if (!(frame =
874  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
875  if (printk_ratelimit())
876  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
877  return 1;
878  }
879 
880  frame->skb = skb;
881 
882  list_add_tail(&frame->queue, &eoe->tx_queue);
883  eoe->tx_queued_frames++;
884  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
885  netif_stop_queue(dev);
886  eoe->tx_queue_active = 0;
887  }
888 
889 #if EOE_DEBUG_LEVEL >= 2
890  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
891  " with %u octets (%u frames queued).\n",
892  eoe->dev->name, skb->len, eoe->tx_queued_frames);
893  if (!eoe->tx_queue_active)
894  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
895 #endif
896 
897  return 0;
898 }
899 
900 /****************************************************************************/
901 
906 struct net_device_stats *ec_eoedev_stats(
907  struct net_device *dev
908  )
909 {
910  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
911  return &eoe->stats;
912 }
913 
914 /****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:391
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:853
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:143
uint16_t ring_position
Ring position.
Definition: slave.h:177
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
Definition: globals.h:110
Queued frame structure.
Definition: ethernet.h:58
uint32_t tx_counter
octets transmitted during last second
Definition: ethernet.h:104
#define EC_EOE_TX_QUEUE_SIZE
Size of the EoE tx queue.
Definition: ethernet.c:66
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:61
uint16_t configured_tx_mailbox_size
Configured send mailbox size.
Definition: slave.h:195
void ec_eoe_state_rx_fetch(ec_eoe_t *)
State: RX_FETCH.
Definition: ethernet.c:466
struct list_head tx_queue
queue for frames to send
Definition: ethernet.h:96
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:100
uint8_t tx_fragment_number
number of the fragment
Definition: ethernet.h:102
int ec_eoe_send(ec_eoe_t *)
Sends a frame or the next fragment.
Definition: ethernet.c:276
ec_slave_t * slave
pointer to the corresponding slave
Definition: ethernet.h:78
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:1015
int ec_eoe_holds_mbox_lock(const ec_eoe_t *eoe)
Returns, if the handler holds the mailbox read lock of its slave.
Definition: ethernet.c:418
int ec_fsm_mbox_exec(ec_fsm_mbox_t *fsm, ec_datagram_t *datagram)
Executes the current state of the state machine.
Definition: fsm_mbox.c:161
unsigned int tx_queue_size
Transmit queue size.
Definition: ethernet.h:97
size_t rx_skb_size
size of the allocated socket buffer memory
Definition: ethernet.h:90
size_t tx_offset
number of octets sent
Definition: ethernet.h:103
EtherCAT slave structure.
void ec_eoe_state_tx_start(ec_eoe_t *)
State: TX START.
Definition: ethernet.c:670
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
Definition: slave.h:84
Not used by its state machine in this cycle and not to be queued.
Definition: datagram.h:73
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3353
unsigned int tx_queue_active
kernel netif queue started
Definition: ethernet.h:98
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
Definition: datagram.h:109
ec_mbox_data_t mbox_eoe_data
Received mailbox data for EoE.
Definition: slave.h:236
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:290
uint16_t working_counter
Working counter.
Definition: datagram.h:96
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:85
#define EC_EOE_TRIES
Number of tries.
Definition: ethernet.c:70
Sent (still in the queue).
Definition: datagram.h:69
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
Definition: ethernet.c:251
uint32_t tx_rate
transmit rate (bps)
Definition: ethernet.h:105
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
Definition: datagram.c:625
Global definitions and macros.
uint8_t rx_expected_fragment
next expected fragment number
Definition: ethernet.h:91
EtherCAT master structure.
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
Definition: ethernet.c:446
EtherCAT slave.
Definition: slave.h:170
int ec_fsm_mbox_result(const ec_fsm_mbox_t *fsm)
Returns the result of the terminated state machine.
Definition: fsm_mbox.c:195
Ethernet over EtherCAT (EoE)
ec_datagram_state_t state
State.
Definition: datagram.h:97
void ec_fsm_mbox_abort(ec_fsm_mbox_t *fsm)
Aborts the state machine and releases the mailbox read lock, if held.
Definition: fsm_mbox.c:220
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
Definition: ethernet.c:430
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
Definition: slave.h:70
unsigned long rate_jiffies
time of last rate output
Definition: ethernet.h:86
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3370
Main device.
Definition: globals.h:204
ec_master_t * master
Master owning the slave.
Definition: slave.h:172
struct list_head queue
list item
Definition: ethernet.h:60
void ec_eoe_state_tx_sent(ec_eoe_t *)
State: TX SENT.
Definition: ethernet.c:745
unsigned int opened
net_device is opened
Definition: ethernet.h:85
off_t rx_skb_offset
current write pointer in the socket buffer
Definition: ethernet.h:89
ec_datagram_t datagram
datagram
Definition: ethernet.h:79
struct net_device_stats stats
device statistics
Definition: ethernet.h:84
uint16_t effective_alias
Effective alias address.
Definition: slave.h:179
unsigned int tx_idle
Idle flag.
Definition: ethernet.h:106
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
Definition: ecrt.h:3261
uint32_t rx_rate
receive rate (bps)
Definition: ethernet.h:93
Mailbox functionality.
struct sk_buff * rx_skb
current rx socket buffer
Definition: ethernet.h:88
void(* state)(ec_eoe_t *)
state function for the state machine
Definition: ethernet.h:81
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
Definition: datagram.c:80
Queued for sending.
Definition: datagram.h:68
uint8_t tx_frame_number
number of the transmitted frame
Definition: ethernet.h:101
uint32_t rx_counter
octets received during last second
Definition: ethernet.h:92
void ec_fsm_mbox_init(ec_fsm_mbox_t *fsm)
Constructor.
Definition: fsm_mbox.c:45
uint16_t configured_rx_mailbox_size
Configured receive mailbox size.
Definition: slave.h:191
int ec_eoedev_open(struct net_device *)
Opens the virtual network device.
Definition: ethernet.c:811
ec_eoe_frame_t * tx_frame
current TX frame
Definition: ethernet.h:100
unsigned int tries
Tries.
Definition: ethernet.h:108
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
Definition: ethernet.c:348
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:832
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:226
unsigned int index
Index.
Definition: master.h:195
int ec_fsm_mbox_holds_lock(const ec_fsm_mbox_t *fsm)
Checks, if the state machine holds the slave&#39;s mailbox read lock.
Definition: fsm_mbox.c:208
Received (dequeued).
Definition: datagram.h:70
void ec_fsm_mbox_poll(ec_fsm_mbox_t *fsm, ec_slave_t *slave, ec_mbox_data_t *mbox_data)
Starts a single mailbox check without waiting.
Definition: fsm_mbox.c:123
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:75
unsigned int error_flag
Stop processing after an error.
Definition: slave.h:187
ec_fsm_mbox_t fsm_mbox
Mailbox receive state machine.
Definition: ethernet.h:82
unsigned int rx_idle
Idle flag.
Definition: ethernet.h:94
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition: master.h:207
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
Definition: ethernet.c:906
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:405
struct net_device * dev
net_device for virtual ethernet device
Definition: ethernet.h:83
void ec_fsm_mbox_clear(ec_fsm_mbox_t *fsm)
Destructor.
Definition: fsm_mbox.c:64
unsigned int queue_datagram
the datagram is ready for queuing
Definition: ethernet.h:80
unsigned int tx_queued_frames
number of frames in the queue
Definition: ethernet.h:99
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
Definition: datagram.c:110