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>
41#if defined(CONFIG_SUSE_KERNEL) && \
42 LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
43#include <linux/suse_version.h>
46# define SUSE_VERSION 0
48# ifndef SUSE_PATCHLEVEL
49# define SUSE_PATCHLEVEL 0
62#define EOE_DEBUG_LEVEL 1
66#define EC_EOE_TX_QUEUE_SIZE 100
70#define EC_EOE_TRIES 100
119 u8 mac_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
140 memset(&eoe->
stats, 0,
sizeof(
struct net_device_stats));
162#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
163 eoe->
dev = alloc_netdev(
sizeof(
ec_eoe_t *), name, NET_NAME_UNKNOWN,
166 eoe->
dev = alloc_netdev(
sizeof(
ec_eoe_t *), name, ether_setup);
170 " for EoE handler!\n", name);
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);
182 memcpy(eoe->
dev->dev_addr, mac_addr,
sizeof(mac_addr));
186 priv = netdev_priv(eoe->
dev);
198 ret = register_netdev(eoe->
dev);
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);
211 memcpy(eoe->
dev->dev_addr, mac_addr,
sizeof(mac_addr));
217 free_netdev(eoe->
dev);
231 unregister_netdev(eoe->
dev);
242 dev_kfree_skb(eoe->
rx_skb);
244 free_netdev(eoe->
dev);
257 struct list_head tx_queue;
259 netif_tx_lock_bh(eoe->
dev);
261 list_replace_init(&eoe->
tx_queue, &tx_queue);
264 netif_tx_unlock_bh(eoe->
dev);
266 list_for_each_entry_safe(frame, next, &tx_queue, queue) {
267 list_del(&frame->
queue);
268 dev_kfree_skb(frame->
skb);
281 size_t remaining_size, current_size, complete_offset;
282 unsigned int last_fragment;
284#if EOE_DEBUG_LEVEL >= 3
290 if (remaining_size <= eoe->slave->configured_tx_mailbox_size - 10) {
291 current_size = remaining_size;
304 complete_offset = remaining_size / 32 + 1;
307#if EOE_DEBUG_LEVEL >= 2
309 " with %zu octets (%zu). %u frames queued.\n",
311 last_fragment ?
"" :
"+", current_size, complete_offset,
315#if EOE_DEBUG_LEVEL >= 3
317 for (i = 0; i < current_size; i++) {
318 printk(KERN_CONT
"%02X ",
320 if ((i + 1) % 16 == 0) {
321 printk(KERN_CONT
"\n");
325 printk(KERN_CONT
"\n");
329 EC_MBOX_TYPE_EOE, current_size + 4);
331 return PTR_ERR(data);
336 (complete_offset & 0x3F) << 6 |
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;
477#if EOE_DEBUG_LEVEL >= 3
498 eoe->
stats.rx_errors++;
499#if EOE_DEBUG_LEVEL >= 1
501 " data for %s.\n", eoe->
dev->name);
508 &mbox_prot, &rec_size);
510 eoe->
stats.rx_errors++;
511#if EOE_DEBUG_LEVEL >= 1
519 if (mbox_prot != EC_MBOX_TYPE_EOE) {
520 eoe->
stats.rx_errors++;
521#if EOE_DEBUG_LEVEL >= 1
531 if (frame_type != EC_EOE_FRAMETYPE_INIT_REQ) {
532#if EOE_DEBUG_LEVEL >= 1
534 " Dropping.\n", eoe->
dev->name);
536 eoe->
stats.rx_dropped++;
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;
551 data_size = time_appended ? rec_size - 8 : rec_size - 4;
553#if EOE_DEBUG_LEVEL >= 2
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);
560#if EOE_DEBUG_LEVEL >= 3
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");
569 printk(KERN_CONT
"\n");
572 if (!fragment_number) {
573 size_t complete_size = size_or_offset * 32;
577 dev_kfree_skb(eoe->
rx_skb);
581 if (!complete_size || data_size > complete_size) {
583 " (%zu of %zu bytes)!\n", data_size, complete_size);
584 eoe->
stats.rx_errors++;
590 if (!(eoe->
rx_skb = dev_alloc_skb(complete_size))) {
591 if (printk_ratelimit()) {
593 " frame dropped.\n");
595 eoe->
stats.rx_dropped++;
605 off_t offset = size_or_offset * 32;
608 eoe->
stats.rx_dropped++;
616 dev_kfree_skb(eoe->
rx_skb);
618 eoe->
stats.rx_errors++;
619#if EOE_DEBUG_LEVEL >= 1
629 memcpy(skb_put(eoe->
rx_skb, data_size), data + 4, data_size);
634 eoe->
stats.rx_packets++;
638#if EOE_DEBUG_LEVEL >= 2
640 " with %u octets.\n", eoe->
dev->name, eoe->
rx_skb->len);
646 eoe->
rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
647 if (netif_rx(eoe->
rx_skb)) {
656#if EOE_DEBUG_LEVEL >= 2
675#if EOE_DEBUG_LEVEL >= 2
676 unsigned int wakeup = 0;
686 netif_tx_lock_bh(eoe->
dev);
689 netif_tx_unlock_bh(eoe->
dev);
702 netif_wake_queue(eoe->
dev);
704#if EOE_DEBUG_LEVEL >= 2
710 netif_tx_unlock_bh(eoe->
dev);
723 eoe->
stats.tx_errors++;
725#if EOE_DEBUG_LEVEL >= 1
731#if EOE_DEBUG_LEVEL >= 2
755 eoe->
stats.tx_errors++;
756#if EOE_DEBUG_LEVEL >= 1
758 " datagram for %s after %u tries.\n",
771 eoe->
stats.tx_errors++;
772#if EOE_DEBUG_LEVEL >= 1
774 " for %s after %u tries.\n",
784 eoe->
stats.tx_packets++;
797 eoe->
stats.tx_errors++;
798#if EOE_DEBUG_LEVEL >= 1
821 netif_start_queue(dev);
823#if EOE_DEBUG_LEVEL >= 2
838 netif_stop_queue(dev);
844#if EOE_DEBUG_LEVEL >= 2
857 struct net_device *dev
866 " exceeds MTU. dropping.\n", skb->len);
868 eoe->
stats.tx_dropped++;
873 WARN_ON_ONCE(skb_get_queue_mapping(skb) != 0);
874 lockdep_assert_held(&netdev_get_tx_queue(dev, 0)->_xmit_lock);
878 if (printk_ratelimit())
888 netif_stop_queue(dev);
892#if EOE_DEBUG_LEVEL >= 2
894 " with %u octets (%u frames queued).\n",
910 struct net_device *dev
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
@ EC_DATAGRAM_UNUSED
Not used by its state machine in this cycle and not to be queued.
@ EC_DATAGRAM_RECEIVED
Received (dequeued).
@ EC_DATAGRAM_SENT
Sent (still in the queue).
@ EC_DATAGRAM_QUEUED
Queued for sending.
void ec_eoe_state_rx_start(ec_eoe_t *)
State: RX_START.
#define EC_EOE_TRIES
Number of tries.
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
int ec_eoedev_tx(struct sk_buff *, struct net_device *)
Transmits data via the virtual network device.
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
void ec_eoe_state_tx_start(ec_eoe_t *)
State: TX START.
int ec_eoe_holds_mbox_lock(const ec_eoe_t *eoe)
Returns, if the handler holds the mailbox read lock of its slave.
int ec_eoe_send(ec_eoe_t *)
Sends a frame or the next fragment.
struct net_device_stats * ec_eoedev_stats(struct net_device *)
Gets statistics about the virtual network device.
static const struct net_device_ops ec_eoedev_ops
Device operations for EoE interfaces.
int ec_eoedev_open(struct net_device *)
Opens the virtual network device.
#define EC_EOE_TX_QUEUE_SIZE
Size of the EoE tx queue.
void ec_eoe_state_rx_fetch(ec_eoe_t *)
State: RX_FETCH.
int ec_eoe_init(ec_eoe_t *eoe, ec_slave_t *slave)
EoE constructor.
void ec_eoe_flush(ec_eoe_t *)
Empties the transmit queue.
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
void ec_eoe_state_tx_sent(ec_eoe_t *)
State: TX SENT.
int ec_eoedev_stop(struct net_device *)
Stops the virtual network device.
Ethernet over EtherCAT (EoE).
void ec_fsm_mbox_clear(ec_fsm_mbox_t *fsm)
Destructor.
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.
int ec_fsm_mbox_exec(ec_fsm_mbox_t *fsm, ec_datagram_t *datagram)
Executes the current state of the state machine.
int ec_fsm_mbox_holds_lock(const ec_fsm_mbox_t *fsm)
Checks, if the state machine holds the slave's mailbox read lock.
int ec_fsm_mbox_result(const ec_fsm_mbox_t *fsm)
Returns the result of the terminated state machine.
void ec_fsm_mbox_abort(ec_fsm_mbox_t *fsm)
Aborts the state machine and releases the mailbox read lock, if held.
void ec_fsm_mbox_init(ec_fsm_mbox_t *fsm)
Constructor.
Global definitions and macros.
struct ec_slave ec_slave_t
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
@ EC_DEVICE_MAIN
Main device.
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
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.
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.
void ec_master_queue_datagram_ext(ec_master_t *master, ec_datagram_t *datagram)
Places a datagram in the non-application datagram queue.
EtherCAT master structure.
EtherCAT slave structure.
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
uint16_t working_counter
Working counter.
ec_datagram_state_t state
State.
ec_device_index_t device_index
Device via which the datagram shall be / was sent.
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
uint8_t link_state
device link state
struct list_head queue
list item
struct sk_buff * skb
socket buffer
uint32_t rx_rate
receive rate (bps)
unsigned int tx_queue_active
kernel netif queue started
unsigned int opened
net_device is opened
unsigned int queue_datagram
the datagram is ready for queuing
unsigned int tx_queued_frames
number of frames in the queue
uint8_t tx_fragment_number
number of the fragment
ec_slave_t * slave
pointer to the corresponding slave
size_t rx_skb_size
size of the allocated socket buffer memory
unsigned int tx_idle
Idle flag.
ec_datagram_t datagram
datagram
ec_eoe_frame_t * tx_frame
current TX frame
off_t rx_skb_offset
current write pointer in the socket buffer
size_t tx_offset
number of octets sent
struct net_device_stats stats
device statistics
uint8_t rx_expected_fragment
next expected fragment number
uint32_t rx_counter
octets received during last second
ec_fsm_mbox_t fsm_mbox
Mailbox receive state machine.
struct net_device * dev
net_device for virtual ethernet device
uint8_t tx_frame_number
number of the transmitted frame
uint32_t tx_counter
octets transmitted during last second
struct sk_buff * rx_skb
current rx socket buffer
unsigned long rate_jiffies
time of last rate output
uint32_t tx_rate
transmit rate (bps)
unsigned int tx_queue_size
Transmit queue size.
unsigned int rx_idle
Idle flag.
void(*) state(ec_eoe_t *)
state function for the state machine
struct list_head tx_queue
queue for frames to send
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
uint16_t configured_rx_mailbox_size
Configured receive mailbox size.
uint16_t ring_position
Ring position.
ec_mbox_data_t mbox_eoe_data
Received mailbox data for EoE.
uint16_t configured_tx_mailbox_size
Configured send mailbox size.
uint16_t effective_alias
Effective alias address.
ec_device_index_t device_index
Index of device the slave responds on.
ec_master_t * master
Master owning the slave.
unsigned int error_flag
Stop processing after an error.