μHAL (v2.7.9)
Part of the IPbus software repository
ProtocolPCIe.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
31  email: tom.williams <AT> cern.ch
32 
33 ---------------------------------------------------------------------------
34 */
35 
42 #ifndef _uhal_ProtocolPCIe_hpp_
43 #define _uhal_ProtocolPCIe_hpp_
44 
45 
46 #include <deque> // for deque
47 #include <stddef.h> // for size_t
48 #include <stdint.h> // for uint32_t, uint8_t
49 #include <string> // for string
50 #include <utility> // for pair
51 #include <vector> // for vector
52 
53 #include <boost/chrono/system_clocks.hpp> // for steady_clock
54 #include <boost/function.hpp> // for function
55 #include <boost/interprocess/managed_shared_memory.hpp>
56 #include <boost/noncopyable.hpp>
57 #include <boost/scoped_ptr.hpp>
58 #include <boost/thread/locks.hpp>
59 
60 #include "uhal/ClientInterface.hpp"
61 #include "uhal/log/exception.hpp"
62 #include "uhal/ProtocolIPbus.hpp"
63 
64 
65 namespace boost
66 {
67  template <class Y> class shared_ptr;
68 }
69 
70 namespace uhal
71 {
72  class Buffers;
73  struct URI;
74 
75  namespace exception
76  {
78  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( PCIeTimeout , ClientTimeout , "Exception class to handle the case in which the PCIe connection timed out." )
80  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( PCIeInitialisationError , TransportLayerError , "Exception class to handle a failure to read from the specified device files during initialisation." )
82  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( PCIeCommunicationError , TransportLayerError , "Exception class to handle a low-level seek/read/write error after initialisation." )
84  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( MutexError , TransportLayerError , "Exception class to handle errors from pthread mutex-related functions." )
85  }
86 
88  class PCIe : public IPbus< 2 , 0 >
89  {
90  public:
91  class PacketFmt {
92  public:
93  PacketFmt(const uint8_t* const, const size_t);
94  PacketFmt(const std::vector< std::pair<const uint8_t*, size_t> >& aData);
95  ~PacketFmt();
96 
97  const std::vector< std::pair<const uint8_t*, size_t> > mData;
98  };
99 
100  class File {
101  public:
102  File(const std::string& aPath, int aFlags);
103  ~File();
104 
105  const std::string& getPath() const;
106  void setPath(const std::string& aPath);
107 
108  void open();
109  void close();
110 
111  void createBuffer(const size_t aNrBytes);
112 
113  void read(const uint32_t aAddr, const uint32_t aNrWords, std::vector<uint32_t>& aValues);
114 
115  void write(const uint32_t aAddr, const std::vector<uint32_t>& aValues);
116 
117  void write(const uint32_t aAddr, const uint8_t* const aPtr, const size_t aNrBytes);
118 
119  void write(const uint32_t aAddr, const std::vector<std::pair<const uint8_t*, size_t> >& aData);
120 
121  bool haveLock() const;
122 
123  void lock();
124 
125  void unlock();
126 
127  private:
128  std::string mPath;
129  int mFd;
130  int mFlags;
131  bool mLocked;
132  size_t mBufferSize;
133  char* mBuffer;
134  };
135 
136  private:
137 
138  class RobustMutex {
139  public:
140  RobustMutex();
141  ~RobustMutex();
142 
143  void lock();
144 
145  void unlock();
146 
147  uint64_t getCounter() const;
148 
149  bool isActive() const;
150 
151  void startSession();
152 
153  void endSession();
154 
155  private:
157 
158  pthread_mutex_t mMutex;
159  uint64_t mCount;
161  };
162 
163  template <class T>
164  class SharedObject : public boost::noncopyable {
165  public:
166  SharedObject(const std::string& aName);
167  ~SharedObject();
168 
169  T* operator->();
170 
171  T& operator*();
172 
173  private:
174  std::string mName;
175  boost::interprocess::managed_shared_memory mSharedMem;
176  T* mObj;
177  };
178 
179  static std::string getSharedMemName(const std::string& );
180 
182  typedef boost::unique_lock<IPCMutex_t> IPCScopedLock_t;
183 
184  public:
190  PCIe ( const std::string& aId, const URI& aUri );
191 
193  virtual ~PCIe();
194 
195  private:
196 
197  PCIe ( const PCIe& aPCIe );
198 
199  PCIe& operator= ( const PCIe& aPCIe );
200 
206  void implementDispatch ( boost::shared_ptr< Buffers > aBuffers );
207 
209  virtual void Flush( );
210 
212  virtual void dispatchExceptionHandler();
213 
215 
216  typedef boost::chrono::steady_clock SteadyClock_t;
217 
222  uint32_t getMaxSendSize();
223 
228  uint32_t getMaxReplySize();
229 
231  void connect();
232 
234  void connect(IPCScopedLock_t& );
235 
237  void disconnect();
238 
240  void write(const boost::shared_ptr<Buffers>& aBuffers);
241 
243  void read();
244 
246 
253 
257 
259 
261 
262  boost::chrono::microseconds mSleepDuration;
263 
264  uint32_t mNumberOfPages, mMaxInFlight, mPageSize, mMaxPacketSize, mIndexNextPage, mPublishedReplyPageCount, mReadReplyPageCount;
265 
267  std::deque < boost::shared_ptr< Buffers > > mReplyQueue;
268  };
269 
270  std::ostream& operator<<(std::ostream& aStream, const PCIe::PacketFmt& aPacket);
271 
272 }
273 
274 
275 #endif
uhal::PCIe::IPCScopedLock_t
boost::unique_lock< IPCMutex_t > IPCScopedLock_t
Definition: ProtocolPCIe.hpp:182
uhal::PCIe::mIPCMutex
SharedObject< IPCMutex_t > mIPCMutex
Definition: ProtocolPCIe.hpp:254
uhal::PCIe::mIPCExternalSessionActive
bool mIPCExternalSessionActive
Definition: ProtocolPCIe.hpp:255
uhal::operator<<
std::ostream & operator<<(std::ostream &aStr, const uhal::HttpResponseType &aHttpResponse)
Definition: HttpResponseGrammar.cpp:41
uhal::PCIe::mUseInterrupt
bool mUseInterrupt
Definition: ProtocolPCIe.hpp:260
boost::shared_ptr
Definition: DerivedNodeFactory.hpp:52
uhal::PCIe::mSleepDuration
boost::chrono::microseconds mSleepDuration
Definition: ProtocolPCIe.hpp:262
uhal::PCIe::File::mBuffer
char * mBuffer
Definition: ProtocolPCIe.hpp:133
uhal::PCIe::RobustMutex::mMutex
pthread_mutex_t mMutex
Definition: ProtocolPCIe.hpp:158
uhal::PCIe::PacketFmt::mData
const std::vector< std::pair< const uint8_t *, size_t > > mData
Definition: ProtocolPCIe.hpp:97
uhal::PCIe::IPCMutex_t
RobustMutex IPCMutex_t
Definition: ProtocolPCIe.hpp:181
uhal::PCIe::File::mFlags
int mFlags
Definition: ProtocolPCIe.hpp:130
uhal::PCIe::mDeviceFileHostToFPGA
File mDeviceFileHostToFPGA
Host-to-FPGA device file.
Definition: ProtocolPCIe.hpp:248
uhal::PCIe::RobustMutex::RobustMutex
RobustMutex(const RobustMutex &)
uhal::PCIe::File::mPath
std::string mPath
Definition: ProtocolPCIe.hpp:128
uhal::PCIe::File::mFd
int mFd
Definition: ProtocolPCIe.hpp:129
uhal::IPbus
A class which provides the version-specific functionality for IPbus.
Definition: ProtocolIPbus.hpp:69
uhal::PCIe::InnerProtocol
IPbus< 2, 0 > InnerProtocol
Definition: ProtocolPCIe.hpp:214
UHAL_DEFINE_DERIVED_EXCEPTION_CLASS
#define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, BaseClassName, ClassDescription)
Macro for simplifying the declaration and definition of derived exception types.
Definition: exception.hpp:49
boost
Definition: log.hpp:13
uhal::PCIe::SharedObject
Definition: ProtocolPCIe.hpp:164
uhal::PCIe::mConnected
bool mConnected
Definition: ProtocolPCIe.hpp:245
uhal::PCIe::PacketFmt
Definition: ProtocolPCIe.hpp:91
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::PCIe::RobustMutex
Definition: ProtocolPCIe.hpp:138
uhal::PCIe::PCIe
PCIe(const PCIe &aPCIe)
uhal::PCIe::mXdma7seriesWorkaround
bool mXdma7seriesWorkaround
Definition: ProtocolPCIe.hpp:258
uhal::PCIe::SharedObject::mSharedMem
boost::interprocess::managed_shared_memory mSharedMem
Definition: ProtocolPCIe.hpp:175
uhal::PCIe::mDeviceFileFPGAEvent
File mDeviceFileFPGAEvent
FPGA-to-host interrupt (event) file.
Definition: ProtocolPCIe.hpp:252
uhal::PCIe::SteadyClock_t
boost::chrono::steady_clock SteadyClock_t
Definition: ProtocolPCIe.hpp:216
uhal::PCIe::mDeviceFileFPGAToHost
File mDeviceFileFPGAToHost
FPGA-to-host device file.
Definition: ProtocolPCIe.hpp:250
ProtocolIPbus.hpp
uhal::PCIe::File
Definition: ProtocolPCIe.hpp:100
uhal::PCIe::mReplyQueue
std::deque< boost::shared_ptr< Buffers > > mReplyQueue
The list of buffers still awaiting a reply.
Definition: ProtocolPCIe.hpp:267
uhal::PCIe::SharedObject::mName
std::string mName
Definition: ProtocolPCIe.hpp:174
uhal::PCIe::RobustMutex::mCount
uint64_t mCount
Definition: ProtocolPCIe.hpp:159
uhal::PCIe::SharedObject::mObj
T * mObj
Definition: ProtocolPCIe.hpp:176
uhal::PCIe::File::mLocked
bool mLocked
Definition: ProtocolPCIe.hpp:131
uhal::PCIe::mReadReplyPageCount
uint32_t mReadReplyPageCount
Definition: ProtocolPCIe.hpp:264
uhal::PCIe::RobustMutex::mSessionActive
bool mSessionActive
Definition: ProtocolPCIe.hpp:160
uhal::tests::write
c write(addr, xx[0])
uhal::URI
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50
exception.hpp
uhal::PCIe
Transport protocol to transfer an IPbus buffer via PCIe.
Definition: ProtocolPCIe.hpp:89
ClientInterface.hpp
uhal::PCIe::File::mBufferSize
size_t mBufferSize
Definition: ProtocolPCIe.hpp:132
uhal::PCIe::mIPCSessionCount
uint64_t mIPCSessionCount
Definition: ProtocolPCIe.hpp:256