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 (!(eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup))) {
152  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
153  " for EoE handler!\n", name);
154  ret = -ENODEV;
155  goto out_return;
156  }
157 
158  // initialize net_device
159 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
160  eoe->dev->netdev_ops = &ec_eoedev_ops;
161 #else
162  eoe->dev->open = ec_eoedev_open;
163  eoe->dev->stop = ec_eoedev_stop;
164  eoe->dev->hard_start_xmit = ec_eoedev_tx;
165  eoe->dev->get_stats = ec_eoedev_stats;
166 #endif
167 
168  for (i = 0; i < ETH_ALEN; i++)
169  eoe->dev->dev_addr[i] = i | (i << 4);
170 
171  // initialize private data
172  priv = netdev_priv(eoe->dev);
173  *priv = eoe;
174 
175  // Usually setting the MTU appropriately makes the upper layers
176  // do the frame fragmenting. In some cases this doesn't work
177  // so the MTU is left on the Ethernet standard value and fragmenting
178  // is done "manually".
179 #if 0
180  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
181 #endif
182 
183  // connect the net_device to the kernel
184  ret = register_netdev(eoe->dev);
185  if (ret) {
186  EC_SLAVE_ERR(slave, "Unable to register net_device:"
187  " error %i\n", ret);
188  goto out_free;
189  }
190 
191  // make the last address octet unique
192  eoe->dev->dev_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
193  return 0;
194 
195  out_free:
196  free_netdev(eoe->dev);
197  eoe->dev = NULL;
198  out_return:
199  return ret;
200 }
201 
202 /*****************************************************************************/
203 
209 {
210  unregister_netdev(eoe->dev); // possibly calls close callback
211 
212  // empty transmit queue
213  ec_eoe_flush(eoe);
214 
215  if (eoe->tx_frame) {
216  dev_kfree_skb(eoe->tx_frame->skb);
217  kfree(eoe->tx_frame);
218  }
219 
220  if (eoe->rx_skb)
221  dev_kfree_skb(eoe->rx_skb);
222 
223  free_netdev(eoe->dev);
224 
226 }
227 
228 /*****************************************************************************/
229 
233 {
234  ec_eoe_frame_t *frame, *next;
235 
236  down(&eoe->tx_queue_sem);
237 
238  list_for_each_entry_safe(frame, next, &eoe->tx_queue, queue) {
239  list_del(&frame->queue);
240  dev_kfree_skb(frame->skb);
241  kfree(frame);
242  }
243  eoe->tx_queued_frames = 0;
244 
245  up(&eoe->tx_queue_sem);
246 }
247 
248 /*****************************************************************************/
249 
255 {
256  size_t remaining_size, current_size, complete_offset;
257  unsigned int last_fragment;
258  uint8_t *data;
259 #if EOE_DEBUG_LEVEL >= 3
260  unsigned int i;
261 #endif
262 
263  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
264 
265  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
266  current_size = remaining_size;
267  last_fragment = 1;
268  } else {
269  current_size = ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
270  last_fragment = 0;
271  }
272 
273  if (eoe->tx_fragment_number) {
274  complete_offset = eoe->tx_offset / 32;
275  }
276  else {
277  // complete size in 32 bit blocks, rounded up.
278  complete_offset = remaining_size / 32 + 1;
279  }
280 
281 #if EOE_DEBUG_LEVEL >= 2
282  EC_SLAVE_DBG(slave, 0, "EoE %s TX sending fragment %u%s"
283  " with %u octets (%u). %u frames queued.\n",
284  eoe->dev->name, eoe->tx_fragment_number,
285  last_fragment ? "" : "+", current_size, complete_offset,
286  eoe->tx_queued_frames);
287 #endif
288 
289 #if EOE_DEBUG_LEVEL >= 3
290  EC_SLAVE_DBG(master, 0, "");
291  for (i = 0; i < current_size; i++) {
292  printk("%02X ", eoe->tx_frame->skb->data[eoe->tx_offset + i]);
293  if ((i + 1) % 16 == 0) {
294  printk("\n");
295  EC_SLAVE_DBG(master, 0, "");
296  }
297  }
298  printk("\n");
299 #endif
300 
301  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
302  0x02, current_size + 4);
303  if (IS_ERR(data))
304  return PTR_ERR(data);
305 
306  EC_WRITE_U8 (data, 0x00); // eoe fragment req.
307  EC_WRITE_U8 (data + 1, last_fragment);
308  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
309  (complete_offset & 0x3F) << 6 |
310  (eoe->tx_frame_number & 0x0F) << 12));
311 
312  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
313  eoe->queue_datagram = 1;
314 
315  eoe->tx_offset += current_size;
316  eoe->tx_fragment_number++;
317  return 0;
318 }
319 
320 /*****************************************************************************/
321 
324 void ec_eoe_run(ec_eoe_t *eoe )
325 {
326  if (!eoe->opened)
327  return;
328 
329  // if the datagram was not sent, or is not yet received, skip this cycle
330  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
331  return;
332 
333  // call state function
334  eoe->state(eoe);
335 
336  // update statistics
337  if (jiffies - eoe->rate_jiffies > HZ) {
338  eoe->rx_rate = eoe->rx_counter;
339  eoe->tx_rate = eoe->tx_counter;
340  eoe->rx_counter = 0;
341  eoe->tx_counter = 0;
342  eoe->rate_jiffies = jiffies;
343  }
344 
346 }
347 
348 /*****************************************************************************/
349 
353 {
354  if (eoe->queue_datagram) {
356  eoe->queue_datagram = 0;
357  }
358 }
359 
360 /*****************************************************************************/
361 
366 int ec_eoe_is_open(const ec_eoe_t *eoe )
367 {
368  return eoe->opened;
369 }
370 
371 /*****************************************************************************/
372 
378 int ec_eoe_is_idle(const ec_eoe_t *eoe )
379 {
380  return eoe->rx_idle && eoe->tx_idle;
381 }
382 
383 /******************************************************************************
384  * STATE PROCESSING FUNCTIONS
385  *****************************************************************************/
386 
395 {
396  if (eoe->slave->error_flag ||
398  eoe->rx_idle = 1;
399  eoe->tx_idle = 1;
400  return;
401  }
402 
404  eoe->queue_datagram = 1;
406 }
407 
408 /*****************************************************************************/
409 
416 {
417  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
418  eoe->stats.rx_errors++;
419 #if EOE_DEBUG_LEVEL >= 1
420  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
421  " check datagram for %s.\n", eoe->dev->name);
422 #endif
424  return;
425  }
426 
427  if (!ec_slave_mbox_check(&eoe->datagram)) {
428  eoe->rx_idle = 1;
430  return;
431  }
432 
433  eoe->rx_idle = 0;
435  eoe->queue_datagram = 1;
437 }
438 
439 /*****************************************************************************/
440 
447 {
448  size_t rec_size, data_size;
449  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
450  uint8_t fragment_offset, fragment_number;
451 #if EOE_DEBUG_LEVEL >= 2
452  uint8_t frame_number;
453 #endif
454  off_t offset;
455 #if EOE_DEBUG_LEVEL >= 3
456  unsigned int i;
457 #endif
458 
459  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
460  eoe->stats.rx_errors++;
461 #if EOE_DEBUG_LEVEL >= 1
462  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
463  " fetch datagram for %s.\n", eoe->dev->name);
464 #endif
466  return;
467  }
468 
469  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
470  &mbox_prot, &rec_size);
471  if (IS_ERR(data)) {
472  eoe->stats.rx_errors++;
473 #if EOE_DEBUG_LEVEL >= 1
474  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
475  eoe->dev->name);
476 #endif
478  return;
479  }
480 
481  if (mbox_prot != 0x02) { // EoE FIXME mailbox handler necessary
482  eoe->stats.rx_errors++;
483 #if EOE_DEBUG_LEVEL >= 1
484  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
485  eoe->dev->name);
486 #endif
488  return;
489  }
490 
491  frame_type = EC_READ_U16(data) & 0x000F;
492 
493  if (frame_type != 0x00) {
494 #if EOE_DEBUG_LEVEL >= 1
495  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
496  " Dropping.\n", eoe->dev->name);
497 #endif
498  eoe->stats.rx_dropped++;
500  return;
501  }
502 
503  // EoE Fragment Request received
504 
505  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
506  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
507  fragment_number = EC_READ_U16(data + 2) & 0x003F;
508  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
509 #if EOE_DEBUG_LEVEL >= 2
510  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
511 #endif
512 
513 #if EOE_DEBUG_LEVEL >= 2
514  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
515  " frame %u%s, %u octets\n", eoe->dev->name, fragment_number,
516  last_fragment ? "" : "+", fragment_offset, frame_number,
517  time_appended ? ", + timestamp" : "",
518  time_appended ? rec_size - 8 : rec_size - 4);
519 #endif
520 
521 #if EOE_DEBUG_LEVEL >= 3
522  EC_SLAVE_DBG(eoe->slave, 0, "");
523  for (i = 0; i < rec_size - 4; i++) {
524  printk("%02X ", data[i + 4]);
525  if ((i + 1) % 16 == 0) {
526  printk("\n");
527  EC_SLAVE_DBG(eoe->slave, 0, "");
528  }
529  }
530  printk("\n");
531 #endif
532 
533  data_size = time_appended ? rec_size - 8 : rec_size - 4;
534 
535  if (!fragment_number) {
536  if (eoe->rx_skb) {
537  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
538  dev_kfree_skb(eoe->rx_skb);
539  }
540 
541  // new socket buffer
542  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
543  if (printk_ratelimit())
544  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
545  " frame dropped.\n");
546  eoe->stats.rx_dropped++;
548  return;
549  }
550 
551  eoe->rx_skb_offset = 0;
552  eoe->rx_skb_size = fragment_offset * 32;
553  eoe->rx_expected_fragment = 0;
554  }
555  else {
556  if (!eoe->rx_skb) {
557  eoe->stats.rx_dropped++;
559  return;
560  }
561 
562  offset = fragment_offset * 32;
563  if (offset != eoe->rx_skb_offset ||
564  offset + data_size > eoe->rx_skb_size ||
565  fragment_number != eoe->rx_expected_fragment) {
566  dev_kfree_skb(eoe->rx_skb);
567  eoe->rx_skb = NULL;
568  eoe->stats.rx_errors++;
569 #if EOE_DEBUG_LEVEL >= 1
570  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
571  eoe->dev->name);
572 #endif
574  return;
575  }
576  }
577 
578  // copy fragment into socket buffer
579  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
580  eoe->rx_skb_offset += data_size;
581 
582  if (last_fragment) {
583  // update statistics
584  eoe->stats.rx_packets++;
585  eoe->stats.rx_bytes += eoe->rx_skb->len;
586  eoe->rx_counter += eoe->rx_skb->len;
587 
588 #if EOE_DEBUG_LEVEL >= 2
589  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
590  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
591 #endif
592 
593  // pass socket buffer to network stack
594  eoe->rx_skb->dev = eoe->dev;
595  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
596  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
597  if (netif_rx(eoe->rx_skb)) {
598  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
599  }
600  eoe->rx_skb = NULL;
601 
603  }
604  else {
605  eoe->rx_expected_fragment++;
606 #if EOE_DEBUG_LEVEL >= 2
607  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
608  eoe->dev->name, eoe->rx_expected_fragment);
609 #endif
611  }
612 }
613 
614 /*****************************************************************************/
615 
624 {
625 #if EOE_DEBUG_LEVEL >= 2
626  unsigned int wakeup = 0;
627 #endif
628 
629  if (eoe->slave->error_flag ||
631  eoe->rx_idle = 1;
632  eoe->tx_idle = 1;
633  return;
634  }
635 
636  down(&eoe->tx_queue_sem);
637 
638  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
639  up(&eoe->tx_queue_sem);
640  eoe->tx_idle = 1;
641  // no data available.
642  // start a new receive immediately.
644  return;
645  }
646 
647  // take the first frame out of the queue
648  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
649  list_del(&eoe->tx_frame->queue);
650  if (!eoe->tx_queue_active &&
651  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
652  netif_wake_queue(eoe->dev);
653  eoe->tx_queue_active = 1;
654 #if EOE_DEBUG_LEVEL >= 2
655  wakeup = 1;
656 #endif
657  }
658 
659  eoe->tx_queued_frames--;
660  up(&eoe->tx_queue_sem);
661 
662  eoe->tx_idle = 0;
663 
664  eoe->tx_frame_number++;
665  eoe->tx_frame_number %= 16;
666  eoe->tx_fragment_number = 0;
667  eoe->tx_offset = 0;
668 
669  if (ec_eoe_send(eoe)) {
670  dev_kfree_skb(eoe->tx_frame->skb);
671  kfree(eoe->tx_frame);
672  eoe->tx_frame = NULL;
673  eoe->stats.tx_errors++;
675 #if EOE_DEBUG_LEVEL >= 1
676  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
677 #endif
678  return;
679  }
680 
681 #if EOE_DEBUG_LEVEL >= 2
682  if (wakeup)
683  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
684  eoe->dev->name);
685 #endif
686 
687  eoe->tries = EC_EOE_TRIES;
689 }
690 
691 /*****************************************************************************/
692 
699 {
700  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
701  if (eoe->tries) {
702  eoe->tries--; // try again
703  eoe->queue_datagram = 1;
704  } else {
705  eoe->stats.tx_errors++;
706 #if EOE_DEBUG_LEVEL >= 1
707  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
708  " datagram for %s after %u tries.\n",
709  eoe->dev->name, EC_EOE_TRIES);
710 #endif
712  }
713  return;
714  }
715 
716  if (eoe->datagram.working_counter != 1) {
717  if (eoe->tries) {
718  eoe->tries--; // try again
719  eoe->queue_datagram = 1;
720  } else {
721  eoe->stats.tx_errors++;
722 #if EOE_DEBUG_LEVEL >= 1
723  EC_SLAVE_WARN(eoe->slave, "No sending response"
724  " for %s after %u tries.\n",
725  eoe->dev->name, EC_EOE_TRIES);
726 #endif
728  }
729  return;
730  }
731 
732  // frame completely sent
733  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
734  eoe->stats.tx_packets++;
735  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
736  eoe->tx_counter += eoe->tx_frame->skb->len;
737  dev_kfree_skb(eoe->tx_frame->skb);
738  kfree(eoe->tx_frame);
739  eoe->tx_frame = NULL;
741  }
742  else { // send next fragment
743  if (ec_eoe_send(eoe)) {
744  dev_kfree_skb(eoe->tx_frame->skb);
745  kfree(eoe->tx_frame);
746  eoe->tx_frame = NULL;
747  eoe->stats.tx_errors++;
748 #if EOE_DEBUG_LEVEL >= 1
749  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
750 #endif
752  }
753  }
754 }
755 
756 /******************************************************************************
757  * NET_DEVICE functions
758  *****************************************************************************/
759 
764 int ec_eoedev_open(struct net_device *dev )
765 {
766  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
767  ec_eoe_flush(eoe);
768  eoe->opened = 1;
769  eoe->rx_idle = 0;
770  eoe->tx_idle = 0;
771  netif_start_queue(dev);
772  eoe->tx_queue_active = 1;
773 #if EOE_DEBUG_LEVEL >= 2
774  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
775 #endif
777  return 0;
778 }
779 
780 /*****************************************************************************/
781 
786 int ec_eoedev_stop(struct net_device *dev )
787 {
788  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
789  netif_stop_queue(dev);
790  eoe->rx_idle = 1;
791  eoe->tx_idle = 1;
792  eoe->tx_queue_active = 0;
793  eoe->opened = 0;
794  ec_eoe_flush(eoe);
795 #if EOE_DEBUG_LEVEL >= 2
796  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
797 #endif
799  return 0;
800 }
801 
802 /*****************************************************************************/
803 
808 int ec_eoedev_tx(struct sk_buff *skb,
809  struct net_device *dev
810  )
811 {
812  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
813  ec_eoe_frame_t *frame;
814 
815 #if 0
816  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
817  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
818  " exceeds MTU. dropping.\n", skb->len);
819  dev_kfree_skb(skb);
820  eoe->stats.tx_dropped++;
821  return 0;
822  }
823 #endif
824 
825  if (!(frame =
826  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
827  if (printk_ratelimit())
828  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
829  return 1;
830  }
831 
832  frame->skb = skb;
833 
834  down(&eoe->tx_queue_sem);
835  list_add_tail(&frame->queue, &eoe->tx_queue);
836  eoe->tx_queued_frames++;
837  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
838  netif_stop_queue(dev);
839  eoe->tx_queue_active = 0;
840  }
841  up(&eoe->tx_queue_sem);
842 
843 #if EOE_DEBUG_LEVEL >= 2
844  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
845  " with %u octets (%u frames queued).\n",
846  eoe->dev->name, skb->len, eoe->tx_queued_frames);
847  if (!eoe->tx_queue_active)
848  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
849 #endif
850 
851  return 0;
852 }
853 
854 /*****************************************************************************/
855 
860 struct net_device_stats *ec_eoedev_stats(
861  struct net_device *dev
862  )
863 {
864  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
865  return &eoe->stats;
866 }
867 
868 /*****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:352
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:446
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:254
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:623
#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:415
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:232
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:394
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:378
#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:808
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:698
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:764
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:324
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:786
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:208
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:860
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:366
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