From patchwork Tue May 23 06:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13251568 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 545351C756 for ; Tue, 23 May 2023 06:52:57 +0000 (UTC) Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83286109; Mon, 22 May 2023 23:52:55 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1ae4c5e12edso52896005ad.3; Mon, 22 May 2023 23:52:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684824775; x=1687416775; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=v6WoMUPBPZ0xKom/xo0TfG/SnouT/YookA478MfBXb4=; b=F0wJm02uha2IhksGg8WhtsbsyRvqPaHuOQ05l9L17GEPb9unQNKjWdzengHn6z1kQ/ /IIQqe+zSusJD/cSFy/0IbFZ/hVJmx4fQ6FlkflnO3il6FJqhiQMg1WpxnefvgaEdXer J2XMaW7Pp2Gs+9fA+hyOl8759MGReIb2cPyh8zIffjtAQm+KQZUjqhL8GudbMcOAETTe 2ViT2Dserzqruo+Yw4Tksn7+iYs6vmYuCyarK/vOCQBUusZPxdGQa6Q3fh2XNBtuJUpn a95lA0icXIjkuMQ0Ai5CQayg5bx+LotQ6sZFBqfVZQOrIAdwQwxoSeqfCdISNF3tEpbZ Ldow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684824775; x=1687416775; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=v6WoMUPBPZ0xKom/xo0TfG/SnouT/YookA478MfBXb4=; b=ZkWNJ/5q7qv92dpC0gLO/3HdifXcRYQgQtC6cHsNPACSrUdxppmZ2g1ZwV5KAuXYvC kqRjiF0G2EGAqZotQ3yZVL6OwrvWxNd3FpDNMfxgehcxOFYT/idQfn78EqdBpPa15/Pe sch3xZBQwieSB2k+nO229uQJTmkfnArdKmAjMgw1mo4Ekt16QT+ZN/SrYK9Qy1QyDQYE xps7Dlj1OLXqap2Pb9N5r28XPLBmx0O28taaZHVrCcUm0xFdKh/NtTKXDvSGedZZx9RH UYQ93hwadYAhD384kxaEXGZZ4hotmobuaCN8wK7tkf7ln0Y431YT3yNLBR/rCc8mvqnk +bQQ== X-Gm-Message-State: AC+VfDwQ9iRFy7cuvmihv5tpT2k4wZiDHg8T+IuH8pxQ/YmIi6xHydbX grLqrnMDucV5ihAYcZK7g1mGm6HmRBc= X-Google-Smtp-Source: ACHHUZ6HV5t+LdWMZI7SSiC067iesOljqC4dUAif5TXzMvtC3V33m2GpmjS4cWyc1Yh1wAdce10Oew== X-Received: by 2002:a17:902:d2c2:b0:1ac:b4db:6a62 with SMTP id n2-20020a170902d2c200b001acb4db6a62mr13896815plc.65.1684824774460; Mon, 22 May 2023 23:52:54 -0700 (PDT) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id d10-20020a170902ceca00b001aafa2e212esm5963920plg.52.2023.05.22.23.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 23:52:54 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde , linux-can@vger.kernel.org, Thomas.Kopp@microchip.com Cc: Oliver Hartkopp , netdev@vger.kernel.org, marex@denx.de, Vincent Mailhol Subject: [PATCH v2 1/3] can: length: fix bitstuffing count Date: Tue, 23 May 2023 15:52:16 +0900 Message-Id: <20230523065218.51227-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523065218.51227-1-mailhol.vincent@wanadoo.fr> References: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> <20230523065218.51227-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net The Stuff Bit Count is always coded on 4 bits (ref [1]). Update the Stuff Bit Count size accordingly. In addition, the CRC fields of CAN FD Frames contain stuff bits at fixed positions call fixed stuff bits [2]. The CRC field starts with a fixed stuff bit and then has another fixed stuff bit after each fourth bit [2], which allow us to derive this formula: FSB count = 1 + round_down(len(CRC field)/4) The length of the CRC field is [1]: len(CRC field) = len(Stuff Bit Count) + len(CRC) = 4 + len(CRC) with len(CRC) either 17 or 21 bits depending of the payload length. In conclusion, for CRC17: FSB count = 1 + round_down((4 + 17)/4) = 6 and for CRC 21: FSB count = 1 + round_down((4 + 21)/4) = 7 Add a Fixed Stuff bits (FSB) field with above values and update CANFD_FRAME_OVERHEAD_SFF and CANFD_FRAME_OVERHEAD_EFF accordingly. [1] ISO 11898-1:2015 section 10.4.2.6 "CRC field": The CRC field shall contain the CRC sequence followed by a recessive CRC delimiter. For FD Frames, the CRC field shall also contain the stuff count. Stuff count If FD Frames, the stuff count shall be at the beginning of the CRC field. It shall consist of the stuff bit count modulo 8 in a 3-bit gray code followed by a parity bit [...] [2] ISO 11898-1:2015 paragraph 10.5 "Frame coding": In the CRC field of FD Frames, the stuff bits shall be inserted at fixed positions; they are called field stuff bits. There shall be a fixed stuff bit before the first bit of the stuff count, even if the last bits of the preceding field are a sequence of five consecutive bits of identical value, there shall be only the fixed stuff bit, there shall not be two consecutive stuff bits. A further fixed stuff bit shall be inserted after each fourth bit of the CRC field [...] Fixes: 85d99c3e2a13 ("can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer") Suggested-by: Thomas Kopp Signed-off-by: Vincent Mailhol --- include/linux/can/length.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/linux/can/length.h b/include/linux/can/length.h index 69336549d24f..b8c12c83bc51 100644 --- a/include/linux/can/length.h +++ b/include/linux/can/length.h @@ -72,17 +72,18 @@ * Error Status Indicator (ESI) 1 * Data length code (DLC) 4 * Data field 0...512 - * Stuff Bit Count (SBC) 0...16: 4 20...64:5 + * Stuff Bit Count (SBC) 4 * CRC 0...16: 17 20...64:21 * CRC delimiter (CD) 1 + * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 * ACK slot (AS) 1 * ACK delimiter (AD) 1 * End-of-frame (EOF) 7 * Inter frame spacing 3 * - * assuming CRC21, rounded up and ignoring bitstuffing + * assuming CRC21, rounded up and ignoring dynamic bitstuffing */ -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8) +#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8) /* * Size of a CAN-FD Extended Frame @@ -101,17 +102,18 @@ * Error Status Indicator (ESI) 1 * Data length code (DLC) 4 * Data field 0...512 - * Stuff Bit Count (SBC) 0...16: 4 20...64:5 + * Stuff Bit Count (SBC) 4 * CRC 0...16: 17 20...64:21 * CRC delimiter (CD) 1 + * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 * ACK slot (AS) 1 * ACK delimiter (AD) 1 * End-of-frame (EOF) 7 * Inter frame spacing 3 * - * assuming CRC21, rounded up and ignoring bitstuffing + * assuming CRC21, rounded up and ignoring dynamic bitstuffing */ -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8) +#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8) /* * Maximum size of a Classical CAN frame From patchwork Tue May 23 06:52:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13251569 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D72F61C756 for ; Tue, 23 May 2023 06:52:58 +0000 (UTC) Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97FD1119; Mon, 22 May 2023 23:52:57 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-64d57cd373fso1833828b3a.1; Mon, 22 May 2023 23:52:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684824777; x=1687416777; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=zpK9eA/3lj+tjk28hEidhkgrs72aVJBKWP37x7Y8nRU=; b=jxSF1xNrMXLa2fxm1HVUxgI+vnvNGjGG11M8uSAyHpciiyZxguwmmwPhAqXAIuX7Al AXQfvAgbkmHVEAFeY69CvN4nFCfMfBSVVaE8o7GQscOHPMZCzjApSWdQfuMQbMgF/qJS Tz5Cdrc5SW94sdDyhA4kFzIqI9pRQ1GP6g8RrPNMTAoNT0LmH9R+DE72NrSG6oB55itw UlXLmZbvB/x3WwJMG5AprzGcrl5rnCtv89xqTAirRoyxh4jy5NZMKtdnZbbIFBjfPc0t 1BYEY8ju8QuR6wfNOt1agDFKlRo653+KePKZ6eDtgpd+6+DVWqyr0H5/nB03nfWd6dnO /Twg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684824777; x=1687416777; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zpK9eA/3lj+tjk28hEidhkgrs72aVJBKWP37x7Y8nRU=; b=QzKjcIsbBm0WI14+y73Cz8QZExeDEx0X2DG2xHDrOJPqD0Pt5pgMM/MUd+V5FA8d5i BeGpTsfS82krUS4YLPWwMECuDoMobdGNp+qzqWjaHRA1QAFLsqed5o6dcyvdLbmYWEcS lHsbJxXFTzAJ42sIWHowNloHTBUjzQu6CUqcbXzXPjwcOk5wFLSvFmK2FpEuSYNqEM+N S/k2rpuYqOfh6EDpZp7dGhZNXuBPGO6LDmIzYfsX11rhQMN4Xuv/w/Dstz1iTScINlq7 GX1bAlR0C38M29Q+WLtsg3ohyHo7P2019qJY5su2q9IcfFBr2id0vNx9ztlmHjFY1/a1 5upQ== X-Gm-Message-State: AC+VfDzkrvnd1MBaHCNOBrPR6lO9LBN2GQVcvq5sZfj+eR0swalZlOKO JyX6t70V9RRDl/Plc9nu3lU= X-Google-Smtp-Source: ACHHUZ5OxZKCKXwKZUZsVbNCwhiHNClCKuTWkPqsjWpXlN1aK1d5YNXWHwIALY/HIiTRg9XVvYnMoQ== X-Received: by 2002:a17:902:ecc8:b0:1ab:1260:19de with SMTP id a8-20020a170902ecc800b001ab126019demr15771034plh.11.1684824776960; Mon, 22 May 2023 23:52:56 -0700 (PDT) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id d10-20020a170902ceca00b001aafa2e212esm5963920plg.52.2023.05.22.23.52.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 23:52:56 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde , linux-can@vger.kernel.org, Thomas.Kopp@microchip.com Cc: Oliver Hartkopp , netdev@vger.kernel.org, marex@denx.de, Vincent Mailhol Subject: [PATCH v2 2/3] can: length: fix description of the RRS field Date: Tue, 23 May 2023 15:52:17 +0900 Message-Id: <20230523065218.51227-3-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523065218.51227-1-mailhol.vincent@wanadoo.fr> References: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> <20230523065218.51227-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net The CAN-FD frames only have one reserved bit. The bit corresponding to Classical CAN frame's RTR bit is called the "Remote Request Substitution (RRS)" [1]. N.B. The RRS is not to be confused with the Substitute remote request (SRR). Fix the description in the CANFD_FRAME_OVERHEAD_SFF/EFF. The total remains unchange, so this is just a documentation fix. In addition to the above add myself as copyright owner for 2020 (as coauthor of the initial version, c.f. Fixes tag). [1] ISO 11898-1:2015 paragraph 10.4.2.3 "Arbitration field": RSS bit [only in FD Frames] The RRS bit shall be transmitted in fD Frames at the position of the RTR bit in Classical Frames. The RRS bit shall be transmitted dominant, but receivers shall accept recessive and dominant RRS bits. Fixes: 85d99c3e2a13 ("can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer") Suggested-by: Thomas Kopp Signed-off-by: Vincent Mailhol --- include/linux/can/length.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/linux/can/length.h b/include/linux/can/length.h index b8c12c83bc51..521fdbce2d69 100644 --- a/include/linux/can/length.h +++ b/include/linux/can/length.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* Copyright (C) 2020 Oliver Hartkopp * Copyright (C) 2020 Marc Kleine-Budde + * Copyright (C) 2020 Vincent Mailhol */ #ifndef _CAN_LENGTH_H @@ -64,7 +65,7 @@ * --------------------------------------------------------- * Start-of-frame 1 * Identifier 11 - * Reserved bit (r1) 1 + * Remote Request Substitution (RRS) 1 * Identifier extension bit (IDE) 1 * Flexible data rate format (FDF) 1 * Reserved bit (r0) 1 @@ -95,7 +96,7 @@ * Substitute remote request (SRR) 1 * Identifier extension bit (IDE) 1 * Identifier B 18 - * Reserved bit (r1) 1 + * Remote Request Substitution (RRS) 1 * Flexible data rate format (FDF) 1 * Reserved bit (r0) 1 * Bit Rate Switch (BRS) 1 From patchwork Tue May 23 06:52:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13251570 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26F891C756 for ; Tue, 23 May 2023 06:53:02 +0000 (UTC) Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1CF1118; Mon, 22 May 2023 23:52:59 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1ae79528d4dso46303095ad.2; Mon, 22 May 2023 23:52:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684824779; x=1687416779; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=pHVJOrZTLhWxfI/dA1jc/7StnNnfOvKPoL5oGZLJdis=; b=mCssCrHbRZzxaj8bWmxvVy63hBj2eLFh5dSTr4sfFOMScdQYPJ4mpxJUP9Hu5mxx12 FCjR7liO0CSm2do/4NiubfpRliyKtLT3v15PzXqRmGeze0VRti85inMdTVpxwxeN2OpI p94QNGuigm3z62c59zixUlpXCzmvl3INRJaT0RDvyXF1BTBABuN5GuKNkXQgxOiwp6zS GgETo9PQvH4CB72r6TW/tWbf6RUnC1zeqxDHMFaEtXHjGV7NX8xQCT1DJBnVFzqSxp74 pwbGL03u+GADOja3nTV/vjd5hcaKLVFCOnux/KDPH2a5EpAyXbY9MgEuRl3Xwy4oUZeT xj+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684824779; x=1687416779; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=pHVJOrZTLhWxfI/dA1jc/7StnNnfOvKPoL5oGZLJdis=; b=L2zhi7I3nDRW4ORPQDw7FVvQ7eB+KUkuhN7QboZLz6CO+xjqs5z0jhDdoNTCT1Xsjm HZ2k3ZxurAbSRKQRwNSpeG3aYvddWpnkEEPi0oRyyO2aJFBxRVEYOIlXsEXTPTk9mr46 tBWNtKICUTXu61hi8lb8ksNlmXVfWHIIXh7LY2x1xKZSEN7L+dvlxNAKAFlz279l1Wyj 7ns158LV5RDO4/5Yy1RSpGrXtHjOyCiJPjjgKzvZSiu8+yJwbA0cmf7lgQZCw7GaodYZ i5lfu6y+8gm8yt5acxIL08gVNdE1eyS1lCf+hlQVJ71bwSjs04+DyMyYxW5rGgcMmyHu 9S9g== X-Gm-Message-State: AC+VfDyjaRy0GwqXHdDnTWyj58RmCC2zDMHl6jCNHSiN//gxnz06eb+6 JZyjuF5o9QJ0c5SypNYbsLUU7LeVnZ4= X-Google-Smtp-Source: ACHHUZ56VO7pRIv3NwujIPlTt9j7JaggLiXDEl86vjoqZ8eleL2NRPo30ZftRRxYForZv7Uaf0/DWw== X-Received: by 2002:a17:902:d04a:b0:1ac:5b6b:df4c with SMTP id l10-20020a170902d04a00b001ac5b6bdf4cmr10768105pll.69.1684824779002; Mon, 22 May 2023 23:52:59 -0700 (PDT) Received: from XH22050090-L.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.gmail.com with ESMTPSA id d10-20020a170902ceca00b001aafa2e212esm5963920plg.52.2023.05.22.23.52.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 23:52:58 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Marc Kleine-Budde , linux-can@vger.kernel.org, Thomas.Kopp@microchip.com Cc: Oliver Hartkopp , netdev@vger.kernel.org, marex@denx.de, Vincent Mailhol Subject: [PATCH v2 3/3] can: length: refactor frame lengths definition to add size in bits Date: Tue, 23 May 2023 15:52:18 +0900 Message-Id: <20230523065218.51227-4-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523065218.51227-1-mailhol.vincent@wanadoo.fr> References: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> <20230523065218.51227-1-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org Introduce a method to calculate the exact size in bits of a CAN(-FD) frame with or without dynamic bitsuffing. These are all the possible combinations taken into account: - Classical CAN or CAN-FD - Standard or Extended frame format - CAN-FD CRC17 or CRC21 - Include or not intermission Instead of doing several macro definitions, declare the can_frame_bits() static inline function. To this extend, do a full refactoring of the length definitions. If given integer constant expressions as argument, can_frame_bits() folds into a compile time constant expression, giving no penalty over the use of macros. Also add the can_frame_bytes(). This function replaces the existing macro: - CAN_FRAME_OVERHEAD_SFF: can_frame_bytes(false, false, 0) - CAN_FRAME_OVERHEAD_EFF: can_frame_bytes(false, true, 0) - CANFD_FRAME_OVERHEAD_SFF: can_frame_bytes(true, false, 0) - CANFD_FRAME_OVERHEAD_EFF: can_frame_bytes(true, true, 0) The different frame lengths (including intermission) are as follow: Frame type bits bytes ---------------------------------------------------------- Classic CAN SFF no-bitstuffing 111 14 Classic CAN EFF no-bitstuffing 131 17 Classic CAN SFF bitstuffing 135 17 Classic CAN EFF bitstuffing 160 20 CAN-FD SFF no-bitstuffing 579 73 CAN-FD EFF no-bitstuffing 598 75 CAN-FD SFF bitstuffing 712 89 CAN-FD EFF bitstuffing 736 92 The macro CAN_FRAME_LEN_MAX and CANFD_FRAME_LEN_MAX are kept to be used in const struct declarations (folding of can_frame_bytes() occurs too late in the compilation to be used in struct declarations). In addition to the above: - Use ISO 11898-1:2015 definitions for the name of the CAN frame fields. - Include linux/bits.h for use of BITS_PER_BYTE. - Include linux/math.h for use of mult_frac() and DIV_ROUND_UP(). N.B: the use of DIV_ROUND_UP() is not new to this patch, but the include was previously omitted. - Add copyright 2023 for myself. [1] commit 85d99c3e2a13 ("can: length: can_skb_get_frame_len(): introduce function to get data length of frame in data link layer") Link: https://git.kernel.org/torvalds/c/85d99c3e2a13 [2] RE: [PATCH] can: mcp251xfd: Increase poll timeout Link: https://lore.kernel.org/linux-can/BL3PR11MB64846C83ACD04E9330B0FE66FB729@BL3PR11MB6484.namprd11.prod.outlook.com/ Signed-off-by: Vincent Mailhol --- In case you ask, here is the proof that can_frame_bits() folds into a compile time constant expression. This file: #include unsigned int canfd_max_len_bitsuffing(void) { return can_frame_bytes(true, true, true, CANFD_MAX_DLEN); } Produces this assembly code: 0000000000000010 : 10: f3 0f 1e fa endbr64 14: b8 5c 00 00 00 mov $0x5c,%eax 19: e9 00 00 00 00 jmpq 1e where 0x5c corresponds to the expected value of 92 bytes. --- drivers/net/can/dev/length.c | 15 +- include/linux/can/length.h | 326 +++++++++++++++++++++++++---------- 2 files changed, 238 insertions(+), 103 deletions(-) diff --git a/drivers/net/can/dev/length.c b/drivers/net/can/dev/length.c index b48140b1102e..b7f4d76dd444 100644 --- a/drivers/net/can/dev/length.c +++ b/drivers/net/can/dev/length.c @@ -78,18 +78,7 @@ unsigned int can_skb_get_frame_len(const struct sk_buff *skb) else len = cf->len; - if (can_is_canfd_skb(skb)) { - if (cf->can_id & CAN_EFF_FLAG) - len += CANFD_FRAME_OVERHEAD_EFF; - else - len += CANFD_FRAME_OVERHEAD_SFF; - } else { - if (cf->can_id & CAN_EFF_FLAG) - len += CAN_FRAME_OVERHEAD_EFF; - else - len += CAN_FRAME_OVERHEAD_SFF; - } - - return len; + return can_frame_bytes(can_is_canfd_skb(skb), cf->can_id & CAN_EFF_FLAG, + false, len); } EXPORT_SYMBOL_GPL(can_skb_get_frame_len); diff --git a/include/linux/can/length.h b/include/linux/can/length.h index 521fdbce2d69..7518117ee5fc 100644 --- a/include/linux/can/length.h +++ b/include/linux/can/length.h @@ -1,132 +1,278 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* Copyright (C) 2020 Oliver Hartkopp * Copyright (C) 2020 Marc Kleine-Budde - * Copyright (C) 2020 Vincent Mailhol + * Copyright (C) 2020, 2023 Vincent Mailhol */ #ifndef _CAN_LENGTH_H #define _CAN_LENGTH_H +#include #include #include +#include /* - * Size of a Classical CAN Standard Frame + * Size of a Classical CAN Standard Frame header in bits + * + * Name of Field Bits + * --------------------------------------------------------- + * Start Of Frame (SOF) 1 + * Arbitration field: + * base ID 11 + * Remote Transmission Request (RTR) 1 + * Control field: + * IDentifier Extension bit (IDE) 1 + * FD Format indicatior (FDF) 1 + * Data Length Code (DLC) 4 + * + * including all fields preceding the data field, ignoring bitstuffing + */ +#define CAN_FRAME_HEADER_SFF_BITS 19 + +/* + * Size of a Classical CAN Extended Frame header in bits + * + * Name of Field Bits + * --------------------------------------------------------- + * Start Of Frame (SOF) 1 + * Arbitration field: + * base ID 11 + * Substitute Remote Request (SRR) 1 + * IDentifier Extension bit (IDE) 1 + * ID extension 18 + * Remote Transmission Request (RTR) 1 + * Control field: + * FD Format indicatior (FDF) 1 + * Reserved bit (r0) 1 + * Data length code (DLC) 4 + * + * including all fields preceding the data field, ignoring bitstuffing + */ +#define CAN_FRAME_HEADER_EFF_BITS 39 + +/* + * Size of a CAN-FD Standard Frame in bits + * + * Name of Field Bits + * --------------------------------------------------------- + * Start Of Frame (SOF) 1 + * Arbitration field: + * base ID 11 + * Remote Request Substitution (RRS) 1 + * Control field: + * IDentifier Extension bit (IDE) 1 + * FD Format indicator (FDF) 1 + * Reserved bit (res) 1 + * Bit Rate Switch (BRS) 1 + * Error Status Indicator (ESI) 1 + * Data length code (DLC) 4 + * + * including all fields preceding the data field, ignoring bitstuffing + */ +#define CANFD_FRAME_HEADER_SFF_BITS 22 + +/* + * Size of a CAN-FD Extended Frame in bits + * + * Name of Field Bits + * --------------------------------------------------------- + * Start Of Frame (SOF) 1 + * Arbitration field: + * base ID 11 + * Substitute Remote Request (SRR) 1 + * IDentifier Extension bit (IDE) 1 + * ID extension 18 + * Remote Request Substitution (RRS) 1 + * Control field: + * FD Format indicator (FDF) 1 + * Reserved bit (res) 1 + * Bit Rate Switch (BRS) 1 + * Error Status Indicator (ESI) 1 + * Data length code (DLC) 4 + * + * including all fields preceding the data field, ignoring bitstuffing + */ +#define CANFD_FRAME_HEADER_EFF_BITS 41 + +/* + * Size of a CAN CRC Field in bits + * + * Name of Field Bits + * --------------------------------------------------------- + * CRC sequence (CRC15) 15 + * CRC Delimiter 1 + * + * ignoring bitstuffing + */ +#define CAN_FRAME_CRC_FIELD_BITS 16 + +/* + * Size of a CAN-FD CRC17 Field in bits (length: 0..16) + * + * Name of Field Bits + * --------------------------------------------------------- + * Stuff Count 4 + * CRC Sequence (CRC17) 17 + * CRC Delimiter 1 + * Fixed stuff bits 6 + */ +#define CANFD_FRAME_CRC17_FIELD_BITS 28 + +/* + * Size of a CAN-FD CRC21 Field in bits (length: 20..64) + * + * Name of Field Bits + * --------------------------------------------------------- + * Stuff Count 4 + * CRC sequence (CRC21) 21 + * CRC Delimiter 1 + * Fixed stuff bits 7 + */ +#define CANFD_FRAME_CRC21_FIELD_BITS 33 + +/* + * Size of a CAN(-FD) Frame footer in bits * * Name of Field Bits * --------------------------------------------------------- - * Start-of-frame 1 - * Identifier 11 - * Remote transmission request (RTR) 1 - * Identifier extension bit (IDE) 1 - * Reserved bit (r0) 1 - * Data length code (DLC) 4 - * Data field 0...64 - * CRC 15 - * CRC delimiter 1 * ACK slot 1 * ACK delimiter 1 - * End-of-frame (EOF) 7 - * Inter frame spacing 3 + * End Of Frame (EOF) 7 * - * rounded up and ignoring bitstuffing + * including all fields following the CRC field */ -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) +#define CAN_FRAME_FOOTER_BITS 9 /* - * Size of a Classical CAN Extended Frame - * - * Name of Field Bits - * --------------------------------------------------------- - * Start-of-frame 1 - * Identifier A 11 - * Substitute remote request (SRR) 1 - * Identifier extension bit (IDE) 1 - * Identifier B 18 - * Remote transmission request (RTR) 1 - * Reserved bits (r1, r0) 2 - * Data length code (DLC) 4 - * Data field 0...64 - * CRC 15 - * CRC delimiter 1 - * ACK slot 1 - * ACK delimiter 1 - * End-of-frame (EOF) 7 - * Inter frame spacing 3 - * - * rounded up and ignoring bitstuffing + * First part of the Inter Frame Space + * (a.k.a. IMF - intermission field) */ -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) +#define CAN_INTERMISSION_BITS 3 /* - * Size of a CAN-FD Standard Frame + * CAN bit stuffing overhead multiplication factor * - * Name of Field Bits - * --------------------------------------------------------- - * Start-of-frame 1 - * Identifier 11 - * Remote Request Substitution (RRS) 1 - * Identifier extension bit (IDE) 1 - * Flexible data rate format (FDF) 1 - * Reserved bit (r0) 1 - * Bit Rate Switch (BRS) 1 - * Error Status Indicator (ESI) 1 - * Data length code (DLC) 4 - * Data field 0...512 - * Stuff Bit Count (SBC) 4 - * CRC 0...16: 17 20...64:21 - * CRC delimiter (CD) 1 - * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 - * ACK slot (AS) 1 - * ACK delimiter (AD) 1 - * End-of-frame (EOF) 7 - * Inter frame spacing 3 + * The worst bit stuffing case is a sequence in which dominant and + * recessive bits alternate every four bits: * - * assuming CRC21, rounded up and ignoring dynamic bitstuffing + * Destuffed: 1 1111 0000 1111 0000 1111 + * Stuffed: 1 1111o 0000i 1111o 0000i 1111o + * + * Nomenclature: + * + * - "0": dominant bit + * - "o": dominant stuff bit + * - "1": recessive bit + * - "i": recessive stuff bit + * + * Ignoring the first bit, one stuff bit is added every four bits + * giving us an overhead of 1/4 = 0.25. */ -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8) +#define can_bit_stuffing_overhead(stream_len) mult_frac(stream_len, 1, 4) + +/** + * can_frame_bits() - Calculate the number of bits in on the wire in a + * CAN frame + * @is_eff: true: Extended Frame; false: Standard Frame. + * @bitstuffing: if true, calculate the bitsuffing worst case, if + * false, calculated the bitsuffing best case (no dynamic + * bitsuffing). Fixed stuff bits always get included. + * @intermission: if and only if true, include the inter frame space + * assuming no bus idle (i.e. only the intermission gets added). + * @data_len: length of the data field in bytes. Correspond to + * can(fd)_frame->len. Should be zero for remote frames. No + * sanitization is done on @data_len. + * + * Return: the numbers of bits on the wire of a CAN frame. + */ +static inline +unsigned int can_frame_bits(bool is_fd, bool is_eff, + bool bitstuffing, bool intermission, + unsigned int data_len) +{ + unsigned int bits; + + if (is_fd) { + if (is_eff) + bits = CANFD_FRAME_HEADER_EFF_BITS; + else + bits = CANFD_FRAME_HEADER_SFF_BITS; + } else { + if (is_eff) + bits = CAN_FRAME_HEADER_EFF_BITS; + else + bits = CAN_FRAME_HEADER_SFF_BITS; + } + + bits += data_len * BITS_PER_BYTE; + + if (!is_fd) { + bits += CAN_FRAME_CRC_FIELD_BITS; + /* + * In Classical CAN, bit stuffing applies to all field + * from SOF to CRC delimiter + */ + if (bitstuffing) + bits += can_bit_stuffing_overhead(bits); + } else { + /* + * In CAN-FD, the fields preceding the CRC field have + * dynamic bit stuffing; but the CRC field has fixed + * bitstuffing + */ + if (bitstuffing) + bits += can_bit_stuffing_overhead(bits - 1); + if (data_len <= 16) + bits += CANFD_FRAME_CRC17_FIELD_BITS; + else + bits += CANFD_FRAME_CRC21_FIELD_BITS; + } + + bits += CAN_FRAME_FOOTER_BITS; + + if (intermission) + bits += CAN_INTERMISSION_BITS; + + return bits; +} /* - * Size of a CAN-FD Extended Frame - * - * Name of Field Bits - * --------------------------------------------------------- - * Start-of-frame 1 - * Identifier A 11 - * Substitute remote request (SRR) 1 - * Identifier extension bit (IDE) 1 - * Identifier B 18 - * Remote Request Substitution (RRS) 1 - * Flexible data rate format (FDF) 1 - * Reserved bit (r0) 1 - * Bit Rate Switch (BRS) 1 - * Error Status Indicator (ESI) 1 - * Data length code (DLC) 4 - * Data field 0...512 - * Stuff Bit Count (SBC) 4 - * CRC 0...16: 17 20...64:21 - * CRC delimiter (CD) 1 - * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 - * ACK slot (AS) 1 - * ACK delimiter (AD) 1 - * End-of-frame (EOF) 7 - * Inter frame spacing 3 - * - * assuming CRC21, rounded up and ignoring dynamic bitstuffing + * Number of bytes in a CAN frame + * (rounded up, including intermission) */ -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8) +static inline +unsigned int can_frame_bytes(bool is_fd, bool is_eff, bool bitstuffing, + unsigned int data_len) +{ + return DIV_ROUND_UP(can_frame_bits(is_fd, is_eff, bitstuffing, true, + data_len), + BITS_PER_BYTE); +} /* * Maximum size of a Classical CAN frame - * (rounded up and ignoring bitstuffing) + * (rounded up, ignoring bitstuffing but including intermission) */ -#define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + CAN_MAX_DLEN) +#define CAN_FRAME_LEN_MAX \ + DIV_ROUND_UP(CAN_FRAME_HEADER_EFF_BITS + \ + CAN_MAX_DLEN * BITS_PER_BYTE + \ + CAN_FRAME_CRC_FIELD_BITS + \ + CAN_FRAME_FOOTER_BITS + \ + CAN_INTERMISSION_BITS, \ + BITS_PER_BYTE) /* * Maximum size of a CAN-FD frame * (rounded up and ignoring bitstuffing) */ -#define CANFD_FRAME_LEN_MAX (CANFD_FRAME_OVERHEAD_EFF + CANFD_MAX_DLEN) +#define CANFD_FRAME_LEN_MAX \ + DIV_ROUND_UP(CANFD_FRAME_HEADER_EFF_BITS + \ + CANFD_MAX_DLEN * BITS_PER_BYTE + \ + CANFD_FRAME_CRC21_FIELD_BITS \ + CAN_FRAME_FOOTER_BITS + \ + CAN_INTERMISSION_BITS, \ + BITS_PER_BYTE) /* * can_cc_dlc2len(value) - convert a given data length code (dlc) of a