block.c 64 KB

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