block.c 25 KB

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