hnae.c 10 KB

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