ocelot_fdma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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/skbuff.h>
  15. #include "ocelot_fdma.h"
  16. #include "ocelot_qs.h"
  17. DEFINE_STATIC_KEY_FALSE(ocelot_fdma_enabled);
  18. static void ocelot_fdma_writel(struct ocelot *ocelot, u32 reg, u32 data)
  19. {
  20. regmap_write(ocelot->targets[FDMA], reg, data);
  21. }
  22. static u32 ocelot_fdma_readl(struct ocelot *ocelot, u32 reg)
  23. {
  24. u32 retval;
  25. regmap_read(ocelot->targets[FDMA], reg, &retval);
  26. return retval;
  27. }
  28. static dma_addr_t ocelot_fdma_idx_dma(dma_addr_t base, u16 idx)
  29. {
  30. return base + idx * sizeof(struct ocelot_fdma_dcb);
  31. }
  32. static u16 ocelot_fdma_dma_idx(dma_addr_t base, dma_addr_t dma)
  33. {
  34. return (dma - base) / sizeof(struct ocelot_fdma_dcb);
  35. }
  36. static u16 ocelot_fdma_idx_next(u16 idx, u16 ring_sz)
  37. {
  38. return unlikely(idx == ring_sz - 1) ? 0 : idx + 1;
  39. }
  40. static u16 ocelot_fdma_idx_prev(u16 idx, u16 ring_sz)
  41. {
  42. return unlikely(idx == 0) ? ring_sz - 1 : idx - 1;
  43. }
  44. static int ocelot_fdma_rx_ring_free(struct ocelot_fdma *fdma)
  45. {
  46. struct ocelot_fdma_rx_ring *rx_ring = &fdma->rx_ring;
  47. if (rx_ring->next_to_use >= rx_ring->next_to_clean)
  48. return OCELOT_FDMA_RX_RING_SIZE -
  49. (rx_ring->next_to_use - rx_ring->next_to_clean) - 1;
  50. else
  51. return rx_ring->next_to_clean - rx_ring->next_to_use - 1;
  52. }
  53. static int ocelot_fdma_tx_ring_free(struct ocelot_fdma *fdma)
  54. {
  55. struct ocelot_fdma_tx_ring *tx_ring = &fdma->tx_ring;
  56. if (tx_ring->next_to_use >= tx_ring->next_to_clean)
  57. return OCELOT_FDMA_TX_RING_SIZE -
  58. (tx_ring->next_to_use - tx_ring->next_to_clean) - 1;
  59. else
  60. return tx_ring->next_to_clean - tx_ring->next_to_use - 1;
  61. }
  62. static bool ocelot_fdma_tx_ring_empty(struct ocelot_fdma *fdma)
  63. {
  64. struct ocelot_fdma_tx_ring *tx_ring = &fdma->tx_ring;
  65. return tx_ring->next_to_clean == tx_ring->next_to_use;
  66. }
  67. static void ocelot_fdma_activate_chan(struct ocelot *ocelot, dma_addr_t dma,
  68. int chan)
  69. {
  70. ocelot_fdma_writel(ocelot, MSCC_FDMA_DCB_LLP(chan), dma);
  71. /* Barrier to force memory writes to DCB to be completed before starting
  72. * the channel.
  73. */
  74. wmb();
  75. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_ACTIVATE, BIT(chan));
  76. }
  77. static u32 ocelot_fdma_read_ch_safe(struct ocelot *ocelot)
  78. {
  79. return ocelot_fdma_readl(ocelot, MSCC_FDMA_CH_SAFE);
  80. }
  81. static int ocelot_fdma_wait_chan_safe(struct ocelot *ocelot, int chan)
  82. {
  83. u32 safe;
  84. return readx_poll_timeout_atomic(ocelot_fdma_read_ch_safe, ocelot, safe,
  85. safe & BIT(chan), 0,
  86. OCELOT_FDMA_CH_SAFE_TIMEOUT_US);
  87. }
  88. static void ocelot_fdma_dcb_set_data(struct ocelot_fdma_dcb *dcb,
  89. dma_addr_t dma_addr,
  90. size_t size)
  91. {
  92. u32 offset = dma_addr & 0x3;
  93. dcb->llp = 0;
  94. dcb->datap = ALIGN_DOWN(dma_addr, 4);
  95. dcb->datal = ALIGN_DOWN(size, 4);
  96. dcb->stat = MSCC_FDMA_DCB_STAT_BLOCKO(offset);
  97. }
  98. static bool ocelot_fdma_rx_alloc_page(struct ocelot *ocelot,
  99. struct ocelot_fdma_rx_buf *rxb)
  100. {
  101. dma_addr_t mapping;
  102. struct page *page;
  103. page = dev_alloc_page();
  104. if (unlikely(!page))
  105. return false;
  106. mapping = dma_map_page(ocelot->dev, page, 0, PAGE_SIZE,
  107. DMA_FROM_DEVICE);
  108. if (unlikely(dma_mapping_error(ocelot->dev, mapping))) {
  109. __free_page(page);
  110. return false;
  111. }
  112. rxb->page = page;
  113. rxb->page_offset = 0;
  114. rxb->dma_addr = mapping;
  115. return true;
  116. }
  117. static int ocelot_fdma_alloc_rx_buffs(struct ocelot *ocelot, u16 alloc_cnt)
  118. {
  119. struct ocelot_fdma *fdma = ocelot->fdma;
  120. struct ocelot_fdma_rx_ring *rx_ring;
  121. struct ocelot_fdma_rx_buf *rxb;
  122. struct ocelot_fdma_dcb *dcb;
  123. dma_addr_t dma_addr;
  124. int ret = 0;
  125. u16 idx;
  126. rx_ring = &fdma->rx_ring;
  127. idx = rx_ring->next_to_use;
  128. while (alloc_cnt--) {
  129. rxb = &rx_ring->bufs[idx];
  130. /* try reuse page */
  131. if (unlikely(!rxb->page)) {
  132. if (unlikely(!ocelot_fdma_rx_alloc_page(ocelot, rxb))) {
  133. dev_err_ratelimited(ocelot->dev,
  134. "Failed to allocate rx\n");
  135. ret = -ENOMEM;
  136. break;
  137. }
  138. }
  139. dcb = &rx_ring->dcbs[idx];
  140. dma_addr = rxb->dma_addr + rxb->page_offset;
  141. ocelot_fdma_dcb_set_data(dcb, dma_addr, OCELOT_FDMA_RXB_SIZE);
  142. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  143. /* Chain the DCB to the next one */
  144. dcb->llp = ocelot_fdma_idx_dma(rx_ring->dcbs_dma, idx);
  145. }
  146. rx_ring->next_to_use = idx;
  147. rx_ring->next_to_alloc = idx;
  148. return ret;
  149. }
  150. static bool ocelot_fdma_tx_dcb_set_skb(struct ocelot *ocelot,
  151. struct ocelot_fdma_tx_buf *tx_buf,
  152. struct ocelot_fdma_dcb *dcb,
  153. struct sk_buff *skb)
  154. {
  155. dma_addr_t mapping;
  156. mapping = dma_map_single(ocelot->dev, skb->data, skb->len,
  157. DMA_TO_DEVICE);
  158. if (unlikely(dma_mapping_error(ocelot->dev, mapping)))
  159. return false;
  160. dma_unmap_addr_set(tx_buf, dma_addr, mapping);
  161. ocelot_fdma_dcb_set_data(dcb, mapping, OCELOT_FDMA_RX_SIZE);
  162. tx_buf->skb = skb;
  163. dcb->stat |= MSCC_FDMA_DCB_STAT_BLOCKL(skb->len);
  164. dcb->stat |= MSCC_FDMA_DCB_STAT_SOF | MSCC_FDMA_DCB_STAT_EOF;
  165. return true;
  166. }
  167. static bool ocelot_fdma_check_stop_rx(struct ocelot *ocelot)
  168. {
  169. u32 llp;
  170. /* Check if the FDMA hits the DCB with LLP == NULL */
  171. llp = ocelot_fdma_readl(ocelot, MSCC_FDMA_DCB_LLP(MSCC_FDMA_XTR_CHAN));
  172. if (unlikely(llp))
  173. return false;
  174. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_DISABLE,
  175. BIT(MSCC_FDMA_XTR_CHAN));
  176. return true;
  177. }
  178. static void ocelot_fdma_rx_set_llp(struct ocelot_fdma_rx_ring *rx_ring)
  179. {
  180. struct ocelot_fdma_dcb *dcb;
  181. unsigned int idx;
  182. idx = ocelot_fdma_idx_prev(rx_ring->next_to_use,
  183. OCELOT_FDMA_RX_RING_SIZE);
  184. dcb = &rx_ring->dcbs[idx];
  185. dcb->llp = 0;
  186. }
  187. static void ocelot_fdma_rx_restart(struct ocelot *ocelot)
  188. {
  189. struct ocelot_fdma *fdma = ocelot->fdma;
  190. struct ocelot_fdma_rx_ring *rx_ring;
  191. const u8 chan = MSCC_FDMA_XTR_CHAN;
  192. dma_addr_t new_llp, dma_base;
  193. unsigned int idx;
  194. u32 llp_prev;
  195. int ret;
  196. rx_ring = &fdma->rx_ring;
  197. ret = ocelot_fdma_wait_chan_safe(ocelot, chan);
  198. if (ret) {
  199. dev_err_ratelimited(ocelot->dev,
  200. "Unable to stop RX channel\n");
  201. return;
  202. }
  203. ocelot_fdma_rx_set_llp(rx_ring);
  204. /* FDMA stopped on the last DCB that contained a NULL LLP, since
  205. * we processed some DCBs in RX, there is free space, and we must set
  206. * DCB_LLP to point to the next DCB
  207. */
  208. llp_prev = ocelot_fdma_readl(ocelot, MSCC_FDMA_DCB_LLP_PREV(chan));
  209. dma_base = rx_ring->dcbs_dma;
  210. /* Get the next DMA addr located after LLP == NULL DCB */
  211. idx = ocelot_fdma_dma_idx(dma_base, llp_prev);
  212. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  213. new_llp = ocelot_fdma_idx_dma(dma_base, idx);
  214. /* Finally reactivate the channel */
  215. ocelot_fdma_activate_chan(ocelot, new_llp, chan);
  216. }
  217. static bool ocelot_fdma_add_rx_frag(struct ocelot_fdma_rx_buf *rxb, u32 stat,
  218. struct sk_buff *skb, bool first)
  219. {
  220. int size = MSCC_FDMA_DCB_STAT_BLOCKL(stat);
  221. struct page *page = rxb->page;
  222. if (likely(first)) {
  223. skb_put(skb, size);
  224. } else {
  225. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  226. rxb->page_offset, size, OCELOT_FDMA_RX_SIZE);
  227. }
  228. /* Try to reuse page */
  229. if (unlikely(page_ref_count(page) != 1 || page_is_pfmemalloc(page)))
  230. return false;
  231. /* Change offset to the other half */
  232. rxb->page_offset ^= OCELOT_FDMA_RX_SIZE;
  233. page_ref_inc(page);
  234. return true;
  235. }
  236. static void ocelot_fdma_reuse_rx_page(struct ocelot *ocelot,
  237. struct ocelot_fdma_rx_buf *old_rxb)
  238. {
  239. struct ocelot_fdma_rx_ring *rx_ring = &ocelot->fdma->rx_ring;
  240. struct ocelot_fdma_rx_buf *new_rxb;
  241. new_rxb = &rx_ring->bufs[rx_ring->next_to_alloc];
  242. rx_ring->next_to_alloc = ocelot_fdma_idx_next(rx_ring->next_to_alloc,
  243. OCELOT_FDMA_RX_RING_SIZE);
  244. /* Copy page reference */
  245. *new_rxb = *old_rxb;
  246. /* Sync for use by the device */
  247. dma_sync_single_range_for_device(ocelot->dev, old_rxb->dma_addr,
  248. old_rxb->page_offset,
  249. OCELOT_FDMA_RX_SIZE, DMA_FROM_DEVICE);
  250. }
  251. static struct sk_buff *ocelot_fdma_get_skb(struct ocelot *ocelot, u32 stat,
  252. struct ocelot_fdma_rx_buf *rxb,
  253. struct sk_buff *skb)
  254. {
  255. bool first = false;
  256. /* Allocate skb head and data */
  257. if (likely(!skb)) {
  258. void *buff_addr = page_address(rxb->page) +
  259. rxb->page_offset;
  260. skb = build_skb(buff_addr, OCELOT_FDMA_SKBFRAG_SIZE);
  261. if (unlikely(!skb)) {
  262. dev_err_ratelimited(ocelot->dev,
  263. "build_skb failed !\n");
  264. return NULL;
  265. }
  266. first = true;
  267. }
  268. dma_sync_single_range_for_cpu(ocelot->dev, rxb->dma_addr,
  269. rxb->page_offset, OCELOT_FDMA_RX_SIZE,
  270. DMA_FROM_DEVICE);
  271. if (ocelot_fdma_add_rx_frag(rxb, stat, skb, first)) {
  272. /* Reuse the free half of the page for the next_to_alloc DCB*/
  273. ocelot_fdma_reuse_rx_page(ocelot, rxb);
  274. } else {
  275. /* page cannot be reused, unmap it */
  276. dma_unmap_page(ocelot->dev, rxb->dma_addr, PAGE_SIZE,
  277. DMA_FROM_DEVICE);
  278. }
  279. /* clear rx buff content */
  280. rxb->page = NULL;
  281. return skb;
  282. }
  283. static bool ocelot_fdma_receive_skb(struct ocelot *ocelot, struct sk_buff *skb)
  284. {
  285. struct net_device *ndev;
  286. void *xfh = skb->data;
  287. u64 timestamp;
  288. u64 src_port;
  289. skb_pull(skb, OCELOT_TAG_LEN);
  290. ocelot_xfh_get_src_port(xfh, &src_port);
  291. if (unlikely(src_port >= ocelot->num_phys_ports))
  292. return false;
  293. ndev = ocelot_port_to_netdev(ocelot, src_port);
  294. if (unlikely(!ndev))
  295. return false;
  296. if (pskb_trim(skb, skb->len - ETH_FCS_LEN))
  297. return false;
  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. ocelot_ifh_set_basic(ifh, ocelot, port, rew_op, skb);
  541. return 0;
  542. }
  543. int ocelot_fdma_inject_frame(struct ocelot *ocelot, int port, u32 rew_op,
  544. struct sk_buff *skb, struct net_device *dev)
  545. {
  546. struct ocelot_fdma *fdma = ocelot->fdma;
  547. int ret = NETDEV_TX_OK;
  548. spin_lock(&fdma->tx_ring.xmit_lock);
  549. if (ocelot_fdma_tx_ring_free(fdma) == 0) {
  550. netif_stop_queue(dev);
  551. ret = NETDEV_TX_BUSY;
  552. goto out;
  553. }
  554. if (ocelot_fdma_prepare_skb(ocelot, port, rew_op, skb, dev))
  555. goto out;
  556. ocelot_fdma_send_skb(ocelot, fdma, skb);
  557. out:
  558. spin_unlock(&fdma->tx_ring.xmit_lock);
  559. return ret;
  560. }
  561. static void ocelot_fdma_free_rx_ring(struct ocelot *ocelot)
  562. {
  563. struct ocelot_fdma *fdma = ocelot->fdma;
  564. struct ocelot_fdma_rx_ring *rx_ring;
  565. struct ocelot_fdma_rx_buf *rxb;
  566. u16 idx;
  567. rx_ring = &fdma->rx_ring;
  568. idx = rx_ring->next_to_clean;
  569. /* Free the pages held in the RX ring */
  570. while (idx != rx_ring->next_to_use) {
  571. rxb = &rx_ring->bufs[idx];
  572. dma_unmap_page(ocelot->dev, rxb->dma_addr, PAGE_SIZE,
  573. DMA_FROM_DEVICE);
  574. __free_page(rxb->page);
  575. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_RX_RING_SIZE);
  576. }
  577. if (fdma->rx_ring.skb)
  578. dev_kfree_skb_any(fdma->rx_ring.skb);
  579. }
  580. static void ocelot_fdma_free_tx_ring(struct ocelot *ocelot)
  581. {
  582. struct ocelot_fdma *fdma = ocelot->fdma;
  583. struct ocelot_fdma_tx_ring *tx_ring;
  584. struct ocelot_fdma_tx_buf *txb;
  585. struct sk_buff *skb;
  586. u16 idx;
  587. tx_ring = &fdma->tx_ring;
  588. idx = tx_ring->next_to_clean;
  589. while (idx != tx_ring->next_to_use) {
  590. txb = &tx_ring->bufs[idx];
  591. skb = txb->skb;
  592. dma_unmap_single(ocelot->dev, dma_unmap_addr(txb, dma_addr),
  593. skb->len, DMA_TO_DEVICE);
  594. dev_kfree_skb_any(skb);
  595. idx = ocelot_fdma_idx_next(idx, OCELOT_FDMA_TX_RING_SIZE);
  596. }
  597. }
  598. static int ocelot_fdma_rings_alloc(struct ocelot *ocelot)
  599. {
  600. struct ocelot_fdma *fdma = ocelot->fdma;
  601. struct ocelot_fdma_dcb *dcbs;
  602. unsigned int adjust;
  603. dma_addr_t dcbs_dma;
  604. int ret;
  605. /* Create a pool of consistent memory blocks for hardware descriptors */
  606. fdma->dcbs_base = dmam_alloc_coherent(ocelot->dev,
  607. OCELOT_DCBS_HW_ALLOC_SIZE,
  608. &fdma->dcbs_dma_base, GFP_KERNEL);
  609. if (!fdma->dcbs_base)
  610. return -ENOMEM;
  611. /* DCBs must be aligned on a 32bit boundary */
  612. dcbs = fdma->dcbs_base;
  613. dcbs_dma = fdma->dcbs_dma_base;
  614. if (!IS_ALIGNED(dcbs_dma, 4)) {
  615. adjust = dcbs_dma & 0x3;
  616. dcbs_dma = ALIGN(dcbs_dma, 4);
  617. dcbs = (void *)dcbs + adjust;
  618. }
  619. /* TX queue */
  620. fdma->tx_ring.dcbs = dcbs;
  621. fdma->tx_ring.dcbs_dma = dcbs_dma;
  622. spin_lock_init(&fdma->tx_ring.xmit_lock);
  623. /* RX queue */
  624. fdma->rx_ring.dcbs = dcbs + OCELOT_FDMA_TX_RING_SIZE;
  625. fdma->rx_ring.dcbs_dma = dcbs_dma + OCELOT_FDMA_TX_DCB_SIZE;
  626. ret = ocelot_fdma_alloc_rx_buffs(ocelot,
  627. ocelot_fdma_tx_ring_free(fdma));
  628. if (ret) {
  629. ocelot_fdma_free_rx_ring(ocelot);
  630. return ret;
  631. }
  632. /* Set the last DCB LLP as NULL, this is normally done when restarting
  633. * the RX chan, but this is for the first run
  634. */
  635. ocelot_fdma_rx_set_llp(&fdma->rx_ring);
  636. return 0;
  637. }
  638. void ocelot_fdma_netdev_init(struct ocelot *ocelot, struct net_device *dev)
  639. {
  640. struct ocelot_fdma *fdma = ocelot->fdma;
  641. dev->needed_headroom = OCELOT_TAG_LEN;
  642. dev->needed_tailroom = ETH_FCS_LEN;
  643. if (fdma->ndev)
  644. return;
  645. fdma->ndev = dev;
  646. netif_napi_add_weight(dev, &fdma->napi, ocelot_fdma_napi_poll,
  647. OCELOT_FDMA_WEIGHT);
  648. }
  649. void ocelot_fdma_netdev_deinit(struct ocelot *ocelot, struct net_device *dev)
  650. {
  651. struct ocelot_fdma *fdma = ocelot->fdma;
  652. if (fdma->ndev == dev) {
  653. netif_napi_del(&fdma->napi);
  654. fdma->ndev = NULL;
  655. }
  656. }
  657. void ocelot_fdma_init(struct platform_device *pdev, struct ocelot *ocelot)
  658. {
  659. struct device *dev = ocelot->dev;
  660. struct ocelot_fdma *fdma;
  661. int ret;
  662. fdma = devm_kzalloc(dev, sizeof(*fdma), GFP_KERNEL);
  663. if (!fdma)
  664. return;
  665. ocelot->fdma = fdma;
  666. ocelot->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  667. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA, 0);
  668. fdma->ocelot = ocelot;
  669. fdma->irq = platform_get_irq_byname(pdev, "fdma");
  670. ret = devm_request_irq(dev, fdma->irq, ocelot_fdma_interrupt, 0,
  671. dev_name(dev), ocelot);
  672. if (ret)
  673. goto err_free_fdma;
  674. ret = ocelot_fdma_rings_alloc(ocelot);
  675. if (ret)
  676. goto err_free_irq;
  677. static_branch_enable(&ocelot_fdma_enabled);
  678. return;
  679. err_free_irq:
  680. devm_free_irq(dev, fdma->irq, fdma);
  681. err_free_fdma:
  682. devm_kfree(dev, fdma);
  683. ocelot->fdma = NULL;
  684. }
  685. void ocelot_fdma_start(struct ocelot *ocelot)
  686. {
  687. struct ocelot_fdma *fdma = ocelot->fdma;
  688. /* Reconfigure for extraction and injection using DMA */
  689. ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_MODE(2), QS_INJ_GRP_CFG, 0);
  690. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(0), QS_INJ_CTRL, 0);
  691. ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_MODE(2), QS_XTR_GRP_CFG, 0);
  692. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_LLP, 0xffffffff);
  693. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_FRM, 0xffffffff);
  694. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_LLP_ENA,
  695. BIT(MSCC_FDMA_INJ_CHAN) | BIT(MSCC_FDMA_XTR_CHAN));
  696. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_FRM_ENA,
  697. BIT(MSCC_FDMA_XTR_CHAN));
  698. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA,
  699. BIT(MSCC_FDMA_INJ_CHAN) | BIT(MSCC_FDMA_XTR_CHAN));
  700. napi_enable(&fdma->napi);
  701. ocelot_fdma_activate_chan(ocelot, ocelot->fdma->rx_ring.dcbs_dma,
  702. MSCC_FDMA_XTR_CHAN);
  703. }
  704. void ocelot_fdma_deinit(struct ocelot *ocelot)
  705. {
  706. struct ocelot_fdma *fdma = ocelot->fdma;
  707. ocelot_fdma_writel(ocelot, MSCC_FDMA_INTR_ENA, 0);
  708. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_FORCEDIS,
  709. BIT(MSCC_FDMA_XTR_CHAN));
  710. ocelot_fdma_writel(ocelot, MSCC_FDMA_CH_FORCEDIS,
  711. BIT(MSCC_FDMA_INJ_CHAN));
  712. napi_synchronize(&fdma->napi);
  713. napi_disable(&fdma->napi);
  714. ocelot_fdma_free_rx_ring(ocelot);
  715. ocelot_fdma_free_tx_ring(ocelot);
  716. }