treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / mic / scif / scif_ports.c
blob547a71285069aedc1f47a2156fc18084abd9aa06
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2014 Intel Corporation.
7 * Intel SCIF driver.
8 */
9 #include <linux/idr.h>
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.
23 struct scif_port {
24 int ref_cnt;
27 /**
28 * __scif_get_port - Reserve a specified port # for SCIF and add it
29 * to the global list.
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)
37 int id;
38 struct scif_port *port = kzalloc(sizeof(*port), GFP_ATOMIC);
40 if (!port)
41 return -ENOMEM;
42 spin_lock(&scif_info.port_lock);
43 id = idr_alloc(&scif_ports, port, start, end, GFP_ATOMIC);
44 if (id >= 0)
45 port->ref_cnt++;
46 spin_unlock(&scif_info.port_lock);
47 return id;
50 /**
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);
62 /**
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);
74 /**
75 * scif_get_port - Increment the reference count for a SCIF port
76 * @id : SCIF port
78 * @return : None
80 void scif_get_port(u16 id)
82 struct scif_port *port;
84 if (!id)
85 return;
86 spin_lock(&scif_info.port_lock);
87 port = idr_find(&scif_ports, id);
88 if (port)
89 port->ref_cnt++;
90 spin_unlock(&scif_info.port_lock);
93 /**
94 * scif_put_port - Release a reserved SCIF port
95 * @id : SCIF port to be released.
97 * @return : None
99 void scif_put_port(u16 id)
101 struct scif_port *port;
103 if (!id)
104 return;
105 spin_lock(&scif_info.port_lock);
106 port = idr_find(&scif_ports, id);
107 if (port) {
108 port->ref_cnt--;
109 if (!port->ref_cnt) {
110 idr_remove(&scif_ports, id);
111 kfree(port);
114 spin_unlock(&scif_info.port_lock);