Commit 638c5115 authored by Ming Lei's avatar Ming Lei Committed by Greg Kroah-Hartman

USBNET: support DMA SG

This patch introduces support of DMA SG if the USB host controller
which usbnet device is attached to is capable of building packet from
discontinuous buffers.

The patch supports passing the skb fragment buffers to usb stack directly
via urb->sg.

Cc: Ben Hutchings <bhutchings@solarflare.com>
Cc: Grant Grundler <grundler@google.com>
Cc: Freddy Xin <freddy@asix.com.tw>
Cc: Alan Stern <stern@rowland.harvard.edu>
Acked-by: default avatarOliver Neukum <oneukum@suse.de>
Signed-off-by: default avatarMing Lei <ming.lei@canonical.com>
Reviewed-by: default avatarEric Dumazet <edumazet@google.com>
Acked-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent fc76051c
...@@ -1197,6 +1197,37 @@ EXPORT_SYMBOL_GPL(usbnet_tx_timeout); ...@@ -1197,6 +1197,37 @@ EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
{
unsigned num_sgs, total_len = 0;
int i, s = 0;
num_sgs = skb_shinfo(skb)->nr_frags + 1;
if (num_sgs == 1)
return 0;
urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist), GFP_ATOMIC);
if (!urb->sg)
return -ENOMEM;
urb->num_sgs = num_sgs;
sg_init_table(urb->sg, urb->num_sgs);
sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
total_len += skb_headlen(skb);
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
total_len += skb_frag_size(f);
sg_set_page(&urb->sg[i + s], f->page.p, f->size,
f->page_offset);
}
urb->transfer_buffer_length = total_len;
return 1;
}
netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
struct net_device *net) struct net_device *net)
{ {
...@@ -1223,7 +1254,6 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, ...@@ -1223,7 +1254,6 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
goto drop; goto drop;
} }
} }
length = skb->len;
if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
netif_dbg(dev, tx_err, dev->net, "no urb\n"); netif_dbg(dev, tx_err, dev->net, "no urb\n");
...@@ -1233,10 +1263,14 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, ...@@ -1233,10 +1263,14 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
entry = (struct skb_data *) skb->cb; entry = (struct skb_data *) skb->cb;
entry->urb = urb; entry->urb = urb;
entry->dev = dev; entry->dev = dev;
entry->length = length;
usb_fill_bulk_urb (urb, dev->udev, dev->out, usb_fill_bulk_urb (urb, dev->udev, dev->out,
skb->data, skb->len, tx_complete, skb); skb->data, skb->len, tx_complete, skb);
if (dev->can_dma_sg) {
if (build_dma_sg(skb, urb) < 0)
goto drop;
}
entry->length = length = urb->transfer_buffer_length;
/* don't assume the hardware handles USB_ZERO_PACKET /* don't assume the hardware handles USB_ZERO_PACKET
* NOTE: strictly conforming cdc-ether devices should expect * NOTE: strictly conforming cdc-ether devices should expect
...@@ -1305,7 +1339,10 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, ...@@ -1305,7 +1339,10 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
not_drop: not_drop:
if (skb) if (skb)
dev_kfree_skb_any (skb); dev_kfree_skb_any (skb);
usb_free_urb (urb); if (urb) {
kfree(urb->sg);
usb_free_urb(urb);
}
} else } else
netif_dbg(dev, tx_queued, dev->net, netif_dbg(dev, tx_queued, dev->net,
"> tx, len %d, type 0x%x\n", length, skb->protocol); "> tx, len %d, type 0x%x\n", length, skb->protocol);
...@@ -1356,6 +1393,7 @@ static void usbnet_bh (unsigned long param) ...@@ -1356,6 +1393,7 @@ static void usbnet_bh (unsigned long param)
rx_process (dev, skb); rx_process (dev, skb);
continue; continue;
case tx_done: case tx_done:
kfree(entry->urb->sg);
case rx_cleanup: case rx_cleanup:
usb_free_urb (entry->urb); usb_free_urb (entry->urb);
dev_kfree_skb (skb); dev_kfree_skb (skb);
...@@ -1689,6 +1727,7 @@ int usbnet_resume (struct usb_interface *intf) ...@@ -1689,6 +1727,7 @@ int usbnet_resume (struct usb_interface *intf)
retval = usb_submit_urb(res, GFP_ATOMIC); retval = usb_submit_urb(res, GFP_ATOMIC);
if (retval < 0) { if (retval < 0) {
dev_kfree_skb_any(skb); dev_kfree_skb_any(skb);
kfree(res->sg);
usb_free_urb(res); usb_free_urb(res);
usb_autopm_put_interface_async(dev->intf); usb_autopm_put_interface_async(dev->intf);
} else { } else {
......
...@@ -34,6 +34,7 @@ struct usbnet { ...@@ -34,6 +34,7 @@ struct usbnet {
struct mutex phy_mutex; struct mutex phy_mutex;
unsigned char suspend_count; unsigned char suspend_count;
unsigned char pkt_cnt, pkt_err; unsigned char pkt_cnt, pkt_err;
unsigned can_dma_sg:1;
/* i/o info: pipes etc */ /* i/o info: pipes etc */
unsigned in, out; unsigned in, out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment