xref: /OK3568_Linux_fs/kernel/Documentation/networking/driver.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun=====================
4*4882a593SmuzhiyunSoftnet Driver Issues
5*4882a593Smuzhiyun=====================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunTransmit path guidelines:
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun1) The ndo_start_xmit method must not return NETDEV_TX_BUSY under
10*4882a593Smuzhiyun   any normal circumstances.  It is considered a hard error unless
11*4882a593Smuzhiyun   there is no way your device can tell ahead of time when it's
12*4882a593Smuzhiyun   transmit function will become busy.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun   Instead it must maintain the queue properly.  For example,
15*4882a593Smuzhiyun   for a driver implementing scatter-gather this means::
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun	static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
18*4882a593Smuzhiyun					       struct net_device *dev)
19*4882a593Smuzhiyun	{
20*4882a593Smuzhiyun		struct drv *dp = netdev_priv(dev);
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun		lock_tx(dp);
23*4882a593Smuzhiyun		...
24*4882a593Smuzhiyun		/* This is a hard error log it. */
25*4882a593Smuzhiyun		if (TX_BUFFS_AVAIL(dp) <= (skb_shinfo(skb)->nr_frags + 1)) {
26*4882a593Smuzhiyun			netif_stop_queue(dev);
27*4882a593Smuzhiyun			unlock_tx(dp);
28*4882a593Smuzhiyun			printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
29*4882a593Smuzhiyun			       dev->name);
30*4882a593Smuzhiyun			return NETDEV_TX_BUSY;
31*4882a593Smuzhiyun		}
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun		... queue packet to card ...
34*4882a593Smuzhiyun		... update tx consumer index ...
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun		if (TX_BUFFS_AVAIL(dp) <= (MAX_SKB_FRAGS + 1))
37*4882a593Smuzhiyun			netif_stop_queue(dev);
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun		...
40*4882a593Smuzhiyun		unlock_tx(dp);
41*4882a593Smuzhiyun		...
42*4882a593Smuzhiyun		return NETDEV_TX_OK;
43*4882a593Smuzhiyun	}
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun   And then at the end of your TX reclamation event handling::
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun	if (netif_queue_stopped(dp->dev) &&
48*4882a593Smuzhiyun	    TX_BUFFS_AVAIL(dp) > (MAX_SKB_FRAGS + 1))
49*4882a593Smuzhiyun		netif_wake_queue(dp->dev);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun   For a non-scatter-gather supporting card, the three tests simply become::
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun		/* This is a hard error log it. */
54*4882a593Smuzhiyun		if (TX_BUFFS_AVAIL(dp) <= 0)
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun   and::
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun		if (TX_BUFFS_AVAIL(dp) == 0)
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun   and::
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun	if (netif_queue_stopped(dp->dev) &&
63*4882a593Smuzhiyun	    TX_BUFFS_AVAIL(dp) > 0)
64*4882a593Smuzhiyun		netif_wake_queue(dp->dev);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun2) An ndo_start_xmit method must not modify the shared parts of a
67*4882a593Smuzhiyun   cloned SKB.
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun3) Do not forget that once you return NETDEV_TX_OK from your
70*4882a593Smuzhiyun   ndo_start_xmit method, it is your driver's responsibility to free
71*4882a593Smuzhiyun   up the SKB and in some finite amount of time.
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun   For example, this means that it is not allowed for your TX
74*4882a593Smuzhiyun   mitigation scheme to let TX packets "hang out" in the TX
75*4882a593Smuzhiyun   ring unreclaimed forever if no new TX packets are sent.
76*4882a593Smuzhiyun   This error can deadlock sockets waiting for send buffer room
77*4882a593Smuzhiyun   to be freed up.
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun   If you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
80*4882a593Smuzhiyun   must not keep any reference to that SKB and you must not attempt
81*4882a593Smuzhiyun   to free it up.
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunProbing guidelines:
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun1) Any hardware layer address you obtain for your device should
86*4882a593Smuzhiyun   be verified.  For example, for ethernet check it with
87*4882a593Smuzhiyun   linux/etherdevice.h:is_valid_ether_addr()
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunClose/stop guidelines:
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun1) After the ndo_stop routine has been called, the hardware must
92*4882a593Smuzhiyun   not receive or transmit any data.  All in flight packets must
93*4882a593Smuzhiyun   be aborted. If necessary, poll or wait for completion of
94*4882a593Smuzhiyun   any reset commands.
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun2) The ndo_stop routine will be called by unregister_netdevice
97*4882a593Smuzhiyun   if device is still UP.
98