block.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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_trim(card))
  634. arg = MMC_TRIM_ARG;
  635. else
  636. arg = MMC_ERASE_ARG;
  637. retry:
  638. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  639. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  640. INAND_CMD38_ARG_EXT_CSD,
  641. arg == MMC_TRIM_ARG ?
  642. INAND_CMD38_ARG_TRIM :
  643. INAND_CMD38_ARG_ERASE,
  644. 0);
  645. if (err)
  646. goto out;
  647. }
  648. err = mmc_erase(card, from, nr, arg);
  649. out:
  650. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  651. goto retry;
  652. if (!err)
  653. mmc_blk_reset_success(md, type);
  654. spin_lock_irq(&md->lock);
  655. __blk_end_request(req, err, blk_rq_bytes(req));
  656. spin_unlock_irq(&md->lock);
  657. return err ? 0 : 1;
  658. }
  659. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  660. struct request *req)
  661. {
  662. struct mmc_blk_data *md = mq->data;
  663. struct mmc_card *card = md->queue.card;
  664. unsigned int from, nr, arg;
  665. int err = 0, type = MMC_BLK_SECDISCARD;
  666. if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
  667. err = -EOPNOTSUPP;
  668. goto out;
  669. }
  670. /* The sanitize operation is supported at v4.5 only */
  671. if (mmc_can_sanitize(card)) {
  672. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  673. EXT_CSD_SANITIZE_START, 1, 0);
  674. goto out;
  675. }
  676. from = blk_rq_pos(req);
  677. nr = blk_rq_sectors(req);
  678. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  679. arg = MMC_SECURE_TRIM1_ARG;
  680. else
  681. arg = MMC_SECURE_ERASE_ARG;
  682. retry:
  683. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  684. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  685. INAND_CMD38_ARG_EXT_CSD,
  686. arg == MMC_SECURE_TRIM1_ARG ?
  687. INAND_CMD38_ARG_SECTRIM1 :
  688. INAND_CMD38_ARG_SECERASE,
  689. 0);
  690. if (err)
  691. goto out;
  692. }
  693. err = mmc_erase(card, from, nr, arg);
  694. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  695. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  696. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  697. INAND_CMD38_ARG_EXT_CSD,
  698. INAND_CMD38_ARG_SECTRIM2,
  699. 0);
  700. if (err)
  701. goto out;
  702. }
  703. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  704. }
  705. out:
  706. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  707. goto retry;
  708. if (!err)
  709. mmc_blk_reset_success(md, type);
  710. spin_lock_irq(&md->lock);
  711. __blk_end_request(req, err, blk_rq_bytes(req));
  712. spin_unlock_irq(&md->lock);
  713. return err ? 0 : 1;
  714. }
  715. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  716. {
  717. struct mmc_blk_data *md = mq->data;
  718. /*
  719. * No-op, only service this because we need REQ_FUA for reliable
  720. * writes.
  721. */
  722. spin_lock_irq(&md->lock);
  723. __blk_end_request_all(req, 0);
  724. spin_unlock_irq(&md->lock);
  725. return 1;
  726. }
  727. /*
  728. * Reformat current write as a reliable write, supporting
  729. * both legacy and the enhanced reliable write MMC cards.
  730. * In each transfer we'll handle only as much as a single
  731. * reliable write can handle, thus finish the request in
  732. * partial completions.
  733. */
  734. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  735. struct mmc_card *card,
  736. struct request *req)
  737. {
  738. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  739. /* Legacy mode imposes restrictions on transfers. */
  740. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  741. brq->data.blocks = 1;
  742. if (brq->data.blocks > card->ext_csd.rel_sectors)
  743. brq->data.blocks = card->ext_csd.rel_sectors;
  744. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  745. brq->data.blocks = 1;
  746. }
  747. }
  748. #define CMD_ERRORS \
  749. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  750. R1_ADDRESS_ERROR | /* Misaligned address */ \
  751. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  752. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  753. R1_CC_ERROR | /* Card controller error */ \
  754. R1_ERROR) /* General/unknown error */
  755. static int mmc_blk_err_check(struct mmc_card *card,
  756. struct mmc_async_req *areq)
  757. {
  758. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  759. mmc_active);
  760. struct mmc_blk_request *brq = &mq_mrq->brq;
  761. struct request *req = mq_mrq->req;
  762. int ecc_err = 0;
  763. /*
  764. * sbc.error indicates a problem with the set block count
  765. * command. No data will have been transferred.
  766. *
  767. * cmd.error indicates a problem with the r/w command. No
  768. * data will have been transferred.
  769. *
  770. * stop.error indicates a problem with the stop command. Data
  771. * may have been transferred, or may still be transferring.
  772. */
  773. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  774. brq->data.error) {
  775. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
  776. case ERR_RETRY:
  777. return MMC_BLK_RETRY;
  778. case ERR_ABORT:
  779. return MMC_BLK_ABORT;
  780. case ERR_CONTINUE:
  781. break;
  782. }
  783. }
  784. /*
  785. * Check for errors relating to the execution of the
  786. * initial command - such as address errors. No data
  787. * has been transferred.
  788. */
  789. if (brq->cmd.resp[0] & CMD_ERRORS) {
  790. pr_err("%s: r/w command failed, status = %#x\n",
  791. req->rq_disk->disk_name, brq->cmd.resp[0]);
  792. return MMC_BLK_ABORT;
  793. }
  794. /*
  795. * Everything else is either success, or a data error of some
  796. * kind. If it was a write, we may have transitioned to
  797. * program mode, which we have to wait for it to complete.
  798. */
  799. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  800. u32 status;
  801. do {
  802. int err = get_card_status(card, &status, 5);
  803. if (err) {
  804. pr_err("%s: error %d requesting status\n",
  805. req->rq_disk->disk_name, err);
  806. return MMC_BLK_CMD_ERR;
  807. }
  808. /*
  809. * Some cards mishandle the status bits,
  810. * so make sure to check both the busy
  811. * indication and the card state.
  812. */
  813. } while (!(status & R1_READY_FOR_DATA) ||
  814. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  815. }
  816. if (brq->data.error) {
  817. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  818. req->rq_disk->disk_name, brq->data.error,
  819. (unsigned)blk_rq_pos(req),
  820. (unsigned)blk_rq_sectors(req),
  821. brq->cmd.resp[0], brq->stop.resp[0]);
  822. if (rq_data_dir(req) == READ) {
  823. if (ecc_err)
  824. return MMC_BLK_ECC_ERR;
  825. return MMC_BLK_DATA_ERR;
  826. } else {
  827. return MMC_BLK_CMD_ERR;
  828. }
  829. }
  830. if (!brq->data.bytes_xfered)
  831. return MMC_BLK_RETRY;
  832. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  833. return MMC_BLK_PARTIAL;
  834. return MMC_BLK_SUCCESS;
  835. }
  836. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  837. struct mmc_card *card,
  838. int disable_multi,
  839. struct mmc_queue *mq)
  840. {
  841. u32 readcmd, writecmd;
  842. struct mmc_blk_request *brq = &mqrq->brq;
  843. struct request *req = mqrq->req;
  844. struct mmc_blk_data *md = mq->data;
  845. /*
  846. * Reliable writes are used to implement Forced Unit Access and
  847. * REQ_META accesses, and are supported only on MMCs.
  848. *
  849. * XXX: this really needs a good explanation of why REQ_META
  850. * is treated special.
  851. */
  852. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  853. (req->cmd_flags & REQ_META)) &&
  854. (rq_data_dir(req) == WRITE) &&
  855. (md->flags & MMC_BLK_REL_WR);
  856. memset(brq, 0, sizeof(struct mmc_blk_request));
  857. brq->mrq.cmd = &brq->cmd;
  858. brq->mrq.data = &brq->data;
  859. brq->cmd.arg = blk_rq_pos(req);
  860. if (!mmc_card_blockaddr(card))
  861. brq->cmd.arg <<= 9;
  862. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  863. brq->data.blksz = 512;
  864. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  865. brq->stop.arg = 0;
  866. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  867. brq->data.blocks = blk_rq_sectors(req);
  868. /*
  869. * The block layer doesn't support all sector count
  870. * restrictions, so we need to be prepared for too big
  871. * requests.
  872. */
  873. if (brq->data.blocks > card->host->max_blk_count)
  874. brq->data.blocks = card->host->max_blk_count;
  875. /*
  876. * After a read error, we redo the request one sector at a time
  877. * in order to accurately determine which sectors can be read
  878. * successfully.
  879. */
  880. if (disable_multi && brq->data.blocks > 1)
  881. brq->data.blocks = 1;
  882. if (brq->data.blocks > 1 || do_rel_wr) {
  883. /* SPI multiblock writes terminate using a special
  884. * token, not a STOP_TRANSMISSION request.
  885. */
  886. if (!mmc_host_is_spi(card->host) ||
  887. rq_data_dir(req) == READ)
  888. brq->mrq.stop = &brq->stop;
  889. readcmd = MMC_READ_MULTIPLE_BLOCK;
  890. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  891. } else {
  892. brq->mrq.stop = NULL;
  893. readcmd = MMC_READ_SINGLE_BLOCK;
  894. writecmd = MMC_WRITE_BLOCK;
  895. }
  896. if (rq_data_dir(req) == READ) {
  897. brq->cmd.opcode = readcmd;
  898. brq->data.flags |= MMC_DATA_READ;
  899. } else {
  900. brq->cmd.opcode = writecmd;
  901. brq->data.flags |= MMC_DATA_WRITE;
  902. }
  903. if (do_rel_wr)
  904. mmc_apply_rel_rw(brq, card, req);
  905. /*
  906. * Pre-defined multi-block transfers are preferable to
  907. * open ended-ones (and necessary for reliable writes).
  908. * However, it is not sufficient to just send CMD23,
  909. * and avoid the final CMD12, as on an error condition
  910. * CMD12 (stop) needs to be sent anyway. This, coupled
  911. * with Auto-CMD23 enhancements provided by some
  912. * hosts, means that the complexity of dealing
  913. * with this is best left to the host. If CMD23 is
  914. * supported by card and host, we'll fill sbc in and let
  915. * the host deal with handling it correctly. This means
  916. * that for hosts that don't expose MMC_CAP_CMD23, no
  917. * change of behavior will be observed.
  918. *
  919. * N.B: Some MMC cards experience perf degradation.
  920. * We'll avoid using CMD23-bounded multiblock writes for
  921. * these, while retaining features like reliable writes.
  922. */
  923. if ((md->flags & MMC_BLK_CMD23) &&
  924. mmc_op_multi(brq->cmd.opcode) &&
  925. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
  926. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  927. brq->sbc.arg = brq->data.blocks |
  928. (do_rel_wr ? (1 << 31) : 0);
  929. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  930. brq->mrq.sbc = &brq->sbc;
  931. }
  932. mmc_set_data_timeout(&brq->data, card);
  933. brq->data.sg = mqrq->sg;
  934. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  935. /*
  936. * Adjust the sg list so it is the same size as the
  937. * request.
  938. */
  939. if (brq->data.blocks != blk_rq_sectors(req)) {
  940. int i, data_size = brq->data.blocks << 9;
  941. struct scatterlist *sg;
  942. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  943. data_size -= sg->length;
  944. if (data_size <= 0) {
  945. sg->length += data_size;
  946. i++;
  947. break;
  948. }
  949. }
  950. brq->data.sg_len = i;
  951. }
  952. mqrq->mmc_active.mrq = &brq->mrq;
  953. mqrq->mmc_active.err_check = mmc_blk_err_check;
  954. mmc_queue_bounce_pre(mqrq);
  955. }
  956. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  957. struct mmc_blk_request *brq, struct request *req,
  958. int ret)
  959. {
  960. /*
  961. * If this is an SD card and we're writing, we can first
  962. * mark the known good sectors as ok.
  963. *
  964. * If the card is not SD, we can still ok written sectors
  965. * as reported by the controller (which might be less than
  966. * the real number of written sectors, but never more).
  967. */
  968. if (mmc_card_sd(card)) {
  969. u32 blocks;
  970. blocks = mmc_sd_num_wr_blocks(card);
  971. if (blocks != (u32)-1) {
  972. spin_lock_irq(&md->lock);
  973. ret = __blk_end_request(req, 0, blocks << 9);
  974. spin_unlock_irq(&md->lock);
  975. }
  976. } else {
  977. spin_lock_irq(&md->lock);
  978. ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
  979. spin_unlock_irq(&md->lock);
  980. }
  981. return ret;
  982. }
  983. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  984. {
  985. struct mmc_blk_data *md = mq->data;
  986. struct mmc_card *card = md->queue.card;
  987. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  988. int ret = 1, disable_multi = 0, retry = 0, type;
  989. enum mmc_blk_status status;
  990. struct mmc_queue_req *mq_rq;
  991. struct request *req;
  992. struct mmc_async_req *areq;
  993. if (!rqc && !mq->mqrq_prev->req)
  994. return 0;
  995. do {
  996. if (rqc) {
  997. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  998. areq = &mq->mqrq_cur->mmc_active;
  999. } else
  1000. areq = NULL;
  1001. areq = mmc_start_req(card->host, areq, (int *) &status);
  1002. if (!areq)
  1003. return 0;
  1004. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  1005. brq = &mq_rq->brq;
  1006. req = mq_rq->req;
  1007. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1008. mmc_queue_bounce_post(mq_rq);
  1009. switch (status) {
  1010. case MMC_BLK_SUCCESS:
  1011. case MMC_BLK_PARTIAL:
  1012. /*
  1013. * A block was successfully transferred.
  1014. */
  1015. mmc_blk_reset_success(md, type);
  1016. spin_lock_irq(&md->lock);
  1017. ret = __blk_end_request(req, 0,
  1018. brq->data.bytes_xfered);
  1019. spin_unlock_irq(&md->lock);
  1020. /*
  1021. * If the blk_end_request function returns non-zero even
  1022. * though all data has been transferred and no errors
  1023. * were returned by the host controller, it's a bug.
  1024. */
  1025. if (status == MMC_BLK_SUCCESS && ret) {
  1026. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1027. __func__, blk_rq_bytes(req),
  1028. brq->data.bytes_xfered);
  1029. rqc = NULL;
  1030. goto cmd_abort;
  1031. }
  1032. break;
  1033. case MMC_BLK_CMD_ERR:
  1034. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1035. if (!mmc_blk_reset(md, card->host, type))
  1036. break;
  1037. goto cmd_abort;
  1038. case MMC_BLK_RETRY:
  1039. if (retry++ < 5)
  1040. break;
  1041. /* Fall through */
  1042. case MMC_BLK_ABORT:
  1043. if (!mmc_blk_reset(md, card->host, type))
  1044. break;
  1045. goto cmd_abort;
  1046. case MMC_BLK_DATA_ERR: {
  1047. int err;
  1048. err = mmc_blk_reset(md, card->host, type);
  1049. if (!err)
  1050. break;
  1051. if (err == -ENODEV)
  1052. goto cmd_abort;
  1053. /* Fall through */
  1054. }
  1055. case MMC_BLK_ECC_ERR:
  1056. if (brq->data.blocks > 1) {
  1057. /* Redo read one sector at a time */
  1058. pr_warning("%s: retrying using single block read\n",
  1059. req->rq_disk->disk_name);
  1060. disable_multi = 1;
  1061. break;
  1062. }
  1063. /*
  1064. * After an error, we redo I/O one sector at a
  1065. * time, so we only reach here after trying to
  1066. * read a single sector.
  1067. */
  1068. spin_lock_irq(&md->lock);
  1069. ret = __blk_end_request(req, -EIO,
  1070. brq->data.blksz);
  1071. spin_unlock_irq(&md->lock);
  1072. if (!ret)
  1073. goto start_new_req;
  1074. break;
  1075. }
  1076. if (ret) {
  1077. /*
  1078. * In case of a incomplete request
  1079. * prepare it again and resend.
  1080. */
  1081. mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
  1082. mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
  1083. }
  1084. } while (ret);
  1085. return 1;
  1086. cmd_abort:
  1087. spin_lock_irq(&md->lock);
  1088. while (ret)
  1089. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  1090. spin_unlock_irq(&md->lock);
  1091. start_new_req:
  1092. if (rqc) {
  1093. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1094. mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
  1095. }
  1096. return 0;
  1097. }
  1098. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1099. {
  1100. int ret;
  1101. struct mmc_blk_data *md = mq->data;
  1102. struct mmc_card *card = md->queue.card;
  1103. if (req && !mq->mqrq_prev->req)
  1104. /* claim host only for the first request */
  1105. mmc_claim_host(card->host);
  1106. ret = mmc_blk_part_switch(card, md);
  1107. if (ret) {
  1108. if (req) {
  1109. spin_lock_irq(&md->lock);
  1110. __blk_end_request_all(req, -EIO);
  1111. spin_unlock_irq(&md->lock);
  1112. }
  1113. ret = 0;
  1114. goto out;
  1115. }
  1116. if (req && req->cmd_flags & REQ_DISCARD) {
  1117. /* complete ongoing async transfer before issuing discard */
  1118. if (card->host->areq)
  1119. mmc_blk_issue_rw_rq(mq, NULL);
  1120. if (req->cmd_flags & REQ_SECURE)
  1121. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1122. else
  1123. ret = mmc_blk_issue_discard_rq(mq, req);
  1124. } else if (req && req->cmd_flags & REQ_FLUSH) {
  1125. /* complete ongoing async transfer before issuing flush */
  1126. if (card->host->areq)
  1127. mmc_blk_issue_rw_rq(mq, NULL);
  1128. ret = mmc_blk_issue_flush(mq, req);
  1129. } else {
  1130. ret = mmc_blk_issue_rw_rq(mq, req);
  1131. }
  1132. out:
  1133. if (!req)
  1134. /* release host only when there are no more requests */
  1135. mmc_release_host(card->host);
  1136. return ret;
  1137. }
  1138. static inline int mmc_blk_readonly(struct mmc_card *card)
  1139. {
  1140. return mmc_card_readonly(card) ||
  1141. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1142. }
  1143. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1144. struct device *parent,
  1145. sector_t size,
  1146. bool default_ro,
  1147. const char *subname)
  1148. {
  1149. struct mmc_blk_data *md;
  1150. int devidx, ret;
  1151. devidx = find_first_zero_bit(dev_use, max_devices);
  1152. if (devidx >= max_devices)
  1153. return ERR_PTR(-ENOSPC);
  1154. __set_bit(devidx, dev_use);
  1155. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1156. if (!md) {
  1157. ret = -ENOMEM;
  1158. goto out;
  1159. }
  1160. /*
  1161. * !subname implies we are creating main mmc_blk_data that will be
  1162. * associated with mmc_card with mmc_set_drvdata. Due to device
  1163. * partitions, devidx will not coincide with a per-physical card
  1164. * index anymore so we keep track of a name index.
  1165. */
  1166. if (!subname) {
  1167. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1168. __set_bit(md->name_idx, name_use);
  1169. }
  1170. else
  1171. md->name_idx = ((struct mmc_blk_data *)
  1172. dev_to_disk(parent)->private_data)->name_idx;
  1173. /*
  1174. * Set the read-only status based on the supported commands
  1175. * and the write protect switch.
  1176. */
  1177. md->read_only = mmc_blk_readonly(card);
  1178. md->disk = alloc_disk(perdev_minors);
  1179. if (md->disk == NULL) {
  1180. ret = -ENOMEM;
  1181. goto err_kfree;
  1182. }
  1183. spin_lock_init(&md->lock);
  1184. INIT_LIST_HEAD(&md->part);
  1185. md->usage = 1;
  1186. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1187. if (ret)
  1188. goto err_putdisk;
  1189. md->queue.issue_fn = mmc_blk_issue_rq;
  1190. md->queue.data = md;
  1191. md->disk->major = MMC_BLOCK_MAJOR;
  1192. md->disk->first_minor = devidx * perdev_minors;
  1193. md->disk->fops = &mmc_bdops;
  1194. md->disk->private_data = md;
  1195. md->disk->queue = md->queue.queue;
  1196. md->disk->driverfs_dev = parent;
  1197. set_disk_ro(md->disk, md->read_only || default_ro);
  1198. /*
  1199. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1200. *
  1201. * - be set for removable media with permanent block devices
  1202. * - be unset for removable block devices with permanent media
  1203. *
  1204. * Since MMC block devices clearly fall under the second
  1205. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1206. * should use the block device creation/destruction hotplug
  1207. * messages to tell when the card is present.
  1208. */
  1209. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1210. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1211. blk_queue_logical_block_size(md->queue.queue, 512);
  1212. set_capacity(md->disk, size);
  1213. if (mmc_host_cmd23(card->host)) {
  1214. if (mmc_card_mmc(card) ||
  1215. (mmc_card_sd(card) &&
  1216. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1217. md->flags |= MMC_BLK_CMD23;
  1218. }
  1219. if (mmc_card_mmc(card) &&
  1220. md->flags & MMC_BLK_CMD23 &&
  1221. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1222. card->ext_csd.rel_sectors)) {
  1223. md->flags |= MMC_BLK_REL_WR;
  1224. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1225. }
  1226. return md;
  1227. err_putdisk:
  1228. put_disk(md->disk);
  1229. err_kfree:
  1230. kfree(md);
  1231. out:
  1232. return ERR_PTR(ret);
  1233. }
  1234. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1235. {
  1236. sector_t size;
  1237. struct mmc_blk_data *md;
  1238. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1239. /*
  1240. * The EXT_CSD sector count is in number or 512 byte
  1241. * sectors.
  1242. */
  1243. size = card->ext_csd.sectors;
  1244. } else {
  1245. /*
  1246. * The CSD capacity field is in units of read_blkbits.
  1247. * set_capacity takes units of 512 bytes.
  1248. */
  1249. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1250. }
  1251. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  1252. return md;
  1253. }
  1254. static int mmc_blk_alloc_part(struct mmc_card *card,
  1255. struct mmc_blk_data *md,
  1256. unsigned int part_type,
  1257. sector_t size,
  1258. bool default_ro,
  1259. const char *subname)
  1260. {
  1261. char cap_str[10];
  1262. struct mmc_blk_data *part_md;
  1263. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1264. subname);
  1265. if (IS_ERR(part_md))
  1266. return PTR_ERR(part_md);
  1267. part_md->part_type = part_type;
  1268. list_add(&part_md->part, &md->part);
  1269. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1270. cap_str, sizeof(cap_str));
  1271. pr_info("%s: %s %s partition %u %s\n",
  1272. part_md->disk->disk_name, mmc_card_id(card),
  1273. mmc_card_name(card), part_md->part_type, cap_str);
  1274. return 0;
  1275. }
  1276. /* MMC Physical partitions consist of two boot partitions and
  1277. * up to four general purpose partitions.
  1278. * For each partition enabled in EXT_CSD a block device will be allocatedi
  1279. * to provide access to the partition.
  1280. */
  1281. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1282. {
  1283. int idx, ret = 0;
  1284. if (!mmc_card_mmc(card))
  1285. return 0;
  1286. for (idx = 0; idx < card->nr_parts; idx++) {
  1287. if (card->part[idx].size) {
  1288. ret = mmc_blk_alloc_part(card, md,
  1289. card->part[idx].part_cfg,
  1290. card->part[idx].size >> 9,
  1291. card->part[idx].force_ro,
  1292. card->part[idx].name);
  1293. if (ret)
  1294. return ret;
  1295. }
  1296. }
  1297. return ret;
  1298. }
  1299. static int
  1300. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  1301. {
  1302. int err;
  1303. mmc_claim_host(card->host);
  1304. err = mmc_set_blocklen(card, 512);
  1305. mmc_release_host(card->host);
  1306. if (err) {
  1307. pr_err("%s: unable to set block size to 512: %d\n",
  1308. md->disk->disk_name, err);
  1309. return -EINVAL;
  1310. }
  1311. return 0;
  1312. }
  1313. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1314. {
  1315. if (md) {
  1316. if (md->disk->flags & GENHD_FL_UP) {
  1317. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1318. /* Stop new requests from getting into the queue */
  1319. del_gendisk(md->disk);
  1320. }
  1321. /* Then flush out any already in there */
  1322. mmc_cleanup_queue(&md->queue);
  1323. mmc_blk_put(md);
  1324. }
  1325. }
  1326. static void mmc_blk_remove_parts(struct mmc_card *card,
  1327. struct mmc_blk_data *md)
  1328. {
  1329. struct list_head *pos, *q;
  1330. struct mmc_blk_data *part_md;
  1331. __clear_bit(md->name_idx, name_use);
  1332. list_for_each_safe(pos, q, &md->part) {
  1333. part_md = list_entry(pos, struct mmc_blk_data, part);
  1334. list_del(pos);
  1335. mmc_blk_remove_req(part_md);
  1336. }
  1337. }
  1338. static int mmc_add_disk(struct mmc_blk_data *md)
  1339. {
  1340. int ret;
  1341. add_disk(md->disk);
  1342. md->force_ro.show = force_ro_show;
  1343. md->force_ro.store = force_ro_store;
  1344. sysfs_attr_init(&md->force_ro.attr);
  1345. md->force_ro.attr.name = "force_ro";
  1346. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1347. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1348. if (ret)
  1349. del_gendisk(md->disk);
  1350. return ret;
  1351. }
  1352. static const struct mmc_fixup blk_fixups[] =
  1353. {
  1354. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1355. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1356. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1357. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1358. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1359. /*
  1360. * Some MMC cards experience performance degradation with CMD23
  1361. * instead of CMD12-bounded multiblock transfers. For now we'll
  1362. * black list what's bad...
  1363. * - Certain Toshiba cards.
  1364. *
  1365. * N.B. This doesn't affect SD cards.
  1366. */
  1367. MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1368. MMC_QUIRK_BLK_NO_CMD23),
  1369. MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1370. MMC_QUIRK_BLK_NO_CMD23),
  1371. MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
  1372. MMC_QUIRK_BLK_NO_CMD23),
  1373. END_FIXUP
  1374. };
  1375. static int mmc_blk_probe(struct mmc_card *card)
  1376. {
  1377. struct mmc_blk_data *md, *part_md;
  1378. int err;
  1379. char cap_str[10];
  1380. /*
  1381. * Check that the card supports the command class(es) we need.
  1382. */
  1383. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1384. return -ENODEV;
  1385. md = mmc_blk_alloc(card);
  1386. if (IS_ERR(md))
  1387. return PTR_ERR(md);
  1388. err = mmc_blk_set_blksize(md, card);
  1389. if (err)
  1390. goto out;
  1391. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1392. cap_str, sizeof(cap_str));
  1393. pr_info("%s: %s %s %s %s\n",
  1394. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1395. cap_str, md->read_only ? "(ro)" : "");
  1396. if (mmc_blk_alloc_parts(card, md))
  1397. goto out;
  1398. mmc_set_drvdata(card, md);
  1399. mmc_fixup_device(card, blk_fixups);
  1400. if (mmc_add_disk(md))
  1401. goto out;
  1402. list_for_each_entry(part_md, &md->part, part) {
  1403. if (mmc_add_disk(part_md))
  1404. goto out;
  1405. }
  1406. return 0;
  1407. out:
  1408. mmc_blk_remove_parts(card, md);
  1409. mmc_blk_remove_req(md);
  1410. return err;
  1411. }
  1412. static void mmc_blk_remove(struct mmc_card *card)
  1413. {
  1414. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1415. mmc_blk_remove_parts(card, md);
  1416. mmc_claim_host(card->host);
  1417. mmc_blk_part_switch(card, md);
  1418. mmc_release_host(card->host);
  1419. mmc_blk_remove_req(md);
  1420. mmc_set_drvdata(card, NULL);
  1421. }
  1422. #ifdef CONFIG_PM
  1423. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1424. {
  1425. struct mmc_blk_data *part_md;
  1426. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1427. if (md) {
  1428. mmc_queue_suspend(&md->queue);
  1429. list_for_each_entry(part_md, &md->part, part) {
  1430. mmc_queue_suspend(&part_md->queue);
  1431. }
  1432. }
  1433. return 0;
  1434. }
  1435. static int mmc_blk_resume(struct mmc_card *card)
  1436. {
  1437. struct mmc_blk_data *part_md;
  1438. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1439. if (md) {
  1440. mmc_blk_set_blksize(md, card);
  1441. /*
  1442. * Resume involves the card going into idle state,
  1443. * so current partition is always the main one.
  1444. */
  1445. md->part_curr = md->part_type;
  1446. mmc_queue_resume(&md->queue);
  1447. list_for_each_entry(part_md, &md->part, part) {
  1448. mmc_queue_resume(&part_md->queue);
  1449. }
  1450. }
  1451. return 0;
  1452. }
  1453. #else
  1454. #define mmc_blk_suspend NULL
  1455. #define mmc_blk_resume NULL
  1456. #endif
  1457. static struct mmc_driver mmc_driver = {
  1458. .drv = {
  1459. .name = "mmcblk",
  1460. },
  1461. .probe = mmc_blk_probe,
  1462. .remove = mmc_blk_remove,
  1463. .suspend = mmc_blk_suspend,
  1464. .resume = mmc_blk_resume,
  1465. };
  1466. static int __init mmc_blk_init(void)
  1467. {
  1468. int res;
  1469. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1470. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1471. max_devices = 256 / perdev_minors;
  1472. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1473. if (res)
  1474. goto out;
  1475. res = mmc_register_driver(&mmc_driver);
  1476. if (res)
  1477. goto out2;
  1478. return 0;
  1479. out2:
  1480. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1481. out:
  1482. return res;
  1483. }
  1484. static void __exit mmc_blk_exit(void)
  1485. {
  1486. mmc_unregister_driver(&mmc_driver);
  1487. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1488. }
  1489. module_init(mmc_blk_init);
  1490. module_exit(mmc_blk_exit);
  1491. MODULE_LICENSE("GPL");
  1492. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");