io.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. 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 64-bit SRAM through the supplied mapping, locking as appropriate. */
  149. static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
  150. efx_qword_t *value, unsigned int index)
  151. {
  152. unsigned int addr = index * sizeof(*value);
  153. unsigned long flags __attribute__ ((unused));
  154. spin_lock_irqsave(&efx->biu_lock, flags);
  155. #ifdef EFX_USE_QWORD_IO
  156. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  157. #else
  158. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  159. rmb();
  160. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  161. #endif
  162. spin_unlock_irqrestore(&efx->biu_lock, flags);
  163. netif_vdbg(efx, hw, efx->net_dev,
  164. "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
  165. addr, EFX_QWORD_VAL(*value));
  166. }
  167. /* Read a 32-bit CSR or SRAM */
  168. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  169. unsigned int reg)
  170. {
  171. value->u32[0] = _efx_readd(efx, reg);
  172. netif_vdbg(efx, hw, efx->net_dev,
  173. "read from register %x, got "EFX_DWORD_FMT"\n",
  174. reg, EFX_DWORD_VAL(*value));
  175. }
  176. /* Write a 128-bit CSR forming part of a table */
  177. static inline void efx_writeo_table(struct efx_nic *efx, efx_oword_t *value,
  178. unsigned int reg, unsigned int index)
  179. {
  180. efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
  181. }
  182. /* Read a 128-bit CSR forming part of a table */
  183. static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
  184. unsigned int reg, unsigned int index)
  185. {
  186. efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
  187. }
  188. /* Write a 32-bit CSR forming part of a table, or 32-bit SRAM */
  189. static inline void efx_writed_table(struct efx_nic *efx, efx_dword_t *value,
  190. unsigned int reg, unsigned int index)
  191. {
  192. efx_writed(efx, value, reg + index * sizeof(efx_oword_t));
  193. }
  194. /* Read a 32-bit CSR forming part of a table, or 32-bit SRAM */
  195. static inline void efx_readd_table(struct efx_nic *efx, efx_dword_t *value,
  196. unsigned int reg, unsigned int index)
  197. {
  198. efx_readd(efx, value, reg + index * sizeof(efx_dword_t));
  199. }
  200. /* Page-mapped register block size */
  201. #define EFX_PAGE_BLOCK_SIZE 0x2000
  202. /* Calculate offset to page-mapped register block */
  203. #define EFX_PAGED_REG(page, reg) \
  204. ((page) * EFX_PAGE_BLOCK_SIZE + (reg))
  205. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  206. static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  207. unsigned int reg, unsigned int page)
  208. {
  209. reg = EFX_PAGED_REG(page, reg);
  210. netif_vdbg(efx, hw, efx->net_dev,
  211. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  212. EFX_OWORD_VAL(*value));
  213. #ifdef EFX_USE_QWORD_IO
  214. _efx_writeq(efx, value->u64[0], reg + 0);
  215. _efx_writeq(efx, value->u64[1], reg + 8);
  216. #else
  217. _efx_writed(efx, value->u32[0], reg + 0);
  218. _efx_writed(efx, value->u32[1], reg + 4);
  219. _efx_writed(efx, value->u32[2], reg + 8);
  220. _efx_writed(efx, value->u32[3], reg + 12);
  221. #endif
  222. wmb();
  223. }
  224. #define efx_writeo_page(efx, value, reg, page) \
  225. _efx_writeo_page(efx, value, \
  226. reg + \
  227. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  228. page)
  229. /* Write a page-mapped 32-bit CSR (EVQ_RPTR or the high bits of
  230. * RX_DESC_UPD or TX_DESC_UPD)
  231. */
  232. static inline void _efx_writed_page(struct efx_nic *efx, efx_dword_t *value,
  233. unsigned int reg, unsigned int page)
  234. {
  235. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  236. }
  237. #define efx_writed_page(efx, value, reg, page) \
  238. _efx_writed_page(efx, value, \
  239. reg + \
  240. BUILD_BUG_ON_ZERO((reg) != 0x400 && (reg) != 0x83c \
  241. && (reg) != 0xa1c), \
  242. page)
  243. /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
  244. * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
  245. * collector register.
  246. */
  247. static inline void _efx_writed_page_locked(struct efx_nic *efx,
  248. efx_dword_t *value,
  249. unsigned int reg,
  250. unsigned int page)
  251. {
  252. unsigned long flags __attribute__ ((unused));
  253. if (page == 0) {
  254. spin_lock_irqsave(&efx->biu_lock, flags);
  255. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  256. spin_unlock_irqrestore(&efx->biu_lock, flags);
  257. } else {
  258. efx_writed(efx, value, EFX_PAGED_REG(page, reg));
  259. }
  260. }
  261. #define efx_writed_page_locked(efx, value, reg, page) \
  262. _efx_writed_page_locked(efx, value, \
  263. reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
  264. page)
  265. #endif /* EFX_IO_H */