block.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. /*
  2. * Block driver for media (i.e., flash cards)
  3. *
  4. * Copyright 2002 Hewlett-Packard Company
  5. * Copyright 2005-2008 Pierre Ossman
  6. *
  7. * Use consistent with the GNU GPL is permitted,
  8. * provided that this copyright notice is
  9. * preserved in its entirety in all copies and derived works.
  10. *
  11. * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
  12. * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
  13. * FITNESS FOR ANY PARTICULAR PURPOSE.
  14. *
  15. * Many thanks to Alessandro Rubini and Jonathan Corbet!
  16. *
  17. * Author: Andrew Christian
  18. * 28 May 2002
  19. */
  20. #include <linux/moduleparam.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/errno.h>
  27. #include <linux/hdreg.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/mutex.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/string_helpers.h>
  33. #include <linux/delay.h>
  34. #include <linux/capability.h>
  35. #include <linux/compat.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/mmc/ioctl.h>
  38. #include <linux/mmc/card.h>
  39. #include <linux/mmc/host.h>
  40. #include <linux/mmc/mmc.h>
  41. #include <linux/mmc/sd.h>
  42. #include <asm/uaccess.h>
  43. #include "queue.h"
  44. MODULE_ALIAS("mmc:block");
  45. #ifdef MODULE_PARAM_PREFIX
  46. #undef MODULE_PARAM_PREFIX
  47. #endif
  48. #define MODULE_PARAM_PREFIX "mmcblk."
  49. #define INAND_CMD38_ARG_EXT_CSD 113
  50. #define INAND_CMD38_ARG_ERASE 0x00
  51. #define INAND_CMD38_ARG_TRIM 0x01
  52. #define INAND_CMD38_ARG_SECERASE 0x80
  53. #define INAND_CMD38_ARG_SECTRIM1 0x81
  54. #define INAND_CMD38_ARG_SECTRIM2 0x88
  55. #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  56. #define MMC_SANITIZE_REQ_TIMEOUT 240000
  57. #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
  58. #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
  59. (req->cmd_flags & REQ_META)) && \
  60. (rq_data_dir(req) == WRITE))
  61. #define PACKED_CMD_VER 0x01
  62. #define PACKED_CMD_WR 0x02
  63. static DEFINE_MUTEX(block_mutex);
  64. /*
  65. * The defaults come from config options but can be overriden by module
  66. * or bootarg options.
  67. */
  68. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  69. /*
  70. * We've only got one major, so number of mmcblk devices is
  71. * limited to 256 / number of minors per device.
  72. */
  73. static int max_devices;
  74. /* 256 minors, so at most 256 separate devices */
  75. static DECLARE_BITMAP(dev_use, 256);
  76. static DECLARE_BITMAP(name_use, 256);
  77. /*
  78. * There is one mmc_blk_data per slot.
  79. */
  80. struct mmc_blk_data {
  81. spinlock_t lock;
  82. struct gendisk *disk;
  83. struct mmc_queue queue;
  84. struct list_head part;
  85. unsigned int flags;
  86. #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
  87. #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
  88. #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
  89. unsigned int usage;
  90. unsigned int read_only;
  91. unsigned int part_type;
  92. unsigned int name_idx;
  93. unsigned int reset_done;
  94. #define MMC_BLK_READ BIT(0)
  95. #define MMC_BLK_WRITE BIT(1)
  96. #define MMC_BLK_DISCARD BIT(2)
  97. #define MMC_BLK_SECDISCARD BIT(3)
  98. /*
  99. * Only set in main mmc_blk_data associated
  100. * with mmc_card with mmc_set_drvdata, and keeps
  101. * track of the current selected device partition.
  102. */
  103. unsigned int part_curr;
  104. struct device_attribute force_ro;
  105. struct device_attribute power_ro_lock;
  106. int area_type;
  107. };
  108. static DEFINE_MUTEX(open_lock);
  109. enum {
  110. MMC_PACKED_NR_IDX = -1,
  111. MMC_PACKED_NR_ZERO,
  112. MMC_PACKED_NR_SINGLE,
  113. };
  114. module_param(perdev_minors, int, 0444);
  115. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  116. static inline int mmc_blk_part_switch(struct mmc_card *card,
  117. struct mmc_blk_data *md);
  118. static int get_card_status(struct mmc_card *card, u32 *status, int retries);
  119. static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
  120. {
  121. struct mmc_packed *packed = mqrq->packed;
  122. BUG_ON(!packed);
  123. mqrq->cmd_type = MMC_PACKED_NONE;
  124. packed->nr_entries = MMC_PACKED_NR_ZERO;
  125. packed->idx_failure = MMC_PACKED_NR_IDX;
  126. packed->retries = 0;
  127. packed->blocks = 0;
  128. }
  129. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  130. {
  131. struct mmc_blk_data *md;
  132. mutex_lock(&open_lock);
  133. md = disk->private_data;
  134. if (md && md->usage == 0)
  135. md = NULL;
  136. if (md)
  137. md->usage++;
  138. mutex_unlock(&open_lock);
  139. return md;
  140. }
  141. static inline int mmc_get_devidx(struct gendisk *disk)
  142. {
  143. int devmaj = MAJOR(disk_devt(disk));
  144. int devidx = MINOR(disk_devt(disk)) / perdev_minors;
  145. if (!devmaj)
  146. devidx = disk->first_minor / perdev_minors;
  147. return devidx;
  148. }
  149. static void mmc_blk_put(struct mmc_blk_data *md)
  150. {
  151. mutex_lock(&open_lock);
  152. md->usage--;
  153. if (md->usage == 0) {
  154. int devidx = mmc_get_devidx(md->disk);
  155. blk_cleanup_queue(md->queue.queue);
  156. __clear_bit(devidx, dev_use);
  157. put_disk(md->disk);
  158. kfree(md);
  159. }
  160. mutex_unlock(&open_lock);
  161. }
  162. static ssize_t power_ro_lock_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. int ret;
  166. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  167. struct mmc_card *card = md->queue.card;
  168. int locked = 0;
  169. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
  170. locked = 2;
  171. else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
  172. locked = 1;
  173. ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
  174. return ret;
  175. }
  176. static ssize_t power_ro_lock_store(struct device *dev,
  177. struct device_attribute *attr, const char *buf, size_t count)
  178. {
  179. int ret;
  180. struct mmc_blk_data *md, *part_md;
  181. struct mmc_card *card;
  182. unsigned long set;
  183. if (kstrtoul(buf, 0, &set))
  184. return -EINVAL;
  185. if (set != 1)
  186. return count;
  187. md = mmc_blk_get(dev_to_disk(dev));
  188. card = md->queue.card;
  189. mmc_get_card(card);
  190. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
  191. card->ext_csd.boot_ro_lock |
  192. EXT_CSD_BOOT_WP_B_PWR_WP_EN,
  193. card->ext_csd.part_time);
  194. if (ret)
  195. pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
  196. else
  197. card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
  198. mmc_put_card(card);
  199. if (!ret) {
  200. pr_info("%s: Locking boot partition ro until next power on\n",
  201. md->disk->disk_name);
  202. set_disk_ro(md->disk, 1);
  203. list_for_each_entry(part_md, &md->part, part)
  204. if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
  205. pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
  206. set_disk_ro(part_md->disk, 1);
  207. }
  208. }
  209. mmc_blk_put(md);
  210. return count;
  211. }
  212. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  213. char *buf)
  214. {
  215. int ret;
  216. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  217. ret = snprintf(buf, PAGE_SIZE, "%d",
  218. get_disk_ro(dev_to_disk(dev)) ^
  219. md->read_only);
  220. mmc_blk_put(md);
  221. return ret;
  222. }
  223. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  224. const char *buf, size_t count)
  225. {
  226. int ret;
  227. char *end;
  228. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  229. unsigned long set = simple_strtoul(buf, &end, 0);
  230. if (end == buf) {
  231. ret = -EINVAL;
  232. goto out;
  233. }
  234. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  235. ret = count;
  236. out:
  237. mmc_blk_put(md);
  238. return ret;
  239. }
  240. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  241. {
  242. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  243. int ret = -ENXIO;
  244. mutex_lock(&block_mutex);
  245. if (md) {
  246. if (md->usage == 2)
  247. check_disk_change(bdev);
  248. ret = 0;
  249. if ((mode & FMODE_WRITE) && md->read_only) {
  250. mmc_blk_put(md);
  251. ret = -EROFS;
  252. }
  253. }
  254. mutex_unlock(&block_mutex);
  255. return ret;
  256. }
  257. static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
  258. {
  259. struct mmc_blk_data *md = disk->private_data;
  260. mutex_lock(&block_mutex);
  261. mmc_blk_put(md);
  262. mutex_unlock(&block_mutex);
  263. }
  264. static int
  265. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  266. {
  267. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  268. geo->heads = 4;
  269. geo->sectors = 16;
  270. return 0;
  271. }
  272. struct mmc_blk_ioc_data {
  273. struct mmc_ioc_cmd ic;
  274. unsigned char *buf;
  275. u64 buf_bytes;
  276. };
  277. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  278. struct mmc_ioc_cmd __user *user)
  279. {
  280. struct mmc_blk_ioc_data *idata;
  281. int err;
  282. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  283. if (!idata) {
  284. err = -ENOMEM;
  285. goto out;
  286. }
  287. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  288. err = -EFAULT;
  289. goto idata_err;
  290. }
  291. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  292. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  293. err = -EOVERFLOW;
  294. goto idata_err;
  295. }
  296. if (!idata->buf_bytes)
  297. return idata;
  298. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  299. if (!idata->buf) {
  300. err = -ENOMEM;
  301. goto idata_err;
  302. }
  303. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  304. idata->ic.data_ptr, idata->buf_bytes)) {
  305. err = -EFAULT;
  306. goto copy_err;
  307. }
  308. return idata;
  309. copy_err:
  310. kfree(idata->buf);
  311. idata_err:
  312. kfree(idata);
  313. out:
  314. return ERR_PTR(err);
  315. }
  316. static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
  317. u32 retries_max)
  318. {
  319. int err;
  320. u32 retry_count = 0;
  321. if (!status || !retries_max)
  322. return -EINVAL;
  323. do {
  324. err = get_card_status(card, status, 5);
  325. if (err)
  326. break;
  327. if (!R1_STATUS(*status) &&
  328. (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
  329. break; /* RPMB programming operation complete */
  330. /*
  331. * Rechedule to give the MMC device a chance to continue
  332. * processing the previous command without being polled too
  333. * frequently.
  334. */
  335. usleep_range(1000, 5000);
  336. } while (++retry_count < retries_max);
  337. if (retry_count == retries_max)
  338. err = -EPERM;
  339. return err;
  340. }
  341. static int ioctl_do_sanitize(struct mmc_card *card)
  342. {
  343. int err;
  344. if (!(mmc_can_sanitize(card) &&
  345. (card->host->caps2 & MMC_CAP2_SANITIZE))) {
  346. pr_warn("%s: %s - SANITIZE is not supported\n",
  347. mmc_hostname(card->host), __func__);
  348. err = -EOPNOTSUPP;
  349. goto out;
  350. }
  351. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  352. mmc_hostname(card->host), __func__);
  353. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  354. EXT_CSD_SANITIZE_START, 1,
  355. MMC_SANITIZE_REQ_TIMEOUT);
  356. if (err)
  357. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  358. mmc_hostname(card->host), __func__, err);
  359. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  360. __func__);
  361. out:
  362. return err;
  363. }
  364. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  365. struct mmc_ioc_cmd __user *ic_ptr)
  366. {
  367. struct mmc_blk_ioc_data *idata;
  368. struct mmc_blk_data *md;
  369. struct mmc_card *card;
  370. struct mmc_command cmd = {0};
  371. struct mmc_data data = {0};
  372. struct mmc_request mrq = {NULL};
  373. struct scatterlist sg;
  374. int err;
  375. int is_rpmb = false;
  376. u32 status = 0;
  377. /*
  378. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  379. * whole block device, not on a partition. This prevents overspray
  380. * between sibling partitions.
  381. */
  382. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  383. return -EPERM;
  384. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  385. if (IS_ERR(idata))
  386. return PTR_ERR(idata);
  387. md = mmc_blk_get(bdev->bd_disk);
  388. if (!md) {
  389. err = -EINVAL;
  390. goto cmd_err;
  391. }
  392. if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
  393. is_rpmb = true;
  394. card = md->queue.card;
  395. if (IS_ERR(card)) {
  396. err = PTR_ERR(card);
  397. goto cmd_done;
  398. }
  399. cmd.opcode = idata->ic.opcode;
  400. cmd.arg = idata->ic.arg;
  401. cmd.flags = idata->ic.flags;
  402. if (idata->buf_bytes) {
  403. data.sg = &sg;
  404. data.sg_len = 1;
  405. data.blksz = idata->ic.blksz;
  406. data.blocks = idata->ic.blocks;
  407. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  408. if (idata->ic.write_flag)
  409. data.flags = MMC_DATA_WRITE;
  410. else
  411. data.flags = MMC_DATA_READ;
  412. /* data.flags must already be set before doing this. */
  413. mmc_set_data_timeout(&data, card);
  414. /* Allow overriding the timeout_ns for empirical tuning. */
  415. if (idata->ic.data_timeout_ns)
  416. data.timeout_ns = idata->ic.data_timeout_ns;
  417. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  418. /*
  419. * Pretend this is a data transfer and rely on the
  420. * host driver to compute timeout. When all host
  421. * drivers support cmd.cmd_timeout for R1B, this
  422. * can be changed to:
  423. *
  424. * mrq.data = NULL;
  425. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  426. */
  427. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  428. }
  429. mrq.data = &data;
  430. }
  431. mrq.cmd = &cmd;
  432. mmc_get_card(card);
  433. err = mmc_blk_part_switch(card, md);
  434. if (err)
  435. goto cmd_rel_host;
  436. if (idata->ic.is_acmd) {
  437. err = mmc_app_cmd(card->host, card);
  438. if (err)
  439. goto cmd_rel_host;
  440. }
  441. if (is_rpmb) {
  442. err = mmc_set_blockcount(card, data.blocks,
  443. idata->ic.write_flag & (1 << 31));
  444. if (err)
  445. goto cmd_rel_host;
  446. }
  447. if (MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) {
  448. err = ioctl_do_sanitize(card);
  449. if (err)
  450. pr_err("%s: ioctl_do_sanitize() failed. err = %d",
  451. __func__, err);
  452. goto cmd_rel_host;
  453. }
  454. mmc_wait_for_req(card->host, &mrq);
  455. if (cmd.error) {
  456. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  457. __func__, cmd.error);
  458. err = cmd.error;
  459. goto cmd_rel_host;
  460. }
  461. if (data.error) {
  462. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  463. __func__, data.error);
  464. err = data.error;
  465. goto cmd_rel_host;
  466. }
  467. /*
  468. * According to the SD specs, some commands require a delay after
  469. * issuing the command.
  470. */
  471. if (idata->ic.postsleep_min_us)
  472. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  473. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  474. err = -EFAULT;
  475. goto cmd_rel_host;
  476. }
  477. if (!idata->ic.write_flag) {
  478. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  479. idata->buf, idata->buf_bytes)) {
  480. err = -EFAULT;
  481. goto cmd_rel_host;
  482. }
  483. }
  484. if (is_rpmb) {
  485. /*
  486. * Ensure RPMB command has completed by polling CMD13
  487. * "Send Status".
  488. */
  489. err = ioctl_rpmb_card_status_poll(card, &status, 5);
  490. if (err)
  491. dev_err(mmc_dev(card->host),
  492. "%s: Card Status=0x%08X, error %d\n",
  493. __func__, status, err);
  494. }
  495. cmd_rel_host:
  496. mmc_put_card(card);
  497. cmd_done:
  498. mmc_blk_put(md);
  499. cmd_err:
  500. kfree(idata->buf);
  501. kfree(idata);
  502. return err;
  503. }
  504. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  505. unsigned int cmd, unsigned long arg)
  506. {
  507. int ret = -EINVAL;
  508. if (cmd == MMC_IOC_CMD)
  509. ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
  510. return ret;
  511. }
  512. #ifdef CONFIG_COMPAT
  513. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  514. unsigned int cmd, unsigned long arg)
  515. {
  516. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  517. }
  518. #endif
  519. static const struct block_device_operations mmc_bdops = {
  520. .open = mmc_blk_open,
  521. .release = mmc_blk_release,
  522. .getgeo = mmc_blk_getgeo,
  523. .owner = THIS_MODULE,
  524. .ioctl = mmc_blk_ioctl,
  525. #ifdef CONFIG_COMPAT
  526. .compat_ioctl = mmc_blk_compat_ioctl,
  527. #endif
  528. };
  529. static inline int mmc_blk_part_switch(struct mmc_card *card,
  530. struct mmc_blk_data *md)
  531. {
  532. int ret;
  533. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  534. if (main_md->part_curr == md->part_type)
  535. return 0;
  536. if (mmc_card_mmc(card)) {
  537. u8 part_config = card->ext_csd.part_config;
  538. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  539. part_config |= md->part_type;
  540. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  541. EXT_CSD_PART_CONFIG, part_config,
  542. card->ext_csd.part_time);
  543. if (ret)
  544. return ret;
  545. card->ext_csd.part_config = part_config;
  546. }
  547. main_md->part_curr = md->part_type;
  548. return 0;
  549. }
  550. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  551. {
  552. int err;
  553. u32 result;
  554. __be32 *blocks;
  555. struct mmc_request mrq = {NULL};
  556. struct mmc_command cmd = {0};
  557. struct mmc_data data = {0};
  558. struct scatterlist sg;
  559. cmd.opcode = MMC_APP_CMD;
  560. cmd.arg = card->rca << 16;
  561. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  562. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  563. if (err)
  564. return (u32)-1;
  565. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  566. return (u32)-1;
  567. memset(&cmd, 0, sizeof(struct mmc_command));
  568. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  569. cmd.arg = 0;
  570. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  571. data.blksz = 4;
  572. data.blocks = 1;
  573. data.flags = MMC_DATA_READ;
  574. data.sg = &sg;
  575. data.sg_len = 1;
  576. mmc_set_data_timeout(&data, card);
  577. mrq.cmd = &cmd;
  578. mrq.data = &data;
  579. blocks = kmalloc(4, GFP_KERNEL);
  580. if (!blocks)
  581. return (u32)-1;
  582. sg_init_one(&sg, blocks, 4);
  583. mmc_wait_for_req(card->host, &mrq);
  584. result = ntohl(*blocks);
  585. kfree(blocks);
  586. if (cmd.error || data.error)
  587. result = (u32)-1;
  588. return result;
  589. }
  590. static int send_stop(struct mmc_card *card, u32 *status)
  591. {
  592. struct mmc_command cmd = {0};
  593. int err;
  594. cmd.opcode = MMC_STOP_TRANSMISSION;
  595. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  596. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  597. if (err == 0)
  598. *status = cmd.resp[0];
  599. return err;
  600. }
  601. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  602. {
  603. struct mmc_command cmd = {0};
  604. int err;
  605. cmd.opcode = MMC_SEND_STATUS;
  606. if (!mmc_host_is_spi(card->host))
  607. cmd.arg = card->rca << 16;
  608. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  609. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  610. if (err == 0)
  611. *status = cmd.resp[0];
  612. return err;
  613. }
  614. #define ERR_NOMEDIUM 3
  615. #define ERR_RETRY 2
  616. #define ERR_ABORT 1
  617. #define ERR_CONTINUE 0
  618. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  619. bool status_valid, u32 status)
  620. {
  621. switch (error) {
  622. case -EILSEQ:
  623. /* response crc error, retry the r/w cmd */
  624. pr_err("%s: %s sending %s command, card status %#x\n",
  625. req->rq_disk->disk_name, "response CRC error",
  626. name, status);
  627. return ERR_RETRY;
  628. case -ETIMEDOUT:
  629. pr_err("%s: %s sending %s command, card status %#x\n",
  630. req->rq_disk->disk_name, "timed out", name, status);
  631. /* If the status cmd initially failed, retry the r/w cmd */
  632. if (!status_valid)
  633. return ERR_RETRY;
  634. /*
  635. * If it was a r/w cmd crc error, or illegal command
  636. * (eg, issued in wrong state) then retry - we should
  637. * have corrected the state problem above.
  638. */
  639. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
  640. return ERR_RETRY;
  641. /* Otherwise abort the command */
  642. return ERR_ABORT;
  643. default:
  644. /* We don't understand the error code the driver gave us */
  645. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  646. req->rq_disk->disk_name, error, status);
  647. return ERR_ABORT;
  648. }
  649. }
  650. /*
  651. * Initial r/w and stop cmd error recovery.
  652. * We don't know whether the card received the r/w cmd or not, so try to
  653. * restore things back to a sane state. Essentially, we do this as follows:
  654. * - Obtain card status. If the first attempt to obtain card status fails,
  655. * the status word will reflect the failed status cmd, not the failed
  656. * r/w cmd. If we fail to obtain card status, it suggests we can no
  657. * longer communicate with the card.
  658. * - Check the card state. If the card received the cmd but there was a
  659. * transient problem with the response, it might still be in a data transfer
  660. * mode. Try to send it a stop command. If this fails, we can't recover.
  661. * - If the r/w cmd failed due to a response CRC error, it was probably
  662. * transient, so retry the cmd.
  663. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  664. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  665. * illegal cmd, retry.
  666. * Otherwise we don't understand what happened, so abort.
  667. */
  668. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  669. struct mmc_blk_request *brq, int *ecc_err)
  670. {
  671. bool prev_cmd_status_valid = true;
  672. u32 status, stop_status = 0;
  673. int err, retry;
  674. if (mmc_card_removed(card))
  675. return ERR_NOMEDIUM;
  676. /*
  677. * Try to get card status which indicates both the card state
  678. * and why there was no response. If the first attempt fails,
  679. * we can't be sure the returned status is for the r/w command.
  680. */
  681. for (retry = 2; retry >= 0; retry--) {
  682. err = get_card_status(card, &status, 0);
  683. if (!err)
  684. break;
  685. prev_cmd_status_valid = false;
  686. pr_err("%s: error %d sending status command, %sing\n",
  687. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  688. }
  689. /* We couldn't get a response from the card. Give up. */
  690. if (err) {
  691. /* Check if the card is removed */
  692. if (mmc_detect_card_removed(card->host))
  693. return ERR_NOMEDIUM;
  694. return ERR_ABORT;
  695. }
  696. /* Flag ECC errors */
  697. if ((status & R1_CARD_ECC_FAILED) ||
  698. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  699. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  700. *ecc_err = 1;
  701. /*
  702. * Check the current card state. If it is in some data transfer
  703. * mode, tell it to stop (and hopefully transition back to TRAN.)
  704. */
  705. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  706. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  707. err = send_stop(card, &stop_status);
  708. if (err)
  709. pr_err("%s: error %d sending stop command\n",
  710. req->rq_disk->disk_name, err);
  711. /*
  712. * If the stop cmd also timed out, the card is probably
  713. * not present, so abort. Other errors are bad news too.
  714. */
  715. if (err)
  716. return ERR_ABORT;
  717. if (stop_status & R1_CARD_ECC_FAILED)
  718. *ecc_err = 1;
  719. }
  720. /* Check for set block count errors */
  721. if (brq->sbc.error)
  722. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  723. prev_cmd_status_valid, status);
  724. /* Check for r/w command errors */
  725. if (brq->cmd.error)
  726. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  727. prev_cmd_status_valid, status);
  728. /* Data errors */
  729. if (!brq->stop.error)
  730. return ERR_CONTINUE;
  731. /* Now for stop errors. These aren't fatal to the transfer. */
  732. pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  733. req->rq_disk->disk_name, brq->stop.error,
  734. brq->cmd.resp[0], status);
  735. /*
  736. * Subsitute in our own stop status as this will give the error
  737. * state which happened during the execution of the r/w command.
  738. */
  739. if (stop_status) {
  740. brq->stop.resp[0] = stop_status;
  741. brq->stop.error = 0;
  742. }
  743. return ERR_CONTINUE;
  744. }
  745. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  746. int type)
  747. {
  748. int err;
  749. if (md->reset_done & type)
  750. return -EEXIST;
  751. md->reset_done |= type;
  752. err = mmc_hw_reset(host);
  753. /* Ensure we switch back to the correct partition */
  754. if (err != -EOPNOTSUPP) {
  755. struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
  756. int part_err;
  757. main_md->part_curr = main_md->part_type;
  758. part_err = mmc_blk_part_switch(host->card, md);
  759. if (part_err) {
  760. /*
  761. * We have failed to get back into the correct
  762. * partition, so we need to abort the whole request.
  763. */
  764. return -ENODEV;
  765. }
  766. }
  767. return err;
  768. }
  769. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  770. {
  771. md->reset_done &= ~type;
  772. }
  773. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  774. {
  775. struct mmc_blk_data *md = mq->data;
  776. struct mmc_card *card = md->queue.card;
  777. unsigned int from, nr, arg;
  778. int err = 0, type = MMC_BLK_DISCARD;
  779. if (!mmc_can_erase(card)) {
  780. err = -EOPNOTSUPP;
  781. goto out;
  782. }
  783. from = blk_rq_pos(req);
  784. nr = blk_rq_sectors(req);
  785. if (mmc_can_discard(card))
  786. arg = MMC_DISCARD_ARG;
  787. else if (mmc_can_trim(card))
  788. arg = MMC_TRIM_ARG;
  789. else
  790. arg = MMC_ERASE_ARG;
  791. retry:
  792. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  793. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  794. INAND_CMD38_ARG_EXT_CSD,
  795. arg == MMC_TRIM_ARG ?
  796. INAND_CMD38_ARG_TRIM :
  797. INAND_CMD38_ARG_ERASE,
  798. 0);
  799. if (err)
  800. goto out;
  801. }
  802. err = mmc_erase(card, from, nr, arg);
  803. out:
  804. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  805. goto retry;
  806. if (!err)
  807. mmc_blk_reset_success(md, type);
  808. blk_end_request(req, err, blk_rq_bytes(req));
  809. return err ? 0 : 1;
  810. }
  811. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  812. struct request *req)
  813. {
  814. struct mmc_blk_data *md = mq->data;
  815. struct mmc_card *card = md->queue.card;
  816. unsigned int from, nr, arg;
  817. int err = 0, type = MMC_BLK_SECDISCARD;
  818. if (!(mmc_can_secure_erase_trim(card))) {
  819. err = -EOPNOTSUPP;
  820. goto out;
  821. }
  822. from = blk_rq_pos(req);
  823. nr = blk_rq_sectors(req);
  824. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  825. arg = MMC_SECURE_TRIM1_ARG;
  826. else
  827. arg = MMC_SECURE_ERASE_ARG;
  828. retry:
  829. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  830. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  831. INAND_CMD38_ARG_EXT_CSD,
  832. arg == MMC_SECURE_TRIM1_ARG ?
  833. INAND_CMD38_ARG_SECTRIM1 :
  834. INAND_CMD38_ARG_SECERASE,
  835. 0);
  836. if (err)
  837. goto out_retry;
  838. }
  839. err = mmc_erase(card, from, nr, arg);
  840. if (err == -EIO)
  841. goto out_retry;
  842. if (err)
  843. goto out;
  844. if (arg == MMC_SECURE_TRIM1_ARG) {
  845. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  846. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  847. INAND_CMD38_ARG_EXT_CSD,
  848. INAND_CMD38_ARG_SECTRIM2,
  849. 0);
  850. if (err)
  851. goto out_retry;
  852. }
  853. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  854. if (err == -EIO)
  855. goto out_retry;
  856. if (err)
  857. goto out;
  858. }
  859. out_retry:
  860. if (err && !mmc_blk_reset(md, card->host, type))
  861. goto retry;
  862. if (!err)
  863. mmc_blk_reset_success(md, type);
  864. out:
  865. blk_end_request(req, err, blk_rq_bytes(req));
  866. return err ? 0 : 1;
  867. }
  868. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  869. {
  870. struct mmc_blk_data *md = mq->data;
  871. struct mmc_card *card = md->queue.card;
  872. int ret = 0;
  873. ret = mmc_flush_cache(card);
  874. if (ret)
  875. ret = -EIO;
  876. blk_end_request_all(req, ret);
  877. return ret ? 0 : 1;
  878. }
  879. /*
  880. * Reformat current write as a reliable write, supporting
  881. * both legacy and the enhanced reliable write MMC cards.
  882. * In each transfer we'll handle only as much as a single
  883. * reliable write can handle, thus finish the request in
  884. * partial completions.
  885. */
  886. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  887. struct mmc_card *card,
  888. struct request *req)
  889. {
  890. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  891. /* Legacy mode imposes restrictions on transfers. */
  892. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  893. brq->data.blocks = 1;
  894. if (brq->data.blocks > card->ext_csd.rel_sectors)
  895. brq->data.blocks = card->ext_csd.rel_sectors;
  896. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  897. brq->data.blocks = 1;
  898. }
  899. }
  900. #define CMD_ERRORS \
  901. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  902. R1_ADDRESS_ERROR | /* Misaligned address */ \
  903. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  904. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  905. R1_CC_ERROR | /* Card controller error */ \
  906. R1_ERROR) /* General/unknown error */
  907. static int mmc_blk_err_check(struct mmc_card *card,
  908. struct mmc_async_req *areq)
  909. {
  910. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  911. mmc_active);
  912. struct mmc_blk_request *brq = &mq_mrq->brq;
  913. struct request *req = mq_mrq->req;
  914. int ecc_err = 0;
  915. /*
  916. * sbc.error indicates a problem with the set block count
  917. * command. No data will have been transferred.
  918. *
  919. * cmd.error indicates a problem with the r/w command. No
  920. * data will have been transferred.
  921. *
  922. * stop.error indicates a problem with the stop command. Data
  923. * may have been transferred, or may still be transferring.
  924. */
  925. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  926. brq->data.error) {
  927. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
  928. case ERR_RETRY:
  929. return MMC_BLK_RETRY;
  930. case ERR_ABORT:
  931. return MMC_BLK_ABORT;
  932. case ERR_NOMEDIUM:
  933. return MMC_BLK_NOMEDIUM;
  934. case ERR_CONTINUE:
  935. break;
  936. }
  937. }
  938. /*
  939. * Check for errors relating to the execution of the
  940. * initial command - such as address errors. No data
  941. * has been transferred.
  942. */
  943. if (brq->cmd.resp[0] & CMD_ERRORS) {
  944. pr_err("%s: r/w command failed, status = %#x\n",
  945. req->rq_disk->disk_name, brq->cmd.resp[0]);
  946. return MMC_BLK_ABORT;
  947. }
  948. /*
  949. * Everything else is either success, or a data error of some
  950. * kind. If it was a write, we may have transitioned to
  951. * program mode, which we have to wait for it to complete.
  952. */
  953. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  954. u32 status;
  955. unsigned long timeout;
  956. timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
  957. do {
  958. int err = get_card_status(card, &status, 5);
  959. if (err) {
  960. pr_err("%s: error %d requesting status\n",
  961. req->rq_disk->disk_name, err);
  962. return MMC_BLK_CMD_ERR;
  963. }
  964. /* Timeout if the device never becomes ready for data
  965. * and never leaves the program state.
  966. */
  967. if (time_after(jiffies, timeout)) {
  968. pr_err("%s: Card stuck in programming state!"\
  969. " %s %s\n", mmc_hostname(card->host),
  970. req->rq_disk->disk_name, __func__);
  971. return MMC_BLK_CMD_ERR;
  972. }
  973. /*
  974. * Some cards mishandle the status bits,
  975. * so make sure to check both the busy
  976. * indication and the card state.
  977. */
  978. } while (!(status & R1_READY_FOR_DATA) ||
  979. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  980. }
  981. if (brq->data.error) {
  982. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  983. req->rq_disk->disk_name, brq->data.error,
  984. (unsigned)blk_rq_pos(req),
  985. (unsigned)blk_rq_sectors(req),
  986. brq->cmd.resp[0], brq->stop.resp[0]);
  987. if (rq_data_dir(req) == READ) {
  988. if (ecc_err)
  989. return MMC_BLK_ECC_ERR;
  990. return MMC_BLK_DATA_ERR;
  991. } else {
  992. return MMC_BLK_CMD_ERR;
  993. }
  994. }
  995. if (!brq->data.bytes_xfered)
  996. return MMC_BLK_RETRY;
  997. if (mmc_packed_cmd(mq_mrq->cmd_type)) {
  998. if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
  999. return MMC_BLK_PARTIAL;
  1000. else
  1001. return MMC_BLK_SUCCESS;
  1002. }
  1003. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1004. return MMC_BLK_PARTIAL;
  1005. return MMC_BLK_SUCCESS;
  1006. }
  1007. static int mmc_blk_packed_err_check(struct mmc_card *card,
  1008. struct mmc_async_req *areq)
  1009. {
  1010. struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
  1011. mmc_active);
  1012. struct request *req = mq_rq->req;
  1013. struct mmc_packed *packed = mq_rq->packed;
  1014. int err, check, status;
  1015. u8 *ext_csd;
  1016. BUG_ON(!packed);
  1017. packed->retries--;
  1018. check = mmc_blk_err_check(card, areq);
  1019. err = get_card_status(card, &status, 0);
  1020. if (err) {
  1021. pr_err("%s: error %d sending status command\n",
  1022. req->rq_disk->disk_name, err);
  1023. return MMC_BLK_ABORT;
  1024. }
  1025. if (status & R1_EXCEPTION_EVENT) {
  1026. ext_csd = kzalloc(512, GFP_KERNEL);
  1027. if (!ext_csd) {
  1028. pr_err("%s: unable to allocate buffer for ext_csd\n",
  1029. req->rq_disk->disk_name);
  1030. return -ENOMEM;
  1031. }
  1032. err = mmc_send_ext_csd(card, ext_csd);
  1033. if (err) {
  1034. pr_err("%s: error %d sending ext_csd\n",
  1035. req->rq_disk->disk_name, err);
  1036. check = MMC_BLK_ABORT;
  1037. goto free;
  1038. }
  1039. if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
  1040. EXT_CSD_PACKED_FAILURE) &&
  1041. (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1042. EXT_CSD_PACKED_GENERIC_ERROR)) {
  1043. if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1044. EXT_CSD_PACKED_INDEXED_ERROR) {
  1045. packed->idx_failure =
  1046. ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
  1047. check = MMC_BLK_PARTIAL;
  1048. }
  1049. pr_err("%s: packed cmd failed, nr %u, sectors %u, "
  1050. "failure index: %d\n",
  1051. req->rq_disk->disk_name, packed->nr_entries,
  1052. packed->blocks, packed->idx_failure);
  1053. }
  1054. free:
  1055. kfree(ext_csd);
  1056. }
  1057. return check;
  1058. }
  1059. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1060. struct mmc_card *card,
  1061. int disable_multi,
  1062. struct mmc_queue *mq)
  1063. {
  1064. u32 readcmd, writecmd;
  1065. struct mmc_blk_request *brq = &mqrq->brq;
  1066. struct request *req = mqrq->req;
  1067. struct mmc_blk_data *md = mq->data;
  1068. bool do_data_tag;
  1069. /*
  1070. * Reliable writes are used to implement Forced Unit Access and
  1071. * REQ_META accesses, and are supported only on MMCs.
  1072. *
  1073. * XXX: this really needs a good explanation of why REQ_META
  1074. * is treated special.
  1075. */
  1076. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  1077. (req->cmd_flags & REQ_META)) &&
  1078. (rq_data_dir(req) == WRITE) &&
  1079. (md->flags & MMC_BLK_REL_WR);
  1080. memset(brq, 0, sizeof(struct mmc_blk_request));
  1081. brq->mrq.cmd = &brq->cmd;
  1082. brq->mrq.data = &brq->data;
  1083. brq->cmd.arg = blk_rq_pos(req);
  1084. if (!mmc_card_blockaddr(card))
  1085. brq->cmd.arg <<= 9;
  1086. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1087. brq->data.blksz = 512;
  1088. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1089. brq->stop.arg = 0;
  1090. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1091. brq->data.blocks = blk_rq_sectors(req);
  1092. /*
  1093. * The block layer doesn't support all sector count
  1094. * restrictions, so we need to be prepared for too big
  1095. * requests.
  1096. */
  1097. if (brq->data.blocks > card->host->max_blk_count)
  1098. brq->data.blocks = card->host->max_blk_count;
  1099. if (brq->data.blocks > 1) {
  1100. /*
  1101. * After a read error, we redo the request one sector
  1102. * at a time in order to accurately determine which
  1103. * sectors can be read successfully.
  1104. */
  1105. if (disable_multi)
  1106. brq->data.blocks = 1;
  1107. /* Some controllers can't do multiblock reads due to hw bugs */
  1108. if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
  1109. rq_data_dir(req) == READ)
  1110. brq->data.blocks = 1;
  1111. }
  1112. if (brq->data.blocks > 1 || do_rel_wr) {
  1113. /* SPI multiblock writes terminate using a special
  1114. * token, not a STOP_TRANSMISSION request.
  1115. */
  1116. if (!mmc_host_is_spi(card->host) ||
  1117. rq_data_dir(req) == READ)
  1118. brq->mrq.stop = &brq->stop;
  1119. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1120. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1121. } else {
  1122. brq->mrq.stop = NULL;
  1123. readcmd = MMC_READ_SINGLE_BLOCK;
  1124. writecmd = MMC_WRITE_BLOCK;
  1125. }
  1126. if (rq_data_dir(req) == READ) {
  1127. brq->cmd.opcode = readcmd;
  1128. brq->data.flags |= MMC_DATA_READ;
  1129. } else {
  1130. brq->cmd.opcode = writecmd;
  1131. brq->data.flags |= MMC_DATA_WRITE;
  1132. }
  1133. if (do_rel_wr)
  1134. mmc_apply_rel_rw(brq, card, req);
  1135. /*
  1136. * Data tag is used only during writing meta data to speed
  1137. * up write and any subsequent read of this meta data
  1138. */
  1139. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1140. (req->cmd_flags & REQ_META) &&
  1141. (rq_data_dir(req) == WRITE) &&
  1142. ((brq->data.blocks * brq->data.blksz) >=
  1143. card->ext_csd.data_tag_unit_size);
  1144. /*
  1145. * Pre-defined multi-block transfers are preferable to
  1146. * open ended-ones (and necessary for reliable writes).
  1147. * However, it is not sufficient to just send CMD23,
  1148. * and avoid the final CMD12, as on an error condition
  1149. * CMD12 (stop) needs to be sent anyway. This, coupled
  1150. * with Auto-CMD23 enhancements provided by some
  1151. * hosts, means that the complexity of dealing
  1152. * with this is best left to the host. If CMD23 is
  1153. * supported by card and host, we'll fill sbc in and let
  1154. * the host deal with handling it correctly. This means
  1155. * that for hosts that don't expose MMC_CAP_CMD23, no
  1156. * change of behavior will be observed.
  1157. *
  1158. * N.B: Some MMC cards experience perf degradation.
  1159. * We'll avoid using CMD23-bounded multiblock writes for
  1160. * these, while retaining features like reliable writes.
  1161. */
  1162. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1163. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1164. do_data_tag)) {
  1165. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1166. brq->sbc.arg = brq->data.blocks |
  1167. (do_rel_wr ? (1 << 31) : 0) |
  1168. (do_data_tag ? (1 << 29) : 0);
  1169. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1170. brq->mrq.sbc = &brq->sbc;
  1171. }
  1172. mmc_set_data_timeout(&brq->data, card);
  1173. brq->data.sg = mqrq->sg;
  1174. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1175. /*
  1176. * Adjust the sg list so it is the same size as the
  1177. * request.
  1178. */
  1179. if (brq->data.blocks != blk_rq_sectors(req)) {
  1180. int i, data_size = brq->data.blocks << 9;
  1181. struct scatterlist *sg;
  1182. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1183. data_size -= sg->length;
  1184. if (data_size <= 0) {
  1185. sg->length += data_size;
  1186. i++;
  1187. break;
  1188. }
  1189. }
  1190. brq->data.sg_len = i;
  1191. }
  1192. mqrq->mmc_active.mrq = &brq->mrq;
  1193. mqrq->mmc_active.err_check = mmc_blk_err_check;
  1194. mmc_queue_bounce_pre(mqrq);
  1195. }
  1196. static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
  1197. struct mmc_card *card)
  1198. {
  1199. unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
  1200. unsigned int max_seg_sz = queue_max_segment_size(q);
  1201. unsigned int len, nr_segs = 0;
  1202. do {
  1203. len = min(hdr_sz, max_seg_sz);
  1204. hdr_sz -= len;
  1205. nr_segs++;
  1206. } while (hdr_sz);
  1207. return nr_segs;
  1208. }
  1209. static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
  1210. {
  1211. struct request_queue *q = mq->queue;
  1212. struct mmc_card *card = mq->card;
  1213. struct request *cur = req, *next = NULL;
  1214. struct mmc_blk_data *md = mq->data;
  1215. struct mmc_queue_req *mqrq = mq->mqrq_cur;
  1216. bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
  1217. unsigned int req_sectors = 0, phys_segments = 0;
  1218. unsigned int max_blk_count, max_phys_segs;
  1219. bool put_back = true;
  1220. u8 max_packed_rw = 0;
  1221. u8 reqs = 0;
  1222. if (!(md->flags & MMC_BLK_PACKED_CMD))
  1223. goto no_packed;
  1224. if ((rq_data_dir(cur) == WRITE) &&
  1225. mmc_host_packed_wr(card->host))
  1226. max_packed_rw = card->ext_csd.max_packed_writes;
  1227. if (max_packed_rw == 0)
  1228. goto no_packed;
  1229. if (mmc_req_rel_wr(cur) &&
  1230. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1231. goto no_packed;
  1232. if (mmc_large_sector(card) &&
  1233. !IS_ALIGNED(blk_rq_sectors(cur), 8))
  1234. goto no_packed;
  1235. mmc_blk_clear_packed(mqrq);
  1236. max_blk_count = min(card->host->max_blk_count,
  1237. card->host->max_req_size >> 9);
  1238. if (unlikely(max_blk_count > 0xffff))
  1239. max_blk_count = 0xffff;
  1240. max_phys_segs = queue_max_segments(q);
  1241. req_sectors += blk_rq_sectors(cur);
  1242. phys_segments += cur->nr_phys_segments;
  1243. if (rq_data_dir(cur) == WRITE) {
  1244. req_sectors += mmc_large_sector(card) ? 8 : 1;
  1245. phys_segments += mmc_calc_packed_hdr_segs(q, card);
  1246. }
  1247. do {
  1248. if (reqs >= max_packed_rw - 1) {
  1249. put_back = false;
  1250. break;
  1251. }
  1252. spin_lock_irq(q->queue_lock);
  1253. next = blk_fetch_request(q);
  1254. spin_unlock_irq(q->queue_lock);
  1255. if (!next) {
  1256. put_back = false;
  1257. break;
  1258. }
  1259. if (mmc_large_sector(card) &&
  1260. !IS_ALIGNED(blk_rq_sectors(next), 8))
  1261. break;
  1262. if (next->cmd_flags & REQ_DISCARD ||
  1263. next->cmd_flags & REQ_FLUSH)
  1264. break;
  1265. if (rq_data_dir(cur) != rq_data_dir(next))
  1266. break;
  1267. if (mmc_req_rel_wr(next) &&
  1268. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1269. break;
  1270. req_sectors += blk_rq_sectors(next);
  1271. if (req_sectors > max_blk_count)
  1272. break;
  1273. phys_segments += next->nr_phys_segments;
  1274. if (phys_segments > max_phys_segs)
  1275. break;
  1276. list_add_tail(&next->queuelist, &mqrq->packed->list);
  1277. cur = next;
  1278. reqs++;
  1279. } while (1);
  1280. if (put_back) {
  1281. spin_lock_irq(q->queue_lock);
  1282. blk_requeue_request(q, next);
  1283. spin_unlock_irq(q->queue_lock);
  1284. }
  1285. if (reqs > 0) {
  1286. list_add(&req->queuelist, &mqrq->packed->list);
  1287. mqrq->packed->nr_entries = ++reqs;
  1288. mqrq->packed->retries = reqs;
  1289. return reqs;
  1290. }
  1291. no_packed:
  1292. mqrq->cmd_type = MMC_PACKED_NONE;
  1293. return 0;
  1294. }
  1295. static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
  1296. struct mmc_card *card,
  1297. struct mmc_queue *mq)
  1298. {
  1299. struct mmc_blk_request *brq = &mqrq->brq;
  1300. struct request *req = mqrq->req;
  1301. struct request *prq;
  1302. struct mmc_blk_data *md = mq->data;
  1303. struct mmc_packed *packed = mqrq->packed;
  1304. bool do_rel_wr, do_data_tag;
  1305. u32 *packed_cmd_hdr;
  1306. u8 hdr_blocks;
  1307. u8 i = 1;
  1308. BUG_ON(!packed);
  1309. mqrq->cmd_type = MMC_PACKED_WRITE;
  1310. packed->blocks = 0;
  1311. packed->idx_failure = MMC_PACKED_NR_IDX;
  1312. packed_cmd_hdr = packed->cmd_hdr;
  1313. memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
  1314. packed_cmd_hdr[0] = (packed->nr_entries << 16) |
  1315. (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
  1316. hdr_blocks = mmc_large_sector(card) ? 8 : 1;
  1317. /*
  1318. * Argument for each entry of packed group
  1319. */
  1320. list_for_each_entry(prq, &packed->list, queuelist) {
  1321. do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
  1322. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1323. (prq->cmd_flags & REQ_META) &&
  1324. (rq_data_dir(prq) == WRITE) &&
  1325. ((brq->data.blocks * brq->data.blksz) >=
  1326. card->ext_csd.data_tag_unit_size);
  1327. /* Argument of CMD23 */
  1328. packed_cmd_hdr[(i * 2)] =
  1329. (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
  1330. (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
  1331. blk_rq_sectors(prq);
  1332. /* Argument of CMD18 or CMD25 */
  1333. packed_cmd_hdr[((i * 2)) + 1] =
  1334. mmc_card_blockaddr(card) ?
  1335. blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
  1336. packed->blocks += blk_rq_sectors(prq);
  1337. i++;
  1338. }
  1339. memset(brq, 0, sizeof(struct mmc_blk_request));
  1340. brq->mrq.cmd = &brq->cmd;
  1341. brq->mrq.data = &brq->data;
  1342. brq->mrq.sbc = &brq->sbc;
  1343. brq->mrq.stop = &brq->stop;
  1344. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1345. brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
  1346. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1347. brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
  1348. brq->cmd.arg = blk_rq_pos(req);
  1349. if (!mmc_card_blockaddr(card))
  1350. brq->cmd.arg <<= 9;
  1351. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1352. brq->data.blksz = 512;
  1353. brq->data.blocks = packed->blocks + hdr_blocks;
  1354. brq->data.flags |= MMC_DATA_WRITE;
  1355. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1356. brq->stop.arg = 0;
  1357. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1358. mmc_set_data_timeout(&brq->data, card);
  1359. brq->data.sg = mqrq->sg;
  1360. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1361. mqrq->mmc_active.mrq = &brq->mrq;
  1362. mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
  1363. mmc_queue_bounce_pre(mqrq);
  1364. }
  1365. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  1366. struct mmc_blk_request *brq, struct request *req,
  1367. int ret)
  1368. {
  1369. struct mmc_queue_req *mq_rq;
  1370. mq_rq = container_of(brq, struct mmc_queue_req, brq);
  1371. /*
  1372. * If this is an SD card and we're writing, we can first
  1373. * mark the known good sectors as ok.
  1374. *
  1375. * If the card is not SD, we can still ok written sectors
  1376. * as reported by the controller (which might be less than
  1377. * the real number of written sectors, but never more).
  1378. */
  1379. if (mmc_card_sd(card)) {
  1380. u32 blocks;
  1381. blocks = mmc_sd_num_wr_blocks(card);
  1382. if (blocks != (u32)-1) {
  1383. ret = blk_end_request(req, 0, blocks << 9);
  1384. }
  1385. } else {
  1386. if (!mmc_packed_cmd(mq_rq->cmd_type))
  1387. ret = blk_end_request(req, 0, brq->data.bytes_xfered);
  1388. }
  1389. return ret;
  1390. }
  1391. static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
  1392. {
  1393. struct request *prq;
  1394. struct mmc_packed *packed = mq_rq->packed;
  1395. int idx = packed->idx_failure, i = 0;
  1396. int ret = 0;
  1397. BUG_ON(!packed);
  1398. while (!list_empty(&packed->list)) {
  1399. prq = list_entry_rq(packed->list.next);
  1400. if (idx == i) {
  1401. /* retry from error index */
  1402. packed->nr_entries -= idx;
  1403. mq_rq->req = prq;
  1404. ret = 1;
  1405. if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
  1406. list_del_init(&prq->queuelist);
  1407. mmc_blk_clear_packed(mq_rq);
  1408. }
  1409. return ret;
  1410. }
  1411. list_del_init(&prq->queuelist);
  1412. blk_end_request(prq, 0, blk_rq_bytes(prq));
  1413. i++;
  1414. }
  1415. mmc_blk_clear_packed(mq_rq);
  1416. return ret;
  1417. }
  1418. static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
  1419. {
  1420. struct request *prq;
  1421. struct mmc_packed *packed = mq_rq->packed;
  1422. BUG_ON(!packed);
  1423. while (!list_empty(&packed->list)) {
  1424. prq = list_entry_rq(packed->list.next);
  1425. list_del_init(&prq->queuelist);
  1426. blk_end_request(prq, -EIO, blk_rq_bytes(prq));
  1427. }
  1428. mmc_blk_clear_packed(mq_rq);
  1429. }
  1430. static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
  1431. struct mmc_queue_req *mq_rq)
  1432. {
  1433. struct request *prq;
  1434. struct request_queue *q = mq->queue;
  1435. struct mmc_packed *packed = mq_rq->packed;
  1436. BUG_ON(!packed);
  1437. while (!list_empty(&packed->list)) {
  1438. prq = list_entry_rq(packed->list.prev);
  1439. if (prq->queuelist.prev != &packed->list) {
  1440. list_del_init(&prq->queuelist);
  1441. spin_lock_irq(q->queue_lock);
  1442. blk_requeue_request(mq->queue, prq);
  1443. spin_unlock_irq(q->queue_lock);
  1444. } else {
  1445. list_del_init(&prq->queuelist);
  1446. }
  1447. }
  1448. mmc_blk_clear_packed(mq_rq);
  1449. }
  1450. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  1451. {
  1452. struct mmc_blk_data *md = mq->data;
  1453. struct mmc_card *card = md->queue.card;
  1454. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  1455. int ret = 1, disable_multi = 0, retry = 0, type;
  1456. enum mmc_blk_status status;
  1457. struct mmc_queue_req *mq_rq;
  1458. struct request *req = rqc;
  1459. struct mmc_async_req *areq;
  1460. const u8 packed_nr = 2;
  1461. u8 reqs = 0;
  1462. if (!rqc && !mq->mqrq_prev->req)
  1463. return 0;
  1464. if (rqc)
  1465. reqs = mmc_blk_prep_packed_list(mq, rqc);
  1466. do {
  1467. if (rqc) {
  1468. /*
  1469. * When 4KB native sector is enabled, only 8 blocks
  1470. * multiple read or write is allowed
  1471. */
  1472. if ((brq->data.blocks & 0x07) &&
  1473. (card->ext_csd.data_sector_size == 4096)) {
  1474. pr_err("%s: Transfer size is not 4KB sector size aligned\n",
  1475. req->rq_disk->disk_name);
  1476. mq_rq = mq->mqrq_cur;
  1477. goto cmd_abort;
  1478. }
  1479. if (reqs >= packed_nr)
  1480. mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
  1481. card, mq);
  1482. else
  1483. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1484. areq = &mq->mqrq_cur->mmc_active;
  1485. } else
  1486. areq = NULL;
  1487. areq = mmc_start_req(card->host, areq, (int *) &status);
  1488. if (!areq) {
  1489. if (status == MMC_BLK_NEW_REQUEST)
  1490. mq->flags |= MMC_QUEUE_NEW_REQUEST;
  1491. return 0;
  1492. }
  1493. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  1494. brq = &mq_rq->brq;
  1495. req = mq_rq->req;
  1496. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1497. mmc_queue_bounce_post(mq_rq);
  1498. switch (status) {
  1499. case MMC_BLK_SUCCESS:
  1500. case MMC_BLK_PARTIAL:
  1501. /*
  1502. * A block was successfully transferred.
  1503. */
  1504. mmc_blk_reset_success(md, type);
  1505. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1506. ret = mmc_blk_end_packed_req(mq_rq);
  1507. break;
  1508. } else {
  1509. ret = blk_end_request(req, 0,
  1510. brq->data.bytes_xfered);
  1511. }
  1512. /*
  1513. * If the blk_end_request function returns non-zero even
  1514. * though all data has been transferred and no errors
  1515. * were returned by the host controller, it's a bug.
  1516. */
  1517. if (status == MMC_BLK_SUCCESS && ret) {
  1518. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1519. __func__, blk_rq_bytes(req),
  1520. brq->data.bytes_xfered);
  1521. rqc = NULL;
  1522. goto cmd_abort;
  1523. }
  1524. break;
  1525. case MMC_BLK_CMD_ERR:
  1526. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1527. if (!mmc_blk_reset(md, card->host, type))
  1528. break;
  1529. goto cmd_abort;
  1530. case MMC_BLK_RETRY:
  1531. if (retry++ < 5)
  1532. break;
  1533. /* Fall through */
  1534. case MMC_BLK_ABORT:
  1535. if (!mmc_blk_reset(md, card->host, type))
  1536. break;
  1537. goto cmd_abort;
  1538. case MMC_BLK_DATA_ERR: {
  1539. int err;
  1540. err = mmc_blk_reset(md, card->host, type);
  1541. if (!err)
  1542. break;
  1543. if (err == -ENODEV ||
  1544. mmc_packed_cmd(mq_rq->cmd_type))
  1545. goto cmd_abort;
  1546. /* Fall through */
  1547. }
  1548. case MMC_BLK_ECC_ERR:
  1549. if (brq->data.blocks > 1) {
  1550. /* Redo read one sector at a time */
  1551. pr_warning("%s: retrying using single block read\n",
  1552. req->rq_disk->disk_name);
  1553. disable_multi = 1;
  1554. break;
  1555. }
  1556. /*
  1557. * After an error, we redo I/O one sector at a
  1558. * time, so we only reach here after trying to
  1559. * read a single sector.
  1560. */
  1561. ret = blk_end_request(req, -EIO,
  1562. brq->data.blksz);
  1563. if (!ret)
  1564. goto start_new_req;
  1565. break;
  1566. case MMC_BLK_NOMEDIUM:
  1567. goto cmd_abort;
  1568. default:
  1569. pr_err("%s: Unhandled return value (%d)",
  1570. req->rq_disk->disk_name, status);
  1571. goto cmd_abort;
  1572. }
  1573. if (ret) {
  1574. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1575. if (!mq_rq->packed->retries)
  1576. goto cmd_abort;
  1577. mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
  1578. mmc_start_req(card->host,
  1579. &mq_rq->mmc_active, NULL);
  1580. } else {
  1581. /*
  1582. * In case of a incomplete request
  1583. * prepare it again and resend.
  1584. */
  1585. mmc_blk_rw_rq_prep(mq_rq, card,
  1586. disable_multi, mq);
  1587. mmc_start_req(card->host,
  1588. &mq_rq->mmc_active, NULL);
  1589. }
  1590. }
  1591. } while (ret);
  1592. return 1;
  1593. cmd_abort:
  1594. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1595. mmc_blk_abort_packed_req(mq_rq);
  1596. } else {
  1597. if (mmc_card_removed(card))
  1598. req->cmd_flags |= REQ_QUIET;
  1599. while (ret)
  1600. ret = blk_end_request(req, -EIO,
  1601. blk_rq_cur_bytes(req));
  1602. }
  1603. start_new_req:
  1604. if (rqc) {
  1605. if (mmc_card_removed(card)) {
  1606. rqc->cmd_flags |= REQ_QUIET;
  1607. blk_end_request_all(rqc, -EIO);
  1608. } else {
  1609. /*
  1610. * If current request is packed, it needs to put back.
  1611. */
  1612. if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
  1613. mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
  1614. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1615. mmc_start_req(card->host,
  1616. &mq->mqrq_cur->mmc_active, NULL);
  1617. }
  1618. }
  1619. return 0;
  1620. }
  1621. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1622. {
  1623. int ret;
  1624. struct mmc_blk_data *md = mq->data;
  1625. struct mmc_card *card = md->queue.card;
  1626. struct mmc_host *host = card->host;
  1627. unsigned long flags;
  1628. if (req && !mq->mqrq_prev->req)
  1629. /* claim host only for the first request */
  1630. mmc_get_card(card);
  1631. ret = mmc_blk_part_switch(card, md);
  1632. if (ret) {
  1633. if (req) {
  1634. blk_end_request_all(req, -EIO);
  1635. }
  1636. ret = 0;
  1637. goto out;
  1638. }
  1639. mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
  1640. if (req && req->cmd_flags & REQ_DISCARD) {
  1641. /* complete ongoing async transfer before issuing discard */
  1642. if (card->host->areq)
  1643. mmc_blk_issue_rw_rq(mq, NULL);
  1644. if (req->cmd_flags & REQ_SECURE &&
  1645. !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
  1646. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1647. else
  1648. ret = mmc_blk_issue_discard_rq(mq, req);
  1649. } else if (req && req->cmd_flags & REQ_FLUSH) {
  1650. /* complete ongoing async transfer before issuing flush */
  1651. if (card->host->areq)
  1652. mmc_blk_issue_rw_rq(mq, NULL);
  1653. ret = mmc_blk_issue_flush(mq, req);
  1654. } else {
  1655. if (!req && host->areq) {
  1656. spin_lock_irqsave(&host->context_info.lock, flags);
  1657. host->context_info.is_waiting_last_req = true;
  1658. spin_unlock_irqrestore(&host->context_info.lock, flags);
  1659. }
  1660. ret = mmc_blk_issue_rw_rq(mq, req);
  1661. }
  1662. out:
  1663. if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
  1664. (req && (req->cmd_flags & MMC_REQ_SPECIAL_MASK)))
  1665. /*
  1666. * Release host when there are no more requests
  1667. * and after special request(discard, flush) is done.
  1668. * In case sepecial request, there is no reentry to
  1669. * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
  1670. */
  1671. mmc_put_card(card);
  1672. return ret;
  1673. }
  1674. static inline int mmc_blk_readonly(struct mmc_card *card)
  1675. {
  1676. return mmc_card_readonly(card) ||
  1677. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1678. }
  1679. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1680. struct device *parent,
  1681. sector_t size,
  1682. bool default_ro,
  1683. const char *subname,
  1684. int area_type)
  1685. {
  1686. struct mmc_blk_data *md;
  1687. int devidx, ret;
  1688. devidx = find_first_zero_bit(dev_use, max_devices);
  1689. if (devidx >= max_devices)
  1690. return ERR_PTR(-ENOSPC);
  1691. __set_bit(devidx, dev_use);
  1692. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1693. if (!md) {
  1694. ret = -ENOMEM;
  1695. goto out;
  1696. }
  1697. /*
  1698. * !subname implies we are creating main mmc_blk_data that will be
  1699. * associated with mmc_card with mmc_set_drvdata. Due to device
  1700. * partitions, devidx will not coincide with a per-physical card
  1701. * index anymore so we keep track of a name index.
  1702. */
  1703. if (!subname) {
  1704. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1705. __set_bit(md->name_idx, name_use);
  1706. } else
  1707. md->name_idx = ((struct mmc_blk_data *)
  1708. dev_to_disk(parent)->private_data)->name_idx;
  1709. md->area_type = area_type;
  1710. /*
  1711. * Set the read-only status based on the supported commands
  1712. * and the write protect switch.
  1713. */
  1714. md->read_only = mmc_blk_readonly(card);
  1715. md->disk = alloc_disk(perdev_minors);
  1716. if (md->disk == NULL) {
  1717. ret = -ENOMEM;
  1718. goto err_kfree;
  1719. }
  1720. spin_lock_init(&md->lock);
  1721. INIT_LIST_HEAD(&md->part);
  1722. md->usage = 1;
  1723. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1724. if (ret)
  1725. goto err_putdisk;
  1726. md->queue.issue_fn = mmc_blk_issue_rq;
  1727. md->queue.data = md;
  1728. md->disk->major = MMC_BLOCK_MAJOR;
  1729. md->disk->first_minor = devidx * perdev_minors;
  1730. md->disk->fops = &mmc_bdops;
  1731. md->disk->private_data = md;
  1732. md->disk->queue = md->queue.queue;
  1733. md->disk->driverfs_dev = parent;
  1734. set_disk_ro(md->disk, md->read_only || default_ro);
  1735. if (area_type & MMC_BLK_DATA_AREA_RPMB)
  1736. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  1737. /*
  1738. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1739. *
  1740. * - be set for removable media with permanent block devices
  1741. * - be unset for removable block devices with permanent media
  1742. *
  1743. * Since MMC block devices clearly fall under the second
  1744. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1745. * should use the block device creation/destruction hotplug
  1746. * messages to tell when the card is present.
  1747. */
  1748. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1749. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1750. if (mmc_card_mmc(card))
  1751. blk_queue_logical_block_size(md->queue.queue,
  1752. card->ext_csd.data_sector_size);
  1753. else
  1754. blk_queue_logical_block_size(md->queue.queue, 512);
  1755. set_capacity(md->disk, size);
  1756. if (mmc_host_cmd23(card->host)) {
  1757. if (mmc_card_mmc(card) ||
  1758. (mmc_card_sd(card) &&
  1759. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1760. md->flags |= MMC_BLK_CMD23;
  1761. }
  1762. if (mmc_card_mmc(card) &&
  1763. md->flags & MMC_BLK_CMD23 &&
  1764. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1765. card->ext_csd.rel_sectors)) {
  1766. md->flags |= MMC_BLK_REL_WR;
  1767. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1768. }
  1769. if (mmc_card_mmc(card) &&
  1770. (area_type == MMC_BLK_DATA_AREA_MAIN) &&
  1771. (md->flags & MMC_BLK_CMD23) &&
  1772. card->ext_csd.packed_event_en) {
  1773. if (!mmc_packed_init(&md->queue, card))
  1774. md->flags |= MMC_BLK_PACKED_CMD;
  1775. }
  1776. return md;
  1777. err_putdisk:
  1778. put_disk(md->disk);
  1779. err_kfree:
  1780. kfree(md);
  1781. out:
  1782. return ERR_PTR(ret);
  1783. }
  1784. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1785. {
  1786. sector_t size;
  1787. struct mmc_blk_data *md;
  1788. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1789. /*
  1790. * The EXT_CSD sector count is in number or 512 byte
  1791. * sectors.
  1792. */
  1793. size = card->ext_csd.sectors;
  1794. } else {
  1795. /*
  1796. * The CSD capacity field is in units of read_blkbits.
  1797. * set_capacity takes units of 512 bytes.
  1798. */
  1799. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1800. }
  1801. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  1802. MMC_BLK_DATA_AREA_MAIN);
  1803. return md;
  1804. }
  1805. static int mmc_blk_alloc_part(struct mmc_card *card,
  1806. struct mmc_blk_data *md,
  1807. unsigned int part_type,
  1808. sector_t size,
  1809. bool default_ro,
  1810. const char *subname,
  1811. int area_type)
  1812. {
  1813. char cap_str[10];
  1814. struct mmc_blk_data *part_md;
  1815. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1816. subname, area_type);
  1817. if (IS_ERR(part_md))
  1818. return PTR_ERR(part_md);
  1819. part_md->part_type = part_type;
  1820. list_add(&part_md->part, &md->part);
  1821. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1822. cap_str, sizeof(cap_str));
  1823. pr_info("%s: %s %s partition %u %s\n",
  1824. part_md->disk->disk_name, mmc_card_id(card),
  1825. mmc_card_name(card), part_md->part_type, cap_str);
  1826. return 0;
  1827. }
  1828. /* MMC Physical partitions consist of two boot partitions and
  1829. * up to four general purpose partitions.
  1830. * For each partition enabled in EXT_CSD a block device will be allocatedi
  1831. * to provide access to the partition.
  1832. */
  1833. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1834. {
  1835. int idx, ret = 0;
  1836. if (!mmc_card_mmc(card))
  1837. return 0;
  1838. for (idx = 0; idx < card->nr_parts; idx++) {
  1839. if (card->part[idx].size) {
  1840. ret = mmc_blk_alloc_part(card, md,
  1841. card->part[idx].part_cfg,
  1842. card->part[idx].size >> 9,
  1843. card->part[idx].force_ro,
  1844. card->part[idx].name,
  1845. card->part[idx].area_type);
  1846. if (ret)
  1847. return ret;
  1848. }
  1849. }
  1850. return ret;
  1851. }
  1852. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1853. {
  1854. struct mmc_card *card;
  1855. if (md) {
  1856. /*
  1857. * Flush remaining requests and free queues. It
  1858. * is freeing the queue that stops new requests
  1859. * from being accepted.
  1860. */
  1861. mmc_cleanup_queue(&md->queue);
  1862. if (md->flags & MMC_BLK_PACKED_CMD)
  1863. mmc_packed_clean(&md->queue);
  1864. card = md->queue.card;
  1865. if (md->disk->flags & GENHD_FL_UP) {
  1866. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1867. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1868. card->ext_csd.boot_ro_lockable)
  1869. device_remove_file(disk_to_dev(md->disk),
  1870. &md->power_ro_lock);
  1871. del_gendisk(md->disk);
  1872. }
  1873. mmc_blk_put(md);
  1874. }
  1875. }
  1876. static void mmc_blk_remove_parts(struct mmc_card *card,
  1877. struct mmc_blk_data *md)
  1878. {
  1879. struct list_head *pos, *q;
  1880. struct mmc_blk_data *part_md;
  1881. __clear_bit(md->name_idx, name_use);
  1882. list_for_each_safe(pos, q, &md->part) {
  1883. part_md = list_entry(pos, struct mmc_blk_data, part);
  1884. list_del(pos);
  1885. mmc_blk_remove_req(part_md);
  1886. }
  1887. }
  1888. static int mmc_add_disk(struct mmc_blk_data *md)
  1889. {
  1890. int ret;
  1891. struct mmc_card *card = md->queue.card;
  1892. add_disk(md->disk);
  1893. md->force_ro.show = force_ro_show;
  1894. md->force_ro.store = force_ro_store;
  1895. sysfs_attr_init(&md->force_ro.attr);
  1896. md->force_ro.attr.name = "force_ro";
  1897. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1898. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1899. if (ret)
  1900. goto force_ro_fail;
  1901. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1902. card->ext_csd.boot_ro_lockable) {
  1903. umode_t mode;
  1904. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  1905. mode = S_IRUGO;
  1906. else
  1907. mode = S_IRUGO | S_IWUSR;
  1908. md->power_ro_lock.show = power_ro_lock_show;
  1909. md->power_ro_lock.store = power_ro_lock_store;
  1910. sysfs_attr_init(&md->power_ro_lock.attr);
  1911. md->power_ro_lock.attr.mode = mode;
  1912. md->power_ro_lock.attr.name =
  1913. "ro_lock_until_next_power_on";
  1914. ret = device_create_file(disk_to_dev(md->disk),
  1915. &md->power_ro_lock);
  1916. if (ret)
  1917. goto power_ro_lock_fail;
  1918. }
  1919. return ret;
  1920. power_ro_lock_fail:
  1921. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1922. force_ro_fail:
  1923. del_gendisk(md->disk);
  1924. return ret;
  1925. }
  1926. #define CID_MANFID_SANDISK 0x2
  1927. #define CID_MANFID_TOSHIBA 0x11
  1928. #define CID_MANFID_MICRON 0x13
  1929. #define CID_MANFID_SAMSUNG 0x15
  1930. static const struct mmc_fixup blk_fixups[] =
  1931. {
  1932. MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1933. MMC_QUIRK_INAND_CMD38),
  1934. MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1935. MMC_QUIRK_INAND_CMD38),
  1936. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1937. MMC_QUIRK_INAND_CMD38),
  1938. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1939. MMC_QUIRK_INAND_CMD38),
  1940. MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1941. MMC_QUIRK_INAND_CMD38),
  1942. /*
  1943. * Some MMC cards experience performance degradation with CMD23
  1944. * instead of CMD12-bounded multiblock transfers. For now we'll
  1945. * black list what's bad...
  1946. * - Certain Toshiba cards.
  1947. *
  1948. * N.B. This doesn't affect SD cards.
  1949. */
  1950. MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1951. MMC_QUIRK_BLK_NO_CMD23),
  1952. MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1953. MMC_QUIRK_BLK_NO_CMD23),
  1954. MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1955. MMC_QUIRK_BLK_NO_CMD23),
  1956. /*
  1957. * Some Micron MMC cards needs longer data read timeout than
  1958. * indicated in CSD.
  1959. */
  1960. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
  1961. MMC_QUIRK_LONG_READ_TIME),
  1962. /*
  1963. * On these Samsung MoviNAND parts, performing secure erase or
  1964. * secure trim can result in unrecoverable corruption due to a
  1965. * firmware bug.
  1966. */
  1967. MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1968. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1969. MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1970. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1971. MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1972. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1973. MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1974. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1975. MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1976. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1977. MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1978. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1979. MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1980. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1981. MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1982. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1983. END_FIXUP
  1984. };
  1985. static int mmc_blk_probe(struct mmc_card *card)
  1986. {
  1987. struct mmc_blk_data *md, *part_md;
  1988. char cap_str[10];
  1989. /*
  1990. * Check that the card supports the command class(es) we need.
  1991. */
  1992. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1993. return -ENODEV;
  1994. md = mmc_blk_alloc(card);
  1995. if (IS_ERR(md))
  1996. return PTR_ERR(md);
  1997. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1998. cap_str, sizeof(cap_str));
  1999. pr_info("%s: %s %s %s %s\n",
  2000. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  2001. cap_str, md->read_only ? "(ro)" : "");
  2002. if (mmc_blk_alloc_parts(card, md))
  2003. goto out;
  2004. mmc_set_drvdata(card, md);
  2005. mmc_fixup_device(card, blk_fixups);
  2006. if (mmc_add_disk(md))
  2007. goto out;
  2008. list_for_each_entry(part_md, &md->part, part) {
  2009. if (mmc_add_disk(part_md))
  2010. goto out;
  2011. }
  2012. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  2013. pm_runtime_use_autosuspend(&card->dev);
  2014. /*
  2015. * Don't enable runtime PM for SD-combo cards here. Leave that
  2016. * decision to be taken during the SDIO init sequence instead.
  2017. */
  2018. if (card->type != MMC_TYPE_SD_COMBO) {
  2019. pm_runtime_set_active(&card->dev);
  2020. pm_runtime_enable(&card->dev);
  2021. }
  2022. return 0;
  2023. out:
  2024. mmc_blk_remove_parts(card, md);
  2025. mmc_blk_remove_req(md);
  2026. return 0;
  2027. }
  2028. static void mmc_blk_remove(struct mmc_card *card)
  2029. {
  2030. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2031. mmc_blk_remove_parts(card, md);
  2032. pm_runtime_get_sync(&card->dev);
  2033. mmc_claim_host(card->host);
  2034. mmc_blk_part_switch(card, md);
  2035. mmc_release_host(card->host);
  2036. if (card->type != MMC_TYPE_SD_COMBO)
  2037. pm_runtime_disable(&card->dev);
  2038. pm_runtime_put_noidle(&card->dev);
  2039. mmc_blk_remove_req(md);
  2040. mmc_set_drvdata(card, NULL);
  2041. }
  2042. #ifdef CONFIG_PM
  2043. static int mmc_blk_suspend(struct mmc_card *card)
  2044. {
  2045. struct mmc_blk_data *part_md;
  2046. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2047. if (md) {
  2048. pm_runtime_get_sync(&card->dev);
  2049. mmc_queue_suspend(&md->queue);
  2050. list_for_each_entry(part_md, &md->part, part) {
  2051. mmc_queue_suspend(&part_md->queue);
  2052. }
  2053. }
  2054. return 0;
  2055. }
  2056. static int mmc_blk_resume(struct mmc_card *card)
  2057. {
  2058. struct mmc_blk_data *part_md;
  2059. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2060. if (md) {
  2061. /*
  2062. * Resume involves the card going into idle state,
  2063. * so current partition is always the main one.
  2064. */
  2065. md->part_curr = md->part_type;
  2066. mmc_queue_resume(&md->queue);
  2067. list_for_each_entry(part_md, &md->part, part) {
  2068. mmc_queue_resume(&part_md->queue);
  2069. }
  2070. pm_runtime_put(&card->dev);
  2071. }
  2072. return 0;
  2073. }
  2074. #else
  2075. #define mmc_blk_suspend NULL
  2076. #define mmc_blk_resume NULL
  2077. #endif
  2078. static struct mmc_driver mmc_driver = {
  2079. .drv = {
  2080. .name = "mmcblk",
  2081. },
  2082. .probe = mmc_blk_probe,
  2083. .remove = mmc_blk_remove,
  2084. .suspend = mmc_blk_suspend,
  2085. .resume = mmc_blk_resume,
  2086. };
  2087. static int __init mmc_blk_init(void)
  2088. {
  2089. int res;
  2090. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2091. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2092. max_devices = 256 / perdev_minors;
  2093. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2094. if (res)
  2095. goto out;
  2096. res = mmc_register_driver(&mmc_driver);
  2097. if (res)
  2098. goto out2;
  2099. return 0;
  2100. out2:
  2101. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2102. out:
  2103. return res;
  2104. }
  2105. static void __exit mmc_blk_exit(void)
  2106. {
  2107. mmc_unregister_driver(&mmc_driver);
  2108. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2109. }
  2110. module_init(mmc_blk_init);
  2111. module_exit(mmc_blk_exit);
  2112. MODULE_LICENSE("GPL");
  2113. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");