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(slave, 0, "EoE %s TX sending fragment %u%s"
289  " with %u octets (%u). %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(master, 0, "");
297  for (i = 0; i < current_size; i++) {
298  printk("%02X ", eoe->tx_frame->skb->data[eoe->tx_offset + i]);
299  if ((i + 1) % 16 == 0) {
300  printk("\n");
301  EC_SLAVE_DBG(master, 0, "");
302  }
303  }
304  printk("\n");
305 #endif
306 
307  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
308  0x02, current_size + 4);
309  if (IS_ERR(data))
310  return PTR_ERR(data);
311 
312  EC_WRITE_U8 (data, 0x00); // eoe fragment req.
313  EC_WRITE_U8 (data + 1, last_fragment);
314  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
315  (complete_offset & 0x3F) << 6 |
316  (eoe->tx_frame_number & 0x0F) << 12));
317 
318  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
319  eoe->queue_datagram = 1;
320 
321  eoe->tx_offset += current_size;
322  eoe->tx_fragment_number++;
323  return 0;
324 }
325 
326 /*****************************************************************************/
327 
330 void ec_eoe_run(ec_eoe_t *eoe )
331 {
332  if (!eoe->opened)
333  return;
334 
335  // if the datagram was not sent, or is not yet received, skip this cycle
336  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
337  return;
338 
339  // call state function
340  eoe->state(eoe);
341 
342  // update statistics
343  if (jiffies - eoe->rate_jiffies > HZ) {
344  eoe->rx_rate = eoe->rx_counter;
345  eoe->tx_rate = eoe->tx_counter;
346  eoe->rx_counter = 0;
347  eoe->tx_counter = 0;
348  eoe->rate_jiffies = jiffies;
349  }
350 
352 }
353 
354 /*****************************************************************************/
355 
359 {
360  if (eoe->queue_datagram) {
362  eoe->queue_datagram = 0;
363  }
364 }
365 
366 /*****************************************************************************/
367 
372 int ec_eoe_is_open(const ec_eoe_t *eoe )
373 {
374  return eoe->opened;
375 }
376 
377 /*****************************************************************************/
378 
384 int ec_eoe_is_idle(const ec_eoe_t *eoe )
385 {
386  return eoe->rx_idle && eoe->tx_idle;
387 }
388 
389 /******************************************************************************
390  * STATE PROCESSING FUNCTIONS
391  *****************************************************************************/
392 
401 {
402  if (eoe->slave->error_flag ||
404  eoe->rx_idle = 1;
405  eoe->tx_idle = 1;
406  return;
407  }
408 
410  eoe->queue_datagram = 1;
412 }
413 
414 /*****************************************************************************/
415 
422 {
423  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
424  eoe->stats.rx_errors++;
425 #if EOE_DEBUG_LEVEL >= 1
426  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
427  " check datagram for %s.\n", eoe->dev->name);
428 #endif
430  return;
431  }
432 
433  if (!ec_slave_mbox_check(&eoe->datagram)) {
434  eoe->rx_idle = 1;
436  return;
437  }
438 
439  eoe->rx_idle = 0;
441  eoe->queue_datagram = 1;
443 }
444 
445 /*****************************************************************************/
446 
453 {
454  size_t rec_size, data_size;
455  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
456  uint8_t fragment_offset, fragment_number;
457 #if EOE_DEBUG_LEVEL >= 2
458  uint8_t frame_number;
459 #endif
460  off_t offset;
461 #if EOE_DEBUG_LEVEL >= 3
462  unsigned int i;
463 #endif
464 
465  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
466  eoe->stats.rx_errors++;
467 #if EOE_DEBUG_LEVEL >= 1
468  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
469  " fetch datagram for %s.\n", eoe->dev->name);
470 #endif
472  return;
473  }
474 
475  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
476  &mbox_prot, &rec_size);
477  if (IS_ERR(data)) {
478  eoe->stats.rx_errors++;
479 #if EOE_DEBUG_LEVEL >= 1
480  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
481  eoe->dev->name);
482 #endif
484  return;
485  }
486 
487  if (mbox_prot != 0x02) { // EoE FIXME mailbox handler necessary
488  eoe->stats.rx_errors++;
489 #if EOE_DEBUG_LEVEL >= 1
490  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
491  eoe->dev->name);
492 #endif
494  return;
495  }
496 
497  frame_type = EC_READ_U16(data) & 0x000F;
498 
499  if (frame_type != 0x00) {
500 #if EOE_DEBUG_LEVEL >= 1
501  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
502  " Dropping.\n", eoe->dev->name);
503 #endif
504  eoe->stats.rx_dropped++;
506  return;
507  }
508 
509  // EoE Fragment Request received
510 
511  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
512  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
513  fragment_number = EC_READ_U16(data + 2) & 0x003F;
514  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
515 #if EOE_DEBUG_LEVEL >= 2
516  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
517 #endif
518 
519 #if EOE_DEBUG_LEVEL >= 2
520  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
521  " frame %u%s, %u octets\n", eoe->dev->name, fragment_number,
522  last_fragment ? "" : "+", fragment_offset, frame_number,
523  time_appended ? ", + timestamp" : "",
524  time_appended ? rec_size - 8 : rec_size - 4);
525 #endif
526 
527 #if EOE_DEBUG_LEVEL >= 3
528  EC_SLAVE_DBG(eoe->slave, 0, "");
529  for (i = 0; i < rec_size - 4; i++) {
530  printk("%02X ", data[i + 4]);
531  if ((i + 1) % 16 == 0) {
532  printk("\n");
533  EC_SLAVE_DBG(eoe->slave, 0, "");
534  }
535  }
536  printk("\n");
537 #endif
538 
539  data_size = time_appended ? rec_size - 8 : rec_size - 4;
540 
541  if (!fragment_number) {
542  if (eoe->rx_skb) {
543  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
544  dev_kfree_skb(eoe->rx_skb);
545  }
546 
547  // new socket buffer
548  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
549  if (printk_ratelimit())
550  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
551  " frame dropped.\n");
552  eoe->stats.rx_dropped++;
554  return;
555  }
556 
557  eoe->rx_skb_offset = 0;
558  eoe->rx_skb_size = fragment_offset * 32;
559  eoe->rx_expected_fragment = 0;
560  }
561  else {
562  if (!eoe->rx_skb) {
563  eoe->stats.rx_dropped++;
565  return;
566  }
567 
568  offset = fragment_offset * 32;
569  if (offset != eoe->rx_skb_offset ||
570  offset + data_size > eoe->rx_skb_size ||
571  fragment_number != eoe->rx_expected_fragment) {
572  dev_kfree_skb(eoe->rx_skb);
573  eoe->rx_skb = NULL;
574  eoe->stats.rx_errors++;
575 #if EOE_DEBUG_LEVEL >= 1
576  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
577  eoe->dev->name);
578 #endif
580  return;
581  }
582  }
583 
584  // copy fragment into socket buffer
585  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
586  eoe->rx_skb_offset += data_size;
587 
588  if (last_fragment) {
589  // update statistics
590  eoe->stats.rx_packets++;
591  eoe->stats.rx_bytes += eoe->rx_skb->len;
592  eoe->rx_counter += eoe->rx_skb->len;
593 
594 #if EOE_DEBUG_LEVEL >= 2
595  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
596  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
597 #endif
598 
599  // pass socket buffer to network stack
600  eoe->rx_skb->dev = eoe->dev;
601  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
602  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
603  if (netif_rx(eoe->rx_skb)) {
604  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
605  }
606  eoe->rx_skb = NULL;
607 
609  }
610  else {
611  eoe->rx_expected_fragment++;
612 #if EOE_DEBUG_LEVEL >= 2
613  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
614  eoe->dev->name, eoe->rx_expected_fragment);
615 #endif
617  }
618 }
619 
620 /*****************************************************************************/
621 
630 {
631 #if EOE_DEBUG_LEVEL >= 2
632  unsigned int wakeup = 0;
633 #endif
634 
635  if (eoe->slave->error_flag ||
637  eoe->rx_idle = 1;
638  eoe->tx_idle = 1;
639  return;
640  }
641 
642  down(&eoe->tx_queue_sem);
643 
644  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
645  up(&eoe->tx_queue_sem);
646  eoe->tx_idle = 1;
647  // no data available.
648  // start a new receive immediately.
650  return;
651  }
652 
653  // take the first frame out of the queue
654  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
655  list_del(&eoe->tx_frame->queue);
656  if (!eoe->tx_queue_active &&
657  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
658  netif_wake_queue(eoe->dev);
659  eoe->tx_queue_active = 1;
660 #if EOE_DEBUG_LEVEL >= 2
661  wakeup = 1;
662 #endif
663  }
664 
665  eoe->tx_queued_frames--;
666  up(&eoe->tx_queue_sem);
667 
668  eoe->tx_idle = 0;
669 
670  eoe->tx_frame_number++;
671  eoe->tx_frame_number %= 16;
672  eoe->tx_fragment_number = 0;
673  eoe->tx_offset = 0;
674 
675  if (ec_eoe_send(eoe)) {
676  dev_kfree_skb(eoe->tx_frame->skb);
677  kfree(eoe->tx_frame);
678  eoe->tx_frame = NULL;
679  eoe->stats.tx_errors++;
681 #if EOE_DEBUG_LEVEL >= 1
682  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
683 #endif
684  return;
685  }
686 
687 #if EOE_DEBUG_LEVEL >= 2
688  if (wakeup)
689  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
690  eoe->dev->name);
691 #endif
692 
693  eoe->tries = EC_EOE_TRIES;
695 }
696 
697 /*****************************************************************************/
698 
705 {
706  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
707  if (eoe->tries) {
708  eoe->tries--; // try again
709  eoe->queue_datagram = 1;
710  } else {
711  eoe->stats.tx_errors++;
712 #if EOE_DEBUG_LEVEL >= 1
713  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
714  " datagram for %s after %u tries.\n",
715  eoe->dev->name, EC_EOE_TRIES);
716 #endif
718  }
719  return;
720  }
721 
722  if (eoe->datagram.working_counter != 1) {
723  if (eoe->tries) {
724  eoe->tries--; // try again
725  eoe->queue_datagram = 1;
726  } else {
727  eoe->stats.tx_errors++;
728 #if EOE_DEBUG_LEVEL >= 1
729  EC_SLAVE_WARN(eoe->slave, "No sending response"
730  " for %s after %u tries.\n",
731  eoe->dev->name, EC_EOE_TRIES);
732 #endif
734  }
735  return;
736  }
737 
738  // frame completely sent
739  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
740  eoe->stats.tx_packets++;
741  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
742  eoe->tx_counter += eoe->tx_frame->skb->len;
743  dev_kfree_skb(eoe->tx_frame->skb);
744  kfree(eoe->tx_frame);
745  eoe->tx_frame = NULL;
747  }
748  else { // send next fragment
749  if (ec_eoe_send(eoe)) {
750  dev_kfree_skb(eoe->tx_frame->skb);
751  kfree(eoe->tx_frame);
752  eoe->tx_frame = NULL;
753  eoe->stats.tx_errors++;
754 #if EOE_DEBUG_LEVEL >= 1
755  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
756 #endif
758  }
759  }
760 }
761 
762 /******************************************************************************
763  * NET_DEVICE functions
764  *****************************************************************************/
765 
770 int ec_eoedev_open(struct net_device *dev )
771 {
772  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
773  ec_eoe_flush(eoe);
774  eoe->opened = 1;
775  eoe->rx_idle = 0;
776  eoe->tx_idle = 0;
777  netif_start_queue(dev);
778  eoe->tx_queue_active = 1;
779 #if EOE_DEBUG_LEVEL >= 2
780  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
781 #endif
783  return 0;
784 }
785 
786 /*****************************************************************************/
787 
792 int ec_eoedev_stop(struct net_device *dev )
793 {
794  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
795  netif_stop_queue(dev);
796  eoe->rx_idle = 1;
797  eoe->tx_idle = 1;
798  eoe->tx_queue_active = 0;
799  eoe->opened = 0;
800  ec_eoe_flush(eoe);
801 #if EOE_DEBUG_LEVEL >= 2
802  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
803 #endif
805  return 0;
806 }
807 
808 /*****************************************************************************/
809 
814 int ec_eoedev_tx(struct sk_buff *skb,
815  struct net_device *dev
816  )
817 {
818  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
819  ec_eoe_frame_t *frame;
820 
821 #if 0
822  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
823  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
824  " exceeds MTU. dropping.\n", skb->len);
825  dev_kfree_skb(skb);
826  eoe->stats.tx_dropped++;
827  return 0;
828  }
829 #endif
830 
831  if (!(frame =
832  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
833  if (printk_ratelimit())
834  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
835  return 1;
836  }
837 
838  frame->skb = skb;
839 
840  down(&eoe->tx_queue_sem);
841  list_add_tail(&frame->queue, &eoe->tx_queue);
842  eoe->tx_queued_frames++;
843  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
844  netif_stop_queue(dev);
845  eoe->tx_queue_active = 0;
846  }
847  up(&eoe->tx_queue_sem);
848 
849 #if EOE_DEBUG_LEVEL >= 2
850  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
851  " with %u octets (%u frames queued).\n",
852  eoe->dev->name, skb->len, eoe->tx_queued_frames);
853  if (!eoe->tx_queue_active)
854  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
855 #endif
856 
857  return 0;
858 }
859 
860 /*****************************************************************************/
861 
866 struct net_device_stats *ec_eoedev_stats(
867  struct net_device *dev
868  )
869 {
870  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
871  return &eoe->stats;
872 }
873 
874 /*****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:358
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:116
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:452
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:969
OP (mailbox communication and input/output update)
Definition: globals.h:138
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:629
#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:2187
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
Definition: ethernet.c:421
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:619
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:400
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:384
#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:2204
Main device.
Definition: globals.h:202
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:814
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:704
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:2135
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:770
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:330
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:792
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:132
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:866
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:372
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