2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2014 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
18 #include <linux/idr.h>
20 #include "scif_main.h"
22 #define SCIF_PORT_COUNT 0x10000 /* Ports available */
24 struct idr scif_ports
;
27 * struct scif_port - SCIF port information
29 * @ref_cnt - Reference count since there can be multiple endpoints
30 * created via scif_accept(..) simultaneously using a port.
37 * __scif_get_port - Reserve a specified port # for SCIF and add it
39 * @port : port # to be reserved.
41 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
42 * On memory allocation failure, returns -ENOMEM.
44 static int __scif_get_port(int start
, int end
)
47 struct scif_port
*port
= kzalloc(sizeof(*port
), GFP_ATOMIC
);
51 spin_lock(&scif_info
.port_lock
);
52 id
= idr_alloc(&scif_ports
, port
, start
, end
, GFP_ATOMIC
);
55 spin_unlock(&scif_info
.port_lock
);
60 * scif_rsrv_port - Reserve a specified port # for SCIF.
61 * @port : port # to be reserved.
63 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
64 * On memory allocation failure, returns -ENOMEM.
66 int scif_rsrv_port(u16 port
)
68 return __scif_get_port(port
, port
+ 1);
72 * scif_get_new_port - Get and reserve any port # for SCIF in the range
73 * SCIF_PORT_RSVD + 1 to SCIF_PORT_COUNT - 1.
75 * @return : Allocated SCIF port #, or -ENOSPC if no ports available.
76 * On memory allocation failure, returns -ENOMEM.
78 int scif_get_new_port(void)
80 return __scif_get_port(SCIF_PORT_RSVD
+ 1, SCIF_PORT_COUNT
);
84 * scif_get_port - Increment the reference count for a SCIF port
89 void scif_get_port(u16 id
)
91 struct scif_port
*port
;
95 spin_lock(&scif_info
.port_lock
);
96 port
= idr_find(&scif_ports
, id
);
99 spin_unlock(&scif_info
.port_lock
);
103 * scif_put_port - Release a reserved SCIF port
104 * @id : SCIF port to be released.
108 void scif_put_port(u16 id
)
110 struct scif_port
*port
;
114 spin_lock(&scif_info
.port_lock
);
115 port
= idr_find(&scif_ports
, id
);
118 if (!port
->ref_cnt
) {
119 idr_remove(&scif_ports
, id
);
123 spin_unlock(&scif_info
.port_lock
);