block.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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/system.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. 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. /*
  85. * Only set in main mmc_blk_data associated
  86. * with mmc_card with mmc_set_drvdata, and keeps
  87. * track of the current selected device partition.
  88. */
  89. unsigned int part_curr;
  90. struct device_attribute force_ro;
  91. };
  92. static DEFINE_MUTEX(open_lock);
  93. module_param(perdev_minors, int, 0444);
  94. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  95. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  96. {
  97. struct mmc_blk_data *md;
  98. mutex_lock(&open_lock);
  99. md = disk->private_data;
  100. if (md && md->usage == 0)
  101. md = NULL;
  102. if (md)
  103. md->usage++;
  104. mutex_unlock(&open_lock);
  105. return md;
  106. }
  107. static inline int mmc_get_devidx(struct gendisk *disk)
  108. {
  109. int devmaj = MAJOR(disk_devt(disk));
  110. int devidx = MINOR(disk_devt(disk)) / perdev_minors;
  111. if (!devmaj)
  112. devidx = disk->first_minor / perdev_minors;
  113. return devidx;
  114. }
  115. static void mmc_blk_put(struct mmc_blk_data *md)
  116. {
  117. mutex_lock(&open_lock);
  118. md->usage--;
  119. if (md->usage == 0) {
  120. int devidx = mmc_get_devidx(md->disk);
  121. blk_cleanup_queue(md->queue.queue);
  122. __clear_bit(devidx, dev_use);
  123. put_disk(md->disk);
  124. kfree(md);
  125. }
  126. mutex_unlock(&open_lock);
  127. }
  128. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  129. char *buf)
  130. {
  131. int ret;
  132. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  133. ret = snprintf(buf, PAGE_SIZE, "%d",
  134. get_disk_ro(dev_to_disk(dev)) ^
  135. md->read_only);
  136. mmc_blk_put(md);
  137. return ret;
  138. }
  139. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  140. const char *buf, size_t count)
  141. {
  142. int ret;
  143. char *end;
  144. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  145. unsigned long set = simple_strtoul(buf, &end, 0);
  146. if (end == buf) {
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  151. ret = count;
  152. out:
  153. mmc_blk_put(md);
  154. return ret;
  155. }
  156. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  157. {
  158. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  159. int ret = -ENXIO;
  160. mutex_lock(&block_mutex);
  161. if (md) {
  162. if (md->usage == 2)
  163. check_disk_change(bdev);
  164. ret = 0;
  165. if ((mode & FMODE_WRITE) && md->read_only) {
  166. mmc_blk_put(md);
  167. ret = -EROFS;
  168. }
  169. }
  170. mutex_unlock(&block_mutex);
  171. return ret;
  172. }
  173. static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
  174. {
  175. struct mmc_blk_data *md = disk->private_data;
  176. mutex_lock(&block_mutex);
  177. mmc_blk_put(md);
  178. mutex_unlock(&block_mutex);
  179. return 0;
  180. }
  181. static int
  182. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  183. {
  184. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  185. geo->heads = 4;
  186. geo->sectors = 16;
  187. return 0;
  188. }
  189. struct mmc_blk_ioc_data {
  190. struct mmc_ioc_cmd ic;
  191. unsigned char *buf;
  192. u64 buf_bytes;
  193. };
  194. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  195. struct mmc_ioc_cmd __user *user)
  196. {
  197. struct mmc_blk_ioc_data *idata;
  198. int err;
  199. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  200. if (!idata) {
  201. err = -ENOMEM;
  202. goto out;
  203. }
  204. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  205. err = -EFAULT;
  206. goto idata_err;
  207. }
  208. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  209. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  210. err = -EOVERFLOW;
  211. goto idata_err;
  212. }
  213. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  214. if (!idata->buf) {
  215. err = -ENOMEM;
  216. goto idata_err;
  217. }
  218. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  219. idata->ic.data_ptr, idata->buf_bytes)) {
  220. err = -EFAULT;
  221. goto copy_err;
  222. }
  223. return idata;
  224. copy_err:
  225. kfree(idata->buf);
  226. idata_err:
  227. kfree(idata);
  228. out:
  229. return ERR_PTR(err);
  230. }
  231. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  232. struct mmc_ioc_cmd __user *ic_ptr)
  233. {
  234. struct mmc_blk_ioc_data *idata;
  235. struct mmc_blk_data *md;
  236. struct mmc_card *card;
  237. struct mmc_command cmd = {0};
  238. struct mmc_data data = {0};
  239. struct mmc_request mrq = {0};
  240. struct scatterlist sg;
  241. int err;
  242. /*
  243. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  244. * whole block device, not on a partition. This prevents overspray
  245. * between sibling partitions.
  246. */
  247. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  248. return -EPERM;
  249. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  250. if (IS_ERR(idata))
  251. return PTR_ERR(idata);
  252. cmd.opcode = idata->ic.opcode;
  253. cmd.arg = idata->ic.arg;
  254. cmd.flags = idata->ic.flags;
  255. data.sg = &sg;
  256. data.sg_len = 1;
  257. data.blksz = idata->ic.blksz;
  258. data.blocks = idata->ic.blocks;
  259. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  260. if (idata->ic.write_flag)
  261. data.flags = MMC_DATA_WRITE;
  262. else
  263. data.flags = MMC_DATA_READ;
  264. mrq.cmd = &cmd;
  265. mrq.data = &data;
  266. md = mmc_blk_get(bdev->bd_disk);
  267. if (!md) {
  268. err = -EINVAL;
  269. goto cmd_done;
  270. }
  271. card = md->queue.card;
  272. if (IS_ERR(card)) {
  273. err = PTR_ERR(card);
  274. goto cmd_done;
  275. }
  276. mmc_claim_host(card->host);
  277. if (idata->ic.is_acmd) {
  278. err = mmc_app_cmd(card->host, card);
  279. if (err)
  280. goto cmd_rel_host;
  281. }
  282. /* data.flags must already be set before doing this. */
  283. mmc_set_data_timeout(&data, card);
  284. /* Allow overriding the timeout_ns for empirical tuning. */
  285. if (idata->ic.data_timeout_ns)
  286. data.timeout_ns = idata->ic.data_timeout_ns;
  287. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  288. /*
  289. * Pretend this is a data transfer and rely on the host driver
  290. * to compute timeout. When all host drivers support
  291. * cmd.cmd_timeout for R1B, this can be changed to:
  292. *
  293. * mrq.data = NULL;
  294. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  295. */
  296. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  297. }
  298. mmc_wait_for_req(card->host, &mrq);
  299. if (cmd.error) {
  300. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  301. __func__, cmd.error);
  302. err = cmd.error;
  303. goto cmd_rel_host;
  304. }
  305. if (data.error) {
  306. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  307. __func__, data.error);
  308. err = data.error;
  309. goto cmd_rel_host;
  310. }
  311. /*
  312. * According to the SD specs, some commands require a delay after
  313. * issuing the command.
  314. */
  315. if (idata->ic.postsleep_min_us)
  316. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  317. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  318. err = -EFAULT;
  319. goto cmd_rel_host;
  320. }
  321. if (!idata->ic.write_flag) {
  322. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  323. idata->buf, idata->buf_bytes)) {
  324. err = -EFAULT;
  325. goto cmd_rel_host;
  326. }
  327. }
  328. cmd_rel_host:
  329. mmc_release_host(card->host);
  330. cmd_done:
  331. mmc_blk_put(md);
  332. kfree(idata->buf);
  333. kfree(idata);
  334. return err;
  335. }
  336. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  337. unsigned int cmd, unsigned long arg)
  338. {
  339. int ret = -EINVAL;
  340. if (cmd == MMC_IOC_CMD)
  341. ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
  342. return ret;
  343. }
  344. #ifdef CONFIG_COMPAT
  345. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  346. unsigned int cmd, unsigned long arg)
  347. {
  348. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  349. }
  350. #endif
  351. static const struct block_device_operations mmc_bdops = {
  352. .open = mmc_blk_open,
  353. .release = mmc_blk_release,
  354. .getgeo = mmc_blk_getgeo,
  355. .owner = THIS_MODULE,
  356. .ioctl = mmc_blk_ioctl,
  357. #ifdef CONFIG_COMPAT
  358. .compat_ioctl = mmc_blk_compat_ioctl,
  359. #endif
  360. };
  361. struct mmc_blk_request {
  362. struct mmc_request mrq;
  363. struct mmc_command sbc;
  364. struct mmc_command cmd;
  365. struct mmc_command stop;
  366. struct mmc_data data;
  367. };
  368. static inline int mmc_blk_part_switch(struct mmc_card *card,
  369. struct mmc_blk_data *md)
  370. {
  371. int ret;
  372. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  373. if (main_md->part_curr == md->part_type)
  374. return 0;
  375. if (mmc_card_mmc(card)) {
  376. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  377. card->ext_csd.part_config |= md->part_type;
  378. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  379. EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
  380. card->ext_csd.part_time);
  381. if (ret)
  382. return ret;
  383. }
  384. main_md->part_curr = md->part_type;
  385. return 0;
  386. }
  387. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  388. {
  389. int err;
  390. u32 result;
  391. __be32 *blocks;
  392. struct mmc_request mrq = {0};
  393. struct mmc_command cmd = {0};
  394. struct mmc_data data = {0};
  395. unsigned int timeout_us;
  396. struct scatterlist sg;
  397. cmd.opcode = MMC_APP_CMD;
  398. cmd.arg = card->rca << 16;
  399. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  400. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  401. if (err)
  402. return (u32)-1;
  403. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  404. return (u32)-1;
  405. memset(&cmd, 0, sizeof(struct mmc_command));
  406. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  407. cmd.arg = 0;
  408. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  409. data.timeout_ns = card->csd.tacc_ns * 100;
  410. data.timeout_clks = card->csd.tacc_clks * 100;
  411. timeout_us = data.timeout_ns / 1000;
  412. timeout_us += data.timeout_clks * 1000 /
  413. (card->host->ios.clock / 1000);
  414. if (timeout_us > 100000) {
  415. data.timeout_ns = 100000000;
  416. data.timeout_clks = 0;
  417. }
  418. data.blksz = 4;
  419. data.blocks = 1;
  420. data.flags = MMC_DATA_READ;
  421. data.sg = &sg;
  422. data.sg_len = 1;
  423. mrq.cmd = &cmd;
  424. mrq.data = &data;
  425. blocks = kmalloc(4, GFP_KERNEL);
  426. if (!blocks)
  427. return (u32)-1;
  428. sg_init_one(&sg, blocks, 4);
  429. mmc_wait_for_req(card->host, &mrq);
  430. result = ntohl(*blocks);
  431. kfree(blocks);
  432. if (cmd.error || data.error)
  433. result = (u32)-1;
  434. return result;
  435. }
  436. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  437. {
  438. struct mmc_command cmd = {0};
  439. int err;
  440. cmd.opcode = MMC_SEND_STATUS;
  441. if (!mmc_host_is_spi(card->host))
  442. cmd.arg = card->rca << 16;
  443. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  444. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  445. if (err == 0)
  446. *status = cmd.resp[0];
  447. return err;
  448. }
  449. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  450. {
  451. struct mmc_blk_data *md = mq->data;
  452. struct mmc_card *card = md->queue.card;
  453. unsigned int from, nr, arg;
  454. int err = 0;
  455. if (!mmc_can_erase(card)) {
  456. err = -EOPNOTSUPP;
  457. goto out;
  458. }
  459. from = blk_rq_pos(req);
  460. nr = blk_rq_sectors(req);
  461. if (mmc_can_trim(card))
  462. arg = MMC_TRIM_ARG;
  463. else
  464. arg = MMC_ERASE_ARG;
  465. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  466. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  467. INAND_CMD38_ARG_EXT_CSD,
  468. arg == MMC_TRIM_ARG ?
  469. INAND_CMD38_ARG_TRIM :
  470. INAND_CMD38_ARG_ERASE,
  471. 0);
  472. if (err)
  473. goto out;
  474. }
  475. err = mmc_erase(card, from, nr, arg);
  476. out:
  477. spin_lock_irq(&md->lock);
  478. __blk_end_request(req, err, blk_rq_bytes(req));
  479. spin_unlock_irq(&md->lock);
  480. return err ? 0 : 1;
  481. }
  482. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  483. struct request *req)
  484. {
  485. struct mmc_blk_data *md = mq->data;
  486. struct mmc_card *card = md->queue.card;
  487. unsigned int from, nr, arg;
  488. int err = 0;
  489. if (!mmc_can_secure_erase_trim(card)) {
  490. err = -EOPNOTSUPP;
  491. goto out;
  492. }
  493. from = blk_rq_pos(req);
  494. nr = blk_rq_sectors(req);
  495. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  496. arg = MMC_SECURE_TRIM1_ARG;
  497. else
  498. arg = MMC_SECURE_ERASE_ARG;
  499. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  500. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  501. INAND_CMD38_ARG_EXT_CSD,
  502. arg == MMC_SECURE_TRIM1_ARG ?
  503. INAND_CMD38_ARG_SECTRIM1 :
  504. INAND_CMD38_ARG_SECERASE,
  505. 0);
  506. if (err)
  507. goto out;
  508. }
  509. err = mmc_erase(card, from, nr, arg);
  510. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  511. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  512. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  513. INAND_CMD38_ARG_EXT_CSD,
  514. INAND_CMD38_ARG_SECTRIM2,
  515. 0);
  516. if (err)
  517. goto out;
  518. }
  519. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  520. }
  521. out:
  522. spin_lock_irq(&md->lock);
  523. __blk_end_request(req, err, blk_rq_bytes(req));
  524. spin_unlock_irq(&md->lock);
  525. return err ? 0 : 1;
  526. }
  527. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  528. {
  529. struct mmc_blk_data *md = mq->data;
  530. /*
  531. * No-op, only service this because we need REQ_FUA for reliable
  532. * writes.
  533. */
  534. spin_lock_irq(&md->lock);
  535. __blk_end_request_all(req, 0);
  536. spin_unlock_irq(&md->lock);
  537. return 1;
  538. }
  539. /*
  540. * Reformat current write as a reliable write, supporting
  541. * both legacy and the enhanced reliable write MMC cards.
  542. * In each transfer we'll handle only as much as a single
  543. * reliable write can handle, thus finish the request in
  544. * partial completions.
  545. */
  546. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  547. struct mmc_card *card,
  548. struct request *req)
  549. {
  550. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  551. /* Legacy mode imposes restrictions on transfers. */
  552. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  553. brq->data.blocks = 1;
  554. if (brq->data.blocks > card->ext_csd.rel_sectors)
  555. brq->data.blocks = card->ext_csd.rel_sectors;
  556. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  557. brq->data.blocks = 1;
  558. }
  559. }
  560. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  561. {
  562. struct mmc_blk_data *md = mq->data;
  563. struct mmc_card *card = md->queue.card;
  564. struct mmc_blk_request brq;
  565. int ret = 1, disable_multi = 0;
  566. /*
  567. * Reliable writes are used to implement Forced Unit Access and
  568. * REQ_META accesses, and are supported only on MMCs.
  569. */
  570. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  571. (req->cmd_flags & REQ_META)) &&
  572. (rq_data_dir(req) == WRITE) &&
  573. (md->flags & MMC_BLK_REL_WR);
  574. do {
  575. u32 readcmd, writecmd, status = 0;
  576. memset(&brq, 0, sizeof(struct mmc_blk_request));
  577. brq.mrq.cmd = &brq.cmd;
  578. brq.mrq.data = &brq.data;
  579. brq.cmd.arg = blk_rq_pos(req);
  580. if (!mmc_card_blockaddr(card))
  581. brq.cmd.arg <<= 9;
  582. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  583. brq.data.blksz = 512;
  584. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  585. brq.stop.arg = 0;
  586. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  587. brq.data.blocks = blk_rq_sectors(req);
  588. /*
  589. * The block layer doesn't support all sector count
  590. * restrictions, so we need to be prepared for too big
  591. * requests.
  592. */
  593. if (brq.data.blocks > card->host->max_blk_count)
  594. brq.data.blocks = card->host->max_blk_count;
  595. /*
  596. * After a read error, we redo the request one sector at a time
  597. * in order to accurately determine which sectors can be read
  598. * successfully.
  599. */
  600. if (disable_multi && brq.data.blocks > 1)
  601. brq.data.blocks = 1;
  602. if (brq.data.blocks > 1 || do_rel_wr) {
  603. /* SPI multiblock writes terminate using a special
  604. * token, not a STOP_TRANSMISSION request.
  605. */
  606. if (!mmc_host_is_spi(card->host) ||
  607. rq_data_dir(req) == READ)
  608. brq.mrq.stop = &brq.stop;
  609. readcmd = MMC_READ_MULTIPLE_BLOCK;
  610. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  611. } else {
  612. brq.mrq.stop = NULL;
  613. readcmd = MMC_READ_SINGLE_BLOCK;
  614. writecmd = MMC_WRITE_BLOCK;
  615. }
  616. if (rq_data_dir(req) == READ) {
  617. brq.cmd.opcode = readcmd;
  618. brq.data.flags |= MMC_DATA_READ;
  619. } else {
  620. brq.cmd.opcode = writecmd;
  621. brq.data.flags |= MMC_DATA_WRITE;
  622. }
  623. if (do_rel_wr)
  624. mmc_apply_rel_rw(&brq, card, req);
  625. /*
  626. * Pre-defined multi-block transfers are preferable to
  627. * open ended-ones (and necessary for reliable writes).
  628. * However, it is not sufficient to just send CMD23,
  629. * and avoid the final CMD12, as on an error condition
  630. * CMD12 (stop) needs to be sent anyway. This, coupled
  631. * with Auto-CMD23 enhancements provided by some
  632. * hosts, means that the complexity of dealing
  633. * with this is best left to the host. If CMD23 is
  634. * supported by card and host, we'll fill sbc in and let
  635. * the host deal with handling it correctly. This means
  636. * that for hosts that don't expose MMC_CAP_CMD23, no
  637. * change of behavior will be observed.
  638. *
  639. * N.B: Some MMC cards experience perf degradation.
  640. * We'll avoid using CMD23-bounded multiblock writes for
  641. * these, while retaining features like reliable writes.
  642. */
  643. if ((md->flags & MMC_BLK_CMD23) &&
  644. mmc_op_multi(brq.cmd.opcode) &&
  645. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
  646. brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
  647. brq.sbc.arg = brq.data.blocks |
  648. (do_rel_wr ? (1 << 31) : 0);
  649. brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  650. brq.mrq.sbc = &brq.sbc;
  651. }
  652. mmc_set_data_timeout(&brq.data, card);
  653. brq.data.sg = mq->sg;
  654. brq.data.sg_len = mmc_queue_map_sg(mq);
  655. /*
  656. * Adjust the sg list so it is the same size as the
  657. * request.
  658. */
  659. if (brq.data.blocks != blk_rq_sectors(req)) {
  660. int i, data_size = brq.data.blocks << 9;
  661. struct scatterlist *sg;
  662. for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
  663. data_size -= sg->length;
  664. if (data_size <= 0) {
  665. sg->length += data_size;
  666. i++;
  667. break;
  668. }
  669. }
  670. brq.data.sg_len = i;
  671. }
  672. mmc_queue_bounce_pre(mq);
  673. mmc_wait_for_req(card->host, &brq.mrq);
  674. mmc_queue_bounce_post(mq);
  675. /*
  676. * Check for errors here, but don't jump to cmd_err
  677. * until later as we need to wait for the card to leave
  678. * programming mode even when things go wrong.
  679. */
  680. if (brq.sbc.error || brq.cmd.error ||
  681. brq.data.error || brq.stop.error) {
  682. if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
  683. /* Redo read one sector at a time */
  684. printk(KERN_WARNING "%s: retrying using single "
  685. "block read\n", req->rq_disk->disk_name);
  686. disable_multi = 1;
  687. continue;
  688. }
  689. get_card_status(card, &status, 0);
  690. }
  691. if (brq.sbc.error) {
  692. printk(KERN_ERR "%s: error %d sending SET_BLOCK_COUNT "
  693. "command, response %#x, card status %#x\n",
  694. req->rq_disk->disk_name, brq.sbc.error,
  695. brq.sbc.resp[0], status);
  696. }
  697. if (brq.cmd.error) {
  698. printk(KERN_ERR "%s: error %d sending read/write "
  699. "command, response %#x, card status %#x\n",
  700. req->rq_disk->disk_name, brq.cmd.error,
  701. brq.cmd.resp[0], status);
  702. }
  703. if (brq.data.error) {
  704. if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
  705. /* 'Stop' response contains card status */
  706. status = brq.mrq.stop->resp[0];
  707. printk(KERN_ERR "%s: error %d transferring data,"
  708. " sector %u, nr %u, card status %#x\n",
  709. req->rq_disk->disk_name, brq.data.error,
  710. (unsigned)blk_rq_pos(req),
  711. (unsigned)blk_rq_sectors(req), status);
  712. }
  713. if (brq.stop.error) {
  714. printk(KERN_ERR "%s: error %d sending stop command, "
  715. "response %#x, card status %#x\n",
  716. req->rq_disk->disk_name, brq.stop.error,
  717. brq.stop.resp[0], status);
  718. }
  719. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  720. do {
  721. int err = get_card_status(card, &status, 5);
  722. if (err) {
  723. printk(KERN_ERR "%s: error %d requesting status\n",
  724. req->rq_disk->disk_name, err);
  725. goto cmd_err;
  726. }
  727. /*
  728. * Some cards mishandle the status bits,
  729. * so make sure to check both the busy
  730. * indication and the card state.
  731. */
  732. } while (!(status & R1_READY_FOR_DATA) ||
  733. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  734. }
  735. if (brq.cmd.error || brq.stop.error || brq.data.error) {
  736. if (rq_data_dir(req) == READ) {
  737. /*
  738. * After an error, we redo I/O one sector at a
  739. * time, so we only reach here after trying to
  740. * read a single sector.
  741. */
  742. spin_lock_irq(&md->lock);
  743. ret = __blk_end_request(req, -EIO, brq.data.blksz);
  744. spin_unlock_irq(&md->lock);
  745. continue;
  746. }
  747. goto cmd_err;
  748. }
  749. /*
  750. * A block was successfully transferred.
  751. */
  752. spin_lock_irq(&md->lock);
  753. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  754. spin_unlock_irq(&md->lock);
  755. } while (ret);
  756. return 1;
  757. cmd_err:
  758. /*
  759. * If this is an SD card and we're writing, we can first
  760. * mark the known good sectors as ok.
  761. *
  762. * If the card is not SD, we can still ok written sectors
  763. * as reported by the controller (which might be less than
  764. * the real number of written sectors, but never more).
  765. */
  766. if (mmc_card_sd(card)) {
  767. u32 blocks;
  768. blocks = mmc_sd_num_wr_blocks(card);
  769. if (blocks != (u32)-1) {
  770. spin_lock_irq(&md->lock);
  771. ret = __blk_end_request(req, 0, blocks << 9);
  772. spin_unlock_irq(&md->lock);
  773. }
  774. } else {
  775. spin_lock_irq(&md->lock);
  776. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  777. spin_unlock_irq(&md->lock);
  778. }
  779. spin_lock_irq(&md->lock);
  780. while (ret)
  781. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  782. spin_unlock_irq(&md->lock);
  783. return 0;
  784. }
  785. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  786. {
  787. int ret;
  788. struct mmc_blk_data *md = mq->data;
  789. struct mmc_card *card = md->queue.card;
  790. mmc_claim_host(card->host);
  791. ret = mmc_blk_part_switch(card, md);
  792. if (ret) {
  793. ret = 0;
  794. goto out;
  795. }
  796. if (req->cmd_flags & REQ_DISCARD) {
  797. if (req->cmd_flags & REQ_SECURE)
  798. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  799. else
  800. ret = mmc_blk_issue_discard_rq(mq, req);
  801. } else if (req->cmd_flags & REQ_FLUSH) {
  802. ret = mmc_blk_issue_flush(mq, req);
  803. } else {
  804. ret = mmc_blk_issue_rw_rq(mq, req);
  805. }
  806. out:
  807. mmc_release_host(card->host);
  808. return ret;
  809. }
  810. static inline int mmc_blk_readonly(struct mmc_card *card)
  811. {
  812. return mmc_card_readonly(card) ||
  813. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  814. }
  815. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  816. struct device *parent,
  817. sector_t size,
  818. bool default_ro,
  819. const char *subname)
  820. {
  821. struct mmc_blk_data *md;
  822. int devidx, ret;
  823. devidx = find_first_zero_bit(dev_use, max_devices);
  824. if (devidx >= max_devices)
  825. return ERR_PTR(-ENOSPC);
  826. __set_bit(devidx, dev_use);
  827. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  828. if (!md) {
  829. ret = -ENOMEM;
  830. goto out;
  831. }
  832. /*
  833. * !subname implies we are creating main mmc_blk_data that will be
  834. * associated with mmc_card with mmc_set_drvdata. Due to device
  835. * partitions, devidx will not coincide with a per-physical card
  836. * index anymore so we keep track of a name index.
  837. */
  838. if (!subname) {
  839. md->name_idx = find_first_zero_bit(name_use, max_devices);
  840. __set_bit(md->name_idx, name_use);
  841. }
  842. else
  843. md->name_idx = ((struct mmc_blk_data *)
  844. dev_to_disk(parent)->private_data)->name_idx;
  845. /*
  846. * Set the read-only status based on the supported commands
  847. * and the write protect switch.
  848. */
  849. md->read_only = mmc_blk_readonly(card);
  850. md->disk = alloc_disk(perdev_minors);
  851. if (md->disk == NULL) {
  852. ret = -ENOMEM;
  853. goto err_kfree;
  854. }
  855. spin_lock_init(&md->lock);
  856. INIT_LIST_HEAD(&md->part);
  857. md->usage = 1;
  858. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  859. if (ret)
  860. goto err_putdisk;
  861. md->queue.issue_fn = mmc_blk_issue_rq;
  862. md->queue.data = md;
  863. md->disk->major = MMC_BLOCK_MAJOR;
  864. md->disk->first_minor = devidx * perdev_minors;
  865. md->disk->fops = &mmc_bdops;
  866. md->disk->private_data = md;
  867. md->disk->queue = md->queue.queue;
  868. md->disk->driverfs_dev = parent;
  869. set_disk_ro(md->disk, md->read_only || default_ro);
  870. /*
  871. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  872. *
  873. * - be set for removable media with permanent block devices
  874. * - be unset for removable block devices with permanent media
  875. *
  876. * Since MMC block devices clearly fall under the second
  877. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  878. * should use the block device creation/destruction hotplug
  879. * messages to tell when the card is present.
  880. */
  881. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  882. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  883. blk_queue_logical_block_size(md->queue.queue, 512);
  884. set_capacity(md->disk, size);
  885. if (mmc_host_cmd23(card->host)) {
  886. if (mmc_card_mmc(card) ||
  887. (mmc_card_sd(card) &&
  888. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  889. md->flags |= MMC_BLK_CMD23;
  890. }
  891. if (mmc_card_mmc(card) &&
  892. md->flags & MMC_BLK_CMD23 &&
  893. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  894. card->ext_csd.rel_sectors)) {
  895. md->flags |= MMC_BLK_REL_WR;
  896. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  897. }
  898. return md;
  899. err_putdisk:
  900. put_disk(md->disk);
  901. err_kfree:
  902. kfree(md);
  903. out:
  904. return ERR_PTR(ret);
  905. }
  906. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  907. {
  908. sector_t size;
  909. struct mmc_blk_data *md;
  910. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  911. /*
  912. * The EXT_CSD sector count is in number or 512 byte
  913. * sectors.
  914. */
  915. size = card->ext_csd.sectors;
  916. } else {
  917. /*
  918. * The CSD capacity field is in units of read_blkbits.
  919. * set_capacity takes units of 512 bytes.
  920. */
  921. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  922. }
  923. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  924. return md;
  925. }
  926. static int mmc_blk_alloc_part(struct mmc_card *card,
  927. struct mmc_blk_data *md,
  928. unsigned int part_type,
  929. sector_t size,
  930. bool default_ro,
  931. const char *subname)
  932. {
  933. char cap_str[10];
  934. struct mmc_blk_data *part_md;
  935. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  936. subname);
  937. if (IS_ERR(part_md))
  938. return PTR_ERR(part_md);
  939. part_md->part_type = part_type;
  940. list_add(&part_md->part, &md->part);
  941. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  942. cap_str, sizeof(cap_str));
  943. printk(KERN_INFO "%s: %s %s partition %u %s\n",
  944. part_md->disk->disk_name, mmc_card_id(card),
  945. mmc_card_name(card), part_md->part_type, cap_str);
  946. return 0;
  947. }
  948. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  949. {
  950. int ret = 0;
  951. if (!mmc_card_mmc(card))
  952. return 0;
  953. if (card->ext_csd.boot_size) {
  954. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
  955. card->ext_csd.boot_size >> 9,
  956. true,
  957. "boot0");
  958. if (ret)
  959. return ret;
  960. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
  961. card->ext_csd.boot_size >> 9,
  962. true,
  963. "boot1");
  964. if (ret)
  965. return ret;
  966. }
  967. return ret;
  968. }
  969. static int
  970. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  971. {
  972. int err;
  973. mmc_claim_host(card->host);
  974. err = mmc_set_blocklen(card, 512);
  975. mmc_release_host(card->host);
  976. if (err) {
  977. printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
  978. md->disk->disk_name, err);
  979. return -EINVAL;
  980. }
  981. return 0;
  982. }
  983. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  984. {
  985. if (md) {
  986. if (md->disk->flags & GENHD_FL_UP) {
  987. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  988. /* Stop new requests from getting into the queue */
  989. del_gendisk(md->disk);
  990. }
  991. /* Then flush out any already in there */
  992. mmc_cleanup_queue(&md->queue);
  993. mmc_blk_put(md);
  994. }
  995. }
  996. static void mmc_blk_remove_parts(struct mmc_card *card,
  997. struct mmc_blk_data *md)
  998. {
  999. struct list_head *pos, *q;
  1000. struct mmc_blk_data *part_md;
  1001. __clear_bit(md->name_idx, name_use);
  1002. list_for_each_safe(pos, q, &md->part) {
  1003. part_md = list_entry(pos, struct mmc_blk_data, part);
  1004. list_del(pos);
  1005. mmc_blk_remove_req(part_md);
  1006. }
  1007. }
  1008. static int mmc_add_disk(struct mmc_blk_data *md)
  1009. {
  1010. int ret;
  1011. add_disk(md->disk);
  1012. md->force_ro.show = force_ro_show;
  1013. md->force_ro.store = force_ro_store;
  1014. sysfs_attr_init(&md->force_ro.attr);
  1015. md->force_ro.attr.name = "force_ro";
  1016. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1017. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1018. if (ret)
  1019. del_gendisk(md->disk);
  1020. return ret;
  1021. }
  1022. static const struct mmc_fixup blk_fixups[] =
  1023. {
  1024. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1025. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1026. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1027. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1028. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1029. /*
  1030. * Some MMC cards experience performance degradation with CMD23
  1031. * instead of CMD12-bounded multiblock transfers. For now we'll
  1032. * black list what's bad...
  1033. * - Certain Toshiba cards.
  1034. *
  1035. * N.B. This doesn't affect SD cards.
  1036. */
  1037. MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1038. MMC_QUIRK_BLK_NO_CMD23),
  1039. MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1040. MMC_QUIRK_BLK_NO_CMD23),
  1041. MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1042. MMC_QUIRK_BLK_NO_CMD23),
  1043. END_FIXUP
  1044. };
  1045. static int mmc_blk_probe(struct mmc_card *card)
  1046. {
  1047. struct mmc_blk_data *md, *part_md;
  1048. int err;
  1049. char cap_str[10];
  1050. /*
  1051. * Check that the card supports the command class(es) we need.
  1052. */
  1053. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1054. return -ENODEV;
  1055. md = mmc_blk_alloc(card);
  1056. if (IS_ERR(md))
  1057. return PTR_ERR(md);
  1058. err = mmc_blk_set_blksize(md, card);
  1059. if (err)
  1060. goto out;
  1061. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1062. cap_str, sizeof(cap_str));
  1063. printk(KERN_INFO "%s: %s %s %s %s\n",
  1064. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1065. cap_str, md->read_only ? "(ro)" : "");
  1066. if (mmc_blk_alloc_parts(card, md))
  1067. goto out;
  1068. mmc_set_drvdata(card, md);
  1069. mmc_fixup_device(card, blk_fixups);
  1070. if (mmc_add_disk(md))
  1071. goto out;
  1072. list_for_each_entry(part_md, &md->part, part) {
  1073. if (mmc_add_disk(part_md))
  1074. goto out;
  1075. }
  1076. return 0;
  1077. out:
  1078. mmc_blk_remove_parts(card, md);
  1079. mmc_blk_remove_req(md);
  1080. return err;
  1081. }
  1082. static void mmc_blk_remove(struct mmc_card *card)
  1083. {
  1084. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1085. mmc_blk_remove_parts(card, md);
  1086. mmc_claim_host(card->host);
  1087. mmc_blk_part_switch(card, md);
  1088. mmc_release_host(card->host);
  1089. mmc_blk_remove_req(md);
  1090. mmc_set_drvdata(card, NULL);
  1091. }
  1092. #ifdef CONFIG_PM
  1093. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1094. {
  1095. struct mmc_blk_data *part_md;
  1096. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1097. if (md) {
  1098. mmc_queue_suspend(&md->queue);
  1099. list_for_each_entry(part_md, &md->part, part) {
  1100. mmc_queue_suspend(&part_md->queue);
  1101. }
  1102. }
  1103. return 0;
  1104. }
  1105. static int mmc_blk_resume(struct mmc_card *card)
  1106. {
  1107. struct mmc_blk_data *part_md;
  1108. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1109. if (md) {
  1110. mmc_blk_set_blksize(md, card);
  1111. /*
  1112. * Resume involves the card going into idle state,
  1113. * so current partition is always the main one.
  1114. */
  1115. md->part_curr = md->part_type;
  1116. mmc_queue_resume(&md->queue);
  1117. list_for_each_entry(part_md, &md->part, part) {
  1118. mmc_queue_resume(&part_md->queue);
  1119. }
  1120. }
  1121. return 0;
  1122. }
  1123. #else
  1124. #define mmc_blk_suspend NULL
  1125. #define mmc_blk_resume NULL
  1126. #endif
  1127. static struct mmc_driver mmc_driver = {
  1128. .drv = {
  1129. .name = "mmcblk",
  1130. },
  1131. .probe = mmc_blk_probe,
  1132. .remove = mmc_blk_remove,
  1133. .suspend = mmc_blk_suspend,
  1134. .resume = mmc_blk_resume,
  1135. };
  1136. static int __init mmc_blk_init(void)
  1137. {
  1138. int res;
  1139. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1140. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1141. max_devices = 256 / perdev_minors;
  1142. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1143. if (res)
  1144. goto out;
  1145. res = mmc_register_driver(&mmc_driver);
  1146. if (res)
  1147. goto out2;
  1148. return 0;
  1149. out2:
  1150. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1151. out:
  1152. return res;
  1153. }
  1154. static void __exit mmc_blk_exit(void)
  1155. {
  1156. mmc_unregister_driver(&mmc_driver);
  1157. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1158. }
  1159. module_init(mmc_blk_init);
  1160. module_exit(mmc_blk_exit);
  1161. MODULE_LICENSE("GPL");
  1162. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");