37 #include <linux/version.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
56 #define EOE_DEBUG_LEVEL 1
60 #define EC_EOE_TX_QUEUE_SIZE 100
64 #define EC_EOE_TRIES 100
85 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
112 u8 mac_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
130 memset(&eoe->
stats, 0,
sizeof(
struct net_device_stats));
152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
153 eoe->
dev = alloc_netdev(
sizeof(
ec_eoe_t *), name, NET_NAME_UNKNOWN,
156 eoe->
dev = alloc_netdev(
sizeof(
ec_eoe_t *), name, ether_setup);
160 " for EoE handler!\n", name);
166 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
176 eth_hw_addr_set(eoe->
dev, mac_addr);
178 memcpy(eoe->
dev->dev_addr, mac_addr,
sizeof(mac_addr));
182 priv = netdev_priv(eoe->
dev);
194 ret = register_netdev(eoe->
dev);
202 mac_addr[ETH_ALEN - 1] = (uint8_t) eoe->
dev->ifindex;
203 #
if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
204 eth_hw_addr_set(eoe->
dev, mac_addr);
206 memcpy(eoe->
dev->dev_addr, mac_addr,
sizeof(mac_addr));
212 free_netdev(eoe->
dev);
226 unregister_netdev(eoe->
dev);
237 dev_kfree_skb(eoe->
rx_skb);
239 free_netdev(eoe->
dev);
254 list_for_each_entry_safe(frame, next, &eoe->
tx_queue, queue) {
255 list_del(&frame->
queue);
256 dev_kfree_skb(frame->
skb);
272 size_t remaining_size, current_size, complete_offset;
273 unsigned int last_fragment;
275 #if EOE_DEBUG_LEVEL >= 3
281 if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
282 current_size = remaining_size;
294 complete_offset = remaining_size / 32 + 1;
297 #if EOE_DEBUG_LEVEL >= 2
299 " with %zu octets (%zu). %u frames queued.\n",
301 last_fragment ?
"" :
"+", current_size, complete_offset,
305 #if EOE_DEBUG_LEVEL >= 3
307 for (i = 0; i < current_size; i++) {
308 printk(KERN_CONT
"%02X ",
310 if ((i + 1) % 16 == 0) {
311 printk(KERN_CONT
"\n");
315 printk(KERN_CONT
"\n");
319 EC_MBOX_TYPE_EOE, current_size + 4);
321 return PTR_ERR(data);
326 (complete_offset & 0x3F) << 6 |
435 eoe->
stats.rx_errors++;
436 #if EOE_DEBUG_LEVEL >= 1
438 " check datagram for %s.\n", eoe->
dev->name);
465 size_t rec_size, data_size;
466 uint8_t *data, frame_type, last_fragment, time_appended, mbox_prot;
467 uint8_t fragment_offset, fragment_number;
468 #if EOE_DEBUG_LEVEL >= 2
469 uint8_t frame_number;
472 #if EOE_DEBUG_LEVEL >= 3
477 eoe->
stats.rx_errors++;
478 #if EOE_DEBUG_LEVEL >= 1
480 " fetch datagram for %s.\n", eoe->
dev->name);
487 &mbox_prot, &rec_size);
489 eoe->
stats.rx_errors++;
490 #if EOE_DEBUG_LEVEL >= 1
498 if (mbox_prot != EC_MBOX_TYPE_EOE) {
499 eoe->
stats.rx_errors++;
500 #if EOE_DEBUG_LEVEL >= 1
510 if (frame_type != 0x00) {
511 #if EOE_DEBUG_LEVEL >= 1
513 " Dropping.\n", eoe->
dev->name);
515 eoe->
stats.rx_dropped++;
525 fragment_offset = (
EC_READ_U16(data + 2) >> 6) & 0x003F;
526 #if EOE_DEBUG_LEVEL >= 2
527 frame_number = (
EC_READ_U16(data + 2) >> 12) & 0x000F;
530 #if EOE_DEBUG_LEVEL >= 2
532 " frame %u%s, %u octets\n", eoe->
dev->name, fragment_number,
533 last_fragment ?
"" :
"+", fragment_offset, frame_number,
534 time_appended ?
", + timestamp" :
"",
535 time_appended ? rec_size - 8 : rec_size - 4);
538 #if EOE_DEBUG_LEVEL >= 3
540 for (i = 0; i < rec_size - 4; i++) {
541 printk(KERN_CONT
"%02X ", data[i + 4]);
542 if ((i + 1) % 16 == 0) {
543 printk(KERN_CONT
"\n");
547 printk(KERN_CONT
"\n");
550 data_size = time_appended ? rec_size - 8 : rec_size - 4;
552 if (!fragment_number) {
555 dev_kfree_skb(eoe->
rx_skb);
559 if (!(eoe->
rx_skb = dev_alloc_skb(fragment_offset * 32))) {
560 if (printk_ratelimit())
562 " frame dropped.\n");
563 eoe->
stats.rx_dropped++;
574 eoe->
stats.rx_dropped++;
579 offset = fragment_offset * 32;
583 dev_kfree_skb(eoe->
rx_skb);
585 eoe->
stats.rx_errors++;
586 #if EOE_DEBUG_LEVEL >= 1
596 memcpy(skb_put(eoe->
rx_skb, data_size), data + 4, data_size);
601 eoe->
stats.rx_packets++;
605 #if EOE_DEBUG_LEVEL >= 2
607 " with %u octets.\n", eoe->
dev->name, eoe->
rx_skb->len);
613 eoe->
rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
614 if (netif_rx(eoe->
rx_skb)) {
623 #if EOE_DEBUG_LEVEL >= 2
642 #if EOE_DEBUG_LEVEL >= 2
643 unsigned int wakeup = 0;
669 netif_wake_queue(eoe->
dev);
671 #if EOE_DEBUG_LEVEL >= 2
690 eoe->
stats.tx_errors++;
692 #if EOE_DEBUG_LEVEL >= 1
698 #if EOE_DEBUG_LEVEL >= 2
722 eoe->
stats.tx_errors++;
723 #if EOE_DEBUG_LEVEL >= 1
725 " datagram for %s after %u tries.\n",
738 eoe->
stats.tx_errors++;
739 #if EOE_DEBUG_LEVEL >= 1
741 " for %s after %u tries.\n",
751 eoe->
stats.tx_packets++;
764 eoe->
stats.tx_errors++;
765 #if EOE_DEBUG_LEVEL >= 1
788 netif_start_queue(dev);
790 #if EOE_DEBUG_LEVEL >= 2
806 netif_stop_queue(dev);
812 #if EOE_DEBUG_LEVEL >= 2
826 struct net_device *dev
835 " exceeds MTU. dropping.\n", skb->len);
837 eoe->
stats.tx_dropped++;
844 if (printk_ratelimit())
855 netif_stop_queue(dev);
860 #if EOE_DEBUG_LEVEL >= 2
862 " with %u octets (%u frames queued).\n",
878 struct net_device *dev
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
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.
uint16_t ring_position
Ring position.
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
uint32_t tx_counter
octets transmitted during last second
#define EC_EOE_TX_QUEUE_SIZE
Size of the EoE tx queue.
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.
static const struct net_device_ops ec_eoedev_ops
Device operations for EoE interfaces.
struct sk_buff * skb
socket buffer
uint16_t configured_tx_mailbox_size
Configured send mailbox size.
void ec_eoe_state_rx_fetch(ec_eoe_t *)
State: RX_FETCH.
struct list_head tx_queue
queue for frames to send
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
uint8_t tx_fragment_number
number of the fragment
int ec_eoe_send(ec_eoe_t *eoe)
Sends a frame or the next fragment.
ec_slave_t * slave
pointer to the corresponding slave
void ec_master_queue_datagram_ext(ec_master_t *master, ec_datagram_t *datagram)
Places a datagram in the non-application datagram queue.
OP (mailbox communication and input/output update)
unsigned int tx_queue_size
Transmit queue size.
size_t rx_skb_size
size of the allocated socket buffer memory
size_t tx_offset
number of octets sent
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.
void ec_eoe_state_tx_start(ec_eoe_t *)
State: TX START.
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
void ec_eoe_state_rx_check(ec_eoe_t *)
State: RX_CHECK.
unsigned int tx_queue_active
kernel netif queue started
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
uint16_t working_counter
Working counter.
int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
EoE constructor.
uint8_t link_state
device link state
#define EC_EOE_TRIES
Number of tries.
Sent (still in the queue).
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
uint32_t tx_rate
transmit rate (bps)
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
Global definitions and macros.
uint8_t rx_expected_fragment
next expected fragment number
void(* state)(ec_eoe_t *)
state function for the state machine
EtherCAT master structure.
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
Ethernet over EtherCAT (EoE)
ec_datagram_state_t state
State.
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
unsigned long rate_jiffies
time of last rate output
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
ec_master_t * master
Master owning the slave.
struct list_head queue
list item
void ec_eoe_state_tx_sent(ec_eoe_t *)
State: TX SENT.
unsigned int opened
net_device is opened
off_t rx_skb_offset
current write pointer in the socket buffer
ec_datagram_t datagram
datagram
struct net_device_stats stats
device statistics
uint16_t effective_alias
Effective alias address.
int ec_slave_mbox_prepare_check(const ec_slave_t *slave, ec_datagram_t *datagram)
Prepares a datagram for checking the mailbox state.
unsigned int tx_idle
Idle flag.
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
uint32_t rx_rate
receive rate (bps)
struct sk_buff * rx_skb
current rx socket buffer
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
uint8_t tx_frame_number
number of the transmitted frame
uint32_t rx_counter
octets received during last second
void ec_slave_request_state(ec_slave_t *slave, ec_slave_state_t state)
Request a slave state and resets the error flag.
uint16_t configured_rx_mailbox_size
Configured receive mailbox size.
int ec_eoedev_open(struct net_device *)
Opens the virtual network device.
struct semaphore tx_queue_sem
Semaphore for the send queue.
ec_eoe_frame_t * tx_frame
current TX frame
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
PREOP state (mailbox communication, no IO)
Ethernet over EtherCAT (EoE) handler.
unsigned int error_flag
Stop processing after an error.
unsigned int rx_idle
Idle flag.
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
int ec_slave_mbox_check(const ec_datagram_t *datagram)
Processes a mailbox state checking datagram.
struct net_device * dev
net_device for virtual ethernet device
unsigned int queue_datagram
the datagram is ready for queuing
unsigned int tx_queued_frames
number of frames in the queue
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.