build.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2007
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Artem Bityutskiy (Битюцкий Артём),
  20. * Frank Haverkamp
  21. */
  22. /*
  23. * This file includes UBI initialization and building of UBI devices.
  24. *
  25. * When UBI is initialized, it attaches all the MTD devices specified as the
  26. * module load parameters or the kernel boot parameters. If MTD devices were
  27. * specified, UBI does not attach any MTD device, but it is possible to do
  28. * later using the "UBI control device".
  29. */
  30. #include <linux/err.h>
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/stringify.h>
  34. #include <linux/namei.h>
  35. #include <linux/stat.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/log2.h>
  39. #include <linux/kthread.h>
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/major.h>
  43. #include <linux/mm.h>
  44. #include "ubi.h"
  45. #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
  46. #if defined(CONFIG_MTK_MLC_NAND_SUPPORT)
  47. #define COMBO_NAND_BLOCK_SIZE (ubi->mtd->erasesize)
  48. #define COMBO_NAND_PAGE_SIZE (ubi->mtd->writesize)
  49. #else
  50. #define COMBO_NAND_BLOCK_SIZE (256*1024)
  51. #define COMBO_NAND_PAGE_SIZE (4*1024)
  52. #endif
  53. #endif
  54. /* Maximum length of the 'mtd=' parameter */
  55. #define MTD_PARAM_LEN_MAX 64
  56. /* Maximum number of comma-separated items in the 'mtd=' parameter */
  57. #define MTD_PARAM_MAX_COUNT 4
  58. /* Maximum value for the number of bad PEBs per 1024 PEBs */
  59. #define MAX_MTD_UBI_BEB_LIMIT 768
  60. #ifdef CONFIG_MTD_UBI_MODULE
  61. #define ubi_is_module() 1
  62. #else
  63. #define ubi_is_module() 0
  64. #endif
  65. #ifdef CONFIG_UBI_SHARE_BUFFER
  66. void *ubi_peb_buf = NULL;
  67. DEFINE_MUTEX(ubi_buf_mutex);
  68. #endif
  69. /**
  70. * struct mtd_dev_param - MTD device parameter description data structure.
  71. * @name: MTD character device node path, MTD device name, or MTD device number
  72. * string
  73. * @vid_hdr_offs: VID header offset
  74. * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
  75. */
  76. struct mtd_dev_param {
  77. char name[MTD_PARAM_LEN_MAX];
  78. int ubi_num;
  79. int vid_hdr_offs;
  80. int max_beb_per1024;
  81. };
  82. /* Numbers of elements set in the @mtd_dev_param array */
  83. static int mtd_devs __initdata;
  84. /* MTD devices specification parameters */
  85. static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES] __initdata;
  86. #ifdef CONFIG_MTD_UBI_FASTMAP
  87. /* UBI module parameter to enable fastmap automatically on non-fastmap images */
  88. #ifdef CONFIG_MTK_NAND_UBIFS_FASTMAP_SUPPORT
  89. static bool fm_autoconvert = 1;
  90. #else
  91. static bool fm_autoconvert;
  92. #endif
  93. #endif
  94. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  95. struct class *ubi_class;
  96. /* Slab cache for wear-leveling entries */
  97. struct kmem_cache *ubi_wl_entry_slab;
  98. /* UBI control character device */
  99. static struct miscdevice ubi_ctrl_cdev = {
  100. .minor = MISC_DYNAMIC_MINOR,
  101. .name = "ubi_ctrl",
  102. .fops = &ubi_ctrl_cdev_operations,
  103. };
  104. /* All UBI devices in system */
  105. static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  106. /* Serializes UBI devices creations and removals */
  107. DEFINE_MUTEX(ubi_devices_mutex);
  108. /* Protects @ubi_devices and @ubi->ref_count */
  109. static DEFINE_SPINLOCK(ubi_devices_lock);
  110. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  111. static ssize_t ubi_version_show(struct class *class,
  112. struct class_attribute *attr, char *buf)
  113. {
  114. return sprintf(buf, "%d\n", UBI_VERSION);
  115. }
  116. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  117. static struct class_attribute ubi_version =
  118. __ATTR(version, S_IRUGO, ubi_version_show, NULL);
  119. static ssize_t dev_attribute_show(struct device *dev,
  120. struct device_attribute *attr, char *buf);
  121. /*MTK*/
  122. static ssize_t dev_attribute_store(struct device *dev, struct device_attribute *attr,
  123. const char *buf, size_t count);
  124. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  125. static struct device_attribute dev_eraseblock_size =
  126. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  127. static struct device_attribute dev_avail_eraseblocks =
  128. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  129. static struct device_attribute dev_total_eraseblocks =
  130. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  131. static struct device_attribute dev_volumes_count =
  132. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  133. static struct device_attribute dev_max_ec =
  134. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  135. /*MTK start*/
  136. static struct device_attribute dev_lbb =
  137. __ATTR(lbb, S_IRUGO, dev_attribute_show, NULL);
  138. static struct device_attribute dev_move_retry =
  139. __ATTR(move_retry, S_IRUGO, dev_attribute_show, NULL);
  140. static struct device_attribute dev_ec_count =
  141. __ATTR(ec_count, S_IRUGO, dev_attribute_show, NULL);
  142. static struct device_attribute dev_mean_ec =
  143. __ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
  144. static struct device_attribute dev_ec_sum =
  145. __ATTR(ec_sum, S_IRUGO, dev_attribute_show, NULL);
  146. static struct device_attribute dev_min_ec =
  147. __ATTR(min_ec, S_IRUGO, dev_attribute_show, NULL);
  148. static struct device_attribute dev_wl_count =
  149. __ATTR(wl_count, S_IRUGO, dev_attribute_show, NULL);
  150. static struct device_attribute dev_wl_size =
  151. __ATTR(wl_size, S_IRUGO, dev_attribute_show, NULL);
  152. static struct device_attribute dev_scrub_count =
  153. __ATTR(scrub_count, S_IRUGO, dev_attribute_show, NULL);
  154. static struct device_attribute dev_scrub_size =
  155. __ATTR(scrub_size, S_IRUGO, dev_attribute_show, NULL);
  156. static struct device_attribute dev_wl_th =
  157. __ATTR(wl_th, 00755, dev_attribute_show, dev_attribute_store);
  158. static struct device_attribute dev_torture =
  159. __ATTR(torture, 00755, dev_attribute_show, NULL);
  160. /*MTK end*/
  161. static struct device_attribute dev_reserved_for_bad =
  162. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  163. static struct device_attribute dev_bad_peb_count =
  164. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  165. static struct device_attribute dev_max_vol_count =
  166. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  167. static struct device_attribute dev_min_io_size =
  168. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  169. static struct device_attribute dev_bgt_enabled =
  170. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  171. static struct device_attribute dev_mtd_num =
  172. __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
  173. /**
  174. * ubi_volume_notify - send a volume change notification.
  175. * @ubi: UBI device description object
  176. * @vol: volume description object of the changed volume
  177. * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
  178. *
  179. * This is a helper function which notifies all subscribers about a volume
  180. * change event (creation, removal, re-sizing, re-naming, updating). Returns
  181. * zero in case of success and a negative error code in case of failure.
  182. */
  183. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
  184. {
  185. struct ubi_notification nt;
  186. ubi_do_get_device_info(ubi, &nt.di);
  187. ubi_do_get_volume_info(ubi, vol, &nt.vi);
  188. #ifdef CONFIG_MTD_UBI_FASTMAP
  189. switch (ntype) {
  190. case UBI_VOLUME_ADDED:
  191. case UBI_VOLUME_REMOVED:
  192. case UBI_VOLUME_RESIZED:
  193. case UBI_VOLUME_RENAMED:
  194. if (ubi_update_fastmap(ubi)) {
  195. ubi_err("Unable to update fastmap!");
  196. ubi_ro_mode(ubi);
  197. }
  198. }
  199. #endif
  200. return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
  201. }
  202. /**
  203. * ubi_notify_all - send a notification to all volumes.
  204. * @ubi: UBI device description object
  205. * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
  206. * @nb: the notifier to call
  207. *
  208. * This function walks all volumes of UBI device @ubi and sends the @ntype
  209. * notification for each volume. If @nb is %NULL, then all registered notifiers
  210. * are called, otherwise only the @nb notifier is called. Returns the number of
  211. * sent notifications.
  212. */
  213. int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
  214. {
  215. struct ubi_notification nt;
  216. int i, count = 0;
  217. ubi_do_get_device_info(ubi, &nt.di);
  218. mutex_lock(&ubi->device_mutex);
  219. for (i = 0; i < ubi->vtbl_slots; i++) {
  220. /*
  221. * Since the @ubi->device is locked, and we are not going to
  222. * change @ubi->volumes, we do not have to lock
  223. * @ubi->volumes_lock.
  224. */
  225. if (!ubi->volumes[i])
  226. continue;
  227. ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
  228. if (nb)
  229. nb->notifier_call(nb, ntype, &nt);
  230. else
  231. blocking_notifier_call_chain(&ubi_notifiers, ntype,
  232. &nt);
  233. count += 1;
  234. }
  235. mutex_unlock(&ubi->device_mutex);
  236. return count;
  237. }
  238. /**
  239. * ubi_enumerate_volumes - send "add" notification for all existing volumes.
  240. * @nb: the notifier to call
  241. *
  242. * This function walks all UBI devices and volumes and sends the
  243. * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
  244. * registered notifiers are called, otherwise only the @nb notifier is called.
  245. * Returns the number of sent notifications.
  246. */
  247. int ubi_enumerate_volumes(struct notifier_block *nb)
  248. {
  249. int i, count = 0;
  250. /*
  251. * Since the @ubi_devices_mutex is locked, and we are not going to
  252. * change @ubi_devices, we do not have to lock @ubi_devices_lock.
  253. */
  254. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  255. struct ubi_device *ubi = ubi_devices[i];
  256. if (!ubi)
  257. continue;
  258. count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
  259. }
  260. return count;
  261. }
  262. /**
  263. * ubi_get_device - get UBI device.
  264. * @ubi_num: UBI device number
  265. *
  266. * This function returns UBI device description object for UBI device number
  267. * @ubi_num, or %NULL if the device does not exist. This function increases the
  268. * device reference count to prevent removal of the device. In other words, the
  269. * device cannot be removed if its reference count is not zero.
  270. */
  271. struct ubi_device *ubi_get_device(int ubi_num)
  272. {
  273. struct ubi_device *ubi;
  274. spin_lock(&ubi_devices_lock);
  275. ubi = ubi_devices[ubi_num];
  276. if (ubi) {
  277. ubi_assert(ubi->ref_count >= 0);
  278. ubi->ref_count += 1;
  279. get_device(&ubi->dev);
  280. }
  281. spin_unlock(&ubi_devices_lock);
  282. return ubi;
  283. }
  284. /**
  285. * ubi_put_device - drop an UBI device reference.
  286. * @ubi: UBI device description object
  287. */
  288. void ubi_put_device(struct ubi_device *ubi)
  289. {
  290. spin_lock(&ubi_devices_lock);
  291. ubi->ref_count -= 1;
  292. put_device(&ubi->dev);
  293. spin_unlock(&ubi_devices_lock);
  294. }
  295. /**
  296. * ubi_get_by_major - get UBI device by character device major number.
  297. * @major: major number
  298. *
  299. * This function is similar to 'ubi_get_device()', but it searches the device
  300. * by its major number.
  301. */
  302. struct ubi_device *ubi_get_by_major(int major)
  303. {
  304. int i;
  305. struct ubi_device *ubi;
  306. spin_lock(&ubi_devices_lock);
  307. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  308. ubi = ubi_devices[i];
  309. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  310. ubi_assert(ubi->ref_count >= 0);
  311. ubi->ref_count += 1;
  312. get_device(&ubi->dev);
  313. spin_unlock(&ubi_devices_lock);
  314. return ubi;
  315. }
  316. }
  317. spin_unlock(&ubi_devices_lock);
  318. return NULL;
  319. }
  320. /**
  321. * ubi_major2num - get UBI device number by character device major number.
  322. * @major: major number
  323. *
  324. * This function searches UBI device number object by its major number. If UBI
  325. * device was not found, this function returns -ENODEV, otherwise the UBI device
  326. * number is returned.
  327. */
  328. int ubi_major2num(int major)
  329. {
  330. int i, ubi_num = -ENODEV;
  331. spin_lock(&ubi_devices_lock);
  332. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  333. struct ubi_device *ubi = ubi_devices[i];
  334. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  335. ubi_num = ubi->ubi_num;
  336. break;
  337. }
  338. }
  339. spin_unlock(&ubi_devices_lock);
  340. return ubi_num;
  341. }
  342. /* MTK: "Store" method for files in '/<sysfs>/class/ubi/ubiX/' */
  343. static ssize_t dev_attribute_store(struct device *dev, struct device_attribute *attr,
  344. const char *buf, size_t count)
  345. {
  346. struct ubi_device *ubi;
  347. int th = 0;
  348. ubi = container_of(dev, struct ubi_device, dev);
  349. ubi = ubi_get_device(ubi->ubi_num);
  350. if (!ubi)
  351. return -ENODEV;
  352. if (attr == &dev_wl_th) {
  353. int ret = kstrtoint(buf, 0, &th);
  354. if (ret == 0) {
  355. ubi_msg("set th=%d\n", th);
  356. ubi->wl_th = th;
  357. }
  358. }
  359. return count;
  360. }
  361. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  362. static ssize_t dev_attribute_show(struct device *dev,
  363. struct device_attribute *attr, char *buf)
  364. {
  365. ssize_t ret;
  366. struct ubi_device *ubi;
  367. /*
  368. * The below code looks weird, but it actually makes sense. We get the
  369. * UBI device reference from the contained 'struct ubi_device'. But it
  370. * is unclear if the device was removed or not yet. Indeed, if the
  371. * device was removed before we increased its reference count,
  372. * 'ubi_get_device()' will return -ENODEV and we fail.
  373. *
  374. * Remember, 'struct ubi_device' is freed in the release function, so
  375. * we still can use 'ubi->ubi_num'.
  376. */
  377. ubi = container_of(dev, struct ubi_device, dev);
  378. ubi = ubi_get_device(ubi->ubi_num);
  379. if (!ubi)
  380. return -ENODEV;
  381. if (attr == &dev_eraseblock_size)
  382. ret = sprintf(buf, "%d\n", ubi->leb_size);
  383. else if (attr == &dev_avail_eraseblocks)
  384. ret = sprintf(buf, "%d\n", ubi->avail_pebs);
  385. else if (attr == &dev_total_eraseblocks)
  386. ret = sprintf(buf, "%d\n", ubi->good_peb_count);
  387. else if (attr == &dev_volumes_count)
  388. ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
  389. else if (attr == &dev_max_ec)
  390. ret = sprintf(buf, "%d\n", ubi->max_ec);
  391. /*MTK start*/
  392. else if (attr == &dev_torture)
  393. ret = sprintf(buf, "torture: %d\n", ubi->torture);
  394. else if (attr == &dev_wl_th)
  395. ret = sprintf(buf, "wl_th: %d\n", ubi->wl_th);
  396. else if (attr == &dev_wl_count)
  397. ret = sprintf(buf, "wl_count: %d\n", ubi->wl_count);
  398. else if (attr == &dev_wl_size)
  399. ret = sprintf(buf, "wl_size: %lld\n", ubi->wl_size);
  400. else if (attr == &dev_scrub_count)
  401. ret = sprintf(buf, "scrub_count: %d\n", ubi->scrub_count);
  402. else if (attr == &dev_scrub_size)
  403. ret = sprintf(buf, "scrub_size: %lld\n", ubi->scrub_size);
  404. else if (attr == &dev_move_retry)
  405. ret = sprintf(buf, "move_retry: %d\n", atomic_read(&ubi->move_retry));
  406. else if (attr == &dev_lbb)
  407. ret = sprintf(buf, "lbb: %d\n", atomic_read(&ubi->lbb));
  408. else if (attr == &dev_ec_count)
  409. ret = sprintf(buf, "ec_count: %d\n", atomic_read(&ubi->ec_count));
  410. else if (attr == &dev_mean_ec)
  411. ret = sprintf(buf, "mean_ec: %d\n", ubi->mean_ec);
  412. else if (attr == &dev_ec_sum)
  413. ret = sprintf(buf, "%lld\n", ubi->ec_sum);
  414. else if (attr == &dev_min_ec) {
  415. struct ubi_wl_entry *e = NULL, *efree = NULL, *eused = NULL;
  416. spin_lock(&ubi->wl_lock);
  417. efree = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
  418. eused = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  419. if (efree && eused) {
  420. if (efree->ec < eused->ec)
  421. e = efree;
  422. else
  423. e = eused;
  424. } else if (efree) {
  425. e = efree;
  426. } else {
  427. e = eused;
  428. }
  429. ret = sprintf(buf, "%d\n", e->ec);
  430. spin_unlock(&ubi->wl_lock);
  431. }
  432. /*MTK end*/
  433. else if (attr == &dev_reserved_for_bad)
  434. ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  435. else if (attr == &dev_bad_peb_count)
  436. ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
  437. else if (attr == &dev_max_vol_count)
  438. ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
  439. else if (attr == &dev_min_io_size)
  440. ret = sprintf(buf, "%d\n", ubi->min_io_size);
  441. else if (attr == &dev_bgt_enabled)
  442. ret = sprintf(buf, "%d\n", ubi->thread_enabled);
  443. else if (attr == &dev_mtd_num)
  444. ret = sprintf(buf, "%d\n", ubi->mtd->index);
  445. else
  446. ret = -EINVAL;
  447. ubi_put_device(ubi);
  448. return ret;
  449. }
  450. static void dev_release(struct device *dev)
  451. {
  452. struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
  453. kfree(ubi);
  454. }
  455. /**
  456. * ubi_sysfs_init - initialize sysfs for an UBI device.
  457. * @ubi: UBI device description object
  458. * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
  459. * taken
  460. *
  461. * This function returns zero in case of success and a negative error code in
  462. * case of failure.
  463. */
  464. static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
  465. {
  466. int err;
  467. ubi->dev.release = dev_release;
  468. ubi->dev.devt = ubi->cdev.dev;
  469. ubi->dev.class = ubi_class;
  470. dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
  471. err = device_register(&ubi->dev);
  472. if (err)
  473. return err;
  474. *ref = 1;
  475. err = device_create_file(&ubi->dev, &dev_eraseblock_size);
  476. if (err)
  477. return err;
  478. err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
  479. if (err)
  480. return err;
  481. err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
  482. if (err)
  483. return err;
  484. err = device_create_file(&ubi->dev, &dev_volumes_count);
  485. if (err)
  486. return err;
  487. err = device_create_file(&ubi->dev, &dev_max_ec);
  488. if (err)
  489. return err;
  490. /*MTK start*/
  491. err = device_create_file(&ubi->dev, &dev_lbb);
  492. if (err)
  493. return err;
  494. err = device_create_file(&ubi->dev, &dev_move_retry);
  495. if (err)
  496. return err;
  497. err = device_create_file(&ubi->dev, &dev_ec_count);
  498. if (err)
  499. return err;
  500. err = device_create_file(&ubi->dev, &dev_mean_ec);
  501. if (err)
  502. return err;
  503. err = device_create_file(&ubi->dev, &dev_ec_sum);
  504. if (err)
  505. return err;
  506. err = device_create_file(&ubi->dev, &dev_min_ec);
  507. if (err)
  508. return err;
  509. err = device_create_file(&ubi->dev, &dev_wl_count);
  510. if (err)
  511. return err;
  512. err = device_create_file(&ubi->dev, &dev_wl_size);
  513. if (err)
  514. return err;
  515. err = device_create_file(&ubi->dev, &dev_scrub_count);
  516. if (err)
  517. return err;
  518. err = device_create_file(&ubi->dev, &dev_scrub_size);
  519. if (err)
  520. return err;
  521. err = device_create_file(&ubi->dev, &dev_wl_th);
  522. if (err)
  523. return err;
  524. err = device_create_file(&ubi->dev, &dev_torture);
  525. if (err)
  526. return err;
  527. /*MTK end*/
  528. err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
  529. if (err)
  530. return err;
  531. err = device_create_file(&ubi->dev, &dev_bad_peb_count);
  532. if (err)
  533. return err;
  534. err = device_create_file(&ubi->dev, &dev_max_vol_count);
  535. if (err)
  536. return err;
  537. err = device_create_file(&ubi->dev, &dev_min_io_size);
  538. if (err)
  539. return err;
  540. err = device_create_file(&ubi->dev, &dev_bgt_enabled);
  541. if (err)
  542. return err;
  543. err = device_create_file(&ubi->dev, &dev_mtd_num);
  544. return err;
  545. }
  546. /**
  547. * ubi_sysfs_close - close sysfs for an UBI device.
  548. * @ubi: UBI device description object
  549. */
  550. static void ubi_sysfs_close(struct ubi_device *ubi)
  551. {
  552. device_remove_file(&ubi->dev, &dev_mtd_num);
  553. device_remove_file(&ubi->dev, &dev_bgt_enabled);
  554. device_remove_file(&ubi->dev, &dev_min_io_size);
  555. device_remove_file(&ubi->dev, &dev_max_vol_count);
  556. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  557. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  558. device_remove_file(&ubi->dev, &dev_max_ec);
  559. device_remove_file(&ubi->dev, &dev_volumes_count);
  560. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  561. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  562. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  563. device_unregister(&ubi->dev);
  564. }
  565. /**
  566. * kill_volumes - destroy all user volumes.
  567. * @ubi: UBI device description object
  568. */
  569. static void kill_volumes(struct ubi_device *ubi)
  570. {
  571. int i;
  572. for (i = 0; i < ubi->vtbl_slots; i++)
  573. if (ubi->volumes[i])
  574. ubi_free_volume(ubi, ubi->volumes[i]);
  575. }
  576. /**
  577. * uif_init - initialize user interfaces for an UBI device.
  578. * @ubi: UBI device description object
  579. * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
  580. * taken, otherwise set to %0
  581. *
  582. * This function initializes various user interfaces for an UBI device. If the
  583. * initialization fails at an early stage, this function frees all the
  584. * resources it allocated, returns an error, and @ref is set to %0. However,
  585. * if the initialization fails after the UBI device was registered in the
  586. * driver core subsystem, this function takes a reference to @ubi->dev, because
  587. * otherwise the release function ('dev_release()') would free whole @ubi
  588. * object. The @ref argument is set to %1 in this case. The caller has to put
  589. * this reference.
  590. *
  591. * This function returns zero in case of success and a negative error code in
  592. * case of failure.
  593. */
  594. static int uif_init(struct ubi_device *ubi, int *ref)
  595. {
  596. int i, err;
  597. dev_t dev;
  598. *ref = 0;
  599. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  600. /*
  601. * Major numbers for the UBI character devices are allocated
  602. * dynamically. Major numbers of volume character devices are
  603. * equivalent to ones of the corresponding UBI character device. Minor
  604. * numbers of UBI character devices are 0, while minor numbers of
  605. * volume character devices start from 1. Thus, we allocate one major
  606. * number and ubi->vtbl_slots + 1 minor numbers.
  607. */
  608. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  609. if (err) {
  610. ubi_err("cannot register UBI character devices");
  611. return err;
  612. }
  613. ubi_assert(MINOR(dev) == 0);
  614. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  615. dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
  616. ubi->cdev.owner = THIS_MODULE;
  617. err = cdev_add(&ubi->cdev, dev, 1);
  618. if (err) {
  619. ubi_err("cannot add character device");
  620. goto out_unreg;
  621. }
  622. err = ubi_sysfs_init(ubi, ref);
  623. if (err)
  624. goto out_sysfs;
  625. for (i = 0; i < ubi->vtbl_slots; i++)
  626. if (ubi->volumes[i]) {
  627. err = ubi_add_volume(ubi, ubi->volumes[i]);
  628. if (err) {
  629. ubi_err("cannot add volume %d", i);
  630. goto out_volumes;
  631. }
  632. }
  633. return 0;
  634. out_volumes:
  635. kill_volumes(ubi);
  636. out_sysfs:
  637. if (*ref)
  638. get_device(&ubi->dev);
  639. ubi_sysfs_close(ubi);
  640. cdev_del(&ubi->cdev);
  641. out_unreg:
  642. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  643. ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
  644. return err;
  645. }
  646. /**
  647. * uif_close - close user interfaces for an UBI device.
  648. * @ubi: UBI device description object
  649. *
  650. * Note, since this function un-registers UBI volume device objects (@vol->dev),
  651. * the memory allocated voe the volumes is freed as well (in the release
  652. * function).
  653. */
  654. static void uif_close(struct ubi_device *ubi)
  655. {
  656. kill_volumes(ubi);
  657. ubi_sysfs_close(ubi);
  658. cdev_del(&ubi->cdev);
  659. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  660. }
  661. /**
  662. * ubi_free_internal_volumes - free internal volumes.
  663. * @ubi: UBI device description object
  664. */
  665. void ubi_free_internal_volumes(struct ubi_device *ubi)
  666. {
  667. int i;
  668. for (i = ubi->vtbl_slots;
  669. i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  670. kfree(ubi->volumes[i]->eba_tbl);
  671. kfree(ubi->volumes[i]);
  672. }
  673. }
  674. static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
  675. {
  676. int limit, device_pebs;
  677. uint64_t device_size;
  678. if (!max_beb_per1024)
  679. return 0;
  680. /*
  681. * Here we are using size of the entire flash chip and
  682. * not just the MTD partition size because the maximum
  683. * number of bad eraseblocks is a percentage of the
  684. * whole device and bad eraseblocks are not fairly
  685. * distributed over the flash chip. So the worst case
  686. * is that all the bad eraseblocks of the chip are in
  687. * the MTD partition we are attaching (ubi->mtd).
  688. */
  689. device_size = mtd_get_device_size(ubi->mtd);
  690. device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
  691. limit = mult_frac(device_pebs, max_beb_per1024, 1024);
  692. /* Round it up */
  693. if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
  694. limit += 1;
  695. return limit;
  696. }
  697. /**
  698. * io_init - initialize I/O sub-system for a given UBI device.
  699. * @ubi: UBI device description object
  700. * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
  701. *
  702. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  703. * assumed:
  704. * o EC header is always at offset zero - this cannot be changed;
  705. * o VID header starts just after the EC header at the closest address
  706. * aligned to @io->hdrs_min_io_size;
  707. * o data starts just after the VID header at the closest address aligned to
  708. * @io->min_io_size
  709. *
  710. * This function returns zero in case of success and a negative error code in
  711. * case of failure.
  712. */
  713. static int io_init(struct ubi_device *ubi, int max_beb_per1024)
  714. {
  715. struct sysinfo i;
  716. dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
  717. dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
  718. if (ubi->mtd->numeraseregions != 0) {
  719. /*
  720. * Some flashes have several erase regions. Different regions
  721. * may have different eraseblock size and other
  722. * characteristics. It looks like mostly multi-region flashes
  723. * have one "main" region and one or more small regions to
  724. * store boot loader code or boot parameters or whatever. I
  725. * guess we should just pick the largest region. But this is
  726. * not implemented.
  727. */
  728. ubi_err("multiple regions, not implemented");
  729. return -EINVAL;
  730. }
  731. if (ubi->vid_hdr_offset < 0)
  732. return -EINVAL;
  733. /*
  734. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  735. * physical eraseblocks maximum.
  736. */
  737. ubi->peb_size = ubi->mtd->erasesize;
  738. ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
  739. #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
  740. ubi->peb_size = COMBO_NAND_BLOCK_SIZE;
  741. ubi->peb_count = (int)div_u64(ubi->mtd->size, ubi->peb_size);
  742. #endif
  743. ubi->flash_size = ubi->mtd->size;
  744. if (mtd_can_have_bb(ubi->mtd)) {
  745. ubi->bad_allowed = 1;
  746. ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
  747. }
  748. if (ubi->mtd->type == MTD_NORFLASH) {
  749. ubi_assert(ubi->mtd->writesize == 1);
  750. ubi->nor_flash = 1;
  751. }
  752. ubi->min_io_size = ubi->mtd->writesize;
  753. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  754. #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
  755. ubi->min_io_size = COMBO_NAND_PAGE_SIZE;
  756. ubi->hdrs_min_io_size = ubi->min_io_size >> ubi->mtd->subpage_sft;
  757. #endif
  758. /*
  759. * Make sure minimal I/O unit is power of 2. Note, there is no
  760. * fundamental reason for this assumption. It is just an optimization
  761. * which allows us to avoid costly division operations.
  762. */
  763. if (!is_power_of_2(ubi->min_io_size)) {
  764. ubi_err("min. I/O unit (%d) is not power of 2",
  765. ubi->min_io_size);
  766. return -EINVAL;
  767. }
  768. ubi_assert(ubi->hdrs_min_io_size > 0);
  769. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  770. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  771. ubi->max_write_size = ubi->mtd->writebufsize;
  772. #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
  773. ubi->max_write_size = COMBO_NAND_PAGE_SIZE;
  774. #endif
  775. si_meminfo(&i);
  776. #define K(x) ((x) << (PAGE_SHIFT - 10))
  777. #if defined(CONFIG_MTK_MLC_NAND_SUPPORT)
  778. ubi_msg("Total ram : %luKB\n", K(i.totalram));
  779. if (K(i.totalram) > 512 * 1024)
  780. ubi->max_write_size = ubi->mtd->erasesize;
  781. else
  782. ubi->max_write_size = ubi->mtd->erasesize/4;
  783. ubi->max_write_size = roundup_pow_of_two(ubi->max_write_size);
  784. #endif
  785. /*
  786. * Maximum write size has to be greater or equivalent to min. I/O
  787. * size, and be multiple of min. I/O size.
  788. */
  789. if (ubi->max_write_size < ubi->min_io_size ||
  790. ubi->max_write_size % ubi->min_io_size ||
  791. !is_power_of_2(ubi->max_write_size)) {
  792. ubi_err("bad write buffer size %d for %d min. I/O unit",
  793. ubi->max_write_size, ubi->min_io_size);
  794. return -EINVAL;
  795. }
  796. /* Calculate default aligned sizes of EC and VID headers */
  797. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  798. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  799. dbg_gen("min_io_size %d", ubi->min_io_size);
  800. dbg_gen("max_write_size %d", ubi->max_write_size);
  801. dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  802. dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  803. dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  804. if (ubi->vid_hdr_offset == 0)
  805. /* Default offset */
  806. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  807. ubi->ec_hdr_alsize;
  808. else {
  809. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  810. ~(ubi->hdrs_min_io_size - 1);
  811. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  812. ubi->vid_hdr_aloffset;
  813. }
  814. /* Similar for the data offset */
  815. ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
  816. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  817. dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
  818. dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  819. dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
  820. dbg_gen("leb_start %d", ubi->leb_start);
  821. /* The shift must be aligned to 32-bit boundary */
  822. if (ubi->vid_hdr_shift % 4) {
  823. ubi_err("unaligned VID header shift %d",
  824. ubi->vid_hdr_shift);
  825. return -EINVAL;
  826. }
  827. /* Check sanity */
  828. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  829. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  830. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  831. ubi->leb_start & (ubi->min_io_size - 1)) {
  832. ubi_err("bad VID header (%d) or data offsets (%d)",
  833. ubi->vid_hdr_offset, ubi->leb_start);
  834. return -EINVAL;
  835. }
  836. /*
  837. * Set maximum amount of physical erroneous eraseblocks to be 10%.
  838. * Erroneous PEB are those which have read errors.
  839. */
  840. ubi->max_erroneous = ubi->peb_count / 10;
  841. if (ubi->max_erroneous < 16)
  842. ubi->max_erroneous = 16;
  843. dbg_gen("max_erroneous %d", ubi->max_erroneous);
  844. /*
  845. * It may happen that EC and VID headers are situated in one minimal
  846. * I/O unit. In this case we can only accept this UBI image in
  847. * read-only mode.
  848. */
  849. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  850. ubi_warn("EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
  851. ubi->ro_mode = 1;
  852. }
  853. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  854. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  855. ubi_msg("MTD device %d is write-protected, attach in read-only mode",
  856. ubi->mtd->index);
  857. ubi->ro_mode = 1;
  858. }
  859. /*
  860. * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
  861. * unfortunately, MTD does not provide this information. We should loop
  862. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  863. * each physical eraseblock. So, we leave @ubi->bad_peb_count
  864. * uninitialized so far.
  865. */
  866. return 0;
  867. }
  868. /**
  869. * autoresize - re-size the volume which has the "auto-resize" flag set.
  870. * @ubi: UBI device description object
  871. * @vol_id: ID of the volume to re-size
  872. *
  873. * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
  874. * the volume table to the largest possible size. See comments in ubi-header.h
  875. * for more description of the flag. Returns zero in case of success and a
  876. * negative error code in case of failure.
  877. */
  878. static int autoresize(struct ubi_device *ubi, int vol_id)
  879. {
  880. struct ubi_volume_desc desc;
  881. struct ubi_volume *vol = ubi->volumes[vol_id];
  882. int err, old_reserved_pebs = vol->reserved_pebs;
  883. if (ubi->ro_mode) {
  884. ubi_warn("skip auto-resize because of R/O mode");
  885. return 0;
  886. }
  887. /*
  888. * Clear the auto-resize flag in the volume in-memory copy of the
  889. * volume table, and 'ubi_resize_volume()' will propagate this change
  890. * to the flash.
  891. */
  892. ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
  893. if (ubi->avail_pebs == 0) {
  894. struct ubi_vtbl_record vtbl_rec;
  895. /*
  896. * No available PEBs to re-size the volume, clear the flag on
  897. * flash and exit.
  898. */
  899. vtbl_rec = ubi->vtbl[vol_id];
  900. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  901. if (err)
  902. ubi_err("cannot clean auto-resize flag for volume %d",
  903. vol_id);
  904. } else {
  905. desc.vol = vol;
  906. err = ubi_resize_volume(&desc,
  907. old_reserved_pebs + ubi->avail_pebs);
  908. if (err)
  909. ubi_err("cannot auto-resize volume %d", vol_id);
  910. }
  911. if (err)
  912. return err;
  913. ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,
  914. vol->name, old_reserved_pebs, vol->reserved_pebs);
  915. return 0;
  916. }
  917. /**
  918. * ubi_attach_mtd_dev - attach an MTD device.
  919. * @mtd: MTD device description object
  920. * @ubi_num: number to assign to the new UBI device
  921. * @vid_hdr_offset: VID header offset
  922. * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
  923. *
  924. * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
  925. * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
  926. * which case this function finds a vacant device number and assigns it
  927. * automatically. Returns the new UBI device number in case of success and a
  928. * negative error code in case of failure.
  929. *
  930. * Note, the invocations of this function has to be serialized by the
  931. * @ubi_devices_mutex.
  932. */
  933. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
  934. int vid_hdr_offset, int max_beb_per1024)
  935. {
  936. struct ubi_device *ubi;
  937. int i, err, ref = 0;
  938. unsigned long long attach_time = 0;
  939. if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
  940. return -EINVAL;
  941. if (!max_beb_per1024)
  942. max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
  943. /*
  944. * Check if we already have the same MTD device attached.
  945. *
  946. * Note, this function assumes that UBI devices creations and deletions
  947. * are serialized, so it does not take the &ubi_devices_lock.
  948. */
  949. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  950. ubi = ubi_devices[i];
  951. if (ubi && mtd->index == ubi->mtd->index) {
  952. ubi_err("mtd%d is already attached to ubi%d",
  953. mtd->index, i);
  954. return -EEXIST;
  955. }
  956. }
  957. /*
  958. * Make sure this MTD device is not emulated on top of an UBI volume
  959. * already. Well, generally this recursion works fine, but there are
  960. * different problems like the UBI module takes a reference to itself
  961. * by attaching (and thus, opening) the emulated MTD device. This
  962. * results in inability to unload the module. And in general it makes
  963. * no sense to attach emulated MTD devices, so we prohibit this.
  964. */
  965. if (mtd->type == MTD_UBIVOLUME) {
  966. ubi_err("refuse attaching mtd%d - it is already emulated on top of UBI",
  967. mtd->index);
  968. return -EINVAL;
  969. }
  970. if (ubi_num == UBI_DEV_NUM_AUTO) {
  971. /* Search for an empty slot in the @ubi_devices array */
  972. for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
  973. if (!ubi_devices[ubi_num])
  974. break;
  975. if (ubi_num == UBI_MAX_DEVICES) {
  976. ubi_err("only %d UBI devices may be created",
  977. UBI_MAX_DEVICES);
  978. return -ENFILE;
  979. }
  980. } else {
  981. if (ubi_num >= UBI_MAX_DEVICES)
  982. return -EINVAL;
  983. /* Make sure ubi_num is not busy */
  984. if (ubi_devices[ubi_num]) {
  985. ubi_err("ubi%d already exists", ubi_num);
  986. return -EEXIST;
  987. }
  988. }
  989. ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
  990. if (!ubi)
  991. return -ENOMEM;
  992. ubi->mtd = mtd;
  993. ubi->ubi_num = ubi_num;
  994. ubi->vid_hdr_offset = vid_hdr_offset;
  995. ubi->autoresize_vol_id = -1;
  996. /*MTK start*/
  997. ubi->wl_th = CONFIG_MTD_UBI_WL_THRESHOLD;
  998. atomic_set(&ubi->ec_count, 0);
  999. atomic_set(&ubi->move_retry, 0);
  1000. /*MTK end*/
  1001. #ifdef CONFIG_MTD_UBI_FASTMAP
  1002. ubi->fm_pool.used = ubi->fm_pool.size = 0;
  1003. ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
  1004. /*
  1005. * fm_pool.max_size is 5% of the total number of PEBs but it's also
  1006. * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
  1007. */
  1008. ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
  1009. ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
  1010. if (ubi->fm_pool.max_size < UBI_FM_MIN_POOL_SIZE)
  1011. ubi->fm_pool.max_size = UBI_FM_MIN_POOL_SIZE;
  1012. ubi->fm_wl_pool.max_size = UBI_FM_WL_POOL_SIZE;
  1013. ubi->fm_disabled = !fm_autoconvert;
  1014. if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
  1015. <= UBI_FM_MAX_START) {
  1016. ubi_err("More than %i PEBs are needed for fastmap, sorry.",
  1017. UBI_FM_MAX_START);
  1018. ubi->fm_disabled = 1;
  1019. }
  1020. ubi_msg("default fastmap pool size: %d", ubi->fm_pool.max_size);
  1021. ubi_msg("default fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
  1022. #else
  1023. ubi->fm_disabled = 1;
  1024. #endif
  1025. #ifndef CONFIG_UBI_SHARE_BUFFER
  1026. mutex_init(&ubi->buf_mutex);
  1027. #endif
  1028. mutex_init(&ubi->ckvol_mutex);
  1029. mutex_init(&ubi->device_mutex);
  1030. spin_lock_init(&ubi->volumes_lock);
  1031. mutex_init(&ubi->fm_mutex);
  1032. init_rwsem(&ubi->fm_sem);
  1033. ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
  1034. err = io_init(ubi, max_beb_per1024);
  1035. if (err)
  1036. goto out_free;
  1037. err = -ENOMEM;
  1038. #ifdef CONFIG_UBI_SHARE_BUFFER
  1039. if (ubi_peb_buf == NULL) {
  1040. ubi_peb_buf = vmalloc(ubi->peb_size);
  1041. mutex_init(&ubi_buf_mutex);
  1042. }
  1043. ubi->peb_buf = ubi_peb_buf;
  1044. #else
  1045. ubi->peb_buf = vmalloc(ubi->peb_size);
  1046. #endif
  1047. if (!ubi->peb_buf)
  1048. goto out_free;
  1049. #ifdef CONFIG_MTD_UBI_FASTMAP
  1050. ubi->fm_size = ubi_calc_fm_size(ubi);
  1051. ubi->fm_buf = vzalloc(ubi->fm_size);
  1052. if (!ubi->fm_buf)
  1053. goto out_free;
  1054. #endif
  1055. attach_time = sched_clock();
  1056. err = ubi_attach(ubi, 0);
  1057. if (err) {
  1058. ubi_err("failed to attach mtd%d, error %d", mtd->index, err);
  1059. goto out_free;
  1060. }
  1061. if (ubi->autoresize_vol_id != -1) {
  1062. err = autoresize(ubi, ubi->autoresize_vol_id);
  1063. if (err)
  1064. goto out_detach;
  1065. }
  1066. err = uif_init(ubi, &ref);
  1067. if (err)
  1068. goto out_detach;
  1069. err = ubi_debugfs_init_dev(ubi);
  1070. if (err)
  1071. goto out_uif;
  1072. ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
  1073. if (IS_ERR(ubi->bgt_thread)) {
  1074. err = PTR_ERR(ubi->bgt_thread);
  1075. ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
  1076. err);
  1077. goto out_debugfs;
  1078. }
  1079. attach_time = sched_clock() - attach_time;
  1080. do_div(attach_time, 1000000);
  1081. ubi_msg("attached mtd%d (name \"%s\", size %llu MiB) to ubi%d",
  1082. mtd->index, mtd->name, ubi->flash_size >> 20, ubi_num);
  1083. ubi_msg("PEB size: %d bytes (%d KiB), LEB size: %d bytes",
  1084. ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
  1085. ubi_msg("min./max. I/O unit sizes: %d/%d, sub-page size %d",
  1086. ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
  1087. ubi_msg("VID header offset: %d (aligned %d), data offset: %d",
  1088. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
  1089. ubi_msg("good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
  1090. ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
  1091. ubi_msg("user volume: %d, internal volumes: %d, max. volumes count: %d",
  1092. ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
  1093. ubi->vtbl_slots);
  1094. ubi_msg("max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
  1095. ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
  1096. ubi->image_seq);
  1097. ubi_msg("available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
  1098. ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
  1099. /*
  1100. * The below lock makes sure we do not race with 'ubi_thread()' which
  1101. * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
  1102. */
  1103. spin_lock(&ubi->wl_lock);
  1104. ubi->thread_enabled = 1;
  1105. wake_up_process(ubi->bgt_thread);
  1106. spin_unlock(&ubi->wl_lock);
  1107. ubi_devices[ubi_num] = ubi;
  1108. ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
  1109. return ubi_num;
  1110. out_debugfs:
  1111. ubi_debugfs_exit_dev(ubi);
  1112. out_uif:
  1113. get_device(&ubi->dev);
  1114. ubi_assert(ref);
  1115. uif_close(ubi);
  1116. out_detach:
  1117. ubi_wl_close(ubi);
  1118. ubi_free_internal_volumes(ubi);
  1119. vfree(ubi->vtbl);
  1120. out_free:
  1121. #ifndef CONFIG_UBI_SHARE_BUFFER
  1122. vfree(ubi->peb_buf);
  1123. #endif
  1124. vfree(ubi->fm_buf);
  1125. if (ref)
  1126. put_device(&ubi->dev);
  1127. else
  1128. kfree(ubi);
  1129. return err;
  1130. }
  1131. /**
  1132. * ubi_detach_mtd_dev - detach an MTD device.
  1133. * @ubi_num: UBI device number to detach from
  1134. * @anyway: detach MTD even if device reference count is not zero
  1135. *
  1136. * This function destroys an UBI device number @ubi_num and detaches the
  1137. * underlying MTD device. Returns zero in case of success and %-EBUSY if the
  1138. * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
  1139. * exist.
  1140. *
  1141. * Note, the invocations of this function has to be serialized by the
  1142. * @ubi_devices_mutex.
  1143. */
  1144. int ubi_detach_mtd_dev(int ubi_num, int anyway)
  1145. {
  1146. struct ubi_device *ubi;
  1147. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  1148. return -EINVAL;
  1149. ubi = ubi_get_device(ubi_num);
  1150. if (!ubi)
  1151. return -EINVAL;
  1152. spin_lock(&ubi_devices_lock);
  1153. put_device(&ubi->dev);
  1154. ubi->ref_count -= 1;
  1155. if (ubi->ref_count) {
  1156. if (!anyway) {
  1157. spin_unlock(&ubi_devices_lock);
  1158. return -EBUSY;
  1159. }
  1160. /* This may only happen if there is a bug */
  1161. ubi_err("%s reference count %d, destroy anyway",
  1162. ubi->ubi_name, ubi->ref_count);
  1163. }
  1164. ubi_devices[ubi_num] = NULL;
  1165. spin_unlock(&ubi_devices_lock);
  1166. ubi_assert(ubi_num == ubi->ubi_num);
  1167. ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
  1168. ubi_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  1169. #ifdef CONFIG_MTD_UBI_FASTMAP
  1170. /* If we don't write a new fastmap at detach time we lose all
  1171. * EC updates that have been made since the last written fastmap. */
  1172. ubi_update_fastmap(ubi);
  1173. #endif
  1174. /*
  1175. * Before freeing anything, we have to stop the background thread to
  1176. * prevent it from doing anything on this device while we are freeing.
  1177. */
  1178. if (ubi->bgt_thread)
  1179. kthread_stop(ubi->bgt_thread);
  1180. /*
  1181. * Get a reference to the device in order to prevent 'dev_release()'
  1182. * from freeing the @ubi object.
  1183. */
  1184. get_device(&ubi->dev);
  1185. ubi_debugfs_exit_dev(ubi);
  1186. uif_close(ubi);
  1187. ubi_wl_close(ubi);
  1188. ubi_free_internal_volumes(ubi);
  1189. vfree(ubi->vtbl);
  1190. put_mtd_device(ubi->mtd);
  1191. #ifdef CONFIG_MTD_UBI_LOWPAGE_BACKUP
  1192. vfree(ubi->databuf);
  1193. vfree(ubi->oobbuf);
  1194. #endif
  1195. #ifndef CONFIG_UBI_SHARE_BUFFER
  1196. vfree(ubi->peb_buf);
  1197. #endif
  1198. vfree(ubi->fm_buf);
  1199. ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
  1200. put_device(&ubi->dev);
  1201. return 0;
  1202. }
  1203. /**
  1204. * open_mtd_by_chdev - open an MTD device by its character device node path.
  1205. * @mtd_dev: MTD character device node path
  1206. *
  1207. * This helper function opens an MTD device by its character node device path.
  1208. * Returns MTD device description object in case of success and a negative
  1209. * error code in case of failure.
  1210. */
  1211. static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
  1212. {
  1213. int err, major, minor, mode;
  1214. struct path path;
  1215. /* Probably this is an MTD character device node path */
  1216. err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
  1217. if (err)
  1218. return ERR_PTR(err);
  1219. /* MTD device number is defined by the major / minor numbers */
  1220. major = imajor(path.dentry->d_inode);
  1221. minor = iminor(path.dentry->d_inode);
  1222. mode = path.dentry->d_inode->i_mode;
  1223. path_put(&path);
  1224. if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
  1225. return ERR_PTR(-EINVAL);
  1226. if (minor & 1)
  1227. /*
  1228. * Just do not think the "/dev/mtdrX" devices support is need,
  1229. * so do not support them to avoid doing extra work.
  1230. */
  1231. return ERR_PTR(-EINVAL);
  1232. return get_mtd_device(NULL, minor / 2);
  1233. }
  1234. /**
  1235. * open_mtd_device - open MTD device by name, character device path, or number.
  1236. * @mtd_dev: name, character device node path, or MTD device device number
  1237. *
  1238. * This function tries to open and MTD device described by @mtd_dev string,
  1239. * which is first treated as ASCII MTD device number, and if it is not true, it
  1240. * is treated as MTD device name, and if that is also not true, it is treated
  1241. * as MTD character device node path. Returns MTD device description object in
  1242. * case of success and a negative error code in case of failure.
  1243. */
  1244. static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
  1245. {
  1246. struct mtd_info *mtd;
  1247. int mtd_num;
  1248. char *endp;
  1249. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  1250. if (*endp != '\0' || mtd_dev == endp) {
  1251. /*
  1252. * This does not look like an ASCII integer, probably this is
  1253. * MTD device name.
  1254. */
  1255. mtd = get_mtd_device_nm(mtd_dev);
  1256. if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
  1257. /* Probably this is an MTD character device node path */
  1258. mtd = open_mtd_by_chdev(mtd_dev);
  1259. } else
  1260. mtd = get_mtd_device(NULL, mtd_num);
  1261. return mtd;
  1262. }
  1263. static int __init ubi_init(void)
  1264. {
  1265. int err, i, k;
  1266. /* Ensure that EC and VID headers have correct size */
  1267. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  1268. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  1269. if (mtd_devs > UBI_MAX_DEVICES) {
  1270. ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
  1271. return -EINVAL;
  1272. }
  1273. /* Create base sysfs directory and sysfs files */
  1274. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  1275. if (IS_ERR(ubi_class)) {
  1276. err = PTR_ERR(ubi_class);
  1277. ubi_err("cannot create UBI class");
  1278. goto out;
  1279. }
  1280. err = class_create_file(ubi_class, &ubi_version);
  1281. if (err) {
  1282. ubi_err("cannot create sysfs file");
  1283. goto out_class;
  1284. }
  1285. err = misc_register(&ubi_ctrl_cdev);
  1286. if (err) {
  1287. ubi_err("cannot register device");
  1288. goto out_version;
  1289. }
  1290. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  1291. sizeof(struct ubi_wl_entry),
  1292. 0, 0, NULL);
  1293. if (!ubi_wl_entry_slab) {
  1294. err = -ENOMEM;
  1295. goto out_dev_unreg;
  1296. }
  1297. err = ubi_debugfs_init();
  1298. if (err)
  1299. goto out_slab;
  1300. /* Attach MTD devices */
  1301. for (i = 0; i < mtd_devs; i++) {
  1302. struct mtd_dev_param *p = &mtd_dev_param[i];
  1303. struct mtd_info *mtd;
  1304. cond_resched();
  1305. mtd = open_mtd_device(p->name);
  1306. if (IS_ERR(mtd)) {
  1307. err = PTR_ERR(mtd);
  1308. ubi_err("cannot open mtd %s, error %d", p->name, err);
  1309. /* See comment below re-ubi_is_module(). */
  1310. if (ubi_is_module())
  1311. goto out_detach;
  1312. continue;
  1313. }
  1314. mutex_lock(&ubi_devices_mutex);
  1315. err = ubi_attach_mtd_dev(mtd, p->ubi_num,
  1316. p->vid_hdr_offs, p->max_beb_per1024);
  1317. mutex_unlock(&ubi_devices_mutex);
  1318. if (err < 0) {
  1319. ubi_err("cannot attach mtd%d", mtd->index);
  1320. put_mtd_device(mtd);
  1321. /*
  1322. * Originally UBI stopped initializing on any error.
  1323. * However, later on it was found out that this
  1324. * behavior is not very good when UBI is compiled into
  1325. * the kernel and the MTD devices to attach are passed
  1326. * through the command line. Indeed, UBI failure
  1327. * stopped whole boot sequence.
  1328. *
  1329. * To fix this, we changed the behavior for the
  1330. * non-module case, but preserved the old behavior for
  1331. * the module case, just for compatibility. This is a
  1332. * little inconsistent, though.
  1333. */
  1334. if (ubi_is_module())
  1335. goto out_detach;
  1336. }
  1337. }
  1338. err = ubiblock_init();
  1339. if (err) {
  1340. ubi_err("block: cannot initialize, error %d", err);
  1341. /* See comment above re-ubi_is_module(). */
  1342. if (ubi_is_module())
  1343. goto out_detach;
  1344. }
  1345. return 0;
  1346. out_detach:
  1347. for (k = 0; k < i; k++)
  1348. if (ubi_devices[k]) {
  1349. mutex_lock(&ubi_devices_mutex);
  1350. ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
  1351. mutex_unlock(&ubi_devices_mutex);
  1352. }
  1353. ubi_debugfs_exit();
  1354. out_slab:
  1355. kmem_cache_destroy(ubi_wl_entry_slab);
  1356. out_dev_unreg:
  1357. misc_deregister(&ubi_ctrl_cdev);
  1358. out_version:
  1359. class_remove_file(ubi_class, &ubi_version);
  1360. out_class:
  1361. class_destroy(ubi_class);
  1362. out:
  1363. ubi_err("cannot initialize UBI, error %d", err);
  1364. return err;
  1365. }
  1366. late_initcall(ubi_init);
  1367. static void __exit ubi_exit(void)
  1368. {
  1369. int i;
  1370. ubiblock_exit();
  1371. for (i = 0; i < UBI_MAX_DEVICES; i++)
  1372. if (ubi_devices[i]) {
  1373. mutex_lock(&ubi_devices_mutex);
  1374. ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
  1375. mutex_unlock(&ubi_devices_mutex);
  1376. }
  1377. ubi_debugfs_exit();
  1378. kmem_cache_destroy(ubi_wl_entry_slab);
  1379. misc_deregister(&ubi_ctrl_cdev);
  1380. class_remove_file(ubi_class, &ubi_version);
  1381. class_destroy(ubi_class);
  1382. #ifdef CONFIG_UBI_SHARE_BUFFER
  1383. vfree(ubi_peb_buf);
  1384. #endif
  1385. }
  1386. module_exit(ubi_exit);
  1387. /**
  1388. * bytes_str_to_int - convert a number of bytes string into an integer.
  1389. * @str: the string to convert
  1390. *
  1391. * This function returns positive resulting integer in case of success and a
  1392. * negative error code in case of failure.
  1393. */
  1394. static int __init bytes_str_to_int(const char *str)
  1395. {
  1396. char *endp;
  1397. unsigned long result;
  1398. result = simple_strtoul(str, &endp, 0);
  1399. if (str == endp || result >= INT_MAX) {
  1400. ubi_err("incorrect bytes count: \"%s\"\n", str);
  1401. return -EINVAL;
  1402. }
  1403. switch (*endp) {
  1404. case 'G':
  1405. result *= 1024;
  1406. case 'M':
  1407. result *= 1024;
  1408. case 'K':
  1409. result *= 1024;
  1410. if (endp[1] == 'i' && endp[2] == 'B')
  1411. endp += 2;
  1412. case '\0':
  1413. break;
  1414. default:
  1415. ubi_err("incorrect bytes count: \"%s\"\n", str);
  1416. return -EINVAL;
  1417. }
  1418. return result;
  1419. }
  1420. /**
  1421. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  1422. * @val: the parameter value to parse
  1423. * @kp: not used
  1424. *
  1425. * This function returns zero in case of success and a negative error code in
  1426. * case of error.
  1427. */
  1428. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1429. {
  1430. int i, len;
  1431. struct mtd_dev_param *p;
  1432. char buf[MTD_PARAM_LEN_MAX];
  1433. char *pbuf = &buf[0];
  1434. char *tokens[MTD_PARAM_MAX_COUNT], *token;
  1435. if (!val)
  1436. return -EINVAL;
  1437. if (mtd_devs == UBI_MAX_DEVICES) {
  1438. ubi_err("too many parameters, max. is %d\n",
  1439. UBI_MAX_DEVICES);
  1440. return -EINVAL;
  1441. }
  1442. len = strnlen(val, MTD_PARAM_LEN_MAX);
  1443. if (len == MTD_PARAM_LEN_MAX) {
  1444. ubi_err("parameter \"%s\" is too long, max. is %d\n",
  1445. val, MTD_PARAM_LEN_MAX);
  1446. return -EINVAL;
  1447. }
  1448. if (len == 0) {
  1449. pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
  1450. return 0;
  1451. }
  1452. strcpy(buf, val);
  1453. /* Get rid of the final newline */
  1454. if (buf[len - 1] == '\n')
  1455. buf[len - 1] = '\0';
  1456. for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
  1457. tokens[i] = strsep(&pbuf, ",");
  1458. if (pbuf) {
  1459. ubi_err("too many arguments at \"%s\"\n", val);
  1460. return -EINVAL;
  1461. }
  1462. p = &mtd_dev_param[mtd_devs];
  1463. strcpy(&p->name[0], tokens[0]);
  1464. token = tokens[1];
  1465. if (token) {
  1466. p->vid_hdr_offs = bytes_str_to_int(token);
  1467. if (p->vid_hdr_offs < 0)
  1468. return p->vid_hdr_offs;
  1469. }
  1470. token = tokens[2];
  1471. if (token) {
  1472. int err = kstrtoint(token, 10, &p->max_beb_per1024);
  1473. if (err) {
  1474. ubi_err("bad value for max_beb_per1024 parameter: %s",
  1475. token);
  1476. return -EINVAL;
  1477. }
  1478. }
  1479. token = tokens[3];
  1480. if (token) {
  1481. int err = kstrtoint(token, 10, &p->ubi_num);
  1482. if (err) {
  1483. ubi_err("bad value for ubi_num parameter: %s", token);
  1484. return -EINVAL;
  1485. }
  1486. } else
  1487. p->ubi_num = UBI_DEV_NUM_AUTO;
  1488. mtd_devs += 1;
  1489. return 0;
  1490. }
  1491. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  1492. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
  1493. "Multiple \"mtd\" parameters may be specified.\n"
  1494. "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
  1495. "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
  1496. "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
  1497. __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
  1498. "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
  1499. "\n"
  1500. "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
  1501. "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"
  1502. "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"
  1503. "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"
  1504. "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
  1505. #ifdef CONFIG_MTD_UBI_FASTMAP
  1506. module_param(fm_autoconvert, bool, 0644);
  1507. MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
  1508. #endif
  1509. MODULE_VERSION(__stringify(UBI_VERSION));
  1510. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  1511. MODULE_AUTHOR("Artem Bityutskiy");
  1512. MODULE_LICENSE("GPL");