Received: by 10.192.165.148 with SMTP id m20csp3283490imm; Mon, 7 May 2018 09:35:29 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpIHiM9O1zR/Hw0UL3pt4bBUpI7KOO1vUryAH/ZpuWH/8pJICnDjFsjBwtXLrU/nyg8IKXH X-Received: by 2002:a24:e9c1:: with SMTP id f184-v6mr2001296ith.79.1525710929217; Mon, 07 May 2018 09:35:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525710929; cv=none; d=google.com; s=arc-20160816; b=YpEqlCz+EWol3XHMj3LTyPR+QdUSa2PMm81n7UKcZoHINShDQL7Wdi65kanG0RsfFn B3JPhfxaVaQZeFiOWppgVrvOUuKaTgduwbdhLV7/VxW+xsZxAcUheFSQBFVbzpdiJjQc 6LssCcZVPkxas57FKZUeVVffHvBW4GQXq5uYN6VT9G/PcDD4K2BywxAWPsu58Cze3abM lSGzUwjLfEHeZxx+mkXrbdcLkLbox9GOHVpQidlKrgS7axnnYHM6mORXbswo98cSfcPB 4hh5qwwze53+8pHnOBkxbOYc4B/0vWRxMBJNs7hL6L45PVk2Zi91HFF2CrNRIiScshU5 TEVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:message-id:date:subject:cc:to :from:dkim-signature:arc-authentication-results; bh=o27BG2oryWhyYV4X0AHopJs2DLX6NcqJGHUwbX9Omv4=; b=zkt2Hc9C8hvhLgjVW3YD2kzzD7DFJfzmbUiALf9Q+7w6bg+8B81Slu7kTPeLqtXuTW AjDha7+ubQ/cYOchxQwKPZAieJ3gT+8wap7Vu4GFFFjBmh0qzAkJlka2n3AB+qgGqZkR Fflh5gMQkw9ebreI/IAnV1uGxEyX1ymf8hCLYxVDQ8hBW0sC7NrHS7YDYr0QSsrIqN3n /JqOtLVbqMsysqudFUWeC1EmO+SBWgYIX/utUnhVfKGfF0MvgdNV3Dv1vHdZd8Lx1h5s fBFxosfNXy+7Px+ik03+RemikCXH9f+2zmwt6Xss9RNnUwvcQ9TIuwkNUmEMltJ57nPU NAIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nokia.onmicrosoft.com header.s=selector1-nokia-com header.b=IDwAL5x8; 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=fail (p=NONE sp=NONE dis=NONE) header.from=nokia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b8-v6si7845969itc.120.2018.05.07.09.35.15; Mon, 07 May 2018 09:35:29 -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=@nokia.onmicrosoft.com header.s=selector1-nokia-com header.b=IDwAL5x8; 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=fail (p=NONE sp=NONE dis=NONE) header.from=nokia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752657AbeEGQet (ORCPT + 99 others); Mon, 7 May 2018 12:34:49 -0400 Received: from mail-he1eur01on0105.outbound.protection.outlook.com ([104.47.0.105]:10878 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752529AbeEGQek (ORCPT ); Mon, 7 May 2018 12:34:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nokia.onmicrosoft.com; s=selector1-nokia-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=o27BG2oryWhyYV4X0AHopJs2DLX6NcqJGHUwbX9Omv4=; b=IDwAL5x8cKnxJTJbS5589PJqd+p44KuLEJjVp6aK1yNajIYsrYb7jvJ6y+15JcIEOcqahs4h1cIUTUyOjGnofheZOKeZ3kbF93euqPTcYdYCaqoDjYi7h0H5bbSlCTPsBpy5FjhENhviFCWiUE0HH3M00+qiMWz6Ui04Y3AdNM8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=jane.wan@nokia.com; Received: from cobuntu-jwan.gainspeed.nsn-rdnet.net (131.228.32.160) by AM4PR07MB1603.eurprd07.prod.outlook.com (2603:10a6:200:25::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.755.10; Mon, 7 May 2018 16:34:33 +0000 From: Jane Wan To: Boris.Brezillon@bootlin.com, miquel.raynal@bootlin.com, dwmw2@infradead.org, computersforpeace@gmail.com, richard@nod.at, marek.vasut@gmail.com, yamada.masahiro@socionext.com, prabhakar.kushwaha@nxp.com, shawnguo@kernel.org, jagdish.gediya@nxp.com, shreeya.patel23498@gmail.com Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, ties.bos@nokia.com, Jane Wan Subject: [PATCH v3 2/2] mtd: rawnand: fsl_ifc: use bit-wise majority to recover the contents of ONFI parameter Date: Mon, 7 May 2018 09:34:15 -0700 Message-Id: <1525710855-8159-1-git-send-email-Jane.Wan@nokia.com> X-Mailer: git-send-email 1.7.9.5 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [131.228.32.160] X-ClientProxiedBy: HE1PR0202CA0036.eurprd02.prod.outlook.com (2603:10a6:3:e4::22) To AM4PR07MB1603.eurprd07.prod.outlook.com (2603:10a6:200:25::17) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:(109105607167333);BCL:0;PCL:0;RULEID:(7020095)(4652020)(8989080)(5600026)(4534165)(4627221)(201703031133081)(201702281549075)(8990040)(48565401081)(2017052603328)(7193020);SRVR:AM4PR07MB1603; X-Microsoft-Exchange-Diagnostics: 1;AM4PR07MB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VRVqTBEkXuUstSKABTXRhXCsC1tob0qmc/wUIydrICwRMcQJneNTHpYgHgGLnxQVf6J1oEJAR/VTn+tmh4+92LOUame5uVmv3yFUmbZjc8643U/3YhzBmgB1eQaIAx7cxUoIIgHJazOFE59mUpPXgj13+a1drrNVi03EGmZHW4uc4U+938RJZnK/nIsmZ+V7imV7wkC5XQSouPUOE7s2AcHZ/ftV4bkIfscQq6f0m7rPd108HBYA2LN9zE3gY/EutL3o3BcANjaSfq0i1jYWMQWfX1PySDVKh9kff8qbEwjBwarCZeGgGvYyIVn+4flxKHdLx37+2Br9qhT8L9QkwyxRS7ah/JMF6vQqXyXgGsFHIDXl7Gyx19/C9s9m4LF6rGOPNkdBjnHyJ6+i;4:qwuZcCe0dUgwiqitehaVaesVLaknuioqRMmnGxXXAB/z2wpbjJePUdlFSbbGuwTlvius5/vDsHfEJwlSasyZzLEbACSVe1yIlUHhXFgP50rY2JXCZOIoPszhL9d0ABo4yEx6buabQLLx8YcJdPB8dh6JufwUq2Xe5hDw/lZuOtATtFuDLBgaFv49XiweCjLWTtvScszSBaef5sY6GwLE3HCaVBfdTtf2NfXsPPDO6djHwvrzQI7p7wdkTg99ZfCL2SjsLt6Ql53O6SKxFvNjNRhKU7Yj/CTP7JXNGCcHntdzThROkr8UvILUxROxxWrmmFTfvmUV7NgWDlSvVTDb0geErjoMqyPpu1mW2eWeMWY= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(82608151540597)(109105607167333); X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040522)(2401047)(5005006)(8121501046)(3002001)(3231254)(11241501184)(806099)(944501410)(52105095)(10201501046)(93006095)(93001095)(6055026)(149027)(150027)(6041310)(20161123562045)(20161123564045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123560045)(6072148)(201708071742011);SRVR:AM4PR07MB1603;BCL:0;PCL:0;RULEID:;SRVR:AM4PR07MB1603; X-Forefront-PRVS: 066517B35B X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(366004)(396003)(346002)(39860400002)(39380400002)(376002)(189003)(199004)(25786009)(305945005)(5660300001)(26005)(186003)(16526019)(59450400001)(6506007)(386003)(7416002)(53936002)(478600001)(52116002)(36756003)(6486002)(66066001)(3846002)(6116002)(47776003)(8656006)(6512007)(51416003)(107886003)(68736007)(316002)(105586002)(106356001)(16586007)(86362001)(48376002)(39060400002)(4326008)(50466002)(97736004)(7736002)(2906002)(6666003)(4720700003)(8676002)(50226002)(2616005)(956004)(476003)(81166006)(8936002)(81156014)(486006)(921003)(1121003);DIR:OUT;SFP:1102;SCL:1;SRVR:AM4PR07MB1603;H:cobuntu-jwan.gainspeed.nsn-rdnet.net;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: nokia.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;AM4PR07MB1603;23:Fwd7Dpi5WxfL2MU2++vlnvMnDxoft49MgrzGppHBn?= =?us-ascii?Q?snpBZubJIfTCVQGb39RsceZAfA2ytv9/wFwO0lx2fw4cq4Adawv5x/iLsr6V?= =?us-ascii?Q?jeO6+S98a61XjyXW2aS5DVOIG849aCbL4b5aDCrn+d6JiYBMllUWoMVeRnNJ?= =?us-ascii?Q?qY0hSbRyCb+hdcpDIlPb92Pa/KkXPIvz47wIG38bBUQ0T8lM+v/3gqJVMH98?= =?us-ascii?Q?rAKcsxP5v2r8t1Mp/kg3TfGagS4euudS4ZTIR54cSkvFhnE40AIIGOwg37wh?= =?us-ascii?Q?446o3K/Md9sFHgoqTQeck48y1Yuk57XHxZ9etGacMvCDRKy6FnTs0T0Jdqaw?= =?us-ascii?Q?Dq3i1hIAP27HAybD9mQtHrPUZiwusBfB5052MwWi/DP09KhYoleGB+5KCDRc?= =?us-ascii?Q?5nOi9g6R5f+07Ts8D5zNamcaKvPlzm90Nh3EM8PuVjocOJqd2DeCUzDfr2O2?= =?us-ascii?Q?S+xLPcC5SAV0404SCsp0waorfLn/5lr5SscSHqprgDb8rILgPjIMN5E7QMtj?= =?us-ascii?Q?lLSIZDNwJ4bygkWe63O0/PeaKDTkjG+yQiLOKpX8LRX4AKaccZEls5cjDA0m?= =?us-ascii?Q?W4xGHWoc574R6LyH+z4Q2KjY0Fwf1iq/WmUAYhwVDHJDz3iaEsjCOG6bsbRt?= =?us-ascii?Q?xBGZHmwPZ1YCcqeElgkQ65S1JIMAWBXBlU4XbBFzAV5SxjNVSsUFDkrFIAq8?= =?us-ascii?Q?IZEWFGrSGZbAABl5uApgmGEu82bE7Me37nUU6hbUNopvUeU30ytfSnIBdraK?= =?us-ascii?Q?AHG114Sn2t9VhZY2GHIoHqC/DZRG0pOTFqgtRFv3vlxT0ZZK1m66O/xpUSka?= =?us-ascii?Q?WeDhdCDbja3KhLuVt3B9FSz43SMopKVsPzHEmIaUCRS6uVARkXQf023PcMez?= =?us-ascii?Q?U9Dgj+soVTsfB8WELomeF+SUNdr1jRuW1Ez0Mpn95XzPYds8Hi6aI6tYTOXW?= =?us-ascii?Q?6qkDrHQsypetRprsei3KA5d8G61r5A0+PqKEquyqKf7hrJCl75rkHOuoeMYO?= =?us-ascii?Q?O5sty2T3eigDQdhy6oBqwlmh1u8qY9BZBhIlWK3NL1T1QZMIb0xvAFR9t6n5?= =?us-ascii?Q?F0nhJx3nCxQqP6yAmDjW11Y2M7ZdDAgLWEym3nMkMgXijEImKOucRJYzGPMR?= =?us-ascii?Q?bRiAlEtZA1ljBwj57dXR3pMxldtS31eedr8BuaQMQiyXQ3JdE+aQV0+emXfd?= =?us-ascii?Q?xjyc/RWv4UT53o2R73CJomtsPkcVE34+Dei6aXJVKmu4DFFs55o71RTlVsVt?= =?us-ascii?Q?Ar9MPgMw6uVr8wNPuk/4JM2Q+dXacwJWrPstLef?= X-Microsoft-Antispam-Message-Info: KPGuAUzrbpeixv77u8JHqE2Whj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: 1;AM4PR07MB1603;7:nu+LgsKqdbJDLNaU6/JoX7p4qk2ju/ZSlbiDeW8qlDWBIoDBqnNkbdfZKYauasnNG4aNlwtooTCk4T1ls6j1uyzSZhFR48tEB4CmIFSi8AgbZAuDgNUu1SfXTn7vl5v1j/R0hPFqUwWhW3NtnPIJbVVe568dfjNpPdWhEFLiTRkM+QOm2wzG/qs8TrAKQhjr6dBAGwQGl9UpcYbuyRMgPmJoWuP8AWZ0s05SXMJdfsOiIqmT9Kka19KK6f3OVHNU X-MS-Office365-Filtering-Correlation-Id: 7a8d0ab5-78fc-4276-2e6a-08d5b4386c3b X-OriginatorOrg: nokia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 May 2018 16:34:33.1288 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7a8d0ab5-78fc-4276-2e6a-08d5b4386c3b X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 5d471751-9675-428d-917b-70f44f9630b0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR07MB1603 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Per ONFI specification (Rev. 4.0), if all parameter pages have invalid CRC values, the bit-wise majority may be used to recover the contents of the parameter pages from the parameter page copies present. Signed-off-by: Jane Wan --- drivers/mtd/nand/raw/nand_base.c | 41 ++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c index 72f3a89..48f2dec 100644 --- a/drivers/mtd/nand/raw/nand_base.c +++ b/drivers/mtd/nand/raw/nand_base.c @@ -5086,15 +5086,18 @@ static int nand_flash_detect_ext_param_page(struct nand_chip *chip, return ret; } +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01) + /* * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. */ static int nand_flash_detect_onfi(struct nand_chip *chip) { struct mtd_info *mtd = nand_to_mtd(chip); - struct nand_onfi_params *p; + struct nand_onfi_params *p = NULL; char id[4]; - int i, ret, val; + int i, ret, val, pagesize; + u8 *buf = NULL; /* Try ONFI for unknown chip or LP */ ret = nand_readid_op(chip, 0x20, id, sizeof(id)); @@ -5102,8 +5105,9 @@ static int nand_flash_detect_onfi(struct nand_chip *chip) return 0; /* ONFI chip: allocate a buffer to hold its parameter page */ - p = kzalloc(sizeof(*p), GFP_KERNEL); - if (!p) + pagesize = sizeof(*p); + buf = kzalloc((pagesize * 3), GFP_KERNEL); + if (!buf) return -ENOMEM; ret = nand_read_param_page_op(chip, 0, NULL, 0); @@ -5113,7 +5117,8 @@ static int nand_flash_detect_onfi(struct nand_chip *chip) } for (i = 0; i < 3; i++) { - ret = nand_read_data_op(chip, p, sizeof(*p), true); + p = (struct nand_onfi_params *)&buf[i*pagesize]; + ret = nand_read_data_op(chip, p, pagesize, true); if (ret) { ret = 0; goto free_onfi_param_page; @@ -5126,8 +5131,27 @@ static int nand_flash_detect_onfi(struct nand_chip *chip) } if (i == 3) { - pr_err("Could not find valid ONFI parameter page; aborting\n"); - goto free_onfi_param_page; + int j, k, l; + u8 v, m; + + pr_err("Could not find valid ONFI parameter page\n"); + pr_info("Recover ONFI params with bit-wise majority\n"); + for (j = 0; j < pagesize; j++) { + v = 0; + for (k = 0; k < 8; k++) { + m = 0; + for (l = 0; l < 3; l++) + m += GET_BIT(k, buf[l*pagesize + j]); + if (m > 1) + v |= BIT(k); + } + ((u8 *)p)[j] = v; + } + if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) != + le16_to_cpu(p->crc)) { + pr_err("ONFI parameter recovery failed, aborting\n"); + goto free_onfi_param_page; + } } /* Check version */ @@ -5220,7 +5244,8 @@ static int nand_flash_detect_onfi(struct nand_chip *chip) sizeof(p->vendor)); free_onfi_param_page: - kfree(p); + if (buf != NULL) + kfree(buf); return ret; } -- 1.7.9.5