block.c 65 KB

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