cvmx-helper-util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  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 "cvmx-config.h"
  33. #include "cvmx-fpa.h"
  34. #include "cvmx-pip.h"
  35. #include "cvmx-pko.h"
  36. #include "cvmx-ipd.h"
  37. #include "cvmx-spi.h"
  38. #include "cvmx-helper.h"
  39. #include "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. * Debug routine to dump the packet structure to the console
  77. *
  78. * @work: Work queue entry containing the packet to dump
  79. * Returns
  80. */
  81. int cvmx_helper_dump_packet(cvmx_wqe_t *work)
  82. {
  83. uint64_t count;
  84. uint64_t remaining_bytes;
  85. union cvmx_buf_ptr buffer_ptr;
  86. uint64_t start_of_buffer;
  87. uint8_t *data_address;
  88. uint8_t *end_of_data;
  89. cvmx_dprintf("Packet Length: %u\n", work->len);
  90. cvmx_dprintf(" Input Port: %u\n", work->ipprt);
  91. cvmx_dprintf(" QoS: %u\n", work->qos);
  92. cvmx_dprintf(" Buffers: %u\n", work->word2.s.bufs);
  93. if (work->word2.s.bufs == 0) {
  94. union cvmx_ipd_wqe_fpa_queue wqe_pool;
  95. wqe_pool.u64 = cvmx_read_csr(CVMX_IPD_WQE_FPA_QUEUE);
  96. buffer_ptr.u64 = 0;
  97. buffer_ptr.s.pool = wqe_pool.s.wqe_pool;
  98. buffer_ptr.s.size = 128;
  99. buffer_ptr.s.addr = cvmx_ptr_to_phys(work->packet_data);
  100. if (likely(!work->word2.s.not_IP)) {
  101. union cvmx_pip_ip_offset pip_ip_offset;
  102. pip_ip_offset.u64 = cvmx_read_csr(CVMX_PIP_IP_OFFSET);
  103. buffer_ptr.s.addr +=
  104. (pip_ip_offset.s.offset << 3) -
  105. work->word2.s.ip_offset;
  106. buffer_ptr.s.addr += (work->word2.s.is_v6 ^ 1) << 2;
  107. } else {
  108. /*
  109. * WARNING: This code assumes that the packet
  110. * is not RAW. If it was, we would use
  111. * PIP_GBL_CFG[RAW_SHF] instead of
  112. * PIP_GBL_CFG[NIP_SHF].
  113. */
  114. union cvmx_pip_gbl_cfg pip_gbl_cfg;
  115. pip_gbl_cfg.u64 = cvmx_read_csr(CVMX_PIP_GBL_CFG);
  116. buffer_ptr.s.addr += pip_gbl_cfg.s.nip_shf;
  117. }
  118. } else
  119. buffer_ptr = work->packet_ptr;
  120. remaining_bytes = work->len;
  121. while (remaining_bytes) {
  122. start_of_buffer =
  123. ((buffer_ptr.s.addr >> 7) - buffer_ptr.s.back) << 7;
  124. cvmx_dprintf(" Buffer Start:%llx\n",
  125. (unsigned long long)start_of_buffer);
  126. cvmx_dprintf(" Buffer I : %u\n", buffer_ptr.s.i);
  127. cvmx_dprintf(" Buffer Back: %u\n", buffer_ptr.s.back);
  128. cvmx_dprintf(" Buffer Pool: %u\n", buffer_ptr.s.pool);
  129. cvmx_dprintf(" Buffer Data: %llx\n",
  130. (unsigned long long)buffer_ptr.s.addr);
  131. cvmx_dprintf(" Buffer Size: %u\n", buffer_ptr.s.size);
  132. cvmx_dprintf("\t\t");
  133. data_address = (uint8_t *) cvmx_phys_to_ptr(buffer_ptr.s.addr);
  134. end_of_data = data_address + buffer_ptr.s.size;
  135. count = 0;
  136. while (data_address < end_of_data) {
  137. if (remaining_bytes == 0)
  138. break;
  139. else
  140. remaining_bytes--;
  141. cvmx_dprintf("%02x", (unsigned int)*data_address);
  142. data_address++;
  143. if (remaining_bytes && (count == 7)) {
  144. cvmx_dprintf("\n\t\t");
  145. count = 0;
  146. } else
  147. count++;
  148. }
  149. cvmx_dprintf("\n");
  150. if (remaining_bytes)
  151. buffer_ptr = *(union cvmx_buf_ptr *)
  152. cvmx_phys_to_ptr(buffer_ptr.s.addr - 8);
  153. }
  154. return 0;
  155. }
  156. /**
  157. * Setup Random Early Drop on a specific input queue
  158. *
  159. * @queue: Input queue to setup RED on (0-7)
  160. * @pass_thresh:
  161. * Packets will begin slowly dropping when there are less than
  162. * this many packet buffers free in FPA 0.
  163. * @drop_thresh:
  164. * All incomming packets will be dropped when there are less
  165. * than this many free packet buffers in FPA 0.
  166. * Returns Zero on success. Negative on failure
  167. */
  168. int cvmx_helper_setup_red_queue(int queue, int pass_thresh, int drop_thresh)
  169. {
  170. union cvmx_ipd_qosx_red_marks red_marks;
  171. union cvmx_ipd_red_quex_param red_param;
  172. /* Set RED to begin dropping packets when there are pass_thresh buffers
  173. left. It will linearly drop more packets until reaching drop_thresh
  174. buffers */
  175. red_marks.u64 = 0;
  176. red_marks.s.drop = drop_thresh;
  177. red_marks.s.pass = pass_thresh;
  178. cvmx_write_csr(CVMX_IPD_QOSX_RED_MARKS(queue), red_marks.u64);
  179. /* Use the actual queue 0 counter, not the average */
  180. red_param.u64 = 0;
  181. red_param.s.prb_con =
  182. (255ul << 24) / (red_marks.s.pass - red_marks.s.drop);
  183. red_param.s.avg_con = 1;
  184. red_param.s.new_con = 255;
  185. red_param.s.use_pcnt = 1;
  186. cvmx_write_csr(CVMX_IPD_RED_QUEX_PARAM(queue), red_param.u64);
  187. return 0;
  188. }
  189. /**
  190. * Setup Random Early Drop to automatically begin dropping packets.
  191. *
  192. * @pass_thresh:
  193. * Packets will begin slowly dropping when there are less than
  194. * this many packet buffers free in FPA 0.
  195. * @drop_thresh:
  196. * All incomming packets will be dropped when there are less
  197. * than this many free packet buffers in FPA 0.
  198. * Returns Zero on success. Negative on failure
  199. */
  200. int cvmx_helper_setup_red(int pass_thresh, int drop_thresh)
  201. {
  202. union cvmx_ipd_portx_bp_page_cnt page_cnt;
  203. union cvmx_ipd_bp_prt_red_end ipd_bp_prt_red_end;
  204. union cvmx_ipd_red_port_enable red_port_enable;
  205. int queue;
  206. int interface;
  207. int port;
  208. /* Disable backpressure based on queued buffers. It needs SW support */
  209. page_cnt.u64 = 0;
  210. page_cnt.s.bp_enb = 0;
  211. page_cnt.s.page_cnt = 100;
  212. for (interface = 0; interface < 2; interface++) {
  213. for (port = cvmx_helper_get_first_ipd_port(interface);
  214. port < cvmx_helper_get_last_ipd_port(interface); port++)
  215. cvmx_write_csr(CVMX_IPD_PORTX_BP_PAGE_CNT(port),
  216. page_cnt.u64);
  217. }
  218. for (queue = 0; queue < 8; queue++)
  219. cvmx_helper_setup_red_queue(queue, pass_thresh, drop_thresh);
  220. /* Shutoff the dropping based on the per port page count. SW isn't
  221. decrementing it right now */
  222. ipd_bp_prt_red_end.u64 = 0;
  223. ipd_bp_prt_red_end.s.prt_enb = 0;
  224. cvmx_write_csr(CVMX_IPD_BP_PRT_RED_END, ipd_bp_prt_red_end.u64);
  225. red_port_enable.u64 = 0;
  226. red_port_enable.s.prt_enb = 0xfffffffffull;
  227. red_port_enable.s.avg_dly = 10000;
  228. red_port_enable.s.prb_dly = 10000;
  229. cvmx_write_csr(CVMX_IPD_RED_PORT_ENABLE, red_port_enable.u64);
  230. return 0;
  231. }
  232. /**
  233. * Setup the common GMX settings that determine the number of
  234. * ports. These setting apply to almost all configurations of all
  235. * chips.
  236. *
  237. * @interface: Interface to configure
  238. * @num_ports: Number of ports on the interface
  239. *
  240. * Returns Zero on success, negative on failure
  241. */
  242. int __cvmx_helper_setup_gmx(int interface, int num_ports)
  243. {
  244. union cvmx_gmxx_tx_prts gmx_tx_prts;
  245. union cvmx_gmxx_rx_prts gmx_rx_prts;
  246. union cvmx_pko_reg_gmx_port_mode pko_mode;
  247. union cvmx_gmxx_txx_thresh gmx_tx_thresh;
  248. int index;
  249. /* Tell GMX the number of TX ports on this interface */
  250. gmx_tx_prts.u64 = cvmx_read_csr(CVMX_GMXX_TX_PRTS(interface));
  251. gmx_tx_prts.s.prts = num_ports;
  252. cvmx_write_csr(CVMX_GMXX_TX_PRTS(interface), gmx_tx_prts.u64);
  253. /* Tell GMX the number of RX ports on this interface. This only
  254. ** applies to *GMII and XAUI ports */
  255. if (cvmx_helper_interface_get_mode(interface) ==
  256. CVMX_HELPER_INTERFACE_MODE_RGMII
  257. || cvmx_helper_interface_get_mode(interface) ==
  258. CVMX_HELPER_INTERFACE_MODE_SGMII
  259. || cvmx_helper_interface_get_mode(interface) ==
  260. CVMX_HELPER_INTERFACE_MODE_GMII
  261. || cvmx_helper_interface_get_mode(interface) ==
  262. CVMX_HELPER_INTERFACE_MODE_XAUI) {
  263. if (num_ports > 4) {
  264. cvmx_dprintf("__cvmx_helper_setup_gmx: Illegal "
  265. "num_ports\n");
  266. return -1;
  267. }
  268. gmx_rx_prts.u64 = cvmx_read_csr(CVMX_GMXX_RX_PRTS(interface));
  269. gmx_rx_prts.s.prts = num_ports;
  270. cvmx_write_csr(CVMX_GMXX_RX_PRTS(interface), gmx_rx_prts.u64);
  271. }
  272. /* Skip setting CVMX_PKO_REG_GMX_PORT_MODE on 30XX, 31XX, and 50XX */
  273. if (!OCTEON_IS_MODEL(OCTEON_CN30XX) && !OCTEON_IS_MODEL(OCTEON_CN31XX)
  274. && !OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  275. /* Tell PKO the number of ports on this interface */
  276. pko_mode.u64 = cvmx_read_csr(CVMX_PKO_REG_GMX_PORT_MODE);
  277. if (interface == 0) {
  278. if (num_ports == 1)
  279. pko_mode.s.mode0 = 4;
  280. else if (num_ports == 2)
  281. pko_mode.s.mode0 = 3;
  282. else if (num_ports <= 4)
  283. pko_mode.s.mode0 = 2;
  284. else if (num_ports <= 8)
  285. pko_mode.s.mode0 = 1;
  286. else
  287. pko_mode.s.mode0 = 0;
  288. } else {
  289. if (num_ports == 1)
  290. pko_mode.s.mode1 = 4;
  291. else if (num_ports == 2)
  292. pko_mode.s.mode1 = 3;
  293. else if (num_ports <= 4)
  294. pko_mode.s.mode1 = 2;
  295. else if (num_ports <= 8)
  296. pko_mode.s.mode1 = 1;
  297. else
  298. pko_mode.s.mode1 = 0;
  299. }
  300. cvmx_write_csr(CVMX_PKO_REG_GMX_PORT_MODE, pko_mode.u64);
  301. }
  302. /*
  303. * Set GMX to buffer as much data as possible before starting
  304. * transmit. This reduces the chances that we have a TX under
  305. * run due to memory contention. Any packet that fits entirely
  306. * in the GMX FIFO can never have an under run regardless of
  307. * memory load.
  308. */
  309. gmx_tx_thresh.u64 = cvmx_read_csr(CVMX_GMXX_TXX_THRESH(0, interface));
  310. if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN31XX)
  311. || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  312. /* These chips have a fixed max threshold of 0x40 */
  313. gmx_tx_thresh.s.cnt = 0x40;
  314. } else {
  315. /* Choose the max value for the number of ports */
  316. if (num_ports <= 1)
  317. gmx_tx_thresh.s.cnt = 0x100 / 1;
  318. else if (num_ports == 2)
  319. gmx_tx_thresh.s.cnt = 0x100 / 2;
  320. else
  321. gmx_tx_thresh.s.cnt = 0x100 / 4;
  322. }
  323. /*
  324. * SPI and XAUI can have lots of ports but the GMX hardware
  325. * only ever has a max of 4.
  326. */
  327. if (num_ports > 4)
  328. num_ports = 4;
  329. for (index = 0; index < num_ports; index++)
  330. cvmx_write_csr(CVMX_GMXX_TXX_THRESH(index, interface),
  331. gmx_tx_thresh.u64);
  332. return 0;
  333. }
  334. /**
  335. * Returns the IPD/PKO port number for a port on the given
  336. * interface.
  337. *
  338. * @interface: Interface to use
  339. * @port: Port on the interface
  340. *
  341. * Returns IPD/PKO port number
  342. */
  343. int cvmx_helper_get_ipd_port(int interface, int port)
  344. {
  345. switch (interface) {
  346. case 0:
  347. return port;
  348. case 1:
  349. return port + 16;
  350. case 2:
  351. return port + 32;
  352. case 3:
  353. return port + 36;
  354. }
  355. return -1;
  356. }
  357. /**
  358. * Returns the interface number for an IPD/PKO port number.
  359. *
  360. * @ipd_port: IPD/PKO port number
  361. *
  362. * Returns Interface number
  363. */
  364. int cvmx_helper_get_interface_num(int ipd_port)
  365. {
  366. if (ipd_port < 16)
  367. return 0;
  368. else if (ipd_port < 32)
  369. return 1;
  370. else if (ipd_port < 36)
  371. return 2;
  372. else if (ipd_port < 40)
  373. return 3;
  374. else
  375. cvmx_dprintf("cvmx_helper_get_interface_num: Illegal IPD "
  376. "port number\n");
  377. return -1;
  378. }
  379. /**
  380. * Returns the interface index number for an IPD/PKO port
  381. * number.
  382. *
  383. * @ipd_port: IPD/PKO port number
  384. *
  385. * Returns Interface index number
  386. */
  387. int cvmx_helper_get_interface_index_num(int ipd_port)
  388. {
  389. if (ipd_port < 32)
  390. return ipd_port & 15;
  391. else if (ipd_port < 36)
  392. return ipd_port & 3;
  393. else if (ipd_port < 40)
  394. return ipd_port & 3;
  395. else
  396. cvmx_dprintf("cvmx_helper_get_interface_index_num: "
  397. "Illegal IPD port number\n");
  398. return -1;
  399. }