cvmx-helper-util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020 Marvell International Ltd.
  4. */
  5. #ifndef __CVMX_HELPER_UTIL_H__
  6. #define __CVMX_HELPER_UTIL_H__
  7. #include "cvmx-mio-defs.h"
  8. #include "cvmx-helper.h"
  9. #include "cvmx-fpa.h"
  10. typedef char cvmx_pknd_t;
  11. typedef char cvmx_bpid_t;
  12. #define CVMX_INVALID_PKND ((cvmx_pknd_t)-1)
  13. #define CVMX_INVALID_BPID ((cvmx_bpid_t)-1)
  14. #define CVMX_MAX_PKND ((cvmx_pknd_t)64)
  15. #define CVMX_MAX_BPID ((cvmx_bpid_t)64)
  16. #define CVMX_HELPER_MAX_IFACE 11
  17. #define CVMX_HELPER_MAX_PORTS 16
  18. /* Maximum range for normalized (a.k.a. IPD) port numbers (12-bit field) */
  19. #define CVMX_PKO3_IPD_NUM_MAX 0x1000 //FIXME- take it from someplace else ?
  20. #define CVMX_PKO3_DQ_NUM_MAX 0x400 // 78xx has 1024 queues
  21. #define CVMX_PKO3_IPD_PORT_NULL (CVMX_PKO3_IPD_NUM_MAX - 1)
  22. #define CVMX_PKO3_IPD_PORT_LOOP 0
  23. struct cvmx_xport {
  24. int node;
  25. int port;
  26. };
  27. typedef struct cvmx_xport cvmx_xport_t;
  28. static inline struct cvmx_xport cvmx_helper_ipd_port_to_xport(int ipd_port)
  29. {
  30. struct cvmx_xport r;
  31. r.port = ipd_port & (CVMX_PKO3_IPD_NUM_MAX - 1);
  32. r.node = (ipd_port >> 12) & CVMX_NODE_MASK;
  33. return r;
  34. }
  35. static inline int cvmx_helper_node_to_ipd_port(int node, int index)
  36. {
  37. return (node << 12) + index;
  38. }
  39. struct cvmx_xdq {
  40. int node;
  41. int queue;
  42. };
  43. typedef struct cvmx_xdq cvmx_xdq_t;
  44. static inline struct cvmx_xdq cvmx_helper_queue_to_xdq(int queue)
  45. {
  46. struct cvmx_xdq r;
  47. r.queue = queue & (CVMX_PKO3_DQ_NUM_MAX - 1);
  48. r.node = (queue >> 10) & CVMX_NODE_MASK;
  49. return r;
  50. }
  51. static inline int cvmx_helper_node_to_dq(int node, int queue)
  52. {
  53. return (node << 10) + queue;
  54. }
  55. struct cvmx_xiface {
  56. int node;
  57. int interface;
  58. };
  59. typedef struct cvmx_xiface cvmx_xiface_t;
  60. /**
  61. * Return node and interface number from XIFACE.
  62. *
  63. * @param xiface interface with node information
  64. *
  65. * Return: struct that contains node and interface number.
  66. */
  67. static inline struct cvmx_xiface cvmx_helper_xiface_to_node_interface(int xiface)
  68. {
  69. cvmx_xiface_t interface_node;
  70. /*
  71. * If the majic number 0xde0000 is not present in the
  72. * interface, then assume it is node 0.
  73. */
  74. if (((xiface >> 0x8) & 0xff) == 0xde) {
  75. interface_node.node = (xiface >> 16) & CVMX_NODE_MASK;
  76. interface_node.interface = xiface & 0xff;
  77. } else {
  78. interface_node.node = cvmx_get_node_num();
  79. interface_node.interface = xiface & 0xff;
  80. }
  81. return interface_node;
  82. }
  83. /* Used internally only*/
  84. static inline bool __cvmx_helper_xiface_is_null(int xiface)
  85. {
  86. return (xiface & 0xff) == 0xff;
  87. }
  88. #define __CVMX_XIFACE_NULL 0xff
  89. /**
  90. * Return interface with majic number and node information (XIFACE)
  91. *
  92. * @param node node of the interface referred to
  93. * @param interface interface to use.
  94. *
  95. * @return
  96. */
  97. static inline int cvmx_helper_node_interface_to_xiface(int node, int interface)
  98. {
  99. return ((node & CVMX_NODE_MASK) << 16) | (0xde << 8) | (interface & 0xff);
  100. }
  101. /**
  102. * Free the pip packet buffers contained in a work queue entry.
  103. * The work queue entry is not freed.
  104. *
  105. * @param work Work queue entry with packet to free
  106. */
  107. static inline void cvmx_helper_free_pip_pkt_data(cvmx_wqe_t *work)
  108. {
  109. u64 number_buffers;
  110. cvmx_buf_ptr_t buffer_ptr;
  111. cvmx_buf_ptr_t next_buffer_ptr;
  112. u64 start_of_buffer;
  113. number_buffers = work->word2.s.bufs;
  114. if (number_buffers == 0)
  115. return;
  116. buffer_ptr = work->packet_ptr;
  117. /* Since the number of buffers is not zero, we know this is not a dynamic
  118. short packet. We need to check if it is a packet received with
  119. IPD_CTL_STATUS[NO_WPTR]. If this is true, we need to free all buffers
  120. except for the first one. The caller doesn't expect their WQE pointer
  121. to be freed */
  122. start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
  123. if (cvmx_ptr_to_phys(work) == start_of_buffer) {
  124. next_buffer_ptr = *(cvmx_buf_ptr_t *)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
  125. buffer_ptr = next_buffer_ptr;
  126. number_buffers--;
  127. }
  128. while (number_buffers--) {
  129. /* Remember the back pointer is in cache lines, not 64bit words */
  130. start_of_buffer = ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
  131. /* Read pointer to next buffer before we free the current buffer. */
  132. next_buffer_ptr = *(cvmx_buf_ptr_t *)cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
  133. cvmx_fpa_free(cvmx_phys_to_ptr(start_of_buffer), buffer_ptr.s.pool, 0);
  134. buffer_ptr = next_buffer_ptr;
  135. }
  136. }
  137. /**
  138. * Free the pki packet buffers contained in a work queue entry.
  139. * If first packet buffer contains wqe, wqe gets freed too so do not access
  140. * wqe after calling this function.
  141. * This function asssumes that buffers to be freed are from
  142. * Naturally aligned pool/aura.
  143. * It does not use don't write back.
  144. * @param work Work queue entry with packet to free
  145. */
  146. static inline void cvmx_helper_free_pki_pkt_data(cvmx_wqe_t *work)
  147. {
  148. u64 number_buffers;
  149. u64 start_of_buffer;
  150. cvmx_buf_ptr_pki_t next_buffer_ptr;
  151. cvmx_buf_ptr_pki_t buffer_ptr;
  152. cvmx_wqe_78xx_t *wqe = (cvmx_wqe_78xx_t *)work;
  153. if (!octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
  154. return;
  155. }
  156. /* Make sure errata pki-20776 has been applied*/
  157. cvmx_wqe_pki_errata_20776(work);
  158. buffer_ptr = wqe->packet_ptr;
  159. number_buffers = cvmx_wqe_get_bufs(work);
  160. while (number_buffers--) {
  161. /* FIXME: change WQE function prototype */
  162. unsigned int x = cvmx_wqe_get_aura(work);
  163. cvmx_fpa3_gaura_t aura = __cvmx_fpa3_gaura(x >> 10, x & 0x3ff);
  164. /* XXX- assumes the buffer is cache-line aligned and naturally aligned mode*/
  165. start_of_buffer = (buffer_ptr.addr >> 7) << 7;
  166. /* Read pointer to next buffer before we free the current buffer. */
  167. next_buffer_ptr = *(cvmx_buf_ptr_pki_t *)cvmx_phys_to_ptr(buffer_ptr.addr - 8);
  168. /* FPA AURA comes from WQE, includes node */
  169. cvmx_fpa3_free(cvmx_phys_to_ptr(start_of_buffer), aura, 0);
  170. buffer_ptr = next_buffer_ptr;
  171. }
  172. }
  173. /**
  174. * Free the pki wqe entry buffer.
  175. * If wqe buffers contains first packet buffer, wqe does not get freed here.
  176. * This function asssumes that buffers to be freed are from
  177. * Naturally aligned pool/aura.
  178. * It does not use don't write back.
  179. * @param work Work queue entry to free
  180. */
  181. static inline void cvmx_wqe_pki_free(cvmx_wqe_t *work)
  182. {
  183. cvmx_wqe_78xx_t *wqe = (cvmx_wqe_78xx_t *)work;
  184. unsigned int x;
  185. cvmx_fpa3_gaura_t aura;
  186. if (!octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
  187. return;
  188. }
  189. /* Do nothing if the first packet buffer shares WQE buffer */
  190. if (!wqe->packet_ptr.packet_outside_wqe)
  191. return;
  192. /* FIXME change WQE function prototype */
  193. x = cvmx_wqe_get_aura(work);
  194. aura = __cvmx_fpa3_gaura(x >> 10, x & 0x3ff);
  195. cvmx_fpa3_free(work, aura, 0);
  196. }
  197. /**
  198. * Convert a interface mode into a human readable string
  199. *
  200. * @param mode Mode to convert
  201. *
  202. * Return: String
  203. */
  204. const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode);
  205. /**
  206. * Debug routine to dump the packet structure to the console
  207. *
  208. * @param work Work queue entry containing the packet to dump
  209. * @return
  210. */
  211. int cvmx_helper_dump_packet(cvmx_wqe_t *work);
  212. /**
  213. * Get the version of the CVMX libraries.
  214. *
  215. * Return: Version string. Note this buffer is allocated statically
  216. * and will be shared by all callers.
  217. */
  218. const char *cvmx_helper_get_version(void);
  219. /**
  220. * @INTERNAL
  221. * Setup the common GMX settings that determine the number of
  222. * ports. These setting apply to almost all configurations of all
  223. * chips.
  224. *
  225. * @param xiface Interface to configure
  226. * @param num_ports Number of ports on the interface
  227. *
  228. * Return: Zero on success, negative on failure
  229. */
  230. int __cvmx_helper_setup_gmx(int xiface, int num_ports);
  231. /**
  232. * @INTERNAL
  233. * Get the number of pko_ports on an interface.
  234. *
  235. * @param interface
  236. *
  237. * Return: the number of pko_ports on the interface.
  238. */
  239. int __cvmx_helper_get_num_pko_ports(int interface);
  240. /**
  241. * Returns the IPD port number for a port on the given
  242. * interface.
  243. *
  244. * @param interface Interface to use
  245. * @param port Port on the interface
  246. *
  247. * Return: IPD port number
  248. */
  249. int cvmx_helper_get_ipd_port(int interface, int port);
  250. /**
  251. * Returns the PKO port number for a port on the given interface,
  252. * This is the base pko_port for o68 and ipd_port for older models.
  253. *
  254. * @param interface Interface to use
  255. * @param port Port on the interface
  256. *
  257. * Return: PKO port number and -1 on error.
  258. */
  259. int cvmx_helper_get_pko_port(int interface, int port);
  260. /**
  261. * Returns the IPD/PKO port number for the first port on the given
  262. * interface.
  263. *
  264. * @param interface Interface to use
  265. *
  266. * Return: IPD/PKO port number
  267. */
  268. static inline int cvmx_helper_get_first_ipd_port(int interface)
  269. {
  270. return cvmx_helper_get_ipd_port(interface, 0);
  271. }
  272. int cvmx_helper_ports_on_interface(int interface);
  273. /**
  274. * Returns the IPD/PKO port number for the last port on the given
  275. * interface.
  276. *
  277. * @param interface Interface to use
  278. *
  279. * Return: IPD/PKO port number
  280. *
  281. * Note: for o68, the last ipd port on an interface does not always equal to
  282. * the first plus the number of ports as the ipd ports are not contiguous in
  283. * some cases, e.g., SGMII.
  284. *
  285. * Note: code that makes the assumption of contiguous ipd port numbers needs to
  286. * be aware of this.
  287. */
  288. static inline int cvmx_helper_get_last_ipd_port(int interface)
  289. {
  290. return cvmx_helper_get_ipd_port(interface, cvmx_helper_ports_on_interface(interface) - 1);
  291. }
  292. /**
  293. * Free the packet buffers contained in a work queue entry.
  294. * The work queue entry is not freed.
  295. * Note that this function will not free the work queue entry
  296. * even if it contains a non-redundant data packet, and hence
  297. * it is not really comparable to how the PKO would free a packet
  298. * buffers if requested.
  299. *
  300. * @param work Work queue entry with packet to free
  301. */
  302. void cvmx_helper_free_packet_data(cvmx_wqe_t *work);
  303. /**
  304. * Returns the interface number for an IPD/PKO port number.
  305. *
  306. * @param ipd_port IPD/PKO port number
  307. *
  308. * Return: Interface number
  309. */
  310. int cvmx_helper_get_interface_num(int ipd_port);
  311. /**
  312. * Returns the interface index number for an IPD/PKO port
  313. * number.
  314. *
  315. * @param ipd_port IPD/PKO port number
  316. *
  317. * Return: Interface index number
  318. */
  319. int cvmx_helper_get_interface_index_num(int ipd_port);
  320. /**
  321. * Get port kind for a given port in an interface.
  322. *
  323. * @param xiface Interface
  324. * @param index index of the port in the interface
  325. *
  326. * Return: port kind on sucicess and -1 on failure
  327. */
  328. int cvmx_helper_get_pknd(int xiface, int index);
  329. /**
  330. * Get bpid for a given port in an interface.
  331. *
  332. * @param interface Interface
  333. * @param port index of the port in the interface
  334. *
  335. * Return: port kind on sucicess and -1 on failure
  336. */
  337. int cvmx_helper_get_bpid(int interface, int port);
  338. /**
  339. * Internal functions.
  340. */
  341. int __cvmx_helper_post_init_interfaces(void);
  342. int cvmx_helper_setup_red(int pass_thresh, int drop_thresh);
  343. void cvmx_helper_show_stats(int port);
  344. /*
  345. * Return number of array alements
  346. */
  347. #define NUM_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
  348. /**
  349. * Prints out a buffer with the address, hex bytes, and ASCII
  350. *
  351. * @param addr Start address to print on the left
  352. * @param[in] buffer array of bytes to print
  353. * @param count Number of bytes to print
  354. */
  355. void cvmx_print_buffer_u8(unsigned int addr, const u8 *buffer, size_t count);
  356. #endif /* __CVMX_HELPER_H__ */