fm10k: Add interrupt support
[cascardo/linux.git] / drivers / net / ethernet / intel / fm10k / fm10k_netdev.c
1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2014 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20
21 #include "fm10k.h"
22
23 /**
24  * fm10k_request_glort_range - Request GLORTs for use in configuring rules
25  * @interface: board private structure
26  *
27  * This function allocates a range of glorts for this inteface to use.
28  **/
29 static void fm10k_request_glort_range(struct fm10k_intfc *interface)
30 {
31         struct fm10k_hw *hw = &interface->hw;
32         u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
33
34         /* establish GLORT base */
35         interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
36         interface->glort_count = 0;
37
38         /* nothing we can do until mask is allocated */
39         if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
40                 return;
41
42         interface->glort_count = mask + 1;
43 }
44
45 /**
46  * fm10k_open - Called when a network interface is made active
47  * @netdev: network interface device structure
48  *
49  * Returns 0 on success, negative value on failure
50  *
51  * The open entry point is called when a network interface is made
52  * active by the system (IFF_UP).  At this point all resources needed
53  * for transmit and receive operations are allocated, the interrupt
54  * handler is registered with the OS, the watchdog timer is started,
55  * and the stack is notified that the interface is ready.
56  **/
57 int fm10k_open(struct net_device *netdev)
58 {
59         struct fm10k_intfc *interface = netdev_priv(netdev);
60         int err;
61
62         /* allocate interrupt resources */
63         err = fm10k_qv_request_irq(interface);
64         if (err)
65                 goto err_req_irq;
66
67         /* setup GLORT assignment for this port */
68         fm10k_request_glort_range(interface);
69
70         fm10k_up(interface);
71
72         return 0;
73
74 err_req_irq:
75         return err;
76 }
77
78 /**
79  * fm10k_close - Disables a network interface
80  * @netdev: network interface device structure
81  *
82  * Returns 0, this is not allowed to fail
83  *
84  * The close entry point is called when an interface is de-activated
85  * by the OS.  The hardware is still under the drivers control, but
86  * needs to be disabled.  A global MAC reset is issued to stop the
87  * hardware, and all transmit and receive resources are freed.
88  **/
89 int fm10k_close(struct net_device *netdev)
90 {
91         struct fm10k_intfc *interface = netdev_priv(netdev);
92
93         fm10k_down(interface);
94
95         fm10k_qv_free_irq(interface);
96
97         return 0;
98 }
99
100 static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
101 {
102         dev_kfree_skb_any(skb);
103         return NETDEV_TX_OK;
104 }
105
106 static int fm10k_change_mtu(struct net_device *dev, int new_mtu)
107 {
108         if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE)
109                 return -EINVAL;
110
111         dev->mtu = new_mtu;
112
113         return 0;
114 }
115
116 static int fm10k_uc_vlan_unsync(struct net_device *netdev,
117                                 const unsigned char *uc_addr)
118 {
119         struct fm10k_intfc *interface = netdev_priv(netdev);
120         struct fm10k_hw *hw = &interface->hw;
121         u16 glort = interface->glort;
122         u16 vid = interface->vid;
123         bool set = !!(vid / VLAN_N_VID);
124         int err;
125
126         /* drop any leading bits on the VLAN ID */
127         vid &= VLAN_N_VID - 1;
128
129         err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0);
130         if (err)
131                 return err;
132
133         /* return non-zero value as we are only doing a partial sync/unsync */
134         return 1;
135 }
136
137 static int fm10k_mc_vlan_unsync(struct net_device *netdev,
138                                 const unsigned char *mc_addr)
139 {
140         struct fm10k_intfc *interface = netdev_priv(netdev);
141         struct fm10k_hw *hw = &interface->hw;
142         u16 glort = interface->glort;
143         u16 vid = interface->vid;
144         bool set = !!(vid / VLAN_N_VID);
145         int err;
146
147         /* drop any leading bits on the VLAN ID */
148         vid &= VLAN_N_VID - 1;
149
150         err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set);
151         if (err)
152                 return err;
153
154         /* return non-zero value as we are only doing a partial sync/unsync */
155         return 1;
156 }
157
158 static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
159 {
160         struct fm10k_intfc *interface = netdev_priv(netdev);
161         struct fm10k_hw *hw = &interface->hw;
162         s32 err;
163
164         /* updates do not apply to VLAN 0 */
165         if (!vid)
166                 return 0;
167
168         if (vid >= VLAN_N_VID)
169                 return -EINVAL;
170
171         /* Verify we have permission to add VLANs */
172         if (hw->mac.vlan_override)
173                 return -EACCES;
174
175         /* if default VLAN is already present do nothing */
176         if (vid == hw->mac.default_vid)
177                 return -EBUSY;
178
179         /* update active_vlans bitmask */
180         set_bit(vid, interface->active_vlans);
181         if (!set)
182                 clear_bit(vid, interface->active_vlans);
183
184         fm10k_mbx_lock(interface);
185
186         /* only need to update the VLAN if not in promiscous mode */
187         if (!(netdev->flags & IFF_PROMISC)) {
188                 err = hw->mac.ops.update_vlan(hw, vid, 0, set);
189                 if (err)
190                         return err;
191         }
192
193         /* update our base MAC address */
194         err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
195                                          vid, set, 0);
196         if (err)
197                 return err;
198
199         /* set vid prior to syncing/unsyncing the VLAN */
200         interface->vid = vid + (set ? VLAN_N_VID : 0);
201
202         /* Update the unicast and multicast address list to add/drop VLAN */
203         __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
204         __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
205
206         fm10k_mbx_unlock(interface);
207
208         return 0;
209 }
210
211 static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
212                                  __always_unused __be16 proto, u16 vid)
213 {
214         /* update VLAN and address table based on changes */
215         return fm10k_update_vid(netdev, vid, true);
216 }
217
218 static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
219                                   __always_unused __be16 proto, u16 vid)
220 {
221         /* update VLAN and address table based on changes */
222         return fm10k_update_vid(netdev, vid, false);
223 }
224
225 static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
226 {
227         struct fm10k_hw *hw = &interface->hw;
228         u16 default_vid = hw->mac.default_vid;
229         u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
230
231         vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
232
233         return vid;
234 }
235
236 static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
237 {
238         struct fm10k_hw *hw = &interface->hw;
239         u32 vid, prev_vid;
240
241         /* loop through and find any gaps in the table */
242         for (vid = 0, prev_vid = 0;
243              prev_vid < VLAN_N_VID;
244              prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
245                 if (prev_vid == vid)
246                         continue;
247
248                 /* send request to clear multiple bits at a time */
249                 prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
250                 hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
251         }
252 }
253
254 static int __fm10k_uc_sync(struct net_device *dev,
255                            const unsigned char *addr, bool sync)
256 {
257         struct fm10k_intfc *interface = netdev_priv(dev);
258         struct fm10k_hw *hw = &interface->hw;
259         u16 vid, glort = interface->glort;
260         s32 err;
261
262         if (!is_valid_ether_addr(addr))
263                 return -EADDRNOTAVAIL;
264
265         /* update table with current entries */
266         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
267              vid < VLAN_N_VID;
268              vid = fm10k_find_next_vlan(interface, vid)) {
269                 err = hw->mac.ops.update_uc_addr(hw, glort, addr,
270                                                   vid, sync, 0);
271                 if (err)
272                         return err;
273         }
274
275         return 0;
276 }
277
278 static int fm10k_uc_sync(struct net_device *dev,
279                          const unsigned char *addr)
280 {
281         return __fm10k_uc_sync(dev, addr, true);
282 }
283
284 static int fm10k_uc_unsync(struct net_device *dev,
285                            const unsigned char *addr)
286 {
287         return __fm10k_uc_sync(dev, addr, false);
288 }
289
290 static int fm10k_set_mac(struct net_device *dev, void *p)
291 {
292         struct fm10k_intfc *interface = netdev_priv(dev);
293         struct fm10k_hw *hw = &interface->hw;
294         struct sockaddr *addr = p;
295         s32 err = 0;
296
297         if (!is_valid_ether_addr(addr->sa_data))
298                 return -EADDRNOTAVAIL;
299
300         if (dev->flags & IFF_UP) {
301                 /* setting MAC address requires mailbox */
302                 fm10k_mbx_lock(interface);
303
304                 err = fm10k_uc_sync(dev, addr->sa_data);
305                 if (!err)
306                         fm10k_uc_unsync(dev, hw->mac.addr);
307
308                 fm10k_mbx_unlock(interface);
309         }
310
311         if (!err) {
312                 ether_addr_copy(dev->dev_addr, addr->sa_data);
313                 ether_addr_copy(hw->mac.addr, addr->sa_data);
314                 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
315         }
316
317         /* if we had a mailbox error suggest trying again */
318         return err ? -EAGAIN : 0;
319 }
320
321 static int __fm10k_mc_sync(struct net_device *dev,
322                            const unsigned char *addr, bool sync)
323 {
324         struct fm10k_intfc *interface = netdev_priv(dev);
325         struct fm10k_hw *hw = &interface->hw;
326         u16 vid, glort = interface->glort;
327         s32 err;
328
329         if (!is_multicast_ether_addr(addr))
330                 return -EADDRNOTAVAIL;
331
332         /* update table with current entries */
333         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
334              vid < VLAN_N_VID;
335              vid = fm10k_find_next_vlan(interface, vid)) {
336                 err = hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
337                 if (err)
338                         return err;
339         }
340
341         return 0;
342 }
343
344 static int fm10k_mc_sync(struct net_device *dev,
345                          const unsigned char *addr)
346 {
347         return __fm10k_mc_sync(dev, addr, true);
348 }
349
350 static int fm10k_mc_unsync(struct net_device *dev,
351                            const unsigned char *addr)
352 {
353         return __fm10k_mc_sync(dev, addr, false);
354 }
355
356 static void fm10k_set_rx_mode(struct net_device *dev)
357 {
358         struct fm10k_intfc *interface = netdev_priv(dev);
359         struct fm10k_hw *hw = &interface->hw;
360         int xcast_mode;
361
362         /* no need to update the harwdare if we are not running */
363         if (!(dev->flags & IFF_UP))
364                 return;
365
366         /* determine new mode based on flags */
367         xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
368                      (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
369                      (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
370                      FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
371
372         fm10k_mbx_lock(interface);
373
374         /* syncronize all of the addresses */
375         if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
376                 __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
377                 if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
378                         __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
379         }
380
381         /* if we aren't changing modes there is nothing to do */
382         if (interface->xcast_mode != xcast_mode) {
383                 /* update VLAN table */
384                 if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
385                         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
386                 if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
387                         fm10k_clear_unused_vlans(interface);
388
389                 /* update xcast mode */
390                 hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
391
392                 /* record updated xcast mode state */
393                 interface->xcast_mode = xcast_mode;
394         }
395
396         fm10k_mbx_unlock(interface);
397 }
398
399 void fm10k_restore_rx_state(struct fm10k_intfc *interface)
400 {
401         struct net_device *netdev = interface->netdev;
402         struct fm10k_hw *hw = &interface->hw;
403         int xcast_mode;
404         u16 vid, glort;
405
406         /* record glort for this interface */
407         glort = interface->glort;
408
409         /* convert interface flags to xcast mode */
410         if (netdev->flags & IFF_PROMISC)
411                 xcast_mode = FM10K_XCAST_MODE_PROMISC;
412         else if (netdev->flags & IFF_ALLMULTI)
413                 xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
414         else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
415                 xcast_mode = FM10K_XCAST_MODE_MULTI;
416         else
417                 xcast_mode = FM10K_XCAST_MODE_NONE;
418
419         fm10k_mbx_lock(interface);
420
421         /* Enable logical port */
422         hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
423
424         /* update VLAN table */
425         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
426                                 xcast_mode == FM10K_XCAST_MODE_PROMISC);
427
428         /* Add filter for VLAN 0 */
429         hw->mac.ops.update_vlan(hw, 0, 0, true);
430
431         /* update table with current entries */
432         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
433              vid < VLAN_N_VID;
434              vid = fm10k_find_next_vlan(interface, vid)) {
435                 hw->mac.ops.update_vlan(hw, vid, 0, true);
436                 hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
437                                            vid, true, 0);
438         }
439
440         /* syncronize all of the addresses */
441         if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
442                 __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
443                 if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
444                         __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
445         }
446
447         /* update xcast mode */
448         hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
449
450         fm10k_mbx_unlock(interface);
451
452         /* record updated xcast mode state */
453         interface->xcast_mode = xcast_mode;
454 }
455
456 void fm10k_reset_rx_state(struct fm10k_intfc *interface)
457 {
458         struct net_device *netdev = interface->netdev;
459         struct fm10k_hw *hw = &interface->hw;
460
461         fm10k_mbx_lock(interface);
462
463         /* clear the logical port state on lower device */
464         hw->mac.ops.update_lport_state(hw, interface->glort,
465                                        interface->glort_count, false);
466
467         fm10k_mbx_unlock(interface);
468
469         /* reset flags to default state */
470         interface->xcast_mode = FM10K_XCAST_MODE_NONE;
471
472         /* clear the sync flag since the lport has been dropped */
473         __dev_uc_unsync(netdev, NULL);
474         __dev_mc_unsync(netdev, NULL);
475 }
476
477 static const struct net_device_ops fm10k_netdev_ops = {
478         .ndo_open               = fm10k_open,
479         .ndo_stop               = fm10k_close,
480         .ndo_validate_addr      = eth_validate_addr,
481         .ndo_start_xmit         = fm10k_xmit_frame,
482         .ndo_set_mac_address    = fm10k_set_mac,
483         .ndo_change_mtu         = fm10k_change_mtu,
484         .ndo_vlan_rx_add_vid    = fm10k_vlan_rx_add_vid,
485         .ndo_vlan_rx_kill_vid   = fm10k_vlan_rx_kill_vid,
486         .ndo_set_rx_mode        = fm10k_set_rx_mode,
487 };
488
489 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
490
491 struct net_device *fm10k_alloc_netdev(void)
492 {
493         struct fm10k_intfc *interface;
494         struct net_device *dev;
495
496         dev = alloc_etherdev(sizeof(struct fm10k_intfc));
497         if (!dev)
498                 return NULL;
499
500         /* set net device and ethtool ops */
501         dev->netdev_ops = &fm10k_netdev_ops;
502
503         /* configure default debug level */
504         interface = netdev_priv(dev);
505         interface->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
506
507         /* configure default features */
508         dev->features |= NETIF_F_SG;
509
510         /* all features defined to this point should be changeable */
511         dev->hw_features |= dev->features;
512
513         /* configure VLAN features */
514         dev->vlan_features |= dev->features;
515
516         /* configure tunnel offloads */
517         dev->hw_enc_features = NETIF_F_SG;
518
519         /* we want to leave these both on as we cannot disable VLAN tag
520          * insertion or stripping on the hardware since it is contained
521          * in the FTAG and not in the frame itself.
522          */
523         dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
524                          NETIF_F_HW_VLAN_CTAG_RX |
525                          NETIF_F_HW_VLAN_CTAG_FILTER;
526
527         dev->priv_flags |= IFF_UNICAST_FLT;
528
529         return dev;
530 }