2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
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.
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
18 #include <linux/bitmap.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include "usnic_transport.h"
23 #include "usnic_log.h"
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
);
31 static const char *transport_to_str(enum usnic_transport_type type
)
34 case USNIC_TRANSPORT_UNKNOWN
:
36 case USNIC_TRANSPORT_ROCE_CUSTOM
:
38 case USNIC_TRANSPORT_MAX
:
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
50 u16
usnic_transport_rsrv_port(enum usnic_transport_type type
, u16 port_num
)
52 if (type
== USNIC_TRANSPORT_ROCE_CUSTOM
) {
53 spin_lock(&roce_bitmap_lock
);
55 port_num
= bitmap_find_next_zero_area(roce_bitmap
,
57 roce_next_port
/* start */,
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
);
67 bitmap_set(roce_bitmap
, port_num
, 1);
68 spin_unlock(&roce_bitmap_lock
);
70 usnic_err("Failed to allocate port - transport %s unsupported\n",
71 transport_to_str(type
));
75 usnic_dbg("Allocating port %hu for %s\n", port_num
,
76 transport_to_str(type
));
83 void usnic_transport_unrsrv_port(enum usnic_transport_type type
, u16 port_num
)
85 if (type
== USNIC_TRANSPORT_ROCE_CUSTOM
) {
86 spin_lock(&roce_bitmap_lock
);
88 usnic_err("Unreserved unvalid port num 0 for %s\n",
89 transport_to_str(type
));
93 if (!test_bit(port_num
, roce_bitmap
)) {
94 usnic_err("Unreserving invalid %hu for %s\n",
96 transport_to_str(type
));
99 bitmap_clear(roce_bitmap
, port_num
, 1);
100 usnic_dbg("Freeing port %hu for %s\n", port_num
,
101 transport_to_str(type
));
103 spin_unlock(&roce_bitmap_lock
);
105 usnic_err("Freeing invalid port %hu for %d\n", port_num
, type
);
109 int usnic_transport_init(void)
111 roce_bitmap
= kzalloc(ROCE_BITMAP_SZ
, GFP_KERNEL
);
113 usnic_err("Failed to allocate bit map");
117 /* Do not ever allocate bit 0, hence set it here */
118 bitmap_set(roce_bitmap
, 0, 1);
122 void usnic_transport_fini(void)