block.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850
  1. /*
  2. * Block driver for media (i.e., flash cards)
  3. *
  4. * Copyright 2002 Hewlett-Packard Company
  5. * Copyright 2005-2008 Pierre Ossman
  6. *
  7. * Use consistent with the GNU GPL version 2 is permitted,
  8. * provided that this copyright notice is
  9. * preserved in its entirety in all copies and derived works.
  10. *
  11. * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
  12. * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
  13. * FITNESS FOR ANY PARTICULAR PURPOSE.
  14. *
  15. * Many thanks to Alessandro Rubini and Jonathan Corbet!
  16. *
  17. * Author: Andrew Christian
  18. * 28 May 2002
  19. */
  20. #include <linux/moduleparam.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/errno.h>
  27. #include <linux/hdreg.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/cdev.h>
  31. #include <linux/mutex.h>
  32. #include <linux/scatterlist.h>
  33. #include <linux/string_helpers.h>
  34. #include <linux/delay.h>
  35. #include <linux/capability.h>
  36. #include <linux/compat.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/idr.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/mmc/ioctl.h>
  41. #include <linux/mmc/card.h>
  42. #include <linux/mmc/host.h>
  43. #include <linux/mmc/mmc.h>
  44. #include <linux/mmc/sd.h>
  45. #ifdef CONFIG_MMC_FFU
  46. #include <linux/mmc/ffu.h>
  47. #endif
  48. #ifdef CONFIG_MTK_MMC_PWR_WP
  49. #include <mt-plat/mtk_partition.h>
  50. #include <linux/types.h>
  51. #include "mtk_emmc_write_protect.h"
  52. #endif
  53. #include <linux/uaccess.h>
  54. #include "mtk_mmc_block.h"
  55. #include "queue.h"
  56. #include "block.h"
  57. #include "core.h"
  58. #include "card.h"
  59. #include "crypto.h"
  60. #include "host.h"
  61. #include "bus.h"
  62. #include "mmc_ops.h"
  63. #include "quirks.h"
  64. #include "sd_ops.h"
  65. #include "mmc_crypto.h"
  66. #ifdef CONFIG_MTK_EMMC_HW_CQ
  67. #include "dbg.h"
  68. #endif
  69. MODULE_ALIAS("mmc:block");
  70. #ifdef MODULE_PARAM_PREFIX
  71. #undef MODULE_PARAM_PREFIX
  72. #endif
  73. #define MODULE_PARAM_PREFIX "mmcblk."
  74. #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  75. #define MMC_SANITIZE_REQ_TIMEOUT 240000
  76. #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
  77. #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
  78. #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
  79. (rq_data_dir(req) == WRITE))
  80. /* emmc cmdq enabled if part idx <= PART_CMDQ_EN
  81. * user: 0
  82. * boot1: 1
  83. * boot2: 2
  84. */
  85. #define PART_CMDQ_EN 0
  86. #ifdef CONFIG_MTK_EMMC_HW_CQ
  87. static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
  88. struct mmc_queue_req *mqrq, struct mmc_queue *mq);
  89. #endif
  90. static DEFINE_MUTEX(block_mutex);
  91. /*
  92. * The defaults come from config options but can be overriden by module
  93. * or bootarg options.
  94. */
  95. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  96. /*
  97. * We've only got one major, so number of mmcblk devices is
  98. * limited to (1 << 20) / number of minors per device. It is also
  99. * limited by the MAX_DEVICES below.
  100. */
  101. static int max_devices;
  102. #define MAX_DEVICES 256
  103. static DEFINE_IDA(mmc_blk_ida);
  104. static DEFINE_IDA(mmc_rpmb_ida);
  105. /*
  106. * There is one mmc_blk_data per slot.
  107. */
  108. struct mmc_blk_data {
  109. spinlock_t lock;
  110. struct device *parent;
  111. struct gendisk *disk;
  112. struct mmc_queue queue;
  113. struct list_head part;
  114. struct list_head rpmbs;
  115. unsigned int flags;
  116. #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
  117. #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
  118. #define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support*/
  119. unsigned int usage;
  120. unsigned int read_only;
  121. unsigned int part_type;
  122. unsigned int reset_done;
  123. #define MMC_BLK_READ BIT(0)
  124. #define MMC_BLK_WRITE BIT(1)
  125. #define MMC_BLK_DISCARD BIT(2)
  126. #define MMC_BLK_SECDISCARD BIT(3)
  127. /*
  128. * Only set in main mmc_blk_data associated
  129. * with mmc_card with dev_set_drvdata, and keeps
  130. * track of the current selected device partition.
  131. */
  132. unsigned int part_curr;
  133. struct device_attribute force_ro;
  134. struct device_attribute power_ro_lock;
  135. int area_type;
  136. /* debugfs files (only in main mmc_blk_data) */
  137. struct dentry *status_dentry;
  138. struct dentry *ext_csd_dentry;
  139. };
  140. /* Device type for RPMB character devices */
  141. static dev_t mmc_rpmb_devt;
  142. /* Bus type for RPMB character devices */
  143. static struct bus_type mmc_rpmb_bus_type = {
  144. .name = "mmc_rpmb",
  145. };
  146. /**
  147. * struct mmc_rpmb_data - special RPMB device type for these areas
  148. * @dev: the device for the RPMB area
  149. * @chrdev: character device for the RPMB area
  150. * @id: unique device ID number
  151. * @part_index: partition index (0 on first)
  152. * @md: parent MMC block device
  153. * @node: list item, so we can put this device on a list
  154. */
  155. struct mmc_rpmb_data {
  156. struct device dev;
  157. struct cdev chrdev;
  158. int id;
  159. unsigned int part_index;
  160. struct mmc_blk_data *md;
  161. struct list_head node;
  162. };
  163. static DEFINE_MUTEX(open_lock);
  164. module_param(perdev_minors, int, 0444);
  165. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  166. static inline int mmc_blk_part_switch(struct mmc_card *card,
  167. unsigned int part_type);
  168. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  169. {
  170. struct mmc_blk_data *md;
  171. mutex_lock(&open_lock);
  172. md = disk->private_data;
  173. if (md && md->usage == 0)
  174. md = NULL;
  175. if (md)
  176. md->usage++;
  177. mutex_unlock(&open_lock);
  178. return md;
  179. }
  180. static inline int mmc_get_devidx(struct gendisk *disk)
  181. {
  182. int devidx = disk->first_minor / perdev_minors;
  183. return devidx;
  184. }
  185. static void mmc_blk_put(struct mmc_blk_data *md)
  186. {
  187. mutex_lock(&open_lock);
  188. md->usage--;
  189. if (md->usage == 0) {
  190. int devidx = mmc_get_devidx(md->disk);
  191. ida_simple_remove(&mmc_blk_ida, devidx);
  192. put_disk(md->disk);
  193. kfree(md);
  194. }
  195. mutex_unlock(&open_lock);
  196. }
  197. static ssize_t power_ro_lock_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. int ret;
  201. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  202. struct mmc_card *card = md->queue.card;
  203. int locked = 0;
  204. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
  205. locked = 2;
  206. else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
  207. locked = 1;
  208. ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
  209. mmc_blk_put(md);
  210. return ret;
  211. }
  212. static ssize_t power_ro_lock_store(struct device *dev,
  213. struct device_attribute *attr, const char *buf, size_t count)
  214. {
  215. int ret;
  216. struct mmc_blk_data *md, *part_md;
  217. struct mmc_queue *mq;
  218. struct request *req;
  219. unsigned long set;
  220. if (kstrtoul(buf, 0, &set))
  221. return -EINVAL;
  222. if (set != 1)
  223. return count;
  224. md = mmc_blk_get(dev_to_disk(dev));
  225. mq = &md->queue;
  226. /* Dispatch locking to the block layer */
  227. req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
  228. if (IS_ERR(req)) {
  229. count = PTR_ERR(req);
  230. goto out_put;
  231. }
  232. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
  233. blk_execute_rq(mq->queue, NULL, req, 0);
  234. ret = req_to_mmc_queue_req(req)->drv_op_result;
  235. blk_put_request(req);
  236. if (!ret) {
  237. pr_info("%s: Locking boot partition ro until next power on\n",
  238. md->disk->disk_name);
  239. set_disk_ro(md->disk, 1);
  240. list_for_each_entry(part_md, &md->part, part)
  241. if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
  242. pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
  243. set_disk_ro(part_md->disk, 1);
  244. }
  245. }
  246. out_put:
  247. mmc_blk_put(md);
  248. return count;
  249. }
  250. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  251. char *buf)
  252. {
  253. int ret;
  254. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  255. ret = snprintf(buf, PAGE_SIZE, "%d\n",
  256. get_disk_ro(dev_to_disk(dev)) ^
  257. md->read_only);
  258. mmc_blk_put(md);
  259. return ret;
  260. }
  261. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. int ret;
  265. char *end;
  266. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  267. unsigned long set = simple_strtoul(buf, &end, 0);
  268. if (end == buf) {
  269. ret = -EINVAL;
  270. goto out;
  271. }
  272. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  273. ret = count;
  274. out:
  275. mmc_blk_put(md);
  276. return ret;
  277. }
  278. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  279. {
  280. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  281. int ret = -ENXIO;
  282. mutex_lock(&block_mutex);
  283. if (md) {
  284. if (md->usage == 2)
  285. check_disk_change(bdev);
  286. ret = 0;
  287. if ((mode & FMODE_WRITE) && md->read_only) {
  288. mmc_blk_put(md);
  289. ret = -EROFS;
  290. }
  291. }
  292. mutex_unlock(&block_mutex);
  293. return ret;
  294. }
  295. static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
  296. {
  297. struct mmc_blk_data *md = disk->private_data;
  298. mutex_lock(&block_mutex);
  299. mmc_blk_put(md);
  300. mutex_unlock(&block_mutex);
  301. }
  302. static int
  303. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  304. {
  305. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  306. geo->heads = 4;
  307. geo->sectors = 16;
  308. return 0;
  309. }
  310. struct mmc_blk_ioc_data {
  311. struct mmc_ioc_cmd ic;
  312. unsigned char *buf;
  313. u64 buf_bytes;
  314. struct mmc_rpmb_data *rpmb;
  315. };
  316. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  317. struct mmc_ioc_cmd __user *user)
  318. {
  319. struct mmc_blk_ioc_data *idata;
  320. int err;
  321. idata = kmalloc(sizeof(*idata), GFP_KERNEL);
  322. if (!idata) {
  323. err = -ENOMEM;
  324. goto out;
  325. }
  326. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  327. err = -EFAULT;
  328. goto idata_err;
  329. }
  330. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  331. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  332. err = -EOVERFLOW;
  333. goto idata_err;
  334. }
  335. if (!idata->buf_bytes) {
  336. idata->buf = NULL;
  337. return idata;
  338. }
  339. idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
  340. if (!idata->buf) {
  341. err = -ENOMEM;
  342. goto idata_err;
  343. }
  344. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  345. idata->ic.data_ptr, idata->buf_bytes)) {
  346. err = -EFAULT;
  347. goto copy_err;
  348. }
  349. return idata;
  350. copy_err:
  351. kfree(idata->buf);
  352. idata_err:
  353. kfree(idata);
  354. out:
  355. return ERR_PTR(err);
  356. }
  357. static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
  358. struct mmc_blk_ioc_data *idata)
  359. {
  360. struct mmc_ioc_cmd *ic = &idata->ic;
  361. if (copy_to_user(&(ic_ptr->response), ic->response,
  362. sizeof(ic->response)))
  363. return -EFAULT;
  364. if (!idata->ic.write_flag) {
  365. if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
  366. idata->buf, idata->buf_bytes))
  367. return -EFAULT;
  368. }
  369. return 0;
  370. }
  371. static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
  372. u32 retries_max)
  373. {
  374. int err;
  375. u32 retry_count = 0;
  376. if (!status || !retries_max)
  377. return -EINVAL;
  378. do {
  379. err = __mmc_send_status(card, status, 5);
  380. if (err)
  381. break;
  382. if (!R1_STATUS(*status) &&
  383. (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
  384. break; /* RPMB programming operation complete */
  385. /*
  386. * Rechedule to give the MMC device a chance to continue
  387. * processing the previous command without being polled too
  388. * frequently.
  389. */
  390. usleep_range(1000, 5000);
  391. } while (++retry_count < retries_max);
  392. if (retry_count == retries_max)
  393. err = -EPERM;
  394. return err;
  395. }
  396. static int ioctl_do_sanitize(struct mmc_card *card)
  397. {
  398. int err;
  399. if (!mmc_can_sanitize(card)) {
  400. pr_warn("%s: %s - SANITIZE is not supported\n",
  401. mmc_hostname(card->host), __func__);
  402. err = BLK_STS_NOTSUPP;
  403. goto out;
  404. }
  405. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  406. mmc_hostname(card->host), __func__);
  407. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  408. EXT_CSD_SANITIZE_START, 1,
  409. MMC_SANITIZE_REQ_TIMEOUT);
  410. if (err)
  411. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  412. mmc_hostname(card->host), __func__, err);
  413. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  414. __func__);
  415. out:
  416. return err;
  417. }
  418. static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
  419. struct mmc_blk_ioc_data *idata)
  420. {
  421. struct mmc_command cmd = {};
  422. struct mmc_data data = {};
  423. struct mmc_request mrq = {};
  424. struct scatterlist sg;
  425. int err;
  426. unsigned int target_part;
  427. u32 status = 0;
  428. if (!card || !md || !idata)
  429. return -EINVAL;
  430. /*
  431. * The RPMB accesses comes in from the character device, so we
  432. * need to target these explicitly. Else we just target the
  433. * partition type for the block device the ioctl() was issued
  434. * on.
  435. */
  436. if (idata->rpmb) {
  437. /* Support multiple RPMB partitions */
  438. target_part = idata->rpmb->part_index;
  439. target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
  440. } else {
  441. target_part = md->part_type;
  442. }
  443. cmd.opcode = idata->ic.opcode;
  444. cmd.arg = idata->ic.arg;
  445. cmd.flags = idata->ic.flags;
  446. if (idata->buf_bytes) {
  447. data.sg = &sg;
  448. data.sg_len = 1;
  449. data.blksz = idata->ic.blksz;
  450. data.blocks = idata->ic.blocks;
  451. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  452. if (idata->ic.write_flag)
  453. data.flags = MMC_DATA_WRITE;
  454. else
  455. data.flags = MMC_DATA_READ;
  456. /* data.flags must already be set before doing this. */
  457. mmc_set_data_timeout(&data, card);
  458. /* Allow overriding the timeout_ns for empirical tuning. */
  459. if (idata->ic.data_timeout_ns)
  460. data.timeout_ns = idata->ic.data_timeout_ns;
  461. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  462. /*
  463. * Pretend this is a data transfer and rely on the
  464. * host driver to compute timeout. When all host
  465. * drivers support cmd.cmd_timeout for R1B, this
  466. * can be changed to:
  467. *
  468. * mrq.data = NULL;
  469. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  470. */
  471. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  472. }
  473. mrq.data = &data;
  474. }
  475. mrq.cmd = &cmd;
  476. err = mmc_blk_part_switch(card, target_part);
  477. if (err)
  478. return err;
  479. if (idata->ic.is_acmd) {
  480. err = mmc_app_cmd(card->host, card);
  481. if (err)
  482. return err;
  483. }
  484. if (idata->rpmb) {
  485. err = mmc_set_blockcount(card, data.blocks,
  486. idata->ic.write_flag & (1 << 31));
  487. if (err)
  488. return err;
  489. }
  490. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
  491. (cmd.opcode == MMC_SWITCH)) {
  492. err = ioctl_do_sanitize(card);
  493. if (err)
  494. pr_err("%s: ioctl_do_sanitize() failed. err = %d",
  495. __func__, err);
  496. return err;
  497. }
  498. #ifdef CONFIG_MMC_FFU
  499. if (cmd.opcode == MMC_FFU_DOWNLOAD_OP
  500. || cmd.opcode == MMC_FFU_INSTALL_OP) {
  501. mmc_wait_for_ffu_req(card->host, &mrq);
  502. } else
  503. #endif
  504. mmc_wait_for_req(card->host, &mrq);
  505. if (cmd.error) {
  506. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  507. __func__, cmd.error);
  508. return cmd.error;
  509. }
  510. if (data.error) {
  511. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  512. __func__, data.error);
  513. return data.error;
  514. }
  515. /*
  516. * Make sure the cache of the PARTITION_CONFIG register and
  517. * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
  518. * changed it successfully.
  519. */
  520. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
  521. (cmd.opcode == MMC_SWITCH)) {
  522. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  523. u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
  524. /*
  525. * Update cache so the next mmc_blk_part_switch call operates
  526. * on up-to-date data.
  527. */
  528. card->ext_csd.part_config = value;
  529. main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
  530. }
  531. /*
  532. * Make sure to update CACHE_CTRL in case it was changed. The cache
  533. * will get turned back on if the card is re-initialized, e.g.
  534. * suspend/resume or hw reset in recovery.
  535. */
  536. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
  537. (cmd.opcode == MMC_SWITCH)) {
  538. u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
  539. card->ext_csd.cache_ctrl = value;
  540. }
  541. /*
  542. * According to the SD specs, some commands require a delay after
  543. * issuing the command.
  544. */
  545. if (idata->ic.postsleep_min_us)
  546. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  547. memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
  548. if (idata->rpmb) {
  549. /*
  550. * Ensure RPMB command has completed by polling CMD13
  551. * "Send Status".
  552. */
  553. err = ioctl_rpmb_card_status_poll(card, &status, 5);
  554. if (err)
  555. dev_err(mmc_dev(card->host),
  556. "%s: Card Status=0x%08X, error %d\n",
  557. __func__, status, err);
  558. }
  559. return err;
  560. }
  561. #ifdef CONFIG_MTK_MMC_PWR_WP
  562. static int mmc_pwr_wp_ioctl(struct block_device *bdev, unsigned long arg)
  563. {
  564. struct mmc_blk_data *md;
  565. struct mmc_card *card;
  566. unsigned int power_on_wp_en = 0;
  567. int err = 0;
  568. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  569. return -EPERM;
  570. if (copy_from_user(&power_on_wp_en, (void *)arg, 1))
  571. return -EFAULT;
  572. /*do noting if power-on write protect arg =0*/
  573. if (power_on_wp_en == 0) {
  574. pr_debug("%s: power_on_wp_en = %d\n", __func__, power_on_wp_en);
  575. return 0;
  576. }
  577. md = mmc_blk_get(bdev->bd_disk);
  578. if (!md)
  579. return -EINVAL;
  580. card = md->queue.card;
  581. if (IS_ERR(card)) {
  582. err = PTR_ERR(card);
  583. goto cmd_done;
  584. }
  585. mmc_get_card(card);
  586. /*
  587. * Default partitions defined in mtk_emmc_write_protect.c will set
  588. * power-on write protect by this function.
  589. */
  590. err = set_power_on_write_protect(card);
  591. mmc_put_card(card);
  592. cmd_done:
  593. mmc_blk_put(md);
  594. return err;
  595. }
  596. #endif
  597. #ifdef CONFIG_MTK_EMMC_SUPPORT_OTP
  598. #define MMC_SEND_WRITE_PROT_TYPE 31
  599. #define EXT_CSD_USR_WP 171 /* R/W */
  600. #define US_PERM_WP_EN 4
  601. int mmc_otp_ops_check_bdev(struct block_device *bdev)
  602. {
  603. if (!bdev || !bdev->bd_part || !bdev->bd_part->info
  604. || strcmp(bdev->bd_part->info->volname, "otp"))
  605. return 0;
  606. return 1;
  607. }
  608. int mmc_otp_ops_check(struct block_device *bdev,
  609. struct mmc_ioc_cmd __user *ic_ptr)
  610. {
  611. u32 opcode, arg;
  612. if ((get_user(opcode, &ic_ptr->opcode) == 0) &&
  613. (get_user(arg, &ic_ptr->arg) == 0)) {
  614. if ((opcode == MMC_SET_WRITE_PROT)
  615. || (opcode == MMC_CLR_WRITE_PROT)
  616. || (opcode == MMC_SEND_WRITE_PROT)
  617. || (opcode == MMC_SEND_WRITE_PROT_TYPE)) {
  618. if (arg >= bdev->bd_part->nr_sects)
  619. return -EFAULT;
  620. arg += bdev->bd_part->start_sect;
  621. } else if (opcode == MMC_SWITCH) {
  622. if (((arg >> 16) & 0xFF) != EXT_CSD_USR_WP)
  623. return -EPERM;
  624. #ifndef CONFIG_MTK_EMMC_SUPPORT_OTP_FOR_CUSTOMER
  625. /* prevent users' permanent writ protect in sqc */
  626. if (((arg >> 8) & US_PERM_WP_EN)
  627. && ((((arg >> 24) & 0x3) == MMC_SWITCH_MODE_SET_BITS)
  628. || (((arg >> 24) & 0x3) == MMC_SWITCH_MODE_WRITE_BYTE))) {
  629. return -EPERM;
  630. }
  631. #endif
  632. } else {
  633. return -EPERM;
  634. }
  635. if (put_user(arg, &ic_ptr->arg) != 0)
  636. return -EFAULT;
  637. } else
  638. return -EFAULT;
  639. return 0;
  640. }
  641. #endif
  642. #ifdef CONFIG_MMC_FFU
  643. int mmc_ffu_ops_check_bdev(struct block_device *bdev,
  644. struct mmc_ioc_cmd __user *ic_ptr)
  645. {
  646. u32 opcode;
  647. struct mmc_blk_data *md;
  648. struct mmc_card *card;
  649. md = mmc_blk_get(bdev->bd_disk);
  650. if (!md)
  651. return 0;
  652. card = md->queue.card;
  653. if (IS_ERR(card) || !mmc_card_mmc(card)) {
  654. mmc_blk_put(md);
  655. return 0;
  656. }
  657. if (get_user(opcode, &ic_ptr->opcode) == 0) {
  658. if (opcode == MMC_FFU_DOWNLOAD_OP
  659. || opcode == MMC_FFU_INSTALL_OP
  660. || opcode == MMC_SEND_EXT_CSD) {
  661. mmc_blk_put(md);
  662. return 1;
  663. }
  664. }
  665. mmc_blk_put(md);
  666. return 0;
  667. }
  668. #endif
  669. static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
  670. struct mmc_ioc_cmd __user *ic_ptr,
  671. struct mmc_rpmb_data *rpmb)
  672. {
  673. struct mmc_blk_ioc_data *idata;
  674. struct mmc_blk_ioc_data *idatas[1];
  675. struct mmc_queue *mq;
  676. struct mmc_card *card;
  677. int err = 0, ioc_err = 0;
  678. struct request *req;
  679. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  680. if (IS_ERR(idata))
  681. return PTR_ERR(idata);
  682. /* This will be NULL on non-RPMB ioctl():s */
  683. idata->rpmb = rpmb;
  684. card = md->queue.card;
  685. if (IS_ERR(card)) {
  686. err = PTR_ERR(card);
  687. goto cmd_done;
  688. }
  689. /*
  690. * Dispatch the ioctl() into the block request queue.
  691. */
  692. mq = &md->queue;
  693. req = blk_get_request(mq->queue,
  694. idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
  695. __GFP_RECLAIM);
  696. if (IS_ERR(req)) {
  697. err = PTR_ERR(req);
  698. goto cmd_done;
  699. }
  700. idatas[0] = idata;
  701. req_to_mmc_queue_req(req)->drv_op =
  702. rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
  703. req_to_mmc_queue_req(req)->drv_op_data = idatas;
  704. req_to_mmc_queue_req(req)->ioc_count = 1;
  705. blk_execute_rq(mq->queue, NULL, req, 0);
  706. ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
  707. err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
  708. blk_put_request(req);
  709. cmd_done:
  710. kfree(idata->buf);
  711. kfree(idata);
  712. return ioc_err ? ioc_err : err;
  713. }
  714. static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
  715. struct mmc_ioc_multi_cmd __user *user,
  716. struct mmc_rpmb_data *rpmb)
  717. {
  718. struct mmc_blk_ioc_data **idata = NULL;
  719. struct mmc_ioc_cmd __user *cmds = user->cmds;
  720. struct mmc_card *card;
  721. struct mmc_queue *mq;
  722. int i, err = 0, ioc_err = 0;
  723. __u64 num_of_cmds;
  724. struct request *req;
  725. if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
  726. sizeof(num_of_cmds)))
  727. return -EFAULT;
  728. if (!num_of_cmds)
  729. return 0;
  730. if (num_of_cmds > MMC_IOC_MAX_CMDS)
  731. return -EINVAL;
  732. idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
  733. if (!idata)
  734. return -ENOMEM;
  735. for (i = 0; i < num_of_cmds; i++) {
  736. idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
  737. if (IS_ERR(idata[i])) {
  738. err = PTR_ERR(idata[i]);
  739. num_of_cmds = i;
  740. goto cmd_err;
  741. }
  742. /* This will be NULL on non-RPMB ioctl():s */
  743. idata[i]->rpmb = rpmb;
  744. }
  745. card = md->queue.card;
  746. if (IS_ERR(card)) {
  747. err = PTR_ERR(card);
  748. goto cmd_err;
  749. }
  750. /*
  751. * Dispatch the ioctl()s into the block request queue.
  752. */
  753. mq = &md->queue;
  754. req = blk_get_request(mq->queue,
  755. idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
  756. __GFP_RECLAIM);
  757. if (IS_ERR(req)) {
  758. err = PTR_ERR(req);
  759. goto cmd_err;
  760. }
  761. req_to_mmc_queue_req(req)->drv_op =
  762. rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
  763. req_to_mmc_queue_req(req)->drv_op_data = idata;
  764. req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
  765. blk_execute_rq(mq->queue, NULL, req, 0);
  766. ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
  767. /* copy to user if data and response */
  768. for (i = 0; i < num_of_cmds && !err; i++)
  769. err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
  770. blk_put_request(req);
  771. cmd_err:
  772. for (i = 0; i < num_of_cmds; i++) {
  773. kfree(idata[i]->buf);
  774. kfree(idata[i]);
  775. }
  776. kfree(idata);
  777. return ioc_err ? ioc_err : err;
  778. }
  779. #ifdef CONFIG_MMC_FFU
  780. static int mt_ffu_mmc_blk_check_blkdev(struct block_device *bdev,
  781. struct mmc_ioc_cmd __user *ic_ptr)
  782. {
  783. /*
  784. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  785. * whole block device, not on a partition. This prevents overspray
  786. * between sibling partitions.
  787. */
  788. if ((!mmc_ffu_ops_check_bdev(bdev, ic_ptr) &&
  789. !capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  790. return -EPERM;
  791. return 0;
  792. }
  793. #endif
  794. #define MMC_BLK_NO_WP 0
  795. #define MMC_BLK_PARTIALLY_WP 1
  796. #define MMC_BLK_FULLY_WP 2
  797. static int mmc_blk_check_disk_range_wp(struct gendisk *disk,
  798. sector_t part_start, sector_t part_nr_sects)
  799. {
  800. struct mmc_command cmd = {0};
  801. struct mmc_request mrq = {NULL};
  802. struct mmc_data data = {0};
  803. struct mmc_blk_data *md;
  804. struct mmc_card *card;
  805. struct scatterlist sg;
  806. unsigned char *buf = NULL, status;
  807. sector_t start, end, quot;
  808. sector_t wp_grp_rem, wp_grp_total, wp_grp_found, status_query_cnt;
  809. unsigned int remain;
  810. int err = 0, i, j, k;
  811. u8 boot_wp_status = 0;
  812. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  813. bool cmdq_en = false;
  814. #endif
  815. md = mmc_blk_get(disk);
  816. if (!md)
  817. return -EINVAL;
  818. if (!md->queue.card) {
  819. err = -EINVAL;
  820. goto out2;
  821. }
  822. card = md->queue.card;
  823. /* NMCARD use a eMMC4.5-like protocol but it is extern storage,
  824. * no need check WP status.
  825. */
  826. if (!mmc_card_mmc(card) ||
  827. (card->host->caps2 & MMC_CAP2_NMCARD) ||
  828. md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  829. err = MMC_BLK_NO_WP;
  830. goto out2;
  831. }
  832. /* BOOT_WP_STATUS in EXT_CSD:
  833. * |-----bit[7:4]-----|-------bit[3:2]--------|-------bit[1:0]--------|
  834. * |-----reserved-----|----boot1 wp status----|----boot0 wp status----|
  835. * boot0 area wp type:depending on bit[1:0]
  836. * 0->not wp; 1->power on wp; 2->permanent wp; 3:reserved value
  837. * boot1 area wp type:depending on bit[3:2]
  838. * 0->not wp; 1->power on wp; 2->permanent wp; 3:reserved value
  839. */
  840. if (md->part_type == EXT_CSD_PART_CONFIG_ACC_BOOT0) {
  841. boot_wp_status = card->ext_csd.boot_wp_status & 0x3;
  842. if (boot_wp_status == 0x1 || boot_wp_status == 0x2) {
  843. pr_notice("%s is fully write protected\n",
  844. disk->disk_name);
  845. err = MMC_BLK_FULLY_WP;
  846. } else
  847. err = MMC_BLK_NO_WP;
  848. goto out2;
  849. }
  850. /* EXT_CSD_PART_CONFIG_ACC_BOOT0 + 1 <=> BOOT1 */
  851. if (md->part_type == (EXT_CSD_PART_CONFIG_ACC_BOOT0 + 1)) {
  852. boot_wp_status = (card->ext_csd.boot_wp_status >> 2) & 0x3;
  853. if (boot_wp_status == 0x1 || boot_wp_status == 0x2) {
  854. pr_notice("%s is fully write protected\n",
  855. disk->disk_name);
  856. err = MMC_BLK_FULLY_WP;
  857. } else
  858. err = MMC_BLK_NO_WP;
  859. goto out2;
  860. }
  861. if (!card->wp_grp_size) {
  862. pr_notice("Write protect group size cannot be 0!\n");
  863. err = -EINVAL;
  864. goto out2;
  865. }
  866. start = part_start;
  867. quot = start;
  868. remain = do_div(quot, card->wp_grp_size);
  869. if (remain) {
  870. pr_notice("Start 0x%llx of disk %s not write group aligned\n",
  871. (unsigned long long)part_start, disk->disk_name);
  872. start -= remain;
  873. }
  874. end = part_start + part_nr_sects;
  875. quot = end;
  876. remain = do_div(quot, card->wp_grp_size);
  877. if (remain) {
  878. pr_notice("End 0x%llx of disk %s not write group aligned\n",
  879. (unsigned long long)part_start, disk->disk_name);
  880. end += card->wp_grp_size - remain;
  881. }
  882. wp_grp_total = end - start;
  883. do_div(wp_grp_total, card->wp_grp_size);
  884. wp_grp_rem = wp_grp_total;
  885. wp_grp_found = 0;
  886. cmd.opcode = MMC_SEND_WRITE_PROT_TYPE;
  887. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  888. buf = kmalloc(8, GFP_KERNEL);
  889. if (!buf) {
  890. err = -ENOMEM;
  891. goto out2;
  892. }
  893. sg_init_one(&sg, buf, 8);
  894. data.blksz = 8;
  895. data.blocks = 1;
  896. data.flags = MMC_DATA_READ;
  897. data.sg = &sg;
  898. data.sg_len = 1;
  899. mmc_set_data_timeout(&data, card);
  900. mrq.cmd = &cmd;
  901. mrq.data = &data;
  902. mmc_get_card(card);
  903. err = mmc_blk_part_switch(card, md->part_type);
  904. if (err) {
  905. err = -EIO;
  906. goto out;
  907. }
  908. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  909. cmdq_en = !!mmc_card_cmdq(card);
  910. if (cmdq_en) {
  911. err = mmc_cmdq_disable(card);
  912. if (err) {
  913. pr_notice("%s, %s: disable cmdq error %d\n",
  914. __func__, mmc_hostname(card->host), err);
  915. err = -EIO;
  916. goto out;
  917. }
  918. }
  919. #endif
  920. status_query_cnt = (wp_grp_total + 31) / 32;
  921. for (i = 0; i < status_query_cnt; i++) {
  922. cmd.arg = start + i * card->wp_grp_size * 32;
  923. mmc_wait_for_req(card->host, &mrq);
  924. if (cmd.error) {
  925. pr_notice("%s: cmd error %d\n", __func__, cmd.error);
  926. err = -EIO;
  927. goto out;
  928. }
  929. /* wp status is returned in 8 bytes.
  930. * The 8 bytes is regarded as 64-bits bit-stream:
  931. * +--------+--------+-------------------------+--------+
  932. * | byte 7 | byte 6 | ... | byte 0 |
  933. * | bits | bits | | bits |
  934. * |76543210|76543210| |76543210|
  935. * +--------+--------+-------------------------+--------+
  936. * The 2 LSBits represent write-protect group status of
  937. * the lowest address group being queried.
  938. * The 2 MSBits represent write-protect group status of
  939. * the highst address group being queried.
  940. */
  941. /* Check write-protect group status from lowest address
  942. * group to highest address group
  943. */
  944. for (j = 0; j < 8; j++) {
  945. status = buf[7 - j];
  946. for (k = 0; k < 8; k += 2) {
  947. if (status & (3 << k))
  948. wp_grp_found++;
  949. wp_grp_rem--;
  950. if (!wp_grp_rem)
  951. goto out;
  952. }
  953. }
  954. memset(buf, 0, 8);
  955. }
  956. out:
  957. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  958. if (cmdq_en) {
  959. err = mmc_cmdq_enable(card);
  960. if (err) {
  961. pr_notice("%s, %s: enable cmdq error %d\n",
  962. __func__, mmc_hostname(card->host), err);
  963. err = -EIO;
  964. }
  965. }
  966. #endif
  967. mmc_put_card(card);
  968. if (!wp_grp_rem) {
  969. if (!wp_grp_found)
  970. err = MMC_BLK_NO_WP;
  971. else if (wp_grp_found == wp_grp_total) {
  972. pr_notice("0x%llx ~ 0x%llx of %s is fully write protected\n",
  973. (unsigned long long)part_start,
  974. (unsigned long long)part_start + part_nr_sects,
  975. disk->disk_name);
  976. err = MMC_BLK_FULLY_WP;
  977. } else {
  978. pr_notice("0x%llx ~ 0x%llx of %s is partially write protected\n",
  979. (unsigned long long)part_start,
  980. (unsigned long long)part_start + part_nr_sects,
  981. disk->disk_name);
  982. err = MMC_BLK_PARTIALLY_WP;
  983. }
  984. }
  985. kfree(buf);
  986. out2:
  987. mmc_blk_put(md);
  988. return err;
  989. }
  990. static int mmc_blk_check_wp(struct block_device *bdev)
  991. {
  992. if (!bdev->bd_disk || !bdev->bd_part)
  993. return -EINVAL;
  994. return mmc_blk_check_disk_range_wp(bdev->bd_disk,
  995. bdev->bd_part->start_sect,
  996. bdev->bd_part->nr_sects);
  997. }
  998. static int mmc_blk_ioctl_roset(struct block_device *bdev,
  999. unsigned long arg)
  1000. {
  1001. int val;
  1002. /* Always return -EACCES to block layer on any error
  1003. * and then block layer will abort the remaining operation
  1004. */
  1005. if (get_user(val, (int __user *)arg))
  1006. return -EACCES;
  1007. /* No need to check write-protect status when setting as readonly */
  1008. if (val)
  1009. return 0;
  1010. if (mmc_blk_check_wp(bdev) != MMC_BLK_NO_WP)
  1011. return -EACCES;
  1012. return 0;
  1013. }
  1014. static int mmc_blk_check_blkdev(struct block_device *bdev)
  1015. {
  1016. /*
  1017. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  1018. * whole block device, not on a partition. This prevents overspray
  1019. * between sibling partitions.
  1020. */
  1021. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  1022. return -EPERM;
  1023. return 0;
  1024. }
  1025. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  1026. unsigned int cmd, unsigned long arg)
  1027. {
  1028. struct mmc_blk_data *md;
  1029. int ret;
  1030. #ifdef CONFIG_MTK_EMMC_SUPPORT_OTP
  1031. bool otp_dev;
  1032. #endif
  1033. switch (cmd) {
  1034. case MMC_IOC_CMD:
  1035. #ifdef CONFIG_MTK_EMMC_SUPPORT_OTP
  1036. otp_dev = mmc_otp_ops_check_bdev(bdev);
  1037. if (!otp_dev) {
  1038. #endif
  1039. #ifdef CONFIG_MMC_FFU
  1040. ret = mt_ffu_mmc_blk_check_blkdev(bdev,
  1041. (struct mmc_ioc_cmd __user *)arg);
  1042. #else
  1043. ret = mmc_blk_check_blkdev(bdev);
  1044. #endif
  1045. #ifdef CONFIG_MTK_EMMC_SUPPORT_OTP
  1046. } else
  1047. ret = mmc_otp_ops_check(bdev,
  1048. (struct mmc_ioc_cmd __user *)arg);
  1049. #endif
  1050. if (ret)
  1051. return ret;
  1052. md = mmc_blk_get(bdev->bd_disk);
  1053. if (!md)
  1054. return -EINVAL;
  1055. ret = mmc_blk_ioctl_cmd(md,
  1056. (struct mmc_ioc_cmd __user *)arg,
  1057. NULL);
  1058. mmc_blk_put(md);
  1059. return ret;
  1060. case MMC_IOC_MULTI_CMD:
  1061. ret = mmc_blk_check_blkdev(bdev);
  1062. if (ret)
  1063. return ret;
  1064. md = mmc_blk_get(bdev->bd_disk);
  1065. if (!md)
  1066. return -EINVAL;
  1067. ret = mmc_blk_ioctl_multi_cmd(md,
  1068. (struct mmc_ioc_multi_cmd __user *)arg,
  1069. NULL);
  1070. mmc_blk_put(md);
  1071. return ret;
  1072. #ifdef CONFIG_MTK_MMC_PWR_WP
  1073. case MMC_IOC_WP_CMD:
  1074. return mmc_pwr_wp_ioctl(bdev, arg);
  1075. #endif
  1076. case BLKROSET:
  1077. return mmc_blk_ioctl_roset(bdev, arg);
  1078. default:
  1079. return -EINVAL;
  1080. }
  1081. }
  1082. #ifdef CONFIG_COMPAT
  1083. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1084. unsigned int cmd, unsigned long arg)
  1085. {
  1086. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  1087. }
  1088. #endif
  1089. static const struct block_device_operations mmc_bdops = {
  1090. .open = mmc_blk_open,
  1091. .release = mmc_blk_release,
  1092. .getgeo = mmc_blk_getgeo,
  1093. .owner = THIS_MODULE,
  1094. .ioctl = mmc_blk_ioctl,
  1095. #ifdef CONFIG_COMPAT
  1096. .compat_ioctl = mmc_blk_compat_ioctl,
  1097. #endif
  1098. .check_disk_range_wp = mmc_blk_check_disk_range_wp,
  1099. };
  1100. static int mmc_blk_part_switch_pre(struct mmc_card *card,
  1101. unsigned int part_type)
  1102. {
  1103. int ret = 0;
  1104. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  1105. /* disabe cmdq
  1106. * if partition does not support cmdq
  1107. */
  1108. if (mmc_card_cmdq(card)
  1109. && !(part_type <= PART_CMDQ_EN)) {
  1110. ret = mmc_cmdq_disable(card);
  1111. if (ret)
  1112. return ret;
  1113. }
  1114. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
  1115. mmc_retune_pause(card->host);
  1116. #else
  1117. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  1118. if (card->ext_csd.cmdq_en) {
  1119. ret = mmc_cmdq_disable(card);
  1120. if (ret)
  1121. return ret;
  1122. }
  1123. mmc_retune_pause(card->host);
  1124. }
  1125. #endif
  1126. return ret;
  1127. }
  1128. static int mmc_blk_part_switch_post(struct mmc_card *card,
  1129. unsigned int part_type)
  1130. {
  1131. int ret = 0;
  1132. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  1133. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
  1134. mmc_retune_unpause(card->host);
  1135. /* enable cmdq
  1136. * if partition supports cmdq
  1137. */
  1138. if ((!mmc_card_cmdq(card)) && (part_type <= PART_CMDQ_EN)) {
  1139. ret = mmc_cmdq_enable(card);
  1140. if (ret)
  1141. return ret;
  1142. }
  1143. #else
  1144. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  1145. mmc_retune_unpause(card->host);
  1146. if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
  1147. ret = mmc_cmdq_enable(card);
  1148. }
  1149. #endif
  1150. return ret;
  1151. }
  1152. static inline int mmc_blk_part_switch(struct mmc_card *card,
  1153. unsigned int part_type)
  1154. {
  1155. int ret = 0;
  1156. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  1157. if (main_md->part_curr == part_type)
  1158. return 0;
  1159. if (mmc_card_mmc(card)) {
  1160. u8 part_config = card->ext_csd.part_config;
  1161. ret = mmc_blk_part_switch_pre(card, part_type);
  1162. if (ret)
  1163. return ret;
  1164. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  1165. part_config |= part_type;
  1166. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1167. EXT_CSD_PART_CONFIG, part_config,
  1168. card->ext_csd.part_time);
  1169. if (ret) {
  1170. mmc_blk_part_switch_post(card, part_type);
  1171. return ret;
  1172. }
  1173. card->ext_csd.part_config = part_config;
  1174. ret = mmc_blk_part_switch_post(card, part_type);
  1175. }
  1176. #ifdef CONFIG_MTK_EMMC_HW_CQ
  1177. card->part_curr = part_type;
  1178. #endif
  1179. main_md->part_curr = part_type;
  1180. return ret;
  1181. }
  1182. static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
  1183. {
  1184. int err;
  1185. u32 result;
  1186. __be32 *blocks;
  1187. struct mmc_request mrq = {};
  1188. struct mmc_command cmd = {};
  1189. struct mmc_data data = {};
  1190. struct scatterlist sg;
  1191. cmd.opcode = MMC_APP_CMD;
  1192. cmd.arg = card->rca << 16;
  1193. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1194. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1195. if (err)
  1196. return err;
  1197. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  1198. return -EIO;
  1199. memset(&cmd, 0, sizeof(struct mmc_command));
  1200. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  1201. cmd.arg = 0;
  1202. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1203. data.blksz = 4;
  1204. data.blocks = 1;
  1205. data.flags = MMC_DATA_READ;
  1206. data.sg = &sg;
  1207. data.sg_len = 1;
  1208. mmc_set_data_timeout(&data, card);
  1209. mrq.cmd = &cmd;
  1210. mrq.data = &data;
  1211. blocks = kmalloc(4, GFP_KERNEL);
  1212. if (!blocks)
  1213. return -ENOMEM;
  1214. sg_init_one(&sg, blocks, 4);
  1215. mmc_wait_for_req(card->host, &mrq);
  1216. result = ntohl(*blocks);
  1217. kfree(blocks);
  1218. if (cmd.error || data.error)
  1219. return -EIO;
  1220. *written_blocks = result;
  1221. return 0;
  1222. }
  1223. static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
  1224. bool hw_busy_detect, struct request *req, bool *gen_err)
  1225. {
  1226. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  1227. int err = 0;
  1228. u32 status;
  1229. do {
  1230. err = __mmc_send_status(card, &status, 5);
  1231. if (err) {
  1232. pr_err("%s: error %d requesting status\n",
  1233. req->rq_disk->disk_name, err);
  1234. return err;
  1235. }
  1236. if (status & R1_ERROR) {
  1237. pr_err("%s: %s: error sending status cmd, status %#x\n",
  1238. req->rq_disk->disk_name, __func__, status);
  1239. *gen_err = true;
  1240. }
  1241. /* We may rely on the host hw to handle busy detection.*/
  1242. if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
  1243. hw_busy_detect)
  1244. break;
  1245. /*
  1246. * Timeout if the device never becomes ready for data and never
  1247. * leaves the program state.
  1248. */
  1249. if (time_after(jiffies, timeout)) {
  1250. pr_err("%s: Card stuck in programming state! %s %s\n",
  1251. mmc_hostname(card->host),
  1252. req->rq_disk->disk_name, __func__);
  1253. return -ETIMEDOUT;
  1254. }
  1255. /*
  1256. * Some cards mishandle the status bits,
  1257. * so make sure to check both the busy
  1258. * indication and the card state.
  1259. */
  1260. } while (!(status & R1_READY_FOR_DATA) ||
  1261. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  1262. return err;
  1263. }
  1264. static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
  1265. struct request *req, bool *gen_err, u32 *stop_status)
  1266. {
  1267. struct mmc_host *host = card->host;
  1268. struct mmc_command cmd = {};
  1269. int err;
  1270. bool use_r1b_resp = rq_data_dir(req) == WRITE;
  1271. /*
  1272. * Normally we use R1B responses for WRITE, but in cases where the host
  1273. * has specified a max_busy_timeout we need to validate it. A failure
  1274. * means we need to prevent the host from doing hw busy detection, which
  1275. * is done by converting to a R1 response instead.
  1276. */
  1277. if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
  1278. use_r1b_resp = false;
  1279. cmd.opcode = MMC_STOP_TRANSMISSION;
  1280. if (use_r1b_resp) {
  1281. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1282. cmd.busy_timeout = timeout_ms;
  1283. } else {
  1284. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1285. }
  1286. err = mmc_wait_for_cmd(host, &cmd, 5);
  1287. if (err)
  1288. return err;
  1289. *stop_status = cmd.resp[0];
  1290. /* No need to check card status in case of READ. */
  1291. if (rq_data_dir(req) == READ)
  1292. return 0;
  1293. if (!mmc_host_is_spi(host) &&
  1294. (*stop_status & R1_ERROR)) {
  1295. pr_err("%s: %s: general error sending stop command, resp %#x\n",
  1296. req->rq_disk->disk_name, __func__, *stop_status);
  1297. *gen_err = true;
  1298. }
  1299. return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
  1300. }
  1301. #define ERR_NOMEDIUM 3
  1302. #define ERR_RETRY 2
  1303. #define ERR_ABORT 1
  1304. #define ERR_CONTINUE 0
  1305. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  1306. bool status_valid, u32 status)
  1307. {
  1308. switch (error) {
  1309. case -EILSEQ:
  1310. /* response crc error, retry the r/w cmd */
  1311. pr_err("%s: %s sending %s command, card status %#x\n",
  1312. req->rq_disk->disk_name, "response CRC error",
  1313. name, status);
  1314. return ERR_RETRY;
  1315. case -ETIMEDOUT:
  1316. pr_err("%s: %s sending %s command, card status %#x\n",
  1317. req->rq_disk->disk_name, "timed out", name, status);
  1318. /* If the status cmd initially failed, retry the r/w cmd */
  1319. if (!status_valid) {
  1320. pr_err("%s: status not valid, retrying timeout\n",
  1321. req->rq_disk->disk_name);
  1322. return ERR_RETRY;
  1323. }
  1324. /*
  1325. * If it was a r/w cmd crc error, or illegal command
  1326. * (eg, issued in wrong state) then retry - we should
  1327. * have corrected the state problem above.
  1328. */
  1329. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
  1330. pr_err("%s: command error, retrying timeout\n",
  1331. req->rq_disk->disk_name);
  1332. return ERR_RETRY;
  1333. }
  1334. /* Otherwise abort the command */
  1335. return ERR_ABORT;
  1336. default:
  1337. /* We don't understand the error code the driver gave us */
  1338. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  1339. req->rq_disk->disk_name, error, status);
  1340. return ERR_ABORT;
  1341. }
  1342. }
  1343. /*
  1344. * Initial r/w and stop cmd error recovery.
  1345. * We don't know whether the card received the r/w cmd or not, so try to
  1346. * restore things back to a sane state. Essentially, we do this as follows:
  1347. * - Obtain card status. If the first attempt to obtain card status fails,
  1348. * the status word will reflect the failed status cmd, not the failed
  1349. * r/w cmd. If we fail to obtain card status, it suggests we can no
  1350. * longer communicate with the card.
  1351. * - Check the card state. If the card received the cmd but there was a
  1352. * transient problem with the response, it might still be in a data transfer
  1353. * mode. Try to send it a stop command. If this fails, we can't recover.
  1354. * - If the r/w cmd failed due to a response CRC error, it was probably
  1355. * transient, so retry the cmd.
  1356. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  1357. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  1358. * illegal cmd, retry.
  1359. * Otherwise we don't understand what happened, so abort.
  1360. */
  1361. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  1362. struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
  1363. {
  1364. bool prev_cmd_status_valid = true;
  1365. u32 status, stop_status = 0;
  1366. int err, retry;
  1367. if (mmc_card_removed(card))
  1368. return ERR_NOMEDIUM;
  1369. /*
  1370. * Try to get card status which indicates both the card state
  1371. * and why there was no response. If the first attempt fails,
  1372. * we can't be sure the returned status is for the r/w command.
  1373. */
  1374. for (retry = 2; retry >= 0; retry--) {
  1375. err = __mmc_send_status(card, &status, 0);
  1376. if (!err)
  1377. break;
  1378. /* Re-tune if needed */
  1379. mmc_retune_recheck(card->host);
  1380. prev_cmd_status_valid = false;
  1381. pr_err("%s: error %d sending status command, %sing\n",
  1382. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  1383. }
  1384. /* We couldn't get a response from the card. Give up. */
  1385. if (err) {
  1386. /* Check if the card is removed */
  1387. if (mmc_detect_card_removed(card->host))
  1388. return ERR_NOMEDIUM;
  1389. return ERR_ABORT;
  1390. }
  1391. /* Flag ECC errors */
  1392. if ((status & R1_CARD_ECC_FAILED) ||
  1393. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  1394. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  1395. (brq->sbc.resp[0] & R1_CARD_ECC_FAILED) ||
  1396. (brq->que.resp[0] & R1_CARD_ECC_FAILED) ||
  1397. #endif
  1398. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  1399. *ecc_err = true;
  1400. /* Flag General errors */
  1401. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
  1402. if ((status & R1_ERROR) ||
  1403. (brq->stop.resp[0] & R1_ERROR)) {
  1404. pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
  1405. req->rq_disk->disk_name, __func__,
  1406. brq->stop.resp[0], status);
  1407. *gen_err = true;
  1408. }
  1409. /*
  1410. * Check the current card state. If it is in some data transfer
  1411. * mode, tell it to stop (and hopefully transition back to TRAN.)
  1412. */
  1413. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  1414. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  1415. err = send_stop(card,
  1416. DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
  1417. req, gen_err, &stop_status);
  1418. if (err) {
  1419. pr_err("%s: error %d sending stop command\n",
  1420. req->rq_disk->disk_name, err);
  1421. /*
  1422. * If the stop cmd also timed out, the card is probably
  1423. * not present, so abort. Other errors are bad news too.
  1424. */
  1425. return ERR_ABORT;
  1426. }
  1427. if (stop_status & R1_CARD_ECC_FAILED)
  1428. *ecc_err = true;
  1429. }
  1430. /* Check for set block count errors */
  1431. if (brq->sbc.error)
  1432. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  1433. prev_cmd_status_valid, status);
  1434. /* Check for r/w command errors */
  1435. if (brq->cmd.error)
  1436. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  1437. prev_cmd_status_valid, status);
  1438. /* Data errors */
  1439. if (!brq->stop.error)
  1440. return ERR_CONTINUE;
  1441. /* Now for stop errors. These aren't fatal to the transfer. */
  1442. pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  1443. req->rq_disk->disk_name, brq->stop.error,
  1444. brq->cmd.resp[0], status);
  1445. /*
  1446. * Subsitute in our own stop status as this will give the error
  1447. * state which happened during the execution of the r/w command.
  1448. */
  1449. if (stop_status) {
  1450. brq->stop.resp[0] = stop_status;
  1451. brq->stop.error = 0;
  1452. }
  1453. return ERR_CONTINUE;
  1454. }
  1455. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  1456. int type)
  1457. {
  1458. int err;
  1459. if (md->reset_done & type)
  1460. return -EEXIST;
  1461. md->reset_done |= type;
  1462. err = mmc_hw_reset(host);
  1463. #ifdef CONFIG_MTK_EMMC_HW_CQ
  1464. if (err && err != -EOPNOTSUPP) {
  1465. /* We failed to reset so we need to abort the request */
  1466. pr_notice("%s: %s: failed to reset %d\n", mmc_hostname(host),
  1467. __func__, err);
  1468. if (host->card && mmc_card_sd(host->card)) {
  1469. pr_notice("%s: %s removing bad card.\n",
  1470. mmc_hostname(host), __func__);
  1471. host->ops->remove_bad_sdcard(host);
  1472. }
  1473. return -ENODEV;
  1474. }
  1475. /* Ensure we switch back to the correct partition */
  1476. if (host->card) {
  1477. #else
  1478. if (err != -EOPNOTSUPP) {
  1479. #endif
  1480. struct mmc_blk_data *main_md =
  1481. dev_get_drvdata(&host->card->dev);
  1482. int part_err;
  1483. main_md->part_curr = main_md->part_type;
  1484. part_err = mmc_blk_part_switch(host->card, md->part_type);
  1485. if (part_err) {
  1486. /*
  1487. * We have failed to get back into the correct
  1488. * partition, so we need to abort the whole request.
  1489. */
  1490. return -ENODEV;
  1491. }
  1492. }
  1493. return err;
  1494. }
  1495. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  1496. {
  1497. md->reset_done &= ~type;
  1498. }
  1499. #ifdef CONFIG_MTK_EMMC_HW_CQ
  1500. static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
  1501. struct request *req)
  1502. {
  1503. struct mmc_blk_data *md = mq->blkdata;
  1504. struct mmc_card *card = md->queue.card;
  1505. struct mmc_host *host = card->host;
  1506. struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
  1507. struct mmc_cmdq_req *cmdq_req;
  1508. struct mmc_queue_req *active_mqrq;
  1509. WARN_ON(req->tag > card->ext_csd.cmdq_depth); /*bug*/
  1510. WARN_ON(test_and_set_bit(req->tag,
  1511. &host->cmdq_ctx.active_reqs)); /*bug*/
  1512. set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
  1513. active_mqrq = &mq->mqrq_cmdq[req->tag];
  1514. active_mqrq->req = req;
  1515. cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
  1516. cmdq_req->cmdq_req_flags |= QBR;
  1517. cmdq_req->mrq.cmd = &cmdq_req->cmd;
  1518. cmdq_req->tag = req->tag;
  1519. return cmdq_req;
  1520. }
  1521. static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
  1522. struct request *req)
  1523. {
  1524. struct mmc_blk_data *md = mq->blkdata;
  1525. struct mmc_card *card = md->queue.card;
  1526. struct mmc_cmdq_req *cmdq_req = NULL;
  1527. unsigned int from, nr, arg;
  1528. int err = 0;
  1529. if (!mmc_can_erase(card)) {
  1530. err = BLK_STS_NOTSUPP;
  1531. blk_end_request(req, err, blk_rq_bytes(req));
  1532. goto out;
  1533. }
  1534. from = blk_rq_pos(req);
  1535. nr = blk_rq_sectors(req);
  1536. if (mmc_can_discard(card))
  1537. arg = MMC_DISCARD_ARG;
  1538. else if (mmc_can_trim(card))
  1539. arg = MMC_TRIM_ARG;
  1540. else
  1541. arg = MMC_ERASE_ARG;
  1542. cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
  1543. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1544. __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
  1545. EXT_CSD_CMD_SET_NORMAL,
  1546. INAND_CMD38_ARG_EXT_CSD,
  1547. arg == MMC_TRIM_ARG ?
  1548. INAND_CMD38_ARG_TRIM :
  1549. INAND_CMD38_ARG_ERASE,
  1550. 0, true, false);
  1551. err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
  1552. if (err)
  1553. goto clear_dcmd;
  1554. }
  1555. err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
  1556. clear_dcmd:
  1557. blk_complete_request(req);
  1558. out:
  1559. return err ? 1 : 0;
  1560. }
  1561. #endif
  1562. /*
  1563. * The non-block commands come back from the block layer after it queued it and
  1564. * processed it with all other requests and then they get issued in this
  1565. * function.
  1566. */
  1567. static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
  1568. {
  1569. struct mmc_queue_req *mq_rq;
  1570. struct mmc_card *card = mq->card;
  1571. struct mmc_blk_data *md = mq->blkdata;
  1572. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  1573. struct mmc_blk_ioc_data **idata;
  1574. bool rpmb_ioctl;
  1575. u8 **ext_csd;
  1576. u32 status;
  1577. int ret;
  1578. int i;
  1579. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  1580. unsigned char cmdq_en;
  1581. cmdq_en = !!mmc_card_cmdq(card);
  1582. /* disable cmdq if partition doesn't support */
  1583. if (cmdq_en) {
  1584. ret = mmc_cmdq_disable(card);
  1585. if (ret) {
  1586. pr_notice("MMC ioctl:disable CQ fail %s\n", __func__);
  1587. return;
  1588. }
  1589. }
  1590. #endif
  1591. mq_rq = req_to_mmc_queue_req(req);
  1592. rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
  1593. switch (mq_rq->drv_op) {
  1594. case MMC_DRV_OP_IOCTL:
  1595. case MMC_DRV_OP_IOCTL_RPMB:
  1596. idata = mq_rq->drv_op_data;
  1597. for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
  1598. ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
  1599. if (ret)
  1600. break;
  1601. }
  1602. /* Always switch back to main area after RPMB access */
  1603. if (rpmb_ioctl)
  1604. mmc_blk_part_switch(card, 0);
  1605. break;
  1606. case MMC_DRV_OP_BOOT_WP:
  1607. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
  1608. card->ext_csd.boot_ro_lock |
  1609. EXT_CSD_BOOT_WP_B_PWR_WP_EN,
  1610. card->ext_csd.part_time);
  1611. if (ret)
  1612. pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
  1613. md->disk->disk_name, ret);
  1614. else
  1615. card->ext_csd.boot_ro_lock |=
  1616. EXT_CSD_BOOT_WP_B_PWR_WP_EN;
  1617. break;
  1618. case MMC_DRV_OP_GET_CARD_STATUS:
  1619. ret = mmc_send_status(card, &status);
  1620. if (!ret)
  1621. ret = status;
  1622. break;
  1623. case MMC_DRV_OP_GET_EXT_CSD:
  1624. ext_csd = mq_rq->drv_op_data;
  1625. ret = mmc_get_ext_csd(card, ext_csd);
  1626. break;
  1627. default:
  1628. pr_notice("%s: unknown driver specific operation:%d\n",
  1629. md->disk->disk_name, mq_rq->drv_op);
  1630. ret = -EINVAL;
  1631. break;
  1632. }
  1633. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  1634. if (cmdq_en && main_md->part_curr <= PART_CMDQ_EN) {
  1635. ret = mmc_cmdq_enable(card);
  1636. if (ret) {
  1637. pr_notice("MMC ioctl:re-enable CQ fail %s\n", __func__);
  1638. return;
  1639. }
  1640. }
  1641. #endif
  1642. mq_rq->drv_op_result = ret;
  1643. blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
  1644. }
  1645. static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  1646. {
  1647. struct mmc_blk_data *md = mq->blkdata;
  1648. struct mmc_card *card = md->queue.card;
  1649. unsigned int from, nr, arg;
  1650. int err = 0, type = MMC_BLK_DISCARD;
  1651. blk_status_t status = BLK_STS_OK;
  1652. if (!mmc_can_erase(card)) {
  1653. status = BLK_STS_NOTSUPP;
  1654. goto fail;
  1655. }
  1656. from = blk_rq_pos(req);
  1657. nr = blk_rq_sectors(req);
  1658. if (mmc_can_discard(card))
  1659. arg = MMC_DISCARD_ARG;
  1660. else if (mmc_can_trim(card))
  1661. arg = MMC_TRIM_ARG;
  1662. else
  1663. arg = MMC_ERASE_ARG;
  1664. do {
  1665. err = 0;
  1666. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1667. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1668. INAND_CMD38_ARG_EXT_CSD,
  1669. arg == MMC_TRIM_ARG ?
  1670. INAND_CMD38_ARG_TRIM :
  1671. INAND_CMD38_ARG_ERASE,
  1672. 0);
  1673. }
  1674. if (!err)
  1675. err = mmc_erase(card, from, nr, arg);
  1676. } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
  1677. if (err)
  1678. status = BLK_STS_IOERR;
  1679. else
  1680. mmc_blk_reset_success(md, type);
  1681. fail:
  1682. blk_end_request(req, status, blk_rq_bytes(req));
  1683. }
  1684. #ifdef CONFIG_MTK_EMMC_HW_CQ
  1685. static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
  1686. struct request *req)
  1687. {
  1688. struct mmc_blk_data *md = mq->blkdata;
  1689. struct mmc_card *card = md->queue.card;
  1690. struct mmc_cmdq_req *cmdq_req = NULL;
  1691. unsigned int from, nr, arg;
  1692. int err = 0;
  1693. if (!(mmc_can_secure_erase_trim(card))) {
  1694. err = BLK_STS_NOTSUPP;
  1695. blk_end_request(req, err, blk_rq_bytes(req));
  1696. goto out;
  1697. }
  1698. from = blk_rq_pos(req);
  1699. nr = blk_rq_sectors(req);
  1700. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  1701. arg = MMC_SECURE_TRIM1_ARG;
  1702. else
  1703. arg = MMC_SECURE_ERASE_ARG;
  1704. cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
  1705. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1706. __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
  1707. EXT_CSD_CMD_SET_NORMAL,
  1708. INAND_CMD38_ARG_EXT_CSD,
  1709. arg == MMC_SECURE_TRIM1_ARG ?
  1710. INAND_CMD38_ARG_SECTRIM1 :
  1711. INAND_CMD38_ARG_SECERASE,
  1712. 0, true, false);
  1713. err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
  1714. if (err)
  1715. goto clear_dcmd;
  1716. }
  1717. err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
  1718. if (err)
  1719. goto clear_dcmd;
  1720. if (arg == MMC_SECURE_TRIM1_ARG) {
  1721. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1722. __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
  1723. EXT_CSD_CMD_SET_NORMAL,
  1724. INAND_CMD38_ARG_EXT_CSD,
  1725. INAND_CMD38_ARG_SECTRIM2,
  1726. 0, true, false);
  1727. err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
  1728. if (err)
  1729. goto clear_dcmd;
  1730. }
  1731. err = mmc_cmdq_erase(cmdq_req, card, from, nr,
  1732. MMC_SECURE_TRIM2_ARG);
  1733. }
  1734. clear_dcmd:
  1735. blk_complete_request(req);
  1736. out:
  1737. return err ? 1 : 0;
  1738. }
  1739. #endif
  1740. static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  1741. struct request *req)
  1742. {
  1743. struct mmc_blk_data *md = mq->blkdata;
  1744. struct mmc_card *card = md->queue.card;
  1745. unsigned int from, nr, arg;
  1746. int err = 0, type = MMC_BLK_SECDISCARD;
  1747. blk_status_t status = BLK_STS_OK;
  1748. if (!(mmc_can_secure_erase_trim(card))) {
  1749. status = BLK_STS_NOTSUPP;
  1750. goto out;
  1751. }
  1752. from = blk_rq_pos(req);
  1753. nr = blk_rq_sectors(req);
  1754. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  1755. arg = MMC_SECURE_TRIM1_ARG;
  1756. else
  1757. arg = MMC_SECURE_ERASE_ARG;
  1758. retry:
  1759. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1760. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1761. INAND_CMD38_ARG_EXT_CSD,
  1762. arg == MMC_SECURE_TRIM1_ARG ?
  1763. INAND_CMD38_ARG_SECTRIM1 :
  1764. INAND_CMD38_ARG_SECERASE,
  1765. 0);
  1766. if (err)
  1767. goto out_retry;
  1768. }
  1769. err = mmc_erase(card, from, nr, arg);
  1770. if (err == -EIO)
  1771. goto out_retry;
  1772. if (err) {
  1773. status = BLK_STS_IOERR;
  1774. goto out;
  1775. }
  1776. if (arg == MMC_SECURE_TRIM1_ARG) {
  1777. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1778. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1779. INAND_CMD38_ARG_EXT_CSD,
  1780. INAND_CMD38_ARG_SECTRIM2,
  1781. 0);
  1782. if (err)
  1783. goto out_retry;
  1784. }
  1785. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  1786. if (err == -EIO)
  1787. goto out_retry;
  1788. if (err) {
  1789. status = BLK_STS_IOERR;
  1790. goto out;
  1791. }
  1792. }
  1793. out_retry:
  1794. if (err && !mmc_blk_reset(md, card->host, type))
  1795. goto retry;
  1796. if (!err)
  1797. mmc_blk_reset_success(md, type);
  1798. out:
  1799. blk_end_request(req, status, blk_rq_bytes(req));
  1800. }
  1801. static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  1802. {
  1803. struct mmc_blk_data *md = mq->blkdata;
  1804. struct mmc_card *card = md->queue.card;
  1805. int ret = 0;
  1806. ret = mmc_flush_cache(card);
  1807. blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
  1808. }
  1809. /*
  1810. * Reformat current write as a reliable write, supporting
  1811. * both legacy and the enhanced reliable write MMC cards.
  1812. * In each transfer we'll handle only as much as a single
  1813. * reliable write can handle, thus finish the request in
  1814. * partial completions.
  1815. */
  1816. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  1817. struct mmc_card *card,
  1818. struct request *req)
  1819. {
  1820. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  1821. /* Legacy mode imposes restrictions on transfers. */
  1822. if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
  1823. brq->data.blocks = 1;
  1824. if (brq->data.blocks > card->ext_csd.rel_sectors)
  1825. brq->data.blocks = card->ext_csd.rel_sectors;
  1826. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  1827. brq->data.blocks = 1;
  1828. }
  1829. }
  1830. #define CMD_ERRORS \
  1831. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  1832. R1_ADDRESS_ERROR | /* Misaligned address */ \
  1833. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  1834. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  1835. R1_CARD_ECC_FAILED | /* Card ECC failed */ \
  1836. R1_CC_ERROR | /* Card controller error */ \
  1837. R1_ERROR) /* General/unknown error */
  1838. static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
  1839. {
  1840. u32 val;
  1841. /*
  1842. * Per the SD specification(physical layer version 4.10)[1],
  1843. * section 4.3.3, it explicitly states that "When the last
  1844. * block of user area is read using CMD18, the host should
  1845. * ignore OUT_OF_RANGE error that may occur even the sequence
  1846. * is correct". And JESD84-B51 for eMMC also has a similar
  1847. * statement on section 6.8.3.
  1848. *
  1849. * Multiple block read/write could be done by either predefined
  1850. * method, namely CMD23, or open-ending mode. For open-ending mode,
  1851. * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
  1852. *
  1853. * However the spec[1] doesn't tell us whether we should also
  1854. * ignore that for predefined method. But per the spec[1], section
  1855. * 4.15 Set Block Count Command, it says"If illegal block count
  1856. * is set, out of range error will be indicated during read/write
  1857. * operation (For example, data transfer is stopped at user area
  1858. * boundary)." In another word, we could expect a out of range error
  1859. * in the response for the following CMD18/25. And if argument of
  1860. * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
  1861. * we could also expect to get a -ETIMEDOUT or any error number from
  1862. * the host drivers due to missing data response(for write)/data(for
  1863. * read), as the cards will stop the data transfer by itself per the
  1864. * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
  1865. */
  1866. if (!brq->stop.error) {
  1867. bool oor_with_open_end;
  1868. /* If there is no error yet, check R1 response */
  1869. val = brq->stop.resp[0] & CMD_ERRORS;
  1870. oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
  1871. if (val && !oor_with_open_end)
  1872. brq->stop.error = -EIO;
  1873. }
  1874. }
  1875. static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
  1876. struct mmc_async_req *areq)
  1877. {
  1878. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  1879. areq);
  1880. struct mmc_blk_request *brq = &mq_mrq->brq;
  1881. struct request *req = mmc_queue_req_to_req(mq_mrq);
  1882. int need_retune = card->host->need_retune;
  1883. bool ecc_err = false;
  1884. bool gen_err = false;
  1885. bool cmdq_en = false;
  1886. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  1887. cmdq_en = mmc_card_cmdq(card);
  1888. #endif
  1889. /*
  1890. * sbc.error indicates a problem with the set block count
  1891. * command. No data will have been transferred.
  1892. *
  1893. * cmd.error indicates a problem with the r/w command. No
  1894. * data will have been transferred.
  1895. *
  1896. * stop.error indicates a problem with the stop command. Data
  1897. * may have been transferred, or may still be transferring.
  1898. */
  1899. if (!cmdq_en) {
  1900. mmc_blk_eval_resp_error(brq);
  1901. if (brq->sbc.error || brq->cmd.error ||
  1902. brq->stop.error || brq->data.error) {
  1903. switch (mmc_blk_cmd_recovery(card,
  1904. req, brq, &ecc_err, &gen_err)) {
  1905. case ERR_RETRY:
  1906. return MMC_BLK_RETRY;
  1907. case ERR_ABORT:
  1908. return MMC_BLK_ABORT;
  1909. case ERR_NOMEDIUM:
  1910. return MMC_BLK_NOMEDIUM;
  1911. case ERR_CONTINUE:
  1912. break;
  1913. }
  1914. }
  1915. }
  1916. /*
  1917. * Check for errors relating to the execution of the
  1918. * initial command - such as address errors. No data
  1919. * has been transferred.
  1920. */
  1921. if (brq->cmd.resp[0] & CMD_ERRORS) {
  1922. pr_err("%s: r/w command failed, status = %#x\n",
  1923. req->rq_disk->disk_name, brq->cmd.resp[0]);
  1924. return MMC_BLK_ABORT;
  1925. }
  1926. /*
  1927. * Everything else is either success, or a data error of some
  1928. * kind. If it was a write, we may have transitioned to
  1929. * program mode, which we have to wait for it to complete.
  1930. */
  1931. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ
  1932. && !cmdq_en) {
  1933. int err;
  1934. /* Check stop command response */
  1935. if (brq->stop.resp[0] & R1_ERROR) {
  1936. pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
  1937. req->rq_disk->disk_name, __func__,
  1938. brq->stop.resp[0]);
  1939. gen_err = true;
  1940. }
  1941. err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
  1942. &gen_err);
  1943. if (err)
  1944. return MMC_BLK_CMD_ERR;
  1945. }
  1946. /* if general error occurs, retry the write operation. */
  1947. if (gen_err) {
  1948. pr_warn("%s: retrying write for general error\n",
  1949. req->rq_disk->disk_name);
  1950. return MMC_BLK_RETRY;
  1951. }
  1952. /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
  1953. if (brq->data.error || brq->stop.error) {
  1954. if (need_retune && !brq->retune_retry_done) {
  1955. pr_debug("%s: retrying because a re-tune was needed\n",
  1956. req->rq_disk->disk_name);
  1957. brq->retune_retry_done = 1;
  1958. return MMC_BLK_RETRY;
  1959. }
  1960. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  1961. req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
  1962. (unsigned)blk_rq_pos(req),
  1963. (unsigned)blk_rq_sectors(req),
  1964. brq->cmd.resp[0], brq->stop.resp[0]);
  1965. if (rq_data_dir(req) == READ) {
  1966. if (ecc_err)
  1967. return MMC_BLK_ECC_ERR;
  1968. return MMC_BLK_DATA_ERR;
  1969. } else {
  1970. return MMC_BLK_CMD_ERR;
  1971. }
  1972. }
  1973. if (!brq->data.bytes_xfered)
  1974. return MMC_BLK_RETRY;
  1975. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1976. return MMC_BLK_PARTIAL;
  1977. return MMC_BLK_SUCCESS;
  1978. }
  1979. static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
  1980. int disable_multi, bool *do_rel_wr,
  1981. bool *do_data_tag)
  1982. {
  1983. struct mmc_blk_data *md = mq->blkdata;
  1984. struct mmc_card *card = md->queue.card;
  1985. struct mmc_blk_request *brq = &mqrq->brq;
  1986. struct request *req = mmc_queue_req_to_req(mqrq);
  1987. /*
  1988. * Reliable writes are used to implement Forced Unit Access and
  1989. * are supported only on MMCs.
  1990. */
  1991. *do_rel_wr = (req->cmd_flags & REQ_FUA) &&
  1992. rq_data_dir(req) == WRITE &&
  1993. (md->flags & MMC_BLK_REL_WR);
  1994. memset(brq, 0, sizeof(struct mmc_blk_request));
  1995. /* mmc_crypto_prepare_req(mqrq); */
  1996. brq->mrq.data = &brq->data;
  1997. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1998. brq->stop.arg = 0;
  1999. if (rq_data_dir(req) == READ) {
  2000. brq->data.flags = MMC_DATA_READ;
  2001. brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  2002. } else {
  2003. brq->data.flags = MMC_DATA_WRITE;
  2004. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  2005. }
  2006. brq->data.blksz = 512;
  2007. brq->data.blocks = blk_rq_sectors(req);
  2008. /*
  2009. * The block layer doesn't support all sector count
  2010. * restrictions, so we need to be prepared for too big
  2011. * requests.
  2012. */
  2013. if (brq->data.blocks > card->host->max_blk_count)
  2014. brq->data.blocks = card->host->max_blk_count;
  2015. if (brq->data.blocks > 1) {
  2016. /*
  2017. * Some SD cards in SPI mode return a CRC error or even lock up
  2018. * completely when trying to read the last block using a
  2019. * multiblock read command.
  2020. */
  2021. if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
  2022. (blk_rq_pos(req) + blk_rq_sectors(req) ==
  2023. get_capacity(md->disk)))
  2024. brq->data.blocks--;
  2025. /*
  2026. * After a read error, we redo the request one sector
  2027. * at a time in order to accurately determine which
  2028. * sectors can be read successfully.
  2029. */
  2030. if (disable_multi)
  2031. brq->data.blocks = 1;
  2032. /*
  2033. * Some controllers have HW issues while operating
  2034. * in multiple I/O mode
  2035. */
  2036. if (card->host->ops->multi_io_quirk)
  2037. brq->data.blocks = card->host->ops->multi_io_quirk(card,
  2038. (rq_data_dir(req) == READ) ?
  2039. MMC_DATA_READ : MMC_DATA_WRITE,
  2040. brq->data.blocks);
  2041. }
  2042. if (*do_rel_wr)
  2043. mmc_apply_rel_rw(brq, card, req);
  2044. /*
  2045. * Data tag is used only during writing meta data to speed
  2046. * up write and any subsequent read of this meta data
  2047. */
  2048. *do_data_tag = card->ext_csd.data_tag_unit_size &&
  2049. (req->cmd_flags & REQ_META) &&
  2050. (rq_data_dir(req) == WRITE) &&
  2051. ((brq->data.blocks * brq->data.blksz) >=
  2052. card->ext_csd.data_tag_unit_size);
  2053. mmc_set_data_timeout(&brq->data, card);
  2054. brq->data.sg = mqrq->sg;
  2055. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  2056. /*
  2057. * Adjust the sg list so it is the same size as the
  2058. * request.
  2059. */
  2060. if (brq->data.blocks != blk_rq_sectors(req)) {
  2061. int i, data_size = brq->data.blocks << 9;
  2062. struct scatterlist *sg;
  2063. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  2064. data_size -= sg->length;
  2065. if (data_size <= 0) {
  2066. sg->length += data_size;
  2067. i++;
  2068. break;
  2069. }
  2070. }
  2071. brq->data.sg_len = i;
  2072. }
  2073. mqrq->areq.mrq = &brq->mrq;
  2074. }
  2075. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  2076. struct mmc_card *card,
  2077. int disable_multi,
  2078. struct mmc_queue *mq)
  2079. {
  2080. u32 readcmd, writecmd;
  2081. struct mmc_blk_request *brq = &mqrq->brq;
  2082. struct request *req = mmc_queue_req_to_req(mqrq);
  2083. struct mmc_blk_data *md = mq->blkdata;
  2084. bool do_rel_wr, do_data_tag;
  2085. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2086. bool cmdq_en = mmc_card_cmdq(card);
  2087. #endif
  2088. mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
  2089. brq->mrq.cmd = &brq->cmd;
  2090. brq->cmd.arg = blk_rq_pos(req);
  2091. if (!mmc_card_blockaddr(card))
  2092. brq->cmd.arg <<= 9;
  2093. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  2094. if (brq->data.blocks > 1 || do_rel_wr) {
  2095. /* SPI multiblock writes terminate using a special
  2096. * token, not a STOP_TRANSMISSION request.
  2097. */
  2098. if (!mmc_host_is_spi(card->host) ||
  2099. rq_data_dir(req) == READ)
  2100. brq->mrq.stop = &brq->stop;
  2101. readcmd = MMC_READ_MULTIPLE_BLOCK;
  2102. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  2103. } else {
  2104. brq->mrq.stop = NULL;
  2105. readcmd = MMC_READ_SINGLE_BLOCK;
  2106. writecmd = MMC_WRITE_BLOCK;
  2107. }
  2108. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2109. if (cmdq_en) {
  2110. readcmd = MMC_EXECUTE_READ_TASK;
  2111. writecmd = MMC_EXECUTE_WRITE_TASK;
  2112. }
  2113. #endif
  2114. brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
  2115. /*
  2116. * Pre-defined multi-block transfers are preferable to
  2117. * open ended-ones (and necessary for reliable writes).
  2118. * However, it is not sufficient to just send CMD23,
  2119. * and avoid the final CMD12, as on an error condition
  2120. * CMD12 (stop) needs to be sent anyway. This, coupled
  2121. * with Auto-CMD23 enhancements provided by some
  2122. * hosts, means that the complexity of dealing
  2123. * with this is best left to the host. If CMD23 is
  2124. * supported by card and host, we'll fill sbc in and let
  2125. * the host deal with handling it correctly. This means
  2126. * that for hosts that don't expose MMC_CAP_CMD23, no
  2127. * change of behavior will be observed.
  2128. *
  2129. * N.B: Some MMC cards experience perf degradation.
  2130. * We'll avoid using CMD23-bounded multiblock writes for
  2131. * these, while retaining features like reliable writes.
  2132. */
  2133. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  2134. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  2135. do_data_tag)) {
  2136. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  2137. brq->sbc.arg = brq->data.blocks |
  2138. (do_rel_wr ? (1 << 31) : 0) |
  2139. (do_data_tag ? (1 << 29) : 0);
  2140. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  2141. brq->mrq.sbc = &brq->sbc;
  2142. }
  2143. mqrq->areq.err_check = mmc_blk_err_check;
  2144. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2145. if (cmdq_en) {
  2146. int rt = IS_RT_CLASS_REQ(req);
  2147. brq->mrq.flags = rt;
  2148. brq->mrq_que.flags = rt;
  2149. brq->sbc.opcode = MMC_QUE_TASK_PARAMS;
  2150. brq->sbc.arg = brq->data.blocks |
  2151. (do_rel_wr ? (1 << 31) : 0) |
  2152. ((rq_data_dir(req) == WRITE) ? 0 : (1 << 30)) |
  2153. (do_data_tag ? (1 << 29) : 0) |
  2154. (rt << 23) | ((atomic_read(&mqrq->index) - 1) << 16);
  2155. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  2156. brq->mrq_que.sbc = &brq->sbc;
  2157. brq->que.opcode = MMC_QUE_TASK_ADDR;
  2158. brq->que.arg = blk_rq_pos(req);
  2159. if (!mmc_card_blockaddr(card))
  2160. brq->que.arg <<= 9;
  2161. brq->que.flags = MMC_RSP_R1 | MMC_CMD_AC;
  2162. brq->mrq_que.cmd = &brq->que;
  2163. brq->cmd.arg = (atomic_read(&mqrq->index) - 1) << 16;
  2164. mqrq->areq.mrq_que = &brq->mrq_que;
  2165. brq->mrq.areq = &mqrq->areq;
  2166. brq->mrq_que.areq = &mqrq->areq;
  2167. }
  2168. #endif
  2169. if (req->bio)
  2170. brq->mrq.req = req;
  2171. #if defined(CONFIG_MTK_HW_FDE)
  2172. /* request is from mmc layer */
  2173. brq->mrq.is_mmc_req = true;
  2174. #endif
  2175. }
  2176. static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  2177. struct mmc_blk_request *brq, struct request *req,
  2178. bool old_req_pending)
  2179. {
  2180. bool req_pending;
  2181. /*
  2182. * If this is an SD card and we're writing, we can first
  2183. * mark the known good sectors as ok.
  2184. *
  2185. * If the card is not SD, we can still ok written sectors
  2186. * as reported by the controller (which might be less than
  2187. * the real number of written sectors, but never more).
  2188. */
  2189. if (mmc_card_sd(card)) {
  2190. u32 blocks;
  2191. int err;
  2192. err = mmc_sd_num_wr_blocks(card, &blocks);
  2193. if (err)
  2194. req_pending = old_req_pending;
  2195. else
  2196. req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
  2197. } else {
  2198. req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
  2199. }
  2200. return req_pending;
  2201. }
  2202. static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
  2203. struct request *req,
  2204. struct mmc_queue_req *mqrq)
  2205. {
  2206. if (mmc_card_removed(card))
  2207. req->rq_flags |= RQF_QUIET;
  2208. while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
  2209. atomic_dec(&mq->qcnt);
  2210. }
  2211. /**
  2212. * mmc_blk_rw_try_restart() - tries to restart the current async request
  2213. * @mq: the queue with the card and host to restart
  2214. * @req: a new request that want to be started after the current one
  2215. */
  2216. static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
  2217. struct mmc_queue_req *mqrq)
  2218. {
  2219. if (!req)
  2220. return;
  2221. /*
  2222. * If the card was removed, just cancel everything and return.
  2223. */
  2224. if (mmc_card_removed(mq->card)) {
  2225. req->rq_flags |= RQF_QUIET;
  2226. blk_end_request_all(req, BLK_STS_IOERR);
  2227. atomic_dec(&mq->qcnt); /* FIXME: just set to 0? */
  2228. return;
  2229. }
  2230. /* Else proceed and try to restart the current async request */
  2231. mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
  2232. mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
  2233. }
  2234. #ifdef CONFIG_MTK_EMMC_HW_CQ
  2235. static int mmc_blk_cmdq_start_req(struct mmc_host *host,
  2236. struct mmc_cmdq_req *cmdq_req)
  2237. {
  2238. struct mmc_request *mrq = &cmdq_req->mrq;
  2239. mrq->done = mmc_blk_cmdq_req_done;
  2240. return mmc_cmdq_start_req(host, cmdq_req);
  2241. }
  2242. /* prepare for non-data commands */
  2243. static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
  2244. struct mmc_queue_req *mqrq, struct mmc_queue *mq)
  2245. {
  2246. struct request *req = mqrq->req;
  2247. struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
  2248. memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
  2249. cmdq_req->mrq.data = NULL;
  2250. cmdq_req->cmd_flags = req->cmd_flags;
  2251. cmdq_req->mrq.req = mqrq->req;
  2252. req->special = mqrq;
  2253. cmdq_req->cmdq_req_flags |= DCMD;
  2254. cmdq_req->mrq.cmdq_req = cmdq_req;
  2255. return &mqrq->cmdq_req;
  2256. }
  2257. static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
  2258. struct mmc_queue_req *mqrq, struct mmc_queue *mq)
  2259. {
  2260. struct mmc_card *card = mq->card;
  2261. struct request *req = mqrq->req;
  2262. struct mmc_blk_data *md = mq->blkdata;
  2263. bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
  2264. bool do_data_tag;
  2265. bool read_dir = (rq_data_dir(req) == READ);
  2266. bool prio = IS_RT_CLASS_REQ(req);
  2267. struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
  2268. memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
  2269. cmdq_rq->tag = req->tag;
  2270. if (read_dir) {
  2271. cmdq_rq->cmdq_req_flags |= DIR;
  2272. cmdq_rq->data.flags = MMC_DATA_READ;
  2273. } else {
  2274. cmdq_rq->data.flags = MMC_DATA_WRITE;
  2275. }
  2276. if (prio)
  2277. cmdq_rq->cmdq_req_flags |= PRIO;
  2278. if (do_rel_wr)
  2279. cmdq_rq->cmdq_req_flags |= REL_WR;
  2280. cmdq_rq->data.blocks = blk_rq_sectors(req);
  2281. cmdq_rq->blk_addr = blk_rq_pos(req);
  2282. /* MMC sector size */
  2283. cmdq_rq->data.blksz = 512;
  2284. mmc_set_data_timeout(&cmdq_rq->data, card);
  2285. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  2286. (req->cmd_flags & REQ_META) &&
  2287. (rq_data_dir(req) == WRITE) &&
  2288. ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
  2289. card->ext_csd.data_tag_unit_size);
  2290. if (do_data_tag)
  2291. cmdq_rq->cmdq_req_flags |= DAT_TAG;
  2292. cmdq_rq->data.sg = mqrq->sg;
  2293. cmdq_rq->data.sg_len = mmc_cmdq_queue_map_sg(mq, mqrq);
  2294. /*
  2295. * Adjust the sg list so it is the same size as the
  2296. * request.
  2297. */
  2298. if (cmdq_rq->data.blocks > card->host->max_blk_count)
  2299. cmdq_rq->data.blocks = card->host->max_blk_count;
  2300. if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
  2301. int i, data_size = cmdq_rq->data.blocks << 9;
  2302. struct scatterlist *sg;
  2303. for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
  2304. data_size -= sg->length;
  2305. if (data_size <= 0) {
  2306. sg->length += data_size;
  2307. i++;
  2308. break;
  2309. }
  2310. }
  2311. cmdq_rq->data.sg_len = i;
  2312. }
  2313. mqrq->cmdq_req.cmd_flags = req->cmd_flags;
  2314. mqrq->cmdq_req.mrq.req = mqrq->req;
  2315. mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
  2316. mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
  2317. mqrq->req->special = mqrq;
  2318. /*
  2319. * Although keep it should work normally, but only
  2320. * call it in CQHCI for safe, SWcmdq will do this in
  2321. * mmc_blk_swcq_issue_rw_rq().
  2322. */
  2323. #ifndef CONFIG_MTK_EMMC_CQ_SUPPORT
  2324. mmc_crypto_prepare_req(mqrq);
  2325. #endif
  2326. #ifdef MMC_CQHCI_DEBUG
  2327. pr_debug("%s: %s: mrq: 0x%p, req: 0x%p, mqrq: 0x%p, bytes to xf: %d, tag: %d, mmc_cmdq_req: 0x%p, card-addr: 0x%08x, dir(r-1/w-0): %d\n",
  2328. mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
  2329. mqrq->req, mqrq,
  2330. (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
  2331. cmdq_rq->tag,
  2332. cmdq_rq, cmdq_rq->blk_addr,
  2333. (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
  2334. #endif
  2335. return &mqrq->cmdq_req;
  2336. }
  2337. static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  2338. {
  2339. struct mmc_queue_req *active_mqrq;
  2340. struct mmc_card *card = mq->card;
  2341. struct mmc_host *host = card->host;
  2342. struct mmc_cmdq_req *mc_rq;
  2343. u8 active_small_sector_read = 0;
  2344. int ret = 0;
  2345. WARN_ON((req->tag < 0) ||
  2346. (req->tag > card->ext_csd.cmdq_depth)); /*bug*/
  2347. WARN_ON(test_and_set_bit(req->tag,
  2348. &host->cmdq_ctx.data_active_reqs)); /*bug*/
  2349. WARN_ON(test_and_set_bit(req->tag,
  2350. &host->cmdq_ctx.active_reqs)); /*bug*/
  2351. active_mqrq = &mq->mqrq_cmdq[req->tag];
  2352. active_mqrq->req = req;
  2353. mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
  2354. if (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) {
  2355. unsigned int sectors = blk_rq_sectors(req);
  2356. if (((sectors > 0) && (sectors < 8))
  2357. && (rq_data_dir(req) == READ))
  2358. active_small_sector_read = 1;
  2359. }
  2360. mt_biolog_cqhci_queue_task(mc_rq->mrq.req->tag,
  2361. &mc_rq->mrq);
  2362. ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
  2363. if (!ret && active_small_sector_read)
  2364. host->cmdq_ctx.active_small_sector_read_reqs++;
  2365. return ret;
  2366. }
  2367. /*
  2368. * Issues a flush (dcmd) request
  2369. */
  2370. int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
  2371. {
  2372. int err;
  2373. struct mmc_queue_req *active_mqrq;
  2374. struct mmc_card *card = mq->card;
  2375. struct mmc_host *host;
  2376. struct mmc_cmdq_req *cmdq_req;
  2377. struct mmc_cmdq_context_info *ctx_info;
  2378. host = card->host;
  2379. WARN_ON(req->tag > card->ext_csd.cmdq_depth); /*bug*/
  2380. WARN_ON(test_and_set_bit(req->tag,
  2381. &host->cmdq_ctx.active_reqs)); /*bug*/
  2382. ctx_info = &host->cmdq_ctx;
  2383. set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
  2384. active_mqrq = &mq->mqrq_cmdq[req->tag];
  2385. active_mqrq->req = req;
  2386. cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
  2387. cmdq_req->cmdq_req_flags |= QBR;
  2388. cmdq_req->mrq.cmd = &cmdq_req->cmd;
  2389. cmdq_req->tag = req->tag;
  2390. err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
  2391. if (err) {
  2392. pr_notice("%s: failed (%d) preparing flush req\n",
  2393. mmc_hostname(host), err);
  2394. return err;
  2395. }
  2396. err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
  2397. return err;
  2398. }
  2399. EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
  2400. static int mmc_blk_cmdq_recovery(struct mmc_host *host)
  2401. {
  2402. int retry = 0;
  2403. bool gen_err = 0;
  2404. u32 status;
  2405. struct mmc_request *mrq = host->err_mrq;
  2406. struct mmc_card *card = host->card;
  2407. unsigned long timeout;
  2408. int err;
  2409. for (retry = 0; retry < 5; retry++) {
  2410. err = __mmc_send_status(card, &status, 0);
  2411. if (!err)
  2412. break;
  2413. }
  2414. pr_notice("%s: status = 0x%x\n",
  2415. mmc_hostname(host), status);
  2416. if (err) {
  2417. pr_notice("%s: No response from card !!!\n",
  2418. mmc_hostname(host));
  2419. goto out;
  2420. }
  2421. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  2422. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  2423. pr_notice("%s: status = 0x%x need send stop\n",
  2424. mmc_hostname(host), status);
  2425. err = send_stop(card,
  2426. DIV_ROUND_UP(mrq->data->timeout_ns, 1000000),
  2427. mrq->req, &gen_err, &status);
  2428. if (err) {
  2429. pr_notice("%s: error %d sending stop (%d) command\n",
  2430. mrq->req->rq_disk->disk_name,
  2431. err, status);
  2432. goto out;
  2433. }
  2434. }
  2435. timeout = jiffies + msecs_to_jiffies(3 * 1000);
  2436. while (R1_CURRENT_STATE(status) != R1_STATE_TRAN) {
  2437. int err = __mmc_send_status(card, &status, 5);
  2438. if (!err)
  2439. break;
  2440. /* Timeout if the device never becomes ready for data
  2441. * and never leaves the program state.
  2442. */
  2443. if (time_after(jiffies, timeout)) {
  2444. pr_notice("%s: Card wait trans timeout %s\n",
  2445. mmc_hostname(card->host),
  2446. __func__);
  2447. err = -ETIMEDOUT;
  2448. break;
  2449. }
  2450. };
  2451. out:
  2452. return err;
  2453. }
  2454. static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
  2455. {
  2456. int err = 0;
  2457. if (mmc_cmdq_halt(host, true)) {
  2458. pr_notice("%s: halt failed\n", mmc_hostname(host));
  2459. goto reset;
  2460. }
  2461. if (clear_all)
  2462. mmc_cmdq_discard_queue(host, 0);
  2463. reset:
  2464. host->cmdq_ops->disable(host, true);
  2465. err = mmc_cmdq_hw_reset(host);
  2466. if (err && err != -EOPNOTSUPP) {
  2467. pr_notice("%s: failed to cmdq_hw_reset err = %d\n",
  2468. mmc_hostname(host), err);
  2469. host->cmdq_ops->enable(host);
  2470. mmc_cmdq_halt(host, false);
  2471. goto out;
  2472. }
  2473. /*
  2474. * CMDQ HW reset would have already made CQE
  2475. * in unhalted state, but reflect the same
  2476. * in software state of cmdq_ctx.
  2477. */
  2478. mmc_host_clr_halt(host);
  2479. out:
  2480. return;
  2481. }
  2482. /**
  2483. * is_cmdq_dcmd_req - Checks if tag belongs to DCMD request.
  2484. * @q: request_queue pointer.
  2485. * @tag: tag number of request to check.
  2486. *
  2487. * This function checks if the request with tag number "tag"
  2488. * is a DCMD request or not based on cmdq_req_flags set.
  2489. *
  2490. * returns true if DCMD req, otherwise false.
  2491. */
  2492. static bool is_cmdq_dcmd_req(struct request_queue *q, int tag)
  2493. {
  2494. struct request *req;
  2495. struct mmc_queue_req *mq_rq;
  2496. struct mmc_cmdq_req *cmdq_req;
  2497. req = blk_queue_find_tag(q, tag);
  2498. if (WARN_ON(!req))
  2499. goto out;
  2500. mq_rq = req->special;
  2501. if (WARN_ON(!mq_rq))
  2502. goto out;
  2503. cmdq_req = &(mq_rq->cmdq_req);
  2504. return (cmdq_req->cmdq_req_flags & DCMD);
  2505. out:
  2506. return -ENOENT;
  2507. }
  2508. /**
  2509. * mmc_blk_cmdq_reset_all - Reset everything for CMDQ block request.
  2510. * @host: mmc_host pointer.
  2511. * @err: error for which reset is performed.
  2512. *
  2513. * This function implements reset_all functionality for
  2514. * cmdq. It resets the controller, power cycle the card,
  2515. * and invalidate all busy tags(requeue all request back to
  2516. * elevator).
  2517. */
  2518. static void mmc_blk_cmdq_reset_all(struct mmc_host *host, int err)
  2519. {
  2520. struct mmc_request *mrq = host->err_mrq;
  2521. struct mmc_card *card = host->card;
  2522. struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
  2523. struct request_queue *q;
  2524. int itag = 0;
  2525. int ret = 0;
  2526. if (WARN_ON(!mrq))
  2527. return;
  2528. q = mrq->req->q;
  2529. WARN_ON(!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
  2530. pr_notice("%s: %s: active_reqs = 0x%lx\n",
  2531. mmc_hostname(host), __func__,
  2532. ctx_info->active_reqs);
  2533. /* Bring device back to TRAN state */
  2534. ret = mmc_blk_cmdq_recovery(host);
  2535. /* Try to discard entire queue */
  2536. if (!ret)
  2537. ret = mmc_cmdq_discard_queue(host, 0);
  2538. /*
  2539. * If recovery or discard fail, reset device.
  2540. * If discard success, reset host.
  2541. */
  2542. if (ret)
  2543. mmc_blk_cmdq_reset(host, false);
  2544. else
  2545. host->cmdq_ops->reset(host, true);
  2546. for_each_set_bit(itag, &ctx_info->active_reqs,
  2547. host->num_cq_slots) {
  2548. ret = is_cmdq_dcmd_req(q, itag);
  2549. if (WARN_ON(ret == -ENOENT))
  2550. continue;
  2551. if (!ret) {
  2552. WARN_ON(!test_and_clear_bit(itag,
  2553. &ctx_info->data_active_reqs));
  2554. mmc_cmdq_post_req(host, itag, err);
  2555. } else {
  2556. clear_bit(CMDQ_STATE_DCMD_ACTIVE,
  2557. &ctx_info->curr_state);
  2558. }
  2559. WARN_ON(!test_and_clear_bit(itag,
  2560. &ctx_info->active_reqs));
  2561. pr_notice("%s: %s: clear active_reqs itag = %d\n",
  2562. mmc_hostname(host), __func__,
  2563. itag);
  2564. mmc_put_card(card);
  2565. }
  2566. spin_lock_irq(q->queue_lock);
  2567. blk_queue_invalidate_tags(q);
  2568. spin_unlock_irq(q->queue_lock);
  2569. }
  2570. static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
  2571. {
  2572. int err;
  2573. struct mmc_card *card = mq->card;
  2574. struct mmc_host *host = card->host;
  2575. mmc_get_card(card);
  2576. err = mmc_cmdq_halt(host, true);
  2577. if (err) {
  2578. pr_notice("%s: halt: failed: %d\n", __func__, err);
  2579. goto out;
  2580. }
  2581. /* disable CQ mode in card */
  2582. if (mmc_card_cmdq(card)) {
  2583. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  2584. EXT_CSD_CMDQ_MODE_EN, 0,
  2585. card->ext_csd.generic_cmd6_time);
  2586. if (err) {
  2587. pr_notice("%s: failed to switch card to legacy mode: %d\n",
  2588. __func__, err);
  2589. goto out;
  2590. }
  2591. mmc_card_clr_cmdq(card);
  2592. }
  2593. host->cmdq_ops->disable(host, false);
  2594. host->card->cqe_init = false;
  2595. out:
  2596. mmc_put_card(card);
  2597. }
  2598. static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
  2599. {
  2600. struct mmc_queue *mq = req->q->queuedata;
  2601. struct mmc_host *host = mq->card->host;
  2602. struct mmc_queue_req *mq_rq = req->special;
  2603. struct mmc_request *mrq;
  2604. struct mmc_cmdq_req *cmdq_req;
  2605. struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
  2606. if (host->cmdq_ops->dumpstate)
  2607. host->cmdq_ops->dumpstate(host, true);
  2608. /*
  2609. * The mmc_queue_req will be present only if the request
  2610. * is issued to the LLD. The request could be fetched from
  2611. * block layer queue but could be waiting to be issued
  2612. * (for e.g. clock scaling is waiting for an empty cmdq queue)
  2613. * Reset the timer in such cases to give LLD more time
  2614. */
  2615. if (!mq_rq) {
  2616. pr_notice("%s: restart timer for tag: %d\n",
  2617. __func__, req->tag);
  2618. return BLK_EH_RESET_TIMER;
  2619. }
  2620. mrq = &mq_rq->cmdq_req.mrq;
  2621. cmdq_req = &mq_rq->cmdq_req;
  2622. WARN_ON(!mrq || !cmdq_req); /*bug*/
  2623. if (cmdq_req->cmdq_req_flags & DCMD)
  2624. mrq->cmd->error = -ETIMEDOUT;
  2625. else
  2626. mrq->data->error = -ETIMEDOUT;
  2627. if (mrq->cmd && mrq->cmd->error) {
  2628. if (!(mrq->req->cmd_flags & REQ_PREFLUSH)) {
  2629. /*
  2630. * Notify completion for non flush commands like
  2631. * discard that wait for DCMD finish.
  2632. */
  2633. set_bit(CMDQ_STATE_REQ_TIMED_OUT,
  2634. &ctx_info->curr_state);
  2635. complete(&mrq->completion);
  2636. return BLK_EH_NOT_HANDLED;
  2637. }
  2638. }
  2639. if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state) ||
  2640. test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state))
  2641. return BLK_EH_NOT_HANDLED;
  2642. set_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
  2643. return BLK_EH_HANDLED;
  2644. }
  2645. /*
  2646. * mmc_blk_cmdq_err: error handling of cmdq error requests.
  2647. * Function should be called in context of error out request
  2648. * which has claim_host and rpm acquired.
  2649. * This may be called with CQ engine halted. Make sure to
  2650. * unhalt it after error recovery.
  2651. *
  2652. * TODO: Currently cmdq error handler does reset_all in case
  2653. * of any erorr. Need to optimize error handling.
  2654. */
  2655. static void mmc_blk_cmdq_err(struct mmc_queue *mq)
  2656. {
  2657. struct mmc_host *host = mq->card->host;
  2658. struct mmc_request *mrq = host->err_mrq;
  2659. struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
  2660. struct request_queue *q;
  2661. int err, ret;
  2662. u32 status = 0;
  2663. if (WARN_ON(!mrq))
  2664. return;
  2665. pr_notice("%s: %s err req = %p, err tag = %d\n",
  2666. mmc_hostname(host), __func__, mrq->req, mrq->req->tag);
  2667. if (host->cmdq_ops->dumpstate && !mrq->cmdq_req->skip_dump)
  2668. host->cmdq_ops->dumpstate(host, true);
  2669. down_write(&ctx_info->err_rwsem);
  2670. pr_notice("%s: %s Starting cmdq Error handler\n",
  2671. mmc_hostname(host), __func__);
  2672. q = mrq->req->q;
  2673. err = mmc_cmdq_halt(host, true);
  2674. if (err) {
  2675. pr_notice("%s halt failed: %d\n", mmc_hostname(host), err);
  2676. goto reset;
  2677. }
  2678. /* RED error - Fatal: requires reset */
  2679. if (mrq->cmdq_req->resp_err) {
  2680. err = mrq->cmdq_req->resp_err;
  2681. pr_notice("%s: Response error detected: Device in bad state\n",
  2682. mmc_hostname(host));
  2683. goto reset;
  2684. }
  2685. /*
  2686. * TIMEOUT errrors can happen because of execution error
  2687. * in the last command. So send cmd 13 to get device status
  2688. */
  2689. if ((mrq->cmd && (mrq->cmd->error == -ETIMEDOUT)) ||
  2690. (mrq->data && (mrq->data->error == -ETIMEDOUT))) {
  2691. if (mmc_host_halt(host) || mmc_host_cq_disable(host)) {
  2692. ret = __mmc_send_status(host->card, &status, 0);
  2693. if (ret)
  2694. pr_notice("%s: CMD13 failed with err %d\n",
  2695. mmc_hostname(host), ret);
  2696. }
  2697. pr_notice("%s: Timeout error detected with device status 0x%08x\n",
  2698. mmc_hostname(host), status);
  2699. }
  2700. /*
  2701. * In case of software request time-out, we schedule err work only for
  2702. * the first error out request and handles all other request in flight
  2703. * here.
  2704. */
  2705. if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state)) {
  2706. err = -ETIMEDOUT;
  2707. pr_notice("%s: %s err req = %p, err = %d, err tag = %d, CMDQ_STATE_REQ_TIMED_OUT\n",
  2708. mmc_hostname(host),
  2709. __func__, mrq->req, err, mrq->req->tag);
  2710. } else if (mrq->data && mrq->data->error) {
  2711. err = mrq->data->error;
  2712. pr_notice("%s: %s err req = %p, err = %d, err tag = %d, data error\n",
  2713. mmc_hostname(host),
  2714. __func__, mrq->req, err, mrq->req->tag);
  2715. } else if (mrq->cmd && mrq->cmd->error) {
  2716. /* DCMD commands */
  2717. err = mrq->cmd->error;
  2718. pr_notice("%s: %s err req = %p, err = %d, err tag = %d, command error\n",
  2719. mmc_hostname(host),
  2720. __func__, mrq->req, err, mrq->req->tag);
  2721. }
  2722. reset:
  2723. mmc_blk_cmdq_reset_all(host, err);
  2724. if (mrq->cmdq_req->resp_err)
  2725. mrq->cmdq_req->resp_err = false;
  2726. if (mrq->cmdq_req->skip_dump)
  2727. mrq->cmdq_req->skip_dump = false;
  2728. if (mrq->cmdq_req->skip_reset)
  2729. mrq->cmdq_req->skip_reset = false;
  2730. mmc_cmdq_halt(host, false);
  2731. host->err_mrq = NULL;
  2732. clear_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
  2733. WARN_ON(!test_and_clear_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
  2734. up_write(&ctx_info->err_rwsem);
  2735. wake_up(&ctx_info->wait);
  2736. }
  2737. /* invoked by block layer in softirq context */
  2738. void mmc_blk_cmdq_complete_rq(struct request *rq)
  2739. {
  2740. struct mmc_queue_req *mq_rq = rq->special;
  2741. struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
  2742. struct mmc_host *host = mrq->host;
  2743. struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
  2744. struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
  2745. struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
  2746. int err = 0;
  2747. int err_resp = 0;
  2748. bool is_dcmd = false;
  2749. bool err_rwsem = false;
  2750. #ifdef CONFIG_MTK_MMC_DEBUG
  2751. int cpu = -1;
  2752. #endif
  2753. if (down_read_trylock(&ctx_info->err_rwsem)) {
  2754. err_rwsem = true;
  2755. } else {
  2756. pr_notice("%s: err_rwsem lock failed to acquire => err handler active\n",
  2757. __func__);
  2758. WARN_ON_ONCE(!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
  2759. goto out;
  2760. }
  2761. if (mrq->cmd && mrq->cmd->error)
  2762. err = mrq->cmd->error;
  2763. else if (mrq->data && mrq->data->error)
  2764. err = mrq->data->error;
  2765. if (cmdq_req->resp_err)
  2766. err_resp = cmdq_req->resp_err;
  2767. #ifdef MMC_CQHCI_DEBUG
  2768. pr_debug("%s: %s completed req = 0x%p, err = %d, tag = %d\n",
  2769. mmc_hostname(host), __func__, mrq->req, err, cmdq_req->tag);
  2770. #endif
  2771. #ifdef CONFIG_MTK_MMC_DEBUG
  2772. /* softirq dump */
  2773. cpu = smp_processor_id();
  2774. dbg_add_sirq_log(host, MAGIC_CQHCI_DBG_TYPE_SIRQ,
  2775. err,
  2776. cmdq_req->tag,
  2777. cpu,
  2778. ctx_info->data_active_reqs);
  2779. #endif
  2780. if ((err || err_resp) && !cmdq_req->skip_err_handling) {
  2781. pr_notice("%s: %s: txfr error(%d)/resp_err(%d)\n",
  2782. mmc_hostname(mrq->host), __func__, err,
  2783. err_resp);
  2784. if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
  2785. pr_notice("%s: CQ in error state, ending current req: %d\n",
  2786. __func__, err);
  2787. } else {
  2788. set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
  2789. WARN_ON(host->err_mrq != NULL); /*bug*/
  2790. host->err_mrq = mrq;
  2791. pr_notice("%s: %s err req = 0x%p, err = %d, err tag = %d\n",
  2792. mmc_hostname(host),
  2793. __func__, mrq->req, err, rq->tag);
  2794. schedule_work(&mq->cmdq_err_work);
  2795. }
  2796. goto out;
  2797. }
  2798. /* clear pending request */
  2799. if (!test_and_clear_bit(cmdq_req->tag,
  2800. &ctx_info->active_reqs)) {
  2801. pr_notice("%s: %s req = 0x%p, tag = %d active_reqs already cleared!\n",
  2802. mmc_hostname(host), __func__, mrq->req, cmdq_req->tag);
  2803. WARN_ON(1); /*bug*/
  2804. }
  2805. if (cmdq_req->cmdq_req_flags & DCMD)
  2806. is_dcmd = true;
  2807. else
  2808. WARN_ON(!test_and_clear_bit(cmdq_req->tag,
  2809. &ctx_info->data_active_reqs)); /*bug*/
  2810. if (!is_dcmd)
  2811. mmc_cmdq_post_req(host, cmdq_req->tag, err);
  2812. if (cmdq_req->cmdq_req_flags & DCMD) {
  2813. clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
  2814. blk_end_request_all(rq, err);
  2815. goto out;
  2816. }
  2817. /*
  2818. * In case of error, cmdq_req->data.bytes_xfered is set to 0.
  2819. * If we call blk_end_request() with nr_bytes as 0 then the request
  2820. * never gets completed. So in case of error, to complete a request
  2821. * with error we should use blk_end_request_all().
  2822. */
  2823. if (err && cmdq_req->skip_err_handling) {
  2824. cmdq_req->skip_err_handling = false;
  2825. blk_end_request_all(rq, err);
  2826. goto out;
  2827. }
  2828. mt_biolog_cqhci_complete(cmdq_req->tag);
  2829. blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
  2830. out:
  2831. /*
  2832. * Instead of checking host CMDQ_STATE_ERR state,
  2833. * use local variable here to prevent some race condition.
  2834. * ex. The previous request complete with no error but
  2835. * CMDQ_STATE_ERR had just been set in an instant.
  2836. */
  2837. if (err_rwsem && !(err || err_resp)) {
  2838. wake_up(&ctx_info->wait);
  2839. mmc_put_card(host->card);
  2840. }
  2841. if (!ctx_info->active_reqs)
  2842. wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
  2843. if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
  2844. complete(&mq->cmdq_shutdown_complete);
  2845. if (err_rwsem)
  2846. up_read(&ctx_info->err_rwsem);
  2847. }
  2848. /*
  2849. * Complete reqs from block layer softirq context
  2850. * Invoked in irq context
  2851. */
  2852. void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
  2853. {
  2854. struct request *req = mrq->req;
  2855. blk_complete_request(req);
  2856. }
  2857. EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
  2858. #endif
  2859. static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
  2860. {
  2861. struct mmc_blk_data *md = mq->blkdata;
  2862. struct mmc_card *card = md->queue.card;
  2863. struct mmc_blk_request *brq;
  2864. int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
  2865. enum mmc_blk_status status;
  2866. struct mmc_queue_req *mqrq_cur = NULL;
  2867. struct mmc_queue_req *mq_rq;
  2868. struct request *old_req;
  2869. struct mmc_async_req *new_areq;
  2870. struct mmc_async_req *old_areq;
  2871. bool req_pending = true;
  2872. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2873. if (mmc_card_cmdq(card) && !new_req) {
  2874. mmc_wait_cmdq_empty(card->host);
  2875. return;
  2876. }
  2877. #endif
  2878. if (new_req) {
  2879. mqrq_cur = req_to_mmc_queue_req(new_req);
  2880. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2881. if (mmc_card_cmdq(card))
  2882. mqrq_cur->sg =
  2883. mq->mqrq[atomic_read(&mqrq_cur->index) - 1].sg;
  2884. #endif
  2885. atomic_inc(&mq->qcnt);
  2886. }
  2887. if (!atomic_read(&mq->qcnt))
  2888. return;
  2889. mt_biolog_mmcqd_req_check();
  2890. do {
  2891. if (new_req) {
  2892. /*
  2893. * When 4KB native sector is enabled, only 8 blocks
  2894. * multiple read or write is allowed
  2895. */
  2896. if (mmc_large_sector(card) &&
  2897. !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
  2898. pr_notice("%s: Transfer size is not 4KB sector size aligned\n",
  2899. new_req->rq_disk->disk_name);
  2900. mmc_blk_rw_cmd_abort(mq, card,
  2901. new_req, mqrq_cur);
  2902. return;
  2903. }
  2904. mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
  2905. new_areq = &mqrq_cur->areq;
  2906. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2907. if (mmc_card_cmdq(card)) {
  2908. card->host->areq_que[
  2909. atomic_read(&mqrq_cur->index) - 1] =
  2910. new_areq;
  2911. }
  2912. #endif
  2913. } else
  2914. new_areq = NULL;
  2915. old_areq = mmc_start_areq(card->host, new_areq, &status);
  2916. if (!old_areq) {
  2917. /*
  2918. * We have just put the first request into the pipeline
  2919. * and there is nothing more to do until it is
  2920. * complete.
  2921. */
  2922. return;
  2923. }
  2924. /*
  2925. * An asynchronous request has been completed and we proceed
  2926. * to handle the result of it.
  2927. */
  2928. mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
  2929. brq = &mq_rq->brq;
  2930. old_req = mmc_queue_req_to_req(mq_rq);
  2931. type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  2932. switch (status) {
  2933. case MMC_BLK_SUCCESS:
  2934. case MMC_BLK_PARTIAL:
  2935. /*
  2936. * A block was successfully transferred.
  2937. */
  2938. mmc_blk_reset_success(md, type);
  2939. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  2940. if (status == MMC_BLK_SUCCESS) {
  2941. req_pending = 0;
  2942. spin_lock_irq(&md->lock);
  2943. blk_complete_request(old_req);
  2944. spin_unlock_irq(&md->lock);
  2945. } else
  2946. #endif
  2947. {
  2948. req_pending = blk_end_request(old_req,
  2949. BLK_STS_OK,
  2950. brq->data.bytes_xfered);
  2951. }
  2952. /*
  2953. * If the blk_end_request function returns non-zero even
  2954. * though all data has been transferred and no errors
  2955. * were returned by the host controller, it's a bug.
  2956. */
  2957. if (status == MMC_BLK_SUCCESS && req_pending) {
  2958. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  2959. __func__, blk_rq_bytes(old_req),
  2960. brq->data.bytes_xfered);
  2961. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  2962. return;
  2963. }
  2964. break;
  2965. case MMC_BLK_CMD_ERR:
  2966. req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
  2967. if (mmc_blk_reset(md, card->host, type)) {
  2968. if (req_pending)
  2969. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  2970. else
  2971. atomic_dec(&mq->qcnt);
  2972. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  2973. return;
  2974. }
  2975. if (!req_pending) {
  2976. atomic_dec(&mq->qcnt);
  2977. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  2978. return;
  2979. }
  2980. break;
  2981. case MMC_BLK_RETRY:
  2982. retune_retry_done = brq->retune_retry_done;
  2983. if (retry++ < 5)
  2984. break;
  2985. /* Fall through */
  2986. case MMC_BLK_ABORT:
  2987. if (!mmc_blk_reset(md, card->host, type))
  2988. break;
  2989. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  2990. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  2991. return;
  2992. case MMC_BLK_DATA_ERR: {
  2993. int err;
  2994. err = mmc_blk_reset(md, card->host, type);
  2995. if (!err)
  2996. break;
  2997. if (err == -ENODEV) {
  2998. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  2999. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  3000. return;
  3001. }
  3002. /* Fall through */
  3003. }
  3004. case MMC_BLK_ECC_ERR:
  3005. if (brq->data.blocks > 1) {
  3006. /* Redo read one sector at a time */
  3007. pr_warn("%s: retrying using single block read\n",
  3008. old_req->rq_disk->disk_name);
  3009. disable_multi = 1;
  3010. break;
  3011. }
  3012. /*
  3013. * After an error, we redo I/O one sector at a
  3014. * time, so we only reach here after trying to
  3015. * read a single sector.
  3016. */
  3017. req_pending = blk_end_request(old_req, BLK_STS_IOERR,
  3018. brq->data.blksz);
  3019. if (!req_pending) {
  3020. atomic_dec(&mq->qcnt);
  3021. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  3022. return;
  3023. }
  3024. break;
  3025. case MMC_BLK_NOMEDIUM:
  3026. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  3027. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  3028. return;
  3029. default:
  3030. pr_err("%s: Unhandled return value (%d)",
  3031. old_req->rq_disk->disk_name, status);
  3032. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  3033. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  3034. return;
  3035. }
  3036. if (req_pending) {
  3037. /*
  3038. * In case of a incomplete request
  3039. * prepare it again and resend.
  3040. */
  3041. mmc_blk_rw_rq_prep(mq_rq, card,
  3042. disable_multi, mq);
  3043. mmc_start_areq(card->host,
  3044. &mq_rq->areq, NULL);
  3045. mq_rq->brq.retune_retry_done = retune_retry_done;
  3046. }
  3047. } while (req_pending);
  3048. atomic_dec(&mq->qcnt);
  3049. }
  3050. /* check if the partition support cmdq or not */
  3051. bool mmc_blk_part_cmdq_en(struct mmc_queue *mq)
  3052. {
  3053. #if defined(CONFIG_MTK_EMMC_CQ_SUPPORT) || defined(CONFIG_MTK_EMMC_HW_CQ)
  3054. int ret = false;
  3055. struct mmc_blk_data *md = mq->blkdata;
  3056. struct mmc_card *card;
  3057. if (!md)
  3058. return false;
  3059. card = md->queue.card;
  3060. /* enable cmdq at support partition */
  3061. if (card->ext_csd.cmdq_support
  3062. && md->part_type <= PART_CMDQ_EN)
  3063. ret = true;
  3064. return ret;
  3065. #else
  3066. /* return false for cmdq off */
  3067. return false;
  3068. #endif
  3069. }
  3070. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3071. int mmc_blk_end_queued_req(struct mmc_host *host,
  3072. struct mmc_async_req *areq_active, int index, int status)
  3073. {
  3074. struct mmc_queue *mq;
  3075. struct mmc_blk_data *md;
  3076. struct mmc_card *card = host->card;
  3077. struct mmc_blk_request *brq;
  3078. int ret = 1, type, areq_cnt;
  3079. struct mmc_queue_req *mq_rq;
  3080. struct request *req;
  3081. unsigned long flags;
  3082. mq_rq = container_of(areq_active, struct mmc_queue_req, areq);
  3083. brq = &mq_rq->brq;
  3084. req = mmc_queue_req_to_req(mq_rq);
  3085. mq = req->q->queuedata;
  3086. md = mq->blkdata;
  3087. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  3088. switch (status) {
  3089. case MMC_BLK_SUCCESS:
  3090. case MMC_BLK_PARTIAL:
  3091. /*
  3092. * A block was successfully transferred.
  3093. */
  3094. mmc_blk_reset_success(md, type);
  3095. ret = blk_end_request(req, 0,
  3096. brq->data.bytes_xfered);
  3097. mq->mqrq[index].req = NULL;
  3098. host->areq_que[index] = NULL;
  3099. /*
  3100. * If the blk_end_request function returns non-zero even
  3101. * though all data has been transferred and no errors
  3102. * were returned by the host controller, it's a bug.
  3103. */
  3104. if (status == MMC_BLK_SUCCESS && ret) {
  3105. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  3106. __func__, blk_rq_bytes(req),
  3107. brq->data.bytes_xfered);
  3108. goto cmd_abort;
  3109. }
  3110. mmc_put_card(card);
  3111. atomic_set(&mq->mqrq[index].index, 0);
  3112. atomic_dec(&mq->qcnt);
  3113. atomic_dec(&host->areq_cnt);
  3114. break;
  3115. case MMC_BLK_CMD_ERR:
  3116. ret = mmc_blk_rw_cmd_err(md, card, brq, req, !!ret);
  3117. mmc_blk_reset(md, card->host, type);
  3118. goto cmd_abort;
  3119. case MMC_BLK_RETRY:
  3120. case MMC_BLK_ABORT:
  3121. mmc_blk_reset(md, card->host, type);
  3122. goto cmd_abort;
  3123. case MMC_BLK_DATA_ERR: {
  3124. int err;
  3125. err = mmc_blk_reset(md, card->host, type);
  3126. if (err == -ENODEV)
  3127. goto cmd_abort;
  3128. /* Fall through */
  3129. }
  3130. case MMC_BLK_ECC_ERR:
  3131. /*
  3132. * After an error, we redo I/O one sector at a
  3133. * time, so we only reach here after trying to
  3134. * read a single sector.
  3135. */
  3136. spin_lock_irqsave(&md->lock, flags);
  3137. ret = __blk_end_request(req, -EIO, brq->data.blksz);
  3138. spin_unlock_irqrestore(&md->lock, flags);
  3139. mq->mqrq[index].req = NULL;
  3140. host->areq_que[index] = NULL;
  3141. mmc_put_card(card);
  3142. atomic_set(&mq->mqrq[index].index, 0);
  3143. atomic_dec(&mq->qcnt);
  3144. atomic_dec(&host->areq_cnt);
  3145. if (!ret)
  3146. goto start_new_req;
  3147. break;
  3148. case MMC_BLK_NOMEDIUM:
  3149. goto cmd_abort;
  3150. default:
  3151. pr_err("%s: Unhandled return value (%d)",
  3152. req->rq_disk->disk_name, status);
  3153. goto cmd_abort;
  3154. }
  3155. /*
  3156. * one request is removed from queue,
  3157. * we wakeup mmcqd to insert new request to queue
  3158. * wakeup only when queue full or queue empty
  3159. */
  3160. areq_cnt = atomic_read(&host->areq_cnt);
  3161. if (areq_cnt >= host->card->ext_csd.cmdq_depth -
  3162. EMMC_MIN_RT_CLASS_TAG_COUNT - 1)
  3163. wake_up_process(mq->thread);
  3164. else if (areq_cnt == 0)
  3165. wake_up_interruptible(&host->cmp_que);
  3166. return 1;
  3167. cmd_abort:
  3168. spin_lock_irq(&md->lock);
  3169. if (mmc_card_removed(card))
  3170. req->cmd_flags |= RQF_QUIET;
  3171. while (ret)
  3172. ret = __blk_end_request(req, -EIO,
  3173. blk_rq_cur_bytes(req));
  3174. spin_unlock_irq(&md->lock);
  3175. mq->mqrq[index].req = NULL;
  3176. host->areq_que[index] = NULL;
  3177. /* Add for coverity check, it shouldn't be NULL */
  3178. if (card)
  3179. mmc_put_card(card);
  3180. atomic_set(&mq->mqrq[index].index, 0);
  3181. atomic_dec(&mq->qcnt);
  3182. atomic_dec(&host->areq_cnt);
  3183. start_new_req:
  3184. /*
  3185. * one request is removed from queue,
  3186. * we wakeup mmcqd to insert new request to queue
  3187. * wakeup only when queue full or queue empty
  3188. */
  3189. areq_cnt = atomic_read(&host->areq_cnt);
  3190. if (areq_cnt >= host->card->ext_csd.cmdq_depth -
  3191. EMMC_MIN_RT_CLASS_TAG_COUNT - 1)
  3192. wake_up_process(mq->thread);
  3193. else if (areq_cnt == 0)
  3194. wake_up_interruptible(&host->cmp_que);
  3195. return 0;
  3196. }
  3197. static int mmc_get_cmdq_index(struct mmc_queue *mq)
  3198. {
  3199. int i;
  3200. /* cmdq should be enabled when calling this function */
  3201. WARN_ON(!mmc_card_cmdq(mq->card));
  3202. for (i = 0; i < mq->card->ext_csd.cmdq_depth; i++) {
  3203. if (!atomic_read(&mq->mqrq[i].index))
  3204. break;
  3205. }
  3206. return i;
  3207. }
  3208. #endif
  3209. #ifdef CONFIG_MTK_EMMC_HW_CQ
  3210. static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
  3211. struct mmc_blk_data *md)
  3212. {
  3213. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  3214. struct mmc_host *host = card->host;
  3215. struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
  3216. u8 part_config = card->ext_csd.part_config;
  3217. if ((main_md->part_curr == md->part_type) &&
  3218. (card->part_curr == md->part_type))
  3219. return 0;
  3220. WARN_ON(!((card->host->caps2 & MMC_CAP2_CQE) &&
  3221. card->ext_csd.cmdq_support &&
  3222. (md->flags & MMC_BLK_CMD_QUEUE)));
  3223. if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
  3224. WARN_ON(mmc_cmdq_halt(host, true));
  3225. /* disable CQ mode in card */
  3226. if (mmc_card_cmdq(card)) {
  3227. WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  3228. EXT_CSD_CMDQ_MODE_EN, 0,
  3229. card->ext_csd.generic_cmd6_time));
  3230. mmc_card_clr_cmdq(card);
  3231. }
  3232. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  3233. part_config |= md->part_type;
  3234. WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  3235. EXT_CSD_PART_CONFIG, part_config,
  3236. card->ext_csd.part_time));
  3237. card->ext_csd.part_config = part_config;
  3238. card->part_curr = md->part_type;
  3239. main_md->part_curr = md->part_type;
  3240. WARN_ON(mmc_cmdq_enable(card));
  3241. /* no need to halt again */
  3242. /*WARN_ON(mmc_cmdq_halt(host, false));*/
  3243. return 0;
  3244. }
  3245. static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
  3246. {
  3247. int ret;
  3248. struct mmc_blk_data *md = mq->blkdata;
  3249. struct mmc_card *card = md->queue.card;
  3250. mmc_get_card(card);
  3251. if (!card->host->cmdq_ctx.active_reqs && mmc_card_doing_bkops(card)) {
  3252. ret = mmc_cmdq_halt(card->host, true);
  3253. if (ret)
  3254. goto out;
  3255. ret = mmc_stop_bkops(card);
  3256. if (ret) {
  3257. pr_notice("%s: %s: mmc_stop_bkops failed %d\n",
  3258. md->disk->disk_name, __func__, ret);
  3259. goto out;
  3260. }
  3261. ret = mmc_cmdq_halt(card->host, false);
  3262. if (ret)
  3263. goto out;
  3264. }
  3265. ret = mmc_blk_cmdq_part_switch(card, md);
  3266. if (ret) {
  3267. pr_notice("%s: %s: partition switch failed %d\n",
  3268. md->disk->disk_name, __func__, ret);
  3269. goto out;
  3270. }
  3271. if (req) {
  3272. struct mmc_host *host = card->host;
  3273. struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
  3274. if (mmc_req_is_special(req) &&
  3275. (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) &&
  3276. ctx->active_small_sector_read_reqs) {
  3277. mmc_cmdq_up_rwsem(host);
  3278. ret = wait_event_interruptible(ctx->queue_empty_wq,
  3279. !ctx->active_reqs);
  3280. if (ret) {
  3281. pr_notice("%s: failed while waiting for the CMDQ to be empty %s err (%d)\n",
  3282. mmc_hostname(host),
  3283. __func__, ret);
  3284. WARN_ON(1); /*bug*/
  3285. }
  3286. /* clear the counter now */
  3287. ctx->active_small_sector_read_reqs = 0;
  3288. ret = mmc_cmdq_down_rwsem(host, req);
  3289. /*
  3290. * If there were small sector (less than 8 sectors) read
  3291. * operations in progress then we have to wait for the
  3292. * outstanding requests to finish and should also have
  3293. * atleast 6 microseconds delay before queuing the DCMD
  3294. * request.
  3295. */
  3296. udelay(MMC_QUIRK_CMDQ_DELAY_BEFORE_DCMD);
  3297. }
  3298. if ((req_op(req) == REQ_OP_DRV_IN)
  3299. || (req_op(req) == REQ_OP_DRV_OUT)) {
  3300. mmc_blk_issue_drv_op(mq, req);
  3301. mmc_put_card(card);
  3302. } else if (req_op(req) == REQ_OP_DISCARD) {
  3303. ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
  3304. } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
  3305. if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
  3306. ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
  3307. else
  3308. ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
  3309. } else if (req_op(req) == REQ_OP_FLUSH) {
  3310. ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
  3311. } else {
  3312. ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
  3313. }
  3314. }
  3315. return ret;
  3316. out:
  3317. if (req)
  3318. blk_end_request_all(req, BLK_STS_IOERR);
  3319. mmc_put_card(card);
  3320. return ret;
  3321. }
  3322. #endif
  3323. void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  3324. {
  3325. int ret;
  3326. struct mmc_blk_data *md = mq->blkdata;
  3327. struct mmc_card *card = md->queue.card;
  3328. bool part_cmdq_en = mmc_blk_part_cmdq_en(mq);
  3329. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3330. bool put_card = false;
  3331. #endif
  3332. if (part_cmdq_en || (req && !atomic_read(&mq->qcnt)))
  3333. /* non-cq: claim host only for the first request
  3334. * cq: claim host for per request
  3335. */
  3336. mmc_get_card(card);
  3337. ret = mmc_blk_part_switch(card, md->part_type);
  3338. if (ret) {
  3339. if (req) {
  3340. blk_end_request_all(req, BLK_STS_IOERR);
  3341. }
  3342. ret = 0;
  3343. if (part_cmdq_en) {
  3344. mmc_put_card(card);
  3345. return;
  3346. }
  3347. goto out;
  3348. }
  3349. if (req) {
  3350. switch (req_op(req)) {
  3351. case REQ_OP_DRV_IN:
  3352. case REQ_OP_DRV_OUT:
  3353. /*
  3354. * Complete ongoing async transfer before issuing
  3355. * ioctl()s
  3356. */
  3357. if (atomic_read(&mq->qcnt))
  3358. mmc_blk_issue_rw_rq(mq, NULL);
  3359. mmc_blk_issue_drv_op(mq, req);
  3360. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3361. put_card = true;
  3362. #endif
  3363. break;
  3364. case REQ_OP_DISCARD:
  3365. /*
  3366. * Complete ongoing async transfer before issuing
  3367. * discard.
  3368. */
  3369. if (atomic_read(&mq->qcnt))
  3370. mmc_blk_issue_rw_rq(mq, NULL);
  3371. mmc_blk_issue_discard_rq(mq, req);
  3372. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3373. put_card = true;
  3374. #endif
  3375. break;
  3376. case REQ_OP_SECURE_ERASE:
  3377. /*
  3378. * Complete ongoing async transfer before issuing
  3379. * secure erase.
  3380. */
  3381. if (atomic_read(&mq->qcnt))
  3382. mmc_blk_issue_rw_rq(mq, NULL);
  3383. mmc_blk_issue_secdiscard_rq(mq, req);
  3384. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3385. put_card = true;
  3386. #endif
  3387. break;
  3388. case REQ_OP_FLUSH:
  3389. /*
  3390. * Complete ongoing async transfer before issuing
  3391. * flush.
  3392. */
  3393. if (atomic_read(&mq->qcnt))
  3394. mmc_blk_issue_rw_rq(mq, NULL);
  3395. mmc_blk_issue_flush(mq, req);
  3396. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3397. put_card = true;
  3398. #endif
  3399. break;
  3400. default:
  3401. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3402. if (part_cmdq_en) {
  3403. int index = 0;
  3404. index = mmc_get_cmdq_index(mq);
  3405. WARN_ON(index >= card->ext_csd.cmdq_depth);
  3406. mq->mqrq[index].req = req;
  3407. atomic_set(&req_to_mmc_queue_req(req)->index,
  3408. index + 1);
  3409. atomic_set(&mq->mqrq[index].index,
  3410. index + 1);
  3411. atomic_inc(&card->host->areq_cnt);
  3412. }
  3413. #endif
  3414. /* Normal request, just issue it */
  3415. mmc_blk_issue_rw_rq(mq, req);
  3416. card->host->context_info.is_waiting_last_req = false;
  3417. break;
  3418. }
  3419. } else {
  3420. /* No request, flushing the pipeline with NULL */
  3421. mmc_blk_issue_rw_rq(mq, NULL);
  3422. card->host->context_info.is_waiting_last_req = false;
  3423. }
  3424. out:
  3425. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3426. /*
  3427. * Since CQ has its own thread to handle mmc_put_card().
  3428. * We don't call mmc_put_card() for read/write requests.
  3429. *
  3430. * But for request REQ_OP_DRV_IN REQ_OP_DRV_OUT
  3431. * REQ_OP_DISCARD REQ_OP_SECURE_ERASE REQ_OP_FLUSH
  3432. * which is other than read/write request needs to
  3433. * call mmc_put_card() here.
  3434. */
  3435. if (part_cmdq_en) {
  3436. if (put_card)
  3437. mmc_put_card(card);
  3438. } else
  3439. #endif
  3440. if (!atomic_read(&mq->qcnt))
  3441. mmc_put_card(card);
  3442. }
  3443. static inline int mmc_blk_readonly(struct mmc_card *card)
  3444. {
  3445. return mmc_card_readonly(card) ||
  3446. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  3447. }
  3448. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  3449. struct device *parent,
  3450. sector_t size,
  3451. bool default_ro,
  3452. const char *subname,
  3453. int area_type)
  3454. {
  3455. struct mmc_blk_data *md;
  3456. int devidx, ret;
  3457. devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
  3458. if (devidx < 0) {
  3459. /*
  3460. * We get -ENOSPC because there are no more any available
  3461. * devidx. The reason may be that, either userspace haven't yet
  3462. * unmounted the partitions, which postpones mmc_blk_release()
  3463. * from being called, or the device has more partitions than
  3464. * what we support.
  3465. */
  3466. if (devidx == -ENOSPC)
  3467. dev_err(mmc_dev(card->host),
  3468. "no more device IDs available\n");
  3469. return ERR_PTR(devidx);
  3470. }
  3471. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  3472. if (!md) {
  3473. ret = -ENOMEM;
  3474. goto out;
  3475. }
  3476. md->area_type = area_type;
  3477. /*
  3478. * Set the read-only status based on the supported commands
  3479. * and the write protect switch.
  3480. */
  3481. md->read_only = mmc_blk_readonly(card);
  3482. md->disk = alloc_disk(perdev_minors);
  3483. if (md->disk == NULL) {
  3484. ret = -ENOMEM;
  3485. goto err_kfree;
  3486. }
  3487. spin_lock_init(&md->lock);
  3488. INIT_LIST_HEAD(&md->part);
  3489. INIT_LIST_HEAD(&md->rpmbs);
  3490. md->usage = 1;
  3491. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  3492. /* inline crypto */
  3493. ret = mmc_init_crypto(card->host);
  3494. if (ret) {
  3495. dev_err(mmc_dev(card->host),
  3496. "mmc-crypto init fail!!\n");
  3497. return ERR_PTR(ret);
  3498. }
  3499. #endif
  3500. ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
  3501. if (ret)
  3502. goto err_putdisk;
  3503. md->queue.blkdata = md;
  3504. md->disk->major = MMC_BLOCK_MAJOR;
  3505. md->disk->first_minor = devidx * perdev_minors;
  3506. md->disk->fops = &mmc_bdops;
  3507. md->disk->private_data = md;
  3508. md->disk->queue = md->queue.queue;
  3509. md->parent = parent;
  3510. set_disk_ro(md->disk, md->read_only || default_ro);
  3511. md->disk->flags = GENHD_FL_EXT_DEVT;
  3512. if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
  3513. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  3514. /*
  3515. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  3516. *
  3517. * - be set for removable media with permanent block devices
  3518. * - be unset for removable block devices with permanent media
  3519. *
  3520. * Since MMC block devices clearly fall under the second
  3521. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  3522. * should use the block device creation/destruction hotplug
  3523. * messages to tell when the card is present.
  3524. */
  3525. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  3526. "mmcblk%u%s", card->host->index, subname ? subname : "");
  3527. if (mmc_card_mmc(card))
  3528. blk_queue_logical_block_size(md->queue.queue,
  3529. card->ext_csd.data_sector_size);
  3530. else
  3531. blk_queue_logical_block_size(md->queue.queue, 512);
  3532. set_capacity(md->disk, size);
  3533. if (mmc_host_cmd23(card->host)) {
  3534. if ((mmc_card_mmc(card) &&
  3535. card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
  3536. (mmc_card_sd(card) &&
  3537. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  3538. md->flags |= MMC_BLK_CMD23;
  3539. }
  3540. if (mmc_card_mmc(card) &&
  3541. md->flags & MMC_BLK_CMD23 &&
  3542. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  3543. card->ext_csd.rel_sectors)) {
  3544. md->flags |= MMC_BLK_REL_WR;
  3545. blk_queue_write_cache(md->queue.queue, true, true);
  3546. }
  3547. #ifdef CONFIG_MTK_EMMC_HW_CQ
  3548. if (card->cqe_init) {
  3549. md->flags |= MMC_BLK_CMD_QUEUE;
  3550. md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
  3551. md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
  3552. md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
  3553. md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
  3554. md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
  3555. }
  3556. #endif
  3557. return md;
  3558. err_putdisk:
  3559. put_disk(md->disk);
  3560. err_kfree:
  3561. kfree(md);
  3562. out:
  3563. ida_simple_remove(&mmc_blk_ida, devidx);
  3564. return ERR_PTR(ret);
  3565. }
  3566. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  3567. {
  3568. sector_t size;
  3569. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  3570. /*
  3571. * The EXT_CSD sector count is in number or 512 byte
  3572. * sectors.
  3573. */
  3574. size = card->ext_csd.sectors;
  3575. } else {
  3576. /*
  3577. * The CSD capacity field is in units of read_blkbits.
  3578. * set_capacity takes units of 512 bytes.
  3579. */
  3580. size = (typeof(sector_t))card->csd.capacity
  3581. << (card->csd.read_blkbits - 9);
  3582. }
  3583. return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  3584. MMC_BLK_DATA_AREA_MAIN);
  3585. }
  3586. static int mmc_blk_alloc_part(struct mmc_card *card,
  3587. struct mmc_blk_data *md,
  3588. unsigned int part_type,
  3589. sector_t size,
  3590. bool default_ro,
  3591. const char *subname,
  3592. int area_type)
  3593. {
  3594. char cap_str[10];
  3595. struct mmc_blk_data *part_md;
  3596. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  3597. subname, area_type);
  3598. if (IS_ERR(part_md))
  3599. return PTR_ERR(part_md);
  3600. part_md->part_type = part_type;
  3601. list_add(&part_md->part, &md->part);
  3602. string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
  3603. cap_str, sizeof(cap_str));
  3604. pr_info("%s: %s %s partition %u %s\n",
  3605. part_md->disk->disk_name, mmc_card_id(card),
  3606. mmc_card_name(card), part_md->part_type, cap_str);
  3607. return 0;
  3608. }
  3609. /**
  3610. * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
  3611. * @filp: the character device file
  3612. * @cmd: the ioctl() command
  3613. * @arg: the argument from userspace
  3614. *
  3615. * This will essentially just redirect the ioctl()s coming in over to
  3616. * the main block device spawning the RPMB character device.
  3617. */
  3618. static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
  3619. unsigned long arg)
  3620. {
  3621. struct mmc_rpmb_data *rpmb = filp->private_data;
  3622. int ret;
  3623. switch (cmd) {
  3624. case MMC_IOC_CMD:
  3625. ret = mmc_blk_ioctl_cmd(rpmb->md,
  3626. (struct mmc_ioc_cmd __user *)arg,
  3627. rpmb);
  3628. break;
  3629. case MMC_IOC_MULTI_CMD:
  3630. ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
  3631. (struct mmc_ioc_multi_cmd __user *)arg,
  3632. rpmb);
  3633. break;
  3634. default:
  3635. ret = -EINVAL;
  3636. break;
  3637. }
  3638. return ret;
  3639. }
  3640. #ifdef CONFIG_COMPAT
  3641. static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
  3642. unsigned long arg)
  3643. {
  3644. return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  3645. }
  3646. #endif
  3647. static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
  3648. {
  3649. struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
  3650. struct mmc_rpmb_data, chrdev);
  3651. get_device(&rpmb->dev);
  3652. filp->private_data = rpmb;
  3653. mmc_blk_get(rpmb->md->disk);
  3654. return nonseekable_open(inode, filp);
  3655. }
  3656. static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
  3657. {
  3658. struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
  3659. struct mmc_rpmb_data, chrdev);
  3660. mmc_blk_put(rpmb->md);
  3661. put_device(&rpmb->dev);
  3662. return 0;
  3663. }
  3664. static const struct file_operations mmc_rpmb_fileops = {
  3665. .release = mmc_rpmb_chrdev_release,
  3666. .open = mmc_rpmb_chrdev_open,
  3667. .owner = THIS_MODULE,
  3668. .llseek = no_llseek,
  3669. .unlocked_ioctl = mmc_rpmb_ioctl,
  3670. #ifdef CONFIG_COMPAT
  3671. .compat_ioctl = mmc_rpmb_ioctl_compat,
  3672. #endif
  3673. };
  3674. static void mmc_blk_rpmb_device_release(struct device *dev)
  3675. {
  3676. struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
  3677. ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
  3678. kfree(rpmb);
  3679. }
  3680. static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
  3681. struct mmc_blk_data *md,
  3682. unsigned int part_index,
  3683. sector_t size,
  3684. const char *subname)
  3685. {
  3686. int devidx, ret;
  3687. char rpmb_name[DISK_NAME_LEN];
  3688. char cap_str[10];
  3689. struct mmc_rpmb_data *rpmb;
  3690. /* This creates the minor number for the RPMB char device */
  3691. devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
  3692. if (devidx < 0)
  3693. return devidx;
  3694. rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
  3695. if (!rpmb) {
  3696. ida_simple_remove(&mmc_rpmb_ida, devidx);
  3697. return -ENOMEM;
  3698. }
  3699. snprintf(rpmb_name, sizeof(rpmb_name),
  3700. "mmcblk%u%s", card->host->index, subname ? subname : "");
  3701. rpmb->id = devidx;
  3702. rpmb->part_index = part_index;
  3703. rpmb->dev.init_name = rpmb_name;
  3704. rpmb->dev.bus = &mmc_rpmb_bus_type;
  3705. rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
  3706. rpmb->dev.parent = &card->dev;
  3707. rpmb->dev.release = mmc_blk_rpmb_device_release;
  3708. device_initialize(&rpmb->dev);
  3709. dev_set_drvdata(&rpmb->dev, rpmb);
  3710. rpmb->md = md;
  3711. cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
  3712. rpmb->chrdev.owner = THIS_MODULE;
  3713. ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
  3714. if (ret) {
  3715. pr_err("%s: could not add character device\n", rpmb_name);
  3716. goto out_put_device;
  3717. }
  3718. list_add(&rpmb->node, &md->rpmbs);
  3719. string_get_size((u64)size, 512, STRING_UNITS_2,
  3720. cap_str, sizeof(cap_str));
  3721. pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
  3722. rpmb_name, mmc_card_id(card),
  3723. mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
  3724. MAJOR(mmc_rpmb_devt), rpmb->id);
  3725. return 0;
  3726. out_put_device:
  3727. put_device(&rpmb->dev);
  3728. return ret;
  3729. }
  3730. static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
  3731. {
  3732. cdev_device_del(&rpmb->chrdev, &rpmb->dev);
  3733. put_device(&rpmb->dev);
  3734. }
  3735. /* MMC Physical partitions consist of two boot partitions and
  3736. * up to four general purpose partitions.
  3737. * For each partition enabled in EXT_CSD a block device will be allocatedi
  3738. * to provide access to the partition.
  3739. */
  3740. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  3741. {
  3742. int idx, ret;
  3743. if (!mmc_card_mmc(card))
  3744. return 0;
  3745. for (idx = 0; idx < card->nr_parts; idx++) {
  3746. if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
  3747. /*
  3748. * RPMB partitions does not provide block access, they
  3749. * are only accessed using ioctl():s. Thus create
  3750. * special RPMB block devices that do not have a
  3751. * backing block queue for these.
  3752. */
  3753. ret = mmc_blk_alloc_rpmb_part(card, md,
  3754. card->part[idx].part_cfg,
  3755. card->part[idx].size >> 9,
  3756. card->part[idx].name);
  3757. if (ret)
  3758. return ret;
  3759. }
  3760. if (card->part[idx].size) {
  3761. ret = mmc_blk_alloc_part(card, md,
  3762. card->part[idx].part_cfg,
  3763. card->part[idx].size >> 9,
  3764. card->part[idx].force_ro,
  3765. card->part[idx].name,
  3766. card->part[idx].area_type);
  3767. if (ret)
  3768. return ret;
  3769. }
  3770. }
  3771. return 0;
  3772. }
  3773. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  3774. {
  3775. struct mmc_card *card;
  3776. if (md) {
  3777. /*
  3778. * Flush remaining requests and free queues. It
  3779. * is freeing the queue that stops new requests
  3780. * from being accepted.
  3781. */
  3782. card = md->queue.card;
  3783. spin_lock_irq(md->queue.queue->queue_lock);
  3784. queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
  3785. spin_unlock_irq(md->queue.queue->queue_lock);
  3786. blk_set_queue_dying(md->queue.queue);
  3787. mmc_cleanup_queue(&md->queue);
  3788. #ifdef CONFIG_MTK_EMMC_HW_CQ
  3789. if (md->flags & MMC_BLK_CMD_QUEUE)
  3790. mmc_cmdq_clean(&md->queue, card);
  3791. #endif
  3792. if (md->disk->flags & GENHD_FL_UP) {
  3793. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  3794. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  3795. card->ext_csd.boot_ro_lockable)
  3796. device_remove_file(disk_to_dev(md->disk),
  3797. &md->power_ro_lock);
  3798. del_gendisk(md->disk);
  3799. }
  3800. mmc_blk_put(md);
  3801. }
  3802. }
  3803. static void mmc_blk_remove_parts(struct mmc_card *card,
  3804. struct mmc_blk_data *md)
  3805. {
  3806. struct list_head *pos, *q;
  3807. struct mmc_blk_data *part_md;
  3808. struct mmc_rpmb_data *rpmb;
  3809. /* Remove RPMB partitions */
  3810. list_for_each_safe(pos, q, &md->rpmbs) {
  3811. rpmb = list_entry(pos, struct mmc_rpmb_data, node);
  3812. list_del(pos);
  3813. mmc_blk_remove_rpmb_part(rpmb);
  3814. }
  3815. /* Remove block partitions */
  3816. list_for_each_safe(pos, q, &md->part) {
  3817. part_md = list_entry(pos, struct mmc_blk_data, part);
  3818. list_del(pos);
  3819. mmc_blk_remove_req(part_md);
  3820. }
  3821. }
  3822. static int mmc_add_disk(struct mmc_blk_data *md)
  3823. {
  3824. int ret;
  3825. struct mmc_card *card = md->queue.card;
  3826. device_add_disk(md->parent, md->disk);
  3827. md->force_ro.show = force_ro_show;
  3828. md->force_ro.store = force_ro_store;
  3829. sysfs_attr_init(&md->force_ro.attr);
  3830. md->force_ro.attr.name = "force_ro";
  3831. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  3832. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  3833. if (ret)
  3834. goto force_ro_fail;
  3835. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  3836. card->ext_csd.boot_ro_lockable) {
  3837. umode_t mode;
  3838. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  3839. mode = S_IRUGO;
  3840. else
  3841. mode = S_IRUGO | S_IWUSR;
  3842. md->power_ro_lock.show = power_ro_lock_show;
  3843. md->power_ro_lock.store = power_ro_lock_store;
  3844. sysfs_attr_init(&md->power_ro_lock.attr);
  3845. md->power_ro_lock.attr.mode = mode;
  3846. md->power_ro_lock.attr.name =
  3847. "ro_lock_until_next_power_on";
  3848. ret = device_create_file(disk_to_dev(md->disk),
  3849. &md->power_ro_lock);
  3850. if (ret)
  3851. goto power_ro_lock_fail;
  3852. }
  3853. return ret;
  3854. power_ro_lock_fail:
  3855. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  3856. force_ro_fail:
  3857. del_gendisk(md->disk);
  3858. return ret;
  3859. }
  3860. #ifdef CONFIG_DEBUG_FS
  3861. static int mmc_dbg_card_status_get(void *data, u64 *val)
  3862. {
  3863. struct mmc_card *card = data;
  3864. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  3865. struct mmc_queue *mq = &md->queue;
  3866. struct request *req;
  3867. int ret;
  3868. /* Ask the block layer about the card status */
  3869. req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
  3870. if (IS_ERR(req))
  3871. return PTR_ERR(req);
  3872. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
  3873. blk_execute_rq(mq->queue, NULL, req, 0);
  3874. ret = req_to_mmc_queue_req(req)->drv_op_result;
  3875. if (ret >= 0) {
  3876. *val = ret;
  3877. ret = 0;
  3878. }
  3879. blk_put_request(req);
  3880. return ret;
  3881. }
  3882. DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
  3883. NULL, "%08llx\n");
  3884. /* That is two digits * 512 + 1 for newline */
  3885. #define EXT_CSD_STR_LEN 1025
  3886. static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
  3887. {
  3888. struct mmc_card *card = inode->i_private;
  3889. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  3890. struct mmc_queue *mq = &md->queue;
  3891. struct request *req;
  3892. char *buf;
  3893. ssize_t n = 0;
  3894. u8 *ext_csd;
  3895. int err, i;
  3896. buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
  3897. if (!buf)
  3898. return -ENOMEM;
  3899. /* Ask the block layer for the EXT CSD */
  3900. req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
  3901. if (IS_ERR(req)) {
  3902. err = PTR_ERR(req);
  3903. goto out_free;
  3904. }
  3905. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
  3906. req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
  3907. blk_execute_rq(mq->queue, NULL, req, 0);
  3908. err = req_to_mmc_queue_req(req)->drv_op_result;
  3909. blk_put_request(req);
  3910. if (err) {
  3911. pr_err("FAILED %d\n", err);
  3912. goto out_free;
  3913. }
  3914. for (i = 0; i < 512; i++)
  3915. n += sprintf(buf + n, "%02x", ext_csd[i]);
  3916. n += sprintf(buf + n, "\n");
  3917. if (n != EXT_CSD_STR_LEN) {
  3918. err = -EINVAL;
  3919. kfree(ext_csd);
  3920. goto out_free;
  3921. }
  3922. filp->private_data = buf;
  3923. kfree(ext_csd);
  3924. return 0;
  3925. out_free:
  3926. kfree(buf);
  3927. return err;
  3928. }
  3929. static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
  3930. size_t cnt, loff_t *ppos)
  3931. {
  3932. char *buf = filp->private_data;
  3933. return simple_read_from_buffer(ubuf, cnt, ppos,
  3934. buf, EXT_CSD_STR_LEN);
  3935. }
  3936. static int mmc_ext_csd_release(struct inode *inode, struct file *file)
  3937. {
  3938. kfree(file->private_data);
  3939. return 0;
  3940. }
  3941. static const struct file_operations mmc_dbg_ext_csd_fops = {
  3942. .open = mmc_ext_csd_open,
  3943. .read = mmc_ext_csd_read,
  3944. .release = mmc_ext_csd_release,
  3945. .llseek = default_llseek,
  3946. };
  3947. static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
  3948. {
  3949. struct dentry *root;
  3950. if (!card->debugfs_root)
  3951. return 0;
  3952. root = card->debugfs_root;
  3953. if (mmc_card_mmc(card) || mmc_card_sd(card)) {
  3954. md->status_dentry =
  3955. debugfs_create_file("status", S_IRUSR, root, card,
  3956. &mmc_dbg_card_status_fops);
  3957. if (!md->status_dentry)
  3958. return -EIO;
  3959. }
  3960. if (mmc_card_mmc(card)) {
  3961. md->ext_csd_dentry =
  3962. debugfs_create_file("ext_csd", S_IRUSR, root, card,
  3963. &mmc_dbg_ext_csd_fops);
  3964. if (!md->ext_csd_dentry)
  3965. return -EIO;
  3966. }
  3967. return 0;
  3968. }
  3969. static void mmc_blk_remove_debugfs(struct mmc_card *card,
  3970. struct mmc_blk_data *md)
  3971. {
  3972. if (!card->debugfs_root)
  3973. return;
  3974. if (!IS_ERR_OR_NULL(md->status_dentry)) {
  3975. debugfs_remove(md->status_dentry);
  3976. md->status_dentry = NULL;
  3977. }
  3978. if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
  3979. debugfs_remove(md->ext_csd_dentry);
  3980. md->ext_csd_dentry = NULL;
  3981. }
  3982. }
  3983. #else
  3984. static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
  3985. {
  3986. return 0;
  3987. }
  3988. static void mmc_blk_remove_debugfs(struct mmc_card *card,
  3989. struct mmc_blk_data *md)
  3990. {
  3991. }
  3992. #endif /* CONFIG_DEBUG_FS */
  3993. static int mmc_blk_probe(struct mmc_card *card)
  3994. {
  3995. struct mmc_blk_data *md, *part_md;
  3996. char cap_str[10];
  3997. /*
  3998. * Check that the card supports the command class(es) we need.
  3999. */
  4000. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  4001. return -ENODEV;
  4002. mmc_fixup_device(card, mmc_blk_fixups);
  4003. md = mmc_blk_alloc(card);
  4004. if (IS_ERR(md))
  4005. return PTR_ERR(md);
  4006. string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
  4007. cap_str, sizeof(cap_str));
  4008. pr_info("%s: %s %s %s %s\n",
  4009. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  4010. cap_str, md->read_only ? "(ro)" : "");
  4011. if (mmc_blk_alloc_parts(card, md))
  4012. goto out;
  4013. dev_set_drvdata(&card->dev, md);
  4014. if (mmc_add_disk(md))
  4015. goto out;
  4016. list_for_each_entry(part_md, &md->part, part) {
  4017. if (mmc_add_disk(part_md))
  4018. goto out;
  4019. }
  4020. /* Add two debugfs entries */
  4021. mmc_blk_add_debugfs(card, md);
  4022. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  4023. pm_runtime_use_autosuspend(&card->dev);
  4024. /*
  4025. * Don't enable runtime PM for SD-combo cards here. Leave that
  4026. * decision to be taken during the SDIO init sequence instead.
  4027. */
  4028. if (card->type != MMC_TYPE_SD_COMBO) {
  4029. pm_runtime_set_active(&card->dev);
  4030. pm_runtime_enable(&card->dev);
  4031. }
  4032. return 0;
  4033. out:
  4034. mmc_blk_remove_parts(card, md);
  4035. mmc_blk_remove_req(md);
  4036. return 0;
  4037. }
  4038. static void mmc_blk_remove(struct mmc_card *card)
  4039. {
  4040. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  4041. mmc_blk_remove_debugfs(card, md);
  4042. mmc_blk_remove_parts(card, md);
  4043. pm_runtime_get_sync(&card->dev);
  4044. mmc_claim_host(card->host);
  4045. mmc_blk_part_switch(card, md->part_type);
  4046. mmc_release_host(card->host);
  4047. if (card->type != MMC_TYPE_SD_COMBO)
  4048. pm_runtime_disable(&card->dev);
  4049. pm_runtime_put_noidle(&card->dev);
  4050. mmc_blk_remove_req(md);
  4051. dev_set_drvdata(&card->dev, NULL);
  4052. }
  4053. #ifdef CONFIG_MTK_EMMC_HW_CQ
  4054. static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
  4055. {
  4056. struct mmc_blk_data *part_md;
  4057. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  4058. int rc = 0;
  4059. if (md) {
  4060. rc = mmc_queue_suspend(&md->queue, wait);
  4061. if (rc)
  4062. goto out;
  4063. list_for_each_entry(part_md, &md->part, part) {
  4064. rc = mmc_queue_suspend(&part_md->queue, wait);
  4065. if (rc)
  4066. goto out_resume;
  4067. }
  4068. }
  4069. goto out;
  4070. out_resume:
  4071. mmc_queue_resume(&md->queue);
  4072. list_for_each_entry(part_md, &md->part, part) {
  4073. mmc_queue_resume(&part_md->queue);
  4074. }
  4075. out:
  4076. return rc;
  4077. }
  4078. #else
  4079. static int _mmc_blk_suspend(struct mmc_card *card)
  4080. {
  4081. struct mmc_blk_data *part_md;
  4082. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  4083. if (md) {
  4084. mmc_queue_suspend(&md->queue);
  4085. list_for_each_entry(part_md, &md->part, part) {
  4086. mmc_queue_suspend(&part_md->queue);
  4087. }
  4088. }
  4089. return 0;
  4090. }
  4091. #endif
  4092. static void mmc_blk_shutdown(struct mmc_card *card)
  4093. {
  4094. #ifdef CONFIG_MTK_EMMC_HW_CQ
  4095. _mmc_blk_suspend(card, 1);
  4096. #else
  4097. _mmc_blk_suspend(card);
  4098. #endif
  4099. }
  4100. #ifdef CONFIG_PM_SLEEP
  4101. static int mmc_blk_suspend(struct device *dev)
  4102. {
  4103. struct mmc_card *card = mmc_dev_to_card(dev);
  4104. struct mmc_blk_data *md = dev_get_drvdata(dev);
  4105. int ret;
  4106. #ifdef CONFIG_MTK_EMMC_HW_CQ
  4107. ret = _mmc_blk_suspend(card, 0);
  4108. #else
  4109. ret = _mmc_blk_suspend(card);
  4110. #endif
  4111. if (ret)
  4112. goto out;
  4113. /*
  4114. * Make sure partition is the main one when
  4115. * suspend.
  4116. */
  4117. if (md) {
  4118. ret = mmc_blk_part_switch(card, md->part_type);
  4119. if (ret)
  4120. pr_info("%s: error %d during suspend\n",
  4121. md->disk->disk_name, ret);
  4122. }
  4123. out:
  4124. return ret;
  4125. }
  4126. static int mmc_blk_resume(struct device *dev)
  4127. {
  4128. struct mmc_blk_data *part_md;
  4129. struct mmc_blk_data *md = dev_get_drvdata(dev);
  4130. if (md) {
  4131. /*
  4132. * Resume involves the card going into idle state,
  4133. * so current partition is always the main one.
  4134. */
  4135. md->part_curr = md->part_type;
  4136. mmc_queue_resume(&md->queue);
  4137. list_for_each_entry(part_md, &md->part, part) {
  4138. mmc_queue_resume(&part_md->queue);
  4139. }
  4140. }
  4141. return 0;
  4142. }
  4143. #endif
  4144. static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
  4145. static struct mmc_driver mmc_driver = {
  4146. .drv = {
  4147. .name = "mmcblk",
  4148. .pm = &mmc_blk_pm_ops,
  4149. },
  4150. .probe = mmc_blk_probe,
  4151. .remove = mmc_blk_remove,
  4152. .shutdown = mmc_blk_shutdown,
  4153. };
  4154. static int __init mmc_blk_init(void)
  4155. {
  4156. int res;
  4157. res = bus_register(&mmc_rpmb_bus_type);
  4158. if (res < 0) {
  4159. pr_err("mmcblk: could not register RPMB bus type\n");
  4160. return res;
  4161. }
  4162. res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
  4163. if (res < 0) {
  4164. pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
  4165. goto out_bus_unreg;
  4166. }
  4167. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  4168. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  4169. max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
  4170. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  4171. if (res)
  4172. goto out_chrdev_unreg;
  4173. res = mmc_register_driver(&mmc_driver);
  4174. if (res)
  4175. goto out_blkdev_unreg;
  4176. mt_mmc_biolog_init();
  4177. return 0;
  4178. out_blkdev_unreg:
  4179. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  4180. out_chrdev_unreg:
  4181. unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
  4182. out_bus_unreg:
  4183. bus_unregister(&mmc_rpmb_bus_type);
  4184. return res;
  4185. }
  4186. static void __exit mmc_blk_exit(void)
  4187. {
  4188. mmc_unregister_driver(&mmc_driver);
  4189. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  4190. unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
  4191. bus_unregister(&mmc_rpmb_bus_type);
  4192. }
  4193. module_init(mmc_blk_init);
  4194. module_exit(mmc_blk_exit);
  4195. MODULE_LICENSE("GPL");
  4196. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");