From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10C64C433FE for ; Tue, 5 Oct 2021 13:58:10 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8710B61074 for ; Tue, 5 Oct 2021 13:58:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8710B61074 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B90D7831D8; Tue, 5 Oct 2021 15:57:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="PSAEXBXq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 13AB983124; Tue, 5 Oct 2021 15:56:55 +0200 (CEST) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7273C831FD for ; Tue, 5 Oct 2021 15:56:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 1C23161989; Tue, 5 Oct 2021 13:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1633442192; bh=h3GZsX2DKqdHpAuWk8ZdsXs/zUx9L5hnU0zvppeXcLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PSAEXBXqzNdD028MyjDuPeCD8E+KDvKGJlVdnF2FBUqWBtgm4TJCNwRVDsrJn7oFY MzwlzBpqnWcFdWJjzSFXXmQf9KcqYXJlMlEd7UcBob5VilJhHtMGeyRyQmgIBr/elQ fQJak6cXGOO4+XcvYJNIV7E/8Y9Kkg+AF4pNmIfKkV6eNzUfzudUxGYE69e2nuIZCw aAiz3vaWh6RXmTADu1R6nm5Vek2ADH4e4XkvPbdOznhE6iVzC+MTxNxc3BrmATb7Pd FRpfl0ndeHewNcIc/0TFFPWsQPDXxzVLfJqiJ57bmTYPC7Su+BXAo6vml2uNsWqAia wWG6sTvg9QxjA== From: =?UTF-8?q?Marek=20Beh=C3=BAn?= To: Jagan Teki , Tom Rini Cc: u-boot@lists.denx.de, Patrick Delaunay , =?UTF-8?q?Pali=20Roh=C3=A1r?= , Patrice Chotard , Marek Vasut , Pratyush Yadav , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Simon Glass , Masami Hiramatsu Subject: [PATCH u-boot-spi v3 09/10] mtd: mtdpart: Make mtdpart's _erase method sane Date: Tue, 5 Oct 2021 15:56:05 +0200 Message-Id: <20211005135606.2246-10-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211005135606.2246-1-kabel@kernel.org> References: <20211005135606.2246-1-kabel@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek BehĂșn The _erase() method of the mtdpart driver, part_erase(), currently implements offset shifting (for given mtdpart partition) in a weird way: 1. part_erase() adds partition offset to block address 2. parent driver's _erase() method is called 3. parent driver's _erase() method calls mtd_erase_callback() 4. mtd_erase_callback() subtracts partition offset from block address so that the callback function is given correct address The problem here is that if the parent's driver does not call mtd_erase_callback() in some scenario (this was recently a case for spi_nor_erase(), which did not call mtd_erase_callback() at all), the offset is not shifted back. Moreover the code would be more readable if part_erase() not only added partition offset before calling parent's _erase(), but also subtracted it back afterwards. Currently the mtd_erase_callback() is expected to do this subtracting since it does have to do it anyway. Add the more steps to this procedure: 5. mtd_erase_callback() adds partition offset to block address so that it returns the the erase_info structure members as it received them 6. part_erase() subtracts partition offset from block address This makes the code more logical and also prevents errors in case parent's driver does not call mtd_erase_callback() for some reason. (BTW, the purpose of mtd_erase_callback() in Linux is to inform the caller that it is done, since in Linux erasing is done asynchronously. We are abusing the purpose of mtd_erase_callback() in U-Boot for completely different purpose. The callback function itself has empty implementation in all cases in U-Boot.) Signed-off-by: Marek BehĂșn Reviewed-by: Simon Glass Tested-by: Masami Hiramatsu --- drivers/mtd/mtdpart.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c index aa58f722da..6ab481a7b1 100644 --- a/drivers/mtd/mtdpart.c +++ b/drivers/mtd/mtdpart.c @@ -446,24 +446,34 @@ static int part_erase(struct mtd_info *mtd, struct erase_info *instr) int ret; instr->addr += mtd->offset; + ret = mtd->parent->_erase(mtd->parent, instr); - if (ret) { - if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) - instr->fail_addr -= mtd->offset; - instr->addr -= mtd->offset; - } + if (ret && instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) + instr->fail_addr -= mtd->offset; + + instr->addr -= mtd->offset; + return ret; } void mtd_erase_callback(struct erase_info *instr) { - if (instr->mtd->_erase == part_erase) { + if (!instr->callback) + return; + + if (instr->mtd->_erase == part_erase && instr->len) { if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) instr->fail_addr -= instr->mtd->offset; instr->addr -= instr->mtd->offset; } - if (instr->callback) - instr->callback(instr); + + instr->callback(instr); + + if (instr->mtd->_erase == part_erase && instr->len) { + if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) + instr->fail_addr += instr->mtd->offset; + instr->addr += instr->mtd->offset; + } } EXPORT_SYMBOL_GPL(mtd_erase_callback); -- 2.32.0