Message ID | 20230507155506.3179711-1-mailhol.vincent@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1648479vqo; Sun, 7 May 2023 09:09:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5INh3oHATQTrbTQnUFGqbad3RIMBHR5jghw2s1UOG2ARaJScHVLh3Yn7Z4baNLROhLdJyV X-Received: by 2002:a17:903:192:b0:1ab:1260:19de with SMTP id z18-20020a170903019200b001ab126019demr10208148plg.11.1683475792315; Sun, 07 May 2023 09:09:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683475792; cv=none; d=google.com; s=arc-20160816; b=JHlpb8vYQ6S1hXtMKxd6awVOsLpH1bg56j6gxPK46hoqKW0HmkGiUJ+VvUVJQ39PT5 IayuDsV4xsr/y9ztbpfVBHvQNYym9Quay2wOwgtOJCRy2sV9vhVRUwBJxQt5R9SEBijq rQOEvFdAXrQM7SP63adbYnHDWBpAj2cSAHxA5GC94W+d20dOqndpsNtRoJUnjkwiqwqm PpV4o7izUq49XI/oYG2dfDlqkkJdK9WwAMbnXTixEr3EBRJUs8NGlRWt1aD3nOrwOBNr OOYkNgLwoQ4kwg9IqwM+Vk0rgtsi7fm+BetOZHwyxgtrg5SNtqDtjrVf0jtAvvq4PQ/A L+bg== 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 :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=GYb3dk460XgtsVCvhsWMRMsftl/DXZ2mh7rVvkm2EDw=; b=wnle/UfSe6KMQ9AA+TmZIsHqBRrxvX6C2azOCWSwV+4RkoitfAADVaXq5nUooQ3Qz8 2KEkj86xXhVS8ueCmINTVdD4JPsD9YW6dYfz99N9mqlfTTB4W7xIlo69dJCuAGwORDJI NtxDk8oFENx4sAj+hl+JNo9funeJ7kTHzs3aOsuCSBBb6SYsSexvrjBndO5x8bYAd9uj 7zRDrMNH2WQcEf9rNqKjoTJkjq2qn8xbKLNn8U8p2NB9aRCwFbmLBfHhRtVbjpmm+fvc yPpzHolKPEFJikZE52MNO7ckCLUx9ldJ7ayZaI1qfiQX3RINYKi9wbdaq+cj2IiXRAQ5 OiUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=k7CifHgN; 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 jj19-20020a170903049300b001a6f3949171si6101572plb.24.2023.05.07.09.09.39; Sun, 07 May 2023 09:09:52 -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=k7CifHgN; 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 S231947AbjEGPzU (ORCPT <rfc822;baris.duru.linux@gmail.com> + 99 others); Sun, 7 May 2023 11:55:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230399AbjEGPzS (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 7 May 2023 11:55:18 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 573DD6A5B; Sun, 7 May 2023 08:55:17 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-64115eef620so29325663b3a.1; Sun, 07 May 2023 08:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683474917; x=1686066917; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=GYb3dk460XgtsVCvhsWMRMsftl/DXZ2mh7rVvkm2EDw=; b=k7CifHgNsCWx5LvdF7fIHaYUiiTtEulUmBd6XZrJknUjjn9gzQN56bL4M435r+xkMl 7Yt8MHg/JaTgzNMCV+lz1ELsQYjd49CRYEKKVe9vOEex0DnO/VaYVcdtRt/Iw3j6QwDn 0waaqU1MtlaJkvTS9RvbFJEZxngjIqnvlpuhfjazqsq948lSgvrGNMSkIWxi75d1mVZr CV3nvxtJ27sEBAVqxFlP6UGkO2huCjmsP1foSNdsysFbKizFLU0X1aHJ9DI4uLJsojuu sJ0jUHkxAlfbmUF5++5TTfO/Pyy1ODG/jh9WzpPDgiBA4r2ay23mwY8zbWWIshHXuRXI Pq6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683474917; x=1686066917; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GYb3dk460XgtsVCvhsWMRMsftl/DXZ2mh7rVvkm2EDw=; b=Ip9m4hFq+Ny0GimrYuM5nZq2RQhhowJwZyGz6Ywz4suLcpLw2NeAY2JC6Oat9lQDZe uI+aa5eY7g7tA4fEkSw31wCzCUcUTi3yjVgKEYWREQCg5SnHqYbfS/DUk7u/oNzqdNtL KC4gt26w/OmdHtuSIPoJnUgeg10u8fa3AqzB27izekd2mspOSApPLCk4PX4Z0qc+w3Ot FU+axNUnEUjGYqZdmu7DVUpxn7ayJ4psqLGb0emBfHCs0OvgkMDBqHSNmVqGZTZ1BFHI 4Id/DQaX5SZSw8wyA3fjF0NBgtkbeOzErMPOLA1N1kR3jCwACRngqr/CgWjpF0Dqm2B9 hc3A== X-Gm-Message-State: AC+VfDwymy9lWglVJ+Ko43kq4lWlkd/IXy5ZbMV8vBr+qdpI1wlsvXgC jOAPkaZwFzDvJJ2Y7S0QVBE= X-Received: by 2002:a17:902:f685:b0:1ac:731b:bc9a with SMTP id l5-20020a170902f68500b001ac731bbc9amr1448873plg.27.1683474916488; Sun, 07 May 2023 08:55:16 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id z6-20020a170903018600b001a3d041ca71sm5338360plg.275.2023.05.07.08.55.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 May 2023 08:55:16 -0700 (PDT) Sender: Vincent Mailhol <vincent.mailhol@gmail.com> From: Vincent Mailhol <mailhol.vincent@wanadoo.fr> To: Marc Kleine-Budde <mkl@pengutronix.de>, linux-can@vger.kernel.org, Marek Vasut <marex@denx.de> Cc: linux-kernel@vger.kernel.org, Vincent Mailhol <mailhol.vincent@wanadoo.fr> Subject: [PATCH] can: length: add definitions for frame lengths in bits Date: Mon, 8 May 2023 00:55:06 +0900 Message-Id: <20230507155506.3179711-1-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765252312247525777?= X-GMAIL-MSGID: =?utf-8?q?1765252312247525777?= |
Series |
can: length: add definitions for frame lengths in bits
|
|
Commit Message
Vincent Mailhol
May 7, 2023, 3:55 p.m. UTC
When created in [1], frames length definitions were added to implement
byte queue limits (bql). Because bql expects lengths in bytes, bit
length definitions were not considered back then.
Recently, a need to refer to the exact frame length in bits, with CAN
bit stuffing, appeared in [2].
Add 9 frames length definitions:
- CAN_FRAME_OVERHEAD_SFF_BITS:
- CAN_FRAME_OVERHEAD_EFF_BITS
- CANFD_FRAME_OVERHEAD_SFF_BITS
- CANFD_FRAME_OVERHEAD_EFF_BITS
- CAN_BIT_STUFFING_OVERHEAD
- CAN_FRAME_LEN_MAX_BITS_NO_STUFFING
- CAN_FRAME_LEN_MAX_BITS_STUFFING
- CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING
- CANFD_FRAME_LEN_MAX_BITS_STUFFING
CAN_FRAME_LEN_MAX_BITS_STUFFING and CANFD_FRAME_LEN_MAX_BITS_STUFFING
define respectively the maximum number of bits in a classical CAN and
CAN-FD frame including bit stuffing. The other definitions are
intermediate values.
In addition to the above:
- Include linux/bits.h and then replace the value 8 by BITS_PER_BYTE
whenever relevant.
- Include linux/math.h because of DIV_ROUND_UP(). N.B: the use of
DIV_ROUND_UP() is not new to this patch, but the include was
previously omitted.
- Update the existing length definitions to use the newly defined values.
- Add myself as copyright owner for 2020 (as coauthor of the initial
version, c.f. [1]) and for 2023 (this patch).
[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 <mailhol.vincent@wanadoo.fr>
---
As always, let me know if you have better inspiration than me for the
naming.
---
include/linux/can/length.h | 84 ++++++++++++++++++++++++++++++++------
1 file changed, 72 insertions(+), 12 deletions(-)
Comments
> When created in [1], frames length definitions were added to implement > byte queue limits (bql). Because bql expects lengths in bytes, bit > length definitions were not considered back then. > > Recently, a need to refer to the exact frame length in bits, with CAN > bit stuffing, appeared in [2]. > > Add 9 frames length definitions: > > - CAN_FRAME_OVERHEAD_SFF_BITS: > - CAN_FRAME_OVERHEAD_EFF_BITS > - CANFD_FRAME_OVERHEAD_SFF_BITS > - CANFD_FRAME_OVERHEAD_EFF_BITS > - CAN_BIT_STUFFING_OVERHEAD > - CAN_FRAME_LEN_MAX_BITS_NO_STUFFING > - CAN_FRAME_LEN_MAX_BITS_STUFFING > - CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING > - CANFD_FRAME_LEN_MAX_BITS_STUFFING > > CAN_FRAME_LEN_MAX_BITS_STUFFING and > CANFD_FRAME_LEN_MAX_BITS_STUFFING > define respectively the maximum number of bits in a classical CAN and > CAN-FD frame including bit stuffing. The other definitions are > intermediate values. > > In addition to the above: > > - Include linux/bits.h and then replace the value 8 by BITS_PER_BYTE > whenever relevant. > - Include linux/math.h because of DIV_ROUND_UP(). N.B: the use of > DIV_ROUND_UP() is not new to this patch, but the include was > previously omitted. > - Update the existing length definitions to use the newly defined values. > - Add myself as copyright owner for 2020 (as coauthor of the initial > version, c.f. [1]) and for 2023 (this patch). > > [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.n > amprd11.prod.outlook.com/ > > Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> > --- > As always, let me know if you have better inspiration than me for the > naming. > --- > include/linux/can/length.h | 84 ++++++++++++++++++++++++++++++++---- > -- > 1 file changed, 72 insertions(+), 12 deletions(-) > > diff --git a/include/linux/can/length.h b/include/linux/can/length.h > index 6995092b774e..60492fcbe34d 100644 > --- a/include/linux/can/length.h > +++ b/include/linux/can/length.h > @@ -1,13 +1,17 @@ > /* SPDX-License-Identifier: GPL-2.0 */ > /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net> > * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de> > + * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr> > */ > > #ifndef _CAN_LENGTH_H > #define _CAN_LENGTH_H > > +#include <linux/bits.h> > +#include <linux/math.h> > + > /* > - * Size of a Classical CAN Standard Frame > + * Size of a Classical CAN Standard Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -25,12 +29,19 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * rounded up and ignoring bitstuffing > + * ignoring bitstuffing > */ > -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) > +#define CAN_FRAME_OVERHEAD_SFF_BITS 47 > > /* > - * Size of a Classical CAN Extended Frame > + * Size of a Classical CAN Standard Frame > + * (rounded up and ignoring bitstuffing) > + */ > +#define CAN_FRAME_OVERHEAD_SFF \ > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > + > +/* > + * Size of a Classical CAN Extended Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -50,12 +61,19 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * rounded up and ignoring bitstuffing > + * ignoring bitstuffing > */ > -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) > +#define CAN_FRAME_OVERHEAD_EFF_BITS 67 > > /* > - * Size of a CAN-FD Standard Frame > + * Size of a Classical CAN Extended Frame > + * (rounded up and ignoring bitstuffing) > + */ > +#define CAN_FRAME_OVERHEAD_EFF \ > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > + > +/* > + * Size of a CAN-FD Standard Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -77,12 +95,19 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * assuming CRC21, rounded up and ignoring bitstuffing > + * assuming CRC21 and ignoring bitstuffing > */ > -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8) > +#define CANFD_FRAME_OVERHEAD_SFF_BITS 61 > > /* > - * Size of a CAN-FD Extended Frame > + * Size of a CAN-FD Standard Frame > + * (assuming CRC21, rounded up and ignoring bitstuffing) > + */ > +#define CANFD_FRAME_OVERHEAD_SFF \ > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > + > +/* > + * Size of a CAN-FD Extended Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -106,9 +131,32 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * assuming CRC21, rounded up and ignoring bitstuffing > + * assuming CRC21 and ignoring bitstuffing > + */ > +#define CANFD_FRAME_OVERHEAD_EFF_BITS 80 > + > +/* > + * Size of a CAN-FD Extended Frame > + * (assuming CRC21, rounded up and ignoring bitstuffing) > + */ > +#define CANFD_FRAME_OVERHEAD_EFF \ > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > + > +/* CAN bit stuffing overhead multiplication factor */ > +#define CAN_BIT_STUFFING_OVERHEAD 1.2 > + > +/* > + * Maximum size of a Classical CAN frame in bits, ignoring bitstuffing > */ > -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8) > +#define CAN_FRAME_LEN_MAX_BITS_NO_STUFFING \ > + (CAN_FRAME_OVERHEAD_EFF_BITS + CAN_MAX_DLEN * > BITS_PER_BYTE) > + > +/* > + * Maximum size of a Classical CAN frame in bits, including bitstuffing > + */ > +#define CAN_FRAME_LEN_MAX_BITS_STUFFING \ > + ((unsigned int)(CAN_FRAME_LEN_MAX_BITS_NO_STUFFING * \ > + CAN_BIT_STUFFING_OVERHEAD)) > > /* > * Maximum size of a Classical CAN frame > @@ -116,6 +164,18 @@ > */ > #define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + > CAN_MAX_DLEN) > > +/* > + * Maximum size of a CAN-FD frame in bits, ignoring bitstuffing > + */ > +#define CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING \ > + (CANFD_FRAME_OVERHEAD_EFF_BITS + CANFD_MAX_DLEN * > BITS_PER_BYTE) > + > +/* > + * Maximum size of a CAN-FD frame in bits, ignoring bitstuffing > + */ > +#define CANFD_FRAME_LEN_MAX_BITS_STUFFING \ > + ((unsigned int)(CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING * \ > + CAN_BIT_STUFFING_OVERHEAD)) > /* > * Maximum size of a CAN-FD frame > * (rounded up and ignoring bitstuffing) > -- I was working on the same thing on Friday but didn't get around to sending it off, so here are a couple thoughts I had when working on the defines in length.h The definitions for IFS in here are called intermission in the standard and I'd argue they shouldn't be part of the frame at all. To quote the ISO: "DFs and RFs shall be separated from preceding frames, whatever frame type they are (DF, RF, EF, OF), by a time period called inter-frame space." So, my suggestion would be to pull out the 3 bit IFS definition that's currently in and introduce 11 bit Bus idle and if necessary 3 bit Intermission separately. index 6995092b774ec..62e92c1553376 100644 --- a/include/linux/can/length.h +++ b/include/linux/can/length.h @@ -6,6 +6,26 @@ #ifndef _CAN_LENGTH_H #define _CAN_LENGTH_H +/* + * First part of the Inter Frame Space + */ +#define CAN_INTERMISSION_BITS 3 + +/* + * Number of consecutive recessive bits on the bus for integration etc. + */ +#define CAN_IDLE_CONDITION_BITS 11 + The field currently called Stuff bit count (SBC) is also not correct I'd say. I'm not sure about the history but given that this is dependent on the DLC I think what's meant is the number of Fixed Stuff bits (FSB) . The ISO does not define a term for the Stuff bit Count but the CiA did define/document it this way. What's meant though is not the number of fixed stuff bits (FSB) which the comment implies here but the modulo 8 3 bit gray-code followed by the parity bit. So for the FD frame definitions I'd propose something like this: Renaming the current SBC to FSB and adding the SBC. /* * Size of a CAN-FD Standard Frame @@ -69,17 +87,17 @@ * 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 */ Best Regards, Thomas
On 08.05.2023 08:54:18, Thomas.Kopp@microchip.com wrote: > I was working on the same thing on Friday but didn't get around to > sending it off, so here are a couple thoughts I had when working on > the defines in length.h > > The definitions for IFS in here are called intermission in the > standard ACK, and IMF seems to be a common abbreviation. > and I'd argue they shouldn't be part of the frame at all. The diagram in https://www.can-cia.org/can-knowledge/can/can-fd/ suggests that IMF is part of the frame. > To > quote the ISO: "DFs and RFs shall be separated from preceding frames, > whatever frame type they are (DF, RF, EF, OF), by a time period called > inter-frame space." > > So, my suggestion would be to pull out the 3 bit IFS definition that's > currently in and introduce 11 bit Bus idle and if necessary 3 bit > Intermission separately. > > index 6995092b774ec..62e92c1553376 100644 > --- a/include/linux/can/length.h > +++ b/include/linux/can/length.h > @@ -6,6 +6,26 @@ > #ifndef _CAN_LENGTH_H > #define _CAN_LENGTH_H > > +/* > + * First part of the Inter Frame Space > + */ > +#define CAN_INTERMISSION_BITS 3 > + > +/* > + * Number of consecutive recessive bits on the bus for integration etc. > + */ > +#define CAN_IDLE_CONDITION_BITS 11 > + > > The field currently called Stuff bit count (SBC) is also not correct > I'd say. I'm not sure about the history but given that this is > dependent on the DLC I think what's meant is the number of Fixed Stuff > bits (FSB) . The ISO does not define a term for the Stuff bit Count > but the CiA did define/document it this way. What's meant though is > not the number of fixed stuff bits (FSB) which the comment implies > here but the modulo 8 3 bit gray-code followed by the parity bit. So > for the FD frame definitions I'd propose something like this: Renaming > the current SBC to FSB and adding the SBC. > /* > * Size of a CAN-FD Standard Frame > @@ -69,17 +87,17 @@ > * 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 ACK > * CRC 0...16: 17 20...64:21 > * CRC delimiter (CD) 1 > + * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 As far as I understand https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047 the FSB is 5 or 6. > * 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 > */ > > Best Regards, > Thomas > Marc
On 08.05.2023 00:55:06, Vincent Mailhol wrote: > When created in [1], frames length definitions were added to implement > byte queue limits (bql). Because bql expects lengths in bytes, bit > length definitions were not considered back then. > > Recently, a need to refer to the exact frame length in bits, with CAN > bit stuffing, appeared in [2]. > > Add 9 frames length definitions: > > - CAN_FRAME_OVERHEAD_SFF_BITS: > - CAN_FRAME_OVERHEAD_EFF_BITS > - CANFD_FRAME_OVERHEAD_SFF_BITS > - CANFD_FRAME_OVERHEAD_EFF_BITS > - CAN_BIT_STUFFING_OVERHEAD > - CAN_FRAME_LEN_MAX_BITS_NO_STUFFING > - CAN_FRAME_LEN_MAX_BITS_STUFFING > - CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING > - CANFD_FRAME_LEN_MAX_BITS_STUFFING > > CAN_FRAME_LEN_MAX_BITS_STUFFING and CANFD_FRAME_LEN_MAX_BITS_STUFFING > define respectively the maximum number of bits in a classical CAN and > CAN-FD frame including bit stuffing. The other definitions are > intermediate values. > > In addition to the above: > > - Include linux/bits.h and then replace the value 8 by BITS_PER_BYTE > whenever relevant. > - Include linux/math.h because of DIV_ROUND_UP(). N.B: the use of > DIV_ROUND_UP() is not new to this patch, but the include was > previously omitted. > - Update the existing length definitions to use the newly defined values. > - Add myself as copyright owner for 2020 (as coauthor of the initial > version, c.f. [1]) and for 2023 (this patch). > > [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 <mailhol.vincent@wanadoo.fr> > --- > As always, let me know if you have better inspiration than me for the > naming. > --- > include/linux/can/length.h | 84 ++++++++++++++++++++++++++++++++------ > 1 file changed, 72 insertions(+), 12 deletions(-) > > diff --git a/include/linux/can/length.h b/include/linux/can/length.h > index 6995092b774e..60492fcbe34d 100644 > --- a/include/linux/can/length.h > +++ b/include/linux/can/length.h > @@ -1,13 +1,17 @@ > /* SPDX-License-Identifier: GPL-2.0 */ > /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net> > * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de> > + * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr> > */ > > #ifndef _CAN_LENGTH_H > #define _CAN_LENGTH_H > > +#include <linux/bits.h> > +#include <linux/math.h> > + > /* > - * Size of a Classical CAN Standard Frame > + * Size of a Classical CAN Standard Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -25,12 +29,19 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * rounded up and ignoring bitstuffing > + * ignoring bitstuffing > */ > -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) > +#define CAN_FRAME_OVERHEAD_SFF_BITS 47 > > /* > - * Size of a Classical CAN Extended Frame > + * Size of a Classical CAN Standard Frame > + * (rounded up and ignoring bitstuffing) > + */ > +#define CAN_FRAME_OVERHEAD_SFF \ > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > + > +/* > + * Size of a Classical CAN Extended Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -50,12 +61,19 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * rounded up and ignoring bitstuffing > + * ignoring bitstuffing > */ > -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) > +#define CAN_FRAME_OVERHEAD_EFF_BITS 67 > > /* > - * Size of a CAN-FD Standard Frame > + * Size of a Classical CAN Extended Frame > + * (rounded up and ignoring bitstuffing) > + */ > +#define CAN_FRAME_OVERHEAD_EFF \ > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > + > +/* > + * Size of a CAN-FD Standard Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -77,12 +95,19 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * assuming CRC21, rounded up and ignoring bitstuffing > + * assuming CRC21 and ignoring bitstuffing > */ > -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8) > +#define CANFD_FRAME_OVERHEAD_SFF_BITS 61 > > /* > - * Size of a CAN-FD Extended Frame > + * Size of a CAN-FD Standard Frame > + * (assuming CRC21, rounded up and ignoring bitstuffing) > + */ > +#define CANFD_FRAME_OVERHEAD_SFF \ > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > + > +/* > + * Size of a CAN-FD Extended Frame in bits > * > * Name of Field Bits > * --------------------------------------------------------- > @@ -106,9 +131,32 @@ > * End-of-frame (EOF) 7 > * Inter frame spacing 3 > * > - * assuming CRC21, rounded up and ignoring bitstuffing > + * assuming CRC21 and ignoring bitstuffing > + */ > +#define CANFD_FRAME_OVERHEAD_EFF_BITS 80 > + > +/* > + * Size of a CAN-FD Extended Frame > + * (assuming CRC21, rounded up and ignoring bitstuffing) > + */ > +#define CANFD_FRAME_OVERHEAD_EFF \ > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > + > +/* CAN bit stuffing overhead multiplication factor */ > +#define CAN_BIT_STUFFING_OVERHEAD 1.2 > + > +/* > + * Maximum size of a Classical CAN frame in bits, ignoring bitstuffing > */ > -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8) > +#define CAN_FRAME_LEN_MAX_BITS_NO_STUFFING \ > + (CAN_FRAME_OVERHEAD_EFF_BITS + CAN_MAX_DLEN * BITS_PER_BYTE) > + > +/* > + * Maximum size of a Classical CAN frame in bits, including bitstuffing > + */ > +#define CAN_FRAME_LEN_MAX_BITS_STUFFING \ > + ((unsigned int)(CAN_FRAME_LEN_MAX_BITS_NO_STUFFING * \ > + CAN_BIT_STUFFING_OVERHEAD)) The 1.2 overhead doesn't apply to the whole frame, according to https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047. Marc
Hi Thomas, Thank you for your review. You had me reopen ISO 11898-1 (haven't done so for a long time). I mostly agree with you. Many of your points are not related to this patch but to the already existing definition. So I will handle these in a separate patch. I will prepare a v2 within the next few days. On Mon. 8 May 2023 à 21:29, Marc Kleine-Budde <mkl@pengutronix.de> wrote: > On 08.05.2023 08:54:18, Thomas.Kopp@microchip.com wrote: > > I was working on the same thing on Friday but didn't get around to > > sending it off, so here are a couple thoughts I had when working on > > the defines in length.h > > > > The definitions for IFS in here are called intermission in the > > standard > > ACK, and IMF seems to be a common abbreviation. ACK. I think I will put a reference to both naming (intermission and IMF) and use the IMF forward for conciseness. > > and I'd argue they shouldn't be part of the frame at all. > > The diagram in https://www.can-cia.org/can-knowledge/can/can-fd/ > suggests that IMF is part of the frame. ISO 11898-1:2015 section 10.4.6 "Specification of inter-frame space" makes it clear that the intermission is not part of the frame but part of the "Inter-frame space". To close the discussion, I would finally argue that the intermission occurs after the EOF bit. Something after an "End of Frame" is not part of the frame, right? I agree with and will follow Thomas's suggestion. > > To > > quote the ISO: "DFs and RFs shall be separated from preceding frames, > > whatever frame type they are (DF, RF, EF, OF), by a time period called > > inter-frame space." > > > > So, my suggestion would be to pull out the 3 bit IFS definition that's > > currently in and introduce 11 bit Bus idle and if necessary 3 bit > > Intermission separately. ACK. > > index 6995092b774ec..62e92c1553376 100644 > > --- a/include/linux/can/length.h > > +++ b/include/linux/can/length.h > > @@ -6,6 +6,26 @@ > > #ifndef _CAN_LENGTH_H > > #define _CAN_LENGTH_H > > > > +/* > > + * First part of the Inter Frame Space > > + */ > > +#define CAN_INTERMISSION_BITS 3 > > + > > +/* > > + * Number of consecutive recessive bits on the bus for integration etc. > > + */ > > +#define CAN_IDLE_CONDITION_BITS 11 > > + > > > > The field currently called Stuff bit count (SBC) is also not correct > > I'd say. I'm not sure about the history but given that this is > > dependent on the DLC I think what's meant is the number of Fixed Stuff > > bits (FSB) . The ISO does not define a term for the Stuff bit Count > > but the CiA did define/document it this way. What's meant though is > > not the number of fixed stuff bits (FSB) which the comment implies > > here but the modulo 8 3 bit gray-code followed by the parity bit. So > > for the FD frame definitions I'd propose something like this: Renaming > > the current SBC to FSB and adding the SBC. ACK. I double checked the standard, you are right. > > /* > > * Size of a CAN-FD Standard Frame > > @@ -69,17 +87,17 @@ > > * 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 > > ACK > > > * CRC 0...16: 17 20...64:21 > > * CRC delimiter (CD) 1 > > + * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 > > As far as I understand > https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047 the FSB > is 5 or 6. Reading the ISO, the fixed bit stuff applies to the CRC field (which, in the standard nomenclature, includes both the stuff count and the CRC sequence). The CRC field starts with a fixed stuff bit and then has another fixed stuff bit after each fourth bit. So the formula would be: FSB count = 1 + round_down(len(CRC field)/4) = 1 + round_down((len(stuff count) + len(CRC sequence))/4) In case of CRC_17: FSB count = 1 + round_down((4 + 17)/4) = 6 This is coherent with the last figure of https://www.can-cia.org/can-knowledge/can/can-fd/ which shows 6 FSB for CRC_17. In case of CRC_21: FSB count = 1 + round_down((4 + 21)/4) = 7 So, ACK for Thomas, NACK for Marc (sorry :)) > > * 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 > > */ Yours sincerely, Vincent Mailhol
On Mon. 8 May 2023 at 21:29, Marc Kleine-Budde <mkl@pengutronix.de> wrote: > On 08.05.2023 00:55:06, Vincent Mailhol wrote: > > When created in [1], frames length definitions were added to implement > > byte queue limits (bql). Because bql expects lengths in bytes, bit > > length definitions were not considered back then. > > > > Recently, a need to refer to the exact frame length in bits, with CAN > > bit stuffing, appeared in [2]. > > > > Add 9 frames length definitions: > > > > - CAN_FRAME_OVERHEAD_SFF_BITS: > > - CAN_FRAME_OVERHEAD_EFF_BITS > > - CANFD_FRAME_OVERHEAD_SFF_BITS > > - CANFD_FRAME_OVERHEAD_EFF_BITS > > - CAN_BIT_STUFFING_OVERHEAD > > - CAN_FRAME_LEN_MAX_BITS_NO_STUFFING > > - CAN_FRAME_LEN_MAX_BITS_STUFFING > > - CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING > > - CANFD_FRAME_LEN_MAX_BITS_STUFFING > > > > CAN_FRAME_LEN_MAX_BITS_STUFFING and CANFD_FRAME_LEN_MAX_BITS_STUFFING > > define respectively the maximum number of bits in a classical CAN and > > CAN-FD frame including bit stuffing. The other definitions are > > intermediate values. > > > > In addition to the above: > > > > - Include linux/bits.h and then replace the value 8 by BITS_PER_BYTE > > whenever relevant. > > - Include linux/math.h because of DIV_ROUND_UP(). N.B: the use of > > DIV_ROUND_UP() is not new to this patch, but the include was > > previously omitted. > > - Update the existing length definitions to use the newly defined values. > > - Add myself as copyright owner for 2020 (as coauthor of the initial > > version, c.f. [1]) and for 2023 (this patch). > > > > [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 <mailhol.vincent@wanadoo.fr> > > --- > > As always, let me know if you have better inspiration than me for the > > naming. > > --- > > include/linux/can/length.h | 84 ++++++++++++++++++++++++++++++++------ > > 1 file changed, 72 insertions(+), 12 deletions(-) > > > > diff --git a/include/linux/can/length.h b/include/linux/can/length.h > > index 6995092b774e..60492fcbe34d 100644 > > --- a/include/linux/can/length.h > > +++ b/include/linux/can/length.h > > @@ -1,13 +1,17 @@ > > /* SPDX-License-Identifier: GPL-2.0 */ > > /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net> > > * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de> > > + * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr> > > */ > > > > #ifndef _CAN_LENGTH_H > > #define _CAN_LENGTH_H > > > > +#include <linux/bits.h> > > +#include <linux/math.h> > > + > > /* > > - * Size of a Classical CAN Standard Frame > > + * Size of a Classical CAN Standard Frame in bits > > * > > * Name of Field Bits > > * --------------------------------------------------------- > > @@ -25,12 +29,19 @@ > > * End-of-frame (EOF) 7 > > * Inter frame spacing 3 > > * > > - * rounded up and ignoring bitstuffing > > + * ignoring bitstuffing > > */ > > -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) > > +#define CAN_FRAME_OVERHEAD_SFF_BITS 47 > > > > /* > > - * Size of a Classical CAN Extended Frame > > + * Size of a Classical CAN Standard Frame > > + * (rounded up and ignoring bitstuffing) > > + */ > > +#define CAN_FRAME_OVERHEAD_SFF \ > > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > > + > > +/* > > + * Size of a Classical CAN Extended Frame in bits > > * > > * Name of Field Bits > > * --------------------------------------------------------- > > @@ -50,12 +61,19 @@ > > * End-of-frame (EOF) 7 > > * Inter frame spacing 3 > > * > > - * rounded up and ignoring bitstuffing > > + * ignoring bitstuffing > > */ > > -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) > > +#define CAN_FRAME_OVERHEAD_EFF_BITS 67 > > > > /* > > - * Size of a CAN-FD Standard Frame > > + * Size of a Classical CAN Extended Frame > > + * (rounded up and ignoring bitstuffing) > > + */ > > +#define CAN_FRAME_OVERHEAD_EFF \ > > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > > + > > +/* > > + * Size of a CAN-FD Standard Frame in bits > > * > > * Name of Field Bits > > * --------------------------------------------------------- > > @@ -77,12 +95,19 @@ > > * End-of-frame (EOF) 7 > > * Inter frame spacing 3 > > * > > - * assuming CRC21, rounded up and ignoring bitstuffing > > + * assuming CRC21 and ignoring bitstuffing > > */ > > -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8) > > +#define CANFD_FRAME_OVERHEAD_SFF_BITS 61 > > > > /* > > - * Size of a CAN-FD Extended Frame > > + * Size of a CAN-FD Standard Frame > > + * (assuming CRC21, rounded up and ignoring bitstuffing) > > + */ > > +#define CANFD_FRAME_OVERHEAD_SFF \ > > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > > + > > +/* > > + * Size of a CAN-FD Extended Frame in bits > > * > > * Name of Field Bits > > * --------------------------------------------------------- > > @@ -106,9 +131,32 @@ > > * End-of-frame (EOF) 7 > > * Inter frame spacing 3 > > * > > - * assuming CRC21, rounded up and ignoring bitstuffing > > + * assuming CRC21 and ignoring bitstuffing > > + */ > > +#define CANFD_FRAME_OVERHEAD_EFF_BITS 80 > > + > > +/* > > + * Size of a CAN-FD Extended Frame > > + * (assuming CRC21, rounded up and ignoring bitstuffing) > > + */ > > +#define CANFD_FRAME_OVERHEAD_EFF \ > > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > > + > > +/* CAN bit stuffing overhead multiplication factor */ > > +#define CAN_BIT_STUFFING_OVERHEAD 1.2 > > + > > +/* > > + * Maximum size of a Classical CAN frame in bits, ignoring bitstuffing > > */ > > -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8) > > +#define CAN_FRAME_LEN_MAX_BITS_NO_STUFFING \ > > + (CAN_FRAME_OVERHEAD_EFF_BITS + CAN_MAX_DLEN * BITS_PER_BYTE) > > + > > +/* > > + * Maximum size of a Classical CAN frame in bits, including bitstuffing > > + */ > > +#define CAN_FRAME_LEN_MAX_BITS_STUFFING \ > > + ((unsigned int)(CAN_FRAME_LEN_MAX_BITS_NO_STUFFING * \ > > + CAN_BIT_STUFFING_OVERHEAD)) > > The 1.2 overhead doesn't apply to the whole frame, according to > https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047. You are right. In fact, I realized this mistake before reading your message (while I was studying the standard to answer Thomas). ISO 11898-1:2015 section 10.5 "Frame coding" says: the frame segment as SOF, arbitration field, control field, data field, and CRC sequence shall be coded by the method of bit stuffing. and: The remaining bit fields of the DF or RF (CRC delimiter, ACK field and EOF) shall be of fixed form and not stuffed. So, indeed, the bit stuffing does not apply to the last 10 bits (1 + 1 + 1 + 7). Furthermore, for FD frames, the CRC field already contains the fixed stuff bits. So the overhead shall not be applied again or else, stuff bits would be counted twice. In conclusion, for FD frames, the otherhead for dynamic bit stuffing overhead should apply to the SOF, arbitration field, control field and data field segments. After reading the standard, I thought again about the overhead ratio and it is more complicated than we all thought. Let's use below nomenclature in the following examples (borrowed from ISO): - "0": dominant bit - "o": dominant stuff bit - "1": recessive bit - "i": recessive stuff bit We probably all though below example to be the worst case: Destuffed: 11111 11111 11111 11111 Stuffed: 11111o 11111o 11111o 11111o Here, indeed, one stuff bit is added every five bits giving us an overhead of 6/5 = 1.2. However, ISO 11898-1:2015 section 10.5 "Frame coding" also says: Whenever a transmitter detects five consecutive bits (*including stuff bits*) of identical value in the bit stream to be transmitted, it shall automatically insert a complementary bit (called stuff bit) ... Pay attention to the *including stuff bits* part. The worst case is actually a sequence in which dominant and recessive alternate every four bits: Destuffed: 1 1111 0000 1111 0000 1111 Stuffed: 1 1111o 0000i 1111o 0000i 1111o Ignoring the first bit, one stuff bit is added every four bits giving us an overhead of 5/4 = 1.25. The exact formula taking in account the first bit we previously ignored then: Number of dynamic stuff bit = 1 + round_down((len(stream) - 5) / 4) = round_down((len(stream) - 1) / 4) Yours sincerely, Vincent Mailhol
> On 08.05.2023 08:54:18, Thomas.Kopp@microchip.com wrote: > > I was working on the same thing on Friday but didn't get around to > > sending it off, so here are a couple thoughts I had when working on > > the defines in length.h > > > > The definitions for IFS in here are called intermission in the > > standard > > ACK, and IMF seems to be a common abbreviation. > > > and I'd argue they shouldn't be part of the frame at all. > > The diagram in https://www.can-cia.org/can-knowledge/can/can-fd/ > suggests that IMF is part of the frame. I'd disagree as the ISO specifically says it's not part of the frame. The diagram on page PDF page 21 of the 2.0 spec: http://esd.cs.ucr.edu/webres/can20.pdf is also in the ISO and shows the Intermission outside the frame. Also the word INTERframe space suggests it shouldn't be part of the frame and lastly the definition is used for a) determining how many bits/bytes are needed to store frames which doesn't need the intermission bits and b) timing, but for those purpose the frame has ended already and if the timing of several frames is needed, complete interframe spaces need to be added. > > To > > quote the ISO: "DFs and RFs shall be separated from preceding frames, > > whatever frame type they are (DF, RF, EF, OF), by a time period called > > inter-frame space." > > > > So, my suggestion would be to pull out the 3 bit IFS definition that's > > currently in and introduce 11 bit Bus idle and if necessary 3 bit > > Intermission separately. > > > > index 6995092b774ec..62e92c1553376 100644 > > --- a/include/linux/can/length.h > > +++ b/include/linux/can/length.h > > @@ -6,6 +6,26 @@ > > #ifndef _CAN_LENGTH_H > > #define _CAN_LENGTH_H > > > > +/* > > + * First part of the Inter Frame Space > > + */ > > +#define CAN_INTERMISSION_BITS 3 > > + > > +/* > > + * Number of consecutive recessive bits on the bus for integration etc. > > + */ > > +#define CAN_IDLE_CONDITION_BITS 11 > > + > > > > The field currently called Stuff bit count (SBC) is also not correct > > I'd say. I'm not sure about the history but given that this is > > dependent on the DLC I think what's meant is the number of Fixed Stuff > > bits (FSB) . The ISO does not define a term for the Stuff bit Count > > but the CiA did define/document it this way. What's meant though is > > not the number of fixed stuff bits (FSB) which the comment implies > > here but the modulo 8 3 bit gray-code followed by the parity bit. So > > for the FD frame definitions I'd propose something like this: Renaming > > the current SBC to FSB and adding the SBC. > > > * CRC 0...16: 17 20...64:21 > > * CRC delimiter (CD) 1 > > + * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 > > As far as I understand > https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047 the > FSB > is 5 or 6. I don't know where the paper got its numbers from but it also seems to be missing the SBC field completely? The ISO says: " There shall be a fixed stuff bit before the first bit of the stuff count[...]" " A further fixed stuff bit shall be inserted after each fourth bit of the CRC field" Not that the CRC field in FD frames also contains the SBC so that adds fixed stuff bits. A good visual representation of the FSBs is on the first page you provided as a source: https://www.can-cia.org/can-knowledge/can/can-fd/ all the way on the bottom. Best Regards, Thomas
On 09.05.2023 13:16:08, Vincent MAILHOL wrote: > > The diagram in https://www.can-cia.org/can-knowledge/can/can-fd/ > > suggests that IMF is part of the frame. > > ISO 11898-1:2015 section 10.4.6 "Specification of inter-frame space" > makes it clear that the intermission is not part of the frame but part > of the "Inter-frame space". For this reason, it is good to have open standards...oh wait! > To close the discussion, I would finally argue that the intermission > occurs after the EOF bit. Something after an "End of Frame" is not > part of the frame, right? > > I agree with and will follow Thomas's suggestion. [...] > > > /* > > > * Size of a CAN-FD Standard Frame > > > @@ -69,17 +87,17 @@ > > > * 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 > > > > ACK > > > > > * CRC 0...16: 17 20...64:21 > > > * CRC delimiter (CD) 1 > > > + * Fixed Stuff bits (FSB) 0...16: 6 20...64:7 > > > > As far as I understand > > https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047 the FSB > > is 5 or 6. > > Reading the ISO, the fixed bit stuff applies to the CRC field (which, > in the standard nomenclature, includes both the stuff count and the > CRC sequence). > The CRC field starts with a fixed stuff bit and then has another fixed > stuff bit after each fourth bit. > > So the formula would be: > > FSB count = 1 + round_down(len(CRC field)/4) > = 1 + round_down((len(stuff count) + len(CRC sequence))/4) > > In case of CRC_17: > > FSB count = 1 + round_down((4 + 17)/4) > = 6 > > This is coherent with the last figure of > https://www.can-cia.org/can-knowledge/can/can-fd/ which shows 6 FSB > for CRC_17. > > In case of CRC_21: > > FSB count = 1 + round_down((4 + 21)/4) > = 7 > > So, ACK for Thomas, NACK for Marc (sorry :)) It seems that the reviewers of this paper missed some parts :) Marc
Hi Vincent, > On Mon. 8 May 2023 at 21:29, Marc Kleine-Budde <mkl@pengutronix.de> > wrote: > > On 08.05.2023 00:55:06, Vincent Mailhol wrote: > > > When created in [1], frames length definitions were added to implement > > > byte queue limits (bql). Because bql expects lengths in bytes, bit > > > length definitions were not considered back then. > > > > > > Recently, a need to refer to the exact frame length in bits, with CAN > > > bit stuffing, appeared in [2]. > > > > > > Add 9 frames length definitions: > > > > > > - CAN_FRAME_OVERHEAD_SFF_BITS: > > > - CAN_FRAME_OVERHEAD_EFF_BITS > > > - CANFD_FRAME_OVERHEAD_SFF_BITS > > > - CANFD_FRAME_OVERHEAD_EFF_BITS > > > - CAN_BIT_STUFFING_OVERHEAD > > > - CAN_FRAME_LEN_MAX_BITS_NO_STUFFING > > > - CAN_FRAME_LEN_MAX_BITS_STUFFING > > > - CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING > > > - CANFD_FRAME_LEN_MAX_BITS_STUFFING > > > > > > CAN_FRAME_LEN_MAX_BITS_STUFFING and > CANFD_FRAME_LEN_MAX_BITS_STUFFING > > > define respectively the maximum number of bits in a classical CAN and > > > CAN-FD frame including bit stuffing. The other definitions are > > > intermediate values. > > > > > > In addition to the above: > > > > > > - Include linux/bits.h and then replace the value 8 by BITS_PER_BYTE > > > whenever relevant. > > > - Include linux/math.h because of DIV_ROUND_UP(). N.B: the use of > > > DIV_ROUND_UP() is not new to this patch, but the include was > > > previously omitted. > > > - Update the existing length definitions to use the newly defined values. > > > - Add myself as copyright owner for 2020 (as coauthor of the initial > > > version, c.f. [1]) and for 2023 (this patch). > > > > > > [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.n > amprd11.prod.outlook.com/ > > > > > > Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> > > > --- > > > As always, let me know if you have better inspiration than me for the > > > naming. > > > --- > > > include/linux/can/length.h | 84 > ++++++++++++++++++++++++++++++++------ > > > 1 file changed, 72 insertions(+), 12 deletions(-) > > > > > > diff --git a/include/linux/can/length.h b/include/linux/can/length.h > > > index 6995092b774e..60492fcbe34d 100644 > > > --- a/include/linux/can/length.h > > > +++ b/include/linux/can/length.h > > > @@ -1,13 +1,17 @@ > > > /* SPDX-License-Identifier: GPL-2.0 */ > > > /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net> > > > * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de> > > > + * Copyright (C) 2020, 2023 Vincent Mailhol > <mailhol.vincent@wanadoo.fr> > > > */ > > > > > > #ifndef _CAN_LENGTH_H > > > #define _CAN_LENGTH_H > > > > > > +#include <linux/bits.h> > > > +#include <linux/math.h> > > > + > > > /* > > > - * Size of a Classical CAN Standard Frame > > > + * Size of a Classical CAN Standard Frame in bits > > > * > > > * Name of Field Bits > > > * --------------------------------------------------------- > > > @@ -25,12 +29,19 @@ > > > * End-of-frame (EOF) 7 > > > * Inter frame spacing 3 > > > * > > > - * rounded up and ignoring bitstuffing > > > + * ignoring bitstuffing > > > */ > > > -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) > > > +#define CAN_FRAME_OVERHEAD_SFF_BITS 47 > > > > > > /* > > > - * Size of a Classical CAN Extended Frame > > > + * Size of a Classical CAN Standard Frame > > > + * (rounded up and ignoring bitstuffing) > > > + */ > > > +#define CAN_FRAME_OVERHEAD_SFF \ > > > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) > > > + > > > +/* > > > + * Size of a Classical CAN Extended Frame in bits > > > * > > > * Name of Field Bits > > > * --------------------------------------------------------- > > > @@ -50,12 +61,19 @@ > > > * End-of-frame (EOF) 7 > > > * Inter frame spacing 3 > > > * > > > - * rounded up and ignoring bitstuffing > > > + * ignoring bitstuffing > > > */ > > > -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) > > > +#define CAN_FRAME_OVERHEAD_EFF_BITS 67 > > > > > > /* > > > - * Size of a CAN-FD Standard Frame > > > + * Size of a Classical CAN Extended Frame > > > + * (rounded up and ignoring bitstuffing) > > > + */ > > > +#define CAN_FRAME_OVERHEAD_EFF \ > > > + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) > > > + > > > +/* > > > + * Size of a CAN-FD Standard Frame in bits > > > * > > > * Name of Field Bits > > > * --------------------------------------------------------- > > > @@ -77,12 +95,19 @@ > > > * End-of-frame (EOF) 7 > > > * Inter frame spacing 3 > > > * > > > - * assuming CRC21, rounded up and ignoring bitstuffing > > > + * assuming CRC21 and ignoring bitstuffing > > > */ > > > -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8) > > > +#define CANFD_FRAME_OVERHEAD_SFF_BITS 61 > > > > > > /* > > > - * Size of a CAN-FD Extended Frame > > > + * Size of a CAN-FD Standard Frame > > > + * (assuming CRC21, rounded up and ignoring bitstuffing) > > > + */ > > > +#define CANFD_FRAME_OVERHEAD_SFF \ > > > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_SFF_BITS, > BITS_PER_BYTE) > > > + > > > +/* > > > + * Size of a CAN-FD Extended Frame in bits > > > * > > > * Name of Field Bits > > > * --------------------------------------------------------- > > > @@ -106,9 +131,32 @@ > > > * End-of-frame (EOF) 7 > > > * Inter frame spacing 3 > > > * > > > - * assuming CRC21, rounded up and ignoring bitstuffing > > > + * assuming CRC21 and ignoring bitstuffing > > > + */ > > > +#define CANFD_FRAME_OVERHEAD_EFF_BITS 80 > > > + > > > +/* > > > + * Size of a CAN-FD Extended Frame > > > + * (assuming CRC21, rounded up and ignoring bitstuffing) > > > + */ > > > +#define CANFD_FRAME_OVERHEAD_EFF \ > > > + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_EFF_BITS, > BITS_PER_BYTE) > > > + > > > +/* CAN bit stuffing overhead multiplication factor */ > > > +#define CAN_BIT_STUFFING_OVERHEAD 1.2 > > > + > > > +/* > > > + * Maximum size of a Classical CAN frame in bits, ignoring bitstuffing > > > */ > > > -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8) > > > +#define CAN_FRAME_LEN_MAX_BITS_NO_STUFFING \ > > > + (CAN_FRAME_OVERHEAD_EFF_BITS + CAN_MAX_DLEN * > BITS_PER_BYTE) > > > + > > > +/* > > > + * Maximum size of a Classical CAN frame in bits, including bitstuffing > > > + */ > > > +#define CAN_FRAME_LEN_MAX_BITS_STUFFING \ > > > + ((unsigned int)(CAN_FRAME_LEN_MAX_BITS_NO_STUFFING * \ > > > + CAN_BIT_STUFFING_OVERHEAD)) > > > > The 1.2 overhead doesn't apply to the whole frame, according to > > https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8338047. > > You are right. In fact, I realized this mistake before reading your > message (while I was studying the standard to answer Thomas). > > ISO 11898-1:2015 section 10.5 "Frame coding" says: > > the frame segment as SOF, arbitration field, control field, > data field, and CRC sequence shall be coded by the method of > bit stuffing. > > and: > > The remaining bit fields of the DF or RF (CRC delimiter, ACK > field and EOF) shall be of fixed form and not stuffed. > > So, indeed, the bit stuffing does not apply to the last 10 bits (1 + 1 + 1 + 7). > Furthermore, for FD frames, the CRC field already contains the fixed > stuff bits. So the overhead shall not be applied again or else, stuff > bits would be counted twice. In conclusion, for FD frames, the > otherhead for dynamic bit stuffing overhead should apply to the SOF, > arbitration field, control field and data field segments. > > After reading the standard, I thought again about the overhead ratio > and it is more complicated than we all thought. > > Let's use below nomenclature in the following examples (borrowed from ISO): > > - "0": dominant bit > - "o": dominant stuff bit > - "1": recessive bit > - "i": recessive stuff bit > > We probably all though below example to be the worst case: > > Destuffed: 11111 11111 11111 11111 > Stuffed: 11111o 11111o 11111o 11111o > > Here, indeed, one stuff bit is added every five bits giving us an > overhead of 6/5 = 1.2. > > However, ISO 11898-1:2015 section 10.5 "Frame coding" also says: > > Whenever a transmitter detects five consecutive bits (*including > stuff bits*) of identical value in the bit stream to be > transmitted, it shall automatically insert a complementary > bit (called stuff bit) ... > > Pay attention to the *including stuff bits* part. The worst case is > actually a sequence in which dominant and recessive alternate every > four bits: > > Destuffed: 1 1111 0000 1111 0000 1111 > Stuffed: 1 1111o 0000i 1111o 0000i 1111o > > Ignoring the first bit, one stuff bit is added every four bits giving > us an overhead of 5/4 = 1.25. Duh, absolutely right. > The exact formula taking in account the first bit we previously ignored then: > > Number of dynamic stuff bit = 1 + round_down((len(stream) - 5) / 4) > = round_down((len(stream) - 1) / 4) > Right, do you plan on separating this for Arbitration bitrate and databitrate? It would probably make sense to use a fixed number of worst case stuffbits for the arbitration phase and the formula for the data phase. Best Regards, Thomas
Hi Thomas,
On Mon. 9 May 2023 at 16:12, <Thomas.Kopp@microchip.com> wrote:
[...]
> Right, do you plan on separating this for Arbitration bitrate and databitrate? It would probably make sense to use a fixed number of worst case stuffbits for the arbitration phase and the formula for the data phase.
I have a few ideas how to implement it, but seeing how complex things
are going, I am thinking of creating an inline helper function for the
bitstuffing calculation (the compiler should be able to fold it into a
constant expression, so there should be no penalty).
For the exact details, I have not decided yet. I need to experiment.
This not being so trivial and not having so much free time now, please
wait a few days for the v2 ;)
On Tue. 9 May 2023 at 15:50, Marc Kleine-Budde <mkl@pengutronix.de> wrote: > On 09.05.2023 13:16:08, Vincent MAILHOL wrote: > > > The diagram in https://www.can-cia.org/can-knowledge/can/can-fd/ > > > suggests that IMF is part of the frame. > > > > ISO 11898-1:2015 section 10.4.6 "Specification of inter-frame space" > > makes it clear that the intermission is not part of the frame but part > > of the "Inter-frame space". > > For this reason, it is good to have open standards...oh wait! It is open is you (or your company, wink, wink) pay CHF 187: https://www.iso.org/standard/63648.html
diff --git a/include/linux/can/length.h b/include/linux/can/length.h index 6995092b774e..60492fcbe34d 100644 --- a/include/linux/can/length.h +++ b/include/linux/can/length.h @@ -1,13 +1,17 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* Copyright (C) 2020 Oliver Hartkopp <socketcan@hartkopp.net> * Copyright (C) 2020 Marc Kleine-Budde <kernel@pengutronix.de> + * Copyright (C) 2020, 2023 Vincent Mailhol <mailhol.vincent@wanadoo.fr> */ #ifndef _CAN_LENGTH_H #define _CAN_LENGTH_H +#include <linux/bits.h> +#include <linux/math.h> + /* - * Size of a Classical CAN Standard Frame + * Size of a Classical CAN Standard Frame in bits * * Name of Field Bits * --------------------------------------------------------- @@ -25,12 +29,19 @@ * End-of-frame (EOF) 7 * Inter frame spacing 3 * - * rounded up and ignoring bitstuffing + * ignoring bitstuffing */ -#define CAN_FRAME_OVERHEAD_SFF DIV_ROUND_UP(47, 8) +#define CAN_FRAME_OVERHEAD_SFF_BITS 47 /* - * Size of a Classical CAN Extended Frame + * Size of a Classical CAN Standard Frame + * (rounded up and ignoring bitstuffing) + */ +#define CAN_FRAME_OVERHEAD_SFF \ + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) + +/* + * Size of a Classical CAN Extended Frame in bits * * Name of Field Bits * --------------------------------------------------------- @@ -50,12 +61,19 @@ * End-of-frame (EOF) 7 * Inter frame spacing 3 * - * rounded up and ignoring bitstuffing + * ignoring bitstuffing */ -#define CAN_FRAME_OVERHEAD_EFF DIV_ROUND_UP(67, 8) +#define CAN_FRAME_OVERHEAD_EFF_BITS 67 /* - * Size of a CAN-FD Standard Frame + * Size of a Classical CAN Extended Frame + * (rounded up and ignoring bitstuffing) + */ +#define CAN_FRAME_OVERHEAD_EFF \ + DIV_ROUND_UP(CAN_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) + +/* + * Size of a CAN-FD Standard Frame in bits * * Name of Field Bits * --------------------------------------------------------- @@ -77,12 +95,19 @@ * End-of-frame (EOF) 7 * Inter frame spacing 3 * - * assuming CRC21, rounded up and ignoring bitstuffing + * assuming CRC21 and ignoring bitstuffing */ -#define CANFD_FRAME_OVERHEAD_SFF DIV_ROUND_UP(61, 8) +#define CANFD_FRAME_OVERHEAD_SFF_BITS 61 /* - * Size of a CAN-FD Extended Frame + * Size of a CAN-FD Standard Frame + * (assuming CRC21, rounded up and ignoring bitstuffing) + */ +#define CANFD_FRAME_OVERHEAD_SFF \ + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_SFF_BITS, BITS_PER_BYTE) + +/* + * Size of a CAN-FD Extended Frame in bits * * Name of Field Bits * --------------------------------------------------------- @@ -106,9 +131,32 @@ * End-of-frame (EOF) 7 * Inter frame spacing 3 * - * assuming CRC21, rounded up and ignoring bitstuffing + * assuming CRC21 and ignoring bitstuffing + */ +#define CANFD_FRAME_OVERHEAD_EFF_BITS 80 + +/* + * Size of a CAN-FD Extended Frame + * (assuming CRC21, rounded up and ignoring bitstuffing) + */ +#define CANFD_FRAME_OVERHEAD_EFF \ + DIV_ROUND_UP(CANFD_FRAME_OVERHEAD_EFF_BITS, BITS_PER_BYTE) + +/* CAN bit stuffing overhead multiplication factor */ +#define CAN_BIT_STUFFING_OVERHEAD 1.2 + +/* + * Maximum size of a Classical CAN frame in bits, ignoring bitstuffing */ -#define CANFD_FRAME_OVERHEAD_EFF DIV_ROUND_UP(80, 8) +#define CAN_FRAME_LEN_MAX_BITS_NO_STUFFING \ + (CAN_FRAME_OVERHEAD_EFF_BITS + CAN_MAX_DLEN * BITS_PER_BYTE) + +/* + * Maximum size of a Classical CAN frame in bits, including bitstuffing + */ +#define CAN_FRAME_LEN_MAX_BITS_STUFFING \ + ((unsigned int)(CAN_FRAME_LEN_MAX_BITS_NO_STUFFING * \ + CAN_BIT_STUFFING_OVERHEAD)) /* * Maximum size of a Classical CAN frame @@ -116,6 +164,18 @@ */ #define CAN_FRAME_LEN_MAX (CAN_FRAME_OVERHEAD_EFF + CAN_MAX_DLEN) +/* + * Maximum size of a CAN-FD frame in bits, ignoring bitstuffing + */ +#define CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING \ + (CANFD_FRAME_OVERHEAD_EFF_BITS + CANFD_MAX_DLEN * BITS_PER_BYTE) + +/* + * Maximum size of a CAN-FD frame in bits, ignoring bitstuffing + */ +#define CANFD_FRAME_LEN_MAX_BITS_STUFFING \ + ((unsigned int)(CANFD_FRAME_LEN_MAX_BITS_NO_STUFFING * \ + CAN_BIT_STUFFING_OVERHEAD)) /* * Maximum size of a CAN-FD frame * (rounded up and ignoring bitstuffing)