block.c 43 KB

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