IgH EtherCAT Master  1.6.1
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 fragment_offset, fragment_number;
470 #if EOE_DEBUG_LEVEL >= 2
471  uint8_t frame_number;
472 #endif
473  off_t offset;
474 #if EOE_DEBUG_LEVEL >= 3
475  unsigned int i;
476 #endif
477 
478  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
479  eoe->stats.rx_errors++;
480 #if EOE_DEBUG_LEVEL >= 1
481  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
482  " fetch datagram for %s.\n", eoe->dev->name);
483 #endif
485  return;
486  }
487 
488  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
489  &mbox_prot, &rec_size);
490  if (IS_ERR(data)) {
491  eoe->stats.rx_errors++;
492 #if EOE_DEBUG_LEVEL >= 1
493  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
494  eoe->dev->name);
495 #endif
497  return;
498  }
499 
500  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
501  eoe->stats.rx_errors++;
502 #if EOE_DEBUG_LEVEL >= 1
503  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
504  eoe->dev->name);
505 #endif
507  return;
508  }
509 
510  frame_type = EC_READ_U16(data) & 0x000F;
511 
512  if (frame_type != EC_EOE_FRAMETYPE_INIT_REQ) { // EoE Fragment Data
513 #if EOE_DEBUG_LEVEL >= 1
514  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
515  " Dropping.\n", eoe->dev->name);
516 #endif
517  eoe->stats.rx_dropped++;
519  return;
520  }
521 
522  // EoE Fragment Request received
523 
524  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
525  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
526  fragment_number = EC_READ_U16(data + 2) & 0x003F;
527  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
528 #if EOE_DEBUG_LEVEL >= 2
529  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
530 #endif
531 
532 #if EOE_DEBUG_LEVEL >= 2
533  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
534  " frame %u%s, %zu octets\n", eoe->dev->name, fragment_number,
535  last_fragment ? "" : "+", fragment_offset, frame_number,
536  time_appended ? ", + timestamp" : "",
537  time_appended ? rec_size - 8 : rec_size - 4);
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  data_size = time_appended ? rec_size - 8 : rec_size - 4;
553 
554  if (!fragment_number) {
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  }
559 
560  // new socket buffer
561  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
562  if (printk_ratelimit())
563  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
564  " frame dropped.\n");
565  eoe->stats.rx_dropped++;
567  return;
568  }
569 
570  eoe->rx_skb_offset = 0;
571  eoe->rx_skb_size = fragment_offset * 32;
572  eoe->rx_expected_fragment = 0;
573  }
574  else {
575  if (!eoe->rx_skb) {
576  eoe->stats.rx_dropped++;
578  return;
579  }
580 
581  offset = fragment_offset * 32;
582  if (offset != eoe->rx_skb_offset ||
583  offset + data_size > eoe->rx_skb_size ||
584  fragment_number != eoe->rx_expected_fragment) {
585  dev_kfree_skb(eoe->rx_skb);
586  eoe->rx_skb = NULL;
587  eoe->stats.rx_errors++;
588 #if EOE_DEBUG_LEVEL >= 1
589  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
590  eoe->dev->name);
591 #endif
593  return;
594  }
595  }
596 
597  // copy fragment into socket buffer
598  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
599  eoe->rx_skb_offset += data_size;
600 
601  if (last_fragment) {
602  // update statistics
603  eoe->stats.rx_packets++;
604  eoe->stats.rx_bytes += eoe->rx_skb->len;
605  eoe->rx_counter += eoe->rx_skb->len;
606 
607 #if EOE_DEBUG_LEVEL >= 2
608  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
609  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
610 #endif
611 
612  // pass socket buffer to network stack
613  eoe->rx_skb->dev = eoe->dev;
614  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
615  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
616  if (netif_rx(eoe->rx_skb)) {
617  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
618  }
619  eoe->rx_skb = NULL;
620 
622  }
623  else {
624  eoe->rx_expected_fragment++;
625 #if EOE_DEBUG_LEVEL >= 2
626  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
627  eoe->dev->name, eoe->rx_expected_fragment);
628 #endif
630  }
631 }
632 
633 /****************************************************************************/
634 
643 {
644 #if EOE_DEBUG_LEVEL >= 2
645  unsigned int wakeup = 0;
646 #endif
647 
648  if (eoe->slave->error_flag ||
650  eoe->rx_idle = 1;
651  eoe->tx_idle = 1;
652  return;
653  }
654 
655  netif_tx_lock_bh(eoe->dev);
656 
657  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
658  netif_tx_unlock_bh(eoe->dev);
659  eoe->tx_idle = 1;
660  // no data available.
661  // start a new receive immediately.
663  return;
664  }
665 
666  // take the first frame out of the queue
667  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
668  list_del(&eoe->tx_frame->queue);
669  if (!eoe->tx_queue_active &&
670  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
671  netif_wake_queue(eoe->dev);
672  eoe->tx_queue_active = 1;
673 #if EOE_DEBUG_LEVEL >= 2
674  wakeup = 1;
675 #endif
676  }
677 
678  eoe->tx_queued_frames--;
679  netif_tx_unlock_bh(eoe->dev);
680 
681  eoe->tx_idle = 0;
682 
683  eoe->tx_frame_number++;
684  eoe->tx_frame_number %= 16;
685  eoe->tx_fragment_number = 0;
686  eoe->tx_offset = 0;
687 
688  if (ec_eoe_send(eoe)) {
689  dev_kfree_skb(eoe->tx_frame->skb);
690  kfree(eoe->tx_frame);
691  eoe->tx_frame = NULL;
692  eoe->stats.tx_errors++;
694 #if EOE_DEBUG_LEVEL >= 1
695  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
696 #endif
697  return;
698  }
699 
700 #if EOE_DEBUG_LEVEL >= 2
701  if (wakeup)
702  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
703  eoe->dev->name);
704 #endif
705 
706  eoe->tries = EC_EOE_TRIES;
708 }
709 
710 /****************************************************************************/
711 
718 {
719  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
720  if (eoe->tries) {
721  eoe->tries--; // try again
722  eoe->queue_datagram = 1;
723  } else {
724  eoe->stats.tx_errors++;
725 #if EOE_DEBUG_LEVEL >= 1
726  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
727  " datagram for %s after %u tries.\n",
728  eoe->dev->name, EC_EOE_TRIES);
729 #endif
731  }
732  return;
733  }
734 
735  if (eoe->datagram.working_counter != 1) {
736  if (eoe->tries) {
737  eoe->tries--; // try again
738  eoe->queue_datagram = 1;
739  } else {
740  eoe->stats.tx_errors++;
741 #if EOE_DEBUG_LEVEL >= 1
742  EC_SLAVE_WARN(eoe->slave, "No sending response"
743  " for %s after %u tries.\n",
744  eoe->dev->name, EC_EOE_TRIES);
745 #endif
747  }
748  return;
749  }
750 
751  // frame completely sent
752  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
753  eoe->stats.tx_packets++;
754  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
755  eoe->tx_counter += eoe->tx_frame->skb->len;
756  dev_kfree_skb(eoe->tx_frame->skb);
757  kfree(eoe->tx_frame);
758  eoe->tx_frame = NULL;
760  }
761  else { // send next fragment
762  if (ec_eoe_send(eoe)) {
763  dev_kfree_skb(eoe->tx_frame->skb);
764  kfree(eoe->tx_frame);
765  eoe->tx_frame = NULL;
766  eoe->stats.tx_errors++;
767 #if EOE_DEBUG_LEVEL >= 1
768  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
769 #endif
771  }
772  }
773 }
774 
775 /*****************************************************************************
776  * NET_DEVICE functions
777  ****************************************************************************/
778 
783 int ec_eoedev_open(struct net_device *dev )
784 {
785  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
786  ec_eoe_flush(eoe);
787  eoe->opened = 1;
788  eoe->rx_idle = 0;
789  eoe->tx_idle = 0;
790  netif_start_queue(dev);
791  eoe->tx_queue_active = 1;
792 #if EOE_DEBUG_LEVEL >= 2
793  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
794 #endif
796  return 0;
797 }
798 
799 /****************************************************************************/
800 
805 int ec_eoedev_stop(struct net_device *dev )
806 {
807  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
808  netif_stop_queue(dev);
809  eoe->rx_idle = 1;
810  eoe->tx_idle = 1;
811  eoe->tx_queue_active = 0;
812  eoe->opened = 0;
813  ec_eoe_flush(eoe);
814 #if EOE_DEBUG_LEVEL >= 2
815  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
816 #endif
818  return 0;
819 }
820 
821 /****************************************************************************/
822 
827 int ec_eoedev_tx(struct sk_buff *skb,
828  struct net_device *dev
829  )
830 {
831  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
832  ec_eoe_frame_t *frame;
833 
834 #if 0
835  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
836  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
837  " exceeds MTU. dropping.\n", skb->len);
838  dev_kfree_skb(skb);
839  eoe->stats.tx_dropped++;
840  return 0;
841  }
842 #endif
843 
844  WARN_ON_ONCE(skb_get_queue_mapping(skb) != 0);
845  lockdep_assert_held(&netdev_get_tx_queue(dev, 0)->_xmit_lock);
846 
847  if (!(frame =
848  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
849  if (printk_ratelimit())
850  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
851  return 1;
852  }
853 
854  frame->skb = skb;
855 
856  list_add_tail(&frame->queue, &eoe->tx_queue);
857  eoe->tx_queued_frames++;
858  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
859  netif_stop_queue(dev);
860  eoe->tx_queue_active = 0;
861  }
862 
863 #if EOE_DEBUG_LEVEL >= 2
864  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
865  " with %u octets (%u frames queued).\n",
866  eoe->dev->name, skb->len, eoe->tx_queued_frames);
867  if (!eoe->tx_queue_active)
868  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
869 #endif
870 
871  return 0;
872 }
873 
874 /****************************************************************************/
875 
880 struct net_device_stats *ec_eoedev_stats(
881  struct net_device *dev
882  )
883 {
884  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
885  return &eoe->stats;
886 }
887 
888 /****************************************************************************/
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:827
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:987
OP (mailbox communication and input/output update)
Definition: globals.h:132
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:642
#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:614
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:199
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:717
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
void ec_slave_request_state(ec_slave_t *slave, ec_slave_state_t state)
Request a slave state and resets the error flag.
Definition: slave.c:306
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:783
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:805
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:223
unsigned int index
Index.
Definition: master.h:188
PREOP state (mailbox communication, no IO)
Definition: globals.h:126
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:880
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