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 i, ret = 0;
111  char name[EC_DATAGRAM_NAME_SIZE];
112 
113  eoe->slave = slave;
114 
115  ec_datagram_init(&eoe->datagram);
116  eoe->queue_datagram = 0;
118  eoe->opened = 0;
119  eoe->rx_skb = NULL;
120  eoe->rx_expected_fragment = 0;
121  INIT_LIST_HEAD(&eoe->tx_queue);
122  eoe->tx_frame = NULL;
123  eoe->tx_queue_active = 0;
125  eoe->tx_queued_frames = 0;
126 
127  sema_init(&eoe->tx_queue_sem, 1);
128  eoe->tx_frame_number = 0xFF;
129  memset(&eoe->stats, 0, sizeof(struct net_device_stats));
130 
131  eoe->rx_counter = 0;
132  eoe->tx_counter = 0;
133  eoe->rx_rate = 0;
134  eoe->tx_rate = 0;
135  eoe->rate_jiffies = 0;
136  eoe->rx_idle = 1;
137  eoe->tx_idle = 1;
138 
139  /* device name eoe<MASTER>[as]<SLAVE>, because networking scripts don't
140  * like hyphens etc. in interface names. */
141  if (slave->effective_alias) {
142  snprintf(name, EC_DATAGRAM_NAME_SIZE,
143  "eoe%ua%u", slave->master->index, slave->effective_alias);
144  } else {
145  snprintf(name, EC_DATAGRAM_NAME_SIZE,
146  "eoe%us%u", slave->master->index, slave->ring_position);
147  }
148 
149  snprintf(eoe->datagram.name, EC_DATAGRAM_NAME_SIZE, name);
150 
151 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
152  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, NET_NAME_UNKNOWN,
153  ether_setup);
154 #else
155  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup);
156 #endif
157  if (!eoe->dev) {
158  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
159  " for EoE handler!\n", name);
160  ret = -ENODEV;
161  goto out_return;
162  }
163 
164  // initialize net_device
165 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
166  eoe->dev->netdev_ops = &ec_eoedev_ops;
167 #else
168  eoe->dev->open = ec_eoedev_open;
169  eoe->dev->stop = ec_eoedev_stop;
170  eoe->dev->hard_start_xmit = ec_eoedev_tx;
171  eoe->dev->get_stats = ec_eoedev_stats;
172 #endif
173 
174  for (i = 0; i < ETH_ALEN; i++)
175  eoe->dev->dev_addr[i] = i | (i << 4);
176 
177  // initialize private data
178  priv = netdev_priv(eoe->dev);
179  *priv = eoe;
180 
181  // Usually setting the MTU appropriately makes the upper layers
182  // do the frame fragmenting. In some cases this doesn't work
183  // so the MTU is left on the Ethernet standard value and fragmenting
184  // is done "manually".
185 #if 0
186  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
187 #endif
188 
189  // connect the net_device to the kernel
190  ret = register_netdev(eoe->dev);
191  if (ret) {
192  EC_SLAVE_ERR(slave, "Unable to register net_device:"
193  " error %i\n", ret);
194  goto out_free;
195  }
196 
197  // make the last address octet unique
198  eoe->dev->dev_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
199  return 0;
200 
201  out_free:
202  free_netdev(eoe->dev);
203  eoe->dev = NULL;
204  out_return:
205  return ret;
206 }
207 
208 /*****************************************************************************/
209 
215 {
216  unregister_netdev(eoe->dev); // possibly calls close callback
217 
218  // empty transmit queue
219  ec_eoe_flush(eoe);
220 
221  if (eoe->tx_frame) {
222  dev_kfree_skb(eoe->tx_frame->skb);
223  kfree(eoe->tx_frame);
224  }
225 
226  if (eoe->rx_skb)
227  dev_kfree_skb(eoe->rx_skb);
228 
229  free_netdev(eoe->dev);
230 
232 }
233 
234 /*****************************************************************************/
235 
239 {
240  ec_eoe_frame_t *frame, *next;
241 
242  down(&eoe->tx_queue_sem);
243 
244  list_for_each_entry_safe(frame, next, &eoe->tx_queue, queue) {
245  list_del(&frame->queue);
246  dev_kfree_skb(frame->skb);
247  kfree(frame);
248  }
249  eoe->tx_queued_frames = 0;
250 
251  up(&eoe->tx_queue_sem);
252 }
253 
254 /*****************************************************************************/
255 
261 {
262  size_t remaining_size, current_size, complete_offset;
263  unsigned int last_fragment;
264  uint8_t *data;
265 #if EOE_DEBUG_LEVEL >= 3
266  unsigned int i;
267 #endif
268 
269  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
270 
271  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
272  current_size = remaining_size;
273  last_fragment = 1;
274  } else {
275  current_size = ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
276  last_fragment = 0;
277  }
278 
279  if (eoe->tx_fragment_number) {
280  complete_offset = eoe->tx_offset / 32;
281  }
282  else {
283  // complete size in 32 bit blocks, rounded up.
284  complete_offset = remaining_size / 32 + 1;
285  }
286 
287 #if EOE_DEBUG_LEVEL >= 2
288  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX sending fragment %u%s"
289  " with %zu octets (%zu). %u frames queued.\n",
290  eoe->dev->name, eoe->tx_fragment_number,
291  last_fragment ? "" : "+", current_size, complete_offset,
292  eoe->tx_queued_frames);
293 #endif
294 
295 #if EOE_DEBUG_LEVEL >= 3
296  EC_SLAVE_DBG(eoe->slave, 0, "");
297  for (i = 0; i < current_size; i++) {
298  printk(KERN_CONT "%02X ",
299  eoe->tx_frame->skb->data[eoe->tx_offset + i]);
300  if ((i + 1) % 16 == 0) {
301  printk(KERN_CONT "\n");
302  EC_SLAVE_DBG(eoe->slave, 0, "");
303  }
304  }
305  printk(KERN_CONT "\n");
306 #endif
307 
308  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
309  0x02, current_size + 4);
310  if (IS_ERR(data))
311  return PTR_ERR(data);
312 
313  EC_WRITE_U8 (data, 0x00); // eoe fragment req.
314  EC_WRITE_U8 (data + 1, last_fragment);
315  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
316  (complete_offset & 0x3F) << 6 |
317  (eoe->tx_frame_number & 0x0F) << 12));
318 
319  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
320  eoe->queue_datagram = 1;
321 
322  eoe->tx_offset += current_size;
323  eoe->tx_fragment_number++;
324  return 0;
325 }
326 
327 /*****************************************************************************/
328 
331 void ec_eoe_run(ec_eoe_t *eoe )
332 {
333  if (!eoe->opened)
334  return;
335 
336  // if the datagram was not sent, or is not yet received, skip this cycle
337  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
338  return;
339 
340  // call state function
341  eoe->state(eoe);
342 
343  // update statistics
344  if (jiffies - eoe->rate_jiffies > HZ) {
345  eoe->rx_rate = eoe->rx_counter;
346  eoe->tx_rate = eoe->tx_counter;
347  eoe->rx_counter = 0;
348  eoe->tx_counter = 0;
349  eoe->rate_jiffies = jiffies;
350  }
351 
353 }
354 
355 /*****************************************************************************/
356 
360 {
361  if (eoe->queue_datagram) {
363  eoe->queue_datagram = 0;
364  }
365 }
366 
367 /*****************************************************************************/
368 
373 int ec_eoe_is_open(const ec_eoe_t *eoe )
374 {
375  return eoe->opened;
376 }
377 
378 /*****************************************************************************/
379 
385 int ec_eoe_is_idle(const ec_eoe_t *eoe )
386 {
387  return eoe->rx_idle && eoe->tx_idle;
388 }
389 
390 /******************************************************************************
391  * STATE PROCESSING FUNCTIONS
392  *****************************************************************************/
393 
402 {
403  if (eoe->slave->error_flag ||
405  eoe->rx_idle = 1;
406  eoe->tx_idle = 1;
407  return;
408  }
409 
411  eoe->queue_datagram = 1;
413 }
414 
415 /*****************************************************************************/
416 
423 {
424  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
425  eoe->stats.rx_errors++;
426 #if EOE_DEBUG_LEVEL >= 1
427  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
428  " check datagram for %s.\n", eoe->dev->name);
429 #endif
431  return;
432  }
433 
434  if (!ec_slave_mbox_check(&eoe->datagram)) {
435  eoe->rx_idle = 1;
437  return;
438  }
439 
440  eoe->rx_idle = 0;
442  eoe->queue_datagram = 1;
444 }
445 
446 /*****************************************************************************/
447 
454 {
455  size_t rec_size, data_size;
456  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
457  uint8_t fragment_offset, fragment_number;
458 #if EOE_DEBUG_LEVEL >= 2
459  uint8_t frame_number;
460 #endif
461  off_t offset;
462 #if EOE_DEBUG_LEVEL >= 3
463  unsigned int i;
464 #endif
465 
466  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
467  eoe->stats.rx_errors++;
468 #if EOE_DEBUG_LEVEL >= 1
469  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
470  " fetch datagram for %s.\n", eoe->dev->name);
471 #endif
473  return;
474  }
475 
476  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
477  &mbox_prot, &rec_size);
478  if (IS_ERR(data)) {
479  eoe->stats.rx_errors++;
480 #if EOE_DEBUG_LEVEL >= 1
481  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
482  eoe->dev->name);
483 #endif
485  return;
486  }
487 
488  if (mbox_prot != 0x02) { // EoE FIXME mailbox handler necessary
489  eoe->stats.rx_errors++;
490 #if EOE_DEBUG_LEVEL >= 1
491  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
492  eoe->dev->name);
493 #endif
495  return;
496  }
497 
498  frame_type = EC_READ_U16(data) & 0x000F;
499 
500  if (frame_type != 0x00) {
501 #if EOE_DEBUG_LEVEL >= 1
502  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
503  " Dropping.\n", eoe->dev->name);
504 #endif
505  eoe->stats.rx_dropped++;
507  return;
508  }
509 
510  // EoE Fragment Request received
511 
512  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
513  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
514  fragment_number = EC_READ_U16(data + 2) & 0x003F;
515  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
516 #if EOE_DEBUG_LEVEL >= 2
517  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
518 #endif
519 
520 #if EOE_DEBUG_LEVEL >= 2
521  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
522  " frame %u%s, %u octets\n", eoe->dev->name, fragment_number,
523  last_fragment ? "" : "+", fragment_offset, frame_number,
524  time_appended ? ", + timestamp" : "",
525  time_appended ? rec_size - 8 : rec_size - 4);
526 #endif
527 
528 #if EOE_DEBUG_LEVEL >= 3
529  EC_SLAVE_DBG(eoe->slave, 0, "");
530  for (i = 0; i < rec_size - 4; i++) {
531  printk(KERN_CONT "%02X ", data[i + 4]);
532  if ((i + 1) % 16 == 0) {
533  printk(KERN_CONT "\n");
534  EC_SLAVE_DBG(eoe->slave, 0, "");
535  }
536  }
537  printk(KERN_CONT "\n");
538 #endif
539 
540  data_size = time_appended ? rec_size - 8 : rec_size - 4;
541 
542  if (!fragment_number) {
543  if (eoe->rx_skb) {
544  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
545  dev_kfree_skb(eoe->rx_skb);
546  }
547 
548  // new socket buffer
549  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
550  if (printk_ratelimit())
551  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
552  " frame dropped.\n");
553  eoe->stats.rx_dropped++;
555  return;
556  }
557 
558  eoe->rx_skb_offset = 0;
559  eoe->rx_skb_size = fragment_offset * 32;
560  eoe->rx_expected_fragment = 0;
561  }
562  else {
563  if (!eoe->rx_skb) {
564  eoe->stats.rx_dropped++;
566  return;
567  }
568 
569  offset = fragment_offset * 32;
570  if (offset != eoe->rx_skb_offset ||
571  offset + data_size > eoe->rx_skb_size ||
572  fragment_number != eoe->rx_expected_fragment) {
573  dev_kfree_skb(eoe->rx_skb);
574  eoe->rx_skb = NULL;
575  eoe->stats.rx_errors++;
576 #if EOE_DEBUG_LEVEL >= 1
577  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
578  eoe->dev->name);
579 #endif
581  return;
582  }
583  }
584 
585  // copy fragment into socket buffer
586  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
587  eoe->rx_skb_offset += data_size;
588 
589  if (last_fragment) {
590  // update statistics
591  eoe->stats.rx_packets++;
592  eoe->stats.rx_bytes += eoe->rx_skb->len;
593  eoe->rx_counter += eoe->rx_skb->len;
594 
595 #if EOE_DEBUG_LEVEL >= 2
596  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
597  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
598 #endif
599 
600  // pass socket buffer to network stack
601  eoe->rx_skb->dev = eoe->dev;
602  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
603  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
604  if (netif_rx(eoe->rx_skb)) {
605  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
606  }
607  eoe->rx_skb = NULL;
608 
610  }
611  else {
612  eoe->rx_expected_fragment++;
613 #if EOE_DEBUG_LEVEL >= 2
614  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
615  eoe->dev->name, eoe->rx_expected_fragment);
616 #endif
618  }
619 }
620 
621 /*****************************************************************************/
622 
631 {
632 #if EOE_DEBUG_LEVEL >= 2
633  unsigned int wakeup = 0;
634 #endif
635 
636  if (eoe->slave->error_flag ||
638  eoe->rx_idle = 1;
639  eoe->tx_idle = 1;
640  return;
641  }
642 
643  down(&eoe->tx_queue_sem);
644 
645  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
646  up(&eoe->tx_queue_sem);
647  eoe->tx_idle = 1;
648  // no data available.
649  // start a new receive immediately.
651  return;
652  }
653 
654  // take the first frame out of the queue
655  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
656  list_del(&eoe->tx_frame->queue);
657  if (!eoe->tx_queue_active &&
658  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
659  netif_wake_queue(eoe->dev);
660  eoe->tx_queue_active = 1;
661 #if EOE_DEBUG_LEVEL >= 2
662  wakeup = 1;
663 #endif
664  }
665 
666  eoe->tx_queued_frames--;
667  up(&eoe->tx_queue_sem);
668 
669  eoe->tx_idle = 0;
670 
671  eoe->tx_frame_number++;
672  eoe->tx_frame_number %= 16;
673  eoe->tx_fragment_number = 0;
674  eoe->tx_offset = 0;
675 
676  if (ec_eoe_send(eoe)) {
677  dev_kfree_skb(eoe->tx_frame->skb);
678  kfree(eoe->tx_frame);
679  eoe->tx_frame = NULL;
680  eoe->stats.tx_errors++;
682 #if EOE_DEBUG_LEVEL >= 1
683  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
684 #endif
685  return;
686  }
687 
688 #if EOE_DEBUG_LEVEL >= 2
689  if (wakeup)
690  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
691  eoe->dev->name);
692 #endif
693 
694  eoe->tries = EC_EOE_TRIES;
696 }
697 
698 /*****************************************************************************/
699 
706 {
707  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
708  if (eoe->tries) {
709  eoe->tries--; // try again
710  eoe->queue_datagram = 1;
711  } else {
712  eoe->stats.tx_errors++;
713 #if EOE_DEBUG_LEVEL >= 1
714  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
715  " datagram for %s after %u tries.\n",
716  eoe->dev->name, EC_EOE_TRIES);
717 #endif
719  }
720  return;
721  }
722 
723  if (eoe->datagram.working_counter != 1) {
724  if (eoe->tries) {
725  eoe->tries--; // try again
726  eoe->queue_datagram = 1;
727  } else {
728  eoe->stats.tx_errors++;
729 #if EOE_DEBUG_LEVEL >= 1
730  EC_SLAVE_WARN(eoe->slave, "No sending response"
731  " for %s after %u tries.\n",
732  eoe->dev->name, EC_EOE_TRIES);
733 #endif
735  }
736  return;
737  }
738 
739  // frame completely sent
740  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
741  eoe->stats.tx_packets++;
742  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
743  eoe->tx_counter += eoe->tx_frame->skb->len;
744  dev_kfree_skb(eoe->tx_frame->skb);
745  kfree(eoe->tx_frame);
746  eoe->tx_frame = NULL;
748  }
749  else { // send next fragment
750  if (ec_eoe_send(eoe)) {
751  dev_kfree_skb(eoe->tx_frame->skb);
752  kfree(eoe->tx_frame);
753  eoe->tx_frame = NULL;
754  eoe->stats.tx_errors++;
755 #if EOE_DEBUG_LEVEL >= 1
756  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
757 #endif
759  }
760  }
761 }
762 
763 /******************************************************************************
764  * NET_DEVICE functions
765  *****************************************************************************/
766 
771 int ec_eoedev_open(struct net_device *dev )
772 {
773  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
774  ec_eoe_flush(eoe);
775  eoe->opened = 1;
776  eoe->rx_idle = 0;
777  eoe->tx_idle = 0;
778  netif_start_queue(dev);
779  eoe->tx_queue_active = 1;
780 #if EOE_DEBUG_LEVEL >= 2
781  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
782 #endif
784  return 0;
785 }
786 
787 /*****************************************************************************/
788 
793 int ec_eoedev_stop(struct net_device *dev )
794 {
795  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
796  netif_stop_queue(dev);
797  eoe->rx_idle = 1;
798  eoe->tx_idle = 1;
799  eoe->tx_queue_active = 0;
800  eoe->opened = 0;
801  ec_eoe_flush(eoe);
802 #if EOE_DEBUG_LEVEL >= 2
803  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
804 #endif
806  return 0;
807 }
808 
809 /*****************************************************************************/
810 
815 int ec_eoedev_tx(struct sk_buff *skb,
816  struct net_device *dev
817  )
818 {
819  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
820  ec_eoe_frame_t *frame;
821 
822 #if 0
823  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
824  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
825  " exceeds MTU. dropping.\n", skb->len);
826  dev_kfree_skb(skb);
827  eoe->stats.tx_dropped++;
828  return 0;
829  }
830 #endif
831 
832  if (!(frame =
833  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
834  if (printk_ratelimit())
835  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
836  return 1;
837  }
838 
839  frame->skb = skb;
840 
841  down(&eoe->tx_queue_sem);
842  list_add_tail(&frame->queue, &eoe->tx_queue);
843  eoe->tx_queued_frames++;
844  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
845  netif_stop_queue(dev);
846  eoe->tx_queue_active = 0;
847  }
848  up(&eoe->tx_queue_sem);
849 
850 #if EOE_DEBUG_LEVEL >= 2
851  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
852  " with %u octets (%u frames queued).\n",
853  eoe->dev->name, skb->len, eoe->tx_queued_frames);
854  if (!eoe->tx_queue_active)
855  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
856 #endif
857 
858  return 0;
859 }
860 
861 /*****************************************************************************/
862 
867 struct net_device_stats *ec_eoedev_stats(
868  struct net_device *dev
869  )
870 {
871  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
872  return &eoe->stats;
873 }
874 
875 /*****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:359
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:815
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:453
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:260
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:630
#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:2265
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
Definition: ethernet.c:422
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:238
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
EtherCAT master structure.
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
Definition: ethernet.c:401
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:385
#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:2282
Main device.
Definition: globals.h:190
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:705
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:2177
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:82
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:771
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:331
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:793
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:214
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:867
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:373
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