block.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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/mmc/card.h>
  34. #include <linux/mmc/host.h>
  35. #include <linux/mmc/mmc.h>
  36. #include <linux/mmc/sd.h>
  37. #include <asm/system.h>
  38. #include <asm/uaccess.h>
  39. #include "queue.h"
  40. MODULE_ALIAS("mmc:block");
  41. #ifdef MODULE_PARAM_PREFIX
  42. #undef MODULE_PARAM_PREFIX
  43. #endif
  44. #define MODULE_PARAM_PREFIX "mmcblk."
  45. #define INAND_CMD38_ARG_EXT_CSD 113
  46. #define INAND_CMD38_ARG_ERASE 0x00
  47. #define INAND_CMD38_ARG_TRIM 0x01
  48. #define INAND_CMD38_ARG_SECERASE 0x80
  49. #define INAND_CMD38_ARG_SECTRIM1 0x81
  50. #define INAND_CMD38_ARG_SECTRIM2 0x88
  51. #define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) && \
  52. (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || \
  53. ((card)->ext_csd.rel_sectors)))
  54. static DEFINE_MUTEX(block_mutex);
  55. /*
  56. * The defaults come from config options but can be overriden by module
  57. * or bootarg options.
  58. */
  59. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  60. /*
  61. * We've only got one major, so number of mmcblk devices is
  62. * limited to 256 / number of minors per device.
  63. */
  64. static int max_devices;
  65. /* 256 minors, so at most 256 separate devices */
  66. static DECLARE_BITMAP(dev_use, 256);
  67. static DECLARE_BITMAP(name_use, 256);
  68. /*
  69. * There is one mmc_blk_data per slot.
  70. */
  71. struct mmc_blk_data {
  72. spinlock_t lock;
  73. struct gendisk *disk;
  74. struct mmc_queue queue;
  75. struct list_head part;
  76. unsigned int usage;
  77. unsigned int read_only;
  78. unsigned int part_type;
  79. unsigned int name_idx;
  80. /*
  81. * Only set in main mmc_blk_data associated
  82. * with mmc_card with mmc_set_drvdata, and keeps
  83. * track of the current selected device partition.
  84. */
  85. unsigned int part_curr;
  86. struct device_attribute force_ro;
  87. };
  88. static DEFINE_MUTEX(open_lock);
  89. module_param(perdev_minors, int, 0444);
  90. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  91. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  92. {
  93. struct mmc_blk_data *md;
  94. mutex_lock(&open_lock);
  95. md = disk->private_data;
  96. if (md && md->usage == 0)
  97. md = NULL;
  98. if (md)
  99. md->usage++;
  100. mutex_unlock(&open_lock);
  101. return md;
  102. }
  103. static inline int mmc_get_devidx(struct gendisk *disk)
  104. {
  105. int devmaj = MAJOR(disk_devt(disk));
  106. int devidx = MINOR(disk_devt(disk)) / perdev_minors;
  107. if (!devmaj)
  108. devidx = disk->first_minor / perdev_minors;
  109. return devidx;
  110. }
  111. static void mmc_blk_put(struct mmc_blk_data *md)
  112. {
  113. mutex_lock(&open_lock);
  114. md->usage--;
  115. if (md->usage == 0) {
  116. int devidx = mmc_get_devidx(md->disk);
  117. blk_cleanup_queue(md->queue.queue);
  118. __clear_bit(devidx, dev_use);
  119. put_disk(md->disk);
  120. kfree(md);
  121. }
  122. mutex_unlock(&open_lock);
  123. }
  124. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  125. char *buf)
  126. {
  127. int ret;
  128. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  129. ret = snprintf(buf, PAGE_SIZE, "%d",
  130. get_disk_ro(dev_to_disk(dev)) ^
  131. md->read_only);
  132. mmc_blk_put(md);
  133. return ret;
  134. }
  135. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  136. const char *buf, size_t count)
  137. {
  138. int ret;
  139. char *end;
  140. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  141. unsigned long set = simple_strtoul(buf, &end, 0);
  142. if (end == buf) {
  143. ret = -EINVAL;
  144. goto out;
  145. }
  146. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  147. ret = count;
  148. out:
  149. mmc_blk_put(md);
  150. return ret;
  151. }
  152. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  153. {
  154. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  155. int ret = -ENXIO;
  156. mutex_lock(&block_mutex);
  157. if (md) {
  158. if (md->usage == 2)
  159. check_disk_change(bdev);
  160. ret = 0;
  161. if ((mode & FMODE_WRITE) && md->read_only) {
  162. mmc_blk_put(md);
  163. ret = -EROFS;
  164. }
  165. }
  166. mutex_unlock(&block_mutex);
  167. return ret;
  168. }
  169. static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
  170. {
  171. struct mmc_blk_data *md = disk->private_data;
  172. mutex_lock(&block_mutex);
  173. mmc_blk_put(md);
  174. mutex_unlock(&block_mutex);
  175. return 0;
  176. }
  177. static int
  178. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  179. {
  180. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  181. geo->heads = 4;
  182. geo->sectors = 16;
  183. return 0;
  184. }
  185. static const struct block_device_operations mmc_bdops = {
  186. .open = mmc_blk_open,
  187. .release = mmc_blk_release,
  188. .getgeo = mmc_blk_getgeo,
  189. .owner = THIS_MODULE,
  190. };
  191. struct mmc_blk_request {
  192. struct mmc_request mrq;
  193. struct mmc_command cmd;
  194. struct mmc_command stop;
  195. struct mmc_data data;
  196. };
  197. static inline int mmc_blk_part_switch(struct mmc_card *card,
  198. struct mmc_blk_data *md)
  199. {
  200. int ret;
  201. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  202. if (main_md->part_curr == md->part_type)
  203. return 0;
  204. if (mmc_card_mmc(card)) {
  205. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  206. card->ext_csd.part_config |= md->part_type;
  207. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  208. EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
  209. card->ext_csd.part_time);
  210. if (ret)
  211. return ret;
  212. }
  213. main_md->part_curr = md->part_type;
  214. return 0;
  215. }
  216. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  217. {
  218. int err;
  219. u32 result;
  220. __be32 *blocks;
  221. struct mmc_request mrq = {0};
  222. struct mmc_command cmd = {0};
  223. struct mmc_data data = {0};
  224. unsigned int timeout_us;
  225. struct scatterlist sg;
  226. cmd.opcode = MMC_APP_CMD;
  227. cmd.arg = card->rca << 16;
  228. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  229. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  230. if (err)
  231. return (u32)-1;
  232. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  233. return (u32)-1;
  234. memset(&cmd, 0, sizeof(struct mmc_command));
  235. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  236. cmd.arg = 0;
  237. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  238. data.timeout_ns = card->csd.tacc_ns * 100;
  239. data.timeout_clks = card->csd.tacc_clks * 100;
  240. timeout_us = data.timeout_ns / 1000;
  241. timeout_us += data.timeout_clks * 1000 /
  242. (card->host->ios.clock / 1000);
  243. if (timeout_us > 100000) {
  244. data.timeout_ns = 100000000;
  245. data.timeout_clks = 0;
  246. }
  247. data.blksz = 4;
  248. data.blocks = 1;
  249. data.flags = MMC_DATA_READ;
  250. data.sg = &sg;
  251. data.sg_len = 1;
  252. mrq.cmd = &cmd;
  253. mrq.data = &data;
  254. blocks = kmalloc(4, GFP_KERNEL);
  255. if (!blocks)
  256. return (u32)-1;
  257. sg_init_one(&sg, blocks, 4);
  258. mmc_wait_for_req(card->host, &mrq);
  259. result = ntohl(*blocks);
  260. kfree(blocks);
  261. if (cmd.error || data.error)
  262. result = (u32)-1;
  263. return result;
  264. }
  265. static u32 get_card_status(struct mmc_card *card, struct request *req)
  266. {
  267. struct mmc_command cmd = {0};
  268. int err;
  269. cmd.opcode = MMC_SEND_STATUS;
  270. if (!mmc_host_is_spi(card->host))
  271. cmd.arg = card->rca << 16;
  272. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  273. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  274. if (err)
  275. printk(KERN_ERR "%s: error %d sending status command",
  276. req->rq_disk->disk_name, err);
  277. return cmd.resp[0];
  278. }
  279. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  280. {
  281. struct mmc_blk_data *md = mq->data;
  282. struct mmc_card *card = md->queue.card;
  283. unsigned int from, nr, arg;
  284. int err = 0;
  285. if (!mmc_can_erase(card)) {
  286. err = -EOPNOTSUPP;
  287. goto out;
  288. }
  289. from = blk_rq_pos(req);
  290. nr = blk_rq_sectors(req);
  291. if (mmc_can_trim(card))
  292. arg = MMC_TRIM_ARG;
  293. else
  294. arg = MMC_ERASE_ARG;
  295. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  296. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  297. INAND_CMD38_ARG_EXT_CSD,
  298. arg == MMC_TRIM_ARG ?
  299. INAND_CMD38_ARG_TRIM :
  300. INAND_CMD38_ARG_ERASE,
  301. 0);
  302. if (err)
  303. goto out;
  304. }
  305. err = mmc_erase(card, from, nr, arg);
  306. out:
  307. spin_lock_irq(&md->lock);
  308. __blk_end_request(req, err, blk_rq_bytes(req));
  309. spin_unlock_irq(&md->lock);
  310. return err ? 0 : 1;
  311. }
  312. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  313. struct request *req)
  314. {
  315. struct mmc_blk_data *md = mq->data;
  316. struct mmc_card *card = md->queue.card;
  317. unsigned int from, nr, arg;
  318. int err = 0;
  319. if (!mmc_can_secure_erase_trim(card)) {
  320. err = -EOPNOTSUPP;
  321. goto out;
  322. }
  323. from = blk_rq_pos(req);
  324. nr = blk_rq_sectors(req);
  325. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  326. arg = MMC_SECURE_TRIM1_ARG;
  327. else
  328. arg = MMC_SECURE_ERASE_ARG;
  329. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  330. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  331. INAND_CMD38_ARG_EXT_CSD,
  332. arg == MMC_SECURE_TRIM1_ARG ?
  333. INAND_CMD38_ARG_SECTRIM1 :
  334. INAND_CMD38_ARG_SECERASE,
  335. 0);
  336. if (err)
  337. goto out;
  338. }
  339. err = mmc_erase(card, from, nr, arg);
  340. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  341. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  342. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  343. INAND_CMD38_ARG_EXT_CSD,
  344. INAND_CMD38_ARG_SECTRIM2,
  345. 0);
  346. if (err)
  347. goto out;
  348. }
  349. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  350. }
  351. out:
  352. spin_lock_irq(&md->lock);
  353. __blk_end_request(req, err, blk_rq_bytes(req));
  354. spin_unlock_irq(&md->lock);
  355. return err ? 0 : 1;
  356. }
  357. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  358. {
  359. struct mmc_blk_data *md = mq->data;
  360. /*
  361. * No-op, only service this because we need REQ_FUA for reliable
  362. * writes.
  363. */
  364. spin_lock_irq(&md->lock);
  365. __blk_end_request_all(req, 0);
  366. spin_unlock_irq(&md->lock);
  367. return 1;
  368. }
  369. /*
  370. * Reformat current write as a reliable write, supporting
  371. * both legacy and the enhanced reliable write MMC cards.
  372. * In each transfer we'll handle only as much as a single
  373. * reliable write can handle, thus finish the request in
  374. * partial completions.
  375. */
  376. static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
  377. struct mmc_card *card,
  378. struct request *req)
  379. {
  380. int err;
  381. struct mmc_command set_count = {0};
  382. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  383. /* Legacy mode imposes restrictions on transfers. */
  384. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  385. brq->data.blocks = 1;
  386. if (brq->data.blocks > card->ext_csd.rel_sectors)
  387. brq->data.blocks = card->ext_csd.rel_sectors;
  388. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  389. brq->data.blocks = 1;
  390. }
  391. set_count.opcode = MMC_SET_BLOCK_COUNT;
  392. set_count.arg = brq->data.blocks | (1 << 31);
  393. set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
  394. err = mmc_wait_for_cmd(card->host, &set_count, 0);
  395. if (err)
  396. printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
  397. req->rq_disk->disk_name, err);
  398. return err;
  399. }
  400. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  401. {
  402. struct mmc_blk_data *md = mq->data;
  403. struct mmc_card *card = md->queue.card;
  404. struct mmc_blk_request brq;
  405. int ret = 1, disable_multi = 0;
  406. /*
  407. * Reliable writes are used to implement Forced Unit Access and
  408. * REQ_META accesses, and are supported only on MMCs.
  409. */
  410. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  411. (req->cmd_flags & REQ_META)) &&
  412. (rq_data_dir(req) == WRITE) &&
  413. REL_WRITES_SUPPORTED(card);
  414. do {
  415. struct mmc_command cmd = {0};
  416. u32 readcmd, writecmd, status = 0;
  417. memset(&brq, 0, sizeof(struct mmc_blk_request));
  418. brq.mrq.cmd = &brq.cmd;
  419. brq.mrq.data = &brq.data;
  420. brq.cmd.arg = blk_rq_pos(req);
  421. if (!mmc_card_blockaddr(card))
  422. brq.cmd.arg <<= 9;
  423. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  424. brq.data.blksz = 512;
  425. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  426. brq.stop.arg = 0;
  427. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  428. brq.data.blocks = blk_rq_sectors(req);
  429. /*
  430. * The block layer doesn't support all sector count
  431. * restrictions, so we need to be prepared for too big
  432. * requests.
  433. */
  434. if (brq.data.blocks > card->host->max_blk_count)
  435. brq.data.blocks = card->host->max_blk_count;
  436. /*
  437. * After a read error, we redo the request one sector at a time
  438. * in order to accurately determine which sectors can be read
  439. * successfully.
  440. */
  441. if (disable_multi && brq.data.blocks > 1)
  442. brq.data.blocks = 1;
  443. if (brq.data.blocks > 1 || do_rel_wr) {
  444. /* SPI multiblock writes terminate using a special
  445. * token, not a STOP_TRANSMISSION request. Reliable
  446. * writes use SET_BLOCK_COUNT and do not use a
  447. * STOP_TRANSMISSION request either.
  448. */
  449. if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
  450. rq_data_dir(req) == READ)
  451. brq.mrq.stop = &brq.stop;
  452. readcmd = MMC_READ_MULTIPLE_BLOCK;
  453. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  454. } else {
  455. brq.mrq.stop = NULL;
  456. readcmd = MMC_READ_SINGLE_BLOCK;
  457. writecmd = MMC_WRITE_BLOCK;
  458. }
  459. if (rq_data_dir(req) == READ) {
  460. brq.cmd.opcode = readcmd;
  461. brq.data.flags |= MMC_DATA_READ;
  462. } else {
  463. brq.cmd.opcode = writecmd;
  464. brq.data.flags |= MMC_DATA_WRITE;
  465. }
  466. if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
  467. goto cmd_err;
  468. mmc_set_data_timeout(&brq.data, card);
  469. brq.data.sg = mq->sg;
  470. brq.data.sg_len = mmc_queue_map_sg(mq);
  471. /*
  472. * Adjust the sg list so it is the same size as the
  473. * request.
  474. */
  475. if (brq.data.blocks != blk_rq_sectors(req)) {
  476. int i, data_size = brq.data.blocks << 9;
  477. struct scatterlist *sg;
  478. for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
  479. data_size -= sg->length;
  480. if (data_size <= 0) {
  481. sg->length += data_size;
  482. i++;
  483. break;
  484. }
  485. }
  486. brq.data.sg_len = i;
  487. }
  488. mmc_queue_bounce_pre(mq);
  489. mmc_wait_for_req(card->host, &brq.mrq);
  490. mmc_queue_bounce_post(mq);
  491. /*
  492. * Check for errors here, but don't jump to cmd_err
  493. * until later as we need to wait for the card to leave
  494. * programming mode even when things go wrong.
  495. */
  496. if (brq.cmd.error || brq.data.error || brq.stop.error) {
  497. if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
  498. /* Redo read one sector at a time */
  499. printk(KERN_WARNING "%s: retrying using single "
  500. "block read\n", req->rq_disk->disk_name);
  501. disable_multi = 1;
  502. continue;
  503. }
  504. status = get_card_status(card, req);
  505. }
  506. if (brq.cmd.error) {
  507. printk(KERN_ERR "%s: error %d sending read/write "
  508. "command, response %#x, card status %#x\n",
  509. req->rq_disk->disk_name, brq.cmd.error,
  510. brq.cmd.resp[0], status);
  511. }
  512. if (brq.data.error) {
  513. if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
  514. /* 'Stop' response contains card status */
  515. status = brq.mrq.stop->resp[0];
  516. printk(KERN_ERR "%s: error %d transferring data,"
  517. " sector %u, nr %u, card status %#x\n",
  518. req->rq_disk->disk_name, brq.data.error,
  519. (unsigned)blk_rq_pos(req),
  520. (unsigned)blk_rq_sectors(req), status);
  521. }
  522. if (brq.stop.error) {
  523. printk(KERN_ERR "%s: error %d sending stop command, "
  524. "response %#x, card status %#x\n",
  525. req->rq_disk->disk_name, brq.stop.error,
  526. brq.stop.resp[0], status);
  527. }
  528. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  529. do {
  530. int err;
  531. cmd.opcode = MMC_SEND_STATUS;
  532. cmd.arg = card->rca << 16;
  533. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  534. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  535. if (err) {
  536. printk(KERN_ERR "%s: error %d requesting status\n",
  537. req->rq_disk->disk_name, err);
  538. goto cmd_err;
  539. }
  540. /*
  541. * Some cards mishandle the status bits,
  542. * so make sure to check both the busy
  543. * indication and the card state.
  544. */
  545. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  546. (R1_CURRENT_STATE(cmd.resp[0]) == 7));
  547. #if 0
  548. if (cmd.resp[0] & ~0x00000900)
  549. printk(KERN_ERR "%s: status = %08x\n",
  550. req->rq_disk->disk_name, cmd.resp[0]);
  551. if (mmc_decode_status(cmd.resp))
  552. goto cmd_err;
  553. #endif
  554. }
  555. if (brq.cmd.error || brq.stop.error || brq.data.error) {
  556. if (rq_data_dir(req) == READ) {
  557. /*
  558. * After an error, we redo I/O one sector at a
  559. * time, so we only reach here after trying to
  560. * read a single sector.
  561. */
  562. spin_lock_irq(&md->lock);
  563. ret = __blk_end_request(req, -EIO, brq.data.blksz);
  564. spin_unlock_irq(&md->lock);
  565. continue;
  566. }
  567. goto cmd_err;
  568. }
  569. /*
  570. * A block was successfully transferred.
  571. */
  572. spin_lock_irq(&md->lock);
  573. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  574. spin_unlock_irq(&md->lock);
  575. } while (ret);
  576. return 1;
  577. cmd_err:
  578. /*
  579. * If this is an SD card and we're writing, we can first
  580. * mark the known good sectors as ok.
  581. *
  582. * If the card is not SD, we can still ok written sectors
  583. * as reported by the controller (which might be less than
  584. * the real number of written sectors, but never more).
  585. */
  586. if (mmc_card_sd(card)) {
  587. u32 blocks;
  588. blocks = mmc_sd_num_wr_blocks(card);
  589. if (blocks != (u32)-1) {
  590. spin_lock_irq(&md->lock);
  591. ret = __blk_end_request(req, 0, blocks << 9);
  592. spin_unlock_irq(&md->lock);
  593. }
  594. } else {
  595. spin_lock_irq(&md->lock);
  596. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  597. spin_unlock_irq(&md->lock);
  598. }
  599. spin_lock_irq(&md->lock);
  600. while (ret)
  601. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  602. spin_unlock_irq(&md->lock);
  603. return 0;
  604. }
  605. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  606. {
  607. int ret;
  608. struct mmc_blk_data *md = mq->data;
  609. struct mmc_card *card = md->queue.card;
  610. mmc_claim_host(card->host);
  611. ret = mmc_blk_part_switch(card, md);
  612. if (ret) {
  613. ret = 0;
  614. goto out;
  615. }
  616. if (req->cmd_flags & REQ_DISCARD) {
  617. if (req->cmd_flags & REQ_SECURE)
  618. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  619. else
  620. ret = mmc_blk_issue_discard_rq(mq, req);
  621. } else if (req->cmd_flags & REQ_FLUSH) {
  622. ret = mmc_blk_issue_flush(mq, req);
  623. } else {
  624. ret = mmc_blk_issue_rw_rq(mq, req);
  625. }
  626. out:
  627. mmc_release_host(card->host);
  628. return ret;
  629. }
  630. static inline int mmc_blk_readonly(struct mmc_card *card)
  631. {
  632. return mmc_card_readonly(card) ||
  633. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  634. }
  635. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  636. struct device *parent,
  637. sector_t size,
  638. bool default_ro,
  639. const char *subname)
  640. {
  641. struct mmc_blk_data *md;
  642. int devidx, ret;
  643. devidx = find_first_zero_bit(dev_use, max_devices);
  644. if (devidx >= max_devices)
  645. return ERR_PTR(-ENOSPC);
  646. __set_bit(devidx, dev_use);
  647. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  648. if (!md) {
  649. ret = -ENOMEM;
  650. goto out;
  651. }
  652. /*
  653. * !subname implies we are creating main mmc_blk_data that will be
  654. * associated with mmc_card with mmc_set_drvdata. Due to device
  655. * partitions, devidx will not coincide with a per-physical card
  656. * index anymore so we keep track of a name index.
  657. */
  658. if (!subname) {
  659. md->name_idx = find_first_zero_bit(name_use, max_devices);
  660. __set_bit(md->name_idx, name_use);
  661. }
  662. else
  663. md->name_idx = ((struct mmc_blk_data *)
  664. dev_to_disk(parent)->private_data)->name_idx;
  665. /*
  666. * Set the read-only status based on the supported commands
  667. * and the write protect switch.
  668. */
  669. md->read_only = mmc_blk_readonly(card);
  670. md->disk = alloc_disk(perdev_minors);
  671. if (md->disk == NULL) {
  672. ret = -ENOMEM;
  673. goto err_kfree;
  674. }
  675. spin_lock_init(&md->lock);
  676. INIT_LIST_HEAD(&md->part);
  677. md->usage = 1;
  678. ret = mmc_init_queue(&md->queue, card, &md->lock);
  679. if (ret)
  680. goto err_putdisk;
  681. md->queue.issue_fn = mmc_blk_issue_rq;
  682. md->queue.data = md;
  683. md->disk->major = MMC_BLOCK_MAJOR;
  684. md->disk->first_minor = devidx * perdev_minors;
  685. md->disk->fops = &mmc_bdops;
  686. md->disk->private_data = md;
  687. md->disk->queue = md->queue.queue;
  688. md->disk->driverfs_dev = parent;
  689. set_disk_ro(md->disk, md->read_only || default_ro);
  690. if (REL_WRITES_SUPPORTED(card))
  691. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  692. /*
  693. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  694. *
  695. * - be set for removable media with permanent block devices
  696. * - be unset for removable block devices with permanent media
  697. *
  698. * Since MMC block devices clearly fall under the second
  699. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  700. * should use the block device creation/destruction hotplug
  701. * messages to tell when the card is present.
  702. */
  703. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  704. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  705. blk_queue_logical_block_size(md->queue.queue, 512);
  706. set_capacity(md->disk, size);
  707. return md;
  708. err_putdisk:
  709. put_disk(md->disk);
  710. err_kfree:
  711. kfree(md);
  712. out:
  713. return ERR_PTR(ret);
  714. }
  715. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  716. {
  717. sector_t size;
  718. struct mmc_blk_data *md;
  719. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  720. /*
  721. * The EXT_CSD sector count is in number or 512 byte
  722. * sectors.
  723. */
  724. size = card->ext_csd.sectors;
  725. } else {
  726. /*
  727. * The CSD capacity field is in units of read_blkbits.
  728. * set_capacity takes units of 512 bytes.
  729. */
  730. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  731. }
  732. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  733. return md;
  734. }
  735. static int mmc_blk_alloc_part(struct mmc_card *card,
  736. struct mmc_blk_data *md,
  737. unsigned int part_type,
  738. sector_t size,
  739. bool default_ro,
  740. const char *subname)
  741. {
  742. char cap_str[10];
  743. struct mmc_blk_data *part_md;
  744. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  745. subname);
  746. if (IS_ERR(part_md))
  747. return PTR_ERR(part_md);
  748. part_md->part_type = part_type;
  749. list_add(&part_md->part, &md->part);
  750. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  751. cap_str, sizeof(cap_str));
  752. printk(KERN_INFO "%s: %s %s partition %u %s\n",
  753. part_md->disk->disk_name, mmc_card_id(card),
  754. mmc_card_name(card), part_md->part_type, cap_str);
  755. return 0;
  756. }
  757. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  758. {
  759. int ret = 0;
  760. if (!mmc_card_mmc(card))
  761. return 0;
  762. if (card->ext_csd.boot_size) {
  763. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
  764. card->ext_csd.boot_size >> 9,
  765. true,
  766. "boot0");
  767. if (ret)
  768. return ret;
  769. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
  770. card->ext_csd.boot_size >> 9,
  771. true,
  772. "boot1");
  773. if (ret)
  774. return ret;
  775. }
  776. return ret;
  777. }
  778. static int
  779. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  780. {
  781. int err;
  782. mmc_claim_host(card->host);
  783. err = mmc_set_blocklen(card, 512);
  784. mmc_release_host(card->host);
  785. if (err) {
  786. printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
  787. md->disk->disk_name, err);
  788. return -EINVAL;
  789. }
  790. return 0;
  791. }
  792. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  793. {
  794. if (md) {
  795. if (md->disk->flags & GENHD_FL_UP) {
  796. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  797. /* Stop new requests from getting into the queue */
  798. del_gendisk(md->disk);
  799. }
  800. /* Then flush out any already in there */
  801. mmc_cleanup_queue(&md->queue);
  802. mmc_blk_put(md);
  803. }
  804. }
  805. static void mmc_blk_remove_parts(struct mmc_card *card,
  806. struct mmc_blk_data *md)
  807. {
  808. struct list_head *pos, *q;
  809. struct mmc_blk_data *part_md;
  810. __clear_bit(md->name_idx, name_use);
  811. list_for_each_safe(pos, q, &md->part) {
  812. part_md = list_entry(pos, struct mmc_blk_data, part);
  813. list_del(pos);
  814. mmc_blk_remove_req(part_md);
  815. }
  816. }
  817. static int mmc_add_disk(struct mmc_blk_data *md)
  818. {
  819. int ret;
  820. add_disk(md->disk);
  821. md->force_ro.show = force_ro_show;
  822. md->force_ro.store = force_ro_store;
  823. sysfs_attr_init(&md->force_ro.attr);
  824. md->force_ro.attr.name = "force_ro";
  825. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  826. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  827. if (ret)
  828. del_gendisk(md->disk);
  829. return ret;
  830. }
  831. static const struct mmc_fixup blk_fixups[] =
  832. {
  833. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  834. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  835. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  836. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  837. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  838. END_FIXUP
  839. };
  840. static int mmc_blk_probe(struct mmc_card *card)
  841. {
  842. struct mmc_blk_data *md, *part_md;
  843. int err;
  844. char cap_str[10];
  845. /*
  846. * Check that the card supports the command class(es) we need.
  847. */
  848. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  849. return -ENODEV;
  850. md = mmc_blk_alloc(card);
  851. if (IS_ERR(md))
  852. return PTR_ERR(md);
  853. err = mmc_blk_set_blksize(md, card);
  854. if (err)
  855. goto out;
  856. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  857. cap_str, sizeof(cap_str));
  858. printk(KERN_INFO "%s: %s %s %s %s\n",
  859. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  860. cap_str, md->read_only ? "(ro)" : "");
  861. if (mmc_blk_alloc_parts(card, md))
  862. goto out;
  863. mmc_set_drvdata(card, md);
  864. mmc_fixup_device(card, blk_fixups);
  865. if (mmc_add_disk(md))
  866. goto out;
  867. list_for_each_entry(part_md, &md->part, part) {
  868. if (mmc_add_disk(part_md))
  869. goto out;
  870. }
  871. return 0;
  872. out:
  873. mmc_blk_remove_parts(card, md);
  874. mmc_blk_remove_req(md);
  875. return err;
  876. }
  877. static void mmc_blk_remove(struct mmc_card *card)
  878. {
  879. struct mmc_blk_data *md = mmc_get_drvdata(card);
  880. mmc_blk_remove_parts(card, md);
  881. mmc_blk_remove_req(md);
  882. mmc_set_drvdata(card, NULL);
  883. }
  884. #ifdef CONFIG_PM
  885. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  886. {
  887. struct mmc_blk_data *part_md;
  888. struct mmc_blk_data *md = mmc_get_drvdata(card);
  889. if (md) {
  890. mmc_queue_suspend(&md->queue);
  891. list_for_each_entry(part_md, &md->part, part) {
  892. mmc_queue_suspend(&part_md->queue);
  893. }
  894. }
  895. return 0;
  896. }
  897. static int mmc_blk_resume(struct mmc_card *card)
  898. {
  899. struct mmc_blk_data *part_md;
  900. struct mmc_blk_data *md = mmc_get_drvdata(card);
  901. if (md) {
  902. mmc_blk_set_blksize(md, card);
  903. /*
  904. * Resume involves the card going into idle state,
  905. * so current partition is always the main one.
  906. */
  907. md->part_curr = md->part_type;
  908. mmc_queue_resume(&md->queue);
  909. list_for_each_entry(part_md, &md->part, part) {
  910. mmc_queue_resume(&part_md->queue);
  911. }
  912. }
  913. return 0;
  914. }
  915. #else
  916. #define mmc_blk_suspend NULL
  917. #define mmc_blk_resume NULL
  918. #endif
  919. static struct mmc_driver mmc_driver = {
  920. .drv = {
  921. .name = "mmcblk",
  922. },
  923. .probe = mmc_blk_probe,
  924. .remove = mmc_blk_remove,
  925. .suspend = mmc_blk_suspend,
  926. .resume = mmc_blk_resume,
  927. };
  928. static int __init mmc_blk_init(void)
  929. {
  930. int res;
  931. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  932. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  933. max_devices = 256 / perdev_minors;
  934. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  935. if (res)
  936. goto out;
  937. res = mmc_register_driver(&mmc_driver);
  938. if (res)
  939. goto out2;
  940. return 0;
  941. out2:
  942. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  943. out:
  944. return res;
  945. }
  946. static void __exit mmc_blk_exit(void)
  947. {
  948. mmc_unregister_driver(&mmc_driver);
  949. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  950. }
  951. module_init(mmc_blk_init);
  952. module_exit(mmc_blk_exit);
  953. MODULE_LICENSE("GPL");
  954. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");