am65-cpsw-qos.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Texas Instruments K3 AM65 Ethernet QoS submodule
  3. * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
  4. *
  5. * quality of service module includes:
  6. * Enhanced Scheduler Traffic (EST - P802.1Qbv/D2.2)
  7. */
  8. #include <linux/pm_runtime.h>
  9. #include <linux/time.h>
  10. #include <net/pkt_cls.h>
  11. #include "am65-cpsw-nuss.h"
  12. #include "am65-cpsw-qos.h"
  13. #include "am65-cpts.h"
  14. #include "cpsw_ale.h"
  15. #define AM65_CPSW_REG_CTL 0x004
  16. #define AM65_CPSW_PN_REG_CTL 0x004
  17. #define AM65_CPSW_PN_REG_FIFO_STATUS 0x050
  18. #define AM65_CPSW_PN_REG_EST_CTL 0x060
  19. /* AM65_CPSW_REG_CTL register fields */
  20. #define AM65_CPSW_CTL_EST_EN BIT(18)
  21. /* AM65_CPSW_PN_REG_CTL register fields */
  22. #define AM65_CPSW_PN_CTL_EST_PORT_EN BIT(17)
  23. /* AM65_CPSW_PN_REG_EST_CTL register fields */
  24. #define AM65_CPSW_PN_EST_ONEBUF BIT(0)
  25. #define AM65_CPSW_PN_EST_BUFSEL BIT(1)
  26. #define AM65_CPSW_PN_EST_TS_EN BIT(2)
  27. #define AM65_CPSW_PN_EST_TS_FIRST BIT(3)
  28. #define AM65_CPSW_PN_EST_ONEPRI BIT(4)
  29. #define AM65_CPSW_PN_EST_TS_PRI_MSK GENMASK(7, 5)
  30. /* AM65_CPSW_PN_REG_FIFO_STATUS register fields */
  31. #define AM65_CPSW_PN_FST_TX_PRI_ACTIVE_MSK GENMASK(7, 0)
  32. #define AM65_CPSW_PN_FST_TX_E_MAC_ALLOW_MSK GENMASK(15, 8)
  33. #define AM65_CPSW_PN_FST_EST_CNT_ERR BIT(16)
  34. #define AM65_CPSW_PN_FST_EST_ADD_ERR BIT(17)
  35. #define AM65_CPSW_PN_FST_EST_BUFACT BIT(18)
  36. /* EST FETCH COMMAND RAM */
  37. #define AM65_CPSW_FETCH_RAM_CMD_NUM 0x80
  38. #define AM65_CPSW_FETCH_CNT_MSK GENMASK(21, 8)
  39. #define AM65_CPSW_FETCH_CNT_MAX (AM65_CPSW_FETCH_CNT_MSK >> 8)
  40. #define AM65_CPSW_FETCH_CNT_OFFSET 8
  41. #define AM65_CPSW_FETCH_ALLOW_MSK GENMASK(7, 0)
  42. #define AM65_CPSW_FETCH_ALLOW_MAX AM65_CPSW_FETCH_ALLOW_MSK
  43. enum timer_act {
  44. TACT_PROG, /* need program timer */
  45. TACT_NEED_STOP, /* need stop first */
  46. TACT_SKIP_PROG, /* just buffer can be updated */
  47. };
  48. static int am65_cpsw_port_est_enabled(struct am65_cpsw_port *port)
  49. {
  50. return port->qos.est_oper || port->qos.est_admin;
  51. }
  52. static void am65_cpsw_est_enable(struct am65_cpsw_common *common, int enable)
  53. {
  54. u32 val;
  55. val = readl(common->cpsw_base + AM65_CPSW_REG_CTL);
  56. if (enable)
  57. val |= AM65_CPSW_CTL_EST_EN;
  58. else
  59. val &= ~AM65_CPSW_CTL_EST_EN;
  60. writel(val, common->cpsw_base + AM65_CPSW_REG_CTL);
  61. common->est_enabled = enable;
  62. }
  63. static void am65_cpsw_port_est_enable(struct am65_cpsw_port *port, int enable)
  64. {
  65. u32 val;
  66. val = readl(port->port_base + AM65_CPSW_PN_REG_CTL);
  67. if (enable)
  68. val |= AM65_CPSW_PN_CTL_EST_PORT_EN;
  69. else
  70. val &= ~AM65_CPSW_PN_CTL_EST_PORT_EN;
  71. writel(val, port->port_base + AM65_CPSW_PN_REG_CTL);
  72. }
  73. /* target new EST RAM buffer, actual toggle happens after cycle completion */
  74. static void am65_cpsw_port_est_assign_buf_num(struct net_device *ndev,
  75. int buf_num)
  76. {
  77. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  78. u32 val;
  79. val = readl(port->port_base + AM65_CPSW_PN_REG_EST_CTL);
  80. if (buf_num)
  81. val |= AM65_CPSW_PN_EST_BUFSEL;
  82. else
  83. val &= ~AM65_CPSW_PN_EST_BUFSEL;
  84. writel(val, port->port_base + AM65_CPSW_PN_REG_EST_CTL);
  85. }
  86. /* am65_cpsw_port_est_is_swapped() - Indicate if h/w is transitioned
  87. * admin -> oper or not
  88. *
  89. * Return true if already transitioned. i.e oper is equal to admin and buf
  90. * numbers match (est_oper->buf match with est_admin->buf).
  91. * false if before transition. i.e oper is not equal to admin, (i.e a
  92. * previous admin command is waiting to be transitioned to oper state
  93. * and est_oper->buf not match with est_oper->buf).
  94. */
  95. static int am65_cpsw_port_est_is_swapped(struct net_device *ndev, int *oper,
  96. int *admin)
  97. {
  98. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  99. u32 val;
  100. val = readl(port->port_base + AM65_CPSW_PN_REG_FIFO_STATUS);
  101. *oper = !!(val & AM65_CPSW_PN_FST_EST_BUFACT);
  102. val = readl(port->port_base + AM65_CPSW_PN_REG_EST_CTL);
  103. *admin = !!(val & AM65_CPSW_PN_EST_BUFSEL);
  104. return *admin == *oper;
  105. }
  106. /* am65_cpsw_port_est_get_free_buf_num() - Get free buffer number for
  107. * Admin to program the new schedule.
  108. *
  109. * Logic as follows:-
  110. * If oper is same as admin, return the other buffer (!oper) as the admin
  111. * buffer. If oper is not the same, driver let the current oper to continue
  112. * as it is in the process of transitioning from admin -> oper. So keep the
  113. * oper by selecting the same oper buffer by writing to EST_BUFSEL bit in
  114. * EST CTL register. In the second iteration they will match and code returns.
  115. * The actual buffer to write command is selected later before it is ready
  116. * to update the schedule.
  117. */
  118. static int am65_cpsw_port_est_get_free_buf_num(struct net_device *ndev)
  119. {
  120. int oper, admin;
  121. int roll = 2;
  122. while (roll--) {
  123. if (am65_cpsw_port_est_is_swapped(ndev, &oper, &admin))
  124. return !oper;
  125. /* admin is not set, so hinder transition as it's not allowed
  126. * to touch memory in-flight, by targeting same oper buf.
  127. */
  128. am65_cpsw_port_est_assign_buf_num(ndev, oper);
  129. dev_info(&ndev->dev,
  130. "Prev. EST admin cycle is in transit %d -> %d\n",
  131. oper, admin);
  132. }
  133. return admin;
  134. }
  135. static void am65_cpsw_admin_to_oper(struct net_device *ndev)
  136. {
  137. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  138. devm_kfree(&ndev->dev, port->qos.est_oper);
  139. port->qos.est_oper = port->qos.est_admin;
  140. port->qos.est_admin = NULL;
  141. }
  142. static void am65_cpsw_port_est_get_buf_num(struct net_device *ndev,
  143. struct am65_cpsw_est *est_new)
  144. {
  145. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  146. u32 val;
  147. val = readl(port->port_base + AM65_CPSW_PN_REG_EST_CTL);
  148. val &= ~AM65_CPSW_PN_EST_ONEBUF;
  149. writel(val, port->port_base + AM65_CPSW_PN_REG_EST_CTL);
  150. est_new->buf = am65_cpsw_port_est_get_free_buf_num(ndev);
  151. /* rolled buf num means changed buf while configuring */
  152. if (port->qos.est_oper && port->qos.est_admin &&
  153. est_new->buf == port->qos.est_oper->buf)
  154. am65_cpsw_admin_to_oper(ndev);
  155. }
  156. static void am65_cpsw_est_set(struct net_device *ndev, int enable)
  157. {
  158. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  159. struct am65_cpsw_common *common = port->common;
  160. int common_enable = 0;
  161. int i;
  162. am65_cpsw_port_est_enable(port, enable);
  163. for (i = 0; i < common->port_num; i++)
  164. common_enable |= am65_cpsw_port_est_enabled(&common->ports[i]);
  165. common_enable |= enable;
  166. am65_cpsw_est_enable(common, common_enable);
  167. }
  168. /* This update is supposed to be used in any routine before getting real state
  169. * of admin -> oper transition, particularly it's supposed to be used in some
  170. * generic routine for providing real state to Taprio Qdisc.
  171. */
  172. static void am65_cpsw_est_update_state(struct net_device *ndev)
  173. {
  174. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  175. int oper, admin;
  176. if (!port->qos.est_admin)
  177. return;
  178. if (!am65_cpsw_port_est_is_swapped(ndev, &oper, &admin))
  179. return;
  180. am65_cpsw_admin_to_oper(ndev);
  181. }
  182. /* Fetch command count it's number of bytes in Gigabit mode or nibbles in
  183. * 10/100Mb mode. So, having speed and time in ns, recalculate ns to number of
  184. * bytes/nibbles that can be sent while transmission on given speed.
  185. */
  186. static int am65_est_cmd_ns_to_cnt(u64 ns, int link_speed)
  187. {
  188. u64 temp;
  189. temp = ns * link_speed;
  190. if (link_speed < SPEED_1000)
  191. temp <<= 1;
  192. return DIV_ROUND_UP(temp, 8 * 1000);
  193. }
  194. static void __iomem *am65_cpsw_est_set_sched_cmds(void __iomem *addr,
  195. int fetch_cnt,
  196. int fetch_allow)
  197. {
  198. u32 prio_mask, cmd_fetch_cnt, cmd;
  199. do {
  200. if (fetch_cnt > AM65_CPSW_FETCH_CNT_MAX) {
  201. fetch_cnt -= AM65_CPSW_FETCH_CNT_MAX;
  202. cmd_fetch_cnt = AM65_CPSW_FETCH_CNT_MAX;
  203. } else {
  204. cmd_fetch_cnt = fetch_cnt;
  205. /* fetch count can't be less than 16? */
  206. if (cmd_fetch_cnt && cmd_fetch_cnt < 16)
  207. cmd_fetch_cnt = 16;
  208. fetch_cnt = 0;
  209. }
  210. prio_mask = fetch_allow & AM65_CPSW_FETCH_ALLOW_MSK;
  211. cmd = (cmd_fetch_cnt << AM65_CPSW_FETCH_CNT_OFFSET) | prio_mask;
  212. writel(cmd, addr);
  213. addr += 4;
  214. } while (fetch_cnt);
  215. return addr;
  216. }
  217. static int am65_cpsw_est_calc_cmd_num(struct net_device *ndev,
  218. struct tc_taprio_qopt_offload *taprio,
  219. int link_speed)
  220. {
  221. int i, cmd_cnt, cmd_sum = 0;
  222. u32 fetch_cnt;
  223. for (i = 0; i < taprio->num_entries; i++) {
  224. if (taprio->entries[i].command != TC_TAPRIO_CMD_SET_GATES) {
  225. dev_err(&ndev->dev, "Only SET command is supported");
  226. return -EINVAL;
  227. }
  228. fetch_cnt = am65_est_cmd_ns_to_cnt(taprio->entries[i].interval,
  229. link_speed);
  230. cmd_cnt = DIV_ROUND_UP(fetch_cnt, AM65_CPSW_FETCH_CNT_MAX);
  231. if (!cmd_cnt)
  232. cmd_cnt++;
  233. cmd_sum += cmd_cnt;
  234. if (!fetch_cnt)
  235. break;
  236. }
  237. return cmd_sum;
  238. }
  239. static int am65_cpsw_est_check_scheds(struct net_device *ndev,
  240. struct am65_cpsw_est *est_new)
  241. {
  242. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  243. int cmd_num;
  244. cmd_num = am65_cpsw_est_calc_cmd_num(ndev, &est_new->taprio,
  245. port->qos.link_speed);
  246. if (cmd_num < 0)
  247. return cmd_num;
  248. if (cmd_num > AM65_CPSW_FETCH_RAM_CMD_NUM / 2) {
  249. dev_err(&ndev->dev, "No fetch RAM");
  250. return -ENOMEM;
  251. }
  252. return 0;
  253. }
  254. static void am65_cpsw_est_set_sched_list(struct net_device *ndev,
  255. struct am65_cpsw_est *est_new)
  256. {
  257. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  258. u32 fetch_cnt, fetch_allow, all_fetch_allow = 0;
  259. void __iomem *ram_addr, *max_ram_addr;
  260. struct tc_taprio_sched_entry *entry;
  261. int i, ram_size;
  262. ram_addr = port->fetch_ram_base;
  263. ram_size = AM65_CPSW_FETCH_RAM_CMD_NUM * 2;
  264. ram_addr += est_new->buf * ram_size;
  265. max_ram_addr = ram_size + ram_addr;
  266. for (i = 0; i < est_new->taprio.num_entries; i++) {
  267. entry = &est_new->taprio.entries[i];
  268. fetch_cnt = am65_est_cmd_ns_to_cnt(entry->interval,
  269. port->qos.link_speed);
  270. fetch_allow = entry->gate_mask;
  271. if (fetch_allow > AM65_CPSW_FETCH_ALLOW_MAX)
  272. dev_dbg(&ndev->dev, "fetch_allow > 8 bits: %d\n",
  273. fetch_allow);
  274. ram_addr = am65_cpsw_est_set_sched_cmds(ram_addr, fetch_cnt,
  275. fetch_allow);
  276. if (!fetch_cnt && i < est_new->taprio.num_entries - 1) {
  277. dev_info(&ndev->dev,
  278. "next scheds after %d have no impact", i + 1);
  279. break;
  280. }
  281. all_fetch_allow |= fetch_allow;
  282. }
  283. /* end cmd, enabling non-timed queues for potential over cycle time */
  284. if (ram_addr < max_ram_addr)
  285. writel(~all_fetch_allow & AM65_CPSW_FETCH_ALLOW_MSK, ram_addr);
  286. }
  287. /*
  288. * Enable ESTf periodic output, set cycle start time and interval.
  289. */
  290. static int am65_cpsw_timer_set(struct net_device *ndev,
  291. struct am65_cpsw_est *est_new)
  292. {
  293. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  294. struct am65_cpsw_common *common = port->common;
  295. struct am65_cpts *cpts = common->cpts;
  296. struct am65_cpts_estf_cfg cfg;
  297. cfg.ns_period = est_new->taprio.cycle_time;
  298. cfg.ns_start = est_new->taprio.base_time;
  299. return am65_cpts_estf_enable(cpts, port->port_id - 1, &cfg);
  300. }
  301. static void am65_cpsw_timer_stop(struct net_device *ndev)
  302. {
  303. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  304. struct am65_cpts *cpts = port->common->cpts;
  305. am65_cpts_estf_disable(cpts, port->port_id - 1);
  306. }
  307. static enum timer_act am65_cpsw_timer_act(struct net_device *ndev,
  308. struct am65_cpsw_est *est_new)
  309. {
  310. struct tc_taprio_qopt_offload *taprio_oper, *taprio_new;
  311. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  312. struct am65_cpts *cpts = port->common->cpts;
  313. u64 cur_time;
  314. s64 diff;
  315. if (!port->qos.est_oper)
  316. return TACT_PROG;
  317. taprio_new = &est_new->taprio;
  318. taprio_oper = &port->qos.est_oper->taprio;
  319. if (taprio_new->cycle_time != taprio_oper->cycle_time)
  320. return TACT_NEED_STOP;
  321. /* in order to avoid timer reset get base_time form oper taprio */
  322. if (!taprio_new->base_time && taprio_oper)
  323. taprio_new->base_time = taprio_oper->base_time;
  324. if (taprio_new->base_time == taprio_oper->base_time)
  325. return TACT_SKIP_PROG;
  326. /* base times are cycle synchronized */
  327. diff = taprio_new->base_time - taprio_oper->base_time;
  328. diff = diff < 0 ? -diff : diff;
  329. if (diff % taprio_new->cycle_time)
  330. return TACT_NEED_STOP;
  331. cur_time = am65_cpts_ns_gettime(cpts);
  332. if (taprio_new->base_time <= cur_time + taprio_new->cycle_time)
  333. return TACT_SKIP_PROG;
  334. /* TODO: Admin schedule at future time is not currently supported */
  335. return TACT_NEED_STOP;
  336. }
  337. static void am65_cpsw_stop_est(struct net_device *ndev)
  338. {
  339. am65_cpsw_est_set(ndev, 0);
  340. am65_cpsw_timer_stop(ndev);
  341. }
  342. static void am65_cpsw_purge_est(struct net_device *ndev)
  343. {
  344. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  345. am65_cpsw_stop_est(ndev);
  346. devm_kfree(&ndev->dev, port->qos.est_admin);
  347. devm_kfree(&ndev->dev, port->qos.est_oper);
  348. port->qos.est_oper = NULL;
  349. port->qos.est_admin = NULL;
  350. }
  351. static int am65_cpsw_configure_taprio(struct net_device *ndev,
  352. struct am65_cpsw_est *est_new)
  353. {
  354. struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
  355. struct am65_cpts *cpts = common->cpts;
  356. int ret = 0, tact = TACT_PROG;
  357. am65_cpsw_est_update_state(ndev);
  358. if (!est_new->taprio.enable) {
  359. am65_cpsw_stop_est(ndev);
  360. return ret;
  361. }
  362. ret = am65_cpsw_est_check_scheds(ndev, est_new);
  363. if (ret < 0)
  364. return ret;
  365. tact = am65_cpsw_timer_act(ndev, est_new);
  366. if (tact == TACT_NEED_STOP) {
  367. dev_err(&ndev->dev,
  368. "Can't toggle estf timer, stop taprio first");
  369. return -EINVAL;
  370. }
  371. if (tact == TACT_PROG)
  372. am65_cpsw_timer_stop(ndev);
  373. if (!est_new->taprio.base_time)
  374. est_new->taprio.base_time = am65_cpts_ns_gettime(cpts);
  375. am65_cpsw_port_est_get_buf_num(ndev, est_new);
  376. am65_cpsw_est_set_sched_list(ndev, est_new);
  377. am65_cpsw_port_est_assign_buf_num(ndev, est_new->buf);
  378. am65_cpsw_est_set(ndev, est_new->taprio.enable);
  379. if (tact == TACT_PROG) {
  380. ret = am65_cpsw_timer_set(ndev, est_new);
  381. if (ret) {
  382. dev_err(&ndev->dev, "Failed to set cycle time");
  383. return ret;
  384. }
  385. }
  386. return ret;
  387. }
  388. static void am65_cpsw_cp_taprio(struct tc_taprio_qopt_offload *from,
  389. struct tc_taprio_qopt_offload *to)
  390. {
  391. int i;
  392. *to = *from;
  393. for (i = 0; i < from->num_entries; i++)
  394. to->entries[i] = from->entries[i];
  395. }
  396. static int am65_cpsw_set_taprio(struct net_device *ndev, void *type_data)
  397. {
  398. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  399. struct tc_taprio_qopt_offload *taprio = type_data;
  400. struct am65_cpsw_est *est_new;
  401. int ret = 0;
  402. if (taprio->cycle_time_extension) {
  403. dev_err(&ndev->dev, "Failed to set cycle time extension");
  404. return -EOPNOTSUPP;
  405. }
  406. est_new = devm_kzalloc(&ndev->dev,
  407. struct_size(est_new, taprio.entries, taprio->num_entries),
  408. GFP_KERNEL);
  409. if (!est_new)
  410. return -ENOMEM;
  411. am65_cpsw_cp_taprio(taprio, &est_new->taprio);
  412. ret = am65_cpsw_configure_taprio(ndev, est_new);
  413. if (!ret) {
  414. if (taprio->enable) {
  415. devm_kfree(&ndev->dev, port->qos.est_admin);
  416. port->qos.est_admin = est_new;
  417. } else {
  418. devm_kfree(&ndev->dev, est_new);
  419. am65_cpsw_purge_est(ndev);
  420. }
  421. } else {
  422. devm_kfree(&ndev->dev, est_new);
  423. }
  424. return ret;
  425. }
  426. static void am65_cpsw_est_link_up(struct net_device *ndev, int link_speed)
  427. {
  428. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  429. ktime_t cur_time;
  430. s64 delta;
  431. port->qos.link_speed = link_speed;
  432. if (!am65_cpsw_port_est_enabled(port))
  433. return;
  434. if (port->qos.link_down_time) {
  435. cur_time = ktime_get();
  436. delta = ktime_us_delta(cur_time, port->qos.link_down_time);
  437. if (delta > USEC_PER_SEC) {
  438. dev_err(&ndev->dev,
  439. "Link has been lost too long, stopping TAS");
  440. goto purge_est;
  441. }
  442. }
  443. return;
  444. purge_est:
  445. am65_cpsw_purge_est(ndev);
  446. }
  447. static int am65_cpsw_setup_taprio(struct net_device *ndev, void *type_data)
  448. {
  449. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  450. struct am65_cpsw_common *common = port->common;
  451. if (!IS_ENABLED(CONFIG_TI_AM65_CPSW_TAS))
  452. return -ENODEV;
  453. if (!netif_running(ndev)) {
  454. dev_err(&ndev->dev, "interface is down, link speed unknown\n");
  455. return -ENETDOWN;
  456. }
  457. if (common->pf_p0_rx_ptype_rrobin) {
  458. dev_err(&ndev->dev,
  459. "p0-rx-ptype-rrobin flag conflicts with taprio qdisc\n");
  460. return -EINVAL;
  461. }
  462. if (port->qos.link_speed == SPEED_UNKNOWN)
  463. return -ENOLINK;
  464. return am65_cpsw_set_taprio(ndev, type_data);
  465. }
  466. static int am65_cpsw_qos_clsflower_add_policer(struct am65_cpsw_port *port,
  467. struct netlink_ext_ack *extack,
  468. struct flow_cls_offload *cls,
  469. u64 rate_pkt_ps)
  470. {
  471. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  472. struct flow_dissector *dissector = rule->match.dissector;
  473. static const u8 mc_mac[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
  474. struct am65_cpsw_qos *qos = &port->qos;
  475. struct flow_match_eth_addrs match;
  476. int ret;
  477. if (dissector->used_keys &
  478. ~(BIT(FLOW_DISSECTOR_KEY_BASIC) |
  479. BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  480. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS))) {
  481. NL_SET_ERR_MSG_MOD(extack,
  482. "Unsupported keys used");
  483. return -EOPNOTSUPP;
  484. }
  485. if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  486. NL_SET_ERR_MSG_MOD(extack, "Not matching on eth address");
  487. return -EOPNOTSUPP;
  488. }
  489. flow_rule_match_eth_addrs(rule, &match);
  490. if (!is_zero_ether_addr(match.mask->src)) {
  491. NL_SET_ERR_MSG_MOD(extack,
  492. "Matching on source MAC not supported");
  493. return -EOPNOTSUPP;
  494. }
  495. if (is_broadcast_ether_addr(match.key->dst) &&
  496. is_broadcast_ether_addr(match.mask->dst)) {
  497. ret = cpsw_ale_rx_ratelimit_bc(port->common->ale, port->port_id, rate_pkt_ps);
  498. if (ret)
  499. return ret;
  500. qos->ale_bc_ratelimit.cookie = cls->cookie;
  501. qos->ale_bc_ratelimit.rate_packet_ps = rate_pkt_ps;
  502. } else if (ether_addr_equal_unaligned(match.key->dst, mc_mac) &&
  503. ether_addr_equal_unaligned(match.mask->dst, mc_mac)) {
  504. ret = cpsw_ale_rx_ratelimit_mc(port->common->ale, port->port_id, rate_pkt_ps);
  505. if (ret)
  506. return ret;
  507. qos->ale_mc_ratelimit.cookie = cls->cookie;
  508. qos->ale_mc_ratelimit.rate_packet_ps = rate_pkt_ps;
  509. } else {
  510. NL_SET_ERR_MSG_MOD(extack, "Not supported matching key");
  511. return -EOPNOTSUPP;
  512. }
  513. return 0;
  514. }
  515. static int am65_cpsw_qos_clsflower_policer_validate(const struct flow_action *action,
  516. const struct flow_action_entry *act,
  517. struct netlink_ext_ack *extack)
  518. {
  519. if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
  520. NL_SET_ERR_MSG_MOD(extack,
  521. "Offload not supported when exceed action is not drop");
  522. return -EOPNOTSUPP;
  523. }
  524. if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
  525. act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
  526. NL_SET_ERR_MSG_MOD(extack,
  527. "Offload not supported when conform action is not pipe or ok");
  528. return -EOPNOTSUPP;
  529. }
  530. if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
  531. !flow_action_is_last_entry(action, act)) {
  532. NL_SET_ERR_MSG_MOD(extack,
  533. "Offload not supported when conform action is ok, but action is not last");
  534. return -EOPNOTSUPP;
  535. }
  536. if (act->police.rate_bytes_ps || act->police.peakrate_bytes_ps ||
  537. act->police.avrate || act->police.overhead) {
  538. NL_SET_ERR_MSG_MOD(extack,
  539. "Offload not supported when bytes per second/peakrate/avrate/overhead is configured");
  540. return -EOPNOTSUPP;
  541. }
  542. return 0;
  543. }
  544. static int am65_cpsw_qos_configure_clsflower(struct am65_cpsw_port *port,
  545. struct flow_cls_offload *cls)
  546. {
  547. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  548. struct netlink_ext_ack *extack = cls->common.extack;
  549. const struct flow_action_entry *act;
  550. int i, ret;
  551. flow_action_for_each(i, act, &rule->action) {
  552. switch (act->id) {
  553. case FLOW_ACTION_POLICE:
  554. ret = am65_cpsw_qos_clsflower_policer_validate(&rule->action, act, extack);
  555. if (ret)
  556. return ret;
  557. return am65_cpsw_qos_clsflower_add_policer(port, extack, cls,
  558. act->police.rate_pkt_ps);
  559. default:
  560. NL_SET_ERR_MSG_MOD(extack,
  561. "Action not supported");
  562. return -EOPNOTSUPP;
  563. }
  564. }
  565. return -EOPNOTSUPP;
  566. }
  567. static int am65_cpsw_qos_delete_clsflower(struct am65_cpsw_port *port, struct flow_cls_offload *cls)
  568. {
  569. struct am65_cpsw_qos *qos = &port->qos;
  570. if (cls->cookie == qos->ale_bc_ratelimit.cookie) {
  571. qos->ale_bc_ratelimit.cookie = 0;
  572. qos->ale_bc_ratelimit.rate_packet_ps = 0;
  573. cpsw_ale_rx_ratelimit_bc(port->common->ale, port->port_id, 0);
  574. }
  575. if (cls->cookie == qos->ale_mc_ratelimit.cookie) {
  576. qos->ale_mc_ratelimit.cookie = 0;
  577. qos->ale_mc_ratelimit.rate_packet_ps = 0;
  578. cpsw_ale_rx_ratelimit_mc(port->common->ale, port->port_id, 0);
  579. }
  580. return 0;
  581. }
  582. static int am65_cpsw_qos_setup_tc_clsflower(struct am65_cpsw_port *port,
  583. struct flow_cls_offload *cls_flower)
  584. {
  585. switch (cls_flower->command) {
  586. case FLOW_CLS_REPLACE:
  587. return am65_cpsw_qos_configure_clsflower(port, cls_flower);
  588. case FLOW_CLS_DESTROY:
  589. return am65_cpsw_qos_delete_clsflower(port, cls_flower);
  590. default:
  591. return -EOPNOTSUPP;
  592. }
  593. }
  594. static int am65_cpsw_qos_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  595. {
  596. struct am65_cpsw_port *port = cb_priv;
  597. if (!tc_cls_can_offload_and_chain0(port->ndev, type_data))
  598. return -EOPNOTSUPP;
  599. switch (type) {
  600. case TC_SETUP_CLSFLOWER:
  601. return am65_cpsw_qos_setup_tc_clsflower(port, type_data);
  602. default:
  603. return -EOPNOTSUPP;
  604. }
  605. }
  606. static LIST_HEAD(am65_cpsw_qos_block_cb_list);
  607. static int am65_cpsw_qos_setup_tc_block(struct net_device *ndev, struct flow_block_offload *f)
  608. {
  609. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  610. return flow_block_cb_setup_simple(f, &am65_cpsw_qos_block_cb_list,
  611. am65_cpsw_qos_setup_tc_block_cb,
  612. port, port, true);
  613. }
  614. int am65_cpsw_qos_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
  615. void *type_data)
  616. {
  617. switch (type) {
  618. case TC_SETUP_QDISC_TAPRIO:
  619. return am65_cpsw_setup_taprio(ndev, type_data);
  620. case TC_SETUP_BLOCK:
  621. return am65_cpsw_qos_setup_tc_block(ndev, type_data);
  622. default:
  623. return -EOPNOTSUPP;
  624. }
  625. }
  626. void am65_cpsw_qos_link_up(struct net_device *ndev, int link_speed)
  627. {
  628. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  629. if (!IS_ENABLED(CONFIG_TI_AM65_CPSW_TAS))
  630. return;
  631. am65_cpsw_est_link_up(ndev, link_speed);
  632. port->qos.link_down_time = 0;
  633. }
  634. void am65_cpsw_qos_link_down(struct net_device *ndev)
  635. {
  636. struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
  637. if (!IS_ENABLED(CONFIG_TI_AM65_CPSW_TAS))
  638. return;
  639. if (!port->qos.link_down_time)
  640. port->qos.link_down_time = ktime_get();
  641. port->qos.link_speed = SPEED_UNKNOWN;
  642. }