From patchwork Tue Jun 14 12:28:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2077 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 3A8F43F03E for ; Tue, 14 Jun 2022 14:28:44 +0200 (CEST) Received: by mail-ej1-f71.google.com with SMTP id a9-20020a17090682c900b0070b513b9dc4sf2798069ejy.4 for ; Tue, 14 Jun 2022 05:28:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655209724; cv=pass; d=google.com; s=arc-20160816; b=lY79aJYqcrdoKoQn0i0Or/AovNU1Hxy7DX1wiiD1VVfhnT5NEmXzg9rSxs5PQ6065d CT1PYwXQxsiLMG9WWZZU8VtaIIiu4Z06lKSw47R2HqEppDsYRUeoutyy1NBKA0TC4HmP jt+tNIhOIIQ9A+daNOuW5FnIJ06lEFnA8Go5DGtDtOdKyb09jCaX2dZVj0BQ3mTL9ma9 j7YCtfQbSiHeUbKaO5NzTg/BIua2SZ1EcMP9ez2qt66+W/K8jPtFyCI34NEhCiYy+CG4 t4BL2RIlhSm89iWraKR4OLt+VlHof0oNgNKHv0bL63SPeFAF5uNhNo7sUhpylgMLoKIe gpJQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=+s/fanNaBH5QndgQhqaRG+5D14HOzuR3zU++YQx1cSc=; b=vY6MnjL9VvxNJSFXgW64zolgcxMyR807NGXV1R5z9SciGIu2zImckRsVAzBEFNktZ1 KAgfzrQhcK/Iv5m1auVJUObHyESPi8OaXxG2Tbly8YNiIwP5Mrrbu9Cri9kTjzRXMUxN pS9K39lxitIofF34KjPpgovJVxZwFfU9KDlYm7yuyJyrlZcXh+sVqMS09pml0mckpb1V VysC/89dXhzWu9RcmakGj/V5rcpM2V8DFmXXvDgEqF/m3unbT6S6crR9b4OsVGij8xgj hJ9agfxP5YnXQ6Wgj+fZcX4rm4mztAp4U+7TPFm4Q7BWdSPiFicdDMNRIjJpZMkEPpJl AfDw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Imdk9ZoA; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=+s/fanNaBH5QndgQhqaRG+5D14HOzuR3zU++YQx1cSc=; b=a185V9fy/QqVDWfuUAxngsGuj66ynI17NsH4Ertj/BzyPbGT79NKz6hn36x0olCqpn lMm2E49YV2SQtdHwDwiWPi8bbpAavprmcs2IR3m4jz8xjOOVS7zr6D36OiZHvyoF2efn RhHL7y5oesP8mffG+jrH0MVV9v7uzeh+iPXS4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=+s/fanNaBH5QndgQhqaRG+5D14HOzuR3zU++YQx1cSc=; b=qHN1+QAv3yW/gxyiFGhX9F/FSQwbCbwWac5QCisoodJJy/Ilqy3zv7Ek7Y9uvj0eg6 895Lm33HT7kYpw1riFgaSyiXOv012+Tbtux87vsY4+FqtTJLDW9jcA0n7lNjkU3x0AXw kkZtOtjhVA1RsgvY86dLaGPcCDpEIkjKZ5yv/7m3aFerc0si+uhLaZ+49ZumMt3sPsrj rT8ITVy3G9qviTd/TDxSmKfnlQLBXCnZdJPiLUF2MqiXG3Xek/QDn6fAs1UMiQw6Evlx 8nN4ogafSJKMMw7P8kHthAwjozkNrK+CNfywTAQGfIIO4c3EimPNfGwyAdijxRHeN9Mh YI4w== X-Gm-Message-State: AJIora9WXfGxAQBSR9ZMzYmnXNfvb2G3zmHNcwO+aFubqkvg82G/Hd51 /8lV85qWEj5wMhN6lWF5LsHTxuoj X-Google-Smtp-Source: AGRyM1t5pUFzvwBxU+SKbRF2jMyDTAD6SFwFJ5Wm6dsT9cU07zt9SiGvHqNbJe9AcVZI36aRtFKTNQ== X-Received: by 2002:a17:906:7b82:b0:6f3:ee8d:b959 with SMTP id s2-20020a1709067b8200b006f3ee8db959mr4266243ejo.458.1655209724056; Tue, 14 Jun 2022 05:28:44 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:2ad6:b0:6fe:976a:7a5 with SMTP id m22-20020a1709062ad600b006fe976a07a5ls992602eje.7.gmail; Tue, 14 Jun 2022 05:28:43 -0700 (PDT) X-Received: by 2002:a17:906:4f87:b0:718:c7ce:bd64 with SMTP id o7-20020a1709064f8700b00718c7cebd64mr4039148eju.507.1655209722979; Tue, 14 Jun 2022 05:28:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655209722; cv=none; d=google.com; s=arc-20160816; b=GDCfnVOH7vsmPPSyma0zhpSC+2nEBfEJMIofEsmcrS6Kt0VNz7+ADE11pZIKRjUBLw KGP+I7S1duR3gVG2RGvkdYqBooee+eoEVQUoaZvsaOh4F9EUmo4D6bkz1w2OJLZdSJPn 9ut11kw1u4xpWCjC4hHScHJNX3Js9Kc8OMtgZE4ykTKXILRy5QMySkyJfqP9TjckbYzO C1GDQwolZrzY7oWzsGtJOmv+Nz3eFDifXsZIV/uTTFaDLqvRrAd0P2/PsKT/j8qLXUR7 TO2+xSMub2zSMXolQ88ldMuVyA9JsmQqj9n4aMhji2Jb/DRiUrvx/IxuZCkgrlD4IOIh ebSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=jYzinqDMpP0ikE9sweLWXVziLbiFyEvA5RI0XjmyrwA=; b=mQtEuiDryWCyvMf8MeqhNvuPUYHwCxHjwXBSupTG1+YWnlEK2VTUdwFQ/rbUN7VozW mJeAm1fPkKkPVEFq5+8PZhFRx/4nYuffV5HjeuQo8CLl/XlsEkmj0VSAyOuz0vLoPvGl YXCeCJjHIqWn+Do6TSuC19vx327fc4QcMXpfJ2mB2G8gzsH00yb3zjZFDkkazRccqHi0 aTqlisby6PnUpihQKpVlmTy6S0HS+p3hd1+/IgIzDtu1M6Sa3+VXKtXd6oDnJimMnGD2 9W5YTCJc/uHBbhge9brb7n+xXePKt6T3hrFwna03W3KEKYE1tPLxDbgykeXrOdBAEXDh MJ4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Imdk9ZoA; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id o9-20020aa7d3c9000000b0042dceef26c2sor4185809edr.51.2022.06.14.05.28.42 for (Google Transport Security); Tue, 14 Jun 2022 05:28:42 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:aa7:c604:0:b0:42d:cffb:f4dc with SMTP id h4-20020aa7c604000000b0042dcffbf4dcmr5869600edq.270.1655209722774; Tue, 14 Jun 2022 05:28:42 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.amarulasolutions.com (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id z22-20020a17090655d600b006f3ef214e2csm5087043ejp.146.2022.06.14.05.28.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jun 2022 05:28:42 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Amarula patchwork , Oliver Hartkopp , michael@amarulasolutions.com, Dario Binacchi , "David S. Miller" , Eric Dumazet , Greg Kroah-Hartman , Jakub Kicinski , Jiri Slaby , Marc Kleine-Budde , Paolo Abeni , Sebastian Andrzej Siewior , Vincent Mailhol , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v4 12/12] can: slcan: extend the protocol with CAN state info Date: Tue, 14 Jun 2022 14:28:21 +0200 Message-Id: <20220614122821.3646071-13-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220614122821.3646071-1-dario.binacchi@amarulasolutions.com> References: <20220614122821.3646071-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Imdk9ZoA; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , It extends the protocol to receive the adapter CAN state changes (warning, busoff, etc.) and forward them to the netdev upper levels. Signed-off-by: Dario Binacchi --- Changes in v4: - Add description of slc_bump_state() function. - Remove check for the 's' character at the beggining of the function. It was already checked by the caller function. - Protect decoding against the case the frame len is longer than the received data (add SLC_STATE_FRAME_LEN macro). - Set cf to NULL in case of alloc_can_err_skb() failure. - Some small changes to make the decoding more readable. - Use the character 'b' instead of 'f' for bus-off state. Changes in v3: - Drop the patch "can: slcan: simplify the device de-allocation". - Add the patch "can: netlink: dump bitrate 0 if can_priv::bittiming.bitrate is -1U". Changes in v2: - Continue error handling even if no skb can be allocated. drivers/net/can/slcan/slcan-core.c | 74 +++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c index 6c7c815eaf45..e2d7645ff8d2 100644 --- a/drivers/net/can/slcan/slcan-core.c +++ b/drivers/net/can/slcan/slcan-core.c @@ -78,7 +78,11 @@ MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces"); #define SLC_CMD_LEN 1 #define SLC_SFF_ID_LEN 3 #define SLC_EFF_ID_LEN 8 - +#define SLC_STATE_LEN 1 +#define SLC_STATE_BE_RXCNT_LEN 3 +#define SLC_STATE_BE_TXCNT_LEN 3 +#define SLC_STATE_FRAME_LEN (1 + SLC_CMD_LEN + SLC_STATE_BE_RXCNT_LEN + \ + SLC_STATE_BE_TXCNT_LEN) struct slcan { struct can_priv can; int magic; @@ -255,6 +259,72 @@ static void slc_bump_frame(struct slcan *sl) dev_kfree_skb(skb); } +/* A change state frame must contain state info and receive and transmit + * error counters. + * + * Examples: + * + * sb256256 : state bus-off: rx counter 256, tx counter 256 + * sa057033 : state active, rx counter 57, tx counter 33 + */ +static void slc_bump_state(struct slcan *sl) +{ + struct net_device *dev = sl->dev; + struct sk_buff *skb; + struct can_frame *cf; + char *cmd = sl->rbuff; + u32 rxerr, txerr; + enum can_state state, rx_state, tx_state; + + switch (cmd[1]) { + case 'a': + state = CAN_STATE_ERROR_ACTIVE; + break; + case 'w': + state = CAN_STATE_ERROR_WARNING; + break; + case 'p': + state = CAN_STATE_ERROR_PASSIVE; + break; + case 'b': + state = CAN_STATE_BUS_OFF; + break; + default: + return; + } + + if (state == sl->can.state || sl->rcount < SLC_STATE_FRAME_LEN) + return; + + cmd += SLC_STATE_BE_RXCNT_LEN + SLC_CMD_LEN + 1; + cmd[SLC_STATE_BE_TXCNT_LEN] = 0; + if (kstrtou32(cmd, 10, &txerr)) + return; + + *cmd = 0; + cmd -= SLC_STATE_BE_RXCNT_LEN; + if (kstrtou32(cmd, 10, &rxerr)) + return; + + skb = alloc_can_err_skb(dev, &cf); + if (skb) { + cf->data[6] = txerr; + cf->data[7] = rxerr; + } else { + cf = NULL; + } + + tx_state = txerr >= rxerr ? state : 0; + rx_state = txerr <= rxerr ? state : 0; + can_change_state(dev, cf, tx_state, rx_state); + + if (state == CAN_STATE_BUS_OFF) + can_bus_off(dev); + + if (skb) + netif_rx(skb); +} + /* An error frame can contain more than one type of error. * * Examples: @@ -388,6 +458,8 @@ static void slc_bump(struct slcan *sl) return slc_bump_frame(sl); case 'e': return slc_bump_err(sl); + case 's': + return slc_bump_state(sl); default: return; }