mtdcore.c 66 KB

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