From patchwork Wed Jul 18 21:50:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 945933 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bootlin.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="m+hwmvBz"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41W9pZ5VcCz9s3x for ; Thu, 19 Jul 2018 07:51:50 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=pPfosjtQN0ortQQ6EwwQBiRwK4MhesugWpYNaisUuU8=; b=m+hwmvBztrdRYXVRYw5rlLwV4g 7PgNYw1MgQSPrFN2j+xM9mr0MOYFB+bhwzEUDNLJE3ifwtv5VoVVTOYjvPHfTKIyBPjOFEAxAwdb2 ep/MgE/2fbRGeOtHsswl4P5kp+5KRCwi1DgN3ItkfIk16kf3yRfAn8e9iqcCH5Ai5wk2EW86BvFvK TFEan+Z49Adq8zrwzDWVPN6pC3RLh5RkRSntu3YmF1K6+2NeDjUi29ge1oaak0PWmSrdIhBaxIcTb 8uhjv/gXpCqezY3AvKVua3fpBRRq4datNImHsjaVncNlUnDnK5wje4O2ia7Xl949OSi9NaFObeBTe xAii3fQQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1ffuMJ-0006e9-2v; Wed, 18 Jul 2018 21:51:43 +0000 Received: from mail.bootlin.com ([62.4.15.54]) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1ffuM8-0006Tz-C7 for linux-mtd@lists.infradead.org; Wed, 18 Jul 2018 21:51:34 +0000 Received: by mail.bootlin.com (Postfix, from userid 110) id 45B7E20935; Wed, 18 Jul 2018 23:51:21 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost.localdomain (unknown [91.224.148.103]) by mail.bootlin.com (Postfix) with ESMTPSA id B6BE820799; Wed, 18 Jul 2018 23:50:59 +0200 (CEST) From: Miquel Raynal To: Boris Brezillon , Richard Weinberger , David Woodhouse , Brian Norris , Marek Vasut Subject: [PATCH v2 2/2] mtd: rawnand: add hooks that may be called during nand_scan() Date: Wed, 18 Jul 2018 23:50:56 +0200 Message-Id: <20180718215056.1242-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180718215056.1242-1-miquel.raynal@bootlin.com> References: <20180718215056.1242-1-miquel.raynal@bootlin.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180718_145132_711062_BADD8FCA X-CRM114-Status: GOOD ( 20.97 ) X-Spam-Score: -0.0 (/) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [62.4.15.54 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-mtd@lists.infradead.org, Miquel Raynal MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org In order to remove the limitation that forbids dynamic allocation in nand_scan_ident(), we must create a path that will be the same for all controller drivers. The idea is to use nand_scan() instead of the widely used nand_scan_ident()/nand_scan_tail() couple. In order to achieve this, controller drivers will need to adjust some parameters between these two functions depending on the NAND chip wired on them. For that, a hook called ->attach_chip() is created as part of the nand_controller_ops structure, itself embedded in the nand_controller structure. The later may be referenced by two ways: 1/ if the driver does not implement its own controller, the chip->controller hook is not populated before nand_scan() so it cannot be dereferenced: use chip->dummy_controller instead (which is statically allocated and will be referenced later by chip->controller anyway). 2/ through chip->controller if the driver implements its own controller. Another hook, ->detach_chip() is also introduced in order to clean the controller driver's potential allocations in case of failure of nand_scan_tail(). There is no need for the controller driver to call the ->detach_chip() hook directly upon error after a successful nand_scan(). In this situation, calling nand_release() as before is enough. Signed-off-by: Miquel Raynal Acked-by: Boris Brezillon --- drivers/mtd/nand/raw/nand_base.c | 32 ++++++++++++++++++++++++++++++-- include/linux/mtd/rawnand.h | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c index 0aef0299f4f2..a64a344bf7a9 100644 --- a/drivers/mtd/nand/raw/nand_base.c +++ b/drivers/mtd/nand/raw/nand_base.c @@ -6697,6 +6697,20 @@ EXPORT_SYMBOL(nand_scan_tail); is_module_text_address((unsigned long)__builtin_return_address(0)) #endif +static int nand_attach(struct nand_chip *chip) +{ + if (chip->controller->ops && chip->controller->ops->attach_chip) + return chip->controller->ops->attach_chip(chip); + + return 0; +} + +static void nand_detach(struct nand_chip *chip) +{ + if (chip->controller->ops && chip->controller->ops->detach_chip) + chip->controller->ops->detach_chip(chip); +} + /** * nand_scan_with_ids - [NAND Interface] Scan for the NAND device * @mtd: MTD device structure @@ -6710,11 +6724,21 @@ EXPORT_SYMBOL(nand_scan_tail); int nand_scan_with_ids(struct mtd_info *mtd, int maxchips, struct nand_flash_dev *ids) { + struct nand_chip *chip = mtd_to_nand(mtd); int ret; ret = nand_scan_ident(mtd, maxchips, ids); - if (!ret) - ret = nand_scan_tail(mtd); + if (ret) + return ret; + + ret = nand_attach(chip); + if (ret) + return ret; + + ret = nand_scan_tail(mtd); + if (ret) + nand_detach(chip); + return ret; } EXPORT_SYMBOL(nand_scan_with_ids); @@ -6742,7 +6766,11 @@ void nand_cleanup(struct nand_chip *chip) /* Free manufacturer priv data. */ nand_manufacturer_cleanup(chip); + + /* Free controller specific allocations after chip identification */ + nand_detach(chip); } + EXPORT_SYMBOL_GPL(nand_cleanup); /** diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h index eb5a053fd06b..d046b3a61205 100644 --- a/include/linux/mtd/rawnand.h +++ b/include/linux/mtd/rawnand.h @@ -509,6 +509,19 @@ struct nand_id { int len; }; +/** + * struct nand_controller_ops - Controller operations + * + * @attach_chip: Callback that may be called between nand_detect() and + * nand_scan_tail() during nand_scan() (optional). + * @detach_chip: Callback that may be called if nand_scan_tail() fails + * (optional). + */ +struct nand_controller_ops { + int (*attach_chip)(struct nand_chip *chip); + void (*detach_chip)(struct nand_chip *chip); +}; + /** * struct nand_controller - Control structure for hardware controller * shared among independent devices @@ -517,11 +530,13 @@ struct nand_id { * @wq: wait queue to sleep on if a NAND operation is in * progress used instead of the per chip wait queue * when a hw controller is available. + * @ops: NAND controller operations. */ struct nand_controller { spinlock_t lock; struct nand_chip *active; wait_queue_head_t wq; + const struct nand_controller_ops *ops; }; static inline void nand_controller_init(struct nand_controller *nfc) @@ -529,6 +544,7 @@ static inline void nand_controller_init(struct nand_controller *nfc) nfc->active = NULL; spin_lock_init(&nfc->lock); init_waitqueue_head(&nfc->wq); + nfc->ops = NULL; } /**