1
/****************************************************************************
2
* Driver for Solarflare Solarstorm network controllers and boards
3
* Copyright 2005-2010 Solarflare Communications Inc.
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 as published
7
* by the Free Software Foundation, incorporated herein by reference.
18
/* "Fudge factors" - difference between programmed value and actual depth.
19
* Due to pipelined implementation we need to program H/W with a value that
20
* is larger than the hop limit we want.
22
#define FILTER_CTL_SRCH_FUDGE_WILD 3
23
#define FILTER_CTL_SRCH_FUDGE_FULL 1
25
/* Hard maximum hop limit. Hardware will time-out beyond 200-something.
26
* We also need to avoid infinite loops in efx_filter_search() when the
29
#define FILTER_CTL_SRCH_MAX 200
31
/* Don't try very hard to find space for performance hints, as this is
32
* counter-productive. */
33
#define FILTER_CTL_SRCH_HINT_MAX 5
35
enum efx_filter_table_id {
36
EFX_FILTER_TABLE_RX_IP = 0,
37
EFX_FILTER_TABLE_RX_MAC,
38
EFX_FILTER_TABLE_COUNT,
41
struct efx_filter_table {
42
enum efx_filter_table_id id;
43
u32 offset; /* address of table relative to BAR */
44
unsigned size; /* number of entries */
45
unsigned step; /* step between entries */
46
unsigned used; /* number currently used */
47
unsigned long *used_bitmap;
48
struct efx_filter_spec *spec;
49
unsigned search_depth[EFX_FILTER_TYPE_COUNT];
52
struct efx_filter_state {
54
struct efx_filter_table table[EFX_FILTER_TABLE_COUNT];
55
#ifdef CONFIG_RFS_ACCEL
57
unsigned rps_expire_index;
61
/* The filter hash function is LFSR polynomial x^16 + x^3 + 1 of a 32-bit
62
* key derived from the n-tuple. The initial LFSR state is 0xffff. */
63
static u16 efx_filter_hash(u32 key)
68
tmp = 0x1fff ^ key >> 16;
69
tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
72
tmp = tmp ^ tmp << 13 ^ key;
73
tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
74
return tmp ^ tmp >> 9;
77
/* To allow for hash collisions, filter search continues at these
78
* increments from the first possible entry selected by the hash. */
79
static u16 efx_filter_increment(u32 key)
84
static enum efx_filter_table_id
85
efx_filter_spec_table_id(const struct efx_filter_spec *spec)
87
BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_TCP_FULL >> 2));
88
BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_TCP_WILD >> 2));
89
BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_UDP_FULL >> 2));
90
BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_UDP_WILD >> 2));
91
BUILD_BUG_ON(EFX_FILTER_TABLE_RX_MAC != (EFX_FILTER_MAC_FULL >> 2));
92
BUILD_BUG_ON(EFX_FILTER_TABLE_RX_MAC != (EFX_FILTER_MAC_WILD >> 2));
93
EFX_BUG_ON_PARANOID(spec->type == EFX_FILTER_UNSPEC);
94
return spec->type >> 2;
97
static struct efx_filter_table *
98
efx_filter_spec_table(struct efx_filter_state *state,
99
const struct efx_filter_spec *spec)
101
if (spec->type == EFX_FILTER_UNSPEC)
104
return &state->table[efx_filter_spec_table_id(spec)];
107
static void efx_filter_table_reset_search_depth(struct efx_filter_table *table)
109
memset(table->search_depth, 0, sizeof(table->search_depth));
112
static void efx_filter_push_rx_limits(struct efx_nic *efx)
114
struct efx_filter_state *state = efx->filter_state;
115
struct efx_filter_table *table;
116
efx_oword_t filter_ctl;
118
efx_reado(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
120
table = &state->table[EFX_FILTER_TABLE_RX_IP];
121
EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_FULL_SRCH_LIMIT,
122
table->search_depth[EFX_FILTER_TCP_FULL] +
123
FILTER_CTL_SRCH_FUDGE_FULL);
124
EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_WILD_SRCH_LIMIT,
125
table->search_depth[EFX_FILTER_TCP_WILD] +
126
FILTER_CTL_SRCH_FUDGE_WILD);
127
EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_FULL_SRCH_LIMIT,
128
table->search_depth[EFX_FILTER_UDP_FULL] +
129
FILTER_CTL_SRCH_FUDGE_FULL);
130
EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_WILD_SRCH_LIMIT,
131
table->search_depth[EFX_FILTER_UDP_WILD] +
132
FILTER_CTL_SRCH_FUDGE_WILD);
134
table = &state->table[EFX_FILTER_TABLE_RX_MAC];
137
filter_ctl, FRF_CZ_ETHERNET_FULL_SEARCH_LIMIT,
138
table->search_depth[EFX_FILTER_MAC_FULL] +
139
FILTER_CTL_SRCH_FUDGE_FULL);
141
filter_ctl, FRF_CZ_ETHERNET_WILDCARD_SEARCH_LIMIT,
142
table->search_depth[EFX_FILTER_MAC_WILD] +
143
FILTER_CTL_SRCH_FUDGE_WILD);
146
efx_writeo(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
149
static inline void __efx_filter_set_ipv4(struct efx_filter_spec *spec,
150
__be32 host1, __be16 port1,
151
__be32 host2, __be16 port2)
153
spec->data[0] = ntohl(host1) << 16 | ntohs(port1);
154
spec->data[1] = ntohs(port2) << 16 | ntohl(host1) >> 16;
155
spec->data[2] = ntohl(host2);
159
* efx_filter_set_ipv4_local - specify IPv4 host, transport protocol and port
160
* @spec: Specification to initialise
161
* @proto: Transport layer protocol number
162
* @host: Local host address (network byte order)
163
* @port: Local port (network byte order)
165
int efx_filter_set_ipv4_local(struct efx_filter_spec *spec, u8 proto,
166
__be32 host, __be16 port)
171
EFX_BUG_ON_PARANOID(!(spec->flags & EFX_FILTER_FLAG_RX));
173
/* This cannot currently be combined with other filtering */
174
if (spec->type != EFX_FILTER_UNSPEC)
175
return -EPROTONOSUPPORT;
182
spec->type = EFX_FILTER_TCP_WILD;
185
spec->type = EFX_FILTER_UDP_WILD;
188
return -EPROTONOSUPPORT;
191
/* Filter is constructed in terms of source and destination,
192
* with the odd wrinkle that the ports are swapped in a UDP
193
* wildcard filter. We need to convert from local and remote
194
* (= zero for wildcard) addresses.
197
if (proto != IPPROTO_UDP) {
204
__efx_filter_set_ipv4(spec, host1, port1, host, port);
209
* efx_filter_set_ipv4_full - specify IPv4 hosts, transport protocol and ports
210
* @spec: Specification to initialise
211
* @proto: Transport layer protocol number
212
* @host: Local host address (network byte order)
213
* @port: Local port (network byte order)
214
* @rhost: Remote host address (network byte order)
215
* @rport: Remote port (network byte order)
217
int efx_filter_set_ipv4_full(struct efx_filter_spec *spec, u8 proto,
218
__be32 host, __be16 port,
219
__be32 rhost, __be16 rport)
221
EFX_BUG_ON_PARANOID(!(spec->flags & EFX_FILTER_FLAG_RX));
223
/* This cannot currently be combined with other filtering */
224
if (spec->type != EFX_FILTER_UNSPEC)
225
return -EPROTONOSUPPORT;
227
if (port == 0 || rport == 0)
232
spec->type = EFX_FILTER_TCP_FULL;
235
spec->type = EFX_FILTER_UDP_FULL;
238
return -EPROTONOSUPPORT;
241
__efx_filter_set_ipv4(spec, rhost, rport, host, port);
246
* efx_filter_set_eth_local - specify local Ethernet address and optional VID
247
* @spec: Specification to initialise
248
* @vid: VLAN ID to match, or %EFX_FILTER_VID_UNSPEC
249
* @addr: Local Ethernet MAC address
251
int efx_filter_set_eth_local(struct efx_filter_spec *spec,
252
u16 vid, const u8 *addr)
254
EFX_BUG_ON_PARANOID(!(spec->flags & EFX_FILTER_FLAG_RX));
256
/* This cannot currently be combined with other filtering */
257
if (spec->type != EFX_FILTER_UNSPEC)
258
return -EPROTONOSUPPORT;
260
if (vid == EFX_FILTER_VID_UNSPEC) {
261
spec->type = EFX_FILTER_MAC_WILD;
264
spec->type = EFX_FILTER_MAC_FULL;
268
spec->data[1] = addr[2] << 24 | addr[3] << 16 | addr[4] << 8 | addr[5];
269
spec->data[2] = addr[0] << 8 | addr[1];
273
/* Build a filter entry and return its n-tuple key. */
274
static u32 efx_filter_build(efx_oword_t *filter, struct efx_filter_spec *spec)
278
switch (efx_filter_spec_table_id(spec)) {
279
case EFX_FILTER_TABLE_RX_IP: {
280
bool is_udp = (spec->type == EFX_FILTER_UDP_FULL ||
281
spec->type == EFX_FILTER_UDP_WILD);
282
EFX_POPULATE_OWORD_7(
285
!!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
287
!!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
288
FRF_BZ_TCP_UDP, is_udp,
289
FRF_BZ_RXQ_ID, spec->dmaq_id,
290
EFX_DWORD_2, spec->data[2],
291
EFX_DWORD_1, spec->data[1],
292
EFX_DWORD_0, spec->data[0]);
297
case EFX_FILTER_TABLE_RX_MAC: {
298
bool is_wild = spec->type == EFX_FILTER_MAC_WILD;
299
EFX_POPULATE_OWORD_8(
302
!!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
303
FRF_CZ_RMFT_SCATTER_EN,
304
!!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
305
FRF_CZ_RMFT_IP_OVERRIDE,
306
!!(spec->flags & EFX_FILTER_FLAG_RX_OVERRIDE_IP),
307
FRF_CZ_RMFT_RXQ_ID, spec->dmaq_id,
308
FRF_CZ_RMFT_WILDCARD_MATCH, is_wild,
309
FRF_CZ_RMFT_DEST_MAC_HI, spec->data[2],
310
FRF_CZ_RMFT_DEST_MAC_LO, spec->data[1],
311
FRF_CZ_RMFT_VLAN_ID, spec->data[0]);
320
return spec->data[0] ^ spec->data[1] ^ spec->data[2] ^ data3;
323
static bool efx_filter_equal(const struct efx_filter_spec *left,
324
const struct efx_filter_spec *right)
326
if (left->type != right->type ||
327
memcmp(left->data, right->data, sizeof(left->data)))
333
static int efx_filter_search(struct efx_filter_table *table,
334
struct efx_filter_spec *spec, u32 key,
335
bool for_insert, int *depth_required)
337
unsigned hash, incr, filter_idx, depth, depth_max;
339
hash = efx_filter_hash(key);
340
incr = efx_filter_increment(key);
342
filter_idx = hash & (table->size - 1);
344
depth_max = (for_insert ?
345
(spec->priority <= EFX_FILTER_PRI_HINT ?
346
FILTER_CTL_SRCH_HINT_MAX : FILTER_CTL_SRCH_MAX) :
347
table->search_depth[spec->type]);
350
/* Return success if entry is used and matches this spec
351
* or entry is unused and we are trying to insert.
353
if (test_bit(filter_idx, table->used_bitmap) ?
354
efx_filter_equal(spec, &table->spec[filter_idx]) :
356
*depth_required = depth;
360
/* Return failure if we reached the maximum search depth */
361
if (depth == depth_max)
362
return for_insert ? -EBUSY : -ENOENT;
364
filter_idx = (filter_idx + incr) & (table->size - 1);
369
/* Construct/deconstruct external filter IDs */
372
efx_filter_make_id(enum efx_filter_table_id table_id, unsigned index)
374
return table_id << 16 | index;
378
* efx_filter_insert_filter - add or replace a filter
379
* @efx: NIC in which to insert the filter
380
* @spec: Specification for the filter
381
* @replace: Flag for whether the specified filter may replace a filter
382
* with an identical match expression and equal or lower priority
384
* On success, return the filter ID.
385
* On failure, return a negative error code.
387
int efx_filter_insert_filter(struct efx_nic *efx, struct efx_filter_spec *spec,
390
struct efx_filter_state *state = efx->filter_state;
391
struct efx_filter_table *table = efx_filter_spec_table(state, spec);
392
struct efx_filter_spec *saved_spec;
394
int filter_idx, depth;
398
if (!table || table->size == 0)
401
key = efx_filter_build(&filter, spec);
403
netif_vdbg(efx, hw, efx->net_dev,
404
"%s: type %d search_depth=%d", __func__, spec->type,
405
table->search_depth[spec->type]);
407
spin_lock_bh(&state->lock);
409
rc = efx_filter_search(table, spec, key, true, &depth);
413
BUG_ON(filter_idx >= table->size);
414
saved_spec = &table->spec[filter_idx];
416
if (test_bit(filter_idx, table->used_bitmap)) {
417
/* Should we replace the existing filter? */
422
if (spec->priority < saved_spec->priority) {
427
__set_bit(filter_idx, table->used_bitmap);
432
if (table->search_depth[spec->type] < depth) {
433
table->search_depth[spec->type] = depth;
434
efx_filter_push_rx_limits(efx);
437
efx_writeo(efx, &filter, table->offset + table->step * filter_idx);
439
netif_vdbg(efx, hw, efx->net_dev,
440
"%s: filter type %d index %d rxq %u set",
441
__func__, spec->type, filter_idx, spec->dmaq_id);
442
rc = efx_filter_make_id(table->id, filter_idx);
445
spin_unlock_bh(&state->lock);
449
static void efx_filter_table_clear_entry(struct efx_nic *efx,
450
struct efx_filter_table *table,
453
static efx_oword_t filter;
455
if (test_bit(filter_idx, table->used_bitmap)) {
456
__clear_bit(filter_idx, table->used_bitmap);
458
memset(&table->spec[filter_idx], 0, sizeof(table->spec[0]));
460
efx_writeo(efx, &filter,
461
table->offset + table->step * filter_idx);
466
* efx_filter_remove_filter - remove a filter by specification
467
* @efx: NIC from which to remove the filter
468
* @spec: Specification for the filter
470
* On success, return zero.
471
* On failure, return a negative error code.
473
int efx_filter_remove_filter(struct efx_nic *efx, struct efx_filter_spec *spec)
475
struct efx_filter_state *state = efx->filter_state;
476
struct efx_filter_table *table = efx_filter_spec_table(state, spec);
477
struct efx_filter_spec *saved_spec;
479
int filter_idx, depth;
486
key = efx_filter_build(&filter, spec);
488
spin_lock_bh(&state->lock);
490
rc = efx_filter_search(table, spec, key, false, &depth);
494
saved_spec = &table->spec[filter_idx];
496
if (spec->priority < saved_spec->priority) {
501
efx_filter_table_clear_entry(efx, table, filter_idx);
502
if (table->used == 0)
503
efx_filter_table_reset_search_depth(table);
507
spin_unlock_bh(&state->lock);
511
static void efx_filter_table_clear(struct efx_nic *efx,
512
enum efx_filter_table_id table_id,
513
enum efx_filter_priority priority)
515
struct efx_filter_state *state = efx->filter_state;
516
struct efx_filter_table *table = &state->table[table_id];
519
spin_lock_bh(&state->lock);
521
for (filter_idx = 0; filter_idx < table->size; ++filter_idx)
522
if (table->spec[filter_idx].priority <= priority)
523
efx_filter_table_clear_entry(efx, table, filter_idx);
524
if (table->used == 0)
525
efx_filter_table_reset_search_depth(table);
527
spin_unlock_bh(&state->lock);
531
* efx_filter_clear_rx - remove RX filters by priority
532
* @efx: NIC from which to remove the filters
533
* @priority: Maximum priority to remove
535
void efx_filter_clear_rx(struct efx_nic *efx, enum efx_filter_priority priority)
537
efx_filter_table_clear(efx, EFX_FILTER_TABLE_RX_IP, priority);
538
efx_filter_table_clear(efx, EFX_FILTER_TABLE_RX_MAC, priority);
541
/* Restore filter stater after reset */
542
void efx_restore_filters(struct efx_nic *efx)
544
struct efx_filter_state *state = efx->filter_state;
545
enum efx_filter_table_id table_id;
546
struct efx_filter_table *table;
550
spin_lock_bh(&state->lock);
552
for (table_id = 0; table_id < EFX_FILTER_TABLE_COUNT; table_id++) {
553
table = &state->table[table_id];
554
for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
555
if (!test_bit(filter_idx, table->used_bitmap))
557
efx_filter_build(&filter, &table->spec[filter_idx]);
558
efx_writeo(efx, &filter,
559
table->offset + table->step * filter_idx);
563
efx_filter_push_rx_limits(efx);
565
spin_unlock_bh(&state->lock);
568
int efx_probe_filters(struct efx_nic *efx)
570
struct efx_filter_state *state;
571
struct efx_filter_table *table;
574
state = kzalloc(sizeof(*efx->filter_state), GFP_KERNEL);
577
efx->filter_state = state;
579
spin_lock_init(&state->lock);
581
if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
582
#ifdef CONFIG_RFS_ACCEL
583
state->rps_flow_id = kcalloc(FR_BZ_RX_FILTER_TBL0_ROWS,
584
sizeof(*state->rps_flow_id),
586
if (!state->rps_flow_id)
589
table = &state->table[EFX_FILTER_TABLE_RX_IP];
590
table->id = EFX_FILTER_TABLE_RX_IP;
591
table->offset = FR_BZ_RX_FILTER_TBL0;
592
table->size = FR_BZ_RX_FILTER_TBL0_ROWS;
593
table->step = FR_BZ_RX_FILTER_TBL0_STEP;
596
if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
597
table = &state->table[EFX_FILTER_TABLE_RX_MAC];
598
table->id = EFX_FILTER_TABLE_RX_MAC;
599
table->offset = FR_CZ_RX_MAC_FILTER_TBL0;
600
table->size = FR_CZ_RX_MAC_FILTER_TBL0_ROWS;
601
table->step = FR_CZ_RX_MAC_FILTER_TBL0_STEP;
604
for (table_id = 0; table_id < EFX_FILTER_TABLE_COUNT; table_id++) {
605
table = &state->table[table_id];
606
if (table->size == 0)
608
table->used_bitmap = kcalloc(BITS_TO_LONGS(table->size),
609
sizeof(unsigned long),
611
if (!table->used_bitmap)
613
table->spec = vzalloc(table->size * sizeof(*table->spec));
621
efx_remove_filters(efx);
625
void efx_remove_filters(struct efx_nic *efx)
627
struct efx_filter_state *state = efx->filter_state;
628
enum efx_filter_table_id table_id;
630
for (table_id = 0; table_id < EFX_FILTER_TABLE_COUNT; table_id++) {
631
kfree(state->table[table_id].used_bitmap);
632
vfree(state->table[table_id].spec);
634
#ifdef CONFIG_RFS_ACCEL
635
kfree(state->rps_flow_id);
640
#ifdef CONFIG_RFS_ACCEL
642
int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
643
u16 rxq_index, u32 flow_id)
645
struct efx_nic *efx = netdev_priv(net_dev);
646
struct efx_channel *channel;
647
struct efx_filter_state *state = efx->filter_state;
648
struct efx_filter_spec spec;
649
const struct iphdr *ip;
654
nhoff = skb_network_offset(skb);
656
if (skb->protocol != htons(ETH_P_IP))
657
return -EPROTONOSUPPORT;
659
/* RFS must validate the IP header length before calling us */
660
EFX_BUG_ON_PARANOID(skb_headlen(skb) < nhoff + sizeof(*ip));
661
ip = (const struct iphdr *)(skb->data + nhoff);
662
if (ip_is_fragment(ip))
663
return -EPROTONOSUPPORT;
664
EFX_BUG_ON_PARANOID(skb_headlen(skb) < nhoff + 4 * ip->ihl + 4);
665
ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
667
efx_filter_init_rx(&spec, EFX_FILTER_PRI_HINT, 0, rxq_index);
668
rc = efx_filter_set_ipv4_full(&spec, ip->protocol,
669
ip->daddr, ports[1], ip->saddr, ports[0]);
673
rc = efx_filter_insert_filter(efx, &spec, true);
677
/* Remember this so we can check whether to expire the filter later */
678
state->rps_flow_id[rc] = flow_id;
679
channel = efx_get_channel(efx, skb_get_rx_queue(skb));
680
++channel->rfs_filters_added;
682
netif_info(efx, rx_status, efx->net_dev,
683
"steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d]\n",
684
(ip->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
685
&ip->saddr, ntohs(ports[0]), &ip->daddr, ntohs(ports[1]),
686
rxq_index, flow_id, rc);
691
bool __efx_filter_rfs_expire(struct efx_nic *efx, unsigned quota)
693
struct efx_filter_state *state = efx->filter_state;
694
struct efx_filter_table *table = &state->table[EFX_FILTER_TABLE_RX_IP];
695
unsigned mask = table->size - 1;
699
if (!spin_trylock_bh(&state->lock))
702
index = state->rps_expire_index;
703
stop = (index + quota) & mask;
705
while (index != stop) {
706
if (test_bit(index, table->used_bitmap) &&
707
table->spec[index].priority == EFX_FILTER_PRI_HINT &&
708
rps_may_expire_flow(efx->net_dev,
709
table->spec[index].dmaq_id,
710
state->rps_flow_id[index], index)) {
711
netif_info(efx, rx_status, efx->net_dev,
712
"expiring filter %d [flow %u]\n",
713
index, state->rps_flow_id[index]);
714
efx_filter_table_clear_entry(efx, table, index);
716
index = (index + 1) & mask;
719
state->rps_expire_index = stop;
720
if (table->used == 0)
721
efx_filter_table_reset_search_depth(table);
723
spin_unlock_bh(&state->lock);
727
#endif /* CONFIG_RFS_ACCEL */