hnae.c 10 KB

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