cvmx-helper.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. *
  29. * Helper functions for common, but complicated tasks.
  30. *
  31. */
  32. #include <asm/octeon/octeon.h>
  33. #include "cvmx-config.h"
  34. #include "cvmx-fpa.h"
  35. #include "cvmx-pip.h"
  36. #include "cvmx-pko.h"
  37. #include "cvmx-ipd.h"
  38. #include "cvmx-spi.h"
  39. #include "cvmx-helper.h"
  40. #include "cvmx-helper-board.h"
  41. #include "cvmx-pip-defs.h"
  42. #include "cvmx-smix-defs.h"
  43. #include "cvmx-asxx-defs.h"
  44. /**
  45. * cvmx_override_pko_queue_priority(int ipd_port, uint64_t
  46. * priorities[16]) is a function pointer. It is meant to allow
  47. * customization of the PKO queue priorities based on the port
  48. * number. Users should set this pointer to a function before
  49. * calling any cvmx-helper operations.
  50. */
  51. void (*cvmx_override_pko_queue_priority) (int pko_port,
  52. uint64_t priorities[16]);
  53. /**
  54. * cvmx_override_ipd_port_setup(int ipd_port) is a function
  55. * pointer. It is meant to allow customization of the IPD port
  56. * setup before packet input/output comes online. It is called
  57. * after cvmx-helper does the default IPD configuration, but
  58. * before IPD is enabled. Users should set this pointer to a
  59. * function before calling any cvmx-helper operations.
  60. */
  61. void (*cvmx_override_ipd_port_setup) (int ipd_port);
  62. /* Port count per interface */
  63. static int interface_port_count[4] = { 0, 0, 0, 0 };
  64. /* Port last configured link info index by IPD/PKO port */
  65. static cvmx_helper_link_info_t
  66. port_link_info[CVMX_PIP_NUM_INPUT_PORTS];
  67. /**
  68. * Return the number of interfaces the chip has. Each interface
  69. * may have multiple ports. Most chips support two interfaces,
  70. * but the CNX0XX and CNX1XX are exceptions. These only support
  71. * one interface.
  72. *
  73. * Returns Number of interfaces on chip
  74. */
  75. int cvmx_helper_get_number_of_interfaces(void)
  76. {
  77. if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN52XX))
  78. return 4;
  79. else
  80. return 3;
  81. }
  82. /**
  83. * Return the number of ports on an interface. Depending on the
  84. * chip and configuration, this can be 1-16. A value of 0
  85. * specifies that the interface doesn't exist or isn't usable.
  86. *
  87. * @interface: Interface to get the port count for
  88. *
  89. * Returns Number of ports on interface. Can be Zero.
  90. */
  91. int cvmx_helper_ports_on_interface(int interface)
  92. {
  93. return interface_port_count[interface];
  94. }
  95. /**
  96. * Get the operating mode of an interface. Depending on the Octeon
  97. * chip and configuration, this function returns an enumeration
  98. * of the type of packet I/O supported by an interface.
  99. *
  100. * @interface: Interface to probe
  101. *
  102. * Returns Mode of the interface. Unknown or unsupported interfaces return
  103. * DISABLED.
  104. */
  105. cvmx_helper_interface_mode_t cvmx_helper_interface_get_mode(int interface)
  106. {
  107. union cvmx_gmxx_inf_mode mode;
  108. if (interface == 2)
  109. return CVMX_HELPER_INTERFACE_MODE_NPI;
  110. if (interface == 3) {
  111. if (OCTEON_IS_MODEL(OCTEON_CN56XX)
  112. || OCTEON_IS_MODEL(OCTEON_CN52XX))
  113. return CVMX_HELPER_INTERFACE_MODE_LOOP;
  114. else
  115. return CVMX_HELPER_INTERFACE_MODE_DISABLED;
  116. }
  117. if (interface == 0
  118. && cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_CN3005_EVB_HS5
  119. && cvmx_sysinfo_get()->board_rev_major == 1) {
  120. /*
  121. * Lie about interface type of CN3005 board. This
  122. * board has a switch on port 1 like the other
  123. * evaluation boards, but it is connected over RGMII
  124. * instead of GMII. Report GMII mode so that the
  125. * speed is forced to 1 Gbit full duplex. Other than
  126. * some initial configuration (which does not use the
  127. * output of this function) there is no difference in
  128. * setup between GMII and RGMII modes.
  129. */
  130. return CVMX_HELPER_INTERFACE_MODE_GMII;
  131. }
  132. /* Interface 1 is always disabled on CN31XX and CN30XX */
  133. if ((interface == 1)
  134. && (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN30XX)
  135. || OCTEON_IS_MODEL(OCTEON_CN50XX)
  136. || OCTEON_IS_MODEL(OCTEON_CN52XX)))
  137. return CVMX_HELPER_INTERFACE_MODE_DISABLED;
  138. mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
  139. if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  140. switch (mode.cn56xx.mode) {
  141. case 0:
  142. return CVMX_HELPER_INTERFACE_MODE_DISABLED;
  143. case 1:
  144. return CVMX_HELPER_INTERFACE_MODE_XAUI;
  145. case 2:
  146. return CVMX_HELPER_INTERFACE_MODE_SGMII;
  147. case 3:
  148. return CVMX_HELPER_INTERFACE_MODE_PICMG;
  149. default:
  150. return CVMX_HELPER_INTERFACE_MODE_DISABLED;
  151. }
  152. } else {
  153. if (!mode.s.en)
  154. return CVMX_HELPER_INTERFACE_MODE_DISABLED;
  155. if (mode.s.type) {
  156. if (OCTEON_IS_MODEL(OCTEON_CN38XX)
  157. || OCTEON_IS_MODEL(OCTEON_CN58XX))
  158. return CVMX_HELPER_INTERFACE_MODE_SPI;
  159. else
  160. return CVMX_HELPER_INTERFACE_MODE_GMII;
  161. } else
  162. return CVMX_HELPER_INTERFACE_MODE_RGMII;
  163. }
  164. }
  165. /**
  166. * Configure the IPD/PIP tagging and QoS options for a specific
  167. * port. This function determines the POW work queue entry
  168. * contents for a port. The setup performed here is controlled by
  169. * the defines in executive-config.h.
  170. *
  171. * @ipd_port: Port to configure. This follows the IPD numbering, not the
  172. * per interface numbering
  173. *
  174. * Returns Zero on success, negative on failure
  175. */
  176. static int __cvmx_helper_port_setup_ipd(int ipd_port)
  177. {
  178. union cvmx_pip_prt_cfgx port_config;
  179. union cvmx_pip_prt_tagx tag_config;
  180. port_config.u64 = cvmx_read_csr(CVMX_PIP_PRT_CFGX(ipd_port));
  181. tag_config.u64 = cvmx_read_csr(CVMX_PIP_PRT_TAGX(ipd_port));
  182. /* Have each port go to a different POW queue */
  183. port_config.s.qos = ipd_port & 0x7;
  184. /* Process the headers and place the IP header in the work queue */
  185. port_config.s.mode = CVMX_HELPER_INPUT_PORT_SKIP_MODE;
  186. tag_config.s.ip6_src_flag = CVMX_HELPER_INPUT_TAG_IPV6_SRC_IP;
  187. tag_config.s.ip6_dst_flag = CVMX_HELPER_INPUT_TAG_IPV6_DST_IP;
  188. tag_config.s.ip6_sprt_flag = CVMX_HELPER_INPUT_TAG_IPV6_SRC_PORT;
  189. tag_config.s.ip6_dprt_flag = CVMX_HELPER_INPUT_TAG_IPV6_DST_PORT;
  190. tag_config.s.ip6_nxth_flag = CVMX_HELPER_INPUT_TAG_IPV6_NEXT_HEADER;
  191. tag_config.s.ip4_src_flag = CVMX_HELPER_INPUT_TAG_IPV4_SRC_IP;
  192. tag_config.s.ip4_dst_flag = CVMX_HELPER_INPUT_TAG_IPV4_DST_IP;
  193. tag_config.s.ip4_sprt_flag = CVMX_HELPER_INPUT_TAG_IPV4_SRC_PORT;
  194. tag_config.s.ip4_dprt_flag = CVMX_HELPER_INPUT_TAG_IPV4_DST_PORT;
  195. tag_config.s.ip4_pctl_flag = CVMX_HELPER_INPUT_TAG_IPV4_PROTOCOL;
  196. tag_config.s.inc_prt_flag = CVMX_HELPER_INPUT_TAG_INPUT_PORT;
  197. tag_config.s.tcp6_tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
  198. tag_config.s.tcp4_tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
  199. tag_config.s.ip6_tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
  200. tag_config.s.ip4_tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
  201. tag_config.s.non_tag_type = CVMX_HELPER_INPUT_TAG_TYPE;
  202. /* Put all packets in group 0. Other groups can be used by the app */
  203. tag_config.s.grp = 0;
  204. cvmx_pip_config_port(ipd_port, port_config, tag_config);
  205. /* Give the user a chance to override our setting for each port */
  206. if (cvmx_override_ipd_port_setup)
  207. cvmx_override_ipd_port_setup(ipd_port);
  208. return 0;
  209. }
  210. /**
  211. * This function probes an interface to determine the actual
  212. * number of hardware ports connected to it. It doesn't setup the
  213. * ports or enable them. The main goal here is to set the global
  214. * interface_port_count[interface] correctly. Hardware setup of the
  215. * ports will be performed later.
  216. *
  217. * @interface: Interface to probe
  218. *
  219. * Returns Zero on success, negative on failure
  220. */
  221. int cvmx_helper_interface_probe(int interface)
  222. {
  223. /* At this stage in the game we don't want packets to be moving yet.
  224. The following probe calls should perform hardware setup
  225. needed to determine port counts. Receive must still be disabled */
  226. switch (cvmx_helper_interface_get_mode(interface)) {
  227. /* These types don't support ports to IPD/PKO */
  228. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  229. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  230. interface_port_count[interface] = 0;
  231. break;
  232. /* XAUI is a single high speed port */
  233. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  234. interface_port_count[interface] =
  235. __cvmx_helper_xaui_probe(interface);
  236. break;
  237. /*
  238. * RGMII/GMII/MII are all treated about the same. Most
  239. * functions refer to these ports as RGMII.
  240. */
  241. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  242. case CVMX_HELPER_INTERFACE_MODE_GMII:
  243. interface_port_count[interface] =
  244. __cvmx_helper_rgmii_probe(interface);
  245. break;
  246. /*
  247. * SPI4 can have 1-16 ports depending on the device at
  248. * the other end.
  249. */
  250. case CVMX_HELPER_INTERFACE_MODE_SPI:
  251. interface_port_count[interface] =
  252. __cvmx_helper_spi_probe(interface);
  253. break;
  254. /*
  255. * SGMII can have 1-4 ports depending on how many are
  256. * hooked up.
  257. */
  258. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  259. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  260. interface_port_count[interface] =
  261. __cvmx_helper_sgmii_probe(interface);
  262. break;
  263. /* PCI target Network Packet Interface */
  264. case CVMX_HELPER_INTERFACE_MODE_NPI:
  265. interface_port_count[interface] =
  266. __cvmx_helper_npi_probe(interface);
  267. break;
  268. /*
  269. * Special loopback only ports. These are not the same
  270. * as other ports in loopback mode.
  271. */
  272. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  273. interface_port_count[interface] =
  274. __cvmx_helper_loop_probe(interface);
  275. break;
  276. }
  277. interface_port_count[interface] =
  278. __cvmx_helper_board_interface_probe(interface,
  279. interface_port_count
  280. [interface]);
  281. /* Make sure all global variables propagate to other cores */
  282. CVMX_SYNCWS;
  283. return 0;
  284. }
  285. /**
  286. * Setup the IPD/PIP for the ports on an interface. Packet
  287. * classification and tagging are set for every port on the
  288. * interface. The number of ports on the interface must already
  289. * have been probed.
  290. *
  291. * @interface: Interface to setup IPD/PIP for
  292. *
  293. * Returns Zero on success, negative on failure
  294. */
  295. static int __cvmx_helper_interface_setup_ipd(int interface)
  296. {
  297. int ipd_port = cvmx_helper_get_ipd_port(interface, 0);
  298. int num_ports = interface_port_count[interface];
  299. while (num_ports--) {
  300. __cvmx_helper_port_setup_ipd(ipd_port);
  301. ipd_port++;
  302. }
  303. return 0;
  304. }
  305. /**
  306. * Setup global setting for IPD/PIP not related to a specific
  307. * interface or port. This must be called before IPD is enabled.
  308. *
  309. * Returns Zero on success, negative on failure.
  310. */
  311. static int __cvmx_helper_global_setup_ipd(void)
  312. {
  313. /* Setup the global packet input options */
  314. cvmx_ipd_config(CVMX_FPA_PACKET_POOL_SIZE / 8,
  315. CVMX_HELPER_FIRST_MBUFF_SKIP / 8,
  316. CVMX_HELPER_NOT_FIRST_MBUFF_SKIP / 8,
  317. /* The +8 is to account for the next ptr */
  318. (CVMX_HELPER_FIRST_MBUFF_SKIP + 8) / 128,
  319. /* The +8 is to account for the next ptr */
  320. (CVMX_HELPER_NOT_FIRST_MBUFF_SKIP + 8) / 128,
  321. CVMX_FPA_WQE_POOL,
  322. CVMX_IPD_OPC_MODE_STT,
  323. CVMX_HELPER_ENABLE_BACK_PRESSURE);
  324. return 0;
  325. }
  326. /**
  327. * Setup the PKO for the ports on an interface. The number of
  328. * queues per port and the priority of each PKO output queue
  329. * is set here. PKO must be disabled when this function is called.
  330. *
  331. * @interface: Interface to setup PKO for
  332. *
  333. * Returns Zero on success, negative on failure
  334. */
  335. static int __cvmx_helper_interface_setup_pko(int interface)
  336. {
  337. /*
  338. * Each packet output queue has an associated priority. The
  339. * higher the priority, the more often it can send a packet. A
  340. * priority of 8 means it can send in all 8 rounds of
  341. * contention. We're going to make each queue one less than
  342. * the last. The vector of priorities has been extended to
  343. * support CN5xxx CPUs, where up to 16 queues can be
  344. * associated to a port. To keep backward compatibility we
  345. * don't change the initial 8 priorities and replicate them in
  346. * the second half. With per-core PKO queues (PKO lockless
  347. * operation) all queues have the same priority.
  348. */
  349. uint64_t priorities[16] =
  350. { 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1 };
  351. /*
  352. * Setup the IPD/PIP and PKO for the ports discovered
  353. * above. Here packet classification, tagging and output
  354. * priorities are set.
  355. */
  356. int ipd_port = cvmx_helper_get_ipd_port(interface, 0);
  357. int num_ports = interface_port_count[interface];
  358. while (num_ports--) {
  359. /*
  360. * Give the user a chance to override the per queue
  361. * priorities.
  362. */
  363. if (cvmx_override_pko_queue_priority)
  364. cvmx_override_pko_queue_priority(ipd_port, priorities);
  365. cvmx_pko_config_port(ipd_port,
  366. cvmx_pko_get_base_queue_per_core(ipd_port,
  367. 0),
  368. cvmx_pko_get_num_queues(ipd_port),
  369. priorities);
  370. ipd_port++;
  371. }
  372. return 0;
  373. }
  374. /**
  375. * Setup global setting for PKO not related to a specific
  376. * interface or port. This must be called before PKO is enabled.
  377. *
  378. * Returns Zero on success, negative on failure.
  379. */
  380. static int __cvmx_helper_global_setup_pko(void)
  381. {
  382. /*
  383. * Disable tagwait FAU timeout. This needs to be done before
  384. * anyone might start packet output using tags.
  385. */
  386. union cvmx_iob_fau_timeout fau_to;
  387. fau_to.u64 = 0;
  388. fau_to.s.tout_val = 0xfff;
  389. fau_to.s.tout_enb = 0;
  390. cvmx_write_csr(CVMX_IOB_FAU_TIMEOUT, fau_to.u64);
  391. return 0;
  392. }
  393. /**
  394. * Setup global backpressure setting.
  395. *
  396. * Returns Zero on success, negative on failure
  397. */
  398. static int __cvmx_helper_global_setup_backpressure(void)
  399. {
  400. #if CVMX_HELPER_DISABLE_RGMII_BACKPRESSURE
  401. /* Disable backpressure if configured to do so */
  402. /* Disable backpressure (pause frame) generation */
  403. int num_interfaces = cvmx_helper_get_number_of_interfaces();
  404. int interface;
  405. for (interface = 0; interface < num_interfaces; interface++) {
  406. switch (cvmx_helper_interface_get_mode(interface)) {
  407. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  408. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  409. case CVMX_HELPER_INTERFACE_MODE_NPI:
  410. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  411. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  412. break;
  413. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  414. case CVMX_HELPER_INTERFACE_MODE_GMII:
  415. case CVMX_HELPER_INTERFACE_MODE_SPI:
  416. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  417. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  418. cvmx_gmx_set_backpressure_override(interface, 0xf);
  419. break;
  420. }
  421. }
  422. #endif
  423. return 0;
  424. }
  425. /**
  426. * Enable packet input/output from the hardware. This function is
  427. * called after all internal setup is complete and IPD is enabled.
  428. * After this function completes, packets will be accepted from the
  429. * hardware ports. PKO should still be disabled to make sure packets
  430. * aren't sent out partially setup hardware.
  431. *
  432. * @interface: Interface to enable
  433. *
  434. * Returns Zero on success, negative on failure
  435. */
  436. static int __cvmx_helper_packet_hardware_enable(int interface)
  437. {
  438. int result = 0;
  439. switch (cvmx_helper_interface_get_mode(interface)) {
  440. /* These types don't support ports to IPD/PKO */
  441. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  442. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  443. /* Nothing to do */
  444. break;
  445. /* XAUI is a single high speed port */
  446. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  447. result = __cvmx_helper_xaui_enable(interface);
  448. break;
  449. /*
  450. * RGMII/GMII/MII are all treated about the same. Most
  451. * functions refer to these ports as RGMII
  452. */
  453. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  454. case CVMX_HELPER_INTERFACE_MODE_GMII:
  455. result = __cvmx_helper_rgmii_enable(interface);
  456. break;
  457. /*
  458. * SPI4 can have 1-16 ports depending on the device at
  459. * the other end
  460. */
  461. case CVMX_HELPER_INTERFACE_MODE_SPI:
  462. result = __cvmx_helper_spi_enable(interface);
  463. break;
  464. /*
  465. * SGMII can have 1-4 ports depending on how many are
  466. * hooked up
  467. */
  468. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  469. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  470. result = __cvmx_helper_sgmii_enable(interface);
  471. break;
  472. /* PCI target Network Packet Interface */
  473. case CVMX_HELPER_INTERFACE_MODE_NPI:
  474. result = __cvmx_helper_npi_enable(interface);
  475. break;
  476. /*
  477. * Special loopback only ports. These are not the same
  478. * as other ports in loopback mode
  479. */
  480. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  481. result = __cvmx_helper_loop_enable(interface);
  482. break;
  483. }
  484. result |= __cvmx_helper_board_hardware_enable(interface);
  485. return result;
  486. }
  487. /**
  488. * Function to adjust internal IPD pointer alignments
  489. *
  490. * Returns 0 on success
  491. * !0 on failure
  492. */
  493. int __cvmx_helper_errata_fix_ipd_ptr_alignment(void)
  494. {
  495. #define FIX_IPD_FIRST_BUFF_PAYLOAD_BYTES \
  496. (CVMX_FPA_PACKET_POOL_SIZE-8-CVMX_HELPER_FIRST_MBUFF_SKIP)
  497. #define FIX_IPD_NON_FIRST_BUFF_PAYLOAD_BYTES \
  498. (CVMX_FPA_PACKET_POOL_SIZE-8-CVMX_HELPER_NOT_FIRST_MBUFF_SKIP)
  499. #define FIX_IPD_OUTPORT 0
  500. /* Ports 0-15 are interface 0, 16-31 are interface 1 */
  501. #define INTERFACE(port) (port >> 4)
  502. #define INDEX(port) (port & 0xf)
  503. uint64_t *p64;
  504. cvmx_pko_command_word0_t pko_command;
  505. union cvmx_buf_ptr g_buffer, pkt_buffer;
  506. cvmx_wqe_t *work;
  507. int size, num_segs = 0, wqe_pcnt, pkt_pcnt;
  508. union cvmx_gmxx_prtx_cfg gmx_cfg;
  509. int retry_cnt;
  510. int retry_loop_cnt;
  511. int mtu;
  512. int i;
  513. cvmx_helper_link_info_t link_info;
  514. /* Save values for restore at end */
  515. uint64_t prtx_cfg =
  516. cvmx_read_csr(CVMX_GMXX_PRTX_CFG
  517. (INDEX(FIX_IPD_OUTPORT), INTERFACE(FIX_IPD_OUTPORT)));
  518. uint64_t tx_ptr_en =
  519. cvmx_read_csr(CVMX_ASXX_TX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)));
  520. uint64_t rx_ptr_en =
  521. cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)));
  522. uint64_t rxx_jabber =
  523. cvmx_read_csr(CVMX_GMXX_RXX_JABBER
  524. (INDEX(FIX_IPD_OUTPORT), INTERFACE(FIX_IPD_OUTPORT)));
  525. uint64_t frame_max =
  526. cvmx_read_csr(CVMX_GMXX_RXX_FRM_MAX
  527. (INDEX(FIX_IPD_OUTPORT), INTERFACE(FIX_IPD_OUTPORT)));
  528. /* Configure port to gig FDX as required for loopback mode */
  529. cvmx_helper_rgmii_internal_loopback(FIX_IPD_OUTPORT);
  530. /*
  531. * Disable reception on all ports so if traffic is present it
  532. * will not interfere.
  533. */
  534. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)), 0);
  535. cvmx_wait(100000000ull);
  536. for (retry_loop_cnt = 0; retry_loop_cnt < 10; retry_loop_cnt++) {
  537. retry_cnt = 100000;
  538. wqe_pcnt = cvmx_read_csr(CVMX_IPD_PTR_COUNT);
  539. pkt_pcnt = (wqe_pcnt >> 7) & 0x7f;
  540. wqe_pcnt &= 0x7f;
  541. num_segs = (2 + pkt_pcnt - wqe_pcnt) & 3;
  542. if (num_segs == 0)
  543. goto fix_ipd_exit;
  544. num_segs += 1;
  545. size =
  546. FIX_IPD_FIRST_BUFF_PAYLOAD_BYTES +
  547. ((num_segs - 1) * FIX_IPD_NON_FIRST_BUFF_PAYLOAD_BYTES) -
  548. (FIX_IPD_NON_FIRST_BUFF_PAYLOAD_BYTES / 2);
  549. cvmx_write_csr(CVMX_ASXX_PRT_LOOP(INTERFACE(FIX_IPD_OUTPORT)),
  550. 1 << INDEX(FIX_IPD_OUTPORT));
  551. CVMX_SYNC;
  552. g_buffer.u64 = 0;
  553. g_buffer.s.addr =
  554. cvmx_ptr_to_phys(cvmx_fpa_alloc(CVMX_FPA_WQE_POOL));
  555. if (g_buffer.s.addr == 0) {
  556. cvmx_dprintf("WARNING: FIX_IPD_PTR_ALIGNMENT "
  557. "buffer allocation failure.\n");
  558. goto fix_ipd_exit;
  559. }
  560. g_buffer.s.pool = CVMX_FPA_WQE_POOL;
  561. g_buffer.s.size = num_segs;
  562. pkt_buffer.u64 = 0;
  563. pkt_buffer.s.addr =
  564. cvmx_ptr_to_phys(cvmx_fpa_alloc(CVMX_FPA_PACKET_POOL));
  565. if (pkt_buffer.s.addr == 0) {
  566. cvmx_dprintf("WARNING: FIX_IPD_PTR_ALIGNMENT "
  567. "buffer allocation failure.\n");
  568. goto fix_ipd_exit;
  569. }
  570. pkt_buffer.s.i = 1;
  571. pkt_buffer.s.pool = CVMX_FPA_PACKET_POOL;
  572. pkt_buffer.s.size = FIX_IPD_FIRST_BUFF_PAYLOAD_BYTES;
  573. p64 = (uint64_t *) cvmx_phys_to_ptr(pkt_buffer.s.addr);
  574. p64[0] = 0xffffffffffff0000ull;
  575. p64[1] = 0x08004510ull;
  576. p64[2] = ((uint64_t) (size - 14) << 48) | 0x5ae740004000ull;
  577. p64[3] = 0x3a5fc0a81073c0a8ull;
  578. for (i = 0; i < num_segs; i++) {
  579. if (i > 0)
  580. pkt_buffer.s.size =
  581. FIX_IPD_NON_FIRST_BUFF_PAYLOAD_BYTES;
  582. if (i == (num_segs - 1))
  583. pkt_buffer.s.i = 0;
  584. *(uint64_t *) cvmx_phys_to_ptr(g_buffer.s.addr +
  585. 8 * i) = pkt_buffer.u64;
  586. }
  587. /* Build the PKO command */
  588. pko_command.u64 = 0;
  589. pko_command.s.segs = num_segs;
  590. pko_command.s.total_bytes = size;
  591. pko_command.s.dontfree = 0;
  592. pko_command.s.gather = 1;
  593. gmx_cfg.u64 =
  594. cvmx_read_csr(CVMX_GMXX_PRTX_CFG
  595. (INDEX(FIX_IPD_OUTPORT),
  596. INTERFACE(FIX_IPD_OUTPORT)));
  597. gmx_cfg.s.en = 1;
  598. cvmx_write_csr(CVMX_GMXX_PRTX_CFG
  599. (INDEX(FIX_IPD_OUTPORT),
  600. INTERFACE(FIX_IPD_OUTPORT)), gmx_cfg.u64);
  601. cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)),
  602. 1 << INDEX(FIX_IPD_OUTPORT));
  603. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)),
  604. 1 << INDEX(FIX_IPD_OUTPORT));
  605. mtu =
  606. cvmx_read_csr(CVMX_GMXX_RXX_JABBER
  607. (INDEX(FIX_IPD_OUTPORT),
  608. INTERFACE(FIX_IPD_OUTPORT)));
  609. cvmx_write_csr(CVMX_GMXX_RXX_JABBER
  610. (INDEX(FIX_IPD_OUTPORT),
  611. INTERFACE(FIX_IPD_OUTPORT)), 65392 - 14 - 4);
  612. cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX
  613. (INDEX(FIX_IPD_OUTPORT),
  614. INTERFACE(FIX_IPD_OUTPORT)), 65392 - 14 - 4);
  615. cvmx_pko_send_packet_prepare(FIX_IPD_OUTPORT,
  616. cvmx_pko_get_base_queue
  617. (FIX_IPD_OUTPORT),
  618. CVMX_PKO_LOCK_CMD_QUEUE);
  619. cvmx_pko_send_packet_finish(FIX_IPD_OUTPORT,
  620. cvmx_pko_get_base_queue
  621. (FIX_IPD_OUTPORT), pko_command,
  622. g_buffer, CVMX_PKO_LOCK_CMD_QUEUE);
  623. CVMX_SYNC;
  624. do {
  625. work = cvmx_pow_work_request_sync(CVMX_POW_WAIT);
  626. retry_cnt--;
  627. } while ((work == NULL) && (retry_cnt > 0));
  628. if (!retry_cnt)
  629. cvmx_dprintf("WARNING: FIX_IPD_PTR_ALIGNMENT "
  630. "get_work() timeout occurred.\n");
  631. /* Free packet */
  632. if (work)
  633. cvmx_helper_free_packet_data(work);
  634. }
  635. fix_ipd_exit:
  636. /* Return CSR configs to saved values */
  637. cvmx_write_csr(CVMX_GMXX_PRTX_CFG
  638. (INDEX(FIX_IPD_OUTPORT), INTERFACE(FIX_IPD_OUTPORT)),
  639. prtx_cfg);
  640. cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)),
  641. tx_ptr_en);
  642. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(INTERFACE(FIX_IPD_OUTPORT)),
  643. rx_ptr_en);
  644. cvmx_write_csr(CVMX_GMXX_RXX_JABBER
  645. (INDEX(FIX_IPD_OUTPORT), INTERFACE(FIX_IPD_OUTPORT)),
  646. rxx_jabber);
  647. cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX
  648. (INDEX(FIX_IPD_OUTPORT), INTERFACE(FIX_IPD_OUTPORT)),
  649. frame_max);
  650. cvmx_write_csr(CVMX_ASXX_PRT_LOOP(INTERFACE(FIX_IPD_OUTPORT)), 0);
  651. /* Set link to down so autonegotiation will set it up again */
  652. link_info.u64 = 0;
  653. cvmx_helper_link_set(FIX_IPD_OUTPORT, link_info);
  654. /*
  655. * Bring the link back up as autonegotiation is not done in
  656. * user applications.
  657. */
  658. cvmx_helper_link_autoconf(FIX_IPD_OUTPORT);
  659. CVMX_SYNC;
  660. if (num_segs)
  661. cvmx_dprintf("WARNING: FIX_IPD_PTR_ALIGNMENT failed.\n");
  662. return !!num_segs;
  663. }
  664. /**
  665. * Called after all internal packet IO paths are setup. This
  666. * function enables IPD/PIP and begins packet input and output.
  667. *
  668. * Returns Zero on success, negative on failure
  669. */
  670. int cvmx_helper_ipd_and_packet_input_enable(void)
  671. {
  672. int num_interfaces;
  673. int interface;
  674. /* Enable IPD */
  675. cvmx_ipd_enable();
  676. /*
  677. * Time to enable hardware ports packet input and output. Note
  678. * that at this point IPD/PIP must be fully functional and PKO
  679. * must be disabled
  680. */
  681. num_interfaces = cvmx_helper_get_number_of_interfaces();
  682. for (interface = 0; interface < num_interfaces; interface++) {
  683. if (cvmx_helper_ports_on_interface(interface) > 0)
  684. __cvmx_helper_packet_hardware_enable(interface);
  685. }
  686. /* Finally enable PKO now that the entire path is up and running */
  687. cvmx_pko_enable();
  688. if ((OCTEON_IS_MODEL(OCTEON_CN31XX_PASS1)
  689. || OCTEON_IS_MODEL(OCTEON_CN30XX_PASS1))
  690. && (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM))
  691. __cvmx_helper_errata_fix_ipd_ptr_alignment();
  692. return 0;
  693. }
  694. /**
  695. * Initialize the PIP, IPD, and PKO hardware to support
  696. * simple priority based queues for the ethernet ports. Each
  697. * port is configured with a number of priority queues based
  698. * on CVMX_PKO_QUEUES_PER_PORT_* where each queue is lower
  699. * priority than the previous.
  700. *
  701. * Returns Zero on success, non-zero on failure
  702. */
  703. int cvmx_helper_initialize_packet_io_global(void)
  704. {
  705. int result = 0;
  706. int interface;
  707. union cvmx_l2c_cfg l2c_cfg;
  708. union cvmx_smix_en smix_en;
  709. const int num_interfaces = cvmx_helper_get_number_of_interfaces();
  710. /*
  711. * CN52XX pass 1: Due to a bug in 2nd order CDR, it needs to
  712. * be disabled.
  713. */
  714. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_0))
  715. __cvmx_helper_errata_qlm_disable_2nd_order_cdr(1);
  716. /*
  717. * Tell L2 to give the IOB statically higher priority compared
  718. * to the cores. This avoids conditions where IO blocks might
  719. * be starved under very high L2 loads.
  720. */
  721. l2c_cfg.u64 = cvmx_read_csr(CVMX_L2C_CFG);
  722. l2c_cfg.s.lrf_arb_mode = 0;
  723. l2c_cfg.s.rfb_arb_mode = 0;
  724. cvmx_write_csr(CVMX_L2C_CFG, l2c_cfg.u64);
  725. /* Make sure SMI/MDIO is enabled so we can query PHYs */
  726. smix_en.u64 = cvmx_read_csr(CVMX_SMIX_EN(0));
  727. if (!smix_en.s.en) {
  728. smix_en.s.en = 1;
  729. cvmx_write_csr(CVMX_SMIX_EN(0), smix_en.u64);
  730. }
  731. /* Newer chips actually have two SMI/MDIO interfaces */
  732. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX) &&
  733. !OCTEON_IS_MODEL(OCTEON_CN58XX) &&
  734. !OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  735. smix_en.u64 = cvmx_read_csr(CVMX_SMIX_EN(1));
  736. if (!smix_en.s.en) {
  737. smix_en.s.en = 1;
  738. cvmx_write_csr(CVMX_SMIX_EN(1), smix_en.u64);
  739. }
  740. }
  741. cvmx_pko_initialize_global();
  742. for (interface = 0; interface < num_interfaces; interface++) {
  743. result |= cvmx_helper_interface_probe(interface);
  744. if (cvmx_helper_ports_on_interface(interface) > 0)
  745. cvmx_dprintf("Interface %d has %d ports (%s)\n",
  746. interface,
  747. cvmx_helper_ports_on_interface(interface),
  748. cvmx_helper_interface_mode_to_string
  749. (cvmx_helper_interface_get_mode
  750. (interface)));
  751. result |= __cvmx_helper_interface_setup_ipd(interface);
  752. result |= __cvmx_helper_interface_setup_pko(interface);
  753. }
  754. result |= __cvmx_helper_global_setup_ipd();
  755. result |= __cvmx_helper_global_setup_pko();
  756. /* Enable any flow control and backpressure */
  757. result |= __cvmx_helper_global_setup_backpressure();
  758. #if CVMX_HELPER_ENABLE_IPD
  759. result |= cvmx_helper_ipd_and_packet_input_enable();
  760. #endif
  761. return result;
  762. }
  763. /**
  764. * Does core local initialization for packet io
  765. *
  766. * Returns Zero on success, non-zero on failure
  767. */
  768. int cvmx_helper_initialize_packet_io_local(void)
  769. {
  770. return cvmx_pko_initialize_local();
  771. }
  772. /**
  773. * Auto configure an IPD/PKO port link state and speed. This
  774. * function basically does the equivalent of:
  775. * cvmx_helper_link_set(ipd_port, cvmx_helper_link_get(ipd_port));
  776. *
  777. * @ipd_port: IPD/PKO port to auto configure
  778. *
  779. * Returns Link state after configure
  780. */
  781. cvmx_helper_link_info_t cvmx_helper_link_autoconf(int ipd_port)
  782. {
  783. cvmx_helper_link_info_t link_info;
  784. int interface = cvmx_helper_get_interface_num(ipd_port);
  785. int index = cvmx_helper_get_interface_index_num(ipd_port);
  786. if (index >= cvmx_helper_ports_on_interface(interface)) {
  787. link_info.u64 = 0;
  788. return link_info;
  789. }
  790. link_info = cvmx_helper_link_get(ipd_port);
  791. if (link_info.u64 == port_link_info[ipd_port].u64)
  792. return link_info;
  793. /* If we fail to set the link speed, port_link_info will not change */
  794. cvmx_helper_link_set(ipd_port, link_info);
  795. /*
  796. * port_link_info should be the current value, which will be
  797. * different than expect if cvmx_helper_link_set() failed.
  798. */
  799. return port_link_info[ipd_port];
  800. }
  801. /**
  802. * Return the link state of an IPD/PKO port as returned by
  803. * auto negotiation. The result of this function may not match
  804. * Octeon's link config if auto negotiation has changed since
  805. * the last call to cvmx_helper_link_set().
  806. *
  807. * @ipd_port: IPD/PKO port to query
  808. *
  809. * Returns Link state
  810. */
  811. cvmx_helper_link_info_t cvmx_helper_link_get(int ipd_port)
  812. {
  813. cvmx_helper_link_info_t result;
  814. int interface = cvmx_helper_get_interface_num(ipd_port);
  815. int index = cvmx_helper_get_interface_index_num(ipd_port);
  816. /* The default result will be a down link unless the code below
  817. changes it */
  818. result.u64 = 0;
  819. if (index >= cvmx_helper_ports_on_interface(interface))
  820. return result;
  821. switch (cvmx_helper_interface_get_mode(interface)) {
  822. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  823. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  824. /* Network links are not supported */
  825. break;
  826. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  827. result = __cvmx_helper_xaui_link_get(ipd_port);
  828. break;
  829. case CVMX_HELPER_INTERFACE_MODE_GMII:
  830. if (index == 0)
  831. result = __cvmx_helper_rgmii_link_get(ipd_port);
  832. else {
  833. result.s.full_duplex = 1;
  834. result.s.link_up = 1;
  835. result.s.speed = 1000;
  836. }
  837. break;
  838. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  839. result = __cvmx_helper_rgmii_link_get(ipd_port);
  840. break;
  841. case CVMX_HELPER_INTERFACE_MODE_SPI:
  842. result = __cvmx_helper_spi_link_get(ipd_port);
  843. break;
  844. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  845. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  846. result = __cvmx_helper_sgmii_link_get(ipd_port);
  847. break;
  848. case CVMX_HELPER_INTERFACE_MODE_NPI:
  849. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  850. /* Network links are not supported */
  851. break;
  852. }
  853. return result;
  854. }
  855. /**
  856. * Configure an IPD/PKO port for the specified link state. This
  857. * function does not influence auto negotiation at the PHY level.
  858. * The passed link state must always match the link state returned
  859. * by cvmx_helper_link_get(). It is normally best to use
  860. * cvmx_helper_link_autoconf() instead.
  861. *
  862. * @ipd_port: IPD/PKO port to configure
  863. * @link_info: The new link state
  864. *
  865. * Returns Zero on success, negative on failure
  866. */
  867. int cvmx_helper_link_set(int ipd_port, cvmx_helper_link_info_t link_info)
  868. {
  869. int result = -1;
  870. int interface = cvmx_helper_get_interface_num(ipd_port);
  871. int index = cvmx_helper_get_interface_index_num(ipd_port);
  872. if (index >= cvmx_helper_ports_on_interface(interface))
  873. return -1;
  874. switch (cvmx_helper_interface_get_mode(interface)) {
  875. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  876. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  877. break;
  878. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  879. result = __cvmx_helper_xaui_link_set(ipd_port, link_info);
  880. break;
  881. /*
  882. * RGMII/GMII/MII are all treated about the same. Most
  883. * functions refer to these ports as RGMII.
  884. */
  885. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  886. case CVMX_HELPER_INTERFACE_MODE_GMII:
  887. result = __cvmx_helper_rgmii_link_set(ipd_port, link_info);
  888. break;
  889. case CVMX_HELPER_INTERFACE_MODE_SPI:
  890. result = __cvmx_helper_spi_link_set(ipd_port, link_info);
  891. break;
  892. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  893. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  894. result = __cvmx_helper_sgmii_link_set(ipd_port, link_info);
  895. break;
  896. case CVMX_HELPER_INTERFACE_MODE_NPI:
  897. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  898. break;
  899. }
  900. /* Set the port_link_info here so that the link status is updated
  901. no matter how cvmx_helper_link_set is called. We don't change
  902. the value if link_set failed */
  903. if (result == 0)
  904. port_link_info[ipd_port].u64 = link_info.u64;
  905. return result;
  906. }
  907. /**
  908. * Configure a port for internal and/or external loopback. Internal loopback
  909. * causes packets sent by the port to be received by Octeon. External loopback
  910. * causes packets received from the wire to sent out again.
  911. *
  912. * @ipd_port: IPD/PKO port to loopback.
  913. * @enable_internal:
  914. * Non zero if you want internal loopback
  915. * @enable_external:
  916. * Non zero if you want external loopback
  917. *
  918. * Returns Zero on success, negative on failure.
  919. */
  920. int cvmx_helper_configure_loopback(int ipd_port, int enable_internal,
  921. int enable_external)
  922. {
  923. int result = -1;
  924. int interface = cvmx_helper_get_interface_num(ipd_port);
  925. int index = cvmx_helper_get_interface_index_num(ipd_port);
  926. if (index >= cvmx_helper_ports_on_interface(interface))
  927. return -1;
  928. switch (cvmx_helper_interface_get_mode(interface)) {
  929. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  930. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  931. case CVMX_HELPER_INTERFACE_MODE_SPI:
  932. case CVMX_HELPER_INTERFACE_MODE_NPI:
  933. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  934. break;
  935. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  936. result =
  937. __cvmx_helper_xaui_configure_loopback(ipd_port,
  938. enable_internal,
  939. enable_external);
  940. break;
  941. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  942. case CVMX_HELPER_INTERFACE_MODE_GMII:
  943. result =
  944. __cvmx_helper_rgmii_configure_loopback(ipd_port,
  945. enable_internal,
  946. enable_external);
  947. break;
  948. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  949. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  950. result =
  951. __cvmx_helper_sgmii_configure_loopback(ipd_port,
  952. enable_internal,
  953. enable_external);
  954. break;
  955. }
  956. return result;
  957. }