cvmx-helper-util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <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. * 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 incoming 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 incoming 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. EXPORT_SYMBOL_GPL(cvmx_helper_setup_red);
  233. /**
  234. * Setup the common GMX settings that determine the number of
  235. * ports. These setting apply to almost all configurations of all
  236. * chips.
  237. *
  238. * @interface: Interface to configure
  239. * @num_ports: Number of ports on the interface
  240. *
  241. * Returns Zero on success, negative on failure
  242. */
  243. int __cvmx_helper_setup_gmx(int interface, int num_ports)
  244. {
  245. union cvmx_gmxx_tx_prts gmx_tx_prts;
  246. union cvmx_gmxx_rx_prts gmx_rx_prts;
  247. union cvmx_pko_reg_gmx_port_mode pko_mode;
  248. union cvmx_gmxx_txx_thresh gmx_tx_thresh;
  249. int index;
  250. /* Tell GMX the number of TX ports on this interface */
  251. gmx_tx_prts.u64 = cvmx_read_csr(CVMX_GMXX_TX_PRTS(interface));
  252. gmx_tx_prts.s.prts = num_ports;
  253. cvmx_write_csr(CVMX_GMXX_TX_PRTS(interface), gmx_tx_prts.u64);
  254. /* Tell GMX the number of RX ports on this interface. This only
  255. ** applies to *GMII and XAUI ports */
  256. if (cvmx_helper_interface_get_mode(interface) ==
  257. CVMX_HELPER_INTERFACE_MODE_RGMII
  258. || cvmx_helper_interface_get_mode(interface) ==
  259. CVMX_HELPER_INTERFACE_MODE_SGMII
  260. || cvmx_helper_interface_get_mode(interface) ==
  261. CVMX_HELPER_INTERFACE_MODE_GMII
  262. || cvmx_helper_interface_get_mode(interface) ==
  263. CVMX_HELPER_INTERFACE_MODE_XAUI) {
  264. if (num_ports > 4) {
  265. cvmx_dprintf("__cvmx_helper_setup_gmx: Illegal "
  266. "num_ports\n");
  267. return -1;
  268. }
  269. gmx_rx_prts.u64 = cvmx_read_csr(CVMX_GMXX_RX_PRTS(interface));
  270. gmx_rx_prts.s.prts = num_ports;
  271. cvmx_write_csr(CVMX_GMXX_RX_PRTS(interface), gmx_rx_prts.u64);
  272. }
  273. /* Skip setting CVMX_PKO_REG_GMX_PORT_MODE on 30XX, 31XX, and 50XX */
  274. if (!OCTEON_IS_MODEL(OCTEON_CN30XX) && !OCTEON_IS_MODEL(OCTEON_CN31XX)
  275. && !OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  276. /* Tell PKO the number of ports on this interface */
  277. pko_mode.u64 = cvmx_read_csr(CVMX_PKO_REG_GMX_PORT_MODE);
  278. if (interface == 0) {
  279. if (num_ports == 1)
  280. pko_mode.s.mode0 = 4;
  281. else if (num_ports == 2)
  282. pko_mode.s.mode0 = 3;
  283. else if (num_ports <= 4)
  284. pko_mode.s.mode0 = 2;
  285. else if (num_ports <= 8)
  286. pko_mode.s.mode0 = 1;
  287. else
  288. pko_mode.s.mode0 = 0;
  289. } else {
  290. if (num_ports == 1)
  291. pko_mode.s.mode1 = 4;
  292. else if (num_ports == 2)
  293. pko_mode.s.mode1 = 3;
  294. else if (num_ports <= 4)
  295. pko_mode.s.mode1 = 2;
  296. else if (num_ports <= 8)
  297. pko_mode.s.mode1 = 1;
  298. else
  299. pko_mode.s.mode1 = 0;
  300. }
  301. cvmx_write_csr(CVMX_PKO_REG_GMX_PORT_MODE, pko_mode.u64);
  302. }
  303. /*
  304. * Set GMX to buffer as much data as possible before starting
  305. * transmit. This reduces the chances that we have a TX under
  306. * run due to memory contention. Any packet that fits entirely
  307. * in the GMX FIFO can never have an under run regardless of
  308. * memory load.
  309. */
  310. gmx_tx_thresh.u64 = cvmx_read_csr(CVMX_GMXX_TXX_THRESH(0, interface));
  311. if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN31XX)
  312. || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  313. /* These chips have a fixed max threshold of 0x40 */
  314. gmx_tx_thresh.s.cnt = 0x40;
  315. } else {
  316. /* Choose the max value for the number of ports */
  317. if (num_ports <= 1)
  318. gmx_tx_thresh.s.cnt = 0x100 / 1;
  319. else if (num_ports == 2)
  320. gmx_tx_thresh.s.cnt = 0x100 / 2;
  321. else
  322. gmx_tx_thresh.s.cnt = 0x100 / 4;
  323. }
  324. /*
  325. * SPI and XAUI can have lots of ports but the GMX hardware
  326. * only ever has a max of 4.
  327. */
  328. if (num_ports > 4)
  329. num_ports = 4;
  330. for (index = 0; index < num_ports; index++)
  331. cvmx_write_csr(CVMX_GMXX_TXX_THRESH(index, interface),
  332. gmx_tx_thresh.u64);
  333. return 0;
  334. }
  335. /**
  336. * Returns the IPD/PKO port number for a port on the given
  337. * interface.
  338. *
  339. * @interface: Interface to use
  340. * @port: Port on the interface
  341. *
  342. * Returns IPD/PKO port number
  343. */
  344. int cvmx_helper_get_ipd_port(int interface, int port)
  345. {
  346. switch (interface) {
  347. case 0:
  348. return port;
  349. case 1:
  350. return port + 16;
  351. case 2:
  352. return port + 32;
  353. case 3:
  354. return port + 36;
  355. }
  356. return -1;
  357. }
  358. EXPORT_SYMBOL_GPL(cvmx_helper_get_ipd_port);
  359. /**
  360. * Returns the interface number for an IPD/PKO port number.
  361. *
  362. * @ipd_port: IPD/PKO port number
  363. *
  364. * Returns Interface number
  365. */
  366. int cvmx_helper_get_interface_num(int ipd_port)
  367. {
  368. if (ipd_port < 16)
  369. return 0;
  370. else if (ipd_port < 32)
  371. return 1;
  372. else if (ipd_port < 36)
  373. return 2;
  374. else if (ipd_port < 40)
  375. return 3;
  376. else
  377. cvmx_dprintf("cvmx_helper_get_interface_num: Illegal IPD "
  378. "port number\n");
  379. return -1;
  380. }
  381. EXPORT_SYMBOL_GPL(cvmx_helper_get_interface_num);
  382. /**
  383. * Returns the interface index number for an IPD/PKO port
  384. * number.
  385. *
  386. * @ipd_port: IPD/PKO port number
  387. *
  388. * Returns Interface index number
  389. */
  390. int cvmx_helper_get_interface_index_num(int ipd_port)
  391. {
  392. if (ipd_port < 32)
  393. return ipd_port & 15;
  394. else if (ipd_port < 36)
  395. return ipd_port & 3;
  396. else if (ipd_port < 40)
  397. return ipd_port & 3;
  398. else
  399. cvmx_dprintf("cvmx_helper_get_interface_index_num: "
  400. "Illegal IPD port number\n");
  401. return -1;
  402. }
  403. EXPORT_SYMBOL_GPL(cvmx_helper_get_interface_index_num);