block.c 40 KB

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