falcon_io.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2008 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #ifndef EFX_FALCON_IO_H
  11. #define EFX_FALCON_IO_H
  12. #include <linux/io.h>
  13. #include <linux/spinlock.h>
  14. /**************************************************************************
  15. *
  16. * Falcon hardware access
  17. *
  18. **************************************************************************
  19. *
  20. * Notes on locking strategy:
  21. *
  22. * Most Falcon registers require 16-byte (or 8-byte, for SRAM
  23. * registers) atomic writes which necessitates locking.
  24. * Under normal operation few writes to the Falcon BAR are made and these
  25. * registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and TX_DESC_UPD_REG) are special
  26. * cased to allow 4-byte (hence lockless) accesses.
  27. *
  28. * It *is* safe to write to these 4-byte registers in the middle of an
  29. * access to an 8-byte or 16-byte register. We therefore use a
  30. * spinlock to protect accesses to the larger registers, but no locks
  31. * for the 4-byte registers.
  32. *
  33. * A write barrier is needed to ensure that DW3 is written after DW0/1/2
  34. * due to the way the 16byte registers are "collected" in the Falcon BIU
  35. *
  36. * We also lock when carrying out reads, to ensure consistency of the
  37. * data (made possible since the BIU reads all 128 bits into a cache).
  38. * Reads are very rare, so this isn't a significant performance
  39. * impact. (Most data transferred from NIC to host is DMAed directly
  40. * into host memory).
  41. *
  42. * I/O BAR access uses locks for both reads and writes (but is only provided
  43. * for testing purposes).
  44. */
  45. /* Special buffer descriptors (Falcon SRAM) */
  46. #define BUF_TBL_KER_A1 0x18000
  47. #define BUF_TBL_KER_B0 0x800000
  48. #if BITS_PER_LONG == 64
  49. #define FALCON_USE_QWORD_IO 1
  50. #endif
  51. #ifdef FALCON_USE_QWORD_IO
  52. static inline void _falcon_writeq(struct efx_nic *efx, __le64 value,
  53. unsigned int reg)
  54. {
  55. __raw_writeq((__force u64)value, efx->membase + reg);
  56. }
  57. static inline __le64 _falcon_readq(struct efx_nic *efx, unsigned int reg)
  58. {
  59. return (__force __le64)__raw_readq(efx->membase + reg);
  60. }
  61. #endif
  62. static inline void _falcon_writel(struct efx_nic *efx, __le32 value,
  63. unsigned int reg)
  64. {
  65. __raw_writel((__force u32)value, efx->membase + reg);
  66. }
  67. static inline __le32 _falcon_readl(struct efx_nic *efx, unsigned int reg)
  68. {
  69. return (__force __le32)__raw_readl(efx->membase + reg);
  70. }
  71. /* Writes to a normal 16-byte Falcon register, locking as appropriate. */
  72. static inline void falcon_write(struct efx_nic *efx, efx_oword_t *value,
  73. unsigned int reg)
  74. {
  75. unsigned long flags;
  76. EFX_REGDUMP(efx, "writing register %x with " EFX_OWORD_FMT "\n", reg,
  77. EFX_OWORD_VAL(*value));
  78. spin_lock_irqsave(&efx->biu_lock, flags);
  79. #ifdef FALCON_USE_QWORD_IO
  80. _falcon_writeq(efx, value->u64[0], reg + 0);
  81. wmb();
  82. _falcon_writeq(efx, value->u64[1], reg + 8);
  83. #else
  84. _falcon_writel(efx, value->u32[0], reg + 0);
  85. _falcon_writel(efx, value->u32[1], reg + 4);
  86. _falcon_writel(efx, value->u32[2], reg + 8);
  87. wmb();
  88. _falcon_writel(efx, value->u32[3], reg + 12);
  89. #endif
  90. mmiowb();
  91. spin_unlock_irqrestore(&efx->biu_lock, flags);
  92. }
  93. /* Writes to an 8-byte Falcon SRAM register, locking as appropriate. */
  94. static inline void falcon_write_sram(struct efx_nic *efx, efx_qword_t *value,
  95. unsigned int index)
  96. {
  97. unsigned int reg = efx->type->buf_tbl_base + (index * sizeof(*value));
  98. unsigned long flags;
  99. EFX_REGDUMP(efx, "writing SRAM register %x with " EFX_QWORD_FMT "\n",
  100. reg, EFX_QWORD_VAL(*value));
  101. spin_lock_irqsave(&efx->biu_lock, flags);
  102. #ifdef FALCON_USE_QWORD_IO
  103. _falcon_writeq(efx, value->u64[0], reg + 0);
  104. #else
  105. _falcon_writel(efx, value->u32[0], reg + 0);
  106. wmb();
  107. _falcon_writel(efx, value->u32[1], reg + 4);
  108. #endif
  109. mmiowb();
  110. spin_unlock_irqrestore(&efx->biu_lock, flags);
  111. }
  112. /* Write dword to Falcon register that allows partial writes
  113. *
  114. * Some Falcon registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and
  115. * TX_DESC_UPD_REG) can be written to as a single dword. This allows
  116. * for lockless writes.
  117. */
  118. static inline void falcon_writel(struct efx_nic *efx, efx_dword_t *value,
  119. unsigned int reg)
  120. {
  121. EFX_REGDUMP(efx, "writing partial register %x with "EFX_DWORD_FMT"\n",
  122. reg, EFX_DWORD_VAL(*value));
  123. /* No lock required */
  124. _falcon_writel(efx, value->u32[0], reg);
  125. }
  126. /* Read from a Falcon register
  127. *
  128. * This reads an entire 16-byte Falcon register in one go, locking as
  129. * appropriate. It is essential to read the first dword first, as this
  130. * prompts Falcon to load the current value into the shadow register.
  131. */
  132. static inline void falcon_read(struct efx_nic *efx, efx_oword_t *value,
  133. unsigned int reg)
  134. {
  135. unsigned long flags;
  136. spin_lock_irqsave(&efx->biu_lock, flags);
  137. value->u32[0] = _falcon_readl(efx, reg + 0);
  138. rmb();
  139. value->u32[1] = _falcon_readl(efx, reg + 4);
  140. value->u32[2] = _falcon_readl(efx, reg + 8);
  141. value->u32[3] = _falcon_readl(efx, reg + 12);
  142. spin_unlock_irqrestore(&efx->biu_lock, flags);
  143. EFX_REGDUMP(efx, "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  144. EFX_OWORD_VAL(*value));
  145. }
  146. /* This reads an 8-byte Falcon SRAM entry in one go. */
  147. static inline void falcon_read_sram(struct efx_nic *efx, efx_qword_t *value,
  148. unsigned int index)
  149. {
  150. unsigned int reg = efx->type->buf_tbl_base + (index * sizeof(*value));
  151. unsigned long flags;
  152. spin_lock_irqsave(&efx->biu_lock, flags);
  153. #ifdef FALCON_USE_QWORD_IO
  154. value->u64[0] = _falcon_readq(efx, reg + 0);
  155. #else
  156. value->u32[0] = _falcon_readl(efx, reg + 0);
  157. rmb();
  158. value->u32[1] = _falcon_readl(efx, reg + 4);
  159. #endif
  160. spin_unlock_irqrestore(&efx->biu_lock, flags);
  161. EFX_REGDUMP(efx, "read from SRAM register %x, got "EFX_QWORD_FMT"\n",
  162. reg, EFX_QWORD_VAL(*value));
  163. }
  164. /* Read dword from Falcon register that allows partial writes (sic) */
  165. static inline void falcon_readl(struct efx_nic *efx, efx_dword_t *value,
  166. unsigned int reg)
  167. {
  168. value->u32[0] = _falcon_readl(efx, reg);
  169. EFX_REGDUMP(efx, "read from register %x, got "EFX_DWORD_FMT"\n",
  170. reg, EFX_DWORD_VAL(*value));
  171. }
  172. /* Write to a register forming part of a table */
  173. static inline void falcon_write_table(struct efx_nic *efx, efx_oword_t *value,
  174. unsigned int reg, unsigned int index)
  175. {
  176. falcon_write(efx, value, reg + index * sizeof(efx_oword_t));
  177. }
  178. /* Read to a register forming part of a table */
  179. static inline void falcon_read_table(struct efx_nic *efx, efx_oword_t *value,
  180. unsigned int reg, unsigned int index)
  181. {
  182. falcon_read(efx, value, reg + index * sizeof(efx_oword_t));
  183. }
  184. /* Write to a dword register forming part of a table */
  185. static inline void falcon_writel_table(struct efx_nic *efx, efx_dword_t *value,
  186. unsigned int reg, unsigned int index)
  187. {
  188. falcon_writel(efx, value, reg + index * sizeof(efx_oword_t));
  189. }
  190. /* Page-mapped register block size */
  191. #define FALCON_PAGE_BLOCK_SIZE 0x2000
  192. /* Calculate offset to page-mapped register block */
  193. #define FALCON_PAGED_REG(page, reg) \
  194. ((page) * FALCON_PAGE_BLOCK_SIZE + (reg))
  195. /* As for falcon_write(), but for a page-mapped register. */
  196. static inline void falcon_write_page(struct efx_nic *efx, efx_oword_t *value,
  197. unsigned int reg, unsigned int page)
  198. {
  199. falcon_write(efx, value, FALCON_PAGED_REG(page, reg));
  200. }
  201. /* As for falcon_writel(), but for a page-mapped register. */
  202. static inline void falcon_writel_page(struct efx_nic *efx, efx_dword_t *value,
  203. unsigned int reg, unsigned int page)
  204. {
  205. falcon_writel(efx, value, FALCON_PAGED_REG(page, reg));
  206. }
  207. /* Write dword to Falcon page-mapped register with an extra lock.
  208. *
  209. * As for falcon_writel_page(), but for a register that suffers from
  210. * SFC bug 3181. If writing to page 0, take out a lock so the BIU
  211. * collector cannot be confused.
  212. */
  213. static inline void falcon_writel_page_locked(struct efx_nic *efx,
  214. efx_dword_t *value,
  215. unsigned int reg,
  216. unsigned int page)
  217. {
  218. unsigned long flags = 0;
  219. if (page == 0)
  220. spin_lock_irqsave(&efx->biu_lock, flags);
  221. falcon_writel(efx, value, FALCON_PAGED_REG(page, reg));
  222. if (page == 0)
  223. spin_unlock_irqrestore(&efx->biu_lock, flags);
  224. }
  225. #endif /* EFX_FALCON_IO_H */