block.c 49 KB

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