From: Marc Kleine-Budde <mkl@pengutronix.de>
To: linux-can@vger.kernel.org
Cc: kernel@pengutronix.de,
Stephane Grosjean <s.grosjean@peak-system.com>,
Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH 22/37] can: pcan_usb: add support of rxerr/txerr counters
Date: Wed, 16 Sep 2020 00:35:12 +0200 [thread overview]
Message-ID: <20200915223527.1417033-23-mkl@pengutronix.de> (raw)
In-Reply-To: <20200915223527.1417033-1-mkl@pengutronix.de>
From: Stephane Grosjean <s.grosjean@peak-system.com>
This patch adds the support of the rx/tx errors CAN counters to the
driver of the PCAN-USB PC-CAN interface from PEAK-System GmbH.
The PCAN-USB is capable of giving back the values of the rx/tx errors
counters, to provide more details and statistics to the linux-can layer.
Getting these values allows the driver to better tune
CAN_ERR_CRTL_TX_xxx and CAN_ERR_CRTL_RX_xxx bits in case of the
interface enters any CAN_STATE_ERROR_xxx state.
Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
drivers/net/can/usb/peak_usb/pcan_usb.c | 133 +++++++++++++++++++++---
1 file changed, 117 insertions(+), 16 deletions(-)
diff --git a/drivers/net/can/usb/peak_usb/pcan_usb.c b/drivers/net/can/usb/peak_usb/pcan_usb.c
index 76468250cabf..63bd2ed96697 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb.c
@@ -41,6 +41,7 @@ MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
#define PCAN_USB_CMD_SN 6
#define PCAN_USB_CMD_REGISTER 9
#define PCAN_USB_CMD_EXT_VCC 10
+#define PCAN_USB_CMD_ERR_FR 11
/* PCAN_USB_CMD_SET_BUS number arg */
#define PCAN_USB_BUS_XCVER 2
@@ -82,6 +83,10 @@ MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
#define PCAN_USB_ERROR_QOVR 0x40
#define PCAN_USB_ERROR_TXQFULL 0x80
+#define PCAN_USB_ERROR_BUS (PCAN_USB_ERROR_BUS_LIGHT | \
+ PCAN_USB_ERROR_BUS_HEAVY | \
+ PCAN_USB_ERROR_BUS_OFF)
+
/* SJA1000 modes */
#define SJA1000_MODE_NORMAL 0x00
#define SJA1000_MODE_INIT 0x01
@@ -101,11 +106,25 @@ MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
#define PCAN_USB_REC_TS 4
#define PCAN_USB_REC_BUSEVT 5
+/* CAN bus events notifications selection mask */
+#define PCAN_USB_ERR_RXERR 0x02 /* ask for rxerr counter */
+#define PCAN_USB_ERR_TXERR 0x04 /* ask for txerr counter */
+
+/* This mask generates an usb packet each time the state of the bus changes.
+ * In other words, its interest is to know which side among rx and tx is
+ * responsible of the change of the bus state.
+ */
+#define PCAN_USB_BERR_MASK (PCAN_USB_ERR_RXERR | PCAN_USB_ERR_TXERR)
+
+/* identify bus event packets with rx/tx error counters */
+#define PCAN_USB_ERR_CNT 0x80
+
/* private to PCAN-USB adapter */
struct pcan_usb {
struct peak_usb_device dev;
struct peak_time_ref time_ref;
struct timer_list restart_timer;
+ struct can_berr_counter bec;
};
/* incoming message context for decoding */
@@ -212,6 +231,16 @@ static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
PCAN_USB_BUS_SILENT_MODE, args);
}
+/* send the cmd to be notified from bus errors */
+static int pcan_usb_set_err_frame(struct peak_usb_device *dev, u8 err_mask)
+{
+ u8 args[PCAN_USB_CMD_ARGS_LEN] = {
+ [0] = err_mask,
+ };
+
+ return pcan_usb_send_cmd(dev, PCAN_USB_CMD_ERR_FR, PCAN_USB_SET, args);
+}
+
static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
{
u8 args[PCAN_USB_CMD_ARGS_LEN] = {
@@ -445,7 +474,7 @@ static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
new_state = CAN_STATE_BUS_OFF;
break;
}
- if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
+ if (n & ~PCAN_USB_ERROR_BUS) {
/*
* trick to bypass next comparison and process other
* errors
@@ -469,7 +498,7 @@ static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
new_state = CAN_STATE_ERROR_WARNING;
break;
}
- if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
+ if (n & ~PCAN_USB_ERROR_BUS) {
/*
* trick to bypass next comparison and process other
* errors
@@ -508,29 +537,50 @@ static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
case CAN_STATE_ERROR_PASSIVE:
cf->can_id |= CAN_ERR_CRTL;
- cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
- CAN_ERR_CRTL_RX_PASSIVE;
+ cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ?
+ CAN_ERR_CRTL_TX_PASSIVE :
+ CAN_ERR_CRTL_RX_PASSIVE;
+ cf->data[6] = mc->pdev->bec.txerr;
+ cf->data[7] = mc->pdev->bec.rxerr;
+
mc->pdev->dev.can.can_stats.error_passive++;
break;
case CAN_STATE_ERROR_WARNING:
cf->can_id |= CAN_ERR_CRTL;
- cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
- CAN_ERR_CRTL_RX_WARNING;
+ cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ?
+ CAN_ERR_CRTL_TX_WARNING :
+ CAN_ERR_CRTL_RX_WARNING;
+ cf->data[6] = mc->pdev->bec.txerr;
+ cf->data[7] = mc->pdev->bec.rxerr;
+
mc->pdev->dev.can.can_stats.error_warning++;
break;
case CAN_STATE_ERROR_ACTIVE:
cf->can_id |= CAN_ERR_CRTL;
cf->data[1] = CAN_ERR_CRTL_ACTIVE;
+
+ /* sync local copies of rxerr/txerr counters */
+ mc->pdev->bec.txerr = 0;
+ mc->pdev->bec.rxerr = 0;
break;
default:
/* CAN_STATE_MAX (trick to handle other errors) */
- cf->can_id |= CAN_ERR_CRTL;
- cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
- mc->netdev->stats.rx_over_errors++;
- mc->netdev->stats.rx_errors++;
+ if (n & PCAN_USB_ERROR_TXQFULL)
+ netdev_dbg(mc->netdev, "device Tx queue full)\n");
+
+ if (n & PCAN_USB_ERROR_RXQOVR) {
+ netdev_dbg(mc->netdev, "data overrun interrupt\n");
+ cf->can_id |= CAN_ERR_CRTL;
+ cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
+ mc->netdev->stats.rx_over_errors++;
+ mc->netdev->stats.rx_errors++;
+ }
+
+ cf->data[6] = mc->pdev->bec.txerr;
+ cf->data[7] = mc->pdev->bec.rxerr;
new_state = mc->pdev->dev.can.state;
break;
@@ -552,6 +602,30 @@ static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
return 0;
}
+/* decode bus event usb packet: first byte contains rxerr while 2nd one contains
+ * txerr.
+ */
+static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir)
+{
+ struct pcan_usb *pdev = mc->pdev;
+
+ /* acccording to the content of the packet */
+ switch (ir) {
+ case PCAN_USB_ERR_CNT:
+
+ /* save rx/tx error counters from in the device context */
+ pdev->bec.rxerr = mc->ptr[0];
+ pdev->bec.txerr = mc->ptr[1];
+ break;
+
+ default:
+ /* reserved */
+ break;
+ }
+
+ return 0;
+}
+
/*
* decode non-data usb message
*/
@@ -606,9 +680,10 @@ static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
break;
case PCAN_USB_REC_BUSEVT:
- /* error frame/bus event */
- if (n & PCAN_USB_ERROR_TXQFULL)
- netdev_dbg(mc->netdev, "device Tx queue full)\n");
+ /* bus event notifications (get rxerr/txerr) */
+ err = pcan_usb_handle_bus_evt(mc, n);
+ if (err)
+ return err;
break;
default:
netdev_err(mc->netdev, "unexpected function %u\n", f);
@@ -792,20 +867,44 @@ static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
return 0;
}
+/* socket callback used to copy berr counters values received through USB */
+static int pcan_usb_get_berr_counter(const struct net_device *netdev,
+ struct can_berr_counter *bec)
+{
+ struct peak_usb_device *dev = netdev_priv(netdev);
+ struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
+
+ *bec = pdev->bec;
+
+ /* must return 0 */
+ return 0;
+}
+
/*
* start interface
*/
static int pcan_usb_start(struct peak_usb_device *dev)
{
struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
+ int err;
/* number of bits used in timestamps read from adapter struct */
peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
+ pdev->bec.rxerr = 0;
+ pdev->bec.txerr = 0;
+
+ /* be notified on error counter changes (if requested by user) */
+ if (dev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+ err = pcan_usb_set_err_frame(dev, PCAN_USB_BERR_MASK);
+ if (err)
+ netdev_warn(dev->netdev,
+ "Asking for BERR reporting error %u\n",
+ err);
+ }
+
/* if revision greater than 3, can put silent mode on/off */
if (dev->device_rev > 3) {
- int err;
-
err = pcan_usb_set_silent(dev,
dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
if (err)
@@ -892,7 +991,8 @@ const struct peak_usb_adapter pcan_usb = {
.name = "PCAN-USB",
.device_id = PCAN_USB_PRODUCT_ID,
.ctrl_count = 1,
- .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
+ .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
+ CAN_CTRLMODE_BERR_REPORTING,
.clock = {
.freq = PCAN_USB_CRYSTAL_HZ / 2 ,
},
@@ -925,4 +1025,5 @@ const struct peak_usb_adapter pcan_usb = {
.dev_encode_msg = pcan_usb_encode_msg,
.dev_start = pcan_usb_start,
.dev_restart_async = pcan_usb_restart_async,
+ .do_get_berr_counter = pcan_usb_get_berr_counter,
};
--
2.28.0
next prev parent reply other threads:[~2020-09-15 22:36 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-15 22:34 [RFC]: can-next 2020-09-16 Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 01/37] can: grcan: fix spelling mistake "buss" -> "bus" Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 02/37] can: flexcan: fix spelling mistake "reserverd" -> "reserved" Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 03/37] can: include: fix spelling mistakes Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 04/37] can: net: " Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 05/37] can: drivers: " Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 06/37] can: raw: fix indention Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 07/37] can: slcan: update dead link Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 08/37] can: softing: " Marc Kleine-Budde
2020-09-15 22:34 ` [PATCH 09/37] can: remove "WITH Linux-syscall-note" from SPDX tag of C files Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 10/37] can: dev: can_put_echo_skb(): print number of echo_skb that is occupied Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 11/37] can: dev: can_put_echo_skb(): propagate error in case of errors Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 12/37] can: dev: can_change_state(): print human readable state change messages Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 13/37] can: dev: can_bus_off(): print scheduling of restart if activated Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 14/37] can: c_can: Remove unused inline function Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 15/37] can: mcba_usb: remove redundant initialization of variable err Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 16/37] can: mscan: mark expected switch fall-through Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 17/37] can: ti_hecc: convert to devm_platform_ioremap_resource_byname() Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 18/37] can: peak_usb: convert to use le32_add_cpu() Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 19/37] can: peak_canfd: Remove unused macros Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 20/37] can: pch_can: use generic power management Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 21/37] can: pcan_usb: Document the commands sent to the device Marc Kleine-Budde
2020-09-15 22:35 ` Marc Kleine-Budde [this message]
2020-09-15 22:35 ` [PATCH 23/37] can: spi: Kconfig: remove unneeded dependencies form Kconfig symbols Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 24/37] dt-bindings: can: mcp251x: change example interrupt type to IRQ_TYPE_LEVEL_LOW Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 25/37] dt-bindings: can: mcp251x: document GPIO support Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 26/37] can: mcp251x: sort include files alphabetically Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 27/37] can: mcp251x: add GPIO support Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 28/37] can: mcp251x: Use readx_poll_timeout() helper Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 29/37] can: mcp251x: add support for half duplex controllers Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 30/37] can: mscan: mpc5xxx_can: update contact email Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 31/37] can: mscan: simplify clock enable/disable Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 32/37] can: rx-offload: can_rx_offload_add_manual(): add new initialization function Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 33/37] dt-binding: can: mcp25xxfd: document device tree bindings Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 34/37] can: mcp25xxfd: add regmap infrastructure Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 35/37] can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 36/37] can: mcp25xxfd: add listen-only mode Marc Kleine-Budde
2020-09-15 22:35 ` [PATCH 37/37] MAINTAINERS: Add entry for Microchip MCP25XXFD SPI-CAN network driver Marc Kleine-Budde
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=20200915223527.1417033-23-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=kernel@pengutronix.de \
--cc=linux-can@vger.kernel.org \
--cc=s.grosjean@peak-system.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).