From patchwork Sat Nov 7 10:16:01 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Hilliard X-Patchwork-Id: 3937 Return-Path: X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mxe881.netcup.net (Postfix) with ESMTPS id AAFC01C52A3 for ; Sat, 7 Nov 2026 11:48:08 +0100 (CET) Authentication-Results: mxe881; dkim=pass header.d=gmail.com; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-27320-noreply=patchwork.local@lists.linux.dev smtp.helo=sea.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 172.234.253.10 as permitted sender) client-ip=172.234.253.10; envelope-from=linux-sunxi+bounces-27320-noreply=patchwork.local@lists.linux.dev; helo=sea.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 3C3DF4676F for ; Sat, 7 Nov 2026 10:17:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794046599; b=pkkAqD3cz0tCLso1dvGOpcdnLki/xuHr9nbyYMD/enKp6NoRvFToptqLIA4TZvoU/7pB GXYuxnmeSKX4ss6g18hS56LUQ6GEIbel0uqAXsCWzl7y84Qro93vQrM9ggCFnlXBq/l2E ycLEHm31+Lp5vzG50TEt63DUX6jH57Rz4/tuDqQFQZfNB/KQl8MQDJ+fc6MdDunsrp0v1 R2J5RnqrtuKU/GEOhAXA3TeQwv3YsTFudpqkFUEkL9inRNWMDr8nQlBqe20y4zxgX4H/v PekyaSbuZK57kSl1IOW2eY1DwhaHU8Hw0m4Lhg2brUZ4l5qFRg15vbev3UbAwI9rrhA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794046599; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature: X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State: X-Gm-Gg:X-Received:Received:From:Date:Subject:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=0zdj0JMmNNgvImUjXW+dc1czDRoUybgPmc34A31iLmc=; b=BOB+ZRHfGaFk5z0mgTA9qqHAxKPBfH8h+McH8DxTunuW1BHxnwtcbBPAoD4sMeIFFr/A aW4nCrhI84MrbZ6eGqfgthiYpW2jRLv7iRQ1r+Fesr+Y4Hb54l7hlDEaykidiqJqckVHl e/8A/7V0TuXzb16a3BKjEvSm6Y3/mDTq7qC2l0R+tUsE+EEYLHquJNQYawda5nJW33OW4 p3wqsmsee/qS21Zd2cnvg54QZL1hApI7i0pt15udwtho6PxijPb+1JDyc9X/Y6Ag/u5K/ pHPgamfyh/zHm2mjQb2POgt2KvGKr4IhHtPbtRYHVWtCiS06asrrQTfbD8oEA8nxh3w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="rIip/0qm"; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3FA2D606EEA4; Sat, 7 Nov 2026 10:16:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rIip/0qm X-Original-To: linux-sunxi@lists.linux.dev Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794046599; b=KqOrx3SrhfdpsCkGGSLEtOIWBQ+vH5ywdoOEcLu20ARciPl/01/SHA5csB56d9+aGPrI ExsilCkhOd94ffoXoCsx65SGnYg6+RVl2z0KIh/LSr/VmlAPv/ANLrD86hdZ5nEchiUcx 4SmOrH0UcFc3cA25Zk7DOcVboMto9beZBpCsC40iMBqDzG9sFmo3BnkmBj8/Uzj/S25dK /vSA+0cK/BfixB09/ofDVSoT2CItZZ4dABUv/5n8eBurFdCyuZWqi9a4+vp+miJuHQQTv i0dg2nGQnyNS1BfpjDw8F74BUx6bJrv3LxTE/z/3xpehNod4SXtS0gAKmkWAvU5NsWw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794046599; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=0zdj0JMmNNgvImUjXW+dc1czDRoUybgPmc34A31iLmc=; b=bcteXjG92HhEOL4Kd074vbhtEba3NYPaIdAUcPQLntN/DIduFkRWjTfHUMjch8BBAcCW cyD6BDSlUUhGwMzswt/d/3hQ+8dhPTdzOirwZu8xrBQzoSVcPnNYX3sWjxe+gcxZQ1MX2 1HGS2+JJwWTu5JksUaolEWMfmTAquDU00nfqMxHSdKUCdZGTmz78LdkYC5lQaPjXgU8po rVOt2bwTrPaBOiuG+fM6Bd4A/xbXcrf7n2yaRy/63ZrHhpkq81aWgH8ZQOmtDRHpFU3Dj WMflmundb6Eouu4kjjs0FkM8O4tBIZjKMbSm6m6+elRlVx/paHsBa4Q5vBC6ciHs7Pg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="rIip/0qm"; arc=none smtp.remote-ip=209.85.210.49 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48BDB606EE9D for ; Sat, 7 Nov 2026 10:16:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 48BDB606EE9D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-7f48c750afcso835979a34.0 for ; Sat, 07 Nov 2026 02:16:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794046597; x=1794651397; darn=lists.linux.dev; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=0zdj0JMmNNgvImUjXW+dc1czDRoUybgPmc34A31iLmc=; b=rIip/0qmyZA1ES8y/azugMbG/SINTpYGZlvFJgQElZALAJcZPZh+2xGsuzPLMyCfcd HAaTa2V1oWcagaIdmwh+GPTy/NX+ZndEWl1SGlAfX0UrhbDkJ9CUC54WFrHCvetvwRK+ 7fzrdv4+nwJRPNOhYo2AwJRv5ljRn+d00XIWz7lOUQHZg0kNzKeq65kZCpOq/mkqSed+ 0p3Ur+0DabJi4o5rHtwp+ogO56yrNme5zICCPG7yhhUZOBQd+/VIZAWHul/yKrNOSJka TMrJrXdXKFEeXS0Zzh7V7fbiK2xWHZY1IwsKAiylobe7d0mqFuS1wQE+SF5SQNpNj/In nUFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794046597; x=1794651397; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=0zdj0JMmNNgvImUjXW+dc1czDRoUybgPmc34A31iLmc=; b=RcGppibV7E6Y58qYjwJuWcPyKH88r6C26OaC/5ssVUQfM8ELRhf+fEwHb5HtjE09J3 IaoQTHv2v8OpGbBo65/8mazNCys02zIxWiuf/Xgt/ytQXl0jM7eXJyg+24I8Atas4uYK RgEGqlviNkKKIkJiBAn4+DwyduyBBxhfY0jWHZ8tTF1JNXNTGA5j3vWR1/+kwMhQa7iW jULNBFABuVuQY4X/b7PoKkSTXwRPuWeBx9WXVl/2qYzYhny6qC+PPXXWY3FpsSuT2enZ ag4pS/Sve+wLq1zIChp0WUy2YXZmdmYGsHPAHe7rX9fAkCSRnL1VpIu2it5y28tPEuoJ XwtQ== X-Forwarded-Encrypted: i=1; AKwUvBxywyprbhBP/wW4CbXXZL3L1X1iT//KeuXhk46mn13sGrdvCbgn554wiej9vDX1nuoQgMKgKH+hiecBZA==@lists.linux.dev X-Gm-Message-State: APuU4Z+zj4cwz2GQ+OLuIM1pxGyqeBWgp+uY2cEyKnkHNAu5qIu47TR+ i7M7+5+wf9lYsdzDEy8UQ0eSvmZRWCtUKis/7Zvfkk7Rwao/Xz/eOONE X-Gm-Gg: AYBFou2gmaF/ogjUen3gW1xsZsIq0jaaOIBpq/5qQbrZ6LAyflO03mXPgpXnBPfrFvM GGBlaXDYXrQvnvI0vQi57hFXyc2p2B2i4h9uBYTarvIyHHjJG5V751dWe3nq4FWXsakSsG4YCBp uzoxuX8T8Vq5tUNK4ezZIGGMW9ylzNuLYAI/mto3NSTiUQDwEMDZGzCieeEyiyY8v7oAjatfWvP cZzy08+BuHKAB+x6wB3KlDhOF/mfNu5gDsiEnwuDnsRH3TM4v9ytFS4KwdULQK4/cGgueD3hA4O j5U0a8n5SQuc+sR96OLMOsvgHkcJa3JNAb+3qRHGawhqjrpd7/8XSR0F6i6ydedNBY1vDSDtPrN 7XOaaUk3JXumwMDSaRY34ngyBwKMTC82OylZxY7nMliLyTHNIhnLtNNXpcUjOksHtTr8aCwn66S 3QzaMtmi1jY4pmowvhhdVPaAhSyR/hmolZF3lSsoDd5BGxs0wwfM7Lcz50D1yyQcMlaCsBenW8J MZBUqlFdZoELNSSivl5nyvk5ni/nv3jcpVfoZuFx1vdBgsJDtpgGULXTZXEYa7ydsGvafIbxZYE di/gnIELsw0imx2SG122FtLxFnLgWFMsj812yi2G6QGGjgLdmZ0UN3HQwRHA/c5ZdHEXXo7UUXZ hnmN3EUKul+HuRhl+W66H0L4xuTtCAZPRAuMmMHsen6+YnyVVSs4MHSqMBo9wkMrBl1MN5B5UrM b6CUrTmo375ZENJOnRfyYe4ftwmUpwnPJ7zefsATrOAu9dl/bR7JJh X-Received: by 2002:a05:6830:6809:b0:85e:ef69:238a with SMTP id 46e09a7af769-85f1cfce0famr5153719a34.11.1794046596984; Sat, 07 Nov 2026 02:16:36 -0800 (PST) Received: from [127.0.1.1] (174-29-1-49.hlrn.qwest.net. [174.29.1.49]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-85f1dbef224sm3990269a34.12.2026.11.07.02.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Nov 2026 02:16:36 -0800 (PST) From: James Hilliard Date: Sat, 07 Nov 2026 03:16:01 -0700 Subject: [PATCH 04/58] mtd: rawnand: separate hardware disconnection from cleanup X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Message-Id: <20261107-submit-rawnand-retained-handle-lifetime-v1-v1-4-0181ef0cc4c8@gmail.com> References: <20261107-submit-rawnand-retained-handle-lifetime-v1-v1-0-0181ef0cc4c8@gmail.com> In-Reply-To: <20261107-submit-rawnand-retained-handle-lifetime-v1-v1-0-0181ef0cc4c8@gmail.com> To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Brian Norris , Boris Brezillon , Arseniy Krasnov , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Thomas Gleixner , Vitaly Wool , David Woodhouse , Russell King , David Woodhouse , Mike Rapoport , Maxime Coquelin , Alexandre Torgue , Christophe Kerello , Jason Roberts , Michal Simek , Boris Brezillon , Mason Yang , Ramuthevar Vadivel Murugan , Stefan Agner , Lucas Stach , Thierry Reding , Jonathan Hunter , Dmitry Osipenko , Matthias Brugger , AngeloGioacchino Del Regno , Jorge Ramirez-Ortiz , Heiko Stuebner , Yifeng Zhao , Nicolas Ferre , Alexandre Belloni , Claudiu Beznea , Keguang Zhang , Jacky Huang , Shan-Chun Hung , Frank Li , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Han Xu , Liang Yang , Neil Armstrong , Kevin Hilman , Jerome Brunet , Martin Blumenstingl , Manivannan Sadhasivam , Kamal Dasu , Broadcom internal kernel review list , Vladimir Zapolskiy , Piotr Wojtaszczyk , Paul Cercueil , Harvey Hunt , Maxim Levitsky , Michael Walle , Srinivas Kandagatla Cc: Tze Yee Ng , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-mips@vger.kernel.org, imx@lists.linux.dev, linux-renesas-soc@vger.kernel.org, linux-amlogic@lists.infradead.org, linux-arm-msm@vger.kernel.org, James Hilliard X-Mailer: b4 0.15.2 X-Rspamd-Server: rspamd-worker-8404 X-Spamd-Result: default: False [4.31 / 15.00]; RBL_SENDERSCORE(2.00)[172.234.253.10:from]; SUSPICIOUS_RECIPS(1.50)[]; DMARC_POLICY_SOFTFAIL(1.00)[gmail.com : SPF not aligned (relaxed), No valid DKIM,none]; MAILLIST(-0.18)[generic]; BAD_REP_POLICIES(0.10)[]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc20261015:i=2]; RCVD_COUNT_FIVE(0.00)[6]; FROM_HAS_DN(0.00)[]; TAGGED_RCPT(0.00)[kernel]; RCVD_VIA_SMTP_AUTH(0.00)[]; FREEMAIL_CC(0.00)[altera.com,lists.infradead.org,vger.kernel.org,lists.linux.dev,gmail.com]; DBL_BLOCKED_OPENRESOLVER(0.00)[sea.lore.kernel.org:helo,sea.lore.kernel.org:rdns]; FORGED_SENDER_MAILLIST(0.00)[]; FROM_NEQ_ENVFROM(0.00)[jameshilliard1@gmail.com,linux-sunxi@lists.linux.dev]; ASN(0.00)[asn:63949, ipnet:172.234.224.0/19, country:SG]; MID_RHS_MATCH_FROM(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; R_SPF_ALLOW(0.00)[+ip4:172.234.253.10]; TO_DN_SOME(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[209.85.210.49:received,174.29.1.49:received,100.77.114.92:received]; TAGGED_FROM(0.00)[bounces-27320-noreply=patchwork.local]; RCPT_COUNT_GT_50(0.00)[73]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; FREEMAIL_TO(0.00)[bootlin.com,nod.at,ti.com,gmail.com,kernel.org,sberdevices.ru,sholland.org,infradead.org,arm.linux.org.uk,intel.com,foss.st.com,st.com,amd.com,collabora.com,mxic.com.tw,linux.intel.com,agner.ch,lynxeye.de,nvidia.com,oss.qualcomm.com,sntech.de,rock-chips.com,microchip.com,tuxon.dev,nuvoton.com,nxp.com,pengutronix.de,amlogic.com,linaro.org,baylibre.com,googlemail.com,broadcom.com,mleia.com,timesys.com,crapouillou.net,walle.cc]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[172.234.253.10:from] X-Rspamd-Queue-Id: AAFC01C52A3 X-MORS-Enabled: yes X-MORS-DOMAIN: patchwork.local X-MORS-HOSTING: hosting172546 X-MORS-USER: hosting172546 X-getmail-retrieved-from-mailbox: INBOX A retained MTD handle can still enter NAND operations after its controller is unbound. Closing a writable character device also calls nand_sync(), which takes the chip and controller locks. Freeing their storage during remove is therefore unsafe even without another explicit read or write. Add nand_disconnect() to serialize disconnection with regular operations under the chip and controller locks. Make nand_get_device() reject a disconnected chip and propagate that error through its callers. A sync after disconnection becomes a no-op. Suspend and resume also honor the disconnected state. Unregister MTD devices first so removal notifiers can finish their I/O, then disconnect before releasing controller resources. Wake any remaining suspended waiters when disconnecting. Panic writes also check the disconnected flag; their notifier clients must be drained by MTD unregistration before controller resources are released. Keep nand_cleanup() separate. Controllers adopting this interface must retain the chip, controller lock storage and NAND metadata until their last MTD and device references are gone. Signed-off-by: James Hilliard --- drivers/mtd/nand/raw/nand_base.c | 76 +++++++++++++++++++++++++++++++++------- include/linux/mtd/rawnand.h | 9 +++-- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c index a5b278ab9384..8f7d2571e8d9 100644 --- a/drivers/mtd/nand/raw/nand_base.c +++ b/drivers/mtd/nand/raw/nand_base.c @@ -336,21 +336,47 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) * * Lock the device and its controller for exclusive access */ -static void nand_get_device(struct nand_chip *chip) +static int nand_get_device(struct nand_chip *chip) { /* Wait until the device is resumed. */ while (1) { mutex_lock(&chip->lock); + if (chip->disconnected) { + mutex_unlock(&chip->lock); + return -ENODEV; + } if (!chip->suspended) { mutex_lock(&chip->controller->lock); - return; + return 0; } mutex_unlock(&chip->lock); - wait_event(chip->resume_wq, !chip->suspended); + wait_event(chip->resume_wq, !READ_ONCE(chip->suspended) || + READ_ONCE(chip->disconnected)); } } +/** + * nand_disconnect - drain hardware access and reject further operations + * @chip: NAND chip object + * + * Unregister the MTD device first so removal notifiers can finish their I/O. + * Call before releasing controller resources. Operations waiting for resume + * are woken and fail with -ENODEV. + * The chip, controller lock and NAND metadata must remain alive + * until existing MTD handles are released; nand_cleanup() is a separate step. + */ +void nand_disconnect(struct nand_chip *chip) +{ + mutex_lock(&chip->lock); + mutex_lock(&chip->controller->lock); + WRITE_ONCE(chip->disconnected, true); + mutex_unlock(&chip->controller->lock); + mutex_unlock(&chip->lock); + wake_up_all(&chip->resume_wq); +} +EXPORT_SYMBOL_GPL(nand_disconnect); + /** * nand_check_wp - [GENERIC] check if the chip is write protected * @chip: NAND chip object @@ -581,7 +607,9 @@ static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs) nand_erase_nand(chip, &einfo, 0); /* Write bad block marker to OOB */ - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; ret = nand_markbad_bbm(chip, ofs); nand_release_device(chip); @@ -3879,7 +3907,9 @@ static int nand_read_oob(struct mtd_info *mtd, loff_t from, ops->mode != MTD_OPS_RAW) return -ENOTSUPP; - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; old_stats = mtd->ecc_stats; @@ -4449,6 +4479,9 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, struct mtd_oob_ops ops; int ret; + if (READ_ONCE(chip->disconnected)) + return -ENODEV; + nand_select_target(chip, chipnr); /* Wait for the device to get ready */ @@ -4479,7 +4512,9 @@ static int nand_write_oob(struct mtd_info *mtd, loff_t to, ops->retlen = 0; - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; switch (ops->mode) { case MTD_OPS_PLACE_OOB: @@ -4539,7 +4574,9 @@ int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, return -EIO; /* Grab the lock and see if the device is available */ - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; /* Shift to get first page */ page = (int)(instr->addr >> chip->page_shift); @@ -4627,7 +4664,8 @@ static void nand_sync(struct mtd_info *mtd) pr_debug("%s: called\n", __func__); /* Grab the lock and see if the device is available */ - nand_get_device(chip); + if (nand_get_device(chip)) + return; /* Release it and go back */ nand_release_device(chip); } @@ -4644,7 +4682,9 @@ static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) int ret; /* Select the NAND device */ - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; nand_select_target(chip, chipnr); @@ -4688,10 +4728,14 @@ static int nand_suspend(struct mtd_info *mtd) int ret = 0; mutex_lock(&chip->lock); + if (chip->disconnected) { + mutex_unlock(&chip->lock); + return -ENODEV; + } if (chip->ops.suspend) ret = chip->ops.suspend(chip); if (!ret) - chip->suspended = 1; + WRITE_ONCE(chip->suspended, true); mutex_unlock(&chip->lock); return ret; @@ -4706,10 +4750,12 @@ static void nand_resume(struct mtd_info *mtd) struct nand_chip *chip = mtd_to_nand(mtd); scoped_guard(mutex, &chip->lock) { + if (chip->disconnected) + return; if (chip->suspended) { if (chip->ops.resume) chip->ops.resume(chip); - chip->suspended = 0; + WRITE_ONCE(chip->suspended, false); } else { pr_err("%s called for a chip which is not in suspended state\n", __func__); @@ -4743,7 +4789,9 @@ static int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) if (!chip->ops.lock_area) return -ENOTSUPP; - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; ret = chip->ops.lock_area(chip, ofs, len); nand_release_device(chip); @@ -4764,7 +4812,9 @@ static int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) if (!chip->ops.unlock_area) return -ENOTSUPP; - nand_get_device(chip); + ret = nand_get_device(chip); + if (ret) + return ret; ret = chip->ops.unlock_area(chip, ofs, len); nand_release_device(chip); diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h index 5c70e7bd3ed5..90b2169cc745 100644 --- a/include/linux/mtd/rawnand.h +++ b/include/linux/mtd/rawnand.h @@ -1264,9 +1264,10 @@ struct nand_secure_region { * @pagecache.page: Page number currently in the cache. -1 means no page is * currently cached * @buf_align: Minimum buffer alignment required by a platform - * @lock: Lock protecting the suspended field. Also used to serialize accesses - * to the NAND device + * @lock: Lock protecting the suspended and disconnected fields. Also serializes + * accesses to the NAND device * @suspended: Set to 1 when the device is suspended, 0 when it's not + * @disconnected: Controller resources are unavailable; reject hardware access * @resume_wq: wait queue to sleep if rawnand is in suspended state. * @cur_cs: Currently selected target. -1 means no target selected, otherwise we * should always have cur_cs >= 0 && cur_cs < nanddev_ntargets(). @@ -1326,7 +1327,8 @@ struct nand_chip { /* Internals */ struct mutex lock; - unsigned int suspended : 1; + bool suspended; + bool disconnected; wait_queue_head_t resume_wq; int cur_cs; int read_retries; @@ -1595,6 +1597,7 @@ void nand_wait_ready(struct nand_chip *chip); * sucessful nand_scan(). */ void nand_cleanup(struct nand_chip *chip); +void nand_disconnect(struct nand_chip *chip); /* * External helper for controller drivers that have to implement the WAITRDY