block.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  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/cdev.h>
  31. #include <linux/mutex.h>
  32. #include <linux/scatterlist.h>
  33. #include <linux/string_helpers.h>
  34. #include <linux/delay.h>
  35. #include <linux/capability.h>
  36. #include <linux/compat.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/idr.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/mmc/ioctl.h>
  41. #include <linux/mmc/card.h>
  42. #include <linux/mmc/host.h>
  43. #include <linux/mmc/mmc.h>
  44. #include <linux/mmc/sd.h>
  45. #include <linux/uaccess.h>
  46. #include "queue.h"
  47. #include "block.h"
  48. #include "core.h"
  49. #include "card.h"
  50. #include "host.h"
  51. #include "bus.h"
  52. #include "mmc_ops.h"
  53. #include "quirks.h"
  54. #include "sd_ops.h"
  55. MODULE_ALIAS("mmc:block");
  56. #ifdef MODULE_PARAM_PREFIX
  57. #undef MODULE_PARAM_PREFIX
  58. #endif
  59. #define MODULE_PARAM_PREFIX "mmcblk."
  60. #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  61. #define MMC_SANITIZE_REQ_TIMEOUT 240000
  62. #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
  63. #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
  64. (rq_data_dir(req) == WRITE))
  65. static DEFINE_MUTEX(block_mutex);
  66. /*
  67. * The defaults come from config options but can be overriden by module
  68. * or bootarg options.
  69. */
  70. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  71. /*
  72. * We've only got one major, so number of mmcblk devices is
  73. * limited to (1 << 20) / number of minors per device. It is also
  74. * limited by the MAX_DEVICES below.
  75. */
  76. static int max_devices;
  77. #define MAX_DEVICES 256
  78. static DEFINE_IDA(mmc_blk_ida);
  79. static DEFINE_IDA(mmc_rpmb_ida);
  80. /*
  81. * There is one mmc_blk_data per slot.
  82. */
  83. struct mmc_blk_data {
  84. spinlock_t lock;
  85. struct device *parent;
  86. struct gendisk *disk;
  87. struct mmc_queue queue;
  88. struct list_head part;
  89. struct list_head rpmbs;
  90. unsigned int flags;
  91. #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
  92. #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
  93. unsigned int usage;
  94. unsigned int read_only;
  95. unsigned int part_type;
  96. unsigned int reset_done;
  97. #define MMC_BLK_READ BIT(0)
  98. #define MMC_BLK_WRITE BIT(1)
  99. #define MMC_BLK_DISCARD BIT(2)
  100. #define MMC_BLK_SECDISCARD BIT(3)
  101. /*
  102. * Only set in main mmc_blk_data associated
  103. * with mmc_card with dev_set_drvdata, and keeps
  104. * track of the current selected device partition.
  105. */
  106. unsigned int part_curr;
  107. struct device_attribute force_ro;
  108. struct device_attribute power_ro_lock;
  109. int area_type;
  110. /* debugfs files (only in main mmc_blk_data) */
  111. struct dentry *status_dentry;
  112. struct dentry *ext_csd_dentry;
  113. };
  114. /* Device type for RPMB character devices */
  115. static dev_t mmc_rpmb_devt;
  116. /* Bus type for RPMB character devices */
  117. static struct bus_type mmc_rpmb_bus_type = {
  118. .name = "mmc_rpmb",
  119. };
  120. /**
  121. * struct mmc_rpmb_data - special RPMB device type for these areas
  122. * @dev: the device for the RPMB area
  123. * @chrdev: character device for the RPMB area
  124. * @id: unique device ID number
  125. * @part_index: partition index (0 on first)
  126. * @md: parent MMC block device
  127. * @node: list item, so we can put this device on a list
  128. */
  129. struct mmc_rpmb_data {
  130. struct device dev;
  131. struct cdev chrdev;
  132. int id;
  133. unsigned int part_index;
  134. struct mmc_blk_data *md;
  135. struct list_head node;
  136. };
  137. static DEFINE_MUTEX(open_lock);
  138. module_param(perdev_minors, int, 0444);
  139. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  140. static inline int mmc_blk_part_switch(struct mmc_card *card,
  141. unsigned int part_type);
  142. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  143. {
  144. struct mmc_blk_data *md;
  145. mutex_lock(&open_lock);
  146. md = disk->private_data;
  147. if (md && md->usage == 0)
  148. md = NULL;
  149. if (md)
  150. md->usage++;
  151. mutex_unlock(&open_lock);
  152. return md;
  153. }
  154. static inline int mmc_get_devidx(struct gendisk *disk)
  155. {
  156. int devidx = disk->first_minor / perdev_minors;
  157. return devidx;
  158. }
  159. static void mmc_blk_put(struct mmc_blk_data *md)
  160. {
  161. mutex_lock(&open_lock);
  162. md->usage--;
  163. if (md->usage == 0) {
  164. int devidx = mmc_get_devidx(md->disk);
  165. blk_put_queue(md->queue.queue);
  166. ida_simple_remove(&mmc_blk_ida, devidx);
  167. put_disk(md->disk);
  168. kfree(md);
  169. }
  170. mutex_unlock(&open_lock);
  171. }
  172. static ssize_t power_ro_lock_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. int ret;
  176. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  177. struct mmc_card *card = md->queue.card;
  178. int locked = 0;
  179. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
  180. locked = 2;
  181. else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
  182. locked = 1;
  183. ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
  184. mmc_blk_put(md);
  185. return ret;
  186. }
  187. static ssize_t power_ro_lock_store(struct device *dev,
  188. struct device_attribute *attr, const char *buf, size_t count)
  189. {
  190. int ret;
  191. struct mmc_blk_data *md, *part_md;
  192. struct mmc_queue *mq;
  193. struct request *req;
  194. unsigned long set;
  195. if (kstrtoul(buf, 0, &set))
  196. return -EINVAL;
  197. if (set != 1)
  198. return count;
  199. md = mmc_blk_get(dev_to_disk(dev));
  200. mq = &md->queue;
  201. /* Dispatch locking to the block layer */
  202. req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
  203. if (IS_ERR(req)) {
  204. count = PTR_ERR(req);
  205. goto out_put;
  206. }
  207. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
  208. blk_execute_rq(mq->queue, NULL, req, 0);
  209. ret = req_to_mmc_queue_req(req)->drv_op_result;
  210. blk_put_request(req);
  211. if (!ret) {
  212. pr_info("%s: Locking boot partition ro until next power on\n",
  213. md->disk->disk_name);
  214. set_disk_ro(md->disk, 1);
  215. list_for_each_entry(part_md, &md->part, part)
  216. if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
  217. pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
  218. set_disk_ro(part_md->disk, 1);
  219. }
  220. }
  221. out_put:
  222. mmc_blk_put(md);
  223. return count;
  224. }
  225. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  226. char *buf)
  227. {
  228. int ret;
  229. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  230. ret = snprintf(buf, PAGE_SIZE, "%d\n",
  231. get_disk_ro(dev_to_disk(dev)) ^
  232. md->read_only);
  233. mmc_blk_put(md);
  234. return ret;
  235. }
  236. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  237. const char *buf, size_t count)
  238. {
  239. int ret;
  240. char *end;
  241. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  242. unsigned long set = simple_strtoul(buf, &end, 0);
  243. if (end == buf) {
  244. ret = -EINVAL;
  245. goto out;
  246. }
  247. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  248. ret = count;
  249. out:
  250. mmc_blk_put(md);
  251. return ret;
  252. }
  253. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  254. {
  255. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  256. int ret = -ENXIO;
  257. mutex_lock(&block_mutex);
  258. if (md) {
  259. if (md->usage == 2)
  260. check_disk_change(bdev);
  261. ret = 0;
  262. if ((mode & FMODE_WRITE) && md->read_only) {
  263. mmc_blk_put(md);
  264. ret = -EROFS;
  265. }
  266. }
  267. mutex_unlock(&block_mutex);
  268. return ret;
  269. }
  270. static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
  271. {
  272. struct mmc_blk_data *md = disk->private_data;
  273. mutex_lock(&block_mutex);
  274. mmc_blk_put(md);
  275. mutex_unlock(&block_mutex);
  276. }
  277. static int
  278. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  279. {
  280. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  281. geo->heads = 4;
  282. geo->sectors = 16;
  283. return 0;
  284. }
  285. struct mmc_blk_ioc_data {
  286. struct mmc_ioc_cmd ic;
  287. unsigned char *buf;
  288. u64 buf_bytes;
  289. struct mmc_rpmb_data *rpmb;
  290. };
  291. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  292. struct mmc_ioc_cmd __user *user)
  293. {
  294. struct mmc_blk_ioc_data *idata;
  295. int err;
  296. idata = kmalloc(sizeof(*idata), GFP_KERNEL);
  297. if (!idata) {
  298. err = -ENOMEM;
  299. goto out;
  300. }
  301. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  302. err = -EFAULT;
  303. goto idata_err;
  304. }
  305. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  306. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  307. err = -EOVERFLOW;
  308. goto idata_err;
  309. }
  310. if (!idata->buf_bytes) {
  311. idata->buf = NULL;
  312. return idata;
  313. }
  314. idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
  315. if (!idata->buf) {
  316. err = -ENOMEM;
  317. goto idata_err;
  318. }
  319. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  320. idata->ic.data_ptr, idata->buf_bytes)) {
  321. err = -EFAULT;
  322. goto copy_err;
  323. }
  324. return idata;
  325. copy_err:
  326. kfree(idata->buf);
  327. idata_err:
  328. kfree(idata);
  329. out:
  330. return ERR_PTR(err);
  331. }
  332. static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
  333. struct mmc_blk_ioc_data *idata)
  334. {
  335. struct mmc_ioc_cmd *ic = &idata->ic;
  336. if (copy_to_user(&(ic_ptr->response), ic->response,
  337. sizeof(ic->response)))
  338. return -EFAULT;
  339. if (!idata->ic.write_flag) {
  340. if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
  341. idata->buf, idata->buf_bytes))
  342. return -EFAULT;
  343. }
  344. return 0;
  345. }
  346. static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
  347. u32 retries_max)
  348. {
  349. int err;
  350. u32 retry_count = 0;
  351. if (!status || !retries_max)
  352. return -EINVAL;
  353. do {
  354. err = __mmc_send_status(card, status, 5);
  355. if (err)
  356. break;
  357. if (!R1_STATUS(*status) &&
  358. (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
  359. break; /* RPMB programming operation complete */
  360. /*
  361. * Rechedule to give the MMC device a chance to continue
  362. * processing the previous command without being polled too
  363. * frequently.
  364. */
  365. usleep_range(1000, 5000);
  366. } while (++retry_count < retries_max);
  367. if (retry_count == retries_max)
  368. err = -EPERM;
  369. return err;
  370. }
  371. static int ioctl_do_sanitize(struct mmc_card *card)
  372. {
  373. int err;
  374. if (!mmc_can_sanitize(card)) {
  375. pr_warn("%s: %s - SANITIZE is not supported\n",
  376. mmc_hostname(card->host), __func__);
  377. err = -EOPNOTSUPP;
  378. goto out;
  379. }
  380. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  381. mmc_hostname(card->host), __func__);
  382. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  383. EXT_CSD_SANITIZE_START, 1,
  384. MMC_SANITIZE_REQ_TIMEOUT);
  385. if (err)
  386. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  387. mmc_hostname(card->host), __func__, err);
  388. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  389. __func__);
  390. out:
  391. return err;
  392. }
  393. static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
  394. struct mmc_blk_ioc_data *idata)
  395. {
  396. struct mmc_command cmd = {};
  397. struct mmc_data data = {};
  398. struct mmc_request mrq = {};
  399. struct scatterlist sg;
  400. int err;
  401. unsigned int target_part;
  402. u32 status = 0;
  403. if (!card || !md || !idata)
  404. return -EINVAL;
  405. /*
  406. * The RPMB accesses comes in from the character device, so we
  407. * need to target these explicitly. Else we just target the
  408. * partition type for the block device the ioctl() was issued
  409. * on.
  410. */
  411. if (idata->rpmb) {
  412. /* Support multiple RPMB partitions */
  413. target_part = idata->rpmb->part_index;
  414. target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
  415. } else {
  416. target_part = md->part_type;
  417. }
  418. cmd.opcode = idata->ic.opcode;
  419. cmd.arg = idata->ic.arg;
  420. cmd.flags = idata->ic.flags;
  421. if (idata->buf_bytes) {
  422. data.sg = &sg;
  423. data.sg_len = 1;
  424. data.blksz = idata->ic.blksz;
  425. data.blocks = idata->ic.blocks;
  426. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  427. if (idata->ic.write_flag)
  428. data.flags = MMC_DATA_WRITE;
  429. else
  430. data.flags = MMC_DATA_READ;
  431. /* data.flags must already be set before doing this. */
  432. mmc_set_data_timeout(&data, card);
  433. /* Allow overriding the timeout_ns for empirical tuning. */
  434. if (idata->ic.data_timeout_ns)
  435. data.timeout_ns = idata->ic.data_timeout_ns;
  436. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  437. /*
  438. * Pretend this is a data transfer and rely on the
  439. * host driver to compute timeout. When all host
  440. * drivers support cmd.cmd_timeout for R1B, this
  441. * can be changed to:
  442. *
  443. * mrq.data = NULL;
  444. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  445. */
  446. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  447. }
  448. mrq.data = &data;
  449. }
  450. mrq.cmd = &cmd;
  451. err = mmc_blk_part_switch(card, target_part);
  452. if (err)
  453. return err;
  454. if (idata->ic.is_acmd) {
  455. err = mmc_app_cmd(card->host, card);
  456. if (err)
  457. return err;
  458. }
  459. if (idata->rpmb) {
  460. err = mmc_set_blockcount(card, data.blocks,
  461. idata->ic.write_flag & (1 << 31));
  462. if (err)
  463. return err;
  464. }
  465. if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
  466. (cmd.opcode == MMC_SWITCH)) {
  467. err = ioctl_do_sanitize(card);
  468. if (err)
  469. pr_err("%s: ioctl_do_sanitize() failed. err = %d",
  470. __func__, err);
  471. return err;
  472. }
  473. mmc_wait_for_req(card->host, &mrq);
  474. if (cmd.error) {
  475. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  476. __func__, cmd.error);
  477. return cmd.error;
  478. }
  479. if (data.error) {
  480. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  481. __func__, data.error);
  482. return data.error;
  483. }
  484. /*
  485. * According to the SD specs, some commands require a delay after
  486. * issuing the command.
  487. */
  488. if (idata->ic.postsleep_min_us)
  489. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  490. memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
  491. if (idata->rpmb) {
  492. /*
  493. * Ensure RPMB command has completed by polling CMD13
  494. * "Send Status".
  495. */
  496. err = ioctl_rpmb_card_status_poll(card, &status, 5);
  497. if (err)
  498. dev_err(mmc_dev(card->host),
  499. "%s: Card Status=0x%08X, error %d\n",
  500. __func__, status, err);
  501. }
  502. return err;
  503. }
  504. static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
  505. struct mmc_ioc_cmd __user *ic_ptr,
  506. struct mmc_rpmb_data *rpmb)
  507. {
  508. struct mmc_blk_ioc_data *idata;
  509. struct mmc_blk_ioc_data *idatas[1];
  510. struct mmc_queue *mq;
  511. struct mmc_card *card;
  512. int err = 0, ioc_err = 0;
  513. struct request *req;
  514. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  515. if (IS_ERR(idata))
  516. return PTR_ERR(idata);
  517. /* This will be NULL on non-RPMB ioctl():s */
  518. idata->rpmb = rpmb;
  519. card = md->queue.card;
  520. if (IS_ERR(card)) {
  521. err = PTR_ERR(card);
  522. goto cmd_done;
  523. }
  524. /*
  525. * Dispatch the ioctl() into the block request queue.
  526. */
  527. mq = &md->queue;
  528. req = blk_get_request(mq->queue,
  529. idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
  530. __GFP_RECLAIM);
  531. if (IS_ERR(req)) {
  532. err = PTR_ERR(req);
  533. goto cmd_done;
  534. }
  535. idatas[0] = idata;
  536. req_to_mmc_queue_req(req)->drv_op =
  537. rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
  538. req_to_mmc_queue_req(req)->drv_op_data = idatas;
  539. req_to_mmc_queue_req(req)->ioc_count = 1;
  540. blk_execute_rq(mq->queue, NULL, req, 0);
  541. ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
  542. err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
  543. blk_put_request(req);
  544. cmd_done:
  545. kfree(idata->buf);
  546. kfree(idata);
  547. return ioc_err ? ioc_err : err;
  548. }
  549. static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
  550. struct mmc_ioc_multi_cmd __user *user,
  551. struct mmc_rpmb_data *rpmb)
  552. {
  553. struct mmc_blk_ioc_data **idata = NULL;
  554. struct mmc_ioc_cmd __user *cmds = user->cmds;
  555. struct mmc_card *card;
  556. struct mmc_queue *mq;
  557. int i, err = 0, ioc_err = 0;
  558. __u64 num_of_cmds;
  559. struct request *req;
  560. if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
  561. sizeof(num_of_cmds)))
  562. return -EFAULT;
  563. if (!num_of_cmds)
  564. return 0;
  565. if (num_of_cmds > MMC_IOC_MAX_CMDS)
  566. return -EINVAL;
  567. idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
  568. if (!idata)
  569. return -ENOMEM;
  570. for (i = 0; i < num_of_cmds; i++) {
  571. idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
  572. if (IS_ERR(idata[i])) {
  573. err = PTR_ERR(idata[i]);
  574. num_of_cmds = i;
  575. goto cmd_err;
  576. }
  577. /* This will be NULL on non-RPMB ioctl():s */
  578. idata[i]->rpmb = rpmb;
  579. }
  580. card = md->queue.card;
  581. if (IS_ERR(card)) {
  582. err = PTR_ERR(card);
  583. goto cmd_err;
  584. }
  585. /*
  586. * Dispatch the ioctl()s into the block request queue.
  587. */
  588. mq = &md->queue;
  589. req = blk_get_request(mq->queue,
  590. idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
  591. __GFP_RECLAIM);
  592. if (IS_ERR(req)) {
  593. err = PTR_ERR(req);
  594. goto cmd_err;
  595. }
  596. req_to_mmc_queue_req(req)->drv_op =
  597. rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
  598. req_to_mmc_queue_req(req)->drv_op_data = idata;
  599. req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
  600. blk_execute_rq(mq->queue, NULL, req, 0);
  601. ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
  602. /* copy to user if data and response */
  603. for (i = 0; i < num_of_cmds && !err; i++)
  604. err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
  605. blk_put_request(req);
  606. cmd_err:
  607. for (i = 0; i < num_of_cmds; i++) {
  608. kfree(idata[i]->buf);
  609. kfree(idata[i]);
  610. }
  611. kfree(idata);
  612. return ioc_err ? ioc_err : err;
  613. }
  614. static int mmc_blk_check_blkdev(struct block_device *bdev)
  615. {
  616. /*
  617. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  618. * whole block device, not on a partition. This prevents overspray
  619. * between sibling partitions.
  620. */
  621. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  622. return -EPERM;
  623. return 0;
  624. }
  625. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  626. unsigned int cmd, unsigned long arg)
  627. {
  628. struct mmc_blk_data *md;
  629. int ret;
  630. switch (cmd) {
  631. case MMC_IOC_CMD:
  632. ret = mmc_blk_check_blkdev(bdev);
  633. if (ret)
  634. return ret;
  635. md = mmc_blk_get(bdev->bd_disk);
  636. if (!md)
  637. return -EINVAL;
  638. ret = mmc_blk_ioctl_cmd(md,
  639. (struct mmc_ioc_cmd __user *)arg,
  640. NULL);
  641. mmc_blk_put(md);
  642. return ret;
  643. case MMC_IOC_MULTI_CMD:
  644. ret = mmc_blk_check_blkdev(bdev);
  645. if (ret)
  646. return ret;
  647. md = mmc_blk_get(bdev->bd_disk);
  648. if (!md)
  649. return -EINVAL;
  650. ret = mmc_blk_ioctl_multi_cmd(md,
  651. (struct mmc_ioc_multi_cmd __user *)arg,
  652. NULL);
  653. mmc_blk_put(md);
  654. return ret;
  655. default:
  656. return -EINVAL;
  657. }
  658. }
  659. #ifdef CONFIG_COMPAT
  660. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  661. unsigned int cmd, unsigned long arg)
  662. {
  663. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  664. }
  665. #endif
  666. static const struct block_device_operations mmc_bdops = {
  667. .open = mmc_blk_open,
  668. .release = mmc_blk_release,
  669. .getgeo = mmc_blk_getgeo,
  670. .owner = THIS_MODULE,
  671. .ioctl = mmc_blk_ioctl,
  672. #ifdef CONFIG_COMPAT
  673. .compat_ioctl = mmc_blk_compat_ioctl,
  674. #endif
  675. };
  676. static int mmc_blk_part_switch_pre(struct mmc_card *card,
  677. unsigned int part_type)
  678. {
  679. int ret = 0;
  680. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  681. if (card->ext_csd.cmdq_en) {
  682. ret = mmc_cmdq_disable(card);
  683. if (ret)
  684. return ret;
  685. }
  686. mmc_retune_pause(card->host);
  687. }
  688. return ret;
  689. }
  690. static int mmc_blk_part_switch_post(struct mmc_card *card,
  691. unsigned int part_type)
  692. {
  693. int ret = 0;
  694. if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
  695. mmc_retune_unpause(card->host);
  696. if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
  697. ret = mmc_cmdq_enable(card);
  698. }
  699. return ret;
  700. }
  701. static inline int mmc_blk_part_switch(struct mmc_card *card,
  702. unsigned int part_type)
  703. {
  704. int ret = 0;
  705. struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
  706. if (main_md->part_curr == part_type)
  707. return 0;
  708. if (mmc_card_mmc(card)) {
  709. u8 part_config = card->ext_csd.part_config;
  710. ret = mmc_blk_part_switch_pre(card, part_type);
  711. if (ret)
  712. return ret;
  713. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  714. part_config |= part_type;
  715. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  716. EXT_CSD_PART_CONFIG, part_config,
  717. card->ext_csd.part_time);
  718. if (ret) {
  719. mmc_blk_part_switch_post(card, part_type);
  720. return ret;
  721. }
  722. card->ext_csd.part_config = part_config;
  723. ret = mmc_blk_part_switch_post(card, main_md->part_curr);
  724. }
  725. main_md->part_curr = part_type;
  726. return ret;
  727. }
  728. static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
  729. {
  730. int err;
  731. u32 result;
  732. __be32 *blocks;
  733. struct mmc_request mrq = {};
  734. struct mmc_command cmd = {};
  735. struct mmc_data data = {};
  736. struct scatterlist sg;
  737. cmd.opcode = MMC_APP_CMD;
  738. cmd.arg = card->rca << 16;
  739. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  740. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  741. if (err)
  742. return err;
  743. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  744. return -EIO;
  745. memset(&cmd, 0, sizeof(struct mmc_command));
  746. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  747. cmd.arg = 0;
  748. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  749. data.blksz = 4;
  750. data.blocks = 1;
  751. data.flags = MMC_DATA_READ;
  752. data.sg = &sg;
  753. data.sg_len = 1;
  754. mmc_set_data_timeout(&data, card);
  755. mrq.cmd = &cmd;
  756. mrq.data = &data;
  757. blocks = kmalloc(4, GFP_KERNEL);
  758. if (!blocks)
  759. return -ENOMEM;
  760. sg_init_one(&sg, blocks, 4);
  761. mmc_wait_for_req(card->host, &mrq);
  762. result = ntohl(*blocks);
  763. kfree(blocks);
  764. if (cmd.error || data.error)
  765. return -EIO;
  766. *written_blocks = result;
  767. return 0;
  768. }
  769. static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
  770. bool hw_busy_detect, struct request *req, bool *gen_err)
  771. {
  772. unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
  773. int err = 0;
  774. u32 status;
  775. do {
  776. err = __mmc_send_status(card, &status, 5);
  777. if (err) {
  778. pr_err("%s: error %d requesting status\n",
  779. req->rq_disk->disk_name, err);
  780. return err;
  781. }
  782. if (status & R1_ERROR) {
  783. pr_err("%s: %s: error sending status cmd, status %#x\n",
  784. req->rq_disk->disk_name, __func__, status);
  785. *gen_err = true;
  786. }
  787. /* We may rely on the host hw to handle busy detection.*/
  788. if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
  789. hw_busy_detect)
  790. break;
  791. /*
  792. * Timeout if the device never becomes ready for data and never
  793. * leaves the program state.
  794. */
  795. if (time_after(jiffies, timeout)) {
  796. pr_err("%s: Card stuck in programming state! %s %s\n",
  797. mmc_hostname(card->host),
  798. req->rq_disk->disk_name, __func__);
  799. return -ETIMEDOUT;
  800. }
  801. /*
  802. * Some cards mishandle the status bits,
  803. * so make sure to check both the busy
  804. * indication and the card state.
  805. */
  806. } while (!(status & R1_READY_FOR_DATA) ||
  807. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  808. return err;
  809. }
  810. static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
  811. struct request *req, bool *gen_err, u32 *stop_status)
  812. {
  813. struct mmc_host *host = card->host;
  814. struct mmc_command cmd = {};
  815. int err;
  816. bool use_r1b_resp = rq_data_dir(req) == WRITE;
  817. /*
  818. * Normally we use R1B responses for WRITE, but in cases where the host
  819. * has specified a max_busy_timeout we need to validate it. A failure
  820. * means we need to prevent the host from doing hw busy detection, which
  821. * is done by converting to a R1 response instead.
  822. */
  823. if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
  824. use_r1b_resp = false;
  825. cmd.opcode = MMC_STOP_TRANSMISSION;
  826. if (use_r1b_resp) {
  827. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  828. cmd.busy_timeout = timeout_ms;
  829. } else {
  830. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  831. }
  832. err = mmc_wait_for_cmd(host, &cmd, 5);
  833. if (err)
  834. return err;
  835. *stop_status = cmd.resp[0];
  836. /* No need to check card status in case of READ. */
  837. if (rq_data_dir(req) == READ)
  838. return 0;
  839. if (!mmc_host_is_spi(host) &&
  840. (*stop_status & R1_ERROR)) {
  841. pr_err("%s: %s: general error sending stop command, resp %#x\n",
  842. req->rq_disk->disk_name, __func__, *stop_status);
  843. *gen_err = true;
  844. }
  845. return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
  846. }
  847. #define ERR_NOMEDIUM 3
  848. #define ERR_RETRY 2
  849. #define ERR_ABORT 1
  850. #define ERR_CONTINUE 0
  851. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  852. bool status_valid, u32 status)
  853. {
  854. switch (error) {
  855. case -EILSEQ:
  856. /* response crc error, retry the r/w cmd */
  857. pr_err("%s: %s sending %s command, card status %#x\n",
  858. req->rq_disk->disk_name, "response CRC error",
  859. name, status);
  860. return ERR_RETRY;
  861. case -ETIMEDOUT:
  862. pr_err("%s: %s sending %s command, card status %#x\n",
  863. req->rq_disk->disk_name, "timed out", name, status);
  864. /* If the status cmd initially failed, retry the r/w cmd */
  865. if (!status_valid) {
  866. pr_err("%s: status not valid, retrying timeout\n",
  867. req->rq_disk->disk_name);
  868. return ERR_RETRY;
  869. }
  870. /*
  871. * If it was a r/w cmd crc error, or illegal command
  872. * (eg, issued in wrong state) then retry - we should
  873. * have corrected the state problem above.
  874. */
  875. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
  876. pr_err("%s: command error, retrying timeout\n",
  877. req->rq_disk->disk_name);
  878. return ERR_RETRY;
  879. }
  880. /* Otherwise abort the command */
  881. return ERR_ABORT;
  882. default:
  883. /* We don't understand the error code the driver gave us */
  884. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  885. req->rq_disk->disk_name, error, status);
  886. return ERR_ABORT;
  887. }
  888. }
  889. /*
  890. * Initial r/w and stop cmd error recovery.
  891. * We don't know whether the card received the r/w cmd or not, so try to
  892. * restore things back to a sane state. Essentially, we do this as follows:
  893. * - Obtain card status. If the first attempt to obtain card status fails,
  894. * the status word will reflect the failed status cmd, not the failed
  895. * r/w cmd. If we fail to obtain card status, it suggests we can no
  896. * longer communicate with the card.
  897. * - Check the card state. If the card received the cmd but there was a
  898. * transient problem with the response, it might still be in a data transfer
  899. * mode. Try to send it a stop command. If this fails, we can't recover.
  900. * - If the r/w cmd failed due to a response CRC error, it was probably
  901. * transient, so retry the cmd.
  902. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  903. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  904. * illegal cmd, retry.
  905. * Otherwise we don't understand what happened, so abort.
  906. */
  907. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  908. struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
  909. {
  910. bool prev_cmd_status_valid = true;
  911. u32 status, stop_status = 0;
  912. int err, retry;
  913. if (mmc_card_removed(card))
  914. return ERR_NOMEDIUM;
  915. /*
  916. * Try to get card status which indicates both the card state
  917. * and why there was no response. If the first attempt fails,
  918. * we can't be sure the returned status is for the r/w command.
  919. */
  920. for (retry = 2; retry >= 0; retry--) {
  921. err = __mmc_send_status(card, &status, 0);
  922. if (!err)
  923. break;
  924. /* Re-tune if needed */
  925. mmc_retune_recheck(card->host);
  926. prev_cmd_status_valid = false;
  927. pr_err("%s: error %d sending status command, %sing\n",
  928. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  929. }
  930. /* We couldn't get a response from the card. Give up. */
  931. if (err) {
  932. /* Check if the card is removed */
  933. if (mmc_detect_card_removed(card->host))
  934. return ERR_NOMEDIUM;
  935. return ERR_ABORT;
  936. }
  937. /* Flag ECC errors */
  938. if ((status & R1_CARD_ECC_FAILED) ||
  939. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  940. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  941. *ecc_err = true;
  942. /* Flag General errors */
  943. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
  944. if ((status & R1_ERROR) ||
  945. (brq->stop.resp[0] & R1_ERROR)) {
  946. pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
  947. req->rq_disk->disk_name, __func__,
  948. brq->stop.resp[0], status);
  949. *gen_err = true;
  950. }
  951. /*
  952. * Check the current card state. If it is in some data transfer
  953. * mode, tell it to stop (and hopefully transition back to TRAN.)
  954. */
  955. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  956. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  957. err = send_stop(card,
  958. DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
  959. req, gen_err, &stop_status);
  960. if (err) {
  961. pr_err("%s: error %d sending stop command\n",
  962. req->rq_disk->disk_name, err);
  963. /*
  964. * If the stop cmd also timed out, the card is probably
  965. * not present, so abort. Other errors are bad news too.
  966. */
  967. return ERR_ABORT;
  968. }
  969. if (stop_status & R1_CARD_ECC_FAILED)
  970. *ecc_err = true;
  971. }
  972. /* Check for set block count errors */
  973. if (brq->sbc.error)
  974. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  975. prev_cmd_status_valid, status);
  976. /* Check for r/w command errors */
  977. if (brq->cmd.error)
  978. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  979. prev_cmd_status_valid, status);
  980. /* Data errors */
  981. if (!brq->stop.error)
  982. return ERR_CONTINUE;
  983. /* Now for stop errors. These aren't fatal to the transfer. */
  984. pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  985. req->rq_disk->disk_name, brq->stop.error,
  986. brq->cmd.resp[0], status);
  987. /*
  988. * Subsitute in our own stop status as this will give the error
  989. * state which happened during the execution of the r/w command.
  990. */
  991. if (stop_status) {
  992. brq->stop.resp[0] = stop_status;
  993. brq->stop.error = 0;
  994. }
  995. return ERR_CONTINUE;
  996. }
  997. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  998. int type)
  999. {
  1000. int err;
  1001. if (md->reset_done & type)
  1002. return -EEXIST;
  1003. md->reset_done |= type;
  1004. err = mmc_hw_reset(host);
  1005. /* Ensure we switch back to the correct partition */
  1006. if (err != -EOPNOTSUPP) {
  1007. struct mmc_blk_data *main_md =
  1008. dev_get_drvdata(&host->card->dev);
  1009. int part_err;
  1010. main_md->part_curr = main_md->part_type;
  1011. part_err = mmc_blk_part_switch(host->card, md->part_type);
  1012. if (part_err) {
  1013. /*
  1014. * We have failed to get back into the correct
  1015. * partition, so we need to abort the whole request.
  1016. */
  1017. return -ENODEV;
  1018. }
  1019. }
  1020. return err;
  1021. }
  1022. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  1023. {
  1024. md->reset_done &= ~type;
  1025. }
  1026. /*
  1027. * The non-block commands come back from the block layer after it queued it and
  1028. * processed it with all other requests and then they get issued in this
  1029. * function.
  1030. */
  1031. static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
  1032. {
  1033. struct mmc_queue_req *mq_rq;
  1034. struct mmc_card *card = mq->card;
  1035. struct mmc_blk_data *md = mq->blkdata;
  1036. struct mmc_blk_ioc_data **idata;
  1037. bool rpmb_ioctl;
  1038. u8 **ext_csd;
  1039. u32 status;
  1040. int ret;
  1041. int i;
  1042. mq_rq = req_to_mmc_queue_req(req);
  1043. rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
  1044. switch (mq_rq->drv_op) {
  1045. case MMC_DRV_OP_IOCTL:
  1046. case MMC_DRV_OP_IOCTL_RPMB:
  1047. idata = mq_rq->drv_op_data;
  1048. for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
  1049. ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
  1050. if (ret)
  1051. break;
  1052. }
  1053. /* Always switch back to main area after RPMB access */
  1054. if (rpmb_ioctl)
  1055. mmc_blk_part_switch(card, 0);
  1056. break;
  1057. case MMC_DRV_OP_BOOT_WP:
  1058. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
  1059. card->ext_csd.boot_ro_lock |
  1060. EXT_CSD_BOOT_WP_B_PWR_WP_EN,
  1061. card->ext_csd.part_time);
  1062. if (ret)
  1063. pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
  1064. md->disk->disk_name, ret);
  1065. else
  1066. card->ext_csd.boot_ro_lock |=
  1067. EXT_CSD_BOOT_WP_B_PWR_WP_EN;
  1068. break;
  1069. case MMC_DRV_OP_GET_CARD_STATUS:
  1070. ret = mmc_send_status(card, &status);
  1071. if (!ret)
  1072. ret = status;
  1073. break;
  1074. case MMC_DRV_OP_GET_EXT_CSD:
  1075. ext_csd = mq_rq->drv_op_data;
  1076. ret = mmc_get_ext_csd(card, ext_csd);
  1077. break;
  1078. default:
  1079. pr_err("%s: unknown driver specific operation\n",
  1080. md->disk->disk_name);
  1081. ret = -EINVAL;
  1082. break;
  1083. }
  1084. mq_rq->drv_op_result = ret;
  1085. blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
  1086. }
  1087. static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  1088. {
  1089. struct mmc_blk_data *md = mq->blkdata;
  1090. struct mmc_card *card = md->queue.card;
  1091. unsigned int from, nr, arg;
  1092. int err = 0, type = MMC_BLK_DISCARD;
  1093. blk_status_t status = BLK_STS_OK;
  1094. if (!mmc_can_erase(card)) {
  1095. status = BLK_STS_NOTSUPP;
  1096. goto fail;
  1097. }
  1098. from = blk_rq_pos(req);
  1099. nr = blk_rq_sectors(req);
  1100. if (mmc_can_discard(card))
  1101. arg = MMC_DISCARD_ARG;
  1102. else if (mmc_can_trim(card))
  1103. arg = MMC_TRIM_ARG;
  1104. else
  1105. arg = MMC_ERASE_ARG;
  1106. do {
  1107. err = 0;
  1108. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1109. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1110. INAND_CMD38_ARG_EXT_CSD,
  1111. arg == MMC_TRIM_ARG ?
  1112. INAND_CMD38_ARG_TRIM :
  1113. INAND_CMD38_ARG_ERASE,
  1114. 0);
  1115. }
  1116. if (!err)
  1117. err = mmc_erase(card, from, nr, arg);
  1118. } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
  1119. if (err)
  1120. status = BLK_STS_IOERR;
  1121. else
  1122. mmc_blk_reset_success(md, type);
  1123. fail:
  1124. blk_end_request(req, status, blk_rq_bytes(req));
  1125. }
  1126. static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  1127. struct request *req)
  1128. {
  1129. struct mmc_blk_data *md = mq->blkdata;
  1130. struct mmc_card *card = md->queue.card;
  1131. unsigned int from, nr, arg;
  1132. int err = 0, type = MMC_BLK_SECDISCARD;
  1133. blk_status_t status = BLK_STS_OK;
  1134. if (!(mmc_can_secure_erase_trim(card))) {
  1135. status = BLK_STS_NOTSUPP;
  1136. goto out;
  1137. }
  1138. from = blk_rq_pos(req);
  1139. nr = blk_rq_sectors(req);
  1140. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  1141. arg = MMC_SECURE_TRIM1_ARG;
  1142. else
  1143. arg = MMC_SECURE_ERASE_ARG;
  1144. retry:
  1145. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1146. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1147. INAND_CMD38_ARG_EXT_CSD,
  1148. arg == MMC_SECURE_TRIM1_ARG ?
  1149. INAND_CMD38_ARG_SECTRIM1 :
  1150. INAND_CMD38_ARG_SECERASE,
  1151. 0);
  1152. if (err)
  1153. goto out_retry;
  1154. }
  1155. err = mmc_erase(card, from, nr, arg);
  1156. if (err == -EIO)
  1157. goto out_retry;
  1158. if (err) {
  1159. status = BLK_STS_IOERR;
  1160. goto out;
  1161. }
  1162. if (arg == MMC_SECURE_TRIM1_ARG) {
  1163. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  1164. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1165. INAND_CMD38_ARG_EXT_CSD,
  1166. INAND_CMD38_ARG_SECTRIM2,
  1167. 0);
  1168. if (err)
  1169. goto out_retry;
  1170. }
  1171. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  1172. if (err == -EIO)
  1173. goto out_retry;
  1174. if (err) {
  1175. status = BLK_STS_IOERR;
  1176. goto out;
  1177. }
  1178. }
  1179. out_retry:
  1180. if (err && !mmc_blk_reset(md, card->host, type))
  1181. goto retry;
  1182. if (!err)
  1183. mmc_blk_reset_success(md, type);
  1184. out:
  1185. blk_end_request(req, status, blk_rq_bytes(req));
  1186. }
  1187. static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  1188. {
  1189. struct mmc_blk_data *md = mq->blkdata;
  1190. struct mmc_card *card = md->queue.card;
  1191. int ret = 0;
  1192. ret = mmc_flush_cache(card);
  1193. blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
  1194. }
  1195. /*
  1196. * Reformat current write as a reliable write, supporting
  1197. * both legacy and the enhanced reliable write MMC cards.
  1198. * In each transfer we'll handle only as much as a single
  1199. * reliable write can handle, thus finish the request in
  1200. * partial completions.
  1201. */
  1202. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  1203. struct mmc_card *card,
  1204. struct request *req)
  1205. {
  1206. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  1207. /* Legacy mode imposes restrictions on transfers. */
  1208. if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
  1209. brq->data.blocks = 1;
  1210. if (brq->data.blocks > card->ext_csd.rel_sectors)
  1211. brq->data.blocks = card->ext_csd.rel_sectors;
  1212. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  1213. brq->data.blocks = 1;
  1214. }
  1215. }
  1216. #define CMD_ERRORS \
  1217. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  1218. R1_ADDRESS_ERROR | /* Misaligned address */ \
  1219. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  1220. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  1221. R1_CARD_ECC_FAILED | /* Card ECC failed */ \
  1222. R1_CC_ERROR | /* Card controller error */ \
  1223. R1_ERROR) /* General/unknown error */
  1224. static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
  1225. {
  1226. u32 val;
  1227. /*
  1228. * Per the SD specification(physical layer version 4.10)[1],
  1229. * section 4.3.3, it explicitly states that "When the last
  1230. * block of user area is read using CMD18, the host should
  1231. * ignore OUT_OF_RANGE error that may occur even the sequence
  1232. * is correct". And JESD84-B51 for eMMC also has a similar
  1233. * statement on section 6.8.3.
  1234. *
  1235. * Multiple block read/write could be done by either predefined
  1236. * method, namely CMD23, or open-ending mode. For open-ending mode,
  1237. * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
  1238. *
  1239. * However the spec[1] doesn't tell us whether we should also
  1240. * ignore that for predefined method. But per the spec[1], section
  1241. * 4.15 Set Block Count Command, it says"If illegal block count
  1242. * is set, out of range error will be indicated during read/write
  1243. * operation (For example, data transfer is stopped at user area
  1244. * boundary)." In another word, we could expect a out of range error
  1245. * in the response for the following CMD18/25. And if argument of
  1246. * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
  1247. * we could also expect to get a -ETIMEDOUT or any error number from
  1248. * the host drivers due to missing data response(for write)/data(for
  1249. * read), as the cards will stop the data transfer by itself per the
  1250. * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
  1251. */
  1252. if (!brq->stop.error) {
  1253. bool oor_with_open_end;
  1254. /* If there is no error yet, check R1 response */
  1255. val = brq->stop.resp[0] & CMD_ERRORS;
  1256. oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
  1257. if (val && !oor_with_open_end)
  1258. brq->stop.error = -EIO;
  1259. }
  1260. }
  1261. static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
  1262. struct mmc_async_req *areq)
  1263. {
  1264. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  1265. areq);
  1266. struct mmc_blk_request *brq = &mq_mrq->brq;
  1267. struct request *req = mmc_queue_req_to_req(mq_mrq);
  1268. int need_retune = card->host->need_retune;
  1269. bool ecc_err = false;
  1270. bool gen_err = false;
  1271. /*
  1272. * sbc.error indicates a problem with the set block count
  1273. * command. No data will have been transferred.
  1274. *
  1275. * cmd.error indicates a problem with the r/w command. No
  1276. * data will have been transferred.
  1277. *
  1278. * stop.error indicates a problem with the stop command. Data
  1279. * may have been transferred, or may still be transferring.
  1280. */
  1281. mmc_blk_eval_resp_error(brq);
  1282. if (brq->sbc.error || brq->cmd.error ||
  1283. brq->stop.error || brq->data.error) {
  1284. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
  1285. case ERR_RETRY:
  1286. return MMC_BLK_RETRY;
  1287. case ERR_ABORT:
  1288. return MMC_BLK_ABORT;
  1289. case ERR_NOMEDIUM:
  1290. return MMC_BLK_NOMEDIUM;
  1291. case ERR_CONTINUE:
  1292. break;
  1293. }
  1294. }
  1295. /*
  1296. * Check for errors relating to the execution of the
  1297. * initial command - such as address errors. No data
  1298. * has been transferred.
  1299. */
  1300. if (brq->cmd.resp[0] & CMD_ERRORS) {
  1301. pr_err("%s: r/w command failed, status = %#x\n",
  1302. req->rq_disk->disk_name, brq->cmd.resp[0]);
  1303. return MMC_BLK_ABORT;
  1304. }
  1305. /*
  1306. * Everything else is either success, or a data error of some
  1307. * kind. If it was a write, we may have transitioned to
  1308. * program mode, which we have to wait for it to complete.
  1309. */
  1310. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  1311. int err;
  1312. /* Check stop command response */
  1313. if (brq->stop.resp[0] & R1_ERROR) {
  1314. pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
  1315. req->rq_disk->disk_name, __func__,
  1316. brq->stop.resp[0]);
  1317. gen_err = true;
  1318. }
  1319. err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
  1320. &gen_err);
  1321. if (err)
  1322. return MMC_BLK_CMD_ERR;
  1323. }
  1324. /* if general error occurs, retry the write operation. */
  1325. if (gen_err) {
  1326. pr_warn("%s: retrying write for general error\n",
  1327. req->rq_disk->disk_name);
  1328. return MMC_BLK_RETRY;
  1329. }
  1330. /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
  1331. if (brq->data.error || brq->stop.error) {
  1332. if (need_retune && !brq->retune_retry_done) {
  1333. pr_debug("%s: retrying because a re-tune was needed\n",
  1334. req->rq_disk->disk_name);
  1335. brq->retune_retry_done = 1;
  1336. return MMC_BLK_RETRY;
  1337. }
  1338. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  1339. req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
  1340. (unsigned)blk_rq_pos(req),
  1341. (unsigned)blk_rq_sectors(req),
  1342. brq->cmd.resp[0], brq->stop.resp[0]);
  1343. if (rq_data_dir(req) == READ) {
  1344. if (ecc_err)
  1345. return MMC_BLK_ECC_ERR;
  1346. return MMC_BLK_DATA_ERR;
  1347. } else {
  1348. return MMC_BLK_CMD_ERR;
  1349. }
  1350. }
  1351. if (!brq->data.bytes_xfered)
  1352. return MMC_BLK_RETRY;
  1353. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1354. return MMC_BLK_PARTIAL;
  1355. return MMC_BLK_SUCCESS;
  1356. }
  1357. static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
  1358. int disable_multi, bool *do_rel_wr_p,
  1359. bool *do_data_tag_p)
  1360. {
  1361. struct mmc_blk_data *md = mq->blkdata;
  1362. struct mmc_card *card = md->queue.card;
  1363. struct mmc_blk_request *brq = &mqrq->brq;
  1364. struct request *req = mmc_queue_req_to_req(mqrq);
  1365. bool do_rel_wr, do_data_tag;
  1366. /*
  1367. * Reliable writes are used to implement Forced Unit Access and
  1368. * are supported only on MMCs.
  1369. */
  1370. do_rel_wr = (req->cmd_flags & REQ_FUA) &&
  1371. rq_data_dir(req) == WRITE &&
  1372. (md->flags & MMC_BLK_REL_WR);
  1373. memset(brq, 0, sizeof(struct mmc_blk_request));
  1374. brq->mrq.data = &brq->data;
  1375. brq->mrq.tag = req->tag;
  1376. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1377. brq->stop.arg = 0;
  1378. if (rq_data_dir(req) == READ) {
  1379. brq->data.flags = MMC_DATA_READ;
  1380. brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1381. } else {
  1382. brq->data.flags = MMC_DATA_WRITE;
  1383. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1384. }
  1385. brq->data.blksz = 512;
  1386. brq->data.blocks = blk_rq_sectors(req);
  1387. brq->data.blk_addr = blk_rq_pos(req);
  1388. /*
  1389. * The command queue supports 2 priorities: "high" (1) and "simple" (0).
  1390. * The eMMC will give "high" priority tasks priority over "simple"
  1391. * priority tasks. Here we always set "simple" priority by not setting
  1392. * MMC_DATA_PRIO.
  1393. */
  1394. /*
  1395. * The block layer doesn't support all sector count
  1396. * restrictions, so we need to be prepared for too big
  1397. * requests.
  1398. */
  1399. if (brq->data.blocks > card->host->max_blk_count)
  1400. brq->data.blocks = card->host->max_blk_count;
  1401. if (brq->data.blocks > 1) {
  1402. /*
  1403. * After a read error, we redo the request one sector
  1404. * at a time in order to accurately determine which
  1405. * sectors can be read successfully.
  1406. */
  1407. if (disable_multi)
  1408. brq->data.blocks = 1;
  1409. /*
  1410. * Some controllers have HW issues while operating
  1411. * in multiple I/O mode
  1412. */
  1413. if (card->host->ops->multi_io_quirk)
  1414. brq->data.blocks = card->host->ops->multi_io_quirk(card,
  1415. (rq_data_dir(req) == READ) ?
  1416. MMC_DATA_READ : MMC_DATA_WRITE,
  1417. brq->data.blocks);
  1418. }
  1419. if (do_rel_wr) {
  1420. mmc_apply_rel_rw(brq, card, req);
  1421. brq->data.flags |= MMC_DATA_REL_WR;
  1422. }
  1423. /*
  1424. * Data tag is used only during writing meta data to speed
  1425. * up write and any subsequent read of this meta data
  1426. */
  1427. do_data_tag = card->ext_csd.data_tag_unit_size &&
  1428. (req->cmd_flags & REQ_META) &&
  1429. (rq_data_dir(req) == WRITE) &&
  1430. ((brq->data.blocks * brq->data.blksz) >=
  1431. card->ext_csd.data_tag_unit_size);
  1432. if (do_data_tag)
  1433. brq->data.flags |= MMC_DATA_DAT_TAG;
  1434. mmc_set_data_timeout(&brq->data, card);
  1435. brq->data.sg = mqrq->sg;
  1436. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1437. /*
  1438. * Adjust the sg list so it is the same size as the
  1439. * request.
  1440. */
  1441. if (brq->data.blocks != blk_rq_sectors(req)) {
  1442. int i, data_size = brq->data.blocks << 9;
  1443. struct scatterlist *sg;
  1444. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1445. data_size -= sg->length;
  1446. if (data_size <= 0) {
  1447. sg->length += data_size;
  1448. i++;
  1449. break;
  1450. }
  1451. }
  1452. brq->data.sg_len = i;
  1453. }
  1454. mqrq->areq.mrq = &brq->mrq;
  1455. if (do_rel_wr_p)
  1456. *do_rel_wr_p = do_rel_wr;
  1457. if (do_data_tag_p)
  1458. *do_data_tag_p = do_data_tag;
  1459. }
  1460. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1461. struct mmc_card *card,
  1462. int disable_multi,
  1463. struct mmc_queue *mq)
  1464. {
  1465. u32 readcmd, writecmd;
  1466. struct mmc_blk_request *brq = &mqrq->brq;
  1467. struct request *req = mmc_queue_req_to_req(mqrq);
  1468. struct mmc_blk_data *md = mq->blkdata;
  1469. bool do_rel_wr, do_data_tag;
  1470. mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
  1471. brq->mrq.cmd = &brq->cmd;
  1472. brq->cmd.arg = blk_rq_pos(req);
  1473. if (!mmc_card_blockaddr(card))
  1474. brq->cmd.arg <<= 9;
  1475. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1476. if (brq->data.blocks > 1 || do_rel_wr) {
  1477. /* SPI multiblock writes terminate using a special
  1478. * token, not a STOP_TRANSMISSION request.
  1479. */
  1480. if (!mmc_host_is_spi(card->host) ||
  1481. rq_data_dir(req) == READ)
  1482. brq->mrq.stop = &brq->stop;
  1483. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1484. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1485. } else {
  1486. brq->mrq.stop = NULL;
  1487. readcmd = MMC_READ_SINGLE_BLOCK;
  1488. writecmd = MMC_WRITE_BLOCK;
  1489. }
  1490. brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
  1491. /*
  1492. * Pre-defined multi-block transfers are preferable to
  1493. * open ended-ones (and necessary for reliable writes).
  1494. * However, it is not sufficient to just send CMD23,
  1495. * and avoid the final CMD12, as on an error condition
  1496. * CMD12 (stop) needs to be sent anyway. This, coupled
  1497. * with Auto-CMD23 enhancements provided by some
  1498. * hosts, means that the complexity of dealing
  1499. * with this is best left to the host. If CMD23 is
  1500. * supported by card and host, we'll fill sbc in and let
  1501. * the host deal with handling it correctly. This means
  1502. * that for hosts that don't expose MMC_CAP_CMD23, no
  1503. * change of behavior will be observed.
  1504. *
  1505. * N.B: Some MMC cards experience perf degradation.
  1506. * We'll avoid using CMD23-bounded multiblock writes for
  1507. * these, while retaining features like reliable writes.
  1508. */
  1509. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1510. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1511. do_data_tag)) {
  1512. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1513. brq->sbc.arg = brq->data.blocks |
  1514. (do_rel_wr ? (1 << 31) : 0) |
  1515. (do_data_tag ? (1 << 29) : 0);
  1516. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1517. brq->mrq.sbc = &brq->sbc;
  1518. }
  1519. mqrq->areq.err_check = mmc_blk_err_check;
  1520. }
  1521. static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  1522. struct mmc_blk_request *brq, struct request *req,
  1523. bool old_req_pending)
  1524. {
  1525. bool req_pending;
  1526. /*
  1527. * If this is an SD card and we're writing, we can first
  1528. * mark the known good sectors as ok.
  1529. *
  1530. * If the card is not SD, we can still ok written sectors
  1531. * as reported by the controller (which might be less than
  1532. * the real number of written sectors, but never more).
  1533. */
  1534. if (mmc_card_sd(card)) {
  1535. u32 blocks;
  1536. int err;
  1537. err = mmc_sd_num_wr_blocks(card, &blocks);
  1538. if (err)
  1539. req_pending = old_req_pending;
  1540. else
  1541. req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
  1542. } else {
  1543. req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
  1544. }
  1545. return req_pending;
  1546. }
  1547. static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
  1548. struct request *req,
  1549. struct mmc_queue_req *mqrq)
  1550. {
  1551. if (mmc_card_removed(card))
  1552. req->rq_flags |= RQF_QUIET;
  1553. while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
  1554. mq->qcnt--;
  1555. }
  1556. /**
  1557. * mmc_blk_rw_try_restart() - tries to restart the current async request
  1558. * @mq: the queue with the card and host to restart
  1559. * @req: a new request that want to be started after the current one
  1560. */
  1561. static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
  1562. struct mmc_queue_req *mqrq)
  1563. {
  1564. if (!req)
  1565. return;
  1566. /*
  1567. * If the card was removed, just cancel everything and return.
  1568. */
  1569. if (mmc_card_removed(mq->card)) {
  1570. req->rq_flags |= RQF_QUIET;
  1571. blk_end_request_all(req, BLK_STS_IOERR);
  1572. mq->qcnt--; /* FIXME: just set to 0? */
  1573. return;
  1574. }
  1575. /* Else proceed and try to restart the current async request */
  1576. mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
  1577. mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
  1578. }
  1579. static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
  1580. {
  1581. struct mmc_blk_data *md = mq->blkdata;
  1582. struct mmc_card *card = md->queue.card;
  1583. struct mmc_blk_request *brq;
  1584. int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
  1585. enum mmc_blk_status status;
  1586. struct mmc_queue_req *mqrq_cur = NULL;
  1587. struct mmc_queue_req *mq_rq;
  1588. struct request *old_req;
  1589. struct mmc_async_req *new_areq;
  1590. struct mmc_async_req *old_areq;
  1591. bool req_pending = true;
  1592. if (new_req) {
  1593. mqrq_cur = req_to_mmc_queue_req(new_req);
  1594. mq->qcnt++;
  1595. }
  1596. if (!mq->qcnt)
  1597. return;
  1598. do {
  1599. if (new_req) {
  1600. /*
  1601. * When 4KB native sector is enabled, only 8 blocks
  1602. * multiple read or write is allowed
  1603. */
  1604. if (mmc_large_sector(card) &&
  1605. !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
  1606. pr_err("%s: Transfer size is not 4KB sector size aligned\n",
  1607. new_req->rq_disk->disk_name);
  1608. mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
  1609. return;
  1610. }
  1611. mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
  1612. new_areq = &mqrq_cur->areq;
  1613. } else
  1614. new_areq = NULL;
  1615. old_areq = mmc_start_areq(card->host, new_areq, &status);
  1616. if (!old_areq) {
  1617. /*
  1618. * We have just put the first request into the pipeline
  1619. * and there is nothing more to do until it is
  1620. * complete.
  1621. */
  1622. return;
  1623. }
  1624. /*
  1625. * An asynchronous request has been completed and we proceed
  1626. * to handle the result of it.
  1627. */
  1628. mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
  1629. brq = &mq_rq->brq;
  1630. old_req = mmc_queue_req_to_req(mq_rq);
  1631. type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1632. switch (status) {
  1633. case MMC_BLK_SUCCESS:
  1634. case MMC_BLK_PARTIAL:
  1635. /*
  1636. * Reset success, and accept bytes_xfered. For
  1637. * MMC_BLK_PARTIAL re-submit the remaining request. For
  1638. * MMC_BLK_SUCCESS error out the remaining request (it
  1639. * could not be re-submitted anyway if a next request
  1640. * had already begun).
  1641. */
  1642. mmc_blk_reset_success(md, type);
  1643. req_pending = blk_end_request(old_req, BLK_STS_OK,
  1644. brq->data.bytes_xfered);
  1645. /*
  1646. * If the blk_end_request function returns non-zero even
  1647. * though all data has been transferred and no errors
  1648. * were returned by the host controller, it's a bug.
  1649. */
  1650. if (status == MMC_BLK_SUCCESS && req_pending) {
  1651. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1652. __func__, blk_rq_bytes(old_req),
  1653. brq->data.bytes_xfered);
  1654. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  1655. return;
  1656. }
  1657. break;
  1658. case MMC_BLK_CMD_ERR:
  1659. /*
  1660. * For SD cards, get bytes written, but do not accept
  1661. * bytes_xfered if that fails. For MMC cards accept
  1662. * bytes_xfered. Then try to reset. If reset fails then
  1663. * error out the remaining request, otherwise retry
  1664. * once (N.B mmc_blk_reset() will not succeed twice in a
  1665. * row).
  1666. */
  1667. req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
  1668. if (mmc_blk_reset(md, card->host, type)) {
  1669. if (req_pending)
  1670. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  1671. else
  1672. mq->qcnt--;
  1673. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1674. return;
  1675. }
  1676. if (!req_pending) {
  1677. mq->qcnt--;
  1678. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1679. return;
  1680. }
  1681. break;
  1682. case MMC_BLK_RETRY:
  1683. /*
  1684. * Do not accept bytes_xfered, but retry up to 5 times,
  1685. * otherwise same as abort.
  1686. */
  1687. retune_retry_done = brq->retune_retry_done;
  1688. if (retry++ < 5)
  1689. break;
  1690. /* Fall through */
  1691. case MMC_BLK_ABORT:
  1692. /*
  1693. * Do not accept bytes_xfered, but try to reset. If
  1694. * reset succeeds, try once more, otherwise error out
  1695. * the request.
  1696. */
  1697. if (!mmc_blk_reset(md, card->host, type))
  1698. break;
  1699. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  1700. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1701. return;
  1702. case MMC_BLK_DATA_ERR: {
  1703. int err;
  1704. /*
  1705. * Do not accept bytes_xfered, but try to reset. If
  1706. * reset succeeds, try once more. If reset fails with
  1707. * ENODEV which means the partition is wrong, then error
  1708. * out the request. Otherwise attempt to read one sector
  1709. * at a time.
  1710. */
  1711. err = mmc_blk_reset(md, card->host, type);
  1712. if (!err)
  1713. break;
  1714. if (err == -ENODEV) {
  1715. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  1716. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1717. return;
  1718. }
  1719. /* Fall through */
  1720. }
  1721. case MMC_BLK_ECC_ERR:
  1722. /*
  1723. * Do not accept bytes_xfered. If reading more than one
  1724. * sector, try reading one sector at a time.
  1725. */
  1726. if (brq->data.blocks > 1) {
  1727. /* Redo read one sector at a time */
  1728. pr_warn("%s: retrying using single block read\n",
  1729. old_req->rq_disk->disk_name);
  1730. disable_multi = 1;
  1731. break;
  1732. }
  1733. /*
  1734. * After an error, we redo I/O one sector at a
  1735. * time, so we only reach here after trying to
  1736. * read a single sector.
  1737. */
  1738. req_pending = blk_end_request(old_req, BLK_STS_IOERR,
  1739. brq->data.blksz);
  1740. if (!req_pending) {
  1741. mq->qcnt--;
  1742. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1743. return;
  1744. }
  1745. break;
  1746. case MMC_BLK_NOMEDIUM:
  1747. /* Do not accept bytes_xfered. Error out the request */
  1748. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  1749. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1750. return;
  1751. default:
  1752. /* Do not accept bytes_xfered. Error out the request */
  1753. pr_err("%s: Unhandled return value (%d)",
  1754. old_req->rq_disk->disk_name, status);
  1755. mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
  1756. mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
  1757. return;
  1758. }
  1759. if (req_pending) {
  1760. /*
  1761. * In case of a incomplete request
  1762. * prepare it again and resend.
  1763. */
  1764. mmc_blk_rw_rq_prep(mq_rq, card,
  1765. disable_multi, mq);
  1766. mmc_start_areq(card->host,
  1767. &mq_rq->areq, NULL);
  1768. mq_rq->brq.retune_retry_done = retune_retry_done;
  1769. }
  1770. } while (req_pending);
  1771. mq->qcnt--;
  1772. }
  1773. void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1774. {
  1775. int ret;
  1776. struct mmc_blk_data *md = mq->blkdata;
  1777. struct mmc_card *card = md->queue.card;
  1778. if (req && !mq->qcnt)
  1779. /* claim host only for the first request */
  1780. mmc_get_card(card, NULL);
  1781. ret = mmc_blk_part_switch(card, md->part_type);
  1782. if (ret) {
  1783. if (req) {
  1784. blk_end_request_all(req, BLK_STS_IOERR);
  1785. }
  1786. goto out;
  1787. }
  1788. if (req) {
  1789. switch (req_op(req)) {
  1790. case REQ_OP_DRV_IN:
  1791. case REQ_OP_DRV_OUT:
  1792. /*
  1793. * Complete ongoing async transfer before issuing
  1794. * ioctl()s
  1795. */
  1796. if (mq->qcnt)
  1797. mmc_blk_issue_rw_rq(mq, NULL);
  1798. mmc_blk_issue_drv_op(mq, req);
  1799. break;
  1800. case REQ_OP_DISCARD:
  1801. /*
  1802. * Complete ongoing async transfer before issuing
  1803. * discard.
  1804. */
  1805. if (mq->qcnt)
  1806. mmc_blk_issue_rw_rq(mq, NULL);
  1807. mmc_blk_issue_discard_rq(mq, req);
  1808. break;
  1809. case REQ_OP_SECURE_ERASE:
  1810. /*
  1811. * Complete ongoing async transfer before issuing
  1812. * secure erase.
  1813. */
  1814. if (mq->qcnt)
  1815. mmc_blk_issue_rw_rq(mq, NULL);
  1816. mmc_blk_issue_secdiscard_rq(mq, req);
  1817. break;
  1818. case REQ_OP_FLUSH:
  1819. /*
  1820. * Complete ongoing async transfer before issuing
  1821. * flush.
  1822. */
  1823. if (mq->qcnt)
  1824. mmc_blk_issue_rw_rq(mq, NULL);
  1825. mmc_blk_issue_flush(mq, req);
  1826. break;
  1827. default:
  1828. /* Normal request, just issue it */
  1829. mmc_blk_issue_rw_rq(mq, req);
  1830. card->host->context_info.is_waiting_last_req = false;
  1831. break;
  1832. }
  1833. } else {
  1834. /* No request, flushing the pipeline with NULL */
  1835. mmc_blk_issue_rw_rq(mq, NULL);
  1836. card->host->context_info.is_waiting_last_req = false;
  1837. }
  1838. out:
  1839. if (!mq->qcnt)
  1840. mmc_put_card(card, NULL);
  1841. }
  1842. static inline int mmc_blk_readonly(struct mmc_card *card)
  1843. {
  1844. return mmc_card_readonly(card) ||
  1845. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1846. }
  1847. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1848. struct device *parent,
  1849. sector_t size,
  1850. bool default_ro,
  1851. const char *subname,
  1852. int area_type)
  1853. {
  1854. struct mmc_blk_data *md;
  1855. int devidx, ret;
  1856. devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
  1857. if (devidx < 0) {
  1858. /*
  1859. * We get -ENOSPC because there are no more any available
  1860. * devidx. The reason may be that, either userspace haven't yet
  1861. * unmounted the partitions, which postpones mmc_blk_release()
  1862. * from being called, or the device has more partitions than
  1863. * what we support.
  1864. */
  1865. if (devidx == -ENOSPC)
  1866. dev_err(mmc_dev(card->host),
  1867. "no more device IDs available\n");
  1868. return ERR_PTR(devidx);
  1869. }
  1870. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1871. if (!md) {
  1872. ret = -ENOMEM;
  1873. goto out;
  1874. }
  1875. md->area_type = area_type;
  1876. /*
  1877. * Set the read-only status based on the supported commands
  1878. * and the write protect switch.
  1879. */
  1880. md->read_only = mmc_blk_readonly(card);
  1881. md->disk = alloc_disk(perdev_minors);
  1882. if (md->disk == NULL) {
  1883. ret = -ENOMEM;
  1884. goto err_kfree;
  1885. }
  1886. spin_lock_init(&md->lock);
  1887. INIT_LIST_HEAD(&md->part);
  1888. INIT_LIST_HEAD(&md->rpmbs);
  1889. md->usage = 1;
  1890. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1891. if (ret)
  1892. goto err_putdisk;
  1893. md->queue.blkdata = md;
  1894. /*
  1895. * Keep an extra reference to the queue so that we can shutdown the
  1896. * queue (i.e. call blk_cleanup_queue()) while there are still
  1897. * references to the 'md'. The corresponding blk_put_queue() is in
  1898. * mmc_blk_put().
  1899. */
  1900. if (!blk_get_queue(md->queue.queue)) {
  1901. mmc_cleanup_queue(&md->queue);
  1902. goto err_putdisk;
  1903. }
  1904. md->disk->major = MMC_BLOCK_MAJOR;
  1905. md->disk->first_minor = devidx * perdev_minors;
  1906. md->disk->fops = &mmc_bdops;
  1907. md->disk->private_data = md;
  1908. md->disk->queue = md->queue.queue;
  1909. md->parent = parent;
  1910. set_disk_ro(md->disk, md->read_only || default_ro);
  1911. md->disk->flags = GENHD_FL_EXT_DEVT;
  1912. if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
  1913. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  1914. /*
  1915. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1916. *
  1917. * - be set for removable media with permanent block devices
  1918. * - be unset for removable block devices with permanent media
  1919. *
  1920. * Since MMC block devices clearly fall under the second
  1921. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1922. * should use the block device creation/destruction hotplug
  1923. * messages to tell when the card is present.
  1924. */
  1925. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1926. "mmcblk%u%s", card->host->index, subname ? subname : "");
  1927. if (mmc_card_mmc(card))
  1928. blk_queue_logical_block_size(md->queue.queue,
  1929. card->ext_csd.data_sector_size);
  1930. else
  1931. blk_queue_logical_block_size(md->queue.queue, 512);
  1932. set_capacity(md->disk, size);
  1933. if (mmc_host_cmd23(card->host)) {
  1934. if ((mmc_card_mmc(card) &&
  1935. card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
  1936. (mmc_card_sd(card) &&
  1937. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1938. md->flags |= MMC_BLK_CMD23;
  1939. }
  1940. if (mmc_card_mmc(card) &&
  1941. md->flags & MMC_BLK_CMD23 &&
  1942. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1943. card->ext_csd.rel_sectors)) {
  1944. md->flags |= MMC_BLK_REL_WR;
  1945. blk_queue_write_cache(md->queue.queue, true, true);
  1946. }
  1947. return md;
  1948. err_putdisk:
  1949. put_disk(md->disk);
  1950. err_kfree:
  1951. kfree(md);
  1952. out:
  1953. ida_simple_remove(&mmc_blk_ida, devidx);
  1954. return ERR_PTR(ret);
  1955. }
  1956. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1957. {
  1958. sector_t size;
  1959. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1960. /*
  1961. * The EXT_CSD sector count is in number or 512 byte
  1962. * sectors.
  1963. */
  1964. size = card->ext_csd.sectors;
  1965. } else {
  1966. /*
  1967. * The CSD capacity field is in units of read_blkbits.
  1968. * set_capacity takes units of 512 bytes.
  1969. */
  1970. size = (typeof(sector_t))card->csd.capacity
  1971. << (card->csd.read_blkbits - 9);
  1972. }
  1973. return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  1974. MMC_BLK_DATA_AREA_MAIN);
  1975. }
  1976. static int mmc_blk_alloc_part(struct mmc_card *card,
  1977. struct mmc_blk_data *md,
  1978. unsigned int part_type,
  1979. sector_t size,
  1980. bool default_ro,
  1981. const char *subname,
  1982. int area_type)
  1983. {
  1984. char cap_str[10];
  1985. struct mmc_blk_data *part_md;
  1986. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1987. subname, area_type);
  1988. if (IS_ERR(part_md))
  1989. return PTR_ERR(part_md);
  1990. part_md->part_type = part_type;
  1991. list_add(&part_md->part, &md->part);
  1992. string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
  1993. cap_str, sizeof(cap_str));
  1994. pr_info("%s: %s %s partition %u %s\n",
  1995. part_md->disk->disk_name, mmc_card_id(card),
  1996. mmc_card_name(card), part_md->part_type, cap_str);
  1997. return 0;
  1998. }
  1999. /**
  2000. * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
  2001. * @filp: the character device file
  2002. * @cmd: the ioctl() command
  2003. * @arg: the argument from userspace
  2004. *
  2005. * This will essentially just redirect the ioctl()s coming in over to
  2006. * the main block device spawning the RPMB character device.
  2007. */
  2008. static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
  2009. unsigned long arg)
  2010. {
  2011. struct mmc_rpmb_data *rpmb = filp->private_data;
  2012. int ret;
  2013. switch (cmd) {
  2014. case MMC_IOC_CMD:
  2015. ret = mmc_blk_ioctl_cmd(rpmb->md,
  2016. (struct mmc_ioc_cmd __user *)arg,
  2017. rpmb);
  2018. break;
  2019. case MMC_IOC_MULTI_CMD:
  2020. ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
  2021. (struct mmc_ioc_multi_cmd __user *)arg,
  2022. rpmb);
  2023. break;
  2024. default:
  2025. ret = -EINVAL;
  2026. break;
  2027. }
  2028. return 0;
  2029. }
  2030. #ifdef CONFIG_COMPAT
  2031. static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
  2032. unsigned long arg)
  2033. {
  2034. return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  2035. }
  2036. #endif
  2037. static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
  2038. {
  2039. struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
  2040. struct mmc_rpmb_data, chrdev);
  2041. get_device(&rpmb->dev);
  2042. filp->private_data = rpmb;
  2043. mmc_blk_get(rpmb->md->disk);
  2044. return nonseekable_open(inode, filp);
  2045. }
  2046. static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
  2047. {
  2048. struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
  2049. struct mmc_rpmb_data, chrdev);
  2050. put_device(&rpmb->dev);
  2051. mmc_blk_put(rpmb->md);
  2052. return 0;
  2053. }
  2054. static const struct file_operations mmc_rpmb_fileops = {
  2055. .release = mmc_rpmb_chrdev_release,
  2056. .open = mmc_rpmb_chrdev_open,
  2057. .owner = THIS_MODULE,
  2058. .llseek = no_llseek,
  2059. .unlocked_ioctl = mmc_rpmb_ioctl,
  2060. #ifdef CONFIG_COMPAT
  2061. .compat_ioctl = mmc_rpmb_ioctl_compat,
  2062. #endif
  2063. };
  2064. static void mmc_blk_rpmb_device_release(struct device *dev)
  2065. {
  2066. struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
  2067. ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
  2068. kfree(rpmb);
  2069. }
  2070. static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
  2071. struct mmc_blk_data *md,
  2072. unsigned int part_index,
  2073. sector_t size,
  2074. const char *subname)
  2075. {
  2076. int devidx, ret;
  2077. char rpmb_name[DISK_NAME_LEN];
  2078. char cap_str[10];
  2079. struct mmc_rpmb_data *rpmb;
  2080. /* This creates the minor number for the RPMB char device */
  2081. devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
  2082. if (devidx < 0)
  2083. return devidx;
  2084. rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
  2085. if (!rpmb) {
  2086. ida_simple_remove(&mmc_rpmb_ida, devidx);
  2087. return -ENOMEM;
  2088. }
  2089. snprintf(rpmb_name, sizeof(rpmb_name),
  2090. "mmcblk%u%s", card->host->index, subname ? subname : "");
  2091. rpmb->id = devidx;
  2092. rpmb->part_index = part_index;
  2093. rpmb->dev.init_name = rpmb_name;
  2094. rpmb->dev.bus = &mmc_rpmb_bus_type;
  2095. rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
  2096. rpmb->dev.parent = &card->dev;
  2097. rpmb->dev.release = mmc_blk_rpmb_device_release;
  2098. device_initialize(&rpmb->dev);
  2099. dev_set_drvdata(&rpmb->dev, rpmb);
  2100. rpmb->md = md;
  2101. cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
  2102. rpmb->chrdev.owner = THIS_MODULE;
  2103. ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
  2104. if (ret) {
  2105. pr_err("%s: could not add character device\n", rpmb_name);
  2106. goto out_put_device;
  2107. }
  2108. list_add(&rpmb->node, &md->rpmbs);
  2109. string_get_size((u64)size, 512, STRING_UNITS_2,
  2110. cap_str, sizeof(cap_str));
  2111. pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
  2112. rpmb_name, mmc_card_id(card),
  2113. mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
  2114. MAJOR(mmc_rpmb_devt), rpmb->id);
  2115. return 0;
  2116. out_put_device:
  2117. put_device(&rpmb->dev);
  2118. return ret;
  2119. }
  2120. static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
  2121. {
  2122. cdev_device_del(&rpmb->chrdev, &rpmb->dev);
  2123. put_device(&rpmb->dev);
  2124. }
  2125. /* MMC Physical partitions consist of two boot partitions and
  2126. * up to four general purpose partitions.
  2127. * For each partition enabled in EXT_CSD a block device will be allocatedi
  2128. * to provide access to the partition.
  2129. */
  2130. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  2131. {
  2132. int idx, ret;
  2133. if (!mmc_card_mmc(card))
  2134. return 0;
  2135. for (idx = 0; idx < card->nr_parts; idx++) {
  2136. if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
  2137. /*
  2138. * RPMB partitions does not provide block access, they
  2139. * are only accessed using ioctl():s. Thus create
  2140. * special RPMB block devices that do not have a
  2141. * backing block queue for these.
  2142. */
  2143. ret = mmc_blk_alloc_rpmb_part(card, md,
  2144. card->part[idx].part_cfg,
  2145. card->part[idx].size >> 9,
  2146. card->part[idx].name);
  2147. if (ret)
  2148. return ret;
  2149. } else if (card->part[idx].size) {
  2150. ret = mmc_blk_alloc_part(card, md,
  2151. card->part[idx].part_cfg,
  2152. card->part[idx].size >> 9,
  2153. card->part[idx].force_ro,
  2154. card->part[idx].name,
  2155. card->part[idx].area_type);
  2156. if (ret)
  2157. return ret;
  2158. }
  2159. }
  2160. return 0;
  2161. }
  2162. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  2163. {
  2164. struct mmc_card *card;
  2165. if (md) {
  2166. /*
  2167. * Flush remaining requests and free queues. It
  2168. * is freeing the queue that stops new requests
  2169. * from being accepted.
  2170. */
  2171. card = md->queue.card;
  2172. mmc_cleanup_queue(&md->queue);
  2173. if (md->disk->flags & GENHD_FL_UP) {
  2174. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2175. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  2176. card->ext_csd.boot_ro_lockable)
  2177. device_remove_file(disk_to_dev(md->disk),
  2178. &md->power_ro_lock);
  2179. del_gendisk(md->disk);
  2180. }
  2181. mmc_blk_put(md);
  2182. }
  2183. }
  2184. static void mmc_blk_remove_parts(struct mmc_card *card,
  2185. struct mmc_blk_data *md)
  2186. {
  2187. struct list_head *pos, *q;
  2188. struct mmc_blk_data *part_md;
  2189. struct mmc_rpmb_data *rpmb;
  2190. /* Remove RPMB partitions */
  2191. list_for_each_safe(pos, q, &md->rpmbs) {
  2192. rpmb = list_entry(pos, struct mmc_rpmb_data, node);
  2193. list_del(pos);
  2194. mmc_blk_remove_rpmb_part(rpmb);
  2195. }
  2196. /* Remove block partitions */
  2197. list_for_each_safe(pos, q, &md->part) {
  2198. part_md = list_entry(pos, struct mmc_blk_data, part);
  2199. list_del(pos);
  2200. mmc_blk_remove_req(part_md);
  2201. }
  2202. }
  2203. static int mmc_add_disk(struct mmc_blk_data *md)
  2204. {
  2205. int ret;
  2206. struct mmc_card *card = md->queue.card;
  2207. device_add_disk(md->parent, md->disk);
  2208. md->force_ro.show = force_ro_show;
  2209. md->force_ro.store = force_ro_store;
  2210. sysfs_attr_init(&md->force_ro.attr);
  2211. md->force_ro.attr.name = "force_ro";
  2212. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  2213. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  2214. if (ret)
  2215. goto force_ro_fail;
  2216. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  2217. card->ext_csd.boot_ro_lockable) {
  2218. umode_t mode;
  2219. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  2220. mode = S_IRUGO;
  2221. else
  2222. mode = S_IRUGO | S_IWUSR;
  2223. md->power_ro_lock.show = power_ro_lock_show;
  2224. md->power_ro_lock.store = power_ro_lock_store;
  2225. sysfs_attr_init(&md->power_ro_lock.attr);
  2226. md->power_ro_lock.attr.mode = mode;
  2227. md->power_ro_lock.attr.name =
  2228. "ro_lock_until_next_power_on";
  2229. ret = device_create_file(disk_to_dev(md->disk),
  2230. &md->power_ro_lock);
  2231. if (ret)
  2232. goto power_ro_lock_fail;
  2233. }
  2234. return ret;
  2235. power_ro_lock_fail:
  2236. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  2237. force_ro_fail:
  2238. del_gendisk(md->disk);
  2239. return ret;
  2240. }
  2241. #ifdef CONFIG_DEBUG_FS
  2242. static int mmc_dbg_card_status_get(void *data, u64 *val)
  2243. {
  2244. struct mmc_card *card = data;
  2245. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2246. struct mmc_queue *mq = &md->queue;
  2247. struct request *req;
  2248. int ret;
  2249. /* Ask the block layer about the card status */
  2250. req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
  2251. if (IS_ERR(req))
  2252. return PTR_ERR(req);
  2253. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
  2254. blk_execute_rq(mq->queue, NULL, req, 0);
  2255. ret = req_to_mmc_queue_req(req)->drv_op_result;
  2256. if (ret >= 0) {
  2257. *val = ret;
  2258. ret = 0;
  2259. }
  2260. blk_put_request(req);
  2261. return ret;
  2262. }
  2263. DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
  2264. NULL, "%08llx\n");
  2265. /* That is two digits * 512 + 1 for newline */
  2266. #define EXT_CSD_STR_LEN 1025
  2267. static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
  2268. {
  2269. struct mmc_card *card = inode->i_private;
  2270. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2271. struct mmc_queue *mq = &md->queue;
  2272. struct request *req;
  2273. char *buf;
  2274. ssize_t n = 0;
  2275. u8 *ext_csd;
  2276. int err, i;
  2277. buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
  2278. if (!buf)
  2279. return -ENOMEM;
  2280. /* Ask the block layer for the EXT CSD */
  2281. req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
  2282. if (IS_ERR(req)) {
  2283. err = PTR_ERR(req);
  2284. goto out_free;
  2285. }
  2286. req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
  2287. req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
  2288. blk_execute_rq(mq->queue, NULL, req, 0);
  2289. err = req_to_mmc_queue_req(req)->drv_op_result;
  2290. blk_put_request(req);
  2291. if (err) {
  2292. pr_err("FAILED %d\n", err);
  2293. goto out_free;
  2294. }
  2295. for (i = 0; i < 512; i++)
  2296. n += sprintf(buf + n, "%02x", ext_csd[i]);
  2297. n += sprintf(buf + n, "\n");
  2298. if (n != EXT_CSD_STR_LEN) {
  2299. err = -EINVAL;
  2300. goto out_free;
  2301. }
  2302. filp->private_data = buf;
  2303. kfree(ext_csd);
  2304. return 0;
  2305. out_free:
  2306. kfree(buf);
  2307. return err;
  2308. }
  2309. static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
  2310. size_t cnt, loff_t *ppos)
  2311. {
  2312. char *buf = filp->private_data;
  2313. return simple_read_from_buffer(ubuf, cnt, ppos,
  2314. buf, EXT_CSD_STR_LEN);
  2315. }
  2316. static int mmc_ext_csd_release(struct inode *inode, struct file *file)
  2317. {
  2318. kfree(file->private_data);
  2319. return 0;
  2320. }
  2321. static const struct file_operations mmc_dbg_ext_csd_fops = {
  2322. .open = mmc_ext_csd_open,
  2323. .read = mmc_ext_csd_read,
  2324. .release = mmc_ext_csd_release,
  2325. .llseek = default_llseek,
  2326. };
  2327. static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
  2328. {
  2329. struct dentry *root;
  2330. if (!card->debugfs_root)
  2331. return 0;
  2332. root = card->debugfs_root;
  2333. if (mmc_card_mmc(card) || mmc_card_sd(card)) {
  2334. md->status_dentry =
  2335. debugfs_create_file("status", S_IRUSR, root, card,
  2336. &mmc_dbg_card_status_fops);
  2337. if (!md->status_dentry)
  2338. return -EIO;
  2339. }
  2340. if (mmc_card_mmc(card)) {
  2341. md->ext_csd_dentry =
  2342. debugfs_create_file("ext_csd", S_IRUSR, root, card,
  2343. &mmc_dbg_ext_csd_fops);
  2344. if (!md->ext_csd_dentry)
  2345. return -EIO;
  2346. }
  2347. return 0;
  2348. }
  2349. static void mmc_blk_remove_debugfs(struct mmc_card *card,
  2350. struct mmc_blk_data *md)
  2351. {
  2352. if (!card->debugfs_root)
  2353. return;
  2354. if (!IS_ERR_OR_NULL(md->status_dentry)) {
  2355. debugfs_remove(md->status_dentry);
  2356. md->status_dentry = NULL;
  2357. }
  2358. if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
  2359. debugfs_remove(md->ext_csd_dentry);
  2360. md->ext_csd_dentry = NULL;
  2361. }
  2362. }
  2363. #else
  2364. static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
  2365. {
  2366. return 0;
  2367. }
  2368. static void mmc_blk_remove_debugfs(struct mmc_card *card,
  2369. struct mmc_blk_data *md)
  2370. {
  2371. }
  2372. #endif /* CONFIG_DEBUG_FS */
  2373. static int mmc_blk_probe(struct mmc_card *card)
  2374. {
  2375. struct mmc_blk_data *md, *part_md;
  2376. char cap_str[10];
  2377. /*
  2378. * Check that the card supports the command class(es) we need.
  2379. */
  2380. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  2381. return -ENODEV;
  2382. mmc_fixup_device(card, mmc_blk_fixups);
  2383. md = mmc_blk_alloc(card);
  2384. if (IS_ERR(md))
  2385. return PTR_ERR(md);
  2386. string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
  2387. cap_str, sizeof(cap_str));
  2388. pr_info("%s: %s %s %s %s\n",
  2389. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  2390. cap_str, md->read_only ? "(ro)" : "");
  2391. if (mmc_blk_alloc_parts(card, md))
  2392. goto out;
  2393. dev_set_drvdata(&card->dev, md);
  2394. if (mmc_add_disk(md))
  2395. goto out;
  2396. list_for_each_entry(part_md, &md->part, part) {
  2397. if (mmc_add_disk(part_md))
  2398. goto out;
  2399. }
  2400. /* Add two debugfs entries */
  2401. mmc_blk_add_debugfs(card, md);
  2402. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  2403. pm_runtime_use_autosuspend(&card->dev);
  2404. /*
  2405. * Don't enable runtime PM for SD-combo cards here. Leave that
  2406. * decision to be taken during the SDIO init sequence instead.
  2407. */
  2408. if (card->type != MMC_TYPE_SD_COMBO) {
  2409. pm_runtime_set_active(&card->dev);
  2410. pm_runtime_enable(&card->dev);
  2411. }
  2412. return 0;
  2413. out:
  2414. mmc_blk_remove_parts(card, md);
  2415. mmc_blk_remove_req(md);
  2416. return 0;
  2417. }
  2418. static void mmc_blk_remove(struct mmc_card *card)
  2419. {
  2420. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2421. mmc_blk_remove_debugfs(card, md);
  2422. mmc_blk_remove_parts(card, md);
  2423. pm_runtime_get_sync(&card->dev);
  2424. mmc_claim_host(card->host);
  2425. mmc_blk_part_switch(card, md->part_type);
  2426. mmc_release_host(card->host);
  2427. if (card->type != MMC_TYPE_SD_COMBO)
  2428. pm_runtime_disable(&card->dev);
  2429. pm_runtime_put_noidle(&card->dev);
  2430. mmc_blk_remove_req(md);
  2431. dev_set_drvdata(&card->dev, NULL);
  2432. }
  2433. static int _mmc_blk_suspend(struct mmc_card *card)
  2434. {
  2435. struct mmc_blk_data *part_md;
  2436. struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
  2437. if (md) {
  2438. mmc_queue_suspend(&md->queue);
  2439. list_for_each_entry(part_md, &md->part, part) {
  2440. mmc_queue_suspend(&part_md->queue);
  2441. }
  2442. }
  2443. return 0;
  2444. }
  2445. static void mmc_blk_shutdown(struct mmc_card *card)
  2446. {
  2447. _mmc_blk_suspend(card);
  2448. }
  2449. #ifdef CONFIG_PM_SLEEP
  2450. static int mmc_blk_suspend(struct device *dev)
  2451. {
  2452. struct mmc_card *card = mmc_dev_to_card(dev);
  2453. return _mmc_blk_suspend(card);
  2454. }
  2455. static int mmc_blk_resume(struct device *dev)
  2456. {
  2457. struct mmc_blk_data *part_md;
  2458. struct mmc_blk_data *md = dev_get_drvdata(dev);
  2459. if (md) {
  2460. /*
  2461. * Resume involves the card going into idle state,
  2462. * so current partition is always the main one.
  2463. */
  2464. md->part_curr = md->part_type;
  2465. mmc_queue_resume(&md->queue);
  2466. list_for_each_entry(part_md, &md->part, part) {
  2467. mmc_queue_resume(&part_md->queue);
  2468. }
  2469. }
  2470. return 0;
  2471. }
  2472. #endif
  2473. static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
  2474. static struct mmc_driver mmc_driver = {
  2475. .drv = {
  2476. .name = "mmcblk",
  2477. .pm = &mmc_blk_pm_ops,
  2478. },
  2479. .probe = mmc_blk_probe,
  2480. .remove = mmc_blk_remove,
  2481. .shutdown = mmc_blk_shutdown,
  2482. };
  2483. static int __init mmc_blk_init(void)
  2484. {
  2485. int res;
  2486. res = bus_register(&mmc_rpmb_bus_type);
  2487. if (res < 0) {
  2488. pr_err("mmcblk: could not register RPMB bus type\n");
  2489. return res;
  2490. }
  2491. res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
  2492. if (res < 0) {
  2493. pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
  2494. goto out_bus_unreg;
  2495. }
  2496. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2497. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2498. max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
  2499. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2500. if (res)
  2501. goto out_chrdev_unreg;
  2502. res = mmc_register_driver(&mmc_driver);
  2503. if (res)
  2504. goto out_blkdev_unreg;
  2505. return 0;
  2506. out_blkdev_unreg:
  2507. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2508. out_chrdev_unreg:
  2509. unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
  2510. out_bus_unreg:
  2511. bus_unregister(&mmc_rpmb_bus_type);
  2512. return res;
  2513. }
  2514. static void __exit mmc_blk_exit(void)
  2515. {
  2516. mmc_unregister_driver(&mmc_driver);
  2517. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2518. unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
  2519. }
  2520. module_init(mmc_blk_init);
  2521. module_exit(mmc_blk_exit);
  2522. MODULE_LICENSE("GPL");
  2523. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");