IgH EtherCAT Master  1.5.2
master.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * $Id$
4  *
5  * Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH
6  *
7  * This file is part of the IgH EtherCAT Master.
8  *
9  * The IgH EtherCAT Master is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2, as
11  * published by the Free Software Foundation.
12  *
13  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with the IgH EtherCAT Master; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * ---
23  *
24  * The license mentioned above concerns the source code only. Using the
25  * EtherCAT technology and brand is only permitted in compliance with the
26  * industrial property and similar rights of Beckhoff Automation GmbH.
27  *
28  *****************************************************************************/
29 
35 /*****************************************************************************/
36 
37 #ifndef __EC_MASTER_H__
38 #define __EC_MASTER_H__
39 
40 #include <linux/version.h>
41 #include <linux/list.h>
42 #include <linux/timer.h>
43 #include <linux/wait.h>
44 #include <linux/kthread.h>
45 
46 #include "device.h"
47 #include "domain.h"
48 #include "ethernet.h"
49 #include "fsm_master.h"
50 #include "locks.h"
51 #include "cdev.h"
52 
53 #ifdef EC_RTDM
54 #include "rtdm.h"
55 #endif
56 
57 /*****************************************************************************/
58 
68 #define EC_MASTER_INFO(master, fmt, args...) \
69  printk(KERN_INFO "EtherCAT %u: " fmt, master->index, ##args)
70 
80 #define EC_MASTER_ERR(master, fmt, args...) \
81  printk(KERN_ERR "EtherCAT ERROR %u: " fmt, master->index, ##args)
82 
92 #define EC_MASTER_WARN(master, fmt, args...) \
93  printk(KERN_WARNING "EtherCAT WARNING %u: " fmt, master->index, ##args)
94 
106 #define EC_MASTER_DBG(master, level, fmt, args...) \
107  do { \
108  if (master->debug_level >= level) { \
109  printk(KERN_DEBUG "EtherCAT DEBUG %u: " fmt, \
110  master->index, ##args); \
111  } \
112  } while (0)
113 
114 
119 #define EC_EXT_RING_SIZE 32
120 
124 #define EOE_STH_TO_SEND 1
125 
132 #define EOE_STH_PENDING 2
133 
134 /*****************************************************************************/
135 
138 typedef enum {
146 
147 /*****************************************************************************/
148 
151 typedef struct {
152  unsigned int timeouts;
153  unsigned int corrupted;
154  unsigned int unmatched;
156  unsigned long output_jiffies;
157 } ec_stats_t;
158 
159 /*****************************************************************************/
160 
163 typedef struct {
164  u64 tx_count;
166  u64 rx_count;
169  u64 tx_bytes;
171  u64 rx_bytes;
174  u64 last_loss;
175  s32 tx_frame_rates[EC_RATE_COUNT];
178  s32 rx_frame_rates[EC_RATE_COUNT];
181  s32 tx_byte_rates[EC_RATE_COUNT];
183  s32 rx_byte_rates[EC_RATE_COUNT];
185  s32 loss_rates[EC_RATE_COUNT];
187  unsigned long jiffies;
189 
190 /*****************************************************************************/
191 
192 #if EC_MAX_NUM_DEVICES < 1
193 #error Invalid number of devices
194 #endif
195 
196 /*****************************************************************************/
197 
202 struct ec_master {
203  unsigned int index;
204  unsigned int reserved;
207 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
208  struct device *class_device;
209 #else
210  struct class_device *class_device;
211 #endif
212 
213 #ifdef EC_RTDM
214  ec_rtdm_dev_t rtdm_dev;
215 #endif
216 
217  ec_lock_t master_sem;
219  ec_device_t devices[EC_MAX_NUM_DEVICES];
220  const uint8_t *macs[EC_MAX_NUM_DEVICES];
221 #if EC_MAX_NUM_DEVICES > 1
222  unsigned int num_devices;
225 #endif
226  ec_lock_t device_sem;
232  unsigned int active;
233  unsigned int config_changed;
234  unsigned int injection_seq_fsm;
236  unsigned int injection_seq_rt;
240  unsigned int slave_count;
242  /* Configuration applied by the application. */
243  struct list_head configs;
244  struct list_head domains;
246  /* Configuration applied during bus scanning. */
247  struct list_head sii_images;
249  u64 app_time;
264  unsigned int reboot;
265  unsigned int scan_busy;
266  unsigned int allow_scan;
267  ec_lock_t scan_sem;
269  wait_queue_head_t scan_queue;
272  unsigned int config_busy;
273  ec_lock_t config_sem;
275  wait_queue_head_t config_queue;
278  struct list_head datagram_queue;
279  uint8_t datagram_index;
281  struct list_head ext_datagram_queue;
283  ec_lock_t ext_queue_sem;
286  ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE];
288  unsigned int ext_ring_idx_rt;
290  unsigned int ext_ring_idx_fsm;
292  unsigned int send_interval;
294  size_t max_queue_size;
295  unsigned int rt_slave_requests;
299  unsigned int rt_slaves_available;
307  struct list_head fsm_exec_list;
308  unsigned int fsm_exec_count;
310  unsigned int debug_level;
313  void *pcap_data;
316  struct task_struct *thread;
318 #ifdef EC_EOE
319  struct task_struct *eoe_thread;
320  struct list_head eoe_handlers;
321 #endif
322 
323  ec_lock_t io_sem;
325  void (*send_cb)(void *);
326  void (*receive_cb)(void *);
327  void *cb_data;
328  void (*app_send_cb)(void *);
330  void (*app_receive_cb)(void *);
332  void *app_cb_data;
334  struct list_head sii_requests;
335  struct list_head emerg_reg_requests;
338  wait_queue_head_t request_queue;
340 };
341 
342 /*****************************************************************************/
343 
344 // static funtions
345 void ec_master_init_static(void);
346 
347 // master creation/deletion
348 int ec_master_init(ec_master_t *, unsigned int, const uint8_t *,
349  const uint8_t *, dev_t, struct class *, unsigned int);
351 
353 
356 #if EC_MAX_NUM_DEVICES > 1
357 #define ec_master_num_devices(MASTER) ((MASTER)->num_devices)
358 #else
359 #define ec_master_num_devices(MASTER) 1
360 #endif
361 
362 // phase transitions
367 
368 #ifdef EC_EOE
369 // EoE
372 #endif
373 
374 // datagram IO
376  const uint8_t *, size_t);
379 
380 // misc.
381 void ec_master_set_send_interval(ec_master_t *, unsigned int);
384 ec_slave_t *ec_master_find_slave(ec_master_t *, uint16_t, uint16_t);
385 const ec_slave_t *ec_master_find_slave_const(const ec_master_t *, uint16_t,
386  uint16_t);
388 #ifdef EC_EOE
389 void ec_master_clear_eoe_handlers(ec_master_t *, unsigned int);
390 #endif
396 
397 unsigned int ec_master_config_count(const ec_master_t *);
399  const ec_master_t *, unsigned int);
401  const ec_master_t *, unsigned int);
402 unsigned int ec_master_domain_count(const ec_master_t *);
405  unsigned int);
406 #ifdef EC_EOE
407 uint16_t ec_master_eoe_handler_count(const ec_master_t *);
408 const ec_eoe_t *ec_master_get_eoe_handler_const(const ec_master_t *, uint16_t);
409 #ifdef EC_RTDM
410 int ec_master_eoe_is_open(ec_master_t *);
411 int ec_master_eoe_process(ec_master_t *);
412 #endif
413 #endif
414 
415 int ec_master_mbox_gateway(ec_master_t *master, uint8_t *data,
416  size_t *data_size, size_t buff_size);
417 
418 int ec_master_debug_level(ec_master_t *, unsigned int);
419 
422  uint16_t, uint32_t, uint32_t);
423 
426 
427 void ec_master_internal_send_cb(void *);
428 void ec_master_internal_receive_cb(void *);
429 
430 int ec_master_dict_upload(ec_master_t *, uint16_t);
431 
432 extern const unsigned int rate_intervals[EC_RATE_COUNT]; // see master.c
433 
434 #ifdef EC_EOE
435 #define MAX_EOE 32
437 extern char *eoe_interfaces[MAX_EOE]; // see module.c
438 extern unsigned int eoe_count; // see module.c
439 extern bool eoe_autocreate; // see module.c
440 #endif
441 extern unsigned long pcap_size; // see module.c
442 
443 /*****************************************************************************/
444 
445 #endif
ec_lock_t ext_queue_sem
Semaphore protecting the ext_datagram_queue.
Definition: master.h:283
unsigned int injection_seq_fsm
Datagram injection sequence number for the FSM side.
Definition: master.h:234
Cyclic statistics.
Definition: master.h:151
void ec_master_clear_slaves(ec_master_t *)
Clear all slaves.
Definition: master.c:606
unsigned int reserved
True, if the master is in use.
Definition: master.h:204
void ec_master_clear_eoe_handlers(ec_master_t *, unsigned int)
Clear and free auto created EoE handlers.
Definition: master.c:480
void ec_master_expire_slave_config_requests(ec_master_t *)
Abort active requests for slave configs without attached slaves.
Definition: master.c:2222
void ec_master_eoe_stop(ec_master_t *)
Stops the Ethernet over EtherCAT processing.
Definition: master.c:2036
void ec_master_internal_receive_cb(void *)
Internal receiving callback.
Definition: master.c:692
unsigned int fsm_exec_count
Number of entries in execution list.
Definition: master.h:308
u64 last_loss
Tx/Rx difference of last statistics cycle.
Definition: master.h:174
void * pcap_curr_data
pcap debug output current memory pointer
Definition: master.h:314
u64 tx_count
Number of frames sent.
Definition: master.h:164
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:2759
bool eoe_autocreate
Auto-create EOE interfaces.
Definition: module.c:64
Finite state machine of an EtherCAT master.
Definition: fsm_master.h:67
unsigned int reboot
Reboot requested.
Definition: master.h:264
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:2445
void * pcap_data
pcap debug output memory pointer
Definition: master.h:313
EtherCAT RTDM device.
Definition: rtdm.h:44
#define EC_RATE_COUNT
Number of statistic rate intervals to maintain.
Definition: globals.h:71
Complete slave information interface data image.
Definition: slave.h:201
ec_datagram_t sync_mon_datagram
Datagram used for DC synchronisation monitoring.
Definition: master.h:258
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:2278
Operation phase.
Definition: master.h:143
unsigned int allow_scan
True, if slave scanning is allowed.
Definition: master.h:266
size_t max_queue_size
Maximum size of datagram queue.
Definition: master.h:294
unsigned int eoe_count
Number of EOE interfaces.
Definition: module.c:63
void ec_master_request_op(ec_master_t *)
Request OP state for configured slaves.
Definition: master.c:2669
EtherCAT datagram.
Definition: datagram.h:88
u64 dc_ref_time
Common reference timestamp for DC start times.
Definition: master.h:250
int ec_master_enter_idle_phase(ec_master_t *)
Transition function from ORPHANED to IDLE phase.
Definition: master.c:764
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:2327
unsigned long jiffies
Jiffies of last statistic cycle.
Definition: master.h:187
const unsigned int rate_intervals[EC_RATE_COUNT]
List of intervals for statistics [s].
Definition: master.c:103
unsigned int rt_slaves_available
if True, slave requests can be handled by calls to ecrt_master_exec_requests() from the applications ...
Definition: master.h:299
u64 last_rx_count
Number of frames received of last statistics cycle.
Definition: master.h:167
wait_queue_head_t request_queue
Wait queue for external requests from user space.
Definition: master.h:338
EtherCAT master state machine.
int ec_master_init(ec_master_t *, unsigned int, const uint8_t *, const uint8_t *, dev_t, struct class *, unsigned int)
Master constructor.
Definition: master.c:145
void * cb_data
Current callback data.
Definition: master.h:327
ec_lock_t config_sem
Semaphore protecting the config_busy variable and the allow_config flag.
Definition: master.h:273
ec_slave_t * fsm_slave
Slave that is queried next for FSM exec.
Definition: master.h:306
unsigned int send_interval
Interval between two calls to ecrt_master_send().
Definition: master.h:292
EtherCAT slave.
Definition: slave.h:214
uint8_t datagram_index
Current datagram index.
Definition: master.h:279
ec_lock_t device_sem
Device semaphore.
Definition: master.h:226
struct task_struct * eoe_thread
EoE thread.
Definition: master.h:319
unsigned int unmatched
unmatched datagrams (received, but not queued any longer)
Definition: master.h:154
unsigned int ext_ring_idx_fsm
Index in external datagram ring for FSM side.
Definition: master.h:290
u8 dc_offset_valid
DC slaves have valid system time offsets.
Definition: master.h:251
Ethernet over EtherCAT (EoE)
ec_device_stats_t device_stats
Device statistics.
Definition: master.h:227
static uint8_t macs[MAX_MASTERS][2][ETH_ALEN]
MAC addresses.
Definition: module.c:75
ec_datagram_t fsm_datagram
Datagram used for state machines.
Definition: master.h:230
ec_master_phase_t phase
Master phase.
Definition: master.h:231
ec_slave_t * slaves
Array of slaves on the bus.
Definition: master.h:239
unsigned int ec_master_domain_count(const ec_master_t *)
Get the number of domains.
Definition: master.c:2357
EtherCAT device.
Definition: device.h:90
void ec_master_output_stats(ec_master_t *)
Output master statistics.
Definition: master.c:1584
void ec_master_slaves_not_available(ec_master_t *)
Set flag to say that the slaves are not available for slave request processing.
Definition: master.c:585
ec_datagram_t sync64_datagram
Datagram used to retrieve 64bit ref slave system clock time.
Definition: master.h:256
ec_domain_t * ec_master_find_domain(ec_master_t *, unsigned int)
Get a domain via its position in the list.
Definition: master.c:2391
unsigned int timeouts
datagram timeouts
Definition: master.h:152
unsigned int debug_level
Master debug level.
Definition: master.h:310
Orphaned phase.
Definition: master.h:139
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:2262
unsigned int corrupted
corrupted frames
Definition: master.h:153
u64 last_tx_count
Number of frames sent of last statistics cycle.
Definition: master.h:165
void ec_master_receive_datagrams(ec_master_t *, ec_device_t *, const uint8_t *, size_t)
Processes a received frame.
Definition: master.c:1318
unsigned int ext_ring_idx_rt
Index in external datagram ring for RT side.
Definition: master.h:288
unsigned int slave_count
Number of slaves on the bus.
Definition: master.h:240
unsigned int scan_busy
Current scan state.
Definition: master.h:265
void ec_master_clear(ec_master_t *)
Destructor.
Definition: master.c:422
void ec_master_calc_dc(ec_master_t *)
Distributed-clocks calculations.
Definition: master.c:2652
unsigned int rt_slave_requests
if True, slave requests are to be handled by calls to ecrt_master_exec_requests() from the applicatio...
Definition: master.h:295
unsigned int active
Master has been activated.
Definition: master.h:232
wait_queue_head_t scan_queue
Queue for processes that wait for slave scanning.
Definition: master.h:269
int ec_master_debug_level(ec_master_t *, unsigned int)
Set the debug level.
Definition: master.c:2470
ec_datagram_t sync_datagram
Datagram used for DC drift compensation.
Definition: master.h:254
struct device * class_device
Master class device.
Definition: master.h:208
void ec_master_internal_send_cb(void *)
Internal sending callback.
Definition: master.c:678
void ec_master_clear_sii_images(ec_master_t *)
Clear the SII data applied during bus scanning.
Definition: master.c:559
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:1052
ec_slave_config_t * dc_ref_config
Application-selected DC reference clock slave config.
Definition: master.h:260
uint16_t ec_master_eoe_handler_count(const ec_master_t *)
Get the number of EoE handlers.
Definition: master.c:2423
Device statistics.
Definition: master.h:163
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
Definition: master.h:172
unsigned long output_jiffies
time of last output
Definition: master.h:156
ec_stats_t stats
Cyclic statistics.
Definition: master.h:311
Idle phase.
Definition: master.h:141
void ec_master_queue_datagram_ext(ec_master_t *, ec_datagram_t *)
Places a datagram in the non-application datagram queue.
Definition: master.c:1140
void ec_master_leave_operation_phase(ec_master_t *)
Transition function from OPERATION to IDLE phase.
Definition: master.c:916
ec_lock_t master_sem
Master semaphore.
Definition: master.h:217
u64 tx_bytes
Number of bytes sent.
Definition: master.h:169
void * app_cb_data
Application callback data.
Definition: master.h:332
void ec_master_eoe_start(ec_master_t *)
Starts Ethernet over EtherCAT processing on demand.
Definition: master.c:1997
wait_queue_head_t config_queue
Queue for processes that wait for slave configuration.
Definition: master.h:275
#define EC_EXT_RING_SIZE
Size of the external datagram ring.
Definition: master.h:119
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:2294
u64 app_time
Time of the last ecrt_master_sync() call.
Definition: master.h:249
ec_datagram_t ref_sync_datagram
Datagram used for synchronizing the reference clock to the master clock.
Definition: master.h:252
u64 rx_count
Number of frames received.
Definition: master.h:166
void ec_master_leave_idle_phase(ec_master_t *)
Transition function from IDLE to ORPHANED phase.
Definition: master.c:819
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:3107
EtherCAT master character device.
Definition: cdev.h:49
EtherCAT slave configuration.
Definition: slave_config.h:118
EtherCAT device structure.
unsigned long pcap_size
Pcap buffer size in bytes.
Definition: module.c:67
ec_master_phase_t
EtherCAT master phase.
Definition: master.h:138
Abstract locks.
void ec_master_attach_slave_configs(ec_master_t *)
Attaches the slave configurations to the slaves.
Definition: master.c:2207
void ec_master_init_static(void)
Static variables initializer.
Definition: master.c:124
unsigned int index
Index.
Definition: master.h:203
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
Definition: master.h:170
void ec_sii_image_clear(ec_sii_image_t *)
Clear the SII data.
Definition: master.c:522
void ec_master_slaves_available(ec_master_t *)
Set flag to say that the slaves are now available for slave request processing.
Definition: master.c:597
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:71
ec_fsm_master_t fsm
Master state machine.
Definition: master.h:229
ec_cdev_t cdev
Master character device.
Definition: master.h:206
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:2406
u64 rx_bytes
Number of bytes received.
Definition: master.h:171
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:2342
void ec_master_reboot_slaves(ec_master_t *)
Requests that all slaves on this master be rebooted (if supported).
Definition: master.c:1067
unsigned int config_changed
The configuration changed.
Definition: master.h:233
EtherCAT master.
Definition: master.h:202
unsigned int injection_seq_rt
Datagram injection sequence number for the realtime side.
Definition: master.h:236
ec_lock_t scan_sem
Semaphore protecting the scan_busy variable and the allow_scan flag.
Definition: master.h:267
unsigned int config_busy
State of slave configuration.
Definition: master.h:272
ec_lock_t io_sem
Semaphore used in IDLE phase.
Definition: master.h:323
EtherCAT master character device.
int ec_master_enter_operation_phase(ec_master_t *)
Transition function from IDLE to OPERATION phase.
Definition: master.c:845
EtherCAT domain.
Definition: domain.h:54
struct task_struct * thread
Master thread.
Definition: master.h:316
ec_slave_t * dc_ref_clock
DC reference clock slave.
Definition: master.h:262
RTDM interface.
void ec_master_queue_datagram(ec_master_t *, ec_datagram_t *)
Places a datagram in the datagram queue.
Definition: master.c:1105