From: Marc Kleine-Budde <mkl@pengutronix.de>
To: linux-can@vger.kernel.org
Cc: David Jander <david@protonic.nl>, Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH 01/10] can: rx-fifo: Add support for simple irq offloading
Date: Mon, 9 May 2016 12:52:25 +0200 [thread overview]
Message-ID: <1462791154-13375-2-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1462791154-13375-1-git-send-email-mkl@pengutronix.de>
From: David Jander <david@protonic.nl>
Some CAN controllers have a usable FIFO already but can still benefit from
off-loading the CAN controller FIFO. The mailboxes of the FIFO are copied to a
ring buffer during the interrupt and then transmitted in a NAPI context.
Signed-off-by: David Jander <david@protonic.nl>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/Makefile | 3 +-
drivers/net/can/rx-fifo.c | 174 ++++++++++++++++++++++++++++++++++++++++++++
include/linux/can/rx-fifo.h | 58 +++++++++++++++
3 files changed, 234 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/can/rx-fifo.c
create mode 100644 include/linux/can/rx-fifo.h
diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile
index e3db0c807f55..9657d55ef03e 100644
--- a/drivers/net/can/Makefile
+++ b/drivers/net/can/Makefile
@@ -6,7 +6,8 @@ obj-$(CONFIG_CAN_VCAN) += vcan.o
obj-$(CONFIG_CAN_SLCAN) += slcan.o
obj-$(CONFIG_CAN_DEV) += can-dev.o
-can-dev-y := dev.o
+can-dev-y += dev.o
+can-dev-y += rx-fifo.o
can-dev-$(CONFIG_CAN_LEDS) += led.o
diff --git a/drivers/net/can/rx-fifo.c b/drivers/net/can/rx-fifo.c
new file mode 100644
index 000000000000..9ec8f99d8e2e
--- /dev/null
+++ b/drivers/net/can/rx-fifo.c
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2014 David Jander, Protonic Holland
+ * Copyright (C) 2014-2016 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the version 2 of the GNU General Public License
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/circ_buf.h>
+#include <linux/can/dev.h>
+#include <linux/can/rx-fifo.h>
+
+static int can_rx_fifo_napi_read_frame(struct can_rx_fifo *fifo, int index)
+{
+ struct net_device *dev = fifo->dev;
+ struct net_device_stats *stats = &dev->stats;
+ struct sk_buff *skb;
+ struct can_frame *cf;
+
+ skb = alloc_can_skb(dev, &cf);
+ if (unlikely(!skb)) {
+ stats->rx_dropped++;
+ return 0;
+ }
+
+ memcpy(cf, &fifo->ring[index], sizeof(*cf));
+ memset(&fifo->ring[index], 0x0, sizeof(*cf));
+
+ stats->rx_packets++;
+ stats->rx_bytes += cf->can_dlc;
+ netif_receive_skb(skb);
+
+ return 1;
+}
+
+static int can_rx_fifo_napi_poll(struct napi_struct *napi, int quota)
+{
+ struct can_rx_fifo *fifo = container_of(napi, struct can_rx_fifo, napi);
+ unsigned int tail;
+ int work_done = 0;
+
+ if (fifo->poll_pre_read && work_done < quota)
+ work_done += fifo->poll_pre_read(fifo);
+
+ /* handle mailboxes */
+ tail = fifo->ring_tail;
+ while ((CIRC_CNT(smp_load_acquire(&fifo->ring_head), tail, fifo->ring_size)) &&
+ (work_done < quota)) {
+ work_done += can_rx_fifo_napi_read_frame(fifo, tail);
+ tail++;
+ tail &= fifo->ring_size -1;
+ smp_store_release(&fifo->ring_tail, tail);
+ }
+
+ if (fifo->poll_post_read && work_done < quota)
+ work_done += fifo->poll_post_read(fifo);
+
+ if (work_done < quota) {
+ unsigned int head;
+
+ napi_complete(napi);
+
+ /* Check if there was another interrupt */
+ head = smp_load_acquire(&fifo->ring_head);
+ if (((CIRC_CNT(head, tail, fifo->ring_size)) || fifo->poll_errors) &&
+ napi_reschedule(&fifo->napi)) {
+ fifo->poll_errors = false;
+ }
+
+ if (fifo->poll_error_interrupts_enable)
+ fifo->poll_error_interrupts_enable(fifo);
+ }
+
+ can_led_event(fifo->dev, CAN_LED_EVENT_RX);
+
+ return work_done;
+}
+
+static unsigned int can_rx_fifo_offload_one(struct can_rx_fifo *fifo, unsigned int n)
+{
+ unsigned int head, tail;
+ unsigned int ret;
+
+ head = fifo->ring_head;
+ tail = ACCESS_ONCE(fifo->ring_tail);
+ if (CIRC_SPACE(head, tail, fifo->ring_size)) {
+ ret = fifo->mailbox_read(fifo, &fifo->ring[head], n);
+ if (ret) {
+ head++;
+ head &= fifo->ring_size - 1;
+ smp_store_release(&fifo->ring_head, head);
+ }
+ } else {
+ /* Circular buffer is fill, read to discard mailbox */
+ ret = fifo->mailbox_read(fifo, &fifo->overflow, n);
+ if (ret)
+ fifo->dev->stats.rx_dropped++;
+ }
+
+ return ret;
+}
+
+int can_rx_fifo_irq_offload_simple(struct can_rx_fifo *fifo)
+{
+ unsigned int received = 0;
+ unsigned int ret;
+
+ do {
+ ret = can_rx_fifo_offload_one(fifo, 0);
+ received += ret;
+ } while (ret);
+
+ if (received)
+ can_rx_fifo_schedule(fifo);
+
+ return received;
+}
+EXPORT_SYMBOL_GPL(can_rx_fifo_irq_offload_simple);
+
+static int can_rx_fifo_init_ring(struct net_device *dev,
+ struct can_rx_fifo *fifo, unsigned int weight)
+{
+ fifo->dev = dev;
+
+ /* Make ring-buffer a sensible size that is a power of 2 */
+ fifo->ring_size = 2 << fls(weight);
+ fifo->ring = kzalloc(sizeof(struct can_frame) * fifo->ring_size,
+ GFP_KERNEL);
+ if (!fifo->ring)
+ return -ENOMEM;
+
+ fifo->ring_head = fifo->ring_tail = 0;
+ netif_napi_add(dev, &fifo->napi, can_rx_fifo_napi_poll, weight);
+
+ return 0;
+}
+
+int can_rx_fifo_add_simple(struct net_device *dev, struct can_rx_fifo *fifo, unsigned int weight)
+{
+ if (!fifo->mailbox_read)
+ return -EINVAL;
+
+ return can_rx_fifo_init_ring(dev, fifo, weight);
+}
+EXPORT_SYMBOL_GPL(can_rx_fifo_add_simple);
+
+void can_rx_fifo_enable(struct can_rx_fifo *fifo)
+{
+ napi_enable(&fifo->napi);
+}
+EXPORT_SYMBOL_GPL(can_rx_fifo_enable);
+
+void can_rx_fifo_irq_error(struct can_rx_fifo *fifo)
+{
+ fifo->poll_errors = true;
+ can_rx_fifo_schedule(fifo);
+}
+EXPORT_SYMBOL_GPL(can_rx_fifo_irq_error);
+
+void can_rx_fifo_del(struct can_rx_fifo *fifo)
+{
+ netif_napi_del(&fifo->napi);
+ kfree(fifo->ring);
+}
+EXPORT_SYMBOL_GPL(can_rx_fifo_del);
diff --git a/include/linux/can/rx-fifo.h b/include/linux/can/rx-fifo.h
new file mode 100644
index 000000000000..0582564deaaf
--- /dev/null
+++ b/include/linux/can/rx-fifo.h
@@ -0,0 +1,58 @@
+/*
+ * linux/can/rx-fifo.h
+ *
+ * Copyright (c) 2014 David Jander, Protonic Holland
+ * Copyright (c) 2014-2016 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the version 2 of the GNU General Public License
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _CAN_RX_FIFO_H
+#define _CAN_RX_FIFO_H
+
+#include <linux/netdevice.h>
+#include <linux/can.h>
+
+struct can_rx_fifo {
+ struct net_device *dev;
+
+ void (*poll_error_interrupts_enable)(struct can_rx_fifo *fifo);
+ unsigned int (*mailbox_read)(struct can_rx_fifo *fifo, struct can_frame *cf, unsigned int mb);
+ unsigned int (*poll_pre_read)(struct can_rx_fifo *fifo);
+ unsigned int (*poll_post_read)(struct can_rx_fifo *fifo);
+
+ unsigned int ring_size;
+ unsigned int ring_head;
+ unsigned int ring_tail;
+
+ struct can_frame *ring;
+ struct can_frame overflow;
+ struct napi_struct napi;
+
+ bool poll_errors;
+};
+
+int can_rx_fifo_add_simple(struct net_device *dev, struct can_rx_fifo *fifo, unsigned int weight);
+int can_rx_fifo_irq_offload_simple(struct can_rx_fifo *fifo);
+void can_rx_fifo_irq_error(struct can_rx_fifo *fifo);
+void can_rx_fifo_del(struct can_rx_fifo *fifo);
+void can_rx_fifo_enable(struct can_rx_fifo *fifo);
+
+static inline void can_rx_fifo_schedule(struct can_rx_fifo *fifo)
+{
+ napi_schedule(&fifo->napi);
+}
+
+static inline void can_rx_fifo_disable(struct can_rx_fifo *fifo)
+{
+ napi_disable(&fifo->napi);
+}
+
+#endif /* !_CAN_RX_FIFO_H */
--
2.8.1
next prev parent reply other threads:[~2016-05-09 10:52 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-09 10:52 rx-fifo: add implmentation and switch flexcan driver to use it Marc Kleine-Budde
2016-05-09 10:52 ` Marc Kleine-Budde [this message]
2016-05-09 10:52 ` [PATCH 02/10] can: rx-fifo: introduce software rx-fifo implementation Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 03/10] can: flexcan: calculate default value for imask1 during runtime Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 04/10] can: flexcan: make TX mailbox selectable " Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 05/10] can: flexcan: make use of rx-fifo's irq_offload_simple Marc Kleine-Budde
2016-05-20 11:31 ` Mirza Krak
2016-05-09 10:52 ` [PATCH 06/10] can: flexcan: add missing register definitions Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 07/10] can: flexcan: activate individual RX masking and initialize reg_rximr Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 08/10] can: flexcan: add quirk FLEXCAN_QUIRK_ENABLE_EACEN_RRS Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 09/10] can: flexcan: add support for rx-fifo based software FIFO implementation Marc Kleine-Budde
2016-05-09 10:52 ` [PATCH 10/10] can: flexcan: switch imx6 and vf610 to software based fifo Marc Kleine-Budde
2016-05-09 10:54 ` rx-fifo: add implmentation and switch flexcan driver to use it Marc Kleine-Budde
2016-05-10 8:27 ` Mirza Krak
2016-05-10 12:55 ` Mirza Krak
2016-05-11 9:12 ` Oliver Hartkopp
[not found] ` <CAJ=nTssNPDeGW+pEAHTpa+5ARQCQHfXumE=a=cr9prmjhgeJxQ@mail.gmail.com>
2016-05-11 19:07 ` Mirza Krak
2016-05-13 11:44 ` Tom Evans
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=1462791154-13375-2-git-send-email-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=david@protonic.nl \
--cc=linux-can@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).