1 /* Intel Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2014 Intel Corporation.
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.
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
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
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
24 * fm10k_request_glort_range - Request GLORTs for use in configuring rules
25 * @interface: board private structure
27 * This function allocates a range of glorts for this inteface to use.
29 static void fm10k_request_glort_range(struct fm10k_intfc
*interface
)
31 struct fm10k_hw
*hw
= &interface
->hw
;
32 u16 mask
= (~hw
->mac
.dglort_map
) >> FM10K_DGLORTMAP_MASK_SHIFT
;
34 /* establish GLORT base */
35 interface
->glort
= hw
->mac
.dglort_map
& FM10K_DGLORTMAP_NONE
;
36 interface
->glort_count
= 0;
38 /* nothing we can do until mask is allocated */
39 if (hw
->mac
.dglort_map
== FM10K_DGLORTMAP_NONE
)
42 interface
->glort_count
= mask
+ 1;
46 * fm10k_open - Called when a network interface is made active
47 * @netdev: network interface device structure
49 * Returns 0 on success, negative value on failure
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.
57 int fm10k_open(struct net_device
*netdev
)
59 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
62 /* allocate interrupt resources */
63 err
= fm10k_qv_request_irq(interface
);
67 /* setup GLORT assignment for this port */
68 fm10k_request_glort_range(interface
);
79 * fm10k_close - Disables a network interface
80 * @netdev: network interface device structure
82 * Returns 0, this is not allowed to fail
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.
89 int fm10k_close(struct net_device
*netdev
)
91 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
93 fm10k_down(interface
);
95 fm10k_qv_free_irq(interface
);
100 static netdev_tx_t
fm10k_xmit_frame(struct sk_buff
*skb
, struct net_device
*dev
)
102 dev_kfree_skb_any(skb
);
106 static int fm10k_change_mtu(struct net_device
*dev
, int new_mtu
)
108 if (new_mtu
< 68 || new_mtu
> FM10K_MAX_JUMBO_FRAME_SIZE
)
116 static int fm10k_uc_vlan_unsync(struct net_device
*netdev
,
117 const unsigned char *uc_addr
)
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
);
126 /* drop any leading bits on the VLAN ID */
127 vid
&= VLAN_N_VID
- 1;
129 err
= hw
->mac
.ops
.update_uc_addr(hw
, glort
, uc_addr
, vid
, set
, 0);
133 /* return non-zero value as we are only doing a partial sync/unsync */
137 static int fm10k_mc_vlan_unsync(struct net_device
*netdev
,
138 const unsigned char *mc_addr
)
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
);
147 /* drop any leading bits on the VLAN ID */
148 vid
&= VLAN_N_VID
- 1;
150 err
= hw
->mac
.ops
.update_mc_addr(hw
, glort
, mc_addr
, vid
, set
);
154 /* return non-zero value as we are only doing a partial sync/unsync */
158 static int fm10k_update_vid(struct net_device
*netdev
, u16 vid
, bool set
)
160 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
161 struct fm10k_hw
*hw
= &interface
->hw
;
164 /* updates do not apply to VLAN 0 */
168 if (vid
>= VLAN_N_VID
)
171 /* Verify we have permission to add VLANs */
172 if (hw
->mac
.vlan_override
)
175 /* if default VLAN is already present do nothing */
176 if (vid
== hw
->mac
.default_vid
)
179 /* update active_vlans bitmask */
180 set_bit(vid
, interface
->active_vlans
);
182 clear_bit(vid
, interface
->active_vlans
);
184 fm10k_mbx_lock(interface
);
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
);
193 /* update our base MAC address */
194 err
= hw
->mac
.ops
.update_uc_addr(hw
, interface
->glort
, hw
->mac
.addr
,
199 /* set vid prior to syncing/unsyncing the VLAN */
200 interface
->vid
= vid
+ (set
? VLAN_N_VID
: 0);
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
);
206 fm10k_mbx_unlock(interface
);
211 static int fm10k_vlan_rx_add_vid(struct net_device
*netdev
,
212 __always_unused __be16 proto
, u16 vid
)
214 /* update VLAN and address table based on changes */
215 return fm10k_update_vid(netdev
, vid
, true);
218 static int fm10k_vlan_rx_kill_vid(struct net_device
*netdev
,
219 __always_unused __be16 proto
, u16 vid
)
221 /* update VLAN and address table based on changes */
222 return fm10k_update_vid(netdev
, vid
, false);
225 static u16
fm10k_find_next_vlan(struct fm10k_intfc
*interface
, u16 vid
)
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
;
231 vid
= find_next_bit(interface
->active_vlans
, vid_limit
, ++vid
);
236 static void fm10k_clear_unused_vlans(struct fm10k_intfc
*interface
)
238 struct fm10k_hw
*hw
= &interface
->hw
;
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
)) {
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);
254 static int __fm10k_uc_sync(struct net_device
*dev
,
255 const unsigned char *addr
, bool sync
)
257 struct fm10k_intfc
*interface
= netdev_priv(dev
);
258 struct fm10k_hw
*hw
= &interface
->hw
;
259 u16 vid
, glort
= interface
->glort
;
262 if (!is_valid_ether_addr(addr
))
263 return -EADDRNOTAVAIL
;
265 /* update table with current entries */
266 for (vid
= hw
->mac
.default_vid
? fm10k_find_next_vlan(interface
, 0) : 0;
268 vid
= fm10k_find_next_vlan(interface
, vid
)) {
269 err
= hw
->mac
.ops
.update_uc_addr(hw
, glort
, addr
,
278 static int fm10k_uc_sync(struct net_device
*dev
,
279 const unsigned char *addr
)
281 return __fm10k_uc_sync(dev
, addr
, true);
284 static int fm10k_uc_unsync(struct net_device
*dev
,
285 const unsigned char *addr
)
287 return __fm10k_uc_sync(dev
, addr
, false);
290 static int fm10k_set_mac(struct net_device
*dev
, void *p
)
292 struct fm10k_intfc
*interface
= netdev_priv(dev
);
293 struct fm10k_hw
*hw
= &interface
->hw
;
294 struct sockaddr
*addr
= p
;
297 if (!is_valid_ether_addr(addr
->sa_data
))
298 return -EADDRNOTAVAIL
;
300 if (dev
->flags
& IFF_UP
) {
301 /* setting MAC address requires mailbox */
302 fm10k_mbx_lock(interface
);
304 err
= fm10k_uc_sync(dev
, addr
->sa_data
);
306 fm10k_uc_unsync(dev
, hw
->mac
.addr
);
308 fm10k_mbx_unlock(interface
);
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
;
317 /* if we had a mailbox error suggest trying again */
318 return err
? -EAGAIN
: 0;
321 static int __fm10k_mc_sync(struct net_device
*dev
,
322 const unsigned char *addr
, bool sync
)
324 struct fm10k_intfc
*interface
= netdev_priv(dev
);
325 struct fm10k_hw
*hw
= &interface
->hw
;
326 u16 vid
, glort
= interface
->glort
;
329 if (!is_multicast_ether_addr(addr
))
330 return -EADDRNOTAVAIL
;
332 /* update table with current entries */
333 for (vid
= hw
->mac
.default_vid
? fm10k_find_next_vlan(interface
, 0) : 0;
335 vid
= fm10k_find_next_vlan(interface
, vid
)) {
336 err
= hw
->mac
.ops
.update_mc_addr(hw
, glort
, addr
, vid
, sync
);
344 static int fm10k_mc_sync(struct net_device
*dev
,
345 const unsigned char *addr
)
347 return __fm10k_mc_sync(dev
, addr
, true);
350 static int fm10k_mc_unsync(struct net_device
*dev
,
351 const unsigned char *addr
)
353 return __fm10k_mc_sync(dev
, addr
, false);
356 static void fm10k_set_rx_mode(struct net_device
*dev
)
358 struct fm10k_intfc
*interface
= netdev_priv(dev
);
359 struct fm10k_hw
*hw
= &interface
->hw
;
362 /* no need to update the harwdare if we are not running */
363 if (!(dev
->flags
& IFF_UP
))
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
;
372 fm10k_mbx_lock(interface
);
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
);
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
);
389 /* update xcast mode */
390 hw
->mac
.ops
.update_xcast_mode(hw
, interface
->glort
, xcast_mode
);
392 /* record updated xcast mode state */
393 interface
->xcast_mode
= xcast_mode
;
396 fm10k_mbx_unlock(interface
);
399 void fm10k_restore_rx_state(struct fm10k_intfc
*interface
)
401 struct net_device
*netdev
= interface
->netdev
;
402 struct fm10k_hw
*hw
= &interface
->hw
;
406 /* record glort for this interface */
407 glort
= interface
->glort
;
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
;
417 xcast_mode
= FM10K_XCAST_MODE_NONE
;
419 fm10k_mbx_lock(interface
);
421 /* Enable logical port */
422 hw
->mac
.ops
.update_lport_state(hw
, glort
, interface
->glort_count
, true);
424 /* update VLAN table */
425 hw
->mac
.ops
.update_vlan(hw
, FM10K_VLAN_ALL
, 0,
426 xcast_mode
== FM10K_XCAST_MODE_PROMISC
);
428 /* Add filter for VLAN 0 */
429 hw
->mac
.ops
.update_vlan(hw
, 0, 0, true);
431 /* update table with current entries */
432 for (vid
= hw
->mac
.default_vid
? fm10k_find_next_vlan(interface
, 0) : 0;
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
,
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
);
447 /* update xcast mode */
448 hw
->mac
.ops
.update_xcast_mode(hw
, glort
, xcast_mode
);
450 fm10k_mbx_unlock(interface
);
452 /* record updated xcast mode state */
453 interface
->xcast_mode
= xcast_mode
;
456 void fm10k_reset_rx_state(struct fm10k_intfc
*interface
)
458 struct net_device
*netdev
= interface
->netdev
;
459 struct fm10k_hw
*hw
= &interface
->hw
;
461 fm10k_mbx_lock(interface
);
463 /* clear the logical port state on lower device */
464 hw
->mac
.ops
.update_lport_state(hw
, interface
->glort
,
465 interface
->glort_count
, false);
467 fm10k_mbx_unlock(interface
);
469 /* reset flags to default state */
470 interface
->xcast_mode
= FM10K_XCAST_MODE_NONE
;
472 /* clear the sync flag since the lport has been dropped */
473 __dev_uc_unsync(netdev
, NULL
);
474 __dev_mc_unsync(netdev
, NULL
);
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
,
489 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
491 struct net_device
*fm10k_alloc_netdev(void)
493 struct fm10k_intfc
*interface
;
494 struct net_device
*dev
;
496 dev
= alloc_etherdev(sizeof(struct fm10k_intfc
));
500 /* set net device and ethtool ops */
501 dev
->netdev_ops
= &fm10k_netdev_ops
;
503 /* configure default debug level */
504 interface
= netdev_priv(dev
);
505 interface
->msg_enable
= (1 << DEFAULT_DEBUG_LEVEL_SHIFT
) - 1;
507 /* configure default features */
508 dev
->features
|= NETIF_F_SG
;
510 /* all features defined to this point should be changeable */
511 dev
->hw_features
|= dev
->features
;
513 /* configure VLAN features */
514 dev
->vlan_features
|= dev
->features
;
516 /* configure tunnel offloads */
517 dev
->hw_enc_features
= NETIF_F_SG
;
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.
523 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
|
524 NETIF_F_HW_VLAN_CTAG_RX
|
525 NETIF_F_HW_VLAN_CTAG_FILTER
;
527 dev
->priv_flags
|= IFF_UNICAST_FLT
;