IgH EtherCAT Master  1.6.13
master.h
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#ifndef __EC_MASTER_H__
30#define __EC_MASTER_H__
31
32#include <linux/version.h>
33#include <linux/irq_work.h>
34#include <linux/list.h>
35#include <linux/timer.h>
36#include <linux/wait.h>
37#include <linux/kthread.h>
38#include <linux/rtmutex.h>
39#include <linux/workqueue.h>
40
41#include "device.h"
42#include "domain.h"
43#include "ethernet.h"
44#include "fsm_master.h"
45#include "cdev.h"
46
47#ifdef EC_RTDM
48#include "rtdm.h"
49#endif
50
51/****************************************************************************/
52
62#define EC_MASTER_INFO(master, fmt, args...) \
63 printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
64
74#define EC_MASTER_ERR(master, fmt, args...) \
75 printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
76
86#define EC_MASTER_WARN(master, fmt, args...) \
87 printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
88
100#define EC_MASTER_DBG(master, level, fmt, args...) \
101 do { \
102 if (master->debug_level >= level) { \
103 printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
104 master->index, ##args); \
105 } \
106 } while (0)
107
108
113#define EC_EXT_RING_SIZE 32
114
117#define EC_MAX_MASTERS 32
118
119/****************************************************************************/
120
131
132/****************************************************************************/
133
136typedef struct {
137 unsigned int timeouts;
138 unsigned int corrupted;
139 unsigned int unmatched;
141 unsigned long output_jiffies;
142} ec_stats_t;
143
144/****************************************************************************/
145
174
175/****************************************************************************/
176
177#if EC_MAX_NUM_DEVICES < 1
178#error Invalid number of devices
179#endif
180
181/****************************************************************************/
182
183// Support old kernels by providing a fallback for `strscpy`
184#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
185#define strscpy strncpy
186#endif
187
188/****************************************************************************/
189
194struct ec_master {
195 unsigned int index;
196 unsigned int reserved;
197
199 struct device *class_device;
200
201#ifdef EC_RTDM
202 ec_rtdm_dev_t rtdm_dev;
203#endif
204
205 struct semaphore master_sem;
206
207 ec_device_t devices[EC_MAX_NUM_DEVICES];
208 const uint8_t *macs[EC_MAX_NUM_DEVICES];
209#if EC_MAX_NUM_DEVICES > 1
210 unsigned int num_devices;
213#endif
214 struct semaphore device_sem;
216
220 unsigned int active;
221 unsigned int config_changed;
222 unsigned int injection_seq_fsm;
224 unsigned int injection_seq_rt;
226
228 unsigned int slave_count;
229
230 /* Configuration applied by the application. */
231 struct list_head configs;
232 struct list_head domains;
233
245
246 unsigned int scan_busy;
247 unsigned int scan_index;
248 unsigned int allow_scan;
249 struct semaphore scan_sem;
251 wait_queue_head_t scan_queue;
253
254 unsigned int config_busy;
255 struct semaphore config_sem;
257 wait_queue_head_t config_queue;
259
260 struct list_head datagram_queue;
262
263 struct list_head ext_datagram_queue;
265 struct semaphore ext_queue_sem;
267
270 unsigned int ext_ring_idx_rt;
272 unsigned int ext_ring_idx_fsm;
274 unsigned int send_interval;
277
279 struct list_head fsm_exec_list;
280 unsigned int fsm_exec_count;
281
282 unsigned int debug_level;
283 unsigned int run_on_cpu;
285
286 struct task_struct *thread;
287
288#ifdef EC_EOE
289 struct task_struct *eoe_thread;
290 struct list_head eoe_handlers;
291#endif
292
293 struct rt_mutex io_mutex;
294
295 void (*send_cb)(void *);
296 void (*receive_cb)(void *);
297 void *cb_data;
298 void (*app_send_cb)(void *);
300 void (*app_receive_cb)(void *);
303
304 struct list_head sii_requests;
305 struct list_head emerg_reg_requests;
307
308 wait_queue_head_t request_queue;
310 struct work_struct sc_reset_work;
312 struct irq_work sc_reset_work_kicker;
314};
315
316/****************************************************************************/
317
318// static funtions
319void ec_master_init_static(void);
320
321// master creation/deletion
322int ec_master_init(ec_master_t *, unsigned int, const uint8_t *,
323 const uint8_t *, dev_t, struct class *, unsigned int, unsigned int);
325
328#if EC_MAX_NUM_DEVICES > 1
329#define ec_master_num_devices(MASTER) ((MASTER)->num_devices)
330#else
331#define ec_master_num_devices(MASTER) 1
332#endif
333
334// phase transitions
339
340#ifdef EC_EOE
341// EoE
344#endif
345
346// datagram IO
348 const uint8_t *, size_t);
351
352// misc.
353void ec_master_set_send_interval(ec_master_t *, unsigned int);
355ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
356const ec_slave_t *ec_master_find_slave_const(const ec_master_t *, uint16_t,
357 uint16_t);
359#ifdef EC_EOE
361#endif
363
364unsigned int ec_master_config_count(const ec_master_t *);
366 const ec_master_t *, unsigned int);
368 const ec_master_t *, unsigned int);
369unsigned int ec_master_domain_count(const ec_master_t *);
372 unsigned int);
373#ifdef EC_EOE
376 uint16_t);
377#endif
378
379int ec_master_debug_level(ec_master_t *, unsigned int);
380
383 uint16_t, uint32_t, uint32_t);
384
387
388void ec_master_internal_send_cb(void *);
390
391extern const unsigned int rate_intervals[EC_RATE_COUNT]; // see master.c
392
393/****************************************************************************/
394
395#endif
EtherCAT master character device.
EtherCAT device structure.
EtherCAT domain structure.
struct ec_device ec_device_t
Definition ecdev.h:45
Ethernet over EtherCAT (EoE).
struct ec_eoe ec_eoe_t
Definition ethernet.h:66
EtherCAT master state machine.
struct ec_fsm_master ec_fsm_master_t
Definition fsm_master.h:56
#define EC_RATE_COUNT
Number of statistic rate intervals to maintain.
Definition globals.h:60
struct ec_slave ec_slave_t
Definition globals.h:309
struct ec_master ec_master_t
Definition ecrt.h:300
struct ec_domain ec_domain_t
Definition ecrt.h:306
struct ec_slave_config ec_slave_config_t
Definition ecrt.h:303
ec_domain_t * ecrt_master_create_domain_err(ec_master_t *master)
Same as ecrt_master_create_domain(), but with ERR_PTR() return value.
Definition master.c:2255
void ec_master_request_op(ec_master_t *master)
Request OP state for configured slaves.
Definition master.c:2221
void ec_master_leave_idle_phase(ec_master_t *master)
Transition function from IDLE to ORPHANED phase.
Definition master.c:681
const unsigned int rate_intervals[]
List of intervals for statistics [s].
Definition master.c:105
void ec_master_init_static(void)
Static variables initializer.
Definition master.c:139
uint16_t ec_master_eoe_handler_count(const ec_master_t *master)
Get the number of EoE handlers.
Definition master.c:1991
void ec_master_set_send_interval(ec_master_t *master, unsigned int send_interval)
Sets the expected interval between calls to ecrt_master_send and calculates the maximum amount of dat...
Definition master.c:912
int ec_master_init(ec_master_t *master, unsigned int index, const uint8_t *main_mac, const uint8_t *backup_mac, dev_t device_number, struct class *class, unsigned int debug_level, unsigned int run_on_cpu)
Master constructor.
Definition master.c:160
ec_slave_config_t * ecrt_master_slave_config_err(ec_master_t *master, uint16_t alias, uint16_t position, uint32_t vendor_id, uint32_t product_code)
Same as ecrt_master_slave_config(), but with ERR_PTR() return value.
Definition master.c:2568
int ec_master_enter_operation_phase(ec_master_t *master)
Transition function from IDLE to OPERATION phase.
Definition master.c:705
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:981
void ec_master_queue_datagram(ec_master_t *master, ec_datagram_t *datagram)
Places a datagram in the datagram queue.
Definition master.c:948
void ec_master_eoe_start(ec_master_t *master)
Starts Ethernet over EtherCAT processing on demand.
Definition master.c:1678
const ec_domain_t * ec_master_find_domain_const(const ec_master_t *master, unsigned int index)
Get a domain via its position in the list.
Definition master.c:1974
const ec_eoe_t * ec_master_get_eoe_handler_const(const ec_master_t *master, uint16_t index)
Get an EoE handler via its position in the list.
Definition master.c:2013
ec_domain_t * ec_master_find_domain(ec_master_t *master, unsigned int index)
Get a domain via its position in the list.
Definition master.c:1959
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.
Definition master.c:1133
int ec_master_enter_idle_phase(ec_master_t *master)
Transition function from ORPHANED to IDLE phase.
Definition master.c:648
int ec_master_debug_level(ec_master_t *master, unsigned int level)
Set the debug level.
Definition master.c:2038
ec_slave_t * ec_master_find_slave(ec_master_t *master, uint16_t alias, uint16_t position)
Finds a slave in the bus, given the alias and position.
Definition master.c:1830
const ec_slave_config_t * ec_master_get_config_const(const ec_master_t *master, unsigned int pos)
Get a slave configuration via its position in the list.
Definition master.c:1910
const ec_slave_t * ec_master_find_slave_const(const ec_master_t *master, uint16_t alias, uint16_t position)
Finds a slave in the bus, given the alias and position.
Definition master.c:1846
void ec_master_attach_slave_configs(ec_master_t *master)
Attaches the slave configurations to the slaves.
Definition master.c:1790
void ec_master_calc_dc(ec_master_t *master)
Distributed-clocks calculations.
Definition master.c:2204
void ec_master_clear_eoe_handlers(ec_master_t *master)
Clear and free all EoE handlers.
Definition master.c:446
void ec_master_clear_slaves(ec_master_t *master)
Clear all slaves.
Definition master.c:482
void ec_master_clear(ec_master_t *master)
Destructor.
Definition master.c:400
unsigned int ec_master_config_count(const ec_master_t *master)
Get the number of slave configurations provided by the application.
Definition master.c:1862
void ec_master_internal_send_cb(void *cb_data)
Internal sending callback.
Definition master.c:553
void ec_master_output_stats(ec_master_t *master)
Output master statistics.
Definition master.c:1275
void ec_master_eoe_stop(ec_master_t *master)
Stops the Ethernet over EtherCAT processing.
Definition master.c:1712
void ec_master_leave_operation_phase(ec_master_t *master)
Transition function from OPERATION to IDLE phase.
Definition master.c:776
unsigned int ec_master_domain_count(const ec_master_t *master)
Get the number of domains.
Definition master.c:1925
ec_slave_config_t * ec_master_get_config(const ec_master_t *master, unsigned int pos)
Get a slave configuration via its position in the list.
Definition master.c:1895
void ec_master_internal_receive_cb(void *cb_data)
Internal receiving callback.
Definition master.c:568
#define EC_EXT_RING_SIZE
Size of the external datagram ring.
Definition master.h:113
ec_master_phase_t
EtherCAT master phase.
Definition master.h:123
@ EC_IDLE
Idle phase.
Definition master.h:126
@ EC_ORPHANED
Orphaned phase.
Definition master.h:124
@ EC_OPERATION
Operation phase.
Definition master.h:128
RTDM interface.
struct ec_rtdm_dev ec_rtdm_dev_t
EtherCAT RTDM device.
EtherCAT master character device.
Definition cdev.h:41
EtherCAT datagram.
Definition datagram.h:79
Device statistics.
Definition master.h:148
s32 rx_byte_rates[EC_RATE_COUNT]
Receive rates in byte/s for different statistics cycle periods.
Definition master.h:168
s32 loss_rates[EC_RATE_COUNT]
Frame loss rates for different statistics cycle periods.
Definition master.h:170
u64 tx_count
Number of frames sent.
Definition master.h:149
s32 rx_frame_rates[EC_RATE_COUNT]
Receive rates in frames/s for different statistics cycle periods.
Definition master.h:163
u64 rx_bytes
Number of bytes received.
Definition master.h:156
u64 last_loss
Tx/Rx difference of last statistics cycle.
Definition master.h:159
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
Definition master.h:155
u64 last_tx_count
Number of frames sent of last statistics cycle.
Definition master.h:150
s32 tx_byte_rates[EC_RATE_COUNT]
Transmit rates in byte/s for different statistics cycle periods.
Definition master.h:166
u64 tx_bytes
Number of bytes sent.
Definition master.h:154
u64 rx_count
Number of frames received.
Definition master.h:151
s32 tx_frame_rates[EC_RATE_COUNT]
Transmit rates in frames/s for different statistics cycle periods.
Definition master.h:160
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
Definition master.h:157
u64 last_rx_count
Number of frames received of last statistics cycle.
Definition master.h:152
unsigned long jiffies
Jiffies of last statistic cycle.
Definition master.h:172
EtherCAT master.
Definition master.h:194
struct list_head datagram_queue
Datagram queue.
Definition master.h:260
struct task_struct * eoe_thread
EoE thread.
Definition master.h:289
struct work_struct sc_reset_work
Task to reset slave configuration.
Definition master.h:310
unsigned int injection_seq_fsm
Datagram injection sequence number for the FSM side.
Definition master.h:222
struct task_struct * thread
Master thread.
Definition master.h:286
struct list_head eoe_handlers
Ethernet over EtherCAT handlers.
Definition master.h:290
ec_datagram_t fsm_datagram
Datagram used for state machines.
Definition master.h:218
struct list_head emerg_reg_requests
Emergency register access requests.
Definition master.h:305
struct semaphore master_sem
Master semaphore.
Definition master.h:205
struct list_head ext_datagram_queue
Queue for non-application datagrams.
Definition master.h:263
ec_datagram_t sync_datagram
Datagram used for DC drift compensation.
Definition master.h:238
size_t max_queue_size
Maximum size of datagram queue.
Definition master.h:276
wait_queue_head_t request_queue
Wait queue for external requests from user space.
Definition master.h:308
void(*) send_cb(void *)
Current send datagrams callback.
Definition master.h:295
struct list_head fsm_exec_list
Slave FSM execution list.
Definition master.h:279
struct list_head sii_requests
SII write requests.
Definition master.h:304
void(*) app_send_cb(void *)
Application's send datagrams callback.
Definition master.h:298
ec_fsm_master_t fsm
Master state machine.
Definition master.h:217
u64 app_time
Time of the last ecrt_master_sync() call.
Definition master.h:234
wait_queue_head_t scan_queue
Queue for processes that wait for slave scanning.
Definition master.h:251
unsigned int scan_busy
Current scan state.
Definition master.h:246
unsigned int send_interval
Interval between two calls to ecrt_master_send().
Definition master.h:274
unsigned int slave_count
Number of slaves on the bus.
Definition master.h:228
void * app_cb_data
Application callback data.
Definition master.h:302
uint8_t datagram_index
Current datagram index.
Definition master.h:261
unsigned int index
Index.
Definition master.h:195
unsigned int ext_ring_idx_fsm
Index in external datagram ring for FSM side.
Definition master.h:272
ec_device_stats_t device_stats
Device statistics.
Definition master.h:215
unsigned int config_busy
State of slave configuration.
Definition master.h:254
ec_slave_t * dc_ref_clock
DC reference clock slave.
Definition master.h:244
ec_datagram_t ref_sync_datagram
Datagram used for synchronizing the reference clock to the master clock.
Definition master.h:236
struct list_head domains
List of domains.
Definition master.h:232
struct semaphore config_sem
Semaphore protecting the config_busy variable and the allow_config flag.
Definition master.h:255
void(*) receive_cb(void *)
Current receive datagrams callback.
Definition master.h:296
unsigned int fsm_exec_count
Number of entries in execution list.
Definition master.h:280
struct irq_work sc_reset_work_kicker
NMI-Safe kicker to trigger reset task above.
Definition master.h:312
u64 dc_ref_time
Common reference timestamp for DC start times.
Definition master.h:235
ec_slave_t * fsm_slave
Slave that is queried next for FSM exec.
Definition master.h:278
unsigned int injection_seq_rt
Datagram injection sequence number for the realtime side.
Definition master.h:224
wait_queue_head_t config_queue
Queue for processes that wait for slave configuration.
Definition master.h:257
struct semaphore scan_sem
Semaphore protecting the scan_busy variable and the allow_scan flag.
Definition master.h:249
unsigned int run_on_cpu
bind kernel threads to this cpu
Definition master.h:283
ec_datagram_t sync_mon_datagram
Datagram used for DC synchronisation monitoring.
Definition master.h:240
struct rt_mutex io_mutex
Mutex used in IDLE and OP phase.
Definition master.h:293
ec_slave_t * slaves
Array of slaves on the bus.
Definition master.h:227
unsigned int active
Master has been activated.
Definition master.h:220
unsigned int ext_ring_idx_rt
Index in external datagram ring for RT side.
Definition master.h:270
unsigned int debug_level
Master debug level.
Definition master.h:282
ec_stats_t stats
Cyclic statistics.
Definition master.h:284
ec_slave_config_t * dc_ref_config
Application-selected DC reference clock slave config.
Definition master.h:242
struct device * class_device
Master class device.
Definition master.h:199
struct semaphore device_sem
Device semaphore.
Definition master.h:214
ec_cdev_t cdev
Master character device.
Definition master.h:198
unsigned int reserved
True, if the master is in use.
Definition master.h:196
unsigned int allow_scan
True, if slave scanning is allowed.
Definition master.h:248
unsigned int config_changed
The configuration changed.
Definition master.h:221
struct list_head configs
List of slave configurations.
Definition master.h:231
const uint8_t * macs[EC_MAX_NUM_DEVICES]
Device MAC addresses.
Definition master.h:208
unsigned int scan_index
Index of slave currently scanned.
Definition master.h:247
ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE]
External datagram ring.
Definition master.h:268
ec_master_phase_t phase
Master phase.
Definition master.h:219
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition master.h:207
void * cb_data
Current callback data.
Definition master.h:297
struct semaphore ext_queue_sem
Semaphore protecting the ext_datagram_queue.
Definition master.h:265
void(*) app_receive_cb(void *)
Application's receive datagrams callback.
Definition master.h:300
Cyclic statistics.
Definition master.h:136
unsigned int corrupted
corrupted frames
Definition master.h:138
unsigned long output_jiffies
time of last output
Definition master.h:141
unsigned int timeouts
datagram timeouts
Definition master.h:137
unsigned int unmatched
unmatched datagrams (received, but not queued any longer)
Definition master.h:139