net/sfc: support concept of switch domains/ports
[dpdk.git] / drivers / net / sfc / sfc_switch.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 Xilinx, Inc.
4  * Copyright(c) 2019 Solarflare Communications Inc.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <stdbool.h>
11
12 #include <rte_common.h>
13 #include <rte_spinlock.h>
14
15 #include "efx.h"
16
17 #include "sfc.h"
18 #include "sfc_log.h"
19 #include "sfc_switch.h"
20
21 /**
22  * Switch port registry entry.
23  *
24  * Drivers aware of RTE switch domains also have to maintain RTE switch
25  * port IDs for RTE ethdev instances they operate. These IDs are supposed
26  * to stand for physical interconnect entities, in example, PCIe functions.
27  *
28  * In terms of MAE, a physical interconnect entity can be referred to using
29  * an MPORT selector, that is, a 32-bit value. RTE switch port IDs, in turn,
30  * are 16-bit values, so indirect mapping has to be maintained:
31  *
32  * +--------------------+          +---------------------------------------+
33  * | RTE switch port ID |  ------  |         MAE switch port entry         |
34  * +--------------------+          |         ---------------------         |
35  *                                 |                                       |
36  *                                 | Entity (PCIe function) MPORT selector |
37  *                                 |                   +                   |
38  *                                 |  Port type (independent/representor)  |
39  *                                 +---------------------------------------+
40  *
41  * This mapping comprises a port type to ensure that RTE switch port ID
42  * of a represented entity and that of its representor are different in
43  * the case when the entity gets plugged into DPDK and not into a guest.
44  */
45 struct sfc_mae_switch_port {
46         TAILQ_ENTRY(sfc_mae_switch_port)        switch_domain_ports;
47
48         /** Entity (PCIe function) MPORT selector */
49         efx_mport_sel_t                         entity_mport;
50         /** Port type (independent/representor) */
51         enum sfc_mae_switch_port_type           type;
52         /** RTE switch port ID */
53         uint16_t                                id;
54 };
55
56 TAILQ_HEAD(sfc_mae_switch_ports, sfc_mae_switch_port);
57
58 /**
59  * Switch domain registry entry.
60  *
61  * Even if an RTE ethdev instance gets unplugged, the corresponding
62  * entry in the switch port registry will not be removed because the
63  * entity (PCIe function) MPORT is static and cannot change. If this
64  * RTE ethdev gets plugged back, the entry will be reused, and
65  * RTE switch port ID will be the same.
66  */
67 struct sfc_mae_switch_domain {
68         TAILQ_ENTRY(sfc_mae_switch_domain)      entries;
69
70         /** HW switch ID */
71         struct sfc_hw_switch_id                 *hw_switch_id;
72         /** The number of ports in the switch port registry */
73         unsigned int                            nb_ports;
74         /** Switch port registry */
75         struct sfc_mae_switch_ports             ports;
76         /** RTE switch domain ID allocated for a group of devices */
77         uint16_t                                id;
78 };
79
80 TAILQ_HEAD(sfc_mae_switch_domains, sfc_mae_switch_domain);
81
82 /**
83  * MAE representation of RTE switch infrastructure.
84  *
85  * It is possible that an RTE flow API client tries to insert a rule
86  * referencing an RTE ethdev deployed on top of a different physical
87  * device (it may belong to the same vendor or not). This particular
88  * driver/engine cannot support this and has to turn down such rules.
89  *
90  * Technically, it's HW switch identifier which, if queried for each
91  * RTE ethdev instance, indicates relationship between the instances.
92  * In the meantime, RTE flow API clients also need to somehow figure
93  * out relationship between RTE ethdev instances in advance.
94  *
95  * The concept of RTE switch domains resolves this issue. The driver
96  * maintains a static list of switch domains which is easy to browse,
97  * and each RTE ethdev fills RTE switch parameters in device
98  * information structure which is made available to clients.
99  *
100  * Even if all RTE ethdev instances belonging to a switch domain get
101  * unplugged, the corresponding entry in the switch domain registry
102  * will not be removed because the corresponding HW switch exists
103  * regardless of its ports being plugged to DPDK or kept aside.
104  * If a port gets plugged back to DPDK, the corresponding
105  * RTE ethdev will indicate the same RTE switch domain ID.
106  */
107 struct sfc_mae_switch {
108         /** A lock to protect the whole structure */
109         rte_spinlock_t                  lock;
110         /** Switch domain registry */
111         struct sfc_mae_switch_domains   domains;
112 };
113
114 static struct sfc_mae_switch sfc_mae_switch = {
115         .lock = RTE_SPINLOCK_INITIALIZER,
116         .domains = TAILQ_HEAD_INITIALIZER(sfc_mae_switch.domains),
117 };
118
119
120 /* This function expects to be called only when the lock is held */
121 static struct sfc_mae_switch_domain *
122 sfc_mae_find_switch_domain_by_id(uint16_t switch_domain_id)
123 {
124         struct sfc_mae_switch_domain *domain;
125
126         SFC_ASSERT(rte_spinlock_is_locked(&sfc_mae_switch.lock));
127
128         TAILQ_FOREACH(domain, &sfc_mae_switch.domains, entries) {
129                 if (domain->id == switch_domain_id)
130                         return domain;
131         }
132
133         return NULL;
134 }
135
136 /* This function expects to be called only when the lock is held */
137 static struct sfc_mae_switch_domain *
138 sfc_mae_find_switch_domain_by_hw_switch_id(const struct sfc_hw_switch_id *id)
139 {
140         struct sfc_mae_switch_domain *domain;
141
142         SFC_ASSERT(rte_spinlock_is_locked(&sfc_mae_switch.lock));
143
144         TAILQ_FOREACH(domain, &sfc_mae_switch.domains, entries) {
145                 if (sfc_hw_switch_ids_equal(domain->hw_switch_id, id))
146                         return domain;
147         }
148
149         return NULL;
150 }
151
152 int
153 sfc_mae_assign_switch_domain(struct sfc_adapter *sa,
154                              uint16_t *switch_domain_id)
155 {
156         struct sfc_hw_switch_id *hw_switch_id;
157         struct sfc_mae_switch_domain *domain;
158         int rc;
159
160         rte_spinlock_lock(&sfc_mae_switch.lock);
161
162         rc = sfc_hw_switch_id_init(sa, &hw_switch_id);
163         if (rc != 0)
164                 goto fail_hw_switch_id_init;
165
166         domain = sfc_mae_find_switch_domain_by_hw_switch_id(hw_switch_id);
167         if (domain != NULL) {
168                 sfc_hw_switch_id_fini(sa, hw_switch_id);
169                 goto done;
170         }
171
172         domain = rte_zmalloc("sfc_mae_switch_domain", sizeof(*domain), 0);
173         if (domain == NULL) {
174                 rc = ENOMEM;
175                 goto fail_mem_alloc;
176         }
177
178         /*
179          * This code belongs to driver init path, that is, negation is
180          * done at the end of the path by sfc_eth_dev_init(). RTE APIs
181          * negate error codes, so drop negation here.
182          */
183         rc = -rte_eth_switch_domain_alloc(&domain->id);
184         if (rc != 0)
185                 goto fail_domain_alloc;
186
187         domain->hw_switch_id = hw_switch_id;
188
189         TAILQ_INIT(&domain->ports);
190
191         TAILQ_INSERT_TAIL(&sfc_mae_switch.domains, domain, entries);
192
193 done:
194         *switch_domain_id = domain->id;
195
196         rte_spinlock_unlock(&sfc_mae_switch.lock);
197
198         return 0;
199
200 fail_domain_alloc:
201         rte_free(domain);
202
203 fail_mem_alloc:
204         sfc_hw_switch_id_fini(sa, hw_switch_id);
205         rte_spinlock_unlock(&sfc_mae_switch.lock);
206
207 fail_hw_switch_id_init:
208         return rc;
209 }
210
211 /* This function expects to be called only when the lock is held */
212 static struct sfc_mae_switch_port *
213 sfc_mae_find_switch_port_by_entity(const struct sfc_mae_switch_domain *domain,
214                                    const efx_mport_sel_t *entity_mportp,
215                                    enum sfc_mae_switch_port_type type)
216 {
217         struct sfc_mae_switch_port *port;
218
219         SFC_ASSERT(rte_spinlock_is_locked(&sfc_mae_switch.lock));
220
221         TAILQ_FOREACH(port, &domain->ports, switch_domain_ports) {
222                 if (port->entity_mport.sel == entity_mportp->sel &&
223                     port->type == type)
224                         return port;
225         }
226
227         return NULL;
228 }
229
230 int
231 sfc_mae_assign_switch_port(uint16_t switch_domain_id,
232                            const struct sfc_mae_switch_port_request *req,
233                            uint16_t *switch_port_id)
234 {
235         struct sfc_mae_switch_domain *domain;
236         struct sfc_mae_switch_port *port;
237         int rc;
238
239         rte_spinlock_lock(&sfc_mae_switch.lock);
240
241         domain = sfc_mae_find_switch_domain_by_id(switch_domain_id);
242         if (domain == NULL) {
243                 rc = EINVAL;
244                 goto fail_find_switch_domain_by_id;
245         }
246
247         port = sfc_mae_find_switch_port_by_entity(domain, req->entity_mportp,
248                                                   req->type);
249         if (port != NULL)
250                 goto done;
251
252         port = rte_zmalloc("sfc_mae_switch_port", sizeof(*port), 0);
253         if (port == NULL) {
254                 rc = ENOMEM;
255                 goto fail_mem_alloc;
256         }
257
258         port->entity_mport.sel = req->entity_mportp->sel;
259         port->type = req->type;
260
261         port->id = (domain->nb_ports++);
262
263         TAILQ_INSERT_TAIL(&domain->ports, port, switch_domain_ports);
264
265 done:
266         *switch_port_id = port->id;
267
268         rte_spinlock_unlock(&sfc_mae_switch.lock);
269
270         return 0;
271
272 fail_mem_alloc:
273 fail_find_switch_domain_by_id:
274         rte_spinlock_unlock(&sfc_mae_switch.lock);
275         return rc;
276 }