+enum ef10_filter_add_action_e {
+ /* Insert a new filter */
+ EF10_FILTER_ADD_NEW,
+ /*
+ * Replace old filter with a new, overriding the old one
+ * if it has lower priority.
+ */
+ EF10_FILTER_ADD_REPLACE,
+ /* Store new, lower priority filter as overridden by old filter */
+ EF10_FILTER_ADD_STORE,
+ /* Special case for AUTO filters, remove AUTO_OLD flag */
+ EF10_FILTER_ADD_REFRESH,
+};
+
+static __checkReturn efx_rc_t
+ef10_filter_add_lookup_equal_spec(
+ __in efx_filter_spec_t *spec,
+ __in efx_filter_spec_t *probe_spec,
+ __in efx_filter_replacement_policy_t policy,
+ __out boolean_t *found)
+{
+ efx_rc_t rc;
+
+ /* Refreshing AUTO filter */
+ if (spec->efs_priority == EFX_FILTER_PRI_AUTO &&
+ probe_spec->efs_priority == EFX_FILTER_PRI_AUTO) {
+ *found = B_TRUE;
+ return (0);
+ }
+
+ /*
+ * With exclusive filters, higher priority ones
+ * override lower priority ones, and lower priority
+ * ones are stored in case the higher priority one
+ * is removed.
+ */
+ if (ef10_filter_is_exclusive(spec)) {
+ switch (policy) {
+ case EFX_FILTER_REPLACEMENT_HIGHER_OR_EQUAL_PRIORITY:
+ if (spec->efs_priority == probe_spec->efs_priority) {
+ *found = B_TRUE;
+ break;
+ }
+ /* Fall-through */
+ case EFX_FILTER_REPLACEMENT_HIGHER_PRIORITY:
+ if (spec->efs_priority > probe_spec->efs_priority) {
+ *found = B_TRUE;
+ break;
+ }
+ /* Fall-through */
+ case EFX_FILTER_REPLACEMENT_NEVER:
+ /*
+ * Lower priority filter needs to be
+ * stored. It does *not* replace the
+ * old one. That is why EEXIST is not
+ * returned in that case.
+ */
+ if (spec->efs_priority < probe_spec->efs_priority) {
+ *found = B_TRUE;
+ break;
+ } else {
+ rc = EEXIST;
+ goto fail1;
+ }
+ default:
+ EFSYS_ASSERT(0);
+ rc = EEXIST;
+ goto fail2;
+ }
+ } else {
+ *found = B_FALSE;
+ }
+
+ return (0);
+
+fail2:
+ EFSYS_PROBE(fail2);
+
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+ return (rc);
+}
+
+
+static void
+ef10_filter_add_select_action(
+ __in efx_filter_spec_t *saved_spec,
+ __in efx_filter_spec_t *spec,
+ __out enum ef10_filter_add_action_e *action,
+ __out efx_filter_spec_t **overridden_spec)
+{
+ efx_filter_spec_t *overridden = NULL;
+
+ if (saved_spec == NULL) {
+ *action = EF10_FILTER_ADD_NEW;
+ } else if (ef10_filter_is_exclusive(spec) == B_FALSE) {
+ /*
+ * Non-exclusive filters are always stored in separate entries
+ * in the table. The only case involving a saved spec is
+ * refreshing an AUTO filter.
+ */
+ EFSYS_ASSERT(saved_spec->efs_overridden_spec == NULL);
+ EFSYS_ASSERT(spec->efs_priority == EFX_FILTER_PRI_AUTO);
+ EFSYS_ASSERT(saved_spec->efs_priority == EFX_FILTER_PRI_AUTO);
+ *action = EF10_FILTER_ADD_REFRESH;
+ } else {
+ /* Exclusive filters stored in the same entry */
+ if (spec->efs_priority > saved_spec->efs_priority) {
+ /*
+ * Insert a high priority filter over a lower priority
+ * one. Only two priority levels are implemented, so
+ * there must not already be an overridden filter.
+ */
+ EFX_STATIC_ASSERT(EFX_FILTER_NPRI == 2);
+ EFSYS_ASSERT(saved_spec->efs_overridden_spec == NULL);
+ overridden = saved_spec;
+ *action = EF10_FILTER_ADD_REPLACE;
+ } else if (spec->efs_priority == saved_spec->efs_priority) {
+ /* Replace in-place or refresh an existing filter */
+ if (spec->efs_priority == EFX_FILTER_PRI_AUTO)
+ *action = EF10_FILTER_ADD_REFRESH;
+ else
+ *action = EF10_FILTER_ADD_REPLACE;
+ } else {
+ /*
+ * Insert a lower priority filter, storing it in case
+ * the higher priority filter is removed.
+ *
+ * Currently there are only two priority levels, so this
+ * must be an AUTO filter.
+ */
+ EFX_STATIC_ASSERT(EFX_FILTER_NPRI == 2);
+ EFSYS_ASSERT(spec->efs_priority == EFX_FILTER_PRI_AUTO);
+ if (saved_spec->efs_overridden_spec != NULL) {
+ *action = EF10_FILTER_ADD_REFRESH;
+ } else {
+ overridden = spec;
+ *action = EF10_FILTER_ADD_STORE;
+ }
+ }
+ }
+
+ *overridden_spec = overridden;
+}
+
+static __checkReturn efx_rc_t
+ef10_filter_add_execute_action(
+ __in efx_nic_t *enp,
+ __in efx_filter_spec_t *saved_spec,
+ __in efx_filter_spec_t *spec,
+ __in efx_filter_spec_t *overridden_spec,
+ __in enum ef10_filter_add_action_e action,
+ __in int ins_index)
+{
+ ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
+ efsys_lock_state_t state;
+ efx_rc_t rc;
+
+ EFSYS_LOCK(enp->en_eslp, state);
+
+ if (action == EF10_FILTER_ADD_REFRESH) {
+ ef10_filter_set_entry_not_auto_old(eftp, ins_index);
+ goto out_unlock;
+ } else if (action == EF10_FILTER_ADD_STORE) {
+ EFSYS_ASSERT(overridden_spec != NULL);
+ saved_spec->efs_overridden_spec = overridden_spec;
+ goto out_unlock;
+ }
+
+ EFSYS_UNLOCK(enp->en_eslp, state);
+
+ switch (action) {
+ case EF10_FILTER_ADD_REPLACE:
+ /*
+ * On replacing the filter handle may change after a
+ * successful replace operation.
+ */
+ rc = efx_mcdi_filter_op_add(enp, spec,
+ MC_CMD_FILTER_OP_IN_OP_REPLACE,
+ &eftp->eft_entry[ins_index].efe_handle);
+ break;
+ case EF10_FILTER_ADD_NEW:
+ if (ef10_filter_is_exclusive(spec)) {
+ rc = efx_mcdi_filter_op_add(enp, spec,
+ MC_CMD_FILTER_OP_IN_OP_INSERT,
+ &eftp->eft_entry[ins_index].efe_handle);
+ } else {
+ rc = efx_mcdi_filter_op_add(enp, spec,
+ MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
+ &eftp->eft_entry[ins_index].efe_handle);
+ }
+ break;
+ default:
+ rc = EINVAL;
+ EFSYS_ASSERT(0);
+ break;
+ }
+ if (rc != 0)
+ goto fail1;
+
+ EFSYS_LOCK(enp->en_eslp, state);
+
+ if (action == EF10_FILTER_ADD_REPLACE) {
+ /* Update the fields that may differ */
+ saved_spec->efs_priority = spec->efs_priority;
+ saved_spec->efs_flags = spec->efs_flags;
+ saved_spec->efs_rss_context = spec->efs_rss_context;
+ saved_spec->efs_dmaq_id = spec->efs_dmaq_id;
+
+ if (overridden_spec != NULL)
+ saved_spec->efs_overridden_spec = overridden_spec;
+ }
+
+out_unlock:
+ EFSYS_UNLOCK(enp->en_eslp, state);
+
+ return (0);
+
+fail1:
+ EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+ return (rc);
+}
+