block.c 43 KB

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