block.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. unsigned int reset_done;
  85. #define MMC_BLK_READ BIT(0)
  86. #define MMC_BLK_WRITE BIT(1)
  87. #define MMC_BLK_DISCARD BIT(2)
  88. #define MMC_BLK_SECDISCARD BIT(3)
  89. /*
  90. * Only set in main mmc_blk_data associated
  91. * with mmc_card with mmc_set_drvdata, and keeps
  92. * track of the current selected device partition.
  93. */
  94. unsigned int part_curr;
  95. struct device_attribute force_ro;
  96. };
  97. static DEFINE_MUTEX(open_lock);
  98. enum mmc_blk_status {
  99. MMC_BLK_SUCCESS = 0,
  100. MMC_BLK_PARTIAL,
  101. MMC_BLK_CMD_ERR,
  102. MMC_BLK_RETRY,
  103. MMC_BLK_ABORT,
  104. MMC_BLK_DATA_ERR,
  105. MMC_BLK_ECC_ERR,
  106. };
  107. module_param(perdev_minors, int, 0444);
  108. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  109. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  110. {
  111. struct mmc_blk_data *md;
  112. mutex_lock(&open_lock);
  113. md = disk->private_data;
  114. if (md && md->usage == 0)
  115. md = NULL;
  116. if (md)
  117. md->usage++;
  118. mutex_unlock(&open_lock);
  119. return md;
  120. }
  121. static inline int mmc_get_devidx(struct gendisk *disk)
  122. {
  123. int devmaj = MAJOR(disk_devt(disk));
  124. int devidx = MINOR(disk_devt(disk)) / perdev_minors;
  125. if (!devmaj)
  126. devidx = disk->first_minor / perdev_minors;
  127. return devidx;
  128. }
  129. static void mmc_blk_put(struct mmc_blk_data *md)
  130. {
  131. mutex_lock(&open_lock);
  132. md->usage--;
  133. if (md->usage == 0) {
  134. int devidx = mmc_get_devidx(md->disk);
  135. blk_cleanup_queue(md->queue.queue);
  136. __clear_bit(devidx, dev_use);
  137. put_disk(md->disk);
  138. kfree(md);
  139. }
  140. mutex_unlock(&open_lock);
  141. }
  142. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  143. char *buf)
  144. {
  145. int ret;
  146. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  147. ret = snprintf(buf, PAGE_SIZE, "%d",
  148. get_disk_ro(dev_to_disk(dev)) ^
  149. md->read_only);
  150. mmc_blk_put(md);
  151. return ret;
  152. }
  153. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  154. const char *buf, size_t count)
  155. {
  156. int ret;
  157. char *end;
  158. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  159. unsigned long set = simple_strtoul(buf, &end, 0);
  160. if (end == buf) {
  161. ret = -EINVAL;
  162. goto out;
  163. }
  164. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  165. ret = count;
  166. out:
  167. mmc_blk_put(md);
  168. return ret;
  169. }
  170. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  171. {
  172. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  173. int ret = -ENXIO;
  174. mutex_lock(&block_mutex);
  175. if (md) {
  176. if (md->usage == 2)
  177. check_disk_change(bdev);
  178. ret = 0;
  179. if ((mode & FMODE_WRITE) && md->read_only) {
  180. mmc_blk_put(md);
  181. ret = -EROFS;
  182. }
  183. }
  184. mutex_unlock(&block_mutex);
  185. return ret;
  186. }
  187. static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
  188. {
  189. struct mmc_blk_data *md = disk->private_data;
  190. mutex_lock(&block_mutex);
  191. mmc_blk_put(md);
  192. mutex_unlock(&block_mutex);
  193. return 0;
  194. }
  195. static int
  196. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  197. {
  198. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  199. geo->heads = 4;
  200. geo->sectors = 16;
  201. return 0;
  202. }
  203. struct mmc_blk_ioc_data {
  204. struct mmc_ioc_cmd ic;
  205. unsigned char *buf;
  206. u64 buf_bytes;
  207. };
  208. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  209. struct mmc_ioc_cmd __user *user)
  210. {
  211. struct mmc_blk_ioc_data *idata;
  212. int err;
  213. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  214. if (!idata) {
  215. err = -ENOMEM;
  216. goto out;
  217. }
  218. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  219. err = -EFAULT;
  220. goto idata_err;
  221. }
  222. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  223. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  224. err = -EOVERFLOW;
  225. goto idata_err;
  226. }
  227. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  228. if (!idata->buf) {
  229. err = -ENOMEM;
  230. goto idata_err;
  231. }
  232. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  233. idata->ic.data_ptr, idata->buf_bytes)) {
  234. err = -EFAULT;
  235. goto copy_err;
  236. }
  237. return idata;
  238. copy_err:
  239. kfree(idata->buf);
  240. idata_err:
  241. kfree(idata);
  242. out:
  243. return ERR_PTR(err);
  244. }
  245. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  246. struct mmc_ioc_cmd __user *ic_ptr)
  247. {
  248. struct mmc_blk_ioc_data *idata;
  249. struct mmc_blk_data *md;
  250. struct mmc_card *card;
  251. struct mmc_command cmd = {0};
  252. struct mmc_data data = {0};
  253. struct mmc_request mrq = {NULL};
  254. struct scatterlist sg;
  255. int err;
  256. /*
  257. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  258. * whole block device, not on a partition. This prevents overspray
  259. * between sibling partitions.
  260. */
  261. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  262. return -EPERM;
  263. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  264. if (IS_ERR(idata))
  265. return PTR_ERR(idata);
  266. cmd.opcode = idata->ic.opcode;
  267. cmd.arg = idata->ic.arg;
  268. cmd.flags = idata->ic.flags;
  269. data.sg = &sg;
  270. data.sg_len = 1;
  271. data.blksz = idata->ic.blksz;
  272. data.blocks = idata->ic.blocks;
  273. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  274. if (idata->ic.write_flag)
  275. data.flags = MMC_DATA_WRITE;
  276. else
  277. data.flags = MMC_DATA_READ;
  278. mrq.cmd = &cmd;
  279. mrq.data = &data;
  280. md = mmc_blk_get(bdev->bd_disk);
  281. if (!md) {
  282. err = -EINVAL;
  283. goto cmd_done;
  284. }
  285. card = md->queue.card;
  286. if (IS_ERR(card)) {
  287. err = PTR_ERR(card);
  288. goto cmd_done;
  289. }
  290. mmc_claim_host(card->host);
  291. if (idata->ic.is_acmd) {
  292. err = mmc_app_cmd(card->host, card);
  293. if (err)
  294. goto cmd_rel_host;
  295. }
  296. /* data.flags must already be set before doing this. */
  297. mmc_set_data_timeout(&data, card);
  298. /* Allow overriding the timeout_ns for empirical tuning. */
  299. if (idata->ic.data_timeout_ns)
  300. data.timeout_ns = idata->ic.data_timeout_ns;
  301. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  302. /*
  303. * Pretend this is a data transfer and rely on the host driver
  304. * to compute timeout. When all host drivers support
  305. * cmd.cmd_timeout for R1B, this can be changed to:
  306. *
  307. * mrq.data = NULL;
  308. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  309. */
  310. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  311. }
  312. mmc_wait_for_req(card->host, &mrq);
  313. if (cmd.error) {
  314. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  315. __func__, cmd.error);
  316. err = cmd.error;
  317. goto cmd_rel_host;
  318. }
  319. if (data.error) {
  320. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  321. __func__, data.error);
  322. err = data.error;
  323. goto cmd_rel_host;
  324. }
  325. /*
  326. * According to the SD specs, some commands require a delay after
  327. * issuing the command.
  328. */
  329. if (idata->ic.postsleep_min_us)
  330. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  331. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  332. err = -EFAULT;
  333. goto cmd_rel_host;
  334. }
  335. if (!idata->ic.write_flag) {
  336. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  337. idata->buf, idata->buf_bytes)) {
  338. err = -EFAULT;
  339. goto cmd_rel_host;
  340. }
  341. }
  342. cmd_rel_host:
  343. mmc_release_host(card->host);
  344. cmd_done:
  345. mmc_blk_put(md);
  346. kfree(idata->buf);
  347. kfree(idata);
  348. return err;
  349. }
  350. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  351. unsigned int cmd, unsigned long arg)
  352. {
  353. int ret = -EINVAL;
  354. if (cmd == MMC_IOC_CMD)
  355. ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
  356. return ret;
  357. }
  358. #ifdef CONFIG_COMPAT
  359. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  360. unsigned int cmd, unsigned long arg)
  361. {
  362. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  363. }
  364. #endif
  365. static const struct block_device_operations mmc_bdops = {
  366. .open = mmc_blk_open,
  367. .release = mmc_blk_release,
  368. .getgeo = mmc_blk_getgeo,
  369. .owner = THIS_MODULE,
  370. .ioctl = mmc_blk_ioctl,
  371. #ifdef CONFIG_COMPAT
  372. .compat_ioctl = mmc_blk_compat_ioctl,
  373. #endif
  374. };
  375. static inline int mmc_blk_part_switch(struct mmc_card *card,
  376. struct mmc_blk_data *md)
  377. {
  378. int ret;
  379. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  380. if (main_md->part_curr == md->part_type)
  381. return 0;
  382. if (mmc_card_mmc(card)) {
  383. u8 part_config = card->ext_csd.part_config;
  384. part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  385. part_config |= md->part_type;
  386. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  387. EXT_CSD_PART_CONFIG, part_config,
  388. card->ext_csd.part_time);
  389. if (ret)
  390. return ret;
  391. card->ext_csd.part_config = part_config;
  392. }
  393. main_md->part_curr = md->part_type;
  394. return 0;
  395. }
  396. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  397. {
  398. int err;
  399. u32 result;
  400. __be32 *blocks;
  401. struct mmc_request mrq = {NULL};
  402. struct mmc_command cmd = {0};
  403. struct mmc_data data = {0};
  404. unsigned int timeout_us;
  405. struct scatterlist sg;
  406. cmd.opcode = MMC_APP_CMD;
  407. cmd.arg = card->rca << 16;
  408. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  409. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  410. if (err)
  411. return (u32)-1;
  412. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  413. return (u32)-1;
  414. memset(&cmd, 0, sizeof(struct mmc_command));
  415. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  416. cmd.arg = 0;
  417. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  418. data.timeout_ns = card->csd.tacc_ns * 100;
  419. data.timeout_clks = card->csd.tacc_clks * 100;
  420. timeout_us = data.timeout_ns / 1000;
  421. timeout_us += data.timeout_clks * 1000 /
  422. (card->host->ios.clock / 1000);
  423. if (timeout_us > 100000) {
  424. data.timeout_ns = 100000000;
  425. data.timeout_clks = 0;
  426. }
  427. data.blksz = 4;
  428. data.blocks = 1;
  429. data.flags = MMC_DATA_READ;
  430. data.sg = &sg;
  431. data.sg_len = 1;
  432. mrq.cmd = &cmd;
  433. mrq.data = &data;
  434. blocks = kmalloc(4, GFP_KERNEL);
  435. if (!blocks)
  436. return (u32)-1;
  437. sg_init_one(&sg, blocks, 4);
  438. mmc_wait_for_req(card->host, &mrq);
  439. result = ntohl(*blocks);
  440. kfree(blocks);
  441. if (cmd.error || data.error)
  442. result = (u32)-1;
  443. return result;
  444. }
  445. static int send_stop(struct mmc_card *card, u32 *status)
  446. {
  447. struct mmc_command cmd = {0};
  448. int err;
  449. cmd.opcode = MMC_STOP_TRANSMISSION;
  450. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  451. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  452. if (err == 0)
  453. *status = cmd.resp[0];
  454. return err;
  455. }
  456. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  457. {
  458. struct mmc_command cmd = {0};
  459. int err;
  460. cmd.opcode = MMC_SEND_STATUS;
  461. if (!mmc_host_is_spi(card->host))
  462. cmd.arg = card->rca << 16;
  463. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  464. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  465. if (err == 0)
  466. *status = cmd.resp[0];
  467. return err;
  468. }
  469. #define ERR_RETRY 2
  470. #define ERR_ABORT 1
  471. #define ERR_CONTINUE 0
  472. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  473. bool status_valid, u32 status)
  474. {
  475. switch (error) {
  476. case -EILSEQ:
  477. /* response crc error, retry the r/w cmd */
  478. pr_err("%s: %s sending %s command, card status %#x\n",
  479. req->rq_disk->disk_name, "response CRC error",
  480. name, status);
  481. return ERR_RETRY;
  482. case -ETIMEDOUT:
  483. pr_err("%s: %s sending %s command, card status %#x\n",
  484. req->rq_disk->disk_name, "timed out", name, status);
  485. /* If the status cmd initially failed, retry the r/w cmd */
  486. if (!status_valid)
  487. return ERR_RETRY;
  488. /*
  489. * If it was a r/w cmd crc error, or illegal command
  490. * (eg, issued in wrong state) then retry - we should
  491. * have corrected the state problem above.
  492. */
  493. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
  494. return ERR_RETRY;
  495. /* Otherwise abort the command */
  496. return ERR_ABORT;
  497. default:
  498. /* We don't understand the error code the driver gave us */
  499. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  500. req->rq_disk->disk_name, error, status);
  501. return ERR_ABORT;
  502. }
  503. }
  504. /*
  505. * Initial r/w and stop cmd error recovery.
  506. * We don't know whether the card received the r/w cmd or not, so try to
  507. * restore things back to a sane state. Essentially, we do this as follows:
  508. * - Obtain card status. If the first attempt to obtain card status fails,
  509. * the status word will reflect the failed status cmd, not the failed
  510. * r/w cmd. If we fail to obtain card status, it suggests we can no
  511. * longer communicate with the card.
  512. * - Check the card state. If the card received the cmd but there was a
  513. * transient problem with the response, it might still be in a data transfer
  514. * mode. Try to send it a stop command. If this fails, we can't recover.
  515. * - If the r/w cmd failed due to a response CRC error, it was probably
  516. * transient, so retry the cmd.
  517. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  518. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  519. * illegal cmd, retry.
  520. * Otherwise we don't understand what happened, so abort.
  521. */
  522. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  523. struct mmc_blk_request *brq, int *ecc_err)
  524. {
  525. bool prev_cmd_status_valid = true;
  526. u32 status, stop_status = 0;
  527. int err, retry;
  528. /*
  529. * Try to get card status which indicates both the card state
  530. * and why there was no response. If the first attempt fails,
  531. * we can't be sure the returned status is for the r/w command.
  532. */
  533. for (retry = 2; retry >= 0; retry--) {
  534. err = get_card_status(card, &status, 0);
  535. if (!err)
  536. break;
  537. prev_cmd_status_valid = false;
  538. pr_err("%s: error %d sending status command, %sing\n",
  539. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  540. }
  541. /* We couldn't get a response from the card. Give up. */
  542. if (err)
  543. return ERR_ABORT;
  544. /* Flag ECC errors */
  545. if ((status & R1_CARD_ECC_FAILED) ||
  546. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  547. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  548. *ecc_err = 1;
  549. /*
  550. * Check the current card state. If it is in some data transfer
  551. * mode, tell it to stop (and hopefully transition back to TRAN.)
  552. */
  553. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  554. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  555. err = send_stop(card, &stop_status);
  556. if (err)
  557. pr_err("%s: error %d sending stop command\n",
  558. req->rq_disk->disk_name, err);
  559. /*
  560. * If the stop cmd also timed out, the card is probably
  561. * not present, so abort. Other errors are bad news too.
  562. */
  563. if (err)
  564. return ERR_ABORT;
  565. if (stop_status & R1_CARD_ECC_FAILED)
  566. *ecc_err = 1;
  567. }
  568. /* Check for set block count errors */
  569. if (brq->sbc.error)
  570. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  571. prev_cmd_status_valid, status);
  572. /* Check for r/w command errors */
  573. if (brq->cmd.error)
  574. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  575. prev_cmd_status_valid, status);
  576. /* Data errors */
  577. if (!brq->stop.error)
  578. return ERR_CONTINUE;
  579. /* Now for stop errors. These aren't fatal to the transfer. */
  580. pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  581. req->rq_disk->disk_name, brq->stop.error,
  582. brq->cmd.resp[0], status);
  583. /*
  584. * Subsitute in our own stop status as this will give the error
  585. * state which happened during the execution of the r/w command.
  586. */
  587. if (stop_status) {
  588. brq->stop.resp[0] = stop_status;
  589. brq->stop.error = 0;
  590. }
  591. return ERR_CONTINUE;
  592. }
  593. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  594. int type)
  595. {
  596. int err;
  597. if (md->reset_done & type)
  598. return -EEXIST;
  599. md->reset_done |= type;
  600. err = mmc_hw_reset(host);
  601. /* Ensure we switch back to the correct partition */
  602. if (err != -EOPNOTSUPP) {
  603. struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
  604. int part_err;
  605. main_md->part_curr = main_md->part_type;
  606. part_err = mmc_blk_part_switch(host->card, md);
  607. if (part_err) {
  608. /*
  609. * We have failed to get back into the correct
  610. * partition, so we need to abort the whole request.
  611. */
  612. return -ENODEV;
  613. }
  614. }
  615. return err;
  616. }
  617. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  618. {
  619. md->reset_done &= ~type;
  620. }
  621. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  622. {
  623. struct mmc_blk_data *md = mq->data;
  624. struct mmc_card *card = md->queue.card;
  625. unsigned int from, nr, arg;
  626. int err = 0, type = MMC_BLK_DISCARD;
  627. if (!mmc_can_erase(card)) {
  628. err = -EOPNOTSUPP;
  629. goto out;
  630. }
  631. from = blk_rq_pos(req);
  632. nr = blk_rq_sectors(req);
  633. if (mmc_can_trim(card))
  634. arg = MMC_TRIM_ARG;
  635. else
  636. arg = MMC_ERASE_ARG;
  637. retry:
  638. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  639. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  640. INAND_CMD38_ARG_EXT_CSD,
  641. arg == MMC_TRIM_ARG ?
  642. INAND_CMD38_ARG_TRIM :
  643. INAND_CMD38_ARG_ERASE,
  644. 0);
  645. if (err)
  646. goto out;
  647. }
  648. err = mmc_erase(card, from, nr, arg);
  649. out:
  650. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  651. goto retry;
  652. if (!err)
  653. mmc_blk_reset_success(md, type);
  654. spin_lock_irq(&md->lock);
  655. __blk_end_request(req, err, blk_rq_bytes(req));
  656. spin_unlock_irq(&md->lock);
  657. return err ? 0 : 1;
  658. }
  659. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  660. struct request *req)
  661. {
  662. struct mmc_blk_data *md = mq->data;
  663. struct mmc_card *card = md->queue.card;
  664. unsigned int from, nr, arg;
  665. int err = 0, type = MMC_BLK_SECDISCARD;
  666. if (!mmc_can_secure_erase_trim(card)) {
  667. err = -EOPNOTSUPP;
  668. goto out;
  669. }
  670. from = blk_rq_pos(req);
  671. nr = blk_rq_sectors(req);
  672. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  673. arg = MMC_SECURE_TRIM1_ARG;
  674. else
  675. arg = MMC_SECURE_ERASE_ARG;
  676. retry:
  677. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  678. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  679. INAND_CMD38_ARG_EXT_CSD,
  680. arg == MMC_SECURE_TRIM1_ARG ?
  681. INAND_CMD38_ARG_SECTRIM1 :
  682. INAND_CMD38_ARG_SECERASE,
  683. 0);
  684. if (err)
  685. goto out;
  686. }
  687. err = mmc_erase(card, from, nr, arg);
  688. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  689. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  690. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  691. INAND_CMD38_ARG_EXT_CSD,
  692. INAND_CMD38_ARG_SECTRIM2,
  693. 0);
  694. if (err)
  695. goto out;
  696. }
  697. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  698. }
  699. out:
  700. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  701. goto retry;
  702. if (!err)
  703. mmc_blk_reset_success(md, type);
  704. spin_lock_irq(&md->lock);
  705. __blk_end_request(req, err, blk_rq_bytes(req));
  706. spin_unlock_irq(&md->lock);
  707. return err ? 0 : 1;
  708. }
  709. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  710. {
  711. struct mmc_blk_data *md = mq->data;
  712. /*
  713. * No-op, only service this because we need REQ_FUA for reliable
  714. * writes.
  715. */
  716. spin_lock_irq(&md->lock);
  717. __blk_end_request_all(req, 0);
  718. spin_unlock_irq(&md->lock);
  719. return 1;
  720. }
  721. /*
  722. * Reformat current write as a reliable write, supporting
  723. * both legacy and the enhanced reliable write MMC cards.
  724. * In each transfer we'll handle only as much as a single
  725. * reliable write can handle, thus finish the request in
  726. * partial completions.
  727. */
  728. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  729. struct mmc_card *card,
  730. struct request *req)
  731. {
  732. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  733. /* Legacy mode imposes restrictions on transfers. */
  734. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  735. brq->data.blocks = 1;
  736. if (brq->data.blocks > card->ext_csd.rel_sectors)
  737. brq->data.blocks = card->ext_csd.rel_sectors;
  738. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  739. brq->data.blocks = 1;
  740. }
  741. }
  742. #define CMD_ERRORS \
  743. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  744. R1_ADDRESS_ERROR | /* Misaligned address */ \
  745. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  746. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  747. R1_CC_ERROR | /* Card controller error */ \
  748. R1_ERROR) /* General/unknown error */
  749. static int mmc_blk_err_check(struct mmc_card *card,
  750. struct mmc_async_req *areq)
  751. {
  752. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  753. mmc_active);
  754. struct mmc_blk_request *brq = &mq_mrq->brq;
  755. struct request *req = mq_mrq->req;
  756. int ecc_err = 0;
  757. /*
  758. * sbc.error indicates a problem with the set block count
  759. * command. No data will have been transferred.
  760. *
  761. * cmd.error indicates a problem with the r/w command. No
  762. * data will have been transferred.
  763. *
  764. * stop.error indicates a problem with the stop command. Data
  765. * may have been transferred, or may still be transferring.
  766. */
  767. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  768. brq->data.error) {
  769. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
  770. case ERR_RETRY:
  771. return MMC_BLK_RETRY;
  772. case ERR_ABORT:
  773. return MMC_BLK_ABORT;
  774. case ERR_CONTINUE:
  775. break;
  776. }
  777. }
  778. /*
  779. * Check for errors relating to the execution of the
  780. * initial command - such as address errors. No data
  781. * has been transferred.
  782. */
  783. if (brq->cmd.resp[0] & CMD_ERRORS) {
  784. pr_err("%s: r/w command failed, status = %#x\n",
  785. req->rq_disk->disk_name, brq->cmd.resp[0]);
  786. return MMC_BLK_ABORT;
  787. }
  788. /*
  789. * Everything else is either success, or a data error of some
  790. * kind. If it was a write, we may have transitioned to
  791. * program mode, which we have to wait for it to complete.
  792. */
  793. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  794. u32 status;
  795. do {
  796. int err = get_card_status(card, &status, 5);
  797. if (err) {
  798. printk(KERN_ERR "%s: error %d requesting status\n",
  799. req->rq_disk->disk_name, err);
  800. return MMC_BLK_CMD_ERR;
  801. }
  802. /*
  803. * Some cards mishandle the status bits,
  804. * so make sure to check both the busy
  805. * indication and the card state.
  806. */
  807. } while (!(status & R1_READY_FOR_DATA) ||
  808. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  809. }
  810. if (brq->data.error) {
  811. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  812. req->rq_disk->disk_name, brq->data.error,
  813. (unsigned)blk_rq_pos(req),
  814. (unsigned)blk_rq_sectors(req),
  815. brq->cmd.resp[0], brq->stop.resp[0]);
  816. if (rq_data_dir(req) == READ) {
  817. if (ecc_err)
  818. return MMC_BLK_ECC_ERR;
  819. return MMC_BLK_DATA_ERR;
  820. } else {
  821. return MMC_BLK_CMD_ERR;
  822. }
  823. }
  824. if (!brq->data.bytes_xfered)
  825. return MMC_BLK_RETRY;
  826. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  827. return MMC_BLK_PARTIAL;
  828. return MMC_BLK_SUCCESS;
  829. }
  830. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  831. struct mmc_card *card,
  832. int disable_multi,
  833. struct mmc_queue *mq)
  834. {
  835. u32 readcmd, writecmd;
  836. struct mmc_blk_request *brq = &mqrq->brq;
  837. struct request *req = mqrq->req;
  838. struct mmc_blk_data *md = mq->data;
  839. /*
  840. * Reliable writes are used to implement Forced Unit Access and
  841. * REQ_META accesses, and are supported only on MMCs.
  842. *
  843. * XXX: this really needs a good explanation of why REQ_META
  844. * is treated special.
  845. */
  846. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  847. (req->cmd_flags & REQ_META)) &&
  848. (rq_data_dir(req) == WRITE) &&
  849. (md->flags & MMC_BLK_REL_WR);
  850. memset(brq, 0, sizeof(struct mmc_blk_request));
  851. brq->mrq.cmd = &brq->cmd;
  852. brq->mrq.data = &brq->data;
  853. brq->cmd.arg = blk_rq_pos(req);
  854. if (!mmc_card_blockaddr(card))
  855. brq->cmd.arg <<= 9;
  856. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  857. brq->data.blksz = 512;
  858. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  859. brq->stop.arg = 0;
  860. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  861. brq->data.blocks = blk_rq_sectors(req);
  862. /*
  863. * The block layer doesn't support all sector count
  864. * restrictions, so we need to be prepared for too big
  865. * requests.
  866. */
  867. if (brq->data.blocks > card->host->max_blk_count)
  868. brq->data.blocks = card->host->max_blk_count;
  869. /*
  870. * After a read error, we redo the request one sector at a time
  871. * in order to accurately determine which sectors can be read
  872. * successfully.
  873. */
  874. if (disable_multi && brq->data.blocks > 1)
  875. brq->data.blocks = 1;
  876. if (brq->data.blocks > 1 || do_rel_wr) {
  877. /* SPI multiblock writes terminate using a special
  878. * token, not a STOP_TRANSMISSION request.
  879. */
  880. if (!mmc_host_is_spi(card->host) ||
  881. rq_data_dir(req) == READ)
  882. brq->mrq.stop = &brq->stop;
  883. readcmd = MMC_READ_MULTIPLE_BLOCK;
  884. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  885. } else {
  886. brq->mrq.stop = NULL;
  887. readcmd = MMC_READ_SINGLE_BLOCK;
  888. writecmd = MMC_WRITE_BLOCK;
  889. }
  890. if (rq_data_dir(req) == READ) {
  891. brq->cmd.opcode = readcmd;
  892. brq->data.flags |= MMC_DATA_READ;
  893. } else {
  894. brq->cmd.opcode = writecmd;
  895. brq->data.flags |= MMC_DATA_WRITE;
  896. }
  897. if (do_rel_wr)
  898. mmc_apply_rel_rw(brq, card, req);
  899. /*
  900. * Pre-defined multi-block transfers are preferable to
  901. * open ended-ones (and necessary for reliable writes).
  902. * However, it is not sufficient to just send CMD23,
  903. * and avoid the final CMD12, as on an error condition
  904. * CMD12 (stop) needs to be sent anyway. This, coupled
  905. * with Auto-CMD23 enhancements provided by some
  906. * hosts, means that the complexity of dealing
  907. * with this is best left to the host. If CMD23 is
  908. * supported by card and host, we'll fill sbc in and let
  909. * the host deal with handling it correctly. This means
  910. * that for hosts that don't expose MMC_CAP_CMD23, no
  911. * change of behavior will be observed.
  912. *
  913. * N.B: Some MMC cards experience perf degradation.
  914. * We'll avoid using CMD23-bounded multiblock writes for
  915. * these, while retaining features like reliable writes.
  916. */
  917. if ((md->flags & MMC_BLK_CMD23) &&
  918. mmc_op_multi(brq->cmd.opcode) &&
  919. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
  920. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  921. brq->sbc.arg = brq->data.blocks |
  922. (do_rel_wr ? (1 << 31) : 0);
  923. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  924. brq->mrq.sbc = &brq->sbc;
  925. }
  926. mmc_set_data_timeout(&brq->data, card);
  927. brq->data.sg = mqrq->sg;
  928. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  929. /*
  930. * Adjust the sg list so it is the same size as the
  931. * request.
  932. */
  933. if (brq->data.blocks != blk_rq_sectors(req)) {
  934. int i, data_size = brq->data.blocks << 9;
  935. struct scatterlist *sg;
  936. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  937. data_size -= sg->length;
  938. if (data_size <= 0) {
  939. sg->length += data_size;
  940. i++;
  941. break;
  942. }
  943. }
  944. brq->data.sg_len = i;
  945. }
  946. mqrq->mmc_active.mrq = &brq->mrq;
  947. mqrq->mmc_active.err_check = mmc_blk_err_check;
  948. mmc_queue_bounce_pre(mqrq);
  949. }
  950. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  951. struct mmc_blk_request *brq, struct request *req,
  952. int ret)
  953. {
  954. /*
  955. * If this is an SD card and we're writing, we can first
  956. * mark the known good sectors as ok.
  957. *
  958. * If the card is not SD, we can still ok written sectors
  959. * as reported by the controller (which might be less than
  960. * the real number of written sectors, but never more).
  961. */
  962. if (mmc_card_sd(card)) {
  963. u32 blocks;
  964. blocks = mmc_sd_num_wr_blocks(card);
  965. if (blocks != (u32)-1) {
  966. spin_lock_irq(&md->lock);
  967. ret = __blk_end_request(req, 0, blocks << 9);
  968. spin_unlock_irq(&md->lock);
  969. }
  970. } else {
  971. spin_lock_irq(&md->lock);
  972. ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
  973. spin_unlock_irq(&md->lock);
  974. }
  975. return ret;
  976. }
  977. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  978. {
  979. struct mmc_blk_data *md = mq->data;
  980. struct mmc_card *card = md->queue.card;
  981. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  982. int ret = 1, disable_multi = 0, retry = 0, type;
  983. enum mmc_blk_status status;
  984. struct mmc_queue_req *mq_rq;
  985. struct request *req;
  986. struct mmc_async_req *areq;
  987. if (!rqc && !mq->mqrq_prev->req)
  988. return 0;
  989. do {
  990. if (rqc) {
  991. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  992. areq = &mq->mqrq_cur->mmc_active;
  993. } else
  994. areq = NULL;
  995. areq = mmc_start_req(card->host, areq, (int *) &status);
  996. if (!areq)
  997. return 0;
  998. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  999. brq = &mq_rq->brq;
  1000. req = mq_rq->req;
  1001. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1002. mmc_queue_bounce_post(mq_rq);
  1003. switch (status) {
  1004. case MMC_BLK_SUCCESS:
  1005. case MMC_BLK_PARTIAL:
  1006. /*
  1007. * A block was successfully transferred.
  1008. */
  1009. mmc_blk_reset_success(md, type);
  1010. spin_lock_irq(&md->lock);
  1011. ret = __blk_end_request(req, 0,
  1012. brq->data.bytes_xfered);
  1013. spin_unlock_irq(&md->lock);
  1014. /*
  1015. * If the blk_end_request function returns non-zero even
  1016. * though all data has been transferred and no errors
  1017. * were returned by the host controller, it's a bug.
  1018. */
  1019. if (status == MMC_BLK_SUCCESS && ret) {
  1020. printk(KERN_ERR "%s BUG rq_tot %d d_xfer %d\n",
  1021. __func__, blk_rq_bytes(req),
  1022. brq->data.bytes_xfered);
  1023. rqc = NULL;
  1024. goto cmd_abort;
  1025. }
  1026. break;
  1027. case MMC_BLK_CMD_ERR:
  1028. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1029. if (!mmc_blk_reset(md, card->host, type))
  1030. break;
  1031. goto cmd_abort;
  1032. case MMC_BLK_RETRY:
  1033. if (retry++ < 5)
  1034. break;
  1035. /* Fall through */
  1036. case MMC_BLK_ABORT:
  1037. if (!mmc_blk_reset(md, card->host, type))
  1038. break;
  1039. goto cmd_abort;
  1040. case MMC_BLK_DATA_ERR: {
  1041. int err;
  1042. err = mmc_blk_reset(md, card->host, type);
  1043. if (!err)
  1044. break;
  1045. if (err == -ENODEV)
  1046. goto cmd_abort;
  1047. /* Fall through */
  1048. }
  1049. case MMC_BLK_ECC_ERR:
  1050. if (brq->data.blocks > 1) {
  1051. /* Redo read one sector at a time */
  1052. pr_warning("%s: retrying using single block read\n",
  1053. req->rq_disk->disk_name);
  1054. disable_multi = 1;
  1055. break;
  1056. }
  1057. /*
  1058. * After an error, we redo I/O one sector at a
  1059. * time, so we only reach here after trying to
  1060. * read a single sector.
  1061. */
  1062. spin_lock_irq(&md->lock);
  1063. ret = __blk_end_request(req, -EIO,
  1064. brq->data.blksz);
  1065. spin_unlock_irq(&md->lock);
  1066. if (!ret)
  1067. goto start_new_req;
  1068. break;
  1069. }
  1070. if (ret) {
  1071. /*
  1072. * In case of a incomplete request
  1073. * prepare it again and resend.
  1074. */
  1075. mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
  1076. mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
  1077. }
  1078. } while (ret);
  1079. return 1;
  1080. cmd_abort:
  1081. spin_lock_irq(&md->lock);
  1082. while (ret)
  1083. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  1084. spin_unlock_irq(&md->lock);
  1085. start_new_req:
  1086. if (rqc) {
  1087. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1088. mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
  1089. }
  1090. return 0;
  1091. }
  1092. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1093. {
  1094. int ret;
  1095. struct mmc_blk_data *md = mq->data;
  1096. struct mmc_card *card = md->queue.card;
  1097. if (req && !mq->mqrq_prev->req)
  1098. /* claim host only for the first request */
  1099. mmc_claim_host(card->host);
  1100. ret = mmc_blk_part_switch(card, md);
  1101. if (ret) {
  1102. if (req) {
  1103. spin_lock_irq(&md->lock);
  1104. __blk_end_request_all(req, -EIO);
  1105. spin_unlock_irq(&md->lock);
  1106. }
  1107. ret = 0;
  1108. goto out;
  1109. }
  1110. if (req && req->cmd_flags & REQ_DISCARD) {
  1111. /* complete ongoing async transfer before issuing discard */
  1112. if (card->host->areq)
  1113. mmc_blk_issue_rw_rq(mq, NULL);
  1114. if (req->cmd_flags & REQ_SECURE)
  1115. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1116. else
  1117. ret = mmc_blk_issue_discard_rq(mq, req);
  1118. } else if (req && req->cmd_flags & REQ_FLUSH) {
  1119. /* complete ongoing async transfer before issuing flush */
  1120. if (card->host->areq)
  1121. mmc_blk_issue_rw_rq(mq, NULL);
  1122. ret = mmc_blk_issue_flush(mq, req);
  1123. } else {
  1124. ret = mmc_blk_issue_rw_rq(mq, req);
  1125. }
  1126. out:
  1127. if (!req)
  1128. /* release host only when there are no more requests */
  1129. mmc_release_host(card->host);
  1130. return ret;
  1131. }
  1132. static inline int mmc_blk_readonly(struct mmc_card *card)
  1133. {
  1134. return mmc_card_readonly(card) ||
  1135. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1136. }
  1137. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1138. struct device *parent,
  1139. sector_t size,
  1140. bool default_ro,
  1141. const char *subname)
  1142. {
  1143. struct mmc_blk_data *md;
  1144. int devidx, ret;
  1145. devidx = find_first_zero_bit(dev_use, max_devices);
  1146. if (devidx >= max_devices)
  1147. return ERR_PTR(-ENOSPC);
  1148. __set_bit(devidx, dev_use);
  1149. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1150. if (!md) {
  1151. ret = -ENOMEM;
  1152. goto out;
  1153. }
  1154. /*
  1155. * !subname implies we are creating main mmc_blk_data that will be
  1156. * associated with mmc_card with mmc_set_drvdata. Due to device
  1157. * partitions, devidx will not coincide with a per-physical card
  1158. * index anymore so we keep track of a name index.
  1159. */
  1160. if (!subname) {
  1161. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1162. __set_bit(md->name_idx, name_use);
  1163. }
  1164. else
  1165. md->name_idx = ((struct mmc_blk_data *)
  1166. dev_to_disk(parent)->private_data)->name_idx;
  1167. /*
  1168. * Set the read-only status based on the supported commands
  1169. * and the write protect switch.
  1170. */
  1171. md->read_only = mmc_blk_readonly(card);
  1172. md->disk = alloc_disk(perdev_minors);
  1173. if (md->disk == NULL) {
  1174. ret = -ENOMEM;
  1175. goto err_kfree;
  1176. }
  1177. spin_lock_init(&md->lock);
  1178. INIT_LIST_HEAD(&md->part);
  1179. md->usage = 1;
  1180. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1181. if (ret)
  1182. goto err_putdisk;
  1183. md->queue.issue_fn = mmc_blk_issue_rq;
  1184. md->queue.data = md;
  1185. md->disk->major = MMC_BLOCK_MAJOR;
  1186. md->disk->first_minor = devidx * perdev_minors;
  1187. md->disk->fops = &mmc_bdops;
  1188. md->disk->private_data = md;
  1189. md->disk->queue = md->queue.queue;
  1190. md->disk->driverfs_dev = parent;
  1191. set_disk_ro(md->disk, md->read_only || default_ro);
  1192. /*
  1193. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1194. *
  1195. * - be set for removable media with permanent block devices
  1196. * - be unset for removable block devices with permanent media
  1197. *
  1198. * Since MMC block devices clearly fall under the second
  1199. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1200. * should use the block device creation/destruction hotplug
  1201. * messages to tell when the card is present.
  1202. */
  1203. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1204. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1205. blk_queue_logical_block_size(md->queue.queue, 512);
  1206. set_capacity(md->disk, size);
  1207. if (mmc_host_cmd23(card->host)) {
  1208. if (mmc_card_mmc(card) ||
  1209. (mmc_card_sd(card) &&
  1210. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1211. md->flags |= MMC_BLK_CMD23;
  1212. }
  1213. if (mmc_card_mmc(card) &&
  1214. md->flags & MMC_BLK_CMD23 &&
  1215. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1216. card->ext_csd.rel_sectors)) {
  1217. md->flags |= MMC_BLK_REL_WR;
  1218. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1219. }
  1220. return md;
  1221. err_putdisk:
  1222. put_disk(md->disk);
  1223. err_kfree:
  1224. kfree(md);
  1225. out:
  1226. return ERR_PTR(ret);
  1227. }
  1228. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1229. {
  1230. sector_t size;
  1231. struct mmc_blk_data *md;
  1232. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1233. /*
  1234. * The EXT_CSD sector count is in number or 512 byte
  1235. * sectors.
  1236. */
  1237. size = card->ext_csd.sectors;
  1238. } else {
  1239. /*
  1240. * The CSD capacity field is in units of read_blkbits.
  1241. * set_capacity takes units of 512 bytes.
  1242. */
  1243. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1244. }
  1245. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  1246. return md;
  1247. }
  1248. static int mmc_blk_alloc_part(struct mmc_card *card,
  1249. struct mmc_blk_data *md,
  1250. unsigned int part_type,
  1251. sector_t size,
  1252. bool default_ro,
  1253. const char *subname)
  1254. {
  1255. char cap_str[10];
  1256. struct mmc_blk_data *part_md;
  1257. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1258. subname);
  1259. if (IS_ERR(part_md))
  1260. return PTR_ERR(part_md);
  1261. part_md->part_type = part_type;
  1262. list_add(&part_md->part, &md->part);
  1263. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1264. cap_str, sizeof(cap_str));
  1265. printk(KERN_INFO "%s: %s %s partition %u %s\n",
  1266. part_md->disk->disk_name, mmc_card_id(card),
  1267. mmc_card_name(card), part_md->part_type, cap_str);
  1268. return 0;
  1269. }
  1270. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1271. {
  1272. int ret = 0;
  1273. if (!mmc_card_mmc(card))
  1274. return 0;
  1275. if (card->ext_csd.boot_size) {
  1276. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
  1277. card->ext_csd.boot_size >> 9,
  1278. true,
  1279. "boot0");
  1280. if (ret)
  1281. return ret;
  1282. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
  1283. card->ext_csd.boot_size >> 9,
  1284. true,
  1285. "boot1");
  1286. if (ret)
  1287. return ret;
  1288. }
  1289. return ret;
  1290. }
  1291. static int
  1292. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  1293. {
  1294. int err;
  1295. mmc_claim_host(card->host);
  1296. err = mmc_set_blocklen(card, 512);
  1297. mmc_release_host(card->host);
  1298. if (err) {
  1299. printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
  1300. md->disk->disk_name, err);
  1301. return -EINVAL;
  1302. }
  1303. return 0;
  1304. }
  1305. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1306. {
  1307. if (md) {
  1308. if (md->disk->flags & GENHD_FL_UP) {
  1309. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1310. /* Stop new requests from getting into the queue */
  1311. del_gendisk(md->disk);
  1312. }
  1313. /* Then flush out any already in there */
  1314. mmc_cleanup_queue(&md->queue);
  1315. mmc_blk_put(md);
  1316. }
  1317. }
  1318. static void mmc_blk_remove_parts(struct mmc_card *card,
  1319. struct mmc_blk_data *md)
  1320. {
  1321. struct list_head *pos, *q;
  1322. struct mmc_blk_data *part_md;
  1323. __clear_bit(md->name_idx, name_use);
  1324. list_for_each_safe(pos, q, &md->part) {
  1325. part_md = list_entry(pos, struct mmc_blk_data, part);
  1326. list_del(pos);
  1327. mmc_blk_remove_req(part_md);
  1328. }
  1329. }
  1330. static int mmc_add_disk(struct mmc_blk_data *md)
  1331. {
  1332. int ret;
  1333. add_disk(md->disk);
  1334. md->force_ro.show = force_ro_show;
  1335. md->force_ro.store = force_ro_store;
  1336. sysfs_attr_init(&md->force_ro.attr);
  1337. md->force_ro.attr.name = "force_ro";
  1338. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1339. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1340. if (ret)
  1341. del_gendisk(md->disk);
  1342. return ret;
  1343. }
  1344. static const struct mmc_fixup blk_fixups[] =
  1345. {
  1346. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1347. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1348. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1349. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1350. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1351. /*
  1352. * Some MMC cards experience performance degradation with CMD23
  1353. * instead of CMD12-bounded multiblock transfers. For now we'll
  1354. * black list what's bad...
  1355. * - Certain Toshiba cards.
  1356. *
  1357. * N.B. This doesn't affect SD cards.
  1358. */
  1359. MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1360. MMC_QUIRK_BLK_NO_CMD23),
  1361. MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1362. MMC_QUIRK_BLK_NO_CMD23),
  1363. MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1364. MMC_QUIRK_BLK_NO_CMD23),
  1365. END_FIXUP
  1366. };
  1367. static int mmc_blk_probe(struct mmc_card *card)
  1368. {
  1369. struct mmc_blk_data *md, *part_md;
  1370. int err;
  1371. char cap_str[10];
  1372. /*
  1373. * Check that the card supports the command class(es) we need.
  1374. */
  1375. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1376. return -ENODEV;
  1377. md = mmc_blk_alloc(card);
  1378. if (IS_ERR(md))
  1379. return PTR_ERR(md);
  1380. err = mmc_blk_set_blksize(md, card);
  1381. if (err)
  1382. goto out;
  1383. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1384. cap_str, sizeof(cap_str));
  1385. printk(KERN_INFO "%s: %s %s %s %s\n",
  1386. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1387. cap_str, md->read_only ? "(ro)" : "");
  1388. if (mmc_blk_alloc_parts(card, md))
  1389. goto out;
  1390. mmc_set_drvdata(card, md);
  1391. mmc_fixup_device(card, blk_fixups);
  1392. if (mmc_add_disk(md))
  1393. goto out;
  1394. list_for_each_entry(part_md, &md->part, part) {
  1395. if (mmc_add_disk(part_md))
  1396. goto out;
  1397. }
  1398. return 0;
  1399. out:
  1400. mmc_blk_remove_parts(card, md);
  1401. mmc_blk_remove_req(md);
  1402. return err;
  1403. }
  1404. static void mmc_blk_remove(struct mmc_card *card)
  1405. {
  1406. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1407. mmc_blk_remove_parts(card, md);
  1408. mmc_claim_host(card->host);
  1409. mmc_blk_part_switch(card, md);
  1410. mmc_release_host(card->host);
  1411. mmc_blk_remove_req(md);
  1412. mmc_set_drvdata(card, NULL);
  1413. }
  1414. #ifdef CONFIG_PM
  1415. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1416. {
  1417. struct mmc_blk_data *part_md;
  1418. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1419. if (md) {
  1420. mmc_queue_suspend(&md->queue);
  1421. list_for_each_entry(part_md, &md->part, part) {
  1422. mmc_queue_suspend(&part_md->queue);
  1423. }
  1424. }
  1425. return 0;
  1426. }
  1427. static int mmc_blk_resume(struct mmc_card *card)
  1428. {
  1429. struct mmc_blk_data *part_md;
  1430. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1431. if (md) {
  1432. mmc_blk_set_blksize(md, card);
  1433. /*
  1434. * Resume involves the card going into idle state,
  1435. * so current partition is always the main one.
  1436. */
  1437. md->part_curr = md->part_type;
  1438. mmc_queue_resume(&md->queue);
  1439. list_for_each_entry(part_md, &md->part, part) {
  1440. mmc_queue_resume(&part_md->queue);
  1441. }
  1442. }
  1443. return 0;
  1444. }
  1445. #else
  1446. #define mmc_blk_suspend NULL
  1447. #define mmc_blk_resume NULL
  1448. #endif
  1449. static struct mmc_driver mmc_driver = {
  1450. .drv = {
  1451. .name = "mmcblk",
  1452. },
  1453. .probe = mmc_blk_probe,
  1454. .remove = mmc_blk_remove,
  1455. .suspend = mmc_blk_suspend,
  1456. .resume = mmc_blk_resume,
  1457. };
  1458. static int __init mmc_blk_init(void)
  1459. {
  1460. int res;
  1461. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1462. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1463. max_devices = 256 / perdev_minors;
  1464. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1465. if (res)
  1466. goto out;
  1467. res = mmc_register_driver(&mmc_driver);
  1468. if (res)
  1469. goto out2;
  1470. return 0;
  1471. out2:
  1472. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1473. out:
  1474. return res;
  1475. }
  1476. static void __exit mmc_blk_exit(void)
  1477. {
  1478. mmc_unregister_driver(&mmc_driver);
  1479. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1480. }
  1481. module_init(mmc_blk_init);
  1482. module_exit(mmc_blk_exit);
  1483. MODULE_LICENSE("GPL");
  1484. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");