block.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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 u32 get_card_status(struct mmc_card *card, struct request *req)
  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, 0);
  445. if (err)
  446. printk(KERN_ERR "%s: error %d sending status command",
  447. req->rq_disk->disk_name, err);
  448. return cmd.resp[0];
  449. }
  450. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  451. {
  452. struct mmc_blk_data *md = mq->data;
  453. struct mmc_card *card = md->queue.card;
  454. unsigned int from, nr, arg;
  455. int err = 0;
  456. if (!mmc_can_erase(card)) {
  457. err = -EOPNOTSUPP;
  458. goto out;
  459. }
  460. from = blk_rq_pos(req);
  461. nr = blk_rq_sectors(req);
  462. if (mmc_can_trim(card))
  463. arg = MMC_TRIM_ARG;
  464. else
  465. arg = MMC_ERASE_ARG;
  466. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  467. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  468. INAND_CMD38_ARG_EXT_CSD,
  469. arg == MMC_TRIM_ARG ?
  470. INAND_CMD38_ARG_TRIM :
  471. INAND_CMD38_ARG_ERASE,
  472. 0);
  473. if (err)
  474. goto out;
  475. }
  476. err = mmc_erase(card, from, nr, arg);
  477. out:
  478. spin_lock_irq(&md->lock);
  479. __blk_end_request(req, err, blk_rq_bytes(req));
  480. spin_unlock_irq(&md->lock);
  481. return err ? 0 : 1;
  482. }
  483. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  484. struct request *req)
  485. {
  486. struct mmc_blk_data *md = mq->data;
  487. struct mmc_card *card = md->queue.card;
  488. unsigned int from, nr, arg;
  489. int err = 0;
  490. if (!mmc_can_secure_erase_trim(card)) {
  491. err = -EOPNOTSUPP;
  492. goto out;
  493. }
  494. from = blk_rq_pos(req);
  495. nr = blk_rq_sectors(req);
  496. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  497. arg = MMC_SECURE_TRIM1_ARG;
  498. else
  499. arg = MMC_SECURE_ERASE_ARG;
  500. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  501. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  502. INAND_CMD38_ARG_EXT_CSD,
  503. arg == MMC_SECURE_TRIM1_ARG ?
  504. INAND_CMD38_ARG_SECTRIM1 :
  505. INAND_CMD38_ARG_SECERASE,
  506. 0);
  507. if (err)
  508. goto out;
  509. }
  510. err = mmc_erase(card, from, nr, arg);
  511. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  512. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  513. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  514. INAND_CMD38_ARG_EXT_CSD,
  515. INAND_CMD38_ARG_SECTRIM2,
  516. 0);
  517. if (err)
  518. goto out;
  519. }
  520. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  521. }
  522. out:
  523. spin_lock_irq(&md->lock);
  524. __blk_end_request(req, err, blk_rq_bytes(req));
  525. spin_unlock_irq(&md->lock);
  526. return err ? 0 : 1;
  527. }
  528. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  529. {
  530. struct mmc_blk_data *md = mq->data;
  531. /*
  532. * No-op, only service this because we need REQ_FUA for reliable
  533. * writes.
  534. */
  535. spin_lock_irq(&md->lock);
  536. __blk_end_request_all(req, 0);
  537. spin_unlock_irq(&md->lock);
  538. return 1;
  539. }
  540. /*
  541. * Reformat current write as a reliable write, supporting
  542. * both legacy and the enhanced reliable write MMC cards.
  543. * In each transfer we'll handle only as much as a single
  544. * reliable write can handle, thus finish the request in
  545. * partial completions.
  546. */
  547. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  548. struct mmc_card *card,
  549. struct request *req)
  550. {
  551. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  552. /* Legacy mode imposes restrictions on transfers. */
  553. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  554. brq->data.blocks = 1;
  555. if (brq->data.blocks > card->ext_csd.rel_sectors)
  556. brq->data.blocks = card->ext_csd.rel_sectors;
  557. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  558. brq->data.blocks = 1;
  559. }
  560. }
  561. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  562. {
  563. struct mmc_blk_data *md = mq->data;
  564. struct mmc_card *card = md->queue.card;
  565. struct mmc_blk_request brq;
  566. int ret = 1, disable_multi = 0;
  567. /*
  568. * Reliable writes are used to implement Forced Unit Access and
  569. * REQ_META accesses, and are supported only on MMCs.
  570. */
  571. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  572. (req->cmd_flags & REQ_META)) &&
  573. (rq_data_dir(req) == WRITE) &&
  574. (md->flags & MMC_BLK_REL_WR);
  575. do {
  576. struct mmc_command cmd = {0};
  577. u32 readcmd, writecmd, status = 0;
  578. memset(&brq, 0, sizeof(struct mmc_blk_request));
  579. brq.mrq.cmd = &brq.cmd;
  580. brq.mrq.data = &brq.data;
  581. brq.cmd.arg = blk_rq_pos(req);
  582. if (!mmc_card_blockaddr(card))
  583. brq.cmd.arg <<= 9;
  584. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  585. brq.data.blksz = 512;
  586. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  587. brq.stop.arg = 0;
  588. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  589. brq.data.blocks = blk_rq_sectors(req);
  590. /*
  591. * The block layer doesn't support all sector count
  592. * restrictions, so we need to be prepared for too big
  593. * requests.
  594. */
  595. if (brq.data.blocks > card->host->max_blk_count)
  596. brq.data.blocks = card->host->max_blk_count;
  597. /*
  598. * After a read error, we redo the request one sector at a time
  599. * in order to accurately determine which sectors can be read
  600. * successfully.
  601. */
  602. if (disable_multi && brq.data.blocks > 1)
  603. brq.data.blocks = 1;
  604. if (brq.data.blocks > 1 || do_rel_wr) {
  605. /* SPI multiblock writes terminate using a special
  606. * token, not a STOP_TRANSMISSION request.
  607. */
  608. if (!mmc_host_is_spi(card->host) ||
  609. rq_data_dir(req) == READ)
  610. brq.mrq.stop = &brq.stop;
  611. readcmd = MMC_READ_MULTIPLE_BLOCK;
  612. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  613. } else {
  614. brq.mrq.stop = NULL;
  615. readcmd = MMC_READ_SINGLE_BLOCK;
  616. writecmd = MMC_WRITE_BLOCK;
  617. }
  618. if (rq_data_dir(req) == READ) {
  619. brq.cmd.opcode = readcmd;
  620. brq.data.flags |= MMC_DATA_READ;
  621. } else {
  622. brq.cmd.opcode = writecmd;
  623. brq.data.flags |= MMC_DATA_WRITE;
  624. }
  625. if (do_rel_wr)
  626. mmc_apply_rel_rw(&brq, card, req);
  627. /*
  628. * Pre-defined multi-block transfers are preferable to
  629. * open ended-ones (and necessary for reliable writes).
  630. * However, it is not sufficient to just send CMD23,
  631. * and avoid the final CMD12, as on an error condition
  632. * CMD12 (stop) needs to be sent anyway. This, coupled
  633. * with Auto-CMD23 enhancements provided by some
  634. * hosts, means that the complexity of dealing
  635. * with this is best left to the host. If CMD23 is
  636. * supported by card and host, we'll fill sbc in and let
  637. * the host deal with handling it correctly. This means
  638. * that for hosts that don't expose MMC_CAP_CMD23, no
  639. * change of behavior will be observed.
  640. *
  641. * N.B: Some MMC cards experience perf degradation.
  642. * We'll avoid using CMD23-bounded multiblock writes for
  643. * these, while retaining features like reliable writes.
  644. */
  645. if ((md->flags & MMC_BLK_CMD23) &&
  646. mmc_op_multi(brq.cmd.opcode) &&
  647. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
  648. brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
  649. brq.sbc.arg = brq.data.blocks |
  650. (do_rel_wr ? (1 << 31) : 0);
  651. brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  652. brq.mrq.sbc = &brq.sbc;
  653. }
  654. mmc_set_data_timeout(&brq.data, card);
  655. brq.data.sg = mq->sg;
  656. brq.data.sg_len = mmc_queue_map_sg(mq);
  657. /*
  658. * Adjust the sg list so it is the same size as the
  659. * request.
  660. */
  661. if (brq.data.blocks != blk_rq_sectors(req)) {
  662. int i, data_size = brq.data.blocks << 9;
  663. struct scatterlist *sg;
  664. for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
  665. data_size -= sg->length;
  666. if (data_size <= 0) {
  667. sg->length += data_size;
  668. i++;
  669. break;
  670. }
  671. }
  672. brq.data.sg_len = i;
  673. }
  674. mmc_queue_bounce_pre(mq);
  675. mmc_wait_for_req(card->host, &brq.mrq);
  676. mmc_queue_bounce_post(mq);
  677. /*
  678. * Check for errors here, but don't jump to cmd_err
  679. * until later as we need to wait for the card to leave
  680. * programming mode even when things go wrong.
  681. */
  682. if (brq.sbc.error || brq.cmd.error ||
  683. brq.data.error || brq.stop.error) {
  684. if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
  685. /* Redo read one sector at a time */
  686. printk(KERN_WARNING "%s: retrying using single "
  687. "block read\n", req->rq_disk->disk_name);
  688. disable_multi = 1;
  689. continue;
  690. }
  691. status = get_card_status(card, req);
  692. }
  693. if (brq.sbc.error) {
  694. printk(KERN_ERR "%s: error %d sending SET_BLOCK_COUNT "
  695. "command, response %#x, card status %#x\n",
  696. req->rq_disk->disk_name, brq.sbc.error,
  697. brq.sbc.resp[0], status);
  698. }
  699. if (brq.cmd.error) {
  700. printk(KERN_ERR "%s: error %d sending read/write "
  701. "command, response %#x, card status %#x\n",
  702. req->rq_disk->disk_name, brq.cmd.error,
  703. brq.cmd.resp[0], status);
  704. }
  705. if (brq.data.error) {
  706. if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
  707. /* 'Stop' response contains card status */
  708. status = brq.mrq.stop->resp[0];
  709. printk(KERN_ERR "%s: error %d transferring data,"
  710. " sector %u, nr %u, card status %#x\n",
  711. req->rq_disk->disk_name, brq.data.error,
  712. (unsigned)blk_rq_pos(req),
  713. (unsigned)blk_rq_sectors(req), status);
  714. }
  715. if (brq.stop.error) {
  716. printk(KERN_ERR "%s: error %d sending stop command, "
  717. "response %#x, card status %#x\n",
  718. req->rq_disk->disk_name, brq.stop.error,
  719. brq.stop.resp[0], status);
  720. }
  721. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  722. do {
  723. int err;
  724. cmd.opcode = MMC_SEND_STATUS;
  725. cmd.arg = card->rca << 16;
  726. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  727. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  728. if (err) {
  729. printk(KERN_ERR "%s: error %d requesting status\n",
  730. req->rq_disk->disk_name, err);
  731. goto cmd_err;
  732. }
  733. /*
  734. * Some cards mishandle the status bits,
  735. * so make sure to check both the busy
  736. * indication and the card state.
  737. */
  738. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  739. (R1_CURRENT_STATE(cmd.resp[0]) == 7));
  740. #if 0
  741. if (cmd.resp[0] & ~0x00000900)
  742. printk(KERN_ERR "%s: status = %08x\n",
  743. req->rq_disk->disk_name, cmd.resp[0]);
  744. if (mmc_decode_status(cmd.resp))
  745. goto cmd_err;
  746. #endif
  747. }
  748. if (brq.cmd.error || brq.stop.error || brq.data.error) {
  749. if (rq_data_dir(req) == READ) {
  750. /*
  751. * After an error, we redo I/O one sector at a
  752. * time, so we only reach here after trying to
  753. * read a single sector.
  754. */
  755. spin_lock_irq(&md->lock);
  756. ret = __blk_end_request(req, -EIO, brq.data.blksz);
  757. spin_unlock_irq(&md->lock);
  758. continue;
  759. }
  760. goto cmd_err;
  761. }
  762. /*
  763. * A block was successfully transferred.
  764. */
  765. spin_lock_irq(&md->lock);
  766. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  767. spin_unlock_irq(&md->lock);
  768. } while (ret);
  769. return 1;
  770. cmd_err:
  771. /*
  772. * If this is an SD card and we're writing, we can first
  773. * mark the known good sectors as ok.
  774. *
  775. * If the card is not SD, we can still ok written sectors
  776. * as reported by the controller (which might be less than
  777. * the real number of written sectors, but never more).
  778. */
  779. if (mmc_card_sd(card)) {
  780. u32 blocks;
  781. blocks = mmc_sd_num_wr_blocks(card);
  782. if (blocks != (u32)-1) {
  783. spin_lock_irq(&md->lock);
  784. ret = __blk_end_request(req, 0, blocks << 9);
  785. spin_unlock_irq(&md->lock);
  786. }
  787. } else {
  788. spin_lock_irq(&md->lock);
  789. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  790. spin_unlock_irq(&md->lock);
  791. }
  792. spin_lock_irq(&md->lock);
  793. while (ret)
  794. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  795. spin_unlock_irq(&md->lock);
  796. return 0;
  797. }
  798. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  799. {
  800. int ret;
  801. struct mmc_blk_data *md = mq->data;
  802. struct mmc_card *card = md->queue.card;
  803. mmc_claim_host(card->host);
  804. ret = mmc_blk_part_switch(card, md);
  805. if (ret) {
  806. ret = 0;
  807. goto out;
  808. }
  809. if (req->cmd_flags & REQ_DISCARD) {
  810. if (req->cmd_flags & REQ_SECURE)
  811. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  812. else
  813. ret = mmc_blk_issue_discard_rq(mq, req);
  814. } else if (req->cmd_flags & REQ_FLUSH) {
  815. ret = mmc_blk_issue_flush(mq, req);
  816. } else {
  817. ret = mmc_blk_issue_rw_rq(mq, req);
  818. }
  819. out:
  820. mmc_release_host(card->host);
  821. return ret;
  822. }
  823. static inline int mmc_blk_readonly(struct mmc_card *card)
  824. {
  825. return mmc_card_readonly(card) ||
  826. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  827. }
  828. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  829. struct device *parent,
  830. sector_t size,
  831. bool default_ro,
  832. const char *subname)
  833. {
  834. struct mmc_blk_data *md;
  835. int devidx, ret;
  836. devidx = find_first_zero_bit(dev_use, max_devices);
  837. if (devidx >= max_devices)
  838. return ERR_PTR(-ENOSPC);
  839. __set_bit(devidx, dev_use);
  840. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  841. if (!md) {
  842. ret = -ENOMEM;
  843. goto out;
  844. }
  845. /*
  846. * !subname implies we are creating main mmc_blk_data that will be
  847. * associated with mmc_card with mmc_set_drvdata. Due to device
  848. * partitions, devidx will not coincide with a per-physical card
  849. * index anymore so we keep track of a name index.
  850. */
  851. if (!subname) {
  852. md->name_idx = find_first_zero_bit(name_use, max_devices);
  853. __set_bit(md->name_idx, name_use);
  854. }
  855. else
  856. md->name_idx = ((struct mmc_blk_data *)
  857. dev_to_disk(parent)->private_data)->name_idx;
  858. /*
  859. * Set the read-only status based on the supported commands
  860. * and the write protect switch.
  861. */
  862. md->read_only = mmc_blk_readonly(card);
  863. md->disk = alloc_disk(perdev_minors);
  864. if (md->disk == NULL) {
  865. ret = -ENOMEM;
  866. goto err_kfree;
  867. }
  868. spin_lock_init(&md->lock);
  869. INIT_LIST_HEAD(&md->part);
  870. md->usage = 1;
  871. ret = mmc_init_queue(&md->queue, card, &md->lock);
  872. if (ret)
  873. goto err_putdisk;
  874. md->queue.issue_fn = mmc_blk_issue_rq;
  875. md->queue.data = md;
  876. md->disk->major = MMC_BLOCK_MAJOR;
  877. md->disk->first_minor = devidx * perdev_minors;
  878. md->disk->fops = &mmc_bdops;
  879. md->disk->private_data = md;
  880. md->disk->queue = md->queue.queue;
  881. md->disk->driverfs_dev = parent;
  882. set_disk_ro(md->disk, md->read_only || default_ro);
  883. /*
  884. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  885. *
  886. * - be set for removable media with permanent block devices
  887. * - be unset for removable block devices with permanent media
  888. *
  889. * Since MMC block devices clearly fall under the second
  890. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  891. * should use the block device creation/destruction hotplug
  892. * messages to tell when the card is present.
  893. */
  894. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  895. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  896. blk_queue_logical_block_size(md->queue.queue, 512);
  897. set_capacity(md->disk, size);
  898. if (mmc_host_cmd23(card->host) &&
  899. mmc_card_mmc(card))
  900. md->flags |= MMC_BLK_CMD23;
  901. if (mmc_card_mmc(card) &&
  902. md->flags & MMC_BLK_CMD23 &&
  903. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  904. card->ext_csd.rel_sectors)) {
  905. md->flags |= MMC_BLK_REL_WR;
  906. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  907. }
  908. return md;
  909. err_putdisk:
  910. put_disk(md->disk);
  911. err_kfree:
  912. kfree(md);
  913. out:
  914. return ERR_PTR(ret);
  915. }
  916. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  917. {
  918. sector_t size;
  919. struct mmc_blk_data *md;
  920. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  921. /*
  922. * The EXT_CSD sector count is in number or 512 byte
  923. * sectors.
  924. */
  925. size = card->ext_csd.sectors;
  926. } else {
  927. /*
  928. * The CSD capacity field is in units of read_blkbits.
  929. * set_capacity takes units of 512 bytes.
  930. */
  931. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  932. }
  933. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  934. return md;
  935. }
  936. static int mmc_blk_alloc_part(struct mmc_card *card,
  937. struct mmc_blk_data *md,
  938. unsigned int part_type,
  939. sector_t size,
  940. bool default_ro,
  941. const char *subname)
  942. {
  943. char cap_str[10];
  944. struct mmc_blk_data *part_md;
  945. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  946. subname);
  947. if (IS_ERR(part_md))
  948. return PTR_ERR(part_md);
  949. part_md->part_type = part_type;
  950. list_add(&part_md->part, &md->part);
  951. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  952. cap_str, sizeof(cap_str));
  953. printk(KERN_INFO "%s: %s %s partition %u %s\n",
  954. part_md->disk->disk_name, mmc_card_id(card),
  955. mmc_card_name(card), part_md->part_type, cap_str);
  956. return 0;
  957. }
  958. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  959. {
  960. int ret = 0;
  961. if (!mmc_card_mmc(card))
  962. return 0;
  963. if (card->ext_csd.boot_size) {
  964. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
  965. card->ext_csd.boot_size >> 9,
  966. true,
  967. "boot0");
  968. if (ret)
  969. return ret;
  970. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
  971. card->ext_csd.boot_size >> 9,
  972. true,
  973. "boot1");
  974. if (ret)
  975. return ret;
  976. }
  977. return ret;
  978. }
  979. static int
  980. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  981. {
  982. int err;
  983. mmc_claim_host(card->host);
  984. err = mmc_set_blocklen(card, 512);
  985. mmc_release_host(card->host);
  986. if (err) {
  987. printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
  988. md->disk->disk_name, err);
  989. return -EINVAL;
  990. }
  991. return 0;
  992. }
  993. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  994. {
  995. if (md) {
  996. if (md->disk->flags & GENHD_FL_UP) {
  997. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  998. /* Stop new requests from getting into the queue */
  999. del_gendisk(md->disk);
  1000. }
  1001. /* Then flush out any already in there */
  1002. mmc_cleanup_queue(&md->queue);
  1003. mmc_blk_put(md);
  1004. }
  1005. }
  1006. static void mmc_blk_remove_parts(struct mmc_card *card,
  1007. struct mmc_blk_data *md)
  1008. {
  1009. struct list_head *pos, *q;
  1010. struct mmc_blk_data *part_md;
  1011. __clear_bit(md->name_idx, name_use);
  1012. list_for_each_safe(pos, q, &md->part) {
  1013. part_md = list_entry(pos, struct mmc_blk_data, part);
  1014. list_del(pos);
  1015. mmc_blk_remove_req(part_md);
  1016. }
  1017. }
  1018. static int mmc_add_disk(struct mmc_blk_data *md)
  1019. {
  1020. int ret;
  1021. add_disk(md->disk);
  1022. md->force_ro.show = force_ro_show;
  1023. md->force_ro.store = force_ro_store;
  1024. sysfs_attr_init(&md->force_ro.attr);
  1025. md->force_ro.attr.name = "force_ro";
  1026. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1027. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1028. if (ret)
  1029. del_gendisk(md->disk);
  1030. return ret;
  1031. }
  1032. static const struct mmc_fixup blk_fixups[] =
  1033. {
  1034. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1035. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1036. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1037. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1038. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1039. /*
  1040. * Some MMC cards experience performance degradation with CMD23
  1041. * instead of CMD12-bounded multiblock transfers. For now we'll
  1042. * black list what's bad...
  1043. * - Certain Toshiba cards.
  1044. *
  1045. * N.B. This doesn't affect SD cards.
  1046. */
  1047. MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1048. MMC_QUIRK_BLK_NO_CMD23),
  1049. MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1050. MMC_QUIRK_BLK_NO_CMD23),
  1051. MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1052. MMC_QUIRK_BLK_NO_CMD23),
  1053. END_FIXUP
  1054. };
  1055. static int mmc_blk_probe(struct mmc_card *card)
  1056. {
  1057. struct mmc_blk_data *md, *part_md;
  1058. int err;
  1059. char cap_str[10];
  1060. /*
  1061. * Check that the card supports the command class(es) we need.
  1062. */
  1063. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1064. return -ENODEV;
  1065. md = mmc_blk_alloc(card);
  1066. if (IS_ERR(md))
  1067. return PTR_ERR(md);
  1068. err = mmc_blk_set_blksize(md, card);
  1069. if (err)
  1070. goto out;
  1071. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1072. cap_str, sizeof(cap_str));
  1073. printk(KERN_INFO "%s: %s %s %s %s\n",
  1074. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1075. cap_str, md->read_only ? "(ro)" : "");
  1076. if (mmc_blk_alloc_parts(card, md))
  1077. goto out;
  1078. mmc_set_drvdata(card, md);
  1079. mmc_fixup_device(card, blk_fixups);
  1080. if (mmc_add_disk(md))
  1081. goto out;
  1082. list_for_each_entry(part_md, &md->part, part) {
  1083. if (mmc_add_disk(part_md))
  1084. goto out;
  1085. }
  1086. return 0;
  1087. out:
  1088. mmc_blk_remove_parts(card, md);
  1089. mmc_blk_remove_req(md);
  1090. return err;
  1091. }
  1092. static void mmc_blk_remove(struct mmc_card *card)
  1093. {
  1094. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1095. mmc_blk_remove_parts(card, md);
  1096. mmc_blk_remove_req(md);
  1097. mmc_set_drvdata(card, NULL);
  1098. }
  1099. #ifdef CONFIG_PM
  1100. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1101. {
  1102. struct mmc_blk_data *part_md;
  1103. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1104. if (md) {
  1105. mmc_queue_suspend(&md->queue);
  1106. list_for_each_entry(part_md, &md->part, part) {
  1107. mmc_queue_suspend(&part_md->queue);
  1108. }
  1109. }
  1110. return 0;
  1111. }
  1112. static int mmc_blk_resume(struct mmc_card *card)
  1113. {
  1114. struct mmc_blk_data *part_md;
  1115. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1116. if (md) {
  1117. mmc_blk_set_blksize(md, card);
  1118. /*
  1119. * Resume involves the card going into idle state,
  1120. * so current partition is always the main one.
  1121. */
  1122. md->part_curr = md->part_type;
  1123. mmc_queue_resume(&md->queue);
  1124. list_for_each_entry(part_md, &md->part, part) {
  1125. mmc_queue_resume(&part_md->queue);
  1126. }
  1127. }
  1128. return 0;
  1129. }
  1130. #else
  1131. #define mmc_blk_suspend NULL
  1132. #define mmc_blk_resume NULL
  1133. #endif
  1134. static struct mmc_driver mmc_driver = {
  1135. .drv = {
  1136. .name = "mmcblk",
  1137. },
  1138. .probe = mmc_blk_probe,
  1139. .remove = mmc_blk_remove,
  1140. .suspend = mmc_blk_suspend,
  1141. .resume = mmc_blk_resume,
  1142. };
  1143. static int __init mmc_blk_init(void)
  1144. {
  1145. int res;
  1146. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1147. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1148. max_devices = 256 / perdev_minors;
  1149. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1150. if (res)
  1151. goto out;
  1152. res = mmc_register_driver(&mmc_driver);
  1153. if (res)
  1154. goto out2;
  1155. return 0;
  1156. out2:
  1157. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1158. out:
  1159. return res;
  1160. }
  1161. static void __exit mmc_blk_exit(void)
  1162. {
  1163. mmc_unregister_driver(&mmc_driver);
  1164. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1165. }
  1166. module_init(mmc_blk_init);
  1167. module_exit(mmc_blk_exit);
  1168. MODULE_LICENSE("GPL");
  1169. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");