block.c 47 KB

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