IgH EtherCAT Master  1.5.4
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 size_or_offset, fragment_number;
478 #if EOE_DEBUG_LEVEL >= 2
479  uint8_t frame_number;
480 #endif
481 #if EOE_DEBUG_LEVEL >= 3
482  unsigned int i;
483 #endif
484 
485  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
486  eoe->stats.rx_errors++;
487 #if EOE_DEBUG_LEVEL >= 1
488  EC_SLAVE_WARN(eoe->slave, "Failed to receive mbox"
489  " fetch datagram for %s.\n", eoe->dev->name);
490 #endif
492  return;
493  }
494 
495  data = ec_slave_mbox_fetch(eoe->slave, &eoe->datagram,
496  &mbox_prot, &rec_size);
497  if (IS_ERR(data)) {
498  eoe->stats.rx_errors++;
499 #if EOE_DEBUG_LEVEL >= 1
500  EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
501  eoe->dev->name);
502 #endif
504  return;
505  }
506 
507  if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
508  eoe->stats.rx_errors++;
509 #if EOE_DEBUG_LEVEL >= 1
510  EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
511  eoe->dev->name);
512 #endif
514  return;
515  }
516 
517  frame_type = EC_READ_U16(data) & 0x000F;
518 
519  if (frame_type != 0x00) {
520 #if EOE_DEBUG_LEVEL >= 1
521  EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
522  " Dropping.\n", eoe->dev->name);
523 #endif
524  eoe->stats.rx_dropped++;
526  return;
527  }
528 
529  // EoE Fragment Request received
530 
531  last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
532  time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
533  fragment_number = EC_READ_U16(data + 2) & 0x003F;
534  size_or_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
535 #if EOE_DEBUG_LEVEL >= 2
536  frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
537 #endif
538 
539  data_size = time_appended ? rec_size - 8 : rec_size - 4;
540 
541 #if EOE_DEBUG_LEVEL >= 2
542  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
543  " frame %u%s, %zu octets\n", eoe->dev->name, fragment_number,
544  last_fragment ? "" : "+", size_or_offset, frame_number,
545  time_appended ? ", + timestamp" : "", data_size);
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  if (!fragment_number) {
561  size_t complete_size = size_or_offset * 32;
562 
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  eoe->rx_skb = NULL;
567  }
568 
569  if (!complete_size || data_size > complete_size) {
570  EC_SLAVE_ERR(eoe->slave, "Received corrupted EoE first fragment"
571  " (%zu of %zu bytes)!\n", data_size, complete_size);
572  eoe->stats.rx_errors++;
574  return;
575  }
576 
577  // new socket buffer
578  if (!(eoe->rx_skb = dev_alloc_skb(complete_size))) {
579  if (printk_ratelimit()) {
580  EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
581  " frame dropped.\n");
582  }
583  eoe->stats.rx_dropped++;
585  return;
586  }
587 
588  eoe->rx_skb_offset = 0;
589  eoe->rx_skb_size = complete_size;
590  eoe->rx_expected_fragment = 0;
591  }
592  else {
593  off_t offset = size_or_offset * 32;
594 
595  if (!eoe->rx_skb) {
596  eoe->stats.rx_dropped++;
598  return;
599  }
600 
601  if (offset != eoe->rx_skb_offset ||
602  offset + data_size > eoe->rx_skb_size ||
603  fragment_number != eoe->rx_expected_fragment) {
604  dev_kfree_skb(eoe->rx_skb);
605  eoe->rx_skb = NULL;
606  eoe->stats.rx_errors++;
607 #if EOE_DEBUG_LEVEL >= 1
608  EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
609  eoe->dev->name);
610 #endif
612  return;
613  }
614  }
615 
616  // copy fragment into socket buffer
617  memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
618  eoe->rx_skb_offset += data_size;
619 
620  if (last_fragment) {
621  // update statistics
622  eoe->stats.rx_packets++;
623  eoe->stats.rx_bytes += eoe->rx_skb->len;
624  eoe->rx_counter += eoe->rx_skb->len;
625 
626 #if EOE_DEBUG_LEVEL >= 2
627  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
628  " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
629 #endif
630 
631  // pass socket buffer to network stack
632  eoe->rx_skb->dev = eoe->dev;
633  eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
634  eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
635  if (netif_rx(eoe->rx_skb)) {
636  EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
637  }
638  eoe->rx_skb = NULL;
639 
641  }
642  else {
643  eoe->rx_expected_fragment++;
644 #if EOE_DEBUG_LEVEL >= 2
645  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
646  eoe->dev->name, eoe->rx_expected_fragment);
647 #endif
649  }
650 }
651 
652 /*****************************************************************************/
653 
662 {
663 #if EOE_DEBUG_LEVEL >= 2
664  unsigned int wakeup = 0;
665 #endif
666 
667  if (eoe->slave->error_flag ||
669  eoe->rx_idle = 1;
670  eoe->tx_idle = 1;
671  return;
672  }
673 
674  down(&eoe->tx_queue_sem);
675 
676  if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
677  up(&eoe->tx_queue_sem);
678  eoe->tx_idle = 1;
679  // no data available.
680  // start a new receive immediately.
682  return;
683  }
684 
685  // take the first frame out of the queue
686  eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
687  list_del(&eoe->tx_frame->queue);
688  if (!eoe->tx_queue_active &&
689  eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
690  netif_wake_queue(eoe->dev);
691  eoe->tx_queue_active = 1;
692 #if EOE_DEBUG_LEVEL >= 2
693  wakeup = 1;
694 #endif
695  }
696 
697  eoe->tx_queued_frames--;
698  up(&eoe->tx_queue_sem);
699 
700  eoe->tx_idle = 0;
701 
702  eoe->tx_frame_number++;
703  eoe->tx_frame_number %= 16;
704  eoe->tx_fragment_number = 0;
705  eoe->tx_offset = 0;
706 
707  if (ec_eoe_send(eoe)) {
708  dev_kfree_skb(eoe->tx_frame->skb);
709  kfree(eoe->tx_frame);
710  eoe->tx_frame = NULL;
711  eoe->stats.tx_errors++;
713 #if EOE_DEBUG_LEVEL >= 1
714  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
715 #endif
716  return;
717  }
718 
719 #if EOE_DEBUG_LEVEL >= 2
720  if (wakeup)
721  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
722  eoe->dev->name);
723 #endif
724 
725  eoe->tries = EC_EOE_TRIES;
727 }
728 
729 /*****************************************************************************/
730 
737 {
738  if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
739  if (eoe->tries) {
740  eoe->tries--; // try again
741  eoe->queue_datagram = 1;
742  } else {
743  eoe->stats.tx_errors++;
744 #if EOE_DEBUG_LEVEL >= 1
745  EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
746  " datagram for %s after %u tries.\n",
747  eoe->dev->name, EC_EOE_TRIES);
748 #endif
750  }
751  return;
752  }
753 
754  if (eoe->datagram.working_counter != 1) {
755  if (eoe->tries) {
756  eoe->tries--; // try again
757  eoe->queue_datagram = 1;
758  } else {
759  eoe->stats.tx_errors++;
760 #if EOE_DEBUG_LEVEL >= 1
761  EC_SLAVE_WARN(eoe->slave, "No sending response"
762  " for %s after %u tries.\n",
763  eoe->dev->name, EC_EOE_TRIES);
764 #endif
766  }
767  return;
768  }
769 
770  // frame completely sent
771  if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
772  eoe->stats.tx_packets++;
773  eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
774  eoe->tx_counter += eoe->tx_frame->skb->len;
775  dev_kfree_skb(eoe->tx_frame->skb);
776  kfree(eoe->tx_frame);
777  eoe->tx_frame = NULL;
779  }
780  else { // send next fragment
781  if (ec_eoe_send(eoe)) {
782  dev_kfree_skb(eoe->tx_frame->skb);
783  kfree(eoe->tx_frame);
784  eoe->tx_frame = NULL;
785  eoe->stats.tx_errors++;
786 #if EOE_DEBUG_LEVEL >= 1
787  EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
788 #endif
790  }
791  }
792 }
793 
794 /******************************************************************************
795  * NET_DEVICE functions
796  *****************************************************************************/
797 
802 int ec_eoedev_open(struct net_device *dev )
803 {
804  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
805  ec_eoe_flush(eoe);
806  eoe->opened = 1;
807  eoe->rx_idle = 0;
808  eoe->tx_idle = 0;
809  netif_start_queue(dev);
810  eoe->tx_queue_active = 1;
811 #if EOE_DEBUG_LEVEL >= 2
812  EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
813 #endif
815  return 0;
816 }
817 
818 /*****************************************************************************/
819 
824 int ec_eoedev_stop(struct net_device *dev )
825 {
826  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
827  netif_stop_queue(dev);
828  eoe->rx_idle = 1;
829  eoe->tx_idle = 1;
830  eoe->tx_queue_active = 0;
831  eoe->opened = 0;
832  ec_eoe_flush(eoe);
833 #if EOE_DEBUG_LEVEL >= 2
834  EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
835 #endif
837  return 0;
838 }
839 
840 /*****************************************************************************/
841 
846 int ec_eoedev_tx(struct sk_buff *skb,
847  struct net_device *dev
848  )
849 {
850  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
851  ec_eoe_frame_t *frame;
852 
853 #if 0
854  if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
855  EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
856  " exceeds MTU. dropping.\n", skb->len);
857  dev_kfree_skb(skb);
858  eoe->stats.tx_dropped++;
859  return 0;
860  }
861 #endif
862 
863  if (!(frame =
864  (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
865  if (printk_ratelimit())
866  EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
867  return 1;
868  }
869 
870  frame->skb = skb;
871 
872  down(&eoe->tx_queue_sem);
873  list_add_tail(&frame->queue, &eoe->tx_queue);
874  eoe->tx_queued_frames++;
875  if (eoe->tx_queued_frames == eoe->tx_queue_size) {
876  netif_stop_queue(dev);
877  eoe->tx_queue_active = 0;
878  }
879  up(&eoe->tx_queue_sem);
880 
881 #if EOE_DEBUG_LEVEL >= 2
882  EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
883  " with %u octets (%u frames queued).\n",
884  eoe->dev->name, skb->len, eoe->tx_queued_frames);
885  if (!eoe->tx_queue_active)
886  EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
887 #endif
888 
889  return 0;
890 }
891 
892 /*****************************************************************************/
893 
898 struct net_device_stats *ec_eoedev_stats(
899  struct net_device *dev
900  )
901 {
902  ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
903  return &eoe->stats;
904 }
905 
906 /*****************************************************************************/
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:846
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:661
#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:736
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:802
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:824
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:898
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