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