29 #include <linux/module.h> 30 #include <linux/skbuff.h> 31 #include <linux/if_ether.h> 32 #include <linux/netdevice.h> 38 #define timersub(a, b, result) \ 40 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ 41 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ 42 if ((result)->tv_usec < 0) { \ 44 (result)->tv_usec += 1000000; \ 84 device->cycles_poll = 0;
87 device->timeval_poll.tv_sec = 0;
88 device->timeval_poll.tv_usec = 0;
95 for (i = 0; i < EC_DEBUG_RING_SIZE; i++) {
96 ec_debug_frame_t *df = &device->debug_frames[i];
105 device->debug_frame_index = 0;
106 device->debug_frame_count = 0;
117 sprintf(ifname,
"ecdbg%c%u", mb, master->
index);
127 if (!(device->
tx_skb[i] = dev_alloc_skb(ETH_FRAME_LEN + EXTRA_HEADROOM))) {
128 EC_MASTER_ERR(master,
"Error allocating device socket buffer!\n");
134 skb_reserve(device->
tx_skb[i], ETH_HLEN + EXTRA_HEADROOM);
135 eth = (
struct ethhdr *) skb_push(device->
tx_skb[i], ETH_HLEN);
136 eth->h_proto = htons(0x88A4);
137 memset(eth->h_dest, 0xFF, ETH_ALEN);
145 dev_kfree_skb(device->
tx_skb[i]);
169 dev_kfree_skb(device->
tx_skb[i]);
181 struct net_device *net_dev,
183 struct module *module
191 device->
dev = net_dev;
196 device->
tx_skb[i]->dev = net_dev;
197 eth = (
struct ethhdr *) (device->
tx_skb[i]->data);
198 memcpy(eth->h_source, net_dev->dev_addr, ETH_ALEN);
229 device->
tx_skb[i]->dev = NULL;
259 ret = device->
dev->netdev_ops->ndo_open(device->
dev);
288 ret = device->
dev->netdev_ops->ndo_stop(device->
dev);
328 skb->len = ETH_HLEN + size;
336 if (device->
dev->netdev_ops->ndo_start_xmit(skb, device->
dev) ==
341 device->
tx_bytes += ETH_HLEN + size;
347 ec_device_debug_ring_append(
348 device, TX, skb->data + ETH_HLEN, size);
389 void ec_device_debug_ring_append(
391 ec_debug_frame_dir_t dir,
396 ec_debug_frame_t *df = &device->debug_frames[device->debug_frame_index];
400 do_gettimeofday(&df->t);
403 df->t = device->timeval_poll;
405 memcpy(df->data, data, size);
406 df->data_size = size;
408 device->debug_frame_index++;
409 device->debug_frame_index %= EC_DEBUG_RING_SIZE;
410 if (unlikely(device->debug_frame_count < EC_DEBUG_RING_SIZE))
411 device->debug_frame_count++;
418 void ec_device_debug_ring_print(
423 unsigned int ring_index;
424 const ec_debug_frame_t *df;
425 struct timeval t0, diff;
428 ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE - 1)
429 % EC_DEBUG_RING_SIZE;
430 t0 = device->debug_frames[ring_index].t;
435 ring_index = (device->debug_frame_index + EC_DEBUG_RING_SIZE
436 - device->debug_frame_count) % EC_DEBUG_RING_SIZE;
438 for (i = 0; i < device->debug_frame_count; i++) {
439 df = &device->debug_frames[ring_index];
440 timersub(&t0, &df->t, &diff);
443 i + 1 - device->debug_frame_count,
444 (
unsigned int) diff.tv_sec,
445 (
unsigned int) diff.tv_usec,
446 (df->dir == TX) ?
"TX" :
"RX");
450 ring_index %= EC_DEBUG_RING_SIZE;
467 #ifdef EC_HAVE_CYCLES 468 device->cycles_poll = get_cycles();
472 do_gettimeofday(&device->timeval_poll);
531 char dev_str[20], mac_str[20];
536 sprintf(dev_str,
"main");
538 sprintf(dev_str,
"backup");
542 sprintf(dev_str,
"UNKNOWN");
545 EC_MASTER_INFO(master,
"Releasing %s device %s.\n", dev_str, mac_str);
563 unsigned int all_open = 1, dev_idx;
567 EC_MASTER_ERR(master,
"Failed to open device: error %d!\n", ret);
627 const void *ec_data = data + ETH_HLEN;
628 size_t ec_size = size - ETH_HLEN;
630 if (unlikely(!data)) {
650 ec_device_debug_ring_append(device, RX, ec_data, ec_size);
670 if (unlikely(!device)) {
671 EC_WARN(
"ecdev_set_link() called with null device!\n");
678 "Link state of %s changed to %s.\n",
679 device->
dev->name, (state ?
"UP" :
"DOWN"));
695 if (unlikely(!device)) {
696 EC_WARN(
"ecdev_get_link() called with null device!\n");
#define EC_WARN(fmt, args...)
Convenience macro for printing EtherCAT-specific warnings to syslog.
struct sk_buff * tx_skb[EC_TX_RING_SIZE]
transmit skb ring
u64 tx_count
Number of frames sent.
const unsigned int rate_intervals[]
List of intervals for statistics [s].
void ecdev_close(ec_device_t *device)
Makes the master leave IDLE phase and closes the network device.
ec_pollfunc_t poll
pointer to the device's poll function
void ec_debug_clear(ec_debug_t *dbg)
Debug interface destructor.
unsigned long jiffies_poll
jiffies of last poll
u64 last_tx_count
Number of frames sent of last statistics cycle.
#define ec_master_num_devices(MASTER)
Number of Ethernet devices.
#define EC_RATE_COUNT
Number of statistic rate intervals to maintain.
int ecdev_open(ec_device_t *device)
Opens the network device and makes the master enter IDLE phase.
struct module * module
pointer to the device's owning module
void ec_device_clear(ec_device_t *device)
Destructor.
int ec_device_open(ec_device_t *device)
Opens the EtherCAT device.
s32 tx_frame_rates[EC_RATE_COUNT]
Transmit rates in frames/s for different statistics cycle periods.
void ec_device_update_stats(ec_device_t *device)
Update device statistics.
uint8_t link_state
device link state
u64 rx_count
Number of frames received.
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
int ec_master_enter_idle_phase(ec_master_t *master)
Transition function from ORPHANED to IDLE phase.
s32 rx_frame_rates[EC_RATE_COUNT]
Receive rates in frames/s for different statistics cycle periods.
EtherCAT master structure.
void ec_debug_unregister(ec_debug_t *dbg)
Unregister debug interface.
void ec_device_send(ec_device_t *device, size_t size)
Sends the content of the transmit socket buffer.
#define EC_MASTER_DBG(master, level, fmt, args...)
Convenience macro for printing master-specific debug messages to syslog.
#define EC_TX_RING_SIZE
Size of the transmit ring.
ec_device_stats_t device_stats
Device statistics.
u64 last_rx_count
Number of frames received of last statistics cycle.
ec_master_phase_t phase
Master phase.
void(* ec_pollfunc_t)(struct net_device *)
Device poll function type.
struct semaphore device_sem
Device semaphore.
s32 rx_byte_rates[EC_RATE_COUNT]
Receive rates in byte/s for different statistics cycle periods.
unsigned int tx_ring_index
last ring entry used to transmit
unsigned int debug_level
Master debug level.
void ec_master_leave_idle_phase(ec_master_t *master)
Transition function from IDLE to ORPHANED phase.
s32 tx_byte_rates[EC_RATE_COUNT]
Transmit rates in byte/s for different statistics cycle periods.
int ec_device_init(ec_device_t *device, ec_master_t *master)
Constructor.
#define EC_MASTER_WARN(master, fmt, args...)
Convenience macro for printing master-specific warnings to syslog.
ec_master_t * master
EtherCAT master.
u64 rx_bytes
Number of bytes received.
u64 tx_count
Number of frames sent.
uint8_t ecdev_get_link(const ec_device_t *device)
Reads the link state.
#define EC_MASTER_ERR(master, fmt, args...)
Convenience macro for printing master-specific errors to syslog.
void ec_debug_send(ec_debug_t *dbg, const uint8_t *data, size_t size)
Sends frame data to the interface.
void ec_debug_register(ec_debug_t *dbg, const struct net_device *net_dev)
Register debug interface.
int ec_debug_init(ec_debug_t *dbg, ec_device_t *device, const char *name)
Debug interface constructor.
uint8_t * ec_device_tx_data(ec_device_t *device)
Returns a pointer to the device's transmit memory.
void ec_print_data(const uint8_t *, size_t)
Outputs frame contents for debugging purposes.
uint8_t open
true, if the net_device has been opened
u64 tx_errors
Number of transmit errors.
ssize_t ec_mac_print(const uint8_t *, char *)
Print a MAC address to a buffer.
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
u64 tx_bytes
Number of bytes sent.
void ecdev_receive(ec_device_t *device, const void *data, size_t size)
Accepts a received frame.
int ec_device_close(ec_device_t *device)
Stops the EtherCAT device.
u64 rx_count
Number of frames received.
void ecdev_set_link(ec_device_t *device, uint8_t state)
Sets a new link state.
void ec_device_attach(ec_device_t *device, struct net_device *net_dev, ec_pollfunc_t poll, struct module *module)
Associate with net_device.
void ec_device_detach(ec_device_t *device)
Disconnect from net_device.
EtherCAT device structure.
struct net_device * dev
pointer to the assigned net_device
void ec_device_poll(ec_device_t *device)
Calls the poll function of the assigned net_device.
void ecdev_withdraw(ec_device_t *device)
Withdraws an EtherCAT device from the master.
u64 rx_bytes
Number of bytes received.
#define EC_MASTER_INFO(master, fmt, args...)
Convenience macro for printing master-specific information to syslog.
void ec_master_receive_datagrams(ec_master_t *master, ec_device_t *device, const uint8_t *frame_data, size_t size)
Processes a received frame.
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
u64 tx_bytes
Number of bytes sent.
void ec_device_clear_stats(ec_device_t *device)
Clears the frame statistics.
#define EC_MAX_DATA_SIZE
Resulting maximum data size of a single datagram in a frame.