ocelot_fdma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Microsemi SoCs FDMA driver
  4. *
  5. * Copyright (c) 2021 Microchip
  6. *
  7. * Page recycling code is mostly taken from gianfar driver.
  8. */
  9. #include <linux/align.h>
  10. #include <linux/bitops.h>
  11. #include <linux/dmapool.h>
  12. #include <linux/dsa/ocelot.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/skbuff.h>
  16. #include "ocelot_fdma.h"
  17. #include "ocelot_qs.h"
  18. DEFINE_STATIC_KEY_FALSE(ocelot_fdma_enabled);
  19. static void ocelot_fdma_writel(struct ocelot *ocelot, u32 reg, u32 data)
  20. {
  21. regmap_write(ocelot->targets[FDMA], reg, data);
  22. }
  23. static u32 ocelot_fdma_readl(struct ocelot *ocelot, u32 reg)
  24. {
  25. u32 retval;
  26. regmap_read(ocelot->targets[FDMA], reg, &retval);
  27. return retval;
  28. }
  29. static dma_addr_t ocelot_fdma_idx_dma(dma_addr_t base, u16 idx)
  30. {
  31. return base + idx * sizeof(struct ocelot_fdma_dcb);
  32. }
  33. static u16 ocelot_fdma_dma_idx(dma_addr_t base, dma_addr_t dma)
  34. {
  35. return (dma - base) / sizeof(struct ocelot_fdma_dcb);
  36. }
  37. static u16 ocelot_fdma_idx_next(u16 idx, u16 ring_sz)
  38. {
  39. return unlikely(idx == ring_sz - 1) ? 0 : idx + 1;
  40. }
  41. static u16 ocelot_fdma_idx_prev(u16 idx, u16 ring_sz)
  42. {
  43. return unlikely(idx == 0) ? ring_sz - 1 : idx - 1;
  44. }
  45. static int ocelot_fdma_rx_ring_free(struct ocelot_fdma *fdma)
  46. {
  47. struct ocelot_fdma_rx_ring *rx_ring = &fdma->rx_ring;
  48. if (rx_ring->next_to_use >= rx_ring->next_to_clean)
  49. return OCELOT_FDMA_RX_RING_SIZE -
  50. (rx_ring->next_to_use - rx_ring->next_to_clean) - 1;
  51. else
  52. return rx_ring->next_to_clean - rx_ring->next_to_use - 1;
  53. }
  54. static int ocelot_fdma_tx_ring_free(struct ocelot_fdma *fdma)
  55. {
  56. struct ocelot_fdma_tx_ring *tx_ring = &fdma->tx_ring;
  57. if (tx_ring->next_to_use >= tx_ring->next_to_clean)
  58. return OCELOT_FDMA_TX_RING_SIZE -
  59. (tx_ring->next_to_use - tx_ring->next_to_clean) - 1;
  60. else
  61. return tx_ring->next_to_clean - tx_ring->next_to_use - 1;
  62. }
  63. static bool ocelot_fdma_tx_ring_empty(struct ocelot_fdma *fdma)
  64. {
  65. struct ocelot_fdma_tx_ring *tx_ring = &fdma->tx_ring;
  66. return tx_ring->next_to_clean == tx_ring->next_to_use;
  67. }
  68. static void ocelot_fdma_activate_chan(struct ocelot *ocelot, dma_addr_t dma,
  69. int chan)
  70. {
  71. ocelot_fdma_writel(ocelot, MSCC_FDMA_DCB_LLP(chan), dma);
  72. /* Barrier to force memory writes to DCB to be completed before starting
  73. * the channel.
  74. */
  75. wmb();
  76. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_ACTIVATE, BIT(chan));
  77. }
  78. static u32 ocelot_fdma_read_ch_safe(struct ocelot *ocelot)
  79. {
  80. return ocelot_fdma_readl(ocelot, MSCC_FDMA_CH_SAFE);
  81. }
  82. static int ocelot_fdma_wait_chan_safe(struct ocelot *ocelot, int chan)
  83. {
  84. u32 safe;
  85. return readx_poll_timeout_atomic(ocelot_fdma_read_ch_safe, ocelot, safe,
  86. safe & BIT(chan), 0,
  87. OCELOT_FDMA_CH_SAFE_TIMEOUT_US);
  88. }
  89. static void ocelot_fdma_dcb_set_data(struct ocelot_fdma_dcb *dcb,
  90. dma_addr_t dma_addr,
  91. size_t size)
  92. {
  93. u32 offset = dma_addr & 0x3;
  94. dcb->llp = 0;
  95. dcb->datap = ALIGN_DOWN(dma_addr, 4);
  96. dcb->datal = ALIGN_DOWN(size, 4);
  97. dcb->stat = MSCC_FDMA_DCB_STAT_BLOCKO(offset);
  98. }
  99. static bool ocelot_fdma_rx_alloc_page(struct ocelot *ocelot,
  100. struct ocelot_fdma_rx_buf *rxb)
  101. {
  102. dma_addr_t mapping;
  103. struct page *page;
  104. page = dev_alloc_page();
  105. if (unlikely(!page))
  106. return false;
  107. mapping = dma_map_page(ocelot->dev, page, 0, PAGE_SIZE,
  108. DMA_FROM_DEVICE);
  109. if (unlikely(dma_mapping_error(ocelot->dev, mapping))) {
  110. __free_page(page);
  111. return false;
  112. }
  113. rxb->page = page;
  114. rxb->page_offset = 0;
  115. rxb->dma_addr = mapping;
  116. return true;
  117. }
  118. static int ocelot_fdma_alloc_rx_buffs(struct ocelot *ocelot, u16 alloc_cnt)
  119. {
  120. struct ocelot_fdma *fdma = ocelot->fdma;
  121. struct ocelot_fdma_rx_ring *rx_ring;
  122. struct ocelot_fdma_rx_buf *rxb;
  123. struct ocelot_fdma_dcb *dcb;
  124. dma_addr_t dma_addr;
  125. int ret = 0;
  126. u16 idx;
  127. rx_ring = &fdma->rx_ring;
  128. idx = rx_ring->next_to_use;
  129. while (alloc_cnt--) {
  130. rxb = &rx_ring->bufs[idx];
  131. /* try reuse page */
  132. if (unlikely(!rxb->page)) {
  133. if (unlikely(!ocelot_fdma_rx_alloc_page(ocelot, rxb))) {
  134. dev_err_ratelimited(ocelot->dev,
  135. "Failed to allocate rx\n");
  136. ret = -ENOMEM;
  137. break;
  138. }
  139. }
  140. dcb = &rx_ring->dcbs[idx];
  141. dma_addr = rxb->dma_addr + rxb->page_offset;
  142. ocelot_fdma_dcb_set_data(dcb, dma_addr, OCELOT_FDMA_RXB_SIZE);
  143. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  144. /* Chain the DCB to the next one */
  145. dcb->llp = ocelot_fdma_idx_dma(rx_ring->dcbs_dma, idx);
  146. }
  147. rx_ring->next_to_use = idx;
  148. rx_ring->next_to_alloc = idx;
  149. return ret;
  150. }
  151. static bool ocelot_fdma_tx_dcb_set_skb(struct ocelot *ocelot,
  152. struct ocelot_fdma_tx_buf *tx_buf,
  153. struct ocelot_fdma_dcb *dcb,
  154. struct sk_buff *skb)
  155. {
  156. dma_addr_t mapping;
  157. mapping = dma_map_single(ocelot->dev, skb->data, skb->len,
  158. DMA_TO_DEVICE);
  159. if (unlikely(dma_mapping_error(ocelot->dev, mapping)))
  160. return false;
  161. dma_unmap_addr_set(tx_buf, dma_addr, mapping);
  162. ocelot_fdma_dcb_set_data(dcb, mapping, OCELOT_FDMA_RX_SIZE);
  163. tx_buf->skb = skb;
  164. dcb->stat |= MSCC_FDMA_DCB_STAT_BLOCKL(skb->len);
  165. dcb->stat |= MSCC_FDMA_DCB_STAT_SOF | MSCC_FDMA_DCB_STAT_EOF;
  166. return true;
  167. }
  168. static bool ocelot_fdma_check_stop_rx(struct ocelot *ocelot)
  169. {
  170. u32 llp;
  171. /* Check if the FDMA hits the DCB with LLP == NULL */
  172. llp = ocelot_fdma_readl(ocelot, MSCC_FDMA_DCB_LLP(MSCC_FDMA_XTR_CHAN));
  173. if (unlikely(llp))
  174. return false;
  175. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_DISABLE,
  176. BIT(MSCC_FDMA_XTR_CHAN));
  177. return true;
  178. }
  179. static void ocelot_fdma_rx_set_llp(struct ocelot_fdma_rx_ring *rx_ring)
  180. {
  181. struct ocelot_fdma_dcb *dcb;
  182. unsigned int idx;
  183. idx = ocelot_fdma_idx_prev(rx_ring->next_to_use,
  184. OCELOT_FDMA_RX_RING_SIZE);
  185. dcb = &rx_ring->dcbs[idx];
  186. dcb->llp = 0;
  187. }
  188. static void ocelot_fdma_rx_restart(struct ocelot *ocelot)
  189. {
  190. struct ocelot_fdma *fdma = ocelot->fdma;
  191. struct ocelot_fdma_rx_ring *rx_ring;
  192. const u8 chan = MSCC_FDMA_XTR_CHAN;
  193. dma_addr_t new_llp, dma_base;
  194. unsigned int idx;
  195. u32 llp_prev;
  196. int ret;
  197. rx_ring = &fdma->rx_ring;
  198. ret = ocelot_fdma_wait_chan_safe(ocelot, chan);
  199. if (ret) {
  200. dev_err_ratelimited(ocelot->dev,
  201. "Unable to stop RX channel\n");
  202. return;
  203. }
  204. ocelot_fdma_rx_set_llp(rx_ring);
  205. /* FDMA stopped on the last DCB that contained a NULL LLP, since
  206. * we processed some DCBs in RX, there is free space, and we must set
  207. * DCB_LLP to point to the next DCB
  208. */
  209. llp_prev = ocelot_fdma_readl(ocelot, MSCC_FDMA_DCB_LLP_PREV(chan));
  210. dma_base = rx_ring->dcbs_dma;
  211. /* Get the next DMA addr located after LLP == NULL DCB */
  212. idx = ocelot_fdma_dma_idx(dma_base, llp_prev);
  213. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  214. new_llp = ocelot_fdma_idx_dma(dma_base, idx);
  215. /* Finally reactivate the channel */
  216. ocelot_fdma_activate_chan(ocelot, new_llp, chan);
  217. }
  218. static bool ocelot_fdma_add_rx_frag(struct ocelot_fdma_rx_buf *rxb, u32 stat,
  219. struct sk_buff *skb, bool first)
  220. {
  221. int size = MSCC_FDMA_DCB_STAT_BLOCKL(stat);
  222. struct page *page = rxb->page;
  223. if (likely(first)) {
  224. skb_put(skb, size);
  225. } else {
  226. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  227. rxb->page_offset, size, OCELOT_FDMA_RX_SIZE);
  228. }
  229. /* Try to reuse page */
  230. if (unlikely(page_ref_count(page) != 1 || page_is_pfmemalloc(page)))
  231. return false;
  232. /* Change offset to the other half */
  233. rxb->page_offset ^= OCELOT_FDMA_RX_SIZE;
  234. page_ref_inc(page);
  235. return true;
  236. }
  237. static void ocelot_fdma_reuse_rx_page(struct ocelot *ocelot,
  238. struct ocelot_fdma_rx_buf *old_rxb)
  239. {
  240. struct ocelot_fdma_rx_ring *rx_ring = &ocelot->fdma->rx_ring;
  241. struct ocelot_fdma_rx_buf *new_rxb;
  242. new_rxb = &rx_ring->bufs[rx_ring->next_to_alloc];
  243. rx_ring->next_to_alloc = ocelot_fdma_idx_next(rx_ring->next_to_alloc,
  244. OCELOT_FDMA_RX_RING_SIZE);
  245. /* Copy page reference */
  246. *new_rxb = *old_rxb;
  247. /* Sync for use by the device */
  248. dma_sync_single_range_for_device(ocelot->dev, old_rxb->dma_addr,
  249. old_rxb->page_offset,
  250. OCELOT_FDMA_RX_SIZE, DMA_FROM_DEVICE);
  251. }
  252. static struct sk_buff *ocelot_fdma_get_skb(struct ocelot *ocelot, u32 stat,
  253. struct ocelot_fdma_rx_buf *rxb,
  254. struct sk_buff *skb)
  255. {
  256. bool first = false;
  257. /* Allocate skb head and data */
  258. if (likely(!skb)) {
  259. void *buff_addr = page_address(rxb->page) +
  260. rxb->page_offset;
  261. skb = build_skb(buff_addr, OCELOT_FDMA_SKBFRAG_SIZE);
  262. if (unlikely(!skb)) {
  263. dev_err_ratelimited(ocelot->dev,
  264. "build_skb failed !\n");
  265. return NULL;
  266. }
  267. first = true;
  268. }
  269. dma_sync_single_range_for_cpu(ocelot->dev, rxb->dma_addr,
  270. rxb->page_offset, OCELOT_FDMA_RX_SIZE,
  271. DMA_FROM_DEVICE);
  272. if (ocelot_fdma_add_rx_frag(rxb, stat, skb, first)) {
  273. /* Reuse the free half of the page for the next_to_alloc DCB*/
  274. ocelot_fdma_reuse_rx_page(ocelot, rxb);
  275. } else {
  276. /* page cannot be reused, unmap it */
  277. dma_unmap_page(ocelot->dev, rxb->dma_addr, PAGE_SIZE,
  278. DMA_FROM_DEVICE);
  279. }
  280. /* clear rx buff content */
  281. rxb->page = NULL;
  282. return skb;
  283. }
  284. static bool ocelot_fdma_receive_skb(struct ocelot *ocelot, struct sk_buff *skb)
  285. {
  286. struct net_device *ndev;
  287. void *xfh = skb->data;
  288. u64 timestamp;
  289. u64 src_port;
  290. skb_pull(skb, OCELOT_TAG_LEN);
  291. ocelot_xfh_get_src_port(xfh, &src_port);
  292. if (unlikely(src_port >= ocelot->num_phys_ports))
  293. return false;
  294. ndev = ocelot_port_to_netdev(ocelot, src_port);
  295. if (unlikely(!ndev))
  296. return false;
  297. pskb_trim(skb, skb->len - ETH_FCS_LEN);
  298. skb->dev = ndev;
  299. skb->protocol = eth_type_trans(skb, skb->dev);
  300. skb->dev->stats.rx_bytes += skb->len;
  301. skb->dev->stats.rx_packets++;
  302. if (ocelot->ptp) {
  303. ocelot_xfh_get_rew_val(xfh, &timestamp);
  304. ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
  305. }
  306. if (likely(!skb_defer_rx_timestamp(skb)))
  307. netif_receive_skb(skb);
  308. return true;
  309. }
  310. static int ocelot_fdma_rx_get(struct ocelot *ocelot, int budget)
  311. {
  312. struct ocelot_fdma *fdma = ocelot->fdma;
  313. struct ocelot_fdma_rx_ring *rx_ring;
  314. struct ocelot_fdma_rx_buf *rxb;
  315. struct ocelot_fdma_dcb *dcb;
  316. struct sk_buff *skb;
  317. int work_done = 0;
  318. int cleaned_cnt;
  319. u32 stat;
  320. u16 idx;
  321. cleaned_cnt = ocelot_fdma_rx_ring_free(fdma);
  322. rx_ring = &fdma->rx_ring;
  323. skb = rx_ring->skb;
  324. while (budget--) {
  325. idx = rx_ring->next_to_clean;
  326. dcb = &rx_ring->dcbs[idx];
  327. stat = dcb->stat;
  328. if (MSCC_FDMA_DCB_STAT_BLOCKL(stat) == 0)
  329. break;
  330. /* New packet is a start of frame but we already got a skb set,
  331. * we probably lost an EOF packet, free skb
  332. */
  333. if (unlikely(skb && (stat & MSCC_FDMA_DCB_STAT_SOF))) {
  334. dev_kfree_skb(skb);
  335. skb = NULL;
  336. }
  337. rxb = &rx_ring->bufs[idx];
  338. /* Fetch next to clean buffer from the rx_ring */
  339. skb = ocelot_fdma_get_skb(ocelot, stat, rxb, skb);
  340. if (unlikely(!skb))
  341. break;
  342. work_done++;
  343. cleaned_cnt++;
  344. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  345. rx_ring->next_to_clean = idx;
  346. if (unlikely(stat & MSCC_FDMA_DCB_STAT_ABORT ||
  347. stat & MSCC_FDMA_DCB_STAT_PD)) {
  348. dev_err_ratelimited(ocelot->dev,
  349. "DCB aborted or pruned\n");
  350. dev_kfree_skb(skb);
  351. skb = NULL;
  352. continue;
  353. }
  354. /* We still need to process the other fragment of the packet
  355. * before delivering it to the network stack
  356. */
  357. if (!(stat & MSCC_FDMA_DCB_STAT_EOF))
  358. continue;
  359. if (unlikely(!ocelot_fdma_receive_skb(ocelot, skb)))
  360. dev_kfree_skb(skb);
  361. skb = NULL;
  362. }
  363. rx_ring->skb = skb;
  364. if (cleaned_cnt)
  365. ocelot_fdma_alloc_rx_buffs(ocelot, cleaned_cnt);
  366. return work_done;
  367. }
  368. static void ocelot_fdma_wakeup_netdev(struct ocelot *ocelot)
  369. {
  370. struct ocelot_port_private *priv;
  371. struct ocelot_port *ocelot_port;
  372. struct net_device *dev;
  373. int port;
  374. for (port = 0; port < ocelot->num_phys_ports; port++) {
  375. ocelot_port = ocelot->ports[port];
  376. if (!ocelot_port)
  377. continue;
  378. priv = container_of(ocelot_port, struct ocelot_port_private,
  379. port);
  380. dev = priv->dev;
  381. if (unlikely(netif_queue_stopped(dev)))
  382. netif_wake_queue(dev);
  383. }
  384. }
  385. static void ocelot_fdma_tx_cleanup(struct ocelot *ocelot, int budget)
  386. {
  387. struct ocelot_fdma *fdma = ocelot->fdma;
  388. struct ocelot_fdma_tx_ring *tx_ring;
  389. struct ocelot_fdma_tx_buf *buf;
  390. unsigned int new_null_llp_idx;
  391. struct ocelot_fdma_dcb *dcb;
  392. bool end_of_list = false;
  393. struct sk_buff *skb;
  394. dma_addr_t dma;
  395. u32 dcb_llp;
  396. u16 ntc;
  397. int ret;
  398. tx_ring = &fdma->tx_ring;
  399. /* Purge the TX packets that have been sent up to the NULL llp or the
  400. * end of done list.
  401. */
  402. while (!ocelot_fdma_tx_ring_empty(fdma)) {
  403. ntc = tx_ring->next_to_clean;
  404. dcb = &tx_ring->dcbs[ntc];
  405. if (!(dcb->stat & MSCC_FDMA_DCB_STAT_PD))
  406. break;
  407. buf = &tx_ring->bufs[ntc];
  408. skb = buf->skb;
  409. dma_unmap_single(ocelot->dev, dma_unmap_addr(buf, dma_addr),
  410. skb->len, DMA_TO_DEVICE);
  411. napi_consume_skb(skb, budget);
  412. dcb_llp = dcb->llp;
  413. /* Only update after accessing all dcb fields */
  414. tx_ring->next_to_clean = ocelot_fdma_idx_next(ntc,
  415. OCELOT_FDMA_TX_RING_SIZE);
  416. /* If we hit the NULL LLP, stop, we might need to reload FDMA */
  417. if (dcb_llp == 0) {
  418. end_of_list = true;
  419. break;
  420. }
  421. }
  422. /* No need to try to wake if there were no TX cleaned_cnt up. */
  423. if (ocelot_fdma_tx_ring_free(fdma))
  424. ocelot_fdma_wakeup_netdev(ocelot);
  425. /* If there is still some DCBs to be processed by the FDMA or if the
  426. * pending list is empty, there is no need to restart the FDMA.
  427. */
  428. if (!end_of_list || ocelot_fdma_tx_ring_empty(fdma))
  429. return;
  430. ret = ocelot_fdma_wait_chan_safe(ocelot, MSCC_FDMA_INJ_CHAN);
  431. if (ret) {
  432. dev_warn(ocelot->dev,
  433. "Failed to wait for TX channel to stop\n");
  434. return;
  435. }
  436. /* Set NULL LLP to be the last DCB used */
  437. new_null_llp_idx = ocelot_fdma_idx_prev(tx_ring->next_to_use,
  438. OCELOT_FDMA_TX_RING_SIZE);
  439. dcb = &tx_ring->dcbs[new_null_llp_idx];
  440. dcb->llp = 0;
  441. dma = ocelot_fdma_idx_dma(tx_ring->dcbs_dma, tx_ring->next_to_clean);
  442. ocelot_fdma_activate_chan(ocelot, dma, MSCC_FDMA_INJ_CHAN);
  443. }
  444. static int ocelot_fdma_napi_poll(struct napi_struct *napi, int budget)
  445. {
  446. struct ocelot_fdma *fdma = container_of(napi, struct ocelot_fdma, napi);
  447. struct ocelot *ocelot = fdma->ocelot;
  448. int work_done = 0;
  449. bool rx_stopped;
  450. ocelot_fdma_tx_cleanup(ocelot, budget);
  451. rx_stopped = ocelot_fdma_check_stop_rx(ocelot);
  452. work_done = ocelot_fdma_rx_get(ocelot, budget);
  453. if (rx_stopped)
  454. ocelot_fdma_rx_restart(ocelot);
  455. if (work_done < budget) {
  456. napi_complete_done(&fdma->napi, work_done);
  457. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA,
  458. BIT(MSCC_FDMA_INJ_CHAN) |
  459. BIT(MSCC_FDMA_XTR_CHAN));
  460. }
  461. return work_done;
  462. }
  463. static irqreturn_t ocelot_fdma_interrupt(int irq, void *dev_id)
  464. {
  465. u32 ident, llp, frm, err, err_code;
  466. struct ocelot *ocelot = dev_id;
  467. ident = ocelot_fdma_readl(ocelot, MSCC_FDMA_INTR_IDENT);
  468. frm = ocelot_fdma_readl(ocelot, MSCC_FDMA_INTR_FRM);
  469. llp = ocelot_fdma_readl(ocelot, MSCC_FDMA_INTR_LLP);
  470. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_LLP, llp & ident);
  471. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_FRM, frm & ident);
  472. if (frm || llp) {
  473. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA, 0);
  474. napi_schedule(&ocelot->fdma->napi);
  475. }
  476. err = ocelot_fdma_readl(ocelot, MSCC_FDMA_EVT_ERR);
  477. if (unlikely(err)) {
  478. err_code = ocelot_fdma_readl(ocelot, MSCC_FDMA_EVT_ERR_CODE);
  479. dev_err_ratelimited(ocelot->dev,
  480. "Error ! chans mask: %#x, code: %#x\n",
  481. err, err_code);
  482. ocelot_fdma_writel(ocelot, MSCC_FDMA_EVT_ERR, err);
  483. ocelot_fdma_writel(ocelot, MSCC_FDMA_EVT_ERR_CODE, err_code);
  484. }
  485. return IRQ_HANDLED;
  486. }
  487. static void ocelot_fdma_send_skb(struct ocelot *ocelot,
  488. struct ocelot_fdma *fdma, struct sk_buff *skb)
  489. {
  490. struct ocelot_fdma_tx_ring *tx_ring = &fdma->tx_ring;
  491. struct ocelot_fdma_tx_buf *tx_buf;
  492. struct ocelot_fdma_dcb *dcb;
  493. dma_addr_t dma;
  494. u16 next_idx;
  495. dcb = &tx_ring->dcbs[tx_ring->next_to_use];
  496. tx_buf = &tx_ring->bufs[tx_ring->next_to_use];
  497. if (!ocelot_fdma_tx_dcb_set_skb(ocelot, tx_buf, dcb, skb)) {
  498. dev_kfree_skb_any(skb);
  499. return;
  500. }
  501. next_idx = ocelot_fdma_idx_next(tx_ring->next_to_use,
  502. OCELOT_FDMA_TX_RING_SIZE);
  503. skb_tx_timestamp(skb);
  504. /* If the FDMA TX chan is empty, then enqueue the DCB directly */
  505. if (ocelot_fdma_tx_ring_empty(fdma)) {
  506. dma = ocelot_fdma_idx_dma(tx_ring->dcbs_dma,
  507. tx_ring->next_to_use);
  508. ocelot_fdma_activate_chan(ocelot, dma, MSCC_FDMA_INJ_CHAN);
  509. } else {
  510. /* Chain the DCBs */
  511. dcb->llp = ocelot_fdma_idx_dma(tx_ring->dcbs_dma, next_idx);
  512. }
  513. tx_ring->next_to_use = next_idx;
  514. }
  515. static int ocelot_fdma_prepare_skb(struct ocelot *ocelot, int port, u32 rew_op,
  516. struct sk_buff *skb, struct net_device *dev)
  517. {
  518. int needed_headroom = max_t(int, OCELOT_TAG_LEN - skb_headroom(skb), 0);
  519. int needed_tailroom = max_t(int, ETH_FCS_LEN - skb_tailroom(skb), 0);
  520. void *ifh;
  521. int err;
  522. if (unlikely(needed_headroom || needed_tailroom ||
  523. skb_header_cloned(skb))) {
  524. err = pskb_expand_head(skb, needed_headroom, needed_tailroom,
  525. GFP_ATOMIC);
  526. if (unlikely(err)) {
  527. dev_kfree_skb_any(skb);
  528. return 1;
  529. }
  530. }
  531. err = skb_linearize(skb);
  532. if (err) {
  533. net_err_ratelimited("%s: skb_linearize error (%d)!\n",
  534. dev->name, err);
  535. dev_kfree_skb_any(skb);
  536. return 1;
  537. }
  538. ifh = skb_push(skb, OCELOT_TAG_LEN);
  539. skb_put(skb, ETH_FCS_LEN);
  540. memset(ifh, 0, OCELOT_TAG_LEN);
  541. ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
  542. return 0;
  543. }
  544. int ocelot_fdma_inject_frame(struct ocelot *ocelot, int port, u32 rew_op,
  545. struct sk_buff *skb, struct net_device *dev)
  546. {
  547. struct ocelot_fdma *fdma = ocelot->fdma;
  548. int ret = NETDEV_TX_OK;
  549. spin_lock(&fdma->tx_ring.xmit_lock);
  550. if (ocelot_fdma_tx_ring_free(fdma) == 0) {
  551. netif_stop_queue(dev);
  552. ret = NETDEV_TX_BUSY;
  553. goto out;
  554. }
  555. if (ocelot_fdma_prepare_skb(ocelot, port, rew_op, skb, dev))
  556. goto out;
  557. ocelot_fdma_send_skb(ocelot, fdma, skb);
  558. out:
  559. spin_unlock(&fdma->tx_ring.xmit_lock);
  560. return ret;
  561. }
  562. static void ocelot_fdma_free_rx_ring(struct ocelot *ocelot)
  563. {
  564. struct ocelot_fdma *fdma = ocelot->fdma;
  565. struct ocelot_fdma_rx_ring *rx_ring;
  566. struct ocelot_fdma_rx_buf *rxb;
  567. u16 idx;
  568. rx_ring = &fdma->rx_ring;
  569. idx = rx_ring->next_to_clean;
  570. /* Free the pages held in the RX ring */
  571. while (idx != rx_ring->next_to_use) {
  572. rxb = &rx_ring->bufs[idx];
  573. dma_unmap_page(ocelot->dev, rxb->dma_addr, PAGE_SIZE,
  574. DMA_FROM_DEVICE);
  575. __free_page(rxb->page);
  576. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  577. }
  578. if (fdma->rx_ring.skb)
  579. dev_kfree_skb_any(fdma->rx_ring.skb);
  580. }
  581. static void ocelot_fdma_free_tx_ring(struct ocelot *ocelot)
  582. {
  583. struct ocelot_fdma *fdma = ocelot->fdma;
  584. struct ocelot_fdma_tx_ring *tx_ring;
  585. struct ocelot_fdma_tx_buf *txb;
  586. struct sk_buff *skb;
  587. u16 idx;
  588. tx_ring = &fdma->tx_ring;
  589. idx = tx_ring->next_to_clean;
  590. while (idx != tx_ring->next_to_use) {
  591. txb = &tx_ring->bufs[idx];
  592. skb = txb->skb;
  593. dma_unmap_single(ocelot->dev, dma_unmap_addr(txb, dma_addr),
  594. skb->len, DMA_TO_DEVICE);
  595. dev_kfree_skb_any(skb);
  596. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_TX_RING_SIZE);
  597. }
  598. }
  599. static int ocelot_fdma_rings_alloc(struct ocelot *ocelot)
  600. {
  601. struct ocelot_fdma *fdma = ocelot->fdma;
  602. struct ocelot_fdma_dcb *dcbs;
  603. unsigned int adjust;
  604. dma_addr_t dcbs_dma;
  605. int ret;
  606. /* Create a pool of consistent memory blocks for hardware descriptors */
  607. fdma->dcbs_base = dmam_alloc_coherent(ocelot->dev,
  608. OCELOT_DCBS_HW_ALLOC_SIZE,
  609. &fdma->dcbs_dma_base, GFP_KERNEL);
  610. if (!fdma->dcbs_base)
  611. return -ENOMEM;
  612. /* DCBs must be aligned on a 32bit boundary */
  613. dcbs = fdma->dcbs_base;
  614. dcbs_dma = fdma->dcbs_dma_base;
  615. if (!IS_ALIGNED(dcbs_dma, 4)) {
  616. adjust = dcbs_dma & 0x3;
  617. dcbs_dma = ALIGN(dcbs_dma, 4);
  618. dcbs = (void *)dcbs + adjust;
  619. }
  620. /* TX queue */
  621. fdma->tx_ring.dcbs = dcbs;
  622. fdma->tx_ring.dcbs_dma = dcbs_dma;
  623. spin_lock_init(&fdma->tx_ring.xmit_lock);
  624. /* RX queue */
  625. fdma->rx_ring.dcbs = dcbs + OCELOT_FDMA_TX_RING_SIZE;
  626. fdma->rx_ring.dcbs_dma = dcbs_dma + OCELOT_FDMA_TX_DCB_SIZE;
  627. ret = ocelot_fdma_alloc_rx_buffs(ocelot,
  628. ocelot_fdma_tx_ring_free(fdma));
  629. if (ret) {
  630. ocelot_fdma_free_rx_ring(ocelot);
  631. return ret;
  632. }
  633. /* Set the last DCB LLP as NULL, this is normally done when restarting
  634. * the RX chan, but this is for the first run
  635. */
  636. ocelot_fdma_rx_set_llp(&fdma->rx_ring);
  637. return 0;
  638. }
  639. void ocelot_fdma_netdev_init(struct ocelot *ocelot, struct net_device *dev)
  640. {
  641. struct ocelot_fdma *fdma = ocelot->fdma;
  642. dev->needed_headroom = OCELOT_TAG_LEN;
  643. dev->needed_tailroom = ETH_FCS_LEN;
  644. if (fdma->ndev)
  645. return;
  646. fdma->ndev = dev;
  647. netif_napi_add_weight(dev, &fdma->napi, ocelot_fdma_napi_poll,
  648. OCELOT_FDMA_WEIGHT);
  649. }
  650. void ocelot_fdma_netdev_deinit(struct ocelot *ocelot, struct net_device *dev)
  651. {
  652. struct ocelot_fdma *fdma = ocelot->fdma;
  653. if (fdma->ndev == dev) {
  654. netif_napi_del(&fdma->napi);
  655. fdma->ndev = NULL;
  656. }
  657. }
  658. void ocelot_fdma_init(struct platform_device *pdev, struct ocelot *ocelot)
  659. {
  660. struct device *dev = ocelot->dev;
  661. struct ocelot_fdma *fdma;
  662. int ret;
  663. fdma = devm_kzalloc(dev, sizeof(*fdma), GFP_KERNEL);
  664. if (!fdma)
  665. return;
  666. ocelot->fdma = fdma;
  667. ocelot->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  668. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA, 0);
  669. fdma->ocelot = ocelot;
  670. fdma->irq = platform_get_irq_byname(pdev, "fdma");
  671. ret = devm_request_irq(dev, fdma->irq, ocelot_fdma_interrupt, 0,
  672. dev_name(dev), ocelot);
  673. if (ret)
  674. goto err_free_fdma;
  675. ret = ocelot_fdma_rings_alloc(ocelot);
  676. if (ret)
  677. goto err_free_irq;
  678. static_branch_enable(&ocelot_fdma_enabled);
  679. return;
  680. err_free_irq:
  681. devm_free_irq(dev, fdma->irq, fdma);
  682. err_free_fdma:
  683. devm_kfree(dev, fdma);
  684. ocelot->fdma = NULL;
  685. }
  686. void ocelot_fdma_start(struct ocelot *ocelot)
  687. {
  688. struct ocelot_fdma *fdma = ocelot->fdma;
  689. /* Reconfigure for extraction and injection using DMA */
  690. ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_MODE(2), QS_INJ_GRP_CFG, 0);
  691. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(0), QS_INJ_CTRL, 0);
  692. ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_MODE(2), QS_XTR_GRP_CFG, 0);
  693. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_LLP, 0xffffffff);
  694. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_FRM, 0xffffffff);
  695. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_LLP_ENA,
  696. BIT(MSCC_FDMA_INJ_CHAN) | BIT(MSCC_FDMA_XTR_CHAN));
  697. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_FRM_ENA,
  698. BIT(MSCC_FDMA_XTR_CHAN));
  699. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA,
  700. BIT(MSCC_FDMA_INJ_CHAN) | BIT(MSCC_FDMA_XTR_CHAN));
  701. napi_enable(&fdma->napi);
  702. ocelot_fdma_activate_chan(ocelot, ocelot->fdma->rx_ring.dcbs_dma,
  703. MSCC_FDMA_XTR_CHAN);
  704. }
  705. void ocelot_fdma_deinit(struct ocelot *ocelot)
  706. {
  707. struct ocelot_fdma *fdma = ocelot->fdma;
  708. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA, 0);
  709. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_FORCEDIS,
  710. BIT(MSCC_FDMA_XTR_CHAN));
  711. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_FORCEDIS,
  712. BIT(MSCC_FDMA_INJ_CHAN));
  713. napi_synchronize(&fdma->napi);
  714. napi_disable(&fdma->napi);
  715. ocelot_fdma_free_rx_ring(ocelot);
  716. ocelot_fdma_free_tx_ring(ocelot);
  717. }