Skip to content

Commit

Permalink
Merge branch 'Add-MBIM-over-MHI-support'
Browse files Browse the repository at this point in the history
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Feb 10, 2021
2 parents 24a1720 + 163c5e6 commit d816f2a
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 34 deletions.
2 changes: 1 addition & 1 deletion drivers/net/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ obj-$(CONFIG_GTP) += gtp.o
obj-$(CONFIG_NLMON) += nlmon.o
obj-$(CONFIG_NET_VRF) += vrf.o
obj-$(CONFIG_VSOCKMON) += vsockmon.o
obj-$(CONFIG_MHI_NET) += mhi_net.o
obj-$(CONFIG_MHI_NET) += mhi/

#
# Networking Drivers
Expand Down
3 changes: 3 additions & 0 deletions drivers/net/mhi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
obj-$(CONFIG_MHI_NET) += mhi_net.o

mhi_net-y := net.o proto_mbim.o
40 changes: 40 additions & 0 deletions drivers/net/mhi/mhi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* MHI Network driver - Network over MHI bus
*
* Copyright (C) 2021 Linaro Ltd <loic.poulain@linaro.org>
*/

struct mhi_net_stats {
u64_stats_t rx_packets;
u64_stats_t rx_bytes;
u64_stats_t rx_errors;
u64_stats_t rx_dropped;
u64_stats_t rx_length_errors;
u64_stats_t tx_packets;
u64_stats_t tx_bytes;
u64_stats_t tx_errors;
u64_stats_t tx_dropped;
struct u64_stats_sync tx_syncp;
struct u64_stats_sync rx_syncp;
};

struct mhi_net_dev {
struct mhi_device *mdev;
struct net_device *ndev;
struct sk_buff *skbagg_head;
struct sk_buff *skbagg_tail;
const struct mhi_net_proto *proto;
void *proto_data;
struct delayed_work rx_refill;
struct mhi_net_stats stats;
u32 rx_queue_sz;
int msg_enable;
};

struct mhi_net_proto {
int (*init)(struct mhi_net_dev *mhi_netdev);
struct sk_buff * (*tx_fixup)(struct mhi_net_dev *mhi_netdev, struct sk_buff *skb);
void (*rx)(struct mhi_net_dev *mhi_netdev, struct sk_buff *skb);
};

extern const struct mhi_net_proto proto_mbim;
90 changes: 57 additions & 33 deletions drivers/net/mhi_net.c → drivers/net/mhi/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,15 @@
#include <linux/skbuff.h>
#include <linux/u64_stats_sync.h>

#include "mhi.h"

#define MHI_NET_MIN_MTU ETH_MIN_MTU
#define MHI_NET_MAX_MTU 0xffff
#define MHI_NET_DEFAULT_MTU 0x4000

struct mhi_net_stats {
u64_stats_t rx_packets;
u64_stats_t rx_bytes;
u64_stats_t rx_errors;
u64_stats_t rx_dropped;
u64_stats_t tx_packets;
u64_stats_t tx_bytes;
u64_stats_t tx_errors;
u64_stats_t tx_dropped;
struct u64_stats_sync tx_syncp;
struct u64_stats_sync rx_syncp;
};

struct mhi_net_dev {
struct mhi_device *mdev;
struct net_device *ndev;
struct sk_buff *skbagg_head;
struct sk_buff *skbagg_tail;
struct delayed_work rx_refill;
struct mhi_net_stats stats;
u32 rx_queue_sz;
struct mhi_device_info {
const char *netname;
const struct mhi_net_proto *proto;
};

static int mhi_ndo_open(struct net_device *ndev)
Expand Down Expand Up @@ -68,26 +52,35 @@ static int mhi_ndo_stop(struct net_device *ndev)
static int mhi_ndo_xmit(struct sk_buff *skb, struct net_device *ndev)
{
struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
const struct mhi_net_proto *proto = mhi_netdev->proto;
struct mhi_device *mdev = mhi_netdev->mdev;
int err;

if (proto && proto->tx_fixup) {
skb = proto->tx_fixup(mhi_netdev, skb);
if (unlikely(!skb))
goto exit_drop;
}

err = mhi_queue_skb(mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
if (unlikely(err)) {
net_err_ratelimited("%s: Failed to queue TX buf (%d)\n",
ndev->name, err);

u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
u64_stats_inc(&mhi_netdev->stats.tx_dropped);
u64_stats_update_end(&mhi_netdev->stats.tx_syncp);

/* drop the packet */
dev_kfree_skb_any(skb);
goto exit_drop;
}

if (mhi_queue_is_full(mdev, DMA_TO_DEVICE))
netif_stop_queue(ndev);

return NETDEV_TX_OK;

exit_drop:
u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
u64_stats_inc(&mhi_netdev->stats.tx_dropped);
u64_stats_update_end(&mhi_netdev->stats.tx_syncp);

return NETDEV_TX_OK;
}

static void mhi_ndo_get_stats64(struct net_device *ndev,
Expand All @@ -102,6 +95,7 @@ static void mhi_ndo_get_stats64(struct net_device *ndev,
stats->rx_bytes = u64_stats_read(&mhi_netdev->stats.rx_bytes);
stats->rx_errors = u64_stats_read(&mhi_netdev->stats.rx_errors);
stats->rx_dropped = u64_stats_read(&mhi_netdev->stats.rx_dropped);
stats->rx_length_errors = u64_stats_read(&mhi_netdev->stats.rx_length_errors);
} while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.rx_syncp, start));

do {
Expand Down Expand Up @@ -164,6 +158,7 @@ static void mhi_net_dl_callback(struct mhi_device *mhi_dev,
struct mhi_result *mhi_res)
{
struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
const struct mhi_net_proto *proto = mhi_netdev->proto;
struct sk_buff *skb = mhi_res->buf_addr;
int free_desc_count;

Expand Down Expand Up @@ -220,7 +215,10 @@ static void mhi_net_dl_callback(struct mhi_device *mhi_dev,
break;
}

netif_rx(skb);
if (proto && proto->rx)
proto->rx(mhi_netdev, skb);
else
netif_rx(skb);
}

/* Refill if RX buffers queue becomes low */
Expand Down Expand Up @@ -302,14 +300,14 @@ static struct device_type wwan_type = {
static int mhi_net_probe(struct mhi_device *mhi_dev,
const struct mhi_device_id *id)
{
const char *netname = (char *)id->driver_data;
const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data;
struct device *dev = &mhi_dev->dev;
struct mhi_net_dev *mhi_netdev;
struct net_device *ndev;
int err;

ndev = alloc_netdev(sizeof(*mhi_netdev), netname, NET_NAME_PREDICTABLE,
mhi_net_setup);
ndev = alloc_netdev(sizeof(*mhi_netdev), info->netname,
NET_NAME_PREDICTABLE, mhi_net_setup);
if (!ndev)
return -ENOMEM;

Expand All @@ -318,6 +316,7 @@ static int mhi_net_probe(struct mhi_device *mhi_dev,
mhi_netdev->ndev = ndev;
mhi_netdev->mdev = mhi_dev;
mhi_netdev->skbagg_head = NULL;
mhi_netdev->proto = info->proto;
SET_NETDEV_DEV(ndev, &mhi_dev->dev);
SET_NETDEV_DEVTYPE(ndev, &wwan_type);

Expand All @@ -337,8 +336,16 @@ static int mhi_net_probe(struct mhi_device *mhi_dev,
if (err)
goto out_err;

if (mhi_netdev->proto) {
err = mhi_netdev->proto->init(mhi_netdev);
if (err)
goto out_err_proto;
}

return 0;

out_err_proto:
unregister_netdev(ndev);
out_err:
free_netdev(ndev);
return err;
Expand All @@ -358,9 +365,26 @@ static void mhi_net_remove(struct mhi_device *mhi_dev)
free_netdev(mhi_netdev->ndev);
}

static const struct mhi_device_info mhi_hwip0 = {
.netname = "mhi_hwip%d",
};

static const struct mhi_device_info mhi_swip0 = {
.netname = "mhi_swip%d",
};

static const struct mhi_device_info mhi_hwip0_mbim = {
.netname = "mhi_mbim%d",
.proto = &proto_mbim,
};

static const struct mhi_device_id mhi_net_id_table[] = {
{ .chan = "IP_HW0", .driver_data = (kernel_ulong_t)"mhi_hwip%d" },
{ .chan = "IP_SW0", .driver_data = (kernel_ulong_t)"mhi_swip%d" },
/* Hardware accelerated data PATH (to modem IPA), protocol agnostic */
{ .chan = "IP_HW0", .driver_data = (kernel_ulong_t)&mhi_hwip0 },
/* Software data PATH (to modem CPU) */
{ .chan = "IP_SW0", .driver_data = (kernel_ulong_t)&mhi_swip0 },
/* Hardware accelerated data PATH (to modem IPA), MBIM protocol */
{ .chan = "IP_HW0_MBIM", .driver_data = (kernel_ulong_t)&mhi_hwip0_mbim },
{}
};
MODULE_DEVICE_TABLE(mhi, mhi_net_id_table);
Expand Down
Loading

0 comments on commit d816f2a

Please sign in to comment.