1 // SPDX-License-Identifier: GPL-2.0
3 #include <net/macsec.h>
6 static inline u64 sci_to_cpu(sci_t sci)
8 return be64_to_cpu((__force __be64)sci);
11 static int nsim_macsec_find_secy(struct netdevsim *ns, sci_t sci)
15 for (i = 0; i < NSIM_MACSEC_MAX_SECY_COUNT; i++) {
16 if (ns->macsec.nsim_secy[i].sci == sci)
23 static int nsim_macsec_find_rxsc(struct nsim_secy *ns_secy, sci_t sci)
27 for (i = 0; i < NSIM_MACSEC_MAX_RXSC_COUNT; i++) {
28 if (ns_secy->nsim_rxsc[i].sci == sci)
35 static int nsim_macsec_add_secy(struct macsec_context *ctx)
37 struct netdevsim *ns = netdev_priv(ctx->netdev);
40 if (ns->macsec.nsim_secy_count == NSIM_MACSEC_MAX_SECY_COUNT)
43 for (idx = 0; idx < NSIM_MACSEC_MAX_SECY_COUNT; idx++) {
44 if (!ns->macsec.nsim_secy[idx].used)
48 if (idx == NSIM_MACSEC_MAX_SECY_COUNT) {
49 netdev_err(ctx->netdev, "%s: nsim_secy_count not full but all SecYs used\n",
54 netdev_dbg(ctx->netdev, "%s: adding new secy with sci %08llx at index %d\n",
55 __func__, sci_to_cpu(ctx->secy->sci), idx);
56 ns->macsec.nsim_secy[idx].used = true;
57 ns->macsec.nsim_secy[idx].nsim_rxsc_count = 0;
58 ns->macsec.nsim_secy[idx].sci = ctx->secy->sci;
59 ns->macsec.nsim_secy_count++;
64 static int nsim_macsec_upd_secy(struct macsec_context *ctx)
66 struct netdevsim *ns = netdev_priv(ctx->netdev);
69 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
71 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
72 __func__, sci_to_cpu(ctx->secy->sci));
76 netdev_dbg(ctx->netdev, "%s: updating secy with sci %08llx at index %d\n",
77 __func__, sci_to_cpu(ctx->secy->sci), idx);
82 static int nsim_macsec_del_secy(struct macsec_context *ctx)
84 struct netdevsim *ns = netdev_priv(ctx->netdev);
87 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
89 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
90 __func__, sci_to_cpu(ctx->secy->sci));
94 netdev_dbg(ctx->netdev, "%s: removing SecY with SCI %08llx at index %d\n",
95 __func__, sci_to_cpu(ctx->secy->sci), idx);
97 ns->macsec.nsim_secy[idx].used = false;
98 memset(&ns->macsec.nsim_secy[idx], 0, sizeof(ns->macsec.nsim_secy[idx]));
99 ns->macsec.nsim_secy_count--;
104 static int nsim_macsec_add_rxsc(struct macsec_context *ctx)
106 struct netdevsim *ns = netdev_priv(ctx->netdev);
107 struct nsim_secy *secy;
110 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
112 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
113 __func__, sci_to_cpu(ctx->secy->sci));
116 secy = &ns->macsec.nsim_secy[idx];
118 if (secy->nsim_rxsc_count == NSIM_MACSEC_MAX_RXSC_COUNT)
121 for (idx = 0; idx < NSIM_MACSEC_MAX_RXSC_COUNT; idx++) {
122 if (!secy->nsim_rxsc[idx].used)
126 if (idx == NSIM_MACSEC_MAX_RXSC_COUNT)
127 netdev_err(ctx->netdev, "%s: nsim_rxsc_count not full but all RXSCs used\n",
130 netdev_dbg(ctx->netdev, "%s: adding new rxsc with sci %08llx at index %d\n",
131 __func__, sci_to_cpu(ctx->rx_sc->sci), idx);
132 secy->nsim_rxsc[idx].used = true;
133 secy->nsim_rxsc[idx].sci = ctx->rx_sc->sci;
134 secy->nsim_rxsc_count++;
139 static int nsim_macsec_upd_rxsc(struct macsec_context *ctx)
141 struct netdevsim *ns = netdev_priv(ctx->netdev);
142 struct nsim_secy *secy;
145 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
147 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
148 __func__, sci_to_cpu(ctx->secy->sci));
151 secy = &ns->macsec.nsim_secy[idx];
153 idx = nsim_macsec_find_rxsc(secy, ctx->rx_sc->sci);
155 netdev_err(ctx->netdev, "%s: sci %08llx not found in RXSC table\n",
156 __func__, sci_to_cpu(ctx->rx_sc->sci));
160 netdev_dbg(ctx->netdev, "%s: updating RXSC with sci %08llx at index %d\n",
161 __func__, sci_to_cpu(ctx->rx_sc->sci), idx);
166 static int nsim_macsec_del_rxsc(struct macsec_context *ctx)
168 struct netdevsim *ns = netdev_priv(ctx->netdev);
169 struct nsim_secy *secy;
172 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
174 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
175 __func__, sci_to_cpu(ctx->secy->sci));
178 secy = &ns->macsec.nsim_secy[idx];
180 idx = nsim_macsec_find_rxsc(secy, ctx->rx_sc->sci);
182 netdev_err(ctx->netdev, "%s: sci %08llx not found in RXSC table\n",
183 __func__, sci_to_cpu(ctx->rx_sc->sci));
187 netdev_dbg(ctx->netdev, "%s: removing RXSC with sci %08llx at index %d\n",
188 __func__, sci_to_cpu(ctx->rx_sc->sci), idx);
190 secy->nsim_rxsc[idx].used = false;
191 memset(&secy->nsim_rxsc[idx], 0, sizeof(secy->nsim_rxsc[idx]));
192 secy->nsim_rxsc_count--;
197 static int nsim_macsec_add_rxsa(struct macsec_context *ctx)
199 struct netdevsim *ns = netdev_priv(ctx->netdev);
200 struct nsim_secy *secy;
203 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
205 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
206 __func__, sci_to_cpu(ctx->secy->sci));
209 secy = &ns->macsec.nsim_secy[idx];
211 idx = nsim_macsec_find_rxsc(secy, ctx->sa.rx_sa->sc->sci);
213 netdev_err(ctx->netdev, "%s: sci %08llx not found in RXSC table\n",
214 __func__, sci_to_cpu(ctx->sa.rx_sa->sc->sci));
218 netdev_dbg(ctx->netdev, "%s: RXSC with sci %08llx, AN %u\n",
219 __func__, sci_to_cpu(ctx->sa.rx_sa->sc->sci), ctx->sa.assoc_num);
224 static int nsim_macsec_upd_rxsa(struct macsec_context *ctx)
226 struct netdevsim *ns = netdev_priv(ctx->netdev);
227 struct nsim_secy *secy;
230 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
232 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
233 __func__, sci_to_cpu(ctx->secy->sci));
236 secy = &ns->macsec.nsim_secy[idx];
238 idx = nsim_macsec_find_rxsc(secy, ctx->sa.rx_sa->sc->sci);
240 netdev_err(ctx->netdev, "%s: sci %08llx not found in RXSC table\n",
241 __func__, sci_to_cpu(ctx->sa.rx_sa->sc->sci));
245 netdev_dbg(ctx->netdev, "%s: RXSC with sci %08llx, AN %u\n",
246 __func__, sci_to_cpu(ctx->sa.rx_sa->sc->sci), ctx->sa.assoc_num);
251 static int nsim_macsec_del_rxsa(struct macsec_context *ctx)
253 struct netdevsim *ns = netdev_priv(ctx->netdev);
254 struct nsim_secy *secy;
257 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
259 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
260 __func__, sci_to_cpu(ctx->secy->sci));
263 secy = &ns->macsec.nsim_secy[idx];
265 idx = nsim_macsec_find_rxsc(secy, ctx->sa.rx_sa->sc->sci);
267 netdev_err(ctx->netdev, "%s: sci %08llx not found in RXSC table\n",
268 __func__, sci_to_cpu(ctx->sa.rx_sa->sc->sci));
272 netdev_dbg(ctx->netdev, "%s: RXSC with sci %08llx, AN %u\n",
273 __func__, sci_to_cpu(ctx->sa.rx_sa->sc->sci), ctx->sa.assoc_num);
278 static int nsim_macsec_add_txsa(struct macsec_context *ctx)
280 struct netdevsim *ns = netdev_priv(ctx->netdev);
283 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
285 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
286 __func__, sci_to_cpu(ctx->secy->sci));
290 netdev_dbg(ctx->netdev, "%s: SECY with sci %08llx, AN %u\n",
291 __func__, sci_to_cpu(ctx->secy->sci), ctx->sa.assoc_num);
296 static int nsim_macsec_upd_txsa(struct macsec_context *ctx)
298 struct netdevsim *ns = netdev_priv(ctx->netdev);
301 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
303 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
304 __func__, sci_to_cpu(ctx->secy->sci));
308 netdev_dbg(ctx->netdev, "%s: SECY with sci %08llx, AN %u\n",
309 __func__, sci_to_cpu(ctx->secy->sci), ctx->sa.assoc_num);
314 static int nsim_macsec_del_txsa(struct macsec_context *ctx)
316 struct netdevsim *ns = netdev_priv(ctx->netdev);
319 idx = nsim_macsec_find_secy(ns, ctx->secy->sci);
321 netdev_err(ctx->netdev, "%s: sci %08llx not found in secy table\n",
322 __func__, sci_to_cpu(ctx->secy->sci));
326 netdev_dbg(ctx->netdev, "%s: SECY with sci %08llx, AN %u\n",
327 __func__, sci_to_cpu(ctx->secy->sci), ctx->sa.assoc_num);
332 static const struct macsec_ops nsim_macsec_ops = {
333 .mdo_add_secy = nsim_macsec_add_secy,
334 .mdo_upd_secy = nsim_macsec_upd_secy,
335 .mdo_del_secy = nsim_macsec_del_secy,
336 .mdo_add_rxsc = nsim_macsec_add_rxsc,
337 .mdo_upd_rxsc = nsim_macsec_upd_rxsc,
338 .mdo_del_rxsc = nsim_macsec_del_rxsc,
339 .mdo_add_rxsa = nsim_macsec_add_rxsa,
340 .mdo_upd_rxsa = nsim_macsec_upd_rxsa,
341 .mdo_del_rxsa = nsim_macsec_del_rxsa,
342 .mdo_add_txsa = nsim_macsec_add_txsa,
343 .mdo_upd_txsa = nsim_macsec_upd_txsa,
344 .mdo_del_txsa = nsim_macsec_del_txsa,
347 void nsim_macsec_init(struct netdevsim *ns)
349 ns->netdev->macsec_ops = &nsim_macsec_ops;
350 ns->netdev->features |= NETIF_F_HW_MACSEC;
351 memset(&ns->macsec, 0, sizeof(ns->macsec));
354 void nsim_macsec_teardown(struct netdevsim *ns)