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 * @start: lowest port # to be reserved (inclusive).
31 * @end: highest port # to be reserved (exclusive).
33 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
34 * On memory allocation failure, returns -ENOMEM.
36 static int __scif_get_port(int start, int end)
39 struct scif_port *port = kzalloc(sizeof(*port), GFP_ATOMIC);
43 spin_lock(&scif_info.port_lock);
44 id = idr_alloc(&scif_ports, port, start, end, GFP_ATOMIC);
47 spin_unlock(&scif_info.port_lock);
52 * scif_rsrv_port - Reserve a specified port # for SCIF.
53 * @port : port # to be reserved.
55 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
56 * On memory allocation failure, returns -ENOMEM.
58 int scif_rsrv_port(u16 port)
60 return __scif_get_port(port, port + 1);
64 * scif_get_new_port - Get and reserve any port # for SCIF in the range
65 * SCIF_PORT_RSVD + 1 to SCIF_PORT_COUNT - 1.
67 * @return : Allocated SCIF port #, or -ENOSPC if no ports available.
68 * On memory allocation failure, returns -ENOMEM.
70 int scif_get_new_port(void)
72 return __scif_get_port(SCIF_PORT_RSVD + 1, SCIF_PORT_COUNT);
76 * scif_get_port - Increment the reference count for a SCIF port
81 void scif_get_port(u16 id)
83 struct scif_port *port;
87 spin_lock(&scif_info.port_lock);
88 port = idr_find(&scif_ports, id);
91 spin_unlock(&scif_info.port_lock);
95 * scif_put_port - Release a reserved SCIF port
96 * @id : SCIF port to be released.
100 void scif_put_port(u16 id)
102 struct scif_port *port;
106 spin_lock(&scif_info.port_lock);
107 port = idr_find(&scif_ports, id);
110 if (!port->ref_cnt) {
111 idr_remove(&scif_ports, id);
115 spin_unlock(&scif_info.port_lock);