cpsw_priv.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments Ethernet Switch Driver
  4. *
  5. * Copyright (C) 2019 Texas Instruments
  6. */
  7. #include <linux/bpf.h>
  8. #include <linux/bpf_trace.h>
  9. #include <linux/if_ether.h>
  10. #include <linux/if_vlan.h>
  11. #include <linux/kmemleak.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/net_tstamp.h>
  15. #include <linux/of.h>
  16. #include <linux/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/skbuff.h>
  20. #include <net/page_pool/helpers.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/pkt_sched.h>
  23. #include "cpsw.h"
  24. #include "cpts.h"
  25. #include "cpsw_ale.h"
  26. #include "cpsw_priv.h"
  27. #include "cpsw_sl.h"
  28. #include "davinci_cpdma.h"
  29. #define CPTS_N_ETX_TS 4
  30. int (*cpsw_slave_index)(struct cpsw_common *cpsw, struct cpsw_priv *priv);
  31. void cpsw_intr_enable(struct cpsw_common *cpsw)
  32. {
  33. writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
  34. writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
  35. cpdma_ctlr_int_ctrl(cpsw->dma, true);
  36. }
  37. void cpsw_intr_disable(struct cpsw_common *cpsw)
  38. {
  39. writel_relaxed(0, &cpsw->wr_regs->tx_en);
  40. writel_relaxed(0, &cpsw->wr_regs->rx_en);
  41. cpdma_ctlr_int_ctrl(cpsw->dma, false);
  42. }
  43. void cpsw_tx_handler(void *token, int len, int status)
  44. {
  45. struct cpsw_meta_xdp *xmeta;
  46. struct xdp_frame *xdpf;
  47. struct net_device *ndev;
  48. struct netdev_queue *txq;
  49. struct sk_buff *skb;
  50. int ch;
  51. if (cpsw_is_xdpf_handle(token)) {
  52. xdpf = cpsw_handle_to_xdpf(token);
  53. xmeta = (void *)xdpf + CPSW_XMETA_OFFSET;
  54. ndev = xmeta->ndev;
  55. ch = xmeta->ch;
  56. xdp_return_frame(xdpf);
  57. } else {
  58. skb = token;
  59. ndev = skb->dev;
  60. ch = skb_get_queue_mapping(skb);
  61. cpts_tx_timestamp(ndev_to_cpsw(ndev)->cpts, skb);
  62. dev_kfree_skb_any(skb);
  63. }
  64. /* Check whether the queue is stopped due to stalled tx dma, if the
  65. * queue is stopped then start the queue as we have free desc for tx
  66. */
  67. txq = netdev_get_tx_queue(ndev, ch);
  68. if (unlikely(netif_tx_queue_stopped(txq)))
  69. netif_tx_wake_queue(txq);
  70. ndev->stats.tx_packets++;
  71. ndev->stats.tx_bytes += len;
  72. }
  73. irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
  74. {
  75. struct cpsw_common *cpsw = dev_id;
  76. writel(0, &cpsw->wr_regs->tx_en);
  77. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
  78. if (cpsw->quirk_irq) {
  79. disable_irq_nosync(cpsw->irqs_table[1]);
  80. cpsw->tx_irq_disabled = true;
  81. }
  82. napi_schedule(&cpsw->napi_tx);
  83. return IRQ_HANDLED;
  84. }
  85. irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
  86. {
  87. struct cpsw_common *cpsw = dev_id;
  88. writel(0, &cpsw->wr_regs->rx_en);
  89. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
  90. if (cpsw->quirk_irq) {
  91. disable_irq_nosync(cpsw->irqs_table[0]);
  92. cpsw->rx_irq_disabled = true;
  93. }
  94. napi_schedule(&cpsw->napi_rx);
  95. return IRQ_HANDLED;
  96. }
  97. irqreturn_t cpsw_misc_interrupt(int irq, void *dev_id)
  98. {
  99. struct cpsw_common *cpsw = dev_id;
  100. writel(0, &cpsw->wr_regs->misc_en);
  101. cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_MISC);
  102. cpts_misc_interrupt(cpsw->cpts);
  103. writel(0x10, &cpsw->wr_regs->misc_en);
  104. return IRQ_HANDLED;
  105. }
  106. int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
  107. {
  108. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  109. int num_tx, cur_budget, ch;
  110. u32 ch_map;
  111. struct cpsw_vector *txv;
  112. /* process every unprocessed channel */
  113. ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
  114. for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
  115. if (!(ch_map & 0x80))
  116. continue;
  117. txv = &cpsw->txv[ch];
  118. if (unlikely(txv->budget > budget - num_tx))
  119. cur_budget = budget - num_tx;
  120. else
  121. cur_budget = txv->budget;
  122. num_tx += cpdma_chan_process(txv->ch, cur_budget);
  123. if (num_tx >= budget)
  124. break;
  125. }
  126. if (num_tx < budget) {
  127. napi_complete(napi_tx);
  128. writel(0xff, &cpsw->wr_regs->tx_en);
  129. }
  130. return num_tx;
  131. }
  132. int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
  133. {
  134. struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
  135. int num_tx;
  136. num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
  137. if (num_tx < budget) {
  138. napi_complete(napi_tx);
  139. writel(0xff, &cpsw->wr_regs->tx_en);
  140. if (cpsw->tx_irq_disabled) {
  141. cpsw->tx_irq_disabled = false;
  142. enable_irq(cpsw->irqs_table[1]);
  143. }
  144. }
  145. return num_tx;
  146. }
  147. int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
  148. {
  149. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  150. int num_rx, cur_budget, ch;
  151. u32 ch_map;
  152. struct cpsw_vector *rxv;
  153. /* process every unprocessed channel */
  154. ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
  155. for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
  156. if (!(ch_map & 0x01))
  157. continue;
  158. rxv = &cpsw->rxv[ch];
  159. if (unlikely(rxv->budget > budget - num_rx))
  160. cur_budget = budget - num_rx;
  161. else
  162. cur_budget = rxv->budget;
  163. num_rx += cpdma_chan_process(rxv->ch, cur_budget);
  164. if (num_rx >= budget)
  165. break;
  166. }
  167. if (num_rx < budget) {
  168. napi_complete_done(napi_rx, num_rx);
  169. writel(0xff, &cpsw->wr_regs->rx_en);
  170. }
  171. return num_rx;
  172. }
  173. int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
  174. {
  175. struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
  176. int num_rx;
  177. num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
  178. if (num_rx < budget) {
  179. napi_complete_done(napi_rx, num_rx);
  180. writel(0xff, &cpsw->wr_regs->rx_en);
  181. if (cpsw->rx_irq_disabled) {
  182. cpsw->rx_irq_disabled = false;
  183. enable_irq(cpsw->irqs_table[0]);
  184. }
  185. }
  186. return num_rx;
  187. }
  188. void cpsw_rx_vlan_encap(struct sk_buff *skb)
  189. {
  190. struct cpsw_priv *priv = netdev_priv(skb->dev);
  191. u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
  192. struct cpsw_common *cpsw = priv->cpsw;
  193. u16 vtag, vid, prio, pkt_type;
  194. /* Remove VLAN header encapsulation word */
  195. skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
  196. pkt_type = (rx_vlan_encap_hdr >>
  197. CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
  198. CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
  199. /* Ignore unknown & Priority-tagged packets*/
  200. if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
  201. pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
  202. return;
  203. vid = (rx_vlan_encap_hdr >>
  204. CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
  205. VLAN_VID_MASK;
  206. /* Ignore vid 0 and pass packet as is */
  207. if (!vid)
  208. return;
  209. /* Untag P0 packets if set for vlan */
  210. if (!cpsw_ale_get_vlan_p0_untag(cpsw->ale, vid)) {
  211. prio = (rx_vlan_encap_hdr >>
  212. CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
  213. CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
  214. vtag = (prio << VLAN_PRIO_SHIFT) | vid;
  215. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
  216. }
  217. /* strip vlan tag for VLAN-tagged packet */
  218. if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
  219. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  220. skb_pull(skb, VLAN_HLEN);
  221. }
  222. }
  223. void cpsw_set_slave_mac(struct cpsw_slave *slave, struct cpsw_priv *priv)
  224. {
  225. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  226. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  227. }
  228. void soft_reset(const char *module, void __iomem *reg)
  229. {
  230. unsigned long timeout = jiffies + HZ;
  231. writel_relaxed(1, reg);
  232. do {
  233. cpu_relax();
  234. } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
  235. WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
  236. }
  237. void cpsw_ndo_tx_timeout(struct net_device *ndev, unsigned int txqueue)
  238. {
  239. struct cpsw_priv *priv = netdev_priv(ndev);
  240. struct cpsw_common *cpsw = priv->cpsw;
  241. int ch;
  242. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  243. ndev->stats.tx_errors++;
  244. cpsw_intr_disable(cpsw);
  245. for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
  246. cpdma_chan_stop(cpsw->txv[ch].ch);
  247. cpdma_chan_start(cpsw->txv[ch].ch);
  248. }
  249. cpsw_intr_enable(cpsw);
  250. netif_trans_update(ndev);
  251. netif_tx_wake_all_queues(ndev);
  252. }
  253. static int cpsw_get_common_speed(struct cpsw_common *cpsw)
  254. {
  255. int i, speed;
  256. for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
  257. if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
  258. speed += cpsw->slaves[i].phy->speed;
  259. return speed;
  260. }
  261. int cpsw_need_resplit(struct cpsw_common *cpsw)
  262. {
  263. int i, rlim_ch_num;
  264. int speed, ch_rate;
  265. /* re-split resources only in case speed was changed */
  266. speed = cpsw_get_common_speed(cpsw);
  267. if (speed == cpsw->speed || !speed)
  268. return 0;
  269. cpsw->speed = speed;
  270. for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
  271. ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
  272. if (!ch_rate)
  273. break;
  274. rlim_ch_num++;
  275. }
  276. /* cases not dependent on speed */
  277. if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
  278. return 0;
  279. return 1;
  280. }
  281. void cpsw_split_res(struct cpsw_common *cpsw)
  282. {
  283. u32 consumed_rate = 0, bigest_rate = 0;
  284. struct cpsw_vector *txv = cpsw->txv;
  285. int i, ch_weight, rlim_ch_num = 0;
  286. int budget, bigest_rate_ch = 0;
  287. u32 ch_rate, max_rate;
  288. int ch_budget = 0;
  289. for (i = 0; i < cpsw->tx_ch_num; i++) {
  290. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  291. if (!ch_rate)
  292. continue;
  293. rlim_ch_num++;
  294. consumed_rate += ch_rate;
  295. }
  296. if (cpsw->tx_ch_num == rlim_ch_num) {
  297. max_rate = consumed_rate;
  298. } else if (!rlim_ch_num) {
  299. ch_budget = NAPI_POLL_WEIGHT / cpsw->tx_ch_num;
  300. bigest_rate = 0;
  301. max_rate = consumed_rate;
  302. } else {
  303. max_rate = cpsw->speed * 1000;
  304. /* if max_rate is less then expected due to reduced link speed,
  305. * split proportionally according next potential max speed
  306. */
  307. if (max_rate < consumed_rate)
  308. max_rate *= 10;
  309. if (max_rate < consumed_rate)
  310. max_rate *= 10;
  311. ch_budget = (consumed_rate * NAPI_POLL_WEIGHT) / max_rate;
  312. ch_budget = (NAPI_POLL_WEIGHT - ch_budget) /
  313. (cpsw->tx_ch_num - rlim_ch_num);
  314. bigest_rate = (max_rate - consumed_rate) /
  315. (cpsw->tx_ch_num - rlim_ch_num);
  316. }
  317. /* split tx weight/budget */
  318. budget = NAPI_POLL_WEIGHT;
  319. for (i = 0; i < cpsw->tx_ch_num; i++) {
  320. ch_rate = cpdma_chan_get_rate(txv[i].ch);
  321. if (ch_rate) {
  322. txv[i].budget = (ch_rate * NAPI_POLL_WEIGHT) / max_rate;
  323. if (!txv[i].budget)
  324. txv[i].budget++;
  325. if (ch_rate > bigest_rate) {
  326. bigest_rate_ch = i;
  327. bigest_rate = ch_rate;
  328. }
  329. ch_weight = (ch_rate * 100) / max_rate;
  330. if (!ch_weight)
  331. ch_weight++;
  332. cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
  333. } else {
  334. txv[i].budget = ch_budget;
  335. if (!bigest_rate_ch)
  336. bigest_rate_ch = i;
  337. cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
  338. }
  339. budget -= txv[i].budget;
  340. }
  341. if (budget)
  342. txv[bigest_rate_ch].budget += budget;
  343. /* split rx budget */
  344. budget = NAPI_POLL_WEIGHT;
  345. ch_budget = budget / cpsw->rx_ch_num;
  346. for (i = 0; i < cpsw->rx_ch_num; i++) {
  347. cpsw->rxv[i].budget = ch_budget;
  348. budget -= ch_budget;
  349. }
  350. if (budget)
  351. cpsw->rxv[0].budget += budget;
  352. }
  353. int cpsw_init_common(struct cpsw_common *cpsw, void __iomem *ss_regs,
  354. int ale_ageout, phys_addr_t desc_mem_phys,
  355. int descs_pool_size)
  356. {
  357. u32 slave_offset, sliver_offset, slave_size;
  358. struct cpsw_ale_params ale_params;
  359. struct cpsw_platform_data *data;
  360. struct cpdma_params dma_params;
  361. struct device *dev = cpsw->dev;
  362. struct device_node *cpts_node;
  363. void __iomem *cpts_regs;
  364. int ret = 0, i;
  365. data = &cpsw->data;
  366. cpsw->rx_ch_num = 1;
  367. cpsw->tx_ch_num = 1;
  368. cpsw->version = readl(&cpsw->regs->id_ver);
  369. memset(&dma_params, 0, sizeof(dma_params));
  370. memset(&ale_params, 0, sizeof(ale_params));
  371. switch (cpsw->version) {
  372. case CPSW_VERSION_1:
  373. cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  374. cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
  375. cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
  376. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  377. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  378. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  379. slave_offset = CPSW1_SLAVE_OFFSET;
  380. slave_size = CPSW1_SLAVE_SIZE;
  381. sliver_offset = CPSW1_SLIVER_OFFSET;
  382. dma_params.desc_mem_phys = 0;
  383. break;
  384. case CPSW_VERSION_2:
  385. case CPSW_VERSION_3:
  386. case CPSW_VERSION_4:
  387. cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  388. cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
  389. cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
  390. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  391. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  392. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  393. slave_offset = CPSW2_SLAVE_OFFSET;
  394. slave_size = CPSW2_SLAVE_SIZE;
  395. sliver_offset = CPSW2_SLIVER_OFFSET;
  396. dma_params.desc_mem_phys = desc_mem_phys;
  397. break;
  398. default:
  399. dev_err(dev, "unknown version 0x%08x\n", cpsw->version);
  400. return -ENODEV;
  401. }
  402. for (i = 0; i < cpsw->data.slaves; i++) {
  403. struct cpsw_slave *slave = &cpsw->slaves[i];
  404. void __iomem *regs = cpsw->regs;
  405. slave->slave_num = i;
  406. slave->data = &cpsw->data.slave_data[i];
  407. slave->regs = regs + slave_offset;
  408. slave->port_vlan = slave->data->dual_emac_res_vlan;
  409. slave->mac_sl = cpsw_sl_get("cpsw", dev, regs + sliver_offset);
  410. if (IS_ERR(slave->mac_sl))
  411. return PTR_ERR(slave->mac_sl);
  412. slave_offset += slave_size;
  413. sliver_offset += SLIVER_SIZE;
  414. }
  415. ale_params.dev = dev;
  416. ale_params.ale_ageout = ale_ageout;
  417. ale_params.ale_ports = CPSW_ALE_PORTS_NUM;
  418. ale_params.dev_id = "cpsw";
  419. ale_params.bus_freq = cpsw->bus_freq_mhz * 1000000;
  420. cpsw->ale = cpsw_ale_create(&ale_params);
  421. if (IS_ERR(cpsw->ale)) {
  422. dev_err(dev, "error initializing ale engine\n");
  423. return PTR_ERR(cpsw->ale);
  424. }
  425. dma_params.dev = dev;
  426. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  427. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  428. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  429. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  430. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  431. dma_params.num_chan = data->channels;
  432. dma_params.has_soft_reset = true;
  433. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  434. dma_params.desc_mem_size = data->bd_ram_size;
  435. dma_params.desc_align = 16;
  436. dma_params.has_ext_regs = true;
  437. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  438. dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
  439. dma_params.descs_pool_size = descs_pool_size;
  440. cpsw->dma = cpdma_ctlr_create(&dma_params);
  441. if (!cpsw->dma) {
  442. dev_err(dev, "error initializing dma\n");
  443. return -ENOMEM;
  444. }
  445. cpts_node = of_get_child_by_name(cpsw->dev->of_node, "cpts");
  446. if (!cpts_node)
  447. cpts_node = cpsw->dev->of_node;
  448. cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpts_node,
  449. CPTS_N_ETX_TS);
  450. if (IS_ERR(cpsw->cpts)) {
  451. ret = PTR_ERR(cpsw->cpts);
  452. cpdma_ctlr_destroy(cpsw->dma);
  453. }
  454. of_node_put(cpts_node);
  455. return ret;
  456. }
  457. #if IS_ENABLED(CONFIG_TI_CPTS)
  458. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  459. {
  460. struct cpsw_common *cpsw = priv->cpsw;
  461. struct cpsw_slave *slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  462. u32 ts_en, seq_id;
  463. if (!priv->tx_ts_enabled && !priv->rx_ts_enabled) {
  464. slave_write(slave, 0, CPSW1_TS_CTL);
  465. return;
  466. }
  467. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  468. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  469. if (priv->tx_ts_enabled)
  470. ts_en |= CPSW_V1_TS_TX_EN;
  471. if (priv->rx_ts_enabled)
  472. ts_en |= CPSW_V1_TS_RX_EN;
  473. slave_write(slave, ts_en, CPSW1_TS_CTL);
  474. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  475. }
  476. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  477. {
  478. struct cpsw_common *cpsw = priv->cpsw;
  479. struct cpsw_slave *slave;
  480. u32 ctrl, mtype;
  481. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  482. ctrl = slave_read(slave, CPSW2_CONTROL);
  483. switch (cpsw->version) {
  484. case CPSW_VERSION_2:
  485. ctrl &= ~CTRL_V2_ALL_TS_MASK;
  486. if (priv->tx_ts_enabled)
  487. ctrl |= CTRL_V2_TX_TS_BITS;
  488. if (priv->rx_ts_enabled)
  489. ctrl |= CTRL_V2_RX_TS_BITS;
  490. break;
  491. case CPSW_VERSION_3:
  492. default:
  493. ctrl &= ~CTRL_V3_ALL_TS_MASK;
  494. if (priv->tx_ts_enabled)
  495. ctrl |= CTRL_V3_TX_TS_BITS;
  496. if (priv->rx_ts_enabled)
  497. ctrl |= CTRL_V3_RX_TS_BITS;
  498. break;
  499. }
  500. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  501. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  502. slave_write(slave, ctrl, CPSW2_CONTROL);
  503. writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
  504. writel_relaxed(ETH_P_8021Q, &cpsw->regs->vlan_ltype);
  505. }
  506. int cpsw_hwtstamp_set(struct net_device *dev,
  507. struct kernel_hwtstamp_config *cfg,
  508. struct netlink_ext_ack *extack)
  509. {
  510. struct cpsw_priv *priv = netdev_priv(dev);
  511. struct cpsw_common *cpsw = priv->cpsw;
  512. /* This will only execute if dev->see_all_hwtstamp_requests is set */
  513. if (cfg->source != HWTSTAMP_SOURCE_NETDEV) {
  514. NL_SET_ERR_MSG_MOD(extack,
  515. "Switch mode only supports MAC timestamping");
  516. return -EOPNOTSUPP;
  517. }
  518. if (cpsw->version != CPSW_VERSION_1 &&
  519. cpsw->version != CPSW_VERSION_2 &&
  520. cpsw->version != CPSW_VERSION_3)
  521. return -EOPNOTSUPP;
  522. if (cfg->tx_type != HWTSTAMP_TX_OFF && cfg->tx_type != HWTSTAMP_TX_ON)
  523. return -ERANGE;
  524. switch (cfg->rx_filter) {
  525. case HWTSTAMP_FILTER_NONE:
  526. priv->rx_ts_enabled = 0;
  527. break;
  528. case HWTSTAMP_FILTER_ALL:
  529. case HWTSTAMP_FILTER_NTP_ALL:
  530. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  531. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  532. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  533. return -ERANGE;
  534. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  535. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  536. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  537. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  538. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  539. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  540. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  541. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  542. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  543. priv->rx_ts_enabled = HWTSTAMP_FILTER_PTP_V2_EVENT;
  544. cfg->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  545. break;
  546. default:
  547. return -ERANGE;
  548. }
  549. priv->tx_ts_enabled = cfg->tx_type == HWTSTAMP_TX_ON;
  550. switch (cpsw->version) {
  551. case CPSW_VERSION_1:
  552. cpsw_hwtstamp_v1(priv);
  553. break;
  554. case CPSW_VERSION_2:
  555. case CPSW_VERSION_3:
  556. cpsw_hwtstamp_v2(priv);
  557. break;
  558. default:
  559. WARN_ON(1);
  560. }
  561. return 0;
  562. }
  563. int cpsw_hwtstamp_get(struct net_device *dev,
  564. struct kernel_hwtstamp_config *cfg)
  565. {
  566. struct cpsw_common *cpsw = ndev_to_cpsw(dev);
  567. struct cpsw_priv *priv = netdev_priv(dev);
  568. if (cpsw->version != CPSW_VERSION_1 &&
  569. cpsw->version != CPSW_VERSION_2 &&
  570. cpsw->version != CPSW_VERSION_3)
  571. return -EOPNOTSUPP;
  572. cfg->tx_type = priv->tx_ts_enabled ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  573. cfg->rx_filter = priv->rx_ts_enabled;
  574. return 0;
  575. }
  576. #else
  577. int cpsw_hwtstamp_get(struct net_device *dev,
  578. struct kernel_hwtstamp_config *cfg)
  579. {
  580. return -EOPNOTSUPP;
  581. }
  582. int cpsw_hwtstamp_set(struct net_device *dev,
  583. struct kernel_hwtstamp_config *cfg,
  584. struct netlink_ext_ack *extack)
  585. {
  586. return -EOPNOTSUPP;
  587. }
  588. #endif /*CONFIG_TI_CPTS*/
  589. int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
  590. {
  591. struct cpsw_priv *priv = netdev_priv(ndev);
  592. struct cpsw_common *cpsw = priv->cpsw;
  593. struct cpsw_slave *slave;
  594. u32 min_rate;
  595. u32 ch_rate;
  596. int i, ret;
  597. ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
  598. if (ch_rate == rate)
  599. return 0;
  600. ch_rate = rate * 1000;
  601. min_rate = cpdma_chan_get_min_rate(cpsw->dma);
  602. if ((ch_rate < min_rate && ch_rate)) {
  603. dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
  604. min_rate);
  605. return -EINVAL;
  606. }
  607. if (rate > cpsw->speed) {
  608. dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
  609. return -EINVAL;
  610. }
  611. ret = pm_runtime_resume_and_get(cpsw->dev);
  612. if (ret < 0)
  613. return ret;
  614. ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
  615. pm_runtime_put(cpsw->dev);
  616. if (ret)
  617. return ret;
  618. /* update rates for slaves tx queues */
  619. for (i = 0; i < cpsw->data.slaves; i++) {
  620. slave = &cpsw->slaves[i];
  621. if (!slave->ndev)
  622. continue;
  623. netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
  624. }
  625. cpsw_split_res(cpsw);
  626. return ret;
  627. }
  628. static int cpsw_tc_to_fifo(int tc, int num_tc)
  629. {
  630. if (tc == num_tc - 1)
  631. return 0;
  632. return CPSW_FIFO_SHAPERS_NUM - tc;
  633. }
  634. bool cpsw_shp_is_off(struct cpsw_priv *priv)
  635. {
  636. struct cpsw_common *cpsw = priv->cpsw;
  637. struct cpsw_slave *slave;
  638. u32 shift, mask, val;
  639. val = readl_relaxed(&cpsw->regs->ptype);
  640. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  641. shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
  642. mask = 7 << shift;
  643. val = val & mask;
  644. return !val;
  645. }
  646. static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
  647. {
  648. struct cpsw_common *cpsw = priv->cpsw;
  649. struct cpsw_slave *slave;
  650. u32 shift, mask, val;
  651. val = readl_relaxed(&cpsw->regs->ptype);
  652. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  653. shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
  654. mask = (1 << --fifo) << shift;
  655. val = on ? val | mask : val & ~mask;
  656. writel_relaxed(val, &cpsw->regs->ptype);
  657. }
  658. static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
  659. {
  660. struct cpsw_common *cpsw = priv->cpsw;
  661. u32 val = 0, send_pct, shift;
  662. struct cpsw_slave *slave;
  663. int pct = 0, i;
  664. if (bw > priv->shp_cfg_speed * 1000)
  665. goto err;
  666. /* shaping has to stay enabled for highest fifos linearly
  667. * and fifo bw no more then interface can allow
  668. */
  669. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  670. send_pct = slave_read(slave, SEND_PERCENT);
  671. for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
  672. if (!bw) {
  673. if (i >= fifo || !priv->fifo_bw[i])
  674. continue;
  675. dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
  676. continue;
  677. }
  678. if (!priv->fifo_bw[i] && i > fifo) {
  679. dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
  680. return -EINVAL;
  681. }
  682. shift = (i - 1) * 8;
  683. if (i == fifo) {
  684. send_pct &= ~(CPSW_PCT_MASK << shift);
  685. val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
  686. if (!val)
  687. val = 1;
  688. send_pct |= val << shift;
  689. pct += val;
  690. continue;
  691. }
  692. if (priv->fifo_bw[i])
  693. pct += (send_pct >> shift) & CPSW_PCT_MASK;
  694. }
  695. if (pct >= 100)
  696. goto err;
  697. slave_write(slave, send_pct, SEND_PERCENT);
  698. priv->fifo_bw[fifo] = bw;
  699. dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
  700. DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
  701. return 0;
  702. err:
  703. dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
  704. return -EINVAL;
  705. }
  706. static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
  707. {
  708. struct cpsw_common *cpsw = priv->cpsw;
  709. struct cpsw_slave *slave;
  710. u32 tx_in_ctl_rg, val;
  711. int ret;
  712. ret = cpsw_set_fifo_bw(priv, fifo, bw);
  713. if (ret)
  714. return ret;
  715. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  716. tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
  717. CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
  718. if (!bw)
  719. cpsw_fifo_shp_on(priv, fifo, bw);
  720. val = slave_read(slave, tx_in_ctl_rg);
  721. if (cpsw_shp_is_off(priv)) {
  722. /* disable FIFOs rate limited queues */
  723. val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
  724. /* set type of FIFO queues to normal priority mode */
  725. val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
  726. /* set type of FIFO queues to be rate limited */
  727. if (bw)
  728. val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
  729. else
  730. priv->shp_cfg_speed = 0;
  731. }
  732. /* toggle a FIFO rate limited queue */
  733. if (bw)
  734. val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
  735. else
  736. val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
  737. slave_write(slave, val, tx_in_ctl_rg);
  738. /* FIFO transmit shape enable */
  739. cpsw_fifo_shp_on(priv, fifo, bw);
  740. return 0;
  741. }
  742. /* Defaults:
  743. * class A - prio 3
  744. * class B - prio 2
  745. * shaping for class A should be set first
  746. */
  747. static int cpsw_set_cbs(struct net_device *ndev,
  748. struct tc_cbs_qopt_offload *qopt)
  749. {
  750. struct cpsw_priv *priv = netdev_priv(ndev);
  751. struct cpsw_common *cpsw = priv->cpsw;
  752. struct cpsw_slave *slave;
  753. int prev_speed = 0;
  754. int tc, ret, fifo;
  755. u32 bw = 0;
  756. tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
  757. /* enable channels in backward order, as highest FIFOs must be rate
  758. * limited first and for compliance with CPDMA rate limited channels
  759. * that also used in bacward order. FIFO0 cannot be rate limited.
  760. */
  761. fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
  762. if (!fifo) {
  763. dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
  764. return -EINVAL;
  765. }
  766. /* do nothing, it's disabled anyway */
  767. if (!qopt->enable && !priv->fifo_bw[fifo])
  768. return 0;
  769. /* shapers can be set if link speed is known */
  770. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  771. if (slave->phy && slave->phy->link) {
  772. if (priv->shp_cfg_speed &&
  773. priv->shp_cfg_speed != slave->phy->speed)
  774. prev_speed = priv->shp_cfg_speed;
  775. priv->shp_cfg_speed = slave->phy->speed;
  776. }
  777. if (!priv->shp_cfg_speed) {
  778. dev_err(priv->dev, "Link speed is not known");
  779. return -1;
  780. }
  781. ret = pm_runtime_resume_and_get(cpsw->dev);
  782. if (ret < 0)
  783. return ret;
  784. bw = qopt->enable ? qopt->idleslope : 0;
  785. ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
  786. if (ret) {
  787. priv->shp_cfg_speed = prev_speed;
  788. prev_speed = 0;
  789. }
  790. if (bw && prev_speed)
  791. dev_warn(priv->dev,
  792. "Speed was changed, CBS shaper speeds are changed!");
  793. pm_runtime_put_sync(cpsw->dev);
  794. return ret;
  795. }
  796. static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
  797. {
  798. struct tc_mqprio_qopt_offload *mqprio = type_data;
  799. struct cpsw_priv *priv = netdev_priv(ndev);
  800. struct cpsw_common *cpsw = priv->cpsw;
  801. int fifo, num_tc, count, offset;
  802. struct cpsw_slave *slave;
  803. u32 tx_prio_map = 0;
  804. int i, tc, ret;
  805. num_tc = mqprio->qopt.num_tc;
  806. if (num_tc > CPSW_TC_NUM)
  807. return -EINVAL;
  808. if (mqprio->mode != TC_MQPRIO_MODE_DCB)
  809. return -EINVAL;
  810. ret = pm_runtime_resume_and_get(cpsw->dev);
  811. if (ret < 0)
  812. return ret;
  813. if (num_tc) {
  814. for (i = 0; i < 8; i++) {
  815. tc = mqprio->qopt.prio_tc_map[i];
  816. fifo = cpsw_tc_to_fifo(tc, num_tc);
  817. tx_prio_map |= fifo << (4 * i);
  818. }
  819. netdev_set_num_tc(ndev, num_tc);
  820. for (i = 0; i < num_tc; i++) {
  821. count = mqprio->qopt.count[i];
  822. offset = mqprio->qopt.offset[i];
  823. netdev_set_tc_queue(ndev, i, count, offset);
  824. }
  825. }
  826. if (!mqprio->qopt.hw) {
  827. /* restore default configuration */
  828. netdev_reset_tc(ndev);
  829. tx_prio_map = TX_PRIORITY_MAPPING;
  830. }
  831. priv->mqprio_hw = mqprio->qopt.hw;
  832. offset = cpsw->version == CPSW_VERSION_1 ?
  833. CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
  834. slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
  835. slave_write(slave, tx_prio_map, offset);
  836. pm_runtime_put_sync(cpsw->dev);
  837. return 0;
  838. }
  839. static int cpsw_qos_setup_tc_block(struct net_device *ndev, struct flow_block_offload *f);
  840. int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
  841. void *type_data)
  842. {
  843. switch (type) {
  844. case TC_SETUP_QDISC_CBS:
  845. return cpsw_set_cbs(ndev, type_data);
  846. case TC_SETUP_QDISC_MQPRIO:
  847. return cpsw_set_mqprio(ndev, type_data);
  848. case TC_SETUP_BLOCK:
  849. return cpsw_qos_setup_tc_block(ndev, type_data);
  850. default:
  851. return -EOPNOTSUPP;
  852. }
  853. }
  854. void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
  855. {
  856. int fifo, bw;
  857. for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
  858. bw = priv->fifo_bw[fifo];
  859. if (!bw)
  860. continue;
  861. cpsw_set_fifo_rlimit(priv, fifo, bw);
  862. }
  863. }
  864. void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
  865. {
  866. struct cpsw_common *cpsw = priv->cpsw;
  867. u32 tx_prio_map = 0;
  868. int i, tc, fifo;
  869. u32 tx_prio_rg;
  870. if (!priv->mqprio_hw)
  871. return;
  872. for (i = 0; i < 8; i++) {
  873. tc = netdev_get_prio_tc_map(priv->ndev, i);
  874. fifo = CPSW_FIFO_SHAPERS_NUM - tc;
  875. tx_prio_map |= fifo << (4 * i);
  876. }
  877. tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
  878. CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
  879. slave_write(slave, tx_prio_map, tx_prio_rg);
  880. }
  881. int cpsw_fill_rx_channels(struct cpsw_priv *priv)
  882. {
  883. struct cpsw_common *cpsw = priv->cpsw;
  884. struct cpsw_meta_xdp *xmeta;
  885. struct page_pool *pool;
  886. struct page *page;
  887. int ch_buf_num;
  888. int ch, i, ret;
  889. dma_addr_t dma;
  890. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  891. pool = cpsw->page_pool[ch];
  892. ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
  893. for (i = 0; i < ch_buf_num; i++) {
  894. page = page_pool_dev_alloc_pages(pool);
  895. if (!page) {
  896. cpsw_err(priv, ifup, "allocate rx page err\n");
  897. return -ENOMEM;
  898. }
  899. xmeta = page_address(page) + CPSW_XMETA_OFFSET;
  900. xmeta->ndev = priv->ndev;
  901. xmeta->ch = ch;
  902. dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM_NA;
  903. ret = cpdma_chan_idle_submit_mapped(cpsw->rxv[ch].ch,
  904. page, dma,
  905. cpsw->rx_packet_max,
  906. 0);
  907. if (ret < 0) {
  908. cpsw_err(priv, ifup,
  909. "cannot submit page to channel %d rx, error %d\n",
  910. ch, ret);
  911. page_pool_recycle_direct(pool, page);
  912. return ret;
  913. }
  914. }
  915. cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
  916. ch, ch_buf_num);
  917. }
  918. return 0;
  919. }
  920. static struct page_pool *cpsw_create_page_pool(struct cpsw_common *cpsw,
  921. int size)
  922. {
  923. struct page_pool_params pp_params = {};
  924. struct page_pool *pool;
  925. pp_params.order = 0;
  926. pp_params.flags = PP_FLAG_DMA_MAP;
  927. pp_params.pool_size = size;
  928. pp_params.nid = NUMA_NO_NODE;
  929. pp_params.dma_dir = DMA_BIDIRECTIONAL;
  930. pp_params.dev = cpsw->dev;
  931. pool = page_pool_create(&pp_params);
  932. if (IS_ERR(pool))
  933. dev_err(cpsw->dev, "cannot create rx page pool\n");
  934. return pool;
  935. }
  936. static int cpsw_create_rx_pool(struct cpsw_common *cpsw, int ch)
  937. {
  938. struct page_pool *pool;
  939. int ret = 0, pool_size;
  940. pool_size = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
  941. pool = cpsw_create_page_pool(cpsw, pool_size);
  942. if (IS_ERR(pool))
  943. ret = PTR_ERR(pool);
  944. else
  945. cpsw->page_pool[ch] = pool;
  946. return ret;
  947. }
  948. static int cpsw_ndev_create_xdp_rxq(struct cpsw_priv *priv, int ch)
  949. {
  950. struct cpsw_common *cpsw = priv->cpsw;
  951. struct xdp_rxq_info *rxq;
  952. struct page_pool *pool;
  953. int ret;
  954. pool = cpsw->page_pool[ch];
  955. rxq = &priv->xdp_rxq[ch];
  956. ret = xdp_rxq_info_reg(rxq, priv->ndev, ch, 0);
  957. if (ret)
  958. return ret;
  959. ret = xdp_rxq_info_reg_mem_model(rxq, MEM_TYPE_PAGE_POOL, pool);
  960. if (ret)
  961. xdp_rxq_info_unreg(rxq);
  962. return ret;
  963. }
  964. static void cpsw_ndev_destroy_xdp_rxq(struct cpsw_priv *priv, int ch)
  965. {
  966. struct xdp_rxq_info *rxq = &priv->xdp_rxq[ch];
  967. if (!xdp_rxq_info_is_reg(rxq))
  968. return;
  969. xdp_rxq_info_unreg(rxq);
  970. }
  971. void cpsw_destroy_xdp_rxqs(struct cpsw_common *cpsw)
  972. {
  973. struct net_device *ndev;
  974. int i, ch;
  975. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  976. for (i = 0; i < cpsw->data.slaves; i++) {
  977. ndev = cpsw->slaves[i].ndev;
  978. if (!ndev)
  979. continue;
  980. cpsw_ndev_destroy_xdp_rxq(netdev_priv(ndev), ch);
  981. }
  982. page_pool_destroy(cpsw->page_pool[ch]);
  983. cpsw->page_pool[ch] = NULL;
  984. }
  985. }
  986. int cpsw_create_xdp_rxqs(struct cpsw_common *cpsw)
  987. {
  988. struct net_device *ndev;
  989. int i, ch, ret;
  990. for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
  991. ret = cpsw_create_rx_pool(cpsw, ch);
  992. if (ret)
  993. goto err_cleanup;
  994. /* using same page pool is allowed as no running rx handlers
  995. * simultaneously for both ndevs
  996. */
  997. for (i = 0; i < cpsw->data.slaves; i++) {
  998. ndev = cpsw->slaves[i].ndev;
  999. if (!ndev)
  1000. continue;
  1001. ret = cpsw_ndev_create_xdp_rxq(netdev_priv(ndev), ch);
  1002. if (ret)
  1003. goto err_cleanup;
  1004. }
  1005. }
  1006. return 0;
  1007. err_cleanup:
  1008. cpsw_destroy_xdp_rxqs(cpsw);
  1009. return ret;
  1010. }
  1011. static int cpsw_xdp_prog_setup(struct cpsw_priv *priv, struct netdev_bpf *bpf)
  1012. {
  1013. struct bpf_prog *prog = bpf->prog;
  1014. if (!priv->xdpi.prog && !prog)
  1015. return 0;
  1016. WRITE_ONCE(priv->xdp_prog, prog);
  1017. xdp_attachment_setup(&priv->xdpi, bpf);
  1018. return 0;
  1019. }
  1020. int cpsw_ndo_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
  1021. {
  1022. struct cpsw_priv *priv = netdev_priv(ndev);
  1023. switch (bpf->command) {
  1024. case XDP_SETUP_PROG:
  1025. return cpsw_xdp_prog_setup(priv, bpf);
  1026. default:
  1027. return -EINVAL;
  1028. }
  1029. }
  1030. int cpsw_xdp_tx_frame(struct cpsw_priv *priv, struct xdp_frame *xdpf,
  1031. struct page *page, int port)
  1032. {
  1033. struct cpsw_common *cpsw = priv->cpsw;
  1034. struct cpsw_meta_xdp *xmeta;
  1035. struct cpdma_chan *txch;
  1036. dma_addr_t dma;
  1037. int ret;
  1038. xmeta = (void *)xdpf + CPSW_XMETA_OFFSET;
  1039. xmeta->ndev = priv->ndev;
  1040. xmeta->ch = 0;
  1041. txch = cpsw->txv[0].ch;
  1042. if (page) {
  1043. dma = page_pool_get_dma_addr(page);
  1044. dma += xdpf->headroom + sizeof(struct xdp_frame);
  1045. ret = cpdma_chan_submit_mapped(txch, cpsw_xdpf_to_handle(xdpf),
  1046. dma, xdpf->len, port);
  1047. } else {
  1048. if (sizeof(*xmeta) > xdpf->headroom)
  1049. return -EINVAL;
  1050. ret = cpdma_chan_submit(txch, cpsw_xdpf_to_handle(xdpf),
  1051. xdpf->data, xdpf->len, port);
  1052. }
  1053. if (ret)
  1054. priv->ndev->stats.tx_dropped++;
  1055. return ret;
  1056. }
  1057. int cpsw_run_xdp(struct cpsw_priv *priv, int ch, struct xdp_buff *xdp,
  1058. struct page *page, int port, int *len)
  1059. {
  1060. struct cpsw_common *cpsw = priv->cpsw;
  1061. struct net_device *ndev = priv->ndev;
  1062. int ret = CPSW_XDP_CONSUMED;
  1063. struct xdp_frame *xdpf;
  1064. struct bpf_prog *prog;
  1065. u32 act;
  1066. prog = READ_ONCE(priv->xdp_prog);
  1067. if (!prog)
  1068. return CPSW_XDP_PASS;
  1069. act = bpf_prog_run_xdp(prog, xdp);
  1070. /* XDP prog might have changed packet data and boundaries */
  1071. *len = xdp->data_end - xdp->data;
  1072. switch (act) {
  1073. case XDP_PASS:
  1074. ret = CPSW_XDP_PASS;
  1075. goto out;
  1076. case XDP_TX:
  1077. xdpf = xdp_convert_buff_to_frame(xdp);
  1078. if (unlikely(!xdpf))
  1079. goto drop;
  1080. if (cpsw_xdp_tx_frame(priv, xdpf, page, port))
  1081. xdp_return_frame_rx_napi(xdpf);
  1082. break;
  1083. case XDP_REDIRECT:
  1084. if (xdp_do_redirect(ndev, xdp, prog))
  1085. goto drop;
  1086. /* Have to flush here, per packet, instead of doing it in bulk
  1087. * at the end of the napi handler. The RX devices on this
  1088. * particular hardware is sharing a common queue, so the
  1089. * incoming device might change per packet.
  1090. */
  1091. xdp_do_flush();
  1092. break;
  1093. default:
  1094. bpf_warn_invalid_xdp_action(ndev, prog, act);
  1095. fallthrough;
  1096. case XDP_ABORTED:
  1097. trace_xdp_exception(ndev, prog, act);
  1098. fallthrough; /* handle aborts by dropping packet */
  1099. case XDP_DROP:
  1100. ndev->stats.rx_bytes += *len;
  1101. ndev->stats.rx_packets++;
  1102. goto drop;
  1103. }
  1104. ndev->stats.rx_bytes += *len;
  1105. ndev->stats.rx_packets++;
  1106. out:
  1107. return ret;
  1108. drop:
  1109. page_pool_recycle_direct(cpsw->page_pool[ch], page);
  1110. return ret;
  1111. }
  1112. static int cpsw_qos_clsflower_add_policer(struct cpsw_priv *priv,
  1113. struct netlink_ext_ack *extack,
  1114. struct flow_cls_offload *cls,
  1115. u64 rate_pkt_ps)
  1116. {
  1117. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  1118. struct flow_dissector *dissector = rule->match.dissector;
  1119. static const u8 mc_mac[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
  1120. struct flow_match_eth_addrs match;
  1121. u32 port_id;
  1122. int ret;
  1123. if (dissector->used_keys &
  1124. ~(BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
  1125. BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
  1126. BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS))) {
  1127. NL_SET_ERR_MSG_MOD(extack,
  1128. "Unsupported keys used");
  1129. return -EOPNOTSUPP;
  1130. }
  1131. if (flow_rule_match_has_control_flags(rule, extack))
  1132. return -EOPNOTSUPP;
  1133. if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  1134. NL_SET_ERR_MSG_MOD(extack, "Not matching on eth address");
  1135. return -EOPNOTSUPP;
  1136. }
  1137. flow_rule_match_eth_addrs(rule, &match);
  1138. if (!is_zero_ether_addr(match.mask->src)) {
  1139. NL_SET_ERR_MSG_MOD(extack,
  1140. "Matching on source MAC not supported");
  1141. return -EOPNOTSUPP;
  1142. }
  1143. port_id = cpsw_slave_index(priv->cpsw, priv) + 1;
  1144. if (is_broadcast_ether_addr(match.key->dst) &&
  1145. is_broadcast_ether_addr(match.mask->dst)) {
  1146. ret = cpsw_ale_rx_ratelimit_bc(priv->cpsw->ale, port_id, rate_pkt_ps);
  1147. if (ret)
  1148. return ret;
  1149. priv->ale_bc_ratelimit.cookie = cls->cookie;
  1150. priv->ale_bc_ratelimit.rate_packet_ps = rate_pkt_ps;
  1151. } else if (ether_addr_equal_unaligned(match.key->dst, mc_mac) &&
  1152. ether_addr_equal_unaligned(match.mask->dst, mc_mac)) {
  1153. ret = cpsw_ale_rx_ratelimit_mc(priv->cpsw->ale, port_id, rate_pkt_ps);
  1154. if (ret)
  1155. return ret;
  1156. priv->ale_mc_ratelimit.cookie = cls->cookie;
  1157. priv->ale_mc_ratelimit.rate_packet_ps = rate_pkt_ps;
  1158. } else {
  1159. NL_SET_ERR_MSG_MOD(extack, "Not supported matching key");
  1160. return -EOPNOTSUPP;
  1161. }
  1162. return 0;
  1163. }
  1164. static int cpsw_qos_clsflower_policer_validate(const struct flow_action *action,
  1165. const struct flow_action_entry *act,
  1166. struct netlink_ext_ack *extack)
  1167. {
  1168. if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
  1169. NL_SET_ERR_MSG_MOD(extack,
  1170. "Offload not supported when exceed action is not drop");
  1171. return -EOPNOTSUPP;
  1172. }
  1173. if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
  1174. act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
  1175. NL_SET_ERR_MSG_MOD(extack,
  1176. "Offload not supported when conform action is not pipe or ok");
  1177. return -EOPNOTSUPP;
  1178. }
  1179. if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
  1180. !flow_action_is_last_entry(action, act)) {
  1181. NL_SET_ERR_MSG_MOD(extack,
  1182. "Offload not supported when conform action is ok, but action is not last");
  1183. return -EOPNOTSUPP;
  1184. }
  1185. if (act->police.rate_bytes_ps || act->police.peakrate_bytes_ps ||
  1186. act->police.avrate || act->police.overhead) {
  1187. NL_SET_ERR_MSG_MOD(extack,
  1188. "Offload not supported when bytes per second/peakrate/avrate/overhead is configured");
  1189. return -EOPNOTSUPP;
  1190. }
  1191. return 0;
  1192. }
  1193. static int cpsw_qos_configure_clsflower(struct cpsw_priv *priv, struct flow_cls_offload *cls)
  1194. {
  1195. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  1196. struct netlink_ext_ack *extack = cls->common.extack;
  1197. const struct flow_action_entry *act;
  1198. int i, ret;
  1199. flow_action_for_each(i, act, &rule->action) {
  1200. switch (act->id) {
  1201. case FLOW_ACTION_POLICE:
  1202. ret = cpsw_qos_clsflower_policer_validate(&rule->action, act, extack);
  1203. if (ret)
  1204. return ret;
  1205. return cpsw_qos_clsflower_add_policer(priv, extack, cls,
  1206. act->police.rate_pkt_ps);
  1207. default:
  1208. NL_SET_ERR_MSG_MOD(extack, "Action not supported");
  1209. return -EOPNOTSUPP;
  1210. }
  1211. }
  1212. return -EOPNOTSUPP;
  1213. }
  1214. static int cpsw_qos_delete_clsflower(struct cpsw_priv *priv, struct flow_cls_offload *cls)
  1215. {
  1216. u32 port_id = cpsw_slave_index(priv->cpsw, priv) + 1;
  1217. if (cls->cookie == priv->ale_bc_ratelimit.cookie) {
  1218. priv->ale_bc_ratelimit.cookie = 0;
  1219. priv->ale_bc_ratelimit.rate_packet_ps = 0;
  1220. cpsw_ale_rx_ratelimit_bc(priv->cpsw->ale, port_id, 0);
  1221. }
  1222. if (cls->cookie == priv->ale_mc_ratelimit.cookie) {
  1223. priv->ale_mc_ratelimit.cookie = 0;
  1224. priv->ale_mc_ratelimit.rate_packet_ps = 0;
  1225. cpsw_ale_rx_ratelimit_mc(priv->cpsw->ale, port_id, 0);
  1226. }
  1227. return 0;
  1228. }
  1229. static int cpsw_qos_setup_tc_clsflower(struct cpsw_priv *priv, struct flow_cls_offload *cls_flower)
  1230. {
  1231. switch (cls_flower->command) {
  1232. case FLOW_CLS_REPLACE:
  1233. return cpsw_qos_configure_clsflower(priv, cls_flower);
  1234. case FLOW_CLS_DESTROY:
  1235. return cpsw_qos_delete_clsflower(priv, cls_flower);
  1236. default:
  1237. return -EOPNOTSUPP;
  1238. }
  1239. }
  1240. static int cpsw_qos_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  1241. {
  1242. struct cpsw_priv *priv = cb_priv;
  1243. int ret;
  1244. if (!tc_cls_can_offload_and_chain0(priv->ndev, type_data))
  1245. return -EOPNOTSUPP;
  1246. ret = pm_runtime_get_sync(priv->dev);
  1247. if (ret < 0) {
  1248. pm_runtime_put_noidle(priv->dev);
  1249. return ret;
  1250. }
  1251. switch (type) {
  1252. case TC_SETUP_CLSFLOWER:
  1253. ret = cpsw_qos_setup_tc_clsflower(priv, type_data);
  1254. break;
  1255. default:
  1256. ret = -EOPNOTSUPP;
  1257. }
  1258. pm_runtime_put(priv->dev);
  1259. return ret;
  1260. }
  1261. static LIST_HEAD(cpsw_qos_block_cb_list);
  1262. static int cpsw_qos_setup_tc_block(struct net_device *ndev, struct flow_block_offload *f)
  1263. {
  1264. struct cpsw_priv *priv = netdev_priv(ndev);
  1265. return flow_block_cb_setup_simple(f, &cpsw_qos_block_cb_list,
  1266. cpsw_qos_setup_tc_block_cb,
  1267. priv, priv, true);
  1268. }
  1269. void cpsw_qos_clsflower_resume(struct cpsw_priv *priv)
  1270. {
  1271. u32 port_id = cpsw_slave_index(priv->cpsw, priv) + 1;
  1272. if (priv->ale_bc_ratelimit.cookie)
  1273. cpsw_ale_rx_ratelimit_bc(priv->cpsw->ale, port_id,
  1274. priv->ale_bc_ratelimit.rate_packet_ps);
  1275. if (priv->ale_mc_ratelimit.cookie)
  1276. cpsw_ale_rx_ratelimit_mc(priv->cpsw->ale, port_id,
  1277. priv->ale_mc_ratelimit.rate_packet_ps);
  1278. }