IgH EtherCAT Master  1.6.0
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 
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 
115 /****************************************************************************/
116 
119 typedef enum {
127 
128 /****************************************************************************/
129 
132 typedef struct {
133  unsigned int timeouts;
134  unsigned int corrupted;
135  unsigned int unmatched;
137  unsigned long output_jiffies;
138 } ec_stats_t;
139 
140 /****************************************************************************/
141 
144 typedef struct {
145  u64 tx_count;
147  u64 rx_count;
150  u64 tx_bytes;
152  u64 rx_bytes;
155  u64 last_loss;
156  s32 tx_frame_rates[EC_RATE_COUNT];
159  s32 rx_frame_rates[EC_RATE_COUNT];
162  s32 tx_byte_rates[EC_RATE_COUNT];
164  s32 rx_byte_rates[EC_RATE_COUNT];
166  s32 loss_rates[EC_RATE_COUNT];
168  unsigned long jiffies;
170 
171 /****************************************************************************/
172 
173 #if EC_MAX_NUM_DEVICES < 1
174 #error Invalid number of devices
175 #endif
176 
177 /****************************************************************************/
178 
183 struct ec_master {
184  unsigned int index;
185  unsigned int reserved;
188  struct device *class_device;
190 #ifdef EC_RTDM
191  ec_rtdm_dev_t rtdm_dev;
192 #endif
193 
194  struct semaphore master_sem;
196  ec_device_t devices[EC_MAX_NUM_DEVICES];
197  const uint8_t *macs[EC_MAX_NUM_DEVICES];
198 #if EC_MAX_NUM_DEVICES > 1
199  unsigned int num_devices;
202 #endif
203  struct semaphore device_sem;
209  unsigned int active;
210  unsigned int config_changed;
211  unsigned int injection_seq_fsm;
213  unsigned int injection_seq_rt;
217  unsigned int slave_count;
219  /* Configuration applied by the application. */
220  struct list_head configs;
221  struct list_head domains;
223  u64 app_time;
235  unsigned int scan_busy;
236  unsigned int scan_index;
237  unsigned int allow_scan;
238  struct semaphore scan_sem;
240  wait_queue_head_t scan_queue;
243  unsigned int config_busy;
244  struct semaphore config_sem;
246  wait_queue_head_t config_queue;
249  struct list_head datagram_queue;
250  uint8_t datagram_index;
252  struct list_head ext_datagram_queue;
254  struct semaphore ext_queue_sem;
259  unsigned int ext_ring_idx_rt;
261  unsigned int ext_ring_idx_fsm;
263  unsigned int send_interval;
265  size_t max_queue_size;
268  struct list_head fsm_exec_list;
269  unsigned int fsm_exec_count;
271  unsigned int debug_level;
272  unsigned int run_on_cpu;
275  struct task_struct *thread;
277 #ifdef EC_EOE
278  struct task_struct *eoe_thread;
279  struct list_head eoe_handlers;
280 #endif
281 
282  struct rt_mutex io_mutex;
284  void (*send_cb)(void *);
285  void (*receive_cb)(void *);
286  void *cb_data;
287  void (*app_send_cb)(void *);
289  void (*app_receive_cb)(void *);
291  void *app_cb_data;
293  struct list_head sii_requests;
294  struct list_head emerg_reg_requests;
297  wait_queue_head_t request_queue;
299  struct work_struct sc_reset_work;
300  struct irq_work sc_reset_work_kicker;
302 };
303 
304 /****************************************************************************/
305 
306 // static funtions
307 void ec_master_init_static(void);
308 
309 // master creation/deletion
310 int ec_master_init(ec_master_t *, unsigned int, const uint8_t *,
311  const uint8_t *, dev_t, struct class *, unsigned int, unsigned int);
313 
316 #if EC_MAX_NUM_DEVICES > 1
317 #define ec_master_num_devices(MASTER) ((MASTER)->num_devices)
318 #else
319 #define ec_master_num_devices(MASTER) 1
320 #endif
321 
322 // phase transitions
327 
328 #ifdef EC_EOE
329 // EoE
332 #endif
333 
334 // datagram IO
336  const uint8_t *, size_t);
339 
340 // misc.
341 void ec_master_set_send_interval(ec_master_t *, unsigned int);
343 ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
344 const ec_slave_t *ec_master_find_slave_const(const ec_master_t *, uint16_t,
345  uint16_t);
347 #ifdef EC_EOE
349 #endif
351 
352 unsigned int ec_master_config_count(const ec_master_t *);
354  const ec_master_t *, unsigned int);
356  const ec_master_t *, unsigned int);
357 unsigned int ec_master_domain_count(const ec_master_t *);
360  unsigned int);
361 #ifdef EC_EOE
362 uint16_t ec_master_eoe_handler_count(const ec_master_t *);
363 const ec_eoe_t *ec_master_get_eoe_handler_const(const ec_master_t *, uint16_t);
364 #endif
365 
366 int ec_master_debug_level(ec_master_t *, unsigned int);
367 
370  uint16_t, uint32_t, uint32_t);
371 
374 
375 void ec_master_internal_send_cb(void *);
376 void ec_master_internal_receive_cb(void *);
377 
378 extern const unsigned int rate_intervals[EC_RATE_COUNT]; // see master.c
379 
380 /****************************************************************************/
381 
382 #endif
unsigned int injection_seq_fsm
Datagram injection sequence number for the FSM side.
Definition: master.h:211
Cyclic statistics.
Definition: master.h:132
void ec_master_clear_slaves(ec_master_t *)
Clear all slaves.
Definition: master.c:459
unsigned int reserved
True, if the master is in use.
Definition: master.h:185
struct list_head ext_datagram_queue
Queue for non-application datagrams.
Definition: master.h:252
void ec_master_eoe_stop(ec_master_t *)
Stops the Ethernet over EtherCAT processing.
Definition: master.c:1688
struct semaphore config_sem
Semaphore protecting the config_busy variable and the allow_config flag.
Definition: master.h:244
void ec_master_internal_receive_cb(void *)
Internal receiving callback.
Definition: master.c:545
unsigned int fsm_exec_count
Number of entries in execution list.
Definition: master.h:269
u64 last_loss
Tx/Rx difference of last statistics cycle.
Definition: master.h:155
u64 tx_count
Number of frames sent.
Definition: master.h:145
ec_domain_t * ecrt_master_create_domain_err(ec_master_t *)
Same as ecrt_master_create_domain(), but with ERR_PTR() return value.
Definition: master.c:2232
struct list_head sii_requests
SII write requests.
Definition: master.h:293
Finite state machine of an EtherCAT master.
Definition: fsm_master.h:60
const ec_eoe_t * ec_master_get_eoe_handler_const(const ec_master_t *, uint16_t)
Get an EoE handler via its position in the list.
Definition: master.c:1989
EtherCAT RTDM device.
Definition: rtdm.h:38
#define EC_RATE_COUNT
Number of statistic rate intervals to maintain.
Definition: globals.h:60
ec_datagram_t sync_mon_datagram
Datagram used for DC synchronisation monitoring.
Definition: master.h:229
struct list_head eoe_handlers
Ethernet over EtherCAT handlers.
Definition: master.h:279
const ec_slave_t * ec_master_find_slave_const(const ec_master_t *, uint16_t, uint16_t)
Finds a slave in the bus, given the alias and position.
Definition: master.c:1822
Operation phase.
Definition: master.h:124
unsigned int allow_scan
True, if slave scanning is allowed.
Definition: master.h:237
size_t max_queue_size
Maximum size of datagram queue.
Definition: master.h:265
void ec_master_request_op(ec_master_t *)
Request OP state for configured slaves.
Definition: master.c:2198
EtherCAT datagram.
Definition: datagram.h:79
struct list_head fsm_exec_list
Slave FSM execution list.
Definition: master.h:268
unsigned int scan_index
Index of slave currently scanned.
Definition: master.h:236
u64 dc_ref_time
Common reference timestamp for DC start times.
Definition: master.h:224
struct list_head emerg_reg_requests
Emergency register access requests.
Definition: master.h:294
int ec_master_enter_idle_phase(ec_master_t *)
Transition function from ORPHANED to IDLE phase.
Definition: master.c:624
struct list_head domains
List of domains.
Definition: master.h:221
ec_slave_config_t * ec_master_get_config(const ec_master_t *, unsigned int)
Get a slave configuration via its position in the list.
Definition: master.c:1871
unsigned long jiffies
Jiffies of last statistic cycle.
Definition: master.h:168
const unsigned int rate_intervals[EC_RATE_COUNT]
List of intervals for statistics [s].
Definition: master.c:94
u64 last_rx_count
Number of frames received of last statistics cycle.
Definition: master.h:148
const uint8_t * macs[EC_MAX_NUM_DEVICES]
Device MAC addresses.
Definition: master.h:197
wait_queue_head_t request_queue
Wait queue for external requests from user space.
Definition: master.h:297
EtherCAT master state machine.
unsigned int run_on_cpu
bind kernel threads to this cpu
Definition: master.h:272
int ec_master_init(ec_master_t *, unsigned int, const uint8_t *, const uint8_t *, dev_t, struct class *, unsigned int, unsigned int)
Master constructor.
Definition: master.c:138
void * cb_data
Current callback data.
Definition: master.h:286
ec_slave_t * fsm_slave
Slave that is queried next for FSM exec.
Definition: master.h:267
unsigned int send_interval
Interval between two calls to ecrt_master_send().
Definition: master.h:263
EtherCAT slave.
Definition: slave.h:168
struct semaphore master_sem
Master semaphore.
Definition: master.h:194
uint8_t datagram_index
Current datagram index.
Definition: master.h:250
struct list_head datagram_queue
Datagram queue.
Definition: master.h:249
struct irq_work sc_reset_work_kicker
NMI-Safe kicker to trigger reset task above.
Definition: master.h:300
struct task_struct * eoe_thread
EoE thread.
Definition: master.h:278
unsigned int unmatched
unmatched datagrams (received, but not queued any longer)
Definition: master.h:135
unsigned int ext_ring_idx_fsm
Index in external datagram ring for FSM side.
Definition: master.h:261
Ethernet over EtherCAT (EoE)
ec_device_stats_t device_stats
Device statistics.
Definition: master.h:204
ec_datagram_t fsm_datagram
Datagram used for state machines.
Definition: master.h:207
ec_master_phase_t phase
Master phase.
Definition: master.h:208
ec_slave_t * slaves
Array of slaves on the bus.
Definition: master.h:216
unsigned int ec_master_domain_count(const ec_master_t *)
Get the number of domains.
Definition: master.c:1901
struct semaphore device_sem
Device semaphore.
Definition: master.h:203
EtherCAT device.
Definition: device.h:73
void ec_master_output_stats(ec_master_t *)
Output master statistics.
Definition: master.c:1258
ec_domain_t * ec_master_find_domain(ec_master_t *, unsigned int)
Get a domain via its position in the list.
Definition: master.c:1935
unsigned int timeouts
datagram timeouts
Definition: master.h:133
unsigned int debug_level
Master debug level.
Definition: master.h:271
Orphaned phase.
Definition: master.h:120
ec_slave_t * ec_master_find_slave(ec_master_t *, uint16_t, uint16_t)
Finds a slave in the bus, given the alias and position.
Definition: master.c:1806
unsigned int corrupted
corrupted frames
Definition: master.h:134
u64 last_tx_count
Number of frames sent of last statistics cycle.
Definition: master.h:146
void ec_master_receive_datagrams(ec_master_t *, ec_device_t *, const uint8_t *, size_t)
Processes a received frame.
Definition: master.c:1120
unsigned int ext_ring_idx_rt
Index in external datagram ring for RT side.
Definition: master.h:259
struct rt_mutex io_mutex
Mutex used in IDLE and OP phase.
Definition: master.h:282
unsigned int slave_count
Number of slaves on the bus.
Definition: master.h:217
unsigned int scan_busy
Current scan state.
Definition: master.h:235
void(* receive_cb)(void *)
Current receive datagrams callback.
Definition: master.h:285
void ec_master_clear(ec_master_t *)
Destructor.
Definition: master.c:377
void ec_master_calc_dc(ec_master_t *)
Distributed-clocks calculations.
Definition: master.c:2181
unsigned int active
Master has been activated.
Definition: master.h:209
wait_queue_head_t scan_queue
Queue for processes that wait for slave scanning.
Definition: master.h:240
int ec_master_debug_level(ec_master_t *, unsigned int)
Set the debug level.
Definition: master.c:2014
ec_datagram_t sync_datagram
Datagram used for DC drift compensation.
Definition: master.h:227
struct device * class_device
Master class device.
Definition: master.h:188
void ec_master_internal_send_cb(void *)
Internal sending callback.
Definition: master.c:530
void ec_master_set_send_interval(ec_master_t *, unsigned int)
Sets the expected interval between calls to ecrt_master_send and calculates the maximum amount of dat...
Definition: master.c:899
struct list_head configs
List of slave configurations.
Definition: master.h:220
ec_slave_config_t * dc_ref_config
Application-selected DC reference clock slave config.
Definition: master.h:231
uint16_t ec_master_eoe_handler_count(const ec_master_t *)
Get the number of EoE handlers.
Definition: master.c:1967
Device statistics.
Definition: master.h:144
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
Definition: master.h:153
unsigned long output_jiffies
time of last output
Definition: master.h:137
ec_stats_t stats
Cyclic statistics.
Definition: master.h:273
Idle phase.
Definition: master.h:122
struct semaphore scan_sem
Semaphore protecting the scan_busy variable and the allow_scan flag.
Definition: master.h:238
void ec_master_queue_datagram_ext(ec_master_t *, ec_datagram_t *)
Places a datagram in the non-application datagram queue.
Definition: master.c:968
void ec_master_leave_operation_phase(ec_master_t *)
Transition function from OPERATION to IDLE phase.
Definition: master.c:763
u64 tx_bytes
Number of bytes sent.
Definition: master.h:150
void(* app_send_cb)(void *)
Application&#39;s send datagrams callback.
Definition: master.h:287
void * app_cb_data
Application callback data.
Definition: master.h:291
void ec_master_eoe_start(ec_master_t *)
Starts Ethernet over EtherCAT processing on demand.
Definition: master.c:1654
ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE]
External datagram ring.
Definition: master.h:257
wait_queue_head_t config_queue
Queue for processes that wait for slave configuration.
Definition: master.h:246
#define EC_EXT_RING_SIZE
Size of the external datagram ring.
Definition: master.h:113
EtherCAT domain structure.
unsigned int ec_master_config_count(const ec_master_t *)
Get the number of slave configurations provided by the application.
Definition: master.c:1838
u64 app_time
Time of the last ecrt_master_sync() call.
Definition: master.h:223
ec_datagram_t ref_sync_datagram
Datagram used for synchronizing the reference clock to the master clock.
Definition: master.h:225
u64 rx_count
Number of frames received.
Definition: master.h:147
void ec_master_leave_idle_phase(ec_master_t *)
Transition function from IDLE to ORPHANED phase.
Definition: master.c:657
struct work_struct sc_reset_work
Task to reset slave configuration.
Definition: master.h:299
ec_slave_config_t * ecrt_master_slave_config_err(ec_master_t *, uint16_t, uint16_t, uint32_t, uint32_t)
Same as ecrt_master_slave_config(), but with ERR_PTR() return value.
Definition: master.c:2541
EtherCAT master character device.
Definition: cdev.h:41
struct semaphore ext_queue_sem
Semaphore protecting the ext_datagram_queue.
Definition: master.h:254
EtherCAT slave configuration.
Definition: slave_config.h:111
EtherCAT device structure.
void(* app_receive_cb)(void *)
Application&#39;s receive datagrams callback.
Definition: master.h:289
ec_master_phase_t
EtherCAT master phase.
Definition: master.h:119
void ec_master_attach_slave_configs(ec_master_t *)
Attaches the slave configurations to the slaves.
Definition: master.c:1766
void ec_master_init_static(void)
Static variables initializer.
Definition: master.c:117
unsigned int index
Index.
Definition: master.h:184
void ec_master_clear_eoe_handlers(ec_master_t *)
Clear and free all EoE handlers.
Definition: master.c:423
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
Definition: master.h:151
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:74
ec_fsm_master_t fsm
Master state machine.
Definition: master.h:206
ec_cdev_t cdev
Master character device.
Definition: master.h:187
const ec_domain_t * ec_master_find_domain_const(const ec_master_t *, unsigned int)
Get a domain via its position in the list.
Definition: master.c:1950
u64 rx_bytes
Number of bytes received.
Definition: master.h:152
const ec_slave_config_t * ec_master_get_config_const(const ec_master_t *, unsigned int)
Get a slave configuration via its position in the list.
Definition: master.c:1886
unsigned int config_changed
The configuration changed.
Definition: master.h:210
EtherCAT master.
Definition: master.h:183
unsigned int injection_seq_rt
Datagram injection sequence number for the realtime side.
Definition: master.h:213
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition: master.h:196
unsigned int config_busy
State of slave configuration.
Definition: master.h:243
EtherCAT master character device.
int ec_master_enter_operation_phase(ec_master_t *)
Transition function from IDLE to OPERATION phase.
Definition: master.c:681
EtherCAT domain.
Definition: domain.h:46
void(* send_cb)(void *)
Current send datagrams callback.
Definition: master.h:284
struct task_struct * thread
Master thread.
Definition: master.h:275
ec_slave_t * dc_ref_clock
DC reference clock slave.
Definition: master.h:233
RTDM interface.
void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *)
Places a datagram in the datagram queue.
Definition: master.c:935