block.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  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. (card->host->caps2 & MMC_CAP2_SANITIZE))) {
  346. pr_warn("%s: %s - SANITIZE is not supported\n",
  347. mmc_hostname(card->host), __func__);
  348. err = -EOPNOTSUPP;
  349. goto out;
  350. }
  351. pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
  352. mmc_hostname(card->host), __func__);
  353. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  354. EXT_CSD_SANITIZE_START, 1,
  355. MMC_SANITIZE_REQ_TIMEOUT);
  356. if (err)
  357. pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
  358. mmc_hostname(card->host), __func__, err);
  359. pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
  360. __func__);
  361. out:
  362. return err;
  363. }
  364. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  365. struct mmc_ioc_cmd __user *ic_ptr)
  366. {
  367. struct mmc_blk_ioc_data *idata;
  368. struct mmc_blk_data *md;
  369. struct mmc_card *card;
  370. struct mmc_command cmd = {0};
  371. struct mmc_data data = {0};
  372. struct mmc_request mrq = {NULL};
  373. struct scatterlist sg;
  374. int err;
  375. int is_rpmb = false;
  376. u32 status = 0;
  377. /*
  378. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  379. * whole block device, not on a partition. This prevents overspray
  380. * between sibling partitions.
  381. */
  382. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  383. return -EPERM;
  384. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  385. if (IS_ERR(idata))
  386. return PTR_ERR(idata);
  387. md = mmc_blk_get(bdev->bd_disk);
  388. if (!md) {
  389. err = -EINVAL;
  390. goto cmd_err;
  391. }
  392. if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
  393. is_rpmb = true;
  394. card = md->queue.card;
  395. if (IS_ERR(card)) {
  396. err = PTR_ERR(card);
  397. goto cmd_done;
  398. }
  399. cmd.opcode = idata->ic.opcode;
  400. cmd.arg = idata->ic.arg;
  401. cmd.flags = idata->ic.flags;
  402. if (idata->buf_bytes) {
  403. data.sg = &sg;
  404. data.sg_len = 1;
  405. data.blksz = idata->ic.blksz;
  406. data.blocks = idata->ic.blocks;
  407. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  408. if (idata->ic.write_flag)
  409. data.flags = MMC_DATA_WRITE;
  410. else
  411. data.flags = MMC_DATA_READ;
  412. /* data.flags must already be set before doing this. */
  413. mmc_set_data_timeout(&data, card);
  414. /* Allow overriding the timeout_ns for empirical tuning. */
  415. if (idata->ic.data_timeout_ns)
  416. data.timeout_ns = idata->ic.data_timeout_ns;
  417. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  418. /*
  419. * Pretend this is a data transfer and rely on the
  420. * host driver to compute timeout. When all host
  421. * drivers support cmd.cmd_timeout for R1B, this
  422. * can be changed to:
  423. *
  424. * mrq.data = NULL;
  425. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  426. */
  427. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  428. }
  429. mrq.data = &data;
  430. }
  431. mrq.cmd = &cmd;
  432. mmc_get_card(card);
  433. err = mmc_blk_part_switch(card, md);
  434. if (err)
  435. goto cmd_rel_host;
  436. if (idata->ic.is_acmd) {
  437. err = mmc_app_cmd(card->host, card);
  438. if (err)
  439. goto cmd_rel_host;
  440. }
  441. if (is_rpmb) {
  442. err = mmc_set_blockcount(card, data.blocks,
  443. idata->ic.write_flag & (1 << 31));
  444. if (err)
  445. goto cmd_rel_host;
  446. }
  447. if (MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) {
  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 send_stop(struct mmc_card *card, u32 *status)
  591. {
  592. struct mmc_command cmd = {0};
  593. int err;
  594. cmd.opcode = MMC_STOP_TRANSMISSION;
  595. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  596. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  597. if (err == 0)
  598. *status = cmd.resp[0];
  599. return err;
  600. }
  601. static int get_card_status(struct mmc_card *card, u32 *status, int retries)
  602. {
  603. struct mmc_command cmd = {0};
  604. int err;
  605. cmd.opcode = MMC_SEND_STATUS;
  606. if (!mmc_host_is_spi(card->host))
  607. cmd.arg = card->rca << 16;
  608. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  609. err = mmc_wait_for_cmd(card->host, &cmd, retries);
  610. if (err == 0)
  611. *status = cmd.resp[0];
  612. return err;
  613. }
  614. #define ERR_NOMEDIUM 3
  615. #define ERR_RETRY 2
  616. #define ERR_ABORT 1
  617. #define ERR_CONTINUE 0
  618. static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
  619. bool status_valid, u32 status)
  620. {
  621. switch (error) {
  622. case -EILSEQ:
  623. /* response crc error, retry the r/w cmd */
  624. pr_err("%s: %s sending %s command, card status %#x\n",
  625. req->rq_disk->disk_name, "response CRC error",
  626. name, status);
  627. return ERR_RETRY;
  628. case -ETIMEDOUT:
  629. pr_err("%s: %s sending %s command, card status %#x\n",
  630. req->rq_disk->disk_name, "timed out", name, status);
  631. /* If the status cmd initially failed, retry the r/w cmd */
  632. if (!status_valid)
  633. return ERR_RETRY;
  634. /*
  635. * If it was a r/w cmd crc error, or illegal command
  636. * (eg, issued in wrong state) then retry - we should
  637. * have corrected the state problem above.
  638. */
  639. if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
  640. return ERR_RETRY;
  641. /* Otherwise abort the command */
  642. return ERR_ABORT;
  643. default:
  644. /* We don't understand the error code the driver gave us */
  645. pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
  646. req->rq_disk->disk_name, error, status);
  647. return ERR_ABORT;
  648. }
  649. }
  650. /*
  651. * Initial r/w and stop cmd error recovery.
  652. * We don't know whether the card received the r/w cmd or not, so try to
  653. * restore things back to a sane state. Essentially, we do this as follows:
  654. * - Obtain card status. If the first attempt to obtain card status fails,
  655. * the status word will reflect the failed status cmd, not the failed
  656. * r/w cmd. If we fail to obtain card status, it suggests we can no
  657. * longer communicate with the card.
  658. * - Check the card state. If the card received the cmd but there was a
  659. * transient problem with the response, it might still be in a data transfer
  660. * mode. Try to send it a stop command. If this fails, we can't recover.
  661. * - If the r/w cmd failed due to a response CRC error, it was probably
  662. * transient, so retry the cmd.
  663. * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
  664. * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
  665. * illegal cmd, retry.
  666. * Otherwise we don't understand what happened, so abort.
  667. */
  668. static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
  669. struct mmc_blk_request *brq, int *ecc_err)
  670. {
  671. bool prev_cmd_status_valid = true;
  672. u32 status, stop_status = 0;
  673. int err, retry;
  674. if (mmc_card_removed(card))
  675. return ERR_NOMEDIUM;
  676. /*
  677. * Try to get card status which indicates both the card state
  678. * and why there was no response. If the first attempt fails,
  679. * we can't be sure the returned status is for the r/w command.
  680. */
  681. for (retry = 2; retry >= 0; retry--) {
  682. err = get_card_status(card, &status, 0);
  683. if (!err)
  684. break;
  685. prev_cmd_status_valid = false;
  686. pr_err("%s: error %d sending status command, %sing\n",
  687. req->rq_disk->disk_name, err, retry ? "retry" : "abort");
  688. }
  689. /* We couldn't get a response from the card. Give up. */
  690. if (err) {
  691. /* Check if the card is removed */
  692. if (mmc_detect_card_removed(card->host))
  693. return ERR_NOMEDIUM;
  694. return ERR_ABORT;
  695. }
  696. /* Flag ECC errors */
  697. if ((status & R1_CARD_ECC_FAILED) ||
  698. (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
  699. (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
  700. *ecc_err = 1;
  701. /*
  702. * Check the current card state. If it is in some data transfer
  703. * mode, tell it to stop (and hopefully transition back to TRAN.)
  704. */
  705. if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
  706. R1_CURRENT_STATE(status) == R1_STATE_RCV) {
  707. err = send_stop(card, &stop_status);
  708. if (err)
  709. pr_err("%s: error %d sending stop command\n",
  710. req->rq_disk->disk_name, err);
  711. /*
  712. * If the stop cmd also timed out, the card is probably
  713. * not present, so abort. Other errors are bad news too.
  714. */
  715. if (err)
  716. return ERR_ABORT;
  717. if (stop_status & R1_CARD_ECC_FAILED)
  718. *ecc_err = 1;
  719. }
  720. /* Check for set block count errors */
  721. if (brq->sbc.error)
  722. return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
  723. prev_cmd_status_valid, status);
  724. /* Check for r/w command errors */
  725. if (brq->cmd.error)
  726. return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
  727. prev_cmd_status_valid, status);
  728. /* Data errors */
  729. if (!brq->stop.error)
  730. return ERR_CONTINUE;
  731. /* Now for stop errors. These aren't fatal to the transfer. */
  732. pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
  733. req->rq_disk->disk_name, brq->stop.error,
  734. brq->cmd.resp[0], status);
  735. /*
  736. * Subsitute in our own stop status as this will give the error
  737. * state which happened during the execution of the r/w command.
  738. */
  739. if (stop_status) {
  740. brq->stop.resp[0] = stop_status;
  741. brq->stop.error = 0;
  742. }
  743. return ERR_CONTINUE;
  744. }
  745. static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
  746. int type)
  747. {
  748. int err;
  749. if (md->reset_done & type)
  750. return -EEXIST;
  751. md->reset_done |= type;
  752. err = mmc_hw_reset(host);
  753. /* Ensure we switch back to the correct partition */
  754. if (err != -EOPNOTSUPP) {
  755. struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
  756. int part_err;
  757. main_md->part_curr = main_md->part_type;
  758. part_err = mmc_blk_part_switch(host->card, md);
  759. if (part_err) {
  760. /*
  761. * We have failed to get back into the correct
  762. * partition, so we need to abort the whole request.
  763. */
  764. return -ENODEV;
  765. }
  766. }
  767. return err;
  768. }
  769. static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
  770. {
  771. md->reset_done &= ~type;
  772. }
  773. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  774. {
  775. struct mmc_blk_data *md = mq->data;
  776. struct mmc_card *card = md->queue.card;
  777. unsigned int from, nr, arg;
  778. int err = 0, type = MMC_BLK_DISCARD;
  779. if (!mmc_can_erase(card)) {
  780. err = -EOPNOTSUPP;
  781. goto out;
  782. }
  783. from = blk_rq_pos(req);
  784. nr = blk_rq_sectors(req);
  785. if (mmc_can_discard(card))
  786. arg = MMC_DISCARD_ARG;
  787. else if (mmc_can_trim(card))
  788. arg = MMC_TRIM_ARG;
  789. else
  790. arg = MMC_ERASE_ARG;
  791. retry:
  792. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  793. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  794. INAND_CMD38_ARG_EXT_CSD,
  795. arg == MMC_TRIM_ARG ?
  796. INAND_CMD38_ARG_TRIM :
  797. INAND_CMD38_ARG_ERASE,
  798. 0);
  799. if (err)
  800. goto out;
  801. }
  802. err = mmc_erase(card, from, nr, arg);
  803. out:
  804. if (err == -EIO && !mmc_blk_reset(md, card->host, type))
  805. goto retry;
  806. if (!err)
  807. mmc_blk_reset_success(md, type);
  808. blk_end_request(req, err, blk_rq_bytes(req));
  809. return err ? 0 : 1;
  810. }
  811. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  812. struct request *req)
  813. {
  814. struct mmc_blk_data *md = mq->data;
  815. struct mmc_card *card = md->queue.card;
  816. unsigned int from, nr, arg;
  817. int err = 0, type = MMC_BLK_SECDISCARD;
  818. if (!(mmc_can_secure_erase_trim(card))) {
  819. err = -EOPNOTSUPP;
  820. goto out;
  821. }
  822. from = blk_rq_pos(req);
  823. nr = blk_rq_sectors(req);
  824. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  825. arg = MMC_SECURE_TRIM1_ARG;
  826. else
  827. arg = MMC_SECURE_ERASE_ARG;
  828. retry:
  829. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  830. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  831. INAND_CMD38_ARG_EXT_CSD,
  832. arg == MMC_SECURE_TRIM1_ARG ?
  833. INAND_CMD38_ARG_SECTRIM1 :
  834. INAND_CMD38_ARG_SECERASE,
  835. 0);
  836. if (err)
  837. goto out_retry;
  838. }
  839. err = mmc_erase(card, from, nr, arg);
  840. if (err == -EIO)
  841. goto out_retry;
  842. if (err)
  843. goto out;
  844. if (arg == MMC_SECURE_TRIM1_ARG) {
  845. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  846. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  847. INAND_CMD38_ARG_EXT_CSD,
  848. INAND_CMD38_ARG_SECTRIM2,
  849. 0);
  850. if (err)
  851. goto out_retry;
  852. }
  853. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  854. if (err == -EIO)
  855. goto out_retry;
  856. if (err)
  857. goto out;
  858. }
  859. out_retry:
  860. if (err && !mmc_blk_reset(md, card->host, type))
  861. goto retry;
  862. if (!err)
  863. mmc_blk_reset_success(md, type);
  864. out:
  865. blk_end_request(req, err, blk_rq_bytes(req));
  866. return err ? 0 : 1;
  867. }
  868. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  869. {
  870. struct mmc_blk_data *md = mq->data;
  871. struct mmc_card *card = md->queue.card;
  872. int ret = 0;
  873. ret = mmc_flush_cache(card);
  874. if (ret)
  875. ret = -EIO;
  876. blk_end_request_all(req, ret);
  877. return ret ? 0 : 1;
  878. }
  879. /*
  880. * Reformat current write as a reliable write, supporting
  881. * both legacy and the enhanced reliable write MMC cards.
  882. * In each transfer we'll handle only as much as a single
  883. * reliable write can handle, thus finish the request in
  884. * partial completions.
  885. */
  886. static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
  887. struct mmc_card *card,
  888. struct request *req)
  889. {
  890. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  891. /* Legacy mode imposes restrictions on transfers. */
  892. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  893. brq->data.blocks = 1;
  894. if (brq->data.blocks > card->ext_csd.rel_sectors)
  895. brq->data.blocks = card->ext_csd.rel_sectors;
  896. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  897. brq->data.blocks = 1;
  898. }
  899. }
  900. #define CMD_ERRORS \
  901. (R1_OUT_OF_RANGE | /* Command argument out of range */ \
  902. R1_ADDRESS_ERROR | /* Misaligned address */ \
  903. R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
  904. R1_WP_VIOLATION | /* Tried to write to protected block */ \
  905. R1_CC_ERROR | /* Card controller error */ \
  906. R1_ERROR) /* General/unknown error */
  907. static int mmc_blk_err_check(struct mmc_card *card,
  908. struct mmc_async_req *areq)
  909. {
  910. struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
  911. mmc_active);
  912. struct mmc_blk_request *brq = &mq_mrq->brq;
  913. struct request *req = mq_mrq->req;
  914. int ecc_err = 0;
  915. /*
  916. * sbc.error indicates a problem with the set block count
  917. * command. No data will have been transferred.
  918. *
  919. * cmd.error indicates a problem with the r/w command. No
  920. * data will have been transferred.
  921. *
  922. * stop.error indicates a problem with the stop command. Data
  923. * may have been transferred, or may still be transferring.
  924. */
  925. if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
  926. brq->data.error) {
  927. switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
  928. case ERR_RETRY:
  929. return MMC_BLK_RETRY;
  930. case ERR_ABORT:
  931. return MMC_BLK_ABORT;
  932. case ERR_NOMEDIUM:
  933. return MMC_BLK_NOMEDIUM;
  934. case ERR_CONTINUE:
  935. break;
  936. }
  937. }
  938. /*
  939. * Check for errors relating to the execution of the
  940. * initial command - such as address errors. No data
  941. * has been transferred.
  942. */
  943. if (brq->cmd.resp[0] & CMD_ERRORS) {
  944. pr_err("%s: r/w command failed, status = %#x\n",
  945. req->rq_disk->disk_name, brq->cmd.resp[0]);
  946. return MMC_BLK_ABORT;
  947. }
  948. /*
  949. * Everything else is either success, or a data error of some
  950. * kind. If it was a write, we may have transitioned to
  951. * program mode, which we have to wait for it to complete.
  952. */
  953. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  954. u32 status;
  955. unsigned long timeout;
  956. timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
  957. do {
  958. int err = get_card_status(card, &status, 5);
  959. if (err) {
  960. pr_err("%s: error %d requesting status\n",
  961. req->rq_disk->disk_name, err);
  962. return MMC_BLK_CMD_ERR;
  963. }
  964. /* Timeout if the device never becomes ready for data
  965. * and never leaves the program state.
  966. */
  967. if (time_after(jiffies, timeout)) {
  968. pr_err("%s: Card stuck in programming state!"\
  969. " %s %s\n", mmc_hostname(card->host),
  970. req->rq_disk->disk_name, __func__);
  971. return MMC_BLK_CMD_ERR;
  972. }
  973. /*
  974. * Some cards mishandle the status bits,
  975. * so make sure to check both the busy
  976. * indication and the card state.
  977. */
  978. } while (!(status & R1_READY_FOR_DATA) ||
  979. (R1_CURRENT_STATE(status) == R1_STATE_PRG));
  980. }
  981. if (brq->data.error) {
  982. pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
  983. req->rq_disk->disk_name, brq->data.error,
  984. (unsigned)blk_rq_pos(req),
  985. (unsigned)blk_rq_sectors(req),
  986. brq->cmd.resp[0], brq->stop.resp[0]);
  987. if (rq_data_dir(req) == READ) {
  988. if (ecc_err)
  989. return MMC_BLK_ECC_ERR;
  990. return MMC_BLK_DATA_ERR;
  991. } else {
  992. return MMC_BLK_CMD_ERR;
  993. }
  994. }
  995. if (!brq->data.bytes_xfered)
  996. return MMC_BLK_RETRY;
  997. if (mmc_packed_cmd(mq_mrq->cmd_type)) {
  998. if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
  999. return MMC_BLK_PARTIAL;
  1000. else
  1001. return MMC_BLK_SUCCESS;
  1002. }
  1003. if (blk_rq_bytes(req) != brq->data.bytes_xfered)
  1004. return MMC_BLK_PARTIAL;
  1005. return MMC_BLK_SUCCESS;
  1006. }
  1007. static int mmc_blk_packed_err_check(struct mmc_card *card,
  1008. struct mmc_async_req *areq)
  1009. {
  1010. struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
  1011. mmc_active);
  1012. struct request *req = mq_rq->req;
  1013. struct mmc_packed *packed = mq_rq->packed;
  1014. int err, check, status;
  1015. u8 *ext_csd;
  1016. BUG_ON(!packed);
  1017. packed->retries--;
  1018. check = mmc_blk_err_check(card, areq);
  1019. err = get_card_status(card, &status, 0);
  1020. if (err) {
  1021. pr_err("%s: error %d sending status command\n",
  1022. req->rq_disk->disk_name, err);
  1023. return MMC_BLK_ABORT;
  1024. }
  1025. if (status & R1_EXCEPTION_EVENT) {
  1026. ext_csd = kzalloc(512, GFP_KERNEL);
  1027. if (!ext_csd) {
  1028. pr_err("%s: unable to allocate buffer for ext_csd\n",
  1029. req->rq_disk->disk_name);
  1030. return -ENOMEM;
  1031. }
  1032. err = mmc_send_ext_csd(card, ext_csd);
  1033. if (err) {
  1034. pr_err("%s: error %d sending ext_csd\n",
  1035. req->rq_disk->disk_name, err);
  1036. check = MMC_BLK_ABORT;
  1037. goto free;
  1038. }
  1039. if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
  1040. EXT_CSD_PACKED_FAILURE) &&
  1041. (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1042. EXT_CSD_PACKED_GENERIC_ERROR)) {
  1043. if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
  1044. EXT_CSD_PACKED_INDEXED_ERROR) {
  1045. packed->idx_failure =
  1046. ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
  1047. check = MMC_BLK_PARTIAL;
  1048. }
  1049. pr_err("%s: packed cmd failed, nr %u, sectors %u, "
  1050. "failure index: %d\n",
  1051. req->rq_disk->disk_name, packed->nr_entries,
  1052. packed->blocks, packed->idx_failure);
  1053. }
  1054. free:
  1055. kfree(ext_csd);
  1056. }
  1057. return check;
  1058. }
  1059. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  1060. struct mmc_card *card,
  1061. int disable_multi,
  1062. struct mmc_queue *mq)
  1063. {
  1064. u32 readcmd, writecmd;
  1065. struct mmc_blk_request *brq = &mqrq->brq;
  1066. struct request *req = mqrq->req;
  1067. struct mmc_blk_data *md = mq->data;
  1068. bool do_data_tag;
  1069. /*
  1070. * Reliable writes are used to implement Forced Unit Access and
  1071. * REQ_META accesses, and are supported only on MMCs.
  1072. *
  1073. * XXX: this really needs a good explanation of why REQ_META
  1074. * is treated special.
  1075. */
  1076. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  1077. (req->cmd_flags & REQ_META)) &&
  1078. (rq_data_dir(req) == WRITE) &&
  1079. (md->flags & MMC_BLK_REL_WR);
  1080. memset(brq, 0, sizeof(struct mmc_blk_request));
  1081. brq->mrq.cmd = &brq->cmd;
  1082. brq->mrq.data = &brq->data;
  1083. brq->cmd.arg = blk_rq_pos(req);
  1084. if (!mmc_card_blockaddr(card))
  1085. brq->cmd.arg <<= 9;
  1086. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1087. brq->data.blksz = 512;
  1088. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1089. brq->stop.arg = 0;
  1090. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1091. brq->data.blocks = blk_rq_sectors(req);
  1092. /*
  1093. * The block layer doesn't support all sector count
  1094. * restrictions, so we need to be prepared for too big
  1095. * requests.
  1096. */
  1097. if (brq->data.blocks > card->host->max_blk_count)
  1098. brq->data.blocks = card->host->max_blk_count;
  1099. if (brq->data.blocks > 1) {
  1100. /*
  1101. * After a read error, we redo the request one sector
  1102. * at a time in order to accurately determine which
  1103. * sectors can be read successfully.
  1104. */
  1105. if (disable_multi)
  1106. brq->data.blocks = 1;
  1107. /* Some controllers can't do multiblock reads due to hw bugs */
  1108. if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
  1109. rq_data_dir(req) == READ)
  1110. brq->data.blocks = 1;
  1111. }
  1112. if (brq->data.blocks > 1 || do_rel_wr) {
  1113. /* SPI multiblock writes terminate using a special
  1114. * token, not a STOP_TRANSMISSION request.
  1115. */
  1116. if (!mmc_host_is_spi(card->host) ||
  1117. rq_data_dir(req) == READ)
  1118. brq->mrq.stop = &brq->stop;
  1119. readcmd = MMC_READ_MULTIPLE_BLOCK;
  1120. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  1121. } else {
  1122. brq->mrq.stop = NULL;
  1123. readcmd = MMC_READ_SINGLE_BLOCK;
  1124. writecmd = MMC_WRITE_BLOCK;
  1125. }
  1126. if (rq_data_dir(req) == READ) {
  1127. brq->cmd.opcode = readcmd;
  1128. brq->data.flags |= MMC_DATA_READ;
  1129. } else {
  1130. brq->cmd.opcode = writecmd;
  1131. brq->data.flags |= MMC_DATA_WRITE;
  1132. }
  1133. if (do_rel_wr)
  1134. mmc_apply_rel_rw(brq, card, req);
  1135. /*
  1136. * Data tag is used only during writing meta data to speed
  1137. * up write and any subsequent read of this meta data
  1138. */
  1139. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1140. (req->cmd_flags & REQ_META) &&
  1141. (rq_data_dir(req) == WRITE) &&
  1142. ((brq->data.blocks * brq->data.blksz) >=
  1143. card->ext_csd.data_tag_unit_size);
  1144. /*
  1145. * Pre-defined multi-block transfers are preferable to
  1146. * open ended-ones (and necessary for reliable writes).
  1147. * However, it is not sufficient to just send CMD23,
  1148. * and avoid the final CMD12, as on an error condition
  1149. * CMD12 (stop) needs to be sent anyway. This, coupled
  1150. * with Auto-CMD23 enhancements provided by some
  1151. * hosts, means that the complexity of dealing
  1152. * with this is best left to the host. If CMD23 is
  1153. * supported by card and host, we'll fill sbc in and let
  1154. * the host deal with handling it correctly. This means
  1155. * that for hosts that don't expose MMC_CAP_CMD23, no
  1156. * change of behavior will be observed.
  1157. *
  1158. * N.B: Some MMC cards experience perf degradation.
  1159. * We'll avoid using CMD23-bounded multiblock writes for
  1160. * these, while retaining features like reliable writes.
  1161. */
  1162. if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
  1163. (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
  1164. do_data_tag)) {
  1165. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1166. brq->sbc.arg = brq->data.blocks |
  1167. (do_rel_wr ? (1 << 31) : 0) |
  1168. (do_data_tag ? (1 << 29) : 0);
  1169. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1170. brq->mrq.sbc = &brq->sbc;
  1171. }
  1172. mmc_set_data_timeout(&brq->data, card);
  1173. brq->data.sg = mqrq->sg;
  1174. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1175. /*
  1176. * Adjust the sg list so it is the same size as the
  1177. * request.
  1178. */
  1179. if (brq->data.blocks != blk_rq_sectors(req)) {
  1180. int i, data_size = brq->data.blocks << 9;
  1181. struct scatterlist *sg;
  1182. for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
  1183. data_size -= sg->length;
  1184. if (data_size <= 0) {
  1185. sg->length += data_size;
  1186. i++;
  1187. break;
  1188. }
  1189. }
  1190. brq->data.sg_len = i;
  1191. }
  1192. mqrq->mmc_active.mrq = &brq->mrq;
  1193. mqrq->mmc_active.err_check = mmc_blk_err_check;
  1194. mmc_queue_bounce_pre(mqrq);
  1195. }
  1196. static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
  1197. struct mmc_card *card)
  1198. {
  1199. unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
  1200. unsigned int max_seg_sz = queue_max_segment_size(q);
  1201. unsigned int len, nr_segs = 0;
  1202. do {
  1203. len = min(hdr_sz, max_seg_sz);
  1204. hdr_sz -= len;
  1205. nr_segs++;
  1206. } while (hdr_sz);
  1207. return nr_segs;
  1208. }
  1209. static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
  1210. {
  1211. struct request_queue *q = mq->queue;
  1212. struct mmc_card *card = mq->card;
  1213. struct request *cur = req, *next = NULL;
  1214. struct mmc_blk_data *md = mq->data;
  1215. struct mmc_queue_req *mqrq = mq->mqrq_cur;
  1216. bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
  1217. unsigned int req_sectors = 0, phys_segments = 0;
  1218. unsigned int max_blk_count, max_phys_segs;
  1219. bool put_back = true;
  1220. u8 max_packed_rw = 0;
  1221. u8 reqs = 0;
  1222. if (!(md->flags & MMC_BLK_PACKED_CMD))
  1223. goto no_packed;
  1224. if ((rq_data_dir(cur) == WRITE) &&
  1225. mmc_host_packed_wr(card->host))
  1226. max_packed_rw = card->ext_csd.max_packed_writes;
  1227. if (max_packed_rw == 0)
  1228. goto no_packed;
  1229. if (mmc_req_rel_wr(cur) &&
  1230. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1231. goto no_packed;
  1232. if (mmc_large_sector(card) &&
  1233. !IS_ALIGNED(blk_rq_sectors(cur), 8))
  1234. goto no_packed;
  1235. mmc_blk_clear_packed(mqrq);
  1236. max_blk_count = min(card->host->max_blk_count,
  1237. card->host->max_req_size >> 9);
  1238. if (unlikely(max_blk_count > 0xffff))
  1239. max_blk_count = 0xffff;
  1240. max_phys_segs = queue_max_segments(q);
  1241. req_sectors += blk_rq_sectors(cur);
  1242. phys_segments += cur->nr_phys_segments;
  1243. if (rq_data_dir(cur) == WRITE) {
  1244. req_sectors += mmc_large_sector(card) ? 8 : 1;
  1245. phys_segments += mmc_calc_packed_hdr_segs(q, card);
  1246. }
  1247. do {
  1248. if (reqs >= max_packed_rw - 1) {
  1249. put_back = false;
  1250. break;
  1251. }
  1252. spin_lock_irq(q->queue_lock);
  1253. next = blk_fetch_request(q);
  1254. spin_unlock_irq(q->queue_lock);
  1255. if (!next) {
  1256. put_back = false;
  1257. break;
  1258. }
  1259. if (mmc_large_sector(card) &&
  1260. !IS_ALIGNED(blk_rq_sectors(next), 8))
  1261. break;
  1262. if (next->cmd_flags & REQ_DISCARD ||
  1263. next->cmd_flags & REQ_FLUSH)
  1264. break;
  1265. if (rq_data_dir(cur) != rq_data_dir(next))
  1266. break;
  1267. if (mmc_req_rel_wr(next) &&
  1268. (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
  1269. break;
  1270. req_sectors += blk_rq_sectors(next);
  1271. if (req_sectors > max_blk_count)
  1272. break;
  1273. phys_segments += next->nr_phys_segments;
  1274. if (phys_segments > max_phys_segs)
  1275. break;
  1276. list_add_tail(&next->queuelist, &mqrq->packed->list);
  1277. cur = next;
  1278. reqs++;
  1279. } while (1);
  1280. if (put_back) {
  1281. spin_lock_irq(q->queue_lock);
  1282. blk_requeue_request(q, next);
  1283. spin_unlock_irq(q->queue_lock);
  1284. }
  1285. if (reqs > 0) {
  1286. list_add(&req->queuelist, &mqrq->packed->list);
  1287. mqrq->packed->nr_entries = ++reqs;
  1288. mqrq->packed->retries = reqs;
  1289. return reqs;
  1290. }
  1291. no_packed:
  1292. mqrq->cmd_type = MMC_PACKED_NONE;
  1293. return 0;
  1294. }
  1295. static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
  1296. struct mmc_card *card,
  1297. struct mmc_queue *mq)
  1298. {
  1299. struct mmc_blk_request *brq = &mqrq->brq;
  1300. struct request *req = mqrq->req;
  1301. struct request *prq;
  1302. struct mmc_blk_data *md = mq->data;
  1303. struct mmc_packed *packed = mqrq->packed;
  1304. bool do_rel_wr, do_data_tag;
  1305. u32 *packed_cmd_hdr;
  1306. u8 hdr_blocks;
  1307. u8 i = 1;
  1308. BUG_ON(!packed);
  1309. mqrq->cmd_type = MMC_PACKED_WRITE;
  1310. packed->blocks = 0;
  1311. packed->idx_failure = MMC_PACKED_NR_IDX;
  1312. packed_cmd_hdr = packed->cmd_hdr;
  1313. memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
  1314. packed_cmd_hdr[0] = (packed->nr_entries << 16) |
  1315. (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
  1316. hdr_blocks = mmc_large_sector(card) ? 8 : 1;
  1317. /*
  1318. * Argument for each entry of packed group
  1319. */
  1320. list_for_each_entry(prq, &packed->list, queuelist) {
  1321. do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
  1322. do_data_tag = (card->ext_csd.data_tag_unit_size) &&
  1323. (prq->cmd_flags & REQ_META) &&
  1324. (rq_data_dir(prq) == WRITE) &&
  1325. ((brq->data.blocks * brq->data.blksz) >=
  1326. card->ext_csd.data_tag_unit_size);
  1327. /* Argument of CMD23 */
  1328. packed_cmd_hdr[(i * 2)] =
  1329. (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
  1330. (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
  1331. blk_rq_sectors(prq);
  1332. /* Argument of CMD18 or CMD25 */
  1333. packed_cmd_hdr[((i * 2)) + 1] =
  1334. mmc_card_blockaddr(card) ?
  1335. blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
  1336. packed->blocks += blk_rq_sectors(prq);
  1337. i++;
  1338. }
  1339. memset(brq, 0, sizeof(struct mmc_blk_request));
  1340. brq->mrq.cmd = &brq->cmd;
  1341. brq->mrq.data = &brq->data;
  1342. brq->mrq.sbc = &brq->sbc;
  1343. brq->mrq.stop = &brq->stop;
  1344. brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
  1345. brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
  1346. brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1347. brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
  1348. brq->cmd.arg = blk_rq_pos(req);
  1349. if (!mmc_card_blockaddr(card))
  1350. brq->cmd.arg <<= 9;
  1351. brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  1352. brq->data.blksz = 512;
  1353. brq->data.blocks = packed->blocks + hdr_blocks;
  1354. brq->data.flags |= MMC_DATA_WRITE;
  1355. brq->stop.opcode = MMC_STOP_TRANSMISSION;
  1356. brq->stop.arg = 0;
  1357. brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1358. mmc_set_data_timeout(&brq->data, card);
  1359. brq->data.sg = mqrq->sg;
  1360. brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
  1361. mqrq->mmc_active.mrq = &brq->mrq;
  1362. mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
  1363. mmc_queue_bounce_pre(mqrq);
  1364. }
  1365. static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
  1366. struct mmc_blk_request *brq, struct request *req,
  1367. int ret)
  1368. {
  1369. struct mmc_queue_req *mq_rq;
  1370. mq_rq = container_of(brq, struct mmc_queue_req, brq);
  1371. /*
  1372. * If this is an SD card and we're writing, we can first
  1373. * mark the known good sectors as ok.
  1374. *
  1375. * If the card is not SD, we can still ok written sectors
  1376. * as reported by the controller (which might be less than
  1377. * the real number of written sectors, but never more).
  1378. */
  1379. if (mmc_card_sd(card)) {
  1380. u32 blocks;
  1381. blocks = mmc_sd_num_wr_blocks(card);
  1382. if (blocks != (u32)-1) {
  1383. ret = blk_end_request(req, 0, blocks << 9);
  1384. }
  1385. } else {
  1386. if (!mmc_packed_cmd(mq_rq->cmd_type))
  1387. ret = blk_end_request(req, 0, brq->data.bytes_xfered);
  1388. }
  1389. return ret;
  1390. }
  1391. static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
  1392. {
  1393. struct request *prq;
  1394. struct mmc_packed *packed = mq_rq->packed;
  1395. int idx = packed->idx_failure, i = 0;
  1396. int ret = 0;
  1397. BUG_ON(!packed);
  1398. while (!list_empty(&packed->list)) {
  1399. prq = list_entry_rq(packed->list.next);
  1400. if (idx == i) {
  1401. /* retry from error index */
  1402. packed->nr_entries -= idx;
  1403. mq_rq->req = prq;
  1404. ret = 1;
  1405. if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
  1406. list_del_init(&prq->queuelist);
  1407. mmc_blk_clear_packed(mq_rq);
  1408. }
  1409. return ret;
  1410. }
  1411. list_del_init(&prq->queuelist);
  1412. blk_end_request(prq, 0, blk_rq_bytes(prq));
  1413. i++;
  1414. }
  1415. mmc_blk_clear_packed(mq_rq);
  1416. return ret;
  1417. }
  1418. static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
  1419. {
  1420. struct request *prq;
  1421. struct mmc_packed *packed = mq_rq->packed;
  1422. BUG_ON(!packed);
  1423. while (!list_empty(&packed->list)) {
  1424. prq = list_entry_rq(packed->list.next);
  1425. list_del_init(&prq->queuelist);
  1426. blk_end_request(prq, -EIO, blk_rq_bytes(prq));
  1427. }
  1428. mmc_blk_clear_packed(mq_rq);
  1429. }
  1430. static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
  1431. struct mmc_queue_req *mq_rq)
  1432. {
  1433. struct request *prq;
  1434. struct request_queue *q = mq->queue;
  1435. struct mmc_packed *packed = mq_rq->packed;
  1436. BUG_ON(!packed);
  1437. while (!list_empty(&packed->list)) {
  1438. prq = list_entry_rq(packed->list.prev);
  1439. if (prq->queuelist.prev != &packed->list) {
  1440. list_del_init(&prq->queuelist);
  1441. spin_lock_irq(q->queue_lock);
  1442. blk_requeue_request(mq->queue, prq);
  1443. spin_unlock_irq(q->queue_lock);
  1444. } else {
  1445. list_del_init(&prq->queuelist);
  1446. }
  1447. }
  1448. mmc_blk_clear_packed(mq_rq);
  1449. }
  1450. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
  1451. {
  1452. struct mmc_blk_data *md = mq->data;
  1453. struct mmc_card *card = md->queue.card;
  1454. struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
  1455. int ret = 1, disable_multi = 0, retry = 0, type;
  1456. enum mmc_blk_status status;
  1457. struct mmc_queue_req *mq_rq;
  1458. struct request *req = rqc;
  1459. struct mmc_async_req *areq;
  1460. const u8 packed_nr = 2;
  1461. u8 reqs = 0;
  1462. if (!rqc && !mq->mqrq_prev->req)
  1463. return 0;
  1464. if (rqc)
  1465. reqs = mmc_blk_prep_packed_list(mq, rqc);
  1466. do {
  1467. if (rqc) {
  1468. /*
  1469. * When 4KB native sector is enabled, only 8 blocks
  1470. * multiple read or write is allowed
  1471. */
  1472. if ((brq->data.blocks & 0x07) &&
  1473. (card->ext_csd.data_sector_size == 4096)) {
  1474. pr_err("%s: Transfer size is not 4KB sector size aligned\n",
  1475. req->rq_disk->disk_name);
  1476. mq_rq = mq->mqrq_cur;
  1477. goto cmd_abort;
  1478. }
  1479. if (reqs >= packed_nr)
  1480. mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
  1481. card, mq);
  1482. else
  1483. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1484. areq = &mq->mqrq_cur->mmc_active;
  1485. } else
  1486. areq = NULL;
  1487. areq = mmc_start_req(card->host, areq, (int *) &status);
  1488. if (!areq) {
  1489. if (status == MMC_BLK_NEW_REQUEST)
  1490. mq->flags |= MMC_QUEUE_NEW_REQUEST;
  1491. return 0;
  1492. }
  1493. mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
  1494. brq = &mq_rq->brq;
  1495. req = mq_rq->req;
  1496. type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
  1497. mmc_queue_bounce_post(mq_rq);
  1498. switch (status) {
  1499. case MMC_BLK_SUCCESS:
  1500. case MMC_BLK_PARTIAL:
  1501. /*
  1502. * A block was successfully transferred.
  1503. */
  1504. mmc_blk_reset_success(md, type);
  1505. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1506. ret = mmc_blk_end_packed_req(mq_rq);
  1507. break;
  1508. } else {
  1509. ret = blk_end_request(req, 0,
  1510. brq->data.bytes_xfered);
  1511. }
  1512. /*
  1513. * If the blk_end_request function returns non-zero even
  1514. * though all data has been transferred and no errors
  1515. * were returned by the host controller, it's a bug.
  1516. */
  1517. if (status == MMC_BLK_SUCCESS && ret) {
  1518. pr_err("%s BUG rq_tot %d d_xfer %d\n",
  1519. __func__, blk_rq_bytes(req),
  1520. brq->data.bytes_xfered);
  1521. rqc = NULL;
  1522. goto cmd_abort;
  1523. }
  1524. break;
  1525. case MMC_BLK_CMD_ERR:
  1526. ret = mmc_blk_cmd_err(md, card, brq, req, ret);
  1527. if (!mmc_blk_reset(md, card->host, type))
  1528. break;
  1529. goto cmd_abort;
  1530. case MMC_BLK_RETRY:
  1531. if (retry++ < 5)
  1532. break;
  1533. /* Fall through */
  1534. case MMC_BLK_ABORT:
  1535. if (!mmc_blk_reset(md, card->host, type))
  1536. break;
  1537. goto cmd_abort;
  1538. case MMC_BLK_DATA_ERR: {
  1539. int err;
  1540. err = mmc_blk_reset(md, card->host, type);
  1541. if (!err)
  1542. break;
  1543. if (err == -ENODEV ||
  1544. mmc_packed_cmd(mq_rq->cmd_type))
  1545. goto cmd_abort;
  1546. /* Fall through */
  1547. }
  1548. case MMC_BLK_ECC_ERR:
  1549. if (brq->data.blocks > 1) {
  1550. /* Redo read one sector at a time */
  1551. pr_warning("%s: retrying using single block read\n",
  1552. req->rq_disk->disk_name);
  1553. disable_multi = 1;
  1554. break;
  1555. }
  1556. /*
  1557. * After an error, we redo I/O one sector at a
  1558. * time, so we only reach here after trying to
  1559. * read a single sector.
  1560. */
  1561. ret = blk_end_request(req, -EIO,
  1562. brq->data.blksz);
  1563. if (!ret)
  1564. goto start_new_req;
  1565. break;
  1566. case MMC_BLK_NOMEDIUM:
  1567. goto cmd_abort;
  1568. default:
  1569. pr_err("%s: Unhandled return value (%d)",
  1570. req->rq_disk->disk_name, status);
  1571. goto cmd_abort;
  1572. }
  1573. if (ret) {
  1574. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1575. if (!mq_rq->packed->retries)
  1576. goto cmd_abort;
  1577. mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
  1578. mmc_start_req(card->host,
  1579. &mq_rq->mmc_active, NULL);
  1580. } else {
  1581. /*
  1582. * In case of a incomplete request
  1583. * prepare it again and resend.
  1584. */
  1585. mmc_blk_rw_rq_prep(mq_rq, card,
  1586. disable_multi, mq);
  1587. mmc_start_req(card->host,
  1588. &mq_rq->mmc_active, NULL);
  1589. }
  1590. }
  1591. } while (ret);
  1592. return 1;
  1593. cmd_abort:
  1594. if (mmc_packed_cmd(mq_rq->cmd_type)) {
  1595. mmc_blk_abort_packed_req(mq_rq);
  1596. } else {
  1597. if (mmc_card_removed(card))
  1598. req->cmd_flags |= REQ_QUIET;
  1599. while (ret)
  1600. ret = blk_end_request(req, -EIO,
  1601. blk_rq_cur_bytes(req));
  1602. }
  1603. start_new_req:
  1604. if (rqc) {
  1605. if (mmc_card_removed(card)) {
  1606. rqc->cmd_flags |= REQ_QUIET;
  1607. blk_end_request_all(rqc, -EIO);
  1608. } else {
  1609. /*
  1610. * If current request is packed, it needs to put back.
  1611. */
  1612. if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
  1613. mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
  1614. mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
  1615. mmc_start_req(card->host,
  1616. &mq->mqrq_cur->mmc_active, NULL);
  1617. }
  1618. }
  1619. return 0;
  1620. }
  1621. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  1622. {
  1623. int ret;
  1624. struct mmc_blk_data *md = mq->data;
  1625. struct mmc_card *card = md->queue.card;
  1626. struct mmc_host *host = card->host;
  1627. unsigned long flags;
  1628. if (req && !mq->mqrq_prev->req)
  1629. /* claim host only for the first request */
  1630. mmc_get_card(card);
  1631. ret = mmc_blk_part_switch(card, md);
  1632. if (ret) {
  1633. if (req) {
  1634. blk_end_request_all(req, -EIO);
  1635. }
  1636. ret = 0;
  1637. goto out;
  1638. }
  1639. mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
  1640. if (req && req->cmd_flags & REQ_DISCARD) {
  1641. /* complete ongoing async transfer before issuing discard */
  1642. if (card->host->areq)
  1643. mmc_blk_issue_rw_rq(mq, NULL);
  1644. if (req->cmd_flags & REQ_SECURE &&
  1645. !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
  1646. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  1647. else
  1648. ret = mmc_blk_issue_discard_rq(mq, req);
  1649. } else if (req && req->cmd_flags & REQ_FLUSH) {
  1650. /* complete ongoing async transfer before issuing flush */
  1651. if (card->host->areq)
  1652. mmc_blk_issue_rw_rq(mq, NULL);
  1653. ret = mmc_blk_issue_flush(mq, req);
  1654. } else {
  1655. if (!req && host->areq) {
  1656. spin_lock_irqsave(&host->context_info.lock, flags);
  1657. host->context_info.is_waiting_last_req = true;
  1658. spin_unlock_irqrestore(&host->context_info.lock, flags);
  1659. }
  1660. ret = mmc_blk_issue_rw_rq(mq, req);
  1661. }
  1662. out:
  1663. if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
  1664. (req && (req->cmd_flags & MMC_REQ_SPECIAL_MASK)))
  1665. /*
  1666. * Release host when there are no more requests
  1667. * and after special request(discard, flush) is done.
  1668. * In case sepecial request, there is no reentry to
  1669. * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
  1670. */
  1671. mmc_put_card(card);
  1672. return ret;
  1673. }
  1674. static inline int mmc_blk_readonly(struct mmc_card *card)
  1675. {
  1676. return mmc_card_readonly(card) ||
  1677. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  1678. }
  1679. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  1680. struct device *parent,
  1681. sector_t size,
  1682. bool default_ro,
  1683. const char *subname,
  1684. int area_type)
  1685. {
  1686. struct mmc_blk_data *md;
  1687. int devidx, ret;
  1688. devidx = find_first_zero_bit(dev_use, max_devices);
  1689. if (devidx >= max_devices)
  1690. return ERR_PTR(-ENOSPC);
  1691. __set_bit(devidx, dev_use);
  1692. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  1693. if (!md) {
  1694. ret = -ENOMEM;
  1695. goto out;
  1696. }
  1697. /*
  1698. * !subname implies we are creating main mmc_blk_data that will be
  1699. * associated with mmc_card with mmc_set_drvdata. Due to device
  1700. * partitions, devidx will not coincide with a per-physical card
  1701. * index anymore so we keep track of a name index.
  1702. */
  1703. if (!subname) {
  1704. md->name_idx = find_first_zero_bit(name_use, max_devices);
  1705. __set_bit(md->name_idx, name_use);
  1706. } else
  1707. md->name_idx = ((struct mmc_blk_data *)
  1708. dev_to_disk(parent)->private_data)->name_idx;
  1709. md->area_type = area_type;
  1710. /*
  1711. * Set the read-only status based on the supported commands
  1712. * and the write protect switch.
  1713. */
  1714. md->read_only = mmc_blk_readonly(card);
  1715. md->disk = alloc_disk(perdev_minors);
  1716. if (md->disk == NULL) {
  1717. ret = -ENOMEM;
  1718. goto err_kfree;
  1719. }
  1720. spin_lock_init(&md->lock);
  1721. INIT_LIST_HEAD(&md->part);
  1722. md->usage = 1;
  1723. ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
  1724. if (ret)
  1725. goto err_putdisk;
  1726. md->queue.issue_fn = mmc_blk_issue_rq;
  1727. md->queue.data = md;
  1728. md->disk->major = MMC_BLOCK_MAJOR;
  1729. md->disk->first_minor = devidx * perdev_minors;
  1730. md->disk->fops = &mmc_bdops;
  1731. md->disk->private_data = md;
  1732. md->disk->queue = md->queue.queue;
  1733. md->disk->driverfs_dev = parent;
  1734. set_disk_ro(md->disk, md->read_only || default_ro);
  1735. if (area_type & MMC_BLK_DATA_AREA_RPMB)
  1736. md->disk->flags |= GENHD_FL_NO_PART_SCAN;
  1737. /*
  1738. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  1739. *
  1740. * - be set for removable media with permanent block devices
  1741. * - be unset for removable block devices with permanent media
  1742. *
  1743. * Since MMC block devices clearly fall under the second
  1744. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  1745. * should use the block device creation/destruction hotplug
  1746. * messages to tell when the card is present.
  1747. */
  1748. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  1749. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  1750. if (mmc_card_mmc(card))
  1751. blk_queue_logical_block_size(md->queue.queue,
  1752. card->ext_csd.data_sector_size);
  1753. else
  1754. blk_queue_logical_block_size(md->queue.queue, 512);
  1755. set_capacity(md->disk, size);
  1756. if (mmc_host_cmd23(card->host)) {
  1757. if (mmc_card_mmc(card) ||
  1758. (mmc_card_sd(card) &&
  1759. card->scr.cmds & SD_SCR_CMD23_SUPPORT))
  1760. md->flags |= MMC_BLK_CMD23;
  1761. }
  1762. if (mmc_card_mmc(card) &&
  1763. md->flags & MMC_BLK_CMD23 &&
  1764. ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
  1765. card->ext_csd.rel_sectors)) {
  1766. md->flags |= MMC_BLK_REL_WR;
  1767. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  1768. }
  1769. if (mmc_card_mmc(card) &&
  1770. (area_type == MMC_BLK_DATA_AREA_MAIN) &&
  1771. (md->flags & MMC_BLK_CMD23) &&
  1772. card->ext_csd.packed_event_en) {
  1773. if (!mmc_packed_init(&md->queue, card))
  1774. md->flags |= MMC_BLK_PACKED_CMD;
  1775. }
  1776. return md;
  1777. err_putdisk:
  1778. put_disk(md->disk);
  1779. err_kfree:
  1780. kfree(md);
  1781. out:
  1782. return ERR_PTR(ret);
  1783. }
  1784. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  1785. {
  1786. sector_t size;
  1787. struct mmc_blk_data *md;
  1788. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  1789. /*
  1790. * The EXT_CSD sector count is in number or 512 byte
  1791. * sectors.
  1792. */
  1793. size = card->ext_csd.sectors;
  1794. } else {
  1795. /*
  1796. * The CSD capacity field is in units of read_blkbits.
  1797. * set_capacity takes units of 512 bytes.
  1798. */
  1799. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  1800. }
  1801. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
  1802. MMC_BLK_DATA_AREA_MAIN);
  1803. return md;
  1804. }
  1805. static int mmc_blk_alloc_part(struct mmc_card *card,
  1806. struct mmc_blk_data *md,
  1807. unsigned int part_type,
  1808. sector_t size,
  1809. bool default_ro,
  1810. const char *subname,
  1811. int area_type)
  1812. {
  1813. char cap_str[10];
  1814. struct mmc_blk_data *part_md;
  1815. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  1816. subname, area_type);
  1817. if (IS_ERR(part_md))
  1818. return PTR_ERR(part_md);
  1819. part_md->part_type = part_type;
  1820. list_add(&part_md->part, &md->part);
  1821. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  1822. cap_str, sizeof(cap_str));
  1823. pr_info("%s: %s %s partition %u %s\n",
  1824. part_md->disk->disk_name, mmc_card_id(card),
  1825. mmc_card_name(card), part_md->part_type, cap_str);
  1826. return 0;
  1827. }
  1828. /* MMC Physical partitions consist of two boot partitions and
  1829. * up to four general purpose partitions.
  1830. * For each partition enabled in EXT_CSD a block device will be allocatedi
  1831. * to provide access to the partition.
  1832. */
  1833. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  1834. {
  1835. int idx, ret = 0;
  1836. if (!mmc_card_mmc(card))
  1837. return 0;
  1838. for (idx = 0; idx < card->nr_parts; idx++) {
  1839. if (card->part[idx].size) {
  1840. ret = mmc_blk_alloc_part(card, md,
  1841. card->part[idx].part_cfg,
  1842. card->part[idx].size >> 9,
  1843. card->part[idx].force_ro,
  1844. card->part[idx].name,
  1845. card->part[idx].area_type);
  1846. if (ret)
  1847. return ret;
  1848. }
  1849. }
  1850. return ret;
  1851. }
  1852. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  1853. {
  1854. struct mmc_card *card;
  1855. if (md) {
  1856. card = md->queue.card;
  1857. if (md->disk->flags & GENHD_FL_UP) {
  1858. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1859. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1860. card->ext_csd.boot_ro_lockable)
  1861. device_remove_file(disk_to_dev(md->disk),
  1862. &md->power_ro_lock);
  1863. /* Stop new requests from getting into the queue */
  1864. del_gendisk(md->disk);
  1865. }
  1866. /* Then flush out any already in there */
  1867. mmc_cleanup_queue(&md->queue);
  1868. if (md->flags & MMC_BLK_PACKED_CMD)
  1869. mmc_packed_clean(&md->queue);
  1870. mmc_blk_put(md);
  1871. }
  1872. }
  1873. static void mmc_blk_remove_parts(struct mmc_card *card,
  1874. struct mmc_blk_data *md)
  1875. {
  1876. struct list_head *pos, *q;
  1877. struct mmc_blk_data *part_md;
  1878. __clear_bit(md->name_idx, name_use);
  1879. list_for_each_safe(pos, q, &md->part) {
  1880. part_md = list_entry(pos, struct mmc_blk_data, part);
  1881. list_del(pos);
  1882. mmc_blk_remove_req(part_md);
  1883. }
  1884. }
  1885. static int mmc_add_disk(struct mmc_blk_data *md)
  1886. {
  1887. int ret;
  1888. struct mmc_card *card = md->queue.card;
  1889. add_disk(md->disk);
  1890. md->force_ro.show = force_ro_show;
  1891. md->force_ro.store = force_ro_store;
  1892. sysfs_attr_init(&md->force_ro.attr);
  1893. md->force_ro.attr.name = "force_ro";
  1894. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  1895. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  1896. if (ret)
  1897. goto force_ro_fail;
  1898. if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
  1899. card->ext_csd.boot_ro_lockable) {
  1900. umode_t mode;
  1901. if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
  1902. mode = S_IRUGO;
  1903. else
  1904. mode = S_IRUGO | S_IWUSR;
  1905. md->power_ro_lock.show = power_ro_lock_show;
  1906. md->power_ro_lock.store = power_ro_lock_store;
  1907. sysfs_attr_init(&md->power_ro_lock.attr);
  1908. md->power_ro_lock.attr.mode = mode;
  1909. md->power_ro_lock.attr.name =
  1910. "ro_lock_until_next_power_on";
  1911. ret = device_create_file(disk_to_dev(md->disk),
  1912. &md->power_ro_lock);
  1913. if (ret)
  1914. goto power_ro_lock_fail;
  1915. }
  1916. return ret;
  1917. power_ro_lock_fail:
  1918. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  1919. force_ro_fail:
  1920. del_gendisk(md->disk);
  1921. return ret;
  1922. }
  1923. #define CID_MANFID_SANDISK 0x2
  1924. #define CID_MANFID_TOSHIBA 0x11
  1925. #define CID_MANFID_MICRON 0x13
  1926. #define CID_MANFID_SAMSUNG 0x15
  1927. static const struct mmc_fixup blk_fixups[] =
  1928. {
  1929. MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1930. MMC_QUIRK_INAND_CMD38),
  1931. MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1932. MMC_QUIRK_INAND_CMD38),
  1933. MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1934. MMC_QUIRK_INAND_CMD38),
  1935. MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1936. MMC_QUIRK_INAND_CMD38),
  1937. MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
  1938. MMC_QUIRK_INAND_CMD38),
  1939. /*
  1940. * Some MMC cards experience performance degradation with CMD23
  1941. * instead of CMD12-bounded multiblock transfers. For now we'll
  1942. * black list what's bad...
  1943. * - Certain Toshiba cards.
  1944. *
  1945. * N.B. This doesn't affect SD cards.
  1946. */
  1947. MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1948. MMC_QUIRK_BLK_NO_CMD23),
  1949. MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1950. MMC_QUIRK_BLK_NO_CMD23),
  1951. MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
  1952. MMC_QUIRK_BLK_NO_CMD23),
  1953. /*
  1954. * Some Micron MMC cards needs longer data read timeout than
  1955. * indicated in CSD.
  1956. */
  1957. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
  1958. MMC_QUIRK_LONG_READ_TIME),
  1959. /*
  1960. * On these Samsung MoviNAND parts, performing secure erase or
  1961. * secure trim can result in unrecoverable corruption due to a
  1962. * firmware bug.
  1963. */
  1964. MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1965. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1966. MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1967. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1968. MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1969. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1970. MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1971. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1972. MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1973. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1974. MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1975. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1976. MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1977. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1978. MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
  1979. MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
  1980. END_FIXUP
  1981. };
  1982. static int mmc_blk_probe(struct mmc_card *card)
  1983. {
  1984. struct mmc_blk_data *md, *part_md;
  1985. char cap_str[10];
  1986. /*
  1987. * Check that the card supports the command class(es) we need.
  1988. */
  1989. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1990. return -ENODEV;
  1991. md = mmc_blk_alloc(card);
  1992. if (IS_ERR(md))
  1993. return PTR_ERR(md);
  1994. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1995. cap_str, sizeof(cap_str));
  1996. pr_info("%s: %s %s %s %s\n",
  1997. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1998. cap_str, md->read_only ? "(ro)" : "");
  1999. if (mmc_blk_alloc_parts(card, md))
  2000. goto out;
  2001. mmc_set_drvdata(card, md);
  2002. mmc_fixup_device(card, blk_fixups);
  2003. if (mmc_add_disk(md))
  2004. goto out;
  2005. list_for_each_entry(part_md, &md->part, part) {
  2006. if (mmc_add_disk(part_md))
  2007. goto out;
  2008. }
  2009. pm_runtime_set_autosuspend_delay(&card->dev, 3000);
  2010. pm_runtime_use_autosuspend(&card->dev);
  2011. /*
  2012. * Don't enable runtime PM for SD-combo cards here. Leave that
  2013. * decision to be taken during the SDIO init sequence instead.
  2014. */
  2015. if (card->type != MMC_TYPE_SD_COMBO) {
  2016. pm_runtime_set_active(&card->dev);
  2017. pm_runtime_enable(&card->dev);
  2018. }
  2019. return 0;
  2020. out:
  2021. mmc_blk_remove_parts(card, md);
  2022. mmc_blk_remove_req(md);
  2023. return 0;
  2024. }
  2025. static void mmc_blk_remove(struct mmc_card *card)
  2026. {
  2027. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2028. mmc_blk_remove_parts(card, md);
  2029. pm_runtime_get_sync(&card->dev);
  2030. mmc_claim_host(card->host);
  2031. mmc_blk_part_switch(card, md);
  2032. mmc_release_host(card->host);
  2033. if (card->type != MMC_TYPE_SD_COMBO)
  2034. pm_runtime_disable(&card->dev);
  2035. pm_runtime_put_noidle(&card->dev);
  2036. mmc_blk_remove_req(md);
  2037. mmc_set_drvdata(card, NULL);
  2038. }
  2039. #ifdef CONFIG_PM
  2040. static int mmc_blk_suspend(struct mmc_card *card)
  2041. {
  2042. struct mmc_blk_data *part_md;
  2043. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2044. if (md) {
  2045. pm_runtime_get_sync(&card->dev);
  2046. mmc_queue_suspend(&md->queue);
  2047. list_for_each_entry(part_md, &md->part, part) {
  2048. mmc_queue_suspend(&part_md->queue);
  2049. }
  2050. }
  2051. return 0;
  2052. }
  2053. static int mmc_blk_resume(struct mmc_card *card)
  2054. {
  2055. struct mmc_blk_data *part_md;
  2056. struct mmc_blk_data *md = mmc_get_drvdata(card);
  2057. if (md) {
  2058. /*
  2059. * Resume involves the card going into idle state,
  2060. * so current partition is always the main one.
  2061. */
  2062. md->part_curr = md->part_type;
  2063. mmc_queue_resume(&md->queue);
  2064. list_for_each_entry(part_md, &md->part, part) {
  2065. mmc_queue_resume(&part_md->queue);
  2066. }
  2067. pm_runtime_put(&card->dev);
  2068. }
  2069. return 0;
  2070. }
  2071. #else
  2072. #define mmc_blk_suspend NULL
  2073. #define mmc_blk_resume NULL
  2074. #endif
  2075. static struct mmc_driver mmc_driver = {
  2076. .drv = {
  2077. .name = "mmcblk",
  2078. },
  2079. .probe = mmc_blk_probe,
  2080. .remove = mmc_blk_remove,
  2081. .suspend = mmc_blk_suspend,
  2082. .resume = mmc_blk_resume,
  2083. };
  2084. static int __init mmc_blk_init(void)
  2085. {
  2086. int res;
  2087. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  2088. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  2089. max_devices = 256 / perdev_minors;
  2090. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2091. if (res)
  2092. goto out;
  2093. res = mmc_register_driver(&mmc_driver);
  2094. if (res)
  2095. goto out2;
  2096. return 0;
  2097. out2:
  2098. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2099. out:
  2100. return res;
  2101. }
  2102. static void __exit mmc_blk_exit(void)
  2103. {
  2104. mmc_unregister_driver(&mmc_driver);
  2105. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  2106. }
  2107. module_init(mmc_blk_init);
  2108. module_exit(mmc_blk_exit);
  2109. MODULE_LICENSE("GPL");
  2110. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");