]> git.sur5r.net Git - u-boot/blob - drivers/mtd/ubi/fastmap-wl.c
SPDX: Convert all of our single license tags to Linux Kernel style
[u-boot] / drivers / mtd / ubi / fastmap-wl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012 Linutronix GmbH
4  * Copyright (c) 2014 sigma star gmbh
5  * Author: Richard Weinberger <richard@nod.at>
6  *
7  */
8
9 /**
10  * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
11  * @wrk: the work description object
12  */
13 #ifndef __UBOOT__
14 static void update_fastmap_work_fn(struct work_struct *wrk)
15 #else
16 void update_fastmap_work_fn(struct ubi_device *ubi)
17 #endif
18 {
19 #ifndef __UBOOT__
20         struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
21 #endif
22
23         ubi_update_fastmap(ubi);
24         spin_lock(&ubi->wl_lock);
25         ubi->fm_work_scheduled = 0;
26         spin_unlock(&ubi->wl_lock);
27 }
28
29 /**
30  * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
31  * @root: the RB-tree where to look for
32  */
33 static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
34 {
35         struct rb_node *p;
36         struct ubi_wl_entry *e, *victim = NULL;
37         int max_ec = UBI_MAX_ERASECOUNTER;
38
39         ubi_rb_for_each_entry(p, e, root, u.rb) {
40                 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
41                         victim = e;
42                         max_ec = e->ec;
43                 }
44         }
45
46         return victim;
47 }
48
49 /**
50  * return_unused_pool_pebs - returns unused PEB to the free tree.
51  * @ubi: UBI device description object
52  * @pool: fastmap pool description object
53  */
54 static void return_unused_pool_pebs(struct ubi_device *ubi,
55                                     struct ubi_fm_pool *pool)
56 {
57         int i;
58         struct ubi_wl_entry *e;
59
60         for (i = pool->used; i < pool->size; i++) {
61                 e = ubi->lookuptbl[pool->pebs[i]];
62                 wl_tree_add(e, &ubi->free);
63                 ubi->free_count++;
64         }
65 }
66
67 static int anchor_pebs_avalible(struct rb_root *root)
68 {
69         struct rb_node *p;
70         struct ubi_wl_entry *e;
71
72         ubi_rb_for_each_entry(p, e, root, u.rb)
73                 if (e->pnum < UBI_FM_MAX_START)
74                         return 1;
75
76         return 0;
77 }
78
79 /**
80  * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
81  * @ubi: UBI device description object
82  * @anchor: This PEB will be used as anchor PEB by fastmap
83  *
84  * The function returns a physical erase block with a given maximal number
85  * and removes it from the wl subsystem.
86  * Must be called with wl_lock held!
87  */
88 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
89 {
90         struct ubi_wl_entry *e = NULL;
91
92         if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
93                 goto out;
94
95         if (anchor)
96                 e = find_anchor_wl_entry(&ubi->free);
97         else
98                 e = find_mean_wl_entry(ubi, &ubi->free);
99
100         if (!e)
101                 goto out;
102
103         self_check_in_wl_tree(ubi, e, &ubi->free);
104
105         /* remove it from the free list,
106          * the wl subsystem does no longer know this erase block */
107         rb_erase(&e->u.rb, &ubi->free);
108         ubi->free_count--;
109 out:
110         return e;
111 }
112
113 /**
114  * ubi_refill_pools - refills all fastmap PEB pools.
115  * @ubi: UBI device description object
116  */
117 void ubi_refill_pools(struct ubi_device *ubi)
118 {
119         struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
120         struct ubi_fm_pool *pool = &ubi->fm_pool;
121         struct ubi_wl_entry *e;
122         int enough;
123
124         spin_lock(&ubi->wl_lock);
125
126         return_unused_pool_pebs(ubi, wl_pool);
127         return_unused_pool_pebs(ubi, pool);
128
129         wl_pool->size = 0;
130         pool->size = 0;
131
132         for (;;) {
133                 enough = 0;
134                 if (pool->size < pool->max_size) {
135                         if (!ubi->free.rb_node)
136                                 break;
137
138                         e = wl_get_wle(ubi);
139                         if (!e)
140                                 break;
141
142                         pool->pebs[pool->size] = e->pnum;
143                         pool->size++;
144                 } else
145                         enough++;
146
147                 if (wl_pool->size < wl_pool->max_size) {
148                         if (!ubi->free.rb_node ||
149                            (ubi->free_count - ubi->beb_rsvd_pebs < 5))
150                                 break;
151
152                         e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
153                         self_check_in_wl_tree(ubi, e, &ubi->free);
154                         rb_erase(&e->u.rb, &ubi->free);
155                         ubi->free_count--;
156
157                         wl_pool->pebs[wl_pool->size] = e->pnum;
158                         wl_pool->size++;
159                 } else
160                         enough++;
161
162                 if (enough == 2)
163                         break;
164         }
165
166         wl_pool->used = 0;
167         pool->used = 0;
168
169         spin_unlock(&ubi->wl_lock);
170 }
171
172 /**
173  * ubi_wl_get_peb - get a physical eraseblock.
174  * @ubi: UBI device description object
175  *
176  * This function returns a physical eraseblock in case of success and a
177  * negative error code in case of failure.
178  * Returns with ubi->fm_eba_sem held in read mode!
179  */
180 int ubi_wl_get_peb(struct ubi_device *ubi)
181 {
182         int ret, retried = 0;
183         struct ubi_fm_pool *pool = &ubi->fm_pool;
184         struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
185
186 again:
187         down_read(&ubi->fm_eba_sem);
188         spin_lock(&ubi->wl_lock);
189
190         /* We check here also for the WL pool because at this point we can
191          * refill the WL pool synchronous. */
192         if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
193                 spin_unlock(&ubi->wl_lock);
194                 up_read(&ubi->fm_eba_sem);
195                 ret = ubi_update_fastmap(ubi);
196                 if (ret) {
197                         ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
198                         down_read(&ubi->fm_eba_sem);
199                         return -ENOSPC;
200                 }
201                 down_read(&ubi->fm_eba_sem);
202                 spin_lock(&ubi->wl_lock);
203         }
204
205         if (pool->used == pool->size) {
206                 spin_unlock(&ubi->wl_lock);
207                 if (retried) {
208                         ubi_err(ubi, "Unable to get a free PEB from user WL pool");
209                         ret = -ENOSPC;
210                         goto out;
211                 }
212                 retried = 1;
213                 up_read(&ubi->fm_eba_sem);
214                 goto again;
215         }
216
217         ubi_assert(pool->used < pool->size);
218         ret = pool->pebs[pool->used++];
219         prot_queue_add(ubi, ubi->lookuptbl[ret]);
220         spin_unlock(&ubi->wl_lock);
221 out:
222         return ret;
223 }
224
225 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
226  *
227  * @ubi: UBI device description object
228  */
229 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
230 {
231         struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
232         int pnum;
233
234         if (pool->used == pool->size) {
235 #ifndef __UBOOT__
236                 /* We cannot update the fastmap here because this
237                  * function is called in atomic context.
238                  * Let's fail here and refill/update it as soon as possible. */
239                 if (!ubi->fm_work_scheduled) {
240                         ubi->fm_work_scheduled = 1;
241                         schedule_work(&ubi->fm_work);
242                 }
243                 return NULL;
244 #else
245                 /*
246                  * No work queues in U-Boot, we must do this immediately
247                  */
248                 update_fastmap_work_fn(ubi);
249 #endif
250         }
251
252         pnum = pool->pebs[pool->used++];
253         return ubi->lookuptbl[pnum];
254 }
255
256 /**
257  * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
258  * @ubi: UBI device description object
259  */
260 int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
261 {
262         struct ubi_work *wrk;
263
264         spin_lock(&ubi->wl_lock);
265         if (ubi->wl_scheduled) {
266                 spin_unlock(&ubi->wl_lock);
267                 return 0;
268         }
269         ubi->wl_scheduled = 1;
270         spin_unlock(&ubi->wl_lock);
271
272         wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
273         if (!wrk) {
274                 spin_lock(&ubi->wl_lock);
275                 ubi->wl_scheduled = 0;
276                 spin_unlock(&ubi->wl_lock);
277                 return -ENOMEM;
278         }
279
280         wrk->anchor = 1;
281         wrk->func = &wear_leveling_worker;
282         schedule_ubi_work(ubi, wrk);
283         return 0;
284 }
285
286 /**
287  * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
288  * sub-system.
289  * see: ubi_wl_put_peb()
290  *
291  * @ubi: UBI device description object
292  * @fm_e: physical eraseblock to return
293  * @lnum: the last used logical eraseblock number for the PEB
294  * @torture: if this physical eraseblock has to be tortured
295  */
296 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
297                       int lnum, int torture)
298 {
299         struct ubi_wl_entry *e;
300         int vol_id, pnum = fm_e->pnum;
301
302         dbg_wl("PEB %d", pnum);
303
304         ubi_assert(pnum >= 0);
305         ubi_assert(pnum < ubi->peb_count);
306
307         spin_lock(&ubi->wl_lock);
308         e = ubi->lookuptbl[pnum];
309
310         /* This can happen if we recovered from a fastmap the very
311          * first time and writing now a new one. In this case the wl system
312          * has never seen any PEB used by the original fastmap.
313          */
314         if (!e) {
315                 e = fm_e;
316                 ubi_assert(e->ec >= 0);
317                 ubi->lookuptbl[pnum] = e;
318         }
319
320         spin_unlock(&ubi->wl_lock);
321
322         vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
323         return schedule_erase(ubi, e, vol_id, lnum, torture);
324 }
325
326 /**
327  * ubi_is_erase_work - checks whether a work is erase work.
328  * @wrk: The work object to be checked
329  */
330 int ubi_is_erase_work(struct ubi_work *wrk)
331 {
332         return wrk->func == erase_worker;
333 }
334
335 static void ubi_fastmap_close(struct ubi_device *ubi)
336 {
337         int i;
338
339         return_unused_pool_pebs(ubi, &ubi->fm_pool);
340         return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
341
342         if (ubi->fm) {
343                 for (i = 0; i < ubi->fm->used_blocks; i++)
344                         kfree(ubi->fm->e[i]);
345         }
346         kfree(ubi->fm);
347 }
348
349 /**
350  * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
351  * See find_mean_wl_entry()
352  *
353  * @ubi: UBI device description object
354  * @e: physical eraseblock to return
355  * @root: RB tree to test against.
356  */
357 static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
358                                            struct ubi_wl_entry *e,
359                                            struct rb_root *root) {
360         if (e && !ubi->fm_disabled && !ubi->fm &&
361             e->pnum < UBI_FM_MAX_START)
362                 e = rb_entry(rb_next(root->rb_node),
363                              struct ubi_wl_entry, u.rb);
364
365         return e;
366 }