Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / mic / scif / scif_ports.c
blob594e18d279d8ab88e6a96ffdb7ada4787eeaeffd
1 /*
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.
15 * Intel SCIF driver.
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.
32 struct scif_port {
33 int ref_cnt;
36 /**
37 * __scif_get_port - Reserve a specified port # for SCIF and add it
38 * to the global list.
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)
46 int id;
47 struct scif_port *port = kzalloc(sizeof(*port), GFP_ATOMIC);
49 if (!port)
50 return -ENOMEM;
51 spin_lock(&scif_info.port_lock);
52 id = idr_alloc(&scif_ports, port, start, end, GFP_ATOMIC);
53 if (id >= 0)
54 port->ref_cnt++;
55 spin_unlock(&scif_info.port_lock);
56 return id;
59 /**
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);
71 /**
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);
83 /**
84 * scif_get_port - Increment the reference count for a SCIF port
85 * @id : SCIF port
87 * @return : None
89 void scif_get_port(u16 id)
91 struct scif_port *port;
93 if (!id)
94 return;
95 spin_lock(&scif_info.port_lock);
96 port = idr_find(&scif_ports, id);
97 if (port)
98 port->ref_cnt++;
99 spin_unlock(&scif_info.port_lock);
103 * scif_put_port - Release a reserved SCIF port
104 * @id : SCIF port to be released.
106 * @return : None
108 void scif_put_port(u16 id)
110 struct scif_port *port;
112 if (!id)
113 return;
114 spin_lock(&scif_info.port_lock);
115 port = idr_find(&scif_ports, id);
116 if (port) {
117 port->ref_cnt--;
118 if (!port->ref_cnt) {
119 idr_remove(&scif_ports, id);
120 kfree(port);
123 spin_unlock(&scif_info.port_lock);