From nobody Wed Nov 27 14:17:29 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Xz1jk1Z1Vz5fGhc; Wed, 27 Nov 2024 14:17:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Xz1jk0LKpz4WlF; Wed, 27 Nov 2024 14:17:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732717050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rEIMQH+R+CMUQtaB8b0Es+kZIoSlWQeh4SJzv8hcuV4=; b=fDHbOBF4BtiSJeK2zbeMEW0cIsIS+MffqNw1PpMNOZ0mNwvTDBMWoRwTUGbfbcPorIneJc 9Id7lyPH66ZFQQ7cuypo5p7bt9UEwYgSePiwEThEgx2yDmE05O4mNPYz/zPlaXWHFAmOMW Q2lnh7Z/nrUt/RfXjw4EbZfCqmvh463+jjLVkSSO68NJl6W45KeNuPnrtSVc6m5e7OxZ3U s23P1DUNQm6uen6lkJxYG7X+ivuWkRXVpx+BZ1LxyXZq58H2sY12HpuR6yaeNwQH6ZGheC Xt+jXww01ffazimwOSC7Iw5I6syx65Lrxs9o0/8DmWWf56beW/DK5LTACzijpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732717050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rEIMQH+R+CMUQtaB8b0Es+kZIoSlWQeh4SJzv8hcuV4=; b=H5dIRp7reFa8p9Op5VNgFQSUaFfTtswmCQvgzuBdlCBwVWe2onIiiWGT+5RA5b6zxzDV+1 H+8hWKb9douj0soMfs7oclzhCObZ7BfseN5LnkY4k7LhuYW1dFz/DmVq173ImxQIwH6vBj nGoTHpLRYQxV5NqvNXhMQP58dnmggG6oCA51vqyPGmeHZeBnzEmy8qNiPzZjXsjzzVcr0M 2zYQ5cDhZwMonc2MnisT8PaK+rxA1uQs/r3fY5XiOdsNNeK2N2o2iWyemjFTVcs1pm3kxM cK0VvRCVk2r7rZJcxIKXU5vvZ4Rd60HRig9vyLluxjVJGvwTXzu7twkJ3cLmmQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732717050; a=rsa-sha256; cv=none; b=J+MWy1ev+cqfEhJVj7A/00okUztSzhotZz4JqdLT1P8zs/4NFvVWXCk2ERLOiS0c3Nf9x0 59qRteeUFPL9yvejNhh7j4oMV7wWBokarDhXOu5pfAA9mmmTO7DC6u4cqMTJPX5xlGU+hL Kd5a2vsosK0yg6vNFpaHtol6QOd+Fi6p7Tj53CS7uxolVK63QPFiDPAOzZS+n84B6kF6mR dzX5z9mPNAVxbg6pGIlrD1N9ZKvUK19SkX+/vSreUO0i1nnv/V1mPWMC3GWJCGgga8ITR9 Qz/1LepSw/y6lnSCRlnB//X50DIN2wb914zN/CtudcCXhitjAMNm0ZK8eLgIzQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Xz1jj6lt4z1C50; Wed, 27 Nov 2024 14:17:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4AREHTDo014443; Wed, 27 Nov 2024 14:17:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AREHTKe014440; Wed, 27 Nov 2024 14:17:29 GMT (envelope-from git) Date: Wed, 27 Nov 2024 14:17:29 GMT Message-Id: <202411271417.4AREHTKe014440@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Justin Hibbits Subject: git: 09ef538713e0 - main - uart: Add primitive noise filtering on RX List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhibbits X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 09ef538713e08a98584dbcf66ff80290cc1b41a2 Auto-Submitted: auto-generated The branch main has been updated by jhibbits: URL: https://cgit.FreeBSD.org/src/commit/?id=09ef538713e08a98584dbcf66ff80290cc1b41a2 commit 09ef538713e08a98584dbcf66ff80290cc1b41a2 Author: Justin Hibbits AuthorDate: 2024-11-20 17:08:26 +0000 Commit: Justin Hibbits CommitDate: 2024-11-27 14:17:09 +0000 uart: Add primitive noise filtering on RX A long cable attached to the UART can act as an antenna if disconnected from the other end. This can cause noise on the receive side, possibly as reflections from the transmit side, leading to an interrupt storm. Filter this by adding a threshold of received characters without TX ready, above which characters are dropped. This is disabled by default, but has been tested with a threshold of 1000+. A high threshold is recommended to avoid dropping characters during, for instance, a large copy/paste from the other end. Sponsored by: Juniper Networks, Inc. --- sys/dev/uart/uart_dev_ns8250.c | 17 +++++++++++++++++ sys/dev/uart/uart_dev_ns8250.h | 1 + 2 files changed, 18 insertions(+) diff --git a/sys/dev/uart/uart_dev_ns8250.c b/sys/dev/uart/uart_dev_ns8250.c index 3da2e8b8c758..1df39b499539 100644 --- a/sys/dev/uart/uart_dev_ns8250.c +++ b/sys/dev/uart/uart_dev_ns8250.c @@ -77,6 +77,11 @@ static int broken_txfifo = 0; SYSCTL_INT(_hw, OID_AUTO, broken_txfifo, CTLFLAG_RWTUN, &broken_txfifo, 0, "UART FIFO has QEMU emulation bug"); +static int uart_noise_threshold = 0; +SYSCTL_INT(_hw, OID_AUTO, uart_noise_threshold, CTLFLAG_RWTUN, + &uart_noise_threshold, 0, + "Number of UART RX interrupts where TX is not ready, before data is discarded"); + /* * To use early printf on x86, add the following to your kernel config: * @@ -1012,6 +1017,7 @@ int ns8250_bus_receive(struct uart_softc *sc) { struct uart_bas *bas; + struct ns8250_softc *ns8250 = (struct ns8250_softc *)sc; int xc; uint8_t lsr; @@ -1023,6 +1029,17 @@ ns8250_bus_receive(struct uart_softc *sc) sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN; break; } + /* Filter out possible noise on the line. + * Expect that the device should be able to transmit as well as + * receive, so if we receive too many characters before transmit + * is ready, it's probably noise. + */ + if ((lsr & (LSR_TXRDY | LSR_TEMT)) == 0 && + uart_noise_threshold > 0) { + if (++ns8250->noise_count >= uart_noise_threshold) + break; + } else + ns8250->noise_count = 0; xc = uart_getreg(bas, REG_DATA); if (lsr & LSR_FE) xc |= UART_STAT_FRAMERR; diff --git a/sys/dev/uart/uart_dev_ns8250.h b/sys/dev/uart/uart_dev_ns8250.h index 324ff72f6e5d..e8a17e96c268 100644 --- a/sys/dev/uart/uart_dev_ns8250.h +++ b/sys/dev/uart/uart_dev_ns8250.h @@ -41,6 +41,7 @@ struct ns8250_softc { uint8_t ier_mask; uint8_t ier_rxbits; uint8_t busy_detect; + int noise_count; }; extern struct uart_ops uart_ns8250_ops;