Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp758908img; Thu, 21 Mar 2019 08:20:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqw+LO4/XqLnamU/MSEPy2GTgbHqHD/x0nC37w4rumcFhIyYtAJvwHRp2q8/lkCqjCHyQgO3 X-Received: by 2002:a65:4384:: with SMTP id m4mr3782875pgp.375.1553181651746; Thu, 21 Mar 2019 08:20:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553181651; cv=none; d=google.com; s=arc-20160816; b=yxMf2mu2eUhsGWj6WGGw+kc7fFiEKkVezH0dNbsNidhTplsFAnXIHqjoPfl3tA+LgU FMtT6ELYN38/+uanu7UqJ+C20FQ51IYGsjw4l8i7bx+BaJdO5v6RY/HwNKUtfQFLkwaC NyIpXpE2arW6REMVqnY9C3mSKvkcJo6yVTyJvws24whe8X57z6xi2hvZimF0Vt0uTYrt DBTyQlc4ZtqWb54Axx9VMIXxYHpjk494enM8a9KF52qlRf6tI6cdxnwi8bNmBp3ZGcTt Im7bWAFWidDoRFAlIm/bfewP/iULiyx8yBh3UNO0KRuKRyKCz3zZiHdB6xWARLY1HPhH wenQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=28yzvLx8CWbN2oAbjieDKxIuRJUsi8Fc56fdxzsiWzs=; b=QZ3MsthnJE8SyOGK1uJdTKy7lznijm356Q9GLLKAZOSIW6EJDD4mf/NyuIhGdUhIhr 4K7I+GzIFk6EQP3TcCxShlw7HQSRXzE9mMZwktVr2ZWognePL1u/ZxU1leBDpiQPzp8h 6B8O977FI2HdbqpNS/Q37BcLqgRBNps3PvaKRfPx63MbJRqyJfiLIw5BucxSlod9im5r f4MkBYyFUDNf7Pv0ZnNy6RGTQR7X5zB9f7lkh1OXQwAn/3dqbbM1h2McNF+4GQ6qF6Aa 0pUE9JnBhJk4srX9/oXbowXPSPXB5VWgw5MGXfJJSYx/5GDGsm3Bn/GTqcvDPwieQsfo fieA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=bR4mJhvS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p2si4166384pfd.257.2019.03.21.08.20.35; Thu, 21 Mar 2019 08:20:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=bR4mJhvS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728439AbfCUPSd (ORCPT + 99 others); Thu, 21 Mar 2019 11:18:33 -0400 Received: from mail-qt1-f196.google.com ([209.85.160.196]:41854 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728131AbfCUPSd (ORCPT ); Thu, 21 Mar 2019 11:18:33 -0400 Received: by mail-qt1-f196.google.com with SMTP id w30so6995964qta.8 for ; Thu, 21 Mar 2019 08:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=28yzvLx8CWbN2oAbjieDKxIuRJUsi8Fc56fdxzsiWzs=; b=bR4mJhvSf3Bij2y6EO1cfxCfUeD/fXIPWrFrsBDqsIf7vtcDH7rVz8GAoZ2seuvzpn Kv+4/QXIgWuuFZJztTRd84fOuiPIZ2r2zI+A0T77yH+HiESBPsHxyDxE7BNEXGGHa9W0 T7dJ9o6aMcNeg+0btQji2xZXAhAKQYcgY9uuJNm5J4fuf1LSEvh9CT/ohKLTUSI56DUE N1qM2Z3+9IiCKYLCirxHHmA0My1xBLlepbSWFEBIwJjXP/twKmXuUvuDoF3XD/Q+16QO AfjrCUvapKRrUl/z1MbeGHbv79YJh9EAaMMwzqZtApP+XiMePhrPx7zuasgSPUijPEzK RYrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=28yzvLx8CWbN2oAbjieDKxIuRJUsi8Fc56fdxzsiWzs=; b=UQDWEl15Vow6s8yV+TcifCZsrYcCJOxKFZzPltMNDkCsS+X2P2DZZyHAbJNbTHBUar PAqct+p9pzV+YHH2OaSuVFgIuTZTun9U42HS/LWfOoJ8hjbzUZ5K1GjMiFnMJT/waFAP LY95UHeOSX6hubsCi9nOxcSbYujEg1N4gdLLJhFDgA9eNK36+bAQVG0khVUezqRUnEYT x/SQEbBmNVuFd+lTmK+L1IlKZSPnfUMuqacnG2gCAkIIO72F93aYUh8ZsV9CoDrMQLwY jpYGVvZOHMwQmWmhNE4W+l4g50SoO4bhKsf0j8acqxUYXiQEIy7NkyB2cMmeDCxp4vJU oYjA== X-Gm-Message-State: APjAAAUPzNowuWCME02OVqMO8cDRDctLEwyZdWdGnGqLEXlyoZRFBi/f /apizOKEWvzSNIbBTAsU7rGY2mu7 X-Received: by 2002:ac8:2539:: with SMTP id 54mr3397294qtm.387.1553181511338; Thu, 21 Mar 2019 08:18:31 -0700 (PDT) Received: from [192.168.0.153] (modemcable235.181-200-24.mc.videotron.ca. [24.200.181.235]) by smtp.gmail.com with ESMTPSA id t55sm3423826qtt.57.2019.03.21.08.18.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Mar 2019 08:18:30 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.2 \(3445.102.3\)) Subject: [PATCH v2] w1: ds2408: reset on output_write retry with readback From: Jean-Francois Dagenais In-Reply-To: <20190318092737.8170-2-manio@skyboo.net> Date: Thu, 21 Mar 2019 11:18:27 -0400 Cc: Greg Kroah-Hartman , Mariusz Bialonczyk Content-Transfer-Encoding: quoted-printable Message-Id: <10244730-36A8-4DAC-95E5-AD7BDEA32F43@gmail.com> References: <20190318092737.8170-1-manio@skyboo.net> <20190318092737.8170-2-manio@skyboo.net> To: open list , Evgeniy Polyakov X-Mailer: Apple Mail (2.3445.102.3) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Originally Reported-by: Mariusz Bialonczyk When we have success in 'Channel Access Write' but reading back latch states fails, a write is retried without doing a proper slave reset. This leads to protocol errors as the slave treats the next 'Channel Access Write' as the continuation of previous command. This commit is fixing this by making sure if the retry loop re-runs, a reset is performed, whatever the failure (CONFIRM_BYTE or the read back). The loop was quite due for a cleanup and this change mandated it. By isolating the CONFIG_W1_SLAVE_DS2408_READBACK case into it's own function, we vastly reduce the visual and branching(runtime and compile-time) noise. Tested-by: Mariusz Bialonczyk Signed-off-by: Jean-Francois Dagenais --- drivers/w1/slaves/w1_ds2408.c | 76 = ++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/drivers/w1/slaves/w1_ds2408.c = b/drivers/w1/slaves/w1_ds2408.c index b535d5ec35b6..92e8f0755b9a 100644 --- a/drivers/w1/slaves/w1_ds2408.c +++ b/drivers/w1/slaves/w1_ds2408.c @@ -138,14 +138,37 @@ static ssize_t status_control_read(struct file = *filp, struct kobject *kobj, W1_F29_REG_CONTROL_AND_STATUS, buf); } =20 +#ifdef fCONFIG_W1_SLAVE_DS2408_READBACK +static bool optional_read_back_valid(struct w1_slave *sl, u8 expected) +{ + u8 w1_buf[3]; + + if (w1_reset_resume_command(sl->master)) + return false; + + w1_buf[0] =3D W1_F29_FUNC_READ_PIO_REGS; + w1_buf[1] =3D W1_F29_REG_OUTPUT_LATCH_STATE; + w1_buf[2] =3D 0; + + w1_write_block(sl->master, w1_buf, 3); + + return (w1_read_8(sl->master) =3D=3D expected); +} +#else +static bool optional_read_back_valid(struct w1_slave *sl, u8 expected) +{ + return true; +} +#endif + static ssize_t output_write(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { struct w1_slave *sl =3D kobj_to_w1_slave(kobj); u8 w1_buf[3]; - u8 readBack; unsigned int retries =3D W1_F29_RETRIES; + ssize_t bytes_written =3D -EIO; =20 if (count !=3D 1 || off !=3D 0) return -EFAULT; @@ -155,54 +178,33 @@ static ssize_t output_write(struct file *filp, = struct kobject *kobj, dev_dbg(&sl->dev, "mutex locked"); =20 if (w1_reset_select_slave(sl)) - goto error; + goto out; =20 - while (retries--) { + do { w1_buf[0] =3D W1_F29_FUNC_CHANN_ACCESS_WRITE; w1_buf[1] =3D *buf; w1_buf[2] =3D ~(*buf); - w1_write_block(sl->master, w1_buf, 3); =20 - readBack =3D w1_read_8(sl->master); + w1_write_block(sl->master, w1_buf, 3); =20 - if (readBack !=3D W1_F29_SUCCESS_CONFIRM_BYTE) { - if (w1_reset_resume_command(sl->master)) - goto error; - /* try again, the slave is ready for a command = */ - continue; + if (w1_read_8(sl->master) =3D=3D = W1_F29_SUCCESS_CONFIRM_BYTE && + optional_read_back_valid(sl, *buf)) { + bytes_written =3D 1; + goto out; } =20 -#ifdef CONFIG_W1_SLAVE_DS2408_READBACK - /* here the master could read another byte which - would be the PIO reg (the actual pin logic state) - since in this driver we don't know which pins are - in and outs, there's no value to read the state and - compare. with (*buf) so end this command abruptly: */ if (w1_reset_resume_command(sl->master)) - goto error; + goto out; /* unrecoverable error */ + /* try again, the slave is ready for a command */ + } while (--retries); =20 - /* go read back the output latches */ - /* (the direct effect of the write above) */ - w1_buf[0] =3D W1_F29_FUNC_READ_PIO_REGS; - w1_buf[1] =3D W1_F29_REG_OUTPUT_LATCH_STATE; - w1_buf[2] =3D 0; - w1_write_block(sl->master, w1_buf, 3); - /* read the result of the READ_PIO_REGS command */ - if (w1_read_8(sl->master) =3D=3D *buf) -#endif - { - /* success! */ - mutex_unlock(&sl->master->bus_mutex); - dev_dbg(&sl->dev, - "mutex unlocked, retries:%d", retries); - return 1; - } - } -error: +out: mutex_unlock(&sl->master->bus_mutex); - dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", = retries); =20 - return -EIO; + dev_dbg(&sl->dev, "%s, mutex unlocked retries:%d\n", + (bytes_written > 0) ? "succeeded" : "error", retries); + + return bytes_written; } =20 =20 --=20 2.11.0=