Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp4150240iog; Tue, 28 Jun 2022 10:01:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sJIn6raB97s36+W5MICibcG1IfJDPEZOANFudR79uu1nBJ9HORHQHh5SGr+0XGny50lPCu X-Received: by 2002:a05:6a00:1a0c:b0:523:1a23:957f with SMTP id g12-20020a056a001a0c00b005231a23957fmr5717076pfv.45.1656435671473; Tue, 28 Jun 2022 10:01:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656435671; cv=none; d=google.com; s=arc-20160816; b=JD0ROBZcW/vZ9L916Ghkz/IzrUG0b8tfS5r2PaOc+Ue8qRvF/dODwnwUZ9jHAelWxJ p9ATmeBLHGuompk72IgSJId4I9RN0AbbQ/pd0TfFWzF1VWeXHEkLATZTUJZ0BtjJiFbO gYEXEQceWUrJhCKTKSN70AcdWB764Y9mhMOCNP9tybhKYwiDx+kgWYUD5avW/cSpcLaS YRK4iaQmJShziDcEyGf01xp+LXAnLUZem5wHzy0jd8Ncgi3zvgveJWvjKGZTxkdoCrPq SOILDiDB5q13BczGzStcH261+nPCPOhbkxe4IWXrXsxvfhXjCsAUOTvzhu9ivbFvK4f2 YY5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dlTOY2xvVGa6BOY+C/5W0PL43wDapCN5T2ZQIpzidGs=; b=Tqy8YZYK+j9LwQe5eVsvVX71oLACyPkof2XHRlOpPiRR2Zdk/C53GYXdg57fzZp/ai jLk6vr05cXjDfJfD7Ii6hvb5t/gCGGQX9uCIHAcZlfLIC67M9LqrzdJVqn/V3TbIW8JC SVOYA6XT1zawMtY3MGVF26G8nyjysPp1eCDN4rHZqSjhANif2Q81hUEB5PJcdIOcFQ2g HPNFkNpB4IrUgG7tu9eYr80vzZqdVeY7goIjnH8R6KewHDIc9hw6npNuJaGzSwn1BXZZ hgoFcMs/cgoSs8r1GBVGjRIA/aGTjJ/NeBmlqo7tWPHuaTFEC7HQZ9rRB3qQoKHBD8bu Coeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=FFGuQTgN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e2-20020a637442000000b0040cf5d7ec89si18201536pgn.680.2022.06.28.10.00.54; Tue, 28 Jun 2022 10:01:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=FFGuQTgN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234028AbiF1QhQ (ORCPT + 99 others); Tue, 28 Jun 2022 12:37:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231927AbiF1Qe7 (ORCPT ); Tue, 28 Jun 2022 12:34:59 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99FD2B94 for ; Tue, 28 Jun 2022 09:32:05 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id mf9so27027782ejb.0 for ; Tue, 28 Jun 2022 09:32:05 -0700 (PDT) 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:content-transfer-encoding; bh=dlTOY2xvVGa6BOY+C/5W0PL43wDapCN5T2ZQIpzidGs=; b=FFGuQTgNFPUOJy7W7LyC6yuBr+1gnvvgi5oICNleFC9tYjsV7E65ZItYDQYG7QxPMu MYIGFHznp0t0+2QDquc6gaj6dHhvwNNv+YdX7n2h4XyOO0mGYF8/Tc+AeoWBnrTskmRA IKRfOaJ1/XK68brtxVVQKiW43xvrFYdzhNbLo= 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:content-transfer-encoding; bh=dlTOY2xvVGa6BOY+C/5W0PL43wDapCN5T2ZQIpzidGs=; b=10A4aT2evpwUclaMixaQ4j6qT5wEeK2a2i902wbc4xUCxkliV4I9Ceot5Xhi5dx59z x3VaFt969ScqQwa1FGpJRS1B7fAit7qP+pOhvpm65Da4bDaftTEkqhbM+QB0x2OKsdo7 MeZfKgwYSaOzb4zJBCEsmNiMY2GL7OTxg+w0jVC3mYdkKpRIMlGF03+G4m4kXjnVT0RZ YEDsunJjUCl5awWvZQdZQrDycE86LLayGLZYcvUgPC/ZvhHvvC5iI+0eKE24tJey1jzH tFfmHWdHHrA0ZQS2Lob4ohCzhGHLw6FB+8ffenNQujuHxlPVfNkNHk6HlnJCrSHbZDWZ kyng== X-Gm-Message-State: AJIora/vRnMkUOd90JYc4LJZ0275RWwkKo9ofTahxoyF5yuK/paDmAMH oq4vdyGeUts3bJAqv7LvNXL2efo1E7qFvQ== X-Received: by 2002:a17:907:94d4:b0:722:e4b8:c2f2 with SMTP id dn20-20020a17090794d400b00722e4b8c2f2mr18565878ejc.527.1656433924935; Tue, 28 Jun 2022 09:32:04 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id b20-20020a0564021f1400b0042e15364d14sm9916952edb.8.2022.06.28.09.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jun 2022 09:32:04 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: michael@amarulasolutions.com, Amarula patchwork , Oliver Hartkopp , 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 v5 11/12] can: slcan: extend the protocol with error info Date: Tue, 28 Jun 2022 18:31:35 +0200 Message-Id: <20220628163137.413025-12-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220628163137.413025-1-dario.binacchi@amarulasolutions.com> References: <20220628163137.413025-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It extends the protocol to receive the adapter CAN communication errors and forward them to the netdev upper levels. Signed-off-by: Dario Binacchi --- (no changes since v4) 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 c1fd1e934d93..4269b2267be2 100644 --- a/drivers/net/can/slcan/slcan-core.c +++ b/drivers/net/can/slcan/slcan-core.c @@ -175,7 +175,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; @@ -254,6 +254,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) { -- 2.32.0