block.c 47 KB

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