block.c 46 KB

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