io.h 9.8 KB

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