block.c 60 KB

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