IgH EtherCAT Master  1.6.0
ethernet.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2006-2008 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 #ifdef CONFIG_SUSE_KERNEL
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 
60 #define EOE_DEBUG_LEVEL 1
61 
64 #define EC_EOE_TX_QUEUE_SIZE 100
65 
68 #define EC_EOE_TRIES 100
69 
70 /****************************************************************************/
71 
72 void ec_eoe_flush(ec_eoe_t *);
73 
74 // state functions
80 
81 // net_device functions
82 int ec_eoedev_open(struct net_device *);
83 int ec_eoedev_stop(struct net_device *);
84 int ec_eoedev_tx(struct sk_buff *, struct net_device *);
85 struct net_device_stats *ec_eoedev_stats(struct net_device *);
86 
87 /****************************************************************************/
88 
91 static const struct net_device_ops ec_eoedev_ops = {
92  .ndo_open = ec_eoedev_open,
93  .ndo_stop = ec_eoedev_stop,
94  .ndo_start_xmit = ec_eoedev_tx,
95  .ndo_get_stats = ec_eoedev_stats,
96 };
97 
98 /****************************************************************************/
99 
107  ec_eoe_t *eoe,
108  ec_slave_t *slave
109  )
110 {
111  ec_eoe_t **priv;
112  int ret = 0;
113  char name[EC_DATAGRAM_NAME_SIZE];
114  u8 mac_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
115 
116  eoe->slave = slave;
117 
118  ec_datagram_init(&eoe->datagram);
119  eoe->queue_datagram = 0;
121  eoe->opened = 0;
122  eoe->rx_skb = NULL;
123  eoe->rx_expected_fragment = 0;
124  INIT_LIST_HEAD(&eoe->tx_queue);
125  eoe->tx_frame = NULL;
126  eoe->tx_queue_active = 0;
128  eoe->tx_queued_frames = 0;
129 
130  eoe->tx_frame_number = 0xFF;
131  memset(&eoe->stats, 0, sizeof(struct net_device_stats));
132 
133  eoe->rx_counter = 0;
134  eoe->tx_counter = 0;
135  eoe->rx_rate = 0;
136  eoe->tx_rate = 0;
137  eoe->rate_jiffies = 0;
138  eoe->rx_idle = 1;
139  eoe->tx_idle = 1;
140 
141  /* device name eoe<MASTER>[as]<SLAVE>, because networking scripts don't
142  * like hyphens etc. in interface names. */
143  if (slave->effective_alias) {
144  snprintf(name, EC_DATAGRAM_NAME_SIZE,
145  "eoe%ua%u", slave->master->index, slave->effective_alias);
146  } else {
147  snprintf(name, EC_DATAGRAM_NAME_SIZE,
148  "eoe%us%u", slave->master->index, slave->ring_position);
149  }
150 
151  snprintf(eoe->datagram.name, EC_DATAGRAM_NAME_SIZE, name);
152 
153 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
154  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, NET_NAME_UNKNOWN,
155  ether_setup);
156 #else
157  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup);
158 #endif
159  if (!eoe->dev) {
160  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
161  " for EoE handler!\n", name);
162  ret = -ENODEV;
163  goto out_return;
164  }
165 
166  // initialize net_device
167  eoe->dev->netdev_ops = &ec_eoedev_ops;
168 
169 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
170  eth_hw_addr_set(eoe->dev, mac_addr);
171 #else
172  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
173 #endif
174 
175  // initialize private data
176  priv = netdev_priv(eoe->dev);
177  *priv = eoe;
178 
179  // Usually setting the MTU appropriately makes the upper layers
180  // do the frame fragmenting. In some cases this doesn't work
181  // so the MTU is left on the Ethernet standard value and fragmenting
182  // is done "manually".
183 #if 0
184  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
185 #endif
186 
187  // connect the net_device to the kernel
188  ret = register_netdev(eoe->dev);
189  if (ret) {
190  EC_SLAVE_ERR(slave, "Unable to register net_device:"
191  " error %i\n", ret);
192  goto out_free;
193  }
194 
195  // make the last address octet unique
196  mac_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
197 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
198  eth_hw_addr_set(eoe->dev, mac_addr);
199 #else
200  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
201 #endif
202 
203  return 0;
204 
205  out_free:
206  free_netdev(eoe->dev);
207  eoe->dev = NULL;
208  out_return:
209  return ret;
210 }
211 
212 /****************************************************************************/
213 
219 {
220  unregister_netdev(eoe->dev); // possibly calls close callback
221 
222  // empty transmit queue
223  ec_eoe_flush(eoe);
224 
225  if (eoe->tx_frame) {
226  dev_kfree_skb(eoe->tx_frame->skb);
227  kfree(eoe->tx_frame);
228  }
229 
230  if (eoe->rx_skb)
231  dev_kfree_skb(eoe->rx_skb);
232 
233  free_netdev(eoe->dev);
234 
236 }
237 
238 /****************************************************************************/
239 
243 {
244  ec_eoe_frame_t *frame, *next;
245  struct list_head tx_queue;
246 
247  netif_tx_lock_bh(eoe->dev);
248 
249  list_replace_init(&eoe->tx_queue, &tx_queue);
250  eoe->tx_queued_frames = 0;
251 
252  netif_tx_unlock_bh(eoe->dev);
253 
254  list_for_each_entry_safe(frame, next, &tx_queue, queue) {
255  list_del(&frame->queue);
256  dev_kfree_skb(frame->skb);
257  kfree(frame);
258  }
259 }
260 
261 /****************************************************************************/
262 
268 {
269  size_t remaining_size, current_size, complete_offset;
270  unsigned int last_fragment;
271  uint8_t *data;
272 #if EOE_DEBUG_LEVEL >= 3
273  unsigned int i;
274 #endif
275 
276  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
277 
278  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
279  current_size = remaining_size;
280  last_fragment = 1;
281  } else {
282  current_size = ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
283  last_fragment = 0;
284  }
285 
286  if (eoe->tx_fragment_number) {
287  complete_offset = eoe->tx_offset / 32;
288  }
289  else {
290  // complete size in 32 bit blocks, rounded up.
291  complete_offset = remaining_size / 32 + 1;
292  }
293 
294 #if EOE_DEBUG_LEVEL >= 2
295  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX sending fragment %u%s"
296  " with %zu octets (%zu). %u frames queued.\n",
297  eoe->dev->name, eoe->tx_fragment_number,
298  last_fragment ? "" : "+", current_size, complete_offset,
299  eoe->tx_queued_frames);
300 #endif
301 
302 #if EOE_DEBUG_LEVEL >= 3
303  EC_SLAVE_DBG(eoe->slave, 0, "");
304  for (i = 0; i < current_size; i++) {
305  printk(KERN_CONT "%02X ",
306  eoe->tx_frame->skb->data[eoe->tx_offset + i]);
307  if ((i + 1) % 16 == 0) {
308  printk(KERN_CONT "\n");
309  EC_SLAVE_DBG(eoe->slave, 0, "");
310  }
311  }
312  printk(KERN_CONT "\n");
313 #endif
314 
315  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
316  EC_MBOX_TYPE_EOE, current_size + 4);
317  if (IS_ERR(data))
318  return PTR_ERR(data);
319 
320  EC_WRITE_U8 (data, EC_EOE_FRAMETYPE_INIT_REQ); // Initiate EoE Request
321  EC_WRITE_U8 (data + 1, last_fragment);
322  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
323  (complete_offset & 0x3F) << 6 |
324  (eoe->tx_frame_number & 0x0F) << 12));
325 
326  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
327  eoe->queue_datagram = 1;
328 
329  eoe->tx_offset += current_size;
330  eoe->tx_fragment_number++;
331  return 0;
332 }
333 
334 /****************************************************************************/
335 
338 void ec_eoe_run(ec_eoe_t *eoe )
339 {
340  if (!eoe->opened)
341  return;
342 
343  // if the datagram was not sent, or is not yet received, skip this cycle
344  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
345  return;
346 
347  // call state function
348  eoe->state(eoe);
349 
350  // update statistics
351  if (jiffies - eoe->rate_jiffies > HZ) {
352  eoe->rx_rate = eoe->rx_counter;
353  eoe->tx_rate = eoe->tx_counter;
354  eoe->rx_counter = 0;
355  eoe->tx_counter = 0;
356  eoe->rate_jiffies = jiffies;
357  }
358 
360 }
361 
362 /****************************************************************************/
363 
367 {
368  if (eoe->queue_datagram) {
370  eoe->queue_datagram = 0;
371  }
372 }
373 
374 /****************************************************************************/
375 
380 int ec_eoe_is_open(const ec_eoe_t *eoe )
381 {
382  return eoe->opened;
383 }
384 
385 /****************************************************************************/
386 
392 int ec_eoe_is_idle(const ec_eoe_t *eoe )
393 {
394  return eoe->rx_idle && eoe->tx_idle;
395 }
396 
397 /*****************************************************************************
398  * STATE PROCESSING FUNCTIONS
399  ****************************************************************************/
400 
409 {
410  if (eoe->slave->error_flag ||
412  eoe->rx_idle = 1;
413  eoe->tx_idle = 1;
414  return;
415  }
416 
418  eoe->queue_datagram = 1;
420 }
421 
422 /****************************************************************************/
423 
430 {
431  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
432  eoe->stats.rx_errors++;
433 #if EOE_DEBUG_LEVEL >= 1
434  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
435  " check datagram for %s.\n", eoe->dev->name);
436 #endif
438  return;
439  }
440 
441  if (!ec_slave_mbox_check(&eoe->datagram)) {
442  eoe->rx_idle = 1;
444  return;
445  }
446 
447  eoe->rx_idle = 0;
449  eoe->queue_datagram = 1;
451 }
452 
453 /****************************************************************************/
454 
461 {
462  size_t rec_size, data_size;
463  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
464  uint8_t fragment_offset, fragment_number;
465 #if EOE_DEBUG_LEVEL >= 2
466  uint8_t frame_number;
467 #endif
468  off_t offset;
469 #if EOE_DEBUG_LEVEL >= 3
470  unsigned int i;
471 #endif
472 
473  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
474  eoe->stats.rx_errors++;
475 #if EOE_DEBUG_LEVEL >= 1
476  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
477  " fetch datagram for %s.\n", eoe->dev->name);
478 #endif
480  return;
481  }
482 
483  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
484  &mbox_prot, &rec_size);
485  if (IS_ERR(data)) {
486  eoe->stats.rx_errors++;
487 #if EOE_DEBUG_LEVEL >= 1
488  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
489  eoe->dev->name);
490 #endif
492  return;
493  }
494 
495  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
496  eoe->stats.rx_errors++;
497 #if EOE_DEBUG_LEVEL >= 1
498  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
499  eoe->dev->name);
500 #endif
502  return;
503  }
504 
505  frame_type = EC_READ_U16(data) & 0x000F;
506 
507  if (frame_type != EC_EOE_FRAMETYPE_INIT_REQ) { // EoE Fragment Data
508 #if EOE_DEBUG_LEVEL >= 1
509  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
510  " Dropping.\n", eoe->dev->name);
511 #endif
512  eoe->stats.rx_dropped++;
514  return;
515  }
516 
517  // EoE Fragment Request received
518 
519  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
520  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
521  fragment_number = EC_READ_U16(data + 2) & 0x003F;
522  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
523 #if EOE_DEBUG_LEVEL >= 2
524  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
525 #endif
526 
527 #if EOE_DEBUG_LEVEL >= 2
528  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
529  " frame %u%s, %u octets\n", eoe->dev->name, fragment_number,
530  last_fragment ? "" : "+", fragment_offset, frame_number,
531  time_appended ? ", + timestamp" : "",
532  time_appended ? rec_size - 8 : rec_size - 4);
533 #endif
534 
535 #if EOE_DEBUG_LEVEL >= 3
536  EC_SLAVE_DBG(eoe->slave, 0, "");
537  for (i = 0; i < rec_size - 4; i++) {
538  printk(KERN_CONT "%02X ", data[i + 4]);
539  if ((i + 1) % 16 == 0) {
540  printk(KERN_CONT "\n");
541  EC_SLAVE_DBG(eoe->slave, 0, "");
542  }
543  }
544  printk(KERN_CONT "\n");
545 #endif
546 
547  data_size = time_appended ? rec_size - 8 : rec_size - 4;
548 
549  if (!fragment_number) {
550  if (eoe->rx_skb) {
551  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
552  dev_kfree_skb(eoe->rx_skb);
553  }
554 
555  // new socket buffer
556  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
557  if (printk_ratelimit())
558  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
559  " frame dropped.\n");
560  eoe->stats.rx_dropped++;
562  return;
563  }
564 
565  eoe->rx_skb_offset = 0;
566  eoe->rx_skb_size = fragment_offset * 32;
567  eoe->rx_expected_fragment = 0;
568  }
569  else {
570  if (!eoe->rx_skb) {
571  eoe->stats.rx_dropped++;
573  return;
574  }
575 
576  offset = fragment_offset * 32;
577  if (offset != eoe->rx_skb_offset ||
578  offset + data_size > eoe->rx_skb_size ||
579  fragment_number != eoe->rx_expected_fragment) {
580  dev_kfree_skb(eoe->rx_skb);
581  eoe->rx_skb = NULL;
582  eoe->stats.rx_errors++;
583 #if EOE_DEBUG_LEVEL >= 1
584  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
585  eoe->dev->name);
586 #endif
588  return;
589  }
590  }
591 
592  // copy fragment into socket buffer
593  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
594  eoe->rx_skb_offset += data_size;
595 
596  if (last_fragment) {
597  // update statistics
598  eoe->stats.rx_packets++;
599  eoe->stats.rx_bytes += eoe->rx_skb->len;
600  eoe->rx_counter += eoe->rx_skb->len;
601 
602 #if EOE_DEBUG_LEVEL >= 2
603  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
604  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
605 #endif
606 
607  // pass socket buffer to network stack
608  eoe->rx_skb->dev = eoe->dev;
609  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
610  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
611  if (netif_rx(eoe->rx_skb)) {
612  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
613  }
614  eoe->rx_skb = NULL;
615 
617  }
618  else {
619  eoe->rx_expected_fragment++;
620 #if EOE_DEBUG_LEVEL >= 2
621  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
622  eoe->dev->name, eoe->rx_expected_fragment);
623 #endif
625  }
626 }
627 
628 /****************************************************************************/
629 
638 {
639 #if EOE_DEBUG_LEVEL >= 2
640  unsigned int wakeup = 0;
641 #endif
642 
643  if (eoe->slave->error_flag ||
645  eoe->rx_idle = 1;
646  eoe->tx_idle = 1;
647  return;
648  }
649 
650  netif_tx_lock_bh(eoe->dev);
651 
652  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
653  netif_tx_unlock_bh(eoe->dev);
654  eoe->tx_idle = 1;
655  // no data available.
656  // start a new receive immediately.
658  return;
659  }
660 
661  // take the first frame out of the queue
662  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
663  list_del(&eoe->tx_frame->queue);
664  if (!eoe->tx_queue_active &&
665  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
666  netif_wake_queue(eoe->dev);
667  eoe->tx_queue_active = 1;
668 #if EOE_DEBUG_LEVEL >= 2
669  wakeup = 1;
670 #endif
671  }
672 
673  eoe->tx_queued_frames--;
674  netif_tx_unlock_bh(eoe->dev);
675 
676  eoe->tx_idle = 0;
677 
678  eoe->tx_frame_number++;
679  eoe->tx_frame_number %= 16;
680  eoe->tx_fragment_number = 0;
681  eoe->tx_offset = 0;
682 
683  if (ec_eoe_send(eoe)) {
684  dev_kfree_skb(eoe->tx_frame->skb);
685  kfree(eoe->tx_frame);
686  eoe->tx_frame = NULL;
687  eoe->stats.tx_errors++;
689 #if EOE_DEBUG_LEVEL >= 1
690  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
691 #endif
692  return;
693  }
694 
695 #if EOE_DEBUG_LEVEL >= 2
696  if (wakeup)
697  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
698  eoe->dev->name);
699 #endif
700 
701  eoe->tries = EC_EOE_TRIES;
703 }
704 
705 /****************************************************************************/
706 
713 {
714  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
715  if (eoe->tries) {
716  eoe->tries--; // try again
717  eoe->queue_datagram = 1;
718  } else {
719  eoe->stats.tx_errors++;
720 #if EOE_DEBUG_LEVEL >= 1
721  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
722  " datagram for %s after %u tries.\n",
723  eoe->dev->name, EC_EOE_TRIES);
724 #endif
726  }
727  return;
728  }
729 
730  if (eoe->datagram.working_counter != 1) {
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, "No sending response"
738  " for %s after %u tries.\n",
739  eoe->dev->name, EC_EOE_TRIES);
740 #endif
742  }
743  return;
744  }
745 
746  // frame completely sent
747  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
748  eoe->stats.tx_packets++;
749  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
750  eoe->tx_counter += eoe->tx_frame->skb->len;
751  dev_kfree_skb(eoe->tx_frame->skb);
752  kfree(eoe->tx_frame);
753  eoe->tx_frame = NULL;
755  }
756  else { // send next fragment
757  if (ec_eoe_send(eoe)) {
758  dev_kfree_skb(eoe->tx_frame->skb);
759  kfree(eoe->tx_frame);
760  eoe->tx_frame = NULL;
761  eoe->stats.tx_errors++;
762 #if EOE_DEBUG_LEVEL >= 1
763  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
764 #endif
766  }
767  }
768 }
769 
770 /*****************************************************************************
771  * NET_DEVICE functions
772  ****************************************************************************/
773 
778 int ec_eoedev_open(struct net_device *dev )
779 {
780  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
781  ec_eoe_flush(eoe);
782  eoe->opened = 1;
783  eoe->rx_idle = 0;
784  eoe->tx_idle = 0;
785  netif_start_queue(dev);
786  eoe->tx_queue_active = 1;
787 #if EOE_DEBUG_LEVEL >= 2
788  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
789 #endif
791  return 0;
792 }
793 
794 /****************************************************************************/
795 
800 int ec_eoedev_stop(struct net_device *dev )
801 {
802  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
803  netif_stop_queue(dev);
804  eoe->rx_idle = 1;
805  eoe->tx_idle = 1;
806  eoe->tx_queue_active = 0;
807  eoe->opened = 0;
808  ec_eoe_flush(eoe);
809 #if EOE_DEBUG_LEVEL >= 2
810  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
811 #endif
813  return 0;
814 }
815 
816 /****************************************************************************/
817 
822 int ec_eoedev_tx(struct sk_buff *skb,
823  struct net_device *dev
824  )
825 {
826  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
827  ec_eoe_frame_t *frame;
828 
829 #if 0
830  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
831  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
832  " exceeds MTU. dropping.\n", skb->len);
833  dev_kfree_skb(skb);
834  eoe->stats.tx_dropped++;
835  return 0;
836  }
837 #endif
838 
839  WARN_ON_ONCE(skb_get_queue_mapping(skb) != 0);
840  lockdep_assert_held(&netdev_get_tx_queue(dev, 0)->_xmit_lock);
841 
842  if (!(frame =
843  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
844  if (printk_ratelimit())
845  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
846  return 1;
847  }
848 
849  frame->skb = skb;
850 
851  list_add_tail(&frame->queue, &eoe->tx_queue);
852  eoe->tx_queued_frames++;
853  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
854  netif_stop_queue(dev);
855  eoe->tx_queue_active = 0;
856  }
857 
858 #if EOE_DEBUG_LEVEL >= 2
859  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
860  " with %u octets (%u frames queued).\n",
861  eoe->dev->name, skb->len, eoe->tx_queued_frames);
862  if (!eoe->tx_queue_active)
863  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
864 #endif
865 
866  return 0;
867 }
868 
869 /****************************************************************************/
870 
875 struct net_device_stats *ec_eoedev_stats(
876  struct net_device *dev
877  )
878 {
879  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
880  return &eoe->stats;
881 }
882 
883 /****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:366
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:822
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:64
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:91
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:460
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 *eoe)
Sends a frame or the next fragment.
Definition: ethernet.c:267
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:968
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:637
#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:2699
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
Definition: ethernet.c:429
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:104
uint16_t working_counter
Working counter.
Definition: datagram.h:91
int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
EoE constructor.
Definition: ethernet.c:106
uint8_t link_state
device link state
Definition: device.h:80
#define EC_EOE_TRIES
Number of tries.
Definition: ethernet.c:68
Sent (still in the queue).
Definition: datagram.h:69
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
Definition: ethernet.c:242
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:408
EtherCAT slave.
Definition: slave.h:168
Ethernet over EtherCAT (EoE)
ec_datagram_state_t state
State.
Definition: datagram.h:92
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
Definition: ethernet.c:392
#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:2716
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:712
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:2611
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:300
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:778
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:338
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:800
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:218
unsigned int index
Index.
Definition: master.h:184
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:196
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
Definition: ethernet.c:875
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:380
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