block.c 46 KB

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