block.c 75 KB

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