Hi Emilio,
Thanks for the detailed comments, which are very helpful to me,
> Is there a reason why you prefer to use ioctl for register access instead of mmap and letting userspace access directly?
This is my first task to write device driver on Linux rather than vxWorks and the task timeline is tight, so I prefer to choose ioctl which has many tutorials and examples online in order to save my time. I found that EPICS devlib2 module uses userspace I/O that you mentioned to access hardware directly and I am also interested in this direct way, probably I will start to learn it in future.
> I know security is usually not a priority, but I've noticed you
don't check/validate the register offset passed in the ioctl syscall,
which means it could give access to other resources (memory or I/O)
I agree, it should be checked that whether the offset exceeds the memory range that Linux OS assigned to the corresponding BAR of the PCI device.
> You might want to provide a fallback path when the target system
doesn't have your kernel module installed, in which case, you can get
register access by mmap'ing
/sys/bus/pci/devices/<X>/resource<Y>, where <X> is the
domain:bus:devices.function of
your device and <Y> is the BAR number you want to access
It is a good idea to have fallback mechanism, I will figure out how to implement such mechanism as you suggested.
> I noticed you dereference a volatile pointer to access the
registers, in kernel space I'd recommend using the functions
ioread<X> or iowrite<X> instead, where <X> is the
access size (8, 16 or 32). This will make code more portable.
Actually, I was always confused about the difference between ioread8/16/32, readb/w/l and dereferencing a pointer, I will spend some time and try to understand them better.
Thanks Again and Best Regards,
Lin
-----Original Messages-----
From:"Perez Juarez, Emilio (DLSLtd,RAL,LSCI)" <emilio.perez-juarez at diamond.ac.uk>
Sent Time:2022-12-04 18:39:37 (Sunday)
To: "tech-talk at aps.anl.gov" <tech-talk at aps.anl.gov>, "Wang, Lin" <wanglin at ihep.ac.cn>
Cc:
Subject: [SPAM] Re: A simple EPICS driver example for PCI devices using asynPortDriver on Linux
Hi Lin Wang and thanks for sharing,
I have some comments/questions:
-
Is there a reason why you prefer to use ioctl for register access instead of mmap and letting userspace access directly?
-
I know security is usually not a priority, but I've noticed you don't check/validate the register offset passed in the ioctl syscall, which means it could give access to other resources (memory or I/O)
-
You might want to provide a fallback path when the target system doesn't have your kernel module installed, in which case, you can get register access by mmap'ing /sys/bus/pci/devices/<X>/resource<Y>, where <X> is the domain:bus:devices.function of
your device and <Y> is the BAR number you want to access
-
I noticed you dereference a volatile pointer to access the registers, in kernel space I'd recommend using the functions ioread<X> or iowrite<X> instead, where <X> is the access size (8, 16 or 32). This will make code more portable.
I hope any of that was useful,
Kind Regards,
Emilio
From: Tech-talk <tech-talk-bounces at aps.anl.gov> on behalf of Wang, Lin via Tech-talk <tech-talk at aps.anl.gov>
Sent: 02 December 2022 10:15
To: tech-talk at aps.anl.gov <tech-talk at aps.anl.gov>
Subject: A simple EPICS driver example for PCI devices using asynPortDriver on Linux
Dear all,
A simple EPICS driver example for PCI devices using asynPortDriver on Linux is shared in the following GitHub link in case somebody might be interested,
https://github.com/wanglin86769/cpciEpicsApp
This is an EPICS IOC developed for the LLRF system of a small-scale accelerator facility at CSNS campus. The LLRF board is a customized cPCI FPGA board with PCI 9056 bridge, the control interface is merely register access plus waveform read with 1 second
update period, no interrupt or DMA is needed.
The Linux driver part is implemented as kernel module rather than UIO (Userspace I/O). This example is only useful for simple customized PCI devices that does not need interrupt and DMA, and only one MMIO (Memory mapped I/O) BAR is used to access the FPGA
registers.
This is my first assignment to control PCI under Linux rather than vxWorks, any suggestion is appreciated.
Regards,
Lin Wang
Staff Engineer
Controls Group, Accelerator System Division, CSNS (China Spallation Neutron Source)
IHEP (Institute of High Energy Physics), Chinese Academy of Science
--
This e-mail and any attachments may contain confidential, copyright and or privileged material, and are for the use of the intended addressee only. If you are not the intended addressee or an authorised recipient of the addressee please notify us of receipt by returning the e-mail and do not use, copy, retain, distribute or disclose the information in or attached to the e-mail.
Any opinions expressed within this e-mail are those of the individual and not necessarily of Diamond Light Source Ltd.
Diamond Light Source Ltd. cannot guarantee that this e-mail or any attachments are free from viruses and we cannot accept liability for any damage which you may sustain as a result of software viruses which may be transmitted in or with the message.
Diamond Light Source Limited (company no. 4375679). Registered in England and Wales with its registered office at Diamond House, Harwell Science and Innovation Campus, Didcot, Oxfordshire, OX11 0DE, United Kingdom
- References:
- A simple EPICS driver example for PCI devices using asynPortDriver on Linux Wang, Lin via Tech-talk
- Re: A simple EPICS driver example for PCI devices using asynPortDriver on Linux Perez Juarez, Emilio (DLSLtd, RAL, LSCI) via Tech-talk
- Navigate by Date:
- Prev:
Re: A simple EPICS driver example for PCI devices using asynPortDriver on Linux Perez Juarez, Emilio (DLSLtd, RAL, LSCI) via Tech-talk
- Next:
motor control css problem whitetiger1123 via Tech-talk
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
<2022>
2023
2024
- Navigate by Thread:
- Prev:
Re: A simple EPICS driver example for PCI devices using asynPortDriver on Linux Perez Juarez, Emilio (DLSLtd, RAL, LSCI) via Tech-talk
- Next:
Re: A simple EPICS driver example for PCI devices using asynPortDriver on Linux Michael Davidsaver via Tech-talk
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
<2022>
2023
2024
|