Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2817775imm; Sat, 12 May 2018 21:31:07 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpkbEMw1q2JLKBdBrPR3nWm4emvxTRHwtfsfNLixB6Zjer0VFQMuDue6NGZ4WC4hH7nAMM0 X-Received: by 2002:a17:902:a986:: with SMTP id bh6-v6mr4762561plb.245.1526185867092; Sat, 12 May 2018 21:31:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526185867; cv=none; d=google.com; s=arc-20160816; b=QZtJdsdIKdjlZrwJ0Rm0KTG1DCx+PSrR6+HGmCsoxSs7KmJuBFSoJYdZEL4uT/oTBL XkiyXqHE0pfgfepcU0AEUDrMjGrjyQqkTkXy7VmDxZIRyHDw57P4rLYpRjKtfPuLP4ig S2hSiIKNVsvz1Rc0gsyBbYqPc+O+ZnEhAfZpCw1BQL80arVrFQjnrKdmSKOaUHhcfHWu Gqs4+3zv6etcND1cnWj4aagqpzHBMKMUrPMYNhGRF4SHvNZtC4x6LHRYKMOYafr/0p4E +C5fbsXva7effFk+NdxV5JSzYB4gCRHnh52H+iC2aNTzLDJBVttfmabGa2GlDHfc0qNj pwtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:message-id:date:thread-index:thread-topic:subject :cc:to:from:dkim-signature:arc-authentication-results; bh=p2yO71TF63YxIW7RBfmlvruZw6w8f13mla/ADD/6eb0=; b=dnStT3u5LRRgWq0oboqig10BAqLox0XhizlMy1pae50o1J45HJ8z9esqzImV0MSmP8 dNZN5U1uvDSKvoFqOAeAlcWDjaI0VGlLnh08iTGLAQz1vhGrH4skwqFRa8pruWav4CIa ZO3dRFEKg5L+OVh6gTcswoewxO2gErCZSMrrMzQE0aCSBZeSF6OILVh3ENQB2m3T1z/7 bzdwanhhQUf1ds6WvD8GcByb52wPsJWFrTXsa3i66UuXj52m8awB+KM40r2Wna1Nr8eR dJtfRlwZpX6nTkSHAKx9HVM/bmv78W6ODgVVV6HkATnpB2sUO3bR2Rs0JcQCegaPpjjS 3apw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nokia.onmicrosoft.com header.s=selector1-nokia-com header.b=bXXtf1Xk; 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 v16-v6si4504885pgo.135.2018.05.12.21.30.17; Sat, 12 May 2018 21:31:07 -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=bXXtf1Xk; 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 S1750949AbeEMEaK (ORCPT + 99 others); Sun, 13 May 2018 00:30:10 -0400 Received: from mail-eopbgr00126.outbound.protection.outlook.com ([40.107.0.126]:55417 "EHLO EUR02-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750850AbeEMEaI (ORCPT ); Sun, 13 May 2018 00:30:08 -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=p2yO71TF63YxIW7RBfmlvruZw6w8f13mla/ADD/6eb0=; b=bXXtf1Xk/AawEkyypbYf6HKDDLBuELF/MD6WNgFcK7tsvOfupT9sRILetLwZ3EPQUAUqpv0e+EXKf33ssceYYCyr9pLpLshIiMHz/3rEIy0wPPjGUnb+cdN+i81Dh/N02nxwyw3HVDgXGCIAVHQr7gmx+834tJBVcJJYlgZPsVc= Received: from VI1PR07MB1615.eurprd07.prod.outlook.com (10.166.142.145) by VI1PR07MB0895.eurprd07.prod.outlook.com (10.161.108.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.755.13; Sun, 13 May 2018 04:30:03 +0000 Received: from VI1PR07MB1615.eurprd07.prod.outlook.com ([fe80::f086:ea8b:4f87:b505]) by VI1PR07MB1615.eurprd07.prod.outlook.com ([fe80::f086:ea8b:4f87:b505%7]) with mapi id 15.20.0776.007; Sun, 13 May 2018 04:30:02 +0000 From: "Wan, Jane (Nokia - US/Sunnyvale)" 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" , "Bos, Ties (Nokia - US/Sunnyvale)" , "Wan, Jane (Nokia - US/Sunnyvale)" Subject: [PATCH v7] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter Thread-Topic: [PATCH v7] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter Thread-Index: AdPqcw1t262DjgFHSjKF174DiCyylw== Date: Sun, 13 May 2018 04:30:02 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=jane.wan@nokia.com; x-originating-ip: [2601:646:8281:8310:f08a:db2e:32f8:9890] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;VI1PR07MB0895;7:WMgCNLlD0YS2Y0jfzgdFYsfO7C1l0EUxYxYwq/mr6sTEvnB/1DDNN83PgOISw3u7wjix6wxl/wSw8qbFNlO1n5ENidWC8JjjTXzm4tvhyLqviLB15Elx9YdQro0spwgELIXM2vMZEYAFDEmFkEFe6+JaDKa02O7Hmk4e0j+RXjIDwGIVekrpkCgV0vPf9v+6XnBYRl646R1SFRfcXTOR06/6FllEdV9eSqxzmx9dLl/JINx3jUfQ50PaCArof3sb x-ms-exchange-antispam-srfa-diagnostics: SOS;SOR; x-forefront-antispam-report: SFV:SKI;SCL:-1;SFV:NSPM;SFS:(10019020)(396003)(39860400002)(366004)(376002)(346002)(39380400002)(199004)(189003)(102836004)(53936002)(8936002)(3280700002)(7736002)(8656006)(99286004)(478600001)(97736004)(68736007)(8676002)(2906002)(81166006)(59450400001)(6506007)(6116002)(107886003)(7696005)(54906003)(110136005)(316002)(186003)(81156014)(6436002)(5660300001)(2900100001)(3660700001)(2201001)(4326008)(305945005)(74316002)(5250100002)(86362001)(7416002)(9686003)(46003)(55016002)(106356001)(2501003)(39060400002)(25786009)(33656002)(486006)(14454004)(105586002)(476003)(921003)(1121003);DIR:OUT;SFP:1102;SCL:1;SRVR:VI1PR07MB0895;H:VI1PR07MB1615.eurprd07.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:(109105607167333);BCL:0;PCL:0;RULEID:(7020095)(4652020)(8989080)(4534165)(4627221)(201703031133081)(201702281549075)(8990040)(5600026)(48565401081)(2017052603328)(7193020);SRVR:VI1PR07MB0895; x-ms-traffictypediagnostic: VI1PR07MB0895: 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)(8121501046)(5005006)(3002001)(93006095)(93001095)(10201501046)(3231254)(11241501184)(806099)(944501410)(52105095)(6055026)(149027)(150027)(6041310)(20161123562045)(20161123560045)(20161123564045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:VI1PR07MB0895;BCL:0;PCL:0;RULEID:;SRVR:VI1PR07MB0895; x-forefront-prvs: 0671F32598 received-spf: None (protection.outlook.com: nokia.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: 75EAPG8K2ilvUrb10+RZMpelpUgjsK6FJha5YWH0I9xBoKomqpcOaPNnhVgf2riQDWrA8fCJLLEsb4LIpXwz7+M9iPhPJF611HNC8vl9bG6M+Dy5NStCmGi46Wz9v2lD7AhgzN19vLsSDXc/dXKOSQnKr0QpPjdhgF1RwKC5qyj253yF5e8bOzaHKuE2hgrsRDhB6VMEKvXPjKFqxkmyiofndbFjcOvtrnVLUwqoGvs= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 9f23d892-530e-4432-79af-08d5b88a31db X-OriginatorOrg: nokia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9f23d892-530e-4432-79af-08d5b88a31db X-MS-Exchange-CrossTenant-originalarrivaltime: 13 May 2018 04:30:02.2716 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 5d471751-9675-428d-917b-70f44f9630b0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR07MB0895 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 pa= rameter pages from the parameter page copies present. Signed-off-by: Jane Wan --- v7: change debug print messages v6: support the cases that srcbufs are not contiguous v5: make the bit-wise majority functon generic v4: move the bit-wise majority code in a separate function v3: fix warning message detected by kbuild test robot v2: rebase the changes on top of v4.17-rc1 =20 drivers/mtd/nand/raw/nand_base.c | 50 ++++++++++++++++++++++++++++++++++-= --- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_b= ase.c index 72f3a89..b43b784 100644 --- a/drivers/mtd/nand/raw/nand_base.c +++ b/drivers/mtd/nand/raw/nand_base.c @@ -5087,6 +5087,35 @@ static int nand_flash_detect_ext_param_page(struct n= and_chip *chip, } =20 /* + * Recover data with bit-wise majority + */ +static void nand_bit_wise_majority(const void **srcbufs, + unsigned int nsrcbufs, + void *dstbuf, + unsigned int bufsize) +{ + int i, j, k; + + for (i =3D 0; i < bufsize; i++) { + u8 cnt, val; + + val =3D 0; + for (j =3D 0; j < 8; j++) { + cnt =3D 0; + for (k =3D 0; k < nsrcbufs; k++) { + const u8 *srcbuf =3D srcbufs[k]; + + if (srcbuf[i] & BIT(j)) + cnt++; + } + if (cnt > nsrcbufs / 2) + val |=3D BIT(j); + } + ((u8 *)dstbuf)[i] =3D val; + } +} + +/* * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwi= se. */ static int nand_flash_detect_onfi(struct nand_chip *chip) @@ -5102,7 +5131,7 @@ static int nand_flash_detect_onfi(struct nand_chip *c= hip) return 0; =20 /* ONFI chip: allocate a buffer to hold its parameter page */ - p =3D kzalloc(sizeof(*p), GFP_KERNEL); + p =3D kzalloc((sizeof(*p) * 3), GFP_KERNEL); if (!p) return -ENOMEM; =20 @@ -5113,21 +5142,32 @@ static int nand_flash_detect_onfi(struct nand_chip = *chip) } =20 for (i =3D 0; i < 3; i++) { - ret =3D nand_read_data_op(chip, p, sizeof(*p), true); + ret =3D nand_read_data_op(chip, &p[i], sizeof(*p), true); if (ret) { ret =3D 0; goto free_onfi_param_page; } =20 - if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) =3D=3D + if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) =3D=3D le16_to_cpu(p->crc)) { + if (i) + memcpy(p, &p[i], sizeof(*p)); break; } } =20 if (i =3D=3D 3) { - pr_err("Could not find valid ONFI parameter page; aborting\n"); - goto free_onfi_param_page; + const void *srcbufs[3] =3D {p, p + 1, p + 2}; + + pr_warn("Could not find a valid ONFI parameter page, trying bit-wise maj= ority to recover it\n"); + nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p, + sizeof(*p)); + + if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=3D + le16_to_cpu(p->crc)) { + pr_err("ONFI parameter recovery failed, aborting\n"); + goto free_onfi_param_page; + } } =20 /* Check version */ --=20 1.7.9.5