125730d8bSEdward Cree // SPDX-License-Identifier: GPL-2.0-only
225730d8bSEdward Cree /****************************************************************************
325730d8bSEdward Cree  * Driver for Solarflare network controllers and boards
425730d8bSEdward Cree  * Copyright 2022 Advanced Micro Devices, Inc.
525730d8bSEdward Cree  *
625730d8bSEdward Cree  * This program is free software; you can redistribute it and/or modify it
725730d8bSEdward Cree  * under the terms of the GNU General Public License version 2 as published
825730d8bSEdward Cree  * by the Free Software Foundation, incorporated herein by reference.
925730d8bSEdward Cree  */
1025730d8bSEdward Cree 
1125730d8bSEdward Cree #include "tc_counters.h"
129a14f2e3SEdward Cree #include "tc_encap_actions.h"
1325730d8bSEdward Cree #include "mae_counter_format.h"
1425730d8bSEdward Cree #include "mae.h"
1525730d8bSEdward Cree #include "rx_common.h"
1625730d8bSEdward Cree 
1719a0c989SEdward Cree /* Counter-management hashtables */
1819a0c989SEdward Cree 
1919a0c989SEdward Cree static const struct rhashtable_params efx_tc_counter_id_ht_params = {
2019a0c989SEdward Cree 	.key_len	= offsetof(struct efx_tc_counter_index, linkage),
2119a0c989SEdward Cree 	.key_offset	= 0,
2219a0c989SEdward Cree 	.head_offset	= offsetof(struct efx_tc_counter_index, linkage),
2319a0c989SEdward Cree };
2419a0c989SEdward Cree 
2519a0c989SEdward Cree static const struct rhashtable_params efx_tc_counter_ht_params = {
2619a0c989SEdward Cree 	.key_len	= offsetof(struct efx_tc_counter, linkage),
2719a0c989SEdward Cree 	.key_offset	= 0,
2819a0c989SEdward Cree 	.head_offset	= offsetof(struct efx_tc_counter, linkage),
2919a0c989SEdward Cree };
3019a0c989SEdward Cree 
efx_tc_counter_free(void * ptr,void * __unused)3119a0c989SEdward Cree static void efx_tc_counter_free(void *ptr, void *__unused)
3219a0c989SEdward Cree {
3319a0c989SEdward Cree 	struct efx_tc_counter *cnt = ptr;
3419a0c989SEdward Cree 
359a14f2e3SEdward Cree 	WARN_ON(!list_empty(&cnt->users));
369a14f2e3SEdward Cree 	/* We'd like to synchronize_rcu() here, but unfortunately we aren't
379a14f2e3SEdward Cree 	 * removing the element from the hashtable (it's not clear that's a
389a14f2e3SEdward Cree 	 * safe thing to do in an rhashtable_free_and_destroy free_fn), so
399a14f2e3SEdward Cree 	 * threads could still be obtaining new pointers to *cnt if they can
409a14f2e3SEdward Cree 	 * race against this function at all.
419a14f2e3SEdward Cree 	 */
429a14f2e3SEdward Cree 	flush_work(&cnt->work);
439a14f2e3SEdward Cree 	EFX_WARN_ON_PARANOID(spin_is_locked(&cnt->lock));
4419a0c989SEdward Cree 	kfree(cnt);
4519a0c989SEdward Cree }
4619a0c989SEdward Cree 
efx_tc_counter_id_free(void * ptr,void * __unused)4719a0c989SEdward Cree static void efx_tc_counter_id_free(void *ptr, void *__unused)
4819a0c989SEdward Cree {
4919a0c989SEdward Cree 	struct efx_tc_counter_index *ctr = ptr;
5019a0c989SEdward Cree 
5119a0c989SEdward Cree 	WARN_ON(refcount_read(&ctr->ref));
5219a0c989SEdward Cree 	kfree(ctr);
5319a0c989SEdward Cree }
5419a0c989SEdward Cree 
efx_tc_init_counters(struct efx_nic * efx)5519a0c989SEdward Cree int efx_tc_init_counters(struct efx_nic *efx)
5619a0c989SEdward Cree {
5719a0c989SEdward Cree 	int rc;
5819a0c989SEdward Cree 
5919a0c989SEdward Cree 	rc = rhashtable_init(&efx->tc->counter_id_ht, &efx_tc_counter_id_ht_params);
6019a0c989SEdward Cree 	if (rc < 0)
6119a0c989SEdward Cree 		goto fail_counter_id_ht;
6219a0c989SEdward Cree 	rc = rhashtable_init(&efx->tc->counter_ht, &efx_tc_counter_ht_params);
6319a0c989SEdward Cree 	if (rc < 0)
6419a0c989SEdward Cree 		goto fail_counter_ht;
6519a0c989SEdward Cree 	return 0;
6619a0c989SEdward Cree fail_counter_ht:
6719a0c989SEdward Cree 	rhashtable_destroy(&efx->tc->counter_id_ht);
6819a0c989SEdward Cree fail_counter_id_ht:
6919a0c989SEdward Cree 	return rc;
7019a0c989SEdward Cree }
7119a0c989SEdward Cree 
7219a0c989SEdward Cree /* Only call this in init failure teardown.
7319a0c989SEdward Cree  * Normal exit should fini instead as there may be entries in the table.
7419a0c989SEdward Cree  */
efx_tc_destroy_counters(struct efx_nic * efx)7519a0c989SEdward Cree void efx_tc_destroy_counters(struct efx_nic *efx)
7619a0c989SEdward Cree {
7719a0c989SEdward Cree 	rhashtable_destroy(&efx->tc->counter_ht);
7819a0c989SEdward Cree 	rhashtable_destroy(&efx->tc->counter_id_ht);
7919a0c989SEdward Cree }
8019a0c989SEdward Cree 
efx_tc_fini_counters(struct efx_nic * efx)8119a0c989SEdward Cree void efx_tc_fini_counters(struct efx_nic *efx)
8219a0c989SEdward Cree {
8319a0c989SEdward Cree 	rhashtable_free_and_destroy(&efx->tc->counter_id_ht, efx_tc_counter_id_free, NULL);
8419a0c989SEdward Cree 	rhashtable_free_and_destroy(&efx->tc->counter_ht, efx_tc_counter_free, NULL);
8519a0c989SEdward Cree }
8619a0c989SEdward Cree 
efx_tc_counter_work(struct work_struct * work)879a14f2e3SEdward Cree static void efx_tc_counter_work(struct work_struct *work)
889a14f2e3SEdward Cree {
899a14f2e3SEdward Cree 	struct efx_tc_counter *cnt = container_of(work, struct efx_tc_counter, work);
909a14f2e3SEdward Cree 	struct efx_tc_encap_action *encap;
919a14f2e3SEdward Cree 	struct efx_tc_action_set *act;
929a14f2e3SEdward Cree 	unsigned long touched;
939a14f2e3SEdward Cree 	struct neighbour *n;
949a14f2e3SEdward Cree 
959a14f2e3SEdward Cree 	spin_lock_bh(&cnt->lock);
969a14f2e3SEdward Cree 	touched = READ_ONCE(cnt->touched);
979a14f2e3SEdward Cree 
989a14f2e3SEdward Cree 	list_for_each_entry(act, &cnt->users, count_user) {
999a14f2e3SEdward Cree 		encap = act->encap_md;
1009a14f2e3SEdward Cree 		if (!encap)
1019a14f2e3SEdward Cree 			continue;
1029a14f2e3SEdward Cree 		if (!encap->neigh) /* can't happen */
1039a14f2e3SEdward Cree 			continue;
1049a14f2e3SEdward Cree 		if (time_after_eq(encap->neigh->used, touched))
1059a14f2e3SEdward Cree 			continue;
1069a14f2e3SEdward Cree 		encap->neigh->used = touched;
1079a14f2e3SEdward Cree 		/* We have passed traffic using this ARP entry, so
1089a14f2e3SEdward Cree 		 * indicate to the ARP cache that it's still active
1099a14f2e3SEdward Cree 		 */
1109a14f2e3SEdward Cree 		if (encap->neigh->dst_ip)
1119a14f2e3SEdward Cree 			n = neigh_lookup(&arp_tbl, &encap->neigh->dst_ip,
1129a14f2e3SEdward Cree 					 encap->neigh->egdev);
1139a14f2e3SEdward Cree 		else
1149a14f2e3SEdward Cree #if IS_ENABLED(CONFIG_IPV6)
1159a14f2e3SEdward Cree 			n = neigh_lookup(ipv6_stub->nd_tbl,
1169a14f2e3SEdward Cree 					 &encap->neigh->dst_ip6,
1179a14f2e3SEdward Cree 					 encap->neigh->egdev);
1189a14f2e3SEdward Cree #else
1199a14f2e3SEdward Cree 			n = NULL;
1209a14f2e3SEdward Cree #endif
1219a14f2e3SEdward Cree 		if (!n)
1229a14f2e3SEdward Cree 			continue;
1239a14f2e3SEdward Cree 
1249a14f2e3SEdward Cree 		neigh_event_send(n, NULL);
1259a14f2e3SEdward Cree 		neigh_release(n);
1269a14f2e3SEdward Cree 	}
1279a14f2e3SEdward Cree 	spin_unlock_bh(&cnt->lock);
1289a14f2e3SEdward Cree }
1299a14f2e3SEdward Cree 
1300363aa29SEdward Cree /* Counter allocation */
1310363aa29SEdward Cree 
efx_tc_flower_allocate_counter(struct efx_nic * efx,int type)1321909387fSEdward Cree struct efx_tc_counter *efx_tc_flower_allocate_counter(struct efx_nic *efx,
1330363aa29SEdward Cree 						      int type)
1340363aa29SEdward Cree {
1350363aa29SEdward Cree 	struct efx_tc_counter *cnt;
1360363aa29SEdward Cree 	int rc, rc2;
1370363aa29SEdward Cree 
1380363aa29SEdward Cree 	cnt = kzalloc(sizeof(*cnt), GFP_USER);
1390363aa29SEdward Cree 	if (!cnt)
1400363aa29SEdward Cree 		return ERR_PTR(-ENOMEM);
1410363aa29SEdward Cree 
142c4bad432SEdward Cree 	spin_lock_init(&cnt->lock);
1439a14f2e3SEdward Cree 	INIT_WORK(&cnt->work, efx_tc_counter_work);
144c4bad432SEdward Cree 	cnt->touched = jiffies;
1450363aa29SEdward Cree 	cnt->type = type;
1460363aa29SEdward Cree 
1470363aa29SEdward Cree 	rc = efx_mae_allocate_counter(efx, cnt);
1480363aa29SEdward Cree 	if (rc)
1490363aa29SEdward Cree 		goto fail1;
1509a14f2e3SEdward Cree 	INIT_LIST_HEAD(&cnt->users);
1510363aa29SEdward Cree 	rc = rhashtable_insert_fast(&efx->tc->counter_ht, &cnt->linkage,
1520363aa29SEdward Cree 				    efx_tc_counter_ht_params);
1530363aa29SEdward Cree 	if (rc)
1540363aa29SEdward Cree 		goto fail2;
1550363aa29SEdward Cree 	return cnt;
1560363aa29SEdward Cree fail2:
1570363aa29SEdward Cree 	/* If we get here, it implies that we couldn't insert into the table,
1580363aa29SEdward Cree 	 * which in turn probably means that the fw_id was already taken.
1590363aa29SEdward Cree 	 * In that case, it's unclear whether we really 'own' the fw_id; but
1600363aa29SEdward Cree 	 * the firmware seemed to think we did, so it's proper to free it.
1610363aa29SEdward Cree 	 */
1620363aa29SEdward Cree 	rc2 = efx_mae_free_counter(efx, cnt);
1630363aa29SEdward Cree 	if (rc2)
1640363aa29SEdward Cree 		netif_warn(efx, hw, efx->net_dev,
1650363aa29SEdward Cree 			   "Failed to free MAE counter %u, rc %d\n",
1660363aa29SEdward Cree 			   cnt->fw_id, rc2);
1670363aa29SEdward Cree fail1:
1680363aa29SEdward Cree 	kfree(cnt);
1690363aa29SEdward Cree 	return ERR_PTR(rc > 0 ? -EIO : rc);
1700363aa29SEdward Cree }
1710363aa29SEdward Cree 
efx_tc_flower_release_counter(struct efx_nic * efx,struct efx_tc_counter * cnt)1721909387fSEdward Cree void efx_tc_flower_release_counter(struct efx_nic *efx,
1730363aa29SEdward Cree 				   struct efx_tc_counter *cnt)
1740363aa29SEdward Cree {
1750363aa29SEdward Cree 	int rc;
1760363aa29SEdward Cree 
1770363aa29SEdward Cree 	rhashtable_remove_fast(&efx->tc->counter_ht, &cnt->linkage,
1780363aa29SEdward Cree 			       efx_tc_counter_ht_params);
1790363aa29SEdward Cree 	rc = efx_mae_free_counter(efx, cnt);
1800363aa29SEdward Cree 	if (rc)
1810363aa29SEdward Cree 		netif_warn(efx, hw, efx->net_dev,
1820363aa29SEdward Cree 			   "Failed to free MAE counter %u, rc %d\n",
1830363aa29SEdward Cree 			   cnt->fw_id, rc);
1849a14f2e3SEdward Cree 	WARN_ON(!list_empty(&cnt->users));
1850363aa29SEdward Cree 	/* This doesn't protect counter updates coming in arbitrarily long
1860363aa29SEdward Cree 	 * after we deleted the counter.  The RCU just ensures that we won't
1870363aa29SEdward Cree 	 * free the counter while another thread has a pointer to it.
1880363aa29SEdward Cree 	 * Ensuring we don't update the wrong counter if the ID gets re-used
1890363aa29SEdward Cree 	 * is handled by the generation count.
1900363aa29SEdward Cree 	 */
1910363aa29SEdward Cree 	synchronize_rcu();
1929a14f2e3SEdward Cree 	flush_work(&cnt->work);
193c4bad432SEdward Cree 	EFX_WARN_ON_PARANOID(spin_is_locked(&cnt->lock));
1940363aa29SEdward Cree 	kfree(cnt);
1950363aa29SEdward Cree }
1960363aa29SEdward Cree 
efx_tc_flower_find_counter_by_fw_id(struct efx_nic * efx,int type,u32 fw_id)197c4bad432SEdward Cree static struct efx_tc_counter *efx_tc_flower_find_counter_by_fw_id(
198c4bad432SEdward Cree 				struct efx_nic *efx, int type, u32 fw_id)
199c4bad432SEdward Cree {
200c4bad432SEdward Cree 	struct efx_tc_counter key = {};
201c4bad432SEdward Cree 
202c4bad432SEdward Cree 	key.fw_id = fw_id;
203c4bad432SEdward Cree 	key.type = type;
204c4bad432SEdward Cree 
205c4bad432SEdward Cree 	return rhashtable_lookup_fast(&efx->tc->counter_ht, &key,
206c4bad432SEdward Cree 				      efx_tc_counter_ht_params);
207c4bad432SEdward Cree }
208c4bad432SEdward Cree 
2090363aa29SEdward Cree /* TC cookie to counter mapping */
2100363aa29SEdward Cree 
efx_tc_flower_put_counter_index(struct efx_nic * efx,struct efx_tc_counter_index * ctr)2110363aa29SEdward Cree void efx_tc_flower_put_counter_index(struct efx_nic *efx,
2120363aa29SEdward Cree 				     struct efx_tc_counter_index *ctr)
2130363aa29SEdward Cree {
2140363aa29SEdward Cree 	if (!refcount_dec_and_test(&ctr->ref))
2150363aa29SEdward Cree 		return; /* still in use */
2160363aa29SEdward Cree 	rhashtable_remove_fast(&efx->tc->counter_id_ht, &ctr->linkage,
2170363aa29SEdward Cree 			       efx_tc_counter_id_ht_params);
2180363aa29SEdward Cree 	efx_tc_flower_release_counter(efx, ctr->cnt);
2190363aa29SEdward Cree 	kfree(ctr);
2200363aa29SEdward Cree }
2210363aa29SEdward Cree 
efx_tc_flower_get_counter_index(struct efx_nic * efx,unsigned long cookie,enum efx_tc_counter_type type)2220363aa29SEdward Cree struct efx_tc_counter_index *efx_tc_flower_get_counter_index(
2230363aa29SEdward Cree 				struct efx_nic *efx, unsigned long cookie,
2240363aa29SEdward Cree 				enum efx_tc_counter_type type)
2250363aa29SEdward Cree {
2260363aa29SEdward Cree 	struct efx_tc_counter_index *ctr, *old;
2270363aa29SEdward Cree 	struct efx_tc_counter *cnt;
2280363aa29SEdward Cree 
2290363aa29SEdward Cree 	ctr = kzalloc(sizeof(*ctr), GFP_USER);
2300363aa29SEdward Cree 	if (!ctr)
2310363aa29SEdward Cree 		return ERR_PTR(-ENOMEM);
2320363aa29SEdward Cree 	ctr->cookie = cookie;
2330363aa29SEdward Cree 	old = rhashtable_lookup_get_insert_fast(&efx->tc->counter_id_ht,
2340363aa29SEdward Cree 						&ctr->linkage,
2350363aa29SEdward Cree 						efx_tc_counter_id_ht_params);
2360363aa29SEdward Cree 	if (old) {
2370363aa29SEdward Cree 		/* don't need our new entry */
2380363aa29SEdward Cree 		kfree(ctr);
239*fc21f083SEdward Cree 		if (IS_ERR(old)) /* oh dear, it's actually an error */
240*fc21f083SEdward Cree 			return ERR_CAST(old);
2410363aa29SEdward Cree 		if (!refcount_inc_not_zero(&old->ref))
2420363aa29SEdward Cree 			return ERR_PTR(-EAGAIN);
2430363aa29SEdward Cree 		/* existing entry found */
2440363aa29SEdward Cree 		ctr = old;
2450363aa29SEdward Cree 	} else {
2460363aa29SEdward Cree 		cnt = efx_tc_flower_allocate_counter(efx, type);
2470363aa29SEdward Cree 		if (IS_ERR(cnt)) {
2480363aa29SEdward Cree 			rhashtable_remove_fast(&efx->tc->counter_id_ht,
2490363aa29SEdward Cree 					       &ctr->linkage,
2500363aa29SEdward Cree 					       efx_tc_counter_id_ht_params);
2510363aa29SEdward Cree 			kfree(ctr);
2520363aa29SEdward Cree 			return (void *)cnt; /* it's an ERR_PTR */
2530363aa29SEdward Cree 		}
2540363aa29SEdward Cree 		ctr->cnt = cnt;
2550363aa29SEdward Cree 		refcount_set(&ctr->ref, 1);
2560363aa29SEdward Cree 	}
2570363aa29SEdward Cree 	return ctr;
2580363aa29SEdward Cree }
2590363aa29SEdward Cree 
efx_tc_flower_find_counter_index(struct efx_nic * efx,unsigned long cookie)26050f8f2f7SEdward Cree struct efx_tc_counter_index *efx_tc_flower_find_counter_index(
26150f8f2f7SEdward Cree 				struct efx_nic *efx, unsigned long cookie)
26250f8f2f7SEdward Cree {
26350f8f2f7SEdward Cree 	struct efx_tc_counter_index key = {};
26450f8f2f7SEdward Cree 
26550f8f2f7SEdward Cree 	key.cookie = cookie;
26650f8f2f7SEdward Cree 	return rhashtable_lookup_fast(&efx->tc->counter_id_ht, &key,
26750f8f2f7SEdward Cree 				      efx_tc_counter_id_ht_params);
26850f8f2f7SEdward Cree }
26950f8f2f7SEdward Cree 
27025730d8bSEdward Cree /* TC Channel.  Counter updates are delivered on this channel's RXQ. */
27125730d8bSEdward Cree 
efx_tc_handle_no_channel(struct efx_nic * efx)27225730d8bSEdward Cree static void efx_tc_handle_no_channel(struct efx_nic *efx)
27325730d8bSEdward Cree {
27425730d8bSEdward Cree 	netif_warn(efx, drv, efx->net_dev,
27525730d8bSEdward Cree 		   "MAE counters require MSI-X and 1 additional interrupt vector.\n");
27625730d8bSEdward Cree }
27725730d8bSEdward Cree 
efx_tc_probe_channel(struct efx_channel * channel)27825730d8bSEdward Cree static int efx_tc_probe_channel(struct efx_channel *channel)
27925730d8bSEdward Cree {
28025730d8bSEdward Cree 	struct efx_rx_queue *rx_queue = &channel->rx_queue;
28125730d8bSEdward Cree 
28225730d8bSEdward Cree 	channel->irq_moderation_us = 0;
28325730d8bSEdward Cree 	rx_queue->core_index = 0;
28425730d8bSEdward Cree 
28525730d8bSEdward Cree 	INIT_WORK(&rx_queue->grant_work, efx_mae_counters_grant_credits);
28625730d8bSEdward Cree 
28725730d8bSEdward Cree 	return 0;
28825730d8bSEdward Cree }
28925730d8bSEdward Cree 
efx_tc_start_channel(struct efx_channel * channel)29025730d8bSEdward Cree static int efx_tc_start_channel(struct efx_channel *channel)
29125730d8bSEdward Cree {
29225730d8bSEdward Cree 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
29325730d8bSEdward Cree 	struct efx_nic *efx = channel->efx;
29425730d8bSEdward Cree 
29525730d8bSEdward Cree 	return efx_mae_start_counters(efx, rx_queue);
29625730d8bSEdward Cree }
29725730d8bSEdward Cree 
efx_tc_stop_channel(struct efx_channel * channel)29825730d8bSEdward Cree static void efx_tc_stop_channel(struct efx_channel *channel)
29925730d8bSEdward Cree {
30025730d8bSEdward Cree 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
30125730d8bSEdward Cree 	struct efx_nic *efx = channel->efx;
30225730d8bSEdward Cree 	int rc;
30325730d8bSEdward Cree 
30425730d8bSEdward Cree 	rc = efx_mae_stop_counters(efx, rx_queue);
30525730d8bSEdward Cree 	if (rc)
30625730d8bSEdward Cree 		netif_warn(efx, drv, efx->net_dev,
30725730d8bSEdward Cree 			   "Failed to stop MAE counters streaming, rc=%d.\n",
30825730d8bSEdward Cree 			   rc);
30925730d8bSEdward Cree 	rx_queue->grant_credits = false;
31025730d8bSEdward Cree 	flush_work(&rx_queue->grant_work);
31125730d8bSEdward Cree }
31225730d8bSEdward Cree 
efx_tc_remove_channel(struct efx_channel * channel)31325730d8bSEdward Cree static void efx_tc_remove_channel(struct efx_channel *channel)
31425730d8bSEdward Cree {
31525730d8bSEdward Cree }
31625730d8bSEdward Cree 
efx_tc_get_channel_name(struct efx_channel * channel,char * buf,size_t len)31725730d8bSEdward Cree static void efx_tc_get_channel_name(struct efx_channel *channel,
31825730d8bSEdward Cree 				    char *buf, size_t len)
31925730d8bSEdward Cree {
32025730d8bSEdward Cree 	snprintf(buf, len, "%s-mae", channel->efx->name);
32125730d8bSEdward Cree }
32225730d8bSEdward Cree 
efx_tc_counter_update(struct efx_nic * efx,enum efx_tc_counter_type counter_type,u32 counter_idx,u64 packets,u64 bytes,u32 mark)32325730d8bSEdward Cree static void efx_tc_counter_update(struct efx_nic *efx,
32425730d8bSEdward Cree 				  enum efx_tc_counter_type counter_type,
32525730d8bSEdward Cree 				  u32 counter_idx, u64 packets, u64 bytes,
32625730d8bSEdward Cree 				  u32 mark)
32725730d8bSEdward Cree {
328c4bad432SEdward Cree 	struct efx_tc_counter *cnt;
329c4bad432SEdward Cree 
330c4bad432SEdward Cree 	rcu_read_lock(); /* Protect against deletion of 'cnt' */
331c4bad432SEdward Cree 	cnt = efx_tc_flower_find_counter_by_fw_id(efx, counter_type, counter_idx);
332c4bad432SEdward Cree 	if (!cnt) {
333c4bad432SEdward Cree 		/* This can legitimately happen when a counter is removed,
334c4bad432SEdward Cree 		 * with updates for the counter still in-flight; however this
335c4bad432SEdward Cree 		 * should be an infrequent occurrence.
336c4bad432SEdward Cree 		 */
337c4bad432SEdward Cree 		if (net_ratelimit())
338c4bad432SEdward Cree 			netif_dbg(efx, drv, efx->net_dev,
339c4bad432SEdward Cree 				  "Got update for unwanted MAE counter %u type %u\n",
340c4bad432SEdward Cree 				  counter_idx, counter_type);
341c4bad432SEdward Cree 		goto out;
342c4bad432SEdward Cree 	}
343c4bad432SEdward Cree 
344c4bad432SEdward Cree 	spin_lock_bh(&cnt->lock);
345c4bad432SEdward Cree 	if ((s32)mark - (s32)cnt->gen < 0) {
346c4bad432SEdward Cree 		/* This counter update packet is from before the counter was
347c4bad432SEdward Cree 		 * allocated; thus it must be for a previous counter with
348c4bad432SEdward Cree 		 * the same ID that has since been freed, and it should be
349c4bad432SEdward Cree 		 * ignored.
350c4bad432SEdward Cree 		 */
351c4bad432SEdward Cree 	} else {
352c4bad432SEdward Cree 		/* Update latest seen generation count.  This ensures that
353c4bad432SEdward Cree 		 * even a long-lived counter won't start getting ignored if
354c4bad432SEdward Cree 		 * the generation count wraps around, unless it somehow
355c4bad432SEdward Cree 		 * manages to go 1<<31 generations without an update.
356c4bad432SEdward Cree 		 */
357c4bad432SEdward Cree 		cnt->gen = mark;
358c4bad432SEdward Cree 		/* update counter values */
359c4bad432SEdward Cree 		cnt->packets += packets;
360c4bad432SEdward Cree 		cnt->bytes += bytes;
361c4bad432SEdward Cree 		cnt->touched = jiffies;
362c4bad432SEdward Cree 	}
363c4bad432SEdward Cree 	spin_unlock_bh(&cnt->lock);
3649a14f2e3SEdward Cree 	schedule_work(&cnt->work);
365c4bad432SEdward Cree out:
366c4bad432SEdward Cree 	rcu_read_unlock();
36725730d8bSEdward Cree }
36825730d8bSEdward Cree 
efx_tc_rx_version_1(struct efx_nic * efx,const u8 * data,u32 mark)36925730d8bSEdward Cree static void efx_tc_rx_version_1(struct efx_nic *efx, const u8 *data, u32 mark)
37025730d8bSEdward Cree {
37125730d8bSEdward Cree 	u16 n_counters, i;
37225730d8bSEdward Cree 
37325730d8bSEdward Cree 	/* Header format:
37425730d8bSEdward Cree 	 * + |   0    |   1    |   2    |   3    |
37525730d8bSEdward Cree 	 * 0 |version |         reserved         |
37625730d8bSEdward Cree 	 * 4 |    seq_index    |   n_counters    |
37725730d8bSEdward Cree 	 */
37825730d8bSEdward Cree 
37925730d8bSEdward Cree 	n_counters = le16_to_cpu(*(const __le16 *)(data + 6));
38025730d8bSEdward Cree 
38125730d8bSEdward Cree 	/* Counter update entry format:
38225730d8bSEdward Cree 	 * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e | f |
38325730d8bSEdward Cree 	 * |  counter_idx  |     packet_count      |      byte_count       |
38425730d8bSEdward Cree 	 */
38525730d8bSEdward Cree 	for (i = 0; i < n_counters; i++) {
38625730d8bSEdward Cree 		const void *entry = data + 8 + 16 * i;
38725730d8bSEdward Cree 		u64 packet_count, byte_count;
38825730d8bSEdward Cree 		u32 counter_idx;
38925730d8bSEdward Cree 
39025730d8bSEdward Cree 		counter_idx = le32_to_cpu(*(const __le32 *)entry);
39125730d8bSEdward Cree 		packet_count = le32_to_cpu(*(const __le32 *)(entry + 4)) |
39225730d8bSEdward Cree 			       ((u64)le16_to_cpu(*(const __le16 *)(entry + 8)) << 32);
39325730d8bSEdward Cree 		byte_count = le16_to_cpu(*(const __le16 *)(entry + 10)) |
39425730d8bSEdward Cree 			     ((u64)le32_to_cpu(*(const __le32 *)(entry + 12)) << 16);
39525730d8bSEdward Cree 		efx_tc_counter_update(efx, EFX_TC_COUNTER_TYPE_AR, counter_idx,
39625730d8bSEdward Cree 				      packet_count, byte_count, mark);
39725730d8bSEdward Cree 	}
39825730d8bSEdward Cree }
39925730d8bSEdward Cree 
40025730d8bSEdward Cree #define TCV2_HDR_PTR(pkt, field)						\
40125730d8bSEdward Cree 	((void)BUILD_BUG_ON_ZERO(ERF_SC_PACKETISER_HEADER_##field##_LBN & 7),	\
40225730d8bSEdward Cree 	 (pkt) + ERF_SC_PACKETISER_HEADER_##field##_LBN / 8)
40325730d8bSEdward Cree #define TCV2_HDR_BYTE(pkt, field)						\
40425730d8bSEdward Cree 	((void)BUILD_BUG_ON_ZERO(ERF_SC_PACKETISER_HEADER_##field##_WIDTH != 8),\
40525730d8bSEdward Cree 	 *TCV2_HDR_PTR(pkt, field))
40625730d8bSEdward Cree #define TCV2_HDR_WORD(pkt, field)						\
40725730d8bSEdward Cree 	((void)BUILD_BUG_ON_ZERO(ERF_SC_PACKETISER_HEADER_##field##_WIDTH != 16),\
40825730d8bSEdward Cree 	 (void)BUILD_BUG_ON_ZERO(ERF_SC_PACKETISER_HEADER_##field##_LBN & 15),	\
40925730d8bSEdward Cree 	 *(__force const __le16 *)TCV2_HDR_PTR(pkt, field))
41025730d8bSEdward Cree #define TCV2_PKT_PTR(pkt, poff, i, field)					\
41125730d8bSEdward Cree 	((void)BUILD_BUG_ON_ZERO(ERF_SC_PACKETISER_PAYLOAD_##field##_LBN & 7),	\
41225730d8bSEdward Cree 	 (pkt) + ERF_SC_PACKETISER_PAYLOAD_##field##_LBN/8 + poff +		\
41325730d8bSEdward Cree 	 i * ER_RX_SL_PACKETISER_PAYLOAD_WORD_SIZE)
41425730d8bSEdward Cree 
41525730d8bSEdward Cree /* Read a little-endian 48-bit field with 16-bit alignment */
efx_tc_read48(const __le16 * field)41625730d8bSEdward Cree static u64 efx_tc_read48(const __le16 *field)
41725730d8bSEdward Cree {
41825730d8bSEdward Cree 	u64 out = 0;
41925730d8bSEdward Cree 	int i;
42025730d8bSEdward Cree 
42125730d8bSEdward Cree 	for (i = 0; i < 3; i++)
42225730d8bSEdward Cree 		out |= (u64)le16_to_cpu(field[i]) << (i * 16);
42325730d8bSEdward Cree 	return out;
42425730d8bSEdward Cree }
42525730d8bSEdward Cree 
efx_tc_rx_version_2(struct efx_nic * efx,const u8 * data,u32 mark)42625730d8bSEdward Cree static enum efx_tc_counter_type efx_tc_rx_version_2(struct efx_nic *efx,
42725730d8bSEdward Cree 						    const u8 *data, u32 mark)
42825730d8bSEdward Cree {
42925730d8bSEdward Cree 	u8 payload_offset, header_offset, ident;
43025730d8bSEdward Cree 	enum efx_tc_counter_type type;
43125730d8bSEdward Cree 	u16 n_counters, i;
43225730d8bSEdward Cree 
43325730d8bSEdward Cree 	ident = TCV2_HDR_BYTE(data, IDENTIFIER);
43425730d8bSEdward Cree 	switch (ident) {
43525730d8bSEdward Cree 	case ERF_SC_PACKETISER_HEADER_IDENTIFIER_AR:
43625730d8bSEdward Cree 		type = EFX_TC_COUNTER_TYPE_AR;
43725730d8bSEdward Cree 		break;
43825730d8bSEdward Cree 	case ERF_SC_PACKETISER_HEADER_IDENTIFIER_CT:
43925730d8bSEdward Cree 		type = EFX_TC_COUNTER_TYPE_CT;
44025730d8bSEdward Cree 		break;
44125730d8bSEdward Cree 	case ERF_SC_PACKETISER_HEADER_IDENTIFIER_OR:
44225730d8bSEdward Cree 		type = EFX_TC_COUNTER_TYPE_OR;
44325730d8bSEdward Cree 		break;
44425730d8bSEdward Cree 	default:
44525730d8bSEdward Cree 		if (net_ratelimit())
44625730d8bSEdward Cree 			netif_err(efx, drv, efx->net_dev,
44725730d8bSEdward Cree 				  "ignored v2 MAE counter packet (bad identifier %u"
44825730d8bSEdward Cree 				  "), counters may be inaccurate\n", ident);
44925730d8bSEdward Cree 		return EFX_TC_COUNTER_TYPE_MAX;
45025730d8bSEdward Cree 	}
45125730d8bSEdward Cree 	header_offset = TCV2_HDR_BYTE(data, HEADER_OFFSET);
45225730d8bSEdward Cree 	/* mae_counter_format.h implies that this offset is fixed, since it
45325730d8bSEdward Cree 	 * carries on with SOP-based LBNs for the fields in this header
45425730d8bSEdward Cree 	 */
45525730d8bSEdward Cree 	if (header_offset != ERF_SC_PACKETISER_HEADER_HEADER_OFFSET_DEFAULT) {
45625730d8bSEdward Cree 		if (net_ratelimit())
45725730d8bSEdward Cree 			netif_err(efx, drv, efx->net_dev,
45825730d8bSEdward Cree 				  "choked on v2 MAE counter packet (bad header_offset %u"
45925730d8bSEdward Cree 				  "), counters may be inaccurate\n", header_offset);
46025730d8bSEdward Cree 		return EFX_TC_COUNTER_TYPE_MAX;
46125730d8bSEdward Cree 	}
46225730d8bSEdward Cree 	payload_offset = TCV2_HDR_BYTE(data, PAYLOAD_OFFSET);
46325730d8bSEdward Cree 	n_counters = le16_to_cpu(TCV2_HDR_WORD(data, COUNT));
46425730d8bSEdward Cree 
46525730d8bSEdward Cree 	for (i = 0; i < n_counters; i++) {
46625730d8bSEdward Cree 		const void *counter_idx_p, *packet_count_p, *byte_count_p;
46725730d8bSEdward Cree 		u64 packet_count, byte_count;
46825730d8bSEdward Cree 		u32 counter_idx;
46925730d8bSEdward Cree 
47025730d8bSEdward Cree 		/* 24-bit field with 32-bit alignment */
47125730d8bSEdward Cree 		counter_idx_p = TCV2_PKT_PTR(data, payload_offset, i, COUNTER_INDEX);
47225730d8bSEdward Cree 		BUILD_BUG_ON(ERF_SC_PACKETISER_PAYLOAD_COUNTER_INDEX_WIDTH != 24);
47325730d8bSEdward Cree 		BUILD_BUG_ON(ERF_SC_PACKETISER_PAYLOAD_COUNTER_INDEX_LBN & 31);
47425730d8bSEdward Cree 		counter_idx = le32_to_cpu(*(const __le32 *)counter_idx_p) & 0xffffff;
47525730d8bSEdward Cree 		/* 48-bit field with 16-bit alignment */
47625730d8bSEdward Cree 		packet_count_p = TCV2_PKT_PTR(data, payload_offset, i, PACKET_COUNT);
47725730d8bSEdward Cree 		BUILD_BUG_ON(ERF_SC_PACKETISER_PAYLOAD_PACKET_COUNT_WIDTH != 48);
47825730d8bSEdward Cree 		BUILD_BUG_ON(ERF_SC_PACKETISER_PAYLOAD_PACKET_COUNT_LBN & 15);
47925730d8bSEdward Cree 		packet_count = efx_tc_read48((const __le16 *)packet_count_p);
48025730d8bSEdward Cree 		/* 48-bit field with 16-bit alignment */
48125730d8bSEdward Cree 		byte_count_p = TCV2_PKT_PTR(data, payload_offset, i, BYTE_COUNT);
48225730d8bSEdward Cree 		BUILD_BUG_ON(ERF_SC_PACKETISER_PAYLOAD_BYTE_COUNT_WIDTH != 48);
48325730d8bSEdward Cree 		BUILD_BUG_ON(ERF_SC_PACKETISER_PAYLOAD_BYTE_COUNT_LBN & 15);
48425730d8bSEdward Cree 		byte_count = efx_tc_read48((const __le16 *)byte_count_p);
48525730d8bSEdward Cree 
48625730d8bSEdward Cree 		if (type == EFX_TC_COUNTER_TYPE_CT) {
48725730d8bSEdward Cree 			/* CT counters are 1-bit saturating counters to update
48825730d8bSEdward Cree 			 * the lastuse time in CT stats. A received CT counter
48925730d8bSEdward Cree 			 * should have packet counter to 0 and only LSB bit on
49025730d8bSEdward Cree 			 * in byte counter.
49125730d8bSEdward Cree 			 */
49225730d8bSEdward Cree 			if (packet_count || byte_count != 1)
49325730d8bSEdward Cree 				netdev_warn_once(efx->net_dev,
49425730d8bSEdward Cree 						 "CT counter with inconsistent state (%llu, %llu)\n",
49525730d8bSEdward Cree 						 packet_count, byte_count);
49625730d8bSEdward Cree 			/* Do not increment the driver's byte counter */
49725730d8bSEdward Cree 			byte_count = 0;
49825730d8bSEdward Cree 		}
49925730d8bSEdward Cree 
50025730d8bSEdward Cree 		efx_tc_counter_update(efx, type, counter_idx, packet_count,
50125730d8bSEdward Cree 				      byte_count, mark);
50225730d8bSEdward Cree 	}
50325730d8bSEdward Cree 	return type;
50425730d8bSEdward Cree }
50525730d8bSEdward Cree 
50625730d8bSEdward Cree /* We always swallow the packet, whether successful or not, since it's not
50725730d8bSEdward Cree  * a network packet and shouldn't ever be forwarded to the stack.
50825730d8bSEdward Cree  * @mark is the generation count for counter allocations.
50925730d8bSEdward Cree  */
efx_tc_rx(struct efx_rx_queue * rx_queue,u32 mark)51025730d8bSEdward Cree static bool efx_tc_rx(struct efx_rx_queue *rx_queue, u32 mark)
51125730d8bSEdward Cree {
51225730d8bSEdward Cree 	struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
51325730d8bSEdward Cree 	struct efx_rx_buffer *rx_buf = efx_rx_buffer(rx_queue,
51425730d8bSEdward Cree 						     channel->rx_pkt_index);
51525730d8bSEdward Cree 	const u8 *data = efx_rx_buf_va(rx_buf);
51625730d8bSEdward Cree 	struct efx_nic *efx = rx_queue->efx;
51725730d8bSEdward Cree 	enum efx_tc_counter_type type;
51825730d8bSEdward Cree 	u8 version;
51925730d8bSEdward Cree 
52025730d8bSEdward Cree 	/* version is always first byte of packet */
52125730d8bSEdward Cree 	version = *data;
52225730d8bSEdward Cree 	switch (version) {
52325730d8bSEdward Cree 	case 1:
52425730d8bSEdward Cree 		type = EFX_TC_COUNTER_TYPE_AR;
52525730d8bSEdward Cree 		efx_tc_rx_version_1(efx, data, mark);
52625730d8bSEdward Cree 		break;
52725730d8bSEdward Cree 	case ERF_SC_PACKETISER_HEADER_VERSION_VALUE: // 2
52825730d8bSEdward Cree 		type = efx_tc_rx_version_2(efx, data, mark);
52925730d8bSEdward Cree 		break;
53025730d8bSEdward Cree 	default:
53125730d8bSEdward Cree 		if (net_ratelimit())
53225730d8bSEdward Cree 			netif_err(efx, drv, efx->net_dev,
53325730d8bSEdward Cree 				  "choked on MAE counter packet (bad version %u"
53425730d8bSEdward Cree 				  "); counters may be inaccurate\n",
53525730d8bSEdward Cree 				  version);
53625730d8bSEdward Cree 		goto out;
53725730d8bSEdward Cree 	}
53825730d8bSEdward Cree 
539e80bd08fSEdward Cree 	if (type < EFX_TC_COUNTER_TYPE_MAX) {
54025730d8bSEdward Cree 		/* Update seen_gen unconditionally, to avoid a missed wakeup if
54125730d8bSEdward Cree 		 * we race with efx_mae_stop_counters().
54225730d8bSEdward Cree 		 */
54325730d8bSEdward Cree 		efx->tc->seen_gen[type] = mark;
54425730d8bSEdward Cree 		if (efx->tc->flush_counters &&
54525730d8bSEdward Cree 		    (s32)(efx->tc->flush_gen[type] - mark) <= 0)
54625730d8bSEdward Cree 			wake_up(&efx->tc->flush_wq);
547e80bd08fSEdward Cree 	}
54825730d8bSEdward Cree out:
54925730d8bSEdward Cree 	efx_free_rx_buffers(rx_queue, rx_buf, 1);
55025730d8bSEdward Cree 	channel->rx_pkt_n_frags = 0;
55125730d8bSEdward Cree 	return true;
55225730d8bSEdward Cree }
55325730d8bSEdward Cree 
55425730d8bSEdward Cree const struct efx_channel_type efx_tc_channel_type = {
55525730d8bSEdward Cree 	.handle_no_channel	= efx_tc_handle_no_channel,
55625730d8bSEdward Cree 	.pre_probe		= efx_tc_probe_channel,
55725730d8bSEdward Cree 	.start			= efx_tc_start_channel,
55825730d8bSEdward Cree 	.stop			= efx_tc_stop_channel,
55925730d8bSEdward Cree 	.post_remove		= efx_tc_remove_channel,
56025730d8bSEdward Cree 	.get_name		= efx_tc_get_channel_name,
56125730d8bSEdward Cree 	.receive_raw		= efx_tc_rx,
56225730d8bSEdward Cree 	.keep_eventq		= true,
56325730d8bSEdward Cree };
564