block.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. static inline int mmc_blk_part_switch(struct mmc_card *card,
  362. struct mmc_blk_data *md)
  363. {
  364. int ret;
  365. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  366. if (main_md->part_curr == md->part_type)
  367. return 0;
  368. if (mmc_card_mmc(card)) {
  369. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  370. card->ext_csd.part_config |= md->part_type;
  371. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  372. EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
  373. card->ext_csd.part_time);
  374. if (ret)
  375. return ret;
  376. }
  377. main_md->part_curr = md->part_type;
  378. return 0;
  379. }
  380. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  381. {
  382. int err;
  383. u32 result;
  384. __be32 *blocks;
  385. struct mmc_request mrq = {0};
  386. struct mmc_command cmd = {0};
  387. struct mmc_data data = {0};
  388. unsigned int timeout_us;
  389. struct scatterlist sg;
  390. cmd.opcode = MMC_APP_CMD;
  391. cmd.arg = card->rca << 16;
  392. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  393. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  394. if (err)
  395. return (u32)-1;
  396. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  397. return (u32)-1;
  398. memset(&cmd, 0, sizeof(struct mmc_command));
  399. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  400. cmd.arg = 0;
  401. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  402. data.timeout_ns = card->csd.tacc_ns * 100;
  403. data.timeout_clks = card->csd.tacc_clks * 100;
  404. timeout_us = data.timeout_ns / 1000;
  405. timeout_us += data.timeout_clks * 1000 /
  406. (card->host->ios.clock / 1000);
  407. if (timeout_us > 100000) {
  408. data.timeout_ns = 100000000;
  409. data.timeout_clks = 0;
  410. }
  411. data.blksz = 4;
  412. data.blocks = 1;
  413. data.flags = MMC_DATA_READ;
  414. data.sg = &sg;
  415. data.sg_len = 1;
  416. mrq.cmd = &cmd;
  417. mrq.data = &data;
  418. blocks = kmalloc(4, GFP_KERNEL);
  419. if (!blocks)
  420. return (u32)-1;
  421. sg_init_one(&sg, blocks, 4);
  422. mmc_wait_for_req(card->host, &mrq);
  423. result = ntohl(*blocks);
  424. kfree(blocks);
  425. if (cmd.error || data.error)
  426. result = (u32)-1;
  427. return result;
  428. }
  429. static int send_stop(struct mmc_card *card, u32 *status)
  430. {
  431. struct mmc_command cmd = {0};
  432. int err;
  433. cmd.opcode = MMC_STOP_TRANSMISSION;
  434. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  435. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  436. if (err == 0)
  437. *status = cmd.resp[0];
  438. return err;
  439. }
  440. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  441. {
  442. struct mmc_command cmd = {0};
  443. int err;
  444. cmd.opcode = MMC_SEND_STATUS;
  445. if (!mmc_host_is_spi(card->host))
  446. cmd.arg = card->rca << 16;
  447. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  448. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  449. if (err == 0)
  450. *status = cmd.resp[0];
  451. return err;
  452. }
  453. #define ERR_RETRY 2
  454. #define ERR_ABORT 1
  455. #define ERR_CONTINUE 0
  456. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  457. bool status_valid, u32 status)
  458. {
  459. switch (error) {
  460. case -EILSEQ:
  461. /* response crc error, retry the r/w cmd */
  462. pr_err("%s: %s sending %s command, card status %#x\n",
  463. req->rq_disk->disk_name, "response CRC error",
  464. name, status);
  465. return ERR_RETRY;
  466. case -ETIMEDOUT:
  467. pr_err("%s: %s sending %s command, card status %#x\n",
  468. req->rq_disk->disk_name, "timed out", name, status);
  469. /* If the status cmd initially failed, retry the r/w cmd */
  470. if (!status_valid)
  471. return ERR_RETRY;
  472. /*
  473. * If it was a r/w cmd crc error, or illegal command
  474. * (eg, issued in wrong state) then retry - we should
  475. * have corrected the state problem above.
  476. */
  477. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
  478. return ERR_RETRY;
  479. /* Otherwise abort the command */
  480. return ERR_ABORT;
  481. default:
  482. /* We don't understand the error code the driver gave us */
  483. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  484. req->rq_disk->disk_name, error, status);
  485. return ERR_ABORT;
  486. }
  487. }
  488. /*
  489. * Initial r/w and stop cmd error recovery.
  490. * We don't know whether the card received the r/w cmd or not, so try to
  491. * restore things back to a sane state. Essentially, we do this as follows:
  492. * - Obtain card status. If the first attempt to obtain card status fails,
  493. * the status word will reflect the failed status cmd, not the failed
  494. * r/w cmd. If we fail to obtain card status, it suggests we can no
  495. * longer communicate with the card.
  496. * - Check the card state. If the card received the cmd but there was a
  497. * transient problem with the response, it might still be in a data transfer
  498. * mode. Try to send it a stop command. If this fails, we can't recover.
  499. * - If the r/w cmd failed due to a response CRC error, it was probably
  500. * transient, so retry the cmd.
  501. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  502. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  503. * illegal cmd, retry.
  504. * Otherwise we don't understand what happened, so abort.
  505. */
  506. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  507. struct mmc_blk_request *brq)
  508. {
  509. bool prev_cmd_status_valid = true;
  510. u32 status, stop_status = 0;
  511. int err, retry;
  512. /*
  513. * Try to get card status which indicates both the card state
  514. * and why there was no response. If the first attempt fails,
  515. * we can't be sure the returned status is for the r/w command.
  516. */
  517. for (retry = 2; retry >= 0; retry--) {
  518. err = get_card_status(card, &status, 0);
  519. if (!err)
  520. break;
  521. prev_cmd_status_valid = false;
  522. pr_err("%s: error %d sending status command, %sing\n",
  523. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  524. }
  525. /* We couldn't get a response from the card. Give up. */
  526. if (err)
  527. return ERR_ABORT;
  528. /*
  529. * Check the current card state. If it is in some data transfer
  530. * mode, tell it to stop (and hopefully transition back to TRAN.)
  531. */
  532. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  533. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  534. err = send_stop(card, &stop_status);
  535. if (err)
  536. pr_err("%s: error %d sending stop command\n",
  537. req->rq_disk->disk_name, err);
  538. /*
  539. * If the stop cmd also timed out, the card is probably
  540. * not present, so abort. Other errors are bad news too.
  541. */
  542. if (err)
  543. return ERR_ABORT;
  544. }
  545. /* Check for set block count errors */
  546. if (brq->sbc.error)
  547. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  548. prev_cmd_status_valid, status);
  549. /* Check for r/w command errors */
  550. if (brq->cmd.error)
  551. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  552. prev_cmd_status_valid, status);
  553. /* Now for stop errors. These aren't fatal to the transfer. */
  554. pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  555. req->rq_disk->disk_name, brq->stop.error,
  556. brq->cmd.resp[0], status);
  557. /*
  558. * Subsitute in our own stop status as this will give the error
  559. * state which happened during the execution of the r/w command.
  560. */
  561. if (stop_status) {
  562. brq->stop.resp[0] = stop_status;
  563. brq->stop.error = 0;
  564. }
  565. return ERR_CONTINUE;
  566. }
  567. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  568. {
  569. struct mmc_blk_data *md = mq->data;
  570. struct mmc_card *card = md->queue.card;
  571. unsigned int from, nr, arg;
  572. int err = 0;
  573. if (!mmc_can_erase(card)) {
  574. err = -EOPNOTSUPP;
  575. goto out;
  576. }
  577. from = blk_rq_pos(req);
  578. nr = blk_rq_sectors(req);
  579. if (mmc_can_trim(card))
  580. arg = MMC_TRIM_ARG;
  581. else
  582. arg = MMC_ERASE_ARG;
  583. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  584. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  585. INAND_CMD38_ARG_EXT_CSD,
  586. arg == MMC_TRIM_ARG ?
  587. INAND_CMD38_ARG_TRIM :
  588. INAND_CMD38_ARG_ERASE,
  589. 0);
  590. if (err)
  591. goto out;
  592. }
  593. err = mmc_erase(card, from, nr, arg);
  594. out:
  595. spin_lock_irq(&md->lock);
  596. __blk_end_request(req, err, blk_rq_bytes(req));
  597. spin_unlock_irq(&md->lock);
  598. return err ? 0 : 1;
  599. }
  600. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  601. struct request *req)
  602. {
  603. struct mmc_blk_data *md = mq->data;
  604. struct mmc_card *card = md->queue.card;
  605. unsigned int from, nr, arg;
  606. int err = 0;
  607. if (!mmc_can_secure_erase_trim(card)) {
  608. err = -EOPNOTSUPP;
  609. goto out;
  610. }
  611. from = blk_rq_pos(req);
  612. nr = blk_rq_sectors(req);
  613. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  614. arg = MMC_SECURE_TRIM1_ARG;
  615. else
  616. arg = MMC_SECURE_ERASE_ARG;
  617. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  618. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  619. INAND_CMD38_ARG_EXT_CSD,
  620. arg == MMC_SECURE_TRIM1_ARG ?
  621. INAND_CMD38_ARG_SECTRIM1 :
  622. INAND_CMD38_ARG_SECERASE,
  623. 0);
  624. if (err)
  625. goto out;
  626. }
  627. err = mmc_erase(card, from, nr, arg);
  628. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  629. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  630. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  631. INAND_CMD38_ARG_EXT_CSD,
  632. INAND_CMD38_ARG_SECTRIM2,
  633. 0);
  634. if (err)
  635. goto out;
  636. }
  637. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  638. }
  639. out:
  640. spin_lock_irq(&md->lock);
  641. __blk_end_request(req, err, blk_rq_bytes(req));
  642. spin_unlock_irq(&md->lock);
  643. return err ? 0 : 1;
  644. }
  645. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  646. {
  647. struct mmc_blk_data *md = mq->data;
  648. /*
  649. * No-op, only service this because we need REQ_FUA for reliable
  650. * writes.
  651. */
  652. spin_lock_irq(&md->lock);
  653. __blk_end_request_all(req, 0);
  654. spin_unlock_irq(&md->lock);
  655. return 1;
  656. }
  657. /*
  658. * Reformat current write as a reliable write, supporting
  659. * both legacy and the enhanced reliable write MMC cards.
  660. * In each transfer we'll handle only as much as a single
  661. * reliable write can handle, thus finish the request in
  662. * partial completions.
  663. */
  664. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  665. struct mmc_card *card,
  666. struct request *req)
  667. {
  668. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  669. /* Legacy mode imposes restrictions on transfers. */
  670. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  671. brq->data.blocks = 1;
  672. if (brq->data.blocks > card->ext_csd.rel_sectors)
  673. brq->data.blocks = card->ext_csd.rel_sectors;
  674. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  675. brq->data.blocks = 1;
  676. }
  677. }
  678. #define CMD_ERRORS \
  679. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  680. R1_ADDRESS_ERROR | /* Misaligned address */ \
  681. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  682. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  683. R1_CC_ERROR | /* Card controller error */ \
  684. R1_ERROR) /* General/unknown error */
  685. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  686. {
  687. struct mmc_blk_data *md = mq->data;
  688. struct mmc_card *card = md->queue.card;
  689. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  690. int ret = 1, disable_multi = 0, retry = 0;
  691. /*
  692. * Reliable writes are used to implement Forced Unit Access and
  693. * REQ_META accesses, and are supported only on MMCs.
  694. */
  695. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  696. (req->cmd_flags & REQ_META)) &&
  697. (rq_data_dir(req) == WRITE) &&
  698. (md->flags & MMC_BLK_REL_WR);
  699. do {
  700. u32 readcmd, writecmd;
  701. memset(brq, 0, sizeof(struct mmc_blk_request));
  702. brq->mrq.cmd = &brq->cmd;
  703. brq->mrq.data = &brq->data;
  704. brq->cmd.arg = blk_rq_pos(req);
  705. if (!mmc_card_blockaddr(card))
  706. brq->cmd.arg <<= 9;
  707. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  708. brq->data.blksz = 512;
  709. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  710. brq->stop.arg = 0;
  711. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  712. brq->data.blocks = blk_rq_sectors(req);
  713. /*
  714. * The block layer doesn't support all sector count
  715. * restrictions, so we need to be prepared for too big
  716. * requests.
  717. */
  718. if (brq->data.blocks > card->host->max_blk_count)
  719. brq->data.blocks = card->host->max_blk_count;
  720. /*
  721. * After a read error, we redo the request one sector at a time
  722. * in order to accurately determine which sectors can be read
  723. * successfully.
  724. */
  725. if (disable_multi && brq->data.blocks > 1)
  726. brq->data.blocks = 1;
  727. if (brq->data.blocks > 1 || do_rel_wr) {
  728. /* SPI multiblock writes terminate using a special
  729. * token, not a STOP_TRANSMISSION request.
  730. */
  731. if (!mmc_host_is_spi(card->host) ||
  732. rq_data_dir(req) == READ)
  733. brq->mrq.stop = &brq->stop;
  734. readcmd = MMC_READ_MULTIPLE_BLOCK;
  735. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  736. } else {
  737. brq->mrq.stop = NULL;
  738. readcmd = MMC_READ_SINGLE_BLOCK;
  739. writecmd = MMC_WRITE_BLOCK;
  740. }
  741. if (rq_data_dir(req) == READ) {
  742. brq->cmd.opcode = readcmd;
  743. brq->data.flags |= MMC_DATA_READ;
  744. } else {
  745. brq->cmd.opcode = writecmd;
  746. brq->data.flags |= MMC_DATA_WRITE;
  747. }
  748. if (do_rel_wr)
  749. mmc_apply_rel_rw(brq, card, req);
  750. /*
  751. * Pre-defined multi-block transfers are preferable to
  752. * open ended-ones (and necessary for reliable writes).
  753. * However, it is not sufficient to just send CMD23,
  754. * and avoid the final CMD12, as on an error condition
  755. * CMD12 (stop) needs to be sent anyway. This, coupled
  756. * with Auto-CMD23 enhancements provided by some
  757. * hosts, means that the complexity of dealing
  758. * with this is best left to the host. If CMD23 is
  759. * supported by card and host, we'll fill sbc in and let
  760. * the host deal with handling it correctly. This means
  761. * that for hosts that don't expose MMC_CAP_CMD23, no
  762. * change of behavior will be observed.
  763. *
  764. * N.B: Some MMC cards experience perf degradation.
  765. * We'll avoid using CMD23-bounded multiblock writes for
  766. * these, while retaining features like reliable writes.
  767. */
  768. if ((md->flags & MMC_BLK_CMD23) &&
  769. mmc_op_multi(brq->cmd.opcode) &&
  770. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
  771. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  772. brq->sbc.arg = brq->data.blocks |
  773. (do_rel_wr ? (1 << 31) : 0);
  774. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  775. brq->mrq.sbc = &brq->sbc;
  776. }
  777. mmc_set_data_timeout(&brq->data, card);
  778. brq->data.sg = mq->mqrq_cur->sg;
  779. brq->data.sg_len = mmc_queue_map_sg(mq, mq->mqrq_cur);
  780. /*
  781. * Adjust the sg list so it is the same size as the
  782. * request.
  783. */
  784. if (brq->data.blocks != blk_rq_sectors(req)) {
  785. int i, data_size = brq->data.blocks << 9;
  786. struct scatterlist *sg;
  787. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  788. data_size -= sg->length;
  789. if (data_size <= 0) {
  790. sg->length += data_size;
  791. i++;
  792. break;
  793. }
  794. }
  795. brq->data.sg_len = i;
  796. }
  797. mmc_queue_bounce_pre(mq->mqrq_cur);
  798. mmc_wait_for_req(card->host, &brq->mrq);
  799. mmc_queue_bounce_post(mq->mqrq_cur);
  800. /*
  801. * sbc.error indicates a problem with the set block count
  802. * command. No data will have been transferred.
  803. *
  804. * cmd.error indicates a problem with the r/w command. No
  805. * data will have been transferred.
  806. *
  807. * stop.error indicates a problem with the stop command. Data
  808. * may have been transferred, or may still be transferring.
  809. */
  810. if (brq->sbc.error || brq->cmd.error || brq->stop.error) {
  811. switch (mmc_blk_cmd_recovery(card, req, brq)) {
  812. case ERR_RETRY:
  813. if (retry++ < 5)
  814. continue;
  815. case ERR_ABORT:
  816. goto cmd_abort;
  817. case ERR_CONTINUE:
  818. break;
  819. }
  820. }
  821. /*
  822. * Check for errors relating to the execution of the
  823. * initial command - such as address errors. No data
  824. * has been transferred.
  825. */
  826. if (brq->cmd.resp[0] & CMD_ERRORS) {
  827. pr_err("%s: r/w command failed, status = %#x\n",
  828. req->rq_disk->disk_name, brq->cmd.resp[0]);
  829. goto cmd_abort;
  830. }
  831. /*
  832. * Everything else is either success, or a data error of some
  833. * kind. If it was a write, we may have transitioned to
  834. * program mode, which we have to wait for it to complete.
  835. */
  836. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  837. u32 status;
  838. do {
  839. int err = get_card_status(card, &status, 5);
  840. if (err) {
  841. printk(KERN_ERR "%s: error %d requesting status\n",
  842. req->rq_disk->disk_name, err);
  843. goto cmd_err;
  844. }
  845. /*
  846. * Some cards mishandle the status bits,
  847. * so make sure to check both the busy
  848. * indication and the card state.
  849. */
  850. } while (!(status & R1_READY_FOR_DATA) ||
  851. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  852. }
  853. if (brq->data.error) {
  854. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  855. req->rq_disk->disk_name, brq->data.error,
  856. (unsigned)blk_rq_pos(req),
  857. (unsigned)blk_rq_sectors(req),
  858. brq->cmd.resp[0], brq->stop.resp[0]);
  859. if (rq_data_dir(req) == READ) {
  860. if (brq->data.blocks > 1) {
  861. /* Redo read one sector at a time */
  862. pr_warning("%s: retrying using single block read\n",
  863. req->rq_disk->disk_name);
  864. disable_multi = 1;
  865. continue;
  866. }
  867. /*
  868. * After an error, we redo I/O one sector at a
  869. * time, so we only reach here after trying to
  870. * read a single sector.
  871. */
  872. spin_lock_irq(&md->lock);
  873. ret = __blk_end_request(req, -EIO,
  874. brq->data.blksz);
  875. spin_unlock_irq(&md->lock);
  876. continue;
  877. } else {
  878. goto cmd_err;
  879. }
  880. }
  881. /*
  882. * A block was successfully transferred.
  883. */
  884. spin_lock_irq(&md->lock);
  885. ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
  886. spin_unlock_irq(&md->lock);
  887. } while (ret);
  888. return 1;
  889. cmd_err:
  890. /*
  891. * If this is an SD card and we're writing, we can first
  892. * mark the known good sectors as ok.
  893. *
  894. * If the card is not SD, we can still ok written sectors
  895. * as reported by the controller (which might be less than
  896. * the real number of written sectors, but never more).
  897. */
  898. if (mmc_card_sd(card)) {
  899. u32 blocks;
  900. blocks = mmc_sd_num_wr_blocks(card);
  901. if (blocks != (u32)-1) {
  902. spin_lock_irq(&md->lock);
  903. ret = __blk_end_request(req, 0, blocks << 9);
  904. spin_unlock_irq(&md->lock);
  905. }
  906. } else {
  907. spin_lock_irq(&md->lock);
  908. ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
  909. spin_unlock_irq(&md->lock);
  910. }
  911. cmd_abort:
  912. spin_lock_irq(&md->lock);
  913. while (ret)
  914. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  915. spin_unlock_irq(&md->lock);
  916. return 0;
  917. }
  918. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  919. {
  920. int ret;
  921. struct mmc_blk_data *md = mq->data;
  922. struct mmc_card *card = md->queue.card;
  923. mmc_claim_host(card->host);
  924. ret = mmc_blk_part_switch(card, md);
  925. if (ret) {
  926. ret = 0;
  927. goto out;
  928. }
  929. if (req->cmd_flags & REQ_DISCARD) {
  930. if (req->cmd_flags & REQ_SECURE)
  931. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  932. else
  933. ret = mmc_blk_issue_discard_rq(mq, req);
  934. } else if (req->cmd_flags & REQ_FLUSH) {
  935. ret = mmc_blk_issue_flush(mq, req);
  936. } else {
  937. ret = mmc_blk_issue_rw_rq(mq, req);
  938. }
  939. out:
  940. mmc_release_host(card->host);
  941. return ret;
  942. }
  943. static inline int mmc_blk_readonly(struct mmc_card *card)
  944. {
  945. return mmc_card_readonly(card) ||
  946. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  947. }
  948. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  949. struct device *parent,
  950. sector_t size,
  951. bool default_ro,
  952. const char *subname)
  953. {
  954. struct mmc_blk_data *md;
  955. int devidx, ret;
  956. devidx = find_first_zero_bit(dev_use, max_devices);
  957. if (devidx >= max_devices)
  958. return ERR_PTR(-ENOSPC);
  959. __set_bit(devidx, dev_use);
  960. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  961. if (!md) {
  962. ret = -ENOMEM;
  963. goto out;
  964. }
  965. /*
  966. * !subname implies we are creating main mmc_blk_data that will be
  967. * associated with mmc_card with mmc_set_drvdata. Due to device
  968. * partitions, devidx will not coincide with a per-physical card
  969. * index anymore so we keep track of a name index.
  970. */
  971. if (!subname) {
  972. md->name_idx = find_first_zero_bit(name_use, max_devices);
  973. __set_bit(md->name_idx, name_use);
  974. }
  975. else
  976. md->name_idx = ((struct mmc_blk_data *)
  977. dev_to_disk(parent)->private_data)->name_idx;
  978. /*
  979. * Set the read-only status based on the supported commands
  980. * and the write protect switch.
  981. */
  982. md->read_only = mmc_blk_readonly(card);
  983. md->disk = alloc_disk(perdev_minors);
  984. if (md->disk == NULL) {
  985. ret = -ENOMEM;
  986. goto err_kfree;
  987. }
  988. spin_lock_init(&md->lock);
  989. INIT_LIST_HEAD(&md->part);
  990. md->usage = 1;
  991. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  992. if (ret)
  993. goto err_putdisk;
  994. md->queue.issue_fn = mmc_blk_issue_rq;
  995. md->queue.data = md;
  996. md->disk->major = MMC_BLOCK_MAJOR;
  997. md->disk->first_minor = devidx * perdev_minors;
  998. md->disk->fops = &mmc_bdops;
  999. md->disk->private_data = md;
  1000. md->disk->queue = md->queue.queue;
  1001. md->disk->driverfs_dev = parent;
  1002. set_disk_ro(md->disk, md->read_only || default_ro);
  1003. /*
  1004. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1005. *
  1006. * - be set for removable media with permanent block devices
  1007. * - be unset for removable block devices with permanent media
  1008. *
  1009. * Since MMC block devices clearly fall under the second
  1010. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1011. * should use the block device creation/destruction hotplug
  1012. * messages to tell when the card is present.
  1013. */
  1014. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1015. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1016. blk_queue_logical_block_size(md->queue.queue, 512);
  1017. set_capacity(md->disk, size);
  1018. if (mmc_host_cmd23(card->host)) {
  1019. if (mmc_card_mmc(card) ||
  1020. (mmc_card_sd(card) &&
  1021. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1022. md->flags |= MMC_BLK_CMD23;
  1023. }
  1024. if (mmc_card_mmc(card) &&
  1025. md->flags & MMC_BLK_CMD23 &&
  1026. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1027. card->ext_csd.rel_sectors)) {
  1028. md->flags |= MMC_BLK_REL_WR;
  1029. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1030. }
  1031. return md;
  1032. err_putdisk:
  1033. put_disk(md->disk);
  1034. err_kfree:
  1035. kfree(md);
  1036. out:
  1037. return ERR_PTR(ret);
  1038. }
  1039. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1040. {
  1041. sector_t size;
  1042. struct mmc_blk_data *md;
  1043. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1044. /*
  1045. * The EXT_CSD sector count is in number or 512 byte
  1046. * sectors.
  1047. */
  1048. size = card->ext_csd.sectors;
  1049. } else {
  1050. /*
  1051. * The CSD capacity field is in units of read_blkbits.
  1052. * set_capacity takes units of 512 bytes.
  1053. */
  1054. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1055. }
  1056. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  1057. return md;
  1058. }
  1059. static int mmc_blk_alloc_part(struct mmc_card *card,
  1060. struct mmc_blk_data *md,
  1061. unsigned int part_type,
  1062. sector_t size,
  1063. bool default_ro,
  1064. const char *subname)
  1065. {
  1066. char cap_str[10];
  1067. struct mmc_blk_data *part_md;
  1068. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1069. subname);
  1070. if (IS_ERR(part_md))
  1071. return PTR_ERR(part_md);
  1072. part_md->part_type = part_type;
  1073. list_add(&part_md->part, &md->part);
  1074. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1075. cap_str, sizeof(cap_str));
  1076. printk(KERN_INFO "%s: %s %s partition %u %s\n",
  1077. part_md->disk->disk_name, mmc_card_id(card),
  1078. mmc_card_name(card), part_md->part_type, cap_str);
  1079. return 0;
  1080. }
  1081. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1082. {
  1083. int ret = 0;
  1084. if (!mmc_card_mmc(card))
  1085. return 0;
  1086. if (card->ext_csd.boot_size) {
  1087. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
  1088. card->ext_csd.boot_size >> 9,
  1089. true,
  1090. "boot0");
  1091. if (ret)
  1092. return ret;
  1093. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
  1094. card->ext_csd.boot_size >> 9,
  1095. true,
  1096. "boot1");
  1097. if (ret)
  1098. return ret;
  1099. }
  1100. return ret;
  1101. }
  1102. static int
  1103. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  1104. {
  1105. int err;
  1106. mmc_claim_host(card->host);
  1107. err = mmc_set_blocklen(card, 512);
  1108. mmc_release_host(card->host);
  1109. if (err) {
  1110. printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
  1111. md->disk->disk_name, err);
  1112. return -EINVAL;
  1113. }
  1114. return 0;
  1115. }
  1116. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1117. {
  1118. if (md) {
  1119. if (md->disk->flags & GENHD_FL_UP) {
  1120. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1121. /* Stop new requests from getting into the queue */
  1122. del_gendisk(md->disk);
  1123. }
  1124. /* Then flush out any already in there */
  1125. mmc_cleanup_queue(&md->queue);
  1126. mmc_blk_put(md);
  1127. }
  1128. }
  1129. static void mmc_blk_remove_parts(struct mmc_card *card,
  1130. struct mmc_blk_data *md)
  1131. {
  1132. struct list_head *pos, *q;
  1133. struct mmc_blk_data *part_md;
  1134. __clear_bit(md->name_idx, name_use);
  1135. list_for_each_safe(pos, q, &md->part) {
  1136. part_md = list_entry(pos, struct mmc_blk_data, part);
  1137. list_del(pos);
  1138. mmc_blk_remove_req(part_md);
  1139. }
  1140. }
  1141. static int mmc_add_disk(struct mmc_blk_data *md)
  1142. {
  1143. int ret;
  1144. add_disk(md->disk);
  1145. md->force_ro.show = force_ro_show;
  1146. md->force_ro.store = force_ro_store;
  1147. sysfs_attr_init(&md->force_ro.attr);
  1148. md->force_ro.attr.name = "force_ro";
  1149. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1150. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1151. if (ret)
  1152. del_gendisk(md->disk);
  1153. return ret;
  1154. }
  1155. static const struct mmc_fixup blk_fixups[] =
  1156. {
  1157. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1158. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1159. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1160. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1161. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1162. /*
  1163. * Some MMC cards experience performance degradation with CMD23
  1164. * instead of CMD12-bounded multiblock transfers. For now we'll
  1165. * black list what's bad...
  1166. * - Certain Toshiba cards.
  1167. *
  1168. * N.B. This doesn't affect SD cards.
  1169. */
  1170. MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1171. MMC_QUIRK_BLK_NO_CMD23),
  1172. MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1173. MMC_QUIRK_BLK_NO_CMD23),
  1174. MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1175. MMC_QUIRK_BLK_NO_CMD23),
  1176. END_FIXUP
  1177. };
  1178. static int mmc_blk_probe(struct mmc_card *card)
  1179. {
  1180. struct mmc_blk_data *md, *part_md;
  1181. int err;
  1182. char cap_str[10];
  1183. /*
  1184. * Check that the card supports the command class(es) we need.
  1185. */
  1186. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1187. return -ENODEV;
  1188. md = mmc_blk_alloc(card);
  1189. if (IS_ERR(md))
  1190. return PTR_ERR(md);
  1191. err = mmc_blk_set_blksize(md, card);
  1192. if (err)
  1193. goto out;
  1194. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1195. cap_str, sizeof(cap_str));
  1196. printk(KERN_INFO "%s: %s %s %s %s\n",
  1197. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1198. cap_str, md->read_only ? "(ro)" : "");
  1199. if (mmc_blk_alloc_parts(card, md))
  1200. goto out;
  1201. mmc_set_drvdata(card, md);
  1202. mmc_fixup_device(card, blk_fixups);
  1203. if (mmc_add_disk(md))
  1204. goto out;
  1205. list_for_each_entry(part_md, &md->part, part) {
  1206. if (mmc_add_disk(part_md))
  1207. goto out;
  1208. }
  1209. return 0;
  1210. out:
  1211. mmc_blk_remove_parts(card, md);
  1212. mmc_blk_remove_req(md);
  1213. return err;
  1214. }
  1215. static void mmc_blk_remove(struct mmc_card *card)
  1216. {
  1217. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1218. mmc_blk_remove_parts(card, md);
  1219. mmc_claim_host(card->host);
  1220. mmc_blk_part_switch(card, md);
  1221. mmc_release_host(card->host);
  1222. mmc_blk_remove_req(md);
  1223. mmc_set_drvdata(card, NULL);
  1224. }
  1225. #ifdef CONFIG_PM
  1226. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1227. {
  1228. struct mmc_blk_data *part_md;
  1229. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1230. if (md) {
  1231. mmc_queue_suspend(&md->queue);
  1232. list_for_each_entry(part_md, &md->part, part) {
  1233. mmc_queue_suspend(&part_md->queue);
  1234. }
  1235. }
  1236. return 0;
  1237. }
  1238. static int mmc_blk_resume(struct mmc_card *card)
  1239. {
  1240. struct mmc_blk_data *part_md;
  1241. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1242. if (md) {
  1243. mmc_blk_set_blksize(md, card);
  1244. /*
  1245. * Resume involves the card going into idle state,
  1246. * so current partition is always the main one.
  1247. */
  1248. md->part_curr = md->part_type;
  1249. mmc_queue_resume(&md->queue);
  1250. list_for_each_entry(part_md, &md->part, part) {
  1251. mmc_queue_resume(&part_md->queue);
  1252. }
  1253. }
  1254. return 0;
  1255. }
  1256. #else
  1257. #define mmc_blk_suspend NULL
  1258. #define mmc_blk_resume NULL
  1259. #endif
  1260. static struct mmc_driver mmc_driver = {
  1261. .drv = {
  1262. .name = "mmcblk",
  1263. },
  1264. .probe = mmc_blk_probe,
  1265. .remove = mmc_blk_remove,
  1266. .suspend = mmc_blk_suspend,
  1267. .resume = mmc_blk_resume,
  1268. };
  1269. static int __init mmc_blk_init(void)
  1270. {
  1271. int res;
  1272. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1273. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1274. max_devices = 256 / perdev_minors;
  1275. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1276. if (res)
  1277. goto out;
  1278. res = mmc_register_driver(&mmc_driver);
  1279. if (res)
  1280. goto out2;
  1281. return 0;
  1282. out2:
  1283. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1284. out:
  1285. return res;
  1286. }
  1287. static void __exit mmc_blk_exit(void)
  1288. {
  1289. mmc_unregister_driver(&mmc_driver);
  1290. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1291. }
  1292. module_init(mmc_blk_init);
  1293. module_exit(mmc_blk_exit);
  1294. MODULE_LICENSE("GPL");
  1295. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");