hnae.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 if (cb->length)
  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. dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
  121. ring->desc_num * sizeof(ring->desc[0]),
  122. ring_to_dma_dir(ring));
  123. ring->desc_dma_addr = 0;
  124. kfree(ring->desc);
  125. ring->desc = NULL;
  126. }
  127. /* alloc desc, without buffer attached */
  128. static int hnae_alloc_desc(struct hnae_ring *ring)
  129. {
  130. int size = ring->desc_num * sizeof(ring->desc[0]);
  131. ring->desc = kzalloc(size, GFP_KERNEL);
  132. if (!ring->desc)
  133. return -ENOMEM;
  134. ring->desc_dma_addr = dma_map_single(ring_to_dev(ring),
  135. ring->desc, size, ring_to_dma_dir(ring));
  136. if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
  137. ring->desc_dma_addr = 0;
  138. kfree(ring->desc);
  139. ring->desc = NULL;
  140. return -ENOMEM;
  141. }
  142. return 0;
  143. }
  144. /* fini ring, also free the buffer for the ring */
  145. static void hnae_fini_ring(struct hnae_ring *ring)
  146. {
  147. if (is_rx_ring(ring))
  148. hnae_free_buffers(ring);
  149. hnae_free_desc(ring);
  150. kfree(ring->desc_cb);
  151. ring->desc_cb = NULL;
  152. ring->next_to_clean = 0;
  153. ring->next_to_use = 0;
  154. }
  155. /* init ring, and with buffer for rx ring */
  156. static int
  157. hnae_init_ring(struct hnae_queue *q, struct hnae_ring *ring, int flags)
  158. {
  159. int ret;
  160. if (ring->desc_num <= 0 || ring->buf_size <= 0)
  161. return -EINVAL;
  162. ring->q = q;
  163. ring->flags = flags;
  164. ring->coal_param = q->handle->coal_param;
  165. assert(!ring->desc && !ring->desc_cb && !ring->desc_dma_addr);
  166. /* not matter for tx or rx ring, the ntc and ntc start from 0 */
  167. assert(ring->next_to_use == 0);
  168. assert(ring->next_to_clean == 0);
  169. ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
  170. GFP_KERNEL);
  171. if (!ring->desc_cb) {
  172. ret = -ENOMEM;
  173. goto out;
  174. }
  175. ret = hnae_alloc_desc(ring);
  176. if (ret)
  177. goto out_with_desc_cb;
  178. if (is_rx_ring(ring)) {
  179. ret = hnae_alloc_buffers(ring);
  180. if (ret)
  181. goto out_with_desc;
  182. }
  183. return 0;
  184. out_with_desc:
  185. hnae_free_desc(ring);
  186. out_with_desc_cb:
  187. kfree(ring->desc_cb);
  188. ring->desc_cb = NULL;
  189. out:
  190. return ret;
  191. }
  192. static int hnae_init_queue(struct hnae_handle *h, struct hnae_queue *q,
  193. struct hnae_ae_dev *dev)
  194. {
  195. int ret;
  196. q->dev = dev;
  197. q->handle = h;
  198. ret = hnae_init_ring(q, &q->tx_ring, q->tx_ring.flags | RINGF_DIR);
  199. if (ret)
  200. goto out;
  201. ret = hnae_init_ring(q, &q->rx_ring, q->rx_ring.flags & ~RINGF_DIR);
  202. if (ret)
  203. goto out_with_tx_ring;
  204. if (dev->ops->init_queue)
  205. dev->ops->init_queue(q);
  206. return 0;
  207. out_with_tx_ring:
  208. hnae_fini_ring(&q->tx_ring);
  209. out:
  210. return ret;
  211. }
  212. static void hnae_fini_queue(struct hnae_queue *q)
  213. {
  214. if (q->dev->ops->fini_queue)
  215. q->dev->ops->fini_queue(q);
  216. hnae_fini_ring(&q->tx_ring);
  217. hnae_fini_ring(&q->rx_ring);
  218. }
  219. /**
  220. * ae_chain - define ae chain head
  221. */
  222. static RAW_NOTIFIER_HEAD(ae_chain);
  223. int hnae_register_notifier(struct notifier_block *nb)
  224. {
  225. return raw_notifier_chain_register(&ae_chain, nb);
  226. }
  227. EXPORT_SYMBOL(hnae_register_notifier);
  228. void hnae_unregister_notifier(struct notifier_block *nb)
  229. {
  230. if (raw_notifier_chain_unregister(&ae_chain, nb))
  231. dev_err(NULL, "notifier chain unregister fail\n");
  232. }
  233. EXPORT_SYMBOL(hnae_unregister_notifier);
  234. int hnae_reinit_handle(struct hnae_handle *handle)
  235. {
  236. int i, j;
  237. int ret;
  238. for (i = 0; i < handle->q_num; i++) /* free ring*/
  239. hnae_fini_queue(handle->qs[i]);
  240. if (handle->dev->ops->reset)
  241. handle->dev->ops->reset(handle);
  242. for (i = 0; i < handle->q_num; i++) {/* reinit ring*/
  243. ret = hnae_init_queue(handle, handle->qs[i], handle->dev);
  244. if (ret)
  245. goto out_when_init_queue;
  246. }
  247. return 0;
  248. out_when_init_queue:
  249. for (j = i - 1; j >= 0; j--)
  250. hnae_fini_queue(handle->qs[j]);
  251. return ret;
  252. }
  253. EXPORT_SYMBOL(hnae_reinit_handle);
  254. /* hnae_get_handle - get a handle from the AE
  255. * @owner_dev: the dev use this handle
  256. * @ae_id: the id of the ae to be used
  257. * @ae_opts: the options set for the handle
  258. * @bops: the callbacks for buffer management
  259. *
  260. * return handle ptr or ERR_PTR
  261. */
  262. struct hnae_handle *hnae_get_handle(struct device *owner_dev,
  263. const struct fwnode_handle *fwnode,
  264. u32 port_id,
  265. struct hnae_buf_ops *bops)
  266. {
  267. struct hnae_ae_dev *dev;
  268. struct hnae_handle *handle;
  269. int i, j;
  270. int ret;
  271. dev = find_ae(fwnode);
  272. if (!dev)
  273. return ERR_PTR(-ENODEV);
  274. handle = dev->ops->get_handle(dev, port_id);
  275. if (IS_ERR(handle)) {
  276. put_device(&dev->cls_dev);
  277. return handle;
  278. }
  279. handle->dev = dev;
  280. handle->owner_dev = owner_dev;
  281. handle->bops = bops ? bops : &hnae_bops;
  282. handle->eport_id = port_id;
  283. for (i = 0; i < handle->q_num; i++) {
  284. ret = hnae_init_queue(handle, handle->qs[i], dev);
  285. if (ret)
  286. goto out_when_init_queue;
  287. }
  288. __module_get(dev->owner);
  289. hnae_list_add(&dev->lock, &handle->node, &dev->handle_list);
  290. return handle;
  291. out_when_init_queue:
  292. for (j = i - 1; j >= 0; j--)
  293. hnae_fini_queue(handle->qs[j]);
  294. put_device(&dev->cls_dev);
  295. return ERR_PTR(-ENOMEM);
  296. }
  297. EXPORT_SYMBOL(hnae_get_handle);
  298. void hnae_put_handle(struct hnae_handle *h)
  299. {
  300. struct hnae_ae_dev *dev = h->dev;
  301. int i;
  302. for (i = 0; i < h->q_num; i++)
  303. hnae_fini_queue(h->qs[i]);
  304. if (h->dev->ops->reset)
  305. h->dev->ops->reset(h);
  306. hnae_list_del(&dev->lock, &h->node);
  307. if (dev->ops->put_handle)
  308. dev->ops->put_handle(h);
  309. module_put(dev->owner);
  310. put_device(&dev->cls_dev);
  311. }
  312. EXPORT_SYMBOL(hnae_put_handle);
  313. static void hnae_release(struct device *dev)
  314. {
  315. }
  316. /**
  317. * hnae_ae_register - register a AE engine to hnae framework
  318. * @hdev: the hnae ae engine device
  319. * @owner: the module who provides this dev
  320. * NOTE: the duplicated name will not be checked
  321. */
  322. int hnae_ae_register(struct hnae_ae_dev *hdev, struct module *owner)
  323. {
  324. static atomic_t id = ATOMIC_INIT(-1);
  325. int ret;
  326. if (!hdev->dev)
  327. return -ENODEV;
  328. if (!hdev->ops || !hdev->ops->get_handle ||
  329. !hdev->ops->toggle_ring_irq ||
  330. !hdev->ops->get_status || !hdev->ops->adjust_link)
  331. return -EINVAL;
  332. hdev->owner = owner;
  333. hdev->id = (int)atomic_inc_return(&id);
  334. hdev->cls_dev.parent = hdev->dev;
  335. hdev->cls_dev.class = hnae_class;
  336. hdev->cls_dev.release = hnae_release;
  337. (void)dev_set_name(&hdev->cls_dev, "hnae%d", hdev->id);
  338. ret = device_register(&hdev->cls_dev);
  339. if (ret)
  340. return ret;
  341. __module_get(THIS_MODULE);
  342. INIT_LIST_HEAD(&hdev->handle_list);
  343. spin_lock_init(&hdev->lock);
  344. ret = raw_notifier_call_chain(&ae_chain, HNAE_AE_REGISTER, NULL);
  345. if (ret)
  346. dev_dbg(hdev->dev,
  347. "has not notifier for AE: %s\n", hdev->name);
  348. return 0;
  349. }
  350. EXPORT_SYMBOL(hnae_ae_register);
  351. /**
  352. * hnae_ae_unregister - unregisters a HNAE AE engine
  353. * @cdev: the device to unregister
  354. */
  355. void hnae_ae_unregister(struct hnae_ae_dev *hdev)
  356. {
  357. device_unregister(&hdev->cls_dev);
  358. module_put(THIS_MODULE);
  359. }
  360. EXPORT_SYMBOL(hnae_ae_unregister);
  361. static int __init hnae_init(void)
  362. {
  363. hnae_class = class_create(THIS_MODULE, "hnae");
  364. return PTR_ERR_OR_ZERO(hnae_class);
  365. }
  366. static void __exit hnae_exit(void)
  367. {
  368. class_destroy(hnae_class);
  369. }
  370. subsys_initcall(hnae_init);
  371. module_exit(hnae_exit);
  372. MODULE_AUTHOR("Hisilicon, Inc.");
  373. MODULE_LICENSE("GPL");
  374. MODULE_DESCRIPTION("Hisilicon Network Acceleration Engine Framework");
  375. /* vi: set tw=78 noet: */