Skip to content

Commit

Permalink
usb: gadget: ncm: Add support to update wMaxSegmentSize via configfs
Browse files Browse the repository at this point in the history
The max segment size is currently limited to the ethernet frame length of
the kernel which happens to be 1514 at this point in time. However the NCM
specification limits it to 64K for sixtenn bit NTB's. For peer to peer
connections, increasing the segment size gives better throughput.

Add support to configure this value before configfs symlink is created.
Also since the NTB Out/In buffer sizes are fixed at 16384 bytes, limit the
segment size to an upper cap of 8000 to allow at least a minimum of 2 MTU
sized datagrams to be aggregated.

Set the default MTU size for the ncm interface during function bind before
network interface is registered allowing MTU to be set in parity with
wMaxSegmentSize.

Update gadget documentation describing the new configfs property.

Signed-off-by: Krishna Kurapati <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
Krishna Kurapati authored and gregkh committed Jan 4, 2024
1 parent 68c26fe commit 1900dae
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
20 changes: 11 additions & 9 deletions Documentation/usb/gadget-testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,17 @@ Function-specific configfs interface
The function name to use when creating the function directory is "ncm".
The NCM function provides these attributes in its function directory:

=============== ==================================================
ifname network device interface name associated with this
function instance
qmult queue length multiplier for high and super speed
host_addr MAC address of host's end of this
Ethernet over USB link
dev_addr MAC address of device's end of this
Ethernet over USB link
=============== ==================================================
=============== ==================================================
ifname network device interface name associated with this
function instance
qmult queue length multiplier for high and super speed
host_addr MAC address of host's end of this
Ethernet over USB link
dev_addr MAC address of device's end of this
Ethernet over USB link
max_segment_size Segment size required for P2P connections. This
will set MTU to (max_segment_size - 14 bytes)
=============== ==================================================

and after creating the functions/ncm.<instance name> they contain default
values: qmult is 5, dev_addr and host_addr are randomly selected.
Expand Down
69 changes: 66 additions & 3 deletions drivers/usb/gadget/function/f_ncm.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ static inline struct f_ncm *func_to_ncm(struct usb_function *f)
/* Delay for the transmit to wait before sending an unfilled NTB frame. */
#define TX_TIMEOUT_NSECS 300000

/*
* Although max mtu as dictated by u_ether is 15412 bytes, setting
* max_segment_sizeto 15426 would not be efficient. If user chooses segment
* size to be (>= 8192), then we can't aggregate more than one buffer in each
* NTB (assuming each packet coming from network layer is >= 8192 bytes) as ep
* maxpacket limit is 16384. So let max_segment_size be limited to 8000 to allow
* at least 2 packets to be aggregated reducing wastage of NTB buffer space
*/
#define MAX_DATAGRAM_SIZE 8000

#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
USB_CDC_NCM_NTB32_SUPPORTED)

Expand Down Expand Up @@ -179,7 +189,6 @@ static struct usb_cdc_ether_desc ecm_desc = {
/* this descriptor actually adds value, surprise! */
/* .iMACAddress = DYNAMIC */
.bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
.wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
.wNumberMCFilters = cpu_to_le16(0),
.bNumberPowerFilters = 0,
};
Expand Down Expand Up @@ -1166,11 +1175,15 @@ static int ncm_unwrap_ntb(struct gether *port,
struct sk_buff *skb2;
int ret = -EINVAL;
unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
unsigned frame_max;
const struct ndp_parser_opts *opts = ncm->parser_opts;
unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
int dgram_counter;
int to_process = skb->len;
struct f_ncm_opts *ncm_opts;

ncm_opts = container_of(port->func.fi, struct f_ncm_opts, func_inst);
frame_max = ncm_opts->max_segment_size;

parse_ntb:
tmp = (__le16 *)ntb_ptr;
Expand Down Expand Up @@ -1430,8 +1443,10 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)

mutex_lock(&ncm_opts->lock);
gether_set_gadget(ncm_opts->net, cdev->gadget);
if (!ncm_opts->bound)
if (!ncm_opts->bound) {
ncm_opts->net->mtu = (ncm_opts->max_segment_size - ETH_HLEN);
status = gether_register_netdev(ncm_opts->net);
}
mutex_unlock(&ncm_opts->lock);

if (status)
Expand Down Expand Up @@ -1474,6 +1489,8 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
ncm_data_intf.bInterfaceNumber = status;
ncm_union_desc.bSlaveInterface0 = status;

ecm_desc.wMaxSegmentSize = ncm_opts->max_segment_size;

status = -ENODEV;

/* allocate instance-specific endpoints */
Expand Down Expand Up @@ -1576,11 +1593,56 @@ USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
/* f_ncm_opts_ifname */
USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);

static ssize_t ncm_opts_max_segment_size_show(struct config_item *item,
char *page)
{
struct f_ncm_opts *opts = to_f_ncm_opts(item);
u16 segment_size;

mutex_lock(&opts->lock);
segment_size = opts->max_segment_size;
mutex_unlock(&opts->lock);

return sysfs_emit(page, "%u\n", segment_size);
}

static ssize_t ncm_opts_max_segment_size_store(struct config_item *item,
const char *page, size_t len)
{
struct f_ncm_opts *opts = to_f_ncm_opts(item);
u16 segment_size;
int ret;

mutex_lock(&opts->lock);
if (opts->refcnt) {
ret = -EBUSY;
goto out;
}

ret = kstrtou16(page, 0, &segment_size);
if (ret)
goto out;

if (segment_size > MAX_DATAGRAM_SIZE) {
ret = -EINVAL;
goto out;
}

opts->max_segment_size = segment_size;
ret = len;
out:
mutex_unlock(&opts->lock);
return ret;
}

CONFIGFS_ATTR(ncm_opts_, max_segment_size);

static struct configfs_attribute *ncm_attrs[] = {
&ncm_opts_attr_dev_addr,
&ncm_opts_attr_host_addr,
&ncm_opts_attr_qmult,
&ncm_opts_attr_ifname,
&ncm_opts_attr_max_segment_size,
NULL,
};

Expand Down Expand Up @@ -1623,6 +1685,7 @@ static struct usb_function_instance *ncm_alloc_inst(void)
kfree(opts);
return ERR_CAST(net);
}
opts->max_segment_size = cpu_to_le16(ETH_FRAME_LEN);
INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);

descs[0] = &opts->ncm_os_desc;
Expand Down
2 changes: 2 additions & 0 deletions drivers/usb/gadget/function/u_ncm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct f_ncm_opts {
*/
struct mutex lock;
int refcnt;

u16 max_segment_size;
};

#endif /* U_NCM_H */

0 comments on commit 1900dae

Please sign in to comment.