io.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2009 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_IO_H
  11. #define EFX_IO_H
  12. #include <linux/io.h>
  13. #include <linux/spinlock.h>
  14. /**************************************************************************
  15. *
  16. * NIC register I/O
  17. *
  18. **************************************************************************
  19. *
  20. * Notes on locking strategy:
  21. *
  22. * Most NIC registers require 16-byte (or 8-byte, for SRAM) atomic writes
  23. * which necessitates locking.
  24. * Under normal operation few writes to NIC registers 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 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. #if BITS_PER_LONG == 64
  46. #define EFX_USE_QWORD_IO 1
  47. #endif
  48. #ifdef EFX_USE_QWORD_IO
  49. static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
  50. unsigned int reg)
  51. {
  52. __raw_writeq((__force u64)value, efx->membase + reg);
  53. }
  54. static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
  55. {
  56. return (__force __le64)__raw_readq(efx->membase + reg);
  57. }
  58. #endif
  59. static inline void _efx_writed(struct efx_nic *efx, __le32 value,
  60. unsigned int reg)
  61. {
  62. __raw_writel((__force u32)value, efx->membase + reg);
  63. }
  64. static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
  65. {
  66. return (__force __le32)__raw_readl(efx->membase + reg);
  67. }
  68. /* Writes to a normal 16-byte Efx register, locking as appropriate. */
  69. static inline void efx_writeo(struct efx_nic *efx, efx_oword_t *value,
  70. unsigned int reg)
  71. {
  72. unsigned long flags __attribute__ ((unused));
  73. EFX_REGDUMP(efx, "writing register %x with " EFX_OWORD_FMT "\n", reg,
  74. EFX_OWORD_VAL(*value));
  75. spin_lock_irqsave(&efx->biu_lock, flags);
  76. #ifdef EFX_USE_QWORD_IO
  77. _efx_writeq(efx, value->u64[0], reg + 0);
  78. wmb();
  79. _efx_writeq(efx, value->u64[1], reg + 8);
  80. #else
  81. _efx_writed(efx, value->u32[0], reg + 0);
  82. _efx_writed(efx, value->u32[1], reg + 4);
  83. _efx_writed(efx, value->u32[2], reg + 8);
  84. wmb();
  85. _efx_writed(efx, value->u32[3], reg + 12);
  86. #endif
  87. mmiowb();
  88. spin_unlock_irqrestore(&efx->biu_lock, flags);
  89. }
  90. /* Write an 8-byte NIC SRAM entry through the supplied mapping,
  91. * locking as appropriate. */
  92. static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
  93. efx_qword_t *value, unsigned int index)
  94. {
  95. unsigned int addr = index * sizeof(*value);
  96. unsigned long flags __attribute__ ((unused));
  97. EFX_REGDUMP(efx, "writing SRAM address %x with " EFX_QWORD_FMT "\n",
  98. addr, EFX_QWORD_VAL(*value));
  99. spin_lock_irqsave(&efx->biu_lock, flags);
  100. #ifdef EFX_USE_QWORD_IO
  101. __raw_writeq((__force u64)value->u64[0], membase + addr);
  102. #else
  103. __raw_writel((__force u32)value->u32[0], membase + addr);
  104. wmb();
  105. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  106. #endif
  107. mmiowb();
  108. spin_unlock_irqrestore(&efx->biu_lock, flags);
  109. }
  110. /* Write dword to NIC register that allows partial writes
  111. *
  112. * Some registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and
  113. * TX_DESC_UPD_REG) can be written to as a single dword. This allows
  114. * for lockless writes.
  115. */
  116. static inline void efx_writed(struct efx_nic *efx, efx_dword_t *value,
  117. unsigned int reg)
  118. {
  119. EFX_REGDUMP(efx, "writing partial register %x with "EFX_DWORD_FMT"\n",
  120. reg, EFX_DWORD_VAL(*value));
  121. /* No lock required */
  122. _efx_writed(efx, value->u32[0], reg);
  123. }
  124. /* Read from a NIC register
  125. *
  126. * This reads an entire 16-byte register in one go, locking as
  127. * appropriate. It is essential to read the first dword first, as this
  128. * prompts the NIC to load the current value into the shadow register.
  129. */
  130. static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
  131. unsigned int reg)
  132. {
  133. unsigned long flags __attribute__ ((unused));
  134. spin_lock_irqsave(&efx->biu_lock, flags);
  135. value->u32[0] = _efx_readd(efx, reg + 0);
  136. rmb();
  137. value->u32[1] = _efx_readd(efx, reg + 4);
  138. value->u32[2] = _efx_readd(efx, reg + 8);
  139. value->u32[3] = _efx_readd(efx, reg + 12);
  140. spin_unlock_irqrestore(&efx->biu_lock, flags);
  141. EFX_REGDUMP(efx, "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  142. EFX_OWORD_VAL(*value));
  143. }
  144. /* Read an 8-byte SRAM entry through supplied mapping,
  145. * locking as appropriate. */
  146. static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
  147. efx_qword_t *value, unsigned int index)
  148. {
  149. unsigned int addr = index * sizeof(*value);
  150. unsigned long flags __attribute__ ((unused));
  151. spin_lock_irqsave(&efx->biu_lock, flags);
  152. #ifdef EFX_USE_QWORD_IO
  153. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  154. #else
  155. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  156. rmb();
  157. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  158. #endif
  159. spin_unlock_irqrestore(&efx->biu_lock, flags);
  160. EFX_REGDUMP(efx, "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
  161. addr, EFX_QWORD_VAL(*value));
  162. }
  163. /* Read dword from register that allows partial writes (sic) */
  164. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  165. unsigned int reg)
  166. {
  167. value->u32[0] = _efx_readd(efx, reg);
  168. EFX_REGDUMP(efx, "read from register %x, got "EFX_DWORD_FMT"\n",
  169. reg, EFX_DWORD_VAL(*value));
  170. }
  171. /* Write to a register forming part of a table */
  172. static inline void efx_writeo_table(struct efx_nic *efx, efx_oword_t *value,
  173. unsigned int reg, unsigned int index)
  174. {
  175. efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
  176. }
  177. /* Read to a register forming part of a table */
  178. static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
  179. unsigned int reg, unsigned int index)
  180. {
  181. efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
  182. }
  183. /* Write to a dword register forming part of a table */
  184. static inline void efx_writed_table(struct efx_nic *efx, efx_dword_t *value,
  185. unsigned int reg, unsigned int index)
  186. {
  187. efx_writed(efx, value, reg + index * sizeof(efx_oword_t));
  188. }
  189. /* Page-mapped register block size */
  190. #define EFX_PAGE_BLOCK_SIZE 0x2000
  191. /* Calculate offset to page-mapped register block */
  192. #define EFX_PAGED_REG(page, reg) \
  193. ((page) * EFX_PAGE_BLOCK_SIZE + (reg))
  194. /* As for efx_writeo(), but for a page-mapped register. */
  195. static inline void efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  196. unsigned int reg, unsigned int page)
  197. {
  198. efx_writeo(efx, value, EFX_PAGED_REG(page, reg));
  199. }
  200. /* As for efx_writed(), but for a page-mapped register. */
  201. static inline void efx_writed_page(struct efx_nic *efx, efx_dword_t *value,
  202. unsigned int reg, unsigned int page)
  203. {
  204. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  205. }
  206. /* Write dword to page-mapped register with an extra lock.
  207. *
  208. * As for efx_writed_page(), but for a register that suffers from
  209. * SFC bug 3181. Take out a lock so the BIU collector cannot be
  210. * confused. */
  211. static inline void efx_writed_page_locked(struct efx_nic *efx,
  212. efx_dword_t *value,
  213. unsigned int reg,
  214. unsigned int page)
  215. {
  216. unsigned long flags __attribute__ ((unused));
  217. if (page == 0) {
  218. spin_lock_irqsave(&efx->biu_lock, flags);
  219. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  220. spin_unlock_irqrestore(&efx->biu_lock, flags);
  221. } else {
  222. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  223. }
  224. }
  225. #endif /* EFX_IO_H */