block.c 50 KB

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