IgH EtherCAT Master  1.6.2
master.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2006-2020 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  * vim: expandtab
21  *
22  ****************************************************************************/
23 
29 /****************************************************************************/
30 
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/device.h>
37 #include <linux/version.h>
38 #include <linux/hrtimer.h>
39 #include <linux/kthread.h>
40 
41 #include "globals.h"
42 #include "slave.h"
43 #include "slave_config.h"
44 #include "device.h"
45 #include "datagram.h"
46 
47 #ifdef EC_EOE
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
49 #include <uapi/linux/sched/types.h> // struct sched_param
50 #include <linux/sched/types.h> // sched_setscheduler
51 #endif
52 #include "ethernet.h"
53 #endif
54 
55 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0) || \
56  (defined(CONFIG_PREEMPT_RT_FULL) && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
57 # define ec_rt_lock_interruptible(lock) \
58  rt_mutex_lock_interruptible(lock)
59 #else
60 # define ec_rt_lock_interruptible(lock) \
61  rt_mutex_lock_interruptible(lock, 0)
62 #endif
63 
64 #include "master.h"
65 
66 /****************************************************************************/
67 
70 #define DEBUG_INJECT 0
71 
74 #define FORCE_OUTPUT_CORRUPTED 0
75 
77 #define EC_SDO_INJECTION_TIMEOUT 10000
78 
79 #ifdef EC_HAVE_CYCLES
80 
83 static cycles_t timeout_cycles;
84 
87 static cycles_t ext_injection_timeout_cycles;
88 
89 #else
90 
93 static unsigned long timeout_jiffies;
94 
97 static unsigned long ext_injection_timeout_jiffies;
98 
99 #endif
100 
103 const unsigned int rate_intervals[] = {
104  1, 10, 60
105 };
106 
107 /****************************************************************************/
108 
112 int ec_master_thread_start(ec_master_t *, int (*)(void *), const char *);
118 int ec_master_calc_topology_rec(ec_master_t *, ec_slave_t *, unsigned int *);
121 static int ec_master_idle_thread(void *);
122 static int ec_master_operation_thread(void *);
123 #ifdef EC_EOE
124 static int ec_master_eoe_thread(void *);
125 #endif
129 static void sc_reset_task_kicker(struct irq_work *work);
130 static void sc_reset_task(struct work_struct *work);
131 
132 /****************************************************************************/
133 
137 {
138 #ifdef EC_HAVE_CYCLES
139  timeout_cycles = (cycles_t) EC_IO_TIMEOUT /* us */ * (cpu_khz / 1000);
140  ext_injection_timeout_cycles =
141  (cycles_t) EC_SDO_INJECTION_TIMEOUT /* us */ * (cpu_khz / 1000);
142 #else
143  // one jiffy may always elapse between time measurement
144  timeout_jiffies = max(EC_IO_TIMEOUT * HZ / 1000000, 1);
146  max(EC_SDO_INJECTION_TIMEOUT * HZ / 1000000, 1);
147 #endif
148 }
149 
150 /****************************************************************************/
151 
158  unsigned int index,
159  const uint8_t *main_mac,
160  const uint8_t *backup_mac,
161  dev_t device_number,
162  struct class *class,
163  unsigned int debug_level,
164  unsigned int run_on_cpu
165  )
166 {
167  int ret;
168  unsigned int dev_idx, i;
169 
170  master->index = index;
171  master->reserved = 0;
172 
173  sema_init(&master->master_sem, 1);
174 
175  for (dev_idx = EC_DEVICE_MAIN; dev_idx < EC_MAX_NUM_DEVICES; dev_idx++) {
176  master->macs[dev_idx] = NULL;
177  }
178 
179  master->macs[EC_DEVICE_MAIN] = main_mac;
180 
181 #if EC_MAX_NUM_DEVICES > 1
182  master->macs[EC_DEVICE_BACKUP] = backup_mac;
183  master->num_devices = 1 + !ec_mac_is_zero(backup_mac);
184 #else
185  if (!ec_mac_is_zero(backup_mac)) {
186  EC_MASTER_WARN(master, "Ignoring backup MAC address!");
187  }
188 #endif
189 
191 
192  sema_init(&master->device_sem, 1);
193 
194  master->phase = EC_ORPHANED;
195  master->active = 0;
196  master->config_changed = 0;
197  master->injection_seq_fsm = 0;
198  master->injection_seq_rt = 0;
199 
200  master->slaves = NULL;
201  master->slave_count = 0;
202 
203  INIT_LIST_HEAD(&master->configs);
204  INIT_LIST_HEAD(&master->domains);
205 
206  master->app_time = 0ULL;
207  master->dc_ref_time = 0ULL;
208 
209  master->scan_busy = 0;
210  master->scan_index = 0;
211  master->allow_scan = 1;
212  sema_init(&master->scan_sem, 1);
213  init_waitqueue_head(&master->scan_queue);
214 
215  master->config_busy = 0;
216  sema_init(&master->config_sem, 1);
217  init_waitqueue_head(&master->config_queue);
218 
219  INIT_LIST_HEAD(&master->datagram_queue);
220  master->datagram_index = 0;
221 
222  INIT_LIST_HEAD(&master->ext_datagram_queue);
223  sema_init(&master->ext_queue_sem, 1);
224 
225  master->ext_ring_idx_rt = 0;
226  master->ext_ring_idx_fsm = 0;
227 
228  // init external datagram ring
229  for (i = 0; i < EC_EXT_RING_SIZE; i++) {
230  ec_datagram_t *datagram = &master->ext_datagram_ring[i];
231  ec_datagram_init(datagram);
232  snprintf(datagram->name, EC_DATAGRAM_NAME_SIZE, "ext-%u", i);
233  }
234 
235  // send interval in IDLE phase
236  ec_master_set_send_interval(master, 1000000 / HZ);
237 
238  master->fsm_slave = NULL;
239  INIT_LIST_HEAD(&master->fsm_exec_list);
240  master->fsm_exec_count = 0U;
241 
242  master->debug_level = debug_level;
243  master->run_on_cpu = run_on_cpu;
244  master->stats.timeouts = 0;
245  master->stats.corrupted = 0;
246  master->stats.unmatched = 0;
247  master->stats.output_jiffies = 0;
248 
249  master->thread = NULL;
250 
251 #ifdef EC_EOE
252  master->eoe_thread = NULL;
253  INIT_LIST_HEAD(&master->eoe_handlers);
254 #endif
255 
256  rt_mutex_init(&master->io_mutex);
257  master->send_cb = NULL;
258  master->receive_cb = NULL;
259  master->cb_data = NULL;
260  master->app_send_cb = NULL;
261  master->app_receive_cb = NULL;
262  master->app_cb_data = NULL;
263 
264  INIT_LIST_HEAD(&master->sii_requests);
265  INIT_LIST_HEAD(&master->emerg_reg_requests);
266 
267  init_waitqueue_head(&master->request_queue);
268 
269  // init devices
270  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
271  dev_idx++) {
272  ret = ec_device_init(&master->devices[dev_idx], master);
273  if (ret < 0) {
274  goto out_clear_devices;
275  }
276  }
277 
278  // init state machine datagram
279  ec_datagram_init(&master->fsm_datagram);
280  snprintf(master->fsm_datagram.name, EC_DATAGRAM_NAME_SIZE, "master-fsm");
282  if (ret < 0) {
284  EC_MASTER_ERR(master, "Failed to allocate FSM datagram.\n");
285  goto out_clear_devices;
286  }
287 
288  // create state machine object
289  ec_fsm_master_init(&master->fsm, master, &master->fsm_datagram);
290 
291  // alloc external datagram ring
292  for (i = 0; i < EC_EXT_RING_SIZE; i++) {
293  ec_datagram_t *datagram = &master->ext_datagram_ring[i];
294  ret = ec_datagram_prealloc(datagram, EC_MAX_DATA_SIZE);
295  if (ret) {
296  EC_MASTER_ERR(master, "Failed to allocate external"
297  " datagram %u.\n", i);
298  goto out_clear_ext_datagrams;
299  }
300  }
301 
302  // init reference sync datagram
304  snprintf(master->ref_sync_datagram.name, EC_DATAGRAM_NAME_SIZE,
305  "refsync");
306  ret = ec_datagram_prealloc(&master->ref_sync_datagram, 4);
307  if (ret < 0) {
309  EC_MASTER_ERR(master, "Failed to allocate reference"
310  " synchronisation datagram.\n");
311  goto out_clear_ext_datagrams;
312  }
313 
314  // init sync datagram
316  snprintf(master->sync_datagram.name, EC_DATAGRAM_NAME_SIZE, "sync");
317  ret = ec_datagram_prealloc(&master->sync_datagram, 4);
318  if (ret < 0) {
320  EC_MASTER_ERR(master, "Failed to allocate"
321  " synchronisation datagram.\n");
322  goto out_clear_ref_sync;
323  }
324 
325  // init sync monitor datagram
327  snprintf(master->sync_mon_datagram.name, EC_DATAGRAM_NAME_SIZE,
328  "syncmon");
329  ret = ec_datagram_brd(&master->sync_mon_datagram, 0x092c, 4);
330  if (ret < 0) {
332  EC_MASTER_ERR(master, "Failed to allocate sync"
333  " monitoring datagram.\n");
334  goto out_clear_sync;
335  }
336 
337  master->dc_ref_config = NULL;
338  master->dc_ref_clock = NULL;
339 
340  INIT_WORK(&master->sc_reset_work, sc_reset_task);
341  init_irq_work(&master->sc_reset_work_kicker, sc_reset_task_kicker);
342 
343  // init character device
344  ret = ec_cdev_init(&master->cdev, master, device_number);
345  if (ret)
346  goto out_clear_sync_mon;
347 
348  master->class_device = device_create(class, NULL,
349  MKDEV(MAJOR(device_number), master->index), NULL,
350  "EtherCAT%u", master->index);
351  if (IS_ERR(master->class_device)) {
352  EC_MASTER_ERR(master, "Failed to create class device!\n");
353  ret = PTR_ERR(master->class_device);
354  goto out_clear_cdev;
355  }
356 
357 #ifdef EC_RTDM
358  // init RTDM device
359  ret = ec_rtdm_dev_init(&master->rtdm_dev, master);
360  if (ret) {
361  goto out_unregister_class_device;
362  }
363 #endif
364 
365  return 0;
366 
367 #ifdef EC_RTDM
368 out_unregister_class_device:
369  device_unregister(master->class_device);
370 #endif
371 out_clear_cdev:
372  ec_cdev_clear(&master->cdev);
373 out_clear_sync_mon:
375 out_clear_sync:
377 out_clear_ref_sync:
379 out_clear_ext_datagrams:
380  for (i = 0; i < EC_EXT_RING_SIZE; i++) {
382  }
383  ec_fsm_master_clear(&master->fsm);
385 out_clear_devices:
386  for (; dev_idx > 0; dev_idx--) {
387  ec_device_clear(&master->devices[dev_idx - 1]);
388  }
389  return ret;
390 }
391 
392 /****************************************************************************/
393 
397  ec_master_t *master
398  )
399 {
400  unsigned int dev_idx, i;
401 
402 #ifdef EC_RTDM
403  ec_rtdm_dev_clear(&master->rtdm_dev);
404 #endif
405 
406  device_unregister(master->class_device);
407 
408  ec_cdev_clear(&master->cdev);
409 
410  irq_work_sync(&master->sc_reset_work_kicker);
411  cancel_work_sync(&master->sc_reset_work);
412 
413 #ifdef EC_EOE
415 #endif
416  ec_master_clear_domains(master);
418  ec_master_clear_slaves(master);
419 
423 
424  for (i = 0; i < EC_EXT_RING_SIZE; i++) {
426  }
427 
428  ec_fsm_master_clear(&master->fsm);
430 
431  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
432  dev_idx++) {
433  ec_device_clear(&master->devices[dev_idx]);
434  }
435 }
436 
437 /****************************************************************************/
438 
439 #ifdef EC_EOE
440 
443  ec_master_t *master
444  )
445 {
446  ec_eoe_t *eoe, *next;
447 
448  list_for_each_entry_safe(eoe, next, &master->eoe_handlers, list) {
449  list_del(&eoe->list);
450  ec_eoe_clear(eoe);
451  kfree(eoe);
452  }
453 }
454 #endif
455 
456 /****************************************************************************/
457 
461 {
462  ec_slave_config_t *sc, *next;
463 
464  master->dc_ref_config = NULL;
465  master->fsm.sdo_request = NULL; // mark sdo_request as invalid
466 
467  list_for_each_entry_safe(sc, next, &master->configs, list) {
468  list_del(&sc->list);
470  kfree(sc);
471  }
472 }
473 
474 /****************************************************************************/
475 
479 {
480  ec_slave_t *slave;
481 
482  master->dc_ref_clock = NULL;
483 
484  // External requests are obsolete, so we wake pending waiters and remove
485  // them from the list.
486 
487  while (!list_empty(&master->sii_requests)) {
488  ec_sii_write_request_t *request =
489  list_entry(master->sii_requests.next,
490  ec_sii_write_request_t, list);
491  list_del_init(&request->list); // dequeue
492  EC_MASTER_WARN(master, "Discarding SII request, slave %u about"
493  " to be deleted.\n", request->slave->ring_position);
494  request->state = EC_INT_REQUEST_FAILURE;
495  wake_up_all(&master->request_queue);
496  }
497 
498  master->fsm_slave = NULL;
499  INIT_LIST_HEAD(&master->fsm_exec_list);
500  master->fsm_exec_count = 0;
501 
502  for (slave = master->slaves;
503  slave < master->slaves + master->slave_count;
504  slave++) {
505  ec_slave_clear(slave);
506  }
507 
508  if (master->slaves) {
509  kfree(master->slaves);
510  master->slaves = NULL;
511  }
512 
513  master->slave_count = 0;
514 }
515 
516 /****************************************************************************/
517 
521 {
522  ec_domain_t *domain, *next;
523 
524  list_for_each_entry_safe(domain, next, &master->domains, list) {
525  list_del(&domain->list);
526  ec_domain_clear(domain);
527  kfree(domain);
528  }
529 }
530 
531 /****************************************************************************/
532 
536  ec_master_t *master
537  )
538 {
539  down(&master->master_sem);
540  ec_master_clear_domains(master);
542  up(&master->master_sem);
543 }
544 
545 /****************************************************************************/
546 
550  void *cb_data
551  )
552 {
553  ec_master_t *master = (ec_master_t *) cb_data;
554  if (ec_rt_lock_interruptible(&master->io_mutex))
555  return;
556  ecrt_master_send_ext(master);
557  rt_mutex_unlock(&master->io_mutex);
558 }
559 
560 /****************************************************************************/
561 
565  void *cb_data
566  )
567 {
568  ec_master_t *master = (ec_master_t *) cb_data;
569  if (ec_rt_lock_interruptible(&master->io_mutex))
570  return;
571  ecrt_master_receive(master);
572  rt_mutex_unlock(&master->io_mutex);
573 }
574 
575 /****************************************************************************/
576 
583  ec_master_t *master,
584  int (*thread_func)(void *),
585  const char *name
586  )
587 {
588  EC_MASTER_INFO(master, "Starting %s thread.\n", name);
589  master->thread = kthread_create(thread_func, master, name);
590  if (IS_ERR(master->thread)) {
591  int err = (int) PTR_ERR(master->thread);
592  EC_MASTER_ERR(master, "Failed to start master thread (error %i)!\n",
593  err);
594  master->thread = NULL;
595  return err;
596  }
597  if (0xffffffff != master->run_on_cpu) {
598  EC_MASTER_INFO(master, " binding thread to cpu %u\n",master->run_on_cpu);
599  kthread_bind(master->thread,master->run_on_cpu);
600  }
601  /* Ignoring return value of wake_up_process */
602  (void) wake_up_process(master->thread);
603 
604  return 0;
605 }
606 
607 /****************************************************************************/
608 
612  ec_master_t *master
613  )
614 {
615  unsigned long sleep_jiffies;
616 
617  if (!master->thread) {
618  EC_MASTER_WARN(master, "%s(): Already finished!\n", __func__);
619  return;
620  }
621 
622  EC_MASTER_DBG(master, 1, "Stopping master thread.\n");
623 
624  kthread_stop(master->thread);
625  master->thread = NULL;
626  EC_MASTER_INFO(master, "Master thread exited.\n");
627 
628  if (master->fsm_datagram.state != EC_DATAGRAM_SENT) {
629  return;
630  }
631 
632  // wait for FSM datagram
633  sleep_jiffies = max(HZ / 100, 1); // 10 ms, at least 1 jiffy
634  schedule_timeout(sleep_jiffies);
635 }
636 
637 /****************************************************************************/
638 
644  ec_master_t *master
645  )
646 {
647  int ret;
648  ec_device_index_t dev_idx;
649 
650  EC_MASTER_DBG(master, 1, "ORPHANED -> IDLE.\n");
651 
654  master->cb_data = master;
655 
656  master->phase = EC_IDLE;
657 
658  // reset number of responding slaves to trigger scanning
659  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
660  dev_idx++) {
661  master->fsm.slaves_responding[dev_idx] = 0;
662  }
663 
665  "EtherCAT-IDLE");
666  if (ret)
667  master->phase = EC_ORPHANED;
668 
669  return ret;
670 }
671 
672 /****************************************************************************/
673 
677 {
678  EC_MASTER_DBG(master, 1, "IDLE -> ORPHANED.\n");
679 
680  master->phase = EC_ORPHANED;
681 
682 #ifdef EC_EOE
683  ec_master_eoe_stop(master);
684 #endif
685  ec_master_thread_stop(master);
686 
687  down(&master->master_sem);
688  ec_master_clear_slaves(master);
689  up(&master->master_sem);
690 
691  ec_fsm_master_reset(&master->fsm);
692 }
693 
694 /****************************************************************************/
695 
701  ec_master_t *master
702  )
703 {
704  int ret = 0;
705  ec_slave_t *slave;
706 #ifdef EC_EOE
707  ec_eoe_t *eoe;
708 #endif
709 
710  EC_MASTER_DBG(master, 1, "IDLE -> OPERATION.\n");
711 
712  down(&master->config_sem);
713  if (master->config_busy) {
714  up(&master->config_sem);
715 
716  // wait for slave configuration to complete
717  ret = wait_event_interruptible(master->config_queue,
718  !master->config_busy);
719  if (ret) {
720  EC_MASTER_INFO(master, "Finishing slave configuration"
721  " interrupted by signal.\n");
722  goto out_return;
723  }
724 
725  EC_MASTER_DBG(master, 1, "Waiting for pending slave"
726  " configuration returned.\n");
727  } else {
728  up(&master->config_sem);
729  }
730 
731  down(&master->scan_sem);
732  master->allow_scan = 0; // 'lock' the slave list
733  if (!master->scan_busy) {
734  up(&master->scan_sem);
735  } else {
736  up(&master->scan_sem);
737 
738  // wait for slave scan to complete
739  ret = wait_event_interruptible(master->scan_queue,
740  !master->scan_busy);
741  if (ret) {
742  EC_MASTER_INFO(master, "Waiting for slave scan"
743  " interrupted by signal.\n");
744  goto out_allow;
745  }
746 
747  EC_MASTER_DBG(master, 1, "Waiting for pending"
748  " slave scan returned.\n");
749  }
750 
751  // set states for all slaves
752  for (slave = master->slaves;
753  slave < master->slaves + master->slave_count;
754  slave++) {
756  }
757 
758 #ifdef EC_EOE
759  // ... but set EoE slaves to OP
760  list_for_each_entry(eoe, &master->eoe_handlers, list) {
761  if (ec_eoe_is_open(eoe))
763  }
764 #endif
765 
766  master->phase = EC_OPERATION;
767  master->app_send_cb = NULL;
768  master->app_receive_cb = NULL;
769  master->app_cb_data = NULL;
770  return ret;
771 
772 out_allow:
773  master->allow_scan = 1;
774 out_return:
775  return ret;
776 }
777 
778 /****************************************************************************/
779 
783  ec_master_t *master
784  )
785 {
786  if (master->active) {
787  ecrt_master_deactivate(master); // also clears config
788  } else {
789  ec_master_clear_config(master);
790  }
791 
792  /* Re-allow scanning for IDLE phase. */
793  master->allow_scan = 1;
794 
795  EC_MASTER_DBG(master, 1, "OPERATION -> IDLE.\n");
796 
797  master->phase = EC_IDLE;
798 }
799 
800 /****************************************************************************/
801 
805  ec_master_t *master
806  )
807 {
808  ec_datagram_t *datagram;
809  size_t queue_size = 0, new_queue_size = 0;
810 #if DEBUG_INJECT
811  unsigned int datagram_count = 0;
812 #endif
813 
814  if (master->ext_ring_idx_rt == master->ext_ring_idx_fsm) {
815  // nothing to inject
816  return;
817  }
818 
819  list_for_each_entry(datagram, &master->datagram_queue, queue) {
820  if (datagram->state == EC_DATAGRAM_QUEUED) {
821  queue_size += datagram->data_size;
822  }
823  }
824 
825 #if DEBUG_INJECT
826  EC_MASTER_DBG(master, 1, "Injecting datagrams, queue_size=%zu\n",
827  queue_size);
828 #endif
829 
830  while (master->ext_ring_idx_rt != master->ext_ring_idx_fsm) {
831  datagram = &master->ext_datagram_ring[master->ext_ring_idx_rt];
832 
833  if (datagram->state != EC_DATAGRAM_INIT) {
834  // skip datagram
835  master->ext_ring_idx_rt =
836  (master->ext_ring_idx_rt + 1) % EC_EXT_RING_SIZE;
837  continue;
838  }
839 
840  new_queue_size = queue_size + datagram->data_size;
841  if (new_queue_size <= master->max_queue_size) {
842 #if DEBUG_INJECT
843  EC_MASTER_DBG(master, 1, "Injecting datagram %s"
844  " size=%zu, queue_size=%zu\n", datagram->name,
845  datagram->data_size, new_queue_size);
846  datagram_count++;
847 #endif
848 #ifdef EC_HAVE_CYCLES
849  datagram->cycles_sent = 0;
850 #endif
851  datagram->jiffies_sent = 0;
852  ec_master_queue_datagram(master, datagram);
853  queue_size = new_queue_size;
854  }
855  else if (datagram->data_size > master->max_queue_size) {
856  datagram->state = EC_DATAGRAM_ERROR;
857  EC_MASTER_ERR(master, "External datagram %s is too large,"
858  " size=%zu, max_queue_size=%zu\n",
859  datagram->name, datagram->data_size,
860  master->max_queue_size);
861  }
862  else { // datagram does not fit in the current cycle
863 #ifdef EC_HAVE_CYCLES
864  cycles_t cycles_now = get_cycles();
865 
866  if (cycles_now - datagram->cycles_sent
867  > ext_injection_timeout_cycles)
868 #else
869  if (jiffies - datagram->jiffies_sent
871 #endif
872  {
873 #if defined EC_RT_SYSLOG || DEBUG_INJECT
874  unsigned int time_us;
875 #endif
876 
877  datagram->state = EC_DATAGRAM_ERROR;
878 
879 #if defined EC_RT_SYSLOG || DEBUG_INJECT
880 #ifdef EC_HAVE_CYCLES
881  time_us = (unsigned int)
882  ((cycles_now - datagram->cycles_sent) * 1000LL)
883  / cpu_khz;
884 #else
885  time_us = (unsigned int)
886  ((jiffies - datagram->jiffies_sent) * 1000000 / HZ);
887 #endif
888  EC_MASTER_ERR(master, "Timeout %u us: Injecting"
889  " external datagram %s size=%zu,"
890  " max_queue_size=%zu\n", time_us, datagram->name,
891  datagram->data_size, master->max_queue_size);
892 #endif
893  }
894  else {
895 #if DEBUG_INJECT
896  EC_MASTER_DBG(master, 1, "Deferred injecting"
897  " external datagram %s size=%u, queue_size=%u\n",
898  datagram->name, datagram->data_size, queue_size);
899 #endif
900  break;
901  }
902  }
903 
904  master->ext_ring_idx_rt =
905  (master->ext_ring_idx_rt + 1) % EC_EXT_RING_SIZE;
906  }
907 
908 #if DEBUG_INJECT
909  EC_MASTER_DBG(master, 1, "Injected %u datagrams.\n", datagram_count);
910 #endif
911 }
912 
913 /****************************************************************************/
914 
919  ec_master_t *master,
920  unsigned int send_interval
921  )
922 {
923  master->send_interval = send_interval;
924  master->max_queue_size =
925  (send_interval * 1000) / EC_BYTE_TRANSMISSION_TIME_NS;
926  master->max_queue_size -= master->max_queue_size / 10;
927 }
928 
929 /****************************************************************************/
930 
936  ec_master_t *master
937  )
938 {
939  if ((master->ext_ring_idx_fsm + 1) % EC_EXT_RING_SIZE !=
940  master->ext_ring_idx_rt) {
941  ec_datagram_t *datagram =
942  &master->ext_datagram_ring[master->ext_ring_idx_fsm];
943  return datagram;
944  }
945  else {
946  return NULL;
947  }
948 }
949 
950 /****************************************************************************/
951 
955  ec_master_t *master,
956  ec_datagram_t *datagram
957  )
958 {
959  ec_datagram_t *queued_datagram;
960 
961  /* It is possible, that a datagram in the queue is re-initialized with the
962  * ec_datagram_<type>() methods and then shall be queued with this method.
963  * In that case, the state is already reset to EC_DATAGRAM_INIT. Check if
964  * the datagram is queued to avoid duplicate queuing (which results in an
965  * infinite loop!). Set the state to EC_DATAGRAM_QUEUED again, probably
966  * causing an unmatched datagram. */
967  list_for_each_entry(queued_datagram, &master->datagram_queue, queue) {
968  if (queued_datagram == datagram) {
969  datagram->skip_count++;
970 #ifdef EC_RT_SYSLOG
971  EC_MASTER_DBG(master, 1,
972  "Datagram %p already queued (skipping).\n", datagram);
973 #endif
974  datagram->state = EC_DATAGRAM_QUEUED;
975  return;
976  }
977  }
978 
979  list_add_tail(&datagram->queue, &master->datagram_queue);
980  datagram->state = EC_DATAGRAM_QUEUED;
981 }
982 
983 /****************************************************************************/
984 
988  ec_master_t *master,
989  ec_datagram_t *datagram
990  )
991 {
992  down(&master->ext_queue_sem);
993  list_add_tail(&datagram->ext_queue, &master->ext_datagram_queue);
994  up(&master->ext_queue_sem);
995 }
996 
997 /****************************************************************************/
998 
1003  ec_master_t *master,
1004  ec_device_index_t device_index
1005  )
1006 {
1007  ec_datagram_t *datagram, *next;
1008  size_t datagram_size;
1009  uint8_t *frame_data, *cur_data = NULL;
1010  void *follows_word;
1011 #ifdef EC_HAVE_CYCLES
1012  cycles_t cycles_start, cycles_sent, cycles_end;
1013 #endif
1014  unsigned long jiffies_sent;
1015  unsigned int frame_count, more_datagrams_waiting;
1016  struct list_head sent_datagrams;
1017 
1018 #ifdef EC_HAVE_CYCLES
1019  cycles_start = get_cycles();
1020 #endif
1021  frame_count = 0;
1022  INIT_LIST_HEAD(&sent_datagrams);
1023 
1024  EC_MASTER_DBG(master, 2, "%s(device_index = %u)\n",
1025  __func__, device_index);
1026 
1027  do {
1028  frame_data = NULL;
1029  follows_word = NULL;
1030  more_datagrams_waiting = 0;
1031 
1032  // fill current frame with datagrams
1033  list_for_each_entry(datagram, &master->datagram_queue, queue) {
1034  if (datagram->state != EC_DATAGRAM_QUEUED ||
1035  datagram->device_index != device_index) {
1036  continue;
1037  }
1038 
1039  if (!frame_data) {
1040  // fetch pointer to transmit socket buffer
1041  frame_data =
1042  ec_device_tx_data(&master->devices[device_index]);
1043  cur_data = frame_data + EC_FRAME_HEADER_SIZE;
1044  }
1045 
1046  // does the current datagram fit in the frame?
1047  datagram_size = EC_DATAGRAM_HEADER_SIZE + datagram->data_size
1049  if (cur_data - frame_data + datagram_size > ETH_DATA_LEN) {
1050  more_datagrams_waiting = 1;
1051  break;
1052  }
1053 
1054  list_add_tail(&datagram->sent, &sent_datagrams);
1055  datagram->index = master->datagram_index++;
1056 
1057  EC_MASTER_DBG(master, 2, "Adding datagram 0x%02X\n",
1058  datagram->index);
1059 
1060  // set "datagram following" flag in previous datagram
1061  if (follows_word) {
1062  EC_WRITE_U16(follows_word,
1063  EC_READ_U16(follows_word) | 0x8000);
1064  }
1065 
1066  // EtherCAT datagram header
1067  EC_WRITE_U8 (cur_data, datagram->type);
1068  EC_WRITE_U8 (cur_data + 1, datagram->index);
1069  memcpy(cur_data + 2, datagram->address, EC_ADDR_LEN);
1070  EC_WRITE_U16(cur_data + 6, datagram->data_size & 0x7FF);
1071  EC_WRITE_U16(cur_data + 8, 0x0000);
1072  follows_word = cur_data + 6;
1073  cur_data += EC_DATAGRAM_HEADER_SIZE;
1074 
1075  // EtherCAT datagram data
1076  memcpy(cur_data, datagram->data, datagram->data_size);
1077  cur_data += datagram->data_size;
1078 
1079  // EtherCAT datagram footer
1080  EC_WRITE_U16(cur_data, 0x0000); // reset working counter
1081  cur_data += EC_DATAGRAM_FOOTER_SIZE;
1082  }
1083 
1084  if (list_empty(&sent_datagrams)) {
1085  EC_MASTER_DBG(master, 2, "nothing to send.\n");
1086  break;
1087  }
1088 
1089  // EtherCAT frame header
1090  EC_WRITE_U16(frame_data, ((cur_data - frame_data
1091  - EC_FRAME_HEADER_SIZE) & 0x7FF) | 0x1000);
1092 
1093  // pad frame
1094  while (cur_data - frame_data < ETH_ZLEN - ETH_HLEN)
1095  EC_WRITE_U8(cur_data++, 0x00);
1096 
1097  EC_MASTER_DBG(master, 2, "frame size: %zu\n", cur_data - frame_data);
1098 
1099  // send frame
1100  ec_device_send(&master->devices[device_index],
1101  cur_data - frame_data);
1102 #ifdef EC_HAVE_CYCLES
1103  cycles_sent = get_cycles();
1104 #endif
1105  jiffies_sent = jiffies;
1106 
1107  // set datagram states and sending timestamps
1108  list_for_each_entry_safe(datagram, next, &sent_datagrams, sent) {
1109  datagram->state = EC_DATAGRAM_SENT;
1110 #ifdef EC_HAVE_CYCLES
1111  datagram->cycles_sent = cycles_sent;
1112 #endif
1113  datagram->jiffies_sent = jiffies_sent;
1114  list_del_init(&datagram->sent); // empty list of sent datagrams
1115  }
1116 
1117  frame_count++;
1118  }
1119  while (more_datagrams_waiting);
1120 
1121 #ifdef EC_HAVE_CYCLES
1122  if (unlikely(master->debug_level > 1)) {
1123  cycles_end = get_cycles();
1124  EC_MASTER_DBG(master, 0, "%s()"
1125  " sent %u frames in %uus.\n", __func__, frame_count,
1126  (unsigned int) (cycles_end - cycles_start) * 1000 / cpu_khz);
1127  }
1128 #endif
1129 }
1130 
1131 /****************************************************************************/
1132 
1140  ec_master_t *master,
1141  ec_device_t *device,
1142  const uint8_t *frame_data,
1143  size_t size
1144  )
1145 {
1146  size_t frame_size, data_size;
1147  uint8_t datagram_type, datagram_index;
1148  unsigned int cmd_follows, matched;
1149  const uint8_t *cur_data;
1150  ec_datagram_t *datagram;
1151 
1152  if (unlikely(size < EC_FRAME_HEADER_SIZE)) {
1153  if (master->debug_level || FORCE_OUTPUT_CORRUPTED) {
1154  EC_MASTER_DBG(master, 0, "Corrupted frame received"
1155  " on %s (size %zu < %u byte):\n",
1156  device->dev->name, size, EC_FRAME_HEADER_SIZE);
1157  ec_print_data(frame_data, size);
1158  }
1159  master->stats.corrupted++;
1160 #ifdef EC_RT_SYSLOG
1161  ec_master_output_stats(master);
1162 #endif
1163  return;
1164  }
1165 
1166  cur_data = frame_data;
1167 
1168  // check length of entire frame
1169  frame_size = EC_READ_U16(cur_data) & 0x07FF;
1170  cur_data += EC_FRAME_HEADER_SIZE;
1171 
1172  if (unlikely(frame_size > size)) {
1173  if (master->debug_level || FORCE_OUTPUT_CORRUPTED) {
1174  EC_MASTER_DBG(master, 0, "Corrupted frame received"
1175  " on %s (invalid frame size %zu for "
1176  "received size %zu):\n", device->dev->name,
1177  frame_size, size);
1178  ec_print_data(frame_data, size);
1179  }
1180  master->stats.corrupted++;
1181 #ifdef EC_RT_SYSLOG
1182  ec_master_output_stats(master);
1183 #endif
1184  return;
1185  }
1186 
1187  cmd_follows = 1;
1188  while (cmd_follows) {
1189  // process datagram header
1190  datagram_type = EC_READ_U8 (cur_data);
1191  datagram_index = EC_READ_U8 (cur_data + 1);
1192  data_size = EC_READ_U16(cur_data + 6) & 0x07FF;
1193  cmd_follows = EC_READ_U16(cur_data + 6) & 0x8000;
1194  cur_data += EC_DATAGRAM_HEADER_SIZE;
1195 
1196  if (unlikely(cur_data - frame_data
1197  + data_size + EC_DATAGRAM_FOOTER_SIZE > size)) {
1198  if (master->debug_level || FORCE_OUTPUT_CORRUPTED) {
1199  EC_MASTER_DBG(master, 0, "Corrupted frame received"
1200  " on %s (invalid data size %zu):\n",
1201  device->dev->name, data_size);
1202  ec_print_data(frame_data, size);
1203  }
1204  master->stats.corrupted++;
1205 #ifdef EC_RT_SYSLOG
1206  ec_master_output_stats(master);
1207 #endif
1208  return;
1209  }
1210 
1211  // search for matching datagram in the queue
1212  matched = 0;
1213  list_for_each_entry(datagram, &master->datagram_queue, queue) {
1214  if (datagram->index == datagram_index
1215  && datagram->state == EC_DATAGRAM_SENT
1216  && datagram->type == datagram_type
1217  && datagram->data_size == data_size) {
1218  matched = 1;
1219  break;
1220  }
1221  }
1222 
1223  // no matching datagram was found
1224  if (!matched) {
1225  master->stats.unmatched++;
1226 #ifdef EC_RT_SYSLOG
1227  ec_master_output_stats(master);
1228 #endif
1229 
1230  if (unlikely(master->debug_level > 0)) {
1231  EC_MASTER_DBG(master, 0, "UNMATCHED datagram:\n");
1233  EC_DATAGRAM_HEADER_SIZE + data_size
1235 #ifdef EC_DEBUG_RING
1236  ec_device_debug_ring_print(&master->devices[EC_DEVICE_MAIN]);
1237 #endif
1238  }
1239 
1240  cur_data += data_size + EC_DATAGRAM_FOOTER_SIZE;
1241  continue;
1242  }
1243 
1244  if (datagram->type != EC_DATAGRAM_APWR &&
1245  datagram->type != EC_DATAGRAM_FPWR &&
1246  datagram->type != EC_DATAGRAM_BWR &&
1247  datagram->type != EC_DATAGRAM_LWR) {
1248  // copy received data into the datagram memory,
1249  // if something has been read
1250  memcpy(datagram->data, cur_data, data_size);
1251  }
1252  cur_data += data_size;
1253 
1254  // set the datagram's working counter
1255  datagram->working_counter = EC_READ_U16(cur_data);
1256  cur_data += EC_DATAGRAM_FOOTER_SIZE;
1257 
1258  // dequeue the received datagram
1259  datagram->state = EC_DATAGRAM_RECEIVED;
1260 #ifdef EC_HAVE_CYCLES
1261  datagram->cycles_received =
1262  master->devices[EC_DEVICE_MAIN].cycles_poll;
1263 #endif
1264  datagram->jiffies_received =
1266  list_del_init(&datagram->queue);
1267  }
1268 }
1269 
1270 /****************************************************************************/
1271 
1278 {
1279  if (unlikely(jiffies - master->stats.output_jiffies >= HZ)) {
1280  master->stats.output_jiffies = jiffies;
1281 
1282  if (master->stats.timeouts) {
1283  EC_MASTER_WARN(master, "%u datagram%s TIMED OUT!\n",
1284  master->stats.timeouts,
1285  master->stats.timeouts == 1 ? "" : "s");
1286  master->stats.timeouts = 0;
1287  }
1288  if (master->stats.corrupted) {
1289  EC_MASTER_WARN(master, "%u frame%s CORRUPTED!\n",
1290  master->stats.corrupted,
1291  master->stats.corrupted == 1 ? "" : "s");
1292  master->stats.corrupted = 0;
1293  }
1294  if (master->stats.unmatched) {
1295  EC_MASTER_WARN(master, "%u datagram%s UNMATCHED!\n",
1296  master->stats.unmatched,
1297  master->stats.unmatched == 1 ? "" : "s");
1298  master->stats.unmatched = 0;
1299  }
1300  }
1301 }
1302 
1303 /****************************************************************************/
1304 
1308  ec_master_t *master
1309  )
1310 {
1311  unsigned int i;
1312 
1313  // zero frame statistics
1314  master->device_stats.tx_count = 0;
1315  master->device_stats.last_tx_count = 0;
1316  master->device_stats.rx_count = 0;
1317  master->device_stats.last_rx_count = 0;
1318  master->device_stats.tx_bytes = 0;
1319  master->device_stats.last_tx_bytes = 0;
1320  master->device_stats.rx_bytes = 0;
1321  master->device_stats.last_rx_bytes = 0;
1322  master->device_stats.last_loss = 0;
1323 
1324  for (i = 0; i < EC_RATE_COUNT; i++) {
1325  master->device_stats.tx_frame_rates[i] = 0;
1326  master->device_stats.rx_frame_rates[i] = 0;
1327  master->device_stats.tx_byte_rates[i] = 0;
1328  master->device_stats.rx_byte_rates[i] = 0;
1329  master->device_stats.loss_rates[i] = 0;
1330  }
1331 
1332  master->device_stats.jiffies = 0;
1333 }
1334 
1335 /****************************************************************************/
1336 
1340  ec_master_t *master
1341  )
1342 {
1343  ec_device_stats_t *s = &master->device_stats;
1344  s32 tx_frame_rate, rx_frame_rate, tx_byte_rate, rx_byte_rate, loss_rate;
1345  u64 loss;
1346  unsigned int i, dev_idx;
1347 
1348  // frame statistics
1349  if (likely(jiffies - s->jiffies < HZ)) {
1350  return;
1351  }
1352 
1353  tx_frame_rate = (s->tx_count - s->last_tx_count) * 1000;
1354  rx_frame_rate = (s->rx_count - s->last_rx_count) * 1000;
1355  tx_byte_rate = s->tx_bytes - s->last_tx_bytes;
1356  rx_byte_rate = s->rx_bytes - s->last_rx_bytes;
1357  loss = s->tx_count - s->rx_count;
1358  loss_rate = (loss - s->last_loss) * 1000;
1359 
1360  /* Low-pass filter:
1361  * Y_n = y_(n - 1) + T / tau * (x - y_(n - 1)) | T = 1
1362  * -> Y_n += (x - y_(n - 1)) / tau
1363  */
1364  for (i = 0; i < EC_RATE_COUNT; i++) {
1365  s32 n = rate_intervals[i];
1366  s->tx_frame_rates[i] += (tx_frame_rate - s->tx_frame_rates[i]) / n;
1367  s->rx_frame_rates[i] += (rx_frame_rate - s->rx_frame_rates[i]) / n;
1368  s->tx_byte_rates[i] += (tx_byte_rate - s->tx_byte_rates[i]) / n;
1369  s->rx_byte_rates[i] += (rx_byte_rate - s->rx_byte_rates[i]) / n;
1370  s->loss_rates[i] += (loss_rate - s->loss_rates[i]) / n;
1371  }
1372 
1373  s->last_tx_count = s->tx_count;
1374  s->last_rx_count = s->rx_count;
1375  s->last_tx_bytes = s->tx_bytes;
1376  s->last_rx_bytes = s->rx_bytes;
1377  s->last_loss = loss;
1378 
1379  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
1380  dev_idx++) {
1381  ec_device_update_stats(&master->devices[dev_idx]);
1382  }
1383 
1384  s->jiffies = jiffies;
1385 }
1386 
1387 /****************************************************************************/
1388 
1389 #ifdef EC_USE_HRTIMER
1390 
1391 /*
1392  * Sleep related functions:
1393  */
1394 static enum hrtimer_restart ec_master_nanosleep_wakeup(struct hrtimer *timer)
1395 {
1396  struct hrtimer_sleeper *t =
1397  container_of(timer, struct hrtimer_sleeper, timer);
1398  struct task_struct *task = t->task;
1399 
1400  t->task = NULL;
1401  if (task)
1402  wake_up_process(task);
1403 
1404  return HRTIMER_NORESTART;
1405 }
1406 
1407 /****************************************************************************/
1408 
1409 
1410 /****************************************************************************/
1411 
1412 void ec_master_nanosleep(const unsigned long nsecs)
1413 {
1414  struct hrtimer_sleeper t;
1415  enum hrtimer_mode mode = HRTIMER_MODE_REL;
1416 
1417  hrtimer_init(&t.timer, CLOCK_MONOTONIC, mode);
1418  t.timer.function = ec_master_nanosleep_wakeup;
1419  t.task = current;
1420  hrtimer_set_expires(&t.timer, ktime_set(0, nsecs));
1421 
1422  do {
1423  set_current_state(TASK_INTERRUPTIBLE);
1424  hrtimer_start(&t.timer, hrtimer_get_expires(&t.timer), mode);
1425 
1426  if (likely(t.task))
1427  schedule();
1428 
1429  hrtimer_cancel(&t.timer);
1430  mode = HRTIMER_MODE_ABS;
1431 
1432  } while (t.task && !signal_pending(current));
1433 }
1434 
1435 #endif // EC_USE_HRTIMER
1436 
1437 /****************************************************************************/
1438 
1442  ec_master_t *master
1443  )
1444 {
1445  ec_datagram_t *datagram;
1446  ec_fsm_slave_t *fsm, *next;
1447  unsigned int count = 0;
1448 
1449  list_for_each_entry_safe(fsm, next, &master->fsm_exec_list, list) {
1450  if (!fsm->datagram) {
1451  EC_MASTER_WARN(master, "Slave %u FSM has zero datagram."
1452  "This is a bug!\n", fsm->slave->ring_position);
1453  list_del_init(&fsm->list);
1454  master->fsm_exec_count--;
1455  return;
1456  }
1457 
1458  if (fsm->datagram->state == EC_DATAGRAM_INIT ||
1459  fsm->datagram->state == EC_DATAGRAM_QUEUED ||
1460  fsm->datagram->state == EC_DATAGRAM_SENT) {
1461  // previous datagram was not sent or received yet.
1462  // wait until next thread execution
1463  return;
1464  }
1465 
1466  datagram = ec_master_get_external_datagram(master);
1467  if (!datagram) {
1468  // no free datagrams at the moment
1469  EC_MASTER_WARN(master, "No free datagram during"
1470  " slave FSM execution. This is a bug!\n");
1471  continue;
1472  }
1473 
1474 #if DEBUG_INJECT
1475  EC_MASTER_DBG(master, 1, "Executing slave %u FSM.\n",
1476  fsm->slave->ring_position);
1477 #endif
1478  if (ec_fsm_slave_exec(fsm, datagram)) {
1479  // FSM consumed datagram
1480 #if DEBUG_INJECT
1481  EC_MASTER_DBG(master, 1, "FSM consumed datagram %s\n",
1482  datagram->name);
1483 #endif
1484  master->ext_ring_idx_fsm =
1485  (master->ext_ring_idx_fsm + 1) % EC_EXT_RING_SIZE;
1486  }
1487  else {
1488  // FSM finished
1489  list_del_init(&fsm->list);
1490  master->fsm_exec_count--;
1491 #if DEBUG_INJECT
1492  EC_MASTER_DBG(master, 1, "FSM finished. %u remaining.\n",
1493  master->fsm_exec_count);
1494 #endif
1495  }
1496  }
1497 
1498  while (master->fsm_exec_count < EC_EXT_RING_SIZE / 2
1499  && count < master->slave_count) {
1500 
1501  if (ec_fsm_slave_is_ready(&master->fsm_slave->fsm)) {
1502  datagram = ec_master_get_external_datagram(master);
1503 
1504  if (ec_fsm_slave_exec(&master->fsm_slave->fsm, datagram)) {
1505  master->ext_ring_idx_fsm =
1506  (master->ext_ring_idx_fsm + 1) % EC_EXT_RING_SIZE;
1507  list_add_tail(&master->fsm_slave->fsm.list,
1508  &master->fsm_exec_list);
1509  master->fsm_exec_count++;
1510 #if DEBUG_INJECT
1511  EC_MASTER_DBG(master, 1, "New slave %u FSM"
1512  " consumed datagram %s, now %u FSMs in list.\n",
1513  master->fsm_slave->ring_position, datagram->name,
1514  master->fsm_exec_count);
1515 #endif
1516  }
1517  }
1518 
1519  master->fsm_slave++;
1520  if (master->fsm_slave >= master->slaves + master->slave_count) {
1521  master->fsm_slave = master->slaves;
1522  }
1523  count++;
1524  }
1525 }
1526 
1527 /****************************************************************************/
1528 
1531 static int ec_master_idle_thread(void *priv_data)
1532 {
1533  ec_master_t *master = (ec_master_t *) priv_data;
1534  int fsm_exec;
1535 #ifdef EC_USE_HRTIMER
1536  size_t sent_bytes;
1537 #endif
1538 
1539  // send interval in IDLE phase
1540  ec_master_set_send_interval(master, 1000000 / HZ);
1541 
1542  EC_MASTER_DBG(master, 1, "Idle thread running with send interval = %u us,"
1543  " max data size=%zu\n", master->send_interval,
1544  master->max_queue_size);
1545 
1546  while (!kthread_should_stop()) {
1548 
1549  // receive
1550  if (ec_rt_lock_interruptible(&master->io_mutex))
1551  break;
1552  ecrt_master_receive(master);
1553  rt_mutex_unlock(&master->io_mutex);
1554 
1555  // execute master & slave state machines
1556  if (down_interruptible(&master->master_sem)) {
1557  break;
1558  }
1559 
1560  fsm_exec = ec_fsm_master_exec(&master->fsm);
1561 
1562  ec_master_exec_slave_fsms(master);
1563 
1564  up(&master->master_sem);
1565 
1566  // queue and send
1567  if (ec_rt_lock_interruptible(&master->io_mutex))
1568  break;
1569  if (fsm_exec) {
1570  ec_master_queue_datagram(master, &master->fsm_datagram);
1571  }
1572  ecrt_master_send(master);
1573 #ifdef EC_USE_HRTIMER
1574  sent_bytes = master->devices[EC_DEVICE_MAIN].tx_skb[
1575  master->devices[EC_DEVICE_MAIN].tx_ring_index]->len;
1576 #endif
1577  rt_mutex_unlock(&master->io_mutex);
1578 
1579  if (ec_fsm_master_idle(&master->fsm)) {
1580 #ifdef EC_USE_HRTIMER
1581  ec_master_nanosleep(master->send_interval * 1000);
1582 #else
1583  set_current_state(TASK_INTERRUPTIBLE);
1584  schedule_timeout(1);
1585 #endif
1586  } else {
1587 #ifdef EC_USE_HRTIMER
1588  ec_master_nanosleep(sent_bytes * EC_BYTE_TRANSMISSION_TIME_NS);
1589 #else
1590  schedule();
1591 #endif
1592  }
1593  }
1594 
1595  EC_MASTER_DBG(master, 1, "Master IDLE thread exiting...\n");
1596 
1597  return 0;
1598 }
1599 
1600 /****************************************************************************/
1601 
1604 static int ec_master_operation_thread(void *priv_data)
1605 {
1606  ec_master_t *master = (ec_master_t *) priv_data;
1607 
1608  EC_MASTER_DBG(master, 1, "Operation thread running"
1609  " with fsm interval = %u us, max data size=%zu\n",
1610  master->send_interval, master->max_queue_size);
1611 
1612  while (!kthread_should_stop()) {
1614 
1615  if (master->injection_seq_rt == master->injection_seq_fsm) {
1616  // output statistics
1617  ec_master_output_stats(master);
1618 
1619  // execute master & slave state machines
1620  if (down_interruptible(&master->master_sem)) {
1621  break;
1622  }
1623 
1624  if (ec_fsm_master_exec(&master->fsm)) {
1625  // Inject datagrams (let the RT thread queue them, see
1626  // ecrt_master_send())
1627  master->injection_seq_fsm++;
1628  }
1629 
1630  ec_master_exec_slave_fsms(master);
1631 
1632  up(&master->master_sem);
1633  }
1634 
1635 #ifdef EC_USE_HRTIMER
1636  // the op thread should not work faster than the sending RT thread
1637  ec_master_nanosleep(master->send_interval * 1000);
1638 #else
1639  if (ec_fsm_master_idle(&master->fsm)) {
1640  set_current_state(TASK_INTERRUPTIBLE);
1641  schedule_timeout(1);
1642  }
1643  else {
1644  schedule();
1645  }
1646 #endif
1647  }
1648 
1649  EC_MASTER_DBG(master, 1, "Master OP thread exiting...\n");
1650  return 0;
1651 }
1652 
1653 /****************************************************************************/
1654 
1655 #ifdef EC_EOE
1656 
1657 /* compatibility for priority changes */
1658 static inline void set_normal_priority(struct task_struct *p, int nice)
1659 {
1660 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 9, 0)
1661  sched_set_normal(p, nice);
1662 #else
1663  struct sched_param param = { .sched_priority = 0 };
1664  sched_setscheduler(p, SCHED_NORMAL, &param);
1665  set_user_nice(p, nice);
1666 #endif
1667 }
1668 
1669 /****************************************************************************/
1670 
1674 {
1675  if (master->eoe_thread) {
1676  EC_MASTER_WARN(master, "EoE already running!\n");
1677  return;
1678  }
1679 
1680  if (list_empty(&master->eoe_handlers))
1681  return;
1682 
1683  if (!master->send_cb || !master->receive_cb) {
1684  EC_MASTER_WARN(master, "No EoE processing"
1685  " because of missing callbacks!\n");
1686  return;
1687  }
1688 
1689  EC_MASTER_INFO(master, "Starting EoE thread.\n");
1690  master->eoe_thread = kthread_run(ec_master_eoe_thread, master,
1691  "EtherCAT-EoE");
1692  if (IS_ERR(master->eoe_thread)) {
1693  int err = (int) PTR_ERR(master->eoe_thread);
1694  EC_MASTER_ERR(master, "Failed to start EoE thread (error %i)!\n",
1695  err);
1696  master->eoe_thread = NULL;
1697  return;
1698  }
1699 
1700  set_normal_priority(master->eoe_thread, 0);
1701 }
1702 
1703 /****************************************************************************/
1704 
1708 {
1709  if (master->eoe_thread) {
1710  EC_MASTER_INFO(master, "Stopping EoE thread.\n");
1711 
1712  kthread_stop(master->eoe_thread);
1713  master->eoe_thread = NULL;
1714  EC_MASTER_INFO(master, "EoE thread exited.\n");
1715  }
1716 }
1717 
1718 /****************************************************************************/
1719 
1722 static int ec_master_eoe_thread(void *priv_data)
1723 {
1724  ec_master_t *master = (ec_master_t *) priv_data;
1725  ec_eoe_t *eoe;
1726  unsigned int none_open, sth_to_send, all_idle;
1727 
1728  EC_MASTER_DBG(master, 1, "EoE thread running.\n");
1729 
1730  while (!kthread_should_stop()) {
1731  none_open = 1;
1732  all_idle = 1;
1733 
1734  list_for_each_entry(eoe, &master->eoe_handlers, list) {
1735  if (ec_eoe_is_open(eoe)) {
1736  none_open = 0;
1737  break;
1738  }
1739  }
1740  if (none_open)
1741  goto schedule;
1742 
1743  // receive datagrams
1744  master->receive_cb(master->cb_data);
1745 
1746  // actual EoE processing
1747  sth_to_send = 0;
1748  list_for_each_entry(eoe, &master->eoe_handlers, list) {
1749  ec_eoe_run(eoe);
1750  if (eoe->queue_datagram) {
1751  sth_to_send = 1;
1752  }
1753  if (!ec_eoe_is_idle(eoe)) {
1754  all_idle = 0;
1755  }
1756  }
1757 
1758  if (sth_to_send) {
1759  list_for_each_entry(eoe, &master->eoe_handlers, list) {
1760  ec_eoe_queue(eoe);
1761  }
1762  // (try to) send datagrams
1763  master->send_cb(master->cb_data);
1764  }
1765 
1766 schedule:
1767  if (all_idle) {
1768  set_current_state(TASK_INTERRUPTIBLE);
1769  schedule_timeout(1);
1770  } else {
1771  schedule();
1772  }
1773  }
1774 
1775  EC_MASTER_DBG(master, 1, "EoE thread exiting...\n");
1776  return 0;
1777 }
1778 
1779 #endif
1780 
1781 /****************************************************************************/
1782 
1786  ec_master_t *master
1787  )
1788 {
1789  ec_slave_config_t *sc;
1790 
1791  list_for_each_entry(sc, &master->configs, list) {
1793  }
1794 }
1795 
1796 /****************************************************************************/
1797 
1801 #define EC_FIND_SLAVE \
1802  do { \
1803  if (alias) { \
1804  for (; slave < master->slaves + master->slave_count; \
1805  slave++) { \
1806  if (slave->effective_alias == alias) \
1807  break; \
1808  } \
1809  if (slave == master->slaves + master->slave_count) \
1810  return NULL; \
1811  } \
1812  \
1813  slave += position; \
1814  if (slave < master->slaves + master->slave_count) { \
1815  return slave; \
1816  } else { \
1817  return NULL; \
1818  } \
1819  } while (0)
1820 
1826  ec_master_t *master,
1827  uint16_t alias,
1828  uint16_t position
1829  )
1830 {
1831  ec_slave_t *slave = master->slaves;
1832  EC_FIND_SLAVE;
1833 }
1834 
1842  const ec_master_t *master,
1843  uint16_t alias,
1844  uint16_t position
1845  )
1846 {
1847  const ec_slave_t *slave = master->slaves;
1848  EC_FIND_SLAVE;
1849 }
1850 
1851 /****************************************************************************/
1852 
1858  const ec_master_t *master
1859  )
1860 {
1861  const ec_slave_config_t *sc;
1862  unsigned int count = 0;
1863 
1864  list_for_each_entry(sc, &master->configs, list) {
1865  count++;
1866  }
1867 
1868  return count;
1869 }
1870 
1871 /****************************************************************************/
1872 
1876 #define EC_FIND_CONFIG \
1877  do { \
1878  list_for_each_entry(sc, &master->configs, list) { \
1879  if (pos--) \
1880  continue; \
1881  return sc; \
1882  } \
1883  return NULL; \
1884  } while (0)
1885 
1891  const ec_master_t *master,
1892  unsigned int pos
1893  )
1894 {
1895  ec_slave_config_t *sc;
1897 }
1898 
1906  const ec_master_t *master,
1907  unsigned int pos
1908  )
1909 {
1910  const ec_slave_config_t *sc;
1912 }
1913 
1914 /****************************************************************************/
1915 
1921  const ec_master_t *master
1922  )
1923 {
1924  const ec_domain_t *domain;
1925  unsigned int count = 0;
1926 
1927  list_for_each_entry(domain, &master->domains, list) {
1928  count++;
1929  }
1930 
1931  return count;
1932 }
1933 
1934 /****************************************************************************/
1935 
1939 #define EC_FIND_DOMAIN \
1940  do { \
1941  list_for_each_entry(domain, &master->domains, list) { \
1942  if (index--) \
1943  continue; \
1944  return domain; \
1945  } \
1946  \
1947  return NULL; \
1948  } while (0)
1949 
1955  ec_master_t *master,
1956  unsigned int index
1957  )
1958 {
1959  ec_domain_t *domain;
1961 }
1962 
1970  const ec_master_t *master,
1971  unsigned int index
1972  )
1973 {
1974  const ec_domain_t *domain;
1976 }
1977 
1978 /****************************************************************************/
1979 
1980 #ifdef EC_EOE
1981 
1987  const ec_master_t *master
1988  )
1989 {
1990  const ec_eoe_t *eoe;
1991  unsigned int count = 0;
1992 
1993  list_for_each_entry(eoe, &master->eoe_handlers, list) {
1994  count++;
1995  }
1996 
1997  return count;
1998 }
1999 
2000 /****************************************************************************/
2001 
2009  const ec_master_t *master,
2010  uint16_t index
2011  )
2012 {
2013  const ec_eoe_t *eoe;
2014 
2015  list_for_each_entry(eoe, &master->eoe_handlers, list) {
2016  if (index--)
2017  continue;
2018  return eoe;
2019  }
2020 
2021  return NULL;
2022 }
2023 
2024 #endif
2025 
2026 /****************************************************************************/
2027 
2034  ec_master_t *master,
2035  unsigned int level
2036  )
2037 {
2038  if (level > 2) {
2039  EC_MASTER_ERR(master, "Invalid debug level %u!\n", level);
2040  return -EINVAL;
2041  }
2042 
2043  if (level != master->debug_level) {
2044  master->debug_level = level;
2045  EC_MASTER_INFO(master, "Master debug level set to %u.\n",
2046  master->debug_level);
2047  }
2048 
2049  return 0;
2050 }
2051 
2052 /****************************************************************************/
2053 
2057  ec_master_t *master
2058  )
2059 {
2060  ec_slave_t *slave, *ref = NULL;
2061 
2062  if (master->dc_ref_config) {
2063  // Use application-selected reference clock
2064  slave = master->dc_ref_config->slave;
2065 
2066  if (slave) {
2067  if (slave->base_dc_supported && slave->has_dc_system_time) {
2068  ref = slave;
2069  }
2070  else {
2071  EC_MASTER_WARN(master, "Slave %u can not act as a"
2072  " DC reference clock!", slave->ring_position);
2073  }
2074  }
2075  else {
2076  EC_MASTER_WARN(master, "DC reference clock config (%u-%u)"
2077  " has no slave attached!\n", master->dc_ref_config->alias,
2078  master->dc_ref_config->position);
2079  }
2080  }
2081  else {
2082  // Use first slave with DC support as reference clock
2083  for (slave = master->slaves;
2084  slave < master->slaves + master->slave_count;
2085  slave++) {
2086  if (slave->base_dc_supported && slave->has_dc_system_time) {
2087  ref = slave;
2088  break;
2089  }
2090  }
2091 
2092  }
2093 
2094  master->dc_ref_clock = ref;
2095 
2096  if (ref) {
2097  EC_MASTER_INFO(master, "Using slave %u as DC reference clock.\n",
2098  ref->ring_position);
2099  }
2100  else {
2101  EC_MASTER_INFO(master, "No DC reference clock found.\n");
2102  }
2103 
2104  // These calls always succeed, because the
2105  // datagrams have been pre-allocated.
2107  ref ? ref->station_address : 0xffff, 0x0910, 4);
2109  ref ? ref->station_address : 0xffff, 0x0910, 4);
2110 }
2111 
2112 /****************************************************************************/
2113 
2119  ec_master_t *master,
2120  ec_slave_t *port0_slave,
2121  unsigned int *slave_position
2122  )
2123 {
2124  ec_slave_t *slave = master->slaves + *slave_position;
2125  unsigned int port_index;
2126  int ret;
2127 
2128  static const unsigned int next_table[EC_MAX_PORTS] = {
2129  3, 2, 0, 1
2130  };
2131 
2132  slave->ports[0].next_slave = port0_slave;
2133 
2134  port_index = 3;
2135  while (port_index != 0) {
2136  if (!slave->ports[port_index].link.loop_closed) {
2137  *slave_position = *slave_position + 1;
2138  if (*slave_position < master->slave_count) {
2139  slave->ports[port_index].next_slave =
2140  master->slaves + *slave_position;
2141  ret = ec_master_calc_topology_rec(master,
2142  slave, slave_position);
2143  if (ret) {
2144  return ret;
2145  }
2146  } else {
2147  return -1;
2148  }
2149  }
2150 
2151  port_index = next_table[port_index];
2152  }
2153 
2154  return 0;
2155 }
2156 
2157 /****************************************************************************/
2158 
2162  ec_master_t *master
2163  )
2164 {
2165  unsigned int slave_position = 0;
2166 
2167  if (master->slave_count == 0)
2168  return;
2169 
2170  if (ec_master_calc_topology_rec(master, NULL, &slave_position))
2171  EC_MASTER_ERR(master, "Failed to calculate bus topology.\n");
2172 }
2173 
2174 /****************************************************************************/
2175 
2179  ec_master_t *master
2180  )
2181 {
2182  ec_slave_t *slave;
2183 
2184  for (slave = master->slaves;
2185  slave < master->slaves + master->slave_count;
2186  slave++) {
2188  }
2189 
2190  if (master->dc_ref_clock) {
2191  uint32_t delay = 0;
2193  }
2194 }
2195 
2196 /****************************************************************************/
2197 
2201  ec_master_t *master
2202  )
2203 {
2204  // find DC reference clock
2206 
2207  // calculate bus topology
2208  ec_master_calc_topology(master);
2209 
2211 }
2212 
2213 /****************************************************************************/
2214 
2218  ec_master_t *master
2219  )
2220 {
2221  unsigned int i;
2222  ec_slave_t *slave;
2223 
2224  if (!master->active)
2225  return;
2226 
2227  EC_MASTER_DBG(master, 1, "Requesting OP...\n");
2228 
2229  // request OP for all configured slaves
2230  for (i = 0; i < master->slave_count; i++) {
2231  slave = master->slaves + i;
2232  if (slave->config) {
2234  }
2235  }
2236 
2237  // always set DC reference clock to OP
2238  if (master->dc_ref_clock) {
2240  }
2241 }
2242 
2243 /*****************************************************************************
2244  * Application interface
2245  ****************************************************************************/
2246 
2252  ec_master_t *master
2253  )
2254 {
2255  ec_domain_t *domain, *last_domain;
2256  unsigned int index;
2257 
2258  EC_MASTER_DBG(master, 1, "ecrt_master_create_domain(master = 0x%p)\n",
2259  master);
2260 
2261  if (!(domain =
2262  (ec_domain_t *) kmalloc(sizeof(ec_domain_t), GFP_KERNEL))) {
2263  EC_MASTER_ERR(master, "Error allocating domain memory!\n");
2264  return ERR_PTR(-ENOMEM);
2265  }
2266 
2267  down(&master->master_sem);
2268 
2269  if (list_empty(&master->domains)) {
2270  index = 0;
2271  } else {
2272  last_domain = list_entry(master->domains.prev, ec_domain_t, list);
2273  index = last_domain->index + 1;
2274  }
2275 
2276  ec_domain_init(domain, master, index);
2277  list_add_tail(&domain->list, &master->domains);
2278 
2279  up(&master->master_sem);
2280 
2281  EC_MASTER_DBG(master, 1, "Created domain %u.\n", domain->index);
2282 
2283  return domain;
2284 }
2285 
2286 /****************************************************************************/
2287 
2289  ec_master_t *master
2290  )
2291 {
2293  return IS_ERR(d) ? NULL : d;
2294 }
2295 
2296 /****************************************************************************/
2297 
2299 {
2300  uint32_t domain_offset;
2301  ec_domain_t *domain;
2302  int ret;
2303 #ifdef EC_EOE
2304  int eoe_was_running;
2305 #endif
2306 
2307  EC_MASTER_DBG(master, 1, "ecrt_master_activate(master = 0x%p)\n", master);
2308 
2309  if (master->active) {
2310  EC_MASTER_WARN(master, "%s: Master already active!\n", __func__);
2311  return 0;
2312  }
2313 
2314  down(&master->master_sem);
2315 
2316  // finish all domains
2317  domain_offset = 0;
2318  list_for_each_entry(domain, &master->domains, list) {
2319  ret = ec_domain_finish(domain, domain_offset);
2320  if (ret < 0) {
2321  up(&master->master_sem);
2322  EC_MASTER_ERR(master, "Failed to finish domain 0x%p!\n", domain);
2323  return ret;
2324  }
2325  domain_offset += domain->data_size;
2326  }
2327 
2328  up(&master->master_sem);
2329 
2330  // restart EoE process and master thread with new locking
2331 
2332  ec_master_thread_stop(master);
2333 #ifdef EC_EOE
2334  eoe_was_running = master->eoe_thread != NULL;
2335  ec_master_eoe_stop(master);
2336 #endif
2337 
2338  EC_MASTER_DBG(master, 1, "FSM datagram is %p.\n", &master->fsm_datagram);
2339 
2340  master->injection_seq_fsm = 0;
2341  master->injection_seq_rt = 0;
2342 
2343  master->send_cb = master->app_send_cb;
2344  master->receive_cb = master->app_receive_cb;
2345  master->cb_data = master->app_cb_data;
2346 
2347 #ifdef EC_EOE
2348  if (eoe_was_running) {
2349  ec_master_eoe_start(master);
2350  }
2351 #endif
2353  "EtherCAT-OP");
2354  if (ret < 0) {
2355  EC_MASTER_ERR(master, "Failed to start master thread!\n");
2356  return ret;
2357  }
2358 
2359  /* Allow scanning after a topology change. */
2360  master->allow_scan = 1;
2361 
2362  master->active = 1;
2363 
2364  // notify state machine, that the configuration shall now be applied
2365  master->config_changed = 1;
2366 
2367  return 0;
2368 }
2369 
2370 /****************************************************************************/
2371 
2373 {
2374  ec_slave_t *slave;
2375 #ifdef EC_EOE
2376  ec_eoe_t *eoe;
2377  int eoe_was_running;
2378 #endif
2379 
2380  EC_MASTER_DBG(master, 1, "%s(master = 0x%p)\n", __func__, master);
2381 
2382  if (!master->active) {
2383  EC_MASTER_WARN(master, "%s: Master not active.\n", __func__);
2384  return -EINVAL;
2385  }
2386 
2387  ec_master_thread_stop(master);
2388 #ifdef EC_EOE
2389  eoe_was_running = master->eoe_thread != NULL;
2390  ec_master_eoe_stop(master);
2391 #endif
2392 
2395  master->cb_data = master;
2396 
2397  ec_master_clear_config(master);
2398 
2399  for (slave = master->slaves;
2400  slave < master->slaves + master->slave_count;
2401  slave++) {
2402 
2403  // set states for all slaves
2405 
2406  // mark for reconfiguration, because the master could have no
2407  // possibility for a reconfiguration between two sequential operation
2408  // phases.
2409  slave->force_config = 1;
2410  }
2411 
2412 #ifdef EC_EOE
2413  // ... but leave EoE slaves in OP
2414  list_for_each_entry(eoe, &master->eoe_handlers, list) {
2415  if (ec_eoe_is_open(eoe))
2417  }
2418 #endif
2419 
2420  master->app_time = 0ULL;
2421  master->dc_ref_time = 0ULL;
2422 
2423 #ifdef EC_EOE
2424  if (eoe_was_running) {
2425  ec_master_eoe_start(master);
2426  }
2427 #endif
2429  "EtherCAT-IDLE")) {
2430  EC_MASTER_WARN(master, "Failed to restart master thread!\n");
2431  }
2432 
2433  /* Disallow scanning to get into the same state like after a master
2434  * request (after ec_master_enter_operation_phase() is called). */
2435  master->allow_scan = 0;
2436 
2437  master->active = 0;
2438  return 0;
2439 }
2440 
2441 /****************************************************************************/
2442 
2444 {
2445  ec_datagram_t *datagram, *n;
2446  ec_device_index_t dev_idx;
2447 
2448  if (master->injection_seq_rt != master->injection_seq_fsm) {
2449  // inject datagram produced by master FSM
2450  ec_master_queue_datagram(master, &master->fsm_datagram);
2451  master->injection_seq_rt = master->injection_seq_fsm;
2452  }
2453 
2455 
2456  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
2457  dev_idx++) {
2458  if (unlikely(!master->devices[dev_idx].link_state)) {
2459  // link is down, no datagram can be sent
2460  list_for_each_entry_safe(datagram, n,
2461  &master->datagram_queue, queue) {
2462  if (datagram->device_index == dev_idx) {
2463  datagram->state = EC_DATAGRAM_ERROR;
2464  list_del_init(&datagram->queue);
2465  }
2466  }
2467 
2468  if (!master->devices[dev_idx].dev) {
2469  continue;
2470  }
2471 
2472  // query link state
2473  ec_device_poll(&master->devices[dev_idx]);
2474 
2475  // clear frame statistics
2476  ec_device_clear_stats(&master->devices[dev_idx]);
2477  continue;
2478  }
2479 
2480  // send frames
2481  ec_master_send_datagrams(master, dev_idx);
2482  }
2483  return 0;
2484 }
2485 
2486 /****************************************************************************/
2487 
2489 {
2490  unsigned int dev_idx;
2491  ec_datagram_t *datagram, *next;
2492 
2493  // receive datagrams
2494  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
2495  dev_idx++) {
2496  ec_device_poll(&master->devices[dev_idx]);
2497  }
2499 
2500  // dequeue all datagrams that timed out
2501  list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) {
2502  if (datagram->state != EC_DATAGRAM_SENT) continue;
2503 
2504 #ifdef EC_HAVE_CYCLES
2505  if (master->devices[EC_DEVICE_MAIN].cycles_poll -
2506  datagram->cycles_sent > timeout_cycles) {
2507 #else
2508  if (master->devices[EC_DEVICE_MAIN].jiffies_poll -
2509  datagram->jiffies_sent > timeout_jiffies) {
2510 #endif
2511  list_del_init(&datagram->queue);
2512  datagram->state = EC_DATAGRAM_TIMED_OUT;
2513  master->stats.timeouts++;
2514 
2515 #ifdef EC_RT_SYSLOG
2516  ec_master_output_stats(master);
2517 
2518  if (unlikely(master->debug_level > 0)) {
2519  unsigned int time_us;
2520 #ifdef EC_HAVE_CYCLES
2521  time_us = (unsigned int)
2522  (master->devices[EC_DEVICE_MAIN].cycles_poll -
2523  datagram->cycles_sent) * 1000 / cpu_khz;
2524 #else
2525  time_us = (unsigned int)
2526  ((master->devices[EC_DEVICE_MAIN].jiffies_poll -
2527  datagram->jiffies_sent) * 1000000 / HZ);
2528 #endif
2529  EC_MASTER_DBG(master, 0, "TIMED OUT datagram %p,"
2530  " index %02X waited %u us.\n",
2531  datagram, datagram->index, time_us);
2532  }
2533 #endif /* RT_SYSLOG */
2534  }
2535  }
2536  return 0;
2537 }
2538 
2539 /****************************************************************************/
2540 
2542 {
2543  ec_datagram_t *datagram, *next;
2544 
2545  if (down_trylock(&master->ext_queue_sem))
2546  return -EAGAIN;
2547 
2548  list_for_each_entry_safe(datagram, next, &master->ext_datagram_queue,
2549  ext_queue) {
2550  list_del_init(&datagram->ext_queue);
2551  ec_master_queue_datagram(master, datagram);
2552  }
2553  up(&master->ext_queue_sem);
2554 
2555  return ecrt_master_send(master);
2556 }
2557 
2558 /****************************************************************************/
2559 
2563  uint16_t alias, uint16_t position, uint32_t vendor_id,
2564  uint32_t product_code)
2565 {
2566  ec_slave_config_t *sc;
2567  unsigned int found = 0;
2568 
2569 
2570  EC_MASTER_DBG(master, 1, "ecrt_master_slave_config(master = 0x%p,"
2571  " alias = %u, position = %u, vendor_id = 0x%08x,"
2572  " product_code = 0x%08x)\n",
2573  master, alias, position, vendor_id, product_code);
2574 
2575  list_for_each_entry(sc, &master->configs, list) {
2576  if (sc->alias == alias && sc->position == position) {
2577  found = 1;
2578  break;
2579  }
2580  }
2581 
2582  if (found) { // config with same alias/position already existing
2583  if (sc->vendor_id != vendor_id || sc->product_code != product_code) {
2584  EC_MASTER_ERR(master, "Slave type mismatch. Slave was"
2585  " configured as 0x%08X/0x%08X before. Now configuring"
2586  " with 0x%08X/0x%08X.\n", sc->vendor_id, sc->product_code,
2587  vendor_id, product_code);
2588  return ERR_PTR(-ENOENT);
2589  }
2590  } else {
2591  EC_MASTER_DBG(master, 1, "Creating slave configuration for %u:%u,"
2592  " 0x%08X/0x%08X.\n",
2593  alias, position, vendor_id, product_code);
2594 
2595  if (!(sc = (ec_slave_config_t *) kmalloc(sizeof(ec_slave_config_t),
2596  GFP_KERNEL))) {
2597  EC_MASTER_ERR(master, "Failed to allocate memory"
2598  " for slave configuration.\n");
2599  return ERR_PTR(-ENOMEM);
2600  }
2601 
2602  ec_slave_config_init(sc, master,
2603  alias, position, vendor_id, product_code);
2604 
2605  down(&master->master_sem);
2606 
2607  // try to find the addressed slave
2610  list_add_tail(&sc->list, &master->configs);
2611 
2612  up(&master->master_sem);
2613  }
2614 
2615  return sc;
2616 }
2617 
2618 /****************************************************************************/
2619 
2621  uint16_t alias, uint16_t position, uint32_t vendor_id,
2622  uint32_t product_code)
2623 {
2624  ec_slave_config_t *sc = ecrt_master_slave_config_err(master, alias,
2625  position, vendor_id, product_code);
2626  return IS_ERR(sc) ? NULL : sc;
2627 }
2628 
2629 /****************************************************************************/
2630 
2632  ec_slave_config_t *sc)
2633 {
2634  if (sc) {
2635  ec_slave_t *slave = sc->slave;
2636 
2637  // output an early warning
2638  if (slave &&
2639  (!slave->base_dc_supported || !slave->has_dc_system_time)) {
2640  EC_MASTER_WARN(master, "Slave %u can not act as"
2641  " a reference clock!", slave->ring_position);
2642  }
2643  }
2644 
2645  master->dc_ref_config = sc;
2646  return 0;
2647 }
2648 
2649 /****************************************************************************/
2650 
2651 int ecrt_master(ec_master_t *master, ec_master_info_t *master_info)
2652 {
2653  EC_MASTER_DBG(master, 1, "ecrt_master(master = 0x%p,"
2654  " master_info = 0x%p)\n", master, master_info);
2655 
2656  master_info->slave_count = master->slave_count;
2657  master_info->link_up = master->devices[EC_DEVICE_MAIN].link_state;
2658  master_info->scan_busy = master->scan_busy;
2659  master_info->app_time = master->app_time;
2660  return 0;
2661 }
2662 
2663 /****************************************************************************/
2664 
2666  ec_master_scan_progress_t *progress)
2667 {
2668  EC_MASTER_DBG(master, 1, "ecrt_master_scan_progress(master = 0x%p,"
2669  " progress = 0x%p)\n", master, progress);
2670 
2671  progress->slave_count = master->slave_count;
2672  progress->scan_index = master->scan_index;
2673  return 0;
2674 }
2675 
2676 /****************************************************************************/
2677 
2678 int ecrt_master_get_slave(ec_master_t *master, uint16_t slave_position,
2679  ec_slave_info_t *slave_info)
2680 {
2681  const ec_slave_t *slave;
2682  unsigned int i;
2683  int ret = 0;
2684 
2685  if (down_interruptible(&master->master_sem)) {
2686  return -EINTR;
2687  }
2688 
2689  slave = ec_master_find_slave_const(master, 0, slave_position);
2690 
2691  if (slave == NULL) {
2692  ret = -ENOENT;
2693  goto out_get_slave;
2694  }
2695 
2696  slave_info->position = slave->ring_position;
2697  slave_info->vendor_id = slave->sii.vendor_id;
2698  slave_info->product_code = slave->sii.product_code;
2699  slave_info->revision_number = slave->sii.revision_number;
2700  slave_info->serial_number = slave->sii.serial_number;
2701  slave_info->alias = slave->effective_alias;
2702  slave_info->current_on_ebus = slave->sii.current_on_ebus;
2703 
2704  for (i = 0; i < EC_MAX_PORTS; i++) {
2705  slave_info->ports[i].desc = slave->ports[i].desc;
2706  slave_info->ports[i].link.link_up = slave->ports[i].link.link_up;
2707  slave_info->ports[i].link.loop_closed =
2708  slave->ports[i].link.loop_closed;
2709  slave_info->ports[i].link.signal_detected =
2710  slave->ports[i].link.signal_detected;
2711  slave_info->ports[i].receive_time = slave->ports[i].receive_time;
2712  if (slave->ports[i].next_slave) {
2713  slave_info->ports[i].next_slave =
2714  slave->ports[i].next_slave->ring_position;
2715  } else {
2716  slave_info->ports[i].next_slave = 0xffff;
2717  }
2718  slave_info->ports[i].delay_to_next_dc =
2719  slave->ports[i].delay_to_next_dc;
2720  }
2721 
2722  slave_info->al_state = slave->current_state;
2723  slave_info->error_flag = slave->error_flag;
2724  slave_info->sync_count = slave->sii.sync_count;
2725  slave_info->sdo_count = ec_slave_sdo_count(slave);
2726  if (slave->sii.name) {
2727  strncpy(slave_info->name, slave->sii.name, EC_MAX_STRING_LENGTH);
2728  } else {
2729  slave_info->name[0] = 0;
2730  }
2731 
2732 out_get_slave:
2733  up(&master->master_sem);
2734 
2735  return ret;
2736 }
2737 
2738 /****************************************************************************/
2739 
2741  void (*send_cb)(void *), void (*receive_cb)(void *), void *cb_data)
2742 {
2743  EC_MASTER_DBG(master, 1, "ecrt_master_callbacks(master = 0x%p,"
2744  " send_cb = 0x%p, receive_cb = 0x%p, cb_data = 0x%p)\n",
2745  master, send_cb, receive_cb, cb_data);
2746 
2747  master->app_send_cb = send_cb;
2748  master->app_receive_cb = receive_cb;
2749  master->app_cb_data = cb_data;
2750 }
2751 
2752 /****************************************************************************/
2753 
2755 {
2756  ec_device_index_t dev_idx;
2757 
2758  state->slaves_responding = 0U;
2759  state->al_states = 0;
2760  state->link_up = 0U;
2761 
2762  for (dev_idx = EC_DEVICE_MAIN; dev_idx < ec_master_num_devices(master);
2763  dev_idx++) {
2764  /* Announce sum of responding slaves on all links. */
2765  state->slaves_responding += master->fsm.slaves_responding[dev_idx];
2766 
2767  /* Binary-or slave states of all links. */
2768  state->al_states |= master->fsm.slave_states[dev_idx];
2769 
2770  /* Signal link up if at least one device has link. */
2771  state->link_up |= master->devices[dev_idx].link_state;
2772  }
2773  return 0;
2774 }
2775 
2776 /****************************************************************************/
2777 
2778 int ecrt_master_link_state(const ec_master_t *master, unsigned int dev_idx,
2779  ec_master_link_state_t *state)
2780 {
2781  if (dev_idx >= ec_master_num_devices(master)) {
2782  return -EINVAL;
2783  }
2784 
2785  state->slaves_responding = master->fsm.slaves_responding[dev_idx];
2786  state->al_states = master->fsm.slave_states[dev_idx];
2787  state->link_up = master->devices[dev_idx].link_state;
2788 
2789  return 0;
2790 }
2791 
2792 /****************************************************************************/
2793 
2794 int ecrt_master_application_time(ec_master_t *master, uint64_t app_time)
2795 {
2796  master->app_time = app_time;
2797 
2798  if (unlikely(!master->dc_ref_time)) {
2799  master->dc_ref_time = app_time;
2800  }
2801  return 0;
2802 }
2803 
2804 /****************************************************************************/
2805 
2807  uint32_t *time)
2808 {
2809  if (!master->dc_ref_clock) {
2810  return -ENXIO;
2811  }
2812 
2813  if (master->sync_datagram.state != EC_DATAGRAM_RECEIVED) {
2814  return -EIO;
2815  }
2816 
2817  // Get returned datagram time, transmission delay removed.
2818  *time = EC_READ_U32(master->sync_datagram.data) -
2820 
2821  return 0;
2822 }
2823 
2824 /****************************************************************************/
2825 
2827 {
2828  if (master->dc_ref_clock) {
2829  EC_WRITE_U32(master->ref_sync_datagram.data, master->app_time);
2830  ec_master_queue_datagram(master, &master->ref_sync_datagram);
2831  } else {
2832  return -ENXIO;
2833  }
2834  return 0;
2835 }
2836 
2837 /****************************************************************************/
2838 
2840  ec_master_t *master,
2841  uint64_t sync_time
2842  )
2843 {
2844  if (master->dc_ref_clock) {
2845  EC_WRITE_U32(master->ref_sync_datagram.data, sync_time);
2846  ec_master_queue_datagram(master, &master->ref_sync_datagram);
2847  } else {
2848  return -ENXIO;
2849  }
2850  return 0;
2851 }
2852 
2853 /****************************************************************************/
2854 
2856 {
2857  if (master->dc_ref_clock) {
2858  ec_datagram_zero(&master->sync_datagram);
2859  ec_master_queue_datagram(master, &master->sync_datagram);
2860  } else {
2861  return -ENXIO;
2862  }
2863  return 0;
2864 }
2865 
2866 /****************************************************************************/
2867 
2869 {
2871  ec_master_queue_datagram(master, &master->sync_mon_datagram);
2872  return 0;
2873 }
2874 
2875 /****************************************************************************/
2876 
2878 {
2879  if (master->sync_mon_datagram.state == EC_DATAGRAM_RECEIVED) {
2880  return EC_READ_U32(master->sync_mon_datagram.data) & 0x7fffffff;
2881  } else {
2882  return 0xffffffff;
2883  }
2884 }
2885 
2886 /****************************************************************************/
2887 
2888 int ecrt_master_sdo_download(ec_master_t *master, uint16_t slave_position,
2889  uint16_t index, uint8_t subindex, const uint8_t *data,
2890  size_t data_size, uint32_t *abort_code)
2891 {
2892  ec_sdo_request_t request;
2893  ec_slave_t *slave;
2894  int ret;
2895 
2896  EC_MASTER_DBG(master, 1, "%s(master = 0x%p,"
2897  " slave_position = %u, index = 0x%04X, subindex = 0x%02X,"
2898  " data = 0x%p, data_size = %zu, abort_code = 0x%p)\n",
2899  __func__, master, slave_position, index, subindex,
2900  data, data_size, abort_code);
2901 
2902  ec_sdo_request_init(&request);
2903  ecrt_sdo_request_index(&request, index, subindex);
2904  ret = ec_sdo_request_alloc(&request, data_size);
2905  if (ret) {
2906  ec_sdo_request_clear(&request);
2907  return ret;
2908  }
2909 
2910  memcpy(request.data, data, data_size);
2911  request.data_size = data_size;
2912  ecrt_sdo_request_write(&request);
2913 
2914  if (down_interruptible(&master->master_sem)) {
2915  ec_sdo_request_clear(&request);
2916  return -EINTR;
2917  }
2918 
2919  if (!(slave = ec_master_find_slave(master, 0, slave_position))) {
2920  up(&master->master_sem);
2921  EC_MASTER_ERR(master, "Slave %u does not exist!\n", slave_position);
2922  ec_sdo_request_clear(&request);
2923  return -EINVAL;
2924  }
2925 
2926  EC_SLAVE_DBG(slave, 1, "Scheduling SDO download request.\n");
2927 
2928  // schedule request.
2929  list_add_tail(&request.list, &slave->sdo_requests);
2930 
2931  up(&master->master_sem);
2932 
2933  // wait for processing through FSM
2934  if (wait_event_interruptible(master->request_queue,
2935  request.state != EC_INT_REQUEST_QUEUED)) {
2936  // interrupted by signal
2937  down(&master->master_sem);
2938  if (request.state == EC_INT_REQUEST_QUEUED) {
2939  list_del(&request.list);
2940  up(&master->master_sem);
2941  ec_sdo_request_clear(&request);
2942  return -EINTR;
2943  }
2944  // request already processing: interrupt not possible.
2945  up(&master->master_sem);
2946  }
2947 
2948  // wait until master FSM has finished processing
2949  wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY);
2950 
2951  *abort_code = request.abort_code;
2952 
2953  if (request.state == EC_INT_REQUEST_SUCCESS) {
2954  ret = 0;
2955  } else if (request.errno) {
2956  ret = -request.errno;
2957  } else {
2958  ret = -EIO;
2959  }
2960 
2961  ec_sdo_request_clear(&request);
2962  return ret;
2963 }
2964 
2965 /****************************************************************************/
2966 
2968  uint16_t slave_position, uint16_t index, const uint8_t *data,
2969  size_t data_size, uint32_t *abort_code)
2970 {
2971  ec_sdo_request_t request;
2972  ec_slave_t *slave;
2973  int ret;
2974 
2975  EC_MASTER_DBG(master, 1, "%s(master = 0x%p,"
2976  " slave_position = %u, index = 0x%04X,"
2977  " data = 0x%p, data_size = %zu, abort_code = 0x%p)\n",
2978  __func__, master, slave_position, index, data, data_size,
2979  abort_code);
2980 
2981  ec_sdo_request_init(&request);
2982  ecrt_sdo_request_index(&request, index, 0);
2983  ret = ec_sdo_request_alloc(&request, data_size);
2984  if (ret) {
2985  ec_sdo_request_clear(&request);
2986  return ret;
2987  }
2988 
2989  request.complete_access = 1;
2990  memcpy(request.data, data, data_size);
2991  request.data_size = data_size;
2992  ecrt_sdo_request_write(&request);
2993 
2994  if (down_interruptible(&master->master_sem)) {
2995  ec_sdo_request_clear(&request);
2996  return -EINTR;
2997  }
2998 
2999  if (!(slave = ec_master_find_slave(master, 0, slave_position))) {
3000  up(&master->master_sem);
3001  EC_MASTER_ERR(master, "Slave %u does not exist!\n", slave_position);
3002  ec_sdo_request_clear(&request);
3003  return -EINVAL;
3004  }
3005 
3006  EC_SLAVE_DBG(slave, 1, "Scheduling SDO download request"
3007  " (complete access).\n");
3008 
3009  // schedule request.
3010  list_add_tail(&request.list, &slave->sdo_requests);
3011 
3012  up(&master->master_sem);
3013 
3014  // wait for processing through FSM
3015  if (wait_event_interruptible(master->request_queue,
3016  request.state != EC_INT_REQUEST_QUEUED)) {
3017  // interrupted by signal
3018  down(&master->master_sem);
3019  if (request.state == EC_INT_REQUEST_QUEUED) {
3020  list_del(&request.list);
3021  up(&master->master_sem);
3022  ec_sdo_request_clear(&request);
3023  return -EINTR;
3024  }
3025  // request already processing: interrupt not possible.
3026  up(&master->master_sem);
3027  }
3028 
3029  // wait until master FSM has finished processing
3030  wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY);
3031 
3032  *abort_code = request.abort_code;
3033 
3034  if (request.state == EC_INT_REQUEST_SUCCESS) {
3035  ret = 0;
3036  } else if (request.errno) {
3037  ret = -request.errno;
3038  } else {
3039  ret = -EIO;
3040  }
3041 
3042  ec_sdo_request_clear(&request);
3043  return ret;
3044 }
3045 
3046 /****************************************************************************/
3047 
3048 int ecrt_master_sdo_upload(ec_master_t *master, uint16_t slave_position,
3049  uint16_t index, uint8_t subindex, uint8_t *target,
3050  size_t target_size, size_t *result_size, uint32_t *abort_code)
3051 {
3052  ec_sdo_request_t request;
3053  ec_slave_t *slave;
3054  int ret = 0;
3055 
3056  EC_MASTER_DBG(master, 1, "%s(master = 0x%p,"
3057  " slave_position = %u, index = 0x%04X, subindex = 0x%02X,"
3058  " target = 0x%p, target_size = %zu, result_size = 0x%p,"
3059  " abort_code = 0x%p)\n",
3060  __func__, master, slave_position, index, subindex,
3061  target, target_size, result_size, abort_code);
3062 
3063  ec_sdo_request_init(&request);
3064  ecrt_sdo_request_index(&request, index, subindex);
3065  ecrt_sdo_request_read(&request);
3066 
3067  if (down_interruptible(&master->master_sem)) {
3068  ec_sdo_request_clear(&request);
3069  return -EINTR;
3070  }
3071 
3072  if (!(slave = ec_master_find_slave(master, 0, slave_position))) {
3073  up(&master->master_sem);
3074  ec_sdo_request_clear(&request);
3075  EC_MASTER_ERR(master, "Slave %u does not exist!\n", slave_position);
3076  return -EINVAL;
3077  }
3078 
3079  EC_SLAVE_DBG(slave, 1, "Scheduling SDO upload request.\n");
3080 
3081  // schedule request.
3082  list_add_tail(&request.list, &slave->sdo_requests);
3083 
3084  up(&master->master_sem);
3085 
3086  // wait for processing through FSM
3087  if (wait_event_interruptible(master->request_queue,
3088  request.state != EC_INT_REQUEST_QUEUED)) {
3089  // interrupted by signal
3090  down(&master->master_sem);
3091  if (request.state == EC_INT_REQUEST_QUEUED) {
3092  list_del(&request.list);
3093  up(&master->master_sem);
3094  ec_sdo_request_clear(&request);
3095  return -EINTR;
3096  }
3097  // request already processing: interrupt not possible.
3098  up(&master->master_sem);
3099  }
3100 
3101  // wait until master FSM has finished processing
3102  wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY);
3103 
3104  *abort_code = request.abort_code;
3105 
3106  if (request.state != EC_INT_REQUEST_SUCCESS) {
3107  *result_size = 0;
3108  if (request.errno) {
3109  ret = -request.errno;
3110  } else {
3111  ret = -EIO;
3112  }
3113  } else {
3114  if (request.data_size > target_size) {
3115  EC_SLAVE_ERR(slave, "%s(): Buffer too small.\n", __func__);
3116  ret = -ENOBUFS;
3117  }
3118  else {
3119  memcpy(target, request.data, request.data_size);
3120  *result_size = request.data_size;
3121  ret = 0;
3122  }
3123  }
3124 
3125  ec_sdo_request_clear(&request);
3126  return ret;
3127 }
3128 
3129 /****************************************************************************/
3130 
3131 int ecrt_master_write_idn(ec_master_t *master, uint16_t slave_position,
3132  uint8_t drive_no, uint16_t idn, const uint8_t *data, size_t data_size,
3133  uint16_t *error_code)
3134 {
3135  ec_soe_request_t request;
3136  ec_slave_t *slave;
3137  int ret;
3138 
3139  if (drive_no > 7) {
3140  EC_MASTER_ERR(master, "Invalid drive number!\n");
3141  return -EINVAL;
3142  }
3143 
3144  ec_soe_request_init(&request);
3145  ec_soe_request_set_drive_no(&request, drive_no);
3146  ec_soe_request_set_idn(&request, idn);
3147 
3148  ret = ec_soe_request_alloc(&request, data_size);
3149  if (ret) {
3150  ec_soe_request_clear(&request);
3151  return ret;
3152  }
3153 
3154  memcpy(request.data, data, data_size);
3155  request.data_size = data_size;
3156  ec_soe_request_write(&request);
3157 
3158  if (down_interruptible(&master->master_sem)) {
3159  ec_soe_request_clear(&request);
3160  return -EINTR;
3161  }
3162 
3163  if (!(slave = ec_master_find_slave(master, 0, slave_position))) {
3164  up(&master->master_sem);
3165  EC_MASTER_ERR(master, "Slave %u does not exist!\n",
3166  slave_position);
3167  ec_soe_request_clear(&request);
3168  return -EINVAL;
3169  }
3170 
3171  EC_SLAVE_DBG(slave, 1, "Scheduling SoE write request.\n");
3172 
3173  // schedule SoE write request.
3174  list_add_tail(&request.list, &slave->soe_requests);
3175 
3176  up(&master->master_sem);
3177 
3178  // wait for processing through FSM
3179  if (wait_event_interruptible(master->request_queue,
3180  request.state != EC_INT_REQUEST_QUEUED)) {
3181  // interrupted by signal
3182  down(&master->master_sem);
3183  if (request.state == EC_INT_REQUEST_QUEUED) {
3184  // abort request
3185  list_del(&request.list);
3186  up(&master->master_sem);
3187  ec_soe_request_clear(&request);
3188  return -EINTR;
3189  }
3190  up(&master->master_sem);
3191  }
3192 
3193  // wait until master FSM has finished processing
3194  wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY);
3195 
3196  if (error_code) {
3197  *error_code = request.error_code;
3198  }
3199  ret = request.state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
3200  ec_soe_request_clear(&request);
3201 
3202  return ret;
3203 }
3204 
3205 /****************************************************************************/
3206 
3207 int ecrt_master_read_idn(ec_master_t *master, uint16_t slave_position,
3208  uint8_t drive_no, uint16_t idn, uint8_t *target, size_t target_size,
3209  size_t *result_size, uint16_t *error_code)
3210 {
3211  ec_soe_request_t request;
3212  ec_slave_t *slave;
3213  int ret;
3214 
3215  if (drive_no > 7) {
3216  EC_MASTER_ERR(master, "Invalid drive number!\n");
3217  return -EINVAL;
3218  }
3219 
3220  ec_soe_request_init(&request);
3221  ec_soe_request_set_drive_no(&request, drive_no);
3222  ec_soe_request_set_idn(&request, idn);
3223  ec_soe_request_read(&request);
3224 
3225  if (down_interruptible(&master->master_sem)) {
3226  ec_soe_request_clear(&request);
3227  return -EINTR;
3228  }
3229 
3230  if (!(slave = ec_master_find_slave(master, 0, slave_position))) {
3231  up(&master->master_sem);
3232  ec_soe_request_clear(&request);
3233  EC_MASTER_ERR(master, "Slave %u does not exist!\n", slave_position);
3234  return -EINVAL;
3235  }
3236 
3237  EC_SLAVE_DBG(slave, 1, "Scheduling SoE read request.\n");
3238 
3239  // schedule request.
3240  list_add_tail(&request.list, &slave->soe_requests);
3241 
3242  up(&master->master_sem);
3243 
3244  // wait for processing through FSM
3245  if (wait_event_interruptible(master->request_queue,
3246  request.state != EC_INT_REQUEST_QUEUED)) {
3247  // interrupted by signal
3248  down(&master->master_sem);
3249  if (request.state == EC_INT_REQUEST_QUEUED) {
3250  list_del(&request.list);
3251  up(&master->master_sem);
3252  ec_soe_request_clear(&request);
3253  return -EINTR;
3254  }
3255  // request already processing: interrupt not possible.
3256  up(&master->master_sem);
3257  }
3258 
3259  // wait until master FSM has finished processing
3260  wait_event(master->request_queue, request.state != EC_INT_REQUEST_BUSY);
3261 
3262  if (error_code) {
3263  *error_code = request.error_code;
3264  }
3265 
3266  if (request.state != EC_INT_REQUEST_SUCCESS) {
3267  if (result_size) {
3268  *result_size = 0;
3269  }
3270  ret = -EIO;
3271  } else { // success
3272  if (request.data_size > target_size) {
3273  EC_SLAVE_ERR(slave, "%s(): Buffer too small.\n", __func__);
3274  ret = -ENOBUFS;
3275  }
3276  else { // data fits in buffer
3277  if (result_size) {
3278  *result_size = request.data_size;
3279  }
3280  memcpy(target, request.data, request.data_size);
3281  ret = 0;
3282  }
3283  }
3284 
3285  ec_soe_request_clear(&request);
3286  return ret;
3287 }
3288 
3289 /****************************************************************************/
3290 
3292 {
3293  ec_slave_config_t *sc;
3294 
3295  list_for_each_entry(sc, &master->configs, list) {
3296  if (sc->slave) {
3298  }
3299  }
3300  return 0;
3301 }
3302 
3303 /****************************************************************************/
3304 
3305 static void sc_reset_task_kicker(struct irq_work *work)
3306 {
3307  struct ec_master *master =
3308  container_of(work, struct ec_master, sc_reset_work_kicker);
3309  schedule_work(&master->sc_reset_work);
3310 }
3311 
3312 /****************************************************************************/
3313 
3314 static void sc_reset_task(struct work_struct *work)
3315 {
3316  struct ec_master *master =
3317  container_of(work, struct ec_master, sc_reset_work);
3318 
3319  down(&master->master_sem);
3320  ecrt_master_reset(master);
3321  up(&master->master_sem);
3322 }
3323 
3324 /****************************************************************************/
3325 
3328 EXPORT_SYMBOL(ecrt_master_create_domain);
3329 EXPORT_SYMBOL(ecrt_master_activate);
3330 EXPORT_SYMBOL(ecrt_master_deactivate);
3331 EXPORT_SYMBOL(ecrt_master_send);
3332 EXPORT_SYMBOL(ecrt_master_send_ext);
3333 EXPORT_SYMBOL(ecrt_master_receive);
3334 EXPORT_SYMBOL(ecrt_master_callbacks);
3335 EXPORT_SYMBOL(ecrt_master);
3336 EXPORT_SYMBOL(ecrt_master_scan_progress);
3337 EXPORT_SYMBOL(ecrt_master_get_slave);
3338 EXPORT_SYMBOL(ecrt_master_slave_config);
3339 EXPORT_SYMBOL(ecrt_master_select_reference_clock);
3340 EXPORT_SYMBOL(ecrt_master_state);
3341 EXPORT_SYMBOL(ecrt_master_link_state);
3342 EXPORT_SYMBOL(ecrt_master_application_time);
3343 EXPORT_SYMBOL(ecrt_master_sync_reference_clock);
3345 EXPORT_SYMBOL(ecrt_master_sync_slave_clocks);
3346 EXPORT_SYMBOL(ecrt_master_reference_clock_time);
3347 EXPORT_SYMBOL(ecrt_master_sync_monitor_queue);
3348 EXPORT_SYMBOL(ecrt_master_sync_monitor_process);
3349 EXPORT_SYMBOL(ecrt_master_sdo_download);
3350 EXPORT_SYMBOL(ecrt_master_sdo_download_complete);
3351 EXPORT_SYMBOL(ecrt_master_sdo_upload);
3352 EXPORT_SYMBOL(ecrt_master_write_idn);
3353 EXPORT_SYMBOL(ecrt_master_read_idn);
3354 EXPORT_SYMBOL(ecrt_master_reset);
3355 
3358 /****************************************************************************/
void ec_eoe_queue(ec_eoe_t *eoe)
Queues the datagram, if necessary.
Definition: ethernet.c:371
unsigned int injection_seq_fsm
Datagram injection sequence number for the FSM side.
Definition: master.h:215
uint32_t serial_number
Serial-Number stored on the slave.
Definition: ecrt.h:456
#define EC_IO_TIMEOUT
Datagram timeout in microseconds.
Definition: globals.h:38
ec_slave_port_desc_t desc
Physical port type.
Definition: ecrt.h:460
uint16_t error_code
SoE error code.
Definition: soe_request.h:56
unsigned int reserved
True, if the master is in use.
Definition: master.h:189
struct list_head ext_datagram_queue
Queue for non-application datagrams.
Definition: master.h:256
int ec_mac_is_zero(const uint8_t *)
Definition: module.c:266
uint16_t ring_position
Ring position.
Definition: slave.h:175
uint32_t revision_number
Revision number.
Definition: slave.h:129
unsigned long jiffies_sent
Jiffies, when the datagram was sent.
Definition: datagram.h:98
void ec_master_clear_config(ec_master_t *)
Clear the configuration applied by the application.
Definition: master.c:535
ec_datagram_t * ec_master_get_external_datagram(ec_master_t *)
Searches for a free datagram in the external datagram ring.
Definition: master.c:935
#define EC_ADDR_LEN
Size of the EtherCAT address field.
Definition: globals.h:76
uint16_t ec_slave_sdo_count(const ec_slave_t *slave)
Get the number of SDOs in the dictionary.
Definition: slave.c:716
int ecrt_master_deactivate(ec_master_t *master)
Deactivates the master.
Definition: master.c:2372
void ec_soe_request_set_idn(ec_soe_request_t *req, uint16_t idn)
Set IDN.
Definition: soe_request.c:111
int ec_rtdm_dev_init(ec_rtdm_dev_t *rtdm_dev, ec_master_t *master)
Initialize an RTDM device.
Definition: rtdm.c:59
#define EC_DATAGRAM_NAME_SIZE
Size of the datagram description string.
Definition: globals.h:104
ec_sii_t sii
Extracted SII data.
Definition: slave.h:215
struct sk_buff * tx_skb[EC_TX_RING_SIZE]
transmit skb ring
Definition: device.h:81
size_t data_size
Size of the data in data.
Definition: datagram.h:91
struct semaphore config_sem
Semaphore protecting the config_busy variable and the allow_config flag.
Definition: master.h:248
void ec_slave_config_init(ec_slave_config_t *sc, ec_master_t *master, uint16_t alias, uint16_t position, uint32_t vendor_id, uint32_t product_code)
Slave configuration constructor.
Definition: slave_config.c:72
uint8_t sync_count
Number of sync managers.
Definition: ecrt.h:470
void ec_master_calc_dc(ec_master_t *master)
Distributed-clocks calculations.
Definition: master.c:2200
int ecrt_sdo_request_write(ec_sdo_request_t *req)
Schedule an SDO write operation.
Definition: sdo_request.c:232
int ecrt_master_link_state(const ec_master_t *master, unsigned int dev_idx, ec_master_link_state_t *state)
Reads the current state of a redundant link.
Definition: master.c:2778
unsigned int fsm_exec_count
Number of entries in execution list.
Definition: master.h:273
unsigned int num_devices
Number of devices.
Definition: master.h:203
u64 last_loss
Tx/Rx difference of last statistics cycle.
Definition: master.h:159
u64 tx_count
Number of frames sent.
Definition: master.h:149
const unsigned int rate_intervals[]
List of intervals for statistics [s].
Definition: master.c:103
uint8_t error_flag
Error flag for that slave.
Definition: ecrt.h:469
void ec_master_clear(ec_master_t *master)
Destructor.
Definition: master.c:396
struct list_head sii_requests
SII write requests.
Definition: master.h:297
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:98
int ecrt_master_send(ec_master_t *master)
Sends all datagrams in the queue.
Definition: master.c:2443
size_t data_size
Size of the process data.
Definition: domain.h:53
unsigned long jiffies_poll
jiffies of last poll
Definition: device.h:89
ec_slave_t * slave
pointer to the corresponding slave
Definition: ethernet.h:77
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:987
OP (mailbox communication and input/output update)
Definition: globals.h:132
s32 tx_byte_rates[EC_RATE_COUNT]
Transmit rates in byte/s for different statistics cycle periods.
Definition: master.h:166
int ecrt_master_scan_progress(ec_master_t *master, ec_master_scan_progress_t *progress)
Obtains network scan progress information.
Definition: master.c:2665
ec_internal_request_state_t state
State of the request.
Definition: fsm_master.h:51
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:1890
unsigned int slaves_responding[EC_MAX_NUM_DEVICES]
Number of responding slaves for every device.
Definition: fsm_master.h:72
ec_slave_port_t ports[EC_MAX_PORTS]
Ports.
Definition: slave.h:179
void ec_fsm_master_reset(ec_fsm_master_t *fsm)
Reset state machine.
Definition: fsm_master.c:144
void ec_master_request_op(ec_master_t *master)
Request OP state for configured slaves.
Definition: master.c:2217
static int ec_master_eoe_thread(void *)
Does the Ethernet over EtherCAT processing.
Definition: master.c:1722
int ec_fsm_slave_is_ready(const ec_fsm_slave_t *fsm)
Returns, if the FSM is currently not busy and ready to execute.
Definition: fsm_slave.c:176
CANopen SDO request.
Definition: sdo_request.h:40
unsigned int slave_count
Number of slaves in the network.
Definition: ecrt.h:399
ec_slave_state_t current_state
Current application state.
Definition: slave.h:184
void ec_master_leave_operation_phase(ec_master_t *master)
Transition function from OPERATION to IDLE phase.
Definition: master.c:782
int ecrt_master_sdo_download_complete(ec_master_t *master, uint16_t slave_position, uint16_t index, const uint8_t *data, size_t data_size, uint32_t *abort_code)
Executes an SDO download request to write data to a slave via complete access.
Definition: master.c:2967
ec_domain_t * ecrt_master_create_domain(ec_master_t *master)
Creates a new process data domain.
Definition: master.c:2288
#define ec_master_num_devices(MASTER)
Number of Ethernet devices.
Definition: master.h:321
#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:233
EtherCAT slave structure.
ec_internal_request_state_t state
SDO request state.
Definition: sdo_request.h:55
uint32_t vendor_id
Vendor-ID stored on the slave.
Definition: ecrt.h:453
void ec_device_clear(ec_device_t *device)
Destructor.
Definition: device.c:159
struct list_head list
List item.
Definition: domain.h:48
struct list_head eoe_handlers
Ethernet over EtherCAT handlers.
Definition: master.h:283
uint32_t product_code
Slave product code.
Definition: slave_config.h:119
ec_slave_port_link_t link
Port link state.
Definition: ecrt.h:461
int ec_master_thread_start(ec_master_t *, int(*)(void *), const char *)
Starts the master thread.
Definition: master.c:582
Operation phase.
Definition: master.h:128
dev_t device_number
Device number for master cdevs.
Definition: module.c:63
ec_slave_port_link_t link
Port link status.
Definition: slave.h:112
unsigned int allow_scan
True, if slave scanning is allowed.
Definition: master.h:241
int ecrt_master_sync_reference_clock(ec_master_t *master)
Queues the DC reference clock drift compensation datagram for sending.
Definition: master.c:2826
size_t max_queue_size
Maximum size of datagram queue.
Definition: master.h:269
void ec_master_internal_receive_cb(void *cb_data)
Internal receiving callback.
Definition: master.c:564
uint16_t position
Index after alias.
Definition: slave_config.h:116
static int ec_master_operation_thread(void *)
Master kernel thread function for OPERATION phase.
Definition: master.c:1604
int ecrt_sdo_request_read(ec_sdo_request_t *req)
Schedule an SDO read operation.
Definition: sdo_request.c:220
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:1841
#define EC_FRAME_HEADER_SIZE
Size of an EtherCAT frame header.
Definition: globals.h:67
void ecrt_master_callbacks(ec_master_t *master, void(*send_cb)(void *), void(*receive_cb)(void *), void *cb_data)
Sets the locking callbacks.
Definition: master.c:2740
EtherCAT datagram.
Definition: datagram.h:79
struct list_head list
List item.
Definition: slave_config.h:112
uint32_t serial_number
Serial number.
Definition: slave.h:130
struct list_head fsm_exec_list
Slave FSM execution list.
Definition: master.h:272
Master scan progress information.
Definition: ecrt.h:414
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:1969
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:2008
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3137
unsigned int scan_index
Index of slave currently scanned.
Definition: master.h:240
uint32_t abort_code
SDO request abort code.
Definition: sdo_request.h:60
u64 dc_ref_time
Common reference timestamp for DC start times.
Definition: master.h:228
ec_slave_state_t slave_states[EC_MAX_NUM_DEVICES]
AL states of responding slaves for every device.
Definition: fsm_master.h:76
struct list_head emerg_reg_requests
Emergency register access requests.
Definition: master.h:298
char name[EC_DATAGRAM_NAME_SIZE]
Description of the datagram.
Definition: datagram.h:106
uint16_t alias
Slave alias.
Definition: slave_config.h:115
void ec_master_send_datagrams(ec_master_t *, ec_device_index_t)
Sends the datagrams in the queue for a certain device.
Definition: master.c:1002
void ec_device_update_stats(ec_device_t *device)
Update device statistics.
Definition: device.c:481
struct list_head domains
List of domains.
Definition: master.h:225
Finite state machine of an EtherCAT slave.
Definition: fsm_slave.h:52
ec_datagram_t * datagram
Previous state datagram.
Definition: fsm_slave.h:57
uint32_t delay_to_next_dc
Delay [ns] to next DC slave.
Definition: ecrt.h:466
ec_fsm_slave_t fsm
Slave state machine.
Definition: slave.h:227
#define EC_FIND_CONFIG
Common implementation for ec_master_get_config() and ec_master_get_config_const().
Definition: master.c:1876
uint16_t working_counter
Working counter.
Definition: datagram.h:93
uint8_t * data
Pointer to SDO data.
Definition: sdo_request.h:44
unsigned long jiffies
Jiffies of last statistic cycle.
Definition: master.h:172
int16_t current_on_ebus
Power consumption in mA.
Definition: slave.h:154
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:1825
uint8_t link_state
device link state
Definition: device.h:80
static unsigned int debug_level
Debug level parameter.
Definition: module.c:55
u64 last_rx_count
Number of frames received of last statistics cycle.
Definition: master.h:152
const uint8_t * macs[EC_MAX_NUM_DEVICES]
Device MAC addresses.
Definition: master.h:201
Sent (still in the queue).
Definition: datagram.h:69
unsigned int slaves_responding
Sum of responding slaves on all Ethernet devices.
Definition: ecrt.h:329
size_t data_size
Size of SDO data.
Definition: soe_request.h:47
wait_queue_head_t request_queue
Wait queue for external requests from user space.
Definition: master.h:301
void ec_master_clear_device_stats(ec_master_t *)
Clears the common device statistics.
Definition: master.c:1307
unsigned int run_on_cpu
bind kernel threads to this cpu
Definition: master.h:276
uint16_t station_address
Configured station address.
Definition: slave.h:176
int ec_soe_request_write(ec_soe_request_t *req)
Request a write operation.
Definition: soe_request.c:241
unsigned int sync_count
Number of sync managers.
Definition: slave.h:158
struct list_head list
List head.
Definition: fsm_master.h:46
SII write request.
Definition: fsm_master.h:45
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:157
void ec_slave_clear(ec_slave_t *slave)
Slave destructor.
Definition: slave.c:169
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:2251
unsigned int link_up
true, if the network link is up.
Definition: ecrt.h:400
void ec_datagram_output_stats(ec_datagram_t *datagram)
Outputs datagram statistics at most every second.
Definition: datagram.c:614
int ecrt_master_write_idn(ec_master_t *master, uint16_t slave_position, uint8_t drive_no, uint16_t idn, const uint8_t *data, size_t data_size, uint16_t *error_code)
Executes an SoE write request.
Definition: master.c:3131
int ec_master_enter_idle_phase(ec_master_t *master)
Transition function from ORPHANED to IDLE phase.
Definition: master.c:643
ec_datagram_type_t type
Datagram type (APRD, BWR, etc.).
Definition: datagram.h:86
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:1905
Global definitions and macros.
uint32_t revision_number
Revision-Number stored on the slave.
Definition: ecrt.h:455
Logical Write.
Definition: datagram.h:54
EtherCAT master structure.
void * cb_data
Current callback data.
Definition: master.h:290
void ec_device_send(ec_device_t *device, size_t size)
Sends the content of the transmit socket buffer.
Definition: device.c:320
int ecrt_master_reference_clock_time(const ec_master_t *master, uint32_t *time)
Get the lower 32 bit of the reference clock system time.
Definition: master.c:2806
void ec_fsm_master_init(ec_fsm_master_t *fsm, ec_master_t *master, ec_datagram_t *datagram)
Constructor.
Definition: fsm_master.c:82
Initial state of a new datagram.
Definition: datagram.h:67
#define EC_MASTER_DBG(master, level, fmt, args...)
Convenience macro for printing master-specific debug messages to syslog.
Definition: master.h:100
ec_slave_t * fsm_slave
Slave that is queried next for FSM exec.
Definition: master.h:271
unsigned int send_interval
Interval between two calls to ecrt_master_send().
Definition: master.h:267
ec_slave_t * slave
EtherCAT slave.
Definition: fsm_master.h:47
EtherCAT slave.
Definition: slave.h:168
struct semaphore master_sem
Master semaphore.
Definition: master.h:198
uint8_t datagram_index
Current datagram index.
Definition: master.h:254
void ec_master_attach_slave_configs(ec_master_t *master)
Attaches the slave configurations to the slaves.
Definition: master.c:1785
struct list_head datagram_queue
Datagram queue.
Definition: master.h:253
int ecrt_master_sync_reference_clock_to(ec_master_t *master, uint64_t sync_time)
Queues the DC reference clock drift compensation datagram for sending.
Definition: master.c:2839
ec_slave_t * slave
slave the FSM runs on
Definition: fsm_slave.h:53
char name[EC_MAX_STRING_LENGTH]
Name of the slave.
Definition: ecrt.h:472
void ec_sdo_request_clear(ec_sdo_request_t *req)
SDO request destructor.
Definition: sdo_request.c:70
struct irq_work sc_reset_work_kicker
NMI-Safe kicker to trigger reset task above.
Definition: master.h:304
struct task_struct * eoe_thread
EoE thread.
Definition: master.h:282
struct list_head sdo_requests
SDO access requests.
Definition: slave.h:221
Master state.
Definition: ecrt.h:328
int ecrt_sdo_request_index(ec_sdo_request_t *req, uint16_t index, uint8_t subindex)
Set the SDO index and subindex.
Definition: sdo_request.c:181
unsigned int unmatched
unmatched datagrams (received, but not queued any longer)
Definition: master.h:139
unsigned int ext_ring_idx_fsm
Index in external datagram ring for FSM side.
Definition: master.h:265
void ec_datagram_zero(ec_datagram_t *datagram)
Fills the datagram payload memory with zeros.
Definition: datagram.c:170
int ec_master_debug_level(ec_master_t *master, unsigned int level)
Set the debug level.
Definition: master.c:2033
s32 tx_frame_rates[EC_RATE_COUNT]
Transmit rates in frames/s for different statistics cycle periods.
Definition: master.h:160
uint64_t app_time
Application time.
Definition: ecrt.h:403
s32 rx_byte_rates[EC_RATE_COUNT]
Receive rates in byte/s for different statistics cycle periods.
Definition: master.h:168
Ethernet over EtherCAT (EoE)
struct list_head soe_requests
SoE requests.
Definition: slave.h:224
#define EC_DATAGRAM_HEADER_SIZE
Size of an EtherCAT datagram header.
Definition: globals.h:70
ec_datagram_state_t state
State.
Definition: datagram.h:94
ec_device_stats_t device_stats
Device statistics.
Definition: master.h:208
ec_datagram_t fsm_datagram
Datagram used for state machines.
Definition: master.h:211
ec_slave_config_t * config
Current configuration.
Definition: slave.h:182
ec_master_phase_t phase
Master phase.
Definition: master.h:212
#define EC_WRITE_U32(DATA, VAL)
Write a 32-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3171
ec_slave_t * slaves
Array of slaves on the bus.
Definition: master.h:220
void ec_domain_clear(ec_domain_t *domain)
Domain destructor.
Definition: domain.c:87
void ec_soe_request_set_drive_no(ec_soe_request_t *req, uint8_t drive_no)
Set drive number.
Definition: soe_request.c:99
void ec_slave_calc_port_delays(ec_slave_t *slave)
Calculates the port transmission delays.
Definition: slave.c:932
int ec_domain_finish(ec_domain_t *domain, uint32_t base_address)
Finishes a domain.
Definition: domain.c:225
static unsigned long ext_injection_timeout_jiffies
Timeout for external datagram injection [jiffies].
Definition: master.c:97
struct semaphore device_sem
Device semaphore.
Definition: master.h:207
int ec_eoe_is_idle(const ec_eoe_t *eoe)
Returns the idle state.
Definition: ethernet.c:397
EtherCAT device.
Definition: device.h:73
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:1954
int ecrt_master_application_time(ec_master_t *master, uint64_t app_time)
Sets the application time.
Definition: master.c:2794
unsigned int tx_ring_index
last ring entry used to transmit
Definition: device.h:82
unsigned int timeouts
datagram timeouts
Definition: master.h:137
ec_sdo_request_t * sdo_request
SDO request to process.
Definition: fsm_master.h:82
unsigned int debug_level
Master debug level.
Definition: master.h:275
int ec_datagram_frmw(ec_datagram_t *datagram, uint16_t configured_address, uint16_t mem_address, size_t data_size)
Initializes an EtherCAT FRMW datagram.
Definition: datagram.c:340
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
Definition: slave.h:68
unsigned int ec_master_domain_count(const ec_master_t *master)
Get the number of domains.
Definition: master.c:1920
Orphaned phase.
Definition: master.h:124
struct list_head ext_queue
External datagram queue item, protected by ext_queue_sem.
Definition: datagram.h:82
s32 loss_rates[EC_RATE_COUNT]
Frame loss rates for different statistics cycle periods.
Definition: master.h:170
unsigned int corrupted
corrupted frames
Definition: master.h:138
struct list_head list
List item.
Definition: soe_request.h:41
u64 last_tx_count
Number of frames sent of last statistics cycle.
Definition: master.h:150
uint32_t transmission_delay
DC system time transmission delay (offset from reference clock).
Definition: slave.h:207
void ec_master_exec_slave_fsms(ec_master_t *)
Execute slave FSMs.
Definition: master.c:1441
void ec_soe_request_clear(ec_soe_request_t *req)
SoE request destructor.
Definition: soe_request.c:71
unsigned int ext_ring_idx_rt
Index in external datagram ring for RT side.
Definition: master.h:263
struct rt_mutex io_mutex
Mutex used in IDLE and OP phase.
Definition: master.h:286
unsigned int slave_count
Number of slaves on the bus.
Definition: master.h:221
unsigned int scan_busy
Current scan state.
Definition: master.h:239
ec_device_index_t
Master devices.
Definition: globals.h:198
void(* receive_cb)(void *)
Current receive datagrams callback.
Definition: master.h:289
s32 rx_frame_rates[EC_RATE_COUNT]
Receive rates in frames/s for different statistics cycle periods.
Definition: master.h:163
int ecrt_master_read_idn(ec_master_t *master, uint16_t slave_position, uint8_t drive_no, uint16_t idn, uint8_t *target, size_t target_size, size_t *result_size, uint16_t *error_code)
Executes an SoE read request.
Definition: master.c:3207
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3154
unsigned int index
Index (just a number).
Definition: domain.h:50
void ec_slave_calc_transmission_delays_rec(ec_slave_t *slave, uint32_t *delay)
Recursively calculates transmission delays.
Definition: slave.c:978
void ec_master_leave_idle_phase(ec_master_t *master)
Transition function from IDLE to ORPHANED phase.
Definition: master.c:676
Main device.
Definition: globals.h:199
unsigned int skip_count
Number of requeues when not yet received.
Definition: datagram.h:104
ec_slave_config_t * ecrt_master_slave_config(ec_master_t *master, uint16_t alias, uint16_t position, uint32_t vendor_id, uint32_t product_code)
Obtains a slave configuration.
Definition: master.c:2620
#define EC_READ_U32(DATA)
Read a 32-bit unsigned value from EtherCAT data.
Definition: ecrt.h:3061
int ec_device_init(ec_device_t *device, ec_master_t *master)
Constructor.
Definition: device.c:60
ec_slave_port_desc_t desc
Port descriptors.
Definition: slave.h:111
#define EC_MASTER_WARN(master, fmt, args...)
Convenience macro for printing master-specific warnings to syslog.
Definition: master.h:86
int ec_fsm_slave_exec(ec_fsm_slave_t *fsm, ec_datagram_t *datagram)
Executes the current state of the state machine.
Definition: fsm_slave.c:135
unsigned int active
Master has been activated.
Definition: master.h:213
struct list_head sent
Master list item for sent datagrams.
Definition: datagram.h:83
int errno
Error number.
Definition: sdo_request.h:59
int ec_datagram_brd(ec_datagram_t *datagram, uint16_t mem_address, size_t data_size)
Initializes an EtherCAT BRD datagram.
Definition: datagram.c:365
int ec_datagram_fpwr(ec_datagram_t *datagram, uint16_t configured_address, uint16_t mem_address, size_t data_size)
Initializes an EtherCAT FPWR datagram.
Definition: datagram.c:290
int ec_master_enter_operation_phase(ec_master_t *master)
Transition function from IDLE to OPERATION phase.
Definition: master.c:700
uint8_t has_dc_system_time
The slave supports the DC system time register.
Definition: slave.h:204
wait_queue_head_t scan_queue
Queue for processes that wait for slave scanning.
Definition: master.h:244
ec_datagram_t sync_datagram
Datagram used for DC drift compensation.
Definition: master.h:231
int ecrt_master_send_ext(ec_master_t *master)
Sends non-application datagrams.
Definition: master.c:2541
#define EC_MASTER_ERR(master, fmt, args...)
Convenience macro for printing master-specific errors to syslog.
Definition: master.h:74
int ecrt_master_select_reference_clock(ec_master_t *master, ec_slave_config_t *sc)
Selects the reference clock for distributed clocks.
Definition: master.c:2631
struct device * class_device
Master class device.
Definition: master.h:192
EtherCAT datagram structure.
void ec_master_queue_datagram(ec_master_t *master, ec_datagram_t *datagram)
Places a datagram in the datagram queue.
Definition: master.c:954
int ec_slave_config_attach(ec_slave_config_t *sc)
Attaches the configuration to the addressed slave object.
Definition: slave_config.c:254
Broadcast Write.
Definition: datagram.h:51
int ecrt_master_sync_monitor_queue(ec_master_t *master)
Queues the DC synchrony monitoring datagram for sending.
Definition: master.c:2868
static int ec_master_idle_thread(void *)
Master kernel thread function for IDLE phase.
Definition: master.c:1531
struct list_head configs
List of slave configurations.
Definition: master.h:224
ec_slave_t * slave
Slave pointer.
Definition: slave_config.h:126
ec_slave_config_t * dc_ref_config
Application-selected DC reference clock slave config.
Definition: master.h:235
ec_device_index_t device_index
Device via which the datagram shall be / was sent.
Definition: datagram.h:84
int ec_soe_request_alloc(ec_soe_request_t *req, size_t size)
Pre-allocates the data memory.
Definition: soe_request.c:144
int ecrt_master(ec_master_t *master, ec_master_info_t *master_info)
Obtains master information.
Definition: master.c:2651
int ec_fsm_master_idle(const ec_fsm_master_t *fsm)
Definition: fsm_master.c:192
void ec_master_clear_slaves(ec_master_t *master)
Clear all slaves.
Definition: master.c:478
Slave information.
Definition: ecrt.h:451
struct list_head list
list item
Definition: ethernet.h:76
Device statistics.
Definition: master.h:148
uint8_t * ec_device_tx_data(ec_device_t *device)
Returns a pointer to the device&#39;s transmit memory.
Definition: device.c:301
u64 last_rx_bytes
Number of bytes received of last statistics cycle.
Definition: master.h:157
unsigned long output_jiffies
time of last output
Definition: master.h:141
ec_stats_t stats
Cyclic statistics.
Definition: master.h:277
void ec_print_data(const uint8_t *, size_t)
Outputs frame contents for debugging purposes.
Definition: module.c:344
Idle phase.
Definition: master.h:126
void ec_master_update_device_stats(ec_master_t *)
Updates the common device statistics.
Definition: master.c:1339
struct semaphore scan_sem
Semaphore protecting the scan_busy variable and the allow_scan flag.
Definition: master.h:242
int ecrt_master_get_slave(ec_master_t *master, uint16_t slave_position, ec_slave_info_t *slave_info)
Obtains slave information.
Definition: master.c:2678
int ec_datagram_prealloc(ec_datagram_t *datagram, size_t size)
Allocates internal payload memory.
Definition: datagram.c:142
static unsigned int run_on_cpu
Bind created kernel threads to a cpu.
Definition: module.c:56
uint16_t effective_alias
Effective alias address.
Definition: slave.h:177
void ec_master_calc_transmission_delays(ec_master_t *)
Calculates the bus transmission delays.
Definition: master.c:2178
void ec_master_clear_eoe_handlers(ec_master_t *master)
Clear and free all EoE handlers.
Definition: master.c:442
uint8_t al_state
Current state of the slave.
Definition: ecrt.h:468
size_t data_size
Size of SDO data.
Definition: sdo_request.h:46
uint8_t scan_busy
true, while the master is scanning the network.
Definition: ecrt.h:401
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
Definition: ecrt.h:3045
void ec_master_eoe_start(ec_master_t *master)
Starts Ethernet over EtherCAT processing on demand.
Definition: master.c:1673
u64 tx_bytes
Number of bytes sent.
Definition: master.h:154
void(* app_send_cb)(void *)
Application&#39;s send datagrams callback.
Definition: master.h:291
void * app_cb_data
Application callback data.
Definition: master.h:295
uint16_t ec_master_eoe_handler_count(const ec_master_t *master)
Get the number of EoE handlers.
Definition: master.c:1986
int16_t current_on_ebus
Used current in mA.
Definition: ecrt.h:458
void ec_master_clear_slave_configs(ec_master_t *)
Clear all slave configurations.
Definition: master.c:460
int ec_soe_request_read(ec_soe_request_t *req)
Request a read operation.
Definition: soe_request.c:226
void ec_master_clear_domains(ec_master_t *)
Clear all domains.
Definition: master.c:520
void ec_master_find_dc_ref_clock(ec_master_t *)
Finds the DC reference clock.
Definition: master.c:2056
struct list_head list
Used for execution list.
Definition: fsm_slave.h:54
void ec_master_thread_stop(ec_master_t *)
Stops the master thread.
Definition: master.c:611
void ec_sdo_request_init(ec_sdo_request_t *req)
SDO request constructor.
Definition: sdo_request.c:48
#define EC_MAX_PORTS
Maximum number of slave ports.
Definition: ecrt.h:276
#define EC_SDO_INJECTION_TIMEOUT
SDO injection timeout in microseconds.
Definition: master.c:77
int ecrt_master_sdo_download(ec_master_t *master, uint16_t slave_position, uint16_t index, uint8_t subindex, const uint8_t *data, size_t data_size, uint32_t *abort_code)
Executes an SDO download request to write data to a slave.
Definition: master.c:2888
struct ec_slave_info_t::@7 ports[EC_MAX_PORTS]
Port information.
ec_datagram_t ext_datagram_ring[EC_EXT_RING_SIZE]
External datagram ring.
Definition: master.h:261
uint16_t sdo_count
Number of SDOs.
Definition: ecrt.h:471
struct list_head list
List item.
Definition: sdo_request.h:41
#define EC_MAX_STRING_LENGTH
Maximum string length.
Definition: ecrt.h:273
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
Definition: datagram.c:80
static unsigned long timeout_jiffies
Frame timeout in jiffies.
Definition: master.c:93
void ec_rtdm_dev_clear(ec_rtdm_dev_t *rtdm_dev)
Clear an RTDM device.
Definition: rtdm.c:108
void ec_cdev_clear(ec_cdev_t *cdev)
Destructor.
Definition: cdev.c:126
ec_slave_t * next_slave
Connected slaves.
Definition: slave.h:113
Queued for sending.
Definition: datagram.h:68
unsigned int link_up
true, if at least one Ethernet link is up.
Definition: ecrt.h:340
uint32_t vendor_id
Slave vendor ID.
Definition: slave_config.h:118
uint32_t receive_time
Port receive times for delay measurement.
Definition: slave.h:114
Timed out (dequeued).
Definition: datagram.h:71
wait_queue_head_t config_queue
Queue for processes that wait for slave configuration.
Definition: master.h:250
#define EC_EXT_RING_SIZE
Size of the external datagram ring.
Definition: master.h:113
void ec_master_internal_send_cb(void *cb_data)
Internal sending callback.
Definition: master.c:549
int ec_master_calc_topology_rec(ec_master_t *, ec_slave_t *, unsigned int *)
Calculates the bus topology; recursion function.
Definition: master.c:2118
unsigned int scan_index
Index of the slave that is currently scanned.
Definition: ecrt.h:416
uint16_t next_slave
Ring position of next DC slave on that port.
Definition: ecrt.h:464
void ec_master_calc_topology(ec_master_t *)
Calculates the bus topology.
Definition: master.c:2161
u64 app_time
Time of the last ecrt_master_sync() call.
Definition: master.h:227
void ec_slave_request_state(ec_slave_t *slave, ec_slave_state_t state)
Request a slave state and resets the error flag.
Definition: slave.c:306
ec_datagram_t ref_sync_datagram
Datagram used for synchronizing the reference clock to the master clock.
Definition: master.h:229
void ec_master_output_stats(ec_master_t *master)
Output master statistics.
Definition: master.c:1277
uint8_t base_dc_supported
Distributed clocks are supported.
Definition: slave.h:202
#define EC_FIND_DOMAIN
Common implementation for ec_master_find_domain() and ec_master_find_domain_const().
Definition: master.c:1939
u64 rx_count
Number of frames received.
Definition: master.h:151
unsigned int al_states
Application-layer states of all slaves.
Definition: ecrt.h:331
int ecrt_master_activate(ec_master_t *master)
Finishes the configuration phase and prepares for cyclic operation.
Definition: master.c:2298
struct work_struct sc_reset_work
Task to reset slave configuration.
Definition: master.h:303
uint8_t * data
Datagram payload.
Definition: datagram.h:88
#define EC_FIND_SLAVE
Common implementation for ec_master_find_slave() and ec_master_find_slave_const().
Definition: master.c:1801
struct semaphore ext_queue_sem
Semaphore protecting the ext_datagram_queue.
Definition: master.h:258
#define EC_BYTE_TRANSMISSION_TIME_NS
Time to send a byte in nanoseconds.
Definition: globals.h:44
uint16_t alias
The slaves alias if not equal to 0.
Definition: ecrt.h:457
void ec_eoe_run(ec_eoe_t *eoe)
Runs the EoE state machine.
Definition: ethernet.c:343
#define EC_READ_U8(DATA)
Read an 8-bit unsigned value from EtherCAT data.
Definition: ecrt.h:3029
EtherCAT slave configuration.
Definition: slave_config.h:111
struct list_head queue
Master datagram queue item, protected by user-supplied mutex.
Definition: datagram.h:80
uint32_t product_code
Product-Code stored on the slave.
Definition: ecrt.h:454
int ecrt_master_receive(ec_master_t *master)
Fetches received frames from the hardware and processes the datagrams.
Definition: master.c:2488
EtherCAT device structure.
void(* app_receive_cb)(void *)
Application&#39;s receive datagrams callback.
Definition: master.h:293
unsigned int slave_count
Number of slaves detected.
Definition: ecrt.h:415
struct net_device * dev
pointer to the assigned net_device
Definition: device.h:76
int ec_fsm_master_exec(ec_fsm_master_t *fsm)
Executes the current state of the state machine.
Definition: fsm_master.c:173
uint32_t ecrt_master_sync_monitor_process(const ec_master_t *master)
Processes the DC synchrony monitoring datagram.
Definition: master.c:2877
void ec_soe_request_init(ec_soe_request_t *req)
SoE request constructor.
Definition: soe_request.c:48
EtherCAT slave configuration structure.
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:2562
void ec_device_poll(ec_device_t *device)
Calls the poll function of the assigned net_device.
Definition: device.c:463
void ec_eoe_clear(ec_eoe_t *eoe)
EoE destructor.
Definition: ethernet.c:223
Master information.
Definition: ecrt.h:398
unsigned int index
Index.
Definition: master.h:188
unsigned int ec_master_config_count(const ec_master_t *master)
Get the number of slave configurations provided by the application.
Definition: master.c:1857
Error while sending/receiving (dequeued).
Definition: datagram.h:72
Auto Increment Physical Write.
Definition: datagram.h:45
uint8_t address[EC_ADDR_LEN]
Recipient address.
Definition: datagram.h:87
u64 last_tx_bytes
Number of bytes sent of last statistics cycle.
Definition: master.h:155
int ec_sdo_request_alloc(ec_sdo_request_t *req, size_t size)
Pre-allocates the data memory.
Definition: sdo_request.c:121
uint32_t product_code
Vendor-specific product code.
Definition: slave.h:128
void ec_domain_init(ec_domain_t *domain, ec_master_t *master, unsigned int index)
Domain constructor.
Definition: domain.c:57
PREOP state (mailbox communication, no IO)
Definition: globals.h:126
void ec_slave_config_clear(ec_slave_config_t *sc)
Slave configuration destructor.
Definition: slave_config.c:126
Backup device.
Definition: globals.h:200
Received (dequeued).
Definition: datagram.h:70
Ethernet over EtherCAT (EoE) handler.
Definition: ethernet.h:74
ec_fsm_master_t fsm
Master state machine.
Definition: master.h:210
ec_cdev_t cdev
Master character device.
Definition: master.h:191
u64 rx_bytes
Number of bytes received.
Definition: master.h:156
#define EC_DATAGRAM_FOOTER_SIZE
Size of an EtherCAT datagram footer.
Definition: globals.h:73
#define EC_MASTER_INFO(master, fmt, args...)
Convenience macro for printing master-specific information to syslog.
Definition: master.h:62
unsigned int error_flag
Stop processing after an error.
Definition: slave.h:185
uint16_t position
Offset of the slave in the ring.
Definition: ecrt.h:452
uint32_t receive_time
Receive time on DC transmission delay measurement.
Definition: ecrt.h:462
unsigned int config_changed
The configuration changed.
Definition: master.h:214
EtherCAT master.
Definition: master.h:187
unsigned int injection_seq_rt
Datagram injection sequence number for the realtime side.
Definition: master.h:217
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:1139
Configured Address Physical Write.
Definition: datagram.h:48
#define FORCE_OUTPUT_CORRUPTED
Always output corrupted frames.
Definition: master.c:74
uint8_t index
Index (set by master).
Definition: datagram.h:92
ec_device_t devices[EC_MAX_NUM_DEVICES]
EtherCAT devices.
Definition: master.h:200
void ec_slave_config_load_default_sync_config(ec_slave_config_t *sc)
Loads the default PDO assignment from the slave object.
Definition: slave_config.c:337
ec_internal_request_state_t state
Request state.
Definition: soe_request.h:52
unsigned int config_busy
State of slave configuration.
Definition: master.h:247
void ec_master_inject_external_datagrams(ec_master_t *)
Injects external datagrams that fit into the datagram queue.
Definition: master.c:804
int ecrt_master_sync_slave_clocks(ec_master_t *master)
Queues the DC clock drift compensation datagram for sending.
Definition: master.c:2855
void ec_fsm_master_clear(ec_fsm_master_t *fsm)
Destructor.
Definition: fsm_master.c:123
int ecrt_master_state(const ec_master_t *master, ec_master_state_t *state)
Reads the current master state.
Definition: master.c:2754
int ec_eoe_is_open(const ec_eoe_t *eoe)
Returns the state of the device.
Definition: ethernet.c:385
void ec_device_clear_stats(ec_device_t *device)
Clears the frame statistics.
Definition: device.c:359
Sercos-over-EtherCAT request.
Definition: soe_request.h:40
char * name
Slave name.
Definition: slave.h:150
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:918
unsigned long jiffies_received
Jiffies, when the datagram was received.
Definition: datagram.h:102
int ecrt_master_reset(ec_master_t *master)
Retry configuring slaves.
Definition: master.c:3291
uint8_t * data
Pointer to SDO data.
Definition: soe_request.h:45
EtherCAT domain.
Definition: domain.h:46
void ec_master_eoe_stop(ec_master_t *master)
Stops the Ethernet over EtherCAT processing.
Definition: master.c:1707
void(* send_cb)(void *)
Current send datagrams callback.
Definition: master.h:288
int ecrt_master_sdo_upload(ec_master_t *master, uint16_t slave_position, uint16_t index, uint8_t subindex, uint8_t *target, size_t target_size, size_t *result_size, uint32_t *abort_code)
Executes an SDO upload request to read data from a slave.
Definition: master.c:3048
uint32_t vendor_id
Vendor ID.
Definition: slave.h:127
uint8_t complete_access
SDO shall be transferred completely.
Definition: sdo_request.h:47
uint32_t delay_to_next_dc
Delay to next slave with DC support behind this port [ns].
Definition: slave.h:116
struct task_struct * thread
Master thread.
Definition: master.h:279
unsigned int queue_datagram
the datagram is ready for queuing
Definition: ethernet.h:79
ec_slave_t * dc_ref_clock
DC reference clock slave.
Definition: master.h:237
int ec_cdev_init(ec_cdev_t *cdev, ec_master_t *master, dev_t dev_num)
Constructor.
Definition: cdev.c:100
unsigned int force_config
Force (re-)configuration.
Definition: slave.h:186
#define EC_MAX_DATA_SIZE
Resulting maximum data size of a single datagram in a frame.
Definition: globals.h:79
void ec_master_init_static(void)
Static variables initializer.
Definition: master.c:136
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
Definition: datagram.c:110