hnae.c 9.9 KB

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