hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
 *
 */
 
#include <linux/mtd/bbt_store.h>
#include <linux/slab.h>
 
#ifdef BBT_DEBUG
#define BBT_DBG pr_err
#else
#define BBT_DBG(args...)
#endif
 
struct nanddev_bbt_info {
   u8 pattern[4];
   unsigned int version;
};
 
static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
 
static int nanddev_read_bbt(struct nand_device *nand, u32 block, bool update)
{
   unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
   unsigned int nblocks = nanddev_neraseblocks(nand);
   unsigned int nbytes = DIV_ROUND_UP(nblocks * bits_per_block,
                      BITS_PER_LONG) * sizeof(*nand->bbt.cache);
   struct mtd_info *mtd = nanddev_to_mtd(nand);
   u8 *data_buf, *oob_buf;
   struct nanddev_bbt_info *bbt_info;
   struct mtd_oob_ops ops;
   int bbt_page_num;
   int ret = 0;
   unsigned int version = 0;
 
   if (!nand->bbt.cache)
       return -ENOMEM;
 
   if (block >= nblocks)
       return -EINVAL;
 
   /* Aligned to page size, and even pages is better */
   bbt_page_num = (sizeof(struct nanddev_bbt_info) + nbytes +
       mtd->writesize - 1) >> (ffs(mtd->writesize) - 1);
   bbt_page_num = (bbt_page_num + 1) / 2 * 2;
   data_buf = kzalloc(bbt_page_num * mtd->writesize, GFP_KERNEL);
   if (!data_buf)
       return -ENOMEM;
   oob_buf = kzalloc(bbt_page_num * mtd->oobsize, GFP_KERNEL);
   if (!oob_buf) {
       kfree(data_buf);
 
       return -ENOMEM;
   }
 
   bbt_info = (struct nanddev_bbt_info *)(data_buf + nbytes);
 
   memset(&ops, 0, sizeof(struct mtd_oob_ops));
   ops.mode = MTD_OPS_PLACE_OOB;
   ops.datbuf = data_buf;
   ops.len = bbt_page_num * mtd->writesize;
   ops.oobbuf = oob_buf;
   ops.ooblen = bbt_page_num * mtd->oobsize;
   ops.ooboffs = 0;
 
   ret = mtd_read_oob(mtd, block * mtd->erasesize, &ops);
   if (ret && ret != -EUCLEAN) {
       pr_err("%s fail %d\n", __func__, ret);
       ret = -EIO;
       goto out;
   } else {
       ret = 0;
   }
 
   if (oob_buf[0] != 0xff && !memcmp(bbt_pattern, bbt_info->pattern, 4))
       version = bbt_info->version;
 
   BBT_DBG("read_bbt from blk=%d tag=%d ver=%d\n", block, update, version);
   if (update && version > nand->bbt.version) {
       memcpy(nand->bbt.cache, data_buf, nbytes);
       nand->bbt.version = version;
   }
 
out:
   kfree(oob_buf);
   kfree(data_buf);
 
   return ret < 0 ? -EIO : version;
}
 
static int nanddev_write_bbt(struct nand_device *nand, u32 block)
{
   unsigned int bits_per_block = fls(NAND_BBT_BLOCK_NUM_STATUS);
   unsigned int nblocks = nanddev_neraseblocks(nand);
   unsigned int nbytes = DIV_ROUND_UP(nblocks * bits_per_block,
                      BITS_PER_LONG) * sizeof(*nand->bbt.cache);
   struct mtd_info *mtd = nanddev_to_mtd(nand);
   u8 *data_buf, *oob_buf;
   struct nanddev_bbt_info *bbt_info;
   struct mtd_oob_ops ops;
   int bbt_page_num;
   int ret = 0;
   struct nand_pos pos;
 
   BBT_DBG("write_bbt to blk=%d ver=%d\n", block, nand->bbt.version);
   if (!nand->bbt.cache)
       return -ENOMEM;
 
   if (block >= nblocks)
       return -EINVAL;
 
   /* Aligned to page size, and even pages is better */
   bbt_page_num = (sizeof(struct nanddev_bbt_info) + nbytes +
       mtd->writesize - 1) >> (ffs(mtd->writesize) - 1);
   bbt_page_num = (bbt_page_num + 1) / 2 * 2;
 
   data_buf = kzalloc(bbt_page_num * mtd->writesize, GFP_KERNEL);
   if (!data_buf)
       return -ENOMEM;
   oob_buf = kzalloc(bbt_page_num * mtd->oobsize, GFP_KERNEL);
   if (!oob_buf) {
       kfree(data_buf);
 
       return -ENOMEM;
   }
 
   bbt_info = (struct nanddev_bbt_info *)(data_buf + nbytes);
 
   memcpy(data_buf, nand->bbt.cache, nbytes);
   memcpy(bbt_info, bbt_pattern, 4);
   bbt_info->version = nand->bbt.version;
 
   nanddev_offs_to_pos(nand, block * mtd->erasesize, &pos);
   ret = nand->ops->erase(nand, &pos);
   if (ret)
       goto out;
 
   memset(&ops, 0, sizeof(struct mtd_oob_ops));
   ops.mode = MTD_OPS_PLACE_OOB;
   ops.datbuf = data_buf;
   ops.len = bbt_page_num * mtd->writesize;
   ops.oobbuf = oob_buf;
   ops.ooblen = bbt_page_num * mtd->oobsize;
   ops.ooboffs = 0;
   ret = mtd_write_oob(mtd, block * mtd->erasesize, &ops);
 
out:
   kfree(oob_buf);
   kfree(data_buf);
 
   return ret;
}
 
static int nanddev_bbt_format(struct nand_device *nand)
{
   unsigned int nblocks = nanddev_neraseblocks(nand);
   struct mtd_info *mtd = nanddev_to_mtd(nand);
   struct nand_pos pos;
   u32 start_block, block;
 
   start_block = nblocks - NANDDEV_BBT_SCAN_MAXBLOCKS;
 
   for (block = 0; block < nblocks; block++) {
       nanddev_offs_to_pos(nand, block * mtd->erasesize, &pos);
       if (nanddev_isbad(nand, &pos))
           nanddev_bbt_set_block_status(nand, block,
                            NAND_BBT_BLOCK_FACTORY_BAD);
   }
 
   for (block = 0; block < NANDDEV_BBT_SCAN_MAXBLOCKS; block++) {
       if (nanddev_bbt_get_block_status(nand, start_block + block) ==
           NAND_BBT_BLOCK_GOOD)
           nanddev_bbt_set_block_status(nand, start_block + block,
                            NAND_BBT_BLOCK_WORN);
   }
 
   return 0;
}
 
/**
 * nanddev_scan_bbt_in_flash() - Scan for a BBT in the flash
 * @nand: nand device
 *
 * Scan a bbt in flash, if not exist, format one.
 *
 * Return: 0 in case of success, a negative error code otherwise.
 */
int nanddev_scan_bbt_in_flash(struct nand_device *nand)
{
   unsigned int nblocks = nanddev_neraseblocks(nand);
   u32 start_block, block;
   int ret = 0;
 
   nand->bbt.version = 0;
   start_block = nblocks - NANDDEV_BBT_SCAN_MAXBLOCKS;
   for (block = 0; block < NANDDEV_BBT_SCAN_MAXBLOCKS; block++)
       nanddev_read_bbt(nand, start_block + block, true);
 
   if (nand->bbt.version == 0) {
       nanddev_bbt_format(nand);
       ret = nanddev_bbt_in_flash_update(nand);
       if (ret) {
           nand->bbt.option = 0;
           pr_err("%s fail\n", __func__);
       }
   }
 
   nand->bbt.option |= NANDDEV_BBT_SCANNED;
 
   return ret;
}
EXPORT_SYMBOL_GPL(nanddev_scan_bbt_in_flash);
 
/**
 * nanddev_bbt_in_flash_update() - Update a BBT
 * @nand: nand device
 *
 * Update the BBT to flash.
 *
 * Return: 0 in case of success, a negative error code otherwise.
 */
int nanddev_bbt_in_flash_update(struct nand_device *nand)
{
   if (nand->bbt.option & NANDDEV_BBT_SCANNED) {
       unsigned int nblocks = nanddev_neraseblocks(nand);
       u32 bbt_version[NANDDEV_BBT_SCAN_MAXBLOCKS];
       int start_block, block;
       u32 min_version, block_des;
       int ret, count = 0;
 
       start_block = nblocks - NANDDEV_BBT_SCAN_MAXBLOCKS;
       for (block = 0; block < NANDDEV_BBT_SCAN_MAXBLOCKS; block++) {
           ret = nanddev_bbt_get_block_status(nand, start_block + block);
           if (ret == NAND_BBT_BLOCK_FACTORY_BAD) {
               bbt_version[block] = 0xFFFFFFFF;
               continue;
           }
           ret = nanddev_read_bbt(nand, start_block + block,
                          false);
           if (ret < 0)
               bbt_version[block] = 0xFFFFFFFF;
           else if (ret == 0)
               bbt_version[block] = 0;
           else
               bbt_version[block] = ret;
       }
get_min_ver:
       min_version = 0xFFFFFFFF;
       block_des = 0;
       for (block = 0; block < NANDDEV_BBT_SCAN_MAXBLOCKS; block++) {
           if (bbt_version[block] < min_version) {
               min_version = bbt_version[block];
               block_des = start_block + block;
           }
       }
 
       if (block_des > 0) {
           nand->bbt.version++;
           ret = nanddev_write_bbt(nand, block_des);
           bbt_version[block_des - start_block] = 0xFFFFFFFF;
           if (ret) {
               pr_err("%s blk= %d ret= %d\n", __func__,
                      block_des, ret);
               goto get_min_ver;
           } else {
               count++;
               if (count < 2)
                   goto get_min_ver;
               BBT_DBG("%s success\n", __func__);
           }
       } else {
           pr_err("%s failed\n", __func__);
 
           return -EINVAL;
       }
   }
 
   return 0;
}
EXPORT_SYMBOL_GPL(nanddev_bbt_in_flash_update);