falcon_io.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2008 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_FALCON_IO_H
  11. #define EFX_FALCON_IO_H
  12. #include <linux/io.h>
  13. #include <linux/spinlock.h>
  14. #include "net_driver.h"
  15. /**************************************************************************
  16. *
  17. * Falcon hardware access
  18. *
  19. **************************************************************************
  20. *
  21. * Notes on locking strategy:
  22. *
  23. * Most Falcon registers require 16-byte (or 8-byte, for SRAM
  24. * registers) atomic writes which necessitates locking.
  25. * Under normal operation few writes to the Falcon BAR are made and these
  26. * registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and TX_DESC_UPD_REG) are special
  27. * cased to allow 4-byte (hence lockless) accesses.
  28. *
  29. * It *is* safe to write to these 4-byte registers in the middle of an
  30. * access to an 8-byte or 16-byte register. We therefore use a
  31. * spinlock to protect accesses to the larger registers, but no locks
  32. * for the 4-byte registers.
  33. *
  34. * A write barrier is needed to ensure that DW3 is written after DW0/1/2
  35. * due to the way the 16byte registers are "collected" in the Falcon BIU
  36. *
  37. * We also lock when carrying out reads, to ensure consistency of the
  38. * data (made possible since the BIU reads all 128 bits into a cache).
  39. * Reads are very rare, so this isn't a significant performance
  40. * impact. (Most data transferred from NIC to host is DMAed directly
  41. * into host memory).
  42. *
  43. * I/O BAR access uses locks for both reads and writes (but is only provided
  44. * for testing purposes).
  45. */
  46. /* Special buffer descriptors (Falcon SRAM) */
  47. #define BUF_TBL_KER_A1 0x18000
  48. #define BUF_TBL_KER_B0 0x800000
  49. #if BITS_PER_LONG == 64
  50. #define FALCON_USE_QWORD_IO 1
  51. #endif
  52. #ifdef FALCON_USE_QWORD_IO
  53. static inline void _falcon_writeq(struct efx_nic *efx, __le64 value,
  54. unsigned int reg)
  55. {
  56. __raw_writeq((__force u64)value, efx->membase + reg);
  57. }
  58. static inline __le64 _falcon_readq(struct efx_nic *efx, unsigned int reg)
  59. {
  60. return (__force __le64)__raw_readq(efx->membase + reg);
  61. }
  62. #endif
  63. static inline void _falcon_writel(struct efx_nic *efx, __le32 value,
  64. unsigned int reg)
  65. {
  66. __raw_writel((__force u32)value, efx->membase + reg);
  67. }
  68. static inline __le32 _falcon_readl(struct efx_nic *efx, unsigned int reg)
  69. {
  70. return (__force __le32)__raw_readl(efx->membase + reg);
  71. }
  72. /* Writes to a normal 16-byte Falcon register, locking as appropriate. */
  73. static inline void falcon_write(struct efx_nic *efx, efx_oword_t *value,
  74. unsigned int reg)
  75. {
  76. unsigned long flags;
  77. EFX_REGDUMP(efx, "writing register %x with " EFX_OWORD_FMT "\n", reg,
  78. EFX_OWORD_VAL(*value));
  79. spin_lock_irqsave(&efx->biu_lock, flags);
  80. #ifdef FALCON_USE_QWORD_IO
  81. _falcon_writeq(efx, value->u64[0], reg + 0);
  82. wmb();
  83. _falcon_writeq(efx, value->u64[1], reg + 8);
  84. #else
  85. _falcon_writel(efx, value->u32[0], reg + 0);
  86. _falcon_writel(efx, value->u32[1], reg + 4);
  87. _falcon_writel(efx, value->u32[2], reg + 8);
  88. wmb();
  89. _falcon_writel(efx, value->u32[3], reg + 12);
  90. #endif
  91. mmiowb();
  92. spin_unlock_irqrestore(&efx->biu_lock, flags);
  93. }
  94. /* Writes to an 8-byte Falcon SRAM register, locking as appropriate. */
  95. static inline void falcon_write_sram(struct efx_nic *efx, efx_qword_t *value,
  96. unsigned int index)
  97. {
  98. unsigned int reg = efx->type->buf_tbl_base + (index * sizeof(*value));
  99. unsigned long flags;
  100. EFX_REGDUMP(efx, "writing SRAM register %x with " EFX_QWORD_FMT "\n",
  101. reg, EFX_QWORD_VAL(*value));
  102. spin_lock_irqsave(&efx->biu_lock, flags);
  103. #ifdef FALCON_USE_QWORD_IO
  104. _falcon_writeq(efx, value->u64[0], reg + 0);
  105. #else
  106. _falcon_writel(efx, value->u32[0], reg + 0);
  107. wmb();
  108. _falcon_writel(efx, value->u32[1], reg + 4);
  109. #endif
  110. mmiowb();
  111. spin_unlock_irqrestore(&efx->biu_lock, flags);
  112. }
  113. /* Write dword to Falcon register that allows partial writes
  114. *
  115. * Some Falcon registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and
  116. * TX_DESC_UPD_REG) can be written to as a single dword. This allows
  117. * for lockless writes.
  118. */
  119. static inline void falcon_writel(struct efx_nic *efx, efx_dword_t *value,
  120. unsigned int reg)
  121. {
  122. EFX_REGDUMP(efx, "writing partial register %x with "EFX_DWORD_FMT"\n",
  123. reg, EFX_DWORD_VAL(*value));
  124. /* No lock required */
  125. _falcon_writel(efx, value->u32[0], reg);
  126. }
  127. /* Read from a Falcon register
  128. *
  129. * This reads an entire 16-byte Falcon register in one go, locking as
  130. * appropriate. It is essential to read the first dword first, as this
  131. * prompts Falcon to load the current value into the shadow register.
  132. */
  133. static inline void falcon_read(struct efx_nic *efx, efx_oword_t *value,
  134. unsigned int reg)
  135. {
  136. unsigned long flags;
  137. spin_lock_irqsave(&efx->biu_lock, flags);
  138. value->u32[0] = _falcon_readl(efx, reg + 0);
  139. rmb();
  140. value->u32[1] = _falcon_readl(efx, reg + 4);
  141. value->u32[2] = _falcon_readl(efx, reg + 8);
  142. value->u32[3] = _falcon_readl(efx, reg + 12);
  143. spin_unlock_irqrestore(&efx->biu_lock, flags);
  144. EFX_REGDUMP(efx, "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  145. EFX_OWORD_VAL(*value));
  146. }
  147. /* This reads an 8-byte Falcon SRAM entry in one go. */
  148. static inline void falcon_read_sram(struct efx_nic *efx, efx_qword_t *value,
  149. unsigned int index)
  150. {
  151. unsigned int reg = efx->type->buf_tbl_base + (index * sizeof(*value));
  152. unsigned long flags;
  153. spin_lock_irqsave(&efx->biu_lock, flags);
  154. #ifdef FALCON_USE_QWORD_IO
  155. value->u64[0] = _falcon_readq(efx, reg + 0);
  156. #else
  157. value->u32[0] = _falcon_readl(efx, reg + 0);
  158. rmb();
  159. value->u32[1] = _falcon_readl(efx, reg + 4);
  160. #endif
  161. spin_unlock_irqrestore(&efx->biu_lock, flags);
  162. EFX_REGDUMP(efx, "read from SRAM register %x, got "EFX_QWORD_FMT"\n",
  163. reg, EFX_QWORD_VAL(*value));
  164. }
  165. /* Read dword from Falcon register that allows partial writes (sic) */
  166. static inline void falcon_readl(struct efx_nic *efx, efx_dword_t *value,
  167. unsigned int reg)
  168. {
  169. value->u32[0] = _falcon_readl(efx, reg);
  170. EFX_REGDUMP(efx, "read from register %x, got "EFX_DWORD_FMT"\n",
  171. reg, EFX_DWORD_VAL(*value));
  172. }
  173. /* Write to a register forming part of a table */
  174. static inline void falcon_write_table(struct efx_nic *efx, efx_oword_t *value,
  175. unsigned int reg, unsigned int index)
  176. {
  177. falcon_write(efx, value, reg + index * sizeof(efx_oword_t));
  178. }
  179. /* Read to a register forming part of a table */
  180. static inline void falcon_read_table(struct efx_nic *efx, efx_oword_t *value,
  181. unsigned int reg, unsigned int index)
  182. {
  183. falcon_read(efx, value, reg + index * sizeof(efx_oword_t));
  184. }
  185. /* Write to a dword register forming part of a table */
  186. static inline void falcon_writel_table(struct efx_nic *efx, efx_dword_t *value,
  187. unsigned int reg, unsigned int index)
  188. {
  189. falcon_writel(efx, value, reg + index * sizeof(efx_oword_t));
  190. }
  191. /* Page-mapped register block size */
  192. #define FALCON_PAGE_BLOCK_SIZE 0x2000
  193. /* Calculate offset to page-mapped register block */
  194. #define FALCON_PAGED_REG(page, reg) \
  195. ((page) * FALCON_PAGE_BLOCK_SIZE + (reg))
  196. /* As for falcon_write(), but for a page-mapped register. */
  197. static inline void falcon_write_page(struct efx_nic *efx, efx_oword_t *value,
  198. unsigned int reg, unsigned int page)
  199. {
  200. falcon_write(efx, value, FALCON_PAGED_REG(page, reg));
  201. }
  202. /* As for falcon_writel(), but for a page-mapped register. */
  203. static inline void falcon_writel_page(struct efx_nic *efx, efx_dword_t *value,
  204. unsigned int reg, unsigned int page)
  205. {
  206. falcon_writel(efx, value, FALCON_PAGED_REG(page, reg));
  207. }
  208. /* Write dword to Falcon page-mapped register with an extra lock.
  209. *
  210. * As for falcon_writel_page(), but for a register that suffers from
  211. * SFC bug 3181. Take out a lock so the BIU collector cannot be
  212. * confused. */
  213. static inline void falcon_writel_page_locked(struct efx_nic *efx,
  214. efx_dword_t *value,
  215. unsigned int reg,
  216. unsigned int page)
  217. {
  218. unsigned long flags;
  219. spin_lock_irqsave(&efx->biu_lock, flags);
  220. falcon_writel(efx, value, FALCON_PAGED_REG(page, reg));
  221. spin_unlock_irqrestore(&efx->biu_lock, flags);
  222. }
  223. #endif /* EFX_FALCON_IO_H */