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
23 static netdev_tx_t
fm10k_xmit_frame(struct sk_buff
*skb
, struct net_device
*dev
)
25 dev_kfree_skb_any(skb
);
29 static int fm10k_change_mtu(struct net_device
*dev
, int new_mtu
)
31 if (new_mtu
< 68 || new_mtu
> FM10K_MAX_JUMBO_FRAME_SIZE
)
39 static int fm10k_uc_vlan_unsync(struct net_device
*netdev
,
40 const unsigned char *uc_addr
)
42 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
43 struct fm10k_hw
*hw
= &interface
->hw
;
44 u16 glort
= interface
->glort
;
45 u16 vid
= interface
->vid
;
46 bool set
= !!(vid
/ VLAN_N_VID
);
49 /* drop any leading bits on the VLAN ID */
50 vid
&= VLAN_N_VID
- 1;
52 err
= hw
->mac
.ops
.update_uc_addr(hw
, glort
, uc_addr
, vid
, set
, 0);
56 /* return non-zero value as we are only doing a partial sync/unsync */
60 static int fm10k_mc_vlan_unsync(struct net_device
*netdev
,
61 const unsigned char *mc_addr
)
63 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
64 struct fm10k_hw
*hw
= &interface
->hw
;
65 u16 glort
= interface
->glort
;
66 u16 vid
= interface
->vid
;
67 bool set
= !!(vid
/ VLAN_N_VID
);
70 /* drop any leading bits on the VLAN ID */
71 vid
&= VLAN_N_VID
- 1;
73 err
= hw
->mac
.ops
.update_mc_addr(hw
, glort
, mc_addr
, vid
, set
);
77 /* return non-zero value as we are only doing a partial sync/unsync */
81 static int fm10k_update_vid(struct net_device
*netdev
, u16 vid
, bool set
)
83 struct fm10k_intfc
*interface
= netdev_priv(netdev
);
84 struct fm10k_hw
*hw
= &interface
->hw
;
87 /* updates do not apply to VLAN 0 */
91 if (vid
>= VLAN_N_VID
)
94 /* Verify we have permission to add VLANs */
95 if (hw
->mac
.vlan_override
)
98 /* if default VLAN is already present do nothing */
99 if (vid
== hw
->mac
.default_vid
)
102 /* update active_vlans bitmask */
103 set_bit(vid
, interface
->active_vlans
);
105 clear_bit(vid
, interface
->active_vlans
);
107 fm10k_mbx_lock(interface
);
109 /* only need to update the VLAN if not in promiscous mode */
110 if (!(netdev
->flags
& IFF_PROMISC
)) {
111 err
= hw
->mac
.ops
.update_vlan(hw
, vid
, 0, set
);
116 /* update our base MAC address */
117 err
= hw
->mac
.ops
.update_uc_addr(hw
, interface
->glort
, hw
->mac
.addr
,
122 /* set vid prior to syncing/unsyncing the VLAN */
123 interface
->vid
= vid
+ (set
? VLAN_N_VID
: 0);
125 /* Update the unicast and multicast address list to add/drop VLAN */
126 __dev_uc_unsync(netdev
, fm10k_uc_vlan_unsync
);
127 __dev_mc_unsync(netdev
, fm10k_mc_vlan_unsync
);
129 fm10k_mbx_unlock(interface
);
134 static int fm10k_vlan_rx_add_vid(struct net_device
*netdev
,
135 __always_unused __be16 proto
, u16 vid
)
137 /* update VLAN and address table based on changes */
138 return fm10k_update_vid(netdev
, vid
, true);
141 static int fm10k_vlan_rx_kill_vid(struct net_device
*netdev
,
142 __always_unused __be16 proto
, u16 vid
)
144 /* update VLAN and address table based on changes */
145 return fm10k_update_vid(netdev
, vid
, false);
148 static u16
fm10k_find_next_vlan(struct fm10k_intfc
*interface
, u16 vid
)
150 struct fm10k_hw
*hw
= &interface
->hw
;
151 u16 default_vid
= hw
->mac
.default_vid
;
152 u16 vid_limit
= vid
< default_vid
? default_vid
: VLAN_N_VID
;
154 vid
= find_next_bit(interface
->active_vlans
, vid_limit
, ++vid
);
159 static void fm10k_clear_unused_vlans(struct fm10k_intfc
*interface
)
161 struct fm10k_hw
*hw
= &interface
->hw
;
164 /* loop through and find any gaps in the table */
165 for (vid
= 0, prev_vid
= 0;
166 prev_vid
< VLAN_N_VID
;
167 prev_vid
= vid
+ 1, vid
= fm10k_find_next_vlan(interface
, vid
)) {
171 /* send request to clear multiple bits at a time */
172 prev_vid
+= (vid
- prev_vid
- 1) << FM10K_VLAN_LENGTH_SHIFT
;
173 hw
->mac
.ops
.update_vlan(hw
, prev_vid
, 0, false);
177 static int __fm10k_uc_sync(struct net_device
*dev
,
178 const unsigned char *addr
, bool sync
)
180 struct fm10k_intfc
*interface
= netdev_priv(dev
);
181 struct fm10k_hw
*hw
= &interface
->hw
;
182 u16 vid
, glort
= interface
->glort
;
185 if (!is_valid_ether_addr(addr
))
186 return -EADDRNOTAVAIL
;
188 /* update table with current entries */
189 for (vid
= hw
->mac
.default_vid
? fm10k_find_next_vlan(interface
, 0) : 0;
191 vid
= fm10k_find_next_vlan(interface
, vid
)) {
192 err
= hw
->mac
.ops
.update_uc_addr(hw
, glort
, addr
,
201 static int fm10k_uc_sync(struct net_device
*dev
,
202 const unsigned char *addr
)
204 return __fm10k_uc_sync(dev
, addr
, true);
207 static int fm10k_uc_unsync(struct net_device
*dev
,
208 const unsigned char *addr
)
210 return __fm10k_uc_sync(dev
, addr
, false);
213 static int fm10k_set_mac(struct net_device
*dev
, void *p
)
215 struct fm10k_intfc
*interface
= netdev_priv(dev
);
216 struct fm10k_hw
*hw
= &interface
->hw
;
217 struct sockaddr
*addr
= p
;
220 if (!is_valid_ether_addr(addr
->sa_data
))
221 return -EADDRNOTAVAIL
;
223 if (dev
->flags
& IFF_UP
) {
224 /* setting MAC address requires mailbox */
225 fm10k_mbx_lock(interface
);
227 err
= fm10k_uc_sync(dev
, addr
->sa_data
);
229 fm10k_uc_unsync(dev
, hw
->mac
.addr
);
231 fm10k_mbx_unlock(interface
);
235 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
236 ether_addr_copy(hw
->mac
.addr
, addr
->sa_data
);
237 dev
->addr_assign_type
&= ~NET_ADDR_RANDOM
;
240 /* if we had a mailbox error suggest trying again */
241 return err
? -EAGAIN
: 0;
244 static int __fm10k_mc_sync(struct net_device
*dev
,
245 const unsigned char *addr
, bool sync
)
247 struct fm10k_intfc
*interface
= netdev_priv(dev
);
248 struct fm10k_hw
*hw
= &interface
->hw
;
249 u16 vid
, glort
= interface
->glort
;
252 if (!is_multicast_ether_addr(addr
))
253 return -EADDRNOTAVAIL
;
255 /* update table with current entries */
256 for (vid
= hw
->mac
.default_vid
? fm10k_find_next_vlan(interface
, 0) : 0;
258 vid
= fm10k_find_next_vlan(interface
, vid
)) {
259 err
= hw
->mac
.ops
.update_mc_addr(hw
, glort
, addr
, vid
, sync
);
267 static int fm10k_mc_sync(struct net_device
*dev
,
268 const unsigned char *addr
)
270 return __fm10k_mc_sync(dev
, addr
, true);
273 static int fm10k_mc_unsync(struct net_device
*dev
,
274 const unsigned char *addr
)
276 return __fm10k_mc_sync(dev
, addr
, false);
279 static void fm10k_set_rx_mode(struct net_device
*dev
)
281 struct fm10k_intfc
*interface
= netdev_priv(dev
);
282 struct fm10k_hw
*hw
= &interface
->hw
;
285 /* no need to update the harwdare if we are not running */
286 if (!(dev
->flags
& IFF_UP
))
289 /* determine new mode based on flags */
290 xcast_mode
= (dev
->flags
& IFF_PROMISC
) ? FM10K_XCAST_MODE_PROMISC
:
291 (dev
->flags
& IFF_ALLMULTI
) ? FM10K_XCAST_MODE_ALLMULTI
:
292 (dev
->flags
& (IFF_BROADCAST
| IFF_MULTICAST
)) ?
293 FM10K_XCAST_MODE_MULTI
: FM10K_XCAST_MODE_NONE
;
295 fm10k_mbx_lock(interface
);
297 /* syncronize all of the addresses */
298 if (xcast_mode
!= FM10K_XCAST_MODE_PROMISC
) {
299 __dev_uc_sync(dev
, fm10k_uc_sync
, fm10k_uc_unsync
);
300 if (xcast_mode
!= FM10K_XCAST_MODE_ALLMULTI
)
301 __dev_mc_sync(dev
, fm10k_mc_sync
, fm10k_mc_unsync
);
304 /* if we aren't changing modes there is nothing to do */
305 if (interface
->xcast_mode
!= xcast_mode
) {
306 /* update VLAN table */
307 if (xcast_mode
== FM10K_XCAST_MODE_PROMISC
)
308 hw
->mac
.ops
.update_vlan(hw
, FM10K_VLAN_ALL
, 0, true);
309 if (interface
->xcast_mode
== FM10K_XCAST_MODE_PROMISC
)
310 fm10k_clear_unused_vlans(interface
);
312 /* update xcast mode */
313 hw
->mac
.ops
.update_xcast_mode(hw
, interface
->glort
, xcast_mode
);
315 /* record updated xcast mode state */
316 interface
->xcast_mode
= xcast_mode
;
319 fm10k_mbx_unlock(interface
);
322 void fm10k_restore_rx_state(struct fm10k_intfc
*interface
)
324 struct net_device
*netdev
= interface
->netdev
;
325 struct fm10k_hw
*hw
= &interface
->hw
;
329 /* record glort for this interface */
330 glort
= interface
->glort
;
332 /* convert interface flags to xcast mode */
333 if (netdev
->flags
& IFF_PROMISC
)
334 xcast_mode
= FM10K_XCAST_MODE_PROMISC
;
335 else if (netdev
->flags
& IFF_ALLMULTI
)
336 xcast_mode
= FM10K_XCAST_MODE_ALLMULTI
;
337 else if (netdev
->flags
& (IFF_BROADCAST
| IFF_MULTICAST
))
338 xcast_mode
= FM10K_XCAST_MODE_MULTI
;
340 xcast_mode
= FM10K_XCAST_MODE_NONE
;
342 fm10k_mbx_lock(interface
);
344 /* Enable logical port */
345 hw
->mac
.ops
.update_lport_state(hw
, glort
, interface
->glort_count
, true);
347 /* update VLAN table */
348 hw
->mac
.ops
.update_vlan(hw
, FM10K_VLAN_ALL
, 0,
349 xcast_mode
== FM10K_XCAST_MODE_PROMISC
);
351 /* Add filter for VLAN 0 */
352 hw
->mac
.ops
.update_vlan(hw
, 0, 0, true);
354 /* update table with current entries */
355 for (vid
= hw
->mac
.default_vid
? fm10k_find_next_vlan(interface
, 0) : 0;
357 vid
= fm10k_find_next_vlan(interface
, vid
)) {
358 hw
->mac
.ops
.update_vlan(hw
, vid
, 0, true);
359 hw
->mac
.ops
.update_uc_addr(hw
, glort
, hw
->mac
.addr
,
363 /* syncronize all of the addresses */
364 if (xcast_mode
!= FM10K_XCAST_MODE_PROMISC
) {
365 __dev_uc_sync(netdev
, fm10k_uc_sync
, fm10k_uc_unsync
);
366 if (xcast_mode
!= FM10K_XCAST_MODE_ALLMULTI
)
367 __dev_mc_sync(netdev
, fm10k_mc_sync
, fm10k_mc_unsync
);
370 /* update xcast mode */
371 hw
->mac
.ops
.update_xcast_mode(hw
, glort
, xcast_mode
);
373 fm10k_mbx_unlock(interface
);
375 /* record updated xcast mode state */
376 interface
->xcast_mode
= xcast_mode
;
379 void fm10k_reset_rx_state(struct fm10k_intfc
*interface
)
381 struct net_device
*netdev
= interface
->netdev
;
382 struct fm10k_hw
*hw
= &interface
->hw
;
384 fm10k_mbx_lock(interface
);
386 /* clear the logical port state on lower device */
387 hw
->mac
.ops
.update_lport_state(hw
, interface
->glort
,
388 interface
->glort_count
, false);
390 fm10k_mbx_unlock(interface
);
392 /* reset flags to default state */
393 interface
->xcast_mode
= FM10K_XCAST_MODE_NONE
;
395 /* clear the sync flag since the lport has been dropped */
396 __dev_uc_unsync(netdev
, NULL
);
397 __dev_mc_unsync(netdev
, NULL
);
400 static const struct net_device_ops fm10k_netdev_ops
= {
401 .ndo_validate_addr
= eth_validate_addr
,
402 .ndo_start_xmit
= fm10k_xmit_frame
,
403 .ndo_set_mac_address
= fm10k_set_mac
,
404 .ndo_change_mtu
= fm10k_change_mtu
,
405 .ndo_vlan_rx_add_vid
= fm10k_vlan_rx_add_vid
,
406 .ndo_vlan_rx_kill_vid
= fm10k_vlan_rx_kill_vid
,
407 .ndo_set_rx_mode
= fm10k_set_rx_mode
,
410 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
412 struct net_device
*fm10k_alloc_netdev(void)
414 struct fm10k_intfc
*interface
;
415 struct net_device
*dev
;
417 dev
= alloc_etherdev(sizeof(struct fm10k_intfc
));
421 /* set net device and ethtool ops */
422 dev
->netdev_ops
= &fm10k_netdev_ops
;
424 /* configure default debug level */
425 interface
= netdev_priv(dev
);
426 interface
->msg_enable
= (1 << DEFAULT_DEBUG_LEVEL_SHIFT
) - 1;
428 /* configure default features */
429 dev
->features
|= NETIF_F_SG
;
431 /* all features defined to this point should be changeable */
432 dev
->hw_features
|= dev
->features
;
434 /* configure VLAN features */
435 dev
->vlan_features
|= dev
->features
;
437 /* configure tunnel offloads */
438 dev
->hw_enc_features
= NETIF_F_SG
;
440 /* we want to leave these both on as we cannot disable VLAN tag
441 * insertion or stripping on the hardware since it is contained
442 * in the FTAG and not in the frame itself.
444 dev
->features
|= NETIF_F_HW_VLAN_CTAG_TX
|
445 NETIF_F_HW_VLAN_CTAG_RX
|
446 NETIF_F_HW_VLAN_CTAG_FILTER
;
448 dev
->priv_flags
|= IFF_UNICAST_FLT
;