block.c 57 KB

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