block.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348
  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 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/mutex.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/string_helpers.h>
  33. #include <linux/delay.h>
  34. #include <linux/capability.h>
  35. #include <linux/compat.h>
  36. #include <linux/pm_runtime.h>
  37. #define CREATE_TRACE_POINTS
  38. #include <trace/events/mmc.h>
  39. #include <linux/mmc/ioctl.h>
  40. #include <linux/mmc/card.h>
  41. #include <linux/mmc/host.h>
  42. #include <linux/mmc/mmc.h>
  43. #include <linux/mmc/sd.h>
  44. #ifdef CONFIG_MMC_FFU
  45. #include <linux/mmc/ffu.h>
  46. #endif
  47. #include <asm/uaccess.h>
  48. /*add vmstat info with block tag log*/
  49. #include <linux/vmstat.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/memblock.h>
  52. #ifdef CONFIG_MTK_EXTMEM
  53. #include <linux/exm_driver.h>
  54. #endif
  55. #include "queue.h"
  56. #include <linux/time.h>
  57. #include <linux/debugfs.h>
  58. #include <linux/cpumask.h>
  59. #include <linux/kernel_stat.h>
  60. #include <linux/tick.h>
  61. MODULE_ALIAS("mmc:block");
  62. #ifdef MODULE_PARAM_PREFIX
  63. #undef MODULE_PARAM_PREFIX
  64. #endif
  65. #define MODULE_PARAM_PREFIX "mmcblk."
  66. #define INAND_CMD38_ARG_EXT_CSD 113
  67. #define INAND_CMD38_ARG_ERASE 0x00
  68. #define INAND_CMD38_ARG_TRIM 0x01
  69. #define INAND_CMD38_ARG_SECERASE 0x80
  70. #define INAND_CMD38_ARG_SECTRIM1 0x81
  71. #define INAND_CMD38_ARG_SECTRIM2 0x88
  72. #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  73. #define MMC_SANITIZE_REQ_TIMEOUT 240000
  74. #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
  75. #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
  76. (req->cmd_flags & REQ_META)) && \
  77. (rq_data_dir(req) == WRITE))
  78. #define PACKED_CMD_VER 0x01
  79. #define PACKED_CMD_WR 0x02
  80. static DEFINE_MUTEX(block_mutex);
  81. /*
  82. * The defaults come from config options but can be overriden by module
  83. * or bootarg options.
  84. */
  85. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  86. /*
  87. * We've only got one major, so number of mmcblk devices is
  88. * limited to 256 / number of minors per device.
  89. */
  90. static int max_devices;
  91. /* 256 minors, so at most 256 separate devices */
  92. static DECLARE_BITMAP(dev_use, 256);
  93. static DECLARE_BITMAP(name_use, 256);
  94. /*
  95. * There is one mmc_blk_data per slot.
  96. */
  97. struct mmc_blk_data {
  98. spinlock_t lock;
  99. struct gendisk *disk;
  100. struct mmc_queue queue;
  101. struct list_head part;
  102. unsigned int flags;
  103. #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
  104. #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
  105. #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
  106. unsigned int usage;
  107. unsigned int read_only;
  108. unsigned int part_type;
  109. unsigned int name_idx;
  110. unsigned int reset_done;
  111. #define MMC_BLK_READ BIT(0)
  112. #define MMC_BLK_WRITE BIT(1)
  113. #define MMC_BLK_DISCARD BIT(2)
  114. #define MMC_BLK_SECDISCARD BIT(3)
  115. /*
  116. * Only set in main mmc_blk_data associated
  117. * with mmc_card with mmc_set_drvdata, and keeps
  118. * track of the current selected device partition.
  119. */
  120. unsigned int part_curr;
  121. struct device_attribute force_ro;
  122. struct device_attribute power_ro_lock;
  123. #ifdef MTK_BKOPS_IDLE_MAYA
  124. struct device_attribute bkops_check_threshold;
  125. #endif
  126. int area_type;
  127. };
  128. static DEFINE_MUTEX(open_lock);
  129. enum {
  130. MMC_PACKED_NR_IDX = -1,
  131. MMC_PACKED_NR_ZERO,
  132. MMC_PACKED_NR_SINGLE,
  133. };
  134. module_param(perdev_minors, int, 0444);
  135. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  136. static inline int mmc_blk_part_switch(struct mmc_card *card,
  137. struct mmc_blk_data *md);
  138. static int get_card_status(struct mmc_card *card, u32 *status, int retries);
  139. static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
  140. {
  141. struct mmc_packed *packed = mqrq->packed;
  142. BUG_ON(!packed);
  143. mqrq->cmd_type = MMC_PACKED_NONE;
  144. packed->nr_entries = MMC_PACKED_NR_ZERO;
  145. packed->idx_failure = MMC_PACKED_NR_IDX;
  146. packed->retries = 0;
  147. packed->blocks = 0;
  148. }
  149. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  150. {
  151. struct mmc_blk_data *md;
  152. mutex_lock(&open_lock);
  153. md = disk->private_data;
  154. if (md && md->usage == 0)
  155. md = NULL;
  156. if (md)
  157. md->usage++;
  158. mutex_unlock(&open_lock);
  159. return md;
  160. }
  161. static inline int mmc_get_devidx(struct gendisk *disk)
  162. {
  163. int devidx = disk->first_minor / perdev_minors;
  164. return devidx;
  165. }
  166. static void mmc_blk_put(struct mmc_blk_data *md)
  167. {
  168. mutex_lock(&open_lock);
  169. md->usage--;
  170. if (md->usage == 0) {
  171. int devidx = mmc_get_devidx(md->disk);
  172. blk_cleanup_queue(md->queue.queue);
  173. __clear_bit(devidx, dev_use);
  174. put_disk(md->disk);
  175. kfree(md);
  176. }
  177. mutex_unlock(&open_lock);
  178. }
  179. static ssize_t power_ro_lock_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. int ret;
  183. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  184. struct mmc_card *card = md->queue.card;
  185. int locked = 0;
  186. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
  187. locked = 2;
  188. else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
  189. locked = 1;
  190. ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
  191. return ret;
  192. }
  193. static ssize_t power_ro_lock_store(struct device *dev,
  194. struct device_attribute *attr, const char *buf, size_t count)
  195. {
  196. int ret;
  197. struct mmc_blk_data *md, *part_md;
  198. struct mmc_card *card;
  199. unsigned long set;
  200. if (kstrtoul(buf, 0, &set))
  201. return -EINVAL;
  202. if (set != 1)
  203. return count;
  204. md = mmc_blk_get(dev_to_disk(dev));
  205. card = md->queue.card;
  206. mmc_get_card(card);
  207. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
  208. card->ext_csd.boot_ro_lock |
  209. EXT_CSD_BOOT_WP_B_PWR_WP_EN,
  210. card->ext_csd.part_time);
  211. if (ret)
  212. pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
  213. else
  214. card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
  215. mmc_put_card(card);
  216. if (!ret) {
  217. pr_info("%s: Locking boot partition ro until next power on\n",
  218. md->disk->disk_name);
  219. set_disk_ro(md->disk, 1);
  220. list_for_each_entry(part_md, &md->part, part)
  221. if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
  222. pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
  223. set_disk_ro(part_md->disk, 1);
  224. }
  225. }
  226. mmc_blk_put(md);
  227. return count;
  228. }
  229. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  230. char *buf)
  231. {
  232. int ret;
  233. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  234. ret = snprintf(buf, PAGE_SIZE, "%d\n",
  235. get_disk_ro(dev_to_disk(dev)) ^
  236. md->read_only);
  237. mmc_blk_put(md);
  238. return ret;
  239. }
  240. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. int ret;
  244. char *end;
  245. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  246. unsigned long set = simple_strtoul(buf, &end, 0);
  247. if (end == buf) {
  248. ret = -EINVAL;
  249. goto out;
  250. }
  251. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  252. ret = count;
  253. out:
  254. mmc_blk_put(md);
  255. return ret;
  256. }
  257. #ifdef MTK_BKOPS_IDLE_MAYA
  258. static ssize_t bkops_check_threshold_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  262. struct mmc_card *card = md->queue.card;
  263. int ret;
  264. if (!card)
  265. ret = -EINVAL;
  266. else
  267. ret = snprintf(buf, PAGE_SIZE, "%d\n",
  268. card->bkops_info.size_percentage_to_queue_delayed_work);
  269. mmc_blk_put(md);
  270. return ret;
  271. }
  272. static ssize_t bkops_check_threshold_store(struct device *dev,
  273. struct device_attribute *attr,
  274. const char *buf, size_t count)
  275. {
  276. char value;
  277. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  278. struct mmc_card *card = md->queue.card;
  279. unsigned int card_size;
  280. int ret = count;
  281. int cnt;
  282. if (!card) {
  283. ret = -EINVAL;
  284. goto exit;
  285. }
  286. cnt = sscanf(buf, "%s", &value);
  287. if (cnt != 1)
  288. return -1;
  289. if ((value <= 0) || (value >= 100)) {
  290. ret = -EINVAL;
  291. goto exit;
  292. }
  293. card_size = (unsigned int)get_capacity(md->disk);
  294. if (card_size <= 0) {
  295. ret = -EINVAL;
  296. goto exit;
  297. }
  298. card->bkops_info.size_percentage_to_queue_delayed_work = value;
  299. card->bkops_info.min_sectors_to_queue_delayed_work =
  300. (card_size * value) / 100;
  301. pr_err("%s: size_percentage = %d, min_sectors = %d",
  302. mmc_hostname(card->host),
  303. card->bkops_info.size_percentage_to_queue_delayed_work,
  304. card->bkops_info.min_sectors_to_queue_delayed_work);
  305. exit:
  306. mmc_blk_put(md);
  307. return count;
  308. }
  309. #endif
  310. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  311. {
  312. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  313. int ret = -ENXIO;
  314. mutex_lock(&block_mutex);
  315. if (md) {
  316. if (md->usage == 2)
  317. check_disk_change(bdev);
  318. ret = 0;
  319. if ((mode & FMODE_WRITE) && md->read_only) {
  320. mmc_blk_put(md);
  321. ret = -EROFS;
  322. }
  323. }
  324. mutex_unlock(&block_mutex);
  325. return ret;
  326. }
  327. static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
  328. {
  329. struct mmc_blk_data *md = disk->private_data;
  330. mutex_lock(&block_mutex);
  331. mmc_blk_put(md);
  332. mutex_unlock(&block_mutex);
  333. }
  334. static int
  335. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  336. {
  337. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  338. geo->heads = 4;
  339. geo->sectors = 16;
  340. return 0;
  341. }
  342. struct mmc_blk_ioc_data {
  343. struct mmc_ioc_cmd ic;
  344. unsigned char *buf;
  345. u64 buf_bytes;
  346. };
  347. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  348. struct mmc_ioc_cmd __user *user)
  349. {
  350. struct mmc_blk_ioc_data *idata;
  351. int err;
  352. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  353. if (!idata) {
  354. err = -ENOMEM;
  355. goto out;
  356. }
  357. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  358. err = -EFAULT;
  359. goto idata_err;
  360. }
  361. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  362. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  363. err = -EOVERFLOW;
  364. goto idata_err;
  365. }
  366. if (!idata->buf_bytes)
  367. return idata;
  368. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  369. if (!idata->buf) {
  370. err = -ENOMEM;
  371. goto idata_err;
  372. }
  373. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  374. idata->ic.data_ptr, idata->buf_bytes)) {
  375. err = -EFAULT;
  376. goto copy_err;
  377. }
  378. return idata;
  379. copy_err:
  380. kfree(idata->buf);
  381. idata_err:
  382. kfree(idata);
  383. out:
  384. return ERR_PTR(err);
  385. }
  386. static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
  387. u32 retries_max)
  388. {
  389. int err;
  390. u32 retry_count = 0;
  391. if (!status || !retries_max)
  392. return -EINVAL;
  393. do {
  394. err = get_card_status(card, status, 5);
  395. if (err)
  396. break;
  397. if (!R1_STATUS(*status) &&
  398. (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
  399. break; /* RPMB programming operation complete */
  400. /*
  401. * Rechedule to give the MMC device a chance to continue
  402. * processing the previous command without being polled too
  403. * frequently.
  404. */
  405. usleep_range(1000, 5000);
  406. } while (++retry_count < retries_max);
  407. if (retry_count == retries_max)
  408. err = -EPERM;
  409. return err;
  410. }
  411. static int ioctl_do_sanitize(struct mmc_card *card)
  412. {
  413. int err;
  414. if (!mmc_can_sanitize(card)) {
  415. pr_warn("%s: %s - SANITIZE is not supported\n",
  416. mmc_hostname(card->host), __func__);
  417. err = -EOPNOTSUPP;
  418. goto out;
  419. }
  420. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  421. mmc_hostname(card->host), __func__);
  422. trace_mmc_blk_erase_start(EXT_CSD_SANITIZE_START, 0, 0);
  423. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  424. EXT_CSD_SANITIZE_START, 1,
  425. MMC_SANITIZE_REQ_TIMEOUT);
  426. trace_mmc_blk_erase_end(EXT_CSD_SANITIZE_START, 0, 0);
  427. if (err)
  428. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  429. mmc_hostname(card->host), __func__, err);
  430. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  431. __func__);
  432. out:
  433. return err;
  434. }
  435. #ifdef CONFIG_MMC_FFU
  436. /* This function is modified by from mmc_blk_ioctl() */
  437. static int mmc_ffu_ioctl(struct block_device *bdev,
  438. struct mmc_ioc_cmd __user *ic_ptr)
  439. {
  440. struct mmc_blk_ioc_data *idata;
  441. struct mmc_blk_data *md;
  442. struct mmc_card *card;
  443. struct mmc_command cmd = {0};
  444. struct mmc_data data = {0};
  445. struct mmc_request mrq = {NULL};
  446. struct scatterlist sg;
  447. int err = 0;
  448. idata = mmc_ffu_ioctl_copy_from_user(ic_ptr);
  449. if (IS_ERR(idata))
  450. return PTR_ERR(idata);
  451. md = mmc_blk_get(bdev->bd_disk);
  452. if (!md) {
  453. err = -EINVAL;
  454. goto cmd_err;
  455. }
  456. card = md->queue.card;
  457. if (IS_ERR(card)) {
  458. err = PTR_ERR(card);
  459. goto cmd_done;
  460. }
  461. cmd.opcode = idata->ic.opcode;
  462. cmd.arg = idata->ic.arg;
  463. cmd.flags = idata->ic.flags;
  464. if (idata->buf_bytes) {
  465. data.sg = &sg;
  466. data.sg_len = 1;
  467. data.blksz = idata->ic.blksz;
  468. data.blocks = idata->ic.blocks;
  469. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  470. if (idata->ic.write_flag)
  471. data.flags = MMC_DATA_WRITE;
  472. else
  473. data.flags = MMC_DATA_READ;
  474. /* data.flags must already be set before doing this. */
  475. mmc_set_data_timeout(&data, card);
  476. /* Allow overriding the timeout_ns for empirical tuning. */
  477. if (idata->ic.data_timeout_ns)
  478. data.timeout_ns = idata->ic.data_timeout_ns;
  479. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  480. /*
  481. * Pretend this is a data transfer and rely on the
  482. * host driver to compute timeout. When all host
  483. * drivers support cmd.cmd_timeout for R1B, this
  484. * can be changed to:
  485. *
  486. * mrq.data = NULL;
  487. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  488. */
  489. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  490. }
  491. mrq.data = &data;
  492. }
  493. mrq.cmd = &cmd;
  494. mmc_claim_host(card->host);
  495. if (cmd.opcode == MMC_FFU_DOWNLOAD_OP) {
  496. pr_err("FFU Download start\n");
  497. err = mmc_ffu_download(card, &cmd , idata->buf,
  498. idata->buf_bytes);
  499. if (err) {
  500. pr_err("FFU: %s: error %d FFU download:\n",
  501. mmc_hostname(card->host), err);
  502. }
  503. }
  504. if (cmd.opcode == MMC_SEND_EXT_CSD) {
  505. mmc_wait_for_req(card->host, &mrq);
  506. if (cmd.error) {
  507. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  508. __func__, cmd.error);
  509. err = cmd.error;
  510. goto cmd_rel_host;
  511. }
  512. if (data.error) {
  513. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  514. __func__, data.error);
  515. err = data.error;
  516. goto cmd_rel_host;
  517. }
  518. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  519. err = -EFAULT;
  520. goto cmd_rel_host;
  521. }
  522. if (!idata->ic.write_flag) {
  523. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  524. idata->buf, idata->buf_bytes)) {
  525. err = -EFAULT;
  526. goto cmd_rel_host;
  527. }
  528. }
  529. }
  530. cmd_rel_host:
  531. mmc_release_host(card->host);
  532. cmd_done:
  533. mmc_blk_put(md);
  534. cmd_err:
  535. kfree(idata->buf);
  536. kfree(idata);
  537. return err;
  538. }
  539. #endif
  540. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  541. struct mmc_ioc_cmd __user *ic_ptr)
  542. {
  543. struct mmc_blk_ioc_data *idata;
  544. struct mmc_blk_data *md;
  545. struct mmc_card *card;
  546. struct mmc_command cmd = {0};
  547. struct mmc_data data = {0};
  548. struct mmc_request mrq = {NULL};
  549. struct scatterlist sg;
  550. int err;
  551. int is_rpmb = false;
  552. u32 status = 0;
  553. /*
  554. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  555. * whole block device, not on a partition. This prevents overspray
  556. * between sibling partitions.
  557. */
  558. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  559. return -EPERM;
  560. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  561. if (IS_ERR(idata))
  562. return PTR_ERR(idata);
  563. md = mmc_blk_get(bdev->bd_disk);
  564. if (!md) {
  565. err = -EINVAL;
  566. goto cmd_err;
  567. }
  568. if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
  569. is_rpmb = true;
  570. card = md->queue.card;
  571. if (IS_ERR(card)) {
  572. err = PTR_ERR(card);
  573. goto cmd_done;
  574. }
  575. cmd.opcode = idata->ic.opcode;
  576. cmd.arg = idata->ic.arg;
  577. cmd.flags = idata->ic.flags;
  578. if (idata->buf_bytes) {
  579. data.sg = &sg;
  580. data.sg_len = 1;
  581. data.blksz = idata->ic.blksz;
  582. data.blocks = idata->ic.blocks;
  583. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  584. if (idata->ic.write_flag)
  585. data.flags = MMC_DATA_WRITE;
  586. else
  587. data.flags = MMC_DATA_READ;
  588. /* data.flags must already be set before doing this. */
  589. mmc_set_data_timeout(&data, card);
  590. /* Allow overriding the timeout_ns for empirical tuning. */
  591. if (idata->ic.data_timeout_ns)
  592. data.timeout_ns = idata->ic.data_timeout_ns;
  593. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  594. /*
  595. * Pretend this is a data transfer and rely on the
  596. * host driver to compute timeout. When all host
  597. * drivers support cmd.cmd_timeout for R1B, this
  598. * can be changed to:
  599. *
  600. * mrq.data = NULL;
  601. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  602. */
  603. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  604. }
  605. mrq.data = &data;
  606. }
  607. mrq.cmd = &cmd;
  608. mmc_get_card(card);
  609. err = mmc_blk_part_switch(card, md);
  610. if (err)
  611. goto cmd_rel_host;
  612. if (idata->ic.is_acmd) {
  613. err = mmc_app_cmd(card->host, card);
  614. if (err)
  615. goto cmd_rel_host;
  616. }
  617. if (is_rpmb) {
  618. err = mmc_set_blockcount(card, data.blocks,
  619. idata->ic.write_flag & (1 << 31));
  620. if (err)
  621. goto cmd_rel_host;
  622. }
  623. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
  624. (cmd.opcode == MMC_SWITCH)) {
  625. err = ioctl_do_sanitize(card);
  626. if (err)
  627. pr_err("%s: ioctl_do_sanitize() failed. err = %d",
  628. __func__, err);
  629. goto cmd_rel_host;
  630. }
  631. mmc_wait_for_req(card->host, &mrq);
  632. if (cmd.error) {
  633. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  634. __func__, cmd.error);
  635. err = cmd.error;
  636. goto cmd_rel_host;
  637. }
  638. if (data.error) {
  639. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  640. __func__, data.error);
  641. err = data.error;
  642. goto cmd_rel_host;
  643. }
  644. /*
  645. * According to the SD specs, some commands require a delay after
  646. * issuing the command.
  647. */
  648. if (idata->ic.postsleep_min_us)
  649. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  650. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  651. err = -EFAULT;
  652. goto cmd_rel_host;
  653. }
  654. if (!idata->ic.write_flag) {
  655. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  656. idata->buf, idata->buf_bytes)) {
  657. err = -EFAULT;
  658. goto cmd_rel_host;
  659. }
  660. }
  661. if (is_rpmb) {
  662. /*
  663. * Ensure RPMB command has completed by polling CMD13
  664. * "Send Status".
  665. */
  666. err = ioctl_rpmb_card_status_poll(card, &status, 5);
  667. if (err)
  668. dev_err(mmc_dev(card->host),
  669. "%s: Card Status=0x%08X, error %d\n",
  670. __func__, status, err);
  671. }
  672. cmd_rel_host:
  673. mmc_put_card(card);
  674. cmd_done:
  675. mmc_blk_put(md);
  676. cmd_err:
  677. kfree(idata->buf);
  678. kfree(idata);
  679. return err;
  680. }
  681. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  682. unsigned int cmd, unsigned long arg)
  683. {
  684. int ret = -EINVAL;
  685. if (cmd == MMC_IOC_CMD)
  686. ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
  687. #ifdef CONFIG_MMC_FFU
  688. else if (cmd == MMC_IOC_FFU_CMD)
  689. ret = mmc_ffu_ioctl(bdev, (struct mmc_ioc_cmd __user *)arg);
  690. #endif
  691. return ret;
  692. }
  693. #ifdef CONFIG_COMPAT
  694. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  695. unsigned int cmd, unsigned long arg)
  696. {
  697. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  698. }
  699. #endif
  700. static const struct block_device_operations mmc_bdops = {
  701. .open = mmc_blk_open,
  702. .release = mmc_blk_release,
  703. .getgeo = mmc_blk_getgeo,
  704. .owner = THIS_MODULE,
  705. .ioctl = mmc_blk_ioctl,
  706. #ifdef CONFIG_COMPAT
  707. .compat_ioctl = mmc_blk_compat_ioctl,
  708. #endif
  709. };
  710. static inline int mmc_blk_part_switch(struct mmc_card *card,
  711. struct mmc_blk_data *md)
  712. {
  713. int ret;
  714. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  715. if (main_md->part_curr == md->part_type)
  716. return 0;
  717. if (mmc_card_mmc(card)) {
  718. u8 part_config = card->ext_csd.part_config;
  719. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  720. part_config |= md->part_type;
  721. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  722. EXT_CSD_PART_CONFIG, part_config,
  723. card->ext_csd.part_time);
  724. if (ret)
  725. return ret;
  726. card->ext_csd.part_config = part_config;
  727. }
  728. main_md->part_curr = md->part_type;
  729. return 0;
  730. }
  731. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  732. {
  733. int err;
  734. u32 result;
  735. __be32 *blocks;
  736. struct mmc_request mrq = {NULL};
  737. struct mmc_command cmd = {0};
  738. struct mmc_data data = {0};
  739. struct scatterlist sg;
  740. cmd.opcode = MMC_APP_CMD;
  741. cmd.arg = card->rca << 16;
  742. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  743. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  744. if (err)
  745. return (u32)-1;
  746. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  747. return (u32)-1;
  748. memset(&cmd, 0, sizeof(struct mmc_command));
  749. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  750. cmd.arg = 0;
  751. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  752. data.blksz = 4;
  753. data.blocks = 1;
  754. data.flags = MMC_DATA_READ;
  755. data.sg = &sg;
  756. data.sg_len = 1;
  757. mmc_set_data_timeout(&data, card);
  758. mrq.cmd = &cmd;
  759. mrq.data = &data;
  760. blocks = kmalloc(4, GFP_KERNEL);
  761. if (!blocks)
  762. return (u32)-1;
  763. sg_init_one(&sg, blocks, 4);
  764. mmc_wait_for_req(card->host, &mrq);
  765. result = ntohl(*blocks);
  766. kfree(blocks);
  767. if (cmd.error || data.error)
  768. result = (u32)-1;
  769. return result;
  770. }
  771. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  772. {
  773. struct mmc_command cmd = {0};
  774. int err;
  775. cmd.opcode = MMC_SEND_STATUS;
  776. if (!mmc_host_is_spi(card->host))
  777. cmd.arg = card->rca << 16;
  778. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  779. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  780. if (err == 0)
  781. *status = cmd.resp[0];
  782. return err;
  783. }
  784. static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
  785. bool hw_busy_detect, struct request *req, int *gen_err)
  786. {
  787. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  788. int err = 0;
  789. u32 status;
  790. do {
  791. err = get_card_status(card, &status, 5);
  792. if (err) {
  793. pr_err("%s: error %d requesting status\n",
  794. req->rq_disk->disk_name, err);
  795. return err;
  796. }
  797. if (status & R1_ERROR) {
  798. pr_err("%s: %s: error sending status cmd, status %#x\n",
  799. req->rq_disk->disk_name, __func__, status);
  800. *gen_err = 1;
  801. }
  802. /* We may rely on the host hw to handle busy detection.*/
  803. if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
  804. hw_busy_detect)
  805. break;
  806. /*
  807. * Timeout if the device never becomes ready for data and never
  808. * leaves the program state.
  809. */
  810. if (time_after(jiffies, timeout)) {
  811. pr_err("%s: Card stuck in programming state! %s %s\n",
  812. mmc_hostname(card->host),
  813. req->rq_disk->disk_name, __func__);
  814. return -ETIMEDOUT;
  815. }
  816. /*
  817. * Some cards mishandle the status bits,
  818. * so make sure to check both the busy
  819. * indication and the card state.
  820. */
  821. } while (!(status & R1_READY_FOR_DATA) ||
  822. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  823. return err;
  824. }
  825. static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
  826. struct request *req, int *gen_err, u32 *stop_status)
  827. {
  828. struct mmc_host *host = card->host;
  829. struct mmc_command cmd = {0};
  830. int err;
  831. bool use_r1b_resp = rq_data_dir(req) == WRITE;
  832. /*
  833. * Normally we use R1B responses for WRITE, but in cases where the host
  834. * has specified a max_busy_timeout we need to validate it. A failure
  835. * means we need to prevent the host from doing hw busy detection, which
  836. * is done by converting to a R1 response instead.
  837. */
  838. if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
  839. use_r1b_resp = false;
  840. cmd.opcode = MMC_STOP_TRANSMISSION;
  841. if (use_r1b_resp) {
  842. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  843. cmd.busy_timeout = timeout_ms;
  844. } else {
  845. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  846. }
  847. err = mmc_wait_for_cmd(host, &cmd, 5);
  848. if (err)
  849. return err;
  850. *stop_status = cmd.resp[0];
  851. /* No need to check card status in case of READ. */
  852. if (rq_data_dir(req) == READ)
  853. return 0;
  854. if (!mmc_host_is_spi(host) &&
  855. (*stop_status & R1_ERROR)) {
  856. pr_err("%s: %s: general error sending stop command, resp %#x\n",
  857. req->rq_disk->disk_name, __func__, *stop_status);
  858. *gen_err = 1;
  859. }
  860. return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
  861. }
  862. #define ERR_NOMEDIUM 3
  863. #define ERR_RETRY 2
  864. #define ERR_ABORT 1
  865. #define ERR_CONTINUE 0
  866. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  867. bool status_valid, u32 status)
  868. {
  869. switch (error) {
  870. case -EILSEQ:
  871. /* response crc error, retry the r/w cmd */
  872. pr_err("%s: %s sending %s command, card status %#x\n",
  873. req->rq_disk->disk_name, "response CRC error",
  874. name, status);
  875. return ERR_RETRY;
  876. case -ETIMEDOUT:
  877. pr_err("%s: %s sending %s command, card status %#x\n",
  878. req->rq_disk->disk_name, "timed out", name, status);
  879. /* If the status cmd initially failed, retry the r/w cmd */
  880. if (!status_valid) {
  881. pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
  882. return ERR_RETRY;
  883. }
  884. /*
  885. * If it was a r/w cmd crc error, or illegal command
  886. * (eg, issued in wrong state) then retry - we should
  887. * have corrected the state problem above.
  888. */
  889. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
  890. pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
  891. return ERR_RETRY;
  892. }
  893. /* Otherwise abort the command */
  894. pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
  895. return ERR_ABORT;
  896. default:
  897. /* We don't understand the error code the driver gave us */
  898. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  899. req->rq_disk->disk_name, error, status);
  900. return ERR_ABORT;
  901. }
  902. }
  903. /*
  904. * Initial r/w and stop cmd error recovery.
  905. * We don't know whether the card received the r/w cmd or not, so try to
  906. * restore things back to a sane state. Essentially, we do this as follows:
  907. * - Obtain card status. If the first attempt to obtain card status fails,
  908. * the status word will reflect the failed status cmd, not the failed
  909. * r/w cmd. If we fail to obtain card status, it suggests we can no
  910. * longer communicate with the card.
  911. * - Check the card state. If the card received the cmd but there was a
  912. * transient problem with the response, it might still be in a data transfer
  913. * mode. Try to send it a stop command. If this fails, we can't recover.
  914. * - If the r/w cmd failed due to a response CRC error, it was probably
  915. * transient, so retry the cmd.
  916. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  917. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  918. * illegal cmd, retry.
  919. * Otherwise we don't understand what happened, so abort.
  920. */
  921. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  922. struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
  923. {
  924. bool prev_cmd_status_valid = true;
  925. u32 status, stop_status = 0;
  926. int err, retry;
  927. if (mmc_card_removed(card))
  928. return ERR_NOMEDIUM;
  929. /*
  930. * Try to get card status which indicates both the card state
  931. * and why there was no response. If the first attempt fails,
  932. * we can't be sure the returned status is for the r/w command.
  933. */
  934. for (retry = 2; retry >= 0; retry--) {
  935. err = get_card_status(card, &status, 0);
  936. if (!err)
  937. break;
  938. prev_cmd_status_valid = false;
  939. pr_err("%s: error %d sending status command, %sing\n",
  940. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  941. }
  942. /* We couldn't get a response from the card. Give up. */
  943. if (err) {
  944. /* Check if the card is removed */
  945. if (mmc_detect_card_removed(card->host))
  946. return ERR_NOMEDIUM;
  947. return ERR_ABORT;
  948. }
  949. /* Flag ECC errors */
  950. if ((status & R1_CARD_ECC_FAILED) ||
  951. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  952. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  953. *ecc_err = 1;
  954. /* Flag General errors */
  955. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
  956. if ((status & R1_ERROR) ||
  957. (brq->stop.resp[0] & R1_ERROR)) {
  958. pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
  959. req->rq_disk->disk_name, __func__,
  960. brq->stop.resp[0], status);
  961. *gen_err = 1;
  962. }
  963. /*
  964. * Check the current card state. If it is in some data transfer
  965. * mode, tell it to stop (and hopefully transition back to TRAN.)
  966. */
  967. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  968. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  969. err = send_stop(card,
  970. DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
  971. req, gen_err, &stop_status);
  972. if (err) {
  973. pr_err("%s: error %d sending stop command\n",
  974. req->rq_disk->disk_name, err);
  975. /*
  976. * If the stop cmd also timed out, the card is probably
  977. * not present, so abort. Other errors are bad news too.
  978. */
  979. return ERR_ABORT;
  980. }
  981. if (stop_status & R1_CARD_ECC_FAILED)
  982. *ecc_err = 1;
  983. }
  984. /* Check for set block count errors */
  985. if (brq->sbc.error)
  986. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  987. prev_cmd_status_valid, status);
  988. /* Check for r/w command errors */
  989. if (brq->cmd.error)
  990. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  991. prev_cmd_status_valid, status);
  992. /* Data errors */
  993. if (!brq->stop.error)
  994. return ERR_CONTINUE;
  995. /* Now for stop errors. These aren't fatal to the transfer. */
  996. pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  997. req->rq_disk->disk_name, brq->stop.error,
  998. brq->cmd.resp[0], status);
  999. /*
  1000. * Subsitute in our own stop status as this will give the error
  1001. * state which happened during the execution of the r/w command.
  1002. */
  1003. if (stop_status) {
  1004. brq->stop.resp[0] = stop_status;
  1005. brq->stop.error = 0;
  1006. }
  1007. return ERR_CONTINUE;
  1008. }
  1009. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  1010. int type)
  1011. {
  1012. int err;
  1013. if (md->reset_done & type)
  1014. return -EEXIST;
  1015. md->reset_done |= type;
  1016. err = mmc_hw_reset(host);
  1017. /* Ensure we switch back to the correct partition */
  1018. if (err != -EOPNOTSUPP) {
  1019. struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
  1020. int part_err;
  1021. main_md->part_curr = main_md->part_type;
  1022. part_err = mmc_blk_part_switch(host->card, md);
  1023. if (part_err) {
  1024. /*
  1025. * We have failed to get back into the correct
  1026. * partition, so we need to abort the whole request.
  1027. */
  1028. return -ENODEV;
  1029. }
  1030. }
  1031. return err;
  1032. }
  1033. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  1034. {
  1035. md->reset_done &= ~type;
  1036. }
  1037. int mmc_access_rpmb(struct mmc_queue *mq)
  1038. {
  1039. struct mmc_blk_data *md = mq->data;
  1040. /*
  1041. * If this is a RPMB partition access, return ture
  1042. */
  1043. if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
  1044. return true;
  1045. return false;
  1046. }
  1047. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  1048. {
  1049. struct mmc_blk_data *md = mq->data;
  1050. struct mmc_card *card = md->queue.card;
  1051. unsigned int from, nr, arg;
  1052. int err = 0, type = MMC_BLK_DISCARD;
  1053. if (!mmc_can_erase(card)) {
  1054. err = -EOPNOTSUPP;
  1055. goto out;
  1056. }
  1057. from = blk_rq_pos(req);
  1058. nr = blk_rq_sectors(req);
  1059. #ifdef MTK_BKOPS_IDLE_MAYA
  1060. if (card->ext_csd.bkops_en)
  1061. card->bkops_info.sectors_changed += blk_rq_sectors(req);
  1062. #endif
  1063. if (mmc_can_discard(card))
  1064. arg = MMC_DISCARD_ARG;
  1065. else if (mmc_can_trim(card))
  1066. arg = MMC_TRIM_ARG;
  1067. else
  1068. arg = MMC_ERASE_ARG;
  1069. retry:
  1070. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1071. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1072. INAND_CMD38_ARG_EXT_CSD,
  1073. arg == MMC_TRIM_ARG ?
  1074. INAND_CMD38_ARG_TRIM :
  1075. INAND_CMD38_ARG_ERASE,
  1076. 0);
  1077. if (err)
  1078. goto out;
  1079. }
  1080. err = mmc_erase(card, from, nr, arg);
  1081. out:
  1082. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  1083. goto retry;
  1084. if (!err)
  1085. mmc_blk_reset_success(md, type);
  1086. blk_end_request(req, err, blk_rq_bytes(req));
  1087. return err ? 0 : 1;
  1088. }
  1089. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  1090. struct request *req)
  1091. {
  1092. struct mmc_blk_data *md = mq->data;
  1093. struct mmc_card *card = md->queue.card;
  1094. unsigned int from, nr, arg;
  1095. int err = 0, type = MMC_BLK_SECDISCARD;
  1096. if (!(mmc_can_secure_erase_trim(card))) {
  1097. err = -EOPNOTSUPP;
  1098. goto out;
  1099. }
  1100. from = blk_rq_pos(req);
  1101. nr = blk_rq_sectors(req);
  1102. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  1103. arg = MMC_SECURE_TRIM1_ARG;
  1104. else
  1105. arg = MMC_SECURE_ERASE_ARG;
  1106. retry:
  1107. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1108. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1109. INAND_CMD38_ARG_EXT_CSD,
  1110. arg == MMC_SECURE_TRIM1_ARG ?
  1111. INAND_CMD38_ARG_SECTRIM1 :
  1112. INAND_CMD38_ARG_SECERASE,
  1113. 0);
  1114. if (err)
  1115. goto out_retry;
  1116. }
  1117. err = mmc_erase(card, from, nr, arg);
  1118. if (err == -EIO)
  1119. goto out_retry;
  1120. if (err)
  1121. goto out;
  1122. if (arg == MMC_SECURE_TRIM1_ARG) {
  1123. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1124. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1125. INAND_CMD38_ARG_EXT_CSD,
  1126. INAND_CMD38_ARG_SECTRIM2,
  1127. 0);
  1128. if (err)
  1129. goto out_retry;
  1130. }
  1131. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  1132. if (err == -EIO)
  1133. goto out_retry;
  1134. if (err)
  1135. goto out;
  1136. }
  1137. out_retry:
  1138. if (err && !mmc_blk_reset(md, card->host, type))
  1139. goto retry;
  1140. if (!err)
  1141. mmc_blk_reset_success(md, type);
  1142. out:
  1143. blk_end_request(req, err, blk_rq_bytes(req));
  1144. return err ? 0 : 1;
  1145. }
  1146. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  1147. {
  1148. struct mmc_blk_data *md = mq->data;
  1149. struct mmc_card *card = md->queue.card;
  1150. int ret = 0;
  1151. ret = mmc_flush_cache(card);
  1152. if (ret)
  1153. ret = -EIO;
  1154. blk_end_request_all(req, ret);
  1155. return ret ? 0 : 1;
  1156. }
  1157. /*
  1158. * Reformat current write as a reliable write, supporting
  1159. * both legacy and the enhanced reliable write MMC cards.
  1160. * In each transfer we'll handle only as much as a single
  1161. * reliable write can handle, thus finish the request in
  1162. * partial completions.
  1163. */
  1164. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  1165. struct mmc_card *card,
  1166. struct request *req)
  1167. {
  1168. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  1169. /* Legacy mode imposes restrictions on transfers. */
  1170. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  1171. brq->data.blocks = 1;
  1172. if (brq->data.blocks > card->ext_csd.rel_sectors)
  1173. brq->data.blocks = card->ext_csd.rel_sectors;
  1174. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  1175. brq->data.blocks = 1;
  1176. }
  1177. }
  1178. #define CMD_ERRORS \
  1179. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  1180. R1_ADDRESS_ERROR | /* Misaligned address */ \
  1181. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  1182. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  1183. R1_CC_ERROR | /* Card controller error */ \
  1184. R1_ERROR) /* General/unknown error */
  1185. static int mmc_blk_err_check(struct mmc_card *card,
  1186. struct mmc_async_req *areq)
  1187. {
  1188. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  1189. mmc_active);
  1190. struct mmc_blk_request *brq = &mq_mrq->brq;
  1191. struct request *req = mq_mrq->req;
  1192. int ecc_err = 0, gen_err = 0;
  1193. /*
  1194. * sbc.error indicates a problem with the set block count
  1195. * command. No data will have been transferred.
  1196. *
  1197. * cmd.error indicates a problem with the r/w command. No
  1198. * data will have been transferred.
  1199. *
  1200. * stop.error indicates a problem with the stop command. Data
  1201. * may have been transferred, or may still be transferring.
  1202. */
  1203. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  1204. brq->data.error) {
  1205. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
  1206. case ERR_RETRY:
  1207. return MMC_BLK_RETRY;
  1208. case ERR_ABORT:
  1209. return MMC_BLK_ABORT;
  1210. case ERR_NOMEDIUM:
  1211. return MMC_BLK_NOMEDIUM;
  1212. case ERR_CONTINUE:
  1213. break;
  1214. }
  1215. }
  1216. /*
  1217. * Check for errors relating to the execution of the
  1218. * initial command - such as address errors. No data
  1219. * has been transferred.
  1220. */
  1221. if (brq->cmd.resp[0] & CMD_ERRORS) {
  1222. pr_err("%s: r/w command failed, status = %#x\n",
  1223. req->rq_disk->disk_name, brq->cmd.resp[0]);
  1224. return MMC_BLK_ABORT;
  1225. }
  1226. /*
  1227. * Everything else is either success, or a data error of some
  1228. * kind. If it was a write, we may have transitioned to
  1229. * program mode, which we have to wait for it to complete.
  1230. */
  1231. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  1232. int err;
  1233. /* Check stop command response */
  1234. if (brq->stop.resp[0] & R1_ERROR) {
  1235. pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
  1236. req->rq_disk->disk_name, __func__,
  1237. brq->stop.resp[0]);
  1238. gen_err = 1;
  1239. }
  1240. err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
  1241. &gen_err);
  1242. if (err)
  1243. return MMC_BLK_CMD_ERR;
  1244. }
  1245. /* if general error occurs, retry the write operation. */
  1246. if (gen_err) {
  1247. pr_warn("%s: retrying write for general error\n",
  1248. req->rq_disk->disk_name);
  1249. return MMC_BLK_RETRY;
  1250. }
  1251. if (brq->data.error) {
  1252. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  1253. req->rq_disk->disk_name, brq->data.error,
  1254. (unsigned)blk_rq_pos(req),
  1255. (unsigned)blk_rq_sectors(req),
  1256. brq->cmd.resp[0], brq->stop.resp[0]);
  1257. if (rq_data_dir(req) == READ) {
  1258. if (ecc_err)
  1259. return MMC_BLK_ECC_ERR;
  1260. return MMC_BLK_DATA_ERR;
  1261. } else {
  1262. return MMC_BLK_CMD_ERR;
  1263. }
  1264. }
  1265. if (!brq->data.bytes_xfered)
  1266. return MMC_BLK_RETRY;
  1267. if (mmc_packed_cmd(mq_mrq->cmd_type)) {
  1268. if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
  1269. return MMC_BLK_PARTIAL;
  1270. else
  1271. return MMC_BLK_SUCCESS;
  1272. }
  1273. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1274. return MMC_BLK_PARTIAL;
  1275. return MMC_BLK_SUCCESS;
  1276. }
  1277. static int mmc_blk_packed_err_check(struct mmc_card *card,
  1278. struct mmc_async_req *areq)
  1279. {
  1280. struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
  1281. mmc_active);
  1282. struct request *req = mq_rq->req;
  1283. struct mmc_packed *packed = mq_rq->packed;
  1284. int err, check, status;
  1285. u8 *ext_csd;
  1286. BUG_ON(!packed);
  1287. packed->retries--;
  1288. check = mmc_blk_err_check(card, areq);
  1289. err = get_card_status(card, &status, 0);
  1290. if (err) {
  1291. pr_err("%s: error %d sending status command\n",
  1292. req->rq_disk->disk_name, err);
  1293. return MMC_BLK_ABORT;
  1294. }
  1295. if (status & R1_EXCEPTION_EVENT) {
  1296. ext_csd = kzalloc(512, GFP_KERNEL);
  1297. if (!ext_csd) {
  1298. pr_err("%s: unable to allocate buffer for ext_csd\n",
  1299. req->rq_disk->disk_name);
  1300. return -ENOMEM;
  1301. }
  1302. err = mmc_send_ext_csd(card, ext_csd);
  1303. if (err) {
  1304. pr_err("%s: error %d sending ext_csd\n",
  1305. req->rq_disk->disk_name, err);
  1306. check = MMC_BLK_ABORT;
  1307. goto free;
  1308. }
  1309. if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
  1310. EXT_CSD_PACKED_FAILURE) &&
  1311. (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1312. EXT_CSD_PACKED_GENERIC_ERROR)) {
  1313. if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1314. EXT_CSD_PACKED_INDEXED_ERROR) {
  1315. packed->idx_failure =
  1316. ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
  1317. check = MMC_BLK_PARTIAL;
  1318. }
  1319. pr_err("%s: packed cmd failed, nr %u, sectors %u, "
  1320. "failure index: %d\n",
  1321. req->rq_disk->disk_name, packed->nr_entries,
  1322. packed->blocks, packed->idx_failure);
  1323. }
  1324. free:
  1325. kfree(ext_csd);
  1326. }
  1327. return check;
  1328. }
  1329. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1330. struct mmc_card *card,
  1331. int disable_multi,
  1332. struct mmc_queue *mq)
  1333. {
  1334. u32 readcmd, writecmd;
  1335. struct mmc_blk_request *brq = &mqrq->brq;
  1336. struct request *req = mqrq->req;
  1337. struct mmc_blk_data *md = mq->data;
  1338. bool do_data_tag;
  1339. /*
  1340. * Reliable writes are used to implement Forced Unit Access and
  1341. * REQ_META accesses, and are supported only on MMCs.
  1342. *
  1343. * XXX: this really needs a good explanation of why REQ_META
  1344. * is treated special.
  1345. */
  1346. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  1347. (req->cmd_flags & REQ_META)) &&
  1348. (rq_data_dir(req) == WRITE) &&
  1349. (md->flags & MMC_BLK_REL_WR);
  1350. memset(brq, 0, sizeof(struct mmc_blk_request));
  1351. brq->mrq.cmd = &brq->cmd;
  1352. brq->mrq.data = &brq->data;
  1353. brq->cmd.arg = blk_rq_pos(req);
  1354. if (!mmc_card_blockaddr(card))
  1355. brq->cmd.arg <<= 9;
  1356. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1357. brq->data.blksz = 512;
  1358. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1359. brq->stop.arg = 0;
  1360. brq->data.blocks = blk_rq_sectors(req);
  1361. /*
  1362. * The block layer doesn't support all sector count
  1363. * restrictions, so we need to be prepared for too big
  1364. * requests.
  1365. */
  1366. if (brq->data.blocks > card->host->max_blk_count)
  1367. brq->data.blocks = card->host->max_blk_count;
  1368. if (brq->data.blocks > 1) {
  1369. /*
  1370. * After a read error, we redo the request one sector
  1371. * at a time in order to accurately determine which
  1372. * sectors can be read successfully.
  1373. */
  1374. if (disable_multi)
  1375. brq->data.blocks = 1;
  1376. /*
  1377. * Some controllers have HW issues while operating
  1378. * in multiple I/O mode
  1379. */
  1380. if (card->host->ops->multi_io_quirk)
  1381. brq->data.blocks = card->host->ops->multi_io_quirk(card,
  1382. (rq_data_dir(req) == READ) ?
  1383. MMC_DATA_READ : MMC_DATA_WRITE,
  1384. brq->data.blocks);
  1385. }
  1386. if (brq->data.blocks > 1 || do_rel_wr) {
  1387. /* SPI multiblock writes terminate using a special
  1388. * token, not a STOP_TRANSMISSION request.
  1389. */
  1390. if (!mmc_host_is_spi(card->host) ||
  1391. rq_data_dir(req) == READ)
  1392. brq->mrq.stop = &brq->stop;
  1393. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1394. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1395. } else {
  1396. brq->mrq.stop = NULL;
  1397. readcmd = MMC_READ_SINGLE_BLOCK;
  1398. writecmd = MMC_WRITE_BLOCK;
  1399. }
  1400. if (rq_data_dir(req) == READ) {
  1401. brq->cmd.opcode = readcmd;
  1402. brq->data.flags |= MMC_DATA_READ;
  1403. if (brq->mrq.stop)
  1404. brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
  1405. MMC_CMD_AC;
  1406. } else {
  1407. brq->cmd.opcode = writecmd;
  1408. brq->data.flags |= MMC_DATA_WRITE;
  1409. if (brq->mrq.stop)
  1410. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
  1411. MMC_CMD_AC;
  1412. }
  1413. if (do_rel_wr)
  1414. mmc_apply_rel_rw(brq, card, req);
  1415. /*
  1416. * Data tag is used only during writing meta data to speed
  1417. * up write and any subsequent read of this meta data
  1418. */
  1419. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1420. (req->cmd_flags & REQ_META) &&
  1421. (rq_data_dir(req) == WRITE) &&
  1422. ((brq->data.blocks * brq->data.blksz) >=
  1423. card->ext_csd.data_tag_unit_size);
  1424. /*
  1425. * Pre-defined multi-block transfers are preferable to
  1426. * open ended-ones (and necessary for reliable writes).
  1427. * However, it is not sufficient to just send CMD23,
  1428. * and avoid the final CMD12, as on an error condition
  1429. * CMD12 (stop) needs to be sent anyway. This, coupled
  1430. * with Auto-CMD23 enhancements provided by some
  1431. * hosts, means that the complexity of dealing
  1432. * with this is best left to the host. If CMD23 is
  1433. * supported by card and host, we'll fill sbc in and let
  1434. * the host deal with handling it correctly. This means
  1435. * that for hosts that don't expose MMC_CAP_CMD23, no
  1436. * change of behavior will be observed.
  1437. *
  1438. * N.B: Some MMC cards experience perf degradation.
  1439. * We'll avoid using CMD23-bounded multiblock writes for
  1440. * these, while retaining features like reliable writes.
  1441. */
  1442. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1443. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1444. do_data_tag)) {
  1445. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1446. brq->sbc.arg = brq->data.blocks |
  1447. (do_rel_wr ? (1 << 31) : 0) |
  1448. (do_data_tag ? (1 << 29) : 0);
  1449. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1450. brq->mrq.sbc = &brq->sbc;
  1451. }
  1452. mmc_set_data_timeout(&brq->data, card);
  1453. brq->data.sg = mqrq->sg;
  1454. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1455. /*
  1456. * Adjust the sg list so it is the same size as the
  1457. * request.
  1458. */
  1459. if (brq->data.blocks != blk_rq_sectors(req)) {
  1460. int i, data_size = brq->data.blocks << 9;
  1461. struct scatterlist *sg;
  1462. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1463. data_size -= sg->length;
  1464. if (data_size <= 0) {
  1465. sg->length += data_size;
  1466. i++;
  1467. break;
  1468. }
  1469. }
  1470. brq->data.sg_len = i;
  1471. }
  1472. mqrq->mmc_active.mrq = &brq->mrq;
  1473. mqrq->mmc_active.err_check = mmc_blk_err_check;
  1474. mmc_queue_bounce_pre(mqrq);
  1475. }
  1476. static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
  1477. struct mmc_card *card)
  1478. {
  1479. unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
  1480. unsigned int max_seg_sz = queue_max_segment_size(q);
  1481. unsigned int len, nr_segs = 0;
  1482. do {
  1483. len = min(hdr_sz, max_seg_sz);
  1484. hdr_sz -= len;
  1485. nr_segs++;
  1486. } while (hdr_sz);
  1487. return nr_segs;
  1488. }
  1489. static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
  1490. {
  1491. struct request_queue *q = mq->queue;
  1492. struct mmc_card *card = mq->card;
  1493. struct request *cur = req, *next = NULL;
  1494. struct mmc_blk_data *md = mq->data;
  1495. struct mmc_queue_req *mqrq = mq->mqrq_cur;
  1496. bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
  1497. unsigned int req_sectors = 0, phys_segments = 0;
  1498. unsigned int max_blk_count, max_phys_segs;
  1499. bool put_back = true;
  1500. u8 max_packed_rw = 0;
  1501. u8 reqs = 0;
  1502. if (!(md->flags & MMC_BLK_PACKED_CMD))
  1503. goto no_packed;
  1504. if ((rq_data_dir(cur) == WRITE) &&
  1505. mmc_host_packed_wr(card->host))
  1506. max_packed_rw = card->ext_csd.max_packed_writes;
  1507. if (max_packed_rw == 0)
  1508. goto no_packed;
  1509. if (mmc_req_rel_wr(cur) &&
  1510. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1511. goto no_packed;
  1512. if (mmc_large_sector(card) &&
  1513. !IS_ALIGNED(blk_rq_sectors(cur), 8))
  1514. goto no_packed;
  1515. mmc_blk_clear_packed(mqrq);
  1516. max_blk_count = min(card->host->max_blk_count,
  1517. card->host->max_req_size >> 9);
  1518. if (unlikely(max_blk_count > 0xffff))
  1519. max_blk_count = 0xffff;
  1520. max_phys_segs = queue_max_segments(q);
  1521. req_sectors += blk_rq_sectors(cur);
  1522. phys_segments += cur->nr_phys_segments;
  1523. if (rq_data_dir(cur) == WRITE) {
  1524. req_sectors += mmc_large_sector(card) ? 8 : 1;
  1525. phys_segments += mmc_calc_packed_hdr_segs(q, card);
  1526. }
  1527. do {
  1528. if (reqs >= max_packed_rw - 1) {
  1529. put_back = false;
  1530. break;
  1531. }
  1532. spin_lock_irq(q->queue_lock);
  1533. next = blk_fetch_request(q);
  1534. spin_unlock_irq(q->queue_lock);
  1535. if (!next) {
  1536. put_back = false;
  1537. break;
  1538. }
  1539. if (mmc_large_sector(card) &&
  1540. !IS_ALIGNED(blk_rq_sectors(next), 8))
  1541. break;
  1542. if (next->cmd_flags & REQ_DISCARD ||
  1543. next->cmd_flags & REQ_FLUSH)
  1544. break;
  1545. if (rq_data_dir(cur) != rq_data_dir(next))
  1546. break;
  1547. if (mmc_req_rel_wr(next) &&
  1548. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1549. break;
  1550. req_sectors += blk_rq_sectors(next);
  1551. if (req_sectors > max_blk_count)
  1552. break;
  1553. phys_segments += next->nr_phys_segments;
  1554. if (phys_segments > max_phys_segs)
  1555. break;
  1556. list_add_tail(&next->queuelist, &mqrq->packed->list);
  1557. cur = next;
  1558. reqs++;
  1559. } while (1);
  1560. if (put_back) {
  1561. spin_lock_irq(q->queue_lock);
  1562. blk_requeue_request(q, next);
  1563. spin_unlock_irq(q->queue_lock);
  1564. }
  1565. if (reqs > 0) {
  1566. list_add(&req->queuelist, &mqrq->packed->list);
  1567. mqrq->packed->nr_entries = ++reqs;
  1568. mqrq->packed->retries = reqs;
  1569. return reqs;
  1570. }
  1571. no_packed:
  1572. mqrq->cmd_type = MMC_PACKED_NONE;
  1573. return 0;
  1574. }
  1575. static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
  1576. struct mmc_card *card,
  1577. struct mmc_queue *mq)
  1578. {
  1579. struct mmc_blk_request *brq = &mqrq->brq;
  1580. struct request *req = mqrq->req;
  1581. struct request *prq;
  1582. struct mmc_blk_data *md = mq->data;
  1583. struct mmc_packed *packed = mqrq->packed;
  1584. bool do_rel_wr, do_data_tag;
  1585. u32 *packed_cmd_hdr;
  1586. u8 hdr_blocks;
  1587. u8 i = 1;
  1588. BUG_ON(!packed);
  1589. mqrq->cmd_type = MMC_PACKED_WRITE;
  1590. packed->blocks = 0;
  1591. packed->idx_failure = MMC_PACKED_NR_IDX;
  1592. packed_cmd_hdr = packed->cmd_hdr;
  1593. memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
  1594. packed_cmd_hdr[0] = (packed->nr_entries << 16) |
  1595. (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
  1596. hdr_blocks = mmc_large_sector(card) ? 8 : 1;
  1597. /*
  1598. * Argument for each entry of packed group
  1599. */
  1600. list_for_each_entry(prq, &packed->list, queuelist) {
  1601. do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
  1602. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1603. (prq->cmd_flags & REQ_META) &&
  1604. (rq_data_dir(prq) == WRITE) &&
  1605. ((brq->data.blocks * brq->data.blksz) >=
  1606. card->ext_csd.data_tag_unit_size);
  1607. /* Argument of CMD23 */
  1608. packed_cmd_hdr[(i * 2)] =
  1609. (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
  1610. (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
  1611. blk_rq_sectors(prq);
  1612. /* Argument of CMD18 or CMD25 */
  1613. packed_cmd_hdr[((i * 2)) + 1] =
  1614. mmc_card_blockaddr(card) ?
  1615. blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
  1616. packed->blocks += blk_rq_sectors(prq);
  1617. i++;
  1618. }
  1619. memset(brq, 0, sizeof(struct mmc_blk_request));
  1620. brq->mrq.cmd = &brq->cmd;
  1621. brq->mrq.data = &brq->data;
  1622. brq->mrq.sbc = &brq->sbc;
  1623. brq->mrq.stop = &brq->stop;
  1624. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1625. brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
  1626. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1627. brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
  1628. brq->cmd.arg = blk_rq_pos(req);
  1629. if (!mmc_card_blockaddr(card))
  1630. brq->cmd.arg <<= 9;
  1631. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1632. brq->data.blksz = 512;
  1633. brq->data.blocks = packed->blocks + hdr_blocks;
  1634. brq->data.flags |= MMC_DATA_WRITE;
  1635. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1636. brq->stop.arg = 0;
  1637. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1638. mmc_set_data_timeout(&brq->data, card);
  1639. brq->data.sg = mqrq->sg;
  1640. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1641. mqrq->mmc_active.mrq = &brq->mrq;
  1642. mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
  1643. mmc_queue_bounce_pre(mqrq);
  1644. }
  1645. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  1646. struct mmc_blk_request *brq, struct request *req,
  1647. int ret)
  1648. {
  1649. struct mmc_queue_req *mq_rq;
  1650. mq_rq = container_of(brq, struct mmc_queue_req, brq);
  1651. /*
  1652. * If this is an SD card and we're writing, we can first
  1653. * mark the known good sectors as ok.
  1654. *
  1655. * If the card is not SD, we can still ok written sectors
  1656. * as reported by the controller (which might be less than
  1657. * the real number of written sectors, but never more).
  1658. */
  1659. if (mmc_card_sd(card)) {
  1660. u32 blocks;
  1661. blocks = mmc_sd_num_wr_blocks(card);
  1662. if (blocks != (u32)-1) {
  1663. ret = blk_end_request(req, 0, blocks << 9);
  1664. }
  1665. } else {
  1666. if (!mmc_packed_cmd(mq_rq->cmd_type))
  1667. ret = blk_end_request(req, 0, brq->data.bytes_xfered);
  1668. }
  1669. return ret;
  1670. }
  1671. static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
  1672. {
  1673. struct request *prq;
  1674. struct mmc_packed *packed = mq_rq->packed;
  1675. int idx = packed->idx_failure, i = 0;
  1676. int ret = 0;
  1677. BUG_ON(!packed);
  1678. while (!list_empty(&packed->list)) {
  1679. prq = list_entry_rq(packed->list.next);
  1680. if (idx == i) {
  1681. /* retry from error index */
  1682. packed->nr_entries -= idx;
  1683. mq_rq->req = prq;
  1684. ret = 1;
  1685. if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
  1686. list_del_init(&prq->queuelist);
  1687. mmc_blk_clear_packed(mq_rq);
  1688. }
  1689. return ret;
  1690. }
  1691. list_del_init(&prq->queuelist);
  1692. blk_end_request(prq, 0, blk_rq_bytes(prq));
  1693. i++;
  1694. }
  1695. mmc_blk_clear_packed(mq_rq);
  1696. return ret;
  1697. }
  1698. static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
  1699. {
  1700. struct request *prq;
  1701. struct mmc_packed *packed = mq_rq->packed;
  1702. BUG_ON(!packed);
  1703. while (!list_empty(&packed->list)) {
  1704. prq = list_entry_rq(packed->list.next);
  1705. list_del_init(&prq->queuelist);
  1706. blk_end_request(prq, -EIO, blk_rq_bytes(prq));
  1707. }
  1708. mmc_blk_clear_packed(mq_rq);
  1709. }
  1710. static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
  1711. struct mmc_queue_req *mq_rq)
  1712. {
  1713. struct request *prq;
  1714. struct request_queue *q = mq->queue;
  1715. struct mmc_packed *packed = mq_rq->packed;
  1716. BUG_ON(!packed);
  1717. while (!list_empty(&packed->list)) {
  1718. prq = list_entry_rq(packed->list.prev);
  1719. if (prq->queuelist.prev != &packed->list) {
  1720. list_del_init(&prq->queuelist);
  1721. spin_lock_irq(q->queue_lock);
  1722. blk_requeue_request(mq->queue, prq);
  1723. spin_unlock_irq(q->queue_lock);
  1724. } else {
  1725. list_del_init(&prq->queuelist);
  1726. }
  1727. }
  1728. mmc_blk_clear_packed(mq_rq);
  1729. }
  1730. #if defined(FEATURE_STORAGE_PERF_INDEX)
  1731. #define PRT_TIME_PERIOD 1000000000
  1732. #define UP_LIMITS_4BYTE 4294967295UL /*((4*1024*1024*1024)-1)*/
  1733. #define ID_CNT 10
  1734. pid_t mmcqd[ID_CNT] = {0};
  1735. bool start_async_req[ID_CNT] = {0};
  1736. unsigned long long start_async_req_time[ID_CNT] = {0};
  1737. unsigned long long mmcqd_tag_t1[ID_CNT] = {0}, mmccid_tag_t1 = 0;
  1738. unsigned long long mmcqd_t_usage_wr[ID_CNT] = {0}, mmcqd_t_usage_rd[ID_CNT] = {0};
  1739. unsigned int mmcqd_rq_size_wr[ID_CNT] = {0}, mmcqd_rq_size_rd[ID_CNT] = {0};
  1740. static unsigned int mmcqd_wr_offset_tag[ID_CNT] = {0}, mmcqd_rd_offset_tag[ID_CNT] = {0};
  1741. static unsigned int mmcqd_wr_offset[ID_CNT] = {0}, mmcqd_rd_offset[ID_CNT] = {0};
  1742. static unsigned int mmcqd_wr_bit[ID_CNT] = {0}, mmcqd_wr_tract[ID_CNT] = {0};
  1743. static unsigned int mmcqd_rd_bit[ID_CNT] = {0}, mmcqd_rd_tract[ID_CNT] = {0};
  1744. static unsigned int mmcqd_wr_break[ID_CNT] = {0}, mmcqd_rd_break[ID_CNT] = {0};
  1745. unsigned int mmcqd_rq_count[ID_CNT] = {0}, mmcqd_wr_rq_count[ID_CNT] = {0}, mmcqd_rd_rq_count[ID_CNT] = {0};
  1746. #ifdef FEATURE_STORAGE_META_LOG
  1747. int check_perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  1748. struct metadata_rwlogger metadata_logger[10] = { { {0} } };
  1749. #endif
  1750. unsigned int mmcqd_work_percent[ID_CNT] = {0};
  1751. unsigned int mmcqd_w_throughput[ID_CNT] = {0};
  1752. unsigned int mmcqd_r_throughput[ID_CNT] = {0};
  1753. unsigned int mmcqd_read_clear[ID_CNT] = {0};
  1754. char block_io_log_dst_buffer[BLOCK_IO_BUFFER_SIZE] = {0};
  1755. char block_io_log_source_buffer[PID_LOG_LENGTH] = {0};
  1756. struct struct_block_io_ring {
  1757. char block_io_log_debugfs[BLOCK_IO_BUFFER_SIZE];
  1758. };
  1759. #define MAX_BLOCK_IO_LOG_COUNT 120
  1760. struct struct_block_io_ring block_io_ring[MAX_BLOCK_IO_LOG_COUNT] = { { { 0 } } };
  1761. int block_io_ring_index = 0;
  1762. int stopringlog = 0;
  1763. static void g_var_clear(unsigned int idx)
  1764. {
  1765. mmcqd_t_usage_wr[idx] = 0;
  1766. mmcqd_t_usage_rd[idx] = 0;
  1767. mmcqd_rq_size_wr[idx] = 0;
  1768. mmcqd_rq_size_rd[idx] = 0;
  1769. mmcqd_rq_count[idx] = 0;
  1770. mmcqd_wr_offset[idx] = 0;
  1771. mmcqd_rd_offset[idx] = 0;
  1772. mmcqd_wr_break[idx] = 0;
  1773. mmcqd_rd_break[idx] = 0;
  1774. mmcqd_wr_tract[idx] = 0;
  1775. mmcqd_wr_bit[idx] = 0;
  1776. mmcqd_rd_tract[idx] = 0;
  1777. mmcqd_rd_bit[idx] = 0;
  1778. mmcqd_wr_rq_count[idx] = 0;
  1779. mmcqd_rd_rq_count[idx] = 0;
  1780. }
  1781. unsigned int find_mmcqd_index(void)
  1782. {
  1783. pid_t mmcqd_pid = 0;
  1784. unsigned int idx = 0;
  1785. unsigned char i = 0;
  1786. mmcqd_pid = task_pid_nr(current);
  1787. if (mmcqd[0] == 0) {
  1788. mmcqd[0] = mmcqd_pid;
  1789. start_async_req[0] = 0;
  1790. }
  1791. for (i = 0; i < ID_CNT; i++) {
  1792. if (mmcqd_pid == mmcqd[i]) {
  1793. idx = i;
  1794. break;
  1795. }
  1796. if ((mmcqd[i] == 0) || (i == ID_CNT-1)) {
  1797. mmcqd[i] = mmcqd_pid;
  1798. start_async_req[i] = 0;
  1799. idx = i;
  1800. break;
  1801. }
  1802. }
  1803. return idx;
  1804. }
  1805. #endif
  1806. #if defined(FEATURE_STORAGE_PID_LOGGER)
  1807. struct struct_pid_logger g_pid_logger[PID_ID_CNT] = { {0, 0, {0} , {0} , {0} , {0} } };
  1808. unsigned char *page_logger = NULL;
  1809. spinlock_t g_locker;
  1810. struct dentry *blockio_dbgfs = NULL;
  1811. static unsigned long long get_current_time_us(void)
  1812. {
  1813. unsigned long long time = sched_clock();
  1814. /* return do_div(time,1000); */
  1815. return time;
  1816. }
  1817. static int block_io_debug_open(struct inode *inode, struct file *file)
  1818. {
  1819. file->private_data = inode->i_private;
  1820. return 0;
  1821. }
  1822. static ssize_t block_io_debug_read(struct file *file, char __user *ubuf, size_t count, loff_t *ppos)
  1823. {
  1824. size_t count1 = 0;
  1825. int index = 0;
  1826. int i = 0;
  1827. char *debug_fs = NULL;
  1828. struct struct_block_io_ring *tmp_logger;
  1829. stopringlog = 1;
  1830. index = block_io_ring_index;
  1831. debug_fs = vmalloc(MAX_BLOCK_IO_LOG_COUNT * sizeof(struct struct_block_io_ring));
  1832. if (debug_fs) {
  1833. memset(debug_fs, 0 , MAX_BLOCK_IO_LOG_COUNT * sizeof(struct struct_block_io_ring));
  1834. for (i = 0; i < MAX_BLOCK_IO_LOG_COUNT; i++) {
  1835. tmp_logger = ((struct struct_block_io_ring *)debug_fs) + i;
  1836. snprintf((tmp_logger->block_io_log_debugfs), BLOCK_IO_BUFFER_SIZE,
  1837. block_io_ring[(index + i)%MAX_BLOCK_IO_LOG_COUNT].block_io_log_debugfs);
  1838. }
  1839. count1 = simple_read_from_buffer(ubuf, count, ppos, debug_fs,
  1840. sizeof(struct struct_block_io_ring) * MAX_BLOCK_IO_LOG_COUNT);
  1841. vfree(debug_fs);
  1842. }
  1843. stopringlog = 0;
  1844. return count1;
  1845. }
  1846. static ssize_t block_io_debug_write(struct file *file, const char __user *ubuf, size_t count, loff_t *ppos)
  1847. {
  1848. return 0;
  1849. }
  1850. static const struct file_operations block_io_debug_fops = {
  1851. .read = block_io_debug_read,
  1852. .write = block_io_debug_write,
  1853. .open = block_io_debug_open,
  1854. };
  1855. void block_io_dbg_Init(void)
  1856. {
  1857. blockio_dbgfs = debugfs_create_file("blockio", S_IFREG | S_IRUGO, NULL, (void *)0, &block_io_debug_fops);
  1858. }
  1859. void block_io_dbg_deinit(void)
  1860. {
  1861. debugfs_remove(blockio_dbgfs);
  1862. }
  1863. static u64 get_idle_time(int cpu)
  1864. {
  1865. u64 idle, idle_time = -1ULL;
  1866. if (cpu_online(cpu))
  1867. idle_time = get_cpu_idle_time_us(cpu, NULL);
  1868. if (idle_time == -1ULL)
  1869. /* !NO_HZ or cpu offline so we can rely on cpustat.idle */
  1870. idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
  1871. else
  1872. idle = usecs_to_cputime64(idle_time);
  1873. return idle;
  1874. }
  1875. static u64 get_iowait_time(int cpu)
  1876. {
  1877. u64 iowait, iowait_time = -1ULL;
  1878. if (cpu_online(cpu))
  1879. iowait_time = get_cpu_iowait_time_us(cpu, NULL);
  1880. if (iowait_time == -1ULL)
  1881. /* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
  1882. iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
  1883. else
  1884. iowait = usecs_to_cputime64(iowait_time);
  1885. return iowait;
  1886. }
  1887. #endif
  1888. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  1889. {
  1890. struct mmc_blk_data *md = mq->data;
  1891. struct mmc_card *card = md->queue.card;
  1892. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  1893. int ret = 1, disable_multi = 0, retry = 0, type;
  1894. enum mmc_blk_status status;
  1895. struct mmc_queue_req *mq_rq;
  1896. struct request *req = rqc;
  1897. struct mmc_async_req *areq;
  1898. const u8 packed_nr = 2;
  1899. u8 reqs = 0;
  1900. #if defined(FEATURE_STORAGE_PERF_INDEX)
  1901. unsigned long long time1 = 0;
  1902. pid_t mmcqd_pid = 0;
  1903. unsigned long long t_period = 0, t_usage = 0;
  1904. unsigned int t_percent = 0;
  1905. unsigned int perf_meter = 0;
  1906. unsigned int rq_byte = 0, rq_sector = 0, sect_offset = 0;
  1907. unsigned int diversity = 0;
  1908. unsigned int idx = 0;
  1909. #endif
  1910. #if defined(FEATURE_STORAGE_PID_LOGGER)
  1911. unsigned int index = 0;
  1912. uint64_t time = 0;
  1913. unsigned long rem_nsec;
  1914. u64 user, nice, system, idle, iowait, irq, softirq;
  1915. int i;
  1916. #endif
  1917. if (!rqc && !mq->mqrq_prev->req)
  1918. return 0;
  1919. #if defined(FEATURE_STORAGE_PERF_INDEX)
  1920. time1 = sched_clock();
  1921. #endif
  1922. if (rqc) {
  1923. reqs = mmc_blk_prep_packed_list(mq, rqc);
  1924. #ifdef MTK_BKOPS_IDLE_MAYA
  1925. if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
  1926. card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
  1927. #endif
  1928. }
  1929. #if defined(FEATURE_STORAGE_PERF_INDEX)
  1930. mmcqd_pid = task_pid_nr(current);
  1931. idx = find_mmcqd_index();
  1932. mmcqd_read_clear[idx] = 1;
  1933. if (mmccid_tag_t1 == 0)
  1934. mmccid_tag_t1 = time1;
  1935. t_period = time1 - mmccid_tag_t1;
  1936. if (t_period >= (unsigned long long)((PRT_TIME_PERIOD)*(unsigned long long)10))
  1937. mmccid_tag_t1 = time1;
  1938. if (mmcqd_tag_t1[idx] == 0)
  1939. mmcqd_tag_t1[idx] = time1;
  1940. t_period = time1 - mmcqd_tag_t1[idx];
  1941. if (t_period >= (unsigned long long)PRT_TIME_PERIOD) {
  1942. mmcqd_read_clear[idx] = 2;
  1943. mmcqd_work_percent[idx] = 1;
  1944. mmcqd_r_throughput[idx] = 0;
  1945. mmcqd_w_throughput[idx] = 0;
  1946. t_usage = mmcqd_t_usage_wr[idx] + mmcqd_t_usage_rd[idx];
  1947. if (t_period > t_usage*100) {
  1948. snprintf(block_io_log_source_buffer, WORKLOAD_LOG_LENGTH,
  1949. "wl:1%%,%lld,%lld,%d.", t_usage, t_period, mmcqd_rq_count[idx]);
  1950. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  1951. WORKLOAD_LOG_LENGTH);
  1952. } else {
  1953. do_div(t_period, 100); /*boundary issue*/
  1954. t_percent = ((unsigned int)t_usage)/((unsigned int)t_period);
  1955. mmcqd_work_percent[idx] = t_percent;
  1956. snprintf(block_io_log_source_buffer, WORKLOAD_LOG_LENGTH,
  1957. "wl:%d%%,%lld,%lld,%d.", t_percent, t_usage,
  1958. t_period, mmcqd_rq_count[idx]);
  1959. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  1960. WORKLOAD_LOG_LENGTH);
  1961. }
  1962. if (mmcqd_wr_rq_count[idx] >= 2) {
  1963. diversity = mmcqd_wr_offset[idx]/(mmcqd_wr_rq_count[idx]-1);
  1964. snprintf(block_io_log_source_buffer , WRITE_DIVERSITY_LOG_LENGTH,
  1965. "wd:%d,%d,%d,%d,%d.", diversity, mmcqd_wr_rq_count[idx],
  1966. mmcqd_wr_break[idx], mmcqd_wr_tract[idx], mmcqd_wr_bit[idx]);
  1967. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  1968. WRITE_DIVERSITY_LOG_LENGTH);
  1969. }
  1970. if (mmcqd_rd_rq_count[idx] >= 2) {
  1971. diversity = mmcqd_rd_offset[idx]/(mmcqd_rd_rq_count[idx]-1);
  1972. snprintf(block_io_log_source_buffer, READ_DIVERSITY_LOG_LENGTH,
  1973. "rd:%d,%d,%d,%d,%d.", diversity, mmcqd_rd_rq_count[idx],
  1974. mmcqd_rd_break[idx], mmcqd_rd_tract[idx], mmcqd_rd_bit[idx]);
  1975. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  1976. READ_DIVERSITY_LOG_LENGTH);
  1977. }
  1978. if (mmcqd_t_usage_wr[idx]) {
  1979. do_div(mmcqd_t_usage_wr[idx], 1000000); /*boundary issue*/
  1980. if (mmcqd_t_usage_wr[idx]) {
  1981. /* discard print if duration will <1ms*/
  1982. perf_meter = (mmcqd_rq_size_wr[idx])/((unsigned int)mmcqd_t_usage_wr[idx]); /*kb/s*/
  1983. mmcqd_w_throughput[idx] = perf_meter;
  1984. snprintf(block_io_log_source_buffer, WRITE_THROUGHPUT_LOG_LENGTH,
  1985. "wt:%d,%d,%lld.", perf_meter,
  1986. mmcqd_rq_size_wr[idx], mmcqd_t_usage_wr[idx]);
  1987. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  1988. WRITE_THROUGHPUT_LOG_LENGTH);
  1989. }
  1990. }
  1991. if (mmcqd_t_usage_rd[idx]) {
  1992. do_div(mmcqd_t_usage_rd[idx], 1000000); /*boundary issue*/
  1993. if (mmcqd_t_usage_rd[idx]) {
  1994. /* discard print if duration will <1ms*/
  1995. perf_meter = (mmcqd_rq_size_rd[idx])/((unsigned int)mmcqd_t_usage_rd[idx]); /*kb/s*/
  1996. mmcqd_r_throughput[idx] = perf_meter;
  1997. snprintf(block_io_log_source_buffer, READ_THROUGHPUT_LOG_LENGTH,
  1998. "rt:%d,%d,%lld.", perf_meter,
  1999. mmcqd_rq_size_rd[idx], mmcqd_t_usage_rd[idx]);
  2000. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  2001. READ_THROUGHPUT_LOG_LENGTH);
  2002. }
  2003. }
  2004. mmcqd_tag_t1[idx] = time1;
  2005. g_var_clear(idx);
  2006. #if defined(FEATURE_STORAGE_VMSTAT_LOGGER)
  2007. snprintf(block_io_log_source_buffer, VMSTAT_LOG_LENGTH, "vm:%ld,%ld,%ld,%ld,%ld.",
  2008. ((global_page_state(NR_FILE_PAGES)) << (PAGE_SHIFT - 10)),
  2009. ((global_page_state(NR_FILE_DIRTY)) << (PAGE_SHIFT - 10)),
  2010. ((global_page_state(NR_DIRTIED)) << (PAGE_SHIFT - 10)),
  2011. ((global_page_state(NR_WRITEBACK)) << (PAGE_SHIFT - 10)),
  2012. ((global_page_state(NR_WRITTEN)) << (PAGE_SHIFT - 10)));
  2013. strncat(block_io_log_dst_buffer, block_io_log_source_buffer, VMSTAT_LOG_LENGTH);
  2014. #endif
  2015. /*/proc/stat*/
  2016. user = nice = system = idle = iowait =
  2017. irq = softirq = 0;
  2018. for_each_possible_cpu(i) {
  2019. user += kcpustat_cpu(i).cpustat[CPUTIME_USER];
  2020. nice += kcpustat_cpu(i).cpustat[CPUTIME_NICE];
  2021. system += kcpustat_cpu(i).cpustat[CPUTIME_SYSTEM];
  2022. idle += get_idle_time(i);
  2023. iowait += get_iowait_time(i);
  2024. irq += kcpustat_cpu(i).cpustat[CPUTIME_IRQ];
  2025. softirq += kcpustat_cpu(i).cpustat[CPUTIME_SOFTIRQ];
  2026. }
  2027. snprintf(block_io_log_source_buffer, CPUSTAT_LOG_LENGTH,
  2028. "cpu:%llu,%llu,%llu,%llu,%llu,%llu,%llu.",
  2029. cputime64_to_clock_t(user),
  2030. cputime64_to_clock_t(nice),
  2031. cputime64_to_clock_t(system),
  2032. cputime64_to_clock_t(idle),
  2033. cputime64_to_clock_t(iowait),
  2034. cputime64_to_clock_t(irq),
  2035. cputime64_to_clock_t(softirq));
  2036. strncat(block_io_log_dst_buffer, block_io_log_source_buffer, CPUSTAT_LOG_LENGTH);
  2037. /*/proc/stat end*/
  2038. #if defined(FEATURE_STORAGE_PID_LOGGER)
  2039. do {
  2040. int i;
  2041. for (index = 0; index < PID_ID_CNT; index++) {
  2042. if (g_pid_logger[index].current_pid != 0 &&
  2043. g_pid_logger[index].current_pid == mmcqd_pid)
  2044. break;
  2045. }
  2046. if (index == PID_ID_CNT)
  2047. break;
  2048. for (i = 0; i < PID_LOGGER_COUNT; i++) {
  2049. /*printk(KERN_INFO"hank mmcqd %d %d", g_pid_logger[index].pid_logger[i], mmcqd_pid);*/
  2050. if (g_pid_logger[index].pid_logger[i] == 0)
  2051. break;
  2052. sprintf(g_pid_logger[index].pid_buffer+i*37, "{%05d:%05d:%08d:%05d:%08d}",
  2053. g_pid_logger[index].pid_logger[i],
  2054. g_pid_logger[index].pid_logger_counter[i],
  2055. g_pid_logger[index].pid_logger_length[i],
  2056. g_pid_logger[index].pid_logger_r_counter[i],
  2057. g_pid_logger[index].pid_logger_r_length[i]);
  2058. }
  2059. if (i != 0) {
  2060. snprintf(block_io_log_source_buffer, PID_LOG_LENGTH, "pid:%d,%s.",
  2061. g_pid_logger[index].current_pid,
  2062. g_pid_logger[index].pid_buffer);
  2063. strncat(block_io_log_dst_buffer, block_io_log_source_buffer,
  2064. PID_LOG_LENGTH);
  2065. memset(&(g_pid_logger[index].pid_logger), 0, sizeof(unsigned short)*PID_LOGGER_COUNT);
  2066. memset(&(g_pid_logger[index].pid_logger_counter), 0,
  2067. sizeof(unsigned short)*PID_LOGGER_COUNT);
  2068. memset(&(g_pid_logger[index].pid_logger_length), 0,
  2069. sizeof(unsigned int)*PID_LOGGER_COUNT);
  2070. memset(&(g_pid_logger[index].pid_logger_r_counter), 0,
  2071. sizeof(unsigned short)*PID_LOGGER_COUNT);
  2072. memset(&(g_pid_logger[index].pid_logger_r_length), 0,
  2073. sizeof(unsigned int)*PID_LOGGER_COUNT);
  2074. memset(&(g_pid_logger[index].pid_buffer), 0, sizeof(char)*PID_BUFFER_SIZE);
  2075. #if defined(CONFIG_MTK_MORE_PID_LOGGER_COUNT)
  2076. g_pid_logger[index].reserved = 0;
  2077. #endif
  2078. }
  2079. g_pid_logger[index].pid_buffer[0] = '\0';
  2080. } while (0);
  2081. #endif
  2082. #if defined(FEATURE_STORAGE_PID_LOGGER)
  2083. pr_debug("[BLOCK_TAG] mmcqd:%d %s\n", mmcqd[idx], block_io_log_dst_buffer);
  2084. if (stopringlog == 0) {
  2085. memset(&(block_io_ring[block_io_ring_index].block_io_log_debugfs), 0,
  2086. sizeof(char)*BLOCK_IO_BUFFER_SIZE);
  2087. time = get_current_time_us();
  2088. rem_nsec = do_div(time, 1000000000);
  2089. snprintf((block_io_ring[block_io_ring_index].block_io_log_debugfs),
  2090. BLOCK_IO_BUFFER_SIZE, "\n[%5lu.%06lu]q:%d.%s",
  2091. (unsigned long)time, rem_nsec / 1000, idx, block_io_log_dst_buffer);
  2092. block_io_ring_index++;
  2093. block_io_ring_index = block_io_ring_index%MAX_BLOCK_IO_LOG_COUNT;
  2094. }
  2095. memset(block_io_log_dst_buffer, 0, BLOCK_IO_BUFFER_SIZE);
  2096. memset(block_io_log_source_buffer, 0, PID_LOG_LENGTH);
  2097. #endif
  2098. }
  2099. if (rqc) {
  2100. rq_byte = blk_rq_bytes(rqc);
  2101. rq_sector = blk_rq_sectors(rqc);
  2102. if (rq_data_dir(rqc) == WRITE) {
  2103. if (mmcqd_wr_offset_tag[idx] > 0) {
  2104. sect_offset = abs(blk_rq_pos(rqc) - mmcqd_wr_offset_tag[idx]);
  2105. mmcqd_wr_offset[idx] += sect_offset;
  2106. if (sect_offset == 1)
  2107. mmcqd_wr_break[idx]++;
  2108. }
  2109. mmcqd_wr_offset_tag[idx] = blk_rq_pos(rqc) + rq_sector;
  2110. if (rq_sector <= 1)/*512 bytes*/
  2111. mmcqd_wr_bit[idx]++;
  2112. else if (rq_sector >= 1016)/*508kB*/
  2113. mmcqd_wr_tract[idx]++;
  2114. } else {/*read*/
  2115. if (mmcqd_rd_offset_tag[idx] > 0) {
  2116. sect_offset = abs(blk_rq_pos(rqc) - mmcqd_rd_offset_tag[idx]);
  2117. mmcqd_rd_offset[idx] += sect_offset;
  2118. if (sect_offset == 1)
  2119. mmcqd_rd_break[idx]++;
  2120. }
  2121. mmcqd_rd_offset_tag[idx] = blk_rq_pos(rqc) + rq_sector;
  2122. if (rq_sector <= 1)/*512 bytes*/
  2123. mmcqd_rd_bit[idx]++;
  2124. else if (rq_sector >= 1016)/*508kB*/
  2125. mmcqd_rd_tract[idx]++;
  2126. }
  2127. }
  2128. #endif
  2129. do {
  2130. if (rqc) {
  2131. /*
  2132. * When 4KB native sector is enabled, only 8 blocks
  2133. * multiple read or write is allowed
  2134. */
  2135. if ((brq->data.blocks & 0x07) &&
  2136. (card->ext_csd.data_sector_size == 4096)) {
  2137. pr_err("%s: Transfer size is not 4KB sector size aligned\n",
  2138. req->rq_disk->disk_name);
  2139. mq_rq = mq->mqrq_cur;
  2140. goto cmd_abort;
  2141. }
  2142. if (reqs >= packed_nr)
  2143. mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
  2144. card, mq);
  2145. else
  2146. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  2147. areq = &mq->mqrq_cur->mmc_active;
  2148. } else
  2149. areq = NULL;
  2150. areq = mmc_start_req(card->host, areq, (int *) &status);
  2151. if (!areq) {
  2152. if (status == MMC_BLK_NEW_REQUEST)
  2153. mq->flags |= MMC_QUEUE_NEW_REQUEST;
  2154. return 0;
  2155. }
  2156. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  2157. brq = &mq_rq->brq;
  2158. req = mq_rq->req;
  2159. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  2160. mmc_queue_bounce_post(mq_rq);
  2161. switch (status) {
  2162. case MMC_BLK_SUCCESS:
  2163. case MMC_BLK_PARTIAL:
  2164. /*
  2165. * A block was successfully transferred.
  2166. */
  2167. mmc_blk_reset_success(md, type);
  2168. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  2169. ret = mmc_blk_end_packed_req(mq_rq);
  2170. break;
  2171. } else {
  2172. ret = blk_end_request(req, 0,
  2173. brq->data.bytes_xfered);
  2174. }
  2175. /*
  2176. * If the blk_end_request function returns non-zero even
  2177. * though all data has been transferred and no errors
  2178. * were returned by the host controller, it's a bug.
  2179. */
  2180. if (status == MMC_BLK_SUCCESS && ret) {
  2181. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  2182. __func__, blk_rq_bytes(req),
  2183. brq->data.bytes_xfered);
  2184. rqc = NULL;
  2185. goto cmd_abort;
  2186. }
  2187. break;
  2188. case MMC_BLK_CMD_ERR:
  2189. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  2190. if (!mmc_blk_reset(md, card->host, type))
  2191. break;
  2192. goto cmd_abort;
  2193. case MMC_BLK_RETRY:
  2194. if (retry++ < 5)
  2195. break;
  2196. /* Fall through */
  2197. case MMC_BLK_ABORT:
  2198. if (!mmc_blk_reset(md, card->host, type))
  2199. break;
  2200. goto cmd_abort;
  2201. case MMC_BLK_DATA_ERR: {
  2202. int err;
  2203. err = mmc_blk_reset(md, card->host, type);
  2204. if (!err)
  2205. break;
  2206. if (err == -ENODEV ||
  2207. mmc_packed_cmd(mq_rq->cmd_type))
  2208. goto cmd_abort;
  2209. /* Fall through */
  2210. }
  2211. case MMC_BLK_ECC_ERR:
  2212. if (brq->data.blocks > 1) {
  2213. /* Redo read one sector at a time */
  2214. pr_warn("%s: retrying using single block read\n",
  2215. req->rq_disk->disk_name);
  2216. disable_multi = 1;
  2217. break;
  2218. }
  2219. /*
  2220. * After an error, we redo I/O one sector at a
  2221. * time, so we only reach here after trying to
  2222. * read a single sector.
  2223. */
  2224. ret = blk_end_request(req, -EIO,
  2225. brq->data.blksz);
  2226. if (!ret)
  2227. goto start_new_req;
  2228. break;
  2229. case MMC_BLK_NOMEDIUM:
  2230. goto cmd_abort;
  2231. default:
  2232. pr_err("%s: Unhandled return value (%d)",
  2233. req->rq_disk->disk_name, status);
  2234. goto cmd_abort;
  2235. }
  2236. if (ret) {
  2237. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  2238. if (!mq_rq->packed->retries)
  2239. goto cmd_abort;
  2240. mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
  2241. mmc_start_req(card->host,
  2242. &mq_rq->mmc_active, NULL);
  2243. } else {
  2244. /*
  2245. * In case of a incomplete request
  2246. * prepare it again and resend.
  2247. */
  2248. mmc_blk_rw_rq_prep(mq_rq, card,
  2249. disable_multi, mq);
  2250. mmc_start_req(card->host,
  2251. &mq_rq->mmc_active, NULL);
  2252. }
  2253. }
  2254. } while (ret);
  2255. return 1;
  2256. cmd_abort:
  2257. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  2258. mmc_blk_abort_packed_req(mq_rq);
  2259. } else {
  2260. if (mmc_card_removed(card))
  2261. req->cmd_flags |= REQ_QUIET;
  2262. while (ret)
  2263. ret = blk_end_request(req, -EIO,
  2264. blk_rq_cur_bytes(req));
  2265. }
  2266. start_new_req:
  2267. if (rqc) {
  2268. if (mmc_card_removed(card)) {
  2269. rqc->cmd_flags |= REQ_QUIET;
  2270. blk_end_request_all(rqc, -EIO);
  2271. } else {
  2272. /*
  2273. * If current request is packed, it needs to put back.
  2274. */
  2275. if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
  2276. mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
  2277. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  2278. mmc_start_req(card->host,
  2279. &mq->mqrq_cur->mmc_active, NULL);
  2280. }
  2281. }
  2282. return 0;
  2283. }
  2284. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  2285. {
  2286. int ret;
  2287. struct mmc_blk_data *md = mq->data;
  2288. struct mmc_card *card = md->queue.card;
  2289. struct mmc_host *host = card->host;
  2290. unsigned long flags;
  2291. unsigned int cmd_flags = req ? req->cmd_flags : 0;
  2292. #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
  2293. if (mmc_bus_needs_resume(card->host))
  2294. mmc_resume_bus(card->host);
  2295. #endif
  2296. if (req && !mq->mqrq_prev->req) {
  2297. /* claim host only for the first request */
  2298. mmc_get_card(card);
  2299. #ifdef MTK_BKOPS_IDLE_MAYA
  2300. if (card->ext_csd.bkops_en)
  2301. mmc_stop_bkops(card);
  2302. #endif
  2303. }
  2304. ret = mmc_blk_part_switch(card, md);
  2305. if (ret) {
  2306. if (req) {
  2307. blk_end_request_all(req, -EIO);
  2308. }
  2309. ret = 0;
  2310. goto out;
  2311. }
  2312. mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
  2313. if (cmd_flags & REQ_DISCARD) {
  2314. /* complete ongoing async transfer before issuing discard */
  2315. if (card->host->areq)
  2316. mmc_blk_issue_rw_rq(mq, NULL);
  2317. if (req->cmd_flags & REQ_SECURE)
  2318. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  2319. else
  2320. ret = mmc_blk_issue_discard_rq(mq, req);
  2321. } else if (cmd_flags & REQ_FLUSH) {
  2322. /* complete ongoing async transfer before issuing flush */
  2323. if (card->host->areq)
  2324. mmc_blk_issue_rw_rq(mq, NULL);
  2325. ret = mmc_blk_issue_flush(mq, req);
  2326. } else {
  2327. if (!req && host->areq) {
  2328. spin_lock_irqsave(&host->context_info.lock, flags);
  2329. host->context_info.is_waiting_last_req = true;
  2330. spin_unlock_irqrestore(&host->context_info.lock, flags);
  2331. }
  2332. ret = mmc_blk_issue_rw_rq(mq, req);
  2333. }
  2334. out:
  2335. if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
  2336. (cmd_flags & MMC_REQ_SPECIAL_MASK)) {
  2337. /*
  2338. * Release host when there are no more requests
  2339. * and after special request(discard, flush) is done.
  2340. * In case sepecial request, there is no reentry to
  2341. * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
  2342. */
  2343. mmc_put_card(card);
  2344. #ifdef MTK_BKOPS_IDLE_MAYA
  2345. if (mmc_card_need_bkops(card))
  2346. mmc_start_bkops(card, false);
  2347. #endif
  2348. }
  2349. return ret;
  2350. }
  2351. static inline int mmc_blk_readonly(struct mmc_card *card)
  2352. {
  2353. return mmc_card_readonly(card) ||
  2354. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  2355. }
  2356. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  2357. struct device *parent,
  2358. sector_t size,
  2359. bool default_ro,
  2360. const char *subname,
  2361. int area_type)
  2362. {
  2363. struct mmc_blk_data *md;
  2364. int devidx, ret;
  2365. #ifdef MTK_BKOPS_IDLE_MAYA
  2366. unsigned int percentage = BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
  2367. #endif
  2368. devidx = find_first_zero_bit(dev_use, max_devices);
  2369. if (devidx >= max_devices)
  2370. return ERR_PTR(-ENOSPC);
  2371. __set_bit(devidx, dev_use);
  2372. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  2373. if (!md) {
  2374. ret = -ENOMEM;
  2375. goto out;
  2376. }
  2377. /*
  2378. * !subname implies we are creating main mmc_blk_data that will be
  2379. * associated with mmc_card with mmc_set_drvdata. Due to device
  2380. * partitions, devidx will not coincide with a per-physical card
  2381. * index anymore so we keep track of a name index.
  2382. */
  2383. if (!subname) {
  2384. md->name_idx = find_first_zero_bit(name_use, max_devices);
  2385. __set_bit(md->name_idx, name_use);
  2386. } else
  2387. md->name_idx = ((struct mmc_blk_data *)
  2388. dev_to_disk(parent)->private_data)->name_idx;
  2389. md->area_type = area_type;
  2390. /*
  2391. * Set the read-only status based on the supported commands
  2392. * and the write protect switch.
  2393. */
  2394. md->read_only = mmc_blk_readonly(card);
  2395. md->disk = alloc_disk(perdev_minors);
  2396. if (md->disk == NULL) {
  2397. ret = -ENOMEM;
  2398. goto err_kfree;
  2399. }
  2400. spin_lock_init(&md->lock);
  2401. INIT_LIST_HEAD(&md->part);
  2402. md->usage = 1;
  2403. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  2404. if (ret)
  2405. goto err_putdisk;
  2406. #if defined(FEATURE_STORAGE_PID_LOGGER)
  2407. if (!page_logger) {
  2408. unsigned long count = system_dram_size >> PAGE_SHIFT;
  2409. #ifdef CONFIG_MTK_EXTMEM
  2410. page_logger = extmem_malloc_page_align(count * sizeof(struct page_pid_logger));
  2411. #else
  2412. page_logger = vmalloc(count * sizeof(struct page_pid_logger));
  2413. #endif
  2414. if (page_logger)
  2415. memset(page_logger, -1, count*sizeof(struct page_pid_logger));
  2416. else
  2417. pr_debug("page_logger malloc fail\n");
  2418. spin_lock_init(&g_locker);
  2419. }
  2420. #endif
  2421. #if defined(FEATURE_STORAGE_META_LOG)
  2422. check_perdev_minors = perdev_minors;
  2423. #endif
  2424. md->queue.issue_fn = mmc_blk_issue_rq;
  2425. md->queue.data = md;
  2426. md->disk->major = MMC_BLOCK_MAJOR;
  2427. md->disk->first_minor = devidx * perdev_minors;
  2428. md->disk->fops = &mmc_bdops;
  2429. md->disk->private_data = md;
  2430. md->disk->queue = md->queue.queue;
  2431. md->disk->driverfs_dev = parent;
  2432. set_disk_ro(md->disk, md->read_only || default_ro);
  2433. md->disk->flags = GENHD_FL_EXT_DEVT;
  2434. if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
  2435. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  2436. /*
  2437. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  2438. *
  2439. * - be set for removable media with permanent block devices
  2440. * - be unset for removable block devices with permanent media
  2441. *
  2442. * Since MMC block devices clearly fall under the second
  2443. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  2444. * should use the block device creation/destruction hotplug
  2445. * messages to tell when the card is present.
  2446. */
  2447. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  2448. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  2449. if (mmc_card_mmc(card))
  2450. blk_queue_logical_block_size(md->queue.queue,
  2451. card->ext_csd.data_sector_size);
  2452. else
  2453. blk_queue_logical_block_size(md->queue.queue, 512);
  2454. set_capacity(md->disk, size);
  2455. #ifdef MTK_BKOPS_IDLE_MAYA
  2456. card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
  2457. card->bkops_info.min_sectors_to_queue_delayed_work =
  2458. ((unsigned int)size * percentage) / 100;
  2459. #endif
  2460. if (mmc_host_cmd23(card->host)) {
  2461. if (mmc_card_mmc(card) ||
  2462. (mmc_card_sd(card) &&
  2463. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  2464. md->flags |= MMC_BLK_CMD23;
  2465. }
  2466. if (mmc_card_mmc(card) &&
  2467. md->flags & MMC_BLK_CMD23 &&
  2468. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  2469. card->ext_csd.rel_sectors)) {
  2470. md->flags |= MMC_BLK_REL_WR;
  2471. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  2472. }
  2473. if (mmc_card_mmc(card) &&
  2474. (area_type == MMC_BLK_DATA_AREA_MAIN) &&
  2475. (md->flags & MMC_BLK_CMD23) &&
  2476. card->ext_csd.packed_event_en) {
  2477. if (!mmc_packed_init(&md->queue, card))
  2478. md->flags |= MMC_BLK_PACKED_CMD;
  2479. }
  2480. return md;
  2481. err_putdisk:
  2482. put_disk(md->disk);
  2483. err_kfree:
  2484. kfree(md);
  2485. out:
  2486. return ERR_PTR(ret);
  2487. }
  2488. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  2489. {
  2490. sector_t size;
  2491. struct mmc_blk_data *md;
  2492. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  2493. /*
  2494. * The EXT_CSD sector count is in number or 512 byte
  2495. * sectors.
  2496. */
  2497. size = card->ext_csd.sectors;
  2498. } else {
  2499. /*
  2500. * The CSD capacity field is in units of read_blkbits.
  2501. * set_capacity takes units of 512 bytes.
  2502. */
  2503. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  2504. }
  2505. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  2506. MMC_BLK_DATA_AREA_MAIN);
  2507. return md;
  2508. }
  2509. static int mmc_blk_alloc_part(struct mmc_card *card,
  2510. struct mmc_blk_data *md,
  2511. unsigned int part_type,
  2512. sector_t size,
  2513. bool default_ro,
  2514. const char *subname,
  2515. int area_type)
  2516. {
  2517. char cap_str[10];
  2518. struct mmc_blk_data *part_md;
  2519. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  2520. subname, area_type);
  2521. if (IS_ERR(part_md))
  2522. return PTR_ERR(part_md);
  2523. part_md->part_type = part_type;
  2524. list_add(&part_md->part, &md->part);
  2525. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  2526. cap_str, sizeof(cap_str));
  2527. pr_info("%s: %s %s partition %u %s\n",
  2528. part_md->disk->disk_name, mmc_card_id(card),
  2529. mmc_card_name(card), part_md->part_type, cap_str);
  2530. return 0;
  2531. }
  2532. /* MMC Physical partitions consist of two boot partitions and
  2533. * up to four general purpose partitions.
  2534. * For each partition enabled in EXT_CSD a block device will be allocatedi
  2535. * to provide access to the partition.
  2536. */
  2537. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  2538. {
  2539. int idx, ret = 0;
  2540. if (!mmc_card_mmc(card))
  2541. return 0;
  2542. for (idx = 0; idx < card->nr_parts; idx++) {
  2543. if (card->part[idx].size) {
  2544. ret = mmc_blk_alloc_part(card, md,
  2545. card->part[idx].part_cfg,
  2546. card->part[idx].size >> 9,
  2547. card->part[idx].force_ro,
  2548. card->part[idx].name,
  2549. card->part[idx].area_type);
  2550. if (ret)
  2551. return ret;
  2552. }
  2553. }
  2554. return ret;
  2555. }
  2556. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  2557. {
  2558. struct mmc_card *card;
  2559. if (md) {
  2560. /*
  2561. * Flush remaining requests and free queues. It
  2562. * is freeing the queue that stops new requests
  2563. * from being accepted.
  2564. */
  2565. card = md->queue.card;
  2566. mmc_cleanup_queue(&md->queue);
  2567. if (md->flags & MMC_BLK_PACKED_CMD)
  2568. mmc_packed_clean(&md->queue);
  2569. if (md->disk->flags & GENHD_FL_UP) {
  2570. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2571. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  2572. card->ext_csd.boot_ro_lockable)
  2573. device_remove_file(disk_to_dev(md->disk),
  2574. &md->power_ro_lock);
  2575. del_gendisk(md->disk);
  2576. }
  2577. mmc_blk_put(md);
  2578. }
  2579. }
  2580. static void mmc_blk_remove_parts(struct mmc_card *card,
  2581. struct mmc_blk_data *md)
  2582. {
  2583. struct list_head *pos, *q;
  2584. struct mmc_blk_data *part_md;
  2585. __clear_bit(md->name_idx, name_use);
  2586. list_for_each_safe(pos, q, &md->part) {
  2587. part_md = list_entry(pos, struct mmc_blk_data, part);
  2588. list_del(pos);
  2589. mmc_blk_remove_req(part_md);
  2590. }
  2591. }
  2592. static int mmc_add_disk(struct mmc_blk_data *md)
  2593. {
  2594. int ret;
  2595. struct mmc_card *card = md->queue.card;
  2596. add_disk(md->disk);
  2597. md->force_ro.show = force_ro_show;
  2598. md->force_ro.store = force_ro_store;
  2599. sysfs_attr_init(&md->force_ro.attr);
  2600. md->force_ro.attr.name = "force_ro";
  2601. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  2602. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  2603. if (ret)
  2604. goto force_ro_fail;
  2605. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  2606. card->ext_csd.boot_ro_lockable) {
  2607. umode_t mode;
  2608. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  2609. mode = S_IRUGO;
  2610. else
  2611. mode = S_IRUGO | S_IWUSR;
  2612. md->power_ro_lock.show = power_ro_lock_show;
  2613. md->power_ro_lock.store = power_ro_lock_store;
  2614. sysfs_attr_init(&md->power_ro_lock.attr);
  2615. md->power_ro_lock.attr.mode = mode;
  2616. md->power_ro_lock.attr.name =
  2617. "ro_lock_until_next_power_on";
  2618. ret = device_create_file(disk_to_dev(md->disk),
  2619. &md->power_ro_lock);
  2620. if (ret)
  2621. goto power_ro_lock_fail;
  2622. }
  2623. #ifdef MTK_BKOPS_IDLE_MAYA
  2624. md->bkops_check_threshold.show = bkops_check_threshold_show;
  2625. md->bkops_check_threshold.store = bkops_check_threshold_store;
  2626. sysfs_attr_init(&md->bkops_check_threshold.attr);
  2627. md->bkops_check_threshold.attr.name = "bkops_check_threshold";
  2628. md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
  2629. ret = device_create_file(disk_to_dev(md->disk),
  2630. &md->bkops_check_threshold);
  2631. if (ret)
  2632. goto bkops_check_threshold_fails;
  2633. #endif
  2634. return ret;
  2635. #ifdef MTK_BKOPS_IDLE_MAYA
  2636. bkops_check_threshold_fails:
  2637. device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
  2638. #endif
  2639. power_ro_lock_fail:
  2640. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2641. force_ro_fail:
  2642. del_gendisk(md->disk);
  2643. return ret;
  2644. }
  2645. #define CID_MANFID_SANDISK 0x2
  2646. #define CID_MANFID_SANDISK_EMMC 0x45
  2647. #define CID_MANFID_TOSHIBA 0x11
  2648. #define CID_MANFID_MICRON 0x13
  2649. #define CID_MANFID_SAMSUNG 0x15
  2650. static const struct mmc_fixup blk_fixups[] =
  2651. {
  2652. MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2653. MMC_QUIRK_INAND_CMD38),
  2654. MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2655. MMC_QUIRK_INAND_CMD38),
  2656. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2657. MMC_QUIRK_INAND_CMD38),
  2658. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2659. MMC_QUIRK_INAND_CMD38),
  2660. MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
  2661. MMC_QUIRK_INAND_CMD38),
  2662. /*
  2663. * Some MMC cards experience performance degradation with CMD23
  2664. * instead of CMD12-bounded multiblock transfers. For now we'll
  2665. * black list what's bad...
  2666. * - Certain Toshiba cards.
  2667. *
  2668. * N.B. This doesn't affect SD cards.
  2669. */
  2670. MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2671. MMC_QUIRK_BLK_NO_CMD23),
  2672. MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2673. MMC_QUIRK_BLK_NO_CMD23),
  2674. MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2675. MMC_QUIRK_BLK_NO_CMD23),
  2676. /*
  2677. * Some Micron MMC cards needs longer data read timeout than
  2678. * indicated in CSD.
  2679. */
  2680. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
  2681. MMC_QUIRK_LONG_READ_TIME),
  2682. /*
  2683. * On these Samsung MoviNAND parts, performing secure erase or
  2684. * secure trim can result in unrecoverable corruption due to a
  2685. * firmware bug.
  2686. */
  2687. MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2688. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2689. MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2690. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2691. MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2692. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2693. MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2694. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2695. MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2696. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2697. MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2698. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2699. MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2700. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2701. MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  2702. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  2703. #ifdef CONFIG_MTK_EMMC_CACHE
  2704. /*
  2705. * Some MMC cards cache feature, cannot flush the previous
  2706. * cache data by force programming or reliable write
  2707. * which cannot gurrantee the strong order betwee meta data and file data.
  2708. */
  2709. /*
  2710. * Toshiba eMMC after enable cache feature, write performance drop,
  2711. * because flush operation waste much time
  2712. */
  2713. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  2714. MMC_QUIRK_DISABLE_CACHE),
  2715. /*
  2716. * Samsung eMMC after enable cache feature, command will timeout in some condition
  2717. */
  2718. MMC_FIXUP("FJ27MB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
  2719. MMC_QUIRK_DISABLE_CACHE),
  2720. MMC_FIXUP("FJ25AB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
  2721. MMC_QUIRK_DISABLE_CACHE),
  2722. #endif
  2723. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK_EMMC, CID_OEMID_ANY, add_quirk_mmc,
  2724. MMC_QUIRK_DISABLE_SNO),
  2725. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK_EMMC, CID_OEMID_ANY, add_quirk_mmc,
  2726. MMC_QUIRK_DISABLE_SNO),
  2727. END_FIXUP
  2728. };
  2729. static int mmc_blk_probe(struct mmc_card *card)
  2730. {
  2731. struct mmc_blk_data *md, *part_md;
  2732. char cap_str[10];
  2733. /*
  2734. * Check that the card supports the command class(es) we need.
  2735. */
  2736. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  2737. return -ENODEV;
  2738. mmc_fixup_device(card, blk_fixups);
  2739. md = mmc_blk_alloc(card);
  2740. if (IS_ERR(md))
  2741. return PTR_ERR(md);
  2742. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  2743. cap_str, sizeof(cap_str));
  2744. pr_info("%s: %s %s %s %s\n",
  2745. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  2746. cap_str, md->read_only ? "(ro)" : "");
  2747. if (mmc_blk_alloc_parts(card, md))
  2748. goto out;
  2749. mmc_set_drvdata(card, md);
  2750. #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
  2751. mmc_set_bus_resume_policy(card->host, 1);
  2752. #endif
  2753. if (mmc_add_disk(md))
  2754. goto out;
  2755. list_for_each_entry(part_md, &md->part, part) {
  2756. if (mmc_add_disk(part_md))
  2757. goto out;
  2758. }
  2759. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  2760. pm_runtime_use_autosuspend(&card->dev);
  2761. /*
  2762. * Don't enable runtime PM for SD-combo cards here. Leave that
  2763. * decision to be taken during the SDIO init sequence instead.
  2764. */
  2765. if (card->type != MMC_TYPE_SD_COMBO) {
  2766. pm_runtime_set_active(&card->dev);
  2767. pm_runtime_enable(&card->dev);
  2768. }
  2769. return 0;
  2770. out:
  2771. mmc_blk_remove_parts(card, md);
  2772. mmc_blk_remove_req(md);
  2773. return 0;
  2774. }
  2775. static void mmc_blk_remove(struct mmc_card *card)
  2776. {
  2777. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2778. mmc_blk_remove_parts(card, md);
  2779. pm_runtime_get_sync(&card->dev);
  2780. mmc_claim_host(card->host);
  2781. mmc_blk_part_switch(card, md);
  2782. mmc_release_host(card->host);
  2783. if (card->type != MMC_TYPE_SD_COMBO)
  2784. pm_runtime_disable(&card->dev);
  2785. pm_runtime_put_noidle(&card->dev);
  2786. mmc_blk_remove_req(md);
  2787. mmc_set_drvdata(card, NULL);
  2788. #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
  2789. mmc_set_bus_resume_policy(card->host, 0);
  2790. #endif
  2791. }
  2792. static int _mmc_blk_suspend(struct mmc_card *card)
  2793. {
  2794. struct mmc_blk_data *part_md;
  2795. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2796. if (md) {
  2797. mmc_queue_suspend(&md->queue);
  2798. list_for_each_entry(part_md, &md->part, part) {
  2799. mmc_queue_suspend(&part_md->queue);
  2800. }
  2801. }
  2802. return 0;
  2803. }
  2804. static void mmc_blk_shutdown(struct mmc_card *card)
  2805. {
  2806. _mmc_blk_suspend(card);
  2807. }
  2808. #ifdef CONFIG_PM
  2809. static int mmc_blk_suspend(struct mmc_card *card)
  2810. {
  2811. return _mmc_blk_suspend(card);
  2812. }
  2813. static int mmc_blk_resume(struct mmc_card *card)
  2814. {
  2815. struct mmc_blk_data *part_md;
  2816. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2817. if (md) {
  2818. /*
  2819. * Resume involves the card going into idle state,
  2820. * so current partition is always the main one.
  2821. */
  2822. md->part_curr = md->part_type;
  2823. mmc_queue_resume(&md->queue);
  2824. list_for_each_entry(part_md, &md->part, part) {
  2825. mmc_queue_resume(&part_md->queue);
  2826. }
  2827. }
  2828. return 0;
  2829. }
  2830. #else
  2831. #define mmc_blk_suspend NULL
  2832. #define mmc_blk_resume NULL
  2833. #endif
  2834. static struct mmc_driver mmc_driver = {
  2835. .drv = {
  2836. .name = "mmcblk",
  2837. },
  2838. .probe = mmc_blk_probe,
  2839. .remove = mmc_blk_remove,
  2840. .suspend = mmc_blk_suspend,
  2841. .resume = mmc_blk_resume,
  2842. .shutdown = mmc_blk_shutdown,
  2843. };
  2844. static int __init mmc_blk_init(void)
  2845. {
  2846. int res;
  2847. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2848. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2849. max_devices = 256 / perdev_minors;
  2850. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2851. if (res)
  2852. goto out;
  2853. res = mmc_register_driver(&mmc_driver);
  2854. if (res)
  2855. goto out2;
  2856. #if defined(FEATURE_STORAGE_PERF_INDEX)
  2857. block_io_dbg_Init();
  2858. #endif
  2859. return 0;
  2860. out2:
  2861. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2862. out:
  2863. return res;
  2864. }
  2865. static void __exit mmc_blk_exit(void)
  2866. {
  2867. mmc_unregister_driver(&mmc_driver);
  2868. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2869. #if defined(FEATURE_STORAGE_PERF_INDEX)
  2870. block_io_dbg_deinit();
  2871. #endif
  2872. }
  2873. module_init(mmc_blk_init);
  2874. module_exit(mmc_blk_exit);
  2875. MODULE_LICENSE("GPL");
  2876. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");