xref: /openbmc/linux/Documentation/networking/driver.rst (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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
7da4f0f82SJakub KicinskiProbing guidelines
8da4f0f82SJakub Kicinski==================
9da4f0f82SJakub Kicinski
10da4f0f82SJakub KicinskiAddress validation
11da4f0f82SJakub Kicinski------------------
12da4f0f82SJakub Kicinski
13da4f0f82SJakub KicinskiAny hardware layer address you obtain for your device should
14da4f0f82SJakub Kicinskibe verified.  For example, for ethernet check it with
15da4f0f82SJakub Kicinskilinux/etherdevice.h:is_valid_ether_addr()
16da4f0f82SJakub Kicinski
17da4f0f82SJakub KicinskiClose/stop guidelines
18da4f0f82SJakub Kicinski=====================
19da4f0f82SJakub Kicinski
20da4f0f82SJakub KicinskiQuiescence
21da4f0f82SJakub Kicinski----------
22da4f0f82SJakub Kicinski
23da4f0f82SJakub KicinskiAfter the ndo_stop routine has been called, the hardware must
24da4f0f82SJakub Kicinskinot receive or transmit any data.  All in flight packets must
25da4f0f82SJakub Kicinskibe aborted. If necessary, poll or wait for completion of
26da4f0f82SJakub Kicinskiany reset commands.
27da4f0f82SJakub Kicinski
28da4f0f82SJakub KicinskiAuto-close
29da4f0f82SJakub Kicinski----------
30da4f0f82SJakub Kicinski
31da4f0f82SJakub KicinskiThe ndo_stop routine will be called by unregister_netdevice
32da4f0f82SJakub Kicinskiif device is still UP.
33da4f0f82SJakub Kicinski
34d2f5c68eSJakub KicinskiTransmit path guidelines
35d2f5c68eSJakub Kicinski========================
3628d23311SMauro Carvalho Chehab
37d2f5c68eSJakub KicinskiStop queues in advance
38d2f5c68eSJakub Kicinski----------------------
39d2f5c68eSJakub Kicinski
40d2f5c68eSJakub KicinskiThe ndo_start_xmit method must not return NETDEV_TX_BUSY under
4128d23311SMauro Carvalho Chehabany normal circumstances.  It is considered a hard error unless
42404a5ad7SRandy Dunlapthere is no way your device can tell ahead of time when its
4328d23311SMauro Carvalho Chehabtransmit function will become busy.
4428d23311SMauro Carvalho Chehab
4528d23311SMauro Carvalho ChehabInstead it must maintain the queue properly.  For example,
4683364625SJakub Kicinskifor a driver implementing scatter-gather this means:
4783364625SJakub Kicinski
4883364625SJakub Kicinski.. code-block:: c
4928d23311SMauro Carvalho Chehab
50*50762d9aSJakub Kicinski	static u32 drv_tx_avail(struct drv_ring *dr)
51*50762d9aSJakub Kicinski	{
52*50762d9aSJakub Kicinski		u32 used = READ_ONCE(dr->prod) - READ_ONCE(dr->cons);
53*50762d9aSJakub Kicinski
54*50762d9aSJakub Kicinski		return dr->tx_ring_size - (used & bp->tx_ring_mask);
55*50762d9aSJakub Kicinski	}
56*50762d9aSJakub Kicinski
5728d23311SMauro Carvalho Chehab	static netdev_tx_t drv_hard_start_xmit(struct sk_buff *skb,
5828d23311SMauro Carvalho Chehab					       struct net_device *dev)
5928d23311SMauro Carvalho Chehab	{
6028d23311SMauro Carvalho Chehab		struct drv *dp = netdev_priv(dev);
61*50762d9aSJakub Kicinski		struct netdev_queue *txq;
62*50762d9aSJakub Kicinski		struct drv_ring *dr;
63*50762d9aSJakub Kicinski		int idx;
6428d23311SMauro Carvalho Chehab
65*50762d9aSJakub Kicinski		idx = skb_get_queue_mapping(skb);
66*50762d9aSJakub Kicinski		dr = dp->tx_rings[idx];
67*50762d9aSJakub Kicinski		txq = netdev_get_tx_queue(dev, idx);
68*50762d9aSJakub Kicinski
6983364625SJakub Kicinski		//...
70*50762d9aSJakub Kicinski		/* This should be a very rare race - log it. */
71*50762d9aSJakub Kicinski		if (drv_tx_avail(dr) <= skb_shinfo(skb)->nr_frags + 1) {
7228d23311SMauro Carvalho Chehab			netif_stop_queue(dev);
73*50762d9aSJakub Kicinski			netdev_warn(dev, "Tx Ring full when queue awake!\n");
7428d23311SMauro Carvalho Chehab			return NETDEV_TX_BUSY;
7528d23311SMauro Carvalho Chehab		}
7628d23311SMauro Carvalho Chehab
7783364625SJakub Kicinski		//... queue packet to card ...
7828d23311SMauro Carvalho Chehab
79*50762d9aSJakub Kicinski		netdev_tx_sent_queue(txq, skb->len);
8028d23311SMauro Carvalho Chehab
81*50762d9aSJakub Kicinski		//... update tx producer index using WRITE_ONCE() ...
82*50762d9aSJakub Kicinski
83*50762d9aSJakub Kicinski		if (!netif_txq_maybe_stop(txq, drv_tx_avail(dr),
84*50762d9aSJakub Kicinski					  MAX_SKB_FRAGS + 1, 2 * MAX_SKB_FRAGS))
85*50762d9aSJakub Kicinski			dr->stats.stopped++;
86*50762d9aSJakub Kicinski
8783364625SJakub Kicinski		//...
8828d23311SMauro Carvalho Chehab		return NETDEV_TX_OK;
8928d23311SMauro Carvalho Chehab	}
9028d23311SMauro Carvalho Chehab
9183364625SJakub KicinskiAnd then at the end of your TX reclamation event handling:
9283364625SJakub Kicinski
9383364625SJakub Kicinski.. code-block:: c
9428d23311SMauro Carvalho Chehab
95*50762d9aSJakub Kicinski	//... update tx consumer index using WRITE_ONCE() ...
9628d23311SMauro Carvalho Chehab
97*50762d9aSJakub Kicinski	netif_txq_completed_wake(txq, cmpl_pkts, cmpl_bytes,
98*50762d9aSJakub Kicinski				 drv_tx_avail(dr), 2 * MAX_SKB_FRAGS);
9928d23311SMauro Carvalho Chehab
100c91c46deSJakub KicinskiLockless queue stop / wake helper macros
101c91c46deSJakub Kicinski~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102c91c46deSJakub Kicinski
103c91c46deSJakub Kicinski.. kernel-doc:: include/net/netdev_queues.h
104c91c46deSJakub Kicinski   :doc: Lockless queue stopping / waking helpers.
105c91c46deSJakub Kicinski
106d2f5c68eSJakub KicinskiNo exclusive ownership
107d2f5c68eSJakub Kicinski----------------------
108d2f5c68eSJakub Kicinski
109d2f5c68eSJakub KicinskiAn ndo_start_xmit method must not modify the shared parts of a
11028d23311SMauro Carvalho Chehabcloned SKB.
11128d23311SMauro Carvalho Chehab
112d2f5c68eSJakub KicinskiTimely completions
113d2f5c68eSJakub Kicinski------------------
114d2f5c68eSJakub Kicinski
115d2f5c68eSJakub KicinskiDo not forget that once you return NETDEV_TX_OK from your
11628d23311SMauro Carvalho Chehabndo_start_xmit method, it is your driver's responsibility to free
11728d23311SMauro Carvalho Chehabup the SKB and in some finite amount of time.
11828d23311SMauro Carvalho Chehab
11928d23311SMauro Carvalho ChehabFor example, this means that it is not allowed for your TX
12028d23311SMauro Carvalho Chehabmitigation scheme to let TX packets "hang out" in the TX
12128d23311SMauro Carvalho Chehabring unreclaimed forever if no new TX packets are sent.
12228d23311SMauro Carvalho ChehabThis error can deadlock sockets waiting for send buffer room
12328d23311SMauro Carvalho Chehabto be freed up.
12428d23311SMauro Carvalho Chehab
12528d23311SMauro Carvalho ChehabIf you return NETDEV_TX_BUSY from the ndo_start_xmit method, you
12628d23311SMauro Carvalho Chehabmust not keep any reference to that SKB and you must not attempt
12728d23311SMauro Carvalho Chehabto free it up.
128