flash.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306
  1. /*
  2. * NAND Flash Controller Device Driver
  3. * Copyright (c) 2009, Intel Corporation and its suppliers.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/slab.h>
  21. #include "flash.h"
  22. #include "ffsdefs.h"
  23. #include "lld.h"
  24. #include "lld_nand.h"
  25. #if CMD_DMA
  26. #include "lld_cdma.h"
  27. #endif
  28. #define BLK_FROM_ADDR(addr) ((u32)(addr >> DeviceInfo.nBitsInBlockDataSize))
  29. #define PAGE_FROM_ADDR(addr, Block) ((u16)((addr - (u64)Block * \
  30. DeviceInfo.wBlockDataSize) >> DeviceInfo.nBitsInPageDataSize))
  31. #define IS_SPARE_BLOCK(blk) (BAD_BLOCK != (pbt[blk] &\
  32. BAD_BLOCK) && SPARE_BLOCK == (pbt[blk] & SPARE_BLOCK))
  33. #define IS_DATA_BLOCK(blk) (0 == (pbt[blk] & BAD_BLOCK))
  34. #define IS_DISCARDED_BLOCK(blk) (BAD_BLOCK != (pbt[blk] &\
  35. BAD_BLOCK) && DISCARD_BLOCK == (pbt[blk] & DISCARD_BLOCK))
  36. #define IS_BAD_BLOCK(blk) (BAD_BLOCK == (pbt[blk] & BAD_BLOCK))
  37. #if DEBUG_BNDRY
  38. void debug_boundary_lineno_error(int chnl, int limit, int no,
  39. int lineno, char *filename)
  40. {
  41. if (chnl >= limit)
  42. printk(KERN_ERR "Boundary Check Fail value %d >= limit %d, "
  43. "at %s:%d. Other info:%d. Aborting...\n",
  44. chnl, limit, filename, lineno, no);
  45. }
  46. /* static int globalmemsize; */
  47. #endif
  48. static u16 FTL_Cache_If_Hit(u64 dwPageAddr);
  49. static int FTL_Cache_Read(u64 dwPageAddr);
  50. static void FTL_Cache_Read_Page(u8 *pData, u64 dwPageAddr,
  51. u16 cache_blk);
  52. static void FTL_Cache_Write_Page(u8 *pData, u64 dwPageAddr,
  53. u8 cache_blk, u16 flag);
  54. static int FTL_Cache_Write(void);
  55. static void FTL_Calculate_LRU(void);
  56. static u32 FTL_Get_Block_Index(u32 wBlockNum);
  57. static int FTL_Search_Block_Table_IN_Block(u32 BT_Block,
  58. u8 BT_Tag, u16 *Page);
  59. static int FTL_Read_Block_Table(void);
  60. static int FTL_Write_Block_Table(int wForce);
  61. static int FTL_Write_Block_Table_Data(void);
  62. static int FTL_Check_Block_Table(int wOldTable);
  63. static int FTL_Static_Wear_Leveling(void);
  64. static u32 FTL_Replace_Block_Table(void);
  65. static int FTL_Write_IN_Progress_Block_Table_Page(void);
  66. static u32 FTL_Get_Page_Num(u64 length);
  67. static u64 FTL_Get_Physical_Block_Addr(u64 blk_addr);
  68. static u32 FTL_Replace_OneBlock(u32 wBlockNum,
  69. u32 wReplaceNum);
  70. static u32 FTL_Replace_LWBlock(u32 wBlockNum,
  71. int *pGarbageCollect);
  72. static u32 FTL_Replace_MWBlock(void);
  73. static int FTL_Replace_Block(u64 blk_addr);
  74. static int FTL_Adjust_Relative_Erase_Count(u32 Index_of_MAX);
  75. struct device_info_tag DeviceInfo;
  76. struct flash_cache_tag Cache;
  77. static struct spectra_l2_cache_info cache_l2;
  78. static u8 *cache_l2_page_buf;
  79. static u8 *cache_l2_blk_buf;
  80. u8 *g_pBlockTable;
  81. u8 *g_pWearCounter;
  82. u16 *g_pReadCounter;
  83. u32 *g_pBTBlocks;
  84. static u16 g_wBlockTableOffset;
  85. static u32 g_wBlockTableIndex;
  86. static u8 g_cBlockTableStatus;
  87. static u8 *g_pTempBuf;
  88. static u8 *flag_check_blk_table;
  89. static u8 *tmp_buf_search_bt_in_block;
  90. static u8 *spare_buf_search_bt_in_block;
  91. static u8 *spare_buf_bt_search_bt_in_block;
  92. static u8 *tmp_buf1_read_blk_table;
  93. static u8 *tmp_buf2_read_blk_table;
  94. static u8 *flags_static_wear_leveling;
  95. static u8 *tmp_buf_write_blk_table_data;
  96. static u8 *tmp_buf_read_disturbance;
  97. u8 *buf_read_page_main_spare;
  98. u8 *buf_write_page_main_spare;
  99. u8 *buf_read_page_spare;
  100. u8 *buf_get_bad_block;
  101. #if (RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE && CMD_DMA)
  102. struct flash_cache_delta_list_tag int_cache[MAX_CHANS + MAX_DESCS];
  103. struct flash_cache_tag cache_start_copy;
  104. #endif
  105. int g_wNumFreeBlocks;
  106. u8 g_SBDCmdIndex;
  107. static u8 *g_pIPF;
  108. static u8 bt_flag = FIRST_BT_ID;
  109. static u8 bt_block_changed;
  110. static u16 cache_block_to_write;
  111. static u8 last_erased = FIRST_BT_ID;
  112. static u8 GC_Called;
  113. static u8 BT_GC_Called;
  114. #if CMD_DMA
  115. #define COPY_BACK_BUF_NUM 10
  116. static u8 ftl_cmd_cnt; /* Init value is 0 */
  117. u8 *g_pBTDelta;
  118. u8 *g_pBTDelta_Free;
  119. u8 *g_pBTStartingCopy;
  120. u8 *g_pWearCounterCopy;
  121. u16 *g_pReadCounterCopy;
  122. u8 *g_pBlockTableCopies;
  123. u8 *g_pNextBlockTable;
  124. static u8 *cp_back_buf_copies[COPY_BACK_BUF_NUM];
  125. static int cp_back_buf_idx;
  126. static u8 *g_temp_buf;
  127. #pragma pack(push, 1)
  128. #pragma pack(1)
  129. struct BTableChangesDelta {
  130. u8 ftl_cmd_cnt;
  131. u8 ValidFields;
  132. u16 g_wBlockTableOffset;
  133. u32 g_wBlockTableIndex;
  134. u32 BT_Index;
  135. u32 BT_Entry_Value;
  136. u32 WC_Index;
  137. u8 WC_Entry_Value;
  138. u32 RC_Index;
  139. u16 RC_Entry_Value;
  140. };
  141. #pragma pack(pop)
  142. struct BTableChangesDelta *p_BTableChangesDelta;
  143. #endif
  144. #define MARK_BLOCK_AS_BAD(blocknode) (blocknode |= BAD_BLOCK)
  145. #define MARK_BLK_AS_DISCARD(blk) (blk = (blk & ~SPARE_BLOCK) | DISCARD_BLOCK)
  146. #define FTL_Get_LBAPBA_Table_Mem_Size_Bytes() (DeviceInfo.wDataBlockNum *\
  147. sizeof(u32))
  148. #define FTL_Get_WearCounter_Table_Mem_Size_Bytes() (DeviceInfo.wDataBlockNum *\
  149. sizeof(u8))
  150. #define FTL_Get_ReadCounter_Table_Mem_Size_Bytes() (DeviceInfo.wDataBlockNum *\
  151. sizeof(u16))
  152. #if SUPPORT_LARGE_BLOCKNUM
  153. #define FTL_Get_LBAPBA_Table_Flash_Size_Bytes() (DeviceInfo.wDataBlockNum *\
  154. sizeof(u8) * 3)
  155. #else
  156. #define FTL_Get_LBAPBA_Table_Flash_Size_Bytes() (DeviceInfo.wDataBlockNum *\
  157. sizeof(u16))
  158. #endif
  159. #define FTL_Get_WearCounter_Table_Flash_Size_Bytes \
  160. FTL_Get_WearCounter_Table_Mem_Size_Bytes
  161. #define FTL_Get_ReadCounter_Table_Flash_Size_Bytes \
  162. FTL_Get_ReadCounter_Table_Mem_Size_Bytes
  163. static u32 FTL_Get_Block_Table_Flash_Size_Bytes(void)
  164. {
  165. u32 byte_num;
  166. if (DeviceInfo.MLCDevice) {
  167. byte_num = FTL_Get_LBAPBA_Table_Flash_Size_Bytes() +
  168. DeviceInfo.wDataBlockNum * sizeof(u8) +
  169. DeviceInfo.wDataBlockNum * sizeof(u16);
  170. } else {
  171. byte_num = FTL_Get_LBAPBA_Table_Flash_Size_Bytes() +
  172. DeviceInfo.wDataBlockNum * sizeof(u8);
  173. }
  174. byte_num += 4 * sizeof(u8);
  175. return byte_num;
  176. }
  177. static u16 FTL_Get_Block_Table_Flash_Size_Pages(void)
  178. {
  179. return (u16)FTL_Get_Page_Num(FTL_Get_Block_Table_Flash_Size_Bytes());
  180. }
  181. static int FTL_Copy_Block_Table_To_Flash(u8 *flashBuf, u32 sizeToTx,
  182. u32 sizeTxed)
  183. {
  184. u32 wBytesCopied, blk_tbl_size, wBytes;
  185. u32 *pbt = (u32 *)g_pBlockTable;
  186. blk_tbl_size = FTL_Get_LBAPBA_Table_Flash_Size_Bytes();
  187. for (wBytes = 0;
  188. (wBytes < sizeToTx) && ((wBytes + sizeTxed) < blk_tbl_size);
  189. wBytes++) {
  190. #if SUPPORT_LARGE_BLOCKNUM
  191. flashBuf[wBytes] = (u8)(pbt[(wBytes + sizeTxed) / 3]
  192. >> (((wBytes + sizeTxed) % 3) ?
  193. ((((wBytes + sizeTxed) % 3) == 2) ? 0 : 8) : 16)) & 0xFF;
  194. #else
  195. flashBuf[wBytes] = (u8)(pbt[(wBytes + sizeTxed) / 2]
  196. >> (((wBytes + sizeTxed) % 2) ? 0 : 8)) & 0xFF;
  197. #endif
  198. }
  199. sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
  200. blk_tbl_size = FTL_Get_WearCounter_Table_Flash_Size_Bytes();
  201. wBytesCopied = wBytes;
  202. wBytes = ((blk_tbl_size - sizeTxed) > (sizeToTx - wBytesCopied)) ?
  203. (sizeToTx - wBytesCopied) : (blk_tbl_size - sizeTxed);
  204. memcpy(flashBuf + wBytesCopied, g_pWearCounter + sizeTxed, wBytes);
  205. sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
  206. if (DeviceInfo.MLCDevice) {
  207. blk_tbl_size = FTL_Get_ReadCounter_Table_Flash_Size_Bytes();
  208. wBytesCopied += wBytes;
  209. for (wBytes = 0; ((wBytes + wBytesCopied) < sizeToTx) &&
  210. ((wBytes + sizeTxed) < blk_tbl_size); wBytes++)
  211. flashBuf[wBytes + wBytesCopied] =
  212. (g_pReadCounter[(wBytes + sizeTxed) / 2] >>
  213. (((wBytes + sizeTxed) % 2) ? 0 : 8)) & 0xFF;
  214. }
  215. return wBytesCopied + wBytes;
  216. }
  217. static int FTL_Copy_Block_Table_From_Flash(u8 *flashBuf,
  218. u32 sizeToTx, u32 sizeTxed)
  219. {
  220. u32 wBytesCopied, blk_tbl_size, wBytes;
  221. u32 *pbt = (u32 *)g_pBlockTable;
  222. blk_tbl_size = FTL_Get_LBAPBA_Table_Flash_Size_Bytes();
  223. for (wBytes = 0; (wBytes < sizeToTx) &&
  224. ((wBytes + sizeTxed) < blk_tbl_size); wBytes++) {
  225. #if SUPPORT_LARGE_BLOCKNUM
  226. if (!((wBytes + sizeTxed) % 3))
  227. pbt[(wBytes + sizeTxed) / 3] = 0;
  228. pbt[(wBytes + sizeTxed) / 3] |=
  229. (flashBuf[wBytes] << (((wBytes + sizeTxed) % 3) ?
  230. ((((wBytes + sizeTxed) % 3) == 2) ? 0 : 8) : 16));
  231. #else
  232. if (!((wBytes + sizeTxed) % 2))
  233. pbt[(wBytes + sizeTxed) / 2] = 0;
  234. pbt[(wBytes + sizeTxed) / 2] |=
  235. (flashBuf[wBytes] << (((wBytes + sizeTxed) % 2) ?
  236. 0 : 8));
  237. #endif
  238. }
  239. sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
  240. blk_tbl_size = FTL_Get_WearCounter_Table_Flash_Size_Bytes();
  241. wBytesCopied = wBytes;
  242. wBytes = ((blk_tbl_size - sizeTxed) > (sizeToTx - wBytesCopied)) ?
  243. (sizeToTx - wBytesCopied) : (blk_tbl_size - sizeTxed);
  244. memcpy(g_pWearCounter + sizeTxed, flashBuf + wBytesCopied, wBytes);
  245. sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
  246. if (DeviceInfo.MLCDevice) {
  247. wBytesCopied += wBytes;
  248. blk_tbl_size = FTL_Get_ReadCounter_Table_Flash_Size_Bytes();
  249. for (wBytes = 0; ((wBytes + wBytesCopied) < sizeToTx) &&
  250. ((wBytes + sizeTxed) < blk_tbl_size); wBytes++) {
  251. if (((wBytes + sizeTxed) % 2))
  252. g_pReadCounter[(wBytes + sizeTxed) / 2] = 0;
  253. g_pReadCounter[(wBytes + sizeTxed) / 2] |=
  254. (flashBuf[wBytes] <<
  255. (((wBytes + sizeTxed) % 2) ? 0 : 8));
  256. }
  257. }
  258. return wBytesCopied+wBytes;
  259. }
  260. static int FTL_Insert_Block_Table_Signature(u8 *buf, u8 tag)
  261. {
  262. int i;
  263. for (i = 0; i < BTSIG_BYTES; i++)
  264. buf[BTSIG_OFFSET + i] =
  265. ((tag + (i * BTSIG_DELTA) - FIRST_BT_ID) %
  266. (1 + LAST_BT_ID-FIRST_BT_ID)) + FIRST_BT_ID;
  267. return PASS;
  268. }
  269. static int FTL_Extract_Block_Table_Tag(u8 *buf, u8 **tagarray)
  270. {
  271. static u8 tag[BTSIG_BYTES >> 1];
  272. int i, j, k, tagi, tagtemp, status;
  273. *tagarray = (u8 *)tag;
  274. tagi = 0;
  275. for (i = 0; i < (BTSIG_BYTES - 1); i++) {
  276. for (j = i + 1; (j < BTSIG_BYTES) &&
  277. (tagi < (BTSIG_BYTES >> 1)); j++) {
  278. tagtemp = buf[BTSIG_OFFSET + j] -
  279. buf[BTSIG_OFFSET + i];
  280. if (tagtemp && !(tagtemp % BTSIG_DELTA)) {
  281. tagtemp = (buf[BTSIG_OFFSET + i] +
  282. (1 + LAST_BT_ID - FIRST_BT_ID) -
  283. (i * BTSIG_DELTA)) %
  284. (1 + LAST_BT_ID - FIRST_BT_ID);
  285. status = FAIL;
  286. for (k = 0; k < tagi; k++) {
  287. if (tagtemp == tag[k])
  288. status = PASS;
  289. }
  290. if (status == FAIL) {
  291. tag[tagi++] = tagtemp;
  292. i = (j == (i + 1)) ? i + 1 : i;
  293. j = (j == (i + 1)) ? i + 1 : i;
  294. }
  295. }
  296. }
  297. }
  298. return tagi;
  299. }
  300. static int FTL_Execute_SPL_Recovery(void)
  301. {
  302. u32 j, block, blks;
  303. u32 *pbt = (u32 *)g_pBlockTable;
  304. int ret;
  305. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  306. __FILE__, __LINE__, __func__);
  307. blks = DeviceInfo.wSpectraEndBlock - DeviceInfo.wSpectraStartBlock;
  308. for (j = 0; j <= blks; j++) {
  309. block = (pbt[j]);
  310. if (((block & BAD_BLOCK) != BAD_BLOCK) &&
  311. ((block & SPARE_BLOCK) == SPARE_BLOCK)) {
  312. ret = GLOB_LLD_Erase_Block(block & ~BAD_BLOCK);
  313. if (FAIL == ret) {
  314. nand_dbg_print(NAND_DBG_WARN,
  315. "NAND Program fail in %s, Line %d, "
  316. "Function: %s, new Bad Block %d "
  317. "generated!\n",
  318. __FILE__, __LINE__, __func__,
  319. (int)(block & ~BAD_BLOCK));
  320. MARK_BLOCK_AS_BAD(pbt[j]);
  321. }
  322. }
  323. }
  324. return PASS;
  325. }
  326. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  327. * Function: GLOB_FTL_IdentifyDevice
  328. * Inputs: pointer to identify data structure
  329. * Outputs: PASS / FAIL
  330. * Description: the identify data structure is filled in with
  331. * information for the block driver.
  332. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  333. int GLOB_FTL_IdentifyDevice(struct spectra_indentfy_dev_tag *dev_data)
  334. {
  335. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  336. __FILE__, __LINE__, __func__);
  337. dev_data->NumBlocks = DeviceInfo.wTotalBlocks;
  338. dev_data->PagesPerBlock = DeviceInfo.wPagesPerBlock;
  339. dev_data->PageDataSize = DeviceInfo.wPageDataSize;
  340. dev_data->wECCBytesPerSector = DeviceInfo.wECCBytesPerSector;
  341. dev_data->wDataBlockNum = DeviceInfo.wDataBlockNum;
  342. return PASS;
  343. }
  344. /* ..... */
  345. static int allocate_memory(void)
  346. {
  347. u32 block_table_size, page_size, block_size, mem_size;
  348. u32 total_bytes = 0;
  349. int i;
  350. #if CMD_DMA
  351. int j;
  352. #endif
  353. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  354. __FILE__, __LINE__, __func__);
  355. page_size = DeviceInfo.wPageSize;
  356. block_size = DeviceInfo.wPagesPerBlock * DeviceInfo.wPageDataSize;
  357. block_table_size = DeviceInfo.wDataBlockNum *
  358. (sizeof(u32) + sizeof(u8) + sizeof(u16));
  359. block_table_size += (DeviceInfo.wPageDataSize -
  360. (block_table_size % DeviceInfo.wPageDataSize)) %
  361. DeviceInfo.wPageDataSize;
  362. /* Malloc memory for block tables */
  363. g_pBlockTable = kzalloc(block_table_size, GFP_ATOMIC);
  364. if (!g_pBlockTable)
  365. goto block_table_fail;
  366. total_bytes += block_table_size;
  367. g_pWearCounter = (u8 *)(g_pBlockTable +
  368. DeviceInfo.wDataBlockNum * sizeof(u32));
  369. if (DeviceInfo.MLCDevice)
  370. g_pReadCounter = (u16 *)(g_pBlockTable +
  371. DeviceInfo.wDataBlockNum *
  372. (sizeof(u32) + sizeof(u8)));
  373. /* Malloc memory and init for cache items */
  374. for (i = 0; i < CACHE_ITEM_NUM; i++) {
  375. Cache.array[i].address = NAND_CACHE_INIT_ADDR;
  376. Cache.array[i].use_cnt = 0;
  377. Cache.array[i].changed = CLEAR;
  378. Cache.array[i].buf = kzalloc(Cache.cache_item_size,
  379. GFP_ATOMIC);
  380. if (!Cache.array[i].buf)
  381. goto cache_item_fail;
  382. total_bytes += Cache.cache_item_size;
  383. }
  384. /* Malloc memory for IPF */
  385. g_pIPF = kzalloc(page_size, GFP_ATOMIC);
  386. if (!g_pIPF)
  387. goto ipf_fail;
  388. total_bytes += page_size;
  389. /* Malloc memory for data merging during Level2 Cache flush */
  390. cache_l2_page_buf = kmalloc(page_size, GFP_ATOMIC);
  391. if (!cache_l2_page_buf)
  392. goto cache_l2_page_buf_fail;
  393. memset(cache_l2_page_buf, 0xff, page_size);
  394. total_bytes += page_size;
  395. cache_l2_blk_buf = kmalloc(block_size, GFP_ATOMIC);
  396. if (!cache_l2_blk_buf)
  397. goto cache_l2_blk_buf_fail;
  398. memset(cache_l2_blk_buf, 0xff, block_size);
  399. total_bytes += block_size;
  400. /* Malloc memory for temp buffer */
  401. g_pTempBuf = kzalloc(Cache.cache_item_size, GFP_ATOMIC);
  402. if (!g_pTempBuf)
  403. goto Temp_buf_fail;
  404. total_bytes += Cache.cache_item_size;
  405. /* Malloc memory for block table blocks */
  406. mem_size = (1 + LAST_BT_ID - FIRST_BT_ID) * sizeof(u32);
  407. g_pBTBlocks = kmalloc(mem_size, GFP_ATOMIC);
  408. if (!g_pBTBlocks)
  409. goto bt_blocks_fail;
  410. memset(g_pBTBlocks, 0xff, mem_size);
  411. total_bytes += mem_size;
  412. /* Malloc memory for function FTL_Check_Block_Table */
  413. flag_check_blk_table = kmalloc(DeviceInfo.wDataBlockNum, GFP_ATOMIC);
  414. if (!flag_check_blk_table)
  415. goto flag_check_blk_table_fail;
  416. total_bytes += DeviceInfo.wDataBlockNum;
  417. /* Malloc memory for function FTL_Search_Block_Table_IN_Block */
  418. tmp_buf_search_bt_in_block = kmalloc(page_size, GFP_ATOMIC);
  419. if (!tmp_buf_search_bt_in_block)
  420. goto tmp_buf_search_bt_in_block_fail;
  421. memset(tmp_buf_search_bt_in_block, 0xff, page_size);
  422. total_bytes += page_size;
  423. mem_size = DeviceInfo.wPageSize - DeviceInfo.wPageDataSize;
  424. spare_buf_search_bt_in_block = kmalloc(mem_size, GFP_ATOMIC);
  425. if (!spare_buf_search_bt_in_block)
  426. goto spare_buf_search_bt_in_block_fail;
  427. memset(spare_buf_search_bt_in_block, 0xff, mem_size);
  428. total_bytes += mem_size;
  429. spare_buf_bt_search_bt_in_block = kmalloc(mem_size, GFP_ATOMIC);
  430. if (!spare_buf_bt_search_bt_in_block)
  431. goto spare_buf_bt_search_bt_in_block_fail;
  432. memset(spare_buf_bt_search_bt_in_block, 0xff, mem_size);
  433. total_bytes += mem_size;
  434. /* Malloc memory for function FTL_Read_Block_Table */
  435. tmp_buf1_read_blk_table = kmalloc(page_size, GFP_ATOMIC);
  436. if (!tmp_buf1_read_blk_table)
  437. goto tmp_buf1_read_blk_table_fail;
  438. memset(tmp_buf1_read_blk_table, 0xff, page_size);
  439. total_bytes += page_size;
  440. tmp_buf2_read_blk_table = kmalloc(page_size, GFP_ATOMIC);
  441. if (!tmp_buf2_read_blk_table)
  442. goto tmp_buf2_read_blk_table_fail;
  443. memset(tmp_buf2_read_blk_table, 0xff, page_size);
  444. total_bytes += page_size;
  445. /* Malloc memory for function FTL_Static_Wear_Leveling */
  446. flags_static_wear_leveling = kmalloc(DeviceInfo.wDataBlockNum,
  447. GFP_ATOMIC);
  448. if (!flags_static_wear_leveling)
  449. goto flags_static_wear_leveling_fail;
  450. total_bytes += DeviceInfo.wDataBlockNum;
  451. /* Malloc memory for function FTL_Write_Block_Table_Data */
  452. if (FTL_Get_Block_Table_Flash_Size_Pages() > 3)
  453. mem_size = FTL_Get_Block_Table_Flash_Size_Bytes() -
  454. 2 * DeviceInfo.wPageSize;
  455. else
  456. mem_size = DeviceInfo.wPageSize;
  457. tmp_buf_write_blk_table_data = kmalloc(mem_size, GFP_ATOMIC);
  458. if (!tmp_buf_write_blk_table_data)
  459. goto tmp_buf_write_blk_table_data_fail;
  460. memset(tmp_buf_write_blk_table_data, 0xff, mem_size);
  461. total_bytes += mem_size;
  462. /* Malloc memory for function FTL_Read_Disturbance */
  463. tmp_buf_read_disturbance = kmalloc(block_size, GFP_ATOMIC);
  464. if (!tmp_buf_read_disturbance)
  465. goto tmp_buf_read_disturbance_fail;
  466. memset(tmp_buf_read_disturbance, 0xff, block_size);
  467. total_bytes += block_size;
  468. /* Alloc mem for function NAND_Read_Page_Main_Spare of lld_nand.c */
  469. buf_read_page_main_spare = kmalloc(DeviceInfo.wPageSize, GFP_ATOMIC);
  470. if (!buf_read_page_main_spare)
  471. goto buf_read_page_main_spare_fail;
  472. total_bytes += DeviceInfo.wPageSize;
  473. /* Alloc mem for function NAND_Write_Page_Main_Spare of lld_nand.c */
  474. buf_write_page_main_spare = kmalloc(DeviceInfo.wPageSize, GFP_ATOMIC);
  475. if (!buf_write_page_main_spare)
  476. goto buf_write_page_main_spare_fail;
  477. total_bytes += DeviceInfo.wPageSize;
  478. /* Alloc mem for function NAND_Read_Page_Spare of lld_nand.c */
  479. buf_read_page_spare = kmalloc(DeviceInfo.wPageSpareSize, GFP_ATOMIC);
  480. if (!buf_read_page_spare)
  481. goto buf_read_page_spare_fail;
  482. memset(buf_read_page_spare, 0xff, DeviceInfo.wPageSpareSize);
  483. total_bytes += DeviceInfo.wPageSpareSize;
  484. /* Alloc mem for function NAND_Get_Bad_Block of lld_nand.c */
  485. buf_get_bad_block = kmalloc(DeviceInfo.wPageSpareSize, GFP_ATOMIC);
  486. if (!buf_get_bad_block)
  487. goto buf_get_bad_block_fail;
  488. memset(buf_get_bad_block, 0xff, DeviceInfo.wPageSpareSize);
  489. total_bytes += DeviceInfo.wPageSpareSize;
  490. #if CMD_DMA
  491. g_temp_buf = kmalloc(block_size, GFP_ATOMIC);
  492. if (!g_temp_buf)
  493. goto temp_buf_fail;
  494. memset(g_temp_buf, 0xff, block_size);
  495. total_bytes += block_size;
  496. /* Malloc memory for copy of block table used in CDMA mode */
  497. g_pBTStartingCopy = kzalloc(block_table_size, GFP_ATOMIC);
  498. if (!g_pBTStartingCopy)
  499. goto bt_starting_copy;
  500. total_bytes += block_table_size;
  501. g_pWearCounterCopy = (u8 *)(g_pBTStartingCopy +
  502. DeviceInfo.wDataBlockNum * sizeof(u32));
  503. if (DeviceInfo.MLCDevice)
  504. g_pReadCounterCopy = (u16 *)(g_pBTStartingCopy +
  505. DeviceInfo.wDataBlockNum *
  506. (sizeof(u32) + sizeof(u8)));
  507. /* Malloc memory for block table copies */
  508. mem_size = 5 * DeviceInfo.wDataBlockNum * sizeof(u32) +
  509. 5 * DeviceInfo.wDataBlockNum * sizeof(u8);
  510. if (DeviceInfo.MLCDevice)
  511. mem_size += 5 * DeviceInfo.wDataBlockNum * sizeof(u16);
  512. g_pBlockTableCopies = kzalloc(mem_size, GFP_ATOMIC);
  513. if (!g_pBlockTableCopies)
  514. goto blk_table_copies_fail;
  515. total_bytes += mem_size;
  516. g_pNextBlockTable = g_pBlockTableCopies;
  517. /* Malloc memory for Block Table Delta */
  518. mem_size = MAX_DESCS * sizeof(struct BTableChangesDelta);
  519. g_pBTDelta = kzalloc(mem_size, GFP_ATOMIC);
  520. if (!g_pBTDelta)
  521. goto bt_delta_fail;
  522. total_bytes += mem_size;
  523. g_pBTDelta_Free = g_pBTDelta;
  524. /* Malloc memory for Copy Back Buffers */
  525. for (j = 0; j < COPY_BACK_BUF_NUM; j++) {
  526. cp_back_buf_copies[j] = kzalloc(block_size, GFP_ATOMIC);
  527. if (!cp_back_buf_copies[j])
  528. goto cp_back_buf_copies_fail;
  529. total_bytes += block_size;
  530. }
  531. cp_back_buf_idx = 0;
  532. /* Malloc memory for pending commands list */
  533. mem_size = sizeof(struct pending_cmd) * MAX_DESCS;
  534. info.pcmds = kzalloc(mem_size, GFP_KERNEL);
  535. if (!info.pcmds)
  536. goto pending_cmds_buf_fail;
  537. total_bytes += mem_size;
  538. /* Malloc memory for CDMA descripter table */
  539. mem_size = sizeof(struct cdma_descriptor) * MAX_DESCS;
  540. info.cdma_desc_buf = kzalloc(mem_size, GFP_KERNEL);
  541. if (!info.cdma_desc_buf)
  542. goto cdma_desc_buf_fail;
  543. total_bytes += mem_size;
  544. /* Malloc memory for Memcpy descripter table */
  545. mem_size = sizeof(struct memcpy_descriptor) * MAX_DESCS;
  546. info.memcp_desc_buf = kzalloc(mem_size, GFP_KERNEL);
  547. if (!info.memcp_desc_buf)
  548. goto memcp_desc_buf_fail;
  549. total_bytes += mem_size;
  550. #endif
  551. nand_dbg_print(NAND_DBG_WARN,
  552. "Total memory allocated in FTL layer: %d\n", total_bytes);
  553. return PASS;
  554. #if CMD_DMA
  555. memcp_desc_buf_fail:
  556. kfree(info.cdma_desc_buf);
  557. cdma_desc_buf_fail:
  558. kfree(info.pcmds);
  559. pending_cmds_buf_fail:
  560. cp_back_buf_copies_fail:
  561. j--;
  562. for (; j >= 0; j--)
  563. kfree(cp_back_buf_copies[j]);
  564. kfree(g_pBTDelta);
  565. bt_delta_fail:
  566. kfree(g_pBlockTableCopies);
  567. blk_table_copies_fail:
  568. kfree(g_pBTStartingCopy);
  569. bt_starting_copy:
  570. kfree(g_temp_buf);
  571. temp_buf_fail:
  572. kfree(buf_get_bad_block);
  573. #endif
  574. buf_get_bad_block_fail:
  575. kfree(buf_read_page_spare);
  576. buf_read_page_spare_fail:
  577. kfree(buf_write_page_main_spare);
  578. buf_write_page_main_spare_fail:
  579. kfree(buf_read_page_main_spare);
  580. buf_read_page_main_spare_fail:
  581. kfree(tmp_buf_read_disturbance);
  582. tmp_buf_read_disturbance_fail:
  583. kfree(tmp_buf_write_blk_table_data);
  584. tmp_buf_write_blk_table_data_fail:
  585. kfree(flags_static_wear_leveling);
  586. flags_static_wear_leveling_fail:
  587. kfree(tmp_buf2_read_blk_table);
  588. tmp_buf2_read_blk_table_fail:
  589. kfree(tmp_buf1_read_blk_table);
  590. tmp_buf1_read_blk_table_fail:
  591. kfree(spare_buf_bt_search_bt_in_block);
  592. spare_buf_bt_search_bt_in_block_fail:
  593. kfree(spare_buf_search_bt_in_block);
  594. spare_buf_search_bt_in_block_fail:
  595. kfree(tmp_buf_search_bt_in_block);
  596. tmp_buf_search_bt_in_block_fail:
  597. kfree(flag_check_blk_table);
  598. flag_check_blk_table_fail:
  599. kfree(g_pBTBlocks);
  600. bt_blocks_fail:
  601. kfree(g_pTempBuf);
  602. Temp_buf_fail:
  603. kfree(cache_l2_blk_buf);
  604. cache_l2_blk_buf_fail:
  605. kfree(cache_l2_page_buf);
  606. cache_l2_page_buf_fail:
  607. kfree(g_pIPF);
  608. ipf_fail:
  609. cache_item_fail:
  610. i--;
  611. for (; i >= 0; i--)
  612. kfree(Cache.array[i].buf);
  613. kfree(g_pBlockTable);
  614. block_table_fail:
  615. printk(KERN_ERR "Failed to kmalloc memory in %s Line %d.\n",
  616. __FILE__, __LINE__);
  617. return -ENOMEM;
  618. }
  619. /* .... */
  620. static int free_memory(void)
  621. {
  622. int i;
  623. #if CMD_DMA
  624. kfree(info.memcp_desc_buf);
  625. kfree(info.cdma_desc_buf);
  626. kfree(info.pcmds);
  627. for (i = COPY_BACK_BUF_NUM - 1; i >= 0; i--)
  628. kfree(cp_back_buf_copies[i]);
  629. kfree(g_pBTDelta);
  630. kfree(g_pBlockTableCopies);
  631. kfree(g_pBTStartingCopy);
  632. kfree(g_temp_buf);
  633. kfree(buf_get_bad_block);
  634. #endif
  635. kfree(buf_read_page_spare);
  636. kfree(buf_write_page_main_spare);
  637. kfree(buf_read_page_main_spare);
  638. kfree(tmp_buf_read_disturbance);
  639. kfree(tmp_buf_write_blk_table_data);
  640. kfree(flags_static_wear_leveling);
  641. kfree(tmp_buf2_read_blk_table);
  642. kfree(tmp_buf1_read_blk_table);
  643. kfree(spare_buf_bt_search_bt_in_block);
  644. kfree(spare_buf_search_bt_in_block);
  645. kfree(tmp_buf_search_bt_in_block);
  646. kfree(flag_check_blk_table);
  647. kfree(g_pBTBlocks);
  648. kfree(g_pTempBuf);
  649. kfree(g_pIPF);
  650. for (i = CACHE_ITEM_NUM - 1; i >= 0; i--)
  651. kfree(Cache.array[i].buf);
  652. kfree(g_pBlockTable);
  653. return 0;
  654. }
  655. static void dump_cache_l2_table(void)
  656. {
  657. struct list_head *p;
  658. struct spectra_l2_cache_list *pnd;
  659. int n;
  660. n = 0;
  661. list_for_each(p, &cache_l2.table.list) {
  662. pnd = list_entry(p, struct spectra_l2_cache_list, list);
  663. nand_dbg_print(NAND_DBG_WARN, "dump_cache_l2_table node: %d, logical_blk_num: %d\n", n, pnd->logical_blk_num);
  664. /*
  665. for (i = 0; i < DeviceInfo.wPagesPerBlock; i++) {
  666. if (pnd->pages_array[i] != MAX_U32_VALUE)
  667. nand_dbg_print(NAND_DBG_WARN, " pages_array[%d]: 0x%x\n", i, pnd->pages_array[i]);
  668. }
  669. */
  670. n++;
  671. }
  672. }
  673. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  674. * Function: GLOB_FTL_Init
  675. * Inputs: none
  676. * Outputs: PASS=0 / FAIL=1
  677. * Description: allocates the memory for cache array,
  678. * important data structures
  679. * clears the cache array
  680. * reads the block table from flash into array
  681. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  682. int GLOB_FTL_Init(void)
  683. {
  684. int i;
  685. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  686. __FILE__, __LINE__, __func__);
  687. Cache.pages_per_item = 1;
  688. Cache.cache_item_size = 1 * DeviceInfo.wPageDataSize;
  689. if (allocate_memory() != PASS)
  690. return FAIL;
  691. #if CMD_DMA
  692. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  693. memcpy((void *)&cache_start_copy, (void *)&Cache,
  694. sizeof(struct flash_cache_tag));
  695. memset((void *)&int_cache, -1,
  696. sizeof(struct flash_cache_delta_list_tag) *
  697. (MAX_CHANS + MAX_DESCS));
  698. #endif
  699. ftl_cmd_cnt = 0;
  700. #endif
  701. if (FTL_Read_Block_Table() != PASS)
  702. return FAIL;
  703. /* Init the Level2 Cache data structure */
  704. for (i = 0; i < BLK_NUM_FOR_L2_CACHE; i++)
  705. cache_l2.blk_array[i] = MAX_U32_VALUE;
  706. cache_l2.cur_blk_idx = 0;
  707. cache_l2.cur_page_num = 0;
  708. INIT_LIST_HEAD(&cache_l2.table.list);
  709. cache_l2.table.logical_blk_num = MAX_U32_VALUE;
  710. dump_cache_l2_table();
  711. return 0;
  712. }
  713. #if CMD_DMA
  714. #if 0
  715. static void save_blk_table_changes(u16 idx)
  716. {
  717. u8 ftl_cmd;
  718. u32 *pbt = (u32 *)g_pBTStartingCopy;
  719. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  720. u16 id;
  721. u8 cache_blks;
  722. id = idx - MAX_CHANS;
  723. if (int_cache[id].item != -1) {
  724. cache_blks = int_cache[id].item;
  725. cache_start_copy.array[cache_blks].address =
  726. int_cache[id].cache.address;
  727. cache_start_copy.array[cache_blks].changed =
  728. int_cache[id].cache.changed;
  729. }
  730. #endif
  731. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  732. while (ftl_cmd <= PendingCMD[idx].Tag) {
  733. if (p_BTableChangesDelta->ValidFields == 0x01) {
  734. g_wBlockTableOffset =
  735. p_BTableChangesDelta->g_wBlockTableOffset;
  736. } else if (p_BTableChangesDelta->ValidFields == 0x0C) {
  737. pbt[p_BTableChangesDelta->BT_Index] =
  738. p_BTableChangesDelta->BT_Entry_Value;
  739. debug_boundary_error(((
  740. p_BTableChangesDelta->BT_Index)),
  741. DeviceInfo.wDataBlockNum, 0);
  742. } else if (p_BTableChangesDelta->ValidFields == 0x03) {
  743. g_wBlockTableOffset =
  744. p_BTableChangesDelta->g_wBlockTableOffset;
  745. g_wBlockTableIndex =
  746. p_BTableChangesDelta->g_wBlockTableIndex;
  747. } else if (p_BTableChangesDelta->ValidFields == 0x30) {
  748. g_pWearCounterCopy[p_BTableChangesDelta->WC_Index] =
  749. p_BTableChangesDelta->WC_Entry_Value;
  750. } else if ((DeviceInfo.MLCDevice) &&
  751. (p_BTableChangesDelta->ValidFields == 0xC0)) {
  752. g_pReadCounterCopy[p_BTableChangesDelta->RC_Index] =
  753. p_BTableChangesDelta->RC_Entry_Value;
  754. nand_dbg_print(NAND_DBG_DEBUG,
  755. "In event status setting read counter "
  756. "GLOB_ftl_cmd_cnt %u Count %u Index %u\n",
  757. ftl_cmd,
  758. p_BTableChangesDelta->RC_Entry_Value,
  759. (unsigned int)p_BTableChangesDelta->RC_Index);
  760. } else {
  761. nand_dbg_print(NAND_DBG_DEBUG,
  762. "This should never occur \n");
  763. }
  764. p_BTableChangesDelta += 1;
  765. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  766. }
  767. }
  768. static void discard_cmds(u16 n)
  769. {
  770. u32 *pbt = (u32 *)g_pBTStartingCopy;
  771. u8 ftl_cmd;
  772. unsigned long k;
  773. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  774. u8 cache_blks;
  775. u16 id;
  776. #endif
  777. if ((PendingCMD[n].CMD == WRITE_MAIN_CMD) ||
  778. (PendingCMD[n].CMD == WRITE_MAIN_SPARE_CMD)) {
  779. for (k = 0; k < DeviceInfo.wDataBlockNum; k++) {
  780. if (PendingCMD[n].Block == (pbt[k] & (~BAD_BLOCK)))
  781. MARK_BLK_AS_DISCARD(pbt[k]);
  782. }
  783. }
  784. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  785. while (ftl_cmd <= PendingCMD[n].Tag) {
  786. p_BTableChangesDelta += 1;
  787. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  788. }
  789. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  790. id = n - MAX_CHANS;
  791. if (int_cache[id].item != -1) {
  792. cache_blks = int_cache[id].item;
  793. if (PendingCMD[n].CMD == MEMCOPY_CMD) {
  794. if ((cache_start_copy.array[cache_blks].buf <=
  795. PendingCMD[n].DataDestAddr) &&
  796. ((cache_start_copy.array[cache_blks].buf +
  797. Cache.cache_item_size) >
  798. PendingCMD[n].DataDestAddr)) {
  799. cache_start_copy.array[cache_blks].address =
  800. NAND_CACHE_INIT_ADDR;
  801. cache_start_copy.array[cache_blks].use_cnt =
  802. 0;
  803. cache_start_copy.array[cache_blks].changed =
  804. CLEAR;
  805. }
  806. } else {
  807. cache_start_copy.array[cache_blks].address =
  808. int_cache[id].cache.address;
  809. cache_start_copy.array[cache_blks].changed =
  810. int_cache[id].cache.changed;
  811. }
  812. }
  813. #endif
  814. }
  815. static void process_cmd_pass(int *first_failed_cmd, u16 idx)
  816. {
  817. if (0 == *first_failed_cmd)
  818. save_blk_table_changes(idx);
  819. else
  820. discard_cmds(idx);
  821. }
  822. static void process_cmd_fail_abort(int *first_failed_cmd,
  823. u16 idx, int event)
  824. {
  825. u32 *pbt = (u32 *)g_pBTStartingCopy;
  826. u8 ftl_cmd;
  827. unsigned long i;
  828. int erase_fail, program_fail;
  829. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  830. u8 cache_blks;
  831. u16 id;
  832. #endif
  833. if (0 == *first_failed_cmd)
  834. *first_failed_cmd = PendingCMD[idx].SBDCmdIndex;
  835. nand_dbg_print(NAND_DBG_DEBUG, "Uncorrectable error has occurred "
  836. "while executing %u Command %u accesing Block %u\n",
  837. (unsigned int)p_BTableChangesDelta->ftl_cmd_cnt,
  838. PendingCMD[idx].CMD,
  839. (unsigned int)PendingCMD[idx].Block);
  840. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  841. while (ftl_cmd <= PendingCMD[idx].Tag) {
  842. p_BTableChangesDelta += 1;
  843. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  844. }
  845. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  846. id = idx - MAX_CHANS;
  847. if (int_cache[id].item != -1) {
  848. cache_blks = int_cache[id].item;
  849. if ((PendingCMD[idx].CMD == WRITE_MAIN_CMD)) {
  850. cache_start_copy.array[cache_blks].address =
  851. int_cache[id].cache.address;
  852. cache_start_copy.array[cache_blks].changed = SET;
  853. } else if ((PendingCMD[idx].CMD == READ_MAIN_CMD)) {
  854. cache_start_copy.array[cache_blks].address =
  855. NAND_CACHE_INIT_ADDR;
  856. cache_start_copy.array[cache_blks].use_cnt = 0;
  857. cache_start_copy.array[cache_blks].changed =
  858. CLEAR;
  859. } else if (PendingCMD[idx].CMD == ERASE_CMD) {
  860. /* ? */
  861. } else if (PendingCMD[idx].CMD == MEMCOPY_CMD) {
  862. /* ? */
  863. }
  864. }
  865. #endif
  866. erase_fail = (event == EVENT_ERASE_FAILURE) &&
  867. (PendingCMD[idx].CMD == ERASE_CMD);
  868. program_fail = (event == EVENT_PROGRAM_FAILURE) &&
  869. ((PendingCMD[idx].CMD == WRITE_MAIN_CMD) ||
  870. (PendingCMD[idx].CMD == WRITE_MAIN_SPARE_CMD));
  871. if (erase_fail || program_fail) {
  872. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  873. if (PendingCMD[idx].Block ==
  874. (pbt[i] & (~BAD_BLOCK)))
  875. MARK_BLOCK_AS_BAD(pbt[i]);
  876. }
  877. }
  878. }
  879. static void process_cmd(int *first_failed_cmd, u16 idx, int event)
  880. {
  881. u8 ftl_cmd;
  882. int cmd_match = 0;
  883. if (p_BTableChangesDelta->ftl_cmd_cnt == PendingCMD[idx].Tag)
  884. cmd_match = 1;
  885. if (PendingCMD[idx].Status == CMD_PASS) {
  886. process_cmd_pass(first_failed_cmd, idx);
  887. } else if ((PendingCMD[idx].Status == CMD_FAIL) ||
  888. (PendingCMD[idx].Status == CMD_ABORT)) {
  889. process_cmd_fail_abort(first_failed_cmd, idx, event);
  890. } else if ((PendingCMD[idx].Status == CMD_NOT_DONE) &&
  891. PendingCMD[idx].Tag) {
  892. nand_dbg_print(NAND_DBG_DEBUG,
  893. " Command no. %hu is not executed\n",
  894. (unsigned int)PendingCMD[idx].Tag);
  895. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  896. while (ftl_cmd <= PendingCMD[idx].Tag) {
  897. p_BTableChangesDelta += 1;
  898. ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
  899. }
  900. }
  901. }
  902. #endif
  903. static void process_cmd(int *first_failed_cmd, u16 idx, int event)
  904. {
  905. printk(KERN_ERR "temporary workaround function. "
  906. "Should not be called! \n");
  907. }
  908. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  909. * Function: GLOB_FTL_Event_Status
  910. * Inputs: none
  911. * Outputs: Event Code
  912. * Description: It is called by SBD after hardware interrupt signalling
  913. * completion of commands chain
  914. * It does following things
  915. * get event status from LLD
  916. * analyze command chain status
  917. * determine last command executed
  918. * analyze results
  919. * rebuild the block table in case of uncorrectable error
  920. * return event code
  921. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  922. int GLOB_FTL_Event_Status(int *first_failed_cmd)
  923. {
  924. int event_code = PASS;
  925. u16 i_P;
  926. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  927. __FILE__, __LINE__, __func__);
  928. *first_failed_cmd = 0;
  929. event_code = GLOB_LLD_Event_Status();
  930. switch (event_code) {
  931. case EVENT_PASS:
  932. nand_dbg_print(NAND_DBG_DEBUG, "Handling EVENT_PASS\n");
  933. break;
  934. case EVENT_UNCORRECTABLE_DATA_ERROR:
  935. nand_dbg_print(NAND_DBG_DEBUG, "Handling Uncorrectable ECC!\n");
  936. break;
  937. case EVENT_PROGRAM_FAILURE:
  938. case EVENT_ERASE_FAILURE:
  939. nand_dbg_print(NAND_DBG_WARN, "Handling Ugly case. "
  940. "Event code: 0x%x\n", event_code);
  941. p_BTableChangesDelta =
  942. (struct BTableChangesDelta *)g_pBTDelta;
  943. for (i_P = MAX_CHANS; i_P < (ftl_cmd_cnt + MAX_CHANS);
  944. i_P++)
  945. process_cmd(first_failed_cmd, i_P, event_code);
  946. memcpy(g_pBlockTable, g_pBTStartingCopy,
  947. DeviceInfo.wDataBlockNum * sizeof(u32));
  948. memcpy(g_pWearCounter, g_pWearCounterCopy,
  949. DeviceInfo.wDataBlockNum * sizeof(u8));
  950. if (DeviceInfo.MLCDevice)
  951. memcpy(g_pReadCounter, g_pReadCounterCopy,
  952. DeviceInfo.wDataBlockNum * sizeof(u16));
  953. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  954. memcpy((void *)&Cache, (void *)&cache_start_copy,
  955. sizeof(struct flash_cache_tag));
  956. memset((void *)&int_cache, -1,
  957. sizeof(struct flash_cache_delta_list_tag) *
  958. (MAX_DESCS + MAX_CHANS));
  959. #endif
  960. break;
  961. default:
  962. nand_dbg_print(NAND_DBG_WARN,
  963. "Handling unexpected event code - 0x%x\n",
  964. event_code);
  965. event_code = ERR;
  966. break;
  967. }
  968. memcpy(g_pBTStartingCopy, g_pBlockTable,
  969. DeviceInfo.wDataBlockNum * sizeof(u32));
  970. memcpy(g_pWearCounterCopy, g_pWearCounter,
  971. DeviceInfo.wDataBlockNum * sizeof(u8));
  972. if (DeviceInfo.MLCDevice)
  973. memcpy(g_pReadCounterCopy, g_pReadCounter,
  974. DeviceInfo.wDataBlockNum * sizeof(u16));
  975. g_pBTDelta_Free = g_pBTDelta;
  976. ftl_cmd_cnt = 0;
  977. g_pNextBlockTable = g_pBlockTableCopies;
  978. cp_back_buf_idx = 0;
  979. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  980. memcpy((void *)&cache_start_copy, (void *)&Cache,
  981. sizeof(struct flash_cache_tag));
  982. memset((void *)&int_cache, -1,
  983. sizeof(struct flash_cache_delta_list_tag) *
  984. (MAX_DESCS + MAX_CHANS));
  985. #endif
  986. return event_code;
  987. }
  988. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  989. * Function: glob_ftl_execute_cmds
  990. * Inputs: none
  991. * Outputs: none
  992. * Description: pass thru to LLD
  993. ***************************************************************/
  994. u16 glob_ftl_execute_cmds(void)
  995. {
  996. nand_dbg_print(NAND_DBG_TRACE,
  997. "glob_ftl_execute_cmds: ftl_cmd_cnt %u\n",
  998. (unsigned int)ftl_cmd_cnt);
  999. g_SBDCmdIndex = 0;
  1000. return glob_lld_execute_cmds();
  1001. }
  1002. #endif
  1003. #if !CMD_DMA
  1004. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1005. * Function: GLOB_FTL_Read Immediate
  1006. * Inputs: pointer to data
  1007. * address of data
  1008. * Outputs: PASS / FAIL
  1009. * Description: Reads one page of data into RAM directly from flash without
  1010. * using or disturbing cache.It is assumed this function is called
  1011. * with CMD-DMA disabled.
  1012. *****************************************************************/
  1013. int GLOB_FTL_Read_Immediate(u8 *read_data, u64 addr)
  1014. {
  1015. int wResult = FAIL;
  1016. u32 Block;
  1017. u16 Page;
  1018. u32 phy_blk;
  1019. u32 *pbt = (u32 *)g_pBlockTable;
  1020. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1021. __FILE__, __LINE__, __func__);
  1022. Block = BLK_FROM_ADDR(addr);
  1023. Page = PAGE_FROM_ADDR(addr, Block);
  1024. if (!IS_SPARE_BLOCK(Block))
  1025. return FAIL;
  1026. phy_blk = pbt[Block];
  1027. wResult = GLOB_LLD_Read_Page_Main(read_data, phy_blk, Page, 1);
  1028. if (DeviceInfo.MLCDevice) {
  1029. g_pReadCounter[phy_blk - DeviceInfo.wSpectraStartBlock]++;
  1030. if (g_pReadCounter[phy_blk - DeviceInfo.wSpectraStartBlock]
  1031. >= MAX_READ_COUNTER)
  1032. FTL_Read_Disturbance(phy_blk);
  1033. if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
  1034. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  1035. FTL_Write_IN_Progress_Block_Table_Page();
  1036. }
  1037. }
  1038. return wResult;
  1039. }
  1040. #endif
  1041. #ifdef SUPPORT_BIG_ENDIAN
  1042. /*********************************************************************
  1043. * Function: FTL_Invert_Block_Table
  1044. * Inputs: none
  1045. * Outputs: none
  1046. * Description: Re-format the block table in ram based on BIG_ENDIAN and
  1047. * LARGE_BLOCKNUM if necessary
  1048. **********************************************************************/
  1049. static void FTL_Invert_Block_Table(void)
  1050. {
  1051. u32 i;
  1052. u32 *pbt = (u32 *)g_pBlockTable;
  1053. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1054. __FILE__, __LINE__, __func__);
  1055. #ifdef SUPPORT_LARGE_BLOCKNUM
  1056. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  1057. pbt[i] = INVERTUINT32(pbt[i]);
  1058. g_pWearCounter[i] = INVERTUINT32(g_pWearCounter[i]);
  1059. }
  1060. #else
  1061. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  1062. pbt[i] = INVERTUINT16(pbt[i]);
  1063. g_pWearCounter[i] = INVERTUINT16(g_pWearCounter[i]);
  1064. }
  1065. #endif
  1066. }
  1067. #endif
  1068. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1069. * Function: GLOB_FTL_Flash_Init
  1070. * Inputs: none
  1071. * Outputs: PASS=0 / FAIL=0x01 (based on read ID)
  1072. * Description: The flash controller is initialized
  1073. * The flash device is reset
  1074. * Perform a flash READ ID command to confirm that a
  1075. * valid device is attached and active.
  1076. * The DeviceInfo structure gets filled in
  1077. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1078. int GLOB_FTL_Flash_Init(void)
  1079. {
  1080. int status = FAIL;
  1081. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1082. __FILE__, __LINE__, __func__);
  1083. g_SBDCmdIndex = 0;
  1084. status = GLOB_LLD_Flash_Init();
  1085. return status;
  1086. }
  1087. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1088. * Inputs: none
  1089. * Outputs: PASS=0 / FAIL=0x01 (based on read ID)
  1090. * Description: The flash controller is released
  1091. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1092. int GLOB_FTL_Flash_Release(void)
  1093. {
  1094. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1095. __FILE__, __LINE__, __func__);
  1096. return GLOB_LLD_Flash_Release();
  1097. }
  1098. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1099. * Function: GLOB_FTL_Cache_Release
  1100. * Inputs: none
  1101. * Outputs: none
  1102. * Description: release all allocated memory in GLOB_FTL_Init
  1103. * (allocated in GLOB_FTL_Init)
  1104. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1105. void GLOB_FTL_Cache_Release(void)
  1106. {
  1107. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1108. __FILE__, __LINE__, __func__);
  1109. free_memory();
  1110. }
  1111. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1112. * Function: FTL_Cache_If_Hit
  1113. * Inputs: Page Address
  1114. * Outputs: Block number/UNHIT BLOCK
  1115. * Description: Determines if the addressed page is in cache
  1116. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1117. static u16 FTL_Cache_If_Hit(u64 page_addr)
  1118. {
  1119. u16 item;
  1120. u64 addr;
  1121. int i;
  1122. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1123. __FILE__, __LINE__, __func__);
  1124. item = UNHIT_CACHE_ITEM;
  1125. for (i = 0; i < CACHE_ITEM_NUM; i++) {
  1126. addr = Cache.array[i].address;
  1127. if ((page_addr >= addr) &&
  1128. (page_addr < (addr + Cache.cache_item_size))) {
  1129. item = i;
  1130. break;
  1131. }
  1132. }
  1133. return item;
  1134. }
  1135. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1136. * Function: FTL_Calculate_LRU
  1137. * Inputs: None
  1138. * Outputs: None
  1139. * Description: Calculate the least recently block in a cache and record its
  1140. * index in LRU field.
  1141. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1142. static void FTL_Calculate_LRU(void)
  1143. {
  1144. u16 i, bCurrentLRU, bTempCount;
  1145. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1146. __FILE__, __LINE__, __func__);
  1147. bCurrentLRU = 0;
  1148. bTempCount = MAX_WORD_VALUE;
  1149. for (i = 0; i < CACHE_ITEM_NUM; i++) {
  1150. if (Cache.array[i].use_cnt < bTempCount) {
  1151. bCurrentLRU = i;
  1152. bTempCount = Cache.array[i].use_cnt;
  1153. }
  1154. }
  1155. Cache.LRU = bCurrentLRU;
  1156. }
  1157. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1158. * Function: FTL_Cache_Read_Page
  1159. * Inputs: pointer to read buffer, logical address and cache item number
  1160. * Outputs: None
  1161. * Description: Read the page from the cached block addressed by blocknumber
  1162. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1163. static void FTL_Cache_Read_Page(u8 *data_buf, u64 logic_addr, u16 cache_item)
  1164. {
  1165. u8 *start_addr;
  1166. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1167. __FILE__, __LINE__, __func__);
  1168. start_addr = Cache.array[cache_item].buf;
  1169. start_addr += (u32)(((logic_addr - Cache.array[cache_item].address) >>
  1170. DeviceInfo.nBitsInPageDataSize) * DeviceInfo.wPageDataSize);
  1171. #if CMD_DMA
  1172. GLOB_LLD_MemCopy_CMD(data_buf, start_addr,
  1173. DeviceInfo.wPageDataSize, 0);
  1174. ftl_cmd_cnt++;
  1175. #else
  1176. memcpy(data_buf, start_addr, DeviceInfo.wPageDataSize);
  1177. #endif
  1178. if (Cache.array[cache_item].use_cnt < MAX_WORD_VALUE)
  1179. Cache.array[cache_item].use_cnt++;
  1180. }
  1181. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1182. * Function: FTL_Cache_Read_All
  1183. * Inputs: pointer to read buffer,block address
  1184. * Outputs: PASS=0 / FAIL =1
  1185. * Description: It reads pages in cache
  1186. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1187. static int FTL_Cache_Read_All(u8 *pData, u64 phy_addr)
  1188. {
  1189. int wResult = PASS;
  1190. u32 Block;
  1191. u32 lba;
  1192. u16 Page;
  1193. u16 PageCount;
  1194. u32 *pbt = (u32 *)g_pBlockTable;
  1195. u32 i;
  1196. Block = BLK_FROM_ADDR(phy_addr);
  1197. Page = PAGE_FROM_ADDR(phy_addr, Block);
  1198. PageCount = Cache.pages_per_item;
  1199. nand_dbg_print(NAND_DBG_DEBUG,
  1200. "%s, Line %d, Function: %s, Block: 0x%x\n",
  1201. __FILE__, __LINE__, __func__, Block);
  1202. lba = 0xffffffff;
  1203. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  1204. if ((pbt[i] & (~BAD_BLOCK)) == Block) {
  1205. lba = i;
  1206. if (IS_SPARE_BLOCK(i) || IS_BAD_BLOCK(i) ||
  1207. IS_DISCARDED_BLOCK(i)) {
  1208. /* Add by yunpeng -2008.12.3 */
  1209. #if CMD_DMA
  1210. GLOB_LLD_MemCopy_CMD(pData, g_temp_buf,
  1211. PageCount * DeviceInfo.wPageDataSize, 0);
  1212. ftl_cmd_cnt++;
  1213. #else
  1214. memset(pData, 0xFF,
  1215. PageCount * DeviceInfo.wPageDataSize);
  1216. #endif
  1217. return wResult;
  1218. } else {
  1219. continue; /* break ?? */
  1220. }
  1221. }
  1222. }
  1223. if (0xffffffff == lba)
  1224. printk(KERN_ERR "FTL_Cache_Read_All: Block is not found in BT\n");
  1225. #if CMD_DMA
  1226. wResult = GLOB_LLD_Read_Page_Main_cdma(pData, Block, Page,
  1227. PageCount, LLD_CMD_FLAG_MODE_CDMA);
  1228. if (DeviceInfo.MLCDevice) {
  1229. g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock]++;
  1230. nand_dbg_print(NAND_DBG_DEBUG,
  1231. "Read Counter modified in ftl_cmd_cnt %u"
  1232. " Block %u Counter%u\n",
  1233. ftl_cmd_cnt, (unsigned int)Block,
  1234. g_pReadCounter[Block -
  1235. DeviceInfo.wSpectraStartBlock]);
  1236. p_BTableChangesDelta =
  1237. (struct BTableChangesDelta *)g_pBTDelta_Free;
  1238. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  1239. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  1240. p_BTableChangesDelta->RC_Index =
  1241. Block - DeviceInfo.wSpectraStartBlock;
  1242. p_BTableChangesDelta->RC_Entry_Value =
  1243. g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock];
  1244. p_BTableChangesDelta->ValidFields = 0xC0;
  1245. ftl_cmd_cnt++;
  1246. if (g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock] >=
  1247. MAX_READ_COUNTER)
  1248. FTL_Read_Disturbance(Block);
  1249. if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
  1250. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  1251. FTL_Write_IN_Progress_Block_Table_Page();
  1252. }
  1253. } else {
  1254. ftl_cmd_cnt++;
  1255. }
  1256. #else
  1257. wResult = GLOB_LLD_Read_Page_Main(pData, Block, Page, PageCount);
  1258. if (wResult == FAIL)
  1259. return wResult;
  1260. if (DeviceInfo.MLCDevice) {
  1261. g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock]++;
  1262. if (g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock] >=
  1263. MAX_READ_COUNTER)
  1264. FTL_Read_Disturbance(Block);
  1265. if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
  1266. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  1267. FTL_Write_IN_Progress_Block_Table_Page();
  1268. }
  1269. }
  1270. #endif
  1271. return wResult;
  1272. }
  1273. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1274. * Function: FTL_Cache_Write_All
  1275. * Inputs: pointer to cache in sys memory
  1276. * address of free block in flash
  1277. * Outputs: PASS=0 / FAIL=1
  1278. * Description: writes all the pages of the block in cache to flash
  1279. *
  1280. * NOTE:need to make sure this works ok when cache is limited
  1281. * to a partial block. This is where copy-back would be
  1282. * activated. This would require knowing which pages in the
  1283. * cached block are clean/dirty.Right now we only know if
  1284. * the whole block is clean/dirty.
  1285. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1286. static int FTL_Cache_Write_All(u8 *pData, u64 blk_addr)
  1287. {
  1288. u16 wResult = PASS;
  1289. u32 Block;
  1290. u16 Page;
  1291. u16 PageCount;
  1292. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1293. __FILE__, __LINE__, __func__);
  1294. nand_dbg_print(NAND_DBG_DEBUG, "This block %d going to be written "
  1295. "on %d\n", cache_block_to_write,
  1296. (u32)(blk_addr >> DeviceInfo.nBitsInBlockDataSize));
  1297. Block = BLK_FROM_ADDR(blk_addr);
  1298. Page = PAGE_FROM_ADDR(blk_addr, Block);
  1299. PageCount = Cache.pages_per_item;
  1300. #if CMD_DMA
  1301. if (FAIL == GLOB_LLD_Write_Page_Main_cdma(pData,
  1302. Block, Page, PageCount)) {
  1303. nand_dbg_print(NAND_DBG_WARN,
  1304. "NAND Program fail in %s, Line %d, "
  1305. "Function: %s, new Bad Block %d generated! "
  1306. "Need Bad Block replacing.\n",
  1307. __FILE__, __LINE__, __func__, Block);
  1308. wResult = FAIL;
  1309. }
  1310. ftl_cmd_cnt++;
  1311. #else
  1312. if (FAIL == GLOB_LLD_Write_Page_Main(pData, Block, Page, PageCount)) {
  1313. nand_dbg_print(NAND_DBG_WARN, "NAND Program fail in %s,"
  1314. " Line %d, Function %s, new Bad Block %d generated!"
  1315. "Need Bad Block replacing.\n",
  1316. __FILE__, __LINE__, __func__, Block);
  1317. wResult = FAIL;
  1318. }
  1319. #endif
  1320. return wResult;
  1321. }
  1322. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1323. * Function: FTL_Copy_Block
  1324. * Inputs: source block address
  1325. * Destination block address
  1326. * Outputs: PASS=0 / FAIL=1
  1327. * Description: used only for static wear leveling to move the block
  1328. * containing static data to new blocks(more worn)
  1329. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1330. int FTL_Copy_Block(u64 old_blk_addr, u64 blk_addr)
  1331. {
  1332. int i, r1, r2, wResult = PASS;
  1333. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1334. __FILE__, __LINE__, __func__);
  1335. for (i = 0; i < DeviceInfo.wPagesPerBlock; i += Cache.pages_per_item) {
  1336. r1 = FTL_Cache_Read_All(g_pTempBuf, old_blk_addr +
  1337. i * DeviceInfo.wPageDataSize);
  1338. r2 = FTL_Cache_Write_All(g_pTempBuf, blk_addr +
  1339. i * DeviceInfo.wPageDataSize);
  1340. if ((ERR == r1) || (FAIL == r2)) {
  1341. wResult = FAIL;
  1342. break;
  1343. }
  1344. }
  1345. return wResult;
  1346. }
  1347. /* Search the block table to find out the least wear block and then return it */
  1348. static u32 find_least_worn_blk_for_l2_cache(void)
  1349. {
  1350. int i;
  1351. u32 *pbt = (u32 *)g_pBlockTable;
  1352. u8 least_wear_cnt = MAX_BYTE_VALUE;
  1353. u32 least_wear_blk_idx = MAX_U32_VALUE;
  1354. u32 phy_idx;
  1355. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  1356. if (IS_SPARE_BLOCK(i)) {
  1357. phy_idx = (u32)((~BAD_BLOCK) & pbt[i]);
  1358. if (phy_idx > DeviceInfo.wSpectraEndBlock)
  1359. printk(KERN_ERR "find_least_worn_blk_for_l2_cache: "
  1360. "Too big phy block num (%d)\n", phy_idx);
  1361. if (g_pWearCounter[phy_idx -DeviceInfo.wSpectraStartBlock] < least_wear_cnt) {
  1362. least_wear_cnt = g_pWearCounter[phy_idx - DeviceInfo.wSpectraStartBlock];
  1363. least_wear_blk_idx = i;
  1364. }
  1365. }
  1366. }
  1367. nand_dbg_print(NAND_DBG_WARN,
  1368. "find_least_worn_blk_for_l2_cache: "
  1369. "find block %d with least worn counter (%d)\n",
  1370. least_wear_blk_idx, least_wear_cnt);
  1371. return least_wear_blk_idx;
  1372. }
  1373. /* Get blocks for Level2 Cache */
  1374. static int get_l2_cache_blks(void)
  1375. {
  1376. int n;
  1377. u32 blk;
  1378. u32 *pbt = (u32 *)g_pBlockTable;
  1379. for (n = 0; n < BLK_NUM_FOR_L2_CACHE; n++) {
  1380. blk = find_least_worn_blk_for_l2_cache();
  1381. if (blk >= DeviceInfo.wDataBlockNum) {
  1382. nand_dbg_print(NAND_DBG_WARN,
  1383. "find_least_worn_blk_for_l2_cache: "
  1384. "No enough free NAND blocks (n: %d) for L2 Cache!\n", n);
  1385. return FAIL;
  1386. }
  1387. /* Tag the free block as discard in block table */
  1388. pbt[blk] = (pbt[blk] & (~BAD_BLOCK)) | DISCARD_BLOCK;
  1389. /* Add the free block to the L2 Cache block array */
  1390. cache_l2.blk_array[n] = pbt[blk] & (~BAD_BLOCK);
  1391. }
  1392. return PASS;
  1393. }
  1394. static int erase_l2_cache_blocks(void)
  1395. {
  1396. int i, ret = PASS;
  1397. u32 pblk, lblk = BAD_BLOCK;
  1398. u64 addr;
  1399. u32 *pbt = (u32 *)g_pBlockTable;
  1400. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  1401. __FILE__, __LINE__, __func__);
  1402. for (i = 0; i < BLK_NUM_FOR_L2_CACHE; i++) {
  1403. pblk = cache_l2.blk_array[i];
  1404. /* If the L2 cache block is invalid, then just skip it */
  1405. if (MAX_U32_VALUE == pblk)
  1406. continue;
  1407. BUG_ON(pblk > DeviceInfo.wSpectraEndBlock);
  1408. addr = (u64)pblk << DeviceInfo.nBitsInBlockDataSize;
  1409. if (PASS == GLOB_FTL_Block_Erase(addr)) {
  1410. /* Get logical block number of the erased block */
  1411. lblk = FTL_Get_Block_Index(pblk);
  1412. BUG_ON(BAD_BLOCK == lblk);
  1413. /* Tag it as free in the block table */
  1414. pbt[lblk] &= (u32)(~DISCARD_BLOCK);
  1415. pbt[lblk] |= (u32)(SPARE_BLOCK);
  1416. } else {
  1417. MARK_BLOCK_AS_BAD(pbt[lblk]);
  1418. ret = ERR;
  1419. }
  1420. }
  1421. return ret;
  1422. }
  1423. /*
  1424. * Merge the valid data page in the L2 cache blocks into NAND.
  1425. */
  1426. static int flush_l2_cache(void)
  1427. {
  1428. struct list_head *p;
  1429. struct spectra_l2_cache_list *pnd, *tmp_pnd;
  1430. u32 *pbt = (u32 *)g_pBlockTable;
  1431. u32 phy_blk, l2_blk;
  1432. u64 addr;
  1433. u16 l2_page;
  1434. int i, ret = PASS;
  1435. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  1436. __FILE__, __LINE__, __func__);
  1437. if (list_empty(&cache_l2.table.list)) /* No data to flush */
  1438. return ret;
  1439. //dump_cache_l2_table();
  1440. if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
  1441. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  1442. FTL_Write_IN_Progress_Block_Table_Page();
  1443. }
  1444. list_for_each(p, &cache_l2.table.list) {
  1445. pnd = list_entry(p, struct spectra_l2_cache_list, list);
  1446. if (IS_SPARE_BLOCK(pnd->logical_blk_num) ||
  1447. IS_BAD_BLOCK(pnd->logical_blk_num) ||
  1448. IS_DISCARDED_BLOCK(pnd->logical_blk_num)) {
  1449. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d\n", __FILE__, __LINE__);
  1450. memset(cache_l2_blk_buf, 0xff, DeviceInfo.wPagesPerBlock * DeviceInfo.wPageDataSize);
  1451. } else {
  1452. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d\n", __FILE__, __LINE__);
  1453. phy_blk = pbt[pnd->logical_blk_num] & (~BAD_BLOCK);
  1454. ret = GLOB_LLD_Read_Page_Main(cache_l2_blk_buf,
  1455. phy_blk, 0, DeviceInfo.wPagesPerBlock);
  1456. if (ret == FAIL) {
  1457. printk(KERN_ERR "Read NAND page fail in %s, Line %d\n", __FILE__, __LINE__);
  1458. }
  1459. }
  1460. for (i = 0; i < DeviceInfo.wPagesPerBlock; i++) {
  1461. if (pnd->pages_array[i] != MAX_U32_VALUE) {
  1462. l2_blk = cache_l2.blk_array[(pnd->pages_array[i] >> 16) & 0xffff];
  1463. l2_page = pnd->pages_array[i] & 0xffff;
  1464. ret = GLOB_LLD_Read_Page_Main(cache_l2_page_buf, l2_blk, l2_page, 1);
  1465. if (ret == FAIL) {
  1466. printk(KERN_ERR "Read NAND page fail in %s, Line %d\n", __FILE__, __LINE__);
  1467. }
  1468. memcpy(cache_l2_blk_buf + i * DeviceInfo.wPageDataSize, cache_l2_page_buf, DeviceInfo.wPageDataSize);
  1469. }
  1470. }
  1471. /* Find a free block and tag the original block as discarded */
  1472. addr = (u64)pnd->logical_blk_num << DeviceInfo.nBitsInBlockDataSize;
  1473. ret = FTL_Replace_Block(addr);
  1474. if (ret == FAIL) {
  1475. printk(KERN_ERR "FTL_Replace_Block fail in %s, Line %d\n", __FILE__, __LINE__);
  1476. }
  1477. /* Write back the updated data into NAND */
  1478. phy_blk = pbt[pnd->logical_blk_num] & (~BAD_BLOCK);
  1479. if (FAIL == GLOB_LLD_Write_Page_Main(cache_l2_blk_buf, phy_blk, 0, DeviceInfo.wPagesPerBlock)) {
  1480. nand_dbg_print(NAND_DBG_WARN,
  1481. "Program NAND block %d fail in %s, Line %d\n",
  1482. phy_blk, __FILE__, __LINE__);
  1483. /* This may not be really a bad block. So just tag it as discarded. */
  1484. /* Then it has a chance to be erased when garbage collection. */
  1485. /* If it is really bad, then the erase will fail and it will be marked */
  1486. /* as bad then. Otherwise it will be marked as free and can be used again */
  1487. MARK_BLK_AS_DISCARD(pbt[pnd->logical_blk_num]);
  1488. /* Find another free block and write it again */
  1489. FTL_Replace_Block(addr);
  1490. phy_blk = pbt[pnd->logical_blk_num] & (~BAD_BLOCK);
  1491. if (FAIL == GLOB_LLD_Write_Page_Main(cache_l2_blk_buf, phy_blk, 0, DeviceInfo.wPagesPerBlock)) {
  1492. printk(KERN_ERR "Failed to write back block %d when flush L2 cache."
  1493. "Some data will be lost!\n", phy_blk);
  1494. MARK_BLOCK_AS_BAD(pbt[pnd->logical_blk_num]);
  1495. }
  1496. } else {
  1497. /* tag the new free block as used block */
  1498. pbt[pnd->logical_blk_num] &= (~SPARE_BLOCK);
  1499. }
  1500. }
  1501. /* Destroy the L2 Cache table and free the memory of all nodes */
  1502. list_for_each_entry_safe(pnd, tmp_pnd, &cache_l2.table.list, list) {
  1503. list_del(&pnd->list);
  1504. kfree(pnd);
  1505. }
  1506. /* Erase discard L2 cache blocks */
  1507. if (erase_l2_cache_blocks() != PASS)
  1508. nand_dbg_print(NAND_DBG_WARN,
  1509. " Erase L2 cache blocks error in %s, Line %d\n",
  1510. __FILE__, __LINE__);
  1511. /* Init the Level2 Cache data structure */
  1512. for (i = 0; i < BLK_NUM_FOR_L2_CACHE; i++)
  1513. cache_l2.blk_array[i] = MAX_U32_VALUE;
  1514. cache_l2.cur_blk_idx = 0;
  1515. cache_l2.cur_page_num = 0;
  1516. INIT_LIST_HEAD(&cache_l2.table.list);
  1517. cache_l2.table.logical_blk_num = MAX_U32_VALUE;
  1518. return ret;
  1519. }
  1520. /*
  1521. * Write back a changed victim cache item to the Level2 Cache
  1522. * and update the L2 Cache table to map the change.
  1523. * If the L2 Cache is full, then start to do the L2 Cache flush.
  1524. */
  1525. static int write_back_to_l2_cache(u8 *buf, u64 logical_addr)
  1526. {
  1527. u32 logical_blk_num;
  1528. u16 logical_page_num;
  1529. struct list_head *p;
  1530. struct spectra_l2_cache_list *pnd, *pnd_new;
  1531. u32 node_size;
  1532. int i, found;
  1533. nand_dbg_print(NAND_DBG_DEBUG, "%s, Line %d, Function: %s\n",
  1534. __FILE__, __LINE__, __func__);
  1535. /*
  1536. * If Level2 Cache table is empty, then it means either:
  1537. * 1. This is the first time that the function called after FTL_init
  1538. * or
  1539. * 2. The Level2 Cache has just been flushed
  1540. *
  1541. * So, 'steal' some free blocks from NAND for L2 Cache using
  1542. * by just mask them as discard in the block table
  1543. */
  1544. if (list_empty(&cache_l2.table.list)) {
  1545. BUG_ON(cache_l2.cur_blk_idx != 0);
  1546. BUG_ON(cache_l2.cur_page_num!= 0);
  1547. BUG_ON(cache_l2.table.logical_blk_num != MAX_U32_VALUE);
  1548. if (FAIL == get_l2_cache_blks()) {
  1549. GLOB_FTL_Garbage_Collection();
  1550. if (FAIL == get_l2_cache_blks()) {
  1551. printk(KERN_ALERT "Fail to get L2 cache blks!\n");
  1552. return FAIL;
  1553. }
  1554. }
  1555. }
  1556. logical_blk_num = BLK_FROM_ADDR(logical_addr);
  1557. logical_page_num = PAGE_FROM_ADDR(logical_addr, logical_blk_num);
  1558. BUG_ON(logical_blk_num == MAX_U32_VALUE);
  1559. /* Write the cache item data into the current position of L2 Cache */
  1560. #if CMD_DMA
  1561. /*
  1562. * TODO
  1563. */
  1564. #else
  1565. if (FAIL == GLOB_LLD_Write_Page_Main(buf,
  1566. cache_l2.blk_array[cache_l2.cur_blk_idx],
  1567. cache_l2.cur_page_num, 1)) {
  1568. nand_dbg_print(NAND_DBG_WARN, "NAND Program fail in "
  1569. "%s, Line %d, new Bad Block %d generated!\n",
  1570. __FILE__, __LINE__,
  1571. cache_l2.blk_array[cache_l2.cur_blk_idx]);
  1572. /* TODO: tag the current block as bad and try again */
  1573. return FAIL;
  1574. }
  1575. #endif
  1576. /*
  1577. * Update the L2 Cache table.
  1578. *
  1579. * First seaching in the table to see whether the logical block
  1580. * has been mapped. If not, then kmalloc a new node for the
  1581. * logical block, fill data, and then insert it to the list.
  1582. * Otherwise, just update the mapped node directly.
  1583. */
  1584. found = 0;
  1585. list_for_each(p, &cache_l2.table.list) {
  1586. pnd = list_entry(p, struct spectra_l2_cache_list, list);
  1587. if (pnd->logical_blk_num == logical_blk_num) {
  1588. pnd->pages_array[logical_page_num] =
  1589. (cache_l2.cur_blk_idx << 16) |
  1590. cache_l2.cur_page_num;
  1591. found = 1;
  1592. break;
  1593. }
  1594. }
  1595. if (!found) { /* Create new node for the logical block here */
  1596. /* The logical pages to physical pages map array is
  1597. * located at the end of struct spectra_l2_cache_list.
  1598. */
  1599. node_size = sizeof(struct spectra_l2_cache_list) +
  1600. sizeof(u32) * DeviceInfo.wPagesPerBlock;
  1601. pnd_new = kmalloc(node_size, GFP_ATOMIC);
  1602. if (!pnd_new) {
  1603. printk(KERN_ERR "Failed to kmalloc in %s Line %d\n",
  1604. __FILE__, __LINE__);
  1605. /*
  1606. * TODO: Need to flush all the L2 cache into NAND ASAP
  1607. * since no memory available here
  1608. */
  1609. }
  1610. pnd_new->logical_blk_num = logical_blk_num;
  1611. for (i = 0; i < DeviceInfo.wPagesPerBlock; i++)
  1612. pnd_new->pages_array[i] = MAX_U32_VALUE;
  1613. pnd_new->pages_array[logical_page_num] =
  1614. (cache_l2.cur_blk_idx << 16) | cache_l2.cur_page_num;
  1615. list_add(&pnd_new->list, &cache_l2.table.list);
  1616. }
  1617. /* Increasing the current position pointer of the L2 Cache */
  1618. cache_l2.cur_page_num++;
  1619. if (cache_l2.cur_page_num >= DeviceInfo.wPagesPerBlock) {
  1620. cache_l2.cur_blk_idx++;
  1621. if (cache_l2.cur_blk_idx >= BLK_NUM_FOR_L2_CACHE) {
  1622. /* The L2 Cache is full. Need to flush it now */
  1623. nand_dbg_print(NAND_DBG_WARN,
  1624. "L2 Cache is full, will start to flush it\n");
  1625. flush_l2_cache();
  1626. } else {
  1627. cache_l2.cur_page_num = 0;
  1628. }
  1629. }
  1630. return PASS;
  1631. }
  1632. /*
  1633. * Search in the Level2 Cache table to find the cache item.
  1634. * If find, read the data from the NAND page of L2 Cache,
  1635. * Otherwise, return FAIL.
  1636. */
  1637. static int search_l2_cache(u8 *buf, u64 logical_addr)
  1638. {
  1639. u32 logical_blk_num;
  1640. u16 logical_page_num;
  1641. struct list_head *p;
  1642. struct spectra_l2_cache_list *pnd;
  1643. u32 tmp = MAX_U32_VALUE;
  1644. u32 phy_blk;
  1645. u16 phy_page;
  1646. int ret = FAIL;
  1647. logical_blk_num = BLK_FROM_ADDR(logical_addr);
  1648. logical_page_num = PAGE_FROM_ADDR(logical_addr, logical_blk_num);
  1649. list_for_each(p, &cache_l2.table.list) {
  1650. pnd = list_entry(p, struct spectra_l2_cache_list, list);
  1651. if (pnd->logical_blk_num == logical_blk_num) {
  1652. tmp = pnd->pages_array[logical_page_num];
  1653. break;
  1654. }
  1655. }
  1656. if (tmp != MAX_U32_VALUE) { /* Found valid map */
  1657. phy_blk = cache_l2.blk_array[(tmp >> 16) & 0xFFFF];
  1658. phy_page = tmp & 0xFFFF;
  1659. #if CMD_DMA
  1660. /* TODO */
  1661. #else
  1662. ret = GLOB_LLD_Read_Page_Main(buf, phy_blk, phy_page, 1);
  1663. #endif
  1664. }
  1665. return ret;
  1666. }
  1667. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1668. * Function: FTL_Cache_Write_Page
  1669. * Inputs: Pointer to buffer, page address, cache block number
  1670. * Outputs: PASS=0 / FAIL=1
  1671. * Description: It writes the data in Cache Block
  1672. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1673. static void FTL_Cache_Write_Page(u8 *pData, u64 page_addr,
  1674. u8 cache_blk, u16 flag)
  1675. {
  1676. u8 *pDest;
  1677. u64 addr;
  1678. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1679. __FILE__, __LINE__, __func__);
  1680. addr = Cache.array[cache_blk].address;
  1681. pDest = Cache.array[cache_blk].buf;
  1682. pDest += (unsigned long)(page_addr - addr);
  1683. Cache.array[cache_blk].changed = SET;
  1684. #if CMD_DMA
  1685. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  1686. int_cache[ftl_cmd_cnt].item = cache_blk;
  1687. int_cache[ftl_cmd_cnt].cache.address =
  1688. Cache.array[cache_blk].address;
  1689. int_cache[ftl_cmd_cnt].cache.changed =
  1690. Cache.array[cache_blk].changed;
  1691. #endif
  1692. GLOB_LLD_MemCopy_CMD(pDest, pData, DeviceInfo.wPageDataSize, flag);
  1693. ftl_cmd_cnt++;
  1694. #else
  1695. memcpy(pDest, pData, DeviceInfo.wPageDataSize);
  1696. #endif
  1697. if (Cache.array[cache_blk].use_cnt < MAX_WORD_VALUE)
  1698. Cache.array[cache_blk].use_cnt++;
  1699. }
  1700. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1701. * Function: FTL_Cache_Write
  1702. * Inputs: none
  1703. * Outputs: PASS=0 / FAIL=1
  1704. * Description: It writes least frequently used Cache block to flash if it
  1705. * has been changed
  1706. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1707. static int FTL_Cache_Write(void)
  1708. {
  1709. int i, bResult = PASS;
  1710. u16 bNO, least_count = 0xFFFF;
  1711. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1712. __FILE__, __LINE__, __func__);
  1713. FTL_Calculate_LRU();
  1714. bNO = Cache.LRU;
  1715. nand_dbg_print(NAND_DBG_DEBUG, "FTL_Cache_Write: "
  1716. "Least used cache block is %d\n", bNO);
  1717. if (Cache.array[bNO].changed != SET)
  1718. return bResult;
  1719. nand_dbg_print(NAND_DBG_DEBUG, "FTL_Cache_Write: Cache"
  1720. " Block %d containing logical block %d is dirty\n",
  1721. bNO,
  1722. (u32)(Cache.array[bNO].address >>
  1723. DeviceInfo.nBitsInBlockDataSize));
  1724. #if CMD_DMA
  1725. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  1726. int_cache[ftl_cmd_cnt].item = bNO;
  1727. int_cache[ftl_cmd_cnt].cache.address =
  1728. Cache.array[bNO].address;
  1729. int_cache[ftl_cmd_cnt].cache.changed = CLEAR;
  1730. #endif
  1731. #endif
  1732. bResult = write_back_to_l2_cache(Cache.array[bNO].buf,
  1733. Cache.array[bNO].address);
  1734. if (bResult != ERR)
  1735. Cache.array[bNO].changed = CLEAR;
  1736. least_count = Cache.array[bNO].use_cnt;
  1737. for (i = 0; i < CACHE_ITEM_NUM; i++) {
  1738. if (i == bNO)
  1739. continue;
  1740. if (Cache.array[i].use_cnt > 0)
  1741. Cache.array[i].use_cnt -= least_count;
  1742. }
  1743. return bResult;
  1744. }
  1745. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1746. * Function: FTL_Cache_Read
  1747. * Inputs: Page address
  1748. * Outputs: PASS=0 / FAIL=1
  1749. * Description: It reads the block from device in Cache Block
  1750. * Set the LRU count to 1
  1751. * Mark the Cache Block as clean
  1752. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1753. static int FTL_Cache_Read(u64 logical_addr)
  1754. {
  1755. u64 item_addr, phy_addr;
  1756. u16 num;
  1757. int ret;
  1758. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1759. __FILE__, __LINE__, __func__);
  1760. num = Cache.LRU; /* The LRU cache item will be overwritten */
  1761. item_addr = (u64)GLOB_u64_Div(logical_addr, Cache.cache_item_size) *
  1762. Cache.cache_item_size;
  1763. Cache.array[num].address = item_addr;
  1764. Cache.array[num].use_cnt = 1;
  1765. Cache.array[num].changed = CLEAR;
  1766. #if CMD_DMA
  1767. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  1768. int_cache[ftl_cmd_cnt].item = num;
  1769. int_cache[ftl_cmd_cnt].cache.address =
  1770. Cache.array[num].address;
  1771. int_cache[ftl_cmd_cnt].cache.changed =
  1772. Cache.array[num].changed;
  1773. #endif
  1774. #endif
  1775. /*
  1776. * Search in L2 Cache. If hit, fill data into L1 Cache item buffer,
  1777. * Otherwise, read it from NAND
  1778. */
  1779. ret = search_l2_cache(Cache.array[num].buf, logical_addr);
  1780. if (PASS == ret) /* Hit in L2 Cache */
  1781. return ret;
  1782. /* Compute the physical start address of NAND device according to */
  1783. /* the logical start address of the cache item (LRU cache item) */
  1784. phy_addr = FTL_Get_Physical_Block_Addr(item_addr) +
  1785. GLOB_u64_Remainder(item_addr, 2);
  1786. return FTL_Cache_Read_All(Cache.array[num].buf, phy_addr);
  1787. }
  1788. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1789. * Function: FTL_Check_Block_Table
  1790. * Inputs: ?
  1791. * Outputs: PASS=0 / FAIL=1
  1792. * Description: It checks the correctness of each block table entry
  1793. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1794. static int FTL_Check_Block_Table(int wOldTable)
  1795. {
  1796. u32 i;
  1797. int wResult = PASS;
  1798. u32 blk_idx;
  1799. u32 *pbt = (u32 *)g_pBlockTable;
  1800. u8 *pFlag = flag_check_blk_table;
  1801. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1802. __FILE__, __LINE__, __func__);
  1803. if (NULL != pFlag) {
  1804. memset(pFlag, FAIL, DeviceInfo.wDataBlockNum);
  1805. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  1806. blk_idx = (u32)(pbt[i] & (~BAD_BLOCK));
  1807. /*
  1808. * 20081006/KBV - Changed to pFlag[i] reference
  1809. * to avoid buffer overflow
  1810. */
  1811. /*
  1812. * 2008-10-20 Yunpeng Note: This change avoid
  1813. * buffer overflow, but changed function of
  1814. * the code, so it should be re-write later
  1815. */
  1816. if ((blk_idx > DeviceInfo.wSpectraEndBlock) ||
  1817. PASS == pFlag[i]) {
  1818. wResult = FAIL;
  1819. break;
  1820. } else {
  1821. pFlag[i] = PASS;
  1822. }
  1823. }
  1824. }
  1825. return wResult;
  1826. }
  1827. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1828. * Function: FTL_Write_Block_Table
  1829. * Inputs: flasg
  1830. * Outputs: 0=Block Table was updated. No write done. 1=Block write needs to
  1831. * happen. -1 Error
  1832. * Description: It writes the block table
  1833. * Block table always mapped to LBA 0 which inturn mapped
  1834. * to any physical block
  1835. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1836. static int FTL_Write_Block_Table(int wForce)
  1837. {
  1838. u32 *pbt = (u32 *)g_pBlockTable;
  1839. int wSuccess = PASS;
  1840. u32 wTempBlockTableIndex;
  1841. u16 bt_pages, new_bt_offset;
  1842. u8 blockchangeoccured = 0;
  1843. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  1844. __FILE__, __LINE__, __func__);
  1845. bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
  1846. if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus)
  1847. return 0;
  1848. if (PASS == wForce) {
  1849. g_wBlockTableOffset =
  1850. (u16)(DeviceInfo.wPagesPerBlock - bt_pages);
  1851. #if CMD_DMA
  1852. p_BTableChangesDelta =
  1853. (struct BTableChangesDelta *)g_pBTDelta_Free;
  1854. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  1855. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  1856. p_BTableChangesDelta->g_wBlockTableOffset =
  1857. g_wBlockTableOffset;
  1858. p_BTableChangesDelta->ValidFields = 0x01;
  1859. #endif
  1860. }
  1861. nand_dbg_print(NAND_DBG_DEBUG,
  1862. "Inside FTL_Write_Block_Table: block %d Page:%d\n",
  1863. g_wBlockTableIndex, g_wBlockTableOffset);
  1864. do {
  1865. new_bt_offset = g_wBlockTableOffset + bt_pages + 1;
  1866. if ((0 == (new_bt_offset % DeviceInfo.wPagesPerBlock)) ||
  1867. (new_bt_offset > DeviceInfo.wPagesPerBlock) ||
  1868. (FAIL == wSuccess)) {
  1869. wTempBlockTableIndex = FTL_Replace_Block_Table();
  1870. if (BAD_BLOCK == wTempBlockTableIndex)
  1871. return ERR;
  1872. if (!blockchangeoccured) {
  1873. bt_block_changed = 1;
  1874. blockchangeoccured = 1;
  1875. }
  1876. g_wBlockTableIndex = wTempBlockTableIndex;
  1877. g_wBlockTableOffset = 0;
  1878. pbt[BLOCK_TABLE_INDEX] = g_wBlockTableIndex;
  1879. #if CMD_DMA
  1880. p_BTableChangesDelta =
  1881. (struct BTableChangesDelta *)g_pBTDelta_Free;
  1882. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  1883. p_BTableChangesDelta->ftl_cmd_cnt =
  1884. ftl_cmd_cnt;
  1885. p_BTableChangesDelta->g_wBlockTableOffset =
  1886. g_wBlockTableOffset;
  1887. p_BTableChangesDelta->g_wBlockTableIndex =
  1888. g_wBlockTableIndex;
  1889. p_BTableChangesDelta->ValidFields = 0x03;
  1890. p_BTableChangesDelta =
  1891. (struct BTableChangesDelta *)g_pBTDelta_Free;
  1892. g_pBTDelta_Free +=
  1893. sizeof(struct BTableChangesDelta);
  1894. p_BTableChangesDelta->ftl_cmd_cnt =
  1895. ftl_cmd_cnt;
  1896. p_BTableChangesDelta->BT_Index =
  1897. BLOCK_TABLE_INDEX;
  1898. p_BTableChangesDelta->BT_Entry_Value =
  1899. pbt[BLOCK_TABLE_INDEX];
  1900. p_BTableChangesDelta->ValidFields = 0x0C;
  1901. #endif
  1902. }
  1903. wSuccess = FTL_Write_Block_Table_Data();
  1904. if (FAIL == wSuccess)
  1905. MARK_BLOCK_AS_BAD(pbt[BLOCK_TABLE_INDEX]);
  1906. } while (FAIL == wSuccess);
  1907. g_cBlockTableStatus = CURRENT_BLOCK_TABLE;
  1908. return 1;
  1909. }
  1910. static int force_format_nand(void)
  1911. {
  1912. u32 i;
  1913. /* Force erase the whole unprotected physical partiton of NAND */
  1914. printk(KERN_ALERT "Start to force erase whole NAND device ...\n");
  1915. printk(KERN_ALERT "From phyical block %d to %d\n",
  1916. DeviceInfo.wSpectraStartBlock, DeviceInfo.wSpectraEndBlock);
  1917. for (i = DeviceInfo.wSpectraStartBlock; i <= DeviceInfo.wSpectraEndBlock; i++) {
  1918. if (GLOB_LLD_Erase_Block(i))
  1919. printk(KERN_ERR "Failed to force erase NAND block %d\n", i);
  1920. }
  1921. printk(KERN_ALERT "Force Erase ends. Please reboot the system ...\n");
  1922. while(1);
  1923. return PASS;
  1924. }
  1925. int GLOB_FTL_Flash_Format(void)
  1926. {
  1927. //return FTL_Format_Flash(1);
  1928. return force_format_nand();
  1929. }
  1930. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  1931. * Function: FTL_Search_Block_Table_IN_Block
  1932. * Inputs: Block Number
  1933. * Pointer to page
  1934. * Outputs: PASS / FAIL
  1935. * Page contatining the block table
  1936. * Description: It searches the block table in the block
  1937. * passed as an argument.
  1938. *
  1939. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  1940. static int FTL_Search_Block_Table_IN_Block(u32 BT_Block,
  1941. u8 BT_Tag, u16 *Page)
  1942. {
  1943. u16 i, j, k;
  1944. u16 Result = PASS;
  1945. u16 Last_IPF = 0;
  1946. u8 BT_Found = 0;
  1947. u8 *tagarray;
  1948. u8 *tempbuf = tmp_buf_search_bt_in_block;
  1949. u8 *pSpareBuf = spare_buf_search_bt_in_block;
  1950. u8 *pSpareBufBTLastPage = spare_buf_bt_search_bt_in_block;
  1951. u8 bt_flag_last_page = 0xFF;
  1952. u8 search_in_previous_pages = 0;
  1953. u16 bt_pages;
  1954. nand_dbg_print(NAND_DBG_DEBUG, "%s, Line %d, Function: %s\n",
  1955. __FILE__, __LINE__, __func__);
  1956. nand_dbg_print(NAND_DBG_DEBUG,
  1957. "Searching block table in %u block\n",
  1958. (unsigned int)BT_Block);
  1959. bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
  1960. for (i = bt_pages; i < DeviceInfo.wPagesPerBlock;
  1961. i += (bt_pages + 1)) {
  1962. nand_dbg_print(NAND_DBG_DEBUG,
  1963. "Searching last IPF: %d\n", i);
  1964. Result = GLOB_LLD_Read_Page_Main_Polling(tempbuf,
  1965. BT_Block, i, 1);
  1966. if (0 == memcmp(tempbuf, g_pIPF, DeviceInfo.wPageDataSize)) {
  1967. if ((i + bt_pages + 1) < DeviceInfo.wPagesPerBlock) {
  1968. continue;
  1969. } else {
  1970. search_in_previous_pages = 1;
  1971. Last_IPF = i;
  1972. }
  1973. }
  1974. if (!search_in_previous_pages) {
  1975. if (i != bt_pages) {
  1976. i -= (bt_pages + 1);
  1977. Last_IPF = i;
  1978. }
  1979. }
  1980. if (0 == Last_IPF)
  1981. break;
  1982. if (!search_in_previous_pages) {
  1983. i = i + 1;
  1984. nand_dbg_print(NAND_DBG_DEBUG,
  1985. "Reading the spare area of Block %u Page %u",
  1986. (unsigned int)BT_Block, i);
  1987. Result = GLOB_LLD_Read_Page_Spare(pSpareBuf,
  1988. BT_Block, i, 1);
  1989. nand_dbg_print(NAND_DBG_DEBUG,
  1990. "Reading the spare area of Block %u Page %u",
  1991. (unsigned int)BT_Block, i + bt_pages - 1);
  1992. Result = GLOB_LLD_Read_Page_Spare(pSpareBufBTLastPage,
  1993. BT_Block, i + bt_pages - 1, 1);
  1994. k = 0;
  1995. j = FTL_Extract_Block_Table_Tag(pSpareBuf, &tagarray);
  1996. if (j) {
  1997. for (; k < j; k++) {
  1998. if (tagarray[k] == BT_Tag)
  1999. break;
  2000. }
  2001. }
  2002. if (k < j)
  2003. bt_flag = tagarray[k];
  2004. else
  2005. Result = FAIL;
  2006. if (Result == PASS) {
  2007. k = 0;
  2008. j = FTL_Extract_Block_Table_Tag(
  2009. pSpareBufBTLastPage, &tagarray);
  2010. if (j) {
  2011. for (; k < j; k++) {
  2012. if (tagarray[k] == BT_Tag)
  2013. break;
  2014. }
  2015. }
  2016. if (k < j)
  2017. bt_flag_last_page = tagarray[k];
  2018. else
  2019. Result = FAIL;
  2020. if (Result == PASS) {
  2021. if (bt_flag == bt_flag_last_page) {
  2022. nand_dbg_print(NAND_DBG_DEBUG,
  2023. "Block table is found"
  2024. " in page after IPF "
  2025. "at block %d "
  2026. "page %d\n",
  2027. (int)BT_Block, i);
  2028. BT_Found = 1;
  2029. *Page = i;
  2030. g_cBlockTableStatus =
  2031. CURRENT_BLOCK_TABLE;
  2032. break;
  2033. } else {
  2034. Result = FAIL;
  2035. }
  2036. }
  2037. }
  2038. }
  2039. if (search_in_previous_pages)
  2040. i = i - bt_pages;
  2041. else
  2042. i = i - (bt_pages + 1);
  2043. Result = PASS;
  2044. nand_dbg_print(NAND_DBG_DEBUG,
  2045. "Reading the spare area of Block %d Page %d",
  2046. (int)BT_Block, i);
  2047. Result = GLOB_LLD_Read_Page_Spare(pSpareBuf, BT_Block, i, 1);
  2048. nand_dbg_print(NAND_DBG_DEBUG,
  2049. "Reading the spare area of Block %u Page %u",
  2050. (unsigned int)BT_Block, i + bt_pages - 1);
  2051. Result = GLOB_LLD_Read_Page_Spare(pSpareBufBTLastPage,
  2052. BT_Block, i + bt_pages - 1, 1);
  2053. k = 0;
  2054. j = FTL_Extract_Block_Table_Tag(pSpareBuf, &tagarray);
  2055. if (j) {
  2056. for (; k < j; k++) {
  2057. if (tagarray[k] == BT_Tag)
  2058. break;
  2059. }
  2060. }
  2061. if (k < j)
  2062. bt_flag = tagarray[k];
  2063. else
  2064. Result = FAIL;
  2065. if (Result == PASS) {
  2066. k = 0;
  2067. j = FTL_Extract_Block_Table_Tag(pSpareBufBTLastPage,
  2068. &tagarray);
  2069. if (j) {
  2070. for (; k < j; k++) {
  2071. if (tagarray[k] == BT_Tag)
  2072. break;
  2073. }
  2074. }
  2075. if (k < j) {
  2076. bt_flag_last_page = tagarray[k];
  2077. } else {
  2078. Result = FAIL;
  2079. break;
  2080. }
  2081. if (Result == PASS) {
  2082. if (bt_flag == bt_flag_last_page) {
  2083. nand_dbg_print(NAND_DBG_DEBUG,
  2084. "Block table is found "
  2085. "in page prior to IPF "
  2086. "at block %u page %d\n",
  2087. (unsigned int)BT_Block, i);
  2088. BT_Found = 1;
  2089. *Page = i;
  2090. g_cBlockTableStatus =
  2091. IN_PROGRESS_BLOCK_TABLE;
  2092. break;
  2093. } else {
  2094. Result = FAIL;
  2095. break;
  2096. }
  2097. }
  2098. }
  2099. }
  2100. if (Result == FAIL) {
  2101. if ((Last_IPF > bt_pages) && (i < Last_IPF) && (!BT_Found)) {
  2102. BT_Found = 1;
  2103. *Page = i - (bt_pages + 1);
  2104. }
  2105. if ((Last_IPF == bt_pages) && (i < Last_IPF) && (!BT_Found))
  2106. goto func_return;
  2107. }
  2108. if (Last_IPF == 0) {
  2109. i = 0;
  2110. Result = PASS;
  2111. nand_dbg_print(NAND_DBG_DEBUG, "Reading the spare area of "
  2112. "Block %u Page %u", (unsigned int)BT_Block, i);
  2113. Result = GLOB_LLD_Read_Page_Spare(pSpareBuf, BT_Block, i, 1);
  2114. nand_dbg_print(NAND_DBG_DEBUG,
  2115. "Reading the spare area of Block %u Page %u",
  2116. (unsigned int)BT_Block, i + bt_pages - 1);
  2117. Result = GLOB_LLD_Read_Page_Spare(pSpareBufBTLastPage,
  2118. BT_Block, i + bt_pages - 1, 1);
  2119. k = 0;
  2120. j = FTL_Extract_Block_Table_Tag(pSpareBuf, &tagarray);
  2121. if (j) {
  2122. for (; k < j; k++) {
  2123. if (tagarray[k] == BT_Tag)
  2124. break;
  2125. }
  2126. }
  2127. if (k < j)
  2128. bt_flag = tagarray[k];
  2129. else
  2130. Result = FAIL;
  2131. if (Result == PASS) {
  2132. k = 0;
  2133. j = FTL_Extract_Block_Table_Tag(pSpareBufBTLastPage,
  2134. &tagarray);
  2135. if (j) {
  2136. for (; k < j; k++) {
  2137. if (tagarray[k] == BT_Tag)
  2138. break;
  2139. }
  2140. }
  2141. if (k < j)
  2142. bt_flag_last_page = tagarray[k];
  2143. else
  2144. Result = FAIL;
  2145. if (Result == PASS) {
  2146. if (bt_flag == bt_flag_last_page) {
  2147. nand_dbg_print(NAND_DBG_DEBUG,
  2148. "Block table is found "
  2149. "in page after IPF at "
  2150. "block %u page %u\n",
  2151. (unsigned int)BT_Block,
  2152. (unsigned int)i);
  2153. BT_Found = 1;
  2154. *Page = i;
  2155. g_cBlockTableStatus =
  2156. CURRENT_BLOCK_TABLE;
  2157. goto func_return;
  2158. } else {
  2159. Result = FAIL;
  2160. }
  2161. }
  2162. }
  2163. if (Result == FAIL)
  2164. goto func_return;
  2165. }
  2166. func_return:
  2167. return Result;
  2168. }
  2169. u8 *get_blk_table_start_addr(void)
  2170. {
  2171. return g_pBlockTable;
  2172. }
  2173. unsigned long get_blk_table_len(void)
  2174. {
  2175. return DeviceInfo.wDataBlockNum * sizeof(u32);
  2176. }
  2177. u8 *get_wear_leveling_table_start_addr(void)
  2178. {
  2179. return g_pWearCounter;
  2180. }
  2181. unsigned long get_wear_leveling_table_len(void)
  2182. {
  2183. return DeviceInfo.wDataBlockNum * sizeof(u8);
  2184. }
  2185. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2186. * Function: FTL_Read_Block_Table
  2187. * Inputs: none
  2188. * Outputs: PASS / FAIL
  2189. * Description: read the flash spare area and find a block containing the
  2190. * most recent block table(having largest block_table_counter).
  2191. * Find the last written Block table in this block.
  2192. * Check the correctness of Block Table
  2193. * If CDMA is enabled, this function is called in
  2194. * polling mode.
  2195. * We don't need to store changes in Block table in this
  2196. * function as it is called only at initialization
  2197. *
  2198. * Note: Currently this function is called at initialization
  2199. * before any read/erase/write command issued to flash so,
  2200. * there is no need to wait for CDMA list to complete as of now
  2201. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2202. static int FTL_Read_Block_Table(void)
  2203. {
  2204. u16 i = 0;
  2205. int k, j;
  2206. u8 *tempBuf, *tagarray;
  2207. int wResult = FAIL;
  2208. int status = FAIL;
  2209. u8 block_table_found = 0;
  2210. int search_result;
  2211. u32 Block;
  2212. u16 Page = 0;
  2213. u16 PageCount;
  2214. u16 bt_pages;
  2215. int wBytesCopied = 0, tempvar;
  2216. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2217. __FILE__, __LINE__, __func__);
  2218. tempBuf = tmp_buf1_read_blk_table;
  2219. bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
  2220. for (j = DeviceInfo.wSpectraStartBlock;
  2221. j <= (int)DeviceInfo.wSpectraEndBlock;
  2222. j++) {
  2223. status = GLOB_LLD_Read_Page_Spare(tempBuf, j, 0, 1);
  2224. k = 0;
  2225. i = FTL_Extract_Block_Table_Tag(tempBuf, &tagarray);
  2226. if (i) {
  2227. status = GLOB_LLD_Read_Page_Main_Polling(tempBuf,
  2228. j, 0, 1);
  2229. for (; k < i; k++) {
  2230. if (tagarray[k] == tempBuf[3])
  2231. break;
  2232. }
  2233. }
  2234. if (k < i)
  2235. k = tagarray[k];
  2236. else
  2237. continue;
  2238. nand_dbg_print(NAND_DBG_DEBUG,
  2239. "Block table is contained in Block %d %d\n",
  2240. (unsigned int)j, (unsigned int)k);
  2241. if (g_pBTBlocks[k-FIRST_BT_ID] == BTBLOCK_INVAL) {
  2242. g_pBTBlocks[k-FIRST_BT_ID] = j;
  2243. block_table_found = 1;
  2244. } else {
  2245. printk(KERN_ERR "FTL_Read_Block_Table -"
  2246. "This should never happens. "
  2247. "Two block table have same counter %u!\n", k);
  2248. }
  2249. }
  2250. if (block_table_found) {
  2251. if (g_pBTBlocks[FIRST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL &&
  2252. g_pBTBlocks[LAST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL) {
  2253. j = LAST_BT_ID;
  2254. while ((j > FIRST_BT_ID) &&
  2255. (g_pBTBlocks[j - FIRST_BT_ID] != BTBLOCK_INVAL))
  2256. j--;
  2257. if (j == FIRST_BT_ID) {
  2258. j = LAST_BT_ID;
  2259. last_erased = LAST_BT_ID;
  2260. } else {
  2261. last_erased = (u8)j + 1;
  2262. while ((j > FIRST_BT_ID) && (BTBLOCK_INVAL ==
  2263. g_pBTBlocks[j - FIRST_BT_ID]))
  2264. j--;
  2265. }
  2266. } else {
  2267. j = FIRST_BT_ID;
  2268. while (g_pBTBlocks[j - FIRST_BT_ID] == BTBLOCK_INVAL)
  2269. j++;
  2270. last_erased = (u8)j;
  2271. while ((j < LAST_BT_ID) && (BTBLOCK_INVAL !=
  2272. g_pBTBlocks[j - FIRST_BT_ID]))
  2273. j++;
  2274. if (g_pBTBlocks[j-FIRST_BT_ID] == BTBLOCK_INVAL)
  2275. j--;
  2276. }
  2277. if (last_erased > j)
  2278. j += (1 + LAST_BT_ID - FIRST_BT_ID);
  2279. for (; (j >= last_erased) && (FAIL == wResult); j--) {
  2280. i = (j - FIRST_BT_ID) %
  2281. (1 + LAST_BT_ID - FIRST_BT_ID);
  2282. search_result =
  2283. FTL_Search_Block_Table_IN_Block(g_pBTBlocks[i],
  2284. i + FIRST_BT_ID, &Page);
  2285. if (g_cBlockTableStatus == IN_PROGRESS_BLOCK_TABLE)
  2286. block_table_found = 0;
  2287. while ((search_result == PASS) && (FAIL == wResult)) {
  2288. nand_dbg_print(NAND_DBG_DEBUG,
  2289. "FTL_Read_Block_Table:"
  2290. "Block: %u Page: %u "
  2291. "contains block table\n",
  2292. (unsigned int)g_pBTBlocks[i],
  2293. (unsigned int)Page);
  2294. tempBuf = tmp_buf2_read_blk_table;
  2295. for (k = 0; k < bt_pages; k++) {
  2296. Block = g_pBTBlocks[i];
  2297. PageCount = 1;
  2298. status =
  2299. GLOB_LLD_Read_Page_Main_Polling(
  2300. tempBuf, Block, Page, PageCount);
  2301. tempvar = k ? 0 : 4;
  2302. wBytesCopied +=
  2303. FTL_Copy_Block_Table_From_Flash(
  2304. tempBuf + tempvar,
  2305. DeviceInfo.wPageDataSize - tempvar,
  2306. wBytesCopied);
  2307. Page++;
  2308. }
  2309. wResult = FTL_Check_Block_Table(FAIL);
  2310. if (FAIL == wResult) {
  2311. block_table_found = 0;
  2312. if (Page > bt_pages)
  2313. Page -= ((bt_pages<<1) + 1);
  2314. else
  2315. search_result = FAIL;
  2316. }
  2317. }
  2318. }
  2319. }
  2320. if (PASS == wResult) {
  2321. if (!block_table_found)
  2322. FTL_Execute_SPL_Recovery();
  2323. if (g_cBlockTableStatus == IN_PROGRESS_BLOCK_TABLE)
  2324. g_wBlockTableOffset = (u16)Page + 1;
  2325. else
  2326. g_wBlockTableOffset = (u16)Page - bt_pages;
  2327. g_wBlockTableIndex = (u32)g_pBTBlocks[i];
  2328. #if CMD_DMA
  2329. if (DeviceInfo.MLCDevice)
  2330. memcpy(g_pBTStartingCopy, g_pBlockTable,
  2331. DeviceInfo.wDataBlockNum * sizeof(u32)
  2332. + DeviceInfo.wDataBlockNum * sizeof(u8)
  2333. + DeviceInfo.wDataBlockNum * sizeof(u16));
  2334. else
  2335. memcpy(g_pBTStartingCopy, g_pBlockTable,
  2336. DeviceInfo.wDataBlockNum * sizeof(u32)
  2337. + DeviceInfo.wDataBlockNum * sizeof(u8));
  2338. #endif
  2339. }
  2340. if (FAIL == wResult)
  2341. printk(KERN_ERR "Yunpeng - "
  2342. "Can not find valid spectra block table!\n");
  2343. #if AUTO_FORMAT_FLASH
  2344. if (FAIL == wResult) {
  2345. nand_dbg_print(NAND_DBG_DEBUG, "doing auto-format\n");
  2346. wResult = FTL_Format_Flash(0);
  2347. }
  2348. #endif
  2349. return wResult;
  2350. }
  2351. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2352. * Function: FTL_Get_Page_Num
  2353. * Inputs: Size in bytes
  2354. * Outputs: Size in pages
  2355. * Description: It calculates the pages required for the length passed
  2356. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2357. static u32 FTL_Get_Page_Num(u64 length)
  2358. {
  2359. return (u32)((length >> DeviceInfo.nBitsInPageDataSize) +
  2360. (GLOB_u64_Remainder(length , 1) > 0 ? 1 : 0));
  2361. }
  2362. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2363. * Function: FTL_Get_Physical_Block_Addr
  2364. * Inputs: Block Address (byte format)
  2365. * Outputs: Physical address of the block.
  2366. * Description: It translates LBA to PBA by returning address stored
  2367. * at the LBA location in the block table
  2368. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2369. static u64 FTL_Get_Physical_Block_Addr(u64 logical_addr)
  2370. {
  2371. u32 *pbt;
  2372. u64 physical_addr;
  2373. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2374. __FILE__, __LINE__, __func__);
  2375. pbt = (u32 *)g_pBlockTable;
  2376. physical_addr = (u64) DeviceInfo.wBlockDataSize *
  2377. (pbt[BLK_FROM_ADDR(logical_addr)] & (~BAD_BLOCK));
  2378. return physical_addr;
  2379. }
  2380. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2381. * Function: FTL_Get_Block_Index
  2382. * Inputs: Physical Block no.
  2383. * Outputs: Logical block no. /BAD_BLOCK
  2384. * Description: It returns the logical block no. for the PBA passed
  2385. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2386. static u32 FTL_Get_Block_Index(u32 wBlockNum)
  2387. {
  2388. u32 *pbt = (u32 *)g_pBlockTable;
  2389. u32 i;
  2390. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2391. __FILE__, __LINE__, __func__);
  2392. for (i = 0; i < DeviceInfo.wDataBlockNum; i++)
  2393. if (wBlockNum == (pbt[i] & (~BAD_BLOCK)))
  2394. return i;
  2395. return BAD_BLOCK;
  2396. }
  2397. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2398. * Function: GLOB_FTL_Wear_Leveling
  2399. * Inputs: none
  2400. * Outputs: PASS=0
  2401. * Description: This is static wear leveling (done by explicit call)
  2402. * do complete static wear leveling
  2403. * do complete garbage collection
  2404. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2405. int GLOB_FTL_Wear_Leveling(void)
  2406. {
  2407. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  2408. __FILE__, __LINE__, __func__);
  2409. FTL_Static_Wear_Leveling();
  2410. GLOB_FTL_Garbage_Collection();
  2411. return PASS;
  2412. }
  2413. static void find_least_most_worn(u8 *chg,
  2414. u32 *least_idx, u8 *least_cnt,
  2415. u32 *most_idx, u8 *most_cnt)
  2416. {
  2417. u32 *pbt = (u32 *)g_pBlockTable;
  2418. u32 idx;
  2419. u8 cnt;
  2420. int i;
  2421. for (i = BLOCK_TABLE_INDEX + 1; i < DeviceInfo.wDataBlockNum; i++) {
  2422. if (IS_BAD_BLOCK(i) || PASS == chg[i])
  2423. continue;
  2424. idx = (u32) ((~BAD_BLOCK) & pbt[i]);
  2425. cnt = g_pWearCounter[idx - DeviceInfo.wSpectraStartBlock];
  2426. if (IS_SPARE_BLOCK(i)) {
  2427. if (cnt > *most_cnt) {
  2428. *most_cnt = cnt;
  2429. *most_idx = idx;
  2430. }
  2431. }
  2432. if (IS_DATA_BLOCK(i)) {
  2433. if (cnt < *least_cnt) {
  2434. *least_cnt = cnt;
  2435. *least_idx = idx;
  2436. }
  2437. }
  2438. if (PASS == chg[*most_idx] || PASS == chg[*least_idx]) {
  2439. debug_boundary_error(*most_idx,
  2440. DeviceInfo.wDataBlockNum, 0);
  2441. debug_boundary_error(*least_idx,
  2442. DeviceInfo.wDataBlockNum, 0);
  2443. continue;
  2444. }
  2445. }
  2446. }
  2447. static int move_blks_for_wear_leveling(u8 *chg,
  2448. u32 *least_idx, u32 *rep_blk_num, int *result)
  2449. {
  2450. u32 *pbt = (u32 *)g_pBlockTable;
  2451. u32 rep_blk;
  2452. int j, ret_cp_blk, ret_erase;
  2453. int ret = PASS;
  2454. chg[*least_idx] = PASS;
  2455. debug_boundary_error(*least_idx, DeviceInfo.wDataBlockNum, 0);
  2456. rep_blk = FTL_Replace_MWBlock();
  2457. if (rep_blk != BAD_BLOCK) {
  2458. nand_dbg_print(NAND_DBG_DEBUG,
  2459. "More than two spare blocks exist so do it\n");
  2460. nand_dbg_print(NAND_DBG_DEBUG, "Block Replaced is %d\n",
  2461. rep_blk);
  2462. chg[rep_blk] = PASS;
  2463. if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
  2464. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  2465. FTL_Write_IN_Progress_Block_Table_Page();
  2466. }
  2467. for (j = 0; j < RETRY_TIMES; j++) {
  2468. ret_cp_blk = FTL_Copy_Block((u64)(*least_idx) *
  2469. DeviceInfo.wBlockDataSize,
  2470. (u64)rep_blk * DeviceInfo.wBlockDataSize);
  2471. if (FAIL == ret_cp_blk) {
  2472. ret_erase = GLOB_FTL_Block_Erase((u64)rep_blk
  2473. * DeviceInfo.wBlockDataSize);
  2474. if (FAIL == ret_erase)
  2475. MARK_BLOCK_AS_BAD(pbt[rep_blk]);
  2476. } else {
  2477. nand_dbg_print(NAND_DBG_DEBUG,
  2478. "FTL_Copy_Block == OK\n");
  2479. break;
  2480. }
  2481. }
  2482. if (j < RETRY_TIMES) {
  2483. u32 tmp;
  2484. u32 old_idx = FTL_Get_Block_Index(*least_idx);
  2485. u32 rep_idx = FTL_Get_Block_Index(rep_blk);
  2486. tmp = (u32)(DISCARD_BLOCK | pbt[old_idx]);
  2487. pbt[old_idx] = (u32)((~SPARE_BLOCK) &
  2488. pbt[rep_idx]);
  2489. pbt[rep_idx] = tmp;
  2490. #if CMD_DMA
  2491. p_BTableChangesDelta = (struct BTableChangesDelta *)
  2492. g_pBTDelta_Free;
  2493. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  2494. p_BTableChangesDelta->ftl_cmd_cnt =
  2495. ftl_cmd_cnt;
  2496. p_BTableChangesDelta->BT_Index = old_idx;
  2497. p_BTableChangesDelta->BT_Entry_Value = pbt[old_idx];
  2498. p_BTableChangesDelta->ValidFields = 0x0C;
  2499. p_BTableChangesDelta = (struct BTableChangesDelta *)
  2500. g_pBTDelta_Free;
  2501. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  2502. p_BTableChangesDelta->ftl_cmd_cnt =
  2503. ftl_cmd_cnt;
  2504. p_BTableChangesDelta->BT_Index = rep_idx;
  2505. p_BTableChangesDelta->BT_Entry_Value = pbt[rep_idx];
  2506. p_BTableChangesDelta->ValidFields = 0x0C;
  2507. #endif
  2508. } else {
  2509. pbt[FTL_Get_Block_Index(rep_blk)] |= BAD_BLOCK;
  2510. #if CMD_DMA
  2511. p_BTableChangesDelta = (struct BTableChangesDelta *)
  2512. g_pBTDelta_Free;
  2513. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  2514. p_BTableChangesDelta->ftl_cmd_cnt =
  2515. ftl_cmd_cnt;
  2516. p_BTableChangesDelta->BT_Index =
  2517. FTL_Get_Block_Index(rep_blk);
  2518. p_BTableChangesDelta->BT_Entry_Value =
  2519. pbt[FTL_Get_Block_Index(rep_blk)];
  2520. p_BTableChangesDelta->ValidFields = 0x0C;
  2521. #endif
  2522. *result = FAIL;
  2523. ret = FAIL;
  2524. }
  2525. if (((*rep_blk_num)++) > WEAR_LEVELING_BLOCK_NUM)
  2526. ret = FAIL;
  2527. } else {
  2528. printk(KERN_ERR "Less than 3 spare blocks exist so quit\n");
  2529. ret = FAIL;
  2530. }
  2531. return ret;
  2532. }
  2533. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2534. * Function: FTL_Static_Wear_Leveling
  2535. * Inputs: none
  2536. * Outputs: PASS=0 / FAIL=1
  2537. * Description: This is static wear leveling (done by explicit call)
  2538. * search for most&least used
  2539. * if difference < GATE:
  2540. * update the block table with exhange
  2541. * mark block table in flash as IN_PROGRESS
  2542. * copy flash block
  2543. * the caller should handle GC clean up after calling this function
  2544. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2545. int FTL_Static_Wear_Leveling(void)
  2546. {
  2547. u8 most_worn_cnt;
  2548. u8 least_worn_cnt;
  2549. u32 most_worn_idx;
  2550. u32 least_worn_idx;
  2551. int result = PASS;
  2552. int go_on = PASS;
  2553. u32 replaced_blks = 0;
  2554. u8 *chang_flag = flags_static_wear_leveling;
  2555. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  2556. __FILE__, __LINE__, __func__);
  2557. if (!chang_flag)
  2558. return FAIL;
  2559. memset(chang_flag, FAIL, DeviceInfo.wDataBlockNum);
  2560. while (go_on == PASS) {
  2561. nand_dbg_print(NAND_DBG_DEBUG,
  2562. "starting static wear leveling\n");
  2563. most_worn_cnt = 0;
  2564. least_worn_cnt = 0xFF;
  2565. least_worn_idx = BLOCK_TABLE_INDEX;
  2566. most_worn_idx = BLOCK_TABLE_INDEX;
  2567. find_least_most_worn(chang_flag, &least_worn_idx,
  2568. &least_worn_cnt, &most_worn_idx, &most_worn_cnt);
  2569. nand_dbg_print(NAND_DBG_DEBUG,
  2570. "Used and least worn is block %u, whos count is %u\n",
  2571. (unsigned int)least_worn_idx,
  2572. (unsigned int)least_worn_cnt);
  2573. nand_dbg_print(NAND_DBG_DEBUG,
  2574. "Free and most worn is block %u, whos count is %u\n",
  2575. (unsigned int)most_worn_idx,
  2576. (unsigned int)most_worn_cnt);
  2577. if ((most_worn_cnt > least_worn_cnt) &&
  2578. (most_worn_cnt - least_worn_cnt > WEAR_LEVELING_GATE))
  2579. go_on = move_blks_for_wear_leveling(chang_flag,
  2580. &least_worn_idx, &replaced_blks, &result);
  2581. else
  2582. go_on = FAIL;
  2583. }
  2584. return result;
  2585. }
  2586. #if CMD_DMA
  2587. static int do_garbage_collection(u32 discard_cnt)
  2588. {
  2589. u32 *pbt = (u32 *)g_pBlockTable;
  2590. u32 pba;
  2591. u8 bt_block_erased = 0;
  2592. int i, cnt, ret = FAIL;
  2593. u64 addr;
  2594. i = 0;
  2595. while ((i < DeviceInfo.wDataBlockNum) && (discard_cnt > 0) &&
  2596. ((ftl_cmd_cnt + 28) < 256)) {
  2597. if (((pbt[i] & BAD_BLOCK) != BAD_BLOCK) &&
  2598. (pbt[i] & DISCARD_BLOCK)) {
  2599. if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
  2600. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  2601. FTL_Write_IN_Progress_Block_Table_Page();
  2602. }
  2603. addr = FTL_Get_Physical_Block_Addr((u64)i *
  2604. DeviceInfo.wBlockDataSize);
  2605. pba = BLK_FROM_ADDR(addr);
  2606. for (cnt = FIRST_BT_ID; cnt <= LAST_BT_ID; cnt++) {
  2607. if (pba == g_pBTBlocks[cnt - FIRST_BT_ID]) {
  2608. nand_dbg_print(NAND_DBG_DEBUG,
  2609. "GC will erase BT block %u\n",
  2610. (unsigned int)pba);
  2611. discard_cnt--;
  2612. i++;
  2613. bt_block_erased = 1;
  2614. break;
  2615. }
  2616. }
  2617. if (bt_block_erased) {
  2618. bt_block_erased = 0;
  2619. continue;
  2620. }
  2621. addr = FTL_Get_Physical_Block_Addr((u64)i *
  2622. DeviceInfo.wBlockDataSize);
  2623. if (PASS == GLOB_FTL_Block_Erase(addr)) {
  2624. pbt[i] &= (u32)(~DISCARD_BLOCK);
  2625. pbt[i] |= (u32)(SPARE_BLOCK);
  2626. p_BTableChangesDelta =
  2627. (struct BTableChangesDelta *)
  2628. g_pBTDelta_Free;
  2629. g_pBTDelta_Free +=
  2630. sizeof(struct BTableChangesDelta);
  2631. p_BTableChangesDelta->ftl_cmd_cnt =
  2632. ftl_cmd_cnt - 1;
  2633. p_BTableChangesDelta->BT_Index = i;
  2634. p_BTableChangesDelta->BT_Entry_Value = pbt[i];
  2635. p_BTableChangesDelta->ValidFields = 0x0C;
  2636. discard_cnt--;
  2637. ret = PASS;
  2638. } else {
  2639. MARK_BLOCK_AS_BAD(pbt[i]);
  2640. }
  2641. }
  2642. i++;
  2643. }
  2644. return ret;
  2645. }
  2646. #else
  2647. static int do_garbage_collection(u32 discard_cnt)
  2648. {
  2649. u32 *pbt = (u32 *)g_pBlockTable;
  2650. u32 pba;
  2651. u8 bt_block_erased = 0;
  2652. int i, cnt, ret = FAIL;
  2653. u64 addr;
  2654. i = 0;
  2655. while ((i < DeviceInfo.wDataBlockNum) && (discard_cnt > 0)) {
  2656. if (((pbt[i] & BAD_BLOCK) != BAD_BLOCK) &&
  2657. (pbt[i] & DISCARD_BLOCK)) {
  2658. if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
  2659. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  2660. FTL_Write_IN_Progress_Block_Table_Page();
  2661. }
  2662. addr = FTL_Get_Physical_Block_Addr((u64)i *
  2663. DeviceInfo.wBlockDataSize);
  2664. pba = BLK_FROM_ADDR(addr);
  2665. for (cnt = FIRST_BT_ID; cnt <= LAST_BT_ID; cnt++) {
  2666. if (pba == g_pBTBlocks[cnt - FIRST_BT_ID]) {
  2667. nand_dbg_print(NAND_DBG_DEBUG,
  2668. "GC will erase BT block %d\n",
  2669. pba);
  2670. discard_cnt--;
  2671. i++;
  2672. bt_block_erased = 1;
  2673. break;
  2674. }
  2675. }
  2676. if (bt_block_erased) {
  2677. bt_block_erased = 0;
  2678. continue;
  2679. }
  2680. /* If the discard block is L2 cache block, then just skip it */
  2681. for (cnt = 0; cnt < BLK_NUM_FOR_L2_CACHE; cnt++) {
  2682. if (cache_l2.blk_array[cnt] == pba) {
  2683. nand_dbg_print(NAND_DBG_DEBUG,
  2684. "GC will erase L2 cache blk %d\n",
  2685. pba);
  2686. break;
  2687. }
  2688. }
  2689. if (cnt < BLK_NUM_FOR_L2_CACHE) { /* Skip it */
  2690. discard_cnt--;
  2691. i++;
  2692. continue;
  2693. }
  2694. addr = FTL_Get_Physical_Block_Addr((u64)i *
  2695. DeviceInfo.wBlockDataSize);
  2696. if (PASS == GLOB_FTL_Block_Erase(addr)) {
  2697. pbt[i] &= (u32)(~DISCARD_BLOCK);
  2698. pbt[i] |= (u32)(SPARE_BLOCK);
  2699. discard_cnt--;
  2700. ret = PASS;
  2701. } else {
  2702. MARK_BLOCK_AS_BAD(pbt[i]);
  2703. }
  2704. }
  2705. i++;
  2706. }
  2707. return ret;
  2708. }
  2709. #endif
  2710. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2711. * Function: GLOB_FTL_Garbage_Collection
  2712. * Inputs: none
  2713. * Outputs: PASS / FAIL (returns the number of un-erased blocks
  2714. * Description: search the block table for all discarded blocks to erase
  2715. * for each discarded block:
  2716. * set the flash block to IN_PROGRESS
  2717. * erase the block
  2718. * update the block table
  2719. * write the block table to flash
  2720. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2721. int GLOB_FTL_Garbage_Collection(void)
  2722. {
  2723. u32 i;
  2724. u32 wDiscard = 0;
  2725. int wResult = FAIL;
  2726. u32 *pbt = (u32 *)g_pBlockTable;
  2727. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  2728. __FILE__, __LINE__, __func__);
  2729. if (GC_Called) {
  2730. printk(KERN_ALERT "GLOB_FTL_Garbage_Collection() "
  2731. "has been re-entered! Exit.\n");
  2732. return PASS;
  2733. }
  2734. GC_Called = 1;
  2735. GLOB_FTL_BT_Garbage_Collection();
  2736. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  2737. if (IS_DISCARDED_BLOCK(i))
  2738. wDiscard++;
  2739. }
  2740. if (wDiscard <= 0) {
  2741. GC_Called = 0;
  2742. return wResult;
  2743. }
  2744. nand_dbg_print(NAND_DBG_DEBUG,
  2745. "Found %d discarded blocks\n", wDiscard);
  2746. FTL_Write_Block_Table(FAIL);
  2747. wResult = do_garbage_collection(wDiscard);
  2748. FTL_Write_Block_Table(FAIL);
  2749. GC_Called = 0;
  2750. return wResult;
  2751. }
  2752. #if CMD_DMA
  2753. static int do_bt_garbage_collection(void)
  2754. {
  2755. u32 pba, lba;
  2756. u32 *pbt = (u32 *)g_pBlockTable;
  2757. u32 *pBTBlocksNode = (u32 *)g_pBTBlocks;
  2758. u64 addr;
  2759. int i, ret = FAIL;
  2760. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2761. __FILE__, __LINE__, __func__);
  2762. if (BT_GC_Called)
  2763. return PASS;
  2764. BT_GC_Called = 1;
  2765. for (i = last_erased; (i <= LAST_BT_ID) &&
  2766. (g_pBTBlocks[((i + 2) % (1 + LAST_BT_ID - FIRST_BT_ID)) +
  2767. FIRST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL) &&
  2768. ((ftl_cmd_cnt + 28)) < 256; i++) {
  2769. pba = pBTBlocksNode[i - FIRST_BT_ID];
  2770. lba = FTL_Get_Block_Index(pba);
  2771. nand_dbg_print(NAND_DBG_DEBUG,
  2772. "do_bt_garbage_collection: pba %d, lba %d\n",
  2773. pba, lba);
  2774. nand_dbg_print(NAND_DBG_DEBUG,
  2775. "Block Table Entry: %d", pbt[lba]);
  2776. if (((pbt[lba] & BAD_BLOCK) != BAD_BLOCK) &&
  2777. (pbt[lba] & DISCARD_BLOCK)) {
  2778. nand_dbg_print(NAND_DBG_DEBUG,
  2779. "do_bt_garbage_collection_cdma: "
  2780. "Erasing Block tables present in block %d\n",
  2781. pba);
  2782. addr = FTL_Get_Physical_Block_Addr((u64)lba *
  2783. DeviceInfo.wBlockDataSize);
  2784. if (PASS == GLOB_FTL_Block_Erase(addr)) {
  2785. pbt[lba] &= (u32)(~DISCARD_BLOCK);
  2786. pbt[lba] |= (u32)(SPARE_BLOCK);
  2787. p_BTableChangesDelta =
  2788. (struct BTableChangesDelta *)
  2789. g_pBTDelta_Free;
  2790. g_pBTDelta_Free +=
  2791. sizeof(struct BTableChangesDelta);
  2792. p_BTableChangesDelta->ftl_cmd_cnt =
  2793. ftl_cmd_cnt - 1;
  2794. p_BTableChangesDelta->BT_Index = lba;
  2795. p_BTableChangesDelta->BT_Entry_Value =
  2796. pbt[lba];
  2797. p_BTableChangesDelta->ValidFields = 0x0C;
  2798. ret = PASS;
  2799. pBTBlocksNode[last_erased - FIRST_BT_ID] =
  2800. BTBLOCK_INVAL;
  2801. nand_dbg_print(NAND_DBG_DEBUG,
  2802. "resetting bt entry at index %d "
  2803. "value %d\n", i,
  2804. pBTBlocksNode[i - FIRST_BT_ID]);
  2805. if (last_erased == LAST_BT_ID)
  2806. last_erased = FIRST_BT_ID;
  2807. else
  2808. last_erased++;
  2809. } else {
  2810. MARK_BLOCK_AS_BAD(pbt[lba]);
  2811. }
  2812. }
  2813. }
  2814. BT_GC_Called = 0;
  2815. return ret;
  2816. }
  2817. #else
  2818. static int do_bt_garbage_collection(void)
  2819. {
  2820. u32 pba, lba;
  2821. u32 *pbt = (u32 *)g_pBlockTable;
  2822. u32 *pBTBlocksNode = (u32 *)g_pBTBlocks;
  2823. u64 addr;
  2824. int i, ret = FAIL;
  2825. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2826. __FILE__, __LINE__, __func__);
  2827. if (BT_GC_Called)
  2828. return PASS;
  2829. BT_GC_Called = 1;
  2830. for (i = last_erased; (i <= LAST_BT_ID) &&
  2831. (g_pBTBlocks[((i + 2) % (1 + LAST_BT_ID - FIRST_BT_ID)) +
  2832. FIRST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL); i++) {
  2833. pba = pBTBlocksNode[i - FIRST_BT_ID];
  2834. lba = FTL_Get_Block_Index(pba);
  2835. nand_dbg_print(NAND_DBG_DEBUG,
  2836. "do_bt_garbage_collection_cdma: pba %d, lba %d\n",
  2837. pba, lba);
  2838. nand_dbg_print(NAND_DBG_DEBUG,
  2839. "Block Table Entry: %d", pbt[lba]);
  2840. if (((pbt[lba] & BAD_BLOCK) != BAD_BLOCK) &&
  2841. (pbt[lba] & DISCARD_BLOCK)) {
  2842. nand_dbg_print(NAND_DBG_DEBUG,
  2843. "do_bt_garbage_collection: "
  2844. "Erasing Block tables present in block %d\n",
  2845. pba);
  2846. addr = FTL_Get_Physical_Block_Addr((u64)lba *
  2847. DeviceInfo.wBlockDataSize);
  2848. if (PASS == GLOB_FTL_Block_Erase(addr)) {
  2849. pbt[lba] &= (u32)(~DISCARD_BLOCK);
  2850. pbt[lba] |= (u32)(SPARE_BLOCK);
  2851. ret = PASS;
  2852. pBTBlocksNode[last_erased - FIRST_BT_ID] =
  2853. BTBLOCK_INVAL;
  2854. nand_dbg_print(NAND_DBG_DEBUG,
  2855. "resetting bt entry at index %d "
  2856. "value %d\n", i,
  2857. pBTBlocksNode[i - FIRST_BT_ID]);
  2858. if (last_erased == LAST_BT_ID)
  2859. last_erased = FIRST_BT_ID;
  2860. else
  2861. last_erased++;
  2862. } else {
  2863. MARK_BLOCK_AS_BAD(pbt[lba]);
  2864. }
  2865. }
  2866. }
  2867. BT_GC_Called = 0;
  2868. return ret;
  2869. }
  2870. #endif
  2871. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2872. * Function: GLOB_FTL_BT_Garbage_Collection
  2873. * Inputs: none
  2874. * Outputs: PASS / FAIL (returns the number of un-erased blocks
  2875. * Description: Erases discarded blocks containing Block table
  2876. *
  2877. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2878. int GLOB_FTL_BT_Garbage_Collection(void)
  2879. {
  2880. return do_bt_garbage_collection();
  2881. }
  2882. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2883. * Function: FTL_Replace_OneBlock
  2884. * Inputs: Block number 1
  2885. * Block number 2
  2886. * Outputs: Replaced Block Number
  2887. * Description: Interchange block table entries at wBlockNum and wReplaceNum
  2888. *
  2889. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2890. static u32 FTL_Replace_OneBlock(u32 blk, u32 rep_blk)
  2891. {
  2892. u32 tmp_blk;
  2893. u32 replace_node = BAD_BLOCK;
  2894. u32 *pbt = (u32 *)g_pBlockTable;
  2895. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2896. __FILE__, __LINE__, __func__);
  2897. if (rep_blk != BAD_BLOCK) {
  2898. if (IS_BAD_BLOCK(blk))
  2899. tmp_blk = pbt[blk];
  2900. else
  2901. tmp_blk = DISCARD_BLOCK | (~SPARE_BLOCK & pbt[blk]);
  2902. replace_node = (u32) ((~SPARE_BLOCK) & pbt[rep_blk]);
  2903. pbt[blk] = replace_node;
  2904. pbt[rep_blk] = tmp_blk;
  2905. #if CMD_DMA
  2906. p_BTableChangesDelta =
  2907. (struct BTableChangesDelta *)g_pBTDelta_Free;
  2908. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  2909. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  2910. p_BTableChangesDelta->BT_Index = blk;
  2911. p_BTableChangesDelta->BT_Entry_Value = pbt[blk];
  2912. p_BTableChangesDelta->ValidFields = 0x0C;
  2913. p_BTableChangesDelta =
  2914. (struct BTableChangesDelta *)g_pBTDelta_Free;
  2915. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  2916. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  2917. p_BTableChangesDelta->BT_Index = rep_blk;
  2918. p_BTableChangesDelta->BT_Entry_Value = pbt[rep_blk];
  2919. p_BTableChangesDelta->ValidFields = 0x0C;
  2920. #endif
  2921. }
  2922. return replace_node;
  2923. }
  2924. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  2925. * Function: FTL_Write_Block_Table_Data
  2926. * Inputs: Block table size in pages
  2927. * Outputs: PASS=0 / FAIL=1
  2928. * Description: Write block table data in flash
  2929. * If first page and last page
  2930. * Write data+BT flag
  2931. * else
  2932. * Write data
  2933. * BT flag is a counter. Its value is incremented for block table
  2934. * write in a new Block
  2935. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  2936. static int FTL_Write_Block_Table_Data(void)
  2937. {
  2938. u64 dwBlockTableAddr, pTempAddr;
  2939. u32 Block;
  2940. u16 Page, PageCount;
  2941. u8 *tempBuf = tmp_buf_write_blk_table_data;
  2942. int wBytesCopied;
  2943. u16 bt_pages;
  2944. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  2945. __FILE__, __LINE__, __func__);
  2946. dwBlockTableAddr =
  2947. (u64)((u64)g_wBlockTableIndex * DeviceInfo.wBlockDataSize +
  2948. (u64)g_wBlockTableOffset * DeviceInfo.wPageDataSize);
  2949. pTempAddr = dwBlockTableAddr;
  2950. bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
  2951. nand_dbg_print(NAND_DBG_DEBUG, "FTL_Write_Block_Table_Data: "
  2952. "page= %d BlockTableIndex= %d "
  2953. "BlockTableOffset=%d\n", bt_pages,
  2954. g_wBlockTableIndex, g_wBlockTableOffset);
  2955. Block = BLK_FROM_ADDR(pTempAddr);
  2956. Page = PAGE_FROM_ADDR(pTempAddr, Block);
  2957. PageCount = 1;
  2958. if (bt_block_changed) {
  2959. if (bt_flag == LAST_BT_ID) {
  2960. bt_flag = FIRST_BT_ID;
  2961. g_pBTBlocks[bt_flag - FIRST_BT_ID] = Block;
  2962. } else if (bt_flag < LAST_BT_ID) {
  2963. bt_flag++;
  2964. g_pBTBlocks[bt_flag - FIRST_BT_ID] = Block;
  2965. }
  2966. if ((bt_flag > (LAST_BT_ID-4)) &&
  2967. g_pBTBlocks[FIRST_BT_ID - FIRST_BT_ID] !=
  2968. BTBLOCK_INVAL) {
  2969. bt_block_changed = 0;
  2970. GLOB_FTL_BT_Garbage_Collection();
  2971. }
  2972. bt_block_changed = 0;
  2973. nand_dbg_print(NAND_DBG_DEBUG,
  2974. "Block Table Counter is %u Block %u\n",
  2975. bt_flag, (unsigned int)Block);
  2976. }
  2977. memset(tempBuf, 0, 3);
  2978. tempBuf[3] = bt_flag;
  2979. wBytesCopied = FTL_Copy_Block_Table_To_Flash(tempBuf + 4,
  2980. DeviceInfo.wPageDataSize - 4, 0);
  2981. memset(&tempBuf[wBytesCopied + 4], 0xff,
  2982. DeviceInfo.wPageSize - (wBytesCopied + 4));
  2983. FTL_Insert_Block_Table_Signature(&tempBuf[DeviceInfo.wPageDataSize],
  2984. bt_flag);
  2985. #if CMD_DMA
  2986. memcpy(g_pNextBlockTable, tempBuf,
  2987. DeviceInfo.wPageSize * sizeof(u8));
  2988. nand_dbg_print(NAND_DBG_DEBUG, "Writing First Page of Block Table "
  2989. "Block %u Page %u\n", (unsigned int)Block, Page);
  2990. if (FAIL == GLOB_LLD_Write_Page_Main_Spare_cdma(g_pNextBlockTable,
  2991. Block, Page, 1,
  2992. LLD_CMD_FLAG_MODE_CDMA | LLD_CMD_FLAG_ORDER_BEFORE_REST)) {
  2993. nand_dbg_print(NAND_DBG_WARN, "NAND Program fail in "
  2994. "%s, Line %d, Function: %s, "
  2995. "new Bad Block %d generated!\n",
  2996. __FILE__, __LINE__, __func__, Block);
  2997. goto func_return;
  2998. }
  2999. ftl_cmd_cnt++;
  3000. g_pNextBlockTable += ((DeviceInfo.wPageSize * sizeof(u8)));
  3001. #else
  3002. if (FAIL == GLOB_LLD_Write_Page_Main_Spare(tempBuf, Block, Page, 1)) {
  3003. nand_dbg_print(NAND_DBG_WARN,
  3004. "NAND Program fail in %s, Line %d, Function: %s, "
  3005. "new Bad Block %d generated!\n",
  3006. __FILE__, __LINE__, __func__, Block);
  3007. goto func_return;
  3008. }
  3009. #endif
  3010. if (bt_pages > 1) {
  3011. PageCount = bt_pages - 1;
  3012. if (PageCount > 1) {
  3013. wBytesCopied += FTL_Copy_Block_Table_To_Flash(tempBuf,
  3014. DeviceInfo.wPageDataSize * (PageCount - 1),
  3015. wBytesCopied);
  3016. #if CMD_DMA
  3017. memcpy(g_pNextBlockTable, tempBuf,
  3018. (PageCount - 1) * DeviceInfo.wPageDataSize);
  3019. if (FAIL == GLOB_LLD_Write_Page_Main_cdma(
  3020. g_pNextBlockTable, Block, Page + 1,
  3021. PageCount - 1)) {
  3022. nand_dbg_print(NAND_DBG_WARN,
  3023. "NAND Program fail in %s, Line %d, "
  3024. "Function: %s, "
  3025. "new Bad Block %d generated!\n",
  3026. __FILE__, __LINE__, __func__,
  3027. (int)Block);
  3028. goto func_return;
  3029. }
  3030. ftl_cmd_cnt++;
  3031. g_pNextBlockTable += (PageCount - 1) *
  3032. DeviceInfo.wPageDataSize * sizeof(u8);
  3033. #else
  3034. if (FAIL == GLOB_LLD_Write_Page_Main(tempBuf,
  3035. Block, Page + 1, PageCount - 1)) {
  3036. nand_dbg_print(NAND_DBG_WARN,
  3037. "NAND Program fail in %s, Line %d, "
  3038. "Function: %s, "
  3039. "new Bad Block %d generated!\n",
  3040. __FILE__, __LINE__, __func__,
  3041. (int)Block);
  3042. goto func_return;
  3043. }
  3044. #endif
  3045. }
  3046. wBytesCopied = FTL_Copy_Block_Table_To_Flash(tempBuf,
  3047. DeviceInfo.wPageDataSize, wBytesCopied);
  3048. memset(&tempBuf[wBytesCopied], 0xff,
  3049. DeviceInfo.wPageSize-wBytesCopied);
  3050. FTL_Insert_Block_Table_Signature(
  3051. &tempBuf[DeviceInfo.wPageDataSize], bt_flag);
  3052. #if CMD_DMA
  3053. memcpy(g_pNextBlockTable, tempBuf,
  3054. DeviceInfo.wPageSize * sizeof(u8));
  3055. nand_dbg_print(NAND_DBG_DEBUG,
  3056. "Writing the last Page of Block Table "
  3057. "Block %u Page %u\n",
  3058. (unsigned int)Block, Page + bt_pages - 1);
  3059. if (FAIL == GLOB_LLD_Write_Page_Main_Spare_cdma(
  3060. g_pNextBlockTable, Block, Page + bt_pages - 1, 1,
  3061. LLD_CMD_FLAG_MODE_CDMA |
  3062. LLD_CMD_FLAG_ORDER_BEFORE_REST)) {
  3063. nand_dbg_print(NAND_DBG_WARN,
  3064. "NAND Program fail in %s, Line %d, "
  3065. "Function: %s, new Bad Block %d generated!\n",
  3066. __FILE__, __LINE__, __func__, Block);
  3067. goto func_return;
  3068. }
  3069. ftl_cmd_cnt++;
  3070. #else
  3071. if (FAIL == GLOB_LLD_Write_Page_Main_Spare(tempBuf,
  3072. Block, Page+bt_pages - 1, 1)) {
  3073. nand_dbg_print(NAND_DBG_WARN,
  3074. "NAND Program fail in %s, Line %d, "
  3075. "Function: %s, "
  3076. "new Bad Block %d generated!\n",
  3077. __FILE__, __LINE__, __func__, Block);
  3078. goto func_return;
  3079. }
  3080. #endif
  3081. }
  3082. nand_dbg_print(NAND_DBG_DEBUG, "FTL_Write_Block_Table_Data: done\n");
  3083. func_return:
  3084. return PASS;
  3085. }
  3086. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3087. * Function: FTL_Replace_Block_Table
  3088. * Inputs: None
  3089. * Outputs: PASS=0 / FAIL=1
  3090. * Description: Get a new block to write block table
  3091. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3092. static u32 FTL_Replace_Block_Table(void)
  3093. {
  3094. u32 blk;
  3095. int gc;
  3096. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3097. __FILE__, __LINE__, __func__);
  3098. blk = FTL_Replace_LWBlock(BLOCK_TABLE_INDEX, &gc);
  3099. if ((BAD_BLOCK == blk) && (PASS == gc)) {
  3100. GLOB_FTL_Garbage_Collection();
  3101. blk = FTL_Replace_LWBlock(BLOCK_TABLE_INDEX, &gc);
  3102. }
  3103. if (BAD_BLOCK == blk)
  3104. printk(KERN_ERR "%s, %s: There is no spare block. "
  3105. "It should never happen\n",
  3106. __FILE__, __func__);
  3107. nand_dbg_print(NAND_DBG_DEBUG, "New Block table Block is %d\n", blk);
  3108. return blk;
  3109. }
  3110. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3111. * Function: FTL_Replace_LWBlock
  3112. * Inputs: Block number
  3113. * Pointer to Garbage Collect flag
  3114. * Outputs:
  3115. * Description: Determine the least weared block by traversing
  3116. * block table
  3117. * Set Garbage collection to be called if number of spare
  3118. * block is less than Free Block Gate count
  3119. * Change Block table entry to map least worn block for current
  3120. * operation
  3121. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3122. static u32 FTL_Replace_LWBlock(u32 wBlockNum, int *pGarbageCollect)
  3123. {
  3124. u32 i;
  3125. u32 *pbt = (u32 *)g_pBlockTable;
  3126. u8 wLeastWornCounter = 0xFF;
  3127. u32 wLeastWornIndex = BAD_BLOCK;
  3128. u32 wSpareBlockNum = 0;
  3129. u32 wDiscardBlockNum = 0;
  3130. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3131. __FILE__, __LINE__, __func__);
  3132. if (IS_SPARE_BLOCK(wBlockNum)) {
  3133. *pGarbageCollect = FAIL;
  3134. pbt[wBlockNum] = (u32)(pbt[wBlockNum] & (~SPARE_BLOCK));
  3135. #if CMD_DMA
  3136. p_BTableChangesDelta =
  3137. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3138. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3139. p_BTableChangesDelta->ftl_cmd_cnt =
  3140. ftl_cmd_cnt;
  3141. p_BTableChangesDelta->BT_Index = (u32)(wBlockNum);
  3142. p_BTableChangesDelta->BT_Entry_Value = pbt[wBlockNum];
  3143. p_BTableChangesDelta->ValidFields = 0x0C;
  3144. #endif
  3145. return pbt[wBlockNum];
  3146. }
  3147. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  3148. if (IS_DISCARDED_BLOCK(i))
  3149. wDiscardBlockNum++;
  3150. if (IS_SPARE_BLOCK(i)) {
  3151. u32 wPhysicalIndex = (u32)((~BAD_BLOCK) & pbt[i]);
  3152. if (wPhysicalIndex > DeviceInfo.wSpectraEndBlock)
  3153. printk(KERN_ERR "FTL_Replace_LWBlock: "
  3154. "This should never occur!\n");
  3155. if (g_pWearCounter[wPhysicalIndex -
  3156. DeviceInfo.wSpectraStartBlock] <
  3157. wLeastWornCounter) {
  3158. wLeastWornCounter =
  3159. g_pWearCounter[wPhysicalIndex -
  3160. DeviceInfo.wSpectraStartBlock];
  3161. wLeastWornIndex = i;
  3162. }
  3163. wSpareBlockNum++;
  3164. }
  3165. }
  3166. nand_dbg_print(NAND_DBG_WARN,
  3167. "FTL_Replace_LWBlock: Least Worn Counter %d\n",
  3168. (int)wLeastWornCounter);
  3169. if ((wDiscardBlockNum >= NUM_FREE_BLOCKS_GATE) ||
  3170. (wSpareBlockNum <= NUM_FREE_BLOCKS_GATE))
  3171. *pGarbageCollect = PASS;
  3172. else
  3173. *pGarbageCollect = FAIL;
  3174. nand_dbg_print(NAND_DBG_DEBUG,
  3175. "FTL_Replace_LWBlock: Discarded Blocks %u Spare"
  3176. " Blocks %u\n",
  3177. (unsigned int)wDiscardBlockNum,
  3178. (unsigned int)wSpareBlockNum);
  3179. return FTL_Replace_OneBlock(wBlockNum, wLeastWornIndex);
  3180. }
  3181. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3182. * Function: FTL_Replace_MWBlock
  3183. * Inputs: None
  3184. * Outputs: most worn spare block no./BAD_BLOCK
  3185. * Description: It finds most worn spare block.
  3186. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3187. static u32 FTL_Replace_MWBlock(void)
  3188. {
  3189. u32 i;
  3190. u32 *pbt = (u32 *)g_pBlockTable;
  3191. u8 wMostWornCounter = 0;
  3192. u32 wMostWornIndex = BAD_BLOCK;
  3193. u32 wSpareBlockNum = 0;
  3194. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3195. __FILE__, __LINE__, __func__);
  3196. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  3197. if (IS_SPARE_BLOCK(i)) {
  3198. u32 wPhysicalIndex = (u32)((~SPARE_BLOCK) & pbt[i]);
  3199. if (g_pWearCounter[wPhysicalIndex -
  3200. DeviceInfo.wSpectraStartBlock] >
  3201. wMostWornCounter) {
  3202. wMostWornCounter =
  3203. g_pWearCounter[wPhysicalIndex -
  3204. DeviceInfo.wSpectraStartBlock];
  3205. wMostWornIndex = wPhysicalIndex;
  3206. }
  3207. wSpareBlockNum++;
  3208. }
  3209. }
  3210. if (wSpareBlockNum <= 2)
  3211. return BAD_BLOCK;
  3212. return wMostWornIndex;
  3213. }
  3214. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3215. * Function: FTL_Replace_Block
  3216. * Inputs: Block Address
  3217. * Outputs: PASS=0 / FAIL=1
  3218. * Description: If block specified by blk_addr parameter is not free,
  3219. * replace it with the least worn block.
  3220. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3221. static int FTL_Replace_Block(u64 blk_addr)
  3222. {
  3223. u32 current_blk = BLK_FROM_ADDR(blk_addr);
  3224. u32 *pbt = (u32 *)g_pBlockTable;
  3225. int wResult = PASS;
  3226. int GarbageCollect = FAIL;
  3227. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3228. __FILE__, __LINE__, __func__);
  3229. if (IS_SPARE_BLOCK(current_blk)) {
  3230. pbt[current_blk] = (~SPARE_BLOCK) & pbt[current_blk];
  3231. #if CMD_DMA
  3232. p_BTableChangesDelta =
  3233. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3234. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3235. p_BTableChangesDelta->ftl_cmd_cnt =
  3236. ftl_cmd_cnt;
  3237. p_BTableChangesDelta->BT_Index = current_blk;
  3238. p_BTableChangesDelta->BT_Entry_Value = pbt[current_blk];
  3239. p_BTableChangesDelta->ValidFields = 0x0C ;
  3240. #endif
  3241. return wResult;
  3242. }
  3243. FTL_Replace_LWBlock(current_blk, &GarbageCollect);
  3244. if (PASS == GarbageCollect)
  3245. wResult = GLOB_FTL_Garbage_Collection();
  3246. return wResult;
  3247. }
  3248. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3249. * Function: GLOB_FTL_Is_BadBlock
  3250. * Inputs: block number to test
  3251. * Outputs: PASS (block is BAD) / FAIL (block is not bad)
  3252. * Description: test if this block number is flagged as bad
  3253. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3254. int GLOB_FTL_Is_BadBlock(u32 wBlockNum)
  3255. {
  3256. u32 *pbt = (u32 *)g_pBlockTable;
  3257. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3258. __FILE__, __LINE__, __func__);
  3259. if (wBlockNum >= DeviceInfo.wSpectraStartBlock
  3260. && BAD_BLOCK == (pbt[wBlockNum] & BAD_BLOCK))
  3261. return PASS;
  3262. else
  3263. return FAIL;
  3264. }
  3265. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3266. * Function: GLOB_FTL_Flush_Cache
  3267. * Inputs: none
  3268. * Outputs: PASS=0 / FAIL=1
  3269. * Description: flush all the cache blocks to flash
  3270. * if a cache block is not dirty, don't do anything with it
  3271. * else, write the block and update the block table
  3272. * Note: This function should be called at shutdown/power down.
  3273. * to write important data into device
  3274. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3275. int GLOB_FTL_Flush_Cache(void)
  3276. {
  3277. int i, ret;
  3278. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  3279. __FILE__, __LINE__, __func__);
  3280. for (i = 0; i < CACHE_ITEM_NUM; i++) {
  3281. if (SET == Cache.array[i].changed) {
  3282. #if CMD_DMA
  3283. #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
  3284. int_cache[ftl_cmd_cnt].item = i;
  3285. int_cache[ftl_cmd_cnt].cache.address =
  3286. Cache.array[i].address;
  3287. int_cache[ftl_cmd_cnt].cache.changed = CLEAR;
  3288. #endif
  3289. #endif
  3290. ret = write_back_to_l2_cache(Cache.array[i].buf, Cache.array[i].address);
  3291. if (PASS == ret) {
  3292. Cache.array[i].changed = CLEAR;
  3293. } else {
  3294. printk(KERN_ALERT "Failed when write back to L2 cache!\n");
  3295. /* TODO - How to handle this? */
  3296. }
  3297. }
  3298. }
  3299. flush_l2_cache();
  3300. return FTL_Write_Block_Table(FAIL);
  3301. }
  3302. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3303. * Function: GLOB_FTL_Page_Read
  3304. * Inputs: pointer to data
  3305. * logical address of data (u64 is LBA * Bytes/Page)
  3306. * Outputs: PASS=0 / FAIL=1
  3307. * Description: reads a page of data into RAM from the cache
  3308. * if the data is not already in cache, read from flash to cache
  3309. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3310. int GLOB_FTL_Page_Read(u8 *data, u64 logical_addr)
  3311. {
  3312. u16 cache_item;
  3313. int res = PASS;
  3314. nand_dbg_print(NAND_DBG_DEBUG, "GLOB_FTL_Page_Read - "
  3315. "page_addr: %llu\n", logical_addr);
  3316. cache_item = FTL_Cache_If_Hit(logical_addr);
  3317. if (UNHIT_CACHE_ITEM == cache_item) {
  3318. nand_dbg_print(NAND_DBG_DEBUG,
  3319. "GLOB_FTL_Page_Read: Cache not hit\n");
  3320. res = FTL_Cache_Write();
  3321. if (ERR == FTL_Cache_Read(logical_addr))
  3322. res = ERR;
  3323. cache_item = Cache.LRU;
  3324. }
  3325. FTL_Cache_Read_Page(data, logical_addr, cache_item);
  3326. return res;
  3327. }
  3328. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3329. * Function: GLOB_FTL_Page_Write
  3330. * Inputs: pointer to data
  3331. * address of data (ADDRESSTYPE is LBA * Bytes/Page)
  3332. * Outputs: PASS=0 / FAIL=1
  3333. * Description: writes a page of data from RAM to the cache
  3334. * if the data is not already in cache, write back the
  3335. * least recently used block and read the addressed block
  3336. * from flash to cache
  3337. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3338. int GLOB_FTL_Page_Write(u8 *pData, u64 dwPageAddr)
  3339. {
  3340. u16 cache_blk;
  3341. u32 *pbt = (u32 *)g_pBlockTable;
  3342. int wResult = PASS;
  3343. nand_dbg_print(NAND_DBG_TRACE, "GLOB_FTL_Page_Write - "
  3344. "dwPageAddr: %llu\n", dwPageAddr);
  3345. cache_blk = FTL_Cache_If_Hit(dwPageAddr);
  3346. if (UNHIT_CACHE_ITEM == cache_blk) {
  3347. wResult = FTL_Cache_Write();
  3348. if (IS_BAD_BLOCK(BLK_FROM_ADDR(dwPageAddr))) {
  3349. wResult = FTL_Replace_Block(dwPageAddr);
  3350. pbt[BLK_FROM_ADDR(dwPageAddr)] |= SPARE_BLOCK;
  3351. if (wResult == FAIL)
  3352. return FAIL;
  3353. }
  3354. if (ERR == FTL_Cache_Read(dwPageAddr))
  3355. wResult = ERR;
  3356. cache_blk = Cache.LRU;
  3357. FTL_Cache_Write_Page(pData, dwPageAddr, cache_blk, 0);
  3358. } else {
  3359. #if CMD_DMA
  3360. FTL_Cache_Write_Page(pData, dwPageAddr, cache_blk,
  3361. LLD_CMD_FLAG_ORDER_BEFORE_REST);
  3362. #else
  3363. FTL_Cache_Write_Page(pData, dwPageAddr, cache_blk, 0);
  3364. #endif
  3365. }
  3366. return wResult;
  3367. }
  3368. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3369. * Function: GLOB_FTL_Block_Erase
  3370. * Inputs: address of block to erase (now in byte format, should change to
  3371. * block format)
  3372. * Outputs: PASS=0 / FAIL=1
  3373. * Description: erases the specified block
  3374. * increments the erase count
  3375. * If erase count reaches its upper limit,call function to
  3376. * do the adjustment as per the relative erase count values
  3377. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3378. int GLOB_FTL_Block_Erase(u64 blk_addr)
  3379. {
  3380. int status;
  3381. u32 BlkIdx;
  3382. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3383. __FILE__, __LINE__, __func__);
  3384. BlkIdx = (u32)(blk_addr >> DeviceInfo.nBitsInBlockDataSize);
  3385. if (BlkIdx < DeviceInfo.wSpectraStartBlock) {
  3386. printk(KERN_ERR "GLOB_FTL_Block_Erase: "
  3387. "This should never occur\n");
  3388. return FAIL;
  3389. }
  3390. #if CMD_DMA
  3391. status = GLOB_LLD_Erase_Block_cdma(BlkIdx, LLD_CMD_FLAG_MODE_CDMA);
  3392. if (status == FAIL)
  3393. nand_dbg_print(NAND_DBG_WARN,
  3394. "NAND Program fail in %s, Line %d, "
  3395. "Function: %s, new Bad Block %d generated!\n",
  3396. __FILE__, __LINE__, __func__, BlkIdx);
  3397. #else
  3398. status = GLOB_LLD_Erase_Block(BlkIdx);
  3399. if (status == FAIL) {
  3400. nand_dbg_print(NAND_DBG_WARN,
  3401. "NAND Program fail in %s, Line %d, "
  3402. "Function: %s, new Bad Block %d generated!\n",
  3403. __FILE__, __LINE__, __func__, BlkIdx);
  3404. return status;
  3405. }
  3406. #endif
  3407. if (DeviceInfo.MLCDevice) {
  3408. g_pReadCounter[BlkIdx - DeviceInfo.wSpectraStartBlock] = 0;
  3409. if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
  3410. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  3411. FTL_Write_IN_Progress_Block_Table_Page();
  3412. }
  3413. }
  3414. g_pWearCounter[BlkIdx - DeviceInfo.wSpectraStartBlock]++;
  3415. #if CMD_DMA
  3416. p_BTableChangesDelta =
  3417. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3418. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3419. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  3420. p_BTableChangesDelta->WC_Index =
  3421. BlkIdx - DeviceInfo.wSpectraStartBlock;
  3422. p_BTableChangesDelta->WC_Entry_Value =
  3423. g_pWearCounter[BlkIdx - DeviceInfo.wSpectraStartBlock];
  3424. p_BTableChangesDelta->ValidFields = 0x30;
  3425. if (DeviceInfo.MLCDevice) {
  3426. p_BTableChangesDelta =
  3427. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3428. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3429. p_BTableChangesDelta->ftl_cmd_cnt =
  3430. ftl_cmd_cnt;
  3431. p_BTableChangesDelta->RC_Index =
  3432. BlkIdx - DeviceInfo.wSpectraStartBlock;
  3433. p_BTableChangesDelta->RC_Entry_Value =
  3434. g_pReadCounter[BlkIdx -
  3435. DeviceInfo.wSpectraStartBlock];
  3436. p_BTableChangesDelta->ValidFields = 0xC0;
  3437. }
  3438. ftl_cmd_cnt++;
  3439. #endif
  3440. if (g_pWearCounter[BlkIdx - DeviceInfo.wSpectraStartBlock] == 0xFE)
  3441. FTL_Adjust_Relative_Erase_Count(BlkIdx);
  3442. return status;
  3443. }
  3444. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3445. * Function: FTL_Adjust_Relative_Erase_Count
  3446. * Inputs: index to block that was just incremented and is at the max
  3447. * Outputs: PASS=0 / FAIL=1
  3448. * Description: If any erase counts at MAX, adjusts erase count of every
  3449. * block by subtracting least worn
  3450. * counter from counter value of every entry in wear table
  3451. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3452. static int FTL_Adjust_Relative_Erase_Count(u32 Index_of_MAX)
  3453. {
  3454. u8 wLeastWornCounter = MAX_BYTE_VALUE;
  3455. u8 wWearCounter;
  3456. u32 i, wWearIndex;
  3457. u32 *pbt = (u32 *)g_pBlockTable;
  3458. int wResult = PASS;
  3459. nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
  3460. __FILE__, __LINE__, __func__);
  3461. for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
  3462. if (IS_BAD_BLOCK(i))
  3463. continue;
  3464. wWearIndex = (u32)(pbt[i] & (~BAD_BLOCK));
  3465. if ((wWearIndex - DeviceInfo.wSpectraStartBlock) < 0)
  3466. printk(KERN_ERR "FTL_Adjust_Relative_Erase_Count:"
  3467. "This should never occur\n");
  3468. wWearCounter = g_pWearCounter[wWearIndex -
  3469. DeviceInfo.wSpectraStartBlock];
  3470. if (wWearCounter < wLeastWornCounter)
  3471. wLeastWornCounter = wWearCounter;
  3472. }
  3473. if (wLeastWornCounter == 0) {
  3474. nand_dbg_print(NAND_DBG_WARN,
  3475. "Adjusting Wear Levelling Counters: Special Case\n");
  3476. g_pWearCounter[Index_of_MAX -
  3477. DeviceInfo.wSpectraStartBlock]--;
  3478. #if CMD_DMA
  3479. p_BTableChangesDelta =
  3480. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3481. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3482. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  3483. p_BTableChangesDelta->WC_Index =
  3484. Index_of_MAX - DeviceInfo.wSpectraStartBlock;
  3485. p_BTableChangesDelta->WC_Entry_Value =
  3486. g_pWearCounter[Index_of_MAX -
  3487. DeviceInfo.wSpectraStartBlock];
  3488. p_BTableChangesDelta->ValidFields = 0x30;
  3489. #endif
  3490. FTL_Static_Wear_Leveling();
  3491. } else {
  3492. for (i = 0; i < DeviceInfo.wDataBlockNum; i++)
  3493. if (!IS_BAD_BLOCK(i)) {
  3494. wWearIndex = (u32)(pbt[i] & (~BAD_BLOCK));
  3495. g_pWearCounter[wWearIndex -
  3496. DeviceInfo.wSpectraStartBlock] =
  3497. (u8)(g_pWearCounter
  3498. [wWearIndex -
  3499. DeviceInfo.wSpectraStartBlock] -
  3500. wLeastWornCounter);
  3501. #if CMD_DMA
  3502. p_BTableChangesDelta =
  3503. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3504. g_pBTDelta_Free +=
  3505. sizeof(struct BTableChangesDelta);
  3506. p_BTableChangesDelta->ftl_cmd_cnt =
  3507. ftl_cmd_cnt;
  3508. p_BTableChangesDelta->WC_Index = wWearIndex -
  3509. DeviceInfo.wSpectraStartBlock;
  3510. p_BTableChangesDelta->WC_Entry_Value =
  3511. g_pWearCounter[wWearIndex -
  3512. DeviceInfo.wSpectraStartBlock];
  3513. p_BTableChangesDelta->ValidFields = 0x30;
  3514. #endif
  3515. }
  3516. }
  3517. return wResult;
  3518. }
  3519. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3520. * Function: FTL_Write_IN_Progress_Block_Table_Page
  3521. * Inputs: None
  3522. * Outputs: None
  3523. * Description: It writes in-progress flag page to the page next to
  3524. * block table
  3525. ***********************************************************************/
  3526. static int FTL_Write_IN_Progress_Block_Table_Page(void)
  3527. {
  3528. int wResult = PASS;
  3529. u16 bt_pages;
  3530. u16 dwIPFPageAddr;
  3531. #if CMD_DMA
  3532. #else
  3533. u32 *pbt = (u32 *)g_pBlockTable;
  3534. u32 wTempBlockTableIndex;
  3535. #endif
  3536. nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
  3537. __FILE__, __LINE__, __func__);
  3538. bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
  3539. dwIPFPageAddr = g_wBlockTableOffset + bt_pages;
  3540. nand_dbg_print(NAND_DBG_DEBUG, "Writing IPF at "
  3541. "Block %d Page %d\n",
  3542. g_wBlockTableIndex, dwIPFPageAddr);
  3543. #if CMD_DMA
  3544. wResult = GLOB_LLD_Write_Page_Main_Spare_cdma(g_pIPF,
  3545. g_wBlockTableIndex, dwIPFPageAddr, 1,
  3546. LLD_CMD_FLAG_MODE_CDMA | LLD_CMD_FLAG_ORDER_BEFORE_REST);
  3547. if (wResult == FAIL) {
  3548. nand_dbg_print(NAND_DBG_WARN,
  3549. "NAND Program fail in %s, Line %d, "
  3550. "Function: %s, new Bad Block %d generated!\n",
  3551. __FILE__, __LINE__, __func__,
  3552. g_wBlockTableIndex);
  3553. }
  3554. g_wBlockTableOffset = dwIPFPageAddr + 1;
  3555. p_BTableChangesDelta = (struct BTableChangesDelta *)g_pBTDelta_Free;
  3556. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3557. p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
  3558. p_BTableChangesDelta->g_wBlockTableOffset = g_wBlockTableOffset;
  3559. p_BTableChangesDelta->ValidFields = 0x01;
  3560. ftl_cmd_cnt++;
  3561. #else
  3562. wResult = GLOB_LLD_Write_Page_Main_Spare(g_pIPF,
  3563. g_wBlockTableIndex, dwIPFPageAddr, 1);
  3564. if (wResult == FAIL) {
  3565. nand_dbg_print(NAND_DBG_WARN,
  3566. "NAND Program fail in %s, Line %d, "
  3567. "Function: %s, new Bad Block %d generated!\n",
  3568. __FILE__, __LINE__, __func__,
  3569. (int)g_wBlockTableIndex);
  3570. MARK_BLOCK_AS_BAD(pbt[BLOCK_TABLE_INDEX]);
  3571. wTempBlockTableIndex = FTL_Replace_Block_Table();
  3572. bt_block_changed = 1;
  3573. if (BAD_BLOCK == wTempBlockTableIndex)
  3574. return ERR;
  3575. g_wBlockTableIndex = wTempBlockTableIndex;
  3576. g_wBlockTableOffset = 0;
  3577. /* Block table tag is '00'. Means it's used one */
  3578. pbt[BLOCK_TABLE_INDEX] = g_wBlockTableIndex;
  3579. return FAIL;
  3580. }
  3581. g_wBlockTableOffset = dwIPFPageAddr + 1;
  3582. #endif
  3583. return wResult;
  3584. }
  3585. /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  3586. * Function: FTL_Read_Disturbance
  3587. * Inputs: block address
  3588. * Outputs: PASS=0 / FAIL=1
  3589. * Description: used to handle read disturbance. Data in block that
  3590. * reaches its read limit is moved to new block
  3591. *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  3592. int FTL_Read_Disturbance(u32 blk_addr)
  3593. {
  3594. int wResult = FAIL;
  3595. u32 *pbt = (u32 *) g_pBlockTable;
  3596. u32 dwOldBlockAddr = blk_addr;
  3597. u32 wBlockNum;
  3598. u32 i;
  3599. u32 wLeastReadCounter = 0xFFFF;
  3600. u32 wLeastReadIndex = BAD_BLOCK;
  3601. u32 wSpareBlockNum = 0;
  3602. u32 wTempNode;
  3603. u32 wReplacedNode;
  3604. u8 *g_pTempBuf;
  3605. nand_dbg_print(NAND_DBG_DEBUG, "%s, Line %d, Function: %s\n",
  3606. __FILE__, __LINE__, __func__);
  3607. #if CMD_DMA
  3608. g_pTempBuf = cp_back_buf_copies[cp_back_buf_idx];
  3609. cp_back_buf_idx++;
  3610. if (cp_back_buf_idx > COPY_BACK_BUF_NUM) {
  3611. printk(KERN_ERR "cp_back_buf_copies overflow! Exit."
  3612. "Maybe too many pending commands in your CDMA chain.\n");
  3613. return FAIL;
  3614. }
  3615. #else
  3616. g_pTempBuf = tmp_buf_read_disturbance;
  3617. #endif
  3618. wBlockNum = FTL_Get_Block_Index(blk_addr);
  3619. do {
  3620. /* This is a bug.Here 'i' should be logical block number
  3621. * and start from 1 (0 is reserved for block table).
  3622. * Have fixed it. - Yunpeng 2008. 12. 19
  3623. */
  3624. for (i = 1; i < DeviceInfo.wDataBlockNum; i++) {
  3625. if (IS_SPARE_BLOCK(i)) {
  3626. u32 wPhysicalIndex =
  3627. (u32)((~SPARE_BLOCK) & pbt[i]);
  3628. if (g_pReadCounter[wPhysicalIndex -
  3629. DeviceInfo.wSpectraStartBlock] <
  3630. wLeastReadCounter) {
  3631. wLeastReadCounter =
  3632. g_pReadCounter[wPhysicalIndex -
  3633. DeviceInfo.wSpectraStartBlock];
  3634. wLeastReadIndex = i;
  3635. }
  3636. wSpareBlockNum++;
  3637. }
  3638. }
  3639. if (wSpareBlockNum <= NUM_FREE_BLOCKS_GATE) {
  3640. wResult = GLOB_FTL_Garbage_Collection();
  3641. if (PASS == wResult)
  3642. continue;
  3643. else
  3644. break;
  3645. } else {
  3646. wTempNode = (u32)(DISCARD_BLOCK | pbt[wBlockNum]);
  3647. wReplacedNode = (u32)((~SPARE_BLOCK) &
  3648. pbt[wLeastReadIndex]);
  3649. #if CMD_DMA
  3650. pbt[wBlockNum] = wReplacedNode;
  3651. pbt[wLeastReadIndex] = wTempNode;
  3652. p_BTableChangesDelta =
  3653. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3654. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3655. p_BTableChangesDelta->ftl_cmd_cnt =
  3656. ftl_cmd_cnt;
  3657. p_BTableChangesDelta->BT_Index = wBlockNum;
  3658. p_BTableChangesDelta->BT_Entry_Value = pbt[wBlockNum];
  3659. p_BTableChangesDelta->ValidFields = 0x0C;
  3660. p_BTableChangesDelta =
  3661. (struct BTableChangesDelta *)g_pBTDelta_Free;
  3662. g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
  3663. p_BTableChangesDelta->ftl_cmd_cnt =
  3664. ftl_cmd_cnt;
  3665. p_BTableChangesDelta->BT_Index = wLeastReadIndex;
  3666. p_BTableChangesDelta->BT_Entry_Value =
  3667. pbt[wLeastReadIndex];
  3668. p_BTableChangesDelta->ValidFields = 0x0C;
  3669. wResult = GLOB_LLD_Read_Page_Main_cdma(g_pTempBuf,
  3670. dwOldBlockAddr, 0, DeviceInfo.wPagesPerBlock,
  3671. LLD_CMD_FLAG_MODE_CDMA);
  3672. if (wResult == FAIL)
  3673. return wResult;
  3674. ftl_cmd_cnt++;
  3675. if (wResult != FAIL) {
  3676. if (FAIL == GLOB_LLD_Write_Page_Main_cdma(
  3677. g_pTempBuf, pbt[wBlockNum], 0,
  3678. DeviceInfo.wPagesPerBlock)) {
  3679. nand_dbg_print(NAND_DBG_WARN,
  3680. "NAND Program fail in "
  3681. "%s, Line %d, Function: %s, "
  3682. "new Bad Block %d "
  3683. "generated!\n",
  3684. __FILE__, __LINE__, __func__,
  3685. (int)pbt[wBlockNum]);
  3686. wResult = FAIL;
  3687. MARK_BLOCK_AS_BAD(pbt[wBlockNum]);
  3688. }
  3689. ftl_cmd_cnt++;
  3690. }
  3691. #else
  3692. wResult = GLOB_LLD_Read_Page_Main(g_pTempBuf,
  3693. dwOldBlockAddr, 0, DeviceInfo.wPagesPerBlock);
  3694. if (wResult == FAIL)
  3695. return wResult;
  3696. if (wResult != FAIL) {
  3697. /* This is a bug. At this time, pbt[wBlockNum]
  3698. is still the physical address of
  3699. discard block, and should not be write.
  3700. Have fixed it as below.
  3701. -- Yunpeng 2008.12.19
  3702. */
  3703. wResult = GLOB_LLD_Write_Page_Main(g_pTempBuf,
  3704. wReplacedNode, 0,
  3705. DeviceInfo.wPagesPerBlock);
  3706. if (wResult == FAIL) {
  3707. nand_dbg_print(NAND_DBG_WARN,
  3708. "NAND Program fail in "
  3709. "%s, Line %d, Function: %s, "
  3710. "new Bad Block %d "
  3711. "generated!\n",
  3712. __FILE__, __LINE__, __func__,
  3713. (int)wReplacedNode);
  3714. MARK_BLOCK_AS_BAD(wReplacedNode);
  3715. } else {
  3716. pbt[wBlockNum] = wReplacedNode;
  3717. pbt[wLeastReadIndex] = wTempNode;
  3718. }
  3719. }
  3720. if ((wResult == PASS) && (g_cBlockTableStatus !=
  3721. IN_PROGRESS_BLOCK_TABLE)) {
  3722. g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
  3723. FTL_Write_IN_Progress_Block_Table_Page();
  3724. }
  3725. #endif
  3726. }
  3727. } while (wResult != PASS)
  3728. ;
  3729. #if CMD_DMA
  3730. /* ... */
  3731. #endif
  3732. return wResult;
  3733. }