47a1b79847881913da5d5c4fa6c4e73f2eff06a4
[pandora-kernel.git] / drivers / net / sfc / filter.c
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2005-2010 Solarflare Communications Inc.
4  *
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.
8  */
9
10 #include <linux/in.h>
11 #include "efx.h"
12 #include "filter.h"
13 #include "io.h"
14 #include "nic.h"
15 #include "regs.h"
16
17 /* "Fudge factors" - difference between programmed value and actual depth.
18  * Due to pipelined implementation we need to program H/W with a value that
19  * is larger than the hop limit we want.
20  */
21 #define FILTER_CTL_SRCH_FUDGE_WILD 3
22 #define FILTER_CTL_SRCH_FUDGE_FULL 1
23
24 /* Hard maximum hop limit.  Hardware will time-out beyond 200-something.
25  * We also need to avoid infinite loops in efx_filter_search() when the
26  * table is full.
27  */
28 #define FILTER_CTL_SRCH_MAX 200
29
30 /* Don't try very hard to find space for performance hints, as this is
31  * counter-productive. */
32 #define FILTER_CTL_SRCH_HINT_MAX 5
33
34 enum efx_filter_table_id {
35         EFX_FILTER_TABLE_RX_IP = 0,
36         EFX_FILTER_TABLE_RX_MAC,
37         EFX_FILTER_TABLE_COUNT,
38 };
39
40 struct efx_filter_table {
41         enum efx_filter_table_id id;
42         u32             offset;         /* address of table relative to BAR */
43         unsigned        size;           /* number of entries */
44         unsigned        step;           /* step between entries */
45         unsigned        used;           /* number currently used */
46         unsigned long   *used_bitmap;
47         struct efx_filter_spec *spec;
48         unsigned        search_depth[EFX_FILTER_TYPE_COUNT];
49 };
50
51 struct efx_filter_state {
52         spinlock_t      lock;
53         struct efx_filter_table table[EFX_FILTER_TABLE_COUNT];
54 };
55
56 /* The filter hash function is LFSR polynomial x^16 + x^3 + 1 of a 32-bit
57  * key derived from the n-tuple.  The initial LFSR state is 0xffff. */
58 static u16 efx_filter_hash(u32 key)
59 {
60         u16 tmp;
61
62         /* First 16 rounds */
63         tmp = 0x1fff ^ key >> 16;
64         tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
65         tmp = tmp ^ tmp >> 9;
66         /* Last 16 rounds */
67         tmp = tmp ^ tmp << 13 ^ key;
68         tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
69         return tmp ^ tmp >> 9;
70 }
71
72 /* To allow for hash collisions, filter search continues at these
73  * increments from the first possible entry selected by the hash. */
74 static u16 efx_filter_increment(u32 key)
75 {
76         return key * 2 - 1;
77 }
78
79 static enum efx_filter_table_id
80 efx_filter_spec_table_id(const struct efx_filter_spec *spec)
81 {
82         BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_TCP_FULL >> 2));
83         BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_TCP_WILD >> 2));
84         BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_UDP_FULL >> 2));
85         BUILD_BUG_ON(EFX_FILTER_TABLE_RX_IP != (EFX_FILTER_UDP_WILD >> 2));
86         BUILD_BUG_ON(EFX_FILTER_TABLE_RX_MAC != (EFX_FILTER_MAC_FULL >> 2));
87         BUILD_BUG_ON(EFX_FILTER_TABLE_RX_MAC != (EFX_FILTER_MAC_WILD >> 2));
88         EFX_BUG_ON_PARANOID(spec->type == EFX_FILTER_UNSPEC);
89         return spec->type >> 2;
90 }
91
92 static struct efx_filter_table *
93 efx_filter_spec_table(struct efx_filter_state *state,
94                       const struct efx_filter_spec *spec)
95 {
96         if (spec->type == EFX_FILTER_UNSPEC)
97                 return NULL;
98         else
99                 return &state->table[efx_filter_spec_table_id(spec)];
100 }
101
102 static void efx_filter_table_reset_search_depth(struct efx_filter_table *table)
103 {
104         memset(table->search_depth, 0, sizeof(table->search_depth));
105 }
106
107 static void efx_filter_push_rx_limits(struct efx_nic *efx)
108 {
109         struct efx_filter_state *state = efx->filter_state;
110         struct efx_filter_table *table;
111         efx_oword_t filter_ctl;
112
113         efx_reado(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
114
115         table = &state->table[EFX_FILTER_TABLE_RX_IP];
116         EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_FULL_SRCH_LIMIT,
117                             table->search_depth[EFX_FILTER_TCP_FULL] +
118                             FILTER_CTL_SRCH_FUDGE_FULL);
119         EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_WILD_SRCH_LIMIT,
120                             table->search_depth[EFX_FILTER_TCP_WILD] +
121                             FILTER_CTL_SRCH_FUDGE_WILD);
122         EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_FULL_SRCH_LIMIT,
123                             table->search_depth[EFX_FILTER_UDP_FULL] +
124                             FILTER_CTL_SRCH_FUDGE_FULL);
125         EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_WILD_SRCH_LIMIT,
126                             table->search_depth[EFX_FILTER_UDP_WILD] +
127                             FILTER_CTL_SRCH_FUDGE_WILD);
128
129         table = &state->table[EFX_FILTER_TABLE_RX_MAC];
130         if (table->size) {
131                 EFX_SET_OWORD_FIELD(
132                         filter_ctl, FRF_CZ_ETHERNET_FULL_SEARCH_LIMIT,
133                         table->search_depth[EFX_FILTER_MAC_FULL] +
134                         FILTER_CTL_SRCH_FUDGE_FULL);
135                 EFX_SET_OWORD_FIELD(
136                         filter_ctl, FRF_CZ_ETHERNET_WILDCARD_SEARCH_LIMIT,
137                         table->search_depth[EFX_FILTER_MAC_WILD] +
138                         FILTER_CTL_SRCH_FUDGE_WILD);
139         }
140
141         efx_writeo(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
142 }
143
144 static inline void __efx_filter_set_ipv4(struct efx_filter_spec *spec,
145                                          __be32 host1, __be16 port1,
146                                          __be32 host2, __be16 port2)
147 {
148         spec->data[0] = ntohl(host1) << 16 | ntohs(port1);
149         spec->data[1] = ntohs(port2) << 16 | ntohl(host1) >> 16;
150         spec->data[2] = ntohl(host2);
151 }
152
153 /**
154  * efx_filter_set_ipv4_local - specify IPv4 host, transport protocol and port
155  * @spec: Specification to initialise
156  * @proto: Transport layer protocol number
157  * @host: Local host address (network byte order)
158  * @port: Local port (network byte order)
159  */
160 int efx_filter_set_ipv4_local(struct efx_filter_spec *spec, u8 proto,
161                               __be32 host, __be16 port)
162 {
163         __be32 host1;
164         __be16 port1;
165
166         EFX_BUG_ON_PARANOID(!(spec->flags & EFX_FILTER_FLAG_RX));
167
168         /* This cannot currently be combined with other filtering */
169         if (spec->type != EFX_FILTER_UNSPEC)
170                 return -EPROTONOSUPPORT;
171
172         if (port == 0)
173                 return -EINVAL;
174
175         switch (proto) {
176         case IPPROTO_TCP:
177                 spec->type = EFX_FILTER_TCP_WILD;
178                 break;
179         case IPPROTO_UDP:
180                 spec->type = EFX_FILTER_UDP_WILD;
181                 break;
182         default:
183                 return -EPROTONOSUPPORT;
184         }
185
186         /* Filter is constructed in terms of source and destination,
187          * with the odd wrinkle that the ports are swapped in a UDP
188          * wildcard filter.  We need to convert from local and remote
189          * (= zero for wildcard) addresses.
190          */
191         host1 = 0;
192         if (proto != IPPROTO_UDP) {
193                 port1 = 0;
194         } else {
195                 port1 = port;
196                 port = 0;
197         }
198
199         __efx_filter_set_ipv4(spec, host1, port1, host, port);
200         return 0;
201 }
202
203 /**
204  * efx_filter_set_ipv4_full - specify IPv4 hosts, transport protocol and ports
205  * @spec: Specification to initialise
206  * @proto: Transport layer protocol number
207  * @host: Local host address (network byte order)
208  * @port: Local port (network byte order)
209  * @rhost: Remote host address (network byte order)
210  * @rport: Remote port (network byte order)
211  */
212 int efx_filter_set_ipv4_full(struct efx_filter_spec *spec, u8 proto,
213                              __be32 host, __be16 port,
214                              __be32 rhost, __be16 rport)
215 {
216         EFX_BUG_ON_PARANOID(!(spec->flags & EFX_FILTER_FLAG_RX));
217
218         /* This cannot currently be combined with other filtering */
219         if (spec->type != EFX_FILTER_UNSPEC)
220                 return -EPROTONOSUPPORT;
221
222         if (port == 0 || rport == 0)
223                 return -EINVAL;
224
225         switch (proto) {
226         case IPPROTO_TCP:
227                 spec->type = EFX_FILTER_TCP_FULL;
228                 break;
229         case IPPROTO_UDP:
230                 spec->type = EFX_FILTER_UDP_FULL;
231                 break;
232         default:
233                 return -EPROTONOSUPPORT;
234         }
235
236         __efx_filter_set_ipv4(spec, rhost, rport, host, port);
237         return 0;
238 }
239
240 /**
241  * efx_filter_set_eth_local - specify local Ethernet address and optional VID
242  * @spec: Specification to initialise
243  * @vid: VLAN ID to match, or %EFX_FILTER_VID_UNSPEC
244  * @addr: Local Ethernet MAC address
245  */
246 int efx_filter_set_eth_local(struct efx_filter_spec *spec,
247                              u16 vid, const u8 *addr)
248 {
249         EFX_BUG_ON_PARANOID(!(spec->flags & EFX_FILTER_FLAG_RX));
250
251         /* This cannot currently be combined with other filtering */
252         if (spec->type != EFX_FILTER_UNSPEC)
253                 return -EPROTONOSUPPORT;
254
255         if (vid == EFX_FILTER_VID_UNSPEC) {
256                 spec->type = EFX_FILTER_MAC_WILD;
257                 spec->data[0] = 0;
258         } else {
259                 spec->type = EFX_FILTER_MAC_FULL;
260                 spec->data[0] = vid;
261         }
262
263         spec->data[1] = addr[2] << 24 | addr[3] << 16 | addr[4] << 8 | addr[5];
264         spec->data[2] = addr[0] << 8 | addr[1];
265         return 0;
266 }
267
268 /* Build a filter entry and return its n-tuple key. */
269 static u32 efx_filter_build(efx_oword_t *filter, struct efx_filter_spec *spec)
270 {
271         u32 data3;
272
273         switch (efx_filter_spec_table_id(spec)) {
274         case EFX_FILTER_TABLE_RX_IP: {
275                 bool is_udp = (spec->type == EFX_FILTER_UDP_FULL ||
276                                spec->type == EFX_FILTER_UDP_WILD);
277                 EFX_POPULATE_OWORD_7(
278                         *filter,
279                         FRF_BZ_RSS_EN,
280                         !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
281                         FRF_BZ_SCATTER_EN,
282                         !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
283                         FRF_BZ_TCP_UDP, is_udp,
284                         FRF_BZ_RXQ_ID, spec->dmaq_id,
285                         EFX_DWORD_2, spec->data[2],
286                         EFX_DWORD_1, spec->data[1],
287                         EFX_DWORD_0, spec->data[0]);
288                 data3 = is_udp;
289                 break;
290         }
291
292         case EFX_FILTER_TABLE_RX_MAC: {
293                 bool is_wild = spec->type == EFX_FILTER_MAC_WILD;
294                 EFX_POPULATE_OWORD_8(
295                         *filter,
296                         FRF_CZ_RMFT_RSS_EN,
297                         !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
298                         FRF_CZ_RMFT_SCATTER_EN,
299                         !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
300                         FRF_CZ_RMFT_IP_OVERRIDE,
301                         !!(spec->flags & EFX_FILTER_FLAG_RX_OVERRIDE_IP),
302                         FRF_CZ_RMFT_RXQ_ID, spec->dmaq_id,
303                         FRF_CZ_RMFT_WILDCARD_MATCH, is_wild,
304                         FRF_CZ_RMFT_DEST_MAC_HI, spec->data[2],
305                         FRF_CZ_RMFT_DEST_MAC_LO, spec->data[1],
306                         FRF_CZ_RMFT_VLAN_ID, spec->data[0]);
307                 data3 = is_wild;
308                 break;
309         }
310
311         default:
312                 BUG();
313         }
314
315         return spec->data[0] ^ spec->data[1] ^ spec->data[2] ^ data3;
316 }
317
318 static bool efx_filter_equal(const struct efx_filter_spec *left,
319                              const struct efx_filter_spec *right)
320 {
321         if (left->type != right->type ||
322             memcmp(left->data, right->data, sizeof(left->data)))
323                 return false;
324
325         return true;
326 }
327
328 static int efx_filter_search(struct efx_filter_table *table,
329                              struct efx_filter_spec *spec, u32 key,
330                              bool for_insert, int *depth_required)
331 {
332         unsigned hash, incr, filter_idx, depth, depth_max;
333         struct efx_filter_spec *cmp;
334
335         hash = efx_filter_hash(key);
336         incr = efx_filter_increment(key);
337         depth_max = (spec->priority <= EFX_FILTER_PRI_HINT ?
338                      FILTER_CTL_SRCH_HINT_MAX : FILTER_CTL_SRCH_MAX);
339
340         for (depth = 1, filter_idx = hash & (table->size - 1);
341              depth <= depth_max && test_bit(filter_idx, table->used_bitmap);
342              ++depth) {
343                 cmp = &table->spec[filter_idx];
344                 if (efx_filter_equal(spec, cmp))
345                         goto found;
346                 filter_idx = (filter_idx + incr) & (table->size - 1);
347         }
348         if (!for_insert)
349                 return -ENOENT;
350         if (depth > depth_max)
351                 return -EBUSY;
352 found:
353         *depth_required = depth;
354         return filter_idx;
355 }
356
357 /* Construct/deconstruct external filter IDs */
358
359 static inline int
360 efx_filter_make_id(enum efx_filter_table_id table_id, unsigned index)
361 {
362         return table_id << 16 | index;
363 }
364
365 /**
366  * efx_filter_insert_filter - add or replace a filter
367  * @efx: NIC in which to insert the filter
368  * @spec: Specification for the filter
369  * @replace: Flag for whether the specified filter may replace a filter
370  *      with an identical match expression and equal or lower priority
371  *
372  * On success, return the filter ID.
373  * On failure, return a negative error code.
374  */
375 int efx_filter_insert_filter(struct efx_nic *efx, struct efx_filter_spec *spec,
376                              bool replace)
377 {
378         struct efx_filter_state *state = efx->filter_state;
379         struct efx_filter_table *table = efx_filter_spec_table(state, spec);
380         struct efx_filter_spec *saved_spec;
381         efx_oword_t filter;
382         int filter_idx, depth;
383         u32 key;
384         int rc;
385
386         if (!table || table->size == 0)
387                 return -EINVAL;
388
389         key = efx_filter_build(&filter, spec);
390
391         netif_vdbg(efx, hw, efx->net_dev,
392                    "%s: type %d search_depth=%d", __func__, spec->type,
393                    table->search_depth[spec->type]);
394
395         spin_lock_bh(&state->lock);
396
397         rc = efx_filter_search(table, spec, key, true, &depth);
398         if (rc < 0)
399                 goto out;
400         filter_idx = rc;
401         BUG_ON(filter_idx >= table->size);
402         saved_spec = &table->spec[filter_idx];
403
404         if (test_bit(filter_idx, table->used_bitmap)) {
405                 /* Should we replace the existing filter? */
406                 if (!replace) {
407                         rc = -EEXIST;
408                         goto out;
409                 }
410                 if (spec->priority < saved_spec->priority) {
411                         rc = -EPERM;
412                         goto out;
413                 }
414         } else {
415                 __set_bit(filter_idx, table->used_bitmap);
416                 ++table->used;
417         }
418         *saved_spec = *spec;
419
420         if (table->search_depth[spec->type] < depth) {
421                 table->search_depth[spec->type] = depth;
422                 efx_filter_push_rx_limits(efx);
423         }
424
425         efx_writeo(efx, &filter, table->offset + table->step * filter_idx);
426
427         netif_vdbg(efx, hw, efx->net_dev,
428                    "%s: filter type %d index %d rxq %u set",
429                    __func__, spec->type, filter_idx, spec->dmaq_id);
430         rc = efx_filter_make_id(table->id, filter_idx);
431
432 out:
433         spin_unlock_bh(&state->lock);
434         return rc;
435 }
436
437 static void efx_filter_table_clear_entry(struct efx_nic *efx,
438                                          struct efx_filter_table *table,
439                                          int filter_idx)
440 {
441         static efx_oword_t filter;
442
443         if (test_bit(filter_idx, table->used_bitmap)) {
444                 __clear_bit(filter_idx, table->used_bitmap);
445                 --table->used;
446                 memset(&table->spec[filter_idx], 0, sizeof(table->spec[0]));
447
448                 efx_writeo(efx, &filter,
449                            table->offset + table->step * filter_idx);
450         }
451 }
452
453 /**
454  * efx_filter_remove_filter - remove a filter by specification
455  * @efx: NIC from which to remove the filter
456  * @spec: Specification for the filter
457  *
458  * On success, return zero.
459  * On failure, return a negative error code.
460  */
461 int efx_filter_remove_filter(struct efx_nic *efx, struct efx_filter_spec *spec)
462 {
463         struct efx_filter_state *state = efx->filter_state;
464         struct efx_filter_table *table = efx_filter_spec_table(state, spec);
465         struct efx_filter_spec *saved_spec;
466         efx_oword_t filter;
467         int filter_idx, depth;
468         u32 key;
469         int rc;
470
471         if (!table)
472                 return -EINVAL;
473
474         key = efx_filter_build(&filter, spec);
475
476         spin_lock_bh(&state->lock);
477
478         rc = efx_filter_search(table, spec, key, false, &depth);
479         if (rc < 0)
480                 goto out;
481         filter_idx = rc;
482         saved_spec = &table->spec[filter_idx];
483
484         if (spec->priority < saved_spec->priority) {
485                 rc = -EPERM;
486                 goto out;
487         }
488
489         efx_filter_table_clear_entry(efx, table, filter_idx);
490         if (table->used == 0)
491                 efx_filter_table_reset_search_depth(table);
492         rc = 0;
493
494 out:
495         spin_unlock_bh(&state->lock);
496         return rc;
497 }
498
499 static void efx_filter_table_clear(struct efx_nic *efx,
500                                    enum efx_filter_table_id table_id,
501                                    enum efx_filter_priority priority)
502 {
503         struct efx_filter_state *state = efx->filter_state;
504         struct efx_filter_table *table = &state->table[table_id];
505         int filter_idx;
506
507         spin_lock_bh(&state->lock);
508
509         for (filter_idx = 0; filter_idx < table->size; ++filter_idx)
510                 if (table->spec[filter_idx].priority <= priority)
511                         efx_filter_table_clear_entry(efx, table, filter_idx);
512         if (table->used == 0)
513                 efx_filter_table_reset_search_depth(table);
514
515         spin_unlock_bh(&state->lock);
516 }
517
518 /**
519  * efx_filter_clear_rx - remove RX filters by priority
520  * @efx: NIC from which to remove the filters
521  * @priority: Maximum priority to remove
522  */
523 void efx_filter_clear_rx(struct efx_nic *efx, enum efx_filter_priority priority)
524 {
525         efx_filter_table_clear(efx, EFX_FILTER_TABLE_RX_IP, priority);
526         efx_filter_table_clear(efx, EFX_FILTER_TABLE_RX_MAC, priority);
527 }
528
529 /* Restore filter stater after reset */
530 void efx_restore_filters(struct efx_nic *efx)
531 {
532         struct efx_filter_state *state = efx->filter_state;
533         enum efx_filter_table_id table_id;
534         struct efx_filter_table *table;
535         efx_oword_t filter;
536         int filter_idx;
537
538         spin_lock_bh(&state->lock);
539
540         for (table_id = 0; table_id < EFX_FILTER_TABLE_COUNT; table_id++) {
541                 table = &state->table[table_id];
542                 for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
543                         if (!test_bit(filter_idx, table->used_bitmap))
544                                 continue;
545                         efx_filter_build(&filter, &table->spec[filter_idx]);
546                         efx_writeo(efx, &filter,
547                                    table->offset + table->step * filter_idx);
548                 }
549         }
550
551         efx_filter_push_rx_limits(efx);
552
553         spin_unlock_bh(&state->lock);
554 }
555
556 int efx_probe_filters(struct efx_nic *efx)
557 {
558         struct efx_filter_state *state;
559         struct efx_filter_table *table;
560         unsigned table_id;
561
562         state = kzalloc(sizeof(*efx->filter_state), GFP_KERNEL);
563         if (!state)
564                 return -ENOMEM;
565         efx->filter_state = state;
566
567         spin_lock_init(&state->lock);
568
569         if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
570                 table = &state->table[EFX_FILTER_TABLE_RX_IP];
571                 table->id = EFX_FILTER_TABLE_RX_IP;
572                 table->offset = FR_BZ_RX_FILTER_TBL0;
573                 table->size = FR_BZ_RX_FILTER_TBL0_ROWS;
574                 table->step = FR_BZ_RX_FILTER_TBL0_STEP;
575         }
576
577         if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
578                 table = &state->table[EFX_FILTER_TABLE_RX_MAC];
579                 table->id = EFX_FILTER_TABLE_RX_MAC;
580                 table->offset = FR_CZ_RX_MAC_FILTER_TBL0;
581                 table->size = FR_CZ_RX_MAC_FILTER_TBL0_ROWS;
582                 table->step = FR_CZ_RX_MAC_FILTER_TBL0_STEP;
583         }
584
585         for (table_id = 0; table_id < EFX_FILTER_TABLE_COUNT; table_id++) {
586                 table = &state->table[table_id];
587                 if (table->size == 0)
588                         continue;
589                 table->used_bitmap = kcalloc(BITS_TO_LONGS(table->size),
590                                              sizeof(unsigned long),
591                                              GFP_KERNEL);
592                 if (!table->used_bitmap)
593                         goto fail;
594                 table->spec = vzalloc(table->size * sizeof(*table->spec));
595                 if (!table->spec)
596                         goto fail;
597         }
598
599         return 0;
600
601 fail:
602         efx_remove_filters(efx);
603         return -ENOMEM;
604 }
605
606 void efx_remove_filters(struct efx_nic *efx)
607 {
608         struct efx_filter_state *state = efx->filter_state;
609         enum efx_filter_table_id table_id;
610
611         for (table_id = 0; table_id < EFX_FILTER_TABLE_COUNT; table_id++) {
612                 kfree(state->table[table_id].used_bitmap);
613                 vfree(state->table[table_id].spec);
614         }
615         kfree(state);
616 }