1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2014 Intel Corporation.
11 #include "scif_main.h"
13 #define SCIF_PORT_COUNT 0x10000 /* Ports available */
15 struct idr scif_ports
;
18 * struct scif_port - SCIF port information
20 * @ref_cnt - Reference count since there can be multiple endpoints
21 * created via scif_accept(..) simultaneously using a port.
28 * __scif_get_port - Reserve a specified port # for SCIF and add it
30 * @port : port # to be reserved.
32 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
33 * On memory allocation failure, returns -ENOMEM.
35 static int __scif_get_port(int start
, int end
)
38 struct scif_port
*port
= kzalloc(sizeof(*port
), GFP_ATOMIC
);
42 spin_lock(&scif_info
.port_lock
);
43 id
= idr_alloc(&scif_ports
, port
, start
, end
, GFP_ATOMIC
);
46 spin_unlock(&scif_info
.port_lock
);
51 * scif_rsrv_port - Reserve a specified port # for SCIF.
52 * @port : port # to be reserved.
54 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
55 * On memory allocation failure, returns -ENOMEM.
57 int scif_rsrv_port(u16 port
)
59 return __scif_get_port(port
, port
+ 1);
63 * scif_get_new_port - Get and reserve any port # for SCIF in the range
64 * SCIF_PORT_RSVD + 1 to SCIF_PORT_COUNT - 1.
66 * @return : Allocated SCIF port #, or -ENOSPC if no ports available.
67 * On memory allocation failure, returns -ENOMEM.
69 int scif_get_new_port(void)
71 return __scif_get_port(SCIF_PORT_RSVD
+ 1, SCIF_PORT_COUNT
);
75 * scif_get_port - Increment the reference count for a SCIF port
80 void scif_get_port(u16 id
)
82 struct scif_port
*port
;
86 spin_lock(&scif_info
.port_lock
);
87 port
= idr_find(&scif_ports
, id
);
90 spin_unlock(&scif_info
.port_lock
);
94 * scif_put_port - Release a reserved SCIF port
95 * @id : SCIF port to be released.
99 void scif_put_port(u16 id
)
101 struct scif_port
*port
;
105 spin_lock(&scif_info
.port_lock
);
106 port
= idr_find(&scif_ports
, id
);
109 if (!port
->ref_cnt
) {
110 idr_remove(&scif_ports
, id
);
114 spin_unlock(&scif_info
.port_lock
);