io.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. mmiowb();
  98. spin_unlock_irqrestore(&efx->biu_lock, flags);
  99. }
  100. /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
  101. static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
  102. efx_qword_t *value, unsigned int index)
  103. {
  104. unsigned int addr = index * sizeof(*value);
  105. unsigned long flags __attribute__ ((unused));
  106. netif_vdbg(efx, hw, efx->net_dev,
  107. "writing SRAM address %x with " EFX_QWORD_FMT "\n",
  108. addr, EFX_QWORD_VAL(*value));
  109. spin_lock_irqsave(&efx->biu_lock, flags);
  110. #ifdef EFX_USE_QWORD_IO
  111. __raw_writeq((__force u64)value->u64[0], membase + addr);
  112. #else
  113. __raw_writel((__force u32)value->u32[0], membase + addr);
  114. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  115. #endif
  116. mmiowb();
  117. spin_unlock_irqrestore(&efx->biu_lock, flags);
  118. }
  119. /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
  120. static inline void efx_writed(struct efx_nic *efx, efx_dword_t *value,
  121. unsigned int reg)
  122. {
  123. netif_vdbg(efx, hw, efx->net_dev,
  124. "writing register %x with "EFX_DWORD_FMT"\n",
  125. reg, EFX_DWORD_VAL(*value));
  126. /* No lock required */
  127. _efx_writed(efx, value->u32[0], reg);
  128. }
  129. /* Read a 128-bit CSR, locking as appropriate. */
  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. value->u32[1] = _efx_readd(efx, reg + 4);
  137. value->u32[2] = _efx_readd(efx, reg + 8);
  138. value->u32[3] = _efx_readd(efx, reg + 12);
  139. spin_unlock_irqrestore(&efx->biu_lock, flags);
  140. netif_vdbg(efx, hw, efx->net_dev,
  141. "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  142. EFX_OWORD_VAL(*value));
  143. }
  144. /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
  145. static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
  146. efx_qword_t *value, unsigned int index)
  147. {
  148. unsigned int addr = index * sizeof(*value);
  149. unsigned long flags __attribute__ ((unused));
  150. spin_lock_irqsave(&efx->biu_lock, flags);
  151. #ifdef EFX_USE_QWORD_IO
  152. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  153. #else
  154. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  155. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  156. #endif
  157. spin_unlock_irqrestore(&efx->biu_lock, flags);
  158. netif_vdbg(efx, hw, efx->net_dev,
  159. "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
  160. addr, EFX_QWORD_VAL(*value));
  161. }
  162. /* Read a 32-bit CSR or SRAM */
  163. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  164. unsigned int reg)
  165. {
  166. value->u32[0] = _efx_readd(efx, reg);
  167. netif_vdbg(efx, hw, efx->net_dev,
  168. "read from register %x, got "EFX_DWORD_FMT"\n",
  169. reg, EFX_DWORD_VAL(*value));
  170. }
  171. /* Write a 128-bit CSR 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 a 128-bit CSR 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 a 32-bit CSR forming part of a table, or 32-bit SRAM */
  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. /* Read a 32-bit CSR forming part of a table, or 32-bit SRAM */
  190. static inline void efx_readd_table(struct efx_nic *efx, efx_dword_t *value,
  191. unsigned int reg, unsigned int index)
  192. {
  193. efx_readd(efx, value, reg + index * sizeof(efx_dword_t));
  194. }
  195. /* Page-mapped register block size */
  196. #define EFX_PAGE_BLOCK_SIZE 0x2000
  197. /* Calculate offset to page-mapped register block */
  198. #define EFX_PAGED_REG(page, reg) \
  199. ((page) * EFX_PAGE_BLOCK_SIZE + (reg))
  200. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  201. static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  202. unsigned int reg, unsigned int page)
  203. {
  204. reg = EFX_PAGED_REG(page, reg);
  205. netif_vdbg(efx, hw, efx->net_dev,
  206. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  207. EFX_OWORD_VAL(*value));
  208. #ifdef EFX_USE_QWORD_IO
  209. _efx_writeq(efx, value->u64[0], reg + 0);
  210. _efx_writeq(efx, value->u64[1], reg + 8);
  211. #else
  212. _efx_writed(efx, value->u32[0], reg + 0);
  213. _efx_writed(efx, value->u32[1], reg + 4);
  214. _efx_writed(efx, value->u32[2], reg + 8);
  215. _efx_writed(efx, value->u32[3], reg + 12);
  216. #endif
  217. }
  218. #define efx_writeo_page(efx, value, reg, page) \
  219. _efx_writeo_page(efx, value, \
  220. reg + \
  221. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  222. page)
  223. /* Write a page-mapped 32-bit CSR (EVQ_RPTR or the high bits of
  224. * RX_DESC_UPD or TX_DESC_UPD)
  225. */
  226. static inline void _efx_writed_page(struct efx_nic *efx, efx_dword_t *value,
  227. unsigned int reg, unsigned int page)
  228. {
  229. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  230. }
  231. #define efx_writed_page(efx, value, reg, page) \
  232. _efx_writed_page(efx, value, \
  233. reg + \
  234. BUILD_BUG_ON_ZERO((reg) != 0x400 && (reg) != 0x83c \
  235. && (reg) != 0xa1c), \
  236. page)
  237. /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
  238. * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
  239. * collector register.
  240. */
  241. static inline void _efx_writed_page_locked(struct efx_nic *efx,
  242. efx_dword_t *value,
  243. unsigned int reg,
  244. unsigned int page)
  245. {
  246. unsigned long flags __attribute__ ((unused));
  247. if (page == 0) {
  248. spin_lock_irqsave(&efx->biu_lock, flags);
  249. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  250. spin_unlock_irqrestore(&efx->biu_lock, flags);
  251. } else {
  252. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  253. }
  254. }
  255. #define efx_writed_page_locked(efx, value, reg, page) \
  256. _efx_writed_page_locked(efx, value, \
  257. reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
  258. page)
  259. #endif /* EFX_IO_H */