1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
4 #include <linux/mlx5/device.h>
5 #include <linux/mlx5/mlx5_ifc.h>
6 #include <linux/xarray.h>
7 #include <linux/if_vlan.h>
11 #include "lib/crypto.h"
12 #include "en_accel/macsec.h"
14 #define MLX5_MACSEC_EPN_SCOPE_MID 0x80000000L
15 #define MLX5E_MACSEC_ASO_CTX_SZ MLX5_ST_SZ_BYTES(macsec_aso)
17 enum mlx5_macsec_aso_event_arm {
18 MLX5E_ASO_EPN_ARM = BIT(0),
22 MLX5_MACSEC_ASO_REMOVE_FLOW_PKT_CNT_OFFSET,
25 struct mlx5e_macsec_handle {
26 struct mlx5e_macsec *macsec;
35 struct mlx5e_macsec_aso_out {
40 struct mlx5e_macsec_aso_in {
45 struct mlx5e_macsec_epn_state {
51 struct mlx5e_macsec_async_work {
52 struct mlx5e_macsec *macsec;
53 struct mlx5_core_dev *mdev;
54 struct work_struct work;
58 struct mlx5e_macsec_sa {
68 union mlx5_macsec_rule *macsec_rule;
69 struct rcu_head rcu_head;
70 struct mlx5e_macsec_epn_state epn_state;
73 struct mlx5e_macsec_rx_sc;
74 struct mlx5e_macsec_rx_sc_xarray_element {
76 struct mlx5e_macsec_rx_sc *rx_sc;
79 struct mlx5e_macsec_rx_sc {
82 struct mlx5e_macsec_sa *rx_sa[MACSEC_NUM_AN];
83 struct list_head rx_sc_list_element;
84 struct mlx5e_macsec_rx_sc_xarray_element *sc_xarray_element;
85 struct metadata_dst *md_dst;
86 struct rcu_head rcu_head;
89 struct mlx5e_macsec_umr {
90 u8 __aligned(64) ctx[MLX5_ST_SZ_BYTES(macsec_aso)];
95 struct mlx5e_macsec_aso {
97 struct mlx5_aso *maso;
98 /* Protects macsec ASO */
99 struct mutex aso_lock;
101 struct mlx5e_macsec_umr *umr;
106 struct mlx5e_macsec_device {
107 const struct net_device *netdev;
108 struct mlx5e_macsec_sa *tx_sa[MACSEC_NUM_AN];
109 struct list_head macsec_rx_sc_list_head;
110 unsigned char *dev_addr;
111 struct list_head macsec_device_list_element;
114 struct mlx5e_macsec {
115 struct list_head macsec_device_list_head;
117 struct mutex lock; /* Protects mlx5e_macsec internal contexts */
119 /* Rx fs_id -> rx_sc mapping */
120 struct xarray sc_xarray;
122 struct mlx5_core_dev *mdev;
125 struct mlx5e_macsec_aso aso;
127 struct notifier_block nb;
128 struct workqueue_struct *wq;
131 struct mlx5_macsec_obj_attrs {
137 struct mlx5e_macsec_epn_state epn_state;
144 struct mlx5_aso_ctrl_param {
146 u8 condition_0_operand;
147 u8 condition_1_operand;
148 u8 condition_0_offset;
149 u8 condition_1_offset;
151 u8 condition_operand;
152 u32 condition_0_data;
153 u32 condition_0_mask;
154 u32 condition_1_data;
155 u32 condition_1_mask;
160 static int mlx5e_macsec_aso_reg_mr(struct mlx5_core_dev *mdev, struct mlx5e_macsec_aso *aso)
162 struct mlx5e_macsec_umr *umr;
163 struct device *dma_device;
167 umr = kzalloc(sizeof(*umr), GFP_KERNEL);
173 dma_device = mlx5_core_dma_dev(mdev);
174 dma_addr = dma_map_single(dma_device, umr->ctx, sizeof(umr->ctx), DMA_BIDIRECTIONAL);
175 err = dma_mapping_error(dma_device, dma_addr);
177 mlx5_core_err(mdev, "Can't map dma device, err=%d\n", err);
181 err = mlx5e_create_mkey(mdev, aso->pdn, &umr->mkey);
183 mlx5_core_err(mdev, "Can't create mkey, err=%d\n", err);
187 umr->dma_addr = dma_addr;
194 dma_unmap_single(dma_device, dma_addr, sizeof(umr->ctx), DMA_BIDIRECTIONAL);
200 static void mlx5e_macsec_aso_dereg_mr(struct mlx5_core_dev *mdev, struct mlx5e_macsec_aso *aso)
202 struct mlx5e_macsec_umr *umr = aso->umr;
204 mlx5_core_destroy_mkey(mdev, umr->mkey);
205 dma_unmap_single(&mdev->pdev->dev, umr->dma_addr, sizeof(umr->ctx), DMA_BIDIRECTIONAL);
209 static int macsec_set_replay_protection(struct mlx5_macsec_obj_attrs *attrs, void *aso_ctx)
213 if (!attrs->replay_protect)
216 switch (attrs->replay_window) {
218 window_sz = MLX5_MACSEC_ASO_REPLAY_WIN_256BIT;
221 window_sz = MLX5_MACSEC_ASO_REPLAY_WIN_128BIT;
224 window_sz = MLX5_MACSEC_ASO_REPLAY_WIN_64BIT;
227 window_sz = MLX5_MACSEC_ASO_REPLAY_WIN_32BIT;
232 MLX5_SET(macsec_aso, aso_ctx, window_size, window_sz);
233 MLX5_SET(macsec_aso, aso_ctx, mode, MLX5_MACSEC_ASO_REPLAY_PROTECTION);
238 static int mlx5e_macsec_create_object(struct mlx5_core_dev *mdev,
239 struct mlx5_macsec_obj_attrs *attrs,
243 u32 in[MLX5_ST_SZ_DW(create_macsec_obj_in)] = {};
244 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
249 obj = MLX5_ADDR_OF(create_macsec_obj_in, in, macsec_object);
250 aso_ctx = MLX5_ADDR_OF(macsec_offload_obj, obj, macsec_aso);
252 MLX5_SET(macsec_offload_obj, obj, confidentiality_en, attrs->encrypt);
253 MLX5_SET(macsec_offload_obj, obj, dekn, attrs->enc_key_id);
254 MLX5_SET(macsec_offload_obj, obj, aso_return_reg, MLX5_MACSEC_ASO_REG_C_4_5);
255 MLX5_SET(macsec_offload_obj, obj, macsec_aso_access_pd, attrs->aso_pdn);
256 MLX5_SET(macsec_aso, aso_ctx, mode_parameter, attrs->next_pn);
259 if (attrs->epn_state.epn_enabled) {
263 MLX5_SET(macsec_aso, aso_ctx, epn_event_arm, 1);
264 MLX5_SET(macsec_offload_obj, obj, epn_en, 1);
265 MLX5_SET(macsec_offload_obj, obj, epn_msb, attrs->epn_state.epn_msb);
266 MLX5_SET(macsec_offload_obj, obj, epn_overlap, attrs->epn_state.overlap);
267 MLX5_SET64(macsec_offload_obj, obj, sci, (__force u64)attrs->ssci);
268 salt_p = MLX5_ADDR_OF(macsec_offload_obj, obj, salt);
269 for (i = 0; i < 3 ; i++)
270 memcpy((u32 *)salt_p + i, &attrs->salt.bytes[4 * (2 - i)], 4);
272 MLX5_SET64(macsec_offload_obj, obj, sci, (__force u64)(attrs->sci));
275 MLX5_SET(macsec_aso, aso_ctx, valid, 0x1);
277 MLX5_SET(macsec_aso, aso_ctx, mode, MLX5_MACSEC_ASO_INC_SN);
279 err = macsec_set_replay_protection(attrs, aso_ctx);
284 /* general object fields set */
285 MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
286 MLX5_SET(general_obj_in_cmd_hdr, in, obj_type, MLX5_GENERAL_OBJECT_TYPES_MACSEC);
288 err = mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
291 "MACsec offload: Failed to create MACsec object (err = %d)\n",
296 *macsec_obj_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
301 static void mlx5e_macsec_destroy_object(struct mlx5_core_dev *mdev, u32 macsec_obj_id)
303 u32 in[MLX5_ST_SZ_DW(general_obj_in_cmd_hdr)] = {};
304 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
306 MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
307 MLX5_SET(general_obj_in_cmd_hdr, in, obj_type, MLX5_GENERAL_OBJECT_TYPES_MACSEC);
308 MLX5_SET(general_obj_in_cmd_hdr, in, obj_id, macsec_obj_id);
310 mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
313 static void mlx5e_macsec_cleanup_sa_fs(struct mlx5e_macsec *macsec,
314 struct mlx5e_macsec_sa *sa, bool is_tx,
315 struct net_device *netdev, u32 fs_id)
317 int action = (is_tx) ? MLX5_ACCEL_MACSEC_ACTION_ENCRYPT :
318 MLX5_ACCEL_MACSEC_ACTION_DECRYPT;
320 if (!sa->macsec_rule)
323 mlx5_macsec_fs_del_rule(macsec->mdev->macsec_fs, sa->macsec_rule, action, netdev,
325 sa->macsec_rule = NULL;
328 static void mlx5e_macsec_cleanup_sa(struct mlx5e_macsec *macsec,
329 struct mlx5e_macsec_sa *sa, bool is_tx,
330 struct net_device *netdev, u32 fs_id)
332 mlx5e_macsec_cleanup_sa_fs(macsec, sa, is_tx, netdev, fs_id);
333 mlx5e_macsec_destroy_object(macsec->mdev, sa->macsec_obj_id);
336 static int mlx5e_macsec_init_sa_fs(struct macsec_context *ctx,
337 struct mlx5e_macsec_sa *sa, bool encrypt,
338 bool is_tx, u32 *fs_id)
340 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
341 struct mlx5_macsec_fs *macsec_fs = priv->mdev->macsec_fs;
342 struct mlx5_macsec_rule_attrs rule_attrs;
343 union mlx5_macsec_rule *macsec_rule;
345 rule_attrs.macsec_obj_id = sa->macsec_obj_id;
346 rule_attrs.sci = sa->sci;
347 rule_attrs.assoc_num = sa->assoc_num;
348 rule_attrs.action = (is_tx) ? MLX5_ACCEL_MACSEC_ACTION_ENCRYPT :
349 MLX5_ACCEL_MACSEC_ACTION_DECRYPT;
351 macsec_rule = mlx5_macsec_fs_add_rule(macsec_fs, ctx, &rule_attrs, fs_id);
355 sa->macsec_rule = macsec_rule;
360 static int mlx5e_macsec_init_sa(struct macsec_context *ctx,
361 struct mlx5e_macsec_sa *sa,
362 bool encrypt, bool is_tx, u32 *fs_id)
364 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
365 struct mlx5e_macsec *macsec = priv->macsec;
366 struct mlx5_core_dev *mdev = priv->mdev;
367 struct mlx5_macsec_obj_attrs obj_attrs;
370 obj_attrs.next_pn = sa->next_pn;
371 obj_attrs.sci = cpu_to_be64((__force u64)sa->sci);
372 obj_attrs.enc_key_id = sa->enc_key_id;
373 obj_attrs.encrypt = encrypt;
374 obj_attrs.aso_pdn = macsec->aso.pdn;
375 obj_attrs.epn_state = sa->epn_state;
377 if (sa->epn_state.epn_enabled) {
378 obj_attrs.ssci = cpu_to_be32((__force u32)sa->ssci);
379 memcpy(&obj_attrs.salt, &sa->salt, sizeof(sa->salt));
382 obj_attrs.replay_window = ctx->secy->replay_window;
383 obj_attrs.replay_protect = ctx->secy->replay_protect;
385 err = mlx5e_macsec_create_object(mdev, &obj_attrs, is_tx, &sa->macsec_obj_id);
390 err = mlx5e_macsec_init_sa_fs(ctx, sa, encrypt, is_tx, fs_id);
392 goto destroy_macsec_object;
397 destroy_macsec_object:
398 mlx5e_macsec_destroy_object(mdev, sa->macsec_obj_id);
403 static struct mlx5e_macsec_rx_sc *
404 mlx5e_macsec_get_rx_sc_from_sc_list(const struct list_head *list, sci_t sci)
406 struct mlx5e_macsec_rx_sc *iter;
408 list_for_each_entry_rcu(iter, list, rx_sc_list_element) {
409 if (iter->sci == sci)
416 static int macsec_rx_sa_active_update(struct macsec_context *ctx,
417 struct mlx5e_macsec_sa *rx_sa,
418 bool active, u32 *fs_id)
420 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
421 struct mlx5e_macsec *macsec = priv->macsec;
424 if (rx_sa->active == active)
427 rx_sa->active = active;
429 mlx5e_macsec_cleanup_sa(macsec, rx_sa, false, ctx->secy->netdev, *fs_id);
433 err = mlx5e_macsec_init_sa(ctx, rx_sa, true, false, fs_id);
435 rx_sa->active = false;
440 static bool mlx5e_macsec_secy_features_validate(struct macsec_context *ctx)
442 const struct net_device *netdev = ctx->netdev;
443 const struct macsec_secy *secy = ctx->secy;
445 if (secy->validate_frames != MACSEC_VALIDATE_STRICT) {
447 "MACsec offload is supported only when validate_frame is in strict mode\n");
451 if (secy->icv_len != MACSEC_DEFAULT_ICV_LEN) {
452 netdev_err(netdev, "MACsec offload is supported only when icv_len is %d\n",
453 MACSEC_DEFAULT_ICV_LEN);
457 if (!secy->protect_frames) {
459 "MACsec offload is supported only when protect_frames is set\n");
463 if (!ctx->secy->tx_sc.encrypt) {
464 netdev_err(netdev, "MACsec offload: encrypt off isn't supported\n");
471 static struct mlx5e_macsec_device *
472 mlx5e_macsec_get_macsec_device_context(const struct mlx5e_macsec *macsec,
473 const struct macsec_context *ctx)
475 struct mlx5e_macsec_device *iter;
476 const struct list_head *list;
478 list = &macsec->macsec_device_list_head;
479 list_for_each_entry_rcu(iter, list, macsec_device_list_element) {
480 if (iter->netdev == ctx->secy->netdev)
487 static void update_macsec_epn(struct mlx5e_macsec_sa *sa, const struct macsec_key *key,
488 const pn_t *next_pn_halves, ssci_t ssci)
490 struct mlx5e_macsec_epn_state *epn_state = &sa->epn_state;
493 sa->salt = key->salt;
494 epn_state->epn_enabled = 1;
495 epn_state->epn_msb = next_pn_halves->upper;
496 epn_state->overlap = next_pn_halves->lower < MLX5_MACSEC_EPN_SCOPE_MID ? 0 : 1;
499 static int mlx5e_macsec_add_txsa(struct macsec_context *ctx)
501 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
502 const struct macsec_tx_sc *tx_sc = &ctx->secy->tx_sc;
503 const struct macsec_tx_sa *ctx_tx_sa = ctx->sa.tx_sa;
504 const struct macsec_secy *secy = ctx->secy;
505 struct mlx5e_macsec_device *macsec_device;
506 struct mlx5_core_dev *mdev = priv->mdev;
507 u8 assoc_num = ctx->sa.assoc_num;
508 struct mlx5e_macsec_sa *tx_sa;
509 struct mlx5e_macsec *macsec;
512 mutex_lock(&priv->macsec->lock);
514 macsec = priv->macsec;
515 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
516 if (!macsec_device) {
517 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
522 if (macsec_device->tx_sa[assoc_num]) {
523 netdev_err(ctx->netdev, "MACsec offload tx_sa: %d already exist\n", assoc_num);
528 tx_sa = kzalloc(sizeof(*tx_sa), GFP_KERNEL);
534 tx_sa->active = ctx_tx_sa->active;
535 tx_sa->next_pn = ctx_tx_sa->next_pn_halves.lower;
536 tx_sa->sci = secy->sci;
537 tx_sa->assoc_num = assoc_num;
540 update_macsec_epn(tx_sa, &ctx_tx_sa->key, &ctx_tx_sa->next_pn_halves,
543 err = mlx5_create_encryption_key(mdev, ctx->sa.key, secy->key_len,
544 MLX5_ACCEL_OBJ_MACSEC_KEY,
549 macsec_device->tx_sa[assoc_num] = tx_sa;
550 if (!secy->operational)
553 err = mlx5e_macsec_init_sa(ctx, tx_sa, tx_sc->encrypt, true, NULL);
555 goto destroy_encryption_key;
557 mutex_unlock(&macsec->lock);
561 destroy_encryption_key:
562 macsec_device->tx_sa[assoc_num] = NULL;
563 mlx5_destroy_encryption_key(mdev, tx_sa->enc_key_id);
567 mutex_unlock(&macsec->lock);
572 static int mlx5e_macsec_upd_txsa(struct macsec_context *ctx)
574 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
575 const struct macsec_tx_sc *tx_sc = &ctx->secy->tx_sc;
576 const struct macsec_tx_sa *ctx_tx_sa = ctx->sa.tx_sa;
577 struct mlx5e_macsec_device *macsec_device;
578 u8 assoc_num = ctx->sa.assoc_num;
579 struct mlx5e_macsec_sa *tx_sa;
580 struct mlx5e_macsec *macsec;
581 struct net_device *netdev;
584 mutex_lock(&priv->macsec->lock);
586 macsec = priv->macsec;
587 netdev = ctx->netdev;
588 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
589 if (!macsec_device) {
590 netdev_err(netdev, "MACsec offload: Failed to find device context\n");
595 tx_sa = macsec_device->tx_sa[assoc_num];
597 netdev_err(netdev, "MACsec offload: TX sa 0x%x doesn't exist\n", assoc_num);
602 if (ctx->sa.update_pn) {
603 netdev_err(netdev, "MACsec offload: update TX sa %d PN isn't supported\n",
609 if (tx_sa->active == ctx_tx_sa->active)
612 tx_sa->active = ctx_tx_sa->active;
613 if (tx_sa->assoc_num != tx_sc->encoding_sa)
616 if (ctx_tx_sa->active) {
617 err = mlx5e_macsec_init_sa_fs(ctx, tx_sa, tx_sc->encrypt, true, NULL);
621 if (!tx_sa->macsec_rule) {
626 mlx5e_macsec_cleanup_sa_fs(macsec, tx_sa, true, ctx->secy->netdev, 0);
629 mutex_unlock(&macsec->lock);
634 static int mlx5e_macsec_del_txsa(struct macsec_context *ctx)
636 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
637 struct mlx5e_macsec_device *macsec_device;
638 u8 assoc_num = ctx->sa.assoc_num;
639 struct mlx5e_macsec_sa *tx_sa;
640 struct mlx5e_macsec *macsec;
643 mutex_lock(&priv->macsec->lock);
644 macsec = priv->macsec;
645 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
646 if (!macsec_device) {
647 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
652 tx_sa = macsec_device->tx_sa[assoc_num];
654 netdev_err(ctx->netdev, "MACsec offload: TX sa 0x%x doesn't exist\n", assoc_num);
659 mlx5e_macsec_cleanup_sa(macsec, tx_sa, true, ctx->secy->netdev, 0);
660 mlx5_destroy_encryption_key(macsec->mdev, tx_sa->enc_key_id);
661 kfree_rcu_mightsleep(tx_sa);
662 macsec_device->tx_sa[assoc_num] = NULL;
665 mutex_unlock(&macsec->lock);
670 static int mlx5e_macsec_add_rxsc(struct macsec_context *ctx)
672 struct mlx5e_macsec_rx_sc_xarray_element *sc_xarray_element;
673 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
674 const struct macsec_rx_sc *ctx_rx_sc = ctx->rx_sc;
675 struct mlx5e_macsec_device *macsec_device;
676 struct mlx5e_macsec_rx_sc *rx_sc;
677 struct list_head *rx_sc_list;
678 struct mlx5e_macsec *macsec;
681 mutex_lock(&priv->macsec->lock);
682 macsec = priv->macsec;
683 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
684 if (!macsec_device) {
685 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
690 rx_sc_list = &macsec_device->macsec_rx_sc_list_head;
691 rx_sc = mlx5e_macsec_get_rx_sc_from_sc_list(rx_sc_list, ctx_rx_sc->sci);
693 netdev_err(ctx->netdev, "MACsec offload: rx_sc (sci %lld) already exists\n",
699 rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
705 sc_xarray_element = kzalloc(sizeof(*sc_xarray_element), GFP_KERNEL);
706 if (!sc_xarray_element) {
711 sc_xarray_element->rx_sc = rx_sc;
712 err = xa_alloc(&macsec->sc_xarray, &sc_xarray_element->fs_id, sc_xarray_element,
713 XA_LIMIT(1, MLX5_MACEC_RX_FS_ID_MAX), GFP_KERNEL);
716 netdev_err(ctx->netdev,
717 "MACsec offload: unable to create entry for RX SC (%d Rx SCs already allocated)\n",
718 MLX5_MACEC_RX_FS_ID_MAX);
719 goto destroy_sc_xarray_elemenet;
722 rx_sc->md_dst = metadata_dst_alloc(0, METADATA_MACSEC, GFP_KERNEL);
723 if (!rx_sc->md_dst) {
728 rx_sc->sci = ctx_rx_sc->sci;
729 rx_sc->active = ctx_rx_sc->active;
730 list_add_rcu(&rx_sc->rx_sc_list_element, rx_sc_list);
732 rx_sc->sc_xarray_element = sc_xarray_element;
733 rx_sc->md_dst->u.macsec_info.sci = rx_sc->sci;
734 mutex_unlock(&macsec->lock);
739 xa_erase(&macsec->sc_xarray, sc_xarray_element->fs_id);
740 destroy_sc_xarray_elemenet:
741 kfree(sc_xarray_element);
746 mutex_unlock(&macsec->lock);
751 static int mlx5e_macsec_upd_rxsc(struct macsec_context *ctx)
753 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
754 const struct macsec_rx_sc *ctx_rx_sc = ctx->rx_sc;
755 struct mlx5e_macsec_device *macsec_device;
756 struct mlx5e_macsec_rx_sc *rx_sc;
757 struct mlx5e_macsec_sa *rx_sa;
758 struct mlx5e_macsec *macsec;
759 struct list_head *list;
763 mutex_lock(&priv->macsec->lock);
765 macsec = priv->macsec;
766 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
767 if (!macsec_device) {
768 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
773 list = &macsec_device->macsec_rx_sc_list_head;
774 rx_sc = mlx5e_macsec_get_rx_sc_from_sc_list(list, ctx_rx_sc->sci);
780 if (rx_sc->active == ctx_rx_sc->active)
783 rx_sc->active = ctx_rx_sc->active;
784 for (i = 0; i < MACSEC_NUM_AN; ++i) {
785 rx_sa = rx_sc->rx_sa[i];
789 err = macsec_rx_sa_active_update(ctx, rx_sa, rx_sa->active && ctx_rx_sc->active,
790 &rx_sc->sc_xarray_element->fs_id);
796 mutex_unlock(&macsec->lock);
801 static void macsec_del_rxsc_ctx(struct mlx5e_macsec *macsec, struct mlx5e_macsec_rx_sc *rx_sc,
802 struct net_device *netdev)
804 struct mlx5e_macsec_sa *rx_sa;
807 for (i = 0; i < MACSEC_NUM_AN; ++i) {
808 rx_sa = rx_sc->rx_sa[i];
812 mlx5e_macsec_cleanup_sa(macsec, rx_sa, false, netdev,
813 rx_sc->sc_xarray_element->fs_id);
814 mlx5_destroy_encryption_key(macsec->mdev, rx_sa->enc_key_id);
817 rx_sc->rx_sa[i] = NULL;
820 /* At this point the relevant MACsec offload Rx rule already removed at
821 * mlx5e_macsec_cleanup_sa need to wait for datapath to finish current
822 * Rx related data propagating using xa_erase which uses rcu to sync,
823 * once fs_id is erased then this rx_sc is hidden from datapath.
825 list_del_rcu(&rx_sc->rx_sc_list_element);
826 xa_erase(&macsec->sc_xarray, rx_sc->sc_xarray_element->fs_id);
827 metadata_dst_free(rx_sc->md_dst);
828 kfree(rx_sc->sc_xarray_element);
829 kfree_rcu_mightsleep(rx_sc);
832 static int mlx5e_macsec_del_rxsc(struct macsec_context *ctx)
834 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
835 struct mlx5e_macsec_device *macsec_device;
836 struct mlx5e_macsec_rx_sc *rx_sc;
837 struct mlx5e_macsec *macsec;
838 struct list_head *list;
841 mutex_lock(&priv->macsec->lock);
843 macsec = priv->macsec;
844 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
845 if (!macsec_device) {
846 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
851 list = &macsec_device->macsec_rx_sc_list_head;
852 rx_sc = mlx5e_macsec_get_rx_sc_from_sc_list(list, ctx->rx_sc->sci);
854 netdev_err(ctx->netdev,
855 "MACsec offload rx_sc sci %lld doesn't exist\n",
856 ctx->sa.rx_sa->sc->sci);
861 macsec_del_rxsc_ctx(macsec, rx_sc, ctx->secy->netdev);
863 mutex_unlock(&macsec->lock);
868 static int mlx5e_macsec_add_rxsa(struct macsec_context *ctx)
870 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
871 const struct macsec_rx_sa *ctx_rx_sa = ctx->sa.rx_sa;
872 struct mlx5e_macsec_device *macsec_device;
873 struct mlx5_core_dev *mdev = priv->mdev;
874 u8 assoc_num = ctx->sa.assoc_num;
875 struct mlx5e_macsec_rx_sc *rx_sc;
876 sci_t sci = ctx_rx_sa->sc->sci;
877 struct mlx5e_macsec_sa *rx_sa;
878 struct mlx5e_macsec *macsec;
879 struct list_head *list;
882 mutex_lock(&priv->macsec->lock);
884 macsec = priv->macsec;
885 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
886 if (!macsec_device) {
887 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
892 list = &macsec_device->macsec_rx_sc_list_head;
893 rx_sc = mlx5e_macsec_get_rx_sc_from_sc_list(list, sci);
895 netdev_err(ctx->netdev,
896 "MACsec offload rx_sc sci %lld doesn't exist\n",
897 ctx->sa.rx_sa->sc->sci);
902 if (rx_sc->rx_sa[assoc_num]) {
903 netdev_err(ctx->netdev,
904 "MACsec offload rx_sc sci %lld rx_sa %d already exist\n",
910 rx_sa = kzalloc(sizeof(*rx_sa), GFP_KERNEL);
916 rx_sa->active = ctx_rx_sa->active;
917 rx_sa->next_pn = ctx_rx_sa->next_pn;
919 rx_sa->assoc_num = assoc_num;
922 update_macsec_epn(rx_sa, &ctx_rx_sa->key, &ctx_rx_sa->next_pn_halves,
925 err = mlx5_create_encryption_key(mdev, ctx->sa.key, ctx->secy->key_len,
926 MLX5_ACCEL_OBJ_MACSEC_KEY,
931 rx_sc->rx_sa[assoc_num] = rx_sa;
935 //TODO - add support for both authentication and encryption flows
936 err = mlx5e_macsec_init_sa(ctx, rx_sa, true, false, &rx_sc->sc_xarray_element->fs_id);
938 goto destroy_encryption_key;
942 destroy_encryption_key:
943 rx_sc->rx_sa[assoc_num] = NULL;
944 mlx5_destroy_encryption_key(mdev, rx_sa->enc_key_id);
948 mutex_unlock(&macsec->lock);
953 static int mlx5e_macsec_upd_rxsa(struct macsec_context *ctx)
955 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
956 const struct macsec_rx_sa *ctx_rx_sa = ctx->sa.rx_sa;
957 struct mlx5e_macsec_device *macsec_device;
958 u8 assoc_num = ctx->sa.assoc_num;
959 struct mlx5e_macsec_rx_sc *rx_sc;
960 sci_t sci = ctx_rx_sa->sc->sci;
961 struct mlx5e_macsec_sa *rx_sa;
962 struct mlx5e_macsec *macsec;
963 struct list_head *list;
966 mutex_lock(&priv->macsec->lock);
968 macsec = priv->macsec;
969 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
970 if (!macsec_device) {
971 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
976 list = &macsec_device->macsec_rx_sc_list_head;
977 rx_sc = mlx5e_macsec_get_rx_sc_from_sc_list(list, sci);
979 netdev_err(ctx->netdev,
980 "MACsec offload rx_sc sci %lld doesn't exist\n",
981 ctx->sa.rx_sa->sc->sci);
986 rx_sa = rx_sc->rx_sa[assoc_num];
988 netdev_err(ctx->netdev,
989 "MACsec offload rx_sc sci %lld rx_sa %d doesn't exist\n",
995 if (ctx->sa.update_pn) {
996 netdev_err(ctx->netdev,
997 "MACsec offload update RX sa %d PN isn't supported\n",
1003 err = macsec_rx_sa_active_update(ctx, rx_sa, ctx_rx_sa->active,
1004 &rx_sc->sc_xarray_element->fs_id);
1006 mutex_unlock(&macsec->lock);
1011 static int mlx5e_macsec_del_rxsa(struct macsec_context *ctx)
1013 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
1014 struct mlx5e_macsec_device *macsec_device;
1015 sci_t sci = ctx->sa.rx_sa->sc->sci;
1016 struct mlx5e_macsec_rx_sc *rx_sc;
1017 u8 assoc_num = ctx->sa.assoc_num;
1018 struct mlx5e_macsec_sa *rx_sa;
1019 struct mlx5e_macsec *macsec;
1020 struct list_head *list;
1023 mutex_lock(&priv->macsec->lock);
1025 macsec = priv->macsec;
1026 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
1027 if (!macsec_device) {
1028 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
1033 list = &macsec_device->macsec_rx_sc_list_head;
1034 rx_sc = mlx5e_macsec_get_rx_sc_from_sc_list(list, sci);
1036 netdev_err(ctx->netdev,
1037 "MACsec offload rx_sc sci %lld doesn't exist\n",
1038 ctx->sa.rx_sa->sc->sci);
1043 rx_sa = rx_sc->rx_sa[assoc_num];
1045 netdev_err(ctx->netdev,
1046 "MACsec offload rx_sc sci %lld rx_sa %d doesn't exist\n",
1053 mlx5e_macsec_cleanup_sa(macsec, rx_sa, false, ctx->secy->netdev,
1054 rx_sc->sc_xarray_element->fs_id);
1055 mlx5_destroy_encryption_key(macsec->mdev, rx_sa->enc_key_id);
1057 rx_sc->rx_sa[assoc_num] = NULL;
1060 mutex_unlock(&macsec->lock);
1065 static int mlx5e_macsec_add_secy(struct macsec_context *ctx)
1067 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
1068 const struct net_device *dev = ctx->secy->netdev;
1069 const struct net_device *netdev = ctx->netdev;
1070 struct mlx5e_macsec_device *macsec_device;
1071 struct mlx5e_macsec *macsec;
1074 if (!mlx5e_macsec_secy_features_validate(ctx))
1077 mutex_lock(&priv->macsec->lock);
1078 macsec = priv->macsec;
1079 if (mlx5e_macsec_get_macsec_device_context(macsec, ctx)) {
1080 netdev_err(netdev, "MACsec offload: MACsec net_device already exist\n");
1084 if (macsec->num_of_devices >= MLX5_MACSEC_NUM_OF_SUPPORTED_INTERFACES) {
1085 netdev_err(netdev, "Currently, only %d MACsec offload devices can be set\n",
1086 MLX5_MACSEC_NUM_OF_SUPPORTED_INTERFACES);
1091 macsec_device = kzalloc(sizeof(*macsec_device), GFP_KERNEL);
1092 if (!macsec_device) {
1097 macsec_device->dev_addr = kmemdup(dev->dev_addr, dev->addr_len, GFP_KERNEL);
1098 if (!macsec_device->dev_addr) {
1099 kfree(macsec_device);
1104 macsec_device->netdev = dev;
1106 INIT_LIST_HEAD_RCU(&macsec_device->macsec_rx_sc_list_head);
1107 list_add_rcu(&macsec_device->macsec_device_list_element, &macsec->macsec_device_list_head);
1109 ++macsec->num_of_devices;
1111 mutex_unlock(&macsec->lock);
1116 static int macsec_upd_secy_hw_address(struct macsec_context *ctx,
1117 struct mlx5e_macsec_device *macsec_device)
1119 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
1120 const struct net_device *dev = ctx->secy->netdev;
1121 struct mlx5e_macsec *macsec = priv->macsec;
1122 struct mlx5e_macsec_rx_sc *rx_sc, *tmp;
1123 struct mlx5e_macsec_sa *rx_sa;
1124 struct list_head *list;
1128 list = &macsec_device->macsec_rx_sc_list_head;
1129 list_for_each_entry_safe(rx_sc, tmp, list, rx_sc_list_element) {
1130 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1131 rx_sa = rx_sc->rx_sa[i];
1132 if (!rx_sa || !rx_sa->macsec_rule)
1135 mlx5e_macsec_cleanup_sa_fs(macsec, rx_sa, false, ctx->secy->netdev,
1136 rx_sc->sc_xarray_element->fs_id);
1140 list_for_each_entry_safe(rx_sc, tmp, list, rx_sc_list_element) {
1141 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1142 rx_sa = rx_sc->rx_sa[i];
1146 if (rx_sa->active) {
1147 err = mlx5e_macsec_init_sa_fs(ctx, rx_sa, true, false,
1148 &rx_sc->sc_xarray_element->fs_id);
1155 memcpy(macsec_device->dev_addr, dev->dev_addr, dev->addr_len);
1160 /* this function is called from 2 macsec ops functions:
1161 * macsec_set_mac_address – MAC address was changed, therefore we need to destroy
1162 * and create new Tx contexts(macsec object + steering).
1163 * macsec_changelink – in this case the tx SC or SecY may be changed, therefore need to
1164 * destroy Tx and Rx contexts(macsec object + steering)
1166 static int mlx5e_macsec_upd_secy(struct macsec_context *ctx)
1168 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
1169 const struct macsec_tx_sc *tx_sc = &ctx->secy->tx_sc;
1170 const struct net_device *dev = ctx->secy->netdev;
1171 struct mlx5e_macsec_device *macsec_device;
1172 struct mlx5e_macsec_sa *tx_sa;
1173 struct mlx5e_macsec *macsec;
1176 if (!mlx5e_macsec_secy_features_validate(ctx))
1179 mutex_lock(&priv->macsec->lock);
1181 macsec = priv->macsec;
1182 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
1183 if (!macsec_device) {
1184 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
1189 /* if the dev_addr hasn't change, it mean the callback is from macsec_changelink */
1190 if (!memcmp(macsec_device->dev_addr, dev->dev_addr, dev->addr_len)) {
1191 err = macsec_upd_secy_hw_address(ctx, macsec_device);
1196 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1197 tx_sa = macsec_device->tx_sa[i];
1201 mlx5e_macsec_cleanup_sa_fs(macsec, tx_sa, true, ctx->secy->netdev, 0);
1204 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1205 tx_sa = macsec_device->tx_sa[i];
1209 if (tx_sa->assoc_num == tx_sc->encoding_sa && tx_sa->active) {
1210 err = mlx5e_macsec_init_sa_fs(ctx, tx_sa, tx_sc->encrypt, true, NULL);
1217 mutex_unlock(&macsec->lock);
1222 static int mlx5e_macsec_del_secy(struct macsec_context *ctx)
1224 struct mlx5e_priv *priv = macsec_netdev_priv(ctx->netdev);
1225 struct mlx5e_macsec_device *macsec_device;
1226 struct mlx5e_macsec_rx_sc *rx_sc, *tmp;
1227 struct mlx5e_macsec_sa *tx_sa;
1228 struct mlx5e_macsec *macsec;
1229 struct list_head *list;
1233 mutex_lock(&priv->macsec->lock);
1234 macsec = priv->macsec;
1235 macsec_device = mlx5e_macsec_get_macsec_device_context(macsec, ctx);
1236 if (!macsec_device) {
1237 netdev_err(ctx->netdev, "MACsec offload: Failed to find device context\n");
1243 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1244 tx_sa = macsec_device->tx_sa[i];
1248 mlx5e_macsec_cleanup_sa(macsec, tx_sa, true, ctx->secy->netdev, 0);
1249 mlx5_destroy_encryption_key(macsec->mdev, tx_sa->enc_key_id);
1251 macsec_device->tx_sa[i] = NULL;
1254 list = &macsec_device->macsec_rx_sc_list_head;
1255 list_for_each_entry_safe(rx_sc, tmp, list, rx_sc_list_element)
1256 macsec_del_rxsc_ctx(macsec, rx_sc, ctx->secy->netdev);
1258 kfree(macsec_device->dev_addr);
1259 macsec_device->dev_addr = NULL;
1261 list_del_rcu(&macsec_device->macsec_device_list_element);
1262 --macsec->num_of_devices;
1263 kfree(macsec_device);
1266 mutex_unlock(&macsec->lock);
1271 static void macsec_build_accel_attrs(struct mlx5e_macsec_sa *sa,
1272 struct mlx5_macsec_obj_attrs *attrs)
1274 attrs->epn_state.epn_msb = sa->epn_state.epn_msb;
1275 attrs->epn_state.overlap = sa->epn_state.overlap;
1278 static void macsec_aso_build_wqe_ctrl_seg(struct mlx5e_macsec_aso *macsec_aso,
1279 struct mlx5_wqe_aso_ctrl_seg *aso_ctrl,
1280 struct mlx5_aso_ctrl_param *param)
1282 struct mlx5e_macsec_umr *umr = macsec_aso->umr;
1284 memset(aso_ctrl, 0, sizeof(*aso_ctrl));
1285 aso_ctrl->va_l = cpu_to_be32(umr->dma_addr | ASO_CTRL_READ_EN);
1286 aso_ctrl->va_h = cpu_to_be32((u64)umr->dma_addr >> 32);
1287 aso_ctrl->l_key = cpu_to_be32(umr->mkey);
1292 aso_ctrl->data_mask_mode = param->data_mask_mode << 6;
1293 aso_ctrl->condition_1_0_operand = param->condition_1_operand |
1294 param->condition_0_operand << 4;
1295 aso_ctrl->condition_1_0_offset = param->condition_1_offset |
1296 param->condition_0_offset << 4;
1297 aso_ctrl->data_offset_condition_operand = param->data_offset |
1298 param->condition_operand << 6;
1299 aso_ctrl->condition_0_data = cpu_to_be32(param->condition_0_data);
1300 aso_ctrl->condition_0_mask = cpu_to_be32(param->condition_0_mask);
1301 aso_ctrl->condition_1_data = cpu_to_be32(param->condition_1_data);
1302 aso_ctrl->condition_1_mask = cpu_to_be32(param->condition_1_mask);
1303 aso_ctrl->bitwise_data = cpu_to_be64(param->bitwise_data);
1304 aso_ctrl->data_mask = cpu_to_be64(param->data_mask);
1307 static int mlx5e_macsec_modify_obj(struct mlx5_core_dev *mdev, struct mlx5_macsec_obj_attrs *attrs,
1310 u32 in[MLX5_ST_SZ_DW(modify_macsec_obj_in)] = {};
1311 u32 out[MLX5_ST_SZ_DW(query_macsec_obj_out)];
1312 u64 modify_field_select = 0;
1316 /* General object fields set */
1317 MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_QUERY_GENERAL_OBJECT);
1318 MLX5_SET(general_obj_in_cmd_hdr, in, obj_type, MLX5_GENERAL_OBJECT_TYPES_MACSEC);
1319 MLX5_SET(general_obj_in_cmd_hdr, in, obj_id, macsec_id);
1320 err = mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
1322 mlx5_core_err(mdev, "Query MACsec object failed (Object id %d), err = %d\n",
1327 obj = MLX5_ADDR_OF(query_macsec_obj_out, out, macsec_object);
1328 modify_field_select = MLX5_GET64(macsec_offload_obj, obj, modify_field_select);
1331 if (!(modify_field_select & MLX5_MODIFY_MACSEC_BITMASK_EPN_OVERLAP) ||
1332 !(modify_field_select & MLX5_MODIFY_MACSEC_BITMASK_EPN_MSB)) {
1333 mlx5_core_dbg(mdev, "MACsec object field is not modifiable (Object id %d)\n",
1338 obj = MLX5_ADDR_OF(modify_macsec_obj_in, in, macsec_object);
1339 MLX5_SET64(macsec_offload_obj, obj, modify_field_select,
1340 MLX5_MODIFY_MACSEC_BITMASK_EPN_OVERLAP | MLX5_MODIFY_MACSEC_BITMASK_EPN_MSB);
1341 MLX5_SET(macsec_offload_obj, obj, epn_msb, attrs->epn_state.epn_msb);
1342 MLX5_SET(macsec_offload_obj, obj, epn_overlap, attrs->epn_state.overlap);
1344 /* General object fields set */
1345 MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_MODIFY_GENERAL_OBJECT);
1347 return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
1350 static void macsec_aso_build_ctrl(struct mlx5e_macsec_aso *aso,
1351 struct mlx5_wqe_aso_ctrl_seg *aso_ctrl,
1352 struct mlx5e_macsec_aso_in *in)
1354 struct mlx5_aso_ctrl_param param = {};
1356 param.data_mask_mode = MLX5_ASO_DATA_MASK_MODE_BITWISE_64BIT;
1357 param.condition_0_operand = MLX5_ASO_ALWAYS_TRUE;
1358 param.condition_1_operand = MLX5_ASO_ALWAYS_TRUE;
1359 if (in->mode == MLX5_MACSEC_EPN) {
1360 param.data_offset = MLX5_MACSEC_ASO_REMOVE_FLOW_PKT_CNT_OFFSET;
1361 param.bitwise_data = BIT_ULL(54);
1362 param.data_mask = param.bitwise_data;
1364 macsec_aso_build_wqe_ctrl_seg(aso, aso_ctrl, ¶m);
1367 static int macsec_aso_set_arm_event(struct mlx5_core_dev *mdev, struct mlx5e_macsec *macsec,
1368 struct mlx5e_macsec_aso_in *in)
1370 struct mlx5e_macsec_aso *aso;
1371 struct mlx5_aso_wqe *aso_wqe;
1372 struct mlx5_aso *maso;
1378 mutex_lock(&aso->aso_lock);
1379 aso_wqe = mlx5_aso_get_wqe(maso);
1380 mlx5_aso_build_wqe(maso, MLX5_MACSEC_ASO_DS_CNT, aso_wqe, in->obj_id,
1381 MLX5_ACCESS_ASO_OPC_MOD_MACSEC);
1382 macsec_aso_build_ctrl(aso, &aso_wqe->aso_ctrl, in);
1383 mlx5_aso_post_wqe(maso, false, &aso_wqe->ctrl);
1384 err = mlx5_aso_poll_cq(maso, false);
1385 mutex_unlock(&aso->aso_lock);
1390 static int macsec_aso_query(struct mlx5_core_dev *mdev, struct mlx5e_macsec *macsec,
1391 struct mlx5e_macsec_aso_in *in, struct mlx5e_macsec_aso_out *out)
1393 struct mlx5e_macsec_aso *aso;
1394 struct mlx5_aso_wqe *aso_wqe;
1395 struct mlx5_aso *maso;
1396 unsigned long expires;
1402 mutex_lock(&aso->aso_lock);
1404 aso_wqe = mlx5_aso_get_wqe(maso);
1405 mlx5_aso_build_wqe(maso, MLX5_MACSEC_ASO_DS_CNT, aso_wqe, in->obj_id,
1406 MLX5_ACCESS_ASO_OPC_MOD_MACSEC);
1407 macsec_aso_build_wqe_ctrl_seg(aso, &aso_wqe->aso_ctrl, NULL);
1409 mlx5_aso_post_wqe(maso, false, &aso_wqe->ctrl);
1410 expires = jiffies + msecs_to_jiffies(10);
1412 err = mlx5_aso_poll_cq(maso, false);
1414 usleep_range(2, 10);
1415 } while (err && time_is_after_jiffies(expires));
1420 if (MLX5_GET(macsec_aso, aso->umr->ctx, epn_event_arm))
1421 out->event_arm |= MLX5E_ASO_EPN_ARM;
1423 out->mode_param = MLX5_GET(macsec_aso, aso->umr->ctx, mode_parameter);
1426 mutex_unlock(&aso->aso_lock);
1430 static struct mlx5e_macsec_sa *get_macsec_tx_sa_from_obj_id(const struct mlx5e_macsec *macsec,
1433 const struct list_head *device_list;
1434 struct mlx5e_macsec_sa *macsec_sa;
1435 struct mlx5e_macsec_device *iter;
1438 device_list = &macsec->macsec_device_list_head;
1440 list_for_each_entry(iter, device_list, macsec_device_list_element) {
1441 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1442 macsec_sa = iter->tx_sa[i];
1443 if (!macsec_sa || !macsec_sa->active)
1445 if (macsec_sa->macsec_obj_id == obj_id)
1453 static struct mlx5e_macsec_sa *get_macsec_rx_sa_from_obj_id(const struct mlx5e_macsec *macsec,
1456 const struct list_head *device_list, *sc_list;
1457 struct mlx5e_macsec_rx_sc *mlx5e_rx_sc;
1458 struct mlx5e_macsec_sa *macsec_sa;
1459 struct mlx5e_macsec_device *iter;
1462 device_list = &macsec->macsec_device_list_head;
1464 list_for_each_entry(iter, device_list, macsec_device_list_element) {
1465 sc_list = &iter->macsec_rx_sc_list_head;
1466 list_for_each_entry(mlx5e_rx_sc, sc_list, rx_sc_list_element) {
1467 for (i = 0; i < MACSEC_NUM_AN; ++i) {
1468 macsec_sa = mlx5e_rx_sc->rx_sa[i];
1469 if (!macsec_sa || !macsec_sa->active)
1471 if (macsec_sa->macsec_obj_id == obj_id)
1480 static void macsec_epn_update(struct mlx5e_macsec *macsec, struct mlx5_core_dev *mdev,
1481 struct mlx5e_macsec_sa *sa, u32 obj_id, u32 mode_param)
1483 struct mlx5_macsec_obj_attrs attrs = {};
1484 struct mlx5e_macsec_aso_in in = {};
1486 /* When the bottom of the replay protection window (mode_param) crosses 2^31 (half sequence
1487 * number wraparound) hence mode_param > MLX5_MACSEC_EPN_SCOPE_MID the SW should update the
1488 * esn_overlap to OLD (1).
1489 * When the bottom of the replay protection window (mode_param) crosses 2^32 (full sequence
1490 * number wraparound) hence mode_param < MLX5_MACSEC_EPN_SCOPE_MID since it did a
1491 * wraparound, the SW should update the esn_overlap to NEW (0), and increment the esn_msb.
1494 if (mode_param < MLX5_MACSEC_EPN_SCOPE_MID) {
1495 sa->epn_state.epn_msb++;
1496 sa->epn_state.overlap = 0;
1498 sa->epn_state.overlap = 1;
1501 macsec_build_accel_attrs(sa, &attrs);
1502 mlx5e_macsec_modify_obj(mdev, &attrs, obj_id);
1504 /* Re-set EPN arm event */
1506 in.mode = MLX5_MACSEC_EPN;
1507 macsec_aso_set_arm_event(mdev, macsec, &in);
1510 static void macsec_async_event(struct work_struct *work)
1512 struct mlx5e_macsec_async_work *async_work;
1513 struct mlx5e_macsec_aso_out out = {};
1514 struct mlx5e_macsec_aso_in in = {};
1515 struct mlx5e_macsec_sa *macsec_sa;
1516 struct mlx5e_macsec *macsec;
1517 struct mlx5_core_dev *mdev;
1520 async_work = container_of(work, struct mlx5e_macsec_async_work, work);
1521 macsec = async_work->macsec;
1522 mutex_lock(&macsec->lock);
1524 mdev = async_work->mdev;
1525 obj_id = async_work->obj_id;
1526 macsec_sa = get_macsec_tx_sa_from_obj_id(macsec, obj_id);
1528 macsec_sa = get_macsec_rx_sa_from_obj_id(macsec, obj_id);
1530 mlx5_core_dbg(mdev, "MACsec SA is not found (SA object id %d)\n", obj_id);
1531 goto out_async_work;
1535 /* Query MACsec ASO context */
1537 macsec_aso_query(mdev, macsec, &in, &out);
1540 if (macsec_sa->epn_state.epn_enabled && !(out.event_arm & MLX5E_ASO_EPN_ARM))
1541 macsec_epn_update(macsec, mdev, macsec_sa, obj_id, out.mode_param);
1545 mutex_unlock(&macsec->lock);
1548 static int macsec_obj_change_event(struct notifier_block *nb, unsigned long event, void *data)
1550 struct mlx5e_macsec *macsec = container_of(nb, struct mlx5e_macsec, nb);
1551 struct mlx5e_macsec_async_work *async_work;
1552 struct mlx5_eqe_obj_change *obj_change;
1553 struct mlx5_eqe *eqe = data;
1557 if (event != MLX5_EVENT_TYPE_OBJECT_CHANGE)
1560 obj_change = &eqe->data.obj_change;
1561 obj_type = be16_to_cpu(obj_change->obj_type);
1562 obj_id = be32_to_cpu(obj_change->obj_id);
1564 if (obj_type != MLX5_GENERAL_OBJECT_TYPES_MACSEC)
1567 async_work = kzalloc(sizeof(*async_work), GFP_ATOMIC);
1571 async_work->macsec = macsec;
1572 async_work->mdev = macsec->mdev;
1573 async_work->obj_id = obj_id;
1575 INIT_WORK(&async_work->work, macsec_async_event);
1577 WARN_ON(!queue_work(macsec->wq, &async_work->work));
1582 static int mlx5e_macsec_aso_init(struct mlx5e_macsec_aso *aso, struct mlx5_core_dev *mdev)
1584 struct mlx5_aso *maso;
1587 err = mlx5_core_alloc_pd(mdev, &aso->pdn);
1590 "MACsec offload: Failed to alloc pd for MACsec ASO, err=%d\n",
1595 maso = mlx5_aso_create(mdev, aso->pdn);
1597 err = PTR_ERR(maso);
1601 err = mlx5e_macsec_aso_reg_mr(mdev, aso);
1605 mutex_init(&aso->aso_lock);
1612 mlx5_aso_destroy(maso);
1614 mlx5_core_dealloc_pd(mdev, aso->pdn);
1618 static void mlx5e_macsec_aso_cleanup(struct mlx5e_macsec_aso *aso, struct mlx5_core_dev *mdev)
1623 mlx5e_macsec_aso_dereg_mr(mdev, aso);
1625 mlx5_aso_destroy(aso->maso);
1627 mlx5_core_dealloc_pd(mdev, aso->pdn);
1630 static const struct macsec_ops macsec_offload_ops = {
1631 .mdo_add_txsa = mlx5e_macsec_add_txsa,
1632 .mdo_upd_txsa = mlx5e_macsec_upd_txsa,
1633 .mdo_del_txsa = mlx5e_macsec_del_txsa,
1634 .mdo_add_rxsc = mlx5e_macsec_add_rxsc,
1635 .mdo_upd_rxsc = mlx5e_macsec_upd_rxsc,
1636 .mdo_del_rxsc = mlx5e_macsec_del_rxsc,
1637 .mdo_add_rxsa = mlx5e_macsec_add_rxsa,
1638 .mdo_upd_rxsa = mlx5e_macsec_upd_rxsa,
1639 .mdo_del_rxsa = mlx5e_macsec_del_rxsa,
1640 .mdo_add_secy = mlx5e_macsec_add_secy,
1641 .mdo_upd_secy = mlx5e_macsec_upd_secy,
1642 .mdo_del_secy = mlx5e_macsec_del_secy,
1645 bool mlx5e_macsec_handle_tx_skb(struct mlx5e_macsec *macsec, struct sk_buff *skb)
1647 struct metadata_dst *md_dst = skb_metadata_dst(skb);
1650 fs_id = mlx5_macsec_fs_get_fs_id_from_hashtable(macsec->mdev->macsec_fs,
1651 &md_dst->u.macsec_info.sci);
1658 dev_kfree_skb_any(skb);
1662 void mlx5e_macsec_tx_build_eseg(struct mlx5e_macsec *macsec,
1663 struct sk_buff *skb,
1664 struct mlx5_wqe_eth_seg *eseg)
1666 struct metadata_dst *md_dst = skb_metadata_dst(skb);
1669 fs_id = mlx5_macsec_fs_get_fs_id_from_hashtable(macsec->mdev->macsec_fs,
1670 &md_dst->u.macsec_info.sci);
1674 eseg->flow_table_metadata = cpu_to_be32(MLX5_ETH_WQE_FT_META_MACSEC | fs_id << 2);
1677 void mlx5e_macsec_offload_handle_rx_skb(struct net_device *netdev,
1678 struct sk_buff *skb,
1679 struct mlx5_cqe64 *cqe)
1681 struct mlx5e_macsec_rx_sc_xarray_element *sc_xarray_element;
1682 u32 macsec_meta_data = be32_to_cpu(cqe->ft_metadata);
1683 struct mlx5e_priv *priv = macsec_netdev_priv(netdev);
1684 struct mlx5e_macsec_rx_sc *rx_sc;
1685 struct mlx5e_macsec *macsec;
1688 macsec = priv->macsec;
1692 fs_id = MLX5_MACSEC_RX_METADAT_HANDLE(macsec_meta_data);
1695 sc_xarray_element = xa_load(&macsec->sc_xarray, fs_id);
1696 rx_sc = sc_xarray_element->rx_sc;
1698 dst_hold(&rx_sc->md_dst->dst);
1699 skb_dst_set(skb, &rx_sc->md_dst->dst);
1705 void mlx5e_macsec_build_netdev(struct mlx5e_priv *priv)
1707 struct net_device *netdev = priv->netdev;
1709 if (!mlx5e_is_macsec_device(priv->mdev))
1713 mlx5_core_dbg(priv->mdev, "mlx5e: MACsec acceleration enabled\n");
1714 netdev->macsec_ops = &macsec_offload_ops;
1715 netdev->features |= NETIF_F_HW_MACSEC;
1716 netif_keep_dst(netdev);
1719 int mlx5e_macsec_init(struct mlx5e_priv *priv)
1721 struct mlx5_core_dev *mdev = priv->mdev;
1722 struct mlx5e_macsec *macsec = NULL;
1723 struct mlx5_macsec_fs *macsec_fs;
1726 if (!mlx5e_is_macsec_device(priv->mdev)) {
1727 mlx5_core_dbg(mdev, "Not a MACsec offload device\n");
1731 macsec = kzalloc(sizeof(*macsec), GFP_KERNEL);
1735 INIT_LIST_HEAD(&macsec->macsec_device_list_head);
1736 mutex_init(&macsec->lock);
1738 err = mlx5e_macsec_aso_init(&macsec->aso, priv->mdev);
1740 mlx5_core_err(mdev, "MACsec offload: Failed to init aso, err=%d\n", err);
1744 macsec->wq = alloc_ordered_workqueue("mlx5e_macsec_%s", 0, priv->netdev->name);
1750 xa_init_flags(&macsec->sc_xarray, XA_FLAGS_ALLOC1);
1752 priv->macsec = macsec;
1754 macsec->mdev = mdev;
1756 macsec_fs = mlx5_macsec_fs_init(mdev);
1762 mdev->macsec_fs = macsec_fs;
1764 macsec->nb.notifier_call = macsec_obj_change_event;
1765 mlx5_notifier_register(mdev, &macsec->nb);
1767 mlx5_core_dbg(mdev, "MACsec attached to netdevice\n");
1772 destroy_workqueue(macsec->wq);
1774 mlx5e_macsec_aso_cleanup(&macsec->aso, priv->mdev);
1777 priv->macsec = NULL;
1781 void mlx5e_macsec_cleanup(struct mlx5e_priv *priv)
1783 struct mlx5e_macsec *macsec = priv->macsec;
1784 struct mlx5_core_dev *mdev = priv->mdev;
1789 mlx5_notifier_unregister(mdev, &macsec->nb);
1790 mlx5_macsec_fs_cleanup(mdev->macsec_fs);
1791 destroy_workqueue(macsec->wq);
1792 mlx5e_macsec_aso_cleanup(&macsec->aso, mdev);
1793 mutex_destroy(&macsec->lock);