IgH EtherCAT Master  1.7.0
ethernet.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * Copyright (C) 2006-2024 Florian Pose, Ingenieurgemeinschaft IgH
4 *
5 * This file is part of the IgH EtherCAT Master.
6 *
7 * The IgH EtherCAT Master is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2, as
9 * published by the Free Software Foundation.
10 *
11 * The IgH EtherCAT Master is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with the IgH EtherCAT Master; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 ****************************************************************************/
21
26
27/****************************************************************************/
28
29#include <linux/version.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/lockdep.h>
33#include <linux/skbuff.h>
34
35#include "globals.h"
36#include "master.h"
37#include "slave.h"
38#include "mailbox.h"
39#include "ethernet.h"
40
41#if defined(CONFIG_SUSE_KERNEL) && \
42 LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
43#include <linux/suse_version.h>
44#else
45# ifndef SUSE_VERSION
46# define SUSE_VERSION 0
47# endif
48# ifndef SUSE_PATCHLEVEL
49# define SUSE_PATCHLEVEL 0
50# endif
51#endif
52
53/****************************************************************************/
54
62#define EOE_DEBUG_LEVEL 1
63
66#define EC_EOE_TX_QUEUE_SIZE 100
67
70#define EC_EOE_TRIES 100
71
72/****************************************************************************/
73
74// prototypes for private methods
77
78/****************************************************************************/
79
80// state functions
85
86// net_device functions
87int ec_eoedev_open(struct net_device *);
88int ec_eoedev_stop(struct net_device *);
89int ec_eoedev_tx(struct sk_buff *, struct net_device *);
90struct net_device_stats *ec_eoedev_stats(struct net_device *);
91
92/****************************************************************************/
93
96static const struct net_device_ops ec_eoedev_ops = {
97 .ndo_open = ec_eoedev_open,
98 .ndo_stop = ec_eoedev_stop,
99 .ndo_start_xmit = ec_eoedev_tx,
100 .ndo_get_stats = ec_eoedev_stats,
101};
102
103/****************************************************************************/
104
112 ec_eoe_t *eoe,
113 ec_slave_t *slave
114 )
115{
116 ec_eoe_t **priv;
117 int ret = 0;
118 char name[EC_DATAGRAM_NAME_SIZE];
119 u8 mac_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
120
121 eoe->slave = slave;
122
124 // set device index only once after scan, since an EoE handler lives only
125 // with a certain slave
126 eoe->datagram.device_index = slave->device_index;
127 eoe->queue_datagram = 0;
130 eoe->opened = 0;
131 eoe->rx_skb = NULL;
132 eoe->rx_expected_fragment = 0;
133 INIT_LIST_HEAD(&eoe->tx_queue);
134 eoe->tx_frame = NULL;
135 eoe->tx_queue_active = 0;
137 eoe->tx_queued_frames = 0;
138
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 eoe->dev->netdev_ops = &ec_eoedev_ops;
177
178#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || \
179 (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
180 eth_hw_addr_set(eoe->dev, mac_addr);
181#else
182 memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
183#endif
184
185 // initialize private data
186 priv = netdev_priv(eoe->dev);
187 *priv = eoe;
188
189 // Usually setting the MTU appropriately makes the upper layers
190 // do the frame fragmenting. In some cases this doesn't work
191 // so the MTU is left on the Ethernet standard value and fragmenting
192 // is done "manually".
193#if 0
194 eoe->dev->mtu = slave->configured_rx_mailbox_size - ETH_HLEN - 10;
195#endif
196
197 // connect the net_device to the kernel
198 ret = register_netdev(eoe->dev);
199 if (ret) {
200 EC_SLAVE_ERR(slave, "Unable to register net_device:"
201 " error %i\n", ret);
202 goto out_free;
203 }
204
205 // make the last address octet unique
206 mac_addr[ETH_ALEN - 1] = (uint8_t) eoe->dev->ifindex;
207#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0) || \
208 (SUSE_VERSION == 15 && SUSE_PATCHLEVEL >= 5)
209 eth_hw_addr_set(eoe->dev, mac_addr);
210#else
211 memcpy(eoe->dev->dev_addr, mac_addr, sizeof(mac_addr));
212#endif
213
214 return 0;
215
216 out_free:
217 free_netdev(eoe->dev);
218 eoe->dev = NULL;
219 out_return:
220 return ret;
221}
222
223/****************************************************************************/
224
230{
231 unregister_netdev(eoe->dev); // possibly calls close callback
232
233 // empty transmit queue
234 ec_eoe_flush(eoe);
235
236 if (eoe->tx_frame) {
237 dev_kfree_skb(eoe->tx_frame->skb);
238 kfree(eoe->tx_frame);
239 }
240
241 if (eoe->rx_skb)
242 dev_kfree_skb(eoe->rx_skb);
243
244 free_netdev(eoe->dev);
245
248}
249
250/****************************************************************************/
251
255{
256 ec_eoe_frame_t *frame, *next;
257 struct list_head tx_queue;
258
259 netif_tx_lock_bh(eoe->dev);
260
261 list_replace_init(&eoe->tx_queue, &tx_queue);
262 eoe->tx_queued_frames = 0;
263
264 netif_tx_unlock_bh(eoe->dev);
265
266 list_for_each_entry_safe(frame, next, &tx_queue, queue) {
267 list_del(&frame->queue);
268 dev_kfree_skb(frame->skb);
269 kfree(frame);
270 }
271}
272
273/****************************************************************************/
274
280{
281 size_t remaining_size, current_size, complete_offset;
282 unsigned int last_fragment;
283 uint8_t *data;
284#if EOE_DEBUG_LEVEL >= 3
285 unsigned int i;
286#endif
287
288 remaining_size = eoe->tx_frame->skb->len - eoe->tx_offset;
289
290 if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
291 current_size = remaining_size;
292 last_fragment = 1;
293 } else {
294 current_size =
295 ((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, EC_EOE_FRAMETYPE_INIT_REQ); // Initiate EoE Request
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
352{
353 if (!eoe->opened) {
354 if (!ec_eoe_holds_mbox_lock(eoe))
355 return;
356
357 // the device was closed during a receive sequence. release the
358 // mailbox read lock, as soon as the datagram is not in use any more.
359 if (eoe->queue_datagram) {
360 eoe->queue_datagram = 0; // not handed over yet
361 } else if (eoe->datagram.state == EC_DATAGRAM_QUEUED ||
363 return;
364 }
365
368 return;
369 }
370
371 // if the datagram was not sent, or is not yet received, skip this cycle
372 if (eoe->queue_datagram || eoe->datagram.state == EC_DATAGRAM_SENT)
373 return;
374
375 // call state function
376 eoe->state(eoe);
377
378 // update statistics
379 if (jiffies - eoe->rate_jiffies > HZ) {
380 eoe->rx_rate = eoe->rx_counter;
381 eoe->tx_rate = eoe->tx_counter;
382 eoe->rx_counter = 0;
383 eoe->tx_counter = 0;
384 eoe->rate_jiffies = jiffies;
385 }
386
388}
389
390/****************************************************************************/
391
395{
396 if (eoe->queue_datagram) {
398 eoe->queue_datagram = 0;
399 }
400}
401
402/****************************************************************************/
403
408int ec_eoe_is_open(const ec_eoe_t *eoe )
409{
410 return eoe->opened;
411}
412
413/****************************************************************************/
414
422{
423 return ec_fsm_mbox_holds_lock(&eoe->fsm_mbox);
424}
425
426/****************************************************************************/
427
433int ec_eoe_is_idle(const ec_eoe_t *eoe )
434{
435 return eoe->rx_idle && eoe->tx_idle;
436}
437
438/*****************************************************************************
439 * STATE PROCESSING FUNCTIONS
440 ****************************************************************************/
441
450{
451 if (eoe->slave->error_flag ||
453 eoe->rx_idle = 1;
454 eoe->tx_idle = 1;
455 return;
456 }
457
460 eoe->state(eoe);
461}
462
463/****************************************************************************/
464
470{
471 size_t rec_size, data_size;
472 uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
473 uint8_t size_or_offset, fragment_number;
474#if EOE_DEBUG_LEVEL >= 2
475 uint8_t frame_number;
476#endif
477#if EOE_DEBUG_LEVEL >= 3
478 unsigned int i;
479#endif
480
481 if (ec_fsm_mbox_exec(&eoe->fsm_mbox, &eoe->datagram)) {
482 if (eoe->datagram.state != EC_DATAGRAM_UNUSED) {
483 eoe->queue_datagram = 1;
484 }
485 return;
486 }
487
488 switch (ec_fsm_mbox_result(&eoe->fsm_mbox)) {
489 case 0:
490 eoe->rx_idle = 0;
491 break;
492 case -EAGAIN: // no EoE data available
493 case -EBUSY: // do not wait for the other read request, but transmit
494 eoe->rx_idle = 1;
496 return;
497 default:
498 eoe->stats.rx_errors++;
499#if EOE_DEBUG_LEVEL >= 1
500 EC_SLAVE_WARN(eoe->slave, "Failed to receive mailbox"
501 " data for %s.\n", eoe->dev->name);
502#endif
504 return;
505 }
506
507 data = ec_slave_mbox_fetch(eoe->slave, &eoe->slave->mbox_eoe_data,
508 &mbox_prot, &rec_size);
509 if (IS_ERR(data)) {
510 eoe->stats.rx_errors++;
511#if EOE_DEBUG_LEVEL >= 1
512 EC_SLAVE_WARN(eoe->slave, "Invalid mailbox response for %s.\n",
513 eoe->dev->name);
514#endif
516 return;
517 }
518
519 if (mbox_prot != EC_MBOX_TYPE_EOE) { // FIXME mailbox handler necessary
520 eoe->stats.rx_errors++;
521#if EOE_DEBUG_LEVEL >= 1
522 EC_SLAVE_WARN(eoe->slave, "Other mailbox protocol response for %s.\n",
523 eoe->dev->name);
524#endif
526 return;
527 }
528
529 frame_type = EC_READ_U16(data) & 0x000F;
530
531 if (frame_type != EC_EOE_FRAMETYPE_INIT_REQ) { // EoE Fragment Data
532#if EOE_DEBUG_LEVEL >= 1
533 EC_SLAVE_WARN(eoe->slave, "%s: Other frame received."
534 " Dropping.\n", eoe->dev->name);
535#endif
536 eoe->stats.rx_dropped++;
538 return;
539 }
540
541 // EoE Fragment Request received
542
543 last_fragment = (EC_READ_U16(data) >> 8) & 0x0001;
544 time_appended = (EC_READ_U16(data) >> 9) & 0x0001;
545 fragment_number = EC_READ_U16(data + 2) & 0x003F;
546 size_or_offset = (EC_READ_U16(data + 2) >> 6) & 0x003F;
547#if EOE_DEBUG_LEVEL >= 2
548 frame_number = (EC_READ_U16(data + 2) >> 12) & 0x000F;
549#endif
550
551 data_size = time_appended ? rec_size - 8 : rec_size - 4;
552
553#if EOE_DEBUG_LEVEL >= 2
554 EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX fragment %u%s, offset %u,"
555 " frame %u%s, %zu octets\n", eoe->dev->name, fragment_number,
556 last_fragment ? "" : "+", size_or_offset, frame_number,
557 time_appended ? ", + timestamp" : "", data_size);
558#endif
559
560#if EOE_DEBUG_LEVEL >= 3
561 EC_SLAVE_DBG(eoe->slave, 0, "");
562 for (i = 0; i < rec_size - 4; i++) {
563 printk(KERN_CONT "%02X ", data[i + 4]);
564 if ((i + 1) % 16 == 0) {
565 printk(KERN_CONT "\n");
566 EC_SLAVE_DBG(eoe->slave, 0, "");
567 }
568 }
569 printk(KERN_CONT "\n");
570#endif
571
572 if (!fragment_number) {
573 size_t complete_size = size_or_offset * 32;
574
575 if (eoe->rx_skb) {
576 EC_SLAVE_WARN(eoe->slave, "EoE RX freeing old socket buffer.\n");
577 dev_kfree_skb(eoe->rx_skb);
578 eoe->rx_skb = NULL;
579 }
580
581 if (!complete_size || data_size > complete_size) {
582 EC_SLAVE_ERR(eoe->slave, "Received corrupted EoE first fragment"
583 " (%zu of %zu bytes)!\n", data_size, complete_size);
584 eoe->stats.rx_errors++;
586 return;
587 }
588
589 // new socket buffer
590 if (!(eoe->rx_skb = dev_alloc_skb(complete_size))) {
591 if (printk_ratelimit()) {
592 EC_SLAVE_WARN(eoe->slave, "EoE RX low on mem,"
593 " frame dropped.\n");
594 }
595 eoe->stats.rx_dropped++;
597 return;
598 }
599
600 eoe->rx_skb_offset = 0;
601 eoe->rx_skb_size = complete_size;
602 eoe->rx_expected_fragment = 0;
603 }
604 else {
605 off_t offset = size_or_offset * 32;
606
607 if (!eoe->rx_skb) {
608 eoe->stats.rx_dropped++;
610 return;
611 }
612
613 if (offset != eoe->rx_skb_offset ||
614 offset + data_size > eoe->rx_skb_size ||
615 fragment_number != eoe->rx_expected_fragment) {
616 dev_kfree_skb(eoe->rx_skb);
617 eoe->rx_skb = NULL;
618 eoe->stats.rx_errors++;
619#if EOE_DEBUG_LEVEL >= 1
620 EC_SLAVE_WARN(eoe->slave, "Fragmenting error at %s.\n",
621 eoe->dev->name);
622#endif
624 return;
625 }
626 }
627
628 // copy fragment into socket buffer
629 memcpy(skb_put(eoe->rx_skb, data_size), data + 4, data_size);
630 eoe->rx_skb_offset += data_size;
631
632 if (last_fragment) {
633 // update statistics
634 eoe->stats.rx_packets++;
635 eoe->stats.rx_bytes += eoe->rx_skb->len;
636 eoe->rx_counter += eoe->rx_skb->len;
637
638#if EOE_DEBUG_LEVEL >= 2
639 EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX frame completed"
640 " with %u octets.\n", eoe->dev->name, eoe->rx_skb->len);
641#endif
642
643 // pass socket buffer to network stack
644 eoe->rx_skb->dev = eoe->dev;
645 eoe->rx_skb->protocol = eth_type_trans(eoe->rx_skb, eoe->dev);
646 eoe->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
647 if (netif_rx(eoe->rx_skb)) {
648 EC_SLAVE_WARN(eoe->slave, "EoE RX netif_rx failed.\n");
649 }
650 eoe->rx_skb = NULL;
651
653 }
654 else {
656#if EOE_DEBUG_LEVEL >= 2
657 EC_SLAVE_DBG(eoe->slave, 0, "EoE %s RX expecting fragment %u\n",
658 eoe->dev->name, eoe->rx_expected_fragment);
659#endif
661 }
662}
663
664/****************************************************************************/
665
674{
675#if EOE_DEBUG_LEVEL >= 2
676 unsigned int wakeup = 0;
677#endif
678
679 if (eoe->slave->error_flag ||
681 eoe->rx_idle = 1;
682 eoe->tx_idle = 1;
683 return;
684 }
685
686 netif_tx_lock_bh(eoe->dev);
687
688 if (!eoe->tx_queued_frames || list_empty(&eoe->tx_queue)) {
689 netif_tx_unlock_bh(eoe->dev);
690 eoe->tx_idle = 1;
691 // no data available.
692 // start a new receive immediately.
694 return;
695 }
696
697 // take the first frame out of the queue
698 eoe->tx_frame = list_entry(eoe->tx_queue.next, ec_eoe_frame_t, queue);
699 list_del(&eoe->tx_frame->queue);
700 if (!eoe->tx_queue_active &&
701 eoe->tx_queued_frames == eoe->tx_queue_size / 2) {
702 netif_wake_queue(eoe->dev);
703 eoe->tx_queue_active = 1;
704#if EOE_DEBUG_LEVEL >= 2
705 wakeup = 1;
706#endif
707 }
708
709 eoe->tx_queued_frames--;
710 netif_tx_unlock_bh(eoe->dev);
711
712 eoe->tx_idle = 0;
713
714 eoe->tx_frame_number++;
715 eoe->tx_frame_number %= 16;
716 eoe->tx_fragment_number = 0;
717 eoe->tx_offset = 0;
718
719 if (ec_eoe_send(eoe)) {
720 dev_kfree_skb(eoe->tx_frame->skb);
721 kfree(eoe->tx_frame);
722 eoe->tx_frame = NULL;
723 eoe->stats.tx_errors++;
725#if EOE_DEBUG_LEVEL >= 1
726 EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
727#endif
728 return;
729 }
730
731#if EOE_DEBUG_LEVEL >= 2
732 if (wakeup)
733 EC_SLAVE_DBG(eoe->slave, 0, "EoE %s waking up TX queue...\n",
734 eoe->dev->name);
735#endif
736
737 eoe->tries = EC_EOE_TRIES;
739}
740
741/****************************************************************************/
742
749{
750 if (eoe->datagram.state != EC_DATAGRAM_RECEIVED) {
751 if (eoe->tries) {
752 eoe->tries--; // try again
753 eoe->queue_datagram = 1;
754 } else {
755 eoe->stats.tx_errors++;
756#if EOE_DEBUG_LEVEL >= 1
757 EC_SLAVE_WARN(eoe->slave, "Failed to receive send"
758 " datagram for %s after %u tries.\n",
759 eoe->dev->name, EC_EOE_TRIES);
760#endif
762 }
763 return;
764 }
765
766 if (eoe->datagram.working_counter != 1) {
767 if (eoe->tries) {
768 eoe->tries--; // try again
769 eoe->queue_datagram = 1;
770 } else {
771 eoe->stats.tx_errors++;
772#if EOE_DEBUG_LEVEL >= 1
773 EC_SLAVE_WARN(eoe->slave, "No sending response"
774 " for %s after %u tries.\n",
775 eoe->dev->name, EC_EOE_TRIES);
776#endif
778 }
779 return;
780 }
781
782 // frame completely sent
783 if (eoe->tx_offset >= eoe->tx_frame->skb->len) {
784 eoe->stats.tx_packets++;
785 eoe->stats.tx_bytes += eoe->tx_frame->skb->len;
786 eoe->tx_counter += eoe->tx_frame->skb->len;
787 dev_kfree_skb(eoe->tx_frame->skb);
788 kfree(eoe->tx_frame);
789 eoe->tx_frame = NULL;
791 }
792 else { // send next fragment
793 if (ec_eoe_send(eoe)) {
794 dev_kfree_skb(eoe->tx_frame->skb);
795 kfree(eoe->tx_frame);
796 eoe->tx_frame = NULL;
797 eoe->stats.tx_errors++;
798#if EOE_DEBUG_LEVEL >= 1
799 EC_SLAVE_WARN(eoe->slave, "Send error at %s.\n", eoe->dev->name);
800#endif
802 }
803 }
804}
805
806/*****************************************************************************
807 * NET_DEVICE functions
808 ****************************************************************************/
809
814int ec_eoedev_open(struct net_device *dev )
815{
816 ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
817 ec_eoe_flush(eoe);
818 eoe->opened = 1;
819 eoe->rx_idle = 0;
820 eoe->tx_idle = 0;
821 netif_start_queue(dev);
822 eoe->tx_queue_active = 1;
823#if EOE_DEBUG_LEVEL >= 2
824 EC_SLAVE_DBG(eoe->slave, 0, "%s opened.\n", dev->name);
825#endif
826 return 0;
827}
828
829/****************************************************************************/
830
835int ec_eoedev_stop(struct net_device *dev )
836{
837 ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
838 netif_stop_queue(dev);
839 eoe->rx_idle = 1;
840 eoe->tx_idle = 1;
841 eoe->tx_queue_active = 0;
842 eoe->opened = 0;
843 ec_eoe_flush(eoe);
844#if EOE_DEBUG_LEVEL >= 2
845 EC_SLAVE_DBG(eoe->slave, 0, "%s stopped.\n", dev->name);
846#endif
847 return 0;
848}
849
850/****************************************************************************/
851
856int ec_eoedev_tx(struct sk_buff *skb,
857 struct net_device *dev
858 )
859{
860 ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
861 ec_eoe_frame_t *frame;
862
863#if 0
864 if (skb->len > eoe->slave->configured_tx_mailbox_size - 10) {
865 EC_SLAVE_WARN(eoe->slave, "EoE TX frame (%u octets)"
866 " exceeds MTU. dropping.\n", skb->len);
867 dev_kfree_skb(skb);
868 eoe->stats.tx_dropped++;
869 return 0;
870 }
871#endif
872
873 WARN_ON_ONCE(skb_get_queue_mapping(skb) != 0);
874 lockdep_assert_held(&netdev_get_tx_queue(dev, 0)->_xmit_lock);
875
876 if (!(frame =
877 (ec_eoe_frame_t *) kmalloc(sizeof(ec_eoe_frame_t), GFP_ATOMIC))) {
878 if (printk_ratelimit())
879 EC_SLAVE_WARN(eoe->slave, "EoE TX: low on mem. frame dropped.\n");
880 return 1;
881 }
882
883 frame->skb = skb;
884
885 list_add_tail(&frame->queue, &eoe->tx_queue);
886 eoe->tx_queued_frames++;
887 if (eoe->tx_queued_frames == eoe->tx_queue_size) {
888 netif_stop_queue(dev);
889 eoe->tx_queue_active = 0;
890 }
891
892#if EOE_DEBUG_LEVEL >= 2
893 EC_SLAVE_DBG(eoe->slave, 0, "EoE %s TX queued frame"
894 " with %u octets (%u frames queued).\n",
895 eoe->dev->name, skb->len, eoe->tx_queued_frames);
896 if (!eoe->tx_queue_active)
897 EC_SLAVE_WARN(eoe->slave, "EoE TX queue is now full.\n");
898#endif
899
900 return 0;
901}
902
903/****************************************************************************/
904
909struct net_device_stats *ec_eoedev_stats(
910 struct net_device *dev
911 )
912{
913 ec_eoe_t *eoe = *((ec_eoe_t **) netdev_priv(dev));
914 return &eoe->stats;
915}
916
917/****************************************************************************/
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
Definition datagram.c:625
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
Definition datagram.c:110
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
Definition datagram.c:80
@ EC_DATAGRAM_UNUSED
Not used by its state machine in this cycle and not to be queued.
Definition datagram.h:73
@ EC_DATAGRAM_RECEIVED
Received (dequeued).
Definition datagram.h:70
@ EC_DATAGRAM_SENT
Sent (still in the queue).
Definition datagram.h:69
@ EC_DATAGRAM_QUEUED
Queued for sending.
Definition datagram.h:68
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
Definition ethernet.c:449
#define EC_EOE_TRIES
Number of tries.
Definition ethernet.c:70
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition ethernet.c:408
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
Definition ethernet.c:856
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
Definition ethernet.c:351
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition ethernet.c:229
void ec_eoe_state_tx_start(ec_eoe_t *)
State: TX START.
Definition ethernet.c:673
int ec_eoe_holds_mbox_lock(const ec_eoe_t *eoe)
Returns, if the handler holds the mailbox read lock of its slave.
Definition ethernet.c:421
int ec_eoe_send(ec_eoe_t *)
Sends a frame or the next fragment.
Definition ethernet.c:279
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
Definition ethernet.c:909
static const struct net_device_ops ec_eoedev_ops
Device operations for EoE interfaces.
Definition ethernet.c:96
int ec_eoedev_open(struct net_device *)
Opens the virtual network device.
Definition ethernet.c:814
#define EC_EOE_TX_QUEUE_SIZE
Size of the EoE tx queue.
Definition ethernet.c:66
void ec_eoe_state_rx_fetch(ec_eoe_t *)
State: RX_FETCH.
Definition ethernet.c:469
int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
EoE constructor.
Definition ethernet.c:111
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
Definition ethernet.c:254
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition ethernet.c:394
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
Definition ethernet.c:433
void ec_eoe_state_tx_sent(ec_eoe_t *)
State: TX SENT.
Definition ethernet.c:748
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Definition ethernet.c:835
Ethernet over EtherCAT (EoE).
struct ec_eoe ec_eoe_t
Definition ethernet.h:67
void ec_fsm_mbox_clear(ec_fsm_mbox_t *fsm)
Destructor.
Definition fsm_mbox.c:64
void ec_fsm_mbox_poll(ec_fsm_mbox_t *fsm, ec_slave_t *slave, ec_mbox_data_t *mbox_data)
Starts a single mailbox check without waiting.
Definition fsm_mbox.c:123
int ec_fsm_mbox_exec(ec_fsm_mbox_t *fsm, ec_datagram_t *datagram)
Executes the current state of the state machine.
Definition fsm_mbox.c:161
int ec_fsm_mbox_holds_lock(const ec_fsm_mbox_t *fsm)
Checks, if the state machine holds the slave's mailbox read lock.
Definition fsm_mbox.c:208
int ec_fsm_mbox_result(const ec_fsm_mbox_t *fsm)
Returns the result of the terminated state machine.
Definition fsm_mbox.c:195
void ec_fsm_mbox_abort(ec_fsm_mbox_t *fsm)
Aborts the state machine and releases the mailbox read lock, if held.
Definition fsm_mbox.c:220
void ec_fsm_mbox_init(ec_fsm_mbox_t *fsm)
Constructor.
Definition fsm_mbox.c:45
Global definitions and macros.
struct ec_slave ec_slave_t
Definition globals.h:315
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
Definition globals.h:110
@ EC_DEVICE_MAIN
Main device.
Definition globals.h:204
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition ecrt.h:3267
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
Definition ecrt.h:3175
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition ecrt.h:3284
uint8_t * ec_slave_mbox_fetch(const ec_slave_t *slave, ec_mbox_data_t *response_data, uint8_t *type, size_t *size)
Processes received mailbox data.
Definition mailbox.c:290
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:143
Mailbox functionality.
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:1015
EtherCAT master structure.
EtherCAT slave structure.
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition slave.h:100
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
Definition slave.h:70
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
Definition slave.h:84
uint16_t working_counter
Working counter.
Definition datagram.h:96
ec_datagram_state_t state
State.
Definition datagram.h:97
ec_device_index_t device_index
Device via which the datagram shall be / was sent.
Definition datagram.h:87
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
Definition datagram.h:109
uint8_t link_state
device link state
Definition device.h:85
Queued frame structure.
Definition ethernet.h:59
struct list_head queue
list item
Definition ethernet.h:60
struct sk_buff * skb
socket buffer
Definition ethernet.h:61
uint32_t rx_rate
receive rate (bps)
Definition ethernet.h:93
unsigned int tx_queue_active
kernel netif queue started
Definition ethernet.h:98
unsigned int opened
net_device is opened
Definition ethernet.h:85
unsigned int queue_datagram
the datagram is ready for queuing
Definition ethernet.h:80
unsigned int tx_queued_frames
number of frames in the queue
Definition ethernet.h:99
uint8_t tx_fragment_number
number of the fragment
Definition ethernet.h:102
ec_slave_t * slave
pointer to the corresponding slave
Definition ethernet.h:78
size_t rx_skb_size
size of the allocated socket buffer memory
Definition ethernet.h:90
unsigned int tx_idle
Idle flag.
Definition ethernet.h:106
ec_datagram_t datagram
datagram
Definition ethernet.h:79
ec_eoe_frame_t * tx_frame
current TX frame
Definition ethernet.h:100
off_t rx_skb_offset
current write pointer in the socket buffer
Definition ethernet.h:89
size_t tx_offset
number of octets sent
Definition ethernet.h:103
struct net_device_stats stats
device statistics
Definition ethernet.h:84
uint8_t rx_expected_fragment
next expected fragment number
Definition ethernet.h:91
uint32_t rx_counter
octets received during last second
Definition ethernet.h:92
unsigned int tries
Tries.
Definition ethernet.h:108
ec_fsm_mbox_t fsm_mbox
Mailbox receive state machine.
Definition ethernet.h:82
struct net_device * dev
net_device for virtual ethernet device
Definition ethernet.h:83
uint8_t tx_frame_number
number of the transmitted frame
Definition ethernet.h:101
uint32_t tx_counter
octets transmitted during last second
Definition ethernet.h:104
struct sk_buff * rx_skb
current rx socket buffer
Definition ethernet.h:88
unsigned long rate_jiffies
time of last rate output
Definition ethernet.h:86
uint32_t tx_rate
transmit rate (bps)
Definition ethernet.h:105
unsigned int tx_queue_size
Transmit queue size.
Definition ethernet.h:97
unsigned int rx_idle
Idle flag.
Definition ethernet.h:94
void(*) state(ec_eoe_t *)
state function for the state machine
Definition ethernet.h:81
struct list_head tx_queue
queue for frames to send
Definition ethernet.h:96
unsigned int index
Index.
Definition master.h:195
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition master.h:207
uint16_t configured_rx_mailbox_size
Configured receive mailbox size.
Definition slave.h:191
uint16_t ring_position
Ring position.
Definition slave.h:177
ec_mbox_data_t mbox_eoe_data
Received mailbox data for EoE.
Definition slave.h:236
uint16_t configured_tx_mailbox_size
Configured send mailbox size.
Definition slave.h:195
uint16_t effective_alias
Effective alias address.
Definition slave.h:179
ec_device_index_t device_index
Index of device the slave responds on.
Definition slave.h:173
ec_master_t * master
Master owning the slave.
Definition slave.h:172
unsigned int error_flag
Stop processing after an error.
Definition slave.h:187