1/*
2 * Intel MIC Platform Software Stack (MPSS)
3 *
4 * Copyright(c) 2014 Intel Corporation.
5 *
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.
9 *
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.
14 *
15 * Intel SCIF driver.
16 *
17 */
18#include <linux/idr.h>
19
20#include "scif_main.h"
21
22#define SCIF_PORT_COUNT	0x10000	/* Ports available */
23
24struct idr scif_ports;
25
26/*
27 * struct scif_port - SCIF port information
28 *
29 * @ref_cnt - Reference count since there can be multiple endpoints
30 *		created via scif_accept(..) simultaneously using a port.
31 */
32struct scif_port {
33	int ref_cnt;
34};
35
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.
40 *
41 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
42 *		On memory allocation failure, returns -ENOMEM.
43 */
44static int __scif_get_port(int start, int end)
45{
46	int id;
47	struct scif_port *port = kzalloc(sizeof(*port), GFP_ATOMIC);
48
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;
57}
58
59/**
60 * scif_rsrv_port - Reserve a specified port # for SCIF.
61 * @port : port # to be reserved.
62 *
63 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
64 *		On memory allocation failure, returns -ENOMEM.
65 */
66int scif_rsrv_port(u16 port)
67{
68	return __scif_get_port(port, port + 1);
69}
70
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.
74 *
75 * @return : Allocated SCIF port #, or -ENOSPC if no ports available.
76 *		On memory allocation failure, returns -ENOMEM.
77 */
78int scif_get_new_port(void)
79{
80	return __scif_get_port(SCIF_PORT_RSVD + 1, SCIF_PORT_COUNT);
81}
82
83/**
84 * scif_get_port - Increment the reference count for a SCIF port
85 * @id : SCIF port
86 *
87 * @return : None
88 */
89void scif_get_port(u16 id)
90{
91	struct scif_port *port;
92
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);
100}
101
102/**
103 * scif_put_port - Release a reserved SCIF port
104 * @id : SCIF port to be released.
105 *
106 * @return : None
107 */
108void scif_put_port(u16 id)
109{
110	struct scif_port *port;
111
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);
121		}
122	}
123	spin_unlock(&scif_info.port_lock);
124}
125