io.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2010 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. * Many CSRs are very wide and cannot be read or written atomically.
  23. * Writes from the host are buffered by the Bus Interface Unit (BIU)
  24. * up to 128 bits. Whenever the host writes part of such a register,
  25. * the BIU collects the written value and does not write to the
  26. * underlying register until all 4 dwords have been written. A
  27. * similar buffering scheme applies to host access to the NIC's 64-bit
  28. * SRAM.
  29. *
  30. * Writes to different CSRs and 64-bit SRAM words must be serialised,
  31. * since interleaved access can result in lost writes. We use
  32. * efx_nic::biu_lock for this.
  33. *
  34. * We also serialise reads from 128-bit CSRs and SRAM with the same
  35. * spinlock. This may not be necessary, but it doesn't really matter
  36. * as there are no such reads on the fast path.
  37. *
  38. * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
  39. * 128-bit but are special-cased in the BIU to avoid the need for
  40. * locking in the host:
  41. *
  42. * - They are write-only.
  43. * - The semantics of writing to these registers are such that
  44. * replacing the low 96 bits with zero does not affect functionality.
  45. * - If the host writes to the last dword address of such a register
  46. * (i.e. the high 32 bits) the underlying register will always be
  47. * written. If the collector and the current write together do not
  48. * provide values for all 128 bits of the register, the low 96 bits
  49. * will be written as zero.
  50. * - If the host writes to the address of any other part of such a
  51. * register while the collector already holds values for some other
  52. * register, the write is discarded and the collector maintains its
  53. * current state.
  54. */
  55. #if BITS_PER_LONG == 64
  56. #define EFX_USE_QWORD_IO 1
  57. #endif
  58. #ifdef EFX_USE_QWORD_IO
  59. static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
  60. unsigned int reg)
  61. {
  62. __raw_writeq((__force u64)value, efx->membase + reg);
  63. }
  64. static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
  65. {
  66. return (__force __le64)__raw_readq(efx->membase + reg);
  67. }
  68. #endif
  69. static inline void _efx_writed(struct efx_nic *efx, __le32 value,
  70. unsigned int reg)
  71. {
  72. __raw_writel((__force u32)value, efx->membase + reg);
  73. }
  74. static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
  75. {
  76. return (__force __le32)__raw_readl(efx->membase + reg);
  77. }
  78. /* Write a normal 128-bit CSR, locking as appropriate. */
  79. static inline void efx_writeo(struct efx_nic *efx, efx_oword_t *value,
  80. unsigned int reg)
  81. {
  82. unsigned long flags __attribute__ ((unused));
  83. netif_vdbg(efx, hw, efx->net_dev,
  84. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  85. EFX_OWORD_VAL(*value));
  86. spin_lock_irqsave(&efx->biu_lock, flags);
  87. #ifdef EFX_USE_QWORD_IO
  88. _efx_writeq(efx, value->u64[0], reg + 0);
  89. _efx_writeq(efx, value->u64[1], reg + 8);
  90. #else
  91. _efx_writed(efx, value->u32[0], reg + 0);
  92. _efx_writed(efx, value->u32[1], reg + 4);
  93. _efx_writed(efx, value->u32[2], reg + 8);
  94. _efx_writed(efx, value->u32[3], reg + 12);
  95. #endif
  96. mmiowb();
  97. spin_unlock_irqrestore(&efx->biu_lock, flags);
  98. }
  99. /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
  100. static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
  101. efx_qword_t *value, unsigned int index)
  102. {
  103. unsigned int addr = index * sizeof(*value);
  104. unsigned long flags __attribute__ ((unused));
  105. netif_vdbg(efx, hw, efx->net_dev,
  106. "writing SRAM address %x with " EFX_QWORD_FMT "\n",
  107. addr, EFX_QWORD_VAL(*value));
  108. spin_lock_irqsave(&efx->biu_lock, flags);
  109. #ifdef EFX_USE_QWORD_IO
  110. __raw_writeq((__force u64)value->u64[0], membase + addr);
  111. #else
  112. __raw_writel((__force u32)value->u32[0], membase + addr);
  113. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  114. #endif
  115. mmiowb();
  116. spin_unlock_irqrestore(&efx->biu_lock, flags);
  117. }
  118. /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
  119. static inline void efx_writed(struct efx_nic *efx, efx_dword_t *value,
  120. unsigned int reg)
  121. {
  122. netif_vdbg(efx, hw, efx->net_dev,
  123. "writing register %x with "EFX_DWORD_FMT"\n",
  124. reg, EFX_DWORD_VAL(*value));
  125. /* No lock required */
  126. _efx_writed(efx, value->u32[0], reg);
  127. }
  128. /* Read a 128-bit CSR, locking as appropriate. */
  129. static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
  130. unsigned int reg)
  131. {
  132. unsigned long flags __attribute__ ((unused));
  133. spin_lock_irqsave(&efx->biu_lock, flags);
  134. value->u32[0] = _efx_readd(efx, reg + 0);
  135. value->u32[1] = _efx_readd(efx, reg + 4);
  136. value->u32[2] = _efx_readd(efx, reg + 8);
  137. value->u32[3] = _efx_readd(efx, reg + 12);
  138. spin_unlock_irqrestore(&efx->biu_lock, flags);
  139. netif_vdbg(efx, hw, efx->net_dev,
  140. "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  141. EFX_OWORD_VAL(*value));
  142. }
  143. /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
  144. static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
  145. efx_qword_t *value, unsigned int index)
  146. {
  147. unsigned int addr = index * sizeof(*value);
  148. unsigned long flags __attribute__ ((unused));
  149. spin_lock_irqsave(&efx->biu_lock, flags);
  150. #ifdef EFX_USE_QWORD_IO
  151. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  152. #else
  153. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  154. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  155. #endif
  156. spin_unlock_irqrestore(&efx->biu_lock, flags);
  157. netif_vdbg(efx, hw, efx->net_dev,
  158. "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
  159. addr, EFX_QWORD_VAL(*value));
  160. }
  161. /* Read a 32-bit CSR or SRAM */
  162. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  163. unsigned int reg)
  164. {
  165. value->u32[0] = _efx_readd(efx, reg);
  166. netif_vdbg(efx, hw, efx->net_dev,
  167. "read from register %x, got "EFX_DWORD_FMT"\n",
  168. reg, EFX_DWORD_VAL(*value));
  169. }
  170. /* Write a 128-bit CSR forming part of a table */
  171. static inline void efx_writeo_table(struct efx_nic *efx, efx_oword_t *value,
  172. unsigned int reg, unsigned int index)
  173. {
  174. efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
  175. }
  176. /* Read a 128-bit CSR forming part of a table */
  177. static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
  178. unsigned int reg, unsigned int index)
  179. {
  180. efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
  181. }
  182. /* Page-mapped register block size */
  183. #define EFX_PAGE_BLOCK_SIZE 0x2000
  184. /* Calculate offset to page-mapped register block */
  185. #define EFX_PAGED_REG(page, reg) \
  186. ((page) * EFX_PAGE_BLOCK_SIZE + (reg))
  187. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  188. static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  189. unsigned int reg, unsigned int page)
  190. {
  191. reg = EFX_PAGED_REG(page, reg);
  192. netif_vdbg(efx, hw, efx->net_dev,
  193. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  194. EFX_OWORD_VAL(*value));
  195. #ifdef EFX_USE_QWORD_IO
  196. _efx_writeq(efx, value->u64[0], reg + 0);
  197. _efx_writeq(efx, value->u64[1], reg + 8);
  198. #else
  199. _efx_writed(efx, value->u32[0], reg + 0);
  200. _efx_writed(efx, value->u32[1], reg + 4);
  201. _efx_writed(efx, value->u32[2], reg + 8);
  202. _efx_writed(efx, value->u32[3], reg + 12);
  203. #endif
  204. }
  205. #define efx_writeo_page(efx, value, reg, page) \
  206. _efx_writeo_page(efx, value, \
  207. reg + \
  208. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  209. page)
  210. /* Write a page-mapped 32-bit CSR (EVQ_RPTR or the high bits of
  211. * RX_DESC_UPD or TX_DESC_UPD)
  212. */
  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. #define efx_writed_page(efx, value, reg, page) \
  219. _efx_writed_page(efx, value, \
  220. reg + \
  221. BUILD_BUG_ON_ZERO((reg) != 0x400 && (reg) != 0x83c \
  222. && (reg) != 0xa1c), \
  223. page)
  224. /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
  225. * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
  226. * collector register.
  227. */
  228. static inline void _efx_writed_page_locked(struct efx_nic *efx,
  229. efx_dword_t *value,
  230. unsigned int reg,
  231. unsigned int page)
  232. {
  233. unsigned long flags __attribute__ ((unused));
  234. if (page == 0) {
  235. spin_lock_irqsave(&efx->biu_lock, flags);
  236. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  237. spin_unlock_irqrestore(&efx->biu_lock, flags);
  238. } else {
  239. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  240. }
  241. }
  242. #define efx_writed_page_locked(efx, value, reg, page) \
  243. _efx_writed_page_locked(efx, value, \
  244. reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
  245. page)
  246. #endif /* EFX_IO_H */