mtdcore.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Core registration and callback routines for MTD
  4. * drivers and users.
  5. *
  6. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  7. * Copyright © 2006 Red Hat UK Limited
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/string.h>
  14. #include <linux/timer.h>
  15. #include <linux/major.h>
  16. #include <linux/fs.h>
  17. #include <linux/err.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/idr.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/gfp.h>
  25. #include <linux/slab.h>
  26. #include <linux/reboot.h>
  27. #include <linux/leds.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/nvmem-provider.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/partitions.h>
  32. #include "mtdcore.h"
  33. struct backing_dev_info *mtd_bdi;
  34. #ifdef CONFIG_PM_SLEEP
  35. static int mtd_cls_suspend(struct device *dev)
  36. {
  37. struct mtd_info *mtd = dev_get_drvdata(dev);
  38. return mtd ? mtd_suspend(mtd) : 0;
  39. }
  40. static int mtd_cls_resume(struct device *dev)
  41. {
  42. struct mtd_info *mtd = dev_get_drvdata(dev);
  43. if (mtd)
  44. mtd_resume(mtd);
  45. return 0;
  46. }
  47. static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume);
  48. #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops)
  49. #else
  50. #define MTD_CLS_PM_OPS NULL
  51. #endif
  52. static struct class mtd_class = {
  53. .name = "mtd",
  54. .owner = THIS_MODULE,
  55. .pm = MTD_CLS_PM_OPS,
  56. };
  57. static DEFINE_IDR(mtd_idr);
  58. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  59. should not use them for _anything_ else */
  60. DEFINE_MUTEX(mtd_table_mutex);
  61. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  62. struct mtd_info *__mtd_next_device(int i)
  63. {
  64. return idr_get_next(&mtd_idr, &i);
  65. }
  66. EXPORT_SYMBOL_GPL(__mtd_next_device);
  67. static LIST_HEAD(mtd_notifiers);
  68. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  69. /* REVISIT once MTD uses the driver model better, whoever allocates
  70. * the mtd_info will probably want to use the release() hook...
  71. */
  72. static void mtd_release(struct device *dev)
  73. {
  74. struct mtd_info *mtd = dev_get_drvdata(dev);
  75. dev_t index = MTD_DEVT(mtd->index);
  76. /* remove /dev/mtdXro node */
  77. device_destroy(&mtd_class, index + 1);
  78. }
  79. static ssize_t mtd_type_show(struct device *dev,
  80. struct device_attribute *attr, char *buf)
  81. {
  82. struct mtd_info *mtd = dev_get_drvdata(dev);
  83. char *type;
  84. switch (mtd->type) {
  85. case MTD_ABSENT:
  86. type = "absent";
  87. break;
  88. case MTD_RAM:
  89. type = "ram";
  90. break;
  91. case MTD_ROM:
  92. type = "rom";
  93. break;
  94. case MTD_NORFLASH:
  95. type = "nor";
  96. break;
  97. case MTD_NANDFLASH:
  98. type = "nand";
  99. break;
  100. case MTD_DATAFLASH:
  101. type = "dataflash";
  102. break;
  103. case MTD_UBIVOLUME:
  104. type = "ubi";
  105. break;
  106. case MTD_MLCNANDFLASH:
  107. type = "mlc-nand";
  108. break;
  109. default:
  110. type = "unknown";
  111. }
  112. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  113. }
  114. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  115. static ssize_t mtd_flags_show(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct mtd_info *mtd = dev_get_drvdata(dev);
  119. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  120. }
  121. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  122. static ssize_t mtd_size_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. struct mtd_info *mtd = dev_get_drvdata(dev);
  126. return snprintf(buf, PAGE_SIZE, "%llu\n",
  127. (unsigned long long)mtd->size);
  128. }
  129. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  130. static ssize_t mtd_erasesize_show(struct device *dev,
  131. struct device_attribute *attr, char *buf)
  132. {
  133. struct mtd_info *mtd = dev_get_drvdata(dev);
  134. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  135. }
  136. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  137. static ssize_t mtd_writesize_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct mtd_info *mtd = dev_get_drvdata(dev);
  141. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  142. }
  143. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  144. static ssize_t mtd_subpagesize_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct mtd_info *mtd = dev_get_drvdata(dev);
  148. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  149. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  150. }
  151. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  152. static ssize_t mtd_oobsize_show(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. struct mtd_info *mtd = dev_get_drvdata(dev);
  156. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  157. }
  158. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  159. static ssize_t mtd_oobavail_show(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. struct mtd_info *mtd = dev_get_drvdata(dev);
  163. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->oobavail);
  164. }
  165. static DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL);
  166. static ssize_t mtd_numeraseregions_show(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. struct mtd_info *mtd = dev_get_drvdata(dev);
  170. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  171. }
  172. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  173. NULL);
  174. static ssize_t mtd_name_show(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. struct mtd_info *mtd = dev_get_drvdata(dev);
  178. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  179. }
  180. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  181. static ssize_t mtd_ecc_strength_show(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. struct mtd_info *mtd = dev_get_drvdata(dev);
  185. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  186. }
  187. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  188. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  189. struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct mtd_info *mtd = dev_get_drvdata(dev);
  193. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  194. }
  195. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  196. struct device_attribute *attr,
  197. const char *buf, size_t count)
  198. {
  199. struct mtd_info *mtd = dev_get_drvdata(dev);
  200. unsigned int bitflip_threshold;
  201. int retval;
  202. retval = kstrtouint(buf, 0, &bitflip_threshold);
  203. if (retval)
  204. return retval;
  205. mtd->bitflip_threshold = bitflip_threshold;
  206. return count;
  207. }
  208. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  209. mtd_bitflip_threshold_show,
  210. mtd_bitflip_threshold_store);
  211. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct mtd_info *mtd = dev_get_drvdata(dev);
  215. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  216. }
  217. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  218. static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. struct mtd_info *mtd = dev_get_drvdata(dev);
  222. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  223. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
  224. }
  225. static DEVICE_ATTR(corrected_bits, S_IRUGO,
  226. mtd_ecc_stats_corrected_show, NULL);
  227. static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct mtd_info *mtd = dev_get_drvdata(dev);
  231. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  232. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
  233. }
  234. static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
  235. static ssize_t mtd_badblocks_show(struct device *dev,
  236. struct device_attribute *attr, char *buf)
  237. {
  238. struct mtd_info *mtd = dev_get_drvdata(dev);
  239. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  240. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
  241. }
  242. static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
  243. static ssize_t mtd_bbtblocks_show(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. struct mtd_info *mtd = dev_get_drvdata(dev);
  247. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  248. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
  249. }
  250. static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
  251. static struct attribute *mtd_attrs[] = {
  252. &dev_attr_type.attr,
  253. &dev_attr_flags.attr,
  254. &dev_attr_size.attr,
  255. &dev_attr_erasesize.attr,
  256. &dev_attr_writesize.attr,
  257. &dev_attr_subpagesize.attr,
  258. &dev_attr_oobsize.attr,
  259. &dev_attr_oobavail.attr,
  260. &dev_attr_numeraseregions.attr,
  261. &dev_attr_name.attr,
  262. &dev_attr_ecc_strength.attr,
  263. &dev_attr_ecc_step_size.attr,
  264. &dev_attr_corrected_bits.attr,
  265. &dev_attr_ecc_failures.attr,
  266. &dev_attr_bad_blocks.attr,
  267. &dev_attr_bbt_blocks.attr,
  268. &dev_attr_bitflip_threshold.attr,
  269. NULL,
  270. };
  271. ATTRIBUTE_GROUPS(mtd);
  272. static const struct device_type mtd_devtype = {
  273. .name = "mtd",
  274. .groups = mtd_groups,
  275. .release = mtd_release,
  276. };
  277. static int mtd_partid_debug_show(struct seq_file *s, void *p)
  278. {
  279. struct mtd_info *mtd = s->private;
  280. seq_printf(s, "%s\n", mtd->dbg.partid);
  281. return 0;
  282. }
  283. DEFINE_SHOW_ATTRIBUTE(mtd_partid_debug);
  284. static int mtd_partname_debug_show(struct seq_file *s, void *p)
  285. {
  286. struct mtd_info *mtd = s->private;
  287. seq_printf(s, "%s\n", mtd->dbg.partname);
  288. return 0;
  289. }
  290. DEFINE_SHOW_ATTRIBUTE(mtd_partname_debug);
  291. static struct dentry *dfs_dir_mtd;
  292. static void mtd_debugfs_populate(struct mtd_info *mtd)
  293. {
  294. struct device *dev = &mtd->dev;
  295. struct dentry *root;
  296. if (IS_ERR_OR_NULL(dfs_dir_mtd))
  297. return;
  298. root = debugfs_create_dir(dev_name(dev), dfs_dir_mtd);
  299. mtd->dbg.dfs_dir = root;
  300. if (mtd->dbg.partid)
  301. debugfs_create_file("partid", 0400, root, mtd,
  302. &mtd_partid_debug_fops);
  303. if (mtd->dbg.partname)
  304. debugfs_create_file("partname", 0400, root, mtd,
  305. &mtd_partname_debug_fops);
  306. }
  307. #ifndef CONFIG_MMU
  308. unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
  309. {
  310. switch (mtd->type) {
  311. case MTD_RAM:
  312. return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
  313. NOMMU_MAP_READ | NOMMU_MAP_WRITE;
  314. case MTD_ROM:
  315. return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
  316. NOMMU_MAP_READ;
  317. default:
  318. return NOMMU_MAP_COPY;
  319. }
  320. }
  321. EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
  322. #endif
  323. static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
  324. void *cmd)
  325. {
  326. struct mtd_info *mtd;
  327. mtd = container_of(n, struct mtd_info, reboot_notifier);
  328. mtd->_reboot(mtd);
  329. return NOTIFY_DONE;
  330. }
  331. /**
  332. * mtd_wunit_to_pairing_info - get pairing information of a wunit
  333. * @mtd: pointer to new MTD device info structure
  334. * @wunit: write unit we are interested in
  335. * @info: returned pairing information
  336. *
  337. * Retrieve pairing information associated to the wunit.
  338. * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
  339. * paired together, and where programming a page may influence the page it is
  340. * paired with.
  341. * The notion of page is replaced by the term wunit (write-unit) to stay
  342. * consistent with the ->writesize field.
  343. *
  344. * The @wunit argument can be extracted from an absolute offset using
  345. * mtd_offset_to_wunit(). @info is filled with the pairing information attached
  346. * to @wunit.
  347. *
  348. * From the pairing info the MTD user can find all the wunits paired with
  349. * @wunit using the following loop:
  350. *
  351. * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
  352. * info.pair = i;
  353. * mtd_pairing_info_to_wunit(mtd, &info);
  354. * ...
  355. * }
  356. */
  357. int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
  358. struct mtd_pairing_info *info)
  359. {
  360. struct mtd_info *master = mtd_get_master(mtd);
  361. int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master);
  362. if (wunit < 0 || wunit >= npairs)
  363. return -EINVAL;
  364. if (master->pairing && master->pairing->get_info)
  365. return master->pairing->get_info(master, wunit, info);
  366. info->group = 0;
  367. info->pair = wunit;
  368. return 0;
  369. }
  370. EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info);
  371. /**
  372. * mtd_pairing_info_to_wunit - get wunit from pairing information
  373. * @mtd: pointer to new MTD device info structure
  374. * @info: pairing information struct
  375. *
  376. * Returns a positive number representing the wunit associated to the info
  377. * struct, or a negative error code.
  378. *
  379. * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
  380. * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
  381. * doc).
  382. *
  383. * It can also be used to only program the first page of each pair (i.e.
  384. * page attached to group 0), which allows one to use an MLC NAND in
  385. * software-emulated SLC mode:
  386. *
  387. * info.group = 0;
  388. * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
  389. * for (info.pair = 0; info.pair < npairs; info.pair++) {
  390. * wunit = mtd_pairing_info_to_wunit(mtd, &info);
  391. * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
  392. * mtd->writesize, &retlen, buf + (i * mtd->writesize));
  393. * }
  394. */
  395. int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
  396. const struct mtd_pairing_info *info)
  397. {
  398. struct mtd_info *master = mtd_get_master(mtd);
  399. int ngroups = mtd_pairing_groups(master);
  400. int npairs = mtd_wunit_per_eb(master) / ngroups;
  401. if (!info || info->pair < 0 || info->pair >= npairs ||
  402. info->group < 0 || info->group >= ngroups)
  403. return -EINVAL;
  404. if (master->pairing && master->pairing->get_wunit)
  405. return mtd->pairing->get_wunit(master, info);
  406. return info->pair;
  407. }
  408. EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit);
  409. /**
  410. * mtd_pairing_groups - get the number of pairing groups
  411. * @mtd: pointer to new MTD device info structure
  412. *
  413. * Returns the number of pairing groups.
  414. *
  415. * This number is usually equal to the number of bits exposed by a single
  416. * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
  417. * to iterate over all pages of a given pair.
  418. */
  419. int mtd_pairing_groups(struct mtd_info *mtd)
  420. {
  421. struct mtd_info *master = mtd_get_master(mtd);
  422. if (!master->pairing || !master->pairing->ngroups)
  423. return 1;
  424. return master->pairing->ngroups;
  425. }
  426. EXPORT_SYMBOL_GPL(mtd_pairing_groups);
  427. static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
  428. void *val, size_t bytes)
  429. {
  430. struct mtd_info *mtd = priv;
  431. size_t retlen;
  432. int err;
  433. err = mtd_read(mtd, offset, bytes, &retlen, val);
  434. if (err && err != -EUCLEAN)
  435. return err;
  436. return retlen == bytes ? 0 : -EIO;
  437. }
  438. static int mtd_nvmem_add(struct mtd_info *mtd)
  439. {
  440. struct nvmem_config config = {};
  441. config.id = -1;
  442. config.dev = &mtd->dev;
  443. config.name = dev_name(&mtd->dev);
  444. config.owner = THIS_MODULE;
  445. config.reg_read = mtd_nvmem_reg_read;
  446. config.size = mtd->size;
  447. config.word_size = 1;
  448. config.stride = 1;
  449. config.read_only = true;
  450. config.root_only = true;
  451. config.no_of_node = true;
  452. config.priv = mtd;
  453. mtd->nvmem = nvmem_register(&config);
  454. if (IS_ERR(mtd->nvmem)) {
  455. /* Just ignore if there is no NVMEM support in the kernel */
  456. if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) {
  457. mtd->nvmem = NULL;
  458. } else {
  459. dev_err(&mtd->dev, "Failed to register NVMEM device\n");
  460. return PTR_ERR(mtd->nvmem);
  461. }
  462. }
  463. return 0;
  464. }
  465. /**
  466. * add_mtd_device - register an MTD device
  467. * @mtd: pointer to new MTD device info structure
  468. *
  469. * Add a device to the list of MTD devices present in the system, and
  470. * notify each currently active MTD 'user' of its arrival. Returns
  471. * zero on success or non-zero on failure.
  472. */
  473. int add_mtd_device(struct mtd_info *mtd)
  474. {
  475. struct mtd_info *master = mtd_get_master(mtd);
  476. struct mtd_notifier *not;
  477. int i, error;
  478. /*
  479. * May occur, for instance, on buggy drivers which call
  480. * mtd_device_parse_register() multiple times on the same master MTD,
  481. * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
  482. */
  483. if (WARN_ONCE(mtd->dev.type, "MTD already registered\n"))
  484. return -EEXIST;
  485. BUG_ON(mtd->writesize == 0);
  486. /*
  487. * MTD drivers should implement ->_{write,read}() or
  488. * ->_{write,read}_oob(), but not both.
  489. */
  490. if (WARN_ON((mtd->_write && mtd->_write_oob) ||
  491. (mtd->_read && mtd->_read_oob)))
  492. return -EINVAL;
  493. if (WARN_ON((!mtd->erasesize || !master->_erase) &&
  494. !(mtd->flags & MTD_NO_ERASE)))
  495. return -EINVAL;
  496. /*
  497. * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the
  498. * master is an MLC NAND and has a proper pairing scheme defined.
  499. * We also reject masters that implement ->_writev() for now, because
  500. * NAND controller drivers don't implement this hook, and adding the
  501. * SLC -> MLC address/length conversion to this path is useless if we
  502. * don't have a user.
  503. */
  504. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION &&
  505. (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH ||
  506. !master->pairing || master->_writev))
  507. return -EINVAL;
  508. mutex_lock(&mtd_table_mutex);
  509. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  510. if (i < 0) {
  511. error = i;
  512. goto fail_locked;
  513. }
  514. mtd->index = i;
  515. mtd->usecount = 0;
  516. /* default value if not set by driver */
  517. if (mtd->bitflip_threshold == 0)
  518. mtd->bitflip_threshold = mtd->ecc_strength;
  519. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  520. int ngroups = mtd_pairing_groups(master);
  521. mtd->erasesize /= ngroups;
  522. mtd->size = (u64)mtd_div_by_eb(mtd->size, master) *
  523. mtd->erasesize;
  524. }
  525. if (is_power_of_2(mtd->erasesize))
  526. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  527. else
  528. mtd->erasesize_shift = 0;
  529. if (is_power_of_2(mtd->writesize))
  530. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  531. else
  532. mtd->writesize_shift = 0;
  533. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  534. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  535. /* Some chips always power up locked. Unlock them now */
  536. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  537. error = mtd_unlock(mtd, 0, mtd->size);
  538. if (error && error != -EOPNOTSUPP)
  539. printk(KERN_WARNING
  540. "%s: unlock failed, writes may not work\n",
  541. mtd->name);
  542. /* Ignore unlock failures? */
  543. error = 0;
  544. }
  545. /* Caller should have set dev.parent to match the
  546. * physical device, if appropriate.
  547. */
  548. mtd->dev.type = &mtd_devtype;
  549. mtd->dev.class = &mtd_class;
  550. mtd->dev.devt = MTD_DEVT(i);
  551. dev_set_name(&mtd->dev, "mtd%d", i);
  552. dev_set_drvdata(&mtd->dev, mtd);
  553. of_node_get(mtd_get_of_node(mtd));
  554. error = device_register(&mtd->dev);
  555. if (error)
  556. goto fail_added;
  557. /* Add the nvmem provider */
  558. error = mtd_nvmem_add(mtd);
  559. if (error)
  560. goto fail_nvmem_add;
  561. mtd_debugfs_populate(mtd);
  562. device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
  563. "mtd%dro", i);
  564. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  565. /* No need to get a refcount on the module containing
  566. the notifier, since we hold the mtd_table_mutex */
  567. list_for_each_entry(not, &mtd_notifiers, list)
  568. not->add(mtd);
  569. mutex_unlock(&mtd_table_mutex);
  570. /* We _know_ we aren't being removed, because
  571. our caller is still holding us here. So none
  572. of this try_ nonsense, and no bitching about it
  573. either. :) */
  574. __module_get(THIS_MODULE);
  575. return 0;
  576. fail_nvmem_add:
  577. device_unregister(&mtd->dev);
  578. fail_added:
  579. of_node_put(mtd_get_of_node(mtd));
  580. idr_remove(&mtd_idr, i);
  581. fail_locked:
  582. mutex_unlock(&mtd_table_mutex);
  583. return error;
  584. }
  585. /**
  586. * del_mtd_device - unregister an MTD device
  587. * @mtd: pointer to MTD device info structure
  588. *
  589. * Remove a device from the list of MTD devices present in the system,
  590. * and notify each currently active MTD 'user' of its departure.
  591. * Returns zero on success or 1 on failure, which currently will happen
  592. * if the requested device does not appear to be present in the list.
  593. */
  594. int del_mtd_device(struct mtd_info *mtd)
  595. {
  596. int ret;
  597. struct mtd_notifier *not;
  598. mutex_lock(&mtd_table_mutex);
  599. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  600. ret = -ENODEV;
  601. goto out_error;
  602. }
  603. /* No need to get a refcount on the module containing
  604. the notifier, since we hold the mtd_table_mutex */
  605. list_for_each_entry(not, &mtd_notifiers, list)
  606. not->remove(mtd);
  607. if (mtd->usecount) {
  608. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  609. mtd->index, mtd->name, mtd->usecount);
  610. ret = -EBUSY;
  611. } else {
  612. debugfs_remove_recursive(mtd->dbg.dfs_dir);
  613. /* Try to remove the NVMEM provider */
  614. if (mtd->nvmem)
  615. nvmem_unregister(mtd->nvmem);
  616. device_unregister(&mtd->dev);
  617. idr_remove(&mtd_idr, mtd->index);
  618. of_node_put(mtd_get_of_node(mtd));
  619. module_put(THIS_MODULE);
  620. ret = 0;
  621. }
  622. out_error:
  623. mutex_unlock(&mtd_table_mutex);
  624. return ret;
  625. }
  626. /*
  627. * Set a few defaults based on the parent devices, if not provided by the
  628. * driver
  629. */
  630. static void mtd_set_dev_defaults(struct mtd_info *mtd)
  631. {
  632. if (mtd->dev.parent) {
  633. if (!mtd->owner && mtd->dev.parent->driver)
  634. mtd->owner = mtd->dev.parent->driver->owner;
  635. if (!mtd->name)
  636. mtd->name = dev_name(mtd->dev.parent);
  637. } else {
  638. pr_debug("mtd device won't show a device symlink in sysfs\n");
  639. }
  640. INIT_LIST_HEAD(&mtd->partitions);
  641. mutex_init(&mtd->master.partitions_lock);
  642. }
  643. /**
  644. * mtd_device_parse_register - parse partitions and register an MTD device.
  645. *
  646. * @mtd: the MTD device to register
  647. * @types: the list of MTD partition probes to try, see
  648. * 'parse_mtd_partitions()' for more information
  649. * @parser_data: MTD partition parser-specific data
  650. * @parts: fallback partition information to register, if parsing fails;
  651. * only valid if %nr_parts > %0
  652. * @nr_parts: the number of partitions in parts, if zero then the full
  653. * MTD device is registered if no partition info is found
  654. *
  655. * This function aggregates MTD partitions parsing (done by
  656. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  657. * basically follows the most common pattern found in many MTD drivers:
  658. *
  659. * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
  660. * registered first.
  661. * * Then It tries to probe partitions on MTD device @mtd using parsers
  662. * specified in @types (if @types is %NULL, then the default list of parsers
  663. * is used, see 'parse_mtd_partitions()' for more information). If none are
  664. * found this functions tries to fallback to information specified in
  665. * @parts/@nr_parts.
  666. * * If no partitions were found this function just registers the MTD device
  667. * @mtd and exits.
  668. *
  669. * Returns zero in case of success and a negative error code in case of failure.
  670. */
  671. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  672. struct mtd_part_parser_data *parser_data,
  673. const struct mtd_partition *parts,
  674. int nr_parts)
  675. {
  676. int ret;
  677. mtd_set_dev_defaults(mtd);
  678. if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
  679. ret = add_mtd_device(mtd);
  680. if (ret)
  681. return ret;
  682. }
  683. /* Prefer parsed partitions over driver-provided fallback */
  684. ret = parse_mtd_partitions(mtd, types, parser_data);
  685. if (ret == -EPROBE_DEFER)
  686. goto out;
  687. if (ret > 0)
  688. ret = 0;
  689. else if (nr_parts)
  690. ret = add_mtd_partitions(mtd, parts, nr_parts);
  691. else if (!device_is_registered(&mtd->dev))
  692. ret = add_mtd_device(mtd);
  693. else
  694. ret = 0;
  695. if (ret)
  696. goto out;
  697. /*
  698. * FIXME: some drivers unfortunately call this function more than once.
  699. * So we have to check if we've already assigned the reboot notifier.
  700. *
  701. * Generally, we can make multiple calls work for most cases, but it
  702. * does cause problems with parse_mtd_partitions() above (e.g.,
  703. * cmdlineparts will register partitions more than once).
  704. */
  705. WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
  706. "MTD already registered\n");
  707. if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
  708. mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
  709. register_reboot_notifier(&mtd->reboot_notifier);
  710. }
  711. out:
  712. if (ret && device_is_registered(&mtd->dev))
  713. del_mtd_device(mtd);
  714. return ret;
  715. }
  716. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  717. /**
  718. * mtd_device_unregister - unregister an existing MTD device.
  719. *
  720. * @master: the MTD device to unregister. This will unregister both the master
  721. * and any partitions if registered.
  722. */
  723. int mtd_device_unregister(struct mtd_info *master)
  724. {
  725. int err;
  726. if (master->_reboot)
  727. unregister_reboot_notifier(&master->reboot_notifier);
  728. err = del_mtd_partitions(master);
  729. if (err)
  730. return err;
  731. if (!device_is_registered(&master->dev))
  732. return 0;
  733. return del_mtd_device(master);
  734. }
  735. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  736. /**
  737. * register_mtd_user - register a 'user' of MTD devices.
  738. * @new: pointer to notifier info structure
  739. *
  740. * Registers a pair of callbacks function to be called upon addition
  741. * or removal of MTD devices. Causes the 'add' callback to be immediately
  742. * invoked for each MTD device currently present in the system.
  743. */
  744. void register_mtd_user (struct mtd_notifier *new)
  745. {
  746. struct mtd_info *mtd;
  747. mutex_lock(&mtd_table_mutex);
  748. list_add(&new->list, &mtd_notifiers);
  749. __module_get(THIS_MODULE);
  750. mtd_for_each_device(mtd)
  751. new->add(mtd);
  752. mutex_unlock(&mtd_table_mutex);
  753. }
  754. EXPORT_SYMBOL_GPL(register_mtd_user);
  755. /**
  756. * unregister_mtd_user - unregister a 'user' of MTD devices.
  757. * @old: pointer to notifier info structure
  758. *
  759. * Removes a callback function pair from the list of 'users' to be
  760. * notified upon addition or removal of MTD devices. Causes the
  761. * 'remove' callback to be immediately invoked for each MTD device
  762. * currently present in the system.
  763. */
  764. int unregister_mtd_user (struct mtd_notifier *old)
  765. {
  766. struct mtd_info *mtd;
  767. mutex_lock(&mtd_table_mutex);
  768. module_put(THIS_MODULE);
  769. mtd_for_each_device(mtd)
  770. old->remove(mtd);
  771. list_del(&old->list);
  772. mutex_unlock(&mtd_table_mutex);
  773. return 0;
  774. }
  775. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  776. /**
  777. * get_mtd_device - obtain a validated handle for an MTD device
  778. * @mtd: last known address of the required MTD device
  779. * @num: internal device number of the required MTD device
  780. *
  781. * Given a number and NULL address, return the num'th entry in the device
  782. * table, if any. Given an address and num == -1, search the device table
  783. * for a device with that address and return if it's still present. Given
  784. * both, return the num'th driver only if its address matches. Return
  785. * error code if not.
  786. */
  787. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  788. {
  789. struct mtd_info *ret = NULL, *other;
  790. int err = -ENODEV;
  791. mutex_lock(&mtd_table_mutex);
  792. if (num == -1) {
  793. mtd_for_each_device(other) {
  794. if (other == mtd) {
  795. ret = mtd;
  796. break;
  797. }
  798. }
  799. } else if (num >= 0) {
  800. ret = idr_find(&mtd_idr, num);
  801. if (mtd && mtd != ret)
  802. ret = NULL;
  803. }
  804. if (!ret) {
  805. ret = ERR_PTR(err);
  806. goto out;
  807. }
  808. err = __get_mtd_device(ret);
  809. if (err)
  810. ret = ERR_PTR(err);
  811. out:
  812. mutex_unlock(&mtd_table_mutex);
  813. return ret;
  814. }
  815. EXPORT_SYMBOL_GPL(get_mtd_device);
  816. int __get_mtd_device(struct mtd_info *mtd)
  817. {
  818. struct mtd_info *master = mtd_get_master(mtd);
  819. int err;
  820. if (!try_module_get(master->owner))
  821. return -ENODEV;
  822. if (master->_get_device) {
  823. err = master->_get_device(mtd);
  824. if (err) {
  825. module_put(master->owner);
  826. return err;
  827. }
  828. }
  829. master->usecount++;
  830. while (mtd->parent) {
  831. mtd->usecount++;
  832. mtd = mtd->parent;
  833. }
  834. return 0;
  835. }
  836. EXPORT_SYMBOL_GPL(__get_mtd_device);
  837. /**
  838. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  839. * device name
  840. * @name: MTD device name to open
  841. *
  842. * This function returns MTD device description structure in case of
  843. * success and an error code in case of failure.
  844. */
  845. struct mtd_info *get_mtd_device_nm(const char *name)
  846. {
  847. int err = -ENODEV;
  848. struct mtd_info *mtd = NULL, *other;
  849. mutex_lock(&mtd_table_mutex);
  850. mtd_for_each_device(other) {
  851. if (!strcmp(name, other->name)) {
  852. mtd = other;
  853. break;
  854. }
  855. }
  856. if (!mtd)
  857. goto out_unlock;
  858. err = __get_mtd_device(mtd);
  859. if (err)
  860. goto out_unlock;
  861. mutex_unlock(&mtd_table_mutex);
  862. return mtd;
  863. out_unlock:
  864. mutex_unlock(&mtd_table_mutex);
  865. return ERR_PTR(err);
  866. }
  867. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  868. void put_mtd_device(struct mtd_info *mtd)
  869. {
  870. mutex_lock(&mtd_table_mutex);
  871. __put_mtd_device(mtd);
  872. mutex_unlock(&mtd_table_mutex);
  873. }
  874. EXPORT_SYMBOL_GPL(put_mtd_device);
  875. void __put_mtd_device(struct mtd_info *mtd)
  876. {
  877. struct mtd_info *master = mtd_get_master(mtd);
  878. while (mtd->parent) {
  879. --mtd->usecount;
  880. BUG_ON(mtd->usecount < 0);
  881. mtd = mtd->parent;
  882. }
  883. master->usecount--;
  884. if (master->_put_device)
  885. master->_put_device(master);
  886. module_put(master->owner);
  887. }
  888. EXPORT_SYMBOL_GPL(__put_mtd_device);
  889. /*
  890. * Erase is an synchronous operation. Device drivers are epected to return a
  891. * negative error code if the operation failed and update instr->fail_addr
  892. * to point the portion that was not properly erased.
  893. */
  894. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  895. {
  896. struct mtd_info *master = mtd_get_master(mtd);
  897. u64 mst_ofs = mtd_get_master_ofs(mtd, 0);
  898. struct erase_info adjinstr;
  899. int ret;
  900. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  901. adjinstr = *instr;
  902. if (!mtd->erasesize || !master->_erase)
  903. return -ENOTSUPP;
  904. if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
  905. return -EINVAL;
  906. if (!(mtd->flags & MTD_WRITEABLE))
  907. return -EROFS;
  908. if (!instr->len)
  909. return 0;
  910. ledtrig_mtd_activity();
  911. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  912. adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) *
  913. master->erasesize;
  914. adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) *
  915. master->erasesize) -
  916. adjinstr.addr;
  917. }
  918. adjinstr.addr += mst_ofs;
  919. ret = master->_erase(master, &adjinstr);
  920. if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) {
  921. instr->fail_addr = adjinstr.fail_addr - mst_ofs;
  922. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  923. instr->fail_addr = mtd_div_by_eb(instr->fail_addr,
  924. master);
  925. instr->fail_addr *= mtd->erasesize;
  926. }
  927. }
  928. return ret;
  929. }
  930. EXPORT_SYMBOL_GPL(mtd_erase);
  931. /*
  932. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  933. */
  934. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  935. void **virt, resource_size_t *phys)
  936. {
  937. struct mtd_info *master = mtd_get_master(mtd);
  938. *retlen = 0;
  939. *virt = NULL;
  940. if (phys)
  941. *phys = 0;
  942. if (!master->_point)
  943. return -EOPNOTSUPP;
  944. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  945. return -EINVAL;
  946. if (!len)
  947. return 0;
  948. from = mtd_get_master_ofs(mtd, from);
  949. return master->_point(master, from, len, retlen, virt, phys);
  950. }
  951. EXPORT_SYMBOL_GPL(mtd_point);
  952. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  953. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  954. {
  955. struct mtd_info *master = mtd_get_master(mtd);
  956. if (!master->_unpoint)
  957. return -EOPNOTSUPP;
  958. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  959. return -EINVAL;
  960. if (!len)
  961. return 0;
  962. return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len);
  963. }
  964. EXPORT_SYMBOL_GPL(mtd_unpoint);
  965. /*
  966. * Allow NOMMU mmap() to directly map the device (if not NULL)
  967. * - return the address to which the offset maps
  968. * - return -ENOSYS to indicate refusal to do the mapping
  969. */
  970. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  971. unsigned long offset, unsigned long flags)
  972. {
  973. size_t retlen;
  974. void *virt;
  975. int ret;
  976. ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL);
  977. if (ret)
  978. return ret;
  979. if (retlen != len) {
  980. mtd_unpoint(mtd, offset, retlen);
  981. return -ENOSYS;
  982. }
  983. return (unsigned long)virt;
  984. }
  985. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  986. static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master,
  987. const struct mtd_ecc_stats *old_stats)
  988. {
  989. struct mtd_ecc_stats diff;
  990. if (master == mtd)
  991. return;
  992. diff = master->ecc_stats;
  993. diff.failed -= old_stats->failed;
  994. diff.corrected -= old_stats->corrected;
  995. while (mtd->parent) {
  996. mtd->ecc_stats.failed += diff.failed;
  997. mtd->ecc_stats.corrected += diff.corrected;
  998. mtd = mtd->parent;
  999. }
  1000. }
  1001. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  1002. u_char *buf)
  1003. {
  1004. struct mtd_oob_ops ops = {
  1005. .len = len,
  1006. .datbuf = buf,
  1007. };
  1008. int ret;
  1009. ret = mtd_read_oob(mtd, from, &ops);
  1010. *retlen = ops.retlen;
  1011. return ret;
  1012. }
  1013. EXPORT_SYMBOL_GPL(mtd_read);
  1014. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  1015. const u_char *buf)
  1016. {
  1017. struct mtd_oob_ops ops = {
  1018. .len = len,
  1019. .datbuf = (u8 *)buf,
  1020. };
  1021. int ret;
  1022. ret = mtd_write_oob(mtd, to, &ops);
  1023. *retlen = ops.retlen;
  1024. return ret;
  1025. }
  1026. EXPORT_SYMBOL_GPL(mtd_write);
  1027. /*
  1028. * In blackbox flight recorder like scenarios we want to make successful writes
  1029. * in interrupt context. panic_write() is only intended to be called when its
  1030. * known the kernel is about to panic and we need the write to succeed. Since
  1031. * the kernel is not going to be running for much longer, this function can
  1032. * break locks and delay to ensure the write succeeds (but not sleep).
  1033. */
  1034. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  1035. const u_char *buf)
  1036. {
  1037. struct mtd_info *master = mtd_get_master(mtd);
  1038. *retlen = 0;
  1039. if (!master->_panic_write)
  1040. return -EOPNOTSUPP;
  1041. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  1042. return -EINVAL;
  1043. if (!(mtd->flags & MTD_WRITEABLE))
  1044. return -EROFS;
  1045. if (!len)
  1046. return 0;
  1047. if (!master->oops_panic_write)
  1048. master->oops_panic_write = true;
  1049. return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len,
  1050. retlen, buf);
  1051. }
  1052. EXPORT_SYMBOL_GPL(mtd_panic_write);
  1053. static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
  1054. struct mtd_oob_ops *ops)
  1055. {
  1056. /*
  1057. * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
  1058. * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
  1059. * this case.
  1060. */
  1061. if (!ops->datbuf)
  1062. ops->len = 0;
  1063. if (!ops->oobbuf)
  1064. ops->ooblen = 0;
  1065. if (offs < 0 || offs + ops->len > mtd->size)
  1066. return -EINVAL;
  1067. if (ops->ooblen) {
  1068. size_t maxooblen;
  1069. if (ops->ooboffs >= mtd_oobavail(mtd, ops))
  1070. return -EINVAL;
  1071. maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
  1072. mtd_div_by_ws(offs, mtd)) *
  1073. mtd_oobavail(mtd, ops)) - ops->ooboffs;
  1074. if (ops->ooblen > maxooblen)
  1075. return -EINVAL;
  1076. }
  1077. return 0;
  1078. }
  1079. static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
  1080. struct mtd_oob_ops *ops)
  1081. {
  1082. struct mtd_info *master = mtd_get_master(mtd);
  1083. int ret;
  1084. from = mtd_get_master_ofs(mtd, from);
  1085. if (master->_read_oob)
  1086. ret = master->_read_oob(master, from, ops);
  1087. else
  1088. ret = master->_read(master, from, ops->len, &ops->retlen,
  1089. ops->datbuf);
  1090. return ret;
  1091. }
  1092. static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to,
  1093. struct mtd_oob_ops *ops)
  1094. {
  1095. struct mtd_info *master = mtd_get_master(mtd);
  1096. int ret;
  1097. to = mtd_get_master_ofs(mtd, to);
  1098. if (master->_write_oob)
  1099. ret = master->_write_oob(master, to, ops);
  1100. else
  1101. ret = master->_write(master, to, ops->len, &ops->retlen,
  1102. ops->datbuf);
  1103. return ret;
  1104. }
  1105. static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read,
  1106. struct mtd_oob_ops *ops)
  1107. {
  1108. struct mtd_info *master = mtd_get_master(mtd);
  1109. int ngroups = mtd_pairing_groups(master);
  1110. int npairs = mtd_wunit_per_eb(master) / ngroups;
  1111. struct mtd_oob_ops adjops = *ops;
  1112. unsigned int wunit, oobavail;
  1113. struct mtd_pairing_info info;
  1114. int max_bitflips = 0;
  1115. u32 ebofs, pageofs;
  1116. loff_t base, pos;
  1117. ebofs = mtd_mod_by_eb(start, mtd);
  1118. base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize;
  1119. info.group = 0;
  1120. info.pair = mtd_div_by_ws(ebofs, mtd);
  1121. pageofs = mtd_mod_by_ws(ebofs, mtd);
  1122. oobavail = mtd_oobavail(mtd, ops);
  1123. while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
  1124. int ret;
  1125. if (info.pair >= npairs) {
  1126. info.pair = 0;
  1127. base += master->erasesize;
  1128. }
  1129. wunit = mtd_pairing_info_to_wunit(master, &info);
  1130. pos = mtd_wunit_to_offset(mtd, base, wunit);
  1131. adjops.len = ops->len - ops->retlen;
  1132. if (adjops.len > mtd->writesize - pageofs)
  1133. adjops.len = mtd->writesize - pageofs;
  1134. adjops.ooblen = ops->ooblen - ops->oobretlen;
  1135. if (adjops.ooblen > oobavail - adjops.ooboffs)
  1136. adjops.ooblen = oobavail - adjops.ooboffs;
  1137. if (read) {
  1138. ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops);
  1139. if (ret > 0)
  1140. max_bitflips = max(max_bitflips, ret);
  1141. } else {
  1142. ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops);
  1143. }
  1144. if (ret < 0)
  1145. return ret;
  1146. max_bitflips = max(max_bitflips, ret);
  1147. ops->retlen += adjops.retlen;
  1148. ops->oobretlen += adjops.oobretlen;
  1149. adjops.datbuf += adjops.retlen;
  1150. adjops.oobbuf += adjops.oobretlen;
  1151. adjops.ooboffs = 0;
  1152. pageofs = 0;
  1153. info.pair++;
  1154. }
  1155. return max_bitflips;
  1156. }
  1157. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  1158. {
  1159. struct mtd_info *master = mtd_get_master(mtd);
  1160. struct mtd_ecc_stats old_stats = master->ecc_stats;
  1161. int ret_code;
  1162. ops->retlen = ops->oobretlen = 0;
  1163. ret_code = mtd_check_oob_ops(mtd, from, ops);
  1164. if (ret_code)
  1165. return ret_code;
  1166. ledtrig_mtd_activity();
  1167. /* Check the validity of a potential fallback on mtd->_read */
  1168. if (!master->_read_oob && (!master->_read || ops->oobbuf))
  1169. return -EOPNOTSUPP;
  1170. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1171. ret_code = mtd_io_emulated_slc(mtd, from, true, ops);
  1172. else
  1173. ret_code = mtd_read_oob_std(mtd, from, ops);
  1174. mtd_update_ecc_stats(mtd, master, &old_stats);
  1175. /*
  1176. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  1177. * similar to mtd->_read(), returning a non-negative integer
  1178. * representing max bitflips. In other cases, mtd->_read_oob() may
  1179. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  1180. */
  1181. if (unlikely(ret_code < 0))
  1182. return ret_code;
  1183. if (mtd->ecc_strength == 0)
  1184. return 0; /* device lacks ecc */
  1185. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  1186. }
  1187. EXPORT_SYMBOL_GPL(mtd_read_oob);
  1188. int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  1189. struct mtd_oob_ops *ops)
  1190. {
  1191. struct mtd_info *master = mtd_get_master(mtd);
  1192. int ret;
  1193. ops->retlen = ops->oobretlen = 0;
  1194. if (!(mtd->flags & MTD_WRITEABLE))
  1195. return -EROFS;
  1196. ret = mtd_check_oob_ops(mtd, to, ops);
  1197. if (ret)
  1198. return ret;
  1199. ledtrig_mtd_activity();
  1200. /* Check the validity of a potential fallback on mtd->_write */
  1201. if (!master->_write_oob && (!master->_write || ops->oobbuf))
  1202. return -EOPNOTSUPP;
  1203. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1204. return mtd_io_emulated_slc(mtd, to, false, ops);
  1205. return mtd_write_oob_std(mtd, to, ops);
  1206. }
  1207. EXPORT_SYMBOL_GPL(mtd_write_oob);
  1208. /**
  1209. * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
  1210. * @mtd: MTD device structure
  1211. * @section: ECC section. Depending on the layout you may have all the ECC
  1212. * bytes stored in a single contiguous section, or one section
  1213. * per ECC chunk (and sometime several sections for a single ECC
  1214. * ECC chunk)
  1215. * @oobecc: OOB region struct filled with the appropriate ECC position
  1216. * information
  1217. *
  1218. * This function returns ECC section information in the OOB area. If you want
  1219. * to get all the ECC bytes information, then you should call
  1220. * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
  1221. *
  1222. * Returns zero on success, a negative error code otherwise.
  1223. */
  1224. int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  1225. struct mtd_oob_region *oobecc)
  1226. {
  1227. struct mtd_info *master = mtd_get_master(mtd);
  1228. memset(oobecc, 0, sizeof(*oobecc));
  1229. if (!master || section < 0)
  1230. return -EINVAL;
  1231. if (!master->ooblayout || !master->ooblayout->ecc)
  1232. return -ENOTSUPP;
  1233. return master->ooblayout->ecc(master, section, oobecc);
  1234. }
  1235. EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
  1236. /**
  1237. * mtd_ooblayout_free - Get the OOB region definition of a specific free
  1238. * section
  1239. * @mtd: MTD device structure
  1240. * @section: Free section you are interested in. Depending on the layout
  1241. * you may have all the free bytes stored in a single contiguous
  1242. * section, or one section per ECC chunk plus an extra section
  1243. * for the remaining bytes (or other funky layout).
  1244. * @oobfree: OOB region struct filled with the appropriate free position
  1245. * information
  1246. *
  1247. * This function returns free bytes position in the OOB area. If you want
  1248. * to get all the free bytes information, then you should call
  1249. * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
  1250. *
  1251. * Returns zero on success, a negative error code otherwise.
  1252. */
  1253. int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  1254. struct mtd_oob_region *oobfree)
  1255. {
  1256. struct mtd_info *master = mtd_get_master(mtd);
  1257. memset(oobfree, 0, sizeof(*oobfree));
  1258. if (!master || section < 0)
  1259. return -EINVAL;
  1260. if (!master->ooblayout || !master->ooblayout->free)
  1261. return -ENOTSUPP;
  1262. return master->ooblayout->free(master, section, oobfree);
  1263. }
  1264. EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
  1265. /**
  1266. * mtd_ooblayout_find_region - Find the region attached to a specific byte
  1267. * @mtd: mtd info structure
  1268. * @byte: the byte we are searching for
  1269. * @sectionp: pointer where the section id will be stored
  1270. * @oobregion: used to retrieve the ECC position
  1271. * @iter: iterator function. Should be either mtd_ooblayout_free or
  1272. * mtd_ooblayout_ecc depending on the region type you're searching for
  1273. *
  1274. * This function returns the section id and oobregion information of a
  1275. * specific byte. For example, say you want to know where the 4th ECC byte is
  1276. * stored, you'll use:
  1277. *
  1278. * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
  1279. *
  1280. * Returns zero on success, a negative error code otherwise.
  1281. */
  1282. static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
  1283. int *sectionp, struct mtd_oob_region *oobregion,
  1284. int (*iter)(struct mtd_info *,
  1285. int section,
  1286. struct mtd_oob_region *oobregion))
  1287. {
  1288. int pos = 0, ret, section = 0;
  1289. memset(oobregion, 0, sizeof(*oobregion));
  1290. while (1) {
  1291. ret = iter(mtd, section, oobregion);
  1292. if (ret)
  1293. return ret;
  1294. if (pos + oobregion->length > byte)
  1295. break;
  1296. pos += oobregion->length;
  1297. section++;
  1298. }
  1299. /*
  1300. * Adjust region info to make it start at the beginning at the
  1301. * 'start' ECC byte.
  1302. */
  1303. oobregion->offset += byte - pos;
  1304. oobregion->length -= byte - pos;
  1305. *sectionp = section;
  1306. return 0;
  1307. }
  1308. /**
  1309. * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
  1310. * ECC byte
  1311. * @mtd: mtd info structure
  1312. * @eccbyte: the byte we are searching for
  1313. * @sectionp: pointer where the section id will be stored
  1314. * @oobregion: OOB region information
  1315. *
  1316. * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
  1317. * byte.
  1318. *
  1319. * Returns zero on success, a negative error code otherwise.
  1320. */
  1321. int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  1322. int *section,
  1323. struct mtd_oob_region *oobregion)
  1324. {
  1325. return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
  1326. mtd_ooblayout_ecc);
  1327. }
  1328. EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
  1329. /**
  1330. * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
  1331. * @mtd: mtd info structure
  1332. * @buf: destination buffer to store OOB bytes
  1333. * @oobbuf: OOB buffer
  1334. * @start: first byte to retrieve
  1335. * @nbytes: number of bytes to retrieve
  1336. * @iter: section iterator
  1337. *
  1338. * Extract bytes attached to a specific category (ECC or free)
  1339. * from the OOB buffer and copy them into buf.
  1340. *
  1341. * Returns zero on success, a negative error code otherwise.
  1342. */
  1343. static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
  1344. const u8 *oobbuf, int start, int nbytes,
  1345. int (*iter)(struct mtd_info *,
  1346. int section,
  1347. struct mtd_oob_region *oobregion))
  1348. {
  1349. struct mtd_oob_region oobregion;
  1350. int section, ret;
  1351. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1352. &oobregion, iter);
  1353. while (!ret) {
  1354. int cnt;
  1355. cnt = min_t(int, nbytes, oobregion.length);
  1356. memcpy(buf, oobbuf + oobregion.offset, cnt);
  1357. buf += cnt;
  1358. nbytes -= cnt;
  1359. if (!nbytes)
  1360. break;
  1361. ret = iter(mtd, ++section, &oobregion);
  1362. }
  1363. return ret;
  1364. }
  1365. /**
  1366. * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
  1367. * @mtd: mtd info structure
  1368. * @buf: source buffer to get OOB bytes from
  1369. * @oobbuf: OOB buffer
  1370. * @start: first OOB byte to set
  1371. * @nbytes: number of OOB bytes to set
  1372. * @iter: section iterator
  1373. *
  1374. * Fill the OOB buffer with data provided in buf. The category (ECC or free)
  1375. * is selected by passing the appropriate iterator.
  1376. *
  1377. * Returns zero on success, a negative error code otherwise.
  1378. */
  1379. static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
  1380. u8 *oobbuf, int start, int nbytes,
  1381. int (*iter)(struct mtd_info *,
  1382. int section,
  1383. struct mtd_oob_region *oobregion))
  1384. {
  1385. struct mtd_oob_region oobregion;
  1386. int section, ret;
  1387. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1388. &oobregion, iter);
  1389. while (!ret) {
  1390. int cnt;
  1391. cnt = min_t(int, nbytes, oobregion.length);
  1392. memcpy(oobbuf + oobregion.offset, buf, cnt);
  1393. buf += cnt;
  1394. nbytes -= cnt;
  1395. if (!nbytes)
  1396. break;
  1397. ret = iter(mtd, ++section, &oobregion);
  1398. }
  1399. return ret;
  1400. }
  1401. /**
  1402. * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
  1403. * @mtd: mtd info structure
  1404. * @iter: category iterator
  1405. *
  1406. * Count the number of bytes in a given category.
  1407. *
  1408. * Returns a positive value on success, a negative error code otherwise.
  1409. */
  1410. static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
  1411. int (*iter)(struct mtd_info *,
  1412. int section,
  1413. struct mtd_oob_region *oobregion))
  1414. {
  1415. struct mtd_oob_region oobregion;
  1416. int section = 0, ret, nbytes = 0;
  1417. while (1) {
  1418. ret = iter(mtd, section++, &oobregion);
  1419. if (ret) {
  1420. if (ret == -ERANGE)
  1421. ret = nbytes;
  1422. break;
  1423. }
  1424. nbytes += oobregion.length;
  1425. }
  1426. return ret;
  1427. }
  1428. /**
  1429. * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
  1430. * @mtd: mtd info structure
  1431. * @eccbuf: destination buffer to store ECC bytes
  1432. * @oobbuf: OOB buffer
  1433. * @start: first ECC byte to retrieve
  1434. * @nbytes: number of ECC bytes to retrieve
  1435. *
  1436. * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
  1437. *
  1438. * Returns zero on success, a negative error code otherwise.
  1439. */
  1440. int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  1441. const u8 *oobbuf, int start, int nbytes)
  1442. {
  1443. return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1444. mtd_ooblayout_ecc);
  1445. }
  1446. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
  1447. /**
  1448. * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
  1449. * @mtd: mtd info structure
  1450. * @eccbuf: source buffer to get ECC bytes from
  1451. * @oobbuf: OOB buffer
  1452. * @start: first ECC byte to set
  1453. * @nbytes: number of ECC bytes to set
  1454. *
  1455. * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
  1456. *
  1457. * Returns zero on success, a negative error code otherwise.
  1458. */
  1459. int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  1460. u8 *oobbuf, int start, int nbytes)
  1461. {
  1462. return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1463. mtd_ooblayout_ecc);
  1464. }
  1465. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
  1466. /**
  1467. * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
  1468. * @mtd: mtd info structure
  1469. * @databuf: destination buffer to store ECC bytes
  1470. * @oobbuf: OOB buffer
  1471. * @start: first ECC byte to retrieve
  1472. * @nbytes: number of ECC bytes to retrieve
  1473. *
  1474. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1475. *
  1476. * Returns zero on success, a negative error code otherwise.
  1477. */
  1478. int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  1479. const u8 *oobbuf, int start, int nbytes)
  1480. {
  1481. return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
  1482. mtd_ooblayout_free);
  1483. }
  1484. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
  1485. /**
  1486. * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
  1487. * @mtd: mtd info structure
  1488. * @databuf: source buffer to get data bytes from
  1489. * @oobbuf: OOB buffer
  1490. * @start: first ECC byte to set
  1491. * @nbytes: number of ECC bytes to set
  1492. *
  1493. * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes.
  1494. *
  1495. * Returns zero on success, a negative error code otherwise.
  1496. */
  1497. int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  1498. u8 *oobbuf, int start, int nbytes)
  1499. {
  1500. return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
  1501. mtd_ooblayout_free);
  1502. }
  1503. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
  1504. /**
  1505. * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
  1506. * @mtd: mtd info structure
  1507. *
  1508. * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
  1509. *
  1510. * Returns zero on success, a negative error code otherwise.
  1511. */
  1512. int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
  1513. {
  1514. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
  1515. }
  1516. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
  1517. /**
  1518. * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
  1519. * @mtd: mtd info structure
  1520. *
  1521. * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
  1522. *
  1523. * Returns zero on success, a negative error code otherwise.
  1524. */
  1525. int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
  1526. {
  1527. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
  1528. }
  1529. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
  1530. /*
  1531. * Method to access the protection register area, present in some flash
  1532. * devices. The user data is one time programmable but the factory data is read
  1533. * only.
  1534. */
  1535. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1536. struct otp_info *buf)
  1537. {
  1538. struct mtd_info *master = mtd_get_master(mtd);
  1539. if (!master->_get_fact_prot_info)
  1540. return -EOPNOTSUPP;
  1541. if (!len)
  1542. return 0;
  1543. return master->_get_fact_prot_info(master, len, retlen, buf);
  1544. }
  1545. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  1546. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1547. size_t *retlen, u_char *buf)
  1548. {
  1549. struct mtd_info *master = mtd_get_master(mtd);
  1550. *retlen = 0;
  1551. if (!master->_read_fact_prot_reg)
  1552. return -EOPNOTSUPP;
  1553. if (!len)
  1554. return 0;
  1555. return master->_read_fact_prot_reg(master, from, len, retlen, buf);
  1556. }
  1557. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  1558. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1559. struct otp_info *buf)
  1560. {
  1561. struct mtd_info *master = mtd_get_master(mtd);
  1562. if (!master->_get_user_prot_info)
  1563. return -EOPNOTSUPP;
  1564. if (!len)
  1565. return 0;
  1566. return master->_get_user_prot_info(master, len, retlen, buf);
  1567. }
  1568. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  1569. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1570. size_t *retlen, u_char *buf)
  1571. {
  1572. struct mtd_info *master = mtd_get_master(mtd);
  1573. *retlen = 0;
  1574. if (!master->_read_user_prot_reg)
  1575. return -EOPNOTSUPP;
  1576. if (!len)
  1577. return 0;
  1578. return master->_read_user_prot_reg(master, from, len, retlen, buf);
  1579. }
  1580. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  1581. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  1582. size_t *retlen, u_char *buf)
  1583. {
  1584. struct mtd_info *master = mtd_get_master(mtd);
  1585. int ret;
  1586. *retlen = 0;
  1587. if (!master->_write_user_prot_reg)
  1588. return -EOPNOTSUPP;
  1589. if (!len)
  1590. return 0;
  1591. ret = master->_write_user_prot_reg(master, to, len, retlen, buf);
  1592. if (ret)
  1593. return ret;
  1594. /*
  1595. * If no data could be written at all, we are out of memory and
  1596. * must return -ENOSPC.
  1597. */
  1598. return (*retlen) ? 0 : -ENOSPC;
  1599. }
  1600. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  1601. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  1602. {
  1603. struct mtd_info *master = mtd_get_master(mtd);
  1604. if (!master->_lock_user_prot_reg)
  1605. return -EOPNOTSUPP;
  1606. if (!len)
  1607. return 0;
  1608. return master->_lock_user_prot_reg(master, from, len);
  1609. }
  1610. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  1611. /* Chip-supported device locking */
  1612. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1613. {
  1614. struct mtd_info *master = mtd_get_master(mtd);
  1615. if (!master->_lock)
  1616. return -EOPNOTSUPP;
  1617. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  1618. return -EINVAL;
  1619. if (!len)
  1620. return 0;
  1621. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1622. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1623. len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
  1624. }
  1625. return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len);
  1626. }
  1627. EXPORT_SYMBOL_GPL(mtd_lock);
  1628. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1629. {
  1630. struct mtd_info *master = mtd_get_master(mtd);
  1631. if (!master->_unlock)
  1632. return -EOPNOTSUPP;
  1633. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  1634. return -EINVAL;
  1635. if (!len)
  1636. return 0;
  1637. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1638. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1639. len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
  1640. }
  1641. return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len);
  1642. }
  1643. EXPORT_SYMBOL_GPL(mtd_unlock);
  1644. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1645. {
  1646. struct mtd_info *master = mtd_get_master(mtd);
  1647. if (!master->_is_locked)
  1648. return -EOPNOTSUPP;
  1649. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  1650. return -EINVAL;
  1651. if (!len)
  1652. return 0;
  1653. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1654. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1655. len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
  1656. }
  1657. return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len);
  1658. }
  1659. EXPORT_SYMBOL_GPL(mtd_is_locked);
  1660. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1661. {
  1662. struct mtd_info *master = mtd_get_master(mtd);
  1663. if (ofs < 0 || ofs >= mtd->size)
  1664. return -EINVAL;
  1665. if (!master->_block_isreserved)
  1666. return 0;
  1667. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1668. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1669. return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs));
  1670. }
  1671. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  1672. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1673. {
  1674. struct mtd_info *master = mtd_get_master(mtd);
  1675. if (ofs < 0 || ofs >= mtd->size)
  1676. return -EINVAL;
  1677. if (!master->_block_isbad)
  1678. return 0;
  1679. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1680. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1681. return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs));
  1682. }
  1683. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  1684. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1685. {
  1686. struct mtd_info *master = mtd_get_master(mtd);
  1687. int ret;
  1688. if (!master->_block_markbad)
  1689. return -EOPNOTSUPP;
  1690. if (ofs < 0 || ofs >= mtd->size)
  1691. return -EINVAL;
  1692. if (!(mtd->flags & MTD_WRITEABLE))
  1693. return -EROFS;
  1694. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1695. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1696. ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs));
  1697. if (ret)
  1698. return ret;
  1699. while (mtd->parent) {
  1700. mtd->ecc_stats.badblocks++;
  1701. mtd = mtd->parent;
  1702. }
  1703. return 0;
  1704. }
  1705. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  1706. /*
  1707. * default_mtd_writev - the default writev method
  1708. * @mtd: mtd device description object pointer
  1709. * @vecs: the vectors to write
  1710. * @count: count of vectors in @vecs
  1711. * @to: the MTD device offset to write to
  1712. * @retlen: on exit contains the count of bytes written to the MTD device.
  1713. *
  1714. * This function returns zero in case of success and a negative error code in
  1715. * case of failure.
  1716. */
  1717. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1718. unsigned long count, loff_t to, size_t *retlen)
  1719. {
  1720. unsigned long i;
  1721. size_t totlen = 0, thislen;
  1722. int ret = 0;
  1723. for (i = 0; i < count; i++) {
  1724. if (!vecs[i].iov_len)
  1725. continue;
  1726. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  1727. vecs[i].iov_base);
  1728. totlen += thislen;
  1729. if (ret || thislen != vecs[i].iov_len)
  1730. break;
  1731. to += vecs[i].iov_len;
  1732. }
  1733. *retlen = totlen;
  1734. return ret;
  1735. }
  1736. /*
  1737. * mtd_writev - the vector-based MTD write method
  1738. * @mtd: mtd device description object pointer
  1739. * @vecs: the vectors to write
  1740. * @count: count of vectors in @vecs
  1741. * @to: the MTD device offset to write to
  1742. * @retlen: on exit contains the count of bytes written to the MTD device.
  1743. *
  1744. * This function returns zero in case of success and a negative error code in
  1745. * case of failure.
  1746. */
  1747. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1748. unsigned long count, loff_t to, size_t *retlen)
  1749. {
  1750. struct mtd_info *master = mtd_get_master(mtd);
  1751. *retlen = 0;
  1752. if (!(mtd->flags & MTD_WRITEABLE))
  1753. return -EROFS;
  1754. if (!master->_writev)
  1755. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1756. return master->_writev(master, vecs, count,
  1757. mtd_get_master_ofs(mtd, to), retlen);
  1758. }
  1759. EXPORT_SYMBOL_GPL(mtd_writev);
  1760. /**
  1761. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1762. * @mtd: mtd device description object pointer
  1763. * @size: a pointer to the ideal or maximum size of the allocation, points
  1764. * to the actual allocation size on success.
  1765. *
  1766. * This routine attempts to allocate a contiguous kernel buffer up to
  1767. * the specified size, backing off the size of the request exponentially
  1768. * until the request succeeds or until the allocation size falls below
  1769. * the system page size. This attempts to make sure it does not adversely
  1770. * impact system performance, so when allocating more than one page, we
  1771. * ask the memory allocator to avoid re-trying, swapping, writing back
  1772. * or performing I/O.
  1773. *
  1774. * Note, this function also makes sure that the allocated buffer is aligned to
  1775. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1776. *
  1777. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1778. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1779. * fragmented-memory situations where such reduced allocations, from a
  1780. * requested ideal, are allowed.
  1781. *
  1782. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1783. */
  1784. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1785. {
  1786. gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
  1787. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1788. void *kbuf;
  1789. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1790. while (*size > min_alloc) {
  1791. kbuf = kmalloc(*size, flags);
  1792. if (kbuf)
  1793. return kbuf;
  1794. *size >>= 1;
  1795. *size = ALIGN(*size, mtd->writesize);
  1796. }
  1797. /*
  1798. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1799. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1800. */
  1801. return kmalloc(*size, GFP_KERNEL);
  1802. }
  1803. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1804. #ifdef CONFIG_PROC_FS
  1805. /*====================================================================*/
  1806. /* Support for /proc/mtd */
  1807. static int mtd_proc_show(struct seq_file *m, void *v)
  1808. {
  1809. struct mtd_info *mtd;
  1810. seq_puts(m, "dev: size erasesize name\n");
  1811. mutex_lock(&mtd_table_mutex);
  1812. mtd_for_each_device(mtd) {
  1813. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1814. mtd->index, (unsigned long long)mtd->size,
  1815. mtd->erasesize, mtd->name);
  1816. }
  1817. mutex_unlock(&mtd_table_mutex);
  1818. return 0;
  1819. }
  1820. #endif /* CONFIG_PROC_FS */
  1821. /*====================================================================*/
  1822. /* Init code */
  1823. static struct backing_dev_info * __init mtd_bdi_init(char *name)
  1824. {
  1825. struct backing_dev_info *bdi;
  1826. int ret;
  1827. bdi = bdi_alloc(NUMA_NO_NODE);
  1828. if (!bdi)
  1829. return ERR_PTR(-ENOMEM);
  1830. bdi->ra_pages = 0;
  1831. bdi->io_pages = 0;
  1832. /*
  1833. * We put '-0' suffix to the name to get the same name format as we
  1834. * used to get. Since this is called only once, we get a unique name.
  1835. */
  1836. ret = bdi_register(bdi, "%.28s-0", name);
  1837. if (ret)
  1838. bdi_put(bdi);
  1839. return ret ? ERR_PTR(ret) : bdi;
  1840. }
  1841. static struct proc_dir_entry *proc_mtd;
  1842. static int __init init_mtd(void)
  1843. {
  1844. int ret;
  1845. ret = class_register(&mtd_class);
  1846. if (ret)
  1847. goto err_reg;
  1848. mtd_bdi = mtd_bdi_init("mtd");
  1849. if (IS_ERR(mtd_bdi)) {
  1850. ret = PTR_ERR(mtd_bdi);
  1851. goto err_bdi;
  1852. }
  1853. proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show);
  1854. ret = init_mtdchar();
  1855. if (ret)
  1856. goto out_procfs;
  1857. dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
  1858. return 0;
  1859. out_procfs:
  1860. if (proc_mtd)
  1861. remove_proc_entry("mtd", NULL);
  1862. bdi_put(mtd_bdi);
  1863. err_bdi:
  1864. class_unregister(&mtd_class);
  1865. err_reg:
  1866. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1867. return ret;
  1868. }
  1869. static void __exit cleanup_mtd(void)
  1870. {
  1871. debugfs_remove_recursive(dfs_dir_mtd);
  1872. cleanup_mtdchar();
  1873. if (proc_mtd)
  1874. remove_proc_entry("mtd", NULL);
  1875. class_unregister(&mtd_class);
  1876. bdi_put(mtd_bdi);
  1877. idr_destroy(&mtd_idr);
  1878. }
  1879. module_init(init_mtd);
  1880. module_exit(cleanup_mtd);
  1881. MODULE_LICENSE("GPL");
  1882. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1883. MODULE_DESCRIPTION("Core MTD registration and access routines");