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