IgH EtherCAT Master  1.5.3
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 #ifdef CONFIG_SUSE_KERNEL
48 #include <linux/suse_version.h>
49 #else
50 # ifndef SUSE_VERSION
51 # define SUSE_VERSION 0
52 # endif
53 # ifndef SUSE_PATCHLEVEL
54 # define SUSE_PATCHLEVEL 0
55 # endif
56 #endif
57 /*****************************************************************************/
58 
66 #define EOE_DEBUG_LEVEL 1
67 
70 #define EC_EOE_TX_QUEUE_SIZE 100
71 
74 #define EC_EOE_TRIES 100
75 
76 /*****************************************************************************/
77 
78 void ec_eoe_flush(ec_eoe_t *);
79 
80 // state functions
86 
87 // net_device functions
88 int ec_eoedev_open(struct net_device *);
89 int ec_eoedev_stop(struct net_device *);
90 int ec_eoedev_tx(struct sk_buff *, struct net_device *);
91 struct net_device_stats *ec_eoedev_stats(struct net_device *);
92 
93 /*****************************************************************************/
94 
95 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
96 
98 static const struct net_device_ops ec_eoedev_ops = {
99  .ndo_open = ec_eoedev_open,
100  .ndo_stop = ec_eoedev_stop,
101  .ndo_start_xmit = ec_eoedev_tx,
102  .ndo_get_stats = ec_eoedev_stats,
103 };
104 #endif
105 
106 /*****************************************************************************/
107 
115  ec_eoe_t *eoe,
116  ec_slave_t *slave
117  )
118 {
119  ec_eoe_t **priv;
120  int ret = 0;
121  char name[EC_DATAGRAM_NAME_SIZE];
122  u8 mac_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
123 
124  eoe->slave = slave;
125 
126  ec_datagram_init(&eoe->datagram);
127  eoe->queue_datagram = 0;
129  eoe->opened = 0;
130  eoe->rx_skb = NULL;
131  eoe->rx_expected_fragment = 0;
132  INIT_LIST_HEAD(&eoe->tx_queue);
133  eoe->tx_frame = NULL;
134  eoe->tx_queue_active = 0;
136  eoe->tx_queued_frames = 0;
137 
138  sema_init(&eoe->tx_queue_sem, 1);
139  eoe->tx_frame_number = 0xFF;
140  memset(&eoe->stats, 0, sizeof(struct net_device_stats));
141 
142  eoe->rx_counter = 0;
143  eoe->tx_counter = 0;
144  eoe->rx_rate = 0;
145  eoe->tx_rate = 0;
146  eoe->rate_jiffies = 0;
147  eoe->rx_idle = 1;
148  eoe->tx_idle = 1;
149 
150  /* device name eoe<MASTER>[as]<SLAVE>, because networking scripts don't
151  * like hyphens etc. in interface names. */
152  if (slave->effective_alias) {
153  snprintf(name, EC_DATAGRAM_NAME_SIZE,
154  "eoe%ua%u", slave->master->index, slave->effective_alias);
155  } else {
156  snprintf(name, EC_DATAGRAM_NAME_SIZE,
157  "eoe%us%u", slave->master->index, slave->ring_position);
158  }
159 
160  snprintf(eoe->datagram.name, EC_DATAGRAM_NAME_SIZE, name);
161 
162 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
163  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, NET_NAME_UNKNOWN,
164  ether_setup);
165 #else
166  eoe->dev = alloc_netdev(sizeof(ec_eoe_t *), name, ether_setup);
167 #endif
168  if (!eoe->dev) {
169  EC_SLAVE_ERR(slave, "Unable to allocate net_device %s"
170  " for EoE handler!\n", name);
171  ret = -ENODEV;
172  goto out_return;
173  }
174 
175  // initialize net_device
176 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
177  eoe->dev->netdev_ops = &ec_eoedev_ops;
178 #else
179  eoe->dev->open = ec_eoedev_open;
180  eoe->dev->stop = ec_eoedev_stop;
181  eoe->dev->hard_start_xmit = ec_eoedev_tx;
182  eoe->dev->get_stats = ec_eoedev_stats;
183 #endif
184 
185 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
186  eth_hw_addr_set(eoe->dev, mac_addr);
187 #else
188  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
189 #endif
190 
191  // initialize private data
192  priv = netdev_priv(eoe->dev);
193  *priv = eoe;
194 
195  // Usually setting the MTU appropriately makes the upper layers
196  // do the frame fragmenting. In some cases this doesn't work
197  // so the MTU is left on the Ethernet standard value and fragmenting
198  // is done "manually".
199 #if 0
200  eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
201 #endif
202 
203  // connect the net_device to the kernel
204  ret = register_netdev(eoe->dev);
205  if (ret) {
206  EC_SLAVE_ERR(slave, "Unable to register net_device:"
207  " error %i\n", ret);
208  goto out_free;
209  }
210 
211  // make the last address octet unique
212  mac_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
213 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
214  eth_hw_addr_set(eoe->dev, mac_addr);
215 #else
216  memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
217 #endif
218 
219  return 0;
220 
221  out_free:
222  free_netdev(eoe->dev);
223  eoe->dev = NULL;
224  out_return:
225  return ret;
226 }
227 
228 /*****************************************************************************/
229 
235 {
236  unregister_netdev(eoe->dev); // possibly calls close callback
237 
238  // empty transmit queue
239  ec_eoe_flush(eoe);
240 
241  if (eoe->tx_frame) {
242  dev_kfree_skb(eoe->tx_frame->skb);
243  kfree(eoe->tx_frame);
244  }
245 
246  if (eoe->rx_skb)
247  dev_kfree_skb(eoe->rx_skb);
248 
249  free_netdev(eoe->dev);
250 
252 }
253 
254 /*****************************************************************************/
255 
259 {
260  ec_eoe_frame_t *frame, *next;
261 
262  down(&eoe->tx_queue_sem);
263 
264  list_for_each_entry_safe(frame, next, &eoe->tx_queue, queue) {
265  list_del(&frame->queue);
266  dev_kfree_skb(frame->skb);
267  kfree(frame);
268  }
269  eoe->tx_queued_frames = 0;
270 
271  up(&eoe->tx_queue_sem);
272 }
273 
274 /*****************************************************************************/
275 
281 {
282  size_t remaining_size, current_size, complete_offset;
283  unsigned int last_fragment;
284  uint8_t *data;
285 #if EOE_DEBUG_LEVEL >= 3
286  unsigned int i;
287 #endif
288 
289  remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
290 
291  if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
292  current_size = remaining_size;
293  last_fragment = 1;
294  } else {
295  current_size = ((eoe->slave->configured_tx_mailbox_size - 10) / 32) * 32;
296  last_fragment = 0;
297  }
298 
299  if (eoe->tx_fragment_number) {
300  complete_offset = eoe->tx_offset / 32;
301  }
302  else {
303  // complete size in 32 bit blocks, rounded up.
304  complete_offset = remaining_size / 32 + 1;
305  }
306 
307 #if EOE_DEBUG_LEVEL >= 2
308  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX sending fragment %u%s"
309  " with %zu octets (%zu). %u frames queued.\n",
310  eoe->dev->name, eoe->tx_fragment_number,
311  last_fragment ? "" : "+", current_size, complete_offset,
312  eoe->tx_queued_frames);
313 #endif
314 
315 #if EOE_DEBUG_LEVEL >= 3
316  EC_SLAVE_DBG(eoe->slave, 0, "");
317  for (i = 0; i < current_size; i++) {
318  printk(KERN_CONT "%02X ",
319  eoe->tx_frame->skb->data[eoe->tx_offset + i]);
320  if ((i + 1) % 16 == 0) {
321  printk(KERN_CONT "\n");
322  EC_SLAVE_DBG(eoe->slave, 0, "");
323  }
324  }
325  printk(KERN_CONT "\n");
326 #endif
327 
328  data = ec_slave_mbox_prepare_send(eoe->slave, &eoe->datagram,
329  EC_MBOX_TYPE_EOE, current_size + 4);
330  if (IS_ERR(data))
331  return PTR_ERR(data);
332 
333  EC_WRITE_U8 (data, 0x00); // eoe fragment req.
334  EC_WRITE_U8 (data + 1, last_fragment);
335  EC_WRITE_U16(data + 2, ((eoe->tx_fragment_number & 0x3F) |
336  (complete_offset & 0x3F) << 6 |
337  (eoe->tx_frame_number & 0x0F) << 12));
338 
339  memcpy(data + 4, eoe->tx_frame->skb->data + eoe->tx_offset, current_size);
340  eoe->queue_datagram = 1;
341 
342  eoe->tx_offset += current_size;
343  eoe->tx_fragment_number++;
344  return 0;
345 }
346 
347 /*****************************************************************************/
348 
351 void ec_eoe_run(ec_eoe_t *eoe )
352 {
353  if (!eoe->opened)
354  return;
355 
356  // if the datagram was not sent, or is not yet received, skip this cycle
357  if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
358  return;
359 
360  // call state function
361  eoe->state(eoe);
362 
363  // update statistics
364  if (jiffies - eoe->rate_jiffies > HZ) {
365  eoe->rx_rate = eoe->rx_counter;
366  eoe->tx_rate = eoe->tx_counter;
367  eoe->rx_counter = 0;
368  eoe->tx_counter = 0;
369  eoe->rate_jiffies = jiffies;
370  }
371 
373 }
374 
375 /*****************************************************************************/
376 
380 {
381  if (eoe->queue_datagram) {
383  eoe->queue_datagram = 0;
384  }
385 }
386 
387 /*****************************************************************************/
388 
393 int ec_eoe_is_open(const ec_eoe_t *eoe )
394 {
395  return eoe->opened;
396 }
397 
398 /*****************************************************************************/
399 
405 int ec_eoe_is_idle(const ec_eoe_t *eoe )
406 {
407  return eoe->rx_idle && eoe->tx_idle;
408 }
409 
410 /******************************************************************************
411  * STATE PROCESSING FUNCTIONS
412  *****************************************************************************/
413 
422 {
423  if (eoe->slave->error_flag ||
425  eoe->rx_idle = 1;
426  eoe->tx_idle = 1;
427  return;
428  }
429 
431  eoe->queue_datagram = 1;
433 }
434 
435 /*****************************************************************************/
436 
443 {
444  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
445  eoe->stats.rx_errors++;
446 #if EOE_DEBUG_LEVEL >= 1
447  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
448  " check datagram for %s.\n", eoe->dev->name);
449 #endif
451  return;
452  }
453 
454  if (!ec_slave_mbox_check(&eoe->datagram)) {
455  eoe->rx_idle = 1;
457  return;
458  }
459 
460  eoe->rx_idle = 0;
462  eoe->queue_datagram = 1;
464 }
465 
466 /*****************************************************************************/
467 
474 {
475  size_t rec_size, data_size;
476  uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
477  uint8_t fragment_offset, fragment_number;
478 #if EOE_DEBUG_LEVEL >= 2
479  uint8_t frame_number;
480 #endif
481  off_t offset;
482 #if EOE_DEBUG_LEVEL >= 3
483  unsigned int i;
484 #endif
485 
486  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
487  eoe->stats.rx_errors++;
488 #if EOE_DEBUG_LEVEL >= 1
489  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
490  " fetch datagram for %s.\n", eoe->dev->name);
491 #endif
493  return;
494  }
495 
496  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
497  &mbox_prot, &rec_size);
498  if (IS_ERR(data)) {
499  eoe->stats.rx_errors++;
500 #if EOE_DEBUG_LEVEL >= 1
501  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
502  eoe->dev->name);
503 #endif
505  return;
506  }
507 
508  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
509  eoe->stats.rx_errors++;
510 #if EOE_DEBUG_LEVEL >= 1
511  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
512  eoe->dev->name);
513 #endif
515  return;
516  }
517 
518  frame_type = EC_READ_U16(data) & 0x000F;
519 
520  if (frame_type != 0x00) {
521 #if EOE_DEBUG_LEVEL >= 1
522  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
523  " Dropping.\n", eoe->dev->name);
524 #endif
525  eoe->stats.rx_dropped++;
527  return;
528  }
529 
530  // EoE Fragment Request received
531 
532  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
533  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
534  fragment_number = EC_READ_U16(data + 2) & 0x003F;
535  fragment_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
536 #if EOE_DEBUG_LEVEL >= 2
537  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
538 #endif
539 
540 #if EOE_DEBUG_LEVEL >= 2
541  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
542  " frame %u%s, %u octets\n", eoe->dev->name, fragment_number,
543  last_fragment ? "" : "+", fragment_offset, frame_number,
544  time_appended ? ", + timestamp" : "",
545  time_appended ? rec_size - 8 : rec_size - 4);
546 #endif
547 
548 #if EOE_DEBUG_LEVEL >= 3
549  EC_SLAVE_DBG(eoe->slave, 0, "");
550  for (i = 0; i < rec_size - 4; i++) {
551  printk(KERN_CONT "%02X ", data[i + 4]);
552  if ((i + 1) % 16 == 0) {
553  printk(KERN_CONT "\n");
554  EC_SLAVE_DBG(eoe->slave, 0, "");
555  }
556  }
557  printk(KERN_CONT "\n");
558 #endif
559 
560  data_size = time_appended ? rec_size - 8 : rec_size - 4;
561 
562  if (!fragment_number) {
563  if (eoe->rx_skb) {
564  EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
565  dev_kfree_skb(eoe->rx_skb);
566  }
567 
568  // new socket buffer
569  if (!(eoe->rx_skb = dev_alloc_skb(fragment_offset * 32))) {
570  if (printk_ratelimit())
571  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
572  " frame dropped.\n");
573  eoe->stats.rx_dropped++;
575  return;
576  }
577 
578  eoe->rx_skb_offset = 0;
579  eoe->rx_skb_size = fragment_offset * 32;
580  eoe->rx_expected_fragment = 0;
581  }
582  else {
583  if (!eoe->rx_skb) {
584  eoe->stats.rx_dropped++;
586  return;
587  }
588 
589  offset = fragment_offset * 32;
590  if (offset != eoe->rx_skb_offset ||
591  offset + data_size > eoe->rx_skb_size ||
592  fragment_number != eoe->rx_expected_fragment) {
593  dev_kfree_skb(eoe->rx_skb);
594  eoe->rx_skb = NULL;
595  eoe->stats.rx_errors++;
596 #if EOE_DEBUG_LEVEL >= 1
597  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
598  eoe->dev->name);
599 #endif
601  return;
602  }
603  }
604 
605  // copy fragment into socket buffer
606  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
607  eoe->rx_skb_offset += data_size;
608 
609  if (last_fragment) {
610  // update statistics
611  eoe->stats.rx_packets++;
612  eoe->stats.rx_bytes += eoe->rx_skb->len;
613  eoe->rx_counter += eoe->rx_skb->len;
614 
615 #if EOE_DEBUG_LEVEL >= 2
616  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
617  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
618 #endif
619 
620  // pass socket buffer to network stack
621  eoe->rx_skb->dev = eoe->dev;
622  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
623  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
624  if (netif_rx(eoe->rx_skb)) {
625  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
626  }
627  eoe->rx_skb = NULL;
628 
630  }
631  else {
632  eoe->rx_expected_fragment++;
633 #if EOE_DEBUG_LEVEL >= 2
634  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
635  eoe->dev->name, eoe->rx_expected_fragment);
636 #endif
638  }
639 }
640 
641 /*****************************************************************************/
642 
651 {
652 #if EOE_DEBUG_LEVEL >= 2
653  unsigned int wakeup = 0;
654 #endif
655 
656  if (eoe->slave->error_flag ||
658  eoe->rx_idle = 1;
659  eoe->tx_idle = 1;
660  return;
661  }
662 
663  down(&eoe->tx_queue_sem);
664 
665  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
666  up(&eoe->tx_queue_sem);
667  eoe->tx_idle = 1;
668  // no data available.
669  // start a new receive immediately.
671  return;
672  }
673 
674  // take the first frame out of the queue
675  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
676  list_del(&eoe->tx_frame->queue);
677  if (!eoe->tx_queue_active &&
678  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
679  netif_wake_queue(eoe->dev);
680  eoe->tx_queue_active = 1;
681 #if EOE_DEBUG_LEVEL >= 2
682  wakeup = 1;
683 #endif
684  }
685 
686  eoe->tx_queued_frames--;
687  up(&eoe->tx_queue_sem);
688 
689  eoe->tx_idle = 0;
690 
691  eoe->tx_frame_number++;
692  eoe->tx_frame_number %= 16;
693  eoe->tx_fragment_number = 0;
694  eoe->tx_offset = 0;
695 
696  if (ec_eoe_send(eoe)) {
697  dev_kfree_skb(eoe->tx_frame->skb);
698  kfree(eoe->tx_frame);
699  eoe->tx_frame = NULL;
700  eoe->stats.tx_errors++;
702 #if EOE_DEBUG_LEVEL >= 1
703  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
704 #endif
705  return;
706  }
707 
708 #if EOE_DEBUG_LEVEL >= 2
709  if (wakeup)
710  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
711  eoe->dev->name);
712 #endif
713 
714  eoe->tries = EC_EOE_TRIES;
716 }
717 
718 /*****************************************************************************/
719 
726 {
727  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
728  if (eoe->tries) {
729  eoe->tries--; // try again
730  eoe->queue_datagram = 1;
731  } else {
732  eoe->stats.tx_errors++;
733 #if EOE_DEBUG_LEVEL >= 1
734  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
735  " datagram for %s after %u tries.\n",
736  eoe->dev->name, EC_EOE_TRIES);
737 #endif
739  }
740  return;
741  }
742 
743  if (eoe->datagram.working_counter != 1) {
744  if (eoe->tries) {
745  eoe->tries--; // try again
746  eoe->queue_datagram = 1;
747  } else {
748  eoe->stats.tx_errors++;
749 #if EOE_DEBUG_LEVEL >= 1
750  EC_SLAVE_WARN(eoe->slave, "No sending response"
751  " for %s after %u tries.\n",
752  eoe->dev->name, EC_EOE_TRIES);
753 #endif
755  }
756  return;
757  }
758 
759  // frame completely sent
760  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
761  eoe->stats.tx_packets++;
762  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
763  eoe->tx_counter += eoe->tx_frame->skb->len;
764  dev_kfree_skb(eoe->tx_frame->skb);
765  kfree(eoe->tx_frame);
766  eoe->tx_frame = NULL;
768  }
769  else { // send next fragment
770  if (ec_eoe_send(eoe)) {
771  dev_kfree_skb(eoe->tx_frame->skb);
772  kfree(eoe->tx_frame);
773  eoe->tx_frame = NULL;
774  eoe->stats.tx_errors++;
775 #if EOE_DEBUG_LEVEL >= 1
776  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
777 #endif
779  }
780  }
781 }
782 
783 /******************************************************************************
784  * NET_DEVICE functions
785  *****************************************************************************/
786 
791 int ec_eoedev_open(struct net_device *dev )
792 {
793  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
794  ec_eoe_flush(eoe);
795  eoe->opened = 1;
796  eoe->rx_idle = 0;
797  eoe->tx_idle = 0;
798  netif_start_queue(dev);
799  eoe->tx_queue_active = 1;
800 #if EOE_DEBUG_LEVEL >= 2
801  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
802 #endif
804  return 0;
805 }
806 
807 /*****************************************************************************/
808 
813 int ec_eoedev_stop(struct net_device *dev )
814 {
815  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
816  netif_stop_queue(dev);
817  eoe->rx_idle = 1;
818  eoe->tx_idle = 1;
819  eoe->tx_queue_active = 0;
820  eoe->opened = 0;
821  ec_eoe_flush(eoe);
822 #if EOE_DEBUG_LEVEL >= 2
823  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
824 #endif
826  return 0;
827 }
828 
829 /*****************************************************************************/
830 
835 int ec_eoedev_tx(struct sk_buff *skb,
836  struct net_device *dev
837  )
838 {
839  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
840  ec_eoe_frame_t *frame;
841 
842 #if 0
843  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
844  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
845  " exceeds MTU. dropping.\n", skb->len);
846  dev_kfree_skb(skb);
847  eoe->stats.tx_dropped++;
848  return 0;
849  }
850 #endif
851 
852  if (!(frame =
853  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
854  if (printk_ratelimit())
855  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
856  return 1;
857  }
858 
859  frame->skb = skb;
860 
861  down(&eoe->tx_queue_sem);
862  list_add_tail(&frame->queue, &eoe->tx_queue);
863  eoe->tx_queued_frames++;
864  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
865  netif_stop_queue(dev);
866  eoe->tx_queue_active = 0;
867  }
868  up(&eoe->tx_queue_sem);
869 
870 #if EOE_DEBUG_LEVEL >= 2
871  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
872  " with %u octets (%u frames queued).\n",
873  eoe->dev->name, skb->len, eoe->tx_queued_frames);
874  if (!eoe->tx_queue_active)
875  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
876 #endif
877 
878  return 0;
879 }
880 
881 /*****************************************************************************/
882 
887 struct net_device_stats *ec_eoedev_stats(
888  struct net_device *dev
889  )
890 {
891  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
892  return &eoe->stats;
893 }
894 
895 /*****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:379
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition: ethernet.c:835
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:70
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:98
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:473
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:280
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:650
#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:2401
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
Definition: ethernet.c:442
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:114
uint8_t link_state
device link state
Definition: device.h:88
#define EC_EOE_TRIES
Number of tries.
Definition: ethernet.c:74
Sent (still in the queue).
Definition: datagram.h:77
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
Definition: ethernet.c:258
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:421
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:405
#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:2418
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:725
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:2313
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:791
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:351
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition: ethernet.c:813
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:234
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:887
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:393
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