IB/usnic: Add Cisco VIC low-level hardware driver
[deliverable/linux.git] / drivers / infiniband / hw / usnic / usnic_transport.c
1 /*
2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3 *
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15 * SOFTWARE.
16 *
17 */
18 #include <linux/bitmap.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21
22 #include "usnic_transport.h"
23 #include "usnic_log.h"
24
25 /* ROCE */
26 static unsigned long *roce_bitmap;
27 static u16 roce_next_port = 1;
28 #define ROCE_BITMAP_SZ ((1 << (8 /*CHAR_BIT*/ * sizeof(u16)))/8 /*CHAR BIT*/)
29 static DEFINE_SPINLOCK(roce_bitmap_lock);
30
31 static const char *transport_to_str(enum usnic_transport_type type)
32 {
33 switch (type) {
34 case USNIC_TRANSPORT_UNKNOWN:
35 return "Unknown";
36 case USNIC_TRANSPORT_ROCE_CUSTOM:
37 return "roce custom";
38 case USNIC_TRANSPORT_MAX:
39 return "Max?";
40 default:
41 return "Not known";
42 }
43 }
44
45 /*
46 * reserve a port number. if "0" specified, we will try to pick one
47 * starting at roce_next_port. roce_next_port will take on the values
48 * 1..4096
49 */
50 u16 usnic_transport_rsrv_port(enum usnic_transport_type type, u16 port_num)
51 {
52 if (type == USNIC_TRANSPORT_ROCE_CUSTOM) {
53 spin_lock(&roce_bitmap_lock);
54 if (!port_num) {
55 port_num = bitmap_find_next_zero_area(roce_bitmap,
56 ROCE_BITMAP_SZ,
57 roce_next_port /* start */,
58 1 /* nr */,
59 0 /* align */);
60 roce_next_port = (port_num & 4095) + 1;
61 } else if (test_bit(port_num, roce_bitmap)) {
62 usnic_err("Failed to allocate port for %s\n",
63 transport_to_str(type));
64 spin_unlock(&roce_bitmap_lock);
65 goto out_fail;
66 }
67 bitmap_set(roce_bitmap, port_num, 1);
68 spin_unlock(&roce_bitmap_lock);
69 } else {
70 usnic_err("Failed to allocate port - transport %s unsupported\n",
71 transport_to_str(type));
72 goto out_fail;
73 }
74
75 usnic_dbg("Allocating port %hu for %s\n", port_num,
76 transport_to_str(type));
77 return port_num;
78
79 out_fail:
80 return 0;
81 }
82
83 void usnic_transport_unrsrv_port(enum usnic_transport_type type, u16 port_num)
84 {
85 if (type == USNIC_TRANSPORT_ROCE_CUSTOM) {
86 spin_lock(&roce_bitmap_lock);
87 if (!port_num) {
88 usnic_err("Unreserved unvalid port num 0 for %s\n",
89 transport_to_str(type));
90 goto out_roce_custom;
91 }
92
93 if (!test_bit(port_num, roce_bitmap)) {
94 usnic_err("Unreserving invalid %hu for %s\n",
95 port_num,
96 transport_to_str(type));
97 goto out_roce_custom;
98 }
99 bitmap_clear(roce_bitmap, port_num, 1);
100 usnic_dbg("Freeing port %hu for %s\n", port_num,
101 transport_to_str(type));
102 out_roce_custom:
103 spin_unlock(&roce_bitmap_lock);
104 } else {
105 usnic_err("Freeing invalid port %hu for %d\n", port_num, type);
106 }
107 }
108
109 int usnic_transport_init(void)
110 {
111 roce_bitmap = kzalloc(ROCE_BITMAP_SZ, GFP_KERNEL);
112 if (!roce_bitmap) {
113 usnic_err("Failed to allocate bit map");
114 return -ENOMEM;
115 }
116
117 /* Do not ever allocate bit 0, hence set it here */
118 bitmap_set(roce_bitmap, 0, 1);
119 return 0;
120 }
121
122 void usnic_transport_fini(void)
123 {
124 kfree(roce_bitmap);
125 }
This page took 0.08205 seconds and 5 git commands to generate.