mtdcore.c 67 KB

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