summaryrefslogtreecommitdiffstats
path: root/drivers/misc/mic/scif/scif_ports.c
blob: 594e18d279d8ab88e6a96ffdb7ada4787eeaeffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Intel MIC Platform Software Stack (MPSS)
 *
 * Copyright(c) 2014 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * Intel SCIF driver.
 *
 */
#include <linux/idr.h>

#include "scif_main.h"

#define SCIF_PORT_COUNT	0x10000	/* Ports available */

struct idr scif_ports;

/*
 * struct scif_port - SCIF port information
 *
 * @ref_cnt - Reference count since there can be multiple endpoints
 *		created via scif_accept(..) simultaneously using a port.
 */
struct scif_port {
	int ref_cnt;
};

/**
 * __scif_get_port - Reserve a specified port # for SCIF and add it
 * to the global list.
 * @port : port # to be reserved.
 *
 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
 *		On memory allocation failure, returns -ENOMEM.
 */
static int __scif_get_port(int start, int end)
{
	int id;
	struct scif_port *port = kzalloc(sizeof(*port), GFP_ATOMIC);

	if (!port)
		return -ENOMEM;
	spin_lock(&scif_info.port_lock);
	id = idr_alloc(&scif_ports, port, start, end, GFP_ATOMIC);
	if (id >= 0)
		port->ref_cnt++;
	spin_unlock(&scif_info.port_lock);
	return id;
}

/**
 * scif_rsrv_port - Reserve a specified port # for SCIF.
 * @port : port # to be reserved.
 *
 * @return : Allocated SCIF port #, or -ENOSPC if port unavailable.
 *		On memory allocation failure, returns -ENOMEM.
 */
int scif_rsrv_port(u16 port)
{
	return __scif_get_port(port, port + 1);
}

/**
 * scif_get_new_port - Get and reserve any port # for SCIF in the range
 *			SCIF_PORT_RSVD + 1 to SCIF_PORT_COUNT - 1.
 *
 * @return : Allocated SCIF port #, or -ENOSPC if no ports available.
 *		On memory allocation failure, returns -ENOMEM.
 */
int scif_get_new_port(void)
{
	return __scif_get_port(SCIF_PORT_RSVD + 1, SCIF_PORT_COUNT);
}

/**
 * scif_get_port - Increment the reference count for a SCIF port
 * @id : SCIF port
 *
 * @return : None
 */
void scif_get_port(u16 id)
{
	struct scif_port *port;

	if (!id)
		return;
	spin_lock(&scif_info.port_lock);
	port = idr_find(&scif_ports, id);
	if (port)
		port->ref_cnt++;
	spin_unlock(&scif_info.port_lock);
}

/**
 * scif_put_port - Release a reserved SCIF port
 * @id : SCIF port to be released.
 *
 * @return : None
 */
void scif_put_port(u16 id)
{
	struct scif_port *port;

	if (!id)
		return;
	spin_lock(&scif_info.port_lock);
	port = idr_find(&scif_ports, id);
	if (port) {
		port->ref_cnt--;
		if (!port->ref_cnt) {
			idr_remove(&scif_ports, id);
			kfree(port);
		}
	}
	spin_unlock(&scif_info.port_lock);
}
OpenPOWER on IntegriCloud