11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2801c135cSArtem B. Bityutskiy /*
3801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006
4801c135cSArtem B. Bityutskiy * Copyright (c) Nokia Corporation, 2007
5801c135cSArtem B. Bityutskiy *
6801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём),
7801c135cSArtem B. Bityutskiy * Frank Haverkamp
8801c135cSArtem B. Bityutskiy */
9801c135cSArtem B. Bityutskiy
10801c135cSArtem B. Bityutskiy /*
119f961b57SArtem Bityutskiy * This file includes UBI initialization and building of UBI devices.
129f961b57SArtem Bityutskiy *
139f961b57SArtem Bityutskiy * When UBI is initialized, it attaches all the MTD devices specified as the
149f961b57SArtem Bityutskiy * module load parameters or the kernel boot parameters. If MTD devices were
159f961b57SArtem Bityutskiy * specified, UBI does not attach any MTD device, but it is possible to do
169f961b57SArtem Bityutskiy * later using the "UBI control device".
17801c135cSArtem B. Bityutskiy */
18801c135cSArtem B. Bityutskiy
19801c135cSArtem B. Bityutskiy #include <linux/err.h>
20801c135cSArtem B. Bityutskiy #include <linux/module.h>
21801c135cSArtem B. Bityutskiy #include <linux/moduleparam.h>
22801c135cSArtem B. Bityutskiy #include <linux/stringify.h>
23f9b0080eSArtem Bityutskiy #include <linux/namei.h>
24801c135cSArtem B. Bityutskiy #include <linux/stat.h>
259f961b57SArtem Bityutskiy #include <linux/miscdevice.h>
26ba4087e9SRichard Genoud #include <linux/mtd/partitions.h>
277753f169SVignesh Babu #include <linux/log2.h>
28cdfa788aSArtem Bityutskiy #include <linux/kthread.h>
29774b1382SRoel Kluin #include <linux/kernel.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31f83c3838SEzequiel Garcia #include <linux/major.h>
32801c135cSArtem B. Bityutskiy #include "ubi.h"
33801c135cSArtem B. Bityutskiy
34801c135cSArtem B. Bityutskiy /* Maximum length of the 'mtd=' parameter */
35801c135cSArtem B. Bityutskiy #define MTD_PARAM_LEN_MAX 64
36801c135cSArtem B. Bityutskiy
375993f9b7SRichard Genoud /* Maximum number of comma-separated items in the 'mtd=' parameter */
387af73882SZhaoLong Wang #define MTD_PARAM_MAX_COUNT 5
395993f9b7SRichard Genoud
40d2f588f9SRichard Genoud /* Maximum value for the number of bad PEBs per 1024 PEBs */
41d2f588f9SRichard Genoud #define MAX_MTD_UBI_BEB_LIMIT 768
42d2f588f9SRichard Genoud
43af7ad7a0SMarc Kleine-Budde #ifdef CONFIG_MTD_UBI_MODULE
44af7ad7a0SMarc Kleine-Budde #define ubi_is_module() 1
45af7ad7a0SMarc Kleine-Budde #else
46af7ad7a0SMarc Kleine-Budde #define ubi_is_module() 0
47af7ad7a0SMarc Kleine-Budde #endif
48af7ad7a0SMarc Kleine-Budde
49801c135cSArtem B. Bityutskiy /**
50801c135cSArtem B. Bityutskiy * struct mtd_dev_param - MTD device parameter description data structure.
51f9b0080eSArtem Bityutskiy * @name: MTD character device node path, MTD device name, or MTD device number
52f9b0080eSArtem Bityutskiy * string
539efbb507SLee Jones * @ubi_num: UBI number
54801c135cSArtem B. Bityutskiy * @vid_hdr_offs: VID header offset
55edac493dSRichard Genoud * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
567af73882SZhaoLong Wang * @enable_fm: enable fastmap when value is non-zero
57801c135cSArtem B. Bityutskiy */
589c9ec147SArtem Bityutskiy struct mtd_dev_param {
59801c135cSArtem B. Bityutskiy char name[MTD_PARAM_LEN_MAX];
6083ff59a0SMike Frysinger int ubi_num;
61801c135cSArtem B. Bityutskiy int vid_hdr_offs;
62edac493dSRichard Genoud int max_beb_per1024;
637af73882SZhaoLong Wang int enable_fm;
64801c135cSArtem B. Bityutskiy };
65801c135cSArtem B. Bityutskiy
66801c135cSArtem B. Bityutskiy /* Numbers of elements set in the @mtd_dev_param array */
67435009d4SAndy Shevchenko static int mtd_devs;
68801c135cSArtem B. Bityutskiy
69801c135cSArtem B. Bityutskiy /* MTD devices specification parameters */
70435009d4SAndy Shevchenko static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
7177e6c2f0SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
7277e6c2f0SRichard Weinberger /* UBI module parameter to enable fastmap automatically on non-fastmap images */
7377e6c2f0SRichard Weinberger static bool fm_autoconvert;
74479c2c0cSRichard Weinberger static bool fm_debug;
7577e6c2f0SRichard Weinberger #endif
76801c135cSArtem B. Bityutskiy
7706b68ba1SArtem Bityutskiy /* Slab cache for wear-leveling entries */
7806b68ba1SArtem Bityutskiy struct kmem_cache *ubi_wl_entry_slab;
7906b68ba1SArtem Bityutskiy
809f961b57SArtem Bityutskiy /* UBI control character device */
819f961b57SArtem Bityutskiy static struct miscdevice ubi_ctrl_cdev = {
829f961b57SArtem Bityutskiy .minor = MISC_DYNAMIC_MINOR,
839f961b57SArtem Bityutskiy .name = "ubi_ctrl",
849f961b57SArtem Bityutskiy .fops = &ubi_ctrl_cdev_operations,
859f961b57SArtem Bityutskiy };
8606b68ba1SArtem Bityutskiy
87e73f4459SArtem Bityutskiy /* All UBI devices in system */
88e73f4459SArtem Bityutskiy static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
89e73f4459SArtem Bityutskiy
90cdfa788aSArtem Bityutskiy /* Serializes UBI devices creations and removals */
91cdfa788aSArtem Bityutskiy DEFINE_MUTEX(ubi_devices_mutex);
92cdfa788aSArtem Bityutskiy
93e73f4459SArtem Bityutskiy /* Protects @ubi_devices and @ubi->ref_count */
94e73f4459SArtem Bityutskiy static DEFINE_SPINLOCK(ubi_devices_lock);
95e73f4459SArtem Bityutskiy
96801c135cSArtem B. Bityutskiy /* "Show" method for files in '/<sysfs>/class/ubi/' */
97219eccdaSGreg Kroah-Hartman /* UBI version attribute ('/<sysfs>/class/ubi/version') */
version_show(const struct class * class,const struct class_attribute * attr,char * buf)9875a2d422SGreg Kroah-Hartman static ssize_t version_show(const struct class *class, const struct class_attribute *attr,
99219eccdaSGreg Kroah-Hartman char *buf)
100801c135cSArtem B. Bityutskiy {
101801c135cSArtem B. Bityutskiy return sprintf(buf, "%d\n", UBI_VERSION);
102801c135cSArtem B. Bityutskiy }
103219eccdaSGreg Kroah-Hartman static CLASS_ATTR_RO(version);
104801c135cSArtem B. Bityutskiy
105219eccdaSGreg Kroah-Hartman static struct attribute *ubi_class_attrs[] = {
106219eccdaSGreg Kroah-Hartman &class_attr_version.attr,
107219eccdaSGreg Kroah-Hartman NULL,
10853cd255cSTakashi Iwai };
109219eccdaSGreg Kroah-Hartman ATTRIBUTE_GROUPS(ubi_class);
11053cd255cSTakashi Iwai
11153cd255cSTakashi Iwai /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
11253cd255cSTakashi Iwai struct class ubi_class = {
11353cd255cSTakashi Iwai .name = UBI_NAME_STR,
114219eccdaSGreg Kroah-Hartman .class_groups = ubi_class_groups,
11553cd255cSTakashi Iwai };
116801c135cSArtem B. Bityutskiy
117801c135cSArtem B. Bityutskiy static ssize_t dev_attribute_show(struct device *dev,
118801c135cSArtem B. Bityutskiy struct device_attribute *attr, char *buf);
119801c135cSArtem B. Bityutskiy
120801c135cSArtem B. Bityutskiy /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
121801c135cSArtem B. Bityutskiy static struct device_attribute dev_eraseblock_size =
122801c135cSArtem B. Bityutskiy __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
123801c135cSArtem B. Bityutskiy static struct device_attribute dev_avail_eraseblocks =
124801c135cSArtem B. Bityutskiy __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
125801c135cSArtem B. Bityutskiy static struct device_attribute dev_total_eraseblocks =
126801c135cSArtem B. Bityutskiy __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
127801c135cSArtem B. Bityutskiy static struct device_attribute dev_volumes_count =
128801c135cSArtem B. Bityutskiy __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
129801c135cSArtem B. Bityutskiy static struct device_attribute dev_max_ec =
130801c135cSArtem B. Bityutskiy __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
131801c135cSArtem B. Bityutskiy static struct device_attribute dev_reserved_for_bad =
132801c135cSArtem B. Bityutskiy __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
133801c135cSArtem B. Bityutskiy static struct device_attribute dev_bad_peb_count =
134801c135cSArtem B. Bityutskiy __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
135801c135cSArtem B. Bityutskiy static struct device_attribute dev_max_vol_count =
136801c135cSArtem B. Bityutskiy __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
137801c135cSArtem B. Bityutskiy static struct device_attribute dev_min_io_size =
138801c135cSArtem B. Bityutskiy __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
139801c135cSArtem B. Bityutskiy static struct device_attribute dev_bgt_enabled =
140801c135cSArtem B. Bityutskiy __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
141b6b76ba4SArtem Bityutskiy static struct device_attribute dev_mtd_num =
142b6b76ba4SArtem Bityutskiy __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
143525bab71SEzequiel Garcia static struct device_attribute dev_ro_mode =
144525bab71SEzequiel Garcia __ATTR(ro_mode, S_IRUGO, dev_attribute_show, NULL);
145801c135cSArtem B. Bityutskiy
146e73f4459SArtem Bityutskiy /**
1470e0ee1ccSDmitry Pervushin * ubi_volume_notify - send a volume change notification.
1480e0ee1ccSDmitry Pervushin * @ubi: UBI device description object
1490e0ee1ccSDmitry Pervushin * @vol: volume description object of the changed volume
1500e0ee1ccSDmitry Pervushin * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
1510e0ee1ccSDmitry Pervushin *
1520e0ee1ccSDmitry Pervushin * This is a helper function which notifies all subscribers about a volume
1530e0ee1ccSDmitry Pervushin * change event (creation, removal, re-sizing, re-naming, updating). Returns
1540e0ee1ccSDmitry Pervushin * zero in case of success and a negative error code in case of failure.
1550e0ee1ccSDmitry Pervushin */
ubi_volume_notify(struct ubi_device * ubi,struct ubi_volume * vol,int ntype)1560e0ee1ccSDmitry Pervushin int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
1570e0ee1ccSDmitry Pervushin {
15884b678f4SRichard Weinberger int ret;
1590e0ee1ccSDmitry Pervushin struct ubi_notification nt;
1600e0ee1ccSDmitry Pervushin
1610e0ee1ccSDmitry Pervushin ubi_do_get_device_info(ubi, &nt.di);
1620e0ee1ccSDmitry Pervushin ubi_do_get_volume_info(ubi, vol, &nt.vi);
16377e6c2f0SRichard Weinberger
16477e6c2f0SRichard Weinberger switch (ntype) {
16577e6c2f0SRichard Weinberger case UBI_VOLUME_ADDED:
16677e6c2f0SRichard Weinberger case UBI_VOLUME_REMOVED:
16777e6c2f0SRichard Weinberger case UBI_VOLUME_RESIZED:
16877e6c2f0SRichard Weinberger case UBI_VOLUME_RENAMED:
16984b678f4SRichard Weinberger ret = ubi_update_fastmap(ubi);
17084b678f4SRichard Weinberger if (ret)
17184b678f4SRichard Weinberger ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
17277e6c2f0SRichard Weinberger }
17384b678f4SRichard Weinberger
1740e0ee1ccSDmitry Pervushin return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
1750e0ee1ccSDmitry Pervushin }
1760e0ee1ccSDmitry Pervushin
1770e0ee1ccSDmitry Pervushin /**
1780e0ee1ccSDmitry Pervushin * ubi_notify_all - send a notification to all volumes.
1790e0ee1ccSDmitry Pervushin * @ubi: UBI device description object
1800e0ee1ccSDmitry Pervushin * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
1810e0ee1ccSDmitry Pervushin * @nb: the notifier to call
1820e0ee1ccSDmitry Pervushin *
1830e0ee1ccSDmitry Pervushin * This function walks all volumes of UBI device @ubi and sends the @ntype
1840e0ee1ccSDmitry Pervushin * notification for each volume. If @nb is %NULL, then all registered notifiers
1850e0ee1ccSDmitry Pervushin * are called, otherwise only the @nb notifier is called. Returns the number of
1860e0ee1ccSDmitry Pervushin * sent notifications.
1870e0ee1ccSDmitry Pervushin */
ubi_notify_all(struct ubi_device * ubi,int ntype,struct notifier_block * nb)1880e0ee1ccSDmitry Pervushin int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
1890e0ee1ccSDmitry Pervushin {
1900e0ee1ccSDmitry Pervushin struct ubi_notification nt;
1910e0ee1ccSDmitry Pervushin int i, count = 0;
1920e0ee1ccSDmitry Pervushin
1930e0ee1ccSDmitry Pervushin ubi_do_get_device_info(ubi, &nt.di);
1940e0ee1ccSDmitry Pervushin
1950e0ee1ccSDmitry Pervushin mutex_lock(&ubi->device_mutex);
1960e0ee1ccSDmitry Pervushin for (i = 0; i < ubi->vtbl_slots; i++) {
1970e0ee1ccSDmitry Pervushin /*
1980e0ee1ccSDmitry Pervushin * Since the @ubi->device is locked, and we are not going to
1990e0ee1ccSDmitry Pervushin * change @ubi->volumes, we do not have to lock
2000e0ee1ccSDmitry Pervushin * @ubi->volumes_lock.
2010e0ee1ccSDmitry Pervushin */
2020e0ee1ccSDmitry Pervushin if (!ubi->volumes[i])
2030e0ee1ccSDmitry Pervushin continue;
2040e0ee1ccSDmitry Pervushin
2050e0ee1ccSDmitry Pervushin ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
2060e0ee1ccSDmitry Pervushin if (nb)
2070e0ee1ccSDmitry Pervushin nb->notifier_call(nb, ntype, &nt);
2080e0ee1ccSDmitry Pervushin else
2090e0ee1ccSDmitry Pervushin blocking_notifier_call_chain(&ubi_notifiers, ntype,
2100e0ee1ccSDmitry Pervushin &nt);
2110e0ee1ccSDmitry Pervushin count += 1;
2120e0ee1ccSDmitry Pervushin }
2130e0ee1ccSDmitry Pervushin mutex_unlock(&ubi->device_mutex);
2140e0ee1ccSDmitry Pervushin
2150e0ee1ccSDmitry Pervushin return count;
2160e0ee1ccSDmitry Pervushin }
2170e0ee1ccSDmitry Pervushin
2180e0ee1ccSDmitry Pervushin /**
2190e0ee1ccSDmitry Pervushin * ubi_enumerate_volumes - send "add" notification for all existing volumes.
2200e0ee1ccSDmitry Pervushin * @nb: the notifier to call
2210e0ee1ccSDmitry Pervushin *
2220e0ee1ccSDmitry Pervushin * This function walks all UBI devices and volumes and sends the
2230e0ee1ccSDmitry Pervushin * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
2240e0ee1ccSDmitry Pervushin * registered notifiers are called, otherwise only the @nb notifier is called.
2250e0ee1ccSDmitry Pervushin * Returns the number of sent notifications.
2260e0ee1ccSDmitry Pervushin */
ubi_enumerate_volumes(struct notifier_block * nb)2270e0ee1ccSDmitry Pervushin int ubi_enumerate_volumes(struct notifier_block *nb)
2280e0ee1ccSDmitry Pervushin {
2290e0ee1ccSDmitry Pervushin int i, count = 0;
2300e0ee1ccSDmitry Pervushin
2310e0ee1ccSDmitry Pervushin /*
2320e0ee1ccSDmitry Pervushin * Since the @ubi_devices_mutex is locked, and we are not going to
2330e0ee1ccSDmitry Pervushin * change @ubi_devices, we do not have to lock @ubi_devices_lock.
2340e0ee1ccSDmitry Pervushin */
2350e0ee1ccSDmitry Pervushin for (i = 0; i < UBI_MAX_DEVICES; i++) {
2360e0ee1ccSDmitry Pervushin struct ubi_device *ubi = ubi_devices[i];
2370e0ee1ccSDmitry Pervushin
2380e0ee1ccSDmitry Pervushin if (!ubi)
2390e0ee1ccSDmitry Pervushin continue;
2400e0ee1ccSDmitry Pervushin count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
2410e0ee1ccSDmitry Pervushin }
2420e0ee1ccSDmitry Pervushin
2430e0ee1ccSDmitry Pervushin return count;
2440e0ee1ccSDmitry Pervushin }
2450e0ee1ccSDmitry Pervushin
2460e0ee1ccSDmitry Pervushin /**
247e73f4459SArtem Bityutskiy * ubi_get_device - get UBI device.
248e73f4459SArtem Bityutskiy * @ubi_num: UBI device number
249e73f4459SArtem Bityutskiy *
250e73f4459SArtem Bityutskiy * This function returns UBI device description object for UBI device number
251e73f4459SArtem Bityutskiy * @ubi_num, or %NULL if the device does not exist. This function increases the
252e73f4459SArtem Bityutskiy * device reference count to prevent removal of the device. In other words, the
253e73f4459SArtem Bityutskiy * device cannot be removed if its reference count is not zero.
254e73f4459SArtem Bityutskiy */
ubi_get_device(int ubi_num)255e73f4459SArtem Bityutskiy struct ubi_device *ubi_get_device(int ubi_num)
256e73f4459SArtem Bityutskiy {
257e73f4459SArtem Bityutskiy struct ubi_device *ubi;
258e73f4459SArtem Bityutskiy
259e73f4459SArtem Bityutskiy spin_lock(&ubi_devices_lock);
260e73f4459SArtem Bityutskiy ubi = ubi_devices[ubi_num];
261e73f4459SArtem Bityutskiy if (ubi) {
262e73f4459SArtem Bityutskiy ubi_assert(ubi->ref_count >= 0);
263e73f4459SArtem Bityutskiy ubi->ref_count += 1;
264e73f4459SArtem Bityutskiy get_device(&ubi->dev);
265e73f4459SArtem Bityutskiy }
266e73f4459SArtem Bityutskiy spin_unlock(&ubi_devices_lock);
267e73f4459SArtem Bityutskiy
268e73f4459SArtem Bityutskiy return ubi;
269e73f4459SArtem Bityutskiy }
270e73f4459SArtem Bityutskiy
271e73f4459SArtem Bityutskiy /**
272e73f4459SArtem Bityutskiy * ubi_put_device - drop an UBI device reference.
273e73f4459SArtem Bityutskiy * @ubi: UBI device description object
274e73f4459SArtem Bityutskiy */
ubi_put_device(struct ubi_device * ubi)275e73f4459SArtem Bityutskiy void ubi_put_device(struct ubi_device *ubi)
276e73f4459SArtem Bityutskiy {
277e73f4459SArtem Bityutskiy spin_lock(&ubi_devices_lock);
278e73f4459SArtem Bityutskiy ubi->ref_count -= 1;
279e73f4459SArtem Bityutskiy put_device(&ubi->dev);
280e73f4459SArtem Bityutskiy spin_unlock(&ubi_devices_lock);
281e73f4459SArtem Bityutskiy }
282e73f4459SArtem Bityutskiy
283e73f4459SArtem Bityutskiy /**
284ebaaf1afSArtem Bityutskiy * ubi_get_by_major - get UBI device by character device major number.
285e73f4459SArtem Bityutskiy * @major: major number
286e73f4459SArtem Bityutskiy *
287e73f4459SArtem Bityutskiy * This function is similar to 'ubi_get_device()', but it searches the device
288e73f4459SArtem Bityutskiy * by its major number.
289e73f4459SArtem Bityutskiy */
ubi_get_by_major(int major)290e73f4459SArtem Bityutskiy struct ubi_device *ubi_get_by_major(int major)
291e73f4459SArtem Bityutskiy {
292e73f4459SArtem Bityutskiy int i;
293e73f4459SArtem Bityutskiy struct ubi_device *ubi;
294e73f4459SArtem Bityutskiy
295e73f4459SArtem Bityutskiy spin_lock(&ubi_devices_lock);
296e73f4459SArtem Bityutskiy for (i = 0; i < UBI_MAX_DEVICES; i++) {
297e73f4459SArtem Bityutskiy ubi = ubi_devices[i];
298e73f4459SArtem Bityutskiy if (ubi && MAJOR(ubi->cdev.dev) == major) {
299e73f4459SArtem Bityutskiy ubi_assert(ubi->ref_count >= 0);
300e73f4459SArtem Bityutskiy ubi->ref_count += 1;
301e73f4459SArtem Bityutskiy get_device(&ubi->dev);
302e73f4459SArtem Bityutskiy spin_unlock(&ubi_devices_lock);
303e73f4459SArtem Bityutskiy return ubi;
304e73f4459SArtem Bityutskiy }
305e73f4459SArtem Bityutskiy }
306e73f4459SArtem Bityutskiy spin_unlock(&ubi_devices_lock);
307e73f4459SArtem Bityutskiy
308e73f4459SArtem Bityutskiy return NULL;
309e73f4459SArtem Bityutskiy }
310e73f4459SArtem Bityutskiy
311e73f4459SArtem Bityutskiy /**
312e73f4459SArtem Bityutskiy * ubi_major2num - get UBI device number by character device major number.
313e73f4459SArtem Bityutskiy * @major: major number
314e73f4459SArtem Bityutskiy *
315e73f4459SArtem Bityutskiy * This function searches UBI device number object by its major number. If UBI
316cdfa788aSArtem Bityutskiy * device was not found, this function returns -ENODEV, otherwise the UBI device
317e73f4459SArtem Bityutskiy * number is returned.
318e73f4459SArtem Bityutskiy */
ubi_major2num(int major)319e73f4459SArtem Bityutskiy int ubi_major2num(int major)
320e73f4459SArtem Bityutskiy {
321e73f4459SArtem Bityutskiy int i, ubi_num = -ENODEV;
322e73f4459SArtem Bityutskiy
323e73f4459SArtem Bityutskiy spin_lock(&ubi_devices_lock);
324e73f4459SArtem Bityutskiy for (i = 0; i < UBI_MAX_DEVICES; i++) {
325e73f4459SArtem Bityutskiy struct ubi_device *ubi = ubi_devices[i];
326e73f4459SArtem Bityutskiy
327e73f4459SArtem Bityutskiy if (ubi && MAJOR(ubi->cdev.dev) == major) {
328e73f4459SArtem Bityutskiy ubi_num = ubi->ubi_num;
329e73f4459SArtem Bityutskiy break;
330e73f4459SArtem Bityutskiy }
331e73f4459SArtem Bityutskiy }
332e73f4459SArtem Bityutskiy spin_unlock(&ubi_devices_lock);
333e73f4459SArtem Bityutskiy
334e73f4459SArtem Bityutskiy return ubi_num;
335e73f4459SArtem Bityutskiy }
336e73f4459SArtem Bityutskiy
337801c135cSArtem B. Bityutskiy /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
dev_attribute_show(struct device * dev,struct device_attribute * attr,char * buf)338801c135cSArtem B. Bityutskiy static ssize_t dev_attribute_show(struct device *dev,
339801c135cSArtem B. Bityutskiy struct device_attribute *attr, char *buf)
340801c135cSArtem B. Bityutskiy {
341e73f4459SArtem Bityutskiy ssize_t ret;
342e73f4459SArtem Bityutskiy struct ubi_device *ubi;
343801c135cSArtem B. Bityutskiy
344e73f4459SArtem Bityutskiy /*
345e73f4459SArtem Bityutskiy * The below code looks weird, but it actually makes sense. We get the
346e73f4459SArtem Bityutskiy * UBI device reference from the contained 'struct ubi_device'. But it
347e73f4459SArtem Bityutskiy * is unclear if the device was removed or not yet. Indeed, if the
348e73f4459SArtem Bityutskiy * device was removed before we increased its reference count,
349e73f4459SArtem Bityutskiy * 'ubi_get_device()' will return -ENODEV and we fail.
350e73f4459SArtem Bityutskiy *
351e73f4459SArtem Bityutskiy * Remember, 'struct ubi_device' is freed in the release function, so
352e73f4459SArtem Bityutskiy * we still can use 'ubi->ubi_num'.
353e73f4459SArtem Bityutskiy */
354801c135cSArtem B. Bityutskiy ubi = container_of(dev, struct ubi_device, dev);
355e73f4459SArtem Bityutskiy
356801c135cSArtem B. Bityutskiy if (attr == &dev_eraseblock_size)
357e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->leb_size);
358801c135cSArtem B. Bityutskiy else if (attr == &dev_avail_eraseblocks)
359e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->avail_pebs);
360801c135cSArtem B. Bityutskiy else if (attr == &dev_total_eraseblocks)
361e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->good_peb_count);
362801c135cSArtem B. Bityutskiy else if (attr == &dev_volumes_count)
3634b3cc340SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
364801c135cSArtem B. Bityutskiy else if (attr == &dev_max_ec)
365e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->max_ec);
366801c135cSArtem B. Bityutskiy else if (attr == &dev_reserved_for_bad)
367e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
368801c135cSArtem B. Bityutskiy else if (attr == &dev_bad_peb_count)
369e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
370801c135cSArtem B. Bityutskiy else if (attr == &dev_max_vol_count)
371e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
372801c135cSArtem B. Bityutskiy else if (attr == &dev_min_io_size)
373e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->min_io_size);
374801c135cSArtem B. Bityutskiy else if (attr == &dev_bgt_enabled)
375e73f4459SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->thread_enabled);
376b6b76ba4SArtem Bityutskiy else if (attr == &dev_mtd_num)
377b6b76ba4SArtem Bityutskiy ret = sprintf(buf, "%d\n", ubi->mtd->index);
378525bab71SEzequiel Garcia else if (attr == &dev_ro_mode)
379525bab71SEzequiel Garcia ret = sprintf(buf, "%d\n", ubi->ro_mode);
380801c135cSArtem B. Bityutskiy else
381b6b76ba4SArtem Bityutskiy ret = -EINVAL;
382801c135cSArtem B. Bityutskiy
383e73f4459SArtem Bityutskiy return ret;
384801c135cSArtem B. Bityutskiy }
385801c135cSArtem B. Bityutskiy
38653cd255cSTakashi Iwai static struct attribute *ubi_dev_attrs[] = {
38753cd255cSTakashi Iwai &dev_eraseblock_size.attr,
38853cd255cSTakashi Iwai &dev_avail_eraseblocks.attr,
38953cd255cSTakashi Iwai &dev_total_eraseblocks.attr,
39053cd255cSTakashi Iwai &dev_volumes_count.attr,
39153cd255cSTakashi Iwai &dev_max_ec.attr,
39253cd255cSTakashi Iwai &dev_reserved_for_bad.attr,
39353cd255cSTakashi Iwai &dev_bad_peb_count.attr,
39453cd255cSTakashi Iwai &dev_max_vol_count.attr,
39553cd255cSTakashi Iwai &dev_min_io_size.attr,
39653cd255cSTakashi Iwai &dev_bgt_enabled.attr,
39753cd255cSTakashi Iwai &dev_mtd_num.attr,
398525bab71SEzequiel Garcia &dev_ro_mode.attr,
39953cd255cSTakashi Iwai NULL
40053cd255cSTakashi Iwai };
40153cd255cSTakashi Iwai ATTRIBUTE_GROUPS(ubi_dev);
40253cd255cSTakashi Iwai
dev_release(struct device * dev)40336b477d0SArtem Bityutskiy static void dev_release(struct device *dev)
40436b477d0SArtem Bityutskiy {
40536b477d0SArtem Bityutskiy struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
40636b477d0SArtem Bityutskiy
40736b477d0SArtem Bityutskiy kfree(ubi);
40836b477d0SArtem Bityutskiy }
409801c135cSArtem B. Bityutskiy
410801c135cSArtem B. Bityutskiy /**
4110bf1c439SArtem Bityutskiy * kill_volumes - destroy all user volumes.
412801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
413801c135cSArtem B. Bityutskiy */
kill_volumes(struct ubi_device * ubi)414801c135cSArtem B. Bityutskiy static void kill_volumes(struct ubi_device *ubi)
415801c135cSArtem B. Bityutskiy {
416801c135cSArtem B. Bityutskiy int i;
417801c135cSArtem B. Bityutskiy
418801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++)
419801c135cSArtem B. Bityutskiy if (ubi->volumes[i])
42089b96b69SArtem Bityutskiy ubi_free_volume(ubi, ubi->volumes[i]);
421801c135cSArtem B. Bityutskiy }
422801c135cSArtem B. Bityutskiy
423801c135cSArtem B. Bityutskiy /**
424801c135cSArtem B. Bityutskiy * uif_init - initialize user interfaces for an UBI device.
425801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
4260bf1c439SArtem Bityutskiy *
4270bf1c439SArtem Bityutskiy * This function initializes various user interfaces for an UBI device. If the
4280bf1c439SArtem Bityutskiy * initialization fails at an early stage, this function frees all the
429493cfaeaSLogan Gunthorpe * resources it allocated, returns an error.
430801c135cSArtem B. Bityutskiy *
431801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in
4320bf1c439SArtem Bityutskiy * case of failure.
433801c135cSArtem B. Bityutskiy */
uif_init(struct ubi_device * ubi)434493cfaeaSLogan Gunthorpe static int uif_init(struct ubi_device *ubi)
435801c135cSArtem B. Bityutskiy {
4368c4c19f1SArtem Bityutskiy int i, err;
437801c135cSArtem B. Bityutskiy dev_t dev;
438801c135cSArtem B. Bityutskiy
439801c135cSArtem B. Bityutskiy sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
440801c135cSArtem B. Bityutskiy
441801c135cSArtem B. Bityutskiy /*
442801c135cSArtem B. Bityutskiy * Major numbers for the UBI character devices are allocated
443801c135cSArtem B. Bityutskiy * dynamically. Major numbers of volume character devices are
444801c135cSArtem B. Bityutskiy * equivalent to ones of the corresponding UBI character device. Minor
445801c135cSArtem B. Bityutskiy * numbers of UBI character devices are 0, while minor numbers of
446801c135cSArtem B. Bityutskiy * volume character devices start from 1. Thus, we allocate one major
447801c135cSArtem B. Bityutskiy * number and ubi->vtbl_slots + 1 minor numbers.
448801c135cSArtem B. Bityutskiy */
449801c135cSArtem B. Bityutskiy err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
450801c135cSArtem B. Bityutskiy if (err) {
45132608703STanya Brokhman ubi_err(ubi, "cannot register UBI character devices");
452801c135cSArtem B. Bityutskiy return err;
453801c135cSArtem B. Bityutskiy }
454801c135cSArtem B. Bityutskiy
455493cfaeaSLogan Gunthorpe ubi->dev.devt = dev;
456493cfaeaSLogan Gunthorpe
45749dfc299SArtem Bityutskiy ubi_assert(MINOR(dev) == 0);
458801c135cSArtem B. Bityutskiy cdev_init(&ubi->cdev, &ubi_cdev_operations);
459c8566350SArtem Bityutskiy dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
460801c135cSArtem B. Bityutskiy ubi->cdev.owner = THIS_MODULE;
461801c135cSArtem B. Bityutskiy
462493cfaeaSLogan Gunthorpe dev_set_name(&ubi->dev, UBI_NAME_STR "%d", ubi->ubi_num);
463493cfaeaSLogan Gunthorpe err = cdev_device_add(&ubi->cdev, &ubi->dev);
464801c135cSArtem B. Bityutskiy if (err)
465493cfaeaSLogan Gunthorpe goto out_unreg;
466801c135cSArtem B. Bityutskiy
467801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++)
468801c135cSArtem B. Bityutskiy if (ubi->volumes[i]) {
46989b96b69SArtem Bityutskiy err = ubi_add_volume(ubi, ubi->volumes[i]);
47001f7b309SArtem Bityutskiy if (err) {
47132608703STanya Brokhman ubi_err(ubi, "cannot add volume %d", i);
472c15859bfSYang Yingliang ubi->volumes[i] = NULL;
473801c135cSArtem B. Bityutskiy goto out_volumes;
474801c135cSArtem B. Bityutskiy }
47501f7b309SArtem Bityutskiy }
476801c135cSArtem B. Bityutskiy
477801c135cSArtem B. Bityutskiy return 0;
478801c135cSArtem B. Bityutskiy
479801c135cSArtem B. Bityutskiy out_volumes:
480801c135cSArtem B. Bityutskiy kill_volumes(ubi);
481493cfaeaSLogan Gunthorpe cdev_device_del(&ubi->cdev, &ubi->dev);
482801c135cSArtem B. Bityutskiy out_unreg:
48349dfc299SArtem Bityutskiy unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
48432608703STanya Brokhman ubi_err(ubi, "cannot initialize UBI %s, error %d",
48532608703STanya Brokhman ubi->ubi_name, err);
486801c135cSArtem B. Bityutskiy return err;
487801c135cSArtem B. Bityutskiy }
488801c135cSArtem B. Bityutskiy
489801c135cSArtem B. Bityutskiy /**
490801c135cSArtem B. Bityutskiy * uif_close - close user interfaces for an UBI device.
491801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
492505d1caaSArtem Bityutskiy *
493505d1caaSArtem Bityutskiy * Note, since this function un-registers UBI volume device objects (@vol->dev),
494505d1caaSArtem Bityutskiy * the memory allocated voe the volumes is freed as well (in the release
495505d1caaSArtem Bityutskiy * function).
496801c135cSArtem B. Bityutskiy */
uif_close(struct ubi_device * ubi)497801c135cSArtem B. Bityutskiy static void uif_close(struct ubi_device *ubi)
498801c135cSArtem B. Bityutskiy {
499801c135cSArtem B. Bityutskiy kill_volumes(ubi);
500493cfaeaSLogan Gunthorpe cdev_device_del(&ubi->cdev, &ubi->dev);
50149dfc299SArtem Bityutskiy unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
502801c135cSArtem B. Bityutskiy }
503801c135cSArtem B. Bityutskiy
504801c135cSArtem B. Bityutskiy /**
505fc55dacfSHou Tao * ubi_free_volumes_from - free volumes from specific index.
506fc55dacfSHou Tao * @ubi: UBI device description object
507fc55dacfSHou Tao * @from: the start index used for volume free.
508fc55dacfSHou Tao */
ubi_free_volumes_from(struct ubi_device * ubi,int from)509fc55dacfSHou Tao static void ubi_free_volumes_from(struct ubi_device *ubi, int from)
510fc55dacfSHou Tao {
511fc55dacfSHou Tao int i;
512fc55dacfSHou Tao
513fc55dacfSHou Tao for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
514fc55dacfSHou Tao if (!ubi->volumes[i])
515fc55dacfSHou Tao continue;
516fc55dacfSHou Tao ubi_eba_replace_table(ubi->volumes[i], NULL);
517fc55dacfSHou Tao ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
518fc55dacfSHou Tao kfree(ubi->volumes[i]);
519fc55dacfSHou Tao ubi->volumes[i] = NULL;
520fc55dacfSHou Tao }
521fc55dacfSHou Tao }
522fc55dacfSHou Tao
523fc55dacfSHou Tao /**
524fc55dacfSHou Tao * ubi_free_all_volumes - free all volumes.
525fc55dacfSHou Tao * @ubi: UBI device description object
526fc55dacfSHou Tao */
ubi_free_all_volumes(struct ubi_device * ubi)527fc55dacfSHou Tao void ubi_free_all_volumes(struct ubi_device *ubi)
528fc55dacfSHou Tao {
529fc55dacfSHou Tao ubi_free_volumes_from(ubi, 0);
530fc55dacfSHou Tao }
531fc55dacfSHou Tao
532fc55dacfSHou Tao /**
53347e1ec70SArtem Bityutskiy * ubi_free_internal_volumes - free internal volumes.
534505d1caaSArtem Bityutskiy * @ubi: UBI device description object
535505d1caaSArtem Bityutskiy */
ubi_free_internal_volumes(struct ubi_device * ubi)53647e1ec70SArtem Bityutskiy void ubi_free_internal_volumes(struct ubi_device *ubi)
537505d1caaSArtem Bityutskiy {
538fc55dacfSHou Tao ubi_free_volumes_from(ubi, ubi->vtbl_slots);
539505d1caaSArtem Bityutskiy }
540505d1caaSArtem Bityutskiy
get_bad_peb_limit(const struct ubi_device * ubi,int max_beb_per1024)54195e6fb02SRichard Genoud static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
54295e6fb02SRichard Genoud {
54395e6fb02SRichard Genoud int limit, device_pebs;
54495e6fb02SRichard Genoud uint64_t device_size;
54595e6fb02SRichard Genoud
546c0e860baSJeff Westfahl if (!max_beb_per1024) {
547c0e860baSJeff Westfahl /*
548c0e860baSJeff Westfahl * Since max_beb_per1024 has not been set by the user in either
549c0e860baSJeff Westfahl * the cmdline or Kconfig, use mtd_max_bad_blocks to set the
550c0e860baSJeff Westfahl * limit if it is supported by the device.
551c0e860baSJeff Westfahl */
552c0e860baSJeff Westfahl limit = mtd_max_bad_blocks(ubi->mtd, 0, ubi->mtd->size);
553c0e860baSJeff Westfahl if (limit < 0)
55495e6fb02SRichard Genoud return 0;
555c0e860baSJeff Westfahl return limit;
556c0e860baSJeff Westfahl }
55795e6fb02SRichard Genoud
55895e6fb02SRichard Genoud /*
55995e6fb02SRichard Genoud * Here we are using size of the entire flash chip and
56095e6fb02SRichard Genoud * not just the MTD partition size because the maximum
56195e6fb02SRichard Genoud * number of bad eraseblocks is a percentage of the
56295e6fb02SRichard Genoud * whole device and bad eraseblocks are not fairly
56395e6fb02SRichard Genoud * distributed over the flash chip. So the worst case
56495e6fb02SRichard Genoud * is that all the bad eraseblocks of the chip are in
56595e6fb02SRichard Genoud * the MTD partition we are attaching (ubi->mtd).
56695e6fb02SRichard Genoud */
56795e6fb02SRichard Genoud device_size = mtd_get_device_size(ubi->mtd);
56895e6fb02SRichard Genoud device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
56995e6fb02SRichard Genoud limit = mult_frac(device_pebs, max_beb_per1024, 1024);
57095e6fb02SRichard Genoud
57195e6fb02SRichard Genoud /* Round it up */
57295e6fb02SRichard Genoud if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
57395e6fb02SRichard Genoud limit += 1;
57495e6fb02SRichard Genoud
57595e6fb02SRichard Genoud return limit;
57695e6fb02SRichard Genoud }
57795e6fb02SRichard Genoud
578505d1caaSArtem Bityutskiy /**
57985c6e6e2SArtem Bityutskiy * io_init - initialize I/O sub-system for a given UBI device.
580801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
581256334c3SRichard Genoud * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
582801c135cSArtem B. Bityutskiy *
583801c135cSArtem B. Bityutskiy * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
584801c135cSArtem B. Bityutskiy * assumed:
585801c135cSArtem B. Bityutskiy * o EC header is always at offset zero - this cannot be changed;
586801c135cSArtem B. Bityutskiy * o VID header starts just after the EC header at the closest address
587cdfa788aSArtem Bityutskiy * aligned to @io->hdrs_min_io_size;
588801c135cSArtem B. Bityutskiy * o data starts just after the VID header at the closest address aligned to
589cdfa788aSArtem Bityutskiy * @io->min_io_size
590801c135cSArtem B. Bityutskiy *
591801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in
592801c135cSArtem B. Bityutskiy * case of failure.
593801c135cSArtem B. Bityutskiy */
io_init(struct ubi_device * ubi,int max_beb_per1024)594256334c3SRichard Genoud static int io_init(struct ubi_device *ubi, int max_beb_per1024)
595801c135cSArtem B. Bityutskiy {
596719bb840SArtem Bityutskiy dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
597719bb840SArtem Bityutskiy dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
598719bb840SArtem Bityutskiy
599801c135cSArtem B. Bityutskiy if (ubi->mtd->numeraseregions != 0) {
600801c135cSArtem B. Bityutskiy /*
601801c135cSArtem B. Bityutskiy * Some flashes have several erase regions. Different regions
602801c135cSArtem B. Bityutskiy * may have different eraseblock size and other
603801c135cSArtem B. Bityutskiy * characteristics. It looks like mostly multi-region flashes
604801c135cSArtem B. Bityutskiy * have one "main" region and one or more small regions to
605801c135cSArtem B. Bityutskiy * store boot loader code or boot parameters or whatever. I
606801c135cSArtem B. Bityutskiy * guess we should just pick the largest region. But this is
607801c135cSArtem B. Bityutskiy * not implemented.
608801c135cSArtem B. Bityutskiy */
60932608703STanya Brokhman ubi_err(ubi, "multiple regions, not implemented");
610801c135cSArtem B. Bityutskiy return -EINVAL;
611801c135cSArtem B. Bityutskiy }
612801c135cSArtem B. Bityutskiy
613dd38fccfSArtem Bityutskiy if (ubi->vid_hdr_offset < 0)
614cdfa788aSArtem Bityutskiy return -EINVAL;
615cdfa788aSArtem Bityutskiy
616801c135cSArtem B. Bityutskiy /*
617801c135cSArtem B. Bityutskiy * Note, in this implementation we support MTD devices with 0x7FFFFFFF
618801c135cSArtem B. Bityutskiy * physical eraseblocks maximum.
619801c135cSArtem B. Bityutskiy */
620801c135cSArtem B. Bityutskiy
621801c135cSArtem B. Bityutskiy ubi->peb_size = ubi->mtd->erasesize;
62269423d99SAdrian Hunter ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
623801c135cSArtem B. Bityutskiy ubi->flash_size = ubi->mtd->size;
624801c135cSArtem B. Bityutskiy
6258beeb3bbSShmulik Ladkani if (mtd_can_have_bb(ubi->mtd)) {
626801c135cSArtem B. Bityutskiy ubi->bad_allowed = 1;
62795e6fb02SRichard Genoud ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
6288beeb3bbSShmulik Ladkani }
629801c135cSArtem B. Bityutskiy
630f669e74bSPratyush Yadav if (ubi->mtd->type == MTD_NORFLASH)
631ebf53f42SArtem Bityutskiy ubi->nor_flash = 1;
632ebf53f42SArtem Bityutskiy
633801c135cSArtem B. Bityutskiy ubi->min_io_size = ubi->mtd->writesize;
634801c135cSArtem B. Bityutskiy ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
635801c135cSArtem B. Bityutskiy
636cadb40ccSKyungmin Park /*
637cadb40ccSKyungmin Park * Make sure minimal I/O unit is power of 2. Note, there is no
638cadb40ccSKyungmin Park * fundamental reason for this assumption. It is just an optimization
639cadb40ccSKyungmin Park * which allows us to avoid costly division operations.
640cadb40ccSKyungmin Park */
6417753f169SVignesh Babu if (!is_power_of_2(ubi->min_io_size)) {
64232608703STanya Brokhman ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
64301f7b309SArtem Bityutskiy ubi->min_io_size);
644801c135cSArtem B. Bityutskiy return -EINVAL;
645801c135cSArtem B. Bityutskiy }
646801c135cSArtem B. Bityutskiy
647801c135cSArtem B. Bityutskiy ubi_assert(ubi->hdrs_min_io_size > 0);
648801c135cSArtem B. Bityutskiy ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
649801c135cSArtem B. Bityutskiy ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
650801c135cSArtem B. Bityutskiy
65130b542efSArtem Bityutskiy ubi->max_write_size = ubi->mtd->writebufsize;
65230b542efSArtem Bityutskiy /*
65330b542efSArtem Bityutskiy * Maximum write size has to be greater or equivalent to min. I/O
65430b542efSArtem Bityutskiy * size, and be multiple of min. I/O size.
65530b542efSArtem Bityutskiy */
65630b542efSArtem Bityutskiy if (ubi->max_write_size < ubi->min_io_size ||
65730b542efSArtem Bityutskiy ubi->max_write_size % ubi->min_io_size ||
65830b542efSArtem Bityutskiy !is_power_of_2(ubi->max_write_size)) {
65932608703STanya Brokhman ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
66030b542efSArtem Bityutskiy ubi->max_write_size, ubi->min_io_size);
66130b542efSArtem Bityutskiy return -EINVAL;
66230b542efSArtem Bityutskiy }
66330b542efSArtem Bityutskiy
664801c135cSArtem B. Bityutskiy /* Calculate default aligned sizes of EC and VID headers */
665801c135cSArtem B. Bityutskiy ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
666801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
667801c135cSArtem B. Bityutskiy
668719bb840SArtem Bityutskiy dbg_gen("min_io_size %d", ubi->min_io_size);
669719bb840SArtem Bityutskiy dbg_gen("max_write_size %d", ubi->max_write_size);
670719bb840SArtem Bityutskiy dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
671719bb840SArtem Bityutskiy dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
672719bb840SArtem Bityutskiy dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
673801c135cSArtem B. Bityutskiy
674801c135cSArtem B. Bityutskiy if (ubi->vid_hdr_offset == 0)
675801c135cSArtem B. Bityutskiy /* Default offset */
676801c135cSArtem B. Bityutskiy ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
677801c135cSArtem B. Bityutskiy ubi->ec_hdr_alsize;
678801c135cSArtem B. Bityutskiy else {
679801c135cSArtem B. Bityutskiy ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
680801c135cSArtem B. Bityutskiy ~(ubi->hdrs_min_io_size - 1);
681801c135cSArtem B. Bityutskiy ubi->vid_hdr_shift = ubi->vid_hdr_offset -
682801c135cSArtem B. Bityutskiy ubi->vid_hdr_aloffset;
683801c135cSArtem B. Bityutskiy }
684801c135cSArtem B. Bityutskiy
6851e020e1bSZhihao Cheng /*
6861e020e1bSZhihao Cheng * Memory allocation for VID header is ubi->vid_hdr_alsize
6871e020e1bSZhihao Cheng * which is described in comments in io.c.
6881e020e1bSZhihao Cheng * Make sure VID header shift + UBI_VID_HDR_SIZE not exceeds
6891e020e1bSZhihao Cheng * ubi->vid_hdr_alsize, so that all vid header operations
6901e020e1bSZhihao Cheng * won't access memory out of bounds.
6911e020e1bSZhihao Cheng */
6921e020e1bSZhihao Cheng if ((ubi->vid_hdr_shift + UBI_VID_HDR_SIZE) > ubi->vid_hdr_alsize) {
6931e020e1bSZhihao Cheng ubi_err(ubi, "Invalid VID header offset %d, VID header shift(%d)"
6941e020e1bSZhihao Cheng " + VID header size(%zu) > VID header aligned size(%d).",
6951e020e1bSZhihao Cheng ubi->vid_hdr_offset, ubi->vid_hdr_shift,
6961e020e1bSZhihao Cheng UBI_VID_HDR_SIZE, ubi->vid_hdr_alsize);
6971e020e1bSZhihao Cheng return -EINVAL;
6981e020e1bSZhihao Cheng }
6991e020e1bSZhihao Cheng
700801c135cSArtem B. Bityutskiy /* Similar for the data offset */
701e8cfe009SJohn Ogness ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
702801c135cSArtem B. Bityutskiy ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
703801c135cSArtem B. Bityutskiy
704719bb840SArtem Bityutskiy dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
705719bb840SArtem Bityutskiy dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
706719bb840SArtem Bityutskiy dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
707719bb840SArtem Bityutskiy dbg_gen("leb_start %d", ubi->leb_start);
708801c135cSArtem B. Bityutskiy
709801c135cSArtem B. Bityutskiy /* The shift must be aligned to 32-bit boundary */
710801c135cSArtem B. Bityutskiy if (ubi->vid_hdr_shift % 4) {
71132608703STanya Brokhman ubi_err(ubi, "unaligned VID header shift %d",
712801c135cSArtem B. Bityutskiy ubi->vid_hdr_shift);
713801c135cSArtem B. Bityutskiy return -EINVAL;
714801c135cSArtem B. Bityutskiy }
715801c135cSArtem B. Bityutskiy
716801c135cSArtem B. Bityutskiy /* Check sanity */
717801c135cSArtem B. Bityutskiy if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
718801c135cSArtem B. Bityutskiy ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
719801c135cSArtem B. Bityutskiy ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
720cadb40ccSKyungmin Park ubi->leb_start & (ubi->min_io_size - 1)) {
72132608703STanya Brokhman ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
722801c135cSArtem B. Bityutskiy ubi->vid_hdr_offset, ubi->leb_start);
723801c135cSArtem B. Bityutskiy return -EINVAL;
724801c135cSArtem B. Bityutskiy }
725801c135cSArtem B. Bityutskiy
726801c135cSArtem B. Bityutskiy /*
727b86a2c56SArtem Bityutskiy * Set maximum amount of physical erroneous eraseblocks to be 10%.
728b86a2c56SArtem Bityutskiy * Erroneous PEB are those which have read errors.
729b86a2c56SArtem Bityutskiy */
730b86a2c56SArtem Bityutskiy ubi->max_erroneous = ubi->peb_count / 10;
731b86a2c56SArtem Bityutskiy if (ubi->max_erroneous < 16)
732b86a2c56SArtem Bityutskiy ubi->max_erroneous = 16;
733719bb840SArtem Bityutskiy dbg_gen("max_erroneous %d", ubi->max_erroneous);
734b86a2c56SArtem Bityutskiy
735b86a2c56SArtem Bityutskiy /*
736801c135cSArtem B. Bityutskiy * It may happen that EC and VID headers are situated in one minimal
737801c135cSArtem B. Bityutskiy * I/O unit. In this case we can only accept this UBI image in
738801c135cSArtem B. Bityutskiy * read-only mode.
739801c135cSArtem B. Bityutskiy */
740801c135cSArtem B. Bityutskiy if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
74132608703STanya Brokhman ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
742801c135cSArtem B. Bityutskiy ubi->ro_mode = 1;
743801c135cSArtem B. Bityutskiy }
744801c135cSArtem B. Bityutskiy
745801c135cSArtem B. Bityutskiy ubi->leb_size = ubi->peb_size - ubi->leb_start;
746801c135cSArtem B. Bityutskiy
747801c135cSArtem B. Bityutskiy if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
74832608703STanya Brokhman ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
749049333ceSArtem Bityutskiy ubi->mtd->index);
750801c135cSArtem B. Bityutskiy ubi->ro_mode = 1;
751801c135cSArtem B. Bityutskiy }
752801c135cSArtem B. Bityutskiy
753801c135cSArtem B. Bityutskiy /*
754fbd0107fSArtem Bityutskiy * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
755801c135cSArtem B. Bityutskiy * unfortunately, MTD does not provide this information. We should loop
756801c135cSArtem B. Bityutskiy * over all physical eraseblocks and invoke mtd->block_is_bad() for
757fbd0107fSArtem Bityutskiy * each physical eraseblock. So, we leave @ubi->bad_peb_count
758fbd0107fSArtem Bityutskiy * uninitialized so far.
759801c135cSArtem B. Bityutskiy */
760801c135cSArtem B. Bityutskiy
761801c135cSArtem B. Bityutskiy return 0;
762801c135cSArtem B. Bityutskiy }
763801c135cSArtem B. Bityutskiy
764801c135cSArtem B. Bityutskiy /**
7654ccf8cffSArtem Bityutskiy * autoresize - re-size the volume which has the "auto-resize" flag set.
7664ccf8cffSArtem Bityutskiy * @ubi: UBI device description object
7674ccf8cffSArtem Bityutskiy * @vol_id: ID of the volume to re-size
7684ccf8cffSArtem Bityutskiy *
769fbd0107fSArtem Bityutskiy * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
7704ccf8cffSArtem Bityutskiy * the volume table to the largest possible size. See comments in ubi-header.h
7714ccf8cffSArtem Bityutskiy * for more description of the flag. Returns zero in case of success and a
7724ccf8cffSArtem Bityutskiy * negative error code in case of failure.
7734ccf8cffSArtem Bityutskiy */
autoresize(struct ubi_device * ubi,int vol_id)7744ccf8cffSArtem Bityutskiy static int autoresize(struct ubi_device *ubi, int vol_id)
7754ccf8cffSArtem Bityutskiy {
7764ccf8cffSArtem Bityutskiy struct ubi_volume_desc desc;
7774ccf8cffSArtem Bityutskiy struct ubi_volume *vol = ubi->volumes[vol_id];
7784ccf8cffSArtem Bityutskiy int err, old_reserved_pebs = vol->reserved_pebs;
7794ccf8cffSArtem Bityutskiy
780abb3e011SArtem Bityutskiy if (ubi->ro_mode) {
78132608703STanya Brokhman ubi_warn(ubi, "skip auto-resize because of R/O mode");
782abb3e011SArtem Bityutskiy return 0;
783abb3e011SArtem Bityutskiy }
784abb3e011SArtem Bityutskiy
7854ccf8cffSArtem Bityutskiy /*
7864ccf8cffSArtem Bityutskiy * Clear the auto-resize flag in the volume in-memory copy of the
787505d1caaSArtem Bityutskiy * volume table, and 'ubi_resize_volume()' will propagate this change
7884ccf8cffSArtem Bityutskiy * to the flash.
7894ccf8cffSArtem Bityutskiy */
7904ccf8cffSArtem Bityutskiy ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
7914ccf8cffSArtem Bityutskiy
7924ccf8cffSArtem Bityutskiy if (ubi->avail_pebs == 0) {
7934ccf8cffSArtem Bityutskiy struct ubi_vtbl_record vtbl_rec;
7944ccf8cffSArtem Bityutskiy
7954ccf8cffSArtem Bityutskiy /*
796505d1caaSArtem Bityutskiy * No available PEBs to re-size the volume, clear the flag on
7974ccf8cffSArtem Bityutskiy * flash and exit.
7984ccf8cffSArtem Bityutskiy */
799d856c13cSEzequiel Garcia vtbl_rec = ubi->vtbl[vol_id];
8004ccf8cffSArtem Bityutskiy err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
8014ccf8cffSArtem Bityutskiy if (err)
80232608703STanya Brokhman ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
8034ccf8cffSArtem Bityutskiy vol_id);
8044ccf8cffSArtem Bityutskiy } else {
8054ccf8cffSArtem Bityutskiy desc.vol = vol;
8064ccf8cffSArtem Bityutskiy err = ubi_resize_volume(&desc,
8074ccf8cffSArtem Bityutskiy old_reserved_pebs + ubi->avail_pebs);
8084ccf8cffSArtem Bityutskiy if (err)
80932608703STanya Brokhman ubi_err(ubi, "cannot auto-resize volume %d",
81032608703STanya Brokhman vol_id);
8114ccf8cffSArtem Bityutskiy }
8124ccf8cffSArtem Bityutskiy
8134ccf8cffSArtem Bityutskiy if (err)
8144ccf8cffSArtem Bityutskiy return err;
8154ccf8cffSArtem Bityutskiy
81632608703STanya Brokhman ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
81732608703STanya Brokhman vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
8184ccf8cffSArtem Bityutskiy return 0;
8194ccf8cffSArtem Bityutskiy }
8204ccf8cffSArtem Bityutskiy
8214ccf8cffSArtem Bityutskiy /**
822cdfa788aSArtem Bityutskiy * ubi_attach_mtd_dev - attach an MTD device.
823ebaaf1afSArtem Bityutskiy * @mtd: MTD device description object
824897a316cSArtem Bityutskiy * @ubi_num: number to assign to the new UBI device
825801c135cSArtem B. Bityutskiy * @vid_hdr_offset: VID header offset
826edac493dSRichard Genoud * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
827669d2044SZhihao Cheng * @disable_fm: whether disable fastmap
828801c135cSArtem B. Bityutskiy *
829897a316cSArtem Bityutskiy * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
830897a316cSArtem Bityutskiy * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
831505d1caaSArtem Bityutskiy * which case this function finds a vacant device number and assigns it
832897a316cSArtem Bityutskiy * automatically. Returns the new UBI device number in case of success and a
833897a316cSArtem Bityutskiy * negative error code in case of failure.
834cdfa788aSArtem Bityutskiy *
835669d2044SZhihao Cheng * If @disable_fm is true, ubi doesn't create new fastmap even the module param
836669d2044SZhihao Cheng * 'fm_autoconvert' is set, and existed old fastmap will be destroyed after
837669d2044SZhihao Cheng * doing full scanning.
838669d2044SZhihao Cheng *
839cdfa788aSArtem Bityutskiy * Note, the invocations of this function has to be serialized by the
840cdfa788aSArtem Bityutskiy * @ubi_devices_mutex.
841801c135cSArtem B. Bityutskiy */
ubi_attach_mtd_dev(struct mtd_info * mtd,int ubi_num,int vid_hdr_offset,int max_beb_per1024,bool disable_fm)842256334c3SRichard Genoud int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
843669d2044SZhihao Cheng int vid_hdr_offset, int max_beb_per1024, bool disable_fm)
844801c135cSArtem B. Bityutskiy {
845801c135cSArtem B. Bityutskiy struct ubi_device *ubi;
846493cfaeaSLogan Gunthorpe int i, err;
847801c135cSArtem B. Bityutskiy
848d2f588f9SRichard Genoud if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
849d2f588f9SRichard Genoud return -EINVAL;
850d2f588f9SRichard Genoud
851d2f588f9SRichard Genoud if (!max_beb_per1024)
852d2f588f9SRichard Genoud max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
853d2f588f9SRichard Genoud
854801c135cSArtem B. Bityutskiy /*
855cdfa788aSArtem Bityutskiy * Check if we already have the same MTD device attached.
856cdfa788aSArtem Bityutskiy *
857cdfa788aSArtem Bityutskiy * Note, this function assumes that UBI devices creations and deletions
858cdfa788aSArtem Bityutskiy * are serialized, so it does not take the &ubi_devices_lock.
859801c135cSArtem B. Bityutskiy */
860897a316cSArtem Bityutskiy for (i = 0; i < UBI_MAX_DEVICES; i++) {
861b96bf4c3SArtem Bityutskiy ubi = ubi_devices[i];
862cdfa788aSArtem Bityutskiy if (ubi && mtd->index == ubi->mtd->index) {
863a51b7ccfSBen Dooks pr_err("ubi: mtd%d is already attached to ubi%d\n",
864801c135cSArtem B. Bityutskiy mtd->index, i);
865897a316cSArtem Bityutskiy return -EEXIST;
866897a316cSArtem Bityutskiy }
867897a316cSArtem Bityutskiy }
868897a316cSArtem Bityutskiy
869897a316cSArtem Bityutskiy /*
870897a316cSArtem Bityutskiy * Make sure this MTD device is not emulated on top of an UBI volume
871897a316cSArtem Bityutskiy * already. Well, generally this recursion works fine, but there are
872897a316cSArtem Bityutskiy * different problems like the UBI module takes a reference to itself
873897a316cSArtem Bityutskiy * by attaching (and thus, opening) the emulated MTD device. This
874897a316cSArtem Bityutskiy * results in inability to unload the module. And in general it makes
875897a316cSArtem Bityutskiy * no sense to attach emulated MTD devices, so we prohibit this.
876897a316cSArtem Bityutskiy */
877897a316cSArtem Bityutskiy if (mtd->type == MTD_UBIVOLUME) {
878a51b7ccfSBen Dooks pr_err("ubi: refuse attaching mtd%d - it is already emulated on top of UBI\n",
879049333ceSArtem Bityutskiy mtd->index);
880cdfa788aSArtem Bityutskiy return -EINVAL;
881cdfa788aSArtem Bityutskiy }
882cdfa788aSArtem Bityutskiy
883b5094b7fSRichard Weinberger /*
884b5094b7fSRichard Weinberger * Both UBI and UBIFS have been designed for SLC NAND and NOR flashes.
885b5094b7fSRichard Weinberger * MLC NAND is different and needs special care, otherwise UBI or UBIFS
886b5094b7fSRichard Weinberger * will die soon and you will lose all your data.
88766aaba3aSBoris Brezillon * Relax this rule if the partition we're attaching to operates in SLC
88866aaba3aSBoris Brezillon * mode.
889b5094b7fSRichard Weinberger */
89066aaba3aSBoris Brezillon if (mtd->type == MTD_MLCNANDFLASH &&
89166aaba3aSBoris Brezillon !(mtd->flags & MTD_SLC_ON_MLC_EMULATION)) {
892b5094b7fSRichard Weinberger pr_err("ubi: refuse attaching mtd%d - MLC NAND is not supported\n",
893b5094b7fSRichard Weinberger mtd->index);
894b5094b7fSRichard Weinberger return -EINVAL;
895b5094b7fSRichard Weinberger }
896b5094b7fSRichard Weinberger
897017c73a3SZhihao Cheng /* UBI cannot work on flashes with zero erasesize. */
898017c73a3SZhihao Cheng if (!mtd->erasesize) {
899017c73a3SZhihao Cheng pr_err("ubi: refuse attaching mtd%d - zero erasesize flash is not supported\n",
900017c73a3SZhihao Cheng mtd->index);
901017c73a3SZhihao Cheng return -EINVAL;
902017c73a3SZhihao Cheng }
903017c73a3SZhihao Cheng
904897a316cSArtem Bityutskiy if (ubi_num == UBI_DEV_NUM_AUTO) {
905cdfa788aSArtem Bityutskiy /* Search for an empty slot in the @ubi_devices array */
906897a316cSArtem Bityutskiy for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
907897a316cSArtem Bityutskiy if (!ubi_devices[ubi_num])
908cdfa788aSArtem Bityutskiy break;
909897a316cSArtem Bityutskiy if (ubi_num == UBI_MAX_DEVICES) {
910a51b7ccfSBen Dooks pr_err("ubi: only %d UBI devices may be created\n",
9119c9ec147SArtem Bityutskiy UBI_MAX_DEVICES);
912cdfa788aSArtem Bityutskiy return -ENFILE;
913801c135cSArtem B. Bityutskiy }
914897a316cSArtem Bityutskiy } else {
915897a316cSArtem Bityutskiy if (ubi_num >= UBI_MAX_DEVICES)
916897a316cSArtem Bityutskiy return -EINVAL;
917897a316cSArtem Bityutskiy
918897a316cSArtem Bityutskiy /* Make sure ubi_num is not busy */
919897a316cSArtem Bityutskiy if (ubi_devices[ubi_num]) {
920a51b7ccfSBen Dooks pr_err("ubi: ubi%i already exists\n", ubi_num);
921897a316cSArtem Bityutskiy return -EEXIST;
922897a316cSArtem Bityutskiy }
923897a316cSArtem Bityutskiy }
924801c135cSArtem B. Bityutskiy
925b96bf4c3SArtem Bityutskiy ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
926cdfa788aSArtem Bityutskiy if (!ubi)
927cdfa788aSArtem Bityutskiy return -ENOMEM;
928801c135cSArtem B. Bityutskiy
929493cfaeaSLogan Gunthorpe device_initialize(&ubi->dev);
930493cfaeaSLogan Gunthorpe ubi->dev.release = dev_release;
931493cfaeaSLogan Gunthorpe ubi->dev.class = &ubi_class;
932493cfaeaSLogan Gunthorpe ubi->dev.groups = ubi_dev_groups;
9331ecf9e39SDaniel Golle ubi->dev.parent = &mtd->dev;
934493cfaeaSLogan Gunthorpe
935801c135cSArtem B. Bityutskiy ubi->mtd = mtd;
936897a316cSArtem Bityutskiy ubi->ubi_num = ubi_num;
937801c135cSArtem B. Bityutskiy ubi->vid_hdr_offset = vid_hdr_offset;
9384ccf8cffSArtem Bityutskiy ubi->autoresize_vol_id = -1;
9394ccf8cffSArtem Bityutskiy
94077e6c2f0SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
94177e6c2f0SRichard Weinberger ubi->fm_pool.used = ubi->fm_pool.size = 0;
94277e6c2f0SRichard Weinberger ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
94377e6c2f0SRichard Weinberger
94477e6c2f0SRichard Weinberger /*
94577e6c2f0SRichard Weinberger * fm_pool.max_size is 5% of the total number of PEBs but it's also
94677e6c2f0SRichard Weinberger * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
94777e6c2f0SRichard Weinberger */
94877e6c2f0SRichard Weinberger ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
94977e6c2f0SRichard Weinberger ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
950212240dfSshengyong ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
951212240dfSshengyong UBI_FM_MIN_POOL_SIZE);
95277e6c2f0SRichard Weinberger
95368e3226bSRichard Weinberger ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
954669d2044SZhihao Cheng ubi->fm_disabled = (!fm_autoconvert || disable_fm) ? 1 : 0;
955479c2c0cSRichard Weinberger if (fm_debug)
956479c2c0cSRichard Weinberger ubi_enable_dbg_chk_fastmap(ubi);
95777e6c2f0SRichard Weinberger
95877e6c2f0SRichard Weinberger if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
95977e6c2f0SRichard Weinberger <= UBI_FM_MAX_START) {
96032608703STanya Brokhman ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
96177e6c2f0SRichard Weinberger UBI_FM_MAX_START);
96277e6c2f0SRichard Weinberger ubi->fm_disabled = 1;
96377e6c2f0SRichard Weinberger }
96477e6c2f0SRichard Weinberger
96532608703STanya Brokhman ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
96632608703STanya Brokhman ubi_msg(ubi, "default fastmap WL pool size: %d",
96732608703STanya Brokhman ubi->fm_wl_pool.max_size);
96877e6c2f0SRichard Weinberger #else
96977e6c2f0SRichard Weinberger ubi->fm_disabled = 1;
97077e6c2f0SRichard Weinberger #endif
9714ccf8cffSArtem Bityutskiy mutex_init(&ubi->buf_mutex);
9724ccf8cffSArtem Bityutskiy mutex_init(&ubi->ckvol_mutex);
973f089c0b2SArtem Bityutskiy mutex_init(&ubi->device_mutex);
9744ccf8cffSArtem Bityutskiy spin_lock_init(&ubi->volumes_lock);
975111ab0b2SRichard Weinberger init_rwsem(&ubi->fm_protect);
976111ab0b2SRichard Weinberger init_rwsem(&ubi->fm_eba_sem);
977cdfa788aSArtem Bityutskiy
97845fc5c81STanya Brokhman ubi_msg(ubi, "attaching mtd%d", mtd->index);
979cdfa788aSArtem Bityutskiy
980256334c3SRichard Genoud err = io_init(ubi, max_beb_per1024);
981801c135cSArtem B. Bityutskiy if (err)
982801c135cSArtem B. Bityutskiy goto out_free;
983801c135cSArtem B. Bityutskiy
984ad5942baSStefan Roese err = -ENOMEM;
9850ca39d74SArtem Bityutskiy ubi->peb_buf = vmalloc(ubi->peb_size);
9860ca39d74SArtem Bityutskiy if (!ubi->peb_buf)
987e88d6e10SArtem Bityutskiy goto out_free;
988e88d6e10SArtem Bityutskiy
98977e6c2f0SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
99077e6c2f0SRichard Weinberger ubi->fm_size = ubi_calc_fm_size(ubi);
99177e6c2f0SRichard Weinberger ubi->fm_buf = vzalloc(ubi->fm_size);
99277e6c2f0SRichard Weinberger if (!ubi->fm_buf)
99377e6c2f0SRichard Weinberger goto out_free;
99477e6c2f0SRichard Weinberger #endif
995669d2044SZhihao Cheng err = ubi_attach(ubi, disable_fm ? 1 : 0);
996801c135cSArtem B. Bityutskiy if (err) {
99732608703STanya Brokhman ubi_err(ubi, "failed to attach mtd%d, error %d",
99832608703STanya Brokhman mtd->index, err);
999eab73772SEzequiel Garcia goto out_free;
1000801c135cSArtem B. Bityutskiy }
1001801c135cSArtem B. Bityutskiy
10024ccf8cffSArtem Bityutskiy if (ubi->autoresize_vol_id != -1) {
10034ccf8cffSArtem Bityutskiy err = autoresize(ubi, ubi->autoresize_vol_id);
10044ccf8cffSArtem Bityutskiy if (err)
10054ccf8cffSArtem Bityutskiy goto out_detach;
10064ccf8cffSArtem Bityutskiy }
10074ccf8cffSArtem Bityutskiy
1008493cfaeaSLogan Gunthorpe err = uif_init(ubi);
1009801c135cSArtem B. Bityutskiy if (err)
10100bf1c439SArtem Bityutskiy goto out_detach;
1011801c135cSArtem B. Bityutskiy
10122a734bb8SArtem Bityutskiy err = ubi_debugfs_init_dev(ubi);
10132a734bb8SArtem Bityutskiy if (err)
10142a734bb8SArtem Bityutskiy goto out_uif;
10152a734bb8SArtem Bityutskiy
1016f170168bSKees Cook ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
1017cdfa788aSArtem Bityutskiy if (IS_ERR(ubi->bgt_thread)) {
1018cdfa788aSArtem Bityutskiy err = PTR_ERR(ubi->bgt_thread);
101932608703STanya Brokhman ubi_err(ubi, "cannot spawn \"%s\", error %d",
102032608703STanya Brokhman ubi->bgt_name, err);
10212a734bb8SArtem Bityutskiy goto out_debugfs;
1022cdfa788aSArtem Bityutskiy }
1023cdfa788aSArtem Bityutskiy
102432608703STanya Brokhman ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
102532608703STanya Brokhman mtd->index, mtd->name, ubi->flash_size >> 20);
102632608703STanya Brokhman ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
1027719bb840SArtem Bityutskiy ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
102832608703STanya Brokhman ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
1029719bb840SArtem Bityutskiy ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
103032608703STanya Brokhman ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
1031719bb840SArtem Bityutskiy ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
103232608703STanya Brokhman ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
1033719bb840SArtem Bityutskiy ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
103432608703STanya Brokhman ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
1035719bb840SArtem Bityutskiy ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
1036719bb840SArtem Bityutskiy ubi->vtbl_slots);
103732608703STanya Brokhman ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
1038719bb840SArtem Bityutskiy ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
1039719bb840SArtem Bityutskiy ubi->image_seq);
104032608703STanya Brokhman ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
1041719bb840SArtem Bityutskiy ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
1042801c135cSArtem B. Bityutskiy
1043ddbd3b61SArtem Bityutskiy /*
1044ddbd3b61SArtem Bityutskiy * The below lock makes sure we do not race with 'ubi_thread()' which
1045ddbd3b61SArtem Bityutskiy * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
1046ddbd3b61SArtem Bityutskiy */
1047ddbd3b61SArtem Bityutskiy spin_lock(&ubi->wl_lock);
1048801c135cSArtem B. Bityutskiy ubi->thread_enabled = 1;
1049801c135cSArtem B. Bityutskiy wake_up_process(ubi->bgt_thread);
1050ddbd3b61SArtem Bityutskiy spin_unlock(&ubi->wl_lock);
1051801c135cSArtem B. Bityutskiy
10523cbf0e39SBaokun Li ubi_devices[ubi_num] = ubi;
10530e0ee1ccSDmitry Pervushin ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
1054897a316cSArtem Bityutskiy return ubi_num;
1055801c135cSArtem B. Bityutskiy
10562a734bb8SArtem Bityutskiy out_debugfs:
10572a734bb8SArtem Bityutskiy ubi_debugfs_exit_dev(ubi);
1058cdfa788aSArtem Bityutskiy out_uif:
1059cdfa788aSArtem Bityutskiy uif_close(ubi);
1060801c135cSArtem B. Bityutskiy out_detach:
1061801c135cSArtem B. Bityutskiy ubi_wl_close(ubi);
1062fc55dacfSHou Tao ubi_free_all_volumes(ubi);
1063d7f0c4dcSVinit Agnihotri vfree(ubi->vtbl);
1064801c135cSArtem B. Bityutskiy out_free:
10650ca39d74SArtem Bityutskiy vfree(ubi->peb_buf);
106677e6c2f0SRichard Weinberger vfree(ubi->fm_buf);
10670bf1c439SArtem Bityutskiy put_device(&ubi->dev);
1068801c135cSArtem B. Bityutskiy return err;
1069801c135cSArtem B. Bityutskiy }
1070801c135cSArtem B. Bityutskiy
1071801c135cSArtem B. Bityutskiy /**
1072cdfa788aSArtem Bityutskiy * ubi_detach_mtd_dev - detach an MTD device.
1073cdfa788aSArtem Bityutskiy * @ubi_num: UBI device number to detach from
1074cdfa788aSArtem Bityutskiy * @anyway: detach MTD even if device reference count is not zero
1075cdfa788aSArtem Bityutskiy *
1076cdfa788aSArtem Bityutskiy * This function destroys an UBI device number @ubi_num and detaches the
1077cdfa788aSArtem Bityutskiy * underlying MTD device. Returns zero in case of success and %-EBUSY if the
1078cdfa788aSArtem Bityutskiy * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
1079cdfa788aSArtem Bityutskiy * exist.
1080cdfa788aSArtem Bityutskiy *
1081cdfa788aSArtem Bityutskiy * Note, the invocations of this function has to be serialized by the
1082cdfa788aSArtem Bityutskiy * @ubi_devices_mutex.
1083801c135cSArtem B. Bityutskiy */
ubi_detach_mtd_dev(int ubi_num,int anyway)1084cdfa788aSArtem Bityutskiy int ubi_detach_mtd_dev(int ubi_num, int anyway)
1085801c135cSArtem B. Bityutskiy {
1086cdfa788aSArtem Bityutskiy struct ubi_device *ubi;
1087801c135cSArtem B. Bityutskiy
1088cdfa788aSArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
1089cdfa788aSArtem Bityutskiy return -EINVAL;
1090cdfa788aSArtem Bityutskiy
10910e0ee1ccSDmitry Pervushin ubi = ubi_get_device(ubi_num);
10920e0ee1ccSDmitry Pervushin if (!ubi)
1093cdfa788aSArtem Bityutskiy return -EINVAL;
1094cdfa788aSArtem Bityutskiy
10950e0ee1ccSDmitry Pervushin spin_lock(&ubi_devices_lock);
10960e0ee1ccSDmitry Pervushin put_device(&ubi->dev);
10970e0ee1ccSDmitry Pervushin ubi->ref_count -= 1;
1098cdfa788aSArtem Bityutskiy if (ubi->ref_count) {
1099cdfa788aSArtem Bityutskiy if (!anyway) {
1100897a316cSArtem Bityutskiy spin_unlock(&ubi_devices_lock);
1101cdfa788aSArtem Bityutskiy return -EBUSY;
1102cdfa788aSArtem Bityutskiy }
1103cdfa788aSArtem Bityutskiy /* This may only happen if there is a bug */
110432608703STanya Brokhman ubi_err(ubi, "%s reference count %d, destroy anyway",
1105cdfa788aSArtem Bityutskiy ubi->ubi_name, ubi->ref_count);
1106cdfa788aSArtem Bityutskiy }
1107897a316cSArtem Bityutskiy ubi_devices[ubi_num] = NULL;
1108cdfa788aSArtem Bityutskiy spin_unlock(&ubi_devices_lock);
1109cdfa788aSArtem Bityutskiy
1110897a316cSArtem Bityutskiy ubi_assert(ubi_num == ubi->ubi_num);
11110e0ee1ccSDmitry Pervushin ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
111232608703STanya Brokhman ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
111377e6c2f0SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
111477e6c2f0SRichard Weinberger /* If we don't write a new fastmap at detach time we lose all
111524b7a347SRichard Weinberger * EC updates that have been made since the last written fastmap.
111624b7a347SRichard Weinberger * In case of fastmap debugging we omit the update to simulate an
111724b7a347SRichard Weinberger * unclean shutdown. */
111824b7a347SRichard Weinberger if (!ubi_dbg_chk_fastmap(ubi))
111977e6c2f0SRichard Weinberger ubi_update_fastmap(ubi);
112077e6c2f0SRichard Weinberger #endif
1121cdfa788aSArtem Bityutskiy /*
1122cdfa788aSArtem Bityutskiy * Before freeing anything, we have to stop the background thread to
1123cdfa788aSArtem Bityutskiy * prevent it from doing anything on this device while we are freeing.
1124cdfa788aSArtem Bityutskiy */
1125cdfa788aSArtem Bityutskiy if (ubi->bgt_thread)
1126cdfa788aSArtem Bityutskiy kthread_stop(ubi->bgt_thread);
1127cdfa788aSArtem Bityutskiy
11286e7d8016SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
11296e7d8016SRichard Weinberger cancel_work_sync(&ubi->fm_work);
11306e7d8016SRichard Weinberger #endif
11312a734bb8SArtem Bityutskiy ubi_debugfs_exit_dev(ubi);
1132801c135cSArtem B. Bityutskiy uif_close(ubi);
113377e6c2f0SRichard Weinberger
1134801c135cSArtem B. Bityutskiy ubi_wl_close(ubi);
113547e1ec70SArtem Bityutskiy ubi_free_internal_volumes(ubi);
113692ad8f37SArtem Bityutskiy vfree(ubi->vtbl);
11370ca39d74SArtem Bityutskiy vfree(ubi->peb_buf);
113877e6c2f0SRichard Weinberger vfree(ubi->fm_buf);
113932608703STanya Brokhman ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
1140b95f83abSPan Bian put_mtd_device(ubi->mtd);
114136b477d0SArtem Bityutskiy put_device(&ubi->dev);
1142cdfa788aSArtem Bityutskiy return 0;
1143801c135cSArtem B. Bityutskiy }
1144801c135cSArtem B. Bityutskiy
11453a8d4642SArtem Bityutskiy /**
1146f9b0080eSArtem Bityutskiy * open_mtd_by_chdev - open an MTD device by its character device node path.
1147f9b0080eSArtem Bityutskiy * @mtd_dev: MTD character device node path
1148f9b0080eSArtem Bityutskiy *
1149f9b0080eSArtem Bityutskiy * This helper function opens an MTD device by its character node device path.
1150f9b0080eSArtem Bityutskiy * Returns MTD device description object in case of success and a negative
1151f9b0080eSArtem Bityutskiy * error code in case of failure.
1152f9b0080eSArtem Bityutskiy */
open_mtd_by_chdev(const char * mtd_dev)1153f9b0080eSArtem Bityutskiy static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1154f9b0080eSArtem Bityutskiy {
115561edc3f3SRichard Weinberger int err, minor;
11561a498ec4SRichard Weinberger struct path path;
115761edc3f3SRichard Weinberger struct kstat stat;
1158f9b0080eSArtem Bityutskiy
1159f9b0080eSArtem Bityutskiy /* Probably this is an MTD character device node path */
11601a498ec4SRichard Weinberger err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1161f9b0080eSArtem Bityutskiy if (err)
1162f9b0080eSArtem Bityutskiy return ERR_PTR(err);
1163f9b0080eSArtem Bityutskiy
1164a528d35eSDavid Howells err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
11651a498ec4SRichard Weinberger path_put(&path);
116661edc3f3SRichard Weinberger if (err)
116761edc3f3SRichard Weinberger return ERR_PTR(err);
116861edc3f3SRichard Weinberger
116961edc3f3SRichard Weinberger /* MTD device number is defined by the major / minor numbers */
117061edc3f3SRichard Weinberger if (MAJOR(stat.rdev) != MTD_CHAR_MAJOR || !S_ISCHR(stat.mode))
1171f9b0080eSArtem Bityutskiy return ERR_PTR(-EINVAL);
1172f9b0080eSArtem Bityutskiy
117361edc3f3SRichard Weinberger minor = MINOR(stat.rdev);
117461edc3f3SRichard Weinberger
1175f9b0080eSArtem Bityutskiy if (minor & 1)
1176f9b0080eSArtem Bityutskiy /*
1177f9b0080eSArtem Bityutskiy * Just do not think the "/dev/mtdrX" devices support is need,
1178f9b0080eSArtem Bityutskiy * so do not support them to avoid doing extra work.
1179f9b0080eSArtem Bityutskiy */
1180f9b0080eSArtem Bityutskiy return ERR_PTR(-EINVAL);
1181f9b0080eSArtem Bityutskiy
1182f9b0080eSArtem Bityutskiy return get_mtd_device(NULL, minor / 2);
1183f9b0080eSArtem Bityutskiy }
1184f9b0080eSArtem Bityutskiy
1185f9b0080eSArtem Bityutskiy /**
1186f9b0080eSArtem Bityutskiy * open_mtd_device - open MTD device by name, character device path, or number.
1187f9b0080eSArtem Bityutskiy * @mtd_dev: name, character device node path, or MTD device device number
1188cdfa788aSArtem Bityutskiy *
1189d1f3dd6cSArtem Bityutskiy * This function tries to open and MTD device described by @mtd_dev string,
1190f9b0080eSArtem Bityutskiy * which is first treated as ASCII MTD device number, and if it is not true, it
1191f9b0080eSArtem Bityutskiy * is treated as MTD device name, and if that is also not true, it is treated
1192f9b0080eSArtem Bityutskiy * as MTD character device node path. Returns MTD device description object in
1193f9b0080eSArtem Bityutskiy * case of success and a negative error code in case of failure.
1194cdfa788aSArtem Bityutskiy */
open_mtd_device(const char * mtd_dev)1195cdfa788aSArtem Bityutskiy static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1196cdfa788aSArtem Bityutskiy {
1197cdfa788aSArtem Bityutskiy struct mtd_info *mtd;
1198cdfa788aSArtem Bityutskiy int mtd_num;
1199cdfa788aSArtem Bityutskiy char *endp;
1200cdfa788aSArtem Bityutskiy
1201cdfa788aSArtem Bityutskiy mtd_num = simple_strtoul(mtd_dev, &endp, 0);
1202cdfa788aSArtem Bityutskiy if (*endp != '\0' || mtd_dev == endp) {
1203d1f3dd6cSArtem Bityutskiy /*
1204d1f3dd6cSArtem Bityutskiy * This does not look like an ASCII integer, probably this is
1205d1f3dd6cSArtem Bityutskiy * MTD device name.
1206d1f3dd6cSArtem Bityutskiy */
1207d1f3dd6cSArtem Bityutskiy mtd = get_mtd_device_nm(mtd_dev);
120845586c70SMasahiro Yamada if (PTR_ERR(mtd) == -ENODEV)
1209f9b0080eSArtem Bityutskiy /* Probably this is an MTD character device node path */
1210f9b0080eSArtem Bityutskiy mtd = open_mtd_by_chdev(mtd_dev);
1211d1f3dd6cSArtem Bityutskiy } else
1212cdfa788aSArtem Bityutskiy mtd = get_mtd_device(NULL, mtd_num);
1213cdfa788aSArtem Bityutskiy
1214cdfa788aSArtem Bityutskiy return mtd;
1215cdfa788aSArtem Bityutskiy }
1216cdfa788aSArtem Bityutskiy
ubi_init(void)1217801c135cSArtem B. Bityutskiy static int __init ubi_init(void)
1218801c135cSArtem B. Bityutskiy {
1219801c135cSArtem B. Bityutskiy int err, i, k;
1220801c135cSArtem B. Bityutskiy
1221801c135cSArtem B. Bityutskiy /* Ensure that EC and VID headers have correct size */
1222801c135cSArtem B. Bityutskiy BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
1223801c135cSArtem B. Bityutskiy BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
1224801c135cSArtem B. Bityutskiy
1225801c135cSArtem B. Bityutskiy if (mtd_devs > UBI_MAX_DEVICES) {
1226278f31a7SBen Dooks pr_err("UBI error: too many MTD devices, maximum is %d\n",
122732608703STanya Brokhman UBI_MAX_DEVICES);
1228801c135cSArtem B. Bityutskiy return -EINVAL;
1229801c135cSArtem B. Bityutskiy }
1230801c135cSArtem B. Bityutskiy
12319f961b57SArtem Bityutskiy /* Create base sysfs directory and sysfs files */
123253cd255cSTakashi Iwai err = class_register(&ubi_class);
123353cd255cSTakashi Iwai if (err < 0)
123453cd255cSTakashi Iwai return err;
12359f961b57SArtem Bityutskiy
12369f961b57SArtem Bityutskiy err = misc_register(&ubi_ctrl_cdev);
12379f961b57SArtem Bityutskiy if (err) {
1238278f31a7SBen Dooks pr_err("UBI error: cannot register device\n");
123953cd255cSTakashi Iwai goto out;
12409f961b57SArtem Bityutskiy }
1241801c135cSArtem B. Bityutskiy
124206b68ba1SArtem Bityutskiy ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
124306b68ba1SArtem Bityutskiy sizeof(struct ubi_wl_entry),
124406b68ba1SArtem Bityutskiy 0, 0, NULL);
12454d525145SJulia Lawall if (!ubi_wl_entry_slab) {
12464d525145SJulia Lawall err = -ENOMEM;
1247b9a06623SArtem Bityutskiy goto out_dev_unreg;
12484d525145SJulia Lawall }
124906b68ba1SArtem Bityutskiy
12502a734bb8SArtem Bityutskiy err = ubi_debugfs_init();
12512a734bb8SArtem Bityutskiy if (err)
12522a734bb8SArtem Bityutskiy goto out_slab;
12532a734bb8SArtem Bityutskiy
12542a734bb8SArtem Bityutskiy
1255801c135cSArtem B. Bityutskiy /* Attach MTD devices */
1256801c135cSArtem B. Bityutskiy for (i = 0; i < mtd_devs; i++) {
1257801c135cSArtem B. Bityutskiy struct mtd_dev_param *p = &mtd_dev_param[i];
1258cdfa788aSArtem Bityutskiy struct mtd_info *mtd;
1259801c135cSArtem B. Bityutskiy
1260801c135cSArtem B. Bityutskiy cond_resched();
1261cdfa788aSArtem Bityutskiy
1262cdfa788aSArtem Bityutskiy mtd = open_mtd_device(p->name);
1263cdfa788aSArtem Bityutskiy if (IS_ERR(mtd)) {
1264cdfa788aSArtem Bityutskiy err = PTR_ERR(mtd);
1265278f31a7SBen Dooks pr_err("UBI error: cannot open mtd %s, error %d\n",
126632608703STanya Brokhman p->name, err);
12671557b9e1SMike Frysinger /* See comment below re-ubi_is_module(). */
12681557b9e1SMike Frysinger if (ubi_is_module())
1269cdfa788aSArtem Bityutskiy goto out_detach;
12701557b9e1SMike Frysinger continue;
1271cdfa788aSArtem Bityutskiy }
1272cdfa788aSArtem Bityutskiy
1273cdfa788aSArtem Bityutskiy mutex_lock(&ubi_devices_mutex);
127483ff59a0SMike Frysinger err = ubi_attach_mtd_dev(mtd, p->ubi_num,
1275669d2044SZhihao Cheng p->vid_hdr_offs, p->max_beb_per1024,
1276ef503113SYang Li p->enable_fm == 0);
1277cdfa788aSArtem Bityutskiy mutex_unlock(&ubi_devices_mutex);
1278cdfa788aSArtem Bityutskiy if (err < 0) {
1279278f31a7SBen Dooks pr_err("UBI error: cannot attach mtd%d\n",
128032608703STanya Brokhman mtd->index);
1281af7ad7a0SMarc Kleine-Budde put_mtd_device(mtd);
1282af7ad7a0SMarc Kleine-Budde
1283af7ad7a0SMarc Kleine-Budde /*
1284af7ad7a0SMarc Kleine-Budde * Originally UBI stopped initializing on any error.
1285af7ad7a0SMarc Kleine-Budde * However, later on it was found out that this
1286af7ad7a0SMarc Kleine-Budde * behavior is not very good when UBI is compiled into
1287af7ad7a0SMarc Kleine-Budde * the kernel and the MTD devices to attach are passed
1288af7ad7a0SMarc Kleine-Budde * through the command line. Indeed, UBI failure
1289af7ad7a0SMarc Kleine-Budde * stopped whole boot sequence.
1290af7ad7a0SMarc Kleine-Budde *
1291af7ad7a0SMarc Kleine-Budde * To fix this, we changed the behavior for the
1292af7ad7a0SMarc Kleine-Budde * non-module case, but preserved the old behavior for
1293af7ad7a0SMarc Kleine-Budde * the module case, just for compatibility. This is a
1294af7ad7a0SMarc Kleine-Budde * little inconsistent, though.
1295af7ad7a0SMarc Kleine-Budde */
1296af7ad7a0SMarc Kleine-Budde if (ubi_is_module())
1297801c135cSArtem B. Bityutskiy goto out_detach;
1298801c135cSArtem B. Bityutskiy }
12999f961b57SArtem Bityutskiy }
1300801c135cSArtem B. Bityutskiy
13019d54c8a3SEzequiel Garcia err = ubiblock_init();
13029d54c8a3SEzequiel Garcia if (err) {
1303278f31a7SBen Dooks pr_err("UBI error: block: cannot initialize, error %d\n", err);
13049d54c8a3SEzequiel Garcia
13059d54c8a3SEzequiel Garcia /* See comment above re-ubi_is_module(). */
13069d54c8a3SEzequiel Garcia if (ubi_is_module())
13079d54c8a3SEzequiel Garcia goto out_detach;
13089d54c8a3SEzequiel Garcia }
13099d54c8a3SEzequiel Garcia
1310801c135cSArtem B. Bityutskiy return 0;
1311801c135cSArtem B. Bityutskiy
1312801c135cSArtem B. Bityutskiy out_detach:
1313801c135cSArtem B. Bityutskiy for (k = 0; k < i; k++)
1314cdfa788aSArtem Bityutskiy if (ubi_devices[k]) {
1315cdfa788aSArtem Bityutskiy mutex_lock(&ubi_devices_mutex);
1316cdfa788aSArtem Bityutskiy ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
1317cdfa788aSArtem Bityutskiy mutex_unlock(&ubi_devices_mutex);
1318cdfa788aSArtem Bityutskiy }
13192a734bb8SArtem Bityutskiy ubi_debugfs_exit();
13202a734bb8SArtem Bityutskiy out_slab:
132106b68ba1SArtem Bityutskiy kmem_cache_destroy(ubi_wl_entry_slab);
13229f961b57SArtem Bityutskiy out_dev_unreg:
13239f961b57SArtem Bityutskiy misc_deregister(&ubi_ctrl_cdev);
13249f961b57SArtem Bityutskiy out:
132553cd255cSTakashi Iwai class_unregister(&ubi_class);
1326278f31a7SBen Dooks pr_err("UBI error: cannot initialize UBI, error %d\n", err);
1327801c135cSArtem B. Bityutskiy return err;
1328801c135cSArtem B. Bityutskiy }
1329cf38aca5SJiang Lu late_initcall(ubi_init);
1330801c135cSArtem B. Bityutskiy
ubi_exit(void)1331801c135cSArtem B. Bityutskiy static void __exit ubi_exit(void)
1332801c135cSArtem B. Bityutskiy {
1333b96bf4c3SArtem Bityutskiy int i;
1334801c135cSArtem B. Bityutskiy
13359d54c8a3SEzequiel Garcia ubiblock_exit();
13369d54c8a3SEzequiel Garcia
1337b96bf4c3SArtem Bityutskiy for (i = 0; i < UBI_MAX_DEVICES; i++)
1338cdfa788aSArtem Bityutskiy if (ubi_devices[i]) {
1339cdfa788aSArtem Bityutskiy mutex_lock(&ubi_devices_mutex);
1340cdfa788aSArtem Bityutskiy ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
1341cdfa788aSArtem Bityutskiy mutex_unlock(&ubi_devices_mutex);
1342cdfa788aSArtem Bityutskiy }
13432a734bb8SArtem Bityutskiy ubi_debugfs_exit();
134406b68ba1SArtem Bityutskiy kmem_cache_destroy(ubi_wl_entry_slab);
13459f961b57SArtem Bityutskiy misc_deregister(&ubi_ctrl_cdev);
134653cd255cSTakashi Iwai class_unregister(&ubi_class);
1347801c135cSArtem B. Bityutskiy }
1348801c135cSArtem B. Bityutskiy module_exit(ubi_exit);
1349801c135cSArtem B. Bityutskiy
1350801c135cSArtem B. Bityutskiy /**
1351ebaaf1afSArtem Bityutskiy * bytes_str_to_int - convert a number of bytes string into an integer.
1352801c135cSArtem B. Bityutskiy * @str: the string to convert
1353801c135cSArtem B. Bityutskiy *
1354801c135cSArtem B. Bityutskiy * This function returns positive resulting integer in case of success and a
1355801c135cSArtem B. Bityutskiy * negative error code in case of failure.
1356801c135cSArtem B. Bityutskiy */
bytes_str_to_int(const char * str)1357435009d4SAndy Shevchenko static int bytes_str_to_int(const char *str)
1358801c135cSArtem B. Bityutskiy {
1359801c135cSArtem B. Bityutskiy char *endp;
1360801c135cSArtem B. Bityutskiy unsigned long result;
1361801c135cSArtem B. Bityutskiy
1362801c135cSArtem B. Bityutskiy result = simple_strtoul(str, &endp, 0);
1363774b1382SRoel Kluin if (str == endp || result >= INT_MAX) {
136432608703STanya Brokhman pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1365801c135cSArtem B. Bityutskiy return -EINVAL;
1366801c135cSArtem B. Bityutskiy }
1367801c135cSArtem B. Bityutskiy
1368801c135cSArtem B. Bityutskiy switch (*endp) {
1369801c135cSArtem B. Bityutskiy case 'G':
1370801c135cSArtem B. Bityutskiy result *= 1024;
1371025a06c1SMiquel Raynal fallthrough;
1372801c135cSArtem B. Bityutskiy case 'M':
1373801c135cSArtem B. Bityutskiy result *= 1024;
1374025a06c1SMiquel Raynal fallthrough;
1375801c135cSArtem B. Bityutskiy case 'K':
1376801c135cSArtem B. Bityutskiy result *= 1024;
13778aa058d7SGustavo A. R. Silva break;
1378801c135cSArtem B. Bityutskiy case '\0':
1379801c135cSArtem B. Bityutskiy break;
1380801c135cSArtem B. Bityutskiy default:
138132608703STanya Brokhman pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1382801c135cSArtem B. Bityutskiy return -EINVAL;
1383801c135cSArtem B. Bityutskiy }
1384801c135cSArtem B. Bityutskiy
1385801c135cSArtem B. Bityutskiy return result;
1386801c135cSArtem B. Bityutskiy }
1387801c135cSArtem B. Bityutskiy
1388801c135cSArtem B. Bityutskiy /**
1389801c135cSArtem B. Bityutskiy * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1390801c135cSArtem B. Bityutskiy * @val: the parameter value to parse
1391801c135cSArtem B. Bityutskiy * @kp: not used
1392801c135cSArtem B. Bityutskiy *
1393801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in
1394801c135cSArtem B. Bityutskiy * case of error.
1395801c135cSArtem B. Bityutskiy */
ubi_mtd_param_parse(const char * val,const struct kernel_param * kp)1396e4dca7b7SKees Cook static int ubi_mtd_param_parse(const char *val, const struct kernel_param *kp)
1397801c135cSArtem B. Bityutskiy {
1398801c135cSArtem B. Bityutskiy int i, len;
1399801c135cSArtem B. Bityutskiy struct mtd_dev_param *p;
1400801c135cSArtem B. Bityutskiy char buf[MTD_PARAM_LEN_MAX];
1401801c135cSArtem B. Bityutskiy char *pbuf = &buf[0];
140283ff59a0SMike Frysinger char *tokens[MTD_PARAM_MAX_COUNT], *token;
1403801c135cSArtem B. Bityutskiy
140477c722ddSArtem Bityutskiy if (!val)
140577c722ddSArtem Bityutskiy return -EINVAL;
140677c722ddSArtem Bityutskiy
1407801c135cSArtem B. Bityutskiy if (mtd_devs == UBI_MAX_DEVICES) {
140832608703STanya Brokhman pr_err("UBI error: too many parameters, max. is %d\n",
1409801c135cSArtem B. Bityutskiy UBI_MAX_DEVICES);
1410801c135cSArtem B. Bityutskiy return -EINVAL;
1411801c135cSArtem B. Bityutskiy }
1412801c135cSArtem B. Bityutskiy
1413801c135cSArtem B. Bityutskiy len = strnlen(val, MTD_PARAM_LEN_MAX);
1414801c135cSArtem B. Bityutskiy if (len == MTD_PARAM_LEN_MAX) {
141532608703STanya Brokhman pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
1416049333ceSArtem Bityutskiy val, MTD_PARAM_LEN_MAX);
1417801c135cSArtem B. Bityutskiy return -EINVAL;
1418801c135cSArtem B. Bityutskiy }
1419801c135cSArtem B. Bityutskiy
1420801c135cSArtem B. Bityutskiy if (len == 0) {
142145fc5c81STanya Brokhman pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
1422801c135cSArtem B. Bityutskiy return 0;
1423801c135cSArtem B. Bityutskiy }
1424801c135cSArtem B. Bityutskiy
1425801c135cSArtem B. Bityutskiy strcpy(buf, val);
1426801c135cSArtem B. Bityutskiy
1427801c135cSArtem B. Bityutskiy /* Get rid of the final newline */
1428801c135cSArtem B. Bityutskiy if (buf[len - 1] == '\n')
1429503990ebSArtem Bityutskiy buf[len - 1] = '\0';
1430801c135cSArtem B. Bityutskiy
14315993f9b7SRichard Genoud for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
1432801c135cSArtem B. Bityutskiy tokens[i] = strsep(&pbuf, ",");
1433801c135cSArtem B. Bityutskiy
1434801c135cSArtem B. Bityutskiy if (pbuf) {
143532608703STanya Brokhman pr_err("UBI error: too many arguments at \"%s\"\n", val);
1436801c135cSArtem B. Bityutskiy return -EINVAL;
1437801c135cSArtem B. Bityutskiy }
1438801c135cSArtem B. Bityutskiy
1439801c135cSArtem B. Bityutskiy p = &mtd_dev_param[mtd_devs];
1440801c135cSArtem B. Bityutskiy strcpy(&p->name[0], tokens[0]);
1441801c135cSArtem B. Bityutskiy
144283ff59a0SMike Frysinger token = tokens[1];
144383ff59a0SMike Frysinger if (token) {
144483ff59a0SMike Frysinger p->vid_hdr_offs = bytes_str_to_int(token);
1445801c135cSArtem B. Bityutskiy
1446801c135cSArtem B. Bityutskiy if (p->vid_hdr_offs < 0)
1447801c135cSArtem B. Bityutskiy return p->vid_hdr_offs;
144883ff59a0SMike Frysinger }
1449801c135cSArtem B. Bityutskiy
145083ff59a0SMike Frysinger token = tokens[2];
145183ff59a0SMike Frysinger if (token) {
145283ff59a0SMike Frysinger int err = kstrtoint(token, 10, &p->max_beb_per1024);
1453edac493dSRichard Genoud
1454edac493dSRichard Genoud if (err) {
14557af73882SZhaoLong Wang pr_err("UBI error: bad value for max_beb_per1024 parameter: %s\n",
145683ff59a0SMike Frysinger token);
1457edac493dSRichard Genoud return -EINVAL;
1458edac493dSRichard Genoud }
1459edac493dSRichard Genoud }
1460edac493dSRichard Genoud
146183ff59a0SMike Frysinger token = tokens[3];
146283ff59a0SMike Frysinger if (token) {
146383ff59a0SMike Frysinger int err = kstrtoint(token, 10, &p->ubi_num);
146483ff59a0SMike Frysinger
1465*9a6550bbSDenis Arefev if (err || p->ubi_num < UBI_DEV_NUM_AUTO) {
14667af73882SZhaoLong Wang pr_err("UBI error: bad value for ubi_num parameter: %s\n",
146732608703STanya Brokhman token);
146883ff59a0SMike Frysinger return -EINVAL;
146983ff59a0SMike Frysinger }
147083ff59a0SMike Frysinger } else
147183ff59a0SMike Frysinger p->ubi_num = UBI_DEV_NUM_AUTO;
147283ff59a0SMike Frysinger
14737af73882SZhaoLong Wang token = tokens[4];
14747af73882SZhaoLong Wang if (token) {
14757af73882SZhaoLong Wang int err = kstrtoint(token, 10, &p->enable_fm);
14767af73882SZhaoLong Wang
14777af73882SZhaoLong Wang if (err) {
14787af73882SZhaoLong Wang pr_err("UBI error: bad value for enable_fm parameter: %s\n",
14797af73882SZhaoLong Wang token);
14807af73882SZhaoLong Wang return -EINVAL;
14817af73882SZhaoLong Wang }
14827af73882SZhaoLong Wang } else
14837af73882SZhaoLong Wang p->enable_fm = 0;
14847af73882SZhaoLong Wang
1485801c135cSArtem B. Bityutskiy mtd_devs += 1;
1486801c135cSArtem B. Bityutskiy return 0;
1487801c135cSArtem B. Bityutskiy }
1488801c135cSArtem B. Bityutskiy
1489997d30cbSAndy Shevchenko module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 0400);
149083ff59a0SMike Frysinger MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
1491801c135cSArtem B. Bityutskiy "Multiple \"mtd\" parameters may be specified.\n"
1492edac493dSRichard Genoud "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1493edac493dSRichard Genoud "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1494edac493dSRichard Genoud "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1495edac493dSRichard Genoud __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
149683ff59a0SMike Frysinger "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
14977af73882SZhaoLong Wang "Optional \"enable_fm\" parameter determines whether to enable fastmap during attach. If the value is non-zero, fastmap is enabled. Default value is 0.\n"
1498edac493dSRichard Genoud "\n"
1499edac493dSRichard Genoud "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1500edac493dSRichard Genoud "Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"
1501edac493dSRichard Genoud "Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and reserve 25*nand_size_in_blocks/1024 erase blocks for bad block handling.\n"
150283ff59a0SMike Frysinger "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
15037af73882SZhaoLong Wang "example 5: mtd=1,0,0,5 mtd=2,0,0,6,1 - attach MTD device /dev/mtd1 to UBI 5 and disable fastmap; attach MTD device /dev/mtd2 to UBI 6 and enable fastmap.(only works when fastmap is enabled and fm_autoconvert=Y).\n"
1504edac493dSRichard Genoud "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
150577e6c2f0SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
150677e6c2f0SRichard Weinberger module_param(fm_autoconvert, bool, 0644);
150777e6c2f0SRichard Weinberger MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
1508479c2c0cSRichard Weinberger module_param(fm_debug, bool, 0);
1509479c2c0cSRichard Weinberger MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
151077e6c2f0SRichard Weinberger #endif
1511801c135cSArtem B. Bityutskiy MODULE_VERSION(__stringify(UBI_VERSION));
1512801c135cSArtem B. Bityutskiy MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1513801c135cSArtem B. Bityutskiy MODULE_AUTHOR("Artem Bityutskiy");
1514801c135cSArtem B. Bityutskiy MODULE_LICENSE("GPL");
1515