block.c 38 KB

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