100-06-mtd-add-core-facility-code-of-NMBM.patch 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422
  1. From 690479081fb6a0c0f77f10fb457ad69e71390f15 Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Mon, 25 Jul 2022 10:26:35 +0800
  4. Subject: [PATCH 40/71] mtd: add core facility code of NMBM
  5. This patch adds a NAND bad block management named NMBM (NAND mapping block
  6. management) which supports using a mapping table to deal with bad blocks
  7. before factory shipping and during use.
  8. Signed-off-by: Weijie Gao <[email protected]>
  9. ---
  10. drivers/mtd/Kconfig | 2 +
  11. drivers/mtd/Makefile | 1 +
  12. drivers/mtd/nmbm/Kconfig | 29 +
  13. drivers/mtd/nmbm/Makefile | 5 +
  14. drivers/mtd/nmbm/nmbm-core.c | 2936 +++++++++++++++++++++++++++++++
  15. drivers/mtd/nmbm/nmbm-debug.h | 37 +
  16. drivers/mtd/nmbm/nmbm-debug.inl | 39 +
  17. drivers/mtd/nmbm/nmbm-private.h | 137 ++
  18. include/nmbm/nmbm-os.h | 66 +
  19. include/nmbm/nmbm.h | 102 ++
  20. 10 files changed, 3354 insertions(+)
  21. create mode 100644 drivers/mtd/nmbm/Kconfig
  22. create mode 100644 drivers/mtd/nmbm/Makefile
  23. create mode 100644 drivers/mtd/nmbm/nmbm-core.c
  24. create mode 100644 drivers/mtd/nmbm/nmbm-debug.h
  25. create mode 100644 drivers/mtd/nmbm/nmbm-debug.inl
  26. create mode 100644 drivers/mtd/nmbm/nmbm-private.h
  27. create mode 100644 include/nmbm/nmbm-os.h
  28. create mode 100644 include/nmbm/nmbm.h
  29. --- a/drivers/mtd/Kconfig
  30. +++ b/drivers/mtd/Kconfig
  31. @@ -274,4 +274,6 @@ source "drivers/mtd/ubi/Kconfig"
  32. source "drivers/mtd/nvmxip/Kconfig"
  33. +source "drivers/mtd/nmbm/Kconfig"
  34. +
  35. endmenu
  36. --- a/drivers/mtd/Makefile
  37. +++ b/drivers/mtd/Makefile
  38. @@ -41,3 +41,4 @@ obj-$(CONFIG_SPL_UBI) += ubispl/
  39. endif
  40. obj-$(CONFIG_MTK_SPI_NAND) += mtk-snand/
  41. +obj-$(CONFIG_NMBM) += nmbm/
  42. --- /dev/null
  43. +++ b/drivers/mtd/nmbm/Kconfig
  44. @@ -0,0 +1,29 @@
  45. +
  46. +config NMBM
  47. + bool "Enable NAND mapping block management"
  48. + default n
  49. +
  50. +choice
  51. + prompt "Default log level"
  52. + depends on NMBM
  53. + default NMBM_LOG_LEVEL_INFO
  54. +
  55. +config NMBM_LOG_LEVEL_DEBUG
  56. + bool "0 - Debug"
  57. +
  58. +config NMBM_LOG_LEVEL_INFO
  59. + bool "1 - Info"
  60. +
  61. +config NMBM_LOG_LEVEL_WARN
  62. + bool "2 - Warn"
  63. +
  64. +config NMBM_LOG_LEVEL_ERR
  65. + bool "3 - Error"
  66. +
  67. +config NMBM_LOG_LEVEL_EMERG
  68. + bool "4 - Emergency"
  69. +
  70. +config NMBM_LOG_LEVEL_NONE
  71. + bool "5 - None"
  72. +
  73. +endchoice
  74. --- /dev/null
  75. +++ b/drivers/mtd/nmbm/Makefile
  76. @@ -0,0 +1,5 @@
  77. +# SPDX-License-Identifier: GPL-2.0
  78. +#
  79. +# (C) Copyright 2020 MediaTek Inc. All rights reserved.
  80. +
  81. +obj-$(CONFIG_NMBM) += nmbm-core.o
  82. --- /dev/null
  83. +++ b/drivers/mtd/nmbm/nmbm-core.c
  84. @@ -0,0 +1,2936 @@
  85. +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  86. +/*
  87. + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
  88. + *
  89. + * Author: Weijie Gao <[email protected]>
  90. + */
  91. +
  92. +#include "nmbm-private.h"
  93. +
  94. +#include "nmbm-debug.h"
  95. +
  96. +#define NMBM_VER_MAJOR 1
  97. +#define NMBM_VER_MINOR 0
  98. +#define NMBM_VER NMBM_VERSION_MAKE(NMBM_VER_MAJOR, \
  99. + NMBM_VER_MINOR)
  100. +
  101. +#define NMBM_ALIGN(v, a) (((v) + (a) - 1) & ~((a) - 1))
  102. +
  103. +/*****************************************************************************/
  104. +/* Logging related functions */
  105. +/*****************************************************************************/
  106. +
  107. +/*
  108. + * nmbm_log_lower - Print log using OS specific routine
  109. + * @nld: NMBM lower device structure
  110. + * @level: log level
  111. + * @fmt: format string
  112. + */
  113. +static void nmbm_log_lower(struct nmbm_lower_device *nld,
  114. + enum nmbm_log_category level, const char *fmt, ...)
  115. +{
  116. + va_list ap;
  117. +
  118. + if (!nld->logprint)
  119. + return;
  120. +
  121. + va_start(ap, fmt);
  122. + nld->logprint(nld->arg, level, fmt, ap);
  123. + va_end(ap);
  124. +}
  125. +
  126. +/*
  127. + * nmbm_log - Print log using OS specific routine
  128. + * @ni: NMBM instance structure
  129. + * @level: log level
  130. + * @fmt: format string
  131. + */
  132. +static void nmbm_log(struct nmbm_instance *ni, enum nmbm_log_category level,
  133. + const char *fmt, ...)
  134. +{
  135. + va_list ap;
  136. +
  137. + if (!ni)
  138. + return;
  139. +
  140. + if (!ni->lower.logprint || level < ni->log_display_level)
  141. + return;
  142. +
  143. + va_start(ap, fmt);
  144. + ni->lower.logprint(ni->lower.arg, level, fmt, ap);
  145. + va_end(ap);
  146. +}
  147. +
  148. +/*
  149. + * nmbm_set_log_level - Set log display level
  150. + * @ni: NMBM instance structure
  151. + * @level: log display level
  152. + */
  153. +enum nmbm_log_category nmbm_set_log_level(struct nmbm_instance *ni,
  154. + enum nmbm_log_category level)
  155. +{
  156. + enum nmbm_log_category old;
  157. +
  158. + if (!ni)
  159. + return __NMBM_LOG_MAX;
  160. +
  161. + old = ni->log_display_level;
  162. + ni->log_display_level = level;
  163. + return old;
  164. +}
  165. +
  166. +/*
  167. + * nlog_table_creation - Print log of table creation event
  168. + * @ni: NMBM instance structure
  169. + * @main_table: whether the table is main info table
  170. + * @start_ba: start block address of the table
  171. + * @end_ba: block address after the end of the table
  172. + */
  173. +static void nlog_table_creation(struct nmbm_instance *ni, bool main_table,
  174. + uint32_t start_ba, uint32_t end_ba)
  175. +{
  176. + if (start_ba == end_ba - 1)
  177. + nlog_info(ni, "%s info table has been written to block %u\n",
  178. + main_table ? "Main" : "Backup", start_ba);
  179. + else
  180. + nlog_info(ni, "%s info table has been written to block %u-%u\n",
  181. + main_table ? "Main" : "Backup", start_ba, end_ba - 1);
  182. +
  183. + nmbm_mark_block_color_info_table(ni, start_ba, end_ba - 1);
  184. +}
  185. +
  186. +/*
  187. + * nlog_table_update - Print log of table update event
  188. + * @ni: NMBM instance structure
  189. + * @main_table: whether the table is main info table
  190. + * @start_ba: start block address of the table
  191. + * @end_ba: block address after the end of the table
  192. + */
  193. +static void nlog_table_update(struct nmbm_instance *ni, bool main_table,
  194. + uint32_t start_ba, uint32_t end_ba)
  195. +{
  196. + if (start_ba == end_ba - 1)
  197. + nlog_debug(ni, "%s info table has been updated in block %u\n",
  198. + main_table ? "Main" : "Backup", start_ba);
  199. + else
  200. + nlog_debug(ni, "%s info table has been updated in block %u-%u\n",
  201. + main_table ? "Main" : "Backup", start_ba, end_ba - 1);
  202. +
  203. + nmbm_mark_block_color_info_table(ni, start_ba, end_ba - 1);
  204. +}
  205. +
  206. +/*
  207. + * nlog_table_found - Print log of table found event
  208. + * @ni: NMBM instance structure
  209. + * @first_table: whether the table is first found info table
  210. + * @write_count: write count of the info table
  211. + * @start_ba: start block address of the table
  212. + * @end_ba: block address after the end of the table
  213. + */
  214. +static void nlog_table_found(struct nmbm_instance *ni, bool first_table,
  215. + uint32_t write_count, uint32_t start_ba,
  216. + uint32_t end_ba)
  217. +{
  218. + if (start_ba == end_ba - 1)
  219. + nlog_info(ni, "%s info table with writecount %u found in block %u\n",
  220. + first_table ? "First" : "Second", write_count,
  221. + start_ba);
  222. + else
  223. + nlog_info(ni, "%s info table with writecount %u found in block %u-%u\n",
  224. + first_table ? "First" : "Second", write_count,
  225. + start_ba, end_ba - 1);
  226. +
  227. + nmbm_mark_block_color_info_table(ni, start_ba, end_ba - 1);
  228. +}
  229. +
  230. +/*****************************************************************************/
  231. +/* Address conversion functions */
  232. +/*****************************************************************************/
  233. +
  234. +/*
  235. + * addr2ba - Convert a linear address to block address
  236. + * @ni: NMBM instance structure
  237. + * @addr: Linear address
  238. + */
  239. +static uint32_t addr2ba(struct nmbm_instance *ni, uint64_t addr)
  240. +{
  241. + return addr >> ni->erasesize_shift;
  242. +}
  243. +
  244. +/*
  245. + * ba2addr - Convert a block address to linear address
  246. + * @ni: NMBM instance structure
  247. + * @ba: Block address
  248. + */
  249. +static uint64_t ba2addr(struct nmbm_instance *ni, uint32_t ba)
  250. +{
  251. + return (uint64_t)ba << ni->erasesize_shift;
  252. +}
  253. +/*
  254. + * size2blk - Get minimum required blocks for storing specific size of data
  255. + * @ni: NMBM instance structure
  256. + * @size: size for storing
  257. + */
  258. +static uint32_t size2blk(struct nmbm_instance *ni, uint64_t size)
  259. +{
  260. + return (size + ni->lower.erasesize - 1) >> ni->erasesize_shift;
  261. +}
  262. +
  263. +/*****************************************************************************/
  264. +/* High level NAND chip APIs */
  265. +/*****************************************************************************/
  266. +
  267. +/*
  268. + * nmbm_reset_chip - Reset NAND device
  269. + * @nld: Lower NAND chip structure
  270. + */
  271. +static void nmbm_reset_chip(struct nmbm_instance *ni)
  272. +{
  273. + if (ni->lower.reset_chip)
  274. + ni->lower.reset_chip(ni->lower.arg);
  275. +}
  276. +
  277. +/*
  278. + * nmbm_read_phys_page - Read page with retry
  279. + * @ni: NMBM instance structure
  280. + * @addr: linear address where the data will be read from
  281. + * @data: the main data to be read
  282. + * @oob: the oob data to be read
  283. + * @mode: mode for processing oob data
  284. + *
  285. + * Read a page for at most NMBM_TRY_COUNT times.
  286. + *
  287. + * Return 0 for success, positive value for corrected bitflip count,
  288. + * -EBADMSG for ecc error, other negative values for other errors
  289. + */
  290. +static int nmbm_read_phys_page(struct nmbm_instance *ni, uint64_t addr,
  291. + void *data, void *oob, enum nmbm_oob_mode mode)
  292. +{
  293. + int tries, ret;
  294. +
  295. + for (tries = 0; tries < NMBM_TRY_COUNT; tries++) {
  296. + ret = ni->lower.read_page(ni->lower.arg, addr, data, oob, mode);
  297. + if (ret >= 0)
  298. + return ret;
  299. +
  300. + nmbm_reset_chip(ni);
  301. + }
  302. +
  303. + if (ret != -EBADMSG)
  304. + nlog_err(ni, "Page read failed at address 0x%08llx\n", addr);
  305. +
  306. + return ret;
  307. +}
  308. +
  309. +/*
  310. + * nmbm_write_phys_page - Write page with retry
  311. + * @ni: NMBM instance structure
  312. + * @addr: linear address where the data will be written to
  313. + * @data: the main data to be written
  314. + * @oob: the oob data to be written
  315. + * @mode: mode for processing oob data
  316. + *
  317. + * Write a page for at most NMBM_TRY_COUNT times.
  318. + */
  319. +static bool nmbm_write_phys_page(struct nmbm_instance *ni, uint64_t addr,
  320. + const void *data, const void *oob,
  321. + enum nmbm_oob_mode mode)
  322. +{
  323. + int tries, ret;
  324. +
  325. + if (ni->lower.flags & NMBM_F_READ_ONLY) {
  326. + nlog_err(ni, "%s called with NMBM_F_READ_ONLY set\n", addr);
  327. + return false;
  328. + }
  329. +
  330. + for (tries = 0; tries < NMBM_TRY_COUNT; tries++) {
  331. + ret = ni->lower.write_page(ni->lower.arg, addr, data, oob, mode);
  332. + if (!ret)
  333. + return true;
  334. +
  335. + nmbm_reset_chip(ni);
  336. + }
  337. +
  338. + nlog_err(ni, "Page write failed at address 0x%08llx\n", addr);
  339. +
  340. + return false;
  341. +}
  342. +
  343. +/*
  344. + * nmbm_erase_phys_block - Erase a block with retry
  345. + * @ni: NMBM instance structure
  346. + * @addr: Linear address
  347. + *
  348. + * Erase a block for at most NMBM_TRY_COUNT times.
  349. + */
  350. +static bool nmbm_erase_phys_block(struct nmbm_instance *ni, uint64_t addr)
  351. +{
  352. + int tries, ret;
  353. +
  354. + if (ni->lower.flags & NMBM_F_READ_ONLY) {
  355. + nlog_err(ni, "%s called with NMBM_F_READ_ONLY set\n", addr);
  356. + return false;
  357. + }
  358. +
  359. + for (tries = 0; tries < NMBM_TRY_COUNT; tries++) {
  360. + ret = ni->lower.erase_block(ni->lower.arg, addr);
  361. + if (!ret)
  362. + return true;
  363. +
  364. + nmbm_reset_chip(ni);
  365. + }
  366. +
  367. + nlog_err(ni, "Block erasure failed at address 0x%08llx\n", addr);
  368. +
  369. + return false;
  370. +}
  371. +
  372. +/*
  373. + * nmbm_check_bad_phys_block - Check whether a block is marked bad in OOB
  374. + * @ni: NMBM instance structure
  375. + * @ba: block address
  376. + */
  377. +static bool nmbm_check_bad_phys_block(struct nmbm_instance *ni, uint32_t ba)
  378. +{
  379. + uint64_t addr = ba2addr(ni, ba);
  380. + int ret;
  381. +
  382. + if (ni->lower.is_bad_block)
  383. + return ni->lower.is_bad_block(ni->lower.arg, addr);
  384. +
  385. + /* Treat ECC error as read success */
  386. + ret = nmbm_read_phys_page(ni, addr, NULL,
  387. + ni->page_cache + ni->lower.writesize,
  388. + NMBM_MODE_RAW);
  389. + if (ret < 0 && ret != -EBADMSG)
  390. + return true;
  391. +
  392. + return ni->page_cache[ni->lower.writesize] != 0xff;
  393. +}
  394. +
  395. +/*
  396. + * nmbm_mark_phys_bad_block - Mark a block bad
  397. + * @ni: NMBM instance structure
  398. + * @addr: Linear address
  399. + */
  400. +static int nmbm_mark_phys_bad_block(struct nmbm_instance *ni, uint32_t ba)
  401. +{
  402. + uint64_t addr = ba2addr(ni, ba);
  403. + enum nmbm_log_category level;
  404. + uint32_t off;
  405. +
  406. + if (ni->lower.flags & NMBM_F_READ_ONLY) {
  407. + nlog_err(ni, "%s called with NMBM_F_READ_ONLY set\n", addr);
  408. + return false;
  409. + }
  410. +
  411. + nlog_info(ni, "Block %u [0x%08llx] will be marked bad\n", ba, addr);
  412. +
  413. + if (ni->lower.mark_bad_block)
  414. + return ni->lower.mark_bad_block(ni->lower.arg, addr);
  415. +
  416. + /* Whole page set to 0x00 */
  417. + memset(ni->page_cache, 0, ni->rawpage_size);
  418. +
  419. + /* Write to all pages within this block, disable all errors */
  420. + level = nmbm_set_log_level(ni, __NMBM_LOG_MAX);
  421. +
  422. + for (off = 0; off < ni->lower.erasesize; off += ni->lower.writesize) {
  423. + nmbm_write_phys_page(ni, addr + off, ni->page_cache,
  424. + ni->page_cache + ni->lower.writesize,
  425. + NMBM_MODE_RAW);
  426. + }
  427. +
  428. + nmbm_set_log_level(ni, level);
  429. +
  430. + return 0;
  431. +}
  432. +
  433. +/*****************************************************************************/
  434. +/* NMBM related functions */
  435. +/*****************************************************************************/
  436. +
  437. +/*
  438. + * nmbm_check_header - Check whether a NMBM structure is valid
  439. + * @data: pointer to a NMBM structure with a NMBM header at beginning
  440. + * @size: Size of the buffer pointed by @header
  441. + *
  442. + * The size of the NMBM structure may be larger than NMBM header,
  443. + * e.g. block mapping table and block state table.
  444. + */
  445. +static bool nmbm_check_header(const void *data, uint32_t size)
  446. +{
  447. + const struct nmbm_header *header = data;
  448. + struct nmbm_header nhdr;
  449. + uint32_t new_checksum;
  450. +
  451. + /*
  452. + * Make sure expected structure size is equal or smaller than
  453. + * buffer size.
  454. + */
  455. + if (header->size > size)
  456. + return false;
  457. +
  458. + memcpy(&nhdr, data, sizeof(nhdr));
  459. +
  460. + nhdr.checksum = 0;
  461. + new_checksum = nmbm_crc32(0, &nhdr, sizeof(nhdr));
  462. + if (header->size > sizeof(nhdr))
  463. + new_checksum = nmbm_crc32(new_checksum,
  464. + (const uint8_t *)data + sizeof(nhdr),
  465. + header->size - sizeof(nhdr));
  466. +
  467. + if (header->checksum != new_checksum)
  468. + return false;
  469. +
  470. + return true;
  471. +}
  472. +
  473. +/*
  474. + * nmbm_update_checksum - Update checksum of a NMBM structure
  475. + * @header: pointer to a NMBM structure with a NMBM header at beginning
  476. + *
  477. + * The size of the NMBM structure must be specified by @header->size
  478. + */
  479. +static void nmbm_update_checksum(struct nmbm_header *header)
  480. +{
  481. + header->checksum = 0;
  482. + header->checksum = nmbm_crc32(0, header, header->size);
  483. +}
  484. +
  485. +/*
  486. + * nmbm_get_spare_block_count - Calculate number of blocks should be reserved
  487. + * @block_count: number of blocks of data
  488. + *
  489. + * Calculate number of blocks should be reserved for data
  490. + */
  491. +static uint32_t nmbm_get_spare_block_count(uint32_t block_count)
  492. +{
  493. + uint32_t val;
  494. +
  495. + val = (block_count + NMBM_SPARE_BLOCK_DIV / 2) / NMBM_SPARE_BLOCK_DIV;
  496. + val *= NMBM_SPARE_BLOCK_MULTI;
  497. +
  498. + if (val < NMBM_SPARE_BLOCK_MIN)
  499. + val = NMBM_SPARE_BLOCK_MIN;
  500. +
  501. + return val;
  502. +}
  503. +
  504. +/*
  505. + * nmbm_get_block_state_raw - Get state of a block from raw block state table
  506. + * @block_state: pointer to raw block state table (bitmap)
  507. + * @ba: block address
  508. + */
  509. +static uint32_t nmbm_get_block_state_raw(nmbm_bitmap_t *block_state,
  510. + uint32_t ba)
  511. +{
  512. + uint32_t unit, shift;
  513. +
  514. + unit = ba / NMBM_BITMAP_BLOCKS_PER_UNIT;
  515. + shift = (ba % NMBM_BITMAP_BLOCKS_PER_UNIT) * NMBM_BITMAP_BITS_PER_BLOCK;
  516. +
  517. + return (block_state[unit] >> shift) & BLOCK_ST_MASK;
  518. +}
  519. +
  520. +/*
  521. + * nmbm_get_block_state - Get state of a block from block state table
  522. + * @ni: NMBM instance structure
  523. + * @ba: block address
  524. + */
  525. +static uint32_t nmbm_get_block_state(struct nmbm_instance *ni, uint32_t ba)
  526. +{
  527. + return nmbm_get_block_state_raw(ni->block_state, ba);
  528. +}
  529. +
  530. +/*
  531. + * nmbm_set_block_state - Set state of a block to block state table
  532. + * @ni: NMBM instance structure
  533. + * @ba: block address
  534. + * @state: block state
  535. + *
  536. + * Set state of a block. If the block state changed, ni->block_state_changed
  537. + * will be increased.
  538. + */
  539. +static bool nmbm_set_block_state(struct nmbm_instance *ni, uint32_t ba,
  540. + uint32_t state)
  541. +{
  542. + uint32_t unit, shift, orig;
  543. + nmbm_bitmap_t uv;
  544. +
  545. + unit = ba / NMBM_BITMAP_BLOCKS_PER_UNIT;
  546. + shift = (ba % NMBM_BITMAP_BLOCKS_PER_UNIT) * NMBM_BITMAP_BITS_PER_BLOCK;
  547. +
  548. + orig = (ni->block_state[unit] >> shift) & BLOCK_ST_MASK;
  549. + state &= BLOCK_ST_MASK;
  550. +
  551. + uv = ni->block_state[unit] & (~(BLOCK_ST_MASK << shift));
  552. + uv |= state << shift;
  553. + ni->block_state[unit] = uv;
  554. +
  555. + if (state == BLOCK_ST_BAD)
  556. + nmbm_mark_block_color_bad(ni, ba);
  557. +
  558. + if (orig != state) {
  559. + ni->block_state_changed++;
  560. + return true;
  561. + }
  562. +
  563. + return false;
  564. +}
  565. +
  566. +/*
  567. + * nmbm_block_walk_asc - Skip specified number of good blocks, ascending addr.
  568. + * @ni: NMBM instance structure
  569. + * @ba: start physical block address
  570. + * @nba: return physical block address after walk
  571. + * @count: number of good blocks to be skipped
  572. + * @limit: highest block address allowed for walking
  573. + *
  574. + * Start from @ba, skipping any bad blocks, counting @count good blocks, and
  575. + * return the next good block address.
  576. + *
  577. + * If no enough good blocks counted while @limit reached, false will be returned.
  578. + *
  579. + * If @count == 0, nearest good block address will be returned.
  580. + * @limit is not counted in walking.
  581. + */
  582. +static bool nmbm_block_walk_asc(struct nmbm_instance *ni, uint32_t ba,
  583. + uint32_t *nba, uint32_t count,
  584. + uint32_t limit)
  585. +{
  586. + int32_t nblock = count;
  587. +
  588. + if (limit >= ni->block_count)
  589. + limit = ni->block_count - 1;
  590. +
  591. + while (ba < limit) {
  592. + if (nmbm_get_block_state(ni, ba) == BLOCK_ST_GOOD)
  593. + nblock--;
  594. +
  595. + if (nblock < 0) {
  596. + *nba = ba;
  597. + return true;
  598. + }
  599. +
  600. + ba++;
  601. + }
  602. +
  603. + return false;
  604. +}
  605. +
  606. +/*
  607. + * nmbm_block_walk_desc - Skip specified number of good blocks, descending addr
  608. + * @ni: NMBM instance structure
  609. + * @ba: start physical block address
  610. + * @nba: return physical block address after walk
  611. + * @count: number of good blocks to be skipped
  612. + * @limit: lowest block address allowed for walking
  613. + *
  614. + * Start from @ba, skipping any bad blocks, counting @count good blocks, and
  615. + * return the next good block address.
  616. + *
  617. + * If no enough good blocks counted while @limit reached, false will be returned.
  618. + *
  619. + * If @count == 0, nearest good block address will be returned.
  620. + * @limit is not counted in walking.
  621. + */
  622. +static bool nmbm_block_walk_desc(struct nmbm_instance *ni, uint32_t ba,
  623. + uint32_t *nba, uint32_t count, uint32_t limit)
  624. +{
  625. + int32_t nblock = count;
  626. +
  627. + if (limit >= ni->block_count)
  628. + limit = ni->block_count - 1;
  629. +
  630. + while (ba > limit) {
  631. + if (nmbm_get_block_state(ni, ba) == BLOCK_ST_GOOD)
  632. + nblock--;
  633. +
  634. + if (nblock < 0) {
  635. + *nba = ba;
  636. + return true;
  637. + }
  638. +
  639. + ba--;
  640. + }
  641. +
  642. + return false;
  643. +}
  644. +
  645. +/*
  646. + * nmbm_block_walk - Skip specified number of good blocks from curr. block addr
  647. + * @ni: NMBM instance structure
  648. + * @ascending: whether to walk ascending
  649. + * @ba: start physical block address
  650. + * @nba: return physical block address after walk
  651. + * @count: number of good blocks to be skipped
  652. + * @limit: highest/lowest block address allowed for walking
  653. + *
  654. + * Start from @ba, skipping any bad blocks, counting @count good blocks, and
  655. + * return the next good block address.
  656. + *
  657. + * If no enough good blocks counted while @limit reached, false will be returned.
  658. + *
  659. + * If @count == 0, nearest good block address will be returned.
  660. + * @limit can be set to negative if no limit required.
  661. + * @limit is not counted in walking.
  662. + */
  663. +static bool nmbm_block_walk(struct nmbm_instance *ni, bool ascending,
  664. + uint32_t ba, uint32_t *nba, int32_t count,
  665. + int32_t limit)
  666. +{
  667. + if (ascending)
  668. + return nmbm_block_walk_asc(ni, ba, nba, count, limit);
  669. +
  670. + return nmbm_block_walk_desc(ni, ba, nba, count, limit);
  671. +}
  672. +
  673. +/*
  674. + * nmbm_scan_badblocks - Scan and record all bad blocks
  675. + * @ni: NMBM instance structure
  676. + *
  677. + * Scan the entire lower NAND chip and record all bad blocks in to block state
  678. + * table.
  679. + */
  680. +static void nmbm_scan_badblocks(struct nmbm_instance *ni)
  681. +{
  682. + uint32_t ba;
  683. +
  684. + for (ba = 0; ba < ni->block_count; ba++) {
  685. + if (nmbm_check_bad_phys_block(ni, ba)) {
  686. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  687. + nlog_info(ni, "Bad block %u [0x%08llx]\n", ba,
  688. + ba2addr(ni, ba));
  689. + }
  690. + }
  691. +}
  692. +
  693. +/*
  694. + * nmbm_build_mapping_table - Build initial block mapping table
  695. + * @ni: NMBM instance structure
  696. + *
  697. + * The initial mapping table will be compatible with the stratage of
  698. + * factory production.
  699. + */
  700. +static void nmbm_build_mapping_table(struct nmbm_instance *ni)
  701. +{
  702. + uint32_t pb, lb;
  703. +
  704. + for (pb = 0, lb = 0; pb < ni->mgmt_start_ba; pb++) {
  705. + if (nmbm_get_block_state(ni, pb) == BLOCK_ST_BAD)
  706. + continue;
  707. +
  708. + /* Always map to the next good block */
  709. + ni->block_mapping[lb++] = pb;
  710. + }
  711. +
  712. + ni->data_block_count = lb;
  713. +
  714. + /* Unusable/Management blocks */
  715. + for (pb = lb; pb < ni->block_count; pb++)
  716. + ni->block_mapping[pb] = -1;
  717. +}
  718. +
  719. +/*
  720. + * nmbm_erase_block_and_check - Erase a block and check its usability
  721. + * @ni: NMBM instance structure
  722. + * @ba: block address to be erased
  723. + *
  724. + * Erase a block anc check its usability
  725. + *
  726. + * Return true if the block is usable, false if erasure failure or the block
  727. + * has too many bitflips.
  728. + */
  729. +static bool nmbm_erase_block_and_check(struct nmbm_instance *ni, uint32_t ba)
  730. +{
  731. + uint64_t addr, off;
  732. + bool success;
  733. + int ret;
  734. +
  735. + success = nmbm_erase_phys_block(ni, ba2addr(ni, ba));
  736. + if (!success)
  737. + return false;
  738. +
  739. + if (!(ni->lower.flags & NMBM_F_EMPTY_PAGE_ECC_OK))
  740. + return true;
  741. +
  742. + /* Check every page to make sure there aren't too many bitflips */
  743. +
  744. + addr = ba2addr(ni, ba);
  745. +
  746. + for (off = 0; off < ni->lower.erasesize; off += ni->lower.writesize) {
  747. + schedule();
  748. +
  749. + ret = nmbm_read_phys_page(ni, addr + off, ni->page_cache, NULL,
  750. + NMBM_MODE_PLACE_OOB);
  751. + if (ret == -EBADMSG) {
  752. + /*
  753. + * NMBM_F_EMPTY_PAGE_ECC_OK means the empty page is
  754. + * still protected by ECC. So reading pages with ECC
  755. + * enabled and -EBADMSG means there are too many
  756. + * bitflips that can't be recovered, and the block
  757. + * containing the page should be marked bad.
  758. + */
  759. + nlog_err(ni,
  760. + "Too many bitflips in empty page at 0x%llx\n",
  761. + addr + off);
  762. + return false;
  763. + }
  764. + }
  765. +
  766. + return true;
  767. +}
  768. +
  769. +/*
  770. + * nmbm_erase_range - Erase a range of blocks
  771. + * @ni: NMBM instance structure
  772. + * @ba: block address where the erasure will start
  773. + * @limit: top block address allowed for erasure
  774. + *
  775. + * Erase blocks within the specific range. Newly-found bad blocks will be
  776. + * marked.
  777. + *
  778. + * @limit is not counted into the allowed erasure address.
  779. + */
  780. +static void nmbm_erase_range(struct nmbm_instance *ni, uint32_t ba,
  781. + uint32_t limit)
  782. +{
  783. + bool success;
  784. +
  785. + while (ba < limit) {
  786. + schedule();
  787. +
  788. + if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
  789. + goto next_block;
  790. +
  791. + /* Insurance to detect unexpected bad block marked by user */
  792. + if (nmbm_check_bad_phys_block(ni, ba)) {
  793. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  794. + goto next_block;
  795. + }
  796. +
  797. + success = nmbm_erase_block_and_check(ni, ba);
  798. + if (success)
  799. + goto next_block;
  800. +
  801. + nmbm_mark_phys_bad_block(ni, ba);
  802. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  803. +
  804. + next_block:
  805. + ba++;
  806. + }
  807. +}
  808. +
  809. +/*
  810. + * nmbm_write_repeated_data - Write critical data to a block with retry
  811. + * @ni: NMBM instance structure
  812. + * @ba: block address where the data will be written to
  813. + * @data: the data to be written
  814. + * @size: size of the data
  815. + *
  816. + * Write data to every page of the block. Success only if all pages within
  817. + * this block have been successfully written.
  818. + *
  819. + * Make sure data size is not bigger than one page.
  820. + *
  821. + * This function will write and verify every page for at most
  822. + * NMBM_TRY_COUNT times.
  823. + */
  824. +static bool nmbm_write_repeated_data(struct nmbm_instance *ni, uint32_t ba,
  825. + const void *data, uint32_t size)
  826. +{
  827. + uint64_t addr, off;
  828. + bool success;
  829. + int ret;
  830. +
  831. + if (size > ni->lower.writesize)
  832. + return false;
  833. +
  834. + addr = ba2addr(ni, ba);
  835. +
  836. + for (off = 0; off < ni->lower.erasesize; off += ni->lower.writesize) {
  837. + schedule();
  838. +
  839. + /* Prepare page data. fill 0xff to unused region */
  840. + memcpy(ni->page_cache, data, size);
  841. + memset(ni->page_cache + size, 0xff, ni->rawpage_size - size);
  842. +
  843. + success = nmbm_write_phys_page(ni, addr + off, ni->page_cache,
  844. + NULL, NMBM_MODE_PLACE_OOB);
  845. + if (!success)
  846. + return false;
  847. +
  848. + /* Verify the data just written. ECC error indicates failure */
  849. + ret = nmbm_read_phys_page(ni, addr + off, ni->page_cache, NULL,
  850. + NMBM_MODE_PLACE_OOB);
  851. + if (ret < 0)
  852. + return false;
  853. +
  854. + if (memcmp(ni->page_cache, data, size))
  855. + return false;
  856. + }
  857. +
  858. + return true;
  859. +}
  860. +
  861. +/*
  862. + * nmbm_write_signature - Write signature to NAND chip
  863. + * @ni: NMBM instance structure
  864. + * @limit: top block address allowed for writing
  865. + * @signature: the signature to be written
  866. + * @signature_ba: the actual block address where signature is written to
  867. + *
  868. + * Write signature within a specific range, from chip bottom to limit.
  869. + * At most one block will be written.
  870. + *
  871. + * @limit is not counted into the allowed write address.
  872. + */
  873. +static bool nmbm_write_signature(struct nmbm_instance *ni, uint32_t limit,
  874. + const struct nmbm_signature *signature,
  875. + uint32_t *signature_ba)
  876. +{
  877. + uint32_t ba = ni->block_count - 1;
  878. + bool success;
  879. +
  880. + while (ba > limit) {
  881. + schedule();
  882. +
  883. + if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
  884. + goto next_block;
  885. +
  886. + /* Insurance to detect unexpected bad block marked by user */
  887. + if (nmbm_check_bad_phys_block(ni, ba)) {
  888. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  889. + goto next_block;
  890. + }
  891. +
  892. + success = nmbm_erase_block_and_check(ni, ba);
  893. + if (!success)
  894. + goto skip_bad_block;
  895. +
  896. + success = nmbm_write_repeated_data(ni, ba, signature,
  897. + sizeof(*signature));
  898. + if (success) {
  899. + *signature_ba = ba;
  900. + return true;
  901. + }
  902. +
  903. + skip_bad_block:
  904. + nmbm_mark_phys_bad_block(ni, ba);
  905. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  906. +
  907. + next_block:
  908. + ba--;
  909. + };
  910. +
  911. + return false;
  912. +}
  913. +
  914. +/*
  915. + * nmbn_read_data - Read data
  916. + * @ni: NMBM instance structure
  917. + * @addr: linear address where the data will be read from
  918. + * @data: the data to be read
  919. + * @size: the size of data
  920. + *
  921. + * Read data range.
  922. + * Every page will be tried for at most NMBM_TRY_COUNT times.
  923. + *
  924. + * Return 0 for success, positive value for corrected bitflip count,
  925. + * -EBADMSG for ecc error, other negative values for other errors
  926. + */
  927. +static int nmbn_read_data(struct nmbm_instance *ni, uint64_t addr, void *data,
  928. + uint32_t size)
  929. +{
  930. + uint64_t off = addr;
  931. + uint8_t *ptr = data;
  932. + uint32_t sizeremain = size, chunksize, leading;
  933. + int ret;
  934. +
  935. + while (sizeremain) {
  936. + schedule();
  937. +
  938. + leading = off & ni->writesize_mask;
  939. + chunksize = ni->lower.writesize - leading;
  940. + if (chunksize > sizeremain)
  941. + chunksize = sizeremain;
  942. +
  943. + if (chunksize == ni->lower.writesize) {
  944. + ret = nmbm_read_phys_page(ni, off - leading, ptr, NULL,
  945. + NMBM_MODE_PLACE_OOB);
  946. + if (ret < 0)
  947. + return ret;
  948. + } else {
  949. + ret = nmbm_read_phys_page(ni, off - leading,
  950. + ni->page_cache, NULL,
  951. + NMBM_MODE_PLACE_OOB);
  952. + if (ret < 0)
  953. + return ret;
  954. +
  955. + memcpy(ptr, ni->page_cache + leading, chunksize);
  956. + }
  957. +
  958. + off += chunksize;
  959. + ptr += chunksize;
  960. + sizeremain -= chunksize;
  961. + }
  962. +
  963. + return 0;
  964. +}
  965. +
  966. +/*
  967. + * nmbn_write_verify_data - Write data with validation
  968. + * @ni: NMBM instance structure
  969. + * @addr: linear address where the data will be written to
  970. + * @data: the data to be written
  971. + * @size: the size of data
  972. + *
  973. + * Write data and verify.
  974. + * Every page will be tried for at most NMBM_TRY_COUNT times.
  975. + */
  976. +static bool nmbn_write_verify_data(struct nmbm_instance *ni, uint64_t addr,
  977. + const void *data, uint32_t size)
  978. +{
  979. + uint64_t off = addr;
  980. + const uint8_t *ptr = data;
  981. + uint32_t sizeremain = size, chunksize, leading;
  982. + bool success;
  983. + int ret;
  984. +
  985. + while (sizeremain) {
  986. + schedule();
  987. +
  988. + leading = off & ni->writesize_mask;
  989. + chunksize = ni->lower.writesize - leading;
  990. + if (chunksize > sizeremain)
  991. + chunksize = sizeremain;
  992. +
  993. + /* Prepare page data. fill 0xff to unused region */
  994. + memset(ni->page_cache, 0xff, ni->rawpage_size);
  995. + memcpy(ni->page_cache + leading, ptr, chunksize);
  996. +
  997. + success = nmbm_write_phys_page(ni, off - leading,
  998. + ni->page_cache, NULL,
  999. + NMBM_MODE_PLACE_OOB);
  1000. + if (!success)
  1001. + return false;
  1002. +
  1003. + /* Verify the data just written. ECC error indicates failure */
  1004. + ret = nmbm_read_phys_page(ni, off - leading, ni->page_cache,
  1005. + NULL, NMBM_MODE_PLACE_OOB);
  1006. + if (ret < 0)
  1007. + return false;
  1008. +
  1009. + if (memcmp(ni->page_cache + leading, ptr, chunksize))
  1010. + return false;
  1011. +
  1012. + off += chunksize;
  1013. + ptr += chunksize;
  1014. + sizeremain -= chunksize;
  1015. + }
  1016. +
  1017. + return true;
  1018. +}
  1019. +
  1020. +/*
  1021. + * nmbm_write_mgmt_range - Write management data into NAND within a range
  1022. + * @ni: NMBM instance structure
  1023. + * @addr: preferred start block address for writing
  1024. + * @limit: highest block address allowed for writing
  1025. + * @data: the data to be written
  1026. + * @size: the size of data
  1027. + * @actual_start_ba: actual start block address of data
  1028. + * @actual_end_ba: block address after the end of data
  1029. + *
  1030. + * @limit is not counted into the allowed write address.
  1031. + */
  1032. +static bool nmbm_write_mgmt_range(struct nmbm_instance *ni, uint32_t ba,
  1033. + uint32_t limit, const void *data,
  1034. + uint32_t size, uint32_t *actual_start_ba,
  1035. + uint32_t *actual_end_ba)
  1036. +{
  1037. + const uint8_t *ptr = data;
  1038. + uint32_t sizeremain = size, chunksize;
  1039. + bool success;
  1040. +
  1041. + while (sizeremain && ba < limit) {
  1042. + schedule();
  1043. +
  1044. + chunksize = sizeremain;
  1045. + if (chunksize > ni->lower.erasesize)
  1046. + chunksize = ni->lower.erasesize;
  1047. +
  1048. + if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
  1049. + goto next_block;
  1050. +
  1051. + /* Insurance to detect unexpected bad block marked by user */
  1052. + if (nmbm_check_bad_phys_block(ni, ba)) {
  1053. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  1054. + goto next_block;
  1055. + }
  1056. +
  1057. + success = nmbm_erase_block_and_check(ni, ba);
  1058. + if (!success)
  1059. + goto skip_bad_block;
  1060. +
  1061. + success = nmbn_write_verify_data(ni, ba2addr(ni, ba), ptr,
  1062. + chunksize);
  1063. + if (!success)
  1064. + goto skip_bad_block;
  1065. +
  1066. + if (sizeremain == size)
  1067. + *actual_start_ba = ba;
  1068. +
  1069. + ptr += chunksize;
  1070. + sizeremain -= chunksize;
  1071. +
  1072. + goto next_block;
  1073. +
  1074. + skip_bad_block:
  1075. + nmbm_mark_phys_bad_block(ni, ba);
  1076. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  1077. +
  1078. + next_block:
  1079. + ba++;
  1080. + }
  1081. +
  1082. + if (sizeremain)
  1083. + return false;
  1084. +
  1085. + *actual_end_ba = ba;
  1086. +
  1087. + return true;
  1088. +}
  1089. +
  1090. +/*
  1091. + * nmbm_generate_info_table_cache - Generate info table cache data
  1092. + * @ni: NMBM instance structure
  1093. + *
  1094. + * Generate info table cache data to be written into flash.
  1095. + */
  1096. +static bool nmbm_generate_info_table_cache(struct nmbm_instance *ni)
  1097. +{
  1098. + bool changed = false;
  1099. +
  1100. + memset(ni->info_table_cache, 0xff, ni->info_table_size);
  1101. +
  1102. + memcpy(ni->info_table_cache + ni->info_table.state_table_off,
  1103. + ni->block_state, ni->state_table_size);
  1104. +
  1105. + memcpy(ni->info_table_cache + ni->info_table.mapping_table_off,
  1106. + ni->block_mapping, ni->mapping_table_size);
  1107. +
  1108. + ni->info_table.header.magic = NMBM_MAGIC_INFO_TABLE;
  1109. + ni->info_table.header.version = NMBM_VER;
  1110. + ni->info_table.header.size = ni->info_table_size;
  1111. +
  1112. + if (ni->block_state_changed || ni->block_mapping_changed) {
  1113. + ni->info_table.write_count++;
  1114. + changed = true;
  1115. + }
  1116. +
  1117. + memcpy(ni->info_table_cache, &ni->info_table, sizeof(ni->info_table));
  1118. +
  1119. + nmbm_update_checksum((struct nmbm_header *)ni->info_table_cache);
  1120. +
  1121. + return changed;
  1122. +}
  1123. +
  1124. +/*
  1125. + * nmbm_write_info_table - Write info table into NAND within a range
  1126. + * @ni: NMBM instance structure
  1127. + * @ba: preferred start block address for writing
  1128. + * @limit: highest block address allowed for writing
  1129. + * @actual_start_ba: actual start block address of info table
  1130. + * @actual_end_ba: block address after the end of info table
  1131. + *
  1132. + * @limit is counted into the allowed write address.
  1133. + */
  1134. +static bool nmbm_write_info_table(struct nmbm_instance *ni, uint32_t ba,
  1135. + uint32_t limit, uint32_t *actual_start_ba,
  1136. + uint32_t *actual_end_ba)
  1137. +{
  1138. + return nmbm_write_mgmt_range(ni, ba, limit, ni->info_table_cache,
  1139. + ni->info_table_size, actual_start_ba,
  1140. + actual_end_ba);
  1141. +}
  1142. +
  1143. +/*
  1144. + * nmbm_mark_tables_clean - Mark info table `clean'
  1145. + * @ni: NMBM instance structure
  1146. + */
  1147. +static void nmbm_mark_tables_clean(struct nmbm_instance *ni)
  1148. +{
  1149. + ni->block_state_changed = 0;
  1150. + ni->block_mapping_changed = 0;
  1151. +}
  1152. +
  1153. +/*
  1154. + * nmbm_try_reserve_blocks - Reserve blocks with compromisation
  1155. + * @ni: NMBM instance structure
  1156. + * @ba: start physical block address
  1157. + * @nba: return physical block address after reservation
  1158. + * @count: number of good blocks to be skipped
  1159. + * @min_count: minimum number of good blocks to be skipped
  1160. + * @limit: highest/lowest block address allowed for walking
  1161. + *
  1162. + * Reserve specific blocks. If failed, try to reserve as many as possible.
  1163. + */
  1164. +static bool nmbm_try_reserve_blocks(struct nmbm_instance *ni, uint32_t ba,
  1165. + uint32_t *nba, uint32_t count,
  1166. + int32_t min_count, int32_t limit)
  1167. +{
  1168. + int32_t nblocks = count;
  1169. + bool success;
  1170. +
  1171. + while (nblocks >= min_count) {
  1172. + success = nmbm_block_walk(ni, true, ba, nba, nblocks, limit);
  1173. + if (success)
  1174. + return true;
  1175. +
  1176. + nblocks--;
  1177. + }
  1178. +
  1179. + return false;
  1180. +}
  1181. +
  1182. +/*
  1183. + * nmbm_rebuild_info_table - Build main & backup info table from scratch
  1184. + * @ni: NMBM instance structure
  1185. + * @allow_no_gap: allow no spare blocks between two tables
  1186. + */
  1187. +static bool nmbm_rebuild_info_table(struct nmbm_instance *ni)
  1188. +{
  1189. + uint32_t table_start_ba, table_end_ba, next_start_ba;
  1190. + uint32_t main_table_end_ba;
  1191. + bool success;
  1192. +
  1193. + /* Set initial value */
  1194. + ni->main_table_ba = 0;
  1195. + ni->backup_table_ba = 0;
  1196. + ni->mapping_blocks_ba = ni->mapping_blocks_top_ba;
  1197. +
  1198. + /* Write main table */
  1199. + success = nmbm_write_info_table(ni, ni->mgmt_start_ba,
  1200. + ni->mapping_blocks_top_ba,
  1201. + &table_start_ba, &table_end_ba);
  1202. + if (!success) {
  1203. + /* Failed to write main table, data will be lost */
  1204. + nlog_emerg(ni, "Unable to write at least one info table!\n");
  1205. + nlog_emerg(ni, "Please save your data before power off!\n");
  1206. + ni->protected = 1;
  1207. + return false;
  1208. + }
  1209. +
  1210. + /* Main info table is successfully written, record its offset */
  1211. + ni->main_table_ba = table_start_ba;
  1212. + main_table_end_ba = table_end_ba;
  1213. +
  1214. + /* Adjust mapping_blocks_ba */
  1215. + ni->mapping_blocks_ba = table_end_ba;
  1216. +
  1217. + nmbm_mark_tables_clean(ni);
  1218. +
  1219. + nlog_table_creation(ni, true, table_start_ba, table_end_ba);
  1220. +
  1221. + /* Reserve spare blocks for main info table. */
  1222. + success = nmbm_try_reserve_blocks(ni, table_end_ba,
  1223. + &next_start_ba,
  1224. + ni->info_table_spare_blocks, 0,
  1225. + ni->mapping_blocks_top_ba -
  1226. + size2blk(ni, ni->info_table_size));
  1227. + if (!success) {
  1228. + /* There is no spare block. */
  1229. + nlog_debug(ni, "No room for backup info table\n");
  1230. + return true;
  1231. + }
  1232. +
  1233. + /* Write backup info table. */
  1234. + success = nmbm_write_info_table(ni, next_start_ba,
  1235. + ni->mapping_blocks_top_ba,
  1236. + &table_start_ba, &table_end_ba);
  1237. + if (!success) {
  1238. + /* There is no enough blocks for backup table. */
  1239. + nlog_debug(ni, "No room for backup info table\n");
  1240. + return true;
  1241. + }
  1242. +
  1243. + /* Backup table is successfully written, record its offset */
  1244. + ni->backup_table_ba = table_start_ba;
  1245. +
  1246. + /* Adjust mapping_blocks_off */
  1247. + ni->mapping_blocks_ba = table_end_ba;
  1248. +
  1249. + /* Erase spare blocks of main table to clean possible interference data */
  1250. + nmbm_erase_range(ni, main_table_end_ba, ni->backup_table_ba);
  1251. +
  1252. + nlog_table_creation(ni, false, table_start_ba, table_end_ba);
  1253. +
  1254. + return true;
  1255. +}
  1256. +
  1257. +/*
  1258. + * nmbm_rescue_single_info_table - Rescue when there is only one info table
  1259. + * @ni: NMBM instance structure
  1260. + *
  1261. + * This function is called when there is only one info table exists.
  1262. + * This function may fail if we can't write new info table
  1263. + */
  1264. +static bool nmbm_rescue_single_info_table(struct nmbm_instance *ni)
  1265. +{
  1266. + uint32_t table_start_ba, table_end_ba, write_ba;
  1267. + bool success;
  1268. +
  1269. + /* Try to write new info table in front of existing table */
  1270. + success = nmbm_write_info_table(ni, ni->mgmt_start_ba,
  1271. + ni->main_table_ba,
  1272. + &table_start_ba,
  1273. + &table_end_ba);
  1274. + if (success) {
  1275. + /*
  1276. + * New table becomes the main table, existing table becomes
  1277. + * the backup table.
  1278. + */
  1279. + ni->backup_table_ba = ni->main_table_ba;
  1280. + ni->main_table_ba = table_start_ba;
  1281. +
  1282. + nmbm_mark_tables_clean(ni);
  1283. +
  1284. + /* Erase spare blocks of main table to clean possible interference data */
  1285. + nmbm_erase_range(ni, table_end_ba, ni->backup_table_ba);
  1286. +
  1287. + nlog_table_creation(ni, true, table_start_ba, table_end_ba);
  1288. +
  1289. + return true;
  1290. + }
  1291. +
  1292. + /* Try to reserve spare blocks for existing table */
  1293. + success = nmbm_try_reserve_blocks(ni, ni->mapping_blocks_ba, &write_ba,
  1294. + ni->info_table_spare_blocks, 0,
  1295. + ni->mapping_blocks_top_ba -
  1296. + size2blk(ni, ni->info_table_size));
  1297. + if (!success) {
  1298. + nlog_warn(ni, "Failed to rescue single info table\n");
  1299. + return false;
  1300. + }
  1301. +
  1302. + /* Try to write new info table next to the existing table */
  1303. + while (write_ba >= ni->mapping_blocks_ba) {
  1304. + schedule();
  1305. +
  1306. + success = nmbm_write_info_table(ni, write_ba,
  1307. + ni->mapping_blocks_top_ba,
  1308. + &table_start_ba,
  1309. + &table_end_ba);
  1310. + if (success)
  1311. + break;
  1312. +
  1313. + write_ba--;
  1314. + }
  1315. +
  1316. + if (success) {
  1317. + /* Erase spare blocks of main table to clean possible interference data */
  1318. + nmbm_erase_range(ni, ni->mapping_blocks_ba, table_start_ba);
  1319. +
  1320. + /* New table becomes the backup table */
  1321. + ni->backup_table_ba = table_start_ba;
  1322. + ni->mapping_blocks_ba = table_end_ba;
  1323. +
  1324. + nmbm_mark_tables_clean(ni);
  1325. +
  1326. + nlog_table_creation(ni, false, table_start_ba, table_end_ba);
  1327. +
  1328. + return true;
  1329. + }
  1330. +
  1331. + nlog_warn(ni, "Failed to rescue single info table\n");
  1332. + return false;
  1333. +}
  1334. +
  1335. +/*
  1336. + * nmbm_update_single_info_table - Update specific one info table
  1337. + * @ni: NMBM instance structure
  1338. + */
  1339. +static bool nmbm_update_single_info_table(struct nmbm_instance *ni,
  1340. + bool update_main_table)
  1341. +{
  1342. + uint32_t write_start_ba, write_limit, table_start_ba, table_end_ba;
  1343. + bool success;
  1344. +
  1345. + /* Determine the write range */
  1346. + if (update_main_table) {
  1347. + write_start_ba = ni->main_table_ba;
  1348. + write_limit = ni->backup_table_ba;
  1349. + } else {
  1350. + write_start_ba = ni->backup_table_ba;
  1351. + write_limit = ni->mapping_blocks_top_ba;
  1352. + }
  1353. +
  1354. + nmbm_mark_block_color_mgmt(ni, write_start_ba, write_limit - 1);
  1355. +
  1356. + success = nmbm_write_info_table(ni, write_start_ba, write_limit,
  1357. + &table_start_ba, &table_end_ba);
  1358. + if (success) {
  1359. + if (update_main_table) {
  1360. + ni->main_table_ba = table_start_ba;
  1361. + } else {
  1362. + ni->backup_table_ba = table_start_ba;
  1363. + ni->mapping_blocks_ba = table_end_ba;
  1364. + }
  1365. +
  1366. + nmbm_mark_tables_clean(ni);
  1367. +
  1368. + nlog_table_update(ni, update_main_table, table_start_ba,
  1369. + table_end_ba);
  1370. +
  1371. + return true;
  1372. + }
  1373. +
  1374. + if (update_main_table) {
  1375. + /*
  1376. + * If failed to update main table, make backup table the new
  1377. + * main table, and call nmbm_rescue_single_info_table()
  1378. + */
  1379. + nlog_warn(ni, "Unable to update %s info table\n",
  1380. + update_main_table ? "Main" : "Backup");
  1381. +
  1382. + ni->main_table_ba = ni->backup_table_ba;
  1383. + ni->backup_table_ba = 0;
  1384. + return nmbm_rescue_single_info_table(ni);
  1385. + }
  1386. +
  1387. + /* Only one table left */
  1388. + ni->mapping_blocks_ba = ni->backup_table_ba;
  1389. + ni->backup_table_ba = 0;
  1390. +
  1391. + return false;
  1392. +}
  1393. +
  1394. +/*
  1395. + * nmbm_rescue_main_info_table - Rescue when failed to write main info table
  1396. + * @ni: NMBM instance structure
  1397. + *
  1398. + * This function is called when main info table failed to be written, and
  1399. + * backup info table exists.
  1400. + */
  1401. +static bool nmbm_rescue_main_info_table(struct nmbm_instance *ni)
  1402. +{
  1403. + uint32_t tmp_table_start_ba, tmp_table_end_ba, main_table_start_ba;
  1404. + uint32_t main_table_end_ba, write_ba;
  1405. + uint32_t info_table_erasesize = size2blk(ni, ni->info_table_size);
  1406. + bool success;
  1407. +
  1408. + /* Try to reserve spare blocks for existing backup info table */
  1409. + success = nmbm_try_reserve_blocks(ni, ni->mapping_blocks_ba, &write_ba,
  1410. + ni->info_table_spare_blocks, 0,
  1411. + ni->mapping_blocks_top_ba -
  1412. + info_table_erasesize);
  1413. + if (!success) {
  1414. + /* There is no spare block. Backup info table becomes the main table. */
  1415. + nlog_err(ni, "No room for temporary info table\n");
  1416. + ni->main_table_ba = ni->backup_table_ba;
  1417. + ni->backup_table_ba = 0;
  1418. + return true;
  1419. + }
  1420. +
  1421. + /* Try to write temporary info table into spare unmapped blocks */
  1422. + while (write_ba >= ni->mapping_blocks_ba) {
  1423. + schedule();
  1424. +
  1425. + success = nmbm_write_info_table(ni, write_ba,
  1426. + ni->mapping_blocks_top_ba,
  1427. + &tmp_table_start_ba,
  1428. + &tmp_table_end_ba);
  1429. + if (success)
  1430. + break;
  1431. +
  1432. + write_ba--;
  1433. + }
  1434. +
  1435. + if (!success) {
  1436. + /* Backup info table becomes the main table */
  1437. + nlog_err(ni, "Failed to update main info table\n");
  1438. + ni->main_table_ba = ni->backup_table_ba;
  1439. + ni->backup_table_ba = 0;
  1440. + return true;
  1441. + }
  1442. +
  1443. + /* Adjust mapping_blocks_off */
  1444. + ni->mapping_blocks_ba = tmp_table_end_ba;
  1445. +
  1446. + nmbm_mark_block_color_mgmt(ni, ni->backup_table_ba,
  1447. + tmp_table_end_ba - 1);
  1448. +
  1449. + /*
  1450. + * Now write main info table at the beginning of management area.
  1451. + * This operation will generally destroy the original backup info
  1452. + * table.
  1453. + */
  1454. + success = nmbm_write_info_table(ni, ni->mgmt_start_ba,
  1455. + tmp_table_start_ba,
  1456. + &main_table_start_ba,
  1457. + &main_table_end_ba);
  1458. + if (!success) {
  1459. + /* Temporary info table becomes the main table */
  1460. + ni->main_table_ba = tmp_table_start_ba;
  1461. + ni->backup_table_ba = 0;
  1462. +
  1463. + nmbm_mark_tables_clean(ni);
  1464. +
  1465. + nlog_err(ni, "Failed to update main info table\n");
  1466. + nmbm_mark_block_color_info_table(ni, tmp_table_start_ba,
  1467. + tmp_table_end_ba - 1);
  1468. +
  1469. + return true;
  1470. + }
  1471. +
  1472. + /* Main info table has been successfully written, record its offset */
  1473. + ni->main_table_ba = main_table_start_ba;
  1474. +
  1475. + nmbm_mark_tables_clean(ni);
  1476. +
  1477. + nlog_table_creation(ni, true, main_table_start_ba, main_table_end_ba);
  1478. +
  1479. + /*
  1480. + * Temporary info table becomes the new backup info table if it's
  1481. + * not overwritten.
  1482. + */
  1483. + if (main_table_end_ba <= tmp_table_start_ba) {
  1484. + ni->backup_table_ba = tmp_table_start_ba;
  1485. +
  1486. + nlog_table_creation(ni, false, tmp_table_start_ba,
  1487. + tmp_table_end_ba);
  1488. +
  1489. + return true;
  1490. + }
  1491. +
  1492. + /* Adjust mapping_blocks_off */
  1493. + ni->mapping_blocks_ba = main_table_end_ba;
  1494. +
  1495. + /* Try to reserve spare blocks for new main info table */
  1496. + success = nmbm_try_reserve_blocks(ni, main_table_end_ba, &write_ba,
  1497. + ni->info_table_spare_blocks, 0,
  1498. + ni->mapping_blocks_top_ba -
  1499. + info_table_erasesize);
  1500. + if (!success) {
  1501. + /* There is no spare block. Only main table exists. */
  1502. + nlog_err(ni, "No room for backup info table\n");
  1503. + ni->backup_table_ba = 0;
  1504. + return true;
  1505. + }
  1506. +
  1507. + /* Write new backup info table. */
  1508. + while (write_ba >= main_table_end_ba) {
  1509. + schedule();
  1510. +
  1511. + success = nmbm_write_info_table(ni, write_ba,
  1512. + ni->mapping_blocks_top_ba,
  1513. + &tmp_table_start_ba,
  1514. + &tmp_table_end_ba);
  1515. + if (success)
  1516. + break;
  1517. +
  1518. + write_ba--;
  1519. + }
  1520. +
  1521. + if (!success) {
  1522. + nlog_err(ni, "No room for backup info table\n");
  1523. + ni->backup_table_ba = 0;
  1524. + return true;
  1525. + }
  1526. +
  1527. + /* Backup info table has been successfully written, record its offset */
  1528. + ni->backup_table_ba = tmp_table_start_ba;
  1529. +
  1530. + /* Adjust mapping_blocks_off */
  1531. + ni->mapping_blocks_ba = tmp_table_end_ba;
  1532. +
  1533. + /* Erase spare blocks of main table to clean possible interference data */
  1534. + nmbm_erase_range(ni, main_table_end_ba, ni->backup_table_ba);
  1535. +
  1536. + nlog_table_creation(ni, false, tmp_table_start_ba, tmp_table_end_ba);
  1537. +
  1538. + return true;
  1539. +}
  1540. +
  1541. +/*
  1542. + * nmbm_update_info_table_once - Update info table once
  1543. + * @ni: NMBM instance structure
  1544. + * @force: force update
  1545. + *
  1546. + * Update both main and backup info table. Return true if at least one info
  1547. + * table has been successfully written.
  1548. + * This function only try to update info table once regard less of the result.
  1549. + */
  1550. +static bool nmbm_update_info_table_once(struct nmbm_instance *ni, bool force)
  1551. +{
  1552. + uint32_t table_start_ba, table_end_ba;
  1553. + uint32_t main_table_limit;
  1554. + bool success;
  1555. +
  1556. + /* Do nothing if there is no change */
  1557. + if (!nmbm_generate_info_table_cache(ni) && !force)
  1558. + return true;
  1559. +
  1560. + /* Check whether both two tables exist */
  1561. + if (!ni->backup_table_ba) {
  1562. + main_table_limit = ni->mapping_blocks_top_ba;
  1563. + goto write_main_table;
  1564. + }
  1565. +
  1566. + nmbm_mark_block_color_mgmt(ni, ni->backup_table_ba,
  1567. + ni->mapping_blocks_ba - 1);
  1568. +
  1569. + /*
  1570. + * Write backup info table in its current range.
  1571. + * Note that limit is set to mapping_blocks_top_off to provide as many
  1572. + * spare blocks as possible for the backup table. If at last
  1573. + * unmapped blocks are used by backup table, mapping_blocks_off will
  1574. + * be adjusted.
  1575. + */
  1576. + success = nmbm_write_info_table(ni, ni->backup_table_ba,
  1577. + ni->mapping_blocks_top_ba,
  1578. + &table_start_ba, &table_end_ba);
  1579. + if (!success) {
  1580. + /*
  1581. + * There is nothing to do if failed to write backup table.
  1582. + * Write the main table now.
  1583. + */
  1584. + nlog_err(ni, "No room for backup table\n");
  1585. + ni->mapping_blocks_ba = ni->backup_table_ba;
  1586. + ni->backup_table_ba = 0;
  1587. + main_table_limit = ni->mapping_blocks_top_ba;
  1588. + goto write_main_table;
  1589. + }
  1590. +
  1591. + /* Backup table is successfully written, record its offset */
  1592. + ni->backup_table_ba = table_start_ba;
  1593. +
  1594. + /* Adjust mapping_blocks_off */
  1595. + ni->mapping_blocks_ba = table_end_ba;
  1596. +
  1597. + nmbm_mark_tables_clean(ni);
  1598. +
  1599. + /* The normal limit of main table */
  1600. + main_table_limit = ni->backup_table_ba;
  1601. +
  1602. + nlog_table_update(ni, false, table_start_ba, table_end_ba);
  1603. +
  1604. +write_main_table:
  1605. + if (!ni->main_table_ba)
  1606. + goto rebuild_tables;
  1607. +
  1608. + if (!ni->backup_table_ba)
  1609. + nmbm_mark_block_color_mgmt(ni, ni->mgmt_start_ba,
  1610. + ni->mapping_blocks_ba - 1);
  1611. + else
  1612. + nmbm_mark_block_color_mgmt(ni, ni->mgmt_start_ba,
  1613. + ni->backup_table_ba - 1);
  1614. +
  1615. + /* Write main info table in its current range */
  1616. + success = nmbm_write_info_table(ni, ni->main_table_ba,
  1617. + main_table_limit, &table_start_ba,
  1618. + &table_end_ba);
  1619. + if (!success) {
  1620. + /* If failed to write main table, go rescue procedure */
  1621. + if (!ni->backup_table_ba)
  1622. + goto rebuild_tables;
  1623. +
  1624. + return nmbm_rescue_main_info_table(ni);
  1625. + }
  1626. +
  1627. + /* Main info table is successfully written, record its offset */
  1628. + ni->main_table_ba = table_start_ba;
  1629. +
  1630. + /* Adjust mapping_blocks_off */
  1631. + if (!ni->backup_table_ba)
  1632. + ni->mapping_blocks_ba = table_end_ba;
  1633. +
  1634. + nmbm_mark_tables_clean(ni);
  1635. +
  1636. + nlog_table_update(ni, true, table_start_ba, table_end_ba);
  1637. +
  1638. + return true;
  1639. +
  1640. +rebuild_tables:
  1641. + return nmbm_rebuild_info_table(ni);
  1642. +}
  1643. +
  1644. +/*
  1645. + * nmbm_update_info_table - Update info table
  1646. + * @ni: NMBM instance structure
  1647. + *
  1648. + * Update both main and backup info table. Return true if at least one table
  1649. + * has been successfully written.
  1650. + * This function will try to update info table repeatedly until no new bad
  1651. + * block found during updating.
  1652. + */
  1653. +static bool nmbm_update_info_table(struct nmbm_instance *ni)
  1654. +{
  1655. + bool success;
  1656. +
  1657. + if (ni->protected)
  1658. + return true;
  1659. +
  1660. + while (ni->block_state_changed || ni->block_mapping_changed) {
  1661. + success = nmbm_update_info_table_once(ni, false);
  1662. + if (!success) {
  1663. + nlog_err(ni, "Failed to update info table\n");
  1664. + return false;
  1665. + }
  1666. + }
  1667. +
  1668. + return true;
  1669. +}
  1670. +
  1671. +/*
  1672. + * nmbm_map_block - Map a bad block to a unused spare block
  1673. + * @ni: NMBM instance structure
  1674. + * @lb: logic block addr to map
  1675. + */
  1676. +static bool nmbm_map_block(struct nmbm_instance *ni, uint32_t lb)
  1677. +{
  1678. + uint32_t pb;
  1679. + bool success;
  1680. +
  1681. + if (ni->mapping_blocks_ba == ni->mapping_blocks_top_ba) {
  1682. + nlog_warn(ni, "No spare unmapped blocks.\n");
  1683. + return false;
  1684. + }
  1685. +
  1686. + success = nmbm_block_walk(ni, false, ni->mapping_blocks_top_ba, &pb, 0,
  1687. + ni->mapping_blocks_ba);
  1688. + if (!success) {
  1689. + nlog_warn(ni, "No spare unmapped blocks.\n");
  1690. + nmbm_update_info_table(ni);
  1691. + ni->mapping_blocks_top_ba = ni->mapping_blocks_ba;
  1692. + return false;
  1693. + }
  1694. +
  1695. + ni->block_mapping[lb] = pb;
  1696. + ni->mapping_blocks_top_ba--;
  1697. + ni->block_mapping_changed++;
  1698. +
  1699. + nlog_info(ni, "Logic block %u mapped to physical blcok %u\n", lb, pb);
  1700. + nmbm_mark_block_color_mapped(ni, pb);
  1701. +
  1702. + return true;
  1703. +}
  1704. +
  1705. +/*
  1706. + * nmbm_create_info_table - Create info table(s)
  1707. + * @ni: NMBM instance structure
  1708. + *
  1709. + * This function assumes that the chip has no existing info table(s)
  1710. + */
  1711. +static bool nmbm_create_info_table(struct nmbm_instance *ni)
  1712. +{
  1713. + uint32_t lb;
  1714. + bool success;
  1715. +
  1716. + /* Set initial mapping_blocks_top_off */
  1717. + success = nmbm_block_walk(ni, false, ni->signature_ba,
  1718. + &ni->mapping_blocks_top_ba, 1,
  1719. + ni->mgmt_start_ba);
  1720. + if (!success) {
  1721. + nlog_err(ni, "No room for spare blocks\n");
  1722. + return false;
  1723. + }
  1724. +
  1725. + /* Generate info table cache */
  1726. + nmbm_generate_info_table_cache(ni);
  1727. +
  1728. + /* Write info table */
  1729. + success = nmbm_rebuild_info_table(ni);
  1730. + if (!success) {
  1731. + nlog_err(ni, "Failed to build info tables\n");
  1732. + return false;
  1733. + }
  1734. +
  1735. + /* Remap bad block(s) at end of data area */
  1736. + for (lb = ni->data_block_count; lb < ni->mgmt_start_ba; lb++) {
  1737. + success = nmbm_map_block(ni, lb);
  1738. + if (!success)
  1739. + break;
  1740. +
  1741. + ni->data_block_count++;
  1742. + }
  1743. +
  1744. + /* If state table and/or mapping table changed, update info table. */
  1745. + success = nmbm_update_info_table(ni);
  1746. + if (!success)
  1747. + return false;
  1748. +
  1749. + return true;
  1750. +}
  1751. +
  1752. +/*
  1753. + * nmbm_create_new - Create NMBM on a new chip
  1754. + * @ni: NMBM instance structure
  1755. + */
  1756. +static bool nmbm_create_new(struct nmbm_instance *ni)
  1757. +{
  1758. + bool success;
  1759. +
  1760. + /* Determine the boundary of management blocks */
  1761. + ni->mgmt_start_ba = ni->block_count * (NMBM_MGMT_DIV - ni->lower.max_ratio) / NMBM_MGMT_DIV;
  1762. +
  1763. + if (ni->lower.max_reserved_blocks && ni->block_count - ni->mgmt_start_ba > ni->lower.max_reserved_blocks)
  1764. + ni->mgmt_start_ba = ni->block_count - ni->lower.max_reserved_blocks;
  1765. +
  1766. + nlog_info(ni, "NMBM management region starts at block %u [0x%08llx]\n",
  1767. + ni->mgmt_start_ba, ba2addr(ni, ni->mgmt_start_ba));
  1768. + nmbm_mark_block_color_mgmt(ni, ni->mgmt_start_ba, ni->block_count - 1);
  1769. +
  1770. + /* Fill block state table & mapping table */
  1771. + nmbm_scan_badblocks(ni);
  1772. + nmbm_build_mapping_table(ni);
  1773. +
  1774. + /* Write signature */
  1775. + ni->signature.header.magic = NMBM_MAGIC_SIGNATURE;
  1776. + ni->signature.header.version = NMBM_VER;
  1777. + ni->signature.header.size = sizeof(ni->signature);
  1778. + ni->signature.nand_size = ni->lower.size;
  1779. + ni->signature.block_size = ni->lower.erasesize;
  1780. + ni->signature.page_size = ni->lower.writesize;
  1781. + ni->signature.spare_size = ni->lower.oobsize;
  1782. + ni->signature.mgmt_start_pb = ni->mgmt_start_ba;
  1783. + ni->signature.max_try_count = NMBM_TRY_COUNT;
  1784. + nmbm_update_checksum(&ni->signature.header);
  1785. +
  1786. + if (ni->lower.flags & NMBM_F_READ_ONLY) {
  1787. + nlog_info(ni, "NMBM has been initialized in read-only mode\n");
  1788. + return true;
  1789. + }
  1790. +
  1791. + success = nmbm_write_signature(ni, ni->mgmt_start_ba,
  1792. + &ni->signature, &ni->signature_ba);
  1793. + if (!success) {
  1794. + nlog_err(ni, "Failed to write signature to a proper offset\n");
  1795. + return false;
  1796. + }
  1797. +
  1798. + nlog_info(ni, "Signature has been written to block %u [0x%08llx]\n",
  1799. + ni->signature_ba, ba2addr(ni, ni->signature_ba));
  1800. + nmbm_mark_block_color_signature(ni, ni->signature_ba);
  1801. +
  1802. + /* Write info table(s) */
  1803. + success = nmbm_create_info_table(ni);
  1804. + if (success) {
  1805. + nlog_info(ni, "NMBM has been successfully created\n");
  1806. + return true;
  1807. + }
  1808. +
  1809. + return false;
  1810. +}
  1811. +
  1812. +/*
  1813. + * nmbm_check_info_table_header - Check if a info table header is valid
  1814. + * @ni: NMBM instance structure
  1815. + * @data: pointer to the info table header
  1816. + */
  1817. +static bool nmbm_check_info_table_header(struct nmbm_instance *ni, void *data)
  1818. +{
  1819. + struct nmbm_info_table_header *ifthdr = data;
  1820. +
  1821. + if (ifthdr->header.magic != NMBM_MAGIC_INFO_TABLE)
  1822. + return false;
  1823. +
  1824. + if (ifthdr->header.size != ni->info_table_size)
  1825. + return false;
  1826. +
  1827. + if (ifthdr->mapping_table_off - ifthdr->state_table_off < ni->state_table_size)
  1828. + return false;
  1829. +
  1830. + if (ni->info_table_size - ifthdr->mapping_table_off < ni->mapping_table_size)
  1831. + return false;
  1832. +
  1833. + return true;
  1834. +}
  1835. +
  1836. +/*
  1837. + * nmbm_check_info_table - Check if a whole info table is valid
  1838. + * @ni: NMBM instance structure
  1839. + * @start_ba: start block address of this table
  1840. + * @end_ba: end block address of this table
  1841. + * @data: pointer to the info table header
  1842. + * @mapping_blocks_top_ba: return the block address of top remapped block
  1843. + */
  1844. +static bool nmbm_check_info_table(struct nmbm_instance *ni, uint32_t start_ba,
  1845. + uint32_t end_ba, void *data,
  1846. + uint32_t *mapping_blocks_top_ba)
  1847. +{
  1848. + struct nmbm_info_table_header *ifthdr = data;
  1849. + int32_t *block_mapping = (int32_t *)((uintptr_t)data + ifthdr->mapping_table_off);
  1850. + nmbm_bitmap_t *block_state = (nmbm_bitmap_t *)((uintptr_t)data + ifthdr->state_table_off);
  1851. + uint32_t minimum_mapping_pb = ni->signature_ba;
  1852. + uint32_t ba;
  1853. +
  1854. + for (ba = 0; ba < ni->data_block_count; ba++) {
  1855. + if ((block_mapping[ba] >= ni->data_block_count && block_mapping[ba] < end_ba) ||
  1856. + block_mapping[ba] == ni->signature_ba)
  1857. + return false;
  1858. +
  1859. + if (block_mapping[ba] >= end_ba && block_mapping[ba] < minimum_mapping_pb)
  1860. + minimum_mapping_pb = block_mapping[ba];
  1861. + }
  1862. +
  1863. + for (ba = start_ba; ba < end_ba; ba++) {
  1864. + if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
  1865. + continue;
  1866. +
  1867. + if (nmbm_get_block_state_raw(block_state, ba) != BLOCK_ST_GOOD)
  1868. + return false;
  1869. + }
  1870. +
  1871. + *mapping_blocks_top_ba = minimum_mapping_pb - 1;
  1872. +
  1873. + return true;
  1874. +}
  1875. +
  1876. +/*
  1877. + * nmbm_try_load_info_table - Try to load info table from a address
  1878. + * @ni: NMBM instance structure
  1879. + * @ba: start block address of the info table
  1880. + * @eba: return the block address after end of the table
  1881. + * @write_count: return the write count of this table
  1882. + * @mapping_blocks_top_ba: return the block address of top remapped block
  1883. + * @table_loaded: used to record whether ni->info_table has valid data
  1884. + */
  1885. +static bool nmbm_try_load_info_table(struct nmbm_instance *ni, uint32_t ba,
  1886. + uint32_t *eba, uint32_t *write_count,
  1887. + uint32_t *mapping_blocks_top_ba,
  1888. + bool table_loaded)
  1889. +{
  1890. + struct nmbm_info_table_header *ifthdr = (void *)ni->info_table_cache;
  1891. + uint8_t *off = ni->info_table_cache;
  1892. + uint32_t limit = ba + size2blk(ni, ni->info_table_size);
  1893. + uint32_t start_ba = 0, chunksize, sizeremain = ni->info_table_size;
  1894. + bool success, checkhdr = true;
  1895. + int ret;
  1896. +
  1897. + while (sizeremain && ba < limit) {
  1898. + schedule();
  1899. +
  1900. + if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
  1901. + goto next_block;
  1902. +
  1903. + if (nmbm_check_bad_phys_block(ni, ba)) {
  1904. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  1905. + goto next_block;
  1906. + }
  1907. +
  1908. + chunksize = sizeremain;
  1909. + if (chunksize > ni->lower.erasesize)
  1910. + chunksize = ni->lower.erasesize;
  1911. +
  1912. + /* Assume block with ECC error has no info table data */
  1913. + ret = nmbn_read_data(ni, ba2addr(ni, ba), off, chunksize);
  1914. + if (ret < 0)
  1915. + goto skip_bad_block;
  1916. + else if (ret > 0)
  1917. + return false;
  1918. +
  1919. + if (checkhdr) {
  1920. + success = nmbm_check_info_table_header(ni, off);
  1921. + if (!success)
  1922. + return false;
  1923. +
  1924. + start_ba = ba;
  1925. + checkhdr = false;
  1926. + }
  1927. +
  1928. + off += chunksize;
  1929. + sizeremain -= chunksize;
  1930. +
  1931. + goto next_block;
  1932. +
  1933. + skip_bad_block:
  1934. + /* Only mark bad in memory */
  1935. + nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
  1936. +
  1937. + next_block:
  1938. + ba++;
  1939. + }
  1940. +
  1941. + if (sizeremain)
  1942. + return false;
  1943. +
  1944. + success = nmbm_check_header(ni->info_table_cache, ni->info_table_size);
  1945. + if (!success)
  1946. + return false;
  1947. +
  1948. + *eba = ba;
  1949. + *write_count = ifthdr->write_count;
  1950. +
  1951. + success = nmbm_check_info_table(ni, start_ba, ba, ni->info_table_cache,
  1952. + mapping_blocks_top_ba);
  1953. + if (!success)
  1954. + return false;
  1955. +
  1956. + if (!table_loaded || ifthdr->write_count > ni->info_table.write_count) {
  1957. + memcpy(&ni->info_table, ifthdr, sizeof(ni->info_table));
  1958. + memcpy(ni->block_state,
  1959. + (uint8_t *)ifthdr + ifthdr->state_table_off,
  1960. + ni->state_table_size);
  1961. + memcpy(ni->block_mapping,
  1962. + (uint8_t *)ifthdr + ifthdr->mapping_table_off,
  1963. + ni->mapping_table_size);
  1964. + ni->info_table.write_count = ifthdr->write_count;
  1965. + }
  1966. +
  1967. + return true;
  1968. +}
  1969. +
  1970. +/*
  1971. + * nmbm_search_info_table - Search info table from specific address
  1972. + * @ni: NMBM instance structure
  1973. + * @ba: start block address to search
  1974. + * @limit: highest block address allowed for searching
  1975. + * @table_start_ba: return the start block address of this table
  1976. + * @table_end_ba: return the block address after end of this table
  1977. + * @write_count: return the write count of this table
  1978. + * @mapping_blocks_top_ba: return the block address of top remapped block
  1979. + * @table_loaded: used to record whether ni->info_table has valid data
  1980. + */
  1981. +static bool nmbm_search_info_table(struct nmbm_instance *ni, uint32_t ba,
  1982. + uint32_t limit, uint32_t *table_start_ba,
  1983. + uint32_t *table_end_ba,
  1984. + uint32_t *write_count,
  1985. + uint32_t *mapping_blocks_top_ba,
  1986. + bool table_loaded)
  1987. +{
  1988. + bool success;
  1989. +
  1990. + while (ba < limit - size2blk(ni, ni->info_table_size)) {
  1991. + schedule();
  1992. +
  1993. + success = nmbm_try_load_info_table(ni, ba, table_end_ba,
  1994. + write_count,
  1995. + mapping_blocks_top_ba,
  1996. + table_loaded);
  1997. + if (success) {
  1998. + *table_start_ba = ba;
  1999. + return true;
  2000. + }
  2001. +
  2002. + ba++;
  2003. + }
  2004. +
  2005. + return false;
  2006. +}
  2007. +
  2008. +/*
  2009. + * nmbm_load_info_table - Load info table(s) from a chip
  2010. + * @ni: NMBM instance structure
  2011. + * @ba: start block address to search info table
  2012. + * @limit: highest block address allowed for searching
  2013. + */
  2014. +static bool nmbm_load_info_table(struct nmbm_instance *ni, uint32_t ba,
  2015. + uint32_t limit)
  2016. +{
  2017. + uint32_t main_table_end_ba, backup_table_end_ba, table_end_ba;
  2018. + uint32_t main_mapping_blocks_top_ba, backup_mapping_blocks_top_ba;
  2019. + uint32_t main_table_write_count, backup_table_write_count;
  2020. + uint32_t i;
  2021. + bool success;
  2022. +
  2023. + /* Set initial value */
  2024. + ni->main_table_ba = 0;
  2025. + ni->backup_table_ba = 0;
  2026. + ni->info_table.write_count = 0;
  2027. + ni->mapping_blocks_top_ba = ni->signature_ba - 1;
  2028. + ni->data_block_count = ni->signature.mgmt_start_pb;
  2029. +
  2030. + /* Find first info table */
  2031. + success = nmbm_search_info_table(ni, ba, limit, &ni->main_table_ba,
  2032. + &main_table_end_ba, &main_table_write_count,
  2033. + &main_mapping_blocks_top_ba, false);
  2034. + if (!success) {
  2035. + nlog_warn(ni, "No valid info table found\n");
  2036. + return false;
  2037. + }
  2038. +
  2039. + table_end_ba = main_table_end_ba;
  2040. +
  2041. + nlog_table_found(ni, true, main_table_write_count, ni->main_table_ba,
  2042. + main_table_end_ba);
  2043. +
  2044. + /* Find second info table */
  2045. + success = nmbm_search_info_table(ni, main_table_end_ba, limit,
  2046. + &ni->backup_table_ba, &backup_table_end_ba,
  2047. + &backup_table_write_count, &backup_mapping_blocks_top_ba, true);
  2048. + if (!success) {
  2049. + nlog_warn(ni, "Second info table not found\n");
  2050. + } else {
  2051. + table_end_ba = backup_table_end_ba;
  2052. +
  2053. + nlog_table_found(ni, false, backup_table_write_count,
  2054. + ni->backup_table_ba, backup_table_end_ba);
  2055. + }
  2056. +
  2057. + /* Pick mapping_blocks_top_ba */
  2058. + if (!ni->backup_table_ba) {
  2059. + ni->mapping_blocks_top_ba= main_mapping_blocks_top_ba;
  2060. + } else {
  2061. + if (main_table_write_count >= backup_table_write_count)
  2062. + ni->mapping_blocks_top_ba = main_mapping_blocks_top_ba;
  2063. + else
  2064. + ni->mapping_blocks_top_ba = backup_mapping_blocks_top_ba;
  2065. + }
  2066. +
  2067. + /* Set final mapping_blocks_ba */
  2068. + ni->mapping_blocks_ba = table_end_ba;
  2069. +
  2070. + /* Set final data_block_count */
  2071. + for (i = ni->signature.mgmt_start_pb; i > 0; i--) {
  2072. + if (ni->block_mapping[i - 1] >= 0) {
  2073. + ni->data_block_count = i;
  2074. + break;
  2075. + }
  2076. + }
  2077. +
  2078. + /* Debug purpose: mark mapped blocks and bad blocks */
  2079. + for (i = 0; i < ni->data_block_count; i++) {
  2080. + if (ni->block_mapping[i] > ni->mapping_blocks_top_ba)
  2081. + nmbm_mark_block_color_mapped(ni, ni->block_mapping[i]);
  2082. + }
  2083. +
  2084. + for (i = 0; i < ni->block_count; i++) {
  2085. + if (nmbm_get_block_state(ni, i) == BLOCK_ST_BAD)
  2086. + nmbm_mark_block_color_bad(ni, i);
  2087. + }
  2088. +
  2089. + /* Regenerate the info table cache from the final selected info table */
  2090. + nmbm_generate_info_table_cache(ni);
  2091. +
  2092. + if (ni->lower.flags & NMBM_F_READ_ONLY)
  2093. + return true;
  2094. +
  2095. + /*
  2096. + * If only one table exists, try to write another table.
  2097. + * If two tables have different write count, try to update info table
  2098. + */
  2099. + if (!ni->backup_table_ba) {
  2100. + success = nmbm_rescue_single_info_table(ni);
  2101. + } else if (main_table_write_count != backup_table_write_count) {
  2102. + /* Mark state & mapping tables changed */
  2103. + ni->block_state_changed = 1;
  2104. + ni->block_mapping_changed = 1;
  2105. +
  2106. + success = nmbm_update_single_info_table(ni,
  2107. + main_table_write_count < backup_table_write_count);
  2108. + } else {
  2109. + success = true;
  2110. + }
  2111. +
  2112. + /*
  2113. + * If there is no spare unmapped blocks, or still only one table
  2114. + * exists, set the chip to read-only
  2115. + */
  2116. + if (ni->mapping_blocks_ba == ni->mapping_blocks_top_ba) {
  2117. + nlog_warn(ni, "No spare unmapped blocks. Device is now read-only\n");
  2118. + ni->protected = 1;
  2119. + } else if (!success) {
  2120. + nlog_warn(ni, "Only one info table found. Device is now read-only\n");
  2121. + ni->protected = 1;
  2122. + }
  2123. +
  2124. + return true;
  2125. +}
  2126. +
  2127. +/*
  2128. + * nmbm_load_existing - Load NMBM from a new chip
  2129. + * @ni: NMBM instance structure
  2130. + */
  2131. +static bool nmbm_load_existing(struct nmbm_instance *ni)
  2132. +{
  2133. + bool success;
  2134. +
  2135. + /* Calculate the boundary of management blocks */
  2136. + ni->mgmt_start_ba = ni->signature.mgmt_start_pb;
  2137. +
  2138. + nlog_debug(ni, "NMBM management region starts at block %u [0x%08llx]\n",
  2139. + ni->mgmt_start_ba, ba2addr(ni, ni->mgmt_start_ba));
  2140. + nmbm_mark_block_color_mgmt(ni, ni->mgmt_start_ba,
  2141. + ni->signature_ba - 1);
  2142. +
  2143. + /* Look for info table(s) */
  2144. + success = nmbm_load_info_table(ni, ni->mgmt_start_ba,
  2145. + ni->signature_ba);
  2146. + if (success) {
  2147. + nlog_info(ni, "NMBM has been successfully attached %s\n",
  2148. + (ni->lower.flags & NMBM_F_READ_ONLY) ? "in read-only mode" : "");
  2149. + return true;
  2150. + }
  2151. +
  2152. + if (!(ni->lower.flags & NMBM_F_CREATE))
  2153. + return false;
  2154. +
  2155. + /* Fill block state table & mapping table */
  2156. + nmbm_scan_badblocks(ni);
  2157. + nmbm_build_mapping_table(ni);
  2158. +
  2159. + if (ni->lower.flags & NMBM_F_READ_ONLY) {
  2160. + nlog_info(ni, "NMBM has been initialized in read-only mode\n");
  2161. + return true;
  2162. + }
  2163. +
  2164. + /* Write info table(s) */
  2165. + success = nmbm_create_info_table(ni);
  2166. + if (success) {
  2167. + nlog_info(ni, "NMBM has been successfully created\n");
  2168. + return true;
  2169. + }
  2170. +
  2171. + return false;
  2172. +}
  2173. +
  2174. +/*
  2175. + * nmbm_find_signature - Find signature in the lower NAND chip
  2176. + * @ni: NMBM instance structure
  2177. + * @signature_ba: used for storing block address of the signature
  2178. + * @signature_ba: return the actual block address of signature block
  2179. + *
  2180. + * Find a valid signature from a specific range in the lower NAND chip,
  2181. + * from bottom (highest address) to top (lowest address)
  2182. + *
  2183. + * Return true if found.
  2184. + */
  2185. +static bool nmbm_find_signature(struct nmbm_instance *ni,
  2186. + struct nmbm_signature *signature,
  2187. + uint32_t *signature_ba)
  2188. +{
  2189. + struct nmbm_signature sig;
  2190. + uint64_t off, addr;
  2191. + uint32_t block_count, ba, limit;
  2192. + bool success;
  2193. + int ret;
  2194. +
  2195. + /* Calculate top and bottom block address */
  2196. + block_count = ni->lower.size >> ni->erasesize_shift;
  2197. + ba = block_count;
  2198. + limit = (block_count / NMBM_MGMT_DIV) * (NMBM_MGMT_DIV - ni->lower.max_ratio);
  2199. + if (ni->lower.max_reserved_blocks && block_count - limit > ni->lower.max_reserved_blocks)
  2200. + limit = block_count - ni->lower.max_reserved_blocks;
  2201. +
  2202. + while (ba >= limit) {
  2203. + schedule();
  2204. +
  2205. + ba--;
  2206. + addr = ba2addr(ni, ba);
  2207. +
  2208. + if (nmbm_check_bad_phys_block(ni, ba))
  2209. + continue;
  2210. +
  2211. + /* Check every page.
  2212. + * As long as at leaset one page contains valid signature,
  2213. + * the block is treated as a valid signature block.
  2214. + */
  2215. + for (off = 0; off < ni->lower.erasesize;
  2216. + off += ni->lower.writesize) {
  2217. + schedule();
  2218. +
  2219. + ret = nmbn_read_data(ni, addr + off, &sig,
  2220. + sizeof(sig));
  2221. + if (ret)
  2222. + continue;
  2223. +
  2224. + /* Check for header size and checksum */
  2225. + success = nmbm_check_header(&sig, sizeof(sig));
  2226. + if (!success)
  2227. + continue;
  2228. +
  2229. + /* Check for header magic */
  2230. + if (sig.header.magic == NMBM_MAGIC_SIGNATURE) {
  2231. + /* Found it */
  2232. + memcpy(signature, &sig, sizeof(sig));
  2233. + *signature_ba = ba;
  2234. + return true;
  2235. + }
  2236. + }
  2237. + };
  2238. +
  2239. + return false;
  2240. +}
  2241. +
  2242. +/*
  2243. + * is_power_of_2_u64 - Check whether a 64-bit integer is power of 2
  2244. + * @n: number to check
  2245. + */
  2246. +static bool is_power_of_2_u64(uint64_t n)
  2247. +{
  2248. + return (n != 0 && ((n & (n - 1)) == 0));
  2249. +}
  2250. +
  2251. +/*
  2252. + * nmbm_check_lower_members - Validate the members of lower NAND device
  2253. + * @nld: Lower NAND chip structure
  2254. + */
  2255. +static bool nmbm_check_lower_members(struct nmbm_lower_device *nld)
  2256. +{
  2257. +
  2258. + if (!nld->size || !is_power_of_2_u64(nld->size)) {
  2259. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2260. + "Chip size %llu is not valid\n", nld->size);
  2261. + return false;
  2262. + }
  2263. +
  2264. + if (!nld->erasesize || !is_power_of_2(nld->erasesize)) {
  2265. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2266. + "Block size %u is not valid\n", nld->erasesize);
  2267. + return false;
  2268. + }
  2269. +
  2270. + if (!nld->writesize || !is_power_of_2(nld->writesize)) {
  2271. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2272. + "Page size %u is not valid\n", nld->writesize);
  2273. + return false;
  2274. + }
  2275. +
  2276. + if (!nld->oobsize || !is_power_of_2(nld->oobsize)) {
  2277. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2278. + "Page spare size %u is not valid\n", nld->oobsize);
  2279. + return false;
  2280. + }
  2281. +
  2282. + if (!nld->read_page) {
  2283. + nmbm_log_lower(nld, NMBM_LOG_ERR, "read_page() is required\n");
  2284. + return false;
  2285. + }
  2286. +
  2287. + if (!(nld->flags & NMBM_F_READ_ONLY) && (!nld->write_page || !nld->erase_block)) {
  2288. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2289. + "write_page() and erase_block() are required\n");
  2290. + return false;
  2291. + }
  2292. +
  2293. + /* Data sanity check */
  2294. + if (!nld->max_ratio)
  2295. + nld->max_ratio = 1;
  2296. +
  2297. + if (nld->max_ratio >= NMBM_MGMT_DIV - 1) {
  2298. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2299. + "max ratio %u is invalid\n", nld->max_ratio);
  2300. + return false;
  2301. + }
  2302. +
  2303. + if (nld->max_reserved_blocks && nld->max_reserved_blocks < NMBM_MGMT_BLOCKS_MIN) {
  2304. + nmbm_log_lower(nld, NMBM_LOG_ERR,
  2305. + "max reserved blocks %u is too small\n", nld->max_reserved_blocks);
  2306. + return false;
  2307. + }
  2308. +
  2309. + return true;
  2310. +}
  2311. +
  2312. +/*
  2313. + * nmbm_calc_structure_size - Calculate the instance structure size
  2314. + * @nld: NMBM lower device structure
  2315. + */
  2316. +size_t nmbm_calc_structure_size(struct nmbm_lower_device *nld)
  2317. +{
  2318. + uint32_t state_table_size, mapping_table_size, info_table_size;
  2319. + uint32_t block_count;
  2320. +
  2321. + block_count = nmbm_lldiv(nld->size, nld->erasesize);
  2322. +
  2323. + /* Calculate info table size */
  2324. + state_table_size = ((block_count + NMBM_BITMAP_BLOCKS_PER_UNIT - 1) /
  2325. + NMBM_BITMAP_BLOCKS_PER_UNIT) * NMBM_BITMAP_UNIT_SIZE;
  2326. + mapping_table_size = block_count * sizeof(int32_t);
  2327. +
  2328. + info_table_size = NMBM_ALIGN(sizeof(struct nmbm_info_table_header),
  2329. + nld->writesize);
  2330. + info_table_size += NMBM_ALIGN(state_table_size, nld->writesize);
  2331. + info_table_size += NMBM_ALIGN(mapping_table_size, nld->writesize);
  2332. +
  2333. + return info_table_size + state_table_size + mapping_table_size +
  2334. + nld->writesize + nld->oobsize + sizeof(struct nmbm_instance);
  2335. +}
  2336. +
  2337. +/*
  2338. + * nmbm_init_structure - Initialize members of instance structure
  2339. + * @ni: NMBM instance structure
  2340. + */
  2341. +static void nmbm_init_structure(struct nmbm_instance *ni)
  2342. +{
  2343. + uint32_t pages_per_block, blocks_per_chip;
  2344. + uintptr_t ptr;
  2345. +
  2346. + pages_per_block = ni->lower.erasesize / ni->lower.writesize;
  2347. + blocks_per_chip = nmbm_lldiv(ni->lower.size, ni->lower.erasesize);
  2348. +
  2349. + ni->rawpage_size = ni->lower.writesize + ni->lower.oobsize;
  2350. + ni->rawblock_size = pages_per_block * ni->rawpage_size;
  2351. + ni->rawchip_size = blocks_per_chip * ni->rawblock_size;
  2352. +
  2353. + ni->writesize_mask = ni->lower.writesize - 1;
  2354. + ni->erasesize_mask = ni->lower.erasesize - 1;
  2355. +
  2356. + ni->writesize_shift = ffs(ni->lower.writesize) - 1;
  2357. + ni->erasesize_shift = ffs(ni->lower.erasesize) - 1;
  2358. +
  2359. + /* Calculate number of block this chip */
  2360. + ni->block_count = ni->lower.size >> ni->erasesize_shift;
  2361. +
  2362. + /* Calculate info table size */
  2363. + ni->state_table_size = ((ni->block_count + NMBM_BITMAP_BLOCKS_PER_UNIT - 1) /
  2364. + NMBM_BITMAP_BLOCKS_PER_UNIT) * NMBM_BITMAP_UNIT_SIZE;
  2365. + ni->mapping_table_size = ni->block_count * sizeof(*ni->block_mapping);
  2366. +
  2367. + ni->info_table_size = NMBM_ALIGN(sizeof(ni->info_table),
  2368. + ni->lower.writesize);
  2369. + ni->info_table.state_table_off = ni->info_table_size;
  2370. +
  2371. + ni->info_table_size += NMBM_ALIGN(ni->state_table_size,
  2372. + ni->lower.writesize);
  2373. + ni->info_table.mapping_table_off = ni->info_table_size;
  2374. +
  2375. + ni->info_table_size += NMBM_ALIGN(ni->mapping_table_size,
  2376. + ni->lower.writesize);
  2377. +
  2378. + ni->info_table_spare_blocks = nmbm_get_spare_block_count(
  2379. + size2blk(ni, ni->info_table_size));
  2380. +
  2381. + /* Assign memory to members */
  2382. + ptr = (uintptr_t)ni + sizeof(*ni);
  2383. +
  2384. + ni->info_table_cache = (void *)ptr;
  2385. + ptr += ni->info_table_size;
  2386. +
  2387. + ni->block_state = (void *)ptr;
  2388. + ptr += ni->state_table_size;
  2389. +
  2390. + ni->block_mapping = (void *)ptr;
  2391. + ptr += ni->mapping_table_size;
  2392. +
  2393. + ni->page_cache = (uint8_t *)ptr;
  2394. +
  2395. + /* Initialize block state table */
  2396. + ni->block_state_changed = 0;
  2397. + memset(ni->block_state, 0xff, ni->state_table_size);
  2398. +
  2399. + /* Initialize block mapping table */
  2400. + ni->block_mapping_changed = 0;
  2401. +}
  2402. +
  2403. +/*
  2404. + * nmbm_attach - Attach to a lower device
  2405. + * @nld: NMBM lower device structure
  2406. + * @ni: NMBM instance structure
  2407. + */
  2408. +int nmbm_attach(struct nmbm_lower_device *nld, struct nmbm_instance *ni)
  2409. +{
  2410. + bool success;
  2411. +
  2412. + if (!nld || !ni)
  2413. + return -EINVAL;
  2414. +
  2415. + /* Set default log level */
  2416. + ni->log_display_level = NMBM_DEFAULT_LOG_LEVEL;
  2417. +
  2418. + /* Check lower members */
  2419. + success = nmbm_check_lower_members(nld);
  2420. + if (!success)
  2421. + return -EINVAL;
  2422. +
  2423. + /* Initialize NMBM instance */
  2424. + memcpy(&ni->lower, nld, sizeof(struct nmbm_lower_device));
  2425. + nmbm_init_structure(ni);
  2426. +
  2427. + success = nmbm_find_signature(ni, &ni->signature, &ni->signature_ba);
  2428. + if (!success) {
  2429. + if (!(nld->flags & NMBM_F_CREATE)) {
  2430. + nlog_err(ni, "Signature not found\n");
  2431. + return -ENODEV;
  2432. + }
  2433. +
  2434. + success = nmbm_create_new(ni);
  2435. + if (!success)
  2436. + return -ENODEV;
  2437. +
  2438. + return 0;
  2439. + }
  2440. +
  2441. + nlog_info(ni, "Signature found at block %u [0x%08llx]\n",
  2442. + ni->signature_ba, ba2addr(ni, ni->signature_ba));
  2443. + nmbm_mark_block_color_signature(ni, ni->signature_ba);
  2444. +
  2445. + if (ni->signature.header.version != NMBM_VER) {
  2446. + nlog_err(ni, "NMBM version %u.%u is not supported\n",
  2447. + NMBM_VERSION_MAJOR_GET(ni->signature.header.version),
  2448. + NMBM_VERSION_MINOR_GET(ni->signature.header.version));
  2449. + return -EINVAL;
  2450. + }
  2451. +
  2452. + if (ni->signature.nand_size != nld->size ||
  2453. + ni->signature.block_size != nld->erasesize ||
  2454. + ni->signature.page_size != nld->writesize ||
  2455. + ni->signature.spare_size != nld->oobsize) {
  2456. + nlog_err(ni, "NMBM configuration mismatch\n");
  2457. + return -EINVAL;
  2458. + }
  2459. +
  2460. + success = nmbm_load_existing(ni);
  2461. + if (!success)
  2462. + return -ENODEV;
  2463. +
  2464. + return 0;
  2465. +}
  2466. +
  2467. +/*
  2468. + * nmbm_detach - Detach from a lower device, and save all tables
  2469. + * @ni: NMBM instance structure
  2470. + */
  2471. +int nmbm_detach(struct nmbm_instance *ni)
  2472. +{
  2473. + if (!ni)
  2474. + return -EINVAL;
  2475. +
  2476. + if (!(ni->lower.flags & NMBM_F_READ_ONLY))
  2477. + nmbm_update_info_table(ni);
  2478. +
  2479. + nmbm_mark_block_color_normal(ni, 0, ni->block_count - 1);
  2480. +
  2481. + return 0;
  2482. +}
  2483. +
  2484. +/*
  2485. + * nmbm_erase_logic_block - Erase a logic block
  2486. + * @ni: NMBM instance structure
  2487. + * @nmbm_erase_logic_block: logic block address
  2488. + *
  2489. + * Logic block will be mapped to physical block before erasing.
  2490. + * Bad block found during erasinh will be remapped to a good block if there is
  2491. + * still at least one good spare block available.
  2492. + */
  2493. +static int nmbm_erase_logic_block(struct nmbm_instance *ni, uint32_t block_addr)
  2494. +{
  2495. + uint32_t pb;
  2496. + bool success;
  2497. +
  2498. +retry:
  2499. + /* Map logic block to physical block */
  2500. + pb = ni->block_mapping[block_addr];
  2501. +
  2502. + /* Whether the logic block is good (has valid mapping) */
  2503. + if ((int32_t)pb < 0) {
  2504. + nlog_debug(ni, "Logic block %u is a bad block\n", block_addr);
  2505. + return -EIO;
  2506. + }
  2507. +
  2508. + /* Remap logic block if current physical block is a bad block */
  2509. + if (nmbm_get_block_state(ni, pb) == BLOCK_ST_BAD ||
  2510. + nmbm_get_block_state(ni, pb) == BLOCK_ST_NEED_REMAP)
  2511. + goto remap_logic_block;
  2512. +
  2513. + /* Insurance to detect unexpected bad block marked by user */
  2514. + if (nmbm_check_bad_phys_block(ni, pb)) {
  2515. + nlog_warn(ni, "Found unexpected bad block possibly marked by user\n");
  2516. + nmbm_set_block_state(ni, pb, BLOCK_ST_BAD);
  2517. + goto remap_logic_block;
  2518. + }
  2519. +
  2520. + success = nmbm_erase_block_and_check(ni, pb);
  2521. + if (success)
  2522. + return 0;
  2523. +
  2524. + /* Mark bad block */
  2525. + nmbm_mark_phys_bad_block(ni, pb);
  2526. + nmbm_set_block_state(ni, pb, BLOCK_ST_BAD);
  2527. +
  2528. +remap_logic_block:
  2529. + /* Try to assign a new block */
  2530. + success = nmbm_map_block(ni, block_addr);
  2531. + if (!success) {
  2532. + /* Mark logic block unusable, and update info table */
  2533. + ni->block_mapping[block_addr] = -1;
  2534. + if (nmbm_get_block_state(ni, pb) != BLOCK_ST_NEED_REMAP)
  2535. + nmbm_set_block_state(ni, pb, BLOCK_ST_BAD);
  2536. + nmbm_update_info_table(ni);
  2537. + return -EIO;
  2538. + }
  2539. +
  2540. + /* Update info table before erasing */
  2541. + if (nmbm_get_block_state(ni, pb) != BLOCK_ST_NEED_REMAP)
  2542. + nmbm_set_block_state(ni, pb, BLOCK_ST_BAD);
  2543. + nmbm_update_info_table(ni);
  2544. +
  2545. + goto retry;
  2546. +}
  2547. +
  2548. +/*
  2549. + * nmbm_erase_block_range - Erase logic blocks
  2550. + * @ni: NMBM instance structure
  2551. + * @addr: logic linear address
  2552. + * @size: erase range
  2553. + * @failed_addr: return failed block address if error occurs
  2554. + */
  2555. +int nmbm_erase_block_range(struct nmbm_instance *ni, uint64_t addr,
  2556. + uint64_t size, uint64_t *failed_addr)
  2557. +{
  2558. + uint32_t start_ba, end_ba;
  2559. + int ret;
  2560. +
  2561. + if (!ni)
  2562. + return -EINVAL;
  2563. +
  2564. + /* Sanity check */
  2565. + if (ni->protected || (ni->lower.flags & NMBM_F_READ_ONLY)) {
  2566. + nlog_debug(ni, "Device is forced read-only\n");
  2567. + return -EROFS;
  2568. + }
  2569. +
  2570. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2571. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2572. + return -EINVAL;
  2573. + }
  2574. +
  2575. + if (addr + size > ba2addr(ni, ni->data_block_count)) {
  2576. + nlog_err(ni, "Erase range 0xllxu is too large\n", size);
  2577. + return -EINVAL;
  2578. + }
  2579. +
  2580. + if (!size) {
  2581. + nlog_warn(ni, "No blocks to be erased\n");
  2582. + return 0;
  2583. + }
  2584. +
  2585. + start_ba = addr2ba(ni, addr);
  2586. + end_ba = addr2ba(ni, addr + size - 1);
  2587. +
  2588. + while (start_ba <= end_ba) {
  2589. + schedule();
  2590. +
  2591. + ret = nmbm_erase_logic_block(ni, start_ba);
  2592. + if (ret) {
  2593. + if (failed_addr)
  2594. + *failed_addr = ba2addr(ni, start_ba);
  2595. + return ret;
  2596. + }
  2597. +
  2598. + start_ba++;
  2599. + }
  2600. +
  2601. + return 0;
  2602. +}
  2603. +
  2604. +/*
  2605. + * nmbm_read_logic_page - Read page based on logic address
  2606. + * @ni: NMBM instance structure
  2607. + * @addr: logic linear address
  2608. + * @data: buffer to store main data. optional.
  2609. + * @oob: buffer to store oob data. optional.
  2610. + * @mode: read mode
  2611. + *
  2612. + * Return 0 for success, positive value for corrected bitflip count,
  2613. + * -EBADMSG for ecc error, other negative values for other errors
  2614. + */
  2615. +static int nmbm_read_logic_page(struct nmbm_instance *ni, uint64_t addr,
  2616. + void *data, void *oob, enum nmbm_oob_mode mode)
  2617. +{
  2618. + uint32_t lb, pb, offset;
  2619. + uint64_t paddr;
  2620. +
  2621. + /* Extract block address and in-block offset */
  2622. + lb = addr2ba(ni, addr);
  2623. + offset = addr & ni->erasesize_mask;
  2624. +
  2625. + /* Map logic block to physical block */
  2626. + pb = ni->block_mapping[lb];
  2627. +
  2628. + /* Whether the logic block is good (has valid mapping) */
  2629. + if ((int32_t)pb < 0) {
  2630. + nlog_debug(ni, "Logic block %u is a bad block\n", lb);
  2631. + return -EIO;
  2632. + }
  2633. +
  2634. + /* Fail if physical block is marked bad */
  2635. + if (nmbm_get_block_state(ni, pb) == BLOCK_ST_BAD)
  2636. + return -EIO;
  2637. +
  2638. + /* Assemble new address */
  2639. + paddr = ba2addr(ni, pb) + offset;
  2640. +
  2641. + return nmbm_read_phys_page(ni, paddr, data, oob, mode);
  2642. +}
  2643. +
  2644. +/*
  2645. + * nmbm_read_single_page - Read one page based on logic address
  2646. + * @ni: NMBM instance structure
  2647. + * @addr: logic linear address
  2648. + * @data: buffer to store main data. optional.
  2649. + * @oob: buffer to store oob data. optional.
  2650. + * @mode: read mode
  2651. + *
  2652. + * Return 0 for success, positive value for corrected bitflip count,
  2653. + * -EBADMSG for ecc error, other negative values for other errors
  2654. + */
  2655. +int nmbm_read_single_page(struct nmbm_instance *ni, uint64_t addr, void *data,
  2656. + void *oob, enum nmbm_oob_mode mode)
  2657. +{
  2658. + if (!ni)
  2659. + return -EINVAL;
  2660. +
  2661. + /* Sanity check */
  2662. + if (ni->protected) {
  2663. + nlog_debug(ni, "Device is forced read-only\n");
  2664. + return -EROFS;
  2665. + }
  2666. +
  2667. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2668. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2669. + return -EINVAL;
  2670. + }
  2671. +
  2672. + return nmbm_read_logic_page(ni, addr, data, oob, mode);
  2673. +}
  2674. +
  2675. +/*
  2676. + * nmbm_read_range - Read data without oob
  2677. + * @ni: NMBM instance structure
  2678. + * @addr: logic linear address
  2679. + * @size: data size to read
  2680. + * @data: buffer to store main data to be read
  2681. + * @mode: read mode
  2682. + * @retlen: return actual data size read
  2683. + *
  2684. + * Return 0 for success, positive value for corrected bitflip count,
  2685. + * -EBADMSG for ecc error, other negative values for other errors
  2686. + */
  2687. +int nmbm_read_range(struct nmbm_instance *ni, uint64_t addr, size_t size,
  2688. + void *data, enum nmbm_oob_mode mode, size_t *retlen)
  2689. +{
  2690. + uint64_t off = addr;
  2691. + uint8_t *ptr = data;
  2692. + size_t sizeremain = size, chunksize, leading;
  2693. + bool has_ecc_err = false;
  2694. + int ret, max_bitflips = 0;
  2695. +
  2696. + if (!ni)
  2697. + return -EINVAL;
  2698. +
  2699. + /* Sanity check */
  2700. + if (ni->protected) {
  2701. + nlog_debug(ni, "Device is forced read-only\n");
  2702. + return -EROFS;
  2703. + }
  2704. +
  2705. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2706. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2707. + return -EINVAL;
  2708. + }
  2709. +
  2710. + if (addr + size > ba2addr(ni, ni->data_block_count)) {
  2711. + nlog_err(ni, "Read range 0x%llx is too large\n", size);
  2712. + return -EINVAL;
  2713. + }
  2714. +
  2715. + if (!size) {
  2716. + nlog_warn(ni, "No data to be read\n");
  2717. + return 0;
  2718. + }
  2719. +
  2720. + while (sizeremain) {
  2721. + schedule();
  2722. +
  2723. + leading = off & ni->writesize_mask;
  2724. + chunksize = ni->lower.writesize - leading;
  2725. + if (chunksize > sizeremain)
  2726. + chunksize = sizeremain;
  2727. +
  2728. + if (chunksize == ni->lower.writesize) {
  2729. + ret = nmbm_read_logic_page(ni, off - leading, ptr,
  2730. + NULL, mode);
  2731. + if (ret < 0 && ret != -EBADMSG)
  2732. + break;
  2733. + } else {
  2734. + ret = nmbm_read_logic_page(ni, off - leading,
  2735. + ni->page_cache, NULL,
  2736. + mode);
  2737. + if (ret < 0 && ret != -EBADMSG)
  2738. + break;
  2739. +
  2740. + memcpy(ptr, ni->page_cache + leading, chunksize);
  2741. + }
  2742. +
  2743. + if (ret == -EBADMSG)
  2744. + has_ecc_err = true;
  2745. +
  2746. + if (ret > max_bitflips)
  2747. + max_bitflips = ret;
  2748. +
  2749. + off += chunksize;
  2750. + ptr += chunksize;
  2751. + sizeremain -= chunksize;
  2752. + }
  2753. +
  2754. + if (retlen)
  2755. + *retlen = size - sizeremain;
  2756. +
  2757. + if (ret < 0 && ret != -EBADMSG)
  2758. + return ret;
  2759. +
  2760. + if (has_ecc_err)
  2761. + return -EBADMSG;
  2762. +
  2763. + return max_bitflips;
  2764. +}
  2765. +
  2766. +/*
  2767. + * nmbm_write_logic_page - Read page based on logic address
  2768. + * @ni: NMBM instance structure
  2769. + * @addr: logic linear address
  2770. + * @data: buffer contains main data. optional.
  2771. + * @oob: buffer contains oob data. optional.
  2772. + * @mode: write mode
  2773. + */
  2774. +static int nmbm_write_logic_page(struct nmbm_instance *ni, uint64_t addr,
  2775. + const void *data, const void *oob,
  2776. + enum nmbm_oob_mode mode)
  2777. +{
  2778. + uint32_t lb, pb, offset;
  2779. + uint64_t paddr;
  2780. + bool success;
  2781. +
  2782. + /* Extract block address and in-block offset */
  2783. + lb = addr2ba(ni, addr);
  2784. + offset = addr & ni->erasesize_mask;
  2785. +
  2786. + /* Map logic block to physical block */
  2787. + pb = ni->block_mapping[lb];
  2788. +
  2789. + /* Whether the logic block is good (has valid mapping) */
  2790. + if ((int32_t)pb < 0) {
  2791. + nlog_debug(ni, "Logic block %u is a bad block\n", lb);
  2792. + return -EIO;
  2793. + }
  2794. +
  2795. + /* Fail if physical block is marked bad */
  2796. + if (nmbm_get_block_state(ni, pb) == BLOCK_ST_BAD)
  2797. + return -EIO;
  2798. +
  2799. + /* Assemble new address */
  2800. + paddr = ba2addr(ni, pb) + offset;
  2801. +
  2802. + success = nmbm_write_phys_page(ni, paddr, data, oob, mode);
  2803. + if (success)
  2804. + return 0;
  2805. +
  2806. + /*
  2807. + * Do not remap bad block here. Just mark this block in state table.
  2808. + * Remap this block on erasing.
  2809. + */
  2810. + nmbm_set_block_state(ni, pb, BLOCK_ST_NEED_REMAP);
  2811. + nmbm_update_info_table(ni);
  2812. +
  2813. + return -EIO;
  2814. +}
  2815. +
  2816. +/*
  2817. + * nmbm_write_single_page - Write one page based on logic address
  2818. + * @ni: NMBM instance structure
  2819. + * @addr: logic linear address
  2820. + * @data: buffer contains main data. optional.
  2821. + * @oob: buffer contains oob data. optional.
  2822. + * @mode: write mode
  2823. + */
  2824. +int nmbm_write_single_page(struct nmbm_instance *ni, uint64_t addr,
  2825. + const void *data, const void *oob,
  2826. + enum nmbm_oob_mode mode)
  2827. +{
  2828. + if (!ni)
  2829. + return -EINVAL;
  2830. +
  2831. + /* Sanity check */
  2832. + if (ni->protected || (ni->lower.flags & NMBM_F_READ_ONLY)) {
  2833. + nlog_debug(ni, "Device is forced read-only\n");
  2834. + return -EROFS;
  2835. + }
  2836. +
  2837. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2838. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2839. + return -EINVAL;
  2840. + }
  2841. +
  2842. + return nmbm_write_logic_page(ni, addr, data, oob, mode);
  2843. +}
  2844. +
  2845. +/*
  2846. + * nmbm_write_range - Write data without oob
  2847. + * @ni: NMBM instance structure
  2848. + * @addr: logic linear address
  2849. + * @size: data size to write
  2850. + * @data: buffer contains data to be written
  2851. + * @mode: write mode
  2852. + * @retlen: return actual data size written
  2853. + */
  2854. +int nmbm_write_range(struct nmbm_instance *ni, uint64_t addr, size_t size,
  2855. + const void *data, enum nmbm_oob_mode mode,
  2856. + size_t *retlen)
  2857. +{
  2858. + uint64_t off = addr;
  2859. + const uint8_t *ptr = data;
  2860. + size_t sizeremain = size, chunksize, leading;
  2861. + int ret;
  2862. +
  2863. + if (!ni)
  2864. + return -EINVAL;
  2865. +
  2866. + /* Sanity check */
  2867. + if (ni->protected || (ni->lower.flags & NMBM_F_READ_ONLY)) {
  2868. + nlog_debug(ni, "Device is forced read-only\n");
  2869. + return -EROFS;
  2870. + }
  2871. +
  2872. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2873. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2874. + return -EINVAL;
  2875. + }
  2876. +
  2877. + if (addr + size > ba2addr(ni, ni->data_block_count)) {
  2878. + nlog_err(ni, "Write size 0x%zx is too large\n", size);
  2879. + return -EINVAL;
  2880. + }
  2881. +
  2882. + if (!size) {
  2883. + nlog_warn(ni, "No data to be written\n");
  2884. + return 0;
  2885. + }
  2886. +
  2887. + while (sizeremain) {
  2888. + schedule();
  2889. +
  2890. + leading = off & ni->writesize_mask;
  2891. + chunksize = ni->lower.writesize - leading;
  2892. + if (chunksize > sizeremain)
  2893. + chunksize = sizeremain;
  2894. +
  2895. + if (chunksize == ni->lower.writesize) {
  2896. + ret = nmbm_write_logic_page(ni, off - leading, ptr,
  2897. + NULL, mode);
  2898. + if (ret)
  2899. + break;
  2900. + } else {
  2901. + memset(ni->page_cache, 0xff, leading);
  2902. + memcpy(ni->page_cache + leading, ptr, chunksize);
  2903. +
  2904. + ret = nmbm_write_logic_page(ni, off - leading,
  2905. + ni->page_cache, NULL,
  2906. + mode);
  2907. + if (ret)
  2908. + break;
  2909. + }
  2910. +
  2911. + off += chunksize;
  2912. + ptr += chunksize;
  2913. + sizeremain -= chunksize;
  2914. + }
  2915. +
  2916. + if (retlen)
  2917. + *retlen = size - sizeremain;
  2918. +
  2919. + return ret;
  2920. +}
  2921. +
  2922. +/*
  2923. + * nmbm_check_bad_block - Check whether a logic block is usable
  2924. + * @ni: NMBM instance structure
  2925. + * @addr: logic linear address
  2926. + */
  2927. +int nmbm_check_bad_block(struct nmbm_instance *ni, uint64_t addr)
  2928. +{
  2929. + uint32_t lb, pb;
  2930. +
  2931. + if (!ni)
  2932. + return -EINVAL;
  2933. +
  2934. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2935. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2936. + return -EINVAL;
  2937. + }
  2938. +
  2939. + lb = addr2ba(ni, addr);
  2940. +
  2941. + /* Map logic block to physical block */
  2942. + pb = ni->block_mapping[lb];
  2943. +
  2944. + if ((int32_t)pb < 0)
  2945. + return 1;
  2946. +
  2947. + if (nmbm_get_block_state(ni, pb) == BLOCK_ST_BAD)
  2948. + return 1;
  2949. +
  2950. + return 0;
  2951. +}
  2952. +
  2953. +/*
  2954. + * nmbm_mark_bad_block - Mark a logic block unusable
  2955. + * @ni: NMBM instance structure
  2956. + * @addr: logic linear address
  2957. + */
  2958. +int nmbm_mark_bad_block(struct nmbm_instance *ni, uint64_t addr)
  2959. +{
  2960. + uint32_t lb, pb;
  2961. +
  2962. + if (!ni)
  2963. + return -EINVAL;
  2964. +
  2965. + /* Sanity check */
  2966. + if (ni->protected || (ni->lower.flags & NMBM_F_READ_ONLY)) {
  2967. + nlog_debug(ni, "Device is forced read-only\n");
  2968. + return -EROFS;
  2969. + }
  2970. +
  2971. + if (addr >= ba2addr(ni, ni->data_block_count)) {
  2972. + nlog_err(ni, "Address 0x%llx is invalid\n", addr);
  2973. + return -EINVAL;
  2974. + }
  2975. +
  2976. + lb = addr2ba(ni, addr);
  2977. +
  2978. + /* Map logic block to physical block */
  2979. + pb = ni->block_mapping[lb];
  2980. +
  2981. + if ((int32_t)pb < 0)
  2982. + return 0;
  2983. +
  2984. + ni->block_mapping[lb] = -1;
  2985. + nmbm_mark_phys_bad_block(ni, pb);
  2986. + nmbm_set_block_state(ni, pb, BLOCK_ST_BAD);
  2987. + nmbm_update_info_table(ni);
  2988. +
  2989. + return 0;
  2990. +}
  2991. +
  2992. +/*
  2993. + * nmbm_get_avail_size - Get available user data size
  2994. + * @ni: NMBM instance structure
  2995. + */
  2996. +uint64_t nmbm_get_avail_size(struct nmbm_instance *ni)
  2997. +{
  2998. + if (!ni)
  2999. + return 0;
  3000. +
  3001. + return (uint64_t)ni->data_block_count << ni->erasesize_shift;
  3002. +}
  3003. +
  3004. +/*
  3005. + * nmbm_get_lower_device - Get lower device structure
  3006. + * @ni: NMBM instance structure
  3007. + * @nld: pointer to hold the data of lower device structure
  3008. + */
  3009. +int nmbm_get_lower_device(struct nmbm_instance *ni, struct nmbm_lower_device *nld)
  3010. +{
  3011. + if (!ni)
  3012. + return -EINVAL;
  3013. +
  3014. + if (nld)
  3015. + memcpy(nld, &ni->lower, sizeof(*nld));
  3016. +
  3017. + return 0;
  3018. +}
  3019. +
  3020. +#include "nmbm-debug.inl"
  3021. --- /dev/null
  3022. +++ b/drivers/mtd/nmbm/nmbm-debug.h
  3023. @@ -0,0 +1,37 @@
  3024. +/* SPDX-License-Identifier: GPL-2.0 */
  3025. +/*
  3026. + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
  3027. + *
  3028. + * Debug addons for NAND Mapped-block Management (NMBM)
  3029. + *
  3030. + * Author: Weijie Gao <[email protected]>
  3031. + */
  3032. +
  3033. +#ifndef _NMBM_DEBUG_H_
  3034. +#define _NMBM_DEBUG_H_
  3035. +
  3036. +#include "nmbm-private.h"
  3037. +
  3038. +#define nmbm_mark_block_color_normal(ni, start_ba, end_ba)
  3039. +#define nmbm_mark_block_color_bad(ni, ba)
  3040. +#define nmbm_mark_block_color_mgmt(ni, start_ba, end_ba)
  3041. +#define nmbm_mark_block_color_signature(ni, ba)
  3042. +#define nmbm_mark_block_color_info_table(ni, start_ba, end_ba)
  3043. +#define nmbm_mark_block_color_mapped(ni, ba)
  3044. +
  3045. +uint32_t nmbm_debug_get_block_state(struct nmbm_instance *ni, uint32_t ba);
  3046. +char nmbm_debug_get_phys_block_type(struct nmbm_instance *ni, uint32_t ba);
  3047. +
  3048. +enum nmmb_block_type {
  3049. + NMBM_BLOCK_GOOD_DATA,
  3050. + NMBM_BLOCK_GOOD_MGMT,
  3051. + NMBM_BLOCK_BAD,
  3052. + NMBM_BLOCK_MAIN_INFO_TABLE,
  3053. + NMBM_BLOCK_BACKUP_INFO_TABLE,
  3054. + NMBM_BLOCK_REMAPPED,
  3055. + NMBM_BLOCK_SIGNATURE,
  3056. +
  3057. + __NMBM_BLOCK_TYPE_MAX
  3058. +};
  3059. +
  3060. +#endif /* _NMBM_DEBUG_H_ */
  3061. --- /dev/null
  3062. +++ b/drivers/mtd/nmbm/nmbm-debug.inl
  3063. @@ -0,0 +1,39 @@
  3064. +
  3065. +uint32_t nmbm_debug_get_block_state(struct nmbm_instance *ni, uint32_t ba)
  3066. +{
  3067. + return nmbm_get_block_state(ni, ba);
  3068. +}
  3069. +
  3070. +char nmbm_debug_get_phys_block_type(struct nmbm_instance *ni, uint32_t ba)
  3071. +{
  3072. + uint32_t eba, limit;
  3073. + bool success;
  3074. +
  3075. + if (nmbm_get_block_state(ni, ba) == BLOCK_ST_BAD)
  3076. + return NMBM_BLOCK_BAD;
  3077. +
  3078. + if (ba < ni->data_block_count)
  3079. + return NMBM_BLOCK_GOOD_DATA;
  3080. +
  3081. + if (ba == ni->signature_ba)
  3082. + return NMBM_BLOCK_SIGNATURE;
  3083. +
  3084. + if (ni->main_table_ba) {
  3085. + limit = ni->backup_table_ba ? ni->backup_table_ba :
  3086. + ni->mapping_blocks_ba;
  3087. +
  3088. + success = nmbm_block_walk_asc(ni, ni->main_table_ba, &eba,
  3089. + size2blk(ni, ni->info_table_size), limit);
  3090. +
  3091. + if (success && ba >= ni->main_table_ba && ba < eba)
  3092. + return NMBM_BLOCK_MAIN_INFO_TABLE;
  3093. + }
  3094. +
  3095. + if (ba >= ni->backup_table_ba && ba < ni->mapping_blocks_ba)
  3096. + return NMBM_BLOCK_BACKUP_INFO_TABLE;
  3097. +
  3098. + if (ba > ni->mapping_blocks_top_ba && ba < ni->signature_ba)
  3099. + return NMBM_BLOCK_REMAPPED;
  3100. +
  3101. + return NMBM_BLOCK_GOOD_MGMT;
  3102. +}
  3103. --- /dev/null
  3104. +++ b/drivers/mtd/nmbm/nmbm-private.h
  3105. @@ -0,0 +1,137 @@
  3106. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
  3107. +/*
  3108. + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
  3109. + *
  3110. + * Definitions for NAND Mapped-block Management (NMBM)
  3111. + *
  3112. + * Author: Weijie Gao <[email protected]>
  3113. + */
  3114. +
  3115. +#ifndef _NMBM_PRIVATE_H_
  3116. +#define _NMBM_PRIVATE_H_
  3117. +
  3118. +#include <nmbm/nmbm.h>
  3119. +
  3120. +#define NMBM_MAGIC_SIGNATURE 0x304d4d4e /* NMM0 */
  3121. +#define NMBM_MAGIC_INFO_TABLE 0x314d4d4e /* NMM1 */
  3122. +
  3123. +#define NMBM_VERSION_MAJOR_S 0
  3124. +#define NMBM_VERSION_MAJOR_M 0xffff
  3125. +#define NMBM_VERSION_MINOR_S 16
  3126. +#define NMBM_VERSION_MINOR_M 0xffff
  3127. +#define NMBM_VERSION_MAKE(major, minor) (((major) & NMBM_VERSION_MAJOR_M) | \
  3128. + (((minor) & NMBM_VERSION_MINOR_M) << \
  3129. + NMBM_VERSION_MINOR_S))
  3130. +#define NMBM_VERSION_MAJOR_GET(ver) (((ver) >> NMBM_VERSION_MAJOR_S) & \
  3131. + NMBM_VERSION_MAJOR_M)
  3132. +#define NMBM_VERSION_MINOR_GET(ver) (((ver) >> NMBM_VERSION_MINOR_S) & \
  3133. + NMBM_VERSION_MINOR_M)
  3134. +
  3135. +typedef uint32_t nmbm_bitmap_t;
  3136. +#define NMBM_BITMAP_UNIT_SIZE (sizeof(nmbm_bitmap_t))
  3137. +#define NMBM_BITMAP_BITS_PER_BLOCK 2
  3138. +#define NMBM_BITMAP_BITS_PER_UNIT (8 * sizeof(nmbm_bitmap_t))
  3139. +#define NMBM_BITMAP_BLOCKS_PER_UNIT (NMBM_BITMAP_BITS_PER_UNIT / \
  3140. + NMBM_BITMAP_BITS_PER_BLOCK)
  3141. +
  3142. +#define NMBM_SPARE_BLOCK_MULTI 1
  3143. +#define NMBM_SPARE_BLOCK_DIV 2
  3144. +#define NMBM_SPARE_BLOCK_MIN 2
  3145. +
  3146. +#define NMBM_MGMT_DIV 16
  3147. +#define NMBM_MGMT_BLOCKS_MIN 32
  3148. +
  3149. +#define NMBM_TRY_COUNT 3
  3150. +
  3151. +#define BLOCK_ST_BAD 0
  3152. +#define BLOCK_ST_NEED_REMAP 2
  3153. +#define BLOCK_ST_GOOD 3
  3154. +#define BLOCK_ST_MASK 3
  3155. +
  3156. +struct nmbm_header {
  3157. + uint32_t magic;
  3158. + uint32_t version;
  3159. + uint32_t size;
  3160. + uint32_t checksum;
  3161. +};
  3162. +
  3163. +struct nmbm_signature {
  3164. + struct nmbm_header header;
  3165. + uint64_t nand_size;
  3166. + uint32_t block_size;
  3167. + uint32_t page_size;
  3168. + uint32_t spare_size;
  3169. + uint32_t mgmt_start_pb;
  3170. + uint8_t max_try_count;
  3171. + uint8_t padding[3];
  3172. +};
  3173. +
  3174. +struct nmbm_info_table_header {
  3175. + struct nmbm_header header;
  3176. + uint32_t write_count;
  3177. + uint32_t state_table_off;
  3178. + uint32_t mapping_table_off;
  3179. + uint32_t padding;
  3180. +};
  3181. +
  3182. +struct nmbm_instance {
  3183. + struct nmbm_lower_device lower;
  3184. +
  3185. + uint32_t rawpage_size;
  3186. + uint32_t rawblock_size;
  3187. + uint32_t rawchip_size;
  3188. +
  3189. + uint32_t writesize_mask;
  3190. + uint32_t erasesize_mask;
  3191. + uint16_t writesize_shift;
  3192. + uint16_t erasesize_shift;
  3193. +
  3194. + struct nmbm_signature signature;
  3195. +
  3196. + uint8_t *info_table_cache;
  3197. + uint32_t info_table_size;
  3198. + uint32_t info_table_spare_blocks;
  3199. + struct nmbm_info_table_header info_table;
  3200. +
  3201. + nmbm_bitmap_t *block_state;
  3202. + uint32_t block_state_changed;
  3203. + uint32_t state_table_size;
  3204. +
  3205. + int32_t *block_mapping;
  3206. + uint32_t block_mapping_changed;
  3207. + uint32_t mapping_table_size;
  3208. +
  3209. + uint8_t *page_cache;
  3210. +
  3211. + int protected;
  3212. +
  3213. + uint32_t block_count;
  3214. + uint32_t data_block_count;
  3215. +
  3216. + uint32_t mgmt_start_ba;
  3217. + uint32_t main_table_ba;
  3218. + uint32_t backup_table_ba;
  3219. + uint32_t mapping_blocks_ba;
  3220. + uint32_t mapping_blocks_top_ba;
  3221. + uint32_t signature_ba;
  3222. +
  3223. + enum nmbm_log_category log_display_level;
  3224. +};
  3225. +
  3226. +/* Log utilities */
  3227. +#define nlog_debug(ni, fmt, ...) \
  3228. + nmbm_log(ni, NMBM_LOG_DEBUG, fmt, ##__VA_ARGS__)
  3229. +
  3230. +#define nlog_info(ni, fmt, ...) \
  3231. + nmbm_log(ni, NMBM_LOG_INFO, fmt, ##__VA_ARGS__)
  3232. +
  3233. +#define nlog_warn(ni, fmt, ...) \
  3234. + nmbm_log(ni, NMBM_LOG_WARN, fmt, ##__VA_ARGS__)
  3235. +
  3236. +#define nlog_err(ni, fmt, ...) \
  3237. + nmbm_log(ni, NMBM_LOG_ERR, fmt, ##__VA_ARGS__)
  3238. +
  3239. +#define nlog_emerg(ni, fmt, ...) \
  3240. + nmbm_log(ni, NMBM_LOG_EMERG, fmt, ##__VA_ARGS__)
  3241. +
  3242. +#endif /* _NMBM_PRIVATE_H_ */
  3243. --- /dev/null
  3244. +++ b/include/nmbm/nmbm-os.h
  3245. @@ -0,0 +1,66 @@
  3246. +/* SPDX-License-Identifier: GPL-2.0 */
  3247. +/*
  3248. + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
  3249. + *
  3250. + * OS-dependent definitions for NAND Mapped-block Management (NMBM)
  3251. + *
  3252. + * Author: Weijie Gao <[email protected]>
  3253. + */
  3254. +
  3255. +#ifndef _NMBM_OS_H_
  3256. +#define _NMBM_OS_H_
  3257. +
  3258. +#include <div64.h>
  3259. +#include <stdbool.h>
  3260. +#include <watchdog.h>
  3261. +#include <u-boot/crc.h>
  3262. +#include <linux/errno.h>
  3263. +#include <linux/log2.h>
  3264. +#include <linux/types.h>
  3265. +
  3266. +static inline uint32_t nmbm_crc32(uint32_t crcval, const void *buf, size_t size)
  3267. +{
  3268. + uint chksz;
  3269. + const unsigned char *p = buf;
  3270. +
  3271. + while (size) {
  3272. + if (size > UINT_MAX)
  3273. + chksz = UINT_MAX;
  3274. + else
  3275. + chksz = (uint)size;
  3276. +
  3277. + crcval = crc32_no_comp(crcval, p, chksz);
  3278. + size -= chksz;
  3279. + p += chksz;
  3280. + }
  3281. +
  3282. + return crcval;
  3283. +}
  3284. +
  3285. +static inline uint32_t nmbm_lldiv(uint64_t dividend, uint32_t divisor)
  3286. +{
  3287. +#if BITS_PER_LONG == 64
  3288. + return dividend / divisor;
  3289. +#else
  3290. + __div64_32(&dividend, divisor);
  3291. + return dividend;
  3292. +#endif
  3293. +}
  3294. +
  3295. +#ifdef CONFIG_NMBM_LOG_LEVEL_DEBUG
  3296. +#define NMBM_DEFAULT_LOG_LEVEL 0
  3297. +#elif defined(NMBM_LOG_LEVEL_INFO)
  3298. +#define NMBM_DEFAULT_LOG_LEVEL 1
  3299. +#elif defined(NMBM_LOG_LEVEL_WARN)
  3300. +#define NMBM_DEFAULT_LOG_LEVEL 2
  3301. +#elif defined(NMBM_LOG_LEVEL_ERR)
  3302. +#define NMBM_DEFAULT_LOG_LEVEL 3
  3303. +#elif defined(NMBM_LOG_LEVEL_EMERG)
  3304. +#define NMBM_DEFAULT_LOG_LEVEL 4
  3305. +#elif defined(NMBM_LOG_LEVEL_NONE)
  3306. +#define NMBM_DEFAULT_LOG_LEVEL 5
  3307. +#else
  3308. +#define NMBM_DEFAULT_LOG_LEVEL 1
  3309. +#endif
  3310. +
  3311. +#endif /* _NMBM_OS_H_ */
  3312. --- /dev/null
  3313. +++ b/include/nmbm/nmbm.h
  3314. @@ -0,0 +1,102 @@
  3315. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
  3316. +/*
  3317. + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
  3318. + *
  3319. + * Definitions for NAND Mapped-block Management (NMBM)
  3320. + *
  3321. + * Author: Weijie Gao <[email protected]>
  3322. + */
  3323. +
  3324. +#ifndef _NMBM_H_
  3325. +#define _NMBM_H_
  3326. +
  3327. +#include <nmbm/nmbm-os.h>
  3328. +
  3329. +enum nmbm_log_category {
  3330. + NMBM_LOG_DEBUG,
  3331. + NMBM_LOG_INFO,
  3332. + NMBM_LOG_WARN,
  3333. + NMBM_LOG_ERR,
  3334. + NMBM_LOG_EMERG,
  3335. +
  3336. + __NMBM_LOG_MAX
  3337. +};
  3338. +
  3339. +enum nmbm_oob_mode {
  3340. + NMBM_MODE_PLACE_OOB,
  3341. + NMBM_MODE_AUTO_OOB,
  3342. + NMBM_MODE_RAW,
  3343. +
  3344. + __NMBM_MODE_MAX
  3345. +};
  3346. +
  3347. +struct nmbm_lower_device {
  3348. + uint32_t max_ratio;
  3349. + uint32_t max_reserved_blocks;
  3350. + int flags;
  3351. +
  3352. + uint64_t size;
  3353. + uint32_t erasesize;
  3354. + uint32_t writesize;
  3355. + uint32_t oobsize;
  3356. + uint32_t oobavail;
  3357. +
  3358. + void *arg;
  3359. + int (*reset_chip)(void *arg);
  3360. +
  3361. + /*
  3362. + * read_page:
  3363. + * return 0 if succeeds
  3364. + * return positive number for ecc error
  3365. + * return negative number for other errors
  3366. + */
  3367. + int (*read_page)(void *arg, uint64_t addr, void *buf, void *oob, enum nmbm_oob_mode mode);
  3368. + int (*write_page)(void *arg, uint64_t addr, const void *buf, const void *oob, enum nmbm_oob_mode mode);
  3369. + int (*erase_block)(void *arg, uint64_t addr);
  3370. +
  3371. + int (*is_bad_block)(void *arg, uint64_t addr);
  3372. + int (*mark_bad_block)(void *arg, uint64_t addr);
  3373. +
  3374. + /* OS-dependent logging function */
  3375. + void (*logprint)(void *arg, enum nmbm_log_category level, const char *fmt, va_list ap);
  3376. +};
  3377. +
  3378. +struct nmbm_instance;
  3379. +
  3380. +/* Create NMBM if management area not found, or not complete */
  3381. +#define NMBM_F_CREATE 0x01
  3382. +
  3383. +/* Empty page is also protected by ECC, and bitflip(s) can be corrected */
  3384. +#define NMBM_F_EMPTY_PAGE_ECC_OK 0x02
  3385. +
  3386. +/* Do not write anything back to flash */
  3387. +#define NMBM_F_READ_ONLY 0x04
  3388. +
  3389. +size_t nmbm_calc_structure_size(struct nmbm_lower_device *nld);
  3390. +int nmbm_attach(struct nmbm_lower_device *nld, struct nmbm_instance *ni);
  3391. +int nmbm_detach(struct nmbm_instance *ni);
  3392. +
  3393. +enum nmbm_log_category nmbm_set_log_level(struct nmbm_instance *ni,
  3394. + enum nmbm_log_category level);
  3395. +
  3396. +int nmbm_erase_block_range(struct nmbm_instance *ni, uint64_t addr,
  3397. + uint64_t size, uint64_t *failed_addr);
  3398. +int nmbm_read_single_page(struct nmbm_instance *ni, uint64_t addr, void *data,
  3399. + void *oob, enum nmbm_oob_mode mode);
  3400. +int nmbm_read_range(struct nmbm_instance *ni, uint64_t addr, size_t size,
  3401. + void *data, enum nmbm_oob_mode mode, size_t *retlen);
  3402. +int nmbm_write_single_page(struct nmbm_instance *ni, uint64_t addr,
  3403. + const void *data, const void *oob,
  3404. + enum nmbm_oob_mode mode);
  3405. +int nmbm_write_range(struct nmbm_instance *ni, uint64_t addr, size_t size,
  3406. + const void *data, enum nmbm_oob_mode mode,
  3407. + size_t *retlen);
  3408. +
  3409. +int nmbm_check_bad_block(struct nmbm_instance *ni, uint64_t addr);
  3410. +int nmbm_mark_bad_block(struct nmbm_instance *ni, uint64_t addr);
  3411. +
  3412. +uint64_t nmbm_get_avail_size(struct nmbm_instance *ni);
  3413. +
  3414. +int nmbm_get_lower_device(struct nmbm_instance *ni, struct nmbm_lower_device *nld);
  3415. +
  3416. +#endif /* _NMBM_H_ */