emac-mac.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
  3. */
  4. /* EMAC DMA HW engine uses three rings:
  5. * Tx:
  6. * TPD: Transmit Packet Descriptor ring.
  7. * Rx:
  8. * RFD: Receive Free Descriptor ring.
  9. * Ring of descriptors with empty buffers to be filled by Rx HW.
  10. * RRD: Receive Return Descriptor ring.
  11. * Ring of descriptors with buffers filled with received data.
  12. */
  13. #ifndef _EMAC_HW_H_
  14. #define _EMAC_HW_H_
  15. /* EMAC_CSR register offsets */
  16. #define EMAC_EMAC_WRAPPER_CSR1 0x000000
  17. #define EMAC_EMAC_WRAPPER_CSR2 0x000004
  18. #define EMAC_EMAC_WRAPPER_TX_TS_LO 0x000104
  19. #define EMAC_EMAC_WRAPPER_TX_TS_HI 0x000108
  20. #define EMAC_EMAC_WRAPPER_TX_TS_INX 0x00010c
  21. /* DMA Order Settings */
  22. enum emac_dma_order {
  23. emac_dma_ord_in = 1,
  24. emac_dma_ord_enh = 2,
  25. emac_dma_ord_out = 4
  26. };
  27. enum emac_dma_req_block {
  28. emac_dma_req_128 = 0,
  29. emac_dma_req_256 = 1,
  30. emac_dma_req_512 = 2,
  31. emac_dma_req_1024 = 3,
  32. emac_dma_req_2048 = 4,
  33. emac_dma_req_4096 = 5
  34. };
  35. /* Returns the value of bits idx...idx+n_bits */
  36. #define BITS_GET(val, lo, hi) ((le32_to_cpu(val) & GENMASK((hi), (lo))) >> lo)
  37. #define BITS_SET(val, lo, hi, new_val) \
  38. val = cpu_to_le32((le32_to_cpu(val) & (~GENMASK((hi), (lo)))) | \
  39. (((new_val) << (lo)) & GENMASK((hi), (lo))))
  40. /* RRD (Receive Return Descriptor) */
  41. struct emac_rrd {
  42. u32 word[6];
  43. /* number of RFD */
  44. #define RRD_NOR(rrd) BITS_GET((rrd)->word[0], 16, 19)
  45. /* start consumer index of rfd-ring */
  46. #define RRD_SI(rrd) BITS_GET((rrd)->word[0], 20, 31)
  47. /* vlan-tag (CVID, CFI and PRI) */
  48. #define RRD_CVALN_TAG(rrd) BITS_GET((rrd)->word[2], 0, 15)
  49. /* length of the packet */
  50. #define RRD_PKT_SIZE(rrd) BITS_GET((rrd)->word[3], 0, 13)
  51. /* L4(TCP/UDP) checksum failed */
  52. #define RRD_L4F(rrd) BITS_GET((rrd)->word[3], 14, 14)
  53. /* vlan tagged */
  54. #define RRD_CVTAG(rrd) BITS_GET((rrd)->word[3], 16, 16)
  55. /* When set, indicates that the descriptor is updated by the IP core.
  56. * When cleared, indicates that the descriptor is invalid.
  57. */
  58. #define RRD_UPDT(rrd) BITS_GET((rrd)->word[3], 31, 31)
  59. #define RRD_UPDT_SET(rrd, val) BITS_SET((rrd)->word[3], 31, 31, val)
  60. /* timestamp low */
  61. #define RRD_TS_LOW(rrd) BITS_GET((rrd)->word[4], 0, 29)
  62. /* timestamp high */
  63. #define RRD_TS_HI(rrd) le32_to_cpu((rrd)->word[5])
  64. };
  65. /* TPD (Transmit Packet Descriptor) */
  66. struct emac_tpd {
  67. u32 word[4];
  68. /* Number of bytes of the transmit packet. (include 4-byte CRC) */
  69. #define TPD_BUF_LEN_SET(tpd, val) BITS_SET((tpd)->word[0], 0, 15, val)
  70. /* Custom Checksum Offload: When set, ask IP core to offload custom checksum */
  71. #define TPD_CSX_SET(tpd, val) BITS_SET((tpd)->word[1], 8, 8, val)
  72. /* TCP Large Send Offload: When set, ask IP core to do offload TCP Large Send */
  73. #define TPD_LSO(tpd) BITS_GET((tpd)->word[1], 12, 12)
  74. #define TPD_LSO_SET(tpd, val) BITS_SET((tpd)->word[1], 12, 12, val)
  75. /* Large Send Offload Version: When set, indicates this is an LSOv2
  76. * (for both IPv4 and IPv6). When cleared, indicates this is an LSOv1
  77. * (only for IPv4).
  78. */
  79. #define TPD_LSOV_SET(tpd, val) BITS_SET((tpd)->word[1], 13, 13, val)
  80. /* IPv4 packet: When set, indicates this is an IPv4 packet, this bit is only
  81. * for LSOV2 format.
  82. */
  83. #define TPD_IPV4_SET(tpd, val) BITS_SET((tpd)->word[1], 16, 16, val)
  84. /* 0: Ethernet frame (DA+SA+TYPE+DATA+CRC)
  85. * 1: IEEE 802.3 frame (DA+SA+LEN+DSAP+SSAP+CTL+ORG+TYPE+DATA+CRC)
  86. */
  87. #define TPD_TYP_SET(tpd, val) BITS_SET((tpd)->word[1], 17, 17, val)
  88. /* Low-32bit Buffer Address */
  89. #define TPD_BUFFER_ADDR_L_SET(tpd, val) ((tpd)->word[2] = cpu_to_le32(val))
  90. /* CVLAN Tag to be inserted if INS_VLAN_TAG is set, CVLAN TPID based on global
  91. * register configuration.
  92. */
  93. #define TPD_CVLAN_TAG_SET(tpd, val) BITS_SET((tpd)->word[3], 0, 15, val)
  94. /* Insert CVlan Tag: When set, ask MAC to insert CVLAN TAG to outgoing packet
  95. */
  96. #define TPD_INSTC_SET(tpd, val) BITS_SET((tpd)->word[3], 17, 17, val)
  97. /* High-14bit Buffer Address, So, the 64b-bit address is
  98. * {DESC_CTRL_11_TX_DATA_HIADDR[17:0],(register) BUFFER_ADDR_H, BUFFER_ADDR_L}
  99. * Extend TPD_BUFFER_ADDR_H to [31, 18], because we never enable timestamping.
  100. */
  101. #define TPD_BUFFER_ADDR_H_SET(tpd, val) BITS_SET((tpd)->word[3], 18, 31, val)
  102. /* Format D. Word offset from the 1st byte of this packet to start to calculate
  103. * the custom checksum.
  104. */
  105. #define TPD_PAYLOAD_OFFSET_SET(tpd, val) BITS_SET((tpd)->word[1], 0, 7, val)
  106. /* Format D. Word offset from the 1st byte of this packet to fill the custom
  107. * checksum to
  108. */
  109. #define TPD_CXSUM_OFFSET_SET(tpd, val) BITS_SET((tpd)->word[1], 18, 25, val)
  110. /* Format C. TCP Header offset from the 1st byte of this packet. (byte unit) */
  111. #define TPD_TCPHDR_OFFSET_SET(tpd, val) BITS_SET((tpd)->word[1], 0, 7, val)
  112. /* Format C. MSS (Maximum Segment Size) got from the protocol layer. (byte unit)
  113. */
  114. #define TPD_MSS_SET(tpd, val) BITS_SET((tpd)->word[1], 18, 30, val)
  115. /* packet length in ext tpd */
  116. #define TPD_PKT_LEN_SET(tpd, val) ((tpd)->word[2] = cpu_to_le32(val))
  117. };
  118. /* emac_ring_header represents a single, contiguous block of DMA space
  119. * mapped for the three descriptor rings (tpd, rfd, rrd)
  120. */
  121. struct emac_ring_header {
  122. void *v_addr; /* virtual address */
  123. dma_addr_t dma_addr; /* dma address */
  124. size_t size; /* length in bytes */
  125. size_t used;
  126. };
  127. /* emac_buffer is wrapper around a pointer to a socket buffer
  128. * so a DMA handle can be stored along with the skb
  129. */
  130. struct emac_buffer {
  131. struct sk_buff *skb; /* socket buffer */
  132. u16 length; /* rx buffer length */
  133. dma_addr_t dma_addr; /* dma address */
  134. };
  135. /* receive free descriptor (rfd) ring */
  136. struct emac_rfd_ring {
  137. struct emac_buffer *rfbuff;
  138. u32 *v_addr; /* virtual address */
  139. dma_addr_t dma_addr; /* dma address */
  140. size_t size; /* length in bytes */
  141. unsigned int count; /* number of desc in the ring */
  142. unsigned int produce_idx;
  143. unsigned int process_idx;
  144. unsigned int consume_idx; /* unused */
  145. };
  146. /* Receive Return Desciptor (RRD) ring */
  147. struct emac_rrd_ring {
  148. u32 *v_addr; /* virtual address */
  149. dma_addr_t dma_addr; /* physical address */
  150. size_t size; /* length in bytes */
  151. unsigned int count; /* number of desc in the ring */
  152. unsigned int produce_idx; /* unused */
  153. unsigned int consume_idx;
  154. };
  155. /* Rx queue */
  156. struct emac_rx_queue {
  157. struct net_device *netdev; /* netdev ring belongs to */
  158. struct emac_rrd_ring rrd;
  159. struct emac_rfd_ring rfd;
  160. struct napi_struct napi;
  161. struct emac_irq *irq;
  162. u32 intr;
  163. u32 produce_mask;
  164. u32 process_mask;
  165. u32 consume_mask;
  166. u16 produce_reg;
  167. u16 process_reg;
  168. u16 consume_reg;
  169. u8 produce_shift;
  170. u8 process_shft;
  171. u8 consume_shift;
  172. };
  173. /* Transimit Packet Descriptor (tpd) ring */
  174. struct emac_tpd_ring {
  175. struct emac_buffer *tpbuff;
  176. u32 *v_addr; /* virtual address */
  177. dma_addr_t dma_addr; /* dma address */
  178. size_t size; /* length in bytes */
  179. unsigned int count; /* number of desc in the ring */
  180. unsigned int produce_idx;
  181. unsigned int consume_idx;
  182. unsigned int last_produce_idx;
  183. };
  184. /* Tx queue */
  185. struct emac_tx_queue {
  186. struct emac_tpd_ring tpd;
  187. u32 produce_mask;
  188. u32 consume_mask;
  189. u16 max_packets; /* max packets per interrupt */
  190. u16 produce_reg;
  191. u16 consume_reg;
  192. u8 produce_shift;
  193. u8 consume_shift;
  194. };
  195. struct emac_adapter;
  196. int emac_mac_up(struct emac_adapter *adpt);
  197. void emac_mac_down(struct emac_adapter *adpt);
  198. void emac_mac_reset(struct emac_adapter *adpt);
  199. void emac_mac_stop(struct emac_adapter *adpt);
  200. void emac_mac_mode_config(struct emac_adapter *adpt);
  201. void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
  202. int *num_pkts, int max_pkts);
  203. int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
  204. struct sk_buff *skb);
  205. void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q);
  206. void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
  207. struct emac_adapter *adpt);
  208. int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt);
  209. void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt);
  210. void emac_mac_multicast_addr_clear(struct emac_adapter *adpt);
  211. void emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr);
  212. #endif /*_EMAC_HW_H_*/