+ if (arg_size != sizeof (*spec->ema_vlan_push_descs)) {
+ rc = EINVAL;
+ goto fail1;
+ }
+
+ if (arg == NULL) {
+ rc = EINVAL;
+ goto fail2;
+ }
+
+ if (n_tags == EFX_MAE_VLAN_PUSH_MAX_NTAGS) {
+ rc = ENOTSUP;
+ goto fail3;
+ }
+
+ memcpy(&spec->ema_vlan_push_descs[n_tags], arg, arg_size);
+ ++(spec->ema_n_vlan_tags_to_push);
+
+ return (0);
+
+fail3:
+ EFSYS_PROBE(fail3);
+fail2:
+ EFSYS_PROBE(fail2);
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+static __checkReturn efx_rc_t
+efx_mae_action_set_add_flag(
+ __in efx_mae_actions_t *spec,
+ __in size_t arg_size,
+ __in_bcount(arg_size) const uint8_t *arg)
+{
+ efx_rc_t rc;
+
+ _NOTE(ARGUNUSED(spec))
+
+ if (arg_size != 0) {
+ rc = EINVAL;
+ goto fail1;
+ }
+
+ if (arg != NULL) {
+ rc = EINVAL;
+ goto fail2;
+ }
+
+ /* This action does not have any arguments, so do nothing here. */
+
+ return (0);
+
+fail2:
+ EFSYS_PROBE(fail2);
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+static __checkReturn efx_rc_t
+efx_mae_action_set_add_mark(
+ __in efx_mae_actions_t *spec,
+ __in size_t arg_size,
+ __in_bcount(arg_size) const uint8_t *arg)
+{
+ efx_rc_t rc;
+
+ if (arg_size != sizeof (spec->ema_mark_value)) {
+ rc = EINVAL;
+ goto fail1;
+ }
+
+ if (arg == NULL) {
+ rc = EINVAL;
+ goto fail2;
+ }
+
+ memcpy(&spec->ema_mark_value, arg, arg_size);
+
+ return (0);
+
+fail2:
+ EFSYS_PROBE(fail2);
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+static __checkReturn efx_rc_t
+efx_mae_action_set_add_deliver(
+ __in efx_mae_actions_t *spec,
+ __in size_t arg_size,
+ __in_bcount(arg_size) const uint8_t *arg)
+{
+ efx_rc_t rc;
+
+ if (arg_size != sizeof (spec->ema_deliver_mport)) {
+ rc = EINVAL;
+ goto fail1;
+ }
+
+ if (arg == NULL) {
+ rc = EINVAL;
+ goto fail2;
+ }
+
+ memcpy(&spec->ema_deliver_mport, arg, arg_size);
+
+ return (0);
+
+fail2:
+ EFSYS_PROBE(fail2);
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+typedef struct efx_mae_action_desc_s {
+ /* Action specific handler */
+ efx_rc_t (*emad_add)(efx_mae_actions_t *,
+ size_t, const uint8_t *);
+} efx_mae_action_desc_t;
+
+static const efx_mae_action_desc_t efx_mae_actions[EFX_MAE_NACTIONS] = {
+ [EFX_MAE_ACTION_VLAN_POP] = {
+ .emad_add = efx_mae_action_set_add_vlan_pop
+ },
+ [EFX_MAE_ACTION_VLAN_PUSH] = {
+ .emad_add = efx_mae_action_set_add_vlan_push
+ },
+ [EFX_MAE_ACTION_FLAG] = {
+ .emad_add = efx_mae_action_set_add_flag
+ },
+ [EFX_MAE_ACTION_MARK] = {
+ .emad_add = efx_mae_action_set_add_mark
+ },
+ [EFX_MAE_ACTION_DELIVER] = {
+ .emad_add = efx_mae_action_set_add_deliver
+ }
+};
+
+static const uint32_t efx_mae_action_ordered_map =
+ (1U << EFX_MAE_ACTION_VLAN_POP) |
+ (1U << EFX_MAE_ACTION_VLAN_PUSH) |
+ (1U << EFX_MAE_ACTION_FLAG) |
+ (1U << EFX_MAE_ACTION_MARK) |
+ (1U << EFX_MAE_ACTION_DELIVER);
+
+/*
+ * These actions must not be added after DELIVER, but
+ * they can have any place among the rest of
+ * strictly ordered actions.
+ */
+static const uint32_t efx_mae_action_nonstrict_map =
+ (1U << EFX_MAE_ACTION_FLAG) |
+ (1U << EFX_MAE_ACTION_MARK);
+
+static const uint32_t efx_mae_action_repeat_map =
+ (1U << EFX_MAE_ACTION_VLAN_POP) |
+ (1U << EFX_MAE_ACTION_VLAN_PUSH);
+
+/*
+ * Add an action to an action set.
+ *
+ * This has to be invoked in the desired action order.
+ * An out-of-order action request will be turned down.
+ */
+static __checkReturn efx_rc_t
+efx_mae_action_set_spec_populate(
+ __in efx_mae_actions_t *spec,
+ __in efx_mae_action_t type,
+ __in size_t arg_size,
+ __in_bcount(arg_size) const uint8_t *arg)
+{
+ uint32_t action_mask;
+ efx_rc_t rc;
+
+ EFX_STATIC_ASSERT(EFX_MAE_NACTIONS <=
+ (sizeof (efx_mae_action_ordered_map) * 8));
+ EFX_STATIC_ASSERT(EFX_MAE_NACTIONS <=
+ (sizeof (efx_mae_action_repeat_map) * 8));
+
+ EFX_STATIC_ASSERT(EFX_MAE_ACTION_DELIVER + 1 == EFX_MAE_NACTIONS);
+ EFX_STATIC_ASSERT(EFX_MAE_ACTION_FLAG + 1 == EFX_MAE_ACTION_MARK);
+ EFX_STATIC_ASSERT(EFX_MAE_ACTION_MARK + 1 == EFX_MAE_ACTION_DELIVER);
+
+ if (type >= EFX_ARRAY_SIZE(efx_mae_actions)) {
+ rc = EINVAL;
+ goto fail1;
+ }
+
+ action_mask = (1U << type);
+
+ if ((spec->ema_actions & action_mask) != 0) {
+ /* The action set already contains this action. */
+ if ((efx_mae_action_repeat_map & action_mask) == 0) {
+ /* Cannot add another non-repeatable action. */
+ rc = ENOTSUP;
+ goto fail2;
+ }
+ }
+
+ if ((efx_mae_action_ordered_map & action_mask) != 0) {
+ uint32_t strict_ordered_map =
+ efx_mae_action_ordered_map & ~efx_mae_action_nonstrict_map;
+ uint32_t later_actions_mask =
+ strict_ordered_map & ~(action_mask | (action_mask - 1));
+
+ if ((spec->ema_actions & later_actions_mask) != 0) {
+ /* Cannot add an action after later ordered actions. */
+ rc = ENOTSUP;
+ goto fail3;
+ }
+ }
+
+ if (efx_mae_actions[type].emad_add != NULL) {
+ rc = efx_mae_actions[type].emad_add(spec, arg_size, arg);
+ if (rc != 0)
+ goto fail4;
+ }
+
+ spec->ema_actions |= action_mask;
+
+ return (0);
+
+fail4:
+ EFSYS_PROBE(fail4);
+fail3:
+ EFSYS_PROBE(fail3);
+fail2:
+ EFSYS_PROBE(fail2);
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+ __checkReturn efx_rc_t
+efx_mae_action_set_populate_vlan_pop(
+ __in efx_mae_actions_t *spec)
+{
+ return (efx_mae_action_set_spec_populate(spec,
+ EFX_MAE_ACTION_VLAN_POP, 0, NULL));
+}
+
+ __checkReturn efx_rc_t
+efx_mae_action_set_populate_vlan_push(
+ __in efx_mae_actions_t *spec,
+ __in uint16_t tpid_be,
+ __in uint16_t tci_be)
+{
+ efx_mae_action_vlan_push_t action;
+ const uint8_t *arg = (const uint8_t *)&action;
+
+ action.emavp_tpid_be = tpid_be;
+ action.emavp_tci_be = tci_be;
+
+ return (efx_mae_action_set_spec_populate(spec,
+ EFX_MAE_ACTION_VLAN_PUSH, sizeof (action), arg));
+}
+
+ __checkReturn efx_rc_t
+efx_mae_action_set_populate_flag(
+ __in efx_mae_actions_t *spec)
+{
+ return (efx_mae_action_set_spec_populate(spec,
+ EFX_MAE_ACTION_FLAG, 0, NULL));
+}
+
+ __checkReturn efx_rc_t
+efx_mae_action_set_populate_mark(
+ __in efx_mae_actions_t *spec,
+ __in uint32_t mark_value)
+{
+ const uint8_t *arg = (const uint8_t *)&mark_value;
+
+ return (efx_mae_action_set_spec_populate(spec,
+ EFX_MAE_ACTION_MARK, sizeof (mark_value), arg));
+}
+
+ __checkReturn efx_rc_t
+efx_mae_action_set_populate_deliver(
+ __in efx_mae_actions_t *spec,
+ __in const efx_mport_sel_t *mportp)
+{
+ const uint8_t *arg;
+ efx_rc_t rc;
+
+ if (mportp == NULL) {
+ rc = EINVAL;
+ goto fail1;
+ }
+
+ arg = (const uint8_t *)&mportp->sel;
+
+ return (efx_mae_action_set_spec_populate(spec,
+ EFX_MAE_ACTION_DELIVER, sizeof (mportp->sel), arg));
+
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+ __checkReturn efx_rc_t
+efx_mae_action_set_populate_drop(
+ __in efx_mae_actions_t *spec)
+{
+ efx_mport_sel_t mport;
+ const uint8_t *arg;
+ efx_dword_t dword;
+
+ EFX_POPULATE_DWORD_1(dword,
+ MAE_MPORT_SELECTOR_FLAT, MAE_MPORT_SELECTOR_NULL);
+
+ /*
+ * The constructed DWORD is little-endian,
+ * but the resulting value is meant to be
+ * passed to MCDIs, where it will undergo
+ * host-order to little endian conversion.
+ */
+ mport.sel = EFX_DWORD_FIELD(dword, EFX_DWORD_0);
+
+ arg = (const uint8_t *)&mport.sel;
+
+ return (efx_mae_action_set_spec_populate(spec,
+ EFX_MAE_ACTION_DELIVER, sizeof (mport.sel), arg));
+}
+
+ __checkReturn boolean_t
+efx_mae_action_set_specs_equal(
+ __in const efx_mae_actions_t *left,
+ __in const efx_mae_actions_t *right)
+{
+ return ((memcmp(left, right, sizeof (*left)) == 0) ? B_TRUE : B_FALSE);
+}
+
+ __checkReturn efx_rc_t
+efx_mae_match_specs_class_cmp(
+ __in efx_nic_t *enp,
+ __in const efx_mae_match_spec_t *left,
+ __in const efx_mae_match_spec_t *right,
+ __out boolean_t *have_same_classp)
+{
+ efx_mae_t *maep = enp->en_maep;
+ unsigned int field_ncaps = maep->em_max_nfields;
+ const efx_mae_field_cap_t *field_caps;
+ const efx_mae_mv_desc_t *desc_setp;
+ unsigned int desc_set_nentries;
+ boolean_t have_same_class = B_TRUE;
+ efx_mae_field_id_t field_id;
+ const uint8_t *mvpl;
+ const uint8_t *mvpr;
+ efx_rc_t rc;
+
+ switch (left->emms_type) {
+ case EFX_MAE_RULE_OUTER:
+ field_caps = maep->em_outer_rule_field_caps;
+ desc_setp = __efx_mae_outer_rule_mv_desc_set;
+ desc_set_nentries =
+ EFX_ARRAY_SIZE(__efx_mae_outer_rule_mv_desc_set);
+ mvpl = left->emms_mask_value_pairs.outer;
+ mvpr = right->emms_mask_value_pairs.outer;
+ break;
+ case EFX_MAE_RULE_ACTION:
+ field_caps = maep->em_action_rule_field_caps;
+ desc_setp = __efx_mae_action_rule_mv_desc_set;
+ desc_set_nentries =
+ EFX_ARRAY_SIZE(__efx_mae_action_rule_mv_desc_set);
+ mvpl = left->emms_mask_value_pairs.action;
+ mvpr = right->emms_mask_value_pairs.action;
+ break;
+ default:
+ rc = ENOTSUP;
+ goto fail1;
+ }
+
+ if (field_caps == NULL) {
+ rc = EAGAIN;
+ goto fail2;
+ }
+
+ if (left->emms_type != right->emms_type ||
+ left->emms_prio != right->emms_prio) {
+ /*
+ * Rules of different types can never map to the same class.
+ *
+ * The FW can support some set of match criteria for one
+ * priority and not support the very same set for
+ * another priority. Thus, two rules which have
+ * different priorities can never map to
+ * the same class.
+ */
+ *have_same_classp = B_FALSE;
+ return (0);
+ }
+
+ for (field_id = 0; (unsigned int)field_id < desc_set_nentries;
+ ++field_id) {
+ const efx_mae_mv_desc_t *descp = &desc_setp[field_id];
+ efx_mae_field_cap_id_t field_cap_id = descp->emmd_field_cap_id;
+ const uint8_t *lmaskp = mvpl + descp->emmd_mask_offset;
+ const uint8_t *rmaskp = mvpr + descp->emmd_mask_offset;
+ size_t mask_size = descp->emmd_mask_size;
+ const uint8_t *lvalp = mvpl + descp->emmd_value_offset;
+ const uint8_t *rvalp = mvpr + descp->emmd_value_offset;
+ size_t value_size = descp->emmd_value_size;
+
+ if (mask_size == 0)
+ continue; /* Skip array gap */
+
+ if ((unsigned int)field_cap_id >= field_ncaps) {
+ /*
+ * The FW has not reported capability status for this
+ * field. It's unknown whether any difference between
+ * the two masks / values affects the class. The only
+ * case when the class must be the same is when these
+ * mask-value pairs match. Otherwise, report mismatch.
+ */
+ if ((memcmp(lmaskp, rmaskp, mask_size) == 0) &&
+ (memcmp(lvalp, rvalp, value_size) == 0))
+ continue;
+ else
+ break;
+ }
+
+ if (field_caps[field_cap_id].emfc_mask_affects_class) {
+ if (memcmp(lmaskp, rmaskp, mask_size) != 0) {
+ have_same_class = B_FALSE;
+ break;
+ }
+ }
+
+ if (field_caps[field_cap_id].emfc_match_affects_class) {
+ if (memcmp(lvalp, rvalp, value_size) != 0) {
+ have_same_class = B_FALSE;
+ break;
+ }
+ }
+ }
+
+ *have_same_classp = have_same_class;
+
+ return (0);
+
+fail2:
+ EFSYS_PROBE(fail2);
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+ return (rc);
+}
+
+ __checkReturn efx_rc_t
+efx_mae_outer_rule_insert(
+ __in efx_nic_t *enp,
+ __in const efx_mae_match_spec_t *spec,
+ __in efx_tunnel_protocol_t encap_type,
+ __out efx_mae_rule_id_t *or_idp)
+{
+ const efx_nic_cfg_t *encp = efx_nic_cfg_get(enp);
+ efx_mcdi_req_t req;
+ EFX_MCDI_DECLARE_BUF(payload,
+ MC_CMD_MAE_OUTER_RULE_INSERT_IN_LENMAX_MCDI2,
+ MC_CMD_MAE_OUTER_RULE_INSERT_OUT_LEN);
+ uint32_t encap_type_mcdi;
+ efx_mae_rule_id_t or_id;
+ size_t offset;
+ efx_rc_t rc;
+
+ EFX_STATIC_ASSERT(sizeof (or_idp->id) ==
+ MC_CMD_MAE_OUTER_RULE_INSERT_OUT_OR_ID_LEN);
+
+ EFX_STATIC_ASSERT(EFX_MAE_RSRC_ID_INVALID ==
+ MC_CMD_MAE_OUTER_RULE_INSERT_OUT_OUTER_RULE_ID_NULL);
+
+ if (encp->enc_mae_supported == B_FALSE) {
+ rc = ENOTSUP;
+ goto fail1;
+ }
+
+ if (spec->emms_type != EFX_MAE_RULE_OUTER) {
+ rc = EINVAL;
+ goto fail2;
+ }
+
+ switch (encap_type) {
+ case EFX_TUNNEL_PROTOCOL_NONE:
+ encap_type_mcdi = MAE_MCDI_ENCAP_TYPE_NONE;
+ break;
+ case EFX_TUNNEL_PROTOCOL_VXLAN:
+ encap_type_mcdi = MAE_MCDI_ENCAP_TYPE_VXLAN;
+ break;
+ case EFX_TUNNEL_PROTOCOL_GENEVE:
+ encap_type_mcdi = MAE_MCDI_ENCAP_TYPE_GENEVE;
+ break;
+ case EFX_TUNNEL_PROTOCOL_NVGRE:
+ encap_type_mcdi = MAE_MCDI_ENCAP_TYPE_NVGRE;
+ break;
+ default:
+ rc = ENOTSUP;
+ goto fail3;
+ }
+
+ req.emr_cmd = MC_CMD_MAE_OUTER_RULE_INSERT;
+ req.emr_in_buf = payload;
+ req.emr_in_length = MC_CMD_MAE_OUTER_RULE_INSERT_IN_LENMAX_MCDI2;
+ req.emr_out_buf = payload;
+ req.emr_out_length = MC_CMD_MAE_OUTER_RULE_INSERT_OUT_LEN;
+
+ MCDI_IN_SET_DWORD(req,
+ MAE_OUTER_RULE_INSERT_IN_ENCAP_TYPE, encap_type_mcdi);
+
+ MCDI_IN_SET_DWORD(req, MAE_OUTER_RULE_INSERT_IN_PRIO, spec->emms_prio);
+
+ /*
+ * Mask-value pairs have been stored in the byte order needed for the
+ * MCDI request and are thus safe to be copied directly to the buffer.
+ * The library cares about byte order in efx_mae_match_spec_field_set().
+ */
+ EFX_STATIC_ASSERT(sizeof (spec->emms_mask_value_pairs.outer) >=
+ MAE_ENC_FIELD_PAIRS_LEN);
+ offset = MC_CMD_MAE_OUTER_RULE_INSERT_IN_FIELD_MATCH_CRITERIA_OFST;
+ memcpy(payload + offset, spec->emms_mask_value_pairs.outer,
+ MAE_ENC_FIELD_PAIRS_LEN);
+
+ efx_mcdi_execute(enp, &req);
+
+ if (req.emr_rc != 0) {
+ rc = req.emr_rc;
+ goto fail4;
+ }
+
+ if (req.emr_out_length_used < MC_CMD_MAE_OUTER_RULE_INSERT_OUT_LEN) {
+ rc = EMSGSIZE;
+ goto fail5;
+ }
+
+ or_id.id = MCDI_OUT_DWORD(req, MAE_OUTER_RULE_INSERT_OUT_OR_ID);
+ if (or_id.id == EFX_MAE_RSRC_ID_INVALID) {
+ rc = ENOENT;
+ goto fail6;
+ }
+
+ or_idp->id = or_id.id;
+
+ return (0);