emac-mac.h 8.5 KB

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