xref: /openbmc/linux/Documentation/networking/driver.rst (revision d2f5c68e3f7157e874a759e382a5eaffa775b869)
128d23311SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
228d23311SMauro Carvalho Chehab
328d23311SMauro Carvalho Chehab=====================
428d23311SMauro Carvalho ChehabSoftnet Driver Issues
528d23311SMauro Carvalho Chehab=====================
628d23311SMauro Carvalho Chehab
7*d2f5c68eSJakub KicinskiTransmit path guidelines
8*d2f5c68eSJakub Kicinski========================
928d23311SMauro Carvalho Chehab
10*d2f5c68eSJakub KicinskiStop queues in advance
11*d2f5c68eSJakub Kicinski----------------------
12*d2f5c68eSJakub Kicinski
13*d2f5c68eSJakub KicinskiThe ndo_start_xmit method must not return NETDEV_TX_BUSY under
1428d23311SMauro Carvalho Chehabany normal circumstances.  It is considered a hard error unless
15404a5ad7SRandy Dunlapthere is no way your device can tell ahead of time when its
1628d23311SMauro Carvalho Chehabtransmit function will become busy.
1728d23311SMauro Carvalho Chehab
1828d23311SMauro Carvalho ChehabInstead it must maintain the queue properly.  For example,
1928d23311SMauro Carvalho Chehabfor a driver implementing scatter-gather this means::
2028d23311SMauro Carvalho Chehab
2128d23311SMauro Carvalho Chehab	static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
2228d23311SMauro Carvalho Chehab					       struct net_device *dev)
2328d23311SMauro Carvalho Chehab	{
2428d23311SMauro Carvalho Chehab		struct drv *dp = netdev_priv(dev);
2528d23311SMauro Carvalho Chehab
2628d23311SMauro Carvalho Chehab		lock_tx(dp);
2728d23311SMauro Carvalho Chehab		...
2828d23311SMauro Carvalho Chehab		/* This is a hard error log it. */
2928d23311SMauro Carvalho Chehab		if (TX_BUFFS_AVAIL(dp) <= (skb_shinfo(skb)->nr_frags + 1)) {
3028d23311SMauro Carvalho Chehab			netif_stop_queue(dev);
3128d23311SMauro Carvalho Chehab			unlock_tx(dp);
3228d23311SMauro Carvalho Chehab			printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
3328d23311SMauro Carvalho Chehab			       dev->name);
3428d23311SMauro Carvalho Chehab			return NETDEV_TX_BUSY;
3528d23311SMauro Carvalho Chehab		}
3628d23311SMauro Carvalho Chehab
3728d23311SMauro Carvalho Chehab		... queue packet to card ...
3828d23311SMauro Carvalho Chehab		... update tx consumer index ...
3928d23311SMauro Carvalho Chehab
4028d23311SMauro Carvalho Chehab		if (TX_BUFFS_AVAIL(dp) <= (MAX_SKB_FRAGS + 1))
4128d23311SMauro Carvalho Chehab			netif_stop_queue(dev);
4228d23311SMauro Carvalho Chehab
4328d23311SMauro Carvalho Chehab		...
4428d23311SMauro Carvalho Chehab		unlock_tx(dp);
4528d23311SMauro Carvalho Chehab		...
4628d23311SMauro Carvalho Chehab		return NETDEV_TX_OK;
4728d23311SMauro Carvalho Chehab	}
4828d23311SMauro Carvalho Chehab
4928d23311SMauro Carvalho ChehabAnd then at the end of your TX reclamation event handling::
5028d23311SMauro Carvalho Chehab
5128d23311SMauro Carvalho Chehab	if (netif_queue_stopped(dp->dev) &&
5228d23311SMauro Carvalho Chehab	    TX_BUFFS_AVAIL(dp) > (MAX_SKB_FRAGS + 1))
5328d23311SMauro Carvalho Chehab		netif_wake_queue(dp->dev);
5428d23311SMauro Carvalho Chehab
5528d23311SMauro Carvalho ChehabFor a non-scatter-gather supporting card, the three tests simply become::
5628d23311SMauro Carvalho Chehab
5728d23311SMauro Carvalho Chehab		/* This is a hard error log it. */
5828d23311SMauro Carvalho Chehab		if (TX_BUFFS_AVAIL(dp) <= 0)
5928d23311SMauro Carvalho Chehab
6028d23311SMauro Carvalho Chehaband::
6128d23311SMauro Carvalho Chehab
6228d23311SMauro Carvalho Chehab		if (TX_BUFFS_AVAIL(dp) == 0)
6328d23311SMauro Carvalho Chehab
6428d23311SMauro Carvalho Chehaband::
6528d23311SMauro Carvalho Chehab
6628d23311SMauro Carvalho Chehab	if (netif_queue_stopped(dp->dev) &&
6728d23311SMauro Carvalho Chehab	    TX_BUFFS_AVAIL(dp) > 0)
6828d23311SMauro Carvalho Chehab		netif_wake_queue(dp->dev);
6928d23311SMauro Carvalho Chehab
70*d2f5c68eSJakub KicinskiNo exclusive ownership
71*d2f5c68eSJakub Kicinski----------------------
72*d2f5c68eSJakub Kicinski
73*d2f5c68eSJakub KicinskiAn ndo_start_xmit method must not modify the shared parts of a
7428d23311SMauro Carvalho Chehabcloned SKB.
7528d23311SMauro Carvalho Chehab
76*d2f5c68eSJakub KicinskiTimely completions
77*d2f5c68eSJakub Kicinski------------------
78*d2f5c68eSJakub Kicinski
79*d2f5c68eSJakub KicinskiDo not forget that once you return NETDEV_TX_OK from your
8028d23311SMauro Carvalho Chehabndo_start_xmit method, it is your driver's responsibility to free
8128d23311SMauro Carvalho Chehabup the SKB and in some finite amount of time.
8228d23311SMauro Carvalho Chehab
8328d23311SMauro Carvalho ChehabFor example, this means that it is not allowed for your TX
8428d23311SMauro Carvalho Chehabmitigation scheme to let TX packets "hang out" in the TX
8528d23311SMauro Carvalho Chehabring unreclaimed forever if no new TX packets are sent.
8628d23311SMauro Carvalho ChehabThis error can deadlock sockets waiting for send buffer room
8728d23311SMauro Carvalho Chehabto be freed up.
8828d23311SMauro Carvalho Chehab
8928d23311SMauro Carvalho ChehabIf you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
9028d23311SMauro Carvalho Chehabmust not keep any reference to that SKB and you must not attempt
9128d23311SMauro Carvalho Chehabto free it up.
9228d23311SMauro Carvalho Chehab
93*d2f5c68eSJakub KicinskiProbing guidelines
94*d2f5c68eSJakub Kicinski==================
9528d23311SMauro Carvalho Chehab
96*d2f5c68eSJakub KicinskiAddress validation
97*d2f5c68eSJakub Kicinski------------------
98*d2f5c68eSJakub Kicinski
99*d2f5c68eSJakub KicinskiAny hardware layer address you obtain for your device should
10028d23311SMauro Carvalho Chehabbe verified.  For example, for ethernet check it with
10128d23311SMauro Carvalho Chehablinux/etherdevice.h:is_valid_ether_addr()
10228d23311SMauro Carvalho Chehab
103*d2f5c68eSJakub KicinskiClose/stop guidelines
104*d2f5c68eSJakub Kicinski=====================
10528d23311SMauro Carvalho Chehab
106*d2f5c68eSJakub KicinskiQuiescence
107*d2f5c68eSJakub Kicinski----------
108*d2f5c68eSJakub Kicinski
109*d2f5c68eSJakub KicinskiAfter the ndo_stop routine has been called, the hardware must
11028d23311SMauro Carvalho Chehabnot receive or transmit any data.  All in flight packets must
11128d23311SMauro Carvalho Chehabbe aborted. If necessary, poll or wait for completion of
11228d23311SMauro Carvalho Chehabany reset commands.
11328d23311SMauro Carvalho Chehab
114*d2f5c68eSJakub KicinskiAuto-close
115*d2f5c68eSJakub Kicinski----------
116*d2f5c68eSJakub Kicinski
117*d2f5c68eSJakub KicinskiThe ndo_stop routine will be called by unregister_netdevice
11828d23311SMauro Carvalho Chehabif device is still UP.
119