From patchwork Tue May 30 14:46:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 100914 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2236962vqr; Tue, 30 May 2023 07:49:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6D+lRoQ5I4Mdf019hLRifjRwSQETVXrvjKSmctnMXx6Rc8SSjNshDbAeFloOvFCZ5QzgQb X-Received: by 2002:a05:6a20:c896:b0:10b:d3e0:78a7 with SMTP id hb22-20020a056a20c89600b0010bd3e078a7mr2479728pzb.61.1685458190192; Tue, 30 May 2023 07:49:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685458190; cv=none; d=google.com; s=arc-20160816; b=fi2evdeo20q3L0amtHoavHwIcb/okDTbsmljQcmuX+gRgaWK3f7BsFmYV49CaHZJKK zOVOmrPt35SeUE6qKIiJ0afYbWg5+xgBn+EjItEjlQHhPq34kYXD50jZYiB9OJmeHJY2 eAXynFmNPLO2AxDw0afBu/sDalLZhx/DJCSbwJc7XuUK+G3ef1mYCQidn7wP1m0ZWhLF ISmiEN4DEzZrahWIJzLS+PD5NmxJTO55kr4sMF0QcuKoZOt0BSnNfzqEGjA5klfVcBJE IyV4Gi/VNyuwdK0cLfP0Ul55qqyQVlW7ujXsbwhHeUZTme3OHY/cTkWjS2hNsRaqIJRm DL0Q== 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:sender :dkim-signature; bh=mZz0jOLUEDQ4PhvWl6svx6nwTxioTgR9+aed2oIyP90=; b=CsW4pk06l+77y4Vglo3wodqFVtXfqiIMkMYGDtLylCmmnrRFmp6J9Odhd4vcuPCJZj WAikgx9bEes2FSiRHDiFR1ipE1kGsqtpDgZkf0EGgj8v1KzQ154yErEOR+wehas6ROnt C4rZoFLpShvRotwhZFJp6nW5HHRjWNCylxtnKQ9OPgLPudSNouyX70vx3jZ6MLcNbvTY puLNZl8sl1Sgt7S3du/HJgyBajwCaPakEKPNPuaf+NX8AAVxjeavptDXfqfoehkZ+XZy QZ+zds+JfKdfUbRLaT1OR7IXCWfQgua31o7SIyOElssO7giLJzvLALat8qlIm/XUyznH ZwfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="aHVoU/JQ"; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n7-20020a6543c7000000b0050f975790dcsi11286171pgp.464.2023.05.30.07.49.30; Tue, 30 May 2023 07:49:50 -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=@gmail.com header.s=20221208 header.b="aHVoU/JQ"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232262AbjE3Orc (ORCPT + 99 others); Tue, 30 May 2023 10:47:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229669AbjE3Ora (ORCPT ); Tue, 30 May 2023 10:47:30 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 850FAA7; Tue, 30 May 2023 07:47:29 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-64d4e45971bso3057666b3a.2; Tue, 30 May 2023 07:47:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685458049; x=1688050049; 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=mZz0jOLUEDQ4PhvWl6svx6nwTxioTgR9+aed2oIyP90=; b=aHVoU/JQBfEnUYOXxPRfcLvz9oAsk5mg4R8nEX+L+NrCEUVKbQK3pqbnjQqBi+GWa2 hOKRZMKDwyVdzwaY5tARCilXmEnNfjBxlQxMITVTdT6+Og5LByszj+cXe1rdZoqKi2GE FMx3KZbP7IQ9EsTVoLDxjbqgznKaabP0wrYqqMFI0nYTD/qouGj6QUEdw/t/70g9zjPz u66O+FYO3HYs64KQ260u3cD3wY0uo00FOpsoPBCQm5TZ+qWbqxprxr+uEpbmsD8qGQYV I5JvnHIGLVzAsWplnaf8XhWskXOFr+iL6D+4fiBIdE1wNgLE8dAAbaoFDubYXKb/lllI NicA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685458049; x=1688050049; 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=mZz0jOLUEDQ4PhvWl6svx6nwTxioTgR9+aed2oIyP90=; b=jnaUdQZSMOdRf6GcLPVWa/HWSCo1/+44dMDl3q2/SFLQHeLX7fR3+okZ7SyftD1nHt EUsRtXgn8LbhZA50RhmfDLVUseiVJ04IjaoWJwEOkREHb8o1bXoSVTx4O1HC4RKehXox y6l5KUPBF3NBjnbihcN86ZVgFLIdJDxNa+1Hpk4UHDV8JGpr1pMegxTtduwktkBTO5lP Pmf5WYYZvk5Gso+8Xh77Y0OdX0XpE6QXGWT5oZ1q7pzYSkNn4GGaWoiNnPYKnQyfraV5 h91r2GCn4weYhTJ/YU7KWTraywcsEwilVzjmBwTB4vTx9SfhrBiOxPVzYzoN+NCXsrrY rz3g== X-Gm-Message-State: AC+VfDxIZBOIlal7tMEi/fGfSwAMkZSTmxcr5cwo7QNCtZzIlMiWAdbw ar80+NvAC/QbiEzaCF8tnVE= X-Received: by 2002:a05:6a21:78b:b0:10d:b160:3d5c with SMTP id mg11-20020a056a21078b00b0010db1603d5cmr2663649pzb.12.1685458048944; Tue, 30 May 2023 07:47:28 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id e3-20020aa78c43000000b0063a04905379sm1703193pfd.137.2023.05.30.07.47.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 07:47:28 -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, Simon Horman , linux-kernel@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 1/3] can: length: fix bitstuffing count Date: Tue, 30 May 2023 23:46:35 +0900 Message-Id: <20230530144637.4746-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230530144637.4746-1-mailhol.vincent@wanadoo.fr> References: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> <20230530144637.4746-1-mailhol.vincent@wanadoo.fr> 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767331007215885248?= X-GMAIL-MSGID: =?utf-8?q?1767331007215885248?= 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 called 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 allows 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 30 14:46:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 100918 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2238932vqr; Tue, 30 May 2023 07:52:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6zeNyifw/gzRmiwoHTq9hJQOR8x0AkIO9WdYeQL0O+Kdmj2R5/gO8W7ABsRTsFRnx1ETyy X-Received: by 2002:a17:90b:30c3:b0:255:3ca1:d620 with SMTP id hi3-20020a17090b30c300b002553ca1d620mr2665259pjb.29.1685458379057; Tue, 30 May 2023 07:52:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685458378; cv=none; d=google.com; s=arc-20160816; b=RTwQd7mQcoOPSaBr/36ssxIYEb+7anlJBe24f7KOgfsK1nKEAZQ4z3wyIf1oqtBR09 nAx2Q5tZ6uvBn9SSaWoMnOrfjN3wmsCsiqHW3oykoX3gtyFTUsao8QskrwTLJ6tzZa3W MOX3xLDegAqzFoxrIysKCRkr6u3nBFRu3wBo5+DCzLr7X1LgMm3md+vel6ChXEWmyrVs 4mZeCu2dTmRMZS1kRso4HKM0IfaQqk3MqRuv5tFvNr8Immp4GJZioS3Umfjd3Dmqjrfe JhmWHOXgoXFmOcmVLl00C6eA+gyrNgxqMkf89eOm7xTpnxT4NWrt6z/8JQYABekp1sWm HBJA== 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:sender :dkim-signature; bh=G5x/T1gjjVFh7gahmvzfEftjIGWRBgFyhKjQXVhJh8w=; b=mNGP1IGKOKdaem2J3iVaGbFolnWv2QPfn65s/V46hha+O9sictBmSBkQMyjZb18+pv KYBgBdkxO1mV89QMOZokDvpBZkamMoKY6hKwqpmiOkxcNZbvpQ1Irc4shODuyOVgGEnZ UvXPqbzRU50CIz1fPxMGxvgew45ooKD0fQ1J2FuXd851JdCm43gCLSuTxx+yNN//uXfP ys4psPOtVDEd6FNcpy4EH3aJdM7jwYAAxa2NXwNFvVOudrB6pLMxoTOdYv/rYNwDE3rN 03KhMfT5ZvYWiG4tQXLsRj19BQnBUDWBrOq4roh4RW8kPIJi8wisutPYHF1U0Poxez9P c3Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="D/XiIQW8"; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k191-20020a6384c8000000b0053fb32e621bsi1609713pgd.896.2023.05.30.07.52.46; Tue, 30 May 2023 07:52:58 -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=@gmail.com header.s=20221208 header.b="D/XiIQW8"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232356AbjE3Orp (ORCPT + 99 others); Tue, 30 May 2023 10:47:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229669AbjE3Ork (ORCPT ); Tue, 30 May 2023 10:47:40 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C742103; Tue, 30 May 2023 07:47:38 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-53fbf2c42bfso441687a12.3; Tue, 30 May 2023 07:47:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685458057; x=1688050057; 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=G5x/T1gjjVFh7gahmvzfEftjIGWRBgFyhKjQXVhJh8w=; b=D/XiIQW8I92UEh/TzKXiijoKcF6jo4tapcHWGer5g1h1T2NT6u/vxDnz9OCNzFDUG8 UmImh6/GwYVIIMEK9W9qZjMifnrXpJwSd6WKX569cHLa5Hsv7+6voZkhVbIMTOqvKrxy 6dAPkh9YZYipePPx1+sqV0orXJ2XLvhfK4lPb/JaF6NM3D4tzwO1QGAzerpuZcfO4UBl 3lmf3U3sxo3vyvgOCJxWYMTalIrdm5kanNX6mhhN+ASjlxTAUOiUvAYZlJnhAXxBPD5j k7VgQwFvoInUlviL7rIT0/ml5wpPrwQSE5N6YOzLsoGJRljCqOZBBhVLWU4RlV3IS05R 5AGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685458057; x=1688050057; 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=G5x/T1gjjVFh7gahmvzfEftjIGWRBgFyhKjQXVhJh8w=; b=Lh+uOruqvVkd5NiZ8SeLFyOH5y1iqiLtgIVke9tjrUoTkfKUqKPGqKBM3EbsQLOIGN 7VucRQC/fvM3YB4oah8T5F/p9YhUFAWCPtf9VRVe/B1PIDEI5Cy+L7esweu0GB0C9FuS mjnM/lSFZ+gTlIJG7hxcWPFgun+vURg7twW5DeiGjhhFPM5/E31k2Dg7mcnmBLg/c+0L LTboDyhle72CkJn+z+RA7k4GwqF4S96Yj1fvJTPbVEmLiy7mmeDp/JGylRzEVcJgxXgY Tf4gIJsq4hbUbyTRfv94LUzge1WdsKJTuHERYV4vXuAK554BTygGyPatUrkxFdxLu8bW LeLw== X-Gm-Message-State: AC+VfDzxJZYnYlVBGw3+GoGmCRcbAVX5NLX+7V/WbhAQFK/3zTj3BAQ1 1SvXRSPqnTqf+UiGyPiAKKk= X-Received: by 2002:a05:6a20:7fa7:b0:105:8173:93a0 with SMTP id d39-20020a056a207fa700b00105817393a0mr3277898pzj.5.1685458057452; Tue, 30 May 2023 07:47:37 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id e3-20020aa78c43000000b0063a04905379sm1703193pfd.137.2023.05.30.07.47.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 07:47:37 -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, Simon Horman , linux-kernel@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 2/3] can: length: fix description of the RRS field Date: Tue, 30 May 2023 23:46:36 +0900 Message-Id: <20230530144637.4746-3-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230530144637.4746-1-mailhol.vincent@wanadoo.fr> References: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> <20230530144637.4746-1-mailhol.vincent@wanadoo.fr> 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767331204962819953?= X-GMAIL-MSGID: =?utf-8?q?1767331204962819953?= 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 unchanged, 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 30 14:46:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 100919 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2241036vqr; Tue, 30 May 2023 07:56:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5R2fZ2+OYWMUum4P1pTowT6Gh+ydCCQO0E1QX2eZqPufMeTQz6PjwFHssUqVfWBfe+bMj2 X-Received: by 2002:a17:90b:3e84:b0:256:4cd3:95c5 with SMTP id rj4-20020a17090b3e8400b002564cd395c5mr3171022pjb.5.1685458600637; Tue, 30 May 2023 07:56:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685458600; cv=none; d=google.com; s=arc-20160816; b=n1WXuqIi00iI5GD+aHv+9svVIEa9A3rJqZfg8KAMhJhiVHUgYEqs/kStpvxxK+fNv3 LdHGBWH6RcqQbYx8fMELBvNH3okjQa6TK641Tlf2e2DsjcyGyIpdin0RR39uLlc0PvNy BmRjSePoWrvDaVlEwOEPBEb4R07lZF1HZQ0C46s0Kp7QlBSFn+Q4OKUaHIfwW+7QyU++ pcO6ek7gu8ZNk2Zpk6jASRqx8py36f+qZeCZaxlPbV/3akGQ0SGpy93DJYNdVMTnmdPS /9HUiuk4xq2uYQTpVIwCO5WI0XAQYj1TRaZd0I0mctlrQcVSreVPXeMbBCsoi1wnwNBl yrFg== 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:sender :dkim-signature; bh=r9Vs5xR8nydXbwbFsmEP2aZ3ezlg/aDoPCpyXtJ3KIM=; b=ql99CSbwJPkFVA6e4aYtO9Bf4dxNf9pdbTihLF7L53Baf0xti955G2r9xA/bgd1UOE v8NRFRY02JxhanZj+uZXWZ2U2i2+AzzZU2KyyqeMEjKi7iYy8WvB+2vEP4+O/M2iUxk7 ZNKaB+pkVItty+QqwdGXFWG+AdHNETJpcWyXMlCbVIBdkFh34BUpfCMOvaFIsAalPVUQ GFcOIrIqQoVozd1aPKIRS4LLGkZGVTEfu0Uaqp26VHKzG8NpQDNRrWOjwY1mH12R7WJM 8JfhrdiNX4AUZWt80qKwcN8EyXBb2Um1yXEnhuSJAoT8mVLOSm8iwd05AggJ1c06Y4Ls p3qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="AX83e/k3"; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id em5-20020a17090b014500b002470f78a168si3780655pjb.104.2023.05.30.07.56.26; Tue, 30 May 2023 07:56:40 -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=@gmail.com header.s=20221208 header.b="AX83e/k3"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232604AbjE3Or6 (ORCPT + 99 others); Tue, 30 May 2023 10:47:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232573AbjE3Ors (ORCPT ); Tue, 30 May 2023 10:47:48 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2979DF7; Tue, 30 May 2023 07:47:44 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-64d2c865e4eso3418951b3a.0; Tue, 30 May 2023 07:47:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685458063; x=1688050063; 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=r9Vs5xR8nydXbwbFsmEP2aZ3ezlg/aDoPCpyXtJ3KIM=; b=AX83e/k3/uhHFrCO6w6wKFI3XUs3CVPwnoNw1fJ8aVjVd4DAPyH/sdCl6gu5WSVmzc 9b8uCMejW1MDMKQr7ZQ0A+TKhvlBEeAEJJd2UuApH/+jXaGNRjWwi48MpPNpzmI1RwF0 kVLDMZnJptKyOQv+O4qbd6qRlVfkB3A/rq0dDgW9K7IylqrwvNk/ARJw0htEs+BJZmSv n3iNF31mnRv6WatIgIzpgznCKwwlKt7IDnRk6Z7PEJgFZlwNJp5oht8ivTgd10FXBZOc 66FtoBKxc/V/vw62sYOuXGWTLx3F+FxwB5yPSxuObKSO47xsfkHKyxyJpT1cHN23lTbY dhxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685458063; x=1688050063; 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=r9Vs5xR8nydXbwbFsmEP2aZ3ezlg/aDoPCpyXtJ3KIM=; b=DrhH7QgS37yKxZiQnPgU8W5CVRwa6ezllK//7vDUhhHPGtvzTh3oV1gEOThoEgcH2j kBjfGaER3IeL3TPCV5wULuhgFRmiMghLdZSqdX9mqOfZ5b+aa4emc4rtO6pVJ4PXoAP4 4hRe3D77AMT9Ygnd7JYk2YO3m6LF48YXlui4Os/wXIl+XQHqCcf+RHv+r5BJOMqdayno CwdUoskthp2Wo0WYBiPqJbJ/veUVngTR2MKmKU+FTrhm60CGBxcBBjKMX+RSJgtSlyli f27FDbwkPqT231I4cg+f8IusyYYHBl+tzgK//TGG0mnOWnFhPttJCwitXroYyaC6erDa Pqxg== X-Gm-Message-State: AC+VfDxiquYDuK6ww7vBxLXhcCdjeQhbzeIKFVclA5BAzAg+/UQPCiy3 mjXnBMKkdd8uIhAgxB3LowmJxYvSao8= X-Received: by 2002:a05:6a00:148b:b0:64c:f4f9:de87 with SMTP id v11-20020a056a00148b00b0064cf4f9de87mr3097455pfu.18.1685458063296; Tue, 30 May 2023 07:47:43 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id e3-20020aa78c43000000b0063a04905379sm1703193pfd.137.2023.05.30.07.47.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 07:47:43 -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, Simon Horman , linux-kernel@vger.kernel.org, Vincent Mailhol Subject: [PATCH v3 3/3] can: length: refactor frame lengths definition to add size in bits Date: Tue, 30 May 2023 23:46:37 +0900 Message-Id: <20230530144637.4746-4-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230530144637.4746-1-mailhol.vincent@wanadoo.fr> References: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> <20230530144637.4746-1-mailhol.vincent@wanadoo.fr> 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767331437656017395?= X-GMAIL-MSGID: =?utf-8?q?1767331437656017395?= 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 individual macro definitions, declare the can_frame_bits() function-like macro. To this extent, do a full refactoring of the length definitions. In addition add the can_frame_bytes(). This function-like macro 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 maximum frame lengths (maximum data length, 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 as an alias to, respectively, can_frame_bytes(false, true, CAN_MAX_DLEN) and can_frame_bytes(true, true, CANFD_MAX_DLEN). 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. Signed-off-by: Vincent Mailhol --- drivers/net/can/dev/length.c | 15 +- include/linux/can/length.h | 298 +++++++++++++++++++++++++---------- 2 files changed, 213 insertions(+), 100 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..ef6e78fa95b9 100644 --- a/include/linux/can/length.h +++ b/include/linux/can/length.h @@ -1,132 +1,256 @@ /* 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 + * 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 + * 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 * - * rounded up and ignoring bitstuffing + * including all fields preceding the data field, ignoring bitstuffing */ -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) +#define CANFD_FRAME_HEADER_EFF_BITS 41 /* - * Size of a Classical CAN Extended Frame + * Size of a CAN CRC Field in bits * * 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 + * CRC sequence (CRC15) 15 + * CRC Delimiter 1 * - * rounded up and ignoring bitstuffing + * ignoring bitstuffing */ -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) +#define CAN_FRAME_CRC_FIELD_BITS 16 /* - * Size of a CAN-FD Standard Frame + * Size of a CAN-FD CRC17 Field in bits (length: 0..16) * * 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 - * - * assuming CRC21, rounded up and ignoring dynamic bitstuffing - */ -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(67, 8) + * 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 Extended Frame + * Size of a CAN-FD CRC21 Field in bits (length: 20..64) * * 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 - */ -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(86, 8) + * 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 + * --------------------------------------------------------- + * ACK slot 1 + * ACK delimiter 1 + * End Of Frame (EOF) 7 + * + * including all fields following the CRC field + */ +#define CAN_FRAME_FOOTER_BITS 9 + +/* + * First part of the Inter Frame Space + * (a.k.a. IMF - intermission field) + */ +#define CAN_INTERMISSION_BITS 3 + +/** + * can_bitstuffing_len() - Calculate the maximum length with bitsuffing + * @bitstream_len: length of a destuffed bit stream + * + * The worst bit stuffing case is a sequence in which dominant and + * recessive bits alternate every four bits: + * + * 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 + * + * Aside of the first bit, one stuff bit is added every four bits. + * + * Return: length of the stuffed bit stream in the worst case scenario. + */ +#define can_bitstuffing_len(destuffed_len) \ + (destuffed_len + (destuffed_len - 1) / 4) + +#define __can_bitstuffing_len(bitstuffing, destuffed_len) \ + (bitstuffing ? can_bitstuffing_len(destuffed_len) : \ + destuffed_len) + +#define __can_cc_frame_bits(is_eff, bitstuffing, \ + intermission, data_len) \ +( \ + __can_bitstuffing_len(bitstuffing, \ + (is_eff ? CAN_FRAME_HEADER_EFF_BITS : \ + CAN_FRAME_HEADER_SFF_BITS) + \ + data_len * BITS_PER_BYTE + \ + CAN_FRAME_CRC_FIELD_BITS) + \ + CAN_FRAME_FOOTER_BITS + \ + (intermission ? CAN_INTERMISSION_BITS : 0) \ +) + +#define __can_fd_frame_bits(is_eff, bitstuffing, \ + intermission, data_len) \ +( \ + __can_bitstuffing_len(bitstuffing, \ + (is_eff ? CANFD_FRAME_HEADER_EFF_BITS : \ + CANFD_FRAME_HEADER_SFF_BITS) + \ + data_len * BITS_PER_BYTE) + \ + (data_len <= 16 ? \ + CANFD_FRAME_CRC17_FIELD_BITS : \ + CANFD_FRAME_CRC21_FIELD_BITS) + \ + CAN_FRAME_FOOTER_BITS + \ + (intermission ? CAN_INTERMISSION_BITS : 0) \ +) + +/** + * can_frame_bits() - Calculate the number of bits in on the wire in a + * CAN frame + * @is_fd: true: CAN-FD frame; false: Classical CAN frame. + * @is_eff: true: Extended frame; false: Standard frame. + * @bitstuffing: true: calculate the bitsuffing worst case; false: + * calculate the bitsuffing best case (no dynamic + * bitsuffing). Fixed stuff bits are always 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. + */ +#define can_frame_bits(is_fd, is_eff, bitstuffing, \ + intermission, data_len) \ +( \ + is_fd ? __can_fd_frame_bits(is_eff, bitstuffing, \ + intermission, data_len) : \ + __can_cc_frame_bits(is_eff, bitstuffing, \ + intermission, data_len) \ +) + +/* + * Number of bytes in a CAN frame + * (rounded up, including intermission) + */ +#define can_frame_bytes(is_fd, is_eff, bitstuffing, data_len) \ + 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 \ + can_frame_bytes(false, true, false, CAN_MAX_DLEN) /* * 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 \ + can_frame_bytes(true, true, false, CANFD_MAX_DLEN) /* * can_cc_dlc2len(value) - convert a given data length code (dlc) of a