io.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. netif_vdbg(efx, hw, efx->net_dev,
  74. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  75. EFX_OWORD_VAL(*value));
  76. spin_lock_irqsave(&efx->biu_lock, flags);
  77. #ifdef EFX_USE_QWORD_IO
  78. _efx_writeq(efx, value->u64[0], reg + 0);
  79. wmb();
  80. _efx_writeq(efx, value->u64[1], reg + 8);
  81. #else
  82. _efx_writed(efx, value->u32[0], reg + 0);
  83. _efx_writed(efx, value->u32[1], reg + 4);
  84. _efx_writed(efx, value->u32[2], reg + 8);
  85. wmb();
  86. _efx_writed(efx, value->u32[3], reg + 12);
  87. #endif
  88. mmiowb();
  89. spin_unlock_irqrestore(&efx->biu_lock, flags);
  90. }
  91. /* Write an 8-byte NIC SRAM entry through the supplied mapping,
  92. * locking as appropriate. */
  93. static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
  94. efx_qword_t *value, unsigned int index)
  95. {
  96. unsigned int addr = index * sizeof(*value);
  97. unsigned long flags __attribute__ ((unused));
  98. netif_vdbg(efx, hw, efx->net_dev,
  99. "writing SRAM address %x with " EFX_QWORD_FMT "\n",
  100. addr, EFX_QWORD_VAL(*value));
  101. spin_lock_irqsave(&efx->biu_lock, flags);
  102. #ifdef EFX_USE_QWORD_IO
  103. __raw_writeq((__force u64)value->u64[0], membase + addr);
  104. #else
  105. __raw_writel((__force u32)value->u32[0], membase + addr);
  106. wmb();
  107. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  108. #endif
  109. mmiowb();
  110. spin_unlock_irqrestore(&efx->biu_lock, flags);
  111. }
  112. /* Write dword to NIC register that allows partial writes
  113. *
  114. * Some 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 efx_writed(struct efx_nic *efx, efx_dword_t *value,
  119. unsigned int reg)
  120. {
  121. netif_vdbg(efx, hw, efx->net_dev,
  122. "writing partial register %x with "EFX_DWORD_FMT"\n",
  123. reg, EFX_DWORD_VAL(*value));
  124. /* No lock required */
  125. _efx_writed(efx, value->u32[0], reg);
  126. }
  127. /* Read from a NIC register
  128. *
  129. * This reads an entire 16-byte register in one go, locking as
  130. * appropriate. It is essential to read the first dword first, as this
  131. * prompts the NIC to load the current value into the shadow register.
  132. */
  133. static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
  134. unsigned int reg)
  135. {
  136. unsigned long flags __attribute__ ((unused));
  137. spin_lock_irqsave(&efx->biu_lock, flags);
  138. value->u32[0] = _efx_readd(efx, reg + 0);
  139. rmb();
  140. value->u32[1] = _efx_readd(efx, reg + 4);
  141. value->u32[2] = _efx_readd(efx, reg + 8);
  142. value->u32[3] = _efx_readd(efx, reg + 12);
  143. spin_unlock_irqrestore(&efx->biu_lock, flags);
  144. netif_vdbg(efx, hw, efx->net_dev,
  145. "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  146. EFX_OWORD_VAL(*value));
  147. }
  148. /* Read an 8-byte SRAM entry through supplied mapping,
  149. * locking as appropriate. */
  150. static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
  151. efx_qword_t *value, unsigned int index)
  152. {
  153. unsigned int addr = index * sizeof(*value);
  154. unsigned long flags __attribute__ ((unused));
  155. spin_lock_irqsave(&efx->biu_lock, flags);
  156. #ifdef EFX_USE_QWORD_IO
  157. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  158. #else
  159. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  160. rmb();
  161. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  162. #endif
  163. spin_unlock_irqrestore(&efx->biu_lock, flags);
  164. netif_vdbg(efx, hw, efx->net_dev,
  165. "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
  166. addr, EFX_QWORD_VAL(*value));
  167. }
  168. /* Read dword from register that allows partial writes (sic) */
  169. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  170. unsigned int reg)
  171. {
  172. value->u32[0] = _efx_readd(efx, reg);
  173. netif_vdbg(efx, hw, efx->net_dev,
  174. "read from register %x, got "EFX_DWORD_FMT"\n",
  175. reg, EFX_DWORD_VAL(*value));
  176. }
  177. /* Write to a register forming part of a table */
  178. static inline void efx_writeo_table(struct efx_nic *efx, efx_oword_t *value,
  179. unsigned int reg, unsigned int index)
  180. {
  181. efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
  182. }
  183. /* Read to a register forming part of a table */
  184. static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
  185. unsigned int reg, unsigned int index)
  186. {
  187. efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
  188. }
  189. /* Write to a dword register forming part of a table */
  190. static inline void efx_writed_table(struct efx_nic *efx, efx_dword_t *value,
  191. unsigned int reg, unsigned int index)
  192. {
  193. efx_writed(efx, value, reg + index * sizeof(efx_oword_t));
  194. }
  195. /* Read from a dword register forming part of a table */
  196. static inline void efx_readd_table(struct efx_nic *efx, efx_dword_t *value,
  197. unsigned int reg, unsigned int index)
  198. {
  199. efx_readd(efx, value, reg + index * sizeof(efx_dword_t));
  200. }
  201. /* Page-mapped register block size */
  202. #define EFX_PAGE_BLOCK_SIZE 0x2000
  203. /* Calculate offset to page-mapped register block */
  204. #define EFX_PAGED_REG(page, reg) \
  205. ((page) * EFX_PAGE_BLOCK_SIZE + (reg))
  206. /* As for efx_writeo(), but for a page-mapped register. */
  207. static inline void efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  208. unsigned int reg, unsigned int page)
  209. {
  210. efx_writeo(efx, value, EFX_PAGED_REG(page, reg));
  211. }
  212. /* As for efx_writed(), but for a page-mapped register. */
  213. static inline void efx_writed_page(struct efx_nic *efx, efx_dword_t *value,
  214. unsigned int reg, unsigned int page)
  215. {
  216. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  217. }
  218. /* Write dword to page-mapped register with an extra lock.
  219. *
  220. * As for efx_writed_page(), but for a register that suffers from
  221. * SFC bug 3181. Take out a lock so the BIU collector cannot be
  222. * confused. */
  223. static inline void efx_writed_page_locked(struct efx_nic *efx,
  224. efx_dword_t *value,
  225. unsigned int reg,
  226. unsigned int page)
  227. {
  228. unsigned long flags __attribute__ ((unused));
  229. if (page == 0) {
  230. spin_lock_irqsave(&efx->biu_lock, flags);
  231. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  232. spin_unlock_irqrestore(&efx->biu_lock, flags);
  233. } else {
  234. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  235. }
  236. }
  237. #endif /* EFX_IO_H */