hnae.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014-2015 Hisilicon Limited.
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/of.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/slab.h>
  10. #include "hnae.h"
  11. #define cls_to_ae_dev(dev) container_of(dev, struct hnae_ae_dev, cls_dev)
  12. static struct class *hnae_class;
  13. static void
  14. hnae_list_add(spinlock_t *lock, struct list_head *node, struct list_head *head)
  15. {
  16. unsigned long flags;
  17. spin_lock_irqsave(lock, flags);
  18. list_add_tail_rcu(node, head);
  19. spin_unlock_irqrestore(lock, flags);
  20. }
  21. static void hnae_list_del(spinlock_t *lock, struct list_head *node)
  22. {
  23. unsigned long flags;
  24. spin_lock_irqsave(lock, flags);
  25. list_del_rcu(node);
  26. spin_unlock_irqrestore(lock, flags);
  27. }
  28. static int hnae_alloc_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
  29. {
  30. unsigned int order = hnae_page_order(ring);
  31. struct page *p = dev_alloc_pages(order);
  32. if (!p)
  33. return -ENOMEM;
  34. cb->priv = p;
  35. cb->page_offset = 0;
  36. cb->reuse_flag = 0;
  37. cb->buf = page_address(p);
  38. cb->length = hnae_page_size(ring);
  39. cb->type = DESC_TYPE_PAGE;
  40. return 0;
  41. }
  42. static void hnae_free_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
  43. {
  44. if (unlikely(!cb->priv))
  45. return;
  46. if (cb->type == DESC_TYPE_SKB)
  47. dev_kfree_skb_any((struct sk_buff *)cb->priv);
  48. else if (unlikely(is_rx_ring(ring)))
  49. put_page((struct page *)cb->priv);
  50. cb->priv = NULL;
  51. }
  52. static int hnae_map_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
  53. {
  54. cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
  55. cb->length, ring_to_dma_dir(ring));
  56. if (dma_mapping_error(ring_to_dev(ring), cb->dma))
  57. return -EIO;
  58. return 0;
  59. }
  60. static void hnae_unmap_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
  61. {
  62. if (cb->type == DESC_TYPE_SKB)
  63. dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
  64. ring_to_dma_dir(ring));
  65. else if (cb->length)
  66. dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
  67. ring_to_dma_dir(ring));
  68. }
  69. static struct hnae_buf_ops hnae_bops = {
  70. .alloc_buffer = hnae_alloc_buffer,
  71. .free_buffer = hnae_free_buffer,
  72. .map_buffer = hnae_map_buffer,
  73. .unmap_buffer = hnae_unmap_buffer,
  74. };
  75. static int __ae_match(struct device *dev, const void *data)
  76. {
  77. struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
  78. if (dev_of_node(hdev->dev))
  79. return (data == &hdev->dev->of_node->fwnode);
  80. else if (is_acpi_node(hdev->dev->fwnode))
  81. return (data == hdev->dev->fwnode);
  82. dev_err(dev, "__ae_match cannot read cfg data from OF or acpi\n");
  83. return 0;
  84. }
  85. static struct hnae_ae_dev *find_ae(const struct fwnode_handle *fwnode)
  86. {
  87. struct device *dev;
  88. WARN_ON(!fwnode);
  89. dev = class_find_device(hnae_class, NULL, fwnode, __ae_match);
  90. return dev ? cls_to_ae_dev(dev) : NULL;
  91. }
  92. static void hnae_free_buffers(struct hnae_ring *ring)
  93. {
  94. int i;
  95. for (i = 0; i < ring->desc_num; i++)
  96. hnae_free_buffer_detach(ring, i);
  97. }
  98. /* Allocate memory for raw pkg, and map with dma */
  99. static int hnae_alloc_buffers(struct hnae_ring *ring)
  100. {
  101. int i, j, ret;
  102. for (i = 0; i < ring->desc_num; i++) {
  103. ret = hnae_alloc_buffer_attach(ring, i);
  104. if (ret)
  105. goto out_buffer_fail;
  106. }
  107. return 0;
  108. out_buffer_fail:
  109. for (j = i - 1; j >= 0; j--)
  110. hnae_free_buffer_detach(ring, j);
  111. return ret;
  112. }
  113. /* free desc along with its attached buffer */
  114. static void hnae_free_desc(struct hnae_ring *ring)
  115. {
  116. dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
  117. ring->desc_num * sizeof(ring->desc[0]),
  118. ring_to_dma_dir(ring));
  119. ring->desc_dma_addr = 0;
  120. kfree(ring->desc);
  121. ring->desc = NULL;
  122. }
  123. /* alloc desc, without buffer attached */
  124. static int hnae_alloc_desc(struct hnae_ring *ring)
  125. {
  126. int size = ring->desc_num * sizeof(ring->desc[0]);
  127. ring->desc = kzalloc(size, GFP_KERNEL);
  128. if (!ring->desc)
  129. return -ENOMEM;
  130. ring->desc_dma_addr = dma_map_single(ring_to_dev(ring),
  131. ring->desc, size, ring_to_dma_dir(ring));
  132. if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
  133. ring->desc_dma_addr = 0;
  134. kfree(ring->desc);
  135. ring->desc = NULL;
  136. return -ENOMEM;
  137. }
  138. return 0;
  139. }
  140. /* fini ring, also free the buffer for the ring */
  141. static void hnae_fini_ring(struct hnae_ring *ring)
  142. {
  143. if (is_rx_ring(ring))
  144. hnae_free_buffers(ring);
  145. hnae_free_desc(ring);
  146. kfree(ring->desc_cb);
  147. ring->desc_cb = NULL;
  148. ring->next_to_clean = 0;
  149. ring->next_to_use = 0;
  150. }
  151. /* init ring, and with buffer for rx ring */
  152. static int
  153. hnae_init_ring(struct hnae_queue *q, struct hnae_ring *ring, int flags)
  154. {
  155. int ret;
  156. if (ring->desc_num <= 0 || ring->buf_size <= 0)
  157. return -EINVAL;
  158. ring->q = q;
  159. ring->flags = flags;
  160. ring->coal_param = q->handle->coal_param;
  161. assert(!ring->desc && !ring->desc_cb && !ring->desc_dma_addr);
  162. /* not matter for tx or rx ring, the ntc and ntc start from 0 */
  163. assert(ring->next_to_use == 0);
  164. assert(ring->next_to_clean == 0);
  165. ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
  166. GFP_KERNEL);
  167. if (!ring->desc_cb) {
  168. ret = -ENOMEM;
  169. goto out;
  170. }
  171. ret = hnae_alloc_desc(ring);
  172. if (ret)
  173. goto out_with_desc_cb;
  174. if (is_rx_ring(ring)) {
  175. ret = hnae_alloc_buffers(ring);
  176. if (ret)
  177. goto out_with_desc;
  178. }
  179. return 0;
  180. out_with_desc:
  181. hnae_free_desc(ring);
  182. out_with_desc_cb:
  183. kfree(ring->desc_cb);
  184. ring->desc_cb = NULL;
  185. out:
  186. return ret;
  187. }
  188. static int hnae_init_queue(struct hnae_handle *h, struct hnae_queue *q,
  189. struct hnae_ae_dev *dev)
  190. {
  191. int ret;
  192. q->dev = dev;
  193. q->handle = h;
  194. ret = hnae_init_ring(q, &q->tx_ring, q->tx_ring.flags | RINGF_DIR);
  195. if (ret)
  196. goto out;
  197. ret = hnae_init_ring(q, &q->rx_ring, q->rx_ring.flags & ~RINGF_DIR);
  198. if (ret)
  199. goto out_with_tx_ring;
  200. if (dev->ops->init_queue)
  201. dev->ops->init_queue(q);
  202. return 0;
  203. out_with_tx_ring:
  204. hnae_fini_ring(&q->tx_ring);
  205. out:
  206. return ret;
  207. }
  208. static void hnae_fini_queue(struct hnae_queue *q)
  209. {
  210. if (q->dev->ops->fini_queue)
  211. q->dev->ops->fini_queue(q);
  212. hnae_fini_ring(&q->tx_ring);
  213. hnae_fini_ring(&q->rx_ring);
  214. }
  215. /*
  216. * ae_chain - define ae chain head
  217. */
  218. static RAW_NOTIFIER_HEAD(ae_chain);
  219. int hnae_register_notifier(struct notifier_block *nb)
  220. {
  221. return raw_notifier_chain_register(&ae_chain, nb);
  222. }
  223. EXPORT_SYMBOL(hnae_register_notifier);
  224. void hnae_unregister_notifier(struct notifier_block *nb)
  225. {
  226. if (raw_notifier_chain_unregister(&ae_chain, nb))
  227. dev_err(NULL, "notifier chain unregister fail\n");
  228. }
  229. EXPORT_SYMBOL(hnae_unregister_notifier);
  230. int hnae_reinit_handle(struct hnae_handle *handle)
  231. {
  232. int i, j;
  233. int ret;
  234. for (i = 0; i < handle->q_num; i++) /* free ring*/
  235. hnae_fini_queue(handle->qs[i]);
  236. if (handle->dev->ops->reset)
  237. handle->dev->ops->reset(handle);
  238. for (i = 0; i < handle->q_num; i++) {/* reinit ring*/
  239. ret = hnae_init_queue(handle, handle->qs[i], handle->dev);
  240. if (ret)
  241. goto out_when_init_queue;
  242. }
  243. return 0;
  244. out_when_init_queue:
  245. for (j = i - 1; j >= 0; j--)
  246. hnae_fini_queue(handle->qs[j]);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL(hnae_reinit_handle);
  250. /* hnae_get_handle - get a handle from the AE
  251. * @owner_dev: the dev use this handle
  252. * @ae_id: the id of the ae to be used
  253. * @ae_opts: the options set for the handle
  254. * @bops: the callbacks for buffer management
  255. *
  256. * return handle ptr or ERR_PTR
  257. */
  258. struct hnae_handle *hnae_get_handle(struct device *owner_dev,
  259. const struct fwnode_handle *fwnode,
  260. u32 port_id,
  261. struct hnae_buf_ops *bops)
  262. {
  263. struct hnae_ae_dev *dev;
  264. struct hnae_handle *handle;
  265. int i, j;
  266. int ret;
  267. dev = find_ae(fwnode);
  268. if (!dev)
  269. return ERR_PTR(-ENODEV);
  270. handle = dev->ops->get_handle(dev, port_id);
  271. if (IS_ERR(handle)) {
  272. put_device(&dev->cls_dev);
  273. return handle;
  274. }
  275. handle->dev = dev;
  276. handle->owner_dev = owner_dev;
  277. handle->bops = bops ? bops : &hnae_bops;
  278. handle->eport_id = port_id;
  279. for (i = 0; i < handle->q_num; i++) {
  280. ret = hnae_init_queue(handle, handle->qs[i], dev);
  281. if (ret)
  282. goto out_when_init_queue;
  283. }
  284. __module_get(dev->owner);
  285. hnae_list_add(&dev->lock, &handle->node, &dev->handle_list);
  286. return handle;
  287. out_when_init_queue:
  288. for (j = i - 1; j >= 0; j--)
  289. hnae_fini_queue(handle->qs[j]);
  290. put_device(&dev->cls_dev);
  291. return ERR_PTR(-ENOMEM);
  292. }
  293. EXPORT_SYMBOL(hnae_get_handle);
  294. void hnae_put_handle(struct hnae_handle *h)
  295. {
  296. struct hnae_ae_dev *dev = h->dev;
  297. int i;
  298. for (i = 0; i < h->q_num; i++)
  299. hnae_fini_queue(h->qs[i]);
  300. if (h->dev->ops->reset)
  301. h->dev->ops->reset(h);
  302. hnae_list_del(&dev->lock, &h->node);
  303. if (dev->ops->put_handle)
  304. dev->ops->put_handle(h);
  305. module_put(dev->owner);
  306. put_device(&dev->cls_dev);
  307. }
  308. EXPORT_SYMBOL(hnae_put_handle);
  309. static void hnae_release(struct device *dev)
  310. {
  311. }
  312. /**
  313. * hnae_ae_register - register a AE engine to hnae framework
  314. * @hdev: the hnae ae engine device
  315. * @owner: the module who provides this dev
  316. * NOTE: the duplicated name will not be checked
  317. */
  318. int hnae_ae_register(struct hnae_ae_dev *hdev, struct module *owner)
  319. {
  320. static atomic_t id = ATOMIC_INIT(-1);
  321. int ret;
  322. if (!hdev->dev)
  323. return -ENODEV;
  324. if (!hdev->ops || !hdev->ops->get_handle ||
  325. !hdev->ops->toggle_ring_irq ||
  326. !hdev->ops->get_status || !hdev->ops->adjust_link)
  327. return -EINVAL;
  328. hdev->owner = owner;
  329. hdev->id = (int)atomic_inc_return(&id);
  330. hdev->cls_dev.parent = hdev->dev;
  331. hdev->cls_dev.class = hnae_class;
  332. hdev->cls_dev.release = hnae_release;
  333. (void)dev_set_name(&hdev->cls_dev, "hnae%d", hdev->id);
  334. ret = device_register(&hdev->cls_dev);
  335. if (ret) {
  336. put_device(&hdev->cls_dev);
  337. return ret;
  338. }
  339. __module_get(THIS_MODULE);
  340. INIT_LIST_HEAD(&hdev->handle_list);
  341. spin_lock_init(&hdev->lock);
  342. ret = raw_notifier_call_chain(&ae_chain, HNAE_AE_REGISTER, NULL);
  343. if (ret)
  344. dev_dbg(hdev->dev,
  345. "has not notifier for AE: %s\n", hdev->name);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(hnae_ae_register);
  349. /**
  350. * hnae_ae_unregister - unregisters a HNAE AE engine
  351. * @hdev: the device to unregister
  352. */
  353. void hnae_ae_unregister(struct hnae_ae_dev *hdev)
  354. {
  355. device_unregister(&hdev->cls_dev);
  356. module_put(THIS_MODULE);
  357. }
  358. EXPORT_SYMBOL(hnae_ae_unregister);
  359. static int __init hnae_init(void)
  360. {
  361. hnae_class = class_create(THIS_MODULE, "hnae");
  362. return PTR_ERR_OR_ZERO(hnae_class);
  363. }
  364. static void __exit hnae_exit(void)
  365. {
  366. class_destroy(hnae_class);
  367. }
  368. subsys_initcall(hnae_init);
  369. module_exit(hnae_exit);
  370. MODULE_AUTHOR("Hisilicon, Inc.");
  371. MODULE_LICENSE("GPL");
  372. MODULE_DESCRIPTION("Hisilicon Network Acceleration Engine Framework");
  373. /* vi: set tw=78 noet: */