treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / infiniband / hw / vmw_pvrdma / pvrdma_doorbell.c
blobbf51357ea3aafac61e140877082843c4ee0bc531
1 /*
2 * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of EITHER the GNU General Public License
6 * version 2 as published by the Free Software Foundation or the BSD
7 * 2-Clause License. This program is distributed in the hope that it
8 * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
9 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10 * See the GNU General Public License version 2 for more details at
11 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
13 * You should have received a copy of the GNU General Public License
14 * along with this program available in the file COPYING in the main
15 * directory of this source tree.
17 * The BSD 2-Clause License
19 * Redistribution and use in source and binary forms, with or
20 * without modification, are permitted provided that the following
21 * conditions are met:
23 * - Redistributions of source code must retain the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer.
27 * - Redistributions in binary form must reproduce the above
28 * copyright notice, this list of conditions and the following
29 * disclaimer in the documentation and/or other materials
30 * provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43 * OF THE POSSIBILITY OF SUCH DAMAGE.
46 #include <linux/bitmap.h>
47 #include <linux/errno.h>
48 #include <linux/slab.h>
50 #include "pvrdma.h"
52 int pvrdma_uar_table_init(struct pvrdma_dev *dev)
54 u32 num = dev->dsr->caps.max_uar;
55 u32 mask = num - 1;
56 struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
58 if (!is_power_of_2(num))
59 return -EINVAL;
61 tbl->last = 0;
62 tbl->top = 0;
63 tbl->max = num;
64 tbl->mask = mask;
65 spin_lock_init(&tbl->lock);
66 tbl->table = kcalloc(BITS_TO_LONGS(num), sizeof(long), GFP_KERNEL);
67 if (!tbl->table)
68 return -ENOMEM;
70 /* 0th UAR is taken by the device. */
71 set_bit(0, tbl->table);
73 return 0;
76 void pvrdma_uar_table_cleanup(struct pvrdma_dev *dev)
78 struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
80 kfree(tbl->table);
83 int pvrdma_uar_alloc(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)
85 struct pvrdma_id_table *tbl;
86 unsigned long flags;
87 u32 obj;
89 tbl = &dev->uar_table.tbl;
91 spin_lock_irqsave(&tbl->lock, flags);
92 obj = find_next_zero_bit(tbl->table, tbl->max, tbl->last);
93 if (obj >= tbl->max) {
94 tbl->top = (tbl->top + tbl->max) & tbl->mask;
95 obj = find_first_zero_bit(tbl->table, tbl->max);
98 if (obj >= tbl->max) {
99 spin_unlock_irqrestore(&tbl->lock, flags);
100 return -ENOMEM;
103 set_bit(obj, tbl->table);
104 obj |= tbl->top;
106 spin_unlock_irqrestore(&tbl->lock, flags);
108 uar->index = obj;
109 uar->pfn = (pci_resource_start(dev->pdev, PVRDMA_PCI_RESOURCE_UAR) >>
110 PAGE_SHIFT) + uar->index;
112 return 0;
115 void pvrdma_uar_free(struct pvrdma_dev *dev, struct pvrdma_uar_map *uar)
117 struct pvrdma_id_table *tbl = &dev->uar_table.tbl;
118 unsigned long flags;
119 u32 obj;
121 obj = uar->index & (tbl->max - 1);
122 spin_lock_irqsave(&tbl->lock, flags);
123 clear_bit(obj, tbl->table);
124 tbl->last = min(tbl->last, obj);
125 tbl->top = (tbl->top + tbl->max) & tbl->mask;
126 spin_unlock_irqrestore(&tbl->lock, flags);