block.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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_discard(card))
  634. arg = MMC_DISCARD_ARG;
  635. else if (mmc_can_trim(card))
  636. arg = MMC_TRIM_ARG;
  637. else
  638. arg = MMC_ERASE_ARG;
  639. retry:
  640. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  641. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  642. INAND_CMD38_ARG_EXT_CSD,
  643. arg == MMC_TRIM_ARG ?
  644. INAND_CMD38_ARG_TRIM :
  645. INAND_CMD38_ARG_ERASE,
  646. 0);
  647. if (err)
  648. goto out;
  649. }
  650. err = mmc_erase(card, from, nr, arg);
  651. out:
  652. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  653. goto retry;
  654. if (!err)
  655. mmc_blk_reset_success(md, type);
  656. spin_lock_irq(&md->lock);
  657. __blk_end_request(req, err, blk_rq_bytes(req));
  658. spin_unlock_irq(&md->lock);
  659. return err ? 0 : 1;
  660. }
  661. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  662. struct request *req)
  663. {
  664. struct mmc_blk_data *md = mq->data;
  665. struct mmc_card *card = md->queue.card;
  666. unsigned int from, nr, arg;
  667. int err = 0, type = MMC_BLK_SECDISCARD;
  668. if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
  669. err = -EOPNOTSUPP;
  670. goto out;
  671. }
  672. /* The sanitize operation is supported at v4.5 only */
  673. if (mmc_can_sanitize(card)) {
  674. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  675. EXT_CSD_SANITIZE_START, 1, 0);
  676. goto out;
  677. }
  678. from = blk_rq_pos(req);
  679. nr = blk_rq_sectors(req);
  680. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  681. arg = MMC_SECURE_TRIM1_ARG;
  682. else
  683. arg = MMC_SECURE_ERASE_ARG;
  684. retry:
  685. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  686. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  687. INAND_CMD38_ARG_EXT_CSD,
  688. arg == MMC_SECURE_TRIM1_ARG ?
  689. INAND_CMD38_ARG_SECTRIM1 :
  690. INAND_CMD38_ARG_SECERASE,
  691. 0);
  692. if (err)
  693. goto out;
  694. }
  695. err = mmc_erase(card, from, nr, arg);
  696. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  697. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  698. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  699. INAND_CMD38_ARG_EXT_CSD,
  700. INAND_CMD38_ARG_SECTRIM2,
  701. 0);
  702. if (err)
  703. goto out;
  704. }
  705. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  706. }
  707. out:
  708. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  709. goto retry;
  710. if (!err)
  711. mmc_blk_reset_success(md, type);
  712. spin_lock_irq(&md->lock);
  713. __blk_end_request(req, err, blk_rq_bytes(req));
  714. spin_unlock_irq(&md->lock);
  715. return err ? 0 : 1;
  716. }
  717. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  718. {
  719. struct mmc_blk_data *md = mq->data;
  720. struct mmc_card *card = md->queue.card;
  721. int ret = 0;
  722. ret = mmc_flush_cache(card);
  723. if (ret)
  724. ret = -EIO;
  725. spin_lock_irq(&md->lock);
  726. __blk_end_request_all(req, ret);
  727. spin_unlock_irq(&md->lock);
  728. return ret ? 0 : 1;
  729. }
  730. /*
  731. * Reformat current write as a reliable write, supporting
  732. * both legacy and the enhanced reliable write MMC cards.
  733. * In each transfer we'll handle only as much as a single
  734. * reliable write can handle, thus finish the request in
  735. * partial completions.
  736. */
  737. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  738. struct mmc_card *card,
  739. struct request *req)
  740. {
  741. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  742. /* Legacy mode imposes restrictions on transfers. */
  743. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  744. brq->data.blocks = 1;
  745. if (brq->data.blocks > card->ext_csd.rel_sectors)
  746. brq->data.blocks = card->ext_csd.rel_sectors;
  747. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  748. brq->data.blocks = 1;
  749. }
  750. }
  751. #define CMD_ERRORS \
  752. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  753. R1_ADDRESS_ERROR | /* Misaligned address */ \
  754. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  755. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  756. R1_CC_ERROR | /* Card controller error */ \
  757. R1_ERROR) /* General/unknown error */
  758. static int mmc_blk_err_check(struct mmc_card *card,
  759. struct mmc_async_req *areq)
  760. {
  761. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  762. mmc_active);
  763. struct mmc_blk_request *brq = &mq_mrq->brq;
  764. struct request *req = mq_mrq->req;
  765. int ecc_err = 0;
  766. /*
  767. * sbc.error indicates a problem with the set block count
  768. * command. No data will have been transferred.
  769. *
  770. * cmd.error indicates a problem with the r/w command. No
  771. * data will have been transferred.
  772. *
  773. * stop.error indicates a problem with the stop command. Data
  774. * may have been transferred, or may still be transferring.
  775. */
  776. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  777. brq->data.error) {
  778. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
  779. case ERR_RETRY:
  780. return MMC_BLK_RETRY;
  781. case ERR_ABORT:
  782. return MMC_BLK_ABORT;
  783. case ERR_CONTINUE:
  784. break;
  785. }
  786. }
  787. /*
  788. * Check for errors relating to the execution of the
  789. * initial command - such as address errors. No data
  790. * has been transferred.
  791. */
  792. if (brq->cmd.resp[0] & CMD_ERRORS) {
  793. pr_err("%s: r/w command failed, status = %#x\n",
  794. req->rq_disk->disk_name, brq->cmd.resp[0]);
  795. return MMC_BLK_ABORT;
  796. }
  797. /*
  798. * Everything else is either success, or a data error of some
  799. * kind. If it was a write, we may have transitioned to
  800. * program mode, which we have to wait for it to complete.
  801. */
  802. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  803. u32 status;
  804. do {
  805. int err = get_card_status(card, &status, 5);
  806. if (err) {
  807. pr_err("%s: error %d requesting status\n",
  808. req->rq_disk->disk_name, err);
  809. return MMC_BLK_CMD_ERR;
  810. }
  811. /*
  812. * Some cards mishandle the status bits,
  813. * so make sure to check both the busy
  814. * indication and the card state.
  815. */
  816. } while (!(status & R1_READY_FOR_DATA) ||
  817. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  818. }
  819. if (brq->data.error) {
  820. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  821. req->rq_disk->disk_name, brq->data.error,
  822. (unsigned)blk_rq_pos(req),
  823. (unsigned)blk_rq_sectors(req),
  824. brq->cmd.resp[0], brq->stop.resp[0]);
  825. if (rq_data_dir(req) == READ) {
  826. if (ecc_err)
  827. return MMC_BLK_ECC_ERR;
  828. return MMC_BLK_DATA_ERR;
  829. } else {
  830. return MMC_BLK_CMD_ERR;
  831. }
  832. }
  833. if (!brq->data.bytes_xfered)
  834. return MMC_BLK_RETRY;
  835. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  836. return MMC_BLK_PARTIAL;
  837. return MMC_BLK_SUCCESS;
  838. }
  839. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  840. struct mmc_card *card,
  841. int disable_multi,
  842. struct mmc_queue *mq)
  843. {
  844. u32 readcmd, writecmd;
  845. struct mmc_blk_request *brq = &mqrq->brq;
  846. struct request *req = mqrq->req;
  847. struct mmc_blk_data *md = mq->data;
  848. /*
  849. * Reliable writes are used to implement Forced Unit Access and
  850. * REQ_META accesses, and are supported only on MMCs.
  851. *
  852. * XXX: this really needs a good explanation of why REQ_META
  853. * is treated special.
  854. */
  855. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  856. (req->cmd_flags & REQ_META)) &&
  857. (rq_data_dir(req) == WRITE) &&
  858. (md->flags & MMC_BLK_REL_WR);
  859. memset(brq, 0, sizeof(struct mmc_blk_request));
  860. brq->mrq.cmd = &brq->cmd;
  861. brq->mrq.data = &brq->data;
  862. brq->cmd.arg = blk_rq_pos(req);
  863. if (!mmc_card_blockaddr(card))
  864. brq->cmd.arg <<= 9;
  865. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  866. brq->data.blksz = 512;
  867. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  868. brq->stop.arg = 0;
  869. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  870. brq->data.blocks = blk_rq_sectors(req);
  871. /*
  872. * The block layer doesn't support all sector count
  873. * restrictions, so we need to be prepared for too big
  874. * requests.
  875. */
  876. if (brq->data.blocks > card->host->max_blk_count)
  877. brq->data.blocks = card->host->max_blk_count;
  878. if (brq->data.blocks > 1) {
  879. /*
  880. * After a read error, we redo the request one sector
  881. * at a time in order to accurately determine which
  882. * sectors can be read successfully.
  883. */
  884. if (disable_multi)
  885. brq->data.blocks = 1;
  886. /* Some controllers can't do multiblock reads due to hw bugs */
  887. if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
  888. rq_data_dir(req) == READ)
  889. brq->data.blocks = 1;
  890. }
  891. if (brq->data.blocks > 1 || do_rel_wr) {
  892. /* SPI multiblock writes terminate using a special
  893. * token, not a STOP_TRANSMISSION request.
  894. */
  895. if (!mmc_host_is_spi(card->host) ||
  896. rq_data_dir(req) == READ)
  897. brq->mrq.stop = &brq->stop;
  898. readcmd = MMC_READ_MULTIPLE_BLOCK;
  899. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  900. } else {
  901. brq->mrq.stop = NULL;
  902. readcmd = MMC_READ_SINGLE_BLOCK;
  903. writecmd = MMC_WRITE_BLOCK;
  904. }
  905. if (rq_data_dir(req) == READ) {
  906. brq->cmd.opcode = readcmd;
  907. brq->data.flags |= MMC_DATA_READ;
  908. } else {
  909. brq->cmd.opcode = writecmd;
  910. brq->data.flags |= MMC_DATA_WRITE;
  911. }
  912. if (do_rel_wr)
  913. mmc_apply_rel_rw(brq, card, req);
  914. /*
  915. * Pre-defined multi-block transfers are preferable to
  916. * open ended-ones (and necessary for reliable writes).
  917. * However, it is not sufficient to just send CMD23,
  918. * and avoid the final CMD12, as on an error condition
  919. * CMD12 (stop) needs to be sent anyway. This, coupled
  920. * with Auto-CMD23 enhancements provided by some
  921. * hosts, means that the complexity of dealing
  922. * with this is best left to the host. If CMD23 is
  923. * supported by card and host, we'll fill sbc in and let
  924. * the host deal with handling it correctly. This means
  925. * that for hosts that don't expose MMC_CAP_CMD23, no
  926. * change of behavior will be observed.
  927. *
  928. * N.B: Some MMC cards experience perf degradation.
  929. * We'll avoid using CMD23-bounded multiblock writes for
  930. * these, while retaining features like reliable writes.
  931. */
  932. if ((md->flags & MMC_BLK_CMD23) &&
  933. mmc_op_multi(brq->cmd.opcode) &&
  934. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
  935. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  936. brq->sbc.arg = brq->data.blocks |
  937. (do_rel_wr ? (1 << 31) : 0);
  938. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  939. brq->mrq.sbc = &brq->sbc;
  940. }
  941. mmc_set_data_timeout(&brq->data, card);
  942. brq->data.sg = mqrq->sg;
  943. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  944. /*
  945. * Adjust the sg list so it is the same size as the
  946. * request.
  947. */
  948. if (brq->data.blocks != blk_rq_sectors(req)) {
  949. int i, data_size = brq->data.blocks << 9;
  950. struct scatterlist *sg;
  951. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  952. data_size -= sg->length;
  953. if (data_size <= 0) {
  954. sg->length += data_size;
  955. i++;
  956. break;
  957. }
  958. }
  959. brq->data.sg_len = i;
  960. }
  961. mqrq->mmc_active.mrq = &brq->mrq;
  962. mqrq->mmc_active.err_check = mmc_blk_err_check;
  963. mmc_queue_bounce_pre(mqrq);
  964. }
  965. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  966. struct mmc_blk_request *brq, struct request *req,
  967. int ret)
  968. {
  969. /*
  970. * If this is an SD card and we're writing, we can first
  971. * mark the known good sectors as ok.
  972. *
  973. * If the card is not SD, we can still ok written sectors
  974. * as reported by the controller (which might be less than
  975. * the real number of written sectors, but never more).
  976. */
  977. if (mmc_card_sd(card)) {
  978. u32 blocks;
  979. blocks = mmc_sd_num_wr_blocks(card);
  980. if (blocks != (u32)-1) {
  981. spin_lock_irq(&md->lock);
  982. ret = __blk_end_request(req, 0, blocks << 9);
  983. spin_unlock_irq(&md->lock);
  984. }
  985. } else {
  986. spin_lock_irq(&md->lock);
  987. ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
  988. spin_unlock_irq(&md->lock);
  989. }
  990. return ret;
  991. }
  992. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  993. {
  994. struct mmc_blk_data *md = mq->data;
  995. struct mmc_card *card = md->queue.card;
  996. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  997. int ret = 1, disable_multi = 0, retry = 0, type;
  998. enum mmc_blk_status status;
  999. struct mmc_queue_req *mq_rq;
  1000. struct request *req;
  1001. struct mmc_async_req *areq;
  1002. if (!rqc && !mq->mqrq_prev->req)
  1003. return 0;
  1004. do {
  1005. if (rqc) {
  1006. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1007. areq = &mq->mqrq_cur->mmc_active;
  1008. } else
  1009. areq = NULL;
  1010. areq = mmc_start_req(card->host, areq, (int *) &status);
  1011. if (!areq)
  1012. return 0;
  1013. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  1014. brq = &mq_rq->brq;
  1015. req = mq_rq->req;
  1016. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1017. mmc_queue_bounce_post(mq_rq);
  1018. switch (status) {
  1019. case MMC_BLK_SUCCESS:
  1020. case MMC_BLK_PARTIAL:
  1021. /*
  1022. * A block was successfully transferred.
  1023. */
  1024. mmc_blk_reset_success(md, type);
  1025. spin_lock_irq(&md->lock);
  1026. ret = __blk_end_request(req, 0,
  1027. brq->data.bytes_xfered);
  1028. spin_unlock_irq(&md->lock);
  1029. /*
  1030. * If the blk_end_request function returns non-zero even
  1031. * though all data has been transferred and no errors
  1032. * were returned by the host controller, it's a bug.
  1033. */
  1034. if (status == MMC_BLK_SUCCESS && ret) {
  1035. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1036. __func__, blk_rq_bytes(req),
  1037. brq->data.bytes_xfered);
  1038. rqc = NULL;
  1039. goto cmd_abort;
  1040. }
  1041. break;
  1042. case MMC_BLK_CMD_ERR:
  1043. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1044. if (!mmc_blk_reset(md, card->host, type))
  1045. break;
  1046. goto cmd_abort;
  1047. case MMC_BLK_RETRY:
  1048. if (retry++ < 5)
  1049. break;
  1050. /* Fall through */
  1051. case MMC_BLK_ABORT:
  1052. if (!mmc_blk_reset(md, card->host, type))
  1053. break;
  1054. goto cmd_abort;
  1055. case MMC_BLK_DATA_ERR: {
  1056. int err;
  1057. err = mmc_blk_reset(md, card->host, type);
  1058. if (!err)
  1059. break;
  1060. if (err == -ENODEV)
  1061. goto cmd_abort;
  1062. /* Fall through */
  1063. }
  1064. case MMC_BLK_ECC_ERR:
  1065. if (brq->data.blocks > 1) {
  1066. /* Redo read one sector at a time */
  1067. pr_warning("%s: retrying using single block read\n",
  1068. req->rq_disk->disk_name);
  1069. disable_multi = 1;
  1070. break;
  1071. }
  1072. /*
  1073. * After an error, we redo I/O one sector at a
  1074. * time, so we only reach here after trying to
  1075. * read a single sector.
  1076. */
  1077. spin_lock_irq(&md->lock);
  1078. ret = __blk_end_request(req, -EIO,
  1079. brq->data.blksz);
  1080. spin_unlock_irq(&md->lock);
  1081. if (!ret)
  1082. goto start_new_req;
  1083. break;
  1084. }
  1085. if (ret) {
  1086. /*
  1087. * In case of a incomplete request
  1088. * prepare it again and resend.
  1089. */
  1090. mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
  1091. mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
  1092. }
  1093. } while (ret);
  1094. return 1;
  1095. cmd_abort:
  1096. spin_lock_irq(&md->lock);
  1097. while (ret)
  1098. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  1099. spin_unlock_irq(&md->lock);
  1100. start_new_req:
  1101. if (rqc) {
  1102. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1103. mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
  1104. }
  1105. return 0;
  1106. }
  1107. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1108. {
  1109. int ret;
  1110. struct mmc_blk_data *md = mq->data;
  1111. struct mmc_card *card = md->queue.card;
  1112. if (req && !mq->mqrq_prev->req)
  1113. /* claim host only for the first request */
  1114. mmc_claim_host(card->host);
  1115. ret = mmc_blk_part_switch(card, md);
  1116. if (ret) {
  1117. if (req) {
  1118. spin_lock_irq(&md->lock);
  1119. __blk_end_request_all(req, -EIO);
  1120. spin_unlock_irq(&md->lock);
  1121. }
  1122. ret = 0;
  1123. goto out;
  1124. }
  1125. if (req && req->cmd_flags & REQ_DISCARD) {
  1126. /* complete ongoing async transfer before issuing discard */
  1127. if (card->host->areq)
  1128. mmc_blk_issue_rw_rq(mq, NULL);
  1129. if (req->cmd_flags & REQ_SECURE)
  1130. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1131. else
  1132. ret = mmc_blk_issue_discard_rq(mq, req);
  1133. } else if (req && req->cmd_flags & REQ_FLUSH) {
  1134. /* complete ongoing async transfer before issuing flush */
  1135. if (card->host->areq)
  1136. mmc_blk_issue_rw_rq(mq, NULL);
  1137. ret = mmc_blk_issue_flush(mq, req);
  1138. } else {
  1139. ret = mmc_blk_issue_rw_rq(mq, req);
  1140. }
  1141. out:
  1142. if (!req)
  1143. /* release host only when there are no more requests */
  1144. mmc_release_host(card->host);
  1145. return ret;
  1146. }
  1147. static inline int mmc_blk_readonly(struct mmc_card *card)
  1148. {
  1149. return mmc_card_readonly(card) ||
  1150. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1151. }
  1152. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1153. struct device *parent,
  1154. sector_t size,
  1155. bool default_ro,
  1156. const char *subname)
  1157. {
  1158. struct mmc_blk_data *md;
  1159. int devidx, ret;
  1160. devidx = find_first_zero_bit(dev_use, max_devices);
  1161. if (devidx >= max_devices)
  1162. return ERR_PTR(-ENOSPC);
  1163. __set_bit(devidx, dev_use);
  1164. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1165. if (!md) {
  1166. ret = -ENOMEM;
  1167. goto out;
  1168. }
  1169. /*
  1170. * !subname implies we are creating main mmc_blk_data that will be
  1171. * associated with mmc_card with mmc_set_drvdata. Due to device
  1172. * partitions, devidx will not coincide with a per-physical card
  1173. * index anymore so we keep track of a name index.
  1174. */
  1175. if (!subname) {
  1176. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1177. __set_bit(md->name_idx, name_use);
  1178. }
  1179. else
  1180. md->name_idx = ((struct mmc_blk_data *)
  1181. dev_to_disk(parent)->private_data)->name_idx;
  1182. /*
  1183. * Set the read-only status based on the supported commands
  1184. * and the write protect switch.
  1185. */
  1186. md->read_only = mmc_blk_readonly(card);
  1187. md->disk = alloc_disk(perdev_minors);
  1188. if (md->disk == NULL) {
  1189. ret = -ENOMEM;
  1190. goto err_kfree;
  1191. }
  1192. spin_lock_init(&md->lock);
  1193. INIT_LIST_HEAD(&md->part);
  1194. md->usage = 1;
  1195. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1196. if (ret)
  1197. goto err_putdisk;
  1198. md->queue.issue_fn = mmc_blk_issue_rq;
  1199. md->queue.data = md;
  1200. md->disk->major = MMC_BLOCK_MAJOR;
  1201. md->disk->first_minor = devidx * perdev_minors;
  1202. md->disk->fops = &mmc_bdops;
  1203. md->disk->private_data = md;
  1204. md->disk->queue = md->queue.queue;
  1205. md->disk->driverfs_dev = parent;
  1206. set_disk_ro(md->disk, md->read_only || default_ro);
  1207. /*
  1208. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1209. *
  1210. * - be set for removable media with permanent block devices
  1211. * - be unset for removable block devices with permanent media
  1212. *
  1213. * Since MMC block devices clearly fall under the second
  1214. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1215. * should use the block device creation/destruction hotplug
  1216. * messages to tell when the card is present.
  1217. */
  1218. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1219. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1220. blk_queue_logical_block_size(md->queue.queue, 512);
  1221. set_capacity(md->disk, size);
  1222. if (mmc_host_cmd23(card->host)) {
  1223. if (mmc_card_mmc(card) ||
  1224. (mmc_card_sd(card) &&
  1225. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1226. md->flags |= MMC_BLK_CMD23;
  1227. }
  1228. if (mmc_card_mmc(card) &&
  1229. md->flags & MMC_BLK_CMD23 &&
  1230. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1231. card->ext_csd.rel_sectors)) {
  1232. md->flags |= MMC_BLK_REL_WR;
  1233. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1234. }
  1235. return md;
  1236. err_putdisk:
  1237. put_disk(md->disk);
  1238. err_kfree:
  1239. kfree(md);
  1240. out:
  1241. return ERR_PTR(ret);
  1242. }
  1243. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1244. {
  1245. sector_t size;
  1246. struct mmc_blk_data *md;
  1247. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1248. /*
  1249. * The EXT_CSD sector count is in number or 512 byte
  1250. * sectors.
  1251. */
  1252. size = card->ext_csd.sectors;
  1253. } else {
  1254. /*
  1255. * The CSD capacity field is in units of read_blkbits.
  1256. * set_capacity takes units of 512 bytes.
  1257. */
  1258. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1259. }
  1260. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  1261. return md;
  1262. }
  1263. static int mmc_blk_alloc_part(struct mmc_card *card,
  1264. struct mmc_blk_data *md,
  1265. unsigned int part_type,
  1266. sector_t size,
  1267. bool default_ro,
  1268. const char *subname)
  1269. {
  1270. char cap_str[10];
  1271. struct mmc_blk_data *part_md;
  1272. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1273. subname);
  1274. if (IS_ERR(part_md))
  1275. return PTR_ERR(part_md);
  1276. part_md->part_type = part_type;
  1277. list_add(&part_md->part, &md->part);
  1278. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1279. cap_str, sizeof(cap_str));
  1280. pr_info("%s: %s %s partition %u %s\n",
  1281. part_md->disk->disk_name, mmc_card_id(card),
  1282. mmc_card_name(card), part_md->part_type, cap_str);
  1283. return 0;
  1284. }
  1285. /* MMC Physical partitions consist of two boot partitions and
  1286. * up to four general purpose partitions.
  1287. * For each partition enabled in EXT_CSD a block device will be allocatedi
  1288. * to provide access to the partition.
  1289. */
  1290. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1291. {
  1292. int idx, ret = 0;
  1293. if (!mmc_card_mmc(card))
  1294. return 0;
  1295. for (idx = 0; idx < card->nr_parts; idx++) {
  1296. if (card->part[idx].size) {
  1297. ret = mmc_blk_alloc_part(card, md,
  1298. card->part[idx].part_cfg,
  1299. card->part[idx].size >> 9,
  1300. card->part[idx].force_ro,
  1301. card->part[idx].name);
  1302. if (ret)
  1303. return ret;
  1304. }
  1305. }
  1306. return ret;
  1307. }
  1308. static int
  1309. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  1310. {
  1311. int err;
  1312. mmc_claim_host(card->host);
  1313. err = mmc_set_blocklen(card, 512);
  1314. mmc_release_host(card->host);
  1315. if (err) {
  1316. pr_err("%s: unable to set block size to 512: %d\n",
  1317. md->disk->disk_name, err);
  1318. return -EINVAL;
  1319. }
  1320. return 0;
  1321. }
  1322. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1323. {
  1324. if (md) {
  1325. if (md->disk->flags & GENHD_FL_UP) {
  1326. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1327. /* Stop new requests from getting into the queue */
  1328. del_gendisk(md->disk);
  1329. }
  1330. /* Then flush out any already in there */
  1331. mmc_cleanup_queue(&md->queue);
  1332. mmc_blk_put(md);
  1333. }
  1334. }
  1335. static void mmc_blk_remove_parts(struct mmc_card *card,
  1336. struct mmc_blk_data *md)
  1337. {
  1338. struct list_head *pos, *q;
  1339. struct mmc_blk_data *part_md;
  1340. __clear_bit(md->name_idx, name_use);
  1341. list_for_each_safe(pos, q, &md->part) {
  1342. part_md = list_entry(pos, struct mmc_blk_data, part);
  1343. list_del(pos);
  1344. mmc_blk_remove_req(part_md);
  1345. }
  1346. }
  1347. static int mmc_add_disk(struct mmc_blk_data *md)
  1348. {
  1349. int ret;
  1350. add_disk(md->disk);
  1351. md->force_ro.show = force_ro_show;
  1352. md->force_ro.store = force_ro_store;
  1353. sysfs_attr_init(&md->force_ro.attr);
  1354. md->force_ro.attr.name = "force_ro";
  1355. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1356. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1357. if (ret)
  1358. del_gendisk(md->disk);
  1359. return ret;
  1360. }
  1361. static const struct mmc_fixup blk_fixups[] =
  1362. {
  1363. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1364. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1365. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1366. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1367. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1368. /*
  1369. * Some MMC cards experience performance degradation with CMD23
  1370. * instead of CMD12-bounded multiblock transfers. For now we'll
  1371. * black list what's bad...
  1372. * - Certain Toshiba cards.
  1373. *
  1374. * N.B. This doesn't affect SD cards.
  1375. */
  1376. MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1377. MMC_QUIRK_BLK_NO_CMD23),
  1378. MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1379. MMC_QUIRK_BLK_NO_CMD23),
  1380. MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1381. MMC_QUIRK_BLK_NO_CMD23),
  1382. END_FIXUP
  1383. };
  1384. static int mmc_blk_probe(struct mmc_card *card)
  1385. {
  1386. struct mmc_blk_data *md, *part_md;
  1387. int err;
  1388. char cap_str[10];
  1389. /*
  1390. * Check that the card supports the command class(es) we need.
  1391. */
  1392. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1393. return -ENODEV;
  1394. md = mmc_blk_alloc(card);
  1395. if (IS_ERR(md))
  1396. return PTR_ERR(md);
  1397. err = mmc_blk_set_blksize(md, card);
  1398. if (err)
  1399. goto out;
  1400. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1401. cap_str, sizeof(cap_str));
  1402. pr_info("%s: %s %s %s %s\n",
  1403. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1404. cap_str, md->read_only ? "(ro)" : "");
  1405. if (mmc_blk_alloc_parts(card, md))
  1406. goto out;
  1407. mmc_set_drvdata(card, md);
  1408. mmc_fixup_device(card, blk_fixups);
  1409. if (mmc_add_disk(md))
  1410. goto out;
  1411. list_for_each_entry(part_md, &md->part, part) {
  1412. if (mmc_add_disk(part_md))
  1413. goto out;
  1414. }
  1415. return 0;
  1416. out:
  1417. mmc_blk_remove_parts(card, md);
  1418. mmc_blk_remove_req(md);
  1419. return err;
  1420. }
  1421. static void mmc_blk_remove(struct mmc_card *card)
  1422. {
  1423. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1424. mmc_blk_remove_parts(card, md);
  1425. mmc_claim_host(card->host);
  1426. mmc_blk_part_switch(card, md);
  1427. mmc_release_host(card->host);
  1428. mmc_blk_remove_req(md);
  1429. mmc_set_drvdata(card, NULL);
  1430. }
  1431. #ifdef CONFIG_PM
  1432. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1433. {
  1434. struct mmc_blk_data *part_md;
  1435. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1436. if (md) {
  1437. mmc_queue_suspend(&md->queue);
  1438. list_for_each_entry(part_md, &md->part, part) {
  1439. mmc_queue_suspend(&part_md->queue);
  1440. }
  1441. }
  1442. return 0;
  1443. }
  1444. static int mmc_blk_resume(struct mmc_card *card)
  1445. {
  1446. struct mmc_blk_data *part_md;
  1447. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1448. if (md) {
  1449. mmc_blk_set_blksize(md, card);
  1450. /*
  1451. * Resume involves the card going into idle state,
  1452. * so current partition is always the main one.
  1453. */
  1454. md->part_curr = md->part_type;
  1455. mmc_queue_resume(&md->queue);
  1456. list_for_each_entry(part_md, &md->part, part) {
  1457. mmc_queue_resume(&part_md->queue);
  1458. }
  1459. }
  1460. return 0;
  1461. }
  1462. #else
  1463. #define mmc_blk_suspend NULL
  1464. #define mmc_blk_resume NULL
  1465. #endif
  1466. static struct mmc_driver mmc_driver = {
  1467. .drv = {
  1468. .name = "mmcblk",
  1469. },
  1470. .probe = mmc_blk_probe,
  1471. .remove = mmc_blk_remove,
  1472. .suspend = mmc_blk_suspend,
  1473. .resume = mmc_blk_resume,
  1474. };
  1475. static int __init mmc_blk_init(void)
  1476. {
  1477. int res;
  1478. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1479. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1480. max_devices = 256 / perdev_minors;
  1481. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1482. if (res)
  1483. goto out;
  1484. res = mmc_register_driver(&mmc_driver);
  1485. if (res)
  1486. goto out2;
  1487. return 0;
  1488. out2:
  1489. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1490. out:
  1491. return res;
  1492. }
  1493. static void __exit mmc_blk_exit(void)
  1494. {
  1495. mmc_unregister_driver(&mmc_driver);
  1496. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1497. }
  1498. module_init(mmc_blk_init);
  1499. module_exit(mmc_blk_exit);
  1500. MODULE_LICENSE("GPL");
  1501. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");