block.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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 void mmc_blk_remove_req(struct mmc_blk_data *md)
  1394. {
  1395. struct mmc_card *card;
  1396. if (md) {
  1397. card = md->queue.card;
  1398. if (md->disk->flags & GENHD_FL_UP) {
  1399. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1400. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1401. card->ext_csd.boot_ro_lockable)
  1402. device_remove_file(disk_to_dev(md->disk),
  1403. &md->power_ro_lock);
  1404. /* Stop new requests from getting into the queue */
  1405. del_gendisk(md->disk);
  1406. }
  1407. /* Then flush out any already in there */
  1408. mmc_cleanup_queue(&md->queue);
  1409. mmc_blk_put(md);
  1410. }
  1411. }
  1412. static void mmc_blk_remove_parts(struct mmc_card *card,
  1413. struct mmc_blk_data *md)
  1414. {
  1415. struct list_head *pos, *q;
  1416. struct mmc_blk_data *part_md;
  1417. __clear_bit(md->name_idx, name_use);
  1418. list_for_each_safe(pos, q, &md->part) {
  1419. part_md = list_entry(pos, struct mmc_blk_data, part);
  1420. list_del(pos);
  1421. mmc_blk_remove_req(part_md);
  1422. }
  1423. }
  1424. static int mmc_add_disk(struct mmc_blk_data *md)
  1425. {
  1426. int ret;
  1427. struct mmc_card *card = md->queue.card;
  1428. add_disk(md->disk);
  1429. md->force_ro.show = force_ro_show;
  1430. md->force_ro.store = force_ro_store;
  1431. sysfs_attr_init(&md->force_ro.attr);
  1432. md->force_ro.attr.name = "force_ro";
  1433. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1434. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1435. if (ret)
  1436. goto force_ro_fail;
  1437. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1438. card->ext_csd.boot_ro_lockable) {
  1439. umode_t mode;
  1440. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  1441. mode = S_IRUGO;
  1442. else
  1443. mode = S_IRUGO | S_IWUSR;
  1444. md->power_ro_lock.show = power_ro_lock_show;
  1445. md->power_ro_lock.store = power_ro_lock_store;
  1446. sysfs_attr_init(&md->power_ro_lock.attr);
  1447. md->power_ro_lock.attr.mode = mode;
  1448. md->power_ro_lock.attr.name =
  1449. "ro_lock_until_next_power_on";
  1450. ret = device_create_file(disk_to_dev(md->disk),
  1451. &md->power_ro_lock);
  1452. if (ret)
  1453. goto power_ro_lock_fail;
  1454. }
  1455. return ret;
  1456. power_ro_lock_fail:
  1457. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1458. force_ro_fail:
  1459. del_gendisk(md->disk);
  1460. return ret;
  1461. }
  1462. #define CID_MANFID_SANDISK 0x2
  1463. #define CID_MANFID_TOSHIBA 0x11
  1464. #define CID_MANFID_MICRON 0x13
  1465. static const struct mmc_fixup blk_fixups[] =
  1466. {
  1467. MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1468. MMC_QUIRK_INAND_CMD38),
  1469. MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1470. MMC_QUIRK_INAND_CMD38),
  1471. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1472. MMC_QUIRK_INAND_CMD38),
  1473. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1474. MMC_QUIRK_INAND_CMD38),
  1475. MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1476. MMC_QUIRK_INAND_CMD38),
  1477. /*
  1478. * Some MMC cards experience performance degradation with CMD23
  1479. * instead of CMD12-bounded multiblock transfers. For now we'll
  1480. * black list what's bad...
  1481. * - Certain Toshiba cards.
  1482. *
  1483. * N.B. This doesn't affect SD cards.
  1484. */
  1485. MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1486. MMC_QUIRK_BLK_NO_CMD23),
  1487. MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1488. MMC_QUIRK_BLK_NO_CMD23),
  1489. MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1490. MMC_QUIRK_BLK_NO_CMD23),
  1491. /*
  1492. * Some Micron MMC cards needs longer data read timeout than
  1493. * indicated in CSD.
  1494. */
  1495. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
  1496. MMC_QUIRK_LONG_READ_TIME),
  1497. END_FIXUP
  1498. };
  1499. static int mmc_blk_probe(struct mmc_card *card)
  1500. {
  1501. struct mmc_blk_data *md, *part_md;
  1502. char cap_str[10];
  1503. /*
  1504. * Check that the card supports the command class(es) we need.
  1505. */
  1506. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1507. return -ENODEV;
  1508. md = mmc_blk_alloc(card);
  1509. if (IS_ERR(md))
  1510. return PTR_ERR(md);
  1511. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1512. cap_str, sizeof(cap_str));
  1513. pr_info("%s: %s %s %s %s\n",
  1514. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1515. cap_str, md->read_only ? "(ro)" : "");
  1516. if (mmc_blk_alloc_parts(card, md))
  1517. goto out;
  1518. mmc_set_drvdata(card, md);
  1519. mmc_fixup_device(card, blk_fixups);
  1520. if (mmc_add_disk(md))
  1521. goto out;
  1522. list_for_each_entry(part_md, &md->part, part) {
  1523. if (mmc_add_disk(part_md))
  1524. goto out;
  1525. }
  1526. return 0;
  1527. out:
  1528. mmc_blk_remove_parts(card, md);
  1529. mmc_blk_remove_req(md);
  1530. return 0;
  1531. }
  1532. static void mmc_blk_remove(struct mmc_card *card)
  1533. {
  1534. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1535. mmc_blk_remove_parts(card, md);
  1536. mmc_claim_host(card->host);
  1537. mmc_blk_part_switch(card, md);
  1538. mmc_release_host(card->host);
  1539. mmc_blk_remove_req(md);
  1540. mmc_set_drvdata(card, NULL);
  1541. }
  1542. #ifdef CONFIG_PM
  1543. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1544. {
  1545. struct mmc_blk_data *part_md;
  1546. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1547. if (md) {
  1548. mmc_queue_suspend(&md->queue);
  1549. list_for_each_entry(part_md, &md->part, part) {
  1550. mmc_queue_suspend(&part_md->queue);
  1551. }
  1552. }
  1553. return 0;
  1554. }
  1555. static int mmc_blk_resume(struct mmc_card *card)
  1556. {
  1557. struct mmc_blk_data *part_md;
  1558. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1559. if (md) {
  1560. /*
  1561. * Resume involves the card going into idle state,
  1562. * so current partition is always the main one.
  1563. */
  1564. md->part_curr = md->part_type;
  1565. mmc_queue_resume(&md->queue);
  1566. list_for_each_entry(part_md, &md->part, part) {
  1567. mmc_queue_resume(&part_md->queue);
  1568. }
  1569. }
  1570. return 0;
  1571. }
  1572. #else
  1573. #define mmc_blk_suspend NULL
  1574. #define mmc_blk_resume NULL
  1575. #endif
  1576. static struct mmc_driver mmc_driver = {
  1577. .drv = {
  1578. .name = "mmcblk",
  1579. },
  1580. .probe = mmc_blk_probe,
  1581. .remove = mmc_blk_remove,
  1582. .suspend = mmc_blk_suspend,
  1583. .resume = mmc_blk_resume,
  1584. };
  1585. static int __init mmc_blk_init(void)
  1586. {
  1587. int res;
  1588. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1589. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1590. max_devices = 256 / perdev_minors;
  1591. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1592. if (res)
  1593. goto out;
  1594. res = mmc_register_driver(&mmc_driver);
  1595. if (res)
  1596. goto out2;
  1597. return 0;
  1598. out2:
  1599. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1600. out:
  1601. return res;
  1602. }
  1603. static void __exit mmc_blk_exit(void)
  1604. {
  1605. mmc_unregister_driver(&mmc_driver);
  1606. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1607. }
  1608. module_init(mmc_blk_init);
  1609. module_exit(mmc_blk_exit);
  1610. MODULE_LICENSE("GPL");
  1611. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");