From patchwork Tue Jun 14 12:28:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2076 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id CB8D33F03E for ; Tue, 14 Jun 2022 14:28:42 +0200 (CEST) Received: by mail-ed1-f71.google.com with SMTP id x15-20020a05640226cf00b004318eab9feasf6085988edd.12 for ; Tue, 14 Jun 2022 05:28:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655209722; cv=pass; d=google.com; s=arc-20160816; b=cs/j2IHBtq4A3PULKXTK4Pq3M3m3VFCaGtsaEzMH8seRqykSIskRSD64pTHW2R7cOa gcmd3/Pvydwa+sSFZhPhSwF2w1XaC043vxp04sMG+4qak/WWtls1wrPZW813HHCsaEYR 3L0PvbXyRAlNA1YelLb/6T+HiwoZrAod4tXVjUjn0VHEXw1k8X1b27x7gWYHVvpbE1P7 LOn9Vx26M4sLmWxUD7miG2Bx8qJCnlypbsws70ZXfct7CDS1LkmIHbnVIEVEwr94bcl3 c8N7jEwBMX0Z1hEp7eFsLYFOI5HDEbvh/0OgjhPAuMagqvdpoNW2bqlZ8ENGw1xhk0o7 rc9Q== 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=t2UJ3SIHapnxFaVjLnUf0CORoPSHZsRIqeBr5mz0wZA=; b=oKs/D+TULA0h5TQ7YwEonVcDiGSXGnEotLsRzW9DDXkCL9PmVgfB6F5e2QBkAgfDs2 rlSkcus4rJMjI3METUzzIG8yt+3B+JCjuLUYn1BXNL6OUyZSseXJ3kpULyWiAnBtiJiK 28R+q1cNgvS9b6JWfz4e7MuyK658kip08YAekVm5yOLgYn1lOUTroAswMVRW/ml7MoHC 03+s/sAsrOH7JAN49lnT3aVbrfZnYEs95N+l03hr+YGAx1MT2MUZyei/ta5ChcM8nlBI DsqgCs/QjytYLFocc5eLtUDNW8la5uASqjFURV0Ik98qSRkPUNSKMmz0mP4IlGdu+arK NzeQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Ja543dMn; 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=t2UJ3SIHapnxFaVjLnUf0CORoPSHZsRIqeBr5mz0wZA=; b=CqXFN286WE0x0bzZbRaiEXY7fuKNjNCrfVgA9MgSfjc4Yy1rhrZoW1xhOk1Kg6tIl3 +L2L16DCs+bpQJtLW9bTI1jDWtQgJRmP981KaDRH7suoKN7psDXwNrA+XdcfUYf6wDAi Jqq/83SH2tmiW3xEp/jZ/9cW48wGk4N99sx5g= 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=t2UJ3SIHapnxFaVjLnUf0CORoPSHZsRIqeBr5mz0wZA=; b=vSHAHt7WSCnHqvMKwnUFu8v5PVfhTtoG2DyPXpE5vaUcsHjGkRGI1NywGLvOEXYcHz fgWlVzdGMsH21VhmDnWMrL9p+4+avXVRYmySqegVJoEYPZ6NhKJsEHmrzAPhrx93LKkq Dh6knuO+6wjG/m6y/6C0UVyaWypOKKwHQs4tXr+03OlfIIyt2EUESHJxeUGVO6y4b8Te bKBIjA5vIT60kD+6d7IgPTC1yWwSqcdPwZjarUkK+DJBTPsNUs/3E44P58zwX/vXOa21 OP5p0ya4v1+b5VFuctc6LC1WCJor3zemdqm2zZuL5uujPrXjL2hx/vhR6XWZeMjPVhvv +bcg== X-Gm-Message-State: AOAM533LZ6qFJ2DGNji9tNKlDbMDKzwK/bmHXRbC59vjuBuR4ROziMxE hWXldJK6MYE6g/V1WmnlHxb6Us4n X-Google-Smtp-Source: ABdhPJwEW265FGIV4SlH5ALiHujUr6sNNyGErTLQTKRJJUYAjF5Eh5S+/nY7Jot61/UCqNovFk8SbA== X-Received: by 2002:a17:907:c302:b0:711:cb4b:1382 with SMTP id tl2-20020a170907c30200b00711cb4b1382mr4073565ejc.476.1655209722567; Tue, 14 Jun 2022 05:28:42 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:907:9705:b0:70d:ff46:7086 with SMTP id jg5-20020a170907970500b0070dff467086ls992104ejc.4.gmail; Tue, 14 Jun 2022 05:28:41 -0700 (PDT) X-Received: by 2002:a17:906:1084:b0:712:2989:b630 with SMTP id u4-20020a170906108400b007122989b630mr4065822eju.344.1655209721519; Tue, 14 Jun 2022 05:28:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655209721; cv=none; d=google.com; s=arc-20160816; b=SE2Hf5LTgzQEATPXMUZSgZ6NwzUyKNzLFcyCwatYN227dd6LH9N6uO098goiPUdXUJ veXlNBcdkyzAfnz2+UQQSscFbvKqFUMgnpVMRa3yefRX+WoqL2Rp266OK353/puCaAZY mAt/nI0JRztmtjXQYX32zjrA30+WvWQgOFolyXWYoE8177L+05XbS0MEaUlPNpdCLxOD X+nfzc7qypqVMIOk4rl3YUd6POJtVAPbmxiWVKRu2bcP6de2VZE+Mop/awuwftb4yeFj n6GqYy6+WTJKfsk6hyXFmndUYE4XFLPQ8CFksfUI8V+XhcY+wqUCv3gkeKZDx1T0r0XC hu+g== 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=UY9xGJVeCuDiPWb4quL1k0Um37OolG5UZs6VBw/8AE4=; b=jLhZt1swnODHaaW1tO62AxcOH1PbobHtwygUG6FRcyFKX6Cp0cCBQDfQ00/b88DwrY 2iUhyIJC8KFGTBbdOvUQuEky/gqfffqeZw4x4TgnHuCv7WnJLvhjw+he2Q9vaOV1Ssui NNoKFanenikoTGIbP9pZLSLAvhJWuijVDprenQbJcYVRYe/04yHQI3ucEC1ZggtOJLXJ nOeNW8PB3lQHibv9Aw3GKxihoE7PH4iSh/UbJph8Qv5b0nMmygKTGS4gA/ptBE9HiP3I ekzf+3QezVsqv+KhY1qhqNXSA4jNZ0VUCfEY4XvXU0sK7W46Q6H2mO3C0bbROfTCZWPL 2qeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=Ja543dMn; 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 k26-20020a1709063e1a00b0070afac782cesor3222727eji.5.2022.06.14.05.28.41 for (Google Transport Security); Tue, 14 Jun 2022 05:28:41 -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:a17:906:4482:b0:70a:19e3:d18a with SMTP id y2-20020a170906448200b0070a19e3d18amr3911625ejo.510.1655209721319; Tue, 14 Jun 2022 05:28:41 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jun 2022 05:28:40 -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 11/12] can: slcan: extend the protocol with error info Date: Tue, 14 Jun 2022 14:28:20 +0200 Message-Id: <20220614122821.3646071-12-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=Ja543dMn; 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 communication errors and forward them to the netdev upper levels. Signed-off-by: Dario Binacchi --- Changes in v4: - Add description of slc_bump_err() function. - Remove check for the 'e' character at the beggining of the function. It was already checked by the caller function. - Protect decoding against the case the len value is longer than the received data. - Some small changes to make the decoding more readable. - Increment all the error counters at the end of the function. Changes in v2: - Protect decoding against the case the len value is longer than the received data. - Continue error handling even if no skb can be allocated. drivers/net/can/slcan/slcan-core.c | 140 ++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/slcan/slcan-core.c b/drivers/net/can/slcan/slcan-core.c index b4f29ab2ab72..6c7c815eaf45 100644 --- a/drivers/net/can/slcan/slcan-core.c +++ b/drivers/net/can/slcan/slcan-core.c @@ -176,7 +176,7 @@ int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on) ************************************************************************/ /* Send one completely decapsulated can_frame to the network layer */ -static void slc_bump(struct slcan *sl) +static void slc_bump_frame(struct slcan *sl) { struct sk_buff *skb; struct can_frame *cf; @@ -255,6 +255,144 @@ static void slc_bump(struct slcan *sl) dev_kfree_skb(skb); } +/* An error frame can contain more than one type of error. + * + * Examples: + * + * e1a : len 1, errors: ACK error + * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error + */ +static void slc_bump_err(struct slcan *sl) +{ + struct net_device *dev = sl->dev; + struct sk_buff *skb; + struct can_frame *cf; + char *cmd = sl->rbuff; + bool rx_errors = false, tx_errors = false, rx_over_errors = false; + int i, len; + + /* get len from sanitized ASCII value */ + len = cmd[1]; + if (len >= '0' && len < '9') + len -= '0'; + else + return; + + if ((len + SLC_CMD_LEN + 1) > sl->rcount) + return; + + skb = alloc_can_err_skb(dev, &cf); + + if (skb) + cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; + + cmd += SLC_CMD_LEN + 1; + for (i = 0; i < len; i++, cmd++) { + switch (*cmd) { + case 'a': + netdev_dbg(dev, "ACK error\n"); + tx_errors = true; + if (skb) { + cf->can_id |= CAN_ERR_ACK; + cf->data[3] = CAN_ERR_PROT_LOC_ACK; + } + + break; + case 'b': + netdev_dbg(dev, "Bit0 error\n"); + tx_errors = true; + if (skb) + cf->data[2] |= CAN_ERR_PROT_BIT0; + + break; + case 'B': + netdev_dbg(dev, "Bit1 error\n"); + tx_errors = true; + if (skb) + cf->data[2] |= CAN_ERR_PROT_BIT1; + + break; + case 'c': + netdev_dbg(dev, "CRC error\n"); + rx_errors = true; + if (skb) { + cf->data[2] |= CAN_ERR_PROT_BIT; + cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; + } + + break; + case 'f': + netdev_dbg(dev, "Form Error\n"); + rx_errors = true; + if (skb) + cf->data[2] |= CAN_ERR_PROT_FORM; + + break; + case 'o': + netdev_dbg(dev, "Rx overrun error\n"); + rx_over_errors = true; + rx_errors = true; + if (skb) { + cf->can_id |= CAN_ERR_CRTL; + cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; + } + + break; + case 'O': + netdev_dbg(dev, "Tx overrun error\n"); + tx_errors = true; + if (skb) { + cf->can_id |= CAN_ERR_CRTL; + cf->data[1] = CAN_ERR_CRTL_TX_OVERFLOW; + } + + break; + case 's': + netdev_dbg(dev, "Stuff error\n"); + rx_errors = true; + if (skb) + cf->data[2] |= CAN_ERR_PROT_STUFF; + + break; + default: + if (skb) + dev_kfree_skb(skb); + + return; + } + } + + if (rx_errors) + dev->stats.rx_errors++; + + if (rx_over_errors) + dev->stats.rx_over_errors++; + + if (tx_errors) + dev->stats.tx_errors++; + + if (skb) + netif_rx(skb); +} + +static void slc_bump(struct slcan *sl) +{ + switch (sl->rbuff[0]) { + case 'r': + fallthrough; + case 't': + fallthrough; + case 'R': + fallthrough; + case 'T': + return slc_bump_frame(sl); + case 'e': + return slc_bump_err(sl); + default: + return; + } +} + /* parse tty input stream */ static void slcan_unesc(struct slcan *sl, unsigned char s) {