mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
To: barebox@lists.infradead.org
Subject: [PATCH 3/5] net: allow the drivers to specify the tx buffer allocator
Date: Fri,  2 Mar 2012 19:20:08 +0100	[thread overview]
Message-ID: <1330712410-26479-3-git-send-email-plagnioj@jcrosoft.com> (raw)
In-Reply-To: <1330712410-26479-1-git-send-email-plagnioj@jcrosoft.com>

This will allow as example to have a non cached buffer.

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 include/net.h |   16 +++++++---------
 net/eth.c     |   41 +++++++++++++++++++++++++++++++++++++++++
 net/net.c     |   24 +++++++++++++++++-------
 3 files changed, 65 insertions(+), 16 deletions(-)

diff --git a/include/net.h b/include/net.h
index c7da380..97515a5 100644
--- a/include/net.h
+++ b/include/net.h
@@ -39,6 +39,9 @@ struct eth_device {
 	int  (*get_ethaddr) (struct eth_device*, unsigned char *adr);
 	int  (*set_ethaddr) (struct eth_device*, unsigned char *adr);
 
+	void *(*alloc_packet)(struct eth_device*);
+	void (*free_packet)(struct eth_device*, void *packet);
+
 	struct eth_device *next;
 	void *priv;
 
@@ -373,6 +376,8 @@ typedef void rx_handler_f(void *ctx, char *packet, unsigned int len);
 void eth_set_current(struct eth_device *eth);
 struct eth_device *eth_get_current(void);
 struct eth_device *eth_get_byname(char *name);
+void *eth_alloc_packet(void);
+void eth_free_packet(void *packet);
 void net_update_env(void);
 
 /**
@@ -396,15 +401,8 @@ struct net_connection {
 	void *priv;
 };
 
-static inline char *net_alloc_packet(void)
-{
-	return xmemalign(32, PKTSIZE);
-}
-
-static inline void net_free_packet(void *packet)
-{
-	free(packet);
-}
+void *net_alloc_packet(void);
+void net_free_packet(void *packet);
 
 struct net_connection *net_udp_new(IPaddr_t dest, uint16_t dport,
 		rx_handler_f *handler, void *ctx);
diff --git a/net/eth.c b/net/eth.c
index 20fdbf4..ba7a0eb 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -109,6 +109,31 @@ struct eth_device *eth_get_byname(char *ethname)
 	return NULL;
 }
 
+void *eth_alloc_packet(void)
+{
+	int ret;
+
+	if (!eth_current)
+		return NULL;
+
+	if (!eth_current->active) {
+		ret = eth_current->open(eth_current);
+		if (ret)
+			return NULL;
+		eth_current->active = 1;
+	}
+
+	return eth_current->alloc_packet(eth_current);
+}
+
+void eth_free_packet(void *packet)
+{
+	if (!eth_current)
+		return;
+
+	eth_current->free_packet(eth_current, packet);
+}
+
 int eth_send(void *packet, int length)
 {
 	int ret;
@@ -185,6 +210,16 @@ static int eth_set_ipaddr(struct device_d *dev, struct param_d *param, const cha
 	return 0;
 }
 
+static void *eth_default_alloc_packet(struct eth_device *edev)
+{
+	return net_alloc_packet();
+}
+
+static void eth_default_free_packet(struct eth_device *edev, void* packet)
+{
+	net_free_packet(packet);
+}
+
 int eth_register(struct eth_device *edev)
 {
         struct device_d *dev = &edev->dev;
@@ -197,6 +232,12 @@ int eth_register(struct eth_device *edev)
 		return -1;
 	}
 
+	if (!edev->alloc_packet)
+		edev->alloc_packet = eth_default_alloc_packet;
+
+	if (!edev->free_packet)
+		edev->free_packet = eth_default_free_packet;
+
 	strcpy(edev->dev.name, "eth");
 	edev->dev.id = -1;
 
diff --git a/net/net.c b/net/net.c
index 07fc23a..cf1bfa8 100644
--- a/net/net.c
+++ b/net/net.c
@@ -1,4 +1,4 @@
-/*
+ /*
  * net.c - barebox networking support
  *
  * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
@@ -222,7 +222,7 @@ static int arp_request(IPaddr_t dest, unsigned char *ether)
 	struct ethernet *et;
 
 	if (!arp_packet) {
-		arp_packet = net_alloc_packet();
+		arp_packet = eth_alloc_packet();
 		if (!arp_packet)
 			return -ENOMEM;
 	}
@@ -370,7 +370,7 @@ static struct net_connection *net_new(IPaddr_t dest, rx_handler_f *handler,
 		return ERR_PTR(-ENETDOWN);
 
 	con = xzalloc(sizeof(*con));
-	con->packet = net_alloc_packet();
+	con->packet = eth_alloc_packet();
 	con->priv = ctx;
 	memset(con->packet, 0, PKTSIZE);
 
@@ -402,7 +402,7 @@ static struct net_connection *net_new(IPaddr_t dest, rx_handler_f *handler,
 
 	return con;
 out:
-	net_free_packet(con->packet);
+	eth_free_packet(con->packet);
 	free(con);
 	return ERR_PTR(ret);
 }
@@ -440,7 +440,7 @@ struct net_connection *net_icmp_new(IPaddr_t dest, rx_handler_f *handler,
 void net_unregister(struct net_connection *con)
 {
 	list_del(&con->list);
-	net_free_packet(con->packet);
+	eth_free_packet(con->packet);
 	free(con);
 }
 
@@ -490,12 +490,12 @@ static int net_answer_arp(unsigned char *pkt, int len)
 	memcpy(&arp->ar_data[0], net_ether, 6);
 	net_copy_ip(&arp->ar_data[6], &net_ip);
 
-	packet = net_alloc_packet();
+	packet = eth_alloc_packet();
 	if (!packet)
 		return 0;
 	memcpy(packet, pkt, ETHER_HDR_SIZE + ARP_HDR_SIZE);
 	eth_send(packet, ETHER_HDR_SIZE + ARP_HDR_SIZE);
-	net_free_packet(packet);
+	eth_free_packet(packet);
 
 	return 0;
 }
@@ -660,6 +660,16 @@ out:
 	return ret;
 }
 
+void *net_alloc_packet(void)
+{
+	return xmemalign(32, PKTSIZE);
+}
+
+void net_free_packet(void *packet)
+{
+	free(packet);
+}
+
 static int net_init(void)
 {
 	int i;
-- 
1.7.7


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2012-03-02 18:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-02 18:20 [PATCH 1/5] net: use net_alloc_packet to allocate packet Jean-Christophe PLAGNIOL-VILLARD
2012-03-02 18:20 ` [PATCH 2/5] net: introduce net_free_packet to free the packet Jean-Christophe PLAGNIOL-VILLARD
2012-03-02 18:20 ` Jean-Christophe PLAGNIOL-VILLARD [this message]
2012-03-02 18:20 ` [PATCH 4/5] macb: implement alloc_packet & free_packet Jean-Christophe PLAGNIOL-VILLARD
2012-03-04 12:08   ` Sascha Hauer
2012-03-05 17:55     ` Jean-Christophe PLAGNIOL-VILLARD
2012-03-02 18:20 ` [PATCH 5/5] macb: add timeout on send Jean-Christophe PLAGNIOL-VILLARD
2012-03-06  8:29   ` Sascha Hauer
2012-03-07  9:21     ` Jean-Christophe PLAGNIOL-VILLARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1330712410-26479-3-git-send-email-plagnioj@jcrosoft.com \
    --to=plagnioj@jcrosoft.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox