scsi: core: reset host byte in DID_NEXUS_FAILURE case
[linux-stable.git] / net / core / hwbm.c
blob2cab489ae62e657c2ec99cca71b558d9a3b3cf7a
1 /* Support for hardware buffer manager.
3 * Copyright (C) 2016 Marvell
5 * Gregory CLEMENT <gregory.clement@free-electrons.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/printk.h>
14 #include <linux/skbuff.h>
15 #include <net/hwbm.h>
17 void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf)
19 if (likely(bm_pool->frag_size <= PAGE_SIZE))
20 skb_free_frag(buf);
21 else
22 kfree(buf);
24 EXPORT_SYMBOL_GPL(hwbm_buf_free);
26 /* Refill processing for HW buffer management */
27 int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp)
29 int frag_size = bm_pool->frag_size;
30 void *buf;
32 if (likely(frag_size <= PAGE_SIZE))
33 buf = netdev_alloc_frag(frag_size);
34 else
35 buf = kmalloc(frag_size, gfp);
37 if (!buf)
38 return -ENOMEM;
40 if (bm_pool->construct)
41 if (bm_pool->construct(bm_pool, buf)) {
42 hwbm_buf_free(bm_pool, buf);
43 return -ENOMEM;
46 return 0;
48 EXPORT_SYMBOL_GPL(hwbm_pool_refill);
50 int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp)
52 int err, i;
53 unsigned long flags;
55 spin_lock_irqsave(&bm_pool->lock, flags);
56 if (bm_pool->buf_num == bm_pool->size) {
57 pr_warn("pool already filled\n");
58 spin_unlock_irqrestore(&bm_pool->lock, flags);
59 return bm_pool->buf_num;
62 if (buf_num + bm_pool->buf_num > bm_pool->size) {
63 pr_warn("cannot allocate %d buffers for pool\n",
64 buf_num);
65 spin_unlock_irqrestore(&bm_pool->lock, flags);
66 return 0;
69 if ((buf_num + bm_pool->buf_num) < bm_pool->buf_num) {
70 pr_warn("Adding %d buffers to the %d current buffers will overflow\n",
71 buf_num, bm_pool->buf_num);
72 spin_unlock_irqrestore(&bm_pool->lock, flags);
73 return 0;
76 for (i = 0; i < buf_num; i++) {
77 err = hwbm_pool_refill(bm_pool, gfp);
78 if (err < 0)
79 break;
82 /* Update BM driver with number of buffers added to pool */
83 bm_pool->buf_num += i;
85 pr_debug("hwpm pool: %d of %d buffers added\n", i, buf_num);
86 spin_unlock_irqrestore(&bm_pool->lock, flags);
88 return i;
90 EXPORT_SYMBOL_GPL(hwbm_pool_add);