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