cpu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (C) 2013 Realtek Semiconductor Corp.
  3. * All Rights Reserved.
  4. *
  5. * Unless you and Realtek execute a separate written software license
  6. * agreement governing use of this software, this software is licensed
  7. * to you under the terms of the GNU General Public License version 2,
  8. * available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
  9. *
  10. * $Revision: 76306 $
  11. * $Date: 2017-03-08 15:13:58 +0800 (週三, 08 三月 2017) $
  12. *
  13. * Purpose : RTK switch high-level API for RTL8367/RTL8367C
  14. * Feature : Here is a list of all functions and variables in CPU module.
  15. *
  16. */
  17. #include <rtk_switch.h>
  18. #include <rtk_error.h>
  19. #include <cpu.h>
  20. #include <string.h>
  21. #include <rtl8367c_asicdrv.h>
  22. #include <rtl8367c_asicdrv_cputag.h>
  23. /* Function Name:
  24. * rtk_cpu_enable_set
  25. * Description:
  26. * Set CPU port function enable/disable.
  27. * Input:
  28. * enable - CPU port function enable
  29. * Output:
  30. * None
  31. * Return:
  32. * RT_ERR_OK - OK
  33. * RT_ERR_FAILED - Failed
  34. * RT_ERR_SMI - SMI access error
  35. * RT_ERR_INPUT - Invalid input parameter.
  36. * RT_ERR_PORT_ID - Invalid port number.
  37. * Note:
  38. * The API can set CPU port function enable/disable.
  39. */
  40. rtk_api_ret_t rtk_cpu_enable_set(rtk_enable_t enable)
  41. {
  42. rtk_api_ret_t retVal;
  43. /* Check initialization state */
  44. RTK_CHK_INIT_STATE();
  45. if (enable >= RTK_ENABLE_END)
  46. return RT_ERR_ENABLE;
  47. if ((retVal = rtl8367c_setAsicCputagEnable(enable)) != RT_ERR_OK)
  48. return retVal;
  49. if (DISABLED == enable)
  50. {
  51. if ((retVal = rtl8367c_setAsicCputagPortmask(0)) != RT_ERR_OK)
  52. return retVal;
  53. }
  54. return RT_ERR_OK;
  55. }
  56. /* Function Name:
  57. * rtk_cpu_enable_get
  58. * Description:
  59. * Get CPU port and its setting.
  60. * Input:
  61. * None
  62. * Output:
  63. * pEnable - CPU port function enable
  64. * Return:
  65. * RT_ERR_OK - OK
  66. * RT_ERR_FAILED - Failed
  67. * RT_ERR_SMI - SMI access error
  68. * RT_ERR_INPUT - Invalid input parameters.
  69. * RT_ERR_L2_NO_CPU_PORT - CPU port is not exist
  70. * Note:
  71. * The API can get CPU port function enable/disable.
  72. */
  73. rtk_api_ret_t rtk_cpu_enable_get(rtk_enable_t *pEnable)
  74. {
  75. rtk_api_ret_t retVal;
  76. /* Check initialization state */
  77. RTK_CHK_INIT_STATE();
  78. if(NULL == pEnable)
  79. return RT_ERR_NULL_POINTER;
  80. if ((retVal = rtl8367c_getAsicCputagEnable(pEnable)) != RT_ERR_OK)
  81. return retVal;
  82. return RT_ERR_OK;
  83. }
  84. /* Function Name:
  85. * rtk_cpu_tagPort_set
  86. * Description:
  87. * Set CPU port and CPU tag insert mode.
  88. * Input:
  89. * port - Port id.
  90. * mode - CPU tag insert for packets egress from CPU port.
  91. * Output:
  92. * None
  93. * Return:
  94. * RT_ERR_OK - OK
  95. * RT_ERR_FAILED - Failed
  96. * RT_ERR_SMI - SMI access error
  97. * RT_ERR_INPUT - Invalid input parameter.
  98. * RT_ERR_PORT_ID - Invalid port number.
  99. * Note:
  100. * The API can set CPU port and inserting proprietary CPU tag mode (Length/Type 0x8899)
  101. * to the frame that transmitting to CPU port.
  102. * The insert CPU tag mode is as following:
  103. * - CPU_INSERT_TO_ALL
  104. * - CPU_INSERT_TO_TRAPPING
  105. * - CPU_INSERT_TO_NONE
  106. */
  107. rtk_api_ret_t rtk_cpu_tagPort_set(rtk_port_t port, rtk_cpu_insert_t mode)
  108. {
  109. rtk_api_ret_t retVal;
  110. /* Check initialization state */
  111. RTK_CHK_INIT_STATE();
  112. /* Check port Valid */
  113. RTK_CHK_PORT_VALID(port);
  114. if (mode >= CPU_INSERT_END)
  115. return RT_ERR_INPUT;
  116. if ((retVal = rtl8367c_setAsicCputagPortmask(1<<rtk_switch_port_L2P_get(port))) != RT_ERR_OK)
  117. return retVal;
  118. if ((retVal = rtl8367c_setAsicCputagTrapPort(rtk_switch_port_L2P_get(port))) != RT_ERR_OK)
  119. return retVal;
  120. if ((retVal = rtl8367c_setAsicCputagInsertMode(mode)) != RT_ERR_OK)
  121. return retVal;
  122. return RT_ERR_OK;
  123. }
  124. /* Function Name:
  125. * rtk_cpu_tagPort_get
  126. * Description:
  127. * Get CPU port and CPU tag insert mode.
  128. * Input:
  129. * None
  130. * Output:
  131. * pPort - Port id.
  132. * pMode - CPU tag insert for packets egress from CPU port, 0:all insert 1:Only for trapped packets 2:no insert.
  133. * Return:
  134. * RT_ERR_OK - OK
  135. * RT_ERR_FAILED - Failed
  136. * RT_ERR_SMI - SMI access error
  137. * RT_ERR_INPUT - Invalid input parameters.
  138. * RT_ERR_L2_NO_CPU_PORT - CPU port is not exist
  139. * Note:
  140. * The API can get configured CPU port and its setting.
  141. * The insert CPU tag mode is as following:
  142. * - CPU_INSERT_TO_ALL
  143. * - CPU_INSERT_TO_TRAPPING
  144. * - CPU_INSERT_TO_NONE
  145. */
  146. rtk_api_ret_t rtk_cpu_tagPort_get(rtk_port_t *pPort, rtk_cpu_insert_t *pMode)
  147. {
  148. rtk_api_ret_t retVal;
  149. rtk_uint32 pmsk, port;
  150. /* Check initialization state */
  151. RTK_CHK_INIT_STATE();
  152. if(NULL == pPort)
  153. return RT_ERR_NULL_POINTER;
  154. if(NULL == pMode)
  155. return RT_ERR_NULL_POINTER;
  156. if ((retVal = rtl8367c_getAsicCputagPortmask(&pmsk)) != RT_ERR_OK)
  157. return retVal;
  158. if ((retVal = rtl8367c_getAsicCputagTrapPort(&port)) != RT_ERR_OK)
  159. return retVal;
  160. *pPort = rtk_switch_port_P2L_get(port);
  161. if ((retVal = rtl8367c_getAsicCputagInsertMode(pMode)) != RT_ERR_OK)
  162. return retVal;
  163. return RT_ERR_OK;
  164. }
  165. /* Function Name:
  166. * rtk_cpu_awarePort_set
  167. * Description:
  168. * Set CPU aware port mask.
  169. * Input:
  170. * portmask - Port mask.
  171. * Output:
  172. * None
  173. * Return:
  174. * RT_ERR_OK - OK
  175. * RT_ERR_FAILED - Failed
  176. * RT_ERR_SMI - SMI access error
  177. * RT_ERR_PORT_MASK - Invalid port mask.
  178. * Note:
  179. * The API can set configured CPU aware port mask.
  180. */
  181. rtk_api_ret_t rtk_cpu_awarePort_set(rtk_portmask_t *pPortmask)
  182. {
  183. rtk_api_ret_t retVal;
  184. rtk_uint32 phyMbrPmask;
  185. /* Check initialization state */
  186. RTK_CHK_INIT_STATE();
  187. /* Check Valid port mask */
  188. if(NULL == pPortmask)
  189. return RT_ERR_NULL_POINTER;
  190. /* Check port mask valid */
  191. RTK_CHK_PORTMASK_VALID(pPortmask);
  192. if(rtk_switch_portmask_L2P_get(pPortmask, &phyMbrPmask) != RT_ERR_OK)
  193. return RT_ERR_FAILED;
  194. if ((retVal = rtl8367c_setAsicCputagPortmask(phyMbrPmask)) != RT_ERR_OK)
  195. return retVal;
  196. return RT_ERR_OK;
  197. }
  198. /* Function Name:
  199. * rtk_cpu_awarePort_get
  200. * Description:
  201. * Get CPU aware port mask.
  202. * Input:
  203. * None
  204. * Output:
  205. * pPortmask - Port mask.
  206. * Return:
  207. * RT_ERR_OK - OK
  208. * RT_ERR_FAILED - Failed
  209. * RT_ERR_SMI - SMI access error
  210. * Note:
  211. * The API can get configured CPU aware port mask.
  212. */
  213. rtk_api_ret_t rtk_cpu_awarePort_get(rtk_portmask_t *pPortmask)
  214. {
  215. rtk_api_ret_t retVal;
  216. rtk_uint32 pmsk;
  217. /* Check initialization state */
  218. RTK_CHK_INIT_STATE();
  219. if(NULL == pPortmask)
  220. return RT_ERR_NULL_POINTER;
  221. if ((retVal = rtl8367c_getAsicCputagPortmask(&pmsk)) != RT_ERR_OK)
  222. return retVal;
  223. if(rtk_switch_portmask_P2L_get(pmsk, pPortmask) != RT_ERR_OK)
  224. return RT_ERR_FAILED;
  225. return RT_ERR_OK;
  226. }
  227. /* Function Name:
  228. * rtk_cpu_tagPosition_set
  229. * Description:
  230. * Set CPU tag position.
  231. * Input:
  232. * position - CPU tag position.
  233. * Output:
  234. * None
  235. * Return:
  236. * RT_ERR_OK - OK
  237. * RT_ERR_FAILED - Failed
  238. * RT_ERR_SMI - SMI access error
  239. * RT_ERR_INPUT - Invalid input.
  240. * Note:
  241. * The API can set CPU tag position.
  242. */
  243. rtk_api_ret_t rtk_cpu_tagPosition_set(rtk_cpu_position_t position)
  244. {
  245. rtk_api_ret_t retVal;
  246. /* Check initialization state */
  247. RTK_CHK_INIT_STATE();
  248. if (position >= CPU_POS_END)
  249. return RT_ERR_INPUT;
  250. if ((retVal = rtl8367c_setAsicCputagPosition(position)) != RT_ERR_OK)
  251. return retVal;
  252. return RT_ERR_OK;
  253. }
  254. /* Function Name:
  255. * rtk_cpu_tagPosition_get
  256. * Description:
  257. * Get CPU tag position.
  258. * Input:
  259. * None
  260. * Output:
  261. * pPosition - CPU tag position.
  262. * Return:
  263. * RT_ERR_OK - OK
  264. * RT_ERR_FAILED - Failed
  265. * RT_ERR_SMI - SMI access error
  266. * RT_ERR_INPUT - Invalid input.
  267. * Note:
  268. * The API can get CPU tag position.
  269. */
  270. rtk_api_ret_t rtk_cpu_tagPosition_get(rtk_cpu_position_t *pPosition)
  271. {
  272. rtk_api_ret_t retVal;
  273. /* Check initialization state */
  274. RTK_CHK_INIT_STATE();
  275. if(NULL == pPosition)
  276. return RT_ERR_NULL_POINTER;
  277. if ((retVal = rtl8367c_getAsicCputagPosition(pPosition)) != RT_ERR_OK)
  278. return retVal;
  279. return RT_ERR_OK;
  280. }
  281. /* Function Name:
  282. * rtk_cpu_tagLength_set
  283. * Description:
  284. * Set CPU tag length.
  285. * Input:
  286. * length - CPU tag length.
  287. * Output:
  288. * None
  289. * Return:
  290. * RT_ERR_OK - OK
  291. * RT_ERR_FAILED - Failed
  292. * RT_ERR_SMI - SMI access error
  293. * RT_ERR_INPUT - Invalid input.
  294. * Note:
  295. * The API can set CPU tag length.
  296. */
  297. rtk_api_ret_t rtk_cpu_tagLength_set(rtk_cpu_tag_length_t length)
  298. {
  299. rtk_api_ret_t retVal;
  300. /* Check initialization state */
  301. RTK_CHK_INIT_STATE();
  302. if (length >= CPU_LEN_END)
  303. return RT_ERR_INPUT;
  304. if ((retVal = rtl8367c_setAsicCputagMode(length)) != RT_ERR_OK)
  305. return retVal;
  306. return RT_ERR_OK;
  307. }
  308. /* Function Name:
  309. * rtk_cpu_tagLength_get
  310. * Description:
  311. * Get CPU tag length.
  312. * Input:
  313. * None
  314. * Output:
  315. * pLength - CPU tag length.
  316. * Return:
  317. * RT_ERR_OK - OK
  318. * RT_ERR_FAILED - Failed
  319. * RT_ERR_SMI - SMI access error
  320. * RT_ERR_INPUT - Invalid input.
  321. * Note:
  322. * The API can get CPU tag length.
  323. */
  324. rtk_api_ret_t rtk_cpu_tagLength_get(rtk_cpu_tag_length_t *pLength)
  325. {
  326. rtk_api_ret_t retVal;
  327. /* Check initialization state */
  328. RTK_CHK_INIT_STATE();
  329. if(NULL == pLength)
  330. return RT_ERR_NULL_POINTER;
  331. if ((retVal = rtl8367c_getAsicCputagMode(pLength)) != RT_ERR_OK)
  332. return retVal;
  333. return RT_ERR_OK;
  334. }
  335. /* Function Name:
  336. * rtk_cpu_priRemap_set
  337. * Description:
  338. * Configure CPU priorities mapping to internal absolute priority.
  339. * Input:
  340. * int_pri - internal priority value.
  341. * new_pri - new internal priority value.
  342. * Output:
  343. * None
  344. * Return:
  345. * RT_ERR_OK - OK
  346. * RT_ERR_FAILED - Failed
  347. * RT_ERR_SMI - SMI access error
  348. * RT_ERR_INPUT - Invalid input parameters.
  349. * RT_ERR_VLAN_PRIORITY - Invalid 1p priority.
  350. * RT_ERR_QOS_INT_PRIORITY - Invalid priority.
  351. * Note:
  352. * Priority of CPU tag assignment for internal asic priority, and it is used for queue usage and packet scheduling.
  353. */
  354. rtk_api_ret_t rtk_cpu_priRemap_set(rtk_pri_t int_pri, rtk_pri_t new_pri)
  355. {
  356. rtk_api_ret_t retVal;
  357. /* Check initialization state */
  358. RTK_CHK_INIT_STATE();
  359. if (new_pri > RTL8367C_PRIMAX || int_pri > RTL8367C_PRIMAX)
  360. return RT_ERR_VLAN_PRIORITY;
  361. if ((retVal = rtl8367c_setAsicCputagPriorityRemapping(int_pri, new_pri)) != RT_ERR_OK)
  362. return retVal;
  363. return RT_ERR_OK;
  364. }
  365. /* Function Name:
  366. * rtk_cpu_priRemap_get
  367. * Description:
  368. * Configure CPU priorities mapping to internal absolute priority.
  369. * Input:
  370. * int_pri - internal priority value.
  371. * Output:
  372. * pNew_pri - new internal priority value.
  373. * Return:
  374. * RT_ERR_OK - OK
  375. * RT_ERR_FAILED - Failed
  376. * RT_ERR_SMI - SMI access error
  377. * RT_ERR_INPUT - Invalid input parameters.
  378. * RT_ERR_VLAN_PRIORITY - Invalid 1p priority.
  379. * RT_ERR_QOS_INT_PRIORITY - Invalid priority.
  380. * Note:
  381. * Priority of CPU tag assignment for internal asic priority, and it is used for queue usage and packet scheduling.
  382. */
  383. rtk_api_ret_t rtk_cpu_priRemap_get(rtk_pri_t int_pri, rtk_pri_t *pNew_pri)
  384. {
  385. rtk_api_ret_t retVal;
  386. /* Check initialization state */
  387. RTK_CHK_INIT_STATE();
  388. if(NULL == pNew_pri)
  389. return RT_ERR_NULL_POINTER;
  390. if (int_pri > RTL8367C_PRIMAX)
  391. return RT_ERR_QOS_INT_PRIORITY;
  392. if ((retVal = rtl8367c_getAsicCputagPriorityRemapping(int_pri, pNew_pri)) != RT_ERR_OK)
  393. return retVal;
  394. return RT_ERR_OK;
  395. }
  396. /* Function Name:
  397. * rtk_cpu_acceptLength_set
  398. * Description:
  399. * Set CPU accept length.
  400. * Input:
  401. * length - CPU tag length.
  402. * Output:
  403. * None
  404. * Return:
  405. * RT_ERR_OK - OK
  406. * RT_ERR_FAILED - Failed
  407. * RT_ERR_SMI - SMI access error
  408. * RT_ERR_INPUT - Invalid input.
  409. * Note:
  410. * The API can set CPU accept length.
  411. */
  412. rtk_api_ret_t rtk_cpu_acceptLength_set(rtk_cpu_rx_length_t length)
  413. {
  414. rtk_api_ret_t retVal;
  415. /* Check initialization state */
  416. RTK_CHK_INIT_STATE();
  417. if (length >= CPU_RX_END)
  418. return RT_ERR_INPUT;
  419. if ((retVal = rtl8367c_setAsicCputagRxMinLength(length)) != RT_ERR_OK)
  420. return retVal;
  421. return RT_ERR_OK;
  422. }
  423. /* Function Name:
  424. * rtk_cpu_acceptLength_get
  425. * Description:
  426. * Get CPU accept length.
  427. * Input:
  428. * None
  429. * Output:
  430. * pLength - CPU tag length.
  431. * Return:
  432. * RT_ERR_OK - OK
  433. * RT_ERR_FAILED - Failed
  434. * RT_ERR_SMI - SMI access error
  435. * RT_ERR_INPUT - Invalid input.
  436. * Note:
  437. * The API can get CPU accept length.
  438. */
  439. rtk_api_ret_t rtk_cpu_acceptLength_get(rtk_cpu_rx_length_t *pLength)
  440. {
  441. rtk_api_ret_t retVal;
  442. /* Check initialization state */
  443. RTK_CHK_INIT_STATE();
  444. if(NULL == pLength)
  445. return RT_ERR_NULL_POINTER;
  446. if ((retVal = rtl8367c_getAsicCputagRxMinLength(pLength)) != RT_ERR_OK)
  447. return retVal;
  448. return RT_ERR_OK;
  449. }