linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Thomas Körper" <Thomas.Koerper@esd.eu>
To: Andri Yngvason <andri.yngvason@marel.com>
Cc: "linux-can@vger.kernel.org" <linux-can@vger.kernel.org>
Subject: AW: [PATCH V5 1/1] can: Add support for esd CAN PCIe/402 card
Date: Tue, 17 Mar 2015 07:30:02 +0100	[thread overview]
Message-ID: <8CE1D0B9BFD2404DA079DDE1814A6F2E03294EFD39B3@esd-s3.esd.local> (raw)
In-Reply-To: <20150316124621.14895.31220@shannon>

Hi Andri,

I've looked at the sources you mentioned, but I'm a little bit unsure now /
the handling seems not perfectly consistent to me. (flexcan calls
can_change_state() with tx/rx_state of 0 in the bus off path. Only
kvaser_usb counts rx_dropped++ if skb alloc failed, and setting 
cf->data[6]/cf->data[7] to the counter values seems also rarely used)

...so let me show the reworked function first before I post a new patch:

static void
handle_core_msg_errstatechange(struct acc_core *core,
			       const struct acc_bmmsg_errstatechange *msg)
{
	struct acc_net_priv *priv = netdev_priv(core->net_dev);
	struct net_device_stats *stats = &core->net_dev->stats;
	struct can_frame *cf;
	struct sk_buff *skb;

	skb = alloc_can_err_skb(core->net_dev, &cf);
	if (skb) {
		enum can_state new_state;
		u8 txerr;
		u8 rxerr;

		txerr = (u8)(msg->reg_status >> 8);
		rxerr = (u8)msg->reg_status;

		cf->data[6] = txerr;
		cf->data[7] = rxerr;

		if (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_BS) {
			new_state = CAN_STATE_BUS_OFF;
		} else if (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_EP) {
			new_state = CAN_STATE_ERROR_PASSIVE;
		} else if (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_ES) {
			new_state = CAN_STATE_ERROR_WARNING;
		} else {
			new_state = CAN_STATE_ERROR_ACTIVE;
		}

		if (new_state != priv->can.state) {
			enum can_state tx_state, rx_state;

			tx_state = (txerr >= rxerr) ? new_state : 0;
			rx_state = (rxerr >= txerr) ? new_state : 0;

			can_change_state(core->net_dev, cf, tx_state, rx_state);
		}

		netif_rx(skb);
		stats->rx_packets++;
		stats->rx_bytes += cf->can_dlc;
	} else {
		stats->rx_dropped++;
	}

	if (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_BS) {
		acc_write32(core, ACC_CORE_OF_TX_ABORT_MASK, 0xffff);
		can_bus_off(core->net_dev);
	}
}


Regards,
    Thomas


-----Ursprüngliche Nachricht-----
Von: Andri Yngvason [mailto:andri.yngvason@marel.com] 
Gesendet: Montag, 16. März 2015 13:46
An: linux-can@vger.kernel.org
Cc: Thomas Körper
Betreff: Re: [PATCH V5 1/1] can: Add support for esd CAN PCIe/402 card

Quoting Thomas Körper (2015-03-16 12:15:13)
[...]
> +static void
> +handle_core_msg_errstatechange(struct acc_core *core,
> +                              const struct acc_bmmsg_errstatechange *msg)
> +{
> +       struct acc_net_priv *priv = netdev_priv(core->net_dev);
> +       struct net_device_stats *stats = &core->net_dev->stats;
> +       struct can_frame *cf;
> +       struct sk_buff *skb;
> +       int is_busoff;
> +       int is_passive;
> +       int is_warning;
> +       u8 txerr;
> +       u8 rxerr;
> +
> +       txerr = (u8)(msg->reg_status >> 8);
> +       rxerr = (u8)msg->reg_status;
> +       is_warning = (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_ES) != 0;
> +       is_passive = (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_EP) != 0;
> +       is_busoff = (msg->reg_status & ACC_REG_STATUS_MASK_STATUS_BS) != 0;
> +
> +       skb = alloc_can_err_skb(core->net_dev, &cf);
> +       if (skb) {
> +               if (is_busoff) {
> +                       priv->can.state = CAN_STATE_BUS_OFF;
> +                       /* bus-offs counted by can_bus_off() */
> +                       cf->can_id |= CAN_ERR_BUSOFF;
> +               } else if (is_passive) {
> +                       priv->can.state = CAN_STATE_ERROR_PASSIVE;
> +                       priv->can.can_stats.error_passive++;
> +                       cf->data[1] = (txerr > rxerr) ?
> +                                       CAN_ERR_CRTL_TX_PASSIVE :
> +                                       CAN_ERR_CRTL_RX_PASSIVE;
> +                       cf->can_id |= CAN_ERR_CRTL;
> +                       cf->data[6] = txerr;
> +                       cf->data[7] = rxerr;
> +               } else if (is_warning) {
> +                       priv->can.state = CAN_STATE_ERROR_WARNING;
> +                       priv->can.can_stats.error_warning++;
> +                       cf->data[1] = (txerr > rxerr) ?
> +                                       CAN_ERR_CRTL_TX_WARNING :
> +                                       CAN_ERR_CRTL_RX_WARNING;
> +                       cf->can_id |= CAN_ERR_CRTL;
> +                       cf->data[6] = txerr;
> +                       cf->data[7] = rxerr;
> +               } else {
> +                       priv->can.state = CAN_STATE_ERROR_ACTIVE;
> +                       /* restarts counted in dev.c */
>
There is now a back-to-error-active message available for use, so you can do
    cf->can_id = CAN_ERR_CRTL;
    cf->data[1] = CAN_ERROR_CRTL_ACTIVE.
However, you could shave off a few lines in this code by using
can_change_state().
>
> +               }
> +
> +               netif_rx(skb);
> +               stats->rx_packets++;
> +               stats->rx_bytes += cf->can_dlc;
> +       }
> +
> +       if (is_busoff) {
> +               acc_write32(core, ACC_CORE_OF_TX_ABORT_MASK, 0xffff);
> +               can_bus_off(core->net_dev);
> +       }
> +}
> +
[...]

can_change_state() is now used in mscan, flexcan, sja1000, kvaser and peak.
Those drivers should provide sufficent examples for you to learn how to use the
function.

Best regards,
Andri Yngvason



  parent reply	other threads:[~2015-03-17  6:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-16 12:15 [PATCH V5 1/1] can: Add support for esd CAN PCIe/402 card Thomas Körper
2015-03-16 12:46 ` Andri Yngvason
2015-03-16 13:35   ` Marc Kleine-Budde
2015-03-17  6:30   ` Thomas Körper [this message]
2015-03-17  7:26     ` Ahmed S. Darwish
2015-03-17 10:10       ` Andri Yngvason
2015-03-17 10:33     ` AW: " Andri Yngvason
2015-03-17 21:27 ` Marc Kleine-Budde
2015-03-18  5:08   ` Thomas Körper

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=8CE1D0B9BFD2404DA079DDE1814A6F2E03294EFD39B3@esd-s3.esd.local \
    --to=thomas.koerper@esd.eu \
    --cc=andri.yngvason@marel.com \
    --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).