block.c 38 KB

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