From: David Jander <david@protonic.nl>
To: David Jander <david@protonic.nl>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>,
linux-can@vger.kernel.org,
Wolfgang Grandegger <wg@grandegger.com>,
Alexander Stein <alexander.stein@systec-electronic.com>
Subject: Re: [PATCH 03/14] can: rx-fifo: Change to do controller off-load in interrupt and NAPI poll
Date: Mon, 10 Nov 2014 12:00:46 +0100 [thread overview]
Message-ID: <20141110120046.28f1d9ca@archvile> (raw)
In-Reply-To: <1415262853-22907-4-git-send-email-david@protonic.nl>
On Thu, 6 Nov 2014 09:34:02 +0100
David Jander <david@protonic.nl> wrote:
> The idea is to use rx-fifo from interrupt context and take away the need
> for NAPI polling from the driver. Currently no support for error-handling
> is included.
>
> Signed-off-by: David Jander <david@protonic.nl>
> ---
> drivers/net/can/dev.c | 213
> +++++++++++++++++++++++++++++++++++++----------- include/linux/can/dev.h |
> 29 +++++-- 2 files changed, 188 insertions(+), 54 deletions(-)
>
> diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c
> index 930b9f4..9b17592 100644
> --- a/drivers/net/can/dev.c
> +++ b/drivers/net/can/dev.c
> @@ -26,6 +26,7 @@
> #include <linux/can/skb.h>
> #include <linux/can/netlink.h>
> #include <linux/can/led.h>
> +#include <linux/circ_buf.h>
> #include <net/rtnetlink.h>
>
> #define MOD_DESC "CAN device driver interface"
> @@ -281,6 +282,14 @@ static bool can_rx_fifo_ge(struct can_rx_fifo *fifo,
> unsigned int a, unsigned in return a <= b;
> }
>
> +static bool can_rx_fifo_le(struct can_rx_fifo *fifo, unsigned int a,
> unsigned int b) +{
> + if (fifo->inc)
> + return a <= b;
> + else
> + return a >= b;
> +}
> +
> static unsigned int can_rx_fifo_inc(struct can_rx_fifo *fifo, unsigned int
> *val) {
> if (fifo->inc)
> @@ -305,27 +314,101 @@ static u64 can_rx_fifo_mask_high(struct can_rx_fifo
> *fifo) return ~0LLU >> (64 - fifo->high_first + fifo->high_last - 1) <<
> fifo->high_last; }
>
> +static int can_rx_fifo_read_napi_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));
> +
> + 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);
> + int work_done = 0;
> + int ret;
> + unsigned int head;
> + unsigned int tail;
> +
> +restart_poll:
> + /* handle mailboxes */
> + head = smp_load_acquire(&fifo->ring_head);
> + tail = fifo->ring_tail;
> + while ((CIRC_CNT(head, tail, fifo->ring_size) >= 1) &&
> + (work_done < quota)) {
> + ret = can_rx_fifo_read_napi_frame(fifo, tail);
> + work_done += ret;
> + tail = (tail + 1) & (fifo->ring_size -1);
> + smp_store_release(&fifo->ring_tail, tail);
> + }
> +
> + if (work_done < quota) {
> + napi_complete(napi);
> +
> + /* Check if there was another interrupt */
> + head = smp_load_acquire(&fifo->ring_head);
> + if ((CIRC_CNT(head, tail, fifo->ring_size) >= 1) &&
> + napi_reschedule(&fifo->napi))
> + goto restart_poll;
> + }
> +
> + can_led_event(fifo->dev, CAN_LED_EVENT_RX);
> +
> + return work_done;
> +}
> +
> int can_rx_fifo_add(struct net_device *dev, struct can_rx_fifo *fifo)
> {
> + unsigned int weight;
> fifo->dev = dev;
>
> if ((fifo->low_first < fifo->high_first) &&
> - (fifo->high_first < fifo->high_last))
> + (fifo->high_first < fifo->high_last)) {
> fifo->inc = true;
> - else if ((fifo->low_first > fifo->high_first) &&
> - (fifo->high_first > fifo->high_last))
> + weight = fifo->high_last - fifo->low_first;
> + } else if ((fifo->low_first > fifo->high_first) &&
> + (fifo->high_first > fifo->high_last)) {
> fifo->inc = false;
> - else
> + weight = fifo->low_first - fifo->high_last;
> + } else {
> return -EINVAL;
> + }
>
> - if (!fifo->read_pending || !fifo->mailbox_enable_mask ||
> - !fifo->mailbox_disable || !fifo->mailbox_receive)
> + if (!fifo->mailbox_enable_mask || !fifo->mailbox_move_to_buffer ||
> + !fifo->mailbox_enable)
> return -EINVAL;
>
> + /* 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;
> +
> + /* Take care of NAPI handling */
> + netif_napi_add(dev, &fifo->napi, can_rx_fifo_napi_poll, weight);
> +
> /* init variables */
> fifo->mask_low = can_rx_fifo_mask_low(fifo);
> fifo->mask_high = can_rx_fifo_mask_high(fifo);
> - fifo->next = fifo->low_first;
> + fifo->scan_high_first = false;
> fifo->active = fifo->mask_low | fifo->mask_high;
> fifo->mailbox_enable_mask(fifo, fifo->active);
>
> @@ -338,60 +421,94 @@ int can_rx_fifo_add(struct net_device *dev, struct
> can_rx_fifo *fifo) }
> EXPORT_SYMBOL_GPL(can_rx_fifo_add);
>
> -int can_rx_fifo_poll(struct can_rx_fifo *fifo, int quota)
> +static unsigned int can_rx_fifo_offload_if_full(struct can_rx_fifo *fifo,
> unsigned int n) +{
> + unsigned int head = fifo->ring_head;
> + unsigned int tail = ACCESS_ONCE(fifo->ring_tail);
> + unsigned int ret = 0;
> +
> + if (CIRC_SPACE(head, tail, fifo->ring_size) >= 1) {
> + ret = fifo->mailbox_move_to_buffer(fifo, &fifo->ring[head],
> n);
> + if (ret)
> + smp_store_release(&fifo->ring_head,
> + (head + 1) & (fifo->ring_size - 1));
> + } else {
> + ret = fifo->mailbox_move_to_buffer(fifo, &fifo->overflow,
> n);
> + if (ret)
> + fifo->dev->stats.rx_dropped++;
> + }
> + return ret;
> +}
> +
> +int can_rx_fifo_irq_offload(struct can_rx_fifo *fifo)
> {
> - int received = 0;
> - u64 pending;
> - unsigned int mb;
> -
> - do {
> - pending = fifo->read_pending(fifo);
> - pending &= fifo->active;
> -
> - if (!(pending & BIT_ULL(fifo->next))) {
> - /*
> - * Wrap around only if:
> - * - we are in the upper group and
> - * - there is a CAN frame in the first mailbox
> - * of the lower group.
> - */
> - if (can_rx_fifo_ge(fifo, fifo->next,
> fifo->high_first) &&
> - (pending & BIT_ULL(fifo->low_first))) {
> - fifo->next = fifo->low_first;
> -
> - fifo->active |= fifo->mask_high;
> - fifo->mailbox_enable_mask(fifo,
> fifo->mask_high);
> - } else {
> - break;
> - }
> + unsigned int i;
> + unsigned int ret;
> + unsigned int received = 0;
> +
> + if (fifo->scan_high_first) {
> + for (i = fifo->high_first;
> + can_rx_fifo_le(fifo, i, fifo->high_last);
> + can_rx_fifo_inc(fifo, &i)) {
> + received += can_rx_fifo_offload_if_full(fifo, i);
> + fifo->active |= BIT_ULL(i);
> + fifo->mailbox_enable(fifo, i);
> }
> + }
>
> - mb = can_rx_fifo_inc(fifo, &fifo->next);
> + /* Copy and disable FULL MBs */
> + for (i = fifo->low_first; can_rx_fifo_le(fifo, i, fifo->high_last);
> + can_rx_fifo_inc(fifo, &i)) {
> + if (!(fifo->active & BIT_ULL(i)))
> + continue;
> + ret = can_rx_fifo_offload_if_full(fifo, i);
> + if (!ret)
> + break;
> + received += ret;
> + fifo->active &= ~BIT_ULL(i);
> + }
>
> - /* disable mailbox */
> - fifo->active &= ~BIT_ULL(mb);
> - fifo->mailbox_disable(fifo, mb);
> + if (can_rx_fifo_ge(fifo, i, fifo->high_first) && fifo->high_first)
Arrgh! There is a typo in this line, introduced while constantly renaming the
fifo->scan_high_first variable :-(
This line should read:
+ if (can_rx_fifo_ge(fifo, i, fifo->high_first) && fifo->scan_high_first)
This bug just causes a lot of unnecessary dmesg pollution, sorry for that!
> + netdev_warn(fifo->dev, "%s: RX order cannot be guaranteed."
> + " (count=%d)\n", __func__, i);
>
> - fifo->mailbox_receive(fifo, mb);
> + fifo->scan_high_first = false;
>
> - if (fifo->next == fifo->high_first) {
> - fifo->active |= fifo->mask_low;
> - fifo->mailbox_enable_mask(fifo, fifo->mask_low);
> - }
> + /* No EMPTY MB in first half? */
> + if (can_rx_fifo_ge(fifo, i, fifo->high_first)) {
> + /* Re-enable all disabled MBs */
> + fifo->active = fifo->mask_low | fifo->mask_high;
> + fifo->mailbox_enable_mask(fifo, fifo->active);
> +
> + /* Next time we need to check the second half first */
> + fifo->scan_high_first = true;
> + }
>
> - received++;
> - quota--;
> - } while (quota);
> + if (received)
> + napi_schedule(&fifo->napi);
>
> return received;
> }
> -EXPORT_SYMBOL_GPL(can_rx_fifo_poll);
> +EXPORT_SYMBOL_GPL(can_rx_fifo_irq_offload);
> +
> +void can_rx_fifo_napi_enable(struct can_rx_fifo *fifo)
> +{
> + napi_enable(&fifo->napi);
> +}
> +EXPORT_SYMBOL_GPL(can_rx_fifo_napi_enable);
> +
> +void can_rx_fifo_napi_disable(struct can_rx_fifo *fifo)
> +{
> + napi_disable(&fifo->napi);
> +}
> +EXPORT_SYMBOL_GPL(can_rx_fifo_napi_disable);
>
> -u64 can_rx_fifo_get_active_mb_mask(const struct can_rx_fifo *fifo)
> +void can_rx_fifo_del(struct can_rx_fifo *fifo)
> {
> - return fifo->active;
> + kfree(fifo->ring);
> + netif_napi_del(&fifo->napi);
> }
> -EXPORT_SYMBOL_GPL(can_rx_fifo_get_active_mb_mask);
> +EXPORT_SYMBOL_GPL(can_rx_fifo_del);
>
> /*
> * Local echo of CAN messages
> diff --git a/include/linux/can/dev.h b/include/linux/can/dev.h
> index ed46f7d..66b0228 100644
> --- a/include/linux/can/dev.h
> +++ b/include/linux/can/dev.h
> @@ -71,18 +71,33 @@ struct can_rx_fifo {
> unsigned int high_first;
> unsigned int high_last; /* not needed during runtime
> */
> - u64 (*read_pending)(struct can_rx_fifo *rx_fifo);
> void (*mailbox_enable_mask)(struct can_rx_fifo *rx_fifo, u64 mask);
> - void (*mailbox_disable)(struct can_rx_fifo *rx_fifo, unsigned int
> mb);
> - void (*mailbox_receive)(struct can_rx_fifo *rx_fifo, unsigned int
> mb);
> + void (*mailbox_enable)(struct can_rx_fifo *rx_fifo, unsigned int
> mb);
> + unsigned int (*mailbox_move_to_buffer)(struct can_rx_fifo *rx_fifo,
> + struct can_frame *frame, unsigned int mb);
>
> u64 mask_low;
> u64 mask_high;
> u64 active;
>
> - unsigned int next;
> + unsigned int scan_high_first;
>
> bool inc;
> +
> + /* CAN frame ring buffer. Will be allocated to an appropriate size
> */
> + struct can_frame *ring;
> +
> + /*
> + * Overflow buffer: This will work sort of as /dev/null if the ring-
> + * buffer is full. We don't want to bother the user with taking care
> + * of that situation, so we just pass it the overflow buffer
> instead.
> + */
> + struct can_frame overflow;
> +
> + size_t ring_size;
> + unsigned int ring_head;
> + unsigned int ring_tail;
> + struct napi_struct napi;
> };
>
> /*
> @@ -127,8 +142,10 @@ u8 can_dlc2len(u8 can_dlc);
> u8 can_len2dlc(u8 len);
>
> int can_rx_fifo_add(struct net_device *dev, struct can_rx_fifo *fifo);
> -int can_rx_fifo_poll(struct can_rx_fifo *fifo, int quota);
> -u64 can_rx_fifo_get_active_mb_mask(const struct can_rx_fifo *fifo);
> +int can_rx_fifo_irq_offload(struct can_rx_fifo *fifo);
> +void can_rx_fifo_napi_enable(struct can_rx_fifo *fifo);
> +void can_rx_fifo_napi_disable(struct can_rx_fifo *fifo);
> +void can_rx_fifo_del(struct can_rx_fifo *fifo);
>
> struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max);
> void free_candev(struct net_device *dev);
Best regards,
--
David Jander
Protonic Holland.
next prev parent reply other threads:[~2014-11-10 11:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-06 8:33 [PATCH V4 00/14] CAN: Add rx-fifo support and port flexcan to it David Jander
2014-11-06 8:34 ` [PATCH 01/14] can: dev: add preliminary rx-fifo David Jander
2014-11-06 8:34 ` [PATCH 02/14] can: rx-fifo: Increase MB size limit from 32 to 64 David Jander
2014-11-06 8:41 ` Marc Kleine-Budde
2014-11-06 16:03 ` David Jander
2014-11-06 16:05 ` Marc Kleine-Budde
2014-11-06 16:20 ` David Jander
2014-11-07 8:40 ` Marc Kleine-Budde
2014-11-07 10:28 ` David Jander
2014-11-06 8:34 ` [PATCH 03/14] can: rx-fifo: Change to do controller off-load in interrupt and NAPI poll David Jander
2014-11-10 11:00 ` David Jander [this message]
2014-11-06 8:34 ` [PATCH 04/14] can: rx-fifo: fix long lines David Jander
2014-11-06 8:34 ` [PATCH 05/14] can: rx-fifo: Add can_rx_fifo_reset() function David Jander
2014-11-06 8:34 ` [PATCH 06/14] can: rx-fifo: remove obsolete comment David Jander
2014-11-06 8:34 ` [PATCH 07/14] can: rx-fifo: Add support for can state tracking and error polling David Jander
2014-11-06 8:34 ` [PATCH 08/14] can: rx-fifo: Add support for simple irq offloading David Jander
2014-11-06 8:34 ` [PATCH 09/14] can: rx-fifo: Add documentation to struct can_rx_fifo David Jander
2014-11-06 8:34 ` [PATCH 10/14] can: flexcan: add documentation about mailbox organizaiton David Jander
2014-11-06 8:34 ` [PATCH 11/14] can: flexcan: rename crl2 -> ctrl2 David Jander
2014-11-06 8:34 ` [PATCH 12/14] can: flexcan: replace open coded mailbox code by proper defines David Jander
2014-11-06 8:34 ` [PATCH 13/14] can: flexcan: Add support for RX-FIFO David Jander
2014-11-06 8:34 ` [PATCH 14/14] can: flexcan: Add MB/Fifo specific column to comment table of IP versions David Jander
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=20141110120046.28f1d9ca@archvile \
--to=david@protonic.nl \
--cc=alexander.stein@systec-electronic.com \
--cc=linux-can@vger.kernel.org \
--cc=mkl@pengutronix.de \
--cc=wg@grandegger.com \
/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).