cvmx-helper-util.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: [email protected]
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (c) 2003-2008 Cavium Networks
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this file; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. * or visit http://www.gnu.org/licenses/.
  23. *
  24. * This file may also be available under a different license from Cavium.
  25. * Contact Cavium Networks for more information
  26. ***********************license end**************************************/
  27. /*
  28. * Small helper utilities.
  29. */
  30. #include <linux/kernel.h>
  31. #include <asm/octeon/octeon.h>
  32. #include <asm/octeon/cvmx-config.h>
  33. #include <asm/octeon/cvmx-fpa.h>
  34. #include <asm/octeon/cvmx-pip.h>
  35. #include <asm/octeon/cvmx-pko.h>
  36. #include <asm/octeon/cvmx-ipd.h>
  37. #include <asm/octeon/cvmx-spi.h>
  38. #include <asm/octeon/cvmx-helper.h>
  39. #include <asm/octeon/cvmx-helper-util.h>
  40. #include <asm/octeon/cvmx-ipd-defs.h>
  41. /**
  42. * Convert a interface mode into a human readable string
  43. *
  44. * @mode: Mode to convert
  45. *
  46. * Returns String
  47. */
  48. const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t
  49. mode)
  50. {
  51. switch (mode) {
  52. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  53. return "DISABLED";
  54. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  55. return "RGMII";
  56. case CVMX_HELPER_INTERFACE_MODE_GMII:
  57. return "GMII";
  58. case CVMX_HELPER_INTERFACE_MODE_SPI:
  59. return "SPI";
  60. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  61. return "PCIE";
  62. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  63. return "XAUI";
  64. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  65. return "SGMII";
  66. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  67. return "PICMG";
  68. case CVMX_HELPER_INTERFACE_MODE_NPI:
  69. return "NPI";
  70. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  71. return "LOOP";
  72. }
  73. return "UNKNOWN";
  74. }
  75. /**
  76. * Setup Random Early Drop on a specific input queue
  77. *
  78. * @queue: Input queue to setup RED on (0-7)
  79. * @pass_thresh:
  80. * Packets will begin slowly dropping when there are less than
  81. * this many packet buffers free in FPA 0.
  82. * @drop_thresh:
  83. * All incoming packets will be dropped when there are less
  84. * than this many free packet buffers in FPA 0.
  85. * Returns Zero on success. Negative on failure
  86. */
  87. static int cvmx_helper_setup_red_queue(int queue, int pass_thresh,
  88. int drop_thresh)
  89. {
  90. union cvmx_ipd_qosx_red_marks red_marks;
  91. union cvmx_ipd_red_quex_param red_param;
  92. /* Set RED to begin dropping packets when there are pass_thresh buffers
  93. left. It will linearly drop more packets until reaching drop_thresh
  94. buffers */
  95. red_marks.u64 = 0;
  96. red_marks.s.drop = drop_thresh;
  97. red_marks.s.pass = pass_thresh;
  98. cvmx_write_csr(CVMX_IPD_QOSX_RED_MARKS(queue), red_marks.u64);
  99. /* Use the actual queue 0 counter, not the average */
  100. red_param.u64 = 0;
  101. red_param.s.prb_con =
  102. (255ul << 24) / (red_marks.s.pass - red_marks.s.drop);
  103. red_param.s.avg_con = 1;
  104. red_param.s.new_con = 255;
  105. red_param.s.use_pcnt = 1;
  106. cvmx_write_csr(CVMX_IPD_RED_QUEX_PARAM(queue), red_param.u64);
  107. return 0;
  108. }
  109. /**
  110. * Setup Random Early Drop to automatically begin dropping packets.
  111. *
  112. * @pass_thresh:
  113. * Packets will begin slowly dropping when there are less than
  114. * this many packet buffers free in FPA 0.
  115. * @drop_thresh:
  116. * All incoming packets will be dropped when there are less
  117. * than this many free packet buffers in FPA 0.
  118. * Returns Zero on success. Negative on failure
  119. */
  120. int cvmx_helper_setup_red(int pass_thresh, int drop_thresh)
  121. {
  122. union cvmx_ipd_portx_bp_page_cnt page_cnt;
  123. union cvmx_ipd_bp_prt_red_end ipd_bp_prt_red_end;
  124. union cvmx_ipd_red_port_enable red_port_enable;
  125. int queue;
  126. int interface;
  127. int port;
  128. /* Disable backpressure based on queued buffers. It needs SW support */
  129. page_cnt.u64 = 0;
  130. page_cnt.s.bp_enb = 0;
  131. page_cnt.s.page_cnt = 100;
  132. for (interface = 0; interface < 2; interface++) {
  133. for (port = cvmx_helper_get_first_ipd_port(interface);
  134. port < cvmx_helper_get_last_ipd_port(interface); port++)
  135. cvmx_write_csr(CVMX_IPD_PORTX_BP_PAGE_CNT(port),
  136. page_cnt.u64);
  137. }
  138. for (queue = 0; queue < 8; queue++)
  139. cvmx_helper_setup_red_queue(queue, pass_thresh, drop_thresh);
  140. /* Shutoff the dropping based on the per port page count. SW isn't
  141. decrementing it right now */
  142. ipd_bp_prt_red_end.u64 = 0;
  143. ipd_bp_prt_red_end.s.prt_enb = 0;
  144. cvmx_write_csr(CVMX_IPD_BP_PRT_RED_END, ipd_bp_prt_red_end.u64);
  145. red_port_enable.u64 = 0;
  146. red_port_enable.s.prt_enb = 0xfffffffffull;
  147. red_port_enable.s.avg_dly = 10000;
  148. red_port_enable.s.prb_dly = 10000;
  149. cvmx_write_csr(CVMX_IPD_RED_PORT_ENABLE, red_port_enable.u64);
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(cvmx_helper_setup_red);
  153. /**
  154. * Setup the common GMX settings that determine the number of
  155. * ports. These setting apply to almost all configurations of all
  156. * chips.
  157. *
  158. * @interface: Interface to configure
  159. * @num_ports: Number of ports on the interface
  160. *
  161. * Returns Zero on success, negative on failure
  162. */
  163. int __cvmx_helper_setup_gmx(int interface, int num_ports)
  164. {
  165. union cvmx_gmxx_tx_prts gmx_tx_prts;
  166. union cvmx_gmxx_rx_prts gmx_rx_prts;
  167. union cvmx_pko_reg_gmx_port_mode pko_mode;
  168. union cvmx_gmxx_txx_thresh gmx_tx_thresh;
  169. int index;
  170. /* Tell GMX the number of TX ports on this interface */
  171. gmx_tx_prts.u64 = cvmx_read_csr(CVMX_GMXX_TX_PRTS(interface));
  172. gmx_tx_prts.s.prts = num_ports;
  173. cvmx_write_csr(CVMX_GMXX_TX_PRTS(interface), gmx_tx_prts.u64);
  174. /* Tell GMX the number of RX ports on this interface. This only
  175. ** applies to *GMII and XAUI ports */
  176. if (cvmx_helper_interface_get_mode(interface) ==
  177. CVMX_HELPER_INTERFACE_MODE_RGMII
  178. || cvmx_helper_interface_get_mode(interface) ==
  179. CVMX_HELPER_INTERFACE_MODE_SGMII
  180. || cvmx_helper_interface_get_mode(interface) ==
  181. CVMX_HELPER_INTERFACE_MODE_GMII
  182. || cvmx_helper_interface_get_mode(interface) ==
  183. CVMX_HELPER_INTERFACE_MODE_XAUI) {
  184. if (num_ports > 4) {
  185. cvmx_dprintf("__cvmx_helper_setup_gmx: Illegal "
  186. "num_ports\n");
  187. return -1;
  188. }
  189. gmx_rx_prts.u64 = cvmx_read_csr(CVMX_GMXX_RX_PRTS(interface));
  190. gmx_rx_prts.s.prts = num_ports;
  191. cvmx_write_csr(CVMX_GMXX_RX_PRTS(interface), gmx_rx_prts.u64);
  192. }
  193. /* Skip setting CVMX_PKO_REG_GMX_PORT_MODE on 30XX, 31XX, and 50XX */
  194. if (!OCTEON_IS_MODEL(OCTEON_CN30XX) && !OCTEON_IS_MODEL(OCTEON_CN31XX)
  195. && !OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  196. /* Tell PKO the number of ports on this interface */
  197. pko_mode.u64 = cvmx_read_csr(CVMX_PKO_REG_GMX_PORT_MODE);
  198. if (interface == 0) {
  199. if (num_ports == 1)
  200. pko_mode.s.mode0 = 4;
  201. else if (num_ports == 2)
  202. pko_mode.s.mode0 = 3;
  203. else if (num_ports <= 4)
  204. pko_mode.s.mode0 = 2;
  205. else if (num_ports <= 8)
  206. pko_mode.s.mode0 = 1;
  207. else
  208. pko_mode.s.mode0 = 0;
  209. } else {
  210. if (num_ports == 1)
  211. pko_mode.s.mode1 = 4;
  212. else if (num_ports == 2)
  213. pko_mode.s.mode1 = 3;
  214. else if (num_ports <= 4)
  215. pko_mode.s.mode1 = 2;
  216. else if (num_ports <= 8)
  217. pko_mode.s.mode1 = 1;
  218. else
  219. pko_mode.s.mode1 = 0;
  220. }
  221. cvmx_write_csr(CVMX_PKO_REG_GMX_PORT_MODE, pko_mode.u64);
  222. }
  223. /*
  224. * Set GMX to buffer as much data as possible before starting
  225. * transmit. This reduces the chances that we have a TX under
  226. * run due to memory contention. Any packet that fits entirely
  227. * in the GMX FIFO can never have an under run regardless of
  228. * memory load.
  229. */
  230. gmx_tx_thresh.u64 = cvmx_read_csr(CVMX_GMXX_TXX_THRESH(0, interface));
  231. if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN31XX)
  232. || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  233. /* These chips have a fixed max threshold of 0x40 */
  234. gmx_tx_thresh.s.cnt = 0x40;
  235. } else {
  236. /* Choose the max value for the number of ports */
  237. if (num_ports <= 1)
  238. gmx_tx_thresh.s.cnt = 0x100 / 1;
  239. else if (num_ports == 2)
  240. gmx_tx_thresh.s.cnt = 0x100 / 2;
  241. else
  242. gmx_tx_thresh.s.cnt = 0x100 / 4;
  243. }
  244. /*
  245. * SPI and XAUI can have lots of ports but the GMX hardware
  246. * only ever has a max of 4.
  247. */
  248. if (num_ports > 4)
  249. num_ports = 4;
  250. for (index = 0; index < num_ports; index++)
  251. cvmx_write_csr(CVMX_GMXX_TXX_THRESH(index, interface),
  252. gmx_tx_thresh.u64);
  253. return 0;
  254. }
  255. /**
  256. * Returns the IPD/PKO port number for a port on the given
  257. * interface.
  258. *
  259. * @interface: Interface to use
  260. * @port: Port on the interface
  261. *
  262. * Returns IPD/PKO port number
  263. */
  264. int cvmx_helper_get_ipd_port(int interface, int port)
  265. {
  266. switch (interface) {
  267. case 0:
  268. return port;
  269. case 1:
  270. return port + 16;
  271. case 2:
  272. return port + 32;
  273. case 3:
  274. return port + 36;
  275. case 4:
  276. return port + 40;
  277. case 5:
  278. return port + 44;
  279. }
  280. return -1;
  281. }
  282. EXPORT_SYMBOL_GPL(cvmx_helper_get_ipd_port);
  283. /**
  284. * Returns the interface number for an IPD/PKO port number.
  285. *
  286. * @ipd_port: IPD/PKO port number
  287. *
  288. * Returns Interface number
  289. */
  290. int cvmx_helper_get_interface_num(int ipd_port)
  291. {
  292. if (ipd_port < 16)
  293. return 0;
  294. else if (ipd_port < 32)
  295. return 1;
  296. else if (ipd_port < 36)
  297. return 2;
  298. else if (ipd_port < 40)
  299. return 3;
  300. else if (ipd_port < 44)
  301. return 4;
  302. else if (ipd_port < 48)
  303. return 5;
  304. else
  305. cvmx_dprintf("cvmx_helper_get_interface_num: Illegal IPD "
  306. "port number\n");
  307. return -1;
  308. }
  309. EXPORT_SYMBOL_GPL(cvmx_helper_get_interface_num);
  310. /**
  311. * Returns the interface index number for an IPD/PKO port
  312. * number.
  313. *
  314. * @ipd_port: IPD/PKO port number
  315. *
  316. * Returns Interface index number
  317. */
  318. int cvmx_helper_get_interface_index_num(int ipd_port)
  319. {
  320. if (ipd_port < 32)
  321. return ipd_port & 15;
  322. else if (ipd_port < 36)
  323. return ipd_port & 3;
  324. else if (ipd_port < 40)
  325. return ipd_port & 3;
  326. else if (ipd_port < 44)
  327. return ipd_port & 3;
  328. else if (ipd_port < 48)
  329. return ipd_port & 3;
  330. else
  331. cvmx_dprintf("cvmx_helper_get_interface_index_num: "
  332. "Illegal IPD port number\n");
  333. return -1;
  334. }
  335. EXPORT_SYMBOL_GPL(cvmx_helper_get_interface_index_num);