From nobody Fri Dec 17 07:31:50 2021 X-Original-To: dev-commits-src-branches@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 CB37619044EF; Fri, 17 Dec 2021 07:31:52 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JFgfH64VZz3KY3; Fri, 17 Dec 2021 07:31:51 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 01AE517B2D; Fri, 17 Dec 2021 07:31:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BH7VoQY045985; Fri, 17 Dec 2021 07:31:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BH7VoSJ045984; Fri, 17 Dec 2021 07:31:50 GMT (envelope-from git) Date: Fri, 17 Dec 2021 07:31:50 GMT Message-Id: <202112170731.1BH7VoSJ045984@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andriy Gapon Subject: git: 3fab2b02d69b - stable/12 - twsi: make data receiving code safer List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: avg X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 3fab2b02d69b17ef049c71a5ebc3013cd8fc771d Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639726312; 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=oP97Zj2bMTNPdsetWq/RrqQu6xFoa/P7WpNqNff6Eg0=; b=KayvoxK2QuRAl7j0f+B+B0+pBPz6vAX33UOhfUDYp2OBCPGAE9cD8p1nPbM+011qE2gXCY fxgBTuwsh2HXJntECXudBxyt3JiZYauKUxLySIXJmsnpQqgL+5hNxxg9aMvhE0qy1Nn6Tw uFQJDor2odkU76gEob8od+E7vn3PiLC8aHgNZKC9oQx7B/o0ePj8y8LuKw0mi5JPGDRd5y jKBoDCmeEowFDxQtAnYxXjW0Ef0U064hjOxbk4zQ1nANTRSAXSRQ9A7BCypER60aQ7Opop e36FAYbKm3FXVTVDUgljrjUEJyU5X7WsVQvNvmbHs3cKgviGWezpyUEUObrmqg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639726312; a=rsa-sha256; cv=none; b=IgtPvcYJf9IFmurXF/rUfgRrYhsO4+teu/eLwJ5t8uvkUuQOQmHMt1QdlmvMQV813cyK/v GFMRywzPHsD+KpROSA1foGsDsOuJ7ANRv2x8J33byxT8A4iDdIb5ab4BjEpIVImRpQpz2w ZR6KkWjpv33hrSOaDbT7Ovv3ExoMW+ZDjSIht9tg/nP6kn441aefLGMtOTkZcNNnRqP/v8 fEGRhHVXhd/APrugY2B2CIQhTfSOTDtltkpjBmPf4AaGk0ZPpCU/4scXQS5FAB9kLDyWJq 3AFEL4AwGtEPA2JM4n9laS3s8m0EzW0Pxcwk2K/mbNsMx/GVF784J18pF9fATQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by avg: URL: https://cgit.FreeBSD.org/src/commit/?id=3fab2b02d69b17ef049c71a5ebc3013cd8fc771d commit 3fab2b02d69b17ef049c71a5ebc3013cd8fc771d Author: Andriy Gapon AuthorDate: 2021-11-26 08:34:42 +0000 Commit: Andriy Gapon CommitDate: 2021-12-17 07:31:32 +0000 twsi: make data receiving code safer Assert that we are not receiving data beyond the requested length. Assert that we have not NACK-ed incoming data prematurely. Abort the current transfer if the incoming data is NACK-ed or not NACK-ed unexpectedly. Add debug logging of received data to complement logging of sent data. (cherry picked from commit 00c07d9559c6197957b00811a0b29876a5c8b573) --- sys/dev/iicbus/twsi/twsi.c | 62 ++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/sys/dev/iicbus/twsi/twsi.c b/sys/dev/iicbus/twsi/twsi.c index b94396883be5..2f77e2dc69c3 100644 --- a/sys/dev/iicbus/twsi/twsi.c +++ b/sys/dev/iicbus/twsi/twsi.c @@ -671,37 +671,57 @@ twsi_intr(void *arg) break; case TWSI_STATUS_DATA_RD_ACK: - debugf(sc, "Ack received after receiving data\n"); - sc->msgs[sc->msg_idx].buf[sc->recv_bytes++] = TWSI_READ(sc, sc->reg_data); - debugf(sc, "msg_len=%d recv_bytes=%d\n", sc->msgs[sc->msg_idx].len, sc->recv_bytes); + debugf(sc, "Received and ACK-ed data\n"); + KASSERT(sc->recv_bytes < sc->msgs[sc->msg_idx].len, + ("receiving beyond the end of buffer")); + + sc->msgs[sc->msg_idx].buf[sc->recv_bytes] = + TWSI_READ(sc, sc->reg_data); + debugf(sc, "Received byte %d (of %d) = 0x%x\n", + sc->recv_bytes, + sc->msgs[sc->msg_idx].len, + sc->msgs[sc->msg_idx].buf[sc->recv_bytes]); + sc->recv_bytes++; /* If we only have one byte left, disable ACK */ - if (sc->msgs[sc->msg_idx].len - sc->recv_bytes == 1) + if (sc->msgs[sc->msg_idx].len - sc->recv_bytes == 1) { sc->control_val &= ~TWSI_CONTROL_ACK; - if (sc->msgs[sc->msg_idx].len == sc->recv_bytes) { - debugf(sc, "Done with msg %d\n", sc->msg_idx); - sc->msg_idx++; - if (sc->msg_idx == sc->nmsgs - 1) { - debugf(sc, "No more msgs\n"); - transfer_done = 1; - sc->error = 0; - } + } else if (sc->msgs[sc->msg_idx].len == sc->recv_bytes) { + /* + * We should not have ACK-ed the last byte. + * The protocol state machine is in invalid state. + */ + debugf(sc, "RX all but asked for more?\n"); + twsi_error(sc, IIC_ESTATUS); } - TWSI_WRITE(sc, sc->reg_control, sc->control_val); break; case TWSI_STATUS_DATA_RD_NOACK: - if (sc->msgs[sc->msg_idx].len - sc->recv_bytes == 1) { - sc->msgs[sc->msg_idx].buf[sc->recv_bytes++] = TWSI_READ(sc, sc->reg_data); - debugf(sc, "Done RX data, send stop (2)\n"); - if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTOP)) + debugf(sc, "Received and NACK-ed data\n"); + KASSERT(sc->recv_bytes == sc->msgs[sc->msg_idx].len - 1, + ("sent NACK before receiving all requested data")); + sc->msgs[sc->msg_idx].buf[sc->recv_bytes] = + TWSI_READ(sc, sc->reg_data); + debugf(sc, "Received byte %d (of %d) = 0x%x\n", + sc->recv_bytes, + sc->msgs[sc->msg_idx].len, + sc->msgs[sc->msg_idx].buf[sc->recv_bytes]); + sc->recv_bytes++; + + if (sc->msgs[sc->msg_idx].len == sc->recv_bytes) { + debugf(sc, "Done RX data\n"); + if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTOP)) { + debugf(sc, "Send STOP\n"); TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_STOP); + } } else { - debugf(sc, "No ack when receiving data, sending stop anyway\n"); - if (!(sc->msgs[sc->msg_idx].flags & IIC_M_NOSTOP)) - TWSI_WRITE(sc, sc->reg_control, - sc->control_val | TWSI_CONTROL_STOP); + /* + * We should not have NACK-ed yet. + * The protocol state machine is in invalid state. + */ + debugf(sc, "NACK-ed before receving all bytes?\n"); + twsi_error(sc, IIC_ESTATUS); } sc->transfer = 0; transfer_done = 1;